Skip to content

Commit 7aced9f

Browse files
authored
Merge pull request #62 from soburi/microip_improvment
MicroIP improvment
2 parents cf57182 + b6ab939 commit 7aced9f

File tree

15 files changed

+489
-135
lines changed

15 files changed

+489
-135
lines changed

boards.txt

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
menu.config=Configuration
2-
menu.trace=Debug Trace
2+
menu.trace=Trace
3+
menu.rpl=RPL
34

45
jn516x.name=NXP JN516x
56
jn516x.upload.tool=contiki-makehelper
@@ -11,7 +12,7 @@ jn516x.build.core=arduino
1112
jn516x.build.variant=jn516x
1213
jn516x.build.compiler.path=C:/NXP/bstudio_nxp/sdk/Tools/ba-elf-ba2-r36379/bin
1314
jn516x.build.uploader.path=""
14-
jn516x.build.make.vars=target_makefile='{runtime.platform.path}/variants/{build.variant}/Makefile.variant' CONTIKI_TARGET_MAIN=override-contiki-jn516x-main.c TARGET_WITH_UART1=1 PLATFORM_CONF_H='{runtime.platform.path}/variants/{build.variant}/{build.variant}-platform-conf.h' PREINCLUDE_FILES='{build.core.path}/weak_pragmas.h' {build.trace}
15+
jn516x.build.make.vars=target_makefile='{runtime.platform.path}/variants/{build.variant}/Makefile.variant' CONTIKI_TARGET_MAIN=override-contiki-jn516x-main.c TARGET_WITH_UART1=1 PLATFORM_CONF_H='{runtime.platform.path}/variants/{build.variant}/{build.variant}-platform-conf.h' PREINCLUDE_FILES='{build.core.path}/weak_pragmas.h' {build.trace} {build.rpl}
1516
jn516x.build.make.goals={build.project_name}.bin {build.project_name}.hex {build.project_name}.elf
1617
jn516x.menu.config.jn5169=JN5169
1718
jn516x.menu.config.jn5169.build.config_vars=CHIP=JN5169 LINKCMD='{runtime.platform.path}/variants/{build.variant}/ldscript/Arduinize_JN5169.ld'
@@ -23,10 +24,14 @@ jn516x.menu.config.jn5161=JN5161
2324
jn516x.menu.config.jn5161.build.config_vars=CHIP=JN5161 LINKCMD='{runtime.platform.path}/variants/{build.variant}/ldscript/Arduinize.ld'
2425
jn516x.menu.config.meshbee=Mesh Bee
2526
jn516x.menu.config.meshbee.build.config_vars=CHIP=JN5168 LINKCMD='{runtime.platform.path}/variants/{build.variant}/ldscript/Arduinize.ld'
26-
jn516x.menu.trace.OFF=Trace Off
27+
jn516x.menu.trace.OFF=Off
2728
jn516x.menu.trace.OFF.build.trace=
28-
jn516x.menu.trace.ON=Trace On
29+
jn516x.menu.trace.ON=On
2930
jn516x.menu.trace.ON.build.trace=TRACE=1
31+
jn516x.menu.rpl.ON=Enable
32+
jn516x.menu.rpl.ON.build.rpl=CONTIKI_WITH_RPL=1
33+
jn516x.menu.rpl.OFF=Disable
34+
jn516x.menu.rpl.OFF.build.rpl=CONTIKI_WITH_RPL=0
3035

