From b781d22602db80e75ff01a173feeb36bfb8651cc Mon Sep 17 00:00:00 2001 From: giomba Date: Thu, 7 Nov 2019 22:34:13 +0100 Subject: [PATCH] Basic Java CoAP subscriber to C Contiki nodes --- oracle/.gitignore | 133 +++++++++++++++++++++ oracle/.project | 11 ++ oracle/Makefile | 16 +++ oracle/main.c | 63 ++++++++++ oracle/project-conf.h | 16 +++ proxy/.classpath | 20 ++++ proxy/.gitignore | 2 + proxy/.project | 23 ++++ proxy/.settings/org.eclipse.jdt.core.prefs | 8 ++ proxy/Californium.properties | 59 +++++++++ proxy/pom.xml | 74 ++++++++++++ proxy/src/netpp/Handler.java | 26 ++++ proxy/src/netpp/Main.java | 40 +++++++ proxy/src/netpp/Node.java | 31 +++++ 14 files changed, 522 insertions(+) create mode 100644 oracle/.gitignore create mode 100644 oracle/.project create mode 100644 oracle/Makefile create mode 100644 oracle/main.c create mode 100644 oracle/project-conf.h create mode 100644 proxy/.classpath create mode 100644 proxy/.gitignore create mode 100644 proxy/.project create mode 100644 proxy/.settings/org.eclipse.jdt.core.prefs create mode 100644 proxy/Californium.properties create mode 100644 proxy/pom.xml create mode 100644 proxy/src/netpp/Handler.java create mode 100644 proxy/src/netpp/Main.java create mode 100644 proxy/src/netpp/Node.java diff --git a/oracle/.gitignore b/oracle/.gitignore new file mode 100644 index 0000000..b4a764e --- /dev/null +++ b/oracle/.gitignore @@ -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 + diff --git a/oracle/.project b/oracle/.project new file mode 100644 index 0000000..7d8f6e6 --- /dev/null +++ b/oracle/.project @@ -0,0 +1,11 @@ + + + 11.coap-observable + + + + + + + + diff --git a/oracle/Makefile b/oracle/Makefile new file mode 100644 index 0000000..03a2aea --- /dev/null +++ b/oracle/Makefile @@ -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 + diff --git a/oracle/main.c b/oracle/main.c new file mode 100644 index 0000000..a1bbd66 --- /dev/null +++ b/oracle/main.c @@ -0,0 +1,63 @@ +#include + +#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(); + +} + diff --git a/oracle/project-conf.h b/oracle/project-conf.h new file mode 100644 index 0000000..4a0dbb2 --- /dev/null +++ b/oracle/project-conf.h @@ -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 + diff --git a/proxy/.classpath b/proxy/.classpath new file mode 100644 index 0000000..149cb3c --- /dev/null +++ b/proxy/.classpath @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/proxy/.gitignore b/proxy/.gitignore new file mode 100644 index 0000000..09e3bc9 --- /dev/null +++ b/proxy/.gitignore @@ -0,0 +1,2 @@ +/bin/ +/target/ diff --git a/proxy/.project b/proxy/.project new file mode 100644 index 0000000..7a1555a --- /dev/null +++ b/proxy/.project @@ -0,0 +1,23 @@ + + + proxy + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.m2e.core.maven2Nature + + diff --git a/proxy/.settings/org.eclipse.jdt.core.prefs b/proxy/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..2f5cc74 --- /dev/null +++ b/proxy/.settings/org.eclipse.jdt.core.prefs @@ -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 diff --git a/proxy/Californium.properties b/proxy/Californium.properties new file mode 100644 index 0000000..39c03fe --- /dev/null +++ b/proxy/Californium.properties @@ -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 diff --git a/proxy/pom.xml b/proxy/pom.xml new file mode 100644 index 0000000..55d1473 --- /dev/null +++ b/proxy/pom.xml @@ -0,0 +1,74 @@ + + 4.0.0 + it.unipi.ing.ce.netpp + proxy + 2019.11.0 + + + + + org.eclipse.californium + californium-core + 2.0.0-M18 + + + + + + src + + + + org.apache.maven.plugins + maven-eclipse-plugin + 2.9 + + true + false + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.0 + + 1.8 + 1.8 + + + + + + org.apache.maven.plugins + maven-assembly-plugin + 2.4.1 + + + + jar-with-dependencies + + + + + netpp.Main + + + + + + make-assembly + + package + + single + + + + + + + + + diff --git a/proxy/src/netpp/Handler.java b/proxy/src/netpp/Handler.java new file mode 100644 index 0000000..d36a863 --- /dev/null +++ b/proxy/src/netpp/Handler.java @@ -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); + } + +} diff --git a/proxy/src/netpp/Main.java b/proxy/src/netpp/Main.java new file mode 100644 index 0000000..64fb6bb --- /dev/null +++ b/proxy/src/netpp/Main.java @@ -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 "); + 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); + } + + } +} diff --git a/proxy/src/netpp/Node.java b/proxy/src/netpp/Node.java new file mode 100644 index 0000000..ab37844 --- /dev/null +++ b/proxy/src/netpp/Node.java @@ -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(); + } + +}