Move packet processing example as a new, simpler example: ipv6-hooks
This commit is contained in:
parent
fc57dbbfaf
commit
3418fff4df
4
examples/ipv6-hooks/Makefile
Normal file
4
examples/ipv6-hooks/Makefile
Normal file
@ -0,0 +1,4 @@
|
||||
all: ipv6-hooks
|
||||
|
||||
CONTIKI=../..
|
||||
include $(CONTIKI)/Makefile.include
|
2
examples/ipv6-hooks/README.md
Normal file
2
examples/ipv6-hooks/README.md
Normal file
@ -0,0 +1,2 @@
|
||||
A simple example of how to set up IPv6 packet hooks with netstack_ip_packet_processor_add.
|
||||
You can run this example as native and then ping the node to see printouts from the hooks, at every packet input/output.
|
58
examples/ipv6-hooks/ipv6-hooks.c
Normal file
58
examples/ipv6-hooks/ipv6-hooks.c
Normal file
@ -0,0 +1,58 @@
|
||||
#include "contiki.h"
|
||||
#include "rpl.h"
|
||||
#include "random.h"
|
||||
#include "net/netstack.h"
|
||||
#include "net/ipv6/simple-udp.h"
|
||||
#include "net/ipv6/uipbuf.h"
|
||||
|
||||
#include "sys/log.h"
|
||||
#define LOG_MODULE "App"
|
||||
#define LOG_LEVEL LOG_LEVEL_INFO
|
||||
|
||||
#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
PROCESS(ipv6_hooks_process, "IPv6 Hooks");
|
||||
AUTOSTART_PROCESSES(&ipv6_hooks_process);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static enum netstack_ip_action
|
||||
ip_input(void)
|
||||
{
|
||||
uint8_t proto = 0;
|
||||
uipbuf_get_last_header(uip_buf, uip_len, &proto);
|
||||
LOG_INFO("Incoming packet proto: %d from ", proto);
|
||||
LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
|
||||
LOG_INFO_("\n");
|
||||
return NETSTACK_IP_PROCESS;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static enum netstack_ip_action
|
||||
ip_output(const linkaddr_t *localdest)
|
||||
{
|
||||
uint8_t proto;
|
||||
uint8_t is_me = 0;
|
||||
uipbuf_get_last_header(uip_buf, uip_len, &proto);
|
||||
is_me = uip_ds6_is_my_addr(&UIP_IP_BUF->srcipaddr);
|
||||
LOG_INFO("Outgoing packet (%s) proto: %d to ", is_me ? "send" : "fwd ", proto);
|
||||
LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr);
|
||||
LOG_INFO_("\n");
|
||||
return NETSTACK_IP_PROCESS;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
struct netstack_ip_packet_processor packet_processor = {
|
||||
.process_input = ip_input,
|
||||
.process_output = ip_output
|
||||
};
|
||||
/*---------------------------------------------------------------------------*/
|
||||
PROCESS_THREAD(ipv6_hooks_process, ev, data)
|
||||
{
|
||||
PROCESS_BEGIN();
|
||||
|
||||
/* Register packet processor */
|
||||
netstack_ip_packet_processor_add(&packet_processor);
|
||||
|
||||
/* Do nothing */
|
||||
|
||||
PROCESS_END();
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
@ -1,4 +1,4 @@
|
||||
all: udp-client udp-server udp-client-packet-debug
|
||||
all: udp-client udp-server
|
||||
|
||||
CONTIKI=../..
|
||||
include $(CONTIKI)/Makefile.include
|
||||
|
@ -1,285 +0,0 @@
|
||||
<?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>RPL-UDP Example -- Packet Debug</title>
|
||||
<randomseed>generated</randomseed>
|
||||
<motedelay_us>5000000</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>sky1</identifier>
|
||||
<description>Sky Mote Type #sky1</description>
|
||||
<source EXPORT="discard">[CONTIKI_DIR]/examples/rpl-udp/udp-server.c</source>
|
||||
<commands EXPORT="discard">make clean udp-server.sky TARGET=sky</commands>
|
||||
<firmware EXPORT="copy">[CONTIKI_DIR]/examples/rpl-udp/udp-server.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>sky2</identifier>
|
||||
<description>Sky Mote Type #sky2</description>
|
||||
<source EXPORT="discard">[CONTIKI_DIR]/examples/rpl-udp/udp-client-packet-debug.c</source>
|
||||
<commands EXPORT="discard">make clean udp-client-packet-debug.sky TARGET=sky</commands>
|
||||
<firmware EXPORT="copy">[CONTIKI_DIR]/examples/rpl-udp/udp-client-packet-debug.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>30.051578821079996</x>
|
||||
<y>-64.69428746901113</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>sky1</motetype_identifier>
|
||||
</mote>
|
||||
<mote>
|
||||
<breakpoints />
|
||||
<interface_config>
|
||||
org.contikios.cooja.interfaces.Position
|
||||
<x>21.31366587648077</x>
|
||||
<y>-34.91404431659299</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>sky2</motetype_identifier>
|
||||
</mote>
|
||||
<mote>
|
||||
<breakpoints />
|
||||
<interface_config>
|
||||
org.contikios.cooja.interfaces.Position
|
||||
<x>57.165216906562264</x>
|
||||
<y>-34.02362118358309</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>sky2</motetype_identifier>
|
||||
</mote>
|
||||
<mote>
|
||||
<breakpoints />
|
||||
<interface_config>
|
||||
org.contikios.cooja.interfaces.Position
|
||||
<x>75.1786320042507</x>
|
||||
<y>-60.441618586411096</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>4</id>
|
||||
</interface_config>
|
||||
<motetype_identifier>sky2</motetype_identifier>
|
||||
</mote>
|
||||
<mote>
|
||||
<breakpoints />
|
||||
<interface_config>
|
||||
org.contikios.cooja.interfaces.Position
|
||||
<x>59.11488934836</x>
|
||||
<y>-7.544844766953879</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>5</id>
|
||||
</interface_config>
|
||||
<motetype_identifier>sky2</motetype_identifier>
|
||||
</mote>
|
||||
<mote>
|
||||
<breakpoints />
|
||||
<interface_config>
|
||||
org.contikios.cooja.interfaces.Position
|
||||
<x>91.720430365082</x>
|
||||
<y>-22.736406051209734</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>6</id>
|
||||
</interface_config>
|
||||
<motetype_identifier>sky2</motetype_identifier>
|
||||
</mote>
|
||||
<mote>
|
||||
<breakpoints />
|
||||
<interface_config>
|
||||
org.contikios.cooja.interfaces.Position
|
||||
<x>92.90372351580778</x>
|
||||
<y>2.532304558933846</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>7</id>
|
||||
</interface_config>
|
||||
<motetype_identifier>sky2</motetype_identifier>
|
||||
</mote>
|
||||
<mote>
|
||||
<breakpoints />
|
||||
<interface_config>
|
||||
org.contikios.cooja.interfaces.Position
|
||||
<x>120.28246466847423</x>
|
||||
<y>19.133402903417824</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>8</id>
|
||||
</interface_config>
|
||||
<motetype_identifier>sky2</motetype_identifier>
|
||||
</mote>
|
||||
</simulation>
|
||||
<plugin>
|
||||
org.contikios.cooja.plugins.SimControl
|
||||
<width>249</width>
|
||||
<z>4</z>
|
||||
<height>184</height>
|
||||
<location_x>3</location_x>
|
||||
<location_y>15</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.AttributeVisualizerSkin</skin>
|
||||
<viewport>1.3598488697820064 0.0 0.0 1.3598488697820064 6.142207908179105 118.20877091196155</viewport>
|
||||
</plugin_config>
|
||||
<width>234</width>
|
||||
<z>1</z>
|
||||
<height>227</height>
|
||||
<location_x>14</location_x>
|
||||
<location_y>210</location_y>
|
||||
</plugin>
|
||||
<plugin>
|
||||
org.contikios.cooja.plugins.LogListener
|
||||
<plugin_config>
|
||||
<filter />
|
||||
<formatted_time />
|
||||
<coloring />
|
||||
</plugin_config>
|
||||
<width>1011</width>
|
||||
<z>0</z>
|
||||
<height>556</height>
|
||||
<location_x>759</location_x>
|
||||
<location_y>7</location_y>
|
||||
</plugin>
|
||||
<plugin>
|
||||
org.contikios.cooja.plugins.TimeLine
|
||||
<plugin_config>
|
||||
<mote>0</mote>
|
||||
<mote>1</mote>
|
||||
<mote>2</mote>
|
||||
<mote>3</mote>
|
||||
<mote>4</mote>
|
||||
<mote>5</mote>
|
||||
<mote>6</mote>
|
||||
<mote>7</mote>
|
||||
<showRadioRXTX />
|
||||
<showRadioHW />
|
||||
<showLEDs />
|
||||
<zoomfactor>681.712557066089</zoomfactor>
|
||||
</plugin_config>
|
||||
<width>1804</width>
|
||||
<z>3</z>
|
||||
<height>352</height>
|
||||
<location_x>0</location_x>
|
||||
<location_y>567</location_y>
|
||||
</plugin>
|
||||
<plugin>
|
||||
org.contikios.cooja.plugins.RadioLogger
|
||||
<plugin_config>
|
||||
<split>150</split>
|
||||
<formatted_time />
|
||||
<showdups>false</showdups>
|
||||
<hidenodests>false</hidenodests>
|
||||
</plugin_config>
|
||||
<width>500</width>
|
||||
<z>2</z>
|
||||
<height>546</height>
|
||||
<location_x>259</location_x>
|
||||
<location_y>12</location_y>
|
||||
</plugin>
|
||||
</simconf>
|
@ -1,110 +0,0 @@
|
||||
#include "contiki.h"
|
||||
#include "rpl.h"
|
||||
#include "random.h"
|
||||
#include "net/netstack.h"
|
||||
#include "net/ipv6/simple-udp.h"
|
||||
#include "net/ipv6/uipbuf.h"
|
||||
|
||||
#include "sys/log.h"
|
||||
#define LOG_MODULE "App"
|
||||
#define LOG_LEVEL LOG_LEVEL_INFO
|
||||
|
||||
#define WITH_SERVER_REPLY 1
|
||||
#define UDP_CLIENT_PORT 8765
|
||||
#define UDP_SERVER_PORT 5678
|
||||
|
||||
static struct simple_udp_connection udp_conn;
|
||||
|
||||
#define START_INTERVAL (15 * CLOCK_SECOND)
|
||||
#define SEND_INTERVAL (60 * CLOCK_SECOND)
|
||||
|
||||
#define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
|
||||
|
||||
static struct simple_udp_connection udp_conn;
|
||||
static uip_ipaddr_t server_ipaddr;
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
PROCESS(udp_client_process, "UDP client -- packet debug");
|
||||
AUTOSTART_PROCESSES(&udp_client_process);
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static enum netstack_ip_action
|
||||
ip_input(void)
|
||||
{
|
||||
uint8_t proto = 0;
|
||||
uipbuf_get_last_header(uip_buf, uip_len, &proto);
|
||||
LOG_INFO("Incoming packet proto: %d from ", proto);
|
||||
LOG_INFO_6ADDR(&UIP_IP_BUF->srcipaddr);
|
||||
LOG_INFO_("\n");
|
||||
return NETSTACK_IP_PROCESS;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static enum netstack_ip_action
|
||||
ip_output(const linkaddr_t *localdest)
|
||||
{
|
||||
uint8_t proto;
|
||||
uint8_t is_me = 0;
|
||||
uipbuf_get_last_header(uip_buf, uip_len, &proto);
|
||||
is_me = uip_ds6_is_my_addr(&UIP_IP_BUF->srcipaddr);
|
||||
LOG_INFO("Outgoing packet (%s) proto: %d to ", is_me ? "send" : "fwd ", proto);
|
||||
LOG_INFO_6ADDR(&UIP_IP_BUF->destipaddr);
|
||||
LOG_INFO_("\n");
|
||||
return NETSTACK_IP_PROCESS;
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
struct netstack_ip_packet_processor packet_processor = {
|
||||
.process_input = ip_input,
|
||||
.process_output = ip_output
|
||||
};
|
||||
/*---------------------------------------------------------------------------*/
|
||||
static void
|
||||
udp_rx_callback(struct simple_udp_connection *c,
|
||||
const uip_ipaddr_t *sender_addr,
|
||||
uint16_t sender_port,
|
||||
const uip_ipaddr_t *receiver_addr,
|
||||
uint16_t receiver_port,
|
||||
const uint8_t *data,
|
||||
uint16_t datalen)
|
||||
{
|
||||
unsigned count = *(unsigned *)data;
|
||||
LOG_INFO("Received response %u from ", count);
|
||||
LOG_INFO_6ADDR(sender_addr);
|
||||
LOG_INFO_("\n");
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
||||
PROCESS_THREAD(udp_client_process, ev, data)
|
||||
{
|
||||
static struct etimer periodic_timer;
|
||||
static unsigned count;
|
||||
|
||||
PROCESS_BEGIN();
|
||||
|
||||
INIT_SERVER_IPADDR(server_ipaddr);
|
||||
/* Initialize UDP connection */
|
||||
simple_udp_register(&udp_conn, UDP_CLIENT_PORT, NULL,
|
||||
UDP_SERVER_PORT, udp_rx_callback);
|
||||
|
||||
/* register packet processor */
|
||||
netstack_ip_packet_processor_add(&packet_processor);
|
||||
|
||||
etimer_set(&periodic_timer, random_rand() % SEND_INTERVAL);
|
||||
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer));
|
||||
etimer_set(&periodic_timer, SEND_INTERVAL);
|
||||
while(1) {
|
||||
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer));
|
||||
|
||||
if(rpl_is_reachable()) {
|
||||
LOG_INFO("Sending request %u to ", count);
|
||||
LOG_INFO_6ADDR(&server_ipaddr);
|
||||
LOG_INFO_("\n");
|
||||
simple_udp_sendto(&udp_conn, &count, sizeof(count), &server_ipaddr);
|
||||
count++;
|
||||
} else {
|
||||
LOG_INFO("Not reachable yet %p\n", rpl_get_any_dag());
|
||||
}
|
||||
|
||||
etimer_reset(&periodic_timer);
|
||||
}
|
||||
|
||||
PROCESS_END();
|
||||
}
|
||||
/*---------------------------------------------------------------------------*/
|
Loading…
Reference in New Issue
Block a user