|
26 | 26 |
|
27 | 27 | #include "shared-bindings/wifi/Radio.h" |
28 | 28 |
|
| 29 | +#include <string.h> |
| 30 | + |
| 31 | +#include "lib/utils/interrupt_char.h" |
29 | 32 | #include "py/runtime.h" |
| 33 | +#include "shared-bindings/ipaddress/IPv4Address.h" |
30 | 34 | #include "shared-bindings/wifi/ScannedNetworks.h" |
31 | 35 |
|
32 | 36 | #include "esp-idf/components/esp_wifi/include/esp_wifi.h" |
@@ -103,16 +107,51 @@ void common_hal_wifi_radio_stop_scanning_networks(wifi_radio_obj_t *self) { |
103 | 107 | ESP_EARLY_LOGI(TAG, "stop scan done"); |
104 | 108 | } |
105 | 109 |
|
106 | | -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, mp_float_t timeout) { |
| 110 | +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) { |
107 | 111 | // check enabled |
108 | | - wifi_config_t config; |
109 | | - esp_err_t result = esp_wifi_set_config(ESP_IF_WIFI_STA, &config); |
| 112 | + wifi_config_t* config = &self->sta_config; |
| 113 | + memcpy(&config->sta.ssid, ssid, ssid_len); |
| 114 | + config->sta.ssid[ssid_len] = 0; |
| 115 | + if (password_len > 0) { |
| 116 | + memcpy(&config->sta.password, password, password_len); |
| 117 | + } |
| 118 | + config->sta.password[password_len] = 0; |
| 119 | + config->sta.channel = channel; |
| 120 | + ESP_EARLY_LOGI(TAG, "connecting to %s", config->sta.ssid); |
| 121 | + esp_err_t result = esp_wifi_set_config(ESP_IF_WIFI_STA, config); |
| 122 | + if (result != ESP_OK) { |
| 123 | + ESP_EARLY_LOGI(TAG, "config fail %d", result); |
| 124 | + } |
110 | 125 | result = esp_wifi_connect(); |
111 | | - return result == ESP_OK; |
| 126 | + if (result != ESP_OK) { |
| 127 | + ESP_EARLY_LOGI(TAG, "connect fail %d", result); |
| 128 | + } |
| 129 | + |
| 130 | + EventBits_t bits; |
| 131 | + do { |
| 132 | + RUN_BACKGROUND_TASKS; |
| 133 | + bits = xEventGroupWaitBits(self->event_group_handle, |
| 134 | + WIFI_CONNECTED_BIT | WIFI_DISCONNECTED_BIT, |
| 135 | + pdTRUE, |
| 136 | + pdTRUE, |
| 137 | + 0); |
| 138 | + } while ((bits & (WIFI_CONNECTED_BIT | WIFI_DISCONNECTED_BIT)) == 0 && !mp_hal_is_interrupted()); |
| 139 | + if ((bits & WIFI_DISCONNECTED_BIT) != 0) { |
| 140 | + return false; |
| 141 | + } |
| 142 | + return true; |
112 | 143 | } |
113 | 144 |
|
114 | | -mp_obj_t common_hal_wifi_radio_get_ip_address(wifi_radio_obj_t *self) { |
115 | | - return mp_const_none; |
| 145 | +mp_obj_t common_hal_wifi_radio_get_ipv4_address(wifi_radio_obj_t *self) { |
| 146 | + if (!esp_netif_is_netif_up(self->netif)) { |
| 147 | + return mp_const_none; |
| 148 | + } |
| 149 | + esp_netif_ip_info_t ip_info; |
| 150 | + esp_err_t result = esp_netif_get_ip_info(self->netif, &ip_info); |
| 151 | + if (result != ESP_OK) { |
| 152 | + ESP_EARLY_LOGI(TAG, "get ip fail %d", result); |
| 153 | + } |
| 154 | + return common_hal_ipaddress_new_ipv4address(ip_info.ip.addr); |
116 | 155 | } |
117 | 156 |
|
118 | 157 | mp_int_t common_hal_wifi_radio_ping(wifi_radio_obj_t *self, mp_obj_t ip_address) { |
|
0 commit comments