blob: f2f691e113ba6ddee0175aeddb7b81dde306187b [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 ")",
Holger Freytherc2995ea2009-04-19 06:35:23 +000082 "CREATE TABLE IF NOT EXISTS VLR ("
83 "id INTEGER PRIMARY KEY AUTOINCREMENT, "
84 "created TIMESTAMP NOT NULL, "
85 "updated TIMESTAMP NOT NULL, "
86 "subscriber_id NUMERIC UNIQUE NOT NULL, "
87 "last_bts NUMERIC NOT NULL "
88 ")",
Harald Welte7e310b12009-03-30 20:56:32 +000089};
90
Holger Freyther12aa50d2009-01-01 18:02:05 +000091void db_error_func(dbi_conn conn, void* data) {
Jan Luebbe5c15c852008-12-27 15:59:25 +000092 const char* msg;
93 dbi_conn_error(conn, &msg);
94 printf("DBI: %s\n", msg);
Jan Luebbe7398eb92008-12-27 00:45:41 +000095}
96
Holger Freyther36650b82009-04-19 06:35:16 +000097int db_init(const char *name, struct gsm_network *network) {
Jan Luebbe5c15c852008-12-27 15:59:25 +000098 dbi_initialize(NULL);
99 conn = dbi_conn_new("sqlite3");
100 if (conn==NULL) {
101 printf("DB: Failed to create connection.\n");
102 return 1;
103 }
Jan Luebbe7398eb92008-12-27 00:45:41 +0000104
Holger Freyther12aa50d2009-01-01 18:02:05 +0000105 dbi_conn_error_handler( conn, db_error_func, NULL );
Jan Luebbe7398eb92008-12-27 00:45:41 +0000106
Jan Luebbe5c15c852008-12-27 15:59:25 +0000107 /* MySQL
108 dbi_conn_set_option(conn, "host", "localhost");
109 dbi_conn_set_option(conn, "username", "your_name");
110 dbi_conn_set_option(conn, "password", "your_password");
111 dbi_conn_set_option(conn, "dbname", "your_dbname");
112 dbi_conn_set_option(conn, "encoding", "UTF-8");
113 */
Jan Luebbe7398eb92008-12-27 00:45:41 +0000114
Jan Luebbe5c15c852008-12-27 15:59:25 +0000115 /* SqLite 3 */
Holger Freyther12aa50d2009-01-01 18:02:05 +0000116 db_basename = strdup(name);
117 db_dirname = strdup(name);
Holger Freytherbde36102008-12-28 22:51:39 +0000118 dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname));
119 dbi_conn_set_option(conn, "dbname", basename(db_basename));
Jan Luebbe7398eb92008-12-27 00:45:41 +0000120
Jan Luebbe5c15c852008-12-27 15:59:25 +0000121 if (dbi_conn_connect(conn) < 0) {
Holger Freytherbde36102008-12-28 22:51:39 +0000122 free(db_dirname);
123 free(db_basename);
124 db_dirname = db_basename = NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000125 return 1;
126 }
127
Holger Freyther36650b82009-04-19 06:35:16 +0000128 current_network = network;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000129 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000130}
131
132int db_prepare() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000133 dbi_result result;
Harald Welte7e310b12009-03-30 20:56:32 +0000134 int i;
Holger Freytherb4064bc2009-02-23 00:50:31 +0000135
Harald Welte7e310b12009-03-30 20:56:32 +0000136 for (i = 0; i < ARRAY_SIZE(create_stmts); i++) {
137 result = dbi_conn_query(conn, create_stmts[i]);
138 if (result==NULL) {
139 printf("DB: Failed to create some table.\n");
140 return 1;
141 }
142 dbi_result_free(result);
Holger Freytherb4064bc2009-02-23 00:50:31 +0000143 }
Holger Freytherb4064bc2009-02-23 00:50:31 +0000144
Jan Luebbe5c15c852008-12-27 15:59:25 +0000145 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000146}
147
148int db_fini() {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000149 dbi_conn_close(conn);
150 dbi_shutdown();
Holger Freytherbde36102008-12-28 22:51:39 +0000151
152 if (db_dirname)
153 free(db_dirname);
154 if (db_basename)
155 free(db_basename);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000156 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000157}
158
Holger Freyther12aa50d2009-01-01 18:02:05 +0000159struct gsm_subscriber* db_create_subscriber(char *imsi) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000160 dbi_result result;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000161 struct gsm_subscriber* subscr;
162
163 /* Is this subscriber known in the db? */
164 subscr = db_get_subscriber(GSM_SUBSCRIBER_IMSI, imsi);
165 if (subscr) {
Harald Welte523200b2008-12-30 14:58:44 +0000166 result = dbi_conn_queryf(conn,
167 "UPDATE Subscriber set updated = datetime('now') "
168 "WHERE imsi = %s " , imsi);
169 if (result==NULL) {
170 printf("DB: failed to update timestamp\n");
171 } else {
172 dbi_result_free(result);
173 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000174 return subscr;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000175 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000176
177 subscr = subscr_alloc();
178 if (!subscr)
179 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000180 result = dbi_conn_queryf(conn,
Jan Luebbe391d86e2008-12-27 22:33:34 +0000181 "INSERT INTO Subscriber "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000182 "(imsi, created, updated) "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000183 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000184 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbe5c15c852008-12-27 15:59:25 +0000185 imsi
186 );
187 if (result==NULL) {
188 printf("DB: Failed to create Subscriber by IMSI.\n");
189 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000190 subscr->id = dbi_conn_sequence_last(conn, NULL);
191 strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000192 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000193 printf("DB: New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi);
194 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000195}
196
Holger Freytherca362a62009-01-04 21:05:01 +0000197struct gsm_subscriber *db_get_subscriber(enum gsm_subscriber_field field, const char *id) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000198 dbi_result result;
Jan Luebbe391d86e2008-12-27 22:33:34 +0000199 const char *string;
200 char *quoted;
Holger Freyther12aa50d2009-01-01 18:02:05 +0000201 struct gsm_subscriber *subscr;
Harald Welte75a983f2008-12-27 21:34:06 +0000202
Jan Luebbe5c15c852008-12-27 15:59:25 +0000203 switch (field) {
204 case GSM_SUBSCRIBER_IMSI:
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 imsi = %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;
213 case GSM_SUBSCRIBER_TMSI:
Holger Freyther12aa50d2009-01-01 18:02:05 +0000214 dbi_conn_quote_string_copy(conn, id, &quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000215 result = dbi_conn_queryf(conn,
216 "SELECT * FROM Subscriber "
217 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000218 quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000219 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000220 free(quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000221 break;
Holger Freyther9c564b82009-02-09 23:39:20 +0000222 case GSM_SUBSCRIBER_EXTENSION:
223 dbi_conn_quote_string_copy(conn, id, &quoted);
224 result = dbi_conn_queryf(conn,
225 "SELECT * FROM Subscriber "
226 "WHERE extension = %s ",
227 quoted
228 );
229 free(quoted);
230 break;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000231 default:
232 printf("DB: Unknown query selector for Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000233 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000234 }
235 if (result==NULL) {
236 printf("DB: Failed to query Subscriber.\n");
Holger Freyther12aa50d2009-01-01 18:02:05 +0000237 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000238 }
239 if (!dbi_result_next_row(result)) {
Holger Freyther1ef983b2009-02-22 20:33:09 +0000240 printf("DB: Failed to find the Subscriber. '%u' '%s'\n",
241 field, id);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000242 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000243 return NULL;
Jan Luebbe5c15c852008-12-27 15:59:25 +0000244 }
Holger Freyther12aa50d2009-01-01 18:02:05 +0000245
246 subscr = subscr_alloc();
247 subscr->id = dbi_result_get_ulonglong(result, "id");
Harald Welte75a983f2008-12-27 21:34:06 +0000248 string = dbi_result_get_string(result, "imsi");
249 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000250 strncpy(subscr->imsi, string, GSM_IMSI_LENGTH);
Harald Welte75a983f2008-12-27 21:34:06 +0000251
252 string = dbi_result_get_string(result, "tmsi");
253 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000254 strncpy(subscr->tmsi, string, GSM_TMSI_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000255
256 string = dbi_result_get_string(result, "name");
257 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000258 strncpy(subscr->name, string, GSM_NAME_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000259
260 string = dbi_result_get_string(result, "extension");
261 if (string)
Holger Freyther12aa50d2009-01-01 18:02:05 +0000262 strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH);
Jan Luebbe391d86e2008-12-27 22:33:34 +0000263
Holger Freyther12aa50d2009-01-01 18:02:05 +0000264 subscr->lac = dbi_result_get_uint(result, "lac");
265 subscr->authorized = dbi_result_get_uint(result, "authorized");
Jan Luebbe3444ddb2008-12-28 12:37:15 +0000266 printf("DB: Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %s, LAC %hu, AUTH %u\n",
Holger Freyther12aa50d2009-01-01 18:02:05 +0000267 subscr->id, subscr->imsi, subscr->name, subscr->tmsi,
268 subscr->lac, subscr->authorized);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000269 dbi_result_free(result);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000270 return subscr;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000271}
272
Holger Freyther12aa50d2009-01-01 18:02:05 +0000273int db_sync_subscriber(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000274 dbi_result result;
275 result = dbi_conn_queryf(conn,
276 "UPDATE Subscriber "
Jan Luebbe391d86e2008-12-27 22:33:34 +0000277 "SET updated = datetime('now'), "
278 "tmsi = %s, "
279 "lac = %i, "
280 "authorized = %i "
Jan Luebbe5c15c852008-12-27 15:59:25 +0000281 "WHERE imsi = %s ",
Jan Luebbe6e2e5452008-12-27 16:47:55 +0000282 subscriber->tmsi, subscriber->lac, subscriber->authorized, subscriber->imsi
Jan Luebbe5c15c852008-12-27 15:59:25 +0000283 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000284
Jan Luebbe5c15c852008-12-27 15:59:25 +0000285 if (result==NULL) {
286 printf("DB: Failed to update Subscriber (by IMSI).\n");
287 return 1;
288 }
289 dbi_result_free(result);
290 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000291}
292
Jan Luebbe5c15c852008-12-27 15:59:25 +0000293int db_subscriber_alloc_tmsi(struct gsm_subscriber* subscriber) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000294 dbi_result result=NULL;
295 char* tmsi_quoted;
296 for (;;) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000297 sprintf(subscriber->tmsi, "%i", rand());
298 dbi_conn_quote_string_copy(conn, subscriber->tmsi, &tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000299 result = dbi_conn_queryf(conn,
300 "SELECT * FROM Subscriber "
301 "WHERE tmsi = %s ",
Jan Luebbe391d86e2008-12-27 22:33:34 +0000302 tmsi_quoted
Jan Luebbe5c15c852008-12-27 15:59:25 +0000303 );
Holger Freyther12aa50d2009-01-01 18:02:05 +0000304 free(tmsi_quoted);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000305 if (result==NULL) {
Jan Luebbe391d86e2008-12-27 22:33:34 +0000306 printf("DB: Failed to query Subscriber while allocating new TMSI.\n");
Jan Luebbe5c15c852008-12-27 15:59:25 +0000307 return 1;
308 }
Jan Luebbe5c15c852008-12-27 15:59:25 +0000309 if (dbi_result_get_numrows(result)){
310 dbi_result_free(result);
311 continue;
312 }
313 if (!dbi_result_next_row(result)) {
Jan Luebbe5c15c852008-12-27 15:59:25 +0000314 dbi_result_free(result);
315 printf("DB: Allocated TMSI %s for IMSI %s.\n", subscriber->tmsi, subscriber->imsi);
Holger Freyther12aa50d2009-01-01 18:02:05 +0000316 return db_sync_subscriber(subscriber);
Jan Luebbe5c15c852008-12-27 15:59:25 +0000317 }
318 dbi_result_free(result);
319 }
320 return 0;
Jan Luebbe7398eb92008-12-27 00:45:41 +0000321}
322
Jan Luebbefac25fc2008-12-27 18:04:34 +0000323int db_subscriber_assoc_imei(struct gsm_subscriber* subscriber, char imei[GSM_IMEI_LENGTH]) {
324 u_int64_t equipment_id, watch_id;
325 dbi_result result;
326
327 result = dbi_conn_queryf(conn,
328 "INSERT OR IGNORE INTO Equipment "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000329 "(imei, created, updated) "
Jan Luebbefac25fc2008-12-27 18:04:34 +0000330 "VALUES "
Jan Luebbee30dbb32008-12-27 18:08:13 +0000331 "(%s, datetime('now'), datetime('now')) ",
Jan Luebbefac25fc2008-12-27 18:04:34 +0000332 imei
333 );
334 if (result==NULL) {
335 printf("DB: Failed to create Equipment by IMEI.\n");
336 return 1;
337 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000338 equipment_id = 0;
339 if (dbi_result_get_numrows_affected(result)) {
340 equipment_id = dbi_conn_sequence_last(conn, NULL);
341 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000342 dbi_result_free(result);
343 if (equipment_id) {
344 printf("DB: New Equipment: ID %llu, IMEI %s\n", equipment_id, imei);
345 }
346 else {
347 result = dbi_conn_queryf(conn,
348 "SELECT id FROM Equipment "
349 "WHERE imei = %s ",
350 imei
351 );
352 if (result==NULL) {
353 printf("DB: Failed to query Equipment by IMEI.\n");
354 return 1;
355 }
356 if (!dbi_result_next_row(result)) {
357 printf("DB: Failed to find the Equipment.\n");
358 dbi_result_free(result);
359 return 1;
360 }
361 equipment_id = dbi_result_get_ulonglong(result, "id");
362 dbi_result_free(result);
363 }
364
365 result = dbi_conn_queryf(conn,
366 "INSERT OR IGNORE INTO EquipmentWatch "
367 "(subscriber_id, equipment_id, created, updated) "
368 "VALUES "
369 "(%llu, %llu, datetime('now'), datetime('now')) ",
370 subscriber->id, equipment_id
371 );
372 if (result==NULL) {
373 printf("DB: Failed to create EquipmentWatch.\n");
374 return 1;
375 }
Jan Luebbe391d86e2008-12-27 22:33:34 +0000376 watch_id = 0;
377 if (dbi_result_get_numrows_affected(result)) {
378 watch_id = dbi_conn_sequence_last(conn, NULL);
379 }
Jan Luebbefac25fc2008-12-27 18:04:34 +0000380 dbi_result_free(result);
381 if (watch_id) {
382 printf("DB: New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
383 }
384 else {
385 result = dbi_conn_queryf(conn,
386 "UPDATE EquipmentWatch "
387 "SET updated = datetime('now') "
388 "WHERE subscriber_id = %llu AND equipment_id = %llu ",
389 subscriber->id, equipment_id
390 );
391 if (result==NULL) {
392 printf("DB: Failed to update EquipmentWatch.\n");
393 return 1;
394 }
395 dbi_result_free(result);
396 printf("DB: Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei);
397 }
398
399 return 0;
400}
401
Harald Welte7e310b12009-03-30 20:56:32 +0000402/* store an [unsent] SMS to the database */
403int db_sms_store(struct gsm_sms *sms)
404{
405 dbi_result result;
406 char *q_text;
407
408 dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text);
409 result = dbi_conn_queryf(conn,
410 "INSERT INTO SMS "
411 "(created,sender_id,receiver_id,header,text) VALUES "
412 "(datetime('now'),%llu,%llu,%s,%s)",
413 sms->sender->id,
414 sms->receiver ? sms->receiver->id : 0,
415 NULL, q_text);
416 free(q_text);
417
418 if (!result)
419 return -EIO;
420
421 dbi_result_free(result);
422 return 0;
423}
424
425/* retrieve the next unsent SMS with ID >= min_id */
426struct gsm_sms *db_sms_get_unsent(int min_id)
427{
428 dbi_result result;
429 struct gsm_sms *sms = malloc(sizeof(*sms));
430
431 if (!sms) {
432 free(sms);
433 return NULL;
434 }
435
436 result = dbi_conn_queryf(conn,
437 "SELECT * FROM SMS "
438 "WHERE id >= %llu ORDER BY id", min_id);
439 if (!result) {
440 free(sms);
441 return NULL;
442 }
443
444 /* FIXME: fill gsm_sms from database */
445
446 dbi_result_free(result);
447 return sms;
448}
449
450/* mark a given SMS as read */
451int db_sms_mark_sent(struct gsm_sms *sms)
452{
453 dbi_result result;
454
455 result = dbi_conn_queryf(conn,
456 "UPDATE SMS "
457 "SET sent = datetime('now') "
458 "WHERE id = %llu", sms->id);
459 if (!result) {
460 printf("DB: Failed to mark SMS %llu as sent.\n", sms->id);
461 return 1;
462 }
463
464 dbi_result_free(result);
465 return 0;
466}