replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / adapter_util / caadapterutils.c
index 3209d8b..be74098 100644 (file)
  *
  ******************************************************************/
 
+#include "iotivity_config.h"
 #include "caadapterutils.h"
 
 #include <string.h>
 #include <ctype.h>
 #include "oic_string.h"
 #include "oic_malloc.h"
+#include <errno.h>
+
+#ifdef HAVE_WS2TCPIP_H
+#include <ws2tcpip.h>
+#endif
+#ifdef HAVE_SYS_SOCKET_H
+#include <sys/socket.h>
+#endif
+#ifdef HAVE_NETINET_IN_H
+#include <netinet/in.h>
+#endif
+#if defined(HAVE_WINSOCK2_H) && defined(HAVE_WS2TCPIP_H)
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#endif
+#ifdef HAVE_NETDB_H
+#include <netdb.h>
+#endif
 
 #ifdef __ANDROID__
 #include <jni.h>
 #endif
 
-#define CA_ADAPTER_UTILS_TAG "CA_ADAPTER_UTILS"
+#define CA_ADAPTER_UTILS_TAG "OIC_CA_ADAP_UTILS"
 
 #ifdef __ANDROID__
 /**
@@ -43,232 +62,10 @@ static JavaVM *g_jvm = NULL;
  * @brief pointer to store context for android callback interface
  */
 static jobject g_Context = NULL;
+static jobject g_Activity = NULL;
 #endif
 
