bfd432da9a6975f9bb7c65573345137809e654de
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_le_adapter / android / caleadapter.c
1 /******************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "jni.h"
26 #include "caleadapter.h"
27 #include "calecore.h"
28 #include "caleserver.h"
29 #include "logger.h"
30 #include "caadapterutils.h"
31
32 #define TAG PCF("CA_LE_ADAPTER")
33
34 // GATT server type
35 static jboolean gIsBluetoothGattServer;
36
37 // received packet callback
38 static CANetworkPacketReceivedCallback gLEReceivedCallback = NULL;
39 static CANetworkChangeCallback gLENetworkChangeCallback = NULL;
40
41 static void CALEPacketReceiveCallback(const char* address, const char* data)
42 {
43     OIC_LOG_V(DEBUG, TAG,
44             "CALEPacketReceiveCallback, from: %s, data: %s", address, data);
45
46     // call the callback
47     if (gLEReceivedCallback != NULL)
48     {
49         CARemoteEndpoint_t* endpoint = NULL;
50         endpoint = (CARemoteEndpoint_t*) OICMalloc(sizeof(CARemoteEndpoint_t));
51
52         if (endpoint == NULL)
53         {
54             OIC_LOG(DEBUG, TAG, "CALEPacketReceiveCallback, Memory allocation failed !");
55             OICFree(address);
56             return;
57         }
58
59         // set address
60         memset((void*) endpoint->addressInfo.BT.btMacAddress, 0, CA_MACADDR_SIZE);
61         if (CA_MACADDR_SIZE > strlen(address))
62         {
63             strcpy((char*) endpoint->addressInfo.BT.btMacAddress, address);
64         }
65         OICFree(address);
66
67         // set connectivity type
68         endpoint->connectivityType = CA_LE;
69
70         gLEReceivedCallback(endpoint, (void *) data, strlen(data));
71     }
72 }
73
74 static void CALENetStateChangeCallback(const char* address, const uint32_t status)
75 {
76     OIC_LOG_V(DEBUG, TAG,
77             "CALENetStateChangeCallback, status:%d", status);
78
79     // call the callback
80     if (gLENetworkChangeCallback != NULL)
81     {
82         CARemoteEndpoint_t* endpoint = NULL;
83         endpoint = (CARemoteEndpoint_t*) OICMalloc(sizeof(CARemoteEndpoint_t));
84
85         if (endpoint == NULL)
86         {
87             OIC_LOG(DEBUG, TAG, "CALENetworkStateChangedCallback, Memory allocation failed !");
88             OICFree(address);
89             return;
90         }
91
92         // set address
93         memset((void*) endpoint->addressInfo.BT.btMacAddress, 0, CA_MACADDR_SIZE);
94         if (CA_MACADDR_SIZE > strlen(address))
95         {
96             strcpy((char*) endpoint->addressInfo.BT.btMacAddress, address);
97         }
98         OICFree(address);
99
100         // set connectivity type
101         endpoint->connectivityType = CA_LE;
102
103         CANetworkStatus_t netStatus = CA_INTERFACE_DOWN;
104         if(status == 12)
105         {
106             netStatus = CA_INTERFACE_UP;
107         }
108         else if(status == 10)
109         {
110             netStatus = CA_INTERFACE_DOWN;
111         }
112
113         gLENetworkChangeCallback(endpoint, netStatus);
114     }
115 }
116
117 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
118         CANetworkPacketReceivedCallback reqRespCallback, CANetworkChangeCallback netCallback,
119         u_thread_pool_t handle)
120 {
121     OIC_LOG(DEBUG, TAG, "IntializeBLE");
122
123     gLEReceivedCallback = reqRespCallback;
124     gLENetworkChangeCallback = netCallback;
125
126     // register handlers
127     CAConnectivityHandler_t handler;
128     memset(&handler, 0, sizeof(CAConnectivityHandler_t));
129
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);
140
141     CALEInitialize(handle);
142     CALEServerInitialize(handle);
143
144     CALESetCallback(CALEPacketReceiveCallback);
145     CALESetNetStateCallback(CALENetStateChangeCallback);
146     CALEServerSetCallback(CALEPacketReceiveCallback);
147
148     return CA_STATUS_OK;
149 }
150
151 CAResult_t CAStartLE()
152 {
153     OIC_LOG_V(DEBUG, TAG, "CAStartLE");
154     //CANativeLEStartScan();
155
156     return CA_STATUS_OK;
157 }
158
159 CAResult_t CAStartLEListeningServer()
160 {
161     OIC_LOG_V(DEBUG, TAG, "CAStartLEListeningServer");
162
163     gIsBluetoothGattServer = JNI_TRUE;
164
165     // start gatt service
166     CALEServerStartMulticastServer();
167
168     return CA_STATUS_OK;
169 }
170
171 CAResult_t CAStartLEDiscoveryServer()
172 {
173     OIC_LOG_V(DEBUG, TAG, "CAStartLEDiscoveryServer");
174
175     gIsBluetoothGattServer = JNI_FALSE;
176
177     // start scan through gatt client
178     CALEStartMulticastServer();
179
180     return CA_STATUS_OK;
181 }
182
183 uint32_t CASendLEUnicastData(const CARemoteEndpoint_t* endpoint, void* data, uint32_t dataLen)
184 {
185
186     if(gIsBluetoothGattServer == JNI_FALSE)
187     {
188         OIC_LOG_V(DEBUG, TAG, "CALESendUnicastData");
189         CALESendUnicastMessage(endpoint->addressInfo.BT.btMacAddress, data, dataLen);
190     }
191     else if(gIsBluetoothGattServer == JNI_TRUE)
192     {
193         OIC_LOG_V(DEBUG, TAG, "CALEServerSendUnicastData");
194         CALEServerSendUnicastMessage(endpoint->addressInfo.BT.btMacAddress, data, dataLen);
195     }
196
197     return 0;
198 }
199
200 uint32_t CASendLEMulticastData(void* data, uint32_t dataLen)
201 {
202     if(gIsBluetoothGattServer == JNI_FALSE)
203     {
204         OIC_LOG_V(DEBUG, TAG, "CASendLEMulticastData");
205         CALESendMulticastMessage(data, dataLen);
206     }
207     else if(gIsBluetoothGattServer == JNI_TRUE)
208     {
209         OIC_LOG_V(DEBUG, TAG, "CALEServerSendMulticastMessage");
210         CALEServerSendMulticastMessage(data, dataLen);
211     }
212
213     return 0;
214 }
215
216 CAResult_t CAStartLENotifyServer()
217 {
218     OIC_LOG_V(DEBUG, TAG, "CAStartLENotifyServer");
219
220     return CA_STATUS_OK;
221 }
222
223 uint32_t CASendLENotification(const CARemoteEndpoint_t* endpoint, void* data, uint32_t dataLen)
224 {
225     if(gIsBluetoothGattServer == JNI_FALSE)
226     {
227         OIC_LOG_V(DEBUG, TAG, "not server mode");
228         return -1;
229     }
230     else if(gIsBluetoothGattServer == JNI_TRUE)
231     {
232         OIC_LOG_V(DEBUG, TAG, "CALEServerSendLEUnicastData");
233         CALEServerSendUnicastMessage(endpoint->addressInfo.BT.btMacAddress, data, dataLen);
234     }
235
236     return 0;
237 }
238
239 CAResult_t CAGetLEInterfaceInformation(CALocalConnectivity_t** info, uint32_t* size)
240 {
241     OIC_LOG_V(DEBUG, TAG, "CAGetLEInterfaceInformation");
242
243     CALocalConnectivity_t *netInfo = NULL;
244
245     int32_t netInfoSize = 1;
246
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);
250
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)
255     {
256         OIC_LOG_V(ERROR, TAG, "Failed to get interface info [%d]", ret);
257
258         OICFree(netInfo);
259         OICFree(macAddress);
260         return ret;
261     }
262
263     // Create local endpoint using util function
264     CALocalConnectivity_t *endpoint = CAAdapterCreateLocalEndpoint(CA_LE, macAddress);
265     if (NULL == endpoint)
266     {
267         OIC_LOG_V(ERROR, TAG, "Failed to create Local Endpoint!",
268                   CA_MEMORY_ALLOC_FAILED);
269         OICFree(netInfo);
270         OICFree(macAddress);
271         return CA_MEMORY_ALLOC_FAILED;
272     }
273
274     // copy unciast server information
275     endpoint->isSecured = CA_FALSE;
276     memcpy(&netInfo[0], endpoint, sizeof(CALocalConnectivity_t));
277     *size = 1;
278     *info = netInfo;
279
280     OICFree(macAddress);
281     CAAdapterFreeLocalEndpoint(endpoint);
282
283     OIC_LOG(INFO, TAG, "GetLEInterfaceInformation success");
284     OIC_LOG(DEBUG, TAG, "OUT");
285     return CA_STATUS_OK;
286 }
287
288 CAResult_t CAReadLEData()
289 {
290     OIC_LOG_V(DEBUG, TAG, "Read LE Data");
291
292     return CA_STATUS_OK;
293 }
294
295 CAResult_t CAStopLE()
296 {
297
298     if(gIsBluetoothGattServer == JNI_FALSE)
299     {
300         OIC_LOG_V(DEBUG, TAG, "CA Stop LE Scan");
301         CANativeLEStopScan();
302     }
303     else if(gIsBluetoothGattServer == JNI_TRUE)
304     {
305         OIC_LOG_V(DEBUG, TAG, "CA Stop Gatt Server");
306     }
307
308     return CA_STATUS_OK;
309 }
310
311 void CATerminateLE()
312 {
313     if(gIsBluetoothGattServer == JNI_FALSE)
314     {
315         OIC_LOG_V(DEBUG, TAG, "Terminat Gatt Client");
316         CALETerminate();
317     }
318     else if(gIsBluetoothGattServer == JNI_TRUE)
319     {
320         OIC_LOG_V(DEBUG, TAG, "Terminat Gatt Server");
321         CALEServerTerminate();
322     }
323 }