[IOT-575] fixed Android Sample App in CA
authorjihwan.seo <jihwan.seo@samsung.com>
Thu, 18 Jun 2015 12:37:45 +0000 (21:37 +0900)
committerErich Keane <erich.keane@intel.com>
Fri, 19 Jun 2015 15:26:19 +0000 (15:26 +0000)
there are some wrong and missed codes
after IPv6 changed

Change-Id: I965dcec243fb9ae59353595ee5a89d8ea881fe34
Signed-off-by: jihwan.seo <jihwan.seo@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1358
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Naga Ashok Jampani <jn.ashok@samsung.com>
Reviewed-by: Erich Keane <erich.keane@intel.com>
resource/csdk/connectivity/samples/android/sample_service/jni/ResourceModel.c
resource/csdk/connectivity/samples/android/sample_service/src/org/iotivity/ca/service/MainActivity.java

index 65006dc..58fb784 100644 (file)
 #define OPTION_INFO_LENGTH 1024
 #define NETWORK_INFO_LENGTH 1024
 
+#define COAP_PREFIX          "coap://"
+#define COAP_PREFIX_LEN      7
+#define COAPS_PREFIX         "coaps://"
+#define COAPS_PREFIX_LEN     8
+
 uint16_t g_localSecurePort = SECURE_DEFAULT_PORT;
 
 void request_handler(const CAEndpoint_t* object, const CARequestInfo_t* requestInfo);
@@ -65,10 +70,16 @@ static uint8_t g_clientTokenLength = NULL;
 static uint16_t g_clientMsgId;
 static char *g_remoteAddress = NULL;
 