-void CALogPDUData(coap_pdu_t *pdu)
-{
-    VERIFY_NON_NULL_VOID(pdu, CA_ADAPTER_UTILS_TAG, "pdu");
-    OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - payload : %s", pdu->data);
-
-    OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - type : %d", pdu->hdr->type);
-
-    OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - code : %d", pdu->hdr->code);
-
-    OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - id : %d", pdu->hdr->id);
-
-    OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "PDU Maker - token : %s", pdu->hdr->token);
-}
-
-CALocalConnectivity_t *CAAdapterCreateLocalEndpoint(CATransportType_t type, const char *address)
-{
-    CALocalConnectivity_t *info = (CALocalConnectivity_t *)
-                                  OICCalloc(1, sizeof(CALocalConnectivity_t));
-    if (NULL == info)
-    {
-        OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
-        return NULL;
-    }
-
-    info->type = type;
-    if (address && strlen(address))
-    {
-        if (CA_EDR == type)
-        {
-            OICStrcpy(info->addressInfo.BT.btMacAddress, sizeof(info->addressInfo.BT.btMacAddress),
-                    address);
-        }
-        else if (CA_LE == type)
-        {
-            OICStrcpy(info->addressInfo.LE.leMacAddress, sizeof(info->addressInfo.LE.leMacAddress),
-                    address);
-        }
-        else if (CA_IPV4 == type)
-        {
-            OICStrcpy(info->addressInfo.IP.ipAddress, sizeof(info->addressInfo.IP.ipAddress),
-                    address);
-        }
-        else if (CA_IPV6 == type)
-        {
-            OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Currently IPV6 is not supported");
-            OICFree(info);
-            return NULL;
-        }
-        else
-        {
-            OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "type is not matched with any transport!");
-            OICFree(info);
-            return NULL;
-        }
-    }
-
-    return info;
-}
-
-CALocalConnectivity_t *CAAdapterCopyLocalEndpoint(const CALocalConnectivity_t *connectivity)
-{
-    VERIFY_NON_NULL_RET(connectivity, CA_ADAPTER_UTILS_TAG, "connectivity is NULL", NULL);
-
-    CALocalConnectivity_t *info = (CALocalConnectivity_t *)
-                                  OICCalloc(1, sizeof(CALocalConnectivity_t));
-    if (NULL == info)
-    {
-        OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
-        return NULL;
-    }
-
-    info->type = connectivity->type;
-    if (CA_EDR == info->type && strlen(connectivity->addressInfo.BT.btMacAddress))
-    {
-        OICStrcpy(info->addressInfo.BT.btMacAddress, sizeof(info->addressInfo.BT.btMacAddress),
-                connectivity->addressInfo.BT.btMacAddress);
-    }
-    else if (CA_LE == info->type && strlen(connectivity->addressInfo.LE.leMacAddress))
-    {
-        OICStrcpy(info->addressInfo.LE.leMacAddress, sizeof(info->addressInfo.LE.leMacAddress),
-            connectivity->addressInfo.LE.leMacAddress);
-    }
-    else if ((CA_IPV4 == info->type)
-
-            && strlen(connectivity->addressInfo.IP.ipAddress))
-    {
-        OICStrcpy(info->addressInfo.IP.ipAddress, sizeof(info->addressInfo.IP.ipAddress),
-                connectivity->addressInfo.IP.ipAddress);
-        info->addressInfo.IP.port = connectivity->addressInfo.IP.port;
-    }
-    else if (CA_IPV6 == info->type)
-    {
-        OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Currently IPV6 is not supported");
-        OICFree(info);
-        return NULL;
-    }
-    else
-    {
-        OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "type is not matched with any transport!");
-        OICFree(info);
-        return NULL;
-    }
-
-    info->isSecured = connectivity->isSecured;
-    return info;
-}
-
-void CAAdapterFreeLocalEndpoint(CALocalConnectivity_t *localEndpoint)
-{
-    OICFree(localEndpoint);
-}
-
-CARemoteEndpoint_t *CAAdapterCreateRemoteEndpoint(CATransportType_t type, const char *address,
-                                                  const char *resourceUri)
-{
-    CARemoteEndpoint_t *info = (CARemoteEndpoint_t *)
-                               OICCalloc(1, sizeof(CARemoteEndpoint_t));
-    if (NULL == info)
-    {
-        OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
-        return NULL;
-    }
-
-    info->transportType = type;
-    if (address && strlen(address))
-    {
-        if (CA_EDR == type)
-        {
-            OICStrcpy(info->addressInfo.BT.btMacAddress, sizeof(info->addressInfo.BT.btMacAddress),
-                    address);
-        }
-        else if (CA_LE == type)
-        {
-            OICStrcpy(info->addressInfo.LE.leMacAddress, sizeof(info->addressInfo.LE.leMacAddress),
-                    address);
-        }
-        else if (CA_IPV4 == type)
-        {
-            OICStrcpy(info->addressInfo.IP.ipAddress, sizeof(info->addressInfo.IP.ipAddress),
-                    address);
-        }
-        else if (CA_IPV6 == type)
-        {
-            OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Currently IPV6 is not supported");
-            OICFree(info);
-            return NULL;
-        }
-        else
-        {
-            OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "type is not matched with any transport!");
-            OICFree(info);
-            return NULL;
-        }
-    }
-
-    if (resourceUri && strlen(resourceUri))
-    {
-        info->resourceUri = OICStrdup(resourceUri);
-    }
-
-    return info;
-}
-
-CARemoteEndpoint_t *CAAdapterCopyRemoteEndpoint(const CARemoteEndpoint_t *remoteEndpoint)
-{
-    VERIFY_NON_NULL_RET(remoteEndpoint, CA_ADAPTER_UTILS_TAG, "Remote endpoint is NULL", NULL);
-
-    CARemoteEndpoint_t *info = (CARemoteEndpoint_t *)
-                               OICCalloc(1, sizeof(CARemoteEndpoint_t));
-    if (NULL == info)
-    {
-        OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Memory allocation failed !");
-        return NULL;
-    }
-
-    info->transportType = remoteEndpoint->transportType;
-    if (CA_EDR == info->transportType && ('\0' != remoteEndpoint->addressInfo.BT.btMacAddress[0]))
-    {
-        OICStrcpy(info->addressInfo.BT.btMacAddress, sizeof(info->addressInfo.BT.btMacAddress),
-                remoteEndpoint->addressInfo.BT.btMacAddress);
-    }
-    else if (CA_LE == info->transportType
-             && ('\0' != remoteEndpoint->addressInfo.LE.leMacAddress[0]))
-    {
-        OICStrcpy(info->addressInfo.LE.leMacAddress, sizeof(info->addressInfo.LE.leMacAddress),
-                remoteEndpoint->addressInfo.LE.leMacAddress);
-    }
-    else if ((CA_IPV4 == info->transportType)
-            && ('\0' != remoteEndpoint->addressInfo.IP.ipAddress[0]))
-    {
-        OICStrcpy(info->addressInfo.IP.ipAddress, sizeof(info->addressInfo.IP.ipAddress),
-                remoteEndpoint->addressInfo.IP.ipAddress);
-        info->addressInfo.IP.port = remoteEndpoint->addressInfo.IP.port;
-    }
-    else if (CA_IPV6 == info->transportType)
-    {
-        OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Currently IPV6 is not supported");
-    }
-    else
-    {
-        OIC_LOG(DEBUG, CA_ADAPTER_UTILS_TAG, "Its not matching. May be multicast.");
-    }
-
-    //For Multicast, remote address will be null while resourceUri will have the service UUID
-
-    if (remoteEndpoint->resourceUri && strlen(remoteEndpoint->resourceUri))
-    {
-        info->resourceUri = OICStrdup(remoteEndpoint->resourceUri);
-    }
-
-    info->isSecured = remoteEndpoint->isSecured;
-    info->identity  = remoteEndpoint->identity;
-    return info;
-}
-
-void CAAdapterFreeRemoteEndpoint(CARemoteEndpoint_t *remoteEndpoint)
-{
-    if (remoteEndpoint)
-    {
-        OICFree(remoteEndpoint->resourceUri);
-        OICFree(remoteEndpoint);
-    }
-}
-
+#ifdef WITH_ARDUINO
 CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
                                    size_t ipAddrLen, uint16_t *port)
 {
@@ -333,267 +130,288 @@ CAResult_t CAParseIPv4AddressInternal(const char *ipAddrStr, uint8_t *ipAddr,
     return CA_STATUS_FAILED;
 }
 
-bool CAAdapterIsSameSubnet(const char *ipAddress1, const char *ipAddress2, const char *netMask)
+#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)
 {
-    VERIFY_NON_NULL_RET(ipAddress1, CA_ADAPTER_UTILS_TAG, "First address", false);
-    VERIFY_NON_NULL_RET(ipAddress2, CA_ADAPTER_UTILS_TAG, "Second address", false);
-    VERIFY_NON_NULL_RET(netMask, CA_ADAPTER_UTILS_TAG, "netMask", false);
+    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)
+    {
+#if defined(EAI_SYSTEM)
+        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));
+        }
+#elif defined(_WIN32)
+        OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
+                            "getnameinfo failed: errno %i", WSAGetLastError());
+#else
+        OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
+                            "getnameinfo failed: %s", gai_strerror(r));
+#endif
+        return;
+    }
+    *port = ntohs(((struct sockaddr_in *)sockAddr)->sin_port); // IPv4 and IPv6
+}
 
