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;
266 info->identity = remoteEndpoint->identity;
270 void CAAdapterFreeRemoteEndpoint(CARemoteEndpoint_t *remoteEndpoint)
274 OICFree(remoteEndpoint->resourceUri);
275 OICFree(remoteEndpoint);
279 CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
280 size_t ipAddrLen, uint16_t *port)
282 if (!ipAddr || !isdigit(ipAddrStr[0]) || !port)
284 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "invalid param");
285 return CA_STATUS_INVALID_PARAM;
289 uint8_t dotCount = 0;
295 if (isdigit(*ipAddrStr))
297 if(index >= ipAddrLen)
299 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "invalid param");
300 return CA_STATUS_INVALID_PARAM;
303 ipAddr[index] += *ipAddrStr - '0';
305 else if (*ipAddrStr == '.')
318 if (*ipAddrStr == ':')
323 if (isdigit(*ipAddrStr))
326 *port += *ipAddrStr - '0';
340 return CA_STATUS_FAILED;
343 bool CAAdapterIsSameSubnet(const char *ipAddress1, const char *ipAddress2, const char *netMask)
345 VERIFY_NON_NULL_RET(ipAddress1, CA_ADAPTER_UTILS_TAG, "First address", false);
346 VERIFY_NON_NULL_RET(ipAddress2, CA_ADAPTER_UTILS_TAG, "Second address", false);
347 VERIFY_NON_NULL_RET(netMask, CA_ADAPTER_UTILS_TAG, "netMask", false);
349 uint8_t ipList1[IPV4_ADDR_ONE_OCTECT_LEN] = { 0 };
350 uint8_t ipList2[IPV4_ADDR_ONE_OCTECT_LEN] = { 0 };
351 uint8_t maskList[IPV4_ADDR_ONE_OCTECT_LEN] = { 0 };
352 CAResult_t ret = CA_STATUS_OK;
354 /* Local Loopback Address */
355 if (0 == strncmp(ipAddress1, "127.", 4) || 0 == strncmp(ipAddress2, "127.", 4))
360 uint16_t parsedPort = 0;
361 ret = CAParseIPv4AddressInternal(ipAddress1, ipList1, sizeof(ipList1), &parsedPort);
362 if (ret != CA_STATUS_OK)
364 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "First ip address parse fail %d", ret);
368 ret = CAParseIPv4AddressInternal(ipAddress2, ipList2, sizeof(ipList2), &parsedPort);
369 if (ret != CA_STATUS_OK)
371 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Second ip address parse fail %d", ret);
375 ret = CAParseIPv4AddressInternal(netMask, maskList, sizeof(maskList), &parsedPort);
376 if (ret != CA_STATUS_OK)
378 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Net mask parse fail %d", ret);
382 return ((ipList1[0] & maskList[0]) == (ipList2[0] & maskList[0])) && ((ipList1[1] & maskList[1])
383 == (ipList2[1] & maskList[1]))
384 && ((ipList1[2] & maskList[2]) == (ipList2[2] & maskList[2]))
385 && ((ipList1[3] & maskList[3]) == (ipList2[3] & maskList[3]));
389 bool CAIsMulticastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
390 const char *multicastAddress, uint16_t port)
392 VERIFY_NON_NULL_RET(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null", false);
393 VERIFY_NON_NULL_RET(ipAddress, CA_ADAPTER_UTILS_TAG, "ipAddress is null", false);
394 VERIFY_NON_NULL_RET(multicastAddress, CA_ADAPTER_UTILS_TAG, "multicastAddress is null", false);
396 uint32_t listLength = u_arraylist_length(serverInfoList);
397 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
399 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
402 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Info is NULL");
406 if (info->isMulticastServer && (strncmp(info->ipAddress, multicastAddress,
407 strlen(multicastAddress) == 0))
408 && (info->port == port) && (strncmp(info->ifAddr, ipAddress, strlen(ipAddress)) == 0))
410 return info->isServerStarted;
416 bool CAIsUnicastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
419 VERIFY_NON_NULL_RET(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null", false);
420 VERIFY_NON_NULL_RET(ipAddress, CA_ADAPTER_UTILS_TAG, "ipAddress is null", false);
422 uint32_t listLength = u_arraylist_length(serverInfoList);
423 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
425 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
431 if (!info->isMulticastServer && (strncmp(info->ipAddress, ipAddress,
432 strlen(ipAddress)) == 0)
433 && (info->port == port))
435 return info->isServerStarted;
441 uint16_t CAGetServerPort(const u_arraylist_t *serverInfoList, const char *ipAddress, bool isSecured)
443 VERIFY_NON_NULL_RET(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null", 0);
444 VERIFY_NON_NULL_RET(ipAddress, CA_ADAPTER_UTILS_TAG, "ipAddress is null", 0);
446 uint32_t listLength = u_arraylist_length(serverInfoList);
447 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
449 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
454 if ((strncmp(info->ipAddress, ipAddress, strlen(ipAddress)) == 0) &&
455 (info->isSecured == isSecured))
464 int CAGetSocketFdForUnicastServer(const u_arraylist_t *serverInfoList, const char *ipAddress,
465 bool isSecured, bool isMulticast, CATransportType_t type)
467 VERIFY_NON_NULL_RET(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null", -1);
468 VERIFY_NON_NULL_RET(ipAddress, CA_ADAPTER_UTILS_TAG, "ipAddress is null", -1);
470 uint32_t listLength = u_arraylist_length(serverInfoList);
472 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
474 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
480 if (!CAAdapterIsSameSubnet(info->ipAddress, ipAddress, info->subNetMask))
485 if (!info->isMulticastServer && (info->isSecured == isSecured))
487 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG,
488 "CAGetSocketFdForServer found socket [%d]", info->socketFd);
489 return info->socketFd;
494 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG,
495 "CAGetSocketFdForServer socket fd is not found");
499 CAResult_t CAAddServerInfo(u_arraylist_t *serverInfoList, CAServerInfo_t *info)
501 VERIFY_NON_NULL(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null");
502 VERIFY_NON_NULL(info, CA_ADAPTER_UTILS_TAG, "info is null");
504 CAResult_t result = u_arraylist_add(serverInfoList, (void *) info);
505 if (CA_STATUS_OK != result)
507 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "u_arraylist_add failed!");
512 void CARemoveServerInfo(u_arraylist_t *serverInfoList, int sockFd)
514 VERIFY_NON_NULL_VOID(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null");
516 uint32_t listLength = u_arraylist_length(serverInfoList);
517 for (uint32_t listIndex = 0; listIndex < listLength;)
519 CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
526 if (info->socketFd == sockFd)
528 if (u_arraylist_remove(serverInfoList, listIndex))
535 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "u_arraylist_remove failed!");
546 void CAClearNetInterfaceInfoList(u_arraylist_t *infoList)
548 uint32_t listLength = u_arraylist_length(infoList);
549 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
551 CANetInfo_t *netInfo = (CANetInfo_t *) u_arraylist_get(infoList, listIndex);
558 u_arraylist_free(&infoList);
561 void CAClearServerInfoList(u_arraylist_t *serverInfoList)
563 uint32_t listLength = u_arraylist_length(serverInfoList);
564 for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
566 CAServerInfo_t *serverInfo = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
573 u_arraylist_free(&serverInfoList);
577 void CANativeJNISetContext(JNIEnv *env, jobject context)
579 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetContext");
583 OIC_LOG(DEBUG, CA_ADAPTER_UTILS_TAG, "context is null");
587 g_Context = (*env)->NewGlobalRef(env, context);
590 void CANativeJNISetJavaVM(JavaVM *jvm)
592 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetJavaVM");
596 jobject CANativeJNIGetContext()
601 JavaVM *CANativeJNIGetJavaVM()