Implementation of OMA LWM2M Engine / IPSO Objects

This commit is contained in:
Niclas Finne 2015-12-01 04:14:46 +01:00
parent 35fb0614de
commit 3dd11603a7
33 changed files with 5304 additions and 0 deletions

View file

@ -0,0 +1,29 @@
CONTIKI_PROJECT = example-ipso-objects
CONTIKI_SOURCEFILES += serial-protocol.c example-ipso-temperature.c
all: $(CONTIKI_PROJECT)
CFLAGS += -DPROJECT_CONF_H=\"project-conf.h\"
APPS += rest-engine
APPS += er-coap
APPS += oma-lwm2m
APPS += ipso-objects
CONTIKI=../..
CONTIKI_WITH_IPV6 = 1
include $(CONTIKI)/Makefile.include
# border router rules
$(CONTIKI)/tools/tunslip6: $(CONTIKI)/tools/tunslip6.c
(cd $(CONTIKI)/tools && $(MAKE) tunslip6)
connect-router: $(CONTIKI)/tools/tunslip6
sudo $(CONTIKI)/tools/tunslip6 aaaa::1/64
connect-router-cooja: $(CONTIKI)/tools/tunslip6
sudo $(CONTIKI)/tools/tunslip6 -a 127.0.0.1 -p 60001 aaaa::1/64
connect-router-native: $(CONTIKI)/examples/ipv6/native-border-router/border-router.native
sudo $(CONTIKI)/exmples/ipv6/native-border-router/border-router.native -a 127.0.0.1 -p 60001 aaaa::1/64

View file

