2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include "ua-manager-common.h"
25 #include "ua-manager-database.h"
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"
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 = ?"
36 #define SELECT_DEVICE_NUMBER "SELECT device_number FROM devices " \
37 "WHERE device_id = ? AND tech_type = ? AND address = ?"
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 (?, ?, ?, ?, ?, ?, ?, ?, ?)"
44 #define UPDATE_LAST_SEEN "UPDATE devices " \
45 "SET last_seen = ? WHERE device_id = ? AND tech_type = ? AND address = ?"
47 #define UPDATE_PRESENCE "UPDATE devices " \
48 "SET presence_state = ? WHERE device_id = ? AND tech_type = ? AND address = ?"
50 #define UPDATE_IP_ADDRESS "UPDATE devices " \
51 "SET ip_address = ? WHERE device_id = ? AND tech_type = ?"
53 #define UPDATE_MAC_ADDRESS "UPDATE devices " \
54 "SET address = ? WHERE device_id = ? AND tech_type = ?"
56 #define UPDATE_DEVICE "UPDATE devices " \
57 "SET (os_type, discriminant, ip_address) = (?, ?, ?) " \
58 "WHERE device_id = ? AND tech_type = ? AND address = ?"
60 #define DELETE_ALL_DEVICES "delete from devices "
62 #define DELETE_DEVICE_INFO "delete from devices " \
63 "WHERE device_id = ? AND tech_type = ? AND address = ?"
65 static void __uam_device_finalize_delete(void);
67 /* DELETE statements */
68 static sqlite3_stmt *delete_all_devices;
69 static sqlite3_stmt *delete_device_info;
71 /* SELECT statements */
72 static sqlite3_stmt *select_all_devices;
73 static sqlite3_stmt *select_device;
74 static sqlite3_stmt *select_device_number;
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;
83 /* INSERT statements */
84 static sqlite3_stmt *insert_device_info;
86 extern sqlite3 *database_handle;
88 static void __uam_device_finalize_delete(void)
92 FINALIZE(delete_all_devices);
93 FINALIZE(delete_device_info);
98 static void __uam_device_finalize_select(void)
102 FINALIZE(select_all_devices);
103 FINALIZE(select_device);
104 FINALIZE(select_device_number);
109 static void __uam_device_finalize_insert(void)
113 FINALIZE(insert_device_info);
118 static void __uam_device_finalize_update(void)
122 FINALIZE(update_last_seen);
123 FINALIZE(update_presence);
124 FINALIZE(update_ip_address);
125 FINALIZE(update_mac_address);
126 FINALIZE(update_device);
131 static int __uam_device_prepare_delete(sqlite3 *db)
135 static int initialized;
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);
152 static int __uam_device_prepare_select(sqlite3 *db)
156 static int initialized;
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);
175 static int __uam_device_prepare_update(sqlite3 *db)
179 static int initialized;
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);
202 static int __uam_device_prepare_insert(sqlite3 *db)
206 static int initialized;
213 PREPARE_QUERY(rc, db, insert_device_info,
214 INSERT_DEVICE, __uam_device_finalize_insert);
221 static int __uam_device_table_devicesinfo_prepare(sqlite3 *db)
225 int error_code = UAM_ERROR_NONE;
229 return UAM_ERROR_DB_FAILED;
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);
242 static void __uam_device_table_devicesinfo_finalize(void)
245 __uam_device_finalize_delete();
246 __uam_device_finalize_select();
247 __uam_device_finalize_update();
248 __uam_device_finalize_insert();
252 int _uam_device_db_deinitialize(void)
256 retv_if(NULL == database_handle, UAM_ERROR_NONE);
258 __uam_device_table_devicesinfo_finalize();
259 sqlite3_close(database_handle);
262 return UAM_ERROR_NONE;
265 int _uam_device_db_initialize(void)
269 EXEC(UAM_ERROR_NONE, __uam_device_table_devicesinfo_prepare(database_handle), handle_error);
272 return UAM_ERROR_NONE;
275 _uam_device_db_deinitialize();
277 return UAM_ERROR_DB_FAILED;
280 int _uam_device_db_update_device_ip_address(char *device_id, int tech_type,
283 int error_code = UAM_ERROR_NONE;
284 sqlite3_stmt *stmt = update_ip_address;
285 int sql_ret = SQLITE_OK;
287 retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
288 retv_if(NULL == ip_address, UAM_ERROR_INVALID_PARAMETER);
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);
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));
302 error_code = UAM_ERROR_DB_FAILED;
306 UAM_DBG("[%d] IP address updated [%s]", tech_type, ip_address);
313 int _uam_device_db_update_device_mac_address(char *device_id, int tech_type,
316 int error_code = UAM_ERROR_NONE;
317 sqlite3_stmt *stmt = update_mac_address;
318 int sql_ret = SQLITE_OK;
320 retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
321 retv_if(NULL == address, UAM_ERROR_INVALID_PARAMETER);
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);
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));
335 error_code = UAM_ERROR_DB_FAILED;
339 UAM_DBG("[%d] MAC address updated [%s]", tech_type, address);
346 int _uam_device_db_update_device_presence(char *device_id, int tech_type,
347 char *address, int presence_state)
349 int error_code = UAM_ERROR_NONE;
350 sqlite3_stmt *stmt = update_presence;
351 int sql_ret = SQLITE_OK;
353 retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
354 retv_if(NULL == address, UAM_ERROR_INVALID_PARAMETER);
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);
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));
370 error_code = UAM_ERROR_DB_FAILED;
374 UAM_DBG("presence state updated [%d]", presence_state);
381 int _uam_device_db_update_device_last_seen(char *device_id, int tech_type,
382 char *address, unsigned long long last_seen)
384 int error_code = UAM_ERROR_NONE;
385 sqlite3_stmt *stmt = update_last_seen;
386 int sql_ret = SQLITE_OK;
388 retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
389 retv_if(NULL == address, UAM_ERROR_INVALID_PARAMETER);
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);
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));
405 error_code = UAM_ERROR_DB_FAILED;
409 UAM_DBG("Device last_seen updated [%llu]", last_seen);
416 int _uam_device_db_update_device(char *device_id, int tech_type,
417 char *address, char *ip, char os_type, char discriminant)
419 int error_code = UAM_ERROR_NONE;
420 sqlite3_stmt *stmt = update_device;
421 int sql_ret = SQLITE_OK;
423 retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
424 retv_if(NULL == address, UAM_ERROR_INVALID_PARAMETER);
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);
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));
444 error_code = UAM_ERROR_DB_FAILED;
448 UAM_DBG("Device discriminant updated [%s]", discriminant ? "true" : "false");
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)
459 int error_code = UAM_ERROR_NONE;
460 sqlite3_stmt *stmt = insert_device_info;
461 int sql_ret = SQLITE_OK;
463 retv_if(NULL == dev_info, UAM_ERROR_INVALID_PARAMETER);
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,
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);
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;
497 UAM_DBG("Device info inserted device_id [%s] ", dev_info->device_id);
505 int _uam_device_db_delete_device_info(const char *device_id, int tech_type,
509 int error_code = UAM_ERROR_NONE;
510 sqlite3_stmt *stmt = delete_device_info;
511 int sql_ret = SQLITE_OK;
513 UAM_INFO("DeviceId: %s", device_id);
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);
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);
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);
538 UAM_DBG("Device info deleted");
540 __uam_db_end_transaction(1);
548 int _uam_device_db_get_device(char *device_id, int tech_type, char *address,
549 db_device_info_t *info)
552 int error_code = UAM_ERROR_NONE;
553 sqlite3_stmt *stmt = select_device;
554 int sql_ret = SQLITE_OK;
556 retv_if(NULL == info, UAM_ERROR_INVALID_PARAMETER);
558 UAM_INFO("device id: %s", device_id);
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);
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;
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);
588 UAM_INFO("%s-%d-%s-%s-%llu-%d-%d-%d-%d",
589 info->dev_info.device_id,
592 info->dev_info.ipv4_addr,
594 info->presence_state,
595 info->dev_info.operating_system,
597 info->dev_info.discriminant);
606 int _uam_db_get_device_number(const char *device_id, int tech_type,
607 const char *address, int *device_number)
610 int error_code = UAM_ERROR_NONE;
611 sqlite3_stmt *stmt = select_device_number;
612 int sql_ret = SQLITE_OK;
614 retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
615 retv_if(NULL == address, UAM_ERROR_INVALID_PARAMETER);
617 UAM_INFO("UserId: %s", device_id);
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);
627 sql_ret = sqlite3_step(stmt);
633 UAM_DBG("Device number info found");
634 *device_number = sqlite3_column_int(stmt, 0);
635 UAM_INFO("device_number %d", *device_number);
639 UAM_ERR("Failed to enumerate device info [%d:%s]",
640 sql_ret, sqlite3_errmsg(database_handle));
642 } while (sql_ret == SQLITE_ROW);
650 GSList *_uam_device_db_get_all_devices(void)
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;
659 sql_ret = sqlite3_step(stmt);
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);
679 UAM_INFO("%s-%d-%s-%s-%llu-%d-%d-%d-%d",
680 info->dev_info.device_id,
683 info->dev_info.ipv4_addr,
685 info->presence_state,
686 info->dev_info.operating_system,
688 info->dev_info.discriminant);
690 device_list = g_slist_append(device_list, info);
694 UAM_ERR("Failed to enumerate device info [%d:%s]",
695 sql_ret, sqlite3_errmsg(database_handle));
697 } while (sql_ret == SQLITE_ROW);
704 int _uam_device_db_clear(void)
706 int error_code = UAM_ERROR_NONE;
707 sqlite3_stmt *stmt = delete_all_devices;
708 int sql_ret = SQLITE_OK;
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;
717 UAM_DBG("Device data deleted ");