Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
![]() |
03d7bf833d | ||
![]() |
e7f3a81173 |
@ -37,6 +37,7 @@
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/WiFi}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/Wire}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/SPI}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/Ethernet}""/>
|
||||
</option>
|
||||
<inputType id="it.baeyens.arduino.compiler.cpp.sketch.input.505756577" name="CPP source files" superClass="it.baeyens.arduino.compiler.cpp.sketch.input"/>
|
||||
</tool>
|
||||
@ -56,6 +57,7 @@
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/WiFi}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/Wire}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/SPI}""/>
|
||||
<listOptionValue builtIn="false" value=""${workspace_loc:/MqttMonitor/Ethernet}""/>
|
||||
</option>
|
||||
<inputType id="it.baeyens.arduino.compiler.c.sketch.input.575652065" name="C Source Files" superClass="it.baeyens.arduino.compiler.c.sketch.input"/>
|
||||
</tool>
|
||||
@ -68,16 +70,16 @@
|
||||
</toolChain>
|
||||
</folderInfo>
|
||||
<sourceEntries>
|
||||
<entry excluding="Wire|SPI|WiFi|Adafruit_FT6206|Adafruit-GFX|Libraries/*/?xamples|Streaming|MQTT|TFT_ILI9341|Libraries/*/?xtras|Metro|Touch-Screen|TFTLCD" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
|
||||
<entry excluding="Ethernet|Wire|SPI|WiFi|Adafruit_FT6206|Adafruit-GFX|Libraries/*/?xamples|Streaming|MQTT|TFT_ILI9341|Libraries/*/?xtras|Metro|Touch-Screen|TFTLCD" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name=""/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="Adafruit-GFX"/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="Adafruit_FT6206"/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="Ethernet"/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="MQTT"/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="Metro"/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="SPI"/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="Streaming"/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="TFT_ILI9341"/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="Touch-Screen"/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="WiFi"/>
|
||||
<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="Wire"/>
|
||||
</sourceEntries>
|
||||
</configuration>
|
||||
|
@ -4,7 +4,7 @@
|
||||
<extension point="org.eclipse.cdt.core.LanguageSettingsProvider">
|
||||
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
|
||||
<provider-reference id="org.eclipse.cdt.core.ReferencedProjectsLanguageSettingsProvider" ref="shared-provider"/>
|
||||
<provider class="it.baeyens.arduino.toolchain.ArduinoLanguageProvider" console="false" env-hash="711432436404618007" id="it.baeyens.arduino.languageSettingsProvider" keep-relative-paths="false" name="Arduino Compiler Settings" parameter="${COMMAND} -E -P -v -dD "${INPUTS}"" prefer-non-shared="true">
|
||||
<provider class="it.baeyens.arduino.toolchain.ArduinoLanguageProvider" console="false" env-hash="-416521856298057376" id="it.baeyens.arduino.languageSettingsProvider" keep-relative-paths="false" name="Arduino Compiler Settings" parameter="${COMMAND} -E -P -v -dD "${INPUTS}"" prefer-non-shared="true">
|
||||
<language-scope id="org.eclipse.cdt.core.gcc"/>
|
||||
<language-scope id="org.eclipse.cdt.core.g++"/>
|
||||
</provider>
|
||||
|
@ -166,10 +166,10 @@ environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/A.EXTRA
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/A.EXTRA.TIME.DTS/value=3600
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/A.EXTRA.TIME.LOCAL/delimiter=\:
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/A.EXTRA.TIME.LOCAL/operation=replace
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/A.EXTRA.TIME.LOCAL/value=1477784076
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/A.EXTRA.TIME.LOCAL/value=1477787767
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/A.EXTRA.TIME.UTC/delimiter=\:
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/A.EXTRA.TIME.UTC/operation=replace
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/A.EXTRA.TIME.UTC/value=1477776876
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/A.EXTRA.TIME.UTC/value=1477780567
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/A.EXTRA.TIME.ZONE/delimiter=\:
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/A.EXTRA.TIME.ZONE/operation=replace
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/A.EXTRA.TIME.ZONE/value=3600
|
||||
@ -439,7 +439,7 @@ environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/JANTJE.
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/JANTJE.BUILD_VARIANT/value=mega
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/JANTJE.COM_PORT/delimiter=\:
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/JANTJE.COM_PORT/operation=replace
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/JANTJE.COM_PORT/value=/dev/tty.usbmodemFD111
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/JANTJE.COM_PORT/value=/dev/ttyACM0
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/JANTJE.COM_PROGMR/delimiter=\:
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/JANTJE.COM_PROGMR/operation=replace
|
||||
environment/project/it.baeyens.arduino.core.toolChain.release.1898938335/JANTJE.COM_PROGMR/value=Default
|
||||
|
471
Ethernet/Dhcp.cpp
Normal file
471
Ethernet/Dhcp.cpp
Normal file
@ -0,0 +1,471 @@
|
||||
// DHCP Library v0.3 - April 25, 2009
|
||||
// Author: Jordan Terrell - blog.jordanterrell.com
|
||||
|
||||
#include "utility/w5100.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "Dhcp.h"
|
||||
#include "Arduino.h"
|
||||
#include "utility/util.h"
|
||||
|
||||
int DhcpClass::beginWithDHCP(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout)
|
||||
{
|
||||
_dhcpLeaseTime=0;
|
||||
_dhcpT1=0;
|
||||
_dhcpT2=0;
|
||||
_timeout = timeout;
|
||||
_responseTimeout = responseTimeout;
|
||||
|
||||
// zero out _dhcpMacAddr
|
||||
memset(_dhcpMacAddr, 0, 6);
|
||||
reset_DHCP_lease();
|
||||
|
||||
memcpy((void*)_dhcpMacAddr, (void*)mac, 6);
|
||||
_dhcp_state = STATE_DHCP_START;
|
||||
return request_DHCP_lease();
|
||||
}
|
||||
|
||||
void DhcpClass::reset_DHCP_lease(){
|
||||
// zero out _dhcpSubnetMask, _dhcpGatewayIp, _dhcpLocalIp, _dhcpDhcpServerIp, _dhcpDnsServerIp
|
||||
memset(_dhcpLocalIp, 0, 20);
|
||||
}
|
||||
|
||||
//return:0 on error, 1 if request is sent and response is received
|
||||
int DhcpClass::request_DHCP_lease(){
|
||||
|
||||
uint8_t messageType = 0;
|
||||
|
||||
|
||||
|
||||
// Pick an initial transaction ID
|
||||
_dhcpTransactionId = random(1UL, 2000UL);
|
||||
_dhcpInitialTransactionId = _dhcpTransactionId;
|
||||
|
||||
_dhcpUdpSocket.stop();
|
||||
if (_dhcpUdpSocket.begin(DHCP_CLIENT_PORT) == 0)
|
||||
{
|
||||
// Couldn't get a socket
|
||||
return 0;
|
||||
}
|
||||
|
||||
presend_DHCP();
|
||||
|
||||
int result = 0;
|
||||
|
||||
unsigned long startTime = millis();
|
||||
|
||||
while(_dhcp_state != STATE_DHCP_LEASED)
|
||||
{
|
||||
if(_dhcp_state == STATE_DHCP_START)
|
||||
{
|
||||
_dhcpTransactionId++;
|
||||
|
||||
send_DHCP_MESSAGE(DHCP_DISCOVER, ((millis() - startTime) / 1000));
|
||||
_dhcp_state = STATE_DHCP_DISCOVER;
|
||||
}
|
||||
else if(_dhcp_state == STATE_DHCP_REREQUEST){
|
||||
_dhcpTransactionId++;
|
||||
send_DHCP_MESSAGE(DHCP_REQUEST, ((millis() - startTime)/1000));
|
||||
_dhcp_state = STATE_DHCP_REQUEST;
|
||||
}
|
||||
else if(_dhcp_state == STATE_DHCP_DISCOVER)
|
||||
{
|
||||
uint32_t respId;
|
||||
messageType = parseDHCPResponse(_responseTimeout, respId);
|
||||
if(messageType == DHCP_OFFER)
|
||||
{
|
||||
// We'll use the transaction ID that the offer came with,
|
||||
// rather than the one we were up to
|
||||
_dhcpTransactionId = respId;
|
||||
send_DHCP_MESSAGE(DHCP_REQUEST, ((millis() - startTime) / 1000));
|
||||
_dhcp_state = STATE_DHCP_REQUEST;
|
||||
}
|
||||
}
|
||||
else if(_dhcp_state == STATE_DHCP_REQUEST)
|
||||
{
|
||||
uint32_t respId;
|
||||
messageType = parseDHCPResponse(_responseTimeout, respId);
|
||||
if(messageType == DHCP_ACK)
|
||||
{
|
||||
_dhcp_state = STATE_DHCP_LEASED;
|
||||
result = 1;
|
||||
//use default lease time if we didn't get it
|
||||
if(_dhcpLeaseTime == 0){
|
||||
_dhcpLeaseTime = DEFAULT_LEASE;
|
||||
}
|
||||
// Calculate T1 & T2 if we didn't get it
|
||||
if(_dhcpT1 == 0){
|
||||
// T1 should be 50% of _dhcpLeaseTime
|
||||
_dhcpT1 = _dhcpLeaseTime >> 1;
|
||||
}
|
||||
if(_dhcpT2 == 0){
|
||||
// T2 should be 87.5% (7/8ths) of _dhcpLeaseTime
|
||||
_dhcpT2 = _dhcpLeaseTime - (_dhcpLeaseTime >> 3);
|
||||
}
|
||||
_renewInSec = _dhcpT1;
|
||||
_rebindInSec = _dhcpT2;
|
||||
}
|
||||
else if(messageType == DHCP_NAK)
|
||||
_dhcp_state = STATE_DHCP_START;
|
||||
}
|
||||
|
||||
if(messageType == 255)
|
||||
{
|
||||
messageType = 0;
|
||||
_dhcp_state = STATE_DHCP_START;
|
||||
}
|
||||
|
||||
if(result != 1 && ((millis() - startTime) > _timeout))
|
||||
break;
|
||||
}
|
||||
|
||||
// We're done with the socket now
|
||||
_dhcpUdpSocket.stop();
|
||||
_dhcpTransactionId++;
|
||||
|
||||
_lastCheckLeaseMillis = millis();
|
||||
return result;
|
||||
}
|
||||
|
||||
void DhcpClass::presend_DHCP()
|
||||
{
|
||||
}
|
||||
|
||||
void DhcpClass::send_DHCP_MESSAGE(uint8_t messageType, uint16_t secondsElapsed)
|
||||
{
|
||||
uint8_t buffer[32];
|
||||
memset(buffer, 0, 32);
|
||||
IPAddress dest_addr( 255, 255, 255, 255 ); // Broadcast address
|
||||
|
||||
if (-1 == _dhcpUdpSocket.beginPacket(dest_addr, DHCP_SERVER_PORT))
|
||||
{
|
||||
// FIXME Need to return errors
|
||||
return;
|
||||
}
|
||||
|
||||
buffer[0] = DHCP_BOOTREQUEST; // op
|
||||
buffer[1] = DHCP_HTYPE10MB; // htype
|
||||
buffer[2] = DHCP_HLENETHERNET; // hlen
|
||||
buffer[3] = DHCP_HOPS; // hops
|
||||
|
||||
// xid
|
||||
unsigned long xid = htonl(_dhcpTransactionId);
|
||||
memcpy(buffer + 4, &(xid), 4);
|
||||
|
||||
// 8, 9 - seconds elapsed
|
||||
buffer[8] = ((secondsElapsed & 0xff00) >> 8);
|
||||
buffer[9] = (secondsElapsed & 0x00ff);
|
||||
|
||||
// flags
|
||||
unsigned short flags = htons(DHCP_FLAGSBROADCAST);
|
||||
memcpy(buffer + 10, &(flags), 2);
|
||||
|
||||
// ciaddr: already zeroed
|
||||
// yiaddr: already zeroed
|
||||
// siaddr: already zeroed
|
||||
// giaddr: already zeroed
|
||||
|
||||
//put data in W5100 transmit buffer
|
||||
_dhcpUdpSocket.write(buffer, 28);
|
||||
|
||||
memset(buffer, 0, 32); // clear local buffer
|
||||
|
||||
memcpy(buffer, _dhcpMacAddr, 6); // chaddr
|
||||
|
||||
//put data in W5100 transmit buffer
|
||||
_dhcpUdpSocket.write(buffer, 16);
|
||||
|
||||
memset(buffer, 0, 32); // clear local buffer
|
||||
|
||||
// leave zeroed out for sname && file
|
||||
// put in W5100 transmit buffer x 6 (192 bytes)
|
||||
|
||||
for(int i = 0; i < 6; i++) {
|
||||
_dhcpUdpSocket.write(buffer, 32);
|
||||
}
|
||||
|
||||
// OPT - Magic Cookie
|
||||
buffer[0] = (uint8_t)((MAGIC_COOKIE >> 24)& 0xFF);
|
||||
buffer[1] = (uint8_t)((MAGIC_COOKIE >> 16)& 0xFF);
|
||||
buffer[2] = (uint8_t)((MAGIC_COOKIE >> 8)& 0xFF);
|
||||
buffer[3] = (uint8_t)(MAGIC_COOKIE& 0xFF);
|
||||
|
||||
// OPT - message type
|
||||
buffer[4] = dhcpMessageType;
|
||||
buffer[5] = 0x01;
|
||||
buffer[6] = messageType; //DHCP_REQUEST;
|
||||
|
||||
// OPT - client identifier
|
||||
buffer[7] = dhcpClientIdentifier;
|
||||
buffer[8] = 0x07;
|
||||
buffer[9] = 0x01;
|
||||
memcpy(buffer + 10, _dhcpMacAddr, 6);
|
||||
|
||||
// OPT - host name
|
||||
buffer[16] = hostName;
|
||||
buffer[17] = strlen(HOST_NAME) + 6; // length of hostname + last 3 bytes of mac address
|
||||
strcpy((char*)&(buffer[18]), HOST_NAME);
|
||||
|
||||
printByte((char*)&(buffer[24]), _dhcpMacAddr[3]);
|
||||
printByte((char*)&(buffer[26]), _dhcpMacAddr[4]);
|
||||
printByte((char*)&(buffer[28]), _dhcpMacAddr[5]);
|
||||
|
||||
//put data in W5100 transmit buffer
|
||||
_dhcpUdpSocket.write(buffer, 30);
|
||||
|
||||
if(messageType == DHCP_REQUEST)
|
||||
{
|
||||
buffer[0] = dhcpRequestedIPaddr;
|
||||
buffer[1] = 0x04;
|
||||
buffer[2] = _dhcpLocalIp[0];
|
||||
buffer[3] = _dhcpLocalIp[1];
|
||||
buffer[4] = _dhcpLocalIp[2];
|
||||
buffer[5] = _dhcpLocalIp[3];
|
||||
|
||||
buffer[6] = dhcpServerIdentifier;
|
||||
buffer[7] = 0x04;
|
||||
buffer[8] = _dhcpDhcpServerIp[0];
|
||||
buffer[9] = _dhcpDhcpServerIp[1];
|
||||
buffer[10] = _dhcpDhcpServerIp[2];
|
||||
buffer[11] = _dhcpDhcpServerIp[3];
|
||||
|
||||
//put data in W5100 transmit buffer
|
||||
_dhcpUdpSocket.write(buffer, 12);
|
||||
}
|
||||
|
||||
buffer[0] = dhcpParamRequest;
|
||||
buffer[1] = 0x06;
|
||||
buffer[2] = subnetMask;
|
||||
buffer[3] = routersOnSubnet;
|
||||
buffer[4] = dns;
|
||||
buffer[5] = domainName;
|
||||
buffer[6] = dhcpT1value;
|
||||
buffer[7] = dhcpT2value;
|
||||
buffer[8] = endOption;
|
||||
|
||||
//put data in W5100 transmit buffer
|
||||
_dhcpUdpSocket.write(buffer, 9);
|
||||
|
||||
_dhcpUdpSocket.endPacket();
|
||||
}
|
||||
|
||||
uint8_t DhcpClass::parseDHCPResponse(unsigned long responseTimeout, uint32_t& transactionId)
|
||||
{
|
||||
uint8_t type = 0;
|
||||
uint8_t opt_len = 0;
|
||||
|
||||
unsigned long startTime = millis();
|
||||
|
||||
while(_dhcpUdpSocket.parsePacket() <= 0)
|
||||
{
|
||||
if((millis() - startTime) > responseTimeout)
|
||||
{
|
||||
return 255;
|
||||
}
|
||||
delay(50);
|
||||
}
|
||||
// start reading in the packet
|
||||
RIP_MSG_FIXED fixedMsg;
|
||||
_dhcpUdpSocket.read((uint8_t*)&fixedMsg, sizeof(RIP_MSG_FIXED));
|
||||
|
||||
if(fixedMsg.op == DHCP_BOOTREPLY && _dhcpUdpSocket.remotePort() == DHCP_SERVER_PORT)
|
||||
{
|
||||
transactionId = ntohl(fixedMsg.xid);
|
||||
if(memcmp(fixedMsg.chaddr, _dhcpMacAddr, 6) != 0 || (transactionId < _dhcpInitialTransactionId) || (transactionId > _dhcpTransactionId))
|
||||
{
|
||||
// Need to read the rest of the packet here regardless
|
||||
_dhcpUdpSocket.flush();
|
||||
return 0;
|
||||
}
|
||||
|
||||
memcpy(_dhcpLocalIp, fixedMsg.yiaddr, 4);
|
||||
|
||||
// Skip to the option part
|
||||
// Doing this a byte at a time so we don't have to put a big buffer
|
||||
// on the stack (as we don't have lots of memory lying around)
|
||||
for (int i =0; i < (240 - (int)sizeof(RIP_MSG_FIXED)); i++)
|
||||
{
|
||||
_dhcpUdpSocket.read(); // we don't care about the returned byte
|
||||
}
|
||||
|
||||
while (_dhcpUdpSocket.available() > 0)
|
||||
{
|
||||
switch (_dhcpUdpSocket.read())
|
||||
{
|
||||
case endOption :
|
||||
break;
|
||||
|
||||
case padOption :
|
||||
break;
|
||||
|
||||
case dhcpMessageType :
|
||||
opt_len = _dhcpUdpSocket.read();
|
||||
type = _dhcpUdpSocket.read();
|
||||
break;
|
||||
|
||||
case subnetMask :
|
||||
opt_len = _dhcpUdpSocket.read();
|
||||
_dhcpUdpSocket.read(_dhcpSubnetMask, 4);
|
||||
break;
|
||||
|
||||
case routersOnSubnet :
|
||||
opt_len = _dhcpUdpSocket.read();
|
||||
_dhcpUdpSocket.read(_dhcpGatewayIp, 4);
|
||||
for (int i = 0; i < opt_len-4; i++)
|
||||
{
|
||||
_dhcpUdpSocket.read();
|
||||
}
|
||||
break;
|
||||
|
||||
case dns :
|
||||
opt_len = _dhcpUdpSocket.read();
|
||||
_dhcpUdpSocket.read(_dhcpDnsServerIp, 4);
|
||||
for (int i = 0; i < opt_len-4; i++)
|
||||
{
|
||||
_dhcpUdpSocket.read();
|
||||
}
|
||||
break;
|
||||
|
||||
case dhcpServerIdentifier :
|
||||
opt_len = _dhcpUdpSocket.read();
|
||||
if ((_dhcpDhcpServerIp[0] == 0 && _dhcpDhcpServerIp[1] == 0 &&
|
||||
_dhcpDhcpServerIp[2] == 0 && _dhcpDhcpServerIp[3] == 0) ||
|
||||
IPAddress(_dhcpDhcpServerIp) == _dhcpUdpSocket.remoteIP())
|
||||
{
|
||||
_dhcpUdpSocket.read(_dhcpDhcpServerIp, sizeof(_dhcpDhcpServerIp));
|
||||
}
|
||||
else
|
||||
{
|
||||
// Skip over the rest of this option
|
||||
while (opt_len--)
|
||||
{
|
||||
_dhcpUdpSocket.read();
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case dhcpT1value :
|
||||
opt_len = _dhcpUdpSocket.read();
|
||||
_dhcpUdpSocket.read((uint8_t*)&_dhcpT1, sizeof(_dhcpT1));
|
||||
_dhcpT1 = ntohl(_dhcpT1);
|
||||
break;
|
||||
|
||||
case dhcpT2value :
|
||||
opt_len = _dhcpUdpSocket.read();
|
||||
_dhcpUdpSocket.read((uint8_t*)&_dhcpT2, sizeof(_dhcpT2));
|
||||
_dhcpT2 = ntohl(_dhcpT2);
|
||||
break;
|
||||
|
||||
case dhcpIPaddrLeaseTime :
|
||||
opt_len = _dhcpUdpSocket.read();
|
||||
_dhcpUdpSocket.read((uint8_t*)&_dhcpLeaseTime, sizeof(_dhcpLeaseTime));
|
||||
_dhcpLeaseTime = ntohl(_dhcpLeaseTime);
|
||||
_renewInSec = _dhcpLeaseTime;
|
||||
break;
|
||||
|
||||
default :
|
||||
opt_len = _dhcpUdpSocket.read();
|
||||
// Skip over the rest of this option
|
||||
while (opt_len--)
|
||||
{
|
||||
_dhcpUdpSocket.read();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Need to skip to end of the packet regardless here
|
||||
_dhcpUdpSocket.flush();
|
||||
|
||||
return type;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
returns:
|
||||
0/DHCP_CHECK_NONE: nothing happened
|
||||
1/DHCP_CHECK_RENEW_FAIL: renew failed
|
||||
2/DHCP_CHECK_RENEW_OK: renew success
|
||||
3/DHCP_CHECK_REBIND_FAIL: rebind fail
|
||||
4/DHCP_CHECK_REBIND_OK: rebind success
|
||||
*/
|
||||
int DhcpClass::checkLease(){
|
||||
int rc = DHCP_CHECK_NONE;
|
||||
|
||||
unsigned long now = millis();
|
||||
unsigned long elapsed = now - _lastCheckLeaseMillis;
|
||||
|
||||
// if more then one sec passed, reduce the counters accordingly
|
||||
if (elapsed >= 1000) {
|
||||
// set the new timestamps
|
||||
_lastCheckLeaseMillis = now - (elapsed % 1000);
|
||||
elapsed = elapsed / 1000;
|
||||
|
||||
// decrease the counters by elapsed seconds
|
||||
// we assume that the cycle time (elapsed) is fairly constant
|
||||
// if the remainder is less than cycle time * 2
|
||||
// do it early instead of late
|
||||
if (_renewInSec < elapsed * 2)
|
||||
_renewInSec = 0;
|
||||
else
|
||||
_renewInSec -= elapsed;
|
||||
|
||||
if (_rebindInSec < elapsed * 2)
|
||||
_rebindInSec = 0;
|
||||
else
|
||||
_rebindInSec -= elapsed;
|
||||
}
|
||||
|
||||
// if we have a lease but should renew, do it
|
||||
if (_renewInSec == 0 &&_dhcp_state == STATE_DHCP_LEASED) {
|
||||
_dhcp_state = STATE_DHCP_REREQUEST;
|
||||
rc = 1 + request_DHCP_lease();
|
||||
}
|
||||
|
||||
// if we have a lease or is renewing but should bind, do it
|
||||
if (_rebindInSec == 0 && (_dhcp_state == STATE_DHCP_LEASED || _dhcp_state == STATE_DHCP_START)) {
|
||||
// this should basically restart completely
|
||||
_dhcp_state = STATE_DHCP_START;
|
||||
reset_DHCP_lease();
|
||||
rc = 3 + request_DHCP_lease();
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
IPAddress DhcpClass::getLocalIp()
|
||||
{
|
||||
return IPAddress(_dhcpLocalIp);
|
||||
}
|
||||
|
||||
IPAddress DhcpClass::getSubnetMask()
|
||||
{
|
||||
return IPAddress(_dhcpSubnetMask);
|
||||
}
|
||||
|
||||
IPAddress DhcpClass::getGatewayIp()
|
||||
{
|
||||
return IPAddress(_dhcpGatewayIp);
|
||||
}
|
||||
|
||||
IPAddress DhcpClass::getDhcpServerIp()
|
||||
{
|
||||
return IPAddress(_dhcpDhcpServerIp);
|
||||
}
|
||||
|
||||
IPAddress DhcpClass::getDnsServerIp()
|
||||
{
|
||||
return IPAddress(_dhcpDnsServerIp);
|
||||
}
|
||||
|
||||
void DhcpClass::printByte(char * buf, uint8_t n ) {
|
||||
char *str = &buf[1];
|
||||
buf[0]='0';
|
||||
do {
|
||||
unsigned long m = n;
|
||||
n /= 16;
|
||||
char c = m - 16 * n;
|
||||
*str-- = c < 10 ? c + '0' : c + 'A' - 10;
|
||||
} while(n);
|
||||
}
|
177
Ethernet/Dhcp.h
Normal file
177
Ethernet/Dhcp.h
Normal file
@ -0,0 +1,177 @@
|
||||
// DHCP Library v0.3 - April 25, 2009
|
||||
// Author: Jordan Terrell - blog.jordanterrell.com
|
||||
|
||||
#ifndef Dhcp_h
|
||||
#define Dhcp_h
|
||||
|
||||
#include "EthernetUdp.h"
|
||||
|
||||
/* DHCP state machine. */
|
||||
#define STATE_DHCP_START 0
|
||||
#define STATE_DHCP_DISCOVER 1
|
||||
#define STATE_DHCP_REQUEST 2
|
||||
#define STATE_DHCP_LEASED 3
|
||||
#define STATE_DHCP_REREQUEST 4
|
||||
#define STATE_DHCP_RELEASE 5
|
||||
|
||||
#define DHCP_FLAGSBROADCAST 0x8000
|
||||
|
||||
/* UDP port numbers for DHCP */
|
||||
#define DHCP_SERVER_PORT 67 /* from server to client */
|
||||
#define DHCP_CLIENT_PORT 68 /* from client to server */
|
||||
|
||||
/* DHCP message OP code */
|
||||
#define DHCP_BOOTREQUEST 1
|
||||
#define DHCP_BOOTREPLY 2
|
||||
|
||||
/* DHCP message type */
|
||||
#define DHCP_DISCOVER 1
|
||||
#define DHCP_OFFER 2
|
||||
#define DHCP_REQUEST 3
|
||||
#define DHCP_DECLINE 4
|
||||
#define DHCP_ACK 5
|
||||
#define DHCP_NAK 6
|
||||
#define DHCP_RELEASE 7
|
||||
#define DHCP_INFORM 8
|
||||
|
||||
#define DHCP_HTYPE10MB 1
|
||||
#define DHCP_HTYPE100MB 2
|
||||
|
||||
#define DHCP_HLENETHERNET 6
|
||||
#define DHCP_HOPS 0
|
||||
#define DHCP_SECS 0
|
||||
|
||||
#define MAGIC_COOKIE 0x63825363
|
||||
#define MAX_DHCP_OPT 16
|
||||
|
||||
#define HOST_NAME "WIZnet"
|
||||
#define DEFAULT_LEASE (900) //default lease time in seconds
|
||||
|
||||
#define DHCP_CHECK_NONE (0)
|
||||
#define DHCP_CHECK_RENEW_FAIL (1)
|
||||
#define DHCP_CHECK_RENEW_OK (2)
|
||||
#define DHCP_CHECK_REBIND_FAIL (3)
|
||||
#define DHCP_CHECK_REBIND_OK (4)
|
||||
|
||||
enum
|
||||
{
|
||||
padOption = 0,
|
||||
subnetMask = 1,
|
||||
timerOffset = 2,
|
||||
routersOnSubnet = 3,
|
||||
/* timeServer = 4,
|
||||
nameServer = 5,*/
|
||||
dns = 6,
|
||||
/*logServer = 7,
|
||||
cookieServer = 8,
|
||||
lprServer = 9,
|
||||
impressServer = 10,
|
||||
resourceLocationServer = 11,*/
|
||||
hostName = 12,
|
||||
/*bootFileSize = 13,
|
||||
meritDumpFile = 14,*/
|
||||
domainName = 15,
|
||||
/*swapServer = 16,
|
||||
rootPath = 17,
|
||||
extentionsPath = 18,
|
||||
IPforwarding = 19,
|
||||
nonLocalSourceRouting = 20,
|
||||
policyFilter = 21,
|
||||
maxDgramReasmSize = 22,
|
||||
defaultIPTTL = 23,
|
||||
pathMTUagingTimeout = 24,
|
||||
pathMTUplateauTable = 25,
|
||||
ifMTU = 26,
|
||||
allSubnetsLocal = 27,
|
||||
broadcastAddr = 28,
|
||||
performMaskDiscovery = 29,
|
||||
maskSupplier = 30,
|
||||
performRouterDiscovery = 31,
|
||||
routerSolicitationAddr = 32,
|
||||
staticRoute = 33,
|
||||
trailerEncapsulation = 34,
|
||||
arpCacheTimeout = 35,
|
||||
ethernetEncapsulation = 36,
|
||||
tcpDefaultTTL = 37,
|
||||
tcpKeepaliveInterval = 38,
|
||||
tcpKeepaliveGarbage = 39,
|
||||
nisDomainName = 40,
|
||||
nisServers = 41,
|
||||
ntpServers = 42,
|
||||
vendorSpecificInfo = 43,
|
||||
netBIOSnameServer = 44,
|
||||
netBIOSdgramDistServer = 45,
|
||||
netBIOSnodeType = 46,
|
||||
netBIOSscope = 47,
|
||||
xFontServer = 48,
|
||||
xDisplayManager = 49,*/
|
||||
dhcpRequestedIPaddr = 50,
|
||||
dhcpIPaddrLeaseTime = 51,
|
||||
/*dhcpOptionOverload = 52,*/
|
||||
dhcpMessageType = 53,
|
||||
dhcpServerIdentifier = 54,
|
||||
dhcpParamRequest = 55,
|
||||
/*dhcpMsg = 56,
|
||||
dhcpMaxMsgSize = 57,*/
|
||||
dhcpT1value = 58,
|
||||
dhcpT2value = 59,
|
||||
/*dhcpClassIdentifier = 60,*/
|
||||
dhcpClientIdentifier = 61,
|
||||
endOption = 255
|
||||
};
|
||||
|
||||
typedef struct __attribute__((packed)) _RIP_MSG_FIXED
|
||||
{
|
||||
uint8_t op;
|
||||
uint8_t htype;
|
||||
uint8_t hlen;
|
||||
uint8_t hops;
|
||||
uint32_t xid;
|
||||
uint16_t secs;
|
||||
uint16_t flags;
|
||||
uint8_t ciaddr[4];
|
||||
uint8_t yiaddr[4];
|
||||
uint8_t siaddr[4];
|
||||
uint8_t giaddr[4];
|
||||
uint8_t chaddr[6];
|
||||
}RIP_MSG_FIXED;
|
||||
|
||||
class DhcpClass {
|
||||
private:
|
||||
uint32_t _dhcpInitialTransactionId;
|
||||
uint32_t _dhcpTransactionId;
|
||||
uint8_t _dhcpMacAddr[6];
|
||||
uint8_t _dhcpLocalIp[4];
|
||||
uint8_t _dhcpSubnetMask[4];
|
||||
uint8_t _dhcpGatewayIp[4];
|
||||
uint8_t _dhcpDhcpServerIp[4];
|
||||
uint8_t _dhcpDnsServerIp[4];
|
||||
uint32_t _dhcpLeaseTime;
|
||||
uint32_t _dhcpT1, _dhcpT2;
|
||||
unsigned long _renewInSec;
|
||||
unsigned long _rebindInSec;
|
||||
unsigned long _timeout;
|
||||
unsigned long _responseTimeout;
|
||||
unsigned long _lastCheckLeaseMillis;
|
||||
uint8_t _dhcp_state;
|
||||
EthernetUDP _dhcpUdpSocket;
|
||||
|
||||
int request_DHCP_lease();
|
||||
void reset_DHCP_lease();
|
||||
void presend_DHCP();
|
||||
void send_DHCP_MESSAGE(uint8_t, uint16_t);
|
||||
void printByte(char *, uint8_t);
|
||||
|
||||
uint8_t parseDHCPResponse(unsigned long responseTimeout, uint32_t& transactionId);
|
||||
public:
|
||||
IPAddress getLocalIp();
|
||||
IPAddress getSubnetMask();
|
||||
IPAddress getGatewayIp();
|
||||
IPAddress getDhcpServerIp();
|
||||
IPAddress getDnsServerIp();
|
||||
|
||||
int beginWithDHCP(uint8_t *, unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
|
||||
int checkLease();
|
||||
};
|
||||
|
||||
#endif
|
406
Ethernet/Dns.cpp
Normal file
406
Ethernet/Dns.cpp
Normal file
@ -0,0 +1,406 @@
|
||||
// Arduino DNS client for WizNet5100-based Ethernet shield
|
||||
// (c) Copyright 2009-2010 MCQN Ltd.
|
||||
// Released under Apache License, version 2.0
|
||||
|
||||
#include "utility/w5100.h"
|
||||
#include "EthernetUdp.h"
|
||||
#include "utility/util.h"
|
||||
|
||||
#include "Dns.h"
|
||||
#include <string.h>
|
||||
//#include <stdlib.h>
|
||||
#include "Arduino.h"
|
||||
|
||||
|
||||
#define SOCKET_NONE 255
|
||||
// Various flags and header field values for a DNS message
|
||||
#define UDP_HEADER_SIZE 8
|
||||
#define DNS_HEADER_SIZE 12
|
||||
#define TTL_SIZE 4
|
||||
#define QUERY_FLAG (0)
|
||||
#define RESPONSE_FLAG (1<<15)
|
||||
#define QUERY_RESPONSE_MASK (1<<15)
|
||||
#define OPCODE_STANDARD_QUERY (0)
|
||||
#define OPCODE_INVERSE_QUERY (1<<11)
|
||||
#define OPCODE_STATUS_REQUEST (2<<11)
|
||||
#define OPCODE_MASK (15<<11)
|
||||
#define AUTHORITATIVE_FLAG (1<<10)
|
||||
#define TRUNCATION_FLAG (1<<9)
|
||||
#define RECURSION_DESIRED_FLAG (1<<8)
|
||||
#define RECURSION_AVAILABLE_FLAG (1<<7)
|
||||
#define RESP_NO_ERROR (0)
|
||||
#define RESP_FORMAT_ERROR (1)
|
||||
#define RESP_SERVER_FAILURE (2)
|
||||
#define RESP_NAME_ERROR (3)
|
||||
#define RESP_NOT_IMPLEMENTED (4)
|
||||
#define RESP_REFUSED (5)
|
||||
#define RESP_MASK (15)
|
||||
#define TYPE_A (0x0001)
|
||||
#define CLASS_IN (0x0001)
|
||||
#define LABEL_COMPRESSION_MASK (0xC0)
|
||||
// Port number that DNS servers listen on
|
||||
#define DNS_PORT 53
|
||||
|
||||
// Possible return codes from ProcessResponse
|
||||
#define SUCCESS 1
|
||||
#define TIMED_OUT -1
|
||||
#define INVALID_SERVER -2
|
||||
#define TRUNCATED -3
|
||||
#define INVALID_RESPONSE -4
|
||||
|
||||
void DNSClient::begin(const IPAddress& aDNSServer)
|
||||
{
|
||||
iDNSServer = aDNSServer;
|
||||
iRequestId = 0;
|
||||
}
|
||||
|
||||
|
||||
int DNSClient::inet_aton(const char* address, IPAddress& result)
|
||||
{
|
||||
// TODO: add support for "a", "a.b", "a.b.c" formats
|
||||
|
||||
uint16_t acc = 0; // Accumulator
|
||||
uint8_t dots = 0;
|
||||
|
||||
while (*address)
|
||||
{
|
||||
char c = *address++;
|
||||
if (c >= '0' && c <= '9')
|
||||
{
|
||||
acc = acc * 10 + (c - '0');
|
||||
if (acc > 255) {
|
||||
// Value out of [0..255] range
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else if (c == '.')
|
||||
{
|
||||
if (dots == 3) {
|
||||
// Too much dots (there must be 3 dots)
|
||||
return 0;
|
||||
}
|
||||
result[dots++] = acc;
|
||||
acc = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Invalid char
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (dots != 3) {
|
||||
// Too few dots (there must be 3 dots)
|
||||
return 0;
|
||||
}
|
||||
result[3] = acc;
|
||||
return 1;
|
||||
}
|
||||
|
||||
int DNSClient::getHostByName(const char* aHostname, IPAddress& aResult)
|
||||
{
|
||||
int ret =0;
|
||||
|
||||
// See if it's a numeric IP address
|
||||
if (inet_aton(aHostname, aResult))
|
||||
{
|
||||
// It is, our work here is done
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Check we've got a valid DNS server to use
|
||||
if (iDNSServer == INADDR_NONE)
|
||||
{
|
||||
return INVALID_SERVER;
|
||||
}
|
||||
|
||||
// Find a socket to use
|
||||
if (iUdp.begin(1024+(millis() & 0xF)) == 1)
|
||||
{
|
||||
// Try up to three times
|
||||
int retries = 0;
|
||||
// while ((retries < 3) && (ret <= 0))
|
||||
{
|
||||
// Send DNS request
|
||||
ret = iUdp.beginPacket(iDNSServer, DNS_PORT);
|
||||
if (ret != 0)
|
||||
{
|
||||
// Now output the request data
|
||||
ret = BuildRequest(aHostname);
|
||||
if (ret != 0)
|
||||
{
|
||||
// And finally send the request
|
||||
ret = iUdp.endPacket();
|
||||
if (ret != 0)
|
||||
{
|
||||
// Now wait for a response
|
||||
int wait_retries = 0;
|
||||
ret = TIMED_OUT;
|
||||
while ((wait_retries < 3) && (ret == TIMED_OUT))
|
||||
{
|
||||
ret = ProcessResponse(5000, aResult);
|
||||
wait_retries++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
retries++;
|
||||
}
|
||||
|
||||
// We're done with the socket now
|
||||
iUdp.stop();
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint16_t DNSClient::BuildRequest(const char* aName)
|
||||
{
|
||||
// Build header
|
||||
// 1 1 1 1 1 1
|
||||
// 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
|
||||
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
||||
// | ID |
|
||||
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
||||
// |QR| Opcode |AA|TC|RD|RA| Z | RCODE |
|
||||
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
||||
// | QDCOUNT |
|
||||
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
||||
// | ANCOUNT |
|
||||
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
||||
// | NSCOUNT |
|
||||
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
||||
// | ARCOUNT |
|
||||
// +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
|
||||
// As we only support one request at a time at present, we can simplify
|
||||
// some of this header
|
||||
iRequestId = millis(); // generate a random ID
|
||||
uint16_t twoByteBuffer;
|
||||
|
||||
// FIXME We should also check that there's enough space available to write to, rather
|
||||
// FIXME than assume there's enough space (as the code does at present)
|
||||
uint16_t _id = htons(iRequestId);
|
||||
iUdp.write((uint8_t*)&_id, sizeof(_id));
|
||||
|
||||
twoByteBuffer = htons(QUERY_FLAG | OPCODE_STANDARD_QUERY | RECURSION_DESIRED_FLAG);
|
||||
iUdp.write((uint8_t*)&twoByteBuffer, sizeof(twoByteBuffer));
|
||||
|
||||
twoByteBuffer = htons(1); // One question record
|
||||
iUdp.write((uint8_t*)&twoByteBuffer, sizeof(twoByteBuffer));
|
||||
|
||||
twoByteBuffer = 0; // Zero answer records
|
||||
iUdp.write((uint8_t*)&twoByteBuffer, sizeof(twoByteBuffer));
|
||||
|
||||
iUdp.write((uint8_t*)&twoByteBuffer, sizeof(twoByteBuffer));
|
||||
// and zero additional records
|
||||
iUdp.write((uint8_t*)&twoByteBuffer, sizeof(twoByteBuffer));
|
||||
|
||||
// Build question
|
||||
const char* start =aName;
|
||||
const char* end =start;
|
||||
uint8_t len;
|
||||
// Run through the name being requested
|
||||
while (*end)
|
||||
{
|
||||
// Find out how long this section of the name is
|
||||
end = start;
|
||||
while (*end && (*end != '.') )
|
||||
{
|
||||
end++;
|
||||
}
|
||||
|
||||
if (end-start > 0)
|
||||
{
|
||||
// Write out the size of this section
|
||||
len = end-start;
|
||||
iUdp.write(&len, sizeof(len));
|
||||
// And then write out the section
|
||||
iUdp.write((uint8_t*)start, end-start);
|
||||
}
|
||||
start = end+1;
|
||||
}
|
||||
|
||||
// We've got to the end of the question name, so
|
||||
// terminate it with a zero-length section
|
||||
len = 0;
|
||||
iUdp.write(&len, sizeof(len));
|
||||
// Finally the type and class of question
|
||||
twoByteBuffer = htons(TYPE_A);
|
||||
iUdp.write((uint8_t*)&twoByteBuffer, sizeof(twoByteBuffer));
|
||||
|
||||
twoByteBuffer = htons(CLASS_IN); // Internet class of question
|
||||
iUdp.write((uint8_t*)&twoByteBuffer, sizeof(twoByteBuffer));
|
||||
// Success! Everything buffered okay
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
uint16_t DNSClient::ProcessResponse(uint16_t aTimeout, IPAddress& aAddress)
|
||||
{
|
||||
uint32_t startTime = millis();
|
||||
|
||||
// Wait for a response packet
|
||||
while(iUdp.parsePacket() <= 0)
|
||||
{
|
||||
if((millis() - startTime) > aTimeout)
|
||||
return TIMED_OUT;
|
||||
delay(50);
|
||||
}
|
||||
|
||||
// We've had a reply!
|
||||
// Read the UDP header
|
||||
uint8_t header[DNS_HEADER_SIZE]; // Enough space to reuse for the DNS header
|
||||
// Check that it's a response from the right server and the right port
|
||||
if ( (iDNSServer != iUdp.remoteIP()) ||
|
||||
(iUdp.remotePort() != DNS_PORT) )
|
||||
{
|
||||
// It's not from who we expected
|
||||
return INVALID_SERVER;
|
||||
}
|
||||
|
||||
// Read through the rest of the response
|
||||
if (iUdp.available() < DNS_HEADER_SIZE)
|
||||
{
|
||||
return TRUNCATED;
|
||||
}
|
||||
iUdp.read(header, DNS_HEADER_SIZE);
|
||||
|
||||
uint16_t header_flags = word(header[2], header[3]);
|
||||
// Check that it's a response to this request
|
||||
if ( (iRequestId != word(header[0], header[1])) ||
|
||||
((header_flags & QUERY_RESPONSE_MASK) != (uint16_t)RESPONSE_FLAG) )
|
||||
{
|
||||
// Mark the entire packet as read
|
||||
iUdp.flush();
|
||||
return INVALID_RESPONSE;
|
||||
}
|
||||
// Check for any errors in the response (or in our request)
|
||||
// although we don't do anything to get round these
|
||||
if ( (header_flags & TRUNCATION_FLAG) || (header_flags & RESP_MASK) )
|
||||
{
|
||||
// Mark the entire packet as read
|
||||
iUdp.flush();
|
||||
return -5; //INVALID_RESPONSE;
|
||||
}
|
||||
|
||||
// And make sure we've got (at least) one answer
|
||||
uint16_t answerCount = word(header[6], header[7]);
|
||||
if (answerCount == 0 )
|
||||
{
|
||||
// Mark the entire packet as read
|
||||
iUdp.flush();
|
||||
return -6; //INVALID_RESPONSE;
|
||||
}
|
||||
|
||||
// Skip over any questions
|
||||
for (uint16_t i =0; i < word(header[4], header[5]); i++)
|
||||
{
|
||||
// Skip over the name
|
||||
uint8_t len;
|
||||
do
|
||||
{
|
||||
iUdp.read(&len, sizeof(len));
|
||||
if (len > 0)
|
||||
{
|
||||
// Don't need to actually read the data out for the string, just
|
||||
// advance ptr to beyond it
|
||||
while(len--)
|
||||
{
|
||||
iUdp.read(); // we don't care about the returned byte
|
||||
}
|
||||
}
|
||||
} while (len != 0);
|
||||
|
||||
// Now jump over the type and class
|
||||
for (int i =0; i < 4; i++)
|
||||
{
|
||||
iUdp.read(); // we don't care about the returned byte
|
||||
}
|
||||
}
|
||||
|
||||
// Now we're up to the bit we're interested in, the answer
|
||||
// There might be more than one answer (although we'll just use the first
|
||||
// type A answer) and some authority and additional resource records but
|
||||
// we're going to ignore all of them.
|
||||
|
||||
for (uint16_t i =0; i < answerCount; i++)
|
||||
{
|
||||
// Skip the name
|
||||
uint8_t len;
|
||||
do
|
||||
{
|
||||
iUdp.read(&len, sizeof(len));
|
||||
if ((len & LABEL_COMPRESSION_MASK) == 0)
|
||||
{
|
||||
// It's just a normal label
|
||||
if (len > 0)
|
||||
{
|
||||
// And it's got a length
|
||||
// Don't need to actually read the data out for the string,
|
||||
// just advance ptr to beyond it
|
||||
while(len--)
|
||||
{
|
||||
iUdp.read(); // we don't care about the returned byte
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// This is a pointer to a somewhere else in the message for the
|
||||
// rest of the name. We don't care about the name, and RFC1035
|
||||
// says that a name is either a sequence of labels ended with a
|
||||
// 0 length octet or a pointer or a sequence of labels ending in
|
||||
// a pointer. Either way, when we get here we're at the end of
|
||||
// the name
|
||||
// Skip over the pointer
|
||||
iUdp.read(); // we don't care about the returned byte
|
||||
// And set len so that we drop out of the name loop
|
||||
len = 0;
|
||||
}
|
||||
} while (len != 0);
|
||||
|
||||
// Check the type and class
|
||||
uint16_t answerType;
|
||||
uint16_t answerClass;
|
||||
iUdp.read((uint8_t*)&answerType, sizeof(answerType));
|
||||
iUdp.read((uint8_t*)&answerClass, sizeof(answerClass));
|
||||
|
||||
// Ignore the Time-To-Live as we don't do any caching
|
||||
for (int i =0; i < TTL_SIZE; i++)
|
||||
{
|
||||
iUdp.read(); // we don't care about the returned byte
|
||||
}
|
||||
|
||||
// And read out the length of this answer
|
||||
// Don't need header_flags anymore, so we can reuse it here
|
||||
iUdp.read((uint8_t*)&header_flags, sizeof(header_flags));
|
||||
|
||||
if ( (htons(answerType) == TYPE_A) && (htons(answerClass) == CLASS_IN) )
|
||||
{
|
||||
if (htons(header_flags) != 4)
|
||||
{
|
||||
// It's a weird size
|
||||
// Mark the entire packet as read
|
||||
iUdp.flush();
|
||||
return -9;//INVALID_RESPONSE;
|
||||
}
|
||||
iUdp.read(aAddress.raw_address(), 4);
|
||||
return SUCCESS;
|
||||
}
|
||||
else
|
||||
{
|
||||
// This isn't an answer type we're after, move onto the next one
|
||||
for (uint16_t i =0; i < htons(header_flags); i++)
|
||||
{
|
||||
iUdp.read(); // we don't care about the returned byte
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Mark the entire packet as read
|
||||
iUdp.flush();
|
||||
|
||||
// If we get here then we haven't found an answer
|
||||
return -10;//INVALID_RESPONSE;
|
||||
}
|
||||
|
41
Ethernet/Dns.h
Normal file
41
Ethernet/Dns.h
Normal file
@ -0,0 +1,41 @@
|
||||
// Arduino DNS client for WizNet5100-based Ethernet shield
|
||||
// (c) Copyright 2009-2010 MCQN Ltd.
|
||||
// Released under Apache License, version 2.0
|
||||
|
||||
#ifndef DNSClient_h
|
||||
#define DNSClient_h
|
||||
|
||||
#include <EthernetUdp.h>
|
||||
|
||||
class DNSClient
|
||||
{
|
||||
public:
|
||||
// ctor
|
||||
void begin(const IPAddress& aDNSServer);
|
||||
|
||||
/** Convert a numeric IP address string into a four-byte IP address.
|
||||
@param aIPAddrString IP address to convert
|
||||
@param aResult IPAddress structure to store the returned IP address
|
||||
@result 1 if aIPAddrString was successfully converted to an IP address,
|
||||
else error code
|
||||
*/
|
||||
int inet_aton(const char *aIPAddrString, IPAddress& aResult);
|
||||
|
||||
/** Resolve the given hostname to an IP address.
|
||||
@param aHostname Name to be resolved
|
||||
@param aResult IPAddress structure to store the returned IP address
|
||||
@result 1 if aIPAddrString was successfully converted to an IP address,
|
||||
else error code
|
||||
*/
|
||||
int getHostByName(const char* aHostname, IPAddress& aResult);
|
||||
|
||||
protected:
|
||||
uint16_t BuildRequest(const char* aName);
|
||||
uint16_t ProcessResponse(uint16_t aTimeout, IPAddress& aAddress);
|
||||
|
||||
IPAddress iDNSServer;
|
||||
uint16_t iRequestId;
|
||||
EthernetUDP iUdp;
|
||||
};
|
||||
|
||||
#endif
|
136
Ethernet/Ethernet.cpp
Normal file
136
Ethernet/Ethernet.cpp
Normal file
@ -0,0 +1,136 @@
|
||||
#include "utility/w5100.h"
|
||||
#include "Ethernet.h"
|
||||
#include "Dhcp.h"
|
||||
|
||||
// XXX: don't make assumptions about the value of MAX_SOCK_NUM.
|
||||
uint8_t EthernetClass::_state[MAX_SOCK_NUM] = {
|
||||
0, 0, 0, 0 };
|
||||
uint16_t EthernetClass::_server_port[MAX_SOCK_NUM] = {
|
||||
0, 0, 0, 0 };
|
||||
|
||||
int EthernetClass::begin(uint8_t *mac_address, unsigned long timeout, unsigned long responseTimeout)
|
||||
{
|
||||
static DhcpClass s_dhcp;
|
||||
_dhcp = &s_dhcp;
|
||||
|
||||
|
||||
// Initialise the basic info
|
||||
W5100.init();
|
||||
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
W5100.setMACAddress(mac_address);
|
||||
W5100.setIPAddress(IPAddress(0,0,0,0).raw_address());
|
||||
SPI.endTransaction();
|
||||
|
||||
// Now try to get our config info from a DHCP server
|
||||
int ret = _dhcp->beginWithDHCP(mac_address, timeout, responseTimeout);
|
||||
if(ret == 1)
|
||||
{
|
||||
// We've successfully found a DHCP server and got our configuration info, so set things
|
||||
// accordingly
|
||||
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
W5100.setIPAddress(_dhcp->getLocalIp().raw_address());
|
||||
W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address());
|
||||
W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address());
|
||||
SPI.endTransaction();
|
||||
_dnsServerAddress = _dhcp->getDnsServerIp();
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip)
|
||||
{
|
||||
// Assume the DNS server will be the machine on the same network as the local IP
|
||||
// but with last octet being '1'
|
||||
IPAddress dns_server = local_ip;
|
||||
dns_server[3] = 1;
|
||||
begin(mac_address, local_ip, dns_server);
|
||||
}
|
||||
|
||||
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server)
|
||||
{
|
||||
// Assume the gateway will be the machine on the same network as the local IP
|
||||
// but with last octet being '1'
|
||||
IPAddress gateway = local_ip;
|
||||
gateway[3] = 1;
|
||||
begin(mac_address, local_ip, dns_server, gateway);
|
||||
}
|
||||
|
||||
void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway)
|
||||
{
|
||||
IPAddress subnet(255, 255, 255, 0);
|
||||
begin(mac_address, local_ip, dns_server, gateway, subnet);
|
||||
}
|
||||
|
||||
void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
|
||||
{
|
||||
W5100.init();
|
||||
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
W5100.setMACAddress(mac);
|
||||
W5100.setIPAddress(local_ip.raw_address());
|
||||
W5100.setGatewayIp(gateway.raw_address());
|
||||
W5100.setSubnetMask(subnet.raw_address());
|
||||
SPI.endTransaction();
|
||||
_dnsServerAddress = dns_server;
|
||||
}
|
||||
|
||||
int EthernetClass::maintain(){
|
||||
int rc = DHCP_CHECK_NONE;
|
||||
if(_dhcp != NULL){
|
||||
//we have a pointer to dhcp, use it
|
||||
rc = _dhcp->checkLease();
|
||||
switch ( rc ){
|
||||
case DHCP_CHECK_NONE:
|
||||
//nothing done
|
||||
break;
|
||||
case DHCP_CHECK_RENEW_OK:
|
||||
case DHCP_CHECK_REBIND_OK:
|
||||
//we might have got a new IP.
|
||||
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
W5100.setIPAddress(_dhcp->getLocalIp().raw_address());
|
||||
W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address());
|
||||
W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address());
|
||||
SPI.endTransaction();
|
||||
_dnsServerAddress = _dhcp->getDnsServerIp();
|
||||
break;
|
||||
default:
|
||||
//this is actually a error, it will retry though
|
||||
break;
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
IPAddress EthernetClass::localIP()
|
||||
{
|
||||
IPAddress ret;
|
||||
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
W5100.getIPAddress(ret.raw_address());
|
||||
SPI.endTransaction();
|
||||
return ret;
|
||||
}
|
||||
|
||||
IPAddress EthernetClass::subnetMask()
|
||||
{
|
||||
IPAddress ret;
|
||||
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
W5100.getSubnetMask(ret.raw_address());
|
||||
SPI.endTransaction();
|
||||
return ret;
|
||||
}
|
||||
|
||||
IPAddress EthernetClass::gatewayIP()
|
||||
{
|
||||
IPAddress ret;
|
||||
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
W5100.getGatewayIp(ret.raw_address());
|
||||
SPI.endTransaction();
|
||||
return ret;
|
||||
}
|
||||
|
||||
IPAddress EthernetClass::dnsServerIP()
|
||||
{
|
||||
return _dnsServerAddress;
|
||||
}
|
||||
|
||||
EthernetClass Ethernet;
|
41
Ethernet/Ethernet.h
Normal file
41
Ethernet/Ethernet.h
Normal file
@ -0,0 +1,41 @@
|
||||
#ifndef ethernet_h
|
||||
#define ethernet_h
|
||||
|
||||
#include <inttypes.h>
|
||||
//#include "w5100.h"
|
||||
#include "IPAddress.h"
|
||||
#include "EthernetClient.h"
|
||||
#include "EthernetServer.h"
|
||||
#include "Dhcp.h"
|
||||
|
||||
#define MAX_SOCK_NUM 4
|
||||
|
||||
class EthernetClass {
|
||||
private:
|
||||
IPAddress _dnsServerAddress;
|
||||
DhcpClass* _dhcp;
|
||||
public:
|
||||
static uint8_t _state[MAX_SOCK_NUM];
|
||||
static uint16_t _server_port[MAX_SOCK_NUM];
|
||||
// Initialise the Ethernet shield to use the provided MAC address and gain the rest of the
|
||||
// configuration through DHCP.
|
||||
// Returns 0 if the DHCP configuration failed, and 1 if it succeeded
|
||||
int begin(uint8_t *mac_address, unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
|
||||
void begin(uint8_t *mac_address, IPAddress local_ip);
|
||||
void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server);
|
||||
void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway);
|
||||
void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet);
|
||||
int maintain();
|
||||
|
||||
IPAddress localIP();
|
||||
IPAddress subnetMask();
|
||||
IPAddress gatewayIP();
|
||||
IPAddress dnsServerIP();
|
||||
|
||||
friend class EthernetClient;
|
||||
friend class EthernetServer;
|
||||
};
|
||||
|
||||
extern EthernetClass Ethernet;
|
||||
|
||||
#endif
|
177
Ethernet/EthernetClient.cpp
Normal file
177
Ethernet/EthernetClient.cpp
Normal file
@ -0,0 +1,177 @@
|
||||
#include "utility/w5100.h"
|
||||
#include "utility/socket.h"
|
||||
|
||||
extern "C" {
|
||||
#include "string.h"
|
||||
}
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
#include "Ethernet.h"
|
||||
#include "EthernetClient.h"
|
||||
#include "EthernetServer.h"
|
||||
#include "Dns.h"
|
||||
|
||||
uint16_t EthernetClient::_srcport = 49152; //Use IANA recommended ephemeral port range 49152-65535
|
||||
|
||||
EthernetClient::EthernetClient() : _sock(MAX_SOCK_NUM) {
|
||||
}
|
||||
|
||||
EthernetClient::EthernetClient(uint8_t sock) : _sock(sock) {
|
||||
}
|
||||
|
||||
int EthernetClient::connect(const char* host, uint16_t port) {
|
||||
// Look up the host first
|
||||
int ret = 0;
|
||||
DNSClient dns;
|
||||
IPAddress remote_addr;
|
||||
|
||||
dns.begin(Ethernet.dnsServerIP());
|
||||
ret = dns.getHostByName(host, remote_addr);
|
||||
if (ret == 1) {
|
||||
return connect(remote_addr, port);
|
||||
} else {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
int EthernetClient::connect(IPAddress ip, uint16_t port) {
|
||||
if (_sock != MAX_SOCK_NUM)
|
||||
return 0;
|
||||
|
||||
for (int i = 0; i < MAX_SOCK_NUM; i++) {
|
||||
uint8_t s = socketStatus(i);
|
||||
if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT || s == SnSR::CLOSE_WAIT) {
|
||||
_sock = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (_sock == MAX_SOCK_NUM)
|
||||
return 0;
|
||||
|
||||
_srcport++;
|
||||
if (_srcport == 0) _srcport = 49152; //Use IANA recommended ephemeral port range 49152-65535
|
||||
socket(_sock, SnMR::TCP, _srcport, 0);
|
||||
|
||||
if (!::connect(_sock, rawIPAddress(ip), port)) {
|
||||
_sock = MAX_SOCK_NUM;
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (status() != SnSR::ESTABLISHED) {
|
||||
delay(1);
|
||||
if (status() == SnSR::CLOSED) {
|
||||
_sock = MAX_SOCK_NUM;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
size_t EthernetClient::write(uint8_t b) {
|
||||
return write(&b, 1);
|
||||
}
|
||||
|
||||
size_t EthernetClient::write(const uint8_t *buf, size_t size) {
|
||||
if (_sock == MAX_SOCK_NUM) {
|
||||
setWriteError();
|
||||
return 0;
|
||||
}
|
||||
if (!send(_sock, buf, size)) {
|
||||
setWriteError();
|
||||
return 0;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
int EthernetClient::available() {
|
||||
if (_sock != MAX_SOCK_NUM)
|
||||
return recvAvailable(_sock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int EthernetClient::read() {
|
||||
uint8_t b;
|
||||
if ( recv(_sock, &b, 1) > 0 )
|
||||
{
|
||||
// recv worked
|
||||
return b;
|
||||
}
|
||||
else
|
||||
{
|
||||
// No data available
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int EthernetClient::read(uint8_t *buf, size_t size) {
|
||||
return recv(_sock, buf, size);
|
||||
}
|
||||
|
||||
int EthernetClient::peek() {
|
||||
uint8_t b;
|
||||
// Unlike recv, peek doesn't check to see if there's any data available, so we must
|
||||
if (!available())
|
||||
return -1;
|
||||
::peek(_sock, &b);
|
||||
return b;
|
||||
}
|
||||
|
||||
void EthernetClient::flush() {
|
||||
::flush(_sock);
|
||||
}
|
||||
|
||||
void EthernetClient::stop() {
|
||||
if (_sock == MAX_SOCK_NUM)
|
||||
return;
|
||||
|
||||
// attempt to close the connection gracefully (send a FIN to other side)
|
||||
disconnect(_sock);
|
||||
unsigned long start = millis();
|
||||
|
||||
// wait up to a second for the connection to close
|
||||
uint8_t s;
|
||||
do {
|
||||
s = status();
|
||||
if (s == SnSR::CLOSED)
|
||||
break; // exit the loop
|
||||
delay(1);
|
||||
} while (millis() - start < 1000);
|
||||
|
||||
// if it hasn't closed, close it forcefully
|
||||
if (s != SnSR::CLOSED)
|
||||
close(_sock);
|
||||
|
||||
EthernetClass::_server_port[_sock] = 0;
|
||||
_sock = MAX_SOCK_NUM;
|
||||
}
|
||||
|
||||
uint8_t EthernetClient::connected() {
|
||||
if (_sock == MAX_SOCK_NUM) return 0;
|
||||
|
||||
uint8_t s = status();
|
||||
return !(s == SnSR::LISTEN || s == SnSR::CLOSED || s == SnSR::FIN_WAIT ||
|
||||
(s == SnSR::CLOSE_WAIT && !available()));
|
||||
}
|
||||
|
||||
uint8_t EthernetClient::status() {
|
||||
if (_sock == MAX_SOCK_NUM) return SnSR::CLOSED;
|
||||
return socketStatus(_sock);
|
||||
}
|
||||
|
||||
// the next function allows us to use the client returned by
|
||||
// EthernetServer::available() as the condition in an if-statement.
|
||||
|
||||
EthernetClient::operator bool() {
|
||||
return _sock != MAX_SOCK_NUM;
|
||||
}
|
||||
|
||||
bool EthernetClient::operator==(const EthernetClient& rhs) {
|
||||
return _sock == rhs._sock && _sock != MAX_SOCK_NUM && rhs._sock != MAX_SOCK_NUM;
|
||||
}
|
||||
|
||||
uint8_t EthernetClient::getSocketNumber() {
|
||||
return _sock;
|
||||
}
|
42
Ethernet/EthernetClient.h
Normal file
42
Ethernet/EthernetClient.h
Normal file
@ -0,0 +1,42 @@
|
||||
#ifndef ethernetclient_h
|
||||
#define ethernetclient_h
|
||||
#include "Arduino.h"
|
||||
#include "Print.h"
|
||||
#include "Client.h"
|
||||
#include "IPAddress.h"
|
||||
|
||||
class EthernetClient : public Client {
|
||||
|
||||
public:
|
||||
EthernetClient();
|
||||
EthernetClient(uint8_t sock);
|
||||
|
||||
uint8_t status();
|
||||
virtual int connect(IPAddress ip, uint16_t port);
|
||||
virtual int connect(const char *host, uint16_t port);
|
||||
virtual size_t write(uint8_t);
|
||||
virtual size_t write(const uint8_t *buf, size_t size);
|
||||
virtual int available();
|
||||
virtual int read();
|
||||
virtual int read(uint8_t *buf, size_t size);
|
||||
virtual int peek();
|
||||
virtual void flush();
|
||||
virtual void stop();
|
||||
virtual uint8_t connected();
|
||||
virtual operator bool();
|
||||
virtual bool operator==(const bool value) { return bool() == value; }
|
||||
virtual bool operator!=(const bool value) { return bool() != value; }
|
||||
virtual bool operator==(const EthernetClient&);
|
||||
virtual bool operator!=(const EthernetClient& rhs) { return !this->operator==(rhs); };
|
||||
uint8_t getSocketNumber();
|
||||
|
||||
friend class EthernetServer;
|
||||
|
||||
using Print::write;
|
||||
|
||||
private:
|
||||
static uint16_t _srcport;
|
||||
uint8_t _sock;
|
||||
};
|
||||
|
||||
#endif
|
92
Ethernet/EthernetServer.cpp
Normal file
92
Ethernet/EthernetServer.cpp
Normal file
@ -0,0 +1,92 @@
|
||||
#include "utility/w5100.h"
|
||||
#include "utility/socket.h"
|
||||
extern "C" {
|
||||
#include "string.h"
|
||||
}
|
||||
|
||||
#include "Ethernet.h"
|
||||
#include "EthernetClient.h"
|
||||
#include "EthernetServer.h"
|
||||
|
||||
EthernetServer::EthernetServer(uint16_t port)
|
||||
{
|
||||
_port = port;
|
||||
}
|
||||
|
||||
void EthernetServer::begin()
|
||||
{
|
||||
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
|
||||
EthernetClient client(sock);
|
||||
if (client.status() == SnSR::CLOSED) {
|
||||
socket(sock, SnMR::TCP, _port, 0);
|
||||
listen(sock);
|
||||
EthernetClass::_server_port[sock] = _port;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void EthernetServer::accept()
|
||||
{
|
||||
int listening = 0;
|
||||
|
||||
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
|
||||
EthernetClient client(sock);
|
||||
|
||||
if (EthernetClass::_server_port[sock] == _port) {
|
||||
if (client.status() == SnSR::LISTEN) {
|
||||
listening = 1;
|
||||
}
|
||||
else if (client.status() == SnSR::CLOSE_WAIT && !client.available()) {
|
||||
client.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!listening) {
|
||||
begin();
|
||||
}
|
||||
}
|
||||
|
||||
EthernetClient EthernetServer::available()
|
||||
{
|
||||
accept();
|
||||
|
||||
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
|
||||
EthernetClient client(sock);
|
||||
if (EthernetClass::_server_port[sock] == _port) {
|
||||
uint8_t s = client.status();
|
||||
if (s == SnSR::ESTABLISHED || s == SnSR::CLOSE_WAIT) {
|
||||
if (client.available()) {
|
||||
// XXX: don't always pick the lowest numbered socket.
|
||||
return client;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return EthernetClient(MAX_SOCK_NUM);
|
||||
}
|
||||
|
||||
size_t EthernetServer::write(uint8_t b)
|
||||
{
|
||||
return write(&b, 1);
|
||||
}
|
||||
|
||||
size_t EthernetServer::write(const uint8_t *buffer, size_t size)
|
||||
{
|
||||
size_t n = 0;
|
||||
|
||||
accept();
|
||||
|
||||
for (int sock = 0; sock < MAX_SOCK_NUM; sock++) {
|
||||
EthernetClient client(sock);
|
||||
|
||||
if (EthernetClass::_server_port[sock] == _port &&
|
||||
client.status() == SnSR::ESTABLISHED) {
|
||||
n += client.write(buffer, size);
|
||||
}
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
22
Ethernet/EthernetServer.h
Normal file
22
Ethernet/EthernetServer.h
Normal file
@ -0,0 +1,22 @@
|
||||
#ifndef ethernetserver_h
|
||||
#define ethernetserver_h
|
||||
|
||||
#include "Server.h"
|
||||
|
||||
class EthernetClient;
|
||||
|
||||
class EthernetServer :
|
||||
public Server {
|
||||
private:
|
||||
uint16_t _port;
|
||||
void accept();
|
||||
public:
|
||||
EthernetServer(uint16_t);
|
||||
EthernetClient available();
|
||||
virtual void begin();
|
||||
virtual size_t write(uint8_t);
|
||||
virtual size_t write(const uint8_t *buf, size_t size);
|
||||
using Print::write;
|
||||
};
|
||||
|
||||
#endif
|
250
Ethernet/EthernetUdp.cpp
Normal file
250
Ethernet/EthernetUdp.cpp
Normal file
@ -0,0 +1,250 @@
|
||||
/*
|
||||
* Udp.cpp: Library to send/receive UDP packets with the Arduino ethernet shield.
|
||||
* This version only offers minimal wrapping of socket.c/socket.h
|
||||
* Drop Udp.h/.cpp into the Ethernet library directory at hardware/libraries/Ethernet/
|
||||
*
|
||||
* MIT License:
|
||||
* Copyright (c) 2008 Bjoern Hartmann
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* bjoern@cs.stanford.edu 12/30/2008
|
||||
*/
|
||||
|
||||
#include "utility/w5100.h"
|
||||
#include "utility/socket.h"
|
||||
#include "Ethernet.h"
|
||||
#include "Udp.h"
|
||||
#include "Dns.h"
|
||||
|
||||
/* Constructor */
|
||||
EthernetUDP::EthernetUDP() : _sock(MAX_SOCK_NUM) {}
|
||||
|
||||
/* Start EthernetUDP socket, listening at local port PORT */
|
||||
uint8_t EthernetUDP::begin(uint16_t port) {
|
||||
if (_sock != MAX_SOCK_NUM)
|
||||
return 0;
|
||||
|
||||
for (int i = 0; i < MAX_SOCK_NUM; i++) {
|
||||
uint8_t s = socketStatus(i);
|
||||
if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT) {
|
||||
_sock = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (_sock == MAX_SOCK_NUM)
|
||||
return 0;
|
||||
|
||||
_port = port;
|
||||
_remaining = 0;
|
||||
socket(_sock, SnMR::UDP, _port, 0);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* return number of bytes available in the current packet,
|
||||
will return zero if parsePacket hasn't been called yet */
|
||||
int EthernetUDP::available() {
|
||||
return _remaining;
|
||||
}
|
||||
|
||||
/* Release any resources being used by this EthernetUDP instance */
|
||||
void EthernetUDP::stop()
|
||||
{
|
||||
if (_sock == MAX_SOCK_NUM)
|
||||
return;
|
||||
|
||||
close(_sock);
|
||||
|
||||
EthernetClass::_server_port[_sock] = 0;
|
||||
_sock = MAX_SOCK_NUM;
|
||||
}
|
||||
|
||||
int EthernetUDP::beginPacket(const char *host, uint16_t port)
|
||||
{
|
||||
// Look up the host first
|
||||
int ret = 0;
|
||||
DNSClient dns;
|
||||
IPAddress remote_addr;
|
||||
|
||||
dns.begin(Ethernet.dnsServerIP());
|
||||
ret = dns.getHostByName(host, remote_addr);
|
||||
if (ret == 1) {
|
||||
return beginPacket(remote_addr, port);
|
||||
} else {
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
int EthernetUDP::beginPacket(IPAddress ip, uint16_t port)
|
||||
{
|
||||
_offset = 0;
|
||||
return startUDP(_sock, rawIPAddress(ip), port);
|
||||
}
|
||||
|
||||
int EthernetUDP::endPacket()
|
||||
{
|
||||
return sendUDP(_sock);
|
||||
}
|
||||
|
||||
size_t EthernetUDP::write(uint8_t byte)
|
||||
{
|
||||
return write(&byte, 1);
|
||||
}
|
||||
|
||||
size_t EthernetUDP::write(const uint8_t *buffer, size_t size)
|
||||
{
|
||||
uint16_t bytes_written = bufferData(_sock, _offset, buffer, size);
|
||||
_offset += bytes_written;
|
||||
return bytes_written;
|
||||
}
|
||||
|
||||
int EthernetUDP::parsePacket()
|
||||
{
|
||||
// discard any remaining bytes in the last packet
|
||||
while (_remaining) {
|
||||
// could this fail (loop endlessly) if _remaining > 0 and recv in read fails?
|
||||
// should only occur if recv fails after telling us the data is there, lets
|
||||
// hope the w5100 always behaves :)
|
||||
read();
|
||||
}
|
||||
|
||||
if (recvAvailable(_sock) > 0)
|
||||
{
|
||||
//HACK - hand-parse the UDP packet using TCP recv method
|
||||
uint8_t tmpBuf[8];
|
||||
int ret =0;
|
||||
//read 8 header bytes and get IP and port from it
|
||||
ret = recv(_sock,tmpBuf,8);
|
||||
if (ret > 0)
|
||||
{
|
||||
_remoteIP = tmpBuf;
|
||||
_remotePort = tmpBuf[4];
|
||||
_remotePort = (_remotePort << 8) + tmpBuf[5];
|
||||
_remaining = tmpBuf[6];
|
||||
_remaining = (_remaining << 8) + tmpBuf[7];
|
||||
|
||||
// When we get here, any remaining bytes are the data
|
||||
ret = _remaining;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
// There aren't any packets available
|
||||
return 0;
|
||||
}
|
||||
|
||||
int EthernetUDP::read()
|
||||
{
|
||||
uint8_t byte;
|
||||
|
||||
if ((_remaining > 0) && (recv(_sock, &byte, 1) > 0))
|
||||
{
|
||||
// We read things without any problems
|
||||
_remaining--;
|
||||
return byte;
|
||||
}
|
||||
|
||||
// If we get here, there's no data available
|
||||
return -1;
|
||||
}
|
||||
|
||||
int EthernetUDP::read(unsigned char* buffer, size_t len)
|
||||
{
|
||||
|
||||
if (_remaining > 0)
|
||||
{
|
||||
|
||||
int got;
|
||||
|
||||
if (_remaining <= len)
|
||||
{
|
||||
// data should fit in the buffer
|
||||
got = recv(_sock, buffer, _remaining);
|
||||
}
|
||||
else
|
||||
{
|
||||
// too much data for the buffer,
|
||||
// grab as much as will fit
|
||||
got = recv(_sock, buffer, len);
|
||||
}
|
||||
|
||||
if (got > 0)
|
||||
{
|
||||
_remaining -= got;
|
||||
return got;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// If we get here, there's no data available or recv failed
|
||||
return -1;
|
||||
|
||||
}
|
||||
|
||||
int EthernetUDP::peek()
|
||||
{
|
||||
uint8_t b;
|
||||
// Unlike recv, peek doesn't check to see if there's any data available, so we must.
|
||||
// If the user hasn't called parsePacket yet then return nothing otherwise they
|
||||
// may get the UDP header
|
||||
if (!_remaining)
|
||||
return -1;
|
||||
::peek(_sock, &b);
|
||||
return b;
|
||||
}
|
||||
|
||||
void EthernetUDP::flush()
|
||||
{
|
||||
// TODO: we should wait for TX buffer to be emptied
|
||||
}
|
||||
|
||||
/* Start EthernetUDP socket, listening at local port PORT */
|
||||
uint8_t EthernetUDP::beginMulticast(IPAddress ip, uint16_t port)
|
||||
{
|
||||
if (_sock != MAX_SOCK_NUM)
|
||||
return 0;
|
||||
|
||||
for (int i = 0; i < MAX_SOCK_NUM; i++) {
|
||||
uint8_t s = W5100.readSnSR(i);
|
||||
if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT) {
|
||||
_sock = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (_sock == MAX_SOCK_NUM)
|
||||
return 0;
|
||||
|
||||
// Calculate MAC address from Multicast IP Address
|
||||
byte mac[] = { 0x01, 0x00, 0x5E, 0x00, 0x00, 0x00 };
|
||||
|
||||
mac[3] = ip[1] & 0x7F;
|
||||
mac[4] = ip[2];
|
||||
mac[5] = ip[3];
|
||||
|
||||
W5100.writeSnDIPR(_sock, rawIPAddress(ip)); //239.255.0.1
|
||||
W5100.writeSnDPORT(_sock, port);
|
||||
W5100.writeSnDHAR(_sock,mac);
|
||||
|
||||
_remaining = 0;
|
||||
socket(_sock, SnMR::UDP, port, SnMR::MULTI);
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
102
Ethernet/EthernetUdp.h
Normal file
102
Ethernet/EthernetUdp.h
Normal file
@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Udp.cpp: Library to send/receive UDP packets with the Arduino ethernet shield.
|
||||
* This version only offers minimal wrapping of socket.c/socket.h
|
||||
* Drop Udp.h/.cpp into the Ethernet library directory at hardware/libraries/Ethernet/
|
||||
*
|
||||
* NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
|
||||
* 1) UDP does not guarantee the order in which assembled UDP packets are received. This
|
||||
* might not happen often in practice, but in larger network topologies, a UDP
|
||||
* packet can be received out of sequence.
|
||||
* 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
|
||||
* aware of it. Again, this may not be a concern in practice on small local networks.
|
||||
* For more information, see http://www.cafeaulait.org/course/week12/35.html
|
||||
*
|
||||
* MIT License:
|
||||
* Copyright (c) 2008 Bjoern Hartmann
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*
|
||||
* bjoern@cs.stanford.edu 12/30/2008
|
||||
*/
|
||||
|
||||
#ifndef ethernetudp_h
|
||||
#define ethernetudp_h
|
||||
|
||||
#include <Udp.h>
|
||||
|
||||
#define UDP_TX_PACKET_MAX_SIZE 24
|
||||
|
||||
class EthernetUDP : public UDP {
|
||||
private:
|
||||
uint16_t _port; // local port to listen on
|
||||
IPAddress _remoteIP; // remote IP address for the incoming packet whilst it's being processed
|
||||
uint16_t _remotePort; // remote port for the incoming packet whilst it's being processed
|
||||
uint16_t _offset; // offset into the packet being sent
|
||||
|
||||
protected:
|
||||
uint8_t _sock; // socket ID for Wiz5100
|
||||
uint16_t _remaining; // remaining bytes of incoming packet yet to be processed
|
||||
|
||||
public:
|
||||
EthernetUDP(); // 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 uint8_t beginMulticast(IPAddress, 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 _remoteIP; };
|
||||
// Return the port of the host who sent the current incoming packet
|
||||
virtual uint16_t remotePort() { return _remotePort; };
|
||||
};
|
||||
|
||||
#endif
|
469
Ethernet/utility/socket.cpp
Normal file
469
Ethernet/utility/socket.cpp
Normal file
@ -0,0 +1,469 @@
|
||||
#include "w5100.h"
|
||||
#include "socket.h"
|
||||
|
||||
static uint16_t local_port;
|
||||
|
||||
/**
|
||||
* @brief This Socket function initialize the channel in perticular mode, and set the port and wait for W5100 done it.
|
||||
* @return 1 for success else 0.
|
||||
*/
|
||||
uint8_t socket(SOCKET s, uint8_t protocol, uint16_t port, uint8_t flag)
|
||||
{
|
||||
if ((protocol == SnMR::TCP) || (protocol == SnMR::UDP) || (protocol == SnMR::IPRAW) || (protocol == SnMR::MACRAW) || (protocol == SnMR::PPPOE))
|
||||
{
|
||||
close(s);
|
||||
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
W5100.writeSnMR(s, protocol | flag);
|
||||
if (port != 0) {
|
||||
W5100.writeSnPORT(s, port);
|
||||
}
|
||||
else {
|
||||
local_port++; // if don't set the source port, set local_port number.
|
||||
W5100.writeSnPORT(s, local_port);
|
||||
}
|
||||
|
||||
W5100.execCmdSn(s, Sock_OPEN);
|
||||
SPI.endTransaction();
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
uint8_t socketStatus(SOCKET s)
|
||||
{
|
||||
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
uint8_t status = W5100.readSnSR(s);
|
||||
SPI.endTransaction();
|
||||
return status;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief This function close the socket and parameter is "s" which represent the socket number
|
||||
*/
|
||||
void close(SOCKET s)
|
||||
{
|
||||
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
W5100.execCmdSn(s, Sock_CLOSE);
|
||||
W5100.writeSnIR(s, 0xFF);
|
||||
SPI.endTransaction();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief This function established the connection for the channel in passive (server) mode. This function waits for the request from the peer.
|
||||
* @return 1 for success else 0.
|
||||
*/
|
||||
uint8_t listen(SOCKET s)
|
||||
{
|
||||
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
if (W5100.readSnSR(s) != SnSR::INIT) {
|
||||
SPI.endTransaction();
|
||||
return 0;
|
||||
}
|
||||
W5100.execCmdSn(s, Sock_LISTEN);
|
||||
SPI.endTransaction();
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief This function established the connection for the channel in Active (client) mode.
|
||||
* This function waits for the untill the connection is established.
|
||||
*
|
||||
* @return 1 for success else 0.
|
||||
*/
|
||||
uint8_t connect(SOCKET s, uint8_t * addr, uint16_t port)
|
||||
{
|
||||
if
|
||||
(
|
||||
((addr[0] == 0xFF) && (addr[1] == 0xFF) && (addr[2] == 0xFF) && (addr[3] == 0xFF)) ||
|
||||
((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) ||
|
||||
(port == 0x00)
|
||||
)
|
||||
return 0;
|
||||
|
||||
// set destination IP
|
||||
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
W5100.writeSnDIPR(s, addr);
|
||||
W5100.writeSnDPORT(s, port);
|
||||
W5100.execCmdSn(s, Sock_CONNECT);
|
||||
SPI.endTransaction();
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief This function used for disconnect the socket and parameter is "s" which represent the socket number
|
||||
* @return 1 for success else 0.
|
||||
*/
|
||||
void disconnect(SOCKET s)
|
||||
{
|
||||
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
W5100.execCmdSn(s, Sock_DISCON);
|
||||
SPI.endTransaction();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief This function used to send the data in TCP mode
|
||||
* @return 1 for success else 0.
|
||||
*/
|
||||
uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len)
|
||||
{
|
||||
uint8_t status=0;
|
||||
uint16_t ret=0;
|
||||
uint16_t freesize=0;
|
||||
|
||||
if (len > W5100.SSIZE)
|
||||
ret = W5100.SSIZE; // check size not to exceed MAX size.
|
||||
else
|
||||
ret = len;
|
||||
|
||||
// if freebuf is available, start.
|
||||
do
|
||||
{
|
||||
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
freesize = W5100.getTXFreeSize(s);
|
||||
status = W5100.readSnSR(s);
|
||||
SPI.endTransaction();
|
||||
if ((status != SnSR::ESTABLISHED) && (status != SnSR::CLOSE_WAIT))
|
||||
{
|
||||
ret = 0;
|
||||
break;
|
||||
}
|
||||
yield();
|
||||
}
|
||||
while (freesize < ret);
|
||||
|
||||
// copy data
|
||||
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
W5100.send_data_processing(s, (uint8_t *)buf, ret);
|
||||
W5100.execCmdSn(s, Sock_SEND);
|
||||
|
||||
/* +2008.01 bj */
|
||||
while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK )
|
||||
{
|
||||
/* m2008.01 [bj] : reduce code */
|
||||
if ( W5100.readSnSR(s) == SnSR::CLOSED )
|
||||
{
|
||||
SPI.endTransaction();
|
||||
close(s);
|
||||
return 0;
|
||||
}
|
||||
SPI.endTransaction();
|
||||
yield();
|
||||
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
}
|
||||
/* +2008.01 bj */
|
||||
W5100.writeSnIR(s, SnIR::SEND_OK);
|
||||
SPI.endTransaction();
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief This function is an application I/F function which is used to receive the data in TCP mode.
|
||||
* It continues to wait for data as much as the application wants to receive.
|
||||
*
|
||||
* @return received data size for success else -1.
|
||||
*/
|
||||
int16_t recv(SOCKET s, uint8_t *buf, int16_t len)
|
||||
{
|
||||
// Check how much data is available
|
||||
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
int16_t ret = W5100.getRXReceivedSize(s);
|
||||
if ( ret == 0 )
|
||||
{
|
||||
// No data available.
|
||||
uint8_t status = W5100.readSnSR(s);
|
||||
if ( status == SnSR::LISTEN || status == SnSR::CLOSED || status == SnSR::CLOSE_WAIT )
|
||||
{
|
||||
// The remote end has closed its side of the connection, so this is the eof state
|
||||
ret = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
// The connection is still up, but there's no data waiting to be read
|
||||
ret = -1;
|
||||
}
|
||||
}
|
||||
else if (ret > len)
|
||||
{
|
||||
ret = len;
|
||||
}
|
||||
|
||||
if ( ret > 0 )
|
||||
{
|
||||
W5100.recv_data_processing(s, buf, ret);
|
||||
W5100.execCmdSn(s, Sock_RECV);
|
||||
}
|
||||
SPI.endTransaction();
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
int16_t recvAvailable(SOCKET s)
|
||||
{
|
||||
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
int16_t ret = W5100.getRXReceivedSize(s);
|
||||
SPI.endTransaction();
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Returns the first byte in the receive queue (no checking)
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
uint16_t peek(SOCKET s, uint8_t *buf)
|
||||
{
|
||||
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
W5100.recv_data_processing(s, buf, 1, 1);
|
||||
SPI.endTransaction();
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief This function is an application I/F function which is used to send the data for other then TCP mode.
|
||||
* Unlike TCP transmission, The peer's destination address and the port is needed.
|
||||
*
|
||||
* @return This function return send data size for success else -1.
|
||||
*/
|
||||
uint16_t sendto(SOCKET s, const uint8_t *buf, uint16_t len, uint8_t *addr, uint16_t port)
|
||||
{
|
||||
uint16_t ret=0;
|
||||
|
||||
if (len > W5100.SSIZE) ret = W5100.SSIZE; // check size not to exceed MAX size.
|
||||
else ret = len;
|
||||
|
||||
if
|
||||
(
|
||||
((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) ||
|
||||
((port == 0x00)) ||(ret == 0)
|
||||
)
|
||||
{
|
||||
/* +2008.01 [bj] : added return value */
|
||||
ret = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
W5100.writeSnDIPR(s, addr);
|
||||
W5100.writeSnDPORT(s, port);
|
||||
|
||||
// copy data
|
||||
W5100.send_data_processing(s, (uint8_t *)buf, ret);
|
||||
W5100.execCmdSn(s, Sock_SEND);
|
||||
|
||||
/* +2008.01 bj */
|
||||
while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK )
|
||||
{
|
||||
if (W5100.readSnIR(s) & SnIR::TIMEOUT)
|
||||
{
|
||||
/* +2008.01 [bj]: clear interrupt */
|
||||
W5100.writeSnIR(s, (SnIR::SEND_OK | SnIR::TIMEOUT)); /* clear SEND_OK & TIMEOUT */
|
||||
SPI.endTransaction();
|
||||
return 0;
|
||||
}
|
||||
SPI.endTransaction();
|
||||
yield();
|
||||
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
}
|
||||
|
||||
/* +2008.01 bj */
|
||||
W5100.writeSnIR(s, SnIR::SEND_OK);
|
||||
SPI.endTransaction();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief This function is an application I/F function which is used to receive the data in other then
|
||||
* TCP mode. This function is used to receive UDP, IP_RAW and MAC_RAW mode, and handle the header as well.
|
||||
*
|
||||
* @return This function return received data size for success else -1.
|
||||
*/
|
||||
uint16_t recvfrom(SOCKET s, uint8_t *buf, uint16_t len, uint8_t *addr, uint16_t *port)
|
||||
{
|
||||
uint8_t head[8];
|
||||
uint16_t data_len=0;
|
||||
uint16_t ptr=0;
|
||||
|
||||
if ( len > 0 )
|
||||
{
|
||||
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
ptr = W5100.readSnRX_RD(s);
|
||||
switch (W5100.readSnMR(s) & 0x07)
|
||||
{
|
||||
case SnMR::UDP :
|
||||
W5100.read_data(s, ptr, head, 0x08);
|
||||
ptr += 8;
|
||||
// read peer's IP address, port number.
|
||||
addr[0] = head[0];
|
||||
addr[1] = head[1];
|
||||
addr[2] = head[2];
|
||||
addr[3] = head[3];
|
||||
*port = head[4];
|
||||
*port = (*port << 8) + head[5];
|
||||
data_len = head[6];
|
||||
data_len = (data_len << 8) + head[7];
|
||||
|
||||
W5100.read_data(s, ptr, buf, data_len); // data copy.
|
||||
ptr += data_len;
|
||||
|
||||
W5100.writeSnRX_RD(s, ptr);
|
||||
break;
|
||||
|
||||
case SnMR::IPRAW :
|
||||
W5100.read_data(s, ptr, head, 0x06);
|
||||
ptr += 6;
|
||||
|
||||
addr[0] = head[0];
|
||||
addr[1] = head[1];
|
||||
addr[2] = head[2];
|
||||
addr[3] = head[3];
|
||||
data_len = head[4];
|
||||
data_len = (data_len << 8) + head[5];
|
||||
|
||||
W5100.read_data(s, ptr, buf, data_len); // data copy.
|
||||
ptr += data_len;
|
||||
|
||||
W5100.writeSnRX_RD(s, ptr);
|
||||
break;
|
||||
|
||||
case SnMR::MACRAW:
|
||||
W5100.read_data(s, ptr, head, 2);
|
||||
ptr+=2;
|
||||
data_len = head[0];
|
||||
data_len = (data_len<<8) + head[1] - 2;
|
||||
|
||||
W5100.read_data(s, ptr, buf, data_len);
|
||||
ptr += data_len;
|
||||
W5100.writeSnRX_RD(s, ptr);
|
||||
break;
|
||||
|
||||
default :
|
||||
break;
|
||||
}
|
||||
W5100.execCmdSn(s, Sock_RECV);
|
||||
SPI.endTransaction();
|
||||
}
|
||||
return data_len;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Wait for buffered transmission to complete.
|
||||
*/
|
||||
void flush(SOCKET s) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
uint16_t igmpsend(SOCKET s, const uint8_t * buf, uint16_t len)
|
||||
{
|
||||
uint16_t ret=0;
|
||||
|
||||
if (len > W5100.SSIZE)
|
||||
ret = W5100.SSIZE; // check size not to exceed MAX size.
|
||||
else
|
||||
ret = len;
|
||||
|
||||
if (ret == 0)
|
||||
return 0;
|
||||
|
||||
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
W5100.send_data_processing(s, (uint8_t *)buf, ret);
|
||||
W5100.execCmdSn(s, Sock_SEND);
|
||||
|
||||
while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK )
|
||||
{
|
||||
if (W5100.readSnIR(s) & SnIR::TIMEOUT)
|
||||
{
|
||||
/* in case of igmp, if send fails, then socket closed */
|
||||
/* if you want change, remove this code. */
|
||||
SPI.endTransaction();
|
||||
close(s);
|
||||
return 0;
|
||||
}
|
||||
SPI.endTransaction();
|
||||
yield();
|
||||
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
}
|
||||
|
||||
W5100.writeSnIR(s, SnIR::SEND_OK);
|
||||
SPI.endTransaction();
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint16_t bufferData(SOCKET s, uint16_t offset, const uint8_t* buf, uint16_t len)
|
||||
{
|
||||
uint16_t ret =0;
|
||||
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
if (len > W5100.getTXFreeSize(s))
|
||||
{
|
||||
ret = W5100.getTXFreeSize(s); // check size not to exceed MAX size.
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = len;
|
||||
}
|
||||
W5100.send_data_processing_offset(s, offset, buf, ret);
|
||||
SPI.endTransaction();
|
||||
return ret;
|
||||
}
|
||||
|
||||
int startUDP(SOCKET s, uint8_t* addr, uint16_t port)
|
||||
{
|
||||
if
|
||||
(
|
||||
((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) ||
|
||||
((port == 0x00))
|
||||
)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
W5100.writeSnDIPR(s, addr);
|
||||
W5100.writeSnDPORT(s, port);
|
||||
SPI.endTransaction();
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
int sendUDP(SOCKET s)
|
||||
{
|
||||
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
W5100.execCmdSn(s, Sock_SEND);
|
||||
|
||||
/* +2008.01 bj */
|
||||
while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK )
|
||||
{
|
||||
if (W5100.readSnIR(s) & SnIR::TIMEOUT)
|
||||
{
|
||||
/* +2008.01 [bj]: clear interrupt */
|
||||
W5100.writeSnIR(s, (SnIR::SEND_OK|SnIR::TIMEOUT));
|
||||
SPI.endTransaction();
|
||||
return 0;
|
||||
}
|
||||
SPI.endTransaction();
|
||||
yield();
|
||||
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
}
|
||||
|
||||
/* +2008.01 bj */
|
||||
W5100.writeSnIR(s, SnIR::SEND_OK);
|
||||
SPI.endTransaction();
|
||||
|
||||
/* Sent ok */
|
||||
return 1;
|
||||
}
|
||||
|
44
Ethernet/utility/socket.h
Normal file
44
Ethernet/utility/socket.h
Normal file
@ -0,0 +1,44 @@
|
||||
#ifndef _SOCKET_H_
|
||||
#define _SOCKET_H_
|
||||
|
||||
#include "utility/w5100.h"
|
||||
|
||||
extern uint8_t socket(SOCKET s, uint8_t protocol, uint16_t port, uint8_t flag); // Opens a socket(TCP or UDP or IP_RAW mode)
|
||||
extern uint8_t socketStatus(SOCKET s);
|
||||
extern void close(SOCKET s); // Close socket
|
||||
extern uint8_t connect(SOCKET s, uint8_t * addr, uint16_t port); // Establish TCP connection (Active connection)
|
||||
extern void disconnect(SOCKET s); // disconnect the connection
|
||||
extern uint8_t listen(SOCKET s); // Establish TCP connection (Passive connection)
|
||||
extern uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len); // Send data (TCP)
|
||||
extern int16_t recv(SOCKET s, uint8_t * buf, int16_t len); // Receive data (TCP)
|
||||
extern int16_t recvAvailable(SOCKET s);
|
||||
extern uint16_t peek(SOCKET s, uint8_t *buf);
|
||||
extern uint16_t sendto(SOCKET s, const uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t port); // Send data (UDP/IP RAW)
|
||||
extern uint16_t recvfrom(SOCKET s, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t *port); // Receive data (UDP/IP RAW)
|
||||
extern void flush(SOCKET s); // Wait for transmission to complete
|
||||
|
||||
extern uint16_t igmpsend(SOCKET s, const uint8_t * buf, uint16_t len);
|
||||
|
||||
// Functions to allow buffered UDP send (i.e. where the UDP datagram is built up over a
|
||||
// number of calls before being sent
|
||||
/*
|
||||
@brief This function sets up a UDP datagram, the data for which will be provided by one
|
||||
or more calls to bufferData and then finally sent with sendUDP.
|
||||
@return 1 if the datagram was successfully set up, or 0 if there was an error
|
||||
*/
|
||||
extern int startUDP(SOCKET s, uint8_t* addr, uint16_t port);
|
||||
/*
|
||||
@brief This function copies up to len bytes of data from buf into a UDP datagram to be
|
||||
sent later by sendUDP. Allows datagrams to be built up from a series of bufferData calls.
|
||||
@return Number of bytes successfully buffered
|
||||
*/
|
||||
uint16_t bufferData(SOCKET s, uint16_t offset, const uint8_t* buf, uint16_t len);
|
||||
/*
|
||||
@brief Send a UDP datagram built up from a sequence of startUDP followed by one or more
|
||||
calls to bufferData.
|
||||
@return 1 if the datagram was successfully sent, or 0 if there was an error
|
||||
*/
|
||||
int sendUDP(SOCKET s);
|
||||
|
||||
#endif
|
||||
/* _SOCKET_H_ */
|
14
Ethernet/utility/util.h
Normal file
14
Ethernet/utility/util.h
Normal file
@ -0,0 +1,14 @@
|
||||
#ifndef UTIL_H
|
||||
#define UTIL_H
|
||||
|
||||
#define htons(x) ( ((x)<< 8 & 0xFF00) | \
|
||||
((x)>> 8 & 0x00FF) )
|
||||
#define ntohs(x) htons(x)
|
||||
|
||||
#define htonl(x) ( ((x)<<24 & 0xFF000000UL) | \
|
||||
((x)<< 8 & 0x00FF0000UL) | \
|
||||
((x)>> 8 & 0x0000FF00UL) | \
|
||||
((x)>>24 & 0x000000FFUL) )
|
||||
#define ntohl(x) htonl(x)
|
||||
|
||||
#endif
|
225
Ethernet/utility/w5100.cpp
Normal file
225
Ethernet/utility/w5100.cpp
Normal file
@ -0,0 +1,225 @@
|
||||
/*
|
||||
* Copyright (c) 2010 by Arduino LLC. All rights reserved.
|
||||
*
|
||||
* This file is free software; you can redistribute it and/or modify
|
||||
* it under the terms of either the GNU General Public License version 2
|
||||
* or the GNU Lesser General Public License version 2.1, both as
|
||||
* published by the Free Software Foundation.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "w5100.h"
|
||||
|
||||
// W5100 controller instance
|
||||
W5100Class W5100;
|
||||
|
||||
#define TX_RX_MAX_BUF_SIZE 2048
|
||||
#define TX_BUF 0x1100
|
||||
#define RX_BUF (TX_BUF + TX_RX_MAX_BUF_SIZE)
|
||||
|
||||
#define TXBUF_BASE 0x4000
|
||||
#define RXBUF_BASE 0x6000
|
||||
|
||||
void W5100Class::init(void)
|
||||
{
|
||||
delay(300);
|
||||
|
||||
#if !defined(SPI_HAS_EXTENDED_CS_PIN_HANDLING)
|
||||
SPI.begin();
|
||||
initSS();
|
||||
#else
|
||||
SPI.begin(ETHERNET_SHIELD_SPI_CS);
|
||||
// Set clock to 4Mhz (W5100 should support up to about 14Mhz)
|
||||
SPI.setClockDivider(ETHERNET_SHIELD_SPI_CS, 21);
|
||||
SPI.setDataMode(ETHERNET_SHIELD_SPI_CS, SPI_MODE0);
|
||||
#endif
|
||||
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
|
||||
writeMR(1<<RST);
|
||||
writeTMSR(0x55);
|
||||
writeRMSR(0x55);
|
||||
SPI.endTransaction();
|
||||
|
||||
for (int i=0; i<MAX_SOCK_NUM; i++) {
|
||||
SBASE[i] = TXBUF_BASE + SSIZE * i;
|
||||
RBASE[i] = RXBUF_BASE + RSIZE * i;
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t W5100Class::getTXFreeSize(SOCKET s)
|
||||
{
|
||||
uint16_t val=0, val1=0;
|
||||
do {
|
||||
val1 = readSnTX_FSR(s);
|
||||
if (val1 != 0)
|
||||
val = readSnTX_FSR(s);
|
||||
}
|
||||
while (val != val1);
|
||||
return val;
|
||||
}
|
||||
|
||||
uint16_t W5100Class::getRXReceivedSize(SOCKET s)
|
||||
{
|
||||
uint16_t val=0,val1=0;
|
||||
do {
|
||||
val1 = readSnRX_RSR(s);
|
||||
if (val1 != 0)
|
||||
val = readSnRX_RSR(s);
|
||||
}
|
||||
while (val != val1);
|
||||
return val;
|
||||
}
|
||||
|
||||
|
||||
void W5100Class::send_data_processing(SOCKET s, const uint8_t *data, uint16_t len)
|
||||
{
|
||||
// This is same as having no offset in a call to send_data_processing_offset
|
||||
send_data_processing_offset(s, 0, data, len);
|
||||
}
|
||||
|
||||
void W5100Class::send_data_processing_offset(SOCKET s, uint16_t data_offset, const uint8_t *data, uint16_t len)
|
||||
{
|
||||
uint16_t ptr = readSnTX_WR(s);
|
||||
ptr += data_offset;
|
||||
uint16_t offset = ptr & SMASK;
|
||||
uint16_t dstAddr = offset + SBASE[s];
|
||||
|
||||
if (offset + len > SSIZE)
|
||||
{
|
||||
// Wrap around circular buffer
|
||||
uint16_t size = SSIZE - offset;
|
||||
write(dstAddr, data, size);
|
||||
write(SBASE[s], data + size, len - size);
|
||||
}
|
||||
else {
|
||||
write(dstAddr, data, len);
|
||||
}
|
||||
|
||||
ptr += len;
|
||||
writeSnTX_WR(s, ptr);
|
||||
}
|
||||
|
||||
|
||||
void W5100Class::recv_data_processing(SOCKET s, uint8_t *data, uint16_t len, uint8_t peek)
|
||||
{
|
||||
uint16_t ptr;
|
||||
ptr = readSnRX_RD(s);
|
||||
read_data(s, ptr, data, len);
|
||||
if (!peek)
|
||||
{
|
||||
ptr += len;
|
||||
writeSnRX_RD(s, ptr);
|
||||
}
|
||||
}
|
||||
|
||||
void W5100Class::read_data(SOCKET s, volatile uint16_t src, volatile uint8_t *dst, uint16_t len)
|
||||
{
|
||||
uint16_t size;
|
||||
uint16_t src_mask;
|
||||
uint16_t src_ptr;
|
||||
|
||||
src_mask = src & RMASK;
|
||||
src_ptr = RBASE[s] + src_mask;
|
||||
|
||||
if( (src_mask + len) > RSIZE )
|
||||
{
|
||||
size = RSIZE - src_mask;
|
||||
read(src_ptr, (uint8_t *)dst, size);
|
||||
dst += size;
|
||||
read(RBASE[s], (uint8_t *) dst, len - size);
|
||||
}
|
||||
else
|
||||
read(src_ptr, (uint8_t *) dst, len);
|
||||
}
|
||||
|
||||
|
||||
uint8_t W5100Class::write(uint16_t _addr, uint8_t _data)
|
||||
{
|
||||
#if !defined(SPI_HAS_EXTENDED_CS_PIN_HANDLING)
|
||||
setSS();
|
||||
SPI.transfer(0xF0);
|
||||
SPI.transfer(_addr >> 8);
|
||||
SPI.transfer(_addr & 0xFF);
|
||||
SPI.transfer(_data);
|
||||
resetSS();
|
||||
#else
|
||||
SPI.transfer(ETHERNET_SHIELD_SPI_CS, 0xF0, SPI_CONTINUE);
|
||||
SPI.transfer(ETHERNET_SHIELD_SPI_CS, _addr >> 8, SPI_CONTINUE);
|
||||
SPI.transfer(ETHERNET_SHIELD_SPI_CS, _addr & 0xFF, SPI_CONTINUE);
|
||||
SPI.transfer(ETHERNET_SHIELD_SPI_CS, _data);
|
||||
#endif
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint16_t W5100Class::write(uint16_t _addr, const uint8_t *_buf, uint16_t _len)
|
||||
{
|
||||
for (uint16_t i=0; i<_len; i++)
|
||||
{
|
||||
#if !defined(SPI_HAS_EXTENDED_CS_PIN_HANDLING)
|
||||
setSS();
|
||||
SPI.transfer(0xF0);
|
||||
SPI.transfer(_addr >> 8);
|
||||
SPI.transfer(_addr & 0xFF);
|
||||
_addr++;
|
||||
SPI.transfer(_buf[i]);
|
||||
resetSS();
|
||||
#else
|
||||
SPI.transfer(ETHERNET_SHIELD_SPI_CS, 0xF0, SPI_CONTINUE);
|
||||
SPI.transfer(ETHERNET_SHIELD_SPI_CS, _addr >> 8, SPI_CONTINUE);
|
||||
SPI.transfer(ETHERNET_SHIELD_SPI_CS, _addr & 0xFF, SPI_CONTINUE);
|
||||
SPI.transfer(ETHERNET_SHIELD_SPI_CS, _buf[i]);
|
||||
_addr++;
|
||||
#endif
|
||||
}
|
||||
return _len;
|
||||
}
|
||||
|
||||
uint8_t W5100Class::read(uint16_t _addr)
|
||||
{
|
||||
#if !defined(SPI_HAS_EXTENDED_CS_PIN_HANDLING)
|
||||
setSS();
|
||||
SPI.transfer(0x0F);
|
||||
SPI.transfer(_addr >> 8);
|
||||
SPI.transfer(_addr & 0xFF);
|
||||
uint8_t _data = SPI.transfer(0);
|
||||
resetSS();
|
||||
#else
|
||||
SPI.transfer(ETHERNET_SHIELD_SPI_CS, 0x0F, SPI_CONTINUE);
|
||||
SPI.transfer(ETHERNET_SHIELD_SPI_CS, _addr >> 8, SPI_CONTINUE);
|
||||
SPI.transfer(ETHERNET_SHIELD_SPI_CS, _addr & 0xFF, SPI_CONTINUE);
|
||||
uint8_t _data = SPI.transfer(ETHERNET_SHIELD_SPI_CS, 0);
|
||||
#endif
|
||||
return _data;
|
||||
}
|
||||
|
||||
uint16_t W5100Class::read(uint16_t _addr, uint8_t *_buf, uint16_t _len)
|
||||
{
|
||||
for (uint16_t i=0; i<_len; i++)
|
||||
{
|
||||
#if !defined(SPI_HAS_EXTENDED_CS_PIN_HANDLING)
|
||||
setSS();
|
||||
SPI.transfer(0x0F);
|
||||
SPI.transfer(_addr >> 8);
|
||||
SPI.transfer(_addr & 0xFF);
|
||||
_addr++;
|
||||
_buf[i] = SPI.transfer(0);
|
||||
resetSS();
|
||||
#else
|
||||
SPI.transfer(ETHERNET_SHIELD_SPI_CS, 0x0F, SPI_CONTINUE);
|
||||
SPI.transfer(ETHERNET_SHIELD_SPI_CS, _addr >> 8, SPI_CONTINUE);
|
||||
SPI.transfer(ETHERNET_SHIELD_SPI_CS, _addr & 0xFF, SPI_CONTINUE);
|
||||
_buf[i] = SPI.transfer(ETHERNET_SHIELD_SPI_CS, 0);
|
||||
_addr++;
|
||||
#endif
|
||||
}
|
||||
return _len;
|
||||
}
|
||||
|
||||
void W5100Class::execCmdSn(SOCKET s, SockCMD _cmd) {
|
||||
// Send command to socket
|
||||
writeSnCR(s, _cmd);
|
||||
// Wait for command to complete
|
||||
while (readSnCR(s))
|
||||
;
|
||||
}
|
426
Ethernet/utility/w5100.h
Normal file
426
Ethernet/utility/w5100.h
Normal file
@ -0,0 +1,426 @@
|
||||
/*
|
||||
* Copyright (c) 2010 by Arduino LLC. All rights reserved.
|
||||
*
|
||||
* This file is free software; you can redistribute it and/or modify
|
||||
* it under the terms of either the GNU General Public License version 2
|
||||
* or the GNU Lesser General Public License version 2.1, both as
|
||||
* published by the Free Software Foundation.
|
||||
*/
|
||||
|
||||
#ifndef W5100_H_INCLUDED
|
||||
#define W5100_H_INCLUDED
|
||||
|
||||
#include <SPI.h>
|
||||
|
||||
#define ETHERNET_SHIELD_SPI_CS 10
|
||||
|
||||
#define MAX_SOCK_NUM 4
|
||||
|
||||
typedef uint8_t SOCKET;
|
||||
|
||||
#define IDM_OR 0x8000
|
||||
#define IDM_AR0 0x8001
|
||||
#define IDM_AR1 0x8002
|
||||
#define IDM_DR 0x8003
|
||||
/*
|
||||
class MR {
|
||||
public:
|
||||
static const uint8_t RST = 0x80;
|
||||
static const uint8_t PB = 0x10;
|
||||
static const uint8_t PPPOE = 0x08;
|
||||
static const uint8_t LB = 0x04;
|
||||
static const uint8_t AI = 0x02;
|
||||
static const uint8_t IND = 0x01;
|
||||
};
|
||||
*/
|
||||
/*
|
||||
class IR {
|
||||
public:
|
||||
static const uint8_t CONFLICT = 0x80;
|
||||
static const uint8_t UNREACH = 0x40;
|
||||
static const uint8_t PPPoE = 0x20;
|
||||
static const uint8_t SOCK0 = 0x01;
|
||||
static const uint8_t SOCK1 = 0x02;
|
||||
static const uint8_t SOCK2 = 0x04;
|
||||
static const uint8_t SOCK3 = 0x08;
|
||||
static inline uint8_t SOCK(SOCKET ch) { return (0x01 << ch); };
|
||||
};
|
||||
*/
|
||||
|
||||
class SnMR {
|
||||
public:
|
||||
static const uint8_t CLOSE = 0x00;
|
||||
static const uint8_t TCP = 0x01;
|
||||
static const uint8_t UDP = 0x02;
|
||||
static const uint8_t IPRAW = 0x03;
|
||||
static const uint8_t MACRAW = 0x04;
|
||||
static const uint8_t PPPOE = 0x05;
|
||||
static const uint8_t ND = 0x20;
|
||||
static const uint8_t MULTI = 0x80;
|
||||
};
|
||||
|
||||
enum SockCMD {
|
||||
Sock_OPEN = 0x01,
|
||||
Sock_LISTEN = 0x02,
|
||||
Sock_CONNECT = 0x04,
|
||||
Sock_DISCON = 0x08,
|
||||
Sock_CLOSE = 0x10,
|
||||
Sock_SEND = 0x20,
|
||||
Sock_SEND_MAC = 0x21,
|
||||
Sock_SEND_KEEP = 0x22,
|
||||
Sock_RECV = 0x40
|
||||
};
|
||||
|
||||
/*class SnCmd {
|
||||
public:
|
||||
static const uint8_t OPEN = 0x01;
|
||||
static const uint8_t LISTEN = 0x02;
|
||||
static const uint8_t CONNECT = 0x04;
|
||||
static const uint8_t DISCON = 0x08;
|
||||
static const uint8_t CLOSE = 0x10;
|
||||
static const uint8_t SEND = 0x20;
|
||||
static const uint8_t SEND_MAC = 0x21;
|
||||
static const uint8_t SEND_KEEP = 0x22;
|
||||
static const uint8_t RECV = 0x40;
|
||||
};
|
||||
*/
|
||||
|
||||
class SnIR {
|
||||
public:
|
||||
static const uint8_t SEND_OK = 0x10;
|
||||
static const uint8_t TIMEOUT = 0x08;
|
||||
static const uint8_t RECV = 0x04;
|
||||
static const uint8_t DISCON = 0x02;
|
||||
static const uint8_t CON = 0x01;
|
||||
};
|
||||
|
||||
class SnSR {
|
||||
public:
|
||||
static const uint8_t CLOSED = 0x00;
|
||||
static const uint8_t INIT = 0x13;
|
||||
static const uint8_t LISTEN = 0x14;
|
||||
static const uint8_t SYNSENT = 0x15;
|
||||
static const uint8_t SYNRECV = 0x16;
|
||||
static const uint8_t ESTABLISHED = 0x17;
|
||||
static const uint8_t FIN_WAIT = 0x18;
|
||||
static const uint8_t CLOSING = 0x1A;
|
||||
static const uint8_t TIME_WAIT = 0x1B;
|
||||
static const uint8_t CLOSE_WAIT = 0x1C;
|
||||
static const uint8_t LAST_ACK = 0x1D;
|
||||
static const uint8_t UDP = 0x22;
|
||||
static const uint8_t IPRAW = 0x32;
|
||||
static const uint8_t MACRAW = 0x42;
|
||||
static const uint8_t PPPOE = 0x5F;
|
||||
};
|
||||
|
||||
class IPPROTO {
|
||||
public:
|
||||
static const uint8_t IP = 0;
|
||||
static const uint8_t ICMP = 1;
|
||||
static const uint8_t IGMP = 2;
|
||||
static const uint8_t GGP = 3;
|
||||
static const uint8_t TCP = 6;
|
||||
static const uint8_t PUP = 12;
|
||||
static const uint8_t UDP = 17;
|
||||
static const uint8_t IDP = 22;
|
||||
static const uint8_t ND = 77;
|
||||
static const uint8_t RAW = 255;
|
||||
};
|
||||
|
||||
class W5100Class {
|
||||
|
||||
public:
|
||||
void init();
|
||||
|
||||
/**
|
||||
* @brief This function is being used for copy the data form Receive buffer of the chip to application buffer.
|
||||
*
|
||||
* It calculate the actual physical address where one has to read
|
||||
* the data from Receive buffer. Here also take care of the condition while it exceed
|
||||
* the Rx memory uper-bound of socket.
|
||||
*/
|
||||
void read_data(SOCKET s, volatile uint16_t src, volatile uint8_t * dst, uint16_t len);
|
||||
|
||||
/**
|
||||
* @brief This function is being called by send() and sendto() function also.
|
||||
*
|
||||
* This function read the Tx write pointer register and after copy the data in buffer update the Tx write pointer
|
||||
* register. User should read upper byte first and lower byte later to get proper value.
|
||||
*/
|
||||
void send_data_processing(SOCKET s, const uint8_t *data, uint16_t len);
|
||||
/**
|
||||
* @brief A copy of send_data_processing that uses the provided ptr for the
|
||||
* write offset. Only needed for the "streaming" UDP API, where
|
||||
* a single UDP packet is built up over a number of calls to
|
||||
* send_data_processing_ptr, because TX_WR doesn't seem to get updated
|
||||
* correctly in those scenarios
|
||||
* @param ptr value to use in place of TX_WR. If 0, then the value is read
|
||||
* in from TX_WR
|
||||
* @return New value for ptr, to be used in the next call
|
||||
*/
|
||||
// FIXME Update documentation
|
||||
void send_data_processing_offset(SOCKET s, uint16_t data_offset, const uint8_t *data, uint16_t len);
|
||||
|
||||
/**
|
||||
* @brief This function is being called by recv() also.
|
||||
*
|
||||
* This function read the Rx read pointer register
|
||||
* and after copy the data from receive buffer update the Rx write pointer register.
|
||||
* User should read upper byte first and lower byte later to get proper value.
|
||||
*/
|
||||
void recv_data_processing(SOCKET s, uint8_t *data, uint16_t len, uint8_t peek = 0);
|
||||
|
||||
inline void setGatewayIp(uint8_t *_addr);
|
||||
inline void getGatewayIp(uint8_t *_addr);
|
||||
|
||||
inline void setSubnetMask(uint8_t *_addr);
|
||||
inline void getSubnetMask(uint8_t *_addr);
|
||||
|
||||
inline void setMACAddress(uint8_t * addr);
|
||||
inline void getMACAddress(uint8_t * addr);
|
||||
|
||||
inline void setIPAddress(uint8_t * addr);
|
||||
inline void getIPAddress(uint8_t * addr);
|
||||
|
||||
inline void setRetransmissionTime(uint16_t timeout);
|
||||
inline void setRetransmissionCount(uint8_t _retry);
|
||||
|
||||
void execCmdSn(SOCKET s, SockCMD _cmd);
|
||||
|
||||
uint16_t getTXFreeSize(SOCKET s);
|
||||
uint16_t getRXReceivedSize(SOCKET s);
|
||||
|
||||
|
||||
// W5100 Registers
|
||||
// ---------------
|
||||
private:
|
||||
static uint8_t write(uint16_t _addr, uint8_t _data);
|
||||
static uint16_t write(uint16_t addr, const uint8_t *buf, uint16_t len);
|
||||
static uint8_t read(uint16_t addr);
|
||||
static uint16_t read(uint16_t addr, uint8_t *buf, uint16_t len);
|
||||
|
||||
#define __GP_REGISTER8(name, address) \
|
||||
static inline void write##name(uint8_t _data) { \
|
||||
write(address, _data); \
|
||||
} \
|
||||
static inline uint8_t read##name() { \
|
||||
return read(address); \
|
||||
}
|
||||
#define __GP_REGISTER16(name, address) \
|
||||
static void write##name(uint16_t _data) { \
|
||||
write(address, _data >> 8); \
|
||||
write(address+1, _data & 0xFF); \
|
||||
} \
|
||||
static uint16_t read##name() { \
|
||||
uint16_t res = read(address); \
|
||||
res = (res << 8) + read(address + 1); \
|
||||
return res; \
|
||||
}
|
||||
#define __GP_REGISTER_N(name, address, size) \
|
||||
static uint16_t write##name(uint8_t *_buff) { \
|
||||
return write(address, _buff, size); \
|
||||
} \
|
||||
static uint16_t read##name(uint8_t *_buff) { \
|
||||
return read(address, _buff, size); \
|
||||
}
|
||||
|
||||
public:
|
||||
__GP_REGISTER8 (MR, 0x0000); // Mode
|
||||
__GP_REGISTER_N(GAR, 0x0001, 4); // Gateway IP address
|
||||
__GP_REGISTER_N(SUBR, 0x0005, 4); // Subnet mask address
|
||||
__GP_REGISTER_N(SHAR, 0x0009, 6); // Source MAC address
|
||||
__GP_REGISTER_N(SIPR, 0x000F, 4); // Source IP address
|
||||
__GP_REGISTER8 (IR, 0x0015); // Interrupt
|
||||
__GP_REGISTER8 (IMR, 0x0016); // Interrupt Mask
|
||||
__GP_REGISTER16(RTR, 0x0017); // Timeout address
|
||||
__GP_REGISTER8 (RCR, 0x0019); // Retry count
|
||||
__GP_REGISTER8 (RMSR, 0x001A); // Receive memory size
|
||||
__GP_REGISTER8 (TMSR, 0x001B); // Transmit memory size
|
||||
__GP_REGISTER8 (PATR, 0x001C); // Authentication type address in PPPoE mode
|
||||
__GP_REGISTER8 (PTIMER, 0x0028); // PPP LCP Request Timer
|
||||
__GP_REGISTER8 (PMAGIC, 0x0029); // PPP LCP Magic Number
|
||||
__GP_REGISTER_N(UIPR, 0x002A, 4); // Unreachable IP address in UDP mode
|
||||
__GP_REGISTER16(UPORT, 0x002E); // Unreachable Port address in UDP mode
|
||||
|
||||
#undef __GP_REGISTER8
|
||||
#undef __GP_REGISTER16
|
||||
#undef __GP_REGISTER_N
|
||||
|
||||
// W5100 Socket registers
|
||||
// ----------------------
|
||||
private:
|
||||
static inline uint8_t readSn(SOCKET _s, uint16_t _addr);
|
||||
static inline uint8_t writeSn(SOCKET _s, uint16_t _addr, uint8_t _data);
|
||||
static inline uint16_t readSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t len);
|
||||
static inline uint16_t writeSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t len);
|
||||
|
||||
static const uint16_t CH_BASE = 0x0400;
|
||||
static const uint16_t CH_SIZE = 0x0100;
|
||||
|
||||
#define __SOCKET_REGISTER8(name, address) \
|
||||
static inline void write##name(SOCKET _s, uint8_t _data) { \
|
||||
writeSn(_s, address, _data); \
|
||||
} \
|
||||
static inline uint8_t read##name(SOCKET _s) { \
|
||||
return readSn(_s, address); \
|
||||
}
|
||||
#define __SOCKET_REGISTER16(name, address) \
|
||||
static void write##name(SOCKET _s, uint16_t _data) { \
|
||||
writeSn(_s, address, _data >> 8); \
|
||||
writeSn(_s, address+1, _data & 0xFF); \
|
||||
} \
|
||||
static uint16_t read##name(SOCKET _s) { \
|
||||
uint16_t res = readSn(_s, address); \
|
||||
uint16_t res2 = readSn(_s,address + 1); \
|
||||
res = res << 8; \
|
||||
res2 = res2 & 0xFF; \
|
||||
res = res | res2; \
|
||||
return res; \
|
||||
}
|
||||
#define __SOCKET_REGISTER_N(name, address, size) \
|
||||
static uint16_t write##name(SOCKET _s, uint8_t *_buff) { \
|
||||
return writeSn(_s, address, _buff, size); \
|
||||
} \
|
||||
static uint16_t read##name(SOCKET _s, uint8_t *_buff) { \
|
||||
return readSn(_s, address, _buff, size); \
|
||||
}
|
||||
|
||||
public:
|
||||
__SOCKET_REGISTER8(SnMR, 0x0000) // Mode
|
||||
__SOCKET_REGISTER8(SnCR, 0x0001) // Command
|
||||
__SOCKET_REGISTER8(SnIR, 0x0002) // Interrupt
|
||||
__SOCKET_REGISTER8(SnSR, 0x0003) // Status
|
||||
__SOCKET_REGISTER16(SnPORT, 0x0004) // Source Port
|
||||
__SOCKET_REGISTER_N(SnDHAR, 0x0006, 6) // Destination Hardw Addr
|
||||
__SOCKET_REGISTER_N(SnDIPR, 0x000C, 4) // Destination IP Addr
|
||||
__SOCKET_REGISTER16(SnDPORT, 0x0010) // Destination Port
|
||||
__SOCKET_REGISTER16(SnMSSR, 0x0012) // Max Segment Size
|
||||
__SOCKET_REGISTER8(SnPROTO, 0x0014) // Protocol in IP RAW Mode
|
||||
__SOCKET_REGISTER8(SnTOS, 0x0015) // IP TOS
|
||||
__SOCKET_REGISTER8(SnTTL, 0x0016) // IP TTL
|
||||
__SOCKET_REGISTER16(SnTX_FSR, 0x0020) // TX Free Size
|
||||
__SOCKET_REGISTER16(SnTX_RD, 0x0022) // TX Read Pointer
|
||||
__SOCKET_REGISTER16(SnTX_WR, 0x0024) // TX Write Pointer
|
||||
__SOCKET_REGISTER16(SnRX_RSR, 0x0026) // RX Free Size
|
||||
__SOCKET_REGISTER16(SnRX_RD, 0x0028) // RX Read Pointer
|
||||
__SOCKET_REGISTER16(SnRX_WR, 0x002A) // RX Write Pointer (supported?)
|
||||
|
||||
#undef __SOCKET_REGISTER8
|
||||
#undef __SOCKET_REGISTER16
|
||||
#undef __SOCKET_REGISTER_N
|
||||
|
||||
|
||||
private:
|
||||
static const uint8_t RST = 7; // Reset BIT
|
||||
|
||||
static const int SOCKETS = 4;
|
||||
static const uint16_t SMASK = 0x07FF; // Tx buffer MASK
|
||||
static const uint16_t RMASK = 0x07FF; // Rx buffer MASK
|
||||
public:
|
||||
static const uint16_t SSIZE = 2048; // Max Tx buffer size
|
||||
private:
|
||||
static const uint16_t RSIZE = 2048; // Max Rx buffer size
|
||||
uint16_t SBASE[SOCKETS]; // Tx buffer base address
|
||||
uint16_t RBASE[SOCKETS]; // Rx buffer base address
|
||||
|
||||
private:
|
||||
#if !defined(SPI_HAS_EXTENDED_CS_PIN_HANDLING)
|
||||
#define SPI_ETHERNET_SETTINGS SPISettings(4000000, MSBFIRST, SPI_MODE0)
|
||||
#if defined(ARDUINO_ARCH_AVR)
|
||||
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
|
||||
inline static void initSS() { DDRB |= _BV(4); };
|
||||
inline static void setSS() { PORTB &= ~_BV(4); };
|
||||
inline static void resetSS() { PORTB |= _BV(4); };
|
||||
#elif defined(__AVR_ATmega32U4__)
|
||||
inline static void initSS() { DDRB |= _BV(6); };
|
||||
inline static void setSS() { PORTB &= ~_BV(6); };
|
||||
inline static void resetSS() { PORTB |= _BV(6); };
|
||||
#elif defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB162__)
|
||||
inline static void initSS() { DDRB |= _BV(0); };
|
||||
inline static void setSS() { PORTB &= ~_BV(0); };
|
||||
inline static void resetSS() { PORTB |= _BV(0); };
|
||||
#else
|
||||
inline static void initSS() { DDRB |= _BV(2); };
|
||||
inline static void setSS() { PORTB &= ~_BV(2); };
|
||||
inline static void resetSS() { PORTB |= _BV(2); };
|
||||
#endif
|
||||
#elif defined(__ARDUINO_ARC__)
|
||||
inline static void initSS() { pinMode(10, OUTPUT); };
|
||||
inline static void setSS() { digitalWrite(10, LOW); };
|
||||
inline static void resetSS() { digitalWrite(10, HIGH); };
|
||||
#else
|
||||
inline static void initSS() {
|
||||
*portModeRegister(digitalPinToPort(ETHERNET_SHIELD_SPI_CS)) |= digitalPinToBitMask(ETHERNET_SHIELD_SPI_CS);
|
||||
}
|
||||
inline static void setSS() {
|
||||
*portOutputRegister(digitalPinToPort(ETHERNET_SHIELD_SPI_CS)) &= ~digitalPinToBitMask(ETHERNET_SHIELD_SPI_CS);
|
||||
}
|
||||
inline static void resetSS() {
|
||||
*portOutputRegister(digitalPinToPort(ETHERNET_SHIELD_SPI_CS)) |= digitalPinToBitMask(ETHERNET_SHIELD_SPI_CS);
|
||||
}
|
||||
#endif
|
||||
#else
|
||||
#define SPI_ETHERNET_SETTINGS ETHERNET_SHIELD_SPI_CS,SPISettings(4000000, MSBFIRST, SPI_MODE0)
|
||||
// initSS(), setSS(), resetSS() not needed with EXTENDED_CS_PIN_HANDLING
|
||||
#endif
|
||||
};
|
||||
|
||||
extern W5100Class W5100;
|
||||
|
||||
uint8_t W5100Class::readSn(SOCKET _s, uint16_t _addr) {
|
||||
return read(CH_BASE + _s * CH_SIZE + _addr);
|
||||
}
|
||||
|
||||
uint8_t W5100Class::writeSn(SOCKET _s, uint16_t _addr, uint8_t _data) {
|
||||
return write(CH_BASE + _s * CH_SIZE + _addr, _data);
|
||||
}
|
||||
|
||||
uint16_t W5100Class::readSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t _len) {
|
||||
return read(CH_BASE + _s * CH_SIZE + _addr, _buf, _len);
|
||||
}
|
||||
|
||||
uint16_t W5100Class::writeSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t _len) {
|
||||
return write(CH_BASE + _s * CH_SIZE + _addr, _buf, _len);
|
||||
}
|
||||
|
||||
void W5100Class::getGatewayIp(uint8_t *_addr) {
|
||||
readGAR(_addr);
|
||||
}
|
||||
|
||||
void W5100Class::setGatewayIp(uint8_t *_addr) {
|
||||
writeGAR(_addr);
|
||||
}
|
||||
|
||||
void W5100Class::getSubnetMask(uint8_t *_addr) {
|
||||
readSUBR(_addr);
|
||||
}
|
||||
|
||||
void W5100Class::setSubnetMask(uint8_t *_addr) {
|
||||
writeSUBR(_addr);
|
||||
}
|
||||
|
||||
void W5100Class::getMACAddress(uint8_t *_addr) {
|
||||
readSHAR(_addr);
|
||||
}
|
||||
|
||||
void W5100Class::setMACAddress(uint8_t *_addr) {
|
||||
writeSHAR(_addr);
|
||||
}
|
||||
|
||||
void W5100Class::getIPAddress(uint8_t *_addr) {
|
||||
readSIPR(_addr);
|
||||
}
|
||||
|
||||
void W5100Class::setIPAddress(uint8_t *_addr) {
|
||||
writeSIPR(_addr);
|
||||
}
|
||||
|
||||
void W5100Class::setRetransmissionTime(uint16_t _timeout) {
|
||||
writeRTR(_timeout);
|
||||
}
|
||||
|
||||
void W5100Class::setRetransmissionCount(uint8_t _retry) {
|
||||
writeRCR(_retry);
|
||||
}
|
||||
|
||||
#endif
|
@ -28,9 +28,9 @@ void setup() {
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (tick.check()) {
|
||||
Serial << "tick" << endl;
|
||||
}
|
||||
// if (tick.check()) {
|
||||
// Serial << "tick" << endl;
|
||||
// }
|
||||
|
||||
hmi.exec();
|
||||
|
||||
|
248
WiFi/WiFi.cpp
248
WiFi/WiFi.cpp
@ -1,248 +0,0 @@
|
||||
/*
|
||||
WiFi.cpp - Library for Arduino Wifi shield.
|
||||
Copyright (c) 2011-2014 Arduino LLC. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include "utility/wifi_drv.h"
|
||||
#include "WiFi.h"
|
||||
|
||||
extern "C" {
|
||||
#include "utility/wl_definitions.h"
|
||||
#include "utility/wl_types.h"
|
||||
#include "utility/debug.h"
|
||||
}
|
||||
|
||||
// XXX: don't make assumptions about the value of MAX_SOCK_NUM.
|
||||
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 };
|
||||
|
||||
WiFiClass::WiFiClass()
|
||||
{
|
||||
}
|
||||
|
||||
void WiFiClass::init()
|
||||
{
|
||||
WiFiDrv::wifiDriverInit();
|
||||
}
|
||||
|
||||
uint8_t WiFiClass::getSocket()
|
||||
{
|
||||
for (uint8_t i = 0; i < MAX_SOCK_NUM; ++i)
|
||||
{
|
||||
if (WiFiClass::_server_port[i] == 0)
|
||||
{
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return NO_SOCKET_AVAIL;
|
||||
}
|
||||
|
||||
char* WiFiClass::firmwareVersion()
|
||||
{
|
||||
return WiFiDrv::getFwVersion();
|
||||
}
|
||||
|
||||
int WiFiClass::begin(char* ssid)
|
||||
{
|
||||
uint8_t status = WL_IDLE_STATUS;
|
||||
uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION;
|
||||
|
||||
if (WiFiDrv::wifiSetNetwork(ssid, strlen(ssid)) != WL_FAILURE)
|
||||
{
|
||||
do
|
||||
{
|
||||
delay(WL_DELAY_START_CONNECTION);
|
||||
status = WiFiDrv::getConnectionStatus();
|
||||
}
|
||||
while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0));
|
||||
}else
|
||||
{
|
||||
status = WL_CONNECT_FAILED;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
int WiFiClass::begin(char* ssid, uint8_t key_idx, const char *key)
|
||||
{
|
||||
uint8_t status = WL_IDLE_STATUS;
|
||||
uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION;
|
||||
|
||||
// set encryption key
|
||||
if (WiFiDrv::wifiSetKey(ssid, strlen(ssid), key_idx, key, strlen(key)) != WL_FAILURE)
|
||||
{
|
||||
do
|
||||
{
|
||||
delay(WL_DELAY_START_CONNECTION);
|
||||
status = WiFiDrv::getConnectionStatus();
|
||||
}while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0));
|
||||
}else{
|
||||
status = WL_CONNECT_FAILED;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
int WiFiClass::begin(char* ssid, const char *passphrase)
|
||||
{
|
||||
uint8_t status = WL_IDLE_STATUS;
|
||||
uint8_t attempts = WL_MAX_ATTEMPT_CONNECTION;
|
||||
|
||||
// set passphrase
|
||||
if (WiFiDrv::wifiSetPassphrase(ssid, strlen(ssid), passphrase, strlen(passphrase))!= WL_FAILURE)
|
||||
{
|
||||
do
|
||||
{
|
||||
delay(WL_DELAY_START_CONNECTION);
|
||||
status = WiFiDrv::getConnectionStatus();
|
||||
}
|
||||
while ((( status == WL_IDLE_STATUS)||(status == WL_SCAN_COMPLETED))&&(--attempts>0));
|
||||
}else{
|
||||
status = WL_CONNECT_FAILED;
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
void WiFiClass::config(IPAddress local_ip)
|
||||
{
|
||||
WiFiDrv::config(1, (uint32_t)local_ip, 0, 0);
|
||||
}
|
||||
|
||||
void WiFiClass::config(IPAddress local_ip, IPAddress dns_server)
|
||||
{
|
||||
WiFiDrv::config(1, (uint32_t)local_ip, 0, 0);
|
||||
WiFiDrv::setDNS(1, (uint32_t)dns_server, 0);
|
||||
}
|
||||
|
||||
void WiFiClass::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway)
|
||||
{
|
||||
WiFiDrv::config(2, (uint32_t)local_ip, (uint32_t)gateway, 0);
|
||||
WiFiDrv::setDNS(1, (uint32_t)dns_server, 0);
|
||||
}
|
||||
|
||||
void WiFiClass::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
|
||||
{
|
||||
WiFiDrv::config(3, (uint32_t)local_ip, (uint32_t)gateway, (uint32_t)subnet);
|
||||
WiFiDrv::setDNS(1, (uint32_t)dns_server, 0);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
uint8_t* WiFiClass::macAddress(uint8_t* mac)
|
||||
{
|
||||
uint8_t* _mac = WiFiDrv::getMacAddress();
|
||||
memcpy(mac, _mac, WL_MAC_ADDR_LENGTH);
|
||||
return mac;
|
||||
}
|
||||
|
||||
IPAddress WiFiClass::localIP()
|
||||
{
|
||||
IPAddress ret;
|
||||
WiFiDrv::getIpAddress(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
IPAddress WiFiClass::subnetMask()
|
||||
{
|
||||
IPAddress ret;
|
||||
WiFiDrv::getSubnetMask(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
IPAddress WiFiClass::gatewayIP()
|
||||
{
|
||||
IPAddress ret;
|
||||
WiFiDrv::getGatewayIP(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
char* WiFiClass::SSID()
|
||||
{
|
||||
return WiFiDrv::getCurrentSSID();
|
||||
}
|
||||
|
||||
uint8_t* WiFiClass::BSSID(uint8_t* bssid)
|
||||
{
|
||||
uint8_t* _bssid = WiFiDrv::getCurrentBSSID();
|
||||
memcpy(bssid, _bssid, WL_MAC_ADDR_LENGTH);
|
||||
return bssid;
|
||||
}
|
||||
|
||||
int32_t WiFiClass::RSSI()
|
||||
{
|
||||
return WiFiDrv::getCurrentRSSI();
|
||||
}
|
||||
|
||||
uint8_t WiFiClass::encryptionType()
|
||||
{
|
||||
return WiFiDrv::getCurrentEncryptionType();
|
||||
}
|
||||
|
||||
|
||||
int8_t WiFiClass::scanNetworks()
|
||||
{
|
||||
uint8_t attempts = 10;
|
||||
uint8_t numOfNetworks = 0;
|
||||
|
||||
if (WiFiDrv::startScanNetworks() == WL_FAILURE)
|
||||
return WL_FAILURE;
|
||||
do
|
||||
{
|
||||
delay(2000);
|
||||
numOfNetworks = WiFiDrv::getScanNetworks();
|
||||
}
|
||||
while (( numOfNetworks == 0)&&(--attempts>0));
|
||||
return numOfNetworks;
|
||||
}
|
||||
|
||||
char* WiFiClass::SSID(uint8_t networkItem)
|
||||
{
|
||||
return WiFiDrv::getSSIDNetoworks(networkItem);
|
||||
}
|
||||
|
||||
int32_t WiFiClass::RSSI(uint8_t networkItem)
|
||||
{
|
||||
return WiFiDrv::getRSSINetoworks(networkItem);
|
||||
}
|
||||
|
||||
uint8_t WiFiClass::encryptionType(uint8_t networkItem)
|
||||
{
|
||||
return WiFiDrv::getEncTypeNetowrks(networkItem);
|
||||
}
|
||||
|
||||
uint8_t WiFiClass::status()
|
||||
{
|
||||
return WiFiDrv::getConnectionStatus();
|
||||
}
|
||||
|
||||
int WiFiClass::hostByName(const char* aHostname, IPAddress& aResult)
|
||||
{
|
||||
return WiFiDrv::getHostByName(aHostname, aResult);
|
||||
}
|
||||
|
||||
WiFiClass WiFi;
|
246
WiFi/WiFi.h
246
WiFi/WiFi.h
@ -1,246 +0,0 @@
|
||||
/*
|
||||
WiFi.h - Library for Arduino Wifi shield.
|
||||
Copyright (c) 2011-2014 Arduino LLC. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef WiFi_h
|
||||
#define WiFi_h
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
extern "C" {
|
||||
#include "utility/wl_definitions.h"
|
||||
#include "utility/wl_types.h"
|
||||
}
|
||||
|
||||
#include "IPAddress.h"
|
||||
#include "WiFiClient.h"
|
||||
#include "WiFiServer.h"
|
||||
|
||||
class WiFiClass
|
||||
{
|
||||
private:
|
||||
|
||||
static void init();
|
||||
public:
|
||||
static int16_t _state[MAX_SOCK_NUM];
|
||||
static uint16_t _server_port[MAX_SOCK_NUM];
|
||||
|
||||
WiFiClass();
|
||||
|
||||
/*
|
||||
* Get the first socket available
|
||||
*/
|
||||
static uint8_t getSocket();
|
||||
|
||||
/*
|
||||
* Get firmware version
|
||||
*/
|
||||
static char* firmwareVersion();
|
||||
|
||||
|
||||
/* Start Wifi connection for OPEN networks
|
||||
*
|
||||
* param ssid: Pointer to the SSID string.
|
||||
*/
|
||||
int begin(char* ssid);
|
||||
|
||||
/* Start Wifi connection with WEP encryption.
|
||||
* Configure a key into the device. The key type (WEP-40, WEP-104)
|
||||
* is determined by the size of the key (5 bytes for WEP-40, 13 bytes for WEP-104).
|
||||
*
|
||||
* param ssid: Pointer to the SSID string.
|
||||
* param key_idx: The key index to set. Valid values are 0-3.
|
||||
* param key: Key input buffer.
|
||||
*/
|
||||
int begin(char* ssid, uint8_t key_idx, const char* key);
|
||||
|
||||
/* Start Wifi connection with passphrase
|
||||
* the most secure supported mode will be automatically selected
|
||||
*
|
||||
* param ssid: Pointer to the SSID string.
|
||||
* param passphrase: Passphrase. Valid characters in a passphrase
|
||||
* must be between ASCII 32-126 (decimal).
|
||||
*/
|
||||
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: IP configuration for DNS server 1
|
||||
*/
|
||||
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: IP configuration for DNS server 1
|
||||
* 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: IP configuration for DNS server 1
|
||||
* param gateway: Static gateway configuration
|
||||
* param subnet: Static Subnet mask
|
||||
*/
|
||||
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
|
||||
*
|
||||
* return: one value of wl_status_t enum
|
||||
*/
|
||||
int disconnect(void);
|
||||
|
||||
/*
|
||||
* Get the interface MAC address.
|
||||
*
|
||||
* return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH
|
||||
*/
|
||||
uint8_t* macAddress(uint8_t* mac);
|
||||
|
||||
/*
|
||||
* Get the interface IP address.
|
||||
*
|
||||
* return: Ip address value
|
||||
*/
|
||||
IPAddress localIP();
|
||||
|
||||
/*
|
||||
* Get the interface subnet mask address.
|
||||
*
|
||||
* return: subnet mask address value
|
||||
*/
|
||||
IPAddress subnetMask();
|
||||
|
||||
/*
|
||||
* Get the gateway ip address.
|
||||
*
|
||||
* return: gateway ip address value
|
||||
*/
|
||||
IPAddress gatewayIP();
|
||||
|
||||
/*
|
||||
* Return the current SSID associated with the network
|
||||
*
|
||||
* return: ssid string
|
||||
*/
|
||||
char* SSID();
|
||||
|
||||
/*
|
||||
* Return the current BSSID associated with the network.
|
||||
* It is the MAC address of the Access Point
|
||||
*
|
||||
* return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH
|
||||
*/
|
||||
uint8_t* BSSID(uint8_t* bssid);
|
||||
|
||||
/*
|
||||
* Return the current RSSI /Received Signal Strength in dBm)
|
||||
* associated with the network
|
||||
*
|
||||
* return: signed value
|
||||
*/
|
||||
int32_t RSSI();
|
||||
|
||||
/*
|
||||
* Return the Encryption Type associated with the network
|
||||
*
|
||||
* return: one value of wl_enc_type enum
|
||||
*/
|
||||
uint8_t encryptionType();
|
||||
|
||||
/*
|
||||
* Start scan WiFi networks available
|
||||
*
|
||||
* return: Number of discovered networks
|
||||
*/
|
||||
int8_t scanNetworks();
|
||||
|
||||
/*
|
||||
* Return the SSID discovered during the network scan.
|
||||
*
|
||||
* param networkItem: specify from which network item want to get the information
|
||||
*
|
||||
* return: ssid string of the specified item on the networks scanned list
|
||||
*/
|
||||
char* SSID(uint8_t networkItem);
|
||||
|
||||
/*
|
||||
* Return the encryption type of the networks discovered during the scanNetworks
|
||||
*
|
||||
* param networkItem: specify from which network item want to get the information
|
||||
*
|
||||
* return: encryption type (enum wl_enc_type) of the specified item on the networks scanned list
|
||||
*/
|
||||
uint8_t encryptionType(uint8_t networkItem);
|
||||
|
||||
/*
|
||||
* Return the RSSI of the networks discovered during the scanNetworks
|
||||
*
|
||||
* param networkItem: specify from which network item want to get the information
|
||||
*
|
||||
* return: signed value of RSSI of the specified item on the networks scanned list
|
||||
*/
|
||||
int32_t RSSI(uint8_t networkItem);
|
||||
|
||||
/*
|
||||
* Return Connection status.
|
||||
*
|
||||
* return: one of the value defined in wl_status_t
|
||||
*/
|
||||
uint8_t status();
|
||||
|
||||
/*
|
||||
* Resolve the given hostname to an IP address.
|
||||
* param aHostname: Name to be resolved
|
||||
* param aResult: IPAddress structure to store the returned IP address
|
||||
* result: 1 if aIPAddrString was successfully converted to an IP address,
|
||||
* else error code
|
||||
*/
|
||||
int hostByName(const char* aHostname, IPAddress& aResult);
|
||||
|
||||
friend class WiFiClient;
|
||||
friend class WiFiServer;
|
||||
};
|
||||
|
||||
extern WiFiClass WiFi;
|
||||
|
||||
#endif
|
@ -1,200 +0,0 @@
|
||||
/*
|
||||
WiFiClient.cpp - Library for Arduino Wifi shield.
|
||||
Copyright (c) 2011-2014 Arduino LLC. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -1,59 +0,0 @@
|
||||
/*
|
||||
WiFiClient.cpp - Library for Arduino Wifi shield.
|
||||
Copyright (c) 2011-2014 Arduino LLC. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef wificlient_h
|
||||
#define wificlient_h
|
||||
#include "Arduino.h"
|
||||
#include "Print.h"
|
||||
#include "Client.h"
|
||||
#include "IPAddress.h"
|
||||
|
||||
class WiFiClient : public Client {
|
||||
|
||||
public:
|
||||
WiFiClient();
|
||||
WiFiClient(uint8_t sock);
|
||||
|
||||
uint8_t status();
|
||||
virtual int connect(IPAddress ip, uint16_t port);
|
||||
virtual int connect(const char *host, uint16_t port);
|
||||
virtual size_t write(uint8_t);
|
||||
virtual size_t write(const uint8_t *buf, size_t size);
|
||||
virtual int available();
|
||||
virtual int read();
|
||||
virtual int read(uint8_t *buf, size_t size);
|
||||
virtual int peek();
|
||||
virtual void flush();
|
||||
virtual void stop();
|
||||
virtual uint8_t connected();
|
||||
virtual operator bool();
|
||||
|
||||
friend class WiFiServer;
|
||||
|
||||
using Print::write;
|
||||
|
||||
private:
|
||||
static uint16_t _srcport;
|
||||
uint8_t _sock; //not used
|
||||
uint16_t _socket;
|
||||
|
||||
uint8_t getFirstSocket();
|
||||
};
|
||||
|
||||
#endif
|
@ -1,108 +0,0 @@
|
||||
/*
|
||||
WiFiServer.cpp - Library for Arduino Wifi shield.
|
||||
Copyright (c) 2011-2014 Arduino LLC. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include "utility/server_drv.h"
|
||||
|
||||
extern "C" {
|
||||
#include "utility/debug.h"
|
||||
}
|
||||
|
||||
#include "WiFi.h"
|
||||
#include "WiFiClient.h"
|
||||
#include "WiFiServer.h"
|
||||
|
||||
WiFiServer::WiFiServer(uint16_t port)
|
||||
{
|
||||
_port = port;
|
||||
}
|
||||
|
||||
void WiFiServer::begin()
|
||||
{
|
||||
uint8_t _sock = WiFiClass::getSocket();
|
||||
if (_sock != NO_SOCKET_AVAIL)
|
||||
{
|
||||
ServerDrv::startServer(_port, _sock);
|
||||
WiFiClass::_server_port[_sock] = _port;
|
||||
WiFiClass::_state[_sock] = _sock;
|
||||
}
|
||||
}
|
||||
|
||||
WiFiClient WiFiServer::available(byte* status)
|
||||
{
|
||||
static int cycle_server_down = 0;
|
||||
const int TH_SERVER_DOWN = 50;
|
||||
|
||||
for (int sock = 0; sock < MAX_SOCK_NUM; sock++)
|
||||
{
|
||||
if (WiFiClass::_server_port[sock] == _port)
|
||||
{
|
||||
WiFiClient client(sock);
|
||||
uint8_t _status = client.status();
|
||||
uint8_t _ser_status = this->status();
|
||||
|
||||
if (status != NULL)
|
||||
*status = _status;
|
||||
|
||||
//server not in listen state, restart it
|
||||
if ((_ser_status == 0)&&(cycle_server_down++ > TH_SERVER_DOWN))
|
||||
{
|
||||
ServerDrv::startServer(_port, sock);
|
||||
cycle_server_down = 0;
|
||||
}
|
||||
|
||||
if (_status == ESTABLISHED)
|
||||
{
|
||||
return client; //TODO
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return WiFiClient(255);
|
||||
}
|
||||
|
||||
uint8_t WiFiServer::status() {
|
||||
return ServerDrv::getServerState(0);
|
||||
}
|
||||
|
||||
|
||||
size_t WiFiServer::write(uint8_t b)
|
||||
{
|
||||
return write(&b, 1);
|
||||
}
|
||||
|
||||
size_t WiFiServer::write(const uint8_t *buffer, size_t size)
|
||||
{
|
||||
size_t n = 0;
|
||||
|
||||
for (int sock = 0; sock < MAX_SOCK_NUM; sock++)
|
||||
{
|
||||
if (WiFiClass::_server_port[sock] != 0)
|
||||
{
|
||||
WiFiClient client(sock);
|
||||
|
||||
if (WiFiClass::_server_port[sock] == _port &&
|
||||
client.status() == ESTABLISHED)
|
||||
{
|
||||
n+=client.write(buffer, size);
|
||||
}
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
/*
|
||||
WiFiServer.h - Library for Arduino Wifi shield.
|
||||
Copyright (c) 2011-2014 Arduino LLC. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef wifiserver_h
|
||||
#define wifiserver_h
|
||||
|
||||
extern "C" {
|
||||
#include "utility/wl_definitions.h"
|
||||
}
|
||||
|
||||
#include "Server.h"
|
||||
|
||||
class WiFiClient;
|
||||
|
||||
class WiFiServer : public Server {
|
||||
private:
|
||||
uint16_t _port;
|
||||
void* pcb;
|
||||
public:
|
||||
WiFiServer(uint16_t);
|
||||
WiFiClient available(uint8_t* status = NULL);
|
||||
void begin();
|
||||
virtual size_t write(uint8_t);
|
||||
virtual size_t write(const uint8_t *buf, size_t size);
|
||||
uint8_t status();
|
||||
|
||||
using Print::write;
|
||||
};
|
||||
|
||||
#endif
|
181
WiFi/WiFiUdp.cpp
181
WiFi/WiFiUdp.cpp
@ -1,181 +0,0 @@
|
||||
/*
|
||||
WiFiUdp.cpp - Library for Arduino Wifi shield.
|
||||
Copyright (c) 2011-2014 Arduino LLC. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -1,80 +0,0 @@
|
||||
/*
|
||||
WiFiUdp.h - Library for Arduino Wifi shield.
|
||||
Copyright (c) 2011-2014 Arduino LLC. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#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
|
@ -1,95 +0,0 @@
|
||||
/*
|
||||
debug.h - Library for Arduino Wifi shield.
|
||||
Copyright (c) 2011-2014 Arduino. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
//*********************************************/
|
||||
//
|
||||
// File: debug.h
|
||||
//
|
||||
// Author: dlf (Metodo2 srl)
|
||||
//
|
||||
//********************************************/
|
||||
|
||||
|
||||
#ifndef Debug_H
|
||||
#define Debug_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define PRINT_FILE_LINE() do { \
|
||||
Serial.print("[");Serial.print(__FILE__); \
|
||||
Serial.print("::");Serial.print(__LINE__);Serial.print("]");\
|
||||
}while (0);
|
||||
|
||||
#ifdef _DEBUG_
|
||||
|
||||
#define INFO(format, args...) do { \
|
||||
char buf[250]; \
|
||||
sprintf(buf, format, args); \
|
||||
Serial.println(buf); \
|
||||
} while(0);
|
||||
|
||||
#define INFO1(x) do { PRINT_FILE_LINE() Serial.print("-I-");\
|
||||
Serial.println(x); \
|
||||
}while (0);
|
||||
|
||||
|
||||
#define INFO2(x,y) do { PRINT_FILE_LINE() Serial.print("-I-");\
|
||||
Serial.print(x,16);Serial.print(",");Serial.println(y,16); \
|
||||
}while (0);
|
||||
|
||||
|
||||
#else
|
||||
#define INFO1(x) do {} while(0);
|
||||
#define INFO2(x,y) do {} while(0);
|
||||
#define INFO(format, args...) do {} while(0);
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
#define WARN(args) do { PRINT_FILE_LINE() \
|
||||
Serial.print("-W-"); Serial.println(args); \
|
||||
}while (0);
|
||||
#else
|
||||
#define WARN(args) do {} while (0);
|
||||
#endif
|
||||
|
||||
#if _DEBUG_SPI_
|
||||
#define DBG_PIN2 5
|
||||
#define DBG_PIN 4
|
||||
|
||||
#define START() digitalWrite(DBG_PIN2, HIGH);
|
||||
#define END() digitalWrite(DBG_PIN2, LOW);
|
||||
#define SET_TRIGGER() digitalWrite(DBG_PIN, HIGH);
|
||||
#define RST_TRIGGER() digitalWrite(DBG_PIN, LOW);
|
||||
|
||||
#define INIT_TRIGGER() pinMode(DBG_PIN, OUTPUT); \
|
||||
pinMode(DBG_PIN2, OUTPUT); \
|
||||
RST_TRIGGER()
|
||||
#define TOGGLE_TRIGGER() SET_TRIGGER() \
|
||||
delayMicroseconds(2); \
|
||||
RST_TRIGGER()
|
||||
#else
|
||||
#define START()
|
||||
#define END()
|
||||
#define SET_TRIGGER()
|
||||
#define RST_TRIGGER()
|
||||
#define INIT_TRIGGER()
|
||||
#define TOGGLE_TRIGGER()
|
||||
#endif
|
||||
|
||||
#endif
|
@ -1,327 +0,0 @@
|
||||
/*
|
||||
server_drv.cpp - Library for Arduino Wifi shield.
|
||||
Copyright (c) 2011-2014 Arduino. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
//#define _DEBUG_
|
||||
|
||||
#include "utility/server_drv.h"
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "utility/spi_drv.h"
|
||||
|
||||
extern "C" {
|
||||
#include "utility/wl_types.h"
|
||||
#include "utility/debug.h"
|
||||
}
|
||||
|
||||
|
||||
// Start server TCP on port specified
|
||||
void ServerDrv::startServer(uint16_t port, uint8_t sock, uint8_t protMode)
|
||||
{
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(START_SERVER_TCP_CMD, PARAM_NUMS_3);
|
||||
SpiDrv::sendParam(port);
|
||||
SpiDrv::sendParam(&sock, 1);
|
||||
SpiDrv::sendParam(&protMode, 1, LAST_PARAM);
|
||||
|
||||
//Wait the reply elaboration
|
||||
SpiDrv::waitForSlaveReady();
|
||||
|
||||
// Wait for reply
|
||||
uint8_t _data = 0;
|
||||
uint8_t _dataLen = 0;
|
||||
if (!SpiDrv::waitResponseCmd(START_SERVER_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen))
|
||||
{
|
||||
WARN("error waitResponse");
|
||||
}
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
}
|
||||
|
||||
// Start server TCP on port specified
|
||||
void ServerDrv::startClient(uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode)
|
||||
{
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(START_CLIENT_TCP_CMD, PARAM_NUMS_4);
|
||||
SpiDrv::sendParam((uint8_t*)&ipAddress, sizeof(ipAddress));
|
||||
SpiDrv::sendParam(port);
|
||||
SpiDrv::sendParam(&sock, 1);
|
||||
SpiDrv::sendParam(&protMode, 1, LAST_PARAM);
|
||||
|
||||
//Wait the reply elaboration
|
||||
SpiDrv::waitForSlaveReady();
|
||||
|
||||
// Wait for reply
|
||||
uint8_t _data = 0;
|
||||
uint8_t _dataLen = 0;
|
||||
if (!SpiDrv::waitResponseCmd(START_CLIENT_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen))
|
||||
{
|
||||
WARN("error waitResponse");
|
||||
}
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
}
|
||||
|
||||
// Start server TCP on port specified
|
||||
void ServerDrv::stopClient(uint8_t sock)
|
||||
{
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(STOP_CLIENT_TCP_CMD, PARAM_NUMS_1);
|
||||
SpiDrv::sendParam(&sock, 1, LAST_PARAM);
|
||||
|
||||
//Wait the reply elaboration
|
||||
SpiDrv::waitForSlaveReady();
|
||||
|
||||
// Wait for reply
|
||||
uint8_t _data = 0;
|
||||
uint8_t _dataLen = 0;
|
||||
if (!SpiDrv::waitResponseCmd(STOP_CLIENT_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen))
|
||||
{
|
||||
WARN("error waitResponse");
|
||||
}
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
}
|
||||
|
||||
|
||||
uint8_t ServerDrv::getServerState(uint8_t sock)
|
||||
{
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(GET_STATE_TCP_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::waitResponseCmd(GET_STATE_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen))
|
||||
{
|
||||
WARN("error waitResponse");
|
||||
}
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
return _data;
|
||||
}
|
||||
|
||||
uint8_t ServerDrv::getClientState(uint8_t sock)
|
||||
{
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(GET_CLIENT_STATE_TCP_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::waitResponseCmd(GET_CLIENT_STATE_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen))
|
||||
{
|
||||
WARN("error waitResponse");
|
||||
}
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
return _data;
|
||||
}
|
||||
|
||||
uint16_t ServerDrv::availData(uint8_t sock)
|
||||
{
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(AVAIL_DATA_TCP_CMD, PARAM_NUMS_1);
|
||||
SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM);
|
||||
|
||||
//Wait the reply elaboration
|
||||
SpiDrv::waitForSlaveReady();
|
||||
|
||||
// Wait for reply
|
||||
uint8_t _dataLen = 0;
|
||||
uint16_t len = 0;
|
||||
|
||||
SpiDrv::waitResponseCmd(AVAIL_DATA_TCP_CMD, PARAM_NUMS_1, (uint8_t*)&len, &_dataLen);
|
||||
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
bool ServerDrv::getData(uint8_t sock, uint8_t *data, uint8_t peek)
|
||||
{
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(GET_DATA_TCP_CMD, PARAM_NUMS_2);
|
||||
SpiDrv::sendParam(&sock, sizeof(sock));
|
||||
SpiDrv::sendParam(peek, LAST_PARAM);
|
||||
|
||||
//Wait the reply elaboration
|
||||
SpiDrv::waitForSlaveReady();
|
||||
|
||||
// Wait for reply
|
||||
uint8_t _data = 0;
|
||||
uint8_t _dataLen = 0;
|
||||
if (!SpiDrv::waitResponseData8(GET_DATA_TCP_CMD, &_data, &_dataLen))
|
||||
{
|
||||
WARN("error waitResponse");
|
||||
}
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
if (_dataLen!=0)
|
||||
{
|
||||
*data = _data;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ServerDrv::getDataBuf(uint8_t sock, uint8_t *_data, uint16_t *_dataLen)
|
||||
{
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(GET_DATABUF_TCP_CMD, PARAM_NUMS_1);
|
||||
SpiDrv::sendBuffer(&sock, sizeof(sock), LAST_PARAM);
|
||||
|
||||
//Wait the reply elaboration
|
||||
SpiDrv::waitForSlaveReady();
|
||||
|
||||
// Wait for reply
|
||||
if (!SpiDrv::waitResponseData16(GET_DATABUF_TCP_CMD, _data, _dataLen))
|
||||
{
|
||||
WARN("error waitResponse");
|
||||
}
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
if (*_dataLen!=0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
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)
|
||||
{
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(SEND_DATA_TCP_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(SEND_DATA_TCP_CMD, &_data, &_dataLen))
|
||||
{
|
||||
WARN("error waitResponse");
|
||||
}
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
if (_dataLen!=0)
|
||||
{
|
||||
return (_data == 1);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
uint8_t ServerDrv::checkDataSent(uint8_t sock)
|
||||
{
|
||||
const uint16_t TIMEOUT_DATA_SENT = 25;
|
||||
uint16_t timeout = 0;
|
||||
uint8_t _data = 0;
|
||||
uint8_t _dataLen = 0;
|
||||
|
||||
do {
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(DATA_SENT_TCP_CMD, PARAM_NUMS_1);
|
||||
SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM);
|
||||
|
||||
//Wait the reply elaboration
|
||||
SpiDrv::waitForSlaveReady();
|
||||
|
||||
// Wait for reply
|
||||
if (!SpiDrv::waitResponseCmd(DATA_SENT_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen))
|
||||
{
|
||||
WARN("error waitResponse isDataSent");
|
||||
}
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
|
||||
if (_data) timeout = 0;
|
||||
else{
|
||||
++timeout;
|
||||
delay(100);
|
||||
}
|
||||
|
||||
}while((_data==0)&&(timeout<TIMEOUT_DATA_SENT));
|
||||
return (timeout==TIMEOUT_DATA_SENT)?0:1;
|
||||
}
|
||||
|
||||
ServerDrv serverDrv;
|
@ -1,60 +0,0 @@
|
||||
/*
|
||||
server_drv.h - Library for Arduino Wifi shield.
|
||||
Copyright (c) 2011-2014 Arduino. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef Server_Drv_h
|
||||
#define Server_Drv_h
|
||||
|
||||
#include <inttypes.h>
|
||||
#include "utility/wifi_spi.h"
|
||||
|
||||
typedef enum eProtMode {TCP_MODE, UDP_MODE}tProtMode;
|
||||
|
||||
class ServerDrv
|
||||
{
|
||||
public:
|
||||
|
||||
// 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 uint8_t getServerState(uint8_t sock);
|
||||
|
||||
static uint8_t getClientState(uint8_t sock);
|
||||
|
||||
static bool getData(uint8_t sock, uint8_t *data, uint8_t peek = 0);
|
||||
|
||||
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 sendUdpData(uint8_t sock);
|
||||
|
||||
static uint16_t availData(uint8_t sock);
|
||||
|
||||
static uint8_t checkDataSent(uint8_t sock);
|
||||
};
|
||||
|
||||
extern ServerDrv serverDrv;
|
||||
|
||||
#endif
|
@ -1,496 +0,0 @@
|
||||
/*
|
||||
spi_drv.cpp - Library for Arduino Wifi shield.
|
||||
Copyright (c) 2011-2014 Arduino. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include "Arduino.h"
|
||||
#include <SPI.h>
|
||||
#include "utility/spi_drv.h"
|
||||
#include "pins_arduino.h"
|
||||
//#define _DEBUG_
|
||||
extern "C" {
|
||||
#include "utility/debug.h"
|
||||
}
|
||||
|
||||
#define DATAOUT 11 // MOSI
|
||||
#define DATAIN 12 // MISO
|
||||
#define SPICLOCK 13 // sck
|
||||
#define SLAVESELECT 10 // ss
|
||||
#define SLAVEREADY 7 // handshake pin
|
||||
#define WIFILED 9 // led on wifi shield
|
||||
|
||||
#define DELAY_SPI(X) { int ii=0; do { asm volatile("nop"); } while (++ii < (X*F_CPU/16000000)); }
|
||||
#define DELAY_TRANSFER() DELAY_SPI(10)
|
||||
|
||||
void SpiDrv::begin()
|
||||
{
|
||||
SPI.begin();
|
||||
pinMode(SLAVESELECT, OUTPUT);
|
||||
pinMode(SLAVEREADY, INPUT);
|
||||
pinMode(WIFILED, OUTPUT);
|
||||
|
||||
// digitalWrite(SCK, LOW);
|
||||
// digitalWrite(MOSI, LOW);
|
||||
digitalWrite(SS, HIGH);
|
||||
digitalWrite(SLAVESELECT, HIGH);
|
||||
digitalWrite(WIFILED, LOW);
|
||||
|
||||
#ifdef _DEBUG_
|
||||
INIT_TRIGGER()
|
||||
#endif
|
||||
}
|
||||
|
||||
void SpiDrv::end() {
|
||||
SPI.end();
|
||||
}
|
||||
|
||||
void SpiDrv::spiSlaveSelect()
|
||||
{
|
||||
digitalWrite(SLAVESELECT,LOW);
|
||||
}
|
||||
|
||||
|
||||
void SpiDrv::spiSlaveDeselect()
|
||||
{
|
||||
digitalWrite(SLAVESELECT,HIGH);
|
||||
}
|
||||
|
||||
|
||||
char SpiDrv::spiTransfer(volatile char data)
|
||||
{
|
||||
char result = SPI.transfer(data);
|
||||
DELAY_TRANSFER();
|
||||
|
||||
return result; // return the received byte
|
||||
}
|
||||
|
||||
int SpiDrv::waitSpiChar(unsigned char waitChar)
|
||||
{
|
||||
int timeout = TIMEOUT_CHAR;
|
||||
unsigned char _readChar = 0;
|
||||
do{
|
||||
_readChar = readChar(); //get data byte
|
||||
if (_readChar == ERR_CMD)
|
||||
{
|
||||
WARN("Err cmd received\n");
|
||||
return -1;
|
||||
}
|
||||
}while((timeout-- > 0) && (_readChar != waitChar));
|
||||
return (_readChar == waitChar);
|
||||
}
|
||||
|
||||
int SpiDrv::readAndCheckChar(char checkChar, char* readChar)
|
||||
{
|
||||
getParam((uint8_t*)readChar);
|
||||
|
||||
return (*readChar == checkChar);
|
||||
}
|
||||
|
||||
char SpiDrv::readChar()
|
||||
{
|
||||
uint8_t readChar = 0;
|
||||
getParam(&readChar);
|
||||
return readChar;
|
||||
}
|
||||
|
||||
#define WAIT_START_CMD(x) waitSpiChar(START_CMD)
|
||||
|
||||
#define IF_CHECK_START_CMD(x) \
|
||||
if (!WAIT_START_CMD(_data)) \
|
||||
{ \
|
||||
TOGGLE_TRIGGER() \
|
||||
WARN("Error waiting START_CMD"); \
|
||||
return 0; \
|
||||
}else \
|
||||
|
||||
#define CHECK_DATA(check, x) \
|
||||
if (!readAndCheckChar(check, &x)) \
|
||||
{ \
|
||||
TOGGLE_TRIGGER() \
|
||||
WARN("Reply error"); \
|
||||
INFO2(check, (uint8_t)x); \
|
||||
return 0; \
|
||||
}else \
|
||||
|
||||
#define waitSlaveReady() (digitalRead(SLAVEREADY) == LOW)
|
||||
#define waitSlaveSign() (digitalRead(SLAVEREADY) == HIGH)
|
||||
#define waitSlaveSignalH() while(digitalRead(SLAVEREADY) != HIGH){}
|
||||
#define waitSlaveSignalL() while(digitalRead(SLAVEREADY) != LOW){}
|
||||
|
||||
void SpiDrv::waitForSlaveSign()
|
||||
{
|
||||
while (!waitSlaveSign());
|
||||
}
|
||||
|
||||
void SpiDrv::waitForSlaveReady()
|
||||
{
|
||||
while (!waitSlaveReady());
|
||||
}
|
||||
|
||||
void SpiDrv::getParam(uint8_t* param)
|
||||
{
|
||||
// Get Params data
|
||||
*param = spiTransfer(DUMMY_DATA);
|
||||
DELAY_TRANSFER();
|
||||
}
|
||||
|
||||
int SpiDrv::waitResponseCmd(uint8_t cmd, uint8_t numParam, uint8_t* param, uint8_t* param_len)
|
||||
{
|
||||
char _data = 0;
|
||||
int ii = 0;
|
||||
|
||||
IF_CHECK_START_CMD(_data)
|
||||
{
|
||||
CHECK_DATA(cmd | REPLY_FLAG, _data){};
|
||||
|
||||
CHECK_DATA(numParam, _data);
|
||||
{
|
||||
readParamLen8(param_len);
|
||||
for (ii=0; ii<(*param_len); ++ii)
|
||||
{
|
||||
// Get Params data
|
||||
//param[ii] = spiTransfer(DUMMY_DATA);
|
||||
getParam(¶m[ii]);
|
||||
}
|
||||
}
|
||||
|
||||
readAndCheckChar(END_CMD, &_data);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
/*
|
||||
int SpiDrv::waitResponse(uint8_t cmd, uint8_t numParam, uint8_t* param, uint16_t* param_len)
|
||||
{
|
||||
char _data = 0;
|
||||
int i =0, ii = 0;
|
||||
|
||||
IF_CHECK_START_CMD(_data)
|
||||
{
|
||||
CHECK_DATA(cmd | REPLY_FLAG, _data){};
|
||||
|
||||
CHECK_DATA(numParam, _data);
|
||||
{
|
||||
readParamLen16(param_len);
|
||||
for (ii=0; ii<(*param_len); ++ii)
|
||||
{
|
||||
// Get Params data
|
||||
param[ii] = spiTransfer(DUMMY_DATA);
|
||||
}
|
||||
}
|
||||
|
||||
readAndCheckChar(END_CMD, &_data);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
*/
|
||||
|
||||
int SpiDrv::waitResponseData16(uint8_t cmd, uint8_t* param, uint16_t* param_len)
|
||||
{
|
||||
char _data = 0;
|
||||
uint16_t ii = 0;
|
||||
|
||||
IF_CHECK_START_CMD(_data)
|
||||
{
|
||||
CHECK_DATA(cmd | REPLY_FLAG, _data){};
|
||||
|
||||
uint8_t numParam = readChar();
|
||||
if (numParam != 0)
|
||||
{
|
||||
readParamLen16(param_len);
|
||||
for (ii=0; ii<(*param_len); ++ii)
|
||||
{
|
||||
// Get Params data
|
||||
param[ii] = spiTransfer(DUMMY_DATA);
|
||||
}
|
||||
}
|
||||
|
||||
readAndCheckChar(END_CMD, &_data);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int SpiDrv::waitResponseData8(uint8_t cmd, uint8_t* param, uint8_t* param_len)
|
||||
{
|
||||
char _data = 0;
|
||||
int ii = 0;
|
||||
|
||||
IF_CHECK_START_CMD(_data)
|
||||
{
|
||||
CHECK_DATA(cmd | REPLY_FLAG, _data){};
|
||||
|
||||
uint8_t numParam = readChar();
|
||||
if (numParam != 0)
|
||||
{
|
||||
readParamLen8(param_len);
|
||||
for (ii=0; ii<(*param_len); ++ii)
|
||||
{
|
||||
// Get Params data
|
||||
param[ii] = spiTransfer(DUMMY_DATA);
|
||||
}
|
||||
}
|
||||
|
||||
readAndCheckChar(END_CMD, &_data);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int SpiDrv::waitResponseParams(uint8_t cmd, uint8_t numParam, tParam* params)
|
||||
{
|
||||
char _data = 0;
|
||||
int i =0, ii = 0;
|
||||
|
||||
|
||||
IF_CHECK_START_CMD(_data)
|
||||
{
|
||||
CHECK_DATA(cmd | REPLY_FLAG, _data){};
|
||||
|
||||
uint8_t _numParam = readChar();
|
||||
if (_numParam != 0)
|
||||
{
|
||||
for (i=0; i<_numParam; ++i)
|
||||
{
|
||||
params[i].paramLen = readParamLen8();
|
||||
for (ii=0; ii<params[i].paramLen; ++ii)
|
||||
{
|
||||
// Get Params data
|
||||
params[i].param[ii] = spiTransfer(DUMMY_DATA);
|
||||
}
|
||||
}
|
||||
} else
|
||||
{
|
||||
WARN("Error numParam == 0");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (numParam != _numParam)
|
||||
{
|
||||
WARN("Mismatch numParam");
|
||||
return 0;
|
||||
}
|
||||
|
||||
readAndCheckChar(END_CMD, &_data);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
int SpiDrv::waitResponse(uint8_t cmd, tParam* params, uint8_t* numParamRead, uint8_t maxNumParams)
|
||||
{
|
||||
char _data = 0;
|
||||
int i =0, ii = 0;
|
||||
|
||||
IF_CHECK_START_CMD(_data)
|
||||
{
|
||||
CHECK_DATA(cmd | REPLY_FLAG, _data){};
|
||||
|
||||
uint8_t numParam = readChar();
|
||||
|
||||
if (numParam > maxNumParams)
|
||||
{
|
||||
numParam = maxNumParams;
|
||||
}
|
||||
*numParamRead = numParam;
|
||||
if (numParam != 0)
|
||||
{
|
||||
for (i=0; i<numParam; ++i)
|
||||
{
|
||||
params[i].paramLen = readParamLen8();
|
||||
|
||||
for (ii=0; ii<params[i].paramLen; ++ii)
|
||||
{
|
||||
// Get Params data
|
||||
params[i].param[ii] = spiTransfer(DUMMY_DATA);
|
||||
}
|
||||
}
|
||||
} else
|
||||
{
|
||||
WARN("Error numParams == 0");
|
||||
Serial.println(cmd, 16);
|
||||
return 0;
|
||||
}
|
||||
readAndCheckChar(END_CMD, &_data);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
*/
|
||||
|
||||
int SpiDrv::waitResponse(uint8_t cmd, uint8_t* numParamRead, uint8_t** params, uint8_t maxNumParams)
|
||||
{
|
||||
char _data = 0;
|
||||
int i =0, ii = 0;
|
||||
|
||||
char *index[WL_SSID_MAX_LENGTH];
|
||||
|
||||
for (i = 0 ; i < WL_NETWORKS_LIST_MAXNUM ; i++)
|
||||
index[i] = (char *)params + WL_SSID_MAX_LENGTH*i;
|
||||
|
||||
IF_CHECK_START_CMD(_data)
|
||||
{
|
||||
CHECK_DATA(cmd | REPLY_FLAG, _data){};
|
||||
|
||||
uint8_t numParam = readChar();
|
||||
|
||||
if (numParam > maxNumParams)
|
||||
{
|
||||
numParam = maxNumParams;
|
||||
}
|
||||
*numParamRead = numParam;
|
||||
if (numParam != 0)
|
||||
{
|
||||
for (i=0; i<numParam; ++i)
|
||||
{
|
||||
uint8_t paramLen = readParamLen8();
|
||||
for (ii=0; ii<paramLen; ++ii)
|
||||
{
|
||||
//ssid[ii] = spiTransfer(DUMMY_DATA);
|
||||
// Get Params data
|
||||
index[i][ii] = (uint8_t)spiTransfer(DUMMY_DATA);
|
||||
|
||||
}
|
||||
index[i][ii]=0;
|
||||
}
|
||||
} else
|
||||
{
|
||||
WARN("Error numParams == 0");
|
||||
readAndCheckChar(END_CMD, &_data);
|
||||
return 0;
|
||||
}
|
||||
readAndCheckChar(END_CMD, &_data);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
void SpiDrv::sendParam(uint8_t* param, uint8_t param_len, uint8_t lastParam)
|
||||
{
|
||||
int i = 0;
|
||||
// Send Spi paramLen
|
||||
sendParamLen8(param_len);
|
||||
|
||||
// Send Spi param data
|
||||
for (i=0; i<param_len; ++i)
|
||||
{
|
||||
spiTransfer(param[i]);
|
||||
}
|
||||
|
||||
// if lastParam==1 Send Spi END CMD
|
||||
if (lastParam == 1)
|
||||
spiTransfer(END_CMD);
|
||||
}
|
||||
|
||||
void SpiDrv::sendParamLen8(uint8_t param_len)
|
||||
{
|
||||
// Send Spi paramLen
|
||||
spiTransfer(param_len);
|
||||
}
|
||||
|
||||
void SpiDrv::sendParamLen16(uint16_t param_len)
|
||||
{
|
||||
// Send Spi paramLen
|
||||
spiTransfer((uint8_t)((param_len & 0xff00)>>8));
|
||||
spiTransfer((uint8_t)(param_len & 0xff));
|
||||
}
|
||||
|
||||
uint8_t SpiDrv::readParamLen8(uint8_t* param_len)
|
||||
{
|
||||
uint8_t _param_len = spiTransfer(DUMMY_DATA);
|
||||
if (param_len != NULL)
|
||||
{
|
||||
*param_len = _param_len;
|
||||
}
|
||||
return _param_len;
|
||||
}
|
||||
|
||||
uint16_t SpiDrv::readParamLen16(uint16_t* param_len)
|
||||
{
|
||||
uint16_t _param_len = spiTransfer(DUMMY_DATA)<<8 | (spiTransfer(DUMMY_DATA)& 0xff);
|
||||
if (param_len != NULL)
|
||||
{
|
||||
*param_len = _param_len;
|
||||
}
|
||||
return _param_len;
|
||||
}
|
||||
|
||||
|
||||
void SpiDrv::sendBuffer(uint8_t* param, uint16_t param_len, uint8_t lastParam)
|
||||
{
|
||||
uint16_t i = 0;
|
||||
|
||||
// Send Spi paramLen
|
||||
sendParamLen16(param_len);
|
||||
|
||||
// Send Spi param data
|
||||
for (i=0; i<param_len; ++i)
|
||||
{
|
||||
spiTransfer(param[i]);
|
||||
}
|
||||
|
||||
// if lastParam==1 Send Spi END CMD
|
||||
if (lastParam == 1)
|
||||
spiTransfer(END_CMD);
|
||||
}
|
||||
|
||||
|
||||
void SpiDrv::sendParam(uint16_t param, uint8_t lastParam)
|
||||
{
|
||||
// Send Spi paramLen
|
||||
sendParamLen8(2);
|
||||
|
||||
spiTransfer((uint8_t)((param & 0xff00)>>8));
|
||||
spiTransfer((uint8_t)(param & 0xff));
|
||||
|
||||
// if lastParam==1 Send Spi END CMD
|
||||
if (lastParam == 1)
|
||||
spiTransfer(END_CMD);
|
||||
}
|
||||
|
||||
/* Cmd Struct Message */
|
||||
/* _________________________________________________________________________________ */
|
||||
/*| START CMD | C/R | CMD |[TOT LEN]| N.PARAM | PARAM LEN | PARAM | .. | END CMD | */
|
||||
/*|___________|______|______|_________|_________|___________|________|____|_________| */
|
||||
/*| 8 bit | 1bit | 7bit | 8bit | 8bit | 8bit | nbytes | .. | 8bit | */
|
||||
/*|___________|______|______|_________|_________|___________|________|____|_________| */
|
||||
|
||||
void SpiDrv::sendCmd(uint8_t cmd, uint8_t numParam)
|
||||
{
|
||||
// Send Spi START CMD
|
||||
spiTransfer(START_CMD);
|
||||
|
||||
//waitForSlaveSign();
|
||||
//wait the interrupt trigger on slave
|
||||
delayMicroseconds(SPI_START_CMD_DELAY);
|
||||
|
||||
// Send Spi C + cmd
|
||||
spiTransfer(cmd & ~(REPLY_FLAG));
|
||||
|
||||
// Send Spi totLen
|
||||
//spiTransfer(totLen);
|
||||
|
||||
// Send Spi numParam
|
||||
spiTransfer(numParam);
|
||||
|
||||
// If numParam == 0 send END CMD
|
||||
if (numParam == 0)
|
||||
spiTransfer(END_CMD);
|
||||
|
||||
}
|
||||
|
||||
SpiDrv spiDrv;
|
@ -1,106 +0,0 @@
|
||||
/*
|
||||
spi_drv.h - Library for Arduino Wifi shield.
|
||||
Copyright (c) 2011-2014 Arduino. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef SPI_Drv_h
|
||||
#define SPI_Drv_h
|
||||
|
||||
#include <inttypes.h>
|
||||
#include "utility/wifi_spi.h"
|
||||
|
||||
#define SPI_START_CMD_DELAY 10
|
||||
|
||||
#define NO_LAST_PARAM 0
|
||||
#define LAST_PARAM 1
|
||||
|
||||
#define DUMMY_DATA 0xFF
|
||||
|
||||
#define WAIT_FOR_SLAVE_SELECT() \
|
||||
if (!initialized) { \
|
||||
SpiDrv::begin(); \
|
||||
initialized = true; \
|
||||
} \
|
||||
SpiDrv::waitForSlaveReady(); \
|
||||
SpiDrv::spiSlaveSelect();
|
||||
|
||||
static bool initialized = false;
|
||||
|
||||
class SpiDrv
|
||||
{
|
||||
private:
|
||||
//static bool waitSlaveReady();
|
||||
static void waitForSlaveSign();
|
||||
static void getParam(uint8_t* param);
|
||||
public:
|
||||
|
||||
static void begin();
|
||||
|
||||
static void end();
|
||||
|
||||
static void spiDriverInit();
|
||||
|
||||
static void spiSlaveSelect();
|
||||
|
||||
static void spiSlaveDeselect();
|
||||
|
||||
static char spiTransfer(volatile char data);
|
||||
|
||||
static void waitForSlaveReady();
|
||||
|
||||
//static int waitSpiChar(char waitChar, char* readChar);
|
||||
|
||||
static int waitSpiChar(unsigned char waitChar);
|
||||
|
||||
static int readAndCheckChar(char checkChar, char* readChar);
|
||||
|
||||
static char readChar();
|
||||
|
||||
static int waitResponseParams(uint8_t cmd, uint8_t numParam, tParam* params);
|
||||
|
||||
static int waitResponseCmd(uint8_t cmd, uint8_t numParam, uint8_t* param, uint8_t* param_len);
|
||||
|
||||
static int waitResponseData8(uint8_t cmd, uint8_t* param, uint8_t* param_len);
|
||||
|
||||
static int waitResponseData16(uint8_t cmd, uint8_t* param, uint16_t* param_len);
|
||||
/*
|
||||
static int waitResponse(uint8_t cmd, tParam* params, uint8_t* numParamRead, uint8_t maxNumParams);
|
||||
|
||||
static int waitResponse(uint8_t cmd, uint8_t numParam, uint8_t* param, uint16_t* param_len);
|
||||
*/
|
||||
static int waitResponse(uint8_t cmd, uint8_t* numParamRead, uint8_t** params, uint8_t maxNumParams);
|
||||
|
||||
static void sendParam(uint8_t* param, uint8_t param_len, uint8_t lastParam = NO_LAST_PARAM);
|
||||
|
||||
static void sendParamLen8(uint8_t param_len);
|
||||
|
||||
static void sendParamLen16(uint16_t param_len);
|
||||
|
||||
static uint8_t readParamLen8(uint8_t* param_len = NULL);
|
||||
|
||||
static uint16_t readParamLen16(uint16_t* param_len = NULL);
|
||||
|
||||
static void sendBuffer(uint8_t* param, uint16_t param_len, uint8_t lastParam = NO_LAST_PARAM);
|
||||
|
||||
static void sendParam(uint16_t param, uint8_t lastParam = NO_LAST_PARAM);
|
||||
|
||||
static void sendCmd(uint8_t cmd, uint8_t numParam);
|
||||
};
|
||||
|
||||
extern SpiDrv spiDrv;
|
||||
|
||||
#endif
|
@ -1,579 +0,0 @@
|
||||
/*
|
||||
wifi_drv.cpp - Library for Arduino Wifi shield.
|
||||
Copyright (c) 2011-2014 Arduino. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "utility/spi_drv.h"
|
||||
#include "utility/wifi_drv.h"
|
||||
|
||||
#define _DEBUG_
|
||||
|
||||
extern "C" {
|
||||
#include "utility/wifi_spi.h"
|
||||
#include "utility/wl_types.h"
|
||||
#include "utility/debug.h"
|
||||
}
|
||||
|
||||
// Array of data to cache the information related to the networks discovered
|
||||
char WiFiDrv::_networkSsid[][WL_SSID_MAX_LENGTH] = {{"1"},{"2"},{"3"},{"4"},{"5"}};
|
||||
int32_t WiFiDrv::_networkRssi[WL_NETWORKS_LIST_MAXNUM] = { 0 };
|
||||
uint8_t WiFiDrv::_networkEncr[WL_NETWORKS_LIST_MAXNUM] = { 0 };
|
||||
|
||||
// Cached values of retrieved data
|
||||
char WiFiDrv::_ssid[] = {0};
|
||||
uint8_t WiFiDrv::_bssid[] = {0};
|
||||
uint8_t WiFiDrv::_mac[] = {0};
|
||||
uint8_t WiFiDrv::_localIp[] = {0};
|
||||
uint8_t WiFiDrv::_subnetMask[] = {0};
|
||||
uint8_t WiFiDrv::_gatewayIp[] = {0};
|
||||
// Firmware version
|
||||
char WiFiDrv::fwVersion[] = {0};
|
||||
|
||||
|
||||
// Private Methods
|
||||
|
||||
void WiFiDrv::getNetworkData(uint8_t *ip, uint8_t *mask, uint8_t *gwip)
|
||||
{
|
||||
tParam params[PARAM_NUMS_3] = { {0, (char*)ip}, {0, (char*)mask}, {0, (char*)gwip}};
|
||||
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(GET_IPADDR_CMD, PARAM_NUMS_1);
|
||||
|
||||
uint8_t _dummy = DUMMY_DATA;
|
||||
SpiDrv::sendParam(&_dummy, sizeof(_dummy), LAST_PARAM);
|
||||
|
||||
//Wait the reply elaboration
|
||||
SpiDrv::waitForSlaveReady();
|
||||
|
||||
// Wait for reply
|
||||
SpiDrv::waitResponseParams(GET_IPADDR_CMD, PARAM_NUMS_3, params);
|
||||
|
||||
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
|
||||
|
||||
|
||||
void WiFiDrv::wifiDriverInit()
|
||||
{
|
||||
SpiDrv::begin();
|
||||
}
|
||||
|
||||
int8_t WiFiDrv::wifiSetNetwork(char* ssid, uint8_t ssid_len)
|
||||
{
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(SET_NET_CMD, PARAM_NUMS_1);
|
||||
SpiDrv::sendParam((uint8_t*)ssid, ssid_len, LAST_PARAM);
|
||||
|
||||
//Wait the reply elaboration
|
||||
SpiDrv::waitForSlaveReady();
|
||||
|
||||
// Wait for reply
|
||||
uint8_t _data = 0;
|
||||
uint8_t _dataLen = 0;
|
||||
if (!SpiDrv::waitResponseCmd(SET_NET_CMD, PARAM_NUMS_1, &_data, &_dataLen))
|
||||
{
|
||||
WARN("error waitResponse");
|
||||
_data = WL_FAILURE;
|
||||
}
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
|
||||
return(_data == WIFI_SPI_ACK) ? WL_SUCCESS : WL_FAILURE;
|
||||
}
|
||||
|
||||
int8_t WiFiDrv::wifiSetPassphrase(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len)
|
||||
{
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(SET_PASSPHRASE_CMD, PARAM_NUMS_2);
|
||||
SpiDrv::sendParam((uint8_t*)ssid, ssid_len, NO_LAST_PARAM);
|
||||
SpiDrv::sendParam((uint8_t*)passphrase, len, LAST_PARAM);
|
||||
|
||||
//Wait the reply elaboration
|
||||
SpiDrv::waitForSlaveReady();
|
||||
|
||||
// Wait for reply
|
||||
uint8_t _data = 0;
|
||||
uint8_t _dataLen = 0;
|
||||
if (!SpiDrv::waitResponseCmd(SET_PASSPHRASE_CMD, PARAM_NUMS_1, &_data, &_dataLen))
|
||||
{
|
||||
WARN("error waitResponse");
|
||||
_data = WL_FAILURE;
|
||||
}
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
return _data;
|
||||
}
|
||||
|
||||
|
||||
int8_t WiFiDrv::wifiSetKey(char* ssid, uint8_t ssid_len, uint8_t key_idx, const void *key, const uint8_t len)
|
||||
{
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(SET_KEY_CMD, PARAM_NUMS_3);
|
||||
SpiDrv::sendParam((uint8_t*)ssid, ssid_len, NO_LAST_PARAM);
|
||||
SpiDrv::sendParam(&key_idx, KEY_IDX_LEN, NO_LAST_PARAM);
|
||||
SpiDrv::sendParam((uint8_t*)key, len, LAST_PARAM);
|
||||
|
||||
//Wait the reply elaboration
|
||||
SpiDrv::waitForSlaveReady();
|
||||
|
||||
// Wait for reply
|
||||
uint8_t _data = 0;
|
||||
uint8_t _dataLen = 0;
|
||||
if (!SpiDrv::waitResponseCmd(SET_KEY_CMD, PARAM_NUMS_1, &_data, &_dataLen))
|
||||
{
|
||||
WARN("error waitResponse");
|
||||
_data = WL_FAILURE;
|
||||
}
|
||||
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()
|
||||
{
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(DISCONNECT_CMD, PARAM_NUMS_1);
|
||||
|
||||
uint8_t _dummy = DUMMY_DATA;
|
||||
SpiDrv::sendParam(&_dummy, 1, LAST_PARAM);
|
||||
|
||||
//Wait the reply elaboration
|
||||
SpiDrv::waitForSlaveReady();
|
||||
|
||||
// Wait for reply
|
||||
uint8_t _data = 0;
|
||||
uint8_t _dataLen = 0;
|
||||
int8_t result = SpiDrv::waitResponseCmd(DISCONNECT_CMD, PARAM_NUMS_1, &_data, &_dataLen);
|
||||
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
uint8_t WiFiDrv::getConnectionStatus()
|
||||
{
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(GET_CONN_STATUS_CMD, PARAM_NUMS_0);
|
||||
|
||||
//Wait the reply elaboration
|
||||
SpiDrv::waitForSlaveReady();
|
||||
|
||||
// Wait for reply
|
||||
uint8_t _data = -1;
|
||||
uint8_t _dataLen = 0;
|
||||
SpiDrv::waitResponseCmd(GET_CONN_STATUS_CMD, PARAM_NUMS_1, &_data, &_dataLen);
|
||||
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
|
||||
return _data;
|
||||
}
|
||||
|
||||
uint8_t* WiFiDrv::getMacAddress()
|
||||
{
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(GET_MACADDR_CMD, PARAM_NUMS_1);
|
||||
|
||||
uint8_t _dummy = DUMMY_DATA;
|
||||
SpiDrv::sendParam(&_dummy, 1, LAST_PARAM);
|
||||
|
||||
//Wait the reply elaboration
|
||||
SpiDrv::waitForSlaveReady();
|
||||
|
||||
// Wait for reply
|
||||
uint8_t _dataLen = 0;
|
||||
SpiDrv::waitResponseCmd(GET_MACADDR_CMD, PARAM_NUMS_1, _mac, &_dataLen);
|
||||
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
|
||||
return _mac;
|
||||
}
|
||||
|
||||
void WiFiDrv::getIpAddress(IPAddress& ip)
|
||||
{
|
||||
getNetworkData(_localIp, _subnetMask, _gatewayIp);
|
||||
ip = _localIp;
|
||||
}
|
||||
|
||||
void WiFiDrv::getSubnetMask(IPAddress& mask)
|
||||
{
|
||||
getNetworkData(_localIp, _subnetMask, _gatewayIp);
|
||||
mask = _subnetMask;
|
||||
}
|
||||
|
||||
void WiFiDrv::getGatewayIP(IPAddress& ip)
|
||||
{
|
||||
getNetworkData(_localIp, _subnetMask, _gatewayIp);
|
||||
ip = _gatewayIp;
|
||||
}
|
||||
|
||||
char* WiFiDrv::getCurrentSSID()
|
||||
{
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(GET_CURR_SSID_CMD, PARAM_NUMS_1);
|
||||
|
||||
uint8_t _dummy = DUMMY_DATA;
|
||||
SpiDrv::sendParam(&_dummy, 1, LAST_PARAM);
|
||||
|
||||
//Wait the reply elaboration
|
||||
SpiDrv::waitForSlaveReady();
|
||||
|
||||
// Wait for reply
|
||||
uint8_t _dataLen = 0;
|
||||
SpiDrv::waitResponseCmd(GET_CURR_SSID_CMD, PARAM_NUMS_1, (uint8_t*)_ssid, &_dataLen);
|
||||
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
|
||||
return _ssid;
|
||||
}
|
||||
|
||||
uint8_t* WiFiDrv::getCurrentBSSID()
|
||||
{
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(GET_CURR_BSSID_CMD, PARAM_NUMS_1);
|
||||
|
||||
uint8_t _dummy = DUMMY_DATA;
|
||||
SpiDrv::sendParam(&_dummy, 1, LAST_PARAM);
|
||||
|
||||
//Wait the reply elaboration
|
||||
SpiDrv::waitForSlaveReady();
|
||||
|
||||
// Wait for reply
|
||||
uint8_t _dataLen = 0;
|
||||
SpiDrv::waitResponseCmd(GET_CURR_BSSID_CMD, PARAM_NUMS_1, _bssid, &_dataLen);
|
||||
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
|
||||
return _bssid;
|
||||
}
|
||||
|
||||
int32_t WiFiDrv::getCurrentRSSI()
|
||||
{
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(GET_CURR_RSSI_CMD, PARAM_NUMS_1);
|
||||
|
||||
uint8_t _dummy = DUMMY_DATA;
|
||||
SpiDrv::sendParam(&_dummy, 1, LAST_PARAM);
|
||||
|
||||
//Wait the reply elaboration
|
||||
SpiDrv::waitForSlaveReady();
|
||||
|
||||
// Wait for reply
|
||||
uint8_t _dataLen = 0;
|
||||
int32_t rssi = 0;
|
||||
SpiDrv::waitResponseCmd(GET_CURR_RSSI_CMD, PARAM_NUMS_1, (uint8_t*)&rssi, &_dataLen);
|
||||
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
|
||||
return rssi;
|
||||
}
|
||||
|
||||
uint8_t WiFiDrv::getCurrentEncryptionType()
|
||||
{
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(GET_CURR_ENCT_CMD, PARAM_NUMS_1);
|
||||
|
||||
uint8_t _dummy = DUMMY_DATA;
|
||||
SpiDrv::sendParam(&_dummy, 1, LAST_PARAM);
|
||||
|
||||
//Wait the reply elaboration
|
||||
SpiDrv::waitForSlaveReady();
|
||||
|
||||
// Wait for reply
|
||||
uint8_t dataLen = 0;
|
||||
uint8_t encType = 0;
|
||||
SpiDrv::waitResponseCmd(GET_CURR_ENCT_CMD, PARAM_NUMS_1, (uint8_t*)&encType, &dataLen);
|
||||
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
|
||||
return encType;
|
||||
}
|
||||
|
||||
int8_t WiFiDrv::startScanNetworks()
|
||||
{
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(START_SCAN_NETWORKS, PARAM_NUMS_0);
|
||||
|
||||
//Wait the reply elaboration
|
||||
SpiDrv::waitForSlaveReady();
|
||||
|
||||
// Wait for reply
|
||||
uint8_t _data = 0;
|
||||
uint8_t _dataLen = 0;
|
||||
|
||||
if (!SpiDrv::waitResponseCmd(START_SCAN_NETWORKS, PARAM_NUMS_1, &_data, &_dataLen))
|
||||
{
|
||||
WARN("error waitResponse");
|
||||
_data = WL_FAILURE;
|
||||
}
|
||||
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
|
||||
return (_data == WL_FAILURE)? _data : WL_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
uint8_t WiFiDrv::getScanNetworks()
|
||||
{
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(SCAN_NETWORKS, PARAM_NUMS_0);
|
||||
|
||||
//Wait the reply elaboration
|
||||
SpiDrv::waitForSlaveReady();
|
||||
|
||||
// Wait for reply
|
||||
uint8_t ssidListNum = 0;
|
||||
SpiDrv::waitResponse(SCAN_NETWORKS, &ssidListNum, (uint8_t**)_networkSsid, WL_NETWORKS_LIST_MAXNUM);
|
||||
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
|
||||
return ssidListNum;
|
||||
}
|
||||
|
||||
char* WiFiDrv::getSSIDNetoworks(uint8_t networkItem)
|
||||
{
|
||||
if (networkItem >= WL_NETWORKS_LIST_MAXNUM)
|
||||
return NULL;
|
||||
|
||||
return _networkSsid[networkItem];
|
||||
}
|
||||
|
||||
uint8_t WiFiDrv::getEncTypeNetowrks(uint8_t networkItem)
|
||||
{
|
||||
if (networkItem >= WL_NETWORKS_LIST_MAXNUM)
|
||||
return NULL;
|
||||
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(GET_IDX_ENCT_CMD, PARAM_NUMS_1);
|
||||
|
||||
SpiDrv::sendParam(&networkItem, 1, LAST_PARAM);
|
||||
|
||||
//Wait the reply elaboration
|
||||
SpiDrv::waitForSlaveReady();
|
||||
|
||||
// Wait for reply
|
||||
uint8_t dataLen = 0;
|
||||
uint8_t encType = 0;
|
||||
SpiDrv::waitResponseCmd(GET_IDX_ENCT_CMD, PARAM_NUMS_1, (uint8_t*)&encType, &dataLen);
|
||||
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
|
||||
return encType;
|
||||
}
|
||||
|
||||
int32_t WiFiDrv::getRSSINetoworks(uint8_t networkItem)
|
||||
{
|
||||
if (networkItem >= WL_NETWORKS_LIST_MAXNUM)
|
||||
return NULL;
|
||||
int32_t networkRssi = 0;
|
||||
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(GET_IDX_RSSI_CMD, PARAM_NUMS_1);
|
||||
|
||||
SpiDrv::sendParam(&networkItem, 1, LAST_PARAM);
|
||||
|
||||
//Wait the reply elaboration
|
||||
SpiDrv::waitForSlaveReady();
|
||||
|
||||
// Wait for reply
|
||||
uint8_t dataLen = 0;
|
||||
SpiDrv::waitResponseCmd(GET_IDX_RSSI_CMD, PARAM_NUMS_1, (uint8_t*)&networkRssi, &dataLen);
|
||||
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
|
||||
return networkRssi;
|
||||
}
|
||||
|
||||
uint8_t WiFiDrv::reqHostByName(const char* aHostname)
|
||||
{
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(REQ_HOST_BY_NAME_CMD, PARAM_NUMS_1);
|
||||
SpiDrv::sendParam((uint8_t*)aHostname, strlen(aHostname), LAST_PARAM);
|
||||
|
||||
//Wait the reply elaboration
|
||||
SpiDrv::waitForSlaveReady();
|
||||
|
||||
// Wait for reply
|
||||
uint8_t _data = 0;
|
||||
uint8_t _dataLen = 0;
|
||||
uint8_t result = SpiDrv::waitResponseCmd(REQ_HOST_BY_NAME_CMD, PARAM_NUMS_1, &_data, &_dataLen);
|
||||
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int WiFiDrv::getHostByName(IPAddress& aResult)
|
||||
{
|
||||
uint8_t _ipAddr[WL_IPV4_LENGTH];
|
||||
IPAddress dummy(0xFF,0xFF,0xFF,0xFF);
|
||||
int result = 0;
|
||||
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(GET_HOST_BY_NAME_CMD, PARAM_NUMS_0);
|
||||
|
||||
//Wait the reply elaboration
|
||||
SpiDrv::waitForSlaveReady();
|
||||
|
||||
// Wait for reply
|
||||
uint8_t _dataLen = 0;
|
||||
if (!SpiDrv::waitResponseCmd(GET_HOST_BY_NAME_CMD, PARAM_NUMS_1, _ipAddr, &_dataLen))
|
||||
{
|
||||
WARN("error waitResponse");
|
||||
}else{
|
||||
aResult = _ipAddr;
|
||||
result = (aResult != dummy);
|
||||
}
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
return result;
|
||||
}
|
||||
|
||||
int WiFiDrv::getHostByName(const char* aHostname, IPAddress& aResult)
|
||||
{
|
||||
uint8_t retry = 10;
|
||||
if (reqHostByName(aHostname))
|
||||
{
|
||||
while(!getHostByName(aResult) && --retry > 0)
|
||||
{
|
||||
delay(1000);
|
||||
}
|
||||
}else{
|
||||
return 0;
|
||||
}
|
||||
return (retry>0);
|
||||
}
|
||||
|
||||
char* WiFiDrv::getFwVersion()
|
||||
{
|
||||
WAIT_FOR_SLAVE_SELECT();
|
||||
// Send Command
|
||||
SpiDrv::sendCmd(GET_FW_VERSION_CMD, PARAM_NUMS_0);
|
||||
|
||||
//Wait the reply elaboration
|
||||
SpiDrv::waitForSlaveReady();
|
||||
|
||||
// Wait for reply
|
||||
uint8_t _dataLen = 0;
|
||||
if (!SpiDrv::waitResponseCmd(GET_FW_VERSION_CMD, PARAM_NUMS_1, (uint8_t*)fwVersion, &_dataLen))
|
||||
{
|
||||
WARN("error waitResponse");
|
||||
}
|
||||
SpiDrv::spiSlaveDeselect();
|
||||
return fwVersion;
|
||||
}
|
||||
|
||||
WiFiDrv wiFiDrv;
|
@ -1,267 +0,0 @@
|
||||
/*
|
||||
wifi_drv.h - Library for Arduino Wifi shield.
|
||||
Copyright (c) 2011-2014 Arduino. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#ifndef WiFi_Drv_h
|
||||
#define WiFi_Drv_h
|
||||
|
||||
#include <inttypes.h>
|
||||
#include "utility/wifi_spi.h"
|
||||
#include "IPAddress.h"
|
||||
#include "WiFiUdp.h"
|
||||
|
||||
// Key index length
|
||||
#define KEY_IDX_LEN 1
|
||||
// 5 secs of delay to have the connection established
|
||||
#define WL_DELAY_START_CONNECTION 5000
|
||||
// firmware version string length
|
||||
#define WL_FW_VER_LENGTH 6
|
||||
|
||||
class WiFiDrv
|
||||
{
|
||||
private:
|
||||
// settings of requested network
|
||||
static char _networkSsid[WL_NETWORKS_LIST_MAXNUM][WL_SSID_MAX_LENGTH];
|
||||
static int32_t _networkRssi[WL_NETWORKS_LIST_MAXNUM];
|
||||
static uint8_t _networkEncr[WL_NETWORKS_LIST_MAXNUM];
|
||||
|
||||
// firmware version string in the format a.b.c
|
||||
static char fwVersion[WL_FW_VER_LENGTH];
|
||||
|
||||
// settings of current selected network
|
||||
static char _ssid[WL_SSID_MAX_LENGTH];
|
||||
static uint8_t _bssid[WL_MAC_ADDR_LENGTH];
|
||||
static uint8_t _mac[WL_MAC_ADDR_LENGTH];
|
||||
static uint8_t _localIp[WL_IPV4_LENGTH];
|
||||
static uint8_t _subnetMask[WL_IPV4_LENGTH];
|
||||
static uint8_t _gatewayIp[WL_IPV4_LENGTH];
|
||||
|
||||
/*
|
||||
* Get network Data information
|
||||
*/
|
||||
static void getNetworkData(uint8_t *ip, uint8_t *mask, uint8_t *gwip);
|
||||
|
||||
static uint8_t reqHostByName(const char* aHostname);
|
||||
|
||||
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:
|
||||
|
||||
/*
|
||||
* Driver initialization
|
||||
*/
|
||||
static void wifiDriverInit();
|
||||
|
||||
/*
|
||||
* Set the desired network which the connection manager should try to
|
||||
* connect to.
|
||||
*
|
||||
* The ssid of the desired network should be specified.
|
||||
*
|
||||
* param ssid: The ssid of the desired network.
|
||||
* param ssid_len: Lenght of ssid string.
|
||||
* return: WL_SUCCESS or WL_FAILURE
|
||||
*/
|
||||
static int8_t wifiSetNetwork(char* ssid, uint8_t ssid_len);
|
||||
|
||||
/* Start Wifi connection with passphrase
|
||||
* the most secure supported mode will be automatically selected
|
||||
*
|
||||
* param ssid: Pointer to the SSID string.
|
||||
* param ssid_len: Lenght of ssid string.
|
||||
* param passphrase: Passphrase. Valid characters in a passphrase
|
||||
* must be between ASCII 32-126 (decimal).
|
||||
* param len: Lenght of passphrase string.
|
||||
* return: WL_SUCCESS or WL_FAILURE
|
||||
*/
|
||||
static int8_t wifiSetPassphrase(char* ssid, uint8_t ssid_len, const char *passphrase, const uint8_t len);
|
||||
|
||||
/* Start Wifi connection with WEP encryption.
|
||||
* Configure a key into the device. The key type (WEP-40, WEP-104)
|
||||
* is determined by the size of the key (5 bytes for WEP-40, 13 bytes for WEP-104).
|
||||
*
|
||||
* param ssid: Pointer to the SSID string.
|
||||
* param ssid_len: Lenght of ssid string.
|
||||
* param key_idx: The key index to set. Valid values are 0-3.
|
||||
* param key: Key input buffer.
|
||||
* param len: Lenght of key string.
|
||||
* return: WL_SUCCESS or WL_FAILURE
|
||||
*/
|
||||
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
|
||||
*
|
||||
* return: WL_SUCCESS or WL_FAILURE
|
||||
*/
|
||||
static int8_t disconnect();
|
||||
|
||||
/*
|
||||
* Disconnect from the network
|
||||
*
|
||||
* return: one value of wl_status_t enum
|
||||
*/
|
||||
static uint8_t getConnectionStatus();
|
||||
|
||||
/*
|
||||
* Get the interface MAC address.
|
||||
*
|
||||
* return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH
|
||||
*/
|
||||
static uint8_t* getMacAddress();
|
||||
|
||||
/*
|
||||
* Get the interface IP address.
|
||||
*
|
||||
* return: copy the ip address value in IPAddress object
|
||||
*/
|
||||
static void getIpAddress(IPAddress& ip);
|
||||
|
||||
/*
|
||||
* Get the interface subnet mask address.
|
||||
*
|
||||
* return: copy the subnet mask address value in IPAddress object
|
||||
*/
|
||||
static void getSubnetMask(IPAddress& mask);
|
||||
|
||||
/*
|
||||
* Get the gateway ip address.
|
||||
*
|
||||
* return: copy the gateway ip address value in IPAddress object
|
||||
*/
|
||||
static void getGatewayIP(IPAddress& ip);
|
||||
|
||||
/*
|
||||
* Return the current SSID associated with the network
|
||||
*
|
||||
* return: ssid string
|
||||
*/
|
||||
static char* getCurrentSSID();
|
||||
|
||||
/*
|
||||
* Return the current BSSID associated with the network.
|
||||
* It is the MAC address of the Access Point
|
||||
*
|
||||
* return: pointer to uint8_t array with length WL_MAC_ADDR_LENGTH
|
||||
*/
|
||||
static uint8_t* getCurrentBSSID();
|
||||
|
||||
/*
|
||||
* Return the current RSSI /Received Signal Strength in dBm)
|
||||
* associated with the network
|
||||
*
|
||||
* return: signed value
|
||||
*/
|
||||
static int32_t getCurrentRSSI();
|
||||
|
||||
/*
|
||||
* Return the Encryption Type associated with the network
|
||||
*
|
||||
* return: one value of wl_enc_type enum
|
||||
*/
|
||||
static uint8_t getCurrentEncryptionType();
|
||||
|
||||
/*
|
||||
* Start scan WiFi networks available
|
||||
*
|
||||
* return: Number of discovered networks
|
||||
*/
|
||||
static int8_t startScanNetworks();
|
||||
|
||||
/*
|
||||
* Get the networks available
|
||||
*
|
||||
* return: Number of discovered networks
|
||||
*/
|
||||
static uint8_t getScanNetworks();
|
||||
|
||||
/*
|
||||
* Return the SSID discovered during the network scan.
|
||||
*
|
||||
* param networkItem: specify from which network item want to get the information
|
||||
*
|
||||
* return: ssid string of the specified item on the networks scanned list
|
||||
*/
|
||||
static char* getSSIDNetoworks(uint8_t networkItem);
|
||||
|
||||
/*
|
||||
* Return the RSSI of the networks discovered during the scanNetworks
|
||||
*
|
||||
* param networkItem: specify from which network item want to get the information
|
||||
*
|
||||
* return: signed value of RSSI of the specified item on the networks scanned list
|
||||
*/
|
||||
static int32_t getRSSINetoworks(uint8_t networkItem);
|
||||
|
||||
/*
|
||||
* Return the encryption type of the networks discovered during the scanNetworks
|
||||
*
|
||||
* param networkItem: specify from which network item want to get the information
|
||||
*
|
||||
* return: encryption type (enum wl_enc_type) of the specified item on the networks scanned list
|
||||
*/
|
||||
static uint8_t getEncTypeNetowrks(uint8_t networkItem);
|
||||
|
||||
/*
|
||||
* Resolve the given hostname to an IP address.
|
||||
* param aHostname: Name to be resolved
|
||||
* param aResult: IPAddress structure to store the returned IP address
|
||||
* result: 1 if aIPAddrString was successfully converted to an IP address,
|
||||
* else error code
|
||||
*/
|
||||
static int getHostByName(const char* aHostname, IPAddress& aResult);
|
||||
|
||||
/*
|
||||
* Get the firmware version
|
||||
* result: version as string with this format a.b.c
|
||||
*/
|
||||
static char* getFwVersion();
|
||||
|
||||
friend class WiFiUDP;
|
||||
|
||||
};
|
||||
|
||||
extern WiFiDrv wiFiDrv;
|
||||
|
||||
#endif
|
@ -1,173 +0,0 @@
|
||||
/*
|
||||
wifi_spi.h - Library for Arduino Wifi shield.
|
||||
Copyright (c) 2011-2014 Arduino. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
#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
|
@ -1,72 +0,0 @@
|
||||
/*
|
||||
wl_definitions.h - Library for Arduino Wifi shield.
|
||||
Copyright (c) 2011-2014 Arduino. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
/*
|
||||
* wl_definitions.h
|
||||
*
|
||||
* Created on: Mar 6, 2011
|
||||
* Author: dlafauci
|
||||
*/
|
||||
|
||||
#ifndef WL_DEFINITIONS_H_
|
||||
#define WL_DEFINITIONS_H_
|
||||
|
||||
// Maximum size of a SSID
|
||||
#define WL_SSID_MAX_LENGTH 32
|
||||
// Length of passphrase. Valid lengths are 8-63.
|
||||
#define WL_WPA_KEY_MAX_LENGTH 63
|
||||
// Length of key in bytes. Valid values are 5 and 13.
|
||||
#define WL_WEP_KEY_MAX_LENGTH 13
|
||||
// Size of a MAC-address or BSSID
|
||||
#define WL_MAC_ADDR_LENGTH 6
|
||||
// Size of a MAC-address or BSSID
|
||||
#define WL_IPV4_LENGTH 4
|
||||
// Maximum size of a SSID list
|
||||
#define WL_NETWORKS_LIST_MAXNUM 10
|
||||
// Maxmium number of socket
|
||||
#define MAX_SOCK_NUM 4
|
||||
// Socket not available constant
|
||||
#define SOCK_NOT_AVAIL 255
|
||||
// Default state value for Wifi state field
|
||||
#define NA_STATE -1
|
||||
//Maximum number of attempts to establish wifi connection
|
||||
#define WL_MAX_ATTEMPT_CONNECTION 10
|
||||
|
||||
typedef enum {
|
||||
WL_NO_SHIELD = 255,
|
||||
WL_IDLE_STATUS = 0,
|
||||
WL_NO_SSID_AVAIL,
|
||||
WL_SCAN_COMPLETED,
|
||||
WL_CONNECTED,
|
||||
WL_CONNECT_FAILED,
|
||||
WL_CONNECTION_LOST,
|
||||
WL_DISCONNECTED
|
||||
} wl_status_t;
|
||||
|
||||
/* Encryption modes */
|
||||
enum wl_enc_type { /* Values map to 802.11 encryption suites... */
|
||||
ENC_TYPE_WEP = 5,
|
||||
ENC_TYPE_TKIP = 2,
|
||||
ENC_TYPE_CCMP = 4,
|
||||
/* ... except these two, 7 and 8 are reserved in 802.11-2007 */
|
||||
ENC_TYPE_NONE = 7,
|
||||
ENC_TYPE_AUTO = 8
|
||||
};
|
||||
|
||||
|
||||
#endif /* WL_DEFINITIONS_H_ */
|
@ -1,49 +0,0 @@
|
||||
/*
|
||||
wl_types.h - Library for Arduino Wifi shield.
|
||||
Copyright (c) 2011-2014 Arduino. All right reserved.
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
/*
|
||||
* wl_types.h
|
||||
*
|
||||
* Created on: Jul 30, 2010
|
||||
* Author: dlafauci
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _WL_TYPES_H_
|
||||
#define _WL_TYPES_H_
|
||||
|
||||
#include <inttypes.h>
|
||||
|
||||
typedef enum {
|
||||
WL_FAILURE = -1,
|
||||
WL_SUCCESS = 1,
|
||||
} wl_error_code_t;
|
||||
|
||||
/* Authentication modes */
|
||||
enum wl_auth_mode {
|
||||
AUTH_MODE_INVALID,
|
||||
AUTH_MODE_AUTO,
|
||||
AUTH_MODE_OPEN_SYSTEM,
|
||||
AUTH_MODE_SHARED_KEY,
|
||||
AUTH_MODE_WPA,
|
||||
AUTH_MODE_WPA2,
|
||||
AUTH_MODE_WPA_PSK,
|
||||
AUTH_MODE_WPA2_PSK
|
||||
};
|
||||
|
||||
#endif //_WL_TYPES_H_
|
14
hmi.cpp
14
hmi.cpp
@ -40,7 +40,7 @@ void Hmi::begin() {
|
||||
m_tft.fillScreen(BLACK);
|
||||
|
||||
if (! m_ctp.begin(40)) { // pass in 'sensitivity' coefficient
|
||||
Serial.println("Couldn't start FT6206 touchscreen controller");
|
||||
// Serial.println("Couldn't start FT6206 touchscreen controller");
|
||||
while (1);
|
||||
}
|
||||
|
||||
@ -99,21 +99,21 @@ void Hmi::exec() {
|
||||
static uint32_t oldSeconds = 0;
|
||||
if (m_seconds != oldSeconds) {
|
||||
oldSeconds = m_seconds;
|
||||
Serial << "LIGHT_OFF: " << m_lightOff << endl;
|
||||
Serial << "LIGHT_ON: " << m_lightOn << endl;
|
||||
Serial << "enable: " << m_backLightEnable << endl;
|
||||
Serial << "Seconds: " << m_seconds << endl;
|
||||
// Serial << "LIGHT_OFF: " << m_lightOff << endl;
|
||||
// Serial << "LIGHT_ON: " << m_lightOn << endl;
|
||||
// Serial << "enable: " << m_backLightEnable << endl;
|
||||
// Serial << "Seconds: " << m_seconds << endl;
|
||||
}
|
||||
|
||||
if (((m_seconds > m_lightOff) || (m_seconds < m_lightOn)) && !m_ctp.touched() && m_backLightEnable) {
|
||||
m_backLightEnable = false;
|
||||
digitalWrite(m_backLightPin, LOW);
|
||||
Serial << "lights off" << endl;
|
||||
// Serial << "lights off" << endl;
|
||||
}
|
||||
|
||||
if ((((m_seconds < m_lightOff) && (m_seconds > m_lightOn)) || m_ctp.touched()) && !m_backLightEnable) {
|
||||
m_backLightEnable = true;
|
||||
digitalWrite(m_backLightPin, HIGH);
|
||||
Serial << "lights on" << endl;
|
||||
// Serial << "lights on" << endl;
|
||||
}
|
||||
}
|
||||
|
@ -9,8 +9,7 @@
|
||||
|
||||
#include <avr/wdt.h>
|
||||
#include <Streaming.h>
|
||||
#include <WiFi.h>
|
||||
#include <WiFiUdp.h>
|
||||
#include <Ethernet.h>
|
||||
#include <Metro.h>
|
||||
#include <PubSubClient.h>
|
||||
#include "hmi.h"
|
||||
@ -28,15 +27,11 @@ static const char WATCHDOG_TOPIC[] = "IoT/Watchdog";
|
||||
|
||||
void callback(char* topic, byte* payload, unsigned int length);
|
||||
|
||||
char ssid[] = "MessWLAN";
|
||||
char pass[] = "UNVmpwbr6heQnMQ7ykXT";
|
||||
const uint8_t WIFI_ENABLE_PIN = 3;
|
||||
|
||||
// static uint8_t MAC[] = { 0x90, 0xA2, 0xDA, 0x00, 0x51, 0x08 };
|
||||
// const static char BROKER[] = "192.168.75.1";
|
||||
static uint8_t MAC[] = { 0x90, 0xA2, 0xDA, 0x00, 0x51, 0x08 };
|
||||
const static char BROKER[] = "mqttbroker";
|
||||
WiFiClient wifiClient;
|
||||
PubSubClient mqttClient = PubSubClient(BROKER, 1883, callback, wifiClient);
|
||||
EthernetClient client;
|
||||
PubSubClient mqttClient = PubSubClient(BROKER, 1883, callback, client);
|
||||
uint8_t disconnectState = 0;
|
||||
uint32_t disconnectTime = 0;
|
||||
Metro minute = Metro(60000);
|
||||
@ -47,12 +42,12 @@ uint32_t uptime;
|
||||
void callback(char* topic, byte* payload, unsigned int length) {
|
||||
const uint8_t BUFSIZE = 128;
|
||||
if ((length + 1) >= BUFSIZE) { // 1 for terminating NUL
|
||||
Serial << "Received message too long, ignore it" << endl;
|
||||
// Serial << "Received message too long, ignore it" << endl;
|
||||
} else {
|
||||
char buffer[BUFSIZE];
|
||||
memcpy(buffer, payload, length);
|
||||
*(buffer + length) = 0;
|
||||
Serial << "Received message: " << length << ", " << String(topic) << ", " << String(buffer) << endl;
|
||||
// Serial << "Received message: " << length << ", " << String(topic) << ", " << String(buffer) << endl;
|
||||
|
||||
if (!(strcmp(topic, MESSAGE_TOPIC) && strcmp(topic, ALARM_TOPIC))) {
|
||||
char *paramPtr = buffer;
|
||||
@ -76,7 +71,7 @@ void callback(char* topic, byte* payload, unsigned int length) {
|
||||
uint8_t slot = atoi(slotStr);
|
||||
|
||||
if (! strcmp(topic, ALARM_TOPIC)) {
|
||||
Serial << "Alarm" << endl;
|
||||
// Serial << "Alarm" << endl;
|
||||
} else if (! strcmp(topic, MESSAGE_TOPIC)) {
|
||||
hmi.updateMessage(slot, headerStr, bodyStr);
|
||||
}
|
||||
@ -87,72 +82,20 @@ void callback(char* topic, byte* payload, unsigned int length) {
|
||||
} else if (!strcmp(topic, WATCHDOG_TOPIC)) {
|
||||
wdt_reset();
|
||||
} else {
|
||||
Serial << "Strange, unknown topic received" << endl;
|
||||
// Serial << "Strange, unknown topic received" << endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void printWifiStatus() {
|
||||
char buffer[16];
|
||||
|
||||
// print the SSID of the network you're attached to:
|
||||
Serial << "SSID: " << WiFi.SSID() << endl;
|
||||
*(hmi.tft()) << "SSID: " << WiFi.SSID() << endl;
|
||||
|
||||
|
||||
// print your WiFi shield's IP address:
|
||||
IPAddress ip = WiFi.localIP();
|
||||
Serial << "IP Address: " << ip << endl;
|
||||
*(hmi.tft()) << "IP Address: " << ip << endl;
|
||||
|
||||
// print the received signal strength:
|
||||
long rssi = WiFi.RSSI();
|
||||
Serial << "signal strength (RSSI):" << rssi << " dBm" << endl;
|
||||
*(hmi.tft()) << "signal strength (RSSI):" << rssi << " dBm" << endl;
|
||||
}
|
||||
|
||||
void MqttClientNS::begin() {
|
||||
pinMode(WIFI_ENABLE_PIN, INPUT_PULLUP);
|
||||
delay(500);
|
||||
bool wifiEnabled = (digitalRead(WIFI_ENABLE_PIN) != 0);
|
||||
|
||||
// Ethernet.begin(MAC);
|
||||
Ethernet.begin(MAC);
|
||||
// Serial << "Got IP address: " << Ethernet.localIP() << endl;
|
||||
if (wifiEnabled) {
|
||||
// check for the presence of the shield:
|
||||
if (WiFi.status() == WL_NO_SHIELD) {
|
||||
Serial << "WiFi shield not present" << endl;
|
||||
*(hmi.tft()) << "WiFi shield not present" << endl;
|
||||
while(true);
|
||||
}
|
||||
|
||||
int status = WL_IDLE_STATUS;
|
||||
while ( status != WL_CONNECTED) {
|
||||
Serial << "Attempting to connect to SSID: " << ssid << endl;
|
||||
*(hmi.tft()) << "Attempting to connect to SSID: " << ssid << endl;
|
||||
status = WiFi.begin(ssid, pass);
|
||||
|
||||
// wait 10 seconds for connection:
|
||||
delay(10000);
|
||||
}
|
||||
Serial << "Connected." << endl;
|
||||
*(hmi.tft()) << "Connected." << endl;
|
||||
|
||||
printWifiStatus();
|
||||
|
||||
disconnectState = 3;
|
||||
disconnectTime = millis();
|
||||
}
|
||||
*(hmi.tft()) << Ethernet.localIP() << endl;
|
||||
disconnectState = 3;
|
||||
disconnectTime = millis();
|
||||
}
|
||||
|
||||
void MqttClientNS::exec() {
|
||||
// if (minute.check() == 1) {
|
||||
// byte r = Ethernet.maintain();
|
||||
// Serial << "Ethernet.maintain: " << r << endl;
|
||||
// if ((r == DHCP_CHECK_REBIND_FAIL) || (r == DHCP_CHECK_RENEW_FAIL)) {
|
||||
// }
|
||||
// }
|
||||
|
||||
if ((disconnectState == 0) && (! mqttClient.loop())) {
|
||||
disconnectState = 1;
|
||||
}
|
||||
@ -163,19 +106,19 @@ void MqttClientNS::exec() {
|
||||
// everything fine
|
||||
break;
|
||||
case 1:
|
||||
Serial.println("discState 1");
|
||||
// Serial.println("discState 1");
|
||||
mqttClient.disconnect();
|
||||
disconnectTime = millis();
|
||||
disconnectState = 2;
|
||||
break;
|
||||
case 2:
|
||||
Serial.println("discState 3");
|
||||
// Serial.println("discState 3");
|
||||
if (disconnectTime + 2000 < millis()) {
|
||||
disconnectState = 3;
|
||||
}
|
||||
break;
|
||||
case 3:
|
||||
Serial.println("discState 3");
|
||||
// Serial.println("discState 3");
|
||||
if (mqttClient.connect("Monitor")) {
|
||||
mqttClient.subscribe(MESSAGE_TOPIC);
|
||||
mqttClient.subscribe(ALARM_TOPIC);
|
||||
@ -196,7 +139,7 @@ void MqttClientNS::exec() {
|
||||
if (second.check() == 1) {
|
||||
uptime++;
|
||||
|
||||
Serial.println("mqtt tick");
|
||||
// Serial.println("mqtt tick");
|
||||
|
||||
if (disconnectState == 0) {
|
||||
String msg = String("{ \"metadata\": { \"device\": \"Monitor\" }, \"data\": { \"uptime\": ") + uptime + String("}}");
|
||||
|
Loading…
x
Reference in New Issue
Block a user