-    uint8_t ipList1[IPV4_ADDR_ONE_OCTECT_LEN] = { 0 };
-    uint8_t ipList2[IPV4_ADDR_ONE_OCTECT_LEN] = { 0 };
-    uint8_t maskList[IPV4_ADDR_ONE_OCTECT_LEN] = { 0 };
-    CAResult_t ret = CA_STATUS_OK;
+void CAConvertNameToAddr(const char *host, uint16_t port, struct sockaddr_storage *sockaddr)
+{
+    VERIFY_NON_NULL_VOID(host, CA_ADAPTER_UTILS_TAG, "host is null");
+    VERIFY_NON_NULL_VOID(sockaddr, CA_ADAPTER_UTILS_TAG, "sockaddr is null");
 
-    /* Local Loopback Address */
-    if (0 == strncmp(ipAddress1, "127.", 4) || 0 == strncmp(ipAddress2, "127.", 4))
-    {
-        return true;
-    }
+    struct addrinfo *addrs = NULL;
+    struct addrinfo hints = { .ai_family = AF_UNSPEC,
+                              .ai_socktype = SOCK_DGRAM,
+                              .ai_flags = AI_NUMERICHOST };
 
-    uint16_t parsedPort = 0;
-    ret = CAParseIPv4AddressInternal(ipAddress1, ipList1, sizeof(ipList1), &parsedPort);
-    if (ret != CA_STATUS_OK)
+    int r = getaddrinfo(host, NULL, &hints, &addrs);
+    if (r)
     {
-        OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "First ip address parse fail %d", ret);
-        return false;
+        if (NULL != addrs)
+        {
+            freeaddrinfo(addrs);
+        }
+#if defined(EAI_SYSTEM)
+        if (EAI_SYSTEM == r)
+        {
+            OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
+                            "getaddrinfo failed: errno %s", strerror(errno));
+        }
+        else
+        {
+            OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
+                            "getaddrinfo failed: %s", gai_strerror(r));
+        }
+#elif defined(_WIN32)
+        OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
+                            "getaddrinfo failed: errno %i", WSAGetLastError());
+#else
+        OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG,
+                            "getaddrinfo failed: %s", gai_strerror(r));
+#endif
+        return;
     }
