blob: 87b41bd855cd4b11d13ef288dec749ac3c106075 [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;
Holger Freyther36650b82009-04-19 06:35:16 +000035static struct gsm_network *current_network = NULL;
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, "
63 "imei NUMERIC UNIQUE NOT NULL"
64 ")",
65 "CREATE TABLE IF NOT EXISTS EquipmentWatch ("
66 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
67 "created TIMESTAMP NOT NULL, "
68 "updated TIMESTAMP NOT NULL, "
69 "subscriber_id NUMERIC NOT NULL, "
70 "equipment_id NUMERIC NOT NULL, "
71 "UNIQUE (subscriber_id, equipment_id) "
72 ")",
73 "CREATE TABLE IF NOT EXISTS SMS ("
74 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
75 "created TIMESTAMP NOT NULL, "
76 "sent TIMESTAMP, "
77 "sender_id NUMERIC NOT NULL, "
78 "receiver_id NUMERIC NOT NULL, "
79 "header NUMERIC, "
80 "text TEXT NOT NULL "
81 ")",
82};
83
Holger Freyther12aa50d2009-01-01 18:02:05 +000084void db_error_func(dbi_conn conn, void* data) {
Jan Luebbe5c15c852008-12-27 15:59:25 +000085 const char* msg;
86 dbi_conn_error(conn, &msg);
87 printf("DBI: %s\n", msg);
Jan Luebbe7398eb92008-12-27 00:45:41 +000088}
89
Holger Freyther36650b82009-04-19 06:35:16 +000090int db_init(const char *name, struct gsm_network *network) {
Jan Luebbe5c15c852008-12-27 15:59:25 +000091 dbi_initialize(NULL);
92 conn = dbi_conn_new("sqlite3");
93 if (conn==NULL) {
94 printf("DB: Failed to create connection.\n");
95 return 1;
96 }
Jan Luebbe7398eb92008-12-27 00:45:41 +000097
Holger Freyther12aa50d2009-01-01 18:02:05 +000098 dbi_conn_error_handler( conn, db_error_func, NULL );
Jan Luebbe7398eb92008-12-27 00:45:41 +000099
Jan Luebbe5c15c852008-12-27 15:59:25 +0000100 /* MySQL
101 dbi_conn_set_option(conn, "host", "localhost");
102 dbi_conn_set_option(conn, "username", "your_name");
103 dbi_conn_set_option(conn, "password", "your_password");
104 dbi_conn_set_option(conn, "dbname", "your_dbname");
105 dbi_conn_set_option(conn, "encoding", "UTF-8");
106 */
Jan Luebbe7398eb92008-12-27 00:45:41 +0000107
Jan Luebbe5c15c852008-12-27 15:59:25 +0000108 /* SqLite 3 */
Holger Freyther12aa50d2009-01-01 18:02:05 +0000109 db_basename = strdup(name);
110 db_dirname = strdup(name);
Holger Freytherbde36102008-12-28 22:51:39 +0000111 dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname));
112 dbi_conn_set_option(conn, "dbname", basename(db_basename));
Jan Luebbe7398eb92008-12-27 00:45:41 +0000113
Jan Luebbe5c15c852008-12-27 15:59:25 +0000114 if (dbi_conn_connect(conn) < 0) {
Holger Freytherbde36102008-12-28 22:51:39 +0000115 free(db_dirname);
116 free(db_basename);
117 db_dirname = db_basename = NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000118 return 1;
119 }
120
Holger Freyther36650b82009-04-19 06:35:16 +0000121 current_network = network;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000122 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000123}
124
125int db_prepare() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000126 dbi_result result;
Harald Welte7e310b12009-03-30 20:56:32 +0000127 int i;
Holger Freytherb4064bc2009-02-23 00:50:31 +0000128
Harald Welte7e310b12009-03-30 20:56:32 +0000129 for (i = 0; i < ARRAY_SIZE(create_stmts); i++) {
130 result = dbi_conn_query(conn, create_stmts[i]);
131 if (result==NULL) {
132 printf("DB: Failed to create some table.\n");
133 return 1;
134 }
135 dbi_result_free(result);
Holger Freytherb4064bc2009-02-23 00:50:31 +0000136 }
Holger Freytherb4064bc2009-02-23 00:50:31 +0000137
Jan Luebbe5c15c852008-12-27 15:59:25 +0000138 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000139}
140
141int db_fini() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000142 dbi_conn_close(conn);
143 dbi_shutdown();
Holger Freytherbde36102008-12-28 22:51:39 +0000144
145 if (db_dirname)
146 free(db_dirname);
147 if (db_basename)
148 free(db_basename);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000149 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000150}
151
Holger Freyther12aa50d2009-01-01 18:02:05 +0000152struct gsm_subscriber* db_create_subscriber(char *imsi) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000153 dbi_result result;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000154 struct gsm_subscriber* subscr;
155
156 /* Is this subscriber known in the db? */
157 subscr = db_get_subscriber(GSM_SUBSCRIBER_IMSI, imsi);
158 if (subscr) {
Harald Welte523200b2008-12-30 14:58:44 +0000159 result = dbi_conn_queryf(conn,
160 "UPDATE Subscriber set updated = datetime('now') "
161 "WHERE imsi = %s " , imsi);
162 if (result==NULL) {
163 printf("DB: failed to update timestamp\n");
164 } else {
165 dbi_result_free(result);
166 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000167 return subscr;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000168 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000169
170 subscr = subscr_alloc();
171 if (!subscr)
172 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000173 result = dbi_conn_queryf(conn,
Jan Luebbe391d86e2008-12-27 22:33:34 +0000174 "INSERT INTO Subscriber "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000175 "(imsi, created, updated) "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000176 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000177 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbe5c15c852008-12-27 15:59:25 +0000178 imsi
179 );
180 if (result==NULL) {
181 printf("DB: Failed to create Subscriber by IMSI.\n");
182 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000183 subscr->id = dbi_conn_sequence_last(conn, NULL);
184 strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000185 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000186 printf("DB: New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi);
187 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000188}
189
Holger Freytherca362a62009-01-04 21:05:01 +0000190struct gsm_subscriber *db_get_subscriber(enum gsm_subscriber_field field, const char *id) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000191 dbi_result result;
Jan Luebbe391d86e2008-12-27 22:33:34 +0000192 const char *string;
193 char *quoted;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000194 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000195
Jan Luebbe5c15c852008-12-27 15:59:25 +0000196 switch (field) {
197 case GSM_SUBSCRIBER_IMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000198 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000199 result = dbi_conn_queryf(conn,
200 "SELECT * FROM Subscriber "
201 "WHERE imsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000202 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000203 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000204 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000205 break;
206 case GSM_SUBSCRIBER_TMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000207 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000208 result = dbi_conn_queryf(conn,
209 "SELECT * FROM Subscriber "
210 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000211 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000212 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000213 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000214 break;
Holger Freyther9c564b82009-02-09 23:39:20 +0000215 case GSM_SUBSCRIBER_EXTENSION:
216 dbi_conn_quote_string_copy(conn, id, &quoted);
217 result = dbi_conn_queryf(conn,
218 "SELECT * FROM Subscriber "
219 "WHERE extension = %s ",
220 quoted
221 );
222 free(quoted);
223 break;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000224 default:
225 printf("DB: Unknown query selector for Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000226 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000227 }
228 if (result==NULL) {
229 printf("DB: Failed to query Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000230 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000231 }
232 if (!dbi_result_next_row(result)) {
Holger Freyther1ef983b2009-02-22 20:33:09 +0000233 printf("DB: Failed to find the Subscriber. '%u' '%s'\n",
234 field, id);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000235 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000236 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000237 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000238
239 subscr = subscr_alloc();
240 subscr->id = dbi_result_get_ulonglong(result, "id");
Harald Welte75a983f2008-12-27 21:34:06 +0000241 string = dbi_result_get_string(result, "imsi");
242 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000243 strncpy(subscr->imsi, string, GSM_IMSI_LENGTH);
Harald Welte75a983f2008-12-27 21:34:06 +0000244
245 string = dbi_result_get_string(result, "tmsi");
246 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000247 strncpy(subscr->tmsi, string, GSM_TMSI_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000248
249 string = dbi_result_get_string(result, "name");
250 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000251 strncpy(subscr->name, string, GSM_NAME_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000252
253 string = dbi_result_get_string(result, "extension");
254 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000255 strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000256
Holger Freyther12aa50d2009-01-01 18:02:05 +0000257 subscr->lac = dbi_result_get_uint(result, "lac");
258 subscr->authorized = dbi_result_get_uint(result, "authorized");
Jan Luebbe3444ddb2008-12-28 12:37:15 +0000259 printf("DB: Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %s, LAC %hu, AUTH %u\n",
Holger Freyther12aa50d2009-01-01 18:02:05 +0000260 subscr->id, subscr->imsi, subscr->name, subscr->tmsi,
261 subscr->lac, subscr->authorized);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000262 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000263 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000264}
265
Holger Freyther12aa50d2009-01-01 18:02:05 +0000266int db_sync_subscriber(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000267 dbi_result result;
268 result = dbi_conn_queryf(conn,
269 "UPDATE Subscriber "
Jan Luebbe391d86e2008-12-27 22:33:34 +0000270 "SET updated = datetime('now'), "
271 "tmsi = %s, "
272 "lac = %i, "
273 "authorized = %i "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000274 "WHERE imsi = %s ",
Jan Luebbe6e2e5452008-12-27 16:47:55 +0000275 subscriber->tmsi, subscriber->lac, subscriber->authorized, subscriber->imsi
Jan Luebbe5c15c852008-12-27 15:59:25 +0000276 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000277
Jan Luebbe5c15c852008-12-27 15:59:25 +0000278 if (result==NULL) {
279 printf("DB: Failed to update Subscriber (by IMSI).\n");
280 return 1;
281 }
282 dbi_result_free(result);
283 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000284}
285
Jan Luebbe5c15c852008-12-27 15:59:25 +0000286int db_subscriber_alloc_tmsi(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000287 dbi_result result=NULL;
288 char* tmsi_quoted;
289 for (;;) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000290 sprintf(subscriber->tmsi, "%i", rand());
291 dbi_conn_quote_string_copy(conn, subscriber->tmsi, &tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000292 result = dbi_conn_queryf(conn,
293 "SELECT * FROM Subscriber "
294 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000295 tmsi_quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000296 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000297 free(tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000298 if (result==NULL) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000299 printf("DB: Failed to query Subscriber while allocating new TMSI.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000300 return 1;
301 }
Jan Luebbe5c15c852008-12-27 15:59:25 +0000302 if (dbi_result_get_numrows(result)){
303 dbi_result_free(result);
304 continue;
305 }
306 if (!dbi_result_next_row(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000307 dbi_result_free(result);
308 printf("DB: Allocated TMSI %s for IMSI %s.\n", subscriber->tmsi, subscriber->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000309 return db_sync_subscriber(subscriber);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000310 }
311 dbi_result_free(result);
312 }
313 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000314}
315
Jan Luebbefac25fc2008-12-27 18:04:34 +0000316int db_subscriber_assoc_imei(struct gsm_subscriber* subscriber, char imei[GSM_IMEI_LENGTH]) {
317 u_int64_t equipment_id, watch_id;
318 dbi_result result;
319
320 result = dbi_conn_queryf(conn,
321 "INSERT OR IGNORE INTO Equipment "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000322 "(imei, created, updated) "
Jan Luebbefac25fc2008-12-27 18:04:34 +0000323 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000324 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbefac25fc2008-12-27 18:04:34 +0000325 imei
326 );
327 if (result==NULL) {
328 printf("DB: Failed to create Equipment by IMEI.\n");
329 return 1;
330 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000331 equipment_id = 0;
332 if (dbi_result_get_numrows_affected(result)) {
333 equipment_id = dbi_conn_sequence_last(conn, NULL);
334 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000335 dbi_result_free(result);
336 if (equipment_id) {
337 printf("DB: New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
338 }
339 else {
340 result = dbi_conn_queryf(conn,
341 "SELECT id FROM Equipment "
342 "WHERE imei = %s ",
343 imei
344 );
345 if (result==NULL) {
346 printf("DB: Failed to query Equipment by IMEI.\n");
347 return 1;
348 }
349 if (!dbi_result_next_row(result)) {
350 printf("DB: Failed to find the Equipment.\n");
351 dbi_result_free(result);
352 return 1;
353 }
354 equipment_id = dbi_result_get_ulonglong(result, "id");
355 dbi_result_free(result);
356 }
357
358 result = dbi_conn_queryf(conn,
359 "INSERT OR IGNORE INTO EquipmentWatch "
360 "(subscriber_id, equipment_id, created, updated) "
361 "VALUES "
362 "(%llu, %llu, datetime('now'), datetime('now')) ",
363 subscriber->id, equipment_id
364 );
365 if (result==NULL) {
366 printf("DB: Failed to create EquipmentWatch.\n");
367 return 1;
368 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000369 watch_id = 0;
370 if (dbi_result_get_numrows_affected(result)) {
371 watch_id = dbi_conn_sequence_last(conn, NULL);
372 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000373 dbi_result_free(result);
374 if (watch_id) {
375 printf("DB: New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
376 }
377 else {
378 result = dbi_conn_queryf(conn,
379 "UPDATE EquipmentWatch "
380 "SET updated = datetime('now') "
381 "WHERE subscriber_id = %llu AND equipment_id = %llu ",
382 subscriber->id, equipment_id
383 );
384 if (result==NULL) {
385 printf("DB: Failed to update EquipmentWatch.\n");
386 return 1;
387 }
388 dbi_result_free(result);
389 printf("DB: Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
390 }
391
392 return 0;
393}
394
Harald Welte7e310b12009-03-30 20:56:32 +0000395/* store an [unsent] SMS to the database */
396int db_sms_store(struct gsm_sms *sms)
397{
398 dbi_result result;
399 char *q_text;
400
401 dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
402 result = dbi_conn_queryf(conn,
403 "INSERT INTO SMS "
404 "(created,sender_id,receiver_id,header,text) VALUES "
405 "(datetime('now'),%llu,%llu,%s,%s)",
406 sms->sender->id,
407 sms->receiver ? sms->receiver->id : 0,
408 NULL, q_text);
409 free(q_text);
410
411 if (!result)
412 return -EIO;
413
414 dbi_result_free(result);
415 return 0;
416}
417
418/* retrieve the next unsent SMS with ID >= min_id */
419struct gsm_sms *db_sms_get_unsent(int min_id)
420{
421 dbi_result result;
422 struct gsm_sms *sms = malloc(sizeof(*sms));
423
424 if (!sms) {
425 free(sms);
426 return NULL;
427 }
428
429 result = dbi_conn_queryf(conn,
430 "SELECT * FROM SMS "
431 "WHERE id >= %llu ORDER BY id", min_id);
432 if (!result) {
433 free(sms);
434 return NULL;
435 }
436
437 /* FIXME: fill gsm_sms from database */
438
439 dbi_result_free(result);
440 return sms;
441}
442
443/* mark a given SMS as read */
444int db_sms_mark_sent(struct gsm_sms *sms)
445{
446 dbi_result result;
447
448 result = dbi_conn_queryf(conn,
449 "UPDATE SMS "
450 "SET sent = datetime('now') "
451 "WHERE id = %llu", sms->id);
452 if (!result) {
453 printf("DB: Failed to mark SMS %llu as sent.\n", sms->id);
454 return 1;
455 }
456
457 dbi_result_free(result);
458 return 0;
459}