记录某次AT编写代码资料
#define AT_OK (0)
#define AT_ERR (1)
#define AT_ERR_UNINVAIL (2)
#define AT_ERR_FUN_UNUSED (3)
typedef enum{
AT_CMD_TEST = 0,
AT_CMD_NAME,
AT_CMD_DISA,
AT_CMD_TX_POWER,
AT_CMD_ADV_INTERVAL,
AT_CMD_CON_INTERVAL,
AT_CMD_ADV_ONOFF,
AT_GET_VER,
AT_GET_CON_STATE,
AT_CMD_UART_CFG,
AT_END
}AT_Cmd;
typedef struct {
AT_Cmd cmd;
unsigned char *str;
pFunc cb;
}AT_cmd_func;
const AT_cmd_func at_cmd_func[] = {
{AT_CMD_TEST, "AT", at_cmd_test},
{AT_CMD_NAME, "AT+NAME=", at_cmd_name},
{AT_CMD_DISA, "AT+DISC", NULL},
{AT_CMD_TX_POWER, "AT+TX=", NULL},
{AT_CMD_ADV_INTERVAL, "AT+ADV_INTERVAL=", NULL},
{AT_CMD_CON_INTERVAL, "AT+CON_INTERVAL=", NULL},
{AT_CMD_ADV_ONOFF, "AT_ADV=", NULL},
{AT_GET_VER, "AT+VERSION=", NULL},
{AT_GET_CON_STATE, "AT+STATE", NULL},
{AT_CMD_UART_CFG, "AT+UART_CFG=", NULL},
{AT_END, NULL, NULL}
};
使用
typedef unsigned char (*pFunc)(unsigned char *ptr, unsigned char len);
unsigned char at_cmd_test(unsigned char *p, unsigned char len){
printf("AT+OK\r\n");
return 0;
}
unsigned char AT_cmd_search(unsigned char *p, unsigned char len){
unsigned char ret = 0;
unsigned char *pstr;
unsigned char i, n;
for(i=1; at_cmd_func[i].cmd != AT_END; i++){
n = mstrlen(at_cmd_func[i].str);
if(!mstrncmp(p, at_cmd_func[i].str, n)){
ret = i;
break;
}
}
return ret;
}
unsigned char at_cmd_parse(unsigned char *p, unsigned char len){
unsigned char ret = AT_SUCCESS;
unsigned char index = 0;
if(len < 4) return AT_ERR;
if((p[0] == 'A') && (p[1] == 'T') && (p[len-2] == 0x0D) && (p[len-1] == 0x0A)){
if(len == 4){
if(at_cmd_func[AT_CMD_TEST].cb != NULL)
at_cmd_func[AT_CMD_TEST].cb(NULL, 0);
}else if(p[2] == '+'){
index = AT_cmd_search(p, len);
if(index){
if(at_cmd_func[index].cb != NULL){
unsigned char n;
n = mstrlen(at_cmd_func[index].str);
ret = at_cmd_func[index].cb(p+n, len-n);
}else
ret = AT_ERR_FUN_UNUSED;
}else{
ret = AT_ERR_UNINVAIL;
}
}
}else{
return AT_ERR;
}
return ret;
}
int main(void){
unsigned char ret;
unsigned char i, n, m;
#define CMD_NUM_MAX (5)
char *test_cmd[CMD_NUM_MAX]={
"AT\r\n",
"AT+NAME=BLE-TEST\r\n",
"AT+NAME=?\r\n",
"AT+DISC\r\n",
"AT+NBME=?\r\n",
};
for(i=0; i<CMD_NUM_MAX; i++){
m = mstrlen(test_cmd[i]);
ret = at_cmd_parse(test_cmd[i], m);
if(ret)
AT_DEBUG_INFO("AT ERR! = %d\r\n", ret);
}
return 0;
}