mirror of
https://github.com/arduino/Arduino.git
synced 2025-02-20 14:54:31 +01:00
Added API to set static IP and DNS server
This commit is contained in:
parent
9c30c73c2b
commit
dc3102d2f4
Binary file not shown.
File diff suppressed because it is too large
Load Diff
@ -26,6 +26,7 @@
|
||||
#include <board_init.h>
|
||||
#include "util.h"
|
||||
#include "lwip/udp.h"
|
||||
#include "lwip_setup.h"
|
||||
|
||||
extern const char* fwVersion;
|
||||
|
||||
@ -94,7 +95,7 @@ bool end_write = false; //TODO only for debug
|
||||
// Signal indicating a new command is coming from SPI interface
|
||||
static volatile Bool startRecvCmdSignal = FALSE;
|
||||
|
||||
#define MAX_CMD_NUM 34
|
||||
#define MAX_CMD_NUM 36
|
||||
typedef struct sCmd_spi_list{
|
||||
cmd_spi_cb_t cb;
|
||||
char cmd_id;
|
||||
@ -535,6 +536,117 @@ int set_passphrase_cmd_cb(int numParam, char* buf, void* ctx) {
|
||||
RETURN_ERR(err)
|
||||
}
|
||||
|
||||
int set_ip_config_cmd_cb(int numParam, char* buf, void* ctx) {
|
||||
struct ip_addr lwip_addr;
|
||||
uint32_t _ip_addr = 0;
|
||||
struct ctx_server *hs = ctx;
|
||||
struct net_cfg *ncfg = &(hs->net_cfg);
|
||||
struct netif *nif = ncfg->netif;
|
||||
uint8_t parmsToChange=0;
|
||||
const uint8_t MAX_IP_CONFIG_PARAMS = 3;
|
||||
const uint8_t DNS_SERVER_IDX_CHANG = 2;
|
||||
|
||||
wl_err_t err = WL_SUCCESS;
|
||||
tParam* params = (tParam*) buf;
|
||||
|
||||
if (params->paramLen == 1)
|
||||
{
|
||||
GET_PARAM_NEXT(BYTE, params, _parmsToChange);
|
||||
parmsToChange = _parmsToChange;
|
||||
}
|
||||
else
|
||||
RETURN_ERR(WL_FAILURE)
|
||||
|
||||
INFO_SPI("%p numParam=%d parmsToChange=%d\n", ctx, numParam, parmsToChange);
|
||||
|
||||
if (parmsToChange <= MAX_IP_CONFIG_PARAMS)
|
||||
{
|
||||
int i=0;
|
||||
for (; i<parmsToChange; ++i)
|
||||
{
|
||||
if (params->paramLen == 4)
|
||||
{
|
||||
GET_PARAM_NEXT(LONG, params, _ip_addr);
|
||||
lwip_addr.addr = _ip_addr;
|
||||
INFO_SPI("%d] nif:%p lwip_addr=0x%x\n", i, nif, lwip_addr.addr);
|
||||
switch (i)
|
||||
{
|
||||
case 0: // local_ip
|
||||
{
|
||||
netif_set_ipaddr(nif, &lwip_addr);
|
||||
break;
|
||||
}
|
||||
case 1: // gateway
|
||||
{
|
||||
netif_set_gw(nif, &lwip_addr);
|
||||
break;
|
||||
}
|
||||
case 2: // subnet
|
||||
{
|
||||
netif_set_netmask(nif, &lwip_addr);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}else{
|
||||
RETURN_ERR(WL_FAILURE)
|
||||
}
|
||||
|
||||
}
|
||||
/* Disable DHCP */
|
||||
ncfg->dhcp_enabled = 0;
|
||||
}else
|
||||
RETURN_ERR(WL_FAILURE)
|
||||
|
||||
RETURN_ERR(err)
|
||||
}
|
||||
|
||||
int set_dns_config_cmd_cb(int numParam, char* buf, void* ctx) {
|
||||
struct ip_addr lwip_addr;
|
||||
struct ctx_server *hs = ctx;
|
||||
struct net_cfg *ncfg = &(hs->net_cfg);
|
||||
struct netif *nif = ncfg->netif;
|
||||
uint8_t parmsToChange=0;
|
||||
const uint8_t MAX_DNS_CONFIG_PARAMS = 2;
|
||||
const uint8_t DNS_SERVER_IDX_CHANG = 2;
|
||||
|
||||
wl_err_t err = WL_SUCCESS;
|
||||
tParam* params = (tParam*) buf;
|
||||
|
||||
if (params->paramLen == 1)
|
||||
{
|
||||
GET_PARAM_NEXT(BYTE, params, _parmsToChange);
|
||||
parmsToChange = _parmsToChange;
|
||||
}
|
||||
else
|
||||
RETURN_ERR(WL_FAILURE)
|
||||
|
||||
INFO_SPI("%p numParam=%d parmsToChange=%d\n", ctx, numParam, parmsToChange);
|
||||
|
||||
if (parmsToChange <= MAX_DNS_CONFIG_PARAMS)
|
||||
{
|
||||
int i=0;
|
||||
for (; i<parmsToChange; ++i)
|
||||
{
|
||||
if (params->paramLen == 4)
|
||||
{
|
||||
GET_PARAM_NEXT(LONG, params, _ip_addr);
|
||||
lwip_addr.addr = _ip_addr;
|
||||
INFO_SPI("%d] nif:%p lwip_addr=0x%x\n", i, nif, lwip_addr.addr);
|
||||
dns_setserver(i, &lwip_addr);
|
||||
}else{
|
||||
RETURN_ERR(WL_FAILURE)
|
||||
}
|
||||
}
|
||||
/* Disable DHCP */
|
||||
ncfg->dhcp_enabled = 0;
|
||||
}else
|
||||
RETURN_ERR(WL_FAILURE)
|
||||
|
||||
RETURN_ERR(err)
|
||||
}
|
||||
|
||||
|
||||
|
||||
void set_result(wl_status_t _status)
|
||||
{
|
||||
result = _status;
|
||||
@ -1402,12 +1514,12 @@ int call_reply_cb(char* recv, char* reply) {
|
||||
{
|
||||
tSpiMsg* spiMsg = (tSpiMsg*) recv;
|
||||
_result = cmd_spi_list[i].cb(spiMsg->nParam,
|
||||
(char*) &(spiMsg->params[0]), NULL);
|
||||
(char*) &(spiMsg->params[0]), cmd_spi_list[i].ctx);
|
||||
}else
|
||||
{
|
||||
tSpiMsgData* spiMsg = (tSpiMsgData*) recv;
|
||||
_result = cmd_spi_list[i].cb(spiMsg->nParam,
|
||||
(char*) &(spiMsg->params[0]), NULL);
|
||||
(char*) &(spiMsg->params[0]), cmd_spi_list[i].ctx);
|
||||
}
|
||||
|
||||
if (_result != WIFI_SPI_ACK)
|
||||
@ -1452,10 +1564,12 @@ int call_reply_cb(char* recv, char* reply) {
|
||||
return REPLY_NO_ERR;
|
||||
}
|
||||
|
||||
void init_spi_cmds() {
|
||||
void init_spi_cmds(void* ctx) {
|
||||
spi_add_cmd(SET_NET_CMD, set_net_cmd_cb, ack_reply_cb, NULL, CMD_SET_FLAG);
|
||||
spi_add_cmd(SET_PASSPHRASE_CMD, set_passphrase_cmd_cb, ack_reply_cb, NULL, CMD_SET_FLAG);
|
||||
spi_add_cmd(SET_KEY_CMD, set_key_cmd_cb, ack_reply_cb, NULL, CMD_SET_FLAG);
|
||||
spi_add_cmd(SET_IP_CONFIG_CMD, set_ip_config_cmd_cb, ack_reply_cb, ctx, CMD_SET_FLAG);
|
||||
spi_add_cmd(SET_DNS_CONFIG_CMD, set_dns_config_cmd_cb, ack_reply_cb, ctx, CMD_SET_FLAG);
|
||||
spi_add_cmd(GET_CONN_STATUS_CMD, get_result_cmd_cb, get_reply_cb, NULL, CMD_GET_FLAG);
|
||||
spi_add_cmd(GET_IPADDR_CMD, ack_cmd_cb, get_reply_ipaddr_cb, NULL, CMD_GET_FLAG);
|
||||
spi_add_cmd(GET_MACADDR_CMD, ack_cmd_cb, get_reply_mac_cb, NULL, CMD_GET_FLAG);
|
||||
@ -1786,7 +1900,7 @@ void initExtInt()
|
||||
Enable_global_interrupt();
|
||||
}
|
||||
|
||||
int initSpi()
|
||||
int initSpi(void* ctx)
|
||||
{
|
||||
volatile avr32_spi_t *spi = &AVR32_SPI0;
|
||||
gpio_map_t spi_piomap = { \
|
||||
@ -1838,7 +1952,7 @@ int initSpi()
|
||||
#ifdef _SPI_STATS_
|
||||
initStatSpi();
|
||||
#endif
|
||||
init_spi_cmds();
|
||||
init_spi_cmds(ctx);
|
||||
|
||||
memset(_receiveBuffer, 0, sizeof(_receiveBuffer));
|
||||
memset(buf, 0, sizeof(buf));
|
||||
|
@ -57,7 +57,7 @@ void set_result_cmd(int err) ;
|
||||
|
||||
void set_result(wl_status_t _status);
|
||||
|
||||
int initSpi(void);
|
||||
int initSpi(void* ctx);
|
||||
|
||||
void initExtInt();
|
||||
|
||||
|
@ -182,7 +182,8 @@ cmd_ibss(int argc, char* argv[], void* ctx)
|
||||
cmd_state_t
|
||||
cmd_set_ip(int argc, char* argv[], void* ctx)
|
||||
{
|
||||
struct net_cfg *ncfg = ctx;
|
||||
struct ctx_server *hs = ctx;
|
||||
struct net_cfg *ncfg = &(hs->net_cfg);
|
||||
struct ip_addr lwip_addr;
|
||||
struct netif *nif = ncfg->netif;
|
||||
|
||||
@ -197,8 +198,10 @@ cmd_set_ip(int argc, char* argv[], void* ctx)
|
||||
printk(" or : ip none (to enable DHCP)\n");
|
||||
return CMD_DONE;
|
||||
}
|
||||
|
||||
/* IP address */
|
||||
lwip_addr = str2ip(argv[1]);
|
||||
INFO_SPI("nif:%p lwip_addr=0x%x\n", nif, lwip_addr.addr);
|
||||
netif_set_ipaddr(nif, &lwip_addr);
|
||||
/* Netmask */
|
||||
lwip_addr = str2ip(argv[2]);
|
||||
|
@ -7,6 +7,11 @@ struct net_cfg {
|
||||
uint8_t dhcp_running;
|
||||
};
|
||||
|
||||
struct ctx_server {
|
||||
struct net_cfg net_cfg;
|
||||
uint8_t wl_init_complete;
|
||||
};
|
||||
|
||||
/*! Start the IP stack.
|
||||
* If cfg->netif must have been allocated and lwip_init()
|
||||
* must have been called before this function is called
|
||||
|
@ -78,11 +78,6 @@ void fw_download_cb(void* ctx, uint8_t** buf, uint32_t* len)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
struct ctx_server {
|
||||
struct net_cfg net_cfg;
|
||||
uint8_t wl_init_complete;
|
||||
};
|
||||
|
||||
bool ifStatus = false;
|
||||
bool scanNetCompleted = false;
|
||||
|
||||
@ -122,7 +117,7 @@ wl_cm_conn_cb(struct wl_network_t* net, void* ctx)
|
||||
hs->net_cfg.dhcp_running = 1;
|
||||
}
|
||||
else {
|
||||
netif_set_up(hs->net_cfg.netif);
|
||||
netif_set_up(hs->net_cfg.netif);
|
||||
}
|
||||
|
||||
INFO_INIT("Start DNS...\n");
|
||||
@ -270,7 +265,7 @@ poll(struct ctx_server* hs)
|
||||
#endif
|
||||
}
|
||||
|
||||
void initShell()
|
||||
void initShell(void* ctx)
|
||||
{
|
||||
/* initialize shell */
|
||||
INFO_INIT("Shell init...\n");
|
||||
@ -281,7 +276,7 @@ void initShell()
|
||||
console_add_cmd("status", cmd_status, NULL);
|
||||
console_add_cmd("debug", cmd_debug, NULL);
|
||||
console_add_cmd("dumpBuf", cmd_dumpBuf, NULL);
|
||||
console_add_cmd("ipconfig", cmd_set_ip, NULL);
|
||||
console_add_cmd("ipconfig", cmd_set_ip, ctx);
|
||||
|
||||
#ifdef ADD_CMDS
|
||||
console_add_cmd("powersave", cmd_power, NULL);
|
||||
@ -325,6 +320,7 @@ wl_init_complete_cb(void* ctx)
|
||||
|
||||
/* default is dhcp enabled */
|
||||
hs->net_cfg.dhcp_enabled = 1;
|
||||
|
||||
start_ip_stack(&hs->net_cfg,
|
||||
ipaddr,
|
||||
netmask,
|
||||
@ -339,7 +335,7 @@ wl_init_complete_cb(void* ctx)
|
||||
|
||||
wl_scan();
|
||||
|
||||
if (initSpi()){
|
||||
if (initSpi(hs)){
|
||||
WARN("Spi not initialized\n");
|
||||
}else
|
||||
{
|
||||
@ -381,8 +377,6 @@ main(void)
|
||||
|
||||
tc_init();
|
||||
|
||||
initShell();
|
||||
|
||||
delay_init(FOSC0);
|
||||
|
||||
#ifdef _TEST_SPI_
|
||||
@ -408,7 +402,7 @@ main(void)
|
||||
|
||||
INFO_INIT("hs:%p size:0x%x netif:%p size:0x%x\n", hs, size_ctx_server,
|
||||
hs->net_cfg.netif, size_netif);
|
||||
|
||||
initShell(hs);
|
||||
timer_init(NULL, NULL);
|
||||
lwip_init();
|
||||
|
||||
|
@ -32,6 +32,8 @@ enum {
|
||||
SET_PASSPHRASE_CMD = 0x11,
|
||||
SET_KEY_CMD = 0x12,
|
||||
TEST_CMD = 0x13,
|
||||
SET_IP_CONFIG_CMD = 0x14,
|
||||
SET_DNS_CONFIG_CMD = 0x15,
|
||||
|
||||
GET_CONN_STATUS_CMD = 0x20,
|
||||
GET_IPADDR_CMD = 0x21,
|
||||
@ -155,3 +157,4 @@ typedef struct __attribute__((__packed__))
|
||||
}tByteParam;
|
||||
|
||||
#endif
|
||||
uint8_t param;
|
@ -98,6 +98,31 @@ int WiFiClass::begin(char* ssid, const char *passphrase)
|
||||
return status;
|
||||
}
|
||||
|
||||
void WiFiClass::config(IPAddress local_ip)
|
||||
{
|
||||
WiFiDrv::config(1, (uint32_t)local_ip, 0, 0);
|
||||
}
|
||||
|
||||
void WiFiClass::config(IPAddress local_ip, IPAddress gateway)
|
||||
{
|
||||
WiFiDrv::config(2, (uint32_t)local_ip, (uint32_t)gateway, 0);
|
||||
}
|
||||
|
||||
void WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet)
|
||||
{
|
||||
WiFiDrv::config(3, (uint32_t)local_ip, (uint32_t)gateway, (uint32_t)subnet);
|
||||
}
|
||||
|
||||
void WiFiClass::setDNS(IPAddress dns_server1)
|
||||
{
|
||||
WiFiDrv::setDNS(1, (uint32_t)dns_server1, 0);
|
||||
}
|
||||
|
||||
void WiFiClass::setDNS(IPAddress dns_server1, IPAddress dns_server2)
|
||||
{
|
||||
WiFiDrv::setDNS(2, (uint32_t)dns_server1, (uint32_t)dns_server2);
|
||||
}
|
||||
|
||||
int WiFiClass::disconnect()
|
||||
{
|
||||
return WiFiDrv::disconnect();
|
||||
|
@ -59,6 +59,50 @@ public:
|
||||
*/
|
||||
int begin(char* ssid, const char *passphrase);
|
||||
|
||||
/* Change Ip configuration settings disabling the dhcp client
|
||||
*
|
||||
* param local_ip: Static ip configuration
|
||||
*/
|
||||
void config(IPAddress local_ip);
|
||||
|
||||
/* Change Ip configuration settings disabling the dhcp client
|
||||
*
|
||||
* param local_ip: Static ip configuration
|
||||
* param dns_server: Static DNS server configuration
|
||||
*/
|
||||
void config(IPAddress local_ip, IPAddress dns_server);
|
||||
|
||||
/* Change Ip configuration settings disabling the dhcp client
|
||||
*
|
||||
* param local_ip: Static ip configuration
|
||||
* param dns_server: Static DNS server configuration
|
||||
* param gateway: Static gateway configuration
|
||||
*/
|
||||
void config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway);
|
||||
|
||||
/* Change Ip configuration settings disabling the dhcp client
|
||||
*
|
||||
* param local_ip: Static ip configuration
|
||||
* param dns_server: Static DNS server configuration
|
||||
* param gateway: Static gateway configuration
|
||||
* param subnet: Static subnet mask configuration
|
||||
*/
|
||||
void config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet);
|
||||
|
||||
/* Change DNS Ip configuration
|
||||
*
|
||||
* param dns_server1: ip configuration for DNS server 1
|
||||
*/
|
||||
void setDNS(IPAddress dns_server1);
|
||||
|
||||
/* Change DNS Ip configuration
|
||||
*
|
||||
* param dns_server1: ip configuration for DNS server 1
|
||||
* param dns_server2: ip configuration for DNS server 2
|
||||
*
|
||||
*/
|
||||
void setDNS(IPAddress dns_server1, IPAddress dns_server2);
|
||||
|
||||
/*
|
||||
* Disconnect from the network
|
||||
*
|
||||
|
@ -151,6 +151,55 @@ int8_t WiFiDrv::wifiSetKey(char* ssid, uint8_t ssid_len, uint8_t key_idx, const
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
return _data;
|
||||
}
|
||||
|
||||
void WiFiDrv::config(uint8_t validParams, uint32_t local_ip, uint32_t gateway, uint32_t subnet)
|
||||
{
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(SET_IP_CONFIG_CMD, PARAM_NUMS_4);
|
||||
SpiDrv::sendParam((uint8_t*)&validParams, 1, NO_LAST_PARAM);
|
||||
SpiDrv::sendParam((uint8_t*)&local_ip, 4, NO_LAST_PARAM);
|
||||
SpiDrv::sendParam((uint8_t*)&gateway, 4, NO_LAST_PARAM);
|
||||
SpiDrv::sendParam((uint8_t*)&subnet, 4, LAST_PARAM);
|
||||
|
||||
//Wait the reply elaboration
|
||||
SpiDrv::waitForSlaveReady();
|
||||
|
||||
// Wait for reply
|
||||
uint8_t _data = 0;
|
||||
uint8_t _dataLen = 0;
|
||||
if (!SpiDrv::waitResponseCmd(SET_IP_CONFIG_CMD, PARAM_NUMS_1, &_data, &_dataLen))
|
||||
{
|
||||
WARN("error waitResponse");
|
||||
_data = WL_FAILURE;
|
||||
}
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
}
|
||||
|
||||
void WiFiDrv::setDNS(uint8_t validParams, uint32_t dns_server1, uint32_t dns_server2)
|
||||
{
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(SET_DNS_CONFIG_CMD, PARAM_NUMS_3);
|
||||
SpiDrv::sendParam((uint8_t*)&validParams, 1, NO_LAST_PARAM);
|
||||
SpiDrv::sendParam((uint8_t*)&dns_server1, 4, NO_LAST_PARAM);
|
||||
SpiDrv::sendParam((uint8_t*)&dns_server2, 4, LAST_PARAM);
|
||||
|
||||
//Wait the reply elaboration
|
||||
SpiDrv::waitForSlaveReady();
|
||||
|
||||
// Wait for reply
|
||||
uint8_t _data = 0;
|
||||
uint8_t _dataLen = 0;
|
||||
if (!SpiDrv::waitResponseCmd(SET_DNS_CONFIG_CMD, PARAM_NUMS_1, &_data, &_dataLen))
|
||||
{
|
||||
WARN("error waitResponse");
|
||||
_data = WL_FAILURE;
|
||||
}
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
}
|
||||
|
||||
|
||||
|
||||
int8_t WiFiDrv::disconnect()
|
||||
{
|
||||
|
@ -90,6 +90,27 @@ public:
|
||||
*/
|
||||
static int8_t wifiSetKey(char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t len);
|
||||
|
||||
/* Set ip configuration disabling dhcp client
|
||||
*
|
||||
* param validParams: set the number of parameters that we want to change
|
||||
* i.e. validParams = 1 means that we'll change only ip address
|
||||
* validParams = 3 means that we'll change ip address, gateway and netmask
|
||||
* param local_ip: Static ip configuration
|
||||
* param gateway: Static gateway configuration
|
||||
* param subnet: Static subnet mask configuration
|
||||
*/
|
||||
static void config(uint8_t validParams, uint32_t local_ip, uint32_t gateway, uint32_t subnet);
|
||||
|
||||
/* Set DNS ip configuration
|
||||
*
|
||||
* param validParams: set the number of parameters that we want to change
|
||||
* i.e. validParams = 1 means that we'll change only dns_server1
|
||||
* validParams = 2 means that we'll change dns_server1 and dns_server2
|
||||
* param dns_server1: Static DNS server1 configuration
|
||||
* param dns_server2: Static DNS server2 configuration
|
||||
*/
|
||||
static void setDNS(uint8_t validParams, uint32_t dns_server1, uint32_t dns_server2);
|
||||
|
||||
/*
|
||||
* Disconnect from the network
|
||||
*
|
||||
|
@ -27,6 +27,8 @@ enum {
|
||||
SET_PASSPHRASE_CMD = 0x11,
|
||||
SET_KEY_CMD = 0x12,
|
||||
TEST_CMD = 0x13,
|
||||
SET_IP_CONFIG_CMD = 0x14,
|
||||
SET_DNS_CONFIG_CMD = 0x15,
|
||||
|
||||
GET_CONN_STATUS_CMD = 0x20,
|
||||
GET_IPADDR_CMD = 0x21,
|
||||
|
Loading…
x
Reference in New Issue
Block a user