Skip to content

Commit

Permalink
1. 同步OTA示例优化。
Browse files Browse the repository at this point in the history
2. 设备信息新增设备类型,支持网关场景。
3. 新增设备动态注册功能及示例。
  • Loading branch information
yougaliu committed Mar 11, 2020
1 parent 9664d19 commit 6e4e6a9
Show file tree
Hide file tree
Showing 19 changed files with 1,110 additions and 331 deletions.
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ set(PLATFORM "linux")
#set(COMPILE_TOOLS "MSVC")
#set(PLATFORM "windows")


# 是否使能设备动态注册
set(FEATURE_DEV_DYN_REG_ENABLED ON)

# 是否使能网关功能
set(FEATURE_GATEWAY_ENABLED OFF)

Expand All @@ -28,7 +32,7 @@ set(FEATURE_MULTITHREAD_ENABLED OFF)
set(FEATURE_EVENT_POST_ENABLED OFF)

# 是否打开数据模板行为功能
set(FEATURE_ACTION_ENABLED ON)
set(FEATURE_ACTION_ENABLED OFF)

# 是否打开OTA固件升级总开关
set(FEATURE_OTA_COMM_ENABLED ON)
Expand All @@ -45,6 +49,7 @@ set(FEATURE_DEBUG_DEV_INFO_USED OFF)
# 是否使用HTTPS下载固件
set(FEATURE_OTA_USE_HTTPS ON)


# 使用SDK AT组件实现通用TCP模组网络读写需要配置 --->begin
# 是否打开AT模组TCP功能
set(FEATURE_AT_TCP_ENABLED OFF)
Expand Down Expand Up @@ -115,6 +120,7 @@ if(${FEATURE_OTA_COMM_ENABLED} STREQUAL "ON")
option(OTA_MQTT_CHANNEL "Enable OTA_MQTT_CHANNEL" ON)
endif()

option(DEV_DYN_REG_ENABLED "Enable DEV_DYN_REG" ${FEATURE_DEV_DYN_REG_ENABLED})
option(GATEWAY_ENABLED "Enable GATEWAY" ${FEATURE_GATEWAY_ENABLED})
option(AUTH_WITH_NOTLS "Enable AUTH_WITH_NOTLS" ${FEATURE_AUTH_WITH_NOTLS})
option(EVENT_POST_ENABLED "Enable EVENT_POST" ${FEATURE_EVENT_POST_ENABLED})
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ $(call CompLib_Map, SYSTEM_COMM_ENABLED, \
$(SRC_DIR)/services/system \
)

$(call CompLib_Map, DEV_DYN_REG_ENABLED, \
$(SRC_DIR)/services/dynreg \
)

$(call CompLib_Map, OTA_COMM_ENABLED, \
$(SRC_DIR)/services/ota \
)
Expand Down
10 changes: 5 additions & 5 deletions include/config.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/* #undef AUTH_MODE_CERT */
#define AUTH_MODE_KEY
/* #undef AUTH_WITH_NOTLS */
#define GATEWAY_ENABLED
/* #undef GATEWAY_ENABLED */
/* #undef COAP_COMM_ENABLED */
#define OTA_MQTT_CHANNEL
/* #undef SYSTEM_COMM */
/* #undef EVENT_POST_ENABLED */
/* #undef ACTION_ENABLED*/
/* #undef DEV_DYN_REG_ENABLED */
/* #undef ACTION_ENABLED */
#define DEV_DYN_REG_ENABLED
/* #undef LOG_UPLOAD */
/* #undef IOT_DEBUG */
/* #undef DEBUG_DEV_INFO_USED */
/* #undef AT_TCP_ENABLED */
/* #undef AT_UART_RECV_IRQ */
/* #undef AT_OS_USED */
/* #undef AT_DEBUG */
/* #undef OTA_USE_HTTPS */
/* #undef GATEWAY_ENABLED*/
#define OTA_USE_HTTPS
/* #undef GATEWAY_ENABLED */
/* #undef MULTITHREAD_ENABLED */
40 changes: 40 additions & 0 deletions include/exports/qcloud_iot_export_dynreg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Tencent is pleased to support the open source community by making IoT Hub available.
* Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* 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 QLCOUD_IOT_EXPORT_DYNREG_H_
#define QLCOUD_IOT_EXPORT_DYNREG_H_

