Skip to content

Commit c62ab6e

Browse files
committed
Add ipaddress
1 parent ddcff85 commit c62ab6e

20 files changed

Lines changed: 634 additions & 33 deletions

File tree

ports/esp32s2/common-hal/wifi/Network.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@
2626

2727
#include "shared-bindings/wifi/Network.h"
2828

29+
#include <string.h>
30+
31+
#include "py/obj.h"
32+
2933
mp_obj_t common_hal_wifi_network_get_ssid(wifi_network_obj_t *self) {
30-
return mp_const_none;
34+
const char* cstr = (const char*) self->record.ssid;
35+
return mp_obj_new_str(cstr, strlen(cstr));
36+
}
37+
38+
mp_obj_t common_hal_wifi_network_get_rssi(wifi_network_obj_t *self) {
39+
return mp_obj_new_int(self->record.rssi);
40+
}
41+
42+
mp_obj_t common_hal_wifi_network_get_channel(wifi_network_obj_t *self) {
43+
return mp_obj_new_int(self->record.primary);
3144
}

ports/esp32s2/common-hal/wifi/Radio.c

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@
2626

2727
#include "shared-bindings/wifi/Radio.h"
2828

29+
#include <string.h>
30+
31+
#include "lib/utils/interrupt_char.h"
2932
#include "py/runtime.h"
33+
#include "shared-bindings/ipaddress/IPv4Address.h"
3034
#include "shared-bindings/wifi/ScannedNetworks.h"
3135

3236
#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) {
103107
ESP_EARLY_LOGI(TAG, "stop scan done");
104108
}
105109

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) {
107111
// 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+
}
110125
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;
112143
}
113144

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);
116155
}
117156

