CAResult_t CADeInitLEGattServer() { OIC_LOG(DEBUG, TAG, "IN"); int ret = bt_gatt_server_unregister_all_services(g_gattServer); if (0 != ret) { OIC_LOG_V(ERROR, TAG, "bt_gatt_server_unregister_all_services failed with ret[%s]", CALEGetErrorMsg(ret)); return CA_STATUS_FAILED; } ret = bt_gatt_server_destroy(g_gattServer); if (0 != ret) { OIC_LOG_V(ERROR, TAG, "bt_gatt_server_destroy failed with ret[%s]", CALEGetErrorMsg(ret)); return CA_STATUS_FAILED; } ret = bt_gatt_server_deinitialize(); if (0 != ret) { OIC_LOG_V(ERROR, TAG, "bt_gatt_server_deinitialize failed with ret[%s]", CALEGetErrorMsg(ret)); return CA_STATUS_FAILED; } OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; }
CAResult_t CARegisterLEServicewithGattServer(const bt_gatt_h svcPath) { OIC_LOG(DEBUG, TAG, "IN"); VERIFY_NON_NULL(svcPath, TAG, "svcPath"); OIC_LOG_V(DEBUG, TAG, "svcPath:%s", svcPath); int ret = bt_gatt_server_register_service(g_gattServer, svcPath); if (0 != ret) { OIC_LOG_V(ERROR, TAG, "bt_gatt_server_register_service failed with ret[%s]", CALEGetErrorMsg(ret)); return CA_STATUS_FAILED; } ret = bt_gatt_server_set_value_changed_cb(g_gattWriteCharPath, CALEGattRemoteCharacteristicWriteCb, NULL); if (0 != ret) { OIC_LOG_V(ERROR, TAG, "bt_gatt_server_set_value_changed_cb failed with ret[%s]", CALEGetErrorMsg(ret)); return CA_STATUS_FAILED; } OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; }
CAResult_t CALEStopAdvertise() { OIC_LOG(DEBUG, TAG, "IN"); if (NULL != g_hAdvertiser) { int ret = bt_adapter_le_stop_advertising(g_hAdvertiser); if (0 != ret) { OIC_LOG_V(ERROR, TAG, "bt_adapter_le_stop_advertising failed with ret[%s]", CALEGetErrorMsg(ret)); } ret = bt_adapter_le_destroy_advertiser(g_hAdvertiser); if (0 != ret) { OIC_LOG_V(ERROR, TAG, "bt_adapter_le_destroy_advertiser failed with ret[%s]", CALEGetErrorMsg(ret)); } g_hAdvertiser = NULL; } else { OIC_LOG(ERROR, TAG, "Advertising is not running"); return CA_STATUS_FAILED; } OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; }
CAResult_t CAInitLEGattServer() { OIC_LOG(DEBUG, TAG, "IN"); int ret = bt_gatt_server_initialize(); if (0 != ret) { OIC_LOG_V(ERROR, TAG, "bt_gatt_server_initialize failed with ret[%s]", CALEGetErrorMsg(ret)); return CA_STATUS_FAILED; } bt_gatt_server_h server; ret = bt_gatt_server_create(&server); if (0 != ret) { OIC_LOG_V(ERROR, TAG, "bt_gatt_server_create failed with ret[%s]", CALEGetErrorMsg(ret)); return CA_STATUS_FAILED; } if (NULL != g_gattServer) { OICFree(g_gattServer); g_gattServer = NULL; } g_gattServer = server; OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; }
CAResult_t CAInitLEGattServer() { OIC_LOG(DEBUG, TAG, "IN"); int ret = bt_gatt_server_initialize(); if (0 != ret) { OIC_LOG_V(ERROR, TAG, "bt_gatt_server_initialize failed with ret[%s]", CALEGetErrorMsg(ret)); return CA_STATUS_FAILED; } if (!g_gattServer) { OIC_LOG(DEBUG, TAG, "g_gattServer is NULL. create gatt server.."); ret = bt_gatt_server_create(&g_gattServer); if (0 != ret) { OIC_LOG_V(ERROR, TAG, "bt_gatt_server_create failed with ret[%s]", CALEGetErrorMsg(ret)); return CA_STATUS_FAILED; } } OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; }
CAResult_t CAUpdateCharacteristicsToAllGattClients(const uint8_t *charValue, uint32_t charValueLen) { OIC_LOG(DEBUG, TAG, "IN"); VERIFY_NON_NULL(charValue, TAG, "charValue"); oc_mutex_lock(g_leCharacteristicMutex); if (!g_LEConnectedState) { OIC_LOG(ERROR, TAG, "g_LEConnectedState is false"); oc_mutex_unlock(g_leCharacteristicMutex); return CA_STATUS_FAILED; } if (NULL == g_gattReadCharPath) { OIC_LOG(ERROR, TAG, "g_gattReadCharPath is NULL"); oc_mutex_unlock(g_leCharacteristicMutex); return CA_STATUS_FAILED; } int ret = bt_gatt_set_value(g_gattReadCharPath, (char *)charValue, charValueLen); if (0 != ret) { OIC_LOG_V(ERROR, TAG, "bt_gatt_set_value failed with return[%s]", CALEGetErrorMsg(ret)); oc_mutex_unlock(g_leCharacteristicMutex); return CA_STATUS_FAILED; } #ifdef BLE_TIZEN_30 ret = bt_gatt_server_notify_characteristic_changed_value(g_gattReadCharPath, CALEServerNotificationSentCB, NULL, NULL); #else ret = bt_gatt_server_notify(g_gattReadCharPath, false, CALEServerNotificationSentCB, NULL); #endif if (0 != ret) { OIC_LOG_V(ERROR, TAG, #ifdef BLE_TIZEN_30 "bt_gatt_server_notify_characteristic_changed_value failed with return[%s]", #else "bt_gatt_server_notify failed with return[%s]", #endif CALEGetErrorMsg(ret)); oc_mutex_unlock(g_leCharacteristicMutex); return CA_STATUS_FAILED; } oc_mutex_unlock(g_leCharacteristicMutex); OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; }
CAResult_t CAAddNewLEServiceInGattServer(const char *serviceUUID) { OIC_LOG(DEBUG, TAG, "IN"); VERIFY_NON_NULL(serviceUUID, TAG, "serviceUUID"); OIC_LOG_V(DEBUG, TAG, "service uuid %s", serviceUUID); bt_gatt_service_type_e type = BT_GATT_SERVICE_TYPE_PRIMARY; oc_mutex_lock(g_leServiceMutex); int ret = bt_gatt_service_create(serviceUUID, type, &g_gattSvcPath); if (0 != ret) { oc_mutex_unlock(g_leServiceMutex); OIC_LOG_V(ERROR, TAG, "bt_gatt_service_create failed with ret [%s]", CALEGetErrorMsg(ret)); return CA_STATUS_FAILED; } oc_mutex_unlock(g_leServiceMutex); if (g_gattSvcPath) { OIC_LOG_V(DEBUG, TAG, "ServicePath obtained is %s", (char *)g_gattSvcPath); } OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; }
CAResult_t CAUpdateCharacteristicsToGattClient(const char *address, const uint8_t *charValue, uint32_t charValueLen) { OIC_LOG(DEBUG, TAG, "IN"); VERIFY_NON_NULL(charValue, TAG, "charValue"); VERIFY_NON_NULL(address, TAG, "address"); OIC_LOG_V(DEBUG, TAG, "Client's Unicast address for sending data [%s]", address); ca_mutex_lock(g_leCharacteristicMutex); if (NULL == g_gattReadCharPath) { OIC_LOG(ERROR, TAG, "g_gattReadCharPath is NULL"); ca_mutex_unlock(g_leCharacteristicMutex); return CA_STATUS_FAILED; } int ret = bt_gatt_set_value(g_gattReadCharPath, (char *)charValue, charValueLen); if (0 != ret) { OIC_LOG_V(ERROR, TAG, "bt_gatt_set_value failed with return [%s]", CALEGetErrorMsg(ret)); ca_mutex_unlock(g_leCharacteristicMutex); return CA_STATUS_FAILED; } ret = bt_gatt_server_notify(g_gattReadCharPath, false, CALEServerNotificationSentCB, address, NULL); if (0 != ret) { OIC_LOG_V(ERROR, TAG, "bt_gatt_server_notify failed with return [%s]", CALEGetErrorMsg(ret)); ca_mutex_unlock(g_leCharacteristicMutex); return CA_STATUS_FAILED; } ca_mutex_unlock(g_leCharacteristicMutex); OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; }
CAResult_t CALEStartAdvertise(const char *serviceUUID) { OIC_LOG(DEBUG, TAG, "IN"); int res = bt_adapter_le_create_advertiser(&g_hAdvertiser); if (NULL == g_hAdvertiser || BT_ERROR_NONE != res) { OIC_LOG_V(ERROR, TAG, "g_hAdvertiser is NULL/ Result is %d", res); return CA_STATUS_FAILED; } res = bt_adapter_le_add_advertising_service_uuid(g_hAdvertiser, BT_ADAPTER_LE_PACKET_ADVERTISING, serviceUUID); if (BT_ERROR_NONE != res) { OIC_LOG_V(ERROR, TAG, "bt_adapter_le_add_advertising_service_uuid failed with ret[%s]", CALEGetErrorMsg(res)); return CA_STATUS_FAILED; } res = bt_adapter_le_set_advertising_device_name(g_hAdvertiser, BT_ADAPTER_LE_PACKET_SCAN_RESPONSE, true); if (BT_ERROR_NONE != res) { OIC_LOG_V(ERROR, TAG, "bt_adapter_le_set_advertising_device_name failed with ret[%s]", CALEGetErrorMsg(res)); return CA_STATUS_FAILED; } res = bt_adapter_le_start_advertising_new(g_hAdvertiser, NULL, NULL); if (BT_ERROR_NONE != res) { OIC_LOG_V(ERROR, TAG, "bt_adapter_le_start_advertising_new failed with ret[%s]", CALEGetErrorMsg(res)); return CA_STATUS_FAILED; } OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; }
uint16_t CALEServerGetMtuSize(const char* address) { OIC_LOG(DEBUG, TAG, "IN"); VERIFY_NON_NULL_RET(address, TAG, "address is null", CA_DEFAULT_BLE_MTU_SIZE); unsigned int mtu; int ret = bt_device_get_att_mtu(address, &mtu); if (0 != ret) { OIC_LOG_V(ERROR, TAG, "bt_device_get_att_mtu failed with return [%s]", CALEGetErrorMsg(ret)); return CA_DEFAULT_BLE_MTU_SIZE; } OIC_LOG_V(INFO, TAG, "mtu size(including header) from bt_device_get_att_mtu is %d", mtu); OIC_LOG(DEBUG, TAG, "OUT"); return mtu - CA_BLE_MTU_HEADER_SIZE; }
CAResult_t CAAddNewCharacteristicsToGattServer(const bt_gatt_h svcPath, const char *charUUID, const char *charValue, int charValueLen, bool read) { OIC_LOG(DEBUG, TAG, "IN"); int permissions = BT_GATT_PERMISSION_READ | BT_GATT_PERMISSION_WRITE; int properties; if(read) { properties = BT_GATT_PROPERTY_NOTIFY | BT_GATT_PROPERTY_READ; } else { properties = BT_GATT_PROPERTY_WRITE | BT_GATT_PROPERTY_READ; } bt_gatt_h charPath; int ret = bt_gatt_characteristic_create(charUUID, permissions, properties, charValue, charValueLen, &charPath); if (0 != ret || NULL == charPath) { OIC_LOG_V(ERROR, TAG, "bt_gatt_add_characteristic failed with ret [%s]", CALEGetErrorMsg(ret)); return CA_STATUS_FAILED; } OIC_LOG_V(DEBUG, TAG, "bt_gatt_characteristic_create charPath obtained: %s", (char *)charPath); if (read) { ret = bt_gatt_server_set_notification_state_change_cb(charPath, CALENotificationCb, NULL); if (0 != ret) { OIC_LOG_V(ERROR, TAG, "bt_gatt_server_set_notification_state_change_cb failed with ret[%s]", CALEGetErrorMsg(ret)); return CA_STATUS_FAILED; } } ret = bt_gatt_service_add_characteristic(svcPath, charPath); if (0 != ret) { OIC_LOG_V(ERROR, TAG, "bt_gatt_service_add_characteristic failed with ret[%s]", CALEGetErrorMsg(ret)); return CA_STATUS_FAILED; } ca_mutex_lock(g_leCharacteristicMutex); if (read) { char desc_value[2] = {1, 0}; // Notification enabled. bt_gatt_h descriptor = NULL; permissions = BT_GATT_PERMISSION_READ | BT_GATT_PERMISSION_WRITE; ret = bt_gatt_descriptor_create(CA_GATT_CONFIGURATION_DESC_UUID, permissions, desc_value, sizeof(desc_value), &descriptor); if (0 != ret) { OIC_LOG_V(ERROR, TAG, "bt_gatt_descriptor_create failed with ret[%s]", CALEGetErrorMsg(ret)); return CA_STATUS_FAILED; } ret = bt_gatt_characteristic_add_descriptor(charPath, descriptor); if (0 != ret) { OIC_LOG_V(ERROR, TAG, "bt_gatt_characteristic_add_descriptor failed with ret[%s]", CALEGetErrorMsg(ret)); return CA_STATUS_FAILED; } if (NULL != g_gattReadCharPath) { OICFree(g_gattReadCharPath); g_gattReadCharPath = NULL; } g_gattReadCharPath = charPath; } else { if (NULL != g_gattWriteCharPath) { OICFree(g_gattWriteCharPath); g_gattWriteCharPath = NULL; } g_gattWriteCharPath = charPath; } ca_mutex_unlock(g_leCharacteristicMutex); OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; }