#include <stdlib.h>
#include <string.h>
#include <rtthread.h>
#include "at_thread.h"
#include <at.h>
#include "board.h"
#include "cJSON.h"
#include "io_counter.h"
#include "stdio.h"
#include "nvm_data.h"
#include "board_config.h"
#define DBG_ENABLE
#define DBG_SECTION_NAME "[AT]"
#define DBG_LEVEL DBG_LOG
#define DBG_COLOR
#include <rtdbg.h>
#define THREAD_STACK_SIZE 1024
#define THREAD_TIMESLICE 5
#define THREAD_PRIORITY 25
static struct rt_thread thread_at;
static char thread_at_stack[THREAD_STACK_SIZE];
/* 邮箱控制块 */
struct rt_mailbox at_mb;
/* 用于放邮件的内存池 */
static uint32_t at_mb_pool=0;
#define MQTT_PAYLOAD_LEN_MAX 300
struct {
uint16_t len;
uint8_t buf[MQTT_PAYLOAD_LEN_MAX];
}sMqttPayload;
/* 按行数循环打印接收到的响应数据 */
void print_resp(at_response_t resp)
{
const char *line_buffer = RT_NULL;
LOG_D("Response buffer");
for(rt_size_t line_num = 1; line_num <= resp->line_counts; line_num++)
{
if((line_buffer = at_resp_get_line(resp, line_num)) != RT_NULL)
{
LOG_D("line %d buffer : %s", line_num, line_buffer);
}
else
{
LOG_E("Parse line buffer error!");
}
}
}
/* AT+RST reset */
int at_reset(int argc, char**argv)
{
at_response_t resp = RT_NULL;
int result = RT_ERROR;
if (argc != 1)
{
LOG_E("at_get_connstate - AT client send commands to AT server.");
return -1;
}
/* 创建响应结构体,设置最大支持响应数据长度为 256 字节
(最大响应长度用户根据实际需求自定义),响应数据行数无限制,超时时间为 5 秒 */
resp = at_create_resp(256, 0, rt_tick_from_millisecond(5000));
if (resp == RT_NULL)
{
LOG_E("No memory for response structure!");
return -2;
}
/* AT Client 发送查询 IP 地址命令并接收 AT Server 响应 */
/* 响应数据及信息存放在 resp 结构体中 */
result = at_exec_cmd(resp, "AT+RST");
if (result != RT_EOK)
{
LOG_E("[AT+RST] send commands failed or return response error!");
goto __exit;
}
/* 按行数循环打印接收到的响应数据 */
print_resp(resp);
/* 按自定义表达式(sscanf 解析方式)解析数据,得到对应数据 */
{
LOG_D("Parse arguments");
/* 解析响应数据中 OK */
if ((at_resp_get_line_by_kw(resp, "OK")) == RT_NULL)
{
LOG_E("parse error. not found 'ok'\n");
result = RT_EINVAL;
goto __exit;
}
else{
result = RT_EOK;
}
}
__exit:
if(resp)
{
/* 删除 resp 结构体 */
at_delete_resp(resp);
}
return result;
}
/* AT+CIFSR Query local IP address and MAC */
int at_get_ip_and_mac(int argc, char**argv)
{
at_response_t resp = RT_NULL;
int result = 0;
if (argc != 1)
{
LOG_E("at_get_ip_and_mac - AT client send commands to AT server.");
return -1;
}
/* 创建响应结构体,设置最大支持响应数据长度为 256 字节
(最大响应长度用户根据实际需求自定义),响应数据行数无限制,超时时间为 5 秒 */
resp = at_create_resp(256, 0, rt_tick_from_millisecond(5000));
if (resp == RT_NULL)
{
LOG_E("No memory for response structure!");
return -2;
}
/* 关闭回显功能 */
at_exec_cmd(resp, "ATE1");
/* AT Client 发送查询 IP 地址命令并接收 AT Server 响应 */
/* 响应数据及信息存放在 resp 结构体中 */
result = at_exec_cmd(resp, "AT+CIFSR");
// LOG_E("send\n");
if (result != RT_EOK)
{
LOG_E("[AT+CIFSR] send commands failed or return response error!");
goto __exit;
}
else{
// LOG_D("send ok\n");
}
/* 按行数循环打印接收到的响应数据 */
print_resp(resp);
/* 按自定义表达式(sscanf 解析方式)解析数据,得到对应数据 */
{
char resp_arg[AT_CMD_MAX_LEN] = { 0 };
/* 自定义数据解析表达式 ,用于解析两双引号之间字符串信息 */
const char * resp_expr = "%*[^\"]\"%[^\"]";
LOG_D("Parse arguments");
/* 解析响应数据中第一行数据,得到对应 IP 地址 */
if (at_resp_parse_line_args(resp, 2, resp_expr, resp_arg) == 1)
{
LOG_D("Station IP : %s", resp_arg);
memset(resp_arg, 0x00, AT_CMD_MAX_LEN);
}
else
{
LOG_E("Parse error, current line buff : %s", at_resp_get_line(resp, 4));
}
/* 解析响应数据中第二行数据,得到对应 MAC 地址 */
if (at_resp_parse_line_args(resp, 3, resp_expr, resp_arg) == 1)
{
LOG_D("Station MAC : %s", resp_arg);
}
else
{
LOG_E("Parse error, current line buff : %s", at_resp_get_line(resp, 5));
goto __exit;
}
}
__exit:
if(resp)
{
/* 删除 resp 结构体 */
at_delete_resp(resp);
}
return result;
}
/* AT+CWJAP get connect state */
int at_get_mode(int argc, char**argv, char *mode)
{
at_response_t resp = RT_NULL;
int result = RT_ERROR;
if (argc != 1)
{
LOG_E("at_get_connstate - AT client send commands to AT server.");
return -1;
}
/* 创建响应结构体,设置最大支持响应数据长度为 256 字节
(最大响应长度用户根据实际需求自定义),响应数据行数无限制,超时时间为 5 秒 */
resp = at_create_resp(256, 0, rt_tick_from_millisecond(5000));
if (resp == RT_NULL)
{
LOG_E("No memory for response structure!");
return -2;
}
/* AT Client 发送查询 IP 地址命令并接收 AT Server 响应 */
/* 响应数据及信息存放在 resp 结构体中 */
result = at_exec_cmd(resp, "AT+CWMODE?");
if (result != RT_EOK)
{
LOG_E("[AT+CWMODE?] send commands failed or return response error!");
goto __exit;
}
/* 按行数循环打印接收到的响应数据 */
print_resp(resp);
/* 按自定义表达式(sscanf 解析方式)解析数据,得到对应数据 */
{
char resp_arg[10] = { 0 };
const char * format = "%*[^:]:%[^\r\n]";
LOG_D("Parse arguments");
/* 解析响应数据中第一行数据,得到对应 状态 */
if (at_resp_parse_line_args(resp, 2, format, resp_arg) > 0){
LOG_D("wifi work mode : %s", resp_arg);
// LOG_D("\n\n");
if(strlen(resp_arg) == 1){
*mode = resp_arg[0] - '0';
}
}
else{
LOG_E("Parse error, current line buff : %s", at_resp_get_line(resp, 2));
}
result = RT_EOK;
}
__exit:
if(resp)
{
/* 删除 resp 结构体 */
at_delete_resp(resp);
}
return result;
}
/* 设置工作模式 */
int at_set_mode(int argc, char**argv)
{
at_response_t resp = RT_NULL;
int result = RT_ERROR;
if (argc != 1)
{
LOG_E("at_get_ip_and_mac - AT client send commands to AT server.");
return -1;
}
/* 创建响应结构体,设置最大支持响应数据长度为 256 字节
(最大响应长度用户根据实际需求自定义),响应数据行数无限制,超时时间为 5 秒 */
resp = at_create_resp(256, 0, rt_tick_from_millisecond(5000));
if (resp == RT_NULL)
{
LOG_E("No memory for response structure!");
return -2;
}
/* AT C
评论0