WINC1500: Support static IP.

This commit is contained in:
iabdalkader 2019-08-25 19:08:53 +02:00
parent 66eac9f032
commit e685b484a0
4 changed files with 69 additions and 61 deletions

View File

@ -193,32 +193,30 @@ static mp_obj_t py_winc_wait_for_sta(mp_obj_t self_in, mp_obj_t timeout_in)
return sta_list;
}
static mp_obj_t py_winc_ifconfig(mp_obj_t self_in)
static mp_obj_t py_winc_ifconfig(size_t n_args, const mp_obj_t *args)
{
winc_ifconfig_t ifconfig;
mp_obj_t ifconfig_list= mp_obj_new_list(0, NULL);
winc_ifconfig(&ifconfig);
// Format MAC address
VSTR_FIXED(mac_vstr, 18);
vstr_printf(&mac_vstr, "%02x:%02x:%02x:%02x:%02x:%02x",
ifconfig.mac_addr[0], ifconfig.mac_addr[1], ifconfig.mac_addr[2],
ifconfig.mac_addr[3], ifconfig.mac_addr[4], ifconfig.mac_addr[5]);
// Format IP address
VSTR_FIXED(ip_vstr, 16);
vstr_printf(&ip_vstr, "%d.%d.%d.%d", ifconfig.ip_addr[0],
ifconfig.ip_addr[1], ifconfig.ip_addr[2], ifconfig.ip_addr[3]);
// Add connection info
mp_obj_list_append(ifconfig_list, mp_obj_new_int(ifconfig.rssi));
mp_obj_list_append(ifconfig_list, mp_obj_new_int(ifconfig.security));
mp_obj_list_append(ifconfig_list, mp_obj_new_str(ifconfig.ssid, strlen(ifconfig.ssid)));
mp_obj_list_append(ifconfig_list, mp_obj_new_str(mac_vstr.buf, mac_vstr.len));
mp_obj_list_append(ifconfig_list, mp_obj_new_str(ip_vstr.buf, ip_vstr.len));
return ifconfig_list;
if (n_args == 1) {
// get ifconfig info
winc_ifconfig(&ifconfig, false);
mp_obj_t tuple[4] = {
netutils_format_ipv4_addr(ifconfig.ip_addr, NETUTILS_BIG),
netutils_format_ipv4_addr(ifconfig.subnet_addr, NETUTILS_BIG),
netutils_format_ipv4_addr(ifconfig.gateway_addr, NETUTILS_BIG),
netutils_format_ipv4_addr(ifconfig.dns_addr, NETUTILS_BIG),
};
return mp_obj_new_tuple(4, tuple);
} else {
// set ifconfig info
mp_obj_t *items;
mp_obj_get_array_fixed_n(args[1], 4, &items);
netutils_parse_ipv4_addr(items[0], ifconfig.ip_addr, NETUTILS_BIG);
netutils_parse_ipv4_addr(items[1], ifconfig.subnet_addr, NETUTILS_BIG);
netutils_parse_ipv4_addr(items[2], ifconfig.gateway_addr, NETUTILS_BIG);
netutils_parse_ipv4_addr(items[3], ifconfig.dns_addr, NETUTILS_BIG);
winc_ifconfig(&ifconfig, true);
return mp_const_none;
}
}
static int winc_scan_callback(winc_scan_result_t *scan_result, void *arg)
@ -503,7 +501,7 @@ static MP_DEFINE_CONST_FUN_OBJ_1(py_winc_disconnect_obj, py_winc_disconnect);
static MP_DEFINE_CONST_FUN_OBJ_1(py_winc_isconnected_obj, py_winc_isconnected);
static MP_DEFINE_CONST_FUN_OBJ_1(py_winc_connected_sta_obj, py_winc_connected_sta);
static MP_DEFINE_CONST_FUN_OBJ_2(py_winc_wait_for_sta_obj, py_winc_wait_for_sta);
static MP_DEFINE_CONST_FUN_OBJ_1(py_winc_ifconfig_obj, py_winc_ifconfig);
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(py_winc_ifconfig_obj, 1, 2, py_winc_ifconfig);
static MP_DEFINE_CONST_FUN_OBJ_1(py_winc_scan_obj, py_winc_scan);
static MP_DEFINE_CONST_FUN_OBJ_1(py_winc_get_rssi_obj, py_winc_get_rssi);
static MP_DEFINE_CONST_FUN_OBJ_1(py_winc_fw_version_obj, py_winc_fw_version);

View File

