WITH_WEBSERVER as a module

This commit is contained in:
Simon Duquennoy 2017-09-01 16:50:48 +02:00 committed by Niclas Finne
parent 65fe7a1bb3
commit dc0a0ed60f
11 changed files with 4 additions and 984 deletions

View File

@ -13,7 +13,7 @@ ifeq ($(WITH_WEBSERVER),1)
CFLAGS += -DWEBSERVER=1
PROJECT_SOURCEFILES += httpd-simple.c
else ifneq ($(WITH_WEBSERVER), 0)
APPS += $(WITH_WEBSERVER)
MODULES += $(WITH_WEBSERVER)
CFLAGS += -DWEBSERVER=2
endif

View File

@ -1,5 +1,5 @@
CONTIKI_PROJECT=border-router
all: $(CONTIKI_PROJECT)
all: $(CONTIKI_PROJECT)
CONTIKI=../../..
@ -8,9 +8,7 @@ PROJECT_SOURCEFILES += slip-bridge.c
#Simple built-in webserver is the default.
#Override with make WITH_WEBSERVER=0 for no webserver.
#WITH_WEBSERVER=webserver-name will use /apps/webserver-name if it can be
#found in the /apps, /arch/platform/$(TARGET)/apps/, or current directory (in that order).
# WITH_WEBSERVER=webserver for /apps/webserver
#WITH_WEBSERVER=module-path will use the module if it can be found
#make clean before changing webservers!
#Note /apps/webserver contains a 2500 byte style sheet which is a severe test
@ -23,7 +21,7 @@ CFLAGS += -DUIP_CONF_TCP=1
CFLAGS += -DWEBSERVER=1
PROJECT_SOURCEFILES += httpd-simple.c
else ifneq ($(WITH_WEBSERVER), 0)
APPS += $(WITH_WEBSERVER)
MODULES += $(WITH_WEBSERVER)
CFLAGS += -DUIP_CONF_TCP=1
CFLAGS += -DWEBSERVER=2
endif

View File

@ -1,22 +0,0 @@
all: sky-websense
CONTIKI=../../../..
APPS += webbrowser
CFLAGS += -DPROJECT_CONF_H=\"project-conf.h\"
CONTIKI_SOURCEFILES += wget.c
PROJECTDIRS += ../rpl-border-router
PROJECT_SOURCEFILES += httpd-simple.c
CONTIKI_WITH_IPV6 = 1
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 fd00::1/64
connect-router-cooja: $(CONTIKI)/tools/tunslip6
sudo $(CONTIKI)/tools/tunslip6 -a 127.0.0.1 fd00::1/64

View File

@ -1 +0,0 @@
TARGET = sky

View File

@ -1,43 +0,0 @@
Sky websense
============
This example features a simple webserver running on top of the IPv6 contiki
stack on Sky motes to provide sensor values, and with a RPL border router to
bridge the sensor network to Internet.
To test the example in COOJA under Linux
----------------------------------------
1. Start COOJA and load the simulation "example-sky-websense.csc"
make TARGET=cooja example-sky-websense.csc
2. Connect to the COOJA simulation using tunslip6:
make connect-router-cooja
3. You should now be able to browse to the nodes using your web browser:
Router: http://[fd00::0212:7401:0001:0101]/
Node 2: http://[fd00::0212:7402:0002:0202]/
To run the example on real nodes under Linux
--------------------------------------------
1. Program the nodes with the websense application
make TARGET=sky sky-websense.upload
2. Disconnect the nodes and program one node with the RPL border router
cd ../rpl-border-router && make TARGET=sky border-router.upload
3. Connect to the border router using tunslip6:
make connect-router
4. Reboot the router and note the router IP address
5. You should now be able to browse to your router node using your web browser:
http://[<ROUTER IPv6 ADDRESS>]/. On this page you should see a list of all
accessible nodes with their IP adresses.

View File

