blob: 03b281a07db66efe856b5eb033d82ed6528d13e5 [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, "
Harald Weltef3efc592009-07-27 20:11:35 +020080 "valid_until TIMESTAMP, "
81 "reply_path_req NUMERIC NOT NULL, "
82 "status_rep_req NUMERIC NOT NULL, "
83 "protocol_id NUMERIC NOT NULL, "
84 "data_coding_scheme NUMERIC NOT NULL, "
Harald Welte7e310b12009-03-30 20:56:32 +000085 "sender_id NUMERIC NOT NULL, "
86 "receiver_id NUMERIC NOT NULL, "
Harald Welteb9c758b2009-07-05 14:02:46 +020087 "header BLOB, "
Harald Welte7e310b12009-03-30 20:56:32 +000088 "text TEXT NOT NULL "
89 ")",
Holger Freytherc2995ea2009-04-19 06:35:23 +000090 "CREATE TABLE IF NOT EXISTS VLR ("
91 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
92 "created TIMESTAMP NOT NULL, "
93 "updated TIMESTAMP NOT NULL, "
94 "subscriber_id NUMERIC UNIQUE NOT NULL, "
95 "last_bts NUMERIC NOT NULL "
96 ")",
Harald Welte7e310b12009-03-30 20:56:32 +000097};
98
Holger Freyther12aa50d2009-01-01 18:02:05 +000099void db_error_func(dbi_conn conn, void* data) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000100 const char* msg;
101 dbi_conn_error(conn, &msg);
102 printf("DBI: %s\n", msg);
Jan Luebbe7398eb92008-12-27 00:45:41 +0000103}
104
Holger Freytherc7b86f92009-06-06 13:54:20 +0000105int db_init(const char *name) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000106 dbi_initialize(NULL);
107 conn = dbi_conn_new("sqlite3");
108 if (conn==NULL) {
109 printf("DB: Failed to create connection.\n");
110 return 1;
111 }
Jan Luebbe7398eb92008-12-27 00:45:41 +0000112
Holger Freyther12aa50d2009-01-01 18:02:05 +0000113 dbi_conn_error_handler( conn, db_error_func, NULL );
Jan Luebbe7398eb92008-12-27 00:45:41 +0000114
Jan Luebbe5c15c852008-12-27 15:59:25 +0000115 /* MySQL
116 dbi_conn_set_option(conn, "host", "localhost");
117 dbi_conn_set_option(conn, "username", "your_name");
118 dbi_conn_set_option(conn, "password", "your_password");
119 dbi_conn_set_option(conn, "dbname", "your_dbname");
120 dbi_conn_set_option(conn, "encoding", "UTF-8");
121 */
Jan Luebbe7398eb92008-12-27 00:45:41 +0000122
Jan Luebbe5c15c852008-12-27 15:59:25 +0000123 /* SqLite 3 */
Holger Freyther12aa50d2009-01-01 18:02:05 +0000124 db_basename = strdup(name);
125 db_dirname = strdup(name);
Holger Freytherbde36102008-12-28 22:51:39 +0000126 dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname));
127 dbi_conn_set_option(conn, "dbname", basename(db_basename));
Jan Luebbe7398eb92008-12-27 00:45:41 +0000128
Jan Luebbe5c15c852008-12-27 15:59:25 +0000129 if (dbi_conn_connect(conn) < 0) {
Holger Freytherbde36102008-12-28 22:51:39 +0000130 free(db_dirname);
131 free(db_basename);
132 db_dirname = db_basename = NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000133 return 1;
134 }
135
136 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000137}
138
139int db_prepare() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000140 dbi_result result;
Harald Welte7e310b12009-03-30 20:56:32 +0000141 int i;
Holger Freytherb4064bc2009-02-23 00:50:31 +0000142
Harald Welte7e310b12009-03-30 20:56:32 +0000143 for (i = 0; i < ARRAY_SIZE(create_stmts); i++) {
144 result = dbi_conn_query(conn, create_stmts[i]);
145 if (result==NULL) {
146 printf("DB: Failed to create some table.\n");
147 return 1;
148 }
149 dbi_result_free(result);
Holger Freytherb4064bc2009-02-23 00:50:31 +0000150 }
Holger Freytherb4064bc2009-02-23 00:50:31 +0000151
Jan Luebbe5c15c852008-12-27 15:59:25 +0000152 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000153}
154
155int db_fini() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000156 dbi_conn_close(conn);
157 dbi_shutdown();
Holger Freytherbde36102008-12-28 22:51:39 +0000158
159 if (db_dirname)
160 free(db_dirname);
161 if (db_basename)
162 free(db_basename);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000163 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000164}
165
Harald Welte9176bd42009-07-23 18:46:00 +0200166struct gsm_subscriber* db_create_subscriber(struct gsm_network *net, char *imsi)
167{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000168 dbi_result result;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000169 struct gsm_subscriber* subscr;
170
171 /* Is this subscriber known in the db? */
Harald Welte9176bd42009-07-23 18:46:00 +0200172 subscr = db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000173 if (subscr) {
Harald Welte523200b2008-12-30 14:58:44 +0000174 result = dbi_conn_queryf(conn,
175 "UPDATE Subscriber set updated = datetime('now') "
176 "WHERE imsi = %s " , imsi);
177 if (result==NULL) {
178 printf("DB: failed to update timestamp\n");
179 } else {
180 dbi_result_free(result);
181 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000182 return subscr;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000183 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000184
185 subscr = subscr_alloc();
186 if (!subscr)
187 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000188 result = dbi_conn_queryf(conn,
Jan Luebbe391d86e2008-12-27 22:33:34 +0000189 "INSERT INTO Subscriber "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000190 "(imsi, created, updated) "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000191 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000192 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbe5c15c852008-12-27 15:59:25 +0000193 imsi
194 );
195 if (result==NULL) {
196 printf("DB: Failed to create Subscriber by IMSI.\n");
197 }
Harald Welte9176bd42009-07-23 18:46:00 +0200198 subscr->net = net;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000199 subscr->id = dbi_conn_sequence_last(conn, NULL);
200 strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000201 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000202 printf("DB: New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi);
203 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000204}
205
Harald Welte9176bd42009-07-23 18:46:00 +0200206struct gsm_subscriber *db_get_subscriber(struct gsm_network *net,
207 enum gsm_subscriber_field field,
208 const char *id)
209{
Jan Luebbe5c15c852008-12-27 15:59:25 +0000210 dbi_result result;
Jan Luebbe391d86e2008-12-27 22:33:34 +0000211 const char *string;
212 char *quoted;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000213 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000214
Jan Luebbe5c15c852008-12-27 15:59:25 +0000215 switch (field) {
216 case GSM_SUBSCRIBER_IMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000217 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000218 result = dbi_conn_queryf(conn,
219 "SELECT * FROM Subscriber "
220 "WHERE imsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000221 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000222 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000223 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000224 break;
225 case GSM_SUBSCRIBER_TMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000226 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000227 result = dbi_conn_queryf(conn,
228 "SELECT * FROM Subscriber "
229 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000230 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000231 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000232 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000233 break;
Holger Freyther9c564b82009-02-09 23:39:20 +0000234 case GSM_SUBSCRIBER_EXTENSION:
235 dbi_conn_quote_string_copy(conn, id, &quoted);
236 result = dbi_conn_queryf(conn,
237 "SELECT * FROM Subscriber "
238 "WHERE extension = %s ",
239 quoted
240 );
241 free(quoted);
242 break;
Harald Weltebe3e3782009-07-05 14:06:41 +0200243 case GSM_SUBSCRIBER_ID:
244 dbi_conn_quote_string_copy(conn, id, &quoted);
245 result = dbi_conn_queryf(conn,
246 "SELECT * FROM Subscriber "
247 "WHERE id = %s ", quoted);
248 free(quoted);
249 break;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000250 default:
251 printf("DB: Unknown query selector for Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000252 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000253 }
254 if (result==NULL) {
255 printf("DB: Failed to query Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000256 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000257 }
258 if (!dbi_result_next_row(result)) {
Holger Freyther1ef983b2009-02-22 20:33:09 +0000259 printf("DB: Failed to find the Subscriber. '%u' '%s'\n",
260 field, id);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000261 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000262 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000263 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000264
265 subscr = subscr_alloc();
Harald Welte9176bd42009-07-23 18:46:00 +0200266 subscr->net = net;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000267 subscr->id = dbi_result_get_ulonglong(result, "id");
Harald Welte75a983f2008-12-27 21:34:06 +0000268 string = dbi_result_get_string(result, "imsi");
269 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000270 strncpy(subscr->imsi, string, GSM_IMSI_LENGTH);
Harald Welte75a983f2008-12-27 21:34:06 +0000271
272 string = dbi_result_get_string(result, "tmsi");
273 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000274 strncpy(subscr->tmsi, string, GSM_TMSI_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000275
276 string = dbi_result_get_string(result, "name");
277 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000278 strncpy(subscr->name, string, GSM_NAME_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000279
280 string = dbi_result_get_string(result, "extension");
281 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000282 strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000283
Holger Freyther12aa50d2009-01-01 18:02:05 +0000284 subscr->lac = dbi_result_get_uint(result, "lac");
285 subscr->authorized = dbi_result_get_uint(result, "authorized");
Holger Freyther91754472009-06-09 08:52:41 +0000286 printf("DB: Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %s, EXTEN '%s', LAC %hu, AUTH %u\n",
287 subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension,
Holger Freyther12aa50d2009-01-01 18:02:05 +0000288 subscr->lac, subscr->authorized);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000289 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000290 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000291}
292
Holger Freyther12aa50d2009-01-01 18:02:05 +0000293int db_sync_subscriber(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000294 dbi_result result;
295 result = dbi_conn_queryf(conn,
296 "UPDATE Subscriber "
Jan Luebbe391d86e2008-12-27 22:33:34 +0000297 "SET updated = datetime('now'), "
Holger Freyther91754472009-06-09 08:52:41 +0000298 "tmsi = '%s', "
Jan Luebbe391d86e2008-12-27 22:33:34 +0000299 "lac = %i, "
300 "authorized = %i "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000301 "WHERE imsi = %s ",
Jan Luebbe6e2e5452008-12-27 16:47:55 +0000302 subscriber->tmsi, subscriber->lac, subscriber->authorized, subscriber->imsi
Jan Luebbe5c15c852008-12-27 15:59:25 +0000303 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000304
Jan Luebbe5c15c852008-12-27 15:59:25 +0000305 if (result==NULL) {
306 printf("DB: Failed to update Subscriber (by IMSI).\n");
307 return 1;
308 }
309 dbi_result_free(result);
310 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000311}
312
Harald Weltec2e302d2009-07-05 14:08:13 +0200313int db_sync_equipment(struct gsm_equipment *equip)
314{
315 dbi_result result;
316 unsigned char *cm2, *cm3;
317
318 dbi_conn_quote_binary_copy(conn, equip->classmark2,
319 equip->classmark2_len, &cm2);
320 dbi_conn_quote_binary_copy(conn, equip->classmark3,
321 equip->classmark3_len, &cm3);
322
323 result = dbi_conn_queryf(conn,
324 "UPDATE Equipment SET "
325 "updated = datetime('now'), "
326 "classmark1 = %u, "
327 "classmark2 = %s, "
328 "classmark3 = %s "
329 "WHERE imei = '%s' ",
330 equip->classmark1, cm2, cm3, equip->imei);
331
332 free(cm2);
333 free(cm3);
334
335 if (!result) {
336 printf("DB: Failed to update Equipment\n");
337 return -EIO;
338 }
339
340 dbi_result_free(result);
341 return 0;
342}
343
Jan Luebbe5c15c852008-12-27 15:59:25 +0000344int db_subscriber_alloc_tmsi(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000345 dbi_result result=NULL;
346 char* tmsi_quoted;
347 for (;;) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000348 sprintf(subscriber->tmsi, "%i", rand());
349 dbi_conn_quote_string_copy(conn, subscriber->tmsi, &tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000350 result = dbi_conn_queryf(conn,
351 "SELECT * FROM Subscriber "
352 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000353 tmsi_quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000354 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000355 free(tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000356 if (result==NULL) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000357 printf("DB: Failed to query Subscriber while allocating new TMSI.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000358 return 1;
359 }
Jan Luebbe5c15c852008-12-27 15:59:25 +0000360 if (dbi_result_get_numrows(result)){
361 dbi_result_free(result);
362 continue;
363 }
364 if (!dbi_result_next_row(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000365 dbi_result_free(result);
366 printf("DB: Allocated TMSI %s for IMSI %s.\n", subscriber->tmsi, subscriber->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000367 return db_sync_subscriber(subscriber);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000368 }
369 dbi_result_free(result);
370 }
371 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000372}
373
Jan Luebbefac25fc2008-12-27 18:04:34 +0000374int db_subscriber_assoc_imei(struct gsm_subscriber* subscriber, char imei[GSM_IMEI_LENGTH]) {
375 u_int64_t equipment_id, watch_id;
376 dbi_result result;
377
Harald Weltec2e302d2009-07-05 14:08:13 +0200378 strncpy(subscriber->equipment.imei, imei,
379 sizeof(subscriber->equipment.imei)-1),
380
Jan Luebbefac25fc2008-12-27 18:04:34 +0000381 result = dbi_conn_queryf(conn,
382 "INSERT OR IGNORE INTO Equipment "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000383 "(imei, created, updated) "
Jan Luebbefac25fc2008-12-27 18:04:34 +0000384 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000385 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbefac25fc2008-12-27 18:04:34 +0000386 imei
387 );
388 if (result==NULL) {
389 printf("DB: Failed to create Equipment by IMEI.\n");
390 return 1;
391 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000392 equipment_id = 0;
393 if (dbi_result_get_numrows_affected(result)) {
394 equipment_id = dbi_conn_sequence_last(conn, NULL);
395 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000396 dbi_result_free(result);
397 if (equipment_id) {
398 printf("DB: New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
399 }
400 else {
401 result = dbi_conn_queryf(conn,
402 "SELECT id FROM Equipment "
403 "WHERE imei = %s ",
404 imei
405 );
406 if (result==NULL) {
407 printf("DB: Failed to query Equipment by IMEI.\n");
408 return 1;
409 }
410 if (!dbi_result_next_row(result)) {
411 printf("DB: Failed to find the Equipment.\n");
412 dbi_result_free(result);
413 return 1;
414 }
415 equipment_id = dbi_result_get_ulonglong(result, "id");
416 dbi_result_free(result);
417 }
418
419 result = dbi_conn_queryf(conn,
420 "INSERT OR IGNORE INTO EquipmentWatch "
421 "(subscriber_id, equipment_id, created, updated) "
422 "VALUES "
423 "(%llu, %llu, datetime('now'), datetime('now')) ",
424 subscriber->id, equipment_id
425 );
426 if (result==NULL) {
427 printf("DB: Failed to create EquipmentWatch.\n");
428 return 1;
429 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000430 watch_id = 0;
431 if (dbi_result_get_numrows_affected(result)) {
432 watch_id = dbi_conn_sequence_last(conn, NULL);
433 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000434 dbi_result_free(result);
435 if (watch_id) {
436 printf("DB: New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
437 }
438 else {
439 result = dbi_conn_queryf(conn,
440 "UPDATE EquipmentWatch "
441 "SET updated = datetime('now') "
442 "WHERE subscriber_id = %llu AND equipment_id = %llu ",
443 subscriber->id, equipment_id
444 );
445 if (result==NULL) {
446 printf("DB: Failed to update EquipmentWatch.\n");
447 return 1;
448 }
449 dbi_result_free(result);
450 printf("DB: Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
451 }
452
453 return 0;
454}
455
Harald Welte7e310b12009-03-30 20:56:32 +0000456/* store an [unsent] SMS to the database */
457int db_sms_store(struct gsm_sms *sms)
458{
459 dbi_result result;
460 char *q_text;
Harald Welteb9c758b2009-07-05 14:02:46 +0200461 unsigned char *q_header;
Harald Welte7e310b12009-03-30 20:56:32 +0000462
463 dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
Harald Welteb9c758b2009-07-05 14:02:46 +0200464 dbi_conn_quote_binary_copy(conn, sms->header, sms->header_len,
465 &q_header);
Harald Weltef3efc592009-07-27 20:11:35 +0200466 /* FIXME: correct validity period */
Harald Welte7e310b12009-03-30 20:56:32 +0000467 result = dbi_conn_queryf(conn,
468 "INSERT INTO SMS "
Harald Weltef3efc592009-07-27 20:11:35 +0200469 "(created,sender_id,receiver_id,header,text,"
470 "valid_until,reply_path_req,status_rep_req,"
471 "protocol_id,data_coding_scheme) VALUES "
472 "(datetime('now'),%llu,%llu,%s,%s,%s,%u,%u,%u,%u)",
Harald Welte7e310b12009-03-30 20:56:32 +0000473 sms->sender->id,
474 sms->receiver ? sms->receiver->id : 0,
Harald Weltef3efc592009-07-27 20:11:35 +0200475 q_header, q_text, '2222-2-2', sms->reply_path_req,
476 sms->status_rep_req, sms->protocol_id,
477 sms->data_coding_scheme);
Harald Welte7e310b12009-03-30 20:56:32 +0000478 free(q_text);
Harald Welteb9c758b2009-07-05 14:02:46 +0200479 free(q_header);
Harald Welte7e310b12009-03-30 20:56:32 +0000480
481 if (!result)
482 return -EIO;
483
484 dbi_result_free(result);
485 return 0;
486}
487
488/* retrieve the next unsent SMS with ID >= min_id */
Harald Welte9176bd42009-07-23 18:46:00 +0200489struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, int min_id)
Harald Welte7e310b12009-03-30 20:56:32 +0000490{
491 dbi_result result;
Harald Weltebe3e3782009-07-05 14:06:41 +0200492 long long unsigned int sender_id, receiver_id;
Harald Welte7e310b12009-03-30 20:56:32 +0000493 struct gsm_sms *sms = malloc(sizeof(*sms));
Harald Weltef3efc592009-07-27 20:11:35 +0200494 const char *text;
495 const unsigned char *header;
Harald Weltebe3e3782009-07-05 14:06:41 +0200496 char buf[32];
Harald Welte7e310b12009-03-30 20:56:32 +0000497
498 if (!sms) {
499 free(sms);
500 return NULL;
501 }
502
503 result = dbi_conn_queryf(conn,
504 "SELECT * FROM SMS "
505 "WHERE id >= %llu ORDER BY id", min_id);
506 if (!result) {
507 free(sms);
508 return NULL;
509 }
Harald Weltebe3e3782009-07-05 14:06:41 +0200510 sms->id = dbi_result_get_ulonglong(result, "id");
Harald Welte7e310b12009-03-30 20:56:32 +0000511
Harald Weltebe3e3782009-07-05 14:06:41 +0200512 sender_id = dbi_result_get_ulonglong(result, "sender_id");
513 sprintf(buf, "%llu", sender_id);
Harald Welte9176bd42009-07-23 18:46:00 +0200514 sms->sender = db_get_subscriber(net, GSM_SUBSCRIBER_ID, buf);
Harald Weltebe3e3782009-07-05 14:06:41 +0200515
516 receiver_id = dbi_result_get_ulonglong(result, "receiver_id");
517 sprintf(buf, "%llu", receiver_id);
Harald Welte9176bd42009-07-23 18:46:00 +0200518 sms->receiver = db_get_subscriber(net, GSM_SUBSCRIBER_ID, buf);
Harald Weltebe3e3782009-07-05 14:06:41 +0200519
Harald Weltef3efc592009-07-27 20:11:35 +0200520 /* FIXME: validity */
521 sms->reply_path_req = dbi_result_get_uchar(result, "reply_path_req");
522 sms->status_rep_req = dbi_result_get_uchar(result, "status_rep_req");
523 sms->protocol_id = dbi_result_get_uchar(result, "protocol_id");
524 sms->data_coding_scheme = dbi_result_get_uchar(result,
525 "data_coding_scheme");
526
527 sms->header_len = dbi_result_get_field_length(result, "header");
528 header = dbi_result_get_binary(result, "header");
529 memcpy(sms->header, header, sms->header_len);
Harald Weltebe3e3782009-07-05 14:06:41 +0200530
531 text = dbi_result_get_string(result, "text");
532 if (text)
533 strncpy(sms->text, text, sizeof(sms->text));
Harald Welte7e310b12009-03-30 20:56:32 +0000534
535 dbi_result_free(result);
536 return sms;
537}
538
539/* mark a given SMS as read */
540int db_sms_mark_sent(struct gsm_sms *sms)
541{
542 dbi_result result;
543
544 result = dbi_conn_queryf(conn,
545 "UPDATE SMS "
546 "SET sent = datetime('now') "
547 "WHERE id = %llu", sms->id);
548 if (!result) {
549 printf("DB: Failed to mark SMS %llu as sent.\n", sms->id);
550 return 1;
551 }
552
553 dbi_result_free(result);
554 return 0;
555}