4242*/
4343#define AT_ADD_POSTFIX
4444
45- #define AT_PREFIX "AT"
46- #define AT_POSTFIX "\r\n"
45+ #define AT_PREFIX "AT"
46+ #define AT_POSTFIX "\r\n"
4747
48- #define AT_TIMENOW () OSP_GET_CUR_MS()
48+ #define AT_TIMENOW () OSP_GET_CUR_MS()
4949
5050#define AT_MAX_LEN 128
5151#define AT_MAX_PARAMETER 8
5252
5353
54- int32_t at_parser_init (AT_PARSER_DEF_PTR obj , uint32_t baudrate ){
54+ int32_t at_parser_init (AT_PARSER_DEF_PTR obj , uint32_t baudrate )
55+ {
5556 obj -> psio = ez_sio_open (obj -> uart_id , baudrate , AT_TX_BUFSIZE , AT_RX_BUFSIZE );
5657 dbg_printf (DBG_MORE_INFO , "[%s]%d: obj->psio 0x%x -> 0x%x\r\n" , __FUNCTION__ , __LINE__ , obj -> psio , * obj -> psio );
5758 return (obj -> psio != NULL ) ? AT_OK : AT_ERROR ;
5859}
5960
60- void at_parser_deinit (AT_PARSER_DEF_PTR obj ){
61+ void at_parser_deinit (AT_PARSER_DEF_PTR obj )
62+ {
6163 ez_sio_close (obj -> psio );
6264 return ;
6365}
@@ -75,42 +77,54 @@ int32_t at_write(AT_PARSER_DEF_PTR obj, char *buf, uint32_t cnt)
7577/*
7678* please use NULL as last parameter
7779*/
78- int32_t at_send_cmd (AT_PARSER_DEF_PTR obj , AT_MODE mode , AT_STRING command , ...){
80+ int32_t at_send_cmd (AT_PARSER_DEF_PTR obj , AT_MODE mode , AT_STRING command , ...)
81+ {
7982 va_list vl ;
8083 char at_out [AT_MAX_LEN ] = AT_PREFIX ;
81- char * str = command ;
82- if (str == NULL ){
84+ char * str = command ;
85+
86+ if (str == NULL ) {
8387 dbg_printf (DBG_MORE_INFO , "[%s]%d: command is NULL, send AT test command\r\n" , __FUNCTION__ , __LINE__ );
8488 } else {
8589 strcat (at_out ,"+" );
8690 strcat (at_out , command );
87- switch (mode ){
88- case AT_LIST :
89- strcat (at_out , "=?" );
90- break ;
91- case AT_READ :
92- strcat (at_out , "?" );
93- break ;
94- case AT_WRITE :
95- strcat (at_out , "=" );
96- va_start (vl , command );
97- for (int i = 0 ; i < AT_MAX_PARAMETER ; i ++ ){
98- str = va_arg (vl , AT_STRING );
99- if (str == NULL ){
100- break ;
101- }
102- if (i != 0 ){
103- strcat (at_out , "," );
104- }
105- strcat (at_out , str );
91+
92+ switch (mode ) {
93+ case AT_LIST :
94+ strcat (at_out , "=?" );
95+ break ;
96+
97+ case AT_READ :
98+ strcat (at_out , "?" );
99+ break ;
100+
101+ case AT_WRITE :
102+ strcat (at_out , "=" );
103+ va_start (vl , command );
104+
105+ for (int i = 0 ; i < AT_MAX_PARAMETER ; i ++ ) {
106+ str = va_arg (vl , AT_STRING );
107+
108+ if (str == NULL ) {
109+ break ;
110+ }
111+
112+ if (i != 0 ) {
113+ strcat (at_out , "," );
106114 }
107- va_end (vl );
108- break ;
109- case AT_EXECUTE :
110- default :
111- break ;
115+
116+ strcat (at_out , str );
117+ }
118+
119+ va_end (vl );
120+ break ;
121+
122+ case AT_EXECUTE :
123+ default :
124+ break ;
112125 }
113126 }
127+
114128#ifdef AT_ADD_POSTFIX
115129 strcat (at_out , AT_POSTFIX );
116130#endif /*AT_ADD_POSTFIX*/
@@ -125,23 +139,29 @@ int32_t at_get_reply(AT_PARSER_DEF_PTR obj, char *buf, uint32_t timeout)
125139 uint32_t read_cnt ;
126140 uint32_t cur_time ;
127141 cur_time = AT_TIMENOW ();
142+
128143 do {
129144 read_cnt = at_read (obj , & buf [cur_ofs ], 1 );
130145 cur_ofs += read_cnt ;
131146 buf [cur_ofs ] = '\0' ;
132- if ((strstr (buf , AT_OK_STR )!= NULL ) || (strstr (buf , AT_ERROR_STR )!= NULL )){
147+
148+ if ((strstr (buf , AT_OK_STR )!= NULL ) || (strstr (buf , AT_ERROR_STR )!= NULL )) {
133149 break ;
134150 }
135- } while ((AT_TIMENOW ()- cur_time ) < timeout );
151+ } while ((AT_TIMENOW ()- cur_time ) < timeout );
152+
136153 buf [cur_ofs ] = '\0' ;
137154 dbg_printf (DBG_LESS_INFO , "[%s]%d: \"%s\" (%d)\r\n" , __FUNCTION__ , __LINE__ , buf , strlen (buf ));
138- if (strstr (buf , AT_OK_STR )!= NULL ){
155+
156+ if (strstr (buf , AT_OK_STR )!= NULL ) {
139157 return AT_OK ;
140158 }
159+
141160 return AT_ERROR ;
142161}
143162
144- int32_t at_test (AT_PARSER_DEF_PTR obj ){
163+ int32_t at_test (AT_PARSER_DEF_PTR obj )
164+ {
145165 char rcv_buf [64 ];
146166 at_send_cmd (obj , AT_LIST , NULL );
147167 return at_get_reply (obj , rcv_buf , AT_NORMAL_TIMEOUT );
0 commit comments