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

Moved vanilla parts of WiFi library into common source folder.

This commit is contained in:
Cristian Maglie 2013-12-10 18:48:07 +01:00
parent 9d48b52312
commit a2482734b5
6 changed files with 0 additions and 498 deletions

View File

@ -1,181 +0,0 @@
extern "C" {
#include "utility/wl_definitions.h"
#include "utility/wl_types.h"
#include "string.h"
#include "utility/debug.h"
}
#include "WiFi.h"
#include "WiFiClient.h"
#include "WiFiServer.h"
#include "utility/server_drv.h"
uint16_t WiFiClient::_srcport = 1024;
WiFiClient::WiFiClient() : _sock(MAX_SOCK_NUM) {
}
WiFiClient::WiFiClient(uint8_t sock) : _sock(sock) {
}
int WiFiClient::connect(const char* host, uint16_t port) {
IPAddress remote_addr;
if (WiFi.hostByName(host, remote_addr))
{
return connect(remote_addr, port);
}
return 0;
}
int WiFiClient::connect(IPAddress ip, uint16_t port) {
_sock = getFirstSocket();
if (_sock != NO_SOCKET_AVAIL)
{
ServerDrv::startClient(uint32_t(ip), port, _sock);
WiFiClass::_state[_sock] = _sock;
unsigned long start = millis();
// wait 4 second for the connection to close
while (!connected() && millis() - start < 10000)
delay(1);
if (!connected())
{
return 0;
}
}else{
Serial.println("No Socket available");
return 0;
}
return 1;
}
size_t WiFiClient::write(uint8_t b) {
return write(&b, 1);
}
size_t WiFiClient::write(const uint8_t *buf, size_t size) {
if (_sock >= MAX_SOCK_NUM)
{
setWriteError();
return 0;
}
if (size==0)
{
setWriteError();
return 0;
}
if (!ServerDrv::sendData(_sock, buf, size))
{
setWriteError();
return 0;
}
if (!ServerDrv::checkDataSent(_sock))
{
setWriteError();
return 0;
}
return size;
}
int WiFiClient::available() {
if (_sock != 255)
{
return ServerDrv::availData(_sock);
}
return 0;
}
int WiFiClient::read() {
uint8_t b;
if (!available())
return -1;
ServerDrv::getData(_sock, &b);
return b;
}
int WiFiClient::read(uint8_t* buf, size_t size) {
// sizeof(size_t) is architecture dependent
// but we need a 16 bit data type here
uint16_t _size = size;
if (!ServerDrv::getDataBuf(_sock, buf, &_size))
return -1;
return 0;
}
int WiFiClient::peek() {
uint8_t b;
if (!available())
return -1;
ServerDrv::getData(_sock, &b, 1);
return b;
}
void WiFiClient::flush() {
while (available())
read();
}
void WiFiClient::stop() {
if (_sock == 255)
return;
ServerDrv::stopClient(_sock);
WiFiClass::_state[_sock] = NA_STATE;
int count = 0;
// wait maximum 5 secs for the connection to close
while (status() != CLOSED && ++count < 50)
delay(100);
_sock = 255;
}
uint8_t WiFiClient::connected() {
if (_sock == 255) {
return 0;
} else {
uint8_t s = status();
return !(s == LISTEN || s == CLOSED || s == FIN_WAIT_1 ||
s == FIN_WAIT_2 || s == TIME_WAIT ||
s == SYN_SENT || s== SYN_RCVD ||
(s == CLOSE_WAIT));
}
}
uint8_t WiFiClient::status() {
if (_sock == 255) {
return CLOSED;
} else {
return ServerDrv::getClientState(_sock);
}
}
WiFiClient::operator bool() {
return _sock != 255;
}
// Private Methods
uint8_t WiFiClient::getFirstSocket()
{
for (int i = 0; i < MAX_SOCK_NUM; i++) {
if (WiFiClass::_state[i] == NA_STATE)
{
return i;
}
}
return SOCK_NOT_AVAIL;
}

View File

@ -1,163 +0,0 @@
extern "C" {
#include "utility/debug.h"
#include "utility/wifi_spi.h"
}
#include <string.h>
#include "utility/server_drv.h"
#include "utility/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())
{
uint16_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;
}

