Drop arduino support: SINGLE_THREAD
[iotivity.git] / resource / csdk / connectivity / src / tcp_adapter / catcpadapter.c
1 /* ****************************************************************
2  *
3  * Copyright 2015 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 "iotivity_config.h"
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdint.h>
26 #include <inttypes.h>
27
28 #ifndef __STDC_FORMAT_MACROS
29 #define __STDC_FORMAT_MACROS
30 #endif
31
32 #include "cainterface.h"
33 #include "caipnwmonitor.h"
34 #include "catcpadapter.h"
35 #include "catcpinterface.h"
36 #include "caqueueingthread.h"
37 #include "caadapterutils.h"
38 #include "octhread.h"
39 #include "uarraylist.h"
40 #include "caremotehandler.h"
41 #include "experimental/logger.h"
42 #include "oic_malloc.h"
43 #ifdef __WITH_TLS__
44 #include "ca_adapter_net_ssl.h"
45 #endif
46 #include "iotivity_debug.h"
47
48 /**
49  * Logging tag for module name.
50  */
51 #define TAG "OIC_CA_TCP_ADAP"
52
53 /**
54  * Holds internal thread TCP data information.
55  */
56 typedef struct
57 {
58     CAEndpoint_t *remoteEndpoint;
59     void *data;
60     size_t dataLen;
61     bool isMulticast;
62     bool encryptedData;
63 } CATCPData;
64
65 #define CA_TCP_LISTEN_BACKLOG  3
66
67 #define CA_TCP_SELECT_TIMEOUT 10
68
69 /**
70  * Queue handle for Send Data.
71  */
72 static CAQueueingThread_t *g_sendQueueHandle = NULL;
73
74 /**
75  * Network Packet Received Callback to CA.
76  */
77 static CANetworkPacketReceivedCallback g_networkPacketCallback = NULL;
78
79 /**
80  * Adapter Changed Callback to CA.
81  */
82 static CAAdapterChangeCallback g_networkChangeCallback = NULL;
83
84 /**
85  * Connection Changed Callback to CA.
86  */
87 static CAConnectionChangeCallback g_connectionChangeCallback = NULL;
88
89 /**
90  * error Callback to CA adapter.
91  */
92 static CAErrorHandleCallback g_errorCallback = NULL;
93
94 /**
95  * KeepAlive Connected or Disconnected Callback to CA adapter.
96  */
97 static CAKeepAliveConnectionCallback g_connKeepAliveCallback = NULL;
98
99 static void CATCPPacketReceivedCB(const CASecureEndpoint_t *sep,
100                                   const void *data, size_t dataLength);
101
102 static void CATCPErrorHandler(const CAEndpoint_t *endpoint, const void *data,
103                               size_t dataLength, CAResult_t result);
104
105 static CAResult_t CATCPInitializeQueueHandles(void);
106
107 static void CATCPDeinitializeQueueHandles(void);
108
109 static void CATCPSendDataThread(void *threadData);
110
111 static CATCPData *CACreateTCPData(const CAEndpoint_t *remoteEndpoint,
112                                   const void *data, size_t dataLength,
113                                   bool isMulticast, bool encryptedData);
114 void CAFreeTCPData(CATCPData *ipData);
115
116 static void CADataDestroyer(void *data, uint32_t size);
117
118 static int32_t CAQueueTCPData(bool isMulticast, const CAEndpoint_t *endpoint,
119                              const void *data, size_t dataLength, bool encryptedData);
120
121 CAResult_t CATCPInitializeQueueHandles(void)
122 {
123     // Check if the message queue is already initialized
124     if (g_sendQueueHandle)
125     {
126         OIC_LOG(DEBUG, TAG, "send queue handle is already initialized!");
127         return CA_STATUS_OK;
128     }
129
130     // Create send message queue
131     g_sendQueueHandle = OICMalloc(sizeof(CAQueueingThread_t));
132     if (!g_sendQueueHandle)
133     {
134         OIC_LOG(ERROR, TAG, "Memory allocation failed!");
135         return CA_MEMORY_ALLOC_FAILED;
136     }
137
138     if (CA_STATUS_OK != CAQueueingThreadInitialize(g_sendQueueHandle,
139                                 (const ca_thread_pool_t)caglobals.tcp.threadpool,
140                                 CATCPSendDataThread, CADataDestroyer))
141     {
142         OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
143         OICFree(g_sendQueueHandle);
144         g_sendQueueHandle = NULL;
145         return CA_STATUS_FAILED;
146     }
147
148     return CA_STATUS_OK;
149 }
150
151 void CATCPDeinitializeQueueHandles(void)
152 {
153     CAQueueingThreadDestroy(g_sendQueueHandle);
154     OICFree(g_sendQueueHandle);
155     g_sendQueueHandle = NULL;
156 }
157
158 void CATCPConnectionStateCB(const char *ipAddress, CANetworkStatus_t status)
159 {
160     (void)ipAddress;
161     (void)status;
162 }
163
164 void CATCPPacketReceivedCB(const CASecureEndpoint_t *sep, const void *data,
165                            size_t dataLength)
166 {
167     VERIFY_NON_NULL_VOID(sep, TAG, "sep is NULL");
168     VERIFY_NON_NULL_VOID(data, TAG, "data is NULL");
169
170     OIC_LOG_V(DEBUG, TAG, "Address: %s, port:%d", sep->endpoint.addr, sep->endpoint.port);
171
172     unsigned char *buffer = (unsigned char*)data;
173     size_t bufferLen = dataLength;
174
175     //get remote device information from file descriptor.
176     CATCPSessionInfo_t *svritem = CAGetTCPSessionInfoFromEndpoint(&sep->endpoint);
177     if (!svritem)
178     {
179         OIC_LOG(ERROR, TAG, "there is no connection information in list");
180         return;
181     }
182     if (UNKNOWN == svritem->protocol)
183     {
184         OIC_LOG(ERROR, TAG, "invalid protocol type");
185         return;
186     }
187
188     //totalLen filled only when header fully read and parsed
189     while (0 != bufferLen)
190     {
191         CAResult_t res = CAConstructCoAP(svritem, &buffer, &bufferLen);
192         if (CA_STATUS_OK != res)
193         {
194             OIC_LOG_V(ERROR, TAG, "CAConstructCoAP return error : %d", res);
195             return;
196         }
197
198         //when successfully read all required data - pass them to upper layer.
199         if (svritem->len == svritem->totalLen)
200         {
201             if (g_networkPacketCallback)
202             {
203                 g_networkPacketCallback(sep, svritem->data, svritem->totalLen);
204             }
205             CACleanData(svritem);
206         }
207         else
208         {
209             OIC_LOG_V(DEBUG, TAG, "%" PRIuPTR " bytes required for complete CoAP",
210                                 svritem->totalLen - svritem->len);
211         }
212     }
213 }
214
215 #ifdef __WITH_TLS__
216 static ssize_t CATCPPacketSendCB(CAEndpoint_t *endpoint, const void *data, size_t dataLength)
217 {
218     OIC_LOG_V(DEBUG, TAG, "In %s", __func__);
219     VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint is NULL", -1);
220     VERIFY_NON_NULL_RET(data, TAG, "data is NULL", -1);
221
222     OIC_LOG_V(DEBUG, TAG, "Address: %s, port:%d", endpoint->addr, endpoint->port);
223     OIC_LOG_BUFFER(DEBUG, TAG, data, dataLength);
224
225     ssize_t ret = CATCPSendData(endpoint, data, dataLength);
226
227     OIC_LOG_V(DEBUG, TAG, "Out %s : %" PRIdPTR " bytes sent", __func__, ret);
228     return ret;
229 }
230 #endif
231
232 static void CATCPErrorHandler(const CAEndpoint_t *endpoint, const void *data,
233                               size_t dataLength, CAResult_t result)
234 {
235     VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint is NULL");
236     VERIFY_NON_NULL_VOID(data, TAG, "data is NULL");
237
238     if (g_errorCallback)
239     {
240         g_errorCallback(endpoint, data, dataLength, result);
241     }
242 }
243
244 static void CATCPConnectionHandler(const CAEndpoint_t *endpoint, bool isConnected, bool isClient)
245 {
246     // Pass the changed connection status to RI Layer for keepalive.
247     if (g_connKeepAliveCallback)
248     {
249         g_connKeepAliveCallback(endpoint, isConnected, isClient);
250     }
251
252     // Pass the changed connection status to CAUtil.
253     if (g_connectionChangeCallback)
254     {
255         g_connectionChangeCallback(endpoint, isConnected);
256     }
257 }
258
259 void CATCPSetKeepAliveCallbacks(CAKeepAliveConnectionCallback ConnHandler)
260 {
261     g_connKeepAliveCallback = ConnHandler;
262 }
263
264 void CATCPAdapterHandler(CATransportAdapter_t adapter, CANetworkStatus_t status)
265 {
266     if (g_networkChangeCallback)
267     {
268         g_networkChangeCallback(adapter, status);
269     }
270
271     if (CA_INTERFACE_DOWN == status)
272     {
273         OIC_LOG(DEBUG, TAG, "Network status is down, close all session");
274         CATCPStopServer();
275     }
276     else if (CA_INTERFACE_UP == status)
277     {
278         OIC_LOG(DEBUG, TAG, "Network status is up, create new socket for listening");
279
280         CAResult_t ret = CA_STATUS_FAILED;
281         ret = CATCPStartServer((const ca_thread_pool_t)caglobals.tcp.threadpool);
282         if (CA_STATUS_OK != ret)
283         {
284             OIC_LOG_V(DEBUG, TAG, "CATCPStartServer failed[%d]", ret);
285         }
286     }
287 }
288
289 static void CAInitializeTCPGlobals(void)
290 {
291     caglobals.tcp.ipv4.fd = OC_INVALID_SOCKET;
292     caglobals.tcp.ipv4s.fd = OC_INVALID_SOCKET;
293     caglobals.tcp.ipv6.fd = OC_INVALID_SOCKET;
294     caglobals.tcp.ipv6s.fd = OC_INVALID_SOCKET;
295
296     // Set the port number received from application.
297     caglobals.tcp.ipv4.port = caglobals.ports.tcp.u4;
298     caglobals.tcp.ipv4s.port = caglobals.ports.tcp.u4s;
299     caglobals.tcp.ipv6.port = caglobals.ports.tcp.u6;
300     caglobals.tcp.ipv6s.port = caglobals.ports.tcp.u6s;
301
302     caglobals.tcp.selectTimeout = CA_TCP_SELECT_TIMEOUT;
303     caglobals.tcp.listenBacklog = CA_TCP_LISTEN_BACKLOG;
304
305     CATransportFlags_t flags = 0;
306     if (caglobals.client)
307     {
308         flags |= caglobals.clientFlags;
309     }
310     if (caglobals.server)
311     {
312         flags |= caglobals.serverFlags;
313     }
314
315     caglobals.tcp.ipv4tcpenabled = flags & CA_IPV4;
316     caglobals.tcp.ipv6tcpenabled = flags & CA_IPV6;
317 }
318
319 CAResult_t CAInitializeTCP(CARegisterConnectivityCallback registerCallback,
320                            CANetworkPacketReceivedCallback networkPacketCallback,
321                            CAAdapterChangeCallback netCallback,
322                            CAConnectionChangeCallback connCallback,
323                            CAErrorHandleCallback errorCallback, ca_thread_pool_t handle)
324 {
325     OIC_LOG(DEBUG, TAG, "IN");
326     VERIFY_NON_NULL(registerCallback, TAG, "registerCallback");
327     VERIFY_NON_NULL(networkPacketCallback, TAG, "networkPacketCallback");
328     VERIFY_NON_NULL(netCallback, TAG, "netCallback");
329     VERIFY_NON_NULL(handle, TAG, "thread pool handle");
330
331 #ifdef WSA_WAIT_EVENT_0
332     // Windows-specific initialization.
333     WORD wVersionRequested = MAKEWORD(2, 2);
334     WSADATA wsaData = {.wVersion = 0};
335     int err = WSAStartup(wVersionRequested, &wsaData);
336     if (0 != err)
337     {
338         OIC_LOG_V(ERROR, TAG, "%s: WSAStartup failed: %i", __func__, err);
339         return CA_STATUS_FAILED;
340     }
341     OIC_LOG(DEBUG, TAG, "WSAStartup Succeeded");
342 #endif
343
344     g_networkChangeCallback = netCallback;
345     g_connectionChangeCallback = connCallback;
346     g_networkPacketCallback = networkPacketCallback;
347     g_errorCallback = errorCallback;
348
349     CAInitializeTCPGlobals();
350     caglobals.tcp.threadpool = handle;
351
352     CATCPSetConnectionChangedCallback(CATCPConnectionHandler);
353     CATCPSetPacketReceiveCallback(CATCPPacketReceivedCB);
354     CATCPSetErrorHandler(CATCPErrorHandler);
355
356 #ifdef __WITH_TLS__
357     if (CA_STATUS_OK != CAinitSslAdapter())
358     {
359         OIC_LOG(ERROR, TAG, "Failed to init SSL adapter");
360     }
361     else
362     {
363         CAsetSslAdapterCallbacks(CATCPPacketReceivedCB, CATCPPacketSendCB, CATCPErrorHandler, CA_ADAPTER_TCP);
364     }
365 #endif
366
367     CAConnectivityHandler_t tcpHandler = {
368         .startAdapter = CAStartTCP,
369         .startListenServer = CAStartTCPListeningServer,
370         .stopListenServer = CAStopTCPListeningServer,
371         .startDiscoveryServer = CAStartTCPDiscoveryServer,
372         .sendData = CASendTCPUnicastData,
373         .sendDataToAll = CASendTCPMulticastData,
374         .GetnetInfo = CAGetTCPInterfaceInformation,
375         .readData = CAReadTCPData,
376         .stopAdapter = CAStopTCP,
377         .terminate = CATerminateTCP,
378         .cType = CA_ADAPTER_TCP};
379
380     registerCallback(tcpHandler);
381
382     OIC_LOG(INFO, TAG, "OUT IntializeTCP is Success");
383     return CA_STATUS_OK;
384 }
385
386 CAResult_t CAStartTCP()
387 {
388     OIC_LOG(DEBUG, TAG, "IN");
389
390     // Start network monitoring to receive adapter status changes.
391     CAIPStartNetworkMonitor(CATCPAdapterHandler, CA_ADAPTER_TCP);
392
393     if (CA_STATUS_OK != CATCPInitializeQueueHandles())
394     {
395         OIC_LOG(ERROR, TAG, "Failed to Initialize Queue Handle");
396         CATerminateTCP();
397         return CA_STATUS_FAILED;
398     }
399
400     // Start send queue thread
401     if (CA_STATUS_OK != CAQueueingThreadStart(g_sendQueueHandle))
402     {
403         OIC_LOG(ERROR, TAG, "Failed to Start Send Data Thread");
404         return CA_STATUS_FAILED;
405     }
406
407     return CA_STATUS_OK;
408 }
409
410 CAResult_t CAStartTCPListeningServer()
411 {
412     if (!caglobals.server)
413     {
414         caglobals.server = true;    // only needed to run CA tests
415     }
416
417     CAResult_t ret = CATCPStartServer((const ca_thread_pool_t)caglobals.tcp.threadpool);
418     if (CA_STATUS_OK != ret)
419     {
420         OIC_LOG_V(ERROR, TAG, "Failed to start listening server![%d]", ret);
421         return ret;
422     }
423
424     return CA_STATUS_OK;
425 }
426
427 CAResult_t CAStopTCPListeningServer()
428 {
429     return CA_STATUS_OK;
430 }
431
432 CAResult_t CAStartTCPDiscoveryServer()
433 {
434     if (!caglobals.client)
435     {
436         caglobals.client = true;    // only needed to run CA tests
437     }
438
439     CAResult_t ret = CATCPStartServer((const ca_thread_pool_t)caglobals.tcp.threadpool);
440     if (CA_STATUS_OK != ret)
441     {
442         OIC_LOG_V(ERROR, TAG, "Failed to start discovery server![%d]", ret);
443         return ret;
444     }
445
446     return CA_STATUS_OK;
447 }
448
449 static int32_t CAQueueTCPData(bool isMulticast, const CAEndpoint_t *endpoint,
450                              const void *data, size_t dataLength, bool encryptedData)
451 {
452     VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", -1);
453     VERIFY_NON_NULL_RET(data, TAG, "data", -1);
454     VERIFY_TRUE_RET((dataLength <= INT_MAX) && (dataLength > 0),
455                     TAG,
456                     "Invalid Data Length",
457                     -1);
458     VERIFY_NON_NULL_RET(g_sendQueueHandle, TAG, "sendQueueHandle", -1);
459
460     // Create TCPData to add to queue
461     CATCPData *tcpData = CACreateTCPData(endpoint, data, dataLength, isMulticast, encryptedData);
462     if (!tcpData)
463     {
464         OIC_LOG(ERROR, TAG, "Failed to create ipData!");
465         return -1;
466     }
467     // Add message to send queue
468     CAQueueingThreadAddData(g_sendQueueHandle, tcpData, sizeof(CATCPData));
469
470     return (int32_t)dataLength;
471 }
472
473 int32_t CASendTCPUnicastData(const CAEndpoint_t *endpoint,
474                              const void *data, uint32_t dataLength,
475                              CADataType_t dataType)
476 {
477     OIC_LOG(DEBUG, TAG, "IN");
478     (void)dataType;
479     if ((0 == dataLength) || (dataLength > INT32_MAX))
480     {
481         OIC_LOG(ERROR, TAG, "Invalid Data Length");
482         return -1;
483     }
484
485     return CAQueueTCPData(false, endpoint, data, dataLength, false);
486 }
487
488 int32_t CASendTCPMulticastData(const CAEndpoint_t *endpoint,
489                                const void *data, uint32_t dataLength,
490                                CADataType_t dataType)
491 {
492     (void)dataType;
493     return CAQueueTCPData(true, endpoint, data, dataLength, false);
494 }
495
496 CAResult_t CAReadTCPData()
497 {
498     OIC_LOG(DEBUG, TAG, "IN");
499     return CA_STATUS_OK;
500 }
501
502 CAResult_t CAStopTCP()
503 {
504     CAIPStopNetworkMonitor(CA_ADAPTER_TCP);
505
506     if (g_sendQueueHandle && g_sendQueueHandle->threadMutex)
507     {
508         CAQueueingThreadStop(g_sendQueueHandle);
509     }
510     CATCPDeinitializeQueueHandles();
511
512     CATCPStopServer();
513
514     //Re-initializing the Globals to start them again
515     CAInitializeTCPGlobals();
516
517 #ifdef __WITH_TLS__
518     CAdeinitSslAdapter();
519 #endif
520
521     return CA_STATUS_OK;
522 }
523
524 void CATerminateTCP()
525 {
526 #ifdef __WITH_TLS__
527     CAsetSslAdapterCallbacks(NULL, NULL, NULL, CA_ADAPTER_TCP);
528 #endif
529     CAStopTCP();
530     CATCPSetPacketReceiveCallback(NULL);
531
532 #ifdef WSA_WAIT_EVENT_0
533     // Windows-specific clean-up.
534     OC_VERIFY(0 == WSACleanup());
535 #endif
536 }
537
538 void CATCPSendDataThread(void *threadData)
539 {
540     CATCPData *tcpData = (CATCPData *) threadData;
541     if (!tcpData)
542     {
543         OIC_LOG(DEBUG, TAG, "Invalid TCP data!");
544         return;
545     }
546
547     if (caglobals.tcp.terminate)
548     {
549         OIC_LOG(DEBUG, TAG, "Adapter is not enabled");
550         CATCPErrorHandler(tcpData->remoteEndpoint, tcpData->data, tcpData->dataLen,
551                           CA_SEND_FAILED);
552         return;
553     }
554
555     if (tcpData->isMulticast)
556     {
557         //Processing for sending multicast
558         OIC_LOG(DEBUG, TAG, "Send Multicast Data is called, not supported");
559         return;
560     }
561     else
562     {
563         if (!tcpData->encryptedData)
564         {
565             // Check payload length from CoAP over TCP format header.
566             size_t payloadLen = CACheckPayloadLengthFromHeader(tcpData->data, tcpData->dataLen);
567             if (!payloadLen)
568             {
569                 // if payload length is zero, disconnect from remote device.
570                 OIC_LOG(DEBUG, TAG, "payload length is zero, disconnect from remote device");
571 #ifdef __WITH_TLS__
572                 if (CA_STATUS_OK != CAcloseSslConnection(tcpData->remoteEndpoint))
573                 {
574                     OIC_LOG(ERROR, TAG, "Failed to close TLS session");
575                 }
576 #endif
577                 CASearchAndDeleteTCPSession(tcpData->remoteEndpoint);
578                 return;
579             }
580
581 #ifdef __WITH_TLS__
582             CAResult_t result = CA_STATUS_OK;
583             if (tcpData->remoteEndpoint && tcpData->remoteEndpoint->flags & CA_SECURE)
584             {
585                 OIC_LOG(DEBUG, TAG, "CAencryptSsl called!");
586                 result = CAencryptSsl(tcpData->remoteEndpoint, tcpData->data, tcpData->dataLen);
587
588                 if (CA_STATUS_OK != result)
589                 {
590                     OIC_LOG(ERROR, TAG, "CAAdapterNetDtlsEncrypt failed!");
591                     CASearchAndDeleteTCPSession(tcpData->remoteEndpoint);
592                     CATCPErrorHandler(tcpData->remoteEndpoint, tcpData->data, tcpData->dataLen,
593                                       CA_SEND_FAILED);
594                 }
595                 OIC_LOG_V(DEBUG, TAG,
596                           "CAAdapterNetDtlsEncrypt returned with result[%d]", result);
597                return;
598             }
599 #endif
600         }
601
602         //Processing for sending unicast
603         ssize_t dlen = CATCPSendData(tcpData->remoteEndpoint, tcpData->data, tcpData->dataLen);
604         if (-1 == dlen)
605         {
606             OIC_LOG(ERROR, TAG, "CATCPSendData failed");
607             CASearchAndDeleteTCPSession(tcpData->remoteEndpoint);
608             CATCPErrorHandler(tcpData->remoteEndpoint, tcpData->data, tcpData->dataLen,
609                               CA_SEND_FAILED);
610         }
611     }
612 }
613
614 CATCPData *CACreateTCPData(const CAEndpoint_t *remoteEndpoint, const void *data,
615                            size_t dataLength, bool isMulticast, bool encryptedData)
616 {
617     VERIFY_NON_NULL_RET(remoteEndpoint, TAG, "remoteEndpoint is NULL", NULL);
618     VERIFY_NON_NULL_RET(data, TAG, "data is NULL", NULL);
619
620     CATCPData *tcpData = (CATCPData *) OICCalloc(1, sizeof(*tcpData));
621     if (!tcpData)
622     {
623         OIC_LOG(ERROR, TAG, "Memory allocation failed!");
624         return NULL;
625     }
626
627     tcpData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
628     tcpData->data = (void *) OICMalloc(dataLength);
629     if (!tcpData->data)
630     {
631         OIC_LOG(ERROR, TAG, "Memory allocation failed!");
632         CAFreeTCPData(tcpData);
633         return NULL;
634     }
635
636     memcpy(tcpData->data, data, dataLength);
637     tcpData->dataLen = dataLength;
638
639     tcpData->isMulticast = isMulticast;
640     tcpData->encryptedData = encryptedData;
641
642     return tcpData;
643 }
644
645 void CAFreeTCPData(CATCPData *tcpData)
646 {
647     VERIFY_NON_NULL_VOID(tcpData, TAG, "tcpData is NULL");
648
649     CAFreeEndpoint(tcpData->remoteEndpoint);
650     OICFree(tcpData->data);
651     OICFree(tcpData);
652 }
653
654 void CADataDestroyer(void *data, uint32_t size)
655 {
656     if (size < sizeof(CATCPData))
657     {
658         OIC_LOG_V(ERROR, TAG, "Destroy data too small %p %" PRIu32, data, size);
659     }
660     CATCPData *TCPData = (CATCPData *) data;
661
662     CAFreeTCPData(TCPData);
663 }
664