#ifdef __cplusplus
extern "C" {
#endif

#include "qcloud_iot_export.h"

/**
* @brief Do dynamic register/create device
*
* @param pDevInfo In: device info with [ProductId, ProductKey, DeviceName]
* Out: device info with [ProductId, DeviceName, DeviceSecret or Device cert/key file]
*
* @return QCLOUD_RET_SUCCESS for success, or err code for failure
*/
int IOT_DynReg_Device(DeviceInfo *pDevInfo);


#ifdef __cplusplus
}
#endif

#endif //QLCOUD_IOT_EXPORT_DYNREG_H_
9 changes: 9 additions & 0 deletions include/exports/qcloud_iot_export_ota.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ int IOT_OTA_StartDownload(void *handle, uint32_t offset, uint32_t size);
*/
void IOT_OTA_UpdateClientMd5(void *handle, char * buff, uint32_t size);


/**
* @brief Reset MD5 of local firmware
*
* @param handle: OTA module handle
*
*/
int IOT_OTA_ResetClientMD5(void *handle);

/**
* @brief Report local firmware version to server
* NOTE: do this report before real download
Expand Down
9 changes: 9 additions & 0 deletions include/qcloud_iot_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ extern "C" {
#define MAX_NUM_SUB_DEV (50)

/**************** QCloud IoT C-SDK constants end *************************/
typedef enum{
eCOMMON_DEV = 0, //common dev
eGW_DEV = 1, //Gateway dev
eGW_SUB_DEV = 2, //sub dev of Gateway
eDEFAULT_DEV
}eDevType;

