阿里云调试

This commit is contained in:
Aladdin-Wang 2020-04-07 22:48:54 +08:00
parent 630e6c0a5f
commit 4de6f893a1
4 changed files with 301 additions and 8 deletions

View File

@ -331,8 +331,8 @@ CONFIG_PKG_AT_DEVICE_PATH="/packages/iot/at_device"
CONFIG_AT_DEVICE_USING_ESP8266=y
CONFIG_AT_DEVICE_ESP8266_INIT_ASYN=y
CONFIG_AT_DEVICE_ESP8266_SAMPLE=y
CONFIG_ESP8266_SAMPLE_WIFI_SSID="xgld1"
CONFIG_ESP8266_SAMPLE_WIFI_PASSWORD="xgld64627816"
CONFIG_ESP8266_SAMPLE_WIFI_SSID="ChinaNet-ssssss"
CONFIG_ESP8266_SAMPLE_WIFI_PASSWORD="SQHWLK9394"
CONFIG_ESP8266_SAMPLE_CLIENT_NAME="uart3"
CONFIG_ESP8266_SAMPLE_RECV_BUFF_LEN=512
# CONFIG_AT_DEVICE_USING_RW007 is not set

View File

@ -7,17 +7,310 @@
#include "infra_compat.h"
#include "mqtt_api.h"
#include "cJSON.h"
#include <arpa/inet.h>
#include <netdev.h>
#define LOG_TAG "ali-sdk"
#define LOG_LVL LOG_LVL_INFO
#define LOG_LVL LOG_LVL_DBG
#include <ulog.h>
#define MQTT_MSGLEN CONFIG_MQTT_MESSAGE_MAXLEN
#define MQTT_REQUEST_TIMEOUT CONFIG_MQTT_REQUEST_TIMEOUT
#define MQTT_KEEPALIVE_INTERNAL CONFIG_MQTT_KEEPALIVE_INTERVAL
#define MQTT_TOPIC_MAX_SIZE (128)
#define MQTT_MAN_INFO_STRING "DiyMultiMeter"
/* <20><EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ */
#define ALI_SERVICE_RGB_CTRL_SUB "Topic-ServiceDoorCtrlSub"
#define ALI_SERVICE_RGB_CTRL_REPLY_PUB "Topic-ServiceDoorCtrlReplyPub"
/* <20><EFBFBD><E8B1B8><EFBFBD><EFBFBD><EFBFBD>ϱ<EFBFBD><CFB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ */
#define ALI_PROPERTY_POST_PUB "Topic-PropertyPostPub"
#define ALI_PROPERTY_POST_REPLY_SUB "Topic-PropertyPostReplySub"
static void *mqtt_client_hd = RT_NULL;
static char *topic_buff = RT_NULL;
static uint32_t mqtt_period_cnt = 0;
static uint32_t mqtt_packet_id = 1;
static void ali_mqtt_rgb_ctrl_msg_arrive(void *pcontext, void *handle, iotx_mqtt_event_msg_pt msg);
static void ali_mqtt_property_post_msg_arrive(void *pcontext, void *handle, iotx_mqtt_event_msg_pt msg);
extern int HAL_GetProductKey(char product_key[IOTX_PRODUCT_KEY_LEN + 1]);
extern int HAL_GetProductSecret(char product_secret[IOTX_PRODUCT_SECRET_LEN + 1]);
extern int HAL_GetDeviceName(char device_name[IOTX_DEVICE_NAME_LEN + 1]);
extern int HAL_GetDeviceSecret(char device_secret[IOTX_DEVICE_SECRET_LEN + 1]);
extern int HAL_SetDeviceSecret(char* device_secret);
typedef struct
{
const char *topic_filter;
iotx_mqtt_qos_t qos;
iotx_mqtt_event_handle_func_fpt topic_handle_func;
void *pcontext;
} mqtt_subscribe_item, *mqtt_subscribe_item_t;
static const mqtt_subscribe_item mqtt_sub_item[] =
{
{ALI_SERVICE_RGB_CTRL_SUB, IOTX_MQTT_QOS1, ali_mqtt_rgb_ctrl_msg_arrive, RT_NULL},
{ALI_SERVICE_RGB_CTRL_REPLY_PUB, IOTX_MQTT_QOS1, RT_NULL, RT_NULL},
{ALI_PROPERTY_POST_PUB, IOTX_MQTT_QOS1, RT_NULL, RT_NULL},
{ALI_PROPERTY_POST_REPLY_SUB, IOTX_MQTT_QOS1, ali_mqtt_property_post_msg_arrive, RT_NULL}
};
static char *mqtt_topic_find(const char *topic_fliter)
{
if (topic_buff == RT_NULL || topic_fliter == RT_NULL)
return RT_NULL;
char *topic;
int topic_idx;
int sub_items = sizeof(mqtt_sub_item) / sizeof(mqtt_subscribe_item);
for (topic = RT_NULL, topic_idx = 0; (topic_idx < sub_items) && (topic == RT_NULL); topic_idx++)
{
if (!rt_strcmp(mqtt_sub_item[topic_idx].topic_filter, topic_fliter))
topic = &topic_buff[MQTT_TOPIC_MAX_SIZE * topic_idx];
}
return topic;
}
static char * mqtt_check_load_topic(void)
{
int topic_idx;
int sub_items = sizeof(mqtt_sub_item) / sizeof(mqtt_subscribe_item);
char product_key[IOTX_PRODUCT_KEY_LEN + 1];
if (HAL_GetProductKey(product_key) <= 0)
LOG_D("Get ProductKey failed.");
char device_name[IOTX_DEVICE_NAME_LEN + 1];
if (HAL_GetDeviceName(device_name) <= 0)
LOG_D("Get DeviceName failed.");
char *topic = rt_calloc(sub_items, MQTT_TOPIC_MAX_SIZE);
if (topic == RT_NULL)
{
LOG_D("not enough memory for topic name!");
return RT_NULL;
}
rt_memset(topic, 0x0, MQTT_TOPIC_MAX_SIZE * sub_items);
for (topic_idx = 0; topic_idx < sub_items; topic_idx++)
{
if (!rt_strcmp(mqtt_sub_item[topic_idx].topic_filter, ALI_SERVICE_RGB_CTRL_SUB))
rt_snprintf(&topic[topic_idx * 128], MQTT_TOPIC_MAX_SIZE, "/sys/%s/%s/thing/service/rgb_ctrl", product_key, device_name);
else if (!rt_strcmp(mqtt_sub_item[topic_idx].topic_filter, ALI_SERVICE_RGB_CTRL_REPLY_PUB))
rt_snprintf(&topic[topic_idx * 128], MQTT_TOPIC_MAX_SIZE, "/sys/%s/%s/thing/service/rgb_ctrl_reply", product_key, device_name);
else if (!rt_strcmp(mqtt_sub_item[topic_idx].topic_filter, ALI_PROPERTY_POST_PUB))
rt_snprintf(&topic[topic_idx * 128], MQTT_TOPIC_MAX_SIZE, "/sys/%s/%s/thing/event/property/post", product_key, device_name);
else if (!rt_strcmp(mqtt_sub_item[topic_idx].topic_filter, ALI_PROPERTY_POST_REPLY_SUB))
rt_snprintf(&topic[topic_idx * 128], MQTT_TOPIC_MAX_SIZE, "/sys/%s/%s/thing/event/property/post_reply", product_key, device_name);
else
{
LOG_D("can not find the topic: %s", mqtt_sub_item[topic_idx].topic_filter);
continue;
}
}
return topic;
}
static void ali_mqtt_rgb_ctrl_msg_arrive (void *pcontext, void *handle, iotx_mqtt_event_msg_pt msg)
{
}
static void ali_mqtt_property_post_msg_arrive (void *pcontext, void *handle, iotx_mqtt_event_msg_pt msg)
{
}
static void ali_mqtt_event_handle(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg)
{
iotx_mqtt_topic_info_pt topic_info = (iotx_mqtt_topic_info_pt)msg->msg;
if (topic_info == RT_NULL)
{
LOG_D("Topic info is null! Exit.");
return;
}
uintptr_t packet_id = (uintptr_t)topic_info;
switch (msg->event_type)
{
case IOTX_MQTT_EVENT_UNDEF:
LOG_D("undefined event occur.");
break;
case IOTX_MQTT_EVENT_DISCONNECT:
LOG_I("MQTT disconnect.");
break;
case IOTX_MQTT_EVENT_RECONNECT:
LOG_I("MQTT reconnect.");
break;
case IOTX_MQTT_EVENT_SUBCRIBE_SUCCESS:
LOG_D("subscribe success, packet-id=%u", (unsigned int)packet_id);
break;
case IOTX_MQTT_EVENT_SUBCRIBE_TIMEOUT:
LOG_D("subscribe wait ack timeout, packet-id=%u", (unsigned int)packet_id);
break;
case IOTX_MQTT_EVENT_SUBCRIBE_NACK:
LOG_D("subscribe nack, packet-id=%u", (unsigned int)packet_id);
break;
case IOTX_MQTT_EVENT_UNSUBCRIBE_SUCCESS:
LOG_D("unsubscribe success, packet-id=%u", (unsigned int)packet_id);
break;
case IOTX_MQTT_EVENT_UNSUBCRIBE_TIMEOUT:
LOG_D("unsubscribe timeout, packet-id=%u", (unsigned int)packet_id);
break;
case IOTX_MQTT_EVENT_UNSUBCRIBE_NACK:
LOG_D("unsubscribe nack, packet-id=%u", (unsigned int)packet_id);
break;
case IOTX_MQTT_EVENT_PUBLISH_SUCCESS:
LOG_D("publish success, packet-id=%u", (unsigned int)packet_id);
break;
case IOTX_MQTT_EVENT_PUBLISH_TIMEOUT:
LOG_D("publish timeout, packet-id=%u", (unsigned int)packet_id);
break;
case IOTX_MQTT_EVENT_PUBLISH_NACK:
LOG_D("publish nack, packet-id=%u", (unsigned int)packet_id);
break;
case IOTX_MQTT_EVENT_PUBLISH_RECEIVED:
LOG_D("topic message arrived but without any related handle: topic=%.*s, topic_msg=%.*s",
topic_info->topic_len,
topic_info->ptopic,
topic_info->payload_len,
topic_info->payload);
break;
case IOTX_MQTT_EVENT_BUFFER_OVERFLOW:
LOG_D("buffer overflow, %s", msg->msg);
break;
default:
LOG_D("Should NOT arrive here.");
break;
}
}
static void mqtt_period_task(void)
{
/* get current time */
rt_uint64_t now;
now = (rt_uint64_t)(time(RT_NULL) - (3600 * 8));
now = now * 1000L;
/* <20><><EFBFBD><EFBFBD><EFBFBD>ϱ<EFBFBD><CFB1><EFBFBD><E8B1B8><EFBFBD><EFBFBD> */
{
cJSON *root = cJSON_CreateObject();
if (root)
{
cJSON *js_params = cJSON_CreateObject();
cJSON *js_quality = cJSON_CreateObject();
cJSON *js_status = cJSON_CreateObject();
cJSON *js_device = cJSON_CreateObject();
if (js_params && js_quality && js_status && js_device)
{
char str_id[16];
rt_memset(str_id, 0, sizeof(str_id));
rt_snprintf(str_id, sizeof(str_id), "%d", mqtt_packet_id++);
cJSON_AddStringToObject(root, "id", str_id);
cJSON_AddStringToObject(root, "version", "1.0");
cJSON_AddItemToObject(root, "params", js_params);
cJSON_AddItemToObject(js_params, "CurrentHumidity", js_quality);
cJSON_AddStringToObject(js_quality, "value", "28");
cJSON_AddNumberToObject(js_quality, "time", now);
cJSON_AddItemToObject(js_params, "CurrentTemperature", js_status);
cJSON_AddStringToObject(js_status, "value", "100");
cJSON_AddNumberToObject(js_status, "time", now);
cJSON_AddStringToObject(root, "method", "thing.event.property.post");
char *msg_pub = cJSON_PrintUnformatted(root);
if (msg_pub)
{
iotx_mqtt_topic_info_t topic_msg;
rt_memset(&topic_msg, 0, sizeof(iotx_mqtt_topic_info_t));
topic_msg.qos = IOTX_MQTT_QOS1;
topic_msg.retain = 0;
topic_msg.dup = 0;
topic_msg.payload = (void *)msg_pub;
topic_msg.payload_len = rt_strlen(msg_pub);
char *topic = mqtt_topic_find(ALI_PROPERTY_POST_PUB);
if (topic != RT_NULL)
IOT_MQTT_Publish(mqtt_client_hd, topic, &topic_msg);
rt_free(msg_pub);
}
}
cJSON_Delete(root);
}
}
}
static void mqtt_thread_main_thread(void *arg)
{
//<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
struct netdev* net = netdev_get_by_name("esp0");
//<2F><><EFBFBD><EFBFBD><EFBFBD>жϵ<D0B6>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
while(netdev_is_internet_up(net) != 1)
{
rt_thread_mdelay(200);
}
if (topic_buff != RT_NULL)
{
rt_free(topic_buff);
topic_buff = RT_NULL;
}
topic_buff = mqtt_check_load_topic();
if (topic_buff == RT_NULL)
{
LOG_D("Load MQTT Topic failed!");
return;
}
/* Initialize MQTT parameter */
iotx_mqtt_param_t mqtt_params;
rt_memset(&mqtt_params, 0x0, sizeof(mqtt_params));
mqtt_params.customize_info = MQTT_MAN_INFO_STRING;
/* timeout of request. uint: ms */
mqtt_params.request_timeout_ms = MQTT_REQUEST_TIMEOUT;
/* internal of keepalive checking: 60s~300s */
mqtt_params.keepalive_interval_ms = MQTT_KEEPALIVE_INTERNAL * 1000;
/* default is 0 */
mqtt_params.clean_session = 0;
/* MQTT read/write buffer size */
mqtt_params.read_buf_size = MQTT_MSGLEN;
mqtt_params.write_buf_size = MQTT_MSGLEN;
/* configure handle of event */
mqtt_params.handle_event.h_fp = ali_mqtt_event_handle;
mqtt_params.handle_event.pcontext = RT_NULL;
/* construct a MQTT device with specify parameter */
mqtt_client_hd = IOT_MQTT_Construct(&mqtt_params);
if (RT_NULL == mqtt_client_hd)
{
LOG_D("construct MQTT failed!");
}
/* sbuscribe all topic */
for (int i = 0; i < (sizeof(mqtt_sub_item) / sizeof(mqtt_subscribe_item)); i++)
{
if (mqtt_sub_item[i].topic_handle_func == RT_NULL)
continue;
if (IOT_MQTT_Subscribe(mqtt_client_hd, &topic_buff[i * 128], mqtt_sub_item[i].qos, mqtt_sub_item[i].topic_handle_func, mqtt_sub_item[i].pcontext) < 0)
{
LOG_D("IOT_MQTT_Subscribe() failed, topic = %s", &topic_buff[i * 128]);
}
}
IOT_MQTT_Yield(mqtt_client_hd, 200);
while (1)
{
rt_thread_mdelay(5000);
}
/* handle the MQTT packet received from TCP or SSL connection */
IOT_MQTT_Yield(mqtt_client_hd, 200);
/* ÿ10sִ<73><D6B4>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
if ((mqtt_period_cnt % 50) == 0)
mqtt_period_task();
mqtt_period_cnt++;
}
}
static void mqtt_connect_check_thread(void *arg)

View File

@ -968,7 +968,7 @@
<Group>
<GroupName>ali-iotkit</GroupName>
<tvExp>0</tvExp>
<tvExp>1</tvExp>
<tvExpOptDlg>0</tvExpOptDlg>
<cbSel>0</cbSel>
<RteFlg>0</RteFlg>

View File

@ -189,8 +189,8 @@
#define AT_DEVICE_USING_ESP8266
#define AT_DEVICE_ESP8266_INIT_ASYN
#define AT_DEVICE_ESP8266_SAMPLE
#define ESP8266_SAMPLE_WIFI_SSID "xgld1"
#define ESP8266_SAMPLE_WIFI_PASSWORD "xgld64627816"
#define ESP8266_SAMPLE_WIFI_SSID "ChinaNet-ssssss"
#define ESP8266_SAMPLE_WIFI_PASSWORD "SQHWLK9394"
#define ESP8266_SAMPLE_CLIENT_NAME "uart3"
#define ESP8266_SAMPLE_RECV_BUFF_LEN 512
#define PKG_USING_AT_DEVICE_LATEST_VERSION