@ -94,7 +94,7 @@ int wifidbg_init(wifidbg_config_t *config)
winc_ifconfig_t ifconfig;
if (winc_ifconfig(&ifconfig) < 0) {
if (winc_ifconfig(&ifconfig, false) < 0) {
return -3;
}

View File

@ -9,6 +9,7 @@
#ifndef __WINC_H__
#define __WINC_H__
#include <stdint.h>
#include <stdbool.h>
#define WINC_IP_ADDR_LEN (4)
#define WINC_MAC_ADDR_LEN (6)
#define WINC_MAX_SSID_LEN (33)
@ -50,11 +51,10 @@ typedef enum {
} winc_security_t;
typedef struct {
int8_t rssi;
uint8_t security;
char ssid[WINC_MAX_SSID_LEN];
uint8_t ip_addr[WINC_IP_ADDR_LEN];
uint8_t mac_addr[WINC_MAC_ADDR_LEN];
uint8_t subnet_addr[WINC_IP_ADDR_LEN];
uint8_t gateway_addr[WINC_IP_ADDR_LEN];
uint8_t dns_addr[WINC_IP_ADDR_LEN];
} winc_ifconfig_t;
typedef struct {
@ -99,7 +99,7 @@ int winc_disconnect();
int winc_isconnected();
int winc_connected_sta(uint32_t *sta_ip);
int winc_wait_for_sta(uint32_t *sta_ip, uint32_t timeout);
int winc_ifconfig(winc_ifconfig_t *ifconfig);
int winc_ifconfig(winc_ifconfig_t *ifconfig, bool set);
int winc_scan(winc_scan_callback_t cb, void *arg);
int winc_get_rssi();
int winc_fw_version(winc_fwver_t *wfwver);

View File

@ -23,10 +23,12 @@
static volatile bool ip_obtained = false;
static volatile bool wlan_connected = false;
static volatile uint32_t connected_sta_ip = false;
static volatile bool use_static_ip = false;
static void *async_request_data;
static uint8_t async_request_type=0;
static volatile bool async_request_done = false;
static winc_ifconfig_t ifconfig;
typedef struct {
int size;
@ -291,7 +293,17 @@ static void wifi_callback_sta(uint8_t msg_type, void *msg)
tstrM2mWifiStateChanged *wifi_state = (tstrM2mWifiStateChanged *)msg;
if (wifi_state->u8CurrState == M2M_WIFI_CONNECTED) {
wlan_connected = true;
m2m_wifi_request_dhcp_client();
if (use_static_ip) {
tstrM2MIPConfig ipconfig;
ipconfig.u32StaticIP = *((uint32_t*)ifconfig.ip_addr);
ipconfig.u32SubnetMask = *((uint32_t*)ifconfig.subnet_addr);
ipconfig.u32Gateway = *((uint32_t*)ifconfig.gateway_addr);
ipconfig.u32DNS = *((uint32_t*)ifconfig.dns_addr);
m2m_wifi_set_static_ip(&ipconfig);
// NOTE: the async request is done because there's
// no DHCP request/response when using a static IP.
async_request_done = true;
}
} else if (wifi_state->u8CurrState == M2M_WIFI_DISCONNECTED) {
ip_obtained = false;
wlan_connected = false;
@ -303,27 +315,15 @@ static void wifi_callback_sta(uint8_t msg_type, void *msg)
case M2M_WIFI_REQ_DHCP_CONF: {
ip_obtained = true;
async_request_done = true;
tstrM2MIPConfig *ipconfig = (tstrM2MIPConfig*)msg;
memcpy(ifconfig.ip_addr, &ipconfig->u32StaticIP, WINC_IP_ADDR_LEN);
memcpy(ifconfig.subnet_addr, &ipconfig->u32SubnetMask, WINC_IP_ADDR_LEN);
memcpy(ifconfig.gateway_addr, &ipconfig->u32Gateway, WINC_IP_ADDR_LEN);
memcpy(ifconfig.dns_addr, &ipconfig->u32DNS, WINC_IP_ADDR_LEN);
break;
}
case M2M_WIFI_RESP_CONN_INFO: {
// Connection info
tstrM2MConnInfo *con_info = (tstrM2MConnInfo*) msg;
winc_ifconfig_t *ifconfig = (winc_ifconfig_t *) async_request_data;
// Set rssi and security
ifconfig->rssi = con_info->s8RSSI;
ifconfig->security = con_info->u8SecType;
// Get MAC Address.
m2m_wifi_get_mac_address(ifconfig->mac_addr);
// Copy IP address.
memcpy(ifconfig->ip_addr, con_info->au8IPAddr, WINC_IP_ADDR_LEN);
// Copy SSID.
strncpy(ifconfig->ssid, con_info->acSSID, WINC_MAX_SSID_LEN-1);
async_request_done = true;
break;
}
@ -404,6 +404,12 @@ int winc_init(winc_mode_t winc_mode)
// Initialize the BSP.
nm_bsp_init();
// Use DHCP by default.
use_static_ip = false;
// Reset ifconfig info.
memset(&ifconfig, 0, sizeof(winc_ifconfig_t));
switch (winc_mode) {
case WINC_MODE_BSP: {
// Initialize the BSP and return.
@ -463,6 +469,11 @@ int winc_init(winc_mode_t winc_mode)
int winc_connect(const char *ssid, uint8_t security, const char *key, uint16_t channel)
{
async_request_data = &ifconfig;
//Disable/Enable DHCP client before connecting.
m2m_wifi_enable_dhcp(!use_static_ip);
// Connect to AP
if (m2m_wifi_connect((char*)ssid, strlen(ssid), security, (void*)key, M2M_WIFI_CH_ALL) != 0) {
return -1;
@ -546,19 +557,18 @@ int winc_wait_for_sta(uint32_t *sta_ip, uint32_t timeout)
return 0;
}
int winc_ifconfig(winc_ifconfig_t *ifconfig)
int winc_ifconfig(winc_ifconfig_t *rifconfig, bool set)
{
async_request_done = false;
async_request_data = ifconfig;
// Request connection info
m2m_wifi_get_connection_info();
while (async_request_done == false) {
// Handle pending events from network controller.
m2m_wifi_handle_events(NULL);
}
if (set) {
use_static_ip = true;
memcpy(&ifconfig, rifconfig, sizeof(winc_ifconfig_t));
} else {
// Copy the ifconfig info stored from the DHCP request.
memcpy(rifconfig->ip_addr, ifconfig.ip_addr, WINC_IP_ADDR_LEN);
memcpy(rifconfig->subnet_addr, ifconfig.subnet_addr, WINC_IP_ADDR_LEN);
memcpy(rifconfig->gateway_addr, ifconfig.gateway_addr, WINC_IP_ADDR_LEN);
memcpy(rifconfig->dns_addr, ifconfig.dns_addr, WINC_IP_ADDR_LEN);
}
return 0;
}