-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathapp_main.c
364 lines (302 loc) · 9.82 KB
/
app_main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
* ESPRESSIF MIT License
*
* Copyright (c) 2017 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP32 only, in which case,
* it is free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished
* to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "nvs_flash.h"
#ifdef CONFIG_AT_WIFI_COMMAND_SUPPORT
#include "esp_event_loop.h"
#include "esp_wifi.h"
#endif
#if defined(CONFIG_BT_ENABLED)
#include "esp_bt.h"
#endif
#include "esp_at.h"
#ifdef CONFIG_AT_OTA_SUPPORT
#include "at_upgrade.h"
#endif
#include "at_interface.h"
#include "at_default_config.h"
#ifdef CONFIG_AT_ETHERNET_SUPPORT
#include "at_eth_init.h"
#endif
#ifdef CONFIG_AT_OTA_SUPPORT
static uint8_t at_exeCmdCipupdate(uint8_t *cmd_name)//add get station ip and ap ip
{
if (esp_at_upgrade_process(ESP_AT_OTA_MODE_NORMAL,NULL)) {
esp_at_response_result(ESP_AT_RESULT_CODE_OK);
esp_at_port_wait_write_complete(portMAX_DELAY);
esp_restart();
for(;;){
}
}
return ESP_AT_RESULT_CODE_ERROR;
}
static uint8_t at_setupCmdCipupdate(uint8_t para_num)
{
int32_t ota_mode = 0;
int32_t cnt = 0;
uint8_t* version = NULL;
if (esp_at_get_para_as_digit (cnt++,&ota_mode) != ESP_AT_PARA_PARSE_RESULT_OK) {
return ESP_AT_RESULT_CODE_ERROR;
}
if (cnt < para_num) {
if(esp_at_get_para_as_str (cnt++,&version) != ESP_AT_PARA_PARSE_RESULT_OK) {
return ESP_AT_RESULT_CODE_ERROR;
}
}
if (cnt != para_num) {
return ESP_AT_RESULT_CODE_ERROR;
}
if (esp_at_upgrade_process(ota_mode,version)) {
esp_at_response_result(ESP_AT_RESULT_CODE_OK);
esp_at_port_wait_write_complete(portMAX_DELAY);
esp_restart();
for(;;){
}
}
return ESP_AT_RESULT_CODE_ERROR;
}
static esp_at_cmd_struct at_update_cmd[] = {
{"+CIUPDATE", NULL, NULL, at_setupCmdCipupdate, at_exeCmdCipupdate},
};
#endif
#ifdef CONFIG_AT_WIFI_COMMAND_SUPPORT
static esp_err_t at_wifi_event_handler(void *ctx, system_event_t *event)
{
esp_err_t ret = esp_at_wifi_event_handler(ctx, event);
return ret;
}
static void initialise_wifi(void)
{
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_event_loop_init(at_wifi_event_handler, NULL) );
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
ESP_ERROR_CHECK( esp_wifi_start() );
}
#endif
#include "driver/uart.h"
#include "at_webai_cmd.h"
#include "soft_urat.h"
void app_main()
{
const esp_partition_t * partition = esp_at_custom_partition_find(0x40, 0xff, "factory_param");
char* data = NULL;
uint32_t module_id = 0;
if (partition) {
data = (char*)malloc(ESP_AT_FACTORY_PARAMETER_SIZE); // Notes
assert(data != NULL);
if(esp_partition_read(partition, 0, data, ESP_AT_FACTORY_PARAMETER_SIZE) != ESP_OK){
free(data);
printf("esp_partition_read failed\r\n");
return ;
} else {
module_id = data[3];
free(data);
}
} else {
printf("factory_parameter partition missed\r\n");
}
const char* module_name = esp_at_get_module_name_by_id(module_id);
uint8_t *version = (uint8_t *)malloc(256);
#ifdef CONFIG_AT_COMMAND_TERMINATOR
uint8_t cmd_terminator[2] = {CONFIG_AT_COMMAND_TERMINATOR,0};
#endif
nvs_flash_init();
tcpip_adapter_init();
#ifdef CONFIG_AT_WIFI_COMMAND_SUPPORT
initialise_wifi();
#endif
at_interface_init();
sprintf((char*)version, "compile time(%s):%s %s\r\n", ESP_AT_PROJECT_COMMIT_ID, __DATE__, __TIME__);
#ifdef CONFIG_ESP_AT_FW_VERSION
if ((strlen(CONFIG_ESP_AT_FW_VERSION) > 0) && (strlen(CONFIG_ESP_AT_FW_VERSION) <= 128)){
printf("%s\r\n", CONFIG_ESP_AT_FW_VERSION);
sprintf((char*)version + strlen((char*)version),"Bin version:%s(%s)\r\n", CONFIG_ESP_AT_FW_VERSION, module_name);
}
#endif
esp_at_module_init (CONFIG_AT_SOCKET_MAX_CONN_NUM, version); // reserved one for server
free(version);
esp_at_factory_parameter_init();
#ifdef CONFIG_AT_BASE_COMMAND_SUPPORT
if(esp_at_base_cmd_regist() == false) {
printf("regist base cmd fail\r\n");
}
#endif
#ifdef CONFIG_AT_WIFI_COMMAND_SUPPORT
if(esp_at_wifi_cmd_regist() == false) {
printf("regist wifi cmd fail\r\n");
}
#endif
#ifdef CONFIG_AT_NET_COMMAND_SUPPORT
if(esp_at_net_cmd_regist() == false) {
printf("regist net cmd fail\r\n");
}
#endif
#ifdef CONFIG_AT_MQTT_COMMAND_SUPPORT
if(esp_at_mqtt_cmd_regist() == false) {
printf("regist mqtt cmd fail\r\n");
}
#endif
#ifdef CONFIG_AT_HTTP_COMMAND_SUPPORT
if(esp_at_http_cmd_regist() == false) {
printf("regist http cmd fail\r\n");
}
#endif
#ifdef CONFIG_AT_BLE_COMMAND_SUPPORT
if(esp_at_ble_cmd_regist() == false) {
printf("regist ble cmd fail\r\n");
}
#endif
#ifdef CONFIG_AT_BLE_HID_COMMAND_SUPPORT
if(esp_at_ble_hid_cmd_regist() == false) {
printf("regist ble hid cmd fail\r\n");
}
#endif
#ifdef CONFIG_AT_BLUFI_COMMAND_SUPPORT
if(esp_at_blufi_cmd_regist() == false) {
printf("regist blufi cmd fail\r\n");
}
#endif
#ifdef CONFIG_AT_BT_COMMAND_SUPPORT
if(esp_at_bt_cmd_regist() == false) {
printf("regist bt cmd fail\r\n");
}
#ifdef CONFIG_AT_BT_SPP_COMMAND_SUPPORT
if(esp_at_bt_spp_cmd_regist() == false) {
printf("regist bt spp cmd fail\r\n");
}
#endif
#ifdef CONFIG_AT_BT_A2DP_COMMAND_SUPPORT
if(esp_at_bt_a2dp_cmd_regist() == false) {
printf("regist bt a2dp cmd fail\r\n");
}
#endif
#endif
#if defined(CONFIG_BT_ENABLED)
#if defined(CONFIG_AT_BLE_COMMAND_SUPPORT) || defined(CONFIG_AT_BLE_HID_COMMAND_SUPPORT) || defined(CONFIG_AT_BLUFI_COMMAND_SUPPORT)
#if !defined(CONFIG_AT_BT_COMMAND_SUPPORT)
esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
#endif
#else
#if defined(CONFIG_AT_BT_COMMAND_SUPPORT)
esp_bt_controller_mem_release(ESP_BT_MODE_BLE);
#else
esp_bt_controller_mem_release(ESP_BT_MODE_BTDM);
#endif
#endif
#endif
#if defined(CONFIG_AT_MDNS_COMMAND_SUPPORT)
if(esp_at_mdns_cmd_regist() == false) {
printf("regist mdns cmd fail\r\n");
}
#endif
#if defined(CONFIG_AT_WPS_COMMAND_SUPPORT)
if(esp_at_wps_cmd_regist() == false) {
printf("regist wps cmd fail\r\n");
}
#endif
#if defined(CONFIG_AT_SMARTCONFIG_COMMAND_SUPPORT)
if(esp_at_smartconfig_cmd_regist() == false) {
printf("regist smartconfig cmd fail\r\n");
}
#endif
#if defined(CONFIG_AT_PING_COMMAND_SUPPORT)
if(esp_at_ping_cmd_regist() == false) {
printf("regist ping cmd fail\r\n");
}
#endif
#ifdef CONFIG_AT_ETHERNET_SUPPORT
if(at_eth_init() == false) {
printf("ethernet init fail\r\n");
}else {
if(esp_at_eth_cmd_regist() == false) {
printf("regist ethernet cmd fail\r\n");
}
}
#endif
#ifdef CONFIG_AT_FS_COMMAND_SUPPORT
if(esp_at_fs_cmd_regist() == false) {
printf("regist fs cmd fail\r\n");
}
#endif
#ifdef CONFIG_AT_EAP_COMMAND_SUPPORT
if(esp_at_eap_cmd_regist() == false) {
printf("regist eap cmd fail\r\n");
}
#endif
#ifdef CONFIG_AT_COMMAND_TERMINATOR
esp_at_custom_cmd_line_terminator_set((uint8_t*)&cmd_terminator);
#endif
#ifdef CONFIG_AT_OTA_SUPPORT
esp_at_custom_cmd_array_regist (at_update_cmd, sizeof(at_update_cmd)/sizeof(at_update_cmd[0]));
#endif
at_setupWebAICmd();
at_custom_init();
SwSerial *tmp = sw_new(14, 12, true, 512);
printf("%u\n", tmp->bitTime);
if (tmp != NULL)
{
sw_open(tmp, 57600); // 115200 > 9600
// puts("sw_write test");
// for (size_t i = 0; i < 256; i++)
// {
// sw_write(tmp, (uint8_t)i);
// }
puts("sw_write test");
for (int i = 0; i <= 0xFF; i++)
{
sw_write(tmp, (uint8_t)i);
}
// vTaskDelay(500 / portTICK_RATE_MS);
// sw_write(tmp, (uint8_t)0x00);
// vTaskDelay(100 / portTICK_RATE_MS);
// sw_write(tmp, (uint8_t)0x01);
// vTaskDelay(100 / portTICK_RATE_MS);
// sw_write(tmp, (uint8_t)0x02);
// vTaskDelay(100 / portTICK_RATE_MS);
// sw_write(tmp, (uint8_t)0x03);
// vTaskDelay(100 / portTICK_RATE_MS);
while (true)
{
vTaskDelay(1000 / portTICK_RATE_MS);
puts("check recvd data");
int len = sw_any(tmp);
if (len > 0)
{
for (size_t i = 0; i < len; i++)
{
uint8_t data = sw_read(tmp);
sw_write(tmp, (uint8_t)data);
printf("%02X ", data);
}
printf("\nrecv sw_any %02u %02u %02u \n", sw_any(tmp), tmp->inPos, tmp->outPos);
}
}
}
sw_del(tmp);
}