Improve error handling by returning error code
authorAbhay agarwal <ay.agarwal@samsung.com>
Mon, 4 Nov 2019 15:28:37 +0000 (20:58 +0530)
committersaerome.kim <saerome.kim@samsung.com>
Tue, 5 Nov 2019 10:40:03 +0000 (19:40 +0900)
Change-Id: Idb6b9e9f2076972a0a4d4a2dedd173d7badfbc48
Signed-off-by: Abhay agarwal <ay.agarwal@samsung.com>
ua-daemon/src/ua-manager-core.c

index ee7a31d..49c4d52 100644 (file)
@@ -341,6 +341,7 @@ static int _uam_remove_user_device(uam_db_device_info_t *device)
                ret = __copy_tech_info_to_device_info(tech, &dev_info);
                if (UAM_ERROR_NONE != ret) {
                        UAM_ERR("__copy_tech_info_to_device_info failed");
+                       __uam_db_end_transaction(0);
                        return ret;
                }
 
@@ -470,12 +471,13 @@ static uam_db_tech_info_t *__get_tech_info_by_mac(const char *mac)
        return NULL;
 }
 
-static void __get_uam_db_dev_list_to_uam_dev_list(
+static int __get_uam_db_dev_list_to_uam_dev_list(
                GSList *db_dev_list, uam_device_info_s **device_list, int *count)
 {
        FUNC_ENTRY;
        GSList *l;
        int indx = 0;
+       int ret = UAM_ERROR_NONE;
 
        *count = 0;
        /* Calculate num devices first */
@@ -503,7 +505,6 @@ static void __get_uam_db_dev_list_to_uam_dev_list(
        for (l = db_dev_list; NULL != l; l = g_slist_next(l)) {
                uam_db_device_info_t *db_info = l->data;
                GSList *l1;
-               int ret;
 
                if (!db_info || !db_info->device_id)
                        continue;
@@ -518,6 +519,7 @@ static void __get_uam_db_dev_list_to_uam_dev_list(
                        ret = __copy_tech_info_to_device_info(tech, &((*device_list)[indx]));
                        if (UAM_ERROR_NONE != ret) {
                                UAM_ERR("__copy_tech_info_to_device_info failed");
+                               return ret;
                        }
                        indx++;
                }
@@ -525,6 +527,7 @@ static void __get_uam_db_dev_list_to_uam_dev_list(
 
        UAM_INFO("Count = %d, indx = %d", *count, indx);
        FUNC_EXIT;
+       return ret;
 }
 
 static uam_monitor_info_t *__uam_find_monitor(GSList *monitor_list,
@@ -757,6 +760,7 @@ int _uam_core_add_user(int *user_id, const char *account, const char *name)
        FUNC_ENTRY;
        GSList *l;
        uam_db_user_info_t *user;
+       int ret = UAM_ERROR_NONE;
 
        l = g_slist_find_custom(users, account, __compare_user_account);
        retv_if((NULL != l) && (l->data != NULL), UAM_ERROR_ALREADY_REGISTERED);
@@ -764,10 +768,11 @@ int _uam_core_add_user(int *user_id, const char *account, const char *name)
        user = g_new0(uam_db_user_info_t, 1);
 
        /* Add user to database */
-       if (UAM_ERROR_NONE != _uam_db_insert_user_info(&(user->user_id), name, account)) {
-               UAM_ERR("_uam_db_insert_user_info failed");
+       ret = _uam_db_insert_user_info(&(user->user_id), name, account);
+       if (UAM_ERROR_NONE != ret) {
+               UAM_ERR("_uam_db_insert_user_info failed [%d]", ret);
                g_free(user);
-               return UAM_ERROR_DB_FAILED;
+               return ret;
        }
 
        user->name = g_strdup(name);
@@ -942,10 +947,15 @@ static int __uam_remove_device(int user_id, uam_db_device_info_t *device,
        /* Send device removed event to application */
        __send_device_event(ret, UAM_EVENT_DEVICE_REMOVED, dev_info);
 
+       retv_if(UAM_ERROR_NONE != __uam_db_begin_transaction(), UAM_ERROR_DB_FAILED);
        /* Remove device from database */
-       if (UAM_ERROR_NONE != _uam_device_db_delete_device_info(
-                               dev_info->device_id, dev_info->type, dev_info->mac))
+       ret = _uam_device_db_delete_device_info(
+                               dev_info->device_id, dev_info->type, dev_info->mac);
+       if (UAM_ERROR_NONE != ret) {
                UAM_ERR("_uam_device_db_delete_device_info failed");
+               __uam_db_end_transaction(0);
+               return ret;
+       }
 
        /* Remove device from service */
        for (l = tech->svc_list; l; l = g_slist_next(l)) {
@@ -953,8 +963,15 @@ static int __uam_remove_device(int user_id, uam_db_device_info_t *device,
 
                if (!svc || !svc->name)
                        continue;
-               _uam_core_service_remove_device(svc->name, dev_info->device_id, dev_info->type);
+               ret = _uam_core_service_remove_device(svc->name,
+                               dev_info->device_id, dev_info->type);
+               if (UAM_ERROR_NONE != ret) {
+                       UAM_ERR("_uam_device_db_delete_device_info failed");
+                       __uam_db_end_transaction(0);
+                       return ret;
+               }
        }
+       __uam_db_end_transaction(1);
 
        /* Remove tech info from device's tech list */
        device->tech_list = g_slist_remove(device->tech_list, tech);
@@ -1073,7 +1090,7 @@ int _uam_core_remove_device_by_mac(const char *mac)
        ret = __copy_tech_info_to_device_info(tech, &dev_info);
        if (UAM_ERROR_NONE != ret) {
                UAM_ERR("__copy_tech_info_to_device_info failed");
-               return UAM_ERROR_INTERNAL;
+               return ret;
        }
 
        ret = __uam_remove_device(device->user->user_id, device, &dev_info, tech);
@@ -1086,7 +1103,7 @@ int _uam_core_update_device(const uam_device_info_s *a_device)
 {
        FUNC_ENTRY;
 
-       int ret;
+       int ret = UAM_ERROR_NONE;
        GSList *l, *l1, *l2;
 
        uam_device_info_s temp;
@@ -1141,25 +1158,33 @@ int _uam_core_update_device(const uam_device_info_s *a_device)
                tech->payload->bt_mac = g_memdup(&(a_device->payload.bt_mac), UAM_BT_MAC_ADDRESS_STRING_LEN);
 
                /* Update device's updated information to database */
-               __copy_tech_info_to_device_info(tech, &temp);
+               ret = __copy_tech_info_to_device_info(tech, &temp);
+               if (UAM_ERROR_NONE != ret) {
+                       UAM_ERR("__copy_tech_info_to_device_info failed [%d]", ret);
+                       return ret;
+               }
 
                ret = _uam_device_db_update_device_device(temp.device_id, temp.type,
                                temp.mac, temp.ipv4_addr, temp.operating_system, temp.discriminant,
                                temp.payload);
-               if (UAM_ERROR_NONE != ret)
-                       UAM_WARN("_uam_device_db_update_device_device failed [%d]", ret);
+               if (UAM_ERROR_NONE != ret) {
+                       UAM_ERR("_uam_device_db_update_device_device failed [%d]", ret);
+                       return ret;
+               }
        }
 
        /* Set/update registered device list to plugins */
        ret = _uam_pm_set_registered_devices(devices);
-       if (UAM_ERROR_NONE != ret)
+       if (UAM_ERROR_NONE != ret) {
                UAM_ERR("_uam_pm_set_registered_devices failed [%d]", ret);
+               return ret;
+       }
 
        /* Set/update registered device list to cloud plugin */
        _uam_cloud_update_registered_devices();
 
        FUNC_EXIT;
-       return UAM_ERROR_NONE;
+       return ret;
 }
 
 int _uam_core_get_default_user(uam_user_info_s *user_info)
@@ -1304,11 +1329,11 @@ int _uam_core_get_devcie_by_mac(const char *mac, uam_device_info_s *device)
        ret = __copy_tech_info_to_device_info(tech, device);
        if (UAM_ERROR_NONE != ret) {
                UAM_ERR("__copy_tech_info_to_device_info failed");
-               return UAM_ERROR_INTERNAL;
+               return ret;
        }
 
        FUNC_EXIT;
-       return UAM_ERROR_NONE;
+       return ret;
 }
 
 int _uam_core_get_user_by_device_id(const char *device_id, uam_user_info_s *user)
@@ -1352,11 +1377,12 @@ int _uam_core_get_user_by_mac(const char *mac, uam_user_info_s *user)
 int _uam_core_get_devices(int *count, uam_device_info_s **device_list)
 {
        FUNC_ENTRY;
+       int ret = UAM_ERROR_NONE;
 
-       __get_uam_db_dev_list_to_uam_dev_list(devices, device_list, count);
+       ret = __get_uam_db_dev_list_to_uam_dev_list(devices, device_list, count);
 
        FUNC_EXIT;
-       return UAM_ERROR_NONE;
+       return ret;
 }
 
 int _uam_core_get_user_devices(const char *account,
@@ -1365,15 +1391,16 @@ int _uam_core_get_user_devices(const char *account,
        FUNC_ENTRY;
        uam_db_user_info_t *user;
        GSList *l;
+       int ret = UAM_ERROR_NONE;
 
        l = g_slist_find_custom(users, account, __compare_user_account);
        retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
        user = l->data;
 
-       __get_uam_db_dev_list_to_uam_dev_list(user->devices, device_list, count);
+       ret = __get_uam_db_dev_list_to_uam_dev_list(user->devices, device_list, count);
 
        FUNC_EXIT;
-       return UAM_ERROR_NONE;
+       return ret;
 }
 
 int _uam_core_get_available_sensors(unsigned int *sensor_bitmask)
@@ -1679,6 +1706,7 @@ int _uam_core_service_remove_device(const char *svc_name, const char *device_id,
        GSList *l;
        char *mac;
        uam_db_tech_info_t *tech_info;
+       int ret = UAM_ERROR_NONE;
 
        retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAMETER);
        retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
@@ -1700,9 +1728,12 @@ int _uam_core_service_remove_device(const char *svc_name, const char *device_id,
 
        mac = __get_mac_addr(tech_info);
        /* Remove service-device from DB */
-       if (UAM_ERROR_NONE != _uam_db_delete_device_service_info(
-                               device_id, tech_type, mac, svc_name))
-               UAM_WARN("Device service removal from persistent DB failed");
+       ret = _uam_db_delete_device_service_info(
+                       device_id, tech_type, mac, svc_name);
+       if (UAM_ERROR_NONE != ret) {
+               UAM_ERR("Device service removal from persistent DB failed");
+               return ret;
+       }
 
        FUNC_EXIT;
        return UAM_ERROR_NONE;
@@ -2452,11 +2483,13 @@ int _uam_core_handle_device_added(int status,
        __uam_core_add_dev_to_list(user, dev_info,
                        UAM_PRESENCE_STATE_PRESENT, timestamp, svc_list);
 
+       retv_if(UAM_ERROR_NONE != __uam_db_begin_transaction(), UAM_ERROR_DB_FAILED);
        /* Add device to database */
        ret = _uam_device_db_insert_device_info(user->user_id,
                                dev_info, UAM_PRESENCE_STATE_PRESENT, timestamp);
        if (UAM_ERROR_NONE != ret) {
                UAM_WARN("Device addition to persistent DB failed");
+               __uam_db_end_transaction(0);
                return ret;
        }
 
@@ -2468,8 +2501,10 @@ int _uam_core_handle_device_added(int status,
        if (UAM_ERROR_NONE != ret) {
                UAM_WARN("Device service addition to persistent DB failed");
                __send_device_event(ret, UAM_EVENT_DEVICE_ADDED, dev_info);
+               __uam_db_end_transaction(0);
                return ret;
        }
+       __uam_db_end_transaction(1);
 
        ret = _uam_core_update_svc_dev_info(
                                dev_info->device_id, dev_info->type,
@@ -3468,12 +3503,13 @@ int _uam_core_unregister_service(const char *svc_name)
        return UAM_ERROR_NONE;
 }
 
-static void __get_service_dev_list(
+static int __get_service_dev_list(
                uam_db_service_info_t* service, uam_device_info_s **device_list, int *count)
 {
        FUNC_ENTRY;
        GSList *l1;
        int indx = 0;
+       int ret = UAM_ERROR_NONE;
 
        *count = 0;
        /* Calculate number of devices */
@@ -3490,7 +3526,6 @@ static void __get_service_dev_list(
        /* Copy devices */
        for (l1 = service->dev_techs; NULL != l1; l1 = g_slist_next(l1)) {
                uam_db_tech_info_t *tech = l1->data;
-               int ret;
 
                if (!tech || !tech->addresses)
                        continue;
@@ -3499,12 +3534,14 @@ static void __get_service_dev_list(
                ret = __copy_tech_info_to_device_info(tech, &((*device_list)[indx]));
                if (UAM_ERROR_NONE != ret) {
                        UAM_ERR("__copy_tech_info_to_device_info failed");
+                       return ret;
                }
                indx++;
        }
 
        UAM_INFO("Count = %d, indx = %d", *count, indx);
        FUNC_EXIT;
+       return ret;
 }
 
 int _uam_core_get_service_devices(const char *svc_name,
@@ -3513,15 +3550,16 @@ int _uam_core_get_service_devices(const char *svc_name,
        FUNC_ENTRY;
        uam_db_service_info_t *service;
        GSList *l;
+       int ret = UAM_ERROR_NONE;
 
        l = g_slist_find_custom(services, svc_name, __compare_svc_name);
        retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
        service = l->data;
 
-       __get_service_dev_list(service, device_list, count);
+       ret = __get_service_dev_list(service, device_list, count);
 
        FUNC_EXIT;
-       return UAM_ERROR_NONE;
+       return ret;
 }
 
 static void __get_service_user_list(