-
-    ret = CAParseIPv4AddressInternal(ipAddress2, ipList2, sizeof(ipList2), &parsedPort);
-    if (ret != CA_STATUS_OK)
+    // assumption: in this case, getaddrinfo will only return one addrinfo
+    // or first is the one we want.
+    if (addrs[0].ai_family == AF_INET6)
     {
-        OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Second ip address parse fail %d", ret);
-        return false;
+        memcpy(sockaddr, addrs[0].ai_addr, sizeof (struct sockaddr_in6));
+        ((struct sockaddr_in6 *)sockaddr)->sin6_port = htons(port);
     }
-
-    ret = CAParseIPv4AddressInternal(netMask, maskList, sizeof(maskList), &parsedPort);
-    if (ret != CA_STATUS_OK)
+    else
     {
-        OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "Net mask parse fail %d", ret);
-        return false;
+        memcpy(sockaddr, addrs[0].ai_addr, sizeof (struct sockaddr_in));
+        ((struct sockaddr_in *)sockaddr)->sin_port = htons(port);
     }
-
-    return ((ipList1[0] & maskList[0]) == (ipList2[0] & maskList[0])) && ((ipList1[1] & maskList[1])
-            == (ipList2[1] & maskList[1]))
-           && ((ipList1[2] & maskList[2]) == (ipList2[2] & maskList[2]))
-           && ((ipList1[3] & maskList[3]) == (ipList2[3] & maskList[3]));
+    freeaddrinfo(addrs);
 }
+#endif // WITH_ARDUINO
 
-
-bool CAIsMulticastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
-                                const char *multicastAddress, uint16_t port)
+#ifdef __ANDROID__
+void CANativeJNISetContext(JNIEnv *env, jobject context)
 {
-    VERIFY_NON_NULL_RET(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null", false);
-    VERIFY_NON_NULL_RET(ipAddress, CA_ADAPTER_UTILS_TAG, "ipAddress is null", false);
-    VERIFY_NON_NULL_RET(multicastAddress, CA_ADAPTER_UTILS_TAG, "multicastAddress is null", false);
+    OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetContext");
 
-    uint32_t listLength = u_arraylist_length(serverInfoList);
-    for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
+    if (!context)
     {
-        CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
-        if (!info)
-        {
-            OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "Info is NULL");
-            return false;
-        }
+        OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "context is null");
+        return;
+    }
 
-        if (info->isMulticastServer && (strncmp(info->ipAddress, multicastAddress,
-                                                strlen(multicastAddress) == 0))
-            && (info->port == port) && (strncmp(info->ifAddr, ipAddress, strlen(ipAddress)) == 0))
-        {
-            return info->isServerStarted;
-        }
+    if (!g_Context)
+    {
+        g_Context = (*env)->NewGlobalRef(env, context);
+    }
+    else
+    {
+        OIC_LOG(INFO, CA_ADAPTER_UTILS_TAG, "context is already set");
     }
