updates http related fucntions to get response code 17/179417/2
authorJeonghoon Park <jh1979.park@samsung.com>
Fri, 18 May 2018 01:34:31 +0000 (10:34 +0900)
committerJin Yoon <jinny.yoon@samsung.com>
Fri, 18 May 2018 04:57:08 +0000 (04:57 +0000)
Change-Id: I9c5b58f130496411e4d96a1ac5eec045ebe4d022

daemon/include/ttd-http.h
daemon/src/tizen-things-daemon.c
daemon/src/ttd-http.c

index a5bc285..f7d9ece 100644 (file)
@@ -19,8 +19,9 @@
 
 int ttd_http_init(void);
 int ttd_http_fini(void);
-int ttd_http_get_cloud_cmd(const char *url, char **cmd_json);
-int ttd_http_post_cmd_result(const char *url, const char *result_json);
-int ttd_http_post_data(const char *url, const char *data);
+int ttd_http_get_cloud_cmd(const char *url, char **cmd_json, long *res_code);
+int ttd_http_post_cmd_result(
+               const char *url, const char *result_json, long *res_code);
+int ttd_http_post_data(const char *url, const char *data, long *res_code);
 
 #endif /* __TT_DAEMON_HTTP_H__ */
index 8d3665b..567104a 100644 (file)
@@ -32,7 +32,7 @@
 
 #ifndef SERVER_URL
 /* TODO : remove it after test */
-#define TEST_SERVER_URL "http://apitest.showiot.xyz/api/cmd?&target=test-page-device&owner=test-page&state=created"
+#define TEST_SERVER_URL "http://test.showiot.xyz/api/cmd?&target=test-page-device&owner=test-page&state=created"
 #define SERVER_URL TEST_SERVER_URL
 #endif
 
@@ -175,10 +175,10 @@ static int __say_hello_to_cloud(void *data)
 {
        int ret  = 0;
        char *cmd = NULL;
+       long res_code = 0;
 
-       ret = ttd_http_get_cloud_cmd(__ttd_get_cloud_url(), &cmd);
-
-       retvm_if(ret, -1, "failed to get cmd");
+       ret = ttd_http_get_cloud_cmd(__ttd_get_cloud_url(), &cmd, &res_code);
+       retvm_if(ret, -1, "failed to get cmd [%ld]", res_code);
 
        if (cmd) {
                GList *cmd_list = NULL;
index 75dce99..edabe65 100644 (file)
@@ -122,11 +122,12 @@ static size_t _get_cmd_write(void *ptr, size_t size, size_t nmemb, void *data)
        return res_size;
 }
 
-int ttd_http_get_cloud_cmd(const char *url, char **cmd_json)
+int ttd_http_get_cloud_cmd(const char *url, char **cmd_json, long *res_code)
 {
        int ret = 0;
        CURL *curl = NULL;
        CURLcode res = CURLE_OK;
+       long r_code = 0;
 
        retvm_if(!http_initialized, -1, "http is not initialized yet");
        retvm_if(!url, -1, "url is null");
@@ -147,7 +148,11 @@ int ttd_http_get_cloud_cmd(const char *url, char **cmd_json)
                g_free(*cmd_json);
                ret = -1;
        }
-       _D("received cmd : %s", *cmd_json ? *cmd_json : "NULL");
+       curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &r_code);
+       if (res_code)
+               *res_code = r_code;
+
+       _D("received code[%ld], cmd : %s", r_code, *cmd_json ? *cmd_json : "NULL");
 
        curl_easy_cleanup(curl);
 
@@ -155,11 +160,12 @@ int ttd_http_get_cloud_cmd(const char *url, char **cmd_json)
 }
 
 static int __ttd_http_post(const char *url,
-       const char *msg_body, struct curl_slist *headers)
+       const char *msg_body, struct curl_slist *headers, long *res_code)
 {
        int ret = 0;
        CURL *curl = NULL;
        CURLcode res = CURLE_OK;
+       long r_code = 0;
 
        retvm_if(!http_initialized, -1, "http is not initialized yet");
        retvm_if(!url, -1, "url is null");
@@ -183,6 +189,11 @@ static int __ttd_http_post(const char *url,
                _E("curl_easy_perform() failed: %s", curl_easy_strerror(res));
                ret = -1;
        }
+
+       curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &r_code);
+       if (res_code)
+               *res_code = r_code;
+
        if (headers)
                curl_slist_free_all(headers);
        curl_easy_cleanup(curl);
@@ -190,7 +201,8 @@ static int __ttd_http_post(const char *url,
        return ret;
 }
 
-int ttd_http_post_cmd_result(const char *url, const char *result_json)
+int ttd_http_post_cmd_result(
+       const char *url, const char *result_json, long *res_code)
 {
        struct curl_slist *headers = NULL;
 
@@ -198,10 +210,10 @@ int ttd_http_post_cmd_result(const char *url, const char *result_json)
        headers = curl_slist_append(headers, "Content-Type: application/json");
        /* add here http headers to set new one */
 
-       return __ttd_http_post(url, result_json, headers);
+       return __ttd_http_post(url, result_json, headers, res_code);
 }
 
-int ttd_http_post_data(const char *url, const char *data)
+int ttd_http_post_data(const char *url, const char *data, long *res_code)
 {
        struct curl_slist *headers = NULL;
 
@@ -209,5 +221,5 @@ int ttd_http_post_data(const char *url, const char *data)
        headers = curl_slist_append(headers, "Content-Type: application/json");
        /* add here http headers to set new one */
 
-       return __ttd_http_post(url, data, headers);
+       return __ttd_http_post(url, data, headers, res_code);
 }