blob: 600699ae704c60636f21b7b7103b08ebeae063c6 [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>
Jan Luebbefaaa49c2008-12-27 01:07:07 +00004 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 */
21
Harald Welte7e310b12009-03-30 20:56:32 +000022#include <openbsc/gsm_data.h>
Jan Luebbefaaa49c2008-12-27 01:07:07 +000023#include <openbsc/db.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000024
Holger Freytherbde36102008-12-28 22:51:39 +000025#include <libgen.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000026#include <stdio.h>
Jan Luebbe5c15c852008-12-27 15:59:25 +000027#include <stdlib.h>
28#include <string.h>
Harald Welte7e310b12009-03-30 20:56:32 +000029#include <errno.h>
Jan Luebbe7398eb92008-12-27 00:45:41 +000030#include <dbi/dbi.h>
31
Holger Freytherbde36102008-12-28 22:51:39 +000032static char *db_basename = NULL;
33static char *db_dirname = NULL;
Holger Freyther1d506c82009-04-19 06:35:20 +000034static dbi_conn conn;
Jan Luebbe7398eb92008-12-27 00:45:41 +000035
Harald Welte7e310b12009-03-30 20:56:32 +000036static char *create_stmts[] = {
37 "CREATE TABLE IF NOT EXISTS Meta ("
38 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
39 "key TEXT UNIQUE NOT NULL, "
40 "value TEXT NOT NULL"
41 ")",
42 "INSERT OR IGNORE INTO Meta "
43 "(key, value) "
44 "VALUES "
45 "('revision', '1')",
46 "CREATE TABLE IF NOT EXISTS Subscriber ("
47 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
48 "created TIMESTAMP NOT NULL, "
49 "updated TIMESTAMP NOT NULL, "
50 "imsi NUMERIC UNIQUE NOT NULL, "
51 "name TEXT, "
52 "extension TEXT UNIQUE, "
53 "authorized INTEGER NOT NULL DEFAULT 0, "
54 "tmsi TEXT UNIQUE, "
55 "lac INTEGER NOT NULL DEFAULT 0"
56 ")",
57 "CREATE TABLE IF NOT EXISTS Equipment ("
58 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
59 "created TIMESTAMP NOT NULL, "
60 "updated TIMESTAMP NOT NULL, "
61 "name TEXT, "
62 "imei NUMERIC UNIQUE NOT NULL"
63 ")",
64 "CREATE TABLE IF NOT EXISTS EquipmentWatch ("
65 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
66 "created TIMESTAMP NOT NULL, "
67 "updated TIMESTAMP NOT NULL, "
68 "subscriber_id NUMERIC NOT NULL, "
69 "equipment_id NUMERIC NOT NULL, "
70 "UNIQUE (subscriber_id, equipment_id) "
71 ")",
72 "CREATE TABLE IF NOT EXISTS SMS ("
73 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
74 "created TIMESTAMP NOT NULL, "
75 "sent TIMESTAMP, "
76 "sender_id NUMERIC NOT NULL, "
77 "receiver_id NUMERIC NOT NULL, "
78 "header NUMERIC, "
79 "text TEXT NOT NULL "
80 ")",
Holger Freytherc2995ea2009-04-19 06:35:23 +000081 "CREATE TABLE IF NOT EXISTS VLR ("
82 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
83 "created TIMESTAMP NOT NULL, "
84 "updated TIMESTAMP NOT NULL, "
85 "subscriber_id NUMERIC UNIQUE NOT NULL, "
86 "last_bts NUMERIC NOT NULL "
87 ")",
Harald Welte7e310b12009-03-30 20:56:32 +000088};
89
Holger Freyther12aa50d2009-01-01 18:02:05 +000090void db_error_func(dbi_conn conn, void* data) {
Jan Luebbe5c15c852008-12-27 15:59:25 +000091 const char* msg;
92 dbi_conn_error(conn, &msg);
93 printf("DBI: %s\n", msg);
Jan Luebbe7398eb92008-12-27 00:45:41 +000094}
95
Holger Freytherc7b86f92009-06-06 13:54:20 +000096int db_init(const char *name) {
Jan Luebbe5c15c852008-12-27 15:59:25 +000097 dbi_initialize(NULL);
98 conn = dbi_conn_new("sqlite3");
99 if (conn==NULL) {
100 printf("DB: Failed to create connection.\n");
101 return 1;
102 }
Jan Luebbe7398eb92008-12-27 00:45:41 +0000103
Holger Freyther12aa50d2009-01-01 18:02:05 +0000104 dbi_conn_error_handler( conn, db_error_func, NULL );
Jan Luebbe7398eb92008-12-27 00:45:41 +0000105
Jan Luebbe5c15c852008-12-27 15:59:25 +0000106 /* MySQL
107 dbi_conn_set_option(conn, "host", "localhost");
108 dbi_conn_set_option(conn, "username", "your_name");
109 dbi_conn_set_option(conn, "password", "your_password");
110 dbi_conn_set_option(conn, "dbname", "your_dbname");
111 dbi_conn_set_option(conn, "encoding", "UTF-8");
112 */
Jan Luebbe7398eb92008-12-27 00:45:41 +0000113
Jan Luebbe5c15c852008-12-27 15:59:25 +0000114 /* SqLite 3 */
Holger Freyther12aa50d2009-01-01 18:02:05 +0000115 db_basename = strdup(name);
116 db_dirname = strdup(name);
Holger Freytherbde36102008-12-28 22:51:39 +0000117 dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname));
118 dbi_conn_set_option(conn, "dbname", basename(db_basename));
Jan Luebbe7398eb92008-12-27 00:45:41 +0000119
Jan Luebbe5c15c852008-12-27 15:59:25 +0000120 if (dbi_conn_connect(conn) < 0) {
Holger Freytherbde36102008-12-28 22:51:39 +0000121 free(db_dirname);
122 free(db_basename);
123 db_dirname = db_basename = NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000124 return 1;
125 }
126
127 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000128}
129
130int db_prepare() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000131 dbi_result result;
Harald Welte7e310b12009-03-30 20:56:32 +0000132 int i;
Holger Freytherb4064bc2009-02-23 00:50:31 +0000133
Harald Welte7e310b12009-03-30 20:56:32 +0000134 for (i = 0; i < ARRAY_SIZE(create_stmts); i++) {
135 result = dbi_conn_query(conn, create_stmts[i]);
136 if (result==NULL) {
137 printf("DB: Failed to create some table.\n");
138 return 1;
139 }
140 dbi_result_free(result);
Holger Freytherb4064bc2009-02-23 00:50:31 +0000141 }
Holger Freytherb4064bc2009-02-23 00:50:31 +0000142
Jan Luebbe5c15c852008-12-27 15:59:25 +0000143 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000144}
145
146int db_fini() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000147 dbi_conn_close(conn);
148 dbi_shutdown();
Holger Freytherbde36102008-12-28 22:51:39 +0000149
150 if (db_dirname)
151 free(db_dirname);
152 if (db_basename)
153 free(db_basename);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000154 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000155}
156
Holger Freyther12aa50d2009-01-01 18:02:05 +0000157struct gsm_subscriber* db_create_subscriber(char *imsi) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000158 dbi_result result;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000159 struct gsm_subscriber* subscr;
160
161 /* Is this subscriber known in the db? */
162 subscr = db_get_subscriber(GSM_SUBSCRIBER_IMSI, imsi);
163 if (subscr) {
Harald Welte523200b2008-12-30 14:58:44 +0000164 result = dbi_conn_queryf(conn,
165 "UPDATE Subscriber set updated = datetime('now') "
166 "WHERE imsi = %s " , imsi);
167 if (result==NULL) {
168 printf("DB: failed to update timestamp\n");
169 } else {
170 dbi_result_free(result);
171 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000172 return subscr;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000173 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000174
175 subscr = subscr_alloc();
176 if (!subscr)
177 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000178 result = dbi_conn_queryf(conn,
Jan Luebbe391d86e2008-12-27 22:33:34 +0000179 "INSERT INTO Subscriber "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000180 "(imsi, created, updated) "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000181 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000182 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbe5c15c852008-12-27 15:59:25 +0000183 imsi
184 );
185 if (result==NULL) {
186 printf("DB: Failed to create Subscriber by IMSI.\n");
187 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000188 subscr->id = dbi_conn_sequence_last(conn, NULL);
189 strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000190 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000191 printf("DB: New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi);
192 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000193}
194
Holger Freytherca362a62009-01-04 21:05:01 +0000195struct gsm_subscriber *db_get_subscriber(enum gsm_subscriber_field field, const char *id) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000196 dbi_result result;
Jan Luebbe391d86e2008-12-27 22:33:34 +0000197 const char *string;
198 char *quoted;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000199 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000200
Jan Luebbe5c15c852008-12-27 15:59:25 +0000201 switch (field) {
202 case GSM_SUBSCRIBER_IMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000203 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000204 result = dbi_conn_queryf(conn,
205 "SELECT * FROM Subscriber "
206 "WHERE imsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000207 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000208 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000209 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000210 break;
211 case GSM_SUBSCRIBER_TMSI:
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 tmsi = %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;
Holger Freyther9c564b82009-02-09 23:39:20 +0000220 case GSM_SUBSCRIBER_EXTENSION:
221 dbi_conn_quote_string_copy(conn, id, &quoted);
222 result = dbi_conn_queryf(conn,
223 "SELECT * FROM Subscriber "
224 "WHERE extension = %s ",
225 quoted
226 );
227 free(quoted);
228 break;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000229 default:
230 printf("DB: Unknown query selector for Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000231 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000232 }
233 if (result==NULL) {
234 printf("DB: Failed to query Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000235 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000236 }
237 if (!dbi_result_next_row(result)) {
Holger Freyther1ef983b2009-02-22 20:33:09 +0000238 printf("DB: Failed to find the Subscriber. '%u' '%s'\n",
239 field, id);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000240 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000241 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000242 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000243
244 subscr = subscr_alloc();
245 subscr->id = dbi_result_get_ulonglong(result, "id");
Harald Welte75a983f2008-12-27 21:34:06 +0000246 string = dbi_result_get_string(result, "imsi");
247 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000248 strncpy(subscr->imsi, string, GSM_IMSI_LENGTH);
Harald Welte75a983f2008-12-27 21:34:06 +0000249
250 string = dbi_result_get_string(result, "tmsi");
251 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000252 strncpy(subscr->tmsi, string, GSM_TMSI_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000253
254 string = dbi_result_get_string(result, "name");
255 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000256 strncpy(subscr->name, string, GSM_NAME_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000257
258 string = dbi_result_get_string(result, "extension");
259 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000260 strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000261
Holger Freyther12aa50d2009-01-01 18:02:05 +0000262 subscr->lac = dbi_result_get_uint(result, "lac");
263 subscr->authorized = dbi_result_get_uint(result, "authorized");
Holger Freyther91754472009-06-09 08:52:41 +0000264 printf("DB: Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %s, EXTEN '%s', LAC %hu, AUTH %u\n",
265 subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension,
Holger Freyther12aa50d2009-01-01 18:02:05 +0000266 subscr->lac, subscr->authorized);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000267 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000268 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000269}
270
Holger Freyther12aa50d2009-01-01 18:02:05 +0000271int db_sync_subscriber(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000272 dbi_result result;
273 result = dbi_conn_queryf(conn,
274 "UPDATE Subscriber "
Jan Luebbe391d86e2008-12-27 22:33:34 +0000275 "SET updated = datetime('now'), "
Holger Freyther91754472009-06-09 08:52:41 +0000276 "tmsi = '%s', "
Jan Luebbe391d86e2008-12-27 22:33:34 +0000277 "lac = %i, "
278 "authorized = %i "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000279 "WHERE imsi = %s ",
Jan Luebbe6e2e5452008-12-27 16:47:55 +0000280 subscriber->tmsi, subscriber->lac, subscriber->authorized, subscriber->imsi
Jan Luebbe5c15c852008-12-27 15:59:25 +0000281 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000282
Jan Luebbe5c15c852008-12-27 15:59:25 +0000283 if (result==NULL) {
284 printf("DB: Failed to update Subscriber (by IMSI).\n");
285 return 1;
286 }
287 dbi_result_free(result);
288 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000289}
290
Jan Luebbe5c15c852008-12-27 15:59:25 +0000291int db_subscriber_alloc_tmsi(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000292 dbi_result result=NULL;
293 char* tmsi_quoted;
294 for (;;) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000295 sprintf(subscriber->tmsi, "%i", rand());
296 dbi_conn_quote_string_copy(conn, subscriber->tmsi, &tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000297 result = dbi_conn_queryf(conn,
298 "SELECT * FROM Subscriber "
299 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000300 tmsi_quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000301 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000302 free(tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000303 if (result==NULL) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000304 printf("DB: Failed to query Subscriber while allocating new TMSI.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000305 return 1;
306 }
Jan Luebbe5c15c852008-12-27 15:59:25 +0000307 if (dbi_result_get_numrows(result)){
308 dbi_result_free(result);
309 continue;
310 }
311 if (!dbi_result_next_row(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000312 dbi_result_free(result);
313 printf("DB: Allocated TMSI %s for IMSI %s.\n", subscriber->tmsi, subscriber->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000314 return db_sync_subscriber(subscriber);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000315 }
316 dbi_result_free(result);
317 }
318 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000319}
320
Jan Luebbefac25fc2008-12-27 18:04:34 +0000321int db_subscriber_assoc_imei(struct gsm_subscriber* subscriber, char imei[GSM_IMEI_LENGTH]) {
322 u_int64_t equipment_id, watch_id;
323 dbi_result result;
324
325 result = dbi_conn_queryf(conn,
326 "INSERT OR IGNORE INTO Equipment "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000327 "(imei, created, updated) "
Jan Luebbefac25fc2008-12-27 18:04:34 +0000328 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000329 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbefac25fc2008-12-27 18:04:34 +0000330 imei
331 );
332 if (result==NULL) {
333 printf("DB: Failed to create Equipment by IMEI.\n");
334 return 1;
335 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000336 equipment_id = 0;
337 if (dbi_result_get_numrows_affected(result)) {
338 equipment_id = dbi_conn_sequence_last(conn, NULL);
339 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000340 dbi_result_free(result);
341 if (equipment_id) {
342 printf("DB: New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
343 }
344 else {
345 result = dbi_conn_queryf(conn,
346 "SELECT id FROM Equipment "
347 "WHERE imei = %s ",
348 imei
349 );
350 if (result==NULL) {
351 printf("DB: Failed to query Equipment by IMEI.\n");
352 return 1;
353 }
354 if (!dbi_result_next_row(result)) {
355 printf("DB: Failed to find the Equipment.\n");
356 dbi_result_free(result);
357 return 1;
358 }
359 equipment_id = dbi_result_get_ulonglong(result, "id");
360 dbi_result_free(result);
361 }
362
363 result = dbi_conn_queryf(conn,
364 "INSERT OR IGNORE INTO EquipmentWatch "
365 "(subscriber_id, equipment_id, created, updated) "
366 "VALUES "
367 "(%llu, %llu, datetime('now'), datetime('now')) ",
368 subscriber->id, equipment_id
369 );
370 if (result==NULL) {
371 printf("DB: Failed to create EquipmentWatch.\n");
372 return 1;
373 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000374 watch_id = 0;
375 if (dbi_result_get_numrows_affected(result)) {
376 watch_id = dbi_conn_sequence_last(conn, NULL);
377 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000378 dbi_result_free(result);
379 if (watch_id) {
380 printf("DB: New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
381 }
382 else {
383 result = dbi_conn_queryf(conn,
384 "UPDATE EquipmentWatch "
385 "SET updated = datetime('now') "
386 "WHERE subscriber_id = %llu AND equipment_id = %llu ",
387 subscriber->id, equipment_id
388 );
389 if (result==NULL) {
390 printf("DB: Failed to update EquipmentWatch.\n");
391 return 1;
392 }
393 dbi_result_free(result);
394 printf("DB: Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
395 }
396
397 return 0;
398}
399
Harald Welte7e310b12009-03-30 20:56:32 +0000400/* store an [unsent] SMS to the database */
401int db_sms_store(struct gsm_sms *sms)
402{
403 dbi_result result;
404 char *q_text;
405
406 dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
407 result = dbi_conn_queryf(conn,
408 "INSERT INTO SMS "
409 "(created,sender_id,receiver_id,header,text) VALUES "
410 "(datetime('now'),%llu,%llu,%s,%s)",
411 sms->sender->id,
412 sms->receiver ? sms->receiver->id : 0,
413 NULL, q_text);
414 free(q_text);
415
416 if (!result)
417 return -EIO;
418
419 dbi_result_free(result);
420 return 0;
421}
422
423/* retrieve the next unsent SMS with ID >= min_id */
424struct gsm_sms *db_sms_get_unsent(int min_id)
425{
426 dbi_result result;
427 struct gsm_sms *sms = malloc(sizeof(*sms));
428
429 if (!sms) {
430 free(sms);
431 return NULL;
432 }
433
434 result = dbi_conn_queryf(conn,
435 "SELECT * FROM SMS "
436 "WHERE id >= %llu ORDER BY id", min_id);
437 if (!result) {
438 free(sms);
439 return NULL;
440 }
441
442 /* FIXME: fill gsm_sms from database */
443
444 dbi_result_free(result);
445 return sms;
446}
447
448/* mark a given SMS as read */
449int db_sms_mark_sent(struct gsm_sms *sms)
450{
451 dbi_result result;
452
453 result = dbi_conn_queryf(conn,
454 "UPDATE SMS "
455 "SET sent = datetime('now') "
456 "WHERE id = %llu", sms->id);
457 if (!result) {
458 printf("DB: Failed to mark SMS %llu as sent.\n", sms->id);
459 return 1;
460 }
461
462 dbi_result_free(result);
463 return 0;
464}