mirror of
https://github.com/Ai-Thinker-Open/Ai-Thinker-Open_RTL8710BX_ALIOS_SDK.git
synced 2026-07-08 19:45:39 +00:00
rel_1.6.0 init
This commit is contained in:
commit
27b3e2883d
19359 changed files with 8093121 additions and 0 deletions
126
Products/example/certification/certification.mk
Normal file
126
Products/example/certification/certification.mk
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
NAME := certification
|
||||
|
||||
# Cloud server region: MAINLAND or SINGAPORE, GERMANY
|
||||
CONFIG_SERVER_REGION ?= MAINLAND
|
||||
|
||||
# Cloud server env: ON_DAILY, ON_PRE, ONLINE
|
||||
CONFIG_SERVER_ENV ?= ONLINE
|
||||
|
||||
$(NAME)_SOURCES := ct_main.c \
|
||||
ct_entry.c \
|
||||
ct_cmds.c \
|
||||
ct_ut.c \
|
||||
ct_ota.c
|
||||
|
||||
# support MCU OTA, default DISABLE
|
||||
CONFIG_SUPPORT_MCU_OTA ?= DISABLE
|
||||
|
||||
CONFIG_FIRMWARE_VERSION = app-1.6.0
|
||||
|
||||
# Compile date and time
|
||||
ifeq ("${DATE}", "")
|
||||
DATE=date
|
||||
endif
|
||||
|
||||
CONFIG_COMPILE_DATE ?= $(shell ${DATE} +%Y%m%d.%H%M%S)
|
||||
|
||||
# Firmware type: 0 - release, 1 - debug
|
||||
CONFIG_DEBUG ?= 1
|
||||
|
||||
$(NAME)_COMPONENTS += framework/protocol/linkkit/sdk \
|
||||
framework/protocol/linkkit/hal \
|
||||
framework/netmgr \
|
||||
framework/common \
|
||||
utility/cjson \
|
||||
framework/uOTA
|
||||
|
||||
GLOBAL_INCLUDES += ../../../../framework/protocol/linkkit/include \
|
||||
../../../../framework/protocol/linkkit/include/imports \
|
||||
../../../../framework/protocol/linkkit/include/exports \
|
||||
|
||||
|
||||
GLOBAL_CFLAGS += -DCONFIG_YWSS \
|
||||
-DBUILD_AOS \
|
||||
-DAWSS_SUPPORT_STATIS
|
||||
|
||||
ifeq ($(CONFIG_SUPPORT_MCU_OTA), ENABLE)
|
||||
GLOBAL_CFLAGS += -DSUPPORT_MCU_OTA
|
||||
endif
|
||||
|
||||
GLOBAL_CFLAGS += -DREGION_${CONFIG_SERVER_REGION}
|
||||
|
||||
GLOBAL_CFLAGS += -D${CONFIG_SERVER_ENV}
|
||||
|
||||
ifeq ($(HOST_MCU_FAMILY),esp8266)
|
||||
GLOBAL_CFLAGS += -DPOWER_CYCLE_AWSS
|
||||
GLOBAL_DEFINES += ESP8266_CHIPSET
|
||||
else
|
||||
$(NAME)_COMPONENTS += cli
|
||||
GLOBAL_CFLAGS += -DCONFIG_AOS_CLI
|
||||
|
||||
#Create thread of cm_yield in mqtt_client.c
|
||||
GLOBAL_CFLAGS += -DCONFIG_SDK_THREAD_COST=1
|
||||
|
||||
GLOBAL_CFLAGS += -DDEV_ERRCODE_ENABLE
|
||||
endif
|
||||
|
||||
BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
|
||||
GLOBAL_CFLAGS += -DGIT_BRANCH=\"${BRANCH}\"
|
||||
COMMIT_HASH := $(shell git rev-parse HEAD)
|
||||
GLOBAL_CFLAGS += -DGIT_HASH=\"${COMMIT_HASH}\"
|
||||
COMPILE_USER := ${USER}
|
||||
GLOBAL_CFLAGS += -DCOMPILE_HOST=\"${COMPILE_USER}\"
|
||||
SERVER_CONF_STRING := "${CONFIG_SERVER_REGION}-${CONFIG_SERVER_ENV}"
|
||||
GLOBAL_CFLAGS += -DREGION_ENV_STRING=\"${SERVER_CONF_STRING}\"
|
||||
GLOBAL_CFLAGS += -DAPP_NAME=\"${APP}\"
|
||||
GLOBAL_CFLAGS += -DPLATFORM=\"${PLATFORM}\"
|
||||
CONFIG_SYSINFO_APP_VERSION = ${CONFIG_FIRMWARE_VERSION}-${CONFIG_COMPILE_DATE}
|
||||
GLOBAL_CFLAGS += -DSYSINFO_APP_VERSION=\"$(CONFIG_SYSINFO_APP_VERSION)\"
|
||||
|
||||
ifneq ("$(BOARD)", "")
|
||||
$(info server region: ${CONFIG_SERVER_REGION})
|
||||
$(info server env: ${CONFIG_SERVER_ENV})
|
||||
$(info APP: ${APP} Board: ${PLATFORM})
|
||||
$(info host user: ${COMPILE_USER})
|
||||
$(info branch: ${BRANCH})
|
||||
$(info hash: ${COMMIT_HASH})
|
||||
$(info app_version_new:${CONFIG_SYSINFO_APP_VERSION})
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_DEBUG), 1)
|
||||
GLOBAL_CFLAGS += -DDEFAULT_LOG_LEVEL_DEBUG
|
||||
GLOBAL_CFLAGS += -DCONFIG_BLDTIME_MUTE_DBGLOG=0
|
||||
$(info firmware type: DEBUG)
|
||||
else
|
||||
GLOBAL_CFLAGS += -DCONFIG_BLDTIME_MUTE_DBGLOG=1
|
||||
$(info firmware type: RELEASE)
|
||||
endif
|
||||
|
||||
ifeq ($(LWIP),1)
|
||||
$(NAME)_COMPONENTS += protocols.net
|
||||
no_with_lwip := 0
|
||||
endif
|
||||
|
||||
GLOBAL_INCLUDES += ./
|
||||
|
||||
include ./make.settings
|
||||
|
||||
SWITCH_VARS := FEATURE_MQTT_COMM_ENABLED FEATURE_ALCS_ENABLED FEATURE_DEVICE_MODEL_ENABLED \
|
||||
FEATURE_DEVICE_MODEL_GATEWAY FEATURE_DEV_BIND_ENABLED \
|
||||
FEATURE_DEVICE_MODEL_RAWDATA_SOLO FEATURE_COAP_COMM_ENABLED FEATURE_HTTP2_COMM_ENABLED \
|
||||
FEATURE_HTTP_COMM_ENABLED FEATURE_SAL_ENABLED FEATURE_WIFI_PROVISION_ENABLED FEATURE_AWSS_SUPPORT_SMARTCONFIG\
|
||||
FEATURE_AWSS_SUPPORT_ZEROCONFIG FEATURE_AWSS_SUPPORT_SMARTCONFIG FEATURE_AWSS_SUPPORT_ZEROCONFIG FEATURE_AWSS_SUPPORT_PHONEASAP \
|
||||
FEATURE_AWSS_SUPPORT_ROUTER FEATURE_AWSS_SUPPORT_DEV_AP FEATURE_OTA_ENABLED FEATURE_MQTT_AUTO_SUBSCRIBE FEATURE_MQTT_PREAUTH_SUPPORT_HTTPS_CDN \
|
||||
FEATURE_DM_UNIFIED_SERVICE_POST
|
||||
|
||||
$(foreach v, \
|
||||
$(SWITCH_VARS), \
|
||||
$(if $(filter y,$($(v))), \
|
||||
$(eval GLOBAL_DEFINES += $(subst FEATURE_,,$(v)))) \
|
||||
)
|
||||
|
||||
$(foreach v, \
|
||||
$(SWITCH_VARS), \
|
||||
$(if $(filter y,$($(v))), \
|
||||
$(eval SDK_DEFINES += $(subst FEATURE_,-D,$(v)))) \
|
||||
)
|
||||
434
Products/example/certification/ct_cmds.c
Normal file
434
Products/example/certification/ct_cmds.c
Normal file
|
|
@ -0,0 +1,434 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2019 Alibaba Group Holding Limited
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <aos/aos.h>
|
||||
#include <aos/yloop.h>
|
||||
#include "netmgr.h"
|
||||
#include "iot_export.h"
|
||||
#include "iot_import.h"
|
||||
#include "aos/kv.h"
|
||||
|
||||
#include <k_api.h>
|
||||
|
||||
#include "ct_main.h"
|
||||
#include "ct_entry.h"
|
||||
#include "ct_cmds.h"
|
||||
#include "ct_ut.h"
|
||||
|
||||
static char awss_running = 0;
|
||||
|
||||
static void awss_close_dev_ap(void *p)
|
||||
{
|
||||
awss_dev_ap_stop();
|
||||
ct_info("%s exit\n", __func__);
|
||||
aos_task_exit(0);
|
||||
}
|
||||
|
||||
void ct_open_dev_ap(void *p)
|
||||
{
|
||||
iotx_event_regist_cb(user_event_monitor);
|
||||
ct_info("%s\n", __func__);
|
||||
if (netmgr_start(false) != 0)
|
||||
{
|
||||
aos_msleep(2000);
|
||||
awss_dev_ap_start();
|
||||
}
|
||||
aos_task_exit(0);
|
||||
}
|
||||
|
||||
static void stop_netmgr(void *p)
|
||||
{
|
||||
awss_stop();
|
||||
ct_info("%s\n", __func__);
|
||||
aos_task_exit(0);
|
||||
}
|
||||
|
||||
void ct_start_netmgr(void *p)
|
||||
{
|
||||
iotx_event_regist_cb(user_event_monitor);
|
||||
netmgr_start(true);
|
||||
aos_task_exit(0);
|
||||
}
|
||||
|
||||
void ct_do_awss_active(void)
|
||||
{
|
||||
ct_info("ct_do_awss_active %d\n", awss_running);
|
||||
awss_running = 1;
|
||||
#ifdef WIFI_PROVISION_ENABLED
|
||||
extern int awss_config_press();
|
||||
awss_config_press();
|
||||
#endif
|
||||
}
|
||||
|
||||
void do_awss_dev_ap(void)
|
||||
{
|
||||
aos_task_new("netmgr_stop", stop_netmgr, NULL, 4096);
|
||||
aos_task_new("dap_open", ct_open_dev_ap, NULL, 4096);
|
||||
}
|
||||
|
||||
void do_awss()
|
||||
{
|
||||
aos_task_new("dap_close", awss_close_dev_ap, NULL, 2048);
|
||||
aos_task_new("netmgr_start", ct_start_netmgr, NULL, 4096);
|
||||
}
|
||||
|
||||
static void linkkit_reset(void *p)
|
||||
{
|
||||
iotx_sdk_reset_local();
|
||||
HAL_Reboot();
|
||||
}
|
||||
|
||||
void ct_awss_reset(void)
|
||||
{
|
||||
#ifdef WIFI_PROVISION_ENABLED
|
||||
aos_task_new("reset", (void (*)(void *))iotx_sdk_reset, NULL, 4096); // stack taken by iTLS is more than taken by TLS.
|
||||
#endif
|
||||
aos_post_delayed_action(2000, linkkit_reset, NULL);
|
||||
}
|
||||
|
||||
#ifdef CONFIG_AOS_CLI
|
||||
static void handle_reset_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
aos_schedule_call(ct_awss_reset, NULL);
|
||||
}
|
||||
|
||||
static void handle_active_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
aos_schedule_call(ct_do_awss_active, NULL);
|
||||
}
|
||||
|
||||
static void handle_dev_ap_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
aos_schedule_call(do_awss_dev_ap, NULL);
|
||||
}
|
||||
|
||||
static void handle_awss_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
aos_schedule_call(do_awss, NULL);
|
||||
}
|
||||
|
||||
#if defined(OFFLINE_LOG_UT_TEST)
|
||||
static void handle_offline_log_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (argc < 2)
|
||||
{
|
||||
aos_cli_printf("params err,oll [save/upload/erase] or [read [0-1]] or [write [0-3] \"test offline log\"]\r\n");
|
||||
return;
|
||||
}
|
||||
|
||||
aos_cli_printf("cmd:%s\r\n", argv[1]);
|
||||
|
||||
if (!strcmp("save", argv[1]) && argc == 2)
|
||||
{
|
||||
diagnosis_offline_log_save_all();
|
||||
}
|
||||
else if (!strcmp("upload", argv[1]) && argc == 4)
|
||||
{
|
||||
int port = atoi(argv[3]);
|
||||
|
||||
diagnosis_offline_log_upload(argv[2], port);
|
||||
}
|
||||
else if (!strcmp("write", argv[1]) && argc == 4)
|
||||
{
|
||||
int loglevel = atoi(argv[2]);
|
||||
|
||||
if (loglevel >= 0 && loglevel <= 3)
|
||||
{
|
||||
diagnosis_offline_log(loglevel, "%s\\n", argv[3]);
|
||||
}
|
||||
}
|
||||
else if (!strcmp("read", argv[1]) && argc == 3)
|
||||
{
|
||||
int log_mode = atoi(argv[2]);
|
||||
|
||||
diagnosis_offline_log_read_all(log_mode);
|
||||
}
|
||||
else if (!strcmp("erase", argv[1]) && argc == 2)
|
||||
{
|
||||
diagnosis_offline_log_erase_flash_desc();
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("cmd:%s is not support\r\n", argv[1]);
|
||||
}
|
||||
|
||||
if (ret != 0)
|
||||
{
|
||||
aos_cli_printf("handle oll cmd:%s failed:%d\r\n", argv[1], ret);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static struct cli_command offline_log_cmd = {.name = "oll",
|
||||
.help = "oll [save]/[erase] or [upload host port] or [read [0-1]] or [write [0-3] \"test offline log\"]",
|
||||
.function = handle_offline_log_cmd};
|
||||
|
||||
#endif
|
||||
|
||||
static void handle_linkkey_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
int index = 0;
|
||||
char key_buf[MAX_KEY_LEN];
|
||||
|
||||
if (argc == 1)
|
||||
{
|
||||
int len = 0;
|
||||
char product_key[PRODUCT_KEY_LEN + 1] = {0};
|
||||
char product_secret[PRODUCT_SECRET_LEN + 1] = {0};
|
||||
char device_name[DEVICE_NAME_LEN + 1] = {0};
|
||||
char device_secret[DEVICE_SECRET_LEN + 1] = {0};
|
||||
|
||||
len = PRODUCT_KEY_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(product_key, 0, sizeof(product_key));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PK, index);
|
||||
aos_kv_get(key_buf, product_key, &len);
|
||||
|
||||
len = PRODUCT_SECRET_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(product_secret, 0, sizeof(product_secret));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PS, index);
|
||||
aos_kv_get(key_buf, product_secret, &len);
|
||||
|
||||
len = DEVICE_NAME_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(device_name, 0, sizeof(device_name));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DN, index);
|
||||
aos_kv_get(key_buf, device_name, &len);
|
||||
|
||||
len = DEVICE_SECRET_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(device_secret, 0, sizeof(device_secret));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DS, index);
|
||||
aos_kv_get(key_buf, device_secret, &len);
|
||||
|
||||
aos_cli_printf("PK=%s.\r\n", product_key);
|
||||
aos_cli_printf("PS=%s.\r\n", product_secret);
|
||||
aos_cli_printf("DN=%s.\r\n", device_name);
|
||||
aos_cli_printf("DS=%s.\r\n", device_secret);
|
||||
}
|
||||
else if (argc == 5)
|
||||
{
|
||||
int devid = 0;
|
||||
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PK, devid);
|
||||
aos_kv_set(key_buf, argv[1], strlen(argv[1]) + 1, 1);
|
||||
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DN, devid);
|
||||
aos_kv_set(key_buf, argv[2], strlen(argv[2]) + 1, 1);
|
||||
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DS, devid);
|
||||
aos_kv_set(key_buf, argv[3], strlen(argv[3]) + 1, 1);
|
||||
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PS, devid);
|
||||
aos_kv_set(key_buf, argv[4], strlen(argv[4]) + 1, 1);
|
||||
|
||||
aos_cli_printf("Done\r\n");
|
||||
}
|
||||
else if (argc == 6)
|
||||
{
|
||||
int devid = atoi(argv[1]);
|
||||
|
||||
if (devid >= 0 && devid <= MAX_DEVICES_META_NUM)
|
||||
{
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PK, devid);
|
||||
aos_kv_set(key_buf, argv[2], strlen(argv[2]) + 1, 1);
|
||||
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DN, devid);
|
||||
aos_kv_set(key_buf, argv[3], strlen(argv[3]) + 1, 1);
|
||||
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DS, devid);
|
||||
aos_kv_set(key_buf, argv[4], strlen(argv[4]) + 1, 1);
|
||||
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PS, devid);
|
||||
aos_kv_set(key_buf, argv[5], strlen(argv[5]) + 1, 1);
|
||||
|
||||
aos_cli_printf("Done\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("Err:devid range[0-%d]", MAX_DEVICES_META_NUM - 1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("Params Err\r\n");
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef DEV_ERRCODE_ENABLE
|
||||
#define DEV_DIAG_CLI_SET_ERRCODE "set_err"
|
||||
#define DEV_DIAG_CLI_ERRCODE_MANUAL_MSG "errcode was manually set"
|
||||
#define DEV_DIAG_CLI_GET_ERRCODE "get_err"
|
||||
#define DEV_DIAG_CLI_DEL_ERRCODE "del_err"
|
||||
|
||||
static void handle_dev_serv_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
uint16_t err_code = 0;
|
||||
char err_msg[DEV_ERRCODE_MSG_MAX_LEN] = {0};
|
||||
uint8_t ret = 0;
|
||||
if (argc == 1)
|
||||
{
|
||||
aos_cli_printf("device diagnosis service test, please input more parameters.\r\n");
|
||||
}
|
||||
else if (argc > 1)
|
||||
{
|
||||
if ((argc == 3) && (!strcmp(argv[1], DEV_DIAG_CLI_SET_ERRCODE)))
|
||||
{
|
||||
err_code = (uint16_t)strtoul(argv[2], NULL, 0);
|
||||
ret = dev_errcode_kv_set(err_code, DEV_DIAG_CLI_ERRCODE_MANUAL_MSG);
|
||||
if (ret == 0)
|
||||
{
|
||||
aos_cli_printf("manually set err_code=%d\r\n", err_code);
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("manually set err_code failed!\r\n");
|
||||
}
|
||||
}
|
||||
else if ((argc == 2) && (!strcmp(argv[1], DEV_DIAG_CLI_GET_ERRCODE)))
|
||||
{
|
||||
ret = dev_errcode_kv_get(&err_code, err_msg);
|
||||
if (ret == 0)
|
||||
{
|
||||
aos_cli_printf("get err_code=%d, err_msg=%s\r\n", err_code, err_msg);
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("get err_code fail or no err_code found!\r\n");
|
||||
}
|
||||
}
|
||||
else if ((argc == 2) && (!strcmp(argv[1], DEV_DIAG_CLI_DEL_ERRCODE)))
|
||||
{
|
||||
dev_errcode_kv_del();
|
||||
aos_cli_printf("del err_code\r\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("Error: %d\r\n", __LINE__);
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef MANUFACT_AP_FIND_ENABLE
|
||||
|
||||
#define MANUAP_CONTROL_KEY "manuap"
|
||||
#define MANUAP_CLI_OPEN "open"
|
||||
#define MANUAP_CLI_CLOSE "close"
|
||||
|
||||
static void handle_manuap_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
int ret = 0;
|
||||
uint8_t manuap_flag = 0;
|
||||
int manu_flag_len = sizeof(manuap_flag);
|
||||
if (argc == 1)
|
||||
{
|
||||
ret = aos_kv_get(MANUAP_CONTROL_KEY, &manuap_flag, &manu_flag_len);
|
||||
if (ret == 0)
|
||||
{
|
||||
if (manuap_flag)
|
||||
{
|
||||
aos_cli_printf("manuap state is open\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("manuap state is close\r\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("no manuap state stored\r\n");
|
||||
}
|
||||
}
|
||||
else if (argc == 2)
|
||||
{
|
||||
if (!strcmp(argv[1], MANUAP_CLI_OPEN))
|
||||
{
|
||||
manuap_flag = 1;
|
||||
ret = aos_kv_set(MANUAP_CONTROL_KEY, &manuap_flag, sizeof(manuap_flag), 1);
|
||||
if (ret == 0)
|
||||
{
|
||||
aos_cli_printf("manuap opened\r\n");
|
||||
}
|
||||
}
|
||||
else if (!strcmp(argv[1], MANUAP_CLI_CLOSE))
|
||||
{
|
||||
manuap_flag = 0;
|
||||
ret = aos_kv_set(MANUAP_CONTROL_KEY, &manuap_flag, sizeof(manuap_flag), 1);
|
||||
if (ret == 0)
|
||||
{
|
||||
aos_cli_printf("manuap closed\r\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("param input err\r\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("param input err\r\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static struct cli_command resetcmd = {.name = "reset",
|
||||
.help = "factory reset",
|
||||
.function = handle_reset_cmd};
|
||||
|
||||
static struct cli_command awss_enable_cmd = {.name = "active_awss",
|
||||
.help = "active_awss [start]",
|
||||
.function = handle_active_cmd};
|
||||
static struct cli_command awss_dev_ap_cmd = {.name = "dev_ap",
|
||||
.help = "awss_dev_ap [start]",
|
||||
.function = handle_dev_ap_cmd};
|
||||
static struct cli_command awss_cmd = {.name = "awss",
|
||||
.help = "awss [start]",
|
||||
.function = handle_awss_cmd};
|
||||
static struct cli_command linkkeycmd = {.name = "linkkey",
|
||||
.help = "set/get linkkit keys. linkkey [devid] [<Product Key> <Device Name> <Device Secret> <Product Secret>]",
|
||||
.function = handle_linkkey_cmd};
|
||||
|
||||
#ifdef DEV_ERRCODE_ENABLE
|
||||
static struct cli_command dev_service_cmd = {.name = "dev_serv",
|
||||
.help = "device diagnosis service tests. like errcode, and log report",
|
||||
.function = handle_dev_serv_cmd};
|
||||
#endif
|
||||
#ifdef MANUFACT_AP_FIND_ENABLE
|
||||
static struct cli_command manuap_cmd = {.name = "manuap",
|
||||
.help = "manuap [open] or manuap [close]",
|
||||
.function = handle_manuap_cmd};
|
||||
#endif
|
||||
|
||||
int ct_register_cmds(void)
|
||||
{
|
||||
aos_cli_register_command(&resetcmd);
|
||||
aos_cli_register_command(&awss_enable_cmd);
|
||||
aos_cli_register_command(&awss_dev_ap_cmd);
|
||||
aos_cli_register_command(&awss_cmd);
|
||||
aos_cli_register_command(&linkkeycmd);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
12
Products/example/certification/ct_cmds.h
Normal file
12
Products/example/certification/ct_cmds.h
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#ifndef __CT_CMDS_H__
|
||||
#define __CT_CMDS_H__
|
||||
|
||||
#define MAX_DEVICES_META_NUM (0)
|
||||
|
||||
extern int ct_register_cmds(void);
|
||||
extern void ct_start_netmgr(void *p);
|
||||
extern void ct_open_dev_ap(void *p);
|
||||
extern void ct_awss_reset(void);
|
||||
extern void ct_do_awss_active(void);
|
||||
|
||||
#endif
|
||||
429
Products/example/certification/ct_entry.c
Normal file
429
Products/example/certification/ct_entry.c
Normal file
|
|
@ -0,0 +1,429 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2019 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <aos/aos.h>
|
||||
#include <aos/yloop.h>
|
||||
#include "netmgr.h"
|
||||
#include "iot_export.h"
|
||||
#include "iot_import.h"
|
||||
#include "aos/kv.h"
|
||||
|
||||
#ifdef CSP_LINUXHOST
|
||||
#include <signal.h>
|
||||
#endif
|
||||
|
||||
#include <k_api.h>
|
||||
|
||||
#if defined(OTA_ENABLED) && defined(BUILD_AOS)
|
||||
#include "ota_service.h"
|
||||
#endif
|
||||
|
||||
#include "ct_main.h"
|
||||
#include "ct_entry.h"
|
||||
#include "ct_cmds.h"
|
||||
#include "ct_ut.h"
|
||||
#include "ct_ota.h"
|
||||
|
||||
static char ct_started = 0;
|
||||
static int ct_duration_interval = 0;
|
||||
|
||||
void set_iotx_info();
|
||||
void do_awss_active();
|
||||
|
||||
extern k_mm_head *g_kmm_head;
|
||||
extern uint32_t dumpsys_mm_info_func(uint32_t len);
|
||||
extern uint32_t dumpsys_task_func(char *buf, uint32_t len, int detail);
|
||||
static void dump_task_and_mem_info(void)
|
||||
{
|
||||
#if defined(CONFIG_AOS_CLI)
|
||||
printf("======================================");
|
||||
dumpsys_mm_info_func(0);
|
||||
printf("======================================");
|
||||
dumpsys_task_func(NULL, 0, 1);
|
||||
printf("======================================");
|
||||
#else
|
||||
printf("============free heap size =%d==========\r\n", g_kmm_head->free_size);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void ct_duration_work(void *param)
|
||||
{
|
||||
dump_task_and_mem_info();
|
||||
|
||||
aos_post_delayed_action(ct_duration_interval, ct_duration_work, NULL);
|
||||
}
|
||||
|
||||
static void wifi_service_event(input_event_t *event, void *priv_data)
|
||||
{
|
||||
netmgr_ap_config_t config;
|
||||
|
||||
if (event->type != EV_WIFI || event->code != CODE_WIFI_ON_GOT_IP)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
memset(&config, 0, sizeof(netmgr_ap_config_t));
|
||||
netmgr_get_ap_config(&config);
|
||||
|
||||
ct_info("wifi_service_event config.ssid:%s", config.ssid);
|
||||
if (strcmp(config.ssid, "adha") == 0 || strcmp(config.ssid, "aha") == 0)
|
||||
{
|
||||
// clear_wifi_ssid();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!ct_started)
|
||||
{
|
||||
aos_task_new("linkkit", (void (*)(void *))ct_main, NULL, CT_MAIN_THREAD_STACKSZIE);
|
||||
ct_started = 1;
|
||||
}
|
||||
}
|
||||
|
||||
static void cloud_service_event(input_event_t *event, void *priv_data)
|
||||
{
|
||||
if (event->type != EV_YUNIO)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ct_info("cloud_service_event %d", event->code);
|
||||
|
||||
if (event->code == CODE_YUNIO_ON_CONNECTED)
|
||||
{
|
||||
ct_info("user sub and pub here");
|
||||
return;
|
||||
}
|
||||
|
||||
if (event->code == CODE_YUNIO_ON_DISCONNECTED)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Note:
|
||||
* the user_event_monitor must not block and should run to complete fast
|
||||
* if user wants to do complex operation with much time,
|
||||
* user should post one task to do this, not implement complex operation in
|
||||
* user_event_monitor
|
||||
*/
|
||||
|
||||
void user_event_monitor(int event)
|
||||
{
|
||||
switch (event)
|
||||
{
|
||||
case IOTX_AWSS_START: // AWSS start without enbale, just supports device discover
|
||||
// operate led to indicate user
|
||||
ct_info("IOTX_AWSS_START");
|
||||
break;
|
||||
case IOTX_AWSS_ENABLE: // AWSS enable, AWSS doesn't parse awss packet until AWSS is enabled.
|
||||
ct_info("IOTX_AWSS_ENABLE");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_LOCK_CHAN: // AWSS lock channel(Got AWSS sync packet)
|
||||
ct_info("IOTX_AWSS_LOCK_CHAN");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_PASSWD_ERR: // AWSS decrypt passwd error
|
||||
ct_info("IOTX_AWSS_PASSWD_ERR");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_GOT_SSID_PASSWD:
|
||||
ct_info("IOTX_AWSS_GOT_SSID_PASSWD");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_ADHA: // AWSS try to connnect adha (device
|
||||
// discover, router solution)
|
||||
ct_info("IOTX_AWSS_CONNECT_ADHA");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_ADHA_FAIL: // AWSS fails to connect adha
|
||||
ct_info("IOTX_AWSS_CONNECT_ADHA_FAIL");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_AHA: // AWSS try to connect aha (AP solution)
|
||||
ct_info("IOTX_AWSS_CONNECT_AHA");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_AHA_FAIL: // AWSS fails to connect aha
|
||||
ct_info("IOTX_AWSS_CONNECT_AHA_FAIL");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_SETUP_NOTIFY: // AWSS sends out device setup information
|
||||
// (AP and router solution)
|
||||
ct_info("IOTX_AWSS_SETUP_NOTIFY");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_ROUTER: // AWSS try to connect destination router
|
||||
ct_info("IOTX_AWSS_CONNECT_ROUTER");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_ROUTER_FAIL: // AWSS fails to connect destination
|
||||
// router.
|
||||
ct_info("IOTX_AWSS_CONNECT_ROUTER_FAIL");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_GOT_IP: // AWSS connects destination successfully and got
|
||||
// ip address
|
||||
ct_info("IOTX_AWSS_GOT_IP");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_SUC_NOTIFY: // AWSS sends out success notify (AWSS
|
||||
// sucess)
|
||||
ct_info("IOTX_AWSS_SUC_NOTIFY");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_BIND_NOTIFY: // AWSS sends out bind notify information to
|
||||
// support bind between user and device
|
||||
ct_info("IOTX_AWSS_BIND_NOTIFY");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_ENABLE_TIMEOUT: // AWSS enable timeout
|
||||
// user needs to enable awss again to support get ssid & passwd of router
|
||||
ct_info("IOTX_AWSS_ENALBE_TIMEOUT");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_CONN_CLOUD: // Device try to connect cloud
|
||||
ct_info("IOTX_CONN_CLOUD");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_CONN_CLOUD_FAIL: // Device fails to connect cloud, refer to
|
||||
// net_sockets.h for error code
|
||||
ct_info("IOTX_CONN_CLOUD_FAIL");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_CONN_CLOUD_SUC: // Device connects cloud successfully
|
||||
ct_info("IOTX_CONN_CLOUD_SUC");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_RESET: // Linkkit reset success (just got reset response from
|
||||
// cloud without any other operation)
|
||||
ct_info("IOTX_RESET");
|
||||
break;
|
||||
case IOTX_CONN_REPORT_TOKEN_SUC:
|
||||
ct_info("---- report token success ----");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void user_key_process(input_event_t *eventinfo, void *priv_data)
|
||||
{
|
||||
if (eventinfo->type != EV_KEY)
|
||||
{
|
||||
return;
|
||||
}
|
||||
ct_info("awss config press %d\n", eventinfo->value);
|
||||
|
||||
if (eventinfo->code == CODE_BOOT)
|
||||
{
|
||||
if (eventinfo->value == VALUE_KEY_CLICK)
|
||||
{
|
||||
|
||||
ct_do_awss_active();
|
||||
}
|
||||
else if (eventinfo->value == VALUE_KEY_LTCLICK)
|
||||
{
|
||||
ct_awss_reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(OTA_ENABLED) && defined(BUILD_AOS)
|
||||
static ota_service_t ctx = {0};
|
||||
static bool ota_service_inited = false;
|
||||
|
||||
void *ct_entry_get_uota_ctx(void)
|
||||
{
|
||||
if (ota_service_inited == true)
|
||||
{
|
||||
return (void *)&ctx;
|
||||
}
|
||||
else
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static int ota_init(void)
|
||||
{
|
||||
char product_key[PRODUCT_KEY_LEN + 1] = {0};
|
||||
char device_name[DEVICE_NAME_LEN + 1] = {0};
|
||||
char device_secret[DEVICE_SECRET_LEN + 1] = {0};
|
||||
HAL_GetProductKey(product_key);
|
||||
HAL_GetDeviceName(device_name);
|
||||
HAL_GetDeviceSecret(device_secret);
|
||||
memset(&ctx, 0, sizeof(ota_service_t));
|
||||
strncpy(ctx.pk, product_key, sizeof(ctx.pk) - 1);
|
||||
strncpy(ctx.dn, device_name, sizeof(ctx.dn) - 1);
|
||||
strncpy(ctx.ds, device_secret, sizeof(ctx.ds) - 1);
|
||||
ctx.trans_protcol = 0;
|
||||
ctx.dl_protcol = 3;
|
||||
ota_service_init(&ctx);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int mqtt_connected_event_handler(void)
|
||||
{
|
||||
#if defined(OTA_ENABLED) && defined(BUILD_AOS)
|
||||
if (ota_service_inited == true)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
LOG("MQTT reconnected, let's redo OTA upgrade");
|
||||
if ((ctx.h_tr) && (ctx.h_tr->upgrade))
|
||||
{
|
||||
LOG("Redoing OTA upgrade");
|
||||
ret = ctx.h_tr->upgrade(&ctx);
|
||||
if (ret < 0)
|
||||
LOG("Failed to do OTA upgrade");
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
LOG("MQTT Construct OTA start to inform");
|
||||
#ifdef DEV_OFFLINE_OTA_ENABLE
|
||||
ota_service_inform(&ctx);
|
||||
#else
|
||||
ota_init();
|
||||
#endif
|
||||
ota_service_inited = true;
|
||||
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void load_device_meta_info(void)
|
||||
{
|
||||
int len = 0;
|
||||
char key_buf[MAX_KEY_LEN];
|
||||
char product_key[PRODUCT_KEY_LEN + 1] = {0};
|
||||
char product_secret[PRODUCT_SECRET_LEN + 1] = {0};
|
||||
char device_name[DEVICE_NAME_LEN + 1] = {0};
|
||||
char device_secret[DEVICE_SECRET_LEN + 1] = {0};
|
||||
|
||||
len = PRODUCT_KEY_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(product_key, 0, sizeof(product_key));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PK, 0);
|
||||
HAL_Kv_Get(key_buf, product_key, &len);
|
||||
|
||||
len = PRODUCT_SECRET_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(product_secret, 0, sizeof(product_secret));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PS, 0);
|
||||
HAL_Kv_Get(key_buf, product_secret, &len);
|
||||
|
||||
len = DEVICE_NAME_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(device_name, 0, sizeof(device_name));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DN, 0);
|
||||
HAL_Kv_Get(key_buf, device_name, &len);
|
||||
|
||||
len = DEVICE_SECRET_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(device_secret, 0, sizeof(device_secret));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DS, 0);
|
||||
HAL_Kv_Get(key_buf, device_secret, &len);
|
||||
|
||||
if ((strlen(product_key) > 0) && (strlen(product_secret) > 0) && (strlen(device_name) > 0))
|
||||
{
|
||||
HAL_SetProductKey(product_key);
|
||||
HAL_SetProductSecret(product_secret);
|
||||
HAL_SetDeviceName(device_name);
|
||||
HAL_SetDeviceSecret(device_secret);
|
||||
printf("pk[%s]\r\n", product_key);
|
||||
printf("dn[%s]\r\n", device_name);
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_SetProductKey(PRODUCT_KEY);
|
||||
HAL_SetProductSecret(PRODUCT_SECRET);
|
||||
HAL_SetDeviceName(DEVICE_NAME);
|
||||
HAL_SetDeviceSecret(DEVICE_SECRET);
|
||||
printf("pk[%s]\r\n", PRODUCT_KEY);
|
||||
printf("dn[%s]\r\n", DEVICE_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
int application_start(int argc, char **argv)
|
||||
{
|
||||
int len = 0;
|
||||
char interval[9] = {0};
|
||||
|
||||
#ifdef CSP_LINUXHOST
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
#endif
|
||||
|
||||
#ifdef WITH_SAL
|
||||
sal_init();
|
||||
#endif
|
||||
|
||||
#ifdef MDAL_MAL_ICA_TEST
|
||||
HAL_MDAL_MAL_Init();
|
||||
#endif
|
||||
|
||||
#ifdef DEFAULT_LOG_LEVEL_DEBUG
|
||||
IOT_SetLogLevel(IOT_LOG_DEBUG);
|
||||
#else
|
||||
#ifdef CSP_LINUXHOST
|
||||
IOT_SetLogLevel(IOT_LOG_DEBUG);
|
||||
#else
|
||||
IOT_SetLogLevel(IOT_LOG_INFO);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
load_device_meta_info();
|
||||
|
||||
#ifdef DEV_OFFLINE_OTA_ENABLE
|
||||
ota_init();
|
||||
#endif
|
||||
|
||||
netmgr_init();
|
||||
|
||||
#ifdef DEV_ERRCODE_ENABLE
|
||||
dev_diagnosis_module_init();
|
||||
#endif
|
||||
|
||||
aos_register_event_filter(EV_KEY, user_key_process, NULL);
|
||||
aos_register_event_filter(EV_WIFI, wifi_service_event, NULL);
|
||||
aos_register_event_filter(EV_YUNIO, cloud_service_event, NULL);
|
||||
|
||||
IOT_RegisterCallback(ITE_MQTT_CONNECT_SUCC, mqtt_connected_event_handler);
|
||||
|
||||
#ifdef CONFIG_AOS_CLI
|
||||
ct_register_cmds();
|
||||
#endif
|
||||
|
||||
#if defined(AWSS_SUPPORT_DEV_AP) && defined(RDA5981x)
|
||||
aos_task_new("dap_open", ct_open_dev_ap, NULL, 4096);
|
||||
#else
|
||||
aos_task_new("netmgr_start", ct_start_netmgr, NULL, 4096);
|
||||
#endif
|
||||
|
||||
//This code just for debug
|
||||
len = sizeof(int);
|
||||
if (0 == HAL_Kv_Get(CT_DUMP_INTERVAL_KV_KEY, interval, &len))
|
||||
{
|
||||
ct_duration_interval = atoi(interval);
|
||||
|
||||
if (ct_duration_interval > 0)
|
||||
{
|
||||
aos_post_delayed_action(ct_duration_interval, ct_duration_work, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
aos_loop_run();
|
||||
|
||||
return 0;
|
||||
}
|
||||
12
Products/example/certification/ct_entry.h
Normal file
12
Products/example/certification/ct_entry.h
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#ifndef __CT_ENTRY_H__
|
||||
#define __CT_ENTRY_H__
|
||||
|
||||
#define CT_MAIN_THREAD_STACKSZIE (8 * 1024)
|
||||
#define CT_DUMP_INTERVAL_KV_KEY "ctdump"
|
||||
|
||||
extern void user_event_monitor(int event);
|
||||
|
||||
#if defined(OTA_ENABLED) && defined(BUILD_AOS)
|
||||
void *ct_entry_get_uota_ctx(void);
|
||||
#endif
|
||||
#endif
|
||||
741
Products/example/certification/ct_main.c
Normal file
741
Products/example/certification/ct_main.c
Normal file
|
|
@ -0,0 +1,741 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2019 Alibaba Group Holding Limited
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "cJSON.h"
|
||||
#include "iot_import.h"
|
||||
#include "iot_export_linkkit.h"
|
||||
|
||||
#include "iotx_log.h"
|
||||
|
||||
#include "ct_main.h"
|
||||
#include "ct_entry.h"
|
||||
#include "ct_ut.h"
|
||||
#include "ct_ota.h"
|
||||
|
||||
static ct_ctx_t g_ct_ctx;
|
||||
|
||||
ct_ctx_t *ct_get_ctx(void)
|
||||
{
|
||||
return &g_ct_ctx;
|
||||
}
|
||||
|
||||
static int user_connected_event_handler(void)
|
||||
{
|
||||
ct_ctx_t *ct_ctx = ct_get_ctx();
|
||||
|
||||
ct_info("Cloud Connected");
|
||||
|
||||
ct_ctx->cloud_connected = 1;
|
||||
|
||||
if (ct_ctx->master_initialized == 1) //initialized
|
||||
{
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_disconnected_event_handler(void)
|
||||
{
|
||||
ct_ctx_t *ct_ctx = ct_get_ctx();
|
||||
|
||||
ct_info("Cloud Disconnected");
|
||||
|
||||
ct_ctx->cloud_connected = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_down_raw_data_arrived_event_handler(const int devid, const unsigned char *payload,
|
||||
const int payload_len)
|
||||
{
|
||||
ct_info("Down Raw Message, Devid: %d, Payload Length: %d", devid, payload_len);
|
||||
if (payload[0] == 0x02 && payload_len == 6)
|
||||
{
|
||||
ct_ut_set_LightSwitch(payload[5]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_service_request_event_handler(const int devid, const char *serviceid, const int serviceid_len,
|
||||
const char *request, const int request_len,
|
||||
char **response, int *response_len)
|
||||
{
|
||||
int transparency = 0;
|
||||
cJSON *root = NULL, *item_transparency = NULL;
|
||||
|
||||
ct_info("Service Request Received, Devid: %d, Service ID: %.*s, Payload: %s", devid, serviceid_len,
|
||||
serviceid,
|
||||
request);
|
||||
|
||||
/* Parse Root */
|
||||
root = cJSON_Parse(request);
|
||||
if (root == NULL || !cJSON_IsObject(root))
|
||||
{
|
||||
ct_err("JSON Parse Error");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (strlen("Custom") == serviceid_len && memcmp("Custom", serviceid, serviceid_len) == 0)
|
||||
{
|
||||
/* Parse Item */
|
||||
const char *response_fmt = "{\"transparency\":%d}";
|
||||
item_transparency = cJSON_GetObjectItem(root, "transparency");
|
||||
if (item_transparency == NULL || !cJSON_IsNumber(item_transparency))
|
||||
{
|
||||
cJSON_Delete(root);
|
||||
return -1;
|
||||
}
|
||||
ct_info("transparency: %d", item_transparency->valueint);
|
||||
transparency = item_transparency->valueint + 1;
|
||||
if (transparency > 100)
|
||||
{
|
||||
transparency = 100;
|
||||
}
|
||||
|
||||
/* Send Service Response To Cloud */
|
||||
*response_len = strlen(response_fmt) + 10 + 1;
|
||||
*response = (char *)HAL_Malloc(*response_len);
|
||||
if (*response == NULL)
|
||||
{
|
||||
ct_err("Memory Not Enough");
|
||||
return -1;
|
||||
}
|
||||
memset(*response, 0, *response_len);
|
||||
HAL_Snprintf(*response, *response_len, response_fmt, transparency);
|
||||
*response_len = strlen(*response);
|
||||
}
|
||||
else if (strlen("ToggleLightSwitch") == serviceid_len && memcmp("ToggleLightSwitch", serviceid, serviceid_len) == 0)
|
||||
{
|
||||
/* Parse Item */
|
||||
const char *response_fmt = "{\"LightSwitch\":%d}";
|
||||
|
||||
/* Send Service Response To Cloud */
|
||||
*response_len = strlen(response_fmt) + strlen("LightSwitch") + 10;
|
||||
*response = (char *)HAL_Malloc(*response_len);
|
||||
if (*response == NULL)
|
||||
{
|
||||
ct_err("Memory Not Enough");
|
||||
return -1;
|
||||
}
|
||||
memset(*response, 0, *response_len);
|
||||
if (ct_ut_get_LightSwitch() == 0)
|
||||
{
|
||||
ct_ut_set_LightSwitch(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
ct_ut_set_LightSwitch(0);
|
||||
}
|
||||
|
||||
HAL_Snprintf(*response, *response_len, response_fmt, ct_ut_get_LightSwitch());
|
||||
*response_len = strlen(*response);
|
||||
}
|
||||
|
||||
cJSON_Delete(root);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef ALCS_ENABLED
|
||||
//Just for reference,user have to change his owner properties
|
||||
static int user_property_get_event_handler(const int devid, const char *request, const int request_len, char **response,
|
||||
int *response_len)
|
||||
{
|
||||
int index = 0;
|
||||
ct_tsl_t *p_ct_tsl_data = ct_ut_get_tsl_data();
|
||||
cJSON *response_root = NULL;
|
||||
cJSON *request_root = NULL, *item_propertyid = NULL;
|
||||
ct_info("Property Get Received, Devid: %d, Request: %s", devid, request);
|
||||
|
||||
/* Parse Request */
|
||||
request_root = cJSON_Parse(request);
|
||||
if (request_root == NULL || !cJSON_IsArray(request_root))
|
||||
{
|
||||
ct_info("JSON Parse Error");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Prepare Response */
|
||||
response_root = cJSON_CreateObject();
|
||||
if (response_root == NULL)
|
||||
{
|
||||
ct_info("No Enough Memory");
|
||||
cJSON_Delete(request_root);
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (index = 0; index < cJSON_GetArraySize(request_root); index++)
|
||||
{
|
||||
item_propertyid = cJSON_GetArrayItem(request_root, index);
|
||||
if (item_propertyid == NULL || !cJSON_IsString(item_propertyid))
|
||||
{
|
||||
ct_info("JSON Parse Error");
|
||||
cJSON_Delete(request_root);
|
||||
cJSON_Delete(response_root);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ct_info("Property ID, index: %d, Value: %s", index, item_propertyid->valuestring);
|
||||
if (strcmp("WIFI_Band", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddStringToObject(response_root, "WIFI_Band", p_ct_tsl_data->wifi.band);
|
||||
}
|
||||
else if (strcmp("WIFI_AP_BSSID", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddStringToObject(response_root, "WIFI_AP_BSSID", p_ct_tsl_data->wifi.bssid);
|
||||
}
|
||||
else if (strcmp("WIFI_Channel", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddNumberToObject(response_root, "WIFI_Channel", p_ct_tsl_data->wifi.Channel);
|
||||
}
|
||||
else if (strcmp("WiFI_SNR", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddNumberToObject(response_root, "WiFI_SNR", p_ct_tsl_data->wifi.SNR);
|
||||
}
|
||||
else if (strcmp("WiFI_RSSI", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddNumberToObject(response_root, "WiFI_RSSI", p_ct_tsl_data->wifi.rssi);
|
||||
}
|
||||
else if (strcmp("LightSwitch", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddBoolToObject(response_root, "LightSwitch", p_ct_tsl_data->LightSwitch);
|
||||
}
|
||||
else if (strcmp("NightLightSwitch", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddBoolToObject(response_root, "NightLightSwitch", p_ct_tsl_data->NightLightSwitch);
|
||||
}
|
||||
else if (strcmp("WorkMode", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddNumberToObject(response_root, "WorkMode", p_ct_tsl_data->WorkMode);
|
||||
}
|
||||
else if (strcmp("worktime", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddStringToObject(response_root, "worktime", p_ct_tsl_data->WorkTime);
|
||||
}
|
||||
else if (strcmp("Brightness", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddNumberToObject(response_root, "Brightness", p_ct_tsl_data->Brightness);
|
||||
}
|
||||
else if (strcmp("onlyread", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddNumberToObject(response_root, "onlyread", p_ct_tsl_data->readonly);
|
||||
}
|
||||
else if (strcmp("floatid", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddNumberToObject(response_root, "floatid", p_ct_tsl_data->f);
|
||||
}
|
||||
else if (strcmp("doubleid", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddNumberToObject(response_root, "doubleid", p_ct_tsl_data->d);
|
||||
}
|
||||
else if (strcmp("PropertyString", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddStringToObject(response_root, "PropertyString", p_ct_tsl_data->PropertyString);
|
||||
}
|
||||
else if (strcmp("RGBColor", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON *item_RGBColor = cJSON_CreateObject();
|
||||
if (item_RGBColor == NULL)
|
||||
{
|
||||
cJSON_Delete(request_root);
|
||||
cJSON_Delete(response_root);
|
||||
|
||||
return -1;
|
||||
}
|
||||
cJSON_AddNumberToObject(item_RGBColor, "Red", p_ct_tsl_data->RGB.R);
|
||||
cJSON_AddNumberToObject(item_RGBColor, "Green", p_ct_tsl_data->RGB.G);
|
||||
cJSON_AddNumberToObject(item_RGBColor, "Blue", p_ct_tsl_data->RGB.B);
|
||||
|
||||
cJSON_AddItemToObject(response_root, "RGBColor", item_RGBColor);
|
||||
}
|
||||
}
|
||||
cJSON_Delete(request_root);
|
||||
|
||||
*response = cJSON_PrintUnformatted(response_root);
|
||||
if (*response == NULL)
|
||||
{
|
||||
ct_info("No Enough Memory");
|
||||
cJSON_Delete(response_root);
|
||||
return -1;
|
||||
}
|
||||
cJSON_Delete(response_root);
|
||||
*response_len = strlen(*response);
|
||||
|
||||
ct_info("Property Get Response: %s", *response);
|
||||
|
||||
return SUCCESS_RETURN;
|
||||
}
|
||||
#endif
|
||||
|
||||
//When code is not 200,maybe call this function
|
||||
static int user_property_cloud_error_handler(const int code, const char *data, const char *detail)
|
||||
{
|
||||
ct_info("code =%d ,data=%s, detail=%s", code, data, detail);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 解析所有属性设置的值
|
||||
* @param request 指向属性设置请求payload的指针
|
||||
* @param request_len 属性设置请求的payload长度
|
||||
* @return 解析成功: 0, 解析失败: <0
|
||||
*/
|
||||
int32_t app_parse_property(const char *request, uint32_t request_len)
|
||||
{
|
||||
cJSON *lightswitch = NULL;
|
||||
cJSON *rgbcolor = NULL;
|
||||
cJSON *nightlightswitch = NULL;
|
||||
cJSON *workmode = NULL;
|
||||
cJSON *brightness = NULL;
|
||||
cJSON *worktime = NULL;
|
||||
cJSON *floatid = NULL;
|
||||
cJSON *doubleid = NULL;
|
||||
cJSON *propertystring = NULL;
|
||||
|
||||
cJSON *req = cJSON_Parse(request);
|
||||
if (req == NULL || !cJSON_IsObject(req))
|
||||
{
|
||||
return -0x911;
|
||||
}
|
||||
|
||||
lightswitch = cJSON_GetObjectItem(req, "LightSwitch");
|
||||
if (lightswitch != NULL && cJSON_IsNumber(lightswitch))
|
||||
{
|
||||
/* process property LightSwitch here */
|
||||
|
||||
ct_info("property id: LightSwitch, value: %d", lightswitch->valueint);
|
||||
ct_ut_set_LightSwitch(lightswitch->valueint);
|
||||
}
|
||||
|
||||
rgbcolor = cJSON_GetObjectItem(req, "RGBColor");
|
||||
if (rgbcolor != NULL && cJSON_IsObject(rgbcolor))
|
||||
{
|
||||
/* process property RGBColor here */
|
||||
cJSON *R = cJSON_GetObjectItem(rgbcolor, "Red");
|
||||
cJSON *G = cJSON_GetObjectItem(rgbcolor, "Green");
|
||||
cJSON *B = cJSON_GetObjectItem(rgbcolor, "Blue");
|
||||
|
||||
if ((R != NULL && cJSON_IsNumber(R)) &&
|
||||
(G != NULL && cJSON_IsNumber(G)) &&
|
||||
(B != NULL && cJSON_IsNumber(B)))
|
||||
{
|
||||
ct_info("struct property id: RGBColor R:%d G:%d B:%d", R->valueint, G->valueint, B->valueint);
|
||||
ct_ut_set_RGB(R->valueint, G->valueint, B->valueint);
|
||||
}
|
||||
}
|
||||
|
||||
nightlightswitch = cJSON_GetObjectItem(req, "NightLightSwitch");
|
||||
if (nightlightswitch != NULL && cJSON_IsNumber(nightlightswitch))
|
||||
{
|
||||
/* process property NightLightSwitch here */
|
||||
|
||||
ct_info("property id: NightLightSwitch, value: %d", nightlightswitch->valueint);
|
||||
ct_ut_set_NightLightSwitch(nightlightswitch->valueint);
|
||||
}
|
||||
|
||||
workmode = cJSON_GetObjectItem(req, "WorkMode");
|
||||
if (workmode != NULL && cJSON_IsNumber(workmode))
|
||||
{
|
||||
/* process property WorkMode here */
|
||||
|
||||
ct_info("property id: WorkMode, value: %d", workmode->valueint);
|
||||
ct_ut_set_WorkMode(workmode->valueint);
|
||||
}
|
||||
|
||||
brightness = cJSON_GetObjectItem(req, "Brightness");
|
||||
if (brightness != NULL && cJSON_IsNumber(brightness))
|
||||
{
|
||||
/* process property Brightness here */
|
||||
|
||||
ct_info("property id: Brightness, value: %d", brightness->valueint);
|
||||
ct_ut_set_Brightness(brightness->valueint);
|
||||
}
|
||||
|
||||
worktime = cJSON_GetObjectItem(req, "worktime");
|
||||
if (worktime != NULL && cJSON_IsString(worktime))
|
||||
{
|
||||
/* process property worktime here */
|
||||
|
||||
ct_info("property id: worktime, value: %s", worktime->valuestring);
|
||||
ct_ut_set_WorkTime(worktime->valuestring);
|
||||
}
|
||||
|
||||
floatid = cJSON_GetObjectItem(req, "floatid");
|
||||
if (floatid != NULL && cJSON_IsNumber(floatid))
|
||||
{
|
||||
/* process property float here */
|
||||
|
||||
ct_info("property id: float, value: %f", floatid->valuedouble);
|
||||
ct_ut_set_Float(floatid->valuedouble);
|
||||
}
|
||||
|
||||
doubleid = cJSON_GetObjectItem(req, "doubleid");
|
||||
if (doubleid != NULL && cJSON_IsNumber(doubleid))
|
||||
{
|
||||
/* process property double here */
|
||||
|
||||
ct_info("property id: double, value: %f", doubleid->valuedouble);
|
||||
ct_ut_set_Double(doubleid->valuedouble);
|
||||
}
|
||||
|
||||
propertystring = cJSON_GetObjectItem(req, "PropertyString");
|
||||
if (propertystring != NULL && cJSON_IsString(propertystring))
|
||||
{
|
||||
/* process property PropertyString here */
|
||||
|
||||
ct_info("property id: PropertyString, value: %s", propertystring->valuestring);
|
||||
ct_ut_set_PropertyString(propertystring->valuestring);
|
||||
}
|
||||
|
||||
cJSON_Delete(req);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_property_set_event_handler(const int devid, const char *request, const int request_len)
|
||||
{
|
||||
int res = 0;
|
||||
ct_ctx_t *ct_ctx = ct_get_ctx();
|
||||
ct_info("Property Set Received, Devid: %d, Request: %s", devid, request);
|
||||
|
||||
app_parse_property(request, request_len);
|
||||
res = IOT_Linkkit_Report(ct_ctx->master_devid, ITM_MSG_POST_PROPERTY,
|
||||
(unsigned char *)request, request_len);
|
||||
|
||||
ct_info("Post Property Message ID: %d", res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_report_reply_event_handler(const int devid, const int msgid, const int code, const char *reply,
|
||||
const int reply_len)
|
||||
{
|
||||
const char *reply_value = (reply == NULL) ? ("NULL") : (reply);
|
||||
const int reply_value_len = (reply_len == 0) ? (strlen("NULL")) : (reply_len);
|
||||
|
||||
ct_info("Message Post Reply Received, Devid: %d, Message ID: %d, Code: %d, Reply: %.*s", devid, msgid, code,
|
||||
reply_value_len,
|
||||
reply_value);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_trigger_event_reply_event_handler(const int devid, const int msgid, const int code, const char *eventid,
|
||||
const int eventid_len, const char *message, const int message_len)
|
||||
{
|
||||
ct_info("Trigger Event Reply Received, Devid: %d, Message ID: %d, Code: %d, EventID: %.*s, Message: %.*s", devid,
|
||||
msgid, code,
|
||||
eventid_len,
|
||||
eventid, message_len, message);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_timestamp_reply_event_handler(const char *timestamp)
|
||||
{
|
||||
ct_info("Current Timestamp: %s", timestamp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint64_t user_update_sec(void)
|
||||
{
|
||||
static uint64_t time_start_ms = 0;
|
||||
|
||||
if (time_start_ms == 0)
|
||||
{
|
||||
time_start_ms = HAL_UptimeMs();
|
||||
}
|
||||
|
||||
return (HAL_UptimeMs() - time_start_ms) / 1000;
|
||||
}
|
||||
|
||||
static int ct_ut_query_timestamp(void)
|
||||
{
|
||||
ct_ctx_t *ct_ctx = ct_get_ctx();
|
||||
ct_info("do query timestamp");
|
||||
|
||||
IOT_Linkkit_Query(ct_ctx->master_devid, ITM_MSG_QUERY_TIMESTAMP, NULL, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_initialized(const int devid)
|
||||
{
|
||||
ct_ctx_t *ct_ctx = ct_get_ctx();
|
||||
ct_info("Device Initialized, Devid: %d", devid);
|
||||
|
||||
if (ct_ctx->master_devid == devid)
|
||||
{
|
||||
ct_ctx->master_initialized = 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_master_dev_available(void)
|
||||
{
|
||||
ct_ctx_t *ct_ctx = ct_get_ctx();
|
||||
|
||||
if (ct_ctx->cloud_connected && ct_ctx->master_initialized)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef CT_FY_SDK_VERSION_1_3_OR_1_4
|
||||
static int user_event_notify_handler(const int devid, const char *request, const int request_len)
|
||||
{
|
||||
int res = 0;
|
||||
ct_ctx_t *ct_ctx = ct_get_ctx();
|
||||
ct_info("Event notify Received, Devid: %d, Request: %s", devid, request);
|
||||
|
||||
res = IOT_Linkkit_Report(ct_ctx->master_devid, ITM_MSG_EVENT_NOTIFY_REPLY,
|
||||
(unsigned char *)request, request_len);
|
||||
ct_info("Post Property Message ID: %d", res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int user_fota_event_handler(int type, const char *version)
|
||||
{
|
||||
char *p_fota_buffer = NULL;
|
||||
ct_ctx_t *ct_ctx = ct_get_ctx();
|
||||
|
||||
p_fota_buffer = HAL_Malloc(CT_OTA_BUFFER_LEN);
|
||||
if (!p_fota_buffer)
|
||||
{
|
||||
ct_err("no mem");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (type == 0)
|
||||
{
|
||||
ct_info("New Firmware Version: %s", version);
|
||||
memset(p_fota_buffer, 0, CT_OTA_BUFFER_LEN);
|
||||
IOT_Linkkit_Query(ct_ctx->master_devid, ITM_MSG_QUERY_FOTA_DATA, (unsigned char *)p_fota_buffer, CT_OTA_BUFFER_LEN);
|
||||
}
|
||||
|
||||
if (p_fota_buffer)
|
||||
HAL_Free(p_fota_buffer);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_cota_event_handler(int type, const char *config_id, int config_size, const char *get_type,
|
||||
const char *sign, const char *sign_method, const char *url)
|
||||
{
|
||||
char *p_cota_buffer = NULL;
|
||||
ct_ctx_t *ct_ctx = ct_get_ctx();
|
||||
|
||||
p_cota_buffer = HAL_Malloc(CT_OTA_BUFFER_LEN);
|
||||
if (!p_cota_buffer)
|
||||
{
|
||||
ct_err("no mem");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (type == 0)
|
||||
{
|
||||
ct_info("New Config ID: %s", config_id);
|
||||
ct_info("New Config Size: %d", config_size);
|
||||
ct_info("New Config Type: %s", get_type);
|
||||
ct_info("New Config Sign: %s", sign);
|
||||
ct_info("New Config Sign Method: %s", sign_method);
|
||||
ct_info("New Config URL: %s", url);
|
||||
|
||||
IOT_Linkkit_Query(ct_ctx->master_devid, ITM_MSG_QUERY_COTA_DATA, (unsigned char *)p_cota_buffer, CT_OTA_BUFFER_LEN);
|
||||
}
|
||||
|
||||
if (p_cota_buffer)
|
||||
HAL_Free(p_cota_buffer);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef CT_FY_SDK_VERSION_1_5
|
||||
static int user_offline_reset_handler(void)
|
||||
{
|
||||
ct_info("user callback user_offline_reset_handler called.");
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef CT_FY_SDK_VERSION_1_3_OR_1_4
|
||||
static int user_dev_bind_event(const int state_code, const char *state_message)
|
||||
{
|
||||
ct_info("state_code: -0x%04x, str_msg= %s", -state_code, state_message == NULL ? "NULL" : state_message);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef DM_UNIFIED_SERVICE_POST
|
||||
static int user_unified_service_post_reply_handler(const int devid, const int id, const int code, const char *payload, const int payload_len)
|
||||
{
|
||||
ct_info("Receive unified service post reply, code:%d, payload:%s", code, payload);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static iotx_linkkit_dev_meta_info_t *ct_main_init(ct_ctx_t *ct_ctx)
|
||||
{
|
||||
int register_type = 0;
|
||||
int post_event_reply = 1;
|
||||
iotx_linkkit_dev_meta_info_t *p_master_meta = NULL;
|
||||
|
||||
memset(ct_ctx, 0, sizeof(ct_ctx_t));
|
||||
|
||||
p_master_meta = HAL_Malloc(sizeof(iotx_linkkit_dev_meta_info_t));
|
||||
if (p_master_meta == NULL)
|
||||
{
|
||||
ct_err("no mem");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(p_master_meta, 0, sizeof(iotx_linkkit_dev_meta_info_t));
|
||||
HAL_GetProductKey(p_master_meta->product_key);
|
||||
HAL_GetDeviceName(p_master_meta->device_name);
|
||||
HAL_GetDeviceSecret(p_master_meta->device_secret);
|
||||
HAL_GetProductSecret(p_master_meta->product_secret);
|
||||
|
||||
if ((0 == strlen(p_master_meta->product_key)) || (0 == strlen(p_master_meta->device_name)) ||
|
||||
(0 == register_type && (0 == strlen(p_master_meta->device_secret))) || (0 == strlen(p_master_meta->product_secret)))
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
printf("Master meta info is invalid...\r\n");
|
||||
printf("pk[%s]\r\n", p_master_meta->product_key);
|
||||
printf("ps[%s]\r\n", p_master_meta->product_secret);
|
||||
printf("dn[%s]\r\n", p_master_meta->device_name);
|
||||
printf("ds[%s]\r\n", p_master_meta->device_secret);
|
||||
HAL_SleepMs(2000);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef CT_PRODUCT_DYNAMIC_REGISTER_AND_USE_RAWDATA
|
||||
register_type = 1;
|
||||
#endif
|
||||
|
||||
/* Register Callback */
|
||||
IOT_RegisterCallback(ITE_INITIALIZE_COMPLETED, user_initialized);
|
||||
IOT_RegisterCallback(ITE_CONNECT_SUCC, user_connected_event_handler);
|
||||
IOT_RegisterCallback(ITE_DISCONNECTED, user_disconnected_event_handler);
|
||||
IOT_RegisterCallback(ITE_RAWDATA_ARRIVED, user_down_raw_data_arrived_event_handler);
|
||||
IOT_RegisterCallback(ITE_SERVICE_REQUEST, user_service_request_event_handler);
|
||||
IOT_RegisterCallback(ITE_PROPERTY_SET, user_property_set_event_handler);
|
||||
#ifdef ALCS_ENABLED
|
||||
/*Only for local communication service(ALCS)*/
|
||||
IOT_RegisterCallback(ITE_PROPERTY_GET, user_property_get_event_handler);
|
||||
#endif
|
||||
IOT_RegisterCallback(ITE_REPORT_REPLY, user_report_reply_event_handler);
|
||||
IOT_RegisterCallback(ITE_TRIGGER_EVENT_REPLY, user_trigger_event_reply_event_handler);
|
||||
IOT_RegisterCallback(ITE_TIMESTAMP_REPLY, user_timestamp_reply_event_handler);
|
||||
|
||||
IOT_RegisterCallback(ITE_CLOUD_ERROR, user_property_cloud_error_handler);
|
||||
|
||||
IOT_RegisterCallback(ITE_FOTA, user_fota_event_handler);
|
||||
IOT_RegisterCallback(ITE_COTA, user_cota_event_handler);
|
||||
#ifdef CT_FY_SDK_VERSION_1_3_OR_1_4
|
||||
IOT_RegisterCallback(ITE_EVENT_NOTIFY, user_event_notify_handler);
|
||||
IOT_RegisterCallback(ITE_STATE_DEV_BIND, user_dev_bind_event);
|
||||
#endif
|
||||
|
||||
#ifdef CT_FY_SDK_VERSION_1_5
|
||||
IOT_RegisterCallback(ITE_OFFLINE_RESET, user_offline_reset_handler);
|
||||
#endif
|
||||
|
||||
#ifdef CT_FY_SDK_VERSION_1_3_OR_1_4
|
||||
#ifdef DM_UNIFIED_SERVICE_POST
|
||||
IOT_RegisterCallback(ITE_UNIFIED_SERVICE_POST, user_unified_service_post_reply_handler);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
IOT_Ioctl(IOTX_IOCTL_SET_DYNAMIC_REGISTER, (void *)®ister_type);
|
||||
|
||||
/* Choose Whether You Need Post Property/Event Reply */
|
||||
IOT_Ioctl(IOTX_IOCTL_RECV_EVENT_REPLY, (void *)&post_event_reply);
|
||||
|
||||
ct_ut_init();
|
||||
|
||||
return p_master_meta;
|
||||
}
|
||||
|
||||
int ct_main(void *paras)
|
||||
{
|
||||
int res = 0;
|
||||
uint64_t time_prev_sec = 0, time_now_sec = 0;
|
||||
ct_ctx_t *ct_ctx = ct_get_ctx();
|
||||
iotx_linkkit_dev_meta_info_t *p_master_meta = NULL;
|
||||
|
||||
p_master_meta = ct_main_init(ct_ctx);
|
||||
if (NULL == p_master_meta)
|
||||
{
|
||||
printf("OOPS:ct_main_init failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Create Master Device Resources */
|
||||
do
|
||||
{
|
||||
ct_ctx->master_devid = IOT_Linkkit_Open(IOTX_LINKKIT_DEV_TYPE_MASTER, p_master_meta);
|
||||
if (ct_ctx->master_devid < 0)
|
||||
{
|
||||
printf("IOT_Linkkit_Open Failed, retry after 5s...\r\n");
|
||||
HAL_SleepMs(5000);
|
||||
}
|
||||
} while (ct_ctx->master_devid < 0);
|
||||
/* Start Connect Aliyun Server */
|
||||
do
|
||||
{
|
||||
res = IOT_Linkkit_Connect(ct_ctx->master_devid);
|
||||
if (res < 0)
|
||||
{
|
||||
printf("IOT_Linkkit_Connect Failed, retry after 5s...\r\n");
|
||||
HAL_SleepMs(5000);
|
||||
}
|
||||
} while (res < 0);
|
||||
|
||||
ct_ut_query_timestamp();
|
||||
|
||||
while (1)
|
||||
{
|
||||
IOT_Linkkit_Yield(CT_YIELD_TIMEOUT_MS);
|
||||
|
||||
time_now_sec = user_update_sec();
|
||||
if (time_prev_sec == time_now_sec)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (user_master_dev_available())
|
||||
{
|
||||
ct_ut_misc_process(time_now_sec);
|
||||
}
|
||||
|
||||
time_prev_sec = time_now_sec;
|
||||
}
|
||||
|
||||
//Should never come here
|
||||
ct_ctx->g_user_dispatch_thread_running = 0;
|
||||
|
||||
if (p_master_meta)
|
||||
{
|
||||
HAL_Free(p_master_meta);
|
||||
}
|
||||
|
||||
IOT_DumpMemoryStats(IOT_LOG_DEBUG);
|
||||
|
||||
return 0;
|
||||
}
|
||||
38
Products/example/certification/ct_main.h
Normal file
38
Products/example/certification/ct_main.h
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#ifndef __CT_MAIN_H__
|
||||
#define __CT_MAIN_H__
|
||||
|
||||
#include <iotx_log.h>
|
||||
|
||||
//#define CT_PRODUCT_DYNAMIC_REGISTER_AND_USE_RAWDATA
|
||||
|
||||
//if your fy sdk is V1.0.0 or V 1.1.0
|
||||
//#define CT_FY_SDK_VERSION_1_0_OR_1_1
|
||||
|
||||
//if your fy sdk is V1.3.0 or V 1.4.0
|
||||
#define CT_FY_SDK_VERSION_1_3_OR_1_4
|
||||
|
||||
//if your fy sdk is V1.5.0
|
||||
//#define CT_FY_SDK_VERSION_1_5
|
||||
|
||||
#define ct_debug(...) log_debug("ct", __VA_ARGS__)
|
||||
#define ct_info(...) log_info("ct", __VA_ARGS__)
|
||||
#define ct_warn(...) log_warning("ct", __VA_ARGS__)
|
||||
#define ct_err(...) log_err("ct", __VA_ARGS__)
|
||||
#define ct_crit(...) log_crit("ct", __VA_ARGS__)
|
||||
|
||||
#define CT_YIELD_TIMEOUT_MS (200)
|
||||
#define CT_OTA_BUFFER_LEN (512)
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int master_devid;
|
||||
int cloud_connected;
|
||||
int master_initialized;
|
||||
void *g_user_dispatch_thread;
|
||||
int g_user_dispatch_thread_running;
|
||||
} ct_ctx_t;
|
||||
|
||||
extern int ct_main(void *paras);
|
||||
extern ct_ctx_t *ct_get_ctx(void);
|
||||
|
||||
#endif
|
||||
184
Products/example/certification/ct_ota.c
Normal file
184
Products/example/certification/ct_ota.c
Normal file
|
|
@ -0,0 +1,184 @@
|
|||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "cJSON.h"
|
||||
#include "iot_import.h"
|
||||
#include "iotx_log.h"
|
||||
|
||||
#include "ct_main.h"
|
||||
#include "ct_entry.h"
|
||||
#include "ct_ut.h"
|
||||
|
||||
#ifdef OTA_ENABLED
|
||||
#ifdef BUILD_AOS
|
||||
#include "ota_service.h"
|
||||
#else
|
||||
#include "dm_ota.h"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include "ct_ota.h"
|
||||
|
||||
#ifdef OTA_ENABLED
|
||||
|
||||
static int ota_get_kv_size(void)
|
||||
{
|
||||
int ret = 0;
|
||||
char ota_size[OTA_DESC_KV_KEY_SIZE_LEN] = {0};
|
||||
int len = OTA_DESC_KV_KEY_SIZE_LEN - 1;
|
||||
|
||||
ret = HAL_Kv_Get(OTA_DESC_KV_KEY_SIZE, ota_size, &len);
|
||||
if (ret == 0)
|
||||
{
|
||||
return atoi(ota_size);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int ota_get_kv_version(char version[OTA_DESC_KV_KEY_VERSION_LEN])
|
||||
{
|
||||
int ret = 0;
|
||||
char ota_ver[OTA_DESC_KV_KEY_VERSION_LEN] = {0};
|
||||
int len = OTA_DESC_KV_KEY_VERSION_LEN - 1;
|
||||
|
||||
ret = HAL_Kv_Get(OTA_DESC_KV_KEY_VERSION, ota_ver, &len);
|
||||
if (ret == 0)
|
||||
{
|
||||
memcpy(version, ota_ver, strlen(ota_ver));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int ota_get_kv_md5(char md5[OTA_DESC_KV_KEY_MD5_LEN])
|
||||
{
|
||||
int ret = 0;
|
||||
char ota_md5[OTA_DESC_KV_KEY_MD5_LEN] = {0};
|
||||
int len = OTA_DESC_KV_KEY_MD5_LEN - 1;
|
||||
|
||||
ret = HAL_Kv_Get(OTA_DESC_KV_KEY_MD5, ota_md5, &len);
|
||||
if (ret == 0)
|
||||
{
|
||||
memcpy(md5, ota_md5, strlen(ota_md5));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int ota_get_kv_url(char url[OTA_DESC_KV_KEY_URL_LEN])
|
||||
{
|
||||
int ret = 0;
|
||||
char ota_url[OTA_DESC_KV_KEY_URL_LEN] = {0};
|
||||
int len = OTA_DESC_KV_KEY_URL_LEN - 1;
|
||||
|
||||
ret = HAL_Kv_Get(OTA_DESC_KV_KEY_URL, ota_url, &len);
|
||||
if (ret == 0)
|
||||
{
|
||||
memcpy(url, ota_url, strlen(ota_url));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifdef BUILD_AOS
|
||||
int ct_ota_download(ota_service_t *p_ota_ctx)
|
||||
{
|
||||
int ret = 0;
|
||||
char *p_ota_desc_data = NULL;
|
||||
int size = 0;
|
||||
char version[OTA_DESC_KV_KEY_VERSION_LEN] = {0};
|
||||
char md5[OTA_DESC_KV_KEY_MD5_LEN] = {0};
|
||||
char url[OTA_DESC_KV_KEY_URL_LEN] = {0};
|
||||
|
||||
if (p_ota_ctx == NULL)
|
||||
{
|
||||
ct_err("param err");
|
||||
return -1;
|
||||
}
|
||||
|
||||
size = ota_get_kv_size();
|
||||
ota_get_kv_version(version);
|
||||
ota_get_kv_md5(md5);
|
||||
ota_get_kv_url(url);
|
||||
|
||||
if (strlen(md5) < 32 || strlen(version) < 1 ||
|
||||
strlen(url) < 10 || size < 1)
|
||||
{
|
||||
ct_err("err ota:size(%d) version(%s) md5(%s) url(%s)", size, version, md5, url);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ct_info("ct ota:size(%d) version(%s) md5(%s) url(%s)", size, version, md5, url);
|
||||
p_ota_desc_data = HAL_Malloc(OTA_DESC_DATA_LEN);
|
||||
if (!p_ota_desc_data)
|
||||
{
|
||||
ct_err("no mem");
|
||||
return -1;
|
||||
}
|
||||
|
||||
HAL_Snprintf(p_ota_desc_data, OTA_DESC_DATA_LEN,
|
||||
OTA_DESC_DATA_FMT, size, md5,
|
||||
version, url, md5);
|
||||
|
||||
ret = p_ota_ctx->upgrade_cb(p_ota_ctx, p_ota_desc_data);
|
||||
|
||||
if (p_ota_desc_data)
|
||||
{
|
||||
HAL_Free(p_ota_desc_data);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
#else
|
||||
int ct_ota_download(void)
|
||||
{
|
||||
int ret = 0;
|
||||
char *p_ota_desc_data = NULL;
|
||||
int size = 0;
|
||||
char version[OTA_DESC_KV_KEY_VERSION_LEN] = {0};
|
||||
char md5[OTA_DESC_KV_KEY_MD5_LEN] = {0};
|
||||
char url[OTA_DESC_KV_KEY_URL_LEN] = {0};
|
||||
|
||||
size = ota_get_kv_size();
|
||||
ota_get_kv_version(version);
|
||||
ota_get_kv_md5(md5);
|
||||
ota_get_kv_url(url);
|
||||
|
||||
if (strlen(md5) < 32 || strlen(version) < 1 ||
|
||||
strlen(url) < 10 || size < 1)
|
||||
{
|
||||
ct_err("param err");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ct_info("ct ota:size(%d) version(%s) md5(%s) url(%s)", size, version, md5, url);
|
||||
p_ota_desc_data = HAL_Malloc(OTA_DESC_DATA_LEN);
|
||||
if (!p_ota_desc_data)
|
||||
{
|
||||
ct_err("no mem");
|
||||
return -1;
|
||||
}
|
||||
|
||||
HAL_Snprintf(p_ota_desc_data, OTA_DESC_DATA_LEN,
|
||||
OTA_DESC_DATA_FMT, size, md5,
|
||||
version, url, md5);
|
||||
|
||||
//If not fy 1.5.0 or larger version,please add dm_ota_download to dm_ota.c
|
||||
#ifdef CT_FY_SDK_VERSION_1_5
|
||||
ret = dm_ota_download(p_ota_desc_data, strlen(p_ota_desc_data));
|
||||
#endif
|
||||
|
||||
if (p_ota_desc_data)
|
||||
{
|
||||
HAL_Free(p_ota_desc_data);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
29
Products/example/certification/ct_ota.h
Normal file
29
Products/example/certification/ct_ota.h
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#ifndef __CT_OTA_H__
|
||||
#define __CT_OTA_H__
|
||||
|
||||
//#define CT_DOWNLOAD_OTA_WHEN_CONNECT_CLOUD
|
||||
|
||||
#ifdef OTA_ENABLED
|
||||
|
||||
#define OTA_DESC_DATA_LEN (1024)
|
||||
#define OTA_DESC_DATA_FMT "{\"code\":\"1000\",\"data\":{\"size\":%d,\"sign\":\"%s\",\"version\":\"%s\",\"url\":\"%s\",\"signMethod\":\"Md5\",\"md5\":\"%s\"},\"id\":1583301049684,\"message\":\"success\"}"
|
||||
|
||||
#define OTA_DESC_KV_KEY_SIZE "ct_ota_size"
|
||||
#define OTA_DESC_KV_KEY_VERSION "ct_ota_ver"
|
||||
#define OTA_DESC_KV_KEY_MD5 "ct_ota_md5"
|
||||
#define OTA_DESC_KV_KEY_URL "ct_ota_url"
|
||||
|
||||
#define OTA_DESC_KV_KEY_SIZE_LEN (16)
|
||||
#define OTA_DESC_KV_KEY_VERSION_LEN (64)
|
||||
#define OTA_DESC_KV_KEY_MD5_LEN (33)
|
||||
#define OTA_DESC_KV_KEY_URL_LEN (1024)
|
||||
|
||||
#ifdef BUILD_AOS
|
||||
#include "ota_service.h"
|
||||
int ct_ota_download(ota_service_t *p_ota_ctx);
|
||||
#else
|
||||
int ct_ota_download(void);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#endif
|
||||
707
Products/example/certification/ct_ut.c
Normal file
707
Products/example/certification/ct_ut.c
Normal file
|
|
@ -0,0 +1,707 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2019 Alibaba Group Holding Limited
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "cJSON.h"
|
||||
#include "iot_import.h"
|
||||
#include "iot_export_linkkit.h"
|
||||
#include "iotx_log.h"
|
||||
|
||||
#include "ct_main.h"
|
||||
#include "ct_entry.h"
|
||||
#include "ct_ut.h"
|
||||
#include "ct_ota.h"
|
||||
|
||||
#if defined(CT_DOWNLOAD_OTA_WHEN_CONNECT_CLOUD) && defined(OTA_ENABLED)
|
||||
static char do_only_once = 0;
|
||||
#endif
|
||||
|
||||
static ct_tsl_t ct_tsl_data;
|
||||
|
||||
ct_tsl_t *ct_ut_get_tsl_data(void)
|
||||
{
|
||||
return &ct_tsl_data;
|
||||
}
|
||||
|
||||
int ct_ut_set_LightSwitch(char LightSwitch)
|
||||
{
|
||||
ct_tsl_data.LightSwitch = LightSwitch;
|
||||
ct_info("set LightSwitch:%s", (LightSwitch == 0) ? "off" : "on");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ct_ut_get_LightSwitch(void)
|
||||
{
|
||||
return ct_tsl_data.LightSwitch;
|
||||
}
|
||||
|
||||
int ct_ut_set_NightLightSwitch(char NightLightSwitch)
|
||||
{
|
||||
ct_tsl_data.NightLightSwitch = NightLightSwitch;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ct_ut_set_WorkMode(unsigned char WorkMode)
|
||||
{
|
||||
ct_tsl_data.WorkMode = WorkMode;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ct_ut_set_Brightness(unsigned char Brightness)
|
||||
{
|
||||
ct_tsl_data.Brightness = Brightness;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ct_ut_set_RGB(unsigned char R, unsigned char G, unsigned char B)
|
||||
{
|
||||
ct_tsl_data.RGB.R = R;
|
||||
ct_tsl_data.RGB.G = G;
|
||||
ct_tsl_data.RGB.B = B;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ct_ut_set_Float(float f)
|
||||
{
|
||||
ct_tsl_data.f = f;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ct_ut_set_Double(double d)
|
||||
{
|
||||
ct_tsl_data.d = d;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ct_ut_set_PropertyString(char *PropertyString)
|
||||
{
|
||||
if (PropertyString)
|
||||
{
|
||||
HAL_Snprintf(ct_tsl_data.PropertyString, PROPERTY_STRING_MAX_LEN, "%s", PropertyString);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ct_ut_set_WorkTime(char *WorkTime)
|
||||
{
|
||||
if (WorkTime)
|
||||
{
|
||||
HAL_Snprintf(ct_tsl_data.WorkTime, WORKTIME_MAX_LEN, "%s", WorkTime);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报属性LightSwitch到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 属性LightSwitch的值,数据类型uint8_t
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_property_LightSwitch(uint32_t devid, uint8_t value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char property_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(property_payload, sizeof(property_payload), "{\"LightSwitch\": %d}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报属性WIFI_Band到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 属性WIFI_Band的值,数据类型char*
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_property_WIFI_Band(uint32_t devid, char *value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char *property_payload = NULL;
|
||||
uint32_t property_payload_len = 0;
|
||||
|
||||
if (value == NULL)
|
||||
{
|
||||
return -0x101;
|
||||
}
|
||||
|
||||
property_payload_len = strlen("WIFI_Band") + strlen(value) + 10;
|
||||
property_payload = HAL_Malloc(property_payload_len);
|
||||
if (property_payload == NULL)
|
||||
{
|
||||
return -0x201;
|
||||
}
|
||||
memset(property_payload, 0, property_payload_len);
|
||||
|
||||
res = HAL_Snprintf(property_payload, property_payload_len, "{\"WIFI_Band\": \"%s\"}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
HAL_Free(property_payload);
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报属性WiFI_RSSI到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 属性WiFI_RSSI的值,数据类型int32_t
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_property_WiFI_RSSI(uint32_t devid, int32_t value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char property_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(property_payload, sizeof(property_payload), "{\"WiFI_RSSI\": %d}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报属性WIFI_AP_BSSID到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 属性WIFI_AP_BSSID的值,数据类型char*
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_property_WIFI_AP_BSSID(uint32_t devid, char *value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char *property_payload = NULL;
|
||||
uint32_t property_payload_len = 0;
|
||||
|
||||
if (value == NULL)
|
||||
{
|
||||
return -0x101;
|
||||
}
|
||||
|
||||
property_payload_len = strlen("WIFI_AP_BSSID") + strlen(value) + 10;
|
||||
property_payload = HAL_Malloc(property_payload_len);
|
||||
if (property_payload == NULL)
|
||||
{
|
||||
return -0x201;
|
||||
}
|
||||
memset(property_payload, 0, property_payload_len);
|
||||
|
||||
res = HAL_Snprintf(property_payload, property_payload_len, "{\"WIFI_AP_BSSID\": \"%s\"}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
HAL_Free(property_payload);
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报属性WIFI_Channel到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 属性WIFI_Channel的值,数据类型int32_t
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_property_WIFI_Channel(uint32_t devid, int32_t value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char property_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(property_payload, sizeof(property_payload), "{\"WIFI_Channel\": %d}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报属性WiFI_SNR到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 属性WiFI_SNR的值,数据类型int32_t
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_property_WiFI_SNR(uint32_t devid, int32_t value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char property_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(property_payload, sizeof(property_payload), "{\"WiFI_SNR\": %d}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报属性NightLightSwitch到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 属性NightLightSwitch的值,数据类型uint8_t
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_property_NightLightSwitch(uint32_t devid, uint8_t value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char property_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(property_payload, sizeof(property_payload), "{\"NightLightSwitch\": %d}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报属性WorkMode到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 属性WorkMode的值,数据类型uint32_t
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_property_WorkMode(uint32_t devid, uint32_t value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char property_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(property_payload, sizeof(property_payload), "{\"WorkMode\": %d}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报属性Brightness到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 属性Brightness的值,数据类型int32_t
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_property_Brightness(uint32_t devid, int32_t value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char property_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(property_payload, sizeof(property_payload), "{\"Brightness\": %d}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报属性worktime到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param utc 属性worktime的值,数据类型${dataType}
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_property_worktime(uint32_t devid, char *worktime)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char property_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(property_payload, sizeof(property_payload), "{\"worktime\": \"%s\"}", worktime);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
return res;
|
||||
}
|
||||
|
||||
int32_t app_post_property_RGB(uint32_t devid, unsigned char R, unsigned char G, unsigned char B)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char property_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(property_payload, sizeof(property_payload), "{\"RGBColor\": {\"Red\": %d, \"Green\": %d, \"Blue\": %d}}", R, G, B);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报属性onlyread到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 属性onlyread的值,数据类型int32_t
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_property_onlyread(uint32_t devid, int32_t value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char property_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(property_payload, sizeof(property_payload), "{\"onlyread\": %d}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报属性float到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 属性float的值,数据类型float
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_property_float(uint32_t devid, float value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char property_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(property_payload, sizeof(property_payload), "{\"floatid\": %f}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报属性double到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 属性double的值,数据类型double
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_property_double(uint32_t devid, double value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char property_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(property_payload, sizeof(property_payload), "{\"doubleid\": %f}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报属性PropertyString到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 属性PropertyString的值,数据类型char*
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_property_PropertyString(uint32_t devid, char *value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char *property_payload = NULL;
|
||||
uint32_t property_payload_len = 0;
|
||||
|
||||
if (value == NULL)
|
||||
{
|
||||
return -0x101;
|
||||
}
|
||||
|
||||
property_payload_len = strlen("PropertyString") + strlen(value) + 10;
|
||||
property_payload = HAL_Malloc(property_payload_len);
|
||||
if (property_payload == NULL)
|
||||
{
|
||||
return -0x201;
|
||||
}
|
||||
memset(property_payload, 0, property_payload_len);
|
||||
|
||||
res = HAL_Snprintf(property_payload, property_payload_len, "{\"PropertyString\": \"%s\"}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
HAL_Free(property_payload);
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
HAL_Free(property_payload);
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报事件Error到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 事件Error的值,数据类型uint32_t
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_event_Error(uint32_t devid, uint32_t value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char *event_id = "Error";
|
||||
char event_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(event_payload, sizeof(event_payload), "{\"ErrorCode\": %d}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_TriggerEvent(EXAMPLE_MASTER_DEVID, event_id, strlen(event_id),
|
||||
event_payload, strlen(event_payload));
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报事件alarm到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 事件alarm的值,数据类型int32_t
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_event_alarm(uint32_t devid, int32_t value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char *event_id = "alarm";
|
||||
char event_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(event_payload, sizeof(event_payload), "{\"alarm\": %d}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_TriggerEvent(EXAMPLE_MASTER_DEVID, event_id, strlen(event_id),
|
||||
event_payload, strlen(event_payload));
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报事件info到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 事件info的值,数据类型int32_t
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_event_info(uint32_t devid, int32_t value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char *event_id = "info";
|
||||
char event_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(event_payload, sizeof(event_payload), "{\"info\": %d}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_TriggerEvent(EXAMPLE_MASTER_DEVID, event_id, strlen(event_id),
|
||||
event_payload, strlen(event_payload));
|
||||
return res;
|
||||
}
|
||||
|
||||
//Just for reference
|
||||
void user_post_property(void)
|
||||
{
|
||||
static int count = 0;
|
||||
|
||||
app_post_property_LightSwitch(EXAMPLE_MASTER_DEVID, ct_tsl_data.LightSwitch);
|
||||
app_post_property_WIFI_Band(EXAMPLE_MASTER_DEVID, ct_tsl_data.wifi.band);
|
||||
app_post_property_WiFI_RSSI(EXAMPLE_MASTER_DEVID, ct_tsl_data.wifi.rssi);
|
||||
app_post_property_WIFI_AP_BSSID(EXAMPLE_MASTER_DEVID, ct_tsl_data.wifi.bssid);
|
||||
app_post_property_WIFI_Channel(EXAMPLE_MASTER_DEVID, ct_tsl_data.wifi.Channel);
|
||||
app_post_property_WiFI_SNR(EXAMPLE_MASTER_DEVID, ct_tsl_data.wifi.SNR);
|
||||
app_post_property_NightLightSwitch(EXAMPLE_MASTER_DEVID, ct_tsl_data.NightLightSwitch);
|
||||
app_post_property_WorkMode(EXAMPLE_MASTER_DEVID, ct_tsl_data.WorkMode);
|
||||
app_post_property_Brightness(EXAMPLE_MASTER_DEVID, ct_tsl_data.Brightness);
|
||||
app_post_property_worktime(EXAMPLE_MASTER_DEVID, ct_tsl_data.WorkTime);
|
||||
app_post_property_RGB(EXAMPLE_MASTER_DEVID, ct_tsl_data.RGB.R, ct_tsl_data.RGB.G, ct_tsl_data.RGB.B);
|
||||
app_post_property_onlyread(EXAMPLE_MASTER_DEVID, ct_tsl_data.readonly);
|
||||
app_post_property_float(EXAMPLE_MASTER_DEVID, ct_tsl_data.f);
|
||||
app_post_property_double(EXAMPLE_MASTER_DEVID, ct_tsl_data.d);
|
||||
app_post_property_PropertyString(EXAMPLE_MASTER_DEVID, ct_tsl_data.PropertyString);
|
||||
|
||||
if (count == 0)
|
||||
{
|
||||
app_post_event_Error(EXAMPLE_MASTER_DEVID, count);
|
||||
}
|
||||
else if (count == 1)
|
||||
{
|
||||
app_post_event_alarm(EXAMPLE_MASTER_DEVID, count);
|
||||
}
|
||||
else if (count == 2)
|
||||
{
|
||||
app_post_event_info(EXAMPLE_MASTER_DEVID, count);
|
||||
}
|
||||
|
||||
if (++count > 2)
|
||||
{
|
||||
count = 0;
|
||||
}
|
||||
}
|
||||
|
||||
//Just for reference
|
||||
void user_post_raw_data(void)
|
||||
{
|
||||
static int id = 0;
|
||||
int res = 0;
|
||||
unsigned char payload[6] = {0};
|
||||
ct_ctx_t *ct_ctx = ct_get_ctx();
|
||||
|
||||
id += 1;
|
||||
payload[0] = 0x01;
|
||||
payload[1] = (id >> 24) & 0xFF;
|
||||
payload[2] = (id >> 16) & 0xFF;
|
||||
payload[3] = (id >> 8) & 0xFF;
|
||||
payload[4] = id & 0xFF;
|
||||
payload[5] = ct_tsl_data.LightSwitch;
|
||||
|
||||
res = IOT_Linkkit_Report(ct_ctx->master_devid, ITM_MSG_POST_RAW_DATA,
|
||||
payload, 6);
|
||||
|
||||
ct_info("Post Raw Data Message ID: %d", res);
|
||||
}
|
||||
|
||||
//Just for reference
|
||||
void user_deviceinfo_update(void)
|
||||
{
|
||||
int res = 0;
|
||||
ct_ctx_t *ct_ctx = ct_get_ctx();
|
||||
char *device_info_update = "[{\"attrKey\":\"ct\",\"attrValue\":\"I am a ct device\"},{\"attrKey\":\"ct2\",\"attrValue\":\"I am a ct2\"}]";
|
||||
|
||||
res = IOT_Linkkit_Report(ct_ctx->master_devid, ITM_MSG_DEVICEINFO_UPDATE,
|
||||
(unsigned char *)device_info_update, strlen(device_info_update));
|
||||
ct_info("Device Info Update Message ID: %d", res);
|
||||
}
|
||||
|
||||
//Just for reference
|
||||
void user_deviceinfo_delete(void)
|
||||
{
|
||||
int res = 0;
|
||||
ct_ctx_t *ct_ctx = ct_get_ctx();
|
||||
char *device_info_delete = "[{\"attrKey\":\"ct2\"}]";
|
||||
|
||||
res = IOT_Linkkit_Report(ct_ctx->master_devid, ITM_MSG_DEVICEINFO_DELETE,
|
||||
(unsigned char *)device_info_delete, strlen(device_info_delete));
|
||||
ct_info("Device Info Delete Message ID: %d", res);
|
||||
}
|
||||
|
||||
void ct_ut_misc_process(uint64_t time_now_sec)
|
||||
{
|
||||
/* Post Proprety Example */
|
||||
if (time_now_sec % 11 == 0)
|
||||
{
|
||||
#ifdef CT_PRODUCT_DYNAMIC_REGISTER_AND_USE_RAWDATA
|
||||
user_post_raw_data();
|
||||
#else
|
||||
user_post_property();
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifndef CT_PRODUCT_DYNAMIC_REGISTER_AND_USE_RAWDATA
|
||||
/* Device Info Update Example */
|
||||
if (time_now_sec % 23 == 0)
|
||||
{
|
||||
user_deviceinfo_update();
|
||||
}
|
||||
|
||||
/* Device Info Delete Example */
|
||||
if (time_now_sec % 29 == 0)
|
||||
{
|
||||
user_deviceinfo_delete();
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(CT_DOWNLOAD_OTA_WHEN_CONNECT_CLOUD) && defined(OTA_ENABLED)
|
||||
if (do_only_once == 0)
|
||||
{
|
||||
do_only_once = 1;
|
||||
#ifdef BUILD_AOS
|
||||
ct_ota_download((ota_service_t *)ct_entry_get_uota_ctx());
|
||||
#else
|
||||
ct_ota_download();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
int ct_ut_init(void)
|
||||
{
|
||||
int ret = SUCCESS_RETURN;
|
||||
|
||||
memset(&ct_tsl_data, 0, sizeof(ct_tsl_t));
|
||||
ct_tsl_data.PropertyString = (char *)HAL_Malloc(PROPERTY_STRING_MAX_LEN);
|
||||
if (ct_tsl_data.PropertyString == NULL)
|
||||
{
|
||||
ct_err("no mem");
|
||||
|
||||
ret = FAIL_RETURN;
|
||||
}
|
||||
|
||||
ct_tsl_data.LightSwitch = 1;
|
||||
ct_tsl_data.NightLightSwitch = 0;
|
||||
ct_tsl_data.Brightness = 88;
|
||||
ct_tsl_data.WorkMode = 3;
|
||||
HAL_Snprintf(ct_tsl_data.WorkTime, WORKTIME_MAX_LEN, "%s", "1582861307282");
|
||||
HAL_Snprintf(ct_tsl_data.PropertyString, PROPERTY_STRING_MAX_LEN, "%s", "This is a testing property string!");
|
||||
ct_tsl_data.f = 9.999999;
|
||||
ct_tsl_data.d = 88.888888;
|
||||
ct_tsl_data.RGB.R = 128;
|
||||
ct_tsl_data.RGB.G = 128;
|
||||
ct_tsl_data.RGB.B = 128;
|
||||
ct_tsl_data.readonly = 38;
|
||||
|
||||
ct_tsl_data.wifi.Channel = 6;
|
||||
ct_tsl_data.wifi.SNR = -127;
|
||||
ct_tsl_data.wifi.rssi = -78;
|
||||
HAL_Snprintf(ct_tsl_data.wifi.bssid, AP_BSSID_MAX_LEN, "%s", "11:22:33:44:55:66");
|
||||
HAL_Snprintf(ct_tsl_data.wifi.band, WIFI_BAND_MAX_LEN, "%s", "2.4G");
|
||||
|
||||
return ret;
|
||||
}
|
||||
67
Products/example/certification/ct_ut.h
Normal file
67
Products/example/certification/ct_ut.h
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
#ifndef __CT_UT_H__
|
||||
#define __CT_UT_H__
|
||||
|
||||
#define KV_KEY_PK "pk"
|
||||
#define KV_KEY_PS "ps"
|
||||
#define KV_KEY_DN "dn"
|
||||
#define KV_KEY_DS "ds"
|
||||
|
||||
#define MAX_KEY_LEN (6)
|
||||
|
||||
#define PRODUCT_KEY "PK_XXXXXX"
|
||||
#define PRODUCT_SECRET "PS_XXXXXX"
|
||||
#define DEVICE_NAME "DN_XXXXXX"
|
||||
#define DEVICE_SECRET "DS_XXXXXX"
|
||||
|
||||
#define EXAMPLE_MASTER_DEVID (0)
|
||||
|
||||
#define PROPERTY_STRING_MAX_LEN (1024)
|
||||
#define WORKTIME_MAX_LEN (64)
|
||||
#define WIFI_BAND_MAX_LEN (64)
|
||||
#define AP_BSSID_MAX_LEN (64)
|
||||
|
||||
typedef struct _RGBColor_s
|
||||
{
|
||||
unsigned char R;
|
||||
unsigned char G;
|
||||
unsigned char B;
|
||||
} RGBColor_t;
|
||||
|
||||
typedef struct _WiFi_s
|
||||
{
|
||||
int Channel;
|
||||
char SNR;
|
||||
char bssid[AP_BSSID_MAX_LEN];
|
||||
char band[WIFI_BAND_MAX_LEN];
|
||||
char rssi;
|
||||
} WiFi_t;
|
||||
|
||||
typedef struct _ct_tsl_s
|
||||
{
|
||||
char LightSwitch;
|
||||
char NightLightSwitch;
|
||||
RGBColor_t RGB;
|
||||
unsigned char Brightness;
|
||||
unsigned char WorkMode;
|
||||
float f;
|
||||
double d;
|
||||
int readonly;
|
||||
char *PropertyString;
|
||||
char WorkTime[WORKTIME_MAX_LEN];
|
||||
WiFi_t wifi;
|
||||
} ct_tsl_t;
|
||||
|
||||
extern int ct_ut_init(void);
|
||||
extern void ct_ut_misc_process(uint64_t time_now_sec);
|
||||
extern ct_tsl_t* ct_ut_get_tsl_data(void);
|
||||
extern int ct_ut_set_LightSwitch(char LightSwitch);
|
||||
extern int ct_ut_get_LightSwitch(void);
|
||||
extern int ct_ut_set_NightLightSwitch(char NightLightSwitch);
|
||||
extern int ct_ut_set_WorkMode(unsigned char WorkMode);
|
||||
extern int ct_ut_set_Brightness(unsigned char Brightness);
|
||||
extern int ct_ut_set_RGB(unsigned char R, unsigned char G, unsigned char B);
|
||||
extern int ct_ut_set_Float(float f);
|
||||
extern int ct_ut_set_Double(double d);
|
||||
extern int ct_ut_set_PropertyString(char *PropertyString);
|
||||
extern int ct_ut_set_WorkTime(char *WorkTime);
|
||||
#endif
|
||||
977
Products/example/certification/docs/ct_model.json
Normal file
977
Products/example/certification/docs/ct_model.json
Normal file
|
|
@ -0,0 +1,977 @@
|
|||
{
|
||||
"schema":"https://iotx-tsl.oss-ap-southeast-1.aliyuncs.com/schema.json",
|
||||
"profile":{
|
||||
"productKey":"a15mFJQoWM1"
|
||||
},
|
||||
"services":[
|
||||
{
|
||||
"outputData":[
|
||||
|
||||
],
|
||||
"identifier":"set",
|
||||
"inputData":[
|
||||
{
|
||||
"identifier":"LightSwitch",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"关闭",
|
||||
"1":"开启"
|
||||
},
|
||||
"type":"bool"
|
||||
},
|
||||
"name":"主灯开关"
|
||||
},
|
||||
{
|
||||
"identifier":"RGBColor",
|
||||
"dataType":{
|
||||
"specs":[
|
||||
{
|
||||
"identifier":"Red",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"unitName":"无",
|
||||
"max":"255",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"红色"
|
||||
},
|
||||
{
|
||||
"identifier":"Green",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"unitName":"无",
|
||||
"max":"255",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"绿色"
|
||||
},
|
||||
{
|
||||
"identifier":"Blue",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"unitName":"无",
|
||||
"max":"255",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"蓝色"
|
||||
}
|
||||
],
|
||||
"type":"struct"
|
||||
},
|
||||
"name":"RGB调色"
|
||||
},
|
||||
{
|
||||
"identifier":"NightLightSwitch",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"关闭",
|
||||
"1":"开启"
|
||||
},
|
||||
"type":"bool"
|
||||
},
|
||||
"name":"夜灯开关"
|
||||
},
|
||||
{
|
||||
"identifier":"WorkMode",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"手动",
|
||||
"1":"阅读",
|
||||
"2":"影院",
|
||||
"3":"夜灯",
|
||||
"4":"生活",
|
||||
"5":"柔和"
|
||||
},
|
||||
"type":"enum"
|
||||
},
|
||||
"name":"工作模式"
|
||||
},
|
||||
{
|
||||
"identifier":"Brightness",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"unit":"%",
|
||||
"min":"0",
|
||||
"unitName":"百分比",
|
||||
"max":"100",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"明暗度"
|
||||
},
|
||||
{
|
||||
"identifier":"worktime",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
|
||||
},
|
||||
"type":"date"
|
||||
},
|
||||
"name":"工作时间"
|
||||
},
|
||||
{
|
||||
"identifier":"floatid",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"max":"10",
|
||||
"step":"0.000001"
|
||||
},
|
||||
"type":"float"
|
||||
},
|
||||
"name":"单精度"
|
||||
},
|
||||
{
|
||||
"identifier":"doubleid",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"max":"100",
|
||||
"step":"0.000001"
|
||||
},
|
||||
"type":"double"
|
||||
},
|
||||
"name":"双精度"
|
||||
},
|
||||
{
|
||||
"identifier":"PropertyString",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"1024"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"PropertyString"
|
||||
}
|
||||
],
|
||||
"method":"thing.service.property.set",
|
||||
"name":"set",
|
||||
"required":true,
|
||||
"callType":"async",
|
||||
"desc":"属性设置"
|
||||
},
|
||||
{
|
||||
"outputData":[
|
||||
{
|
||||
"identifier":"LightSwitch",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"关闭",
|
||||
"1":"开启"
|
||||
},
|
||||
"type":"bool"
|
||||
},
|
||||
"name":"主灯开关"
|
||||
},
|
||||
{
|
||||
"identifier":"WIFI_Band",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"255"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"频段"
|
||||
},
|
||||
{
|
||||
"identifier":"WiFI_RSSI",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"-127",
|
||||
"max":"-1",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"信号强度"
|
||||
},
|
||||
{
|
||||
"identifier":"WIFI_AP_BSSID",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"255"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"热点BSSID"
|
||||
},
|
||||
{
|
||||
"identifier":"WIFI_Channel",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"1",
|
||||
"max":"255",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"信道"
|
||||
},
|
||||
{
|
||||
"identifier":"WiFI_SNR",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"-127",
|
||||
"max":"127",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"信噪比"
|
||||
},
|
||||
{
|
||||
"identifier":"RGBColor",
|
||||
"dataType":{
|
||||
"specs":[
|
||||
{
|
||||
"identifier":"Red",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"unitName":"无",
|
||||
"max":"255",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"红色"
|
||||
},
|
||||
{
|
||||
"identifier":"Green",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"unitName":"无",
|
||||
"max":"255",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"绿色"
|
||||
},
|
||||
{
|
||||
"identifier":"Blue",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"unitName":"无",
|
||||
"max":"255",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"蓝色"
|
||||
}
|
||||
],
|
||||
"type":"struct"
|
||||
},
|
||||
"name":"RGB调色"
|
||||
},
|
||||
{
|
||||
"identifier":"NightLightSwitch",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"关闭",
|
||||
"1":"开启"
|
||||
},
|
||||
"type":"bool"
|
||||
},
|
||||
"name":"夜灯开关"
|
||||
},
|
||||
{
|
||||
"identifier":"WorkMode",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"手动",
|
||||
"1":"阅读",
|
||||
"2":"影院",
|
||||
"3":"夜灯",
|
||||
"4":"生活",
|
||||
"5":"柔和"
|
||||
},
|
||||
"type":"enum"
|
||||
},
|
||||
"name":"工作模式"
|
||||
},
|
||||
{
|
||||
"identifier":"Brightness",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"unit":"%",
|
||||
"min":"0",
|
||||
"unitName":"百分比",
|
||||
"max":"100",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"明暗度"
|
||||
},
|
||||
{
|
||||
"identifier":"worktime",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
|
||||
},
|
||||
"type":"date"
|
||||
},
|
||||
"name":"工作时间"
|
||||
},
|
||||
{
|
||||
"identifier":"onlyread",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"max":"50",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"只读"
|
||||
},
|
||||
{
|
||||
"identifier":"floatid",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"max":"10",
|
||||
"step":"0.000001"
|
||||
},
|
||||
"type":"float"
|
||||
},
|
||||
"name":"单精度"
|
||||
},
|
||||
{
|
||||
"identifier":"doubleid",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"max":"100",
|
||||
"step":"0.000001"
|
||||
},
|
||||
"type":"double"
|
||||
},
|
||||
"name":"双精度"
|
||||
},
|
||||
{
|
||||
"identifier":"PropertyString",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"1024"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"PropertyString"
|
||||
}
|
||||
],
|
||||
"identifier":"get",
|
||||
"inputData":[
|
||||
"LightSwitch",
|
||||
"WIFI_Band",
|
||||
"WiFI_RSSI",
|
||||
"WIFI_AP_BSSID",
|
||||
"WIFI_Channel",
|
||||
"WiFI_SNR",
|
||||
"RGBColor",
|
||||
"NightLightSwitch",
|
||||
"WorkMode",
|
||||
"Brightness",
|
||||
"worktime",
|
||||
"onlyread",
|
||||
"floatid",
|
||||
"doubleid",
|
||||
"PropertyString"
|
||||
],
|
||||
"method":"thing.service.property.get",
|
||||
"name":"get",
|
||||
"required":true,
|
||||
"callType":"async",
|
||||
"desc":"属性获取"
|
||||
},
|
||||
{
|
||||
"outputData":[
|
||||
{
|
||||
"identifier":"transparency",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"max":"100",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"Contrastratio"
|
||||
}
|
||||
],
|
||||
"identifier":"Custom",
|
||||
"inputData":[
|
||||
{
|
||||
"identifier":"transparency",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"max":"100",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"Contrastratio"
|
||||
}
|
||||
],
|
||||
"method":"thing.service.Custom",
|
||||
"name":"Custom",
|
||||
"required":false,
|
||||
"callType":"async"
|
||||
},
|
||||
{
|
||||
"outputData":[
|
||||
|
||||
],
|
||||
"identifier":"ToggleLightSwitch",
|
||||
"inputData":[
|
||||
|
||||
],
|
||||
"method":"thing.service.ToggleLightSwitch",
|
||||
"name":"翻转主灯开关",
|
||||
"required":false,
|
||||
"callType":"sync"
|
||||
}
|
||||
],
|
||||
"properties":[
|
||||
{
|
||||
"identifier":"LightSwitch",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"关闭",
|
||||
"1":"开启"
|
||||
},
|
||||
"type":"bool"
|
||||
},
|
||||
"name":"主灯开关",
|
||||
"accessMode":"rw",
|
||||
"required":false
|
||||
},
|
||||
{
|
||||
"identifier":"WIFI_Band",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"255"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"频段",
|
||||
"accessMode":"r",
|
||||
"required":false
|
||||
},
|
||||
{
|
||||
"identifier":"WiFI_RSSI",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"-127",
|
||||
"max":"-1",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"信号强度",
|
||||
"accessMode":"r",
|
||||
"required":false
|
||||
},
|
||||
{
|
||||
"identifier":"WIFI_AP_BSSID",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"255"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"热点BSSID",
|
||||
"accessMode":"r",
|
||||
"required":false
|
||||
},
|
||||
{
|
||||
"identifier":"WIFI_Channel",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"1",
|
||||
"max":"255",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"信道",
|
||||
"accessMode":"r",
|
||||
"required":false
|
||||
},
|
||||
{
|
||||
"identifier":"WiFI_SNR",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"-127",
|
||||
"max":"127",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"信噪比",
|
||||
"accessMode":"r",
|
||||
"required":false
|
||||
},
|
||||
{
|
||||
"identifier":"RGBColor",
|
||||
"dataType":{
|
||||
"specs":[
|
||||
{
|
||||
"identifier":"Red",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"unitName":"无",
|
||||
"max":"255",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"红色"
|
||||
},
|
||||
{
|
||||
"identifier":"Green",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"unitName":"无",
|
||||
"max":"255",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"绿色"
|
||||
},
|
||||
{
|
||||
"identifier":"Blue",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"unitName":"无",
|
||||
"max":"255",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"蓝色"
|
||||
}
|
||||
],
|
||||
"type":"struct"
|
||||
},
|
||||
"name":"RGB调色",
|
||||
"accessMode":"rw",
|
||||
"required":false
|
||||
},
|
||||
{
|
||||
"identifier":"NightLightSwitch",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"关闭",
|
||||
"1":"开启"
|
||||
},
|
||||
"type":"bool"
|
||||
},
|
||||
"name":"夜灯开关",
|
||||
"accessMode":"rw",
|
||||
"required":false
|
||||
},
|
||||
{
|
||||
"identifier":"WorkMode",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"手动",
|
||||
"1":"阅读",
|
||||
"2":"影院",
|
||||
"3":"夜灯",
|
||||
"4":"生活",
|
||||
"5":"柔和"
|
||||
},
|
||||
"type":"enum"
|
||||
},
|
||||
"name":"工作模式",
|
||||
"accessMode":"rw",
|
||||
"required":false
|
||||
},
|
||||
{
|
||||
"identifier":"Brightness",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"unit":"%",
|
||||
"min":"0",
|
||||
"unitName":"百分比",
|
||||
"max":"100",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"明暗度",
|
||||
"accessMode":"rw",
|
||||
"required":false
|
||||
},
|
||||
{
|
||||
"identifier":"worktime",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
|
||||
},
|
||||
"type":"date"
|
||||
},
|
||||
"name":"工作时间",
|
||||
"accessMode":"rw",
|
||||
"required":false
|
||||
},
|
||||
{
|
||||
"identifier":"onlyread",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"max":"50",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"只读",
|
||||
"accessMode":"r",
|
||||
"required":false
|
||||
},
|
||||
{
|
||||
"identifier":"floatid",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"max":"10",
|
||||
"step":"0.000001"
|
||||
},
|
||||
"type":"float"
|
||||
},
|
||||
"name":"单精度",
|
||||
"accessMode":"rw",
|
||||
"required":false
|
||||
},
|
||||
{
|
||||
"identifier":"doubleid",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"max":"100",
|
||||
"step":"0.000001"
|
||||
},
|
||||
"type":"double"
|
||||
},
|
||||
"name":"双精度",
|
||||
"accessMode":"rw",
|
||||
"required":false
|
||||
},
|
||||
{
|
||||
"identifier":"PropertyString",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"1024"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"PropertyString",
|
||||
"accessMode":"rw",
|
||||
"required":false
|
||||
}
|
||||
],
|
||||
"events":[
|
||||
{
|
||||
"outputData":[
|
||||
{
|
||||
"identifier":"LightSwitch",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"关闭",
|
||||
"1":"开启"
|
||||
},
|
||||
"type":"bool"
|
||||
},
|
||||
"name":"主灯开关"
|
||||
},
|
||||
{
|
||||
"identifier":"WIFI_Band",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"255"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"频段"
|
||||
},
|
||||
{
|
||||
"identifier":"WiFI_RSSI",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"-127",
|
||||
"max":"-1",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"信号强度"
|
||||
},
|
||||
{
|
||||
"identifier":"WIFI_AP_BSSID",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"255"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"热点BSSID"
|
||||
},
|
||||
{
|
||||
"identifier":"WIFI_Channel",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"1",
|
||||
"max":"255",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"信道"
|
||||
},
|
||||
{
|
||||
"identifier":"WiFI_SNR",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"-127",
|
||||
"max":"127",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"信噪比"
|
||||
},
|
||||
{
|
||||
"identifier":"RGBColor",
|
||||
"dataType":{
|
||||
"specs":[
|
||||
{
|
||||
"identifier":"Red",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"unitName":"无",
|
||||
"max":"255",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"红色"
|
||||
},
|
||||
{
|
||||
"identifier":"Green",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"unitName":"无",
|
||||
"max":"255",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"绿色"
|
||||
},
|
||||
{
|
||||
"identifier":"Blue",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"unitName":"无",
|
||||
"max":"255",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"蓝色"
|
||||
}
|
||||
],
|
||||
"type":"struct"
|
||||
},
|
||||
"name":"RGB调色"
|
||||
},
|
||||
{
|
||||
"identifier":"NightLightSwitch",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"关闭",
|
||||
"1":"开启"
|
||||
},
|
||||
"type":"bool"
|
||||
},
|
||||
"name":"夜灯开关"
|
||||
},
|
||||
{
|
||||
"identifier":"WorkMode",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"手动",
|
||||
"1":"阅读",
|
||||
"2":"影院",
|
||||
"3":"夜灯",
|
||||
"4":"生活",
|
||||
"5":"柔和"
|
||||
},
|
||||
"type":"enum"
|
||||
},
|
||||
"name":"工作模式"
|
||||
},
|
||||
{
|
||||
"identifier":"Brightness",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"unit":"%",
|
||||
"min":"0",
|
||||
"unitName":"百分比",
|
||||
"max":"100",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"明暗度"
|
||||
},
|
||||
{
|
||||
"identifier":"worktime",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
|
||||
},
|
||||
"type":"date"
|
||||
},
|
||||
"name":"工作时间"
|
||||
},
|
||||
{
|
||||
"identifier":"onlyread",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"max":"50",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"只读"
|
||||
},
|
||||
{
|
||||
"identifier":"floatid",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"max":"10",
|
||||
"step":"0.000001"
|
||||
},
|
||||
"type":"float"
|
||||
},
|
||||
"name":"单精度"
|
||||
},
|
||||
{
|
||||
"identifier":"doubleid",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"max":"100",
|
||||
"step":"0.000001"
|
||||
},
|
||||
"type":"double"
|
||||
},
|
||||
"name":"双精度"
|
||||
},
|
||||
{
|
||||
"identifier":"PropertyString",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"1024"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"PropertyString"
|
||||
}
|
||||
],
|
||||
"identifier":"post",
|
||||
"method":"thing.event.property.post",
|
||||
"name":"post",
|
||||
"type":"info",
|
||||
"required":true,
|
||||
"desc":"属性上报"
|
||||
},
|
||||
{
|
||||
"outputData":[
|
||||
{
|
||||
"identifier":"ErrorCode",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"恢复正常"
|
||||
},
|
||||
"type":"enum"
|
||||
},
|
||||
"name":"故障代码"
|
||||
}
|
||||
],
|
||||
"identifier":"Error",
|
||||
"method":"thing.event.Error.post",
|
||||
"name":"故障上报",
|
||||
"type":"error",
|
||||
"required":false
|
||||
},
|
||||
{
|
||||
"outputData":[
|
||||
{
|
||||
"identifier":"alarm",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"max":"10",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"alarm"
|
||||
}
|
||||
],
|
||||
"identifier":"alarm",
|
||||
"method":"thing.event.alarm.post",
|
||||
"name":"alarm",
|
||||
"type":"alert",
|
||||
"required":false
|
||||
},
|
||||
{
|
||||
"outputData":[
|
||||
{
|
||||
"identifier":"info",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"max":"20",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"info"
|
||||
}
|
||||
],
|
||||
"identifier":"info",
|
||||
"method":"thing.event.info.post",
|
||||
"name":"info",
|
||||
"type":"info",
|
||||
"required":false
|
||||
}
|
||||
]
|
||||
}
|
||||
BIN
Products/example/certification/docs/网络环境监测工具-WiFi魔盒.pdf
Normal file
BIN
Products/example/certification/docs/网络环境监测工具-WiFi魔盒.pdf
Normal file
Binary file not shown.
BIN
Products/example/certification/docs/飞燕SDK的MQTT通道压测指导文档.pdf
Normal file
BIN
Products/example/certification/docs/飞燕SDK的MQTT通道压测指导文档.pdf
Normal file
Binary file not shown.
BIN
Products/example/certification/docs/飞燕SDK的OTA压测指导文档.pdf
Normal file
BIN
Products/example/certification/docs/飞燕SDK的OTA压测指导文档.pdf
Normal file
Binary file not shown.
BIN
Products/example/certification/docs/飞燕模组认证应用使用说明.pdf
Normal file
BIN
Products/example/certification/docs/飞燕模组认证应用使用说明.pdf
Normal file
Binary file not shown.
33
Products/example/certification/make.settings
Normal file
33
Products/example/certification/make.settings
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#
|
||||
# Configure FeiYan SDK Features
|
||||
#
|
||||
|
||||
#=========Basic Features==========
|
||||
FEATURE_SRCPATH="."
|
||||
FEATURE_MQTT_COMM_ENABLED=y
|
||||
FEATURE_MQTT_AUTO_SUBSCRIBE=y
|
||||
FEATURE_DEVICE_MODEL_ENABLED=y
|
||||
FEATURE_OTA_ENABLED=y
|
||||
FEATURE_DEV_BIND_ENABLED=y
|
||||
FEATURE_SUPPORT_TLS=y
|
||||
#Support unified service post
|
||||
FEATURE_DM_UNIFIED_SERVICE_POST=y
|
||||
#=========Basic Features==========
|
||||
|
||||
#=========User Config Features==========
|
||||
FEATURE_ALCS_ENABLED=y
|
||||
|
||||
FEATURE_DEVICE_MODEL_GATEWAY=n
|
||||
|
||||
#
|
||||
# AWSS Configurations
|
||||
#
|
||||
FEATURE_WIFI_PROVISION_ENABLED=y
|
||||
|
||||
FEATURE_AWSS_SUPPORT_SMARTCONFIG=y
|
||||
FEATURE_AWSS_SUPPORT_ZEROCONFIG=y
|
||||
FEATURE_AWSS_SUPPORT_DEV_AP=y
|
||||
|
||||
FEATURE_AWSS_SUPPORT_PHONEASAP=n
|
||||
FEATURE_AWSS_SUPPORT_ROUTER=n
|
||||
#=========User Config Features==========
|
||||
110
Products/example/certification/makefile
Normal file
110
Products/example/certification/makefile
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
|
||||
include certification.mk
|
||||
target = certification
|
||||
|
||||
CFLAGS += -DALCS_ENABLED
|
||||
|
||||
INCLUDE_PATH = -I$(PWD)
|
||||
INCLUDE = $(INCLUDE_PATH)/ \
|
||||
-I$(PWD)/../../../prebuild/include \
|
||||
-I$(PWD)/../../../prebuild/include/exports \
|
||||
-I$(PWD)/../../../prebuild/include/imports \
|
||||
-I$(PWD)/../../../prebuild/include/kernel/rhino/core/include \
|
||||
-I$(PWD)/../../../prebuild/include/framework/netmgr/include \
|
||||
-I$(PWD)/../../../prebuild/include/platform/arch/arm/armv7m/gcc/m4 \
|
||||
-I$(PWD)/../../../prebuild/include/board/$(BOARD)
|
||||
|
||||
app = $(wildcard ./*.c)
|
||||
obj_app = $(patsubst %.c,%.o,$(app))
|
||||
|
||||
|
||||
CROSS_PREFIX="arm-none-eabi-"
|
||||
DEPENDS=""
|
||||
CCLD=""
|
||||
LDFLAGS=""
|
||||
CC=arm-none-eabi-gcc
|
||||
LD=arm-none-eabi-ld
|
||||
AR=arm-none-eabi-ar
|
||||
STRIP=arm-none-eabi-strip
|
||||
OBJCOPY=arm-none-eabi-objcopy
|
||||
|
||||
# CFLAGS += -MD -ggdb -Os -Wall -Wfatal-errors -fsigned-char -ffunction-sections -fdata-sections -fno-common -std=gnu11 -Werror \
|
||||
# -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
|
||||
CFLAGS += -DWITH_LWIP -DCONFIG_AOS_CLI -DLOG_SIMPLE
|
||||
CFLAGS += ${GLOBAL_CFLAGS}
|
||||
CFLAGS += ${SDK_DEFINES}
|
||||
|
||||
ifeq ("$(BOARD)", "hf-lpt230")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
else ifeq ("$(BOARD)", "hf-lpt130")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
else ifeq ("$(BOARD)", "hf-lpb130")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
else ifeq ("$(BOARD)", "hf-lpb135")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
else ifeq ("$(BOARD)", "uno-91h")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
|
||||
else ifeq ("$(BOARD)", "mk1101")
|
||||
else ifeq ("$(BOARD)", "mk3060")
|
||||
CFLAGS += -mcpu=arm968e-s -march=armv5te -mthumb -mthumb-interwork -mlittle-endian -w -Os -Wall -Wfatal-errors -fsigned-char -ffunction-sections -fdata-sections -fno-common -std=gnu11
|
||||
else ifeq ("$(BOARD)", "mk3061")
|
||||
CFLAGS += -mcpu=arm968e-s -march=armv5te -mthumb -mthumb-interwork -mlittle-endian -w -Os -Wall -Wfatal-errors -fsigned-char -ffunction-sections -fdata-sections -fno-common -std=gnu11
|
||||
|
||||
else ifeq ("$(BOARD)", "mk3080")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
else ifeq ("$(BOARD)", "mk3092")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
else ifeq ("$(BOARD)", "mk5080")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
|
||||
|
||||
else ifeq ("$(BOARD)", "asr5501")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -mfpu=fpv4-sp-d16 -std=gnu11 -w
|
||||
else ifeq ("$(BOARD)", "mx1270")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -mfpu=fpv4-sp-d16 -std=gnu11 -w
|
||||
|
||||
else ifeq ("$(BOARD)", "bk7231devkitc")
|
||||
CFLAGS += -mcpu=arm968e-s -march=armv5te -mthumb -mthumb-interwork -mlittle-endian -w -Os -Wall -Wfatal-errors -fsigned-char -ffunction-sections -fdata-sections -fno-common -std=gnu11
|
||||
CFLAGS += -DBK7231DEVKITC
|
||||
else ifeq ("$(BOARD)", "bk7231sdevkitc")
|
||||
CFLAGS += -mcpu=arm968e-s -march=armv5te -mthumb -mthumb-interwork -mlittle-endian -w -Os -Wall -Wfatal-errors -fsigned-char -ffunction-sections -fdata-sections -fno-common -std=gnu11
|
||||
CFLAGS += -DBK7231SDEVKITC
|
||||
else ifeq ("$(BOARD)", "bk7231udevkitc")
|
||||
CFLAGS += -mcpu=arm968e-s -march=armv5te -mthumb -mthumb-interwork -mlittle-endian -w -Os -Wall -Wfatal-errors -fsigned-char -ffunction-sections -fdata-sections -fno-common -std=gnu11
|
||||
CFLAGS += -DBK7231UDEVKITC
|
||||
else ifeq ("$(BOARD)", "b_l475e")
|
||||
|
||||
else ifeq ("$(BOARD)", "eml3047")
|
||||
else ifeq ("$(BOARD)", "frdmkl27z")
|
||||
else ifeq ("$(BOARD)", "lpcxpresso54102")
|
||||
|
||||
else ifeq ("$(BOARD)", "pca10056")
|
||||
|
||||
## toolchains['arm-rockchip-linux-gnueabihf']
|
||||
else ifeq ("$(BOARD)", "rk1108")
|
||||
|
||||
## toolchains['nds32le-elf-newlib-v3']
|
||||
else ifeq ("$(BOARD)", "sv6266_evb")
|
||||
endif
|
||||
|
||||
ifneq ("$(BOARD)", "")
|
||||
$(info BOARD=${BOARD} CONFIG_COMPILE_DATE=${CONFIG_COMPILE_DATE} region=${CONFIG_SERVER_REGION} env=${CONFIG_SERVER_ENV} debug=${CONFIG_DEBUG})
|
||||
endif
|
||||
|
||||
# Compile date
|
||||
CONFIG_COMPILE_DATE ?= $(shell date +%Y%m%d.%H%M%S)
|
||||
all : $(target)
|
||||
|
||||
$(target):$(obj_app)
|
||||
@mkdir -p obj
|
||||
@mv *.o obj
|
||||
@$(RM) -rf *.d
|
||||
@$(AR) -rcs $(PWD)/../../../prebuild/lib/$(target).a obj/*.o 2>/dev/null
|
||||
@$(RM) -rf obj
|
||||
%.o:%.c
|
||||
@$(CC) -c $(CFLAGS) $(INCLUDE) -o $@ $<
|
||||
|
||||
clean:
|
||||
@-$(RM) obj/*
|
||||
96
Products/example/certification/tools/Alink协议与二进制转换脚本.txt
Normal file
96
Products/example/certification/tools/Alink协议与二进制转换脚本.txt
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
var COMMAND_REPORT = 0x01; //属性上报
|
||||
var COMMAND_SET = 0x02; //属性设置
|
||||
|
||||
var ALINK_PROP_REPORT_METHOD = 'thing.event.property.post'; //标准ALink JSON格式topic, 设备上传属性数据到云端
|
||||
var ALINK_PROP_SET_METHOD = 'thing.service.property.set'; //标准ALink JSON格式topic, 云端下发属性控制指令到设备端
|
||||
var ALINK_PROP_SET_REPLY_METHOD = 'thing.service.property.set'; //标准ALink JSON格式topic, 设备上报属性设置的结果到云端
|
||||
/*
|
||||
示例数据:
|
||||
设备上报数据
|
||||
传入参数 ->
|
||||
0x010000006400
|
||||
输出结果 ->
|
||||
{"method":"thing.event.property.post","id":"100","params":{"LightSwitch":0},"version":"1.0"}
|
||||
*/
|
||||
function rawDataToProtocol(bytes) {
|
||||
var uint8Array = new Uint8Array(bytes.length);
|
||||
for (var i = 0; i < bytes.length; i++) {
|
||||
uint8Array[i] = bytes[i] & 0xff;
|
||||
}
|
||||
var dataView = new DataView(uint8Array.buffer, 0);
|
||||
var jsonMap = new Object();
|
||||
var fHead = uint8Array[0]; // command
|
||||
if (fHead == COMMAND_REPORT) {
|
||||
jsonMap['method'] = ALINK_PROP_REPORT_METHOD; //ALink JSON格式 - 属性上报topic
|
||||
jsonMap['version'] = '1.0'; //ALink JSON格式 - 协议版本号固定字段
|
||||
jsonMap['id'] = '' + dataView.getInt32(1); //ALink JSON格式 - 标示该次请求id值
|
||||
var params = {};
|
||||
params['LightSwitch'] = dataView.getInt8(5); //对应产品属性中 prop_int16
|
||||
jsonMap['params'] = params; //ALink JSON格式 - params标准字段
|
||||
}
|
||||
|
||||
return jsonMap;
|
||||
}
|
||||
/*
|
||||
示例数据:
|
||||
属性设置
|
||||
传入参数 ->
|
||||
{"method":"thing.service.property.set","id":"100","version":"1.0","params":{"LightSwitch":0}}
|
||||
输出结果 ->
|
||||
0x020000006400
|
||||
*/
|
||||
function protocolToRawData(json) {
|
||||
var method = json['method'];
|
||||
var id = json['id'];
|
||||
var version = json['version'];
|
||||
var payloadArray = [];
|
||||
if (method == ALINK_PROP_SET_METHOD) // 属性设置
|
||||
{
|
||||
var params = json['params'];
|
||||
var light_switch = params['LightSwitch'];
|
||||
|
||||
//按照自定义协议格式拼接 rawData
|
||||
payloadArray = payloadArray.concat(buffer_uint8(COMMAND_SET)); // command字段
|
||||
payloadArray = payloadArray.concat(buffer_int32(parseInt(id))); // ALink JSON格式 'id'
|
||||
payloadArray = payloadArray.concat(buffer_uint8(light_switch)); // 属性'light_switch'的值
|
||||
}
|
||||
|
||||
return payloadArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将设备自定义topic数据转换为json格式数据, 设备上报数据到物联网平台时调用
|
||||
* 入参:topic 字符串,设备上报消息的topic
|
||||
* 入参:rawData byte[]数组 不能为空
|
||||
* 出参:jsonObj 对象 不能为空
|
||||
*/
|
||||
function transformPayload(topic, rawData) {
|
||||
jsonObj = {}
|
||||
return jsonObj
|
||||
}
|
||||
|
||||
//以下是部分辅助函数
|
||||
function buffer_uint8(value) {
|
||||
var uint8Array = new Uint8Array(1);
|
||||
var dv = new DataView(uint8Array.buffer, 0);
|
||||
dv.setUint8(0, value);
|
||||
return [].slice.call(uint8Array);
|
||||
}
|
||||
function buffer_int16(value) {
|
||||
var uint8Array = new Uint8Array(2);
|
||||
var dv = new DataView(uint8Array.buffer, 0);
|
||||
dv.setInt16(0, value);
|
||||
return [].slice.call(uint8Array);
|
||||
}
|
||||
function buffer_int32(value) {
|
||||
var uint8Array = new Uint8Array(4);
|
||||
var dv = new DataView(uint8Array.buffer, 0);
|
||||
dv.setInt32(0, value);
|
||||
return [].slice.call(uint8Array);
|
||||
}
|
||||
function buffer_float32(value) {
|
||||
var uint8Array = new Uint8Array(4);
|
||||
var dv = new DataView(uint8Array.buffer, 0);
|
||||
dv.setFloat32(0, value);
|
||||
return [].slice.call(uint8Array);
|
||||
}
|
||||
9
Products/example/certification/tools/ReadMe.txt
Normal file
9
Products/example/certification/tools/ReadMe.txt
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
工具名称:log_extract.sh
|
||||
工具功能:从一个或者多个日志文件中提取感兴趣的日志信息
|
||||
使用方式:
|
||||
1. 运行环境:Linux运行环境
|
||||
2. Linux环境下运行log_extract.sh脚本,要求用户输入三个参数:
|
||||
- 日志文件夹名称,例如:logfiles
|
||||
- 需要提取的关键信息,例如:disconnect
|
||||
- 关键字前后的行数,例如:10
|
||||
3. 脚本执行之后会自动生成一个类似这样的文件:logfiles-20200304_183318.log,如果抓取的日志文件比较多的时候使用这个工具可以提高分析效率
|
||||
46
Products/example/certification/tools/log_extract.sh
Normal file
46
Products/example/certification/tools/log_extract.sh
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
#!/bin/bash
|
||||
#This script can extract some lines from some log files according some key words
|
||||
#Date:2020-03-03
|
||||
#Version:1.0
|
||||
#Author:Liuese
|
||||
#Contacts:TingTalk number:13826595824
|
||||
#Changes:Create
|
||||
|
||||
read -p "Please input a folder which include some log files:" logfiles
|
||||
test -e $logfiles || echo "Folder $logfiles is not exist" || exit 1
|
||||
test -r $logfiles || echo "Folder $logfiles can not read" || exit 1
|
||||
|
||||
read -p "Please input a keyword that want be searched:" keyword
|
||||
test -z $keyword && echo "You input is none" && exit 1
|
||||
|
||||
declare -i lines
|
||||
read -p "Please input a number which is the lines around the keyword $keyword:" lines
|
||||
if test $lines -lt 1
|
||||
then
|
||||
echo "Please input a number that must larger than 0"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if test $? -ne 0
|
||||
then
|
||||
echo "Please input a number"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
time=$(date +%Y%m%d_%H%M%S)
|
||||
savedfile=$logfiles-$time.log
|
||||
|
||||
files=$(ls $logfiles)
|
||||
for file in $files
|
||||
do
|
||||
printf "+++++++++++++++File:$file +++++++++++++++\r\n" >> ./$savedfile
|
||||
keylog=$(cat ./$logfiles/$file | grep -C $lines "$keyword")
|
||||
echo $keylog >> ./$savedfile
|
||||
printf "===============File:$file ===============\r\n" >> ./$savedfile
|
||||
done
|
||||
|
||||
dir=$(pwd)
|
||||
echo "Your input params log folder is:$logfiles keyword is:$keyword lines is:$lines"
|
||||
echo "The parsed log saved to file:$dir/$savedfile"
|
||||
|
||||
exit 0
|
||||
BIN
Products/example/linkkit_gateway/docs/智能生活网关产品参考应用程序说明.pdf
Normal file
BIN
Products/example/linkkit_gateway/docs/智能生活网关产品参考应用程序说明.pdf
Normal file
Binary file not shown.
204
Products/example/linkkit_gateway/gateway_api.c
Normal file
204
Products/example/linkkit_gateway/gateway_api.c
Normal file
|
|
@ -0,0 +1,204 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2020 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include "cJSON.h"
|
||||
#include "iotx_log.h"
|
||||
#include "iot_import.h"
|
||||
#include "iot_export_linkkit.h"
|
||||
#include "gateway_main.h"
|
||||
#include "gateway_api.h"
|
||||
|
||||
/*
|
||||
[params]
|
||||
subdev_mate:one subdev meta info
|
||||
|
||||
[return]
|
||||
SUCCESS_RETURN:success
|
||||
FAIL_RETURN:failed
|
||||
*/
|
||||
GATEWAY_API int gateway_add_subdev(iotx_linkkit_dev_meta_info_t *subdev_mate)
|
||||
{
|
||||
int res = SUCCESS_RETURN;
|
||||
int devid = -1;
|
||||
|
||||
devid = IOT_Linkkit_Open(IOTX_LINKKIT_DEV_TYPE_SLAVE, subdev_mate);
|
||||
if (devid == FAIL_RETURN)
|
||||
{
|
||||
gateway_info("subdev open Failed");
|
||||
return FAIL_RETURN;
|
||||
}
|
||||
gateway_info("subdev open susseed, devid = %d", devid);
|
||||
|
||||
res = IOT_Linkkit_Connect(devid);
|
||||
if (res == FAIL_RETURN)
|
||||
{
|
||||
gateway_info("subdev connect Failed");
|
||||
return res;
|
||||
}
|
||||
gateway_info("subdev connect success: devid = %d", devid);
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_LOGIN, NULL, 0);
|
||||
if (res == FAIL_RETURN)
|
||||
{
|
||||
gateway_info("subdev login Failed");
|
||||
return res;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/*
|
||||
[params]
|
||||
master_devid:master device id
|
||||
subdev_list:one or more subdev meta info
|
||||
subdev_num:subdev total
|
||||
|
||||
[return]
|
||||
SUCCESS_RETURN:success
|
||||
FAIL_RETURN:failed
|
||||
*/
|
||||
GATEWAY_API int gateway_add_multi_subdev(int master_devid, iotx_linkkit_dev_meta_info_t *subdev_list, int subdev_num)
|
||||
{
|
||||
int res = SUCCESS_RETURN;
|
||||
int subdev_id = 0;
|
||||
int index = 0;
|
||||
int inner_index = 0;
|
||||
int connect_times = 0;
|
||||
int cur_subdev_num = 0;
|
||||
iotx_linkkit_dev_meta_info_t *p_cur_subdev = NULL;
|
||||
|
||||
if (!subdev_list || subdev_num < 1)
|
||||
{
|
||||
gateway_err("add multi subdev Failed\n");
|
||||
return FAIL_RETURN;
|
||||
}
|
||||
|
||||
connect_times = subdev_num / GATEWAY_SUBDEV_ONE_TIME_CONNECT_MAX_NUM + (((subdev_num % GATEWAY_SUBDEV_ONE_TIME_CONNECT_MAX_NUM) == 0) ? 0 : 1);
|
||||
|
||||
for (index = 0; index < connect_times; index++)
|
||||
{
|
||||
p_cur_subdev = subdev_list + (index * GATEWAY_SUBDEV_ONE_TIME_CONNECT_MAX_NUM);
|
||||
if ((index == (connect_times - 1)) && ((subdev_num % GATEWAY_SUBDEV_ONE_TIME_CONNECT_MAX_NUM) != 0))
|
||||
{
|
||||
cur_subdev_num = subdev_num - (index * GATEWAY_SUBDEV_ONE_TIME_CONNECT_MAX_NUM);
|
||||
}
|
||||
else
|
||||
{
|
||||
cur_subdev_num = GATEWAY_SUBDEV_ONE_TIME_CONNECT_MAX_NUM;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(master_devid, ITM_MSG_CONNECT_SUBDEV, (unsigned char *)p_cur_subdev, sizeof(iotx_linkkit_dev_meta_info_t) * cur_subdev_num);
|
||||
if (res == FAIL_RETURN)
|
||||
{
|
||||
gateway_err("add multi subdev index:%d Failed\n", index);
|
||||
}
|
||||
else
|
||||
{
|
||||
gateway_info("add multi subdev index:%d success\n", index);
|
||||
|
||||
iotx_linkkit_dev_meta_info_t *p_subdev_mate = NULL;
|
||||
|
||||
for (inner_index = 0; inner_index < cur_subdev_num; inner_index++)
|
||||
{
|
||||
p_subdev_mate = p_cur_subdev + inner_index;
|
||||
subdev_id = gateway_query_subdev_id(master_devid, p_subdev_mate);
|
||||
if (subdev_id > 0)
|
||||
{
|
||||
res = IOT_Linkkit_Report(subdev_id, ITM_MSG_LOGIN, NULL, 0);
|
||||
if (res == FAIL_RETURN)
|
||||
{
|
||||
gateway_err("login subdev dn:%s failed", p_subdev_mate->device_name);
|
||||
}
|
||||
else
|
||||
{
|
||||
gateway_debug("login subdev dn:%s success", p_subdev_mate->device_name);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
gateway_err("connect subdev dn:%s failed", p_subdev_mate->device_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/*
|
||||
[params]
|
||||
subdev_mate:one subdev meta info
|
||||
|
||||
[return]
|
||||
SUCCESS_RETURN:success
|
||||
FAIL_RETURN:failed
|
||||
*/
|
||||
GATEWAY_API int gateway_del_subdev(iotx_linkkit_dev_meta_info_t *subdev_mate)
|
||||
{
|
||||
int subdev_id = 1000; //set an not exist subdev id
|
||||
int ret = SUCCESS_RETURN;
|
||||
|
||||
gateway_info("do del subdev");
|
||||
|
||||
//Here pk and dn of subdev is priorior than subdev id
|
||||
ret = IOT_Linkkit_Report(subdev_id, ITM_MSG_DELETE_TOPO, (unsigned char *)subdev_mate, sizeof(iotx_linkkit_dev_meta_info_t));
|
||||
if (SUCCESS_RETURN != ret)
|
||||
{
|
||||
gateway_err("del subdev failed");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
[params]
|
||||
subdev_mate:one subdev meta info
|
||||
|
||||
[return]
|
||||
SUCCESS_RETURN:success
|
||||
FAIL_RETURN:failed
|
||||
*/
|
||||
GATEWAY_API int gateway_reset_subdev(iotx_linkkit_dev_meta_info_t *subdev_mate)
|
||||
{
|
||||
int subdev_id = 1000; //set an not exist subdev id
|
||||
int ret = SUCCESS_RETURN;
|
||||
|
||||
gateway_info("do reset subdev");
|
||||
|
||||
//Here pk and dn of subdev is priorior than subdev id
|
||||
ret = IOT_Linkkit_Report(subdev_id, ITM_MSG_SUBDEV_RESET, (unsigned char *)subdev_mate, sizeof(iotx_linkkit_dev_meta_info_t));
|
||||
if (SUCCESS_RETURN != ret)
|
||||
{
|
||||
gateway_err("reset subdev failed");
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
[params]
|
||||
master_devid:master device id
|
||||
subdev_mate:one subdev meta info
|
||||
|
||||
[return]
|
||||
subdev_id:>0 success <=0 failed
|
||||
*/
|
||||
GATEWAY_API int gateway_query_subdev_id(int master_devid, iotx_linkkit_dev_meta_info_t *subdev_mate)
|
||||
{
|
||||
int subdev_id = -1; //set an not exist subdev id
|
||||
|
||||
gateway_info("query subdev id");
|
||||
|
||||
subdev_id = IOT_Linkkit_Query(master_devid, ITM_MSG_QUERY_SUBDEV_ID, (unsigned char *)subdev_mate, sizeof(iotx_linkkit_dev_meta_info_t));
|
||||
if (subdev_id < 0)
|
||||
{
|
||||
gateway_err("subdev not exist");
|
||||
}
|
||||
else
|
||||
{
|
||||
gateway_info("got subdev id is %d", subdev_id);
|
||||
}
|
||||
|
||||
return subdev_id;
|
||||
}
|
||||
62
Products/example/linkkit_gateway/gateway_api.h
Normal file
62
Products/example/linkkit_gateway/gateway_api.h
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
#ifndef __GATEWAY_API_H__
|
||||
#define __GATEWAY_API_H__
|
||||
|
||||
#ifndef GATEWAY_API
|
||||
#define GATEWAY_API
|
||||
#endif
|
||||
|
||||
#define GATEWAY_SUBDEV_ONE_TIME_CONNECT_MAX_NUM (5)
|
||||
|
||||
/*
|
||||
[params]
|
||||
subdev_mate:one subdev meta info
|
||||
|
||||
[return]
|
||||
SUCCESS_RETURN:success
|
||||
FAIL_RETURN:failed
|
||||
*/
|
||||
extern GATEWAY_API int gateway_add_subdev(iotx_linkkit_dev_meta_info_t *subdev_mate);
|
||||
|
||||
/*
|
||||
[params]
|
||||
master_devid:master device id
|
||||
subdev_list:one or more subdev meta info
|
||||
subdev_num:subdev total
|
||||
|
||||
[return]
|
||||
SUCCESS_RETURN:success
|
||||
FAIL_RETURN:failed
|
||||
*/
|
||||
extern GATEWAY_API int gateway_add_multi_subdev(int master_devid, iotx_linkkit_dev_meta_info_t *subdev_list, int subdev_num);
|
||||
|
||||
/*
|
||||
[params]
|
||||
subdev_mate:one subdev meta info
|
||||
|
||||
[return]
|
||||
SUCCESS_RETURN:success
|
||||
FAIL_RETURN:failed
|
||||
*/
|
||||
extern GATEWAY_API int gateway_del_subdev(iotx_linkkit_dev_meta_info_t *subdev_mate);
|
||||
|
||||
/*
|
||||
[params]
|
||||
subdev_mate:one subdev meta info
|
||||
|
||||
[return]
|
||||
SUCCESS_RETURN:success
|
||||
FAIL_RETURN:failed
|
||||
*/
|
||||
extern GATEWAY_API int gateway_reset_subdev(iotx_linkkit_dev_meta_info_t *subdev_mate);
|
||||
|
||||
/*
|
||||
[params]
|
||||
master_devid:master device id
|
||||
subdev_mate:one subdev meta info
|
||||
|
||||
[return]
|
||||
subdev_id:>0 success <=0 failed
|
||||
*/
|
||||
extern GATEWAY_API int gateway_query_subdev_id(int master_devid, iotx_linkkit_dev_meta_info_t *subdev_mate);
|
||||
|
||||
#endif
|
||||
599
Products/example/linkkit_gateway/gateway_cmds.c
Normal file
599
Products/example/linkkit_gateway/gateway_cmds.c
Normal file
|
|
@ -0,0 +1,599 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2019 Alibaba Group Holding Limited
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <aos/aos.h>
|
||||
#include <aos/yloop.h>
|
||||
#include "netmgr.h"
|
||||
#include "iot_export.h"
|
||||
#include "iot_import.h"
|
||||
#include "aos/kv.h"
|
||||
|
||||
#include <k_api.h>
|
||||
|
||||
#include "gateway_main.h"
|
||||
#include "gateway_entry.h"
|
||||
#include "gateway_cmds.h"
|
||||
#include "gateway_ut.h"
|
||||
|
||||
static char awss_running = 0;
|
||||
|
||||
static void awss_close_dev_ap(void *p)
|
||||
{
|
||||
awss_dev_ap_stop();
|
||||
gateway_info("%s exit\n", __func__);
|
||||
aos_task_exit(0);
|
||||
}
|
||||
|
||||
void gateway_open_dev_ap(void *p)
|
||||
{
|
||||
iotx_event_regist_cb(user_event_monitor);
|
||||
gateway_info("%s\n", __func__);
|
||||
if (netmgr_start(false) != 0)
|
||||
{
|
||||
aos_msleep(2000);
|
||||
awss_dev_ap_start();
|
||||
}
|
||||
aos_task_exit(0);
|
||||
}
|
||||
|
||||
static void stop_netmgr(void *p)
|
||||
{
|
||||
awss_stop();
|
||||
gateway_info("%s\n", __func__);
|
||||
aos_task_exit(0);
|
||||
}
|
||||
|
||||
void gateway_start_netmgr(void *p)
|
||||
{
|
||||
iotx_event_regist_cb(user_event_monitor);
|
||||
netmgr_start(true);
|
||||
aos_task_exit(0);
|
||||
}
|
||||
|
||||
void gateway_do_awss_active(void)
|
||||
{
|
||||
gateway_info("gateway_do_awss_active %d\n", awss_running);
|
||||
awss_running = 1;
|
||||
#ifdef WIFI_PROVISION_ENABLED
|
||||
extern int awss_config_press();
|
||||
awss_config_press();
|
||||
#endif
|
||||
}
|
||||
|
||||
void do_awss_dev_ap(void)
|
||||
{
|
||||
aos_task_new("netmgr_stop", stop_netmgr, NULL, 4096);
|
||||
aos_task_new("dap_open", gateway_open_dev_ap, NULL, 4096);
|
||||
}
|
||||
|
||||
void do_awss()
|
||||
{
|
||||
aos_task_new("dap_close", awss_close_dev_ap, NULL, 2048);
|
||||
aos_task_new("netmgr_start", gateway_start_netmgr, NULL, 4096);
|
||||
}
|
||||
|
||||
static void linkkit_reset(void *p)
|
||||
{
|
||||
iotx_sdk_reset_local();
|
||||
HAL_Reboot();
|
||||
}
|
||||
|
||||
static iotx_vendor_dev_reset_type_t reset_type = IOTX_VENDOR_DEV_RESET_TYPE_UNBIND_SHADOW_CLEAR;
|
||||
|
||||
void gateway_awss_reset(void)
|
||||
{
|
||||
#ifdef WIFI_PROVISION_ENABLED
|
||||
aos_task_new("reset", (void (*)(void *))iotx_sdk_reset, (void *)&reset_type, 4096); // stack taken by iTLS is more than taken by TLS.
|
||||
#endif
|
||||
aos_post_delayed_action(2000, linkkit_reset, NULL);
|
||||
}
|
||||
|
||||
#ifdef CONFIG_AOS_CLI
|
||||
static void handle_reset_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
aos_schedule_call(gateway_awss_reset, NULL);
|
||||
}
|
||||
|
||||
static void handle_active_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
aos_schedule_call(gateway_do_awss_active, NULL);
|
||||
}
|
||||
|
||||
static void handle_dev_ap_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
aos_schedule_call(do_awss_dev_ap, NULL);
|
||||
}
|
||||
|
||||
static void handle_awss_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
aos_schedule_call(do_awss, NULL);
|
||||
}
|
||||
|
||||
#if defined(CONFIG_AOS_CLI) && defined(OFFLINE_LOG_UT_TEST)
|
||||
static void handle_offline_log_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (argc < 2)
|
||||
{
|
||||
aos_cli_printf("params err,oll [save/upload/erase] or [read [0-1]] or [write [0-3] \"test offline log\"]\r\n");
|
||||
return;
|
||||
}
|
||||
|
||||
aos_cli_printf("cmd:%s\r\n", argv[1]);
|
||||
|
||||
if (!strcmp("save", argv[1]) && argc == 2)
|
||||
{
|
||||
diagnosis_offline_log_save_all();
|
||||
}
|
||||
else if (!strcmp("upload", argv[1]) && argc == 4)
|
||||
{
|
||||
int port = atoi(argv[3]);
|
||||
|
||||
diagnosis_offline_log_upload(argv[2], port);
|
||||
}
|
||||
else if (!strcmp("write", argv[1]) && argc == 4)
|
||||
{
|
||||
int loglevel = atoi(argv[2]);
|
||||
|
||||
if (loglevel >= 0 && loglevel <= 3)
|
||||
{
|
||||
diagnosis_offline_log(loglevel, "%s\\n", argv[3]);
|
||||
}
|
||||
}
|
||||
else if (!strcmp("read", argv[1]) && argc == 3)
|
||||
{
|
||||
int log_mode = atoi(argv[2]);
|
||||
|
||||
diagnosis_offline_log_read_all(log_mode);
|
||||
}
|
||||
else if (!strcmp("erase", argv[1]) && argc == 2)
|
||||
{
|
||||
diagnosis_offline_log_erase_flash_desc();
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("cmd:%s is not support\r\n", argv[1]);
|
||||
}
|
||||
|
||||
if (ret != 0)
|
||||
{
|
||||
aos_cli_printf("handle oll cmd:%s failed:%d\r\n", argv[1], ret);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static struct cli_command offline_log_cmd = {.name = "oll",
|
||||
.help = "oll [save]/[erase] or [upload host port] or [read [0-1]] or [write [0-3] \"test offline log\"]",
|
||||
.function = handle_offline_log_cmd};
|
||||
|
||||
#endif
|
||||
|
||||
static void handle_linkkey_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
int index = 0;
|
||||
char key_buf[MAX_KEY_LEN];
|
||||
|
||||
if (argc == 1)
|
||||
{
|
||||
int len = 0;
|
||||
char product_key[PRODUCT_KEY_LEN + 1] = {0};
|
||||
char product_secret[PRODUCT_SECRET_LEN + 1] = {0};
|
||||
char device_name[DEVICE_NAME_LEN + 1] = {0};
|
||||
char device_secret[DEVICE_SECRET_LEN + 1] = {0};
|
||||
|
||||
for (index = 0; index < MAX_DEVICES_META_NUM; index++)
|
||||
{
|
||||
len = PRODUCT_KEY_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(product_key, 0, sizeof(product_key));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PK, index);
|
||||
aos_kv_get(key_buf, product_key, &len);
|
||||
|
||||
len = PRODUCT_SECRET_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(product_secret, 0, sizeof(product_secret));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PS, index);
|
||||
aos_kv_get(key_buf, product_secret, &len);
|
||||
|
||||
len = DEVICE_NAME_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(device_name, 0, sizeof(device_name));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DN, index);
|
||||
aos_kv_get(key_buf, device_name, &len);
|
||||
|
||||
len = DEVICE_SECRET_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(device_secret, 0, sizeof(device_secret));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DS, index);
|
||||
aos_kv_get(key_buf, device_secret, &len);
|
||||
|
||||
if (index == 0)
|
||||
{
|
||||
aos_cli_printf("\r\nMaster Dev:\r\n");
|
||||
aos_cli_printf("PK=%s.\r\n", product_key);
|
||||
aos_cli_printf("PS=%s.\r\n", product_secret);
|
||||
aos_cli_printf("DN=%s.\r\n", device_name);
|
||||
aos_cli_printf("DS=%s.\r\n", device_secret);
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("\r\nSubDev(%d):\r\n", index);
|
||||
aos_cli_printf("PK=%s.\r\n", product_key);
|
||||
aos_cli_printf("PS=%s.\r\n", product_secret);
|
||||
aos_cli_printf("DN=%s.\r\n", device_name);
|
||||
aos_cli_printf("DS=%s.\r\n", device_secret);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (argc == 6)
|
||||
{
|
||||
int devid = atoi(argv[1]);
|
||||
|
||||
if (devid >= 0 && devid <= MAX_DEVICES_META_NUM)
|
||||
{
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PK, devid);
|
||||
aos_kv_set(key_buf, argv[2], strlen(argv[2]) + 1, 1);
|
||||
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DN, devid);
|
||||
aos_kv_set(key_buf, argv[3], strlen(argv[3]) + 1, 1);
|
||||
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DS, devid);
|
||||
aos_kv_set(key_buf, argv[4], strlen(argv[4]) + 1, 1);
|
||||
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PS, devid);
|
||||
aos_kv_set(key_buf, argv[5], strlen(argv[5]) + 1, 1);
|
||||
|
||||
aos_cli_printf("Done\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("Err:devid range[0-%d]", MAX_DEVICES_META_NUM - 1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("Params Err\r\n");
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef GATEWAY_UT_TESTING
|
||||
//This func just to simulate gateway operate subdev
|
||||
void handle_gw_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
gateway_msg_t msg;
|
||||
int ret = 0;
|
||||
|
||||
if (argc < 2)
|
||||
{
|
||||
gateway_err("params err,gw [add/del/reset/queryid subdevid] or gw [addall/update]");
|
||||
return;
|
||||
}
|
||||
|
||||
gateway_info("cmd:%s", argv[1]);
|
||||
|
||||
if (!strcmp("add", argv[1]) && argc == 3)
|
||||
{
|
||||
int devid = atoi(argv[2]);
|
||||
|
||||
if (devid > 0 && devid < MAX_DEVICES_META_NUM)
|
||||
{
|
||||
msg.msg_type = GATEWAY_MSG_TYPE_ADD;
|
||||
msg.devid = devid;
|
||||
|
||||
ret = gateway_ut_send_msg(&msg, sizeof(gateway_msg_t));
|
||||
}
|
||||
else
|
||||
{
|
||||
gateway_warn("devid should in range[1-%d]", MAX_DEVICES_META_NUM - 1);
|
||||
}
|
||||
}
|
||||
else if (!strcmp("addr", argv[1]) && argc == 4)
|
||||
{
|
||||
int devid = atoi(argv[2]);
|
||||
int devid_end = atoi(argv[3]);
|
||||
|
||||
if (devid > 0 && devid < MAX_DEVICES_META_NUM &&
|
||||
devid_end > 0 && devid_end < MAX_DEVICES_META_NUM &&
|
||||
devid < devid_end)
|
||||
{
|
||||
msg.msg_type = GATEWAY_MSG_TYPE_ADD_RANGE;
|
||||
msg.devid = devid;
|
||||
msg.devid_end = devid_end;
|
||||
|
||||
ret = gateway_ut_send_msg(&msg, sizeof(gateway_msg_t));
|
||||
}
|
||||
else
|
||||
{
|
||||
gateway_warn("devid should in range[1-%d]", MAX_DEVICES_META_NUM - 1);
|
||||
}
|
||||
}
|
||||
else if (!strcmp("del", argv[1]) && argc == 3)
|
||||
{
|
||||
int devid = atoi(argv[2]);
|
||||
|
||||
if (devid > 0 && devid < MAX_DEVICES_META_NUM)
|
||||
{
|
||||
msg.msg_type = GATEWAY_MSG_TYPE_DEL;
|
||||
msg.devid = devid;
|
||||
|
||||
ret = gateway_ut_send_msg(&msg, sizeof(gateway_msg_t));
|
||||
}
|
||||
else
|
||||
{
|
||||
gateway_warn("devid should in range[1-%d]", MAX_DEVICES_META_NUM - 1);
|
||||
}
|
||||
}
|
||||
else if (!strcmp("delr", argv[1]) && argc == 4)
|
||||
{
|
||||
int devid = atoi(argv[2]);
|
||||
int devid_end = atoi(argv[3]);
|
||||
|
||||
if (devid > 0 && devid < MAX_DEVICES_META_NUM &&
|
||||
devid_end > 0 && devid_end < MAX_DEVICES_META_NUM &&
|
||||
devid < devid_end)
|
||||
{
|
||||
msg.msg_type = GATEWAY_MSG_TYPE_DEL_RANGE;
|
||||
msg.devid = devid;
|
||||
msg.devid_end = devid_end;
|
||||
|
||||
ret = gateway_ut_send_msg(&msg, sizeof(gateway_msg_t));
|
||||
}
|
||||
else
|
||||
{
|
||||
gateway_warn("devid should in range[1-%d]", MAX_DEVICES_META_NUM - 1);
|
||||
}
|
||||
}
|
||||
else if (!strcmp("addall", argv[1]) && argc == 2)
|
||||
{
|
||||
msg.msg_type = GATEWAY_MSG_TYPE_ADDALL;
|
||||
msg.devid = 1;
|
||||
|
||||
ret = gateway_ut_send_msg(&msg, sizeof(gateway_msg_t));
|
||||
}
|
||||
else if (!strcmp("delall", argv[1]) && argc == 2)
|
||||
{
|
||||
msg.msg_type = GATEWAY_MSG_TYPE_DELALL;
|
||||
msg.devid = 1;
|
||||
|
||||
ret = gateway_ut_send_msg(&msg, sizeof(gateway_msg_t));
|
||||
}
|
||||
else if (!strcmp("update", argv[1]) && argc == 2)
|
||||
{
|
||||
msg.msg_type = GATEWAY_MSG_TYPE_UPDATE;
|
||||
msg.devid = 1;
|
||||
|
||||
ret = gateway_ut_send_msg(&msg, sizeof(gateway_msg_t));
|
||||
}
|
||||
else if (!strcmp("reset", argv[1]) && argc == 3)
|
||||
{
|
||||
int devid = atoi(argv[2]);
|
||||
|
||||
if (devid > 0 && devid < MAX_DEVICES_META_NUM)
|
||||
{
|
||||
msg.msg_type = GATEWAY_MSG_TYPE_RESET;
|
||||
msg.devid = devid;
|
||||
|
||||
ret = gateway_ut_send_msg(&msg, sizeof(gateway_msg_t));
|
||||
}
|
||||
else
|
||||
{
|
||||
gateway_warn("devid should in range[1-%d]", MAX_DEVICES_META_NUM - 1);
|
||||
}
|
||||
}
|
||||
else if (!strcmp("queryid", argv[1]) && argc == 3)
|
||||
{
|
||||
int devid = atoi(argv[2]);
|
||||
|
||||
if (devid > 0 && devid < MAX_DEVICES_META_NUM)
|
||||
{
|
||||
msg.msg_type = GATEWAY_MSG_TYPE_QUERY_SUBDEV_ID;
|
||||
msg.devid = devid; //This devid is device in KV
|
||||
|
||||
ret = gateway_ut_send_msg(&msg, sizeof(gateway_msg_t));
|
||||
}
|
||||
else
|
||||
{
|
||||
gateway_warn("devid should in range[1-%d]", MAX_DEVICES_META_NUM - 1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
gateway_warn("cmd:%s is not support", argv[1]);
|
||||
}
|
||||
|
||||
if (ret != 0)
|
||||
{
|
||||
gateway_err("handle gw cmd:%s failed:%d", argv[1], ret);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef DEV_ERRCODE_ENABLE
|
||||
#define DEV_DIAG_CLI_SET_ERRCODE "set_err"
|
||||
#define DEV_DIAG_CLI_ERRCODE_MANUAL_MSG "errcode was manually set"
|
||||
#define DEV_DIAG_CLI_GET_ERRCODE "get_err"
|
||||
#define DEV_DIAG_CLI_DEL_ERRCODE "del_err"
|
||||
|
||||
static void handle_dev_serv_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
uint16_t err_code = 0;
|
||||
char err_msg[DEV_ERRCODE_MSG_MAX_LEN] = {0};
|
||||
uint8_t ret = 0;
|
||||
if (argc == 1)
|
||||
{
|
||||
aos_cli_printf("device diagnosis service test, please input more parameters.\r\n");
|
||||
}
|
||||
else if (argc > 1)
|
||||
{
|
||||
if ((argc == 3) && (!strcmp(argv[1], DEV_DIAG_CLI_SET_ERRCODE)))
|
||||
{
|
||||
err_code = (uint16_t)strtoul(argv[2], NULL, 0);
|
||||
ret = dev_errcode_kv_set(err_code, DEV_DIAG_CLI_ERRCODE_MANUAL_MSG);
|
||||
if (ret == 0)
|
||||
{
|
||||
aos_cli_printf("manually set err_code=%d\r\n", err_code);
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("manually set err_code failed!\r\n");
|
||||
}
|
||||
}
|
||||
else if ((argc == 2) && (!strcmp(argv[1], DEV_DIAG_CLI_GET_ERRCODE)))
|
||||
{
|
||||
ret = dev_errcode_kv_get(&err_code, err_msg);
|
||||
if (ret == 0)
|
||||
{
|
||||
aos_cli_printf("get err_code=%d, err_msg=%s\r\n", err_code, err_msg);
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("get err_code fail or no err_code found!\r\n");
|
||||
}
|
||||
}
|
||||
else if ((argc == 2) && (!strcmp(argv[1], DEV_DIAG_CLI_DEL_ERRCODE)))
|
||||
{
|
||||
dev_errcode_kv_del();
|
||||
aos_cli_printf("del err_code\r\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("Error: %d\r\n", __LINE__);
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef MANUFACT_AP_FIND_ENABLE
|
||||
|
||||
#define MANUAP_CONTROL_KEY "manuap"
|
||||
#define MANUAP_CLI_OPEN "open"
|
||||
#define MANUAP_CLI_CLOSE "close"
|
||||
|
||||
static void handle_manuap_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
int ret = 0;
|
||||
uint8_t manuap_flag = 0;
|
||||
int manu_flag_len = sizeof(manuap_flag);
|
||||
if (argc == 1)
|
||||
{
|
||||
ret = aos_kv_get(MANUAP_CONTROL_KEY, &manuap_flag, &manu_flag_len);
|
||||
if (ret == 0)
|
||||
{
|
||||
if (manuap_flag)
|
||||
{
|
||||
aos_cli_printf("manuap state is open\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("manuap state is close\r\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("no manuap state stored\r\n");
|
||||
}
|
||||
}
|
||||
else if (argc == 2)
|
||||
{
|
||||
if (!strcmp(argv[1], MANUAP_CLI_OPEN))
|
||||
{
|
||||
manuap_flag = 1;
|
||||
ret = aos_kv_set(MANUAP_CONTROL_KEY, &manuap_flag, sizeof(manuap_flag), 1);
|
||||
if (ret == 0)
|
||||
{
|
||||
aos_cli_printf("manuap opened\r\n");
|
||||
}
|
||||
}
|
||||
else if (!strcmp(argv[1], MANUAP_CLI_CLOSE))
|
||||
{
|
||||
manuap_flag = 0;
|
||||
ret = aos_kv_set(MANUAP_CONTROL_KEY, &manuap_flag, sizeof(manuap_flag), 1);
|
||||
if (ret == 0)
|
||||
{
|
||||
aos_cli_printf("manuap closed\r\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("param input err\r\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("param input err\r\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static struct cli_command resetcmd = {.name = "reset",
|
||||
.help = "factory reset",
|
||||
.function = handle_reset_cmd};
|
||||
|
||||
static struct cli_command awss_enable_cmd = {.name = "active_awss",
|
||||
.help = "active_awss [start]",
|
||||
.function = handle_active_cmd};
|
||||
static struct cli_command awss_dev_ap_cmd = {.name = "dev_ap",
|
||||
.help = "awss_dev_ap [start]",
|
||||
.function = handle_dev_ap_cmd};
|
||||
static struct cli_command awss_cmd = {.name = "awss",
|
||||
.help = "awss [start]",
|
||||
.function = handle_awss_cmd};
|
||||
static struct cli_command linkkeycmd = {.name = "linkkey",
|
||||
.help = "set/get linkkit keys. linkkey [devid] [<Product Key> <Device Name> <Device Secret> <Product Secret>]",
|
||||
.function = handle_linkkey_cmd};
|
||||
#ifdef GATEWAY_UT_TESTING
|
||||
static struct cli_command gw_ops = {.name = "gw",
|
||||
.help = "ops gw subdev. gw [add/del/reset/queryid subdevid] or gw [addall/update/delall] or gw [addr/delr",
|
||||
.function = handle_gw_cmd};
|
||||
#endif
|
||||
#ifdef DEV_ERRCODE_ENABLE
|
||||
static struct cli_command dev_service_cmd = {.name = "dev_serv",
|
||||
.help = "device diagnosis service tests. like errcode, and log report",
|
||||
.function = handle_dev_serv_cmd};
|
||||
#endif
|
||||
#ifdef MANUFACT_AP_FIND_ENABLE
|
||||
static struct cli_command manuap_cmd = {.name = "manuap",
|
||||
.help = "manuap [open] or manuap [close]",
|
||||
.function = handle_manuap_cmd};
|
||||
#endif
|
||||
|
||||
int gateway_register_cmds(void)
|
||||
{
|
||||
aos_cli_register_command(&resetcmd);
|
||||
aos_cli_register_command(&awss_enable_cmd);
|
||||
aos_cli_register_command(&awss_dev_ap_cmd);
|
||||
aos_cli_register_command(&awss_cmd);
|
||||
aos_cli_register_command(&linkkeycmd);
|
||||
#ifdef GATEWAY_UT_TESTING
|
||||
aos_cli_register_command(&gw_ops);
|
||||
#endif
|
||||
#ifdef OFFLINE_LOG_UT_TEST
|
||||
aos_cli_register_command(&offline_log_cmd);
|
||||
#endif
|
||||
#ifdef DEV_ERRCODE_ENABLE
|
||||
aos_cli_register_command(&dev_service_cmd);
|
||||
#endif
|
||||
#ifdef MANUFACT_AP_FIND_ENABLE
|
||||
aos_cli_register_command(&manuap_cmd);
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
18
Products/example/linkkit_gateway/gateway_cmds.h
Normal file
18
Products/example/linkkit_gateway/gateway_cmds.h
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#ifndef __GATEWAY_CMDS_H__
|
||||
#define __GATEWAY_CMDS_H__
|
||||
|
||||
#define KV_KEY_PK "pk"
|
||||
#define KV_KEY_PS "ps"
|
||||
#define KV_KEY_DN "dn"
|
||||
#define KV_KEY_DS "ds"
|
||||
|
||||
#define MAX_KEY_LEN (6)
|
||||
#define MAX_DEVICES_META_NUM (11) //support 10 sub devices
|
||||
|
||||
extern int gateway_register_cmds(void);
|
||||
extern void gateway_start_netmgr(void *p);
|
||||
extern void gateway_open_dev_ap(void *p);
|
||||
extern void gateway_awss_reset(void);
|
||||
extern void gateway_do_awss_active(void);
|
||||
|
||||
#endif
|
||||
449
Products/example/linkkit_gateway/gateway_entry.c
Executable file
449
Products/example/linkkit_gateway/gateway_entry.c
Executable file
|
|
@ -0,0 +1,449 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2019 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <aos/aos.h>
|
||||
#include <aos/yloop.h>
|
||||
#include "netmgr.h"
|
||||
#include "iot_export.h"
|
||||
#include "iot_import.h"
|
||||
#include "aos/kv.h"
|
||||
|
||||
#ifdef CSP_LINUXHOST
|
||||
#include <signal.h>
|
||||
#endif
|
||||
|
||||
#include <k_api.h>
|
||||
|
||||
#if defined(OTA_ENABLED) && defined(BUILD_AOS)
|
||||
#include "ota_service.h"
|
||||
#endif
|
||||
|
||||
#include "gateway_main.h"
|
||||
#include "gateway_cmds.h"
|
||||
#include "gateway_entry.h"
|
||||
#include "gateway_ut.h"
|
||||
|
||||
static char gateway_started = 0;
|
||||
static int gateway_duration_interval = 0;
|
||||
|
||||
void set_iotx_info();
|
||||
void do_awss_active();
|
||||
|
||||
extern k_mm_head *g_kmm_head;
|
||||
extern uint32_t dumpsys_mm_info_func(uint32_t len);
|
||||
extern uint32_t dumpsys_task_func(char *buf, uint32_t len, int detail);
|
||||
static void dump_task_and_mem_info(void)
|
||||
{
|
||||
#if defined(CONFIG_AOS_CLI)
|
||||
printf("======================================");
|
||||
dumpsys_mm_info_func(0);
|
||||
printf("======================================");
|
||||
dumpsys_task_func(NULL, 0, 1);
|
||||
printf("======================================");
|
||||
#else
|
||||
printf("============free heap size =%d==========\r\n", g_kmm_head->free_size);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef GATEWAY_UT_STABILITY_TESTING
|
||||
static int switch_cmd = 0;
|
||||
int send_addall_subdev(void)
|
||||
{
|
||||
gateway_msg_t msg;
|
||||
|
||||
msg.msg_type = GATEWAY_MSG_TYPE_ADDALL;
|
||||
msg.devid = 1;
|
||||
#ifdef GATEWAY_UT_TESTING
|
||||
gateway_ut_send_msg(&msg, sizeof(gateway_msg_t));
|
||||
#endif
|
||||
}
|
||||
int send_delall_subdev(void)
|
||||
{
|
||||
gateway_msg_t msg;
|
||||
|
||||
msg.msg_type = GATEWAY_MSG_TYPE_DELALL;
|
||||
msg.devid = 1;
|
||||
#ifdef GATEWAY_UT_TESTING
|
||||
gateway_ut_send_msg(&msg, sizeof(gateway_msg_t));
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
|
||||
static void gateway_duration_work(void *param)
|
||||
{
|
||||
dump_task_and_mem_info();
|
||||
|
||||
#ifdef GATEWAY_UT_STABILITY_TESTING
|
||||
if (switch_cmd == 0)
|
||||
{
|
||||
send_addall_subdev();
|
||||
switch_cmd = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
send_delall_subdev();
|
||||
switch_cmd = 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
aos_post_delayed_action(gateway_duration_interval, gateway_duration_work, NULL);
|
||||
}
|
||||
|
||||
static void wifi_service_event(input_event_t *event, void *priv_data)
|
||||
{
|
||||
netmgr_ap_config_t config;
|
||||
|
||||
if (event->type != EV_WIFI || event->code != CODE_WIFI_ON_GOT_IP)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
memset(&config, 0, sizeof(netmgr_ap_config_t));
|
||||
netmgr_get_ap_config(&config);
|
||||
|
||||
gateway_info("wifi_service_event config.ssid:%s", config.ssid);
|
||||
if (strcmp(config.ssid, "adha") == 0 || strcmp(config.ssid, "aha") == 0)
|
||||
{
|
||||
// clear_wifi_ssid();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!gateway_started)
|
||||
{
|
||||
aos_task_new("gateway_main", (void (*)(void *))gateway_main, NULL, GATEWAY_MAIN_THREAD_STACKSZIE);
|
||||
gateway_started = 1;
|
||||
}
|
||||
}
|
||||
|
||||
static void cloud_service_event(input_event_t *event, void *priv_data)
|
||||
{
|
||||
if (event->type != EV_YUNIO)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
gateway_info("cloud_service_event %d", event->code);
|
||||
|
||||
if (event->code == CODE_YUNIO_ON_CONNECTED)
|
||||
{
|
||||
gateway_info("user sub and pub here");
|
||||
return;
|
||||
}
|
||||
|
||||
if (event->code == CODE_YUNIO_ON_DISCONNECTED)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Note:
|
||||
* the user_event_monitor must not block and should run to complete fast
|
||||
* if user wants to do complex operation with much time,
|
||||
* user should post one task to do this, not implement complex operation in
|
||||
* user_event_monitor
|
||||
*/
|
||||
|
||||
void user_event_monitor(int event)
|
||||
{
|
||||
switch (event)
|
||||
{
|
||||
case IOTX_AWSS_START: // AWSS start without enbale, just supports device discover
|
||||
// operate led to indicate user
|
||||
gateway_info("IOTX_AWSS_START");
|
||||
break;
|
||||
case IOTX_AWSS_ENABLE: // AWSS enable, AWSS doesn't parse awss packet until AWSS is enabled.
|
||||
gateway_info("IOTX_AWSS_ENABLE");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_LOCK_CHAN: // AWSS lock channel(Got AWSS sync packet)
|
||||
gateway_info("IOTX_AWSS_LOCK_CHAN");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_PASSWD_ERR: // AWSS decrypt passwd error
|
||||
gateway_info("IOTX_AWSS_PASSWD_ERR");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_GOT_SSID_PASSWD:
|
||||
gateway_info("IOTX_AWSS_GOT_SSID_PASSWD");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_ADHA: // AWSS try to connnect adha (device
|
||||
// discover, router solution)
|
||||
gateway_info("IOTX_AWSS_CONNECT_ADHA");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_ADHA_FAIL: // AWSS fails to connect adha
|
||||
gateway_info("IOTX_AWSS_CONNECT_ADHA_FAIL");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_AHA: // AWSS try to connect aha (AP solution)
|
||||
gateway_info("IOTX_AWSS_CONNECT_AHA");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_AHA_FAIL: // AWSS fails to connect aha
|
||||
gateway_info("IOTX_AWSS_CONNECT_AHA_FAIL");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_SETUP_NOTIFY: // AWSS sends out device setup information
|
||||
// (AP and router solution)
|
||||
gateway_info("IOTX_AWSS_SETUP_NOTIFY");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_ROUTER: // AWSS try to connect destination router
|
||||
gateway_info("IOTX_AWSS_CONNECT_ROUTER");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_ROUTER_FAIL: // AWSS fails to connect destination
|
||||
// router.
|
||||
gateway_info("IOTX_AWSS_CONNECT_ROUTER_FAIL");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_GOT_IP: // AWSS connects destination successfully and got
|
||||
// ip address
|
||||
gateway_info("IOTX_AWSS_GOT_IP");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_SUC_NOTIFY: // AWSS sends out success notify (AWSS
|
||||
// sucess)
|
||||
gateway_info("IOTX_AWSS_SUC_NOTIFY");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_BIND_NOTIFY: // AWSS sends out bind notify information to
|
||||
// support bind between user and device
|
||||
gateway_info("IOTX_AWSS_BIND_NOTIFY");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_ENABLE_TIMEOUT: // AWSS enable timeout
|
||||
// user needs to enable awss again to support get ssid & passwd of router
|
||||
gateway_info("IOTX_AWSS_ENALBE_TIMEOUT");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_CONN_CLOUD: // Device try to connect cloud
|
||||
gateway_info("IOTX_CONN_CLOUD");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_CONN_CLOUD_FAIL: // Device fails to connect cloud, refer to
|
||||
// net_sockets.h for error code
|
||||
gateway_info("IOTX_CONN_CLOUD_FAIL");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_CONN_CLOUD_SUC: // Device connects cloud successfully
|
||||
gateway_info("IOTX_CONN_CLOUD_SUC");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_RESET: // Linkkit reset success (just got reset response from
|
||||
// cloud without any other operation)
|
||||
gateway_info("IOTX_RESET");
|
||||
break;
|
||||
case IOTX_CONN_REPORT_TOKEN_SUC:
|
||||
gateway_info("---- report token success ----");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void user_key_process(input_event_t *eventinfo, void *priv_data)
|
||||
{
|
||||
if (eventinfo->type != EV_KEY)
|
||||
{
|
||||
return;
|
||||
}
|
||||
gateway_info("awss config press %d\n", eventinfo->value);
|
||||
|
||||
if (eventinfo->code == CODE_BOOT)
|
||||
{
|
||||
if (eventinfo->value == VALUE_KEY_CLICK)
|
||||
{
|
||||
|
||||
gateway_do_awss_active();
|
||||
}
|
||||
else if (eventinfo->value == VALUE_KEY_LTCLICK)
|
||||
{
|
||||
gateway_awss_reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(OTA_ENABLED) && defined(BUILD_AOS)
|
||||
static ota_service_t ctx = {0};
|
||||
|
||||
static int ota_init(void)
|
||||
{
|
||||
char product_key[PRODUCT_KEY_LEN + 1] = {0};
|
||||
char device_name[DEVICE_NAME_LEN + 1] = {0};
|
||||
char device_secret[DEVICE_SECRET_LEN + 1] = {0};
|
||||
HAL_GetProductKey(product_key);
|
||||
HAL_GetDeviceName(device_name);
|
||||
HAL_GetDeviceSecret(device_secret);
|
||||
memset(&ctx, 0, sizeof(ota_service_t));
|
||||
strncpy(ctx.pk, product_key, sizeof(ctx.pk) - 1);
|
||||
strncpy(ctx.dn, device_name, sizeof(ctx.dn) - 1);
|
||||
strncpy(ctx.ds, device_secret, sizeof(ctx.ds) - 1);
|
||||
ctx.trans_protcol = 0;
|
||||
ctx.dl_protcol = 3;
|
||||
ota_service_init(&ctx);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int mqtt_connected_event_handler(void)
|
||||
{
|
||||
#if defined(OTA_ENABLED) && defined(BUILD_AOS)
|
||||
static bool ota_service_inited = false;
|
||||
|
||||
if (ota_service_inited == true)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
LOG("MQTT reconnected, let's redo OTA upgrade");
|
||||
if ((ctx.h_tr) && (ctx.h_tr->upgrade))
|
||||
{
|
||||
LOG("Redoing OTA upgrade");
|
||||
ret = ctx.h_tr->upgrade(&ctx);
|
||||
if (ret < 0)
|
||||
LOG("Failed to do OTA upgrade");
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
LOG("MQTT Construct OTA start to inform");
|
||||
#ifdef DEV_OFFLINE_OTA_ENABLE
|
||||
ota_service_inform(&ctx);
|
||||
#else
|
||||
ota_init();
|
||||
#endif
|
||||
ota_service_inited = true;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void load_device_meta_info(void)
|
||||
{
|
||||
int len = 0;
|
||||
char key_buf[MAX_KEY_LEN];
|
||||
char product_key[PRODUCT_KEY_LEN + 1] = {0};
|
||||
char product_secret[PRODUCT_SECRET_LEN + 1] = {0};
|
||||
char device_name[DEVICE_NAME_LEN + 1] = {0};
|
||||
char device_secret[DEVICE_SECRET_LEN + 1] = {0};
|
||||
|
||||
len = PRODUCT_KEY_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(product_key, 0, sizeof(product_key));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PK, 0);
|
||||
HAL_Kv_Get(key_buf, product_key, &len);
|
||||
|
||||
len = PRODUCT_SECRET_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(product_secret, 0, sizeof(product_secret));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PS, 0);
|
||||
HAL_Kv_Get(key_buf, product_secret, &len);
|
||||
|
||||
len = DEVICE_NAME_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(device_name, 0, sizeof(device_name));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DN, 0);
|
||||
HAL_Kv_Get(key_buf, device_name, &len);
|
||||
|
||||
len = DEVICE_SECRET_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(device_secret, 0, sizeof(device_secret));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DS, 0);
|
||||
HAL_Kv_Get(key_buf, device_secret, &len);
|
||||
|
||||
if ((strlen(product_key) > 0) && (strlen(product_secret) > 0) && (strlen(device_name) > 0) && (strlen(device_secret) > 0))
|
||||
{
|
||||
HAL_SetProductKey(product_key);
|
||||
HAL_SetProductSecret(product_secret);
|
||||
HAL_SetDeviceName(device_name);
|
||||
HAL_SetDeviceSecret(device_secret);
|
||||
printf("pk[%s]\r\n", product_key);
|
||||
printf("dn[%s]\r\n", device_name);
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_SetProductKey(PRODUCT_KEY);
|
||||
HAL_SetProductSecret(PRODUCT_SECRET);
|
||||
HAL_SetDeviceName(DEVICE_NAME);
|
||||
HAL_SetDeviceSecret(DEVICE_SECRET);
|
||||
printf("pk[%s]\r\n", PRODUCT_KEY);
|
||||
printf("dn[%s]\r\n", DEVICE_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
int application_start(int argc, char **argv)
|
||||
{
|
||||
int len = 0;
|
||||
char interval[9] = {0};
|
||||
|
||||
#ifdef CSP_LINUXHOST
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
#endif
|
||||
|
||||
#ifdef WITH_SAL
|
||||
sal_init();
|
||||
#endif
|
||||
|
||||
#ifdef MDAL_MAL_ICA_TEST
|
||||
HAL_MDAL_MAL_Init();
|
||||
#endif
|
||||
|
||||
#ifdef DEFAULT_LOG_LEVEL_DEBUG
|
||||
IOT_SetLogLevel(IOT_LOG_DEBUG);
|
||||
#else
|
||||
#ifdef CSP_LINUXHOST
|
||||
IOT_SetLogLevel(IOT_LOG_DEBUG);
|
||||
#else
|
||||
IOT_SetLogLevel(IOT_LOG_INFO);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
load_device_meta_info();
|
||||
|
||||
#ifdef DEV_OFFLINE_OTA_ENABLE
|
||||
ota_init();
|
||||
#endif
|
||||
|
||||
netmgr_init();
|
||||
dev_diagnosis_module_init();
|
||||
|
||||
aos_register_event_filter(EV_KEY, user_key_process, NULL);
|
||||
aos_register_event_filter(EV_WIFI, wifi_service_event, NULL);
|
||||
aos_register_event_filter(EV_YUNIO, cloud_service_event, NULL);
|
||||
|
||||
IOT_RegisterCallback(ITE_MQTT_CONNECT_SUCC, mqtt_connected_event_handler);
|
||||
|
||||
#ifdef CONFIG_AOS_CLI
|
||||
gateway_register_cmds();
|
||||
#endif
|
||||
|
||||
#if defined(AWSS_SUPPORT_DEV_AP) && defined(RDA5981x)
|
||||
aos_task_new("dap_open", gateway_open_dev_ap, NULL, 4096);
|
||||
#else
|
||||
aos_task_new("netmgr_start", gateway_start_netmgr, NULL, 4096);
|
||||
#endif
|
||||
|
||||
//This code just for debug
|
||||
len = sizeof(int);
|
||||
if (0 == HAL_Kv_Get(GATEWAY_DUMP_INTERVAL_KV_KEY, interval, &len))
|
||||
{
|
||||
gateway_duration_interval = atoi(interval);
|
||||
|
||||
if (gateway_duration_interval > 0)
|
||||
{
|
||||
aos_post_delayed_action(gateway_duration_interval, gateway_duration_work, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
aos_loop_run();
|
||||
|
||||
return 0;
|
||||
}
|
||||
9
Products/example/linkkit_gateway/gateway_entry.h
Normal file
9
Products/example/linkkit_gateway/gateway_entry.h
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
#ifndef __GATEWAY_ENTRY_H__
|
||||
#define __GATEWAY_ENTRY_H__
|
||||
|
||||
#define GATEWAY_MAIN_THREAD_STACKSZIE (8 * 1024)
|
||||
#define GATEWAY_DUMP_INTERVAL_KV_KEY "gwdump"
|
||||
|
||||
extern void user_event_monitor(int event);
|
||||
|
||||
#endif
|
||||
790
Products/example/linkkit_gateway/gateway_main.c
Executable file
790
Products/example/linkkit_gateway/gateway_main.c
Executable file
|
|
@ -0,0 +1,790 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2019 Alibaba Group Holding Limited
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "cJSON.h"
|
||||
#include "iot_import.h"
|
||||
#include "iot_export_linkkit.h"
|
||||
|
||||
#include "iotx_log.h"
|
||||
|
||||
#include "gateway_main.h"
|
||||
#include "gateway_api.h"
|
||||
#include "gateway_ut.h"
|
||||
|
||||
static gateway_ctx_t g_gateway_ctx;
|
||||
static void *permit_join_timer = NULL;
|
||||
|
||||
gateway_ctx_t *gateway_get_ctx(void)
|
||||
{
|
||||
return &g_gateway_ctx;
|
||||
}
|
||||
|
||||
static int user_connected_event_handler(void)
|
||||
{
|
||||
gateway_ctx_t *gateway_ctx = gateway_get_ctx();
|
||||
|
||||
gateway_info("Cloud Connected");
|
||||
|
||||
gateway_ctx->cloud_connected = 1;
|
||||
|
||||
if (gateway_ctx->master_initialized == 1) //initialized
|
||||
{
|
||||
#ifdef GATEWAY_UT_TESTING
|
||||
gateway_ut_update_subdev(GW_TOPO_GET_REASON_CONNECT_CLOUD);
|
||||
#endif
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_disconnected_event_handler(void)
|
||||
{
|
||||
gateway_ctx_t *gateway_ctx = gateway_get_ctx();
|
||||
|
||||
gateway_info("Cloud Disconnected");
|
||||
|
||||
gateway_ctx->cloud_connected = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_down_raw_data_arrived_event_handler(const int devid, const unsigned char *payload,
|
||||
const int payload_len)
|
||||
{
|
||||
gateway_info("Down Raw Message, Devid: %d, Payload Length: %d", devid, payload_len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_service_request_event_handler(const int devid, const char *serviceid, const int serviceid_len,
|
||||
const char *request, const int request_len,
|
||||
char **response, int *response_len)
|
||||
{
|
||||
gateway_info("Service Request Received, Devid: %d, Service ID: %.*s, Payload: %s", devid, serviceid_len,
|
||||
serviceid,
|
||||
request);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef ALCS_ENABLED
|
||||
//Just for reference,user have to change his owner properties
|
||||
static int user_property_get_event_handler(const int devid, const char *request, const int request_len, char **response,
|
||||
int *response_len)
|
||||
{
|
||||
cJSON *request_root = NULL, *item_propertyid = NULL;
|
||||
cJSON *response_root = NULL;
|
||||
int index = 0;
|
||||
gateway_info("Property Get Received, Devid: %d, Request: %s", devid, request);
|
||||
|
||||
/* Parse Request */
|
||||
request_root = cJSON_Parse(request);
|
||||
if (request_root == NULL || !cJSON_IsArray(request_root))
|
||||
{
|
||||
gateway_info("JSON Parse Error");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Prepare Response */
|
||||
response_root = cJSON_CreateObject();
|
||||
if (response_root == NULL)
|
||||
{
|
||||
gateway_info("No Enough Memory");
|
||||
cJSON_Delete(request_root);
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (index = 0; index < cJSON_GetArraySize(request_root); index++)
|
||||
{
|
||||
item_propertyid = cJSON_GetArrayItem(request_root, index);
|
||||
if (item_propertyid == NULL || !cJSON_IsString(item_propertyid))
|
||||
{
|
||||
gateway_info("JSON Parse Error");
|
||||
cJSON_Delete(request_root);
|
||||
cJSON_Delete(response_root);
|
||||
return -1;
|
||||
}
|
||||
|
||||
gateway_info("Property ID, index: %d, Value: %s", index, item_propertyid->valuestring);
|
||||
if (strcmp("WIFI_Tx_Rate", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddNumberToObject(response_root, "WIFI_Tx_Rate", 1111);
|
||||
}
|
||||
else if (strcmp("WIFI_Rx_Rate", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddNumberToObject(response_root, "WIFI_Rx_Rate", 2222);
|
||||
}
|
||||
else if (strcmp("LocalTimer", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON *array_localtimer = cJSON_CreateArray();
|
||||
if (array_localtimer == NULL)
|
||||
{
|
||||
cJSON_Delete(request_root);
|
||||
cJSON_Delete(response_root);
|
||||
return -1;
|
||||
}
|
||||
|
||||
cJSON *item_localtimer = cJSON_CreateObject();
|
||||
if (item_localtimer == NULL)
|
||||
{
|
||||
cJSON_Delete(request_root);
|
||||
cJSON_Delete(response_root);
|
||||
cJSON_Delete(array_localtimer);
|
||||
return -1;
|
||||
}
|
||||
cJSON_AddStringToObject(item_localtimer, "Timer", "10 11 * * * 1 2 3 4 5");
|
||||
cJSON_AddNumberToObject(item_localtimer, "Enable", 1);
|
||||
cJSON_AddNumberToObject(item_localtimer, "IsValid", 1);
|
||||
cJSON_AddItemToArray(array_localtimer, item_localtimer);
|
||||
cJSON_AddItemToObject(response_root, "LocalTimer", array_localtimer);
|
||||
}
|
||||
}
|
||||
cJSON_Delete(request_root);
|
||||
|
||||
*response = cJSON_PrintUnformatted(response_root);
|
||||
if (*response == NULL)
|
||||
{
|
||||
gateway_info("No Enough Memory");
|
||||
cJSON_Delete(response_root);
|
||||
return -1;
|
||||
}
|
||||
cJSON_Delete(response_root);
|
||||
*response_len = strlen(*response);
|
||||
|
||||
gateway_info("Property Get Response: %s", *response);
|
||||
|
||||
return SUCCESS_RETURN;
|
||||
}
|
||||
#endif
|
||||
|
||||
//When code is not 200,maybe call this function
|
||||
static int user_property_cloud_error_handler(const int code, const char *data, const char *detail)
|
||||
{
|
||||
gateway_info("code =%d ,data=%s, detail=%s", code, data, detail);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_property_set_event_handler(const int devid, const char *request, const int request_len)
|
||||
{
|
||||
int res = 0;
|
||||
gateway_ctx_t *gateway_ctx = gateway_get_ctx();
|
||||
gateway_info("Property Set Received, Devid: %d, Request: %s", devid, request);
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(unsigned char *)request, request_len);
|
||||
|
||||
gateway_info("Post Property Message ID: %d", res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_report_reply_event_handler(const int devid, const int msgid, const int code, const char *reply,
|
||||
const int reply_len)
|
||||
{
|
||||
const char *reply_value = (reply == NULL) ? ("NULL") : (reply);
|
||||
const int reply_value_len = (reply_len == 0) ? (strlen("NULL")) : (reply_len);
|
||||
|
||||
gateway_info("Message Post Reply Received, Devid: %d, Message ID: %d, Code: %d, Reply: %.*s", devid, msgid, code,
|
||||
reply_value_len,
|
||||
reply_value);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_trigger_event_reply_event_handler(const int devid, const int msgid, const int code, const char *eventid,
|
||||
const int eventid_len, const char *message, const int message_len)
|
||||
{
|
||||
gateway_info("Trigger Event Reply Received, Devid: %d, Message ID: %d, Code: %d, EventID: %.*s, Message: %.*s", devid,
|
||||
msgid, code,
|
||||
eventid_len,
|
||||
eventid, message_len, message);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_timestamp_reply_event_handler(const char *timestamp)
|
||||
{
|
||||
gateway_info("Current Timestamp: %s", timestamp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint64_t user_update_sec(void)
|
||||
{
|
||||
static uint64_t time_start_ms = 0;
|
||||
|
||||
if (time_start_ms == 0)
|
||||
{
|
||||
time_start_ms = HAL_UptimeMs();
|
||||
}
|
||||
|
||||
return (HAL_UptimeMs() - time_start_ms) / 1000;
|
||||
}
|
||||
|
||||
static int user_initialized(const int devid)
|
||||
{
|
||||
gateway_ctx_t *gateway_ctx = gateway_get_ctx();
|
||||
gateway_info("Device Initialized, Devid: %d", devid);
|
||||
|
||||
if (gateway_ctx->master_devid == devid)
|
||||
{
|
||||
gateway_ctx->master_initialized = 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_master_dev_available(void)
|
||||
{
|
||||
gateway_ctx_t *gateway_ctx = gateway_get_ctx();
|
||||
|
||||
if (gateway_ctx->cloud_connected && gateway_ctx->master_initialized)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int gw_notify_msg_handle(int devid, const char *request, const int request_len)
|
||||
{
|
||||
int ret = 0;
|
||||
cJSON *request_root = NULL;
|
||||
cJSON *item = NULL;
|
||||
gateway_msg_t gw_msg;
|
||||
|
||||
request_root = cJSON_Parse(request);
|
||||
if (request_root == NULL)
|
||||
{
|
||||
gateway_info("JSON Parse Error");
|
||||
return -1;
|
||||
}
|
||||
|
||||
item = cJSON_GetObjectItem(request_root, "identifier");
|
||||
if (item == NULL || !cJSON_IsString(item))
|
||||
{
|
||||
cJSON_Delete(request_root);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!strcmp(item->valuestring, "awss.BindNotify"))
|
||||
{
|
||||
cJSON *value = cJSON_GetObjectItem(request_root, "value");
|
||||
if (value == NULL || !cJSON_IsObject(value))
|
||||
{
|
||||
cJSON_Delete(request_root);
|
||||
return -1;
|
||||
}
|
||||
cJSON *op = cJSON_GetObjectItem(value, "Operation");
|
||||
if (op != NULL && cJSON_IsString(op))
|
||||
{
|
||||
if (!strcmp(op->valuestring, "Bind"))
|
||||
{
|
||||
gateway_info("Device Bind");
|
||||
}
|
||||
else if (!strcmp(op->valuestring, "Unbind"))
|
||||
{
|
||||
gateway_info("Device unBind");
|
||||
if (devid > 0) //User unbind subdev
|
||||
{
|
||||
memset(&gw_msg, 0, sizeof(gateway_msg_t));
|
||||
gw_msg.msg_type = GATEWAY_MSG_TYPE_DEL;
|
||||
gw_msg.devid = devid;
|
||||
|
||||
ret = gateway_ut_send_msg(&gw_msg, sizeof(gateway_msg_t));
|
||||
}
|
||||
}
|
||||
else if (!strcmp(op->valuestring, "Reset"))
|
||||
{
|
||||
gateway_info("Device reset");
|
||||
if (devid > 0) //User reset subdev
|
||||
{
|
||||
memset(&gw_msg, 0, sizeof(gateway_msg_t));
|
||||
gw_msg.msg_type = GATEWAY_MSG_TYPE_RESET;
|
||||
gw_msg.devid = devid;
|
||||
|
||||
ret = gateway_ut_send_msg(&gw_msg, sizeof(gateway_msg_t));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cJSON_Delete(request_root);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_event_notify_handler(const int devid, const char *request, const int request_len)
|
||||
{
|
||||
int res = 0;
|
||||
gateway_ctx_t *gateway_ctx = gateway_get_ctx();
|
||||
gateway_info("Event notify Received, Devid: %d, Request: %s", devid, request);
|
||||
|
||||
gw_notify_msg_handle(devid, request, request_len);
|
||||
res = IOT_Linkkit_Report(gateway_ctx->master_devid, ITM_MSG_EVENT_NOTIFY_REPLY,
|
||||
(unsigned char *)request, request_len);
|
||||
gateway_info("Post Property Message ID: %d", res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//get topo list reply
|
||||
static int user_topolist_reply_handler(const int devid, const int id, const int code, const char *payload, const int payload_len)
|
||||
{
|
||||
gateway_info("Receive topolist reply, code:%d, payload:%s", code, payload);
|
||||
|
||||
#ifdef GATEWAY_UT_TESTING
|
||||
if (code == 200 && payload && payload_len > 0)
|
||||
{
|
||||
gateway_ut_handle_topolist_reply(payload, payload_len);
|
||||
}
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int permit_join_timer_cb(void)
|
||||
{
|
||||
gateway_ctx_t *gateway_ctx = gateway_get_ctx();
|
||||
|
||||
if (permit_join_timer)
|
||||
{
|
||||
HAL_Timer_Stop(permit_join_timer);
|
||||
HAL_Timer_Delete(permit_join_timer);
|
||||
permit_join_timer = NULL;
|
||||
gateway_ctx->permit_join = 0;
|
||||
gateway_info("delete permit join timer");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_permit_join_event_handler(const char *product_key, const int time)
|
||||
{
|
||||
gateway_ctx_t *gateway_ctx = gateway_get_ctx();
|
||||
|
||||
gateway_info("Product Key: %s, Time: %dS", product_key, time);
|
||||
|
||||
memset(gateway_ctx->permit_join_pk, '\0', PRODUCT_KEY_MAXLEN);
|
||||
if (strlen(product_key) > 0 && strlen(product_key) < PRODUCT_KEY_MAXLEN)
|
||||
{
|
||||
HAL_Snprintf(gateway_ctx->permit_join_pk, PRODUCT_KEY_MAXLEN, "%s", product_key);
|
||||
}
|
||||
|
||||
gateway_ctx->permit_join = 1;
|
||||
|
||||
if (permit_join_timer) //Just restart this timer
|
||||
{
|
||||
HAL_Timer_Stop(permit_join_timer);
|
||||
HAL_Timer_Start(permit_join_timer, time * 1000);
|
||||
}
|
||||
else if (time > 0)
|
||||
{
|
||||
permit_join_timer = HAL_Timer_Create("oll_upload", (void (*)(void *))permit_join_timer_cb, NULL);
|
||||
if (permit_join_timer)
|
||||
{
|
||||
HAL_Timer_Stop(permit_join_timer);
|
||||
HAL_Timer_Start(permit_join_timer, time * 1000);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef GATEWAY_UT_TESTING
|
||||
gateway_ut_handle_permit_join();
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef GATEWAY_SUPPORT_TOPO_CHANGE
|
||||
static int user_gateway_topo_change_handler(const int devid, const char *payload, const int payload_len)
|
||||
{
|
||||
int index = 0;
|
||||
int subdev_id = -1;
|
||||
cJSON *topo_change = NULL, *status = NULL, *subList = NULL;
|
||||
cJSON *subdev, *pk = NULL, *dn = NULL;
|
||||
gateway_ctx_t *gateway_ctx = gateway_get_ctx();
|
||||
iotx_linkkit_dev_meta_info_t subdev_mate;
|
||||
|
||||
gateway_debug("Receive topo change, payload:%s", payload);
|
||||
|
||||
/* Parse Request */
|
||||
topo_change = cJSON_Parse(payload);
|
||||
if (topo_change == NULL)
|
||||
{
|
||||
gateway_err("topo change json format err");
|
||||
return -1;
|
||||
}
|
||||
|
||||
status = cJSON_GetObjectItem(topo_change, TOPO_CHANGE_STATUS);
|
||||
if (!cJSON_IsNumber(status))
|
||||
{
|
||||
gateway_err("topo change status err");
|
||||
cJSON_Delete(topo_change);
|
||||
return -1;
|
||||
}
|
||||
|
||||
subList = cJSON_GetObjectItem(topo_change, TOPO_CHANGE_SUBLIST);
|
||||
if (!cJSON_IsArray(subList))
|
||||
{
|
||||
gateway_err("topo change subList err");
|
||||
cJSON_Delete(topo_change);
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (index = 0; index < cJSON_GetArraySize(subList); index++)
|
||||
{
|
||||
subdev = cJSON_GetArrayItem(subList, index);
|
||||
if (subdev == NULL || !cJSON_IsObject(subdev))
|
||||
{
|
||||
gateway_err("subdev json err");
|
||||
cJSON_Delete(topo_change);
|
||||
return -1;
|
||||
}
|
||||
|
||||
pk = cJSON_GetObjectItem(subdev, TOPO_LIST_PK);
|
||||
dn = cJSON_GetObjectItem(subdev, TOPO_LIST_DN);
|
||||
if (cJSON_IsString(pk) && cJSON_IsString(dn))
|
||||
{
|
||||
memset(&subdev_mate, 0, sizeof(iotx_linkkit_dev_meta_info_t));
|
||||
HAL_Snprintf(subdev_mate.product_key, PRODUCT_KEY_MAXLEN, "%s", pk->valuestring);
|
||||
HAL_Snprintf(subdev_mate.device_name, DEVICE_NAME_MAXLEN, "%s", dn->valuestring);
|
||||
subdev_id = gateway_query_subdev_id(gateway_ctx->master_devid, &subdev_mate);
|
||||
if (subdev_id > 0)
|
||||
{
|
||||
switch (status->valueint)
|
||||
{
|
||||
case GW_TOPO_CHANGE_STATUS_ADD:
|
||||
{
|
||||
}
|
||||
break;
|
||||
case GW_TOPO_CHANGE_STATUS_DELETE:
|
||||
{
|
||||
gateway_info("topo change del dn:%s", subdev_mate.device_name);
|
||||
IOT_Linkkit_Close(subdev_id);
|
||||
}
|
||||
break;
|
||||
case GW_TOPO_CHANGE_STATUS_ENABLE:
|
||||
{
|
||||
}
|
||||
break;
|
||||
case GW_TOPO_CHANGE_STATUS_DISABLE:
|
||||
{
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cJSON_Delete(topo_change);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int user_subdev_misc_reply_handler(const int devid, const int event_id, const int code, const char *payload, const int payload_len)
|
||||
{
|
||||
gateway_info("Receive subdev misc reply, code:%d, payload:%s", code, payload);
|
||||
switch (event_id)
|
||||
{
|
||||
case ITM_EVENT_TOPO_DELETE_REPLY:
|
||||
{
|
||||
gateway_info("ITM_EVENT_TOPO_DELETE_REPLY, devid:%d code:%d, payload:%s", devid, code, payload);
|
||||
if (200 == code)
|
||||
{
|
||||
IOT_Linkkit_Close(devid); //remove subdev from dm(device manager)
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ITM_EVENT_SUBDEV_RESET_REPLY:
|
||||
{
|
||||
gateway_info("ITM_EVENT_SUBDEV_RESET_REPLY, devid:%d code:%d, payload:%s", devid, code, payload);
|
||||
}
|
||||
break;
|
||||
case ITM_EVENT_TOPO_ADD_REPLY:
|
||||
{
|
||||
gateway_info("ITM_EVENT_TOPO_ADD_REPLY, devid:%d code:%d, payload:%s", devid, code, payload);
|
||||
}
|
||||
break;
|
||||
case ITM_EVENT_COMBINE_LOGIN_REPLY:
|
||||
{
|
||||
gateway_info("ITM_EVENT_COMBINE_LOGIN_REPLY, devid:%d code:%d, payload:%s", devid, code, payload);
|
||||
}
|
||||
break;
|
||||
case ITM_EVENT_COMBINE_LOGOUT_REPLY:
|
||||
{
|
||||
gateway_info("ITM_EVENT_COMBINE_LOGOUT_REPLY, devid:%d code:%d, payload:%s", devid, code, payload);
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_fota_event_handler(int type, const char *version)
|
||||
{
|
||||
char *p_fota_buffer = NULL;
|
||||
gateway_ctx_t *gateway_ctx = gateway_get_ctx();
|
||||
|
||||
p_fota_buffer = HAL_Malloc(GATEWAY_OTA_BUFFER_LEN);
|
||||
if (!p_fota_buffer)
|
||||
{
|
||||
gateway_err("no mem");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (type == 0)
|
||||
{
|
||||
gateway_info("New Firmware Version: %s", version);
|
||||
memset(p_fota_buffer, 0, GATEWAY_OTA_BUFFER_LEN);
|
||||
IOT_Linkkit_Query(gateway_ctx->master_devid, ITM_MSG_QUERY_FOTA_DATA, (unsigned char *)p_fota_buffer, GATEWAY_OTA_BUFFER_LEN);
|
||||
}
|
||||
|
||||
if (p_fota_buffer)
|
||||
HAL_Free(p_fota_buffer);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_cota_event_handler(int type, const char *config_id, int config_size, const char *get_type,
|
||||
const char *sign, const char *sign_method, const char *url)
|
||||
{
|
||||
char *p_cota_buffer = NULL;
|
||||
gateway_ctx_t *gateway_ctx = gateway_get_ctx();
|
||||
|
||||
p_cota_buffer = HAL_Malloc(GATEWAY_OTA_BUFFER_LEN);
|
||||
if (!p_cota_buffer)
|
||||
{
|
||||
gateway_err("no mem");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (type == 0)
|
||||
{
|
||||
gateway_info("New Config ID: %s", config_id);
|
||||
gateway_info("New Config Size: %d", config_size);
|
||||
gateway_info("New Config Type: %s", get_type);
|
||||
gateway_info("New Config Sign: %s", sign);
|
||||
gateway_info("New Config Sign Method: %s", sign_method);
|
||||
gateway_info("New Config URL: %s", url);
|
||||
|
||||
IOT_Linkkit_Query(gateway_ctx->master_devid, ITM_MSG_QUERY_COTA_DATA, (unsigned char *)p_cota_buffer, GATEWAY_OTA_BUFFER_LEN);
|
||||
}
|
||||
|
||||
if (p_cota_buffer)
|
||||
HAL_Free(p_cota_buffer);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_offline_reset_handler(void)
|
||||
{
|
||||
gateway_info("user callback user_offline_reset_handler called.");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_dev_bind_event(const int state_code, const char *state_message)
|
||||
{
|
||||
gateway_info("state_code: -0x%04x, str_msg= %s", -state_code, state_message == NULL ? "NULL" : state_message);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef DM_UNIFIED_SERVICE_POST
|
||||
static int user_unified_service_post_reply_handler(const int devid, const int id, const int code, const char *payload, const int payload_len)
|
||||
{
|
||||
gateway_info("Receive unified service post reply, code:%d, payload:%s", code, payload);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
void *user_dispatch_yield(void *args)
|
||||
{
|
||||
gateway_ctx_t *gateway_ctx = gateway_get_ctx();
|
||||
|
||||
while (gateway_ctx->g_user_dispatch_thread_running)
|
||||
{
|
||||
IOT_Linkkit_Yield(GATEWAY_YIELD_TIMEOUT_MS);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static iotx_linkkit_dev_meta_info_t *gateway_main_init(gateway_ctx_t *gateway_ctx)
|
||||
{
|
||||
int ret = 0;
|
||||
int dynamic_register = 0; //0:You have to burn DS for each devices,1:Request DS from cloud by https
|
||||
int post_event_reply = 1;
|
||||
iotx_linkkit_dev_meta_info_t *p_master_meta = NULL;
|
||||
|
||||
memset(gateway_ctx, 0, sizeof(gateway_ctx_t));
|
||||
|
||||
p_master_meta = HAL_Malloc(sizeof(iotx_linkkit_dev_meta_info_t));
|
||||
if (p_master_meta == NULL)
|
||||
{
|
||||
gateway_err("no mem");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(p_master_meta, 0, sizeof(iotx_linkkit_dev_meta_info_t));
|
||||
HAL_GetProductKey(p_master_meta->product_key);
|
||||
HAL_GetDeviceName(p_master_meta->device_name);
|
||||
HAL_GetDeviceSecret(p_master_meta->device_secret);
|
||||
HAL_GetProductSecret(p_master_meta->product_secret);
|
||||
|
||||
if ((0 == strlen(p_master_meta->product_key)) || (0 == strlen(p_master_meta->device_name)) || (0 == dynamic_register && (0 == strlen(p_master_meta->device_secret))) || (0 == strlen(p_master_meta->product_secret)))
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
printf("Master meta info is invalid...\r\n");
|
||||
printf("pk[%s]\r\n", p_master_meta->product_key);
|
||||
printf("ps[%s]\r\n", p_master_meta->product_secret);
|
||||
printf("dn[%s]\r\n", p_master_meta->device_name);
|
||||
printf("ds[%s]\r\n", p_master_meta->device_secret);
|
||||
HAL_SleepMs(2000);
|
||||
}
|
||||
}
|
||||
|
||||
/* Register Callback */
|
||||
IOT_RegisterCallback(ITE_INITIALIZE_COMPLETED, user_initialized);
|
||||
IOT_RegisterCallback(ITE_CONNECT_SUCC, user_connected_event_handler);
|
||||
IOT_RegisterCallback(ITE_DISCONNECTED, user_disconnected_event_handler);
|
||||
IOT_RegisterCallback(ITE_RAWDATA_ARRIVED, user_down_raw_data_arrived_event_handler);
|
||||
IOT_RegisterCallback(ITE_SERVICE_REQUEST, user_service_request_event_handler);
|
||||
IOT_RegisterCallback(ITE_PROPERTY_SET, user_property_set_event_handler);
|
||||
#ifdef ALCS_ENABLED
|
||||
/*Only for local communication service(ALCS)*/
|
||||
IOT_RegisterCallback(ITE_PROPERTY_GET, user_property_get_event_handler);
|
||||
#endif
|
||||
IOT_RegisterCallback(ITE_REPORT_REPLY, user_report_reply_event_handler);
|
||||
IOT_RegisterCallback(ITE_TRIGGER_EVENT_REPLY, user_trigger_event_reply_event_handler);
|
||||
IOT_RegisterCallback(ITE_TIMESTAMP_REPLY, user_timestamp_reply_event_handler);
|
||||
|
||||
IOT_RegisterCallback(ITE_PERMIT_JOIN, user_permit_join_event_handler);
|
||||
IOT_RegisterCallback(ITE_CLOUD_ERROR, user_property_cloud_error_handler);
|
||||
IOT_RegisterCallback(ITE_EVENT_NOTIFY, user_event_notify_handler);
|
||||
|
||||
IOT_RegisterCallback(ITE_TOPOLIST_REPLY, user_topolist_reply_handler);
|
||||
IOT_RegisterCallback(ITE_SUBDEV_MISC_OPS, user_subdev_misc_reply_handler);
|
||||
#ifdef GATEWAY_SUPPORT_TOPO_CHANGE
|
||||
IOT_RegisterCallback(ITE_TOPO_CHANGE, user_gateway_topo_change_handler);
|
||||
#endif
|
||||
|
||||
IOT_RegisterCallback(ITE_FOTA, user_fota_event_handler);
|
||||
IOT_RegisterCallback(ITE_COTA, user_cota_event_handler);
|
||||
IOT_RegisterCallback(ITE_STATE_DEV_BIND, user_dev_bind_event);
|
||||
IOT_RegisterCallback(ITE_OFFLINE_RESET, user_offline_reset_handler);
|
||||
|
||||
#ifdef DM_UNIFIED_SERVICE_POST
|
||||
IOT_RegisterCallback(ITE_UNIFIED_SERVICE_POST, user_unified_service_post_reply_handler);
|
||||
#endif
|
||||
|
||||
IOT_Ioctl(IOTX_IOCTL_SET_DYNAMIC_REGISTER, (void *)&dynamic_register);
|
||||
|
||||
/* Choose Whether You Need Post Property/Event Reply */
|
||||
IOT_Ioctl(IOTX_IOCTL_RECV_EVENT_REPLY, (void *)&post_event_reply);
|
||||
|
||||
#ifdef GATEWAY_UT_TESTING
|
||||
gateway_ut_init();
|
||||
#endif
|
||||
|
||||
return p_master_meta;
|
||||
}
|
||||
|
||||
int gateway_main(void *paras)
|
||||
{
|
||||
int res = 0;
|
||||
uint64_t time_prev_sec = 0, time_now_sec = 0, time_begin_sec = 0;
|
||||
gateway_ctx_t *gateway_ctx = gateway_get_ctx();
|
||||
iotx_linkkit_dev_meta_info_t *p_master_meta = NULL;
|
||||
hal_os_thread_param_t hal_os_thread_param;
|
||||
|
||||
p_master_meta = gateway_main_init(gateway_ctx);
|
||||
if (NULL == p_master_meta)
|
||||
{
|
||||
printf("OOPS:gateway_main_init failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Create Master Device Resources */
|
||||
do
|
||||
{
|
||||
gateway_ctx->master_devid = IOT_Linkkit_Open(IOTX_LINKKIT_DEV_TYPE_MASTER, p_master_meta);
|
||||
if (gateway_ctx->master_devid < 0)
|
||||
{
|
||||
printf("IOT_Linkkit_Open Failed, retry after 5s...\r\n");
|
||||
HAL_SleepMs(5000);
|
||||
}
|
||||
} while (gateway_ctx->master_devid < 0);
|
||||
/* Start Connect Aliyun Server */
|
||||
do
|
||||
{
|
||||
res = IOT_Linkkit_Connect(gateway_ctx->master_devid);
|
||||
if (res < 0)
|
||||
{
|
||||
printf("IOT_Linkkit_Connect Failed, retry after 5s...\r\n");
|
||||
HAL_SleepMs(5000);
|
||||
}
|
||||
} while (res < 0);
|
||||
|
||||
gateway_ctx->g_user_dispatch_thread_running = 1;
|
||||
|
||||
//Attation:please don't remove this thread,because sub dev operations is sync
|
||||
memset(&hal_os_thread_param, 0, sizeof(hal_os_thread_param_t));
|
||||
hal_os_thread_param.stack_size = GATEWAY_YIELD_THREAD_STACKSIZE;
|
||||
hal_os_thread_param.name = GATEWAY_YIELD_THREAD_NAME;
|
||||
|
||||
res = HAL_ThreadCreate(&gateway_ctx->g_user_dispatch_thread, user_dispatch_yield, NULL, &hal_os_thread_param, NULL);
|
||||
if (res < 0)
|
||||
{
|
||||
gateway_info("HAL_ThreadCreate Failed\n");
|
||||
IOT_Linkkit_Close(gateway_ctx->master_devid);
|
||||
return -1;
|
||||
}
|
||||
|
||||
time_begin_sec = user_update_sec();
|
||||
|
||||
while (1)
|
||||
{
|
||||
time_now_sec = user_update_sec();
|
||||
if (time_prev_sec == time_now_sec)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
#ifdef GATEWAY_UT_TESTING
|
||||
HAL_SleepMs(GATEWAY_YIELD_TIMEOUT_MS / 2);
|
||||
|
||||
if (user_master_dev_available())
|
||||
{
|
||||
gateway_ut_msg_process(gateway_ctx->master_devid, GATEWAY_YIELD_TIMEOUT_MS / 2);
|
||||
gateway_ut_misc_process(time_now_sec);
|
||||
}
|
||||
#else
|
||||
HAL_SleepMs(GATEWAY_YIELD_TIMEOUT_MS);
|
||||
#endif
|
||||
time_prev_sec = time_now_sec;
|
||||
}
|
||||
|
||||
//Should never come here
|
||||
gateway_ctx->g_user_dispatch_thread_running = 0;
|
||||
|
||||
if (p_master_meta)
|
||||
{
|
||||
HAL_Free(p_master_meta);
|
||||
}
|
||||
|
||||
IOT_DumpMemoryStats(IOT_LOG_DEBUG);
|
||||
|
||||
return 0;
|
||||
}
|
||||
33
Products/example/linkkit_gateway/gateway_main.h
Normal file
33
Products/example/linkkit_gateway/gateway_main.h
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#ifndef __GATEWAY_MAIN_H__
|
||||
#define __GATEWAY_MAIN_H__
|
||||
|
||||
#include <iotx_log.h>
|
||||
|
||||
#define gateway_debug(...) log_debug("gateway", __VA_ARGS__)
|
||||
#define gateway_info(...) log_info("gateway", __VA_ARGS__)
|
||||
#define gateway_warn(...) log_warning("gateway", __VA_ARGS__)
|
||||
#define gateway_err(...) log_err("gateway", __VA_ARGS__)
|
||||
#define gateway_crit(...) log_crit("gateway", __VA_ARGS__)
|
||||
|
||||
#define GATEWAY_YIELD_THREAD_NAME "linkkit_yield"
|
||||
#define GATEWAY_YIELD_THREAD_STACKSIZE (8 * 1024)
|
||||
#define GATEWAY_YIELD_TIMEOUT_MS (200)
|
||||
#define GATEWAY_OTA_BUFFER_LEN (512 + 1)
|
||||
|
||||
//#define GATEWAY_SUPPORT_TOPO_CHANGE
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int master_devid;
|
||||
int cloud_connected;
|
||||
int master_initialized;
|
||||
int permit_join;
|
||||
char permit_join_pk[PRODUCT_KEY_MAXLEN];
|
||||
void *g_user_dispatch_thread;
|
||||
int g_user_dispatch_thread_running;
|
||||
} gateway_ctx_t;
|
||||
|
||||
extern int gateway_main(void *paras);
|
||||
extern gateway_ctx_t *gateway_get_ctx(void);
|
||||
|
||||
#endif
|
||||
694
Products/example/linkkit_gateway/gateway_ut.c
Normal file
694
Products/example/linkkit_gateway/gateway_ut.c
Normal file
|
|
@ -0,0 +1,694 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2019 Alibaba Group Holding Limited
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <aos/kernel.h>
|
||||
#include "cJSON.h"
|
||||
#include "iot_import.h"
|
||||
#include "iot_export_linkkit.h"
|
||||
#include "aos/kv.h"
|
||||
#include "iotx_log.h"
|
||||
|
||||
#include "gateway_main.h"
|
||||
#include "gateway_cmds.h"
|
||||
#include "gateway_api.h"
|
||||
#include "gateway_ut.h"
|
||||
|
||||
#ifdef GATEWAY_UT_TESTING
|
||||
static char is_forbidden_auto_add_subdev = 0;
|
||||
static int subdev_index = 0;
|
||||
|
||||
static aos_queue_t gateway_queue_t;
|
||||
static char gateway_queue_buf[MAX_QUEUE_SIZE];
|
||||
static gw_topo_get_reason_e topo_get_reason = GW_TOPO_GET_REASON_MAX;
|
||||
|
||||
static int gateway_find_subdev_by_pk_dn(iotx_linkkit_dev_meta_info_t *subdev_meta);
|
||||
|
||||
//For cmds call send msg to linkkit_main
|
||||
int gateway_ut_send_msg(gateway_msg_t *msg, int len)
|
||||
{
|
||||
if (msg && len == QUEUE_MSG_SIZE)
|
||||
{
|
||||
gateway_info("send msg type(%d)", msg->msg_type);
|
||||
return aos_queue_send(&gateway_queue_t, msg, len);
|
||||
}
|
||||
|
||||
gateway_err("param err");
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
//This example sub dev mate mainly for CI
|
||||
iotx_linkkit_dev_meta_info_t subdevArr[GATEWAY_SUBDEV_MAX_NUM] = {
|
||||
#ifdef REGION_SINGAPORE
|
||||
{"a2DUsQ3C7T8",
|
||||
"Q5OX8RLGBo9PCq3M",
|
||||
"fyci_subdev_sing",
|
||||
"iyEqGnk5lb8bz6oiLKgacTGWwcsLmB9M"},
|
||||
{"a2IWy8PbZxP",
|
||||
"p9t22CYSxQffujBd",
|
||||
"fyci_subdev_raw_sing",
|
||||
"c8Jc8SuymnPvUfVyMMWTEAKpufApDDxQ"},
|
||||
#else /* Mainland(Shanghai) for default */
|
||||
{"a185scQ2c22",
|
||||
"rwPp08IDoKpNi0n2",
|
||||
"fyci_subdev",
|
||||
"kVbpNJDPUEEJGyEnYmUUaOZocqSub7Wc"},
|
||||
{"a1y80y4lvBy",
|
||||
"XnNOHUi0FPWb9qFF",
|
||||
"fyci_subdev_raw",
|
||||
"YlqeyeIPjsWBbTAadyEnEuXFrJrNqHO0"},
|
||||
#endif
|
||||
};
|
||||
|
||||
static int gateway_connect_cloud(char *payload, int payload_len)
|
||||
{
|
||||
int index = 0;
|
||||
int subdev_id = 0;
|
||||
int subdev_total = 0;
|
||||
int subdev_num = 0;
|
||||
gateway_msg_t msg;
|
||||
cJSON *topo_list = NULL, *subdev = NULL;
|
||||
cJSON *pk = NULL, *dn = NULL;
|
||||
gateway_ctx_t *p_gateway_ctx = gateway_get_ctx();
|
||||
|
||||
iotx_linkkit_dev_meta_info_t *p_subdev_mate = NULL;
|
||||
iotx_linkkit_dev_meta_info_t *p_subdev_mate_index = NULL;
|
||||
|
||||
if (payload == NULL || payload_len < 1)
|
||||
{
|
||||
gateway_err("param err");
|
||||
return -1;
|
||||
}
|
||||
/* Parse Request */
|
||||
topo_list = cJSON_Parse(payload);
|
||||
if (topo_list == NULL || !cJSON_IsArray(topo_list))
|
||||
{
|
||||
gateway_err("topo list json format err");
|
||||
return -1;
|
||||
}
|
||||
|
||||
subdev_total = cJSON_GetArraySize(topo_list);
|
||||
if (subdev_total < 1)
|
||||
{
|
||||
gateway_err("topo is empty");
|
||||
return -1;
|
||||
}
|
||||
|
||||
p_subdev_mate = HAL_Malloc(sizeof(iotx_linkkit_dev_meta_info_t) * subdev_total);
|
||||
if (p_subdev_mate == NULL)
|
||||
{
|
||||
gateway_err("no mem");
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(p_subdev_mate, 0, sizeof(iotx_linkkit_dev_meta_info_t) * subdev_total);
|
||||
for (index = 0; index < subdev_total; index++)
|
||||
{
|
||||
subdev = cJSON_GetArrayItem(topo_list, index);
|
||||
if (subdev == NULL || !cJSON_IsObject(subdev))
|
||||
{
|
||||
gateway_err("subdev json err");
|
||||
continue;
|
||||
}
|
||||
|
||||
pk = cJSON_GetObjectItem(subdev, TOPO_LIST_PK);
|
||||
dn = cJSON_GetObjectItem(subdev, TOPO_LIST_DN);
|
||||
if (cJSON_IsString(pk) && cJSON_IsString(dn))
|
||||
{
|
||||
p_subdev_mate_index = p_subdev_mate + subdev_num;
|
||||
subdev_num++;
|
||||
HAL_Snprintf(p_subdev_mate_index->product_key, PRODUCT_KEY_MAXLEN, "%s", pk->valuestring);
|
||||
HAL_Snprintf(p_subdev_mate_index->device_name, DEVICE_NAME_MAXLEN, "%s", dn->valuestring);
|
||||
}
|
||||
}
|
||||
|
||||
cJSON_Delete(topo_list);
|
||||
|
||||
gateway_add_multi_subdev(p_gateway_ctx->master_devid, p_subdev_mate, subdev_num);
|
||||
if (p_subdev_mate)
|
||||
HAL_Free(p_subdev_mate);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
gw_device_type_e gateway_get_device_type(void)
|
||||
{
|
||||
int ret = 0;
|
||||
char dev_type[GATEWAY_DEVICE_TYPE_LEN] = {0};
|
||||
int len = GATEWAY_DEVICE_TYPE_LEN - 1;
|
||||
|
||||
ret = HAL_Kv_Get(GATEWAY_DEVICE_TYPE_KEY, dev_type, &len);
|
||||
if (ret == 0)
|
||||
{
|
||||
if (!strcmp(dev_type, GATEWAY_DEVICE_TYPE_MASTER))
|
||||
{
|
||||
return GW_DEVICE_MASTER;
|
||||
}
|
||||
else if (!strcmp(dev_type, GATEWAY_DEVICE_TYPE_SLAVE))
|
||||
{
|
||||
return GW_DEVICE_SLAVE;
|
||||
}
|
||||
}
|
||||
|
||||
return GW_DEVICE_INVALID;
|
||||
}
|
||||
|
||||
static int gateway_get_forbidden_auto_add_subdev_flag(void)
|
||||
{
|
||||
int ret = 0;
|
||||
char forbidden_flag[GATEWAY_FORBIDDEN_AUTO_ADD_SUBDEV_FLAG_LEN] = {0};
|
||||
int len = GATEWAY_FORBIDDEN_AUTO_ADD_SUBDEV_FLAG_LEN - 1;
|
||||
|
||||
ret = HAL_Kv_Get(GATEWAY_FORBIDDEN_AUTO_ADD_SUBDEV_FLAG_KEY, forbidden_flag, &len);
|
||||
if (ret == 0)
|
||||
{
|
||||
if ((!strcmp(forbidden_flag, "Y")) || (!strcmp(forbidden_flag, "y")))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//Just for reference
|
||||
void user_post_property(void)
|
||||
{
|
||||
int res = 0;
|
||||
gateway_ctx_t *gateway_ctx = gateway_get_ctx();
|
||||
char *property_payload = "{\"LightSwitch\":1}";
|
||||
|
||||
res = IOT_Linkkit_Report(gateway_ctx->master_devid, ITM_MSG_POST_PROPERTY,
|
||||
(unsigned char *)property_payload, strlen(property_payload));
|
||||
|
||||
gateway_info("Post Property Message ID: %d", res);
|
||||
}
|
||||
|
||||
//Just for reference
|
||||
void user_post_sub_property(int subdev_id)
|
||||
{
|
||||
int res = 0;
|
||||
gateway_ctx_t *gateway_ctx = gateway_get_ctx();
|
||||
char *property_payload = "{\"LightSwitch\":1}";
|
||||
|
||||
res = IOT_Linkkit_Report(subdev_id, ITM_MSG_POST_PROPERTY,
|
||||
(unsigned char *)property_payload, strlen(property_payload));
|
||||
|
||||
gateway_info("Post Property Message ID: %d", res);
|
||||
}
|
||||
|
||||
//Just for reference
|
||||
void user_post_event(void)
|
||||
{
|
||||
int res = 0;
|
||||
gateway_ctx_t *gateway_ctx = gateway_get_ctx();
|
||||
char *event_id = "Error";
|
||||
char *event_payload = "{\"ErrorCode\":0}";
|
||||
|
||||
res = IOT_Linkkit_TriggerEvent(gateway_ctx->master_devid, event_id, strlen(event_id),
|
||||
event_payload, strlen(event_payload));
|
||||
gateway_info("Post Event Message ID: %d", res);
|
||||
}
|
||||
|
||||
//Just for reference
|
||||
void user_deviceinfo_update(void)
|
||||
{
|
||||
int res = 0;
|
||||
gateway_ctx_t *gateway_ctx = gateway_get_ctx();
|
||||
char *device_info_update = "[{\"attrKey\":\"gateway\",\"attrValue\":\"I am a gateway\"},{\"attrKey\":\"subdev\",\"attrValue\":\"I am a subdev\"}]";
|
||||
|
||||
res = IOT_Linkkit_Report(gateway_ctx->master_devid, ITM_MSG_DEVICEINFO_UPDATE,
|
||||
(unsigned char *)device_info_update, strlen(device_info_update));
|
||||
gateway_info("Device Info Update Message ID: %d", res);
|
||||
}
|
||||
|
||||
//Just for reference
|
||||
void user_deviceinfo_delete(void)
|
||||
{
|
||||
int res = 0;
|
||||
gateway_ctx_t *gateway_ctx = gateway_get_ctx();
|
||||
char *device_info_delete = "[{\"attrKey\":\"subdev\"}]";
|
||||
|
||||
res = IOT_Linkkit_Report(gateway_ctx->master_devid, ITM_MSG_DEVICEINFO_DELETE,
|
||||
(unsigned char *)device_info_delete, strlen(device_info_delete));
|
||||
gateway_info("Device Info Delete Message ID: %d", res);
|
||||
}
|
||||
|
||||
//Just for reference
|
||||
void user_post_raw_data(void)
|
||||
{
|
||||
int res = 0;
|
||||
gateway_ctx_t *gateway_ctx = gateway_get_ctx();
|
||||
//raw_data is ASCII of [This is raw data.]
|
||||
unsigned char raw_data[] = {0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x72, 0x61, 0x77, 0x20, 0x64, 0x61, 0x74, 0x61, 0x2E};
|
||||
|
||||
res = IOT_Linkkit_Report(gateway_ctx->master_devid, ITM_MSG_POST_RAW_DATA,
|
||||
raw_data, sizeof(raw_data));
|
||||
|
||||
gateway_info("Post Raw Data Message ID: %d", res);
|
||||
}
|
||||
|
||||
int gateway_ut_handle_topolist_reply(const char *payload, const int payload_len)
|
||||
{
|
||||
gateway_msg_t msg;
|
||||
|
||||
if (topo_get_reason != GW_TOPO_GET_REASON_CONNECT_CLOUD)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
memset(&msg, 0, sizeof(gateway_msg_t));
|
||||
msg.payload = HAL_Malloc(payload_len + 1);
|
||||
|
||||
if (msg.payload)
|
||||
{
|
||||
memset(msg.payload, '\0', payload_len + 1);
|
||||
memcpy(msg.payload, payload, payload_len);
|
||||
msg.msg_type = GATEWAY_MSG_TYPE_CLOUD_CONNECT;
|
||||
msg.devid = 1;
|
||||
msg.payload_len = payload_len;
|
||||
gateway_ut_send_msg(&msg, sizeof(gateway_msg_t));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int gateway_ut_handle_permit_join(void)
|
||||
{
|
||||
gateway_msg_t msg;
|
||||
|
||||
msg.msg_type = GATEWAY_MSG_TYPE_PERMIT_JOIN;
|
||||
msg.devid = 1;
|
||||
gateway_ut_send_msg(&msg, sizeof(gateway_msg_t));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//get a sub dev mate info from KV,just for testing
|
||||
int get_one_device_mate(int devid, iotx_linkkit_dev_meta_info_t *p_subdev)
|
||||
{
|
||||
int len = 0;
|
||||
char key_buf[MAX_KEY_LEN];
|
||||
|
||||
if (!p_subdev)
|
||||
return -1;
|
||||
|
||||
len = PRODUCT_KEY_MAXLEN;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(p_subdev->product_key, 0, PRODUCT_KEY_MAXLEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PK, devid);
|
||||
HAL_Kv_Get(key_buf, p_subdev->product_key, &len);
|
||||
|
||||
len = PRODUCT_SECRET_MAXLEN;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(p_subdev->product_secret, 0, PRODUCT_SECRET_MAXLEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PS, devid);
|
||||
HAL_Kv_Get(key_buf, p_subdev->product_secret, &len);
|
||||
|
||||
len = DEVICE_NAME_MAXLEN;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(p_subdev->device_name, 0, DEVICE_NAME_MAXLEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DN, devid);
|
||||
HAL_Kv_Get(key_buf, p_subdev->device_name, &len);
|
||||
|
||||
len = DEVICE_SECRET_MAXLEN;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(p_subdev->device_secret, 0, DEVICE_SECRET_MAXLEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DS, devid);
|
||||
HAL_Kv_Get(key_buf, p_subdev->device_secret, &len);
|
||||
|
||||
gateway_info("DevNum(%d) DN:%s", devid, p_subdev->device_name);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//Find sub device mate info from KV,just for testing
|
||||
static int gateway_find_subdev_by_pk_dn(iotx_linkkit_dev_meta_info_t *subdev_meta)
|
||||
{
|
||||
int index = 0;
|
||||
|
||||
iotx_linkkit_dev_meta_info_t subdev;
|
||||
|
||||
if (!subdev_meta)
|
||||
return -1;
|
||||
|
||||
for (index = 1; index < MAX_DEVICES_META_NUM; index++)
|
||||
{
|
||||
get_one_device_mate(index, &subdev);
|
||||
if (!strcmp(subdev_meta->product_key, subdev.product_key) && !strcmp(subdev_meta->device_name, subdev.device_name))
|
||||
{
|
||||
return index;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
//get multi sub devices mate info from KV,just for testing
|
||||
static int get_all_subdev_mate(iotx_linkkit_dev_meta_info_t *subdev_meta)
|
||||
{
|
||||
int index = 0;
|
||||
|
||||
iotx_linkkit_dev_meta_info_t *p_subdev = NULL;
|
||||
|
||||
if (!subdev_meta)
|
||||
return -1;
|
||||
|
||||
for (index = 1; index < MAX_DEVICES_META_NUM; index++)
|
||||
{
|
||||
p_subdev = subdev_meta + (index - 1);
|
||||
|
||||
get_one_device_mate(index, p_subdev);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//get multi sub devices mate info from KV,just for testing
|
||||
static int get_range_subdev_mate(iotx_linkkit_dev_meta_info_t *subdev_meta, int devid_start, int devid_end)
|
||||
{
|
||||
int index = 0;
|
||||
|
||||
iotx_linkkit_dev_meta_info_t *p_subdev = NULL;
|
||||
|
||||
if (!subdev_meta)
|
||||
return -1;
|
||||
|
||||
if (devid_start > 0 && devid_start < MAX_DEVICES_META_NUM &&
|
||||
devid_end > 0 && devid_end < MAX_DEVICES_META_NUM &&
|
||||
devid_start < devid_end)
|
||||
{
|
||||
for (index = devid_start; index <= devid_end; index++)
|
||||
{
|
||||
p_subdev = subdev_meta + (index - 1);
|
||||
|
||||
get_one_device_mate(index, p_subdev);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
gateway_warn("devid err");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//This is an example for get topolist info
|
||||
//you can get topo list info in func:user_topolist_reply_handler
|
||||
int gateway_ut_update_subdev(gw_topo_get_reason_e reason)
|
||||
{
|
||||
gateway_ctx_t *gateway_ctx = gateway_get_ctx();
|
||||
gateway_info("do update subdev");
|
||||
|
||||
IOT_Linkkit_Query(gateway_ctx->master_devid, ITM_MSG_QUERY_TOPOLIST, NULL, 0);
|
||||
topo_get_reason = reason;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int gateway_ut_msg_process(int master_devid, int timeout)
|
||||
{
|
||||
int ret = 0;
|
||||
unsigned int recv_size = 0;
|
||||
int subdev_id = -1;
|
||||
gateway_msg_t msg;
|
||||
gateway_ctx_t *gateway_ctx = gateway_get_ctx();
|
||||
|
||||
ret = aos_queue_recv(&gateway_queue_t, timeout, &msg, &recv_size);
|
||||
if (ret == 0 && recv_size == QUEUE_MSG_SIZE)
|
||||
{
|
||||
gateway_info("msg.type:%d, devid:%d", msg.msg_type, msg.devid);
|
||||
if (msg.devid < 1 || msg.devid > MAX_DEVICES_META_NUM - 1)
|
||||
{
|
||||
gateway_warn("sub dev id(%d) err", msg.devid);
|
||||
return -1;
|
||||
}
|
||||
|
||||
switch (msg.msg_type)
|
||||
{
|
||||
case GATEWAY_MSG_TYPE_ADD:
|
||||
{
|
||||
iotx_linkkit_dev_meta_info_t subdev_mate;
|
||||
memset(&subdev_mate, 0, sizeof(subdev_mate));
|
||||
get_one_device_mate(msg.devid, &subdev_mate);
|
||||
|
||||
if (gateway_ctx->permit_join == 1 && strlen(gateway_ctx->permit_join_pk) > 0)
|
||||
{
|
||||
if (strcmp(subdev_mate.product_key, gateway_ctx->permit_join_pk))
|
||||
{
|
||||
gateway_warn("permit join pk:%s is not found", gateway_ctx->permit_join_pk);
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ret = gateway_add_subdev(&subdev_mate);
|
||||
}
|
||||
break;
|
||||
case GATEWAY_MSG_TYPE_ADDALL:
|
||||
{
|
||||
iotx_linkkit_dev_meta_info_t *p_meta_info = NULL;
|
||||
|
||||
p_meta_info = HAL_Malloc(sizeof(iotx_linkkit_dev_meta_info_t) * (MAX_DEVICES_META_NUM - 1));
|
||||
if (!p_meta_info)
|
||||
{
|
||||
gateway_err("no mem");
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(p_meta_info, '\0', sizeof(iotx_linkkit_dev_meta_info_t) * (MAX_DEVICES_META_NUM - 1));
|
||||
get_all_subdev_mate(p_meta_info);
|
||||
ret = gateway_add_multi_subdev(master_devid, p_meta_info, MAX_DEVICES_META_NUM - 1);
|
||||
|
||||
HAL_Free(p_meta_info);
|
||||
}
|
||||
break;
|
||||
case GATEWAY_MSG_TYPE_ADD_RANGE:
|
||||
{
|
||||
iotx_linkkit_dev_meta_info_t *p_meta_info = NULL;
|
||||
int subdev_num = msg.devid_end - msg.devid + 1;
|
||||
|
||||
if (msg.devid < 1 || msg.devid >= MAX_DEVICES_META_NUM ||
|
||||
msg.devid_end < 1 || msg.devid_end >= MAX_DEVICES_META_NUM ||
|
||||
msg.devid >= msg.devid_end)
|
||||
{
|
||||
gateway_warn("sub dev id err");
|
||||
return -1;
|
||||
}
|
||||
|
||||
p_meta_info = HAL_Malloc(sizeof(iotx_linkkit_dev_meta_info_t) * subdev_num);
|
||||
if (!p_meta_info)
|
||||
{
|
||||
gateway_err("no mem");
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(p_meta_info, '\0', sizeof(iotx_linkkit_dev_meta_info_t) * subdev_num);
|
||||
get_range_subdev_mate(p_meta_info, msg.devid, msg.devid_end);
|
||||
ret = gateway_add_multi_subdev(master_devid, p_meta_info, subdev_num);
|
||||
|
||||
HAL_Free(p_meta_info);
|
||||
}
|
||||
break;
|
||||
case GATEWAY_MSG_TYPE_DELALL:
|
||||
{
|
||||
iotx_linkkit_dev_meta_info_t subdev_mate;
|
||||
for (subdev_id = 1; subdev_id < MAX_DEVICES_META_NUM; subdev_id++)
|
||||
{
|
||||
|
||||
memset(&subdev_mate, 0, sizeof(subdev_mate));
|
||||
get_one_device_mate(subdev_id, &subdev_mate);
|
||||
ret = gateway_del_subdev(&subdev_mate);
|
||||
gateway_info("del subdev id(%d) ret = %d", subdev_id, ret);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case GATEWAY_MSG_TYPE_DEL:
|
||||
{
|
||||
iotx_linkkit_dev_meta_info_t subdev_mate;
|
||||
|
||||
memset(&subdev_mate, 0, sizeof(subdev_mate));
|
||||
get_one_device_mate(msg.devid, &subdev_mate);
|
||||
ret = gateway_del_subdev(&subdev_mate);
|
||||
}
|
||||
break;
|
||||
|
||||
case GATEWAY_MSG_TYPE_DEL_RANGE:
|
||||
{
|
||||
iotx_linkkit_dev_meta_info_t subdev_mate;
|
||||
|
||||
if (msg.devid < 1 || msg.devid >= MAX_DEVICES_META_NUM ||
|
||||
msg.devid_end < 1 || msg.devid_end >= MAX_DEVICES_META_NUM ||
|
||||
msg.devid >= msg.devid_end)
|
||||
{
|
||||
gateway_warn("sub dev id err");
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (subdev_id = msg.devid; subdev_id <= msg.devid_end; subdev_id++)
|
||||
{
|
||||
|
||||
memset(&subdev_mate, 0, sizeof(subdev_mate));
|
||||
get_one_device_mate(subdev_id, &subdev_mate);
|
||||
ret = gateway_del_subdev(&subdev_mate);
|
||||
gateway_info("del subdev id(%d) ret = %d", subdev_id, ret);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case GATEWAY_MSG_TYPE_UPDATE:
|
||||
{
|
||||
ret = gateway_ut_update_subdev(GW_TOPO_GET_REASON_CLI_CMD);
|
||||
}
|
||||
break;
|
||||
case GATEWAY_MSG_TYPE_RESET:
|
||||
{
|
||||
iotx_linkkit_dev_meta_info_t subdev_mate;
|
||||
|
||||
memset(&subdev_mate, 0, sizeof(subdev_mate));
|
||||
get_one_device_mate(msg.devid, &subdev_mate);
|
||||
ret = gateway_reset_subdev(&subdev_mate);
|
||||
}
|
||||
break;
|
||||
case GATEWAY_MSG_TYPE_QUERY_SUBDEV_ID:
|
||||
{
|
||||
iotx_linkkit_dev_meta_info_t subdev_mate;
|
||||
|
||||
memset(&subdev_mate, 0, sizeof(subdev_mate));
|
||||
get_one_device_mate(msg.devid, &subdev_mate);
|
||||
ret = gateway_query_subdev_id(gateway_ctx->master_devid, &subdev_mate);
|
||||
}
|
||||
break;
|
||||
case GATEWAY_MSG_TYPE_CLOUD_CONNECT:
|
||||
{
|
||||
if (msg.payload)
|
||||
{
|
||||
gateway_connect_cloud(msg.payload, msg.payload_len);
|
||||
HAL_Free(msg.payload);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case GATEWAY_MSG_TYPE_PERMIT_JOIN:
|
||||
{
|
||||
if (gateway_ctx->permit_join == 1)
|
||||
{
|
||||
int matched_subdev_num = 0;
|
||||
iotx_linkkit_dev_meta_info_t subdev_mate = {0};
|
||||
iotx_linkkit_dev_meta_info_t *p_subdev_mate = NULL;
|
||||
|
||||
p_subdev_mate = HAL_Malloc(sizeof(iotx_linkkit_dev_meta_info_t) * MAX_DEVICES_META_NUM);
|
||||
if (p_subdev_mate == NULL)
|
||||
{
|
||||
gateway_err("no mem");
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(p_subdev_mate, 0, sizeof(iotx_linkkit_dev_meta_info_t) * MAX_DEVICES_META_NUM);
|
||||
for (subdev_id = 1; subdev_id < MAX_DEVICES_META_NUM; subdev_id++)
|
||||
{
|
||||
memset(&subdev_mate, 0, sizeof(subdev_mate));
|
||||
if (0 == get_one_device_mate(subdev_id, &subdev_mate) && 0 == strcmp(gateway_ctx->permit_join_pk, subdev_mate.product_key))
|
||||
{
|
||||
memcpy(p_subdev_mate + matched_subdev_num, &subdev_mate, sizeof(iotx_linkkit_dev_meta_info_t));
|
||||
matched_subdev_num++;
|
||||
}
|
||||
else
|
||||
{
|
||||
gateway_warn("permit join pk:%s found pk:%s dn:%s", gateway_ctx->permit_join_pk, subdev_mate.product_key, subdev_mate.device_name);
|
||||
}
|
||||
}
|
||||
|
||||
gateway_add_multi_subdev(gateway_ctx->master_devid, p_subdev_mate, matched_subdev_num);
|
||||
if (p_subdev_mate)
|
||||
{
|
||||
HAL_Free(p_subdev_mate);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
gateway_warn("unKnow msg type(%d)", msg.msg_type);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void gateway_ut_misc_process(uint64_t time_now_sec)
|
||||
{
|
||||
/* Add subdev for CI*/
|
||||
if (is_forbidden_auto_add_subdev == 0 && (subdev_index < GATEWAY_SUBDEV_MAX_NUM))
|
||||
{
|
||||
/* Add next subdev */
|
||||
if (gateway_add_subdev(&subdevArr[subdev_index]) == SUCCESS_RETURN)
|
||||
{
|
||||
gateway_info("subdev DN:%s add succeed", subdevArr[subdev_index].device_name);
|
||||
}
|
||||
else
|
||||
{
|
||||
gateway_info("subdev DN:%s add failed", subdevArr[subdev_index].device_name);
|
||||
}
|
||||
|
||||
subdev_index++;
|
||||
}
|
||||
|
||||
/* Post Proprety Example */
|
||||
if (time_now_sec % 11 == 0)
|
||||
{
|
||||
user_post_property();
|
||||
user_post_sub_property(subdev_index);
|
||||
}
|
||||
|
||||
/* Post Event Example */
|
||||
if (time_now_sec % 17 == 0)
|
||||
{
|
||||
user_post_event();
|
||||
}
|
||||
|
||||
/* Device Info Update Example */
|
||||
if (time_now_sec % 23 == 0)
|
||||
{
|
||||
user_deviceinfo_update();
|
||||
}
|
||||
|
||||
/* Device Info Delete Example */
|
||||
if (time_now_sec % 29 == 0)
|
||||
{
|
||||
user_deviceinfo_delete();
|
||||
}
|
||||
|
||||
/* Post Raw Example */
|
||||
if (time_now_sec % 37 == 0)
|
||||
{
|
||||
user_post_raw_data();
|
||||
}
|
||||
}
|
||||
|
||||
int gateway_ut_init(void)
|
||||
{
|
||||
int ret = SUCCESS_RETURN;
|
||||
|
||||
ret = aos_queue_new(&gateway_queue_t, gateway_queue_buf, sizeof(gateway_queue_buf), QUEUE_MSG_SIZE);
|
||||
if (FAIL_RETURN == ret)
|
||||
{
|
||||
gateway_info("aos_queue_new failed");
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (gateway_get_forbidden_auto_add_subdev_flag())
|
||||
{
|
||||
gateway_info("gw forbiden auto add subdev");
|
||||
is_forbidden_auto_add_subdev = 1;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif
|
||||
88
Products/example/linkkit_gateway/gateway_ut.h
Normal file
88
Products/example/linkkit_gateway/gateway_ut.h
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
#ifndef __GATEWAY_UT_H__
|
||||
#define __GATEWAY_UT_H__
|
||||
|
||||
// for demo only
|
||||
#define PRODUCT_KEY "PK_XXXXX"
|
||||
#define PRODUCT_SECRET "PS_XXXXX"
|
||||
#define DEVICE_NAME "DN_XXXXX"
|
||||
#define DEVICE_SECRET "DS_XXXXX"
|
||||
|
||||
#define TOPO_LIST_PK "productKey"
|
||||
#define TOPO_LIST_DN "deviceName"
|
||||
|
||||
#define TOPO_CHANGE_STATUS "status"
|
||||
#define TOPO_CHANGE_SUBLIST "subList"
|
||||
|
||||
#define GATEWAY_SUBDEV_MAX_NUM (2)
|
||||
|
||||
#define QUEUE_MSG_SIZE sizeof(gateway_msg_t)
|
||||
#define MAX_QUEUE_SIZE 10 * QUEUE_MSG_SIZE
|
||||
|
||||
//You should undefine this Macro in your products
|
||||
//#define GATEWAY_UT_TESTING
|
||||
|
||||
//KV config gw type,VALUE:master mean it is gateway,slave is subdev
|
||||
#define GATEWAY_DEVICE_TYPE_KEY "gwtype"
|
||||
#define GATEWAY_DEVICE_TYPE_MASTER "master"
|
||||
#define GATEWAY_DEVICE_TYPE_SLAVE "slave"
|
||||
#define GATEWAY_DEVICE_TYPE_LEN (7)
|
||||
|
||||
#define GATEWAY_FORBIDDEN_AUTO_ADD_SUBDEV_FLAG_KEY "gwf"
|
||||
#define GATEWAY_FORBIDDEN_AUTO_ADD_SUBDEV_FLAG_LEN 2
|
||||
|
||||
typedef enum _gw_topo_change_status_e
|
||||
{
|
||||
GW_TOPO_CHANGE_STATUS_ADD = 0,
|
||||
GW_TOPO_CHANGE_STATUS_DELETE = 1,
|
||||
GW_TOPO_CHANGE_STATUS_ENABLE = 2,
|
||||
GW_TOPO_CHANGE_STATUS_DISABLE = 8,
|
||||
GW_TOPO_CHANGE_STATUS_INVALID
|
||||
} gw_topo_change_status_e;
|
||||
|
||||
typedef enum _gw_device_type_e
|
||||
{
|
||||
GW_DEVICE_MASTER = 0,
|
||||
GW_DEVICE_SLAVE,
|
||||
GW_DEVICE_INVALID
|
||||
} gw_device_type_e;
|
||||
|
||||
typedef enum _gw_topo_get_reason_e
|
||||
{
|
||||
GW_TOPO_GET_REASON_CONNECT_CLOUD = 0,
|
||||
GW_TOPO_GET_REASON_CLI_CMD,
|
||||
GW_TOPO_GET_REASON_MAX
|
||||
} gw_topo_get_reason_e;
|
||||
|
||||
typedef enum _gateway_msg_type_e
|
||||
{
|
||||
GATEWAY_MSG_TYPE_ADD,
|
||||
GATEWAY_MSG_TYPE_ADD_RANGE,
|
||||
GATEWAY_MSG_TYPE_DEL,
|
||||
GATEWAY_MSG_TYPE_DEL_RANGE,
|
||||
GATEWAY_MSG_TYPE_RESET,
|
||||
GATEWAY_MSG_TYPE_UPDATE,
|
||||
GATEWAY_MSG_TYPE_ADDALL,
|
||||
GATEWAY_MSG_TYPE_DELALL,
|
||||
GATEWAY_MSG_TYPE_QUERY_SUBDEV_ID,
|
||||
GATEWAY_MSG_TYPE_CLOUD_CONNECT,
|
||||
GATEWAY_MSG_TYPE_PERMIT_JOIN,
|
||||
GATEWAY_MSG_TYPE_MAX
|
||||
} gateway_msg_type_e;
|
||||
|
||||
typedef struct _gateway_msg_s
|
||||
{
|
||||
gateway_msg_type_e msg_type;
|
||||
int devid;
|
||||
int devid_end;
|
||||
char *payload;
|
||||
int payload_len;
|
||||
} gateway_msg_t;
|
||||
|
||||
extern int gateway_ut_init(void);
|
||||
extern int gateway_ut_handle_permit_join(void);
|
||||
extern int gateway_ut_handle_topolist_reply(const char *payload, const int payload_len);
|
||||
extern int gateway_ut_send_msg(gateway_msg_t *, int);
|
||||
extern int gateway_ut_msg_process(int master_devid, int timeout);
|
||||
extern void gateway_ut_misc_process(uint64_t time_now_sec);
|
||||
extern int gateway_ut_update_subdev(gw_topo_get_reason_e reason);
|
||||
#endif
|
||||
136
Products/example/linkkit_gateway/linkkit_gateway.mk
Normal file
136
Products/example/linkkit_gateway/linkkit_gateway.mk
Normal file
|
|
@ -0,0 +1,136 @@
|
|||
NAME := linkkit_gateway
|
||||
|
||||
# Cloud server region: MAINLAND or SINGAPORE, GERMANY
|
||||
CONFIG_SERVER_REGION ?= MAINLAND
|
||||
|
||||
# Cloud server env: ON_DAILY, ON_PRE, ONLINE
|
||||
CONFIG_SERVER_ENV ?= ONLINE
|
||||
|
||||
CONFIG_FIRMWARE_VERSION = app-1.6.0
|
||||
|
||||
$(NAME)_SOURCES := gateway_main.c \
|
||||
gateway_entry.c \
|
||||
gateway_cmds.c \
|
||||
gateway_api.c \
|
||||
gateway_ut.c
|
||||
|
||||
# support MCU OTA, default DISABLE
|
||||
CONFIG_SUPPORT_MCU_OTA ?= DISABLE
|
||||
|
||||
# Compile date and time
|
||||
ifeq ("${DATE}", "")
|
||||
DATE=date
|
||||
endif
|
||||
CONFIG_COMPILE_DATE ?= $(shell ${DATE} +%Y%m%d.%H%M%S)
|
||||
|
||||
# Firmware type: 0 - release, 1 - debug
|
||||
CONFIG_DEBUG ?= 0
|
||||
|
||||
# Manufacturer self-defined AP auto find, default close
|
||||
CONFIG_MANUFACT_AP_FIND ?= FALSE
|
||||
|
||||
# Dev ap support batch awss, default close
|
||||
CONFIG_BAT_DEV_AP_AWSS ?= FALSE
|
||||
|
||||
# Zero wifi provision support transfer app token, default close
|
||||
CONFIG_ZERO_APPTOKEN ?= FALSE
|
||||
|
||||
|
||||
$(NAME)_COMPONENTS += framework/protocol/linkkit/sdk \
|
||||
framework/protocol/linkkit/hal \
|
||||
framework/netmgr \
|
||||
framework/common \
|
||||
utility/cjson \
|
||||
framework/uOTA
|
||||
|
||||
GLOBAL_INCLUDES += ../../../../framework/protocol/linkkit/include \
|
||||
../../../../framework/protocol/linkkit/include/imports \
|
||||
../../../../framework/protocol/linkkit/include/exports \
|
||||
|
||||
GLOBAL_CFLAGS += -DCONFIG_YWSS \
|
||||
-DBUILD_AOS \
|
||||
-DAWSS_SUPPORT_STATIS
|
||||
|
||||
ifeq ($(HOST_MCU_FAMILY),esp8266)
|
||||
GLOBAL_DEFINES += ESP8266_CHIPSET
|
||||
else
|
||||
$(NAME)_COMPONENTS += cli
|
||||
GLOBAL_CFLAGS += -DCONFIG_AOS_CLI
|
||||
|
||||
#Create thread of cm_yield in mqtt_client.c
|
||||
GLOBAL_CFLAGS += -DCONFIG_SDK_THREAD_COST=1
|
||||
|
||||
GLOBAL_CFLAGS += -DDEV_ERRCODE_ENABLE \
|
||||
-DDEV_OFFLINE_OTA_ENABLE \
|
||||
-DAWSS_BATCH_DEVAP_ENABLE \
|
||||
-DMANUFACT_AP_FIND_ENABLE
|
||||
|
||||
#GLOBAL_CFLAGS += -DDEV_OFFLINE_SECURE_OTA_ENABLE
|
||||
GLOBAL_CFLAGS += -DDEV_OFFLINE_LOG_ENABLE
|
||||
#GLOBAL_CFLAGS += -DOFFLINE_LOG_UT_TEST
|
||||
endif
|
||||
|
||||
GLOBAL_CFLAGS += -DREGION_${CONFIG_SERVER_REGION}
|
||||
|
||||
GLOBAL_CFLAGS += -D${CONFIG_SERVER_ENV}
|
||||
|
||||
BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
|
||||
GLOBAL_CFLAGS += -DGIT_BRANCH=\"${BRANCH}\"
|
||||
COMMIT_HASH := $(shell git rev-parse HEAD)
|
||||
GLOBAL_CFLAGS += -DGIT_HASH=\"${COMMIT_HASH}\"
|
||||
COMPILE_USER := ${USER}
|
||||
GLOBAL_CFLAGS += -DCOMPILE_HOST=\"${COMPILE_USER}\"
|
||||
SERVER_CONF_STRING := "${CONFIG_SERVER_REGION}-${CONFIG_SERVER_ENV}"
|
||||
GLOBAL_CFLAGS += -DREGION_ENV_STRING=\"${SERVER_CONF_STRING}\"
|
||||
GLOBAL_CFLAGS += -DAPP_NAME=\"${APP}\"
|
||||
GLOBAL_CFLAGS += -DPLATFORM=\"${PLATFORM}\"
|
||||
CONFIG_SYSINFO_APP_VERSION = ${CONFIG_FIRMWARE_VERSION}-${CONFIG_COMPILE_DATE}
|
||||
GLOBAL_CFLAGS += -DSYSINFO_APP_VERSION=\"$(CONFIG_SYSINFO_APP_VERSION)\"
|
||||
|
||||
ifneq ("$(BOARD)", "")
|
||||
$(info server region: ${CONFIG_SERVER_REGION})
|
||||
$(info server env: ${CONFIG_SERVER_ENV})
|
||||
$(info APP: ${APP} Board: ${PLATFORM})
|
||||
$(info host user: ${COMPILE_USER})
|
||||
$(info branch: ${BRANCH})
|
||||
$(info hash: ${COMMIT_HASH})
|
||||
$(info app_version_new:${CONFIG_SYSINFO_APP_VERSION})
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_DEBUG), 1)
|
||||
GLOBAL_CFLAGS += -DDEFAULT_LOG_LEVEL_DEBUG
|
||||
GLOBAL_CFLAGS += -DCONFIG_BLDTIME_MUTE_DBGLOG=0
|
||||
$(info firmware type: DEBUG)
|
||||
else
|
||||
GLOBAL_CFLAGS += -DCONFIG_BLDTIME_MUTE_DBGLOG=1
|
||||
$(info firmware type: RELEASE)
|
||||
endif
|
||||
|
||||
ifeq ($(LWIP),1)
|
||||
$(NAME)_COMPONENTS += protocols.net
|
||||
no_with_lwip := 0
|
||||
endif
|
||||
|
||||
GLOBAL_INCLUDES += ./
|
||||
|
||||
include ./make.settings
|
||||
|
||||
SWITCH_VARS := FEATURE_MQTT_COMM_ENABLED FEATURE_ALCS_ENABLED FEATURE_DEVICE_MODEL_ENABLED \
|
||||
FEATURE_DEVICE_MODEL_GATEWAY \
|
||||
FEATURE_DEVICE_MODEL_RAWDATA_SOLO FEATURE_COAP_COMM_ENABLED FEATURE_HTTP2_COMM_ENABLED \
|
||||
FEATURE_HTTP_COMM_ENABLED FEATURE_SAL_ENABLED FEATURE_WIFI_PROVISION_ENABLED FEATURE_AWSS_SUPPORT_SMARTCONFIG\
|
||||
FEATURE_AWSS_SUPPORT_ZEROCONFIG FEATURE_AWSS_SUPPORT_SMARTCONFIG FEATURE_AWSS_SUPPORT_ZEROCONFIG FEATURE_AWSS_SUPPORT_PHONEASAP \
|
||||
FEATURE_AWSS_SUPPORT_ROUTER FEATURE_AWSS_SUPPORT_DEV_AP FEATURE_OTA_ENABLED FEATURE_MQTT_AUTO_SUBSCRIBE FEATURE_MQTT_PREAUTH_SUPPORT_HTTPS_CDN \
|
||||
FEATURE_DM_UNIFIED_SERVICE_POST
|
||||
|
||||
$(foreach v, \
|
||||
$(SWITCH_VARS), \
|
||||
$(if $(filter y,$($(v))), \
|
||||
$(eval GLOBAL_DEFINES += $(subst FEATURE_,,$(v)))) \
|
||||
)
|
||||
|
||||
$(foreach v, \
|
||||
$(SWITCH_VARS), \
|
||||
$(if $(filter y,$($(v))), \
|
||||
$(eval SDK_DEFINES += $(subst FEATURE_,-D,$(v)))) \
|
||||
)
|
||||
32
Products/example/linkkit_gateway/make.settings
Executable file
32
Products/example/linkkit_gateway/make.settings
Executable file
|
|
@ -0,0 +1,32 @@
|
|||
#
|
||||
# Configure FeiYan SDK Features
|
||||
#
|
||||
|
||||
#=========Basic Features==========
|
||||
FEATURE_SRCPATH="."
|
||||
FEATURE_MQTT_COMM_ENABLED=y
|
||||
FEATURE_MQTT_AUTO_SUBSCRIBE=y
|
||||
FEATURE_DEVICE_MODEL_ENABLED=y
|
||||
FEATURE_OTA_ENABLED=y
|
||||
FEATURE_DEV_BIND_ENABLED=y
|
||||
FEATURE_SUPPORT_TLS=y
|
||||
FEATURE_DEVICE_MODEL_GATEWAY=y
|
||||
#Support unified service post
|
||||
FEATURE_DM_UNIFIED_SERVICE_POST=y
|
||||
#=========Basic Features==========
|
||||
|
||||
#=========User Config Features==========
|
||||
FEATURE_ALCS_ENABLED=y
|
||||
|
||||
#
|
||||
# AWSS Configurations
|
||||
#
|
||||
FEATURE_WIFI_PROVISION_ENABLED=y
|
||||
|
||||
FEATURE_AWSS_SUPPORT_SMARTCONFIG=y
|
||||
FEATURE_AWSS_SUPPORT_ZEROCONFIG=y
|
||||
FEATURE_AWSS_SUPPORT_DEV_AP=y
|
||||
|
||||
FEATURE_AWSS_SUPPORT_PHONEASAP=n
|
||||
FEATURE_AWSS_SUPPORT_ROUTER=n
|
||||
#=========User Config Features==========
|
||||
145
Products/example/linkkit_gateway/makefile
Normal file
145
Products/example/linkkit_gateway/makefile
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
|
||||
include linkkit_gateway.mk
|
||||
target = linkkit_gateway
|
||||
|
||||
CFLAGS += -DALCS_ENABLED
|
||||
|
||||
INCLUDE_PATH = -I$(PWD)
|
||||
INCLUDE = $(INCLUDE_PATH)/ \
|
||||
-I$(PWD)/../../../prebuild/include \
|
||||
-I$(PWD)/../../../prebuild/include/exports \
|
||||
-I$(PWD)/../../../prebuild/include/imports \
|
||||
-I$(PWD)/../../../prebuild/include/kernel/rhino/core/include \
|
||||
-I$(PWD)/../../../prebuild/include/framework/netmgr/include \
|
||||
-I$(PWD)/../../../prebuild/include/platform/arch/arm/armv7m/gcc/m4 \
|
||||
-I$(PWD)/../../../prebuild/include/board/$(BOARD) \
|
||||
-I$(PWD)/../../../Living_SDK/kernel/protocols/net/include/
|
||||
|
||||
app = $(wildcard ./*.c)
|
||||
obj_app = $(patsubst %.c,%.o,$(app))
|
||||
|
||||
|
||||
CROSS_PREFIX="arm-none-eabi-"
|
||||
DEPENDS=""
|
||||
CCLD=""
|
||||
LDFLAGS=""
|
||||
CC=arm-none-eabi-gcc
|
||||
LD=arm-none-eabi-ld
|
||||
AR=arm-none-eabi-ar
|
||||
STRIP=arm-none-eabi-strip
|
||||
OBJCOPY=arm-none-eabi-objcopy
|
||||
|
||||
# CFLAGS += -MD -ggdb -Os -Wall -Wfatal-errors -fsigned-char -ffunction-sections -fdata-sections -fno-common -std=gnu11 -Werror \
|
||||
# -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
|
||||
CFLAGS += -DWITH_LWIP -DCONFIG_AOS_CLI -DLOG_SIMPLE
|
||||
CFLAGS += ${GLOBAL_CFLAGS}
|
||||
CFLAGS += ${SDK_DEFINES}
|
||||
|
||||
ifeq ("$(BOARD)", "hf-lpt230")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
INCLUDE += -I$(PWD)/../../../Living_SDK/platform/mcu/rda5981x/wifi/inc/ \
|
||||
-I$(PWD)/../../../Living_SDK/platform/mcu/rda5981x/wifi/inc/arch
|
||||
else ifeq ("$(BOARD)", "hf-lpt130")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
INCLUDE += -I$(PWD)/../../../Living_SDK/platform/mcu/rda5981x/wifi/inc/ \
|
||||
-I$(PWD)/../../../Living_SDK/platform/mcu/rda5981x/wifi/inc/arch
|
||||
else ifeq ("$(BOARD)", "hf-lpb130")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
INCLUDE += -I$(PWD)/../../../Living_SDK/platform/mcu/rda5981x/wifi/inc/ \
|
||||
-I$(PWD)/../../../Living_SDK/platform/mcu/rda5981x/wifi/inc/arch
|
||||
else ifeq ("$(BOARD)", "hf-lpb135")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
INCLUDE += -I$(PWD)/../../../Living_SDK/platform/mcu/rda5981x/wifi/inc/ \
|
||||
-I$(PWD)/../../../Living_SDK/platform/mcu/rda5981x/wifi/inc/arch
|
||||
else ifeq ("$(BOARD)", "uno-91h")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
INCLUDE += -I$(PWD)/../../../Living_SDK/platform/mcu/rda5981x/wifi/inc/ \
|
||||
-I$(PWD)/../../../Living_SDK/platform/mcu/rda5981x/wifi/inc/arch
|
||||
else ifeq ("$(BOARD)", "mk1101")
|
||||
else ifeq ("$(BOARD)", "mk3060")
|
||||
CFLAGS += -mcpu=arm968e-s -march=armv5te -mthumb -mthumb-interwork -mlittle-endian -w -Os -Wall -Wfatal-errors -fsigned-char -ffunction-sections -fdata-sections -fno-common -std=gnu11
|
||||
INCLUDE += -I$(PWD)/../../../Living_SDK/platform/mcu/moc108/include/lwip-2.0.2/port/ \
|
||||
-I$(PWD)/../../../Living_SDK/platform/mcu/moc108/include/lwip-2.0.2/port/arch \
|
||||
-I$(PWD)/../../../Living_SDK/platform/mcu/moc108/include/common/ \
|
||||
-I$(PWD)/../../../Living_SDK/platform/mcu/moc108/include/ \
|
||||
-I$(PWD)/../../../Living_SDK/platform/mcu/moc108/include/ip/common \
|
||||
-I$(PWD)/../../../Living_SDK/platform/mcu/moc108/include/driver/include \
|
||||
-I$(PWD)/../../../Living_SDK/platform/mcu/moc108/include/driver/common
|
||||
else ifeq ("$(BOARD)", "mk3061")
|
||||
CFLAGS += -mcpu=arm968e-s -march=armv5te -mthumb -mthumb-interwork -mlittle-endian -w -Os -Wall -Wfatal-errors -fsigned-char -ffunction-sections -fdata-sections -fno-common -std=gnu11
|
||||
INCLUDE += -I$(PWD)/../../../Living_SDK/platform/mcu/moc108/include/lwip-2.0.2/port/ \
|
||||
-I$(PWD)/../../../Living_SDK/platform/mcu/moc108/include/lwip-2.0.2/port/arch \
|
||||
-I$(PWD)/../../../Living_SDK/platform/mcu/moc108/include/common/ \
|
||||
-I$(PWD)/../../../Living_SDK/platform/mcu/moc108/include/ \
|
||||
-I$(PWD)/../../../Living_SDK/platform/mcu/moc108/include/ip/common \
|
||||
-I$(PWD)/../../../Living_SDK/platform/mcu/moc108/include/driver/include \
|
||||
-I$(PWD)/../../../Living_SDK/platform/mcu/moc108/include/driver/common
|
||||
|
||||
else ifeq ("$(BOARD)", "mk3080")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
INCLUDE += -I$(PWD)/../../../Living_SDK/platform/mcu/rtl8710bn/ \
|
||||
-I$(PWD)/../../../Living_SDK/platform/mcu/rtl8710bn/arch
|
||||
else ifeq ("$(BOARD)", "mk3092")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
INCLUDE += -I$(PWD)/../../../Living_SDK/platform/mcu/rtl8710bn/ \
|
||||
-I$(PWD)/../../../Living_SDK/platform/mcu/rtl8710bn/arch
|
||||
else ifeq ("$(BOARD)", "mk5080")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
INCLUDE += -I$(PWD)/../../../Living_SDK/platform/mcu/rtl8710bn/ \
|
||||
-I$(PWD)/../../../Living_SDK/platform/mcu/rtl8710bn/arch
|
||||
|
||||
|
||||
else ifeq ("$(BOARD)", "asr5501")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -mfpu=fpv4-sp-d16 -std=gnu11 -w
|
||||
INCLUDE += -I$(PWD)/../../../Living_SDK/board/asr5501/drivers/include/lwip_if/ \
|
||||
-I$(PWD)/../../../Living_SDK/board/asr5501/drivers/include/lwip_if/arch/
|
||||
else ifeq ("$(BOARD)", "mx1270")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -mfpu=fpv4-sp-d16 -std=gnu11 -w
|
||||
INCLUDE += -I$(PWD)/../../../Living_SDK/board/mx1270/drivers/include/lwip_if/ \
|
||||
-I$(PWD)/../../../Living_SDK/board/mx1270/drivers/include/lwip_if/arch/
|
||||
|
||||
else ifeq ("$(BOARD)", "bk7231devkitc")
|
||||
CFLAGS += -mcpu=arm968e-s -march=armv5te -mthumb -mthumb-interwork -mlittle-endian -w -Os -Wall -Wfatal-errors -fsigned-char -ffunction-sections -fdata-sections -fno-common -std=gnu11
|
||||
CFLAGS += -DBK7231DEVKITC
|
||||
else ifeq ("$(BOARD)", "bk7231sdevkitc")
|
||||
CFLAGS += -mcpu=arm968e-s -march=armv5te -mthumb -mthumb-interwork -mlittle-endian -w -Os -Wall -Wfatal-errors -fsigned-char -ffunction-sections -fdata-sections -fno-common -std=gnu11
|
||||
CFLAGS += -DBK7231SDEVKITC
|
||||
else ifeq ("$(BOARD)", "bk7231udevkitc")
|
||||
CFLAGS += -mcpu=arm968e-s -march=armv5te -mthumb -mthumb-interwork -mlittle-endian -w -Os -Wall -Wfatal-errors -fsigned-char -ffunction-sections -fdata-sections -fno-common -std=gnu11
|
||||
CFLAGS += -DBK7231UDEVKITC
|
||||
else ifeq ("$(BOARD)", "b_l475e")
|
||||
|
||||
else ifeq ("$(BOARD)", "eml3047")
|
||||
else ifeq ("$(BOARD)", "frdmkl27z")
|
||||
else ifeq ("$(BOARD)", "lpcxpresso54102")
|
||||
|
||||
else ifeq ("$(BOARD)", "pca10056")
|
||||
|
||||
## toolchains['arm-rockchip-linux-gnueabihf']
|
||||
else ifeq ("$(BOARD)", "rk1108")
|
||||
|
||||
## toolchains['nds32le-elf-newlib-v3']
|
||||
else ifeq ("$(BOARD)", "sv6266_evb")
|
||||
endif
|
||||
|
||||
ifneq ("$(BOARD)", "")
|
||||
$(info BOARD=${BOARD} CONFIG_COMPILE_DATE=${CONFIG_COMPILE_DATE})
|
||||
endif
|
||||
|
||||
# Compile date
|
||||
CONFIG_COMPILE_DATE ?= $(shell date +%Y%m%d.%H%M%S)
|
||||
all : $(target)
|
||||
|
||||
$(target):$(obj_app)
|
||||
@mkdir -p obj
|
||||
@mv *.o obj
|
||||
@$(RM) -rf *.d
|
||||
@$(AR) -rcs $(PWD)/../../../prebuild/lib/$(target).a obj/*.o 2>/dev/null
|
||||
@$(RM) -rf obj
|
||||
|
||||
%.o:%.c
|
||||
@$(CC) -c $(CFLAGS) $(INCLUDE) -o $@ $<
|
||||
|
||||
clean:
|
||||
@-$(RM) obj/*
|
||||
564
Products/example/living_platform/app_entry.c
Normal file
564
Products/example/living_platform/app_entry.c
Normal file
|
|
@ -0,0 +1,564 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2019 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <aos/aos.h>
|
||||
#include <aos/yloop.h>
|
||||
#include "netmgr.h"
|
||||
#include "iot_export.h"
|
||||
#include "iot_import.h"
|
||||
#include "aos/kv.h"
|
||||
|
||||
#ifdef CSP_LINUXHOST
|
||||
#include <signal.h>
|
||||
#endif
|
||||
|
||||
#include <k_api.h>
|
||||
|
||||
#if defined(OTA_ENABLED) && defined(BUILD_AOS)
|
||||
#include "ota_service.h"
|
||||
#endif
|
||||
|
||||
#include "living_platform_main.h"
|
||||
#include "app_entry.h"
|
||||
#include "living_platform_cmds.h"
|
||||
#include "living_platform_ut.h"
|
||||
|
||||
static char living_platform_started = 0;
|
||||
static int living_platform_duration_interval = 0;
|
||||
|
||||
extern k_mm_head *g_kmm_head;
|
||||
extern uint32_t dumpsys_mm_info_func(uint32_t len);
|
||||
extern uint32_t dumpsys_task_func(char *buf, uint32_t len, int detail);
|
||||
static void dump_task_and_mem_info(void)
|
||||
{
|
||||
#if defined(CONFIG_AOS_CLI)
|
||||
printf("======================================");
|
||||
dumpsys_mm_info_func(0);
|
||||
printf("======================================");
|
||||
dumpsys_task_func(NULL, 0, 1);
|
||||
printf("======================================");
|
||||
#else
|
||||
printf("============free heap size =%d==========\r\n", g_kmm_head->free_size);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void living_platform_duration_work(void *param)
|
||||
{
|
||||
dump_task_and_mem_info();
|
||||
|
||||
aos_post_delayed_action(living_platform_duration_interval, living_platform_duration_work, NULL);
|
||||
}
|
||||
|
||||
static void wifi_service_event(input_event_t *event, void *priv_data)
|
||||
{
|
||||
netmgr_ap_config_t config;
|
||||
|
||||
if (event->type != EV_WIFI || event->code != CODE_WIFI_ON_GOT_IP)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
memset(&config, 0, sizeof(netmgr_ap_config_t));
|
||||
netmgr_get_ap_config(&config);
|
||||
|
||||
living_platform_info("wifi_service_event config.ssid:%s", config.ssid);
|
||||
if (strcmp(config.ssid, "adha") == 0 || strcmp(config.ssid, "aha") == 0)
|
||||
{
|
||||
// clear_wifi_ssid();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!living_platform_started)
|
||||
{
|
||||
aos_task_new("linkkit", (void (*)(void *))living_platform_main, NULL, LIVING_PLATFORM_MAIN_THREAD_STACKSZIE);
|
||||
living_platform_started = 1;
|
||||
}
|
||||
}
|
||||
|
||||
static void cloud_service_event(input_event_t *event, void *priv_data)
|
||||
{
|
||||
if (event->type != EV_YUNIO)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
living_platform_info("cloud_service_event %d", event->code);
|
||||
|
||||
if (event->code == CODE_YUNIO_ON_CONNECTED)
|
||||
{
|
||||
living_platform_info("user sub and pub here");
|
||||
return;
|
||||
}
|
||||
|
||||
if (event->code == CODE_YUNIO_ON_DISCONNECTED)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Note:
|
||||
* the linkkit_event_monitor must not block and should run to complete fast
|
||||
* if user wants to do complex operation with much time,
|
||||
* user should post one task to do this, not implement complex operation in
|
||||
* linkkit_event_monitor
|
||||
*/
|
||||
|
||||
void linkkit_event_monitor(int event)
|
||||
{
|
||||
switch (event)
|
||||
{
|
||||
case IOTX_AWSS_START: // AWSS start without enbale, just supports device discover
|
||||
// operate led to indicate user
|
||||
living_platform_info("IOTX_AWSS_START");
|
||||
break;
|
||||
case IOTX_AWSS_ENABLE: // AWSS enable, AWSS doesn't parse awss packet until AWSS is enabled.
|
||||
living_platform_info("IOTX_AWSS_ENABLE");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_LOCK_CHAN: // AWSS lock channel(Got AWSS sync packet)
|
||||
living_platform_info("IOTX_AWSS_LOCK_CHAN");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_PASSWD_ERR: // AWSS decrypt passwd error
|
||||
living_platform_info("IOTX_AWSS_PASSWD_ERR");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_GOT_SSID_PASSWD:
|
||||
living_platform_info("IOTX_AWSS_GOT_SSID_PASSWD");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_ADHA: // AWSS try to connnect adha (device
|
||||
// discover, router solution)
|
||||
living_platform_info("IOTX_AWSS_CONNECT_ADHA");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_ADHA_FAIL: // AWSS fails to connect adha
|
||||
living_platform_info("IOTX_AWSS_CONNECT_ADHA_FAIL");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_AHA: // AWSS try to connect aha (AP solution)
|
||||
living_platform_info("IOTX_AWSS_CONNECT_AHA");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_AHA_FAIL: // AWSS fails to connect aha
|
||||
living_platform_info("IOTX_AWSS_CONNECT_AHA_FAIL");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_SETUP_NOTIFY: // AWSS sends out device setup information
|
||||
// (AP and router solution)
|
||||
living_platform_info("IOTX_AWSS_SETUP_NOTIFY");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_ROUTER: // AWSS try to connect destination router
|
||||
living_platform_info("IOTX_AWSS_CONNECT_ROUTER");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_ROUTER_FAIL: // AWSS fails to connect destination
|
||||
// router.
|
||||
living_platform_info("IOTX_AWSS_CONNECT_ROUTER_FAIL");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_GOT_IP: // AWSS connects destination successfully and got
|
||||
// ip address
|
||||
living_platform_info("IOTX_AWSS_GOT_IP");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_SUC_NOTIFY: // AWSS sends out success notify (AWSS
|
||||
// sucess)
|
||||
living_platform_info("IOTX_AWSS_SUC_NOTIFY");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_BIND_NOTIFY: // AWSS sends out bind notify information to
|
||||
// support bind between user and device
|
||||
living_platform_info("IOTX_AWSS_BIND_NOTIFY");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_ENABLE_TIMEOUT: // AWSS enable timeout
|
||||
// user needs to enable awss again to support get ssid & passwd of router
|
||||
living_platform_info("IOTX_AWSS_ENALBE_TIMEOUT");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_CONN_CLOUD: // Device try to connect cloud
|
||||
living_platform_info("IOTX_CONN_CLOUD");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_CONN_CLOUD_FAIL: // Device fails to connect cloud, refer to
|
||||
// net_sockets.h for error code
|
||||
living_platform_info("IOTX_CONN_CLOUD_FAIL");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_CONN_CLOUD_SUC: // Device connects cloud successfully
|
||||
living_platform_info("IOTX_CONN_CLOUD_SUC");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_RESET: // Linkkit reset success (just got reset response from
|
||||
// cloud without any other operation)
|
||||
living_platform_info("IOTX_RESET");
|
||||
break;
|
||||
case IOTX_CONN_REPORT_TOKEN_SUC:
|
||||
living_platform_info("---- report token success ----");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void user_key_process(input_event_t *eventinfo, void *priv_data)
|
||||
{
|
||||
if (eventinfo->type != EV_KEY)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
living_platform_info("awss config press %d\n", eventinfo->value);
|
||||
|
||||
if (eventinfo->code == CODE_BOOT)
|
||||
{
|
||||
if (eventinfo->value == VALUE_KEY_CLICK)
|
||||
{
|
||||
living_platform_do_awss_active();
|
||||
}
|
||||
else if (eventinfo->value == VALUE_KEY_LTCLICK)
|
||||
{
|
||||
living_platform_awss_reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(OTA_ENABLED) && defined(BUILD_AOS)
|
||||
static ota_service_t ctx = {0};
|
||||
static bool ota_service_inited = false;
|
||||
|
||||
void *living_platform_entry_get_uota_ctx(void)
|
||||
{
|
||||
if (ota_service_inited == true)
|
||||
{
|
||||
return (void *)&ctx;
|
||||
}
|
||||
else
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static int ota_init(void)
|
||||
{
|
||||
char product_key[PRODUCT_KEY_LEN + 1] = {0};
|
||||
char device_name[DEVICE_NAME_LEN + 1] = {0};
|
||||
char device_secret[DEVICE_SECRET_LEN + 1] = {0};
|
||||
HAL_GetProductKey(product_key);
|
||||
HAL_GetDeviceName(device_name);
|
||||
HAL_GetDeviceSecret(device_secret);
|
||||
memset(&ctx, 0, sizeof(ota_service_t));
|
||||
strncpy(ctx.pk, product_key, sizeof(ctx.pk) - 1);
|
||||
strncpy(ctx.dn, device_name, sizeof(ctx.dn) - 1);
|
||||
strncpy(ctx.ds, device_secret, sizeof(ctx.ds) - 1);
|
||||
ctx.trans_protcol = 0;
|
||||
ctx.dl_protcol = 3;
|
||||
ota_service_init(&ctx);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int mqtt_connected_event_handler(void)
|
||||
{
|
||||
#if defined(OTA_ENABLED) && defined(BUILD_AOS)
|
||||
if (ota_service_inited == true)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
LOG("MQTT reconnected, let's redo OTA upgrade");
|
||||
if ((ctx.h_tr) && (ctx.h_tr->upgrade))
|
||||
{
|
||||
LOG("Redoing OTA upgrade");
|
||||
ret = ctx.h_tr->upgrade(&ctx);
|
||||
if (ret < 0)
|
||||
LOG("Failed to do OTA upgrade");
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
LOG("MQTT Construct OTA start to inform");
|
||||
#ifdef DEV_OFFLINE_OTA_ENABLE
|
||||
ota_service_inform(&ctx);
|
||||
#else
|
||||
ota_init();
|
||||
#endif
|
||||
ota_service_inited = true;
|
||||
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef POWER_CYCLE_AWSS
|
||||
|
||||
#ifndef DEFAULT_CYCLE_TIMEOUT
|
||||
#define DEFAULT_CYCLE_TIMEOUT 5000
|
||||
#endif
|
||||
static int network_start(void)
|
||||
{
|
||||
netmgr_ap_config_t my_config = {0};
|
||||
netmgr_get_ap_config(&my_config);
|
||||
if (strlen(my_config.ssid))
|
||||
{
|
||||
aos_task_new("netmgr_start", living_platform_start_netmgr, NULL, 5120);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("INFO: %s no valied ap confing\r\n", __func__);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int check_and_start_awss(void)
|
||||
{
|
||||
int ret, len = 0;
|
||||
int awss_type = 0; /* 0 -- unknown, 1 -- dev_ap, 2 -- one key */
|
||||
char value_string[8];
|
||||
netmgr_ap_config_t my_config = {0};
|
||||
|
||||
memset(value_string, 0, sizeof(value_string));
|
||||
len = sizeof(value_string);
|
||||
/* check awss type */
|
||||
ret = aos_kv_get("awss_type", value_string, &len);
|
||||
if (0 == strncmp("dev_ap", value_string, strlen("dev_ap")))
|
||||
{
|
||||
printf("INFO: %s awss type: device ap\r\n", __func__);
|
||||
living_platform_do_awss_dev_ap();
|
||||
awss_type = 1;
|
||||
}
|
||||
else if (0 == strncmp("one_key", value_string, strlen("dev_ap")))
|
||||
{
|
||||
printf("INFO: %s awss type: one key\r\n", __func__);
|
||||
living_platform_do_awss();
|
||||
living_platform_do_awss_active();
|
||||
awss_type = 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("WARN: %s awss type: unknown\r\n", __func__);
|
||||
awss_type = 0;
|
||||
}
|
||||
/* Clesr awss type */
|
||||
ret = aos_kv_del("awss_type");
|
||||
return awss_type;
|
||||
}
|
||||
|
||||
static void power_cycle_check(void *p)
|
||||
{
|
||||
int ret = -1;
|
||||
int count;
|
||||
|
||||
if (NULL == p)
|
||||
{
|
||||
printf("ERR: %s data pointer is NULL\r\n", __func__);
|
||||
return;
|
||||
}
|
||||
count = *(int *)p;
|
||||
printf("INFO: %s power cycle %d timeout clear\r\n", __func__, count);
|
||||
/* Reset cycle count */
|
||||
ret = aos_kv_set("power_cycle_count", "1", strlen("1") + 1, 1);
|
||||
if (0 != ret)
|
||||
{
|
||||
printf("ERR: %s aos_kv_set return: %d\r\n", __func__, ret);
|
||||
return;
|
||||
}
|
||||
if (count == 3)
|
||||
{ /* Three times for device ap awss */
|
||||
ret = aos_kv_set("awss_type", "dev_ap", strlen("dev_ap") + 1, 1);
|
||||
printf("INFO: %s prepare for device soft ap awss\r\n", __func__);
|
||||
aos_schedule_call(living_platform_awss_reset, NULL);
|
||||
}
|
||||
else if (count == 5)
|
||||
{ /* Five times for one key awss */
|
||||
aos_kv_set("awss_type", "one_key", strlen("one_key") + 1, 1);
|
||||
printf("INFO: %s prepare for smart config awss\r\n", __func__);
|
||||
aos_schedule_call(living_platform_awss_reset, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
static int power_cycle_awss(void)
|
||||
{
|
||||
int ret, len = 0;
|
||||
int awss_type = 0;
|
||||
int cycle_count = 0;
|
||||
char value_string[8];
|
||||
|
||||
memset(value_string, 0, sizeof(value_string));
|
||||
len = sizeof(value_string);
|
||||
|
||||
/* read stored cycle value */
|
||||
ret = aos_kv_get("power_cycle_count", value_string, &len);
|
||||
if (0 != ret)
|
||||
{
|
||||
printf("INFO: %s init power cycle count\r\n", __func__);
|
||||
snprintf(value_string, sizeof(value_string), "%d", 1); /* Set initial value */
|
||||
ret = aos_kv_set("power_cycle_count", value_string, strlen(value_string), 1);
|
||||
if (0 != ret)
|
||||
{
|
||||
printf("ERR: %s, aos_kv_set return: %d\r\n", __func__, ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cycle_count = atoi(value_string);
|
||||
printf("INFO: %s power cycle count: %d\r\n", __func__, cycle_count);
|
||||
memset(value_string, 0, sizeof(value_string));
|
||||
len = sizeof(value_string);
|
||||
if (cycle_count == 1)
|
||||
{ /* AWSS always start after a timeout clear reboot */
|
||||
awss_type = check_and_start_awss();
|
||||
snprintf(value_string, sizeof(value_string), "%d", 1);
|
||||
}
|
||||
if (cycle_count > 4)
|
||||
{ /* Cycle count max value is 5 */
|
||||
snprintf(value_string, sizeof(value_string), "%d", 1);
|
||||
}
|
||||
else
|
||||
{ /* Increase cycle count */
|
||||
snprintf(value_string, sizeof(value_string), "%d", cycle_count + 1);
|
||||
}
|
||||
if (0 == awss_type)
|
||||
{ /* Do not store cycle count when doing awss */
|
||||
ret = aos_kv_set("power_cycle_count", value_string, strlen(value_string) + 1, 1);
|
||||
aos_post_delayed_action(DEFAULT_CYCLE_TIMEOUT, power_cycle_check, (void *)(&cycle_count));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef MANUFACT_AP_FIND_ENABLE
|
||||
void manufact_ap_find_process(int result)
|
||||
{
|
||||
// Informed manufact ap found or not.
|
||||
// If manufact ap found, lower layer will auto connect the manufact ap
|
||||
// IF manufact ap not found, lower layer will enter normal awss state
|
||||
if (result == 0)
|
||||
{
|
||||
LOG("%s ap found.\n", __func__);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG("%s ap not found.\n", __func__);
|
||||
}
|
||||
}
|
||||
|
||||
int check_manufacture_ap_info(void)
|
||||
{
|
||||
uint8_t m_manu_flag = 0;
|
||||
int m_manu_flag_len = sizeof(m_manu_flag);
|
||||
aos_kv_get(MANUAP_CONTROL_KEY, &m_manu_flag, &m_manu_flag_len);
|
||||
if (m_manu_flag)
|
||||
{
|
||||
netmgr_manuap_info_set("TEST_AP", "TEST_PASSWORD", manufact_ap_find_process);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static void show_firmware_version(void)
|
||||
{
|
||||
printf("\n--------Firmware info--------");
|
||||
printf("\napp: %s, board: %s", APP_NAME, PLATFORM);
|
||||
#ifdef DEBUG
|
||||
printf("\nHost: %s", COMPILE_HOST);
|
||||
#endif
|
||||
printf("\nBranch: %s", GIT_BRANCH);
|
||||
printf("\nHash: %s", GIT_HASH);
|
||||
printf("\nDate: %s %s", __DATE__, __TIME__);
|
||||
printf("\nKernel: %s", aos_get_kernel_version());
|
||||
printf("\nLinkKit: %s", LINKKIT_VERSION);
|
||||
printf("\nAPP ver: %s", aos_get_app_version());
|
||||
|
||||
printf("\nRegion env: %s\n\n", REGION_ENV_STRING);
|
||||
}
|
||||
|
||||
int application_start(int argc, char **argv)
|
||||
{
|
||||
int len = 0;
|
||||
char interval[9] = {0};
|
||||
|
||||
#ifdef CSP_LINUXHOST
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
#endif
|
||||
|
||||
#ifdef WITH_SAL
|
||||
sal_init();
|
||||
#endif
|
||||
|
||||
#ifdef MDAL_MAL_ICA_TEST
|
||||
HAL_MDAL_MAL_Init();
|
||||
#endif
|
||||
|
||||
#ifdef DEFAULT_LOG_LEVEL_DEBUG
|
||||
IOT_SetLogLevel(IOT_LOG_DEBUG);
|
||||
#else
|
||||
#ifdef CSP_LINUXHOST
|
||||
IOT_SetLogLevel(IOT_LOG_DEBUG);
|
||||
#else
|
||||
IOT_SetLogLevel(IOT_LOG_INFO);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
show_firmware_version();
|
||||
living_platform_load_device_meta_info(); //User have to change their own way to burn device mate info
|
||||
|
||||
#ifdef DEV_OFFLINE_OTA_ENABLE
|
||||
ota_init();
|
||||
#endif
|
||||
|
||||
netmgr_init();
|
||||
|
||||
#ifdef MANUFACT_AP_FIND_ENABLE
|
||||
check_manufacture_ap_info();
|
||||
#endif
|
||||
|
||||
#ifdef DEV_ERRCODE_ENABLE
|
||||
dev_diagnosis_module_init();
|
||||
#endif
|
||||
|
||||
aos_register_event_filter(EV_KEY, user_key_process, NULL);
|
||||
aos_register_event_filter(EV_WIFI, wifi_service_event, NULL);
|
||||
aos_register_event_filter(EV_YUNIO, cloud_service_event, NULL);
|
||||
|
||||
IOT_RegisterCallback(ITE_MQTT_CONNECT_SUCC, mqtt_connected_event_handler);
|
||||
|
||||
#ifdef CONFIG_AOS_CLI
|
||||
living_platform_register_cmds();
|
||||
#endif
|
||||
|
||||
#ifdef POWER_CYCLE_AWSS
|
||||
network_start();
|
||||
power_cycle_awss();
|
||||
#else
|
||||
aos_task_new("netmgr_start", living_platform_start_netmgr, NULL, 5120);
|
||||
#endif
|
||||
|
||||
//This code just for debug
|
||||
len = sizeof(int);
|
||||
if (0 == HAL_Kv_Get(LIVING_PLATFORM_DUMP_INTERVAL_KV_KEY, interval, &len))
|
||||
{
|
||||
living_platform_duration_interval = atoi(interval);
|
||||
|
||||
if (living_platform_duration_interval > 0)
|
||||
{
|
||||
aos_post_delayed_action(living_platform_duration_interval, living_platform_duration_work, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
aos_loop_run();
|
||||
|
||||
return 0;
|
||||
}
|
||||
9
Products/example/living_platform/app_entry.h
Normal file
9
Products/example/living_platform/app_entry.h
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
#ifndef __LIVING_PLATFORM_ENTRY_H__
|
||||
#define __LIVING_PLATFORM_ENTRY_H__
|
||||
|
||||
#define LIVING_PLATFORM_MAIN_THREAD_STACKSZIE (6 * 1024)
|
||||
#define LIVING_PLATFORM_DUMP_INTERVAL_KV_KEY "lpdump"
|
||||
|
||||
extern void linkkit_event_monitor(int event);
|
||||
|
||||
#endif
|
||||
Binary file not shown.
134
Products/example/living_platform/living_platform.mk
Normal file
134
Products/example/living_platform/living_platform.mk
Normal file
|
|
@ -0,0 +1,134 @@
|
|||
NAME := living_platform
|
||||
|
||||
# Cloud server region: MAINLAND or SINGAPORE, GERMANY
|
||||
CONFIG_SERVER_REGION ?= MAINLAND
|
||||
|
||||
# Cloud server env: ON_DAILY, ON_PRE, ONLINE
|
||||
CONFIG_SERVER_ENV ?= ONLINE
|
||||
|
||||
$(NAME)_SOURCES := living_platform_main.c \
|
||||
living_platform_cmds.c \
|
||||
living_platform_ut.c \
|
||||
app_entry.c
|
||||
|
||||
# support MCU OTA, default DISABLE
|
||||
CONFIG_SUPPORT_MCU_OTA ?= DISABLE
|
||||
|
||||
CONFIG_FIRMWARE_VERSION = app-1.6.0
|
||||
|
||||
# Compile date and time
|
||||
ifeq ("${DATE}", "")
|
||||
DATE=date
|
||||
endif
|
||||
|
||||
CONFIG_COMPILE_DATE ?= $(shell ${DATE} +%Y%m%d.%H%M%S)
|
||||
|
||||
# Firmware type: 0 - release, 1 - debug
|
||||
CONFIG_DEBUG ?= 0
|
||||
|
||||
$(NAME)_COMPONENTS += framework/protocol/linkkit/sdk \
|
||||
framework/protocol/linkkit/hal \
|
||||
framework/netmgr \
|
||||
framework/common \
|
||||
utility/cjson \
|
||||
framework/uOTA
|
||||
|
||||
GLOBAL_INCLUDES += ../../../../framework/protocol/linkkit/include \
|
||||
../../../../framework/protocol/linkkit/include/imports \
|
||||
../../../../framework/protocol/linkkit/include/exports \
|
||||
|
||||
|
||||
GLOBAL_CFLAGS += -DCONFIG_YWSS \
|
||||
-DBUILD_AOS \
|
||||
-DAWSS_SUPPORT_STATIS
|
||||
|
||||
ifeq ($(CONFIG_SUPPORT_MCU_OTA), ENABLE)
|
||||
GLOBAL_CFLAGS += -DSUPPORT_MCU_OTA
|
||||
endif
|
||||
|
||||
GLOBAL_CFLAGS += -DREGION_${CONFIG_SERVER_REGION}
|
||||
|
||||
GLOBAL_CFLAGS += -D${CONFIG_SERVER_ENV}
|
||||
|
||||
ifeq ($(HOST_MCU_FAMILY),esp8266)
|
||||
GLOBAL_CFLAGS += -DPOWER_CYCLE_AWSS
|
||||
GLOBAL_DEFINES += ESP8266_CHIPSET
|
||||
else
|
||||
$(NAME)_COMPONENTS += cli
|
||||
GLOBAL_CFLAGS += -DCONFIG_AOS_CLI
|
||||
|
||||
#Create thread of cm_yield in mqtt_client.c
|
||||
GLOBAL_CFLAGS += -DCONFIG_SDK_THREAD_COST=1
|
||||
|
||||
GLOBAL_CFLAGS += -DDEV_ERRCODE_ENABLE \
|
||||
-DDEV_OFFLINE_OTA_ENABLE \
|
||||
-DAWSS_BATCH_DEVAP_ENABLE \
|
||||
-DMANUFACT_AP_FIND_ENABLE
|
||||
|
||||
#GLOBAL_CFLAGS += -DDEV_OFFLINE_SECURE_OTA_ENABLE
|
||||
|
||||
ifneq ("$(PLATFORM)", "hf-lpt230")
|
||||
GLOBAL_CFLAGS += -DDEV_OFFLINE_LOG_ENABLE
|
||||
endif
|
||||
endif
|
||||
|
||||
BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
|
||||
GLOBAL_CFLAGS += -DGIT_BRANCH=\"${BRANCH}\"
|
||||
COMMIT_HASH := $(shell git rev-parse HEAD)
|
||||
GLOBAL_CFLAGS += -DGIT_HASH=\"${COMMIT_HASH}\"
|
||||
COMPILE_USER := ${USER}
|
||||
GLOBAL_CFLAGS += -DCOMPILE_HOST=\"${COMPILE_USER}\"
|
||||
SERVER_CONF_STRING := "${CONFIG_SERVER_REGION}-${CONFIG_SERVER_ENV}"
|
||||
GLOBAL_CFLAGS += -DREGION_ENV_STRING=\"${SERVER_CONF_STRING}\"
|
||||
GLOBAL_CFLAGS += -DAPP_NAME=\"${APP}\"
|
||||
GLOBAL_CFLAGS += -DPLATFORM=\"${PLATFORM}\"
|
||||
CONFIG_SYSINFO_APP_VERSION = ${CONFIG_FIRMWARE_VERSION}-${CONFIG_COMPILE_DATE}
|
||||
GLOBAL_CFLAGS += -DSYSINFO_APP_VERSION=\"$(CONFIG_SYSINFO_APP_VERSION)\"
|
||||
|
||||
ifneq ("$(BOARD)", "")
|
||||
$(info server region: ${CONFIG_SERVER_REGION})
|
||||
$(info server env: ${CONFIG_SERVER_ENV})
|
||||
$(info APP: ${APP} Board: ${PLATFORM})
|
||||
$(info host user: ${COMPILE_USER})
|
||||
$(info branch: ${BRANCH})
|
||||
$(info hash: ${COMMIT_HASH})
|
||||
$(info app_version_new:${CONFIG_SYSINFO_APP_VERSION})
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_DEBUG), 1)
|
||||
GLOBAL_CFLAGS += -DDEFAULT_LOG_LEVEL_DEBUG
|
||||
GLOBAL_CFLAGS += -DCONFIG_BLDTIME_MUTE_DBGLOG=0
|
||||
$(info firmware type: DEBUG)
|
||||
else
|
||||
GLOBAL_CFLAGS += -DCONFIG_BLDTIME_MUTE_DBGLOG=1
|
||||
$(info firmware type: RELEASE)
|
||||
endif
|
||||
|
||||
ifeq ($(LWIP),1)
|
||||
$(NAME)_COMPONENTS += protocols.net
|
||||
no_with_lwip := 0
|
||||
endif
|
||||
|
||||
GLOBAL_INCLUDES += ./
|
||||
|
||||
include ./make.settings
|
||||
|
||||
SWITCH_VARS := FEATURE_MQTT_COMM_ENABLED FEATURE_ALCS_ENABLED FEATURE_DEVICE_MODEL_ENABLED \
|
||||
FEATURE_DEVICE_MODEL_GATEWAY FEATURE_DEV_BIND_ENABLED \
|
||||
FEATURE_DEVICE_MODEL_RAWDATA_SOLO FEATURE_COAP_COMM_ENABLED FEATURE_HTTP2_COMM_ENABLED \
|
||||
FEATURE_HTTP_COMM_ENABLED FEATURE_SAL_ENABLED FEATURE_WIFI_PROVISION_ENABLED FEATURE_AWSS_SUPPORT_SMARTCONFIG\
|
||||
FEATURE_AWSS_SUPPORT_ZEROCONFIG FEATURE_AWSS_SUPPORT_SMARTCONFIG FEATURE_AWSS_SUPPORT_ZEROCONFIG FEATURE_AWSS_SUPPORT_PHONEASAP \
|
||||
FEATURE_AWSS_SUPPORT_ROUTER FEATURE_AWSS_SUPPORT_DEV_AP FEATURE_OTA_ENABLED FEATURE_MQTT_AUTO_SUBSCRIBE FEATURE_MQTT_PREAUTH_SUPPORT_HTTPS_CDN \
|
||||
FEATURE_DM_UNIFIED_SERVICE_POST
|
||||
|
||||
$(foreach v, \
|
||||
$(SWITCH_VARS), \
|
||||
$(if $(filter y,$($(v))), \
|
||||
$(eval GLOBAL_DEFINES += $(subst FEATURE_,,$(v)))) \
|
||||
)
|
||||
|
||||
$(foreach v, \
|
||||
$(SWITCH_VARS), \
|
||||
$(if $(filter y,$($(v))), \
|
||||
$(eval SDK_DEFINES += $(subst FEATURE_,-D,$(v)))) \
|
||||
)
|
||||
505
Products/example/living_platform/living_platform_cmds.c
Normal file
505
Products/example/living_platform/living_platform_cmds.c
Normal file
|
|
@ -0,0 +1,505 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2019 Alibaba Group Holding Limited
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <aos/aos.h>
|
||||
#include <aos/yloop.h>
|
||||
#include "netmgr.h"
|
||||
#include "iot_export.h"
|
||||
#include "iot_import.h"
|
||||
#include "aos/kv.h"
|
||||
|
||||
#include <k_api.h>
|
||||
|
||||
#include "living_platform_main.h"
|
||||
#include "app_entry.h"
|
||||
#include "living_platform_cmds.h"
|
||||
#include "living_platform_ut.h"
|
||||
|
||||
static char awss_running = 0;
|
||||
|
||||
#ifdef AWSS_BATCH_DEVAP_ENABLE
|
||||
#define DEV_AP_ZCONFIG_TIMEOUT_MS 120000 // (ms)
|
||||
extern void awss_set_config_press(uint8_t press);
|
||||
extern uint8_t awss_get_config_press(void);
|
||||
extern void zconfig_80211_frame_filter_set(uint8_t filter, uint8_t fix_channel);
|
||||
|
||||
static aos_timer_t dev_ap_zconfig_timeout_timer;
|
||||
static uint8_t g_dev_ap_zconfig_timer = 0; // this timer create once and can restart
|
||||
static uint8_t g_dev_ap_zconfig_run = 0;
|
||||
|
||||
static void timer_func_devap_zconfig_timeout(void *arg1, void *arg2)
|
||||
{
|
||||
living_platform_info("%s run\n", __func__);
|
||||
|
||||
if (awss_get_config_press())
|
||||
{
|
||||
// still in zero wifi provision stage, should stop and switch to dev ap
|
||||
living_platform_do_awss_dev_ap();
|
||||
}
|
||||
else
|
||||
{
|
||||
// zero wifi provision finished
|
||||
}
|
||||
|
||||
awss_set_config_press(0);
|
||||
zconfig_80211_frame_filter_set(0xFF, 0xFF);
|
||||
g_dev_ap_zconfig_run = 0;
|
||||
aos_timer_stop(&dev_ap_zconfig_timeout_timer);
|
||||
}
|
||||
|
||||
static void awss_dev_ap_switch_to_zeroconfig(void *p)
|
||||
{
|
||||
living_platform_info("%s run\n", __func__);
|
||||
// Stop dev ap wifi provision
|
||||
awss_dev_ap_stop();
|
||||
// Start and enable zero wifi provision
|
||||
iotx_event_regist_cb(linkkit_event_monitor);
|
||||
awss_set_config_press(1);
|
||||
|
||||
// Start timer to count duration time of zero provision timeout
|
||||
if (!g_dev_ap_zconfig_timer)
|
||||
{
|
||||
aos_timer_new(&dev_ap_zconfig_timeout_timer, timer_func_devap_zconfig_timeout, NULL, DEV_AP_ZCONFIG_TIMEOUT_MS, 0);
|
||||
g_dev_ap_zconfig_timer = 1;
|
||||
}
|
||||
aos_timer_start(&dev_ap_zconfig_timeout_timer);
|
||||
|
||||
// This will hold thread, when awss is going
|
||||
netmgr_start(true);
|
||||
|
||||
living_platform_info("%s exit\n", __func__);
|
||||
aos_task_exit(0);
|
||||
}
|
||||
|
||||
int awss_dev_ap_modeswitch_cb(uint8_t awss_new_mode, uint8_t new_mode_timeout, uint8_t fix_channel)
|
||||
{
|
||||
if ((awss_new_mode == 0) && !g_dev_ap_zconfig_run)
|
||||
{
|
||||
g_dev_ap_zconfig_run = 1;
|
||||
// Only receive zero provision packets
|
||||
zconfig_80211_frame_filter_set(0x00, fix_channel);
|
||||
living_platform_info("switch to awssmode %d, mode_timeout %d, chan %d\n", 0x00, new_mode_timeout, fix_channel);
|
||||
// switch to zero config
|
||||
aos_task_new("devap_to_zeroconfig", awss_dev_ap_switch_to_zeroconfig, NULL, 2048);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static void awss_close_dev_ap(void *p)
|
||||
{
|
||||
awss_dev_ap_stop();
|
||||
living_platform_info("%s exit\n", __func__);
|
||||
aos_task_exit(0);
|
||||
}
|
||||
|
||||
static void awss_open_dev_ap(void *p)
|
||||
{
|
||||
iotx_event_regist_cb(linkkit_event_monitor);
|
||||
living_platform_info("%s\n", __func__);
|
||||
if (netmgr_start(false) != 0)
|
||||
{
|
||||
aos_msleep(2000);
|
||||
#ifdef AWSS_BATCH_DEVAP_ENABLE
|
||||
awss_dev_ap_reg_modeswit_cb(awss_dev_ap_modeswitch_cb);
|
||||
#endif
|
||||
awss_dev_ap_start();
|
||||
}
|
||||
aos_task_exit(0);
|
||||
}
|
||||
|
||||
static void stop_netmgr(void *p)
|
||||
{
|
||||
awss_stop();
|
||||
living_platform_info("%s\n", __func__);
|
||||
aos_task_exit(0);
|
||||
}
|
||||
|
||||
void living_platform_start_netmgr(void *p)
|
||||
{
|
||||
iotx_event_regist_cb(linkkit_event_monitor);
|
||||
netmgr_start(true);
|
||||
aos_task_exit(0);
|
||||
}
|
||||
|
||||
void living_platform_do_awss_active(void)
|
||||
{
|
||||
living_platform_info("living_platform_do_awss_active %d\n", awss_running);
|
||||
awss_running = 1;
|
||||
#ifdef WIFI_PROVISION_ENABLED
|
||||
extern int awss_config_press();
|
||||
awss_config_press();
|
||||
#endif
|
||||
}
|
||||
|
||||
void living_platform_do_awss_dev_ap(void)
|
||||
{
|
||||
aos_task_new("netmgr_stop", stop_netmgr, NULL, 4096);
|
||||
aos_task_new("dap_open", awss_open_dev_ap, NULL, 4096);
|
||||
}
|
||||
|
||||
void living_platform_do_awss(void)
|
||||
{
|
||||
aos_task_new("dap_close", awss_close_dev_ap, NULL, 2048);
|
||||
aos_task_new("netmgr_start", living_platform_start_netmgr, NULL, 4096);
|
||||
}
|
||||
|
||||
static void linkkit_reset(void *p)
|
||||
{
|
||||
iotx_sdk_reset_local();
|
||||
HAL_Reboot();
|
||||
}
|
||||
|
||||
void living_platform_awss_reset(void)
|
||||
{
|
||||
#ifdef WIFI_PROVISION_ENABLED
|
||||
aos_task_new("reset", (void (*)(void *))iotx_sdk_reset, NULL, 4096); // stack taken by iTLS is more than taken by TLS.
|
||||
#endif
|
||||
aos_post_delayed_action(2000, linkkit_reset, NULL);
|
||||
}
|
||||
|
||||
#ifdef CONFIG_AOS_CLI
|
||||
static void handle_reset_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
aos_schedule_call(living_platform_awss_reset, NULL);
|
||||
}
|
||||
|
||||
static void handle_active_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
aos_schedule_call(living_platform_do_awss_active, NULL);
|
||||
}
|
||||
|
||||
static void handle_dev_ap_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
aos_schedule_call(living_platform_do_awss_dev_ap, NULL);
|
||||
}
|
||||
|
||||
static void handle_awss_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
aos_schedule_call(living_platform_do_awss, NULL);
|
||||
}
|
||||
|
||||
#if defined(OFFLINE_LOG_UT_TEST)
|
||||
static void handle_offline_log_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (argc < 2)
|
||||
{
|
||||
aos_cli_printf("params err,oll [save/upload/erase] or [read [0-1]] or [write [0-3] \"test offline log\"]\r\n");
|
||||
return;
|
||||
}
|
||||
|
||||
aos_cli_printf("cmd:%s\r\n", argv[1]);
|
||||
|
||||
if (!strcmp("save", argv[1]) && argc == 2)
|
||||
{
|
||||
diagnosis_offline_log_save_all();
|
||||
}
|
||||
else if (!strcmp("upload", argv[1]) && argc == 4)
|
||||
{
|
||||
int port = atoi(argv[3]);
|
||||
|
||||
diagnosis_offline_log_upload(argv[2], port);
|
||||
}
|
||||
else if (!strcmp("write", argv[1]) && argc == 4)
|
||||
{
|
||||
int loglevel = atoi(argv[2]);
|
||||
|
||||
if (loglevel >= 0 && loglevel <= 3)
|
||||
{
|
||||
diagnosis_offline_log(loglevel, "%s\\n", argv[3]);
|
||||
}
|
||||
}
|
||||
else if (!strcmp("read", argv[1]) && argc == 3)
|
||||
{
|
||||
int log_mode = atoi(argv[2]);
|
||||
|
||||
diagnosis_offline_log_read_all(log_mode);
|
||||
}
|
||||
else if (!strcmp("erase", argv[1]) && argc == 2)
|
||||
{
|
||||
diagnosis_offline_log_erase_flash_desc();
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("cmd:%s is not support\r\n", argv[1]);
|
||||
}
|
||||
|
||||
if (ret != 0)
|
||||
{
|
||||
aos_cli_printf("handle oll cmd:%s failed:%d\r\n", argv[1], ret);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static struct cli_command offline_log_cmd = {.name = "oll",
|
||||
.help = "oll [save]/[erase] or [upload host port] or [read [0-1]] or [write [0-3] \"test offline log\"]",
|
||||
.function = handle_offline_log_cmd};
|
||||
|
||||
#endif
|
||||
|
||||
static void handle_linkkey_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
int index = 0;
|
||||
char key_buf[MAX_KEY_LEN];
|
||||
|
||||
if (argc == 1)
|
||||
{
|
||||
int len = 0;
|
||||
char product_key[PRODUCT_KEY_LEN + 1] = {0};
|
||||
char product_secret[PRODUCT_SECRET_LEN + 1] = {0};
|
||||
char device_name[DEVICE_NAME_LEN + 1] = {0};
|
||||
char device_secret[DEVICE_SECRET_LEN + 1] = {0};
|
||||
|
||||
len = PRODUCT_KEY_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(product_key, 0, sizeof(product_key));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PK, index);
|
||||
aos_kv_get(key_buf, product_key, &len);
|
||||
|
||||
len = PRODUCT_SECRET_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(product_secret, 0, sizeof(product_secret));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PS, index);
|
||||
aos_kv_get(key_buf, product_secret, &len);
|
||||
|
||||
len = DEVICE_NAME_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(device_name, 0, sizeof(device_name));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DN, index);
|
||||
aos_kv_get(key_buf, device_name, &len);
|
||||
|
||||
len = DEVICE_SECRET_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(device_secret, 0, sizeof(device_secret));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DS, index);
|
||||
aos_kv_get(key_buf, device_secret, &len);
|
||||
|
||||
aos_cli_printf("PK=%s.\r\n", product_key);
|
||||
aos_cli_printf("PS=%s.\r\n", product_secret);
|
||||
aos_cli_printf("DN=%s.\r\n", device_name);
|
||||
aos_cli_printf("DS=%s.\r\n", device_secret);
|
||||
}
|
||||
else if (argc == 5)
|
||||
{
|
||||
int devid = 0;
|
||||
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PK, devid);
|
||||
aos_kv_set(key_buf, argv[1], strlen(argv[1]) + 1, 1);
|
||||
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DN, devid);
|
||||
aos_kv_set(key_buf, argv[2], strlen(argv[2]) + 1, 1);
|
||||
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DS, devid);
|
||||
aos_kv_set(key_buf, argv[3], strlen(argv[3]) + 1, 1);
|
||||
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PS, devid);
|
||||
aos_kv_set(key_buf, argv[4], strlen(argv[4]) + 1, 1);
|
||||
|
||||
aos_cli_printf("Done\r\n");
|
||||
}
|
||||
else if (argc == 6)
|
||||
{
|
||||
int devid = atoi(argv[1]);
|
||||
|
||||
if (devid >= 0 && devid <= MAX_DEVICES_META_NUM)
|
||||
{
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PK, devid);
|
||||
aos_kv_set(key_buf, argv[2], strlen(argv[2]) + 1, 1);
|
||||
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DN, devid);
|
||||
aos_kv_set(key_buf, argv[3], strlen(argv[3]) + 1, 1);
|
||||
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DS, devid);
|
||||
aos_kv_set(key_buf, argv[4], strlen(argv[4]) + 1, 1);
|
||||
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PS, devid);
|
||||
aos_kv_set(key_buf, argv[5], strlen(argv[5]) + 1, 1);
|
||||
|
||||
aos_cli_printf("Done\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("Err:devid range[0-%d]", MAX_DEVICES_META_NUM - 1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("Params Err\r\n");
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef DEV_ERRCODE_ENABLE
|
||||
#define DEV_DIAG_CLI_SET_ERRCODE "set_err"
|
||||
#define DEV_DIAG_CLI_ERRCODE_MANUAL_MSG "errcode was manually set"
|
||||
#define DEV_DIAG_CLI_GET_ERRCODE "get_err"
|
||||
#define DEV_DIAG_CLI_DEL_ERRCODE "del_err"
|
||||
|
||||
static void handle_dev_serv_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
uint16_t err_code = 0;
|
||||
char err_msg[DEV_ERRCODE_MSG_MAX_LEN] = {0};
|
||||
uint8_t ret = 0;
|
||||
if (argc == 1)
|
||||
{
|
||||
aos_cli_printf("device diagnosis service test, please input more parameters.\r\n");
|
||||
}
|
||||
else if (argc > 1)
|
||||
{
|
||||
if ((argc == 3) && (!strcmp(argv[1], DEV_DIAG_CLI_SET_ERRCODE)))
|
||||
{
|
||||
err_code = (uint16_t)strtoul(argv[2], NULL, 0);
|
||||
ret = dev_errcode_kv_set(err_code, DEV_DIAG_CLI_ERRCODE_MANUAL_MSG);
|
||||
if (ret == 0)
|
||||
{
|
||||
aos_cli_printf("manually set err_code=%d\r\n", err_code);
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("manually set err_code failed!\r\n");
|
||||
}
|
||||
}
|
||||
else if ((argc == 2) && (!strcmp(argv[1], DEV_DIAG_CLI_GET_ERRCODE)))
|
||||
{
|
||||
ret = dev_errcode_kv_get(&err_code, err_msg);
|
||||
if (ret == 0)
|
||||
{
|
||||
aos_cli_printf("get err_code=%d, err_msg=%s\r\n", err_code, err_msg);
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("get err_code fail or no err_code found!\r\n");
|
||||
}
|
||||
}
|
||||
else if ((argc == 2) && (!strcmp(argv[1], DEV_DIAG_CLI_DEL_ERRCODE)))
|
||||
{
|
||||
dev_errcode_kv_del();
|
||||
aos_cli_printf("del err_code\r\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("Error: %d\r\n", __LINE__);
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef MANUFACT_AP_FIND_ENABLE
|
||||
#define MANUAP_CLI_OPEN "open"
|
||||
#define MANUAP_CLI_CLOSE "close"
|
||||
|
||||
static void handle_manuap_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
int ret = 0;
|
||||
uint8_t manuap_flag = 0;
|
||||
int manu_flag_len = sizeof(manuap_flag);
|
||||
if (argc == 1)
|
||||
{
|
||||
ret = aos_kv_get(MANUAP_CONTROL_KEY, &manuap_flag, &manu_flag_len);
|
||||
if (ret == 0)
|
||||
{
|
||||
if (manuap_flag)
|
||||
{
|
||||
aos_cli_printf("manuap state is open\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("manuap state is close\r\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("no manuap state stored\r\n");
|
||||
}
|
||||
}
|
||||
else if (argc == 2)
|
||||
{
|
||||
if (!strcmp(argv[1], MANUAP_CLI_OPEN))
|
||||
{
|
||||
manuap_flag = 1;
|
||||
ret = aos_kv_set(MANUAP_CONTROL_KEY, &manuap_flag, sizeof(manuap_flag), 1);
|
||||
if (ret == 0)
|
||||
{
|
||||
aos_cli_printf("manuap opened\r\n");
|
||||
}
|
||||
}
|
||||
else if (!strcmp(argv[1], MANUAP_CLI_CLOSE))
|
||||
{
|
||||
manuap_flag = 0;
|
||||
ret = aos_kv_set(MANUAP_CONTROL_KEY, &manuap_flag, sizeof(manuap_flag), 1);
|
||||
if (ret == 0)
|
||||
{
|
||||
aos_cli_printf("manuap closed\r\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("param input err\r\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("param input err\r\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static struct cli_command resetcmd = {.name = "reset",
|
||||
.help = "factory reset",
|
||||
.function = handle_reset_cmd};
|
||||
|
||||
static struct cli_command awss_enable_cmd = {.name = "active_awss",
|
||||
.help = "active_awss [start]",
|
||||
.function = handle_active_cmd};
|
||||
static struct cli_command awss_dev_ap_cmd = {.name = "dev_ap",
|
||||
.help = "awss_dev_ap [start]",
|
||||
.function = handle_dev_ap_cmd};
|
||||
static struct cli_command awss_cmd = {.name = "awss",
|
||||
.help = "awss [start]",
|
||||
.function = handle_awss_cmd};
|
||||
static struct cli_command linkkeycmd = {.name = "linkkey",
|
||||
.help = "set/get linkkit keys. linkkey [devid] [<Product Key> <Device Name> <Device Secret> <Product Secret>]",
|
||||
.function = handle_linkkey_cmd};
|
||||
|
||||
#ifdef DEV_ERRCODE_ENABLE
|
||||
static struct cli_command dev_service_cmd = {.name = "dev_serv",
|
||||
.help = "device diagnosis service tests. like errcode, and log report",
|
||||
.function = handle_dev_serv_cmd};
|
||||
#endif
|
||||
#ifdef MANUFACT_AP_FIND_ENABLE
|
||||
static struct cli_command manuap_cmd = {.name = "manuap",
|
||||
.help = "manuap [open] or manuap [close]",
|
||||
.function = handle_manuap_cmd};
|
||||
#endif
|
||||
|
||||
int living_platform_register_cmds(void)
|
||||
{
|
||||
aos_cli_register_command(&resetcmd);
|
||||
aos_cli_register_command(&awss_enable_cmd);
|
||||
aos_cli_register_command(&awss_dev_ap_cmd);
|
||||
aos_cli_register_command(&awss_cmd);
|
||||
aos_cli_register_command(&linkkeycmd);
|
||||
#ifdef MANUFACT_AP_FIND_ENABLE
|
||||
aos_cli_register_command(&manuap_cmd);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
16
Products/example/living_platform/living_platform_cmds.h
Normal file
16
Products/example/living_platform/living_platform_cmds.h
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#ifndef __LIVING_PLATFORM_CMDS_H__
|
||||
#define __LIVING_PLATFORM_CMDS_H__
|
||||
|
||||
#define MAX_DEVICES_META_NUM (0)
|
||||
|
||||
#ifdef MANUFACT_AP_FIND_ENABLE
|
||||
#define MANUAP_CONTROL_KEY "manuap"
|
||||
#endif
|
||||
|
||||
extern int living_platform_register_cmds(void);
|
||||
extern void living_platform_start_netmgr(void *p);
|
||||
extern void living_platform_do_awss_dev_ap(void);
|
||||
extern void living_platform_awss_reset(void);
|
||||
extern void living_platform_do_awss_active(void);
|
||||
extern void living_platform_do_awss(void);
|
||||
#endif
|
||||
852
Products/example/living_platform/living_platform_main.c
Executable file
852
Products/example/living_platform/living_platform_main.c
Executable file
|
|
@ -0,0 +1,852 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2019 Alibaba Group Holding Limited
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "cJSON.h"
|
||||
#include "iot_import.h"
|
||||
#include "iot_export_linkkit.h"
|
||||
|
||||
#include "iotx_log.h"
|
||||
|
||||
#include "living_platform_main.h"
|
||||
#include "app_entry.h"
|
||||
#include "living_platform_ut.h"
|
||||
|
||||
#define PRODUCT_KEY "PK_XXXXXX"
|
||||
#define PRODUCT_SECRET "PS_XXXXXX"
|
||||
#define DEVICE_NAME "DN_XXXXXX"
|
||||
#define DEVICE_SECRET "DS_XXXXXX"
|
||||
|
||||
static living_platform_ctx_t g_living_platform_ctx;
|
||||
|
||||
living_platform_ctx_t *living_platform_get_ctx(void)
|
||||
{
|
||||
return &g_living_platform_ctx;
|
||||
}
|
||||
|
||||
//Have to put this function at here for FeiYan CT
|
||||
//User have to change their own way to burn device mate info
|
||||
void living_platform_load_device_meta_info(void)
|
||||
{
|
||||
int len = 0;
|
||||
char key_buf[MAX_KEY_LEN];
|
||||
char product_key[PRODUCT_KEY_LEN + 1] = {0};
|
||||
char product_secret[PRODUCT_SECRET_LEN + 1] = {0};
|
||||
char device_name[DEVICE_NAME_LEN + 1] = {0};
|
||||
char device_secret[DEVICE_SECRET_LEN + 1] = {0};
|
||||
|
||||
len = PRODUCT_KEY_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(product_key, 0, sizeof(product_key));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PK, 0);
|
||||
HAL_Kv_Get(key_buf, product_key, &len);
|
||||
|
||||
len = PRODUCT_SECRET_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(product_secret, 0, sizeof(product_secret));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PS, 0);
|
||||
HAL_Kv_Get(key_buf, product_secret, &len);
|
||||
|
||||
len = DEVICE_NAME_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(device_name, 0, sizeof(device_name));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DN, 0);
|
||||
HAL_Kv_Get(key_buf, device_name, &len);
|
||||
|
||||
len = DEVICE_SECRET_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(device_secret, 0, sizeof(device_secret));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DS, 0);
|
||||
HAL_Kv_Get(key_buf, device_secret, &len);
|
||||
|
||||
if ((strlen(product_key) > 0) && (strlen(product_secret) > 0) && (strlen(device_name) > 0))
|
||||
{
|
||||
HAL_SetProductKey(product_key);
|
||||
HAL_SetProductSecret(product_secret);
|
||||
HAL_SetDeviceName(device_name);
|
||||
HAL_SetDeviceSecret(device_secret);
|
||||
printf("pk[%s]\r\n", product_key);
|
||||
printf("dn[%s]\r\n", device_name);
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_SetProductKey(PRODUCT_KEY);
|
||||
HAL_SetProductSecret(PRODUCT_SECRET);
|
||||
HAL_SetDeviceName(DEVICE_NAME);
|
||||
HAL_SetDeviceSecret(DEVICE_SECRET);
|
||||
printf("pk[%s]\r\n", PRODUCT_KEY);
|
||||
printf("dn[%s]\r\n", DEVICE_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
static int user_connected_event_handler(void)
|
||||
{
|
||||
living_platform_ctx_t *living_platform_ctx = living_platform_get_ctx();
|
||||
|
||||
living_platform_info("Cloud Connected");
|
||||
|
||||
living_platform_ctx->cloud_connected = 1;
|
||||
|
||||
if (living_platform_ctx->master_initialized == 1) //initialized
|
||||
{
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_disconnected_event_handler(void)
|
||||
{
|
||||
living_platform_ctx_t *living_platform_ctx = living_platform_get_ctx();
|
||||
|
||||
living_platform_info("Cloud Disconnected");
|
||||
|
||||
living_platform_ctx->cloud_connected = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_down_raw_data_arrived_event_handler(const int devid, const unsigned char *payload,
|
||||
const int payload_len)
|
||||
{
|
||||
living_platform_info("Down Raw Message, Devid: %d, Payload Length: %d", devid, payload_len);
|
||||
if (payload[0] == 0x02 && payload_len == 6)
|
||||
{
|
||||
living_platform_ut_set_LightSwitch(payload[5]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_service_request_event_handler(const int devid, const char *serviceid, const int serviceid_len,
|
||||
const char *request, const int request_len,
|
||||
char **response, int *response_len)
|
||||
{
|
||||
int transparency = 0;
|
||||
cJSON *root = NULL, *item_transparency = NULL;
|
||||
|
||||
living_platform_info("Service Request Received, Devid: %d, Service ID: %.*s, Payload: %s", devid, serviceid_len,
|
||||
serviceid,
|
||||
request);
|
||||
|
||||
/* Parse Root */
|
||||
root = cJSON_Parse(request);
|
||||
if (root == NULL || !cJSON_IsObject(root))
|
||||
{
|
||||
living_platform_err("JSON Parse Error");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (strlen("Custom") == serviceid_len && memcmp("Custom", serviceid, serviceid_len) == 0)
|
||||
{
|
||||
/* Parse Item */
|
||||
const char *response_fmt = "{\"transparency\":%d}";
|
||||
item_transparency = cJSON_GetObjectItem(root, "transparency");
|
||||
if (item_transparency == NULL || !cJSON_IsNumber(item_transparency))
|
||||
{
|
||||
cJSON_Delete(root);
|
||||
return -1;
|
||||
}
|
||||
living_platform_info("transparency: %d", item_transparency->valueint);
|
||||
transparency = item_transparency->valueint + 1;
|
||||
if (transparency > 100)
|
||||
{
|
||||
transparency = 100;
|
||||
}
|
||||
|
||||
/* Send Service Response To Cloud */
|
||||
*response_len = strlen(response_fmt) + 10 + 1;
|
||||
*response = (char *)HAL_Malloc(*response_len);
|
||||
if (*response == NULL)
|
||||
{
|
||||
living_platform_err("Memory Not Enough");
|
||||
return -1;
|
||||
}
|
||||
memset(*response, 0, *response_len);
|
||||
HAL_Snprintf(*response, *response_len, response_fmt, transparency);
|
||||
*response_len = strlen(*response);
|
||||
}
|
||||
else if (strlen("ToggleLightSwitch") == serviceid_len && memcmp("ToggleLightSwitch", serviceid, serviceid_len) == 0)
|
||||
{
|
||||
/* Parse Item */
|
||||
const char *response_fmt = "{\"LightSwitch\":%d}";
|
||||
|
||||
/* Send Service Response To Cloud */
|
||||
*response_len = strlen(response_fmt) + strlen("LightSwitch") + 10;
|
||||
*response = (char *)HAL_Malloc(*response_len);
|
||||
if (*response == NULL)
|
||||
{
|
||||
living_platform_err("Memory Not Enough");
|
||||
return -1;
|
||||
}
|
||||
memset(*response, 0, *response_len);
|
||||
if (living_platform_ut_get_LightSwitch() == 0)
|
||||
{
|
||||
living_platform_ut_set_LightSwitch(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
living_platform_ut_set_LightSwitch(0);
|
||||
}
|
||||
|
||||
HAL_Snprintf(*response, *response_len, response_fmt, living_platform_ut_get_LightSwitch());
|
||||
*response_len = strlen(*response);
|
||||
}
|
||||
|
||||
cJSON_Delete(root);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef ALCS_ENABLED
|
||||
//Just for reference,user have to change his owner properties
|
||||
static int user_property_get_event_handler(const int devid, const char *request, const int request_len, char **response,
|
||||
int *response_len)
|
||||
{
|
||||
int index = 0;
|
||||
living_platform_tsl_t *p_living_platform_tsl_data = living_platform_ut_get_tsl_data();
|
||||
cJSON *response_root = NULL;
|
||||
cJSON *request_root = NULL, *item_propertyid = NULL;
|
||||
living_platform_info("Property Get Received, Devid: %d, Request: %s", devid, request);
|
||||
|
||||
/* Parse Request */
|
||||
request_root = cJSON_Parse(request);
|
||||
if (request_root == NULL || !cJSON_IsArray(request_root))
|
||||
{
|
||||
living_platform_info("JSON Parse Error");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Prepare Response */
|
||||
response_root = cJSON_CreateObject();
|
||||
if (response_root == NULL)
|
||||
{
|
||||
living_platform_info("No Enough Memory");
|
||||
cJSON_Delete(request_root);
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (index = 0; index < cJSON_GetArraySize(request_root); index++)
|
||||
{
|
||||
item_propertyid = cJSON_GetArrayItem(request_root, index);
|
||||
if (item_propertyid == NULL || !cJSON_IsString(item_propertyid))
|
||||
{
|
||||
living_platform_info("JSON Parse Error");
|
||||
cJSON_Delete(request_root);
|
||||
cJSON_Delete(response_root);
|
||||
return -1;
|
||||
}
|
||||
|
||||
living_platform_info("Property ID, index: %d, Value: %s", index, item_propertyid->valuestring);
|
||||
if (strcmp("WIFI_Band", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddStringToObject(response_root, "WIFI_Band", p_living_platform_tsl_data->wifi.band);
|
||||
}
|
||||
else if (strcmp("WIFI_AP_BSSID", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddStringToObject(response_root, "WIFI_AP_BSSID", p_living_platform_tsl_data->wifi.bssid);
|
||||
}
|
||||
else if (strcmp("WIFI_Channel", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddNumberToObject(response_root, "WIFI_Channel", p_living_platform_tsl_data->wifi.Channel);
|
||||
}
|
||||
else if (strcmp("WiFI_SNR", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddNumberToObject(response_root, "WiFI_SNR", p_living_platform_tsl_data->wifi.SNR);
|
||||
}
|
||||
else if (strcmp("WiFI_RSSI", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddNumberToObject(response_root, "WiFI_RSSI", p_living_platform_tsl_data->wifi.rssi);
|
||||
}
|
||||
else if (strcmp("LightSwitch", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddBoolToObject(response_root, "LightSwitch", p_living_platform_tsl_data->LightSwitch);
|
||||
}
|
||||
else if (strcmp("NightLightSwitch", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddBoolToObject(response_root, "NightLightSwitch", p_living_platform_tsl_data->NightLightSwitch);
|
||||
}
|
||||
else if (strcmp("WorkMode", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddNumberToObject(response_root, "WorkMode", p_living_platform_tsl_data->WorkMode);
|
||||
}
|
||||
else if (strcmp("worktime", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddStringToObject(response_root, "worktime", p_living_platform_tsl_data->WorkTime);
|
||||
}
|
||||
else if (strcmp("Brightness", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddNumberToObject(response_root, "Brightness", p_living_platform_tsl_data->Brightness);
|
||||
}
|
||||
else if (strcmp("onlyread", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddNumberToObject(response_root, "onlyread", p_living_platform_tsl_data->readonly);
|
||||
}
|
||||
else if (strcmp("floatid", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddNumberToObject(response_root, "floatid", p_living_platform_tsl_data->f);
|
||||
}
|
||||
else if (strcmp("doubleid", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddNumberToObject(response_root, "doubleid", p_living_platform_tsl_data->d);
|
||||
}
|
||||
else if (strcmp("PropertyString", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON_AddStringToObject(response_root, "PropertyString", p_living_platform_tsl_data->PropertyString);
|
||||
}
|
||||
else if (strcmp("RGBColor", item_propertyid->valuestring) == 0)
|
||||
{
|
||||
cJSON *item_RGBColor = cJSON_CreateObject();
|
||||
if (item_RGBColor == NULL)
|
||||
{
|
||||
cJSON_Delete(request_root);
|
||||
cJSON_Delete(response_root);
|
||||
|
||||
return -1;
|
||||
}
|
||||
cJSON_AddNumberToObject(item_RGBColor, "Red", p_living_platform_tsl_data->RGB.R);
|
||||
cJSON_AddNumberToObject(item_RGBColor, "Green", p_living_platform_tsl_data->RGB.G);
|
||||
cJSON_AddNumberToObject(item_RGBColor, "Blue", p_living_platform_tsl_data->RGB.B);
|
||||
|
||||
cJSON_AddItemToObject(response_root, "RGBColor", item_RGBColor);
|
||||
}
|
||||
}
|
||||
cJSON_Delete(request_root);
|
||||
|
||||
*response = cJSON_PrintUnformatted(response_root);
|
||||
if (*response == NULL)
|
||||
{
|
||||
living_platform_info("No Enough Memory");
|
||||
cJSON_Delete(response_root);
|
||||
return -1;
|
||||
}
|
||||
cJSON_Delete(response_root);
|
||||
*response_len = strlen(*response);
|
||||
|
||||
living_platform_info("Property Get Response: %s", *response);
|
||||
|
||||
return SUCCESS_RETURN;
|
||||
}
|
||||
#endif
|
||||
|
||||
//When code is not 200,maybe call this function
|
||||
static int user_property_cloud_error_handler(const int code, const char *data, const char *detail)
|
||||
{
|
||||
living_platform_info("code =%d ,data=%s, detail=%s", code, data, detail);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 解析所有属性设置的值
|
||||
* @param request 指向属性设置请求payload的指针
|
||||
* @param request_len 属性设置请求的payload长度
|
||||
* @return 解析成功: 0, 解析失败: <0
|
||||
*/
|
||||
int32_t app_parse_property(const char *request, uint32_t request_len)
|
||||
{
|
||||
cJSON *lightswitch = NULL;
|
||||
cJSON *rgbcolor = NULL;
|
||||
cJSON *nightlightswitch = NULL;
|
||||
cJSON *workmode = NULL;
|
||||
cJSON *brightness = NULL;
|
||||
cJSON *worktime = NULL;
|
||||
cJSON *floatid = NULL;
|
||||
cJSON *doubleid = NULL;
|
||||
cJSON *propertystring = NULL;
|
||||
|
||||
cJSON *req = cJSON_Parse(request);
|
||||
if (req == NULL || !cJSON_IsObject(req))
|
||||
{
|
||||
return -0x911;
|
||||
}
|
||||
|
||||
lightswitch = cJSON_GetObjectItem(req, "LightSwitch");
|
||||
if (lightswitch != NULL && cJSON_IsNumber(lightswitch))
|
||||
{
|
||||
/* process property LightSwitch here */
|
||||
|
||||
living_platform_info("property id: LightSwitch, value: %d", lightswitch->valueint);
|
||||
living_platform_ut_set_LightSwitch(lightswitch->valueint);
|
||||
}
|
||||
|
||||
rgbcolor = cJSON_GetObjectItem(req, "RGBColor");
|
||||
if (rgbcolor != NULL && cJSON_IsObject(rgbcolor))
|
||||
{
|
||||
/* process property RGBColor here */
|
||||
cJSON *R = cJSON_GetObjectItem(rgbcolor, "Red");
|
||||
cJSON *G = cJSON_GetObjectItem(rgbcolor, "Green");
|
||||
cJSON *B = cJSON_GetObjectItem(rgbcolor, "Blue");
|
||||
|
||||
if ((R != NULL && cJSON_IsNumber(R)) &&
|
||||
(G != NULL && cJSON_IsNumber(G)) &&
|
||||
(B != NULL && cJSON_IsNumber(B)))
|
||||
{
|
||||
living_platform_info("struct property id: RGBColor R:%d G:%d B:%d", R->valueint, G->valueint, B->valueint);
|
||||
living_platform_ut_set_RGB(R->valueint, G->valueint, B->valueint);
|
||||
}
|
||||
}
|
||||
|
||||
nightlightswitch = cJSON_GetObjectItem(req, "NightLightSwitch");
|
||||
if (nightlightswitch != NULL && cJSON_IsNumber(nightlightswitch))
|
||||
{
|
||||
/* process property NightLightSwitch here */
|
||||
|
||||
living_platform_info("property id: NightLightSwitch, value: %d", nightlightswitch->valueint);
|
||||
living_platform_ut_set_NightLightSwitch(nightlightswitch->valueint);
|
||||
}
|
||||
|
||||
workmode = cJSON_GetObjectItem(req, "WorkMode");
|
||||
if (workmode != NULL && cJSON_IsNumber(workmode))
|
||||
{
|
||||
/* process property WorkMode here */
|
||||
|
||||
living_platform_info("property id: WorkMode, value: %d", workmode->valueint);
|
||||
living_platform_ut_set_WorkMode(workmode->valueint);
|
||||
}
|
||||
|
||||
brightness = cJSON_GetObjectItem(req, "Brightness");
|
||||
if (brightness != NULL && cJSON_IsNumber(brightness))
|
||||
{
|
||||
/* process property Brightness here */
|
||||
|
||||
living_platform_info("property id: Brightness, value: %d", brightness->valueint);
|
||||
living_platform_ut_set_Brightness(brightness->valueint);
|
||||
}
|
||||
|
||||
worktime = cJSON_GetObjectItem(req, "worktime");
|
||||
if (worktime != NULL && cJSON_IsString(worktime))
|
||||
{
|
||||
/* process property worktime here */
|
||||
|
||||
living_platform_info("property id: worktime, value: %s", worktime->valuestring);
|
||||
living_platform_ut_set_WorkTime(worktime->valuestring);
|
||||
}
|
||||
|
||||
floatid = cJSON_GetObjectItem(req, "floatid");
|
||||
if (floatid != NULL && cJSON_IsNumber(floatid))
|
||||
{
|
||||
/* process property float here */
|
||||
|
||||
living_platform_info("property id: float, value: %f", floatid->valuedouble);
|
||||
living_platform_ut_set_Float(floatid->valuedouble);
|
||||
}
|
||||
|
||||
doubleid = cJSON_GetObjectItem(req, "doubleid");
|
||||
if (doubleid != NULL && cJSON_IsNumber(doubleid))
|
||||
{
|
||||
/* process property double here */
|
||||
|
||||
living_platform_info("property id: double, value: %f", doubleid->valuedouble);
|
||||
living_platform_ut_set_Double(doubleid->valuedouble);
|
||||
}
|
||||
|
||||
propertystring = cJSON_GetObjectItem(req, "PropertyString");
|
||||
if (propertystring != NULL && cJSON_IsString(propertystring))
|
||||
{
|
||||
/* process property PropertyString here */
|
||||
|
||||
living_platform_info("property id: PropertyString, value: %s", propertystring->valuestring);
|
||||
living_platform_ut_set_PropertyString(propertystring->valuestring);
|
||||
}
|
||||
|
||||
cJSON_Delete(req);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_property_set_event_handler(const int devid, const char *request, const int request_len)
|
||||
{
|
||||
int res = 0;
|
||||
living_platform_ctx_t *living_platform_ctx = living_platform_get_ctx();
|
||||
living_platform_info("Property Set Received, Devid: %d, Request: %s", devid, request);
|
||||
|
||||
app_parse_property(request, request_len);
|
||||
res = IOT_Linkkit_Report(living_platform_ctx->master_devid, ITM_MSG_POST_PROPERTY,
|
||||
(unsigned char *)request, request_len);
|
||||
|
||||
living_platform_info("Post Property Message ID: %d", res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_report_reply_event_handler(const int devid, const int msgid, const int code, const char *reply,
|
||||
const int reply_len)
|
||||
{
|
||||
const char *reply_value = (reply == NULL) ? ("NULL") : (reply);
|
||||
const int reply_value_len = (reply_len == 0) ? (strlen("NULL")) : (reply_len);
|
||||
|
||||
living_platform_info("Message Post Reply Received, Devid: %d, Message ID: %d, Code: %d, Reply: %.*s", devid, msgid, code,
|
||||
reply_value_len,
|
||||
reply_value);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_trigger_event_reply_event_handler(const int devid, const int msgid, const int code, const char *eventid,
|
||||
const int eventid_len, const char *message, const int message_len)
|
||||
{
|
||||
living_platform_info("Trigger Event Reply Received, Devid: %d, Message ID: %d, Code: %d, EventID: %.*s, Message: %.*s", devid,
|
||||
msgid, code,
|
||||
eventid_len,
|
||||
eventid, message_len, message);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_timestamp_reply_event_handler(const char *timestamp)
|
||||
{
|
||||
living_platform_info("Current Timestamp: %s", timestamp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint64_t user_update_sec(void)
|
||||
{
|
||||
static uint64_t time_start_ms = 0;
|
||||
|
||||
if (time_start_ms == 0)
|
||||
{
|
||||
time_start_ms = HAL_UptimeMs();
|
||||
}
|
||||
|
||||
return (HAL_UptimeMs() - time_start_ms) / 1000;
|
||||
}
|
||||
|
||||
static int living_platform_ut_query_timestamp(void)
|
||||
{
|
||||
living_platform_ctx_t *living_platform_ctx = living_platform_get_ctx();
|
||||
living_platform_info("do query timestamp");
|
||||
|
||||
IOT_Linkkit_Query(living_platform_ctx->master_devid, ITM_MSG_QUERY_TIMESTAMP, NULL, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_initialized(const int devid)
|
||||
{
|
||||
living_platform_ctx_t *living_platform_ctx = living_platform_get_ctx();
|
||||
living_platform_info("Device Initialized, Devid: %d", devid);
|
||||
|
||||
if (living_platform_ctx->master_devid == devid)
|
||||
{
|
||||
living_platform_ctx->master_initialized = 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_master_dev_available(void)
|
||||
{
|
||||
living_platform_ctx_t *living_platform_ctx = living_platform_get_ctx();
|
||||
|
||||
if (living_platform_ctx->cloud_connected && living_platform_ctx->master_initialized)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int living_platform_notify_msg_handle(int devid, const char *request, const int request_len)
|
||||
{
|
||||
int ret = 0;
|
||||
cJSON *request_root = NULL;
|
||||
cJSON *item = NULL;
|
||||
|
||||
request_root = cJSON_Parse(request);
|
||||
if (request_root == NULL)
|
||||
{
|
||||
living_platform_info("JSON Parse Error");
|
||||
return -1;
|
||||
}
|
||||
|
||||
item = cJSON_GetObjectItem(request_root, "identifier");
|
||||
if (item == NULL || !cJSON_IsString(item))
|
||||
{
|
||||
cJSON_Delete(request_root);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!strcmp(item->valuestring, "awss.BindNotify"))
|
||||
{
|
||||
cJSON *value = cJSON_GetObjectItem(request_root, "value");
|
||||
if (value == NULL || !cJSON_IsObject(value))
|
||||
{
|
||||
cJSON_Delete(request_root);
|
||||
return -1;
|
||||
}
|
||||
cJSON *op = cJSON_GetObjectItem(value, "Operation");
|
||||
if (op != NULL && cJSON_IsString(op))
|
||||
{
|
||||
if (!strcmp(op->valuestring, "Bind"))
|
||||
{
|
||||
living_platform_info("Device Bind");
|
||||
//TODO:User can do something according thier own requirements
|
||||
}
|
||||
else if (!strcmp(op->valuestring, "Unbind"))
|
||||
{
|
||||
living_platform_info("Device unBind");
|
||||
//TODO:User can do something according thier own requirements
|
||||
}
|
||||
else if (!strcmp(op->valuestring, "Reset"))
|
||||
{
|
||||
living_platform_info("Device reset");
|
||||
//TODO:User can do something according thier own requirements
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cJSON_Delete(request_root);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_event_notify_handler(const int devid, const char *request, const int request_len)
|
||||
{
|
||||
int res = 0;
|
||||
living_platform_ctx_t *living_platform_ctx = living_platform_get_ctx();
|
||||
living_platform_info("Event notify Received, Devid: %d, Request: %s", devid, request);
|
||||
|
||||
living_platform_notify_msg_handle(devid, request, request_len);
|
||||
res = IOT_Linkkit_Report(living_platform_ctx->master_devid, ITM_MSG_EVENT_NOTIFY_REPLY,
|
||||
(unsigned char *)request, request_len);
|
||||
living_platform_info("Post Property Message ID: %d", res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_fota_event_handler(int type, const char *version)
|
||||
{
|
||||
char *p_fota_buffer = NULL;
|
||||
living_platform_ctx_t *living_platform_ctx = living_platform_get_ctx();
|
||||
|
||||
p_fota_buffer = HAL_Malloc(LIVING_PLATFORM_OTA_BUFFER_LEN);
|
||||
if (!p_fota_buffer)
|
||||
{
|
||||
living_platform_err("no mem");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (type == 0)
|
||||
{
|
||||
living_platform_info("New Firmware Version: %s", version);
|
||||
memset(p_fota_buffer, 0, LIVING_PLATFORM_OTA_BUFFER_LEN);
|
||||
IOT_Linkkit_Query(living_platform_ctx->master_devid, ITM_MSG_QUERY_FOTA_DATA, (unsigned char *)p_fota_buffer, LIVING_PLATFORM_OTA_BUFFER_LEN);
|
||||
}
|
||||
|
||||
if (p_fota_buffer)
|
||||
HAL_Free(p_fota_buffer);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_cota_event_handler(int type, const char *config_id, int config_size, const char *get_type,
|
||||
const char *sign, const char *sign_method, const char *url)
|
||||
{
|
||||
char *p_cota_buffer = NULL;
|
||||
living_platform_ctx_t *living_platform_ctx = living_platform_get_ctx();
|
||||
|
||||
p_cota_buffer = HAL_Malloc(LIVING_PLATFORM_OTA_BUFFER_LEN);
|
||||
if (!p_cota_buffer)
|
||||
{
|
||||
living_platform_err("no mem");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (type == 0)
|
||||
{
|
||||
living_platform_info("New Config ID: %s", config_id);
|
||||
living_platform_info("New Config Size: %d", config_size);
|
||||
living_platform_info("New Config Type: %s", get_type);
|
||||
living_platform_info("New Config Sign: %s", sign);
|
||||
living_platform_info("New Config Sign Method: %s", sign_method);
|
||||
living_platform_info("New Config URL: %s", url);
|
||||
|
||||
IOT_Linkkit_Query(living_platform_ctx->master_devid, ITM_MSG_QUERY_COTA_DATA, (unsigned char *)p_cota_buffer, LIVING_PLATFORM_OTA_BUFFER_LEN);
|
||||
}
|
||||
|
||||
if (p_cota_buffer)
|
||||
HAL_Free(p_cota_buffer);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_offline_reset_handler(void)
|
||||
{
|
||||
living_platform_info("user callback user_offline_reset_handler called.");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_dev_bind_event(const int state_code, const char *state_message)
|
||||
{
|
||||
living_platform_info("state_code: -0x%04x, str_msg= %s", -state_code, state_message == NULL ? "NULL" : state_message);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef DM_UNIFIED_SERVICE_POST
|
||||
static int user_unified_service_post_reply_handler(const int devid, const int id, const int code, const char *payload, const int payload_len)
|
||||
{
|
||||
living_platform_info("Receive unified service post reply, code:%d, payload:%s", code, payload);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static iotx_linkkit_dev_meta_info_t *living_platform_main_init(living_platform_ctx_t *living_platform_ctx)
|
||||
{
|
||||
int register_type = 0;
|
||||
int post_event_reply = 1;
|
||||
iotx_linkkit_dev_meta_info_t *p_master_meta = NULL;
|
||||
|
||||
memset(living_platform_ctx, 0, sizeof(living_platform_ctx_t));
|
||||
|
||||
p_master_meta = HAL_Malloc(sizeof(iotx_linkkit_dev_meta_info_t));
|
||||
if (p_master_meta == NULL)
|
||||
{
|
||||
living_platform_err("no mem");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(p_master_meta, 0, sizeof(iotx_linkkit_dev_meta_info_t));
|
||||
HAL_GetProductKey(p_master_meta->product_key);
|
||||
HAL_GetDeviceName(p_master_meta->device_name);
|
||||
HAL_GetDeviceSecret(p_master_meta->device_secret);
|
||||
HAL_GetProductSecret(p_master_meta->product_secret);
|
||||
|
||||
if ((0 == strlen(p_master_meta->product_key)) || (0 == strlen(p_master_meta->device_name)) ||
|
||||
(0 == register_type && (0 == strlen(p_master_meta->device_secret))) || (0 == strlen(p_master_meta->product_secret)))
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
printf("Master meta info is invalid...\r\n");
|
||||
printf("pk[%s]\r\n", p_master_meta->product_key);
|
||||
printf("ps[%s]\r\n", p_master_meta->product_secret);
|
||||
printf("dn[%s]\r\n", p_master_meta->device_name);
|
||||
printf("ds[%s]\r\n", p_master_meta->device_secret);
|
||||
HAL_SleepMs(2000);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef LIVING_PLATFORM_PRODUCT_DYNAMIC_REGISTER
|
||||
register_type = 1;
|
||||
#endif
|
||||
|
||||
/* Register Callback */
|
||||
IOT_RegisterCallback(ITE_INITIALIZE_COMPLETED, user_initialized);
|
||||
IOT_RegisterCallback(ITE_CONNECT_SUCC, user_connected_event_handler);
|
||||
IOT_RegisterCallback(ITE_DISCONNECTED, user_disconnected_event_handler);
|
||||
IOT_RegisterCallback(ITE_RAWDATA_ARRIVED, user_down_raw_data_arrived_event_handler);
|
||||
IOT_RegisterCallback(ITE_SERVICE_REQUEST, user_service_request_event_handler);
|
||||
IOT_RegisterCallback(ITE_PROPERTY_SET, user_property_set_event_handler);
|
||||
#ifdef ALCS_ENABLED
|
||||
/*Only for local communication service(ALCS)*/
|
||||
IOT_RegisterCallback(ITE_PROPERTY_GET, user_property_get_event_handler);
|
||||
#endif
|
||||
IOT_RegisterCallback(ITE_REPORT_REPLY, user_report_reply_event_handler);
|
||||
IOT_RegisterCallback(ITE_TRIGGER_EVENT_REPLY, user_trigger_event_reply_event_handler);
|
||||
IOT_RegisterCallback(ITE_TIMESTAMP_REPLY, user_timestamp_reply_event_handler);
|
||||
|
||||
IOT_RegisterCallback(ITE_CLOUD_ERROR, user_property_cloud_error_handler);
|
||||
|
||||
IOT_RegisterCallback(ITE_FOTA, user_fota_event_handler);
|
||||
IOT_RegisterCallback(ITE_COTA, user_cota_event_handler);
|
||||
|
||||
IOT_RegisterCallback(ITE_EVENT_NOTIFY, user_event_notify_handler);
|
||||
IOT_RegisterCallback(ITE_STATE_DEV_BIND, user_dev_bind_event);
|
||||
|
||||
IOT_RegisterCallback(ITE_OFFLINE_RESET, user_offline_reset_handler);
|
||||
|
||||
#ifdef DM_UNIFIED_SERVICE_POST
|
||||
IOT_RegisterCallback(ITE_UNIFIED_SERVICE_POST, user_unified_service_post_reply_handler);
|
||||
#endif
|
||||
|
||||
IOT_Ioctl(IOTX_IOCTL_SET_DYNAMIC_REGISTER, (void *)®ister_type);
|
||||
|
||||
/* Choose Whether You Need Post Property/Event Reply */
|
||||
IOT_Ioctl(IOTX_IOCTL_RECV_EVENT_REPLY, (void *)&post_event_reply);
|
||||
|
||||
#ifdef LIVING_PLATFORM_USE_UT_FOR_TESTING
|
||||
living_platform_ut_init();
|
||||
#endif
|
||||
|
||||
return p_master_meta;
|
||||
}
|
||||
|
||||
int living_platform_main(void *paras)
|
||||
{
|
||||
int res = 0;
|
||||
uint64_t time_prev_sec = 0, time_now_sec = 0;
|
||||
living_platform_ctx_t *living_platform_ctx = living_platform_get_ctx();
|
||||
iotx_linkkit_dev_meta_info_t *p_master_meta = NULL;
|
||||
|
||||
p_master_meta = living_platform_main_init(living_platform_ctx);
|
||||
if (NULL == p_master_meta)
|
||||
{
|
||||
printf("OOPS:living_platform_main_init failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Create Master Device Resources */
|
||||
do
|
||||
{
|
||||
living_platform_ctx->master_devid = IOT_Linkkit_Open(IOTX_LINKKIT_DEV_TYPE_MASTER, p_master_meta);
|
||||
if (living_platform_ctx->master_devid < 0)
|
||||
{
|
||||
printf("IOT_Linkkit_Open Failed, retry after 5s...\r\n");
|
||||
HAL_SleepMs(5000);
|
||||
}
|
||||
} while (living_platform_ctx->master_devid < 0);
|
||||
/* Start Connect Aliyun Server */
|
||||
do
|
||||
{
|
||||
res = IOT_Linkkit_Connect(living_platform_ctx->master_devid);
|
||||
if (res < 0)
|
||||
{
|
||||
printf("IOT_Linkkit_Connect Failed, retry after 5s...\r\n");
|
||||
HAL_SleepMs(5000);
|
||||
}
|
||||
} while (res < 0);
|
||||
|
||||
living_platform_ut_query_timestamp();
|
||||
|
||||
while (1)
|
||||
{
|
||||
IOT_Linkkit_Yield(LIVING_PLATFORM_YIELD_TIMEOUT_MS);
|
||||
|
||||
time_now_sec = user_update_sec();
|
||||
if (time_prev_sec == time_now_sec)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (user_master_dev_available())
|
||||
{
|
||||
#ifdef LIVING_PLATFORM_USE_UT_FOR_TESTING
|
||||
living_platform_ut_misc_process(time_now_sec);
|
||||
#endif
|
||||
/* Device Info Update For feiyan CT */
|
||||
/* if (time_now_sec % 11 == 0)
|
||||
{
|
||||
living_platform_deviceinfo_update();
|
||||
} */
|
||||
}
|
||||
|
||||
time_prev_sec = time_now_sec;
|
||||
}
|
||||
|
||||
//Should never come here
|
||||
living_platform_ctx->g_user_dispatch_thread_running = 0;
|
||||
|
||||
if (p_master_meta)
|
||||
{
|
||||
HAL_Free(p_master_meta);
|
||||
}
|
||||
|
||||
IOT_DumpMemoryStats(IOT_LOG_DEBUG);
|
||||
|
||||
return 0;
|
||||
}
|
||||
34
Products/example/living_platform/living_platform_main.h
Normal file
34
Products/example/living_platform/living_platform_main.h
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#ifndef __LIVING_PLATFORM_MAIN_H__
|
||||
#define __LIVING_PLATFORM_MAIN_H__
|
||||
|
||||
#include <iotx_log.h>
|
||||
|
||||
//#define LIVING_PLATFORM_PRODUCT_DYNAMIC_REGISTER
|
||||
|
||||
/*
|
||||
Note:You can test some function to define LIVING_PLATFORM_USE_UT_FOR_TESTING
|
||||
*/
|
||||
//#define LIVING_PLATFORM_USE_UT_FOR_TESTING
|
||||
|
||||
#define living_platform_debug(...) log_debug("ct", __VA_ARGS__)
|
||||
#define living_platform_info(...) log_info("ct", __VA_ARGS__)
|
||||
#define living_platform_warn(...) log_warning("ct", __VA_ARGS__)
|
||||
#define living_platform_err(...) log_err("ct", __VA_ARGS__)
|
||||
#define living_platform_crit(...) log_crit("ct", __VA_ARGS__)
|
||||
|
||||
#define LIVING_PLATFORM_YIELD_TIMEOUT_MS (200)
|
||||
#define LIVING_PLATFORM_OTA_BUFFER_LEN (512 + 1)
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int master_devid;
|
||||
int cloud_connected;
|
||||
int master_initialized;
|
||||
void *g_user_dispatch_thread;
|
||||
int g_user_dispatch_thread_running;
|
||||
} living_platform_ctx_t;
|
||||
|
||||
extern int living_platform_main(void *paras);
|
||||
extern living_platform_ctx_t *living_platform_get_ctx(void);
|
||||
extern void living_platform_load_device_meta_info(void);
|
||||
#endif
|
||||
667
Products/example/living_platform/living_platform_ut.c
Normal file
667
Products/example/living_platform/living_platform_ut.c
Normal file
|
|
@ -0,0 +1,667 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2019 Alibaba Group Holding Limited
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "cJSON.h"
|
||||
#include "iot_import.h"
|
||||
#include "iot_export_linkkit.h"
|
||||
#include "iotx_log.h"
|
||||
|
||||
#include "living_platform_main.h"
|
||||
#include "app_entry.h"
|
||||
#include "living_platform_ut.h"
|
||||
|
||||
static living_platform_tsl_t living_platform_tsl_data;
|
||||
|
||||
living_platform_tsl_t *living_platform_ut_get_tsl_data(void)
|
||||
{
|
||||
return &living_platform_tsl_data;
|
||||
}
|
||||
|
||||
int living_platform_ut_set_LightSwitch(char LightSwitch)
|
||||
{
|
||||
living_platform_tsl_data.LightSwitch = LightSwitch;
|
||||
living_platform_info("set LightSwitch:%s", (LightSwitch == 0) ? "off" : "on");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int living_platform_ut_get_LightSwitch(void)
|
||||
{
|
||||
return living_platform_tsl_data.LightSwitch;
|
||||
}
|
||||
|
||||
int living_platform_ut_set_NightLightSwitch(char NightLightSwitch)
|
||||
{
|
||||
living_platform_tsl_data.NightLightSwitch = NightLightSwitch;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int living_platform_ut_set_WorkMode(unsigned char WorkMode)
|
||||
{
|
||||
living_platform_tsl_data.WorkMode = WorkMode;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int living_platform_ut_set_Brightness(unsigned char Brightness)
|
||||
{
|
||||
living_platform_tsl_data.Brightness = Brightness;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int living_platform_ut_set_RGB(unsigned char R, unsigned char G, unsigned char B)
|
||||
{
|
||||
living_platform_tsl_data.RGB.R = R;
|
||||
living_platform_tsl_data.RGB.G = G;
|
||||
living_platform_tsl_data.RGB.B = B;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int living_platform_ut_set_Float(float f)
|
||||
{
|
||||
living_platform_tsl_data.f = f;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int living_platform_ut_set_Double(double d)
|
||||
{
|
||||
living_platform_tsl_data.d = d;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int living_platform_ut_set_PropertyString(char *PropertyString)
|
||||
{
|
||||
if (PropertyString)
|
||||
{
|
||||
HAL_Snprintf(living_platform_tsl_data.PropertyString, PROPERTY_STRING_MAX_LEN, "%s", PropertyString);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int living_platform_ut_set_WorkTime(char *WorkTime)
|
||||
{
|
||||
if (WorkTime)
|
||||
{
|
||||
HAL_Snprintf(living_platform_tsl_data.WorkTime, WORKTIME_MAX_LEN, "%s", WorkTime);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报属性LightSwitch到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 属性LightSwitch的值,数据类型uint8_t
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_property_LightSwitch(uint32_t devid, uint8_t value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char property_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(property_payload, sizeof(property_payload), "{\"LightSwitch\": %d}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报属性WIFI_Band到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 属性WIFI_Band的值,数据类型char*
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_property_WIFI_Band(uint32_t devid, char *value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char *property_payload = NULL;
|
||||
uint32_t property_payload_len = 0;
|
||||
|
||||
if (value == NULL)
|
||||
{
|
||||
return -0x101;
|
||||
}
|
||||
|
||||
property_payload_len = strlen("WIFI_Band") + strlen(value) + 10;
|
||||
property_payload = HAL_Malloc(property_payload_len);
|
||||
if (property_payload == NULL)
|
||||
{
|
||||
return -0x201;
|
||||
}
|
||||
memset(property_payload, 0, property_payload_len);
|
||||
|
||||
res = HAL_Snprintf(property_payload, property_payload_len, "{\"WIFI_Band\": \"%s\"}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
HAL_Free(property_payload);
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报属性WiFI_RSSI到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 属性WiFI_RSSI的值,数据类型int32_t
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_property_WiFI_RSSI(uint32_t devid, int32_t value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char property_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(property_payload, sizeof(property_payload), "{\"WiFI_RSSI\": %d}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报属性WIFI_AP_BSSID到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 属性WIFI_AP_BSSID的值,数据类型char*
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_property_WIFI_AP_BSSID(uint32_t devid, char *value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char *property_payload = NULL;
|
||||
uint32_t property_payload_len = 0;
|
||||
|
||||
if (value == NULL)
|
||||
{
|
||||
return -0x101;
|
||||
}
|
||||
|
||||
property_payload_len = strlen("WIFI_AP_BSSID") + strlen(value) + 10;
|
||||
property_payload = HAL_Malloc(property_payload_len);
|
||||
if (property_payload == NULL)
|
||||
{
|
||||
return -0x201;
|
||||
}
|
||||
memset(property_payload, 0, property_payload_len);
|
||||
|
||||
res = HAL_Snprintf(property_payload, property_payload_len, "{\"WIFI_AP_BSSID\": \"%s\"}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
HAL_Free(property_payload);
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报属性WIFI_Channel到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 属性WIFI_Channel的值,数据类型int32_t
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_property_WIFI_Channel(uint32_t devid, int32_t value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char property_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(property_payload, sizeof(property_payload), "{\"WIFI_Channel\": %d}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报属性WiFI_SNR到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 属性WiFI_SNR的值,数据类型int32_t
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_property_WiFI_SNR(uint32_t devid, int32_t value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char property_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(property_payload, sizeof(property_payload), "{\"WiFI_SNR\": %d}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报属性NightLightSwitch到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 属性NightLightSwitch的值,数据类型uint8_t
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_property_NightLightSwitch(uint32_t devid, uint8_t value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char property_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(property_payload, sizeof(property_payload), "{\"NightLightSwitch\": %d}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报属性WorkMode到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 属性WorkMode的值,数据类型uint32_t
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_property_WorkMode(uint32_t devid, uint32_t value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char property_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(property_payload, sizeof(property_payload), "{\"WorkMode\": %d}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报属性Brightness到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 属性Brightness的值,数据类型int32_t
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_property_Brightness(uint32_t devid, int32_t value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char property_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(property_payload, sizeof(property_payload), "{\"Brightness\": %d}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报属性worktime到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param utc 属性worktime的值,数据类型${dataType}
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_property_worktime(uint32_t devid, char *worktime)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char property_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(property_payload, sizeof(property_payload), "{\"worktime\": \"%s\"}", worktime);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
return res;
|
||||
}
|
||||
|
||||
int32_t app_post_property_RGB(uint32_t devid, unsigned char R, unsigned char G, unsigned char B)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char property_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(property_payload, sizeof(property_payload), "{\"RGBColor\": {\"Red\": %d, \"Green\": %d, \"Blue\": %d}}", R, G, B);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报属性onlyread到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 属性onlyread的值,数据类型int32_t
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_property_onlyread(uint32_t devid, int32_t value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char property_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(property_payload, sizeof(property_payload), "{\"onlyread\": %d}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报属性float到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 属性float的值,数据类型float
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_property_float(uint32_t devid, float value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char property_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(property_payload, sizeof(property_payload), "{\"floatid\": %f}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报属性double到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 属性double的值,数据类型double
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_property_double(uint32_t devid, double value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char property_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(property_payload, sizeof(property_payload), "{\"doubleid\": %f}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报属性PropertyString到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 属性PropertyString的值,数据类型char*
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_property_PropertyString(uint32_t devid, char *value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char *property_payload = NULL;
|
||||
uint32_t property_payload_len = 0;
|
||||
|
||||
if (value == NULL)
|
||||
{
|
||||
return -0x101;
|
||||
}
|
||||
|
||||
property_payload_len = strlen("PropertyString") + strlen(value) + 10;
|
||||
property_payload = HAL_Malloc(property_payload_len);
|
||||
if (property_payload == NULL)
|
||||
{
|
||||
return -0x201;
|
||||
}
|
||||
memset(property_payload, 0, property_payload_len);
|
||||
|
||||
res = HAL_Snprintf(property_payload, property_payload_len, "{\"PropertyString\": \"%s\"}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
HAL_Free(property_payload);
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_Report(devid, ITM_MSG_POST_PROPERTY,
|
||||
(uint8_t *)property_payload, strlen(property_payload));
|
||||
HAL_Free(property_payload);
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报事件Error到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 事件Error的值,数据类型uint32_t
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_event_Error(uint32_t devid, uint32_t value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char *event_id = "Error";
|
||||
char event_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(event_payload, sizeof(event_payload), "{\"ErrorCode\": %d}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_TriggerEvent(EXAMPLE_MASTER_DEVID, event_id, strlen(event_id),
|
||||
event_payload, strlen(event_payload));
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报事件alarm到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 事件alarm的值,数据类型int32_t
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_event_alarm(uint32_t devid, int32_t value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char *event_id = "alarm";
|
||||
char event_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(event_payload, sizeof(event_payload), "{\"alarm\": %d}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_TriggerEvent(EXAMPLE_MASTER_DEVID, event_id, strlen(event_id),
|
||||
event_payload, strlen(event_payload));
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 上报事件info到云端
|
||||
* @param devid 由调用IOT_Linkkit_Open返回的设备标示符
|
||||
* @param value 事件info的值,数据类型int32_t
|
||||
* @return 消息id:(>=1), 上报失败: <0
|
||||
*/
|
||||
int32_t app_post_event_info(uint32_t devid, int32_t value)
|
||||
{
|
||||
int32_t res = -0x100;
|
||||
char *event_id = "info";
|
||||
char event_payload[64] = {0};
|
||||
|
||||
res = HAL_Snprintf(event_payload, sizeof(event_payload), "{\"info\": %d}", value);
|
||||
if (res < 0)
|
||||
{
|
||||
return -0x10E;
|
||||
}
|
||||
|
||||
res = IOT_Linkkit_TriggerEvent(EXAMPLE_MASTER_DEVID, event_id, strlen(event_id),
|
||||
event_payload, strlen(event_payload));
|
||||
return res;
|
||||
}
|
||||
|
||||
//Just for reference
|
||||
void user_post_property(void)
|
||||
{
|
||||
static int count = 0;
|
||||
|
||||
app_post_property_LightSwitch(EXAMPLE_MASTER_DEVID, living_platform_tsl_data.LightSwitch);
|
||||
app_post_property_WIFI_Band(EXAMPLE_MASTER_DEVID, living_platform_tsl_data.wifi.band);
|
||||
app_post_property_WiFI_RSSI(EXAMPLE_MASTER_DEVID, living_platform_tsl_data.wifi.rssi);
|
||||
app_post_property_WIFI_AP_BSSID(EXAMPLE_MASTER_DEVID, living_platform_tsl_data.wifi.bssid);
|
||||
app_post_property_WIFI_Channel(EXAMPLE_MASTER_DEVID, living_platform_tsl_data.wifi.Channel);
|
||||
app_post_property_WiFI_SNR(EXAMPLE_MASTER_DEVID, living_platform_tsl_data.wifi.SNR);
|
||||
app_post_property_NightLightSwitch(EXAMPLE_MASTER_DEVID, living_platform_tsl_data.NightLightSwitch);
|
||||
app_post_property_WorkMode(EXAMPLE_MASTER_DEVID, living_platform_tsl_data.WorkMode);
|
||||
app_post_property_Brightness(EXAMPLE_MASTER_DEVID, living_platform_tsl_data.Brightness);
|
||||
app_post_property_worktime(EXAMPLE_MASTER_DEVID, living_platform_tsl_data.WorkTime);
|
||||
app_post_property_RGB(EXAMPLE_MASTER_DEVID, living_platform_tsl_data.RGB.R, living_platform_tsl_data.RGB.G, living_platform_tsl_data.RGB.B);
|
||||
app_post_property_onlyread(EXAMPLE_MASTER_DEVID, living_platform_tsl_data.readonly);
|
||||
app_post_property_float(EXAMPLE_MASTER_DEVID, living_platform_tsl_data.f);
|
||||
app_post_property_double(EXAMPLE_MASTER_DEVID, living_platform_tsl_data.d);
|
||||
app_post_property_PropertyString(EXAMPLE_MASTER_DEVID, living_platform_tsl_data.PropertyString);
|
||||
|
||||
if (count == 0)
|
||||
{
|
||||
app_post_event_Error(EXAMPLE_MASTER_DEVID, count);
|
||||
}
|
||||
else if (count == 1)
|
||||
{
|
||||
app_post_event_alarm(EXAMPLE_MASTER_DEVID, count);
|
||||
}
|
||||
else if (count == 2)
|
||||
{
|
||||
app_post_event_info(EXAMPLE_MASTER_DEVID, count);
|
||||
}
|
||||
|
||||
if (++count > 2)
|
||||
{
|
||||
count = 0;
|
||||
}
|
||||
}
|
||||
|
||||
//Just for reference
|
||||
void user_deviceinfo_update(void)
|
||||
{
|
||||
int res = 0;
|
||||
living_platform_ctx_t *living_platform_ctx = living_platform_get_ctx();
|
||||
char *device_info_update = "[{\"attrKey\":\"ct\",\"attrValue\":\"I am a ct device\"},{\"attrKey\":\"ct2\",\"attrValue\":\"I am a ct2\"}]";
|
||||
|
||||
res = IOT_Linkkit_Report(living_platform_ctx->master_devid, ITM_MSG_DEVICEINFO_UPDATE,
|
||||
(unsigned char *)device_info_update, strlen(device_info_update));
|
||||
living_platform_info("Device Info Update Message ID: %d", res);
|
||||
}
|
||||
|
||||
void living_platform_deviceinfo_update(void)
|
||||
{
|
||||
user_deviceinfo_update();
|
||||
}
|
||||
|
||||
//Just for reference
|
||||
void user_deviceinfo_delete(void)
|
||||
{
|
||||
int res = 0;
|
||||
living_platform_ctx_t *living_platform_ctx = living_platform_get_ctx();
|
||||
char *device_info_delete = "[{\"attrKey\":\"ct2\"}]";
|
||||
|
||||
res = IOT_Linkkit_Report(living_platform_ctx->master_devid, ITM_MSG_DEVICEINFO_DELETE,
|
||||
(unsigned char *)device_info_delete, strlen(device_info_delete));
|
||||
living_platform_info("Device Info Delete Message ID: %d", res);
|
||||
}
|
||||
|
||||
void living_platform_ut_misc_process(uint64_t time_now_sec)
|
||||
{
|
||||
/* Post Proprety Example */
|
||||
if (time_now_sec % 11 == 0)
|
||||
{
|
||||
user_post_property();
|
||||
}
|
||||
|
||||
/* Device Info Update Example */
|
||||
if (time_now_sec % 23 == 0)
|
||||
{
|
||||
user_deviceinfo_update();
|
||||
}
|
||||
|
||||
/* Device Info Delete Example */
|
||||
if (time_now_sec % 29 == 0)
|
||||
{
|
||||
user_deviceinfo_delete();
|
||||
}
|
||||
}
|
||||
|
||||
int living_platform_ut_init(void)
|
||||
{
|
||||
int ret = SUCCESS_RETURN;
|
||||
|
||||
memset(&living_platform_tsl_data, 0, sizeof(living_platform_tsl_t));
|
||||
living_platform_tsl_data.PropertyString = (char *)HAL_Malloc(PROPERTY_STRING_MAX_LEN);
|
||||
if (living_platform_tsl_data.PropertyString == NULL)
|
||||
{
|
||||
living_platform_err("no mem");
|
||||
|
||||
ret = FAIL_RETURN;
|
||||
}
|
||||
|
||||
living_platform_tsl_data.LightSwitch = 1;
|
||||
living_platform_tsl_data.NightLightSwitch = 0;
|
||||
living_platform_tsl_data.Brightness = 88;
|
||||
living_platform_tsl_data.WorkMode = 3;
|
||||
HAL_Snprintf(living_platform_tsl_data.WorkTime, WORKTIME_MAX_LEN, "%s", "1582861307282");
|
||||
HAL_Snprintf(living_platform_tsl_data.PropertyString, PROPERTY_STRING_MAX_LEN, "%s", "This is a testing property string!");
|
||||
living_platform_tsl_data.f = 9.999999;
|
||||
living_platform_tsl_data.d = 88.888888;
|
||||
living_platform_tsl_data.RGB.R = 128;
|
||||
living_platform_tsl_data.RGB.G = 128;
|
||||
living_platform_tsl_data.RGB.B = 128;
|
||||
living_platform_tsl_data.readonly = 38;
|
||||
|
||||
living_platform_tsl_data.wifi.Channel = 6;
|
||||
living_platform_tsl_data.wifi.SNR = -127;
|
||||
living_platform_tsl_data.wifi.rssi = -78;
|
||||
HAL_Snprintf(living_platform_tsl_data.wifi.bssid, AP_BSSID_MAX_LEN, "%s", "11:22:33:44:55:66");
|
||||
HAL_Snprintf(living_platform_tsl_data.wifi.band, WIFI_BAND_MAX_LEN, "%s", "2.4G");
|
||||
|
||||
return ret;
|
||||
}
|
||||
63
Products/example/living_platform/living_platform_ut.h
Normal file
63
Products/example/living_platform/living_platform_ut.h
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
#ifndef __LIVING_PLATFORM_UT_H__
|
||||
#define __LIVING_PLATFORM_UT_H__
|
||||
|
||||
#define KV_KEY_PK "pk"
|
||||
#define KV_KEY_PS "ps"
|
||||
#define KV_KEY_DN "dn"
|
||||
#define KV_KEY_DS "ds"
|
||||
|
||||
#define MAX_KEY_LEN (6)
|
||||
|
||||
#define EXAMPLE_MASTER_DEVID (0)
|
||||
|
||||
#define PROPERTY_STRING_MAX_LEN (1024)
|
||||
#define WORKTIME_MAX_LEN (64)
|
||||
#define WIFI_BAND_MAX_LEN (64)
|
||||
#define AP_BSSID_MAX_LEN (64)
|
||||
|
||||
typedef struct _RGBColor_s
|
||||
{
|
||||
unsigned char R;
|
||||
unsigned char G;
|
||||
unsigned char B;
|
||||
} RGBColor_t;
|
||||
|
||||
typedef struct _WiFi_s
|
||||
{
|
||||
int Channel;
|
||||
char SNR;
|
||||
char bssid[AP_BSSID_MAX_LEN];
|
||||
char band[WIFI_BAND_MAX_LEN];
|
||||
char rssi;
|
||||
} WiFi_t;
|
||||
|
||||
typedef struct _living_platform_tsl_s
|
||||
{
|
||||
char LightSwitch;
|
||||
char NightLightSwitch;
|
||||
RGBColor_t RGB;
|
||||
unsigned char Brightness;
|
||||
unsigned char WorkMode;
|
||||
float f;
|
||||
double d;
|
||||
int readonly;
|
||||
char *PropertyString;
|
||||
char WorkTime[WORKTIME_MAX_LEN];
|
||||
WiFi_t wifi;
|
||||
} living_platform_tsl_t;
|
||||
|
||||
extern int living_platform_ut_init(void);
|
||||
extern void living_platform_ut_misc_process(uint64_t time_now_sec);
|
||||
extern living_platform_tsl_t* living_platform_ut_get_tsl_data(void);
|
||||
extern int living_platform_ut_set_LightSwitch(char LightSwitch);
|
||||
extern int living_platform_ut_get_LightSwitch(void);
|
||||
extern int living_platform_ut_set_NightLightSwitch(char NightLightSwitch);
|
||||
extern int living_platform_ut_set_WorkMode(unsigned char WorkMode);
|
||||
extern int living_platform_ut_set_Brightness(unsigned char Brightness);
|
||||
extern int living_platform_ut_set_RGB(unsigned char R, unsigned char G, unsigned char B);
|
||||
extern int living_platform_ut_set_Float(float f);
|
||||
extern int living_platform_ut_set_Double(double d);
|
||||
extern int living_platform_ut_set_PropertyString(char *PropertyString);
|
||||
extern int living_platform_ut_set_WorkTime(char *WorkTime);
|
||||
extern void living_platform_deviceinfo_update(void);
|
||||
#endif
|
||||
33
Products/example/living_platform/make.settings
Normal file
33
Products/example/living_platform/make.settings
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#
|
||||
# Configure FeiYan SDK Features
|
||||
#
|
||||
|
||||
#=========Basic Features==========
|
||||
FEATURE_SRCPATH="."
|
||||
FEATURE_MQTT_COMM_ENABLED=y
|
||||
FEATURE_MQTT_AUTO_SUBSCRIBE=y
|
||||
FEATURE_DEVICE_MODEL_ENABLED=y
|
||||
FEATURE_OTA_ENABLED=y
|
||||
FEATURE_DEV_BIND_ENABLED=y
|
||||
FEATURE_SUPPORT_TLS=y
|
||||
#Support unified service post
|
||||
FEATURE_DM_UNIFIED_SERVICE_POST=y
|
||||
#=========Basic Features==========
|
||||
|
||||
#=========User Config Features==========
|
||||
FEATURE_ALCS_ENABLED=y
|
||||
|
||||
FEATURE_DEVICE_MODEL_GATEWAY=n
|
||||
|
||||
#
|
||||
# AWSS Configurations
|
||||
#
|
||||
FEATURE_WIFI_PROVISION_ENABLED=y
|
||||
|
||||
FEATURE_AWSS_SUPPORT_SMARTCONFIG=y
|
||||
FEATURE_AWSS_SUPPORT_ZEROCONFIG=y
|
||||
FEATURE_AWSS_SUPPORT_DEV_AP=y
|
||||
|
||||
FEATURE_AWSS_SUPPORT_PHONEASAP=n
|
||||
FEATURE_AWSS_SUPPORT_ROUTER=n
|
||||
#=========User Config Features==========
|
||||
110
Products/example/living_platform/makefile
Executable file
110
Products/example/living_platform/makefile
Executable file
|
|
@ -0,0 +1,110 @@
|
|||
|
||||
include living_platform.mk
|
||||
target = living_platform
|
||||
|
||||
CFLAGS += -DALCS_ENABLED
|
||||
|
||||
INCLUDE_PATH = -I$(PWD)
|
||||
INCLUDE = $(INCLUDE_PATH)/ \
|
||||
-I$(PWD)/../../../prebuild/include \
|
||||
-I$(PWD)/../../../prebuild/include/exports \
|
||||
-I$(PWD)/../../../prebuild/include/imports \
|
||||
-I$(PWD)/../../../prebuild/include/kernel/rhino/core/include \
|
||||
-I$(PWD)/../../../prebuild/include/framework/netmgr/include \
|
||||
-I$(PWD)/../../../prebuild/include/platform/arch/arm/armv7m/gcc/m4 \
|
||||
-I$(PWD)/../../../prebuild/include/board/$(BOARD)
|
||||
|
||||
app = $(wildcard ./*.c)
|
||||
obj_app = $(patsubst %.c,%.o,$(app))
|
||||
|
||||
|
||||
CROSS_PREFIX="arm-none-eabi-"
|
||||
DEPENDS=""
|
||||
CCLD=""
|
||||
LDFLAGS=""
|
||||
CC=arm-none-eabi-gcc
|
||||
LD=arm-none-eabi-ld
|
||||
AR=arm-none-eabi-ar
|
||||
STRIP=arm-none-eabi-strip
|
||||
OBJCOPY=arm-none-eabi-objcopy
|
||||
|
||||
# CFLAGS += -MD -ggdb -Os -Wall -Wfatal-errors -fsigned-char -ffunction-sections -fdata-sections -fno-common -std=gnu11 -Werror \
|
||||
# -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
|
||||
CFLAGS += -DWITH_LWIP -DCONFIG_AOS_CLI -DLOG_SIMPLE
|
||||
CFLAGS += ${GLOBAL_CFLAGS}
|
||||
CFLAGS += ${SDK_DEFINES}
|
||||
|
||||
ifeq ("$(BOARD)", "hf-lpt230")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
else ifeq ("$(BOARD)", "hf-lpt130")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
else ifeq ("$(BOARD)", "hf-lpb130")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
else ifeq ("$(BOARD)", "hf-lpb135")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
else ifeq ("$(BOARD)", "uno-91h")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
|
||||
else ifeq ("$(BOARD)", "mk1101")
|
||||
else ifeq ("$(BOARD)", "mk3060")
|
||||
CFLAGS += -mcpu=arm968e-s -march=armv5te -mthumb -mthumb-interwork -mlittle-endian -w -Os -Wall -Wfatal-errors -fsigned-char -ffunction-sections -fdata-sections -fno-common -std=gnu11
|
||||
else ifeq ("$(BOARD)", "mk3061")
|
||||
CFLAGS += -mcpu=arm968e-s -march=armv5te -mthumb -mthumb-interwork -mlittle-endian -w -Os -Wall -Wfatal-errors -fsigned-char -ffunction-sections -fdata-sections -fno-common -std=gnu11
|
||||
|
||||
else ifeq ("$(BOARD)", "mk3080")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
else ifeq ("$(BOARD)", "mk3092")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
else ifeq ("$(BOARD)", "mk5080")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
|
||||
|
||||
else ifeq ("$(BOARD)", "asr5501")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -mfpu=fpv4-sp-d16 -std=gnu11 -w
|
||||
else ifeq ("$(BOARD)", "mx1270")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -mfpu=fpv4-sp-d16 -std=gnu11 -w
|
||||
|
||||
else ifeq ("$(BOARD)", "bk7231devkitc")
|
||||
CFLAGS += -mcpu=arm968e-s -march=armv5te -mthumb -mthumb-interwork -mlittle-endian -w -Os -Wall -Wfatal-errors -fsigned-char -ffunction-sections -fdata-sections -fno-common -std=gnu11
|
||||
CFLAGS += -DBK7231DEVKITC
|
||||
else ifeq ("$(BOARD)", "bk7231sdevkitc")
|
||||
CFLAGS += -mcpu=arm968e-s -march=armv5te -mthumb -mthumb-interwork -mlittle-endian -w -Os -Wall -Wfatal-errors -fsigned-char -ffunction-sections -fdata-sections -fno-common -std=gnu11
|
||||
CFLAGS += -DBK7231SDEVKITC
|
||||
else ifeq ("$(BOARD)", "bk7231udevkitc")
|
||||
CFLAGS += -mcpu=arm968e-s -march=armv5te -mthumb -mthumb-interwork -mlittle-endian -w -Os -Wall -Wfatal-errors -fsigned-char -ffunction-sections -fdata-sections -fno-common -std=gnu11
|
||||
CFLAGS += -DBK7231UDEVKITC
|
||||
else ifeq ("$(BOARD)", "b_l475e")
|
||||
|
||||
else ifeq ("$(BOARD)", "eml3047")
|
||||
else ifeq ("$(BOARD)", "frdmkl27z")
|
||||
else ifeq ("$(BOARD)", "lpcxpresso54102")
|
||||
|
||||
else ifeq ("$(BOARD)", "pca10056")
|
||||
|
||||
## toolchains['arm-rockchip-linux-gnueabihf']
|
||||
else ifeq ("$(BOARD)", "rk1108")
|
||||
|
||||
## toolchains['nds32le-elf-newlib-v3']
|
||||
else ifeq ("$(BOARD)", "sv6266_evb")
|
||||
endif
|
||||
|
||||
ifneq ("$(BOARD)", "")
|
||||
$(info BOARD=${BOARD} CONFIG_COMPILE_DATE=${CONFIG_COMPILE_DATE} region=${CONFIG_SERVER_REGION} env=${CONFIG_SERVER_ENV} debug=${CONFIG_DEBUG})
|
||||
endif
|
||||
|
||||
# Compile date
|
||||
CONFIG_COMPILE_DATE ?= $(shell date +%Y%m%d.%H%M%S)
|
||||
all : $(target)
|
||||
|
||||
$(target):$(obj_app)
|
||||
@mkdir -p obj
|
||||
@mv *.o obj
|
||||
@$(RM) -rf *.d
|
||||
@$(AR) -rcs $(PWD)/../../../prebuild/lib/$(target).a obj/*.o 2>/dev/null
|
||||
@$(RM) -rf obj
|
||||
%.o:%.c
|
||||
@$(CC) -c $(CFLAGS) $(INCLUDE) -o $@ $<
|
||||
|
||||
clean:
|
||||
@-$(RM) obj/*
|
||||
45
Products/example/living_platform/ucube.py
Executable file
45
Products/example/living_platform/ucube.py
Executable file
|
|
@ -0,0 +1,45 @@
|
|||
src =Split('''
|
||||
living_platform_main.c
|
||||
app_entry.c
|
||||
''')
|
||||
|
||||
component =aos_component('living_platform', src)
|
||||
|
||||
dependencis =Split('''
|
||||
framework/protocol/linkkit/sdk
|
||||
framework/protocol/linkkit/hal
|
||||
framework/netmgr
|
||||
framework/common
|
||||
framework/ywss4linkkit
|
||||
utility/cjson
|
||||
tools/cli
|
||||
''')
|
||||
for i in dependencis:
|
||||
component.add_comp_deps(i)
|
||||
|
||||
global_macros =Split('''
|
||||
ALIOT_DEBUG
|
||||
IOTX_DEBUG
|
||||
USE_LPTHREAD
|
||||
CONFIG_DM_DEVTYPE_SINGLE
|
||||
TEST_ALCS
|
||||
CONFIG_AOS_CLI
|
||||
''')
|
||||
for i in global_macros:
|
||||
component.add_global_macros(i)
|
||||
|
||||
if aos_global_config.get('LWIP') == 1:
|
||||
component.add_comp_deps("kernel/protocols/net")
|
||||
aos_global_config.set('no_with_lwip', 0)
|
||||
|
||||
if aos_global_config.get('print_heap') == 1:
|
||||
component.add_global_macros('CONFIG_PRINT_HEAP')
|
||||
|
||||
if aos_global_config.mcu_family == 'esp8266':
|
||||
component.add_global_macros('FOTA_RAM_LIMIT_MODE')
|
||||
component.add_global_macros('ESP8266_CHIPSET')
|
||||
else :
|
||||
component.add_global_macros('CONFIG_AOS_CLI')
|
||||
component.add_comp_deps("cli")
|
||||
|
||||
|
||||
617
Products/example/living_platform_rawdata/app_entry.c
Normal file
617
Products/example/living_platform_rawdata/app_entry.c
Normal file
|
|
@ -0,0 +1,617 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2019 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <aos/aos.h>
|
||||
#include <aos/yloop.h>
|
||||
#include "netmgr.h"
|
||||
#include "iot_export.h"
|
||||
#include "iot_import.h"
|
||||
#include "aos/kv.h"
|
||||
|
||||
#ifdef CSP_LINUXHOST
|
||||
#include <signal.h>
|
||||
#endif
|
||||
|
||||
#include <k_api.h>
|
||||
|
||||
#if defined(OTA_ENABLED) && defined(BUILD_AOS)
|
||||
#include "ota_service.h"
|
||||
#endif
|
||||
|
||||
#include "living_platform_rawdata_main.h"
|
||||
#include "app_entry.h"
|
||||
#include "living_platform_rawdata_cmds.h"
|
||||
#include "living_platform_rawdata_ut.h"
|
||||
|
||||
static char living_platform_rawdata_started = 0;
|
||||
static int living_platform_rawdata_duration_interval = 0;
|
||||
|
||||
extern k_mm_head *g_kmm_head;
|
||||
extern uint32_t dumpsys_mm_info_func(uint32_t len);
|
||||
extern uint32_t dumpsys_task_func(char *buf, uint32_t len, int detail);
|
||||
static void dump_task_and_mem_info(void)
|
||||
{
|
||||
#if defined(CONFIG_AOS_CLI)
|
||||
printf("======================================");
|
||||
dumpsys_mm_info_func(0);
|
||||
printf("======================================");
|
||||
dumpsys_task_func(NULL, 0, 1);
|
||||
printf("======================================");
|
||||
#else
|
||||
printf("============free heap size =%d==========\r\n", g_kmm_head->free_size);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void living_platform_rawdata_duration_work(void *param)
|
||||
{
|
||||
dump_task_and_mem_info();
|
||||
|
||||
aos_post_delayed_action(living_platform_rawdata_duration_interval, living_platform_rawdata_duration_work, NULL);
|
||||
}
|
||||
|
||||
static void wifi_service_event(input_event_t *event, void *priv_data)
|
||||
{
|
||||
netmgr_ap_config_t config;
|
||||
|
||||
if (event->type != EV_WIFI || event->code != CODE_WIFI_ON_GOT_IP)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
memset(&config, 0, sizeof(netmgr_ap_config_t));
|
||||
netmgr_get_ap_config(&config);
|
||||
|
||||
living_platform_rawdata_info("wifi_service_event config.ssid:%s", config.ssid);
|
||||
if (strcmp(config.ssid, "adha") == 0 || strcmp(config.ssid, "aha") == 0)
|
||||
{
|
||||
// clear_wifi_ssid();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!living_platform_rawdata_started)
|
||||
{
|
||||
aos_task_new("linkkit", (void (*)(void *))living_platform_rawdata_main, NULL, LIVING_PLATFORM_RAWDATA_MAIN_THREAD_STACKSZIE);
|
||||
living_platform_rawdata_started = 1;
|
||||
}
|
||||
}
|
||||
|
||||
static void cloud_service_event(input_event_t *event, void *priv_data)
|
||||
{
|
||||
if (event->type != EV_YUNIO)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
living_platform_rawdata_info("cloud_service_event %d", event->code);
|
||||
|
||||
if (event->code == CODE_YUNIO_ON_CONNECTED)
|
||||
{
|
||||
living_platform_rawdata_info("user sub and pub here");
|
||||
return;
|
||||
}
|
||||
|
||||
if (event->code == CODE_YUNIO_ON_DISCONNECTED)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Note:
|
||||
* the linkkit_event_monitor must not block and should run to complete fast
|
||||
* if user wants to do complex operation with much time,
|
||||
* user should post one task to do this, not implement complex operation in
|
||||
* linkkit_event_monitor
|
||||
*/
|
||||
|
||||
void linkkit_event_monitor(int event)
|
||||
{
|
||||
switch (event)
|
||||
{
|
||||
case IOTX_AWSS_START: // AWSS start without enbale, just supports device discover
|
||||
// operate led to indicate user
|
||||
living_platform_rawdata_info("IOTX_AWSS_START");
|
||||
break;
|
||||
case IOTX_AWSS_ENABLE: // AWSS enable, AWSS doesn't parse awss packet until AWSS is enabled.
|
||||
living_platform_rawdata_info("IOTX_AWSS_ENABLE");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_LOCK_CHAN: // AWSS lock channel(Got AWSS sync packet)
|
||||
living_platform_rawdata_info("IOTX_AWSS_LOCK_CHAN");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_PASSWD_ERR: // AWSS decrypt passwd error
|
||||
living_platform_rawdata_info("IOTX_AWSS_PASSWD_ERR");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_GOT_SSID_PASSWD:
|
||||
living_platform_rawdata_info("IOTX_AWSS_GOT_SSID_PASSWD");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_ADHA: // AWSS try to connnect adha (device
|
||||
// discover, router solution)
|
||||
living_platform_rawdata_info("IOTX_AWSS_CONNECT_ADHA");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_ADHA_FAIL: // AWSS fails to connect adha
|
||||
living_platform_rawdata_info("IOTX_AWSS_CONNECT_ADHA_FAIL");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_AHA: // AWSS try to connect aha (AP solution)
|
||||
living_platform_rawdata_info("IOTX_AWSS_CONNECT_AHA");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_AHA_FAIL: // AWSS fails to connect aha
|
||||
living_platform_rawdata_info("IOTX_AWSS_CONNECT_AHA_FAIL");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_SETUP_NOTIFY: // AWSS sends out device setup information
|
||||
// (AP and router solution)
|
||||
living_platform_rawdata_info("IOTX_AWSS_SETUP_NOTIFY");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_ROUTER: // AWSS try to connect destination router
|
||||
living_platform_rawdata_info("IOTX_AWSS_CONNECT_ROUTER");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_ROUTER_FAIL: // AWSS fails to connect destination
|
||||
// router.
|
||||
living_platform_rawdata_info("IOTX_AWSS_CONNECT_ROUTER_FAIL");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_GOT_IP: // AWSS connects destination successfully and got
|
||||
// ip address
|
||||
living_platform_rawdata_info("IOTX_AWSS_GOT_IP");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_SUC_NOTIFY: // AWSS sends out success notify (AWSS
|
||||
// sucess)
|
||||
living_platform_rawdata_info("IOTX_AWSS_SUC_NOTIFY");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_BIND_NOTIFY: // AWSS sends out bind notify information to
|
||||
// support bind between user and device
|
||||
living_platform_rawdata_info("IOTX_AWSS_BIND_NOTIFY");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_ENABLE_TIMEOUT: // AWSS enable timeout
|
||||
// user needs to enable awss again to support get ssid & passwd of router
|
||||
living_platform_rawdata_info("IOTX_AWSS_ENALBE_TIMEOUT");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_CONN_CLOUD: // Device try to connect cloud
|
||||
living_platform_rawdata_info("IOTX_CONN_CLOUD");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_CONN_CLOUD_FAIL: // Device fails to connect cloud, refer to
|
||||
// net_sockets.h for error code
|
||||
living_platform_rawdata_info("IOTX_CONN_CLOUD_FAIL");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_CONN_CLOUD_SUC: // Device connects cloud successfully
|
||||
living_platform_rawdata_info("IOTX_CONN_CLOUD_SUC");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_RESET: // Linkkit reset success (just got reset response from
|
||||
// cloud without any other operation)
|
||||
living_platform_rawdata_info("IOTX_RESET");
|
||||
break;
|
||||
case IOTX_CONN_REPORT_TOKEN_SUC:
|
||||
living_platform_rawdata_info("---- report token success ----");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void user_key_process(input_event_t *eventinfo, void *priv_data)
|
||||
{
|
||||
if (eventinfo->type != EV_KEY)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
living_platform_rawdata_info("awss config press %d\n", eventinfo->value);
|
||||
|
||||
if (eventinfo->code == CODE_BOOT)
|
||||
{
|
||||
if (eventinfo->value == VALUE_KEY_CLICK)
|
||||
{
|
||||
living_platform_rawdata_do_awss_active();
|
||||
}
|
||||
else if (eventinfo->value == VALUE_KEY_LTCLICK)
|
||||
{
|
||||
living_platform_rawdata_awss_reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(OTA_ENABLED) && defined(BUILD_AOS)
|
||||
static ota_service_t ctx = {0};
|
||||
static bool ota_service_inited = false;
|
||||
|
||||
void *living_platform_rawdata_entry_get_uota_ctx(void)
|
||||
{
|
||||
if (ota_service_inited == true)
|
||||
{
|
||||
return (void *)&ctx;
|
||||
}
|
||||
else
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
static int ota_init(void)
|
||||
{
|
||||
char product_key[PRODUCT_KEY_LEN + 1] = {0};
|
||||
char device_name[DEVICE_NAME_LEN + 1] = {0};
|
||||
char device_secret[DEVICE_SECRET_LEN + 1] = {0};
|
||||
HAL_GetProductKey(product_key);
|
||||
HAL_GetDeviceName(device_name);
|
||||
HAL_GetDeviceSecret(device_secret);
|
||||
memset(&ctx, 0, sizeof(ota_service_t));
|
||||
strncpy(ctx.pk, product_key, sizeof(ctx.pk) - 1);
|
||||
strncpy(ctx.dn, device_name, sizeof(ctx.dn) - 1);
|
||||
strncpy(ctx.ds, device_secret, sizeof(ctx.ds) - 1);
|
||||
ctx.trans_protcol = 0;
|
||||
ctx.dl_protcol = 3;
|
||||
ota_service_init(&ctx);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int mqtt_connected_event_handler(void)
|
||||
{
|
||||
#if defined(OTA_ENABLED) && defined(BUILD_AOS)
|
||||
if (ota_service_inited == true)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
LOG("MQTT reconnected, let's redo OTA upgrade");
|
||||
if ((ctx.h_tr) && (ctx.h_tr->upgrade))
|
||||
{
|
||||
LOG("Redoing OTA upgrade");
|
||||
ret = ctx.h_tr->upgrade(&ctx);
|
||||
if (ret < 0)
|
||||
LOG("Failed to do OTA upgrade");
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
LOG("MQTT Construct OTA start to inform");
|
||||
#ifdef DEV_OFFLINE_OTA_ENABLE
|
||||
ota_service_inform(&ctx);
|
||||
#else
|
||||
ota_init();
|
||||
#endif
|
||||
ota_service_inited = true;
|
||||
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void load_device_meta_info(void)
|
||||
{
|
||||
int len = 0;
|
||||
char key_buf[MAX_KEY_LEN];
|
||||
char product_key[PRODUCT_KEY_LEN + 1] = {0};
|
||||
char product_secret[PRODUCT_SECRET_LEN + 1] = {0};
|
||||
char device_name[DEVICE_NAME_LEN + 1] = {0};
|
||||
char device_secret[DEVICE_SECRET_LEN + 1] = {0};
|
||||
|
||||
len = PRODUCT_KEY_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(product_key, 0, sizeof(product_key));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PK, 0);
|
||||
HAL_Kv_Get(key_buf, product_key, &len);
|
||||
|
||||
len = PRODUCT_SECRET_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(product_secret, 0, sizeof(product_secret));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PS, 0);
|
||||
HAL_Kv_Get(key_buf, product_secret, &len);
|
||||
|
||||
len = DEVICE_NAME_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(device_name, 0, sizeof(device_name));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DN, 0);
|
||||
HAL_Kv_Get(key_buf, device_name, &len);
|
||||
|
||||
len = DEVICE_SECRET_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(device_secret, 0, sizeof(device_secret));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DS, 0);
|
||||
HAL_Kv_Get(key_buf, device_secret, &len);
|
||||
|
||||
if ((strlen(product_key) > 0) && (strlen(product_secret) > 0) && (strlen(device_name) > 0))
|
||||
{
|
||||
HAL_SetProductKey(product_key);
|
||||
HAL_SetProductSecret(product_secret);
|
||||
HAL_SetDeviceName(device_name);
|
||||
HAL_SetDeviceSecret(device_secret);
|
||||
printf("pk[%s]\r\n", product_key);
|
||||
printf("dn[%s]\r\n", device_name);
|
||||
}
|
||||
else
|
||||
{
|
||||
HAL_SetProductKey(PRODUCT_KEY);
|
||||
HAL_SetProductSecret(PRODUCT_SECRET);
|
||||
HAL_SetDeviceName(DEVICE_NAME);
|
||||
HAL_SetDeviceSecret(DEVICE_SECRET);
|
||||
printf("pk[%s]\r\n", PRODUCT_KEY);
|
||||
printf("dn[%s]\r\n", DEVICE_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef POWER_CYCLE_AWSS
|
||||
|
||||
#ifndef DEFAULT_CYCLE_TIMEOUT
|
||||
#define DEFAULT_CYCLE_TIMEOUT 5000
|
||||
#endif
|
||||
static int network_start(void)
|
||||
{
|
||||
netmgr_ap_config_t my_config = {0};
|
||||
netmgr_get_ap_config(&my_config);
|
||||
if (strlen(my_config.ssid))
|
||||
{
|
||||
aos_task_new("netmgr_start", living_platform_rawdata_start_netmgr, NULL, 5120);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("INFO: %s no valied ap confing\r\n", __func__);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int check_and_start_awss(void)
|
||||
{
|
||||
int ret, len = 0;
|
||||
int awss_type = 0; /* 0 -- unknown, 1 -- dev_ap, 2 -- one key */
|
||||
char value_string[8];
|
||||
netmgr_ap_config_t my_config = {0};
|
||||
|
||||
memset(value_string, 0, sizeof(value_string));
|
||||
len = sizeof(value_string);
|
||||
/* check awss type */
|
||||
ret = aos_kv_get("awss_type", value_string, &len);
|
||||
if (0 == strncmp("dev_ap", value_string, strlen("dev_ap")))
|
||||
{
|
||||
printf("INFO: %s awss type: device ap\r\n", __func__);
|
||||
living_platform_rawdata_do_awss_dev_ap();
|
||||
awss_type = 1;
|
||||
}
|
||||
else if (0 == strncmp("one_key", value_string, strlen("dev_ap")))
|
||||
{
|
||||
printf("INFO: %s awss type: one key\r\n", __func__);
|
||||
living_platform_rawdata_do_awss();
|
||||
living_platform_rawdata_do_awss_active();
|
||||
awss_type = 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("WARN: %s awss type: unknown\r\n", __func__);
|
||||
awss_type = 0;
|
||||
}
|
||||
/* Clesr awss type */
|
||||
ret = aos_kv_del("awss_type");
|
||||
return awss_type;
|
||||
}
|
||||
|
||||
static void power_cycle_check(void *p)
|
||||
{
|
||||
int ret = -1;
|
||||
int count;
|
||||
|
||||
if (NULL == p)
|
||||
{
|
||||
printf("ERR: %s data pointer is NULL\r\n", __func__);
|
||||
return;
|
||||
}
|
||||
count = *(int *)p;
|
||||
printf("INFO: %s power cycle %d timeout clear\r\n", __func__, count);
|
||||
/* Reset cycle count */
|
||||
ret = aos_kv_set("power_cycle_count", "1", strlen("1") + 1, 1);
|
||||
if (0 != ret)
|
||||
{
|
||||
printf("ERR: %s aos_kv_set return: %d\r\n", __func__, ret);
|
||||
return;
|
||||
}
|
||||
if (count == 3)
|
||||
{ /* Three times for device ap awss */
|
||||
ret = aos_kv_set("awss_type", "dev_ap", strlen("dev_ap") + 1, 1);
|
||||
printf("INFO: %s prepare for device soft ap awss\r\n", __func__);
|
||||
aos_schedule_call(living_platform_rawdata_awss_reset, NULL);
|
||||
}
|
||||
else if (count == 5)
|
||||
{ /* Five times for one key awss */
|
||||
aos_kv_set("awss_type", "one_key", strlen("one_key") + 1, 1);
|
||||
printf("INFO: %s prepare for smart config awss\r\n", __func__);
|
||||
aos_schedule_call(living_platform_rawdata_awss_reset, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
static int power_cycle_awss(void)
|
||||
{
|
||||
int ret, len = 0;
|
||||
int awss_type = 0;
|
||||
int cycle_count = 0;
|
||||
char value_string[8];
|
||||
|
||||
memset(value_string, 0, sizeof(value_string));
|
||||
len = sizeof(value_string);
|
||||
|
||||
/* read stored cycle value */
|
||||
ret = aos_kv_get("power_cycle_count", value_string, &len);
|
||||
if (0 != ret)
|
||||
{
|
||||
printf("INFO: %s init power cycle count\r\n", __func__);
|
||||
snprintf(value_string, sizeof(value_string), "%d", 1); /* Set initial value */
|
||||
ret = aos_kv_set("power_cycle_count", value_string, strlen(value_string), 1);
|
||||
if (0 != ret)
|
||||
{
|
||||
printf("ERR: %s, aos_kv_set return: %d\r\n", __func__, ret);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cycle_count = atoi(value_string);
|
||||
printf("INFO: %s power cycle count: %d\r\n", __func__, cycle_count);
|
||||
memset(value_string, 0, sizeof(value_string));
|
||||
len = sizeof(value_string);
|
||||
if (cycle_count == 1)
|
||||
{ /* AWSS always start after a timeout clear reboot */
|
||||
awss_type = check_and_start_awss();
|
||||
snprintf(value_string, sizeof(value_string), "%d", 1);
|
||||
}
|
||||
if (cycle_count > 4)
|
||||
{ /* Cycle count max value is 5 */
|
||||
snprintf(value_string, sizeof(value_string), "%d", 1);
|
||||
}
|
||||
else
|
||||
{ /* Increase cycle count */
|
||||
snprintf(value_string, sizeof(value_string), "%d", cycle_count + 1);
|
||||
}
|
||||
if (0 == awss_type)
|
||||
{ /* Do not store cycle count when doing awss */
|
||||
ret = aos_kv_set("power_cycle_count", value_string, strlen(value_string) + 1, 1);
|
||||
aos_post_delayed_action(DEFAULT_CYCLE_TIMEOUT, power_cycle_check, (void *)(&cycle_count));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef MANUFACT_AP_FIND_ENABLE
|
||||
void manufact_ap_find_process(int result)
|
||||
{
|
||||
// Informed manufact ap found or not.
|
||||
// If manufact ap found, lower layer will auto connect the manufact ap
|
||||
// IF manufact ap not found, lower layer will enter normal awss state
|
||||
if (result == 0)
|
||||
{
|
||||
LOG("%s ap found.\n", __func__);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG("%s ap not found.\n", __func__);
|
||||
}
|
||||
}
|
||||
|
||||
int check_manufacture_ap_info(void)
|
||||
{
|
||||
uint8_t m_manu_flag = 0;
|
||||
int m_manu_flag_len = sizeof(m_manu_flag);
|
||||
aos_kv_get(MANUAP_CONTROL_KEY, &m_manu_flag, &m_manu_flag_len);
|
||||
if (m_manu_flag)
|
||||
{
|
||||
netmgr_manuap_info_set("TEST_AP", "TEST_PASSWORD", manufact_ap_find_process);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static void show_firmware_version(void)
|
||||
{
|
||||
printf("\n--------Firmware info--------");
|
||||
printf("\napp: %s, board: %s", APP_NAME, PLATFORM);
|
||||
#ifdef DEBUG
|
||||
printf("\nHost: %s", COMPILE_HOST);
|
||||
#endif
|
||||
printf("\nBranch: %s", GIT_BRANCH);
|
||||
printf("\nHash: %s", GIT_HASH);
|
||||
printf("\nDate: %s %s", __DATE__, __TIME__);
|
||||
printf("\nKernel: %s", aos_get_kernel_version());
|
||||
printf("\nLinkKit: %s", LINKKIT_VERSION);
|
||||
printf("\nAPP ver: %s", aos_get_app_version());
|
||||
|
||||
printf("\nRegion env: %s\n\n", REGION_ENV_STRING);
|
||||
}
|
||||
|
||||
int application_start(int argc, char **argv)
|
||||
{
|
||||
int len = 0;
|
||||
char interval[9] = {0};
|
||||
|
||||
#ifdef CSP_LINUXHOST
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
#endif
|
||||
|
||||
#ifdef WITH_SAL
|
||||
sal_init();
|
||||
#endif
|
||||
|
||||
#ifdef MDAL_MAL_ICA_TEST
|
||||
HAL_MDAL_MAL_Init();
|
||||
#endif
|
||||
|
||||
#ifdef DEFAULT_LOG_LEVEL_DEBUG
|
||||
IOT_SetLogLevel(IOT_LOG_DEBUG);
|
||||
#else
|
||||
#ifdef CSP_LINUXHOST
|
||||
IOT_SetLogLevel(IOT_LOG_DEBUG);
|
||||
#else
|
||||
IOT_SetLogLevel(IOT_LOG_INFO);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
show_firmware_version();
|
||||
load_device_meta_info();
|
||||
|
||||
#ifdef DEV_OFFLINE_OTA_ENABLE
|
||||
ota_init();
|
||||
#endif
|
||||
|
||||
netmgr_init();
|
||||
|
||||
#ifdef MANUFACT_AP_FIND_ENABLE
|
||||
check_manufacture_ap_info();
|
||||
#endif
|
||||
|
||||
#ifdef DEV_ERRCODE_ENABLE
|
||||
dev_diagnosis_module_init();
|
||||
#endif
|
||||
|
||||
aos_register_event_filter(EV_KEY, user_key_process, NULL);
|
||||
aos_register_event_filter(EV_WIFI, wifi_service_event, NULL);
|
||||
aos_register_event_filter(EV_YUNIO, cloud_service_event, NULL);
|
||||
|
||||
IOT_RegisterCallback(ITE_MQTT_CONNECT_SUCC, mqtt_connected_event_handler);
|
||||
|
||||
#ifdef CONFIG_AOS_CLI
|
||||
living_platform_rawdata_register_cmds();
|
||||
#endif
|
||||
|
||||
#ifdef POWER_CYCLE_AWSS
|
||||
network_start();
|
||||
power_cycle_awss();
|
||||
#else
|
||||
aos_task_new("netmgr_start", living_platform_rawdata_start_netmgr, NULL, 5120);
|
||||
#endif
|
||||
|
||||
//This code just for debug
|
||||
len = sizeof(int);
|
||||
if (0 == HAL_Kv_Get(LIVING_PLATFORM_RAWDATA_DUMP_INTERVAL_KV_KEY, interval, &len))
|
||||
{
|
||||
living_platform_rawdata_duration_interval = atoi(interval);
|
||||
|
||||
if (living_platform_rawdata_duration_interval > 0)
|
||||
{
|
||||
aos_post_delayed_action(living_platform_rawdata_duration_interval, living_platform_rawdata_duration_work, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
aos_loop_run();
|
||||
|
||||
return 0;
|
||||
}
|
||||
9
Products/example/living_platform_rawdata/app_entry.h
Normal file
9
Products/example/living_platform_rawdata/app_entry.h
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
#ifndef __LIVING_PLATFORM_RAWDATA_ENTRY_H__
|
||||
#define __LIVING_PLATFORM_RAWDATA_ENTRY_H__
|
||||
|
||||
#define LIVING_PLATFORM_RAWDATA_MAIN_THREAD_STACKSZIE (6 * 1024)
|
||||
#define LIVING_PLATFORM_RAWDATA_DUMP_INTERVAL_KV_KEY "lpdump"
|
||||
|
||||
extern void linkkit_event_monitor(int event);
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,96 @@
|
|||
var COMMAND_REPORT = 0x01; //属性上报
|
||||
var COMMAND_SET = 0x02; //属性设置
|
||||
|
||||
var ALINK_PROP_REPORT_METHOD = 'thing.event.property.post'; //标准ALink JSON格式topic, 设备上传属性数据到云端
|
||||
var ALINK_PROP_SET_METHOD = 'thing.service.property.set'; //标准ALink JSON格式topic, 云端下发属性控制指令到设备端
|
||||
var ALINK_PROP_SET_REPLY_METHOD = 'thing.service.property.set'; //标准ALink JSON格式topic, 设备上报属性设置的结果到云端
|
||||
/*
|
||||
示例数据:
|
||||
设备上报数据
|
||||
传入参数 ->
|
||||
0x010000006400
|
||||
输出结果 ->
|
||||
{"method":"thing.event.property.post","id":"100","params":{"LightSwitch":0},"version":"1.0"}
|
||||
*/
|
||||
function rawDataToProtocol(bytes) {
|
||||
var uint8Array = new Uint8Array(bytes.length);
|
||||
for (var i = 0; i < bytes.length; i++) {
|
||||
uint8Array[i] = bytes[i] & 0xff;
|
||||
}
|
||||
var dataView = new DataView(uint8Array.buffer, 0);
|
||||
var jsonMap = new Object();
|
||||
var fHead = uint8Array[0]; // command
|
||||
if (fHead == COMMAND_REPORT) {
|
||||
jsonMap['method'] = ALINK_PROP_REPORT_METHOD; //ALink JSON格式 - 属性上报topic
|
||||
jsonMap['version'] = '1.0'; //ALink JSON格式 - 协议版本号固定字段
|
||||
jsonMap['id'] = '' + dataView.getInt32(1); //ALink JSON格式 - 标示该次请求id值
|
||||
var params = {};
|
||||
params['LightSwitch'] = dataView.getInt8(5); //对应产品属性中 prop_int16
|
||||
jsonMap['params'] = params; //ALink JSON格式 - params标准字段
|
||||
}
|
||||
|
||||
return jsonMap;
|
||||
}
|
||||
/*
|
||||
示例数据:
|
||||
属性设置
|
||||
传入参数 ->
|
||||
{"method":"thing.service.property.set","id":"100","version":"1.0","params":{"LightSwitch":0}}
|
||||
输出结果 ->
|
||||
0x020000006400
|
||||
*/
|
||||
function protocolToRawData(json) {
|
||||
var method = json['method'];
|
||||
var id = json['id'];
|
||||
var version = json['version'];
|
||||
var payloadArray = [];
|
||||
if (method == ALINK_PROP_SET_METHOD) // 属性设置
|
||||
{
|
||||
var params = json['params'];
|
||||
var light_switch = params['LightSwitch'];
|
||||
|
||||
//按照自定义协议格式拼接 rawData
|
||||
payloadArray = payloadArray.concat(buffer_uint8(COMMAND_SET)); // command字段
|
||||
payloadArray = payloadArray.concat(buffer_int32(parseInt(id))); // ALink JSON格式 'id'
|
||||
payloadArray = payloadArray.concat(buffer_uint8(light_switch)); // 属性'light_switch'的值
|
||||
}
|
||||
|
||||
return payloadArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将设备自定义topic数据转换为json格式数据, 设备上报数据到物联网平台时调用
|
||||
* 入参:topic 字符串,设备上报消息的topic
|
||||
* 入参:rawData byte[]数组 不能为空
|
||||
* 出参:jsonObj 对象 不能为空
|
||||
*/
|
||||
function transformPayload(topic, rawData) {
|
||||
jsonObj = {}
|
||||
return jsonObj
|
||||
}
|
||||
|
||||
//以下是部分辅助函数
|
||||
function buffer_uint8(value) {
|
||||
var uint8Array = new Uint8Array(1);
|
||||
var dv = new DataView(uint8Array.buffer, 0);
|
||||
dv.setUint8(0, value);
|
||||
return [].slice.call(uint8Array);
|
||||
}
|
||||
function buffer_int16(value) {
|
||||
var uint8Array = new Uint8Array(2);
|
||||
var dv = new DataView(uint8Array.buffer, 0);
|
||||
dv.setInt16(0, value);
|
||||
return [].slice.call(uint8Array);
|
||||
}
|
||||
function buffer_int32(value) {
|
||||
var uint8Array = new Uint8Array(4);
|
||||
var dv = new DataView(uint8Array.buffer, 0);
|
||||
dv.setInt32(0, value);
|
||||
return [].slice.call(uint8Array);
|
||||
}
|
||||
function buffer_float32(value) {
|
||||
var uint8Array = new Uint8Array(4);
|
||||
var dv = new DataView(uint8Array.buffer, 0);
|
||||
dv.setFloat32(0, value);
|
||||
return [].slice.call(uint8Array);
|
||||
}
|
||||
Binary file not shown.
|
|
@ -0,0 +1,134 @@
|
|||
NAME := living_platform_rawdata
|
||||
|
||||
# Cloud server region: MAINLAND or SINGAPORE, GERMANY
|
||||
CONFIG_SERVER_REGION ?= MAINLAND
|
||||
|
||||
# Cloud server env: ON_DAILY, ON_PRE, ONLINE
|
||||
CONFIG_SERVER_ENV ?= ONLINE
|
||||
|
||||
$(NAME)_SOURCES := living_platform_rawdata_main.c \
|
||||
living_platform_rawdata_cmds.c \
|
||||
living_platform_rawdata_ut.c \
|
||||
app_entry.c
|
||||
|
||||
# support MCU OTA, default DISABLE
|
||||
CONFIG_SUPPORT_MCU_OTA ?= DISABLE
|
||||
|
||||
CONFIG_FIRMWARE_VERSION = app-1.6.0
|
||||
|
||||
# Compile date and time
|
||||
ifeq ("${DATE}", "")
|
||||
DATE=date
|
||||
endif
|
||||
|
||||
CONFIG_COMPILE_DATE ?= $(shell ${DATE} +%Y%m%d.%H%M%S)
|
||||
|
||||
# Firmware type: 0 - release, 1 - debug
|
||||
CONFIG_DEBUG ?= 0
|
||||
|
||||
$(NAME)_COMPONENTS += framework/protocol/linkkit/sdk \
|
||||
framework/protocol/linkkit/hal \
|
||||
framework/netmgr \
|
||||
framework/common \
|
||||
utility/cjson \
|
||||
framework/uOTA
|
||||
|
||||
GLOBAL_INCLUDES += ../../../../framework/protocol/linkkit/include \
|
||||
../../../../framework/protocol/linkkit/include/imports \
|
||||
../../../../framework/protocol/linkkit/include/exports \
|
||||
|
||||
|
||||
GLOBAL_CFLAGS += -DCONFIG_YWSS \
|
||||
-DBUILD_AOS \
|
||||
-DAWSS_SUPPORT_STATIS
|
||||
|
||||
ifeq ($(CONFIG_SUPPORT_MCU_OTA), ENABLE)
|
||||
GLOBAL_CFLAGS += -DSUPPORT_MCU_OTA
|
||||
endif
|
||||
|
||||
GLOBAL_CFLAGS += -DREGION_${CONFIG_SERVER_REGION}
|
||||
|
||||
GLOBAL_CFLAGS += -D${CONFIG_SERVER_ENV}
|
||||
|
||||
ifeq ($(HOST_MCU_FAMILY),esp8266)
|
||||
GLOBAL_CFLAGS += -DPOWER_CYCLE_AWSS
|
||||
GLOBAL_DEFINES += ESP8266_CHIPSET
|
||||
else
|
||||
$(NAME)_COMPONENTS += cli
|
||||
GLOBAL_CFLAGS += -DCONFIG_AOS_CLI
|
||||
|
||||
#Create thread of cm_yield in mqtt_client.c
|
||||
GLOBAL_CFLAGS += -DCONFIG_SDK_THREAD_COST=1
|
||||
|
||||
GLOBAL_CFLAGS += -DDEV_ERRCODE_ENABLE \
|
||||
-DDEV_OFFLINE_OTA_ENABLE \
|
||||
-DAWSS_BATCH_DEVAP_ENABLE \
|
||||
-DMANUFACT_AP_FIND_ENABLE
|
||||
|
||||
#GLOBAL_CFLAGS += -DDEV_OFFLINE_SECURE_OTA_ENABLE
|
||||
|
||||
ifneq ("$(PLATFORM)", "hf-lpt230")
|
||||
GLOBAL_CFLAGS += -DDEV_OFFLINE_LOG_ENABLE
|
||||
endif
|
||||
endif
|
||||
|
||||
BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
|
||||
GLOBAL_CFLAGS += -DGIT_BRANCH=\"${BRANCH}\"
|
||||
COMMIT_HASH := $(shell git rev-parse HEAD)
|
||||
GLOBAL_CFLAGS += -DGIT_HASH=\"${COMMIT_HASH}\"
|
||||
COMPILE_USER := ${USER}
|
||||
GLOBAL_CFLAGS += -DCOMPILE_HOST=\"${COMPILE_USER}\"
|
||||
SERVER_CONF_STRING := "${CONFIG_SERVER_REGION}-${CONFIG_SERVER_ENV}"
|
||||
GLOBAL_CFLAGS += -DREGION_ENV_STRING=\"${SERVER_CONF_STRING}\"
|
||||
GLOBAL_CFLAGS += -DAPP_NAME=\"${APP}\"
|
||||
GLOBAL_CFLAGS += -DPLATFORM=\"${PLATFORM}\"
|
||||
CONFIG_SYSINFO_APP_VERSION = ${CONFIG_FIRMWARE_VERSION}-${CONFIG_COMPILE_DATE}
|
||||
GLOBAL_CFLAGS += -DSYSINFO_APP_VERSION=\"$(CONFIG_SYSINFO_APP_VERSION)\"
|
||||
|
||||
ifneq ("$(BOARD)", "")
|
||||
$(info server region: ${CONFIG_SERVER_REGION})
|
||||
$(info server env: ${CONFIG_SERVER_ENV})
|
||||
$(info APP: ${APP} Board: ${PLATFORM})
|
||||
$(info host user: ${COMPILE_USER})
|
||||
$(info branch: ${BRANCH})
|
||||
$(info hash: ${COMMIT_HASH})
|
||||
$(info app_version_new:${CONFIG_SYSINFO_APP_VERSION})
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_DEBUG), 1)
|
||||
GLOBAL_CFLAGS += -DDEFAULT_LOG_LEVEL_DEBUG
|
||||
GLOBAL_CFLAGS += -DCONFIG_BLDTIME_MUTE_DBGLOG=0
|
||||
$(info firmware type: DEBUG)
|
||||
else
|
||||
GLOBAL_CFLAGS += -DCONFIG_BLDTIME_MUTE_DBGLOG=1
|
||||
$(info firmware type: RELEASE)
|
||||
endif
|
||||
|
||||
ifeq ($(LWIP),1)
|
||||
$(NAME)_COMPONENTS += protocols.net
|
||||
no_with_lwip := 0
|
||||
endif
|
||||
|
||||
GLOBAL_INCLUDES += ./
|
||||
|
||||
include ./make.settings
|
||||
|
||||
SWITCH_VARS := FEATURE_MQTT_COMM_ENABLED FEATURE_ALCS_ENABLED FEATURE_DEVICE_MODEL_ENABLED \
|
||||
FEATURE_DEVICE_MODEL_GATEWAY FEATURE_DEV_BIND_ENABLED \
|
||||
FEATURE_DEVICE_MODEL_RAWDATA_SOLO FEATURE_COAP_COMM_ENABLED FEATURE_HTTP2_COMM_ENABLED \
|
||||
FEATURE_HTTP_COMM_ENABLED FEATURE_SAL_ENABLED FEATURE_WIFI_PROVISION_ENABLED FEATURE_AWSS_SUPPORT_SMARTCONFIG\
|
||||
FEATURE_AWSS_SUPPORT_ZEROCONFIG FEATURE_AWSS_SUPPORT_SMARTCONFIG FEATURE_AWSS_SUPPORT_ZEROCONFIG FEATURE_AWSS_SUPPORT_PHONEASAP \
|
||||
FEATURE_AWSS_SUPPORT_ROUTER FEATURE_AWSS_SUPPORT_DEV_AP FEATURE_OTA_ENABLED FEATURE_MQTT_AUTO_SUBSCRIBE FEATURE_MQTT_PREAUTH_SUPPORT_HTTPS_CDN \
|
||||
FEATURE_DM_UNIFIED_SERVICE_POST
|
||||
|
||||
$(foreach v, \
|
||||
$(SWITCH_VARS), \
|
||||
$(if $(filter y,$($(v))), \
|
||||
$(eval GLOBAL_DEFINES += $(subst FEATURE_,,$(v)))) \
|
||||
)
|
||||
|
||||
$(foreach v, \
|
||||
$(SWITCH_VARS), \
|
||||
$(if $(filter y,$($(v))), \
|
||||
$(eval SDK_DEFINES += $(subst FEATURE_,-D,$(v)))) \
|
||||
)
|
||||
|
|
@ -0,0 +1,505 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2019 Alibaba Group Holding Limited
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <aos/aos.h>
|
||||
#include <aos/yloop.h>
|
||||
#include "netmgr.h"
|
||||
#include "iot_export.h"
|
||||
#include "iot_import.h"
|
||||
#include "aos/kv.h"
|
||||
|
||||
#include <k_api.h>
|
||||
|
||||
#include "living_platform_rawdata_main.h"
|
||||
#include "app_entry.h"
|
||||
#include "living_platform_rawdata_cmds.h"
|
||||
#include "living_platform_rawdata_ut.h"
|
||||
|
||||
static char awss_running = 0;
|
||||
|
||||
#ifdef AWSS_BATCH_DEVAP_ENABLE
|
||||
#define DEV_AP_ZCONFIG_TIMEOUT_MS 120000 // (ms)
|
||||
extern void awss_set_config_press(uint8_t press);
|
||||
extern uint8_t awss_get_config_press(void);
|
||||
extern void zconfig_80211_frame_filter_set(uint8_t filter, uint8_t fix_channel);
|
||||
|
||||
static aos_timer_t dev_ap_zconfig_timeout_timer;
|
||||
static uint8_t g_dev_ap_zconfig_timer = 0; // this timer create once and can restart
|
||||
static uint8_t g_dev_ap_zconfig_run = 0;
|
||||
|
||||
static void timer_func_devap_zconfig_timeout(void *arg1, void *arg2)
|
||||
{
|
||||
living_platform_rawdata_info("%s run\n", __func__);
|
||||
|
||||
if (awss_get_config_press())
|
||||
{
|
||||
// still in zero wifi provision stage, should stop and switch to dev ap
|
||||
living_platform_rawdata_do_awss_dev_ap();
|
||||
}
|
||||
else
|
||||
{
|
||||
// zero wifi provision finished
|
||||
}
|
||||
|
||||
awss_set_config_press(0);
|
||||
zconfig_80211_frame_filter_set(0xFF, 0xFF);
|
||||
g_dev_ap_zconfig_run = 0;
|
||||
aos_timer_stop(&dev_ap_zconfig_timeout_timer);
|
||||
}
|
||||
|
||||
static void awss_dev_ap_switch_to_zeroconfig(void *p)
|
||||
{
|
||||
living_platform_rawdata_info("%s run\n", __func__);
|
||||
// Stop dev ap wifi provision
|
||||
awss_dev_ap_stop();
|
||||
// Start and enable zero wifi provision
|
||||
iotx_event_regist_cb(linkkit_event_monitor);
|
||||
awss_set_config_press(1);
|
||||
|
||||
// Start timer to count duration time of zero provision timeout
|
||||
if (!g_dev_ap_zconfig_timer)
|
||||
{
|
||||
aos_timer_new(&dev_ap_zconfig_timeout_timer, timer_func_devap_zconfig_timeout, NULL, DEV_AP_ZCONFIG_TIMEOUT_MS, 0);
|
||||
g_dev_ap_zconfig_timer = 1;
|
||||
}
|
||||
aos_timer_start(&dev_ap_zconfig_timeout_timer);
|
||||
|
||||
// This will hold thread, when awss is going
|
||||
netmgr_start(true);
|
||||
|
||||
living_platform_rawdata_info("%s exit\n", __func__);
|
||||
aos_task_exit(0);
|
||||
}
|
||||
|
||||
int awss_dev_ap_modeswitch_cb(uint8_t awss_new_mode, uint8_t new_mode_timeout, uint8_t fix_channel)
|
||||
{
|
||||
if ((awss_new_mode == 0) && !g_dev_ap_zconfig_run)
|
||||
{
|
||||
g_dev_ap_zconfig_run = 1;
|
||||
// Only receive zero provision packets
|
||||
zconfig_80211_frame_filter_set(0x00, fix_channel);
|
||||
living_platform_rawdata_info("switch to awssmode %d, mode_timeout %d, chan %d\n", 0x00, new_mode_timeout, fix_channel);
|
||||
// switch to zero config
|
||||
aos_task_new("devap_to_zeroconfig", awss_dev_ap_switch_to_zeroconfig, NULL, 2048);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static void awss_close_dev_ap(void *p)
|
||||
{
|
||||
awss_dev_ap_stop();
|
||||
living_platform_rawdata_info("%s exit\n", __func__);
|
||||
aos_task_exit(0);
|
||||
}
|
||||
|
||||
static void awss_open_dev_ap(void *p)
|
||||
{
|
||||
iotx_event_regist_cb(linkkit_event_monitor);
|
||||
living_platform_rawdata_info("%s\n", __func__);
|
||||
if (netmgr_start(false) != 0)
|
||||
{
|
||||
aos_msleep(2000);
|
||||
#ifdef AWSS_BATCH_DEVAP_ENABLE
|
||||
awss_dev_ap_reg_modeswit_cb(awss_dev_ap_modeswitch_cb);
|
||||
#endif
|
||||
awss_dev_ap_start();
|
||||
}
|
||||
aos_task_exit(0);
|
||||
}
|
||||
|
||||
static void stop_netmgr(void *p)
|
||||
{
|
||||
awss_stop();
|
||||
living_platform_rawdata_info("%s\n", __func__);
|
||||
aos_task_exit(0);
|
||||
}
|
||||
|
||||
void living_platform_rawdata_start_netmgr(void *p)
|
||||
{
|
||||
iotx_event_regist_cb(linkkit_event_monitor);
|
||||
netmgr_start(true);
|
||||
aos_task_exit(0);
|
||||
}
|
||||
|
||||
void living_platform_rawdata_do_awss_active(void)
|
||||
{
|
||||
living_platform_rawdata_info("living_platform_rawdata_do_awss_active %d\n", awss_running);
|
||||
awss_running = 1;
|
||||
#ifdef WIFI_PROVISION_ENABLED
|
||||
extern int awss_config_press();
|
||||
awss_config_press();
|
||||
#endif
|
||||
}
|
||||
|
||||
void living_platform_rawdata_do_awss_dev_ap(void)
|
||||
{
|
||||
aos_task_new("netmgr_stop", stop_netmgr, NULL, 4096);
|
||||
aos_task_new("dap_open", awss_open_dev_ap, NULL, 4096);
|
||||
}
|
||||
|
||||
void living_platform_rawdata_do_awss(void)
|
||||
{
|
||||
aos_task_new("dap_close", awss_close_dev_ap, NULL, 2048);
|
||||
aos_task_new("netmgr_start", living_platform_rawdata_start_netmgr, NULL, 4096);
|
||||
}
|
||||
|
||||
static void linkkit_reset(void *p)
|
||||
{
|
||||
iotx_sdk_reset_local();
|
||||
HAL_Reboot();
|
||||
}
|
||||
|
||||
void living_platform_rawdata_awss_reset(void)
|
||||
{
|
||||
#ifdef WIFI_PROVISION_ENABLED
|
||||
aos_task_new("reset", (void (*)(void *))iotx_sdk_reset, NULL, 4096); // stack taken by iTLS is more than taken by TLS.
|
||||
#endif
|
||||
aos_post_delayed_action(2000, linkkit_reset, NULL);
|
||||
}
|
||||
|
||||
#ifdef CONFIG_AOS_CLI
|
||||
static void handle_reset_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
aos_schedule_call(living_platform_rawdata_awss_reset, NULL);
|
||||
}
|
||||
|
||||
static void handle_active_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
aos_schedule_call(living_platform_rawdata_do_awss_active, NULL);
|
||||
}
|
||||
|
||||
static void handle_dev_ap_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
aos_schedule_call(living_platform_rawdata_do_awss_dev_ap, NULL);
|
||||
}
|
||||
|
||||
static void handle_awss_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
aos_schedule_call(living_platform_rawdata_do_awss, NULL);
|
||||
}
|
||||
|
||||
#if defined(OFFLINE_LOG_UT_TEST)
|
||||
static void handle_offline_log_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
if (argc < 2)
|
||||
{
|
||||
aos_cli_printf("params err,oll [save/upload/erase] or [read [0-1]] or [write [0-3] \"test offline log\"]\r\n");
|
||||
return;
|
||||
}
|
||||
|
||||
aos_cli_printf("cmd:%s\r\n", argv[1]);
|
||||
|
||||
if (!strcmp("save", argv[1]) && argc == 2)
|
||||
{
|
||||
diagnosis_offline_log_save_all();
|
||||
}
|
||||
else if (!strcmp("upload", argv[1]) && argc == 4)
|
||||
{
|
||||
int port = atoi(argv[3]);
|
||||
|
||||
diagnosis_offline_log_upload(argv[2], port);
|
||||
}
|
||||
else if (!strcmp("write", argv[1]) && argc == 4)
|
||||
{
|
||||
int loglevel = atoi(argv[2]);
|
||||
|
||||
if (loglevel >= 0 && loglevel <= 3)
|
||||
{
|
||||
diagnosis_offline_log(loglevel, "%s\\n", argv[3]);
|
||||
}
|
||||
}
|
||||
else if (!strcmp("read", argv[1]) && argc == 3)
|
||||
{
|
||||
int log_mode = atoi(argv[2]);
|
||||
|
||||
diagnosis_offline_log_read_all(log_mode);
|
||||
}
|
||||
else if (!strcmp("erase", argv[1]) && argc == 2)
|
||||
{
|
||||
diagnosis_offline_log_erase_flash_desc();
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("cmd:%s is not support\r\n", argv[1]);
|
||||
}
|
||||
|
||||
if (ret != 0)
|
||||
{
|
||||
aos_cli_printf("handle oll cmd:%s failed:%d\r\n", argv[1], ret);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static struct cli_command offline_log_cmd = {.name = "oll",
|
||||
.help = "oll [save]/[erase] or [upload host port] or [read [0-1]] or [write [0-3] \"test offline log\"]",
|
||||
.function = handle_offline_log_cmd};
|
||||
|
||||
#endif
|
||||
|
||||
static void handle_linkkey_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
int index = 0;
|
||||
char key_buf[MAX_KEY_LEN];
|
||||
|
||||
if (argc == 1)
|
||||
{
|
||||
int len = 0;
|
||||
char product_key[PRODUCT_KEY_LEN + 1] = {0};
|
||||
char product_secret[PRODUCT_SECRET_LEN + 1] = {0};
|
||||
char device_name[DEVICE_NAME_LEN + 1] = {0};
|
||||
char device_secret[DEVICE_SECRET_LEN + 1] = {0};
|
||||
|
||||
len = PRODUCT_KEY_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(product_key, 0, sizeof(product_key));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PK, index);
|
||||
aos_kv_get(key_buf, product_key, &len);
|
||||
|
||||
len = PRODUCT_SECRET_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(product_secret, 0, sizeof(product_secret));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PS, index);
|
||||
aos_kv_get(key_buf, product_secret, &len);
|
||||
|
||||
len = DEVICE_NAME_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(device_name, 0, sizeof(device_name));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DN, index);
|
||||
aos_kv_get(key_buf, device_name, &len);
|
||||
|
||||
len = DEVICE_SECRET_LEN + 1;
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
memset(device_secret, 0, sizeof(device_secret));
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DS, index);
|
||||
aos_kv_get(key_buf, device_secret, &len);
|
||||
|
||||
aos_cli_printf("PK=%s.\r\n", product_key);
|
||||
aos_cli_printf("PS=%s.\r\n", product_secret);
|
||||
aos_cli_printf("DN=%s.\r\n", device_name);
|
||||
aos_cli_printf("DS=%s.\r\n", device_secret);
|
||||
}
|
||||
else if (argc == 5)
|
||||
{
|
||||
int devid = 0;
|
||||
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PK, devid);
|
||||
aos_kv_set(key_buf, argv[1], strlen(argv[1]) + 1, 1);
|
||||
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DN, devid);
|
||||
aos_kv_set(key_buf, argv[2], strlen(argv[2]) + 1, 1);
|
||||
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DS, devid);
|
||||
aos_kv_set(key_buf, argv[3], strlen(argv[3]) + 1, 1);
|
||||
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PS, devid);
|
||||
aos_kv_set(key_buf, argv[4], strlen(argv[4]) + 1, 1);
|
||||
|
||||
aos_cli_printf("Done\r\n");
|
||||
}
|
||||
else if (argc == 6)
|
||||
{
|
||||
int devid = atoi(argv[1]);
|
||||
|
||||
if (devid >= 0 && devid <= MAX_DEVICES_META_NUM)
|
||||
{
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PK, devid);
|
||||
aos_kv_set(key_buf, argv[2], strlen(argv[2]) + 1, 1);
|
||||
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DN, devid);
|
||||
aos_kv_set(key_buf, argv[3], strlen(argv[3]) + 1, 1);
|
||||
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_DS, devid);
|
||||
aos_kv_set(key_buf, argv[4], strlen(argv[4]) + 1, 1);
|
||||
|
||||
memset(key_buf, 0, MAX_KEY_LEN);
|
||||
HAL_Snprintf(key_buf, MAX_KEY_LEN, "%s_%d", KV_KEY_PS, devid);
|
||||
aos_kv_set(key_buf, argv[5], strlen(argv[5]) + 1, 1);
|
||||
|
||||
aos_cli_printf("Done\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("Err:devid range[0-%d]", MAX_DEVICES_META_NUM - 1);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("Params Err\r\n");
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef DEV_ERRCODE_ENABLE
|
||||
#define DEV_DIAG_CLI_SET_ERRCODE "set_err"
|
||||
#define DEV_DIAG_CLI_ERRCODE_MANUAL_MSG "errcode was manually set"
|
||||
#define DEV_DIAG_CLI_GET_ERRCODE "get_err"
|
||||
#define DEV_DIAG_CLI_DEL_ERRCODE "del_err"
|
||||
|
||||
static void handle_dev_serv_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
uint16_t err_code = 0;
|
||||
char err_msg[DEV_ERRCODE_MSG_MAX_LEN] = {0};
|
||||
uint8_t ret = 0;
|
||||
if (argc == 1)
|
||||
{
|
||||
aos_cli_printf("device diagnosis service test, please input more parameters.\r\n");
|
||||
}
|
||||
else if (argc > 1)
|
||||
{
|
||||
if ((argc == 3) && (!strcmp(argv[1], DEV_DIAG_CLI_SET_ERRCODE)))
|
||||
{
|
||||
err_code = (uint16_t)strtoul(argv[2], NULL, 0);
|
||||
ret = dev_errcode_kv_set(err_code, DEV_DIAG_CLI_ERRCODE_MANUAL_MSG);
|
||||
if (ret == 0)
|
||||
{
|
||||
aos_cli_printf("manually set err_code=%d\r\n", err_code);
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("manually set err_code failed!\r\n");
|
||||
}
|
||||
}
|
||||
else if ((argc == 2) && (!strcmp(argv[1], DEV_DIAG_CLI_GET_ERRCODE)))
|
||||
{
|
||||
ret = dev_errcode_kv_get(&err_code, err_msg);
|
||||
if (ret == 0)
|
||||
{
|
||||
aos_cli_printf("get err_code=%d, err_msg=%s\r\n", err_code, err_msg);
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("get err_code fail or no err_code found!\r\n");
|
||||
}
|
||||
}
|
||||
else if ((argc == 2) && (!strcmp(argv[1], DEV_DIAG_CLI_DEL_ERRCODE)))
|
||||
{
|
||||
dev_errcode_kv_del();
|
||||
aos_cli_printf("del err_code\r\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("Error: %d\r\n", __LINE__);
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef MANUFACT_AP_FIND_ENABLE
|
||||
#define MANUAP_CLI_OPEN "open"
|
||||
#define MANUAP_CLI_CLOSE "close"
|
||||
|
||||
static void handle_manuap_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
int ret = 0;
|
||||
uint8_t manuap_flag = 0;
|
||||
int manu_flag_len = sizeof(manuap_flag);
|
||||
if (argc == 1)
|
||||
{
|
||||
ret = aos_kv_get(MANUAP_CONTROL_KEY, &manuap_flag, &manu_flag_len);
|
||||
if (ret == 0)
|
||||
{
|
||||
if (manuap_flag)
|
||||
{
|
||||
aos_cli_printf("manuap state is open\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("manuap state is close\r\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("no manuap state stored\r\n");
|
||||
}
|
||||
}
|
||||
else if (argc == 2)
|
||||
{
|
||||
if (!strcmp(argv[1], MANUAP_CLI_OPEN))
|
||||
{
|
||||
manuap_flag = 1;
|
||||
ret = aos_kv_set(MANUAP_CONTROL_KEY, &manuap_flag, sizeof(manuap_flag), 1);
|
||||
if (ret == 0)
|
||||
{
|
||||
aos_cli_printf("manuap opened\r\n");
|
||||
}
|
||||
}
|
||||
else if (!strcmp(argv[1], MANUAP_CLI_CLOSE))
|
||||
{
|
||||
manuap_flag = 0;
|
||||
ret = aos_kv_set(MANUAP_CONTROL_KEY, &manuap_flag, sizeof(manuap_flag), 1);
|
||||
if (ret == 0)
|
||||
{
|
||||
aos_cli_printf("manuap closed\r\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("param input err\r\n");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
aos_cli_printf("param input err\r\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static struct cli_command resetcmd = {.name = "reset",
|
||||
.help = "factory reset",
|
||||
.function = handle_reset_cmd};
|
||||
|
||||
static struct cli_command awss_enable_cmd = {.name = "active_awss",
|
||||
.help = "active_awss [start]",
|
||||
.function = handle_active_cmd};
|
||||
static struct cli_command awss_dev_ap_cmd = {.name = "dev_ap",
|
||||
.help = "awss_dev_ap [start]",
|
||||
.function = handle_dev_ap_cmd};
|
||||
static struct cli_command awss_cmd = {.name = "awss",
|
||||
.help = "awss [start]",
|
||||
.function = handle_awss_cmd};
|
||||
static struct cli_command linkkeycmd = {.name = "linkkey",
|
||||
.help = "set/get linkkit keys. linkkey [devid] [<Product Key> <Device Name> <Device Secret> <Product Secret>]",
|
||||
.function = handle_linkkey_cmd};
|
||||
|
||||
#ifdef DEV_ERRCODE_ENABLE
|
||||
static struct cli_command dev_service_cmd = {.name = "dev_serv",
|
||||
.help = "device diagnosis service tests. like errcode, and log report",
|
||||
.function = handle_dev_serv_cmd};
|
||||
#endif
|
||||
#ifdef MANUFACT_AP_FIND_ENABLE
|
||||
static struct cli_command manuap_cmd = {.name = "manuap",
|
||||
.help = "manuap [open] or manuap [close]",
|
||||
.function = handle_manuap_cmd};
|
||||
#endif
|
||||
|
||||
int living_platform_rawdata_register_cmds(void)
|
||||
{
|
||||
aos_cli_register_command(&resetcmd);
|
||||
aos_cli_register_command(&awss_enable_cmd);
|
||||
aos_cli_register_command(&awss_dev_ap_cmd);
|
||||
aos_cli_register_command(&awss_cmd);
|
||||
aos_cli_register_command(&linkkeycmd);
|
||||
#ifdef MANUFACT_AP_FIND_ENABLE
|
||||
aos_cli_register_command(&manuap_cmd);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
#ifndef __LIVING_PLATFORM_RAWDATA_CMDS_H__
|
||||
#define __LIVING_PLATFORM_RAWDATA_CMDS_H__
|
||||
|
||||
#define MAX_DEVICES_META_NUM (0)
|
||||
|
||||
#ifdef MANUFACT_AP_FIND_ENABLE
|
||||
#define MANUAP_CONTROL_KEY "manuap"
|
||||
#endif
|
||||
|
||||
extern int living_platform_rawdata_register_cmds(void);
|
||||
extern void living_platform_rawdata_start_netmgr(void *p);
|
||||
extern void living_platform_rawdata_do_awss_dev_ap(void);
|
||||
extern void living_platform_rawdata_awss_reset(void);
|
||||
extern void living_platform_rawdata_do_awss_active(void);
|
||||
extern void living_platform_rawdata_do_awss(void);
|
||||
#endif
|
||||
|
|
@ -0,0 +1,435 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2019 Alibaba Group Holding Limited
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "cJSON.h"
|
||||
#include "iot_import.h"
|
||||
#include "iot_export_linkkit.h"
|
||||
|
||||
#include "iotx_log.h"
|
||||
|
||||
#include "living_platform_rawdata_main.h"
|
||||
#include "app_entry.h"
|
||||
#include "living_platform_rawdata_ut.h"
|
||||
|
||||
static living_platform_rawdata_ctx_t g_living_platform_rawdata_ctx;
|
||||
|
||||
living_platform_rawdata_ctx_t *living_platform_rawdata_get_ctx(void)
|
||||
{
|
||||
return &g_living_platform_rawdata_ctx;
|
||||
}
|
||||
|
||||
static int user_connected_event_handler(void)
|
||||
{
|
||||
living_platform_rawdata_ctx_t *living_platform_rawdata_ctx = living_platform_rawdata_get_ctx();
|
||||
|
||||
living_platform_rawdata_info("Cloud Connected");
|
||||
|
||||
living_platform_rawdata_ctx->cloud_connected = 1;
|
||||
|
||||
if (living_platform_rawdata_ctx->master_initialized == 1) //initialized
|
||||
{
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_disconnected_event_handler(void)
|
||||
{
|
||||
living_platform_rawdata_ctx_t *living_platform_rawdata_ctx = living_platform_rawdata_get_ctx();
|
||||
|
||||
living_platform_rawdata_info("Cloud Disconnected");
|
||||
|
||||
living_platform_rawdata_ctx->cloud_connected = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_down_raw_data_arrived_event_handler(const int devid, const unsigned char *payload,
|
||||
const int payload_len)
|
||||
{
|
||||
living_platform_rawdata_info("Down Raw Message, Devid: %d, Payload Length: %d", devid, payload_len);
|
||||
if (payload[0] == 0x02 && payload_len == 6)
|
||||
{
|
||||
living_platform_rawdata_ut_set_LightSwitch(payload[5]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//When code is not 200,maybe call this function
|
||||
static int user_property_cloud_error_handler(const int code, const char *data, const char *detail)
|
||||
{
|
||||
living_platform_rawdata_info("code =%d ,data=%s, detail=%s", code, data, detail);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_report_reply_event_handler(const int devid, const int msgid, const int code, const char *reply,
|
||||
const int reply_len)
|
||||
{
|
||||
const char *reply_value = (reply == NULL) ? ("NULL") : (reply);
|
||||
const int reply_value_len = (reply_len == 0) ? (strlen("NULL")) : (reply_len);
|
||||
|
||||
living_platform_rawdata_info("Message Post Reply Received, Devid: %d, Message ID: %d, Code: %d, Reply: %.*s", devid, msgid, code,
|
||||
reply_value_len,
|
||||
reply_value);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_trigger_event_reply_event_handler(const int devid, const int msgid, const int code, const char *eventid,
|
||||
const int eventid_len, const char *message, const int message_len)
|
||||
{
|
||||
living_platform_rawdata_info("Trigger Event Reply Received, Devid: %d, Message ID: %d, Code: %d, EventID: %.*s, Message: %.*s", devid,
|
||||
msgid, code,
|
||||
eventid_len,
|
||||
eventid, message_len, message);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_timestamp_reply_event_handler(const char *timestamp)
|
||||
{
|
||||
living_platform_rawdata_info("Current Timestamp: %s", timestamp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint64_t user_update_sec(void)
|
||||
{
|
||||
static uint64_t time_start_ms = 0;
|
||||
|
||||
if (time_start_ms == 0)
|
||||
{
|
||||
time_start_ms = HAL_UptimeMs();
|
||||
}
|
||||
|
||||
return (HAL_UptimeMs() - time_start_ms) / 1000;
|
||||
}
|
||||
|
||||
static int living_platform_rawdata_ut_query_timestamp(void)
|
||||
{
|
||||
living_platform_rawdata_ctx_t *living_platform_rawdata_ctx = living_platform_rawdata_get_ctx();
|
||||
living_platform_rawdata_info("do query timestamp");
|
||||
|
||||
IOT_Linkkit_Query(living_platform_rawdata_ctx->master_devid, ITM_MSG_QUERY_TIMESTAMP, NULL, 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_initialized(const int devid)
|
||||
{
|
||||
living_platform_rawdata_ctx_t *living_platform_rawdata_ctx = living_platform_rawdata_get_ctx();
|
||||
living_platform_rawdata_info("Device Initialized, Devid: %d", devid);
|
||||
|
||||
if (living_platform_rawdata_ctx->master_devid == devid)
|
||||
{
|
||||
living_platform_rawdata_ctx->master_initialized = 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_master_dev_available(void)
|
||||
{
|
||||
living_platform_rawdata_ctx_t *living_platform_rawdata_ctx = living_platform_rawdata_get_ctx();
|
||||
|
||||
if (living_platform_rawdata_ctx->cloud_connected && living_platform_rawdata_ctx->master_initialized)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int living_platform_rawdata_notify_msg_handle(int devid, const char *request, const int request_len)
|
||||
{
|
||||
int ret = 0;
|
||||
cJSON *request_root = NULL;
|
||||
cJSON *item = NULL;
|
||||
|
||||
request_root = cJSON_Parse(request);
|
||||
if (request_root == NULL)
|
||||
{
|
||||
living_platform_rawdata_info("JSON Parse Error");
|
||||
return -1;
|
||||
}
|
||||
|
||||
item = cJSON_GetObjectItem(request_root, "identifier");
|
||||
if (item == NULL || !cJSON_IsString(item))
|
||||
{
|
||||
cJSON_Delete(request_root);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!strcmp(item->valuestring, "awss.BindNotify"))
|
||||
{
|
||||
cJSON *value = cJSON_GetObjectItem(request_root, "value");
|
||||
if (value == NULL || !cJSON_IsObject(value))
|
||||
{
|
||||
cJSON_Delete(request_root);
|
||||
return -1;
|
||||
}
|
||||
cJSON *op = cJSON_GetObjectItem(value, "Operation");
|
||||
if (op != NULL && cJSON_IsString(op))
|
||||
{
|
||||
if (!strcmp(op->valuestring, "Bind"))
|
||||
{
|
||||
living_platform_rawdata_info("Device Bind");
|
||||
//TODO:User can do something according thier own requirements
|
||||
}
|
||||
else if (!strcmp(op->valuestring, "Unbind"))
|
||||
{
|
||||
living_platform_rawdata_info("Device unBind");
|
||||
//TODO:User can do something according thier own requirements
|
||||
}
|
||||
else if (!strcmp(op->valuestring, "Reset"))
|
||||
{
|
||||
living_platform_rawdata_info("Device reset");
|
||||
//TODO:User can do something according thier own requirements
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cJSON_Delete(request_root);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_event_notify_handler(const int devid, const char *request, const int request_len)
|
||||
{
|
||||
int res = 0;
|
||||
living_platform_rawdata_ctx_t *living_platform_rawdata_ctx = living_platform_rawdata_get_ctx();
|
||||
living_platform_rawdata_info("Event notify Received, Devid: %d, Request: %s", devid, request);
|
||||
|
||||
living_platform_rawdata_notify_msg_handle(devid, request, request_len);
|
||||
res = IOT_Linkkit_Report(living_platform_rawdata_ctx->master_devid, ITM_MSG_EVENT_NOTIFY_REPLY,
|
||||
(unsigned char *)request, request_len);
|
||||
living_platform_rawdata_info("Post Property Message ID: %d", res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_fota_event_handler(int type, const char *version)
|
||||
{
|
||||
char *p_fota_buffer = NULL;
|
||||
living_platform_rawdata_ctx_t *living_platform_rawdata_ctx = living_platform_rawdata_get_ctx();
|
||||
|
||||
p_fota_buffer = HAL_Malloc(LIVING_PLATFORM_RAWDATA_OTA_BUFFER_LEN);
|
||||
if (!p_fota_buffer)
|
||||
{
|
||||
living_platform_rawdata_err("no mem");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (type == 0)
|
||||
{
|
||||
living_platform_rawdata_info("New Firmware Version: %s", version);
|
||||
memset(p_fota_buffer, 0, LIVING_PLATFORM_RAWDATA_OTA_BUFFER_LEN);
|
||||
IOT_Linkkit_Query(living_platform_rawdata_ctx->master_devid, ITM_MSG_QUERY_FOTA_DATA, (unsigned char *)p_fota_buffer, LIVING_PLATFORM_RAWDATA_OTA_BUFFER_LEN);
|
||||
}
|
||||
|
||||
if (p_fota_buffer)
|
||||
HAL_Free(p_fota_buffer);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_cota_event_handler(int type, const char *config_id, int config_size, const char *get_type,
|
||||
const char *sign, const char *sign_method, const char *url)
|
||||
{
|
||||
char *p_cota_buffer = NULL;
|
||||
living_platform_rawdata_ctx_t *living_platform_rawdata_ctx = living_platform_rawdata_get_ctx();
|
||||
|
||||
p_cota_buffer = HAL_Malloc(LIVING_PLATFORM_RAWDATA_OTA_BUFFER_LEN);
|
||||
if (!p_cota_buffer)
|
||||
{
|
||||
living_platform_rawdata_err("no mem");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (type == 0)
|
||||
{
|
||||
living_platform_rawdata_info("New Config ID: %s", config_id);
|
||||
living_platform_rawdata_info("New Config Size: %d", config_size);
|
||||
living_platform_rawdata_info("New Config Type: %s", get_type);
|
||||
living_platform_rawdata_info("New Config Sign: %s", sign);
|
||||
living_platform_rawdata_info("New Config Sign Method: %s", sign_method);
|
||||
living_platform_rawdata_info("New Config URL: %s", url);
|
||||
|
||||
IOT_Linkkit_Query(living_platform_rawdata_ctx->master_devid, ITM_MSG_QUERY_COTA_DATA, (unsigned char *)p_cota_buffer, LIVING_PLATFORM_RAWDATA_OTA_BUFFER_LEN);
|
||||
}
|
||||
|
||||
if (p_cota_buffer)
|
||||
HAL_Free(p_cota_buffer);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_offline_reset_handler(void)
|
||||
{
|
||||
living_platform_rawdata_info("user callback user_offline_reset_handler called.");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_dev_bind_event(const int state_code, const char *state_message)
|
||||
{
|
||||
living_platform_rawdata_info("state_code: -0x%04x, str_msg= %s", -state_code, state_message == NULL ? "NULL" : state_message);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef DM_UNIFIED_SERVICE_POST
|
||||
static int user_unified_service_post_reply_handler(const int devid, const int id, const int code, const char *payload, const int payload_len)
|
||||
{
|
||||
living_platform_rawdata_info("Receive unified service post reply, code:%d, payload:%s", code, payload);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static iotx_linkkit_dev_meta_info_t *living_platform_rawdata_main_init(living_platform_rawdata_ctx_t *living_platform_rawdata_ctx)
|
||||
{
|
||||
int register_type = 0;
|
||||
int post_event_reply = 1;
|
||||
iotx_linkkit_dev_meta_info_t *p_master_meta = NULL;
|
||||
|
||||
memset(living_platform_rawdata_ctx, 0, sizeof(living_platform_rawdata_ctx_t));
|
||||
|
||||
p_master_meta = HAL_Malloc(sizeof(iotx_linkkit_dev_meta_info_t));
|
||||
if (p_master_meta == NULL)
|
||||
{
|
||||
living_platform_rawdata_err("no mem");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memset(p_master_meta, 0, sizeof(iotx_linkkit_dev_meta_info_t));
|
||||
HAL_GetProductKey(p_master_meta->product_key);
|
||||
HAL_GetDeviceName(p_master_meta->device_name);
|
||||
HAL_GetDeviceSecret(p_master_meta->device_secret);
|
||||
HAL_GetProductSecret(p_master_meta->product_secret);
|
||||
|
||||
if ((0 == strlen(p_master_meta->product_key)) || (0 == strlen(p_master_meta->device_name)) ||
|
||||
(0 == register_type && (0 == strlen(p_master_meta->device_secret))) || (0 == strlen(p_master_meta->product_secret)))
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
printf("Master meta info is invalid...\r\n");
|
||||
printf("pk[%s]\r\n", p_master_meta->product_key);
|
||||
printf("ps[%s]\r\n", p_master_meta->product_secret);
|
||||
printf("dn[%s]\r\n", p_master_meta->device_name);
|
||||
printf("ds[%s]\r\n", p_master_meta->device_secret);
|
||||
HAL_SleepMs(2000);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef LIVING_PLATFORM_RAWDATA_PRODUCT_DYNAMIC_REGISTER
|
||||
register_type = 1;
|
||||
#endif
|
||||
|
||||
/* Register Callback */
|
||||
IOT_RegisterCallback(ITE_INITIALIZE_COMPLETED, user_initialized);
|
||||
IOT_RegisterCallback(ITE_CONNECT_SUCC, user_connected_event_handler);
|
||||
IOT_RegisterCallback(ITE_DISCONNECTED, user_disconnected_event_handler);
|
||||
IOT_RegisterCallback(ITE_RAWDATA_ARRIVED, user_down_raw_data_arrived_event_handler);
|
||||
|
||||
IOT_RegisterCallback(ITE_REPORT_REPLY, user_report_reply_event_handler);
|
||||
IOT_RegisterCallback(ITE_TRIGGER_EVENT_REPLY, user_trigger_event_reply_event_handler);
|
||||
IOT_RegisterCallback(ITE_TIMESTAMP_REPLY, user_timestamp_reply_event_handler);
|
||||
|
||||
IOT_RegisterCallback(ITE_CLOUD_ERROR, user_property_cloud_error_handler);
|
||||
|
||||
IOT_RegisterCallback(ITE_FOTA, user_fota_event_handler);
|
||||
IOT_RegisterCallback(ITE_COTA, user_cota_event_handler);
|
||||
|
||||
IOT_RegisterCallback(ITE_EVENT_NOTIFY, user_event_notify_handler);
|
||||
IOT_RegisterCallback(ITE_STATE_DEV_BIND, user_dev_bind_event);
|
||||
|
||||
IOT_RegisterCallback(ITE_OFFLINE_RESET, user_offline_reset_handler);
|
||||
|
||||
IOT_Ioctl(IOTX_IOCTL_SET_DYNAMIC_REGISTER, (void *)®ister_type);
|
||||
|
||||
/* Choose Whether You Need Post Property/Event Reply */
|
||||
IOT_Ioctl(IOTX_IOCTL_RECV_EVENT_REPLY, (void *)&post_event_reply);
|
||||
|
||||
#ifdef LIVING_PLATFORM_RAWDATA_USE_UT_FOR_TESTING
|
||||
living_platform_rawdata_ut_init();
|
||||
#endif
|
||||
|
||||
return p_master_meta;
|
||||
}
|
||||
|
||||
int living_platform_rawdata_main(void *paras)
|
||||
{
|
||||
int res = 0;
|
||||
uint64_t time_prev_sec = 0, time_now_sec = 0;
|
||||
living_platform_rawdata_ctx_t *living_platform_rawdata_ctx = living_platform_rawdata_get_ctx();
|
||||
iotx_linkkit_dev_meta_info_t *p_master_meta = NULL;
|
||||
|
||||
p_master_meta = living_platform_rawdata_main_init(living_platform_rawdata_ctx);
|
||||
if (NULL == p_master_meta)
|
||||
{
|
||||
printf("OOPS:living_platform_rawdata_main_init failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Create Master Device Resources */
|
||||
do
|
||||
{
|
||||
living_platform_rawdata_ctx->master_devid = IOT_Linkkit_Open(IOTX_LINKKIT_DEV_TYPE_MASTER, p_master_meta);
|
||||
if (living_platform_rawdata_ctx->master_devid < 0)
|
||||
{
|
||||
printf("IOT_Linkkit_Open Failed, retry after 5s...\r\n");
|
||||
HAL_SleepMs(5000);
|
||||
}
|
||||
} while (living_platform_rawdata_ctx->master_devid < 0);
|
||||
/* Start Connect Aliyun Server */
|
||||
do
|
||||
{
|
||||
res = IOT_Linkkit_Connect(living_platform_rawdata_ctx->master_devid);
|
||||
if (res < 0)
|
||||
{
|
||||
printf("IOT_Linkkit_Connect Failed, retry after 5s...\r\n");
|
||||
HAL_SleepMs(5000);
|
||||
}
|
||||
} while (res < 0);
|
||||
|
||||
living_platform_rawdata_ut_query_timestamp();
|
||||
|
||||
while (1)
|
||||
{
|
||||
IOT_Linkkit_Yield(LIVING_PLATFORM_RAWDATA_YIELD_TIMEOUT_MS);
|
||||
|
||||
time_now_sec = user_update_sec();
|
||||
if (time_prev_sec == time_now_sec)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (user_master_dev_available())
|
||||
{
|
||||
#ifdef LIVING_PLATFORM_RAWDATA_USE_UT_FOR_TESTING
|
||||
living_platform_rawdata_ut_misc_process(time_now_sec);
|
||||
#endif
|
||||
}
|
||||
|
||||
time_prev_sec = time_now_sec;
|
||||
}
|
||||
|
||||
//Should never come here
|
||||
living_platform_rawdata_ctx->g_user_dispatch_thread_running = 0;
|
||||
|
||||
if (p_master_meta)
|
||||
{
|
||||
HAL_Free(p_master_meta);
|
||||
}
|
||||
|
||||
IOT_DumpMemoryStats(IOT_LOG_DEBUG);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
#ifndef __LIVING_PLATFORM_RAWDATA_MAIN_H__
|
||||
#define __LIVING_PLATFORM_RAWDATA_MAIN_H__
|
||||
|
||||
#include <iotx_log.h>
|
||||
|
||||
//#define LIVING_PLATFORM_RAWDATA_PRODUCT_DYNAMIC_REGISTER
|
||||
|
||||
/*
|
||||
Note:You can test some function to define LIVING_PLATFORM_RAWDATA_USE_UT_FOR_TESTING
|
||||
*/
|
||||
// #define LIVING_PLATFORM_RAWDATA_USE_UT_FOR_TESTING
|
||||
|
||||
#define living_platform_rawdata_debug(...) log_debug("ct", __VA_ARGS__)
|
||||
#define living_platform_rawdata_info(...) log_info("ct", __VA_ARGS__)
|
||||
#define living_platform_rawdata_warn(...) log_warning("ct", __VA_ARGS__)
|
||||
#define living_platform_rawdata_err(...) log_err("ct", __VA_ARGS__)
|
||||
#define living_platform_rawdata_crit(...) log_crit("ct", __VA_ARGS__)
|
||||
|
||||
#define LIVING_PLATFORM_RAWDATA_YIELD_TIMEOUT_MS (200)
|
||||
#define LIVING_PLATFORM_RAWDATA_OTA_BUFFER_LEN (512 + 1) //Have to +1 or else ota SHA256 will crash
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int master_devid;
|
||||
int cloud_connected;
|
||||
int master_initialized;
|
||||
void *g_user_dispatch_thread;
|
||||
int g_user_dispatch_thread_running;
|
||||
} living_platform_rawdata_ctx_t;
|
||||
|
||||
extern int living_platform_rawdata_main(void *paras);
|
||||
extern living_platform_rawdata_ctx_t *living_platform_rawdata_get_ctx(void);
|
||||
|
||||
#endif
|
||||
|
|
@ -0,0 +1,80 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2019 Alibaba Group Holding Limited
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "cJSON.h"
|
||||
#include "iot_import.h"
|
||||
#include "iot_export_linkkit.h"
|
||||
#include "iotx_log.h"
|
||||
|
||||
#include "app_entry.h"
|
||||
#include "living_platform_rawdata_main.h"
|
||||
#include "living_platform_rawdata_ut.h"
|
||||
|
||||
static living_platform_rawdata_tsl_t living_platform_rawdata_tsl_data;
|
||||
|
||||
living_platform_rawdata_tsl_t *living_platform_rawdata_ut_get_tsl_data(void)
|
||||
{
|
||||
return &living_platform_rawdata_tsl_data;
|
||||
}
|
||||
|
||||
int living_platform_rawdata_ut_set_LightSwitch(char LightSwitch)
|
||||
{
|
||||
living_platform_rawdata_tsl_data.LightSwitch = LightSwitch;
|
||||
living_platform_rawdata_info("set LightSwitch:%s", (LightSwitch == 0) ? "off" : "on");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int living_platform_rawdata_ut_get_LightSwitch(void)
|
||||
{
|
||||
return living_platform_rawdata_tsl_data.LightSwitch;
|
||||
}
|
||||
|
||||
//Just for reference
|
||||
void user_post_raw_data(void)
|
||||
{
|
||||
static int id = 0;
|
||||
int res = 0;
|
||||
unsigned char payload[6] = {0};
|
||||
living_platform_rawdata_ctx_t *living_platform_rawdata_ctx = living_platform_rawdata_get_ctx();
|
||||
|
||||
id += 1;
|
||||
payload[0] = 0x01;
|
||||
payload[1] = (id >> 24) & 0xFF;
|
||||
payload[2] = (id >> 16) & 0xFF;
|
||||
payload[3] = (id >> 8) & 0xFF;
|
||||
payload[4] = id & 0xFF;
|
||||
payload[5] = living_platform_rawdata_tsl_data.LightSwitch;
|
||||
|
||||
res = IOT_Linkkit_Report(living_platform_rawdata_ctx->master_devid, ITM_MSG_POST_RAW_DATA,
|
||||
payload, 6);
|
||||
|
||||
living_platform_rawdata_info("Post Raw Data Message ID: %d", res);
|
||||
}
|
||||
|
||||
void living_platform_rawdata_ut_misc_process(uint64_t time_now_sec)
|
||||
{
|
||||
/* Post Proprety Example */
|
||||
if (time_now_sec % 11 == 0)
|
||||
{
|
||||
user_post_raw_data();
|
||||
}
|
||||
}
|
||||
|
||||
int living_platform_rawdata_ut_init(void)
|
||||
{
|
||||
int ret = SUCCESS_RETURN;
|
||||
|
||||
memset(&living_platform_rawdata_tsl_data, 0, sizeof(living_platform_rawdata_tsl_t));
|
||||
|
||||
living_platform_rawdata_tsl_data.LightSwitch = 1;
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
#ifndef __LIVING_PLATFORM_RAWDATA_UT_H__
|
||||
#define __LIVING_PLATFORM_RAWDATA_UT_H__
|
||||
|
||||
#define KV_KEY_PK "pk"
|
||||
#define KV_KEY_PS "ps"
|
||||
#define KV_KEY_DN "dn"
|
||||
#define KV_KEY_DS "ds"
|
||||
|
||||
#define MAX_KEY_LEN (6)
|
||||
|
||||
#define PRODUCT_KEY "PK_XXXXXX"
|
||||
#define PRODUCT_SECRET "PS_XXXXXX"
|
||||
#define DEVICE_NAME "DN_XXXXXX"
|
||||
#define DEVICE_SECRET "DS_XXXXXX"
|
||||
|
||||
#define EXAMPLE_MASTER_DEVID (0)
|
||||
|
||||
typedef struct _living_platform_rawdata_tsl_s
|
||||
{
|
||||
char LightSwitch;
|
||||
} living_platform_rawdata_tsl_t;
|
||||
|
||||
extern int living_platform_rawdata_ut_init(void);
|
||||
extern void living_platform_rawdata_ut_misc_process(uint64_t time_now_sec);
|
||||
extern living_platform_rawdata_tsl_t* living_platform_rawdata_ut_get_tsl_data(void);
|
||||
extern int living_platform_rawdata_ut_set_LightSwitch(char LightSwitch);
|
||||
extern int living_platform_rawdata_ut_get_LightSwitch(void);
|
||||
#endif
|
||||
30
Products/example/living_platform_rawdata/make.settings
Normal file
30
Products/example/living_platform_rawdata/make.settings
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#
|
||||
# Configure FeiYan SDK Features
|
||||
#
|
||||
|
||||
#=========Basic Features==========
|
||||
FEATURE_SRCPATH="."
|
||||
FEATURE_MQTT_COMM_ENABLED=y
|
||||
FEATURE_MQTT_AUTO_SUBSCRIBE=y
|
||||
FEATURE_DEVICE_MODEL_ENABLED=y
|
||||
FEATURE_OTA_ENABLED=y
|
||||
FEATURE_DEV_BIND_ENABLED=y
|
||||
FEATURE_SUPPORT_TLS=y
|
||||
FEATURE_DEVICE_MODEL_RAWDATA_SOLO=y
|
||||
#=========Basic Features==========
|
||||
|
||||
#=========User Config Features==========
|
||||
FEATURE_ALCS_ENABLED=y
|
||||
|
||||
#
|
||||
# AWSS Configurations
|
||||
#
|
||||
FEATURE_WIFI_PROVISION_ENABLED=y
|
||||
|
||||
FEATURE_AWSS_SUPPORT_SMARTCONFIG=y
|
||||
FEATURE_AWSS_SUPPORT_ZEROCONFIG=y
|
||||
FEATURE_AWSS_SUPPORT_DEV_AP=y
|
||||
|
||||
FEATURE_AWSS_SUPPORT_PHONEASAP=n
|
||||
FEATURE_AWSS_SUPPORT_ROUTER=n
|
||||
#=========User Config Features==========
|
||||
110
Products/example/living_platform_rawdata/makefile
Normal file
110
Products/example/living_platform_rawdata/makefile
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
|
||||
include living_platform_rawdata.mk
|
||||
target = living_platform_rawdata
|
||||
|
||||
CFLAGS += -DALCS_ENABLED
|
||||
|
||||
INCLUDE_PATH = -I$(PWD)
|
||||
INCLUDE = $(INCLUDE_PATH)/ \
|
||||
-I$(PWD)/../../../prebuild/include \
|
||||
-I$(PWD)/../../../prebuild/include/exports \
|
||||
-I$(PWD)/../../../prebuild/include/imports \
|
||||
-I$(PWD)/../../../prebuild/include/kernel/rhino/core/include \
|
||||
-I$(PWD)/../../../prebuild/include/framework/netmgr/include \
|
||||
-I$(PWD)/../../../prebuild/include/platform/arch/arm/armv7m/gcc/m4 \
|
||||
-I$(PWD)/../../../prebuild/include/board/$(BOARD)
|
||||
|
||||
app = $(wildcard ./*.c)
|
||||
obj_app = $(patsubst %.c,%.o,$(app))
|
||||
|
||||
|
||||
CROSS_PREFIX="arm-none-eabi-"
|
||||
DEPENDS=""
|
||||
CCLD=""
|
||||
LDFLAGS=""
|
||||
CC=arm-none-eabi-gcc
|
||||
LD=arm-none-eabi-ld
|
||||
AR=arm-none-eabi-ar
|
||||
STRIP=arm-none-eabi-strip
|
||||
OBJCOPY=arm-none-eabi-objcopy
|
||||
|
||||
# CFLAGS += -MD -ggdb -Os -Wall -Wfatal-errors -fsigned-char -ffunction-sections -fdata-sections -fno-common -std=gnu11 -Werror \
|
||||
# -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
|
||||
CFLAGS += -DWITH_LWIP -DCONFIG_AOS_CLI -DLOG_SIMPLE
|
||||
CFLAGS += ${GLOBAL_CFLAGS}
|
||||
CFLAGS += ${SDK_DEFINES}
|
||||
|
||||
ifeq ("$(BOARD)", "hf-lpt230")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
else ifeq ("$(BOARD)", "hf-lpt130")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
else ifeq ("$(BOARD)", "hf-lpb130")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
else ifeq ("$(BOARD)", "hf-lpb135")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
else ifeq ("$(BOARD)", "uno-91h")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
|
||||
else ifeq ("$(BOARD)", "mk1101")
|
||||
else ifeq ("$(BOARD)", "mk3060")
|
||||
CFLAGS += -mcpu=arm968e-s -march=armv5te -mthumb -mthumb-interwork -mlittle-endian -w -Os -Wall -Wfatal-errors -fsigned-char -ffunction-sections -fdata-sections -fno-common -std=gnu11
|
||||
else ifeq ("$(BOARD)", "mk3061")
|
||||
CFLAGS += -mcpu=arm968e-s -march=armv5te -mthumb -mthumb-interwork -mlittle-endian -w -Os -Wall -Wfatal-errors -fsigned-char -ffunction-sections -fdata-sections -fno-common -std=gnu11
|
||||
|
||||
else ifeq ("$(BOARD)", "mk3080")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
else ifeq ("$(BOARD)", "mk3092")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
else ifeq ("$(BOARD)", "mk5080")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
|
||||
|
||||
else ifeq ("$(BOARD)", "asr5501")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -mfpu=fpv4-sp-d16 -std=gnu11 -w
|
||||
else ifeq ("$(BOARD)", "mx1270")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -mfpu=fpv4-sp-d16 -std=gnu11 -w
|
||||
|
||||
else ifeq ("$(BOARD)", "bk7231devkitc")
|
||||
CFLAGS += -mcpu=arm968e-s -march=armv5te -mthumb -mthumb-interwork -mlittle-endian -w -Os -Wall -Wfatal-errors -fsigned-char -ffunction-sections -fdata-sections -fno-common -std=gnu11
|
||||
CFLAGS += -DBK7231DEVKITC
|
||||
else ifeq ("$(BOARD)", "bk7231sdevkitc")
|
||||
CFLAGS += -mcpu=arm968e-s -march=armv5te -mthumb -mthumb-interwork -mlittle-endian -w -Os -Wall -Wfatal-errors -fsigned-char -ffunction-sections -fdata-sections -fno-common -std=gnu11
|
||||
CFLAGS += -DBK7231SDEVKITC
|
||||
else ifeq ("$(BOARD)", "bk7231udevkitc")
|
||||
CFLAGS += -mcpu=arm968e-s -march=armv5te -mthumb -mthumb-interwork -mlittle-endian -w -Os -Wall -Wfatal-errors -fsigned-char -ffunction-sections -fdata-sections -fno-common -std=gnu11
|
||||
CFLAGS += -DBK7231UDEVKITC
|
||||
else ifeq ("$(BOARD)", "b_l475e")
|
||||
|
||||
else ifeq ("$(BOARD)", "eml3047")
|
||||
else ifeq ("$(BOARD)", "frdmkl27z")
|
||||
else ifeq ("$(BOARD)", "lpcxpresso54102")
|
||||
|
||||
else ifeq ("$(BOARD)", "pca10056")
|
||||
|
||||
## toolchains['arm-rockchip-linux-gnueabihf']
|
||||
else ifeq ("$(BOARD)", "rk1108")
|
||||
|
||||
## toolchains['nds32le-elf-newlib-v3']
|
||||
else ifeq ("$(BOARD)", "sv6266_evb")
|
||||
endif
|
||||
|
||||
ifneq ("$(BOARD)", "")
|
||||
$(info BOARD=${BOARD} CONFIG_COMPILE_DATE=${CONFIG_COMPILE_DATE} region=${CONFIG_SERVER_REGION} env=${CONFIG_SERVER_ENV} debug=${CONFIG_DEBUG})
|
||||
endif
|
||||
|
||||
# Compile date
|
||||
CONFIG_COMPILE_DATE ?= $(shell date +%Y%m%d.%H%M%S)
|
||||
all : $(target)
|
||||
|
||||
$(target):$(obj_app)
|
||||
@mkdir -p obj
|
||||
@mv *.o obj
|
||||
@$(RM) -rf *.d
|
||||
@$(AR) -rcs $(PWD)/../../../prebuild/lib/$(target).a obj/*.o 2>/dev/null
|
||||
@$(RM) -rf obj
|
||||
%.o:%.c
|
||||
@$(CC) -c $(CFLAGS) $(INCLUDE) -o $@ $<
|
||||
|
||||
clean:
|
||||
@-$(RM) obj/*
|
||||
45
Products/example/living_platform_rawdata/ucube.py
Normal file
45
Products/example/living_platform_rawdata/ucube.py
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
src =Split('''
|
||||
living_platform_rawdata_main.c
|
||||
app_entry.c
|
||||
''')
|
||||
|
||||
component =aos_component('living_platform_rawdata', src)
|
||||
|
||||
dependencis =Split('''
|
||||
framework/protocol/linkkit/sdk
|
||||
framework/protocol/linkkit/hal
|
||||
framework/netmgr
|
||||
framework/common
|
||||
framework/ywss4linkkit
|
||||
utility/cjson
|
||||
tools/cli
|
||||
''')
|
||||
for i in dependencis:
|
||||
component.add_comp_deps(i)
|
||||
|
||||
global_macros =Split('''
|
||||
ALIOT_DEBUG
|
||||
IOTX_DEBUG
|
||||
USE_LPTHREAD
|
||||
CONFIG_DM_DEVTYPE_SINGLE
|
||||
TEST_ALCS
|
||||
CONFIG_AOS_CLI
|
||||
''')
|
||||
for i in global_macros:
|
||||
component.add_global_macros(i)
|
||||
|
||||
if aos_global_config.get('LWIP') == 1:
|
||||
component.add_comp_deps("kernel/protocols/net")
|
||||
aos_global_config.set('no_with_lwip', 0)
|
||||
|
||||
if aos_global_config.get('print_heap') == 1:
|
||||
component.add_global_macros('CONFIG_PRINT_HEAP')
|
||||
|
||||
if aos_global_config.mcu_family == 'esp8266':
|
||||
component.add_global_macros('FOTA_RAM_LIMIT_MODE')
|
||||
component.add_global_macros('ESP8266_CHIPSET')
|
||||
else :
|
||||
component.add_global_macros('CONFIG_AOS_CLI')
|
||||
component.add_comp_deps("cli")
|
||||
|
||||
|
||||
732
Products/example/smart_outlet/app_entry.c
Executable file
732
Products/example/smart_outlet/app_entry.c
Executable file
|
|
@ -0,0 +1,732 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <aos/aos.h>
|
||||
#include <aos/yloop.h>
|
||||
#include "netmgr.h"
|
||||
#include "iot_export.h"
|
||||
#include "iot_import.h"
|
||||
#include "app_entry.h"
|
||||
#include "aos/kv.h"
|
||||
#include "vendor.h"
|
||||
#include "device_state_manger.h"
|
||||
#include "smart_outlet.h"
|
||||
#include "msg_process_center.h"
|
||||
#include "property_report.h"
|
||||
|
||||
#ifdef AOS_TIMER_SERVICE
|
||||
#include "iot_export_timer.h"
|
||||
#endif
|
||||
#ifdef CSP_LINUXHOST
|
||||
#include <signal.h>
|
||||
#endif
|
||||
|
||||
#include <k_api.h>
|
||||
|
||||
#if defined(OTA_ENABLED) && defined(BUILD_AOS)
|
||||
#include "ota_service.h"
|
||||
#endif
|
||||
|
||||
#ifdef EN_COMBO_NET
|
||||
#include "combo_net.h"
|
||||
#endif
|
||||
|
||||
static aos_task_t task_key_detect;
|
||||
static aos_task_t task_msg_process;
|
||||
static aos_task_t task_property_report;
|
||||
static aos_task_t task_linkkit_reset;
|
||||
static aos_task_t task_reboot_device;
|
||||
|
||||
static char linkkit_started = 0;
|
||||
|
||||
#ifdef EN_COMBO_NET
|
||||
char awss_running = 0;
|
||||
char awss_success_flag = 0;
|
||||
#else
|
||||
static char awss_running = 0;
|
||||
#endif
|
||||
uint8_t g_ap_state = 0;
|
||||
extern int init_awss_flag(void);
|
||||
|
||||
void do_awss_active(void);
|
||||
|
||||
#ifdef CONFIG_PRINT_HEAP
|
||||
void print_heap()
|
||||
{
|
||||
extern k_mm_head *g_kmm_head;
|
||||
int free = g_kmm_head->free_size;
|
||||
LOG("============free heap size =%d==========", free);
|
||||
}
|
||||
#endif
|
||||
|
||||
static void wifi_service_event(input_event_t *event, void *priv_data)
|
||||
{
|
||||
if (event->type != EV_WIFI) {
|
||||
return;
|
||||
}
|
||||
|
||||
LOG("wifi_service_event(), event->code=%d.", event->code);
|
||||
if (event->code == CODE_WIFI_ON_CONNECTED) {
|
||||
LOG("CODE_WIFI_ON_CONNECTED");
|
||||
} else if (event->code == CODE_WIFI_ON_DISCONNECT) {
|
||||
LOG("CODE_WIFI_ON_DISCONNECT");
|
||||
#ifdef EN_COMBO_NET
|
||||
g_ap_state = 0;
|
||||
if (!combo_ble_conn_state()) {
|
||||
aos_post_event(EV_BZ_COMBO, COMBO_EVT_CODE_RESTART_ADV, 0);
|
||||
}
|
||||
#endif
|
||||
} else if (event->code == CODE_WIFI_ON_CONNECT_FAILED) {
|
||||
LOG("CODE_WIFI_ON_CONNECT_FAILED");
|
||||
} else if (event->code == CODE_WIFI_ON_GOT_IP) {
|
||||
#ifdef EN_COMBO_NET
|
||||
g_ap_state = 1;
|
||||
if (!combo_ble_conn_state()) {
|
||||
aos_post_event(EV_BZ_COMBO, COMBO_EVT_CODE_RESTART_ADV, 0);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
if (event->code != CODE_WIFI_ON_GOT_IP) {
|
||||
LOG("not CODE_WIFI_ON_GOT_IP");
|
||||
return;
|
||||
}
|
||||
|
||||
netmgr_ap_config_t config;
|
||||
memset(&config, 0, sizeof(netmgr_ap_config_t));
|
||||
netmgr_get_ap_config(&config);
|
||||
LOG("wifi_service_event config.ssid %s", config.ssid);
|
||||
if (strcmp(config.ssid, "adha") == 0 || strcmp(config.ssid, "aha") == 0) {
|
||||
// clear_wifi_ssid();
|
||||
return;
|
||||
}
|
||||
#ifdef EN_COMBO_NET
|
||||
awss_success_flag = 1;
|
||||
uint8_t rsp[] = {0x01, 0x01, 0x01};
|
||||
if (combo_ble_conn_state()) {
|
||||
breeze_post(rsp, sizeof(rsp));
|
||||
}
|
||||
#endif
|
||||
|
||||
if (!linkkit_started) {
|
||||
#ifdef CONFIG_PRINT_HEAP
|
||||
print_heap();
|
||||
#endif
|
||||
aos_task_new("linkkit", (void (*)(void *))linkkit_main, NULL, 1024 * 8);
|
||||
linkkit_started = 1;
|
||||
}
|
||||
}
|
||||
|
||||
static void cloud_service_event(input_event_t * event, void *priv_data)
|
||||
{
|
||||
if (event->type != EV_YUNIO) {
|
||||
return;
|
||||
}
|
||||
|
||||
LOG("cloud_service_event %d", event->code);
|
||||
|
||||
if (event->code == CODE_YUNIO_ON_CONNECTED) {
|
||||
LOG("user sub and pub here");
|
||||
return;
|
||||
}
|
||||
|
||||
if (event->code == CODE_YUNIO_ON_DISCONNECTED) {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Note:
|
||||
* the linkkit_event_monitor must not block and should run to complete fast
|
||||
* if user wants to do complex operation with much time,
|
||||
* user should post one task to do this, not implement complex operation in
|
||||
* linkkit_event_monitor
|
||||
*/
|
||||
|
||||
static void linkkit_event_monitor(int event)
|
||||
{
|
||||
switch (event) {
|
||||
case IOTX_AWSS_START: // AWSS start without enbale, just supports device discover
|
||||
// operate led to indicate user
|
||||
LOG("IOTX_AWSS_START");
|
||||
break;
|
||||
case IOTX_AWSS_ENABLE: // AWSS enable, AWSS doesn't parse awss packet until AWSS is enabled.
|
||||
LOG("IOTX_AWSS_ENABLE");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_LOCK_CHAN: // AWSS lock channel(Got AWSS sync packet)
|
||||
LOG("IOTX_AWSS_LOCK_CHAN");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_PASSWD_ERR: // AWSS decrypt passwd error
|
||||
LOG("IOTX_AWSS_PASSWD_ERR");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_GOT_SSID_PASSWD:
|
||||
LOG("IOTX_AWSS_GOT_SSID_PASSWD");
|
||||
// operate led to indicate user
|
||||
set_net_state(GOT_AP_SSID);
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_ADHA: // AWSS try to connnect adha (device
|
||||
// discover, router solution)
|
||||
LOG("IOTX_AWSS_CONNECT_ADHA");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_ADHA_FAIL: // AWSS fails to connect adha
|
||||
LOG("IOTX_AWSS_CONNECT_ADHA_FAIL");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_AHA: // AWSS try to connect aha (AP solution)
|
||||
LOG("IOTX_AWSS_CONNECT_AHA");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_AHA_FAIL: // AWSS fails to connect aha
|
||||
LOG("IOTX_AWSS_CONNECT_AHA_FAIL");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_SETUP_NOTIFY: // AWSS sends out device setup information
|
||||
// (AP and router solution)
|
||||
LOG("IOTX_AWSS_SETUP_NOTIFY");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_ROUTER: // AWSS try to connect destination router
|
||||
LOG("IOTX_AWSS_CONNECT_ROUTER");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_CONNECT_ROUTER_FAIL: // AWSS fails to connect destination
|
||||
// router.
|
||||
LOG("IOTX_AWSS_CONNECT_ROUTER_FAIL");
|
||||
set_net_state(CONNECT_AP_FAILED);
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_GOT_IP: // AWSS connects destination successfully and got
|
||||
// ip address
|
||||
LOG("IOTX_AWSS_GOT_IP");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_SUC_NOTIFY: // AWSS sends out success notify (AWSS
|
||||
// sucess)
|
||||
LOG("IOTX_AWSS_SUC_NOTIFY");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_AWSS_BIND_NOTIFY: // AWSS sends out bind notify information to
|
||||
// support bind between user and device
|
||||
LOG("IOTX_AWSS_BIND_NOTIFY");
|
||||
// operate led to indicate user
|
||||
user_example_ctx_t *user_example_ctx = user_example_get_ctx();
|
||||
user_example_ctx->bind_notified = 1;
|
||||
break;
|
||||
case IOTX_AWSS_ENABLE_TIMEOUT: // AWSS enable timeout
|
||||
// user needs to enable awss again to support get ssid & passwd of router
|
||||
LOG("IOTX_AWSS_ENALBE_TIMEOUT");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_CONN_CLOUD: // Device try to connect cloud
|
||||
LOG("IOTX_CONN_CLOUD");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_CONN_CLOUD_FAIL: // Device fails to connect cloud, refer to
|
||||
// net_sockets.h for error code
|
||||
LOG("IOTX_CONN_CLOUD_FAIL");
|
||||
set_net_state(CONNECT_CLOUD_FAILED);
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_CONN_CLOUD_SUC: // Device connects cloud successfully
|
||||
set_net_state(CONNECT_CLOUD_SUCCESS);
|
||||
LOG("IOTX_CONN_CLOUD_SUC");
|
||||
// operate led to indicate user
|
||||
break;
|
||||
case IOTX_RESET: // Linkkit reset success (just got reset response from
|
||||
// cloud without any other operation)
|
||||
LOG("IOTX_RESET");
|
||||
break;
|
||||
case IOTX_CONN_REPORT_TOKEN_SUC:
|
||||
#ifdef EN_COMBO_NET
|
||||
{
|
||||
uint8_t rsp[] = { 0x01, 0x01, 0x03 };
|
||||
if (combo_ble_conn_state()) {
|
||||
breeze_post(rsp, sizeof(rsp));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
LOG("---- report token success ----");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef AWSS_BATCH_DEVAP_ENABLE
|
||||
#define DEV_AP_ZCONFIG_TIMEOUT_MS 120000 // (ms)
|
||||
extern void awss_set_config_press(uint8_t press);
|
||||
extern uint8_t awss_get_config_press(void);
|
||||
extern void zconfig_80211_frame_filter_set(uint8_t filter, uint8_t fix_channel);
|
||||
void do_awss_dev_ap();
|
||||
|
||||
static aos_timer_t dev_ap_zconfig_timeout_timer;
|
||||
static uint8_t g_dev_ap_zconfig_timer = 0; // this timer create once and can restart
|
||||
static uint8_t g_dev_ap_zconfig_run = 0;
|
||||
|
||||
static void timer_func_devap_zconfig_timeout(void *arg1, void *arg2)
|
||||
{
|
||||
LOG("%s run\n", __func__);
|
||||
|
||||
if (awss_get_config_press()) {
|
||||
// still in zero wifi provision stage, should stop and switch to dev ap
|
||||
do_awss_dev_ap();
|
||||
} else {
|
||||
// zero wifi provision finished
|
||||
}
|
||||
|
||||
awss_set_config_press(0);
|
||||
zconfig_80211_frame_filter_set(0xFF, 0xFF);
|
||||
g_dev_ap_zconfig_run = 0;
|
||||
aos_timer_stop(&dev_ap_zconfig_timeout_timer);
|
||||
}
|
||||
|
||||
static void awss_dev_ap_switch_to_zeroconfig(void *p)
|
||||
{
|
||||
LOG("%s run\n", __func__);
|
||||
// Stop dev ap wifi provision
|
||||
awss_dev_ap_stop();
|
||||
// Start and enable zero wifi provision
|
||||
iotx_event_regist_cb(linkkit_event_monitor);
|
||||
awss_set_config_press(1);
|
||||
|
||||
// Start timer to count duration time of zero provision timeout
|
||||
if (!g_dev_ap_zconfig_timer) {
|
||||
aos_timer_new(&dev_ap_zconfig_timeout_timer, timer_func_devap_zconfig_timeout, NULL, DEV_AP_ZCONFIG_TIMEOUT_MS, 0);
|
||||
g_dev_ap_zconfig_timer = 1;
|
||||
}
|
||||
aos_timer_start(&dev_ap_zconfig_timeout_timer);
|
||||
|
||||
// This will hold thread, when awss is going
|
||||
netmgr_start(true);
|
||||
|
||||
LOG("%s exit\n", __func__);
|
||||
aos_task_exit(0);
|
||||
}
|
||||
|
||||
int awss_dev_ap_modeswitch_cb(uint8_t awss_new_mode, uint8_t new_mode_timeout, uint8_t fix_channel)
|
||||
{
|
||||
if ((awss_new_mode == 0) && !g_dev_ap_zconfig_run) {
|
||||
g_dev_ap_zconfig_run = 1;
|
||||
// Only receive zero provision packets
|
||||
zconfig_80211_frame_filter_set(0x00, fix_channel);
|
||||
LOG("switch to awssmode %d, mode_timeout %d, chan %d\n", 0x00, new_mode_timeout, fix_channel);
|
||||
// switch to zero config
|
||||
aos_task_new("devap_to_zeroconfig", awss_dev_ap_switch_to_zeroconfig, NULL, 2048);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static void awss_close_dev_ap(void *p)
|
||||
{
|
||||
awss_dev_ap_stop();
|
||||
aos_task_exit(0);
|
||||
}
|
||||
|
||||
void awss_open_dev_ap(void *p)
|
||||
{
|
||||
iotx_event_regist_cb(linkkit_event_monitor);
|
||||
/*if (netmgr_start(false) != 0) */{
|
||||
//aos_msleep(2000);
|
||||
#ifdef AWSS_BATCH_DEVAP_ENABLE
|
||||
awss_dev_ap_reg_modeswit_cb(awss_dev_ap_modeswitch_cb);
|
||||
#endif
|
||||
awss_dev_ap_start();
|
||||
}
|
||||
aos_task_exit(0);
|
||||
}
|
||||
|
||||
void stop_netmgr(void *p)
|
||||
{
|
||||
awss_stop();
|
||||
aos_task_exit(0);
|
||||
}
|
||||
|
||||
void start_netmgr(void *p)
|
||||
{
|
||||
iotx_event_regist_cb(linkkit_event_monitor);
|
||||
netmgr_start(true);
|
||||
aos_task_exit(0);
|
||||
}
|
||||
|
||||
void do_awss_active(void)
|
||||
{
|
||||
LOG("do_awss_active %d\n", awss_running);
|
||||
awss_running = 1;
|
||||
#ifdef WIFI_PROVISION_ENABLED
|
||||
extern int awss_config_press();
|
||||
awss_config_press();
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef EN_COMBO_NET
|
||||
void ble_awss_open(void *p)
|
||||
{
|
||||
iotx_event_regist_cb(linkkit_event_monitor);
|
||||
/*if (netmgr_start(false) != 0)*/ {
|
||||
//aos_msleep(2000);
|
||||
combo_net_init();
|
||||
}
|
||||
aos_task_exit(0);
|
||||
}
|
||||
|
||||
void do_ble_awss()
|
||||
{
|
||||
aos_task_new("netmgr_stop", stop_netmgr, NULL, 4096);
|
||||
//aos_msleep(2000);
|
||||
aos_task_new("ble_awss_open", ble_awss_open, NULL, 4096);
|
||||
}
|
||||
#endif
|
||||
|
||||
void do_awss_dev_ap()
|
||||
{
|
||||
aos_task_new("netmgr_stop", stop_netmgr, NULL, 4096);
|
||||
aos_task_new("dap_open", awss_open_dev_ap, NULL, 4096);
|
||||
}
|
||||
|
||||
void do_awss()
|
||||
{
|
||||
aos_task_new("dap_close", awss_close_dev_ap, NULL, 2048);
|
||||
aos_task_new("netmgr_start", start_netmgr, NULL, 5120);
|
||||
}
|
||||
|
||||
void linkkit_reset(void *p)
|
||||
{
|
||||
aos_msleep(2000);
|
||||
#ifdef AOS_TIMER_SERVICE
|
||||
timer_service_clear();
|
||||
#endif
|
||||
iotx_sdk_reset_local();
|
||||
#ifdef EN_COMBO_NET
|
||||
breeze_clear_bind_info();
|
||||
#endif
|
||||
HAL_Reboot();
|
||||
aos_task_exit(0);
|
||||
}
|
||||
|
||||
extern int iotx_sdk_reset(iotx_vendor_dev_reset_type_t *reset_type);
|
||||
iotx_vendor_dev_reset_type_t reset_type = IOTX_VENDOR_DEV_RESET_TYPE_UNBIND_ONLY;
|
||||
void do_awss_reset(void)
|
||||
{
|
||||
#ifdef WIFI_PROVISION_ENABLED
|
||||
aos_task_new("reset", (void (*)(void *))iotx_sdk_reset, &reset_type, 6144); // stack taken by iTLS is more than taken by TLS.
|
||||
#endif
|
||||
aos_task_new_ext(&task_linkkit_reset, "reset task", linkkit_reset, NULL, 1024, 0);
|
||||
//aos_post_delayed_action(3000, linkkit_reset, NULL);
|
||||
}
|
||||
|
||||
void reboot_device(void *p)
|
||||
{
|
||||
aos_msleep(500);
|
||||
HAL_Reboot();
|
||||
aos_task_exit(0);
|
||||
}
|
||||
|
||||
void do_awss_reboot(void)
|
||||
{
|
||||
int ret;
|
||||
unsigned char awss_flag = 1;
|
||||
int len = sizeof(awss_flag);
|
||||
|
||||
ret = aos_kv_set("awss_flag", &awss_flag, len, 1);
|
||||
if (ret != 0)
|
||||
LOG("KV Setting failed");
|
||||
|
||||
aos_task_new_ext(&task_reboot_device, "reboot task", reboot_device, NULL, 1024, 0);
|
||||
//aos_post_delayed_action(500, reboot_device, NULL);
|
||||
}
|
||||
|
||||
void linkkit_key_process(input_event_t *eventinfo, void *priv_data)
|
||||
{
|
||||
if (eventinfo->type != EV_KEY) {
|
||||
return;
|
||||
}
|
||||
LOG("awss config press %d\n", eventinfo->value);
|
||||
|
||||
if (eventinfo->code == CODE_BOOT) {
|
||||
if (eventinfo->value == VALUE_KEY_CLICK) {
|
||||
|
||||
do_awss_active();
|
||||
} else if (eventinfo->value == VALUE_KEY_LTCLICK) {
|
||||
do_awss_reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef MANUFACT_AP_FIND_ENABLE
|
||||
void manufact_ap_find_process(int result)
|
||||
{
|
||||
// Informed manufact ap found or not.
|
||||
// If manufact ap found, lower layer will auto connect the manufact ap
|
||||
// IF manufact ap not found, lower layer will enter normal awss state
|
||||
if (result == 0) {
|
||||
LOG("%s ap found.\n", __func__);
|
||||
} else {
|
||||
LOG("%s ap not found.\n", __func__);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_AOS_CLI
|
||||
static void handle_reset_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
aos_schedule_call((aos_call_t)do_awss_reset, NULL);
|
||||
}
|
||||
|
||||
static void handle_active_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
aos_schedule_call((aos_call_t)do_awss_active, NULL);
|
||||
}
|
||||
|
||||
static void handle_dev_ap_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
aos_schedule_call((aos_call_t)do_awss_dev_ap, NULL);
|
||||
}
|
||||
|
||||
#ifdef EN_COMBO_NET
|
||||
static void handle_ble_awss_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
aos_schedule_call((aos_call_t)do_ble_awss, NULL);
|
||||
}
|
||||
#endif
|
||||
|
||||
static void handle_linkkey_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
if (argc == 1) {
|
||||
int len = 0;
|
||||
char product_key[PRODUCT_KEY_LEN + 1] = { 0 };
|
||||
char product_secret[PRODUCT_SECRET_LEN + 1] = { 0 };
|
||||
char device_name[DEVICE_NAME_LEN + 1] = { 0 };
|
||||
char device_secret[DEVICE_SECRET_LEN + 1] = { 0 };
|
||||
char pidStr[9] = { 0 };
|
||||
|
||||
len = PRODUCT_KEY_LEN + 1;
|
||||
aos_kv_get("linkkit_product_key", product_key, &len);
|
||||
|
||||
len = PRODUCT_SECRET_LEN + 1;
|
||||
aos_kv_get("linkkit_product_secret", product_secret, &len);
|
||||
|
||||
len = DEVICE_NAME_LEN + 1;
|
||||
aos_kv_get("linkkit_device_name", device_name, &len);
|
||||
|
||||
len = DEVICE_SECRET_LEN + 1;
|
||||
aos_kv_get("linkkit_device_secret", device_secret, &len);
|
||||
|
||||
aos_cli_printf("Product Key=%s.\r\n", product_key);
|
||||
aos_cli_printf("Device Name=%s.\r\n", device_name);
|
||||
aos_cli_printf("Device Secret=%s.\r\n", device_secret);
|
||||
aos_cli_printf("Product Secret=%s.\r\n", product_secret);
|
||||
len = sizeof(pidStr);
|
||||
if (aos_kv_get("linkkit_product_id", pidStr, &len) == 0) {
|
||||
aos_cli_printf("Product Id=%d.\r\n", atoi(pidStr));
|
||||
}
|
||||
} else if (argc == 5 || argc == 6) {
|
||||
aos_kv_set("linkkit_product_key", argv[1], strlen(argv[1]) + 1, 1);
|
||||
aos_kv_set("linkkit_device_name", argv[2], strlen(argv[2]) + 1, 1);
|
||||
aos_kv_set("linkkit_device_secret", argv[3], strlen(argv[3]) + 1, 1);
|
||||
aos_kv_set("linkkit_product_secret", argv[4], strlen(argv[4]) + 1, 1);
|
||||
if (argc == 6)
|
||||
aos_kv_set("linkkit_product_id", argv[5], strlen(argv[5]) + 1, 1);
|
||||
aos_cli_printf("Done");
|
||||
} else {
|
||||
aos_cli_printf("Error: %d\r\n", __LINE__);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void handle_awss_cmd(char *pwbuf, int blen, int argc, char **argv)
|
||||
{
|
||||
aos_schedule_call(do_awss, NULL);
|
||||
}
|
||||
|
||||
static struct cli_command resetcmd = {
|
||||
.name = "reset",
|
||||
.help = "factory reset",
|
||||
.function = handle_reset_cmd
|
||||
};
|
||||
|
||||
static struct cli_command awss_enable_cmd = {
|
||||
.name = "active_awss",
|
||||
.help = "active_awss [start]",
|
||||
.function = handle_active_cmd
|
||||
};
|
||||
|
||||
static struct cli_command awss_dev_ap_cmd = {
|
||||
.name = "dev_ap",
|
||||
.help = "awss_dev_ap [start]",
|
||||
.function = handle_dev_ap_cmd
|
||||
};
|
||||
|
||||
static struct cli_command awss_cmd = {
|
||||
.name = "awss",
|
||||
.help = "awss [start]",
|
||||
.function = handle_awss_cmd
|
||||
};
|
||||
|
||||
#ifdef EN_COMBO_NET
|
||||
static struct cli_command awss_ble_cmd = {
|
||||
.name = "ble_awss",
|
||||
.help = "ble_awss [start]",
|
||||
.function = handle_ble_awss_cmd
|
||||
};
|
||||
#endif
|
||||
|
||||
static struct cli_command linkkeycmd = {
|
||||
.name = "linkkey",
|
||||
.help = "set/get linkkit keys. linkkey [<Product Key> <Device Name> <Device Secret> <Product Secret>]",
|
||||
.function = handle_linkkey_cmd
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_PRINT_HEAP
|
||||
static void duration_work(void *p)
|
||||
{
|
||||
print_heap();
|
||||
aos_post_delayed_action(5000, duration_work, NULL);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(OTA_ENABLED) && defined(BUILD_AOS)
|
||||
static int ota_init(void);
|
||||
static ota_service_t ctx = {0};
|
||||
#endif
|
||||
static int mqtt_connected_event_handler(void)
|
||||
{
|
||||
#if defined(OTA_ENABLED) && defined(BUILD_AOS)
|
||||
static bool ota_service_inited = false;
|
||||
|
||||
if (ota_service_inited == true) {
|
||||
int ret = 0;
|
||||
|
||||
LOG("MQTT reconnected, let's redo OTA upgrade");
|
||||
if ((ctx.h_tr) && (ctx.h_tr->upgrade)) {
|
||||
LOG("Redoing OTA upgrade");
|
||||
ret = ctx.h_tr->upgrade(&ctx);
|
||||
if (ret < 0) LOG("Failed to do OTA upgrade");
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
LOG("MQTT Construct OTA start to inform");
|
||||
#ifdef DEV_OFFLINE_OTA_ENABLE
|
||||
ota_service_inform(&ctx);
|
||||
#else
|
||||
ota_init();
|
||||
#endif
|
||||
ota_service_inited = true;
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int ota_init(void)
|
||||
{
|
||||
#if defined(OTA_ENABLED) && defined(BUILD_AOS)
|
||||
char product_key[PRODUCT_KEY_LEN + 1] = {0};
|
||||
char device_name[DEVICE_NAME_LEN + 1] = {0};
|
||||
char device_secret[DEVICE_SECRET_LEN + 1] = {0};
|
||||
HAL_GetProductKey(product_key);
|
||||
HAL_GetDeviceName(device_name);
|
||||
HAL_GetDeviceSecret(device_secret);
|
||||
memset(&ctx, 0, sizeof(ota_service_t));
|
||||
strncpy(ctx.pk, product_key, sizeof(ctx.pk)-1);
|
||||
strncpy(ctx.dn, device_name, sizeof(ctx.dn)-1);
|
||||
strncpy(ctx.ds, device_secret, sizeof(ctx.ds)-1);
|
||||
ctx.trans_protcol = 0;
|
||||
ctx.dl_protcol = 3;
|
||||
ota_service_init(&ctx);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void show_firmware_version(void)
|
||||
{
|
||||
printf("\r\n--------Firmware info--------");
|
||||
printf("\r\nHost: %s", COMPILE_HOST);
|
||||
printf("\r\nBranch: %s", GIT_BRANCH);
|
||||
printf("\r\nHash: %s", GIT_HASH);
|
||||
printf("\r\nDate: %s %s", __DATE__, __TIME__);
|
||||
printf("\r\nKernel: %s", aos_get_kernel_version());
|
||||
printf("\r\nLinkKit: %s", LINKKIT_VERSION);
|
||||
printf("\r\nAPP: %s", aos_get_app_version());
|
||||
|
||||
printf("\r\nRegion env: %s\r\n\r\n", REGION_ENV_STRING);
|
||||
}
|
||||
|
||||
int application_start(int argc, char **argv)
|
||||
{
|
||||
#ifdef CONFIG_PRINT_HEAP
|
||||
print_heap();
|
||||
aos_post_delayed_action(5000, duration_work, NULL);
|
||||
#endif
|
||||
|
||||
#ifdef CSP_LINUXHOST
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
#endif
|
||||
|
||||
#ifdef WITH_SAL
|
||||
sal_init();
|
||||
#endif
|
||||
|
||||
#ifdef MDAL_MAL_ICA_TEST
|
||||
HAL_MDAL_MAL_Init();
|
||||
#endif
|
||||
|
||||
#ifdef DEFAULT_LOG_LEVEL_DEBUG
|
||||
IOT_SetLogLevel(IOT_LOG_DEBUG);
|
||||
#else
|
||||
IOT_SetLogLevel(IOT_LOG_WARNING);
|
||||
#endif
|
||||
|
||||
show_firmware_version();
|
||||
/* Must call set_device_meta_info before netmgr_init */
|
||||
set_device_meta_info();
|
||||
netmgr_init();
|
||||
vendor_product_init();
|
||||
dev_diagnosis_module_init();
|
||||
#ifdef DEV_OFFLINE_OTA_ENABLE
|
||||
ota_init();
|
||||
#endif
|
||||
|
||||
#ifdef MANUFACT_AP_FIND_ENABLE
|
||||
netmgr_manuap_info_set("TEST_AP", "TEST_PASSWORD", manufact_ap_find_process);
|
||||
#endif
|
||||
|
||||
aos_register_event_filter(EV_KEY, linkkit_key_process, NULL);
|
||||
aos_register_event_filter(EV_WIFI, wifi_service_event, NULL);
|
||||
aos_register_event_filter(EV_YUNIO, cloud_service_event, NULL);
|
||||
IOT_RegisterCallback(ITE_MQTT_CONNECT_SUCC,mqtt_connected_event_handler);
|
||||
|
||||
#ifdef CONFIG_AOS_CLI
|
||||
aos_cli_register_command(&resetcmd);
|
||||
aos_cli_register_command(&awss_enable_cmd);
|
||||
aos_cli_register_command(&awss_dev_ap_cmd);
|
||||
aos_cli_register_command(&linkkeycmd);
|
||||
aos_cli_register_command(&awss_cmd);
|
||||
#ifdef EN_COMBO_NET
|
||||
aos_cli_register_command(&awss_ble_cmd);
|
||||
#endif
|
||||
#endif
|
||||
init_awss_flag();
|
||||
aos_task_new_ext(&task_key_detect, "detect key pressed", key_detect_event_task, NULL, 1024, AOS_DEFAULT_APP_PRI);
|
||||
|
||||
init_msg_queue();
|
||||
aos_task_new_ext(&task_msg_process, "cmd msg process", msg_process_task, NULL, 2048, AOS_DEFAULT_APP_PRI - 1);
|
||||
#ifdef REPORT_MULTHREAD
|
||||
aos_task_new_ext(&task_property_report, "property report", process_property_report_task, NULL, 2048, AOS_DEFAULT_APP_PRI);
|
||||
#endif
|
||||
check_factory_mode();
|
||||
|
||||
aos_loop_run();
|
||||
|
||||
return 0;
|
||||
}
|
||||
23
Products/example/smart_outlet/app_entry.h
Normal file
23
Products/example/smart_outlet/app_entry.h
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef __APP_ENTRY_H__
|
||||
#define __APP_ENTRY_H__
|
||||
|
||||
#include <aos/aos.h>
|
||||
|
||||
typedef struct {
|
||||
int argc;
|
||||
char **argv;
|
||||
}app_main_paras_t;
|
||||
|
||||
int linkkit_main(void *paras);
|
||||
void do_awss_reset(void);
|
||||
void do_awss_reboot(void);
|
||||
void do_ble_awss(void);
|
||||
void do_awss_dev_ap(void);
|
||||
void stop_netmgr(void *p);
|
||||
void start_netmgr(void *p);
|
||||
|
||||
#endif
|
||||
354
Products/example/smart_outlet/combo_net.c
Normal file
354
Products/example/smart_outlet/combo_net.c
Normal file
|
|
@ -0,0 +1,354 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
|
||||
*/
|
||||
#ifdef EN_COMBO_NET
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "aos/kernel.h"
|
||||
#include <aos/yloop.h>
|
||||
#include "netmgr.h"
|
||||
#include "iot_export.h"
|
||||
#include "iot_import.h"
|
||||
#include "breeze_export.h"
|
||||
#include "combo_net.h"
|
||||
|
||||
#define HAL_IEEE80211_IS_BEACON ieee80211_is_beacon
|
||||
#define HAL_IEEE80211_IS_PROBE_RESP ieee80211_is_probe_resp
|
||||
#define HAL_IEEE80211_GET_SSID ieee80211_get_ssid
|
||||
#define HAL_IEEE80211_GET_BSSID ieee80211_get_bssid
|
||||
#define HAL_AWSS_OPEN_MONITOR HAL_Awss_Open_Monitor
|
||||
#define HAL_AWSS_CLOSE_MONITOR HAL_Awss_Close_Monitor
|
||||
#define HAL_AWSS_SWITCH_CHANNEL HAL_Awss_Switch_Channel
|
||||
|
||||
struct ieee80211_hdr {
|
||||
uint16_t frame_control;
|
||||
uint16_t duration_id;
|
||||
uint8_t addr1[ETH_ALEN];
|
||||
uint8_t addr2[ETH_ALEN];
|
||||
uint8_t addr3[ETH_ALEN];
|
||||
uint16_t seq_ctrl;
|
||||
uint8_t addr4[ETH_ALEN];
|
||||
};
|
||||
|
||||
extern char awss_running;
|
||||
static breeze_apinfo_t apinfo;
|
||||
static char monitor_got_bssid = 0;
|
||||
static netmgr_ap_config_t config;
|
||||
uint8_t g_ble_state = 0;
|
||||
extern uint8_t g_ap_state;
|
||||
uint8_t g_bind_state;
|
||||
extern uint8_t *os_wifi_get_mac(uint8_t mac[6]);
|
||||
|
||||
/* Device info(five elements) will be used by ble breeze */
|
||||
/* ProductKey, ProductSecret, DeviceName, DeviceSecret, ProductID */
|
||||
char g_combo_pk[PRODUCT_KEY_LEN + 1] = { 0 };
|
||||
char g_combo_ps[PRODUCT_SECRET_LEN + 1] = { 0 };
|
||||
char g_combo_dn[DEVICE_NAME_LEN + 1] = { 0 };
|
||||
char g_combo_ds[DEVICE_SECRET_LEN + 1] = { 0 };
|
||||
|
||||
uint32_t g_combo_pid = 0;
|
||||
|
||||
static void combo_status_change_cb(breeze_event_t event)
|
||||
{
|
||||
switch (event) {
|
||||
case CONNECTED:
|
||||
LOG("Connected");
|
||||
g_ble_state = 1;
|
||||
break;
|
||||
|
||||
case DISCONNECTED:
|
||||
LOG("Disconnected");
|
||||
g_ble_state = 0;
|
||||
aos_post_event(EV_BZ_COMBO, COMBO_EVT_CODE_RESTART_ADV, 0);
|
||||
break;
|
||||
|
||||
case AUTHENTICATED:
|
||||
LOG("Authenticated");
|
||||
break;
|
||||
|
||||
case TX_DONE:
|
||||
break;
|
||||
|
||||
case EVT_USER_BIND:
|
||||
LOG("Ble bind");
|
||||
g_bind_state = 1;
|
||||
awss_clear_reset();
|
||||
break;
|
||||
|
||||
case EVT_USER_UNBIND:
|
||||
LOG("Ble unbind");
|
||||
g_bind_state = 0;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void combo_set_dev_status_handler(uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
LOG("COM_SET:%.*s", length, buffer);
|
||||
}
|
||||
|
||||
static void combo_get_dev_status_handler(uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
LOG("COM_GET:%.*s", length, buffer);
|
||||
}
|
||||
|
||||
static void combo_apinfo_rx_cb(breeze_apinfo_t * ap)
|
||||
{
|
||||
if (!ap)
|
||||
return;
|
||||
|
||||
memcpy(&apinfo, ap, sizeof(apinfo));
|
||||
aos_post_event(EV_BZ_COMBO, COMBO_EVT_CODE_AP_INFO, (unsigned long)&apinfo);
|
||||
}
|
||||
|
||||
static int combo_80211_frame_callback(char *buf, int length, enum AWSS_LINK_TYPE link_type, int with_fcs, signed char rssi)
|
||||
{
|
||||
uint8_t ssid[MAX_SSID_LEN] = {0}, bssid[ETH_ALEN] = {0};
|
||||
struct ieee80211_hdr *hdr;
|
||||
int fc;
|
||||
int ret = -1;
|
||||
|
||||
if (link_type != AWSS_LINK_TYPE_NONE) {
|
||||
return -1;
|
||||
}
|
||||
hdr = (struct ieee80211_hdr *)buf;
|
||||
|
||||
fc = hdr->frame_control;
|
||||
if (!HAL_IEEE80211_IS_BEACON(fc) && !HAL_IEEE80211_IS_PROBE_RESP(fc)) {
|
||||
return -1;
|
||||
}
|
||||
ret = HAL_IEEE80211_GET_BSSID(hdr, bssid);
|
||||
if (ret < 0) {
|
||||
return -1;
|
||||
}
|
||||
if (memcmp(config.bssid, bssid, ETH_ALEN) != 0) {
|
||||
return -1;
|
||||
}
|
||||
ret = HAL_IEEE80211_GET_SSID(hdr, length, ssid);
|
||||
if (ret < 0) {
|
||||
return -1;
|
||||
}
|
||||
monitor_got_bssid = 1;
|
||||
strncpy(config.ssid, ssid, sizeof(config.ssid) - 1);
|
||||
HAL_AWSS_CLOSE_MONITOR();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static char is_str_asii(char *str)
|
||||
{
|
||||
for (uint32_t i = 0; str[i] != '\0'; ++i) {
|
||||
if ((uint8_t) str[i] > 0x7F) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void combo_connect_ap(breeze_apinfo_t * info)
|
||||
{
|
||||
uint64_t pre_chn_time = HAL_UptimeMs();
|
||||
int ms_cnt = 0;
|
||||
uint8_t ap_connected = 0;
|
||||
ap_scan_info_t scan_result;
|
||||
int ap_scan_result = -1;
|
||||
|
||||
memset(&config, 0, sizeof(netmgr_ap_config_t));
|
||||
if (!info)
|
||||
return;
|
||||
|
||||
strncpy(config.ssid, info->ssid, sizeof(config.ssid) - 1);
|
||||
strncpy(config.pwd, info->pw, sizeof(config.pwd) - 1);
|
||||
if (info->apptoken_len > 0) {
|
||||
memcpy(config.bssid, info->bssid, ETH_ALEN);
|
||||
awss_set_token(info->apptoken, info->token_type);
|
||||
if (!is_str_asii(config.ssid)) {
|
||||
LOG("SSID not asicci encode");
|
||||
HAL_AWSS_OPEN_MONITOR(combo_80211_frame_callback);
|
||||
while (1) {
|
||||
aos_msleep(50);
|
||||
if (monitor_got_bssid) {
|
||||
break;
|
||||
}
|
||||
uint64_t cur_chn_time = HAL_UptimeMs();
|
||||
if (cur_chn_time - pre_chn_time > 250) {
|
||||
int channel = aws_next_channel();
|
||||
HAL_AWSS_SWITCH_CHANNEL(channel, 0, NULL);
|
||||
pre_chn_time = cur_chn_time;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// get region information
|
||||
if (info->region_type == REGION_TYPE_ID) {
|
||||
LOG("info->region_id: %d", info->region_id);
|
||||
iotx_guider_set_dynamic_region(info->region_id);
|
||||
} else if (info->region_type == REGION_TYPE_MQTTURL) {
|
||||
LOG("info->region_mqtturl: %s", info->region_mqtturl);
|
||||
iotx_guider_set_dynamic_mqtt_url(info->region_mqtturl);
|
||||
} else {
|
||||
LOG("REGION TYPE not supported");
|
||||
iotx_guider_set_dynamic_region(IOTX_CLOUD_REGION_INVALID);
|
||||
}
|
||||
netmgr_set_ap_config(&config);
|
||||
hal_wifi_suspend_station(NULL);
|
||||
netmgr_reconnect_wifi();
|
||||
|
||||
while (ms_cnt < 30000) {
|
||||
// set connect ap timeout
|
||||
if (netmgr_get_ip_state() == false) {
|
||||
aos_msleep(500);
|
||||
ms_cnt += 500;
|
||||
LOG("wait ms_cnt(%d)", ms_cnt);
|
||||
} else {
|
||||
LOG("AP connected");
|
||||
ap_connected = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!ap_connected) {
|
||||
uint16_t err_code = 0;
|
||||
|
||||
// if AP connect fail, should inform the module to suspend station
|
||||
// to avoid module always reconnect and block Upper Layer running
|
||||
hal_wifi_suspend_station(NULL);
|
||||
|
||||
// if ap connect failed in specified timeout, rescan and analyse fail reason
|
||||
memset(&scan_result, 0, sizeof(ap_scan_info_t));
|
||||
LOG("start awss_apscan_process");
|
||||
ap_scan_result = awss_apscan_process(NULL, config.ssid, &scan_result);
|
||||
LOG("stop awss_apscan_process");
|
||||
if ( (ap_scan_result == 0) && (scan_result.found) ) {
|
||||
if (scan_result.rssi < -70) {
|
||||
err_code = 0xC4E1; // rssi too low
|
||||
} else {
|
||||
err_code = 0xC4E3; // AP connect fail(Authentication fail or Association fail or AP exeption)
|
||||
}
|
||||
// should set all ap info to err msg
|
||||
} else {
|
||||
err_code = 0xC4E0; // AP not found
|
||||
}
|
||||
|
||||
if(g_ble_state){
|
||||
uint8_t ble_rsp[DEV_ERRCODE_MSG_MAX_LEN + 8] = {0};
|
||||
uint8_t ble_rsp_idx = 0;
|
||||
ble_rsp[ble_rsp_idx++] = 0x01; // Notify Code Type
|
||||
ble_rsp[ble_rsp_idx++] = 0x01; // Notify Code Length
|
||||
ble_rsp[ble_rsp_idx++] = 0x02; // Notify Code Value, 0x02-fail
|
||||
ble_rsp[ble_rsp_idx++] = 0x03; // Notify SubErrcode Type
|
||||
ble_rsp[ble_rsp_idx++] = sizeof(err_code); // Notify SubErrcode Length
|
||||
memcpy(ble_rsp + ble_rsp_idx, (uint8_t *)&err_code, sizeof(err_code)); // Notify SubErrcode Value
|
||||
ble_rsp_idx += sizeof(err_code);
|
||||
breeze_post(ble_rsp, ble_rsp_idx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void combo_service_evt_handler(input_event_t * event, void *priv_data)
|
||||
{
|
||||
if (event->type != EV_BZ_COMBO) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (event->code == COMBO_EVT_CODE_AP_INFO) {
|
||||
awss_running = 1;
|
||||
combo_connect_ap((breeze_apinfo_t *) event->value);
|
||||
} else if (event->code == COMBO_EVT_CODE_RESTART_ADV) {
|
||||
breeze_stop_advertising();
|
||||
aos_msleep(500); // wait for adv stop
|
||||
breeze_start_advertising(g_ap_state?4:3, g_bind_state, g_bind_state);
|
||||
}
|
||||
}
|
||||
|
||||
static int reverse_byte_array(uint8_t *byte_array, int byte_len) {
|
||||
uint8_t temp = 0;
|
||||
int i = 0;
|
||||
if (!byte_array) {
|
||||
LOG("reverse_mac invalid params!");
|
||||
return -1;
|
||||
}
|
||||
for (i = 0; i < (byte_len / 2); i++) {
|
||||
temp = byte_array[i];
|
||||
byte_array[i] = byte_array[byte_len - i - 1];
|
||||
byte_array[byte_len - i - 1] = temp;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int combo_net_init()
|
||||
{
|
||||
breeze_dev_info_t dinfo = { 0 };
|
||||
int len;
|
||||
|
||||
len = sizeof(g_combo_pk);
|
||||
vendor_get_product_key(g_combo_pk, &len);
|
||||
|
||||
len = sizeof(g_combo_ps);
|
||||
vendor_get_product_secret(g_combo_ps, &len);
|
||||
|
||||
len = sizeof(g_combo_dn);
|
||||
vendor_get_device_name(g_combo_dn, &len);
|
||||
|
||||
len = sizeof(g_combo_ds);
|
||||
vendor_get_device_secret(g_combo_ds, &len);
|
||||
|
||||
vendor_get_product_id(&g_combo_pid);
|
||||
|
||||
aos_register_event_filter(EV_BZ_COMBO, combo_service_evt_handler, NULL);
|
||||
g_bind_state = breeze_get_bind_state();
|
||||
if ((strlen(g_combo_pk) > 0) && (strlen(g_combo_ps) > 0)
|
||||
&& (strlen(g_combo_dn) > 0) && (strlen(g_combo_ds) > 0) && g_combo_pid > 0) {
|
||||
uint8_t combo_adv_mac[6] = {0};
|
||||
os_wifi_get_mac(combo_adv_mac);
|
||||
// Set wifi mac to breeze awss adv, to notify app this is a wifi dev
|
||||
// Awss use wifi module device info.
|
||||
// Only use BT mac when use breeze and other bluetooth communication functions
|
||||
reverse_byte_array(combo_adv_mac, 6);
|
||||
dinfo.product_id = g_combo_pid;
|
||||
dinfo.product_key = g_combo_pk;
|
||||
dinfo.product_secret = g_combo_ps;
|
||||
dinfo.device_name = g_combo_dn;
|
||||
dinfo.device_secret = g_combo_ds;
|
||||
dinfo.dev_adv_mac = combo_adv_mac;
|
||||
breeze_awss_init(&dinfo,
|
||||
combo_status_change_cb,
|
||||
combo_set_dev_status_handler,
|
||||
combo_get_dev_status_handler,
|
||||
combo_apinfo_rx_cb,
|
||||
NULL);
|
||||
breeze_awss_start();
|
||||
breeze_start_advertising(g_ap_state?4:3, g_bind_state, g_bind_state);
|
||||
} else {
|
||||
LOG("combo device info not set!");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int combo_net_deinit()
|
||||
{
|
||||
breeze_awss_stop();
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t combo_ble_conn_state(void)
|
||||
{
|
||||
return g_ble_state;
|
||||
}
|
||||
|
||||
uint8_t combo_get_bind_state(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t combo_get_ap_state(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
24
Products/example/smart_outlet/combo_net.h
Normal file
24
Products/example/smart_outlet/combo_net.h
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifdef EN_COMBO_NET
|
||||
|
||||
#ifndef __COMBO_NET_H__
|
||||
#define __COMBO_NET_H__
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define COMBO_EVT_CODE_AP_INFO 0x0001
|
||||
#define COMBO_EVT_CODE_RESTART_ADV 0x0002
|
||||
|
||||
int combo_net_init(void);
|
||||
int combo_net_deinit(void);
|
||||
uint8_t combo_ble_conn_state(void);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
304
Products/example/smart_outlet/device_state_manger.c
Executable file
304
Products/example/smart_outlet/device_state_manger.c
Executable file
|
|
@ -0,0 +1,304 @@
|
|||
/*
|
||||
*copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <aos/aos.h>
|
||||
#include <aos/yloop.h>
|
||||
#include "netmgr.h"
|
||||
#include "iot_export.h"
|
||||
#include "iot_import.h"
|
||||
#include "vendor.h"
|
||||
#include "app_entry.h"
|
||||
#include "device_state_manger.h"
|
||||
#include "factory.h"
|
||||
#include "smart_outlet.h"
|
||||
|
||||
#define AWSS_TIMEOUT_MS (10 * 60 * 1000)
|
||||
#define CLEAR_AWSS_FLAG_TIMEOUT_MS (30 * 1000)
|
||||
#define CONNECT_AP_FAILED_TIMEOUT_MS (2 * 60 * 1000)
|
||||
|
||||
static aos_timer_t awss_timeout_timer;
|
||||
static aos_timer_t connect_ap_failed_timeout_timer;
|
||||
static aos_timer_t clear_awss_flag_timeout_timer;
|
||||
|
||||
static void app_start_wifi_awss(void);
|
||||
|
||||
static int device_net_state = UNKNOW_STATE;
|
||||
int get_net_state(void)
|
||||
{
|
||||
return device_net_state;
|
||||
}
|
||||
|
||||
void set_net_state(int state)
|
||||
{
|
||||
device_net_state = state;
|
||||
}
|
||||
|
||||
static void device_stop_awss(void)
|
||||
{
|
||||
awss_dev_ap_stop();
|
||||
awss_stop();
|
||||
}
|
||||
|
||||
static void timer_func_awss_timeout(void *arg1, void *arg2)
|
||||
{
|
||||
LOG("awss timeout, stop awss");
|
||||
set_net_state(AWSS_NOT_START);
|
||||
device_stop_awss();
|
||||
aos_timer_stop(&awss_timeout_timer);
|
||||
}
|
||||
|
||||
static void timer_func_connect_ap_failed_timeout(void *arg1, void *arg2)
|
||||
{
|
||||
LOG("connect ap failed timeout");
|
||||
set_net_state(CONNECT_AP_FAILED_TIMEOUT);
|
||||
aos_timer_stop(&connect_ap_failed_timeout_timer);
|
||||
}
|
||||
|
||||
static unsigned char awss_flag = 0;
|
||||
int init_awss_flag(void)
|
||||
{
|
||||
unsigned char value;
|
||||
int ret, len = sizeof(value);
|
||||
|
||||
ret = aos_kv_get("awss_flag", &value, &len);
|
||||
if (ret == 0 && len > 0) {
|
||||
awss_flag = value;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define AWSS_REBOOT_TIMEOUT (4 * 1000)
|
||||
#define AWSS_RESET_TIMEOUT (6 * 1000)
|
||||
#define KEY_PRESSED_VALID_TIME 100
|
||||
#define KEY_DETECT_INTERVAL 50
|
||||
#define AWSS_REBOOT_CNT AWSS_REBOOT_TIMEOUT / KEY_DETECT_INTERVAL
|
||||
#define AWSS_RESET_CNT AWSS_RESET_TIMEOUT / KEY_DETECT_INTERVAL
|
||||
#define KEY_PRESSED_CNT KEY_PRESSED_VALID_TIME / KEY_DETECT_INTERVAL
|
||||
|
||||
void key_detect_event_task(void *arg)
|
||||
{
|
||||
int nCount = 0, awss_mode = 0;
|
||||
int timeout = (AWSS_REBOOT_CNT < AWSS_RESET_TIMEOUT)? AWSS_REBOOT_CNT : AWSS_RESET_TIMEOUT;
|
||||
|
||||
while (1) {
|
||||
if (!product_get_key()) {
|
||||
nCount++;
|
||||
LOG("nCount :%d", nCount);
|
||||
} else {
|
||||
if (nCount >= KEY_PRESSED_CNT && nCount < timeout) {
|
||||
if (product_get_switch() == ON) {
|
||||
product_set_switch(OFF);
|
||||
user_post_powerstate(OFF);
|
||||
} else {
|
||||
product_set_switch(ON);
|
||||
user_post_powerstate(ON);
|
||||
}
|
||||
}
|
||||
if ((awss_flag == 0) && (nCount >= AWSS_REBOOT_CNT)) {
|
||||
LOG("do awss reboot");
|
||||
do_awss_reboot();
|
||||
break;
|
||||
} else if ((awss_flag == 1) && (nCount > AWSS_RESET_CNT)) {
|
||||
LOG("do awss reset");
|
||||
do_awss_reset();
|
||||
break;
|
||||
}
|
||||
nCount = 0;
|
||||
}
|
||||
if ((awss_flag == 0) && (nCount >= AWSS_REBOOT_CNT && awss_mode == 0)) {
|
||||
set_net_state(RECONFIGED);
|
||||
awss_mode = 1;
|
||||
} else if ((awss_flag == 1) && (nCount > AWSS_RESET_CNT && awss_mode == 0)) {
|
||||
set_net_state(UNCONFIGED);
|
||||
awss_mode = 1;
|
||||
}
|
||||
aos_msleep(KEY_DETECT_INTERVAL);
|
||||
}
|
||||
aos_task_exit(0);
|
||||
}
|
||||
|
||||
static void indicate_net_state_task(void *arg)
|
||||
{
|
||||
uint32_t nCount = 0;
|
||||
uint32_t duration = 0;
|
||||
int pre_state = UNKNOW_STATE;
|
||||
int cur_state = UNKNOW_STATE;
|
||||
int switch_stat = 0;
|
||||
|
||||
while (1) {
|
||||
pre_state = cur_state;
|
||||
cur_state = get_net_state();
|
||||
switch (cur_state) {
|
||||
case RECONFIGED:
|
||||
nCount++;
|
||||
if (nCount >= 8) {
|
||||
product_toggle_led();
|
||||
nCount = 0;
|
||||
}
|
||||
break;
|
||||
|
||||
case UNCONFIGED:
|
||||
nCount++;
|
||||
if (nCount >= 2) {
|
||||
product_toggle_led();
|
||||
nCount = 0;
|
||||
}
|
||||
break;
|
||||
|
||||
case AWSS_NOT_START:
|
||||
if (pre_state != cur_state) {
|
||||
switch_stat = (int)product_get_switch();
|
||||
LOG("[net_state]awss timeout, set led %d", switch_stat);
|
||||
product_set_led(switch_stat);
|
||||
}
|
||||
break;
|
||||
|
||||
case GOT_AP_SSID:
|
||||
case CONNECT_CLOUD_FAILED:
|
||||
nCount++;
|
||||
if (nCount >= 8) {
|
||||
product_toggle_led();
|
||||
nCount = 0;
|
||||
}
|
||||
break;
|
||||
|
||||
case CONNECT_AP_FAILED_TIMEOUT:
|
||||
if (pre_state != cur_state) {
|
||||
switch_stat = (int)product_get_switch();
|
||||
LOG("[net_state]connect AP failed timeout, set led %d", switch_stat);
|
||||
product_set_led(switch_stat);
|
||||
}
|
||||
break;
|
||||
|
||||
case CONNECT_AP_FAILED:
|
||||
nCount++;
|
||||
if (nCount >= 5) {
|
||||
product_toggle_led();
|
||||
nCount = 0;
|
||||
}
|
||||
if (pre_state != cur_state) {
|
||||
LOG("[net_state]connect AP failed");
|
||||
aos_timer_new_ext(&connect_ap_failed_timeout_timer,
|
||||
timer_func_connect_ap_failed_timeout, NULL, CONNECT_AP_FAILED_TIMEOUT_MS, 0, 1);
|
||||
device_stop_awss();
|
||||
}
|
||||
break;
|
||||
|
||||
case CONNECT_CLOUD_SUCCESS:
|
||||
if (pre_state != cur_state) {
|
||||
aos_timer_stop(&awss_timeout_timer);
|
||||
|
||||
switch_stat = (int)product_get_switch();
|
||||
LOG("[net_state]connect cloud success, set led %d", switch_stat);
|
||||
product_set_led(switch_stat);
|
||||
}
|
||||
break;
|
||||
|
||||
case APP_BIND_SUCCESS:
|
||||
if (pre_state != cur_state) {
|
||||
set_net_state(CONNECT_CLOUD_SUCCESS);
|
||||
}
|
||||
break;
|
||||
|
||||
case FACTORY_BEGIN:
|
||||
LOG("[net_state]factory begin");
|
||||
set_net_state(FACTORY_SUCCESS);
|
||||
break;
|
||||
|
||||
case FACTORY_SUCCESS:
|
||||
if (pre_state != cur_state) {
|
||||
LOG("[net_state]factory success, set led OFF");
|
||||
product_set_led(OFF);
|
||||
}
|
||||
break;
|
||||
|
||||
case FACTORY_FAILED_1:
|
||||
nCount++;
|
||||
if (nCount >= 5) {
|
||||
product_toggle_led();
|
||||
nCount = 0;
|
||||
}
|
||||
break;
|
||||
|
||||
case FACTORY_FAILED_2:
|
||||
nCount++;
|
||||
if (nCount >= 2) {
|
||||
product_toggle_led();
|
||||
nCount = 0;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
aos_msleep(100);
|
||||
}
|
||||
|
||||
LOG("exit quick_light mode");
|
||||
aos_task_exit(0);
|
||||
}
|
||||
|
||||
static void timer_func_clear_awss_flag_timeout(void *arg1, void *arg2)
|
||||
{
|
||||
LOG("clear awss flag");
|
||||
awss_flag = 0;
|
||||
aos_kv_del("awss_falg");
|
||||
aos_timer_stop(&clear_awss_flag_timeout_timer);
|
||||
}
|
||||
|
||||
static void app_start_wifi_awss(void)
|
||||
{
|
||||
aos_timer_new_ext(&awss_timeout_timer, timer_func_awss_timeout, NULL, AWSS_TIMEOUT_MS, 0, 1);
|
||||
aos_timer_new_ext(&clear_awss_flag_timeout_timer, timer_func_clear_awss_flag_timeout, NULL, CLEAR_AWSS_FLAG_TIMEOUT_MS, 0, 1);
|
||||
|
||||
#if defined (AWSS_ONESHOT_MODE)
|
||||
awss_config_press();
|
||||
do_awss();
|
||||
#elif defined (AWSS_DEV_AP_MODE)
|
||||
do_awss_dev_ap();
|
||||
#elif defined (AWSS_BT_MODE)
|
||||
do_ble_awss();
|
||||
#else
|
||||
#warning "Unsupported awss mode!!!"
|
||||
#endif
|
||||
}
|
||||
|
||||
extern void start_netmgr(void *p);
|
||||
void check_factory_mode(void)
|
||||
{
|
||||
int ret = 0;
|
||||
netmgr_ap_config_t ap_config;
|
||||
memset(&ap_config, 0, sizeof(netmgr_ap_config_t));
|
||||
|
||||
aos_task_new("indicate net state", indicate_net_state_task, NULL, 1024);
|
||||
|
||||
netmgr_get_ap_config(&ap_config);
|
||||
if (strlen(ap_config.ssid) <= 0) {
|
||||
LOG("scan factory ap, set led ON");
|
||||
product_set_led(ON);
|
||||
|
||||
ret = scan_factory_ap();
|
||||
if (0 != ret) {
|
||||
set_net_state(UNCONFIGED);
|
||||
LOG("not enter factory mode, start awss");
|
||||
app_start_wifi_awss();
|
||||
}
|
||||
} else {
|
||||
if (awss_flag == 1) {
|
||||
LOG("start awss with netconfig exist");
|
||||
set_net_state(RECONFIGED);
|
||||
aos_kv_del("awss_flag");
|
||||
app_start_wifi_awss();
|
||||
} else {
|
||||
set_net_state(GOT_AP_SSID);
|
||||
LOG("start connect");
|
||||
aos_task_new("netmgr_start", start_netmgr, NULL, 5120);
|
||||
}
|
||||
}
|
||||
}
|
||||
35
Products/example/smart_outlet/device_state_manger.h
Executable file
35
Products/example/smart_outlet/device_state_manger.h
Executable file
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef __DEVICE_STATE_MANGER_H__
|
||||
#define __DEVICE_STATE_MANGER_H__
|
||||
|
||||
#include <aos/aos.h>
|
||||
|
||||
typedef enum {
|
||||
RECONFIGED = 0, //reconfig with netconfig exist
|
||||
UNCONFIGED, //awss start
|
||||
AWSS_NOT_START, //standby mode(not start softAP)
|
||||
GOT_AP_SSID, //connect AP successfully
|
||||
CONNECT_CLOUD_SUCCESS, //connect cloud successfully
|
||||
CONNECT_CLOUD_FAILED, //connect cloud failded
|
||||
CONNECT_AP_FAILED, //connect ap failed
|
||||
CONNECT_AP_FAILED_TIMEOUT, //connect ap failed timeout
|
||||
APP_BIND_SUCCESS, //bind sucessfully, wait cmd from APP
|
||||
FACTORY_BEGIN, //factory test begin
|
||||
FACTORY_SUCCESS, //factory test successfully
|
||||
FACTORY_FAILED_1, //factory test failed for rssi < -60dbm
|
||||
FACTORY_FAILED_2, //factory test failed
|
||||
UNKNOW_STATE
|
||||
} eNetState;
|
||||
|
||||
int get_net_state(void);
|
||||
void set_net_state(int state);
|
||||
void check_net_config(void);
|
||||
void check_factory_mode(void);
|
||||
void key_detect_event_task(void *arg);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
BIN
Products/example/smart_outlet/doc/插座的标品方案开发文档v1.0.pdf
Executable file
BIN
Products/example/smart_outlet/doc/插座的标品方案开发文档v1.0.pdf
Executable file
Binary file not shown.
75
Products/example/smart_outlet/factory.c
Executable file
75
Products/example/smart_outlet/factory.c
Executable file
|
|
@ -0,0 +1,75 @@
|
|||
/*
|
||||
*copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <aos/aos.h>
|
||||
#include <aos/yloop.h>
|
||||
#include "netmgr.h"
|
||||
#include "iot_export.h"
|
||||
#include "iot_import.h"
|
||||
#include "vendor.h"
|
||||
#include "device_state_manger.h"
|
||||
#include "hal/wifi.h"
|
||||
#include "factory.h"
|
||||
|
||||
#define FACTORY_TEST_AP_SSID "YOUR_SSID"
|
||||
#define FACTORY_TEST_AP_PSW "YOUR_PASSWORD"
|
||||
|
||||
|
||||
|
||||
int scan_factory_ap(void)
|
||||
{
|
||||
/* scan wifi */
|
||||
int ret = 0;
|
||||
ap_scan_info_t scan_result;
|
||||
int ap_scan_result = -1;
|
||||
// start ap scanning for default 3 seconds
|
||||
memset(&scan_result, 0, sizeof(ap_scan_info_t));
|
||||
ap_scan_result = awss_apscan_process(NULL, FACTORY_TEST_AP_SSID, &scan_result);
|
||||
LOG("[FACTORY]scan factory AP result = %d", scan_result.found);
|
||||
if ( (ap_scan_result == 0) && (scan_result.found) ) {
|
||||
enter_factory_mode(scan_result.rssi);
|
||||
} else {
|
||||
ret = -1;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int enter_factory_mode(int8_t rssi)
|
||||
{
|
||||
#ifdef WIFI_PROVISION_ENABLED
|
||||
extern int awss_stop(void);
|
||||
awss_stop();
|
||||
#endif
|
||||
|
||||
/* factory: Don't connect AP */
|
||||
#if 0
|
||||
netmgr_ap_config_t config;
|
||||
strncpy(config.ssid, FACTORY_TEST_AP_SSID, sizeof(config.ssid) - 1);
|
||||
strncpy(config.pwd, FACTORY_TEST_AP_PSW, sizeof(config.pwd) - 1);
|
||||
netmgr_set_ap_config(&config);
|
||||
netmgr_start(false);
|
||||
#endif
|
||||
|
||||
/* RSSI > -60dBm */
|
||||
if (rssi < -60) {
|
||||
LOG("[FACTORY]factory AP power < -60dbm");
|
||||
set_net_state(FACTORY_FAILED_1);
|
||||
} else {
|
||||
LOG("[FACTORY]meter calibrate begin");
|
||||
set_net_state(FACTORY_BEGIN);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int exit_factory_mode()
|
||||
{
|
||||
//need to reboot
|
||||
}
|
||||
|
||||
26
Products/example/smart_outlet/factory.h
Executable file
26
Products/example/smart_outlet/factory.h
Executable file
|
|
@ -0,0 +1,26 @@
|
|||
/*
|
||||
*copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef _FACTORY_H
|
||||
#define _FACTORY_H
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define CODE_FACTORY_WIFI_ON_CONNECT_FAILED 1
|
||||
#define CODE_FACTORY_WIFI_ON_CONNECTED 2
|
||||
|
||||
int scan_factory_ap(void);
|
||||
int enter_factory_mode(int8_t rssi);
|
||||
int exit_factory_mode(void);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif // _FACTORY_H
|
||||
43
Products/example/smart_outlet/make.settings
Executable file
43
Products/example/smart_outlet/make.settings
Executable file
|
|
@ -0,0 +1,43 @@
|
|||
#
|
||||
# Automatically generated file; DO NOT EDIT.
|
||||
# Main Menu
|
||||
#
|
||||
|
||||
#
|
||||
# Configure Link Kit SDK for IoT Embedded Devices
|
||||
#
|
||||
FEATURE_SRCPATH="."
|
||||
FEATURE_MQTT_COMM_ENABLED=y
|
||||
FEATURE_ALCS_ENABLED=y
|
||||
#
|
||||
# MQTT Configurations
|
||||
#
|
||||
# FEATURE_MQTT_SHADOW is not set
|
||||
# FEATURE_CLOUD_OFFLINE_RESET is not set
|
||||
# FEATURE_MQTT_LOGPOST is not set
|
||||
FEATURE_DEVICE_MODEL_ENABLED=y
|
||||
|
||||
FEATURE_MQTT_AUTO_SUBSCRIBE=y
|
||||
|
||||
#
|
||||
# Device Model Configurations
|
||||
#
|
||||
# FEATURE_DEVICE_MODEL_GATEWAY is not set
|
||||
FEATURE_DEV_BIND_ENABLED=y
|
||||
# FEATURE_DEVICE_MODEL_RAWDATA_SOLO is not set
|
||||
# FEATURE_COAP_COMM_ENABLED is not set
|
||||
FEATURE_OTA_ENABLED=y
|
||||
# FEATURE_HTTP2_COMM_ENABLED is not set
|
||||
# FEATURE_HTTP_COMM_ENABLED is not set
|
||||
FEATURE_SUPPORT_TLS=y
|
||||
# FEATURE_SAL_ENABLED is not set
|
||||
FEATURE_WIFI_PROVISION_ENABLED=y
|
||||
|
||||
#
|
||||
# AWSS Configurations
|
||||
#
|
||||
FEATURE_AWSS_SUPPORT_SMARTCONFIG=y
|
||||
FEATURE_AWSS_SUPPORT_ZEROCONFIG=y
|
||||
# FEATURE_AWSS_SUPPORT_PHONEASAP is not set
|
||||
# FEATURE_AWSS_SUPPORT_ROUTER is not set
|
||||
FEATURE_AWSS_SUPPORT_DEV_AP=y
|
||||
123
Products/example/smart_outlet/makefile
Executable file
123
Products/example/smart_outlet/makefile
Executable file
|
|
@ -0,0 +1,123 @@
|
|||
|
||||
include smart_outlet.mk
|
||||
target = smart_outlet
|
||||
|
||||
CFLAGS += -DALCS_ENABLED
|
||||
|
||||
INCLUDE_PATH = -I$(PWD)
|
||||
INCLUDE = $(INCLUDE_PATH)/ \
|
||||
-I$(PWD)/../../../prebuild/include \
|
||||
-I$(PWD)/../../../prebuild/include/exports \
|
||||
-I$(PWD)/../../../prebuild/include/imports \
|
||||
-I$(PWD)/../../../prebuild/include/kernel/rhino/core/include \
|
||||
-I$(PWD)/../../../prebuild/include/framework/netmgr/include \
|
||||
-I$(PWD)/../../../prebuild/include/platform/arch/arm/armv7m/gcc/m4 \
|
||||
-I$(PWD)/../../../prebuild/include/board/$(BOARD)
|
||||
|
||||
app = $(wildcard ./*.c)
|
||||
obj_app = $(patsubst %.c,%.o,$(app))
|
||||
|
||||
CROSS_PREFIX="arm-none-eabi-"
|
||||
DEPENDS=""
|
||||
CCLD=""
|
||||
LDFLAGS=""
|
||||
CC=arm-none-eabi-gcc
|
||||
LD=arm-none-eabi-ld
|
||||
AR=arm-none-eabi-ar
|
||||
STRIP=arm-none-eabi-strip
|
||||
OBJCOPY=arm-none-eabi-objcopy
|
||||
|
||||
# CFLAGS += -MD -ggdb -Os -Wall -Wfatal-errors -fsigned-char -ffunction-sections -fdata-sections -fno-common -std=gnu11 -Werror \
|
||||
# -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
|
||||
CFLAGS += -DWITH_LWIP -DCONFIG_AOS_CLI -DLOG_SIMPLE
|
||||
CFLAGS += ${GLOBAL_CFLAGS}
|
||||
CFLAGS += ${SDK_DEFINES}
|
||||
|
||||
ifeq ("$(BOARD)", "hf-lpt230")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
CFLAGS += -DHF_LPT230
|
||||
else ifeq ("$(BOARD)", "hf-lpt130")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
CFLAGS += -DHF_LPT130
|
||||
else ifeq ("$(BOARD)", "hf-lpb130")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
CFLAGS += -DHF_LPB130
|
||||
else ifeq ("$(BOARD)", "hf-lpb135")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
CFLAGS += -DHF_LPB135
|
||||
else ifeq ("$(BOARD)", "uno-91h")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
CFLAGS += -DUNO_91H
|
||||
|
||||
else ifeq ("$(BOARD)", "mk1101")
|
||||
else ifeq ("$(BOARD)", "mk3060")
|
||||
CFLAGS += -mcpu=arm968e-s -march=armv5te -mthumb -mthumb-interwork -mlittle-endian -w -Os -Wall -Wfatal-errors -fsigned-char -ffunction-sections -fdata-sections -fno-common -std=gnu11
|
||||
CFLAGS += -DMK3060
|
||||
|
||||
else ifeq ("$(BOARD)", "mk3061")
|
||||
CFLAGS += -mcpu=arm968e-s -march=armv5te -mthumb -mthumb-interwork -mlittle-endian -w -Os -Wall -Wfatal-errors -fsigned-char -ffunction-sections -fdata-sections -fno-common -std=gnu11
|
||||
CFLAGS += -DMK3061
|
||||
|
||||
else ifeq ("$(BOARD)", "mk3080")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
CFLAGS += -DMK3080
|
||||
else ifeq ("$(BOARD)", "amebaz_dev")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
CFLAGS += -DAMEBAZ_DEV
|
||||
else ifeq ("$(BOARD)", "mk3092")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
CFLAGS += -DMK3092
|
||||
|
||||
else ifeq ("$(BOARD)", "mk5080")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -march=armv7-m -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -w
|
||||
CFLAGS += -DMK5080
|
||||
|
||||
else ifeq ("$(BOARD)", "asr5501")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -mfpu=fpv4-sp-d16 -std=gnu11 -w
|
||||
CFLAGS += -DASR5501
|
||||
|
||||
else ifeq ("$(BOARD)", "mx1270")
|
||||
CFLAGS += -Os -Werror -fdata-sections -ffunction-sections -mcpu=cortex-m4 -mfloat-abi=soft -mlittle-endian -mthumb -mthumb-interwork -mfpu=fpv4-sp-d16 -std=gnu11 -w
|
||||
CFLAGS += -DMX1270
|
||||
|
||||
else ifeq ("$(BOARD)", "bk7231devkitc")
|
||||
else ifeq ("$(BOARD)", "bk7231sdevkitc")
|
||||
else ifeq ("$(BOARD)", "bk7231udevkitc")
|
||||
CFLAGS += -mcpu=arm968e-s -march=armv5te -mthumb -mthumb-interwork -mlittle-endian -w -Os -Wall -Wfatal-errors -fsigned-char -ffunction-sections -fdata-sections -fno-common -std=gnu11
|
||||
CFLAGS += -DBK7231UDEVKITC
|
||||
else ifeq ("$(BOARD)", "b_l475e")
|
||||
|
||||
else ifeq ("$(BOARD)", "eml3047")
|
||||
else ifeq ("$(BOARD)", "frdmkl27z")
|
||||
else ifeq ("$(BOARD)", "lpcxpresso54102")
|
||||
|
||||
else ifeq ("$(BOARD)", "pca10056")
|
||||
|
||||
## toolchains['arm-rockchip-linux-gnueabihf']
|
||||
else ifeq ("$(BOARD)", "rk1108")
|
||||
|
||||
## toolchains['nds32le-elf-newlib-v3']
|
||||
else ifeq ("$(BOARD)", "sv6266_evb")
|
||||
endif
|
||||
|
||||
ifneq ("$(BOARD)", "")
|
||||
$(info BOARD=${BOARD} CONFIG_COMPILE_DATE=${CONFIG_COMPILE_DATE})
|
||||
endif
|
||||
|
||||
# Compile date
|
||||
CONFIG_COMPILE_DATE ?= $(shell date +%Y%m%d.%H%M%S)
|
||||
all : $(target)
|
||||
|
||||
$(target):$(obj_app)
|
||||
@mkdir -p obj
|
||||
@mv *.o obj
|
||||
@$(RM) -rf *.d
|
||||
@$(AR) -rcs $(PWD)/../../../prebuild/lib/$(target).a obj/*.o 2>/dev/null
|
||||
@$(RM) -rf obj
|
||||
|
||||
%.o:%.c
|
||||
@$(CC) -c $(CFLAGS) $(INCLUDE) -o $@ $<
|
||||
|
||||
clean:
|
||||
@-$(RM) obj/*
|
||||
73
Products/example/smart_outlet/msg_process_center.c
Normal file
73
Products/example/smart_outlet/msg_process_center.c
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
/*
|
||||
*copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <aos/aos.h>
|
||||
#include <aos/yloop.h>
|
||||
#include "netmgr.h"
|
||||
#include "iot_export.h"
|
||||
#include "iot_import.h"
|
||||
#include "smart_outlet.h"
|
||||
#include "vendor.h"
|
||||
#include "msg_process_center.h"
|
||||
#include "property_report.h"
|
||||
|
||||
#define MB_RGBSTATUS_COUNT 10
|
||||
|
||||
static aos_queue_t *g_cmd_msg_queue_id = NULL;
|
||||
static char *g_cmd_msg_queue_buff = NULL;
|
||||
|
||||
aos_queue_t *g_property_report_queue_id = NULL;
|
||||
char *g_property_report_queue_buff = NULL;
|
||||
|
||||
void init_msg_queue(void)
|
||||
{
|
||||
if (g_cmd_msg_queue_buff == NULL) {
|
||||
g_cmd_msg_queue_id = (aos_queue_t *) aos_malloc(sizeof(aos_queue_t));
|
||||
g_cmd_msg_queue_buff = aos_malloc(MB_RGBSTATUS_COUNT * sizeof(recv_msg_t));
|
||||
|
||||
aos_queue_new(g_cmd_msg_queue_id, g_cmd_msg_queue_buff, MB_RGBSTATUS_COUNT * sizeof(recv_msg_t),
|
||||
sizeof(recv_msg_t));
|
||||
}
|
||||
|
||||
if (g_property_report_queue_buff == NULL) {
|
||||
g_property_report_queue_id = (aos_queue_t *) aos_malloc(sizeof(aos_queue_t));
|
||||
g_property_report_queue_buff = aos_malloc(MB_RGBSTATUS_COUNT * sizeof(property_report_msg_t));
|
||||
|
||||
aos_queue_new(g_property_report_queue_id, g_property_report_queue_buff,
|
||||
MB_RGBSTATUS_COUNT * sizeof(property_report_msg_t), sizeof(property_report_msg_t));
|
||||
}
|
||||
}
|
||||
|
||||
void send_msg_to_queue(recv_msg_t * cmd_msg)
|
||||
{
|
||||
int ret = aos_queue_send(g_cmd_msg_queue_id, cmd_msg, sizeof(recv_msg_t));
|
||||
if (0 != ret)
|
||||
LOG_TRACE("###############ERROR: CMD MSG: aos_queue_send failed! #################\r\n");
|
||||
}
|
||||
|
||||
void msg_process_task(void *argv)
|
||||
{
|
||||
uint32_t h, s, v;
|
||||
unsigned int rcvLen;
|
||||
recv_msg_t msg;
|
||||
user_example_ctx_t *user_example_ctx = user_example_get_ctx();
|
||||
device_status_t *device_status = &user_example_ctx->status;
|
||||
|
||||
while (true) {
|
||||
if (aos_queue_recv(g_cmd_msg_queue_id, AOS_WAIT_FOREVER, &msg, &rcvLen) == 0) {
|
||||
device_status->powerswitch = msg.powerswitch;
|
||||
if (msg.powerswitch == 1) {
|
||||
product_set_switch(ON);
|
||||
} else {
|
||||
product_set_switch(OFF);
|
||||
}
|
||||
report_device_property(msg.seq, msg.flag);
|
||||
}
|
||||
}
|
||||
}
|
||||
25
Products/example/smart_outlet/msg_process_center.h
Executable file
25
Products/example/smart_outlet/msg_process_center.h
Executable file
|
|
@ -0,0 +1,25 @@
|
|||
/*
|
||||
*copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef __MSG_PROCESS_CENTER_H__
|
||||
#define __MSG_PROCESS_CENTER_H__
|
||||
|
||||
typedef enum _MSG_FROM{
|
||||
FROM_PROPERTY_SET = 0,
|
||||
FROM_SERVICE_SET
|
||||
} msg_from_t;
|
||||
|
||||
typedef struct _RECV_MSG{
|
||||
uint8_t powerswitch;
|
||||
int flag;
|
||||
char seq[24];
|
||||
uint8_t method;
|
||||
uint8_t from;
|
||||
} recv_msg_t;
|
||||
|
||||
void init_msg_queue(void);
|
||||
void send_msg_to_queue(recv_msg_t* cmd_msg);
|
||||
void msg_process_task(void *argv);
|
||||
|
||||
#endif
|
||||
232
Products/example/smart_outlet/property_report.c
Executable file
232
Products/example/smart_outlet/property_report.c
Executable file
|
|
@ -0,0 +1,232 @@
|
|||
/*
|
||||
*copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <aos/aos.h>
|
||||
#include <aos/yloop.h>
|
||||
#include "netmgr.h"
|
||||
#include "iot_export.h"
|
||||
#include "iot_import.h"
|
||||
|
||||
#include "iot_export_linkkit.h"
|
||||
#include "linkkit_export.h"
|
||||
#include "cJSON.h"
|
||||
#include "aos/kv.h"
|
||||
#include "iot_export.h"
|
||||
#include "smart_outlet.h"
|
||||
#include "vendor.h"
|
||||
#include "device_state_manger.h"
|
||||
#include "msg_process_center.h"
|
||||
#include "property_report.h"
|
||||
|
||||
extern aos_queue_t *g_property_report_queue_id;
|
||||
extern char *g_property_report_queue_buff;
|
||||
|
||||
const char *g_light_property_report[] = {
|
||||
"PowerSwitch",
|
||||
"CommonServiceResponse"
|
||||
};
|
||||
|
||||
#define TIMER_UPDATE_INTERVAL (30 * 60 * 1000)
|
||||
|
||||
static aos_timer_t ntp_timer;
|
||||
static uint64_t ntp_time_ms = 0;
|
||||
static uint32_t basic_time = 0;
|
||||
|
||||
void ntp_timer_update(const char *str)
|
||||
{
|
||||
if (strlen(str) < 13)
|
||||
return;
|
||||
ntp_time_ms = (uint64_t) strtoll(str, NULL, 10);
|
||||
basic_time = aos_now_ms();
|
||||
}
|
||||
|
||||
static void timer_callback(void *arg1, void *arg2)
|
||||
{
|
||||
linkkit_ntp_time_request(ntp_timer_update);
|
||||
}
|
||||
|
||||
static void sync_localtime(void *arg)
|
||||
{
|
||||
linkkit_ntp_time_request(ntp_timer_update);
|
||||
}
|
||||
|
||||
void ntp_server_init(void)
|
||||
{
|
||||
aos_post_delayed_action(500, sync_localtime, NULL);
|
||||
aos_timer_new_ext(&ntp_timer, timer_callback, NULL, TIMER_UPDATE_INTERVAL, 1, 1);
|
||||
}
|
||||
|
||||
static int user_property_format(const char *request_in, const int request_len_in, char **request_out,
|
||||
uint32_t * request_len_out)
|
||||
{
|
||||
cJSON *request_in_json = NULL, *request_out_json = NULL;
|
||||
int index = 0;
|
||||
|
||||
if (ntp_time_ms == 0)
|
||||
return -1;
|
||||
|
||||
/* Parse Request */
|
||||
request_in_json = cJSON_Parse(request_in);
|
||||
if (request_in_json == NULL) {
|
||||
LOG_TRACE("JSON Parse Error");
|
||||
return -1;
|
||||
}
|
||||
/* Prepare Response */
|
||||
request_out_json = cJSON_CreateObject();
|
||||
if (request_out_json == NULL) {
|
||||
LOG_TRACE("No Enough Memory");
|
||||
cJSON_Delete(request_in_json);
|
||||
return -1;
|
||||
}
|
||||
uint64_t timestamp = ntp_time_ms + aos_now_ms() - basic_time;
|
||||
for (index = 0; index < sizeof(g_light_property_report) / sizeof(const char *); index++) {
|
||||
cJSON *item_property = cJSON_CreateObject();
|
||||
if (item_property == NULL) {
|
||||
cJSON_Delete(request_in_json);
|
||||
cJSON_Delete(request_out_json);
|
||||
return -1;
|
||||
}
|
||||
|
||||
cJSON *item_value = cJSON_GetObjectItem(request_in_json, g_light_property_report[index]);
|
||||
if (item_value != NULL) {
|
||||
if (item_value->type == cJSON_Number) {
|
||||
cJSON_AddNumberToObject(item_property, "value", item_value->valueint);
|
||||
} else if (item_value->type == cJSON_String) {
|
||||
cJSON_AddStringToObject(item_property, "value", item_value->valuestring);
|
||||
} else if (item_value->type == cJSON_Object || item_value->type == cJSON_Array) {
|
||||
cJSON *new_item_value = cJSON_Duplicate(item_value, 1);
|
||||
if (new_item_value == NULL) {
|
||||
cJSON_Delete(item_property);
|
||||
cJSON_Delete(request_in_json);
|
||||
cJSON_Delete(request_out_json);
|
||||
return -1;
|
||||
}
|
||||
cJSON_AddItemToObject(item_property, "value", new_item_value);
|
||||
}
|
||||
cJSON_AddNumberToObject(item_property, "time", timestamp);
|
||||
cJSON_AddItemToObject(request_out_json, g_light_property_report[index], item_property);
|
||||
} else {
|
||||
cJSON_Delete(item_property);
|
||||
}
|
||||
}
|
||||
cJSON_Delete(request_in_json);
|
||||
|
||||
*request_out = cJSON_PrintUnformatted(request_out_json);
|
||||
if (*request_out == NULL) {
|
||||
LOG_TRACE("No Enough Memory");
|
||||
cJSON_Delete(request_out_json);
|
||||
return -1;
|
||||
}
|
||||
cJSON_Delete(request_out_json);
|
||||
*request_len_out = strlen(*request_out);
|
||||
|
||||
return SUCCESS_RETURN;
|
||||
}
|
||||
|
||||
void user_post_property(property_report_msg_t * msg)
|
||||
{
|
||||
int res = 0;
|
||||
user_example_ctx_t *user_example_ctx = user_example_get_ctx();
|
||||
char *property_payload = NULL;
|
||||
cJSON *response_root = NULL, *item_csr = NULL;
|
||||
|
||||
response_root = cJSON_CreateObject();
|
||||
if (response_root == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg->seq != NULL && strcmp(msg->seq, SPEC_SEQ)) {
|
||||
item_csr = cJSON_CreateObject();
|
||||
if (item_csr == NULL) {
|
||||
cJSON_Delete(response_root);
|
||||
return;
|
||||
}
|
||||
cJSON_AddStringToObject(item_csr, "seq", msg->seq);
|
||||
cJSON_AddItemToObject(response_root, "CommonServiceResponse", item_csr);
|
||||
}
|
||||
cJSON_AddNumberToObject(response_root, "PowerSwitch", msg->powerswitch);
|
||||
property_payload = cJSON_PrintUnformatted(response_root);
|
||||
cJSON_Delete(response_root);
|
||||
|
||||
char *property_formated;
|
||||
uint32_t len;
|
||||
res = user_property_format(property_payload, strlen(property_payload), &property_formated, &len);
|
||||
if (0 == res) {
|
||||
if (msg->seq != NULL && strcmp(msg->seq, SPEC_SEQ)) {
|
||||
res = IOT_Linkkit_Report_Ext(user_example_ctx->master_devid, ITM_MSG_POST_PROPERTY,
|
||||
(unsigned char *)property_formated, strlen(property_formated), msg->flag);
|
||||
} else {
|
||||
res = IOT_Linkkit_Report(user_example_ctx->master_devid, ITM_MSG_POST_PROPERTY,
|
||||
(unsigned char *)property_formated, strlen(property_formated));
|
||||
}
|
||||
LOG_TRACE("Post Property Message ID: %d Payload %s", res, property_formated);
|
||||
example_free(property_formated);
|
||||
} else {
|
||||
if (msg->seq != NULL && strcmp(msg->seq, SPEC_SEQ)) {
|
||||
res = IOT_Linkkit_Report_Ext(user_example_ctx->master_devid, ITM_MSG_POST_PROPERTY,
|
||||
(unsigned char *)property_payload, strlen(property_payload), msg->flag);
|
||||
} else {
|
||||
res = IOT_Linkkit_Report(user_example_ctx->master_devid, ITM_MSG_POST_PROPERTY,
|
||||
(unsigned char *)property_payload, strlen(property_payload));
|
||||
}
|
||||
LOG_TRACE("Post Property Message ID: %d Payload %s", res, property_payload);
|
||||
}
|
||||
example_free(property_payload);
|
||||
}
|
||||
|
||||
void report_device_property(char *seq, int flag)
|
||||
{
|
||||
user_example_ctx_t *user_example_ctx = user_example_get_ctx();
|
||||
device_status_t *device_status = &user_example_ctx->status;
|
||||
property_report_msg_t msg;
|
||||
|
||||
if (flag == 0x07)
|
||||
return;
|
||||
|
||||
msg.flag = flag;
|
||||
if (seq == NULL) {
|
||||
strcpy(msg.seq, SPEC_SEQ);
|
||||
} else {
|
||||
strcpy(msg.seq, seq);
|
||||
}
|
||||
|
||||
msg.powerswitch = device_status->powerswitch;
|
||||
|
||||
int ret = aos_queue_send(g_property_report_queue_id, &msg, sizeof(property_report_msg_t));
|
||||
if (0 != ret) {
|
||||
LOG_TRACE("###############ERROR: report_light_property aos_queue_send failed! #################\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
#ifndef REPORT_MULTHREAD
|
||||
void process_property_report(void)
|
||||
{
|
||||
unsigned int rcvLen;
|
||||
property_report_msg_t msg;
|
||||
|
||||
if (aos_queue_recv(g_property_report_queue_id, 0, &msg, &rcvLen) == 0) {
|
||||
//LOG_TRACE("===============aos_queue_recved: seq %s============.\r\n", msg.seq);
|
||||
user_post_property(&msg);
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
void process_property_report_task(void *argv)
|
||||
{
|
||||
unsigned int rcvLen;
|
||||
property_report_msg_t msg;
|
||||
|
||||
while (1) {
|
||||
if (aos_queue_recv(g_property_report_queue_id, AOS_WAIT_FOREVER, &msg, &rcvLen) == 0) {
|
||||
user_post_property(&msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
29
Products/example/smart_outlet/property_report.h
Executable file
29
Products/example/smart_outlet/property_report.h
Executable file
|
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
*copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef _PROPERTY_REPORT_H
|
||||
#define _PROPERTY_REPORT_H
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#define SPEC_SEQ "123456@1578666696145"
|
||||
|
||||
typedef struct _PROPERTY_REPORT_MSG {
|
||||
uint8_t powerswitch;
|
||||
char seq[24];
|
||||
uint32_t flag;
|
||||
} property_report_msg_t;
|
||||
|
||||
void report_device_property(char *seq, int flag);
|
||||
void ntp_server_init(void);
|
||||
void process_property_report(void);
|
||||
void process_property_report_task(void *argv);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif // _PROPERTY_REPORT_H
|
||||
40
Products/example/smart_outlet/smart_outlet.h
Executable file
40
Products/example/smart_outlet/smart_outlet.h
Executable file
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
*copyright (C) 2019-2022 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#ifndef __SMART_OUTLET_METER_H__
|
||||
#define __SMART_OUTLET_METER_H__
|
||||
|
||||
#include "aos/aos.h"
|
||||
|
||||
#define DEBUG_OUT 1
|
||||
|
||||
#ifdef DEBUG_OUT
|
||||
#define LOG_TRACE(...) \
|
||||
do { \
|
||||
HAL_Printf("%s.%d: ", __func__, __LINE__); \
|
||||
HAL_Printf(__VA_ARGS__); \
|
||||
HAL_Printf("\r\n"); \
|
||||
} while (0)
|
||||
#else
|
||||
#define LOG_TRACE(...)
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
uint8_t powerswitch;
|
||||
} device_status_t;
|
||||
|
||||
typedef struct {
|
||||
int master_devid;
|
||||
int cloud_connected;
|
||||
int master_initialized;
|
||||
int bind_notified;
|
||||
device_status_t status;
|
||||
} user_example_ctx_t;
|
||||
|
||||
user_example_ctx_t *user_example_get_ctx(void);
|
||||
void user_post_powerstate(int powerstate);
|
||||
void update_power_state(int state);
|
||||
void example_free(void *ptr);
|
||||
|
||||
#endif
|
||||
772
Products/example/smart_outlet/smart_outlet.json
Executable file
772
Products/example/smart_outlet/smart_outlet.json
Executable file
|
|
@ -0,0 +1,772 @@
|
|||
{
|
||||
"schema":"https://iotx-tsl.oss-ap-southeast-1.aliyuncs.com/schema.json",
|
||||
"profile":{
|
||||
"productKey":"a2Yu2ONaISl"
|
||||
},
|
||||
"services":[
|
||||
{
|
||||
"outputData":[
|
||||
|
||||
],
|
||||
"identifier":"set",
|
||||
"inputData":[
|
||||
{
|
||||
"identifier":"PowerSwitch",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"关闭",
|
||||
"1":"开启"
|
||||
},
|
||||
"type":"bool"
|
||||
},
|
||||
"name":"电源开关"
|
||||
},
|
||||
{
|
||||
"identifier":"CommonServiceResponse",
|
||||
"dataType":{
|
||||
"specs":[
|
||||
{
|
||||
"identifier":"seq",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"30"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"seq"
|
||||
}
|
||||
],
|
||||
"type":"struct"
|
||||
},
|
||||
"name":"CommonServiceResponse"
|
||||
},
|
||||
{
|
||||
"identifier":"setPropsExtends",
|
||||
"dataType":{
|
||||
"specs":[
|
||||
{
|
||||
"identifier":"seq",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"32"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"seq"
|
||||
},
|
||||
{
|
||||
"identifier":"flag",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"max":"65535",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"flag"
|
||||
}
|
||||
],
|
||||
"type":"struct"
|
||||
},
|
||||
"name":"setPropsExtends"
|
||||
},
|
||||
{
|
||||
"identifier":"LocalTimer",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"item":{
|
||||
"specs":[
|
||||
{
|
||||
"identifier":"Timer",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"255"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"定时时间"
|
||||
},
|
||||
{
|
||||
"identifier":"Enable",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"关",
|
||||
"1":"开"
|
||||
},
|
||||
"type":"bool"
|
||||
},
|
||||
"name":"启用"
|
||||
},
|
||||
{
|
||||
"identifier":"IsValid",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"无效",
|
||||
"1":"有效"
|
||||
},
|
||||
"type":"bool"
|
||||
},
|
||||
"name":"可执行"
|
||||
},
|
||||
{
|
||||
"identifier":"PowerSwitch",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"关闭",
|
||||
"1":"开启"
|
||||
},
|
||||
"type":"bool"
|
||||
},
|
||||
"name":"电源开关"
|
||||
}
|
||||
],
|
||||
"type":"struct"
|
||||
},
|
||||
"size":"5"
|
||||
},
|
||||
"type":"array"
|
||||
},
|
||||
"name":"本地定时"
|
||||
},
|
||||
{
|
||||
"identifier":"CountDownList",
|
||||
"dataType":{
|
||||
"specs":[
|
||||
{
|
||||
"identifier":"Target",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"2048"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"操作对象"
|
||||
},
|
||||
{
|
||||
"identifier":"Contents",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"2048"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"倒计时命令"
|
||||
},
|
||||
{
|
||||
"identifier":"PowerSwitch",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"关闭",
|
||||
"1":"开启"
|
||||
},
|
||||
"type":"bool"
|
||||
},
|
||||
"name":"电源开关"
|
||||
}
|
||||
],
|
||||
"type":"struct"
|
||||
},
|
||||
"name":"倒计时列表"
|
||||
}
|
||||
],
|
||||
"method":"thing.service.property.set",
|
||||
"name":"set",
|
||||
"required":true,
|
||||
"callType":"async",
|
||||
"desc":"属性设置"
|
||||
},
|
||||
{
|
||||
"outputData":[
|
||||
{
|
||||
"identifier":"PowerSwitch",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"关闭",
|
||||
"1":"开启"
|
||||
},
|
||||
"type":"bool"
|
||||
},
|
||||
"name":"电源开关"
|
||||
},
|
||||
{
|
||||
"identifier":"CommonServiceResponse",
|
||||
"dataType":{
|
||||
"specs":[
|
||||
{
|
||||
"identifier":"seq",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"30"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"seq"
|
||||
}
|
||||
],
|
||||
"type":"struct"
|
||||
},
|
||||
"name":"CommonServiceResponse"
|
||||
},
|
||||
{
|
||||
"identifier":"setPropsExtends",
|
||||
"dataType":{
|
||||
"specs":[
|
||||
{
|
||||
"identifier":"seq",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"32"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"seq"
|
||||
},
|
||||
{
|
||||
"identifier":"flag",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"max":"65535",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"flag"
|
||||
}
|
||||
],
|
||||
"type":"struct"
|
||||
},
|
||||
"name":"setPropsExtends"
|
||||
},
|
||||
{
|
||||
"identifier":"LocalTimer",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"item":{
|
||||
"specs":[
|
||||
{
|
||||
"identifier":"Timer",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"255"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"定时时间"
|
||||
},
|
||||
{
|
||||
"identifier":"Enable",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"关",
|
||||
"1":"开"
|
||||
},
|
||||
"type":"bool"
|
||||
},
|
||||
"name":"启用"
|
||||
},
|
||||
{
|
||||
"identifier":"IsValid",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"无效",
|
||||
"1":"有效"
|
||||
},
|
||||
"type":"bool"
|
||||
},
|
||||
"name":"可执行"
|
||||
},
|
||||
{
|
||||
"identifier":"PowerSwitch",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"关闭",
|
||||
"1":"开启"
|
||||
},
|
||||
"type":"bool"
|
||||
},
|
||||
"name":"电源开关"
|
||||
}
|
||||
],
|
||||
"type":"struct"
|
||||
},
|
||||
"size":"5"
|
||||
},
|
||||
"type":"array"
|
||||
},
|
||||
"name":"本地定时"
|
||||
},
|
||||
{
|
||||
"identifier":"CountDownList",
|
||||
"dataType":{
|
||||
"specs":[
|
||||
{
|
||||
"identifier":"Target",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"2048"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"操作对象"
|
||||
},
|
||||
{
|
||||
"identifier":"Contents",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"2048"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"倒计时命令"
|
||||
},
|
||||
{
|
||||
"identifier":"PowerSwitch",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"关闭",
|
||||
"1":"开启"
|
||||
},
|
||||
"type":"bool"
|
||||
},
|
||||
"name":"电源开关"
|
||||
}
|
||||
],
|
||||
"type":"struct"
|
||||
},
|
||||
"name":"倒计时列表"
|
||||
}
|
||||
],
|
||||
"identifier":"get",
|
||||
"inputData":[
|
||||
"PowerSwitch",
|
||||
"CommonServiceResponse",
|
||||
"setPropsExtends",
|
||||
"LocalTimer",
|
||||
"CountDownList"
|
||||
],
|
||||
"method":"thing.service.property.get",
|
||||
"name":"get",
|
||||
"required":true,
|
||||
"callType":"async",
|
||||
"desc":"属性获取"
|
||||
},
|
||||
{
|
||||
"outputData":[
|
||||
|
||||
],
|
||||
"identifier":"CommonService",
|
||||
"inputData":[
|
||||
{
|
||||
"identifier":"method",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"setProps"
|
||||
},
|
||||
"type":"enum"
|
||||
},
|
||||
"name":"method"
|
||||
},
|
||||
{
|
||||
"identifier":"params",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"256"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"params"
|
||||
},
|
||||
{
|
||||
"identifier":"flag",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"max":"65535",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"flag"
|
||||
},
|
||||
{
|
||||
"identifier":"seq",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"30"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"seq"
|
||||
}
|
||||
],
|
||||
"method":"thing.service.CommonService",
|
||||
"name":"CommonService",
|
||||
"required":false,
|
||||
"callType":"async"
|
||||
}
|
||||
],
|
||||
"properties":[
|
||||
{
|
||||
"identifier":"PowerSwitch",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"关闭",
|
||||
"1":"开启"
|
||||
},
|
||||
"type":"bool"
|
||||
},
|
||||
"name":"电源开关",
|
||||
"accessMode":"rw",
|
||||
"required":true
|
||||
},
|
||||
{
|
||||
"identifier":"CommonServiceResponse",
|
||||
"dataType":{
|
||||
"specs":[
|
||||
{
|
||||
"identifier":"seq",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"30"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"seq"
|
||||
}
|
||||
],
|
||||
"type":"struct"
|
||||
},
|
||||
"name":"CommonServiceResponse",
|
||||
"accessMode":"rw",
|
||||
"required":false
|
||||
},
|
||||
{
|
||||
"identifier":"setPropsExtends",
|
||||
"dataType":{
|
||||
"specs":[
|
||||
{
|
||||
"identifier":"seq",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"32"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"seq"
|
||||
},
|
||||
{
|
||||
"identifier":"flag",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"max":"65535",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"flag"
|
||||
}
|
||||
],
|
||||
"type":"struct"
|
||||
},
|
||||
"name":"setPropsExtends",
|
||||
"accessMode":"rw",
|
||||
"required":false
|
||||
},
|
||||
{
|
||||
"identifier":"LocalTimer",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"item":{
|
||||
"specs":[
|
||||
{
|
||||
"identifier":"Timer",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"255"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"定时时间"
|
||||
},
|
||||
{
|
||||
"identifier":"Enable",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"关",
|
||||
"1":"开"
|
||||
},
|
||||
"type":"bool"
|
||||
},
|
||||
"name":"启用"
|
||||
},
|
||||
{
|
||||
"identifier":"IsValid",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"无效",
|
||||
"1":"有效"
|
||||
},
|
||||
"type":"bool"
|
||||
},
|
||||
"name":"可执行"
|
||||
},
|
||||
{
|
||||
"identifier":"PowerSwitch",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"关闭",
|
||||
"1":"开启"
|
||||
},
|
||||
"type":"bool"
|
||||
},
|
||||
"name":"电源开关"
|
||||
}
|
||||
],
|
||||
"type":"struct"
|
||||
},
|
||||
"size":"5"
|
||||
},
|
||||
"type":"array"
|
||||
},
|
||||
"name":"本地定时",
|
||||
"accessMode":"rw",
|
||||
"required":true,
|
||||
"desc":"用于为设备配置本地控制,其中前三个参数必选,其它参数自主填写,用于表示该产品哪些功能作为“可被本地控制的功能”,这些参数请和设备的标准功能保持一致。"
|
||||
},
|
||||
{
|
||||
"identifier":"CountDownList",
|
||||
"dataType":{
|
||||
"specs":[
|
||||
{
|
||||
"identifier":"Target",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"2048"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"操作对象"
|
||||
},
|
||||
{
|
||||
"identifier":"Contents",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"2048"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"倒计时命令"
|
||||
},
|
||||
{
|
||||
"identifier":"PowerSwitch",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"关闭",
|
||||
"1":"开启"
|
||||
},
|
||||
"type":"bool"
|
||||
},
|
||||
"name":"电源开关"
|
||||
}
|
||||
],
|
||||
"type":"struct"
|
||||
},
|
||||
"name":"倒计时列表",
|
||||
"accessMode":"rw",
|
||||
"required":true
|
||||
}
|
||||
],
|
||||
"events":[
|
||||
{
|
||||
"outputData":[
|
||||
{
|
||||
"identifier":"PowerSwitch",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"关闭",
|
||||
"1":"开启"
|
||||
},
|
||||
"type":"bool"
|
||||
},
|
||||
"name":"电源开关"
|
||||
},
|
||||
{
|
||||
"identifier":"CommonServiceResponse",
|
||||
"dataType":{
|
||||
"specs":[
|
||||
{
|
||||
"identifier":"seq",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"30"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"seq"
|
||||
}
|
||||
],
|
||||
"type":"struct"
|
||||
},
|
||||
"name":"CommonServiceResponse"
|
||||
},
|
||||
{
|
||||
"identifier":"setPropsExtends",
|
||||
"dataType":{
|
||||
"specs":[
|
||||
{
|
||||
"identifier":"seq",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"32"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"seq"
|
||||
},
|
||||
{
|
||||
"identifier":"flag",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"min":"0",
|
||||
"max":"65535",
|
||||
"step":"1"
|
||||
},
|
||||
"type":"int"
|
||||
},
|
||||
"name":"flag"
|
||||
}
|
||||
],
|
||||
"type":"struct"
|
||||
},
|
||||
"name":"setPropsExtends"
|
||||
},
|
||||
{
|
||||
"identifier":"LocalTimer",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"item":{
|
||||
"specs":[
|
||||
{
|
||||
"identifier":"Timer",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"255"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"定时时间"
|
||||
},
|
||||
{
|
||||
"identifier":"Enable",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"关",
|
||||
"1":"开"
|
||||
},
|
||||
"type":"bool"
|
||||
},
|
||||
"name":"启用"
|
||||
},
|
||||
{
|
||||
"identifier":"IsValid",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"无效",
|
||||
"1":"有效"
|
||||
},
|
||||
"type":"bool"
|
||||
},
|
||||
"name":"可执行"
|
||||
},
|
||||
{
|
||||
"identifier":"PowerSwitch",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"关闭",
|
||||
"1":"开启"
|
||||
},
|
||||
"type":"bool"
|
||||
},
|
||||
"name":"电源开关"
|
||||
}
|
||||
],
|
||||
"type":"struct"
|
||||
},
|
||||
"size":"5"
|
||||
},
|
||||
"type":"array"
|
||||
},
|
||||
"name":"本地定时"
|
||||
},
|
||||
{
|
||||
"identifier":"CountDownList",
|
||||
"dataType":{
|
||||
"specs":[
|
||||
{
|
||||
"identifier":"Target",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"2048"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"操作对象"
|
||||
},
|
||||
{
|
||||
"identifier":"Contents",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"length":"2048"
|
||||
},
|
||||
"type":"text"
|
||||
},
|
||||
"name":"倒计时命令"
|
||||
},
|
||||
{
|
||||
"identifier":"PowerSwitch",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"关闭",
|
||||
"1":"开启"
|
||||
},
|
||||
"type":"bool"
|
||||
},
|
||||
"name":"电源开关"
|
||||
}
|
||||
],
|
||||
"type":"struct"
|
||||
},
|
||||
"name":"倒计时列表"
|
||||
}
|
||||
],
|
||||
"identifier":"post",
|
||||
"method":"thing.event.property.post",
|
||||
"name":"post",
|
||||
"type":"info",
|
||||
"required":true,
|
||||
"desc":"属性上报"
|
||||
},
|
||||
{
|
||||
"outputData":[
|
||||
{
|
||||
"identifier":"ErrorCode",
|
||||
"dataType":{
|
||||
"specs":{
|
||||
"0":"0"
|
||||
},
|
||||
"type":"enum"
|
||||
},
|
||||
"name":"故障代码"
|
||||
}
|
||||
],
|
||||
"identifier":"Error",
|
||||
"method":"thing.event.Error.post",
|
||||
"name":"故障上报",
|
||||
"type":"error",
|
||||
"required":true
|
||||
}
|
||||
]
|
||||
}
|
||||
173
Products/example/smart_outlet/smart_outlet.mk
Normal file
173
Products/example/smart_outlet/smart_outlet.mk
Normal file
|
|
@ -0,0 +1,173 @@
|
|||
NAME := smart_outlet
|
||||
|
||||
# Cloud server region: MAINLAND or SINGAPORE, GERMANY
|
||||
CONFIG_SERVER_REGION ?= SINGAPORE
|
||||
|
||||
# Cloud server env: ON_DAILY, ON_PRE, ONLINE
|
||||
CONFIG_SERVER_ENV ?= ONLINE
|
||||
# AWSS MODE: AWSS_ONESHOT_MODE, AWSS_DEV_AP_MODE, AWSS_BT_MODE
|
||||
CONFIG_AWSS_MODE ?= AWSS_DEV_AP_MODE
|
||||
|
||||
CONFIG_FIRMWARE_VERSION = app-1.6.0
|
||||
|
||||
# Compile date and time
|
||||
ifeq ("${DATE}", "")
|
||||
DATE=date
|
||||
endif
|
||||
CONFIG_COMPILE_DATE ?= $(shell ${DATE} +%Y%m%d.%H%M%S)
|
||||
|
||||
# Firmware type: 0 - release, 1 - debug
|
||||
CONFIG_DEBUG ?= 0
|
||||
|
||||
# Manufacturer self-defined AP auto find, default close
|
||||
CONFIG_MANUFACT_AP_FIND ?= FALSE
|
||||
|
||||
CONFIG_BOARD_NAME = $(shell echo $(PLATFORM) | tr a-z A-Z | tr '-' '_')
|
||||
|
||||
$(NAME)_SOURCES := app_entry.c \
|
||||
smart_outlet_main.c \
|
||||
vendor.c \
|
||||
device_state_manger.c \
|
||||
factory.c \
|
||||
property_report.c \
|
||||
msg_process_center.c
|
||||
|
||||
ifeq ($(CONFIG_BOARD_NAME), BK7231UDEVKITC)
|
||||
$(NAME)_SOURCES += combo_net.c
|
||||
CONFIG_COMBOAPP := 1
|
||||
CONFIG_AWSS_MODE = AWSS_BT_MODE
|
||||
endif
|
||||
|
||||
$(NAME)_COMPONENTS += framework/protocol/linkkit/sdk \
|
||||
framework/protocol/linkkit/hal \
|
||||
framework/netmgr \
|
||||
framework/common \
|
||||
utility/cjson \
|
||||
framework/uOTA
|
||||
|
||||
GLOBAL_INCLUDES += ../../../../framework/protocol/linkkit/include \
|
||||
../../../../framework/protocol/linkkit/include/imports \
|
||||
../../../../framework/protocol/linkkit/include/exports \
|
||||
|
||||
GLOBAL_CFLAGS += -DCONFIG_SDK_THREAD_COST=1
|
||||
GLOBAL_CFLAGS += -DCONFIG_YWSS \
|
||||
-DBUILD_AOS \
|
||||
-DAWSS_SUPPORT_STATIS \
|
||||
-DDEV_ERRCODE_ENABLE \
|
||||
-DDEV_OFFLINE_OTA_ENABLE \
|
||||
-DALCS_GROUP_COMM_ENABLE \
|
||||
-DDM_UNIFIED_SERVICE_POST \
|
||||
-DAWSS_BATCH_DEVAP_ENABLE
|
||||
|
||||
#GLOBAL_CFLAGS += -DDEV_OFFLINE_SECURE_OTA_ENABLE
|
||||
|
||||
ifeq ($(CONFIG_MANUFACT_AP_FIND), ENABLE)
|
||||
GLOBAL_CFLAGS += -DMANUFACT_AP_FIND_ENABLE
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_SUPPORT_MCU_OTA), ENABLE)
|
||||
GLOBAL_CFLAGS += -DSUPPORT_MCU_OTA
|
||||
endif
|
||||
|
||||
GLOBAL_CFLAGS += -DREGION_${CONFIG_SERVER_REGION}
|
||||
|
||||
GLOBAL_CFLAGS += -D${CONFIG_BOARD_NAME}
|
||||
|
||||
GLOBAL_CFLAGS += -D${CONFIG_AWSS_MODE}
|
||||
|
||||
GLOBAL_CFLAGS += -D${CONFIG_SERVER_ENV}
|
||||
|
||||
GLOBAL_CFLAGS += -DAOS_TIMER_SERVICE
|
||||
GLOBAL_CFLAGS += -DENABLE_COUNTDOWN_LIST
|
||||
GLOBAL_CFLAGS += -DENABLE_LOCALTIMER
|
||||
GLOBAL_CFLAGS += -DENABLE_PERIOD_TIMER
|
||||
GLOBAL_CFLAGS += -DENABLE_RANDOM_TIMER
|
||||
|
||||
GLOBAL_CFLAGS += -DREPORT_MULTHREAD
|
||||
|
||||
ifeq ($(CONFIG_MANUFACT_AP_FIND), ENABLE)
|
||||
GLOBAL_CFLAGS += -DMANUFACT_AP_FIND_ENABLE
|
||||
endif
|
||||
|
||||
BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
|
||||
GLOBAL_CFLAGS += -DGIT_BRANCH=\"${BRANCH}\"
|
||||
COMMIT_HASH := $(shell git rev-parse HEAD)
|
||||
GLOBAL_CFLAGS += -DGIT_HASH=\"${COMMIT_HASH}\"
|
||||
COMPILE_USER := ${USER}
|
||||
GLOBAL_CFLAGS += -DCOMPILE_HOST=\"${COMPILE_USER}\"
|
||||
SERVER_CONF_STRING := "${CONFIG_SERVER_REGION}-${CONFIG_SERVER_ENV}"
|
||||
GLOBAL_CFLAGS += -DREGION_ENV_STRING=\"${SERVER_CONF_STRING}\"
|
||||
GLOBAL_CFLAGS += -DAPP_NAME=\"${APP}\"
|
||||
GLOBAL_CFLAGS += -DPLATFORM=\"${PLATFORM}\"
|
||||
CONFIG_SYSINFO_APP_VERSION = ${CONFIG_FIRMWARE_VERSION}-${CONFIG_COMPILE_DATE}
|
||||
GLOBAL_CFLAGS += -DSYSINFO_APP_VERSION=\"$(CONFIG_SYSINFO_APP_VERSION)\"
|
||||
|
||||
$(info CONFIG_BOARD_NAME : $(CONFIG_BOARD_NAME))
|
||||
$(info server region: ${CONFIG_SERVER_REGION})
|
||||
$(info server env: ${CONFIG_SERVER_ENV})
|
||||
$(info APP: ${APP} Board: ${PLATFORM})
|
||||
$(info host user: ${COMPILE_USER})
|
||||
$(info branch: ${BRANCH})
|
||||
$(info hash: ${COMMIT_HASH})
|
||||
$(info app_version_new:${CONFIG_SYSINFO_APP_VERSION})
|
||||
|
||||
ifeq ($(CONFIG_DEBUG), 1)
|
||||
GLOBAL_CFLAGS += -DDEFAULT_LOG_LEVEL_DEBUG
|
||||
GLOBAL_CFLAGS += -DCONFIG_BLDTIME_MUTE_DBGLOG=0
|
||||
$(info firmware type: DEBUG)
|
||||
else
|
||||
GLOBAL_CFLAGS += -DCONFIG_BLDTIME_MUTE_DBGLOG=1
|
||||
$(info firmware type: RELEASE)
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_COMBOAPP), 1)
|
||||
$(NAME)_COMPONENTS += framework.bluetooth.breeze
|
||||
GLOBAL_CFLAGS += -DEN_COMBO_NET
|
||||
bz_en_auth := 1
|
||||
bz_en_awss := 1
|
||||
offline_auth := 1
|
||||
ble := 1
|
||||
endif
|
||||
|
||||
ifeq ($(LWIP),1)
|
||||
$(NAME)_COMPONENTS += protocols.net
|
||||
no_with_lwip := 0
|
||||
endif
|
||||
|
||||
ifeq ($(print_heap),1)
|
||||
GLOBAL_CFLAGS += -DCONFIG_PRINT_HEAP
|
||||
endif
|
||||
|
||||
ifneq ($(HOST_MCU_FAMILY),esp8266)
|
||||
$(NAME)_COMPONENTS += cli
|
||||
GLOBAL_CFLAGS += -DCONFIG_AOS_CLI
|
||||
else
|
||||
GLOBAL_CFLAGS += -DESP8266_CHIPSET
|
||||
endif
|
||||
|
||||
ifeq (y,$(pvtest))
|
||||
GLOBAL_CFLAGS += -DPREVALIDATE_TEST
|
||||
endif
|
||||
|
||||
GLOBAL_INCLUDES += ./
|
||||
|
||||
include ./make.settings
|
||||
|
||||
SWITCH_VARS := FEATURE_MQTT_COMM_ENABLED FEATURE_ALCS_ENABLED FEATURE_DEVICE_MODEL_ENABLED \
|
||||
FEATURE_DEVICE_MODEL_GATEWAY FEATURE_DEV_BIND_ENABLED FEATURE_MQTT_SHADOW FEATURE_CLOUD_OFFLINE_RESET \
|
||||
FEATURE_DEVICE_MODEL_RAWDATA_SOLO FEATURE_COAP_COMM_ENABLED FEATURE_HTTP2_COMM_ENABLED \
|
||||
FEATURE_HTTP_COMM_ENABLED FEATURE_SAL_ENABLED FEATURE_WIFI_PROVISION_ENABLED FEATURE_AWSS_SUPPORT_SMARTCONFIG\
|
||||
FEATURE_AWSS_SUPPORT_ZEROCONFIG FEATURE_AWSS_SUPPORT_SMARTCONFIG FEATURE_AWSS_SUPPORT_ZEROCONFIG FEATURE_AWSS_SUPPORT_PHONEASAP \
|
||||
FEATURE_AWSS_SUPPORT_ROUTER FEATURE_AWSS_SUPPORT_DEV_AP FEATURE_OTA_ENABLED FEATURE_MQTT_AUTO_SUBSCRIBE FEATURE_MQTT_PREAUTH_SUPPORT_HTTPS_CDN
|
||||
|
||||
$(foreach v, \
|
||||
$(SWITCH_VARS), \
|
||||
$(if $(filter y,$($(v))), \
|
||||
$(eval GLOBAL_CFLAGS += -D$(subst FEATURE_,,$(v)))) \
|
||||
)
|
||||
|
||||
$(foreach v, \
|
||||
$(SWITCH_VARS), \
|
||||
$(if $(filter y,$($(v))), \
|
||||
$(eval SDK_DEFINES += $(subst FEATURE_,-D,$(v)))) \
|
||||
)
|
||||
762
Products/example/smart_outlet/smart_outlet_main.c
Executable file
762
Products/example/smart_outlet/smart_outlet_main.c
Executable file
|
|
@ -0,0 +1,762 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
#include "stdio.h"
|
||||
#include "iot_export_linkkit.h"
|
||||
#include "cJSON.h"
|
||||
#include "app_entry.h"
|
||||
#include "aos/kv.h"
|
||||
#ifdef AOS_TIMER_SERVICE
|
||||
#include "iot_export_timer.h"
|
||||
#endif
|
||||
#include "smart_outlet.h"
|
||||
#include "vendor.h"
|
||||
#include "device_state_manger.h"
|
||||
#include "msg_process_center.h"
|
||||
#include "property_report.h"
|
||||
|
||||
#define USER_EXAMPLE_YIELD_TIMEOUT_MS (30)
|
||||
|
||||
#define RESPONE_BUFFER_SIZE 128
|
||||
|
||||
#ifdef AOS_TIMER_SERVICE
|
||||
#define NUM_OF_PROPERTYS 1 /* <=30 dont add timer property */
|
||||
const char *control_targets_list[NUM_OF_PROPERTYS] = { "PowerSwitch" };
|
||||
static int num_of_tsl_type[NUM_OF_TSL_TYPES] = { 1, 0, 0 }; /* 1:int/enum/bool; 2:float/double; 3:text/date */
|
||||
|
||||
#define NUM_OF_COUNTDOWN_LIST_TARGET 1 /* <=10 */
|
||||
const char *countdownlist_target_list[NUM_OF_COUNTDOWN_LIST_TARGET] = { "PowerSwitch" };
|
||||
|
||||
#define NUM_OF_LOCAL_TIMER_TARGET 1 /* <=5 */
|
||||
const char *localtimer_target_list[NUM_OF_LOCAL_TIMER_TARGET] = { "PowerSwitch" };
|
||||
#endif
|
||||
|
||||
static user_example_ctx_t g_user_example_ctx;
|
||||
|
||||
void user_post_property_json(const char *property);
|
||||
|
||||
user_example_ctx_t *user_example_get_ctx(void)
|
||||
{
|
||||
return &g_user_example_ctx;
|
||||
}
|
||||
|
||||
void *example_malloc(size_t size)
|
||||
{
|
||||
return HAL_Malloc(size);
|
||||
}
|
||||
|
||||
void example_free(void *ptr)
|
||||
{
|
||||
HAL_Free(ptr);
|
||||
}
|
||||
|
||||
void update_power_state(int state)
|
||||
{
|
||||
user_example_ctx_t *user_example_ctx = user_example_get_ctx();
|
||||
|
||||
user_example_ctx->status.powerswitch = state;
|
||||
}
|
||||
|
||||
static void user_deviceinfo_update(void)
|
||||
{
|
||||
int res = 0;
|
||||
user_example_ctx_t *user_example_ctx = user_example_get_ctx();
|
||||
char *device_info_update = "[{\"attrKey\":\"OutletFWVersion\",\"attrValue\":\"smo_1.5.1\"}]";
|
||||
|
||||
res = IOT_Linkkit_Report(user_example_ctx->master_devid, ITM_MSG_DEVICEINFO_UPDATE,
|
||||
(unsigned char *)device_info_update, strlen(device_info_update));
|
||||
LOG_TRACE("Device Info Update Message ID: %d", res);
|
||||
}
|
||||
|
||||
void user_post_property_after_connected(void);
|
||||
|
||||
static int user_connected_event_handler(void)
|
||||
{
|
||||
user_example_ctx_t *user_example_ctx = user_example_get_ctx();
|
||||
|
||||
LOG_TRACE("Cloud Connected");
|
||||
|
||||
user_example_ctx->cloud_connected = 1;
|
||||
|
||||
set_net_state(CONNECT_CLOUD_SUCCESS);
|
||||
user_post_property_after_connected();
|
||||
|
||||
user_deviceinfo_update();
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_disconnected_event_handler(void)
|
||||
{
|
||||
user_example_ctx_t *user_example_ctx = user_example_get_ctx();
|
||||
|
||||
LOG_TRACE("Cloud Disconnected");
|
||||
|
||||
set_net_state(CONNECT_CLOUD_FAILED);
|
||||
user_example_ctx->cloud_connected = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#define MAX_USER 5
|
||||
|
||||
struct Seq_Info {
|
||||
char userId[8];
|
||||
uint64_t timeStampe;
|
||||
int isValid;
|
||||
};
|
||||
|
||||
static struct Seq_Info seqence_info[MAX_USER];
|
||||
|
||||
static int property_seq_handle(const char *seqenceId, recv_msg_t * msg)
|
||||
{
|
||||
char userId[8], timeStr[16], *ptr;
|
||||
int isDiscard = 1, i;
|
||||
static int repalce_index = 0;
|
||||
|
||||
if (strlen(seqenceId) > sizeof(msg->seq)) {
|
||||
LOG_TRACE("seq to long !!!");
|
||||
return -1;
|
||||
}
|
||||
if (NULL != (ptr = strchr(seqenceId, '@'))) {
|
||||
int len = ptr - seqenceId;
|
||||
|
||||
if (len > (sizeof(userId) -1))
|
||||
len = sizeof(userId) -1;
|
||||
memset(userId, 0, sizeof(userId));
|
||||
memcpy(userId, seqenceId, len);
|
||||
|
||||
len = strlen(seqenceId) - strlen(userId) - 1;
|
||||
if (len > (sizeof(timeStr) -1))
|
||||
len = sizeof(timeStr) -1;
|
||||
memset(timeStr, 0, sizeof(timeStr));
|
||||
memcpy(timeStr, ptr + 1, len);
|
||||
|
||||
uint64_t time = atoll(timeStr);
|
||||
for (i = 0; i < MAX_USER; i++) {
|
||||
if (!strcmp(userId, seqence_info[i].userId)) {
|
||||
if (time >= seqence_info[i].timeStampe) {
|
||||
isDiscard = 0;
|
||||
seqence_info[i].timeStampe = time;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i == MAX_USER) {
|
||||
for (i = 0; i < MAX_USER; i++) {
|
||||
if (!seqence_info[i].isValid) {
|
||||
strcpy(seqence_info[i].userId, userId);
|
||||
seqence_info[i].timeStampe = time;
|
||||
isDiscard = 0;
|
||||
seqence_info[i].isValid = 1;
|
||||
LOG_TRACE("new user %s", userId);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i == MAX_USER) {
|
||||
strcpy(seqence_info[repalce_index].userId, userId);
|
||||
seqence_info[repalce_index].timeStampe = time;
|
||||
isDiscard = 0;
|
||||
seqence_info[repalce_index].isValid = 1;
|
||||
repalce_index = (++repalce_index) % MAX_USER;
|
||||
LOG_TRACE("replace new user %s index %d", userId, repalce_index);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isDiscard == 1) {
|
||||
LOG_TRACE("Discard msg !!!");
|
||||
return -1;
|
||||
}
|
||||
if (NULL != seqenceId)
|
||||
strncpy(msg->seq, seqenceId, sizeof(msg->seq) - 1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int property_setting_handle(const char *request, const int request_len, recv_msg_t * msg)
|
||||
{
|
||||
cJSON *root = NULL, *item = NULL;
|
||||
int ret = -1;
|
||||
|
||||
if ((root = cJSON_Parse(request)) == NULL) {
|
||||
LOG_TRACE("property set payload is not JSON format");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if ((item = cJSON_GetObjectItem(root, "setPropsExtends")) != NULL && cJSON_IsObject(item)) {
|
||||
int isDiscard = 0;
|
||||
cJSON *seq = NULL, *flag = NULL;
|
||||
|
||||
if ((seq = cJSON_GetObjectItem(item, "seq")) != NULL && cJSON_IsString(seq)) {
|
||||
if (property_seq_handle(seq->valuestring, msg)) {
|
||||
isDiscard = 1;
|
||||
}
|
||||
}
|
||||
if (isDiscard == 0) {
|
||||
if ((flag = cJSON_GetObjectItem(item, "flag")) != NULL && cJSON_IsNumber(flag)) {
|
||||
msg->flag = flag->valueint;
|
||||
} else {
|
||||
msg->flag = 0;
|
||||
}
|
||||
} else {
|
||||
cJSON_Delete(root);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if ((item = cJSON_GetObjectItem(root, "PowerSwitch")) != NULL && cJSON_IsNumber(item)) {
|
||||
msg->powerswitch = item->valueint;
|
||||
ret = 0;
|
||||
}
|
||||
#ifdef AOS_TIMER_SERVICE
|
||||
else if (((item = cJSON_GetObjectItem(root, "LocalTimer")) != NULL && cJSON_IsArray(item)) || \
|
||||
((item = cJSON_GetObjectItem(root, "CountDownList")) != NULL && cJSON_IsObject(item)) || \
|
||||
((item = cJSON_GetObjectItem(root, "PeriodTimer")) != NULL && cJSON_IsObject(item)) || \
|
||||
((item = cJSON_GetObjectItem(root, "RandomTimer")) != NULL && cJSON_IsObject(item)))
|
||||
{
|
||||
// Before LocalTimer Handle, Free Memory
|
||||
cJSON_Delete(root);
|
||||
timer_service_property_set(request);
|
||||
user_example_ctx_t *user_example_ctx = user_example_get_ctx();
|
||||
IOT_Linkkit_Report(user_example_ctx->master_devid, ITM_MSG_POST_PROPERTY,
|
||||
(unsigned char *)request, request_len);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
else {
|
||||
LOG_TRACE("property set payload is not JSON format");
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
cJSON_Delete(root);
|
||||
if (ret != -1)
|
||||
send_msg_to_queue(msg);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int user_service_request_event_handler(const int devid, const char *serviceid, const int serviceid_len,
|
||||
const char *request, const int request_len,
|
||||
char **response, int *response_len)
|
||||
{
|
||||
cJSON *root = NULL;
|
||||
LOG_TRACE("Service Request Received, Devid: %d, Service ID: %.*s, Payload: %s", devid, serviceid_len, serviceid,
|
||||
request);
|
||||
|
||||
root = cJSON_Parse(request);
|
||||
if (root == NULL || !cJSON_IsObject(root)) {
|
||||
LOG_TRACE("JSON Parse Error");
|
||||
return -1;
|
||||
}
|
||||
if (strlen("CommonService") == serviceid_len && memcmp("CommonService", serviceid, serviceid_len) == 0) {
|
||||
cJSON *item;
|
||||
const char *response_fmt = "{}";
|
||||
recv_msg_t msg;
|
||||
int isDiscard = 0;
|
||||
|
||||
strcpy(msg.seq, SPEC_SEQ);
|
||||
if ((item = cJSON_GetObjectItem(root, "seq")) != NULL && cJSON_IsString(item)) {
|
||||
if (property_seq_handle(item->valuestring, &msg)) {
|
||||
isDiscard = 1;
|
||||
}
|
||||
}
|
||||
if (isDiscard == 0) {
|
||||
if ((item = cJSON_GetObjectItem(root, "flag")) != NULL && cJSON_IsNumber(item)) {
|
||||
msg.flag = item->valueint;
|
||||
} else {
|
||||
msg.flag = 0;
|
||||
}
|
||||
|
||||
if ((item = cJSON_GetObjectItem(root, "method")) != NULL && cJSON_IsNumber(item)) {
|
||||
msg.method = item->valueint;
|
||||
} else {
|
||||
msg.method = 0;
|
||||
}
|
||||
|
||||
if ((item = cJSON_GetObjectItem(root, "params")) != NULL && cJSON_IsString(item)) {
|
||||
if (msg.method == 0) {
|
||||
msg.from = FROM_SERVICE_SET;
|
||||
property_setting_handle(item->valuestring, strlen(item->valuestring), &msg);
|
||||
} else
|
||||
LOG_TRACE("todo!!");
|
||||
}
|
||||
}
|
||||
*response = HAL_Malloc(RESPONE_BUFFER_SIZE);
|
||||
if (*response == NULL) {
|
||||
LOG_TRACE("Memory Not Enough");
|
||||
cJSON_Delete(root);
|
||||
return -1;
|
||||
}
|
||||
memset(*response, 0, RESPONE_BUFFER_SIZE);
|
||||
memcpy(*response, response_fmt, strlen(response_fmt));
|
||||
*response_len = strlen(*response);
|
||||
}
|
||||
cJSON_Delete(root);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_property_set_event_handler(const int devid, const char *request, const int request_len)
|
||||
{
|
||||
int ret = 0;
|
||||
recv_msg_t msg;
|
||||
|
||||
LOG_TRACE("property set, Devid: %d, payload: \"%s\"", devid, request);
|
||||
msg.from = FROM_PROPERTY_SET;
|
||||
strcpy(msg.seq, SPEC_SEQ);
|
||||
property_setting_handle(request, request_len, &msg);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifdef ALCS_ENABLED
|
||||
static int user_property_get_event_handler(const int devid, const char *request, const int request_len, char **response,
|
||||
int *response_len)
|
||||
{
|
||||
user_example_ctx_t *user_example_ctx = user_example_get_ctx();
|
||||
device_status_t *device_status = &user_example_ctx->status;
|
||||
cJSON *request_root = NULL, *item_propertyid = NULL;
|
||||
cJSON *response_root = NULL;
|
||||
|
||||
LOG_TRACE("Property Get Received, Devid: %d, Request: %s", devid, request);
|
||||
request_root = cJSON_Parse(request);
|
||||
if (request_root == NULL || !cJSON_IsArray(request_root)) {
|
||||
LOG_TRACE("JSON Parse Error");
|
||||
return -1;
|
||||
}
|
||||
|
||||
response_root = cJSON_CreateObject();
|
||||
if (response_root == NULL) {
|
||||
LOG_TRACE("No Enough Memory");
|
||||
cJSON_Delete(request_root);
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (int index = 0; index < cJSON_GetArraySize(request_root); index++) {
|
||||
item_propertyid = cJSON_GetArrayItem(request_root, index);
|
||||
if (item_propertyid == NULL || !cJSON_IsString(item_propertyid)) {
|
||||
LOG_TRACE("JSON Parse Error");
|
||||
cJSON_Delete(request_root);
|
||||
cJSON_Delete(response_root);
|
||||
return -1;
|
||||
}
|
||||
LOG_TRACE("Property ID, index: %d, Value: %s", index, item_propertyid->valuestring);
|
||||
if (strcmp("PowerSwitch", item_propertyid->valuestring) == 0) {
|
||||
cJSON_AddNumberToObject(response_root, "PowerSwitch", device_status->powerswitch);
|
||||
#ifdef AOS_TIMER_SERVICE
|
||||
} else if (strcmp("LocalTimer", item_propertyid->valuestring) == 0) {
|
||||
char *local_timer_str = NULL;
|
||||
|
||||
if (NULL != (local_timer_str = timer_service_property_get("[\"LocalTimer\"]"))) {
|
||||
LOG_TRACE("local_timer %s", local_timer_str);
|
||||
cJSON *property = NULL, *value = NULL;
|
||||
|
||||
property = cJSON_Parse(local_timer_str);
|
||||
if (property == NULL) {
|
||||
LOG_TRACE("No Enough Memory");
|
||||
continue;
|
||||
}
|
||||
value = cJSON_GetObjectItem(property, "LocalTimer");
|
||||
if (value == NULL) {
|
||||
LOG_TRACE("No Enough Memory");
|
||||
cJSON_Delete(property);
|
||||
continue;
|
||||
}
|
||||
cJSON *dup_value = cJSON_Duplicate(value, 1);
|
||||
|
||||
cJSON_AddItemToObject(response_root, "LocalTimer", dup_value);
|
||||
cJSON_Delete(property);
|
||||
example_free(local_timer_str);
|
||||
} else {
|
||||
cJSON *array = cJSON_CreateArray();
|
||||
cJSON_AddItemToObject(response_root, "LocalTimer", array);
|
||||
}
|
||||
} else if (strcmp("CountDownList", item_propertyid->valuestring) == 0) {
|
||||
char *count_down_list_str = NULL;
|
||||
|
||||
if (NULL != (count_down_list_str = timer_service_property_get("[\"CountDownList\"]"))) {
|
||||
LOG_TRACE("CountDownList %s", count_down_list_str);
|
||||
cJSON *property = NULL, *value = NULL;
|
||||
|
||||
property = cJSON_Parse(count_down_list_str);
|
||||
if (property == NULL) {
|
||||
LOG_TRACE("No Enough Memory");
|
||||
continue;
|
||||
}
|
||||
value = cJSON_GetObjectItem(property, "CountDownList");
|
||||
if (value == NULL) {
|
||||
LOG_TRACE("No Enough Memory");
|
||||
cJSON_Delete(property);
|
||||
continue;
|
||||
}
|
||||
cJSON *dup_value = cJSON_Duplicate(value, 1);
|
||||
|
||||
cJSON_AddItemToObject(response_root, "CountDownList", dup_value);
|
||||
cJSON_Delete(property);
|
||||
example_free(count_down_list_str);
|
||||
} else {
|
||||
cJSON_AddStringToObject(response_root, "CountDownList", "");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
cJSON_Delete(request_root);
|
||||
|
||||
*response = cJSON_PrintUnformatted(response_root);
|
||||
if (*response == NULL) {
|
||||
LOG_TRACE("cJSON_PrintUnformatted Error");
|
||||
cJSON_Delete(response_root);
|
||||
return -1;
|
||||
}
|
||||
cJSON_Delete(response_root);
|
||||
*response_len = strlen(*response);
|
||||
|
||||
LOG_TRACE("Property Get Response: %s", *response);
|
||||
return SUCCESS_RETURN;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int user_report_reply_event_handler(const int devid, const int msgid, const int code, const char *reply,
|
||||
const int reply_len)
|
||||
{
|
||||
const char *reply_value = (reply == NULL) ? ("NULL") : (reply);
|
||||
const int reply_value_len = (reply_len == 0) ? (strlen("NULL")) : (reply_len);
|
||||
|
||||
//LOG_TRACE("Message Post Reply Received, Devid: %d, Message ID: %d, Code: %d, Reply: %.*s", devid, msgid, code,
|
||||
// reply_value_len, reply_value);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_trigger_event_reply_event_handler(const int devid, const int msgid, const int code, const char *eventid,
|
||||
const int eventid_len, const char *message, const int message_len)
|
||||
{
|
||||
LOG_TRACE("Trigger Event Reply Received, Devid: %d, Message ID: %d, Code: %d, EventID: %.*s, Message: %.*s", devid,
|
||||
msgid, code, eventid_len, eventid, message_len, message);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef AOS_TIMER_SERVICE
|
||||
static void timer_service_cb(const char *report_data, const char *property_name, int i_value,
|
||||
double d_value, const char *s_value, int prop_idx)
|
||||
{
|
||||
if (prop_idx >= NUM_OF_CONTROL_TARGETS) {
|
||||
LOG_TRACE("ERROR: prop_idx=%d is out of limit=%d", prop_idx, NUM_OF_CONTROL_TARGETS);
|
||||
}
|
||||
if (report_data != NULL) /* post property to cloud */
|
||||
user_post_property_json(report_data);
|
||||
if (property_name != NULL) { /* set value to device */
|
||||
LOG_TRACE("timer event callback: property_name=%s prop_idx=%d", property_name, prop_idx);
|
||||
if (prop_idx < num_of_tsl_type[0] && strcmp(control_targets_list[0], property_name) == 0) {
|
||||
recv_msg_t msg;
|
||||
|
||||
msg.powerswitch = i_value;
|
||||
msg.flag = 0x00;
|
||||
strcpy(msg.seq, SPEC_SEQ);
|
||||
send_msg_to_queue(&msg);
|
||||
LOG_TRACE("timer event callback: int_value=%d", i_value);
|
||||
/* set int value */
|
||||
} else if (prop_idx < num_of_tsl_type[0] + num_of_tsl_type[1]) {
|
||||
LOG_TRACE("timer event callback: double_value=%f", d_value);
|
||||
/* set doube value */
|
||||
} else {
|
||||
if (s_value != NULL)
|
||||
LOG_TRACE("timer event callback: test_value=%s", s_value);
|
||||
/* set string value */
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
static int user_initialized(const int devid)
|
||||
{
|
||||
user_example_ctx_t *user_example_ctx = user_example_get_ctx();
|
||||
LOG_TRACE("Device Initialized, Devid: %d", devid);
|
||||
|
||||
if (user_example_ctx->master_devid == devid) {
|
||||
user_example_ctx->master_initialized = 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** type:
|
||||
*
|
||||
* 0 - new firmware exist
|
||||
*
|
||||
*/
|
||||
// static int user_fota_event_handler(int type, const char *version)
|
||||
// {
|
||||
// char buffer[128] = {0};
|
||||
// int buffer_length = 128;
|
||||
// user_example_ctx_t *user_example_ctx = user_example_get_ctx();
|
||||
|
||||
// if (type == 0) {
|
||||
// LOG_TRACE("New Firmware Version: %s", version);
|
||||
|
||||
// IOT_Linkkit_Query(user_example_ctx->master_devid, ITM_MSG_QUERY_FOTA_DATA, (unsigned char *)buffer, buffer_length);
|
||||
// }
|
||||
|
||||
// return 0;
|
||||
// }
|
||||
|
||||
static uint64_t user_update_sec(void)
|
||||
{
|
||||
static uint64_t time_start_ms = 0;
|
||||
|
||||
if (time_start_ms == 0) {
|
||||
time_start_ms = HAL_UptimeMs();
|
||||
}
|
||||
|
||||
return (HAL_UptimeMs() - time_start_ms) / 1000;
|
||||
}
|
||||
|
||||
void user_post_property_json(const char *property)
|
||||
{
|
||||
user_example_ctx_t *user_example_ctx = user_example_get_ctx();
|
||||
int res = IOT_Linkkit_Report(user_example_ctx->master_devid, ITM_MSG_POST_PROPERTY, (unsigned char *)property,
|
||||
strlen(property));
|
||||
|
||||
LOG_TRACE("Property post Response: %s", property);
|
||||
return;
|
||||
}
|
||||
|
||||
static int notify_msg_handle(const char *request, const int request_len)
|
||||
{
|
||||
cJSON *request_root = NULL;
|
||||
cJSON *item = NULL;
|
||||
|
||||
request_root = cJSON_Parse(request);
|
||||
if (request_root == NULL) {
|
||||
LOG_TRACE("JSON Parse Error");
|
||||
return -1;
|
||||
}
|
||||
|
||||
item = cJSON_GetObjectItem(request_root, "identifier");
|
||||
if (item == NULL || !cJSON_IsString(item)) {
|
||||
cJSON_Delete(request_root);
|
||||
return -1;
|
||||
}
|
||||
if (!strcmp(item->valuestring, "awss.BindNotify")) {
|
||||
cJSON *value = cJSON_GetObjectItem(request_root, "value");
|
||||
if (value == NULL || !cJSON_IsObject(value)) {
|
||||
cJSON_Delete(request_root);
|
||||
return -1;
|
||||
}
|
||||
cJSON *op = cJSON_GetObjectItem(value, "Operation");
|
||||
if (op != NULL && cJSON_IsString(op)) {
|
||||
if (!strcmp(op->valuestring, "Bind")) {
|
||||
LOG_TRACE("Device Bind");
|
||||
vendor_device_bind();
|
||||
} else if (!strcmp(op->valuestring, "Unbind")) {
|
||||
LOG_TRACE("Device unBind");
|
||||
vendor_device_unbind();
|
||||
} else if (!strcmp(op->valuestring, "Reset")) {
|
||||
LOG_TRACE("Device reset");
|
||||
vendor_device_reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cJSON_Delete(request_root);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int user_event_notify_handler(const int devid, const char *request, const int request_len)
|
||||
{
|
||||
int res = 0;
|
||||
user_example_ctx_t *user_example_ctx = user_example_get_ctx();
|
||||
LOG_TRACE("Event notify Received, Devid: %d, Request: %s", devid, request);
|
||||
|
||||
notify_msg_handle(request, request_len);
|
||||
|
||||
res = IOT_Linkkit_Report(user_example_ctx->master_devid, ITM_MSG_EVENT_NOTIFY_REPLY,
|
||||
(unsigned char *)request, request_len);
|
||||
LOG_TRACE("Post Property Message ID: %d", res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if defined (CLOUD_OFFLINE_RESET)
|
||||
static int user_offline_reset_handler(void)
|
||||
{
|
||||
LOG_TRACE("callback user_offline_reset_handler called.");
|
||||
vendor_device_unbind();
|
||||
}
|
||||
#endif
|
||||
|
||||
void user_post_powerstate(int powerstate)
|
||||
{
|
||||
user_example_ctx_t *user_example_ctx = user_example_get_ctx();
|
||||
device_status_t *device_status = &user_example_ctx->status;
|
||||
|
||||
device_status->powerswitch = powerstate;
|
||||
report_device_property(NULL, 0);
|
||||
}
|
||||
|
||||
void user_post_property_after_connected(void)
|
||||
{
|
||||
int res = 0;
|
||||
user_example_ctx_t *user_example_ctx = user_example_get_ctx();
|
||||
device_status_t *device_status = &user_example_ctx->status;
|
||||
char property_payload[128];
|
||||
|
||||
snprintf(property_payload, sizeof(property_payload), "{\"%s\":%d}", "PowerSwitch", device_status->powerswitch);
|
||||
res = IOT_Linkkit_Report(user_example_ctx->master_devid, ITM_MSG_POST_PROPERTY,
|
||||
property_payload, strlen(property_payload));
|
||||
|
||||
LOG_TRACE("Post Event Message ID: %d payload %s", res, property_payload);
|
||||
}
|
||||
|
||||
static int user_master_dev_available(void)
|
||||
{
|
||||
user_example_ctx_t *user_example_ctx = user_example_get_ctx();
|
||||
|
||||
if (user_example_ctx->cloud_connected && user_example_ctx->master_initialized) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int max_running_seconds = 0;
|
||||
int linkkit_main(void *paras)
|
||||
{
|
||||
uint64_t time_prev_sec = 0, time_now_sec = 0;
|
||||
uint64_t time_begin_sec = 0;
|
||||
int res = 0;
|
||||
iotx_linkkit_dev_meta_info_t master_meta_info;
|
||||
user_example_ctx_t *user_example_ctx = user_example_get_ctx();
|
||||
device_status_t *device_status = &user_example_ctx->status;
|
||||
#if defined(__UBUNTU_SDK_DEMO__)
|
||||
int argc = ((app_main_paras_t *) paras)->argc;
|
||||
char **argv = ((app_main_paras_t *) paras)->argv;
|
||||
|
||||
if (argc > 1) {
|
||||
int tmp = atoi(argv[1]);
|
||||
|
||||
if (tmp >= 60) {
|
||||
max_running_seconds = tmp;
|
||||
LOG_TRACE("set [max_running_seconds] = %d seconds\n", max_running_seconds);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !defined(WIFI_PROVISION_ENABLED) || !defined(BUILD_AOS)
|
||||
set_device_meta_info();
|
||||
#endif
|
||||
|
||||
memset(user_example_ctx, 0, sizeof(user_example_ctx_t));
|
||||
|
||||
device_status->powerswitch = (int)product_get_switch();
|
||||
|
||||
/* Register Callback */
|
||||
IOT_RegisterCallback(ITE_CONNECT_SUCC, user_connected_event_handler);
|
||||
IOT_RegisterCallback(ITE_DISCONNECTED, user_disconnected_event_handler);
|
||||
// IOT_RegisterCallback(ITE_RAWDATA_ARRIVED, user_down_raw_data_arrived_event_handler);
|
||||
IOT_RegisterCallback(ITE_SERVICE_REQUEST, user_service_request_event_handler);
|
||||
IOT_RegisterCallback(ITE_PROPERTY_SET, user_property_set_event_handler);
|
||||
#ifdef ALCS_ENABLED
|
||||
/*Only for local communication service(ALCS) */
|
||||
IOT_RegisterCallback(ITE_PROPERTY_GET, user_property_get_event_handler);
|
||||
#endif
|
||||
IOT_RegisterCallback(ITE_REPORT_REPLY, user_report_reply_event_handler);
|
||||
IOT_RegisterCallback(ITE_TRIGGER_EVENT_REPLY, user_trigger_event_reply_event_handler);
|
||||
// IOT_RegisterCallback(ITE_TIMESTAMP_REPLY, user_timestamp_reply_event_handler);
|
||||
IOT_RegisterCallback(ITE_INITIALIZE_COMPLETED, user_initialized);
|
||||
// IOT_RegisterCallback(ITE_FOTA, user_fota_event_handler);
|
||||
IOT_RegisterCallback(ITE_EVENT_NOTIFY, user_event_notify_handler);
|
||||
#if defined (CLOUD_OFFLINE_RESET)
|
||||
IOT_RegisterCallback(ITE_OFFLINE_RESET, user_offline_reset_handler);
|
||||
#endif
|
||||
|
||||
memset(&master_meta_info, 0, sizeof(iotx_linkkit_dev_meta_info_t));
|
||||
HAL_GetProductKey(master_meta_info.product_key);
|
||||
HAL_GetDeviceName(master_meta_info.device_name);
|
||||
HAL_GetDeviceSecret(master_meta_info.device_secret);
|
||||
HAL_GetProductSecret(master_meta_info.product_secret);
|
||||
|
||||
if ((0 == strlen(master_meta_info.product_key)) || (0 == strlen(master_meta_info.device_name))
|
||||
|| (0 == strlen(master_meta_info.device_secret)) || (0 == strlen(master_meta_info.product_secret))) {
|
||||
LOG_TRACE("No device meta info found...\n");
|
||||
while (1) {
|
||||
aos_msleep(USER_EXAMPLE_YIELD_TIMEOUT_MS);
|
||||
}
|
||||
}
|
||||
|
||||
/* Choose Login Method */
|
||||
int dynamic_register = 0;
|
||||
IOT_Ioctl(IOTX_IOCTL_SET_DYNAMIC_REGISTER, (void *)&dynamic_register);
|
||||
|
||||
/* Choose Whether You Need Post Property/Event Reply */
|
||||
int post_event_reply = 1;
|
||||
IOT_Ioctl(IOTX_IOCTL_RECV_EVENT_REPLY, (void *)&post_event_reply);
|
||||
|
||||
/* Create Master Device Resources */
|
||||
do {
|
||||
user_example_ctx->master_devid = IOT_Linkkit_Open(IOTX_LINKKIT_DEV_TYPE_MASTER, &master_meta_info);
|
||||
if (user_example_ctx->master_devid < 0) {
|
||||
LOG_TRACE("IOT_Linkkit_Open Failed, retry after 5s...\n");
|
||||
HAL_SleepMs(5000);
|
||||
}
|
||||
} while (user_example_ctx->master_devid < 0);
|
||||
/* Start Connect Aliyun Server */
|
||||
do {
|
||||
res = IOT_Linkkit_Connect(user_example_ctx->master_devid);
|
||||
if (res < 0) {
|
||||
LOG_TRACE("IOT_Linkkit_Connect Failed, retry after 5s...\n");
|
||||
HAL_SleepMs(5000);
|
||||
}
|
||||
} while (res < 0);
|
||||
|
||||
#ifdef AOS_TIMER_SERVICE
|
||||
static bool ntp_update = false;
|
||||
int ret = timer_service_init(control_targets_list, NUM_OF_PROPERTYS,
|
||||
countdownlist_target_list, NUM_OF_COUNTDOWN_LIST_TARGET,
|
||||
localtimer_target_list, NUM_OF_LOCAL_TIMER_TARGET,
|
||||
timer_service_cb, num_of_tsl_type, NULL);
|
||||
if (ret == 0)
|
||||
ntp_update = true;
|
||||
#endif
|
||||
time_begin_sec = user_update_sec();
|
||||
ntp_server_init();
|
||||
|
||||
while (1) {
|
||||
IOT_Linkkit_Yield(USER_EXAMPLE_YIELD_TIMEOUT_MS);
|
||||
|
||||
#ifndef REPORT_MULTHREAD
|
||||
process_property_report();
|
||||
#endif
|
||||
time_now_sec = user_update_sec();
|
||||
if (time_prev_sec == time_now_sec) {
|
||||
continue;
|
||||
}
|
||||
if (max_running_seconds && (time_now_sec - time_begin_sec > max_running_seconds)) {
|
||||
LOG_TRACE("Example Run for Over %d Seconds, Break Loop!\n", max_running_seconds);
|
||||
break;
|
||||
}
|
||||
|
||||
/* init timer service */
|
||||
if (time_now_sec % 11 == 0 && user_master_dev_available()) {
|
||||
#ifdef AOS_TIMER_SERVICE
|
||||
if (!ntp_update) {
|
||||
int ret = timer_service_init(control_targets_list, NUM_OF_PROPERTYS,
|
||||
countdownlist_target_list, NUM_OF_COUNTDOWN_LIST_TARGET,
|
||||
localtimer_target_list, NUM_OF_LOCAL_TIMER_TARGET,
|
||||
timer_service_cb, num_of_tsl_type, NULL);
|
||||
if (ret == 0)
|
||||
ntp_update = true;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
time_prev_sec = time_now_sec;
|
||||
}
|
||||
|
||||
IOT_Linkkit_Close(user_example_ctx->master_devid);
|
||||
|
||||
IOT_DumpMemoryStats(IOT_LOG_DEBUG);
|
||||
IOT_SetLogLevel(IOT_LOG_NONE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
367
Products/example/smart_outlet/vendor.c
Executable file
367
Products/example/smart_outlet/vendor.c
Executable file
|
|
@ -0,0 +1,367 @@
|
|||
/*
|
||||
*copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <aos/aos.h>
|
||||
#include <aos/yloop.h>
|
||||
#include "netmgr.h"
|
||||
#include "iot_export.h"
|
||||
#include "iot_import.h"
|
||||
#include "app_entry.h"
|
||||
#include "aos/kv.h"
|
||||
#include <hal/soc/gpio.h>
|
||||
#include "vendor.h"
|
||||
#include "device_state_manger.h"
|
||||
#include "iot_import_awss.h"
|
||||
#include "smart_outlet.h"
|
||||
#if defined(HF_LPT230) || defined(HF_LPT130)
|
||||
#include "hfilop/hfilop.h"
|
||||
#endif
|
||||
|
||||
#if defined(HF_LPT130) /* config for PRODUCT: zuowei outlet */
|
||||
#define LED_GPIO 22
|
||||
#define RELAY_GPIO 5
|
||||
#define KEY_GPIO 4
|
||||
#elif defined(HF_LPT230) || defined(UNO_91H) /* on hf-lpt230 EVB */
|
||||
#define LED_GPIO 8
|
||||
#define RELAY_GPIO 24
|
||||
#define KEY_GPIO 25
|
||||
#elif defined(MK3080) /* on mk3080 EVB */
|
||||
#define LED_GPIO 9
|
||||
#define RELAY_GPIO 7
|
||||
#define KEY_GPIO 12
|
||||
#elif defined(MX1270) /* on mx1270 EVB */
|
||||
#define LED_GPIO 11
|
||||
#define RELAY_GPIO 6
|
||||
#define KEY_GPIO 8
|
||||
#elif defined(BK7231UDEVKITC)
|
||||
#define LED_GPIO 15
|
||||
#define RELAY_GPIO 17
|
||||
#define KEY_GPIO 28
|
||||
#elif defined(AMEBAZ_DEV) /* on amebaz_dev */
|
||||
#define LED_GPIO 9
|
||||
#define RELAY_GPIO 7
|
||||
#define KEY_GPIO 12
|
||||
#else /* default config */
|
||||
#define LED_GPIO 22
|
||||
#define RELAY_GPIO 5
|
||||
#define KEY_GPIO 4
|
||||
#endif
|
||||
|
||||
static gpio_dev_t io_led;
|
||||
static gpio_dev_t io_key;
|
||||
static gpio_dev_t io_relay;
|
||||
|
||||
static int led_state = -1;
|
||||
void product_set_led(bool state)
|
||||
{
|
||||
if (led_state == (int)state) {
|
||||
return;
|
||||
}
|
||||
|
||||
#if defined(HF_LPT130) || defined(UNO_91H)
|
||||
if (state) {
|
||||
hal_gpio_output_high(&io_led);
|
||||
} else {
|
||||
hal_gpio_output_low(&io_led);
|
||||
}
|
||||
#else
|
||||
if (state) {
|
||||
hal_gpio_output_low(&io_led);
|
||||
} else {
|
||||
hal_gpio_output_high(&io_led);
|
||||
}
|
||||
#endif
|
||||
led_state = (int)state;
|
||||
}
|
||||
|
||||
static bool product_get_led()
|
||||
{
|
||||
return (bool)led_state;
|
||||
}
|
||||
|
||||
void product_toggle_led()
|
||||
{
|
||||
if (product_get_led() == ON) {
|
||||
product_set_led(OFF);
|
||||
} else {
|
||||
product_set_led(ON);
|
||||
}
|
||||
}
|
||||
|
||||
static int switch_state = -1;
|
||||
void product_init_switch(void)
|
||||
{
|
||||
int state = ON;
|
||||
int len = sizeof(int);
|
||||
int ret = 0;
|
||||
|
||||
#if (REBOOT_STATE == LAST_STATE)
|
||||
ret = aos_kv_get(KV_KEY_SWITCH_STATE, (void *)&state, &len);
|
||||
LOG("get kv save switch state:%d, ret:%d", state, ret);
|
||||
if (ret != 0)
|
||||
state = ON;
|
||||
#elif (REBOOT_STATE == POWER_OFF)
|
||||
state == OFF;
|
||||
#else
|
||||
state == ON;
|
||||
#endif
|
||||
if (state == OFF) {
|
||||
product_set_switch(OFF);
|
||||
} else {
|
||||
product_set_switch(ON);
|
||||
}
|
||||
}
|
||||
|
||||
#if (REBOOT_STATE == LAST_STATE)
|
||||
aos_timer_t g_timer_period_save_device_status;
|
||||
#define PERIOD_SAVE_DEVICE_STATUS_INTERVAL (1000 * 30)
|
||||
static void timer_period_save_device_status_cb(void *arg1, void *arg2)
|
||||
{
|
||||
int state, len = sizeof(int);
|
||||
int ret;
|
||||
|
||||
ret = aos_kv_get(KV_KEY_SWITCH_STATE, (void *)&state, &len);
|
||||
|
||||
if(ret != 0 || state != switch_state) {
|
||||
LOG("KV DEVICE_STATUS Update!!!,ret=%d.\n",ret);
|
||||
ret = aos_kv_set(KV_KEY_SWITCH_STATE, &switch_state, len, 0);
|
||||
if (ret < 0) LOG("KV set Error: %d\r\n", __LINE__);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void vendor_product_init(void)
|
||||
{
|
||||
io_led.port = LED_GPIO;
|
||||
io_relay.port = RELAY_GPIO;
|
||||
io_key.port = KEY_GPIO;
|
||||
|
||||
io_led.config = OUTPUT_PUSH_PULL;
|
||||
io_relay.config = OUTPUT_PUSH_PULL;
|
||||
io_key.config = INPUT_PULL_UP;
|
||||
|
||||
hal_gpio_init(&io_led);
|
||||
hal_gpio_init(&io_relay);
|
||||
hal_gpio_init(&io_key);
|
||||
|
||||
product_init_switch();
|
||||
#if (REBOOT_STATE == LAST_STATE)
|
||||
aos_timer_new(&g_timer_period_save_device_status, timer_period_save_device_status_cb, NULL, PERIOD_SAVE_DEVICE_STATUS_INTERVAL, 1);
|
||||
#endif
|
||||
}
|
||||
|
||||
void product_set_switch(bool state)
|
||||
{
|
||||
int ret = 0;
|
||||
|
||||
//LOG("product_set_switch, state:%d, switch_state:%d", state, switch_state);
|
||||
if (switch_state == (int)state) {
|
||||
return;
|
||||
}
|
||||
|
||||
#if defined(HF_LPT130) || defined(UNO_91H)
|
||||
if (state) {
|
||||
hal_gpio_output_high(&io_relay);
|
||||
} else {
|
||||
hal_gpio_output_low(&io_relay);
|
||||
}
|
||||
#else
|
||||
if (state) {
|
||||
hal_gpio_output_low(&io_relay);
|
||||
} else {
|
||||
hal_gpio_output_high(&io_relay);
|
||||
}
|
||||
#endif
|
||||
switch_state = (int)state;
|
||||
product_set_led(state);
|
||||
|
||||
update_power_state(state);
|
||||
}
|
||||
|
||||
bool product_get_switch(void)
|
||||
{
|
||||
return (bool)switch_state;
|
||||
}
|
||||
|
||||
bool product_get_key(void)
|
||||
{
|
||||
uint32_t level;
|
||||
hal_gpio_input_get(&io_key, &level);
|
||||
return level;
|
||||
}
|
||||
|
||||
int vendor_get_product_key(char *product_key, int *len)
|
||||
{
|
||||
char *pk = NULL;
|
||||
int ret = -1;
|
||||
int pk_len = *len;
|
||||
|
||||
ret = aos_kv_get("linkkit_product_key", product_key, &pk_len);
|
||||
#if defined(HF_LPT230) || defined(HF_LPT130)
|
||||
if ((ret != 0)&&((pk = hfilop_layer_get_product_key()) != NULL)) {
|
||||
pk_len = strlen(pk);
|
||||
memcpy(product_key, pk, pk_len);
|
||||
ret = 0;
|
||||
}
|
||||
#else
|
||||
/*
|
||||
todo: get pk...
|
||||
*/
|
||||
#endif
|
||||
if (ret == 0) {
|
||||
*len = pk_len;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int vendor_get_product_secret(char *product_secret, int *len)
|
||||
{
|
||||
char *ps = NULL;
|
||||
int ret = -1;
|
||||
int ps_len = *len;
|
||||
|
||||
ret = aos_kv_get("linkkit_product_secret", product_secret, &ps_len);
|
||||
#if defined(HF_LPT230) || defined(HF_LPT130)
|
||||
if ((ret != 0)&&((ps = hfilop_layer_get_product_secret()) != NULL)) {
|
||||
ps_len = strlen(ps);
|
||||
memcpy(product_secret, ps, ps_len);
|
||||
ret = 0;
|
||||
}
|
||||
#else
|
||||
/*
|
||||
todo: get ps...
|
||||
*/
|
||||
#endif
|
||||
if (ret == 0) {
|
||||
*len = ps_len;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int vendor_get_device_name(char *device_name, int *len)
|
||||
{
|
||||
char *dn = NULL;
|
||||
int ret = -1;
|
||||
int dn_len = *len;
|
||||
|
||||
ret = aos_kv_get("linkkit_device_name", device_name, &dn_len);
|
||||
#if defined(HF_LPT230) || defined(HF_LPT130)
|
||||
if ((ret != 0)&&((dn = hfilop_layer_get_device_name()) != NULL)) {
|
||||
dn_len = strlen(dn);
|
||||
memcpy(device_name, dn, dn_len);
|
||||
ret = 0;
|
||||
}
|
||||
#else
|
||||
/*
|
||||
todo: get dn...
|
||||
*/
|
||||
#endif
|
||||
if (ret == 0) {
|
||||
*len = dn_len;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int vendor_get_device_secret(char *device_secret, int *len)
|
||||
{
|
||||
char *ds = NULL;
|
||||
int ret = -1;
|
||||
int ds_len = *len;
|
||||
|
||||
ret = aos_kv_get("linkkit_device_secret", device_secret, &ds_len);
|
||||
#if defined(HF_LPT230) || defined(HF_LPT130)
|
||||
if ((ret != 0)&&((ds = hfilop_layer_get_device_secret()) != NULL)) {
|
||||
ds_len = strlen(ds);
|
||||
memcpy(device_secret, ds, ds_len);
|
||||
ret = 0;
|
||||
}
|
||||
#else
|
||||
/*
|
||||
todo: get ds...
|
||||
*/
|
||||
#endif
|
||||
if (ret == 0) {
|
||||
*len = ds_len;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int vendor_get_product_id(uint32_t *pid)
|
||||
{
|
||||
int ret = -1;
|
||||
char pidStr[9] = { 0 };
|
||||
int len = sizeof(pidStr);
|
||||
|
||||
ret = aos_kv_get("linkkit_product_id", pidStr, &len);
|
||||
if (ret == 0 && len < sizeof(pidStr)) {
|
||||
*pid = atoi(pidStr);
|
||||
} else {
|
||||
ret = -1;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int set_device_meta_info(void)
|
||||
{
|
||||
int len = 0;
|
||||
char product_key[PRODUCT_KEY_LEN + 1] = {0};
|
||||
char product_secret[PRODUCT_SECRET_LEN + 1] = {0};
|
||||
char device_name[DEVICE_NAME_LEN + 1] = {0};
|
||||
char device_secret[DEVICE_SECRET_LEN + 1] = {0};
|
||||
|
||||
memset(product_key, 0, sizeof(product_key));
|
||||
memset(product_secret, 0, sizeof(product_secret));
|
||||
memset(device_name, 0, sizeof(device_name));
|
||||
memset(device_secret, 0, sizeof(device_secret));
|
||||
|
||||
len = PRODUCT_KEY_LEN + 1;
|
||||
vendor_get_product_key(product_key, &len);
|
||||
|
||||
len = PRODUCT_SECRET_LEN + 1;
|
||||
vendor_get_product_secret(product_secret, &len);
|
||||
|
||||
len = DEVICE_NAME_LEN + 1;
|
||||
vendor_get_device_name(device_name, &len);
|
||||
|
||||
len = DEVICE_SECRET_LEN + 1;
|
||||
vendor_get_device_secret(device_secret, &len);
|
||||
|
||||
if ((strlen(product_key) > 0) && (strlen(product_secret) > 0) \
|
||||
&& (strlen(device_name) > 0) && (strlen(device_secret) > 0)) {
|
||||
HAL_SetProductKey(product_key);
|
||||
HAL_SetProductSecret(product_secret);
|
||||
HAL_SetDeviceName(device_name);
|
||||
HAL_SetDeviceSecret(device_secret);
|
||||
LOG("pk[%s]", product_key);
|
||||
LOG("dn[%s]", device_name);
|
||||
return 0;
|
||||
} else {
|
||||
LOG("no valid device meta data");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
void linkkit_reset(void *p);
|
||||
void vendor_device_bind(void)
|
||||
{
|
||||
set_net_state(APP_BIND_SUCCESS);
|
||||
}
|
||||
|
||||
void vendor_device_unbind(void)
|
||||
{
|
||||
linkkit_reset(NULL);
|
||||
}
|
||||
void vendor_device_reset(void)
|
||||
{
|
||||
/* do factory reset */
|
||||
// clean kv ...
|
||||
// clean buffer ...
|
||||
/* end */
|
||||
do_awss_reset();
|
||||
}
|
||||
82
Products/example/smart_outlet/vendor.h
Executable file
82
Products/example/smart_outlet/vendor.h
Executable file
|
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
#ifndef __VENDOR_H__
|
||||
#define __VENDOR_H__
|
||||
|
||||
#include <aos/aos.h>
|
||||
|
||||
typedef enum {
|
||||
OFF = 0,
|
||||
ON
|
||||
} eSwitchState;
|
||||
|
||||
#define POWER_OFF 0
|
||||
#define POWER_ON 1
|
||||
#define LAST_STATE 2
|
||||
|
||||
#define KV_KEY_SWITCH_STATE "OUTLET_SWITCH_STATE"
|
||||
|
||||
#define REBOOT_STATE LAST_STATE
|
||||
/**
|
||||
* @brief product init.
|
||||
*
|
||||
* @param [in] None.
|
||||
*
|
||||
* @return None.
|
||||
* @see None.
|
||||
*/
|
||||
void vendor_product_init(void);
|
||||
void product_set_switch(bool state);
|
||||
bool product_get_switch(void);
|
||||
void product_toggle_led(void);
|
||||
void product_set_led(bool state);
|
||||
bool product_get_key(void);
|
||||
|
||||
/**
|
||||
* @brief set device meta info.
|
||||
*
|
||||
* @param [in] None.
|
||||
* @param [in] None.
|
||||
*
|
||||
* @return 0:success, -1:failed.
|
||||
* @see None.
|
||||
*/
|
||||
int set_device_meta_info(void);
|
||||
|
||||
/**
|
||||
* @brief vendor operation after receiving device_bind event from Cloud.
|
||||
*
|
||||
* @param [in] None.
|
||||
*
|
||||
* @return None.
|
||||
* @see None.
|
||||
*/
|
||||
void vendor_device_bind(void);
|
||||
|
||||
/**
|
||||
* @brief vendor operation after receiving device_unbind event from Cloud.
|
||||
*
|
||||
* @param [in] None.
|
||||
*
|
||||
* @return None.
|
||||
* @see None.
|
||||
*/
|
||||
void vendor_device_unbind(void);
|
||||
/**
|
||||
* @brief vendor operation after receiving device_reset event from Cloud.
|
||||
*
|
||||
* @param [in] None.
|
||||
*
|
||||
* @return None.
|
||||
* @see None.
|
||||
*/
|
||||
void vendor_device_reset(void);
|
||||
|
||||
int vendor_get_product_key(char *product_key, int *len);
|
||||
int vendor_get_product_secret(char *product_secret, int *len);
|
||||
int vendor_get_device_name(char *device_name, int *len);
|
||||
int vendor_get_device_secret(char *device_secret, int *len);
|
||||
int vendor_get_produtc_id(uint32_t *pid);
|
||||
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue