Revert "Adding error handling in IP Adapter"
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / cainterfacecontroller.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 #include <inttypes.h>
25
26 #include "cainterfacecontroller.h"
27 #include "caipadapter.h"
28 #include "caedradapter.h"
29 #include "caleadapter.h"
30 #include "canetworkconfigurator.h"
31 #include "caremotehandler.h"
32 #include "oic_malloc.h"
33 #include "logger.h"
34 #include "cathreadpool.h"
35
36 #define TAG PCF("CA")
37
38 #define CA_MEMORY_ALLOC_CHECK(arg) {if (arg == NULL) \
39     {OIC_LOG(ERROR, TAG, "memory error");goto memory_error_exit;} }
40
41 #define CA_TRANSPORT_TYPE_NUM   4
42
43 static CAConnectivityHandler_t g_adapterHandler[CA_TRANSPORT_TYPE_NUM];
44
45 static CANetworkPacketReceivedCallback g_networkPacketReceivedCallback = NULL;
46
47 static CANetworkChangeCallback g_networkChangeCallback = NULL;
48
49 static int CAGetAdapterIndex(CATransportType_t cType)
50 {
51     switch (cType)
52     {
53         case CA_IPV4:
54             return 0;
55         case CA_IPV6:
56             return 1;
57         case CA_EDR:
58             return 2;
59         case CA_LE:
60             return 3;
61     }
62
63     OIC_LOG(DEBUG, TAG, "CA_TRANSPORT_TYPE_NUM is not 4");
64
65     return -1;
66 }
67
68 static void CARegisterCallback(CAConnectivityHandler_t handler, CATransportType_t cType)
69 {
70     OIC_LOG(DEBUG, TAG, "CARegisterCallback - Entry");
71
72     if(handler.startAdapter == NULL ||
73         handler.startListenServer == NULL ||
74         handler.startDiscoveryServer == NULL ||
75         handler.sendData == NULL ||
76         handler.sendDataToAll == NULL ||
77         handler.GetnetInfo == NULL ||
78         handler.readData == NULL ||
79         handler.stopAdapter == NULL ||
80         handler.terminate == NULL)
81     {
82         OIC_LOG(ERROR, TAG, "connectivity handler is not enough to be used!");
83         return;
84     }
85
86     int index = CAGetAdapterIndex(cType);
87
88     if (index == -1)
89     {
90         OIC_LOG(ERROR, TAG, "unknown connectivity type!");
91         return;
92     }
93
94     memcpy(&g_adapterHandler[index], &handler, sizeof(CAConnectivityHandler_t));
95
96     OIC_LOG_V(DEBUG, TAG, "%d type adapter, register complete!", cType);
97 }
98
99 static void CAReceivedPacketCallback(CARemoteEndpoint_t *endpoint, void *data,
100                                      uint32_t dataLen)
101 {
102     OIC_LOG(DEBUG, TAG, "receivedPacketCallback in interface controller");
103
104     // Call the callback.
105     if (g_networkPacketReceivedCallback != NULL)
106     {
107         g_networkPacketReceivedCallback(endpoint, data, dataLen);
108     }
109     else
110     {
111         OICFree(endpoint);
112         endpoint = NULL;
113         OICFree(data);
114         data = NULL;
115
116         OIC_LOG(ERROR, TAG, "network packet received callback is NULL!");
117     }
118 }
119
120 static void CANetworkChangedCallback(CALocalConnectivity_t *info,
121                                      CANetworkStatus_t status)
122 {
123     OIC_LOG(DEBUG, TAG, "Network Changed callback");
124
125     // Call the callback.
126     if (g_networkChangeCallback != NULL)
127     {
128         g_networkChangeCallback(info, status);
129     }
130 }
131
132 void CAInitializeAdapters(ca_thread_pool_t handle)
133 {
134     OIC_LOG(DEBUG, TAG, "initialize adapters..");
135
136     memset(g_adapterHandler, 0, sizeof(CAConnectivityHandler_t) * CA_TRANSPORT_TYPE_NUM);
137
138     // Initialize adapters and register callback.
139 #ifdef IP_ADAPTER
140     CAInitializeIP(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
141                          handle);
142 #endif /* IP_ADAPTER */
143
144 #ifdef EDR_ADAPTER
145     CAInitializeEDR(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
146                     handle);
147 #endif /* EDR_ADAPTER */
148
149 #ifdef LE_ADAPTER
150     CAInitializeLE(CARegisterCallback, CAReceivedPacketCallback, CANetworkChangedCallback,
151                    handle);
152 #endif /* LE_ADAPTER */
153
154 }
155
156 void CASetPacketReceivedCallback(CANetworkPacketReceivedCallback callback)
157 {
158     OIC_LOG(DEBUG, TAG, "Set packet received callback");
159
160     g_networkPacketReceivedCallback = callback;
161 }
162
163 void CASetNetworkChangeCallback(CANetworkChangeCallback callback)
164 {
165     OIC_LOG(DEBUG, TAG, "Set network change callback");
166
167     g_networkChangeCallback = callback;
168 }
169
170 CAResult_t CAStartAdapter(CATransportType_t transportType)
171 {
172     OIC_LOG_V(DEBUG, TAG, "Start the adapter of CAConnectivityType[%d]", transportType);
173
174     int index = CAGetAdapterIndex(transportType);
175
176     if (index == -1)
177     {
178         OIC_LOG(ERROR, TAG, "unknown connectivity type!");
179         return CA_STATUS_FAILED;
180     }
181
182     if (g_adapterHandler[index].startAdapter != NULL)
183     {
184         g_adapterHandler[index].startAdapter();
185     }
186
187     return CA_STATUS_OK;
188 }
189
190 void CAStopAdapter(CATransportType_t transportType)
191 {
192     OIC_LOG_V(DEBUG, TAG, "Stop the adapter of CATransportType[%d]", transportType);
193
194     int index = CAGetAdapterIndex(transportType);
195
196     if (index == -1)
197     {
198         OIC_LOG(ERROR, TAG, "unknown transport type!");
199         return;
200     }
201
202     if (g_adapterHandler[index].stopAdapter != NULL)
203     {
204         g_adapterHandler[index].stopAdapter();
205     }
206 }
207
208 CAResult_t CAGetNetworkInfo(CALocalConnectivity_t **info, uint32_t *size)
209 {
210     if (info == NULL || size == NULL)
211     {
212         return CA_STATUS_INVALID_PARAM;
213     }
214
215     CALocalConnectivity_t *tempInfo[CA_TRANSPORT_TYPE_NUM] = { 0 };
216     uint32_t tempSize[CA_TRANSPORT_TYPE_NUM] = { 0 };
217
218     CAResult_t res = CA_STATUS_FAILED;
219     uint32_t resSize = 0;
220     for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
221     {
222         if (g_adapterHandler[index].GetnetInfo != NULL)
223         {
224             // #1. get information for each adapter
225             res = g_adapterHandler[index].GetnetInfo(&tempInfo[index],
226                                                      &tempSize[index]);
227
228             // #2. total size
229             if (res == CA_STATUS_OK)
230             {
231                 resSize += tempSize[index];
232             }
233
234             OIC_LOG_V(DEBUG,
235                       TAG,
236                       "%d adapter network info size is %" PRIu32 " res:%d",
237                       index,
238                       tempSize[index],
239                       res);
240         }
241     }
242
243     OIC_LOG_V(DEBUG, TAG, "network info total size is %d!", resSize);
244
245     if (resSize == 0)
246     {
247         if (res == CA_ADAPTER_NOT_ENABLED || res == CA_NOT_SUPPORTED)
248         {
249             return res;
250         }
251         return CA_STATUS_FAILED;
252     }
253
254     // #3. add data into result
255     // memory allocation
256
257     CALocalConnectivity_t * resInfo = OICCalloc(resSize, sizeof(*resInfo));
258     CA_MEMORY_ALLOC_CHECK(resInfo);
259
260     // #4. save data
261     *info = resInfo;
262     *size = resSize;
263
264     for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
265     {
266         // check information
267         if (tempSize[index] == 0)
268         {
269             continue;
270         }
271
272         memcpy(resInfo,
273                tempInfo[index],
274                sizeof(*resInfo) * tempSize[index]);
275
276         resInfo += tempSize[index];
277
278         // free adapter data
279         OICFree(tempInfo[index]);
280         tempInfo[index] = NULL;
281     }
282
283     OIC_LOG(DEBUG, TAG, "each network info save success!");
284     return CA_STATUS_OK;
285
286     // memory error label.
287 memory_error_exit:
288
289     for (int index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
290     {
291
292         OICFree(tempInfo[index]);
293         tempInfo[index] = NULL;
294     }
295
296     return CA_MEMORY_ALLOC_FAILED;
297 }
298
299 CAResult_t CASendUnicastData(const CARemoteEndpoint_t *endpoint, const void *data, uint32_t length)
300 {
301     OIC_LOG(DEBUG, TAG, "Send unicast data to enabled interface..");
302
303     CAResult_t res = CA_STATUS_FAILED;
304
305     if (endpoint == NULL)
306     {
307         OIC_LOG(DEBUG, TAG, "Invalid endpoint");
308         return CA_STATUS_INVALID_PARAM;
309     }
310
311     CATransportType_t type = endpoint->transportType;
312
313     int index = CAGetAdapterIndex(type);
314
315     if (index == -1)
316     {
317         OIC_LOG(ERROR, TAG, "unknown transport type!");
318         return CA_STATUS_INVALID_PARAM;
319     }
320
321     uint32_t sentDataLen = 0;
322
323     if (g_adapterHandler[index].sendData != NULL)
324     {
325         sentDataLen = g_adapterHandler[index].sendData(endpoint, data, length);
326     }
327
328     if (sentDataLen != -1)
329     {
330         res = CA_STATUS_OK;
331     }
332     return res;
333 }
334
335 CAResult_t CASendMulticastData(const void *data, uint32_t length)
336 {
337     OIC_LOG(DEBUG, TAG, "Send multicast data to enabled interface..");
338
339     CAResult_t res = CA_STATUS_FAILED;
340     u_arraylist_t *list = CAGetSelectedNetworkList();
341
342     if (!list)
343     {
344         OIC_LOG(DEBUG, TAG, "No selected network");
345         return CA_STATUS_FAILED;
346     }
347
348     int i = 0;
349     for (i = 0; i < u_arraylist_length(list); i++)
350     {
351         void* ptrType = u_arraylist_get(list, i);
352
353         if(ptrType == NULL)
354         {
355             continue;
356         }
357
358         CATransportType_t connType = *(CATransportType_t *) ptrType;
359
360         int index = CAGetAdapterIndex(connType);
361
362         if (index == -1)
363         {
364             OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
365             continue;
366         }
367
368         uint32_t sentDataLen = 0;
369
370         if (g_adapterHandler[index].sendDataToAll != NULL)
371         {
372             void *payload = (void *) OICMalloc(length);
373             if (!payload)
374             {
375                 OIC_LOG(ERROR, TAG, "Out of memory!");
376                 return CA_MEMORY_ALLOC_FAILED;
377             }
378             memcpy(payload, data, length);
379             sentDataLen = g_adapterHandler[index].sendDataToAll(payload, length);
380             OICFree(payload);
381         }
382
383         if (sentDataLen == length)
384         {
385            res = CA_STATUS_OK;
386         }
387         else
388         {
389             OIC_LOG(ERROR, TAG, "sendDataToAll failed!");
390         }
391     }
392
393     return res;
394 }
395
396 CAResult_t CAStartListeningServerAdapters()
397 {
398     OIC_LOG(DEBUG, TAG, "Start listening server from adapters..");
399
400     u_arraylist_t *list = CAGetSelectedNetworkList();
401     if (!list)
402     {
403         OIC_LOG(ERROR, TAG, "No selected network");
404         return CA_STATUS_FAILED;
405     }
406
407     int i = 0;
408     for (i = 0; i < u_arraylist_length(list); i++)
409     {
410         void* ptrType = u_arraylist_get(list, i);
411
412         if(ptrType == NULL)
413         {
414             continue;
415         }
416
417         CATransportType_t connType = *(CATransportType_t *) ptrType;
418
419         int index = CAGetAdapterIndex(connType);
420         if (index == -1)
421         {
422             OIC_LOG(ERROR, TAG, "unknown connectivity type!");
423             continue;
424         }
425
426         if (g_adapterHandler[index].startListenServer != NULL)
427         {
428             g_adapterHandler[index].startListenServer();
429         }
430     }
431
432     return CA_STATUS_OK;
433 }
434
435 CAResult_t CAStartDiscoveryServerAdapters()
436 {
437     OIC_LOG(DEBUG, TAG, "Start discovery server from adapters..");
438
439     u_arraylist_t *list = CAGetSelectedNetworkList();
440
441     if (!list)
442     {
443         OIC_LOG(ERROR, TAG, "No selected network");
444         return CA_STATUS_FAILED;
445     }
446
447     int i = 0;
448     for (i = 0; i < u_arraylist_length(list); i++)
449     {
450         void* ptrType = u_arraylist_get(list, i);
451
452         if(ptrType == NULL)
453         {
454             continue;
455         }
456
457         CATransportType_t connType = *(CATransportType_t *) ptrType;
458
459         int index = CAGetAdapterIndex(connType);
460
461         if (index == -1)
462         {
463             OIC_LOG(DEBUG, TAG, "unknown connectivity type!");
464             continue;
465         }
466
467         if (g_adapterHandler[index].startDiscoveryServer != NULL)
468         {
469             g_adapterHandler[index].startDiscoveryServer();
470         }
471     }
472
473     return CA_STATUS_OK;
474 }
475
476 void CATerminateAdapters()
477 {
478     OIC_LOG(DEBUG, TAG, "terminate all adapters..");
479
480     uint32_t index;
481     for (index = 0; index < CA_TRANSPORT_TYPE_NUM; index++)
482     {
483         if (g_adapterHandler[index].terminate != NULL)
484         {
485             g_adapterHandler[index].terminate();
486         }
487     }
488 }