Download firmware package when wwst. 88/147288/1
authorhhk86.heo <hhk86.heo@samsung.com>
Mon, 4 Sep 2017 01:15:59 +0000 (10:15 +0900)
committerhhk86.heo <hhk86.heo@samsung.com>
Mon, 4 Sep 2017 01:15:59 +0000 (10:15 +0900)
Change-Id: I3027a5739bef159ad3045324c0529970c8849bea

CMakeLists.txt
fmwup-resource.manifest [new file with mode: 0755]
inc/fmwup_resource_internal.h
packaging/fmwup-resource.spec
src/fmwup_resource.c
src/fmwup_resource_http.c
src/fmwup_resource_internal.c
test/CMakeLists.txt

index 3f160f2cd5d1d2a0e6bc444b4960f08ee55b2b87..7052ba515ee0ffda78f41f7b236f077659a319c0 100644 (file)
@@ -1,5 +1,5 @@
 CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
-PROJECT(fmwup_resource)
+PROJECT(fmwup-resource)
 SET(VERSION_MAJOR 0)
 SET(VERSION "${VERSION_MAJOR}.1.0")
 SET(LIBDIR ${LIB_INSTALL_DIR})
@@ -22,6 +22,8 @@ INSTALL(FILES ${API-HEADERS} DESTINATION include/fmwup-resource)
 CONFIGURE_FILE(fmwup-resource.pc.in fmwup-resource.pc @ONLY)
 INSTALL(FILES ${CMAKE_BINARY_DIR}/fmwup-resource.pc DESTINATION ${LIBDIR}/pkgconfig)
 
