Add database APIs to update device service payload info
[platform/core/connectivity/ua-manager.git] / ua-daemon / src / ua-manager-device-db.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License")
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <sqlite3.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <dlfcn.h>
23
24 #include "ua-manager-common.h"
25 #include "ua-manager-database.h"
26
27 #define SELECT_ALL_DEVICES "SELECT device_id, tech_type, address, " \
28         "ip_address, last_seen, presence_state, os_type, " \
29         "user_id, discriminant FROM devices"
30
31 #define SELECT_DEVICE "SELECT device_id, tech_type, address, " \
32         "ip_address, last_seen, presence_state, os_type, " \
33         "user_id, discriminant FROM devices " \
34         "WHERE device_id = ? AND tech_type = ? AND address = ?"
35
36 #define SELECT_DEVICE_NUMBER "SELECT device_number FROM devices " \
37         "WHERE device_id = ? AND tech_type = ? AND address = ?"
38
39 #define INSERT_DEVICE "insert into devices " \
40         "(device_id, tech_type, address, ip_address, last_seen, " \
41         "presence_state, os_type, user_id, discriminant)" \
42         "values (?, ?, ?, ?, ?, ?, ?, ?, ?)"
43
44 #define UPDATE_LAST_SEEN "UPDATE devices " \
45         "SET last_seen = ? WHERE device_id = ? AND tech_type = ? AND address = ?"
46
47 #define UPDATE_PRESENCE "UPDATE devices " \
48         "SET presence_state = ? WHERE device_id = ? AND tech_type = ? AND address = ?"
49
50 #define UPDATE_IP_ADDRESS "UPDATE devices " \
51         "SET ip_address = ? WHERE device_id = ? AND tech_type = ?"
52
53 #define UPDATE_MAC_ADDRESS "UPDATE devices " \
54         "SET address = ? WHERE device_id = ? AND tech_type = ?"
55
56 #define UPDATE_DEVICE "UPDATE devices " \
57         "SET (os_type, discriminant, ip_address) = (?, ?, ?) " \
58         "WHERE device_id = ? AND tech_type = ? AND address = ?"
59
60 #define DELETE_ALL_DEVICES "delete from devices "
61
62 #define DELETE_DEVICE_INFO "delete from devices " \
63         "WHERE device_id = ? AND tech_type = ? AND address = ?"
64
65 static void __uam_device_finalize_delete(void);
66
67 /* DELETE statements */
68 static sqlite3_stmt *delete_all_devices;
69 static sqlite3_stmt *delete_device_info;
70
71 /* SELECT statements */
72 static sqlite3_stmt *select_all_devices;
73 static sqlite3_stmt *select_device;
74 static sqlite3_stmt *select_device_number;
75
76 /* UPDATE statements */
77 static sqlite3_stmt *update_last_seen;
78 static sqlite3_stmt *update_presence;
79 static sqlite3_stmt *update_ip_address;
80 static sqlite3_stmt *update_mac_address;
81 static sqlite3_stmt *update_device;
82
83 /* INSERT statements */
84 static sqlite3_stmt *insert_device_info;
85
86 extern sqlite3 *database_handle;
87
88 static void __uam_device_finalize_delete(void)
89 {
90         FUNC_ENTRY;
91
92         FINALIZE(delete_all_devices);
93         FINALIZE(delete_device_info);
94
95         FUNC_EXIT;
96 }
97
98 static void __uam_device_finalize_select(void)
99 {
100         FUNC_ENTRY;
101
102         FINALIZE(select_all_devices);
103         FINALIZE(select_device);
104         FINALIZE(select_device_number);
105
106         FUNC_EXIT;
107 }
108
109 static void __uam_device_finalize_insert(void)
110 {
111         FUNC_ENTRY;
112
113         FINALIZE(insert_device_info);
114
115         FUNC_EXIT;
116 }
117
118 static void __uam_device_finalize_update(void)
119 {
120         FUNC_ENTRY;
121
122         FINALIZE(update_last_seen);
123         FINALIZE(update_presence);
124         FINALIZE(update_ip_address);
125         FINALIZE(update_mac_address);
126         FINALIZE(update_device);
127
128         FUNC_EXIT;
129 }
130
131 static int __uam_device_prepare_delete(sqlite3 *db)
132 {
133         FUNC_ENTRY;
134         int rc;
135         static int initialized;
136
137         if (initialized) {
138                 FUNC_EXIT;
139                 return SQLITE_OK;
140         }
141
142         PREPARE_QUERY(rc, db, delete_all_devices,
143                 DELETE_ALL_DEVICES,     __uam_device_finalize_delete);
144         PREPARE_QUERY(rc, db, delete_device_info,
145                 DELETE_DEVICE_INFO, __uam_device_finalize_delete);
146
147         initialized = 1;
148         FUNC_EXIT;
149         return rc;
150 }
151
152 static int __uam_device_prepare_select(sqlite3 *db)
153 {
154         FUNC_ENTRY;
155         int rc;
156         static int initialized;
157
158         if (initialized) {
159                 FUNC_EXIT;
160                 return SQLITE_OK;
161         }
162
163         PREPARE_QUERY(rc, db, select_all_devices,
164                 SELECT_ALL_DEVICES, __uam_device_finalize_select);
165         PREPARE_QUERY(rc, db, select_device,
166                 SELECT_DEVICE, __uam_device_finalize_select);
167         PREPARE_QUERY(rc, db, select_device_number,
168                 SELECT_DEVICE_NUMBER, __uam_device_finalize_select);
169
170         initialized = 1;
171         FUNC_EXIT;
172         return rc;
173 }
174
175 static int __uam_device_prepare_update(sqlite3 *db)
176 {
177         FUNC_ENTRY;
178         int rc;
179         static int initialized;
180
181         if (initialized) {
182                 FUNC_EXIT;
183                 return SQLITE_OK;
184         }
185
186         PREPARE_QUERY(rc, db, update_last_seen,
187                 UPDATE_LAST_SEEN, __uam_device_finalize_update);
188         PREPARE_QUERY(rc, db, update_presence,
189                 UPDATE_PRESENCE, __uam_device_finalize_update);
190         PREPARE_QUERY(rc, db, update_ip_address,
191                 UPDATE_IP_ADDRESS, __uam_device_finalize_update);
192                 PREPARE_QUERY(rc, db, update_mac_address,
193                 UPDATE_MAC_ADDRESS, __uam_device_finalize_update);
194         PREPARE_QUERY(rc, db, update_device,
195                 UPDATE_DEVICE, __uam_device_finalize_update);
196
197         initialized = 1;
198         FUNC_EXIT;
199         return rc;
200 }
201
202 static int __uam_device_prepare_insert(sqlite3 *db)
203 {
204         FUNC_ENTRY;
205         int rc;
206         static int initialized;
207
208         if (initialized) {
209                 FUNC_EXIT;
210                 return SQLITE_OK;
211         }
212
213         PREPARE_QUERY(rc, db, insert_device_info,
214                 INSERT_DEVICE, __uam_device_finalize_insert);
215
216         initialized = 1;
217         FUNC_EXIT;
218         return rc;
219 }
220
221 static int __uam_device_table_devicesinfo_prepare(sqlite3 *db)
222 {
223         FUNC_ENTRY;
224
225         int error_code = UAM_ERROR_NONE;
226
227         if (db == NULL) {
228                 FUNC_EXIT;
229                 return UAM_ERROR_DB_FAILED;
230         }
231
232         DB_ACTION(__uam_device_prepare_delete(db), error_code, handle_error);
233         DB_ACTION(__uam_device_prepare_select(db), error_code, handle_error);
234         DB_ACTION(__uam_device_prepare_update(db), error_code, handle_error);
235         DB_ACTION(__uam_device_prepare_insert(db), error_code, handle_error);
236
237 handle_error:
238         FUNC_EXIT;
239         return error_code;
240 }
241
242 static void __uam_device_table_devicesinfo_finalize(void)
243 {
244         FUNC_ENTRY;
245         __uam_device_finalize_delete();
246         __uam_device_finalize_select();
247         __uam_device_finalize_update();
248         __uam_device_finalize_insert();
249         FUNC_EXIT;
250 }
251
252 int _uam_device_db_deinitialize(void)
253 {
254         FUNC_ENTRY;
255
256         retv_if(NULL == database_handle, UAM_ERROR_NONE);
257
258         __uam_device_table_devicesinfo_finalize();
259         sqlite3_close(database_handle);
260
261         FUNC_EXIT;
262         return UAM_ERROR_NONE;
263 }
264
265 int _uam_device_db_initialize(void)
266 {
267         FUNC_ENTRY;
268
269         EXEC(UAM_ERROR_NONE, __uam_device_table_devicesinfo_prepare(database_handle), handle_error);
270
271         FUNC_EXIT;
272         return UAM_ERROR_NONE;
273
274 handle_error:
275         _uam_device_db_deinitialize();
276         FUNC_EXIT;
277         return UAM_ERROR_DB_FAILED;
278 }
279
280 int _uam_device_db_update_device_ip_address(char *device_id, int tech_type,
281         char *ip_address)
282 {
283         int error_code = UAM_ERROR_NONE;
284         sqlite3_stmt *stmt = update_ip_address;
285         int sql_ret = SQLITE_OK;
286
287         retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
288         retv_if(NULL == ip_address, UAM_ERROR_INVALID_PARAMETER);
289
290         DB_ACTION(sqlite3_bind_text(stmt, 1, ip_address, -1, SQLITE_TRANSIENT),
291                 error_code, handle_error);
292         DB_ACTION(sqlite3_bind_text(stmt, 2, device_id, -1, SQLITE_TRANSIENT),
293                 error_code, handle_error);
294         DB_ACTION(sqlite3_bind_int(stmt, 3, tech_type),
295                 error_code, handle_error);
296
297         sql_ret = sqlite3_step(stmt);
298         if (sql_ret != SQLITE_DONE) {
299                 UAM_ERR("Failed to update IP address [%d:%s]",
300                         sql_ret, sqlite3_errmsg(database_handle));
301
302                 error_code = UAM_ERROR_DB_FAILED;
303                 goto handle_error;
304         }
305
306         UAM_DBG("[%d] IP address updated [%s]", tech_type, ip_address);
307
308 handle_error:
309         sqlite3_reset(stmt);
310         return error_code;
311 }
312
313 int _uam_device_db_update_device_mac_address(char *device_id, int tech_type,
314         char *address)
315 {
316         int error_code = UAM_ERROR_NONE;
317         sqlite3_stmt *stmt = update_mac_address;
318         int sql_ret = SQLITE_OK;
319
320         retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
321         retv_if(NULL == address, UAM_ERROR_INVALID_PARAMETER);
322
323         DB_ACTION(sqlite3_bind_text(stmt, 1, address, -1, SQLITE_TRANSIENT),
324                 error_code, handle_error);
325         DB_ACTION(sqlite3_bind_text(stmt, 2, device_id, -1, SQLITE_TRANSIENT),
326                 error_code, handle_error);
327         DB_ACTION(sqlite3_bind_int(stmt, 3, tech_type),
328                 error_code, handle_error);
329
330         sql_ret = sqlite3_step(stmt);
331         if (sql_ret != SQLITE_DONE) {
332                 UAM_ERR("Failed to update MAC address [%d:%s]",
333                         sql_ret, sqlite3_errmsg(database_handle));
334
335                 error_code = UAM_ERROR_DB_FAILED;
336                 goto handle_error;
337         }
338
339         UAM_DBG("[%d] MAC address updated [%s]", tech_type, address);
340
341 handle_error:
342         sqlite3_reset(stmt);
343         return error_code;
344 }
345
346 int _uam_device_db_update_device_presence(char *device_id, int tech_type,
347         char *address, int presence_state)
348 {
349         int error_code = UAM_ERROR_NONE;
350         sqlite3_stmt *stmt = update_presence;
351         int sql_ret = SQLITE_OK;
352
353         retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
354         retv_if(NULL == address, UAM_ERROR_INVALID_PARAMETER);
355
356         DB_ACTION(sqlite3_bind_int(stmt, 1, presence_state),
357                 error_code, handle_error);
358         DB_ACTION(sqlite3_bind_text(stmt, 2, device_id, -1, SQLITE_TRANSIENT),
359                 error_code, handle_error);
360         DB_ACTION(sqlite3_bind_int(stmt, 3, tech_type),
361                 error_code, handle_error);
362         DB_ACTION(sqlite3_bind_text(stmt, 4, address, -1, SQLITE_TRANSIENT),
363                 error_code, handle_error);
364
365         sql_ret = sqlite3_step(stmt);
366         if (sql_ret != SQLITE_DONE) {
367                 UAM_ERR("Failed to update presence [%d:%s]",
368                         sql_ret, sqlite3_errmsg(database_handle));
369
370                 error_code = UAM_ERROR_DB_FAILED;
371                 goto handle_error;
372         }
373
374         UAM_DBG("presence state updated [%d]", presence_state);
375
376 handle_error:
377         sqlite3_reset(stmt);
378         return error_code;
379 }
380
381 int _uam_device_db_update_device_last_seen(char *device_id, int tech_type,
382                                           char *address, unsigned long long last_seen)
383 {
384         int error_code = UAM_ERROR_NONE;
385         sqlite3_stmt *stmt = update_last_seen;
386         int sql_ret = SQLITE_OK;
387
388         retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
389         retv_if(NULL == address, UAM_ERROR_INVALID_PARAMETER);
390
391         DB_ACTION(sqlite3_bind_int64(stmt, 1, last_seen),
392                 error_code, handle_error);
393         DB_ACTION(sqlite3_bind_text(stmt, 2, device_id, -1, SQLITE_TRANSIENT),
394                 error_code, handle_error);
395         DB_ACTION(sqlite3_bind_int(stmt, 3, tech_type),
396                 error_code, handle_error);
397         DB_ACTION(sqlite3_bind_text(stmt, 4, address, -1, SQLITE_TRANSIENT),
398                 error_code, handle_error);
399
400         sql_ret = sqlite3_step(stmt);
401         if (sql_ret != SQLITE_DONE) {
402                 UAM_ERR("Failed to update device last_seen [%d:%s]",
403                         sql_ret, sqlite3_errmsg(database_handle));
404
405                 error_code = UAM_ERROR_DB_FAILED;
406                 goto handle_error;
407         }
408
409         UAM_DBG("Device last_seen updated [%llu]", last_seen);
410
411 handle_error:
412         sqlite3_reset(stmt);
413         return error_code;
414 }
415
416 int _uam_device_db_update_device(char *device_id, int tech_type,
417         char *address, char *ip, char os_type, char discriminant)
418 {
419         int error_code = UAM_ERROR_NONE;
420         sqlite3_stmt *stmt = update_device;
421         int sql_ret = SQLITE_OK;
422
423         retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
424         retv_if(NULL == address, UAM_ERROR_INVALID_PARAMETER);
425
426         DB_ACTION(sqlite3_bind_int(stmt, 1, os_type),
427                 error_code, handle_error);
428         DB_ACTION(sqlite3_bind_int(stmt, 2, discriminant),
429                 error_code, handle_error);
430         DB_ACTION(sqlite3_bind_text(stmt, 3, ip, -1, SQLITE_TRANSIENT),
431                 error_code, handle_error);
432         DB_ACTION(sqlite3_bind_text(stmt, 4, device_id, -1, SQLITE_TRANSIENT),
433                 error_code, handle_error);
434         DB_ACTION(sqlite3_bind_int(stmt, 5, tech_type),
435                 error_code, handle_error);
436         DB_ACTION(sqlite3_bind_text(stmt, 6, address, -1, SQLITE_TRANSIENT),
437                 error_code, handle_error);
438
439         sql_ret = sqlite3_step(stmt);
440         if (sql_ret != SQLITE_DONE) {
441                 UAM_ERR("Failed to update device discriminant [%d:%s]",
442                         sql_ret, sqlite3_errmsg(database_handle));
443
444                 error_code = UAM_ERROR_DB_FAILED;
445                 goto handle_error;
446         }
447
448         UAM_DBG("Device discriminant updated [%s]", discriminant ? "true" : "false");
449
450 handle_error:
451         sqlite3_reset(stmt);
452         return error_code;
453 }
454
455 int _uam_device_db_insert_device_info(int user_id,
456         const uam_device_info_s *dev_info, int presence_state, unsigned long long last_seen)
457 {
458         FUNC_ENTRY;
459         int error_code = UAM_ERROR_NONE;
460         sqlite3_stmt *stmt = insert_device_info;
461         int sql_ret = SQLITE_OK;
462
463         retv_if(NULL == dev_info, UAM_ERROR_INVALID_PARAMETER);
464
465         UAM_INFO("%s-%d-%s-%s-%llu-%d-%d-%d-%d", dev_info->device_id,
466                         dev_info->type, dev_info->mac, dev_info->ipv4_addr,
467                         last_seen, presence_state, dev_info->operating_system, dev_info->discriminant,
468                         user_id);
469
470         DB_ACTION(sqlite3_bind_text(stmt, 1, dev_info->device_id, -1, SQLITE_TRANSIENT),
471                 error_code, handle_error);
472         DB_ACTION(sqlite3_bind_int(stmt, 2, dev_info->type),
473                 error_code, handle_error);
474         DB_ACTION(sqlite3_bind_text(stmt, 3, dev_info->mac, -1, SQLITE_TRANSIENT),
475                 error_code, handle_error);
476         DB_ACTION(sqlite3_bind_text(stmt, 4, dev_info->ipv4_addr, -1, SQLITE_TRANSIENT),
477                 error_code, handle_error);
478         DB_ACTION(sqlite3_bind_int64(stmt, 5, last_seen),
479                 error_code, handle_error);
480         DB_ACTION(sqlite3_bind_int(stmt, 6, presence_state),
481                 error_code, handle_error);
482         DB_ACTION(sqlite3_bind_int(stmt, 7, dev_info->operating_system),
483                 error_code, handle_error);
484         DB_ACTION(sqlite3_bind_int(stmt, 8, user_id),
485                 error_code, handle_error);
486         DB_ACTION(sqlite3_bind_int(stmt, 9, dev_info->discriminant),
487                 error_code, handle_error);
488
489         sql_ret = sqlite3_step(stmt);
490         if (sql_ret != SQLITE_DONE) {
491                 UAM_ERR("Failed to insert device info [%d:%s]",
492                         sql_ret, sqlite3_errmsg(database_handle));
493                 error_code = UAM_ERROR_DB_FAILED;
494                 goto handle_error;
495         }
496
497         UAM_DBG("Device info inserted device_id [%s] ", dev_info->device_id);
498
499 handle_error:
500         sqlite3_reset(stmt);
501         FUNC_EXIT;
502         return error_code;
503 }
504
505 int _uam_device_db_delete_device_info(const char *device_id, int tech_type,
506         const char *address)
507 {
508         FUNC_ENTRY;
509         int error_code = UAM_ERROR_NONE;
510         sqlite3_stmt *stmt = delete_device_info;
511         int sql_ret = SQLITE_OK;
512
513         UAM_INFO("DeviceId: %s", device_id);
514
515         DB_ACTION(sqlite3_bind_text(stmt, 1, device_id, -1, SQLITE_STATIC),
516                 error_code, handle_error);
517         DB_ACTION(sqlite3_bind_int(stmt, 2, tech_type),
518                 error_code, handle_error);
519         DB_ACTION(sqlite3_bind_text(stmt, 3, address, -1, SQLITE_STATIC),
520                 error_code, handle_error);
521
522         retv_if(UAM_ERROR_NONE != __uam_db_begin_transaction(), UAM_ERROR_DB_FAILED);
523         error_code = _uam_db_delete_device_service_mapping(device_id, tech_type, address);
524         if (UAM_ERROR_NONE != error_code) {
525                 UAM_ERR("Failed to delete device service mapping");
526                 __uam_db_end_transaction(0);
527                 goto handle_error;
528         }
529
530         sql_ret = sqlite3_step(stmt);
531         if (sql_ret != SQLITE_DONE) {
532                 UAM_ERR("Failed to delete device info [%d:%s]",
533                         sql_ret, sqlite3_errmsg(database_handle));
534                 error_code = UAM_ERROR_DB_FAILED;
535                 __uam_db_end_transaction(0);
536                 goto handle_error;
537         } else
538                 UAM_DBG("Device info deleted");
539
540         __uam_db_end_transaction(1);
541
542 handle_error:
543         sqlite3_reset(stmt);
544         FUNC_EXIT;
545         return error_code;
546 }
547
548 int _uam_device_db_get_device(char *device_id, int tech_type, char *address,
549                 db_device_info_t *info)
550 {
551         FUNC_ENTRY;
552         int error_code = UAM_ERROR_NONE;
553         sqlite3_stmt *stmt = select_device;
554         int sql_ret = SQLITE_OK;
555
556         retv_if(NULL == info, UAM_ERROR_INVALID_PARAMETER);
557
558         UAM_INFO("device id: %s", device_id);
559
560         DB_ACTION(sqlite3_bind_text(stmt, 1, device_id, -1, SQLITE_STATIC),
561                 error_code, handle_error);
562         DB_ACTION(sqlite3_bind_int(stmt, 2, tech_type),
563                 error_code, handle_error);
564         DB_ACTION(sqlite3_bind_text(stmt, 3, address, -1, SQLITE_STATIC),
565                 error_code, handle_error);
566
567         sql_ret = sqlite3_step(stmt);
568         if (sql_ret != SQLITE_DONE) {
569                 UAM_ERR("Failed to select device info [%d:%s]",
570                         sql_ret, sqlite3_errmsg(database_handle));
571                 error_code = UAM_ERROR_DB_FAILED;
572         } else {
573                 UAM_DBG("Device info found");
574                 info = g_new0(db_device_info_t, 1);
575                 g_strlcpy(info->dev_info.device_id, (char *)sqlite3_column_text(stmt, 0),
576                         UAM_DEVICE_ID_MAX_STRING_LEN);
577                 info->dev_info.type = sqlite3_column_int(stmt, 1);
578                 g_strlcpy(info->dev_info.mac, (char *)sqlite3_column_text(stmt, 2),
579                         UAM_MAC_ADDRESS_STRING_LEN);
580                 g_strlcpy(info->dev_info.ipv4_addr, (char *)sqlite3_column_text(stmt, 3),
581                         UAM_IP_ADDRESS_MAX_STRING_LEN);
582                 info->last_seen = sqlite3_column_int64(stmt, 4);
583                 info->presence_state = sqlite3_column_int(stmt, 5);
584                 info->dev_info.operating_system = sqlite3_column_int(stmt, 6);
585                 info->user_id = sqlite3_column_int(stmt, 7);
586                 info->dev_info.discriminant = sqlite3_column_int(stmt, 8);
587
588                 UAM_INFO("%s-%d-%s-%s-%llu-%d-%d-%d-%d",
589                         info->dev_info.device_id,
590                         info->dev_info.type,
591                         info->dev_info.mac,
592                         info->dev_info.ipv4_addr,
593                         info->last_seen,
594                         info->presence_state,
595                         info->dev_info.operating_system,
596                         info->user_id,
597                         info->dev_info.discriminant);
598         }
599
600 handle_error:
601         sqlite3_reset(stmt);
602         FUNC_EXIT;
603         return error_code;
604 }
605
606 int _uam_db_get_device_number(const char *device_id, int tech_type,
607         const char *address, int *device_number)
608 {
609         FUNC_ENTRY;
610         int error_code = UAM_ERROR_NONE;
611         sqlite3_stmt *stmt = select_device_number;
612         int sql_ret = SQLITE_OK;
613
614         retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
615         retv_if(NULL == address, UAM_ERROR_INVALID_PARAMETER);
616
617         UAM_INFO("UserId: %s", device_id);
618
619         DB_ACTION(sqlite3_bind_text(stmt, 1, device_id, -1, SQLITE_STATIC),
620                 error_code, handle_error);
621         DB_ACTION(sqlite3_bind_int(stmt, 2, tech_type),
622                 error_code, handle_error);
623         DB_ACTION(sqlite3_bind_text(stmt, 3, address, -1, SQLITE_STATIC),
624                 error_code, handle_error);
625
626         do {
627                 sql_ret = sqlite3_step(stmt);
628
629                 switch (sql_ret) {
630                 case SQLITE_DONE:
631                         break;
632                 case SQLITE_ROW:
633                         UAM_DBG("Device number info found");
634                         *device_number = sqlite3_column_int(stmt, 0);
635                         UAM_INFO("device_number %d", *device_number);
636                         break;
637                 case SQLITE_ERROR:
638                 default:
639                         UAM_ERR("Failed to enumerate device info [%d:%s]",
640                                 sql_ret, sqlite3_errmsg(database_handle));
641                 }
642         } while (sql_ret == SQLITE_ROW);
643
644 handle_error:
645         sqlite3_reset(stmt);
646         FUNC_EXIT;
647         return error_code;
648 }
649
650 GSList *_uam_device_db_get_all_devices(void)
651 {
652         FUNC_ENTRY;
653         sqlite3_stmt *stmt = select_all_devices;
654         GSList *device_list = NULL;
655         db_device_info_t *info = NULL;
656         int sql_ret = SQLITE_OK;
657
658         do {
659                 sql_ret = sqlite3_step(stmt);
660
661                 switch (sql_ret) {
662                 case SQLITE_DONE:
663                         break;
664                 case SQLITE_ROW:
665                         info = g_new0(db_device_info_t, 1);
666                         g_strlcpy(info->dev_info.device_id, (char *)sqlite3_column_text(stmt, 0),
667                                 UAM_DEVICE_ID_MAX_STRING_LEN);
668                         info->dev_info.type = sqlite3_column_int(stmt, 1);
669                         g_strlcpy(info->dev_info.mac, (char *)sqlite3_column_text(stmt, 2),
670                                 UAM_MAC_ADDRESS_STRING_LEN);
671                         g_strlcpy(info->dev_info.ipv4_addr, (char *)sqlite3_column_text(stmt, 3),
672                                 UAM_IP_ADDRESS_MAX_STRING_LEN);
673                         info->last_seen = sqlite3_column_int64(stmt, 4);
674                         info->presence_state = sqlite3_column_int(stmt, 5);
675                         info->dev_info.operating_system = sqlite3_column_int(stmt, 6);
676                         info->user_id = sqlite3_column_int(stmt, 7);
677                         info->dev_info.discriminant = sqlite3_column_int(stmt, 8);
678
679                         UAM_INFO("%s-%d-%s-%s-%llu-%d-%d-%d-%d",
680                                 info->dev_info.device_id,
681                                 info->dev_info.type,
682                                 info->dev_info.mac,
683                                 info->dev_info.ipv4_addr,
684                                 info->last_seen,
685                                 info->presence_state,
686                                 info->dev_info.operating_system,
687                                 info->user_id,
688                                 info->dev_info.discriminant);
689
690                         device_list = g_slist_append(device_list, info);
691                         break;
692                 case SQLITE_ERROR:
693                 default:
694                         UAM_ERR("Failed to enumerate device info [%d:%s]",
695                                 sql_ret, sqlite3_errmsg(database_handle));
696                 }
697         } while (sql_ret == SQLITE_ROW);
698
699         sqlite3_reset(stmt);
700         FUNC_EXIT;
701         return device_list;
702 }
703
704 int _uam_device_db_clear(void)
705 {
706         int error_code = UAM_ERROR_NONE;
707         sqlite3_stmt *stmt = delete_all_devices;
708         int sql_ret = SQLITE_OK;
709
710         sql_ret = sqlite3_step(stmt);
711         if (sql_ret != SQLITE_DONE) {
712                 UAM_ERR("Failed to delete device data [%d:%s]",
713                         sql_ret, sqlite3_errmsg(database_handle));
714                 error_code = UAM_ERROR_DB_FAILED;
715                 goto handle_error;
716         }
717         UAM_DBG("Device data deleted ");
718
719 handle_error:
720         sqlite3_reset(stmt);
721         return error_code;
722 }