Merge branch 'master' into cloud-interface
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / adapter_util / caadapterutils.c
index 0f87e4e..bb79375 100644 (file)
 #include "caadapterutils.h"
 
 #include <string.h>
-
+#include <ctype.h>
+#include "oic_string.h"
 #include "oic_malloc.h"
+#include <errno.h>
+
+#ifndef WITH_ARDUINO
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#endif
+
+#ifdef __ANDROID__
+#include <jni.h>
+#endif
+
+#define CA_ADAPTER_UTILS_TAG "OIC_CA_ADAP_UTILS"
 
-#define CA_ADAPTER_UTILS_TAG "CA_ADAPTER_UTILS"
+#ifdef __ANDROID__
+/**
+ * @var g_jvm
+ * @brief pointer to store JavaVM
+ */
+static JavaVM *g_jvm = NULL;
 
-CALocalConnectivity_t *CAAdapterCreateLocalEndpoint(CAConnectivityType_t type,
-                                                    const char *address)
+/**
+ * @var gContext
+ * @brief pointer to store context for android callback interface
+ */
+static jobject g_Context = NULL;
+static jobject g_Activity = NULL;
+#endif
+
+#ifdef WITH_ARDUINO
+CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
+                                   size_t ipAddrLen, uint16_t *port)
 {
-    CALocalConnectivity_t *info = (CALocalConnectivity_t *)
-                                  OICMalloc(sizeof(CALocalConnectivity_t));
-    if (NULL == info)
+    if (!ipAddr || !isdigit(ipAddrStr[0]) || !port)
     {
-        OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
-        return NULL;
+        OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "invalid param");
+        return CA_STATUS_INVALID_PARAM;
     }
-    memset(info, 0, sizeof(CALocalConnectivity_t));
 
-    info->type = type;
-    if (address && strlen(address))
+    size_t index = 0;
+    uint8_t dotCount = 0;
+
+    ipAddr[index] = 0;
+    *port = 0;
+    while (*ipAddrStr)
     {
-        if (CA_EDR == type)
+        if (isdigit(*ipAddrStr))
         {
-            strncpy(info->addressInfo.BT.btMacAddress, address, CA_MACADDR_SIZE - 1);
-            info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
+            if(index >= ipAddrLen)
+            {
+                OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "invalid param");
+                return CA_STATUS_INVALID_PARAM;
+            }
+            ipAddr[index] *= 10;
+            ipAddr[index] += *ipAddrStr - '0';
         }
-        else if (CA_LE == type)
+        else if (*ipAddrStr == '.')
         {
-            strncpy(info->addressInfo.LE.leMacAddress, address, CA_MACADDR_SIZE - 1);
-            info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
+            index++;
+            dotCount++;
+            ipAddr[index] = 0;
         }
-        else if (CA_WIFI == type || CA_ETHERNET == type)
+        else
         {
-            strncpy(info->addressInfo.IP.ipAddress, address, CA_IPADDR_SIZE - 1);
-            info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
+            break;
         }
+        ipAddrStr++;
     }
 
-    return info;
-}
-
-CALocalConnectivity_t *CAAdapterCopyLocalEndpoint(CALocalConnectivity_t *connectivity)
-{
-    VERIFY_NON_NULL_RET(connectivity, CA_ADAPTER_UTILS_TAG, "connectivity is NULL", NULL);
-
-    CALocalConnectivity_t *info = (CALocalConnectivity_t *)
-                                  OICMalloc(sizeof(CALocalConnectivity_t));
-    if (NULL == info)
+    if (*ipAddrStr == ':')
     {
-        OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
-        return NULL;
+        ipAddrStr++;
+        while (*ipAddrStr)
+        {
+            if (isdigit(*ipAddrStr))
+            {
+                *port *= 10;
+                *port += *ipAddrStr - '0';
+            }
+            else
+            {
+                break;
+            }
+            ipAddrStr++;
+        }
     }
-    memset(info, 0, sizeof(CALocalConnectivity_t));
 
-    info->type = connectivity->type;
-    if (CA_EDR == info->type && strlen(connectivity->addressInfo.BT.btMacAddress))
-    {
-        strncpy(info->addressInfo.BT.btMacAddress, connectivity->addressInfo.BT.btMacAddress,
-                CA_MACADDR_SIZE - 1);
-        info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
-    }
-    else if (CA_LE == info->type && strlen(connectivity->addressInfo.LE.leMacAddress))
+    if (dotCount == 3)
     {
-        strncpy(info->addressInfo.LE.leMacAddress, connectivity->addressInfo.LE.leMacAddress,
-                CA_MACADDR_SIZE - 1);
-        info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
+        return CA_STATUS_OK;
     }
-    else if ((CA_WIFI == info->type || CA_ETHERNET == info->type)
-             && strlen(connectivity->addressInfo.IP.ipAddress))
-    {
-        strncpy(info->addressInfo.IP.ipAddress, connectivity->addressInfo.IP.ipAddress,
-                CA_IPADDR_SIZE - 1);
-        info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
-        info->addressInfo.IP.port = connectivity->addressInfo.IP.port;
-    }
-
-    info->isSecured = connectivity->isSecured;
-    return info;
+    return CA_STATUS_FAILED;
 }
 
