From 2716bcebb41470962c5fa4524d3e06d4529ec89b Mon Sep 17 00:00:00 2001 From: "ethan.du" Date: Tue, 25 Feb 2020 14:14:15 +0800 Subject: [PATCH] support http client --- README.md | 11 +- SConscript | 29 ++- docs/quick_start.md | 3 +- samples/dev_model/dev_model_sample.c | 4 +- samples/http/http_client_sample.c | 121 ++++++++++++ samples/http/upload_file_sample.c | 130 +++++++++++++ samples/ota/ota_sample.c | 1 + samples/shadow/shadow_sample.c | 130 ++++++++----- uiot/http/http_client.c | 237 ++++++++++++++++++++++++ uiot/http/http_upload_file.c | 231 +++++++++++++++++++++++ uiot/sdk-impl/uiot_export_http.h | 35 ++++ uiot/shadow/src/shadow_client_manager.c | 2 + uiot/utils/utils_httpc.c | 44 ++++- uiot/utils/utils_httpc.h | 5 + uiot/utils/utils_net.c | 80 -------- 15 files changed, 918 insertions(+), 145 deletions(-) create mode 100644 samples/http/http_client_sample.c create mode 100644 samples/http/upload_file_sample.c create mode 100644 uiot/http/http_client.c create mode 100644 uiot/http/http_upload_file.c create mode 100644 uiot/sdk-impl/uiot_export_http.h diff --git a/README.md b/README.md index e83fe70..7b6fbd7 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ UCloud IOT SDK for rt-thread Package 是基于[UCloud设备端C-SDK](https://git | samples | 示例目录 | | ├─mqtt | 静态注册收发消息示例 | | ├─dynamic_auth | 动态注册示例 | +| ├─http | Http连接云平台示例 | | ├─dev_model | 物模型示例 | | ├─ota | ota升级示例 | | ├─shadow | 设备影子示例 | @@ -43,7 +44,8 @@ RT-Thread online packages ---> IoT Cloud ---> [ ] ucloud-iot-sdk: ucloud iot sdk for uiot-core platform. ---> --- ucloud-iothub: ucloud iot sdk for uiot-core platform - [ ] Enable Mqtt + [ ] Enable Mqtt link uiot-core platform + [ ] Enable Http link uiot-core platform ucloud Device Config ---> Version (latest) ---> ``` @@ -52,7 +54,8 @@ RT-Thread online packages ---> 根据产品需求选择合适的应用示例修改新增业务逻辑,也可新增例程编写新的业务逻辑。 ``` --- ucloud-iot-sdk: ucloud iot sdk for uiot-core platform. - [*] Enable Mqtt + [*] Enable Mqtt link uiot-core platform + [ ] Enable Http link uiot-core platform Ucloud Device Config ---> [*] Enable Ucloud Mqtt Sample [ ] Enable Ucloud Mqtt Dynamic Auth Sample @@ -69,7 +72,9 @@ RT-Thread online packages ---> - 选项说明 -`Enable Mqtt`:使能MQTT连接云平台功能。 +`Enable Mqtt link uiot-core platform`:使能MQTT连接云平台功能。 + +`Enable Http link uiot-core platform`:使能HTTP连接云平台功能,只支持https,勾选会关联选中mbedTLS软件包。 `Ucloud Device Config `:填写当前设备认证要素,当认证模式为动态认证时,设备密钥可以不填写 diff --git a/SConscript b/SConscript index 690cb41..acef0ae 100644 --- a/SConscript +++ b/SConscript @@ -8,6 +8,8 @@ src_base = [] sample_ucloud_mqtt_src = [] sample_ucloud_mqtt_dynamic_auth_src = [] +sample_ucloud_http_publish_src = [] +sample_ucloud_http_upload_file_src = [] sample_ucloud_shadow_src = [] sample_ucloud_dev_model_src = [] sample_ucloud_ota_src = [] @@ -29,6 +31,8 @@ CPPPATH += [cwd + '/uiot/sdk-impl'] CPPPATH += [cwd + '/uiot/shadow/include'] CPPPATH += [cwd + '/uiot/utils'] +src_base += Glob('uiot/utils/*.c') +src_base += Glob('ports/rtthread/*.c') #Debug if GetDepend(['PKG_USING_UCLOUD_DEBUG']): CPPDEFINES += ['ENABLE_LOG_DEBUG', 'ENABLE_LOG_INFO', 'ENABLE_LOG_WARN', 'ENABLE_LOG_ERROR'] @@ -36,13 +40,10 @@ if GetDepend(['PKG_USING_UCLOUD_DEBUG']): #Gen MQTT src file if GetDepend(['PKG_USING_UCLOUD_MQTT']): src_base += Glob('uiot/mqtt/src/*.c') - src_base += Glob('uiot/utils/utils_timer.c') - src_base += Glob('uiot/utils/utils_net.c') - src_base += Glob('uiot/utils/utils_list.c') - src_base += Glob('uiot/utils/json_parser.c') - src_base += Glob('uiot/utils/json_token.c') - src_base += Glob('uiot/utils/string_utils.c') - src_base += Glob('ports/rtthread/*.c') + +#Gen HTTP src file +if GetDepend(['PKG_USING_UCLOUD_HTTP']): + src_base += Glob('uiot/http/*.c') #Gen shadow src file if GetDepend(['PKG_USING_UCLOUD_SHADOW']): @@ -56,8 +57,6 @@ if GetDepend(['PKG_USING_UCLOUD_DEV_MODEL']): if GetDepend(['PKG_USING_UCLOUD_OTA']): src_base += Glob('uiot/ota/src/*.c') src_base += Glob('ports/fal/*.c') - src_base += Glob('uiot/utils/utils_md5.c') - src_base += Glob('uiot/utils/utils_httpc.c') #TLS used if GetDepend(['PKG_USING_UCLOUD_TLS']): @@ -80,6 +79,18 @@ if GetDepend(['PKG_USING_UCLOUD_MQTT_DYNAMIC_AUTH_SAMPLE']): group = DefineGroup('sample_ucloud_mqtt_dynamic_auth', sample_ucloud_mqtt_dynamic_auth_src, depend = ['PKG_USING_UCLOUD_MQTT_DYNAMIC_AUTH_SAMPLE'], CPPPATH = CPPPATH, LOCAL_CCFLAGS = LOCAL_CCFLAGS, CPPDEFINES = CPPDEFINES) +#Http Publish Example +if GetDepend(['PKG_USING_UCLOUD_HTTP_PUBLISH_SAMPLE']): + sample_ucloud_http_publish_src += Glob('samples/http/http_client_sample.c') + +group = DefineGroup('sample_ucloud_http_publish', sample_ucloud_http_publish_src, depend = ['PKG_USING_UCLOUD_HTTP_PUBLISH_SAMPLE'], CPPPATH = CPPPATH, LOCAL_CCFLAGS = LOCAL_CCFLAGS, CPPDEFINES = CPPDEFINES) + +#Http Upload File Example +if GetDepend(['PKG_USING_UCLOUD_HTTP_UPLOAD_FILE_SAMPLE']): + sample_ucloud_http_upload_file_src += Glob('samples/http/upload_file_sample.c') + +group = DefineGroup('sample_ucloud_http_upload_file', sample_ucloud_http_upload_file_src, depend = ['PKG_USING_UCLOUD_HTTP_UPLOAD_FILE_SAMPLE'], CPPPATH = CPPPATH, LOCAL_CCFLAGS = LOCAL_CCFLAGS, CPPDEFINES = CPPDEFINES) + #Shadow Example if GetDepend(['PKG_USING_UCLOUD_SHADOW_SAMPLE']): sample_ucloud_shadow_src += Glob('samples/shadow/shadow_sample.c') diff --git a/docs/quick_start.md b/docs/quick_start.md index 22ca5ab..5e46bf0 100644 --- a/docs/quick_start.md +++ b/docs/quick_start.md @@ -42,7 +42,8 @@ 1. 打开mqtt sample ``` --- ucloud-iot-sdk: ucloud iot sdk for uiot-core platform. - [*] Enable Mqtt + [*] Enable Mqtt link uiot-core platform + [ ] Enable Http link uiot-core platform Ucloud Device Config ---> [*] Enable Ucloud Mqtt Sample [ ] Enable Ucloud Mqtt Dynamic Auth Sample diff --git a/samples/dev_model/dev_model_sample.c b/samples/dev_model/dev_model_sample.c index 1ac2414..3985733 100644 --- a/samples/dev_model/dev_model_sample.c +++ b/samples/dev_model/dev_model_sample.c @@ -125,7 +125,8 @@ static void mqtt_devmodel_thread(void) IOT_MQTT_Yield(client, 50); void *h_dm = IOT_DM_Init(PKG_USING_UCLOUD_IOT_SDK_PRODUCT_SN, PKG_USING_UCLOUD_IOT_SDK_DEVICE_SN, client); - if (NULL == h_dm) { + if (NULL == h_dm) { + IOT_MQTT_Destroy(&client); LOG_ERROR("initialize device model failed"); return; } @@ -135,6 +136,7 @@ static void mqtt_devmodel_thread(void) IOT_DM_RegisterCallback(COMMAND , h_dm, command_cb); IOT_DM_RegisterCallback(PROPERTY_POST , h_dm, property_post_cb); IOT_DM_RegisterCallback(PROPERTY_SET , h_dm, property_set_cb); + IOT_DM_Yield(h_dm, 200); for (int i = 0; i < 10; i++) { IOT_DM_Property_Report(h_dm, PROPERTY_POST, i * 2, "{\"volume\": {\"Value\":50}}"); diff --git a/samples/http/http_client_sample.c b/samples/http/http_client_sample.c new file mode 100644 index 0000000..0b8ce5a --- /dev/null +++ b/samples/http/http_client_sample.c @@ -0,0 +1,121 @@ +/* +* 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_http.h" + +#define UIOT_PUBLISH_TOPIC "%s/%s/upload/event" + +static int running_state = 0; + +static void http_publish_test_thread(void) +{ + char *token = (char *)HAL_Malloc(1024); + memset(token, 0, 1024); + int ret = SUCCESS_RET; + char *topic = (char *)HAL_Malloc(256); + memset(topic, 0, 256); + HAL_Snprintf((char *)topic, 256, UIOT_PUBLISH_TOPIC,PKG_USING_UCLOUD_IOT_SDK_PRODUCT_SN, PKG_USING_UCLOUD_IOT_SDK_DEVICE_SN); + char *data = "{\"test\": \"18\"}"; + + ret = IOT_HTTP_Get_Token(PKG_USING_UCLOUD_IOT_SDK_PRODUCT_SN, PKG_USING_UCLOUD_IOT_SDK_DEVICE_SN, PKG_USING_UCLOUD_IOT_SDK_DEVICE_SECRET, token); + if(SUCCESS_RET != ret) + { + HAL_Printf("get Token fail,ret:%d\r\n", ret); + return; + } + + HAL_Printf("get token:%s\n", token); + HAL_Printf("topic:%s\n", topic); + ret = IOT_HTTP_Publish(token, topic, data, 5000); + if(SUCCESS_RET != ret) + { + HAL_Printf("Publish fail,ret:%d\r\n", ret); + return; + } + HAL_Printf("Publish success\n"); + HAL_Free(token); + HAL_Free(topic); + return; +} + +static int http_publish_test_example(int argc, char **argv) +{ + rt_thread_t tid; + int stack_size = 8192; + + if (2 == argc) + { + if (!strcmp("start", argv[1])) + { + if (1 == running_state) + { + HAL_Printf("http_publish_test_example is already running\n"); + return 0; + } + } + else if (!strcmp("stop", argv[1])) + { + if (0 == running_state) + { + HAL_Printf("http_publish_test_example is already stopped\n"); + return 0; + } + running_state = 0; + return 0; + } + else + { + HAL_Printf("Usage: http_publish_test_example start/stop"); + return 0; + } + } + else + { + HAL_Printf("Para err, usage: http_publish_test_example start/stop"); + return 0; + } + + tid = rt_thread_create("http_publish_test", (void (*)(void *))http_publish_test_thread, + NULL, stack_size, RT_THREAD_PRIORITY_MAX / 2 - 1, 100); + + if (tid != RT_NULL) + { + rt_thread_startup(tid); + } + + return 0; +} + + +#ifdef RT_USING_FINSH +#include +FINSH_FUNCTION_EXPORT(http_publish_test_example, startup http publish example); +#endif + +#ifdef FINSH_USING_MSH +MSH_CMD_EXPORT(http_publish_test_example, startup http publish example); +#endif + diff --git a/samples/http/upload_file_sample.c b/samples/http/upload_file_sample.c new file mode 100644 index 0000000..4c6db0d --- /dev/null +++ b/samples/http/upload_file_sample.c @@ -0,0 +1,130 @@ +/* +* 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_http.h" + +//upload file name saved in ufile +#define FILE_NAME "upload_file.txt" +//data saved in file that you upload +#define UPLOAD_BUFFER "This is a test file for upload test!" + +static int running_state = 0; + +static void http_upload_file_test_thread(void) +{ + 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_buffer_md5(UPLOAD_BUFFER, sizeof(UPLOAD_BUFFER), md5); + HAL_Printf("MD5:%s\n", md5); + + ret = IOT_GET_URL_AND_AUTH(PKG_USING_UCLOUD_IOT_SDK_PRODUCT_SN, PKG_USING_UCLOUD_IOT_SDK_DEVICE_SN, PKG_USING_UCLOUD_IOT_SDK_DEVICE_SECRET, \ + FILE_NAME, UPLOAD_BUFFER, sizeof(UPLOAD_BUFFER), md5, authorization, put_url); + if(SUCCESS_RET != ret) + { + HAL_Printf("get url and auth fail,ret:%d\r\n", ret); + return; + } + + HAL_Printf("get authorization:%s\n", authorization); + HAL_Printf("get put_url:%s\n", put_url); + + //上传文件超时时间与文件长度相关 + ret = IOT_HTTP_UPLOAD_FILE(FILE_NAME, UPLOAD_BUFFER, sizeof(UPLOAD_BUFFER), md5, authorization, put_url, 10000); + if(SUCCESS_RET != ret) + { + HAL_Printf("upload file fail,ret:%d\r\n", ret); + return; + } + HAL_Printf("upload success\n"); + HAL_Free(authorization); + HAL_Free(put_url); + return; +} + +static int http_upload_file_test_example(int argc, char **argv) +{ + rt_thread_t tid; + int stack_size = 8192; + + if (2 == argc) + { + if (!strcmp("start", argv[1])) + { + if (1 == running_state) + { + HAL_Printf("http_publish_test_example is already running\n"); + return 0; + } + } + else if (!strcmp("stop", argv[1])) + { + if (0 == running_state) + { + HAL_Printf("http_publish_test_example is already stopped\n"); + return 0; + } + running_state = 0; + return 0; + } + else + { + HAL_Printf("Usage: http_publish_test_example start/stop"); + return 0; + } + } + else + { + HAL_Printf("Para err, usage: http_publish_test_example start/stop"); + return 0; + } + + tid = rt_thread_create("http_upload_file_test", (void (*)(void *))http_upload_file_test_thread, + NULL, stack_size, RT_THREAD_PRIORITY_MAX / 2 - 1, 100); + + if (tid != RT_NULL) + { + rt_thread_startup(tid); + } + + return 0; +} + + +#ifdef RT_USING_FINSH +#include +FINSH_FUNCTION_EXPORT(http_upload_file_test_example, startup http upload_file example); +#endif + +#ifdef FINSH_USING_MSH +MSH_CMD_EXPORT(http_upload_file_test_example, startup http upload_file example); +#endif + + diff --git a/samples/ota/ota_sample.c b/samples/ota/ota_sample.c index 8f7f7f0..cf7a487 100644 --- a/samples/ota/ota_sample.c +++ b/samples/ota/ota_sample.c @@ -104,6 +104,7 @@ static void ota_test_thread(void) void *h_ota = IOT_OTA_Init(PKG_USING_UCLOUD_IOT_SDK_PRODUCT_SN, PKG_USING_UCLOUD_IOT_SDK_DEVICE_SN, client); if (NULL == h_ota) { + IOT_MQTT_Destroy(&client); LOG_ERROR("init OTA failed"); return; } diff --git a/samples/shadow/shadow_sample.c b/samples/shadow/shadow_sample.c index 0e29c07..4e30bd5 100644 --- a/samples/shadow/shadow_sample.c +++ b/samples/shadow/shadow_sample.c @@ -24,14 +24,9 @@ #include "uiot_import.h" #include "shadow_client.h" -#define MAX_SIZE_OF_TOPIC_CONTENT 100 - #define SIZE_OF_JSON_BUFFER 256 static int running_state = 0; -static UIoT_Shadow *sg_pshadow; -static MQTTInitParams sg_initParams = DEFAULT_MQTT_INIT_PARAMS; - //当设备直接按照desired字段中的属性值更新时不需要上报 void RegCallback_update(void *pClient, RequestParams *pParams, char *pJsonValueBuffer, uint32_t valueLength, DeviceProperty *pProperty) { @@ -88,33 +83,37 @@ static int _setup_connect_init_params(MQTTInitParams* initParams) static void shadow_test_thread(void) { int ret = SUCCESS_RET; - ret = _setup_connect_init_params(&sg_initParams); + MQTTInitParams initParams = DEFAULT_MQTT_INIT_PARAMS; + ret = _setup_connect_init_params(&initParams); if(ret != SUCCESS_RET) { HAL_Printf("_setup_connect_init_params fail:%d\n", ret); return; } - void *mqtt_client = IOT_MQTT_Construct(&sg_initParams); + void *mqtt_client = IOT_MQTT_Construct(&initParams); if(mqtt_client == NULL) { HAL_Printf("IOT_MQTT_Construct fail\n"); return; } - - void *shadow_client = IOT_Shadow_Construct(PKG_USING_UCLOUD_IOT_SDK_PRODUCT_SN, PKG_USING_UCLOUD_IOT_SDK_DEVICE_SN, mqtt_client); + + UIoT_Shadow *shadow_client = (UIoT_Shadow *)IOT_Shadow_Construct(PKG_USING_UCLOUD_IOT_SDK_PRODUCT_SN, PKG_USING_UCLOUD_IOT_SDK_DEVICE_SN, mqtt_client); if(shadow_client == NULL) { - HAL_Printf("IOT_Shadow_Construct fail\n"); - return; - } - sg_pshadow = (UIoT_Shadow *)shadow_client; - bool isConnected = IOT_MQTT_IsConnected(sg_pshadow->mqtt); + HAL_Printf("IOT_Shadow_Construct fail\n"); + IOT_MQTT_Destroy(&mqtt_client); + } + + IOT_Shadow_Yield(shadow_client, 200); + + bool isConnected = IOT_MQTT_IsConnected(shadow_client->mqtt); if(isConnected != true) { HAL_Printf("IOT_MQTT_IsConnected fail\n"); - return; + IOT_MQTT_Destroy(&mqtt_client); + return; } int time_sec = MAX_WAIT_TIME_SEC; @@ -126,10 +125,13 @@ static void shadow_test_thread(void) Property1->key= str1; Property1->data = &num1; Property1->type = JINT32; - ret = IOT_Shadow_Register_Property(sg_pshadow, Property1, RegCallback_hold); + ret = IOT_Shadow_Register_Property(shadow_client, Property1, RegCallback_hold); if(SUCCESS_RET != ret) { HAL_Printf("Register Property1 fail:%d\n", ret); + HAL_Free(Property1); + IOT_Shadow_Destroy(shadow_client); + IOT_MQTT_Destroy(&mqtt_client); return; } @@ -139,10 +141,14 @@ static void shadow_test_thread(void) Property2->key= str2; Property2->data = &num2; Property2->type = JFLOAT; - ret = IOT_Shadow_Register_Property(sg_pshadow, Property2, RegCallback_update); + ret = IOT_Shadow_Register_Property(shadow_client, Property2, RegCallback_update); if(SUCCESS_RET != ret) { HAL_Printf("Register Property2 fail:%d\n", ret); + HAL_Free(Property1); + HAL_Free(Property2); + IOT_Shadow_Destroy(shadow_client); + IOT_MQTT_Destroy(&mqtt_client); return; } @@ -152,10 +158,14 @@ static void shadow_test_thread(void) Property3->key= str3; Property3->data = &num3; Property3->type = JDOUBLE; - ret = IOT_Shadow_Register_Property(sg_pshadow, Property3, RegCallback_update); + ret = IOT_Shadow_Register_Property(shadow_client, Property3, RegCallback_update); if(SUCCESS_RET != ret) { HAL_Printf("Register Property3 fail:%d\n", ret); + HAL_Free(Property1); + HAL_Free(Property2); + IOT_Shadow_Destroy(shadow_client); + IOT_MQTT_Destroy(&mqtt_client); return; } @@ -165,10 +175,16 @@ static void shadow_test_thread(void) Property4->key= str4; Property4->data = num4; Property4->type = JSTRING; - ret = IOT_Shadow_Register_Property(sg_pshadow, Property4, RegCallback_update); + ret = IOT_Shadow_Register_Property(shadow_client, Property4, RegCallback_update); if(SUCCESS_RET != ret) { HAL_Printf("Register Property4 fail:%d\n", ret); + HAL_Free(Property1); + HAL_Free(Property2); + HAL_Free(Property3); + HAL_Free(Property4); + IOT_Shadow_Destroy(shadow_client); + IOT_MQTT_Destroy(&mqtt_client); return; } @@ -178,10 +194,17 @@ static void shadow_test_thread(void) Property5->key= str5; Property5->data = &num5; Property5->type = JBOOL; - ret = IOT_Shadow_Register_Property(sg_pshadow, Property5, RegCallback_update); + ret = IOT_Shadow_Register_Property(shadow_client, Property5, RegCallback_update); if(SUCCESS_RET != ret) { HAL_Printf("Register Property5 fail:%d\n", ret); + HAL_Free(Property1); + HAL_Free(Property2); + HAL_Free(Property3); + HAL_Free(Property4); + HAL_Free(Property5); + IOT_Shadow_Destroy(shadow_client); + IOT_MQTT_Destroy(&mqtt_client); return; } @@ -191,43 +214,51 @@ static void shadow_test_thread(void) Property6->key= str6; Property6->data = num6; Property6->type = JOBJECT; - ret = IOT_Shadow_Register_Property(sg_pshadow, Property6, RegCallback_update); + ret = IOT_Shadow_Register_Property(shadow_client, Property6, RegCallback_update); if(SUCCESS_RET != ret) { HAL_Printf("Register Property6 fail:%d\n", ret); + HAL_Free(Property1); + HAL_Free(Property2); + HAL_Free(Property3); + HAL_Free(Property4); + HAL_Free(Property5); + HAL_Free(Property6); + IOT_Shadow_Destroy(shadow_client); + IOT_MQTT_Destroy(&mqtt_client); return; } /* 先同步一下版本号和设备掉电期间更新的属性 */ - ret = IOT_Shadow_Get_Sync(sg_pshadow, _update_ack_cb, time_sec, &ack_update); + ret = IOT_Shadow_Get_Sync(shadow_client, _update_ack_cb, time_sec, &ack_update); if(SUCCESS_RET != ret) { HAL_Printf("Get Sync fail:%d\n", ret); - return; + goto end; } while (ACK_NONE == ack_update) { - IOT_Shadow_Yield(sg_pshadow, MAX_WAIT_TIME_MS); + IOT_Shadow_Yield(shadow_client, MAX_WAIT_TIME_MS); } /* update */ ack_update = ACK_NONE; - ret = IOT_Shadow_Update(sg_pshadow, _update_ack_cb, time_sec, &ack_update, 6, Property1, Property2, Property3, Property4, Property5, Property6); + ret = IOT_Shadow_Update(shadow_client, _update_ack_cb, time_sec, &ack_update, 6, Property1, Property2, Property3, Property4, Property5, Property6); if(SUCCESS_RET != ret) { HAL_Printf("Update Property1 Property2 Property3 Property4 Property5 Property6 fail:%d\n", ret); - return; + goto end; } while (ACK_NONE == ack_update) { - IOT_Shadow_Yield(sg_pshadow, MAX_WAIT_TIME_MS); + IOT_Shadow_Yield(shadow_client, MAX_WAIT_TIME_MS); } ack_update = ACK_NONE; - ret = IOT_Shadow_Get_Sync(sg_pshadow, _update_ack_cb, time_sec, &ack_update); + ret = IOT_Shadow_Get_Sync(shadow_client, _update_ack_cb, time_sec, &ack_update); while (ACK_NONE == ack_update) { - IOT_Shadow_Yield(sg_pshadow, MAX_WAIT_TIME_MS); + IOT_Shadow_Yield(shadow_client, MAX_WAIT_TIME_MS); } /* update */ @@ -238,58 +269,58 @@ static void shadow_test_thread(void) Property4->data = num9; ack_update = ACK_NONE; - ret = IOT_Shadow_Update(sg_pshadow, _update_ack_cb, time_sec, &ack_update, 2, Property1, Property4); + ret = IOT_Shadow_Update(shadow_client, _update_ack_cb, time_sec, &ack_update, 2, Property1, Property4); if(SUCCESS_RET != ret) { HAL_Printf("Update Property1 Property4 fail:%d\n", ret); - return; + goto end; } while (ACK_NONE == ack_update) { - IOT_Shadow_Yield(sg_pshadow, MAX_WAIT_TIME_MS); + IOT_Shadow_Yield(shadow_client, MAX_WAIT_TIME_MS); } /* delete */ ack_update = ACK_NONE; - ret = IOT_Shadow_Delete(sg_pshadow, _update_ack_cb, time_sec, &ack_update, 2, Property1, Property2); + ret = IOT_Shadow_Delete(shadow_client, _update_ack_cb, time_sec, &ack_update, 2, Property1, Property2); if(SUCCESS_RET != ret) { HAL_Printf("Delete Property1 Property2 fail:%d\n", ret); - return; + goto end; } while (ACK_NONE == ack_update) { - IOT_Shadow_Yield(sg_pshadow, MAX_WAIT_TIME_MS); + IOT_Shadow_Yield(shadow_client, MAX_WAIT_TIME_MS); } ack_update = ACK_NONE; - ret = IOT_Shadow_Get_Sync(sg_pshadow, _update_ack_cb, time_sec, &ack_update); + ret = IOT_Shadow_Get_Sync(shadow_client, _update_ack_cb, time_sec, &ack_update); while (ACK_NONE == ack_update) { - IOT_Shadow_Yield(sg_pshadow, MAX_WAIT_TIME_MS); + IOT_Shadow_Yield(shadow_client, MAX_WAIT_TIME_MS); } /* delete all */ ack_update = ACK_NONE; - ret = IOT_Shadow_Delete_All(sg_pshadow, _update_ack_cb, time_sec, &ack_update); + ret = IOT_Shadow_Delete_All(shadow_client, _update_ack_cb, time_sec, &ack_update); if(SUCCESS_RET != ret) { HAL_Printf("Delete All fail:%d\n", ret); - return; + goto end; } while (ACK_NONE == ack_update) { - IOT_Shadow_Yield(sg_pshadow, MAX_WAIT_TIME_MS); + IOT_Shadow_Yield(shadow_client, MAX_WAIT_TIME_MS); } ack_update = ACK_NONE; - ret = IOT_Shadow_Get_Sync(sg_pshadow, _update_ack_cb, time_sec, &ack_update); + ret = IOT_Shadow_Get_Sync(shadow_client, _update_ack_cb, time_sec, &ack_update); while (ACK_NONE == ack_update) { - IOT_Shadow_Yield(sg_pshadow, MAX_WAIT_TIME_MS); + IOT_Shadow_Yield(shadow_client, MAX_WAIT_TIME_MS); } Property1->data = &num1; @@ -299,31 +330,32 @@ static void shadow_test_thread(void) /* update */ ack_update = ACK_NONE; - ret = IOT_Shadow_Update_And_Reset_Version(sg_pshadow, _update_ack_cb, time_sec, &ack_update, 4, Property1, Property4, Property5, Property6); + ret = IOT_Shadow_Update_And_Reset_Version(shadow_client, _update_ack_cb, time_sec, &ack_update, 4, Property1, Property4, Property5, Property6); if(SUCCESS_RET != ret) { HAL_Printf("Update and Reset Ver fail:%d\n", ret); - return; + goto end; } while (ACK_NONE == ack_update) { - IOT_Shadow_Yield(sg_pshadow, MAX_WAIT_TIME_MS); + IOT_Shadow_Yield(shadow_client, MAX_WAIT_TIME_MS); } ack_update = ACK_NONE; - ret = IOT_Shadow_Get_Sync(sg_pshadow, _update_ack_cb, time_sec, &ack_update); + ret = IOT_Shadow_Get_Sync(shadow_client, _update_ack_cb, time_sec, &ack_update); while (ACK_NONE == ack_update) { - IOT_Shadow_Yield(sg_pshadow, MAX_WAIT_TIME_MS); + IOT_Shadow_Yield(shadow_client, MAX_WAIT_TIME_MS); } - + +end: HAL_Free(Property1); HAL_Free(Property2); HAL_Free(Property3); HAL_Free(Property4); HAL_Free(Property5); HAL_Free(Property6); - IOT_Shadow_Destroy(sg_pshadow); + IOT_Shadow_Destroy(shadow_client); IOT_MQTT_Destroy(&mqtt_client); return; } diff --git a/uiot/http/http_client.c b/uiot/http/http_client.c new file mode 100644 index 0000000..fd3e828 --- /dev/null +++ b/uiot/http/http_client.c @@ -0,0 +1,237 @@ +/* +* 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 "utils_httpc.h" +#include "lite-utils.h" +#include "ca.h" +#include "utils_sha2.h" + +int IOT_HTTP_Get_Token(const char *product_sn, const char *device_sn, const char *device_sercret, char *token) +{ + int ret = SUCCESS_RET; + http_client_t *http_client_post = (http_client_t *)HAL_Malloc(sizeof(http_client_t)); + if(NULL == http_client_post) + { + LOG_ERROR("http_client_post malloc fail\n"); + return FAILURE_RET; + } + http_client_data_t *http_data_post = (http_client_data_t *)HAL_Malloc(sizeof(http_client_data_t)); + if(NULL == http_data_post) + { + HAL_Free(http_client_post); + LOG_ERROR("http_data_post malloc fail\n"); + return FAILURE_RET; + } + + memset(http_client_post, 0, sizeof(http_client_t)); + memset(http_data_post, 0, sizeof(http_client_data_t)); + + http_data_post->response_buf = (char *)HAL_Malloc(1024); + if(NULL == http_data_post->response_buf) + { + HAL_Free(http_client_post); + HAL_Free(http_data_post); + LOG_ERROR("http_data_post->response_buf malloc fail\n"); + return FAILURE_RET; + } + memset(http_data_post->response_buf, 0, 1024); + http_data_post->response_buf_len = 1024; + http_data_post->post_content_type = (char *)"application/json"; + http_data_post->post_buf = (unsigned char *)HAL_Malloc(1024); + if(NULL == http_data_post->post_buf) + { + HAL_Free(http_client_post); + HAL_Free(http_data_post->response_buf); + HAL_Free(http_data_post); + LOG_ERROR("http_data_post->post_buf malloc fail\n"); + return FAILURE_RET; + } + memset(http_data_post->post_buf, 0, 1024); + + HAL_Snprintf((char *)http_data_post->post_buf, 1024, "{\"ProductSN\":\"%s\",\"DeviceSN\":\"%s\"}",product_sn, device_sn); + http_data_post->post_buf_len = strlen((char *)http_data_post->post_buf); + uint8_t mac_output_hex[32] = {0}; + char mac_output_char[65] = {0}; + + utils_hmac_sha256((const uint8_t *)http_data_post->post_buf, http_data_post->post_buf_len, (const uint8_t *)device_sercret, strlen(device_sercret), mac_output_hex); + LITE_hexbuf_convert((unsigned char *)mac_output_hex, mac_output_char, 32, 0); + LOG_DEBUG("hmac:%s\r\n",mac_output_char); + + http_client_post->header = (char *)HAL_Malloc(1024); + if(NULL == http_client_post->header) + { + HAL_Free(http_client_post); + HAL_Free(http_data_post->response_buf); + HAL_Free(http_data_post->post_buf); + HAL_Free(http_data_post); + LOG_ERROR("http_client_post->header malloc fail\n"); + return FAILURE_RET; + } + memset(http_client_post->header, 0, 1024); + HAL_Snprintf(http_client_post->header, 1024, "Content-Type: application/json\r\nAuthorization: %s\r\nbody: %s\r\n",mac_output_char,http_data_post->post_buf); + + const char *ca_crt = iot_https_ca_get(); + char *url = (char *)"https://http-cn-sh2.iot.ucloud.cn/auth"; + + ret = http_client_common(http_client_post, url, 443, ca_crt, HTTP_POST, http_data_post,5000); + if(SUCCESS_RET != ret) + { + LOG_ERROR("HTTP_POST error\n"); + goto end; + } + + ret = http_client_recv_data(http_client_post, 5000, http_data_post); + if(SUCCESS_RET != ret) + { + LOG_ERROR("http_client_recv_data error\n"); + goto end; + } + LOG_DEBUG("response_buf:%s\n",http_data_post->response_buf); + + char *temp = LITE_json_value_of((char *)"Token", http_data_post->response_buf); + if(NULL == temp) + { + LOG_ERROR("parse Token error\n"); + char *temp_message = LITE_json_value_of((char *)"Message", http_data_post->response_buf); + LOG_ERROR("%s\n", temp_message); + HAL_Free(temp_message); + goto end; + } + strncpy(token,temp,strlen(temp)); + token[strlen(temp)+1] = '\0'; + LOG_DEBUG("token:%s\n",token); + HAL_Free(temp); +end: + http_client_close(http_client_post); + HAL_Free(http_client_post->header); + HAL_Free(http_client_post); + HAL_Free(http_data_post->response_buf); + HAL_Free(http_data_post->post_buf); + HAL_Free(http_data_post); + return ret; +} + +int IOT_HTTP_Publish(char *token, char *topic, char *data, uint32_t timeout_ms) +{ + int ret = SUCCESS_RET; + http_client_t *http_client_post = (http_client_t *)HAL_Malloc(sizeof(http_client_t)); + if(NULL == http_client_post) + { + LOG_ERROR("http_client_post malloc fail\n"); + return FAILURE_RET; + } + http_client_data_t *http_data_post = (http_client_data_t *)HAL_Malloc(sizeof(http_client_data_t)); + if(NULL == http_data_post) + { + HAL_Free(http_client_post); + LOG_ERROR("http_data_post malloc fail\n"); + return FAILURE_RET; + } + + memset(http_client_post, 0, sizeof(http_client_t)); + memset(http_data_post, 0, sizeof(http_client_data_t)); + + http_data_post->response_buf = (char *)HAL_Malloc(1024); + if(NULL == http_data_post->response_buf) + { + HAL_Free(http_client_post); + HAL_Free(http_data_post); + LOG_ERROR("http_data_post->response_buf malloc fail\n"); + return FAILURE_RET; + } + memset(http_data_post->response_buf, 0, 1024); + http_data_post->response_buf_len = 1024; + http_data_post->post_content_type = (char *)"application/octet-stream"; + http_data_post->post_buf = (unsigned char *)HAL_Malloc(1024); + if(NULL == http_data_post->post_buf) + { + HAL_Free(http_client_post); + HAL_Free(http_data_post->response_buf); + HAL_Free(http_data_post); + LOG_ERROR("http_data_post->post_buf malloc fail\n"); + return FAILURE_RET; + } + memset(http_data_post->post_buf, 0, 1024); + + HAL_Snprintf((char *)http_data_post->post_buf, 1024, "%s",data); + http_data_post->post_buf_len = strlen((char *)http_data_post->post_buf); + + http_client_post->header = (char *)HAL_Malloc(1024); + if(NULL == http_client_post->header) + { + HAL_Free(http_client_post); + HAL_Free(http_data_post->response_buf); + HAL_Free(http_data_post->post_buf); + HAL_Free(http_data_post); + LOG_ERROR("http_client_post->header malloc fail\n"); + return FAILURE_RET; + } + memset(http_client_post->header, 0, 1024); + HAL_Snprintf(http_client_post->header, 1024, "Password: %s\r\nContent-Type: application/octet-stream\r\nbody: %s\r\n", token, http_data_post->post_buf); + + const char *ca_crt = iot_https_ca_get(); + char *url = (char *)HAL_Malloc(256); + if(NULL == url) + { + goto end; + LOG_ERROR("http_client_post->header malloc fail\n"); + return FAILURE_RET; + } + memset(url, 0, 256); + HAL_Snprintf(url, 256, "https://http-cn-sh2.iot.ucloud.cn/topic/%s",topic); + + + ret = http_client_common(http_client_post, url, 443, ca_crt, HTTP_POST, http_data_post,5000); + if(SUCCESS_RET != ret) + { + LOG_ERROR("HTTP_POST error\n"); + goto end; + } + + HAL_Free(url); + + ret = http_client_recv_data(http_client_post, 5000, http_data_post); + if(SUCCESS_RET != ret) + { + LOG_ERROR("http_client_recv_data error\n"); + goto end; + } + LOG_DEBUG("response_buf:%s\n",http_data_post->response_buf); + + char *temp_message = LITE_json_value_of((char *)"Message", http_data_post->response_buf); + if(NULL == temp_message) + { + LOG_ERROR("parse Message error\n"); + goto end; + } + LOG_DEBUG("%s\n", temp_message); + HAL_Free(temp_message); +end: + http_client_close(http_client_post); + HAL_Free(http_client_post->header); + HAL_Free(http_client_post); + HAL_Free(http_data_post->response_buf); + HAL_Free(http_data_post->post_buf); + HAL_Free(http_data_post); + return ret; +} + + + + diff --git a/uiot/http/http_upload_file.c b/uiot/http/http_upload_file.c new file mode 100644 index 0000000..70cf8b8 --- /dev/null +++ b/uiot/http/http_upload_file.c @@ -0,0 +1,231 @@ +/* +* 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 "utils_httpc.h" +#include "lite-utils.h" +#include "ca.h" +#include "utils_sha2.h" + +int IOT_GET_URL_AND_AUTH(const char *product_sn, const char *device_sn, const char *device_sercret, char *file_name, char *upload_buffer, uint32_t upload_buffer_len, char *md5, char *authorization, char *put_url) +{ + int ret = SUCCESS_RET; + http_client_t *http_client_post = (http_client_t *)HAL_Malloc(sizeof(http_client_t)); + if(NULL == http_client_post) + { + LOG_ERROR("http_client_post malloc fail\n"); + return FAILURE_RET; + } + http_client_data_t *http_data_post = (http_client_data_t *)HAL_Malloc(sizeof(http_client_data_t)); + if(NULL == http_data_post) + { + HAL_Free(http_client_post); + LOG_ERROR("http_data_post malloc fail\n"); + return FAILURE_RET; + } + + memset(http_client_post, 0, sizeof(http_client_t)); + memset(http_data_post, 0, sizeof(http_client_data_t)); + + http_data_post->response_buf = (char *)HAL_Malloc(1024); + if(NULL == http_data_post->response_buf) + { + HAL_Free(http_client_post); + HAL_Free(http_data_post); + LOG_ERROR("http_data_post->response_buf malloc fail\n"); + return FAILURE_RET; + } + memset(http_data_post->response_buf, 0, 1024); + http_data_post->response_buf_len = 1024; + http_data_post->post_content_type = (char *)"application/json"; + http_data_post->post_buf = (unsigned char *)HAL_Malloc(1024); + if(NULL == http_data_post->post_buf) + { + HAL_Free(http_client_post); + HAL_Free(http_data_post->response_buf); + HAL_Free(http_data_post); + LOG_ERROR("http_data_post->post_buf malloc fail\n"); + return FAILURE_RET; + } + memset(http_data_post->post_buf, 0, 1024); + + HAL_Snprintf((char *)http_data_post->post_buf, 1024, "{\"ProductSN\":\"%s\",\"DeviceSN\":\"%s\"," \ + "\"FileName\":\"%s\",\"FileSize\":%d,\"MD5\":\"%s\",\"Content-Type\":\"plain/text\"}", \ + product_sn, device_sn, file_name, upload_buffer_len, md5); + http_data_post->post_buf_len = strlen((char *)http_data_post->post_buf); + uint8_t mac_output_hex[32] = {0}; + char mac_output_char[65] = {0}; + + utils_hmac_sha256((const uint8_t *)http_data_post->post_buf, http_data_post->post_buf_len, (const uint8_t *)device_sercret, strlen(device_sercret), mac_output_hex); + LITE_hexbuf_convert((unsigned char *)mac_output_hex, mac_output_char, 32, 0); + LOG_DEBUG("hmac:%s\r\n",mac_output_char); + + http_client_post->header = (char *)HAL_Malloc(1024); + if(NULL == http_client_post->header) + { + HAL_Free(http_client_post); + HAL_Free(http_data_post->response_buf); + HAL_Free(http_data_post->post_buf); + HAL_Free(http_data_post); + LOG_ERROR("http_client_post->header malloc fail\n"); + return FAILURE_RET; + } + memset(http_client_post->header, 0, 1024); + HAL_Snprintf(http_client_post->header, 1024, "Content-Type: application/json\r\nAuthorization:%s\r\n",mac_output_char); + + const char *ca_crt = iot_https_ca_get(); + char *url = (char *)"https://file-cn-sh2.iot.ucloud.cn/api/v1/url"; + + ret = http_client_common(http_client_post, url, 443, ca_crt, HTTP_POST, http_data_post,5000); + if(SUCCESS_RET != ret) + { + LOG_ERROR("HTTP_POST error\n"); + goto end; + } + + ret = http_client_recv_data(http_client_post, 5000, http_data_post); + if(SUCCESS_RET != ret) + { + LOG_ERROR("http_client_recv_data error\n"); + goto end; + } + LOG_DEBUG("response_buf:%s\n",http_data_post->response_buf); + + char *temp_auth = LITE_json_value_of((char *)"Authorization", http_data_post->response_buf); + if(NULL == temp_auth) + { + LOG_ERROR("parse Authorization error\n"); + goto end; + } + strncpy(authorization,temp_auth,strlen(temp_auth)); + authorization[strlen(temp_auth)+1] = '\0'; + LOG_DEBUG("authorization:%s\n",authorization); + HAL_Free(temp_auth); + + char *temp_url = LITE_json_value_of((char *)"URL", http_data_post->response_buf); + if(NULL == temp_url) + { + LOG_ERROR("parse URL error\n"); + goto end; + } + strncpy(put_url,temp_url,strlen(temp_url)); + put_url[strlen(temp_url)+1] = '\0'; + LOG_DEBUG("put_url:%s\n",put_url); + HAL_Free(temp_url); + +end: + http_client_close(http_client_post); + HAL_Free(http_client_post->header); + HAL_Free(http_client_post); + HAL_Free(http_data_post->response_buf); + HAL_Free(http_data_post->post_buf); + HAL_Free(http_data_post); + return ret; +} + +int IOT_HTTP_UPLOAD_FILE(char *file_name, char *upload_buffer, uint32_t upload_buffer_len, char *md5, char *authorization, char *url, uint32_t timeout_ms) +{ + int ret = SUCCESS_RET; + http_client_t *http_client_put = (http_client_t *)HAL_Malloc(sizeof(http_client_t)); + if(NULL == http_client_put) + { + LOG_ERROR("http_client_put malloc fail\n"); + return FAILURE_RET; + } + http_client_data_t *http_data_put = (http_client_data_t *)HAL_Malloc(sizeof(http_client_data_t)); + if(NULL == http_data_put) + { + HAL_Free(http_client_put); + LOG_ERROR("http_data_put malloc fail\n"); + return FAILURE_RET; + } + + memset(http_client_put, 0, sizeof(http_client_t)); + memset(http_data_put, 0, sizeof(http_client_data_t)); + + http_data_put->response_buf = (char *)HAL_Malloc(512); + if(NULL == http_data_put) + { + HAL_Free(http_client_put); + HAL_Free(http_data_put); + LOG_ERROR("http_data_put->response_buf malloc fail\n"); + return FAILURE_RET; + } + + memset(http_data_put->response_buf, 0, 512); + http_data_put->response_buf_len = 512; + + http_client_put->header = (char *)HAL_Malloc(1024); + if(NULL == http_data_put) + { + HAL_Free(http_client_put); + HAL_Free(http_data_put->response_buf); + HAL_Free(http_data_put); + LOG_ERROR("http_client_put->header malloc fail\n"); + return FAILURE_RET; + } + memset(http_client_put->header, 0, 1024); + HAL_Snprintf(http_client_put->header, 1024, "Authorization:%s\r\nContent-Type:plain/text\r\nContent-MD5:%s\r\n",authorization,md5); + LOG_DEBUG("header:%s\n", http_client_put->header); + + http_data_put->post_content_type = (char *)"plain/text"; + + http_data_put->post_buf = (unsigned char *)HAL_Malloc(upload_buffer_len + 1); + if(NULL == http_data_put->post_buf) + { + HAL_Free(http_data_put->response_buf); + HAL_Free(http_data_put); + HAL_Free(http_client_put->header); + HAL_Free(http_client_put); + LOG_ERROR("http_data_put->post_buf malloc fail\n"); + return FAILURE_RET; + } + memset(http_data_put->post_buf, 0, upload_buffer_len + 1); + + strncpy((char *)http_data_put->post_buf, upload_buffer, upload_buffer_len); + http_data_put->post_buf[upload_buffer_len]= '\0'; + http_data_put->post_buf_len = upload_buffer_len; + const char *ca_crt = iot_https_ca_get(); + + ret = http_client_common(http_client_put, url, 443, ca_crt, HTTP_PUT, http_data_put, timeout_ms); + if(SUCCESS_RET != ret) + { + LOG_ERROR("http_client_common error\n"); + goto end; + } + + ret = http_client_recv_data(http_client_put, timeout_ms, http_data_put); + if(SUCCESS_RET != ret) + { + LOG_ERROR("http_client_recv_data error\n"); + goto end; + } + + LOG_DEBUG("content_len:%d response_received_len:%d\n",http_data_put->response_content_len, http_data_put->response_received_len); + LOG_DEBUG("response_buf:%s\n",http_data_put->response_buf); +end: + http_client_close(http_client_put); + HAL_Free(http_data_put->post_buf); + HAL_Free(http_data_put->response_buf); + HAL_Free(http_data_put); + HAL_Free(http_client_put->header); + HAL_Free(http_client_put); + return ret; +} + + diff --git a/uiot/sdk-impl/uiot_export_http.h b/uiot/sdk-impl/uiot_export_http.h new file mode 100644 index 0000000..99e91d5 --- /dev/null +++ b/uiot/sdk-impl/uiot_export_http.h @@ -0,0 +1,35 @@ +/* +* 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. +*/ + +#ifndef C_SDK_UIOT_EXPORT_HTTP_ +#define C_SDK_UIOT_EXPORT_HTTP_ + +#if defined(__cplusplus) +extern "C" { +#endif + +int IOT_HTTP_Get_Token(const char *product_sn, const char *device_sn, const char *device_sercret, char *token); + +int IOT_HTTP_Publish(char *token, char *topic, char *data, uint32_t timeout_ms); + +int IOT_GET_URL_AND_AUTH(const char *product_sn, const char *device_sn, const char *device_sercret, char *file_name, char *upload_buffer, uint32_t upload_buffer_len, char *md5, char *authorization, char *put_url); + +int IOT_HTTP_UPLOAD_FILE(char *file_name, char *upload_buffer, uint32_t upload_buffer_len, char *md5, char *authorization, char *url, uint32_t timeout_ms); + +#if defined(__cplusplus) +} +#endif + +#endif diff --git a/uiot/shadow/src/shadow_client_manager.c b/uiot/shadow/src/shadow_client_manager.c index 29ea71d..e978d93 100644 --- a/uiot/shadow/src/shadow_client_manager.c +++ b/uiot/shadow/src/shadow_client_manager.c @@ -121,6 +121,8 @@ void uiot_shadow_reset(void *pClient) uiot_shadow_unsubscribe_topic(shadow_client,SHADOW_SUBSCRIBE_REQUEST_TEMPLATE); uiot_shadow_unsubscribe_topic(shadow_client,SHADOW_SUBSCRIBE_SYNC_TEMPLATE); + + IOT_MQTT_Yield(shadow_client->mqtt,200); if (shadow_client->inner_data.request_list) { diff --git a/uiot/utils/utils_httpc.c b/uiot/utils/utils_httpc.c index 601ee95..565b8d3 100644 --- a/uiot/utils/utils_httpc.c +++ b/uiot/utils/utils_httpc.c @@ -122,8 +122,15 @@ static int _http_send_header(http_client_t *client, char *host, const char *path memset(send_buf, 0, HTTP_CLIENT_SEND_BUF_SIZE); len = 0; /* Reset send buffer */ - HAL_Snprintf(buf, sizeof(buf), "%s %s HTTP/1.1\r\nHost: %s\r\nRange: bytes=%d-%d\r\n", pMethod, path, host, size_fetched, size_fetched + range_len); /* Write request */ - + if(range_len != 0) + { + HAL_Snprintf(buf, sizeof(buf), "%s %s HTTP/1.1\r\nHost: %s\r\nRange: bytes=%d-%d\r\n", pMethod, path, host, size_fetched, size_fetched + range_len); /* Write request */ + } + else + { + HAL_Snprintf(buf, sizeof(buf), "%s %s HTTP/1.1\r\nHost: %s\r\n", pMethod, path, host); /* Write request */ + } + ret = _utils_fill_tx_buffer(client, send_buf, &len, buf, strlen(buf)); if (ret < 0) { /* LOG_ERROR("Could not write request"); */ @@ -479,6 +486,27 @@ int http_client_connect(http_client_t *client, const char *url, int port, const return rc; } +int http_client_common(http_client_t *client, const char *url, int port, const char *ca_crt, + HTTP_Request_Method method, http_client_data_t *client_data, uint32_t timeout_ms) { + int rc; + + if (client->net.handle == 0) { + rc = http_client_connect(client, url, port, ca_crt); + if (rc != SUCCESS_RET) { + return rc; + } + } + + rc = _http_send_request(client, url, method, 0, 0, client_data, timeout_ms); + if (rc != SUCCESS_RET) { + LOG_ERROR("http_send_request error, rc = %d", rc); + http_client_close(client); + return rc; + } + + return SUCCESS_RET; +} + int http_client_recv_data(http_client_t *client, uint32_t timeout_ms, http_client_data_t *client_data) { int rc = SUCCESS_RET; Timer timer; @@ -536,6 +564,18 @@ void http_client_file_md5(char* file_path, char *output) HAL_Free(buffer); } +void http_client_buffer_md5(char* buffer, uint32_t buffer_len, char *output) +{ + iot_md5_context ctx; + + utils_md5_init(&ctx); + utils_md5_starts(&ctx); + utils_md5_update(&ctx, (unsigned char *)buffer, buffer_len); + utils_md5_finish_hb2hex(&ctx, output); + utils_md5_free(&ctx); + return; +} + #ifdef __cplusplus } #endif diff --git a/uiot/utils/utils_httpc.h b/uiot/utils/utils_httpc.h index 6883db7..d270f62 100644 --- a/uiot/utils/utils_httpc.h +++ b/uiot/utils/utils_httpc.h @@ -59,6 +59,9 @@ typedef struct { int http_client_connect(http_client_t *client, const char *url, int port, const char *ca_crt); +int http_client_common(http_client_t *client, const char *url, int port, const char *ca_crt, + HTTP_Request_Method method, http_client_data_t *client_data, uint32_t timeout_ms); + int http_client_recv_data(http_client_t *client, uint32_t timeout_ms, http_client_data_t *client_data); void http_client_close(http_client_t *client); @@ -67,6 +70,8 @@ int _http_send_user_data(http_client_t *client, http_client_data_t *client_data, void http_client_file_md5(char* file_path, char *output); +void http_client_buffer_md5(char* buffer, uint32_t buffer_len, char *output); + int _http_send_request(http_client_t *client, const char *url, HTTP_Request_Method method, uint32_t size_fetched, size_t range_len, http_client_data_t *client_data, uint32_t timeout_ms); diff --git a/uiot/utils/utils_net.c b/uiot/utils/utils_net.c index f153228..8358702 100644 --- a/uiot/utils/utils_net.c +++ b/uiot/utils/utils_net.c @@ -80,54 +80,6 @@ static int connect_ssl(utils_network_pt pNetwork) } } -/*** TCP connection ***/ -static int read_tcp(utils_network_pt pNetwork, unsigned char *buffer, size_t len, uint32_t timeout_ms) -{ - if (NULL == pNetwork) { - LOG_ERROR("network is null"); - return FAILURE_RET; - } - - return HAL_TCP_Read((uintptr_t)pNetwork->handle, buffer, len, timeout_ms); -} - - -static int write_tcp(utils_network_pt pNetwork, unsigned char *buffer, size_t len, uint32_t timeout_ms) -{ - if (NULL == pNetwork) { - LOG_ERROR("network is null"); - return FAILURE_RET; - } - - return HAL_TCP_Write((uintptr_t)pNetwork->handle, buffer, len, timeout_ms); -} - -static int disconnect_tcp(utils_network_pt pNetwork) -{ - if (NULL == pNetwork) { - LOG_ERROR("network is null"); - return FAILURE_RET; - } - - HAL_TCP_Disconnect(pNetwork->handle); - pNetwork->handle = (uintptr_t)(-1); - return SUCCESS_RET; -} - -static int connect_tcp(utils_network_pt pNetwork) -{ - if (NULL == pNetwork) { - LOG_ERROR("network is null"); - return FAILURE_RET; - } - - pNetwork->handle = HAL_TCP_Connect(pNetwork->pHostAddress, pNetwork->port); - if (pNetwork->handle == (uintptr_t)(-1)) { - return FAILURE_RET; - } - - return SUCCESS_RET; -} #else /*** TCP connection ***/ static int read_tcp(utils_network_pt pNetwork, unsigned char *buffer, size_t len, uint32_t timeout_ms) @@ -184,17 +136,9 @@ int utils_net_read(utils_network_pt pNetwork, unsigned char *buffer, size_t len, { int ret = 0; #ifdef PKG_USING_UCLOUD_TLS - if (NULL != pNetwork->ca_crt) { ret = read_ssl(pNetwork, buffer, len, timeout_ms); - } - else - { - ret = read_tcp(pNetwork, buffer, len, timeout_ms); - } #else - if (NULL == pNetwork->ca_crt) { ret = read_tcp(pNetwork, buffer, len, timeout_ms); - } #endif return ret; @@ -204,17 +148,9 @@ int utils_net_write(utils_network_pt pNetwork,unsigned char *buffer, size_t len, { int ret = 0; #ifdef PKG_USING_UCLOUD_TLS - if (NULL != pNetwork->ca_crt) { ret = write_ssl(pNetwork, buffer, len, timeout_ms); - } - else - { - ret = write_tcp(pNetwork, buffer, len, timeout_ms); - } #else - if (NULL == pNetwork->ca_crt) { ret = write_tcp(pNetwork, buffer, len, timeout_ms); - } #endif return ret; @@ -224,17 +160,9 @@ int utils_net_disconnect(utils_network_pt pNetwork) { int ret = 0; #ifdef PKG_USING_UCLOUD_TLS - if (NULL != pNetwork->ca_crt) { ret = disconnect_ssl(pNetwork); - } - else - { - ret = disconnect_tcp(pNetwork); - } #else - if (NULL == pNetwork->ca_crt) { ret = disconnect_tcp(pNetwork); - } #endif return ret; @@ -244,17 +172,9 @@ int utils_net_connect(utils_network_pt pNetwork) { int ret = 0; #ifdef PKG_USING_UCLOUD_TLS - if (NULL != pNetwork->ca_crt) { ret = connect_ssl(pNetwork); - } - else - { - ret = connect_tcp(pNetwork); - } #else - if (NULL == pNetwork->ca_crt) { ret = connect_tcp(pNetwork); - } #endif return ret;