board_perf:add support for qcloud c-sdk on ch32v307 board
This commit is contained in:
@@ -62,6 +62,22 @@
|
||||
<tool id="ilg.gnumcueclipse.managedbuild.cross.riscv.tool.c.compiler.1731377187" name="GNU RISC-V Cross C Compiler" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.tool.c.compiler">
|
||||
<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.compiler.include.paths.1567947810" name="Include paths (-I)" superClass="ilg.gnumcueclipse.managedbuild.cross.riscv.option.c.compiler.include.paths" useByScannerDiscovery="true" valueType="includePath">
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/Debug}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/c-sdk/include}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/c-sdk/include/common}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/c-sdk/include/config}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/c-sdk/include/services/common}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/c-sdk/include/services/explorer}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/c-sdk/common/cryptology/inc}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/c-sdk/common/mqtt_packet/inc}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/c-sdk/common/utils/inc}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/c-sdk/examples}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/c-sdk/platform/network/inc}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/c-sdk/services/explorer/data_template/inc}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/c-sdk/services/explorer/service_mqtt/inc}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/c-sdk/services/common/log_upload/inc}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/c-sdk/services/common/mqtt_client/inc}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/net/sal_module_wrapper}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/devices/esp8266}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/fs/kv/include}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/fs/fatfs/include}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/${ProjName}/fs/vfs/include}""/>
|
||||
@@ -126,13 +142,19 @@
|
||||
</toolChain>
|
||||
</folderInfo>
|
||||
<sourceEntries>
|
||||
<entry excluding="examples/fatfs_through_vfs_sample.c|User/mqtt_iot_explorer.c|examples/kv_sample.c|fs/fatfs|fs/vfs|fs/kv|components/kv|platform/hal/wch/ch32v30xx|kernel/hal|devices/esp8266_tencent_firmware|net/tencent_firmware_module_wrapper|kernel/core|net/at|kernel|arch|TencentOS-Tiny|Startup|Peripheral|Ld|Debug|Core" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
|
||||
<entry excluding="examples/qcloud_csdk_iot_explorer|c-sdk/services/common|c-sdk/include|examples/mqtt_iot_explorer.c|c-sdk/examples|c-sdk/app|c-sdk/services/explorer|c-sdk/explorer|c-sdk/platform|devices/esp8266|c-sdk/common|net/sal_module_wrapper|examples/fatfs_through_vfs_sample.c|User/mqtt_iot_explorer.c|examples/kv_sample.c|fs/fatfs|fs/vfs|fs/kv|components/kv|platform/hal/wch/ch32v30xx|kernel/hal|devices/esp8266_tencent_firmware|net/tencent_firmware_module_wrapper|kernel/core|net/at|kernel|arch|TencentOS-Tiny|Startup|Peripheral|Ld|Debug|Core" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="Core"/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="Debug"/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="Ld"/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="Peripheral"/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="Startup"/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="arch"/>
|
||||
<entry excluding="utils/test|mqtt_packet/test|cryptology/test" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="c-sdk/common"/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="c-sdk/include"/>
|
||||
<entry excluding="os/Linux" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="c-sdk/platform"/>
|
||||
<entry excluding="system/test|system/sample|ota/test|ota/sample|mqtt_client/test|mqtt_client/sample|log_upload/test|log_upload/sample|cos/sample|dynreg/test|dynreg/sample|dynreg/src/dynreg.c" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="c-sdk/services/common"/>
|
||||
<entry excluding="service_mqtt/test|service_mqtt/sample|data_template/test|data_template/sample" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="c-sdk/services/explorer"/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="devices/esp8266"/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="devices/esp8266_tencent_firmware"/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="fs/fatfs"/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="fs/kv"/>
|
||||
@@ -140,6 +162,7 @@
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="kernel/core"/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="kernel/hal"/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="net/at"/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="net/sal_module_wrapper"/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="net/tencent_firmware_module_wrapper"/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="platform/hal/wch/ch32v30xx"/>
|
||||
</sourceEntries>
|
||||
|
@@ -26,11 +26,46 @@
|
||||
<type>2</type>
|
||||
<locationURI>PARENT-2-PROJECT_LOC/arch/risc-v/risc-v3a</locationURI>
|
||||
</link>
|
||||
<link>
|
||||
<name>c-sdk/common</name>
|
||||
<type>2</type>
|
||||
<locationURI>PARENT-2-PROJECT_LOC/components/connectivity/iot-hub-device-c-sdk/common</locationURI>
|
||||
</link>
|
||||
<link>
|
||||
<name>c-sdk/include</name>
|
||||
<type>2</type>
|
||||
<locationURI>PARENT-2-PROJECT_LOC/components/connectivity/iot-hub-device-c-sdk/include</locationURI>
|
||||
</link>
|
||||
<link>
|
||||
<name>c-sdk/platform</name>
|
||||
<type>2</type>
|
||||
<locationURI>PARENT-2-PROJECT_LOC/components/connectivity/iot-hub-device-c-sdk/platform</locationURI>
|
||||
</link>
|
||||
<link>
|
||||
<name>devices/esp8266</name>
|
||||
<type>2</type>
|
||||
<locationURI>PARENT-2-PROJECT_LOC/devices/esp8266</locationURI>
|
||||
</link>
|
||||
<link>
|
||||
<name>devices/esp8266_tencent_firmware</name>
|
||||
<type>2</type>
|
||||
<locationURI>PARENT-2-PROJECT_LOC/devices/esp8266_tencent_firmware</locationURI>
|
||||
</link>
|
||||
<link>
|
||||
<name>fs/fatfs</name>
|
||||
<type>2</type>
|
||||
<locationURI>PARENT-2-PROJECT_LOC/components/fs/fatfs/wrapper</locationURI>
|
||||
</link>
|
||||
<link>
|
||||
<name>fs/kv</name>
|
||||
<type>2</type>
|
||||
<locationURI>PARENT-2-PROJECT_LOC/components/fs/kv</locationURI>
|
||||
</link>
|
||||
<link>
|
||||
<name>fs/vfs</name>
|
||||
<type>2</type>
|
||||
<locationURI>PARENT-2-PROJECT_LOC/components/fs/vfs</locationURI>
|
||||
</link>
|
||||
<link>
|
||||
<name>kernel/core</name>
|
||||
<type>2</type>
|
||||
@@ -46,6 +81,11 @@
|
||||
<type>2</type>
|
||||
<locationURI>PARENT-2-PROJECT_LOC/net/at</locationURI>
|
||||
</link>
|
||||
<link>
|
||||
<name>net/sal_module_wrapper</name>
|
||||
<type>2</type>
|
||||
<locationURI>PARENT-2-PROJECT_LOC/net/sal_module_wrapper</locationURI>
|
||||
</link>
|
||||
<link>
|
||||
<name>net/tencent_firmware_module_wrapper</name>
|
||||
<type>2</type>
|
||||
@@ -57,19 +97,14 @@
|
||||
<locationURI>PARENT-2-PROJECT_LOC/platform/hal/wch/ch32v30xx/src</locationURI>
|
||||
</link>
|
||||
<link>
|
||||
<name>fs/kv</name>
|
||||
<name>c-sdk/services/common</name>
|
||||
<type>2</type>
|
||||
<location>PARENT-2-PROJECT_LOC/components/fs/kv</location>
|
||||
<location>PARENT-2-PROJECT_LOC/components/connectivity/iot-hub-device-c-sdk/services/common</location>
|
||||
</link>
|
||||
<link>
|
||||
<name>fs/vfs</name>
|
||||
<name>c-sdk/services/explorer</name>
|
||||
<type>2</type>
|
||||
<location>PARENT-2-PROJECT_LOC/components/fs/vfs</location>
|
||||
</link>
|
||||
<link>
|
||||
<name>fs/fatfs</name>
|
||||
<type>2</type>
|
||||
<location>PARENT-2-PROJECT_LOC/components/fs/fatfs/wrapper</location>
|
||||
<location>PARENT-2-PROJECT_LOC/components/connectivity/iot-hub-device-c-sdk/services/explorer</location>
|
||||
</link>
|
||||
</linkedResources>
|
||||
<filteredResources>
|
||||
|
@@ -23,7 +23,7 @@
|
||||
|
||||
#define TOS_CFG_MMHEAP_DEFAULT_POOL_EN 1u
|
||||
|
||||
#define TOS_CFG_MMHEAP_DEFAULT_POOL_SIZE 0x3000
|
||||
#define TOS_CFG_MMHEAP_DEFAULT_POOL_SIZE 0x8000
|
||||
|
||||
#define TOS_CFG_MAIL_QUEUE_EN 1u
|
||||
|
||||
|
@@ -9,6 +9,7 @@
|
||||
#include "tos_k.h"
|
||||
#include "tos_at.h"
|
||||
extern at_agent_t esp8266_tf_agent;
|
||||
extern at_agent_t esp8266_agent;
|
||||
|
||||
void NMI_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||
void HardFault_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
|
||||
@@ -76,7 +77,7 @@ void UART6_IRQHandler(void)
|
||||
if(USART_GetITStatus(UART6, USART_IT_RXNE) != RESET)
|
||||
{
|
||||
data= USART_ReceiveData(UART6);
|
||||
tos_at_uart_input_byte(&esp8266_tf_agent,data);
|
||||
tos_at_uart_input_byte(&esp8266_agent,data);
|
||||
|
||||
}
|
||||
|
||||
|
@@ -223,40 +223,12 @@ void SD_Card_test(void)
|
||||
}
|
||||
}
|
||||
|
||||
#define TASK1_STK_SIZE 1024
|
||||
k_task_t task1;
|
||||
__aligned(4) uint8_t task1_stk[TASK1_STK_SIZE];
|
||||
|
||||
|
||||
#define TASK2_STK_SIZE 1024
|
||||
k_task_t task2;
|
||||
__aligned(4) uint8_t task2_stk[TASK2_STK_SIZE];
|
||||
|
||||
|
||||
#define APPLICATION_TASK_STK_SIZE 4096
|
||||
k_task_t application_task;
|
||||
__aligned(4) uint8_t application_task_stk[APPLICATION_TASK_STK_SIZE];
|
||||
|
||||
extern void application_entry(void *arg);
|
||||
|
||||
void task1_entry(void *arg)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
printf("###I am task1\r\n");
|
||||
tos_task_delay(2000);
|
||||
}
|
||||
}
|
||||
|
||||
void task2_entry(void *arg)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
printf("***I am task2\r\n");
|
||||
tos_task_delay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
volatile uint8_t key=0;
|
||||
/*******************************************************************************
|
||||
* Function Name : main
|
||||
@@ -270,17 +242,14 @@ int main(void)
|
||||
USART_Printf_Init(115200);
|
||||
printf("SystemClk:%d\r\n",SystemCoreClock);
|
||||
led_key_init();
|
||||
LCD_Init();//LCD<43><44>ʼ<EFBFBD><CABC>
|
||||
LCD_Init();
|
||||
SPI_Flash_Init();
|
||||
printf("Welcome to TencentOS tiny(%s)\r\n", TOS_VERSION);
|
||||
LCD_Fill(0,0,LCD_W,LCD_H,BLACK);
|
||||
LCD_ShowString(0,0,"Welcome to TencentOS",WHITE,BLACK,16,0);
|
||||
|
||||
|
||||
tos_knl_init();
|
||||
tos_task_create(&application_task, "application_task", application_entry, NULL, 4, application_task_stk, APPLICATION_TASK_STK_SIZE, 0);
|
||||
//tos_task_create(&task1, "task1", task1_entry, NULL, 3, task1_stk, TASK1_STK_SIZE, 0); // Create task1
|
||||
//tos_task_create(&task2, "task2", task2_entry, NULL, 3, task2_stk, TASK2_STK_SIZE, 0);// Create task2
|
||||
tos_knl_start();
|
||||
|
||||
printf("should not run at here!\r\n");
|
||||
|
34
board/TencentOS_Tiny_CH32V307_EVB/examples/hello_world.c
Normal file
34
board/TencentOS_Tiny_CH32V307_EVB/examples/hello_world.c
Normal file
@@ -0,0 +1,34 @@
|
||||
#include "tos_k.h"
|
||||
|
||||
#define TASK1_STK_SIZE 1024
|
||||
k_task_t task1;
|
||||
uint8_t task1_stk[TASK1_STK_SIZE];
|
||||
|
||||
|
||||
#define TASK2_STK_SIZE 1024
|
||||
k_task_t task2;
|
||||
uint8_t task2_stk[TASK2_STK_SIZE];
|
||||
|
||||
void task1_entry(void *arg)
|
||||
{
|
||||
while (1) {
|
||||
printf("###I am task1\r\n");
|
||||
tos_task_delay(2000);
|
||||
}
|
||||
}
|
||||
|
||||
void task2_entry(void *arg)
|
||||
{
|
||||
while (1) {
|
||||
printf("***I am task2\r\n");
|
||||
tos_task_delay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void application_entry(void *arg)
|
||||
{
|
||||
tos_task_create(&task1, "task1", task1_entry, NULL, 3, task1_stk, TASK1_STK_SIZE, 0); // Create task1
|
||||
tos_task_create(&task2, "task2", task2_entry, NULL, 3, task2_stk, TASK2_STK_SIZE, 0);// Create task2
|
||||
}
|
||||
|
@@ -0,0 +1,326 @@
|
||||
/**
|
||||
* @copyright
|
||||
*
|
||||
* Tencent is pleased to support the open source community by making IoT Hub available.
|
||||
* Copyright(C) 2018 - 2021 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.
|
||||
*
|
||||
* @file data_template_sample.c
|
||||
* @brief
|
||||
* @author fancyxu (fancyxu@tencent.com)
|
||||
* @version 1.0
|
||||
* @date 2021-09-27
|
||||
*
|
||||
* @par Change Log:
|
||||
* <table>
|
||||
* <tr><th>Date <th>Version <th>Author <th>Description
|
||||
* <tr><td>2021-09-27 <td>1.0 <td>fancyxu <td>first commit
|
||||
* </table>
|
||||
*/
|
||||
|
||||
#include "qcloud_iot_common.h"
|
||||
#include "qcloud_iot_explorer.h"
|
||||
#include "data_template_config.h"
|
||||
|
||||
#include "utils_log.h"
|
||||
|
||||
#include "esp8266.h"
|
||||
|
||||
/**
|
||||
* @brief MQTT event callback, @see MQTTEventHandleFun
|
||||
*
|
||||
* @param[in] client pointer to mqtt client
|
||||
* @param[in] handle_context context
|
||||
* @param[in] msg msg
|
||||
*/
|
||||
static void _mqtt_event_handler(void *client, void *handle_context, MQTTEventMsg *msg)
|
||||
{
|
||||
MQTTMessage *mqtt_message = (MQTTMessage *)msg->msg;
|
||||
uintptr_t packet_id = (uintptr_t)msg->msg;
|
||||
|
||||
switch (msg->event_type) {
|
||||
case MQTT_EVENT_UNDEF:
|
||||
Log_i("undefined event occur.");
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_DISCONNECT:
|
||||
Log_i("MQTT disconnect.");
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_RECONNECT:
|
||||
Log_i("MQTT reconnect.");
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_PUBLISH_RECEIVED:
|
||||
Log_i("topic message arrived but without any related handle: topic=%.*s, topic_msg=%.*s",
|
||||
mqtt_message->topic_len, STRING_PTR_PRINT_SANITY_CHECK(mqtt_message->topic_name),
|
||||
mqtt_message->payload_len, STRING_PTR_PRINT_SANITY_CHECK((char *)mqtt_message->payload));
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_SUBSCRIBE_SUCCESS:
|
||||
Log_i("subscribe success, packet-id=%u", (unsigned int)packet_id);
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_SUBSCRIBE_TIMEOUT:
|
||||
Log_i("subscribe wait ack timeout, packet-id=%u", (unsigned int)packet_id);
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_SUBSCRIBE_NACK:
|
||||
Log_i("subscribe nack, packet-id=%u", (unsigned int)packet_id);
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_UNSUBSCRIBE_SUCCESS:
|
||||
Log_i("unsubscribe success, packet-id=%u", (unsigned int)packet_id);
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_UNSUBSCRIBE_TIMEOUT:
|
||||
Log_i("unsubscribe timeout, packet-id=%u", (unsigned int)packet_id);
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_UNSUBSCRIBE_NACK:
|
||||
Log_i("unsubscribe nack, packet-id=%u", (unsigned int)packet_id);
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_PUBLISH_SUCCESS:
|
||||
Log_i("publish success, packet-id=%u", (unsigned int)packet_id);
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_PUBLISH_TIMEOUT:
|
||||
Log_i("publish timeout, packet-id=%u", (unsigned int)packet_id);
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_PUBLISH_NACK:
|
||||
Log_i("publish nack, packet-id=%u", (unsigned int)packet_id);
|
||||
break;
|
||||
|
||||
default:
|
||||
Log_i("Should NOT arrive here.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Setup MQTT construct parameters.
|
||||
*
|
||||
* @param[in,out] initParams @see MQTTInitParams
|
||||
* @param[in] device_info @see DeviceInfo
|
||||
*/
|
||||
static void _setup_connect_init_params(MQTTInitParams *init_params, DeviceInfo *device_info)
|
||||
{
|
||||
init_params->device_info = device_info;
|
||||
init_params->command_timeout = QCLOUD_IOT_MQTT_COMMAND_TIMEOUT;
|
||||
init_params->keep_alive_interval_ms = QCLOUD_IOT_MQTT_KEEP_ALIVE_INTERNAL;
|
||||
init_params->auto_connect_enable = 1;
|
||||
init_params->event_handle.h_fp = _mqtt_event_handler;
|
||||
init_params->event_handle.context = NULL;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Data template callback
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
static void _method_control_callback(UtilsJsonValue client_token, UtilsJsonValue params, void *usr_data)
|
||||
{
|
||||
char buf[256];
|
||||
Log_i("recv msg[%.*s]: params=%.*s", client_token.value_len, client_token.value, params.value_len, params.value);
|
||||
IOT_DataTemplate_PropertyControlReply(usr_data, buf, sizeof(buf), 0, client_token);
|
||||
usr_data_template_property_parse(params);
|
||||
}
|
||||
|
||||
static void _method_get_status_reply_callback(UtilsJsonValue client_token, int code, UtilsJsonValue reported,
|
||||
UtilsJsonValue control, void *usr_data)
|
||||
{
|
||||
char buf[256];
|
||||
Log_i("recv msg[%.*s]: code=%d|reported=%.*s|control=%.*s", client_token.value_len, client_token.value, code,
|
||||
reported.value_len, STRING_PTR_PRINT_SANITY_CHECK(reported.value), control.value_len,
|
||||
STRING_PTR_PRINT_SANITY_CHECK(control.value));
|
||||
usr_data_template_property_parse(control);
|
||||
IOT_DataTemplate_PropertyClearControl(usr_data, buf, sizeof(buf));
|
||||
}
|
||||
|
||||
static void _method_action_callback(UtilsJsonValue client_token, UtilsJsonValue action_id, UtilsJsonValue params,
|
||||
void *usr_data)
|
||||
{
|
||||
UsrActionIndex index;
|
||||
int rc;
|
||||
DataTemplatePropertyValue value_time, value_color, value_total_time;
|
||||
|
||||
char buf[256];
|
||||
|
||||
Log_i("recv msg[%.*s]: action_id=%.*s|params=%.*s", client_token.value_len, client_token.value, action_id.value_len,
|
||||
action_id.value, params.value_len, params.value);
|
||||
|
||||
rc = usr_data_template_action_parse(action_id, params, &index);
|
||||
if (rc) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (index) {
|
||||
case USR_ACTION_INDEX_LIGHT_BLINK:
|
||||
value_time = usr_data_template_action_input_value_get(USR_ACTION_INDEX_LIGHT_BLINK,
|
||||
USR_ACTION_LIGHT_BLINK_INPUT_INDEX_TIME);
|
||||
value_color = usr_data_template_action_input_value_get(USR_ACTION_INDEX_LIGHT_BLINK,
|
||||
USR_ACTION_LIGHT_BLINK_INPUT_INDEX_COLOR);
|
||||
value_total_time = usr_data_template_action_input_value_get(USR_ACTION_INDEX_LIGHT_BLINK,
|
||||
USR_ACTION_LIGHT_BLINK_INPUT_INDEX_TOTAL_TIME);
|
||||
Log_i("light[%d] blink %d every %d s ", value_color.value_enum, value_time.value_int,
|
||||
value_total_time.value_int);
|
||||
usr_data_template_action_reply(usr_data, buf, sizeof(buf), index, client_token, 0, "{\"err_code\":0}");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Data template upstream
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* @brief Report status.
|
||||
*
|
||||
* @param[in,out] client pointer to mqtt client
|
||||
*/
|
||||
static void _cycle_report(void *client)
|
||||
{
|
||||
static int lightness = 0;
|
||||
char buf[256];
|
||||
static Timer sg_cycle_report_timer;
|
||||
DataTemplatePropertyValue property_value;
|
||||
|
||||
if (HAL_Timer_Expired(&sg_cycle_report_timer)) {
|
||||
usr_data_template_event_post(client, buf, sizeof(buf), USR_EVENT_INDEX_STATUS_REPORT,
|
||||
"{\"status\":0,\"message\":\"ok\"}");
|
||||
|
||||
property_value.value_int = lightness;
|
||||
if (++lightness > 100) {
|
||||
lightness = 0;
|
||||
}
|
||||
usr_data_template_property_value_set(USR_PROPERTY_INDEX_BRIGHTNESS, property_value);
|
||||
HAL_Timer_Countdown(&sg_cycle_report_timer, 10);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Init usr data template and data.
|
||||
*
|
||||
*/
|
||||
static void _usr_init(void)
|
||||
{
|
||||
usr_data_template_init();
|
||||
|
||||
DataTemplatePropertyValue value;
|
||||
value.value_int = 0;
|
||||
usr_data_template_property_value_set(USR_PROPERTY_INDEX_POWER_SWITCH, value);
|
||||
value.value_enum = 0;
|
||||
usr_data_template_property_value_set(USR_PROPERTY_INDEX_COLOR, value);
|
||||
value.value_int = 10;
|
||||
usr_data_template_property_value_set(USR_PROPERTY_INDEX_BRIGHTNESS, value);
|
||||
value.value_string = "light";
|
||||
usr_data_template_property_value_set(USR_PROPERTY_INDEX_NAME, value);
|
||||
value.value_int = 30;
|
||||
usr_data_template_property_struct_value_set(USR_PROPERTY_INDEX_POSITION, USR_PROPERTY_POSITION_INDEX_LONGITUDE,
|
||||
value);
|
||||
value.value_int = 30;
|
||||
usr_data_template_property_struct_value_set(USR_PROPERTY_INDEX_POSITION, USR_PROPERTY_POSITION_INDEX_LATITUDE,
|
||||
value);
|
||||
value.value_string = "high";
|
||||
usr_data_template_property_value_set(USR_PROPERTY_INDEX_POWER, value);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Main
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
int data_template_app()
|
||||
{
|
||||
int rc;
|
||||
char buf[1024];
|
||||
|
||||
// init log level
|
||||
LogHandleFunc func = {0};
|
||||
|
||||
func.log_malloc = HAL_Malloc;
|
||||
func.log_free = HAL_Free;
|
||||
func.log_get_current_time_str = HAL_Timer_Current;
|
||||
func.log_printf = HAL_Printf;
|
||||
utils_log_init(func, LOG_LEVEL_INFO, 2048);
|
||||
|
||||
DeviceInfo device_info;
|
||||
|
||||
rc = HAL_GetDevInfo((void *)&device_info);
|
||||
if (rc) {
|
||||
Log_e("get device info failed: %d", rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
_usr_init();
|
||||
|
||||
// init connection
|
||||
MQTTInitParams init_params = DEFAULT_MQTT_INIT_PARAMS;
|
||||
_setup_connect_init_params(&init_params, &device_info);
|
||||
|
||||
// create MQTT client and connect with server
|
||||
void *client = IOT_MQTT_Construct(&init_params);
|
||||
if (client) {
|
||||
Log_i("Cloud Device Construct Success");
|
||||
} else {
|
||||
Log_e("MQTT Construct failed!");
|
||||
return QCLOUD_ERR_FAILURE;
|
||||
}
|
||||
|
||||
// subscribe normal topics and wait result
|
||||
IotDataTemplateCallback callback = DEFAULT_DATA_TEMPLATE_CALLBACK;
|
||||
callback.property_callback.method_control_callback = _method_control_callback;
|
||||
callback.property_callback.method_get_status_reply_callback = _method_get_status_reply_callback;
|
||||
callback.action_callback.method_action_callback = _method_action_callback;
|
||||
|
||||
rc = IOT_DataTemplate_Init(client, callback, client);
|
||||
if (rc) {
|
||||
Log_e("Client Subscribe Topic Failed: %d", rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
IOT_DataTemplate_PropertyGetStatus(client, buf, sizeof(buf));
|
||||
|
||||
while (1) {
|
||||
rc = IOT_MQTT_Yield(client, QCLOUD_IOT_MQTT_YIELD_TIMEOUT);
|
||||
switch (rc) {
|
||||
case QCLOUD_RET_SUCCESS:
|
||||
break;
|
||||
case QCLOUD_ERR_MQTT_ATTEMPTING_RECONNECT:
|
||||
continue;
|
||||
case QCLOUD_RET_MQTT_RECONNECTED:
|
||||
IOT_DataTemplate_PropertyGetStatus(client, buf, sizeof(buf));
|
||||
break;
|
||||
default:
|
||||
Log_e("Exit loop caused of errCode:%d", rc);
|
||||
goto exit;
|
||||
}
|
||||
_cycle_report(client);
|
||||
usr_data_template_property_report(client, buf, sizeof(buf));
|
||||
}
|
||||
|
||||
exit:
|
||||
IOT_DataTemplate_Deinit(client);
|
||||
rc |= IOT_MQTT_Destroy(&client);
|
||||
utils_log_deinit();
|
||||
return rc;
|
||||
}
|
||||
|
||||
void application_entry(void *arg)
|
||||
{
|
||||
esp8266_sal_init(HAL_UART_PORT_6);
|
||||
|
||||
esp8266_join_ap("Mculover666", "mculover666");
|
||||
|
||||
data_template_app();
|
||||
}
|
@@ -0,0 +1,494 @@
|
||||
/**
|
||||
* @copyright
|
||||
*
|
||||
* Tencent is pleased to support the open source community by making IoT Hub available.
|
||||
* Copyright(C) 2018 - 2021 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.
|
||||
*
|
||||
* @file data_template_config.c
|
||||
* @brief
|
||||
* @author fancyxu (fancyxu@tencent.com)
|
||||
* @version 1.0
|
||||
* @date 2021-10-09
|
||||
*
|
||||
* @par Change Log:
|
||||
* <table>
|
||||
* <tr><th>Date <th>Version <th>Author <th>Description
|
||||
* <tr><td>2021-10-09 <td>1.0 <td>fancyxu <td>first commit
|
||||
* </table>
|
||||
*/
|
||||
|
||||
#include "data_template_config.h"
|
||||
|
||||
/**
|
||||
* @brief Set property value.
|
||||
*
|
||||
* @param[in,out] property pointer to property
|
||||
* @param[in] value value to set, should match with property type
|
||||
* @return 0 for success.
|
||||
*/
|
||||
static int _set_property_value(DataTemplateProperty* property, UtilsJsonValue value)
|
||||
{
|
||||
int i, rc = 0;
|
||||
|
||||
switch (property->type) {
|
||||
case DATA_TEMPLATE_TYPE_INT:
|
||||
case DATA_TEMPLATE_TYPE_ENUM:
|
||||
case DATA_TEMPLATE_TYPE_BOOL:
|
||||
return utils_json_value_data_get(value, UTILS_JSON_VALUE_TYPE_INT32, &property->value.value_int);
|
||||
case DATA_TEMPLATE_TYPE_TIME:
|
||||
return utils_json_value_data_get(value, UTILS_JSON_VALUE_TYPE_UINT32, &property->value.value_time);
|
||||
case DATA_TEMPLATE_TYPE_STRING:
|
||||
case DATA_TEMPLATE_TYPE_STRING_ENUM:
|
||||
if (!property->value.value_string) { // no need copy
|
||||
return 0;
|
||||
}
|
||||
strncpy(property->value.value_string, value.value, value.value_len);
|
||||
property->value.value_string[value.value_len] = '\0';
|
||||
return 0;
|
||||
case DATA_TEMPLATE_TYPE_FLOAT:
|
||||
return utils_json_value_data_get(value, UTILS_JSON_VALUE_TYPE_FLOAT, &property->value.value_float);
|
||||
case DATA_TEMPLATE_TYPE_STRUCT:
|
||||
for (i = 0; i < property->value.value_struct.count; i++) {
|
||||
rc |= _set_property_value(property->value.value_struct.property + i, value);
|
||||
}
|
||||
return rc;
|
||||
case DATA_TEMPLATE_TYPE_ARRAY:
|
||||
Log_e("array type is not supportted yet!");
|
||||
return -1;
|
||||
default:
|
||||
Log_e("unkown type!");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get property node in json.
|
||||
*
|
||||
* @param[out] json_buf buffer to put node
|
||||
* @param[in] buf_len buffer length
|
||||
* @param[in] property property to get node
|
||||
* @return 0 for success.
|
||||
*/
|
||||
static int _get_property_node(char* json_buf, int buf_len, const DataTemplateProperty* property)
|
||||
{
|
||||
int len, i;
|
||||
switch (property->type) {
|
||||
case DATA_TEMPLATE_TYPE_INT:
|
||||
case DATA_TEMPLATE_TYPE_ENUM:
|
||||
case DATA_TEMPLATE_TYPE_BOOL:
|
||||
return HAL_Snprintf(json_buf, buf_len, "\"%s\":%d", property->key, property->value.value_int);
|
||||
case DATA_TEMPLATE_TYPE_TIME:
|
||||
return HAL_Snprintf(json_buf, buf_len, "\"%s\":%u", property->key, property->value.value_time);
|
||||
case DATA_TEMPLATE_TYPE_STRING:
|
||||
case DATA_TEMPLATE_TYPE_STRING_ENUM:
|
||||
if (!property->value.value_string) {
|
||||
return 0;
|
||||
}
|
||||
return HAL_Snprintf(json_buf, buf_len, "\"%s\":\"%s\"", property->key, property->value.value_string);
|
||||
case DATA_TEMPLATE_TYPE_FLOAT:
|
||||
return HAL_Snprintf(json_buf, buf_len, "\"%s\":%f", property->key, property->value.value_float);
|
||||
case DATA_TEMPLATE_TYPE_STRUCT:
|
||||
len = HAL_Snprintf(json_buf, buf_len, "\"%s\":{", property->key);
|
||||
for (i = 0; i < property->value.value_struct.count; i++) {
|
||||
len += _get_property_node(json_buf + len, buf_len - len, property->value.value_struct.property + i);
|
||||
json_buf[len++] = ',';
|
||||
}
|
||||
json_buf[--len] = '}';
|
||||
return len + 1;
|
||||
case DATA_TEMPLATE_TYPE_ARRAY:
|
||||
Log_e("array type is not supportted yet!");
|
||||
return -1;
|
||||
default:
|
||||
Log_e("unkown type!");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Parse property array.
|
||||
*
|
||||
* @param[in] json_buf json string to parse
|
||||
* @param[in] buf_len json len
|
||||
* @param[in,out] properties pointer to property array
|
||||
* @param[in] property_count count of property
|
||||
*/
|
||||
static void _parse_property_array(const char* json_buf, int buf_len, DataTemplateProperty* properties,
|
||||
int property_count)
|
||||
{
|
||||
DataTemplateProperty* property;
|
||||
UtilsJsonValue value;
|
||||
for (int i = 0; i < property_count; i++) {
|
||||
property = &properties[i];
|
||||
if (!utils_json_value_get(property->key, strlen(property->key), json_buf, buf_len, &value)) {
|
||||
property->need_report = !_set_property_value(property, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**************************************************************************************
|
||||
* user property
|
||||
**************************************************************************************/
|
||||
|
||||
#define TOTAL_USR_PROPERTY_COUNT 6
|
||||
|
||||
static DataTemplateProperty sg_usr_data_template_property[TOTAL_USR_PROPERTY_COUNT];
|
||||
|
||||
#define TOTAL_USR_PROPERTY_STRUCT_POSITION_COUNT 2
|
||||
|
||||
static DataTemplateProperty sg_usr_property_position[TOTAL_USR_PROPERTY_STRUCT_POSITION_COUNT];
|
||||
|
||||
static void _init_data_template_property_position(void)
|
||||
{
|
||||
sg_usr_property_position[USR_PROPERTY_POSITION_INDEX_LONGITUDE].value.value_int = 0;
|
||||
sg_usr_property_position[USR_PROPERTY_POSITION_INDEX_LONGITUDE].key = "longitude";
|
||||
sg_usr_property_position[USR_PROPERTY_POSITION_INDEX_LONGITUDE].type = DATA_TEMPLATE_TYPE_INT;
|
||||
|
||||
sg_usr_property_position[USR_PROPERTY_POSITION_INDEX_LATITUDE].value.value_int = 0;
|
||||
sg_usr_property_position[USR_PROPERTY_POSITION_INDEX_LATITUDE].key = "latitude";
|
||||
sg_usr_property_position[USR_PROPERTY_POSITION_INDEX_LATITUDE].type = DATA_TEMPLATE_TYPE_INT;
|
||||
}
|
||||
|
||||
static void _init_data_template_property(void)
|
||||
{
|
||||
sg_usr_data_template_property[USR_PROPERTY_INDEX_POWER_SWITCH].value.value_bool = 0;
|
||||
sg_usr_data_template_property[USR_PROPERTY_INDEX_POWER_SWITCH].key = "power_switch";
|
||||
sg_usr_data_template_property[USR_PROPERTY_INDEX_POWER_SWITCH].type = DATA_TEMPLATE_TYPE_BOOL;
|
||||
sg_usr_data_template_property[USR_PROPERTY_INDEX_POWER_SWITCH].need_report = 1;
|
||||
|
||||
sg_usr_data_template_property[USR_PROPERTY_INDEX_COLOR].value.value_enum = 0;
|
||||
sg_usr_data_template_property[USR_PROPERTY_INDEX_COLOR].key = "color";
|
||||
sg_usr_data_template_property[USR_PROPERTY_INDEX_COLOR].type = DATA_TEMPLATE_TYPE_ENUM;
|
||||
sg_usr_data_template_property[USR_PROPERTY_INDEX_COLOR].need_report = 1;
|
||||
|
||||
sg_usr_data_template_property[USR_PROPERTY_INDEX_BRIGHTNESS].value.value_int = 0;
|
||||
sg_usr_data_template_property[USR_PROPERTY_INDEX_BRIGHTNESS].key = "brightness";
|
||||
sg_usr_data_template_property[USR_PROPERTY_INDEX_BRIGHTNESS].type = DATA_TEMPLATE_TYPE_INT;
|
||||
sg_usr_data_template_property[USR_PROPERTY_INDEX_BRIGHTNESS].need_report = 1;
|
||||
|
||||
static char sg_usr_property_name[64 + 1];
|
||||
sg_usr_data_template_property[USR_PROPERTY_INDEX_NAME].value.value_string = sg_usr_property_name;
|
||||
sg_usr_data_template_property[USR_PROPERTY_INDEX_NAME].key = "name";
|
||||
sg_usr_data_template_property[USR_PROPERTY_INDEX_NAME].type = DATA_TEMPLATE_TYPE_STRING;
|
||||
sg_usr_data_template_property[USR_PROPERTY_INDEX_NAME].need_report = 1;
|
||||
|
||||
_init_data_template_property_position();
|
||||
sg_usr_data_template_property[USR_PROPERTY_INDEX_POSITION].value.value_struct.property = sg_usr_property_position;
|
||||
sg_usr_data_template_property[USR_PROPERTY_INDEX_POSITION].value.value_struct.count =
|
||||
TOTAL_USR_PROPERTY_STRUCT_POSITION_COUNT;
|
||||
sg_usr_data_template_property[USR_PROPERTY_INDEX_POSITION].key = "position";
|
||||
sg_usr_data_template_property[USR_PROPERTY_INDEX_POSITION].type = DATA_TEMPLATE_TYPE_STRUCT;
|
||||
sg_usr_data_template_property[USR_PROPERTY_INDEX_POSITION].need_report = 1;
|
||||
|
||||
static char sg_usr_property_power[64 + 1];
|
||||
sg_usr_data_template_property[USR_PROPERTY_INDEX_POWER].value.value_string_enum = sg_usr_property_power;
|
||||
sg_usr_data_template_property[USR_PROPERTY_INDEX_POWER].key = "power";
|
||||
sg_usr_data_template_property[USR_PROPERTY_INDEX_POWER].type = DATA_TEMPLATE_TYPE_STRING_ENUM;
|
||||
sg_usr_data_template_property[USR_PROPERTY_INDEX_POWER].need_report = 1;
|
||||
}
|
||||
|
||||
/**************************************************************************************
|
||||
* user event
|
||||
**************************************************************************************/
|
||||
|
||||
#define TOTAL_USR_EVENT_COUNT 3
|
||||
|
||||
DataTemplateEvent sg_usr_data_template_event[TOTAL_USR_EVENT_COUNT];
|
||||
|
||||
/**
|
||||
* @brief Sample of event post params.
|
||||
*
|
||||
*/
|
||||
static const char* sg_usr_event_status_report_params = "{\"status\":0,\"message\":\"ok\"}";
|
||||
static const char* sg_usr_event_low_voltage_params = "{\"voltage\":1.0}";
|
||||
static const char* sg_usr_event_hardware_fault_params = "{\"name\":\"broken\",\"error_code\":-1}";
|
||||
|
||||
static void _init_data_template_event(void)
|
||||
{
|
||||
sg_usr_data_template_event[USR_EVENT_INDEX_STATUS_REPORT].event_id = "status_report";
|
||||
sg_usr_data_template_event[USR_EVENT_INDEX_STATUS_REPORT].type = IOT_DATA_TEMPLATE_EVENT_TYPE_INFO;
|
||||
sg_usr_data_template_event[USR_EVENT_INDEX_STATUS_REPORT].params = sg_usr_event_status_report_params;
|
||||
|
||||
sg_usr_data_template_event[USR_EVENT_INDEX_LOW_VOLTAGE].event_id = "low_voltage";
|
||||
sg_usr_data_template_event[USR_EVENT_INDEX_LOW_VOLTAGE].type = IOT_DATA_TEMPLATE_EVENT_TYPE_ALERT;
|
||||
sg_usr_data_template_event[USR_EVENT_INDEX_LOW_VOLTAGE].params = sg_usr_event_low_voltage_params;
|
||||
|
||||
sg_usr_data_template_event[USR_EVENT_INDEX_HARDWARE_FAULT].event_id = "hardware_fault";
|
||||
sg_usr_data_template_event[USR_EVENT_INDEX_HARDWARE_FAULT].type = IOT_DATA_TEMPLATE_EVENT_TYPE_FAULT;
|
||||
sg_usr_data_template_event[USR_EVENT_INDEX_HARDWARE_FAULT].params = sg_usr_event_hardware_fault_params;
|
||||
}
|
||||
|
||||
/**************************************************************************************
|
||||
* user action
|
||||
**************************************************************************************/
|
||||
|
||||
#define TOTAL_USR_ACTION_COUNT 1
|
||||
|
||||
DataTemplateAction sg_usr_data_template_action[TOTAL_USR_ACTION_COUNT];
|
||||
|
||||
#define TOTAL_USR_ACTION_LIGHT_BLINK_INPUT_PARAMS_COUNT 3
|
||||
|
||||
static DataTemplateProperty sg_usr_action_light_blink_input[TOTAL_USR_ACTION_LIGHT_BLINK_INPUT_PARAMS_COUNT];
|
||||
|
||||
static void _init_data_template_action_light_blink_input(void)
|
||||
{
|
||||
sg_usr_action_light_blink_input[USR_ACTION_LIGHT_BLINK_INPUT_INDEX_TIME].value.value_int = 0;
|
||||
sg_usr_action_light_blink_input[USR_ACTION_LIGHT_BLINK_INPUT_INDEX_TIME].key = "time";
|
||||
sg_usr_action_light_blink_input[USR_ACTION_LIGHT_BLINK_INPUT_INDEX_TIME].type = DATA_TEMPLATE_TYPE_INT;
|
||||
|
||||
sg_usr_action_light_blink_input[USR_ACTION_LIGHT_BLINK_INPUT_INDEX_COLOR].value.value_int = 0;
|
||||
sg_usr_action_light_blink_input[USR_ACTION_LIGHT_BLINK_INPUT_INDEX_COLOR].key = "color";
|
||||
sg_usr_action_light_blink_input[USR_ACTION_LIGHT_BLINK_INPUT_INDEX_COLOR].type = DATA_TEMPLATE_TYPE_ENUM;
|
||||
|
||||
sg_usr_action_light_blink_input[USR_ACTION_LIGHT_BLINK_INPUT_INDEX_TOTAL_TIME].value.value_int = 0;
|
||||
sg_usr_action_light_blink_input[USR_ACTION_LIGHT_BLINK_INPUT_INDEX_TOTAL_TIME].key = "total_time";
|
||||
sg_usr_action_light_blink_input[USR_ACTION_LIGHT_BLINK_INPUT_INDEX_TOTAL_TIME].type = DATA_TEMPLATE_TYPE_INT;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Sample of action reply.
|
||||
*
|
||||
*/
|
||||
static IotDataTemplateActionReply sg_usr_action_light_blink_reply = {
|
||||
.code = 0,
|
||||
.client_token = {.value = "test", .value_len = 4},
|
||||
.response = "{\"err_code\":0}",
|
||||
};
|
||||
|
||||
static void _init_data_template_action(void)
|
||||
{
|
||||
_init_data_template_action_light_blink_input();
|
||||
sg_usr_data_template_action[USR_ACTION_INDEX_LIGHT_BLINK].action_id = "light_blink";
|
||||
sg_usr_data_template_action[USR_ACTION_INDEX_LIGHT_BLINK].input_struct.value_struct.property =
|
||||
sg_usr_action_light_blink_input;
|
||||
sg_usr_data_template_action[USR_ACTION_INDEX_LIGHT_BLINK].input_struct.value_struct.count =
|
||||
TOTAL_USR_ACTION_LIGHT_BLINK_INPUT_PARAMS_COUNT;
|
||||
sg_usr_data_template_action[USR_ACTION_INDEX_LIGHT_BLINK].reply = sg_usr_action_light_blink_reply;
|
||||
}
|
||||
|
||||
/**************************************************************************************
|
||||
* API
|
||||
**************************************************************************************/
|
||||
|
||||
/**
|
||||
* @brief Init user data template(property/event/action).
|
||||
*
|
||||
*/
|
||||
void usr_data_template_init(void)
|
||||
{
|
||||
_init_data_template_property();
|
||||
_init_data_template_event();
|
||||
_init_data_template_action();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get property value.
|
||||
*
|
||||
* @param[in] index @see UsrPropertyIndex
|
||||
* @return @see DataTemplatePropertyValue
|
||||
*/
|
||||
DataTemplatePropertyValue usr_data_template_property_value_get(UsrPropertyIndex index)
|
||||
{
|
||||
return sg_usr_data_template_property[index].value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set property value.
|
||||
*
|
||||
* @param[in] index @see UsrPropertyIndex
|
||||
* @param[in] value @see DataTemplatePropertyValue, @note value should match property type.
|
||||
*/
|
||||
void usr_data_template_property_value_set(UsrPropertyIndex index, DataTemplatePropertyValue value)
|
||||
{
|
||||
if (sg_usr_data_template_property[index].type == DATA_TEMPLATE_TYPE_STRING ||
|
||||
sg_usr_data_template_property[index].type == DATA_TEMPLATE_TYPE_STRING_ENUM) {
|
||||
strncpy(sg_usr_data_template_property[index].value.value_string, value.value_string,
|
||||
strlen(value.value_string));
|
||||
}
|
||||
sg_usr_data_template_property[index].value = value;
|
||||
sg_usr_data_template_property[index].need_report = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get property(struct) value.
|
||||
*
|
||||
* @param[in] struct_index @see UsrPropertyIndex, @note DATA_TEMPLATE_TYPE_STRUCT is required here.
|
||||
* @param[in] property_index depends on which struct
|
||||
* @return @see DataTemplatePropertyValue
|
||||
*/
|
||||
DataTemplatePropertyValue usr_data_template_property_struct_value_get(UsrPropertyIndex struct_index, int property_index)
|
||||
{
|
||||
return sg_usr_data_template_property[struct_index].value.value_struct.property[property_index].value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set property(struct) value.
|
||||
*
|
||||
* @param[in] struct_index @see UsrPropertyIndex, @note DATA_TEMPLATE_TYPE_STRUCT is required here.
|
||||
* @param[in] property_index depends on which struct
|
||||
* @param[in] value @see DataTemplatePropertyValue, @note value should match property type.
|
||||
*/
|
||||
void usr_data_template_property_struct_value_set(UsrPropertyIndex struct_index, int property_index,
|
||||
DataTemplatePropertyValue value)
|
||||
{
|
||||
if (sg_usr_data_template_property[struct_index].value.value_struct.property[property_index].type ==
|
||||
DATA_TEMPLATE_TYPE_STRING ||
|
||||
sg_usr_data_template_property[struct_index].value.value_struct.property[property_index].type ==
|
||||
DATA_TEMPLATE_TYPE_STRING_ENUM) {
|
||||
strncpy(
|
||||
sg_usr_data_template_property[struct_index].value.value_struct.property[property_index].value.value_string,
|
||||
value.value_string, strlen(value.value_string));
|
||||
}
|
||||
sg_usr_data_template_property[struct_index].need_report = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Parse control message and set property value.
|
||||
*
|
||||
* @param[in] params params filed of control message
|
||||
*/
|
||||
void usr_data_template_property_parse(UtilsJsonValue params)
|
||||
{
|
||||
_parse_property_array(params.value, params.value_len, sg_usr_data_template_property, TOTAL_USR_PROPERTY_COUNT);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get property status.
|
||||
*
|
||||
* @param[in] index @see UsrPropertyIndex
|
||||
* @return need_report
|
||||
*/
|
||||
int usr_data_template_property_status_get(UsrPropertyIndex index)
|
||||
{
|
||||
return sg_usr_data_template_property[index].need_report;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Report all the properties needed report.
|
||||
*
|
||||
* @param[in,out] client pointer to mqtt client
|
||||
* @param[in] buf buffer to report
|
||||
* @param[in] buf_len buffer length
|
||||
* @return packet id (>=0) when success, or err code (<0) @see IotReturnCode
|
||||
*/
|
||||
int usr_data_template_property_report(void* client, char* buf, int buf_len)
|
||||
{
|
||||
char params[512];
|
||||
memset(params, 0, sizeof(params));
|
||||
params[0] = '{';
|
||||
int offset = 1;
|
||||
for (int i = 0; i < TOTAL_USR_PROPERTY_COUNT; i++) {
|
||||
DataTemplateProperty* property = &sg_usr_data_template_property[i];
|
||||
if (property->need_report) {
|
||||
offset += _get_property_node(params + offset, sizeof(params) - offset, property);
|
||||
if (offset > 0) {
|
||||
params[offset++] = ',';
|
||||
}
|
||||
property->need_report = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (offset > 0) {
|
||||
params[--offset] = '}';
|
||||
return IOT_DataTemplate_PropertyReport(client, buf, buf_len, params);
|
||||
}
|
||||
return QCLOUD_RET_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Post event.
|
||||
*
|
||||
* @param[in,out] client pointer to mqtt client
|
||||
* @param[in] buf buffer to report
|
||||
* @param[in] buf_len buffer length
|
||||
* @param[in] id @see UsrEventIndex
|
||||
* @param[in] params user should construct params according to event.
|
||||
*/
|
||||
void usr_data_template_event_post(void* client, char* buf, int buf_len, UsrEventIndex id, const char* params)
|
||||
{
|
||||
if (params) {
|
||||
sg_usr_data_template_event[id].params = params;
|
||||
}
|
||||
IOT_DataTemplate_EventPost(client, buf, buf_len, sg_usr_data_template_event[id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Parse action message and set action input params.
|
||||
*
|
||||
* @param[in] action_id action id
|
||||
* @param[in] params params of action
|
||||
* @param[out] index @see UsrActionIndex
|
||||
* @return 0 for success, QCLOUD_ERR_JSON_PARSE for invalid json.
|
||||
*/
|
||||
int usr_data_template_action_parse(UtilsJsonValue action_id, UtilsJsonValue params, UsrActionIndex* index)
|
||||
{
|
||||
DataTemplateProperty* property;
|
||||
|
||||
int input_property_count;
|
||||
|
||||
for (int i = 0; i < TOTAL_USR_ACTION_COUNT; i++) {
|
||||
if (!strncmp(action_id.value, sg_usr_data_template_action[i].action_id, action_id.value_len)) {
|
||||
property = sg_usr_data_template_action[i].input_struct.value_struct.property;
|
||||
input_property_count = sg_usr_data_template_action[i].input_struct.value_struct.count;
|
||||
|
||||
// 1. reset need report
|
||||
for (int j = 0; j < input_property_count; j++) {
|
||||
property[j].need_report = 0;
|
||||
}
|
||||
|
||||
// 2. parse
|
||||
_parse_property_array(params.value, params.value_len, property, input_property_count);
|
||||
|
||||
// 3. check all the input params is set
|
||||
for (int j = 0; j < input_property_count; j++) {
|
||||
if (!property[j].need_report) {
|
||||
return QCLOUD_ERR_JSON_PARSE;
|
||||
}
|
||||
}
|
||||
*index = i;
|
||||
return QCLOUD_RET_SUCCESS;
|
||||
}
|
||||
}
|
||||
return QCLOUD_ERR_JSON_PARSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get input value, should call after usr_data_template_action_parse
|
||||
*
|
||||
* @param[in] index index get from usr_data_template_action_parse
|
||||
* @param[in] property_index property index of action input params
|
||||
* @return @see DataTemplatePropertyValue
|
||||
*/
|
||||
DataTemplatePropertyValue usr_data_template_action_input_value_get(UsrActionIndex index, int property_index)
|
||||
{
|
||||
return sg_usr_data_template_action[index].input_struct.value_struct.property[property_index].value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Reply action, should call after parse action message.
|
||||
*
|
||||
* @param[in,out] client pointer to mqtt client
|
||||
* @param[in] buf buffer to report
|
||||
* @param[in] buf_len buffer length
|
||||
* @param[in] index @see UsrActionIndex
|
||||
* @param[in] client_token client token of action message
|
||||
* @param[in] code result code for action, 0 for success
|
||||
* @param[in] response action output params, user should construct params according to action
|
||||
* @return packet id (>=0) when success, or err code (<0) @see IotReturnCode
|
||||
*/
|
||||
int usr_data_template_action_reply(void* client, char* buf, int buf_len, UsrActionIndex index,
|
||||
UtilsJsonValue client_token, int code, const char* response)
|
||||
{
|
||||
sg_usr_data_template_action[index].reply.code = code;
|
||||
sg_usr_data_template_action[index].reply.client_token = client_token;
|
||||
sg_usr_data_template_action[index].reply.response = response;
|
||||
return IOT_DataTemplate_ActionReply(client, buf, buf_len, sg_usr_data_template_action[index].reply);
|
||||
}
|
@@ -0,0 +1,264 @@
|
||||
/**
|
||||
* @copyright
|
||||
*
|
||||
* Tencent is pleased to support the open source community by making IoT Hub available.
|
||||
* Copyright(C) 2018 - 2021 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.
|
||||
*
|
||||
* @file data_template_config.h
|
||||
* @brief
|
||||
* @author fancyxu (fancyxu@tencent.com)
|
||||
* @version 1.0
|
||||
* @date 2021-10-09
|
||||
*
|
||||
* @par Change Log:
|
||||
* <table>
|
||||
* <tr><th>Date <th>Version <th>Author <th>Description
|
||||
* <tr><td>2021-10-09 <td>1.0 <td>fancyxu <td>first commit
|
||||
* </table>
|
||||
*/
|
||||
|
||||
#ifndef IOT_HUB_DEVICE_C_SDK_APP_DATA_TEMPLATE_DATA_TEMPLATE_CONFIG_H_
|
||||
#define IOT_HUB_DEVICE_C_SDK_APP_DATA_TEMPLATE_DATA_TEMPLATE_CONFIG_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "qcloud_iot_common.h"
|
||||
#include "qcloud_iot_data_template.h"
|
||||
|
||||
/**
|
||||
* @brief Type of property.
|
||||
*
|
||||
*/
|
||||
typedef enum {
|
||||
// basic type
|
||||
DATA_TEMPLATE_TYPE_INT,
|
||||
DATA_TEMPLATE_TYPE_ENUM,
|
||||
DATA_TEMPLATE_TYPE_STRING_ENUM,
|
||||
DATA_TEMPLATE_TYPE_FLOAT,
|
||||
DATA_TEMPLATE_TYPE_BOOL,
|
||||
DATA_TEMPLATE_TYPE_STRING,
|
||||
DATA_TEMPLATE_TYPE_TIME,
|
||||
// construct by basic type
|
||||
DATA_TEMPLATE_TYPE_STRUCT,
|
||||
DATA_TEMPLATE_TYPE_ARRAY,
|
||||
} DataTemplatePropertyType;
|
||||
|
||||
/**
|
||||
* @brief Declaration.
|
||||
*
|
||||
*/
|
||||
typedef struct DataTemplateProperty DataTemplateProperty;
|
||||
typedef union DataTemplatePropertyValue DataTemplatePropertyValue;
|
||||
|
||||
/**
|
||||
* @brief Property value definition.
|
||||
*
|
||||
*/
|
||||
union DataTemplatePropertyValue {
|
||||
int32_t value_int;
|
||||
int32_t value_enum;
|
||||
char* value_string_enum;
|
||||
float value_float;
|
||||
int32_t value_bool;
|
||||
char* value_string;
|
||||
uint32_t value_time;
|
||||
struct {
|
||||
DataTemplateProperty* property;
|
||||
int count;
|
||||
} value_struct;
|
||||
DataTemplatePropertyValue* value_arrary; /**< not supportted yet */
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Property definition.
|
||||
*
|
||||
*/
|
||||
struct DataTemplateProperty {
|
||||
DataTemplatePropertyType type;
|
||||
const char* key;
|
||||
DataTemplatePropertyValue value;
|
||||
int need_report;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Event definition.
|
||||
*
|
||||
*/
|
||||
#define DataTemplateEvent IotDataTemplateEventData
|
||||
|
||||
/**
|
||||
* @brief Action definition.
|
||||
*
|
||||
*/
|
||||
typedef struct {
|
||||
const char* action_id;
|
||||
DataTemplatePropertyValue input_struct;
|
||||
IotDataTemplateActionReply reply;
|
||||
} DataTemplateAction;
|
||||
|
||||
/**************************************************************************************
|
||||
* usr data template definition
|
||||
**************************************************************************************/
|
||||
|
||||
typedef enum {
|
||||
USR_PROPERTY_INDEX_POWER_SWITCH = 0,
|
||||
USR_PROPERTY_INDEX_COLOR,
|
||||
USR_PROPERTY_INDEX_BRIGHTNESS,
|
||||
USR_PROPERTY_INDEX_NAME,
|
||||
USR_PROPERTY_INDEX_POSITION,
|
||||
USR_PROPERTY_INDEX_POWER,
|
||||
} UsrPropertyIndex;
|
||||
|
||||
typedef enum {
|
||||
USR_PROPERTY_POSITION_INDEX_LONGITUDE = 0,
|
||||
USR_PROPERTY_POSITION_INDEX_LATITUDE,
|
||||
} UsrPropertyPositionIndex;
|
||||
|
||||
typedef enum {
|
||||
USR_EVENT_INDEX_STATUS_REPORT = 0,
|
||||
USR_EVENT_INDEX_LOW_VOLTAGE,
|
||||
USR_EVENT_INDEX_HARDWARE_FAULT,
|
||||
} UsrEventIndex;
|
||||
|
||||
typedef enum {
|
||||
USR_ACTION_INDEX_LIGHT_BLINK = 0,
|
||||
} UsrActionIndex;
|
||||
|
||||
typedef enum {
|
||||
USR_ACTION_LIGHT_BLINK_INPUT_INDEX_TIME = 0,
|
||||
USR_ACTION_LIGHT_BLINK_INPUT_INDEX_COLOR,
|
||||
USR_ACTION_LIGHT_BLINK_INPUT_INDEX_TOTAL_TIME,
|
||||
} UsrActionLightBlinkInputIndex;
|
||||
|
||||
/**************************************************************************************
|
||||
* api for user data template
|
||||
**************************************************************************************/
|
||||
|
||||
/**
|
||||
* @brief Init user data template(property/event/action).
|
||||
*
|
||||
*/
|
||||
void usr_data_template_init(void);
|
||||
|
||||
/**
|
||||
* @brief Get property value.
|
||||
*
|
||||
* @param[in] index @see UsrPropertyIndex
|
||||
* @return @see DataTemplatePropertyValue
|
||||
*/
|
||||
DataTemplatePropertyValue usr_data_template_property_value_get(UsrPropertyIndex index);
|
||||
|
||||
/**
|
||||
* @brief Set property value.
|
||||
*
|
||||
* @param[in] index @see UsrPropertyIndex
|
||||
* @param[in] value @see DataTemplatePropertyValue, @note value should match property type.
|
||||
*/
|
||||
void usr_data_template_property_value_set(UsrPropertyIndex index, DataTemplatePropertyValue value);
|
||||
|
||||
/**
|
||||
* @brief Get property(struct) value.
|
||||
*
|
||||
* @param[in] struct_index @see UsrPropertyIndex, @note DATA_TEMPLATE_TYPE_STRUCT is required here.
|
||||
* @param[in] property_index depends on which struct
|
||||
* @return @see DataTemplatePropertyValue
|
||||
*/
|
||||
DataTemplatePropertyValue usr_data_template_property_struct_value_get(UsrPropertyIndex struct_index,
|
||||
int property_index);
|
||||
|
||||
/**
|
||||
* @brief Set property(struct) value.
|
||||
*
|
||||
* @param[in] struct_index @see UsrPropertyIndex, @note DATA_TEMPLATE_TYPE_STRUCT is required here.
|
||||
* @param[in] property_index depends on which struct
|
||||
* @param[in] value @see DataTemplatePropertyValue, @note value should match property type.
|
||||
*/
|
||||
void usr_data_template_property_struct_value_set(UsrPropertyIndex struct_index, int property_index,
|
||||
DataTemplatePropertyValue value);
|
||||
|
||||
/**
|
||||
* @brief Parse control message and set property value.
|
||||
*
|
||||
* @param[in] params params filed of control message
|
||||
*/
|
||||
void usr_data_template_property_parse(UtilsJsonValue params);
|
||||
|
||||
/**
|
||||
* @brief Get property status.
|
||||
*
|
||||
* @param[in] index @see UsrPropertyIndex
|
||||
* @return need_report
|
||||
*/
|
||||
int usr_data_template_property_status_get(UsrPropertyIndex index);
|
||||
|
||||
/**
|
||||
* @brief Report all the properties needed report.
|
||||
*
|
||||
* @param[in,out] client pointer to mqtt client
|
||||
* @param[in] buf buffer to report
|
||||
* @param[in] buf_len buffer length
|
||||
* @return packet id (>=0) when success, or err code (<0) @see IotReturnCode
|
||||
*/
|
||||
int usr_data_template_property_report(void* client, char* buf, int buf_len);
|
||||
|
||||
/**
|
||||
* @brief Post event.
|
||||
*
|
||||
* @param[in,out] client pointer to mqtt client
|
||||
* @param[in] buf buffer to report
|
||||
* @param[in] buf_len buffer length
|
||||
* @param[in] id @see UsrEventIndex
|
||||
* @param[in] params user should construct params according to event.
|
||||
*/
|
||||
void usr_data_template_event_post(void* client, char* buf, int buf_len, UsrEventIndex id, const char* params);
|
||||
|
||||
/**
|
||||
* @brief Parse action message and set action input params.
|
||||
*
|
||||
* @param[in] action_id action id
|
||||
* @param[in] params params of action
|
||||
* @param[out] index @see UsrActionIndex
|
||||
* @return 0 for success, QCLOUD_ERR_JSON_PARSE for invalid json.
|
||||
*/
|
||||
int usr_data_template_action_parse(UtilsJsonValue action_id, UtilsJsonValue params, UsrActionIndex* index);
|
||||
|
||||
/**
|
||||
* @brief Get input value, should call after usr_data_template_action_parse
|
||||
*
|
||||
* @param[in] index index get from usr_data_template_action_parse
|
||||
* @param[in] property_index property index of action input params
|
||||
* @return @see DataTemplatePropertyValue
|
||||
*/
|
||||
DataTemplatePropertyValue usr_data_template_action_input_value_get(UsrActionIndex index, int property_index);
|
||||
|
||||
/**
|
||||
* @brief Reply action, should call after parse action message.
|
||||
*
|
||||
* @param[in,out] client pointer to mqtt client
|
||||
* @param[in] buf buffer to report
|
||||
* @param[in] buf_len buffer length
|
||||
* @param[in] index @see UsrActionIndex
|
||||
* @param[in] client_token client token of action message
|
||||
* @param[in] code result code for action, 0 for success
|
||||
* @param[in] response action output params, user should construct params according to action
|
||||
* @return packet id (>=0) when success, or err code (<0) @see IotReturnCode
|
||||
*/
|
||||
int usr_data_template_action_reply(void* client, char* buf, int buf_len, UsrActionIndex index,
|
||||
UtilsJsonValue client_token, int code, const char* response);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // IOT_HUB_DEVICE_C_SDK_APP_DATA_TEMPLATE_DATA_TEMPLATE_CONFIG_H_
|
Reference in New Issue
Block a user