Change gatt_servers to gslist from array 35/229135/4
authorWootak Jung <wootak.jung@samsung.com>
Fri, 27 Mar 2020 02:02:32 +0000 (11:02 +0900)
committerWootak Jung <wootak.jung@samsung.com>
Mon, 30 Mar 2020 06:36:03 +0000 (06:36 +0000)
Change-Id: I1bd03900a0db5667901ad38b3e526a2d8dbe2b4e

bt-oal/oal-gatt.c

index 45e88fb..a5074b7 100644 (file)
@@ -32,7 +32,6 @@
 #define MAX_PROP_LEN 95 /* SVACE WGID: 324403*/
 #define MAX_PERM_LEN 80 //Fix for svace wgid:324402
 
-#define NUM_SERVER_INST 10 // Maximum slots supported by stack is set to 10
 #define CHECK_OAL_GATT_ENABLED() \
        do { \
                if (gatt_api == NULL) { \
 
 #define CHECK_SERVER_INSTANCE(instance_id) \
        do { \
-               if (instance_id < 1 || instance_id > NUM_SERVER_INST) { \
+               if (instance_id < 1) { \
                        BT_ERR("Invalid Instance"); \
                        return OAL_STATUS_INVALID_PARAM; \
                } \
        } while (0)
 
-#define CHECK_SERVER_REGISTRATION(instance_id) \
+#define CHECK_CORRECT_SERVER_ID(server_if, server_inst) \
        do { \
-               if (gatt_servers[instance_id - 1].server_id < 0)  {\
-                       BT_INFO("GATT server registeration is not done for server Instance : %d", instance_id);\
-                       return OAL_STATUS_INTERNAL_ERROR;\
-               } \
-       } while (0)
-
-#define CHECK_CORRECT_SERVER_ID(instance_id, server_if, server_inst)\
-       do { \
-               for (instance_id = 1 ; instance_id <= NUM_SERVER_INST; instance_id++) { \
-                       if (gatt_servers[instance_id - 1].server_id == server_if) { \
-                               server_inst = instance_id; \
-                               goto sendevent;\
-                       } \
+               gatt_server_t *info; \
+               info = __gatts_find_server_instance(server_if); \
+               if (info) { \
+                       server_inst = server_if; \
+                       goto sendevent; \
                } \
        } while (0)
 
@@ -106,9 +97,10 @@ typedef enum {
 } gatt_data_state;
 
 typedef struct {
-       int server_id;
+       int instance_id;
        oal_uuid_t uuid;
        int state;
+       gboolean cur_adv_state;
 } gatt_server_t;
 
 const char *oal_device_type[] = {
@@ -125,8 +117,7 @@ typedef enum {
 } oal_pending_gattc_req_e;
 
 static const btgatt_interface_t * gatt_api;
-static gboolean cur_adv_state[NUM_SERVER_INST];
-static gatt_server_t gatt_servers[NUM_SERVER_INST];
+static GSList *gatt_servers = NULL; /* List of gatt_server_t */
 
 /* Forward declarations of GATT Server callbacks */
 static void cb_gatts_register_app(int status, int server_if, bt_uuid_t *uuid);
@@ -262,14 +253,6 @@ oal_status_t gatt_enable(void)
 {
        const bt_interface_t * blued_api;
        int ret;
-       int i = 0;
-
-       for (i = 0; i < NUM_SERVER_INST; i++) {
-               gatt_servers[i].server_id = -1;
-               gatt_servers[i].state = GATT_INS_DISABLED;
-               memset(&gatt_servers[i].uuid, 0, sizeof(oal_uuid_t));
-               cur_adv_state[i] = FALSE;
-       }
 
        /* Get stack interface */
        blued_api = (const bt_interface_t *) adapter_get_stack_interface();
@@ -303,37 +286,51 @@ oal_status_t gatt_enable(void)
 
 oal_status_t gatt_disable(void)
 {
-       int i;
        if (gatt_api) {
                gatt_api->cleanup();
                gatt_api = NULL;
        }
 
-       for (i = 0; i < NUM_SERVER_INST; i++) {
-               gatt_servers[i].server_id = -1;
-               gatt_servers[i].state = GATT_INS_DISABLED;
-               memset(&gatt_servers[i].uuid, 0, sizeof(oal_uuid_t));
-       }
+       g_slist_free_full(gatt_servers, g_free);
        return OAL_STATUS_SUCCESS;
 }
 
+static gatt_server_t *__gatts_find_server_instance(int instance_id)
+{
+       gatt_server_t *info;
+       GSList *l;
+
+       for (l = gatt_servers; l; l = g_slist_next(l)) {
+               info = l->data;
+               if (!info)
+                       continue;
+               if (info->instance_id == instance_id)
+                       return info;
+       }
+
+       return NULL;
+}
+
 /************************************GATT Server Functions*************************************/
 /*Public */
 oal_status_t gatts_register(oal_uuid_t* server_uuid)
 {
        char str[2*BT_UUID_STRING_MAX];
        int ret = OAL_STATUS_SUCCESS;
-       int i;
+       GSList *l;
 
        CHECK_OAL_GATT_ENABLED();
        uuid_to_stringname((service_uuid_t*)server_uuid->uuid, str);
        API_TRACE("Register the server instance: UUID: [%s]", str);
 
-       for (i = 0; i < NUM_SERVER_INST; i++) {
-               if (memcmp(server_uuid->uuid, gatt_servers[i].uuid.uuid, sizeof(oal_uuid_t)) == 0) {
-                       if (gatt_servers[i].server_id != -1) {
-                               BT_ERR("This is resevered UUID for easy set up application i = %d \
-                                               gatt_servers[i].server_id = %d", i, gatt_servers[i].server_id);
+       for (l = gatt_servers; l; l = g_slist_next(l)) {
+               gatt_server_t *info = l->data;
+               if (!info)
+                       continue;
+               if (memcmp(server_uuid->uuid, info->uuid.uuid, sizeof(oal_uuid_t)) == 0) {
+                       if (info->instance_id != -1) {
+                               BT_ERR("This is resevered UUID for easy set up application \
+                                               instance_id = %d", info->instance_id);
                                return OAL_STATUS_ALREADY_DONE;
                        }
                }
@@ -351,24 +348,30 @@ oal_status_t gatts_register(oal_uuid_t* server_uuid)
 oal_status_t gatts_unregister(int instance_id)
 {
        int ret = OAL_STATUS_SUCCESS;
+       gatt_server_t *info;
        API_TRACE("Unregister the registered server, instance_id: %d", instance_id);
 
        CHECK_OAL_GATT_ENABLED();
        CHECK_SERVER_INSTANCE(instance_id);
-       CHECK_SERVER_REGISTRATION(instance_id);
 
-       ret = gatt_api->server->unregister_server(gatt_servers[instance_id - 1].server_id);
+       info = __gatts_find_server_instance(instance_id);
+       if (!info)
+               return OAL_STATUS_INTERNAL_ERROR;
 
+       ret = gatt_api->server->unregister_server(instance_id);
        if (ret != BT_STATUS_SUCCESS) {
                BT_ERR("GATT server unregistration failed: %d", instance_id);
                return convert_to_oal_status(ret);
        }
 
-       gatt_servers[instance_id-1].server_id = -1;
-       gatt_servers[instance_id-1].state = GATT_INS_DISABLED;
-
-       if (instance_id != 1 && instance_id != 2)
-               memset(&gatt_servers[instance_id-1].uuid, 0, sizeof(oal_uuid_t));
+       info->instance_id = -1;
+       info->state = GATT_INS_DISABLED;
+       info->cur_adv_state = FALSE;
+       if (instance_id != 1 && instance_id != 2) {
+               gatt_servers = g_slist_remove(gatt_servers, info);
+               BT_DBG("GATT Server Removed. count of gatt_servers: %d",
+                               g_slist_length(gatt_servers));
+       }
 
        return OAL_STATUS_SUCCESS;
 }
@@ -377,17 +380,21 @@ oal_status_t gatts_unregister(int instance_id)
 oal_status_t gatts_start_listen(int instance_id, gboolean enable)
 {
        int ret = OAL_STATUS_SUCCESS;
-       API_TRACE("server-id:%d enable: %d", gatt_servers[instance_id-1].server_id, enable);
+       gatt_server_t *info;
+       API_TRACE("instance_id: %d enable: %d", instance_id, enable);
 
        CHECK_OAL_GATT_ENABLED();
        CHECK_SERVER_INSTANCE(instance_id);
-       CHECK_SERVER_REGISTRATION(instance_id);
-
-       ret = gatt_api->server->listen(gatt_servers[instance_id-1].server_id, enable);
+       info = __gatts_find_server_instance(instance_id);
+       if (!info)
+               return OAL_STATUS_INTERNAL_ERROR;
 
+       ret = gatt_api->server->listen(instance_id, enable);
        if (ret != BT_STATUS_SUCCESS) {
-               BT_ERR("Error:Advertising start/stop on server-id:%d failed With Status: [%s]",
-                               gatt_servers[instance_id-1].server_id, status2string(ret));
+               BT_ERR("Error:Advertising %s on instance_id: %d failed \
+                               with status: %s, cur_adv_state: %d",
+                               enable ? "Start" : "Stop", instance_id,
+                               status2string(ret), info->cur_adv_state);
                return convert_to_oal_status(ret);
        }
 
@@ -401,10 +408,10 @@ oal_status_t gatts_stop_advertising(int instance_id)
 
        CHECK_OAL_GATT_ENABLED();
        CHECK_SERVER_INSTANCE(instance_id);
-       CHECK_SERVER_REGISTRATION(instance_id);
-
-       ret = gatt_api->server->listen(gatt_servers[instance_id - 1].server_id, FALSE);
+       if (__gatts_find_server_instance(instance_id) == NULL)
+               return OAL_STATUS_INTERNAL_ERROR;
 
+       ret = gatt_api->server->listen(instance_id, FALSE);
        if (ret != BT_STATUS_SUCCESS) {
                BT_ERR("Stop Advertising failed for server Instance:%d With Status: %s",
                        instance_id, status2string(ret));
@@ -460,12 +467,14 @@ oal_status_t gatts_set_scan_response(int instance_id, uint16_t manufac_len, char
 oal_status_t gatts_multi_adv_enable(int instance_id)
 {
        int ret = OAL_STATUS_SUCCESS;
-       API_TRACE("Start Multi advertising, srv_if: %d, instance_id: %d",
-                       gatt_servers[instance_id - 1].server_id, instance_id);
+       gatt_server_t *info;
+       API_TRACE("Start Multi advertising, instance_id: %d", instance_id);
 
        CHECK_OAL_GATT_ENABLED();
        CHECK_SERVER_INSTANCE(instance_id);
-       CHECK_SERVER_REGISTRATION(instance_id);
+       info = __gatts_find_server_instance(instance_id);
+       if (!info)
+               return OAL_STATUS_INTERNAL_ERROR;
 
        if (gatt_api->server->multi_adv_enable == NULL)
                return OAL_STATUS_NOT_SUPPORT;
@@ -477,22 +486,17 @@ oal_status_t gatts_multi_adv_enable(int instance_id)
                return OAL_STATUS_BUSY;
        }
 */
-       if (gatt_servers[instance_id - 1].state == GATT_INS_ENABLED) {
-               BT_ERR("Not allowed, state: %d, instance_id: %d",
-                               gatt_servers[instance_id - 1].state, instance_id);
-               return OAL_STATUS_ALREADY_DONE;
-       }
 
-       if (gatt_servers[instance_id - 1].state == GATT_INS_ENABLING) {
-               BT_ERR("Not allowed, state: %d, instance_id: %d",
-                               gatt_servers[instance_id - 1].state, instance_id);
+       if (info->state == GATT_INS_ENABLED) {
+               BT_ERR("Not allowed, state: %d, instance_id: %d", info->state, instance_id);
+               return OAL_STATUS_ALREADY_DONE;
+       } else if (info->state == GATT_INS_ENABLING) {
+               BT_ERR("Not allowed, state: %d, instance_id: %d", info->state, instance_id);
                return OAL_STATUS_BUSY;
        }
+       info->state = GATT_INS_ENABLING;
 
-       gatt_servers[instance_id - 1].state = GATT_INS_ENABLING;
-
-       ret = gatt_api->server->multi_adv_enable(gatt_servers[instance_id - 1].server_id);
-
+       ret = gatt_api->server->multi_adv_enable(instance_id);
        if (ret != BT_STATUS_SUCCESS) {
                BT_ERR("GATT enable multi advertising failed for server Instance: %d With Status: %s",
                                instance_id, status2string(ret));
@@ -505,24 +509,24 @@ oal_status_t gatts_multi_adv_enable(int instance_id)
 oal_status_t gatts_multi_adv_disable(int instance_id)
 {
        int ret = OAL_STATUS_SUCCESS;
-       API_TRACE("Stop Multi advertising, srv_if: %d, instance_id: %d",
-                       gatt_servers[instance_id - 1].server_id, instance_id);
+       gatt_server_t *info;
+       API_TRACE("Stop Multi advertising, instance_id: %d", instance_id);
 
        CHECK_OAL_GATT_ENABLED();
        CHECK_SERVER_INSTANCE(instance_id);
-       CHECK_SERVER_REGISTRATION(instance_id);
+       info = __gatts_find_server_instance(instance_id);
+       if (!info)
+               return OAL_STATUS_INTERNAL_ERROR;
 
        if (gatt_api->server->multi_adv_disable == NULL)
                return OAL_STATUS_NOT_SUPPORT;
 
-       if ((gatt_servers[instance_id - 1].state != GATT_INS_ENABLED)) {
-               BT_ERR("Not Allowed, state: %d, instance_id: %d",
-                       gatt_servers[instance_id - 1].state, instance_id);
+       if (info->state != GATT_INS_ENABLED) {
+               BT_ERR("Not Allowed, state: %d, instance_id: %d", info->state, instance_id);
                return OAL_STATUS_BUSY;
        }
 
-       ret = gatt_api->server->multi_adv_disable(gatt_servers[instance_id - 1].server_id);
-
+       ret = gatt_api->server->multi_adv_disable(instance_id);
        if (ret != BT_STATUS_SUCCESS) {
                BT_ERR("GATT disable multi advertising failed for server Instance: %d With Status: %s",
                                instance_id, status2string(ret));
@@ -558,16 +562,16 @@ oal_status_t gatts_multi_adv_update(int instance_id,
 
        CHECK_OAL_GATT_ENABLED();
        CHECK_SERVER_INSTANCE(instance_id);
-       CHECK_SERVER_REGISTRATION(instance_id);
+       if (__gatts_find_server_instance(instance_id) == NULL)
+               return OAL_STATUS_INTERNAL_ERROR;
 
        if (gatt_api->server->multi_adv_update == NULL)
                return OAL_STATUS_NOT_SUPPORT;
 
-       ret = gatt_api->server->multi_adv_update(gatt_servers[instance_id - 1].server_id,
+       ret = gatt_api->server->multi_adv_update(instance_id,
                        min_intv, max_intv,
                        adv_type, chnl_map,
                        tx_power, timeout_s);
-
        if (ret != BT_STATUS_SUCCESS) {
                BT_ERR("GATT update multi advertising failed for server Instance: %d With Status: %s",
                                instance_id, status2string(ret));
@@ -581,23 +585,23 @@ oal_status_t gatts_multi_adv_set_inst_data(int instance_id,
 {
        int ret = OAL_STATUS_SUCCESS;
        btgatt_adv_param_setup_t adv_setup;
-       API_TRACE("Set Multi advertising data, srv_if: %d, instance_id: %d",
-                       gatt_servers[instance_id - 1].server_id, instance_id);
+       gatt_server_t *info;
+       API_TRACE("Set Multi advertising data, instance_id: %d", instance_id);
 
        CHECK_OAL_GATT_ENABLED();
        CHECK_SERVER_INSTANCE(instance_id);
-       CHECK_SERVER_REGISTRATION(instance_id);
+       info = __gatts_find_server_instance(instance_id);
+       if (!info)
+               return OAL_STATUS_INTERNAL_ERROR;
 
        if (gatt_api->server->multi_adv_set_inst_data == NULL)
                return OAL_STATUS_NOT_SUPPORT;
 
-       if ((gatt_servers[instance_id - 1].state >= GATT_INS_DATA_SETTING) && (gatt_servers[instance_id - 1].state != GATT_INS_DATA_SET)) {
-               BT_ERR("Not Allowed, state: %d, instance_id: %d", gatt_servers[instance_id - 1].state, instance_id);
+       if (info->state >= GATT_INS_DATA_SETTING && info->state != GATT_INS_DATA_SET) {
+               BT_ERR("Not Allowed, state: %d, instance_id: %d", info->state, instance_id);
                return OAL_STATUS_BUSY;
        }
-
-       gatt_servers[instance_id - 1].state = GATT_INS_DATA_SETTING;
-
+       info->state = GATT_INS_DATA_SETTING;
 
        adv_setup.set_scan_rsp = adv_param_setup->set_scan_rsp;
        adv_setup.include_name = adv_param_setup->include_name;
@@ -620,12 +624,12 @@ oal_status_t gatts_multi_adv_set_inst_data(int instance_id,
        adv_setup.tx_power = adv_param_setup->tx_power;
        adv_setup.timeout_s = adv_param_setup->timeout_s;
 
-       adv_setup.server_if = gatt_servers[instance_id - 1].server_id;
+       adv_setup.server_if = instance_id;
 
        ret = gatt_api->server->multi_adv_set_inst_data(adv_setup);
        if (ret != BT_STATUS_SUCCESS) {
                BT_ERR("GATT Set Multi advertising data failed: %s", status2string(ret));
-               gatt_servers[instance_id - 1].state = GATT_INS_DISABLED;
+               info->state = GATT_INS_DISABLED;
                return convert_to_oal_status(ret);
        }
        return OAL_STATUS_SUCCESS;
@@ -640,11 +644,12 @@ oal_status_t gatts_disconnect(int instance_id, bt_address_t *device_address, int
 
        CHECK_OAL_GATT_ENABLED();
        CHECK_SERVER_INSTANCE(instance_id);
-       CHECK_SERVER_REGISTRATION(instance_id);
+       if (__gatts_find_server_instance(instance_id) == NULL)
+               return OAL_STATUS_INTERNAL_ERROR;
 
        API_TRACE("[%s]", bdt_bd2str(device_address, &bdstr));
 
-       ret = gatt_api->server->disconnect(gatt_servers[instance_id - 1].server_id, (bt_bdaddr_t *) device_address, conn_id);
+       ret = gatt_api->server->disconnect(instance_id, (bt_bdaddr_t *) device_address, conn_id);
        if (ret != BT_STATUS_SUCCESS) {
                BT_ERR("GATT Server disconnect failed: %s", status2string(ret));
                return convert_to_oal_status(ret);
@@ -657,54 +662,46 @@ oal_status_t gatts_disconnect(int instance_id, bt_address_t *device_address, int
 /*Server Callbacks*/
 static void cb_gatts_register_app(int status, int server_if, bt_uuid_t *uuid)
 {
-       int i;
        char str[2*BT_UUID_STRING_MAX];
        uuid_to_stringname((service_uuid_t*)uuid, str);
 
        event_gatts_register_t *event = g_malloc0(sizeof(event_gatts_register_t));
 
-       BT_INFO("BTGATT SERVER REGISTER APP CB, status:%d, server_if:%d", status, server_if);
-       BT_INFO("UUID: [%s]", str);
+       BT_INFO("BTGATT SERVER REGISTER APP CB, UUID:%s, status:%d, server_if:%d",
+                       str, status, server_if);
 
-       for (i = 0; i < NUM_SERVER_INST; i++) {
-               if (gatt_servers[i].server_id == -1) {
-                       BT_INFO("Server Instance registered with stack [%d]", i+1);
-                       gatt_servers[i].server_id = server_if;
-                       event->server_inst = i+1;
-                       memcpy(gatt_servers[i].uuid.uuid, uuid->uu, sizeof(bt_uuid_t));
-                       memcpy(event->server_uuid.uuid, uuid->uu, sizeof(bt_uuid_t));
-                       break;
-               }
-       }
+       gatt_server_t *info = g_malloc0(sizeof(gatt_server_t));
+       info->instance_id = server_if;
+       memcpy(info->uuid.uuid, uuid->uu, sizeof(bt_uuid_t));
+       gatt_servers = g_slist_append(gatt_servers, info);
+       BT_DBG("GATT Server Added. count of gatt_servers: %d", g_slist_length(gatt_servers));
+
+       event->server_inst = server_if;
+       memcpy(event->server_uuid.uuid, uuid->uu, sizeof(bt_uuid_t));
        send_event(OAL_EVENT_BLE_SERVER_INSTANCE_INITIALISED, event, sizeof(event_gatts_register_t));
 }
 
 static void cb_gatts_listen(int status, int server_if)
 {
-       gboolean prev_state[NUM_SERVER_INST];
-       gboolean new_state;
+       gboolean prev_state;
+       gboolean new_state = FALSE;
        event_ble_adv_status *event = g_malloc0(sizeof(event_ble_adv_status));
-       int instance_id;
-
-       for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
-               if (gatt_servers[instance_id - 1].server_id == server_if) {
-                       event->server_inst = instance_id;
+       GSList *l;
+
+       for (l = gatt_servers; l; l = g_slist_next(l)) {
+               gatt_server_t *info = l->data;
+               if (!info)
+                       continue;
+               if (info->instance_id == server_if) {
+                       event->server_inst = server_if;
+                       prev_state = info->cur_adv_state;
+                       new_state = (status == BT_STATUS_SUCCESS) ? !prev_state : prev_state;
+                       info->cur_adv_state = new_state;
                        break;
-               } else {
-                       BT_WARN("Server ID:%d recevied server Id:%d\n", gatt_servers[instance_id - 1].server_id, server_if);
                }
        }
-       if ((event->server_inst > 0) && (event->server_inst <= 10)) {
-               prev_state[event->server_inst - 1] = cur_adv_state[event->server_inst - 1];
-               new_state = (status == BT_STATUS_SUCCESS) ? !(prev_state[event->server_inst - 1]) : (prev_state[event->server_inst - 1]);
-               cur_adv_state[event->server_inst - 1] = new_state;
-       } else {
-               BT_ERR("Invalid Callback...");
-               g_free(event);
-               return;
-       }
 
-       BT_INFO("Adv State of server instance %d :%d", instance_id, new_state);
+       BT_INFO("Adv State of server instance %d, new_state: %d", server_if, new_state);
 
        if (TRUE == new_state)
                send_event(OAL_EVENT_BLE_ADVERTISING_STARTED, event, sizeof(event_ble_adv_status));
@@ -717,19 +714,21 @@ static void cb_gatts_multi_adv_enable(int server_if, int status)
 
        BT_INFO("BTGATT SERVER MULTI ADV ENABLE CB, status:%d, srv_if: %d", status, server_if);
        event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
-       int instance_id;
+       GSList *l;
 
        if (status != BT_STATUS_SUCCESS)
                BT_ERR("MULTI ADV ENABLE failed, status:%d, srv_if: %d", status, server_if);
 
-       for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
-               if (gatt_servers[instance_id - 1].server_id == server_if) {
-                       event->server_inst = instance_id;
-                       gatt_servers[instance_id - 1].state = GATT_INS_ENABLED;
+       for (l = gatt_servers; l; l = g_slist_next(l)) {
+               gatt_server_t *info = l->data;
+               if (!info)
+                       continue;
+               if (info->instance_id == server_if) {
+                       event->server_inst = server_if;
+                       info->state = GATT_INS_ENABLED;
                        goto sendevent;
                }
        }
-       BT_ERR("Invalid Interface, srv_if: %d", server_if);
        event->server_inst = -1;
 sendevent:
        event->status = convert_to_oal_status(status);
@@ -740,19 +739,21 @@ static void cb_gatts_multi_adv_disable(int server_if, int status)
 {
        BT_INFO("BTGATT SERVER MULTI ADV DISABLE CB, status:%d, srv_if: %d", status, server_if);
        event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
-       int instance_id;
+       GSList *l;
 
        if (status != BT_STATUS_SUCCESS)
                BT_ERR("MULTI ADV DISABLE failed, status:%d, srv_if: %d", status, server_if);
 
-       for (instance_id = 1 ; instance_id <= NUM_SERVER_INST; instance_id++) {
-               if (gatt_servers[instance_id - 1].server_id == server_if) {
-                       event->server_inst = instance_id;
-                       gatt_servers[instance_id - 1].state = GATT_INS_DISABLED;
+       for (l = gatt_servers; l; l = g_slist_next(l)) {
+               gatt_server_t *info = l->data;
+               if (!info)
+                       continue;
+               if (info->instance_id == server_if) {
+                       event->server_inst = server_if;
+                       info->state = GATT_INS_DISABLED;
                        goto sendevent;
                }
        }
-       BT_ERR("Invalid Interface, srv_if: %d", server_if);
        event->server_inst = -1;
 sendevent:
        event->status = convert_to_oal_status(status);
@@ -763,18 +764,20 @@ static void cb_gatts_multi_adv_update(int server_if, int status)
 {
        BT_INFO("BTGATT SERVER MULTI ADV UPDATE CB, status:%d, srv_if: %d", status, server_if);
        event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
-       int instance_id;
+       GSList *l;
 
        if (status != BT_STATUS_SUCCESS)
                BT_ERR("MULTI ADV UPDATE failed, status:%d, srv_if: %d", status, server_if);
 
-       for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
-               if (gatt_servers[instance_id - 1].server_id == server_if) {
-                       event->server_inst = instance_id;
+       for (l = gatt_servers; l; l = g_slist_next(l)) {
+               gatt_server_t *info = l->data;
+               if (!info)
+                       continue;
+               if (info->instance_id == server_if) {
+                       event->server_inst = server_if;
                        goto sendevent;
                }
        }
-       BT_ERR("Invalid Interface, srv_if: %d", server_if);
        event->server_inst = -1;
 sendevent:
        event->status = convert_to_oal_status(status);
@@ -785,19 +788,21 @@ static void cb_gatts_multi_adv_set_inst_data(int server_if, int status)
 {
        BT_INFO("BTGATT SERVER MULTI ADV SET INST DATA CB, status:%d, srv_if: %d", status, server_if);
        event_ble_multiadv_status *event = g_malloc0(sizeof(event_ble_multiadv_status));
-       int instance_id;
+       GSList *l;
 
        if (status != BT_STATUS_SUCCESS)
                BT_ERR("MULTI ADV SET DATA failed, status:%d, srv_if: %d", status, server_if);
 
-       for (instance_id = 1 ; instance_id <= NUM_SERVER_INST ; instance_id++) {
-               if (gatt_servers[instance_id - 1].server_id == server_if) {
-                       event->server_inst = instance_id;
-                       gatt_servers[instance_id - 1].state = GATT_INS_DATA_SET;
+       for (l = gatt_servers; l; l = g_slist_next(l)) {
+               gatt_server_t *info = l->data;
+               if (!info)
+                       continue;
+               if (info->instance_id == server_if) {
+                       event->server_inst = server_if;
+                       info->state = GATT_INS_DATA_SET;
                        goto sendevent;
                }
        }
-       BT_ERR("Invalid Interface, srv_if: %d", server_if);
        event->server_inst = -1;
 sendevent:
        event->status = convert_to_oal_status(status);
@@ -853,7 +858,8 @@ oal_status_t gatts_add_service(int instance_id, oal_gatt_srvc_id_t *gatt_serv_id
        CHECK_OAL_GATT_ENABLED();
 
        CHECK_SERVER_INSTANCE(instance_id);
-       CHECK_SERVER_REGISTRATION(instance_id);
+       if (__gatts_find_server_instance(instance_id) == NULL)
+               return OAL_STATUS_INTERNAL_ERROR;
 
        if (gatt_serv_id != NULL) {
                uuid_to_stringname(&(gatt_serv_id->id.uuid), str);
@@ -866,8 +872,7 @@ oal_status_t gatts_add_service(int instance_id, oal_gatt_srvc_id_t *gatt_serv_id
                return OAL_STATUS_INVALID_PARAM;
        }
 
-       ret = gatt_api->server->add_service(gatt_servers[instance_id - 1].server_id, &btgatt_srvc_id, num_handles);
-
+       ret = gatt_api->server->add_service(instance_id, &btgatt_srvc_id, num_handles);
        if (ret != BT_STATUS_SUCCESS) {
                BT_ERR("GATT Server add service failed: %s", status2string(ret));
                return convert_to_oal_status(ret);
@@ -884,10 +889,10 @@ oal_status_t gatts_add_included_services(int instance_id, int serv_handle,
        CHECK_OAL_GATT_ENABLED();
 
        CHECK_SERVER_INSTANCE(instance_id);
+       if (__gatts_find_server_instance(instance_id) == NULL)
+               return OAL_STATUS_INTERNAL_ERROR;
 
-       CHECK_SERVER_REGISTRATION(instance_id);
-
-       ret = gatt_api->server->add_included_service(gatt_servers[instance_id - 1].server_id, serv_handle, incl_handle);
+       ret = gatt_api->server->add_included_service(instance_id, serv_handle, incl_handle);
        if (ret != BT_STATUS_SUCCESS) {
                BT_ERR("GATT Server Add included Service failed: %s", status2string(ret));
                return convert_to_oal_status(ret);
@@ -908,15 +913,15 @@ oal_status_t gatts_add_characteristics(int instance_id, int serv_handle,
        CHECK_OAL_GATT_ENABLED();
 
        CHECK_SERVER_INSTANCE(instance_id);
-
-       CHECK_SERVER_REGISTRATION(instance_id);
+       if (__gatts_find_server_instance(instance_id) == NULL)
+               return OAL_STATUS_INTERNAL_ERROR;
 
        if (charc_uuid != NULL) {
                uuid_to_stringname(charc_uuid, str);
                API_TRACE("uuid: [%s]", str);
        }
 
-       ret = gatt_api->server->add_characteristic(gatt_servers[instance_id - 1].server_id,
+       ret = gatt_api->server->add_characteristic(instance_id,
                        serv_handle, (bt_uuid_t*)charc_uuid, propts, permsn);
        if (ret != BT_STATUS_SUCCESS) {
                BT_ERR("GATT Server Add Characteristic failed: %s", status2string(ret));
@@ -937,17 +942,16 @@ oal_status_t gatts_add_descriptor(int instance_id, int serv_handle,
        CHECK_OAL_GATT_ENABLED();
 
        CHECK_SERVER_INSTANCE(instance_id);
-
-       CHECK_SERVER_REGISTRATION(instance_id);
+       if (__gatts_find_server_instance(instance_id) == NULL)
+               return OAL_STATUS_INTERNAL_ERROR;
 
        if (desc_uuid != NULL) {
                uuid_to_stringname(desc_uuid, str);
                API_TRACE("uuid: [%s]", str);
        }
 
-       ret = gatt_api->server->add_descriptor(gatt_servers[instance_id - 1].server_id,
+       ret = gatt_api->server->add_descriptor(instance_id,
                                serv_handle, (bt_uuid_t*)desc_uuid, permsn);
-
        if (ret != BT_STATUS_SUCCESS) {
                BT_ERR("GATT Server Add Descriptor failed: %s", status2string(ret));
                return convert_to_oal_status(ret);
@@ -964,11 +968,10 @@ oal_status_t gatts_start_service(int instance_id, int svc_handle, int transport)
        CHECK_OAL_GATT_ENABLED();
 
        CHECK_SERVER_INSTANCE(instance_id);
+       if (__gatts_find_server_instance(instance_id) == NULL)
+               return OAL_STATUS_INTERNAL_ERROR;
 
-       CHECK_SERVER_REGISTRATION(instance_id);
-
-       ret = gatt_api->server->start_service(gatt_servers[instance_id - 1].server_id, svc_handle, transport);
-
+       ret = gatt_api->server->start_service(instance_id, svc_handle, transport);
        if (ret != BT_STATUS_SUCCESS) {
                BT_ERR("GATT Server Start Service failed: %s", status2string(ret));
                return convert_to_oal_status(ret);
@@ -976,17 +979,18 @@ oal_status_t gatts_start_service(int instance_id, int svc_handle, int transport)
        return OAL_STATUS_SUCCESS;
 }
 
-oal_status_t gatts_stop_service(int ins_id, int srv_hdl)
+oal_status_t gatts_stop_service(int instance_id, int srv_hdl)
 {
        int ret = OAL_STATUS_SUCCESS;
 
-       API_TRACE("Server Stop Service : Ins_id = %d, srvHndl = %d, ", ins_id, srv_hdl);
+       API_TRACE("Server Stop Service : Ins_id = %d, srvHndl = %d, ", instance_id, srv_hdl);
        CHECK_OAL_GATT_ENABLED();
 
-       CHECK_SERVER_INSTANCE(ins_id);
-       CHECK_SERVER_REGISTRATION(ins_id);
+       CHECK_SERVER_INSTANCE(instance_id);
+       if (__gatts_find_server_instance(instance_id) == NULL)
+               return OAL_STATUS_INTERNAL_ERROR;
 
-       ret = gatt_api->server->stop_service(gatt_servers[ins_id - 1].server_id, srv_hdl);
+       ret = gatt_api->server->stop_service(instance_id, srv_hdl);
        if (ret != BT_STATUS_SUCCESS) {
                BT_ERR("GATT Server Stop Service failed: %s", status2string(ret));
                return convert_to_oal_status(ret);
@@ -994,18 +998,18 @@ oal_status_t gatts_stop_service(int ins_id, int srv_hdl)
        return OAL_STATUS_SUCCESS;
 }
 
-oal_status_t gatts_delete_service(int ins_id, int srv_hdl)
+oal_status_t gatts_delete_service(int instance_id, int srv_hdl)
 {
        int ret = OAL_STATUS_SUCCESS;
 
-       API_TRACE("Server Delete Service : Ins_id = %d, srvHndl = %d, ", ins_id, srv_hdl);
+       API_TRACE("Server Delete Service : Ins_id = %d, srvHndl = %d, ", instance_id, srv_hdl);
        CHECK_OAL_GATT_ENABLED();
 
-       CHECK_SERVER_INSTANCE(ins_id);
-
-       CHECK_SERVER_REGISTRATION(ins_id);
+       CHECK_SERVER_INSTANCE(instance_id);
+       if (__gatts_find_server_instance(instance_id) == NULL)
+               return OAL_STATUS_INTERNAL_ERROR;
 
-       ret = gatt_api->server->delete_service(gatt_servers[ins_id - 1].server_id, srv_hdl);
+       ret = gatt_api->server->delete_service(instance_id, srv_hdl);
        if (ret != BT_STATUS_SUCCESS) {
                BT_ERR("GATT Server Delete Service failed: %s", status2string(ret));
                return convert_to_oal_status(ret);
@@ -1033,26 +1037,25 @@ oal_status_t gatts_send_response(int conn_id, int trans_id, int resp_status, oal
        return OAL_STATUS_SUCCESS;
 }
 
-oal_status_t gatts_send_indication(int ins_id, int attr_hndl, int conn_id, int len, int confirm, char* value)
+oal_status_t gatts_send_indication(int instance_id, int attr_hndl, int conn_id, int len, int confirm, char* value)
 {
        int ret = OAL_STATUS_SUCCESS;
 
        API_TRACE("Server Send Indication : Ins_id = %d, srvHndl = %d, [%s]",
-                       ins_id, attr_hndl, confirm == TRUE ? "INDICATION" : "NOTIFICATION");
+                       instance_id, attr_hndl, confirm == TRUE ? "INDICATION" : "NOTIFICATION");
        CHECK_OAL_GATT_ENABLED();
 
-       CHECK_SERVER_INSTANCE(ins_id);
-
-       CHECK_SERVER_REGISTRATION(ins_id);
+       CHECK_SERVER_INSTANCE(instance_id);
+       if (__gatts_find_server_instance(instance_id) == NULL)
+               return OAL_STATUS_INTERNAL_ERROR;
 
        if (value == NULL || len == 0) {
                BT_ERR("GATT Server attribute value is empty");
                return OAL_STATUS_INVALID_PARAM;
        }
 
-       ret = gatt_api->server->send_indication(gatt_servers[ins_id - 1].server_id,
+       ret = gatt_api->server->send_indication(instance_id,
                        attr_hndl, conn_id, len, confirm, value);
-
        if (ret != BT_STATUS_SUCCESS) {
                BT_ERR("GATT Server Send Indication failed: %s", status2string(ret));
                return convert_to_oal_status(ret);
@@ -1060,15 +1063,15 @@ oal_status_t gatts_send_indication(int ins_id, int attr_hndl, int conn_id, int l
        return OAL_STATUS_SUCCESS;
 }
 
-oal_status_t gatts_update_att_value(int ins_id, oal_gatt_value_t *value)
+oal_status_t gatts_update_att_value(int instance_id, oal_gatt_value_t *value)
 {
        int ret = OAL_STATUS_SUCCESS;
 
        CHECK_OAL_GATT_ENABLED();
 
-       CHECK_SERVER_INSTANCE(ins_id);
-
-       CHECK_SERVER_REGISTRATION(ins_id);
+       CHECK_SERVER_INSTANCE(instance_id);
+       if (__gatts_find_server_instance(instance_id) == NULL)
+               return OAL_STATUS_INTERNAL_ERROR;
 
        if (value == NULL || value->len == 0) {
                BT_ERR("GATT Server attribute value is empty");
@@ -1076,11 +1079,10 @@ oal_status_t gatts_update_att_value(int ins_id, oal_gatt_value_t *value)
        }
 
        API_TRACE("Server Update Value: Ins_id = [%d], att handle = [%d] data len [%d]",
-                       ins_id, value->handle, value->len);
+                       instance_id, value->handle, value->len);
 
-       ret = gatt_api->server->update_att_value(gatt_servers[ins_id - 1].server_id,
+       ret = gatt_api->server->update_att_value(instance_id,
                        (int)value->handle, (int)value->len, (char*)value->value);
-
        if (ret != BT_STATUS_SUCCESS) {
                BT_ERR("GATT Server Update Attribute Value failed: [%s]", status2string(ret));
                return convert_to_oal_status(ret);
@@ -1092,12 +1094,10 @@ oal_status_t gatts_update_att_value(int ins_id, oal_gatt_value_t *value)
 static void cb_gatts_service_added(int status, int server_if,
                btgatt_srvc_id_t *psrvc_id, int srvc_handle)
 {
-       int instance_id;
-
        BT_INFO("BTGATT SERVER SERVICE ADDED CB, status:%d, handle:%d", status, srvc_handle);
        event_gatts_srvc_prm_t* event = g_new0(event_gatts_srvc_prm_t, 1);
 
-       CHECK_CORRECT_SERVER_ID(instance_id, server_if, event->gatt_srvc_stat.server_inst);
+       CHECK_CORRECT_SERVER_ID(server_if, event->gatt_srvc_stat.server_inst);
 
        BT_ERR("Invalid Interface, srv_if: %d", server_if);
        event->gatt_srvc_stat.server_inst = -1;
@@ -1119,13 +1119,12 @@ static void cb_gatts_included_service_added(int status, int server_if,
                int srvc_handle,
                int incl_srvc_handle)
 {
-       int ins_id ;
        BT_INFO("BTGATT SERVER INCLUDED SERVICE ADDED CB, status:%d, srvc handle:%d, incl handle:%d",
                        status, srvc_handle, incl_srvc_handle);
 
        event_gatts_incld_srvc_t* event = g_new0(event_gatts_incld_srvc_t, 1);
 
-       CHECK_CORRECT_SERVER_ID(ins_id, server_if, event->gatt_srvc_stat.server_inst);
+       CHECK_CORRECT_SERVER_ID(server_if, event->gatt_srvc_stat.server_inst);
 
        BT_ERR("Invalid Interface, srv_if: %d", server_if);
        event->gatt_srvc_stat.server_inst = -1;
@@ -1139,14 +1138,12 @@ sendevent:
 static void cb_gatts_characteristic_added(int status, int server_if, bt_uuid_t *char_id,
                int srvc_handle, int char_handle)
 {
-       int Ins_id ;
-
        BT_INFO("BTGATT SERVER CHARACTERISTIC ADDED CB, status:%d, srvc handle:%d, char handle:%d",
                        status, srvc_handle, char_handle);
 
        event_gatts_srvc_charctr_t* event = g_new0(event_gatts_srvc_charctr_t, 1);
 
-       CHECK_CORRECT_SERVER_ID(Ins_id, server_if, event->gatt_srvc_stat.server_inst);
+       CHECK_CORRECT_SERVER_ID(server_if, event->gatt_srvc_stat.server_inst);
 
        BT_ERR("Invalid Interface, srv_if: %d", server_if);
        event->gatt_srvc_stat.server_inst = -1;
@@ -1162,13 +1159,12 @@ static void cb_gatts_descriptor_added(int status, int server_if,
                bt_uuid_t *descr_id, int srvc_handle,
                int descr_handle)
 {
-       int Ins_id ;
        BT_INFO("BTGATT SERVER DESCRIPTOR ADDED CB, status:%d, srvc handle:%d, desc handle:%d",
                        status, srvc_handle, descr_handle);
 
        event_gatts_srvc_descr_t* event = g_new0(event_gatts_srvc_descr_t, 1);
 
-       CHECK_CORRECT_SERVER_ID(Ins_id, server_if, event->gatt_srvc_stat.server_inst);
+       CHECK_CORRECT_SERVER_ID(server_if, event->gatt_srvc_stat.server_inst);
 
        BT_ERR("Invalid Interface, srv_if: %d", server_if);
        event->gatt_srvc_stat.server_inst = -1;
@@ -1182,12 +1178,11 @@ sendevent:
 
 static void cb_gatts_service_started(int status, int server_if, int srvc_handle)
 {
-       int Ins_id ;
        BT_INFO("BTGATT SERVER SERVICE STARTED CB, status:%d, srvc_handle:%d", status, srvc_handle);
 
        event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
 
-       CHECK_CORRECT_SERVER_ID(Ins_id, server_if, event->server_inst);
+       CHECK_CORRECT_SERVER_ID(server_if, event->server_inst);
 
        BT_ERR("Invalid Interface, srv_if: %d", server_if);
        event->server_inst = -1;
@@ -1199,12 +1194,11 @@ sendevent:
 
 static void cb_gatts_service_stopped(int status, int server_if, int srvc_handle)
 {
-       int ins_id ;
        BT_INFO("BTGATT SERVER SERVICE STOPPED CB, status:%d, srvc_handle:%d", status, srvc_handle);
 
        event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
 
-       CHECK_CORRECT_SERVER_ID(ins_id, server_if, event->server_inst);
+       CHECK_CORRECT_SERVER_ID(server_if, event->server_inst);
 
        BT_ERR("Invalid Interface, srv_if: %d", server_if);
        event->server_inst = -1;
@@ -1216,12 +1210,11 @@ sendevent:
 
 static void cb_gatts_service_deleted(int status, int server_if, int srvc_handle)
 {
-       int ins_id;
        BT_INFO("BTGATT SERVER SERVICE DELETED CB, status:%d, srvc_handle:%d", status, srvc_handle);
 
        event_gatts_srvc_t* event = g_new0(event_gatts_srvc_t, 1);
 
-       CHECK_CORRECT_SERVER_ID(ins_id, server_if, event->server_inst);
+       CHECK_CORRECT_SERVER_ID(server_if, event->server_inst);
 
        BT_ERR("Invalid Interface, srv_if: %d", server_if);
        event->server_inst = -1;
@@ -1233,26 +1226,22 @@ sendevent:
 
 static void cb_gatts_connection(int conn_id, int server_if, int connected, bt_bdaddr_t *bda)
 {
-       int ins_id ;
+       GSList *l;
        oal_event_t event_type;
 
        BT_INFO("BTGATT SERVER CONNECTION  connnected:%d, conn_id:%d server_if:%d", connected, conn_id, server_if);
        event_gatts_conn_t *event = g_new0(event_gatts_conn_t, 1);
 
        if (connected == TRUE) {
-               for (ins_id = 1 ; ins_id <= NUM_SERVER_INST ; ins_id++) {
-                       if ((gatt_servers[ins_id - 1].server_id == server_if)) {
-                               event->server_inst = ins_id;
+               for (l = gatt_servers; l; l = g_slist_next(l)) {
+                       gatt_server_t *info = l->data;
+                       if (!info)
+                               continue;
+                       if (info->instance_id == server_if) {
+                               event->server_inst = server_if;
                                break;
-                       } else {
-                               BT_INFO("Server ID:%d recevied server Id:%d\n", gatt_servers[ins_id - 1].server_id, server_if);
                        }
                }
-               if (ins_id == NUM_SERVER_INST+1) {
-                       BT_WARN("Incorrect Server Interface received: %d\n",  server_if);
-                       g_free(event);
-                       return;
-               }
        } else {
                /* server_inst is not required in disconnected case */
        }
@@ -2462,7 +2451,7 @@ oal_status_t gattc_configure_mtu(int conn_id, int mtu)
        return OAL_STATUS_SUCCESS;
 }
 
-oal_status_t gattc_add_connection_info(bt_address_t *device_address, int conn_id, int server_inst_id)
+oal_status_t gattc_add_connection_info(bt_address_t *device_address, int conn_id, int instance_id)
 {
        int ret = OAL_STATUS_SUCCESS;
        bdstr_t bdstr;
@@ -2470,10 +2459,11 @@ oal_status_t gattc_add_connection_info(bt_address_t *device_address, int conn_id
        OAL_CHECK_PARAMETER(device_address, return);
        API_TRACE("Add connection info: [%s]", bdt_bd2str(device_address, &bdstr));
        CHECK_OAL_GATT_ENABLED();
-       CHECK_SERVER_INSTANCE(server_inst_id);
-       CHECK_SERVER_REGISTRATION(server_inst_id);
+       CHECK_SERVER_INSTANCE(instance_id);
+       if (__gatts_find_server_instance(instance_id) == NULL)
+               return OAL_STATUS_INTERNAL_ERROR;
 
-       ret = gatt_api->client->add_connection_info((bt_bdaddr_t *)device_address, conn_id, server_inst_id);
+       ret = gatt_api->client->add_connection_info((bt_bdaddr_t *)device_address, conn_id, instance_id);
        if (ret != BT_STATUS_SUCCESS) {
                BT_ERR("GATT connection info add failed: %s", status2string(ret));
                return convert_to_oal_status(ret);