blob: 543f44ce2939a0dcb5c1c1cc35cbcec2cb1f8ca9 [file] [log] [blame]
Holger Freyther12aa50d2009-01-01 18:02:05 +00001/* Simple HLR/VLR database backend using dbi */
Jan Luebbefaaa49c2008-12-27 01:07:07 +00002/* (C) 2008 by Jan Luebbe <jluebbe@debian.org>
Holger Freyther12aa50d2009-01-01 18:02:05 +00003 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Weltec2e302d2009-07-05 14:08:13 +02004 * (C) 2009 by Harald Welte <laforge@gnumonks.org>
Jan Luebbefaaa49c2008-12-27 01:07:07 +00005 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
Harald Welte7e310b12009-03-30 20:56:32 +000023#include <openbsc/gsm_data.h>
Jan Luebbefaaa49c2008-12-27 01:07:07 +000024#include <openbsc/db.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000025
Holger Freytherbde36102008-12-28 22:51:39 +000026#include <libgen.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000027#include <stdio.h>
Jan Luebbe5c15c852008-12-27 15:59:25 +000028#include <stdlib.h>
29#include <string.h>
Harald Welte7e310b12009-03-30 20:56:32 +000030#include <errno.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000031#include <dbi/dbi.h>
32
Holger Freytherbde36102008-12-28 22:51:39 +000033static char *db_basename = NULL;
34static char *db_dirname = NULL;
Holger Freyther1d506c82009-04-19 06:35:20 +000035static dbi_conn conn;
Jan Luebbe7398eb92008-12-27 00:45:41 +000036
Harald Welte7e310b12009-03-30 20:56:32 +000037static char *create_stmts[] = {
38 "CREATE TABLE IF NOT EXISTS Meta ("
39 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
40 "key TEXT UNIQUE NOT NULL, "
41 "value TEXT NOT NULL"
42 ")",
43 "INSERT OR IGNORE INTO Meta "
44 "(key, value) "
45 "VALUES "
46 "('revision', '1')",
47 "CREATE TABLE IF NOT EXISTS Subscriber ("
48 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
49 "created TIMESTAMP NOT NULL, "
50 "updated TIMESTAMP NOT NULL, "
51 "imsi NUMERIC UNIQUE NOT NULL, "
52 "name TEXT, "
53 "extension TEXT UNIQUE, "
54 "authorized INTEGER NOT NULL DEFAULT 0, "
55 "tmsi TEXT UNIQUE, "
56 "lac INTEGER NOT NULL DEFAULT 0"
57 ")",
58 "CREATE TABLE IF NOT EXISTS Equipment ("
59 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
60 "created TIMESTAMP NOT NULL, "
61 "updated TIMESTAMP NOT NULL, "
62 "name TEXT, "
Harald Weltec2e302d2009-07-05 14:08:13 +020063 "classmark1 NUMERIC, "
64 "classmark2 BLOB, "
65 "classmark3 BLOB, "
Harald Welte7e310b12009-03-30 20:56:32 +000066 "imei NUMERIC UNIQUE NOT NULL"
67 ")",
68 "CREATE TABLE IF NOT EXISTS EquipmentWatch ("
69 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
70 "created TIMESTAMP NOT NULL, "
71 "updated TIMESTAMP NOT NULL, "
72 "subscriber_id NUMERIC NOT NULL, "
73 "equipment_id NUMERIC NOT NULL, "
74 "UNIQUE (subscriber_id, equipment_id) "
75 ")",
76 "CREATE TABLE IF NOT EXISTS SMS ("
77 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
78 "created TIMESTAMP NOT NULL, "
79 "sent TIMESTAMP, "
80 "sender_id NUMERIC NOT NULL, "
81 "receiver_id NUMERIC NOT NULL, "
82 "header NUMERIC, "
83 "text TEXT NOT NULL "
84 ")",
Holger Freytherc2995ea2009-04-19 06:35:23 +000085 "CREATE TABLE IF NOT EXISTS VLR ("
86 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
87 "created TIMESTAMP NOT NULL, "
88 "updated TIMESTAMP NOT NULL, "
89 "subscriber_id NUMERIC UNIQUE NOT NULL, "
90 "last_bts NUMERIC NOT NULL "
91 ")",
Harald Welte7e310b12009-03-30 20:56:32 +000092};
93
Holger Freyther12aa50d2009-01-01 18:02:05 +000094void db_error_func(dbi_conn conn, void* data) {
Jan Luebbe5c15c852008-12-27 15:59:25 +000095 const char* msg;
96 dbi_conn_error(conn, &msg);
97 printf("DBI: %s\n", msg);
Jan Luebbe7398eb92008-12-27 00:45:41 +000098}
99
Holger Freytherc7b86f92009-06-06 13:54:20 +0000100int db_init(const char *name) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000101 dbi_initialize(NULL);
102 conn = dbi_conn_new("sqlite3");
103 if (conn==NULL) {
104 printf("DB: Failed to create connection.\n");
105 return 1;
106 }
Jan Luebbe7398eb92008-12-27 00:45:41 +0000107
Holger Freyther12aa50d2009-01-01 18:02:05 +0000108 dbi_conn_error_handler( conn, db_error_func, NULL );
Jan Luebbe7398eb92008-12-27 00:45:41 +0000109
Jan Luebbe5c15c852008-12-27 15:59:25 +0000110 /* MySQL
111 dbi_conn_set_option(conn, "host", "localhost");
112 dbi_conn_set_option(conn, "username", "your_name");
113 dbi_conn_set_option(conn, "password", "your_password");
114 dbi_conn_set_option(conn, "dbname", "your_dbname");
115 dbi_conn_set_option(conn, "encoding", "UTF-8");
116 */
Jan Luebbe7398eb92008-12-27 00:45:41 +0000117
Jan Luebbe5c15c852008-12-27 15:59:25 +0000118 /* SqLite 3 */
Holger Freyther12aa50d2009-01-01 18:02:05 +0000119 db_basename = strdup(name);
120 db_dirname = strdup(name);
Holger Freytherbde36102008-12-28 22:51:39 +0000121 dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname));
122 dbi_conn_set_option(conn, "dbname", basename(db_basename));
Jan Luebbe7398eb92008-12-27 00:45:41 +0000123
Jan Luebbe5c15c852008-12-27 15:59:25 +0000124 if (dbi_conn_connect(conn) < 0) {
Holger Freytherbde36102008-12-28 22:51:39 +0000125 free(db_dirname);
126 free(db_basename);
127 db_dirname = db_basename = NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000128 return 1;
129 }
130
131 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000132}
133
134int db_prepare() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000135 dbi_result result;
Harald Welte7e310b12009-03-30 20:56:32 +0000136 int i;
Holger Freytherb4064bc2009-02-23 00:50:31 +0000137
Harald Welte7e310b12009-03-30 20:56:32 +0000138 for (i = 0; i < ARRAY_SIZE(create_stmts); i++) {
139 result = dbi_conn_query(conn, create_stmts[i]);
140 if (result==NULL) {
141 printf("DB: Failed to create some table.\n");
142 return 1;
143 }
144 dbi_result_free(result);
Holger Freytherb4064bc2009-02-23 00:50:31 +0000145 }
Holger Freytherb4064bc2009-02-23 00:50:31 +0000146
Jan Luebbe5c15c852008-12-27 15:59:25 +0000147 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000148}
149
150int db_fini() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000151 dbi_conn_close(conn);
152 dbi_shutdown();
Holger Freytherbde36102008-12-28 22:51:39 +0000153
154 if (db_dirname)
155 free(db_dirname);
156 if (db_basename)
157 free(db_basename);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000158 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000159}
160
Holger Freyther12aa50d2009-01-01 18:02:05 +0000161struct gsm_subscriber* db_create_subscriber(char *imsi) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000162 dbi_result result;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000163 struct gsm_subscriber* subscr;
164
165 /* Is this subscriber known in the db? */
166 subscr = db_get_subscriber(GSM_SUBSCRIBER_IMSI, imsi);
167 if (subscr) {
Harald Welte523200b2008-12-30 14:58:44 +0000168 result = dbi_conn_queryf(conn,
169 "UPDATE Subscriber set updated = datetime('now') "
170 "WHERE imsi = %s " , imsi);
171 if (result==NULL) {
172 printf("DB: failed to update timestamp\n");
173 } else {
174 dbi_result_free(result);
175 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000176 return subscr;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000177 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000178
179 subscr = subscr_alloc();
180 if (!subscr)
181 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000182 result = dbi_conn_queryf(conn,
Jan Luebbe391d86e2008-12-27 22:33:34 +0000183 "INSERT INTO Subscriber "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000184 "(imsi, created, updated) "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000185 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000186 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbe5c15c852008-12-27 15:59:25 +0000187 imsi
188 );
189 if (result==NULL) {
190 printf("DB: Failed to create Subscriber by IMSI.\n");
191 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000192 subscr->id = dbi_conn_sequence_last(conn, NULL);
193 strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000194 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000195 printf("DB: New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi);
196 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000197}
198
Holger Freytherca362a62009-01-04 21:05:01 +0000199struct gsm_subscriber *db_get_subscriber(enum gsm_subscriber_field field, const char *id) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000200 dbi_result result;
Jan Luebbe391d86e2008-12-27 22:33:34 +0000201 const char *string;
202 char *quoted;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000203 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000204
Jan Luebbe5c15c852008-12-27 15:59:25 +0000205 switch (field) {
206 case GSM_SUBSCRIBER_IMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000207 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000208 result = dbi_conn_queryf(conn,
209 "SELECT * FROM Subscriber "
210 "WHERE imsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000211 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000212 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000213 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000214 break;
215 case GSM_SUBSCRIBER_TMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000216 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000217 result = dbi_conn_queryf(conn,
218 "SELECT * FROM Subscriber "
219 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000220 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000221 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000222 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000223 break;
Holger Freyther9c564b82009-02-09 23:39:20 +0000224 case GSM_SUBSCRIBER_EXTENSION:
225 dbi_conn_quote_string_copy(conn, id, &quoted);
226 result = dbi_conn_queryf(conn,
227 "SELECT * FROM Subscriber "
228 "WHERE extension = %s ",
229 quoted
230 );
231 free(quoted);
232 break;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000233 default:
234 printf("DB: Unknown query selector for Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000235 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000236 }
237 if (result==NULL) {
238 printf("DB: Failed to query Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000239 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000240 }
241 if (!dbi_result_next_row(result)) {
Holger Freyther1ef983b2009-02-22 20:33:09 +0000242 printf("DB: Failed to find the Subscriber. '%u' '%s'\n",
243 field, id);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000244 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000245 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000246 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000247
248 subscr = subscr_alloc();
249 subscr->id = dbi_result_get_ulonglong(result, "id");
Harald Welte75a983f2008-12-27 21:34:06 +0000250 string = dbi_result_get_string(result, "imsi");
251 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000252 strncpy(subscr->imsi, string, GSM_IMSI_LENGTH);
Harald Welte75a983f2008-12-27 21:34:06 +0000253
254 string = dbi_result_get_string(result, "tmsi");
255 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000256 strncpy(subscr->tmsi, string, GSM_TMSI_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000257
258 string = dbi_result_get_string(result, "name");
259 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000260 strncpy(subscr->name, string, GSM_NAME_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000261
262 string = dbi_result_get_string(result, "extension");
263 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000264 strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000265
Holger Freyther12aa50d2009-01-01 18:02:05 +0000266 subscr->lac = dbi_result_get_uint(result, "lac");
267 subscr->authorized = dbi_result_get_uint(result, "authorized");
Holger Freyther91754472009-06-09 08:52:41 +0000268 printf("DB: Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %s, EXTEN '%s', LAC %hu, AUTH %u\n",
269 subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension,
Holger Freyther12aa50d2009-01-01 18:02:05 +0000270 subscr->lac, subscr->authorized);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000271 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000272 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000273}
274
Holger Freyther12aa50d2009-01-01 18:02:05 +0000275int db_sync_subscriber(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000276 dbi_result result;
277 result = dbi_conn_queryf(conn,
278 "UPDATE Subscriber "
Jan Luebbe391d86e2008-12-27 22:33:34 +0000279 "SET updated = datetime('now'), "
Holger Freyther91754472009-06-09 08:52:41 +0000280 "tmsi = '%s', "
Jan Luebbe391d86e2008-12-27 22:33:34 +0000281 "lac = %i, "
282 "authorized = %i "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000283 "WHERE imsi = %s ",
Jan Luebbe6e2e5452008-12-27 16:47:55 +0000284 subscriber->tmsi, subscriber->lac, subscriber->authorized, subscriber->imsi
Jan Luebbe5c15c852008-12-27 15:59:25 +0000285 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000286
Jan Luebbe5c15c852008-12-27 15:59:25 +0000287 if (result==NULL) {
288 printf("DB: Failed to update Subscriber (by IMSI).\n");
289 return 1;
290 }
291 dbi_result_free(result);
292 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000293}
294
Harald Weltec2e302d2009-07-05 14:08:13 +0200295int db_sync_equipment(struct gsm_equipment *equip)
296{
297 dbi_result result;
298 unsigned char *cm2, *cm3;
299
300 dbi_conn_quote_binary_copy(conn, equip->classmark2,
301 equip->classmark2_len, &cm2);
302 dbi_conn_quote_binary_copy(conn, equip->classmark3,
303 equip->classmark3_len, &cm3);
304
305 result = dbi_conn_queryf(conn,
306 "UPDATE Equipment SET "
307 "updated = datetime('now'), "
308 "classmark1 = %u, "
309 "classmark2 = %s, "
310 "classmark3 = %s "
311 "WHERE imei = '%s' ",
312 equip->classmark1, cm2, cm3, equip->imei);
313
314 free(cm2);
315 free(cm3);
316
317 if (!result) {
318 printf("DB: Failed to update Equipment\n");
319 return -EIO;
320 }
321
322 dbi_result_free(result);
323 return 0;
324}
325
Jan Luebbe5c15c852008-12-27 15:59:25 +0000326int db_subscriber_alloc_tmsi(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000327 dbi_result result=NULL;
328 char* tmsi_quoted;
329 for (;;) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000330 sprintf(subscriber->tmsi, "%i", rand());
331 dbi_conn_quote_string_copy(conn, subscriber->tmsi, &tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000332 result = dbi_conn_queryf(conn,
333 "SELECT * FROM Subscriber "
334 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000335 tmsi_quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000336 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000337 free(tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000338 if (result==NULL) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000339 printf("DB: Failed to query Subscriber while allocating new TMSI.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000340 return 1;
341 }
Jan Luebbe5c15c852008-12-27 15:59:25 +0000342 if (dbi_result_get_numrows(result)){
343 dbi_result_free(result);
344 continue;
345 }
346 if (!dbi_result_next_row(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000347 dbi_result_free(result);
348 printf("DB: Allocated TMSI %s for IMSI %s.\n", subscriber->tmsi, subscriber->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000349 return db_sync_subscriber(subscriber);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000350 }
351 dbi_result_free(result);
352 }
353 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000354}
355
Jan Luebbefac25fc2008-12-27 18:04:34 +0000356int db_subscriber_assoc_imei(struct gsm_subscriber* subscriber, char imei[GSM_IMEI_LENGTH]) {
357 u_int64_t equipment_id, watch_id;
358 dbi_result result;
359
Harald Weltec2e302d2009-07-05 14:08:13 +0200360 strncpy(subscriber->equipment.imei, imei,
361 sizeof(subscriber->equipment.imei)-1),
362
Jan Luebbefac25fc2008-12-27 18:04:34 +0000363 result = dbi_conn_queryf(conn,
364 "INSERT OR IGNORE INTO Equipment "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000365 "(imei, created, updated) "
Jan Luebbefac25fc2008-12-27 18:04:34 +0000366 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000367 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbefac25fc2008-12-27 18:04:34 +0000368 imei
369 );
370 if (result==NULL) {
371 printf("DB: Failed to create Equipment by IMEI.\n");
372 return 1;
373 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000374 equipment_id = 0;
375 if (dbi_result_get_numrows_affected(result)) {
376 equipment_id = dbi_conn_sequence_last(conn, NULL);
377 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000378 dbi_result_free(result);
379 if (equipment_id) {
380 printf("DB: New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
381 }
382 else {
383 result = dbi_conn_queryf(conn,
384 "SELECT id FROM Equipment "
385 "WHERE imei = %s ",
386 imei
387 );
388 if (result==NULL) {
389 printf("DB: Failed to query Equipment by IMEI.\n");
390 return 1;
391 }
392 if (!dbi_result_next_row(result)) {
393 printf("DB: Failed to find the Equipment.\n");
394 dbi_result_free(result);
395 return 1;
396 }
397 equipment_id = dbi_result_get_ulonglong(result, "id");
398 dbi_result_free(result);
399 }
400
401 result = dbi_conn_queryf(conn,
402 "INSERT OR IGNORE INTO EquipmentWatch "
403 "(subscriber_id, equipment_id, created, updated) "
404 "VALUES "
405 "(%llu, %llu, datetime('now'), datetime('now')) ",
406 subscriber->id, equipment_id
407 );
408 if (result==NULL) {
409 printf("DB: Failed to create EquipmentWatch.\n");
410 return 1;
411 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000412 watch_id = 0;
413 if (dbi_result_get_numrows_affected(result)) {
414 watch_id = dbi_conn_sequence_last(conn, NULL);
415 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000416 dbi_result_free(result);
417 if (watch_id) {
418 printf("DB: New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
419 }
420 else {
421 result = dbi_conn_queryf(conn,
422 "UPDATE EquipmentWatch "
423 "SET updated = datetime('now') "
424 "WHERE subscriber_id = %llu AND equipment_id = %llu ",
425 subscriber->id, equipment_id
426 );
427 if (result==NULL) {
428 printf("DB: Failed to update EquipmentWatch.\n");
429 return 1;
430 }
431 dbi_result_free(result);
432 printf("DB: Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
433 }
434
435 return 0;
436}
437
Harald Welte7e310b12009-03-30 20:56:32 +0000438/* store an [unsent] SMS to the database */
439int db_sms_store(struct gsm_sms *sms)
440{
441 dbi_result result;
442 char *q_text;
443
444 dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
445 result = dbi_conn_queryf(conn,
446 "INSERT INTO SMS "
447 "(created,sender_id,receiver_id,header,text) VALUES "
448 "(datetime('now'),%llu,%llu,%s,%s)",
449 sms->sender->id,
450 sms->receiver ? sms->receiver->id : 0,
451 NULL, q_text);
452 free(q_text);
453
454 if (!result)
455 return -EIO;
456
457 dbi_result_free(result);
458 return 0;
459}
460
461/* retrieve the next unsent SMS with ID >= min_id */
462struct gsm_sms *db_sms_get_unsent(int min_id)
463{
464 dbi_result result;
465 struct gsm_sms *sms = malloc(sizeof(*sms));
466
467 if (!sms) {
468 free(sms);
469 return NULL;
470 }
471
472 result = dbi_conn_queryf(conn,
473 "SELECT * FROM SMS "
474 "WHERE id >= %llu ORDER BY id", min_id);
475 if (!result) {
476 free(sms);
477 return NULL;
478 }
479
480 /* FIXME: fill gsm_sms from database */
481
482 dbi_result_free(result);
483 return sms;
484}
485
486/* mark a given SMS as read */
487int db_sms_mark_sent(struct gsm_sms *sms)
488{
489 dbi_result result;
490
491 result = dbi_conn_queryf(conn,
492 "UPDATE SMS "
493 "SET sent = datetime('now') "
494 "WHERE id = %llu", sms->id);
495 if (!result) {
496 printf("DB: Failed to mark SMS %llu as sent.\n", sms->id);
497 return 1;
498 }
499
500 dbi_result_free(result);
501 return 0;
502}