-
Notifications
You must be signed in to change notification settings - Fork 7
/
user_main.c
179 lines (156 loc) · 5.74 KB
/
user_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
/*
* ESPRSSIF MIT License
*
* Copyright (c) 2015 <ESPRESSIF SYSTEMS (SHANGHAI) PTE LTD>
*
* Permission is hereby granted for use on ESPRESSIF SYSTEMS ESP8266 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 "esp_common.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include "lwip/opt.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "openssl/ssl.h"
#include "iothub_client_sample_mqtt.h"
#include "lwip/apps/sntp.h"
#include "lwip/apps/sntp_time.h"
#include "uart.h"
#define OPENSSL_DEMO_THREAD_NAME "ssl_demo"
#define OPENSSL_DEMO_THREAD_STACK_WORDS 1024*2
#define OPENSSL_DEMO_THREAD_PRORIOTY 6
static os_timer_t timer;
LOCAL void ICACHE_FLASH_ATTR mqtt_sample()
{
while (1) {
struct ip_info info;
wifi_get_ip_info(STATION_IF, &info);
if (info.ip.addr != 0) {
break;
} else {
vTaskDelay(10);
}
}
iothub_client_sample_mqtt_run();
vTaskDelete(NULL);
}
LOCAL void ICACHE_FLASH_ATTR wait_for_connection_ready(uint8 flag)
{
unsigned char ret = 0;
struct ip_info ipconfig;
xTaskHandle openssl_handle;
os_timer_disarm(&timer);
ret = wifi_station_get_connect_status();
os_printf("ret %d\n", ret);
if(ret == STATION_GOT_IP){
printf("azure iot program starts %d\n", system_get_free_heap_size());
int result;
result = xTaskCreate(mqtt_sample,
OPENSSL_DEMO_THREAD_NAME,
OPENSSL_DEMO_THREAD_STACK_WORDS,
NULL,
OPENSSL_DEMO_THREAD_PRORIOTY,
&openssl_handle);
if (result != pdPASS){
os_printf("create thread %s failed\n", OPENSSL_DEMO_THREAD_NAME);
return ;
}
}else{
os_timer_disarm(&timer);
os_timer_setfn(&timer, (os_timer_func_t *)wait_for_connection_ready, NULL);
os_timer_arm(&timer, 2000, 0);
}
}
LOCAL void ICACHE_FLASH_ATTR configWiFi()
{
uint8 ssid[] = {'s','s','i','d'};
uint8 password[] = {'p','a','s','s','w','o','r','d'};
struct station_config sta_conf;
wifi_set_opmode(STATION_MODE);
memset(sta_conf.ssid, 0, 32);
memset(sta_conf.password, 0, 64);
memset(sta_conf.bssid, 0, 6);
memcpy(sta_conf.ssid, ssid, sizeof(ssid));
memcpy(sta_conf.password, password, sizeof(password));
sta_conf.bssid_set = 0;
wifi_station_set_config(&sta_conf);
os_timer_disarm(&timer);
os_timer_setfn(&timer, (os_timer_func_t *)wait_for_connection_ready, NULL);
os_timer_arm(&timer, 2000, 0);
}
/******************************************************************************
* FunctionName : user_rf_cal_sector_set
* Description : SDK just reversed 4 sectors, used for rf init data and paramters.
* We add this function to force users to set rf cal sector, since
* we don't know which sector is free in user's application.
* sector map for last several sectors : ABCCC
* A : rf cal
* B : rf init data
* C : sdk parameters
* Parameters : none
* Returns : rf cal sector
*******************************************************************************/
uint32 user_rf_cal_sector_set(void)
{
flash_size_map size_map = system_get_flash_size_map();
uint32 rf_cal_sec = 0;
switch (size_map) {
case FLASH_SIZE_4M_MAP_256_256:
rf_cal_sec = 128 - 5;
break;
case FLASH_SIZE_8M_MAP_512_512:
rf_cal_sec = 256 - 5;
break;
case FLASH_SIZE_16M_MAP_512_512:
case FLASH_SIZE_16M_MAP_1024_1024:
rf_cal_sec = 512 - 5;
break;
case FLASH_SIZE_32M_MAP_512_512:
case FLASH_SIZE_32M_MAP_1024_1024:
rf_cal_sec = 1024 - 5;
break;
default:
rf_cal_sec = 0;
break;
}
return rf_cal_sec;
}
/******************************************************************************
* FunctionName : user_init
* Description : entry of user application, init user function here
* Parameters : none
* Returns : none
*******************************************************************************/
void user_init(void)
{
printf("SDK version:%s\n", system_get_sdk_version());
//mac host doesn't like 74880. uart_init doesn't work, use uart_div_modify
uart_div_modify(0, UART_CLK_FREQ / 115200);
//If you need to use a different UART port:
// os_install_putc1((void *)uart1_write_char);
// uart_div_modify(1, UART_CLK_FREQ / 115200);
//printf("user_init: %d\n", system_get_free_heap_size());
//set system time
//set_time();
//lwip_connection_test();
configWiFi();
//iothub_client_sample_amqp_run();
}