Initial commit of a RESTful web service example using experimental COAP/HTTP/REST implementation for Contiki
This commit is contained in:
parent
e441bd4321
commit
b71de57f94
31
examples/rest-example/Makefile
Normal file
31
examples/rest-example/Makefile
Normal file
@ -0,0 +1,31 @@
|
||||
ifndef TARGET
|
||||
TARGET=sky
|
||||
endif
|
||||
|
||||
CONTIKI=../..
|
||||
|
||||
WITH_UIP6=1
|
||||
UIP_CONF_IPV6=1
|
||||
|
||||
WITH_COAP = 1
|
||||
|
||||
CFLAGS += -DPROJECT_CONF_H=\"web-conf.h\"
|
||||
|
||||
ifdef WITH_COAP
|
||||
CFLAGS += -DWITH_COAP
|
||||
APPS = rest-coap
|
||||
else
|
||||
CFLAGS += -DWITH_HTTP
|
||||
APPS = rest-http
|
||||
endif
|
||||
|
||||
include $(CONTIKI)/Makefile.include
|
||||
|
||||
$(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 aaaa::1/64
|
31
examples/rest-example/README
Normal file
31
examples/rest-example/README
Normal file
@ -0,0 +1,31 @@
|
||||
COOJA RUN:
|
||||
|
||||
Open a terminal and go to "rest" directory.
|
||||
In rest/Makefile, define WITH_COAP = 1 if you want to use COAP, rather than HTTP.
|
||||
Issue following command to load and compile Rest api with COOJA.
|
||||
>make rest-example-no-rpl.csc TARGET=cooja
|
||||
|
||||
In another terminal, issue the following command
|
||||
>make connect-router-cooja
|
||||
|
||||
There are 2 nodes running the Rest code in COOJA (node 2 and 6). Their IP addresses are aaaa::0212:7402:0002:0202 and aaaa::0212:7406:0006:0606 respectively.
|
||||
|
||||
TEST:
|
||||
Please check the rest/rest-example.c source code to see which resources are available. (check the RESOURCE macros in the code).
|
||||
|
||||
COAP Examples:
|
||||
You can use COAPTester client to interact with the COOJA motes running REST code.
|
||||
|
||||
java COAPTester GET .well-known/core
|
||||
java COAPTester POST led?color=blue mode=on #turn on the blue led
|
||||
java COAPTester GET toggle
|
||||
java COAPTester GET helloworld #get helloworld plain text
|
||||
|
||||
|
||||
HTTP Examples
|
||||
You can use curl as an http client to interact with the COOJA motes running REST code.
|
||||
curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #get helloworld plain text
|
||||
curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/led?color=green -d mode=off -i #turn off the green led
|
||||
curl -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/.well-known/core -i
|
||||
curl -X POST -H "User-Agent: curl" aaaa::0212:7402:0002:0202:8080/helloworld #method not allowed
|
||||
|
185
examples/rest-example/rest-example-no-rpl.csc
Normal file
185
examples/rest-example/rest-example-no-rpl.csc
Normal file
@ -0,0 +1,185 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<simconf>
|
||||
<project EXPORT="discard">[CONTIKI_DIR]/tools/cooja/apps/mrm</project>
|
||||
<project EXPORT="discard">[CONTIKI_DIR]/tools/cooja/apps/mspsim</project>
|
||||
<project EXPORT="discard">[CONTIKI_DIR]/tools/cooja/apps/avrora</project>
|
||||
<project EXPORT="discard">[CONTIKI_DIR]/tools/cooja/apps/serial_socket</project>
|
||||
<simulation>
|
||||
<title>Rest without RPL router</title>
|
||||
<delaytime>-2147483648</delaytime>
|
||||
<randomseed>123456</randomseed>
|
||||
<motedelay_us>1000000</motedelay_us>
|
||||
<radiomedium>
|
||||
se.sics.cooja.radiomediums.UDGM
|
||||
<transmitting_range>50.0</transmitting_range>
|
||||
<interference_range>50.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>
|
||||
se.sics.cooja.mspmote.SkyMoteType
|
||||
<identifier>rplroot</identifier>
|
||||
<description>Sky RPL Root</description>
|
||||
<source EXPORT="discard">[CONTIKI_DIR]/examples/ipv6/border-router-no-rpl/border-router.c</source>
|
||||
<commands EXPORT="discard">make border-router.sky TARGET=sky</commands>
|
||||
<firmware EXPORT="copy">[CONTIKI_DIR]/examples/ipv6/border-router-no-rpl/border-router.sky</firmware>
|
||||
<moteinterface>se.sics.cooja.interfaces.Position</moteinterface>
|
||||
<moteinterface>se.sics.cooja.interfaces.RimeAddress</moteinterface>
|
||||
<moteinterface>se.sics.cooja.interfaces.IPAddress</moteinterface>
|
||||
<moteinterface>se.sics.cooja.interfaces.Mote2MoteRelations</moteinterface>
|
||||
<moteinterface>se.sics.cooja.interfaces.MoteAttributes</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.MspClock</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.MspMoteID</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.SkyButton</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.SkyFlash</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.SkyCoffeeFilesystem</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.SkyByteRadio</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.MspSerial</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.SkyLED</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.MspDebugOutput</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.SkyTemperature</moteinterface>
|
||||
</motetype>
|
||||
<motetype>
|
||||
se.sics.cooja.mspmote.SkyMoteType
|
||||
<identifier>skyweb</identifier>
|
||||
<description>Rest</description>
|
||||
<source EXPORT="discard">[CONTIKI_DIR]/examples/rest-example/rest-example.c</source>
|
||||
<commands EXPORT="discard">make rest-example.sky TARGET=sky</commands>
|
||||
<firmware EXPORT="copy">[CONTIKI_DIR]/examples/rest-example/rest-example.sky</firmware>
|
||||
<moteinterface>se.sics.cooja.interfaces.Position</moteinterface>
|
||||
<moteinterface>se.sics.cooja.interfaces.RimeAddress</moteinterface>
|
||||
<moteinterface>se.sics.cooja.interfaces.IPAddress</moteinterface>
|
||||
<moteinterface>se.sics.cooja.interfaces.Mote2MoteRelations</moteinterface>
|
||||
<moteinterface>se.sics.cooja.interfaces.MoteAttributes</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.MspClock</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.MspMoteID</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.SkyButton</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.SkyFlash</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.SkyCoffeeFilesystem</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.SkyByteRadio</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.MspSerial</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.SkyLED</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.MspDebugOutput</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.SkyTemperature</moteinterface>
|
||||
</motetype>
|
||||
<mote>
|
||||
<breakpoints />
|
||||
<interface_config>
|
||||
se.sics.cooja.interfaces.Position
|
||||
<x>33.260163187353555</x>
|
||||
<y>30.643217359962595</y>
|
||||
<z>0.0</z>
|
||||
</interface_config>
|
||||
<interface_config>
|
||||
se.sics.cooja.mspmote.interfaces.MspMoteID
|
||||
<id>1</id>
|
||||
</interface_config>
|
||||
<motetype_identifier>rplroot</motetype_identifier>
|
||||
</mote>
|
||||
<mote>
|
||||
<breakpoints />
|
||||
<interface_config>
|
||||
se.sics.cooja.interfaces.Position
|
||||
<x>62.239287566073514</x>
|
||||
<y>34.43810269527116</y>
|
||||
<z>0.0</z>
|
||||
</interface_config>
|
||||
<interface_config>
|
||||
se.sics.cooja.mspmote.interfaces.MspMoteID
|
||||
<id>2</id>
|
||||
</interface_config>
|
||||
<motetype_identifier>skyweb</motetype_identifier>
|
||||
</mote>
|
||||
<mote>
|
||||
<breakpoints />
|
||||
<interface_config>
|
||||
se.sics.cooja.interfaces.Position
|
||||
<x>47.68359039801751</x>
|
||||
<y>47.26544238238854</y>
|
||||
<z>0.0</z>
|
||||
</interface_config>
|
||||
<interface_config>
|
||||
se.sics.cooja.mspmote.interfaces.MspMoteID
|
||||
<id>6</id>
|
||||
</interface_config>
|
||||
<motetype_identifier>skyweb</motetype_identifier>
|
||||
</mote>
|
||||
</simulation>
|
||||
<plugin>
|
||||
se.sics.cooja.plugins.SimControl
|
||||
<width>259</width>
|
||||
<z>1</z>
|
||||
<height>179</height>
|
||||
<location_x>0</location_x>
|
||||
<location_y>0</location_y>
|
||||
</plugin>
|
||||
<plugin>
|
||||
se.sics.cooja.plugins.Visualizer
|
||||
<plugin_config>
|
||||
<skin>se.sics.cooja.plugins.skins.IDVisualizerSkin</skin>
|
||||
<skin>se.sics.cooja.plugins.skins.UDGMVisualizerSkin</skin>
|
||||
<skin>se.sics.cooja.plugins.skins.MoteTypeVisualizerSkin</skin>
|
||||
<skin>se.sics.cooja.plugins.skins.AttributeVisualizerSkin</skin>
|
||||
<viewport>10.505309204322225 0.0 0.0 10.505309204322225 -249.89475921566975 -141.01191150973983</viewport>
|
||||
</plugin_config>
|
||||
<width>819</width>
|
||||
<z>5</z>
|
||||
<height>563</height>
|
||||
<location_x>35</location_x>
|
||||
<location_y>306</location_y>
|
||||
</plugin>
|
||||
<plugin>
|
||||
se.sics.cooja.plugins.LogListener
|
||||
<plugin_config>
|
||||
<filter />
|
||||
</plugin_config>
|
||||
<width>762</width>
|
||||
<z>0</z>
|
||||
<height>326</height>
|
||||
<location_x>36</location_x>
|
||||
<location_y>296</location_y>
|
||||
</plugin>
|
||||
<plugin>
|
||||
se.sics.cooja.plugins.RadioLogger
|
||||
<plugin_config>
|
||||
<split>150</split>
|
||||
<analyzers name="6lowpan" />
|
||||
</plugin_config>
|
||||
<width>815</width>
|
||||
<z>4</z>
|
||||
<height>385</height>
|
||||
<location_x>255</location_x>
|
||||
<location_y>8</location_y>
|
||||
</plugin>
|
||||
<plugin>
|
||||
SerialSocketServer
|
||||
<mote_arg>0</mote_arg>
|
||||
<width>422</width>
|
||||
<z>3</z>
|
||||
<height>74</height>
|
||||
<location_x>1234</location_x>
|
||||
<location_y>93</location_y>
|
||||
</plugin>
|
||||
<plugin>
|
||||
se.sics.cooja.plugins.TimeLine
|
||||
<plugin_config>
|
||||
<mote>0</mote>
|
||||
<mote>1</mote>
|
||||
<showRadioRXTX />
|
||||
<showRadioHW />
|
||||
<showLEDs />
|
||||
<showWatchpoints />
|
||||
<split>125</split>
|
||||
<zoomfactor>25.49079397896416</zoomfactor>
|
||||
</plugin_config>
|
||||
<width>1624</width>
|
||||
<z>2</z>
|
||||
<height>252</height>
|
||||
<location_x>166</location_x>
|
||||
<location_y>699</location_y>
|
||||
</plugin>
|
||||
</simconf>
|
||||
|
185
examples/rest-example/rest-example-with-rpl.csc
Normal file
185
examples/rest-example/rest-example-with-rpl.csc
Normal file
@ -0,0 +1,185 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<simconf>
|
||||
<project EXPORT="discard">[CONTIKI_DIR]/tools/cooja/apps/mrm</project>
|
||||
<project EXPORT="discard">[CONTIKI_DIR]/tools/cooja/apps/mspsim</project>
|
||||
<project EXPORT="discard">[CONTIKI_DIR]/tools/cooja/apps/avrora</project>
|
||||
<project EXPORT="discard">[CONTIKI_DIR]/tools/cooja/apps/serial_socket</project>
|
||||
<simulation>
|
||||
<title>Rest with RPL router</title>
|
||||
<delaytime>-2147483648</delaytime>
|
||||
<randomseed>123456</randomseed>
|
||||
<motedelay_us>1000000</motedelay_us>
|
||||
<radiomedium>
|
||||
se.sics.cooja.radiomediums.UDGM
|
||||
<transmitting_range>50.0</transmitting_range>
|
||||
<interference_range>50.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>
|
||||
se.sics.cooja.mspmote.SkyMoteType
|
||||
<identifier>rplroot</identifier>
|
||||
<description>Sky RPL Root</description>
|
||||
<source EXPORT="discard">[CONTIKI_DIR]/examples/ipv6/rpl-border-router/border-router.c</source>
|
||||
<commands EXPORT="discard">make border-router.sky TARGET=sky</commands>
|
||||
<firmware EXPORT="copy">[CONTIKI_DIR]/examples/ipv6/rpl-border-router/border-router.sky</firmware>
|
||||
<moteinterface>se.sics.cooja.interfaces.Position</moteinterface>
|
||||
<moteinterface>se.sics.cooja.interfaces.RimeAddress</moteinterface>
|
||||
<moteinterface>se.sics.cooja.interfaces.IPAddress</moteinterface>
|
||||
<moteinterface>se.sics.cooja.interfaces.Mote2MoteRelations</moteinterface>
|
||||
<moteinterface>se.sics.cooja.interfaces.MoteAttributes</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.MspClock</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.MspMoteID</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.SkyButton</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.SkyFlash</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.SkyCoffeeFilesystem</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.SkyByteRadio</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.MspSerial</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.SkyLED</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.MspDebugOutput</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.SkyTemperature</moteinterface>
|
||||
</motetype>
|
||||
<motetype>
|
||||
se.sics.cooja.mspmote.SkyMoteType
|
||||
<identifier>skyweb</identifier>
|
||||
<description>Rest</description>
|
||||
<source EXPORT="discard">[CONTIKI_DIR]/examples/rest-example/rest-example.c</source>
|
||||
<commands EXPORT="discard">make rest-example.sky TARGET=sky</commands>
|
||||
<firmware EXPORT="copy">[CONTIKI_DIR]/examples/rest-example/rest-example.sky</firmware>
|
||||
<moteinterface>se.sics.cooja.interfaces.Position</moteinterface>
|
||||
<moteinterface>se.sics.cooja.interfaces.RimeAddress</moteinterface>
|
||||
<moteinterface>se.sics.cooja.interfaces.IPAddress</moteinterface>
|
||||
<moteinterface>se.sics.cooja.interfaces.Mote2MoteRelations</moteinterface>
|
||||
<moteinterface>se.sics.cooja.interfaces.MoteAttributes</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.MspClock</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.MspMoteID</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.SkyButton</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.SkyFlash</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.SkyCoffeeFilesystem</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.SkyByteRadio</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.MspSerial</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.SkyLED</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.MspDebugOutput</moteinterface>
|
||||
<moteinterface>se.sics.cooja.mspmote.interfaces.SkyTemperature</moteinterface>
|
||||
</motetype>
|
||||
<mote>
|
||||
<breakpoints />
|
||||
<interface_config>
|
||||
se.sics.cooja.interfaces.Position
|
||||
<x>33.260163187353555</x>
|
||||
<y>30.643217359962595</y>
|
||||
<z>0.0</z>
|
||||
</interface_config>
|
||||
<interface_config>
|
||||
se.sics.cooja.mspmote.interfaces.MspMoteID
|
||||
<id>1</id>
|
||||
</interface_config>
|
||||
<motetype_identifier>rplroot</motetype_identifier>
|
||||
</mote>
|
||||
<mote>
|
||||
<breakpoints />
|
||||
<interface_config>
|
||||
se.sics.cooja.interfaces.Position
|
||||
<x>62.239287566073514</x>
|
||||
<y>34.43810269527116</y>
|
||||
<z>0.0</z>
|
||||
</interface_config>
|
||||
<interface_config>
|
||||
se.sics.cooja.mspmote.interfaces.MspMoteID
|
||||
<id>2</id>
|
||||
</interface_config>
|
||||
<motetype_identifier>skyweb</motetype_identifier>
|
||||
</mote>
|
||||
<mote>
|
||||
<breakpoints />
|
||||
<interface_config>
|
||||
se.sics.cooja.interfaces.Position
|
||||
<x>47.68359039801751</x>
|
||||
<y>47.26544238238854</y>
|
||||
<z>0.0</z>
|
||||
</interface_config>
|
||||
<interface_config>
|
||||
se.sics.cooja.mspmote.interfaces.MspMoteID
|
||||
<id>6</id>
|
||||
</interface_config>
|
||||
<motetype_identifier>skyweb</motetype_identifier>
|
||||
</mote>
|
||||
</simulation>
|
||||
<plugin>
|
||||
se.sics.cooja.plugins.SimControl
|
||||
<width>259</width>
|
||||
<z>1</z>
|
||||
<height>179</height>
|
||||
<location_x>0</location_x>
|
||||
<location_y>0</location_y>
|
||||
</plugin>
|
||||
<plugin>
|
||||
se.sics.cooja.plugins.Visualizer
|
||||
<plugin_config>
|
||||
<skin>se.sics.cooja.plugins.skins.IDVisualizerSkin</skin>
|
||||
<skin>se.sics.cooja.plugins.skins.UDGMVisualizerSkin</skin>
|
||||
<skin>se.sics.cooja.plugins.skins.MoteTypeVisualizerSkin</skin>
|
||||
<skin>se.sics.cooja.plugins.skins.AttributeVisualizerSkin</skin>
|
||||
<viewport>10.505309204322225 0.0 0.0 10.505309204322225 -249.89475921566975 -141.01191150973983</viewport>
|
||||
</plugin_config>
|
||||
<width>819</width>
|
||||
<z>5</z>
|
||||
<height>563</height>
|
||||
<location_x>35</location_x>
|
||||
<location_y>306</location_y>
|
||||
</plugin>
|
||||
<plugin>
|
||||
se.sics.cooja.plugins.LogListener
|
||||
<plugin_config>
|
||||
<filter />
|
||||
</plugin_config>
|
||||
<width>762</width>
|
||||
<z>0</z>
|
||||
<height>326</height>
|
||||
<location_x>36</location_x>
|
||||
<location_y>296</location_y>
|
||||
</plugin>
|
||||
<plugin>
|
||||
se.sics.cooja.plugins.RadioLogger
|
||||
<plugin_config>
|
||||
<split>150</split>
|
||||
<analyzers name="6lowpan" />
|
||||
</plugin_config>
|
||||
<width>815</width>
|
||||
<z>4</z>
|
||||
<height>385</height>
|
||||
<location_x>255</location_x>
|
||||
<location_y>8</location_y>
|
||||
</plugin>
|
||||
<plugin>
|
||||
SerialSocketServer
|
||||
<mote_arg>0</mote_arg>
|
||||
<width>422</width>
|
||||
<z>3</z>
|
||||
<height>74</height>
|
||||
<location_x>1234</location_x>
|
||||
<location_y>93</location_y>
|
||||
</plugin>
|
||||
<plugin>
|
||||
se.sics.cooja.plugins.TimeLine
|
||||
<plugin_config>
|
||||
<mote>0</mote>
|
||||
<mote>1</mote>
|
||||
<showRadioRXTX />
|
||||
<showRadioHW />
|
||||
<showLEDs />
|
||||
<showWatchpoints />
|
||||
<split>125</split>
|
||||
<zoomfactor>25.49079397896416</zoomfactor>
|
||||
</plugin_config>
|
||||
<width>1624</width>
|
||||
<z>2</z>
|
||||
<height>252</height>
|
||||
<location_x>166</location_x>
|
||||
<location_y>699</location_y>
|
||||
</plugin>
|
||||
</simconf>
|
||||
|
136
examples/rest-example/rest-example.c
Normal file
136
examples/rest-example/rest-example.c
Normal file
@ -0,0 +1,136 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "contiki.h"
|
||||
#include "contiki-net.h"
|
||||
#include "dev/leds.h"
|
||||
|
||||
#include "rest.h"
|
||||
|
||||
PROCESS(rest_example, "Rest Example");
|
||||
AUTOSTART_PROCESSES(&rest_example);
|
||||
|
||||
#define DEBUG 1
|
||||
#if DEBUG
|
||||
#include <stdio.h>
|
||||
#define PRINTF(...) printf(__VA_ARGS__)
|
||||
#define PRINT6ADDR(addr) PRINTF(" %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x ", ((u8_t *)addr)[0], ((u8_t *)addr)[1], ((u8_t *)addr)[2], ((u8_t *)addr)[3], ((u8_t *)addr)[4], ((u8_t *)addr)[5], ((u8_t *)addr)[6], ((u8_t *)addr)[7], ((u8_t *)addr)[8], ((u8_t *)addr)[9], ((u8_t *)addr)[10], ((u8_t *)addr)[11], ((u8_t *)addr)[12], ((u8_t *)addr)[13], ((u8_t *)addr)[14], ((u8_t *)addr)[15])
|
||||
#define PRINTLLADDR(lladdr) PRINTF(" %02x:%02x:%02x:%02x:%02x:%02x ",(lladdr)->addr[0], (lladdr)->addr[1], (lladdr)->addr[2], (lladdr)->addr[3],(lladdr)->addr[4], (lladdr)->addr[5])
|
||||
#else
|
||||
#define PRINTF(...)
|
||||
#define PRINT6ADDR(addr)
|
||||
#define PRINTLLADDR(addr)
|
||||
#endif
|
||||
|
||||
char temp[100];
|
||||
|
||||
/* Resources are defined by RESOURCE macro, signature: resource name, the http methods it handles and its url*/
|
||||
RESOURCE(helloworld, METHOD_GET, "helloworld");
|
||||
|
||||
/* For each resource defined, there corresponds an handler method which should be defined too.
|
||||
* Name of the handler method should be [resource name]_handler
|
||||
* */
|
||||
void
|
||||
helloworld_handler(REQUEST* request, RESPONSE* response)
|
||||
{
|
||||
sprintf(temp,"Hello World!\n");
|
||||
|
||||
rest_set_header_content_type(response, TEXT_PLAIN);
|
||||
rest_set_payload(response, temp, strlen(temp));
|
||||
}
|
||||
|
||||
/*A simple actuator example, depending on the color query parameter and post variable mode, corresponding led is activated or deactivated*/
|
||||
RESOURCE(led, METHOD_POST | METHOD_PUT , "led");
|
||||
|
||||
void
|
||||
led_handler(REQUEST* request, RESPONSE* response)
|
||||
{
|
||||
char color[10];
|
||||
char mode[10];
|
||||
uint8_t led = 0;
|
||||
int success = 1;
|
||||
|
||||
if (rest_get_query_variable(request, "color", color, 10)) {
|
||||
PRINTF("color %s\n", color);
|
||||
|
||||
if (!strcmp(color,"red")) {
|
||||
led = LEDS_RED;
|
||||
} else if(!strcmp(color,"green")) {
|
||||
led = LEDS_GREEN;
|
||||
} else if ( !strcmp(color,"blue") ) {
|
||||
led = LEDS_BLUE;
|
||||
} else {
|
||||
success = 0;
|
||||
}
|
||||
} else {
|
||||
success = 0;
|
||||
}
|
||||
|
||||
if (success && rest_get_post_variable(request, "mode", mode, 10)) {
|
||||
PRINTF("mode %s\n", mode);
|
||||
|
||||
if (!strcmp(mode, "on")) {
|
||||
leds_on(led);
|
||||
} else if (!strcmp(mode, "off")) {
|
||||
leds_off(led);
|
||||
} else {
|
||||
success = 0;
|
||||
}
|
||||
} else {
|
||||
success = 0;
|
||||
}
|
||||
|
||||
if (!success) {
|
||||
rest_set_response_status(response, BAD_REQUEST_400);
|
||||
}
|
||||
}
|
||||
|
||||
/*A simple getter example. Returns the reading from light sensor with a simple etag*/
|
||||
RESOURCE(light, METHOD_GET, "light");
|
||||
void
|
||||
light_handler(REQUEST* request, RESPONSE* response)
|
||||
{
|
||||
sprintf(temp,"%d.%d", 131,6);
|
||||
|
||||
char etag[4] = "ABCD";
|
||||
rest_set_header_content_type(response, TEXT_PLAIN);
|
||||
rest_set_header_etag(response, etag, sizeof(etag));
|
||||
rest_set_payload(response, temp, strlen(temp));
|
||||
}
|
||||
|
||||
/*A simple actuator example. Toggles the red led*/
|
||||
RESOURCE(toggle, METHOD_GET | METHOD_PUT | METHOD_POST, "toggle");
|
||||
void
|
||||
toggle_handler(REQUEST* request, RESPONSE* response)
|
||||
{
|
||||
leds_toggle(LEDS_RED);
|
||||
}
|
||||
|
||||
RESOURCE(discover, METHOD_GET, ".well-known/core");
|
||||
void
|
||||
discover_handler(REQUEST* request, RESPONSE* response)
|
||||
{
|
||||
char temp[100];
|
||||
int index = 0;
|
||||
index += sprintf(temp + index, "%s\n", "<helloworld>;n=\"HelloWorld\"");
|
||||
index += sprintf(temp + index, "%s\n", "<led>;n=\"LedControl\"");
|
||||
index += sprintf(temp + index, "%s\n", "<light>;n=\"Light\"");
|
||||
|
||||
rest_set_payload(response, temp, strlen(temp));
|
||||
rest_set_header_content_type(response, APPLICATION_LINK_FORMAT);
|
||||
}
|
||||
|
||||
PROCESS_THREAD(rest_example, ev, data)
|
||||
{
|
||||
PROCESS_BEGIN();
|
||||
PRINTF("REST Example\n");
|
||||
|
||||
rest_init();
|
||||
|
||||
rest_activate_resource(&resource_helloworld);
|
||||
rest_activate_resource(&resource_led);
|
||||
rest_activate_resource(&resource_light);
|
||||
rest_activate_resource(&resource_toggle);
|
||||
rest_activate_resource(&resource_discover);
|
||||
|
||||
PROCESS_END();
|
||||
}
|
47
examples/rest-example/web-conf.h
Normal file
47
examples/rest-example/web-conf.h
Normal file
@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright (c) 2010, Swedish Institute of Computer Science.
|
||||
* 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 Institute 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 INSTITUTE 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 INSTITUTE OR CONTRIBUTORS 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.
|
||||
*
|
||||
* $Id: web-conf.h,v 1.1 2010/10/28 12:42:07 doganyazar Exp $
|
||||
*/
|
||||
|
||||
#ifndef __PROJECT_RPL_WEB_CONF_H__
|
||||
#define __PROJECT_RPL_WEB_CONF_H__
|
||||
|
||||
#undef QUEUEBUF_CONF_NUM
|
||||
#define QUEUEBUF_CONF_NUM 6
|
||||
|
||||
#undef UIP_CONF_BUFFER_SIZE
|
||||
#define UIP_CONF_BUFFER_SIZE 140
|
||||
|
||||
#undef UIP_CONF_RECEIVE_WINDOW
|
||||
#define UIP_CONF_RECEIVE_WINDOW 60
|
||||
|
||||
#undef WEBSERVER_CONF_CFS_CONNS
|
||||
#define WEBSERVER_CONF_CFS_CONNS 2
|
||||
|
||||
#endif /* __PROJECT_RPL_WEB_CONF_H__ */
|
Loading…
Reference in New Issue
Block a user