-    return false;
 }
 
-bool CAIsUnicastServerStarted(const u_arraylist_t *serverInfoList, const char *ipAddress,
-                              uint16_t port)
+void CANativeJNISetJavaVM(JavaVM *jvm)
 {
-    VERIFY_NON_NULL_RET(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null", false);
-    VERIFY_NON_NULL_RET(ipAddress, CA_ADAPTER_UTILS_TAG, "ipAddress is null", false);
+    OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetJavaVM");
+    g_jvm = jvm;
+}
 
-    uint32_t listLength = u_arraylist_length(serverInfoList);
-    for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
-    {
-        CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
-        if (!info)
-        {
-            continue;
-        }
+jobject CANativeJNIGetContext()
+{
+    return g_Context;
+}
 
-        if (!info->isMulticastServer && (strncmp(info->ipAddress, ipAddress,
-                                                 strlen(ipAddress)) == 0)
-            && (info->port == port))
-        {
-            return info->isServerStarted;
-        }
-    }
-    return false;
+JavaVM *CANativeJNIGetJavaVM()
+{
+    return g_jvm;
 }
 
-uint16_t CAGetServerPort(const u_arraylist_t *serverInfoList, const char *ipAddress, bool isSecured)
+void CANativeSetActivity(JNIEnv *env, jobject activity)
 {
-    VERIFY_NON_NULL_RET(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null", 0);
-    VERIFY_NON_NULL_RET(ipAddress, CA_ADAPTER_UTILS_TAG, "ipAddress is null", 0);
+    OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeSetActivity");
 
-    uint32_t listLength = u_arraylist_length(serverInfoList);
-    for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
+    if (!activity)
     {
-        CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
-        if (!info)
-        {
-            continue;
-        }
-        if ((strncmp(info->ipAddress, ipAddress, strlen(ipAddress)) == 0) &&
-                    (info->isSecured == isSecured))
-        {
-            return info->port;
-        }
+        OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "activity is null");
+        return;
     }
 
-    return 0;
+    if (!g_Activity)
+    {
+        g_Activity = (*env)->NewGlobalRef(env, activity);
+    }
+    else
+    {
+        OIC_LOG(INFO, CA_ADAPTER_UTILS_TAG, "activity is already set");
+    }
 }
 
-int CAGetSocketFdForUnicastServer(const u_arraylist_t *serverInfoList, const char *ipAddress,
-                                  bool isSecured, bool isMulticast, CATransportType_t type)
+jobject *CANativeGetActivity()
 {
-    VERIFY_NON_NULL_RET(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null", -1);
-    VERIFY_NON_NULL_RET(ipAddress, CA_ADAPTER_UTILS_TAG, "ipAddress is null", -1);
+    return g_Activity;
+}
 
-    uint32_t listLength = u_arraylist_length(serverInfoList);
+jmethodID CAGetJNIMethodID(JNIEnv *env, const char* className,
+                           const char* methodName,
+                           const char* methodFormat)
+{
+    VERIFY_NON_NULL_RET(env, CA_ADAPTER_UTILS_TAG, "env", NULL);
+    VERIFY_NON_NULL_RET(className, CA_ADAPTER_UTILS_TAG, "className", NULL);
+    VERIFY_NON_NULL_RET(methodName, CA_ADAPTER_UTILS_TAG, "methodName", NULL);
+    VERIFY_NON_NULL_RET(methodFormat, CA_ADAPTER_UTILS_TAG, "methodFormat", NULL);
 
-    for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
+    jclass jni_cid = (*env)->FindClass(env, className);
+    if (!jni_cid)
     {
-        CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
-        if (!info)
-        {
-            continue;
-        }
-
-        if (!CAAdapterIsSameSubnet(info->ipAddress, ipAddress, info->subNetMask))
-        {
-            continue;
-        }
-
-        if (!info->isMulticastServer && (info->isSecured == isSecured))
-        {
-            OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG,
-                      "CAGetSocketFdForServer found socket [%d]", info->socketFd);
-            return info->socketFd;
-        }
+        OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "jni_cid [%s] is null", className);
+        CACheckJNIException(env);
+        return NULL;
+    }
 
+    jmethodID jni_midID = (*env)->GetMethodID(env, jni_cid, methodName, methodFormat);
+    if (!jni_midID)
+    {
+        OIC_LOG_V(ERROR, CA_ADAPTER_UTILS_TAG, "jni_midID [%s] is null", methodName);
+        CACheckJNIException(env);
+        (*env)->DeleteLocalRef(env, jni_cid);
+        return NULL;
     }
 
-    OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG,
-            "CAGetSocketFdForServer socket fd is not found");
-    return -1;
+    (*env)->DeleteLocalRef(env, jni_cid);
+    return jni_midID;
 }
 
