1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
21 #include "caadapterutils.h"
25 #include "oic_string.h"
26 #include "oic_malloc.h"
32 #define CA_ADAPTER_UTILS_TAG "CA_ADAPTER_UTILS"
37 * @brief pointer to store JavaVM
39 static JavaVM *g_jvm = NULL;
43 * @brief pointer to store context for android callback interface
45 static jobject g_Context = NULL;
48 void CALogPDUData(coap_pdu_t *pdu)
50 VERIFY_NON_NULL_VOID(pdu, CA_ADAPTER_UTILS_TAG, "pdu");
51 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - payload : %s", pdu->data);
53 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - type : %d", pdu->hdr->type);
55 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - code : %d", pdu->hdr->code);
57 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - id : %d", pdu->hdr->id);
59 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - token : %s", pdu->hdr->token);
62 CALocalConnectivity_t *CAAdapterCreateLocalEndpoint(CATransportType_t type, const char *address)
64 CALocalConnectivity_t *info = (CALocalConnectivity_t *)
65 OICCalloc(1, sizeof(CALocalConnectivity_t));
68 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
73 if (address && strlen(address))
77 OICStrcpy(info->addressInfo.BT.btMacAddress, sizeof(info->addressInfo.BT.btMacAddress),
80 else if (CA_LE == type)
82 OICStrcpy(info->addressInfo.LE.leMacAddress, sizeof(info->addressInfo.LE.leMacAddress),
85 else if (CA_IPV4 == type)
87 OICStrcpy(info->addressInfo.IP.ipAddress, sizeof(info->addressInfo.IP.ipAddress),
90 else if (CA_IPV6 == type)
92 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Currently IPV6 is not supported");
98 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "type is not matched with any transport!");
107 CALocalConnectivity_t *CAAdapterCopyLocalEndpoint(const CALocalConnectivity_t *connectivity)
109 VERIFY_NON_NULL_RET(connectivity, CA_ADAPTER_UTILS_TAG, "connectivity is NULL", NULL);
111 CALocalConnectivity_t *info = (CALocalConnectivity_t *)
112 OICCalloc(1, sizeof(CALocalConnectivity_t));
115 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
119 info->type = connectivity->type;
120 if (CA_EDR == info->type && strlen(connectivity->addressInfo.BT.btMacAddress))
122 OICStrcpy(info->addressInfo.BT.btMacAddress, sizeof(info->addressInfo.BT.btMacAddress),
123 connectivity->addressInfo.BT.btMacAddress);
125 else if (CA_LE == info->type && strlen(connectivity->addressInfo.LE.leMacAddress))
127 OICStrcpy(info->addressInfo.LE.leMacAddress, sizeof(info->addressInfo.LE.leMacAddress),
128 connectivity->addressInfo.LE.leMacAddress);
130 else if ((CA_IPV4 == info->type)
132 && strlen(connectivity->addressInfo.IP.ipAddress))
134 OICStrcpy(info->addressInfo.IP.ipAddress, sizeof(info->addressInfo.IP.ipAddress),
135 connectivity->addressInfo.IP.ipAddress);
136 info->addressInfo.IP.port = connectivity->addressInfo.IP.port;
138 else if (CA_IPV6 == info->type)
140 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Currently IPV6 is not supported");
146 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "type is not matched with any transport!");
151 info->isSecured = connectivity->isSecured;
155 void CAAdapterFreeLocalEndpoint(CALocalConnectivity_t *localEndpoint)
157 OICFree(localEndpoint);
160 CARemoteEndpoint_t *CAAdapterCreateRemoteEndpoint(CATransportType_t type, const char *address,
161 const char *resourceUri)
163 CARemoteEndpoint_t *info = (CARemoteEndpoint_t *)
164 OICCalloc(1, sizeof(CARemoteEndpoint_t));
167 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
171 info->transportType = type;
172 if (address && strlen(address))
176 OICStrcpy(info->addressInfo.BT.btMacAddress, sizeof(info->addressInfo.BT.btMacAddress),
179 else if (CA_LE == type)
181 OICStrcpy(info->addressInfo.LE.leMacAddress, sizeof(info->addressInfo.LE.leMacAddress),
184 else if (CA_IPV4 == type)
186 OICStrcpy(info->addressInfo.IP.ipAddress, sizeof(info->addressInfo.IP.ipAddress),
189 else if (CA_IPV6 == type)
191 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Currently IPV6 is not supported");
197 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "type is not matched with any transport!");
203 if (resourceUri && strlen(resourceUri))
205 info->resourceUri = OICStrdup(resourceUri);
211 CARemoteEndpoint_t *CAAdapterCopyRemoteEndpoint(const CARemoteEndpoint_t *remoteEndpoint)
213 VERIFY_NON_NULL_RET(remoteEndpoint, CA_ADAPTER_UTILS_TAG, "Remote endpoint is NULL", NULL);
215 CARemoteEndpoint_t *info = (CARemoteEndpoint_t *)
216 OICCalloc(1, sizeof(CARemoteEndpoint_t));
219 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
223 info->transportType = remoteEndpoint->transportType;
224 if (CA_EDR == info->transportType && ('\0' != remoteEndpoint->addressInfo.BT.btMacAddress[0]))
226 OICStrcpy(info->addressInfo.BT.btMacAddress, sizeof(info->addressInfo.BT.btMacAddress),
227 remoteEndpoint->addressInfo.BT.btMacAddress);
229 else if (CA_LE == info->transportType
230 && ('\0' != remoteEndpoint->addressInfo.LE.leMacAddress[0]))
232 OICStrcpy(info->addressInfo.LE.leMacAddress, sizeof(info->addressInfo.LE.leMacAddress),
233 remoteEndpoint->addressInfo.LE.leMacAddress);
235 else if ((CA_IPV4 == info->transportType)
236 && ('\0' != remoteEndpoint->addressInfo.IP.ipAddress[0]))
238 OICStrcpy(info->addressInfo.IP.ipAddress, sizeof(info->addressInfo.IP.ipAddress),
239 remoteEndpoint->addressInfo.IP.ipAddress);
240 info->addressInfo.IP.port = remoteEndpoint->addressInfo.IP.port;
242 else if (CA_IPV6 == info->transportType)
244 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Currently IPV6 is not supported");
248 OIC_LOG(DEBUG, CA_ADAPTER_UTILS_TAG, "Its not matching. May be multicast.");
251 //For Multicast, remote address will be null while resourceUri will have the service UUID
253 if (remoteEndpoint->resourceUri && strlen(remoteEndpoint->resourceUri))
255 info->resourceUri = OICStrdup(remoteEndpoint->resourceUri);
258 info->isSecured = remoteEndpoint->isSecured;
259 info->identity = remoteEndpoint->identity;
263 void CAAdapterFreeRemoteEndpoint(CARemoteEndpoint_t *remoteEndpoint)
267 OICFree(remoteEndpoint->resourceUri);
268 OICFree(remoteEndpoint);
272 CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
273 size_t ipAddrLen, uint16_t *port)
275 if (!ipAddr || !isdigit(ipAddrStr[0]) || !port)
277 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "invalid param");
278 return CA_STATUS_INVALID_PARAM;
282 uint8_t dotCount = 0;
288 if (isdigit(*ipAddrStr))
290 if(index >= ipAddrLen)
292 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "invalid param");
293 return CA_STATUS_INVALID_PARAM;
296 ipAddr[index] += *ipAddrStr - '0';
298 else if (*ipAddrStr == '.')
311 if (*ipAddrStr == ':')
316 if (isdigit(*ipAddrStr))
319 *port += *ipAddrStr - '0';
333 return CA_STATUS_FAILED;
336 bool CAAdapterIsSameSubnet(const char *ipAddress1, const char *ipAddress2, const char *netMask)
338 VERIFY_NON_NULL_RET(ipAddress1, CA_ADAPTER_UTILS_TAG, "First address", false);
339 VERIFY_NON_NULL_RET(ipAddress2, CA_ADAPTER_UTILS_TAG, "Second address", false);
340 VERIFY_NON_NULL_RET(netMask, CA_ADAPTER_UTILS_TAG, "netMask", false);
342 uint8_t ipList1[IPV4_ADDR_ONE_OCTECT_LEN] = { 0 };
343 uint8_t ipList2[IPV4_ADDR_ONE_OCTECT_LEN] = { 0 };
344 uint8_t maskList[IPV4_ADDR_ONE_OCTECT_LEN] = { 0 };
345 CAResult_t ret = CA_STATUS_OK;
347 /* Local Loopback Address */
348 if (0 == strncmp(ipAddress1, "127.", 4) || 0 == strncmp(ipAddress2, "127.", 4))
353 uint16_t parsedPort = 0;
354 ret = CAParseIPv4AddressInternal(ipAddress1, ipList1, sizeof(ipList1), &parsedPort);
355 if (ret != CA_STATUS_OK)
357 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "First ip address parse fail %d", ret);
361 ret = CAParseIPv4AddressInternal(ipAddress2, ipList2, sizeof(ipList2), &parsedPort);
362 if (ret != CA_STATUS_OK)
364 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Second ip address parse fail %d", ret);
368 ret = CAParseIPv4AddressInternal(netMask, maskList, sizeof(maskList), &parsedPort);
369 if (ret != CA_STATUS_OK)
371 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Net mask parse fail %d", ret);
375 return ((ipList1[0] & maskList[0]) == (ipList2[0] & maskList[0])) && ((ipList1[1] & maskList[1])
376 == (ipList2[1] & maskList[1]))
377 && ((ipList1[2] & maskList[2]) == (ipList2[2] & maskList[2]))
378 && ((ipList1[3] & maskList[3]) == (ipList2[3] & maskList[3]));
382 bool CAIsMulticastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
383 const char *multicastAddress, uint16_t port)
385 VERIFY_NON_NULL_RET(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null", false);
386 VERIFY_NON_NULL_RET(ipAddress, CA_ADAPTER_UTILS_TAG, "ipAddress is null", false);
387 VERIFY_NON_NULL_RET(multicastAddress, CA_ADAPTER_UTILS_TAG, "multicastAddress is null", false);
389 uint32_t listLength = u_arraylist_length(serverInfoList);
390 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
392 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
395 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Info is NULL");
399 if (info->isMulticastServer && (strncmp(info->ipAddress, multicastAddress,
400 strlen(multicastAddress) == 0))
401 && (info->port == port) && (strncmp(info->ifAddr, ipAddress, strlen(ipAddress)) == 0))
403 return info->isServerStarted;
409 bool CAIsUnicastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
412 VERIFY_NON_NULL_RET(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null", false);
413 VERIFY_NON_NULL_RET(ipAddress, CA_ADAPTER_UTILS_TAG, "ipAddress is null", false);
415 uint32_t listLength = u_arraylist_length(serverInfoList);
416 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
418 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
424 if (!info->isMulticastServer && (strncmp(info->ipAddress, ipAddress,
425 strlen(ipAddress)) == 0)
426 && (info->port == port))
428 return info->isServerStarted;
434 uint16_t CAGetServerPort(const u_arraylist_t *serverInfoList, const char *ipAddress, bool isSecured)
436 VERIFY_NON_NULL_RET(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null", 0);
437 VERIFY_NON_NULL_RET(ipAddress, CA_ADAPTER_UTILS_TAG, "ipAddress is null", 0);
439 uint32_t listLength = u_arraylist_length(serverInfoList);
440 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
442 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
447 if ((strncmp(info->ipAddress, ipAddress, strlen(ipAddress)) == 0) &&
448 (info->isSecured == isSecured))
457 int CAGetSocketFdForUnicastServer(const u_arraylist_t *serverInfoList, const char *ipAddress,
458 bool isSecured, bool isMulticast, CATransportType_t type)
460 VERIFY_NON_NULL_RET(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null", -1);
461 VERIFY_NON_NULL_RET(ipAddress, CA_ADAPTER_UTILS_TAG, "ipAddress is null", -1);
463 uint32_t listLength = u_arraylist_length(serverInfoList);
465 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
467 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
473 if (!CAAdapterIsSameSubnet(info->ipAddress, ipAddress, info->subNetMask))
478 if (!info->isMulticastServer && (info->isSecured == isSecured))
480 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG,
481 "CAGetSocketFdForServer found socket [%d]", info->socketFd);
482 return info->socketFd;
487 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG,
488 "CAGetSocketFdForServer socket fd is not found");
492 CAResult_t CAAddServerInfo(u_arraylist_t *serverInfoList, CAServerInfo_t *info)
494 VERIFY_NON_NULL(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null");
495 VERIFY_NON_NULL(info, CA_ADAPTER_UTILS_TAG, "info is null");
497 CAResult_t result = u_arraylist_add(serverInfoList, (void *) info);
498 if (CA_STATUS_OK != result)
500 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "u_arraylist_add failed!");
505 void CARemoveServerInfo(u_arraylist_t *serverInfoList, int sockFd)
507 VERIFY_NON_NULL_VOID(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null");
509 uint32_t listLength = u_arraylist_length(serverInfoList);
510 for (uint32_t listIndex = 0; listIndex < listLength;)
512 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
519 if (info->socketFd == sockFd)
521 if (u_arraylist_remove(serverInfoList, listIndex))
528 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "u_arraylist_remove failed!");
539 void CAClearNetInterfaceInfoList(u_arraylist_t *infoList)
541 uint32_t listLength = u_arraylist_length(infoList);
542 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
544 CANetInfo_t *netInfo = (CANetInfo_t *) u_arraylist_get(infoList, listIndex);
551 u_arraylist_free(&infoList);
554 void CAClearServerInfoList(u_arraylist_t *serverInfoList)
556 uint32_t listLength = u_arraylist_length(serverInfoList);
557 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
559 CAServerInfo_t *serverInfo = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
566 u_arraylist_free(&serverInfoList);
570 void CANativeJNISetContext(JNIEnv *env, jobject context)
572 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetContext");
576 OIC_LOG(DEBUG, CA_ADAPTER_UTILS_TAG, "context is null");
580 g_Context = (*env)->NewGlobalRef(env, context);
583 void CANativeJNISetJavaVM(JavaVM *jvm)
585 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetJavaVM");
589 jobject CANativeJNIGetContext()
594 JavaVM *CANativeJNIGetJavaVM()