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  BC_ESP8266_EVENT_AP_AVAILABILITY_RESULT = 9,
39  BC_ESP8266_EVENT_DISCONNECTED = 10
40 
42 
44 
45 typedef struct bc_esp8266_t bc_esp8266_t;
46 
47 typedef enum
48 {
49  BC_ESP8266_STATE_READY = 0,
50  BC_ESP8266_STATE_ERROR = 1,
51  BC_ESP8266_STATE_INITIALIZE = 2,
52  BC_ESP8266_STATE_IDLE = 3,
53  BC_ESP8266_STATE_RECEIVE = 4,
54  BC_ESP8266_STATE_INITIALIZE_COMMAND_SEND = 5,
55  BC_ESP8266_STATE_INITIALIZE_COMMAND_RESPONSE = 6,
56  BC_ESP8266_STATE_WIFI_CONNECT_COMMAND = 7,
57  BC_ESP8266_STATE_SOCKET_CONNECT_COMMAND = 8,
58  BC_ESP8266_STATE_SOCKET_SEND_COMMAND = 9,
59  BC_ESP8266_STATE_SOCKET_SEND_DATA = 10,
60  BC_ESP8266_STATE_WIFI_CONNECT_RESPONSE = 11,
61  BC_ESP8266_STATE_SOCKET_CONNECT_RESPONSE = 12,
62  BC_ESP8266_STATE_SOCKET_SEND_RESPONSE = 13,
63  BC_ESP8266_STATE_WIFI_CONNECT_ERROR = 14,
64  BC_ESP8266_STATE_SOCKET_CONNECT_ERROR = 15,
65  BC_ESP8266_STATE_SOCKET_SEND_ERROR = 16,
66  BC_ESP8266_STATE_SOCKET_RECEIVE = 17,
67  BC_ESP8266_STATE_DISCONNECTED = 18,
68  BC_ESP8266_STATE_SNTP_CONFIG_COMMAND = 19,
69  BC_ESP8266_STATE_SNTP_CONFIG_RESPONSE = 20,
70  BC_ESP8266_STATE_SNTP_TIME_COMMAND = 21,
71  BC_ESP8266_STATE_SNTP_TIME_RESPONSE = 22,
72  BC_ESP8266_STATE_AP_AVAILABILITY_OPT_COMMAND = 23,
73  BC_ESP8266_STATE_AP_AVAILABILITY_OPT_RESPONSE = 24,
74  BC_ESP8266_STATE_AP_AVAILABILITY_COMMAND = 25,
75  BC_ESP8266_STATE_AP_AVAILABILITY_RESPONSE = 26
76 
77 } bc_esp8266_state_t;
78 
79 typedef enum
80 {
81  BC_ESP8266_CONFIG_MODE_STATION = 0,
82  BC_ESP8266_CONFIG_MODE_AP = 1
83 } bc_esp8266_config_mode_t;
84 
85 typedef struct
86 {
87  bc_esp8266_config_mode_t mode;
88  char ssid[64];
89  char password[64];
90  uint8_t sntp_enabled;
91  int sntp_timezone;
92  char sntp_server1[128];
93  char sntp_server2[128];
94  char sntp_server3[128];
96 
98 {
99  bc_scheduler_task_id_t _task_id;
100  bc_uart_channel_t _uart_channel;
101  bc_esp8266_state_t _state;
102  bc_esp8266_state_t _state_after_init;
103  bc_fifo_t _tx_fifo;
104  bc_fifo_t _rx_fifo;
105  uint8_t _tx_fifo_buffer[BC_ESP8266_TX_FIFO_BUFFER_SIZE];
106  uint8_t _rx_fifo_buffer[BC_ESP8266_RX_FIFO_BUFFER_SIZE];
107  void (*_event_handler)(bc_esp8266_t *, bc_esp8266_event_t, void *);
108  void *_event_param;
109  char _command[BC_ESP8266_TX_FIFO_BUFFER_SIZE];
110  char _response[BC_ESP8266_RX_FIFO_BUFFER_SIZE];
111  uint8_t _message_buffer[BC_ESP8266_TX_MAX_PACKET_SIZE];
112  size_t _message_length;
113  size_t _message_part_length;
114  uint8_t _init_command_index;
115  uint8_t _timeout_cnt;
116  bc_esp8266_config _config;
117  bool _ap_available;
118  int _rssi;
119 };
120 
122 
126 
127 void bc_esp8266_init(bc_esp8266_t *self, bc_uart_channel_t uart_channel);
128 
133 
134 void bc_esp8266_set_event_handler(bc_esp8266_t *self, void (*event_handler)(bc_esp8266_t *, bc_esp8266_event_t, void *), void *event_param);
135 
140 
141 void bc_esp8266_set_station_mode(bc_esp8266_t *self, char *ssid, char *password);
142 
146 
147 void bc_esp8266_set_sntp(bc_esp8266_t *self, int timezone);
148 
155 
156 void bc_esp8266_set_sntp_with_servers(bc_esp8266_t *self, int timezone, char *sntp_server1, char *sntp_server2, char *sntp_server3);
157 
162 
164 
169 
170 bool bc_esp8266_connect(bc_esp8266_t *self);
171 
176 
178 
185 
186 bool bc_esp8266_tcp_connect(bc_esp8266_t *self, const char *host, uint16_t port);
187 
194 
195 bool bc_esp8266_udp_connect(bc_esp8266_t *self, const char *host, uint16_t port);
196 
203 
204 bool bc_esp8266_ssl_connect(bc_esp8266_t *self, const char *host, uint16_t port);
205 
212 
213 bool bc_esp8266_send_data(bc_esp8266_t *self, const void *buffer, size_t length);
214 
218 
220 
226 
227 uint32_t bc_esp8266_get_received_message_data(bc_esp8266_t *self, uint8_t *buffer, uint32_t buffer_size);
228 
233 
235 
240 
241 void bc_esp8266_get_ap_availability(bc_esp8266_t *self, bool *available, int *rssi);
242 
246 
247 void bc_esp8266_get_ssid(bc_esp8266_t *self, char *ssid);
248 
252 
253 void bc_esp8266_set_ssid(bc_esp8266_t *self, char *ssid);
254 
258 
259 void bc_esp8266_get_password(bc_esp8266_t *self, char *password);
260 
264 
265 void bc_esp8266_set_password(bc_esp8266_t *self, char *password);
266 
268 
269 #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:104
Ready event.
Definition: bc_esp8266.h:26
bc_uart_channel_t
UART channels.
Definition: bc_uart.h:13
void bc_esp8266_get_password(bc_esp8266_t *self, char *password)
Get password.
Definition: bc_esp8266.c:1012
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:174
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:790
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:795
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:98
void bc_esp8266_set_ssid(bc_esp8266_t *self, char *ssid)
Set SSID.
Definition: bc_esp8266.c:1007
bool bc_esp8266_udp_connect(bc_esp8266_t *self, const char *host, uint16_t port)
Establish UDP Connection.
Definition: bc_esp8266.c:215
bool bc_esp8266_ssl_connect(bc_esp8266_t *self, const char *host, uint16_t port)
Establish SSL Connection.
Definition: bc_esp8266.c:220
bool bc_esp8266_send_data(bc_esp8266_t *self, const void *buffer, size_t length)
Send data.
Definition: bc_esp8266.c:225
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:111
bool bc_esp8266_connect(bc_esp8266_t *self)
Enable ESP8266 and connect to WiFi.
Definition: bc_esp8266.c:156
void bc_esp8266_set_password(bc_esp8266_t *self, char *password)
Set password.
Definition: bc_esp8266.c:1017
void bc_esp8266_get_ap_availability(bc_esp8266_t *self, bool *available, int *rssi)
Get AP availability result.
Definition: bc_esp8266.c:996
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:210
bool bc_esp8266_check_ap_availability(bc_esp8266_t *self)
Check AP availability.
Definition: bc_esp8266.c:979
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:116
void bc_esp8266_get_ssid(bc_esp8266_t *self, char *ssid)
Get SSID.
Definition: bc_esp8266.c:1002
bool bc_esp8266_is_ready(bc_esp8266_t *self)
Check if modem is ready for commands.
Definition: bc_esp8266.c:146