blob: fc2950a364bc758e72d9b2dc8c9ff75f1d03375b [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
Harald Welte761e9442009-07-23 19:21:02 +0200161struct gsm_subscriber* db_create_subscriber(struct gsm_network *net, char *imsi)
162{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000163 dbi_result result;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000164 struct gsm_subscriber* subscr;
165
166 /* Is this subscriber known in the db? */
Harald Welte761e9442009-07-23 19:21:02 +0200167 subscr = db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000168 if (subscr) {
Harald Welte523200b2008-12-30 14:58:44 +0000169 result = dbi_conn_queryf(conn,
170 "UPDATE Subscriber set updated = datetime('now') "
171 "WHERE imsi = %s " , imsi);
172 if (result==NULL) {
173 printf("DB: failed to update timestamp\n");
174 } else {
175 dbi_result_free(result);
176 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000177 return subscr;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000178 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000179
180 subscr = subscr_alloc();
181 if (!subscr)
182 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000183 result = dbi_conn_queryf(conn,
Jan Luebbe391d86e2008-12-27 22:33:34 +0000184 "INSERT INTO Subscriber "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000185 "(imsi, created, updated) "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000186 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000187 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbe5c15c852008-12-27 15:59:25 +0000188 imsi
189 );
190 if (result==NULL) {
191 printf("DB: Failed to create Subscriber by IMSI.\n");
192 }
Harald Welte761e9442009-07-23 19:21:02 +0200193 subscr->net = net;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000194 subscr->id = dbi_conn_sequence_last(conn, NULL);
195 strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000196 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000197 printf("DB: New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi);
198 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000199}
200
Harald Welte761e9442009-07-23 19:21:02 +0200201struct gsm_subscriber *db_get_subscriber(struct gsm_network *net,
202 enum gsm_subscriber_field field,
203 const char *id)
204{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000205 dbi_result result;
Jan Luebbe391d86e2008-12-27 22:33:34 +0000206 const char *string;
207 char *quoted;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000208 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000209
Jan Luebbe5c15c852008-12-27 15:59:25 +0000210 switch (field) {
211 case GSM_SUBSCRIBER_IMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000212 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000213 result = dbi_conn_queryf(conn,
214 "SELECT * FROM Subscriber "
215 "WHERE imsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000216 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000217 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000218 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000219 break;
220 case GSM_SUBSCRIBER_TMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000221 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000222 result = dbi_conn_queryf(conn,
223 "SELECT * FROM Subscriber "
224 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000225 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000226 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000227 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000228 break;
Holger Freyther9c564b82009-02-09 23:39:20 +0000229 case GSM_SUBSCRIBER_EXTENSION:
230 dbi_conn_quote_string_copy(conn, id, &quoted);
231 result = dbi_conn_queryf(conn,
232 "SELECT * FROM Subscriber "
233 "WHERE extension = %s ",
234 quoted
235 );
236 free(quoted);
237 break;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000238 default:
239 printf("DB: Unknown query selector for Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000240 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000241 }
242 if (result==NULL) {
243 printf("DB: Failed to query Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000244 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000245 }
246 if (!dbi_result_next_row(result)) {
Holger Freyther1ef983b2009-02-22 20:33:09 +0000247 printf("DB: Failed to find the Subscriber. '%u' '%s'\n",
248 field, id);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000249 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000250 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000251 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000252
253 subscr = subscr_alloc();
Harald Welte761e9442009-07-23 19:21:02 +0200254 subscr->net = net;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000255 subscr->id = dbi_result_get_ulonglong(result, "id");
Harald Welte75a983f2008-12-27 21:34:06 +0000256 string = dbi_result_get_string(result, "imsi");
257 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000258 strncpy(subscr->imsi, string, GSM_IMSI_LENGTH);
Harald Welte75a983f2008-12-27 21:34:06 +0000259
260 string = dbi_result_get_string(result, "tmsi");
261 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000262 strncpy(subscr->tmsi, string, GSM_TMSI_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000263
264 string = dbi_result_get_string(result, "name");
265 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000266 strncpy(subscr->name, string, GSM_NAME_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000267
268 string = dbi_result_get_string(result, "extension");
269 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000270 strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000271
Holger Freyther12aa50d2009-01-01 18:02:05 +0000272 subscr->lac = dbi_result_get_uint(result, "lac");
273 subscr->authorized = dbi_result_get_uint(result, "authorized");
Holger Freyther91754472009-06-09 08:52:41 +0000274 printf("DB: Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %s, EXTEN '%s', LAC %hu, AUTH %u\n",
275 subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension,
Holger Freyther12aa50d2009-01-01 18:02:05 +0000276 subscr->lac, subscr->authorized);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000277 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000278 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000279}
280
Holger Freyther12aa50d2009-01-01 18:02:05 +0000281int db_sync_subscriber(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000282 dbi_result result;
283 result = dbi_conn_queryf(conn,
284 "UPDATE Subscriber "
Jan Luebbe391d86e2008-12-27 22:33:34 +0000285 "SET updated = datetime('now'), "
Holger Freyther91754472009-06-09 08:52:41 +0000286 "tmsi = '%s', "
Jan Luebbe391d86e2008-12-27 22:33:34 +0000287 "lac = %i, "
288 "authorized = %i "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000289 "WHERE imsi = %s ",
Jan Luebbe6e2e5452008-12-27 16:47:55 +0000290 subscriber->tmsi, subscriber->lac, subscriber->authorized, subscriber->imsi
Jan Luebbe5c15c852008-12-27 15:59:25 +0000291 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000292
Jan Luebbe5c15c852008-12-27 15:59:25 +0000293 if (result==NULL) {
294 printf("DB: Failed to update Subscriber (by IMSI).\n");
295 return 1;
296 }
297 dbi_result_free(result);
298 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000299}
300
Harald Weltec2e302d2009-07-05 14:08:13 +0200301int db_sync_equipment(struct gsm_equipment *equip)
302{
303 dbi_result result;
304 unsigned char *cm2, *cm3;
305
306 dbi_conn_quote_binary_copy(conn, equip->classmark2,
307 equip->classmark2_len, &cm2);
308 dbi_conn_quote_binary_copy(conn, equip->classmark3,
309 equip->classmark3_len, &cm3);
310
311 result = dbi_conn_queryf(conn,
312 "UPDATE Equipment SET "
313 "updated = datetime('now'), "
314 "classmark1 = %u, "
315 "classmark2 = %s, "
316 "classmark3 = %s "
317 "WHERE imei = '%s' ",
318 equip->classmark1, cm2, cm3, equip->imei);
319
320 free(cm2);
321 free(cm3);
322
323 if (!result) {
324 printf("DB: Failed to update Equipment\n");
325 return -EIO;
326 }
327
328 dbi_result_free(result);
329 return 0;
330}
331
Jan Luebbe5c15c852008-12-27 15:59:25 +0000332int db_subscriber_alloc_tmsi(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000333 dbi_result result=NULL;
334 char* tmsi_quoted;
335 for (;;) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000336 sprintf(subscriber->tmsi, "%i", rand());
337 dbi_conn_quote_string_copy(conn, subscriber->tmsi, &tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000338 result = dbi_conn_queryf(conn,
339 "SELECT * FROM Subscriber "
340 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000341 tmsi_quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000342 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000343 free(tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000344 if (result==NULL) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000345 printf("DB: Failed to query Subscriber while allocating new TMSI.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000346 return 1;
347 }
Jan Luebbe5c15c852008-12-27 15:59:25 +0000348 if (dbi_result_get_numrows(result)){
349 dbi_result_free(result);
350 continue;
351 }
352 if (!dbi_result_next_row(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000353 dbi_result_free(result);
354 printf("DB: Allocated TMSI %s for IMSI %s.\n", subscriber->tmsi, subscriber->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000355 return db_sync_subscriber(subscriber);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000356 }
357 dbi_result_free(result);
358 }
359 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000360}
361
Jan Luebbefac25fc2008-12-27 18:04:34 +0000362int db_subscriber_assoc_imei(struct gsm_subscriber* subscriber, char imei[GSM_IMEI_LENGTH]) {
363 u_int64_t equipment_id, watch_id;
364 dbi_result result;
365
Harald Weltec2e302d2009-07-05 14:08:13 +0200366 strncpy(subscriber->equipment.imei, imei,
367 sizeof(subscriber->equipment.imei)-1),
368
Jan Luebbefac25fc2008-12-27 18:04:34 +0000369 result = dbi_conn_queryf(conn,
370 "INSERT OR IGNORE INTO Equipment "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000371 "(imei, created, updated) "
Jan Luebbefac25fc2008-12-27 18:04:34 +0000372 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000373 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbefac25fc2008-12-27 18:04:34 +0000374 imei
375 );
376 if (result==NULL) {
377 printf("DB: Failed to create Equipment by IMEI.\n");
378 return 1;
379 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000380 equipment_id = 0;
381 if (dbi_result_get_numrows_affected(result)) {
382 equipment_id = dbi_conn_sequence_last(conn, NULL);
383 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000384 dbi_result_free(result);
385 if (equipment_id) {
386 printf("DB: New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
387 }
388 else {
389 result = dbi_conn_queryf(conn,
390 "SELECT id FROM Equipment "
391 "WHERE imei = %s ",
392 imei
393 );
394 if (result==NULL) {
395 printf("DB: Failed to query Equipment by IMEI.\n");
396 return 1;
397 }
398 if (!dbi_result_next_row(result)) {
399 printf("DB: Failed to find the Equipment.\n");
400 dbi_result_free(result);
401 return 1;
402 }
403 equipment_id = dbi_result_get_ulonglong(result, "id");
404 dbi_result_free(result);
405 }
406
407 result = dbi_conn_queryf(conn,
408 "INSERT OR IGNORE INTO EquipmentWatch "
409 "(subscriber_id, equipment_id, created, updated) "
410 "VALUES "
411 "(%llu, %llu, datetime('now'), datetime('now')) ",
412 subscriber->id, equipment_id
413 );
414 if (result==NULL) {
415 printf("DB: Failed to create EquipmentWatch.\n");
416 return 1;
417 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000418 watch_id = 0;
419 if (dbi_result_get_numrows_affected(result)) {
420 watch_id = dbi_conn_sequence_last(conn, NULL);
421 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000422 dbi_result_free(result);
423 if (watch_id) {
424 printf("DB: New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
425 }
426 else {
427 result = dbi_conn_queryf(conn,
428 "UPDATE EquipmentWatch "
429 "SET updated = datetime('now') "
430 "WHERE subscriber_id = %llu AND equipment_id = %llu ",
431 subscriber->id, equipment_id
432 );
433 if (result==NULL) {
434 printf("DB: Failed to update EquipmentWatch.\n");
435 return 1;
436 }
437 dbi_result_free(result);
438 printf("DB: Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
439 }
440
441 return 0;
442}
443
Harald Welte7e310b12009-03-30 20:56:32 +0000444/* store an [unsent] SMS to the database */
445int db_sms_store(struct gsm_sms *sms)
446{
447 dbi_result result;
448 char *q_text;
449
450 dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
451 result = dbi_conn_queryf(conn,
452 "INSERT INTO SMS "
453 "(created,sender_id,receiver_id,header,text) VALUES "
454 "(datetime('now'),%llu,%llu,%s,%s)",
455 sms->sender->id,
456 sms->receiver ? sms->receiver->id : 0,
457 NULL, q_text);
458 free(q_text);
459
460 if (!result)
461 return -EIO;
462
463 dbi_result_free(result);
464 return 0;
465}
466
467/* retrieve the next unsent SMS with ID >= min_id */
Harald Welte761e9442009-07-23 19:21:02 +0200468struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, int min_id)
Harald Welte7e310b12009-03-30 20:56:32 +0000469{
470 dbi_result result;
471 struct gsm_sms *sms = malloc(sizeof(*sms));
472
473 if (!sms) {
474 free(sms);
475 return NULL;
476 }
477
478 result = dbi_conn_queryf(conn,
479 "SELECT * FROM SMS "
480 "WHERE id >= %llu ORDER BY id", min_id);
481 if (!result) {
482 free(sms);
483 return NULL;
484 }
485
486 /* FIXME: fill gsm_sms from database */
487
488 dbi_result_free(result);
489 return sms;
490}
491
492/* mark a given SMS as read */
493int db_sms_mark_sent(struct gsm_sms *sms)
494{
495 dbi_result result;
496
497 result = dbi_conn_queryf(conn,
498 "UPDATE SMS "
499 "SET sent = datetime('now') "
500 "WHERE id = %llu", sms->id);
501 if (!result) {
502 printf("DB: Failed to mark SMS %llu as sent.\n", sms->id);
503 return 1;
504 }
505
506 dbi_result_free(result);
507 return 0;
508}