first commit for opensource
first commit for opensource
This commit is contained in:
363
devices/esp8266_tencent_firmware/esp8266_tencent_firmware.c
Normal file
363
devices/esp8266_tencent_firmware/esp8266_tencent_firmware.c
Normal file
@@ -0,0 +1,363 @@
|
||||
#include "stdio.h"
|
||||
#include "string.h"
|
||||
|
||||
#include "tos_at.h"
|
||||
#include "tos_hal.h"
|
||||
#include "tencent_firmware_module_wrapper.h"
|
||||
#include "esp8266_tencent_firmware.h"
|
||||
|
||||
/*
|
||||
* config dev info to module, do this operate only once in factroy is suggested
|
||||
*/
|
||||
int esp8266_tencent_firmware_module_info_set(device_info_t *info, tls_mode_t mode)
|
||||
{
|
||||
at_echo_t echo;
|
||||
|
||||
tos_at_echo_create(&echo, NULL, 0, "+TCDEVINFOSET:OK");
|
||||
|
||||
tos_at_cmd_exec(&echo, 2000, "AT+TCDEVINFOSET=%d,\"%s\",\"%s\",\"%s\"\r\n",
|
||||
mode, info->product_id, info->device_name, info->device_serc);
|
||||
if (echo.status == AT_ECHO_STATUS_OK || echo.status == AT_ECHO_STATUS_EXPECT) {
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* mqtt setup connect */
|
||||
int esp8266_tencent_firmware_module_mqtt_conn(mqtt_param_t init_params)
|
||||
{
|
||||
int try = 0;
|
||||
at_echo_t echo;
|
||||
|
||||
tos_at_echo_create(&echo, NULL, 0, "+TCMQTTCONN:OK");
|
||||
while (try++ < 10) {
|
||||
tos_at_cmd_exec(&echo, 2000, "AT+TCMQTTCONN=%d,%d,%d,%d,%d\r\n", init_params.tls_mode,
|
||||
init_params.command_timeout, init_params.keep_alive_interval_ms,
|
||||
init_params.clean_session, init_params.auto_connect_enable);
|
||||
if (echo.status == AT_ECHO_STATUS_OK || echo.status == AT_ECHO_STATUS_EXPECT) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* mqtt disconn */
|
||||
int esp8266_tencent_firmware_module_mqtt_discon(void)
|
||||
{
|
||||
at_echo_t echo;
|
||||
|
||||
tos_at_echo_create(&echo, NULL, 0, NULL);
|
||||
|
||||
tos_at_cmd_exec(&echo, 1000, "AT+TCMQTTDISCONN\r\n");
|
||||
if (echo.status == AT_ECHO_STATUS_OK) {
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int esp8266_tencent_firmware_module_mqtt_publ(const char *topic, qos_t qos, char *payload)
|
||||
{
|
||||
at_echo_t echo;
|
||||
|
||||
tos_at_echo_create(&echo, NULL, 0, ">");
|
||||
|
||||
tos_at_cmd_exec(&echo, 1000, "AT+TCMQTTPUBL=\"%s\",%d,%d\r\n", topic, qos, strlen(payload));
|
||||
if (echo.status != AT_ECHO_STATUS_OK && echo.status != AT_ECHO_STATUS_EXPECT) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
tos_at_echo_create(&echo, NULL, 0, "+TCMQTTPUB:OK");
|
||||
tos_at_raw_data_send(&echo, 1000, (uint8_t *)payload, strlen(payload));
|
||||
if (echo.status != AT_ECHO_STATUS_OK && echo.status != AT_ECHO_STATUS_EXPECT) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* mqtt pub msg */
|
||||
int esp8266_tencent_firmware_module_mqtt_pub(const char *topic, qos_t qos, char *payload)
|
||||
{
|
||||
at_echo_t echo;
|
||||
|
||||
tos_at_echo_create(&echo, NULL, 0, "+TCMQTTPUB:OK");
|
||||
|
||||
tos_at_cmd_exec(&echo, 1000, "AT+TCMQTTPUB=\"%s\",%d,\"%s\"\r\n", topic, qos, payload);
|
||||
if (echo.status == AT_ECHO_STATUS_OK || echo.status == AT_ECHO_STATUS_EXPECT) {
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int esp8266_tencent_firmware_module_mqtt_sub(char *topic, qos_t qos)
|
||||
{
|
||||
at_echo_t echo;
|
||||
|
||||
tos_at_echo_create(&echo, NULL, 0, "+TCMQTTSUB:OK");
|
||||
|
||||
tos_at_cmd_exec(&echo, 2000, "AT+TCMQTTSUB=\"%s\",%d\r\n", topic, qos);
|
||||
if (echo.status == AT_ECHO_STATUS_OK || echo.status == AT_ECHO_STATUS_EXPECT) {
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int esp8266_tencent_firmware_module_mqtt_unsub(const char *topic)
|
||||
{
|
||||
at_echo_t echo;
|
||||
|
||||
tos_at_echo_create(&echo, NULL, 0, "+TCMQTTUNSUB:OK");
|
||||
|
||||
tos_at_cmd_exec(&echo, 2000, "AT+TCMQTTUNSUB=\"%s\"\r\n", topic);
|
||||
if (echo.status == AT_ECHO_STATUS_OK || echo.status == AT_ECHO_STATUS_EXPECT) {
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int esp8266_tencent_firmware_module_mqtt_state_get(mqtt_state_t *state)
|
||||
{
|
||||
char *str;
|
||||
int ret_state;
|
||||
at_echo_t echo;
|
||||
char echo_buffer[64];
|
||||
|
||||
tos_at_echo_create(&echo, echo_buffer, sizeof(echo_buffer), NULL);
|
||||
|
||||
tos_at_cmd_exec(&echo, 1000, "AT+TCMQTTSTATE?\r\n");
|
||||
if (echo.status != AT_ECHO_STATUS_OK) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
str = strstr(echo.buffer, "+TCMQTTSTATE:");
|
||||
sscanf(str, "+TCMQTTSTATE:%d", &ret_state);
|
||||
if (ret_state == 0) {
|
||||
*state = MQTT_STATE_DISCONNECTED;
|
||||
return 0;
|
||||
} else if (ret_state == 1) {
|
||||
*state = MQTT_STATE_CONNECTED;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int esp8266_tencent_firmware_module_debug_level_set(int log_level)
|
||||
{
|
||||
at_echo_t echo;
|
||||
|
||||
tos_at_echo_create(&echo, NULL, 0, NULL);
|
||||
|
||||
tos_at_cmd_exec(&echo, 1000, "AT+TCTEMLOG=%d\r\n", log_level);
|
||||
if (echo.status == AT_ECHO_STATUS_OK) {
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int esp8266_tencent_firmware_join_ap(const char *ssid, const char *pwd)
|
||||
{
|
||||
int try = 0;
|
||||
at_echo_t echo;
|
||||
|
||||
tos_at_echo_create(&echo, NULL, 0, "WIFI CONNECTED");
|
||||
while (try++ < 10) {
|
||||
tos_at_cmd_exec(&echo, 10000, "AT+CWJAP=\"%s\",\"%s\"\r\n", ssid, pwd);
|
||||
if (echo.status == AT_ECHO_STATUS_OK || echo.status == AT_ECHO_STATUS_EXPECT) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int esp8266_tencent_firmware_restore(void)
|
||||
{
|
||||
int try = 0;
|
||||
at_echo_t echo;
|
||||
|
||||
tos_at_echo_create(&echo, NULL, 0, "ready");
|
||||
while (try++ < 10) {
|
||||
tos_at_cmd_exec(&echo, 5000, "AT+RESTORE\r\n");
|
||||
if (echo.status == AT_ECHO_STATUS_OK || echo.status == AT_ECHO_STATUS_EXPECT) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int esp8266_tencent_firmware_echo_close(void)
|
||||
{
|
||||
at_echo_t echo;
|
||||
|
||||
tos_at_echo_create(&echo, NULL, 0, NULL);
|
||||
tos_at_cmd_exec(&echo, 1000, "ATE0\r\n");
|
||||
if (echo.status == AT_ECHO_STATUS_OK) {
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int esp8266_tencent_firmware_net_mode_set(sal_net_mode_t mode)
|
||||
{
|
||||
int try = 0;
|
||||
char *cmd = NULL;
|
||||
at_echo_t echo;
|
||||
|
||||
switch (mode) {
|
||||
case SAL_NET_MODE_STA:
|
||||
cmd = "AT+CWMODE=1\r\n";
|
||||
break;
|
||||
case SAL_NET_MODE_AP:
|
||||
cmd = "AT+CWMODE=2\r\n";
|
||||
break;
|
||||
case SAL_NET_MODE_STA_AP:
|
||||
cmd = "AT+CWMODE=3\r\n";
|
||||
break;
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
tos_at_echo_create(&echo, NULL, 0, "no change");
|
||||
while (try++ < 10) {
|
||||
tos_at_cmd_exec(&echo, 1000, cmd);
|
||||
if (echo.status == AT_ECHO_STATUS_OK || echo.status == AT_ECHO_STATUS_EXPECT) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int esp8266_tencent_firmware_send_mode_set(sal_send_mode_t mode)
|
||||
{
|
||||
int try = 0;
|
||||
at_echo_t echo;
|
||||
|
||||
tos_at_echo_create(&echo, NULL, 0, NULL);
|
||||
while (try++ < 10) {
|
||||
tos_at_cmd_exec(&echo, 1000, "AT+CIPMODE=%d\r\n", mode == SAL_SEND_MODE_NORMAL ? 0 : 1);
|
||||
if (echo.status == AT_ECHO_STATUS_OK) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int esp8266_tencent_firmware_init(void)
|
||||
{
|
||||
printf("Init ESP8266 with tencent firmware ...\n");
|
||||
|
||||
if (esp8266_tencent_firmware_restore() != 0) {
|
||||
printf("esp8266 restore FAILED\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (esp8266_tencent_firmware_echo_close() != 0) {
|
||||
printf("esp8266 echo close FAILED\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (esp8266_tencent_firmware_net_mode_set(SAL_NET_MODE_STA) != 0) {
|
||||
printf("esp8266 net mode set FAILED\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (esp8266_tencent_firmware_send_mode_set(SAL_SEND_MODE_NORMAL) != 0) {
|
||||
printf("esp8266 send mode set FAILED\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("Init ESP8266 with tencent firmware Done\n" );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
__STATIC__ uint8_t topic_buffer[64];
|
||||
__STATIC__ uint8_t payload_buffer[64];
|
||||
|
||||
void esp8266_tencent_firmware_recvpub(void)
|
||||
{
|
||||
/*
|
||||
+TCMQTTRCVPUB:"xxx/yyy/zzz",44,"{"type":"get", "clientToken":"123456781234"}"
|
||||
*/
|
||||
|
||||
uint8_t data;
|
||||
int read_len = 0, payload_len = 0;
|
||||
|
||||
while (1) {
|
||||
if (tos_at_uart_read(&data, 1) != 1) {
|
||||
return;
|
||||
}
|
||||
if (data == '"') {
|
||||
continue;
|
||||
} else if (data == ',') {
|
||||
break;
|
||||
}
|
||||
if (read_len < sizeof(topic_buffer)) {
|
||||
topic_buffer[read_len++] = data;
|
||||
}
|
||||
}
|
||||
if (read_len == sizeof(topic_buffer)) {
|
||||
topic_buffer[read_len - 1] = '\0';
|
||||
} else {
|
||||
topic_buffer[read_len] = '\0';
|
||||
}
|
||||
|
||||
while (1) {
|
||||
if (tos_at_uart_read(&data, 1) != 1) {
|
||||
return;
|
||||
}
|
||||
if (data == ',') {
|
||||
break;
|
||||
}
|
||||
payload_len = payload_len * 10 + (data - '0');
|
||||
}
|
||||
|
||||
if (payload_len > sizeof(payload_buffer)) {
|
||||
payload_len = sizeof(payload_buffer);
|
||||
}
|
||||
|
||||
read_len = tos_at_uart_read(payload_buffer, payload_len + 2);
|
||||
if (read_len != payload_len + 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
printf("topic received: %s\n", topic_buffer);
|
||||
printf("payload: %s\n", payload_buffer);
|
||||
}
|
||||
|
||||
at_event_t esp8266_tencent_firmware_at_event[] = {
|
||||
{ "+TCMQTTRCVPUB:", esp8266_tencent_firmware_recvpub },
|
||||
};
|
||||
|
||||
tencent_firmware_module_t tencent_firmware_module_esp8266 = {
|
||||
.init = esp8266_tencent_firmware_init,
|
||||
.info_set = esp8266_tencent_firmware_module_info_set,
|
||||
.mqtt_conn = esp8266_tencent_firmware_module_mqtt_conn,
|
||||
.mqtt_discon = esp8266_tencent_firmware_module_mqtt_discon,
|
||||
.mqtt_pub = esp8266_tencent_firmware_module_mqtt_pub,
|
||||
.mqtt_publ = esp8266_tencent_firmware_module_mqtt_publ,
|
||||
.mqtt_sub = esp8266_tencent_firmware_module_mqtt_sub,
|
||||
.mqtt_unsub = esp8266_tencent_firmware_module_mqtt_unsub,
|
||||
.mqtt_state_get = esp8266_tencent_firmware_module_mqtt_state_get,
|
||||
.debug_level_set = esp8266_tencent_firmware_module_debug_level_set,
|
||||
};
|
||||
|
||||
int esp8266_tencent_firmware_sal_init(hal_uart_port_t uart_port)
|
||||
{
|
||||
if (tos_at_init(uart_port, esp8266_tencent_firmware_at_event,
|
||||
sizeof(esp8266_tencent_firmware_at_event) /
|
||||
sizeof(esp8266_tencent_firmware_at_event[0])) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tos_tf_module_register(&tencent_firmware_module_esp8266) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (tos_tf_module_init() != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
38
devices/esp8266_tencent_firmware/esp8266_tencent_firmware.h
Normal file
38
devices/esp8266_tencent_firmware/esp8266_tencent_firmware.h
Normal file
@@ -0,0 +1,38 @@
|
||||
/*----------------------------------------------------------------------------
|
||||
* Tencent is pleased to support the open source community by making TencentOS
|
||||
* available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
* If you have downloaded a copy of the TencentOS binary from Tencent, please
|
||||
* note that the TencentOS binary is licensed under the BSD 3-Clause License.
|
||||
*
|
||||
* If you have downloaded a copy of the TencentOS source code from Tencent,
|
||||
* please note that TencentOS source code is licensed under the BSD 3-Clause
|
||||
* License, except for the third-party components listed below which are
|
||||
* subject to different license terms. Your integration of TencentOS into your
|
||||
* own projects may require compliance with the BSD 3-Clause License, as well
|
||||
* as the other licenses applicable to the third-party components included
|
||||
* within TencentOS.
|
||||
*---------------------------------------------------------------------------*/
|
||||
|
||||
#ifndef __ESP8266_TENCENT_FIRMWARE_H__
|
||||
#define __ESP8266_TENCENT_FIRMWARE_H__
|
||||
|
||||
#include "tos_hal.h"
|
||||
|
||||
typedef enum sal_net_mode {
|
||||
SAL_NET_MODE_STA,
|
||||
SAL_NET_MODE_AP,
|
||||
SAL_NET_MODE_STA_AP,
|
||||
} sal_net_mode_t;
|
||||
|
||||
typedef enum sal_send_mode {
|
||||
SAL_SEND_MODE_NORMAL,
|
||||
SAL_SEND_MODE_TRANSPARENT,
|
||||
} sal_send_mode_t;
|
||||
|
||||
int esp8266_tencent_firmware_sal_init(hal_uart_port_t uart_port);
|
||||
int esp8266_tencent_firmware_join_ap(const char *ssid, const char *pwd);
|
||||
|
||||
#endif /* __ESP8266_TENCENT_FIRMWARE_H__ */
|
||||
|
Reference in New Issue
Block a user