118157
mp_int_t common_hal_wifi_radio_ping(wifi_radio_obj_t *self, mp_obj_t ip_address) {

ports/esp32s2/common-hal/wifi/Radio.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
// Event bits for the Radio event group.
3737
#define WIFI_SCAN_DONE_BIT BIT0
38+
#define WIFI_CONNECTED_BIT BIT1
39+
#define WIFI_DISCONNECTED_BIT BIT2
3840

3941
typedef struct {
4042
mp_obj_base_t base;

ports/esp32s2/common-hal/wifi/ScannedNetworks.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <string.h>
3030

3131
#include "lib/utils/interrupt_char.h"
32+
#include "py/gc.h"
3233
#include "py/objstr.h"
3334
#include "py/runtime.h"
3435
#include "shared-bindings/wifi/__init__.h"
@@ -45,7 +46,10 @@ static void wifi_scannednetworks_done(wifi_scannednetworks_obj_t *self) {
4546
self->done = true;
4647
ESP_EARLY_LOGI(TAG, "free %x", self->results);
4748
if (self->results != NULL) {
48-
m_free(self->results);
49+
// Check to see if the heap is still active. If not, it'll be freed automatically.
50+
if (gc_alloc_possible()) {
51+
m_free(self->results);
52+
}
4953
self->results = NULL;
5054
}
5155
}
@@ -137,7 +141,6 @@ mp_obj_t common_hal_wifi_scannednetworks_next(wifi_scannednetworks_obj_t *self)
137141
static uint8_t scan_pattern[] = {6, 1, 11, 3, 9, 13, 2, 4, 8, 12, 5, 7, 10, 14};
138142

139143
void wifi_scannednetworks_scan_next_channel(wifi_scannednetworks_obj_t *self) {
140-
141144
uint8_t next_channel = sizeof(scan_pattern);
142145
while (self->current_channel_index < sizeof(scan_pattern)) {
143146
next_channel = scan_pattern[self->current_channel_index];

ports/esp32s2/common-hal/wifi/__init__.c

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,32 @@ static void event_handler(void* arg, esp_event_base_t event_base,
4141
int32_t event_id, void* event_data) {
4242
ESP_LOGI(TAG, "event %x", event_id);
4343
wifi_radio_obj_t* radio = arg;
44-
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_SCAN_DONE) {
45-
xEventGroupSetBits(radio->event_group_handle, WIFI_SCAN_DONE_BIT);
44+
if (event_base == WIFI_EVENT) {
45+
if (event_id == WIFI_EVENT_SCAN_DONE) {
46+
ESP_LOGI(TAG, "scan done");
47+
xEventGroupSetBits(radio->event_group_handle, WIFI_SCAN_DONE_BIT);
48+
} else if (event_id == WIFI_EVENT_STA_START) {
49+
ESP_LOGI(TAG, "station start");
50+
51+
} else if (event_id == WIFI_EVENT_STA_STOP) {
52+
ESP_LOGI(TAG, "station stop");
53+
54+
} else if (event_id == WIFI_EVENT_STA_CONNECTED) {
55+
ESP_LOGI(TAG, "connected to ap");
56+
} else if (event_id == WIFI_EVENT_STA_DISCONNECTED) {
57+
ESP_LOGI(TAG, "disconnected");
58+
wifi_event_sta_disconnected_t* d = (wifi_event_sta_disconnected_t*) event_data;
59+
ESP_LOGI(TAG, "reason %d", d->reason);
60+
if (event_id != WIFI_REASON_ASSOC_LEAVE) {
61+
// reconnect
62+
}
63+
xEventGroupSetBits(radio->event_group_handle, WIFI_DISCONNECTED_BIT);
64+
} else if (event_id == WIFI_EVENT_STA_AUTHMODE_CHANGE) {
65+
ESP_LOGI(TAG, "auth change");
66+
67+
}
4668
}
69+
4770
// esp_wifi_connect();
4871
// if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
4972
// esp_wifi_connect();
@@ -56,12 +79,13 @@ static void event_handler(void* arg, esp_event_base_t event_base,
5679
// xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
5780
// }
5881
// ESP_LOGI(TAG,"connect to the AP fail");
59-
// } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
60-
// ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
61-
// ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
62-
// s_retry_num = 0;
63-
// xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
64-
// }
82+
// } else
83+
if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
84+
ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data;
85+
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
86+
// s_retry_num = 0;
87+
xEventGroupSetBits(radio->event_group_handle, WIFI_CONNECTED_BIT);
88+
}
6589
}
6690

6791
static bool wifi_inited;

py/circuitpy_defns.mk

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ endif
168168
ifeq ($(CIRCUITPY_I2CPERIPHERAL),1)
169169
SRC_PATTERNS += i2cperipheral/%
170170
endif
171+
ifeq ($(CIRCUITPY_IPADDRESS),1)
172+
SRC_PATTERNS += ipaddress/%
173+
endif
171174
ifeq ($(CIRCUITPY_MATH),1)
172175
SRC_PATTERNS += math/%
173176
endif
@@ -408,6 +411,8 @@ SRC_SHARED_MODULE_ALL = \
408411
fontio/__init__.c \
409412
framebufferio/FramebufferDisplay.c \
410413
framebufferio/__init__.c \
414+
ipaddress/IPv4Address.c \
415+
ipaddress/__init__.c \
411416
sdcardio/SDCard.c \
412417
sdcardio/__init__.c \
413418
gamepad/GamePad.c \

py/circuitpy_mpconfig.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,13 @@ extern const struct _mp_obj_module_t i2cperipheral_module;
420420
#define I2CPERIPHERAL_MODULE
421421
#endif
422422

423+
#if CIRCUITPY_IPADDRESS
424+
extern const struct _mp_obj_module_t ipaddress_module;
425+
#define IPADDRESS_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_ipaddress), (mp_obj_t)&ipaddress_module },
426+
#else
427+
#define IPADDRESS_MODULE
428+
#endif
429+
423430
#if CIRCUITPY_MATH
424431
extern const struct _mp_obj_module_t math_module;
425432
#define MATH_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_math), (mp_obj_t)&math_module },

py/circuitpy_mpconfig.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ CFLAGS += -DCIRCUITPY_GNSS=$(CIRCUITPY_GNSS)
120120
CIRCUITPY_I2CPERIPHERAL ?= $(CIRCUITPY_FULL_BUILD)
121121
CFLAGS += -DCIRCUITPY_I2CPERIPHERAL=$(CIRCUITPY_I2CPERIPHERAL)
122122

123+
CIRCUITPY_IPADDRESS ?= $(CIRCUITPY_WIFI)
124+
CFLAGS += -DCIRCUITPY_IPADDRESS=$(CIRCUITPY_IPADDRESS)
125+
123126
CIRCUITPY_MATH ?= 1
124127
CFLAGS += -DCIRCUITPY_MATH=$(CIRCUITPY_MATH)
125128

0 commit comments

Comments
 (0)