1 /* *****************************************************************
2 * Copyright 2015 Samsung Electronics All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 ******************************************************************/
17 #include "canfcadapter.h"
19 #include "canfcinterface.h"
20 #include "caqueueingthread.h"
21 #include "caadapterutils.h"
23 #include "uarraylist.h"
24 #include "caremotehandler.h"
26 #include "oic_malloc.h"
27 #include "oic_string.h"
30 * Logging tag for module name
32 #define TAG "NFC_ADAP"
35 * Holds inter thread nfc data information.
40 CAEndpoint_t *remoteEndpoint;
46 * Reference to threadpool.
48 static ca_thread_pool_t g_nfcThreadPool = NULL;
51 * Queue handle for Send Data
53 static CAQueueingThread_t *g_sendQueueHandle = NULL;
56 * Network Packet Received Callback to CA
58 static CANetworkPacketReceivedCallback g_packetReceivedCallback = NULL;
61 * Network Changed Callback to CA
63 static CANetworkChangeCallback g_connectionStateCallback = NULL;
66 * error Callback to CA adapter
68 static CAErrorHandleCallback g_errorCallback = NULL;
70 static void CANFCPacketReceivedCB(const CASecureEndpoint_t *endpoint, const void *data,
74 static CAResult_t CANFCInitializeQueueHandles();
76 static void CANFCDeinitializeQueueHandles();
78 static void CANFCSendDataThread(void *threadData);
80 CANFCData *CACreateNFCData(const CAEndpoint_t *remoteEndpoint, const void *data,
83 void CAFreeNFCData(CANFCData *ipData);
85 static void CANFCDataDestroyer(void *data, uint32_t size);
87 CAResult_t CANFCInitializeQueueHandles()
89 OIC_LOG(DEBUG, TAG, "IN");
91 // Check if the message queue is already initialized
92 if (g_sendQueueHandle)
94 OIC_LOG(DEBUG, TAG, "send queue handle is already initialized!");
98 // Create send message queue
99 g_sendQueueHandle = OICCalloc(1, sizeof(*g_sendQueueHandle));
100 if (!g_sendQueueHandle)
102 OIC_LOG(ERROR, TAG, "Memory allocation failed!");
103 return CA_MEMORY_ALLOC_FAILED;
107 != CAQueueingThreadInitialize(g_sendQueueHandle, (const ca_thread_pool_t) g_nfcThreadPool,
108 CANFCSendDataThread, CANFCDataDestroyer))
110 OIC_LOG(ERROR, TAG, "Failed to Initialize send queue thread");
111 OICFree(g_sendQueueHandle);
112 g_sendQueueHandle = NULL;
113 return CA_STATUS_FAILED;
116 OIC_LOG(DEBUG, TAG, "OUT");
120 void CANFCDeinitializeQueueHandles()
122 OIC_LOG(DEBUG, TAG, "IN");
124 CAQueueingThreadDestroy(g_sendQueueHandle);
125 OICFree(g_sendQueueHandle);
126 g_sendQueueHandle = NULL;
128 OIC_LOG(DEBUG, TAG, "OUT");
131 #endif // SINGLE_THREAD
132 void CANFCConnectionStateCB(const char *nfcAddress, CANetworkStatus_t status)
134 OIC_LOG(DEBUG, TAG, "Currently Not Supported");
140 void CANFCPacketReceivedCB(const CASecureEndpoint_t *sep, const void *data, uint32_t dataLength)
142 OIC_LOG(DEBUG, TAG, "IN");
144 VERIFY_NON_NULL_VOID(sep, TAG, "endpoint is NULL");
145 VERIFY_NON_NULL_VOID(data, TAG, "data is NULL");
149 OIC_LOG(ERROR, TAG, "Invalid call, data length is 0");
153 OIC_LOG_V(DEBUG, TAG, "Address: %s, port:%d", sep->endpoint.addr, sep->endpoint.port);
155 if (g_packetReceivedCallback)
157 g_packetReceivedCallback(sep, data, dataLength);
160 OIC_LOG(DEBUG, TAG, "OUT");
163 void CANFCErrorHandler(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLength,
166 OIC_LOG(DEBUG, TAG, "IN");
168 VERIFY_NON_NULL_VOID(endpoint, TAG, "endpoint is NULL");
170 VERIFY_NON_NULL_VOID(data, TAG, "data is NULL");
173 g_errorCallback(endpoint, data, dataLength, result);
175 OIC_LOG(DEBUG, TAG, "OUT");
178 CAResult_t CAInitializeNFC(CARegisterConnectivityCallback registerCallback,
179 CANetworkPacketReceivedCallback packetReceivedCallback,
180 CANetworkChangeCallback connectionStateCallback,
181 CAErrorHandleCallback errorCallback, ca_thread_pool_t handle)
183 OIC_LOG(DEBUG, TAG, "IN");
184 VERIFY_NON_NULL(registerCallback, TAG, "registerCallback");
185 VERIFY_NON_NULL(packetReceivedCallback, TAG, "packetReceivedCallback");
186 VERIFY_NON_NULL(connectionStateCallback, TAG, "connectionStateCallback");
187 VERIFY_NON_NULL(handle, TAG, "thread pool handle");
189 g_connectionStateCallback = connectionStateCallback;
190 g_packetReceivedCallback = packetReceivedCallback;
191 g_errorCallback = errorCallback;
193 g_nfcThreadPool = handle;
195 CAResult_t result = CAInitializeNfcServer();
196 if (CA_STATUS_OK != result)
198 OIC_LOG(ERROR, TAG, "CAInitializeNfcServer failed");
202 CANFCSetPacketReceiveCallback(CANFCPacketReceivedCB);
203 static const CAConnectivityHandler_t nfcHandler =
205 .startAdapter = CAStartNFC,
206 .stopAdapter = CAStopNFC,
207 .startListenServer = CAStartNFCListeningServer,
208 .stopListenServer = CAStopNFCListeningServer,
209 .startDiscoveryServer = CAStartNFCDiscoveryServer,
210 .sendData = CASendNFCUnicastData,
211 .sendDataToAll = CASendNFCMulticastData,
212 .GetnetInfo = CAGetNFCInterfaceInformation,
213 .readData = CAReadNFCData,
214 .terminate = CATerminateNFC,
215 .cType = CA_ADAPTER_NFC
217 registerCallback(nfcHandler);
219 OIC_LOG(INFO, TAG, "OUT Intialize NFC is Success");
223 CAResult_t CAStartNFC()
226 OIC_LOG(DEBUG, TAG, "IN");
227 CAResult_t res = CANFCInitializeQueueHandles();
229 if (CA_STATUS_OK != res)
231 OIC_LOG(ERROR, TAG, "Failed to Initialize Queue Handle");
235 res = CAQueueingThreadStart(g_sendQueueHandle);
236 // Start send queue thread
237 if (CA_STATUS_OK != res)
239 OIC_LOG(ERROR, TAG, "Failed to Start Send Data Thread");
243 OIC_LOG(DEBUG, TAG, "OUT");
247 CAResult_t CAStartNFCListeningServer()
249 OIC_LOG(DEBUG, TAG, "IN");
251 CAResult_t ret = CANFCStartServer();
252 if (CA_STATUS_OK != ret)
254 OIC_LOG_V(ERROR, TAG, "Failed to start server![%d]", ret);
258 OIC_LOG(DEBUG, TAG, "OUT");
262 CAResult_t CAStopNFCListeningServer()
268 CAResult_t CAStartNFCDiscoveryServer()
270 return CAStartNFCListeningServer();
273 static int32_t CAQueueNFCData(const CAEndpoint_t *endpoint, const void *data,
276 OIC_LOG(DEBUG, TAG, "IN");
278 VERIFY_NON_NULL_RET(endpoint, TAG, "remoteEndpoint", -1);
279 VERIFY_NON_NULL_RET(data, TAG, "data", -1);
283 OIC_LOG(ERROR, TAG, "Invalid Data Length");
287 VERIFY_NON_NULL_RET(g_sendQueueHandle, TAG, "sendQueueHandle", -1);
288 // Create NFCData to add to queue
289 CANFCData *nfcData = CACreateNFCData(endpoint, data, dataLength);
292 OIC_LOG(ERROR, TAG, "Failed to create ipData!");
295 // Add message to send queue
296 CAQueueingThreadAddData(g_sendQueueHandle, nfcData, sizeof(CANFCData));
298 OIC_LOG(DEBUG, TAG, "OUT");
303 int32_t CASendNFCUnicastData(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLength)
305 return CAQueueNFCData(endpoint, data, dataLength);
308 int32_t CASendNFCMulticastData(const CAEndpoint_t *endpoint, const void *data, uint32_t dataLength)
310 return CAQueueNFCData(endpoint, data, dataLength);
313 CAResult_t CAReadNFCData()
316 OIC_LOG(DEBUG, TAG, "IN");
318 OIC_LOG(DEBUG, TAG, "OUT");
323 CAResult_t CAStopNFC()
325 OIC_LOG(DEBUG, TAG, "IN");
327 if (g_sendQueueHandle && g_sendQueueHandle->threadMutex)
329 CAQueueingThreadStop(g_sendQueueHandle);
332 CANFCDeinitializeQueueHandles();
336 OIC_LOG(DEBUG, TAG, "OUT");
341 void CATerminateNFC()
344 OIC_LOG(DEBUG, TAG, "IN");
345 CANFCSetPacketReceiveCallback(NULL);
346 CANFCDeinitializeQueueHandles();
348 OIC_LOG(DEBUG, TAG, "OUT");
351 void CANFCSendDataThread(void *threadData)
353 OIC_LOG(DEBUG, TAG, "IN");
354 CANFCData *nfcData = (CANFCData *) threadData;
357 OIC_LOG(ERROR, TAG, "Invalid ip data!");
361 CANFCSendData(nfcData->remoteEndpoint, nfcData->data, nfcData->dataLen);
362 OIC_LOG(DEBUG, TAG, "OUT");
365 CANFCData *CACreateNFCData(const CAEndpoint_t *remoteEndpoint, const void *data,
368 VERIFY_NON_NULL_RET(data, TAG, "NFCData is NULL", NULL);
370 CANFCData *nfcData = (CANFCData *) OICCalloc(1,sizeof(*nfcData));
373 OIC_LOG(ERROR, TAG, "Memory allocation failed!");
377 nfcData->remoteEndpoint = CACloneEndpoint(remoteEndpoint);
378 nfcData->data = (void *) OICMalloc(dataLength);
381 OIC_LOG(ERROR, TAG, "Memory allocation failed!");
382 CAFreeNFCData(nfcData);
386 memcpy(nfcData->data, data, dataLength);
387 nfcData->dataLen = dataLength;
393 void CAFreeNFCData(CANFCData *nfcData)
397 CAFreeEndpoint(nfcData->remoteEndpoint);
398 OICFree(nfcData->data);
403 void CANFCDataDestroyer(void *data, uint32_t size)
405 CANFCData *nfcData = (CANFCData *) data;
407 CAFreeNFCData(nfcData);
411 CAResult_t CAGetNFCInterfaceInformation(CAEndpoint_t **info, uint32_t *size)
413 OIC_LOG(DEBUG, TAG, "CAGetNFCInterfaceInformation not supported");
416 OIC_LOG(ERROR, TAG, "CAGetNFCInterfaceInformation : Invalid Input");
417 return CA_STATUS_FAILED;