@ -0,0 +1,171 @@
<?xml version="1.0" encoding="UTF-8"?>
<simconf>
<project EXPORT="discard">[APPS_DIR]/mrm</project>
<project EXPORT="discard">[APPS_DIR]/mspsim</project>
<project EXPORT="discard">[APPS_DIR]/avrora</project>
<project EXPORT="discard">[APPS_DIR]/serial_socket</project>
<project EXPORT="discard">[APPS_DIR]/collect-view</project>
<project EXPORT="discard">[APPS_DIR]/powertracker</project>
<simulation>
<title>LWM2M &amp; IPSO Objects Example</title>
<speedlimit>1.0</speedlimit>
<randomseed>123456</randomseed>
<motedelay_us>1000000</motedelay_us>
<radiomedium>
org.contikios.cooja.radiomediums.UDGM
<transmitting_range>50.0</transmitting_range>
<interference_range>100.0</interference_range>
<success_ratio_tx>1.0</success_ratio_tx>
<success_ratio_rx>1.0</success_ratio_rx>
</radiomedium>
<events>
<logoutput>40000</logoutput>
</events>
<motetype>
org.contikios.cooja.mspmote.WismoteMoteType
<identifier>wismote1</identifier>
<description>Wismote Border Router #border-router</description>
<source EXPORT="discard">[CONTIKI_DIR]/examples/ipv6/rpl-border-router/border-router.c</source>
<commands EXPORT="discard">make border-router.wismote TARGET=wismote DEFINES=NETSTACK_RDC=nullrdc_driver,NETSTACK_MAC=nullmac_driver</commands>
<firmware EXPORT="copy">[CONTIKI_DIR]/examples/ipv6/rpl-border-router/border-router.wismote</firmware>
<moteinterface>org.contikios.cooja.interfaces.Position</moteinterface>
<moteinterface>org.contikios.cooja.interfaces.RimeAddress</moteinterface>
<moteinterface>org.contikios.cooja.interfaces.IPAddress</moteinterface>
<moteinterface>org.contikios.cooja.interfaces.Mote2MoteRelations</moteinterface>
<moteinterface>org.contikios.cooja.interfaces.MoteAttributes</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspClock</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspMoteID</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspButton</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.Msp802154Radio</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspDefaultSerial</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspLED</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspDebugOutput</moteinterface>
</motetype>
<motetype>
org.contikios.cooja.mspmote.WismoteMoteType
<identifier>wismote2</identifier>
<description>Wismote IPSO Objects #ipso-example</description>
<source EXPORT="discard">[CONTIKI_DIR]/examples/ipso-objects/example-ipso-objects.c</source>
<commands EXPORT="discard">make example-ipso-objects.wismote TARGET=wismote</commands>
<firmware EXPORT="copy">[CONTIKI_DIR]/examples/ipso-objects/example-ipso-objects.wismote</firmware>
<moteinterface>org.contikios.cooja.interfaces.Position</moteinterface>
<moteinterface>org.contikios.cooja.interfaces.RimeAddress</moteinterface>
<moteinterface>org.contikios.cooja.interfaces.IPAddress</moteinterface>
<moteinterface>org.contikios.cooja.interfaces.Mote2MoteRelations</moteinterface>
<moteinterface>org.contikios.cooja.interfaces.MoteAttributes</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspClock</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspMoteID</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspButton</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.Msp802154Radio</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspDefaultSerial</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspLED</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspDebugOutput</moteinterface>
</motetype>
<mote>
<breakpoints />
<interface_config>
org.contikios.cooja.interfaces.Position
<x>56.362361976162035</x>
<y>11.826023799100883</y>
<z>0.0</z>
</interface_config>
<interface_config>
org.contikios.cooja.mspmote.interfaces.MspClock
<deviation>1.0</deviation>
</interface_config>
<interface_config>
org.contikios.cooja.mspmote.interfaces.MspMoteID
<id>1</id>
</interface_config>
<motetype_identifier>wismote1</motetype_identifier>
</mote>
<mote>
<breakpoints />
<interface_config>
org.contikios.cooja.interfaces.Position
<x>60.1539674439426</x>
<y>11.827942168467365</y>
<z>0.0</z>
</interface_config>
<interface_config>
org.contikios.cooja.mspmote.interfaces.MspClock
<deviation>1.0</deviation>
</interface_config>
<interface_config>
org.contikios.cooja.mspmote.interfaces.MspMoteID
<id>2</id>
</interface_config>
<motetype_identifier>wismote2</motetype_identifier>
</mote>
</simulation>
<plugin>
org.contikios.cooja.plugins.SimControl
<width>280</width>
<z>2</z>
<height>160</height>
<location_x>400</location_x>
<location_y>0</location_y>
</plugin>
<plugin>
org.contikios.cooja.plugins.Visualizer
<plugin_config>
<moterelations>true</moterelations>
<skin>org.contikios.cooja.plugins.skins.IDVisualizerSkin</skin>
<skin>org.contikios.cooja.plugins.skins.TrafficVisualizerSkin</skin>
<skin>org.contikios.cooja.plugins.skins.UDGMVisualizerSkin</skin>
<skin>org.contikios.cooja.plugins.skins.LEDVisualizerSkin</skin>
<skin>org.contikios.cooja.plugins.skins.MoteTypeVisualizerSkin</skin>
<viewport>53.336918739504526 0.0 0.0 53.336918739504526 -2924.9161170527295 -473.3614543395965</viewport>
</plugin_config>
<width>400</width>
<z>4</z>
<height>400</height>
<location_x>1</location_x>
<location_y>1</location_y>
</plugin>
<plugin>
org.contikios.cooja.plugins.LogListener
<plugin_config>
<filter>ID:2</filter>
<formatted_time />
<coloring />
</plugin_config>
<width>1286</width>
<z>1</z>
<height>240</height>
<location_x>400</location_x>
<location_y>160</location_y>
</plugin>
<plugin>
org.contikios.cooja.plugins.Notes
<plugin_config>
<notes>OMA LWM2M &amp; IPSO Object example
1. Start a LWM2M server, for example Leshan
2. Run the example and bridge Cooja using tunslip with the prefix aaaa::1/64:
(cd contiki/examples/ipso-objects &amp;&amp; make connect-router-cooja)
After a short time, the example node should register with the LWM2M server at [aaaa::1]:5683.</notes>
<decorations>true</decorations>
</plugin_config>
<width>1006</width>
<z>0</z>
<height>160</height>
<location_x>680</location_x>
<location_y>0</location_y>
</plugin>
<plugin>
org.contikios.cooja.serialsocket.SerialSocketServer
<mote_arg>0</mote_arg>
<plugin_config>
<port>60001</port>
<bound>true</bound>
</plugin_config>
<width>362</width>
<z>3</z>
<height>116</height>
<location_x>1</location_x>
<location_y>399</location_y>
</plugin>
</simconf>

View file

