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 "OIC_CA_ADAP_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;
53 static jobject g_Activity = NULL;
57 CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
58 size_t ipAddrLen, uint16_t *port)
60 if (!ipAddr || !isdigit(ipAddrStr[0]) || !port)
62 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "invalid param");
63 return CA_STATUS_INVALID_PARAM;
73 if (isdigit(*ipAddrStr))
75 if(index >= ipAddrLen)
77 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "invalid param");
78 return CA_STATUS_INVALID_PARAM;
81 ipAddr[index] += *ipAddrStr - '0';
83 else if (*ipAddrStr == '.')
96 if (*ipAddrStr == ':')
101 if (isdigit(*ipAddrStr))
104 *port += *ipAddrStr - '0';
118 return CA_STATUS_FAILED;
121 #else // not with_arduino
123 * These two conversion functions return void because errors can't happen
124 * (because of NI_NUMERIC), and there's nothing to do if they do happen.
126 void CAConvertAddrToName(const struct sockaddr_storage *sockAddr, socklen_t sockAddrLen,
127 char *host, uint16_t *port)
129 VERIFY_NON_NULL_VOID(sockAddr, CA_ADAPTER_UTILS_TAG, "sockAddr is null");
130 VERIFY_NON_NULL_VOID(host, CA_ADAPTER_UTILS_TAG, "host is null");
131 VERIFY_NON_NULL_VOID(port, CA_ADAPTER_UTILS_TAG, "port is null");
133 int r = getnameinfo((struct sockaddr *)sockAddr,
135 host, MAX_ADDR_STR_SIZE_CA,
137 NI_NUMERICHOST|NI_NUMERICSERV);
142 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
143 "getnameinfo failed: errno %s", strerror(errno));
147 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
148 "getnameinfo failed: %s", gai_strerror(r));
152 *port = ntohs(((struct sockaddr_in *)sockAddr)->sin_port); // IPv4 and IPv6
155 void CAConvertNameToAddr(const char *host, uint16_t port, struct sockaddr_storage *sockaddr)
157 VERIFY_NON_NULL_VOID(host, CA_ADAPTER_UTILS_TAG, "host is null");
158 VERIFY_NON_NULL_VOID(sockaddr, CA_ADAPTER_UTILS_TAG, "sockaddr is null");
160 struct addrinfo *addrs;
161 struct addrinfo hints = { .ai_family = AF_UNSPEC,
162 .ai_socktype = SOCK_DGRAM,
163 .ai_flags = AI_NUMERICHOST };
165 int r = getaddrinfo(host, NULL, &hints, &addrs);
170 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
171 "getaddrinfo failed: errno %s", strerror(errno));
175 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
176 "getaddrinfo failed: %s", gai_strerror(r));
180 // assumption: in this case, getaddrinfo will only return one addrinfo
181 // or first is the one we want.
182 if (addrs[0].ai_family == AF_INET6)
184 memcpy(sockaddr, addrs[0].ai_addr, sizeof (struct sockaddr_in6));
185 ((struct sockaddr_in6 *)sockaddr)->sin6_port = htons(port);
189 memcpy(sockaddr, addrs[0].ai_addr, sizeof (struct sockaddr_in));
190 ((struct sockaddr_in *)sockaddr)->sin_port = htons(port);
194 #endif // WITH_ARDUINO
197 void CANativeJNISetContext(JNIEnv *env, jobject context)
199 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetContext");
203 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "context is null");
209 g_Context = (*env)->NewGlobalRef(env, context);
213 OIC_LOG(INFO, CA_ADAPTER_UTILS_TAG, "context is already set");
217 void CANativeJNISetJavaVM(JavaVM *jvm)
219 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetJavaVM");
223 jobject CANativeJNIGetContext()
228 JavaVM *CANativeJNIGetJavaVM()
233 void CANativeSetActivity(JNIEnv *env, jobject activity)
235 OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeSetActivity");
239 OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "activity is null");
245 g_Activity = (*env)->NewGlobalRef(env, activity);
249 OIC_LOG(INFO, CA_ADAPTER_UTILS_TAG, "activity is already set");
253 jobject *CANativeGetActivity()
258 jmethodID CAGetJNIMethodID(JNIEnv *env, const char* className,
259 const char* methodName,
260 const char* methodFormat)
262 VERIFY_NON_NULL_RET(env, CA_ADAPTER_UTILS_TAG, "env", NULL);
263 VERIFY_NON_NULL_RET(className, CA_ADAPTER_UTILS_TAG, "className", NULL);
264 VERIFY_NON_NULL_RET(methodName, CA_ADAPTER_UTILS_TAG, "methodName", NULL);
265 VERIFY_NON_NULL_RET(methodFormat, CA_ADAPTER_UTILS_TAG, "methodFormat", NULL);
267 jclass jni_cid = (*env)->FindClass(env, className);
270 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "jni_cid [%s] is null", className);
274 jmethodID jni_midID = (*env)->GetMethodID(env, jni_cid, methodName, methodFormat);
277 OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "jni_midID [%s] is null", methodName);
278 (*env)->DeleteLocalRef(env, jni_cid);
282 (*env)->DeleteLocalRef(env, jni_cid);
286 void CADeleteGlobalReferences(JNIEnv *env)
290 (*env)->DeleteGlobalRef(env, g_Context);
296 (*env)->DeleteGlobalRef(env, g_Activity);