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"
30 #include "oic_malloc.h"
31 #include "oic_string.h"
33 #define CA_ADAPTER_UTILS_TAG "CA_ADAPTER_UTILS"
38 * @brief pointer to store JavaVM
40 static JavaVM *g_jvm = NULL;
44 * @brief pointer to store context for android callback interface
46 static jobject g_Context = NULL;
49 void CALogPDUData(coap_pdu_t *pdu)
51 VERIFY_NON_NULL_VOID(pdu, CA_ADAPTER_UTILS_TAG, "pdu");
52 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - payload : %s", pdu->data);
54 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - type : %d", pdu->hdr->type);
56 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - code : %d", pdu->hdr->code);
58 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - id : %d", pdu->hdr->id);
60 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - token : %s", pdu->hdr->token);
63 CALocalConnectivity_t *CAAdapterCreateLocalEndpoint(CATransportType_t type, const char *address)
65 CALocalConnectivity_t *info = (CALocalConnectivity_t *)
66 OICCalloc(1, sizeof(CALocalConnectivity_t));
69 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
74 if (address && strlen(address))
78 strncpy(info->addressInfo.BT.btMacAddress, address, CA_MACADDR_SIZE - 1);
79 info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
81 else if (CA_LE == type)
83 strncpy(info->addressInfo.LE.leMacAddress, address, CA_MACADDR_SIZE - 1);
84 info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
86 else if (CA_IPV4 == type)
88 strncpy(info->addressInfo.IP.ipAddress, address, CA_IPADDR_SIZE - 1);
89 info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
91 else if (CA_IPV6 == type)
93 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Currently IPV6 is not supported");
99 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "type is not matched with any transport!");
108 CALocalConnectivity_t *CAAdapterCopyLocalEndpoint(const CALocalConnectivity_t *connectivity)
110 VERIFY_NON_NULL_RET(connectivity, CA_ADAPTER_UTILS_TAG, "connectivity is NULL", NULL);
112 CALocalConnectivity_t *info = (CALocalConnectivity_t *)
113 OICCalloc(1, sizeof(CALocalConnectivity_t));
116 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
120 info->type = connectivity->type;
121 if (CA_EDR == info->type && strlen(connectivity->addressInfo.BT.btMacAddress))
123 strncpy(info->addressInfo.BT.btMacAddress, connectivity->addressInfo.BT.btMacAddress,
124 CA_MACADDR_SIZE - 1);
125 info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
127 else if (CA_LE == info->type && strlen(connectivity->addressInfo.LE.leMacAddress))
129 strncpy(info->addressInfo.LE.leMacAddress, connectivity->addressInfo.LE.leMacAddress,
130 CA_MACADDR_SIZE - 1);
131 info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
133 else if ((CA_IPV4 == info->type)
135 && strlen(connectivity->addressInfo.IP.ipAddress))
137 strncpy(info->addressInfo.IP.ipAddress, connectivity->addressInfo.IP.ipAddress,
139 info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
140 info->addressInfo.IP.port = connectivity->addressInfo.IP.port;
142 else if (CA_IPV6 == info->type)
144 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Currently IPV6 is not supported");
150 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "type is not matched with any transport!");
155 info->isSecured = connectivity->isSecured;
159 void CAAdapterFreeLocalEndpoint(CALocalConnectivity_t *localEndpoint)
161 OICFree(localEndpoint);
164 CARemoteEndpoint_t *CAAdapterCreateRemoteEndpoint(CATransportType_t type, const char *address,
165 const char *resourceUri)
167 CARemoteEndpoint_t *info = (CARemoteEndpoint_t *)
168 OICCalloc(1, sizeof(CARemoteEndpoint_t));
171 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
175 info->transportType = type;
176 if (address && strlen(address))
180 strncpy(info->addressInfo.BT.btMacAddress, address, CA_MACADDR_SIZE - 1);
181 info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
183 else if (CA_LE == type)
185 strncpy(info->addressInfo.LE.leMacAddress, address, CA_MACADDR_SIZE - 1);
186 info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
188 else if (CA_IPV4 == type)
190 strncpy(info->addressInfo.IP.ipAddress, address, CA_IPADDR_SIZE - 1);
191 info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
193 else if (CA_IPV6 == type)
195 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Currently IPV6 is not supported");
201 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "type is not matched with any transport!");
207 if (resourceUri && strlen(resourceUri))
209 info->resourceUri = OICStrdup(resourceUri);
215 CARemoteEndpoint_t *CAAdapterCopyRemoteEndpoint(const CARemoteEndpoint_t *remoteEndpoint)
217 VERIFY_NON_NULL_RET(remoteEndpoint, CA_ADAPTER_UTILS_TAG, "Remote endpoint is NULL", NULL);
219 CARemoteEndpoint_t *info = (CARemoteEndpoint_t *)
220 OICCalloc(1, sizeof(CARemoteEndpoint_t));
223 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
227 info->transportType = remoteEndpoint->transportType;
228 if (CA_EDR == info->transportType && ('\0' != remoteEndpoint->addressInfo.BT.btMacAddress[0]))
230 strncpy(info->addressInfo.BT.btMacAddress, remoteEndpoint->addressInfo.BT.btMacAddress,
231 CA_MACADDR_SIZE - 1);
232 info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
234 else if (CA_LE == info->transportType
235 && ('\0' != remoteEndpoint->addressInfo.LE.leMacAddress[0]))
237 strncpy(info->addressInfo.LE.leMacAddress, remoteEndpoint->addressInfo.LE.leMacAddress,
238 CA_MACADDR_SIZE - 1);
239 info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
241 else if ((CA_IPV4 == info->transportType)
242 && ('\0' != remoteEndpoint->addressInfo.IP.ipAddress[0]))
244 strncpy(info->addressInfo.IP.ipAddress, remoteEndpoint->addressInfo.IP.ipAddress,
246 info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
247 info->addressInfo.IP.port = remoteEndpoint->addressInfo.IP.port;
249 else if (CA_IPV6 == info->transportType)
251 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Currently IPV6 is not supported");
255 OIC_LOG(DEBUG, CA_ADAPTER_UTILS_TAG, "Its not matching. May be multicast.");
258 //For Multicast, remote address will be null while resourceUri will have the service UUID
260 if (remoteEndpoint->resourceUri && strlen(remoteEndpoint->resourceUri))
262 info->resourceUri = OICStrdup(remoteEndpoint->resourceUri);
265 info->isSecured = remoteEndpoint->isSecured;
269 void CAAdapterFreeRemoteEndpoint(CARemoteEndpoint_t *remoteEndpoint)
273 OICFree(remoteEndpoint->resourceUri);
274 OICFree(remoteEndpoint);
278 CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
279 size_t ipAddrLen, uint16_t *port)
281 if (!ipAddr || !isdigit(ipAddrStr[0]) || !port)
283 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "invalid param");
284 return CA_STATUS_INVALID_PARAM;
288 uint8_t dotCount = 0;
294 if (isdigit(*ipAddrStr))
296 if(index >= ipAddrLen)
298 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "invalid param");
299 return CA_STATUS_INVALID_PARAM;
302 ipAddr[index] += *ipAddrStr - '0';
304 else if (*ipAddrStr == '.')
317 if (*ipAddrStr == ':')
322 if (isdigit(*ipAddrStr))
325 *port += *ipAddrStr - '0';
339 return CA_STATUS_FAILED;
342 bool CAAdapterIsSameSubnet(const char *ipAddress1, const char *ipAddress2, const char *netMask)
344 VERIFY_NON_NULL_RET(ipAddress1, CA_ADAPTER_UTILS_TAG, "First address", false);
345 VERIFY_NON_NULL_RET(ipAddress2, CA_ADAPTER_UTILS_TAG, "Second address", false);
346 VERIFY_NON_NULL_RET(netMask, CA_ADAPTER_UTILS_TAG, "netMask", false);
348 uint8_t ipList1[IPV4_ADDR_ONE_OCTECT_LEN] = { 0 };
349 uint8_t ipList2[IPV4_ADDR_ONE_OCTECT_LEN] = { 0 };
350 uint8_t maskList[IPV4_ADDR_ONE_OCTECT_LEN] = { 0 };
351 CAResult_t ret = CA_STATUS_OK;
353 /* Local Loopback Address */
354 if (0 == strncmp(ipAddress1, "127.", 4) || 0 == strncmp(ipAddress2, "127.", 4))
359 uint16_t parsedPort = 0;
360 ret = CAParseIPv4AddressInternal(ipAddress1, ipList1, sizeof(ipList1), &parsedPort);
361 if (ret != CA_STATUS_OK)
363 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "First ip address parse fail %d", ret);
367 ret = CAParseIPv4AddressInternal(ipAddress2, ipList2, sizeof(ipList2), &parsedPort);
368 if (ret != CA_STATUS_OK)
370 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Second ip address parse fail %d", ret);
374 ret = CAParseIPv4AddressInternal(netMask, maskList, sizeof(maskList), &parsedPort);
375 if (ret != CA_STATUS_OK)
377 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Net mask parse fail %d", ret);
381 return ((ipList1[0] & maskList[0]) == (ipList2[0] & maskList[0])) && ((ipList1[1] & maskList[1])
382 == (ipList2[1] & maskList[1]))
383 && ((ipList1[2] & maskList[2]) == (ipList2[2] & maskList[2]))
384 && ((ipList1[3] & maskList[3]) == (ipList2[3] & maskList[3]));
388 bool CAIsMulticastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
389 const char *multicastAddress, uint16_t port)
391 VERIFY_NON_NULL_RET(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null", false);
392 VERIFY_NON_NULL_RET(ipAddress, CA_ADAPTER_UTILS_TAG, "ipAddress is null", false);
393 VERIFY_NON_NULL_RET(multicastAddress, CA_ADAPTER_UTILS_TAG, "multicastAddress is null", false);
395 uint32_t listLength = u_arraylist_length(serverInfoList);
396 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
398 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
401 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Info is NULL");
405 if (info->isMulticastServer && (strncmp(info->ipAddress, multicastAddress,
406 strlen(multicastAddress) == 0))
407 && (info->port == port) && (strncmp(info->ifAddr, ipAddress, strlen(ipAddress)) == 0))
409 return info->isServerStarted;
415 bool CAIsUnicastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
418 VERIFY_NON_NULL_RET(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null", false);
419 VERIFY_NON_NULL_RET(ipAddress, CA_ADAPTER_UTILS_TAG, "ipAddress is null", false);
421 uint32_t listLength = u_arraylist_length(serverInfoList);
422 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
424 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
430 if (!info->isMulticastServer && (strncmp(info->ipAddress, ipAddress,
431 strlen(ipAddress)) == 0)
432 && (info->port == port))
434 return info->isServerStarted;
440 uint16_t CAGetServerPort(const u_arraylist_t *serverInfoList, const char *ipAddress, bool isSecured)
442 VERIFY_NON_NULL_RET(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null", 0);
443 VERIFY_NON_NULL_RET(ipAddress, CA_ADAPTER_UTILS_TAG, "ipAddress is null", 0);
445 uint32_t listLength = u_arraylist_length(serverInfoList);
446 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
448 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
453 if ((strncmp(info->ipAddress, ipAddress, strlen(ipAddress)) == 0) &&
454 (info->isSecured == isSecured))
463 int CAGetSocketFdForUnicastServer(const u_arraylist_t *serverInfoList, const char *ipAddress,
464 bool isSecured, bool isMulticast, CATransportType_t type)
466 VERIFY_NON_NULL_RET(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null", -1);
467 VERIFY_NON_NULL_RET(ipAddress, CA_ADAPTER_UTILS_TAG, "ipAddress is null", -1);
469 uint32_t listLength = u_arraylist_length(serverInfoList);
471 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
473 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
479 if (!CAAdapterIsSameSubnet(info->ipAddress, ipAddress, info->subNetMask))
484 if (!info->isMulticastServer && (info->isSecured == isSecured))
486 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG,
487 "CAGetSocketFdForServer found socket [%d]", info->socketFd);
488 return info->socketFd;
493 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG,
494 "CAGetSocketFdForServer socket fd is not found");
498 CAResult_t CAAddServerInfo(u_arraylist_t *serverInfoList, CAServerInfo_t *info)
500 VERIFY_NON_NULL(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null");
501 VERIFY_NON_NULL(info, CA_ADAPTER_UTILS_TAG, "info is null");
503 CAResult_t result = u_arraylist_add(serverInfoList, (void *) info);
504 if (CA_STATUS_OK != result)
506 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "u_arraylist_add failed!");
511 void CARemoveServerInfo(u_arraylist_t *serverInfoList, int sockFd)
513 VERIFY_NON_NULL_VOID(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null");
515 uint32_t listLength = u_arraylist_length(serverInfoList);
516 for (uint32_t listIndex = 0; listIndex < listLength;)
518 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
525 if (info->socketFd == sockFd)
527 if (u_arraylist_remove(serverInfoList, listIndex))
534 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "u_arraylist_remove failed!");
545 void CAClearNetInterfaceInfoList(u_arraylist_t *infoList)
547 uint32_t listLength = u_arraylist_length(infoList);
548 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
550 CANetInfo_t *netInfo = (CANetInfo_t *) u_arraylist_get(infoList, listIndex);
557 u_arraylist_free(&infoList);
560 void CAClearServerInfoList(u_arraylist_t *serverInfoList)
562 uint32_t listLength = u_arraylist_length(serverInfoList);
563 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
565 CAServerInfo_t *serverInfo = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
572 u_arraylist_free(&serverInfoList);
576 void CANativeJNISetContext(JNIEnv *env, jobject context)
578 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetContext");
582 OIC_LOG(DEBUG, CA_ADAPTER_UTILS_TAG, "context is null");
586 g_Context = (*env)->NewGlobalRef(env, context);
589 void CANativeJNISetJavaVM(JavaVM *jvm)
591 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetJavaVM");
595 jobject CANativeJNIGetContext()
600 JavaVM *CANativeJNIGetJavaVM()