examples/dtls_client: support tash argument for testing
authorJunyeon LEE <junyeon2.lee@samsung.com>
Wed, 19 Apr 2017 09:55:37 +0000 (18:55 +0900)
committerHeesub Shin <heesub.shin@samsung.com>
Sat, 6 May 2017 10:51:22 +0000 (19:51 +0900)
After this commit, dtls client example supports ip address input.

Change-Id: I869e5dc26a862486b2ab40694c5968b7ea03c9f0
Signed-off-by: Junyeon LEE <junyeon2.lee@samsung.com>
apps/examples/dtls_client/dtls_client_main.c

index 8f83fba..ad7f1b7 100644 (file)
 #define READ_TIMEOUT_MS 1000
 #define MAX_RETRY       5
 
+#define USAGE_DTLS \
+"\n--------------------------USAGE----------------------------\n"      \
+"dtlsc\n"                                                                                                                      \
+"dtlsc server_addr=xxx.xxx.xxx.xxx\n"                                                          \
+"-----------------------------------------------------------\n"
+
 #define DEBUG_LEVEL 0
 
 /*
@@ -93,7 +99,12 @@ static void my_debug(void *ctx, int level, const char *file, int line, const cha
 
 int dtls_client_cb(void *args)
 {
-       int ret;
+       int argc;
+       char **argv;
+       char *p;
+       char *q;
+
+       int ret = 0;
        int len;
        mbedtls_net_context server_fd;
        uint32_t flags;
@@ -112,6 +123,31 @@ int dtls_client_cb(void *args)
        mbedtls_debug_set_threshold(DEBUG_LEVEL);
 #endif
 
+       char *server_addr = SERVER_ADDR;
+
+       argc = ((struct pthread_arg *)args)->argc;
+       argv = ((struct pthread_arg *)args)->argv;
+
+       if (argc == 0) {
+usage:
+               printf(USAGE_DTLS);
+               goto exit;
+       } else {
+               int i;
+               for (i = 1; i < argc; i++) {
+                       p = argv[i];
+                       if ((q = strchr(p, '=')) == NULL) {
+                               goto usage;
+                       }
+                       *q++ = '\0';
+                       if (strcmp(p, "server_addr") == 0) {
+                               server_addr = q;
+                       } else {
+                               goto usage;
+                       }
+               }
+       }
+
        /*
         * 0. Initialize the RNG and the session data
         */
@@ -149,10 +185,10 @@ int dtls_client_cb(void *args)
        /*
         * 1. Start the connection
         */
-       mbedtls_printf("  . Connecting to udp/%s/%s...", SERVER_NAME, SERVER_PORT);
+       mbedtls_printf("  . Connecting to udp/%s/%s...", server_addr, SERVER_PORT);
        fflush(stdout);
 
-       if ((ret = mbedtls_net_connect(&server_fd, SERVER_ADDR, SERVER_PORT, MBEDTLS_NET_PROTO_UDP)) != 0) {
+       if ((ret = mbedtls_net_connect(&server_fd, server_addr, SERVER_PORT, MBEDTLS_NET_PROTO_UDP)) != 0) {
                mbedtls_printf(" failed\n  ! mbedtls_net_connect returned %d\n\n", ret);
                goto exit;
        }