3136
twelite.name=Mono Wireless TWELITE
3237
twelite.upload.tool=contiki-makehelper
@@ -38,16 +43,20 @@ twelite.build.core=arduino
3843
twelite.build.variant=jn516x
3944
twelite.build.compiler.path=C:/MWSDK/Tools/ba-elf-ba2-r36379/bin
4045
twelite.build.uploader.path=""
41-
twelite.build.make.vars=target_makefile='{runtime.platform.path}/variants/{build.variant}/Makefile.variant' CONTIKI_TARGET_MAIN=override-contiki-jn516x-main.c TARGET_WITH_UART1=1 PLATFORM_CONF_H='{runtime.platform.path}/variants/{build.variant}/{build.variant}-platform-conf.h' PREINCLUDE_FILES='{build.core.path}/weak_pragmas.h' {build.trace} SDK_BASE_DIR=C:/MWSDK/ChipLib/SW4063V1416 FLASH_PROGRAMMER=C:/MWSDK/Tools/ProductionFlashProgrammer/JN51xxProgrammer.exe
46+
twelite.build.make.vars=target_makefile='{runtime.platform.path}/variants/{build.variant}/Makefile.variant' CONTIKI_TARGET_MAIN=override-contiki-jn516x-main.c TARGET_WITH_UART1=1 PLATFORM_CONF_H='{runtime.platform.path}/variants/{build.variant}/{build.variant}-platform-conf.h' PREINCLUDE_FILES='{build.core.path}/weak_pragmas.h' {build.trace} {build.rpl} SDK_BASE_DIR=C:/MWSDK/ChipLib/SW4063V1416 FLASH_PROGRAMMER=C:/MWSDK/Tools/ProductionFlashProgrammer/JN51xxProgrammer.exe
4247
twelite.build.make.goals={build.project_name}.bin {build.project_name}.hex {build.project_name}.elf
4348
twelite.menu.config.TWELITE_BLUE=TWELITE BLUE
4449
twelite.menu.config.TWELITE_BLUE.build.config_vars=CHIP=JN5164 LINKCMD='{runtime.platform.path}/variants/{build.variant}/ldscript/Arduinize.ld'
4550
twelite.menu.config.TWELITE_RED=TWELITE RED
4651
twelite.menu.config.TWELITE_RED.build.config_vars=CHIP=JN5169 LINKCMD='{runtime.platform.path}/variants/{build.variant}/ldscript/Arduinize_JN5169.ld'
47-
twelite.menu.trace.OFF=Trace Off
52+
twelite.menu.trace.OFF=Off
4853
twelite.menu.trace.OFF.build.trace=
49-
twelite.menu.trace.ON=Trace On
54+
twelite.menu.trace.ON=On
5055
twelite.menu.trace.ON.build.trace=TRACE=1
56+
twelite.menu.rpl.ON=Enable
57+
twelite.menu.rpl.ON.build.rpl=CONTIKI_WITH_RPL=1
58+
twelite.menu.rpl.OFF=Disable
59+
twelite.menu.rpl.OFF.build.rpl=CONTIKI_WITH_RPL=0
5160

5261
native.name=native
5362
native.upload.tool=contiki-makehelper

cores/arduino/IPAddress.h

+9
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,14 @@ class IPAddress : public Printable {
8787
};
8888

8989
const IPAddress INADDR_NONE;
90+
#if NETSTACK_CONF_WITH_IPV6
91+
const IPAddress IN6ADDR_ANY_INIT(0, 0, 0, 0, 0, 0, 0, 0);
92+
const IPAddress IN6ADDR_LOOPBACK_INIT(0, 0, 0, 0, 0, 0, 0, 1);
93+
const IPAddress IN6ADDR_LINKLOCAL_ALLNODES_INIT( 0xff02,0,0,0, 0,0,0,1);
94+
const IPAddress IN6ADDR_LINKLOCAL_ALLROUTERS_INIT(0xff02,0,0,0, 0,0,0,2);
95+
const IPAddress IN6ADDR_INTERFACELOCAL_ALLNODES_INIT(0xff01,0,0,0, 0,0,0,1);
96+
const IPAddress IN6ADDR_INTERFACELOCAL_ALLROUTERS_INIT(0xff01,0,0,0, 0,0,0,2);
97+
const IPAddress IN6ADDR_SITELOCAL_ALLROUTERS_INIT(0xff05,0,0,0, 0,0,0,2);
98+
#endif
9099

91100
#endif

cores/arduino/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ PROCESS_THREAD(main_thread, ev, data)
174174

175175
void yield()
176176
{
177-
YIELD_TO_MAIN_THREAD(WT_PAUSE, NULL, NULL, NULL, NULL);
177+
delay(0);
178178
}
179179

180180
void yield_until(fp_run run, void* run_param, fp_condition condition, void* condition_param)

