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"
30 #include <sys/socket.h>
31 #include <netinet/in.h>
39 #define CA_ADAPTER_UTILS_TAG "CA_ADAPTER_UTILS"
44 * @brief pointer to store JavaVM
46 static JavaVM *g_jvm = NULL;
50 * @brief pointer to store context for android callback interface
52 static jobject g_Context = NULL;
55 void CALogPDUData(coap_pdu_t *pdu)
57 VERIFY_NON_NULL_VOID(pdu, CA_ADAPTER_UTILS_TAG, "pdu");
58 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - payload : %s", pdu->data);
60 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - type : %d", pdu->hdr->type);
62 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - code : %d", pdu->hdr->code);
64 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - id : %d", pdu->hdr->id);
66 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - token : %s", pdu->hdr->token);
70 CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
71 size_t ipAddrLen, uint16_t *port)
73 if (!ipAddr || !isdigit(ipAddrStr[0]) || !port)
75 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "invalid param");
76 return CA_STATUS_INVALID_PARAM;
86 if (isdigit(*ipAddrStr))
88 if(index >= ipAddrLen)
90 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "invalid param");
91 return CA_STATUS_INVALID_PARAM;
94 ipAddr[index] += *ipAddrStr - '0';
96 else if (*ipAddrStr == '.')
109 if (*ipAddrStr == ':')
114 if (isdigit(*ipAddrStr))
117 *port += *ipAddrStr - '0';
131 return CA_STATUS_FAILED;
134 #else // not with_arduino
136 * These two conversion functions return void because errors can't happen
137 * (because of NI_NUMERIC), and there's nothing to do if they do happen.
139 void CAConvertAddrToName(const struct sockaddr_storage *sockAddr, char *host, uint16_t *port)
141 VERIFY_NON_NULL_VOID(sockAddr, CA_ADAPTER_UTILS_TAG, "sockAddr is null");
142 VERIFY_NON_NULL_VOID(host, CA_ADAPTER_UTILS_TAG, "host is null");
143 VERIFY_NON_NULL_VOID(port, CA_ADAPTER_UTILS_TAG, "port is null");
145 int r = getnameinfo((struct sockaddr *)sockAddr,
146 sizeof (struct sockaddr_storage),
147 host, MAX_ADDR_STR_SIZE_CA,
149 NI_NUMERICHOST|NI_NUMERICSERV);
154 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
155 "getaddrinfo failed: errno %s", strerror(errno));
159 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
160 "getaddrinfo failed: %s", gai_strerror(r));
164 *port = ntohs(((struct sockaddr_in *)sockAddr)->sin_port); // IPv4 and IPv6
167 void CAConvertNameToAddr(const char *host, uint16_t port, struct sockaddr_storage *sockaddr)
169 VERIFY_NON_NULL_VOID(host, CA_ADAPTER_UTILS_TAG, "host is null");
170 VERIFY_NON_NULL_VOID(sockaddr, CA_ADAPTER_UTILS_TAG, "sockaddr is null");
172 struct addrinfo *addrs;
173 struct addrinfo hints = { .ai_family = AF_UNSPEC,
174 .ai_socktype = SOCK_DGRAM,
175 .ai_flags = AI_NUMERICHOST };
177 int r = getaddrinfo(host, NULL, &hints, &addrs);
182 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
183 "getaddrinfo failed: errno %s", strerror(errno));
187 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
188 "getaddrinfo failed: %s", gai_strerror(r));
192 // assumption: in this case, getaddrinfo will only return one addrinfo
193 // or first is the one we want.
194 if (addrs[0].ai_family == AF_INET6)
196 memcpy(sockaddr, addrs[0].ai_addr, sizeof (struct sockaddr_in6));
197 ((struct sockaddr_in6 *)sockaddr)->sin6_port = htons(port);
201 memcpy(sockaddr, addrs[0].ai_addr, sizeof (struct sockaddr_in));
202 ((struct sockaddr_in *)sockaddr)->sin_port = htons(port);
206 #endif // WITH_ARDUINO
209 void CANativeJNISetContext(JNIEnv *env, jobject context)
211 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetContext");
215 OIC_LOG(DEBUG, CA_ADAPTER_UTILS_TAG, "context is null");
219 g_Context = (*env)->NewGlobalRef(env, context);
222 void CANativeJNISetJavaVM(JavaVM *jvm)
224 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetJavaVM");
228 jobject CANativeJNIGetContext()
233 JavaVM *CANativeJNIGetJavaVM()