blob: 57c5c85f6272d19fd89affba12d293a73877669e [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, "
Harald Welteb9c758b2009-07-05 14:02:46 +020082 "header BLOB, "
Harald Welte7e310b12009-03-30 20:56:32 +000083 "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 Welte9176bd42009-07-23 18:46:00 +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 Welte9176bd42009-07-23 18:46:00 +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 Welte9176bd42009-07-23 18:46:00 +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 Welte9176bd42009-07-23 18:46:00 +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;
Harald Weltebe3e3782009-07-05 14:06:41 +0200238 case GSM_SUBSCRIBER_ID:
239 dbi_conn_quote_string_copy(conn, id, &quoted);
240 result = dbi_conn_queryf(conn,
241 "SELECT * FROM Subscriber "
242 "WHERE id = %s ", quoted);
243 free(quoted);
244 break;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000245 default:
246 printf("DB: Unknown query selector for Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000247 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000248 }
249 if (result==NULL) {
250 printf("DB: Failed to query Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000251 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000252 }
253 if (!dbi_result_next_row(result)) {
Holger Freyther1ef983b2009-02-22 20:33:09 +0000254 printf("DB: Failed to find the Subscriber. '%u' '%s'\n",
255 field, id);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000256 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000257 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000258 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000259
260 subscr = subscr_alloc();
Harald Welte9176bd42009-07-23 18:46:00 +0200261 subscr->net = net;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000262 subscr->id = dbi_result_get_ulonglong(result, "id");
Harald Welte75a983f2008-12-27 21:34:06 +0000263 string = dbi_result_get_string(result, "imsi");
264 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000265 strncpy(subscr->imsi, string, GSM_IMSI_LENGTH);
Harald Welte75a983f2008-12-27 21:34:06 +0000266
267 string = dbi_result_get_string(result, "tmsi");
268 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000269 strncpy(subscr->tmsi, string, GSM_TMSI_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000270
271 string = dbi_result_get_string(result, "name");
272 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000273 strncpy(subscr->name, string, GSM_NAME_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000274
275 string = dbi_result_get_string(result, "extension");
276 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000277 strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000278
Holger Freyther12aa50d2009-01-01 18:02:05 +0000279 subscr->lac = dbi_result_get_uint(result, "lac");
280 subscr->authorized = dbi_result_get_uint(result, "authorized");
Holger Freyther91754472009-06-09 08:52:41 +0000281 printf("DB: Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %s, EXTEN '%s', LAC %hu, AUTH %u\n",
282 subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension,
Holger Freyther12aa50d2009-01-01 18:02:05 +0000283 subscr->lac, subscr->authorized);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000284 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000285 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000286}
287
Holger Freyther12aa50d2009-01-01 18:02:05 +0000288int db_sync_subscriber(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000289 dbi_result result;
290 result = dbi_conn_queryf(conn,
291 "UPDATE Subscriber "
Jan Luebbe391d86e2008-12-27 22:33:34 +0000292 "SET updated = datetime('now'), "
Holger Freyther91754472009-06-09 08:52:41 +0000293 "tmsi = '%s', "
Jan Luebbe391d86e2008-12-27 22:33:34 +0000294 "lac = %i, "
295 "authorized = %i "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000296 "WHERE imsi = %s ",
Jan Luebbe6e2e5452008-12-27 16:47:55 +0000297 subscriber->tmsi, subscriber->lac, subscriber->authorized, subscriber->imsi
Jan Luebbe5c15c852008-12-27 15:59:25 +0000298 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000299
Jan Luebbe5c15c852008-12-27 15:59:25 +0000300 if (result==NULL) {
301 printf("DB: Failed to update Subscriber (by IMSI).\n");
302 return 1;
303 }
304 dbi_result_free(result);
305 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000306}
307
Harald Weltec2e302d2009-07-05 14:08:13 +0200308int db_sync_equipment(struct gsm_equipment *equip)
309{
310 dbi_result result;
311 unsigned char *cm2, *cm3;
312
313 dbi_conn_quote_binary_copy(conn, equip->classmark2,
314 equip->classmark2_len, &cm2);
315 dbi_conn_quote_binary_copy(conn, equip->classmark3,
316 equip->classmark3_len, &cm3);
317
318 result = dbi_conn_queryf(conn,
319 "UPDATE Equipment SET "
320 "updated = datetime('now'), "
321 "classmark1 = %u, "
322 "classmark2 = %s, "
323 "classmark3 = %s "
324 "WHERE imei = '%s' ",
325 equip->classmark1, cm2, cm3, equip->imei);
326
327 free(cm2);
328 free(cm3);
329
330 if (!result) {
331 printf("DB: Failed to update Equipment\n");
332 return -EIO;
333 }
334
335 dbi_result_free(result);
336 return 0;
337}
338
Jan Luebbe5c15c852008-12-27 15:59:25 +0000339int db_subscriber_alloc_tmsi(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000340 dbi_result result=NULL;
341 char* tmsi_quoted;
342 for (;;) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000343 sprintf(subscriber->tmsi, "%i", rand());
344 dbi_conn_quote_string_copy(conn, subscriber->tmsi, &tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000345 result = dbi_conn_queryf(conn,
346 "SELECT * FROM Subscriber "
347 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000348 tmsi_quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000349 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000350 free(tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000351 if (result==NULL) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000352 printf("DB: Failed to query Subscriber while allocating new TMSI.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000353 return 1;
354 }
Jan Luebbe5c15c852008-12-27 15:59:25 +0000355 if (dbi_result_get_numrows(result)){
356 dbi_result_free(result);
357 continue;
358 }
359 if (!dbi_result_next_row(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000360 dbi_result_free(result);
361 printf("DB: Allocated TMSI %s for IMSI %s.\n", subscriber->tmsi, subscriber->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000362 return db_sync_subscriber(subscriber);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000363 }
364 dbi_result_free(result);
365 }
366 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000367}
368
Jan Luebbefac25fc2008-12-27 18:04:34 +0000369int db_subscriber_assoc_imei(struct gsm_subscriber* subscriber, char imei[GSM_IMEI_LENGTH]) {
370 u_int64_t equipment_id, watch_id;
371 dbi_result result;
372
Harald Weltec2e302d2009-07-05 14:08:13 +0200373 strncpy(subscriber->equipment.imei, imei,
374 sizeof(subscriber->equipment.imei)-1),
375
Jan Luebbefac25fc2008-12-27 18:04:34 +0000376 result = dbi_conn_queryf(conn,
377 "INSERT OR IGNORE INTO Equipment "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000378 "(imei, created, updated) "
Jan Luebbefac25fc2008-12-27 18:04:34 +0000379 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000380 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbefac25fc2008-12-27 18:04:34 +0000381 imei
382 );
383 if (result==NULL) {
384 printf("DB: Failed to create Equipment by IMEI.\n");
385 return 1;
386 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000387 equipment_id = 0;
388 if (dbi_result_get_numrows_affected(result)) {
389 equipment_id = dbi_conn_sequence_last(conn, NULL);
390 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000391 dbi_result_free(result);
392 if (equipment_id) {
393 printf("DB: New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
394 }
395 else {
396 result = dbi_conn_queryf(conn,
397 "SELECT id FROM Equipment "
398 "WHERE imei = %s ",
399 imei
400 );
401 if (result==NULL) {
402 printf("DB: Failed to query Equipment by IMEI.\n");
403 return 1;
404 }
405 if (!dbi_result_next_row(result)) {
406 printf("DB: Failed to find the Equipment.\n");
407 dbi_result_free(result);
408 return 1;
409 }
410 equipment_id = dbi_result_get_ulonglong(result, "id");
411 dbi_result_free(result);
412 }
413
414 result = dbi_conn_queryf(conn,
415 "INSERT OR IGNORE INTO EquipmentWatch "
416 "(subscriber_id, equipment_id, created, updated) "
417 "VALUES "
418 "(%llu, %llu, datetime('now'), datetime('now')) ",
419 subscriber->id, equipment_id
420 );
421 if (result==NULL) {
422 printf("DB: Failed to create EquipmentWatch.\n");
423 return 1;
424 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000425 watch_id = 0;
426 if (dbi_result_get_numrows_affected(result)) {
427 watch_id = dbi_conn_sequence_last(conn, NULL);
428 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000429 dbi_result_free(result);
430 if (watch_id) {
431 printf("DB: New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
432 }
433 else {
434 result = dbi_conn_queryf(conn,
435 "UPDATE EquipmentWatch "
436 "SET updated = datetime('now') "
437 "WHERE subscriber_id = %llu AND equipment_id = %llu ",
438 subscriber->id, equipment_id
439 );
440 if (result==NULL) {
441 printf("DB: Failed to update EquipmentWatch.\n");
442 return 1;
443 }
444 dbi_result_free(result);
445 printf("DB: Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
446 }
447
448 return 0;
449}
450
Harald Welte7e310b12009-03-30 20:56:32 +0000451/* store an [unsent] SMS to the database */
452int db_sms_store(struct gsm_sms *sms)
453{
454 dbi_result result;
455 char *q_text;
Harald Welteb9c758b2009-07-05 14:02:46 +0200456 unsigned char *q_header;
Harald Welte7e310b12009-03-30 20:56:32 +0000457
458 dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
Harald Welteb9c758b2009-07-05 14:02:46 +0200459 dbi_conn_quote_binary_copy(conn, sms->header, sms->header_len,
460 &q_header);
Harald Welte7e310b12009-03-30 20:56:32 +0000461 result = dbi_conn_queryf(conn,
462 "INSERT INTO SMS "
463 "(created,sender_id,receiver_id,header,text) VALUES "
464 "(datetime('now'),%llu,%llu,%s,%s)",
465 sms->sender->id,
466 sms->receiver ? sms->receiver->id : 0,
Harald Welteb9c758b2009-07-05 14:02:46 +0200467 q_header, q_text);
Harald Welte7e310b12009-03-30 20:56:32 +0000468 free(q_text);
Harald Welteb9c758b2009-07-05 14:02:46 +0200469 free(q_header);
Harald Welte7e310b12009-03-30 20:56:32 +0000470
471 if (!result)
472 return -EIO;
473
474 dbi_result_free(result);
475 return 0;
476}
477
478/* retrieve the next unsent SMS with ID >= min_id */
Harald Welte9176bd42009-07-23 18:46:00 +0200479struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, int min_id)
Harald Welte7e310b12009-03-30 20:56:32 +0000480{
481 dbi_result result;
Harald Weltebe3e3782009-07-05 14:06:41 +0200482 long long unsigned int sender_id, receiver_id;
Harald Welte7e310b12009-03-30 20:56:32 +0000483 struct gsm_sms *sms = malloc(sizeof(*sms));
Harald Weltebe3e3782009-07-05 14:06:41 +0200484 char *text;
485 char buf[32];
Harald Welte7e310b12009-03-30 20:56:32 +0000486
487 if (!sms) {
488 free(sms);
489 return NULL;
490 }
491
492 result = dbi_conn_queryf(conn,
493 "SELECT * FROM SMS "
494 "WHERE id >= %llu ORDER BY id", min_id);
495 if (!result) {
496 free(sms);
497 return NULL;
498 }
Harald Weltebe3e3782009-07-05 14:06:41 +0200499 sms->id = dbi_result_get_ulonglong(result, "id");
Harald Welte7e310b12009-03-30 20:56:32 +0000500
Harald Weltebe3e3782009-07-05 14:06:41 +0200501 sender_id = dbi_result_get_ulonglong(result, "sender_id");
502 sprintf(buf, "%llu", sender_id);
Harald Welte9176bd42009-07-23 18:46:00 +0200503 sms->sender = db_get_subscriber(net, GSM_SUBSCRIBER_ID, buf);
Harald Weltebe3e3782009-07-05 14:06:41 +0200504
505 receiver_id = dbi_result_get_ulonglong(result, "receiver_id");
506 sprintf(buf, "%llu", receiver_id);
Harald Welte9176bd42009-07-23 18:46:00 +0200507 sms->receiver = db_get_subscriber(net, GSM_SUBSCRIBER_ID, buf);
Harald Weltebe3e3782009-07-05 14:06:41 +0200508
509 /* FIXME: fill header */
510
511 text = dbi_result_get_string(result, "text");
512 if (text)
513 strncpy(sms->text, text, sizeof(sms->text));
Harald Welte7e310b12009-03-30 20:56:32 +0000514
515 dbi_result_free(result);
516 return sms;
517}
518
519/* mark a given SMS as read */
520int db_sms_mark_sent(struct gsm_sms *sms)
521{
522 dbi_result result;
523
524 result = dbi_conn_queryf(conn,
525 "UPDATE SMS "
526 "SET sent = datetime('now') "
527 "WHERE id = %llu", sms->id);
528 if (!result) {
529 printf("DB: Failed to mark SMS %llu as sent.\n", sms->id);
530 return 1;
531 }
532
533 dbi_result_free(result);
534 return 0;
535}