1 /******************************************************************
3 * Copyright 2014 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 ******************************************************************/
26 #include "caleadapter.h"
28 #include "caleserver.h"
30 #include "caadapterutils.h"
32 #define TAG PCF("CA_LE_ADAPTER")
35 static jboolean gIsBluetoothGattServer;
37 // received packet callback
38 static CANetworkPacketReceivedCallback gLEReceivedCallback = NULL;
39 static CANetworkChangeCallback gLENetworkChangeCallback = NULL;
41 static void CALEPacketReceiveCallback(const char* address, const char* data)
44 "CALEPacketReceiveCallback, from: %s, data: %s", address, data);
47 if (gLEReceivedCallback != NULL)
49 CARemoteEndpoint_t* endpoint = NULL;
50 endpoint = (CARemoteEndpoint_t*) OICMalloc(sizeof(CARemoteEndpoint_t));
54 OIC_LOG(DEBUG, TAG, "CALEPacketReceiveCallback, Memory allocation failed !");
60 memset((void*) endpoint->addressInfo.BT.btMacAddress, 0, CA_MACADDR_SIZE);
61 if (CA_MACADDR_SIZE > strlen(address))
63 strcpy((char*) endpoint->addressInfo.BT.btMacAddress, address);
67 // set connectivity type
68 endpoint->connectivityType = CA_LE;
70 gLEReceivedCallback(endpoint, (void *) data, strlen(data));
74 static void CALENetStateChangeCallback(const char* address, const uint32_t status)
77 "CALENetStateChangeCallback, status:%d", status);
80 if (gLENetworkChangeCallback != NULL)
82 CARemoteEndpoint_t* endpoint = NULL;
83 endpoint = (CARemoteEndpoint_t*) OICMalloc(sizeof(CARemoteEndpoint_t));
87 OIC_LOG(DEBUG, TAG, "CALENetworkStateChangedCallback, Memory allocation failed !");
93 memset((void*) endpoint->addressInfo.BT.btMacAddress, 0, CA_MACADDR_SIZE);
94 if (CA_MACADDR_SIZE > strlen(address))
96 strcpy((char*) endpoint->addressInfo.BT.btMacAddress, address);
100 // set connectivity type
101 endpoint->connectivityType = CA_LE;
103 CANetworkStatus_t netStatus = CA_INTERFACE_DOWN;
106 netStatus = CA_INTERFACE_UP;
108 else if(status == 10)
110 netStatus = CA_INTERFACE_DOWN;
113 gLENetworkChangeCallback(endpoint, netStatus);
117 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
118 CANetworkPacketReceivedCallback reqRespCallback, CANetworkChangeCallback netCallback,
119 u_thread_pool_t handle)
121 OIC_LOG(DEBUG, TAG, "IntializeBLE");
123 gLEReceivedCallback = reqRespCallback;
124 gLENetworkChangeCallback = netCallback;
127 CAConnectivityHandler_t handler;
128 memset(&handler, 0, sizeof(CAConnectivityHandler_t));
130 handler.startAdapter = CAStartLE;
131 handler.startListenServer = CAStartLEListeningServer;
132 handler.startDiscoverServer = CAStartLEDiscoveryServer;
133 handler.sendData = CASendLEUnicastData;
134 handler.sendDataToAll = CASendLEMulticastData;
135 handler.GetnetInfo = CAGetLEInterfaceInformation;
136 handler.readData = CAReadLEData;
137 handler.stopAdapter = CAStopLE;
138 handler.terminate = CATerminateLE;
139 registerCallback(handler, CA_LE);
141 CALEInitialize(handle);
142 CALEServerInitialize(handle);
144 CALESetCallback(CALEPacketReceiveCallback);
145 CALESetNetStateCallback(CALENetStateChangeCallback);
146 CALEServerSetCallback(CALEPacketReceiveCallback);
151 CAResult_t CAStartLE()
153 OIC_LOG_V(DEBUG, TAG, "CAStartLE");
154 //CANativeLEStartScan();
159 CAResult_t CAStartLEListeningServer()
161 OIC_LOG_V(DEBUG, TAG, "CAStartLEListeningServer");
163 gIsBluetoothGattServer = JNI_TRUE;
165 // start gatt service
166 CALEServerStartMulticastServer();
171 CAResult_t CAStartLEDiscoveryServer()
173 OIC_LOG_V(DEBUG, TAG, "CAStartLEDiscoveryServer");
175 gIsBluetoothGattServer = JNI_FALSE;
177 // start scan through gatt client
178 CALEStartMulticastServer();
183 uint32_t CASendLEUnicastData(const CARemoteEndpoint_t* endpoint, void* data, uint32_t dataLen)
186 if(gIsBluetoothGattServer == JNI_FALSE)
188 OIC_LOG_V(DEBUG, TAG, "CALESendUnicastData");
189 CALESendUnicastMessage(endpoint->addressInfo.BT.btMacAddress, data, dataLen);
191 else if(gIsBluetoothGattServer == JNI_TRUE)
193 OIC_LOG_V(DEBUG, TAG, "CALEServerSendUnicastData");
194 CALEServerSendUnicastMessage(endpoint->addressInfo.BT.btMacAddress, data, dataLen);
200 uint32_t CASendLEMulticastData(void* data, uint32_t dataLen)
202 if(gIsBluetoothGattServer == JNI_FALSE)
204 OIC_LOG_V(DEBUG, TAG, "CASendLEMulticastData");
205 CALESendMulticastMessage(data, dataLen);
207 else if(gIsBluetoothGattServer == JNI_TRUE)
209 OIC_LOG_V(DEBUG, TAG, "CALEServerSendMulticastMessage");
210 CALEServerSendMulticastMessage(data, dataLen);
216 CAResult_t CAStartLENotifyServer()
218 OIC_LOG_V(DEBUG, TAG, "CAStartLENotifyServer");
223 uint32_t CASendLENotification(const CARemoteEndpoint_t* endpoint, void* data, uint32_t dataLen)
225 if(gIsBluetoothGattServer == JNI_FALSE)
227 OIC_LOG_V(DEBUG, TAG, "not server mode");
230 else if(gIsBluetoothGattServer == JNI_TRUE)
232 OIC_LOG_V(DEBUG, TAG, "CALEServerSendLEUnicastData");
233 CALEServerSendUnicastMessage(endpoint->addressInfo.BT.btMacAddress, data, dataLen);
239 CAResult_t CAGetLEInterfaceInformation(CALocalConnectivity_t** info, uint32_t* size)
241 OIC_LOG_V(DEBUG, TAG, "CAGetLEInterfaceInformation");
243 CALocalConnectivity_t *netInfo = NULL;
245 int32_t netInfoSize = 1;
247 netInfo = (CALocalConnectivity_t *) OICMalloc(sizeof(CALocalConnectivity_t) * netInfoSize);
248 VERIFY_NON_NULL_RET(netInfo, TAG, "malloc failed", CA_MEMORY_ALLOC_FAILED);
249 memset(netInfo, 0, sizeof(CALocalConnectivity_t) * netInfoSize);
251 char *macAddress = NULL;
252 CAResult_t ret = CALEGetInterfaceInfo(&macAddress);
253 OIC_LOG_V(ERROR, TAG, "address : %s", macAddress);
254 if (CA_STATUS_OK != ret || NULL == macAddress)
256 OIC_LOG_V(ERROR, TAG, "Failed to get interface info [%d]", ret);
263 // Create local endpoint using util function
264 CALocalConnectivity_t *endpoint = CAAdapterCreateLocalEndpoint(CA_LE, macAddress);
265 if (NULL == endpoint)
267 OIC_LOG_V(ERROR, TAG, "Failed to create Local Endpoint!",
268 CA_MEMORY_ALLOC_FAILED);
271 return CA_MEMORY_ALLOC_FAILED;
274 // copy unciast server information
275 endpoint->isSecured = CA_FALSE;
276 memcpy(&netInfo[0], endpoint, sizeof(CALocalConnectivity_t));
281 CAAdapterFreeLocalEndpoint(endpoint);
283 OIC_LOG(INFO, TAG, "GetLEInterfaceInformation success");
284 OIC_LOG(DEBUG, TAG, "OUT");
288 CAResult_t CAReadLEData()
290 OIC_LOG_V(DEBUG, TAG, "Read LE Data");
295 CAResult_t CAStopLE()
298 if(gIsBluetoothGattServer == JNI_FALSE)
300 OIC_LOG_V(DEBUG, TAG, "CA Stop LE Scan");
301 CANativeLEStopScan();
303 else if(gIsBluetoothGattServer == JNI_TRUE)
305 OIC_LOG_V(DEBUG, TAG, "CA Stop Gatt Server");
313 if(gIsBluetoothGattServer == JNI_FALSE)
315 OIC_LOG_V(DEBUG, TAG, "Terminat Gatt Client");
318 else if(gIsBluetoothGattServer == JNI_TRUE)
320 OIC_LOG_V(DEBUG, TAG, "Terminat Gatt Server");
321 CALEServerTerminate();