Files
kunlun/app/grapp/lwmqtt/src/lwmqtt_aliyun.c
2024-09-28 14:24:04 +08:00

176 lines
4.9 KiB
C

/****************************************************************************
Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
be copied by any method or incorporated into another program without
the express written consent of Aerospace C.Power. This Information or any portion
thereof remains the property of Aerospace C.Power. The Information contained herein
is believed to be accurate and Aerospace C.Power assumes no responsibility or
liability for its use in any way and conveys no license or title under
any patent or copyright and makes no representation or warranty that this
Information is free from patent or copyright infringement.
****************************************************************************/
#include "lwmqtt_main.h"
#include "iot_io_api.h"
#include "iot_string_api.h"
#include "iot_errno_api.h"
#include "lwmqtt_aliyun.h"
#include "iot_board_api.h"
#include "iot_gpio_api.h"
#include "os_mem_api.h"
char p_temp_buffer[512];
char *lwmqtt_strstr(char* src,char *sub)
{
const char *bp=src, *sp=sub;
if(NULL == src || NULL == sub) {
return src;
}
while (*src) {
bp = src;
sp = sub;
do{
if (!*sp) return src;
} while (*bp++ == *sp++);
src++;
}
return NULL;
}
void lwmqtt_ali_led_force_on(uint32_t force_on)
{
uint8_t led_pin_rx, led_pin_tx;
led_pin_rx = iot_board_get_gpio(GPIO_RX_LED);
led_pin_tx = iot_board_get_gpio(GPIO_RX_LED);
(void)iot_gpio_open_as_output(led_pin_rx);
(void)iot_gpio_open_as_output(led_pin_tx);
(void)iot_gpio_set_pull_mode(led_pin_rx, GPIO_PULL_UP);
(void)iot_gpio_set_pull_mode(led_pin_tx, GPIO_PULL_UP);
(void)iot_gpio_value_set(led_pin_rx, (force_on == 0));
(void)iot_gpio_value_set(led_pin_tx, (force_on == 0));
return;
}
static void lwmqtt_ali_func_powerswitch(lwmqtt_alimsg_t *p_alimsg)
{
lwmqtt_string_t topic;
lwmqtt_alimsg_t alimsg_to_ge;
lwmqtt_message_t message;
if (LWMQTT_ALIMTHD_SET != p_alimsg->alimthd) {
return;
}
lwmqtt_ali_led_force_on(p_alimsg->param);
if (LWMQTT_SOURCE_ETH == p_alimsg->src) {
topic.len = iot_strlen(LWMQTT_ALI_PUB);
topic.data = LWMQTT_ALI_PUB;
message.qos = LWMQTT_QOS0;
message.retained = 0;
message.payload = p_temp_buffer;
message.payload_len = iot_sprintf(p_temp_buffer, RARAM_STRING_POST_SWITCH,
p_alimsg->param);
lwmqtt_message_public(&topic, &message);
message.payload_len = iot_sprintf(p_temp_buffer, RARAM_STRING_POST_MESSAGE,
p_alimsg->param);
lwmqtt_message_public(&topic, &message);
os_mem_cpy(&alimsg_to_ge, p_alimsg, sizeof(alimsg_to_ge));
alimsg_to_ge.src = LWMQTT_SOURCE_PLC;
lwmqtt_message_send_to_lowlayer_ge((uint8_t *)&alimsg_to_ge,
sizeof(alimsg_to_ge));
}
return;
}
static uint32_t lwmqtt_ali_message_analyze(lwmqtt_string_t *topic,
lwmqtt_message_t * org_message, lwmqtt_alimsg_t *p_alimsg)
{
uint16_t methold, function, dlen = 0;
uint8_t *p_fun_start;
uint32_t param = 0;
if (NULL != lwmqtt_strstr(topic->data, LWMQTT_ALI_MTHD_SET_STR)) {
methold = LWMQTT_ALIMTHD_SET;
} else if (NULL != lwmqtt_strstr(topic->data, LWMQTT_ALI_MTHD_GET_STR)) {
methold = LWMQTT_ALIMTHD_GET;
} else if (NULL != lwmqtt_strstr(topic->data, LWMQTT_ALI_MTHD_PST_STR)) {
methold = LWMQTT_ALIMTHD_POST;
} else {
methold = LWMQTT_ALIMTHD_MAX;
return ERR_FAIL;
}
if (NULL != (p_fun_start = (uint8_t *)lwmqtt_strstr(org_message->payload,
PARAM_STRING_SWITCH))) {
function = LWMQTT_ALIFUNC_POWERSWITCH;
param = p_fun_start[iot_strlen(PARAM_STRING_SWITCH)] == '0' ? 0 : 1;
} else {
function = LWMQTT_ALIFUNC_MAX;
return ERR_FAIL;
}
p_alimsg->alifunc = function;
p_alimsg->alimthd = methold;
p_alimsg->dlen = dlen;
p_alimsg->param = param;
p_alimsg->src = LWMQTT_SOURCE_ETH;
return ERR_OK;
}
uint32_t lwmqtt_ali_function_deliver(lwmqtt_alimsg_t *p_alimsg)
{
if (NULL == p_alimsg) {
return ERR_FAIL;
}
if (LWMQTT_ALIFUNC_POWERSWITCH == p_alimsg->alifunc) {
lwmqtt_ali_func_powerswitch(p_alimsg);
} else {
return ERR_FAIL;
}
return ERR_OK;
}
void lwmqtt_ali_message_receive(lwmqtt_string_t *topic,
lwmqtt_message_t *message)
{
lwmqtt_alimsg_t alimsg;
if (NULL == message) {
LWMQTT_DEBUG(("\r\nBad argument!\r\n"));
return;
}
if (ERR_OK != lwmqtt_ali_message_analyze(topic, message, &alimsg)) {
LWMQTT_DEBUG(("\r\nmessage analyze failed!\r\n"));
return;
}
if (ERR_OK != lwmqtt_ali_function_deliver(&alimsg)) {
LWMQTT_DEBUG(("\r\nmessage deliver failed!\r\n"));
return;
}
return;
}