View File

@ -1,154 +0,0 @@
#ifndef WiFi_Spi_h
#define WiFi_Spi_h
#include <inttypes.h>
#include "utility/wl_definitions.h"
#define CMD_FLAG 0
#define REPLY_FLAG 1<<7
#define DATA_FLAG 0x40
#define WIFI_SPI_ACK 1
#define WIFI_SPI_ERR 0xFF
#define TIMEOUT_CHAR 1000
//#define MAX_SOCK_NUM 4 /**< Maxmium number of socket */
#define NO_SOCKET_AVAIL 255
#define START_CMD 0xE0
#define END_CMD 0xEE
#define ERR_CMD 0xEF
#define CMD_POS 1 // Position of Command OpCode on SPI stream
#define PARAM_LEN_POS 2 // Position of Param len on SPI stream
enum {
SET_NET_CMD = 0x10,
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,
GET_MACADDR_CMD = 0x22,
GET_CURR_SSID_CMD = 0x23,
GET_CURR_BSSID_CMD = 0x24,
GET_CURR_RSSI_CMD = 0x25,
GET_CURR_ENCT_CMD = 0x26,
SCAN_NETWORKS = 0x27,
START_SERVER_TCP_CMD= 0x28,
GET_STATE_TCP_CMD = 0x29,
DATA_SENT_TCP_CMD = 0x2A,
AVAIL_DATA_TCP_CMD = 0x2B,
GET_DATA_TCP_CMD = 0x2C,
START_CLIENT_TCP_CMD= 0x2D,
STOP_CLIENT_TCP_CMD = 0x2E,
GET_CLIENT_STATE_TCP_CMD= 0x2F,
DISCONNECT_CMD = 0x30,
GET_IDX_SSID_CMD = 0x31,
GET_IDX_RSSI_CMD = 0x32,
GET_IDX_ENCT_CMD = 0x33,
REQ_HOST_BY_NAME_CMD= 0x34,
GET_HOST_BY_NAME_CMD= 0x35,
START_SCAN_NETWORKS = 0x36,
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
SEND_DATA_TCP_CMD = 0x44,
GET_DATABUF_TCP_CMD = 0x45,
INSERT_DATABUF_CMD = 0x46,
};
enum wl_tcp_state {
CLOSED = 0,
LISTEN = 1,
SYN_SENT = 2,
SYN_RCVD = 3,
ESTABLISHED = 4,
FIN_WAIT_1 = 5,
FIN_WAIT_2 = 6,
CLOSE_WAIT = 7,
CLOSING = 8,
LAST_ACK = 9,
TIME_WAIT = 10
};
enum numParams{
PARAM_NUMS_0,
PARAM_NUMS_1,
PARAM_NUMS_2,
PARAM_NUMS_3,
PARAM_NUMS_4,
PARAM_NUMS_5,
MAX_PARAM_NUMS
};
#define MAX_PARAMS MAX_PARAM_NUMS-1
#define PARAM_LEN_SIZE 1
typedef struct __attribute__((__packed__))
{
uint8_t paramLen;
char* param;
}tParam;
typedef struct __attribute__((__packed__))
{
uint16_t dataLen;
char* data;
}tDataParam;
typedef struct __attribute__((__packed__))
{
unsigned char cmd;
unsigned char tcmd;
unsigned char nParam;
tParam params[MAX_PARAMS];
}tSpiMsg;
typedef struct __attribute__((__packed__))
{
unsigned char cmd;
unsigned char tcmd;
unsigned char nParam;
tDataParam params[MAX_PARAMS];
}tSpiMsgData;
typedef struct __attribute__((__packed__))
{
unsigned char cmd;
unsigned char tcmd;
//unsigned char totLen;
unsigned char nParam;
}tSpiHdr;
typedef struct __attribute__((__packed__))
{
uint8_t paramLen;
uint32_t param;
}tLongParam;
typedef struct __attribute__((__packed__))
{
uint8_t paramLen;
uint16_t param;
}tIntParam;
typedef struct __attribute__((__packed__))
{
uint8_t paramLen;
uint8_t param;
}tByteParam;
#endif