dist/submodules.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
"path": "system/contiki",
44
"url": "https://github.com/soburi/contiki",
5-
"revision": "441f679658fee95fd93013af51d884cce6c988ab"
5+
"revision": "c661080b2946bac7e616ecd7e32e95a8863f134e"
66
},
77
{
88
"path": "system/contiki/cpu/cc26xx-cc13xx/lib/cc13xxware",
@@ -34,4 +34,4 @@
3434
"url": "https://github.com/g-oikonomou/sensniff.git",
3535
"revision": "0d57c1129b601d29a58bffe6e34803af5e1701af"
3636
}
37-
]
37+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/*
2+
Udp7EchoClientServer:
3+
4+
This is a client / server program which communicate
5+
by "well-known udp/7" echo protocol.
6+
7+
Write same program to 2 nodes to use.
8+
One is act as server, the other is client.
9+
10+
Program is start as echo server at boot up.
11+
Server send response same data what if data received.
12+
Enter the IPv6 address of the server to serial,
13+
and act as a client when the address is recognized.
14+
15+
This program must compile with [RPL: "disabled"] configuration.
16+
17+
created 29 May 2018
18+
by Tokita Hiroshi
19+
*/
20+
21+
#include <MicroIp.h>
22+
#include <MicroIpUdp.h>
23+
24+
IPAddress destAddr;
25+
26+
const size_t MAX_PAYLOAD_LEN = 33;
27+
const size_t MAX_V6_ADDR_STR = 40;
28+
29+
const unsigned int ECHO_PORT = 7;
30+
31+
char packetBuffer[MAX_PAYLOAD_LEN];
32+
char serialBuffer[MAX_V6_ADDR_STR];
33+
size_t serialBufferLen;
34+
35+
MicroIPUDP Udp;
36+
37+
void setup() {
38+
Serial.begin(1000000);
39+
40+
Serial.print("Echo protocol (");
41+
Serial.print(ECHO_PORT);
42+
Serial.println("/udp) listen start.");
43+
Serial.println();
44+
45+
MicroIP.begin();
46+
47+
// Show link-local address
48+
Serial.print("This linklocal Address is ");
49+
Serial.println(MicroIP.linklocalAddress());
50+
Serial.println();
51+
52+
Udp.begin(ECHO_PORT);
53+
54+
Serial.println();
55+
Serial.println("Enter destination IP address.");
56+
Serial.println("Or invalid IP address is entered, stop sending.");
57+
Serial.println();
58+
}
59+
60+
void loop() {
61+
62+
if (Serial.available())
63+
{
64+
memset(serialBuffer, 0, MAX_V6_ADDR_STR);
65+
serialBufferLen = Serial.readBytes(serialBuffer, MAX_V6_ADDR_STR);
66+
67+
// Set destination IPaddress when string received from serial line.
68+
if (destAddr == IN6ADDR_ANY_INIT)
69+
{
70+
bool valid = destAddr.fromString(serialBuffer);
71+
if (valid)
72+
{
73+
Serial.print("Destination IP is ");
74+
Serial.println(destAddr);
75+
Serial.println("Enter message to send.");
76+
}
77+
else
78+
{
79+
Serial.print(serialBuffer);
80+
Serial.println(" is invalid. Stop sending.");
81+
}
82+
serialBufferLen = 0;
83+
}
84+
}
85+
86+
// When destination is set, send serial buffer
87+
if (!(destAddr == IN6ADDR_ANY_INIT) && serialBufferLen != 0)
88+
{
89+
// construct send data
90+
memcpy(packetBuffer, serialBuffer, serialBufferLen);
91+
92+
Serial.print("Send to [");
93+
Serial.print(destAddr);
94+
Serial.print("]:");
95+
Serial.print(ECHO_PORT);
96+
Serial.print(" :");
97+
Serial.println(serialBuffer);
98+
99+
// send packet
100+
Udp.beginPacket(destAddr, ECHO_PORT);
101+
Udp.write(serialBuffer, min(MAX_PAYLOAD_LEN, serialBufferLen) );
102+
Udp.endPacket();
103+
104+
serialBufferLen = 0;
105+
}
106+
107+
// print received data.
108+
while (int packetSize = Udp.parsePacket()) {
109+
Serial.print("Received packet from [");
110+
Serial.print(Udp.remoteIP());
111+
Serial.print("]:");
112+
Serial.print(Udp.remotePort());
113+
Serial.print(" datasize=");
114+
Serial.println(packetSize);
115+
116+
// Read the packet into packetBufffer
117+
memset(packetBuffer, 0, MAX_PAYLOAD_LEN);
118+
Udp.read(packetBuffer, MAX_PAYLOAD_LEN);
119+
120+
if (destAddr == Udp.remoteIP()) {
121+
// Not response to echo.
122+
Serial.print("Received: ");
123+
Serial.println(packetBuffer);
124+
}
125+
else {
126+
// Send echo
127+
Serial.print("Echo: ");
128+
Serial.println(packetBuffer);
129+
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
130+
Udp.write(packetBuffer, packetSize);
131+
Udp.endPacket();
132+
}
133+
}
134+
135+
}
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,107 @@
1+
/*
2+
UDP Echo client
3+
4+
A simple UDP communication demo.
5+
The program send simple message to 'udp-echo-server-rpl' node,
6+
the server node send echo on the message received.
7+
The received echo message shows on serial port.
8+
9+
Even if RPL is enabled or disabled works.
10+
Use same settings to compile server side programs.
11+
This programs behavior is based on contiki os's
12+
example(examples/udp-ipv6/udp-client.c).
13+
14+
created 24 Jan 2016
15+
modified 29 May 2018
16+
by Tokita Hiroshi
17+
*/
18+
119
#include <MicroIp.h>
220
#include <MicroIpUdp.h>
21+
#include <IPAddress.h>
322

