4 * Copyright (c) 2016-2017 Samsung Electronics Co., Ltd.
6 * Contact: Anupam Roy (anupam.r@samsung.com)
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
23 #include "bt-service-common.h"
24 #include "bt-service-core-adapter.h"
25 #include "bt-service-event-receiver.h"
26 #include "bt-request-handler.h"
27 #include "bluetooth-api.h"
29 #include "bluetooth-api.h"
30 #include "bt-internal-types.h"
31 #include "bt-service-util.h"
32 #include "bt-service-common.h"
33 #include "bt-service-event.h"
35 #include "bt-internal-types.h"
36 #include "bt-service-gatt.h"
37 #include "bt-service-gatt.h"
38 #include "bt-service-core-device.h"
39 #include "bt-service-core-adapter-le.h"
41 #include <oal-hardware.h>
42 #include <oal-manager.h>
43 #include <oal-event.h>
44 #include <oal-adapter-mgr.h>
45 #include <oal-device-mgr.h>
48 #define BT_GATT_TRANSPORT_LE 0
49 #define BT_GATT_TRANSPORT_BR_EDR 1
50 #define BT_GATT_TRANSPORT_LE_BR_EDR 2
51 #define BT_UUID_STRING_MAX 64
52 #define BT_SENDER_MAX_LENGTH 50
53 #define MAX_APPS_SUPPORTED 11 /* Slot 0 is not used */
55 #define UUID_MAX_LEN 50
58 #define BDADDR_ANY (&(bluetooth_device_address_t) {{0, 0, 0, 0, 0, 0} })
60 char uuid_list[NUM_UUID][BT_UUID_STRING_MAX] = {"0000b00b-0000-0000-f065-080080fa49b5", /* Used by BLEAPP */
61 "0000b00b-1111-1111-0123-456789ab0cd2", /* Used by BLEAPP */
62 "0000b00b-2222-1111-0123-456789ab0cd2",
63 "0000b00b-3333-1111-0123-456789ab0cd2",
64 "0000b00b-4444-1111-0123-456789ab0cd2",
65 "0000b00b-5555-1111-0123-456789ab0cd2",
66 "0000b00b-6666-1111-0123-456789ab0cd2",
67 "0000b00b-7777-1111-0123-456789ab0cd2",
68 "0000b00b-8888-1111-0123-456789ab0cd2",
69 "0000b00b-9999-1111-0123-456789ab0cd2"};
72 /* App Information structure */
75 char sender[BT_SENDER_MAX_LENGTH];
76 char uuid[BT_UUID_STRING_MAX];
79 bluetooth_advertising_data_t adv_data; /* Will store adv data for specific slot */
81 bluetooth_scan_resp_data_t scan_rsp; /* Will store scan rsp data for specific slot */
83 gboolean is_initialized;
84 GSList *service_handles;
85 } bt_service_app_info_t;
87 /* GATT Server Request Info Structure */
88 struct gatt_server_req_info {
89 int connection_id; /* This value will uniquely identify a GATT client-server connection */
90 int request_id; /* This is an unique transaction ID assigned against each request by stack */
91 int attribute_handle; /* GATT server attribute handle */
92 int offset; /* GATT server attribute offset on which request is invoked by GATT client */
93 bluetooth_gatt_att_request_type_e request_type; /* Read or Write request */
94 char *addr; /* Remote GATT client address */
97 /* GATT Client Info List Structure */
98 struct gatt_client_info_t {
99 int connection_id; /* This value will uniquely identify a GATT client-server connection */
100 int instance_id; /* This value unique identifies a GATT server instance */
101 char *addr; /* Remote GATT client address */
104 /* GATT Indicate confirm result */
105 struct gatt_indicate_cfm_result_info_t {
106 int result; /* Result of event */
107 char *addr; /* Remote GATT client address */
108 int att_hdl; /* Characteristic Attribute handle */
109 int completed; /* 1 if last event, otheriwse 0 */
112 /* Request Search Utility method */
113 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
114 bluetooth_gatt_att_request_type_e req_type);
116 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
117 bluetooth_gatt_server_indication_params_t *param);
119 static void __bt_remove_all_service_handles(int instance_id);
121 static int __bt_do_unregister_server_instance(int server_instance);
123 /* Linked List of GATT requests from Remote GATT Clients */
124 static GSList *gatt_server_requests = NULL;
126 /* Linked List of connected Remote GATT clients */
127 static GSList *gatt_client_info_list = NULL;
129 /* Number of clients to be notified to */
130 static int num_indicate_clients;
132 /* List of applications */
133 static bt_service_app_info_t numapps[MAX_APPS_SUPPORTED];
135 static void __bt_gatt_handle_pending_request_info(int result,
136 int service_function, void *data, unsigned int size);
138 static void __bt_handle_server_instance_registered(event_gatts_register_t *data);
140 static void __bt_gatt_event_handler(int event_type, gpointer event_data);
142 void _bt_check_adv_app_termination(const char *name)
144 bt_service_app_info_t *app = NULL;
146 int apps[MAX_APPS_SUPPORTED];
148 ret_if(NULL == name);
150 memset(&apps, -1, sizeof(apps));
152 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
155 /* Search for a app which has same sender and stop adv is running */
156 if (!strncasecmp(app->sender, name, strlen(name)) && app->is_initialized == TRUE) {
157 BT_DBG("Match found, name: %s", name);
159 /* Unregister all service handles with stack */
160 __bt_remove_all_service_handles(app->instance_id);
162 /* If Advertising is enabled, stop it */
163 if (app->adv_handle > 0) {
164 BT_INFO("Stop advertising on instance ID [%d]", app->instance_id);
165 /* Disable adv if running */
166 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
167 app->adv_handle, name);
168 _bt_set_advertising(app->sender, app->adv_handle, FALSE, FALSE);
171 /* Save instances of all apps that need to be unregistered */
172 apps[app->instance_id] = 1;
176 /* Unregister all apps one by one */
177 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
179 BT_INFO("Unregister app[%d]", k);
180 /* Unregister server instance */
181 __bt_do_unregister_server_instance(k);
186 static int __bt_gatt_send_indication_to_all_connected_clients(bluetooth_gatt_att_data_t *data,
187 bluetooth_gatt_server_indication_params_t *param)
190 int ret = OAL_STATUS_SUCCESS;
191 int result = OAL_STATUS_INTERNAL_ERROR;
193 BT_INFO("Current total number of connected clients [%d]", g_slist_length(gatt_client_info_list));
194 for (l = gatt_client_info_list; l != NULL; l = l->next) {
195 struct gatt_client_info_t *info = l->data;
198 BT_INFO("GATT Remote client address [%s] connection Id [%d]", info->addr, info->connection_id);
200 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
201 info->connection_id, data->length,
202 param->need_confirmation, (char *)(&data->data[0]));
204 BT_INFO("Send Indication to GATT client [%s] result: [%d]", info->addr, ret);
205 if (ret == OAL_STATUS_SUCCESS) {
206 BT_INFO("Send Indication sent successfully to GATT client [%s]", info->addr);
208 num_indicate_clients++;
212 BT_INFO("Indication sending done for total number of clients [%d]", num_indicate_clients);
216 static struct gatt_server_req_info *__bt_gatt_server_find_request_info(int request_id,
217 bluetooth_gatt_att_request_type_e req_type)
221 for (l = gatt_server_requests; l != NULL; l = l->next) {
222 struct gatt_server_req_info *req_info = l->data;
224 if (req_info && req_info->request_id == request_id && req_info->request_type == req_type) {
225 BT_INFO("GATT Server request info found Req ID [%d] handle [%d] conn ID [%d]",
226 req_info->request_id, req_info->attribute_handle, req_info->connection_id);
230 BT_ERR("Gatt Request not found");
234 void _bt_get_adv_handle_from_instance(int server_inst, int *adv_handle)
238 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
239 if (numapps[k].is_initialized == 1 && numapps[k].instance_id == server_inst) {
240 *adv_handle = numapps[k].adv_handle;
246 int _bt_gatt_init(void)
250 if (OAL_STATUS_SUCCESS != gatt_enable()) {
251 BT_ERR("gatt Init failed");
252 return BLUETOOTH_ERROR_INTERNAL;
255 /* Register gatt event handler */
256 _bt_service_register_event_handler_callback(BT_GATT_MODULE, __bt_gatt_event_handler);
258 return BLUETOOTH_ERROR_NONE;
261 static void __bt_service_reset_gatt_data()
265 BT_INFO("Rest numapp");
268 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
269 numapps[k].is_initialized = 0;
270 numapps[k].instance_id = -1;
271 numapps[k].adv_handle = 0;
272 numapps[k].adv_instance = -1;
273 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
274 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
275 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
276 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
277 numapps[k].adv_data_len = 0;
278 numapps[k].scan_rsp_len = 0;
282 void _bt_gatt_deinit(void)
284 BT_INFO("GATT deinit");
286 if (OAL_STATUS_SUCCESS != gatt_disable())
287 BT_ERR("gatt deinit failed");
289 /* Un-register gatt event handler */
290 _bt_service_unregister_event_handler_callback(BT_GATT_MODULE);
292 __bt_service_reset_gatt_data();
295 void _bt_update_adv_handle(const char *sender, int adv_handle)
298 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
299 bt_service_app_info_t *info = NULL;
301 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
303 /* Search for a app which has same sender and adv handle as 0 */
304 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0)
305 info->adv_handle = adv_handle;
309 int _bt_is_sender_gatt_server_with_no_adv(const char *sender, int adv_handle)
312 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
313 bt_service_app_info_t *info = NULL;
315 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
317 /* Search for a app which has same sender and adv handle as 0
318 It is possible that same sender but different adv handle */
319 if (!g_strcmp0(info->sender, sender) && info->adv_handle == 0) {
320 //info->adv_handle = adv_handle;
321 return info->instance_id;
327 int _bt_get_allocated_server_instance(const char *sender, int adv_handle, gboolean use_reserved_slot)
330 BT_DBG("Sender [%s] Adv handle [%d]", sender, adv_handle);
331 bt_service_app_info_t *info = NULL;
333 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
336 /* Exact matching of Adv handle + sender combination */
337 if (!g_strcmp0(info->sender, sender) && info->adv_handle == adv_handle)
338 return info->instance_id;
340 if (!g_strcmp0(info->sender, sender) && info->adv_handle == -1)
341 return info->instance_id;
347 char * _bt_get_sender_and_handle(int server_instance, int *adv_handle)
350 bt_service_app_info_t *info = NULL;
352 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
355 if (info->instance_id == server_instance && info->adv_handle != -1) {
356 *adv_handle = info->adv_handle;
357 BT_DBG("Server instance [%d] Adv handle [%d] Sender [%s]", server_instance, *adv_handle, info->sender);
358 return g_strdup(info->sender);
364 void _bt_set_new_adv_data(bluetooth_advertising_data_t *adv, int len, int instance)
368 bt_service_app_info_t *info = NULL;
369 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
372 if (info->instance_id == instance) {
373 memcpy(info->adv_data.data, &adv->data, len);
380 void _bt_set_new_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int len, int instance)
384 bt_service_app_info_t *info = NULL;
385 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
388 if (info->instance_id == instance) {
389 memcpy(info->scan_rsp.data, &scan->data, len);
396 void _bt_get_previous_adv_data(bluetooth_advertising_data_t *adv, int *len, int instance)
400 bt_service_app_info_t *info = NULL;
402 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
405 if (info->instance_id == instance) {
406 memcpy(&adv->data, info->adv_data.data, info->adv_data_len);
407 *len = info->adv_data_len;
413 void _bt_get_previous_scan_rsp_data(bluetooth_scan_resp_data_t *scan, int *len, int instance)
417 bt_service_app_info_t *info = NULL;
419 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
422 if (info->instance_id == instance) {
423 memcpy(&scan->data, info->scan_rsp.data, info->scan_rsp_len);
424 *len = info->scan_rsp_len;
430 static int __bt_do_unregister_server_instance(int server_instance)
432 int ret = OAL_STATUS_SUCCESS;
435 /* Unregister the server instance */
436 ret = gatts_unregister(server_instance);
437 if (ret != OAL_STATUS_SUCCESS) {
438 BT_ERR("DeAllocate server instance with stack Fail ret: %d", ret);
439 return BLUETOOTH_ERROR_INTERNAL;
441 BT_INFO("DeAllocated server instance with stack successful..");
444 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
445 if (numapps[k].instance_id == server_instance) {
446 numapps[k].is_initialized = 0;
447 numapps[k].instance_id = -1;
448 numapps[k].adv_handle = 0;
449 numapps[k].adv_instance = -1;
450 memset(numapps[k].sender, 0x00, sizeof(numapps[k].sender));
451 memset(numapps[k].uuid, 0x00, sizeof(numapps[k].uuid));
452 memset(numapps[k].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
453 memset(numapps[k].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
454 numapps[k].adv_data_len = 0;
455 numapps[k].scan_rsp_len = 0;
459 BT_DBG("Going8 to reset numapp block num [%d]", k);
460 return BLUETOOTH_ERROR_NONE;
463 static void __bt_remove_all_service_handles(int instance_id)
467 int ret = OAL_STATUS_SUCCESS;
470 count = g_slist_length(numapps[instance_id].service_handles);
471 BT_INFO("Before handle removal: current count [%d]", count);
473 for (l = numapps[instance_id].service_handles; l != NULL;) {
478 BT_INFO("Server Handle to be Removed [%d] Instance ID [%d]", *handle, instance_id);
479 if (_bt_gatt_server_stop_service(numapps[instance_id].sender, *handle, instance_id) != BLUETOOTH_ERROR_NONE)
482 ret = gatts_delete_service(instance_id, *handle);
483 if (ret != OAL_STATUS_SUCCESS) {
484 BT_ERR("ret: %d", ret);
487 numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
490 count = g_slist_length(numapps[instance_id].service_handles);
491 BT_INFO("After deleting current count [%d]", count);
497 int _bt_unregister_server_instance(const char *sender, int adv_handle)
499 BT_INFO("Unregister Allocated server instance request Sender [%s] Adv handle [%d]", sender, adv_handle);
500 int result = BLUETOOTH_ERROR_NONE;
501 int apps[MAX_APPS_SUPPORTED];
505 memset(&apps, -1, sizeof(apps));
507 if (adv_handle == 0) {
508 BT_DBG("Its a direct GATT Server app request to unregister");
509 /* Unregister server instance for each app with same sender (case: GATT Server with multiple adv handle) */
510 bt_service_app_info_t *info = NULL;
512 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
515 /* Exact matching of sender */
516 if (!g_strcmp0(info->sender, sender)) {
517 BT_INFO("Unregister GATT server instance [%d]", info->instance_id);
518 /* Unregister all service handles with stack */
519 __bt_remove_all_service_handles(info->instance_id);
521 /* Disable adv if running */
522 BT_INFO("Disable Advertising Adv Handle [%d] sender [%s]",
523 info->adv_handle, sender);
524 _bt_set_advertising(sender, info->adv_handle, FALSE, FALSE);
526 /* Save all instances which need to be unregistered */
527 apps[info->instance_id] = 1;
531 BT_DBG("Its an Internal unregister request by adv application");
532 server_instance = _bt_get_allocated_server_instance(sender, adv_handle, FALSE);
533 BT_DBG("Its an Internal unregister request by adv application: Adv disabled srv instance [%d]", server_instance);
534 if (server_instance == -1) {
535 BT_ERR("No allocated server instance to be removed");
536 return BLUETOOTH_ERROR_INVALID_PARAM;
538 if (!numapps[server_instance].service_handles) {
539 BT_INFO("There are no Service handles with this app, safe to unregister");
540 /* Unregister server instance only if this sender does not have any gatt services in it */
541 result = __bt_do_unregister_server_instance(server_instance);
543 numapps[server_instance].adv_handle = 0;
544 memset(numapps[server_instance].adv_data.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
545 numapps[server_instance].adv_data_len = 0;
546 memset(numapps[server_instance].scan_rsp.data, 0x00, BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX);
547 numapps[server_instance].scan_rsp_len = 0;
552 /* Unregister all apps one by one */
553 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
555 BT_INFO("Unregister app[%d]", k);
556 /* Unregister server instance */
557 __bt_do_unregister_server_instance(k);
564 int _bt_register_server_instance(const char *sender, int adv_handle)
566 int ret = OAL_STATUS_SUCCESS;
567 char *uuid_string = NULL;
572 BT_INFO("###Check on which instance Server instance can be initialized....");
573 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
574 if (numapps[k].is_initialized == 1) {
575 BT_INFO("Instance ID [%d] is already in use..Check next slot", numapps[k].instance_id);
578 BT_INFO("Time to register GATT Server..UUID to be used is [%s] slot [%d]", uuid_list[slot-1], slot);
584 BT_ERR("No Slot if free for GATT Server registration..");
585 return BLUETOOTH_ERROR_REGISTRATION_FAILED;
588 uuid_string = g_malloc0(BT_UUID_STRING_MAX);
589 _bt_string_to_uuid(uuid_list[slot-1], (service_uuid_t*)&uuid);
590 g_strlcpy(uuid_string, uuid_list[slot-1], BT_UUID_STRING_MAX);
591 BT_INFO("Copied UUID string [%s]", uuid_string);
593 /* Register GATT Server */
594 ret = gatts_register(&uuid);
595 if (ret != OAL_STATUS_SUCCESS) {
596 BT_ERR("ret: %d", ret);
598 return BLUETOOTH_ERROR_INTERNAL;
600 BT_INFO("GATT Server registration call successfully accepted by OAL..wait for Instance Initialized event from OAL..");
601 /* Return & wait for GATT Server Instance Initialization event */
602 memset(numapps[slot].sender, 0x00, sizeof(numapps[slot].sender));
603 memset(numapps[slot].uuid, 0x00, sizeof(numapps[slot].uuid));
605 g_strlcpy(numapps[slot].sender, sender, sizeof(numapps[slot].sender));
606 g_strlcpy(numapps[slot].uuid, uuid_string, sizeof(numapps[slot].uuid));
608 numapps[slot].is_initialized = 0; /* Set initialization from app registered callback */
609 numapps[slot].adv_handle = adv_handle;
612 return BLUETOOTH_ERROR_NONE;
616 static void __bt_gatt_handle_pending_request_info(int result,
617 int service_function, void *data, unsigned int size)
621 invocation_info_t *req_info = NULL;
622 ret_if(data == NULL);
625 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
627 if (req_info == NULL || req_info->service_function != service_function)
630 switch (service_function) {
632 case BT_GATT_SERVER_REGISTER: {
633 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
634 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
635 if (!g_strcmp0(req_info->sender, param->sender)) {
636 BT_DBG("GATT Server app found [%s]", req_info->sender);
638 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
639 g_array_append_vals(out_param, ¶m->instance_id, sizeof(int));
640 _bt_service_method_return(req_info->context, out_param, result);
642 g_free(req_info->user_data);
643 _bt_free_info_from_invocation_list(req_info);
644 g_array_free(out_param, TRUE);
648 case BT_GATT_SERVER_START_SERVICE:
649 case BT_GATT_SERVER_DELETE_SERVICE: {
650 bt_service_app_info_t *param = (bt_service_app_info_t*)data;
651 BT_INFO("Sender [%s] Request Sender [%s]", param->sender, req_info->sender);
653 int *saved_instance_id = (int*)req_info->user_data;
654 if (!g_strcmp0(req_info->sender, param->sender) && param->instance_id == *saved_instance_id) {
655 BT_DBG("GATT Server app found [%s] Instance ID [%d] Reply DBUS",
656 req_info->sender, *saved_instance_id);
658 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
659 g_array_append_vals(out_param, &saved_instance_id, sizeof(int));
660 _bt_service_method_return(req_info->context, out_param, result);
662 g_free(req_info->user_data);
663 _bt_free_info_from_invocation_list(req_info);
664 g_array_free(out_param, TRUE);
668 case BT_GATT_SERVER_ADD_SERVICE:
669 case BT_GATT_SERVER_ADD_DESCRIPTOR:
670 case BT_GATT_SERVER_ADD_CHARACTERISTIC: {
671 int *handle = (int*)data;
672 BT_INFO("Characteristic added: Handle [%d]", *handle);
673 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
675 g_array_append_vals(out_param, handle, sizeof(int));
676 _bt_service_method_return(req_info->context, out_param, result);
678 g_free(req_info->user_data);
679 _bt_free_info_from_invocation_list(req_info);
680 g_array_free(out_param, TRUE);
685 case BT_DISCONNECT_LE: {
686 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
688 g_array_append_vals(out_param, (bluetooth_device_address_t*)data,
689 sizeof(bluetooth_device_address_t));
690 _bt_service_method_return(req_info->context, out_param, result);
692 g_free(req_info->user_data);
693 _bt_free_info_from_invocation_list(req_info);
695 g_array_free(out_param, TRUE);
704 static void __bt_handle_server_instance_registered(event_gatts_register_t *data)
706 bt_service_app_info_t *info = NULL;
708 char *uuid_string = g_malloc0(BT_UUID_STRING_MAX);
710 _bt_uuid_to_string(&(data->server_uuid), uuid_string);
711 BT_INFO("Instance ID is Intialized [%d] UUID initialized [%s]", data->server_inst, uuid_string);
713 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
714 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
717 if (g_strcmp0(info->uuid, uuid_string) == 0) {
718 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
719 info->is_initialized = TRUE;
720 info->instance_id = data->server_inst;
721 info->adv_instance = data->server_inst;
722 __bt_gatt_handle_pending_request_info(BLUETOOTH_ERROR_NONE, BT_GATT_SERVER_REGISTER,
723 (void*)info, sizeof(bt_service_app_info_t));
731 static void __bt_handle_gatt_server_service_added(event_gatts_srvc_prm_t *event)
733 int result = BLUETOOTH_ERROR_NONE;
737 bt_service_app_info_t *info = NULL;
740 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
741 _bt_uuid_to_string(&(event->gatt_srvc_id.id.uuid), uuid_str);
742 BT_INFO("GATT Added Service UUID: [%s] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
743 uuid_str, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
745 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
746 result = BLUETOOTH_ERROR_INTERNAL;
747 svc_handle = 0; /* Service handle set to 0 indicates.
748 0 is reserved by BT SIG, cant be used by app*/
750 svc_handle = event->gatt_srvc_stat.servic_hndl;
752 BT_INFO("GATT Added Service Status [%d] Server Ins [%d] Service hdl [%d] Is Primary [%d]\n",
753 event->gatt_srvc_stat.status, event->gatt_srvc_stat.server_inst, svc_handle, event->gatt_srvc_id.is_prmry);
755 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
756 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
759 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
760 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
761 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_SERVICE,
762 (int*)&svc_handle, sizeof(int));
764 /* Add Service Handle */
765 if (svc_handle > 0) {
766 handle = g_malloc0(sizeof(int));
767 *handle = svc_handle;
768 numapps[event->gatt_srvc_stat.server_inst].service_handles = g_slist_append(numapps[event->gatt_srvc_stat.server_inst].service_handles, handle);
769 count = g_slist_length(numapps[event->gatt_srvc_stat.server_inst].service_handles);
770 BT_INFO("Added Service handle [%d] to list..current count [%d]", svc_handle, count);
780 static void __bt_handle_gatt_server_characteristic_added(event_gatts_srvc_charctr_t *event)
782 int result = BLUETOOTH_ERROR_NONE;
784 bt_service_app_info_t *info = NULL;
786 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
788 BT_INFO("GATT Server Char added status [%d]", event->gatt_srvc_stat.status);
789 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
790 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
791 BT_INFO("GATT Add characteristic Status: [%d]", event->gatt_srvc_stat.status);
792 BT_INFO("GATT Service characteristic Handle: [%d]", event->charctr_hndl);
794 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
795 result = BLUETOOTH_ERROR_INTERNAL;
796 char_handle = 0; /* characteristic handle set to 0 indicates.
797 0 is reserved by BT SIG, cant be used by app*/
799 char_handle = event->charctr_hndl;
802 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
803 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
806 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
807 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
808 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_CHARACTERISTIC,
809 (int*)&char_handle, sizeof(int));
815 _bt_uuid_to_string(&(event->charctr_uuid), uuid_str);
816 BT_INFO("GATT Added Characteristic: UUID: [%s]", uuid_str);
822 static void __bt_handle_gatt_server_descriptor_added(event_gatts_srvc_descr_t* event)
824 int result = BLUETOOTH_ERROR_NONE;
826 bt_service_app_info_t *info = NULL;
828 char *uuid_str = g_malloc0(BT_UUID_STRING_MAX);
830 BT_INFO("GATT Server Descriptor added status [%d]", event->gatt_srvc_stat.status);
831 BT_INFO("GATT Server_inst: [%d]", event->gatt_srvc_stat.server_inst);
832 BT_INFO("GATT Service Handle: [%d]", event->gatt_srvc_stat.servic_hndl);
833 BT_INFO("GATT Add Descriptor Status: [%d]", event->gatt_srvc_stat.status);
834 BT_INFO("GATT Service Descriptor Handle: [%d]", event->descrptr_hndl);
836 if (event->gatt_srvc_stat.status != OAL_STATUS_SUCCESS) {
837 result = BLUETOOTH_ERROR_INTERNAL;
838 desc_handle = 0; /* Service handle set to 0 indicates.
839 0 is reserved by BT SIG, cant be used by app*/
841 desc_handle = event->descrptr_hndl;
843 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
844 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
847 if (info->instance_id == event->gatt_srvc_stat.server_inst) {
848 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
849 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_ADD_DESCRIPTOR,
850 (int*)&desc_handle, sizeof(int));
855 _bt_uuid_to_string(&(event->descrptr_uuid), uuid_str);
856 BT_INFO("GATT Added Descriptor: UUID: [%s]", uuid_str);
862 static void __bt_handle_gatt_server_service_started(event_gatts_srvc_t *event)
864 bt_service_app_info_t *info = NULL;
865 int result = BLUETOOTH_ERROR_NONE;
867 BT_INFO("GATT Server Service Started..");
870 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
871 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
872 BT_INFO("GATT Service Started Status: [%d]", event->status);
874 if (event->status != OAL_STATUS_SUCCESS) {
875 BT_ERR("GATT Server Service Start Failed Err: [%d]", event->status);
876 result = BLUETOOTH_ERROR_INTERNAL;
879 /* Check if the just registered Instance ID belongs to requester */
880 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
883 if (info->instance_id == event->server_inst) {
884 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
885 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_START_SERVICE,
886 (void*)info, sizeof(bt_service_app_info_t));
894 static void __bt_handle_gatt_server_service_stopped(event_gatts_srvc_t *event)
896 int result = BLUETOOTH_ERROR_NONE;
897 bt_service_app_info_t *info = NULL;
899 BT_INFO("GATT Server Service Stopped..");
901 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
902 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
903 BT_INFO("GATT Service Stopped Status: [%d]", event->status);
905 if (event->status != OAL_STATUS_SUCCESS) {
906 BT_ERR("GATT Server Service Stop Failed Err: [%d]", event->status);
907 result = BLUETOOTH_ERROR_INTERNAL;
910 /* Check if the just registered Instance ID belongs to requester */
911 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
914 if (info->instance_id == event->server_inst) {
915 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
916 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_STOP_SERVICE,
917 (void*)info, sizeof(bt_service_app_info_t));
925 static void __bt_handle_gatt_server_service_deleted(event_gatts_srvc_t *event)
927 int result = BLUETOOTH_ERROR_NONE;
928 bt_service_app_info_t *info = NULL;
930 BT_INFO("GATT Server Service Deleted..");
932 BT_INFO("GATT Server_inst: [%d]", event->server_inst);
933 BT_INFO("GATT Service Handle: [%d]", event->servic_hndl);
934 BT_INFO("GATT Service Deleted Status: [%d]", event->status);
936 if (event->status != OAL_STATUS_SUCCESS) {
937 BT_ERR("GATT Server Service Delete Failed Err: [%d]", event->status);
938 result = BLUETOOTH_ERROR_INTERNAL;
941 /* Check if the just registered Instance ID belongs to requester */
942 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
945 if (info->instance_id == event->server_inst) {
946 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
947 __bt_gatt_handle_pending_request_info(result, BT_GATT_SERVER_DELETE_SERVICE,
948 (void*)info, sizeof(bt_service_app_info_t));
956 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info(char *address)
959 struct gatt_client_info_t *info = NULL;
960 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
961 info = (struct gatt_client_info_t*)l->data;
965 if (!g_strcmp0(info->addr, address)) {
966 BT_INFO("Remote GATT client found addr[%s]", info->addr);
973 static struct gatt_client_info_t *__bt_find_remote_gatt_client_info_from_conn_id(int conn_id)
976 struct gatt_client_info_t *info = NULL;
978 for (l = gatt_client_info_list; l != NULL; l = g_slist_next(l)) {
979 info = (struct gatt_client_info_t*)l->data;
983 if (info->connection_id == conn_id) {
984 BT_INFO("Remote GATT client found addr[%s]", info->addr);
991 static void __bt_handle_gatt_server_connection_state(event_gatts_conn_t *event)
993 int result = BLUETOOTH_ERROR_NONE;
994 struct gatt_client_info_t *conn_info = NULL;
995 bt_service_app_info_t *info = NULL;
996 bluetooth_device_address_t dev_addr;
997 GVariant *param = NULL;
1000 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1002 memcpy(dev_addr.addr, event->address.addr, 6);
1005 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1006 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1009 if (info->instance_id == event->server_inst) {
1010 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1011 __bt_gatt_handle_pending_request_info(result, BT_CONNECT_LE,
1012 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1018 /* Send event to BT-API */
1019 _bt_convert_addr_type_to_string(address,
1020 (unsigned char *)dev_addr.addr);
1022 BT_INFO("GATT Server Connedted: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1023 address, event->server_inst, event->conn_id);
1026 /* Check if device is already in connected list */
1027 conn_info = __bt_find_remote_gatt_client_info(address);
1030 param = g_variant_new("(is)", result, address);
1031 /* Send event to application */
1032 _bt_send_event(BT_GATT_SERVER_EVENT,
1033 BLUETOOTH_EVENT_GATT_CONNECTED,
1036 /* Save Connection info */
1037 conn_info = g_new0(struct gatt_client_info_t, 1);
1038 conn_info->addr = g_strdup(address);
1039 BT_INFO("Added GATT client addr[%s]", conn_info->addr);
1040 conn_info->connection_id = event->conn_id;
1041 conn_info->instance_id = event->server_inst;
1042 gatt_client_info_list = g_slist_append(gatt_client_info_list, conn_info);
1043 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1049 /* GATT Server Dis connected */
1050 static void __bt_handle_gatt_server_disconnection_state(event_gatts_conn_t *event)
1052 int result = BLUETOOTH_ERROR_NONE;
1053 struct gatt_client_info_t *conn_info = NULL;
1054 bluetooth_device_address_t dev_addr;
1055 bt_service_app_info_t *info = NULL;
1056 GVariant *param = NULL;
1059 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1061 memcpy(dev_addr.addr, event->address.addr, 6);
1063 /* Check if the just registered Instance ID belongs to BLEAPP or GATT Server */
1064 for (k = 1; k < MAX_APPS_SUPPORTED; k++) {
1067 if (info->instance_id == event->server_inst) {
1068 BT_INFO("Found GATT Server.. UUID [%s], sender [%s]", info->uuid, info->sender);
1069 __bt_gatt_handle_pending_request_info(result, BT_DISCONNECT_LE,
1070 (bluetooth_device_address_t*)&dev_addr, sizeof(bluetooth_device_address_t));
1076 /* Send event to BT-API */
1077 _bt_convert_addr_type_to_string(address,
1078 (unsigned char *)dev_addr.addr);
1080 BT_INFO("GATT Server DisConnected: Remote Client addr[%s] Server Instance [%d] Connection ID [%d]",
1081 address, event->server_inst, event->conn_id);
1083 param = g_variant_new("(is)", result, address);
1084 /* Send event to application */
1085 _bt_send_event(BT_GATT_SERVER_EVENT,
1086 BLUETOOTH_EVENT_GATT_DISCONNECTED,
1089 /* Remove Connection info */
1090 conn_info = __bt_find_remote_gatt_client_info(address);
1092 BT_INFO("Remove GATT client info from List..");
1093 /* Remove info from List */
1094 gatt_client_info_list = g_slist_remove(gatt_client_info_list, conn_info);
1095 BT_INFO("Total num of connected GATT clients [%d]", g_slist_length(gatt_client_info_list));
1096 g_free(conn_info->addr);
1103 static void __bt_handle_gatt_server_write_requested(event_gatts_srvc_write_attr_t *event)
1106 bluetooth_device_address_t dev_addr;
1107 GVariant *param = NULL;
1108 int result = BLUETOOTH_ERROR_NONE;
1110 gboolean is_prepare_write;
1112 GVariant *data = NULL;
1114 struct gatt_server_req_info *req_info = NULL;
1115 BT_INFO("GATT Server Write Requested");
1117 memcpy(dev_addr.addr, event->address.addr, 6);
1119 BT_INFO("GATT Server Write Req Connection ID: [%d]", event->attr_trans.conn_id);
1120 BT_INFO("GATT Server Write Req Transaction ID:[%d]", event->attr_trans.trans_id);
1121 BT_INFO("GATT Server Write Req Attribute Handle: [%d]", event->attr_trans.attr_handle);
1122 BT_INFO("GATT Server Write Req Attribute Offset: [%d]", event->attr_trans.offset);
1123 BT_INFO("GATT Server Write Req value length [%d]", event->length);
1124 BT_INFO("GATT Server Write Req needs response: [%d]", event->need_rsp);
1125 BT_INFO("GATT Server Write Req Is Prep: [%d]", event->is_prep);
1127 if (event->length <= 0) {
1128 BT_INFO("GATT Server write requested, but length of attr value is 0");
1133 need_resp = event->need_rsp;
1134 is_prepare_write = event->is_prep;
1136 write_val = g_memdup(&event->value[0], event->length);
1138 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1139 _bt_convert_addr_type_to_string(address,
1140 (unsigned char *)dev_addr.addr);
1142 BT_INFO("GATT Server Write Request from remote client [%s]", address);
1144 if (event->length > 0) {
1145 for (i = 0; i < event->length; i++)
1146 BT_DBG("Data[%d] = [0x%x]", i, event->value[i]);
1148 /* Save Write Request Info */
1149 req_info = g_new0(struct gatt_server_req_info, 1);
1150 req_info->request_id = event->attr_trans.trans_id;
1151 req_info->attribute_handle = event->attr_trans.attr_handle;
1152 req_info->connection_id = event->attr_trans.conn_id;
1153 req_info->addr = address;
1154 req_info->offset = event->attr_trans.offset;
1155 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_WRITE;
1156 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1158 data = g_variant_new_from_data(
1159 G_VARIANT_TYPE_BYTESTRING,
1164 param = g_variant_new("(iiiiiibbsn@ay)", result,
1165 event->attr_trans.conn_id,
1166 event->attr_trans.trans_id,
1167 event->attr_trans.attr_handle,
1168 event->attr_trans.offset,
1176 _bt_send_event(BT_GATT_SERVER_EVENT,
1177 BLUETOOTH_EVENT_GATT_SERVER_VALUE_CHANGED,
1184 static void __bt_handle_gatt_server_read_requested(event_gatts_srvc_read_attr_t *event)
1186 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1187 bluetooth_device_address_t dev_addr;
1188 int result = BLUETOOTH_ERROR_NONE;
1189 struct gatt_server_req_info *req_info = NULL;
1190 GVariant *param = NULL;
1192 BT_INFO("GATT Server Read Requested");
1194 memcpy(dev_addr.addr, event->address.addr, 6);
1196 BT_INFO("GATT Server-Client Connection ID: [%d]", event->attr_trans.conn_id);
1197 BT_INFO("GATT Server-Client Transaction ID: [%d]", event->attr_trans.trans_id);
1198 BT_INFO("GATT Server Attribute Handle: [%d]", event->attr_trans.attr_handle);
1199 BT_INFO("GATT Server Attribute Offset for read: [%d]", event->attr_trans.offset);
1200 BT_INFO("GATT Server Attribute is long: [%d]", event->is_long);
1202 is_long = event->is_long;
1204 /* Save Read Request Info */
1205 req_info = g_new0(struct gatt_server_req_info, 1);
1206 req_info->request_id = event->attr_trans.trans_id;
1207 req_info->attribute_handle = event->attr_trans.attr_handle;
1208 req_info->connection_id = event->attr_trans.conn_id;
1209 req_info->addr = address;
1210 req_info->offset = event->attr_trans.offset;
1211 req_info->request_type = BLUETOOTH_GATT_REQUEST_TYPE_READ;
1212 gatt_server_requests = g_slist_append(gatt_server_requests, req_info);
1214 /* Send event to BT-API */
1215 _bt_convert_addr_type_to_string(address,
1216 (unsigned char *)dev_addr.addr);
1218 BT_INFO("GATT Server Read Request from remote client [%s]", address);
1220 param = g_variant_new("(iiiiibs)", result,
1221 event->attr_trans.conn_id,
1222 event->attr_trans.trans_id,
1223 event->attr_trans.attr_handle,
1224 event->attr_trans.offset,
1228 _bt_send_event(BT_GATT_SERVER_EVENT,
1229 BLUETOOTH_EVENT_GATT_SERVER_READ_REQUESTED,
1235 static void __bt_handle_gatt_server_indicate_confirmed(event_gatts_ind_cnfrm_t *event)
1237 bluetooth_device_address_t dev_addr;
1238 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1239 int cur_connected_clients;
1240 static int recvd = 0;
1241 gboolean completed = 0;
1242 GVariant *param = NULL;
1244 /* OAL event does provide error, so MW assumes event will never contain wrong data,
1245 incase of any issues, check with OAL */
1246 int result = BLUETOOTH_ERROR_NONE;
1248 memcpy(dev_addr.addr, event->address.addr, 6);
1249 _bt_convert_addr_type_to_string(address,
1250 (unsigned char *)dev_addr.addr);
1252 BT_INFO("Indication sent to GATT client [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d]",
1253 address, event->conn_id, event->trans_id, event->attr_handle);
1256 cur_connected_clients = g_slist_length(gatt_client_info_list);
1257 BT_INFO("Number of connected clients during sending Indication [%d] & current connected count [%d]",
1258 num_indicate_clients, cur_connected_clients);
1261 if (recvd == num_indicate_clients) {
1262 BT_INFO("Gatt indication confirm event for last GATT client.. [%s]", address);
1263 completed = 1; /* Last event */
1264 recvd = 0; /* Reset */
1265 num_indicate_clients = 0;
1268 param = g_variant_new("(isib)",
1274 /* Send event to BT-API */
1275 _bt_send_event(BT_GATT_SERVER_EVENT,
1276 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_COMPLETED,
1279 BT_INFO("Received Indication confirm for client number [%d]", recvd);
1284 /* Tizen Platform Specific */
1285 static void __bt_handle_gatt_server_notification_changed(event_gatts_notif_t *event)
1287 bluetooth_device_address_t dev_addr;
1288 char *address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1289 GVariant *param = NULL;
1292 /* OAL event does provide error, so MW assumes event will never contain wrong data,
1293 incase of any issues, check with OAL */
1294 int result = BLUETOOTH_ERROR_NONE;
1296 memcpy(dev_addr.addr, event->address.addr, 6);
1297 _bt_convert_addr_type_to_string(address,
1298 (unsigned char *)dev_addr.addr);
1300 BT_INFO("notification_changed [%s] conn_ ID [%d] transaction ID [%d] Att handle [%d] Notify[%d]",
1301 address, event->conn_id, event->trans_id, event->attr_handle, event->notify);
1303 /* Set Notifcation status */
1304 notify = event->notify;
1306 param = g_variant_new("(isib)",
1312 /* Send event to BT-API */
1313 _bt_send_event(BT_GATT_SERVER_EVENT,
1314 BLUETOOTH_EVENT_GATT_SERVER_NOTIFICATION_STATE_CHANGED,
1321 static void __bt_handle_gatt_mtu_changed_event(event_gatts_mtu_changed_t *event)
1323 int result = BLUETOOTH_ERROR_NONE;
1324 struct gatt_client_info_t *conn_info = NULL;
1325 GVariant *param = NULL;
1327 BT_INFO("GATT Server MTU changed event");
1329 conn_info = __bt_find_remote_gatt_client_info_from_conn_id(event->conn_id);
1330 if (conn_info == NULL) {
1331 BT_ERR("Cant find connection Information");
1335 BT_INFO("Got connection Info GATT client [%s] MTU Size [%d]",
1336 conn_info->addr, event->mtu_size);
1338 param = g_variant_new("(isqy)",
1344 /* Send event to BT-API */
1345 _bt_send_event(BT_GATT_SERVER_EVENT,
1346 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
1351 static void __bt_gatt_event_handler(int event_type, gpointer event_data)
1353 BT_INFO("OAL event = 0x%x, \n", event_type);
1354 switch (event_type) {
1355 case OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED: {
1356 BT_INFO("OAL Event: Server Instance Registered");
1357 event_gatts_register_t* event = g_memdup(event_data, sizeof(event_gatts_register_t));
1358 /* GATT Server Registered event is handled in MAIN thread context */
1359 __bt_handle_server_instance_registered(event);
1362 case OAL_EVENT_GATTS_SERVICE_ADDED: {
1363 BT_INFO("OAL Event: GATT Service added");
1364 event_gatts_srvc_prm_t *service_parm = g_memdup(event_data, sizeof(event_gatts_srvc_prm_t));
1365 __bt_handle_gatt_server_service_added(service_parm);
1368 case OAL_EVENT_GATTS_CHARACTERISTIC_ADDED: {
1369 BT_INFO("OAL Event: GATT characteristic added");
1370 event_gatts_srvc_charctr_t *char_parm = g_memdup(event_data, sizeof(event_gatts_srvc_charctr_t));
1371 __bt_handle_gatt_server_characteristic_added(char_parm);
1374 case OAL_EVENT_GATTS_DESCRIPTOR_ADDED: {
1375 BT_INFO("OAL Event: GATT descriptor added");
1376 event_gatts_srvc_descr_t *desc_parm = g_memdup(event_data, sizeof(event_gatts_srvc_descr_t));
1377 __bt_handle_gatt_server_descriptor_added(desc_parm);
1380 case OAL_EVENT_GATTS_SERVICE_STARTED: {
1381 BT_INFO("OAL Event: GATT Service started");
1382 event_gatts_srvc_t *svc_started = g_memdup(event_data, sizeof(event_gatts_srvc_t));
1383 __bt_handle_gatt_server_service_started(svc_started);
1386 case OAL_EVENT_GATTS_SERVICE_STOPED: {
1387 BT_INFO("OAL Event: GATT Service stopped");
1388 event_gatts_srvc_t *svc_stopped = g_memdup(event_data, sizeof(event_gatts_srvc_t));
1389 __bt_handle_gatt_server_service_stopped(svc_stopped);
1392 case OAL_EVENT_GATTS_SERVICE_DELETED: {
1393 BT_INFO("OAL Event: GATT Service deleted");
1394 event_gatts_srvc_t *svc_deleted = g_memdup(event_data, sizeof(event_gatts_srvc_t));
1395 __bt_handle_gatt_server_service_deleted(svc_deleted);
1398 case OAL_EVENT_GATTS_CONNECTION_COMPLETED: {
1399 BT_INFO("OAL Event: GATT Server Connected");
1400 event_gatts_conn_t* event = g_memdup(event_data, sizeof(event_gatts_conn_t));
1401 __bt_handle_gatt_server_connection_state(event);
1404 case OAL_EVENT_GATTS_DISCONNECTION_COMPLETED: {
1405 BT_INFO("OAL Event: GATT Server Disconnected");
1406 event_gatts_conn_t* event = g_memdup(event_data, sizeof(event_gatts_conn_t));
1407 __bt_handle_gatt_server_disconnection_state(event);
1410 case OAL_EVENT_GATTS_REQUEST_READ: {
1411 BT_INFO("OAL Event: GATT Server Read Request");
1412 event_gatts_srvc_read_attr_t *read_req = g_memdup(event_data, sizeof(event_gatts_srvc_read_attr_t));
1413 __bt_handle_gatt_server_read_requested(read_req);
1416 case OAL_EVENT_GATTS_REQUEST_WRITE: {
1417 BT_INFO("OAL Event: GATT Server Write Request");
1418 event_gatts_srvc_write_attr_t *write_req = g_memdup(event_data, sizeof(event_gatts_srvc_write_attr_t));
1419 __bt_handle_gatt_server_write_requested(write_req);
1422 case OAL_EVENT_GATTS_IND_CONFIRM: {
1423 BT_INFO("OAL Event: GATT Server Indication confirmed");
1424 event_gatts_ind_cnfrm_t *parm = g_memdup(event_data, sizeof(event_gatts_ind_cnfrm_t));
1425 __bt_handle_gatt_server_indicate_confirmed(parm);
1428 case OAL_EVENT_GATTS_NOTIFICATION: { /* Tizen Platform Specific */
1429 BT_INFO("OAL Event: GATT Server DisConnected");
1430 event_gatts_notif_t* notif = g_memdup(event_data, sizeof(event_gatts_notif_t));
1431 __bt_handle_gatt_server_notification_changed(notif);
1434 case OAL_EVENT_GATTS_MTU_CHANGED: {
1435 BT_INFO("OAL Event: GATT Server MTU changed event callback");
1436 event_gatts_mtu_changed_t *mtu_changed = g_memdup(event_data, sizeof(event_gatts_mtu_changed_t));
1437 __bt_handle_gatt_mtu_changed_event(mtu_changed);
1446 int _bt_gatt_server_add_service(char *sender, int service_type,
1447 int num_handles, char *svc_uuid, int instance_id)
1449 BT_CHECK_PARAMETER(svc_uuid, return);
1450 BT_CHECK_PARAMETER(sender, return);
1451 int ret = OAL_STATUS_SUCCESS;
1453 oal_gatt_srvc_id_t svc_data;
1455 svc_data.is_prmry = service_type;
1456 svc_data.id.inst_id = instance_id;
1458 BT_INFO("Service UUID [%s] Num handles [%d] Instance ID [%d]", svc_uuid, num_handles, instance_id);
1459 _bt_string_to_uuid(svc_uuid, (service_uuid_t*)&svc_data.id.uuid);
1461 ret = gatts_add_service(instance_id, &svc_data, num_handles);
1462 if (ret != OAL_STATUS_SUCCESS) {
1463 BT_ERR("ret: %d", ret);
1464 return BLUETOOTH_ERROR_INTERNAL;
1467 return BLUETOOTH_ERROR_NONE;
1471 int _bt_gatt_server_add_included_service(char *sender, int instance_id,
1472 int service_handle, int included_svc_handle)
1474 BT_CHECK_PARAMETER(sender, return);
1475 int ret = OAL_STATUS_SUCCESS;
1477 ret = gatts_add_included_services(instance_id, service_handle, included_svc_handle);
1478 if (ret != OAL_STATUS_SUCCESS) {
1479 BT_ERR("ret: %d", ret);
1480 return BLUETOOTH_ERROR_INTERNAL;
1482 return BLUETOOTH_ERROR_NONE;
1485 int _bt_gatt_server_add_characteristic(char *sender, char *char_uuid,
1486 bluetooth_gatt_server_attribute_params_t *param)
1488 BT_CHECK_PARAMETER(char_uuid, return);
1489 BT_CHECK_PARAMETER(sender, return);
1490 BT_CHECK_PARAMETER(param, return);
1491 int ret = OAL_STATUS_SUCCESS;
1493 oal_uuid_t uuid = {{0} };
1495 BT_INFO("Char UUID [%s] Instance ID [%d]", char_uuid, param->instance_id);
1496 _bt_string_to_uuid(char_uuid, (service_uuid_t*)&uuid);
1498 BT_INFO("Char permission From API [0x%x]", param->permissions);
1500 ret = gatts_add_characteristics(param->instance_id, param->service_handle, &uuid,
1501 param->properties, (int)param->permissions);
1502 if (ret != OAL_STATUS_SUCCESS) {
1503 BT_ERR("ret: %d", ret);
1504 return BLUETOOTH_ERROR_INTERNAL;
1506 return BLUETOOTH_ERROR_NONE;
1509 int _bt_gatt_server_add_descriptor(char *sender, char *desc_uuid,
1510 bt_gatt_permission_t *param, int service_handle, int instance_id)
1512 BT_CHECK_PARAMETER(desc_uuid, return);
1513 BT_CHECK_PARAMETER(sender, return);
1514 BT_CHECK_PARAMETER(param, return);
1515 int ret = OAL_STATUS_SUCCESS;
1517 oal_uuid_t uuid = {{0} };
1519 BT_INFO("Descriptor UUID [%s] Instance ID [%d] Service handle [%d]",
1520 desc_uuid, service_handle, instance_id);
1522 _bt_string_to_uuid(desc_uuid, (service_uuid_t*)&uuid);
1524 BT_INFO("Descriptor permission From API [0x%x]", *param);
1525 ret = gatts_add_descriptor(instance_id, service_handle, &uuid, (int)*param);
1527 if (ret != OAL_STATUS_SUCCESS) {
1528 BT_ERR("ret: %d", ret);
1529 return BLUETOOTH_ERROR_INTERNAL;
1531 return BLUETOOTH_ERROR_NONE;
1534 int _bt_gatt_server_start_service(char *sender, int service_handle, int instance_id)
1536 BT_CHECK_PARAMETER(sender, return);
1537 int ret = OAL_STATUS_SUCCESS;
1539 ret = gatts_start_service(instance_id, service_handle, BT_GATT_TRANSPORT_LE);
1540 if (ret != OAL_STATUS_SUCCESS) {
1541 BT_ERR("ret: %d", ret);
1542 return BLUETOOTH_ERROR_INTERNAL;
1544 return BLUETOOTH_ERROR_NONE;
1547 int _bt_gatt_server_stop_service(char *sender, int service_handle, int instance_id)
1549 BT_CHECK_PARAMETER(sender, return);
1550 int ret = OAL_STATUS_SUCCESS;
1552 ret = gatts_stop_service(instance_id, service_handle);
1553 if (ret != OAL_STATUS_SUCCESS) {
1554 BT_ERR("ret: %d", ret);
1555 return BLUETOOTH_ERROR_INTERNAL;
1557 return BLUETOOTH_ERROR_NONE;
1560 int _bt_gatt_server_delete_service(char *sender, int service_handle, int instance_id)
1562 BT_CHECK_PARAMETER(sender, return);
1563 int ret = OAL_STATUS_SUCCESS;
1567 ret = gatts_delete_service(instance_id, service_handle);
1568 if (ret != OAL_STATUS_SUCCESS) {
1569 BT_ERR("ret: %d", ret);
1570 return BLUETOOTH_ERROR_INTERNAL;
1573 /* Remove the Service Handle */
1574 for (l = numapps[instance_id].service_handles; l != NULL;) {
1576 l = g_slist_next(l);
1577 if (handle && *handle == service_handle) {
1578 BT_INFO("Remove Service handle [%d]", *handle);
1579 numapps[instance_id].service_handles = g_slist_remove(numapps[instance_id].service_handles, handle);
1585 return BLUETOOTH_ERROR_NONE;
1588 int _bt_gatt_server_send_response(char *sender, bluetooth_gatt_att_data_t *data,
1589 bluetooth_gatt_server_response_params_t *param)
1591 BT_CHECK_PARAMETER(sender, return);
1592 BT_CHECK_PARAMETER(data, return);
1593 BT_CHECK_PARAMETER(param, return);
1594 struct gatt_server_req_info *req_info = NULL;
1595 int ret = OAL_STATUS_SUCCESS;
1597 oal_gatt_response_t response;
1599 BT_INFO("GATT Server Response: Req Type [%d] req_id [%d] status [%d] auth_req [%d] offset[%d] data len[%d]",
1600 param->req_type, param->request_id,
1601 param->response_status, param->auth_req,
1602 data->offset, data->length);
1604 /* Search for matching Request in List */
1605 req_info = __bt_gatt_server_find_request_info(param->request_id, param->req_type);
1607 BT_ERR("GATT Server Req Info not found for current response..return Error");
1608 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1611 memset(&response, 0x00, sizeof(oal_gatt_response_t));
1613 response.handle = req_info->attribute_handle;
1614 response.attr_value.auth_req = param->auth_req;
1615 response.attr_value.handle = req_info->attribute_handle;
1616 response.attr_value.offset = data->offset;
1617 response.attr_value.len = data->length;
1618 memcpy(&response.attr_value.value, &data->data, data->length);
1621 ret = gatts_send_response(req_info->connection_id, param->request_id,
1622 param->response_status, &response);
1624 if (ret != OAL_STATUS_SUCCESS) {
1625 BT_ERR("ret: %d", ret);
1626 return BLUETOOTH_ERROR_INTERNAL;
1629 BT_INFO("GATT Server Response successfully sent");
1630 /* Remove GATT server request from list */
1631 gatt_server_requests = g_slist_remove(gatt_server_requests, req_info);
1632 g_free(req_info->addr);
1634 return BLUETOOTH_ERROR_NONE;
1637 int _bt_gatt_server_send_indication(char *sender, bluetooth_device_address_t *dev_addr,
1638 bluetooth_gatt_att_data_t *data,
1639 bluetooth_gatt_server_indication_params_t *param)
1641 BT_CHECK_PARAMETER(sender, return);
1642 BT_CHECK_PARAMETER(data, return);
1643 BT_CHECK_PARAMETER(param, return);
1645 gboolean all_send = FALSE;
1646 int ret = OAL_STATUS_SUCCESS;
1647 struct gatt_client_info_t *conn;
1649 address = g_malloc0(BT_ADDRESS_STRING_SIZE);
1650 _bt_convert_addr_type_to_string(address, dev_addr->addr);
1652 if (memcmp(dev_addr->addr, BDADDR_ANY, 6) == 0) {
1653 BT_INFO("GATT Server: Send Indication to all connected GATT clients..");
1656 BT_INFO("GATT Server: Send Indication to connected GATT client addr [%s]", address);
1659 /* Attempt to send Notification/Indication to all Connected GATT clients */
1661 ret = __bt_gatt_send_indication_to_all_connected_clients(data, param);
1662 if (ret != OAL_STATUS_SUCCESS) {
1663 BT_ERR("ret: %d", ret);
1665 return BLUETOOTH_ERROR_INTERNAL;
1669 conn = __bt_find_remote_gatt_client_info(address);
1671 ret = gatts_send_indication(param->instance_id, param->atrribute_handle,
1672 conn->connection_id, data->length,
1673 param->need_confirmation, (char *)(&data->data[0]));
1675 if (ret != OAL_STATUS_SUCCESS) {
1676 BT_ERR("ret: %d", ret);
1677 BT_INFO("Indication failed to send to Remote GATT Client [%s]", address);
1679 return BLUETOOTH_ERROR_INTERNAL;
1681 BT_INFO("Indication sent to Remote GATT Client [%s] wait for Notification completed event from OAL", address);
1683 num_indicate_clients = 1;
1684 return BLUETOOTH_ERROR_NONE;
1686 BT_ERR("Remote GATT client [%s] is not connected..Cant send Indication!!", address);
1688 return BLUETOOTH_ERROR_NOT_CONNECTED;
1691 return BLUETOOTH_ERROR_NONE;
1694 int _bt_gatt_server_update_attribute_value(char *sender, int instance_id,
1695 bluetooth_gatt_server_update_value_t *param)
1697 BT_CHECK_PARAMETER(sender, return);
1698 BT_CHECK_PARAMETER(param, return);
1699 int ret = OAL_STATUS_SUCCESS;
1701 oal_gatt_value_t value;
1702 BT_INFO("GATT Server Update value: Instance ID [%d] attr handle [%d] Value len [%d]",
1703 instance_id, param->attribute_handle, param->length);
1706 memset(&value, 0x00, sizeof(oal_gatt_value_t));
1708 value.handle = param->attribute_handle;
1709 value.len = param->length;
1710 memcpy(&value.value, ¶m->data.data, param->length);
1712 ret = gatts_update_att_value(instance_id, &value);
1714 if (ret != OAL_STATUS_SUCCESS) {
1715 BT_ERR("ret: %d", ret);
1716 return BLUETOOTH_ERROR_INTERNAL;
1719 BT_INFO("GATT Server Update Attribute Value successfully");
1720 return BLUETOOTH_ERROR_NONE;
1723 int _bt_get_att_mtu(bluetooth_device_address_t *address,
1726 BT_CHECK_PARAMETER(address, return);
1727 BT_CHECK_PARAMETER(mtu, return);
1728 struct gatt_client_info_t *conn_info = NULL;
1729 char addr[BT_ADDRESS_STRING_SIZE] = { 0 };
1730 int ret = OAL_STATUS_SUCCESS;
1733 _bt_convert_addr_type_to_string(addr, address->addr);
1735 BT_INFO("Get current MTU size for the remote client:DevAddress:[%s]", addr);
1737 conn_info = __bt_find_remote_gatt_client_info(addr);
1739 BT_INFO("GATT Client [%s] is connected, conn Id [%d] Instance ID [%d]",
1740 conn_info->addr, conn_info->connection_id, conn_info->instance_id);
1742 BT_ERR("GATT Client [%s] is not yet connected..");
1743 return BLUETOOTH_ERROR_NOT_CONNECTED;
1746 ret = gatts_get_att_mtu(conn_info->connection_id, &stack_mtu);
1747 if (ret != OAL_STATUS_SUCCESS) {
1748 BT_ERR("ret: %d", ret);
1749 return BLUETOOTH_ERROR_INTERNAL;
1751 BT_INFO("ATT MTU received from OAL [%d]", stack_mtu);
1752 *mtu = (unsigned int)stack_mtu;
1753 return BLUETOOTH_ERROR_NONE;