Fixed klockwork memory leaks and modified the logs
[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((char*)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((char*)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         CANetworkStatus_t netStatus = CA_INTERFACE_DOWN;
83         if(status == 12)
84         {
85             netStatus = CA_INTERFACE_UP;
86         }
87         else if(status == 10)
88         {
89             netStatus = CA_INTERFACE_DOWN;
90         }
91
92         CALocalConnectivity_t *localEndpoint = CAAdapterCreateLocalEndpoint(CA_LE, address);
93         if (!localEndpoint)
94         {
95             OIC_LOG_V(ERROR, TAG, "Out of memory");
96             return;
97         }
98
99         gLENetworkChangeCallback(localEndpoint, netStatus);
100
101         CAAdapterFreeLocalEndpoint(localEndpoint);
102
103     }
104 }
105
106 CAResult_t CAInitializeLE(CARegisterConnectivityCallback registerCallback,
107         CANetworkPacketReceivedCallback reqRespCallback, CANetworkChangeCallback netCallback,
108         u_thread_pool_t handle)
109 {
110     OIC_LOG(DEBUG, TAG, "IntializeBLE");
111
112     gLEReceivedCallback = reqRespCallback;
113     gLENetworkChangeCallback = netCallback;
114
115     // register handlers
116     CAConnectivityHandler_t handler;
117     memset(&handler, 0, sizeof(CAConnectivityHandler_t));
118
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);
129
130     CALEInitialize(handle);
131     CALEServerInitialize(handle);
132
133     CALESetCallback(CALEPacketReceiveCallback);
134     CALESetNetStateCallback(CALENetStateChangeCallback);
135     CALEServerSetCallback(CALEPacketReceiveCallback);
136
137     return CA_STATUS_OK;
138 }
139
140 CAResult_t CAStartLE()
141 {
142     OIC_LOG_V(DEBUG, TAG, "CAStartLE");
143     //CANativeLEStartScan();
144
145     return CA_STATUS_OK;
146 }
147
148 CAResult_t CAStartLEListeningServer()
149 {
150     OIC_LOG_V(DEBUG, TAG, "CAStartLEListeningServer");
151
152     gIsBluetoothGattServer = JNI_TRUE;
153
154     // start gatt service
155     CALEServerStartMulticastServer();
156
157     return CA_STATUS_OK;
158 }
159
160 CAResult_t CAStartLEDiscoveryServer()
161 {
162     OIC_LOG_V(DEBUG, TAG, "CAStartLEDiscoveryServer");
163
164     gIsBluetoothGattServer = JNI_FALSE;
165
166     // start scan through gatt client
167     CALEStartMulticastServer();
168
169     return CA_STATUS_OK;
170 }
171
172 uint32_t CASendLEUnicastData(const CARemoteEndpoint_t* endpoint, void* data, uint32_t dataLen)
173 {
174
175     if(gIsBluetoothGattServer == JNI_FALSE)
176     {
177         OIC_LOG_V(DEBUG, TAG, "CALESendUnicastData");
178         CALESendUnicastMessage(endpoint->addressInfo.BT.btMacAddress, data, dataLen);
179     }
180     else if(gIsBluetoothGattServer == JNI_TRUE)
181     {
182         OIC_LOG_V(DEBUG, TAG, "CALEServerSendUnicastData");
183         CALEServerSendUnicastMessage(endpoint->addressInfo.BT.btMacAddress, data, dataLen);
184     }
185
186     return 0;
187 }
188
189 uint32_t CASendLEMulticastData(void* data, uint32_t dataLen)
190 {
191     if(gIsBluetoothGattServer == JNI_FALSE)
192     {
193         OIC_LOG_V(DEBUG, TAG, "CASendLEMulticastData");
194         CALESendMulticastMessage(data, dataLen);
195     }
196     else if(gIsBluetoothGattServer == JNI_TRUE)
197     {
198         OIC_LOG_V(DEBUG, TAG, "CALEServerSendMulticastMessage");
199         CALEServerSendMulticastMessage(data, dataLen);
200     }
201
202     return 0;
203 }
204
205 CAResult_t CAStartLENotifyServer()
206 {
207     OIC_LOG_V(DEBUG, TAG, "CAStartLENotifyServer");
208
209     return CA_STATUS_OK;
210 }
211
212 uint32_t CASendLENotification(const CARemoteEndpoint_t* endpoint, void* data, uint32_t dataLen)
213 {
214     if(gIsBluetoothGattServer == JNI_FALSE)
215     {
216         OIC_LOG_V(DEBUG, TAG, "not server mode");
217         return -1;
218     }
219     else if(gIsBluetoothGattServer == JNI_TRUE)
220     {
221         OIC_LOG_V(DEBUG, TAG, "CALEServerSendLEUnicastData");
222         CALEServerSendUnicastMessage(endpoint->addressInfo.BT.btMacAddress, data, dataLen);
223     }
224
225     return 0;
226 }
227
228 CAResult_t CAGetLEInterfaceInformation(CALocalConnectivity_t** info, uint32_t* size)
229 {
230     OIC_LOG_V(DEBUG, TAG, "CAGetLEInterfaceInformation");
231
232     CALocalConnectivity_t *netInfo = NULL;
233
234     int32_t netInfoSize = 1;
235
236     netInfo = (CALocalConnectivity_t *) OICMalloc(sizeof(CALocalConnectivity_t) * netInfoSize);
237     if(NULL == netInfo)
238     {
239         OIC_LOG_V(ERROR, TAG, "Invalid input..");
240         return CA_MEMORY_ALLOC_FAILED;
241     }
242     memset(netInfo, 0, sizeof(CALocalConnectivity_t) * netInfoSize);
243
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)
248     {
249         OIC_LOG_V(ERROR, TAG, "Failed to get interface info [%d]", ret);
250
251         OICFree(netInfo);
252         OICFree(macAddress);
253         return ret;
254     }
255
256     // Create local endpoint using util function
257     CALocalConnectivity_t *endpoint = CAAdapterCreateLocalEndpoint(CA_LE, macAddress);
258     if (NULL == endpoint)
259     {
260         OIC_LOG_V(ERROR, TAG, "Failed to create Local Endpoint!",
261                   CA_MEMORY_ALLOC_FAILED);
262         OICFree(netInfo);
263         OICFree(macAddress);
264         return CA_MEMORY_ALLOC_FAILED;
265     }
266
267     // copy unciast server information
268     endpoint->isSecured = CA_FALSE;
269     memcpy(&netInfo[0], endpoint, sizeof(CALocalConnectivity_t));
270     *size = 1;
271     *info = netInfo;
272
273     OICFree(macAddress);
274     CAAdapterFreeLocalEndpoint(endpoint);
275
276     OIC_LOG(INFO, TAG, "GetLEInterfaceInformation success");
277     OIC_LOG(DEBUG, TAG, "OUT");
278     return CA_STATUS_OK;
279 }
280
281 CAResult_t CAReadLEData()
282 {
283     OIC_LOG_V(DEBUG, TAG, "Read LE Data");
284
285     return CA_STATUS_OK;
286 }
287
288 CAResult_t CAStopLE()
289 {
290
291     if(gIsBluetoothGattServer == JNI_FALSE)
292     {
293         OIC_LOG_V(DEBUG, TAG, "CA Stop LE Scan");
294         CANativeLEStopScan();
295     }
296     else if(gIsBluetoothGattServer == JNI_TRUE)
297     {
298         OIC_LOG_V(DEBUG, TAG, "CA Stop Gatt Server");
299     }
300
301     return CA_STATUS_OK;
302 }
303
304 void CATerminateLE()
305 {
306     if(gIsBluetoothGattServer == JNI_FALSE)
307     {
308         OIC_LOG_V(DEBUG, TAG, "Terminat Gatt Client");
309         CALETerminate();
310     }
311     else if(gIsBluetoothGattServer == JNI_TRUE)
312     {
313         OIC_LOG_V(DEBUG, TAG, "Terminat Gatt Server");
314         CALEServerTerminate();
315     }
316 }