From 9dafa2447bf68e03fd7ed7411586eb06bf5e4b0f Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Thu, 29 Jul 2021 21:14:04 +0200 Subject: [PATCH] Nina: Implement AP mode. --- src/drivers/ninaw10/include/nina.h | 6 ++++ src/drivers/ninaw10/src/nina.c | 47 ++++++++++++++++++++++++++---- src/micropython | 2 +- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/src/drivers/ninaw10/include/nina.h b/src/drivers/ninaw10/include/nina.h index df406135e..e351ed90f 100644 --- a/src/drivers/ninaw10/include/nina.h +++ b/src/drivers/ninaw10/include/nina.h @@ -39,6 +39,12 @@ typedef enum { NINA_ERROR_TIMEOUT = -2, } nina_error_t; + +typedef enum { + NINA_WIFI_MODE_STA = 0, + NINA_WIFI_MODE_AP, +} nina_wifi_mode_t; + typedef struct { uint8_t ip_addr[NINA_IPV4_ADDR_LEN]; uint8_t subnet_addr[NINA_IPV4_ADDR_LEN]; diff --git a/src/drivers/ninaw10/src/nina.c b/src/drivers/ninaw10/src/nina.c index 4bbfd163d..737949f56 100644 --- a/src/drivers/ninaw10/src/nina.c +++ b/src/drivers/ninaw10/src/nina.c @@ -359,7 +359,7 @@ int nina_connect(const char *ssid, uint8_t security, const char *key, uint16_t c if (key == NULL && security != NINA_SEC_OPEN) { return -1; } - + switch (security) { case NINA_SEC_OPEN: if (nina_send_command_read_ack(SET_NET_CMD, @@ -381,17 +381,17 @@ int nina_connect(const char *ssid, uint8_t security, const char *key, uint16_t c break; default: return -1; - } + } - for (mp_uint_t start = mp_hal_ticks_ms(); ; mp_hal_delay_ms(100)) { - status = nina_connection_status(); + for (mp_uint_t start = mp_hal_ticks_ms(); ; mp_hal_delay_ms(10)) { + status = nina_connection_status(); if ((status != WL_IDLE_STATUS) && (status != WL_NO_SSID_AVAIL) && (status != WL_SCAN_COMPLETED)) { break; } if ((mp_hal_ticks_ms() - start) >= NINA_CONNECT_TIMEOUT) { break; - } + } } return (status == WL_CONNECTED) ? 0 : -1; @@ -399,7 +399,42 @@ int nina_connect(const char *ssid, uint8_t security, const char *key, uint16_t c int nina_start_ap(const char *ssid, uint8_t security, const char *key, uint16_t channel) { - return -1; + uint8_t status = WL_AP_FAILED; + + if ((key == NULL && security != NINA_SEC_OPEN) || + (security != NINA_SEC_OPEN && security != NINA_SEC_WEP)) { + return -1; + } + + switch (security) { + case NINA_SEC_OPEN: + if (nina_send_command_read_ack(SET_AP_NET_CMD, + 2, ARG_8BITS, NINA_ARGS(ARG_STR(ssid), ARG_BYTE(channel))) != SPI_ACK) { + return -1; + } + break; + case NINA_SEC_WEP: + if (nina_send_command_read_ack(SET_AP_PASSPHRASE_CMD, + 3, ARG_8BITS, NINA_ARGS(ARG_STR(ssid), ARG_STR(key), ARG_BYTE(channel))) != SPI_ACK) { + return -1; + } + break; + default: + return -1; + } + + for (mp_uint_t start = mp_hal_ticks_ms(); ; mp_hal_delay_ms(10)) { + status = nina_connection_status(); + if ((status != WL_IDLE_STATUS) && (status != WL_NO_SSID_AVAIL) && (status != WL_SCAN_COMPLETED)) { + break; + } + + if ((mp_hal_ticks_ms() - start) >= NINA_CONNECT_TIMEOUT) { + break; + } + } + + return (status == WL_AP_LISTENING) ? 0 : -1; } int nina_disconnect() diff --git a/src/micropython b/src/micropython index ff9bd957f..7ae9fc12e 160000 --- a/src/micropython +++ b/src/micropython @@ -1 +1 @@ -Subproject commit ff9bd957f0475fcaf9dc5cec3c19f8e07506370f +Subproject commit 7ae9fc12e7f27c4b94784db13f80eac02ae83aa5