blob: 17ea17d3ea1a28c09c45fe1001480e419f86bf77 [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;
Jan Luebbe7398eb92008-12-27 00:45:41 +000034dbi_conn conn;
35
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 ")",
81};
82
Holger Freyther12aa50d2009-01-01 18:02:05 +000083void db_error_func(dbi_conn conn, void* data) {
Jan Luebbe5c15c852008-12-27 15:59:25 +000084 const char* msg;
85 dbi_conn_error(conn, &msg);
86 printf("DBI: %s\n", msg);
Jan Luebbe7398eb92008-12-27 00:45:41 +000087}
88
Holger Freytherbde36102008-12-28 22:51:39 +000089int db_init(const char *name) {
Jan Luebbe5c15c852008-12-27 15:59:25 +000090 dbi_initialize(NULL);
91 conn = dbi_conn_new("sqlite3");
92 if (conn==NULL) {
93 printf("DB: Failed to create connection.\n");
94 return 1;
95 }
Jan Luebbe7398eb92008-12-27 00:45:41 +000096
Holger Freyther12aa50d2009-01-01 18:02:05 +000097 dbi_conn_error_handler( conn, db_error_func, NULL );
Jan Luebbe7398eb92008-12-27 00:45:41 +000098
Jan Luebbe5c15c852008-12-27 15:59:25 +000099 /* MySQL
100 dbi_conn_set_option(conn, "host", "localhost");
101 dbi_conn_set_option(conn, "username", "your_name");
102 dbi_conn_set_option(conn, "password", "your_password");
103 dbi_conn_set_option(conn, "dbname", "your_dbname");
104 dbi_conn_set_option(conn, "encoding", "UTF-8");
105 */
Jan Luebbe7398eb92008-12-27 00:45:41 +0000106
Jan Luebbe5c15c852008-12-27 15:59:25 +0000107 /* SqLite 3 */
Holger Freyther12aa50d2009-01-01 18:02:05 +0000108 db_basename = strdup(name);
109 db_dirname = strdup(name);
Holger Freytherbde36102008-12-28 22:51:39 +0000110 dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname));
111 dbi_conn_set_option(conn, "dbname", basename(db_basename));
Jan Luebbe7398eb92008-12-27 00:45:41 +0000112
Jan Luebbe5c15c852008-12-27 15:59:25 +0000113 if (dbi_conn_connect(conn) < 0) {
Holger Freytherbde36102008-12-28 22:51:39 +0000114 free(db_dirname);
115 free(db_basename);
116 db_dirname = db_basename = NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000117 return 1;
118 }
119
120 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000121}
122
123int db_prepare() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000124 dbi_result result;
Harald Welte7e310b12009-03-30 20:56:32 +0000125 int i;
Holger Freytherb4064bc2009-02-23 00:50:31 +0000126
Harald Welte7e310b12009-03-30 20:56:32 +0000127 for (i = 0; i < ARRAY_SIZE(create_stmts); i++) {
128 result = dbi_conn_query(conn, create_stmts[i]);
129 if (result==NULL) {
130 printf("DB: Failed to create some table.\n");
131 return 1;
132 }
133 dbi_result_free(result);
Holger Freytherb4064bc2009-02-23 00:50:31 +0000134 }
Holger Freytherb4064bc2009-02-23 00:50:31 +0000135
Jan Luebbe5c15c852008-12-27 15:59:25 +0000136 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000137}
138
139int db_fini() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000140 dbi_conn_close(conn);
141 dbi_shutdown();
Holger Freytherbde36102008-12-28 22:51:39 +0000142
143 if (db_dirname)
144 free(db_dirname);
145 if (db_basename)
146 free(db_basename);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000147 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000148}
149
Holger Freyther12aa50d2009-01-01 18:02:05 +0000150struct gsm_subscriber* db_create_subscriber(char *imsi) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000151 dbi_result result;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000152 struct gsm_subscriber* subscr;
153
154 /* Is this subscriber known in the db? */
155 subscr = db_get_subscriber(GSM_SUBSCRIBER_IMSI, imsi);
156 if (subscr) {
Harald Welte523200b2008-12-30 14:58:44 +0000157 result = dbi_conn_queryf(conn,
158 "UPDATE Subscriber set updated = datetime('now') "
159 "WHERE imsi = %s " , imsi);
160 if (result==NULL) {
161 printf("DB: failed to update timestamp\n");
162 } else {
163 dbi_result_free(result);
164 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000165 return subscr;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000166 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000167
168 subscr = subscr_alloc();
169 if (!subscr)
170 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000171 result = dbi_conn_queryf(conn,
Jan Luebbe391d86e2008-12-27 22:33:34 +0000172 "INSERT INTO Subscriber "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000173 "(imsi, created, updated) "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000174 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000175 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbe5c15c852008-12-27 15:59:25 +0000176 imsi
177 );
178 if (result==NULL) {
179 printf("DB: Failed to create Subscriber by IMSI.\n");
180 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000181 subscr->id = dbi_conn_sequence_last(conn, NULL);
182 strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000183 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000184 printf("DB: New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi);
185 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000186}
187
Holger Freytherca362a62009-01-04 21:05:01 +0000188struct gsm_subscriber *db_get_subscriber(enum gsm_subscriber_field field, const char *id) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000189 dbi_result result;
Jan Luebbe391d86e2008-12-27 22:33:34 +0000190 const char *string;
191 char *quoted;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000192 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000193
Jan Luebbe5c15c852008-12-27 15:59:25 +0000194 switch (field) {
195 case GSM_SUBSCRIBER_IMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000196 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000197 result = dbi_conn_queryf(conn,
198 "SELECT * FROM Subscriber "
199 "WHERE imsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000200 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000201 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000202 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000203 break;
204 case GSM_SUBSCRIBER_TMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000205 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000206 result = dbi_conn_queryf(conn,
207 "SELECT * FROM Subscriber "
208 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000209 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000210 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000211 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000212 break;
Holger Freyther9c564b82009-02-09 23:39:20 +0000213 case GSM_SUBSCRIBER_EXTENSION:
214 dbi_conn_quote_string_copy(conn, id, &quoted);
215 result = dbi_conn_queryf(conn,
216 "SELECT * FROM Subscriber "
217 "WHERE extension = %s ",
218 quoted
219 );
220 free(quoted);
221 break;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000222 default:
223 printf("DB: Unknown query selector for Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000224 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000225 }
226 if (result==NULL) {
227 printf("DB: Failed to query Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000228 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000229 }
230 if (!dbi_result_next_row(result)) {
Holger Freyther1ef983b2009-02-22 20:33:09 +0000231 printf("DB: Failed to find the Subscriber. '%u' '%s'\n",
232 field, id);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000233 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000234 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000235 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000236
237 subscr = subscr_alloc();
238 subscr->id = dbi_result_get_ulonglong(result, "id");
Harald Welte75a983f2008-12-27 21:34:06 +0000239 string = dbi_result_get_string(result, "imsi");
240 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000241 strncpy(subscr->imsi, string, GSM_IMSI_LENGTH);
Harald Welte75a983f2008-12-27 21:34:06 +0000242
243 string = dbi_result_get_string(result, "tmsi");
244 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000245 strncpy(subscr->tmsi, string, GSM_TMSI_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000246
247 string = dbi_result_get_string(result, "name");
248 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000249 strncpy(subscr->name, string, GSM_NAME_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000250
251 string = dbi_result_get_string(result, "extension");
252 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000253 strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000254
Jan Luebbe5c15c852008-12-27 15:59:25 +0000255 // FIXME handle extension
Holger Freyther12aa50d2009-01-01 18:02:05 +0000256 subscr->lac = dbi_result_get_uint(result, "lac");
257 subscr->authorized = dbi_result_get_uint(result, "authorized");
Jan Luebbe3444ddb2008-12-28 12:37:15 +0000258 printf("DB: Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %s, LAC %hu, AUTH %u\n",
Holger Freyther12aa50d2009-01-01 18:02:05 +0000259 subscr->id, subscr->imsi, subscr->name, subscr->tmsi,
260 subscr->lac, subscr->authorized);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000261 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000262 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000263}
264
Holger Freyther12aa50d2009-01-01 18:02:05 +0000265int db_sync_subscriber(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000266 dbi_result result;
267 result = dbi_conn_queryf(conn,
268 "UPDATE Subscriber "
Jan Luebbe391d86e2008-12-27 22:33:34 +0000269 "SET updated = datetime('now'), "
270 "tmsi = %s, "
271 "lac = %i, "
272 "authorized = %i "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000273 "WHERE imsi = %s ",
Jan Luebbe6e2e5452008-12-27 16:47:55 +0000274 subscriber->tmsi, subscriber->lac, subscriber->authorized, subscriber->imsi
Jan Luebbe5c15c852008-12-27 15:59:25 +0000275 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000276
Jan Luebbe5c15c852008-12-27 15:59:25 +0000277 if (result==NULL) {
278 printf("DB: Failed to update Subscriber (by IMSI).\n");
279 return 1;
280 }
281 dbi_result_free(result);
282 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000283}
284
Jan Luebbe5c15c852008-12-27 15:59:25 +0000285int db_subscriber_alloc_tmsi(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000286 dbi_result result=NULL;
287 char* tmsi_quoted;
288 for (;;) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000289 sprintf(subscriber->tmsi, "%i", rand());
290 dbi_conn_quote_string_copy(conn, subscriber->tmsi, &tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000291 result = dbi_conn_queryf(conn,
292 "SELECT * FROM Subscriber "
293 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000294 tmsi_quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000295 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000296 free(tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000297 if (result==NULL) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000298 printf("DB: Failed to query Subscriber while allocating new TMSI.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000299 return 1;
300 }
Jan Luebbe5c15c852008-12-27 15:59:25 +0000301 if (dbi_result_get_numrows(result)){
302 dbi_result_free(result);
303 continue;
304 }
305 if (!dbi_result_next_row(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000306 dbi_result_free(result);
307 printf("DB: Allocated TMSI %s for IMSI %s.\n", subscriber->tmsi, subscriber->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000308 return db_sync_subscriber(subscriber);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000309 }
310 dbi_result_free(result);
311 }
312 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000313}
314
Jan Luebbefac25fc2008-12-27 18:04:34 +0000315int db_subscriber_assoc_imei(struct gsm_subscriber* subscriber, char imei[GSM_IMEI_LENGTH]) {
316 u_int64_t equipment_id, watch_id;
317 dbi_result result;
318
319 result = dbi_conn_queryf(conn,
320 "INSERT OR IGNORE INTO Equipment "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000321 "(imei, created, updated) "
Jan Luebbefac25fc2008-12-27 18:04:34 +0000322 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000323 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbefac25fc2008-12-27 18:04:34 +0000324 imei
325 );
326 if (result==NULL) {
327 printf("DB: Failed to create Equipment by IMEI.\n");
328 return 1;
329 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000330 equipment_id = 0;
331 if (dbi_result_get_numrows_affected(result)) {
332 equipment_id = dbi_conn_sequence_last(conn, NULL);
333 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000334 dbi_result_free(result);
335 if (equipment_id) {
336 printf("DB: New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
337 }
338 else {
339 result = dbi_conn_queryf(conn,
340 "SELECT id FROM Equipment "
341 "WHERE imei = %s ",
342 imei
343 );
344 if (result==NULL) {
345 printf("DB: Failed to query Equipment by IMEI.\n");
346 return 1;
347 }
348 if (!dbi_result_next_row(result)) {
349 printf("DB: Failed to find the Equipment.\n");
350 dbi_result_free(result);
351 return 1;
352 }
353 equipment_id = dbi_result_get_ulonglong(result, "id");
354 dbi_result_free(result);
355 }
356
357 result = dbi_conn_queryf(conn,
358 "INSERT OR IGNORE INTO EquipmentWatch "
359 "(subscriber_id, equipment_id, created, updated) "
360 "VALUES "
361 "(%llu, %llu, datetime('now'), datetime('now')) ",
362 subscriber->id, equipment_id
363 );
364 if (result==NULL) {
365 printf("DB: Failed to create EquipmentWatch.\n");
366 return 1;
367 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000368 watch_id = 0;
369 if (dbi_result_get_numrows_affected(result)) {
370 watch_id = dbi_conn_sequence_last(conn, NULL);
371 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000372 dbi_result_free(result);
373 if (watch_id) {
374 printf("DB: New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
375 }
376 else {
377 result = dbi_conn_queryf(conn,
378 "UPDATE EquipmentWatch "
379 "SET updated = datetime('now') "
380 "WHERE subscriber_id = %llu AND equipment_id = %llu ",
381 subscriber->id, equipment_id
382 );
383 if (result==NULL) {
384 printf("DB: Failed to update EquipmentWatch.\n");
385 return 1;
386 }
387 dbi_result_free(result);
388 printf("DB: Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
389 }
390
391 return 0;
392}
393
Harald Welte7e310b12009-03-30 20:56:32 +0000394/* store an [unsent] SMS to the database */
395int db_sms_store(struct gsm_sms *sms)
396{
397 dbi_result result;
398 char *q_text;
399
400 dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
401 result = dbi_conn_queryf(conn,
402 "INSERT INTO SMS "
403 "(created,sender_id,receiver_id,header,text) VALUES "
404 "(datetime('now'),%llu,%llu,%s,%s)",
405 sms->sender->id,
406 sms->receiver ? sms->receiver->id : 0,
407 NULL, q_text);
408 free(q_text);
409
410 if (!result)
411 return -EIO;
412
413 dbi_result_free(result);
414 return 0;
415}
416
417/* retrieve the next unsent SMS with ID >= min_id */
418struct gsm_sms *db_sms_get_unsent(int min_id)
419{
420 dbi_result result;
421 struct gsm_sms *sms = malloc(sizeof(*sms));
422
423 if (!sms) {
424 free(sms);
425 return NULL;
426 }
427
428 result = dbi_conn_queryf(conn,
429 "SELECT * FROM SMS "
430 "WHERE id >= %llu ORDER BY id", min_id);
431 if (!result) {
432 free(sms);
433 return NULL;
434 }
435
436 /* FIXME: fill gsm_sms from database */
437
438 dbi_result_free(result);
439 return sms;
440}
441
442/* mark a given SMS as read */
443int db_sms_mark_sent(struct gsm_sms *sms)
444{
445 dbi_result result;
446
447 result = dbi_conn_queryf(conn,
448 "UPDATE SMS "
449 "SET sent = datetime('now') "
450 "WHERE id = %llu", sms->id);
451 if (!result) {
452 printf("DB: Failed to mark SMS %llu as sent.\n", sms->id);
453 return 1;
454 }
455
456 dbi_result_free(result);
457 return 0;
458}