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 "iotivity_config.h"
22 #include "caadapterutils.h"
26 #include "oic_string.h"
27 #include "oic_malloc.h"
30 #ifdef HAVE_WS2TCPIP_H
33 #ifdef HAVE_SYS_SOCKET_H
34 #include <sys/socket.h>
36 #ifdef HAVE_NETINET_IN_H
37 #include <netinet/in.h>
39 #if defined(HAVE_WINSOCK2_H) && defined(HAVE_WS2TCPIP_H)
51 #define CA_ADAPTER_UTILS_TAG "OIC_CA_ADAP_UTILS"
56 * @brief pointer to store JavaVM
58 static JavaVM *g_jvm = NULL;
62 * @brief pointer to store context for android callback interface
64 static jobject g_Context = NULL;
65 static jobject g_Activity = NULL;
69 CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
70 size_t ipAddrLen, uint16_t *port)
72 if (!ipAddr || !isdigit(ipAddrStr[0]) || !port)
74 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "invalid param");
75 return CA_STATUS_INVALID_PARAM;
85 if (isdigit(*ipAddrStr))
87 if(index >= ipAddrLen)
89 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "invalid param");
90 return CA_STATUS_INVALID_PARAM;
93 ipAddr[index] += *ipAddrStr - '0';
95 else if (*ipAddrStr == '.')
108 if (*ipAddrStr == ':')
113 if (isdigit(*ipAddrStr))
116 *port += *ipAddrStr - '0';
130 return CA_STATUS_FAILED;
133 #else // not with_arduino
135 * These two conversion functions return void because errors can't happen
136 * (because of NI_NUMERIC), and there's nothing to do if they do happen.
138 void CAConvertAddrToName(const struct sockaddr_storage *sockAddr, socklen_t sockAddrLen,
139 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,
147 host, MAX_ADDR_STR_SIZE_CA,
149 NI_NUMERICHOST|NI_NUMERICSERV);
152 #if defined(EAI_SYSTEM)
155 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
156 "getnameinfo failed: errno %s", strerror(errno));
160 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
161 "getnameinfo failed: %s", gai_strerror(r));
163 #elif defined(_WIN32)
164 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
165 "getnameinfo failed: errno %i", WSAGetLastError());
167 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
168 "getnameinfo failed: %s", gai_strerror(r));
172 *port = ntohs(((struct sockaddr_in *)sockAddr)->sin_port); // IPv4 and IPv6
175 void CAConvertNameToAddr(const char *host, uint16_t port, struct sockaddr_storage *sockaddr)
177 VERIFY_NON_NULL_VOID(host, CA_ADAPTER_UTILS_TAG, "host is null");
178 VERIFY_NON_NULL_VOID(sockaddr, CA_ADAPTER_UTILS_TAG, "sockaddr is null");
180 struct addrinfo *addrs = NULL;
181 struct addrinfo hints = { .ai_family = AF_UNSPEC,
182 .ai_socktype = SOCK_DGRAM,
183 .ai_flags = AI_NUMERICHOST };
185 int r = getaddrinfo(host, NULL, &hints, &addrs);
188 #if defined(EAI_SYSTEM)
191 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
192 "getaddrinfo failed: errno %s", strerror(errno));
196 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
197 "getaddrinfo failed: %s", gai_strerror(r));
199 #elif defined(_WIN32)
200 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
201 "getaddrinfo failed: errno %i", WSAGetLastError());
203 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
204 "getaddrinfo failed: %s", gai_strerror(r));
208 // assumption: in this case, getaddrinfo will only return one addrinfo
209 // or first is the one we want.
210 if (addrs[0].ai_family == AF_INET6)
212 memcpy(sockaddr, addrs[0].ai_addr, sizeof (struct sockaddr_in6));
213 ((struct sockaddr_in6 *)sockaddr)->sin6_port = htons(port);
217 memcpy(sockaddr, addrs[0].ai_addr, sizeof (struct sockaddr_in));
218 ((struct sockaddr_in *)sockaddr)->sin_port = htons(port);
222 #endif // WITH_ARDUINO
225 void CANativeJNISetContext(JNIEnv *env, jobject context)
227 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetContext");
231 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "context is null");
237 g_Context = (*env)->NewGlobalRef(env, context);
241 OIC_LOG(INFO, CA_ADAPTER_UTILS_TAG, "context is already set");
245 void CANativeJNISetJavaVM(JavaVM *jvm)
247 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetJavaVM");
251 jobject CANativeJNIGetContext()
256 JavaVM *CANativeJNIGetJavaVM()
261 void CANativeSetActivity(JNIEnv *env, jobject activity)
263 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeSetActivity");
267 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "activity is null");
273 g_Activity = (*env)->NewGlobalRef(env, activity);
277 OIC_LOG(INFO, CA_ADAPTER_UTILS_TAG, "activity is already set");
281 jobject *CANativeGetActivity()
286 jmethodID CAGetJNIMethodID(JNIEnv *env, const char* className,
287 const char* methodName,
288 const char* methodFormat)
290 VERIFY_NON_NULL_RET(env, CA_ADAPTER_UTILS_TAG, "env", NULL);
291 VERIFY_NON_NULL_RET(className, CA_ADAPTER_UTILS_TAG, "className", NULL);
292 VERIFY_NON_NULL_RET(methodName, CA_ADAPTER_UTILS_TAG, "methodName", NULL);
293 VERIFY_NON_NULL_RET(methodFormat, CA_ADAPTER_UTILS_TAG, "methodFormat", NULL);
295 jclass jni_cid = (*env)->FindClass(env, className);
298 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "jni_cid [%s] is null", className);
302 jmethodID jni_midID = (*env)->GetMethodID(env, jni_cid, methodName, methodFormat);
305 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "jni_midID [%s] is null", methodName);
306 (*env)->DeleteLocalRef(env, jni_cid);
310 (*env)->DeleteLocalRef(env, jni_cid);
314 void CADeleteGlobalReferences(JNIEnv *env)
318 (*env)->DeleteGlobalRef(env, g_Context);
324 (*env)->DeleteGlobalRef(env, g_Activity);