+ADD_DEFINITIONS(-D_USE_WWST_)
+
 INCLUDE(FindPkgConfig)
 PKG_CHECK_MODULES(fmwup_pkgs REQUIRED
        capi-appfw-application
diff --git a/fmwup-resource.manifest b/fmwup-resource.manifest
new file mode 100755 (executable)
index 0000000..50eefc9
--- /dev/null
@@ -0,0 +1,5 @@
+<manifest>
+       <request>
+               <domain name="_" />
+       </request>
+</manifest>
\ No newline at end of file
index 5c31b8e05fbc6edc9852c84dd99995238dd52323..27d568dfa93b52a0a206ca536bd3ee1502ae6fa6 100644 (file)
@@ -56,6 +56,9 @@
 #define FWR_LOGI(fmt, args...)  LOGI(fmt, ##args)
 #define FWR_LOGE(fmt, args...)  LOGE(fmt, ##args)
 
+#define FWR_ENTER      FWR_LOGD("(%s) ENTER", __FUNCTION__);
+#define FWR_EXIT       FWR_LOGD("(%s) EXIT", __FUNCTION__);
+
 typedef struct {
        /* Update Property */
     int state;                                 //[R][M] fmwup_state_e - 0: Idle, 1: Downloading, 2: Downloaded, 3: Updating
index 01433851372ebf7772653c19bcc8083065f8f16b..22200859ec5feb5bacd3d06e90045ac1b3af1ca5 100644 (file)
@@ -45,7 +45,7 @@ cmake . -DCMAKE_INSTALL_PREFIX=%{_prefix} \
 make %{?jobs:-j%jobs}
 
 %install
-
 %make_install
 
 %clean
@@ -61,14 +61,17 @@ rm -rf %{buildroot}
 /usr/bin/vconftool set -t string db/private/firmware_update/package_md5 0 -s tizen::vconf::platform::rw
 mkdir -p /opt/usr/data/ua-client
 
+chsmack -a "_" ./usr/lib/libfmwup-resource.so*
+
 %postun 
 /sbin/ldconfig
 
 %files
-%{_libdir}/libfmwup_resource.so*
+%defattr(-,root,root,-)
+%{_libdir}/libfmwup-resource.so*
 
 %files devel
-%{_libdir}/libfmwup_resource.so
+%{_libdir}/libfmwup-resource.so
 %{_libdir}/pkgconfig/fmwup-resource.pc
 %{_includedir}/fmwup-resource/*
 
@@ -77,3 +80,5 @@ mkdir -p /opt/usr/data/ua-client
 %{_bindir}/fmwup_test
 #%config(noreplace) /opt/usr/data/ua-client/thawte.cert.pem
 #%config(noreplace) /opt/usr/data/ua-client/oic_svr_db_client.dat
+
+
index bc8cf9f367a75f77b17c8e8b0426ddafba387b71..84e5827a0b1bfa9a27bcef56dc730d3f93b0e16b 100644 (file)
@@ -45,7 +45,7 @@ int fmwup_destory_firmware_resource(OCResourceHandle firmware_res_h)
 
        if (firmware_res_h == g_firmware_resource->resource_handle) {
                if (OCDeleteResource(g_firmware_resource->resource_handle) != OC_STACK_OK) {
-                       FWR_LOGD("OCDeleteResource() failed for firmware_resource");
+                       FWR_LOGE("OCDeleteResource() failed for firmware_resource");
                }
 
                g_free(g_firmware_resource->current_version);
@@ -69,6 +69,7 @@ int fmwup_set_firmware_download_path(const char *download_path)
        g_free(g_download_path);
        g_download_path = NULL;
        g_download_path = g_strdup(download_path);
+       FWR_LOGI("g_download_path [%s]", g_download_path);
 
        return FMWUP_SUCCESS;
 }
index 3a743dcaa9e844aa127080df68e3c2be34202ed0..e97a2243bba257f06f8f3bb8d7a8a96aa2b26136 100644 (file)
@@ -20,18 +20,22 @@ void _curl_set_response(CURL *curl,
                char **res_body,
                void *user_data)
 {
+       FWR_ENTER
        CURLcode curl_ret_code;
 
        long response = 0;
        curl_ret_code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
-       if (CURLE_OK != curl_ret_code)
+       if (CURLE_OK != curl_ret_code) {
+               FWR_LOGE("curl_easy_perform(): [%s][%d]", curl_easy_strerror(curl_ret_code), curl_ret_code);
                return;
+       }
 
        char *tmp_header = g_strndup((const gchar *)response_header->data, response_header->len);
        char *tmp_body = g_strndup((const gchar *)response_body->data, response_body->len);
 
        *res_header = tmp_header;
        *res_body = tmp_body;
+       FWR_EXIT
 }
 
 
@@ -121,7 +125,7 @@ static void _curl_set_request_headers(CURL *curl)
 
 int fmwup_http_send_request(fmwup_http_req_e type, char *req_url, char **res_header, char **res_body)
 {
-       //LOG("Enter http_send_request()");
+       FWR_ENTER
 
        CURL *curl;
        GByteArray *response_header = NULL;
@@ -148,7 +152,7 @@ int fmwup_http_send_request(fmwup_http_req_e type, char *req_url, char **res_hea
 
        //LOG("Start curl_easy_perform......");
        error_code = curl_easy_perform(curl);
-       //LOG("curl_easy_perform(curl): %s (%d)", curl_easy_strerror(error_code), error_code);
+       FWR_LOGI("curl_easy_perform(): [%s][%d]", curl_easy_strerror(error_code), error_code);
 
        if (error_code == CURLE_ABORTED_BY_CALLBACK) {
                ret = -1;
@@ -161,6 +165,7 @@ int fmwup_http_send_request(fmwup_http_req_e type, char *req_url, char **res_hea
        _curl_set_response(curl, response_header, response_body, res_header, res_body, NULL);
 
 _END_OF_FUNC_:
+
        if (response_header) {
                g_byte_array_free(response_header, TRUE);
        }
@@ -169,41 +174,45 @@ _END_OF_FUNC_:
        }
 
        curl_easy_cleanup(curl);
+       FWR_EXIT
        return ret;
 }
 
 
 int fmwup_http_download_file(const char *download_url, const char *download_path)
 {
-       //LOG("http_download_file() enter");
-
-    if (!download_url)
-        return -1;
-
-    int ret = 0;
-    CURL *curl;
-    FILE *fp;
-    CURLcode error_code;
-
-    curl = curl_easy_init();
-    if (curl)
-    {
-        fp = fopen(download_path, "wb");
-        curl_easy_setopt(curl, CURLOPT_URL, download_url);
-        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
-        curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
-        error_code = curl_easy_perform(curl);
-        //LOG("curl_easy_perform() [%d]", error_code);
-        curl_easy_cleanup(curl);
-        fclose(fp);
-
-        if (error_code != CURLE_OK) {
-               remove(download_path);
-               ret = -1;
-        }
-    } else {
-        ret = -1;
-    }
-
-    return ret;
+       FWR_ENTER
+
+       if (!download_url || !download_path) {
+               FWR_LOGE("Invalid parameter");
+               return -1;
+       }
+
+       int ret = 0;
+       CURL *curl;
+       FILE *fp;
+       CURLcode error_code;
+
+       curl = curl_easy_init();
+       if (curl)
+       {
+               fp = fopen(download_path, "wb");
+               curl_easy_setopt(curl, CURLOPT_URL, download_url);
+               curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL);
+               curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
+               error_code = curl_easy_perform(curl);
+               FWR_LOGI("curl_easy_perform() [%s][%d]", curl_easy_strerror(error_code), error_code);
+               curl_easy_cleanup(curl);
+               fclose(fp);
+
+               if (error_code != CURLE_OK) {
+                       remove(download_path);
+                       ret = -1;
+               }
+       } else {
+               ret = -1;
+       }
+
+       FWR_EXIT
+       return ret;
 }
index 352e51e70f1d4c52bceaaeb82770201be495fc56..f4fe2ffbd760e744164fa5fb58a945482660a391 100644 (file)
@@ -1,16 +1,53 @@
 #include <app_common.h>
 #include <fmwup_resource_internal.h>
 
-
 extern fmwup_s* g_firmware_resource;
 extern char *g_download_path;
 extern state_changed_cb g_state_changed_cb;
 
 #define UPDATE_FILE "/opt/usr/data/fota/tota_init"
+#define FIRMWARE_FILE_NAME "fmwup.zip"
+
+#ifdef _USE_WWST_
+#define REST_API_DOWNLOAD_URL "/api/v1/firmwares/downloadUrl/"
+#define CURRENT_VERSION "?currentVersion="
+#define TOKEN "&token="
+#define TOKEN_KEY_TEMP "ZjExNGM1MGUtMWE5YS00NDJiLWE2ZGItNmUzZGM0Y2I4MGE4LnppcA=="
+#endif
 
 char *g_app_data_path = NULL;
 
 
+bool _parse_secure_url(char *json, char **url_value)
+{
+       if (!json && *json != '{' && *(json+1) != '\"') {
+
+               return false;
+       }
+
+       char *ret_str;
+       ret_str = strtok(json,"{\"");
+
+       if (ret_str == NULL && g_strcmp0(ret_str, "url") != 0) {
+               FWR_LOGE("Invalid json [url]");
+               return false;
+       }
+
+       ret_str = strtok(NULL,"\"");
+       if (ret_str == NULL && g_strcmp0(ret_str, ":") != 0) {
+               FWR_LOGE("Invalid delimeter [:]");
+               return false;
+       }
+
+       ret_str = strtok(NULL,"\"");
+       if (ret_str == NULL)
+               return false;
+
+       *url_value = g_strdup(ret_str);
+
+       return true;
+}
+
 bool _compare_resource_interface(char *from, char *iface)
 {
        char *str = g_strdup(from);
@@ -71,6 +108,8 @@ void fmwup_propagate_firmware_resource(void)
 
 void _handle_update_command(int64_t update_type)
 {
+       int result = 0;
+
        if (g_strcmp0(g_firmware_resource->package_uri, "") && (
                        (g_firmware_resource->state == 0 && update_type == FMWUP_EXEC_DOWNLOAD) ||
                        (g_firmware_resource->state == 0 && update_type == FMWUP_EXEC_DOWNLOAD_AND_UPGRADE))) {
@@ -84,6 +123,52 @@ void _handle_update_command(int64_t update_type)
                                g_state_changed_cb(g_firmware_resource->state);
                        }
 
+#ifdef _USE_WWST_
+                       //int fmwup_http_send_request(fmwup_http_req_e type, char *req_url, char **res_header, char **res_body)
+                       char *res_header = NULL;
+                       char *res_body = NULL;
+                       char *request_uri = g_strconcat(g_firmware_resource->package_uri, REST_API_DOWNLOAD_URL, CURRENT_VERSION,
+                                       g_firmware_resource->current_version, TOKEN, TOKEN_KEY_TEMP, NULL);
+
+                       FWR_LOGI("request_uri[%s]", request_uri);
+
+                       if (fmwup_http_send_request(FMWUP_HTTP_GET, request_uri, &res_header, &res_body) != 0) {
+                               FWR_LOGE("cannot get Secure URL");
+                       } else {
+                               //parsing
+                               FWR_LOGI("res_header[%s]   res_body[%s]",res_header, res_body);
+
+                               char *sec_download_url = NULL;
+                               if (_parse_secure_url(res_body, &sec_download_url)) {
+
+                                       char *download_path = g_strconcat(g_download_path, FIRMWARE_FILE_NAME, NULL);
+                                       FWR_LOGI("download url[%s]  download path[%s]",sec_download_url, download_path);
+
+                                       if (fmwup_http_download_file(sec_download_url, (const char *)download_path) != 0) {
+                                               g_firmware_resource->state = FMWUP_STATE_IDLE;
+                                               vconf_set_int(VCONF_FIRMWARE_UPDATE_STATE, g_firmware_resource->state);
+                                               g_firmware_resource->result = 6;
+                                               vconf_set_int(VCONF_FIRMWARE_UPDATE_RESULT, g_firmware_resource->result);
+
+                                               fmwup_propagate_firmware_resource();
+                                               if (g_state_changed_cb) {
+                                                       g_state_changed_cb(g_firmware_resource->state);
+                                               }
+
+                                               g_free(download_path);
+                                               return;
+                                       }
+
+                                       g_free(download_path);
+                                       g_free(sec_download_url);
+                               } else {
+                                       FWR_LOGE("_parse_secure_url failed");
+                               }
+                       }
+
+                       g_free(request_uri);
+
+#else
                        if (fmwup_http_download_file(g_firmware_resource->package_uri, (const char *)g_download_path) != 0) {
                                g_firmware_resource->state = FMWUP_STATE_IDLE;
                                vconf_set_int(VCONF_FIRMWARE_UPDATE_STATE, g_firmware_resource->state);
@@ -93,6 +178,7 @@ void _handle_update_command(int64_t update_type)
                                }
                                return;
                        }
+#endif
 
                        FWR_LOGD("*** Firmware image downloaded ***");
                        g_firmware_resource->state = FMWUP_STATE_DOWNLOADED;
@@ -182,17 +268,20 @@ void _update_firmware_resource(OCRepPayload *input)
                }
                g_free(new_firmware);
                FWR_LOGD("g_firmware_resource->new_version = [%s]", g_firmware_resource->new_version);
-       }
 
-       char *package_uri = NULL;
-       if (OCRepPayloadGetPropString(input, OC_RSRVD_FIRMWARE_PACKAGE_URI, &package_uri)) {
-               if (g_strcmp0(package_uri, g_firmware_resource->package_uri)) {
-                       g_free(g_firmware_resource->package_uri);
-                       g_firmware_resource->package_uri = g_strdup(new_firmware);
-                       vconf_set_str(VCONF_FIRMWARE_UPDATE_PACKAGE_URL, new_firmware);
+               char *package_uri = NULL;
+               if (OCRepPayloadGetPropString(input, OC_RSRVD_FIRMWARE_PACKAGE_URI, &package_uri)) {
+                       FWR_LOGD("g_firmware_resource->package_uri = [%s]", package_uri);
+                       if (g_strcmp0(package_uri, g_firmware_resource->package_uri)) {
+                               g_free(g_firmware_resource->package_uri);
+                               g_firmware_resource->package_uri = g_strdup(package_uri);
+                               vconf_set_str(VCONF_FIRMWARE_UPDATE_PACKAGE_URL, package_uri);
+                       }
+                       g_free(package_uri);
+                       FWR_LOGD("g_firmware_resource->package_uri = [%s]", g_firmware_resource->package_uri);
+               } else {
+                       FWR_LOGE("package uri none");
                }
-               g_free(package_uri);
-               FWR_LOGD("g_firmware_resource->package_uri = [%s]", g_firmware_resource->package_uri);
        }
 
        int64_t package_size;
@@ -209,7 +298,7 @@ void _update_firmware_resource(OCRepPayload *input)
                        g_firmware_resource->package_md5 = g_strdup(package_md5);
                        vconf_set_str(VCONF_FIRMWARE_UPDATE_PACKAGE_MD5, package_md5);
                }
-               g_free(package_uri);
+               g_free(package_md5);
                FWR_LOGD("g_firmware_resource->package_md5 = [%s]", g_firmware_resource->package_md5);
        }
 
index 9223853db0288b7dba410654fe52fe18c0798d7b..24c4b06ef7d3ca1255d8a5d56dbea5b558164e73 100644 (file)
@@ -18,7 +18,7 @@ INCLUDE_DIRECTORIES(${test_pkgs_INCLUDE_DIRS})
 LINK_DIRECTORIES(${test_pkgs_LIBRARY_DIRS})
 
 FIND_LIBRARY(RD_LIB NAMES resource_directory PATHS /usr/lib)
-SET(LIBNAME fmwup_resource)
+SET(LIBNAME fmwup-resource)
 ADD_EXECUTABLE(${testapp} ${FMWUP_TEST_SRCS})
 TARGET_LINK_LIBRARIES(${testapp} ${test_pkgs_LIBRARIES} ${LIBNAME} ${RD_LIB} -lpthread)