[HRP Collector] Notify Fixes and Code cleanup 58/188558/3
authorAbhishek Chandra <abhishek.ch@samsung.com>
Thu, 6 Sep 2018 05:58:30 +0000 (11:28 +0530)
committerPyun DoHyun <dh79.pyun@samsung.com>
Fri, 7 Sep 2018 05:13:48 +0000 (05:13 +0000)
Changes related to heart rate notification and
body sensor location due to fixes in the bluetooth
framework related to notification.

Change-Id: I2b8b9935ed6958c1f75999e9d6228ae1492d6b3d
Signed-off-by: Abhishek Chandra <abhishek.ch@samsung.com>
src/bluetooth-hrp.c

index e754b941491f00e662b612aef694a39cf1e97331..7ac98eddce767d60accca77fa6f99176e66770d2 100755 (executable)
@@ -101,8 +101,10 @@ static bt_hrp_server_info_s hrp_server_info_s;
 typedef struct {
        char *remote_address;   //remote sensor
        bool connected;         //is connected to sensor or not
+       bt_gatt_client_h client;
+       bt_gatt_client_h bsl_chr_h;     //bsl char handle
+       bt_gatt_client_h hr_chr_notify_h; //notify char hr handle
        int heart_rate_measurement;
-       bt_gatt_h bsl_char_handle;
        bt_hrp_collector_connection_state_changed_cb connection_callback;       //GATT connection state changes callback
        void *conn_cb_user_data;        //
        char *hrs_service_handle;            /* HRS Primary Service Handle */
@@ -157,30 +159,6 @@ static void __bt_hrp_sensor_read_value_requested_cb(
 
 }
 
-
-static void __bt_hrp_sensor_write_value_requested_cb(const char *remote_address,
-                               int request_id, bt_gatt_server_h server,
-                               bt_gatt_h gatt_handle, bool response_needed, int offset,
-                               const char *value, int len, void *user_data)
-{
-
-       BT_INFO("[HR] __bt_gatt_server_read_value_requested_cb");
-       BT_INFO("[HR] remote_address %s", remote_address);
-       BT_INFO("[HR] req_id %d", request_id);
-       BT_INFO("[HR] server %s", (char *)server);
-       BT_INFO("[HR] gatt_handle %s", (char *)gatt_handle);
-       BT_INFO("[HR] Offset %d", offset);
-
-#if TODO
-       else {
-               /* Send Cached response*/
-               bt_gatt_server_send_response(request_id,
-                       BLUETOOTH_GATT_ATT_REQUEST_TYPE_WRITE, offset,
-                       resp_status, char_value_1, 3 - offset);
-       }
-#endif
-}
-
 static void __bt_hrp_notification_send_cb(int result,
        const char *remote_address, bt_gatt_server_h server,
        bt_gatt_h characteristic, bool completed, void *user_data)
@@ -335,15 +313,6 @@ static int __bt_hrp_create_primary_service(void)
                return error_code;
        }
 