typedef struct {
char product_id[MAX_SIZE_OF_PRODUCT_ID + 1];
Expand All @@ -70,6 +76,7 @@ typedef struct {
#ifdef DEV_DYN_REG_ENABLED
char product_secret[MAX_SIZE_OF_PRODUCT_SECRET + 1];
#endif
eDevType dev_type;
} DeviceInfo;

#ifdef GATEWAY_ENABLED
Expand All @@ -88,6 +95,8 @@ typedef struct {
#include "qcloud_iot_export_data_template.h"
#include "qcloud_iot_export_ota.h"
#include "qcloud_iot_export_gateway.h"
#include "qcloud_iot_export_dynreg.h"




Expand Down
3 changes: 3 additions & 0 deletions make.settings
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ FEATURE_GATEWAY_ENABLED = n
# 是否使能多线程
FEATURE_MULTITHREAD_ENABLED = n

# 是否使能设备动态注册
FEATURE_DEV_DYN_REG_ENABLED = y

# 是否打开OTA固件升级总开关
FEATURE_OTA_COMM_ENABLED = y

Expand Down
6 changes: 6 additions & 0 deletions platform/os/linux/HAL_Device_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,12 @@ static int iot_save_devinfo_to_json_file(DeviceInfo *pDevInfo)

Log_d("JsonDoc(%d):%s", MAX_DEV_INFO_FILE_LEN - remain_size, JsonDoc);

if(eGW_SUB_DEV == pDevInfo->dev_type) {
memset(sg_device_info_file, '\0', MAX_SIZE_OF_DEVICE_INFO_FILE);
HAL_Snprintf(sg_device_info_file, MAX_SIZE_OF_DEVICE_INFO_FILE, "./%s_%s_device_info.json", \
pDevInfo->product_id, pDevInfo->device_name);
}

fp = fopen(sg_device_info_file, "w");
if (NULL == fp) {
Log_e("open file %s failed", sg_device_info_file);
Expand Down
7 changes: 6 additions & 1 deletion samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,9 @@ if(${FEATURE_OTA_COMM_ENABLED} STREQUAL "ON")
target_link_libraries(ota_mqtt_sample ${lib})
endif()


# DYN_REG
if(${FEATURE_DEV_DYN_REG_ENABLED} STREQUAL "ON")
file(GLOB src_dynreg_dev_sample ${PROJECT_SOURCE_DIR}/samples/dynreg_dev/dynreg_dev_sample.c)
add_executable(dynreg_dev_sample ${src_dynreg_dev_sample})
target_link_libraries(dynreg_dev_sample ${lib})
endif()
13 changes: 11 additions & 2 deletions samples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ ifeq ($(FEATURE_AUTH_MODE),CERT)
CFLAGS += -DAUTH_MODE_CERT
endif

.PHONY: ota_mqtt_sample data_template_sample gateway_sample mqtt_sample
.PHONY: ota_mqtt_sample data_template_sample gateway_sample mqtt_sample dynreg_dev_sample

all: ota_mqtt_sample data_template_sample gateway_sample mqtt_sample
all: ota_mqtt_sample data_template_sample gateway_sample mqtt_sample dynreg_dev_sample


ifneq (,$(filter -DOTA_COMM_ENABLED,$(CFLAGS)))
Expand Down Expand Up @@ -88,6 +88,15 @@ gateway_sample:
mv $@ $(FINAL_DIR)/bin
endif

ifneq (,$(filter -DDEV_DYN_REG_ENABLED,$(CFLAGS)))
dynreg_dev_sample:
$(TOP_Q) \
$(PLATFORM_CC) $(CFLAGS) $(SAMPLE_DIR)/dynreg_dev/$@.c $(LDFLAGS) -o $@

$(TOP_Q) \
mv $@ $(FINAL_DIR)/bin
endif

clean:
rm -rf $(FINAL_DIR)/bin/*

112 changes: 112 additions & 0 deletions samples/dynreg_dev/dynreg_dev_sample.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Tencent is pleased to support the open source community by making IoT Hub available.
* Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
* Licensed under the MIT License (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* 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 <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

#include "utils_getopt.h"
#include "qcloud_iot_export.h"
#include "qcloud_iot_import.h"

#ifdef AUTH_MODE_CERT
/* NULL cert file */
#define QCLOUD_IOT_NULL_CERT_FILENAME "YOUR_DEVICE_CERT_FILE_NAME"
/* NULL key file */
#define QCLOUD_IOT_NULL_KEY_FILENAME "YOUR_DEVICE_PRIVATE_KEY_FILE_NAME"
#else
/* NULL device secret */
#define QCLOUD_IOT_NULL_DEVICE_SECRET "YOUR_IOT_PSK"
#endif


int main(int argc, char **argv)
{
int c;
while ((c = utils_getopt(argc, argv, "c:")) != EOF)
switch (c) {
case 'c':
if (HAL_SetDevInfoFile(utils_optarg))
return -1;
break;

default:
HAL_Printf("usage: %s [options]\n"
" [-c <config file for DeviceInfo>] \n"
, argv[0]);

return -1;
}


//init log level
IOT_Log_Set_Level(eLOG_DEBUG);

int ret;
DeviceInfo sDevInfo;
bool infoNullFlag = false;

memset((char *)&sDevInfo, 0, sizeof(DeviceInfo));

ret = HAL_GetDevInfo(&sDevInfo);

#ifndef GATEWAY_ENABLED
sDevInfo.dev_type = eCOMMON_DEV;
#else
sDevInfo.dev_type = eGW_SUB_DEV;
#endif

#ifdef AUTH_MODE_CERT
/* just demo the cert/key files are empty */
if (!strcmp(sDevInfo.dev_cert_file_name, QCLOUD_IOT_NULL_CERT_FILENAME)
|| !strcmp(sDevInfo.dev_key_file_name, QCLOUD_IOT_NULL_KEY_FILENAME)) {
Log_d("dev Cert not exist!");
infoNullFlag = true;
} else {
Log_d("dev Cert exist");
}
#else
/* just demo the PSK is empty */
if (!strcmp(sDevInfo.device_secret, QCLOUD_IOT_NULL_DEVICE_SECRET)) {
Log_d("dev psk not exist!");
infoNullFlag = true;
} else {
Log_d("dev psk exist");
}
#endif

/* device cert/key files or PSK is empty, do dynamic register to fetch */
if (infoNullFlag) {
if (QCLOUD_RET_SUCCESS == IOT_DynReg_Device(&sDevInfo)) {
ret = HAL_SetDevInfo(&sDevInfo);
if (QCLOUD_RET_SUCCESS != ret) {
Log_e("devices info save fail");
} else {
#ifdef AUTH_MODE_CERT
Log_d("dynamic register success, productID: %s, devName: %s, CertFile: %s, KeyFile: %s", \
sDevInfo.product_id, sDevInfo.device_name, sDevInfo.dev_cert_file_name, sDevInfo.dev_key_file_name);
#else
Log_d("dynamic register success,productID: %s, devName: %s, device_secret: %s", \
sDevInfo.product_id, sDevInfo.device_name, sDevInfo.device_secret);
#endif
}
} else {
Log_e("%s dynamic register fail", sDevInfo.device_name);
}
}

return ret;
}
Loading

0 comments on commit 6e4e6a9

Please sign in to comment.