From a406bb5e11aedef0c24670f78bb2be572b478321 Mon Sep 17 00:00:00 2001 From: ZodiusInfuser Date: Tue, 20 Apr 2021 12:05:29 +0100 Subject: [PATCH] Added final missing functions from wifi api --- drivers/esp32spi/esp32spi.cpp | 95 +++++++++++++++++-- drivers/esp32spi/esp32spi.hpp | 16 +--- .../modules/pico_wireless/pico_wireless.c | 10 ++ .../modules/pico_wireless/pico_wireless.cpp | 69 ++++++++++++++ .../modules/pico_wireless/pico_wireless.h | 5 + 5 files changed, 174 insertions(+), 21 deletions(-) diff --git a/drivers/esp32spi/esp32spi.cpp b/drivers/esp32spi/esp32spi.cpp index 46925617..dda5d036 100644 --- a/drivers/esp32spi/esp32spi.cpp +++ b/drivers/esp32spi/esp32spi.cpp @@ -55,19 +55,19 @@ namespace pimoroni { GET_SOCKET = 0x3f, // 0x40 -> 0x4f - SET_CLIENT_CERT = 0x40, //TODO No Matching Function - SET_CERT_KEY = 0x41, //TODO No Matching Function + SET_CLIENT_CERT = 0x40, //NOTE No matching function + SET_CERT_KEY = 0x41, //NOTE No matching function //NULL, NULL, SEND_DATA_TCP = 0x44, GET_DATABUF_TCP = 0x45, INSERT_DATABUF = 0x46, //NULL, NULL, NULL, - WPA2_ENT_SET_IDENTITY = 0x4a, //TODO No Matching Function //NOTE Exposed in CPy - WPA2_ENT_SET_USERNAME = 0x4b, //TODO No Matching Function //NOTE Exposed in CPy - WPA2_ENT_SET_PASSWORD = 0x4c, //TODO No Matching Function //NOTE Exposed in CPy - WPA2_ENT_SET_CA_CERT = 0x4d, //TODO No Matching Function - WPA2_ENT_SET_CERT_KEY = 0x4e, //TODO No Matching Function - WPA2_ENT_ENABLE = 0x4f, //TODO No Matching Function //NOTE Exposed in CPy + WPA2_ENT_SET_IDENTITY = 0x4a, + WPA2_ENT_SET_USERNAME = 0x4b, + WPA2_ENT_SET_PASSWORD = 0x4c, + WPA2_ENT_SET_CA_CERT = 0x4d, //NOTE Not functional in Nina FW + WPA2_ENT_SET_CERT_KEY = 0x4e, //NOTE Not functional in Nina FW + WPA2_ENT_ENABLE = 0x4f, // 0x50 -> 0x5f SET_PIN_MODE = 0x50, @@ -993,7 +993,7 @@ namespace pimoroni { driver.esp_deselect(); } - void Esp32Spi::start_client(std::string host, uint32_t ip_address, uint16_t port, uint8_t sock, uint8_t protocol_mode) { + void Esp32Spi::start_client(const std::string host, uint32_t ip_address, uint16_t port, uint8_t sock, uint8_t protocol_mode) { driver.wait_for_esp_select(); // Send Command @@ -1325,4 +1325,81 @@ namespace pimoroni { return data; } + + void Esp32Spi::wifi_set_ent_identity(const std::string identity) { + driver.wait_for_esp_select(); + + // Send Command + driver.send_cmd(WPA2_ENT_SET_IDENTITY, SpiDrv::PARAM_NUMS_1); + driver.send_param((uint8_t*)identity.data(), identity.length(), SpiDrv::LAST_PARAM); + driver.pad_to_multiple_of_4(5 + identity.length()); + + driver.esp_deselect(); + driver.wait_for_esp_select(); + + // Wait for reply + uint8_t data = 0 , data_len = 0; + if(!driver.wait_response_cmd(WPA2_ENT_SET_IDENTITY, SpiDrv::PARAM_NUMS_1, &data, &data_len)) { + WARN("Response Err: WPA2_ENT_SET_IDENTITY\n"); + data = WL_FAILURE; + } + driver.esp_deselect(); + } + + void Esp32Spi::wifi_set_ent_username(const std::string username) { + driver.wait_for_esp_select(); + + // Send Command + driver.send_cmd(WPA2_ENT_SET_USERNAME, SpiDrv::PARAM_NUMS_1); + driver.send_param((uint8_t*)username.data(), username.length(), SpiDrv::LAST_PARAM); + driver.pad_to_multiple_of_4(5 + username.length()); + + driver.esp_deselect(); + driver.wait_for_esp_select(); + + // Wait for reply + uint8_t data = 0 , data_len = 0; + if(!driver.wait_response_cmd(WPA2_ENT_SET_USERNAME, SpiDrv::PARAM_NUMS_1, &data, &data_len)) { + WARN("Response Err: WPA2_ENT_SET_USERNAME\n"); + data = WL_FAILURE; + } + driver.esp_deselect(); + } + + void Esp32Spi::wifi_set_ent_password(const std::string password) { + driver.wait_for_esp_select(); + + // Send Command + driver.send_cmd(WPA2_ENT_SET_PASSWORD, SpiDrv::PARAM_NUMS_1); + driver.send_param((uint8_t*)password.data(), password.length(), SpiDrv::LAST_PARAM); + driver.pad_to_multiple_of_4(5 + password.length()); + + driver.esp_deselect(); + driver.wait_for_esp_select(); + + // Wait for reply + uint8_t data = 0 , data_len = 0; + if(!driver.wait_response_cmd(WPA2_ENT_SET_PASSWORD, SpiDrv::PARAM_NUMS_1, &data, &data_len)) { + WARN("Response Err: WPA2_ENT_SET_PASSWORD\n"); + data = WL_FAILURE; + } + driver.esp_deselect(); + } + + void Esp32Spi::wifi_set_ent_enable() { + driver.wait_for_esp_select(); + + // Send Command + driver.send_cmd(WPA2_ENT_ENABLE, SpiDrv::PARAM_NUMS_0); + + driver.esp_deselect(); + driver.wait_for_esp_select(); + + // Wait for reply + uint8_t data = 0, data_len = 0; + if(!driver.wait_response_cmd(WPA2_ENT_ENABLE, SpiDrv::PARAM_NUMS_1, (uint8_t*)&data, &data_len)) { + WARN("Response Err: WPA2_ENT_ENABLE\n"); + } + driver.esp_deselect(); + } } \ No newline at end of file diff --git a/drivers/esp32spi/esp32spi.hpp b/drivers/esp32spi/esp32spi.hpp index c3163f86..fb30b9f2 100644 --- a/drivers/esp32spi/esp32spi.hpp +++ b/drivers/esp32spi/esp32spi.hpp @@ -211,20 +211,12 @@ namespace pimoroni { uint8_t check_data_sent(uint8_t sock); uint8_t get_socket(); - //-------------------------------------------------- - // TODO Sort below ones out, particularly wpa2s from CPy //-------------------------------------------------- - // void wifi_set_entidentity(); //SET_ENT_IDENT - // void wifi_set_entusername(); //SET_ENT_UNAME - // void wifi_set_entpassword(); //SET_ENT_PASSWD - - // void wifi_set_entenable(); //SET_ENT_ENABLE - - // void is_connected(void); //Reads an internal status var for WL_CONNECTED - - // void connect(void); //Calls connect_ap - // void connect_ap(void); //Calls wifi_set_network and wifi_set_passphrase + void wifi_set_ent_identity(const std::string identity); + void wifi_set_ent_username(const std::string username); + void wifi_set_ent_password(const std::string password); + void wifi_set_ent_enable(); }; } \ No newline at end of file diff --git a/micropython/modules/pico_wireless/pico_wireless.c b/micropython/modules/pico_wireless/pico_wireless.c index 22d42886..4863580e 100644 --- a/micropython/modules/pico_wireless/pico_wireless.c +++ b/micropython/modules/pico_wireless/pico_wireless.c @@ -79,6 +79,11 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_KW(picowireless_send_data_obj, 2, picowireless_se STATIC MP_DEFINE_CONST_FUN_OBJ_KW(picowireless_check_data_sent_obj, 1, picowireless_check_data_sent); STATIC MP_DEFINE_CONST_FUN_OBJ_0(picowireless_get_socket_obj, picowireless_get_socket); +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(picowireless_wifi_set_ent_identity_obj, 1, picowireless_wifi_set_ent_identity); +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(picowireless_wifi_set_ent_username_obj, 1, picowireless_wifi_set_ent_username); +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(picowireless_wifi_set_ent_password_obj, 1, picowireless_wifi_set_ent_password); +STATIC MP_DEFINE_CONST_FUN_OBJ_0(picowireless_wifi_set_ent_enable_obj, picowireless_wifi_set_ent_enable); + STATIC MP_DEFINE_CONST_FUN_OBJ_KW(picowireless_set_led_obj, 3, picowireless_set_led); STATIC MP_DEFINE_CONST_FUN_OBJ_0(picowireless_is_pressed_obj, picowireless_is_pressed); STATIC MP_DEFINE_CONST_FUN_OBJ_0(picowireless_is_sdcard_detected_obj, picowireless_is_sdcard_detected); @@ -153,6 +158,11 @@ STATIC const mp_map_elem_t picowireless_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_send_data), MP_ROM_PTR(&picowireless_send_data_obj) }, { MP_ROM_QSTR(MP_QSTR_check_data_sent), MP_ROM_PTR(&picowireless_check_data_sent_obj) }, { MP_ROM_QSTR(MP_QSTR_get_socket), MP_ROM_PTR(&picowireless_get_socket_obj) }, + + { MP_ROM_QSTR(MP_QSTR_wifi_set_ent_identity), MP_ROM_PTR(&picowireless_wifi_set_ent_identity_obj) }, + { MP_ROM_QSTR(MP_QSTR_wifi_set_ent_username), MP_ROM_PTR(&picowireless_wifi_set_ent_username_obj) }, + { MP_ROM_QSTR(MP_QSTR_wifi_set_ent_password), MP_ROM_PTR(&picowireless_wifi_set_ent_password_obj) }, + { MP_ROM_QSTR(MP_QSTR_wifi_set_ent_enable), MP_ROM_PTR(&picowireless_wifi_set_ent_enable_obj) }, { MP_ROM_QSTR(MP_QSTR_set_led), MP_ROM_PTR(&picowireless_set_led_obj) }, { MP_ROM_QSTR(MP_QSTR_is_pressed), MP_ROM_PTR(&picowireless_is_pressed_obj) }, diff --git a/micropython/modules/pico_wireless/pico_wireless.cpp b/micropython/modules/pico_wireless/pico_wireless.cpp index 507c7596..450b6de4 100644 --- a/micropython/modules/pico_wireless/pico_wireless.cpp +++ b/micropython/modules/pico_wireless/pico_wireless.cpp @@ -1092,6 +1092,75 @@ mp_obj_t picowireless_get_socket() { return mp_const_none; } +mp_obj_t picowireless_wifi_set_ent_identity(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + if(wireless != nullptr) { + enum { ARG_identity }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_identity, MP_ARG_REQUIRED | MP_ARG_OBJ }, + }; + + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + std::string identity; + mp_obj_to_string(args[ARG_identity].u_obj, identity); + wireless->wifi_set_ent_identity(identity); + } + else + mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG); + + return mp_const_none; +} + +mp_obj_t picowireless_wifi_set_ent_username(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + if(wireless != nullptr) { + enum { ARG_username }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_username, MP_ARG_REQUIRED | MP_ARG_OBJ }, + }; + + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + std::string username; + mp_obj_to_string(args[ARG_username].u_obj, username); + wireless->wifi_set_ent_username(username); + } + else + mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG); + + return mp_const_none; +} + +mp_obj_t picowireless_wifi_set_ent_password(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + if(wireless != nullptr) { + enum { ARG_password }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_password, MP_ARG_REQUIRED | MP_ARG_OBJ }, + }; + + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + + std::string password; + mp_obj_to_string(args[ARG_password].u_obj, password); + wireless->wifi_set_ent_password(password); + } + else + mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG); + + return mp_const_none; +} + +mp_obj_t picowireless_wifi_set_ent_enable() { + if(wireless != nullptr) + wireless->wifi_set_ent_enable(); + else + mp_raise_msg(&mp_type_RuntimeError, NOT_INITIALISED_MSG); + + return mp_const_none; +} + mp_obj_t picowireless_set_led(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { if(wireless != nullptr) { enum { ARG_r, ARG_g, ARG_b }; diff --git a/micropython/modules/pico_wireless/pico_wireless.h b/micropython/modules/pico_wireless/pico_wireless.h index 66768ff8..0be9a7b5 100644 --- a/micropython/modules/pico_wireless/pico_wireless.h +++ b/micropython/modules/pico_wireless/pico_wireless.h @@ -77,6 +77,11 @@ extern mp_obj_t picowireless_send_data(size_t n_args, const mp_obj_t *pos_args, extern mp_obj_t picowireless_check_data_sent(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); extern mp_obj_t picowireless_get_socket(); +extern mp_obj_t picowireless_wifi_set_ent_identity(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); +extern mp_obj_t picowireless_wifi_set_ent_username(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); +extern mp_obj_t picowireless_wifi_set_ent_password(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); +extern mp_obj_t picowireless_wifi_set_ent_enable(); + extern mp_obj_t picowireless_set_led(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args); extern mp_obj_t picowireless_is_pressed(); extern mp_obj_t picowireless_is_sdcard_detected(); \ No newline at end of file