+typedef struct
+{
+    char ipAddress[CA_IPADDR_SIZE];
+    uint16_t port;
+} addressSet_t;
+
 // init
 JNIEXPORT void JNICALL
 Java_org_iotivity_ca_service_RMInterface_setNativeResponseListener(JNIEnv *env, jobject obj,
-                                                                jobject listener)
+                                                                   jobject listener)
 {
     LOGI("setNativeResponseListener");
     g_responseListenerObject = (*env)->NewGlobalRef(env, obj);
@@ -257,8 +268,8 @@ Java_org_iotivity_ca_service_RMInterface_RMFindResource(JNIEnv *env, jobject obj
 
 JNIEXPORT void JNICALL
 Java_org_iotivity_ca_service_RMInterface_RMSendRequest(JNIEnv *env, jobject obj, jstring uri,
-                                                    jstring payload, jint selectedNetwork,
-                                                    jint isSecured, jint msgType)
+                                                       jstring payload, jint selectedNetwork,
+                                                       jint isSecured, jint msgType)
 {
     LOGI("selectedNetwork - %d", selectedNetwork);
     CAResult_t res = get_network_type(selectedNetwork);
@@ -270,15 +281,17 @@ Java_org_iotivity_ca_service_RMInterface_RMSendRequest(JNIEnv *env, jobject obj,
     const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
     LOGI("RMSendRequest - %s", strUri);
 
+    addressSet_t address = {};
+    parsing_coap_uri(strUri, &address);
+
     //create remote endpoint
     CAEndpoint_t* endpoint = NULL;
-    res = CACreateEndpoint(CA_DEFAULT_FLAGS, g_selectedNwType, strUri, 0, &endpoint);
-    //ReleaseStringUTFChars for strUri
-    (*env)->ReleaseStringUTFChars(env, uri, strUri);
-
+    res = CACreateEndpoint(CA_DEFAULT_FLAGS, g_selectedNwType, (const char*)address.ipAddress,
+                           address.port, &endpoint);
     if (CA_STATUS_OK != res)
     {
         LOGE("Could not create remote end point");
+        (*env)->ReleaseStringUTFChars(env, uri, strUri);
         return;
     }
 
@@ -294,12 +307,14 @@ Java_org_iotivity_ca_service_RMInterface_RMSendRequest(JNIEnv *env, jobject obj,
         LOGE("token generate error!!");
         // destroy remote endpoint
         CADestroyEndpoint(endpoint);
+        (*env)->ReleaseStringUTFChars(env, uri, strUri);
         return;
     }
 
     char resourceURI[RESOURCE_URI_LENGTH + 1] = { 0 };
 
     get_resource_uri((const CAURI_t) strUri, resourceURI, RESOURCE_URI_LENGTH);
+    (*env)->ReleaseStringUTFChars(env, uri, strUri);
 
     CAInfo_t requestData = { 0 };
     requestData.token = token;
@@ -337,6 +352,18 @@ Java_org_iotivity_ca_service_RMInterface_RMSendRequest(JNIEnv *env, jobject obj,
     }
 
     requestData.type = messageType;
+    requestData.resourceUri = (CAURI_t) malloc(sizeof(resourceURI));
+    if (NULL == requestData.resourceUri)
+    {
+        LOGE("Memory allocation failed!");
+        // destroy token
+        CADestroyToken(token);
+        // destroy remote endpoint
+        CADestroyEndpoint(endpoint);
+        free(requestData.payload);
+        return;
+    }
+    memcpy(requestData.resourceUri, resourceURI, sizeof(resourceURI));
 
     CARequestInfo_t requestInfo = { 0 };
     requestInfo.method = CA_GET;
@@ -356,11 +383,12 @@ Java_org_iotivity_ca_service_RMInterface_RMSendRequest(JNIEnv *env, jobject obj,
     CADestroyEndpoint(endpoint);
 
     free(requestData.payload);
+    free(requestData.resourceUri);
 }
 
 JNIEXPORT void JNICALL
 Java_org_iotivity_ca_service_RMInterface_RMSendReqestToAll(JNIEnv *env, jobject obj, jstring uri,
-                                                        jint selectedNetwork)
+                                                           jint selectedNetwork)
 {
     LOGI("selectedNetwork - %d", selectedNetwork);
     CAResult_t res = get_network_type(selectedNetwork);
@@ -369,15 +397,9 @@ Java_org_iotivity_ca_service_RMInterface_RMSendReqestToAll(JNIEnv *env, jobject
         return;
     }
 
-    const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
-    LOGI("RMSendReqestToAll - %s", strUri);
-
     // create remote endpoint
     CAEndpoint_t *endpoint = NULL;
-    res = CACreateEndpoint(CA_DEFAULT_FLAGS, g_selectedNwType, strUri, 0, &endpoint);
-
-    //ReleaseStringUTFChars for strUri
-    (*env)->ReleaseStringUTFChars(env, uri, strUri);
+    res = CACreateEndpoint(CA_DEFAULT_FLAGS, g_selectedNwType, NULL, 0, &endpoint);
 
     if (CA_STATUS_OK != res)
     {
@@ -406,6 +428,10 @@ Java_org_iotivity_ca_service_RMInterface_RMSendReqestToAll(JNIEnv *env, jobject
     requestData.payload = "Temp Json Payload";
     requestData.type = CA_MSG_NONCONFIRM;
 
+    const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
+    LOGI("resourceUri - %s", strUri);
+    requestData.resourceUri = (CAURI_t)strUri;
+
     CARequestInfo_t requestInfo = { 0 };
     requestInfo.method = CA_GET;
     requestInfo.isMulticast = true;
@@ -426,15 +452,18 @@ Java_org_iotivity_ca_service_RMInterface_RMSendReqestToAll(JNIEnv *env, jobject
         g_lastRequestTokenLength = tokenLength;
     }
 
+    //ReleaseStringUTFChars for strUri
+    (*env)->ReleaseStringUTFChars(env, uri, strUri);
+
     // destroy remote endpoint
     CADestroyEndpoint(endpoint);
 }
 
 JNIEXPORT void JNICALL
 Java_org_iotivity_ca_service_RMInterface_RMSendResponse(JNIEnv *env, jobject obj,
-                                                     jint selectedNetwork,
-                                                     jint isSecured, jint msgType,
-                                                     jint responseValue)
+                                                        jint selectedNetwork,
+                                                        jint isSecured, jint msgType,
+                                                        jint responseValue)
 {
     LOGI("RMSendResponse");
 
@@ -458,6 +487,7 @@ Java_org_iotivity_ca_service_RMInterface_RMSendResponse(JNIEnv *env, jobject obj
     CAInfo_t responseData = { 0 };
     responseData.type = messageType;
     responseData.messageId = g_clientMsgId;
+    responseData.resourceUri = (CAURI_t)g_resourceUri;
 
     CAResponseInfo_t responseInfo = { 0 };
 
@@ -507,10 +537,16 @@ Java_org_iotivity_ca_service_RMInterface_RMSendResponse(JNIEnv *env, jobject obj
 }
 
 JNIEXPORT void JNICALL
+Java_org_iotivity_ca_service_RMInterface_RMAdvertiseResource(JNIEnv *env, jobject obj, jstring uri)
+{
+    LOGE("Java_org_iotivity_ca_service_RMInterface_RMAdvertiseResource not implemented");
+}
+
+JNIEXPORT void JNICALL
 Java_org_iotivity_ca_service_RMInterface_RMSendNotification(JNIEnv *env, jobject obj, jstring uri,
-                                                         jstring payload, jint selectedNetwork,
-                                                         jint isSecured, jint msgType,
-                                                         jint responseValue)
+                                                            jstring payload, jint selectedNetwork,
+                                                            jint isSecured, jint msgType,
+                                                            jint responseValue)
 {
     LOGI("selectedNetwork - %d", selectedNetwork);
 
@@ -524,9 +560,14 @@ Java_org_iotivity_ca_service_RMInterface_RMSendNotification(JNIEnv *env, jobject
     const char* strUri = (*env)->GetStringUTFChars(env, uri, NULL);
     LOGI("RMSendNotification - %s", strUri);
 
+    addressSet_t address = {};
+    parsing_coap_uri(strUri, &address);
+
     //create remote endpoint
     CAEndpoint_t* endpoint = NULL;
-    if (CA_STATUS_OK != CACreateEndpoint(CA_DEFAULT_FLAGS, g_selectedNwType, strUri, 0, &endpoint))
+    if (CA_STATUS_OK != CACreateEndpoint(CA_DEFAULT_FLAGS, g_selectedNwType,
+                                         (const char*)address.ipAddress,
+                                         address.port, &endpoint))
     {
         //ReleaseStringUTFChars for strUri
         (*env)->ReleaseStringUTFChars(env, uri, strUri);
@@ -557,6 +598,17 @@ Java_org_iotivity_ca_service_RMInterface_RMSendNotification(JNIEnv *env, jobject
     CAInfo_t responseData = { 0 };
     responseData.token = token;
     responseData.tokenLength = tokenLength;
+    responseData.resourceUri = (CAURI_t) malloc(sizeof(resourceURI));
+    if (NULL == responseData.resourceUri)
+    {
+        LOGE("Memory allocation failed!");
+        // destroy token
+        CADestroyToken(token);
+        // destroy remote endpoint
+        CADestroyEndpoint(endpoint);
+        return;
+    }
+    memcpy(responseData.resourceUri, resourceURI, sizeof(resourceURI));
 
     if (1 == isSecured)
     {
@@ -569,6 +621,8 @@ Java_org_iotivity_ca_service_RMInterface_RMSendNotification(JNIEnv *env, jobject
             CADestroyToken(token);
             // destroy remote endpoint
             CADestroyEndpoint(endpoint);
+
+            free(responseData.resourceUri);
             return;
         }
         snprintf(responseData.payload, length, SECURE_INFO_DATA, resourceURI, g_localSecurePort);
@@ -584,6 +638,8 @@ Java_org_iotivity_ca_service_RMInterface_RMSendNotification(JNIEnv *env, jobject
             CADestroyToken(token);
             // destroy remote endpoint
             CADestroyEndpoint(endpoint);
+
+            free(responseData.resourceUri);
             return;
         }
         snprintf(responseData.payload, length, NORMAL_INFO_DATA, resourceURI);
@@ -610,10 +666,12 @@ Java_org_iotivity_ca_service_RMInterface_RMSendNotification(JNIEnv *env, jobject
     CADestroyEndpoint(endpoint);
 
     free(responseData.payload);
+    free(responseData.resourceUri);
 }
 
 JNIEXPORT void JNICALL
-Java_org_iotivity_ca_service_RMInterface_RMSelectNetwork(JNIEnv *env, jobject obj, jint networkType)
+Java_org_iotivity_ca_service_RMInterface_RMSelectNetwork(JNIEnv *env, jobject obj,
+                                                         jint networkType)
 {
     LOGI("RMSelectNetwork Type : %d", networkType);
 
@@ -624,7 +682,8 @@ Java_org_iotivity_ca_service_RMInterface_RMSelectNetwork(JNIEnv *env, jobject ob
 }
 
 JNIEXPORT void JNICALL
-Java_org_iotivity_ca_service_RMInterface_RMUnSelectNetwork(JNIEnv *env, jobject obj, jint networkType)
+Java_org_iotivity_ca_service_RMInterface_RMUnSelectNetwork(JNIEnv *env, jobject obj,
+                                                           jint networkType)
 {
     LOGI("RMUnSelectNetwork Type : %d", networkType);
 
@@ -912,7 +971,7 @@ void request_handler(const CAEndpoint_t* object, const CARequestInfo_t* requestI
 
             CAEndpoint_t *endpoint = NULL;
             if (CA_STATUS_OK != CACreateEndpoint(CA_SECURE,
-                        object->adapter, uri, securePort, &endpoint))
+                        object->adapter, object->addr, securePort, &endpoint))
             {
                 LOGE("Failed to create duplicate of remote endpoint!");
                 free(uri);
@@ -1218,3 +1277,132 @@ CAResult_t get_remote_address(CATransportAdapter_t transportType, const char *ad
 
     return CA_STATUS_OK;
 }
+
+
+void parsing_coap_uri(const char* uri, addressSet_t* address)
+{
+    if (NULL == uri || NULL == address)
+    {
+        LOGE("parameter is null");
+        return;
+    }
+
+    // parse uri
+    // #1. check prefix
+    uint8_t startIndex = 0;
+    if (strncmp(COAPS_PREFIX, uri, COAPS_PREFIX_LEN) == 0)
+    {
+        LOGI("uri has '%s' prefix", COAPS_PREFIX);
+        startIndex = COAPS_PREFIX_LEN;
+    }
+    else if (strncmp(COAP_PREFIX, uri, COAP_PREFIX_LEN) == 0)
+    {
+        LOGI("uri has '%s' prefix", COAP_PREFIX);
+        startIndex = COAP_PREFIX_LEN;
+    }
+
+    // #2. copy uri for parse
+    int32_t len = strlen(uri) - startIndex;
+
+    if (len <= 0)
+    {
+        LOGE("uri length is 0!");
+        return;
+    }
+
+    char *cloneUri = (char *) calloc(len + 1, sizeof(char));
+    if (NULL == cloneUri)
+    {
+        LOGE("Out of memory");
+        return;
+    }
+
+    memcpy(cloneUri, &uri[startIndex], sizeof(char) * len);
+    cloneUri[len] = '\0';
+
+    char *pAddress = cloneUri;
+    char *pResourceUri = NULL;
+
+    int32_t i = 0;
+    for (i = 0; i < len; i++)
+    {
+        if (cloneUri[i] == '/')
+        {
+            // separate
+            cloneUri[i] = 0;
+            pResourceUri = &cloneUri[i + 1];
+            break;
+        }
+    }
+    LOGI("pAddress : %s", pAddress);
+
+    int res = get_address_set(pAddress, address);
+    if (res == -1)
+    {
+        LOGE("address parse error");
+
+        free(cloneUri);
+        return;
+    }
+    return;
+}
+
+int get_address_set(const char *pAddress, addressSet_t* outAddress)
+{
+    if (NULL == pAddress || NULL == outAddress)
+    {
+        LOGE("parameter is null");
+        return -1;
+    }
+
+    int32_t len = strlen(pAddress);
+    int32_t isIp = 0;
+    int32_t ipLen = 0;
+
+    int32_t i = 0;
+    for (i = 0; i < len; i++)
+    {
+        if (pAddress[i] == '.')
+        {
+            isIp = 1;
+        }
+
+        // found port number start index
+        if (isIp && pAddress[i] == ':')
+        {
+            ipLen = i;
+            break;
+        }
+    }
+
+    if (isIp)
+    {
+        if(ipLen && ipLen < sizeof(outAddress->ipAddress))
+        {
+            strncpy(outAddress->ipAddress, pAddress, ipLen);
+            outAddress->ipAddress[ipLen] = '\0';
+        }
+        else if (!ipLen && len < sizeof(outAddress->ipAddress))
+        {
+            strncpy(outAddress->ipAddress, pAddress, len);
+            outAddress->ipAddress[len] = '\0';
+        }
+        else
+        {
+            LOGE("IP Address too long: %d", ipLen==0 ? len : ipLen);
+            return -1;
+        }
+
+        if (ipLen > 0)
+        {
+            outAddress->port = atoi(pAddress + ipLen + 1);
+        }
+    }
+    else
+    {
+        strncpy(outAddress->ipAddress, pAddress, len);
+        outAddress->ipAddress[len] = '\0';
+    }
+
+    return isIp;
+}
index 9bbe94f..5cdef44 100644 (file)
@@ -22,8 +22,8 @@ public class MainActivity extends Activity {
 
     private final static String TAG = "MainActivity";
 
-    private final CharSequence[] mNetworkCheckBoxItems = { Network.IPV4.name(),
-            Network.IPV6.name(), Network.EDR.name(), Network.LE.name()};
+    private final CharSequence[] mNetworkCheckBoxItems = { Network.IP.name(),
+            Network.LE.name(), Network.EDR.name()};
 
     private final CharSequence[] mDTLSCheckBoxItems = { DTLS.UNSECURED.name(),
             DTLS.SECURED.name() };
@@ -44,7 +44,7 @@ public class MainActivity extends Activity {
     };
 
     private enum Network {
-        IPV4, IPV6, EDR, LE
+        IP, LE, EDR
     };
 
     private enum DTLS {
@@ -64,9 +64,9 @@ public class MainActivity extends Activity {
             false, false, false, false
     };
 
-    private int mSelectedItems[] = { 0, 0, 0, 0 };
+    private int mSelectedItems[] = { 0, 0, 0 };
 
-    private int mUnSelectedItems[] = { 0, 0, 0, 0 };
+    private int mUnSelectedItems[] = { 0, 0, 0 };
 
     private Mode mCurrentMode = Mode.UNKNOWN;
 
@@ -149,12 +149,11 @@ public class MainActivity extends Activity {
     /**
      * Defined ConnectivityType in cacommon.c
      *
-     * CA_IPV4 = (1 << 0) CA_IPV6 = (1 << 1) CA_EDR = (1 << 2) CA_LE = (1 << 3)
+     * CA_IP = (1 << 0) CA_LE = (1 << 2) CA_EDR = (1 << 3)
      */
-    private int CA_IPV4 = (1 << 0);
-    private int CA_IPV6 = (1 << 1);
+    private int CA_IP = (1 << 0);
+    private int CA_LE = (1 << 1);
     private int CA_EDR = (1 << 2);
-    private int CA_LE = (1 << 3);
     private int isSecured = 0;
     private int msgType = 1;
     private int responseValue = 0;
@@ -415,8 +414,7 @@ public class MainActivity extends Activity {
         public void onClick(View v) {
 
             DLog.v(TAG, "FindResource click");
-            RM.RMFindResource(mUri_ed.getText().toString());
-
+            RM.RMSendReqestToAll(mReqToAllData_ed.getText().toString(), selectedNetwork);
         }
     };
 
@@ -568,10 +566,7 @@ public class MainActivity extends Activity {
 
                         for (int i = 0; i < mSelectedItems.length; i++) {
                             if (mSelectedItems[i] == 1) {
-                                if(i != 1)
-                                    interestedNetwork |= (1 << i);
-                                else
-                                    checkNotSupportedTransport("Not Supported Transport");
+                                interestedNetwork |= (1 << i);
                             }
                         }
                         if(0 != interestedNetwork)
@@ -581,11 +576,7 @@ public class MainActivity extends Activity {
 
                         for (int i = 0; i < mUnSelectedItems.length; i++) {
                             if (mUnSelectedItems[i] == 1) {
-                                if (i != 1)
-                                    uninterestedNetwork |= (1 << i);
-                                else
-                                    checkNotSupportedTransport("Not Supported Transport");
-                                mUnSelectedItems[i] = 0;
+                                uninterestedNetwork |= (1 << i);
                             }
                         }
                         if(0 != uninterestedNetwork)
@@ -595,20 +586,6 @@ public class MainActivity extends Activity {
                 }).show();
     }
 
-    private void checkNotSupportedTransport(String title) {
-
-        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
-        builder.setTitle(title).
-        setMessage("Selected Transport Not Supported")
-        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
-
-            @Override
-            public void onClick(DialogInterface dialog, int which) {
-
-            }
-        }).show();
-    }
-
     private void checkMsgSecured(String title) {
 
         AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
@@ -775,17 +752,16 @@ public class MainActivity extends Activity {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {
 
-                        if (selectedNetworkType == Network.IPV4.ordinal()) {
-                            selectedNetwork = CA_IPV4;
-                            DLog.v(TAG, "Selected Network is CA_IPV4");
-                        } else if (selectedNetworkType == Network.EDR.ordinal()) {
-                            selectedNetwork = CA_EDR;
-                            DLog.v(TAG, "Selected Network is EDR");
+                        if (selectedNetworkType == Network.IP.ordinal()) {
+                            selectedNetwork = CA_IP;
+                            DLog.v(TAG, "Selected Network is CA_IP");
                         } else if (selectedNetworkType == Network.LE.ordinal()) {
                             selectedNetwork = CA_LE;
                             DLog.v(TAG, "Selected Network is LE");
-                        }
-                        else {
+                        } else if (selectedNetworkType == Network.EDR.ordinal()) {
+                            selectedNetwork = CA_EDR;
+                            DLog.v(TAG, "Selected Network is EDR");
+                        } else {
                             DLog.v(TAG, "Selected Network is NULL");
                             selectedNetwork = -1;
                         }