adds http module 21/176421/1
authorJeonghoon Park <jh1979.park@samsung.com>
Thu, 19 Apr 2018 06:35:02 +0000 (15:35 +0900)
committerJeonghoon Park <jh1979.park@samsung.com>
Thu, 19 Apr 2018 06:35:02 +0000 (15:35 +0900)
Change-Id: I57325d113f72a2fc9fc3168b2b4dc98e46fa3d19

daemon/include/ttd-http.h [new file with mode: 0644]
daemon/src/ttd-http.c [new file with mode: 0644]

diff --git a/daemon/include/ttd-http.h b/daemon/include/ttd-http.h
new file mode 100644 (file)
index 0000000..7f56302
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Flora License, Version 1.1 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __TT_DAEMON_HTTP_H__
+#define __TT_DAEMON_HTTP_H__
+
+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);
+
+#endif /* __TT_DAEMON_HTTP_H__ */
diff --git a/daemon/src/ttd-http.c b/daemon/src/ttd-http.c
new file mode 100644 (file)
index 0000000..2baeeb4
--- /dev/null
@@ -0,0 +1,113 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Flora License, Version 1.1 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <curl/curl.h>
+#include <glib.h>
+#include "ttd-log.h"
+
+#define CERT_FILE_PATH "/opt/share/cert-svc/ca-certificate.crt"
+
+int ttd_http_init(void)
+{
+       curl_global_init(CURL_GLOBAL_DEFAULT);
+       return 0;
+}
+
+int ttd_http_fini(void)
+{
+       curl_global_cleanup();
+       return 0;
+}
+
+static size_t _get_cmd_write(void *ptr, size_t size, size_t nmemb, void *data)
+{
+       size_t res_size = 0;
+       char **received = data;
+
+       res_size = size * nmemb;
+
+       if (received && res_size > 0)
+               *received = g_strndup((const char *)ptr, res_size);
+       else
+               _E("fail to get response [res size : %d]", res_size);
+
+       return res_size;
+}
+
+int ttd_http_get_cloud_cmd(const char *url, char **cmd_json)
+{
+       int ret = 0;
+       CURL *curl = NULL;
+       CURLcode res = CURLE_OK;
+
+       retvm_if(!url, -1, "url is null");
+       retvm_if(!cmd_json, -1, "cmd_json is null");
+
+       curl = curl_easy_init();
+
+       retvm_if(!curl, NULL, "failed to curl_easy_init()");
+
+       curl_easy_setopt(curl, CURLOPT_URL, url);
+       curl_easy_setopt(curl, CURLOPT_CAPATH, CERT_FILE_PATH);
+       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, _get_cmd_write);
+       curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)cmd_json);
+
+       res = curl_easy_perform(curl);
+       if (res != CURLE_OK) {
+               _E("curl_easy_perform() failed: %s", curl_easy_strerror(res));
+               ret = -1;
+       }
+       _D("received cmd : %s", *cmd_json ? *cmd_json : "NULL");
+
+       curl_easy_cleanup(curl);
+
+       return ret;
+}
+
+int ttd_http_post_cmd_result(const char *url, const char *result_json)
+{
+       int ret = 0;
+       CURL *curl = NULL;
+       CURLcode res = CURLE_OK;
+       struct curl_slist *headers = NULL;
+
+       retvm_if(!url, -1, "url is null");
+       retvm_if(!result_json, -1, "result_json is null");
+
+       curl = curl_easy_init();
+
+       retvm_if(!curl, NULL, "failed to curl_easy_init()");
+
+       headers = curl_slist_append(headers, "Accept: application/json");
+       headers = curl_slist_append(headers, "Content-Type: application/json");
+
+       /* TODO : what should I do before post result */
+       curl_easy_setopt(curl, CURLOPT_URL, url);
+       curl_easy_setopt(curl, CURLOPT_CAPATH, CERT_FILE_PATH);
+       curl_easy_setopt(curl, CURLOPT_POST, 1L);
+       curl_easy_setopt(curl, CURLOPT_POSTFIELDS, result_json);
+
+       res = curl_easy_perform(curl);
+       if (res != CURLE_OK) {
+               _E("curl_easy_perform() failed: %s", curl_easy_strerror(res));
+               ret = -1;
+       }
+
+       curl_slist_free_all(headers);
+       curl_easy_cleanup(curl);
+
+       return ret;
+}