@ -0,0 +1,187 @@
<?xml version="1.0" encoding="UTF-8"?>
<simconf>
<project EXPORT="discard">[APPS_DIR]/mrm</project>
<project EXPORT="discard">[APPS_DIR]/mspsim</project>
<project EXPORT="discard">[APPS_DIR]/avrora</project>
<project EXPORT="discard">[APPS_DIR]/serial_socket</project>
<project EXPORT="discard">[APPS_DIR]/collect-view</project>
<project EXPORT="discard">[APPS_DIR]/powertracker</project>
<simulation>
<title>OMA LWM2M and IPSO Object example</title>
<speedlimit>2.0</speedlimit>
<randomseed>123456</randomseed>
<motedelay_us>1000000</motedelay_us>
<radiomedium>
org.contikios.cooja.radiomediums.UDGM
<transmitting_range>500.0</transmitting_range>
<interference_range>500.0</interference_range>
<success_ratio_tx>1.0</success_ratio_tx>
<success_ratio_rx>1.0</success_ratio_rx>
</radiomedium>
<events>
<logoutput>40000</logoutput>
</events>
<motetype>
org.contikios.cooja.mspmote.WismoteMoteType
<identifier>wismote1</identifier>
<description>Wismote Router #wismote1</description>
<source EXPORT="discard">[CONTIKI_DIR]/examples/ipso-objects/example-server.c</source>
<commands EXPORT="discard">make example-server.wismote TARGET=wismote</commands>
<firmware EXPORT="copy">[CONTIKI_DIR]/examples/ipso-objects/example-server.wismote</firmware>
<moteinterface>org.contikios.cooja.interfaces.Position</moteinterface>
<moteinterface>org.contikios.cooja.interfaces.RimeAddress</moteinterface>
<moteinterface>org.contikios.cooja.interfaces.IPAddress</moteinterface>
<moteinterface>org.contikios.cooja.interfaces.Mote2MoteRelations</moteinterface>
<moteinterface>org.contikios.cooja.interfaces.MoteAttributes</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspClock</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspMoteID</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspButton</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.Msp802154Radio</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspDefaultSerial</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspLED</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspDebugOutput</moteinterface>
</motetype>
<motetype>
org.contikios.cooja.mspmote.WismoteMoteType
<identifier>wismote2</identifier>
<description>Wismote Mote Type #wismote2</description>
<source EXPORT="discard">[CONTIKI_DIR]/examples/ipso-objects/example-ipso-objects.c</source>
<commands EXPORT="discard">make example-ipso-objects.wismote TARGET=wismote</commands>
<firmware EXPORT="copy">[CONTIKI_DIR]/examples/ipso-objects/example-ipso-objects.wismote</firmware>
<moteinterface>org.contikios.cooja.interfaces.Position</moteinterface>
<moteinterface>org.contikios.cooja.interfaces.RimeAddress</moteinterface>
<moteinterface>org.contikios.cooja.interfaces.IPAddress</moteinterface>
<moteinterface>org.contikios.cooja.interfaces.Mote2MoteRelations</moteinterface>
<moteinterface>org.contikios.cooja.interfaces.MoteAttributes</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspClock</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspMoteID</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspButton</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.Msp802154Radio</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspDefaultSerial</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspLED</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspDebugOutput</moteinterface>
</motetype>
<mote>
<breakpoints />
<interface_config>
org.contikios.cooja.interfaces.Position
<x>30.243188653185154</x>
<y>29.963547412144486</y>
<z>0.0</z>
</interface_config>
<interface_config>
org.contikios.cooja.mspmote.interfaces.MspClock
<deviation>1.0</deviation>
</interface_config>
<interface_config>
org.contikios.cooja.mspmote.interfaces.MspMoteID
<id>1</id>
</interface_config>
<interface_config>
org.contikios.cooja.mspmote.interfaces.MspDefaultSerial
<history>s aaaa::200:0:0:3 /3311/0/5850 0~;s aaaa::200:0:0:3 /3311/0/5850 1~;g aaaa::200:0:0:3 /3311/1/5850~;g aaaa::200:0:0:3 /3311/0/5850~;g aaaa::200:0:0:3 /3311/1/5850~;g aaaa::200:0:0:3 /3311/0/5850~;s aaaa::200:0:0:3 /3311/0/5850 1~;s aaaa::200:0:0:2 /3311/0/5850 1~;h~;s aaaa::200:0:0:2 /3311/0/5850 1~;s aaaa::200:0:0:2 /3311/0/5850 0~;g aaaa::200:0:0:2 /3311/0/5850~;g aaaa::200:0:0:2 /3311/1/5850~;g aaaa::200:0:0:2 /3311/2/5850~;l~;</history>
</interface_config>
<motetype_identifier>wismote1</motetype_identifier>
</mote>
<mote>
<breakpoints />
<interface_config>
org.contikios.cooja.interfaces.Position
<x>59.75123136831088</x>
<y>29.84506209179908</y>
<z>0.0</z>
</interface_config>
<interface_config>
org.contikios.cooja.mspmote.interfaces.MspClock
<deviation>1.0</deviation>
</interface_config>
<interface_config>
org.contikios.cooja.mspmote.interfaces.MspMoteID
<id>2</id>
</interface_config>
<motetype_identifier>wismote2</motetype_identifier>
</mote>
<mote>
<breakpoints />
<interface_config>
org.contikios.cooja.interfaces.Position
<x>60.30742753391745</x>
<y>59.35092511889063</y>
<z>0.0</z>
</interface_config>
<interface_config>
org.contikios.cooja.mspmote.interfaces.MspClock
<deviation>1.0</deviation>
</interface_config>
<interface_config>
org.contikios.cooja.mspmote.interfaces.MspMoteID
<id>3</id>
</interface_config>
<motetype_identifier>wismote2</motetype_identifier>
</mote>
</simulation>
<plugin>
org.contikios.cooja.plugins.SimControl
<width>280</width>
<z>0</z>
<height>160</height>
<location_x>400</location_x>
<location_y>0</location_y>
</plugin>
<plugin>
org.contikios.cooja.plugins.Visualizer
<plugin_config>
<moterelations>true</moterelations>
<skin>org.contikios.cooja.plugins.skins.IDVisualizerSkin</skin>
<skin>org.contikios.cooja.plugins.skins.GridVisualizerSkin</skin>
<skin>org.contikios.cooja.plugins.skins.TrafficVisualizerSkin</skin>
<skin>org.contikios.cooja.plugins.skins.UDGMVisualizerSkin</skin>
<skin>org.contikios.cooja.plugins.skins.LEDVisualizerSkin</skin>
<viewport>4.593848158957425 0.0 0.0 4.593848158957425 13.734375417550426 -121.37641081710846</viewport>
</plugin_config>
<width>400</width>
<z>3</z>
<height>400</height>
<location_x>1</location_x>
<location_y>1</location_y>
</plugin>
<plugin>
org.contikios.cooja.plugins.LogListener
<plugin_config>
<filter />
<formatted_time />
<coloring />
</plugin_config>
<width>959</width>
<z>2</z>
<height>447</height>
<location_x>400</location_x>
<location_y>160</location_y>
</plugin>
<plugin>
org.contikios.cooja.plugins.Notes
<plugin_config>
<notes>Enter notes here</notes>
<decorations>true</decorations>
</plugin_config>
<width>679</width>
<z>1</z>
<height>160</height>
<location_x>680</location_x>
<location_y>0</location_y>
</plugin>
<plugin>
org.contikios.cooja.plugins.MoteInterfaceViewer
<mote_arg>0</mote_arg>
<plugin_config>
<interface>Serial port</interface>
<scrollpos>0,0</scrollpos>
</plugin_config>
<width>579</width>
<z>4</z>
<height>300</height>
<location_x>49</location_x>
<location_y>414</location_y>
</plugin>
</simconf>