@ -1,207 +0,0 @@
<?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>Sky Websense with RPL router</title>
<delaytime>0</delaytime>
<randomseed>123456</randomseed>
<motedelay_us>1000000</motedelay_us>
<radiomedium>
org.contikios.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>
org.contikios.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>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.SkyButton</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.SkyFlash</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.SkyCoffeeFilesystem</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.Msp802154Radio</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspSerial</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.SkyLED</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspDebugOutput</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.SkyTemperature</moteinterface>
</motetype>
<motetype>
org.contikios.cooja.mspmote.SkyMoteType
<identifier>skyweb</identifier>
<description>Sky Websense</description>
<source EXPORT="discard">[CONTIKI_DIR]/examples/ipv6/sky-websense/sky-websense.c</source>
<commands EXPORT="discard">make sky-websense.sky TARGET=sky</commands>
<firmware EXPORT="copy">[CONTIKI_DIR]/examples/ipv6/sky-websense/sky-websense.sky</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.SkyButton</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.SkyFlash</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.SkyCoffeeFilesystem</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.Msp802154Radio</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspSerial</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.SkyLED</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.MspDebugOutput</moteinterface>
<moteinterface>org.contikios.cooja.mspmote.interfaces.SkyTemperature</moteinterface>
</motetype>
<mote>
<breakpoints />
<interface_config>
org.contikios.cooja.interfaces.Position
<x>33.260163187353555</x>
<y>30.643217359962595</y>
<z>0.0</z>
</interface_config>
<interface_config>
org.contikios.cooja.mspmote.interfaces.MspMoteID
<id>1</id>
</interface_config>
<motetype_identifier>rplroot</motetype_identifier>
</mote>
<mote>
<breakpoints />
<interface_config>
org.contikios.cooja.interfaces.Position
<x>62.239287566073514</x>
<y>34.43810269527116</y>
<z>0.0</z>
</interface_config>
<interface_config>
org.contikios.cooja.mspmote.interfaces.MspMoteID
<id>2</id>
</interface_config>
<motetype_identifier>skyweb</motetype_identifier>
</mote>
<mote>
<breakpoints />
<interface_config>
org.contikios.cooja.interfaces.Position
<x>56.71945435107925</x>
<y>20.293530081848317</y>
<z>0.0</z>
</interface_config>
<interface_config>
org.contikios.cooja.mspmote.interfaces.MspMoteID
<id>3</id>
</interface_config>
<motetype_identifier>skyweb</motetype_identifier>
</mote>
<mote>
<breakpoints />
<interface_config>
org.contikios.cooja.interfaces.Position
<x>75.34889145168493</x>
<y>24.43340499309403</y>
<z>0.0</z>
</interface_config>
<interface_config>
org.contikios.cooja.mspmote.interfaces.MspMoteID
<id>4</id>
</interface_config>
<motetype_identifier>skyweb</motetype_identifier>
</mote>
<mote>
<breakpoints />
<interface_config>
org.contikios.cooja.interfaces.Position
<x>92.59837024854205</x>
<y>38.57797760651687</y>
<z>0.0</z>
</interface_config>
<interface_config>
org.contikios.cooja.mspmote.interfaces.MspMoteID
<id>5</id>
</interface_config>
<motetype_identifier>skyweb</motetype_identifier>
</mote>
<mote>
<breakpoints />
<interface_config>
org.contikios.cooja.interfaces.Position
<x>47.68359039801751</x>
<y>47.26544238238854</y>
<z>0.0</z>
</interface_config>
<interface_config>
org.contikios.cooja.mspmote.interfaces.MspMoteID
<id>6</id>
</interface_config>
<motetype_identifier>skyweb</motetype_identifier>
</mote>
</simulation>
<plugin>
org.contikios.cooja.plugins.SimControl
<width>259</width>
<z>4</z>
<height>179</height>
<location_x>0</location_x>
<location_y>0</location_y>
</plugin>
<plugin>
org.contikios.cooja.plugins.Visualizer
<plugin_config>
<skin>org.contikios.cooja.plugins.skins.IDVisualizerSkin</skin>
<skin>org.contikios.cooja.plugins.skins.UDGMVisualizerSkin</skin>
<skin>org.contikios.cooja.plugins.skins.MoteTypeVisualizerSkin</skin>
<viewport>2.898638306051894 0.0 0.0 2.898638306051894 -68.40918308040007 -27.82360366026197</viewport>
</plugin_config>
<width>258</width>
<z>2</z>
<height>209</height>
<location_x>0</location_x>
<location_y>178</location_y>
</plugin>
<plugin>
org.contikios.cooja.plugins.LogListener
<plugin_config>
<filter />
</plugin_config>
<width>1024</width>
<z>3</z>
<height>311</height>
<location_x>0</location_x>
<location_y>385</location_y>
</plugin>
<plugin>
org.contikios.cooja.plugins.RadioLogger
<plugin_config>
<split>150</split>
<analyzers name="6lowpan" />
</plugin_config>
<width>769</width>
<z>0</z>
<height>342</height>
<location_x>255</location_x>
<location_y>-1</location_y>
</plugin>
<plugin>
org.contikios.cooja.serialsocket.SerialSocketServer
<mote_arg>0</mote_arg>
<width>422</width>
<z>1</z>
<height>74</height>
<location_x>601</location_x>
<location_y>310</location_y>
</plugin>
</simconf>