-CAResult_t CAAddServerInfo(u_arraylist_t *serverInfoList, CAServerInfo_t *info)
+bool CACheckJNIException(JNIEnv *env)
 {
-    VERIFY_NON_NULL(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null");
-    VERIFY_NON_NULL(info, CA_ADAPTER_UTILS_TAG, "info is null");
-
-    CAResult_t result = u_arraylist_add(serverInfoList, (void *) info);
-    if (CA_STATUS_OK != result)
+    if ((*env)->ExceptionCheck(env))
     {
-        OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "u_arraylist_add failed!");
+        (*env)->ExceptionDescribe(env);
+        (*env)->ExceptionClear(env);
+        return true;
     }
-    return result;
+    return false;
 }
 
-void CARemoveServerInfo(u_arraylist_t *serverInfoList, int sockFd)
+void CADeleteGlobalReferences(JNIEnv *env)
 {
-    VERIFY_NON_NULL_VOID(serverInfoList, CA_ADAPTER_UTILS_TAG, "serverInfoList is null");
-
-    uint32_t listLength = u_arraylist_length(serverInfoList);
-    for (uint32_t listIndex = 0; listIndex < listLength;)
+    if (g_Context)
     {
-        CAServerInfo_t *info = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
-        if (!info)
-        {
-            listIndex++;
-            continue;
-        }
-
-        if (info->socketFd == sockFd)
-        {
-            if (u_arraylist_remove(serverInfoList, listIndex))
-            {
-                OICFree(info);
-                listLength--;
-            }
-            else
-            {
-                OIC_LOG(ERROR, CA_ADAPTER_UTILS_TAG, "u_arraylist_remove failed!");
-                break;
-            }
-        }
-        else
-        {
-            listIndex++;
-        }
+        (*env)->DeleteGlobalRef(env, g_Context);
+        g_Context = NULL;
     }
-}
 
-void CAClearNetInterfaceInfoList(u_arraylist_t *infoList)
-{
-    uint32_t listLength = u_arraylist_length(infoList);
-    for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
+    if (g_Activity)
     {
-        CANetInfo_t *netInfo = (CANetInfo_t *) u_arraylist_get(infoList, listIndex);
-        if (!netInfo)
-        {
-            continue;
-        }
-        OICFree(netInfo);
+        (*env)->DeleteGlobalRef(env, g_Activity);
+        g_Activity = NULL;
     }
-    u_arraylist_free(&infoList);
 }
+#endif
 
