support http publish message

This commit is contained in:
ethan.du 2020-02-21 17:33:21 +08:00
parent e3f8593a70
commit 22268aa835
16 changed files with 333 additions and 36 deletions

View File

@ -15,7 +15,7 @@ option(ENABLE_FEATURE_DEVICE_SHADOW "是否打开设备影子的总开关" ON)
option(ENABLE_FEATURE_OTA "是否打开OTA固件升级总开关" ON)
option(ENABLE_FEATURE_DEVICE_MODEL "是否打开物模型的总开关" ON)
option(ENABLE_FEATURE_AUTH_MODE_DYNAMIC "是否打开设备动态注册" ON)
option(ENABLE_FEATURE_UPLOAD_FILE "是否打开设备文件上传" ON)
option(ENABLE_FEATURE_HTTP_CLIENT "是否打开http上云" ON)
option(ENABLE_TLS_SUPPORT "是否打开TLS支持" ON)
option(ENABLE_SDK_TESTS "是否打开SDK测试用例编译" ON)
@ -108,12 +108,12 @@ else ()
message("ENABLE_FEATURE_DEVICE_MODEL = n")
endif ()
if (ENABLE_FEATURE_UPLOAD_FILE)
message("ENABLE_FEATURE_UPLOAD_FILE = y")
AUX_SOURCE_DIRECTORY(src/upload_file UPLOAD_FILE_SRC)
set(src_iot_sdk ${src_iot_sdk} ${UPLOAD_FILE_SRC})
if (ENABLE_FEATURE_HTTP_CLIENT)
message("ENABLE_FEATURE_HTTP_CLIENT = y")
AUX_SOURCE_DIRECTORY(src/http HTTP_CLENT_SRC)
set(src_iot_sdk ${src_iot_sdk} ${HTTP_CLENT_SRC})
else ()
message("ENABLE_FEATURE_UPLOAD_FILE = n")
message("ENABLE_FEATURE_HTTP_CLIENT = n")
endif ()
if (ENABLE_TLS_SUPPORT)

View File

@ -17,7 +17,7 @@ $(call CompLib_Map, OTA_ENABLED, src/ota/src)
$(call CompLib_Map, DEVICE_MODEL_ENABLED, src/dev_model/src)
$(call CompLib_Map, FILE_UPLOAD_ENABLED, src/upload_file)
$(call CompLib_Map, HTTP_CLIENT_ENABLED, src/http)
$(call CompLib_Map, SUPPORT_AT_CMD, src/at/src src/at/class/$(PLATFORM_MODULE) platform/module)
IOTSDK_SRC_FILES := \

View File

@ -18,7 +18,7 @@ FEATURE_MQTT_COMM_ENABLED = y # 是否打开MQTT通道
FEATURE_DEVICE_SHADOW_ENABLED = y # 是否打开设备影子
FEATURE_OTA_ENABLED = y # 是否打开OTA固件升级
FEATURE_DEVICE_MODEL_ENABLED = y # 是否打开物模型
FEATURE_FILE_UPLOAD_ENABLED = y # 是否打开文件上传
FEATURE_HTTP_CLIENT_ENABLED = y # 是否打开http上云
FEATURE_AUTH_MODE_DYNAMIC = y # 是否使能设备动态注册
FEATURE_SUPPORT_TLS = y # 是否打开TLS支持

View File

@ -31,10 +31,13 @@ if (ENABLE_FEATURE_AUTH_MODE_DYNAMIC)
add_dependencies(dynamic_auth_sample iot_sdk iot_platform)
endif ()
if (ENABLE_FEATURE_UPLOAD_FILE)
add_executable(download_file_sample upload_file/download_file_sample.c)
if (ENABLE_FEATURE_HTTP_CLIENT)
add_executable(download_file_sample http/download_file_sample.c)
add_dependencies(download_file_sample iot_sdk iot_platform)
add_executable(upload_file_sample upload_file/upload_file_sample.c)
add_dependencies(upload_file_sample iot_sdk iot_platform)
add_executable(upload_file_sample http/upload_file_sample.c)
add_dependencies(upload_file_sample iot_sdk iot_platform)
add_executable(http_client_sample http/http_client_sample.c)
add_dependencies(http_client_sample iot_sdk iot_platform)
endif ()

View File

