1 /******************************************************************
3 * Copyright 2017 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
24 #include "caleserver.h"
25 #include "caleutils.h"
26 #include "caleinterface.h"
27 #include "caadapterutils.h"
30 #include "oic_malloc.h"
31 #include "cathreadpool.h"
33 #include "uarraylist.h"
35 #define TAG PCF("OIC_CA_LE_SERVER")
37 #define WAIT_TIME_WRITE_CHARACTERISTIC 10000000
39 static CAPacketReceiveCallback g_packetReceiveCallback = NULL;
40 static CABLEErrorHandleCallback g_serverErrorCallback;
42 static u_arraylist_t *g_connectedDeviceList = NULL;
44 static bool g_isStartServer = false;
45 static bool g_isInitializedServer = false;
47 static CABLEDataReceivedCallback g_CABLEServerDataReceivedCallback = NULL;
48 static oc_mutex g_bleReqRespCbMutex = NULL;
49 static oc_mutex g_bleClientBDAddressMutex = NULL;
50 static oc_mutex g_connectedDeviceListMutex = NULL;
52 static oc_cond g_transmitQueueCond = NULL;
53 static oc_mutex g_transmitQueueMutex = NULL;
55 static bool g_isStartAdvertising = false;
56 static bool isEnableGattServer = true;
58 static id cbPeripheralIf;
60 @interface CBPeripheralIfClass:NSObject <CBPeripheralManagerDelegate>
63 NSMutableArray *peripheralMgrs;
64 static CBMutableCharacteristic *readCharacteristic = nil;
65 static CBMutableCharacteristic *writeCharacteristic = nil;
66 NSMutableDictionary *cbCentrals;
67 NSMutableDictionary *cbCentralsConnInfo;
69 @implementation CBPeripheralIfClass
70 static CBPeripheralManager *peripheralMgr = nil;
72 - (void) createPeripheralManager{
73 OIC_LOG(DEBUG, TAG, "[CBPrl] create peripheral manager");
74 peripheralMgr = [[CBPeripheralManager alloc] initWithDelegate:self
75 queue:nil options:nil];
76 peripheralMgrs = [NSMutableArray new];
77 cbCentrals = [[NSMutableDictionary alloc] init];
78 cbCentralsConnInfo = [[NSMutableDictionary alloc] init];
81 - (void) setCharacteristic{
82 OIC_LOG(DEBUG, TAG, "[CBPrl] set Characteristic");
83 readCharacteristic = [[CBMutableCharacteristic alloc]
84 initWithType:[CBUUID UUIDWithString:
85 [NSString stringWithUTF8String:OIC_GATT_CHARACTERISTIC_RESPONSE_UUID]]
86 properties:CBCharacteristicPropertyNotify | CBCharacteristicPropertyRead
88 permissions:CBAttributePermissionsReadable];
89 writeCharacteristic = [[CBMutableCharacteristic alloc]
90 initWithType:[CBUUID UUIDWithString:
91 [NSString stringWithUTF8String:OIC_GATT_CHARACTERISTIC_REQUEST_UUID]]
92 properties: CBCharacteristicPropertyWrite | CBCharacteristicPropertyWriteWithoutResponse
94 permissions:CBAttributePermissionsWriteable];
97 - (CBMutableService *) setService{
98 OIC_LOG(DEBUG, TAG, "PeripheralInterface: set Service");
99 CBMutableService *leService = [[CBMutableService alloc]
100 initWithType:[CBUUID UUIDWithString:
101 [NSString stringWithUTF8String:OIC_GATT_SERVICE_UUID]]
107 - (void) addService:(CBMutableService *) leService :(CBPeripheralManager *)peripheral{
108 OIC_LOG(DEBUG, TAG, "PeripheralInterface: add Service");
109 leService.characteristics = @[readCharacteristic, writeCharacteristic];
110 [peripheral addService:leService];
113 - (void) startAdvertising: (CBPeripheralManager *)peripheral{
114 [self setCharacteristic];
115 [self addService:[self setService]:peripheral];
117 NSString *beaconName = @"IoTivity Service";
119 if (!g_isStartAdvertising){
120 OIC_LOG(DEBUG, TAG, "PeripheralInterface: startAdvertising 1");
121 [peripheral startAdvertising:@{
122 CBAdvertisementDataLocalNameKey: beaconName,
123 CBAdvertisementDataServiceUUIDsKey:
124 @[[CBUUID UUIDWithString:[NSString stringWithUTF8String:OIC_GATT_SERVICE_UUID]]]}
126 g_isStartAdvertising = true;
130 - (void) stopAdvertising{
131 OIC_LOG(DEBUG, TAG, "PeripheralInterface: stopAdvertising");
132 [peripheralMgr stopAdvertising];
135 - (CAResult_t) sendData:(CBCentral *) connCentral :(NSData *)data{
136 if (peripheralMgr != nil){
137 if (connCentral != nil){
138 OIC_LOG_V(DEBUG, TAG, "central: %s, senddata updateValue",
139 [[connCentral.identifier UUIDString] UTF8String]);
140 NSUInteger tLen = [data length];
141 uint8_t *tData = (uint8_t *)malloc(tLen);
142 memcpy(tData, [data bytes], tLen);
143 OIC_LOG_BUFFER(DEBUG, TAG, tData, tLen);
145 bool result = [peripheralMgr updateValue: data
146 forCharacteristic:readCharacteristic
147 onSubscribedCentrals:@[connCentral]];
149 OIC_LOG(DEBUG, TAG, "CBCI: sending success");
152 OIC_LOG(ERROR, TAG, "CBCI: transmit queue is full! --> wait");
154 oc_mutex_lock(g_transmitQueueMutex);
155 oc_cond_wait(g_transmitQueueCond, g_transmitQueueMutex);
156 oc_mutex_unlock(g_transmitQueueMutex);
158 [peripheralMgr updateValue: data
159 forCharacteristic:readCharacteristic
160 onSubscribedCentrals:@[connCentral]];
164 OIC_LOG(ERROR, TAG, "CBCI: connectedCentral ERROR");
165 return CA_STATUS_FAILED;
168 return CA_STATUS_FAILED;
171 - (NSString *) createCentralUUID{
172 CFUUIDRef theUUID = CFUUIDCreate(NULL);
173 CFStringRef uuidString = CFUUIDCreateString(NULL, theUUID);
174 NSString *uuid = (NSString *)CFStringCreateCopy(NULL, uuidString);
176 CFRelease(uuidString);
177 return [uuid autorelease];
180 #pragma mark - CBPeripheralManagerDelegate
182 - (void) peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral{
183 if (peripheral.state == CBPeripheralManagerStatePoweredOn){
184 OIC_LOG(DEBUG, TAG, "PeripheralInterface: update state --> POWERED ON");
185 peripheralMgr = peripheral;
186 [peripheralMgrs addObject:peripheralMgr];
188 CALEServerSetFlagBTAdapter(true);
190 if (!g_isStartAdvertising){
191 CALEServerStartAdvertise();
194 OIC_LOG(DEBUG, TAG, "PeripheralInterface: update state --> ! powered on");
195 [peripheralMgrs removeObject: peripheral];
196 CALEServerSetFlagBTAdapter(false);
202 - (void)peripheralManager:(CBPeripheralManager *)peripheral
203 didAddService:(CBService *)service error:(NSError *)error{
204 OIC_LOG_V(INFO, TAG, "didAddService:%s", [[service.UUID UUIDString] UTF8String]);
206 OIC_LOG_V(ERROR, TAG, "didAddService:%s, error:%s",
207 [[service.UUID UUIDString] UTF8String], [[error localizedDescription] UTF8String]);
211 - (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral
212 error:(NSError *)error{
213 if ([[error localizedDescription] isEqualToString:@"null"]
214 || [[error localizedDescription] isEqualToString:@"<null>"]
215 || [[error localizedDescription] isEqualToString:@"(null)"]
216 || [[error localizedDescription] length] != 0){
218 OIC_LOG_V(ERROR, TAG, "peripheralManagerDidStartAdvertising : error:%s",
219 [[error localizedDescription] UTF8String]);
223 - (void)peripheralManager: (CBPeripheralManager *)peripheral
224 didReceiveReadRequest:(CBATTRequest *)request{
225 OIC_LOG(DEBUG, TAG, "didReceiveReadRequest");
228 - (void)peripheralManager:(CBPeripheralManager *)peripheral
229 central:(CBCentral *)central
230 didSubscribeToCharacteristic:(CBCharacteristic *)characteristic{
231 OIC_LOG(DEBUG, TAG, "PeripheralInterface: connected");
232 OIC_LOG_V(DEBUG, TAG, "connected characteristic %s",
233 [[characteristic.UUID UUIDString] UTF8String]);
235 bool hasCentral = false;
236 for (CBCentral *key in cbCentrals){
237 if ([key isEqual:central]){
239 OIC_LOG_V(DEBUG, TAG, "tuuid %s", [[cbCentrals objectForKey:key] UTF8String]);
244 OIC_LOG(DEBUG, TAG, "set Central Object");
245 [cbCentrals setObject:[cbPeripheralIf createCentralUUID] forKey:central];
248 const char *address = [CALEServerGetAddressFromGattObj(central) UTF8String];
249 if (false == CALEServerIsDeviceInList(address)){
250 OIC_LOG_V(DEBUG, TAG, "add connected Central obj to cache: %s", address);
251 CALEServerAddDeviceToList(central);
254 [cbCentralsConnInfo setObject:[NSNumber numberWithInteger:(int)STATE_CONNECTED] forKey:central];
257 - (void)peripheralManager:(CBPeripheralManager *)peripheral
258 central:(CBCentral *)central
259 didUnsubscribeFromCharacteristic:(CBCharacteristic *)characteristic{
260 OIC_LOG(DEBUG, TAG, "PeripheralInterface: disconnected");
262 const char *address = [CALEServerGetAddressFromGattObj(central) UTF8String];
263 if (true == CALEServerIsDeviceInList(address)){
264 OIC_LOG_V(DEBUG, TAG, "remove connected Central obj to cache: %s", address);
265 CALEServerRemoveDevice(address);
268 [cbCentralsConnInfo setObject:[NSNumber numberWithInteger:
269 (int)STATE_DISCONNECTED] forKey:central];
272 - (void)peripheralManager:(CBPeripheralManager *)peripheral
273 didReceiveWriteRequests: (NSArray *)requests{
274 NSMutableData *nsData = [[NSMutableData alloc] init];
275 for (CBATTRequest *request in requests){
276 if ([request.characteristic.UUID isEqual:
277 [CBUUID UUIDWithString:
278 [NSString stringWithUTF8String:OIC_GATT_CHARACTERISTIC_REQUEST_UUID]]]){
279 OIC_LOG_V(DEBUG, TAG, "Received data from central %s",
280 [[request.central.identifier UUIDString] UTF8String]);
282 const char* address = [CALEServerGetAddressFromGattObj(request.central) UTF8String];
284 OIC_LOG(ERROR, TAG, "address is not available");
288 OIC_LOG_V(DEBUG, TAG, "------------- received data (size:%lu)----------------",
289 request.value.length);
290 [nsData appendData:request.value];
291 uint32_t len = [nsData length];
292 uint8_t *data = (uint8_t*)OICMalloc(len);
293 uint32_t sentLength = 0;
295 memcpy(data, (const uint8_t*)[nsData bytes], len);
297 OIC_LOG_BUFFER(DEBUG, TAG, data, len);
299 oc_mutex_lock(g_bleClientBDAddressMutex);
300 g_CABLEServerDataReceivedCallback(address, data, len, &sentLength);
301 oc_mutex_unlock(g_bleClientBDAddressMutex);
306 - (void)peripheralManagerIsReadyToUpdateSubscribers:(CBPeripheralManager *) peripheral{
307 oc_mutex_lock(g_transmitQueueMutex);
308 OIC_LOG(DEBUG, TAG, "transmit queue is ready to update subscribers");
309 oc_cond_signal(g_transmitQueueCond);
310 oc_mutex_unlock(g_transmitQueueMutex);
317 * get the current connection state of the gatt profile to the remote device.
318 * @param[in] device bluetooth device object
319 * @return state of the profile connection.
321 static int CALEServerGetConnectionState(CBCentral *central)
323 OIC_LOG(DEBUG, TAG, "CALEServerGetConnectionState");
325 int connState = [[cbCentralsConnInfo objectForKey:central] intValue];
326 OIC_LOG_V(INFO, TAG, "connection state is %d", connState);
330 CAResult_t CALEStartAdvertise()
332 CAResult_t ret = CALEServerStartAdvertise();
333 if (CA_STATUS_OK != ret)
335 OIC_LOG(ERROR, TAG, "CALEServerStartAdvertise has failed");
341 CAResult_t CALEServerStartAdvertise()
343 OIC_LOG(DEBUG, TAG, "IN - CALEServerStartAdvertise");
345 if (!CALEServerIsEnableBTAdapter())
347 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
348 return CA_ADAPTER_NOT_ENABLED;
351 if ([peripheralMgrs count] != 0 && caglobals.serverFlags){
352 [cbPeripheralIf startAdvertising: peripheralMgrs[0]];
354 return CA_STATUS_FAILED;
357 OIC_LOG(DEBUG, TAG, "Advertising started!!");
359 OIC_LOG(DEBUG, TAG, "OUT - CALEServerStartAdvertise");
364 CAResult_t CALEServerStopAdvertise()
366 OIC_LOG(DEBUG, TAG, "LEServerStopAdvertise");
368 if (!CALEServerIsEnableBTAdapter())
370 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
371 return CA_ADAPTER_NOT_ENABLED;
374 [cbPeripheralIf stopAdvertising];
376 OIC_LOG(DEBUG, TAG, "Advertising stopped!!");
380 CAResult_t CALEServerStartGattServer()
382 OIC_LOG(DEBUG, TAG, "IN - CALEServerStartGattServer");
384 if (!CALEServerIsEnableBTAdapter())
386 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
387 return CA_ADAPTER_NOT_ENABLED;
392 OIC_LOG(DEBUG, TAG, "Gatt server already started");
395 OIC_LOG(DEBUG,TAG, "OUT - CALEServerStartGattServer");
399 CAResult_t CALEServerSend(CBCentral *central, const uint8_t *data, const uint32_t dataLen)
401 OIC_LOG(DEBUG, TAG, "IN - CALEServerSend");
403 if (!CALEServerIsEnableBTAdapter())
405 OIC_LOG(ERROR, TAG, "BT adapter is not enabled");
406 return CA_ADAPTER_NOT_ENABLED;
409 NSData *nsData = [NSData dataWithBytes:(const uint8_t*)data length:dataLen];
410 OIC_LOG(DEBUG, TAG, "send data");
411 OIC_LOG_BUFFER(DEBUG, TAG, data, dataLen);
413 CAResult_t result = [cbPeripheralIf sendData: central: nsData];
415 if (CA_STATUS_OK != result)
417 OIC_LOG(ERROR, TAG, "Fail to send response data");
421 OIC_LOG(DEBUG, TAG, "OUT - CALEServerSend");
425 CAResult_t CALEServerInitialize()
427 OIC_LOG(DEBUG, TAG, "IN - CALEServerInitialize");
429 cbPeripheralIf = [[CBPeripheralIfClass alloc] init];
430 [cbPeripheralIf createPeripheralManager];
432 CAResult_t ret = CA_STATUS_FAILED;
433 g_transmitQueueCond = oc_cond_new();
435 ret = CALEServerInitMutexVaraibles();
436 if (CA_STATUS_OK != ret)
438 OIC_LOG(ERROR, TAG, "CALEServerInitMutexVaraibles has failed");
440 return CA_STATUS_FAILED;
442 CALEServerCreateCachedDeviceList();
444 g_isInitializedServer = true;
445 OIC_LOG(DEBUG, TAG, "OUT - CALEServerInitialize");
449 void CALEServerTerminate()
451 OIC_LOG(DEBUG, TAG, "IN - CALEServerTerminate");
453 CALEServerTerminateMutexVaraibles();
454 CALEServerTerminateConditionVaraibles();
456 g_isInitializedServer = false;
458 oc_cond_free(g_transmitQueueCond);
459 g_transmitQueueCond = NULL;
461 OIC_LOG(DEBUG, TAG, "OUT - CALEServerTerminate");
464 CAResult_t CALEServerSendUnicastMessage(const char* address, const uint8_t* data, uint32_t dataLen)
466 OIC_LOG_V(DEBUG, TAG, "CALEServerSendUnicastMessage(%s, %p)", address, data);
467 VERIFY_NON_NULL(address, TAG, "address is null");
468 VERIFY_NON_NULL(data, TAG, "data is null");
470 CAResult_t ret = CALEServerSendUnicastMessageImpl(address, data, dataLen);
471 if (CA_STATUS_OK != ret)
473 OIC_LOG(ERROR, TAG, "CALEServerSendUnicastMessageImpl has failed");
479 CAResult_t CALEServerSendMulticastMessage(const uint8_t* data, uint32_t dataLen)
481 OIC_LOG_V(DEBUG, TAG, "CALEServerSendMulticastMessage(%p)", data);
482 VERIFY_NON_NULL(data, TAG, "data is null");
484 CAResult_t ret = CALEServerSendMulticastMessageImpl(data, dataLen);
485 if (CA_STATUS_OK != ret)
487 OIC_LOG(ERROR, TAG, "CALEServerSendMulticastMessageImpl has failed");
493 CAResult_t CALEServerStartMulticastServer()
495 OIC_LOG(DEBUG, TAG, "IN - CALEServerStartMulticastServer");
497 if (!g_isInitializedServer)
499 OIC_LOG(INFO, TAG, "server is not initialized");
500 return CA_STATUS_FAILED;
505 OIC_LOG(INFO, TAG, "server is already started..it will be skipped");
506 return CA_STATUS_FAILED;
509 g_isStartServer = true;
511 CAResult_t ret = CALEServerStartGattServer();
512 if (CA_STATUS_OK != ret)
514 OIC_LOG(ERROR, TAG, "Fail to start gatt server");
518 if(!CALEServerIsEnableBTAdapter())
520 OIC_LOG(DEBUG, TAG, "Cannot start advertise. Not powered on yet.");
523 if(!g_isStartAdvertising)
524 ret = CALEServerStartAdvertise();
527 OIC_LOG(DEBUG, TAG, "skipped cause already advertising.");
529 if (CA_STATUS_OK != ret)
531 OIC_LOG(ERROR, TAG, "CALEServerStartAdvertise has failed");
534 OIC_LOG(DEBUG, TAG, "OUT - CALEServerStartMulticastServer");
538 CAResult_t CALEServerStopMulticastServer()
540 OIC_LOG(DEBUG, TAG, "IN - CALEServerStopMulticastServer");
542 if (false == g_isStartServer)
544 OIC_LOG(INFO, TAG, "server is already stopped..it will be skipped");
545 return CA_STATUS_FAILED;
548 CAResult_t ret = CALEServerStopAdvertise();
549 if (CA_STATUS_OK != ret)
551 OIC_LOG(ERROR, TAG, "CALEServerStopAdvertise has failed");
554 g_isStartServer = false;
556 OIC_LOG(DEBUG, TAG, "OUT - CALEServerStopMulticastServer");
560 void CALEServerSetCallback(CAPacketReceiveCallback callback)
562 OIC_LOG(DEBUG, TAG, "CALEServerSetCallback");
563 g_packetReceiveCallback = callback;
566 CAResult_t CALEServerSendUnicastMessageImpl(const char* address, const uint8_t* data,
569 OIC_LOG_V(DEBUG, TAG, "CALEServerSendUnicastMessageImpl, address: %s, data: %p, size: %u",
570 address, data, dataLen);
571 OIC_LOG_BUFFER(DEBUG, TAG, data, dataLen);
572 VERIFY_NON_NULL(address, TAG, "address is null");
573 VERIFY_NON_NULL(data, TAG, "data is null");
575 if (!g_connectedDeviceList)
577 OIC_LOG(ERROR, TAG, "g_connectedDeviceList is null");
578 return CA_STATUS_FAILED;
582 uint32_t length = u_arraylist_length(g_connectedDeviceList);
583 for (uint32_t index = 0; index < length; index++)
585 OIC_LOG(DEBUG, TAG, "check device address");
586 CBCentral *tCentral = (CBCentral *) u_arraylist_get(g_connectedDeviceList, index);
589 OIC_LOG(ERROR, TAG, "tCentral is null");
590 return CA_SEND_FAILED;
593 const char *setAddress = [CALEServerGetAddressFromGattObj(tCentral) UTF8String];
596 OIC_LOG(ERROR, TAG, "setAddress is null");
597 return CA_SEND_FAILED;
600 OIC_LOG_V(DEBUG, TAG, "setAddress : %s", setAddress);
601 OIC_LOG_V(DEBUG, TAG, "address : %s", address);
603 if (!strcmp(setAddress, address))
605 OIC_LOG_V(DEBUG, TAG, "found the device: %s", setAddress);
607 CAResult_t res = CALEServerSend(tCentral, data, dataLen);
608 if (CA_STATUS_OK != res)
610 OIC_LOG(ERROR, TAG, "send has failed");
611 return CA_SEND_FAILED;
617 OIC_LOG(INFO, TAG, "unicast - send request is successful");
621 CAResult_t CALEServerSendMulticastMessageImpl(const uint8_t *data, uint32_t dataLen)
623 OIC_LOG_V(DEBUG, TAG, "CALEServerSendMulticastMessageImpl, send to, data: %s", data);
624 VERIFY_NON_NULL(data, TAG, "data is null");
626 if (!g_connectedDeviceList)
628 OIC_LOG(ERROR, TAG, "g_connectedDeviceList is null");
629 return CA_STATUS_FAILED;
632 uint32_t length = u_arraylist_length(g_connectedDeviceList);
633 for (uint32_t index = 0; index < length; index++)
635 CBCentral *tCentral = (CBCentral *) u_arraylist_get(g_connectedDeviceList, index);
638 OIC_LOG(ERROR, TAG, "central is null");
642 const char *addr = [CALEServerGetAddressFromGattObj(tCentral) UTF8String];
643 CAResult_t res = CALEServerSend(tCentral, data, dataLen);
644 if (CA_STATUS_OK != res)
646 OIC_LOG_V(ERROR, TAG, "Send failed: %s", addr);
649 OIC_LOG_V(INFO, TAG, "unicast - send request is successful for a device[%s]", addr);
655 void CALEServerCreateCachedDeviceList()
657 oc_mutex_lock(g_connectedDeviceListMutex);
658 if (!g_connectedDeviceList)
660 OIC_LOG(DEBUG, TAG, "Create device list");
661 g_connectedDeviceList = u_arraylist_create();
663 oc_mutex_unlock(g_connectedDeviceListMutex);
666 bool CALEServerIsDeviceInList(const char* remoteAddress)
668 VERIFY_NON_NULL_RET(remoteAddress, TAG, "remoteAddress is null", false);
670 if (!g_connectedDeviceList)
672 OIC_LOG(ERROR, TAG, "list is null");
676 uint32_t length = u_arraylist_length(g_connectedDeviceList);
677 for (uint32_t index = 0; index < length; index++)
679 CBCentral *central = (CBCentral *) u_arraylist_get(g_connectedDeviceList, index);
683 OIC_LOG(ERROR, TAG, "connectedDevice is not available");
687 const char* setAddress = [CALEServerGetAddressFromGattObj(central) UTF8String];
690 OIC_LOG(ERROR, TAG, "setAddress is null");
694 if (!strcmp(remoteAddress, setAddress))
696 OIC_LOG(ERROR, TAG, "the device is already set");
701 OIC_LOG(DEBUG, TAG, "there are no device in the list");
705 CAResult_t CALEServerAddDeviceToList(CBCentral *central)
707 OIC_LOG(DEBUG, TAG, "IN - CALEServerAddDeviceToList");
708 VERIFY_NON_NULL(central, TAG, "central is null");
710 oc_mutex_lock(g_connectedDeviceListMutex);
712 if (!g_connectedDeviceList)
714 OIC_LOG(ERROR, TAG, "list is null");
715 oc_mutex_unlock(g_connectedDeviceListMutex);
716 return CA_STATUS_FAILED;
719 const char *remoteAddress = [CALEServerGetAddressFromGattObj(central) UTF8String];
722 OIC_LOG(ERROR, TAG, "remoteAddress is null");
723 oc_mutex_unlock(g_connectedDeviceListMutex);
724 return CA_STATUS_FAILED;
727 if (false == CALEServerIsDeviceInList(remoteAddress))
729 u_arraylist_add(g_connectedDeviceList, central);
730 OIC_LOG_V(DEBUG, TAG, "Set the object to ArrayList as Element : %s", remoteAddress);
733 oc_mutex_unlock(g_connectedDeviceListMutex);
734 OIC_LOG(DEBUG, TAG, "OUT - CALEServerAddDeviceToList");
738 CAResult_t CALEServerRemoveAllDevices()
740 OIC_LOG(DEBUG, TAG, "IN - CALEServerRemoveAllDevices");
742 oc_mutex_lock(g_connectedDeviceListMutex);
743 if (!g_connectedDeviceList)
745 OIC_LOG(ERROR, TAG, "g_connectedDeviceList is null");
746 oc_mutex_unlock(g_connectedDeviceListMutex);
747 return CA_STATUS_FAILED;
750 uint32_t length = u_arraylist_length(g_connectedDeviceList);
751 for (uint32_t index = 0; index < length; index++)
753 CBCentral *central = (CBCentral *)u_arraylist_get(g_connectedDeviceList, index);
754 [cbCentrals removeObjectForKey: central];
755 if (NULL == u_arraylist_remove(g_connectedDeviceList, index)){
756 oc_mutex_unlock(g_connectedDeviceListMutex);
757 return CA_STATUS_FAILED;
761 OICFree(g_connectedDeviceList);
762 g_connectedDeviceList = NULL;
763 oc_mutex_unlock(g_connectedDeviceListMutex);
765 OIC_LOG(DEBUG, TAG, "OUT - CALEServerRemoveAllDevices");
769 CAResult_t CALEServerRemoveDevice(const char *remoteAddress)
771 OIC_LOG(DEBUG, TAG, "IN CALEServerRemoveDevice");
773 oc_mutex_lock(g_connectedDeviceListMutex);
774 if (!g_connectedDeviceList)
776 OIC_LOG(ERROR, TAG, "no deviceList");
777 oc_mutex_unlock(g_connectedDeviceListMutex);
778 return CA_STATUS_FAILED;
781 uint32_t length = u_arraylist_length(g_connectedDeviceList);
782 for (uint32_t index = 0; index < length; index++)
784 CBCentral *central = (CBCentral *)u_arraylist_get(g_connectedDeviceList, index);
788 const char* setAddress = [CALEServerGetAddressFromGattObj(central) UTF8String];
791 OIC_LOG(ERROR, TAG, "setAddress is null");
795 if (!strcmp(setAddress, remoteAddress))
797 OIC_LOG_V(DEBUG, TAG, "device address : %s", remoteAddress);
799 if (NULL == u_arraylist_remove(g_connectedDeviceList, index))
801 OIC_LOG(ERROR, TAG, "List removal failed.");
802 oc_mutex_unlock(g_connectedDeviceListMutex);
803 return CA_STATUS_FAILED;
805 [cbCentrals removeObjectForKey: central];
806 oc_mutex_unlock(g_connectedDeviceListMutex);
812 oc_mutex_unlock(g_connectedDeviceListMutex);
814 OIC_LOG(DEBUG, TAG, "there are no device in the device list");
816 OIC_LOG(DEBUG, TAG, "IN CALEServerRemoveDevice");
817 return CA_STATUS_FAILED;
825 CAResult_t CAStartLEGattServer()
827 CALEServerStartMulticastServer();
832 CAResult_t CAStopLEGattServer()
834 OIC_LOG(DEBUG, TAG, "CAStopLEGattServer");
836 CAResult_t ret = CALEServerRemoveAllDevices();
837 if (CA_STATUS_OK != ret)
839 OIC_LOG(ERROR, TAG, "CALEServerRemoveAllDevices has failed");
840 return CA_STATUS_FAILED;
843 g_isStartServer = false;
848 CAResult_t CAInitializeLEGattServer()
850 OIC_LOG(DEBUG, TAG, "Initialize Gatt Server");
851 return CALEServerInitialize();
854 void CATerminateLEGattServer()
856 OIC_LOG(DEBUG, TAG, "Terminate Gatt Server");
857 CALEServerTerminate();
860 void CASetLEReqRespServerCallback(CABLEDataReceivedCallback callback)
862 oc_mutex_lock(g_bleReqRespCbMutex);
863 g_CABLEServerDataReceivedCallback = callback;
864 oc_mutex_unlock(g_bleReqRespCbMutex);
867 void CASetBLEServerErrorHandleCallback(CABLEErrorHandleCallback callback)
869 g_serverErrorCallback = callback;
872 CAResult_t CAUpdateCharacteristicsToGattClient(const char *address,
873 const uint8_t *charValue,
874 uint32_t charValueLen)
876 CAResult_t result = CA_SEND_FAILED;
877 VERIFY_NON_NULL(charValue, TAG, "charValue is null");
881 result = CALEServerSendUnicastMessage(address, charValue, charValueLen);
887 CAResult_t CAUpdateCharacteristicsToAllGattClients(const uint8_t *charValue,
888 uint32_t charValueLen)
890 VERIFY_NON_NULL(charValue, TAG, "device is null");
892 CAResult_t result = CALEServerSendMulticastMessage(charValue, charValueLen);
897 void CASetLEServerThreadPoolHandle(ca_thread_pool_t handle)
899 OIC_LOG(INFO, TAG, "CASetLEServerThreadPoolHandle is not support");
903 CAResult_t CALEServerInitMutexVaraibles()
905 if (NULL == g_bleReqRespCbMutex)
907 g_bleReqRespCbMutex = oc_mutex_new();
908 if (NULL == g_bleReqRespCbMutex)
910 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
911 return CA_STATUS_FAILED;
915 if (NULL == g_bleClientBDAddressMutex)
917 g_bleClientBDAddressMutex = oc_mutex_new();
918 if (NULL == g_bleClientBDAddressMutex)
920 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
921 return CA_STATUS_FAILED;
925 if (NULL == g_connectedDeviceListMutex)
927 g_connectedDeviceListMutex = oc_mutex_new();
928 if (NULL == g_connectedDeviceListMutex)
930 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
931 return CA_STATUS_FAILED;
935 if (NULL == g_transmitQueueMutex){
936 g_transmitQueueMutex = oc_mutex_new();
937 if (NULL == g_transmitQueueMutex){
938 OIC_LOG(ERROR, TAG, "oc_mutex_new has failed");
939 return CA_STATUS_FAILED;
946 void CALEServerTerminateMutexVaraibles()
948 oc_mutex_free(g_bleReqRespCbMutex);
949 g_bleReqRespCbMutex = NULL;
951 oc_mutex_free(g_bleClientBDAddressMutex);
952 g_bleClientBDAddressMutex = NULL;
954 oc_mutex_free(g_connectedDeviceListMutex);
955 g_connectedDeviceListMutex = NULL;
957 oc_mutex_free(g_transmitQueueMutex);
958 g_transmitQueueMutex = NULL;
961 void CALEServerTerminateConditionVaraibles()
963 OIC_LOG(DEBUG, TAG, "this method is not supported");
966 NSString *CALEServerGetAddressFromGattObj(CBCentral *central){
967 VERIFY_NON_NULL_RET(central, TAG, "central is null", NULL);
968 NSString *address = [cbCentrals objectForKey: central];
970 OIC_LOG(ERROR, TAG, "address isn't available");
976 void CALEServerSetFlagBTAdapter(bool state){
977 isEnableGattServer = state;
980 bool CALEServerIsEnableBTAdapter(){
981 return isEnableGattServer;