1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-01 12:24:14 +01:00

Added WiFi Udp Implementation

This commit is contained in:
Mimmo La Fauci 2013-02-08 19:22:23 +01:00
parent a0c96bebdd
commit 863b47ad5e
13 changed files with 1510 additions and 1185 deletions

View File

@ -8,7 +8,7 @@ extern "C" {
} }
// XXX: don't make assumptions about the value of MAX_SOCK_NUM. // XXX: don't make assumptions about the value of MAX_SOCK_NUM.
int16_t WiFiClass::_state[MAX_SOCK_NUM] = { 0, 0, 0, 0 }; int16_t WiFiClass::_state[MAX_SOCK_NUM] = { NA_STATE, NA_STATE, NA_STATE, NA_STATE };
uint16_t WiFiClass::_server_port[MAX_SOCK_NUM] = { 0, 0, 0, 0 }; uint16_t WiFiClass::_server_port[MAX_SOCK_NUM] = { 0, 0, 0, 0 };
WiFiClass::WiFiClass() WiFiClass::WiFiClass()
@ -71,8 +71,7 @@ int WiFiClass::begin(char* ssid, uint8_t key_idx, const char *key)
{ {
delay(WL_DELAY_START_CONNECTION); delay(WL_DELAY_START_CONNECTION);
status = WiFiDrv::getConnectionStatus(); status = WiFiDrv::getConnectionStatus();
} }while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0));
while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0));
}else{ }else{
status = WL_CONNECT_FAILED; status = WL_CONNECT_FAILED;
} }

View File