@ -24,7 +24,7 @@
#include "uiot_import.h"
#include "ca.h"
#include "utils_httpc.h"
#include "uiot_export_file_upload.h"
#include "uiot_export_http.h"
int main(int argc, char **argv) {
http_client_t *http_client = (http_client_t *)HAL_Malloc(sizeof(http_client_t));

View File

@ -0,0 +1,66 @@
/*
* 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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
#include <stdbool.h>
#include <string.h>
#include <signal.h>
#include "uiot_import.h"
#include "ca.h"
#include "utils_httpc.h"
#include "uiot_export_http.h"
#define UIOT_MY_PRODUCT_SN "PRODUCT_SN"
#define UIOT_MY_DEVICE_SN "DEVICE_SN"
#define UIOT_MY_DEVICE_SECRET "DEVICE_SECRET"
#define UIOT_PUBLISH_TOPIC "%s/%s/topic"
int main(int argc, char **argv) {
char *token = (char *)malloc(1024);
memset(token, 0, 1024);
int ret = SUCCESS_RET;
char *topic = (char *)malloc(256);
memset(topic, 0, 256);
HAL_Snprintf((char *)topic, 256, UIOT_PUBLISH_TOPIC,UIOT_MY_PRODUCT_SN, UIOT_MY_DEVICE_SN);
char *data = "{\"test\": \"18\"}";
ret = IOT_HTTP_Get_Token(UIOT_MY_PRODUCT_SN, UIOT_MY_DEVICE_SN, UIOT_MY_DEVICE_SECRET, token);
if(SUCCESS_RET != ret)
{
HAL_Printf("get Token fail,ret:%d\r\n", ret);
return FAILURE_RET;
}
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 FAILURE_RET;
}
HAL_Printf("Publish success\n");
HAL_Free(token);
HAL_Free(topic);
return ret;
}

View File

@ -24,7 +24,7 @@
#include "uiot_import.h"
#include "ca.h"
#include "utils_httpc.h"
#include "uiot_export_file_upload.h"
#include "uiot_export_http.h"
#define UIOT_MY_PRODUCT_SN "PRODUCT_SN"
@ -57,7 +57,7 @@ int main(int argc, char **argv) {
HAL_Printf("get put_url:%s\n", put_url);
//上传文件超时时间与文件长度相关
ret = IOT_UPLOAD_FILE(FILE_PATH, md5, authorization, put_url, 40000);
ret = IOT_HTTP_UPLOAD_FILE(FILE_PATH, md5, authorization, put_url, 10000);
if(SUCCESS_RET != ret)
{
HAL_Printf("upload file fail,ret:%d\r\n", ret);
@ -66,5 +66,6 @@ int main(int argc, char **argv) {
HAL_Printf("upload success\n");
HAL_Free(authorization);
HAL_Free(put_url);
return ret;
}

View File

@ -64,23 +64,29 @@ dev_model_sample:
mv $@ $(FINAL_DIR)/bin
endif
ifneq (,$(filter -DFILE_UPLOAD_ENABLED,$(CFLAGS)))
ifneq (,$(filter -DHTTP_CLIENT_ENABLED,$(CFLAGS)))
http_client_sample:
$(TOP_Q) \
$(PLATFORM_CC) $(CFLAGS) $(SAMPLE_DIR)/http/$@.c $(LDFLAGS) -o $@
$(TOP_Q) \
mv $@ $(FINAL_DIR)/bin
download_file_sample:
$(TOP_Q) \
$(PLATFORM_CC) $(CFLAGS) $(SAMPLE_DIR)/upload_file/$@.c $(LDFLAGS) -o $@
$(PLATFORM_CC) $(CFLAGS) $(SAMPLE_DIR)/http/$@.c $(LDFLAGS) -o $@
$(TOP_Q) \
mv $@ $(FINAL_DIR)/bin
upload_file_sample:
$(TOP_Q) \
$(PLATFORM_CC) $(CFLAGS) $(SAMPLE_DIR)/upload_file/$@.c $(LDFLAGS) -o $@
$(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/

221
src/http/http_client.c Normal file
View File

@ -0,0 +1,221 @@
/*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#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);
HAL_Free(http_data_post->response_buf);
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);
HAL_Free(http_data_post->response_buf);
HAL_Free(http_data_post->post_buf);
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)
{
char *temp_message = LITE_json_value_of((char *)"Message", http_data_post->response_buf);
LOG_ERROR("%s\n", temp_message);
LOG_ERROR("parse Token error\n");
goto end;
}
strncpy(token,temp,strlen(temp));
token[strlen(temp)+1] = '\0';
LOG_DEBUG("token:%s\n",token);
end:
HAL_Free(http_client_post);
HAL_Free(http_data_post->response_buf);
HAL_Free(http_data_post->post_buf);
HAL_Free(http_client_post->header);
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);
HAL_Free(http_data_post->response_buf);
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);
HAL_Free(http_data_post->response_buf);
HAL_Free(http_data_post->post_buf);
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);
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;
}
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);
LOG_DEBUG("%s\n", temp_message);
end:
HAL_Free(http_client_post);
HAL_Free(http_data_post->response_buf);
HAL_Free(http_data_post->post_buf);
HAL_Free(http_client_post->header);
HAL_Free(http_data_post);
return ret;
}

View File

@ -160,7 +160,7 @@ end:
return ret;
}
int IOT_UPLOAD_FILE(char *file_path, char *md5, char *authorization, char *url, uint32_t timeout_ms)
int IOT_HTTP_UPLOAD_FILE(char *file_path, 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));

View File

@ -46,9 +46,6 @@ void* IOT_MQTT_Construct(MQTTInitParams *pParams)
POINTER_VALID_CHECK(pParams, NULL);
STRING_PTR_VALID_CHECK(pParams->product_sn, NULL);
STRING_PTR_VALID_CHECK(pParams->device_sn, NULL);
#ifndef AUTH_MODE_DYNAMIC
STRING_PTR_VALID_CHECK(pParams->device_secret, NULL);
#endif
UIoT_Client* mqtt_client = NULL;
@ -81,9 +78,8 @@ void* IOT_MQTT_Construct(MQTTInitParams *pParams)
}
int auth_mode = 0;
#ifdef AUTH_MODE_DYNAMIC
//动态认证时如果还未获取设备密钥则先使用产品密钥进行预认证
if(NULL == pParams->device_secret)
if((NULL == pParams->device_secret) && (NULL != pParams->product_secret))
{
auth_mode = DYNAMIC_AUTH;
int password_len = strlen(pParams->product_secret) + 1;
@ -96,7 +92,7 @@ void* IOT_MQTT_Construct(MQTTInitParams *pParams)
}
HAL_Snprintf(connect_params.password, password_len, "%s", pParams->product_secret);
}
#endif
//使用获取到的设备密钥进行静态注册
if(NULL != pParams->device_secret)
{

View File

@ -13,16 +13,20 @@
* permissions and limitations under the License.
*/
#ifndef C_SDK_UIOT_EXPORT_FILE_UPLOAD_
#define C_SDK_UIOT_EXPORT_FILE_UPLOAD_
#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_path, char *md5, char *authorization, char *put_url);
int IOT_UPLOAD_FILE(char *file_path, char *md5, char *authorization, char *url, uint32_t timeout_ms);
int IOT_HTTP_UPLOAD_FILE(char *file_path, char *md5, char *authorization, char *url, uint32_t timeout_ms);
#if defined(__cplusplus)
}

