Firmware SDK
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
bc_esp8266.h
1 #ifndef _BC_ESP8266_H
2 #define _BC_ESP8266_H
3 
4 #include <bc_scheduler.h>
5 #include <bc_gpio.h>
6 #include <bc_uart.h>
7 
11 
13 
14 #define BC_ESP8266_TX_MAX_PACKET_SIZE 1024
15 
16 #define BC_ESP8266_TX_FIFO_BUFFER_SIZE BC_ESP8266_TX_MAX_PACKET_SIZE
17 #define BC_ESP8266_RX_FIFO_BUFFER_SIZE 2048
18 
20 
22 
23 typedef enum
24 {
27 
30 
31  BC_ESP8266_EVENT_WIFI_CONNECT_SUCCESS = 2,
32  BC_ESP8266_EVENT_WIFI_CONNECT_ERROR = 3,
33  BC_ESP8266_EVENT_SOCKET_CONNECT_SUCCESS = 4,
34  BC_ESP8266_EVENT_SOCKET_CONNECT_ERROR = 5,
35  BC_ESP8266_EVENT_SOCKET_SEND_SUCCESS = 6,
36  BC_ESP8266_EVENT_SOCKET_SEND_ERROR = 7,
37  BC_ESP8266_EVENT_DATA_RECEIVED = 8
38 
40 
42 
43 typedef struct bc_esp8266_t bc_esp8266_t;
44 
45 typedef enum
46 {
47  BC_ESP8266_STATE_READY = 0,
48  BC_ESP8266_STATE_ERROR = 1,
49  BC_ESP8266_STATE_INITIALIZE = 2,
50  BC_ESP8266_STATE_IDLE = 3,
51  BC_ESP8266_STATE_RECEIVE = 4,
52  BC_ESP8266_STATE_INITIALIZE_COMMAND_SEND = 5,
53  BC_ESP8266_STATE_INITIALIZE_COMMAND_RESPONSE = 6,
54  BC_ESP8266_STATE_WIFI_CONNECT_COMMAND = 7,
55  BC_ESP8266_STATE_SOCKET_CONNECT_COMMAND = 8,
56  BC_ESP8266_STATE_SOCKET_SEND_COMMAND = 9,
57  BC_ESP8266_STATE_SOCKET_SEND_DATA = 10,
58  BC_ESP8266_STATE_WIFI_CONNECT_RESPONSE = 11,
59  BC_ESP8266_STATE_SOCKET_CONNECT_RESPONSE = 12,
60  BC_ESP8266_STATE_SOCKET_SEND_RESPONSE = 13,
61  BC_ESP8266_STATE_WIFI_CONNECT_ERROR = 14,
62  BC_ESP8266_STATE_SOCKET_CONNECT_ERROR = 15,
63  BC_ESP8266_STATE_SOCKET_SEND_ERROR = 16,
64  BC_ESP8266_STATE_SOCKET_RECEIVE = 17,
65  BC_ESP8266_STATE_DISCONNECTED = 18,
66  BC_ESP8266_STATE_SNTP_CONFIG_COMMAND = 19,
67  BC_ESP8266_STATE_SNTP_CONFIG_RESPONSE = 20,
68  BC_ESP8266_STATE_SNTP_TIME_COMMAND = 21,
69  BC_ESP8266_STATE_SNTP_TIME_RESPONSE = 22
70 } bc_esp8266_state_t;
71 
72 typedef enum
73 {
74  BC_ESP8266_CONFIG_MODE_STATION = 0,
75  BC_ESP8266_CONFIG_MODE_AP = 1
76 } bc_esp8266_config_mode_t;
77 
78 typedef struct
79 {
80  bc_esp8266_config_mode_t mode;
81  char ssid[64];
82  char password[64];
83  uint8_t sntp_enabled;
84  int sntp_timezone;
85  char sntp_server1[128];
86  char sntp_server2[128];
87  char sntp_server3[128];
89 
91 {
92  bc_scheduler_task_id_t _task_id;
93  bc_uart_channel_t _uart_channel;
94  bc_esp8266_state_t _state;
95  bc_fifo_t _tx_fifo;
96  bc_fifo_t _rx_fifo;
97  uint8_t _tx_fifo_buffer[BC_ESP8266_TX_FIFO_BUFFER_SIZE];
98  uint8_t _rx_fifo_buffer[BC_ESP8266_RX_FIFO_BUFFER_SIZE];
99  void (*_event_handler)(bc_esp8266_t *, bc_esp8266_event_t, void *);
100  void *_event_param;
101  char _command[BC_ESP8266_TX_FIFO_BUFFER_SIZE];
102  char _response[BC_ESP8266_RX_FIFO_BUFFER_SIZE];
103  uint8_t _message_buffer[BC_ESP8266_TX_MAX_PACKET_SIZE];
104  size_t _message_length;
105  size_t _message_part_length;
106  uint8_t _init_command_index;
107  uint8_t _timeout_cnt;
108  bc_esp8266_config _config;
109 };
110 
112 
116 
117 void bc_esp8266_init(bc_esp8266_t *self, bc_uart_channel_t uart_channel);
118 
123 
124 void bc_esp8266_set_event_handler(bc_esp8266_t *self, void (*event_handler)(bc_esp8266_t *, bc_esp8266_event_t, void *), void *event_param);
125 
130 
131 void bc_esp8266_set_station_mode(bc_esp8266_t *self, char *ssid, char *password);
132 
136 
137 void bc_esp8266_set_sntp(bc_esp8266_t *self, int timezone);
138 
145 
146 void bc_esp8266_set_sntp_with_servers(bc_esp8266_t *self, int timezone, char *sntp_server1, char *sntp_server2, char *sntp_server3);
147 
152 
154 
159 
160 bool bc_esp8266_connect(bc_esp8266_t *self);
161 
166 
168 
175 
176 bool bc_esp8266_tcp_connect(bc_esp8266_t *self, const char *host, uint16_t port);
177 
184 
185 bool bc_esp8266_udp_connect(bc_esp8266_t *self, const char *host, uint16_t port);
186 
193 
194 bool bc_esp8266_ssl_connect(bc_esp8266_t *self, const char *host, uint16_t port);
195 
202 
203 bool bc_esp8266_send_data(bc_esp8266_t *self, const void *buffer, size_t length);
204 
208 
210 
216 
217 uint32_t bc_esp8266_get_received_message_data(bc_esp8266_t *self, uint8_t *buffer, uint32_t buffer_size);
218 
220 
221 #endif // _BC_ESP8266_H
void bc_esp8266_set_station_mode(bc_esp8266_t *self, char *ssid, char *password)
Set station mode.
Definition: bc_esp8266.c:88
Ready event.
Definition: bc_esp8266.h:26
bc_uart_channel_t
UART channels.
Definition: bc_uart.h:13
Structure of FIFO instance.
Definition: bc_fifo.h:12
bool bc_esp8266_disconnect(bc_esp8266_t *self)
Disable ESP8266 and disconnect.
Definition: bc_esp8266.c:158
bc_esp8266_event_t
Callback events.
Definition: bc_esp8266.h:23
Error event.
Definition: bc_esp8266.h:29
uint32_t bc_esp8266_get_received_message_length(bc_esp8266_t *self)
Get length of the received message.
Definition: bc_esp8266.c:678
uint32_t bc_esp8266_get_received_message_data(bc_esp8266_t *self, uint8_t *buffer, uint32_t buffer_size)
Get received message data.
Definition: bc_esp8266.c:683
void bc_esp8266_set_event_handler(bc_esp8266_t *self, void(*event_handler)(bc_esp8266_t *, bc_esp8266_event_t, void *), void *event_param)
Set callback function.
Definition: bc_esp8266.c:82
bool bc_esp8266_udp_connect(bc_esp8266_t *self, const char *host, uint16_t port)
Establish UDP Connection.
Definition: bc_esp8266.c:195
bool bc_esp8266_ssl_connect(bc_esp8266_t *self, const char *host, uint16_t port)
Establish SSL Connection.
Definition: bc_esp8266.c:200
bool bc_esp8266_send_data(bc_esp8266_t *self, const void *buffer, size_t length)
Send data.
Definition: bc_esp8266.c:205
size_t bc_scheduler_task_id_t
Task ID assigned by scheduler.
Definition: bc_scheduler.h:18
void bc_esp8266_set_sntp(bc_esp8266_t *self, int timezone)
Enable SNTP and set time zone.
Definition: bc_esp8266.c:95
bool bc_esp8266_connect(bc_esp8266_t *self)
Enable ESP8266 and connect to WiFi.
Definition: bc_esp8266.c:140
void bc_esp8266_init(bc_esp8266_t *self, bc_uart_channel_t uart_channel)
Initialize ESP8266.
Definition: bc_esp8266.c:37
bool bc_esp8266_tcp_connect(bc_esp8266_t *self, const char *host, uint16_t port)
Establish TCP Connection.
Definition: bc_esp8266.c:190
void bc_esp8266_set_sntp_with_servers(bc_esp8266_t *self, int timezone, char *sntp_server1, char *sntp_server2, char *sntp_server3)
Enable SNTP and set configuration.
Definition: bc_esp8266.c:100
bool bc_esp8266_is_ready(bc_esp8266_t *self)
Check if modem is ready for commands.
Definition: bc_esp8266.c:130