Basic Java CoAP subscriber to C Contiki nodes

This commit is contained in:
giomba 2019-11-07 22:34:13 +01:00
parent 0eef228ca2
commit b781d22602
14 changed files with 522 additions and 0 deletions

133
oracle/.gitignore vendored Normal file
View File

@ -0,0 +1,133 @@
*.a
*.bin
*.co
*.map
*.png
*.log
*.elf
*.zip
*.d
*.ihex
*.pyc
*.redbee-econotag
*.econotag
*.native
*.z1
*.minimal-net
*.sky
*.wismote
*.esb
*.avr-raven
*.exp5438
*.mbxxx
*.win32
*.apple2enh
*.atarixl
*.c128
*.c64
*.cc2538dk
*.zoul
*.jn516x
*.srf06-cc26xx
*.ev-aducrf101mkxz
*.report
summary
*.summary
*.runerr
*.runlog
*.faillog
*.orig
*~
.DS_Store
obj_*
symbols.*
Makefile.target
doc/html
doc/latex
patches-*
tools/tunslip
tools/tunslip6
build
tools/coffee-manager/build/
tools/cooja/dist/
tools/collect-view/build/
tools/collect-view/dist/
COOJA.testlog
tools/cooja/apps/mrm/lib/
tools/cooja/apps/mspsim/lib/
tools/cooja/apps/powertracker/lib/
tools/cooja/apps/serial_socket/lib/
tools/coffee-manager/coffee.jar
tools/cooja/apps/avrora/lib/cooja_avrora.jar
tools/cooja/apps/collect-view/cooja-collect-view.jar
# sdcc build artifacts
contiki-cc2530dk.lib
*.ihx
*.hex
*.mem
*.lk
*.omf
*.cdb
*.banks
*.cc2530dk
# VC++ build artifacts
*.exp
*.ilk
*.lib
*.pdb
*.prg
*.dsc
#cc65 build artifacts
*.s
*.eth
*.dsk
*.po
*.atr
*.d64
*.d71
*.d81
# Cooja Build Artifacts
*.cooja
#regression tests artifacts
*.testlog
*.log.prog
regression-tests/[0-9][0-9]-*/report
regression-tests/[0-9][0-9]-*/org/
# rl78 build artifacts
*.eval-adf7xxxmb4z
*.eval-adf7xxxmb4z.srec
# cscope files
cscope.*
# vim swap files
*.swp
*.swo
# x86 UEFI files
cpu/x86/uefi/Makefile.uefi
cpu/x86/uefi/edk2
# galileo bsp files
platform/galileo/bsp/libc/Makefile.libc
platform/galileo/bsp/libc/i586-elf/
platform/galileo/bsp/libc/newlib-2.2.0-1*
platform/galileo/bsp/grub/src/
platform/galileo/bsp/grub/bin/
# galileo build and debug artefacts
*.galileo
*.galileo.dll
*.galileo.efi
LOG_OPENOCD
# nRF52 build artifacts
*.jlink
*.nrf52dk

11
oracle/.project Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>11.coap-observable</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
</natures>
</projectDescription>

16
oracle/Makefile Normal file
View File

@ -0,0 +1,16 @@
CONTIKI_PROJECT = main
SMALL=1
CFLAGS += -DUIP_CONF_IPV6=1 -DRPL_CONF_STATS=1 -DUIP_CONF_IPV6_RPL=1 -DUIP_CONF_TCP=0 -DPROJECT_CONF_H=\"project-conf.h\"
CONTIKI = /home/giomba/workspace/uni/contiki
WITH_UIP6=1
UIP_CONF_IPV6=1
UIP_CONF_IPV6_RPL=1
APPS += er-coap rest-engine
all: $(CONTIKI_PROJECT)
include $(CONTIKI)/Makefile.include

63
oracle/main.c Normal file
View File