-       error_code = bt_gatt_server_set_write_value_requested_cb(hrp_server_info_s.hrs_descriptor,
-                                       __bt_hrp_sensor_write_value_requested_cb, NULL);
-
-       if (error_code != BT_ERROR_NONE) {
-               BT_ERR("[HR]bt_gatt_server_set_read_value_requested_cb : %s\n", _bt_convert_error_to_string(error_code));
-               return error_code;
-       }
-
-
        BT_INFO("[HR] PRIMARY SERVICE BSL\n");
 
        if (hrp_server_info_s.body_sensor_location != BT_BSL_NOTSUPPORTED) {
@@ -898,29 +867,45 @@ static void _bt_hrp_collector_characteristic_value_changed_cb(bt_hrp_collector_h
 {
 
        BT_INFO("_bt_hrp_collector_characteristic_value_changed_cb Called with value %s", value);
-       bt_hrp_collector_s *collector_s = (bt_hrp_collector_s *)characteristic;
+       bt_hrp_collector_s *collector_s = NULL;
+       GSList *l;
+
+       for (l = hrp_collector_list; l; l = g_slist_next(l)) {
+               if (((bt_hrp_collector_s *)l->data)->hr_chr_notify_h == characteristic) {
+                       collector_s = (bt_hrp_collector_s *)l->data;
+                       break;
+               }
+       }
 
        /*To do :Extract value in to uint and send*/
-       unsigned short hr_value = 1;
+       unsigned short hr_value = *value;
 
-       if (collector_s->char_update_cb)
+       if (collector_s && collector_s->char_update_cb)
                collector_s->char_update_cb(characteristic, hr_value, user_data);
 
 }
 static void _bt_hrp_collector_bsl_read_completed_cb (int result,       bt_hrp_collector_h collector, void *user_data)
 {
        BT_INFO();
-       bt_hrp_collector_s *collector_s = (bt_hrp_collector_s *)collector;
+       bt_hrp_collector_s *collector_s = NULL;
        char *value = NULL;
        int len = 0;
+       GSList *l;
 
-       int error_code = bt_gatt_get_value(collector_s->bsl_char_handle, &value, &len);
+       int error_code = bt_gatt_get_value(collector, &value, &len);
        if (error_code != BT_ERROR_NONE) {
                BT_ERR("bt_gatt_get_value is failed : %d", error_code);
                return;
        }
 
-       if (collector_s->bsl_read_cb)
+       for (l = hrp_collector_list; l; l = g_slist_next(l)) {
+               if (((bt_hrp_collector_s *)l->data)->bsl_chr_h == collector) {
+                       collector_s = (bt_hrp_collector_s *)l->data;
+                       break;
+               }
+       }
+
+       if (collector_s && collector_s->bsl_read_cb)
                collector_s->bsl_read_cb(result, collector, (bt_body_sensor_location_e) *value, user_data);
 
        g_free(value);
@@ -946,20 +931,15 @@ static bt_hrp_collector_s *_bt_hrp_collector_find(const char *remote_address)
 
 static void __bt_hrp_reset_collector_data(bt_hrp_collector_s *collector)
 {
-
-       g_free((collector)->remote_address);
-       (collector)->remote_address = NULL;
-
-       (collector)->connected = false;
-
-       (collector)->connection_callback = NULL;
-
-       (collector)->conn_cb_user_data = NULL;
-
-       g_free(collector->hrs_service_handle);
+       collector->remote_address = NULL;
+       collector->connected = false;
+       collector->connection_callback = NULL;
+       collector->conn_cb_user_data = NULL;
        collector->hrs_service_handle = NULL;
-
        collector->cccd_enabled = false;
+       collector->client = NULL;
+       collector->bsl_chr_h = NULL;
+       collector->hr_chr_notify_h = NULL;
 }
 
 int bt_hrp_collector_create(const char *remote_address, bt_hrp_collector_h *collector)
@@ -974,11 +954,6 @@ int bt_hrp_collector_create(const char *remote_address, bt_hrp_collector_h *coll
        if (_bt_hrp_collector_find(remote_address) != NULL)
                return BT_ERROR_ALREADY_DONE;
 
-       error_code = bt_gatt_client_create(remote_address, collector);
-       if (error_code != BT_ERROR_NONE) {
-               BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code), error_code);
-               return error_code;
-       }
 
        collector_s = g_malloc0(sizeof(*collector_s));
        if (collector_s == NULL) {
@@ -989,6 +964,13 @@ int bt_hrp_collector_create(const char *remote_address, bt_hrp_collector_h *coll
 
        __bt_hrp_reset_collector_data(collector_s);
 
+       error_code = bt_gatt_client_create(remote_address, &collector_s->client);
+       if (error_code != BT_ERROR_NONE) {
+               BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error_code), error_code);
+               g_free(collector_s);
+               return error_code;
+       }
+
        collector_s->remote_address = g_strdup(remote_address);
        if (collector_s->remote_address == NULL) {
                g_free(collector_s);
@@ -997,9 +979,8 @@ int bt_hrp_collector_create(const char *remote_address, bt_hrp_collector_h *coll
                return error_code;
        }
 
+       *collector = (bt_hrp_collector_h)collector_s;
        hrp_collector_list = g_slist_append(hrp_collector_list, collector_s);
-       BT_INFO("bt_hrp_collector_create Exit");
-
        return error_code;
 }
 
@@ -1010,6 +991,9 @@ void bt_hrp_collector_destory(bt_hrp_collector_h collector)
 
        if (collector_s) {
                BT_DBG("bt_hrp_collector_destory destroy Remote Provider [%s]", collector_s->remote_address);
+
+               bt_gatt_client_destroy(collector_s->client);
+
                __bt_hrp_reset_collector_data(collector_s);
 
                hrp_collector_list = g_slist_remove(hrp_collector_list, collector_s);
@@ -1162,7 +1146,8 @@ int bt_hrp_collector_set_notification(bt_hrp_collector_h collector,
        bt_hrp_collector_s *collector_s = (bt_hrp_collector_s *)collector;
        bt_gatt_h svc = NULL;
        bt_gatt_h chr = NULL;
-
+       bt_gatt_h desc = NULL;
+       char *buf = (char *)&isNotify;
 
        BT_CHECK_HRC_SUPPORT();
        BT_CHECK_INIT_STATUS();
@@ -1171,29 +1156,43 @@ int bt_hrp_collector_set_notification(bt_hrp_collector_h collector,
        if (_bt_hrp_collector_find(collector_s->remote_address) == NULL)
                return BT_ERROR_NOT_INITIALIZED;
 
-
-       error_code = bt_gatt_client_get_service(collector, HEART_RATE_MEASUREMENT_UUID, &svc);
+       error_code = bt_gatt_client_get_service(collector_s->client, HEART_RATE_UUID, &svc);
        if (error_code != BT_ERROR_NONE) {
                BT_ERR("bt_gatt_client_get_service is failed : %d", error_code);
                return error_code;
        }
 
-       error_code = bt_gatt_service_get_characteristic(svc, HEART_RATE_CHARAC_CFG_UUID, &chr);
+       error_code = bt_gatt_service_get_characteristic(svc, HEART_RATE_MEASUREMENT_UUID, &chr);
+       if (error_code != BT_ERROR_NONE) {
+               BT_ERR("bt_gatt_service_get_characteristic is failed : %d", error_code);
+               return error_code;
+       }
+
+       error_code = bt_gatt_characteristic_get_descriptor(chr, HEART_RATE_CHARAC_CFG_UUID, &desc);
        if (error_code != BT_ERROR_NONE) {
                BT_ERR("bt_gatt_service_get_characteristic is failed : %d", error_code);
                return error_code;
        }
 
+       bt_gatt_set_value(desc, buf, 2);
+
        collector_s->char_update_cb = callback;
 
        if (isNotify == true) {
+
                error_code = bt_gatt_client_set_characteristic_value_changed_cb(chr, _bt_hrp_collector_characteristic_value_changed_cb, NULL);
                if (error_code != BT_ERROR_NONE)
                        BT_ERR("bt_gatt_client_set_characteristic_value_changed_cb is failed : %d", error_code);
+
+               collector_s->hr_chr_notify_h = chr;
        } else {
+
                error_code = bt_gatt_client_unset_characteristic_value_changed_cb(chr);
+
                if (error_code != BT_ERROR_NONE)
                        BT_ERR("bt_gatt_client_set_characteristic_value_changed_cb is failed : %d", error_code);
+
+               collector_s->hr_chr_notify_h = NULL;
        }
 
        return error_code;
@@ -1215,7 +1214,7 @@ bt_body_sensor_location_e bt_hrp_get_body_sensor_location(bt_hrp_collector_h col
        if (_bt_hrp_collector_find(collector_s->remote_address) == NULL)
                return BT_ERROR_NOT_INITIALIZED;
 
-       error_code = bt_gatt_client_get_service(collector, HEART_RATE_UUID, &svc);
+       error_code = bt_gatt_client_get_service(collector_s->client, HEART_RATE_UUID, &svc);
        if (error_code != BT_ERROR_NONE) {
                BT_ERR("bt_gatt_client_get_service is failed : %d", error_code);
                return error_code;
@@ -1228,7 +1227,7 @@ bt_body_sensor_location_e bt_hrp_get_body_sensor_location(bt_hrp_collector_h col
        }
 
        collector_s->bsl_read_cb = bsl_read_cb;
-       collector_s->bsl_char_handle = chr;
+       collector_s->bsl_chr_h = chr;
 
        //read bsl char
        error_code = bt_gatt_client_read_value(chr, _bt_hrp_collector_bsl_read_completed_cb, NULL);