Skip to content

Commit b2ccf68

Browse files
author
Wayne Ren
committed
example: update esp8266 httpserver example
* bug fixes * code cleanup Signed-off-by: Wayne Ren <[email protected]>
1 parent d17823c commit b2ccf68

File tree

5 files changed

+251
-189
lines changed

5 files changed

+251
-189
lines changed

example/freertos/esp8266_wifi/at_parser.c

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,24 @@
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);

example/freertos/esp8266_wifi/at_parser.h

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,42 +34,42 @@
3434
#include "ez_sio.h"
3535
#include "embARC_error.h"
3636

37-
#define AT_OK 0
38-
#define AT_ERROR -1
39-
#define AT_OK_STR "OK"
37+
#define AT_OK 0
38+
#define AT_ERROR -1
39+
#define AT_OK_STR "OK"
4040
#define AT_ERROR_STR "ERROR"
4141

4242
#define AT_RX_BUFSIZE 512
4343
#define AT_TX_BUFSIZE 128
4444

45-
#define AT_NORMAL_TIMEOUT 100
46-
#define AT_LONG_TIMEOUT 5000
47-
#define AT_EXTRA_TIMEOUT 20000
45+
#define AT_NORMAL_TIMEOUT 100
46+
#define AT_LONG_TIMEOUT 5000
47+
#define AT_EXTRA_TIMEOUT 20000
4848

4949
typedef enum {
5050
AT_LIST,
5151
AT_READ,
5252
AT_WRITE,
5353
AT_EXECUTE
54-
}AT_MODE;
54+
} AT_MODE;
5555

56-
typedef char * AT_STRING;
56+
typedef char *AT_STRING;
5757

58-
/** HM1X object type */
58+
/** AT_PARSER type */
5959
typedef struct {
6060
uint32_t uart_id;
6161
EZ_SIO *psio;
6262
} AT_PARSER_DEF, *AT_PARSER_DEF_PTR;
6363

6464
#define AT_PARSER_DEFINE(NAME, UART_ID) \
6565
AT_PARSER_DEF __ ## NAME = { \
66-
.uart_id = UART_ID, \
67-
.psio = NULL, \
68-
}; \
66+
.uart_id = UART_ID, \
67+
.psio = NULL, \
68+
}; \
6969
AT_PARSER_DEF_PTR NAME = &__ ## NAME
7070

7171
int32_t at_parser_init(AT_PARSER_DEF_PTR obj, uint32_t baudrate);
72-
void at_parser_deinit(AT_PARSER_DEF_PTR obj);
72+
void at_parser_deinit(AT_PARSER_DEF_PTR obj);
7373
int32_t at_read(AT_PARSER_DEF_PTR obj, char *buf, uint32_t cnt);
7474
int32_t at_write(AT_PARSER_DEF_PTR obj, char *buf, uint32_t cnt);
7575
int32_t at_send_cmd(AT_PARSER_DEF_PTR obj, AT_MODE mode, AT_STRING command, ...);

0 commit comments

Comments
 (0)