Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 1 | /* Simple HLR/VLR database backend using dbi */ |
Jan Luebbe | faaa49c | 2008-12-27 01:07:07 +0000 | [diff] [blame] | 2 | /* (C) 2008 by Jan Luebbe <jluebbe@debian.org> |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 3 | * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
Harald Welte | c2e302d | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 4 | * (C) 2009 by Harald Welte <laforge@gnumonks.org> |
Jan Luebbe | faaa49c | 2008-12-27 01:07:07 +0000 | [diff] [blame] | 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 23 | #include <openbsc/gsm_data.h> |
Jan Luebbe | faaa49c | 2008-12-27 01:07:07 +0000 | [diff] [blame] | 24 | #include <openbsc/db.h> |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 25 | #include <openbsc/talloc.h> |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 26 | |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 27 | #include <libgen.h> |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 28 | #include <stdio.h> |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 31 | #include <errno.h> |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 32 | #include <dbi/dbi.h> |
| 33 | |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 34 | static char *db_basename = NULL; |
| 35 | static char *db_dirname = NULL; |
Holger Freyther | 1d506c8 | 2009-04-19 06:35:20 +0000 | [diff] [blame] | 36 | static dbi_conn conn; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 37 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 38 | static char *create_stmts[] = { |
| 39 | "CREATE TABLE IF NOT EXISTS Meta (" |
| 40 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 41 | "key TEXT UNIQUE NOT NULL, " |
| 42 | "value TEXT NOT NULL" |
| 43 | ")", |
| 44 | "INSERT OR IGNORE INTO Meta " |
| 45 | "(key, value) " |
| 46 | "VALUES " |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 47 | "('revision', '2')", |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 48 | "CREATE TABLE IF NOT EXISTS Subscriber (" |
| 49 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 50 | "created TIMESTAMP NOT NULL, " |
| 51 | "updated TIMESTAMP NOT NULL, " |
| 52 | "imsi NUMERIC UNIQUE NOT NULL, " |
| 53 | "name TEXT, " |
| 54 | "extension TEXT UNIQUE, " |
| 55 | "authorized INTEGER NOT NULL DEFAULT 0, " |
| 56 | "tmsi TEXT UNIQUE, " |
| 57 | "lac INTEGER NOT NULL DEFAULT 0" |
| 58 | ")", |
| 59 | "CREATE TABLE IF NOT EXISTS Equipment (" |
| 60 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 61 | "created TIMESTAMP NOT NULL, " |
| 62 | "updated TIMESTAMP NOT NULL, " |
| 63 | "name TEXT, " |
Harald Welte | c2e302d | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 64 | "classmark1 NUMERIC, " |
| 65 | "classmark2 BLOB, " |
| 66 | "classmark3 BLOB, " |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 67 | "imei NUMERIC UNIQUE NOT NULL" |
| 68 | ")", |
| 69 | "CREATE TABLE IF NOT EXISTS EquipmentWatch (" |
| 70 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 71 | "created TIMESTAMP NOT NULL, " |
| 72 | "updated TIMESTAMP NOT NULL, " |
| 73 | "subscriber_id NUMERIC NOT NULL, " |
| 74 | "equipment_id NUMERIC NOT NULL, " |
| 75 | "UNIQUE (subscriber_id, equipment_id) " |
| 76 | ")", |
| 77 | "CREATE TABLE IF NOT EXISTS SMS (" |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 78 | /* metadata, not part of sms */ |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 79 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 80 | "created TIMESTAMP NOT NULL, " |
| 81 | "sent TIMESTAMP, " |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 82 | "sender_id INTEGER NOT NULL, " |
| 83 | "receiver_id INTEGER NOT NULL, " |
| 84 | /* data directly copied/derived from SMS */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 85 | "valid_until TIMESTAMP, " |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 86 | "reply_path_req INTEGER NOT NULL, " |
| 87 | "status_rep_req INTEGER NOT NULL, " |
| 88 | "protocol_id INTEGER NOT NULL, " |
| 89 | "data_coding_scheme INTEGER NOT NULL, " |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 90 | "ud_hdr_ind INTEGER NOT NULL, " |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 91 | "dest_addr TEXT, " |
| 92 | "user_data BLOB, " /* TP-UD */ |
| 93 | /* additional data, interpreted from SMS */ |
| 94 | "header BLOB, " /* UD Header */ |
| 95 | "text TEXT " /* decoded UD after UDH */ |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 96 | ")", |
Holger Freyther | c2995ea | 2009-04-19 06:35:23 +0000 | [diff] [blame] | 97 | "CREATE TABLE IF NOT EXISTS VLR (" |
| 98 | "id INTEGER PRIMARY KEY AUTOINCREMENT, " |
| 99 | "created TIMESTAMP NOT NULL, " |
| 100 | "updated TIMESTAMP NOT NULL, " |
| 101 | "subscriber_id NUMERIC UNIQUE NOT NULL, " |
| 102 | "last_bts NUMERIC NOT NULL " |
| 103 | ")", |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 106 | void db_error_func(dbi_conn conn, void* data) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 107 | const char* msg; |
| 108 | dbi_conn_error(conn, &msg); |
| 109 | printf("DBI: %s\n", msg); |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 112 | static int check_db_revision(void) |
| 113 | { |
| 114 | dbi_result result; |
| 115 | const char *rev; |
| 116 | |
| 117 | result = dbi_conn_query(conn, |
| 118 | "SELECT value FROM Meta WHERE key='revision'"); |
| 119 | if (!result) |
| 120 | return -EINVAL; |
| 121 | |
| 122 | if (!dbi_result_next_row(result)) { |
| 123 | dbi_result_free(result); |
| 124 | return -EINVAL; |
| 125 | } |
| 126 | rev = dbi_result_get_string(result, "value"); |
| 127 | if (!rev || atoi(rev) != 2) { |
| 128 | dbi_result_free(result); |
| 129 | return -EINVAL; |
| 130 | } |
| 131 | |
| 132 | dbi_result_free(result); |
| 133 | return 0; |
| 134 | } |
| 135 | |
Holger Freyther | c7b86f9 | 2009-06-06 13:54:20 +0000 | [diff] [blame] | 136 | int db_init(const char *name) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 137 | dbi_initialize(NULL); |
| 138 | conn = dbi_conn_new("sqlite3"); |
| 139 | if (conn==NULL) { |
| 140 | printf("DB: Failed to create connection.\n"); |
| 141 | return 1; |
| 142 | } |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 143 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 144 | dbi_conn_error_handler( conn, db_error_func, NULL ); |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 145 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 146 | /* MySQL |
| 147 | dbi_conn_set_option(conn, "host", "localhost"); |
| 148 | dbi_conn_set_option(conn, "username", "your_name"); |
| 149 | dbi_conn_set_option(conn, "password", "your_password"); |
| 150 | dbi_conn_set_option(conn, "dbname", "your_dbname"); |
| 151 | dbi_conn_set_option(conn, "encoding", "UTF-8"); |
| 152 | */ |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 153 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 154 | /* SqLite 3 */ |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 155 | db_basename = strdup(name); |
| 156 | db_dirname = strdup(name); |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 157 | dbi_conn_set_option(conn, "sqlite3_dbdir", dirname(db_dirname)); |
| 158 | dbi_conn_set_option(conn, "dbname", basename(db_basename)); |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 159 | |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 160 | if (dbi_conn_connect(conn) < 0) |
| 161 | goto out_err; |
| 162 | |
| 163 | if (check_db_revision() < 0) { |
| 164 | fprintf(stderr, "Database schema revision invalid, " |
| 165 | "please update your database schema\n"); |
| 166 | goto out_err; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | return 0; |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 170 | |
| 171 | out_err: |
| 172 | free(db_dirname); |
| 173 | free(db_basename); |
| 174 | db_dirname = db_basename = NULL; |
| 175 | return -1; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 178 | |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 179 | int db_prepare() { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 180 | dbi_result result; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 181 | int i; |
Holger Freyther | b4064bc | 2009-02-23 00:50:31 +0000 | [diff] [blame] | 182 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 183 | for (i = 0; i < ARRAY_SIZE(create_stmts); i++) { |
| 184 | result = dbi_conn_query(conn, create_stmts[i]); |
| 185 | if (result==NULL) { |
| 186 | printf("DB: Failed to create some table.\n"); |
| 187 | return 1; |
| 188 | } |
| 189 | dbi_result_free(result); |
Holger Freyther | b4064bc | 2009-02-23 00:50:31 +0000 | [diff] [blame] | 190 | } |
Holger Freyther | b4064bc | 2009-02-23 00:50:31 +0000 | [diff] [blame] | 191 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 192 | return 0; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | int db_fini() { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 196 | dbi_conn_close(conn); |
| 197 | dbi_shutdown(); |
Holger Freyther | bde3610 | 2008-12-28 22:51:39 +0000 | [diff] [blame] | 198 | |
| 199 | if (db_dirname) |
| 200 | free(db_dirname); |
| 201 | if (db_basename) |
| 202 | free(db_basename); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 203 | return 0; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Harald Welte | 9176bd4 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 206 | struct gsm_subscriber* db_create_subscriber(struct gsm_network *net, char *imsi) |
| 207 | { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 208 | dbi_result result; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 209 | struct gsm_subscriber* subscr; |
| 210 | |
| 211 | /* Is this subscriber known in the db? */ |
Harald Welte | 9176bd4 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 212 | subscr = db_get_subscriber(net, GSM_SUBSCRIBER_IMSI, imsi); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 213 | if (subscr) { |
Harald Welte | 523200b | 2008-12-30 14:58:44 +0000 | [diff] [blame] | 214 | result = dbi_conn_queryf(conn, |
| 215 | "UPDATE Subscriber set updated = datetime('now') " |
| 216 | "WHERE imsi = %s " , imsi); |
| 217 | if (result==NULL) { |
| 218 | printf("DB: failed to update timestamp\n"); |
| 219 | } else { |
| 220 | dbi_result_free(result); |
| 221 | } |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 222 | return subscr; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 223 | } |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 224 | |
| 225 | subscr = subscr_alloc(); |
| 226 | if (!subscr) |
| 227 | return NULL; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 228 | result = dbi_conn_queryf(conn, |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 229 | "INSERT INTO Subscriber " |
Jan Luebbe | e30dbb3 | 2008-12-27 18:08:13 +0000 | [diff] [blame] | 230 | "(imsi, created, updated) " |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 231 | "VALUES " |
Jan Luebbe | e30dbb3 | 2008-12-27 18:08:13 +0000 | [diff] [blame] | 232 | "(%s, datetime('now'), datetime('now')) ", |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 233 | imsi |
| 234 | ); |
| 235 | if (result==NULL) { |
| 236 | printf("DB: Failed to create Subscriber by IMSI.\n"); |
| 237 | } |
Harald Welte | 9176bd4 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 238 | subscr->net = net; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 239 | subscr->id = dbi_conn_sequence_last(conn, NULL); |
| 240 | strncpy(subscr->imsi, imsi, GSM_IMSI_LENGTH-1); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 241 | dbi_result_free(result); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 242 | printf("DB: New Subscriber: ID %llu, IMSI %s\n", subscr->id, subscr->imsi); |
| 243 | return subscr; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Harald Welte | 9176bd4 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 246 | struct gsm_subscriber *db_get_subscriber(struct gsm_network *net, |
| 247 | enum gsm_subscriber_field field, |
| 248 | const char *id) |
| 249 | { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 250 | dbi_result result; |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 251 | const char *string; |
| 252 | char *quoted; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 253 | struct gsm_subscriber *subscr; |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 254 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 255 | switch (field) { |
| 256 | case GSM_SUBSCRIBER_IMSI: |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 257 | dbi_conn_quote_string_copy(conn, id, "ed); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 258 | result = dbi_conn_queryf(conn, |
| 259 | "SELECT * FROM Subscriber " |
| 260 | "WHERE imsi = %s ", |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 261 | quoted |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 262 | ); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 263 | free(quoted); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 264 | break; |
| 265 | case GSM_SUBSCRIBER_TMSI: |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 266 | dbi_conn_quote_string_copy(conn, id, "ed); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 267 | result = dbi_conn_queryf(conn, |
| 268 | "SELECT * FROM Subscriber " |
| 269 | "WHERE tmsi = %s ", |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 270 | quoted |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 271 | ); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 272 | free(quoted); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 273 | break; |
Holger Freyther | 9c564b8 | 2009-02-09 23:39:20 +0000 | [diff] [blame] | 274 | case GSM_SUBSCRIBER_EXTENSION: |
| 275 | dbi_conn_quote_string_copy(conn, id, "ed); |
| 276 | result = dbi_conn_queryf(conn, |
| 277 | "SELECT * FROM Subscriber " |
| 278 | "WHERE extension = %s ", |
| 279 | quoted |
| 280 | ); |
| 281 | free(quoted); |
| 282 | break; |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 283 | case GSM_SUBSCRIBER_ID: |
| 284 | dbi_conn_quote_string_copy(conn, id, "ed); |
| 285 | result = dbi_conn_queryf(conn, |
| 286 | "SELECT * FROM Subscriber " |
| 287 | "WHERE id = %s ", quoted); |
| 288 | free(quoted); |
| 289 | break; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 290 | default: |
| 291 | printf("DB: Unknown query selector for Subscriber.\n"); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 292 | return NULL; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 293 | } |
| 294 | if (result==NULL) { |
| 295 | printf("DB: Failed to query Subscriber.\n"); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 296 | return NULL; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 297 | } |
| 298 | if (!dbi_result_next_row(result)) { |
Holger Freyther | 1ef983b | 2009-02-22 20:33:09 +0000 | [diff] [blame] | 299 | printf("DB: Failed to find the Subscriber. '%u' '%s'\n", |
| 300 | field, id); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 301 | dbi_result_free(result); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 302 | return NULL; |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 303 | } |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 304 | |
| 305 | subscr = subscr_alloc(); |
Harald Welte | 9176bd4 | 2009-07-23 18:46:00 +0200 | [diff] [blame] | 306 | subscr->net = net; |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 307 | subscr->id = dbi_result_get_ulonglong(result, "id"); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 308 | string = dbi_result_get_string(result, "imsi"); |
| 309 | if (string) |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 310 | strncpy(subscr->imsi, string, GSM_IMSI_LENGTH); |
Harald Welte | 75a983f | 2008-12-27 21:34:06 +0000 | [diff] [blame] | 311 | |
| 312 | string = dbi_result_get_string(result, "tmsi"); |
| 313 | if (string) |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 314 | strncpy(subscr->tmsi, string, GSM_TMSI_LENGTH); |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 315 | |
| 316 | string = dbi_result_get_string(result, "name"); |
| 317 | if (string) |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 318 | strncpy(subscr->name, string, GSM_NAME_LENGTH); |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 319 | |
| 320 | string = dbi_result_get_string(result, "extension"); |
| 321 | if (string) |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 322 | strncpy(subscr->extension, string, GSM_EXTENSION_LENGTH); |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 323 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 324 | subscr->lac = dbi_result_get_uint(result, "lac"); |
| 325 | subscr->authorized = dbi_result_get_uint(result, "authorized"); |
Holger Freyther | 9175447 | 2009-06-09 08:52:41 +0000 | [diff] [blame] | 326 | printf("DB: Found Subscriber: ID %llu, IMSI %s, NAME '%s', TMSI %s, EXTEN '%s', LAC %hu, AUTH %u\n", |
| 327 | subscr->id, subscr->imsi, subscr->name, subscr->tmsi, subscr->extension, |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 328 | subscr->lac, subscr->authorized); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 329 | dbi_result_free(result); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 330 | return subscr; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 333 | int db_sync_subscriber(struct gsm_subscriber* subscriber) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 334 | dbi_result result; |
| 335 | result = dbi_conn_queryf(conn, |
| 336 | "UPDATE Subscriber " |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 337 | "SET updated = datetime('now'), " |
Holger Freyther | 9175447 | 2009-06-09 08:52:41 +0000 | [diff] [blame] | 338 | "tmsi = '%s', " |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 339 | "lac = %i, " |
| 340 | "authorized = %i " |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 341 | "WHERE imsi = %s ", |
Jan Luebbe | 6e2e545 | 2008-12-27 16:47:55 +0000 | [diff] [blame] | 342 | subscriber->tmsi, subscriber->lac, subscriber->authorized, subscriber->imsi |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 343 | ); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 344 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 345 | if (result==NULL) { |
| 346 | printf("DB: Failed to update Subscriber (by IMSI).\n"); |
| 347 | return 1; |
| 348 | } |
| 349 | dbi_result_free(result); |
| 350 | return 0; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Harald Welte | c2e302d | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 353 | int db_sync_equipment(struct gsm_equipment *equip) |
| 354 | { |
| 355 | dbi_result result; |
| 356 | unsigned char *cm2, *cm3; |
| 357 | |
| 358 | dbi_conn_quote_binary_copy(conn, equip->classmark2, |
| 359 | equip->classmark2_len, &cm2); |
| 360 | dbi_conn_quote_binary_copy(conn, equip->classmark3, |
| 361 | equip->classmark3_len, &cm3); |
| 362 | |
| 363 | result = dbi_conn_queryf(conn, |
| 364 | "UPDATE Equipment SET " |
| 365 | "updated = datetime('now'), " |
| 366 | "classmark1 = %u, " |
| 367 | "classmark2 = %s, " |
| 368 | "classmark3 = %s " |
| 369 | "WHERE imei = '%s' ", |
| 370 | equip->classmark1, cm2, cm3, equip->imei); |
| 371 | |
| 372 | free(cm2); |
| 373 | free(cm3); |
| 374 | |
| 375 | if (!result) { |
| 376 | printf("DB: Failed to update Equipment\n"); |
| 377 | return -EIO; |
| 378 | } |
| 379 | |
| 380 | dbi_result_free(result); |
| 381 | return 0; |
| 382 | } |
| 383 | |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 384 | int db_subscriber_alloc_tmsi(struct gsm_subscriber* subscriber) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 385 | dbi_result result=NULL; |
| 386 | char* tmsi_quoted; |
| 387 | for (;;) { |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 388 | sprintf(subscriber->tmsi, "%i", rand()); |
| 389 | dbi_conn_quote_string_copy(conn, subscriber->tmsi, &tmsi_quoted); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 390 | result = dbi_conn_queryf(conn, |
| 391 | "SELECT * FROM Subscriber " |
| 392 | "WHERE tmsi = %s ", |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 393 | tmsi_quoted |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 394 | ); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 395 | free(tmsi_quoted); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 396 | if (result==NULL) { |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 397 | printf("DB: Failed to query Subscriber while allocating new TMSI.\n"); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 398 | return 1; |
| 399 | } |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 400 | if (dbi_result_get_numrows(result)){ |
| 401 | dbi_result_free(result); |
| 402 | continue; |
| 403 | } |
| 404 | if (!dbi_result_next_row(result)) { |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 405 | dbi_result_free(result); |
| 406 | printf("DB: Allocated TMSI %s for IMSI %s.\n", subscriber->tmsi, subscriber->imsi); |
Holger Freyther | 12aa50d | 2009-01-01 18:02:05 +0000 | [diff] [blame] | 407 | return db_sync_subscriber(subscriber); |
Jan Luebbe | 5c15c85 | 2008-12-27 15:59:25 +0000 | [diff] [blame] | 408 | } |
| 409 | dbi_result_free(result); |
| 410 | } |
| 411 | return 0; |
Jan Luebbe | 7398eb9 | 2008-12-27 00:45:41 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 414 | int db_subscriber_assoc_imei(struct gsm_subscriber* subscriber, char imei[GSM_IMEI_LENGTH]) { |
| 415 | u_int64_t equipment_id, watch_id; |
| 416 | dbi_result result; |
| 417 | |
Harald Welte | c2e302d | 2009-07-05 14:08:13 +0200 | [diff] [blame] | 418 | strncpy(subscriber->equipment.imei, imei, |
| 419 | sizeof(subscriber->equipment.imei)-1), |
| 420 | |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 421 | result = dbi_conn_queryf(conn, |
| 422 | "INSERT OR IGNORE INTO Equipment " |
Jan Luebbe | e30dbb3 | 2008-12-27 18:08:13 +0000 | [diff] [blame] | 423 | "(imei, created, updated) " |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 424 | "VALUES " |
Jan Luebbe | e30dbb3 | 2008-12-27 18:08:13 +0000 | [diff] [blame] | 425 | "(%s, datetime('now'), datetime('now')) ", |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 426 | imei |
| 427 | ); |
| 428 | if (result==NULL) { |
| 429 | printf("DB: Failed to create Equipment by IMEI.\n"); |
| 430 | return 1; |
| 431 | } |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 432 | equipment_id = 0; |
| 433 | if (dbi_result_get_numrows_affected(result)) { |
| 434 | equipment_id = dbi_conn_sequence_last(conn, NULL); |
| 435 | } |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 436 | dbi_result_free(result); |
| 437 | if (equipment_id) { |
| 438 | printf("DB: New Equipment: ID %llu, IMEI %s\n", equipment_id, imei); |
| 439 | } |
| 440 | else { |
| 441 | result = dbi_conn_queryf(conn, |
| 442 | "SELECT id FROM Equipment " |
| 443 | "WHERE imei = %s ", |
| 444 | imei |
| 445 | ); |
| 446 | if (result==NULL) { |
| 447 | printf("DB: Failed to query Equipment by IMEI.\n"); |
| 448 | return 1; |
| 449 | } |
| 450 | if (!dbi_result_next_row(result)) { |
| 451 | printf("DB: Failed to find the Equipment.\n"); |
| 452 | dbi_result_free(result); |
| 453 | return 1; |
| 454 | } |
| 455 | equipment_id = dbi_result_get_ulonglong(result, "id"); |
| 456 | dbi_result_free(result); |
| 457 | } |
| 458 | |
| 459 | result = dbi_conn_queryf(conn, |
| 460 | "INSERT OR IGNORE INTO EquipmentWatch " |
| 461 | "(subscriber_id, equipment_id, created, updated) " |
| 462 | "VALUES " |
| 463 | "(%llu, %llu, datetime('now'), datetime('now')) ", |
| 464 | subscriber->id, equipment_id |
| 465 | ); |
| 466 | if (result==NULL) { |
| 467 | printf("DB: Failed to create EquipmentWatch.\n"); |
| 468 | return 1; |
| 469 | } |
Jan Luebbe | 391d86e | 2008-12-27 22:33:34 +0000 | [diff] [blame] | 470 | watch_id = 0; |
| 471 | if (dbi_result_get_numrows_affected(result)) { |
| 472 | watch_id = dbi_conn_sequence_last(conn, NULL); |
| 473 | } |
Jan Luebbe | fac25fc | 2008-12-27 18:04:34 +0000 | [diff] [blame] | 474 | dbi_result_free(result); |
| 475 | if (watch_id) { |
| 476 | printf("DB: New EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei); |
| 477 | } |
| 478 | else { |
| 479 | result = dbi_conn_queryf(conn, |
| 480 | "UPDATE EquipmentWatch " |
| 481 | "SET updated = datetime('now') " |
| 482 | "WHERE subscriber_id = %llu AND equipment_id = %llu ", |
| 483 | subscriber->id, equipment_id |
| 484 | ); |
| 485 | if (result==NULL) { |
| 486 | printf("DB: Failed to update EquipmentWatch.\n"); |
| 487 | return 1; |
| 488 | } |
| 489 | dbi_result_free(result); |
| 490 | printf("DB: Updated EquipmentWatch: ID %llu, IMSI %s, IMEI %s\n", equipment_id, subscriber->imsi, imei); |
| 491 | } |
| 492 | |
| 493 | return 0; |
| 494 | } |
| 495 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 496 | /* store an [unsent] SMS to the database */ |
| 497 | int db_sms_store(struct gsm_sms *sms) |
| 498 | { |
| 499 | dbi_result result; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 500 | char *q_text, *q_daddr; |
| 501 | unsigned char *q_udata; |
| 502 | char *validity_timestamp = "2222-2-2"; |
| 503 | |
| 504 | /* FIXME: generate validity timestamp based on validity_minutes */ |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 505 | |
| 506 | dbi_conn_quote_string_copy(conn, (char *)sms->text, &q_text); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 507 | dbi_conn_quote_string_copy(conn, (char *)sms->dest_addr, &q_daddr); |
| 508 | dbi_conn_quote_binary_copy(conn, sms->user_data, sms->user_data_len, |
| 509 | &q_udata); |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 510 | /* FIXME: correct validity period */ |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 511 | result = dbi_conn_queryf(conn, |
| 512 | "INSERT INTO SMS " |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 513 | "(created, sender_id, receiver_id, valid_until, " |
| 514 | "reply_path_req, status_rep_req, protocol_id, " |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 515 | "data_coding_scheme, ud_hdr_ind, dest_addr, " |
| 516 | "user_data, text) VALUES " |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 517 | "(datetime('now'), %llu, %llu, %u, " |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 518 | "%u, %u, %u, %u, %u, %s, %s, %s)", |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 519 | sms->sender->id, |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 520 | sms->receiver ? sms->receiver->id : 0, validity_timestamp, |
| 521 | sms->reply_path_req, sms->status_rep_req, sms->protocol_id, |
Harald Welte | d0b7b77 | 2009-08-09 19:03:42 +0200 | [diff] [blame] | 522 | sms->data_coding_scheme, sms->ud_hdr_ind, |
| 523 | q_daddr, q_udata, q_text); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 524 | free(q_text); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 525 | free(q_daddr); |
| 526 | free(q_udata); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 527 | |
| 528 | if (!result) |
| 529 | return -EIO; |
| 530 | |
| 531 | dbi_result_free(result); |
| 532 | return 0; |
| 533 | } |
| 534 | |
Harald Welte | 2ebabca | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 535 | static struct gsm_sms *sms_from_result(struct gsm_network *net, dbi_result result) |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 536 | { |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 537 | struct gsm_sms *sms = sms_alloc(); |
Harald Welte | 2ebabca | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 538 | long long unsigned int sender_id, receiver_id; |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 539 | const char *text, *daddr; |
| 540 | const unsigned char *user_data; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 541 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 542 | if (!sms) |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 543 | return NULL; |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 544 | |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 545 | sms->id = dbi_result_get_ulonglong(result, "id"); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 546 | |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 547 | sender_id = dbi_result_get_ulonglong(result, "sender_id"); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 548 | sms->sender = subscr_get_by_id(net, sender_id); |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 549 | |
| 550 | receiver_id = dbi_result_get_ulonglong(result, "receiver_id"); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 551 | sms->receiver = subscr_get_by_id(net, receiver_id); |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 552 | |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 553 | /* FIXME: validity */ |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 554 | /* FIXME: those should all be get_uchar, but sqlite3 is braindead */ |
| 555 | sms->reply_path_req = dbi_result_get_uint(result, "reply_path_req"); |
| 556 | sms->status_rep_req = dbi_result_get_uint(result, "status_rep_req"); |
| 557 | sms->ud_hdr_ind = dbi_result_get_uint(result, "ud_hdr_ind"); |
| 558 | sms->protocol_id = dbi_result_get_uint(result, "protocol_id"); |
| 559 | sms->data_coding_scheme = dbi_result_get_uint(result, |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 560 | "data_coding_scheme"); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 561 | /* sms->msg_ref is temporary and not stored in DB */ |
Harald Welte | f3efc59 | 2009-07-27 20:11:35 +0200 | [diff] [blame] | 562 | |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 563 | daddr = dbi_result_get_string(result, "dest_addr"); |
| 564 | if (daddr) { |
| 565 | strncpy(sms->dest_addr, daddr, sizeof(sms->dest_addr)); |
| 566 | sms->dest_addr[sizeof(sms->dest_addr)-1] = '\0'; |
| 567 | } |
| 568 | |
| 569 | sms->user_data_len = dbi_result_get_field_length(result, "user_data"); |
| 570 | user_data = dbi_result_get_binary(result, "user_data"); |
| 571 | if (sms->user_data_len > sizeof(sms->user_data)) |
| 572 | sms->user_data_len = sizeof(sms->user_data); |
| 573 | memcpy(sms->user_data, user_data, sms->user_data_len); |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 574 | |
| 575 | text = dbi_result_get_string(result, "text"); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 576 | if (text) { |
Harald Welte | be3e378 | 2009-07-05 14:06:41 +0200 | [diff] [blame] | 577 | strncpy(sms->text, text, sizeof(sms->text)); |
Harald Welte | 7604218 | 2009-08-08 16:03:15 +0200 | [diff] [blame] | 578 | sms->text[sizeof(sms->text)-1] = '\0'; |
| 579 | } |
Harald Welte | 2ebabca | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 580 | return sms; |
| 581 | } |
| 582 | |
| 583 | /* retrieve the next unsent SMS with ID >= min_id */ |
| 584 | struct gsm_sms *db_sms_get_unsent(struct gsm_network *net, int min_id) |
| 585 | { |
| 586 | dbi_result result; |
| 587 | struct gsm_sms *sms; |
| 588 | |
| 589 | result = dbi_conn_queryf(conn, |
| 590 | "SELECT * FROM SMS " |
| 591 | "WHERE id >= %llu AND sent is NULL ORDER BY id", |
| 592 | min_id); |
| 593 | if (!result) |
| 594 | return NULL; |
| 595 | |
| 596 | if (!dbi_result_next_row(result)) { |
| 597 | dbi_result_free(result); |
| 598 | return NULL; |
| 599 | } |
| 600 | |
| 601 | sms = sms_from_result(net, result); |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 602 | |
| 603 | dbi_result_free(result); |
Harald Welte | 2ebabca | 2009-08-09 19:05:21 +0200 | [diff] [blame] | 604 | |
| 605 | return sms; |
| 606 | } |
| 607 | |
| 608 | /* retrieve the next unsent SMS with ID >= min_id */ |
| 609 | struct gsm_sms *db_sms_get_unsent_for_subscr(struct gsm_subscriber *subscr) |
| 610 | { |
| 611 | dbi_result result; |
| 612 | struct gsm_sms *sms; |
| 613 | |
| 614 | result = dbi_conn_queryf(conn, |
| 615 | "SELECT * FROM SMS " |
| 616 | "WHERE receiver_id = %llu AND sent is NULL ORDER BY id", |
| 617 | subscr->id); |
| 618 | if (!result) |
| 619 | return NULL; |
| 620 | |
| 621 | if (!dbi_result_next_row(result)) { |
| 622 | dbi_result_free(result); |
| 623 | return NULL; |
| 624 | } |
| 625 | |
| 626 | sms = sms_from_result(subscr->net, result); |
| 627 | |
| 628 | dbi_result_free(result); |
| 629 | |
Harald Welte | 7e310b1 | 2009-03-30 20:56:32 +0000 | [diff] [blame] | 630 | return sms; |
| 631 | } |
| 632 | |
| 633 | /* mark a given SMS as read */ |
| 634 | int db_sms_mark_sent(struct gsm_sms *sms) |
| 635 | { |
| 636 | dbi_result result; |
| 637 | |
| 638 | result = dbi_conn_queryf(conn, |
| 639 | "UPDATE SMS " |
| 640 | "SET sent = datetime('now') " |
| 641 | "WHERE id = %llu", sms->id); |
| 642 | if (!result) { |
| 643 | printf("DB: Failed to mark SMS %llu as sent.\n", sms->id); |
| 644 | return 1; |
| 645 | } |
| 646 | |
| 647 | dbi_result_free(result); |
| 648 | return 0; |
| 649 | } |