-void CAClearServerInfoList(u_arraylist_t *serverInfoList)
+#ifndef WITH_ARDUINO
+void CALogAdapterStateInfo(CATransportAdapter_t adapter, CANetworkStatus_t state)
 {
-    uint32_t listLength = u_arraylist_length(serverInfoList);
-    for (uint32_t listIndex = 0; listIndex < listLength; listIndex++)
+    OIC_LOG(DEBUG, CA_ADAPTER_UTILS_TAG, "CALogAdapterStateInfo");
+    OIC_LOG(DEBUG, ANALYZER_TAG, "=================================================");
+    CALogAdapterTypeInfo(adapter);
+    if (CA_INTERFACE_UP == state)
     {
-        CAServerInfo_t *serverInfo = (CAServerInfo_t *) u_arraylist_get(serverInfoList, listIndex);
-        if (!serverInfo)
-        {
-            continue;
-        }
-        OICFree(serverInfo);
+        OIC_LOG(INFO, ANALYZER_TAG, "adapter status is changed to CA_INTERFACE_UP");
+    }
+    else
+    {
+        OIC_LOG(INFO, ANALYZER_TAG, "adapter status is changed to CA_INTERFACE_DOWN");
     }
-    u_arraylist_free(&serverInfoList);
+    OIC_LOG(DEBUG, ANALYZER_TAG, "=================================================");
 }
 
-#ifdef __ANDROID__
-void CANativeJNISetContext(JNIEnv *env, jobject context)
+void CALogSendStateInfo(CATransportAdapter_t adapter,
+                        const char *addr, uint16_t port, ssize_t sentLen,
+                        bool isSuccess, const char* message)
 {
-    OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetContext");
+    OIC_LOG(DEBUG, CA_ADAPTER_UTILS_TAG, "CALogSendStateInfo");
+    OIC_LOG(DEBUG, ANALYZER_TAG, "=================================================");
 
-    if (!context)
+    if (true == isSuccess)
     {
-        OIC_LOG(DEBUG, CA_ADAPTER_UTILS_TAG, "context is null");
-
+        OIC_LOG_V(INFO, ANALYZER_TAG, "Send Success, sent length = [%d]", sentLen);
+    }
+    else
+    {
+        OIC_LOG_V(INFO, ANALYZER_TAG, "Send Failure, error message  = [%s]",
+                  message != NULL ? message : "no message");
     }
 
-    g_Context = (*env)->NewGlobalRef(env, context);
-}
-
-void CANativeJNISetJavaVM(JavaVM *jvm)
-{
-    OIC_LOG_V(DEBUG, CA_ADAPTER_UTILS_TAG, "CANativeJNISetJavaVM");
-    g_jvm = jvm;
-}
+    CALogAdapterTypeInfo(adapter);
+    OIC_LOG_V(INFO, ANALYZER_TAG, "Address = [%s]:[%d]", addr, port);
+    OIC_LOG(DEBUG, ANALYZER_TAG, "=================================================");
 
-jobject CANativeJNIGetContext()
-{
-    return g_Context;
+    // samsung log
+    if (true == isSuccess)
+    {
+        OIC_LOG_V(INFO, CA_ADAPTER_UTILS_TAG, "| Analyzer(Retail) | %02x", 1);
+    }
+    else
+    {
+        OIC_LOG_V(INFO, CA_ADAPTER_UTILS_TAG, "| Analyzer(Retail) | %02x", 0);
+    }
 }
 
-JavaVM *CANativeJNIGetJavaVM()
+void CALogAdapterTypeInfo(CATransportAdapter_t adapter)
 {
-    return g_jvm;
+    switch(adapter)
+    {
+        case CA_ADAPTER_IP:
+            OIC_LOG(INFO, ANALYZER_TAG, "Transport Type = [OC_ADAPTER_IP]");
+            break;
+        case CA_ADAPTER_TCP:
+            OIC_LOG(INFO, ANALYZER_TAG, "Transport Type = [OC_ADAPTER_TCP]");
+            break;
+        case CA_ADAPTER_GATT_BTLE:
+            OIC_LOG(INFO, ANALYZER_TAG, "Transport Type = [OC_ADAPTER_GATT_BTLE]");
+            break;
+        case CA_ADAPTER_RFCOMM_BTEDR:
+            OIC_LOG(INFO, ANALYZER_TAG, "Transport Type = [OC_ADAPTER_RFCOMM_BTEDR]");
+            break;
+        default:
+            OIC_LOG_V(INFO, ANALYZER_TAG, "Transport Type = [%d]", adapter);
+            break;
+    }
 }
 #endif
-