blob: a761364fdf021760a5b7d290511aafc55ab5ed14 [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
Holger Freyther12aa50d2009-01-01 18:02:05 +0000255 subscr->lac = dbi_result_get_uint(result, "lac");
256 subscr->authorized = dbi_result_get_uint(result, "authorized");
Jan Luebbe3444ddb2008-12-28 12:37:15 +0000257 printf("DB: Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %s, LAC %hu, AUTH %u\n",
Holger Freyther12aa50d2009-01-01 18:02:05 +0000258 subscr->id, subscr->imsi, subscr->name, subscr->tmsi,
259 subscr->lac, subscr->authorized);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000260 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000261 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000262}
263
Holger Freyther12aa50d2009-01-01 18:02:05 +0000264int db_sync_subscriber(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000265 dbi_result result;
266 result = dbi_conn_queryf(conn,
267 "UPDATE Subscriber "
Jan Luebbe391d86e2008-12-27 22:33:34 +0000268 "SET updated = datetime('now'), "
269 "tmsi = %s, "
270 "lac = %i, "
271 "authorized = %i "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000272 "WHERE imsi = %s ",
Jan Luebbe6e2e5452008-12-27 16:47:55 +0000273 subscriber->tmsi, subscriber->lac, subscriber->authorized, subscriber->imsi
Jan Luebbe5c15c852008-12-27 15:59:25 +0000274 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000275
Jan Luebbe5c15c852008-12-27 15:59:25 +0000276 if (result==NULL) {
277 printf("DB: Failed to update Subscriber (by IMSI).\n");
278 return 1;
279 }
280 dbi_result_free(result);
281 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000282}
283
Jan Luebbe5c15c852008-12-27 15:59:25 +0000284int db_subscriber_alloc_tmsi(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000285 dbi_result result=NULL;
286 char* tmsi_quoted;
287 for (;;) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000288 sprintf(subscriber->tmsi, "%i", rand());
289 dbi_conn_quote_string_copy(conn, subscriber->tmsi, &tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000290 result = dbi_conn_queryf(conn,
291 "SELECT * FROM Subscriber "
292 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000293 tmsi_quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000294 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000295 free(tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000296 if (result==NULL) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000297 printf("DB: Failed to query Subscriber while allocating new TMSI.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000298 return 1;
299 }
Jan Luebbe5c15c852008-12-27 15:59:25 +0000300 if (dbi_result_get_numrows(result)){
301 dbi_result_free(result);
302 continue;
303 }
304 if (!dbi_result_next_row(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000305 dbi_result_free(result);
306 printf("DB: Allocated TMSI %s for IMSI %s.\n", subscriber->tmsi, subscriber->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000307 return db_sync_subscriber(subscriber);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000308 }
309 dbi_result_free(result);
310 }
311 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000312}
313
Jan Luebbefac25fc2008-12-27 18:04:34 +0000314int db_subscriber_assoc_imei(struct gsm_subscriber* subscriber, char imei[GSM_IMEI_LENGTH]) {
315 u_int64_t equipment_id, watch_id;
316 dbi_result result;
317
318 result = dbi_conn_queryf(conn,
319 "INSERT OR IGNORE INTO Equipment "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000320 "(imei, created, updated) "
Jan Luebbefac25fc2008-12-27 18:04:34 +0000321 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000322 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbefac25fc2008-12-27 18:04:34 +0000323 imei
324 );
325 if (result==NULL) {
326 printf("DB: Failed to create Equipment by IMEI.\n");
327 return 1;
328 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000329 equipment_id = 0;
330 if (dbi_result_get_numrows_affected(result)) {
331 equipment_id = dbi_conn_sequence_last(conn, NULL);
332 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000333 dbi_result_free(result);
334 if (equipment_id) {
335 printf("DB: New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
336 }
337 else {
338 result = dbi_conn_queryf(conn,
339 "SELECT id FROM Equipment "
340 "WHERE imei = %s ",
341 imei
342 );
343 if (result==NULL) {
344 printf("DB: Failed to query Equipment by IMEI.\n");
345 return 1;
346 }
347 if (!dbi_result_next_row(result)) {
348 printf("DB: Failed to find the Equipment.\n");
349 dbi_result_free(result);
350 return 1;
351 }
352 equipment_id = dbi_result_get_ulonglong(result, "id");
353 dbi_result_free(result);
354 }
355
356 result = dbi_conn_queryf(conn,
357 "INSERT OR IGNORE INTO EquipmentWatch "
358 "(subscriber_id, equipment_id, created, updated) "
359 "VALUES "
360 "(%llu, %llu, datetime('now'), datetime('now')) ",
361 subscriber->id, equipment_id
362 );
363 if (result==NULL) {
364 printf("DB: Failed to create EquipmentWatch.\n");
365 return 1;
366 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000367 watch_id = 0;
368 if (dbi_result_get_numrows_affected(result)) {
369 watch_id = dbi_conn_sequence_last(conn, NULL);
370 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000371 dbi_result_free(result);
372 if (watch_id) {
373 printf("DB: New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
374 }
375 else {
376 result = dbi_conn_queryf(conn,
377 "UPDATE EquipmentWatch "
378 "SET updated = datetime('now') "
379 "WHERE subscriber_id = %llu AND equipment_id = %llu ",
380 subscriber->id, equipment_id
381 );
382 if (result==NULL) {
383 printf("DB: Failed to update EquipmentWatch.\n");
384 return 1;
385 }
386 dbi_result_free(result);
387 printf("DB: Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
388 }
389
390 return 0;
391}
392
Harald Welte7e310b12009-03-30 20:56:32 +0000393/* store an [unsent] SMS to the database */
394int db_sms_store(struct gsm_sms *sms)
395{
396 dbi_result result;
397 char *q_text;
398
399 dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
400 result = dbi_conn_queryf(conn,
401 "INSERT INTO SMS "
402 "(created,sender_id,receiver_id,header,text) VALUES "
403 "(datetime('now'),%llu,%llu,%s,%s)",
404 sms->sender->id,
405 sms->receiver ? sms->receiver->id : 0,
406 NULL, q_text);
407 free(q_text);
408
409 if (!result)
410 return -EIO;
411
412 dbi_result_free(result);
413 return 0;
414}
415
416/* retrieve the next unsent SMS with ID >= min_id */
417struct gsm_sms *db_sms_get_unsent(int min_id)
418{
419 dbi_result result;
420 struct gsm_sms *sms = malloc(sizeof(*sms));
421
422 if (!sms) {
423 free(sms);
424 return NULL;
425 }
426
427 result = dbi_conn_queryf(conn,
428 "SELECT * FROM SMS "
429 "WHERE id >= %llu ORDER BY id", min_id);
430 if (!result) {
431 free(sms);
432 return NULL;
433 }
434
435 /* FIXME: fill gsm_sms from database */
436
437 dbi_result_free(result);
438 return sms;
439}
440
441/* mark a given SMS as read */
442int db_sms_mark_sent(struct gsm_sms *sms)
443{
444 dbi_result result;
445
446 result = dbi_conn_queryf(conn,
447 "UPDATE SMS "
448 "SET sent = datetime('now') "
449 "WHERE id = %llu", sms->id);
450 if (!result) {
451 printf("DB: Failed to mark SMS %llu as sent.\n", sms->id);
452 return 1;
453 }
454
455 dbi_result_free(result);
456 return 0;
457}