AT-Command/at_chat.h

148 lines
5.2 KiB
C
Raw Normal View History

2020-09-22 22:14:09 +08:00
/*******************************************************************************
* Copyright(C)20 roger.luo
* All rights reserved.
* @file atchat.h
* @brief AT command communications.
*
* @version 3.0
* @date 2018-02-11
* @author roger.luo
*
* Change Logs:
* Date Author Notes
* 2016-01-22 roger.luo Initial version.
* 2017-05-21 roger.luo 1.1 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* 2018-02-11 roger.luo 3.0
* 2020-01-02 roger.luo 4.0 os version
*******************************************************************************/
2020-06-13 12:24:28 +08:00
#ifndef _ATCHAT_H_
#define _ATCHAT_H_
#include "at_util.h"
#include <list.h>
#include <stdbool.h>
#define MAX_AT_CMD_LEN 128
struct at_core;
/*urc<72><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD> -----------------------------------------------------------------*/
typedef struct {
const char *prefix; //<2F><>Ҫƥ<D2AA><C6A5><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7>
void (*handler)(char *recvbuf, int size);
}utc_item_t;
typedef struct {
unsigned int (*write)(const void *buf, unsigned int len); /*<2A><><EFBFBD>ͽӿ<CDBD>*/
unsigned int (*read)(void *buf, unsigned int len); /*<2A><><EFBFBD>սӿ<D5BD>*/
void (*lock)(void); /*<2A><><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD>OS*/
void (*unlock)(void); /*<2A><><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD>OS*/
/*Events -----------------------------------------------------------------*/
void (*before_at)(void); /*<2A><>ʼִ<CABC><D6B4>AT*/
void (*after_at)(void);
void (*error)(void);
2020-09-22 22:14:09 +08:00
utc_item_t *utc_tbl; /*utc <20><>*/
unsigned char *urc_buf; /*urc<72><63><EFBFBD>ջ<EFBFBD><D5BB><EFBFBD><EFBFBD><EFBFBD>*/
2020-06-13 12:24:28 +08:00
unsigned char *rcv_buf;
2020-09-22 22:14:09 +08:00
unsigned short urc_tbl_count;
unsigned short urc_bufsize; /*urc<72><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С*/
2020-06-13 12:24:28 +08:00
unsigned short rcv_bufsize; /*<2A><><EFBFBD>ջ<EFBFBD><D5BB><EFBFBD><EFBFBD><EFBFBD>*/
}at_core_conf_t;
/*AT<41><54>ҵ<EFBFBD><D2B5><EFBFBD>л<EFBFBD><D0BB><EFBFBD>*/
typedef struct {
2020-09-22 22:14:09 +08:00
int i,j,state;
void *params;
2020-06-13 12:24:28 +08:00
void (*reset_timer)(struct at_core *ac);
2020-09-22 22:14:09 +08:00
bool (*is_timeout)(struct at_core *ac, unsigned int ms); /*ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>*/
void (*printf)(struct at_core *ac, const char *fmt, ...);
char * (*find)(struct at_core *ac, const char *expect);
2020-06-13 12:24:28 +08:00
char * (*recvbuf)(struct at_core *ac); /*ָ<><D6B8><EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><D5BB><EFBFBD><EFBFBD><EFBFBD>*/
unsigned int(*recvlen)(struct at_core *ac); /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܳ<EFBFBD><DCB3><EFBFBD>*/
void (*recvclr)(struct at_core *ac); /*<2A><><EFBFBD>ս<EFBFBD><D5BD>ջ<EFBFBD><D5BB><EFBFBD><EFBFBD><EFBFBD>*/
bool (*abort)(struct at_core *ac); /*<2A><>ִֹ<D6B9><D6B4>*/
}at_env_t;
/*AT<41><54><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6>*/
typedef enum {
AT_RET_OK = 0, /*ִ<>гɹ<D0B3>*/
AT_RET_ERROR, /*ִ<>д<EFBFBD><D0B4><EFBFBD>*/
AT_RET_TIMEOUT, /*<2A><>Ӧ<EFBFBD><D3A6>ʱ*/
2020-09-22 22:14:09 +08:00
AT_RET_ABORT, /*ǿ<><C7BF><EFBFBD><EFBFBD>ֹ*/
2020-06-13 12:24:28 +08:00
}at_return;
/*AT<41><54>Ӧ */
typedef struct {
void *param;
2020-09-22 22:14:09 +08:00
char *recvbuf;
unsigned short recvcnt;
2020-06-13 12:24:28 +08:00
at_return ret;
}at_response_t;
typedef void (*at_callback_t)(at_response_t *r);
/*AT״̬ */
typedef enum {
AT_STATE_IDLE, /*<2A><><EFBFBD><EFBFBD>״̬*/
AT_STATE_WAIT, /*<2A>ȴ<EFBFBD>ִ<EFBFBD><D6B4>*/
AT_STATE_EXEC, /*<2A><><EFBFBD><EFBFBD>ִ<EFBFBD><D6B4>*/
}at_work_state;
/*AT<41><54>ҵ<EFBFBD><D2B5>*/
typedef struct {
at_work_state state : 3;
unsigned char type : 3;
unsigned char abort : 1;
void *param;
2020-09-22 22:14:09 +08:00
void *info;
2020-06-13 12:24:28 +08:00
struct list_head node;
}at_item_t;
/*AT<41><54><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ------------------------------------------------------------------*/
typedef struct at_core{
2020-09-22 22:14:09 +08:00
at_core_conf_t cfg;
2020-06-13 12:24:28 +08:00
at_env_t env;
2020-09-22 22:14:09 +08:00
at_item_t tbl[10];
2020-06-13 12:24:28 +08:00
at_item_t *cursor;
struct list_head ls_ready, ls_idle; /*<2A><><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD><D2B5>*/
2020-09-22 22:14:09 +08:00
unsigned int resp_timer;
unsigned int urc_timer;
at_return ret;
//urc<72><63><EFBFBD>ռ<EFBFBD><D5BC><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD>ռ<EFBFBD><D5BC><EFBFBD><EFBFBD><EFBFBD>
unsigned short urc_cnt, rcv_cnt;
unsigned char suspend: 1;
2020-06-13 12:24:28 +08:00
}at_core_t;
typedef struct {
void (*sender)(at_env_t *e); /*<2A>Զ<EFBFBD><D4B6><EFBFBD><E5B7A2><EFBFBD><EFBFBD> */
const char *matcher; /*<2A><><EFBFBD><EFBFBD>ƥ<EFBFBD>䴮 */
at_callback_t cb; /*<2A><>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD> */
unsigned char retry; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD> */
unsigned short timeout; /*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱʱ<CAB1><CAB1> */
}at_cmd_t;
void at_core_init(at_core_t *ac, const at_core_conf_t cfg);
/*<2A><><EFBFBD>͵<EFBFBD><CDB5><EFBFBD>AT<41><54><EFBFBD><EFBFBD>*/
bool at_send_singlline(at_core_t *ac, at_callback_t cb, const char *singlline);
/*<2A><><EFBFBD>Ͷ<EFBFBD><CDB6><EFBFBD>AT<41><54><EFBFBD><EFBFBD>*/
bool at_send_multiline(at_core_t *ac, at_callback_t cb, const char **multiline);
/*ִ<><D6B4>AT<41><54><EFBFBD><EFBFBD>*/
bool at_do_cmd(at_core_t *ac, void *params, const at_cmd_t *cmd);
/*<2A>Զ<EFBFBD><D4B6><EFBFBD>AT<41><54>ҵ*/
bool at_do_work(at_core_t *ac, int (*work)(at_env_t *e), void *params);
void at_item_abort(at_item_t *it); /*<2A><>ֹ<EFBFBD><D6B9>ǰ<EFBFBD><C7B0>ҵ*/
bool at_core_busy(at_core_t *ac);
void at_suspend(at_core_t *ac);
void at_resume(at_core_t *ac);
void at_poll_task(at_core_t *ac);
#endif