@ -0,0 +1,63 @@
#include <stdio.h>
#include "contiki.h"
#include "contiki-net.h"
#include "rest-engine.h"
#include "net/ip/uip-debug.h"
int precious_resource = 0;
/* handlers for coap */
void res_get_handler(void* request, void* response, uint8_t* buffer, uint16_t preferred_size, int32_t* offset) {
static char payload[16];
sprintf(payload, "pr = %d", precious_resource);
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
REST.set_response_payload(response, payload, strlen(payload));
}
void res_periodic_handler();
/* declare resource and set handlers */
PERIODIC_RESOURCE(myresource, "title=\"An observable resource\";rt=\"some descriptive text\";obs", res_get_handler, NULL, NULL, NULL, 10 * CLOCK_SECOND, res_periodic_handler);
void res_periodic_handler() {
/* sample some sensor */
/* byte mysample = *(some_bar + some_offset); */
++precious_resource;
/* Notify subscribers of the new sample */
REST.notify_subscribers(&myresource);
}
/* contiki protothread, as usual... */
PROCESS(main_process_name, "Simple-CoAP-Server");
AUTOSTART_PROCESSES(&main_process_name);
PROCESS_THREAD(main_process_name, ev, data) {
PROCESS_BEGIN();
printf("Started....\n");
/* print enabled addresses */
printf("My addresses:\n");
int i;
for (i = 0; i < UIP_DS6_ADDR_NB; ++i) {
if (uip_ds6_if.addr_list[i].isused) {
uip_debug_ipaddr_print(&uip_ds6_if.addr_list[i].ipaddr);
printf("\n");
}
}
/* enable CoAP engine */
rest_init_engine();
rest_activate_resource(&myresource, "test/myresource");
while (1) {
PROCESS_WAIT_EVENT();
}
PROCESS_END();
}

16
oracle/project-conf.h Normal file
View File

@ -0,0 +1,16 @@
/* reduce RAM consumption */
#ifndef PROJECT_CONF_H_
#define PROJECT_CONF_H_
#undef REST_MAX_CHUNK_SIZE // Set the max response payload before fragmentation
#define REST_MAX_CHUNK_SIZE 64
#undef COAP_MAX_OPEN_TRANSACTIONS // Set the max number of concurrent transactions
#define COAP_MAX_OPEN_TRANSACTIONS 4
#undef NBR_TABLE_CONF_MAX_NEIGHBORS // Set the max number of entries in neighbors table
#define NBR_TABLE_CONF_MAX_NEIGHBORS 10
#undef UIP_CONF_MAX_ROUTES // Set the max number of routes handled by the node
#define UIP_CONF_MAX_ROUTES 10
#undef UIP_CONF_BUFFER_SIZE // Set the amount of memory reserved to the uIP packet buffer
#define UIP_CONF_BUFFER_SIZE 280
#endif

20
proxy/.classpath Normal file
View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>

2
proxy/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/bin/
/target/

23
proxy/.project Normal file
View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>proxy</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>

View File

@ -0,0 +1,8 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.8

View File

@ -0,0 +1,59 @@
#Californium CoAP Properties file
#Thu Nov 07 17:22:31 CET 2019
HEALTH_STATUS_INTERVAL=0
ACK_TIMEOUT=2000
UDP_CONNECTOR_SEND_BUFFER=0
ACK_TIMEOUT_SCALE=2.0
HTTP_SERVER_SOCKET_TIMEOUT=100000
USE_RANDOM_MID_START=true
BLOCKWISE_STRICT_BLOCK2_OPTION=false
MAX_ACTIVE_PEERS=150000
PROTOCOL_STAGE_THREAD_COUNT=8
BLOCKWISE_STATUS_LIFETIME=300000
MAX_RESOURCE_BODY_SIZE=8192
HTTP_CACHE_SIZE=32
UDP_CONNECTOR_DATAGRAM_SIZE=2048
UDP_CONNECTOR_RECEIVE_BUFFER=0
DTLS_CONNECTION_ID_NODE_ID=
MAX_TRANSMIT_WAIT=93000
NOTIFICATION_REREGISTRATION_BACKOFF=2000
DEDUPLICATOR=DEDUPLICATOR_MARK_AND_SWEEP
COAP_PORT=5683
MID_TACKER=GROUPED
COAP_SECURE_PORT=5684
NETWORK_STAGE_RECEIVER_THREAD_COUNT=1
HTTP_CACHE_RESPONSE_MAX_AGE=86400
MULTICAST_BASE_MID=65000
HTTP_SERVER_SOCKET_BUFFER_SIZE=8192
EXCHANGE_LIFETIME=247000
TLS_HANDSHAKE_TIMEOUT=10000
DEDUPLICATOR_AUTO_REPLACE=true
TCP_CONNECTION_IDLE_TIMEOUT=10
LEISURE=5000
HTTP_PORT=8080
DTLS_CONNECTION_ID_LENGTH=
NOTIFICATION_CHECK_INTERVAL=86400000
CONGESTION_CONTROL_ALGORITHM=Cocoa
RESPONSE_MATCHING=STRICT
MID_TRACKER_GROUPS=16
TOKEN_SIZE_LIMIT=8
NETWORK_STAGE_SENDER_THREAD_COUNT=1
TCP_WORKER_THREADS=1
SECURE_SESSION_TIMEOUT=86400
TCP_CONNECT_TIMEOUT=10000
MAX_RETRANSMIT=4
MAX_MESSAGE_SIZE=1024
ACK_RANDOM_FACTOR=1.5
NSTART=1
MAX_LATENCY=100000
PROBING_RATE=1.0
USE_CONGESTION_CONTROL=false
MAX_SERVER_RESPONSE_DELAY=250000
CROP_ROTATION_PERIOD=247000
MAX_PEER_INACTIVITY_PERIOD=600
UDP_CONNECTOR_OUT_CAPACITY=2147483647
DTLS_AUTO_RESUME_TIMEOUT=30000
PREFERRED_BLOCK_SIZE=512
NON_LIFETIME=145000
NOTIFICATION_CHECK_INTERVAL_COUNT=100
MARK_AND_SWEEP_INTERVAL=10000