-void CAAdapterFreeLocalEndpoint(CALocalConnectivity_t *localEndpoint)
+#else // not with_arduino
+/*
+ * These two conversion functions return void because errors can't happen
+ * (because of NI_NUMERIC), and there's nothing to do if they do happen.
+ */
+void CAConvertAddrToName(const struct sockaddr_storage *sockAddr, socklen_t sockAddrLen,
+                         char *host, uint16_t *port)
 {
-    if (localEndpoint)
+    VERIFY_NON_NULL_VOID(sockAddr, CA_ADAPTER_UTILS_TAG, "sockAddr is null");
+    VERIFY_NON_NULL_VOID(host, CA_ADAPTER_UTILS_TAG, "host is null");
+    VERIFY_NON_NULL_VOID(port, CA_ADAPTER_UTILS_TAG, "port is null");
+
+    int r = getnameinfo((struct sockaddr *)sockAddr,
+                        sockAddrLen,
+                        host, MAX_ADDR_STR_SIZE_CA,
+                        NULL, 0,
+                        NI_NUMERICHOST|NI_NUMERICSERV);
+    if (r)
     {
-        OICFree(localEndpoint);
+        if (EAI_SYSTEM == r)
+        {
+            OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
+                            "getnameinfo failed: errno %s", strerror(errno));
+        }
+        else
+        {
+            OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
+                            "getnameinfo failed: %s", gai_strerror(r));
+        }
+        return;
     }
+    *port = ntohs(((struct sockaddr_in *)sockAddr)->sin_port); // IPv4 and IPv6
 }
 
-CARemoteEndpoint_t *CAAdapterCreateRemoteEndpoint(CAConnectivityType_t type,
-                                                  const char *address,
-                                                  const char *resourceUri)
+void CAConvertNameToAddr(const char *host, uint16_t port, struct sockaddr_storage *sockaddr)
 {
-    CARemoteEndpoint_t *info = (CARemoteEndpoint_t *)
-                               OICMalloc(sizeof(CARemoteEndpoint_t));
-    if (NULL == info)
-    {
-        OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
-        return NULL;
-    }
-    memset(info, 0, sizeof(CARemoteEndpoint_t));
+    VERIFY_NON_NULL_VOID(host, CA_ADAPTER_UTILS_TAG, "host is null");
+    VERIFY_NON_NULL_VOID(sockaddr, CA_ADAPTER_UTILS_TAG, "sockaddr is null");
 
-    info->connectivityType = type;
-    if (address && strlen(address))
+    struct addrinfo *addrs;
+    struct addrinfo hints = { .ai_family = AF_UNSPEC,
+                              .ai_socktype = SOCK_DGRAM,
+                              .ai_flags = AI_NUMERICHOST };
+
+    int r = getaddrinfo(host, NULL, &hints, &addrs);
+    if (r)
     {
-        if (CA_EDR == type)
-        {
-            strncpy(info->addressInfo.BT.btMacAddress, address, CA_MACADDR_SIZE - 1);
-            info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
-        }
-        else if (CA_LE == info->connectivityType)
+        if (EAI_SYSTEM == r)
         {
-            strncpy(info->addressInfo.LE.leMacAddress, address, CA_MACADDR_SIZE - 1);
-            info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
+            OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
+                            "getaddrinfo failed: errno %s", strerror(errno));
         }
-        else if (CA_WIFI == type || CA_ETHERNET == type)
+        else
         {
-            strncpy(info->addressInfo.IP.ipAddress, address, CA_IPADDR_SIZE - 1);
-            info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
+            OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
+                            "getaddrinfo failed: %s", gai_strerror(r));
         }
+        return;
     }
-
-    if (resourceUri && strlen(resourceUri))
+    // assumption: in this case, getaddrinfo will only return one addrinfo
+    // or first is the one we want.
+    if (addrs[0].ai_family == AF_INET6)
     {
-        info->resourceUri = OICStrdup(resourceUri);
+        memcpy(sockaddr, addrs[0].ai_addr, sizeof (struct sockaddr_in6));
+        ((struct sockaddr_in6 *)sockaddr)->sin6_port = htons(port);
     }
-
-    return info;
+    else
+    {
+        memcpy(sockaddr, addrs[0].ai_addr, sizeof (struct sockaddr_in));
+        ((struct sockaddr_in *)sockaddr)->sin_port = htons(port);
+    }
+    freeaddrinfo(addrs);
 }
+#endif // WITH_ARDUINO
 
-CARemoteEndpoint_t *CAAdapterCopyRemoteEndpoint(const CARemoteEndpoint_t *remoteEndpoint)
+#ifdef __ANDROID__
+void CANativeJNISetContext(JNIEnv *env, jobject context)
 {
-    VERIFY_NON_NULL_RET(remoteEndpoint, CA_ADAPTER_UTILS_TAG, "Remote endpoint is NULL", NULL);
+    OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetContext");
 
-    CARemoteEndpoint_t *info = (CARemoteEndpoint_t *)
-                               OICMalloc(sizeof(CARemoteEndpoint_t));
-    if (NULL == info)
+    if (!context)
     {
-        OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
-        return NULL;
+        OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "context is null");
+        return;
     }
-    memset(info, 0, sizeof(CARemoteEndpoint_t));
 
-    info->connectivityType = remoteEndpoint->connectivityType;
-    if (CA_EDR == info->connectivityType && strlen(remoteEndpoint->addressInfo.BT.btMacAddress))
+    if (!g_Context)
     {
-        strncpy(info->addressInfo.BT.btMacAddress, remoteEndpoint->addressInfo.BT.btMacAddress,
-                CA_MACADDR_SIZE - 1);
-        info->addressInfo.BT.btMacAddress[CA_MACADDR_SIZE - 1] = '\0';
+        g_Context = (*env)->NewGlobalRef(env, context);
     }
-    else if (CA_LE == info->connectivityType && strlen(remoteEndpoint->addressInfo.LE.leMacAddress))
+    else
     {
-        strncpy(info->addressInfo.LE.leMacAddress, remoteEndpoint->addressInfo.LE.leMacAddress,
-                CA_MACADDR_SIZE - 1);
-        info->addressInfo.LE.leMacAddress[CA_MACADDR_SIZE - 1] = '\0';
+        OIC_LOG(INFO, CA_ADAPTER_UTILS_TAG, "context is already set");
     }
-    else if ((CA_WIFI == info->connectivityType || CA_ETHERNET == info->connectivityType)
-             && strlen(remoteEndpoint->addressInfo.IP.ipAddress))
+}
+
+void CANativeJNISetJavaVM(JavaVM *jvm)
+{
+    OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetJavaVM");
+    g_jvm = jvm;
+}
+
+jobject CANativeJNIGetContext()
+{
+    return g_Context;
+}
+
+JavaVM *CANativeJNIGetJavaVM()
+{
+    return g_jvm;
+}
+
+void CANativeSetActivity(JNIEnv *env, jobject activity)
+{
+    OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeSetActivity");
+
+    if (!activity)
     {
-        strncpy(info->addressInfo.IP.ipAddress, remoteEndpoint->addressInfo.IP.ipAddress,
-                CA_IPADDR_SIZE - 1);
-        info->addressInfo.IP.ipAddress[CA_IPADDR_SIZE - 1] = '\0';
-        info->addressInfo.IP.port = remoteEndpoint->addressInfo.IP.port;
+        OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "activity is null");
+        return;
     }
 
-    if (remoteEndpoint->resourceUri && strlen(remoteEndpoint->resourceUri))
+    if (!g_Activity)
     {
-        info->resourceUri = OICStrdup(remoteEndpoint->resourceUri);
+        g_Activity = (*env)->NewGlobalRef(env, activity);
     }
+    else
+    {
+        OIC_LOG(INFO, CA_ADAPTER_UTILS_TAG, "activity is already set");
+    }
+}
 
-    info->isSecured = remoteEndpoint->isSecured;
-    return info;
+jobject *CANativeGetActivity()
+{
+    return g_Activity;
 }
 
-void CAAdapterFreeRemoteEndpoint(CARemoteEndpoint_t *remoteEndpoint)
+void CADeleteGlobalReferences(JNIEnv *env)
 {
-    if (remoteEndpoint)
+    if (g_Context)
     {
-        if (remoteEndpoint->resourceUri)
-        {
-            OICFree(remoteEndpoint->resourceUri);
-        }
+        (*env)->DeleteGlobalRef(env, g_Context);
+        g_Context = NULL;
+    }
 
-        OICFree(remoteEndpoint);
+    if (g_Activity)
+    {
+        (*env)->DeleteGlobalRef(env, g_Activity);
+        g_Activity = NULL;
     }
 }
-
+#endif