-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #62 from soburi/microip_improvment
MicroIP improvment
- Loading branch information
Showing
15 changed files
with
489 additions
and
135 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
libraries/uIP/examples/Udp7EchoClientServer/Udp7EchoClientServer.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
/* | ||
Udp7EchoClientServer: | ||
This is a client / server program which communicate | ||
by "well-known udp/7" echo protocol. | ||
Write same program to 2 nodes to use. | ||
One is act as server, the other is client. | ||
Program is start as echo server at boot up. | ||
Server send response same data what if data received. | ||
Enter the IPv6 address of the server to serial, | ||
and act as a client when the address is recognized. | ||
This program must compile with [RPL: "disabled"] configuration. | ||
created 29 May 2018 | ||
by Tokita Hiroshi | ||
*/ | ||
|
||
#include <MicroIp.h> | ||
#include <MicroIpUdp.h> | ||
|
||
IPAddress destAddr; | ||
|
||
const size_t MAX_PAYLOAD_LEN = 33; | ||
const size_t MAX_V6_ADDR_STR = 40; | ||
|
||
const unsigned int ECHO_PORT = 7; | ||
|
||
char packetBuffer[MAX_PAYLOAD_LEN]; | ||
char serialBuffer[MAX_V6_ADDR_STR]; | ||
size_t serialBufferLen; | ||
|
||
MicroIPUDP Udp; | ||
|
||
void setup() { | ||
Serial.begin(1000000); | ||
|
||
Serial.print("Echo protocol ("); | ||
Serial.print(ECHO_PORT); | ||
Serial.println("/udp) listen start."); | ||
Serial.println(); | ||
|
||
MicroIP.begin(); | ||
|
||
// Show link-local address | ||
Serial.print("This linklocal Address is "); | ||
Serial.println(MicroIP.linklocalAddress()); | ||
Serial.println(); | ||
|
||
Udp.begin(ECHO_PORT); | ||
|
||
Serial.println(); | ||
Serial.println("Enter destination IP address."); | ||
Serial.println("Or invalid IP address is entered, stop sending."); | ||
Serial.println(); | ||
} | ||
|
||
void loop() { | ||
|
||
if (Serial.available()) | ||
{ | ||
memset(serialBuffer, 0, MAX_V6_ADDR_STR); | ||
serialBufferLen = Serial.readBytes(serialBuffer, MAX_V6_ADDR_STR); | ||
|
||
// Set destination IPaddress when string received from serial line. | ||
if (destAddr == IN6ADDR_ANY_INIT) | ||
{ | ||
bool valid = destAddr.fromString(serialBuffer); | ||
if (valid) | ||
{ | ||
Serial.print("Destination IP is "); | ||
Serial.println(destAddr); | ||
Serial.println("Enter message to send."); | ||
} | ||
else | ||
{ | ||
Serial.print(serialBuffer); | ||
Serial.println(" is invalid. Stop sending."); | ||
} | ||
serialBufferLen = 0; | ||
} | ||
} | ||
|
||
// When destination is set, send serial buffer | ||
if (!(destAddr == IN6ADDR_ANY_INIT) && serialBufferLen != 0) | ||
{ | ||
// construct send data | ||
memcpy(packetBuffer, serialBuffer, serialBufferLen); | ||
|
||
Serial.print("Send to ["); | ||
Serial.print(destAddr); | ||
Serial.print("]:"); | ||
Serial.print(ECHO_PORT); | ||
Serial.print(" :"); | ||
Serial.println(serialBuffer); | ||
|
||
// send packet | ||
Udp.beginPacket(destAddr, ECHO_PORT); | ||
Udp.write(serialBuffer, min(MAX_PAYLOAD_LEN, serialBufferLen) ); | ||
Udp.endPacket(); | ||
|
||
serialBufferLen = 0; | ||
} | ||
|
||
// print received data. | ||
while (int packetSize = Udp.parsePacket()) { | ||
Serial.print("Received packet from ["); | ||
Serial.print(Udp.remoteIP()); | ||
Serial.print("]:"); | ||
Serial.print(Udp.remotePort()); | ||
Serial.print(" datasize="); | ||
Serial.println(packetSize); | ||
|
||
// Read the packet into packetBufffer | ||
memset(packetBuffer, 0, MAX_PAYLOAD_LEN); | ||
Udp.read(packetBuffer, MAX_PAYLOAD_LEN); | ||
|
||
if (destAddr == Udp.remoteIP()) { | ||
// Not response to echo. | ||
Serial.print("Received: "); | ||
Serial.println(packetBuffer); | ||
} | ||
else { | ||
// Send echo | ||
Serial.print("Echo: "); | ||
Serial.println(packetBuffer); | ||
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); | ||
Udp.write(packetBuffer, packetSize); | ||
Udp.endPacket(); | ||
} | ||
} | ||
|
||
} |
113 changes: 84 additions & 29 deletions
113
libraries/uIP/examples/udp-echo-client/udp-echo-client.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,107 @@ | ||
/* | ||
UDP Echo client | ||
A simple UDP communication demo. | ||
The program send simple message to 'udp-echo-server-rpl' node, | ||
the server node send echo on the message received. | ||
The received echo message shows on serial port. | ||
Even if RPL is enabled or disabled works. | ||
Use same settings to compile server side programs. | ||
This programs behavior is based on contiki os's | ||
example(examples/udp-ipv6/udp-client.c). | ||
created 24 Jan 2016 | ||
modified 29 May 2018 | ||
by Tokita Hiroshi | ||
*/ | ||
|
||
#include <MicroIp.h> | ||
#include <MicroIpUdp.h> | ||
#include <IPAddress.h> | ||
|
||
IPAddress server(0xaaaa, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0001); | ||
|
||
unsigned int localPort = 7; | ||
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; | ||
const size_t MAX_PAYLOAD_LEN = 40; | ||
const char* UDP_CONNECTION_ADDR = "contiki-udp-server.local"; | ||
const unsigned short LOCAL_PORT = 3001; | ||
const unsigned short DEST_PORT = 3000; | ||
const int INTERVAL = 15; | ||
|
||
MicroIPUDP Udp; | ||
IPAddress server; | ||
|
||
char packetBuffer[MAX_PAYLOAD_LEN]; | ||
long lastsend; | ||
long seq_id; | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
Serial.begin(1000000); | ||
Serial.println("Start udp-echo-cleint"); | ||
MicroIP.begin(); | ||
|
||
server = MicroIP.lookup("udp-echo-server.local"); | ||
Udp.begin(localPort); | ||
server = MicroIP.lookup(UDP_CONNECTION_ADDR); | ||
|
||
if (server == IN6ADDR_ANY_INIT) { | ||
Serial.print("Server ["); | ||
Serial.print(UDP_CONNECTION_ADDR); | ||
Serial.println("] is not found."); | ||
while (true) { | ||
yield(); | ||
} | ||
} | ||
|
||
Serial.print("Server "); | ||
Serial.print(UDP_CONNECTION_ADDR); | ||
Serial.print(" is ["); | ||
Serial.print(server); | ||
Serial.println("]"); | ||
|
||
Udp.begin(LOCAL_PORT); | ||
Serial.print("Start listen port:"); | ||
Serial.println(LOCAL_PORT); | ||
} | ||
|
||
void loop() { | ||
|
||
int avail = Serial.available(); | ||
if(avail) { | ||
int len = min(avail, UDP_TX_PACKET_MAX_SIZE); | ||
long now = millis(); | ||
|
||
for(int i=0; i<len; i++) { | ||
packetBuffer[i] = Serial.read(); | ||
} | ||
// Periodically send. | ||
if ((now - lastsend) > (INTERVAL * 1000)) { | ||
// format message | ||
memset(packetBuffer, 0, MAX_PAYLOAD_LEN); | ||
strcpy(packetBuffer, "Hello "); | ||
itoa(++seq_id, packetBuffer + strlen(packetBuffer), 10); | ||
strcpy(packetBuffer + strlen(packetBuffer), " from the client"); | ||
|
||
Udp.beginPacket(server, localPort); | ||
Udp.write(packetBuffer, len); | ||
Serial.print("Client sending to "); | ||
Serial.print(server); | ||
Serial.print(" (msg: "); | ||
Serial.print(packetBuffer); | ||
Serial.println(");"); | ||
|
||
// send packet | ||
Udp.beginPacket(server, DEST_PORT); | ||
Udp.write(packetBuffer, strlen(packetBuffer)); | ||
Udp.endPacket(); | ||
|
||
lastsend = now; | ||
} | ||
|
||
while(int packetSize = Udp.parsePacket()) { | ||
Serial.print("Received packet of size "); | ||
Serial.println(packetSize); | ||
Serial.print("From "); | ||
IPAddress remote = Udp.remoteIP(); | ||
Serial.print(remote); | ||
Serial.print(", port "); | ||
Serial.println(Udp.remotePort()); | ||
while (int packetSize = Udp.parsePacket()) { | ||
/* | ||
//more info | ||
Serial.print("Receive from "); | ||
Serial.print(Udp.remoteIP()); | ||
Serial.print(":"); | ||
Serial.print(Udp.remotePort()); | ||
Serial.print(" size:"); | ||
Serial.println(packetSize); | ||
*/ | ||
|
||
// read the packet into packetBufffer | ||
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE); | ||
Serial.println("Contents:"); | ||
for(int i=0; i<packetSize; i++) { | ||
Serial.print(packetBuffer[i]); | ||
} | ||
Serial.println(); | ||
Udp.read(packetBuffer, packetSize); | ||
Serial.print("Response from the server: '"); | ||
Serial.print(packetBuffer); | ||
Serial.println("'"); | ||
} | ||
|
||
} |
Oops, something went wrong.