View File

@ -22,7 +22,7 @@
#include "uiot_import.h"
#include "ca.h"
#include "utils_httpc.h"
#include "uiot_export_file_upload.h"
#include "uiot_export_http.h"
class HTTPClientTests : public testing::Test
{
@ -108,7 +108,7 @@ TEST_F(HTTPClientTests, HTTPUploadFile) {
ret = IOT_GET_URL_AND_AUTH(ProductSN, DeviceSN, DeviceSecret, file_path, md5, authorization, put_url);
ASSERT_TRUE(SUCCESS_RET == ret);
ret = IOT_UPLOAD_FILE(file_path, md5, authorization, put_url, 40000);
ret = IOT_HTTP_UPLOAD_FILE(file_path, md5, authorization, put_url, 40000);
ASSERT_TRUE(SUCCESS_RET == ret);
HAL_Free(authorization);
HAL_Free(put_url);

View File

@ -7,7 +7,7 @@ FEATURE_MQTT_COMM_ENABLED ?= y
FEATURE_DEVICE_SHADOW_ENABLED ?= n
FEATURE_OTA_ENABLED ?= n
FEATURE_DEVICE_MODEL_ENABLED ?= y
FEATURE_FILE_UPLOAD_ENABLED ?= y
FEATURE_HTTP_CLIENT_ENABLED ?= y
FEATURE_AUTH_MODE_DYNAMIC ?= n
FEATURE_SUPPORT_TLS ?= n
FEATURE_SUPPORT_AT_CMD ?= n

View File

@ -15,7 +15,7 @@ SWITCH_VARS := \
FEATURE_MQTT_RMDUP_MSG_ENABLED \
FEATURE_AUTH_MODE_DYNAMIC \
FEATURE_DEVICE_MODEL_ENABLED \
FEATURE_FILE_UPLOAD_ENABLED \
FEATURE_HTTP_CLIENT_ENABLED \
FEATURE_SUPPORT_AT_CMD
$(foreach v, \

View File

@ -16,6 +16,6 @@ endif
$(TOP_Q) \
rm -rf ${TEMP_DIR}
.PHONY: mqtt_sample ota_sample shadow_sample dynamic_auth_sample dev_model_sample samples_final smart_bracelet_heart_rate_shadow_sample smart_bracelet_walk_step_shadow_sample download_file_sample upload_file_sample
.PHONY: mqtt_sample ota_sample shadow_sample dynamic_auth_sample dev_model_sample samples_final smart_bracelet_heart_rate_shadow_sample smart_bracelet_walk_step_shadow_sample download_file_sample upload_file_sample http_client_sample
final : mqtt_sample ota_sample shadow_sample dynamic_auth_sample dev_model_sample samples_final smart_bracelet_heart_rate_shadow_sample smart_bracelet_walk_step_shadow_sample download_file_sample upload_file_sample
final : mqtt_sample ota_sample shadow_sample dynamic_auth_sample dev_model_sample samples_final smart_bracelet_heart_rate_shadow_sample smart_bracelet_walk_step_shadow_sample download_file_sample upload_file_sample http_client_sample