74
proxy/pom.xml Normal file
View File

@ -0,0 +1,74 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>it.unipi.ing.ce.netpp</groupId>
<artifactId>proxy</artifactId>
<version>2019.11.0</version>
<dependencies>
<dependency>
<groupId>org.eclipse.californium</groupId>
<artifactId>californium-core</artifactId>
<version>2.0.0-M18</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
</configuration>
</plugin>
<!-- Set a compiler level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- Maven Assembly Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>netpp.Main</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,26 @@
package netpp;
import java.net.URI;
import org.eclipse.californium.core.*;
public class Handler implements CoapHandler {
private URI uri;
public Handler(URI uri) {
this.uri = uri;
}
@Override
public void onLoad(CoapResponse response) {
String content = response.getResponseText();
System.out.println("[D] resource: " + uri);
System.out.println(" new value: " + content);
}
@Override
public void onError() {
System.err.println("[E] failed " + uri);
}
}

40
proxy/src/netpp/Main.java Normal file
View File

@ -0,0 +1,40 @@
package netpp;
public class Main {
public final static String COOJA_MAGIC = "c30c";
public final static String resource = "/test/myresource";
public static void main(String[] args) {
if (args.length < 2) {
System.err.println("[F] Bad parameters");
System.err.println("Usage: ");
System.err.println("$ exe <prefix> <n>");
System.err.println(" prefix IPv6 /64 prefix (mind the format!)");
System.err.println(" n number of nodes");
System.err.println();
System.err.println("Example:");
System.err.println("$ exe 2001:db8:0:0 10");
System.exit(1);
}
final String prefix = args[0];
final int n = Integer.parseInt(args[1]);
System.err.println("[I] now subscribing to node [2; " + n + "] in " + prefix + "/64");
// TODO pay attention to this for-cycle and mind the hexadecimal values!
/* subscribe to every node */
for (int i = 2; i <= n; i++) { // node 1 is the gateway, skip. And mind the <=
String uri_string = "coap://[" + prefix + ":" + COOJA_MAGIC + "::" + Integer.toString(i) + "]" + resource;
System.err.println("[I] subscribing to " + uri_string);
Node node = new Node(uri_string);
}
try {
Thread.sleep(5 * 60 * 1000); // 5 min
} catch (InterruptedException ex) {
System.err.println(ex);
}
}
}

31
proxy/src/netpp/Node.java Normal file
View File

@ -0,0 +1,31 @@
package netpp;
import java.net.URI;
import java.net.URISyntaxException;
import org.eclipse.californium.core.*;
public class Node {
private URI uri;
private CoapClient client;
private CoapObserveRelation relation;
public Node(String uriString) {
try {
this.uri = new URI(uriString);
this.client = new CoapClient(uri);
this.relation = client.observe(new Handler(this.uri));
} catch (URISyntaxException e) {
System.err.println("[F] Invalid URI: " + e.getMessage());
System.exit(1);
}
}
/* be kind with the poor resource constrained node */
@Override
public void finalize() {
this.relation.proactiveCancel();
}
}