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 !");
55 OICFree((char*)address);
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);
65 OICFree((char*)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 CANetworkStatus_t netStatus = CA_INTERFACE_DOWN;
85 netStatus = CA_INTERFACE_UP;
89 netStatus = CA_INTERFACE_DOWN;
92 CALocalConnectivity_t *localEndpoint = CAAdapterCreateLocalEndpoint(CA_LE, address);
95 OIC_LOG_V(ERROR, TAG, "Out of memory");
99 gLENetworkChangeCallback(localEndpoint, netStatus);
101 CAAdapterFreeLocalEndpoint(localEndpoint);
106 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
107 CANetworkPacketReceivedCallback reqRespCallback, CANetworkChangeCallback netCallback,
108 u_thread_pool_t handle)
110 OIC_LOG(DEBUG, TAG, "IntializeBLE");
112 gLEReceivedCallback = reqRespCallback;
113 gLENetworkChangeCallback = netCallback;
116 CAConnectivityHandler_t handler;
117 memset(&handler, 0, sizeof(CAConnectivityHandler_t));
119 handler.startAdapter = CAStartLE;
120 handler.startListenServer = CAStartLEListeningServer;
121 handler.startDiscoverServer = CAStartLEDiscoveryServer;
122 handler.sendData = CASendLEUnicastData;
123 handler.sendDataToAll = CASendLEMulticastData;
124 handler.GetnetInfo = CAGetLEInterfaceInformation;
125 handler.readData = CAReadLEData;
126 handler.stopAdapter = CAStopLE;
127 handler.terminate = CATerminateLE;
128 registerCallback(handler, CA_LE);
130 CALEInitialize(handle);
131 CALEServerInitialize(handle);
133 CALESetCallback(CALEPacketReceiveCallback);
134 CALESetNetStateCallback(CALENetStateChangeCallback);
135 CALEServerSetCallback(CALEPacketReceiveCallback);
140 CAResult_t CAStartLE()
142 OIC_LOG_V(DEBUG, TAG, "CAStartLE");
143 //CANativeLEStartScan();
148 CAResult_t CAStartLEListeningServer()
150 OIC_LOG_V(DEBUG, TAG, "CAStartLEListeningServer");
152 gIsBluetoothGattServer = JNI_TRUE;
154 // start gatt service
155 CALEServerStartMulticastServer();
160 CAResult_t CAStartLEDiscoveryServer()
162 OIC_LOG_V(DEBUG, TAG, "CAStartLEDiscoveryServer");
164 gIsBluetoothGattServer = JNI_FALSE;
166 // start scan through gatt client
167 CALEStartMulticastServer();
172 uint32_t CASendLEUnicastData(const CARemoteEndpoint_t* endpoint, void* data, uint32_t dataLen)
175 if(gIsBluetoothGattServer == JNI_FALSE)
177 OIC_LOG_V(DEBUG, TAG, "CALESendUnicastData");
178 CALESendUnicastMessage(endpoint->addressInfo.BT.btMacAddress, data, dataLen);
180 else if(gIsBluetoothGattServer == JNI_TRUE)
182 OIC_LOG_V(DEBUG, TAG, "CALEServerSendUnicastData");
183 CALEServerSendUnicastMessage(endpoint->addressInfo.BT.btMacAddress, data, dataLen);
189 uint32_t CASendLEMulticastData(void* data, uint32_t dataLen)
191 if(gIsBluetoothGattServer == JNI_FALSE)
193 OIC_LOG_V(DEBUG, TAG, "CASendLEMulticastData");
194 CALESendMulticastMessage(data, dataLen);
196 else if(gIsBluetoothGattServer == JNI_TRUE)
198 OIC_LOG_V(DEBUG, TAG, "CALEServerSendMulticastMessage");
199 CALEServerSendMulticastMessage(data, dataLen);
205 CAResult_t CAStartLENotifyServer()
207 OIC_LOG_V(DEBUG, TAG, "CAStartLENotifyServer");
212 uint32_t CASendLENotification(const CARemoteEndpoint_t* endpoint, void* data, uint32_t dataLen)
214 if(gIsBluetoothGattServer == JNI_FALSE)
216 OIC_LOG_V(DEBUG, TAG, "not server mode");
219 else if(gIsBluetoothGattServer == JNI_TRUE)
221 OIC_LOG_V(DEBUG, TAG, "CALEServerSendLEUnicastData");
222 CALEServerSendUnicastMessage(endpoint->addressInfo.BT.btMacAddress, data, dataLen);
228 CAResult_t CAGetLEInterfaceInformation(CALocalConnectivity_t** info, uint32_t* size)
230 OIC_LOG_V(DEBUG, TAG, "CAGetLEInterfaceInformation");
232 CALocalConnectivity_t *netInfo = NULL;
234 int32_t netInfoSize = 1;
236 netInfo = (CALocalConnectivity_t *) OICMalloc(sizeof(CALocalConnectivity_t) * netInfoSize);
239 OIC_LOG_V(ERROR, TAG, "Invalid input..");
240 return CA_MEMORY_ALLOC_FAILED;
242 memset(netInfo, 0, sizeof(CALocalConnectivity_t) * netInfoSize);
244 char *macAddress = NULL;
245 CAResult_t ret = CALEGetInterfaceInfo(&macAddress);
246 OIC_LOG_V(ERROR, TAG, "address : %s", macAddress);
247 if (CA_STATUS_OK != ret || NULL == macAddress)
249 OIC_LOG_V(ERROR, TAG, "Failed to get interface info [%d]", ret);
256 // Create local endpoint using util function
257 CALocalConnectivity_t *endpoint = CAAdapterCreateLocalEndpoint(CA_LE, macAddress);
258 if (NULL == endpoint)
260 OIC_LOG_V(ERROR, TAG, "Failed to create Local Endpoint!",
261 CA_MEMORY_ALLOC_FAILED);
264 return CA_MEMORY_ALLOC_FAILED;
267 // copy unciast server information
268 endpoint->isSecured = CA_FALSE;
269 memcpy(&netInfo[0], endpoint, sizeof(CALocalConnectivity_t));
274 CAAdapterFreeLocalEndpoint(endpoint);
276 OIC_LOG(INFO, TAG, "GetLEInterfaceInformation success");
277 OIC_LOG(DEBUG, TAG, "OUT");
281 CAResult_t CAReadLEData()
283 OIC_LOG_V(DEBUG, TAG, "Read LE Data");
288 CAResult_t CAStopLE()
291 if(gIsBluetoothGattServer == JNI_FALSE)
293 OIC_LOG_V(DEBUG, TAG, "CA Stop LE Scan");
294 CANativeLEStopScan();
296 else if(gIsBluetoothGattServer == JNI_TRUE)
298 OIC_LOG_V(DEBUG, TAG, "CA Stop Gatt Server");
306 if(gIsBluetoothGattServer == JNI_FALSE)
308 OIC_LOG_V(DEBUG, TAG, "Terminat Gatt Client");
311 else if(gIsBluetoothGattServer == JNI_TRUE)
313 OIC_LOG_V(DEBUG, TAG, "Terminat Gatt Server");
314 CALEServerTerminate();