View File

@ -1,47 +0,0 @@
/*
* 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.
*
*/
#ifndef PROJECT_RPL_WEB_CONF_H_
#define PROJECT_RPL_WEB_CONF_H_
#undef QUEUEBUF_CONF_NUM
#define QUEUEBUF_CONF_NUM 5
#undef UIP_CONF_BUFFER_SIZE
#define UIP_CONF_BUFFER_SIZE 140
#undef WEBSERVER_CONF_CFS_CONNS
#define WEBSERVER_CONF_CFS_CONNS 2
/* Reserve space for a file name (default is to not use file name) */
#undef WEBSERVER_CONF_CFS_PATHLEN
#define WEBSERVER_CONF_CFS_PATHLEN 80
#endif /* PROJECT_RPL_WEB_CONF_H_ */

View File

@ -1,178 +0,0 @@
/*
* 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.
*
*/
/**
* \file
* Light and temperatur sensor web demo
* \author
* Niclas Finne <nfi@sics.se>
* Joakim Eriksson <joakime@sics.se>
* Joel Hoglund <joel@sics.se>
*/
#include "contiki.h"
#include "httpd-simple.h"
#include "dev/sht11/sht11-sensor.h"
#include "dev/light-sensor.h"
#include "dev/leds.h"
#include <stdio.h>
PROCESS(web_sense_process, "Sense Web Demo");
PROCESS(webserver_nogui_process, "Web server");
PROCESS_THREAD(webserver_nogui_process, ev, data)
{
PROCESS_BEGIN();
httpd_init();
while(1) {
PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);
httpd_appcall(data);
}
PROCESS_END();
}
AUTOSTART_PROCESSES(&web_sense_process,&webserver_nogui_process);
#define HISTORY 16
static int temperature[HISTORY];
static int light1[HISTORY];
static int sensors_pos;
/*---------------------------------------------------------------------------*/
static int
get_light(void)
{
return 10 * light_sensor.value(LIGHT_SENSOR_PHOTOSYNTHETIC) / 7;
}
/*---------------------------------------------------------------------------*/
static int
get_temp(void)
{
return ((sht11_sensor.value(SHT11_SENSOR_TEMP) / 10) - 396) / 10;
}
/*---------------------------------------------------------------------------*/
static const char *TOP = "<html><head><title>Contiki Web Sense</title></head><body>\n";
static const char *BOTTOM = "</body></html>\n";
/*---------------------------------------------------------------------------*/
/* Only one single request at time */
static char buf[256];
static int blen;
#define ADD(...) do { \
blen += snprintf(&buf[blen], sizeof(buf) - blen, __VA_ARGS__); \
} while(0)
static void
generate_chart(const char *title, const char *unit, int min, int max, int *values)
{
int i;
blen = 0;
ADD("<h1>%s</h1>\n"
"<img src=\"http://chart.apis.google.com/chart?"
"cht=lc&chs=400x300&chxt=x,x,y,y&chxp=1,50|3,50&"
"chxr=2,%d,%d|0,0,30&chds=%d,%d&chxl=1:|Time|3:|%s&chd=t:",
title, min, max, min, max, unit);
for(i = 0; i < HISTORY; i++) {
ADD("%s%d", i > 0 ? "," : "", values[(sensors_pos + i) % HISTORY]);
}
ADD("\">");
}
static
PT_THREAD(send_values(struct httpd_state *s))
{
PSOCK_BEGIN(&s->sout);
SEND_STRING(&s->sout, TOP);
if(strncmp(s->filename, "/index", 6) == 0 ||
s->filename[1] == '\0') {
/* Default page: show latest sensor values as text (does not
require Internet connection to Google for charts). */
blen = 0;
ADD("<h1>Current readings</h1>\n"
"Light: %u<br>"
"Temperature: %u&deg; C",
get_light(), get_temp());
SEND_STRING(&s->sout, buf);
} else if(s->filename[1] == '0') {
/* Turn off leds */
leds_off(LEDS_ALL);
SEND_STRING(&s->sout, "Turned off leds!");
} else if(s->filename[1] == '1') {
/* Turn on leds */
leds_on(LEDS_ALL);
SEND_STRING(&s->sout, "Turned on leds!");
} else {
if(s->filename[1] != 't') {
generate_chart("Light", "Light", 0, 500, light1);
SEND_STRING(&s->sout, buf);
}
if(s->filename[1] != 'l') {
generate_chart("Temperature", "Celsius", 15, 50, temperature);
SEND_STRING(&s->sout, buf);
}
}
SEND_STRING(&s->sout, BOTTOM);
PSOCK_END(&s->sout);
}
/*---------------------------------------------------------------------------*/
httpd_simple_script_t
httpd_simple_get_script(const char *name)
{
return send_values;
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(web_sense_process, ev, data)
{
static struct etimer timer;
PROCESS_BEGIN();
sensors_pos = 0;
etimer_set(&timer, CLOCK_SECOND * 2);
SENSORS_ACTIVATE(light_sensor);
SENSORS_ACTIVATE(sht11_sensor);
while(1) {
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&timer));
etimer_reset(&timer);
light1[sensors_pos] = get_light();;
temperature[sensors_pos] = get_temp();
sensors_pos = (sensors_pos + 1) % HISTORY;
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/

View File

@ -1,184 +0,0 @@
/*
* 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.
*
*/
/**
* \file
* A simple example using HTTP to control and be controlled
* \author
* Niclas Finne <nfi@sics.se>
* Joakim Eriksson <joakime@sics.se>
*/
#include "contiki.h"
#include "dev/button-sensor.h"
#include "dev/leds.h"
#include "wget.h"
#include "httpd-simple.h"
#include <stdio.h>
/* The address of the server to register the services for this node */
#define SERVER "[fd00::1]"
/* This command registers two services (/0 and /1) to turn the leds on or off */
#define REGISTER_COMMAND "/r?p=0&d=Turn%20off%20leds&p=1&d=Turn%20on%20leds"
/* The address of the other node to control */
#define OTHER_NODE "[fd00::1]"
/* The commands to send to the other node */
#define SET_LEDS_ON "/1"
#define SET_LEDS_OFF "/0"
PROCESS(websense_remote_process, "Websense Remote");
PROCESS(webserver_nogui_process, "Web server");
PROCESS_THREAD(webserver_nogui_process, ev, data)
{
PROCESS_BEGIN();
httpd_init();
while(1) {
PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event);
httpd_appcall(data);
}
PROCESS_END();
}
AUTOSTART_PROCESSES(&websense_remote_process,&webserver_nogui_process);
/*---------------------------------------------------------------------------*/
static const char *TOP = "<html><head><title>Contiki Websense Remote</title></head><body>\n";
static const char *BOTTOM = "</body></html>\n";
/*---------------------------------------------------------------------------*/
static
PT_THREAD(handle_command(struct httpd_state *s))
{
PSOCK_BEGIN(&s->sout);
SEND_STRING(&s->sout, TOP);
if(s->filename[1] == '0') {
/* Turn off leds */
leds_off(LEDS_ALL);
SEND_STRING(&s->sout, "Turned off leds!");
} else if(s->filename[1] == '1') {
/* Turn on leds */
leds_on(LEDS_ALL);
SEND_STRING(&s->sout, "Turned on leds!");
} else {
SEND_STRING(&s->sout, "Unknown command");
}
SEND_STRING(&s->sout, BOTTOM);
PSOCK_END(&s->sout);
}
/*---------------------------------------------------------------------------*/
httpd_simple_script_t
httpd_simple_get_script(const char *name)
{
return handle_command;
}
/*---------------------------------------------------------------------------*/
static void
wget_done(int status)
{
switch(status) {
case WGET_OK:
printf("Command sent.\n");
break;
case WGET_TIMEDOUT:
printf("Command timedout.\n");
break;
case WGET_ABORTED:
printf("Connection aborted.\n");
break;
case WGET_CONNECT_FAILED:
printf("Failed to connect.\n");
break;
}
}
static const struct wget_callbacks callbacks = {
NULL, wget_done
};
static void
send_command(const char *server, const char *command)
{
int ret;
printf("Sending to [%s]: %s\n", server, command);
ret = wget_get(server, 80, command, &callbacks);
if(ret != WGET_OK) {
if(ret == WGET_ALREADY_RUNNING) {
printf("Waiting for previous command to finish.\n");
} else {
printf("Failed to send command: %u\n", ret);
}
}
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(websense_remote_process, ev, data)
{
static int mode;
static struct etimer timer;
PROCESS_BEGIN();
mode = 0;
wget_init();
SENSORS_ACTIVATE(button_sensor);
/* Give the node some time to join the network before registering
the available services. */
etimer_set(&timer, CLOCK_SECOND * 30);
while(1) {
PROCESS_WAIT_EVENT();
if(ev == sensors_event && data == &button_sensor) {
/* The button has been pressed. Send command to the other node. */
if(mode) {
/* Turn on leds */
send_command(OTHER_NODE, SET_LEDS_OFF);
} else {
send_command(OTHER_NODE, SET_LEDS_ON);
}
/* Alternate between the two commands */
mode = !mode;
} else if(ev == PROCESS_EVENT_TIMER && etimer_expired(&timer)) {
printf("Registering services\n");
send_command(SERVER, REGISTER_COMMAND);
}
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/

View File

@ -1,234 +0,0 @@
/*
* 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.
*
*/
/**
* \file
* A simple wget implementation
* \author
* Niclas Finne <nfi@sics.se>
* Joakim Eriksson <joakime@sics.se>
*/
#include "webclient.h"
#include "wget.h"
#include "dev/leds.h"
#define DEBUG DEBUG_NONE
#include "net/ip/uip-debug.h"
#define DEBUG_LEDS 0
#undef LEDS_ON
#undef LEDS_OFF
#if DEBUG_LEDS
#define LEDS_ON(led) leds_on(led)
#define LEDS_OFF(led) leds_off(led)
#else
#define LEDS_ON(led)
#define LEDS_OFF(led)
#endif /* DEBUG */
static int fetch_running;
#define STATS ((DEBUG) & DEBUG_PRINT) && 1
#if STATS
static clock_time_t fetch_started;
static unsigned long fetch_counter;
#endif /* STATUS */
static const char *server;
static const char *file;
static uint16_t port;
static const struct wget_callbacks *callbacks;
const char http_10[9] =
/* "HTTP/1.0" */
{0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x30, };
const char http_11[9] =
/* "HTTP/1.1" */
{0x48, 0x54, 0x54, 0x50, 0x2f, 0x31, 0x2e, 0x31, };
const char http_200[5] =
/* "200 " */
{0x32, 0x30, 0x30, 0x20, };
const char http_301[5] =
/* "301 " */
{0x33, 0x30, 0x31, 0x20, };
const char http_302[5] =
/* "302 " */
{0x33, 0x30, 0x32, 0x20, };
const char http_crnl[3] =
/* "\r\n" */
{0xd, 0xa, };
const char http_host[7] =
/* "host: " */
{0x68, 0x6f, 0x73, 0x74, 0x3a, 0x20, };
const char http_content_type[15] =
/* "content-type: " */
{0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x74, 0x79, 0x70, 0x65, 0x3a, 0x20, };
const char http_location[11] =
/* "location: " */
{0x6c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x3a, 0x20, };
const char http_http[8] =
/* "http://" */
{0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, };
PROCESS(wget_process, "wget");
/*---------------------------------------------------------------------------*/
static void
call_done(int status)
{
if(callbacks != NULL && callbacks->done != NULL) {
callbacks->done(status);
}
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(wget_process, ev, data)
{
PROCESS_BEGIN();
PRINTF("wget: fetching %s\n", file);
#if STATS
fetch_counter = 0;
fetch_started = clock_time();
#endif /* STATS */
LEDS_ON(LEDS_YELLOW);
if(webclient_get(server, port, file) == 0) {
PRINTF("wget: failed to connect\n");
LEDS_OFF(LEDS_YELLOW);
fetch_running = 0;
call_done(WGET_CONNECT_FAILED);
} else {
while(fetch_running) {
PROCESS_WAIT_EVENT();
if(ev == tcpip_event) {
webclient_appcall(data);
}
}
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
void
webclient_datahandler(char *data, uint16_t len)
{
if(len == 0) {
#if STATS
clock_time_t elapsed;
elapsed = clock_time() - fetch_started;
PRINTF("wget: recv %lu bytes during %lu sec (",
fetch_counter, (elapsed / CLOCK_SECOND));
#if CLOCK_SECOND == 128
PRINTF("%lu.%02lus, ", (unsigned long) elapsed >> 7,
(unsigned long)((elapsed & 127) * 100) / 128);
PRINTF("%lu byte/sec ", (fetch_counter * 128L) / elapsed);
#endif
PRINTF("%lu tick): ", (unsigned long) elapsed);
if(elapsed > CLOCK_SECOND) {
PRINTF("%lu", fetch_counter / (elapsed / CLOCK_SECOND));
} else {
PRINTF("-");
}
PRINTF(" byte/sec\n");
#else /* STATS */
PRINTF("wget: complete\n");
#endif /* STATS */
fetch_running = 0;
call_done(WGET_OK);
LEDS_OFF(LEDS_RED|LEDS_YELLOW);
} else {
#if STATS
fetch_counter += len;
#endif /* STATS */
if(callbacks != NULL && callbacks->data != NULL) {
/* Assume the buffer is large enough for ending zero */
data[len] = '\0';
callbacks->data(data, len);
}
}
}
/*---------------------------------------------------------------------------*/
void
webclient_connected(void)
{
LEDS_OFF(LEDS_YELLOW);
LEDS_ON(LEDS_RED);
}
/*---------------------------------------------------------------------------*/
void
webclient_timedout(void)
{
PRINTF("wget: timedout\n");
LEDS_OFF(LEDS_RED|LEDS_YELLOW);
fetch_running = 0;
call_done(WGET_TIMEDOUT);
}
/*---------------------------------------------------------------------------*/
void
webclient_aborted(void)
{
PRINTF("wget: aborted\n");
LEDS_OFF(LEDS_RED|LEDS_YELLOW);
fetch_running = 0;
call_done(WGET_ABORTED);
}
/*---------------------------------------------------------------------------*/
void
webclient_closed(void)
{
PRINTF("wget: closed\n");
fetch_running = 0;
LEDS_OFF(LEDS_RED|LEDS_YELLOW);
call_done(WGET_CLOSED);
}
/*---------------------------------------------------------------------------*/
void
wget_init(void)
{
webclient_init();
}
/*---------------------------------------------------------------------------*/
int
wget_get(const char *s, uint16_t p, const char *f,
const struct wget_callbacks *c)
{
if(fetch_running) {
return WGET_ALREADY_RUNNING;
}
fetch_running = 1;
server = s;
port = p;
file = f;
callbacks = c;
process_start(&wget_process, NULL);
return WGET_OK;
}
/*---------------------------------------------------------------------------*/

View File

@ -1,62 +0,0 @@
/*
* 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.
*
*/
/**
* \file
* A simple wget implementation
* \author
* Niclas Finne <nfi@sics.se>
* Joakim Eriksson <joakime@sics.se>
*/
#ifndef WGET_H_
#define WGET_H_
#include "contiki.h"
struct wget_callbacks {
void (* data)(const char *data, uint16_t len);
void (* done)(int status);
};
void wget_init(void);
int wget_get(const char *server, uint16_t port, const char *file,
const struct wget_callbacks *c);
enum {
WGET_OK,
WGET_ALREADY_RUNNING,
WGET_TIMEDOUT,
WGET_ABORTED,
WGET_CONNECT_FAILED,
WGET_CLOSED
};
#endif /* WGET_H_ */