Add the callback function to change gatt service 99/86399/1
authorDoHyun Pyun <dh79.pyun@samsung.com>
Thu, 1 Sep 2016 04:18:10 +0000 (13:18 +0900)
committerDoHyun Pyun <dh79.pyun@samsung.com>
Thu, 1 Sep 2016 04:18:10 +0000 (13:18 +0900)
Change-Id: Ida0ed8000896b8fea6c58ad16e41ee0f0ecbca29
Signed-off-by: DoHyun Pyun <dh79.pyun@samsung.com>
include/bluetooth_private.h
src/bluetooth-gatt.c
test/bt_unit_test.c
test/bt_unit_test.h

index 8f8144c7eb8431543417790ed0d30efdfd990a1d..8ce9e5e1202d3c58fc4f3ebaf4ce6c8d7f070ce2 100644 (file)
@@ -1004,6 +1004,39 @@ int bt_passkey_reply(char *passkey, bool authentication_reply);
  */
 int bt_passkey_confirmation_reply(bool confirmation_reply);
 
+/**
+ * @ingroup CAPI_NETWORK_BLUETOOTH_GATT_MODULE
+ * @brief  Registers a callback function to be invoked when service is changed from a remote device(GATT server).
+ * @since_tizen 3.0
+ *
+ * @param[in] client The GATT client's handle
+ * @param[in] callback The callback to be invoked
+ * @param[in] user_data The user data to be passed to @a callback function
+ * @return  0 on success, otherwise a negative error value
+ * @retval #BT_ERROR_NONE Successful
+ * @retval #BT_ERROR_INVALID_PARAMETER  Invalid parameter
+ * @retval #BT_ERROR_NOT_SUPPORTED  Not supported
+ *
+ * @see bt_gatt_client_unset_service_changed_cb()
+ */
+int bt_gatt_client_set_service_changed_cb(bt_gatt_client_h client,
+               bt_gatt_client_service_changed_cb callback, void *user_data);
+
+/**
+ * @ingroup CAPI_NETWORK_BLUETOOTH_GATT_MODULE
+ * @brief  Unregisters a callback function
+ * @since_tizen 3.0
+ *
+ * @param[in] client The GATT client's handle
+ * @return  0 on success, otherwise a negative error value
+ * @retval #BT_ERROR_NONE Successful
+ * @retval #BT_ERROR_INVALID_PARAMETER  Invalid parameter
+ * @retval #BT_ERROR_NOT_SUPPORTED  Not supported
+ *
+ * @see bt_gatt_client_unset_service_changed_cb()
+ */
+int bt_gatt_client_unset_service_changed_cb(bt_gatt_client_h client);
+
 /**
  * @ingroup CAPI_NETWORK_BLUETOOTH_HID_MODULE
  * @brief Sends the custom event data.
index cfe372d81f367ff73e846c7291fc5278f1454917..75696b9ea10bb8d553aaac4c2d4d97aaf013508e 100644 (file)
@@ -3150,3 +3150,59 @@ int bt_gatt_client_foreach_services(bt_gatt_client_h client,
 
        return BT_ERROR_NONE;
 }
+
+int bt_gatt_client_set_service_changed_cb(bt_gatt_client_h client,
+               bt_gatt_client_service_changed_cb callback, void *user_data)
+{
+       int ret;
+       bluetooth_device_address_t bd_addr = { {0,} };
+       bt_gatt_client_s *client_s = (bt_gatt_client_s *)client;
+
+       BT_CHECK_GATT_SUPPORT();
+       BT_CHECK_INIT_STATUS();
+       BT_CHECK_INPUT_PARAMETER(client);
+       BT_CHECK_INPUT_PARAMETER(callback);
+
+       if (client_s->service_changed_cb) {
+               BT_INFO("Already registered");
+               client_s->service_changed_cb = callback;
+               client_s->service_changed_user_data = user_data;
+               return BT_ERROR_NONE;
+       }
+
+       _bt_convert_address_to_hex(&bd_addr, client_s->remote_address);
+       ret = _bt_get_error_code(bluetooth_gatt_set_service_change_watcher(&bd_addr, TRUE));
+
+       if (ret == BT_ERROR_NONE) {
+               BT_INFO("Service Changed callback registered");
+               client_s->service_changed_cb = callback;
+               client_s->service_changed_user_data = user_data;
+       } else {
+               BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(ret), ret);
+       }
+
+       return ret;
+}
+
+int bt_gatt_client_unset_service_changed_cb(bt_gatt_client_h client)
+{
+       int ret;
+       bluetooth_device_address_t bd_addr = { {0,} };
+       bt_gatt_client_s *client_s = (bt_gatt_client_s *)client;
+
+       BT_CHECK_GATT_SUPPORT();
+       BT_CHECK_INIT_STATUS();
+       BT_CHECK_INPUT_PARAMETER(client);
+
+       _bt_convert_address_to_hex(&bd_addr, client_s->remote_address);
+       ret = _bt_get_error_code(bluetooth_gatt_set_service_change_watcher(&bd_addr, FALSE));
+       if (ret == BT_ERROR_NONE) {
+               BT_INFO("Service Changed callback unregistered [%s]", client_s->remote_address);
+               client_s->service_changed_cb = NULL;
+               client_s->service_changed_user_data = NULL;
+       } else {
+               BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(ret), ret);
+       }
+
+       return BT_ERROR_NONE;
+}
index 13454c9d4a40f657169516a2911d65efcdcaea5a..9d25c916411da255daf2e3e6dbf784f2af6db9e1 100644 (file)
@@ -561,6 +561,10 @@ tc_table_t tc_gatt[] = {
                , BT_UNIT_TEST_FUNCTION_GATT_CLIENT_UNSET_CHAR_VALUE_CHANGED_CB},
        {"bt_gatt_client_foreach_services"
                , BT_UNIT_TEST_FUNCTION_GATT_CLIENT_FOREACH_SERVICES},
+       {"bt_gatt_client_set_service_changed_cb"
+               , BT_UNIT_TEST_FUNCTION_GATT_CLIENT_SET_SERVICE_CHANGED_CB},
+       {"bt_gatt_client_unset_service_changed_cb"
+               , BT_UNIT_TEST_FUNCTION_GATT_CLIENT_UNSET_SERVICE_CHANGED_CB},
        {"bt_gatt_server_initialize"
                , BT_UNIT_TEST_FUNCTION_GATT_SERVER_INITIALIZE},
        {"bt_gatt_server_deinitilaize"
@@ -1985,6 +1989,29 @@ void __bt_hps_connection_state_changed_cb(int result,
        }
 }
 
+void __bt_gatt_client_service_changed_cb(bt_gatt_client_h c,
+               bt_gatt_client_service_change_type_e type,
+               const char *uuid, void *user_data)
+{
+       char *addr = NULL;
+       char *svc = NULL;
+       int ret;
+
+       ret = bt_gatt_client_get_remote_address(client, &addr);
+       if (ret != BT_ERROR_NONE)
+               TC_PRT("Cannot get remote address");
+
+       ret = bt_get_uuid_name(uuid, &svc);
+       if (ret != BT_ERROR_NONE)
+               svc = g_strdup(uuid);
+
+       TC_PRT("Remote deivce [%s] : %s is %s", addr, svc,
+                       type == BT_GATT_CLIENT_SERVICE_ADDED ?
+                       "Added" : "Removed");
+       g_free(svc);
+       g_free(addr);
+}
+
 int __bt_gatt_client_set_value(char *type, char *value, bt_gatt_h h)
 {
        int ret;
@@ -5970,6 +5997,19 @@ int test_input_callback(void *data)
                                TC_PRT("bt_gatt_client_foreach_services_by_uuid is failed");
                        break;
                }
+               case BT_UNIT_TEST_FUNCTION_GATT_CLIENT_SET_SERVICE_CHANGED_CB: {
+                       ret = bt_gatt_client_set_service_changed_cb(client,
+                                       __bt_gatt_client_service_changed_cb, NULL);
+                       if (ret != BT_ERROR_NONE)
+                               TC_PRT("bt_gatt_client_set_service_changed_cb is failed");
+                       break;
+               }
+               case BT_UNIT_TEST_FUNCTION_GATT_CLIENT_UNSET_SERVICE_CHANGED_CB: {
+                       ret = bt_gatt_client_unset_service_changed_cb(client);
+                       if (ret != BT_ERROR_NONE)
+                               TC_PRT("bt_gatt_client_unset_service_changed_cb is failed");
+                       break;
+               }
                case BT_UNIT_TEST_FUNCTION_GATT_SERVER_INITIALIZE: {
 
                        ret = bt_gatt_server_initialize();
index 208b4cc69b61b4e1fbb3d174e14ce9f7d300f6a6..1fea06dfc62f15a6f0490e96f47abf9fa72d25c7 100644 (file)
@@ -228,6 +228,8 @@ typedef enum {
        BT_UNIT_TEST_FUNCTION_GATT_CLIENT_SET_CHAR_VALUE_CHANGED_CB,
        BT_UNIT_TEST_FUNCTION_GATT_CLIENT_UNSET_CHAR_VALUE_CHANGED_CB,
        BT_UNIT_TEST_FUNCTION_GATT_CLIENT_FOREACH_SERVICES,
+       BT_UNIT_TEST_FUNCTION_GATT_CLIENT_SET_SERVICE_CHANGED_CB,
+       BT_UNIT_TEST_FUNCTION_GATT_CLIENT_UNSET_SERVICE_CHANGED_CB,
        BT_UNIT_TEST_FUNCTION_GATT_SERVER_INITIALIZE,
        BT_UNIT_TEST_FUNCTION_GATT_SERVER_DEINITIALIZE,
        BT_UNIT_TEST_FUNCTION_GATT_SERVER_UNREGISTER_ALL_SERVICES,