Skip to content

Instantly share code, notes, and snippets.

@cansik
Created June 23, 2020 16:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cansik/2f135a04d833f4be940f3cd385d14c75 to your computer and use it in GitHub Desktop.
Save cansik/2f135a04d833f4be940f3cd385d14c75 to your computer and use it in GitHub Desktop.
WiFi NINA / NINA-FW Concurrency Bug Example
#include <SPI.h>
#include <WiFiNINA.h>
#include <WiFiUdp.h>
#define IN_PORT 8000
#define OUT_PORT 9000
// wifi
char ssid[] = "";
char pass[] = "";
int keyIndex = 0;
int status = WL_IDLE_STATUS;
// udp
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
char route1[] = "/hello/dog"; // 10 chars
char route2[] = "/goodbye/horse"; // 14 chars
bool received = false;
WiFiUDP Udp;
void setup() {
Serial.begin(9600);
// connect wifi
while (status != WL_CONNECTED) {
Serial.print("Connecting to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, keyIndex, pass);
}
// setup UDP
Udp.begin(IN_PORT);
Serial.println("everything setup to test!");
}
void loop() {
int packetSize = Udp.parsePacket();
if (packetSize)
{
Udp.read(packetBuffer, packetSize);
received = false;
if (startsWith(route1, packetBuffer, 10)) {
Serial.print("1");
received = true;
}
if (startsWith(route2, packetBuffer, 14)) {
Serial.print("2");
received = true;
}
Serial.print(" received [");
Serial.print(packetSize);
Serial.println("]");
// something went wrong
// show buffer
if (!received) {
Serial.print("BUFFER: ");
for (int i = 0; i < UDP_TX_PACKET_MAX_SIZE; i++) {
Serial.print(packetBuffer[i], HEX);
Serial.print(" ");
}
Serial.println();
Serial.print("BUFFER: ");
for (int i = 0; i < UDP_TX_PACKET_MAX_SIZE; i++) {
Serial.print(packetBuffer[i]);
Serial.print(" ");
}
Serial.println();
}
}
}
bool startsWith(char reference[], char msg[], int size) {
for (auto i = 0; i < size; i++) {
if (reference[i] != msg[i])
return false;
}
return true;
}
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.net.InetAddress;
DatagramSocket outSocket;
InetAddress ip;
void setup() {
size(500, 500);
frameRate(30);
try {
outSocket = new DatagramSocket();
ip = InetAddress.getByName("192.168.1.34");
}
catch(Exception ex) {
println(ex);
}
}
void draw() {
background(55);
send("/hello/dog");
send("/goodbye/horse");
}
void send(String msg) {
try {
byte[] data = msg.getBytes();
DatagramPacket myPacket = new DatagramPacket(data, data.length, ip, 8000);
outSocket.send(myPacket);
}
catch(Exception ex) {
println("Socket: " + ex.getMessage());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment