diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index 7c2a2ae..95ebcbd 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -29,4 +29,12 @@ endif () if (ENABLE_FEATURE_AUTH_MODE_DYNAMIC) add_executable(dynamic_auth_sample dynamic_auth/dynamic_auth_sample.c) add_dependencies(dynamic_auth_sample iot_sdk iot_platform) +endif () + +if (ENABLE_FEATURE_UPLOAD_FILE) + add_executable(http_download http/http_download.c) + add_dependencies(http_download iot_sdk iot_platform) + + add_executable(http_upload http/http_upload.c) + add_dependencies(http_upload iot_sdk iot_platform) endif () \ No newline at end of file diff --git a/samples/http/http_download.c b/samples/http/http_download.c new file mode 100644 index 0000000..5882512 --- /dev/null +++ b/samples/http/http_download.c @@ -0,0 +1,94 @@ +/* +* Copyright (C) 2012-2019 UCloud. All Rights Reserved. +* +* Licensed under the Apache License, Version 2.0 (the "License"). +* You may not use this file except in compliance with the License. +* A copy of the License is located at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* or in the "license" file accompanying this file. This file 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 +#include +#include +#include +#include +#include +#include + +#include "uiot_import.h" +#include "ca.h" +#include "utils_httpc.h" +#include "uiot_export_file_upload.h" + +int main(int argc, char **argv) { + http_client_t *http_client = (http_client_t *)HAL_Malloc(sizeof(http_client_t)); + http_client_data_t *http_data = (http_client_data_t *)HAL_Malloc(sizeof(http_client_data_t)); + memset(http_client, 0, sizeof(http_client_t)); + memset(http_data, 0, sizeof(http_client_data_t)); + + char *buf = (char *)HAL_Malloc(2046); + memset(buf, 0, 2046); + http_data->response_buf = buf; + http_data->response_buf_len = 2046; + http_client->header = (char *)"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" \ + "Accept-Encoding: gzip, deflate\r\n"; + + FILE *fp; + char *file_path = (char*)"download.txt"; + fp = fopen(file_path, "wb+"); + if(NULL == fp) + { + HAL_Printf("open file fp fail\r\n"); + return FAILURE_RET; + } + + char *url = (char *)"https://uiot.cn-sh2.ufileos.com/test.txt"; + + const char *ca_crt = iot_https_ca_get(); + uint32_t timeout_ms = 5000; + int rc; + int32_t total = 0; +#ifdef SUPPORT_TLS + http_client_common(http_client, url, 443, ca_crt, HTTP_GET, http_data); +#else + http_client_common(http_client, url, 80, ca_crt, HTTP_GET, http_data); +#endif + do { + int diff = http_data->response_content_len - http_data->retrieve_len; + + http_client_recv_data(http_client, timeout_ms, http_data); + + int32_t len = http_data->response_content_len - http_data->retrieve_len - diff; + if (len > 0) { + rc = fwrite(http_data->response_buf, len, 1, fp); + if(rc != 1) + { + HAL_Printf("fwrite fail\r\n"); + return FAILURE_RET; + } + total += len; + } + } while (http_data->retrieve_len != 0); + + fclose(fp); + http_client_close(http_client); + HAL_Free(buf); + HAL_Free(http_data); + HAL_Free(http_client); + + if(total != 11358) + { + HAL_Printf("download fail\r\n"); + return FAILURE_RET; + } + + return SUCCESS_RET; +} + + diff --git a/samples/http/http_upload.c b/samples/http/http_upload.c new file mode 100644 index 0000000..ffd7ff2 --- /dev/null +++ b/samples/http/http_upload.c @@ -0,0 +1,70 @@ +/* +* Copyright (C) 2012-2019 UCloud. All Rights Reserved. +* +* Licensed under the Apache License, Version 2.0 (the "License"). +* You may not use this file except in compliance with the License. +* A copy of the License is located at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* or in the "license" file accompanying this file. This file 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 +#include +#include +#include +#include +#include +#include + +#include "uiot_import.h" +#include "ca.h" +#include "utils_httpc.h" +#include "uiot_export_file_upload.h" + +#define UIOT_MY_PRODUCT_SN "iwfrdgwhmwscqbmv" + +#define UIOT_MY_DEVICE_SN "mosjgqhqqx1aut0a" + +#define UIOT_MY_DEVICE_SECRET "zn9srzorb96kwat7" + +#define FILE_PATH "test.zip" + +int main(int argc, char **argv) { + char md5[100]; + char *authorization = (char *)malloc(1024); + memset(authorization, 0, 1024); + char *put_url = (char *)malloc(1024); + memset(put_url, 0, 1024); + int ret = SUCCESS_RET; + + http_client_file_md5(FILE_PATH, md5); + HAL_Printf("MD5:%s\n", md5); + + ret = IOT_GET_URL_AND_AUTH(UIOT_MY_PRODUCT_SN, UIOT_MY_DEVICE_SN, UIOT_MY_DEVICE_SECRET, FILE_PATH, md5, authorization, put_url); + if(SUCCESS_RET != ret) + { + HAL_Printf("get url and auth fail\r\n"); + return FAILURE_RET; + } + + HAL_Printf("get MD5:%s\n", md5); + HAL_Printf("get authorization:%s\n", authorization); + HAL_Printf("get put_url:%s\n", put_url); + + ret = IOT_UPLOAD_FILE(FILE_PATH, md5, authorization, put_url); + if(SUCCESS_RET != ret) + { + HAL_Printf("upload file fail\r\n"); + return FAILURE_RET; + } + HAL_Printf("upload success\n"); + HAL_Free(authorization); + HAL_Free(put_url); +} + + diff --git a/samples/samples.mk b/samples/samples.mk index 7bf7d0b..601e96c 100644 --- a/samples/samples.mk +++ b/samples/samples.mk @@ -12,6 +12,7 @@ mqtt_sample: $(PLATFORM_CC) $(CFLAGS) $(SAMPLE_DIR)/mqtt/$@.c $(LDFLAGS) -o $@ mv $@ $(FINAL_DIR)/bin +endif ifneq (,$(filter -DAUTH_MODE_DYNAMIC,$(CFLAGS))) dynamic_auth_sample: @@ -62,8 +63,24 @@ dev_model_sample: $(TOP_Q) \ mv $@ $(FINAL_DIR)/bin endif + +ifneq (,$(filter -DFILE_UPLOAD_ENABLED,$(CFLAGS))) +http_download: + $(TOP_Q) \ + $(PLATFORM_CC) $(CFLAGS) $(SAMPLE_DIR)/http/$@.c $(LDFLAGS) -o $@ + + $(TOP_Q) \ + mv $@ $(FINAL_DIR)/bin + +http_upload: + $(TOP_Q) \ + $(PLATFORM_CC) $(CFLAGS) $(SAMPLE_DIR)/http/$@.c $(LDFLAGS) -o $@ + + $(TOP_Q) \ + mv $@ $(FINAL_DIR)/bin endif + samples_final: $(TOP_Q) \ cp -rf $(TOP_DIR)/src/sdk-impl/*port*.h $(FINAL_DIR)/include/