4-
IPAddress server(0xaaaa, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0001);
5-
6-
unsigned int localPort = 7;
7-
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
23+
const size_t MAX_PAYLOAD_LEN = 40;
24+
const char* UDP_CONNECTION_ADDR = "contiki-udp-server.local";
25+
const unsigned short LOCAL_PORT = 3001;
26+
const unsigned short DEST_PORT = 3000;
27+
const int INTERVAL = 15;
828

929
MicroIPUDP Udp;
30+
IPAddress server;
31+
32+
char packetBuffer[MAX_PAYLOAD_LEN];
33+
long lastsend;
34+
long seq_id;
1035

1136
void setup() {
12-
Serial.begin(115200);
37+
Serial.begin(1000000);
38+
Serial.println("Start udp-echo-cleint");
1339
MicroIP.begin();
1440

15-
server = MicroIP.lookup("udp-echo-server.local");
16-
Udp.begin(localPort);
41+
server = MicroIP.lookup(UDP_CONNECTION_ADDR);
42+
43+
if (server == IN6ADDR_ANY_INIT) {
44+
Serial.print("Server [");
45+
Serial.print(UDP_CONNECTION_ADDR);
46+
Serial.println("] is not found.");
47+
while (true) {
48+
yield();
49+
}
50+
}
51+
52+
Serial.print("Server ");
53+
Serial.print(UDP_CONNECTION_ADDR);
54+
Serial.print(" is [");
55+
Serial.print(server);
56+
Serial.println("]");
57+
58+
Udp.begin(LOCAL_PORT);
59+
Serial.print("Start listen port:");
60+
Serial.println(LOCAL_PORT);
1761
}
1862

1963
void loop() {
2064

21-
int avail = Serial.available();
22-
if(avail) {
23-
int len = min(avail, UDP_TX_PACKET_MAX_SIZE);
65+
long now = millis();
2466

25-
for(int i=0; i<len; i++) {
26-
packetBuffer[i] = Serial.read();
27-
}
67+
// Periodically send.
68+
if ((now - lastsend) > (INTERVAL * 1000)) {
69+
// format message
70+
memset(packetBuffer, 0, MAX_PAYLOAD_LEN);
71+
strcpy(packetBuffer, "Hello ");
72+
itoa(++seq_id, packetBuffer + strlen(packetBuffer), 10);
73+
strcpy(packetBuffer + strlen(packetBuffer), " from the client");
2874

29-
Udp.beginPacket(server, localPort);
30-
Udp.write(packetBuffer, len);
75+
Serial.print("Client sending to ");
76+
Serial.print(server);
77+
Serial.print(" (msg: ");
78+
Serial.print(packetBuffer);
79+
Serial.println(");");
80+
81+
// send packet
82+
Udp.beginPacket(server, DEST_PORT);
83+
Udp.write(packetBuffer, strlen(packetBuffer));
3184
Udp.endPacket();
85+
86+
lastsend = now;
3287
}
3388

34-
while(int packetSize = Udp.parsePacket()) {
35-
Serial.print("Received packet of size ");
36-
Serial.println(packetSize);
37-
Serial.print("From ");
38-
IPAddress remote = Udp.remoteIP();
39-
Serial.print(remote);
40-
Serial.print(", port ");
41-
Serial.println(Udp.remotePort());
89+
while (int packetSize = Udp.parsePacket()) {
90+
/*
91+
//more info
92+
Serial.print("Receive from ");
93+
Serial.print(Udp.remoteIP());
94+
Serial.print(":");
95+
Serial.print(Udp.remotePort());
96+
Serial.print(" size:");
97+
Serial.println(packetSize);
98+
*/
4299

43100
// read the packet into packetBufffer
44-
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
45-
Serial.println("Contents:");
46-
for(int i=0; i<packetSize; i++) {
47-
Serial.print(packetBuffer[i]);
48-
}
49-
Serial.println();
101+
Udp.read(packetBuffer, packetSize);
102+
Serial.print("Response from the server: '");
103+
Serial.print(packetBuffer);
104+
Serial.println("'");
50105
}
51106

52107
}

0 commit comments

Comments
 (0)