@ -129,6 +129,7 @@ void WiFiClient::stop() {
return; return;
ServerDrv::stopClient(_sock); ServerDrv::stopClient(_sock);
WiFiClass::_state[_sock] = NA_STATE;
unsigned long start = millis(); unsigned long start = millis();
@ -169,7 +170,7 @@ WiFiClient::operator bool() {
uint8_t WiFiClient::getFirstSocket() uint8_t WiFiClient::getFirstSocket()
{ {
for (int i = 0; i < MAX_SOCK_NUM; i++) { for (int i = 0; i < MAX_SOCK_NUM; i++) {
if (WiFiClass::_state[i] == 0) if (WiFiClass::_state[i] == NA_STATE)
{ {
return i; return i;
} }

View File

@ -21,6 +21,7 @@ void WiFiServer::begin()
{ {
ServerDrv::startServer(_port, _sock); ServerDrv::startServer(_port, _sock);
WiFiClass::_server_port[_sock] = _port; WiFiClass::_server_port[_sock] = _port;
WiFiClass::_state[_sock] = _sock;
} }
} }

163
libraries/WiFi/WiFiUdp.cpp Normal file
View File

@ -0,0 +1,163 @@
extern "C" {
#include "utility/debug.h"
#include "utility/wifi_spi.h"
}
#include <string.h>
#include "server_drv.h"
#include "wifi_drv.h"
#include "WiFi.h"
#include "WiFiUdp.h"
#include "WiFiClient.h"
#include "WiFiServer.h"
/* Constructor */
WiFiUDP::WiFiUDP() : _sock(NO_SOCKET_AVAIL) {}
/* Start WiFiUDP socket, listening at local port PORT */
uint8_t WiFiUDP::begin(uint16_t port) {
uint8_t sock = WiFiClass::getSocket();
if (sock != NO_SOCKET_AVAIL)
{
ServerDrv::startServer(port, sock, UDP_MODE);
WiFiClass::_server_port[sock] = port;
_sock = sock;
_port = port;
return 1;
}
return 0;
}
/* return number of bytes available in the current packet,
will return zero if parsePacket hasn't been called yet */
int WiFiUDP::available() {
if (_sock != NO_SOCKET_AVAIL)
{
return ServerDrv::availData(_sock);
}
return 0;
}
/* Release any resources being used by this WiFiUDP instance */
void WiFiUDP::stop()
{
if (_sock == NO_SOCKET_AVAIL)
return;
ServerDrv::stopClient(_sock);
_sock = NO_SOCKET_AVAIL;
}
int WiFiUDP::beginPacket(const char *host, uint16_t port)
{
// Look up the host first
int ret = 0;
IPAddress remote_addr;
if (WiFi.hostByName(host, remote_addr))
{
return beginPacket(remote_addr, port);
}
return ret;
}
int WiFiUDP::beginPacket(IPAddress ip, uint16_t port)
{
if (_sock == NO_SOCKET_AVAIL)
_sock = WiFiClass::getSocket();
if (_sock != NO_SOCKET_AVAIL)
{
ServerDrv::startClient(uint32_t(ip), port, _sock, UDP_MODE);
WiFiClass::_state[_sock] = _sock;
return 1;
}
return 0;
}
int WiFiUDP::endPacket()
{
return ServerDrv::sendUdpData(_sock);
}
size_t WiFiUDP::write(uint8_t byte)
{
return write(&byte, 1);
}
size_t WiFiUDP::write(const uint8_t *buffer, size_t size)
{
ServerDrv::insertDataBuf(_sock, buffer, size);
return size;
}
int WiFiUDP::parsePacket()
{
return available();
}
int WiFiUDP::read()
{
uint8_t b;
if (available())
{
ServerDrv::getData(_sock, &b);
return b;
}else{
return -1;
}
}
int WiFiUDP::read(unsigned char* buffer, size_t len)
{
if (available())
{
size_t size = 0;
if (!ServerDrv::getDataBuf(_sock, buffer, &size))
return -1;
// TODO check if the buffer is too smal respect to buffer size
return size;
}else{
return -1;
}
}
int WiFiUDP::peek()
{
uint8_t b;
if (!available())
return -1;
ServerDrv::getData(_sock, &b, 1);
return b;
}
void WiFiUDP::flush()
{
while (available())
read();
}
IPAddress WiFiUDP::remoteIP()
{
uint8_t _remoteIp[4] = {0};
uint8_t _remotePort[2] = {0};
WiFiDrv::getRemoteData(_sock, _remoteIp, _remotePort);
IPAddress ip(_remoteIp);
return ip;
}
uint16_t WiFiUDP::remotePort()
{
uint8_t _remoteIp[4] = {0};
uint8_t _remotePort[2] = {0};
WiFiDrv::getRemoteData(_sock, _remoteIp, _remotePort);
uint16_t port = (_remotePort[0]<<8)+_remotePort[1];
return port;
}

61
libraries/WiFi/WiFiUdp.h Normal file
View File

@ -0,0 +1,61 @@
#ifndef wifiudp_h
#define wifiudp_h
#include <Udp.h>
#define UDP_TX_PACKET_MAX_SIZE 24
class WiFiUDP : public UDP {
private:
uint8_t _sock; // socket ID for Wiz5100
uint16_t _port; // local port to listen on
public:
WiFiUDP(); // Constructor
virtual uint8_t begin(uint16_t); // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
virtual void stop(); // Finish with the UDP socket
// Sending UDP packets
// Start building up a packet to send to the remote host specific in ip and port
// Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
virtual int beginPacket(IPAddress ip, uint16_t port);
// Start building up a packet to send to the remote host specific in host and port
// Returns 1 if successful, 0 if there was a problem resolving the hostname or port
virtual int beginPacket(const char *host, uint16_t port);
// Finish off this packet and send it
// Returns 1 if the packet was sent successfully, 0 if there was an error
virtual int endPacket();
// Write a single byte into the packet
virtual size_t write(uint8_t);
// Write size bytes from buffer into the packet
virtual size_t write(const uint8_t *buffer, size_t size);
using Print::write;
// Start processing the next available incoming packet
// Returns the size of the packet in bytes, or 0 if no packets are available
virtual int parsePacket();
// Number of bytes remaining in the current packet
virtual int available();
// Read a single byte from the current packet
virtual int read();
// Read up to len bytes from the current packet and place them into buffer
// Returns the number of bytes read, or 0 if none are available
virtual int read(unsigned char* buffer, size_t len);
// Read up to len characters from the current packet and place them into buffer
// Returns the number of characters read, or 0 if none are available
virtual int read(char* buffer, size_t len) { return read((unsigned char*)buffer, len); };
// Return the next byte from the current packet without moving on to the next byte
virtual int peek();
virtual void flush(); // Finish reading the current packet
// Return the IP address of the host who sent the current incoming packet
virtual IPAddress remoteIP();
// Return the port of the host who sent the current incoming packet
virtual uint16_t remotePort();
friend class WiFiDrv;
};
#endif

View File

@ -36,6 +36,12 @@ getResult KEYWORD2
getSocket KEYWORD2 getSocket KEYWORD2
WiFiClient KEYWORD2 WiFiClient KEYWORD2
WiFiServer KEYWORD2 WiFiServer KEYWORD2
WiFiUDP KEYWORD2
beginPacket KEYWORD2
endPacket KEYWORD2
parsePacket KEYWORD2
remoteIP KEYWORD2
remotePort KEYWORD2
####################################### #######################################
# Constants (LITERAL1) # Constants (LITERAL1)

View File

@ -12,13 +12,14 @@ extern "C" {
// Start server TCP on port specified // Start server TCP on port specified
void ServerDrv::startServer(uint16_t port, uint8_t sock) void ServerDrv::startServer(uint16_t port, uint8_t sock, uint8_t protMode)
{ {
WAIT_FOR_SLAVE_SELECT(); WAIT_FOR_SLAVE_SELECT();
// Send Command // Send Command
SpiDrv::sendCmd(START_SERVER_TCP_CMD, PARAM_NUMS_2); SpiDrv::sendCmd(START_SERVER_TCP_CMD, PARAM_NUMS_3);
SpiDrv::sendParam(port); SpiDrv::sendParam(port);
SpiDrv::sendParam(&sock, 1, LAST_PARAM); SpiDrv::sendParam(&sock, 1);
SpiDrv::sendParam(&protMode, 1, LAST_PARAM);
//Wait the reply elaboration //Wait the reply elaboration
SpiDrv::waitForSlaveReady(); SpiDrv::waitForSlaveReady();
@ -34,14 +35,15 @@ void ServerDrv::startServer(uint16_t port, uint8_t sock)
} }
// Start server TCP on port specified // Start server TCP on port specified
void ServerDrv::startClient(uint32_t ipAddress, uint16_t port, uint8_t sock) void ServerDrv::startClient(uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode)
{ {
WAIT_FOR_SLAVE_SELECT(); WAIT_FOR_SLAVE_SELECT();
// Send Command // Send Command
SpiDrv::sendCmd(START_CLIENT_TCP_CMD, PARAM_NUMS_3); SpiDrv::sendCmd(START_CLIENT_TCP_CMD, PARAM_NUMS_4);
SpiDrv::sendParam((uint8_t*)&ipAddress, sizeof(ipAddress)); SpiDrv::sendParam((uint8_t*)&ipAddress, sizeof(ipAddress));
SpiDrv::sendParam(port); SpiDrv::sendParam(port);
SpiDrv::sendParam(&sock, 1, LAST_PARAM); SpiDrv::sendParam(&sock, 1);
SpiDrv::sendParam(&protMode, 1, LAST_PARAM);
//Wait the reply elaboration //Wait the reply elaboration
SpiDrv::waitForSlaveReady(); SpiDrv::waitForSlaveReady();
@ -196,6 +198,57 @@ bool ServerDrv::getDataBuf(uint8_t sock, uint8_t *_data, uint16_t *_dataLen)
return false; return false;
} }
bool ServerDrv::insertDataBuf(uint8_t sock, const uint8_t *data, uint16_t _len)
{
WAIT_FOR_SLAVE_SELECT();
// Send Command
SpiDrv::sendCmd(INSERT_DATABUF_CMD, PARAM_NUMS_2);
SpiDrv::sendBuffer(&sock, sizeof(sock));
SpiDrv::sendBuffer((uint8_t *)data, _len, LAST_PARAM);
//Wait the reply elaboration
SpiDrv::waitForSlaveReady();
// Wait for reply
uint8_t _data = 0;
uint8_t _dataLen = 0;
if (!SpiDrv::waitResponseData8(INSERT_DATABUF_CMD, &_data, &_dataLen))
{
WARN("error waitResponse");
}
SpiDrv::spiSlaveDeselect();
if (_dataLen!=0)
{
return (_data == 1);
}
return false;
}
bool ServerDrv::sendUdpData(uint8_t sock)
{
WAIT_FOR_SLAVE_SELECT();
// Send Command
SpiDrv::sendCmd(SEND_DATA_UDP_CMD, PARAM_NUMS_1);
SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM);
//Wait the reply elaboration
SpiDrv::waitForSlaveReady();
// Wait for reply
uint8_t _data = 0;
uint8_t _dataLen = 0;
if (!SpiDrv::waitResponseData8(SEND_DATA_UDP_CMD, &_data, &_dataLen))
{
WARN("error waitResponse");
}
SpiDrv::spiSlaveDeselect();
if (_dataLen!=0)
{
return (_data == 1);
}
return false;
}
bool ServerDrv::sendData(uint8_t sock, const uint8_t *data, uint16_t len) bool ServerDrv::sendData(uint8_t sock, const uint8_t *data, uint16_t len)
{ {

View File

@ -4,13 +4,16 @@
#include <inttypes.h> #include <inttypes.h>
#include "wifi_spi.h" #include "wifi_spi.h"
typedef enum eProtMode {TCP_MODE, UDP_MODE}tProtMode;
class ServerDrv class ServerDrv
{ {
public: public:
// Start server TCP on port specified
static void startServer(uint16_t port, uint8_t sock);
static void startClient(uint32_t ipAddress, uint16_t port, uint8_t sock); // Start server TCP on port specified
static void startServer(uint16_t port, uint8_t sock, uint8_t protMode=TCP_MODE);
static void startClient(uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode=TCP_MODE);
static void stopClient(uint8_t sock); static void stopClient(uint8_t sock);
@ -22,8 +25,12 @@ public:
static bool getDataBuf(uint8_t sock, uint8_t *data, uint16_t *len); static bool getDataBuf(uint8_t sock, uint8_t *data, uint16_t *len);
static bool insertDataBuf(uint8_t sock, const uint8_t *_data, uint16_t _dataLen);
static bool sendData(uint8_t sock, const uint8_t *data, uint16_t len); static bool sendData(uint8_t sock, const uint8_t *data, uint16_t len);
static bool sendUdpData(uint8_t sock);
static uint8_t availData(uint8_t sock); static uint8_t availData(uint8_t sock);
static uint8_t checkDataSent(uint8_t sock); static uint8_t checkDataSent(uint8_t sock);

View File

@ -4,7 +4,7 @@
#include <inttypes.h> #include <inttypes.h>
#include "wifi_spi.h" #include "wifi_spi.h"
#define SPI_START_CMD_DELAY 12 #define SPI_START_CMD_DELAY 10
#define NO_LAST_PARAM 0 #define NO_LAST_PARAM 0
#define LAST_PARAM 1 #define LAST_PARAM 1

View File

@ -53,6 +53,26 @@ void WiFiDrv::getNetworkData(uint8_t *ip, uint8_t *mask, uint8_t *gwip)
SpiDrv::spiSlaveDeselect(); SpiDrv::spiSlaveDeselect();
} }
void WiFiDrv::getRemoteData(uint8_t sock, uint8_t *ip, uint8_t *port)
{
tParam params[PARAM_NUMS_2] = { {0, (char*)ip}, {0, (char*)port} };
WAIT_FOR_SLAVE_SELECT();
// Send Command
SpiDrv::sendCmd(GET_REMOTE_DATA_CMD, PARAM_NUMS_1);
SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM);
//Wait the reply elaboration
SpiDrv::waitForSlaveReady();
// Wait for reply
SpiDrv::waitResponseParams(GET_REMOTE_DATA_CMD, PARAM_NUMS_2, params);
SpiDrv::spiSlaveDeselect();
}
// Public Methods // Public Methods

View File

@ -4,6 +4,7 @@
#include <inttypes.h> #include <inttypes.h>
#include "wifi_spi.h" #include "wifi_spi.h"
#include "IPAddress.h" #include "IPAddress.h"
#include "../WiFiUdp.h"
// Key index length // Key index length
#define KEY_IDX_LEN 1 #define KEY_IDX_LEN 1
@ -40,6 +41,11 @@ private:
static int getHostByName(IPAddress& aResult); static int getHostByName(IPAddress& aResult);
/*
* Get remote Data information on UDP socket
*/
static void getRemoteData(uint8_t sock, uint8_t *ip, uint8_t *port);
public: public:
/* /*
@ -212,6 +218,8 @@ public:
*/ */
static char* getFwVersion(); static char* getFwVersion();
friend class WiFiUDP;
}; };
extern WiFiDrv wiFiDrv; extern WiFiDrv wiFiDrv;

View File

@ -49,11 +49,15 @@ enum {
GET_HOST_BY_NAME_CMD= 0x35, GET_HOST_BY_NAME_CMD= 0x35,
START_SCAN_NETWORKS = 0x36, START_SCAN_NETWORKS = 0x36,
GET_FW_VERSION_CMD = 0x37, GET_FW_VERSION_CMD = 0x37,
GET_TEST_CMD = 0x38,
SEND_DATA_UDP_CMD = 0x39,
GET_REMOTE_DATA_CMD = 0x3A,
// All command with DATA_FLAG 0x40 send a 16bit Len // All command with DATA_FLAG 0x40 send a 16bit Len
SEND_DATA_TCP_CMD = 0x44, SEND_DATA_TCP_CMD = 0x44,
GET_DATABUF_TCP_CMD = 0x45, GET_DATABUF_TCP_CMD = 0x45,
INSERT_DATABUF_CMD = 0x46,
}; };

View File

@ -22,6 +22,8 @@
#define WL_NETWORKS_LIST_MAXNUM 10 #define WL_NETWORKS_LIST_MAXNUM 10
// Maxmium number of socket // Maxmium number of socket
#define MAX_SOCK_NUM 4 #define MAX_SOCK_NUM 4
// Default state value for Wifi state field
#define NA_STATE -1
//Maximum number of attempts to establish wifi connection //Maximum number of attempts to establish wifi connection
#define WL_MAX_ATTEMPT_CONNECTION 10 #define WL_MAX_ATTEMPT_CONNECTION 10