Reduce the complexity of __bt_update_otp_server_data function 36/187636/2 accepted/tizen/unified/20180828.062333 submit/tizen/20180827.065630
authorDoHyun Pyun <dh79.pyun@samsung.com>
Mon, 27 Aug 2018 05:47:49 +0000 (14:47 +0900)
committerDoHyun Pyun <dh79.pyun@samsung.com>
Mon, 27 Aug 2018 06:23:09 +0000 (15:23 +0900)
Change-Id: I03d9a8b29e5d2d8dc3ecd04db3650a7896175ac1
Signed-off-by: DoHyun Pyun <dh79.pyun@samsung.com>
src/bluetooth-otp.c

index a4848a6..d99c170 100644 (file)
@@ -502,6 +502,166 @@ int bt_otp_client_unset_connection_state_changed_cb(bt_otp_client_h otp_client)
        return BT_ERROR_NONE;
 }
 
+static int __bt_otp_update_data(bt_otp_client_s *otp_client_s, bt_gatt_char_property_t *characteristic)
+{
+       int ret = BLUETOOTH_ERROR_NONE;
+
+       if (g_strstr_len(characteristic->uuid, -1, BT_OTP_FEATURE_UUID)) {
+               BT_DBG("OTP feature characteristic discovered");
+
+               if (otp_client_s->otp_feature_obj_path)
+                       g_free(otp_client_s->otp_feature_obj_path);
+               otp_client_s->otp_feature_obj_path = g_strdup(characteristic->handle);
+
+               ret = _bt_get_error_code(bluetooth_otp_read_characteristic_value(otp_client_s->otp_feature_obj_path));
+               if (ret != BT_ERROR_NONE)
+                       BT_ERR("Failed to read OTP Feature : %s(0x%08x)", _bt_convert_error_to_string(ret), ret);
+
+               BT_DBG("OTP feature handle [%s]", otp_client_s->otp_feature_obj_path);
+       } else if (g_strstr_len(characteristic->uuid, -1, BT_OTP_OACP_CONTROL_POINT_UUID)) {
+               BT_DBG("OTP OACP Control Point discovered");
+               bt_gatt_char_descriptor_property_t desc_property;
+               memset(&desc_property, 0x00, sizeof(desc_property));
+
+               /* Get CCCD for Control Point */
+               ret = bluetooth_gatt_get_char_descriptor_property(
+                               characteristic->char_desc_handle.handle[0], &desc_property);
+
+               if (ret != BLUETOOTH_ERROR_NONE) {
+                       BT_ERR("Failed to discover CCCD for OACP Control point");
+                       bluetooth_gatt_free_desc_property(&desc_property);
+                       return ret;
+               }
+               if (otp_client_s->otp_oacp_control_point)
+                       g_free(otp_client_s->otp_oacp_control_point);
+               otp_client_s->otp_oacp_control_point = g_strdup(characteristic->handle);
+
+               if (otp_client_s->otp_oacp_cccd)
+                       g_free(otp_client_s->otp_oacp_cccd);
+               otp_client_s->otp_oacp_cccd = g_strdup(desc_property.handle);
+
+               BT_DBG("OACP Control point handle [%s]", otp_client_s->otp_oacp_control_point);
+               BT_DBG("OACP Control point CCCD handle [%s]", otp_client_s->otp_oacp_cccd);
+       } else if (g_strstr_len(characteristic->uuid, -1, BT_OTP_OLCP_CONTROL_POINT_UUID)) {
+               BT_DBG("OTP OLCP Control Point discovered");
+               bt_gatt_char_descriptor_property_t desc_property;
+               memset(&desc_property, 0x00, sizeof(desc_property));
+
+               /* Get CCCD for Control Point */
+               ret = bluetooth_gatt_get_char_descriptor_property(
+                               characteristic->char_desc_handle.handle[0], &desc_property);
+
+               if (ret != BLUETOOTH_ERROR_NONE) {
+                       BT_ERR("Failed to discover CCCD for OLCP Control point");
+                       bluetooth_gatt_free_desc_property(&desc_property);
+                       return ret;
+               }
+               if (otp_client_s->otp_olcp_control_point)
+                       g_free(otp_client_s->otp_olcp_control_point);
+               otp_client_s->otp_olcp_control_point = g_strdup(characteristic->handle);
+
+               if (otp_client_s->otp_olcp_cccd)
+                       g_free(otp_client_s->otp_olcp_cccd);
+               otp_client_s->otp_olcp_cccd = g_strdup(desc_property.handle);
+
+               BT_DBG("OLCP Control point handle [%s]", otp_client_s->otp_olcp_control_point);
+               BT_DBG("OLCP Control point CCCD handle [%s]", otp_client_s->otp_olcp_cccd);
+       }  else if (g_strstr_len(characteristic->uuid, -1, BT_OTP_OBJECT_NAME_UUID)) {
+               BT_DBG("OTP Object Name characteristic discovered");
+
+               if (otp_client_s->otp_name_obj_path)
+                       g_free(otp_client_s->otp_name_obj_path);
+               otp_client_s->otp_name_obj_path = g_strdup(characteristic->handle);
+
+               BT_DBG("OTP Object Name handle [%s]", otp_client_s->otp_name_obj_path);
+       }  else if (g_strstr_len(characteristic->uuid, -1, BT_OTP_OBJECT_TYPE_UUID)) {
+               BT_DBG("OTP Object Type characteristic discovered");
+
+               if (otp_client_s->otp_type_obj_path)
+                       g_free(otp_client_s->otp_type_obj_path);
+               otp_client_s->otp_type_obj_path = g_strdup(characteristic->handle);
+
+               BT_DBG("OTP Object Type handle [%s]", otp_client_s->otp_type_obj_path);
+       }  else if (g_strstr_len(characteristic->uuid, -1, BT_OTP_OBJECT_SIZE_UUID)) {
+               BT_DBG("OTP Object Size characteristic discovered");
+
+               if (otp_client_s->otp_size_obj_path)
+                       g_free(otp_client_s->otp_size_obj_path);
+               otp_client_s->otp_size_obj_path = g_strdup(characteristic->handle);
+
+               BT_DBG("OTP Object Size handle [%s]", otp_client_s->otp_size_obj_path);
+       }  else if (g_strstr_len(characteristic->uuid, -1, BT_OTP_OBJECT_FIRST_CREATED_UUID)) {
+               BT_DBG("OTP Object First Created characteristic discovered");
+
+               if (otp_client_s->otp_first_created_obj_path)
+                       g_free(otp_client_s->otp_first_created_obj_path);
+               otp_client_s->otp_first_created_obj_path = g_strdup(characteristic->handle);
+
+               BT_DBG("OTP Object First Created handle [%s]", otp_client_s->otp_first_created_obj_path);
+       }  else if (g_strstr_len(characteristic->uuid, -1, BT_OTP_OBJECT_LAST_MODIFIED_UUID)) {
+               BT_DBG("OTP Object Last Modified characteristic discovered");
+
+               if (otp_client_s->otp_last_modified_obj_path)
+                       g_free(otp_client_s->otp_last_modified_obj_path);
+               otp_client_s->otp_last_modified_obj_path = g_strdup(characteristic->handle);
+
+               BT_DBG("OTP Object Last Modified handle [%s]", otp_client_s->otp_last_modified_obj_path);
+       }  else if (g_strstr_len(characteristic->uuid, -1, BT_OTP_OBJECT_ID_UUID)) {
+               BT_DBG("OTP Object ID characteristic discovered");
+
+               if (otp_client_s->otp_id_obj_path)
+                       g_free(otp_client_s->otp_id_obj_path);
+               otp_client_s->otp_id_obj_path = g_strdup(characteristic->handle);
+
+               BT_DBG("OTP Object ID handle [%s]", otp_client_s->otp_id_obj_path);
+       }  else if (g_strstr_len(characteristic->uuid, -1, BT_OTP_OBJECT_PROP_UUID)) {
+               BT_DBG("OTP Object Properties characteristic discovered");
+
+               if (otp_client_s->otp_props_obj_path)
+                       g_free(otp_client_s->otp_props_obj_path);
+               otp_client_s->otp_props_obj_path = g_strdup(characteristic->handle);
+
+               BT_DBG("OTP Object Properties handle [%s]", otp_client_s->otp_props_obj_path);
+       }  else if (g_strstr_len(characteristic->uuid, -1, BT_OTP_OBJECT_LIST_FILTER_UUID)) {
+               BT_DBG("OTP Object List Filter characteristic discovered");
+
+               if (otp_client_s->otp_list_filter_obj_path)
+                       g_free(otp_client_s->otp_list_filter_obj_path);
+               otp_client_s->otp_list_filter_obj_path = g_strdup(characteristic->handle);
+
+               BT_DBG("OTP Object List Filter handle [%s]", otp_client_s->otp_list_filter_obj_path);
+       }  else if (g_strstr_len(characteristic->uuid, -1, BT_OTP_OBJECT_CHANGED_UUID)) {
+               BT_DBG("OTP Object Changed characteristic discovered");
+               bt_gatt_char_descriptor_property_t desc_property;
+               memset(&desc_property, 0x00, sizeof(desc_property));
+
+               /* Get CCCD for Object Changed Charc */
+               ret = bluetooth_gatt_get_char_descriptor_property(
+                               characteristic->char_desc_handle.handle[0], &desc_property);
+
+               if (ret != BLUETOOTH_ERROR_NONE) {
+                       BT_ERR("Failed to discover CCCD for Object Changed Charc");
+                       bluetooth_gatt_free_desc_property(&desc_property);
+                       return ret;
+               }
+               if (otp_client_s->otp_obj_changed_obj_path)
+                       g_free(otp_client_s->otp_obj_changed_obj_path);
+               otp_client_s->otp_obj_changed_obj_path = g_strdup(characteristic->handle);
+
+               if (otp_client_s->otp_obj_changed_cccd)
+                       g_free(otp_client_s->otp_obj_changed_cccd);
+               otp_client_s->otp_obj_changed_cccd = g_strdup(desc_property.handle);
+
+               BT_DBG("OTP Object Changed handle [%s]", otp_client_s->otp_obj_changed_obj_path);
+               BT_DBG("OTP Object Changed CCCD handle [%s]", otp_client_s->otp_obj_changed_cccd);
+       }       else {
+               BT_DBG("Other OTP Characteristic handle [%s]", characteristic->handle);
+               BT_DBG("UUID [%s]", characteristic->uuid);
+       }
+
+       return ret;
+}
+
 static int __bt_update_otp_server_data(bluetooth_device_address_t *address, bt_otp_client_s *otp_client_s)
 {
        int ret = BLUETOOTH_ERROR_INTERNAL;
@@ -529,166 +689,16 @@ static int __bt_update_otp_server_data(bluetooth_device_address_t *address, bt_o
                        bluetooth_gatt_free_service_property(&service);
                        bluetooth_gatt_free_char_property(&characteristic);
                        goto fail;
-               } else {
-                       if (g_strstr_len(characteristic.uuid, -1, BT_OTP_FEATURE_UUID)) {
-                               BT_DBG("OTP feature characteristic discovered");
-
-                               if (otp_client_s->otp_feature_obj_path)
-                                       g_free(otp_client_s->otp_feature_obj_path);
-                               otp_client_s->otp_feature_obj_path = g_strdup(characteristic.handle);
-
-                               ret = _bt_get_error_code(bluetooth_otp_read_characteristic_value(otp_client_s->otp_feature_obj_path));
-                               if (ret != BT_ERROR_NONE)
-                                       BT_ERR("Failed to read OTP Feature : %s(0x%08x)", _bt_convert_error_to_string(ret), ret);
-
-                               BT_DBG("OTP feature handle [%s]", otp_client_s->otp_feature_obj_path);
-                       } else if (g_strstr_len(characteristic.uuid, -1, BT_OTP_OACP_CONTROL_POINT_UUID)) {
-                               BT_DBG("OTP OACP Control Point discovered");
-                               bt_gatt_char_descriptor_property_t desc_property;
-                               memset(&desc_property, 0x00, sizeof(desc_property));
-
-                               /* Get CCCD for Control Point */
-                               ret = bluetooth_gatt_get_char_descriptor_property(
-                                               characteristic.char_desc_handle.handle[0], &desc_property);
-
-                               if (ret != BLUETOOTH_ERROR_NONE) {
-                                       BT_ERR("Failed to discover CCCD for OACP Control point");
-                                       bluetooth_gatt_free_service_property(&service);
-                                       bluetooth_gatt_free_char_property(&characteristic);
-                                       bluetooth_gatt_free_desc_property(&desc_property);
-                                       goto fail;
-                               }
-                               if (otp_client_s->otp_oacp_control_point)
-                                       g_free(otp_client_s->otp_oacp_control_point);
-                               otp_client_s->otp_oacp_control_point = g_strdup(characteristic.handle);
-
-                               if (otp_client_s->otp_oacp_cccd)
-                                       g_free(otp_client_s->otp_oacp_cccd);
-                               otp_client_s->otp_oacp_cccd = g_strdup(desc_property.handle);
-
-                               BT_DBG("OACP Control point handle [%s]", otp_client_s->otp_oacp_control_point);
-                               BT_DBG("OACP Control point CCCD handle [%s]", otp_client_s->otp_oacp_cccd);
-                       } else if (g_strstr_len(characteristic.uuid, -1, BT_OTP_OLCP_CONTROL_POINT_UUID)) {
-                               BT_DBG("OTP OLCP Control Point discovered");
-                               bt_gatt_char_descriptor_property_t desc_property;
-                               memset(&desc_property, 0x00, sizeof(desc_property));
-
-                               /* Get CCCD for Control Point */
-                               ret = bluetooth_gatt_get_char_descriptor_property(
-                                               characteristic.char_desc_handle.handle[0], &desc_property);
-
-                               if (ret != BLUETOOTH_ERROR_NONE) {
-                                       BT_ERR("Failed to discover CCCD for OLCP Control point");
-                                       bluetooth_gatt_free_service_property(&service);
-                                       bluetooth_gatt_free_char_property(&characteristic);
-                                       bluetooth_gatt_free_desc_property(&desc_property);
-                                       goto fail;
-                               }
-                               if (otp_client_s->otp_olcp_control_point)
-                                       g_free(otp_client_s->otp_olcp_control_point);
-                               otp_client_s->otp_olcp_control_point = g_strdup(characteristic.handle);
-
-                               if (otp_client_s->otp_olcp_cccd)
-                                       g_free(otp_client_s->otp_olcp_cccd);
-                               otp_client_s->otp_olcp_cccd = g_strdup(desc_property.handle);
-
-                               BT_DBG("OLCP Control point handle [%s]", otp_client_s->otp_olcp_control_point);
-                               BT_DBG("OLCP Control point CCCD handle [%s]", otp_client_s->otp_olcp_cccd);
-                       }  else if (g_strstr_len(characteristic.uuid, -1, BT_OTP_OBJECT_NAME_UUID)) {
-                               BT_DBG("OTP Object Name characteristic discovered");
-
-                               if (otp_client_s->otp_name_obj_path)
-                                       g_free(otp_client_s->otp_name_obj_path);
-                               otp_client_s->otp_name_obj_path = g_strdup(characteristic.handle);
-
-                               BT_DBG("OTP Object Name handle [%s]", otp_client_s->otp_name_obj_path);
-                       }  else if (g_strstr_len(characteristic.uuid, -1, BT_OTP_OBJECT_TYPE_UUID)) {
-                               BT_DBG("OTP Object Type characteristic discovered");
-
-                               if (otp_client_s->otp_type_obj_path)
-                                       g_free(otp_client_s->otp_type_obj_path);
-                               otp_client_s->otp_type_obj_path = g_strdup(characteristic.handle);
-
-                               BT_DBG("OTP Object Type handle [%s]", otp_client_s->otp_type_obj_path);
-                       }  else if (g_strstr_len(characteristic.uuid, -1, BT_OTP_OBJECT_SIZE_UUID)) {
-                               BT_DBG("OTP Object Size characteristic discovered");
-
-                               if (otp_client_s->otp_size_obj_path)
-                                       g_free(otp_client_s->otp_size_obj_path);
-                               otp_client_s->otp_size_obj_path = g_strdup(characteristic.handle);
-
-                               BT_DBG("OTP Object Size handle [%s]", otp_client_s->otp_size_obj_path);
-                       }  else if (g_strstr_len(characteristic.uuid, -1, BT_OTP_OBJECT_FIRST_CREATED_UUID)) {
-                               BT_DBG("OTP Object First Created characteristic discovered");
-
-                               if (otp_client_s->otp_first_created_obj_path)
-                                       g_free(otp_client_s->otp_first_created_obj_path);
-                               otp_client_s->otp_first_created_obj_path = g_strdup(characteristic.handle);
-
-                               BT_DBG("OTP Object First Created handle [%s]", otp_client_s->otp_first_created_obj_path);
-                       }  else if (g_strstr_len(characteristic.uuid, -1, BT_OTP_OBJECT_LAST_MODIFIED_UUID)) {
-                               BT_DBG("OTP Object Last Modified characteristic discovered");
-
-                               if (otp_client_s->otp_last_modified_obj_path)
-                                       g_free(otp_client_s->otp_last_modified_obj_path);
-                               otp_client_s->otp_last_modified_obj_path = g_strdup(characteristic.handle);
-
-                               BT_DBG("OTP Object Last Modified handle [%s]", otp_client_s->otp_last_modified_obj_path);
-                       }  else if (g_strstr_len(characteristic.uuid, -1, BT_OTP_OBJECT_ID_UUID)) {
-                               BT_DBG("OTP Object ID characteristic discovered");
-
-                               if (otp_client_s->otp_id_obj_path)
-                                       g_free(otp_client_s->otp_id_obj_path);
-                               otp_client_s->otp_id_obj_path = g_strdup(characteristic.handle);
-
-                               BT_DBG("OTP Object ID handle [%s]", otp_client_s->otp_id_obj_path);
-                       }  else if (g_strstr_len(characteristic.uuid, -1, BT_OTP_OBJECT_PROP_UUID)) {
-                               BT_DBG("OTP Object Properties characteristic discovered");
-
-                               if (otp_client_s->otp_props_obj_path)
-                                       g_free(otp_client_s->otp_props_obj_path);
-                               otp_client_s->otp_props_obj_path = g_strdup(characteristic.handle);
-
-                               BT_DBG("OTP Object Properties handle [%s]", otp_client_s->otp_props_obj_path);
-                       }  else if (g_strstr_len(characteristic.uuid, -1, BT_OTP_OBJECT_LIST_FILTER_UUID)) {
-                               BT_DBG("OTP Object List Filter characteristic discovered");
-
-                               if (otp_client_s->otp_list_filter_obj_path)
-                                       g_free(otp_client_s->otp_list_filter_obj_path);
-                               otp_client_s->otp_list_filter_obj_path = g_strdup(characteristic.handle);
-
-                               BT_DBG("OTP Object List Filter handle [%s]", otp_client_s->otp_list_filter_obj_path);
-                       }  else if (g_strstr_len(characteristic.uuid, -1, BT_OTP_OBJECT_CHANGED_UUID)) {
-                               BT_DBG("OTP Object Changed characteristic discovered");
-                               bt_gatt_char_descriptor_property_t desc_property;
-                               memset(&desc_property, 0x00, sizeof(desc_property));
-
-                               /* Get CCCD for Object Changed Charc */
-                               ret = bluetooth_gatt_get_char_descriptor_property(
-                                               characteristic.char_desc_handle.handle[0], &desc_property);
-
-                               if (ret != BLUETOOTH_ERROR_NONE) {
-                                       BT_ERR("Failed to discover CCCD for Object Changed Charc");
-                                       bluetooth_gatt_free_service_property(&service);
-                                       bluetooth_gatt_free_char_property(&characteristic);
-                                       bluetooth_gatt_free_desc_property(&desc_property);
-                                       goto fail;
-                               }
-                               if (otp_client_s->otp_obj_changed_obj_path)
-                                       g_free(otp_client_s->otp_obj_changed_obj_path);
-                               otp_client_s->otp_obj_changed_obj_path = g_strdup(characteristic.handle);
-
-                               if (otp_client_s->otp_obj_changed_cccd)
-                                       g_free(otp_client_s->otp_obj_changed_cccd);
-                               otp_client_s->otp_obj_changed_cccd = g_strdup(desc_property.handle);
-
-                               BT_DBG("OTP Object Changed handle [%s]", otp_client_s->otp_obj_changed_obj_path);
-                               BT_DBG("OTP Object Changed CCCD handle [%s]", otp_client_s->otp_obj_changed_cccd);
-                       }       else {
-                               BT_DBG("Other OTP Characteristic handle [%s]", characteristic.handle);
-                               BT_DBG("UUID [%s]", characteristic.uuid);
-                       }
-               } /* Characteristic property get successful */
+               }
+
+               ret = __bt_otp_update_data(otp_client_s, &characteristic);
+               if (ret != BLUETOOTH_ERROR_NONE) {
+                       BT_ERR("Update data failed(0x%08x)", ret);
+                       bluetooth_gatt_free_service_property(&service);
+                       bluetooth_gatt_free_char_property(&characteristic);
+                       goto fail;
+               }
+
                bluetooth_gatt_free_char_property(&characteristic);
        } /* Next Charatceristic */