38
38
#include "esp-idf/components/esp_wifi/include/esp_wifi.h"
39
39
#include "esp-idf/components/lwip/include/apps/ping/ping_sock.h"
40
40
41
- #include "esp_log.h"
42
- static const char * TAG = "cp radio" ;
43
-
44
41
static void start_station (wifi_radio_obj_t * self ) {
45
- if (self -> sta_mode ) {
46
- return ;
47
- }
48
- wifi_mode_t next_mode ;
49
- if (self -> ap_mode ) {
50
- next_mode = WIFI_MODE_APSTA ;
51
- } else {
52
- next_mode = WIFI_MODE_STA ;
53
- }
54
- esp_wifi_set_mode (next_mode );
55
-
56
- esp_wifi_set_config (WIFI_MODE_STA , & self -> sta_config );
42
+ if (self -> sta_mode ) {
43
+ return ;
44
+ }
45
+ wifi_mode_t next_mode ;
46
+ if (self -> ap_mode ) {
47
+ next_mode = WIFI_MODE_APSTA ;
48
+ } else {
49
+ next_mode = WIFI_MODE_STA ;
50
+ }
51
+ esp_wifi_set_mode (next_mode );
52
+
53
+ esp_wifi_set_config (WIFI_MODE_STA , & self -> sta_config );
57
54
}
58
55
59
56
bool common_hal_wifi_radio_get_enabled (wifi_radio_obj_t * self ) {
60
- return self -> started ;
57
+ return self -> started ;
61
58
}
62
59
63
60
void common_hal_wifi_radio_set_enabled (wifi_radio_obj_t * self , bool enabled ) {
64
- if (self -> started && !enabled ) {
65
- ESP_LOGI (TAG , "stop" );
66
- if (self -> current_scan != NULL ) {
67
- common_hal_wifi_radio_stop_scanning_networks (self );
68
- }
69
- ESP_ERROR_CHECK (esp_wifi_stop ());
70
- self -> started = false;
71
- return ;
72
- }
73
- if (!self -> started && enabled ) {
74
- ESP_LOGI (TAG , "start" );
75
- ESP_ERROR_CHECK (esp_wifi_start ());
76
- self -> started = true;
77
- return ;
78
- }
61
+ if (self -> started && !enabled ) {
62
+ if (self -> current_scan != NULL ) {
63
+ common_hal_wifi_radio_stop_scanning_networks (self );
64
+ }
65
+ ESP_ERROR_CHECK (esp_wifi_stop ());
66
+ self -> started = false;
67
+ return ;
68
+ }
69
+ if (!self -> started && enabled ) {
70
+ ESP_ERROR_CHECK (esp_wifi_start ());
71
+ self -> started = true;
72
+ return ;
73
+ }
79
74
}
80
75
81
76
mp_obj_t common_hal_wifi_radio_get_mac_address (wifi_radio_obj_t * self ) {
82
- uint8_t mac [6 ];
83
- esp_wifi_get_mac (ESP_IF_WIFI_STA , mac );
84
- return mp_const_none ;
77
+ uint8_t mac [6 ];
78
+ esp_wifi_get_mac (ESP_IF_WIFI_STA , mac );
79
+ return mp_const_none ;
85
80
}
86
81
87
82
mp_obj_t common_hal_wifi_radio_start_scanning_networks (wifi_radio_obj_t * self ) {
88
- if (self -> current_scan != NULL ) {
89
- mp_raise_RuntimeError (translate ("Already scanning for wifi networks" ));
90
- }
91
- // check enabled
92
- start_station (self );
93
-
94
- ESP_LOGI (TAG , "start scan" );
95
- wifi_scannednetworks_obj_t * scan = m_new_obj (wifi_scannednetworks_obj_t );
96
- self -> current_scan = scan ;
97
- scan -> base .type = & wifi_scannednetworks_type ;
98
- scan -> start_channel = 1 ;
99
- scan -> end_channel = 11 ;
100
- scan -> radio_event_group = self -> event_group_handle ;
101
- wifi_scannednetworks_scan_next_channel (scan );
102
- return scan ;
83
+ if (self -> current_scan != NULL ) {
84
+ mp_raise_RuntimeError (translate ("Already scanning for wifi networks" ));
85
+ }
86
+ // check enabled
87
+ start_station (self );
88
+
89
+ wifi_scannednetworks_obj_t * scan = m_new_obj (wifi_scannednetworks_obj_t );
90
+ self -> current_scan = scan ;
91
+ scan -> base .type = & wifi_scannednetworks_type ;
92
+ scan -> start_channel = 1 ;
93
+ scan -> end_channel = 11 ;
94
+ scan -> radio_event_group = self -> event_group_handle ;
95
+ wifi_scannednetworks_scan_next_channel (scan );
96
+ return scan ;
103
97
}
104
98
105
99
void common_hal_wifi_radio_stop_scanning_networks (wifi_radio_obj_t * self ) {
106
- // Free the memory used to store the found aps.
107
- ESP_EARLY_LOGI (TAG , "stop scan" );
108
- wifi_scannednetworks_deinit (self -> current_scan );
109
- self -> current_scan = NULL ;
110
- ESP_EARLY_LOGI (TAG , "stop scan done" );
100
+ // Free the memory used to store the found aps.
101
+ wifi_scannednetworks_deinit (self -> current_scan );
102
+ self -> current_scan = NULL ;
111
103
}
112
104
113
105
bool common_hal_wifi_radio_connect (wifi_radio_obj_t * self , uint8_t * ssid , size_t ssid_len , uint8_t * password , size_t password_len , uint8_t channel , mp_float_t timeout ) {
114
- // check enabled
115
- wifi_config_t * config = & self -> sta_config ;
116
- memcpy (& config -> sta .ssid , ssid , ssid_len );
117
- config -> sta .ssid [ssid_len ] = 0 ;
118
- if (password_len > 0 ) {
119
- memcpy (& config -> sta .password , password , password_len );
120
- }
121
- config -> sta .password [password_len ] = 0 ;
122
- config -> sta .channel = channel ;
123
- ESP_EARLY_LOGI (TAG , "connecting to %s" , config -> sta .ssid );
124
- esp_err_t result = esp_wifi_set_config (ESP_IF_WIFI_STA , config );
125
- if (result != ESP_OK ) {
126
- ESP_EARLY_LOGI (TAG , "config fail %d" , result );
127
- }
128
- result = esp_wifi_connect ();
129
- if (result != ESP_OK ) {
130
- ESP_EARLY_LOGI (TAG , "connect fail %d" , result );
131
- }
132
-
133
- EventBits_t bits ;
106
+ // check enabled
107
+ wifi_config_t * config = & self -> sta_config ;
108
+ memcpy (& config -> sta .ssid , ssid , ssid_len );
109
+ config -> sta .ssid [ssid_len ] = 0 ;
110
+ if (password_len > 0 ) {
111
+ memcpy (& config -> sta .password , password , password_len );
112
+ }
113
+ config -> sta .password [password_len ] = 0 ;
114
+ config -> sta .channel = channel ;
115
+ esp_wifi_set_config (ESP_IF_WIFI_STA , config );
116
+ esp_wifi_connect ();
117
+
118
+ EventBits_t bits ;
134
119
do {
135
120
RUN_BACKGROUND_TASKS ;
136
121
bits = xEventGroupWaitBits (self -> event_group_handle ,
@@ -140,21 +125,18 @@ bool common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t* ssid, size_t
140
125
0 );
141
126
} while ((bits & (WIFI_CONNECTED_BIT | WIFI_DISCONNECTED_BIT )) == 0 && !mp_hal_is_interrupted ());
142
127
if ((bits & WIFI_DISCONNECTED_BIT ) != 0 ) {
143
- return false;
128
+ return false;
144
129
}
145
- return true;
130
+ return true;
146
131
}
147
132
148
133
mp_obj_t common_hal_wifi_radio_get_ipv4_address (wifi_radio_obj_t * self ) {
149
- if (!esp_netif_is_netif_up (self -> netif )) {
150
- return mp_const_none ;
151
- }
152
- esp_netif_ip_info_t ip_info ;
153
- esp_err_t result = esp_netif_get_ip_info (self -> netif , & ip_info );
154
- if (result != ESP_OK ) {
155
- ESP_EARLY_LOGI (TAG , "get ip fail %d" , result );
156
- }
157
- return common_hal_ipaddress_new_ipv4address (ip_info .ip .addr );
134
+ if (!esp_netif_is_netif_up (self -> netif )) {
135
+ return mp_const_none ;
136
+ }
137
+ esp_netif_ip_info_t ip_info ;
138
+ esp_netif_get_ip_info (self -> netif , & ip_info );
139
+ return common_hal_ipaddress_new_ipv4address (ip_info .ip .addr );
158
140
}
159
141
160
142
mp_int_t common_hal_wifi_radio_ping (wifi_radio_obj_t * self , mp_obj_t ip_address , mp_float_t timeout ) {
@@ -171,15 +153,15 @@ mp_int_t common_hal_wifi_radio_ping(wifi_radio_obj_t *self, mp_obj_t ip_address,
171
153
uint32_t received = 0 ;
172
154
uint32_t total_time_ms = 0 ;
173
155
while (received == 0 && total_time_ms < timeout_ms ) {
174
- RUN_BACKGROUND_TASKS ;
175
- esp_ping_get_profile (ping , ESP_PING_PROF_DURATION , & total_time_ms , sizeof (total_time_ms ));
176
- esp_ping_get_profile (ping , ESP_PING_PROF_REPLY , & received , sizeof (received ));
156
+ RUN_BACKGROUND_TASKS ;
157
+ esp_ping_get_profile (ping , ESP_PING_PROF_DURATION , & total_time_ms , sizeof (total_time_ms ));
158
+ esp_ping_get_profile (ping , ESP_PING_PROF_REPLY , & received , sizeof (received ));
177
159
}
178
- uint32_t elapsed_time = 0xffffffff ;
160
+ uint32_t elapsed_time = 0xffffffff ;
179
161
if (received > 0 ) {
180
- esp_ping_get_profile (ping , ESP_PING_PROF_TIMEGAP , & elapsed_time , sizeof (elapsed_time ));
162
+ esp_ping_get_profile (ping , ESP_PING_PROF_TIMEGAP , & elapsed_time , sizeof (elapsed_time ));
181
163
}
182
164
esp_ping_delete_session (ping );
183
165
184
- return elapsed_time ;
166
+ return elapsed_time ;
185
167
}
0 commit comments