View file

@ -0,0 +1,100 @@
/*
* Copyright (c) 2015, Yanzi Networks AB.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
/**
* \file
* OMA LWM2M and IPSO Objects example.
* \author
* Joakim Eriksson, joakime@sics.se
* Niclas Finne, nfi@sics.se
*/
#include "contiki.h"
#include "lwm2m-engine.h"
#include "ipso-objects.h"
#define DEBUG DEBUG_NONE
#include "net/ip/uip-debug.h"
#ifndef REGISTER_WITH_LWM2M_BOOTSTRAP_SERVER
#define REGISTER_WITH_LWM2M_BOOTSTRAP_SERVER 0
#endif
#ifndef REGISTER_WITH_LWM2M_SERVER
#define REGISTER_WITH_LWM2M_SERVER 1
#endif
#ifndef LWM2M_SERVER_ADDRESS
#define LWM2M_SERVER_ADDRESS "aaaa::1"
#endif
PROCESS(example_ipso_objects, "IPSO object example");
AUTOSTART_PROCESSES(&example_ipso_objects);
/*---------------------------------------------------------------------------*/
static void
setup_lwm2m_servers(void)
{
#ifdef LWM2M_SERVER_ADDRESS
uip_ipaddr_t addr;
if(uiplib_ipaddrconv(LWM2M_SERVER_ADDRESS, &addr)) {
lwm2m_engine_register_with_bootstrap_server(&addr, 0);
lwm2m_engine_register_with_server(&addr, 0);
}
#endif /* LWM2M_SERVER_ADDRESS */
lwm2m_engine_use_bootstrap_server(REGISTER_WITH_LWM2M_BOOTSTRAP_SERVER);
lwm2m_engine_use_registration_server(REGISTER_WITH_LWM2M_SERVER);
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(example_ipso_objects, ev, data)
{
PROCESS_BEGIN();
PROCESS_PAUSE();
PRINTF("Starting IPSO objects example\n");
/* Initialize the OMA LWM2M engine */
lwm2m_engine_init();
/* Register default LWM2M objects */
lwm2m_engine_register_default_objects();
/* Register default IPSO objects */
ipso_objects_init();
setup_lwm2m_servers();
while(1) {
PROCESS_WAIT_EVENT();
}
PROCESS_END();
}

View file

@ -0,0 +1,59 @@
/*
* Copyright (c) 2015, Yanzi Networks AB.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/**
* \file
* An dummy temperature driver as example
* \author
* Joakim Eriksson <joakime@sics.se>
* Niclas Finne <nfi@sics.se>
*/
#include "ipso-objects.h"
#include "lib/random.h"
static int32_t last_value = 27000;
/*---------------------------------------------------------------------------*/
static int
read_value(int32_t *value)
{
last_value = last_value + (random_rand() % 1000) - 500;
if(last_value < 18000) {
last_value = 18000;
} else if(last_value > 35000) {
last_value = 35000;
}
*value = last_value;
return 0;
}
/*---------------------------------------------------------------------------*/
const struct ipso_objects_sensor example_ipso_temperature = {
.read_value = read_value
};
/*---------------------------------------------------------------------------*/

View file

@ -0,0 +1,392 @@
/*
* Copyright (c) 2015, Yanzi Networks AB.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
/**
* \file
* IPSO Objects and OMA LWM2M example.
* \author
* Joakim Eriksson, joakime@sics.se
* Niclas Finne, nfi@sics.se
*/
#include "contiki.h"
#include "net/ip/uip.h"
#include "net/rpl/rpl.h"
#include "net/netstack.h"
#include "er-coap-constants.h"
#include "er-coap-engine.h"
#include "lwm2m-engine.h"
#include "oma-tlv.h"
#include "dev/serial-line.h"
#include "serial-protocol.h"
#if CONTIKI_TARGET_WISMOTE
#include "dev/uart1.h"
#endif
#define DEBUG DEBUG_PRINT
#include "net/ip/uip-debug.h"
#define REMOTE_PORT UIP_HTONS(COAP_DEFAULT_PORT)
#define URL_WELL_KNOWN ".well-known/core"
#define URL_DEVICE_MODEL "/3/0/1"
#define URL_DEVICE_FIRMWARE_VERSION "/3/0/3"
#define URL_LIGHT_CONTROL "/3311/0/5850"
#define URL_POWER_CONTROL "/3312/0/5850"
#define MAX_NODES 10
#define NODE_HAS_TYPE (1 << 0)
struct node {
uip_ipaddr_t ipaddr;
char type[32];
uint8_t flags;
uint8_t retries;
};
static struct node nodes[MAX_NODES];
static uint8_t node_count;
static struct node *current_target;
static char current_uri[32] = URL_LIGHT_CONTROL;
static char current_value[32] = "1";
static int current_request = COAP_PUT;
static uint8_t fetching_type = 0;
PROCESS(router_process, "router process");
AUTOSTART_PROCESSES(&router_process);
/*---------------------------------------------------------------------------*/
static struct node *
add_node(const uip_ipaddr_t *addr)
{
int i;
for(i = 0; i < node_count; i++) {
if(uip_ipaddr_cmp(&nodes[i].ipaddr, addr)) {
/* Node already added */
return &nodes[i];
}
}
if(node_count < MAX_NODES) {
uip_ipaddr_copy(&nodes[node_count].ipaddr, addr);
return &nodes[node_count++];
}
return NULL;
}
/*---------------------------------------------------------------------------*/
void
set_value(const uip_ipaddr_t *addr, char *uri, char *value)
{
int i;
printf("#set value ");
uip_debug_ipaddr_print(addr);
printf(" URI: %s Value: %s\n", uri, value);
for(i = 0; i < node_count; i++) {
if(uip_ipaddr_cmp(&nodes[i].ipaddr, addr)) {
/* setup command */
current_target = &nodes[i];
current_request = COAP_PUT;
strncpy(current_uri, uri, sizeof(current_uri) - 1);
strncpy(current_value, value, sizeof(current_value) - 1);
process_poll(&router_process);
break;
}
}
}
/*---------------------------------------------------------------------------*/
void
get_value(const uip_ipaddr_t *addr, char *uri)
{
int i;
printf("#get value ");
uip_debug_ipaddr_print(addr);
printf(" URI: %s\n", uri);
for(i = 0; i < node_count; i++) {
if(uip_ipaddr_cmp(&nodes[i].ipaddr, addr)) {
/* setup command */
current_target = &nodes[i];
current_request = COAP_GET;
strncpy(current_uri, uri, sizeof(current_uri) - 1);
current_value[0] = 0;
process_poll(&router_process);
break;
}
}
}
/*---------------------------------------------------------------------------*/
void
print_node_list(void)
{
int i;
int out = 0;
for(i = 0; i < node_count; i++) {
if(nodes[i].flags & NODE_HAS_TYPE) {
if(out++) {
printf(";");
}
printf("%s,", nodes[i].type);
uip_debug_ipaddr_print(&nodes[i].ipaddr);
}
}
printf("\n");
}
/*---------------------------------------------------------------------------*/
/**
* This function is will be passed to COAP_BLOCKING_REQUEST() to
* handle responses.
*/
static void
client_chunk_handler(void *response)
{
const uint8_t *chunk;
unsigned int format;
int len = coap_get_payload(response, &chunk);
coap_get_header_content_format(response, &format);
/* if(len > 0) { */
/* printf("|%.*s (%d,%d)", len, (char *)chunk, len, format); */
/* } */
if(current_target != NULL && fetching_type) {
if(len > sizeof(current_target->type) - 1) {
len = sizeof(current_target->type) - 1;
}
memcpy(current_target->type, chunk, len);
current_target->type[len] = 0;
current_target->flags |= NODE_HAS_TYPE;
PRINTF("\nNODE ");
PRINT6ADDR(&current_target->ipaddr);
PRINTF(" HAS TYPE %s\n", current_target->type);
} else {
/* otherwise update the current value */
if(format == LWM2M_TLV) {
oma_tlv_t tlv;
/* we can only read int32 for now ? */
if(oma_tlv_read(&tlv, chunk, len) > 0) {
/* printf("TLV.type=%d len=%d id=%d value[0]=%d\n", */
/* tlv.type, tlv.length, tlv.id, tlv.value[0]); */
int value = oma_tlv_get_int32(&tlv);
snprintf(current_value, sizeof(current_value), "%d", value);
}
} else {
if(len > sizeof(current_value) - 1) {
len = sizeof(current_value) - 1;
}
memcpy(current_value, chunk, len);
current_value[len] = 0;
}
}
}
/*---------------------------------------------------------------------------*/
static void
setup_network(void)
{
uip_ipaddr_t ipaddr;
struct uip_ds6_addr *root_if;
rpl_dag_t *dag;
int i;
uint8_t state;
#if CONTIKI_TARGET_WISMOTE
uart1_set_input(serial_line_input_byte);
serial_line_init();
#endif
#if UIP_CONF_ROUTER
/**
* The choice of server address determines its 6LoWPAN header compression.
* Obviously the choice made here must also be selected in udp-client.c.
*
* For correct Wireshark decoding using a sniffer, add the /64 prefix to the 6LowPAN protocol preferences,
* e.g. set Context 0 to aaaa::. At present Wireshark copies Context/128 and then overwrites it.
* (Setting Context 0 to aaaa::1111:2222:3333:4444 will report a 16 bit compressed address of aaaa::1111:22ff:fe33:xxxx)
* Note Wireshark's IPCMV6 checksum verification depends on the correct uncompressed addresses.
*/
#if 0
/* Mode 1 - 64 bits inline */
uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 1);
#elif 1
/* Mode 2 - 16 bits inline */
uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0x00ff, 0xfe00, 1);
#else
/* Mode 3 - derived from link local (MAC) address */
uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
#endif
uip_ds6_addr_add(&ipaddr, 0, ADDR_MANUAL);
root_if = uip_ds6_addr_lookup(&ipaddr);
if(root_if != NULL) {
dag = rpl_set_root(RPL_DEFAULT_INSTANCE, &ipaddr);
uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
rpl_set_prefix(dag, &ipaddr, 64);
PRINTF("created a new RPL dag\n");
} else {
PRINTF("failed to create a new RPL DAG\n");
}
#endif /* UIP_CONF_ROUTER */
PRINTF("IPv6 addresses: ");
for(i = 0; i < UIP_DS6_ADDR_NB; i++) {
state = uip_ds6_if.addr_list[i].state;
if(state == ADDR_TENTATIVE || state == ADDR_PREFERRED) {
PRINT6ADDR(&uip_ds6_if.addr_list[i].ipaddr);
PRINTF("\n");
/* hack to make address "final" */
if (state == ADDR_TENTATIVE) {
uip_ds6_if.addr_list[i].state = ADDR_PREFERRED;
}
}
}
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(router_process, ev, data)
{
/* This way the packet can be treated as pointer as usual. */
static coap_packet_t request[1];
static struct etimer timer;
uip_ds6_route_t *r;
uip_ipaddr_t *nexthop;
int n;
PROCESS_BEGIN();
PROCESS_PAUSE();
/* receives all CoAP messages */
coap_init_engine();
setup_network();
/* The data sink runs with a 100% duty cycle in order to ensure high
packet reception rates. */
NETSTACK_MAC.off(1);
while(1) {
etimer_set(&timer, CLOCK_SECOND * 5);
PROCESS_YIELD();
/* Handle serial line input */
if(ev == serial_line_event_message) {
serial_protocol_input((char *) data);
}
if(etimer_expired(&timer)) {
current_target = NULL;
n = 0;
for(r = uip_ds6_route_head(); r != NULL; r = uip_ds6_route_next(r)) {
current_target = add_node(&r->ipaddr);
if(current_target == NULL ||
(current_target->flags & NODE_HAS_TYPE) != 0 ||
current_target->retries > 5) {
continue;
}
PRINTF(" ");
PRINT6ADDR(&r->ipaddr);
PRINTF(" -> ");
nexthop = uip_ds6_route_nexthop(r);
if(nexthop != NULL) {
PRINT6ADDR(nexthop);
PRINTF("\n");
} else {
PRINTF("-");
}
PRINTF("\n");
n++;
break;
}
}
/* This is a node type discovery */
if(current_target != NULL &&
(current_target->flags & NODE_HAS_TYPE) == 0 &&
current_target->retries < 6) {
/* prepare request, TID is set by COAP_BLOCKING_REQUEST() */
coap_init_message(request, COAP_TYPE_CON, COAP_GET, 0);
coap_set_header_uri_path(request, URL_DEVICE_MODEL);
current_target->retries++;
PRINTF("CoAP request to [");
PRINT6ADDR(&current_target->ipaddr);
PRINTF("]:%u (%u tx)\n", UIP_HTONS(REMOTE_PORT),
current_target->retries);
fetching_type = 1;
COAP_BLOCKING_REQUEST(&current_target->ipaddr, REMOTE_PORT, request,
client_chunk_handler);
fetching_type = 0;
strncpy(current_uri, URL_LIGHT_CONTROL, sizeof(current_uri));
printf("\n--Done--\n");
}
/* If having a type this is another type of request */
if(current_target != NULL &&
(current_target->flags & NODE_HAS_TYPE) && strlen(current_uri) > 0) {
/* prepare request, TID is set by COAP_BLOCKING_REQUEST() */
coap_init_message(request, COAP_TYPE_CON, current_request, 0);
coap_set_header_uri_path(request, current_uri);
if(strlen(current_value) > 0) {
coap_set_payload(request, (uint8_t *)current_value,
strlen(current_value));
}
PRINTF("CoAP request to [");
PRINT6ADDR(&current_target->ipaddr);
PRINTF("]:%u %s\n", UIP_HTONS(REMOTE_PORT), current_uri);
COAP_BLOCKING_REQUEST(&current_target->ipaddr, REMOTE_PORT, request,
client_chunk_handler);
/* print out result of command */
if(current_request == COAP_PUT) {
printf("s ");
} else {
printf("g ");
}
uip_debug_ipaddr_print(&current_target->ipaddr);
printf(" %s %s\n", current_uri, current_value);
current_target = NULL;
current_uri[0] = 0;
current_value[0] = 0;
}
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/

View file

@ -0,0 +1,75 @@
/*
* Copyright (c) 2015, Yanzi Networks AB.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifndef PROJECT_CONF_H_
#define PROJECT_CONF_H_
#ifdef BOARD_STRING
#define LWM2M_DEVICE_MODEL_NUMBER BOARD_STRING
#elif defined(CONTIKI_TARGET_WISMOTE)
#include "dev/watchdog.h"
#define LWM2M_DEVICE_MODEL_NUMBER "wismote"
#define LWM2M_DEVICE_MANUFACTURER "Arago Systems"
#define LWM2M_DEVICE_SERIAL_NO "001"
#define PLATFORM_REBOOT watchdog_reboot
#endif
#define IPSO_TEMPERATURE example_ipso_temperature
/**
* Disabling RDC and CSMA to save memory on constrained devices.
*/
#undef NETSTACK_CONF_RDC
#define NETSTACK_CONF_RDC nullrdc_driver
#undef NETSTACK_CONF_MAC
#define NETSTACK_CONF_MAC nullmac_driver
/* Disabling TCP on CoAP nodes. */
#undef UIP_CONF_TCP
#define UIP_CONF_TCP 0
/* Increase rpl-border-router IP-buffer when using more than 64. */
#undef REST_MAX_CHUNK_SIZE
#define REST_MAX_CHUNK_SIZE 64
/* Multiplies with chunk size, be aware of memory constraints. */
#undef COAP_MAX_OPEN_TRANSACTIONS
#define COAP_MAX_OPEN_TRANSACTIONS 4
/* Filtering .well-known/core per query can be disabled to save space. */
#undef COAP_LINK_FORMAT_FILTERING
#define COAP_LINK_FORMAT_FILTERING 0
#undef COAP_PROXY_OPTION_PROCESSING
#define COAP_PROXY_OPTION_PROCESSING 0
/* Enable client-side support for COAP observe */
#define COAP_OBSERVE_CLIENT 1
#endif /* PROJECT_CONF_H_ */

View file

@ -0,0 +1,126 @@
/*
* Copyright (c) 2015, Yanzi Networks AB.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/**
* \file
* Simple serial protocol to list and interact with devices
* \author
* Joakim Eriksson <joakime@sics.se>
* Niclas Finne <nfi@sics.se>
*/
#include "contiki.h"
#include "net/ip/uip.h"
#include "net/ip/uiplib.h"
#include <stdio.h>
void print_node_list(void);
void set_value(const uip_ipaddr_t *addr, char *uri, char *value);
void get_value(const uip_ipaddr_t *addr, char *uri);
/*---------------------------------------------------------------------------*/
int
find_next_sep(const char *str, char sep, int pos)
{
char c;
while((c = str[pos]) != 0) {
if(c == sep) {
return pos + 1;
}
pos++;
}
return -1;
}
/*---------------------------------------------------------------------------*/
/*
* l - list all discovered devices
* s - set <IP> <URI> <value>
* d - get <IP> <URI>
*/
void
serial_protocol_input(char *data)
{
/* We assume that we have a string here */
char cmd = data[0];
int pos = 0;
switch(cmd) {
case 'l':
/* list devices */
print_node_list();
break;
case 's': {
uip_ip6addr_t ipaddr;
char *uri;
char *value;
pos = find_next_sep(data, ' ', pos);
if(pos > 0) {
/* start of IP */
int start = pos;
pos = find_next_sep(data, ' ', pos);
if(pos == -1) {
return;
}
data[pos - 1] = 0;
if(uiplib_ip6addrconv(&data[start], &ipaddr) == 0) {
printf("* Error not valid IP\n");
}
uri = &data[pos];
pos = find_next_sep(data, ' ', pos);
if(pos == -1) return;
data[pos - 1] = 0;
value = &data[pos];
/* set the value at the specified node */
set_value(&ipaddr, uri, value);
}
break;
}
case 'g': {
uip_ip6addr_t ipaddr;
char *uri;
pos = find_next_sep(data, ' ', pos);
if(pos > 0) {
/* start of IP */
int start = pos;
pos = find_next_sep(data, ' ', pos);
if(pos == -1) return;
data[pos - 1] = 0;
if(uiplib_ip6addrconv((const char *) &data[start], &ipaddr) == 0) {
printf("* Error not valid IP\n");
}
uri = &data[pos];
/* get the value at the specified node */
get_value(&ipaddr, uri);
}
break;
}
default:
printf("Unknown command\n");
}
}
/*---------------------------------------------------------------------------*/

View file

@ -0,0 +1,43 @@
/*
* Copyright (c) 2015, Yanzi Networks AB.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/**
* \file
* Simple serial protocol to list and interact with devices
* \author
* Joakim Eriksson <joakime@sics.se>
* Niclas Finne <nfi@sics.se>
*/
#ifndef SERIAL_PROTOCOL_H_
#define SERIAL_PROTOCOL_H_
void serial_protocol_input(char *data);
#endif /* SERIAL_PROTOCOL_H_ */