support aliyun sdk on TencentOS tiny
sample: examples\aliyun_iotkit_csdk_mqtt project: board\TencentOS_tiny_EVB_MX_Plus\KEIL\aliyun_iotkit_csdk_mqtt
This commit is contained in:
77
components/connectivity/iotkit-embedded-3.0.1/3rdparty/src/dev_reset/dev_reset.c
vendored
Normal file
77
components/connectivity/iotkit-embedded-3.0.1/3rdparty/src/dev_reset/dev_reset.c
vendored
Normal file
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
|
||||
*/
|
||||
|
||||
#include "dev_reset_internal.h"
|
||||
|
||||
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
static int g_dev_reset_sub_flag = 0;
|
||||
iotx_devrst_evt_handle_t g_devrst_event_handle = NULL;
|
||||
|
||||
|
||||
#include "mqtt_api.h"
|
||||
static void mqtt_sub_handle(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg)
|
||||
{
|
||||
switch (msg->event_type)
|
||||
{
|
||||
case IOTX_MQTT_EVENT_PUBLISH_RECEIVED: {
|
||||
iotx_mqtt_topic_info_t *packet_info = (iotx_mqtt_topic_info_t *)msg->msg;
|
||||
if (g_devrst_event_handle) {
|
||||
iotx_devrst_evt_recv_msg_t recv_msg;
|
||||
memset(&recv_msg, 0, sizeof(iotx_devrst_evt_recv_msg_t));
|
||||
recv_msg.msgid = packet_info->packet_id;
|
||||
recv_msg.payload = (char *)packet_info->payload;
|
||||
recv_msg.payload_len = packet_info->payload_len;
|
||||
g_devrst_event_handle(IOTX_DEVRST_EVT_RECEIVED, (void *)&recv_msg);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int IOT_DevReset_Report(iotx_dev_meta_info_t *meta_info, iotx_devrst_evt_handle_t handle, void *extended)
|
||||
{
|
||||
int res = 0;
|
||||
const char *reset_fmt = "/sys/%s/%s/thing/reset";
|
||||
const char *reset_reply_fmt = "/sys/%s/%s/thing/reset_reply";
|
||||
const char *payload_fmt = "{\"id\":%d, \"version\":\"1.0\", \"method\":\"thing.reset\", \"params\":{}}";
|
||||
char topic[IOTX_PRODUCT_KEY_LEN + IOTX_DEVICE_NAME_LEN + 30] = {0};
|
||||
char payload[128] = {0};
|
||||
|
||||
if (meta_info == NULL || handle== NULL ) {
|
||||
return FAIL_RETURN;
|
||||
}
|
||||
g_devrst_event_handle = handle;
|
||||
|
||||
memset(topic, 0, IOTX_PRODUCT_KEY_LEN + IOTX_DEVICE_NAME_LEN + 30);
|
||||
HAL_Snprintf(topic,IOTX_PRODUCT_KEY_LEN + IOTX_DEVICE_NAME_LEN + 30, reset_reply_fmt, meta_info->product_key, meta_info->device_name);
|
||||
|
||||
if (g_dev_reset_sub_flag == 0) {
|
||||
res = IOT_MQTT_Subscribe_Sync(NULL, topic, IOTX_MQTT_QOS0, mqtt_sub_handle, NULL, 5000);
|
||||
if (res < 0 ) {
|
||||
return FAIL_RETURN;
|
||||
}
|
||||
g_dev_reset_sub_flag = 1;
|
||||
}
|
||||
|
||||
memset(topic, 0, IOTX_PRODUCT_KEY_LEN + IOTX_DEVICE_NAME_LEN + 30);
|
||||
HAL_Snprintf(topic, IOTX_PRODUCT_KEY_LEN + IOTX_DEVICE_NAME_LEN + 30, reset_fmt, meta_info->product_key, meta_info->device_name);
|
||||
|
||||
memset(payload, 0, 128);
|
||||
HAL_Snprintf(payload, 128, payload_fmt, iotx_report_id());
|
||||
|
||||
res = IOT_MQTT_Publish_Simple(NULL, topic, IOTX_MQTT_QOS0, payload, strlen(payload));
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
|
||||
}
|
||||
#endif
|
||||
|
31
components/connectivity/iotkit-embedded-3.0.1/3rdparty/src/dev_reset/dev_reset_api.h
vendored
Normal file
31
components/connectivity/iotkit-embedded-3.0.1/3rdparty/src/dev_reset/dev_reset_api.h
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef __DEV_RESET_API_H__
|
||||
#define __DEV_RESET_API_H__
|
||||
|
||||
#include "infra_types.h"
|
||||
#include "infra_defs.h"
|
||||
|
||||
typedef enum {
|
||||
IOTX_DEVRST_EVT_RECEIVED
|
||||
} iotx_devrst_evt_type_t;
|
||||
|
||||
typedef struct {
|
||||
int msgid;
|
||||
char *payload;
|
||||
uint32_t payload_len;
|
||||
} iotx_devrst_evt_recv_msg_t;
|
||||
|
||||
typedef void (*iotx_devrst_evt_handle_t)(iotx_devrst_evt_type_t evt, void *msg);
|
||||
|
||||
/**
|
||||
* @brief report reset message to cloud.
|
||||
*
|
||||
* @param meta_info. device meta info, only product_key and device_name needed.
|
||||
* @param extended. reserved.
|
||||
*
|
||||
* @retval -1 : failure
|
||||
* @retval 0 : sucess
|
||||
*/
|
||||
int IOT_DevReset_Report(iotx_dev_meta_info_t *meta_info, iotx_devrst_evt_handle_t handle, void *extended);
|
||||
|
||||
#endif
|
||||
|
36
components/connectivity/iotkit-embedded-3.0.1/3rdparty/src/dev_reset/dev_reset_internal.h
vendored
Normal file
36
components/connectivity/iotkit-embedded-3.0.1/3rdparty/src/dev_reset/dev_reset_internal.h
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
#ifndef _DEV_RESET_INTERNAL_H_
|
||||
#define _DEV_RESET_INTERNAL_H_
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "infra_config.h"
|
||||
#include "infra_types.h"
|
||||
#include "infra_defs.h"
|
||||
#include "infra_report.h"
|
||||
#include "dev_reset_internal.h"
|
||||
#include "dev_reset_wrapper.h"
|
||||
#include "dev_reset_api.h"
|
||||
#include "mqtt_api.h"
|
||||
|
||||
#ifdef INFRA_LOG
|
||||
#include "infra_log.h"
|
||||
#define devrst_err(...) log_err("devrst", __VA_ARGS__)
|
||||
#define devrst_info(...) log_info("devrst", __VA_ARGS__)
|
||||
#define devrst_debug(...) log_debug("devrst", __VA_ARGS__)
|
||||
#else
|
||||
#define devrst_info(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
|
||||
#define devrst_err(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
|
||||
#define devrst_debug(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
|
||||
#endif
|
||||
|
||||
#ifdef INFRA_MEM_STATS
|
||||
#include "infra_mem_stats.h"
|
||||
#define devrst_malloc(size) LITE_malloc(size, MEM_MAGIC, "devrst")
|
||||
#define devrst_free(ptr) LITE_free(ptr)
|
||||
#else
|
||||
#define devrst_malloc(size) HAL_Malloc(size)
|
||||
#define devrst_free(ptr) {HAL_Free((void *)ptr);ptr = NULL;}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
7
components/connectivity/iotkit-embedded-3.0.1/3rdparty/src/dev_reset/dev_reset_wrapper.h
vendored
Normal file
7
components/connectivity/iotkit-embedded-3.0.1/3rdparty/src/dev_reset/dev_reset_wrapper.h
vendored
Normal file
@@ -0,0 +1,7 @@
|
||||
#ifndef _DEV_RESET_WRAPPER_H_
|
||||
#define _DEV_RESET_WRAPPER_H_
|
||||
|
||||
int HAL_Snprintf(char *str, const int len, const char *fmt, ...);
|
||||
|
||||
#endif
|
||||
|
@@ -0,0 +1,82 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "mqtt_api.h"
|
||||
#include "dev_reset_api.h"
|
||||
|
||||
void HAL_Printf(const char *fmt, ...);
|
||||
int HAL_GetProductKey(char product_key[IOTX_PRODUCT_KEY_LEN]);
|
||||
int HAL_GetDeviceName(char device_name[IOTX_DEVICE_NAME_LEN]);
|
||||
|
||||
#define EXAMPLE_TRACE(fmt, ...) \
|
||||
do { \
|
||||
HAL_Printf("%s|%03d :: ", __func__, __LINE__); \
|
||||
HAL_Printf(fmt, ##__VA_ARGS__); \
|
||||
HAL_Printf("%s", "\r\n"); \
|
||||
} while(0)
|
||||
|
||||
static int reset_mqtt_packet_id = 0;
|
||||
static int reset_reply_received = 0;
|
||||
|
||||
void example_event_handle(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg)
|
||||
{
|
||||
EXAMPLE_TRACE("msg->event_type : %d", msg->event_type);
|
||||
}
|
||||
|
||||
void example_devrst_evt_handle(iotx_devrst_evt_type_t evt, void *msg)
|
||||
{
|
||||
switch (evt)
|
||||
{
|
||||
case IOTX_DEVRST_EVT_RECEIVED: {
|
||||
iotx_devrst_evt_recv_msg_t *recv_msg = (iotx_devrst_evt_recv_msg_t *)msg;
|
||||
if (recv_msg->msgid != reset_mqtt_packet_id) {
|
||||
return;
|
||||
}
|
||||
EXAMPLE_TRACE("Receive Reset Responst");
|
||||
EXAMPLE_TRACE("Msg ID: %d", recv_msg->msgid);
|
||||
EXAMPLE_TRACE("Payload: %.*s", recv_msg->payload_len, recv_msg->payload);
|
||||
reset_reply_received = 1;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int res = 0;
|
||||
void *pclient = NULL;
|
||||
iotx_dev_meta_info_t meta_info;
|
||||
iotx_mqtt_param_t mqtt_params;
|
||||
|
||||
memset(&mqtt_params, 0, sizeof(iotx_mqtt_param_t));
|
||||
memset(&meta_info, 0, sizeof(iotx_dev_meta_info_t));
|
||||
|
||||
HAL_GetProductKey(meta_info.product_key);
|
||||
HAL_GetDeviceName(meta_info.device_name);
|
||||
|
||||
mqtt_params.handle_event.h_fp = example_event_handle;
|
||||
|
||||
pclient = IOT_MQTT_Construct(&mqtt_params);
|
||||
if (NULL == pclient) {
|
||||
EXAMPLE_TRACE("MQTT construct failed");
|
||||
return -1;
|
||||
}
|
||||
|
||||
res = IOT_DevReset_Report(&meta_info, example_devrst_evt_handle, NULL);
|
||||
if (res < 0) {
|
||||
return -1;
|
||||
}
|
||||
reset_mqtt_packet_id = res;
|
||||
|
||||
while (!reset_reply_received) {
|
||||
|
||||
IOT_MQTT_Yield(pclient, 200);
|
||||
}
|
||||
|
||||
EXAMPLE_TRACE("Example Execute Success, Now Exit...");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
11
components/connectivity/iotkit-embedded-3.0.1/3rdparty/src/dev_reset/iot.mk
vendored
Normal file
11
components/connectivity/iotkit-embedded-3.0.1/3rdparty/src/dev_reset/iot.mk
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
LIBA_TARGET := libiot_reset.a
|
||||
|
||||
HDR_REFS := src/infra src/mqtt
|
||||
|
||||
DEPENDS += wrappers
|
||||
LDFLAGS += -liot_sdk -liot_hal -liot_tls
|
||||
|
||||
LIB_SRCS_EXCLUDE += examples/dev_reset_example.c
|
||||
SRCS_dev-reset-example := examples/dev_reset_example.c
|
||||
|
||||
$(call Append_Conditional, TARGET, dev-reset-example, DEV_RESET, BUILD_AOS NO_EXECUTABLES)
|
Reference in New Issue
Block a user