<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://anrg.usc.edu/contiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Pedro</id>
		<title>Contiki - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="http://anrg.usc.edu/contiki/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Pedro"/>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php/Special:Contributions/Pedro"/>
		<updated>2026-05-22T07:27:21Z</updated>
		<subtitle>User contributions</subtitle>
		<generator>MediaWiki 1.26.2</generator>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=Installation&amp;diff=1864</id>
		<title>Installation</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=Installation&amp;diff=1864"/>
				<updated>2017-01-05T17:05:34Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* You will learn */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This guide covers the installation of ContikiOS 3.0.  You will need either a Linux-based operating system, or an operating system that supports [https://www.virtualbox.org/ VirtualBox] or [https://my.vmware.com/web/vmware/downloads VMWare Player].&lt;br /&gt;
&lt;br /&gt;
== You will learn ==&lt;br /&gt;
&lt;br /&gt;
This tutorial will take you through the steps for downloading and installing ContikiOS the following ways:&lt;br /&gt;
&lt;br /&gt;
* Using Instant Contiki [http://sourceforge.net/projects/contiki/files/Instant%20Contiki/]&lt;br /&gt;
* Using ContikiOS from Github repository [https://github.com/contiki-os/contiki]&lt;br /&gt;
&lt;br /&gt;
=== Using Instant Contiki ===&lt;br /&gt;
&lt;br /&gt;
This is the easiest way of installing and using ContikiOS. You simply need to download Instant Contiki, which is a virtual machine created with all necessary toolchains and software for ContikiOS development.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Link to download Instant Contiki: [http://sourceforge.net/projects/contiki/files/Instant%20Contiki/]&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
After downloading Instant Contiki you need also to download either VMWare Player or VirtualBox, if you are using Windows as your host OS. If you use MacOS X you should download VMWare Fusion, instead of VMWare Player.&lt;br /&gt;
&lt;br /&gt;
IMPORTANT: To login in to Instant Contiki: &amp;#039;&amp;#039;&amp;#039;username&amp;#039;&amp;#039;&amp;#039;: &amp;#039;&amp;#039;user&amp;#039;&amp;#039; &amp;#039;&amp;#039;&amp;#039;password&amp;#039;&amp;#039;&amp;#039;: &amp;#039;&amp;#039;user&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
=== Using ContikiOS from Github repository ===&lt;br /&gt;
&lt;br /&gt;
This method requires an existing installation of a Linux-based operating system. The guide assumes you are using Ubuntu 16.04 but should still be useful for similar versions.&lt;br /&gt;
&lt;br /&gt;
Download contiki-3.0.zip from [https://github.com/contiki-os/contiki/releases https://github.com/contiki-os/contiki/releases]&lt;br /&gt;
&lt;br /&gt;
  wget https://github.com/contiki-os/contiki/archive/3.0.zip&lt;br /&gt;
&lt;br /&gt;
Unzip the file. For the purposes of this guide, it is assumed that the file is unzipped into /home/user/&lt;br /&gt;
&lt;br /&gt;
  unzip 3.0.zip&lt;br /&gt;
&lt;br /&gt;
Rename the folder from contiki-3.0 to contiki.&lt;br /&gt;
&lt;br /&gt;
  mv contiki-3.0 contiki&lt;br /&gt;
&lt;br /&gt;
Install all the required packages for compiling and running ContikiOS. &lt;br /&gt;
&lt;br /&gt;
If you are using a MSP430-based platforms (such as Tmote) you only need the following packages: &amp;#039;&amp;#039;binutils-msp430&amp;#039;&amp;#039;, &amp;#039;&amp;#039;gcc-msp430&amp;#039;&amp;#039;, &amp;#039;&amp;#039;msp430-libc&amp;#039;&amp;#039;, &amp;#039;&amp;#039;msp430mcu&amp;#039;&amp;#039; and &amp;#039;&amp;#039;mspdebug&amp;#039;&amp;#039;. If you intend to code for CC2538-based platforms (such as OpenMote), you should also install the following tool chains: &amp;#039;&amp;#039;gcc-arm-none-eabi&amp;#039;&amp;#039; and &amp;#039;&amp;#039;gdb-arm-none-eabi&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
You can run the following command to install all packages for multiple platforms:&lt;br /&gt;
&lt;br /&gt;
  sudo apt-get install build-essential binutils-msp430 gcc-msp430 msp430-libc msp430mcu mspdebug gcc-arm-none-eabi gdb-arm-none-eabi openjdk-8-jdk openjdk-8-jre ant libncurses5-dev&lt;br /&gt;
&lt;br /&gt;
When working with a 64-bit virtual machine, you may run into problems with the `serialdump-linux` executable because it may have been compiled for 32-bit machines. Install the following package to fix this issue.&lt;br /&gt;
&lt;br /&gt;
  sudo apt-get install lib32ncurses5&lt;br /&gt;
&lt;br /&gt;
You are done! You can now try to compile and run any example, such as [[Hello_World | Hello World]].&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
Edited by: Kwame, Pedro&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=Installation&amp;diff=1863</id>
		<title>Installation</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=Installation&amp;diff=1863"/>
				<updated>2017-01-04T17:13:16Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This guide covers the installation of ContikiOS 3.0.  You will need either a Linux-based operating system, or an operating system that supports [https://www.virtualbox.org/ VirtualBox] or [https://my.vmware.com/web/vmware/downloads VMWare Player].&lt;br /&gt;
&lt;br /&gt;
== You will learn ==&lt;br /&gt;
&lt;br /&gt;
This tutorial will take you through the steps for downloading and installing ContikiOS the following ways:&lt;br /&gt;
&lt;br /&gt;
* Download Instant Contiki [http://sourceforge.net/projects/contiki/files/Instant%20Contiki/]&lt;br /&gt;
* Download ContikiOS from Sourceforge repository [http://sourceforge.net/projects/contiki/files/latest/download]&lt;br /&gt;
* Clone ContikiOS from Github repository [https://github.com/contiki-os/contiki]&lt;br /&gt;
&lt;br /&gt;
=== Using Instant Contiki ===&lt;br /&gt;
&lt;br /&gt;
This is the easiest way of installing and using ContikiOS. You simply need to download Instant Contiki, which is a virtual machine created with all necessary toolchains and software for ContikiOS development.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Link to download Instant Contiki: [http://sourceforge.net/projects/contiki/files/Instant%20Contiki/]&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
After downloading Instant Contiki you need also to download either VMWare Player or VirtualBox, if you are using Windows as your host OS. If you use MacOS X you should download VMWare Fusion, instead of VMWare Player.&lt;br /&gt;
&lt;br /&gt;
IMPORTANT: To login in to Instant Contiki: &amp;#039;&amp;#039;&amp;#039;username&amp;#039;&amp;#039;&amp;#039;: &amp;#039;&amp;#039;user&amp;#039;&amp;#039; &amp;#039;&amp;#039;&amp;#039;password&amp;#039;&amp;#039;&amp;#039;: &amp;#039;&amp;#039;user&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
=== Using Sourceforge repository ===&lt;br /&gt;
&lt;br /&gt;
This method requires an existing installation of a Linux-based operating system. The guide assumes you are using Ubuntu 12.04 but should still be useful for similar versions.&lt;br /&gt;
&lt;br /&gt;
Download contiki-3.0.zip from [http://downloads.sourceforge.net/project/contiki/Contiki/Contiki%202.7/contiki-2.7.zip http://downloads.sourceforge.net/project/contiki/Contiki/Contiki 2.7/contiki-2.7.zip]&lt;br /&gt;
&lt;br /&gt;
  wget http://downloads.sourceforge.net/project/contiki/Contiki/Contiki%202.7/contiki-2.7.zip&lt;br /&gt;
&lt;br /&gt;
Unzip the file. For the purposes of this guide, it is assumed that the file is unzipped into /home/user/&lt;br /&gt;
&lt;br /&gt;
  unzip contiki-3.0.zip&lt;br /&gt;
&lt;br /&gt;
Rename the folder from contiki-3.0 to contiki.&lt;br /&gt;
&lt;br /&gt;
  mv contiki-3.0 contiki&lt;br /&gt;
&lt;br /&gt;
Install all the required packages for compiling and running ContikiOS. &lt;br /&gt;
&lt;br /&gt;
If you are using a MSP430-based platform (such as Tmote) you only need the following packages: &amp;#039;&amp;#039;binutils-msp430&amp;#039;&amp;#039;, &amp;#039;&amp;#039;gcc-msp430&amp;#039;&amp;#039;, &amp;#039;&amp;#039;msp430-libc&amp;#039;&amp;#039;, &amp;#039;&amp;#039;msp430mcu&amp;#039;&amp;#039; and &amp;#039;&amp;#039;mspdebug&amp;#039;&amp;#039;. If you intend to code for multiple platforms, you should also install the following tool chains for other micro-controller: &amp;#039;&amp;#039;binutils-avr&amp;#039;&amp;#039;, &amp;#039;&amp;#039;gcc-avr&amp;#039;&amp;#039;, &amp;#039;&amp;#039;gdb-avr&amp;#039;&amp;#039;, &amp;#039;&amp;#039;avr-libc&amp;#039;&amp;#039; and &amp;#039;&amp;#039;avrdude&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
You can run the following command to install all packages for multiple platforms:&lt;br /&gt;
&lt;br /&gt;
  sudo apt-get install build-essential binutils-msp430 gcc-msp430 msp430-libc msp430mcu mspdebug binutils-avr gcc-avr gdb-avr avr-libc avrdude openjdk-8-jdk openjdk-8-jre ant libncurses5-dev&lt;br /&gt;
&lt;br /&gt;
When working with a 64-bit virtual machine, you may run into problems with the `serialdump-linux` executable because it may have been compiled for 32-bit machines. Install the following package to fix this issue.&lt;br /&gt;
&lt;br /&gt;
  sudo apt-get install lib32ncurses5&lt;br /&gt;
&lt;br /&gt;
You are done! You can now try to compile and run any example, such as [[Hello_World | Hello World]].&lt;br /&gt;
&lt;br /&gt;
=== Using Github repository ===&lt;br /&gt;
&lt;br /&gt;
This method also requires an existing installation of a Linux-based operating system. It is very similar to the previous installation method, but instead of downloading the source code you are going to &amp;#039;&amp;#039;clone&amp;#039;&amp;#039; it from the official Github repository. &lt;br /&gt;
&lt;br /&gt;
This method eases the update of your ContikiOS to the new releases.&lt;br /&gt;
&lt;br /&gt;
You need to &amp;#039;&amp;#039;clone&amp;#039;&amp;#039; the repository using the following command:&lt;br /&gt;
&lt;br /&gt;
  git clone git://github.com/contiki-os/contiki.git contiki&lt;br /&gt;
&lt;br /&gt;
After that you need to download all development tools as described in the previous installation method.&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
Edited by: Kwame, Pedro&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=MediaWiki:Common.js&amp;diff=1839</id>
		<title>MediaWiki:Common.js</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=MediaWiki:Common.js&amp;diff=1839"/>
				<updated>2016-10-05T00:36:24Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: Created page with &amp;quot;/* Any JavaScript here will be loaded for all users on every page load. */  jQuery(function($){ if ( mw.user.isAnon() ) { $(&amp;#039;#p-tb&amp;#039;).hide(); //id or class varies with skin } } );&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;/* Any JavaScript here will be loaded for all users on every page load. */&lt;br /&gt;
&lt;br /&gt;
jQuery(function($){ if ( mw.user.isAnon() ) {&lt;br /&gt;
$(&amp;#039;#p-tb&amp;#039;).hide(); //id or class varies with skin&lt;br /&gt;
} } );&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=Contiki_Shell&amp;diff=1833</id>
		<title>Contiki Shell</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=Contiki_Shell&amp;diff=1833"/>
				<updated>2016-09-28T04:24:32Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
The Contiki Shell is an interactive on-mote UNIX-­style shell that allows for text‐based interaction with a sensor node or a network of sensor nodes through a set of commands that can be executed on a UNIX like command line terminal. It has features such as piping data, run in background, file system interaction, network commands, sensor measurement commands and system commands. The shell can be accessed either over a serial USB connection or over a network using Telnet. &lt;br /&gt;
&lt;br /&gt;
The various instances of shells provided in Contiki are:&lt;br /&gt;
&amp;lt;p&amp;gt;1) &amp;#039;&amp;#039;&amp;#039;example-shell&amp;#039;&amp;#039;&amp;#039;: This shell can be compiled only for the native target, but not on Tmote Sky due to the firmware image size limitation on the Tmote Sky.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;2) &amp;#039;&amp;#039;&amp;#039;sky-shell&amp;#039;&amp;#039;&amp;#039;: This shell is a thinned out version, which can be compiled on Tmote Sky nodes, allowing the firmware image to fit the Tmote Sky memory.&amp;lt;/p&amp;gt; &lt;br /&gt;
&amp;lt;p&amp;gt;3) &amp;#039;&amp;#039;&amp;#039;sky-shell-exec&amp;#039;&amp;#039;&amp;#039;: This shell is a further thinned out version and has features such as the exec command allowing to load and execute ELF files.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;4) &amp;#039;&amp;#039;&amp;#039;sky-shell-webserver&amp;#039;&amp;#039;&amp;#039;: This is a shell that features the command sky-all-data, allowing to collect different sensor measurements and network   statistics from a Tmote Sky mote.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this tutorial, we will run the shell over a USB serial connection and will focus on &amp;#039;&amp;#039;&amp;#039;example-shell&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;sky-shell&amp;#039;&amp;#039;&amp;#039; provided in Contiki source.&lt;br /&gt;
&lt;br /&gt;
== Objective ==&lt;br /&gt;
&lt;br /&gt;
The goal of the tutorial is to familiarize the user with the various shells provided in the Contiki operating system.&lt;br /&gt;
&lt;br /&gt;
In this tutorial, we will learn how to install Contiki Shell, the various Contiki shell commands and their usage.&lt;br /&gt;
&lt;br /&gt;
== You will learn ==&lt;br /&gt;
&lt;br /&gt;
The tutorial gives detailed description on the following topics:&lt;br /&gt;
&amp;lt;p&amp;gt;1) How to get a Contiki Serial Shell up and running on Tmote sky nodes.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;2) Various Contiki Shell Commands, their usage and Contiki shell programming.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;3) How to create your own Shell Commands.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;4) How to create your own Shell for Contiki through an example project.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;5) You get to execute various fun examples and understand the Contiki shell along the way.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sounds exciting? Game for it ? Let&amp;#039;s begin then!&lt;br /&gt;
&lt;br /&gt;
== Source Code and Example Projects ==&lt;br /&gt;
&lt;br /&gt;
The source code and header files for Contiki Shell can be found in the following folder,&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039; ~/contiki-2.7/apps/shell &amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
The following path has the example-shell project,&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039; ~/contiki-2.7/examples/example-shell &amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
The sky-shell project can be found at, &lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;~/contiki-2.7/examples/sky-shell &amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== How to Compile and Install a Contiki shell ? ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt; &amp;#039;&amp;#039;&amp;#039; example-shell is a Comprehensive Shell and can be used for initial development. &amp;#039;&amp;#039;&amp;#039; &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Pros: It&amp;#039;s compiled on the native target, so you have the flexibility of developing your own shell commands and include it in the shell for further testing. Since, there&amp;#039;s no stringent memory constraints on native targets, this project is useful for initial development phase.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Cons: Since this shell has comprehensive set of shell commands, it increases the memory footprint of the firmware and hence can&amp;#039;t be compiled on Tmote Sky motes. &amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Navigate to this path to find the example-shell project : ~/contiki-2.7/examples/example-shell &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;example-shell.c&amp;#039;&amp;#039;&amp;#039; is the shell file in this project.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;*** Each shell_xx_init() adds a set of features to shell, hence if you run out of space while compiling try removing these. ***&amp;#039;&amp;#039;&amp;#039;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt; These shell_xx_init() are located in contiki-2.7/apps/shell/ in files like shell-filename.c. You can explore the source code to check for what features they come with and what&amp;#039;s in it, to make our lives simpler. &amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/*---------------------------------------------------------------------------*/&lt;br /&gt;
PROCESS(example_shell_process, &amp;quot;Contiki shell&amp;quot;);&lt;br /&gt;
AUTOSTART_PROCESSES(&amp;amp;example_shell_process);&lt;br /&gt;
/*---------------------------------------------------------------------------*/&lt;br /&gt;
PROCESS_THREAD(example_shell_process, ev, data)&lt;br /&gt;
{&lt;br /&gt;
  PROCESS_BEGIN();&lt;br /&gt;
&lt;br /&gt;
  serial_shell_init();&lt;br /&gt;
&lt;br /&gt;
  shell_base64_init();&lt;br /&gt;
  shell_blink_init();&lt;br /&gt;
  /*shell_checkpoint_init();*/&lt;br /&gt;
  /*shell_coffee_init();*/&lt;br /&gt;
  shell_download_init();&lt;br /&gt;
  /*shell_exec_init();*/&lt;br /&gt;
  shell_file_init();&lt;br /&gt;
  shell_httpd_init();&lt;br /&gt;
  shell_irc_init();&lt;br /&gt;
  shell_netfile_init();&lt;br /&gt;
  /*shell_ping_init();*/ /* uIP ping */&lt;br /&gt;
  shell_power_init();&lt;br /&gt;
  /*shell_profile_init();*/&lt;br /&gt;
  shell_ps_init();&lt;br /&gt;
  /*shell_reboot_init();*/&lt;br /&gt;
  shell_rime_debug_init();&lt;br /&gt;
  shell_rime_netcmd_init();&lt;br /&gt;
  shell_rime_ping_init(); /* Rime ping */&lt;br /&gt;
  shell_rime_sendcmd_init();&lt;br /&gt;
  shell_rime_sniff_init();&lt;br /&gt;
  shell_rime_init();&lt;br /&gt;
  /*shell_rsh_init();*/&lt;br /&gt;
  shell_run_init();&lt;br /&gt;
  shell_sendtest_init();&lt;br /&gt;
  /*shell_sky_init();*/&lt;br /&gt;
  shell_tcpsend_init();&lt;br /&gt;
  shell_text_init();&lt;br /&gt;
  shell_time_init();&lt;br /&gt;
  shell_udpsend_init();&lt;br /&gt;
  shell_vars_init();&lt;br /&gt;
  shell_wget_init();&lt;br /&gt;
&lt;br /&gt;
  PROCESS_END();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt; As we see above some of the shell_xx_init() are already commented out, this done on the pretext that these features are not supported on native target.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt; &amp;#039;&amp;#039;&amp;#039; Compiling and installing the Contiki shell on native target &amp;#039;&amp;#039;&amp;#039; &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;cd contiki-2.7/examples/example-shell &lt;br /&gt;
make  &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039; Run the Contiki Shell on native target &amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;./example-shell.native &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039; After running the Contiki Shell, you will see the Contiki Shell prompt &amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;user@instant-contiki:~/contiki-2.7/examples/example-shell$ ./example-shell.native &lt;br /&gt;
Contiki 2.7 started&lt;br /&gt;
Rime started with address 2.1&lt;br /&gt;
MAC nullmac RDC nullrdc NETWORK Rime&lt;br /&gt;
2.1: Contiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Punch in help on the Contiki Shell prompt to see all the available shell commands in Contiki shell.&lt;br /&gt;
&amp;lt;pre&amp;gt;help&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;You can now run any supported shell commands&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;For example, running &amp;#039;&amp;#039;&amp;#039;echo hello&amp;#039;&amp;#039;&amp;#039; will echo the &amp;lt;text&amp;gt; back on the shell prompt. &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;2.1: Contiki&amp;gt; &lt;br /&gt;
echo hello&lt;br /&gt;
hello&lt;br /&gt;
2.1: Contiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
----------------------------------------------------------------------------------------------------------------------------------------&lt;br /&gt;
&amp;lt;p&amp;gt; &amp;#039;&amp;#039;&amp;#039;sky-shell is a thinned out version of Contiki Shell for Tmote Sky motes. &amp;#039;&amp;#039;&amp;#039; &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Navigate to this path to find the sky-shell project : ~/contiki-2.7/examples/sky-shell &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;sky-shell.c&amp;#039;&amp;#039;&amp;#039; is the shell file in this project&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;*** Each shell_xx_init() adds a set of features to shell, hence if you run out of space on the mote try removing these. ***&amp;#039;&amp;#039;&amp;#039;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/*---------------------------------------------------------------------------*/&lt;br /&gt;
PROCESS(sky_shell_process, &amp;quot;Sky Contiki shell&amp;quot;);&lt;br /&gt;
AUTOSTART_PROCESSES(&amp;amp;sky_shell_process);&lt;br /&gt;
/*---------------------------------------------------------------------------*/&lt;br /&gt;
#define WITH_PERIODIC_DEBUG 0&lt;br /&gt;
#if WITH_PERIODIC_DEBUG&lt;br /&gt;
static struct ctimer debug_timer;&lt;br /&gt;
static void&lt;br /&gt;
periodic_debug(void *ptr)&lt;br /&gt;
{&lt;br /&gt;
  ctimer_set(&amp;amp;debug_timer, 20 * CLOCK_SECOND, periodic_debug, NULL);&lt;br /&gt;
  collect_print_stats();&lt;br /&gt;
}&lt;br /&gt;
#endif /* WITH_PERIODIC_DEBUG */&lt;br /&gt;
/*---------------------------------------------------------------------------*/&lt;br /&gt;
PROCESS_THREAD(sky_shell_process, ev, data)&lt;br /&gt;
{&lt;br /&gt;
  PROCESS_BEGIN();&lt;br /&gt;
&lt;br /&gt;
#if WITH_PERIODIC_DEBUG&lt;br /&gt;
  ctimer_set(&amp;amp;debug_timer, 20 * CLOCK_SECOND, periodic_debug, NULL);&lt;br /&gt;
#endif /* WITH_PERIODIC_DEBUG */&lt;br /&gt;
&lt;br /&gt;
  serial_shell_init();&lt;br /&gt;
  shell_blink_init();&lt;br /&gt;
  /*  shell_file_init();&lt;br /&gt;
      shell_coffee_init();*/&lt;br /&gt;
  /*  shell_download_init();&lt;br /&gt;
      shell_rime_sendcmd_init();*/&lt;br /&gt;
  /*  shell_ps_init();*/&lt;br /&gt;
  shell_reboot_init();&lt;br /&gt;
  shell_rime_init();&lt;br /&gt;
  shell_rime_netcmd_init();&lt;br /&gt;
  /*  shell_rime_ping_init();&lt;br /&gt;
  shell_rime_debug_init();&lt;br /&gt;
  shell_rime_debug_runicast_init();*/&lt;br /&gt;
  /*  shell_rime_sniff_init();*/&lt;br /&gt;
  shell_sky_init();&lt;br /&gt;
  shell_power_init();&lt;br /&gt;
  shell_powertrace_init();&lt;br /&gt;
  /*  shell_base64_init();*/&lt;br /&gt;
  shell_text_init();&lt;br /&gt;
  shell_time_init();&lt;br /&gt;
  /*  shell_checkpoint_init();*/&lt;br /&gt;
  /*  shell_sendtest_init();*/&lt;br /&gt;
  shell_rime_unicast_init();&lt;br /&gt;
  shell_collect_view_init();&lt;br /&gt;
&lt;br /&gt;
#if DEBUG_SNIFFERS&lt;br /&gt;
  rime_sniffer_add(&amp;amp;s);&lt;br /&gt;
#endif /* DEBUG_SNIFFERS */&lt;br /&gt;
  &lt;br /&gt;
  PROCESS_END();&lt;br /&gt;
}&lt;br /&gt;
/*---------------------------------------------------------------------------*/&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt; As we see above some of the shell_xx_init() are already commented out, this done on the pretext that these features if supported will overflow the Tmote Sky memory and the Code will not compile.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt; Hence, commented out shell_xx_init() which corresponds to specific set of shell commands, will not be available in this shell &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039; However, if we require any shell_xx_init() features, we can uncomment them but we need to make sure the code compiles such that the sky mote memory doesn&amp;#039;t overflow. serial_shell_init() invokes the serial contiki shell&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt; &amp;#039;&amp;#039;&amp;#039; Compiling and uploading the Contiki Sky Shell on sky motes &amp;#039;&amp;#039;&amp;#039; &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;To install a shell on the a node, go to the sky-shell, connect a TMote sky to your computer and upload the shell application on the node.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;cd contiki-2.7/examples/sky-shell &lt;br /&gt;
make TARGET=sky sky-shell.upload savetarget &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Wait for the compilation and uploading to finish.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;To connect to the shell over the USB port, run: &amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039; You can now login to the node with the command &amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;make login &amp;lt;/pre&amp;gt; &amp;lt;p&amp;gt;Press the return key to get a shell prompt.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039; After running the Contiki Shell, you will see the Contiki Sky Shell prompt &amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;user@instant-contiki:~/contiki-2.7/examples/sky-shell$ make login&lt;br /&gt;
using saved target &amp;#039;sky&amp;#039;&lt;br /&gt;
../../tools/sky/serialdump-linux -b115200 /dev/ttyUSB0&lt;br /&gt;
connecting to /dev/ttyUSB0 (115200) [OK]&lt;br /&gt;
&lt;br /&gt;
SEND 1 bytes&lt;br /&gt;
5.0: Contiki&amp;gt; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039; Punch in help on the Contiki Shell prompt to see all the available shell commands in Contiki shell. &amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&amp;lt;pre&amp;gt;help&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;You can now run any supported shell commands&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;For example, running &amp;#039;&amp;#039;&amp;#039;echo hello&amp;#039;&amp;#039;&amp;#039; will echo the &amp;lt;text&amp;gt; back on the shell prompt. &amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;5.0: Contiki&amp;gt; &lt;br /&gt;
echo hello&lt;br /&gt;
SEND 11 bytes&lt;br /&gt;
hello&lt;br /&gt;
5.0: Contiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Modify sky-shell project for this tutorial ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Replace the sky-shell.c file with the following version of sky-shell.c file&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt; Please copy the below source code into the sky-shell.c file &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 2008, Swedish Institute of Computer Science.&lt;br /&gt;
 * All rights reserved.&lt;br /&gt;
 *&lt;br /&gt;
 * Redistribution and use in source and binary forms, with or without&lt;br /&gt;
 * modification, are permitted provided that the following conditions&lt;br /&gt;
 * are met:&lt;br /&gt;
 * 1. Redistributions of source code must retain the above copyright&lt;br /&gt;
 *    notice, this list of conditions and the following disclaimer.&lt;br /&gt;
 * 2. Redistributions in binary form must reproduce the above copyright&lt;br /&gt;
 *    notice, this list of conditions and the following disclaimer in the&lt;br /&gt;
 *    documentation and/or other materials provided with the distribution.&lt;br /&gt;
 * 3. Neither the name of the Institute nor the names of its contributors&lt;br /&gt;
 *    may be used to endorse or promote products derived from this software&lt;br /&gt;
 *    without specific prior written permission.&lt;br /&gt;
 *&lt;br /&gt;
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS&amp;#039;&amp;#039; AND&lt;br /&gt;
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE&lt;br /&gt;
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE&lt;br /&gt;
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE&lt;br /&gt;
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL&lt;br /&gt;
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS&lt;br /&gt;
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)&lt;br /&gt;
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT&lt;br /&gt;
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY&lt;br /&gt;
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF&lt;br /&gt;
 * SUCH DAMAGE.&lt;br /&gt;
 *&lt;br /&gt;
 * This file is part of the Contiki operating system.&lt;br /&gt;
 *&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * \file&lt;br /&gt;
 *         Tmote Sky-specific Contiki shell&lt;br /&gt;
 * \author&lt;br /&gt;
 *         Adam Dunkels &amp;lt;adam@sics.se&amp;gt;&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;contiki.h&amp;quot;&lt;br /&gt;
#include &amp;quot;shell.h&amp;quot;&lt;br /&gt;
#include &amp;quot;serial-shell.h&amp;quot;&lt;br /&gt;
#include &amp;quot;collect-view.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;net/rime/rime.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
/*---------------------------------------------------------------------------*/&lt;br /&gt;
PROCESS(sky_shell_process, &amp;quot;Sky Contiki shell&amp;quot;);&lt;br /&gt;
AUTOSTART_PROCESSES(&amp;amp;sky_shell_process);&lt;br /&gt;
/*---------------------------------------------------------------------------*/&lt;br /&gt;
#define WITH_PERIODIC_DEBUG 0&lt;br /&gt;
#if WITH_PERIODIC_DEBUG&lt;br /&gt;
static struct ctimer debug_timer;&lt;br /&gt;
static void&lt;br /&gt;
periodic_debug(void *ptr)&lt;br /&gt;
{&lt;br /&gt;
  ctimer_set(&amp;amp;debug_timer, 20 * CLOCK_SECOND, periodic_debug, NULL);&lt;br /&gt;
  collect_print_stats();&lt;br /&gt;
}&lt;br /&gt;
#endif /* WITH_PERIODIC_DEBUG */&lt;br /&gt;
/*---------------------------------------------------------------------------*/&lt;br /&gt;
PROCESS_THREAD(sky_shell_process, ev, data)&lt;br /&gt;
{&lt;br /&gt;
  PROCESS_BEGIN();&lt;br /&gt;
&lt;br /&gt;
#if WITH_PERIODIC_DEBUG&lt;br /&gt;
  ctimer_set(&amp;amp;debug_timer, 20 * CLOCK_SECOND, periodic_debug, NULL);&lt;br /&gt;
#endif /* WITH_PERIODIC_DEBUG */&lt;br /&gt;
&lt;br /&gt;
  serial_shell_init();&lt;br /&gt;
  shell_blink_init();&lt;br /&gt;
  shell_file_init();&lt;br /&gt;
&lt;br /&gt;
  shell_ps_init();&lt;br /&gt;
  shell_reboot_init();&lt;br /&gt;
&lt;br /&gt;
  shell_sky_init();&lt;br /&gt;
  shell_power_init();&lt;br /&gt;
  shell_powertrace_init();&lt;br /&gt;
&lt;br /&gt;
  shell_text_init();&lt;br /&gt;
  shell_time_init();&lt;br /&gt;
&lt;br /&gt;
  shell_collect_view_init();&lt;br /&gt;
&lt;br /&gt;
#if DEBUG_SNIFFERS&lt;br /&gt;
  rime_sniffer_add(&amp;amp;s);&lt;br /&gt;
#endif /* DEBUG_SNIFFERS */&lt;br /&gt;
  &lt;br /&gt;
  PROCESS_END();&lt;br /&gt;
}&lt;br /&gt;
/*---------------------------------------------------------------------------*/&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This will create a sky shell with features or shell commands that we will go through in the next section&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Please compile the sky-shell project and upload the sky-shell.upload to the Sky mote and Login to the mote as explained in previous section&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;If successful you should be able to see the following and get a Contiki shell prompt&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;user@instant-contiki:~/contiki-2.7/examples/sky-shell$ make TARGET=sky sky-shell.upload savetarget&lt;br /&gt;
  AR        contiki-sky.a&lt;br /&gt;
  CC        sky-shell.c&lt;br /&gt;
  CC        ../../platform/sky/./contiki-sky-main.c&lt;br /&gt;
  LD        sky-shell.sky&lt;br /&gt;
msp430-objcopy sky-shell.sky -O ihex sky-shell.ihex&lt;br /&gt;
make IHEXFILE=sky-shell.ihex sky-reset sky-upload&lt;br /&gt;
make[1]: Entering directory `/home/user/contiki-2.7/examples/sky-shell&amp;#039;&lt;br /&gt;
make -k -j 20 sky-reset-sequence&lt;br /&gt;
make[2]: Entering directory `/home/user/contiki-2.7/examples/sky-shell&amp;#039;&lt;br /&gt;
../../tools/sky/msp430-bsl-linux --telosb -c /dev/ttyUSB0 -r&lt;br /&gt;
MSP430 Bootstrap Loader Version: 1.39-telos-7&lt;br /&gt;
Use -h for help&lt;br /&gt;
Reset device ...&lt;br /&gt;
Done&lt;br /&gt;
make[2]: Leaving directory `/home/user/contiki-2.7/examples/sky-shell&amp;#039;&lt;br /&gt;
make -j 20 sky-upload-sequence&lt;br /&gt;
make[2]: Entering directory `/home/user/contiki-2.7/examples/sky-shell&amp;#039;&lt;br /&gt;
+++++ Erasing /dev/ttyUSB0&lt;br /&gt;
MSP430 Bootstrap Loader Version: 1.39-telos-7&lt;br /&gt;
Use -h for help&lt;br /&gt;
Mass Erase...&lt;br /&gt;
Transmit default password ...&lt;br /&gt;
+++++ Programming /dev/ttyUSB0&lt;br /&gt;
MSP430 Bootstrap Loader Version: 1.39-telos-7&lt;br /&gt;
Invoking BSL...&lt;br /&gt;
Transmit default password ...&lt;br /&gt;
Current bootstrap loader version: 1.61 (Device ID: f16c)&lt;br /&gt;
Changing baudrate to 38400 ...&lt;br /&gt;
Program ...&lt;br /&gt;
45722 bytes programmed.&lt;br /&gt;
+++++ Resetting /dev/ttyUSB0&lt;br /&gt;
MSP430 Bootstrap Loader Version: 1.39-telos-7&lt;br /&gt;
Use -h for help&lt;br /&gt;
Reset device ...&lt;br /&gt;
Done&lt;br /&gt;
make[2]: Leaving directory `/home/user/contiki-2.7/examples/sky-shell&amp;#039;&lt;br /&gt;
make[1]: Leaving directory `/home/user/contiki-2.7/examples/sky-shell&amp;#039;&lt;br /&gt;
saving Makefile.target&lt;br /&gt;
rm sky-shell.co obj_sky/contiki-sky-main.o sky-shell.ihex&lt;br /&gt;
user@instant-contiki:~/contiki-2.7/examples/sky-shell$ make login&lt;br /&gt;
using saved target &amp;#039;sky&amp;#039;&lt;br /&gt;
../../tools/sky/serialdump-linux -b115200 /dev/ttyUSB0&lt;br /&gt;
connecting to /dev/ttyUSB0 (115200) [OK]&lt;br /&gt;
+�{/#+�3++�&amp;amp;�&lt;br /&gt;
SEND 1 bytes&lt;br /&gt;
5.0: Contiki&amp;gt; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;If you don&amp;#039;t end up getting the above set of messages on the comand prompt and end up getting the set of messages on command prompt as shown below, then your setup has not been successful&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;user@instant-contiki:~/contiki-2.7/examples/sky-shell$ make TARGET=sky sky-shell.upload savetarget&lt;br /&gt;
msp430-objcopy sky-shell.sky -O ihex sky-shell.ihex&lt;br /&gt;
make IHEXFILE=sky-shell.ihex sky-reset sky-upload&lt;br /&gt;
make[1]: Entering directory `/home/user/contiki-2.7/examples/sky-shell&amp;#039;&lt;br /&gt;
make -k -j 20 sky-reset-sequence&lt;br /&gt;
make[2]: Entering directory `/home/user/contiki-2.7/examples/sky-shell&amp;#039;&lt;br /&gt;
Done&lt;br /&gt;
make[2]: Leaving directory `/home/user/contiki-2.7/examples/sky-shell&amp;#039;&lt;br /&gt;
make -j 20 sky-upload-sequence&lt;br /&gt;
make[2]: Entering directory `/home/user/contiki-2.7/examples/sky-shell&amp;#039;&lt;br /&gt;
Done&lt;br /&gt;
make[2]: Leaving directory `/home/user/contiki-2.7/examples/sky-shell&amp;#039;&lt;br /&gt;
make[1]: Leaving directory `/home/user/contiki-2.7/examples/sky-shell&amp;#039;&lt;br /&gt;
saving Makefile.target&lt;br /&gt;
rm sky-shell.ihex&lt;br /&gt;
user@instant-contiki:~/contiki-2.7/examples/sky-shell$&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;This indicates that your Tmote Sky mote is disconnected!!!&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Make sure that your Tmote Sky mote is connected and try compiling again and it should work this time :) Did you heave a sigh there?&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;I did! When I figured out what was going wrong.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Nevertheless, we are All Set and Good to Launch now :) :)&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
== Contiki Shell Commands, their usage and Contiki shell programming ==&lt;br /&gt;
Ready to dive in ?&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Everyone needs help at some point in time, don&amp;#039;t we? So let&amp;#039;s punch in help on the Contiki Sky Shell to figure out what all shell commands are at our disposal and we will take them head on, one at a time :) &amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;You should see a list of all available shell commands as shown below:&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;5.0: Contiki&amp;gt; &lt;br /&gt;
help&lt;br /&gt;
SEND 5 bytes&lt;br /&gt;
Available commands:&lt;br /&gt;
?: shows this help&lt;br /&gt;
append &amp;lt;filename&amp;gt;: append to file&lt;br /&gt;
binprint: print binary data in decimal format&lt;br /&gt;
blink [num]: blink LEDs ([num] times)&lt;br /&gt;
collect-view-data: sensor data, power consumption, network stats&lt;br /&gt;
echo &amp;lt;text&amp;gt;: print &amp;lt;text&amp;gt;&lt;br /&gt;
energy: print energy profile&lt;br /&gt;
exit: exit shell&lt;br /&gt;
hd: print binary data in hexadecimal format&lt;br /&gt;
help: shows this help&lt;br /&gt;
kill &amp;lt;command&amp;gt;: stop a specific command&lt;br /&gt;
killall: stop all running commands&lt;br /&gt;
ls: list files&lt;br /&gt;
nodeid: set node ID&lt;br /&gt;
null: discard input&lt;br /&gt;
power: print power profile&lt;br /&gt;
powerconv: convert power profile to human readable output&lt;br /&gt;
powertrace [interval]: turn powertracing on or off, with reporting interval &amp;lt;interval&amp;gt;&lt;br /&gt;
ps: list all running processes&lt;br /&gt;
quit: exit shell&lt;br /&gt;
randwait &amp;lt;maxtime&amp;gt; &amp;lt;command&amp;gt;: wait for a random time before running a command&lt;br /&gt;
read &amp;lt;filename&amp;gt; [offset] [block size]: read from a file, with the offset and the block size as options&lt;br /&gt;
reboot: reboot the system&lt;br /&gt;
repeat &amp;lt;num&amp;gt; &amp;lt;time&amp;gt; &amp;lt;command&amp;gt;: run a command every &amp;lt;time&amp;gt; seconds&lt;br /&gt;
rfchannel &amp;lt;channel&amp;gt;: change CC2420 radio channel (11 - 26)&lt;br /&gt;
rm &amp;lt;filename&amp;gt;: remove the file named filename&lt;br /&gt;
sense: print out sensor data&lt;br /&gt;
senseconv: convert &amp;#039;sense&amp;#039; data to human readable format&lt;br /&gt;
size: print the size of the input&lt;br /&gt;
time [seconds]: output time in binary format, or set time in seconds since 1970&lt;br /&gt;
timestamp: prepend a timestamp to data&lt;br /&gt;
txpower &amp;lt;power&amp;gt;: change CC2420 transmission power (0 - 31)&lt;br /&gt;
write &amp;lt;filename&amp;gt;: write to file&lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Lets, go ahead and do the simplest first! echo :)&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;5.0: Contiki&amp;gt; &lt;br /&gt;
echo howdy contiki shell?&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 6 bytes&lt;br /&gt;
howdy contiki shell?&lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;Contiki Shell Commands&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;1) Text Related Commands:&amp;#039;&amp;#039;&amp;#039;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;echo &amp;lt;text&amp;gt;&amp;#039;&amp;#039;&amp;#039;: print &amp;lt;text&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;binprint&amp;#039;&amp;#039;&amp;#039;: print binary data in decimal format&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;hd&amp;#039;&amp;#039;&amp;#039;: print binary data in hexadecimal format&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;size&amp;#039;&amp;#039;&amp;#039;: print the size of the input&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;shell-text.c&amp;#039;&amp;#039;&amp;#039; : This file holds all the text related shell commands. You can find the source file in this path ~/contiki-2.7/apps/shell&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;shell_text_init()&amp;#039;&amp;#039;&amp;#039;: This function is used to register text related shell commands with the Contiki Shell.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Examples&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
------------------------------------------&lt;br /&gt;
&amp;lt;p&amp;gt;echo carpe diem!&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;5.0: Contiki&amp;gt; &lt;br /&gt;
echo carpe diem!&lt;br /&gt;
SEND 17 bytes&lt;br /&gt;
carpe diem!&lt;br /&gt;
5.0: Contiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
------------------------------------------&lt;br /&gt;
&amp;lt;p&amp;gt;The following commands prints time data in decimal format.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;2943 secs have elapsed since the mote was last power up.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;time | binprint &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;5.0: Contiki&amp;gt; &lt;br /&gt;
time | binprint&lt;br /&gt;
SEND 16 bytes&lt;br /&gt;
6 49032 34883 0 65535 0 2943 &lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
------------------------------------------&lt;br /&gt;
&amp;lt;p&amp;gt;The following command prints data in hexadecimal format.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;hd&amp;lt;/p&amp;gt;&lt;br /&gt;
Press Cntrl+Return&lt;br /&gt;
&amp;lt;p&amp;gt;hello world&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Press Cntrl+Return to get the shell prompt back after output is printed.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;5.0: Contiki&amp;gt; &lt;br /&gt;
hd&lt;br /&gt;
SEND 3 bytes&lt;br /&gt;
hello world&lt;br /&gt;
SEND 12 bytes&lt;br /&gt;
0x6568 0x6c6c 0x206f 0x6f77 0x6c72 &amp;lt;/pre&amp;gt;&lt;br /&gt;
------------------------------------------&lt;br /&gt;
&amp;lt;p&amp;gt;The following command prints size of the data.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;size&amp;lt;/p&amp;gt;&lt;br /&gt;
Press Cntrl+Return&lt;br /&gt;
&amp;lt;p&amp;gt;trojan for life!&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Press Cntrl+Return to get the shell prompt back after output is printed.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;5.0: Contiki&amp;gt; &lt;br /&gt;
size&lt;br /&gt;
SEND 5 bytes&lt;br /&gt;
trojan for life!&lt;br /&gt;
SEND 17 bytes&lt;br /&gt;
&lt;br /&gt;
SEND 1 bytes&lt;br /&gt;
16&lt;br /&gt;
5.0: Contiki&amp;gt; &amp;lt;/pre&amp;gt;&lt;br /&gt;
------------------------------------------&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;2) Time related Shell commands&amp;#039;&amp;#039;&amp;#039;&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;time [seconds]&amp;#039;&amp;#039;&amp;#039;: output time in binary format, or set time in seconds since 1970.&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;repeat &amp;lt;num&amp;gt; &amp;lt;time&amp;gt; &amp;lt;command&amp;gt;&amp;#039;&amp;#039;&amp;#039;: run a command every time seconds.&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;randwait &amp;lt;maxtime&amp;gt; &amp;lt;command&amp;gt;&amp;#039;&amp;#039;&amp;#039;: wait for a random time before running a command.&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;timestamp&amp;#039;&amp;#039;&amp;#039;: prepend a timestamp to data.&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;shell-time.c&amp;#039;&amp;#039;&amp;#039; : This file holds all the time related shell commands. You can find the source file in this path ~/contiki-2.7/apps/shell&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;shell_time_init()&amp;#039;&amp;#039;&amp;#039;: This function is used to register time related shell commands with the Contiki Shell.&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Examples&amp;#039;&amp;#039;&amp;#039;&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
-------------------------------------------&lt;br /&gt;
&amp;lt;p&amp;gt;Following command will set the current time on mote to 10 secs after 1970.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;That it forces the time to be set to a value given.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;format of print : [len] [clock] [rtimer] [timesynch] [timesynch] [authority] time[1] time[0]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;time 10 | binprint &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;5.0: Contiki&amp;gt; &lt;br /&gt;
time 10 | binprint&lt;br /&gt;
SEND 19 bytes&lt;br /&gt;
6 59950 11844 0 65535 0 10 &lt;br /&gt;
5.0: Contiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
--------------------------------------------&lt;br /&gt;
&amp;lt;p&amp;gt;Following command will set the current time on mote to 66000 secs after 1970.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Since 66000 &amp;gt; 65536 , time[0] overflows and time[1] has the order bits &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;time 66000 | binprint &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;5.0: Contiki&amp;gt; &lt;br /&gt;
time 66000 | binprint&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 2 bytes&lt;br /&gt;
6 39554 33340 0 65535 1 464 &lt;br /&gt;
5.0: Contiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
----------------------------------------------&lt;br /&gt;
&amp;lt;p&amp;gt;Following command will print time elapsed every one sec, after waiting for randomtime from the time was reset to 0 secs.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;time 0 | randwait 10 {repeat 10 1 {time | binprint}}&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Press Cntrl+Return to get the shell prompt back after output is printed.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;5.0: Contiki&amp;gt; &lt;br /&gt;
time 0 | randwait 10 {repeat 10 1 {time | binprint}}    &lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 13 bytes&lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
6 4870 1641 0 65535 0 9 &lt;br /&gt;
6 4998 34372 0 65535 0 10 &lt;br /&gt;
6 5126 1604 0 65535 0 11 &lt;br /&gt;
6 5254 34372 0 65535 0 12 &lt;br /&gt;
6 5382 1605 0 65535 0 13 &lt;br /&gt;
6 5510 34372 0 65535 0 14 &lt;br /&gt;
6 5638 1604 0 65535 0 15 &lt;br /&gt;
6 5766 34372 0 65535 0 16 &lt;br /&gt;
6 5894 1604 0 65535 0 17 &lt;br /&gt;
6 6022 34372 0 65535 0 18&amp;lt;/pre&amp;gt;&lt;br /&gt;
---------------------------------------------&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Following command will print the time elapsed every sec for 100 secs&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;time | repeat 20 1 {time | binprint}&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Press Cntrl+Return to get the shell prompt back after output is printed.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;5.0: Contiki&amp;gt; &lt;br /&gt;
time | repeat 20 1 {time | binprint}&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 17 bytes&lt;br /&gt;
�z�^z���&lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
6 35963 31502 0 65535 0 251 &lt;br /&gt;
6 36090 64068 0 65535 0 252 &lt;br /&gt;
6 36218 31300 0 65535 0 253 &lt;br /&gt;
6 36346 64068 0 65535 0 254 &lt;br /&gt;
6 36474 31300 0 65535 0 255 &lt;br /&gt;
6 36602 64068 0 65535 0 256 &lt;br /&gt;
6 36730 31300 0 65535 0 257 &lt;br /&gt;
6 36858 64068 0 65535 0 258 &lt;br /&gt;
6 36986 31300 0 65535 0 259 &lt;br /&gt;
6 37114 64068 0 65535 0 260 &lt;br /&gt;
6 37242 31300 0 65535 0 261 &lt;br /&gt;
6 37370 64068 0 65535 0 262 &lt;br /&gt;
6 37498 31300 0 65535 0 263 &lt;br /&gt;
6 37626 64068 0 65535 0 264 &lt;br /&gt;
6 37754 31300 0 65535 0 265 &lt;br /&gt;
6 37882 64068 0 65535 0 266 &lt;br /&gt;
6 38010 31300 0 65535 0 267 &lt;br /&gt;
6 38138 64068 0 65535 0 268 &lt;br /&gt;
6 38266 31300 0 65535 0 269 &lt;br /&gt;
6 38394 64068 0 65535 0 270&amp;lt;/pre&amp;gt;&lt;br /&gt;
---------------------------------------------&lt;br /&gt;
&amp;lt;p&amp;gt;prints hello 4 times every 1 time step and immediately gets back to prompt and runs the command in the background&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;repeat 4 1 {echo hello} &amp;amp;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Press Cntrl+Return to get the shell prompt back after output is printed.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;5.0: Contiki&amp;gt; &lt;br /&gt;
repeat 4 1 {echo hello} &amp;amp;&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 6 bytes&lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
hello&lt;br /&gt;
hello&lt;br /&gt;
hello&lt;br /&gt;
hello&lt;br /&gt;
&lt;br /&gt;
SEND 1 bytes&lt;br /&gt;
5.0: Contiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
---------------------------------------------&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;3) File System Related Commands:&amp;#039;&amp;#039;&amp;#039;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;ls&amp;#039;&amp;#039;&amp;#039;: list files&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;write &amp;lt;filename&amp;gt;&amp;#039;&amp;#039;&amp;#039;: write to file. To write a string of character to a file.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;read &amp;lt;filename&amp;gt; [offset] [block size]&amp;#039;&amp;#039;&amp;#039;: read from a file, with the offset and the block size as options. The offset is the number of characters that must be skipped before reading the file, starting from 0. The block size option separates the output of the read functions in blocks of size block size. &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;append &amp;lt;filename&amp;gt;&amp;#039;&amp;#039;&amp;#039;: append to file.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;rm &amp;lt;filename&amp;gt;&amp;#039;&amp;#039;&amp;#039;: remove the file named filename.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;shell-file.c&amp;#039;&amp;#039;&amp;#039; : This file holds all the file system related shell commands. You can find the source file in this path ~/contiki-2.7/apps/shell&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;shell_file_init()&amp;#039;&amp;#039;&amp;#039;: This function is used to register file system related shell commands with the Contiki Shell.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;Examples&amp;#039;&amp;#039;&amp;#039;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
---------------------------------------------&lt;br /&gt;
&amp;lt;p&amp;gt; Following command echos the text and is written to a file &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;echo howdy? | write msg.txt&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;echo hi! | write hi.txt&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;read msg.txt 0 6 &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;ls&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;5.0: Contiki&amp;gt; &lt;br /&gt;
echo howdy? | write msg.txt&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 8 bytes&lt;br /&gt;
howdy?&lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
echo hi! | write hi.txt&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 4 bytes&lt;br /&gt;
hi!&lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
read msg.txt 0 6 &lt;br /&gt;
SEND 18 bytes&lt;br /&gt;
howdy?&lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
ls&lt;br /&gt;
SEND 3 bytes&lt;br /&gt;
58 &lt;br /&gt;
6 msg.txt&lt;br /&gt;
3 hi.txt&lt;br /&gt;
Total size: 67&lt;br /&gt;
5.0: Contiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
----------------------------------------------&lt;br /&gt;
&amp;lt;p&amp;gt;The following set of commands repeats hey text 4 times every time step and appends the text into a file. Removes all the existing files later &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;repeat 4 1 {echo hey | append logfile.txt}&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;read logfile.txt 0 3&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;ls&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;rm msg.txt&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;rm hi.txt&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;rm logfile.txt&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;ls&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;5.0: Contiki&amp;gt; &lt;br /&gt;
repeat 4 1 {echo hey | append logfile.txt}&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 3 bytes&lt;br /&gt;
hey&lt;br /&gt;
hey&lt;br /&gt;
hey&lt;br /&gt;
hey&lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
read logfile.txt 0 3&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 1 bytes&lt;br /&gt;
hey&lt;br /&gt;
hey&lt;br /&gt;
hey&lt;br /&gt;
hey&lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
ls&lt;br /&gt;
SEND 3 bytes&lt;br /&gt;
58 &lt;br /&gt;
6 msg.txt&lt;br /&gt;
3 hi.txt&lt;br /&gt;
12 logfile.txt&lt;br /&gt;
Total size: 79&lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
rm msg.txt&lt;br /&gt;
SEND 11 bytes&lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
rm hi.txt&lt;br /&gt;
SEND 10 bytes&lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
rm logfile.txt&lt;br /&gt;
SEND 15 bytes&lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
ls&lt;br /&gt;
SEND 3 bytes&lt;br /&gt;
58 &lt;br /&gt;
Total size: 58&lt;br /&gt;
5.0: Contiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
----------------------------------------------&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;4) Sky mote Related Commands:&amp;#039;&amp;#039;&amp;#039;&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;sense&amp;#039;&amp;#039;&amp;#039;: print out sensor data.&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;senseconv&amp;#039;&amp;#039;&amp;#039;: convert &amp;#039;sense&amp;#039; data to human readable format.&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;nodeid&amp;#039;&amp;#039;&amp;#039;: set node ID, If no node ID was given on the command line, we print out the current nodeid or else we burn the new node ID.&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;txpower &amp;lt;power&amp;gt;&amp;#039;&amp;#039;&amp;#039;: change CC2420 transmission power (0 - 31).If no power was given on the command line, we print out the current power.&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;rfchannel &amp;lt;channel&amp;gt;&amp;#039;&amp;#039;&amp;#039;: change CC2420 radio channel (11 - 26).If no channel was given on the command line, we print out thecurrent channel.&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;shell-sky.c&amp;#039;&amp;#039;&amp;#039; : This file holds all the sky mote related shell commands. You can find the source file in this path ~/contiki-2.7/apps/shell.&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;shell_sky_init()&amp;#039;&amp;#039;&amp;#039;: This function is used to register sky mote related shell commands with the Contiki Shell.&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Examples&amp;#039;&amp;#039;&amp;#039;&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
---------------------------------------------&lt;br /&gt;
&amp;lt;p&amp;gt; This set of commands prints the sensor data in human readable format&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;sense|senseconv&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;5.0: Contiki&amp;gt; &lt;br /&gt;
sense|senseconv&lt;br /&gt;
SEND 16 bytes&lt;br /&gt;
Light 1 4&lt;br /&gt;
Light 2 13&lt;br /&gt;
Temperature 23.2&lt;br /&gt;
Relative humidity 79 &lt;br /&gt;
RSSI 8&lt;br /&gt;
Voltage 2.5&lt;br /&gt;
5.0: Contiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
---------------------------------------------&lt;br /&gt;
&amp;lt;p&amp;gt; Following command prints out the txpower as 5, which is being set &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Print format: msglen power&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;txpower 5 | binprint&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;5.0: Contiki&amp;gt; &lt;br /&gt;
txpower 5 | binprint&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 1 bytes&lt;br /&gt;
1 5 &lt;br /&gt;
5.0: Contiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
---------------------------------------------&lt;br /&gt;
&amp;lt;p&amp;gt; Following command prints out the rfchannel as 15, which is being set &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Print format: msglen channelno&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;rfchannel 15| binprint&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;5.0: Contiki&amp;gt; &lt;br /&gt;
rfchannel 15| binprint&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 3 bytes&lt;br /&gt;
1 15 &lt;br /&gt;
5.0: Contiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
---------------------------------------------&lt;br /&gt;
&amp;lt;p&amp;gt;Following command outputs the sensor measurements every time step 4 times and appends to a file &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;repeat 4 1 {sense | senseconv | append logfile.txt}&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;read logfile.txt 0 36&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;5.0: Contiki&amp;gt; &lt;br /&gt;
repeat 4 1 {sense | senseconv | append logfile.txt}&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 12 bytes&lt;br /&gt;
Light 1 5&lt;br /&gt;
Light 2 13&lt;br /&gt;
Temperature 23.2&lt;br /&gt;
Relative humidity 79&lt;br /&gt;
RSSI 11&lt;br /&gt;
Voltage 2.5&lt;br /&gt;
Light 1 4&lt;br /&gt;
Light 2 13&lt;br /&gt;
Temperature 23.2&lt;br /&gt;
Relative humidity 79&lt;br /&gt;
RSSI 8&lt;br /&gt;
Voltage 2.5&lt;br /&gt;
Light 1 4&lt;br /&gt;
Light 2 13&lt;br /&gt;
Temperature 23.2&lt;br /&gt;
Relative humidity 79&lt;br /&gt;
RSSI 2&lt;br /&gt;
Voltage 2.5&lt;br /&gt;
Light 1 4&lt;br /&gt;
Light 2 13&lt;br /&gt;
Temperature 23.2&lt;br /&gt;
Relative humidity 79&lt;br /&gt;
RSSI 0&lt;br /&gt;
Voltage 2.5&lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
read logfile.txt 0 36&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 2 bytes&lt;br /&gt;
Light 1 5Light 2 13Temperature 23.2&lt;br /&gt;
Relative humidity 79RSSI 11Voltage&lt;br /&gt;
 2.5Light 1 4Light 2 13Temperature 23.2&lt;br /&gt;
Relative humidity 79RSSI 8Volt&lt;br /&gt;
age 2.5Light 1 4Light 2 13Temperatur&lt;br /&gt;
e 23.2Relative humidity 79RSSI 2V&lt;br /&gt;
oltage 2.5Light 1 4Light 2 13Tempera&lt;br /&gt;
ture 23.2Relative humidity 79RSSI&lt;br /&gt;
 0Voltage 2.5&lt;br /&gt;
5.0: Contiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
---------------------------------------------&lt;br /&gt;
&amp;lt;p&amp;gt;set nodeid&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;nodeid 5 &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;5.0: Contiki&amp;gt; &lt;br /&gt;
nodeid 5&lt;br /&gt;
SEND 9 bytes&lt;br /&gt;
Node ID: 5&lt;br /&gt;
5.0: Contiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
---------------------------------------------&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;5) LED Related Commands:&amp;#039;&amp;#039;&amp;#039;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;blink [num]&amp;#039;&amp;#039;&amp;#039;: blink LEDs ([num] times)&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;shell-blink.c&amp;#039;&amp;#039;&amp;#039; : This file holds all the LED blink related shell commands. You can find the source file in this path ~/contiki-2.7/apps/shell&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;shell_blink_init()&amp;#039;&amp;#039;&amp;#039;: This function is used to register LED blink related shell commands with the Contiki Shell.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;Examples&amp;#039;&amp;#039;&amp;#039;&amp;lt;/p&amp;gt;&lt;br /&gt;
----------------------------&lt;br /&gt;
&amp;lt;p&amp;gt;LED&amp;#039;s blink 5 times&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;blink 5&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;5.0: Contiki&amp;gt; &lt;br /&gt;
blink 5&lt;br /&gt;
SEND 8 bytes&lt;br /&gt;
5.0: Contiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
----------------------------&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;6) Power Related Commands:&amp;#039;&amp;#039;&amp;#039;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;power&amp;#039;&amp;#039;&amp;#039;: print power profile&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;energy&amp;#039;&amp;#039;&amp;#039;: print energy profile&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;powerconv&amp;#039;&amp;#039;&amp;#039;: convert power profile to human readable output&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;shell-power.c&amp;#039;&amp;#039;&amp;#039; : This file holds all the power related shell commands. You can find the source file in this path ~/contiki-2.7/apps/shell&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;shell_power_init()&amp;#039;&amp;#039;&amp;#039;: This function is used to register power related shell commands with the Contiki Shell.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;Examples&amp;#039;&amp;#039;&amp;#039;&amp;lt;/p&amp;gt;&lt;br /&gt;
----------------------------&lt;br /&gt;
&amp;lt;p&amp;gt;Following power commands prints the power profile of sensor node in human readable format &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;power | powerconv&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;5.0: Contiki&amp;gt; &lt;br /&gt;
power | powerconv&lt;br /&gt;
SEND 18 bytes&lt;br /&gt;
CPU 3% LPM 8% tx 0% rx 0% idle tx 0% idle rx 0% tot 719 uW&lt;br /&gt;
5.0: Contiki&amp;gt; &amp;lt;/pre&amp;gt;&lt;br /&gt;
----------------------------&lt;br /&gt;
&amp;lt;p&amp;gt; Experiment &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;echo change power to 0 | append powfile.log &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;txpower 0 | binprint | power | powerconv | append powfile.log &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;repeat 2 1 {power | powerconv} | append powfile.log &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;echo change power to 3 | append powfile.log &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;txpower 3 | binprint | power | powerconv | append powfile.log &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;repeat 2 1 {power | powerconv} | append powfile.log &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;echo change power to 10 | append powfile.log &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;txpower 10| binprint | power | powerconv | append powfile.log &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;repeat 2 1 {power | powerconv} | append powfile.log &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;echo change power to 31 | append powfile.log &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;txpower 31| binprint | power | powerconv | append powfile.log &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;repeat 2 1 {power | powerconv} | append powfile.log &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;read powfile.log 0 40 &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;5.0: Contiki&amp;gt; &lt;br /&gt;
echo change power to 0 | append powfile.log&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 4 bytes&lt;br /&gt;
change power to 0&lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
txpower 0 | binprint | power | powerconv | append powfile.log&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 2 bytes&lt;br /&gt;
CPU 3% LPM 96% tx 0% rx 0% idle tx 0% idle rx 0% tot 2043 uW&lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
repeat 2 1 {power | powerconv} | append powfile.log&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 12 bytes&lt;br /&gt;
CPU 3% LPM 96% tx 0% rx 0% idle tx 0% idle rx 0% tot 2073 uW&lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
repeat 2 1 {power | powerconv} | append powfile.log&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 12 bytes&lt;br /&gt;
CPU 3% LPM 96% tx 0% rx 0% idle tx 0% idle rx 0% tot 2046 uW&lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
txpower 3 | binprint | power | powerconv | append powfile.log&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 2 bytes&lt;br /&gt;
CPU 3% LPM 96% tx 0% rx 0% idle tx 0% idle rx 0% tot 2150 uW&lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
repeat 2 1 {power | powerconv} | append powfile.log&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 12 bytes&lt;br /&gt;
CPU 3% LPM 96% tx 0% rx 0% idle tx 0% idle rx 0% tot 2099 uW&lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
echo change power to 10 | append powfile.log&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 5 bytes&lt;br /&gt;
change power to 10&lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
txpower 10| binprint | power | powerconv | append powfile.log&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 2 bytes&lt;br /&gt;
CPU 3% LPM 96% tx 0% rx 0% idle tx 0% idle rx 0% tot 2148 uW&lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
repeat 2 1 {power | powerconv} | append powfile.log&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 12 bytes&lt;br /&gt;
CPU 3% LPM 96% tx 0% rx 0% idle tx 0% idle rx 0% tot 2099 uW&lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
echo change power to 31 | append powfile.log&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 5 bytes&lt;br /&gt;
change power to 31&lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
txpower 31| binprint | power | powerconv | append powfile.log&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 2 bytes&lt;br /&gt;
CPU 3% LPM 96% tx 0% rx 0% idle tx 0% idle rx 0% tot 2071 uW&lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
repeat 2 1 {power | powerconv} | append powfile.log&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 12 bytes&lt;br /&gt;
CPU 3% LPM 96% tx 0% rx 0% idle tx 0% idle rx 0% tot 2086 uW&lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
read powfile.log 0 40&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 2 bytes&lt;br /&gt;
change power to 0CPU 3% LPM 96% tx 0% rx&lt;br /&gt;
 0% idle tx 0% idle rx 0% tot 2043 uWCPU&lt;br /&gt;
 3% LPM 96% tx 0% rx 0% idle tx 0% idle &lt;br /&gt;
rx 0% tot 2073 uWCPU 3% LPM 96% tx 0% rx&lt;br /&gt;
 0% idle tx 0% idle rx 0% tot 2046 uWCPU&lt;br /&gt;
 3% LPM 96% tx 0% rx 0% idle tx 0% idle &lt;br /&gt;
rx 0% tot 2150 uWCPU 3% LPM 96% tx 0% rx&lt;br /&gt;
 0% idle tx 0% idle rx 0% tot 2099 uWcha&lt;br /&gt;
nge power to 10CPU 3% LPM 96% tx 0% rx 0&lt;br /&gt;
% idle tx 0% idle rx 0% tot 2148 uWCPU 3&lt;br /&gt;
% LPM 96% tx 0% rx 0% idle tx 0% idle rx&lt;br /&gt;
 0% tot 2099 uWchange power to 31CPU 3% &lt;br /&gt;
LPM 96% tx 0% rx 0% idle tx 0% idle rx 0&lt;br /&gt;
% tot 2071 uWCPU 3% LPM 96% tx 0% rx 0% &lt;br /&gt;
idle tx 0% idle rx 0% tot 2086 uW&lt;br /&gt;
5.0: Contiki&amp;gt; &amp;lt;/pre&amp;gt;&lt;br /&gt;
----------------------------&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;Other Commands&amp;#039;&amp;#039;&amp;#039;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;1)&amp;#039;&amp;#039;&amp;#039;ps&amp;#039;&amp;#039;&amp;#039;: this command will list all running processes. By default, the following processes are executed: Shell server, Shell, Contiki serial shell, Serial driver, CC2420 driver, ctimer process, Sensors, Event timer.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;2)&amp;#039;&amp;#039;&amp;#039;kill &amp;lt;command&amp;gt;&amp;#039;&amp;#039;&amp;#039;: stop a specific command. &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;3)&amp;#039;&amp;#039;&amp;#039;killall&amp;#039;&amp;#039;&amp;#039;: stop all running commands.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;4)&amp;#039;&amp;#039;&amp;#039;reboot&amp;#039;&amp;#039;&amp;#039;: this command reboots the mote. &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;5)&amp;#039;&amp;#039;&amp;#039;collect-view-data&amp;#039;&amp;#039;&amp;#039;: provides sensor data, power consumption, network stats for the sky mote. &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;6)&amp;#039;&amp;#039;&amp;#039;powertrace&amp;#039;&amp;#039;&amp;#039;: this gives the power profile of the mote. &amp;#039;&amp;#039;&amp;#039; &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;Examples&amp;#039;&amp;#039;&amp;#039;&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
----------------------------&lt;br /&gt;
&amp;lt;p&amp;gt;This command lists all the running processes &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;ps&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;5.0: Contiki&amp;gt; &lt;br /&gt;
ps&lt;br /&gt;
SEND 3 bytes&lt;br /&gt;
Processes:&lt;br /&gt;
ps&lt;br /&gt;
Shell server&lt;br /&gt;
Shell&lt;br /&gt;
Contiki serial shell&lt;br /&gt;
Serial driver&lt;br /&gt;
CC2420 driver&lt;br /&gt;
Sensors&lt;br /&gt;
Ctimer process&lt;br /&gt;
Event timer&lt;br /&gt;
5.0: Contiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
---------------------------&lt;br /&gt;
&amp;lt;p&amp;gt; This command reboots the shell &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;time|binprint&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;reboot&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt; After rebooting print time, to see the restart of the mote time &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;time|binprint&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;5.0: Contiki&amp;gt; &lt;br /&gt;
time | binprint&lt;br /&gt;
SEND 16 bytes&lt;br /&gt;
6 34454 38458 0 65535 0 269 &lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
reboot&lt;br /&gt;
SEND 7 bytes&lt;br /&gt;
Rebooting the node in four seconds...&lt;br /&gt;
Rime started with address 5.0&lt;br /&gt;
MAC 05:00:00:00:00:00:00:00 Contiki 2.7 started. Node id is set to 5.&lt;br /&gt;
CSMA ContikiMAC, channel check rate 8 Hz, radio channel 26&lt;br /&gt;
Starting &amp;#039;Sky Contiki shell&amp;#039;&lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
time | binprint&lt;br /&gt;
SEND 16 bytes&lt;br /&gt;
6 2736 45115 0 65535 0 21 &lt;br /&gt;
5.0: Contiki&amp;gt; &amp;lt;/pre&amp;gt;&lt;br /&gt;
---------------------------&lt;br /&gt;
&amp;lt;p&amp;gt; This command prints out the collect view data for the mote &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt; Format of the print: [len][clock][timesynch_time][cpu][lpm][transmit][listen][parent][parent_etx][current_rtmetric][num_neighbors][beacon_interval][BATTERY_VOLTAGE_SENSOR][BATTERY_INDICATOR][LIGHT1_SENSOR][LIGHT2_SENSOR][TEMP_SENSOR][HUMIDITY_SENSOR][RSSI_SENSOR][ETX1_SENSOR][ETX2_SENSOR][ETX3_SENSOR][ETX4_SENSOR]&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;collect-view-data | binprint&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;5.0: Contiki&amp;gt; &lt;br /&gt;
collect-view-data | binprint&lt;br /&gt;
SEND 20 bytes&lt;br /&gt;
SEND 9 bytes&lt;br /&gt;
22 26024 0 1672 50278 0 269 0 128 0 6 0 2075 1 6 3 65535 65535 0 0 0 0 &lt;br /&gt;
5.0: Contiki&amp;gt; &amp;lt;/pre&amp;gt;&lt;br /&gt;
--------------------------&lt;br /&gt;
&amp;lt;p&amp;gt;This commands prints the power profile of the mote. &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;powertrace&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;5.0: Contiki&amp;gt; &lt;br /&gt;
powertrace&lt;br /&gt;
SEND 11 bytes&lt;br /&gt;
 16188 P 5.0 0 130619 4009338 0 23333 0 23333 130619 4009338 0 23333 0 23333 (radio 0.56% / 0.56% tx 0.00% / 0.00% listen 0.56% / 0.56%)&lt;br /&gt;
5.0: Contiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to write your own Shell commands ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Step1: Write a shell-your-cmdfile.c and shell-your-cmdfile.h files and place them in &amp;#039;&amp;#039;&amp;#039; ~/contiki-2.7/apps/shell &amp;#039;&amp;#039;&amp;#039; &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Step2: Add the shell-your-cmdfile.c for compilation in the shell_src var of Makefile.shell located in &amp;#039;&amp;#039;&amp;#039; ~/contiki-2.7/apps/shell &amp;#039;&amp;#039;&amp;#039; &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Shell.c has all the basic code blocks to build your own shell command. &amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;Example skeleton of code in shell-your-cmdfile.c to create your own shell command&amp;#039;&amp;#039;&amp;#039; &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
PROCESS(multiplyNums_process, &amp;quot;Multiply two numbers&amp;quot;);&lt;br /&gt;
SHELL_COMMAND(mulNum_command, &amp;quot;mn&amp;quot;, &amp;quot;mn: Multiply two numbers&amp;quot;, &amp;amp;multiplyNums_process);&lt;br /&gt;
/* --------------------------------- */&lt;br /&gt;
PROCESS_THREAD(multiplyNums_process, ev, data) {&lt;br /&gt;
PROCESS_BEGIN();&lt;br /&gt;
  // ...&lt;br /&gt;
  // ...&lt;br /&gt;
PROCESS_END();&lt;br /&gt;
}&lt;br /&gt;
.....&lt;br /&gt;
.....&lt;br /&gt;
shell_register_command(&amp;amp;mulNum_command);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== An example project for creating your own Shell with your commands ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Place the following two files in this path &amp;#039;&amp;#039;&amp;#039; ~/contiki-2.7/apps/shell &amp;#039;&amp;#039;&amp;#039; &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;1)shell-try-example.c&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;2)shell-try-example.h&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Source code: shell-try-example.c&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039; Two new shell commands are created&amp;#039;&amp;#039;&amp;#039;&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;&amp;#039;&amp;#039;&amp;#039;echo2&amp;#039;&amp;#039;&amp;#039;: This shell command echoes the text twice back onto the shell prompt &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt; &amp;#039;&amp;#039;&amp;#039;blinkAllOnce&amp;#039;&amp;#039;&amp;#039;: This shell command is used to make each of the LED&amp;#039;s (Red,Blu,Green) blink once each. &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Once the shell commands are created they are registered with the Contiki Shell using shell_try_init() &amp;lt;/p&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang =&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * \file   shell-try-example.c&lt;br /&gt;
 *         try Example Contiki shell command&lt;br /&gt;
 * \author&lt;br /&gt;
 *         Abhilash Hegde &amp;lt;hegdea@usc.edu&amp;gt;&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;contiki.h&amp;quot;&lt;br /&gt;
#include &amp;quot;shell.h&amp;quot;&lt;br /&gt;
#include &amp;lt;ctype.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;string.h&amp;gt;&lt;br /&gt;
#include &amp;quot;dev/leds.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
/*---------------------------------------------------------------------------*/&lt;br /&gt;
PROCESS(shell_echo2_process, &amp;quot;echo2&amp;quot;);&lt;br /&gt;
SHELL_COMMAND(echo2_command,&lt;br /&gt;
	      &amp;quot;echo2&amp;quot;,&lt;br /&gt;
	      &amp;quot;echo2: echoes back entered text twice&amp;quot;,&lt;br /&gt;
	      &amp;amp;shell_echo2_process);&lt;br /&gt;
/*---------------------------------------------------------------------------*/&lt;br /&gt;
PROCESS_THREAD(shell_echo2_process, ev, data)&lt;br /&gt;
{&lt;br /&gt;
&lt;br /&gt;
  PROCESS_BEGIN();&lt;br /&gt;
  &lt;br /&gt;
  /* Echo the data back twice */&lt;br /&gt;
  shell_output(&amp;amp;echo2_command, data, (int)strlen(data), data, (int)strlen(data));&lt;br /&gt;
  &lt;br /&gt;
  PROCESS_END();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/*---------------------------------------------------------------------------*/&lt;br /&gt;
PROCESS(shell_blinkAllOnce_process, &amp;quot;blinkAllOnce&amp;quot;);&lt;br /&gt;
SHELL_COMMAND(blinkAllOnce_command,&lt;br /&gt;
	      &amp;quot;blinkAllOnce&amp;quot;,&lt;br /&gt;
	      &amp;quot;blinkAllOnce: blink all LEDs one time&amp;quot;,&lt;br /&gt;
	      &amp;amp;shell_blinkAllOnce_process);&lt;br /&gt;
/*---------------------------------------------------------------------------*/&lt;br /&gt;
&lt;br /&gt;
PROCESS_THREAD(shell_blinkAllOnce_process, ev, data)&lt;br /&gt;
{  &lt;br /&gt;
  static struct etimer etimer;&lt;br /&gt;
&lt;br /&gt;
  PROCESS_EXITHANDLER(leds_off(LEDS_ALL);)&lt;br /&gt;
  &lt;br /&gt;
  PROCESS_BEGIN();&lt;br /&gt;
&lt;br /&gt;
  etimer_set(&amp;amp;etimer, CLOCK_SECOND/2);&lt;br /&gt;
  PROCESS_WAIT_UNTIL(etimer_expired(&amp;amp;etimer));&lt;br /&gt;
  leds_on(LEDS_RED);&lt;br /&gt;
&lt;br /&gt;
  etimer_reset(&amp;amp;etimer);&lt;br /&gt;
  PROCESS_WAIT_UNTIL(etimer_expired(&amp;amp;etimer));&lt;br /&gt;
  leds_off(LEDS_RED);&lt;br /&gt;
&lt;br /&gt;
  etimer_reset(&amp;amp;etimer);&lt;br /&gt;
  PROCESS_WAIT_UNTIL(etimer_expired(&amp;amp;etimer));&lt;br /&gt;
  leds_on(LEDS_GREEN);&lt;br /&gt;
&lt;br /&gt;
  etimer_reset(&amp;amp;etimer);&lt;br /&gt;
  PROCESS_WAIT_UNTIL(etimer_expired(&amp;amp;etimer));&lt;br /&gt;
  leds_off(LEDS_GREEN);&lt;br /&gt;
&lt;br /&gt;
  etimer_reset(&amp;amp;etimer);&lt;br /&gt;
  PROCESS_WAIT_UNTIL(etimer_expired(&amp;amp;etimer));&lt;br /&gt;
  leds_on(LEDS_BLUE);&lt;br /&gt;
&lt;br /&gt;
  etimer_reset(&amp;amp;etimer);&lt;br /&gt;
  PROCESS_WAIT_UNTIL(etimer_expired(&amp;amp;etimer));&lt;br /&gt;
  leds_off(LEDS_BLUE);&lt;br /&gt;
&lt;br /&gt;
  PROCESS_END();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
/*---------------------------------------------------------------------------*/&lt;br /&gt;
void&lt;br /&gt;
shell_try_init(void)&lt;br /&gt;
{&lt;br /&gt;
  shell_register_command(&amp;amp;echo2_command);&lt;br /&gt;
  shell_register_command(&amp;amp;blinkAllOnce_command);&lt;br /&gt;
}&lt;br /&gt;
/*---------------------------------------------------------------------------*/&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Header: shell-try-example.h&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;source lang =&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * \file   shell-try-example.h&lt;br /&gt;
 *         try Example Contiki shell command&lt;br /&gt;
 * \author&lt;br /&gt;
 *         Abhilash Hegde &amp;lt;hegdea@usc.edu&amp;gt;&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
#ifndef __SHELL_TRY_H__&lt;br /&gt;
#define __SHELL_TRY_H__&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;shell.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
void shell_try_init(void);&lt;br /&gt;
&lt;br /&gt;
#endif /* __SHELL_TRY_H__ */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Make the changes in Makefile.shell located in &amp;#039;&amp;#039;&amp;#039; ~/contiki-2.7/apps/shell &amp;#039;&amp;#039;&amp;#039; to include shell-try-example.c in shell_src &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;shell_src = shell.c shell-reboot.c \&lt;br /&gt;
            shell-vars.c shell-ps.c shell-rime.c shell-sendtest.c \&lt;br /&gt;
            shell-blink.c shell-text.c shell-try-example.c shell-time.c \&lt;br /&gt;
            shell-file.c shell-netfile.c shell-run.c \&lt;br /&gt;
            shell-rime-ping.c shell-rime-sniff.c shell-rime-netcmd.c \&lt;br /&gt;
            shell-rime-debug.c shell-rime-debug-runicast.c shell-coffee.c \&lt;br /&gt;
            shell-wget.c shell-httpd.c shell-irc.c \&lt;br /&gt;
            shell-checkpoint.c shell-power.c \&lt;br /&gt;
            shell-tcpsend.c shell-udpsend.c shell-ping.c shell-netstat.c \&lt;br /&gt;
            shell-rime-sendcmd.c shell-download.c shell-rime-neighbors.c \&lt;br /&gt;
            shell-rime-unicast.c \&lt;br /&gt;
            shell-base64.c \&lt;br /&gt;
            shell-netperf.c shell-memdebug.c \&lt;br /&gt;
	    shell-powertrace.c shell-collect-view.c shell-crc.c&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Now create a new project mySky-shell under ~/contiki-2.7/examples &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Add this source file mySky-shell.c in the project &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;source lang =&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 2008, Swedish Institute of Computer Science.&lt;br /&gt;
 * All rights reserved.&lt;br /&gt;
 *&lt;br /&gt;
 * Redistribution and use in source and binary forms, with or without&lt;br /&gt;
 * modification, are permitted provided that the following conditions&lt;br /&gt;
 * are met:&lt;br /&gt;
 * 1. Redistributions of source code must retain the above copyright&lt;br /&gt;
 *    notice, this list of conditions and the following disclaimer.&lt;br /&gt;
 * 2. Redistributions in binary form must reproduce the above copyright&lt;br /&gt;
 *    notice, this list of conditions and the following disclaimer in the&lt;br /&gt;
 *    documentation and/or other materials provided with the distribution.&lt;br /&gt;
 * 3. Neither the name of the Institute nor the names of its contributors&lt;br /&gt;
 *    may be used to endorse or promote products derived from this software&lt;br /&gt;
 *    without specific prior written permission.&lt;br /&gt;
 *&lt;br /&gt;
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS&amp;#039;&amp;#039; AND&lt;br /&gt;
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE&lt;br /&gt;
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE&lt;br /&gt;
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE&lt;br /&gt;
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL&lt;br /&gt;
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS&lt;br /&gt;
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)&lt;br /&gt;
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT&lt;br /&gt;
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY&lt;br /&gt;
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF&lt;br /&gt;
 * SUCH DAMAGE.&lt;br /&gt;
 *&lt;br /&gt;
 * This file is part of the Contiki operating system.&lt;br /&gt;
 *&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
/**&lt;br /&gt;
 * \file&lt;br /&gt;
 *         Tmote Sky-specific My Contiki shell &lt;br /&gt;
 *         Base code reference: sky-shell.c&lt;br /&gt;
 * \author&lt;br /&gt;
 *         Abhilash Hegde &amp;lt;hegdea@usc.edu&amp;gt;&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;contiki.h&amp;quot;&lt;br /&gt;
#include &amp;quot;shell.h&amp;quot;&lt;br /&gt;
#include &amp;quot;serial-shell.h&amp;quot;&lt;br /&gt;
#include &amp;quot;collect-view.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
//#include &amp;quot;net/rime.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
/*---------------------------------------------------------------------------*/&lt;br /&gt;
PROCESS(mysky_shell_process, &amp;quot;My Sky Contiki shell&amp;quot;);&lt;br /&gt;
AUTOSTART_PROCESSES(&amp;amp;mysky_shell_process);&lt;br /&gt;
/*---------------------------------------------------------------------------*/&lt;br /&gt;
#define WITH_PERIODIC_DEBUG 0&lt;br /&gt;
#if WITH_PERIODIC_DEBUG&lt;br /&gt;
static struct ctimer debug_timer;&lt;br /&gt;
static void&lt;br /&gt;
periodic_debug(void *ptr)&lt;br /&gt;
{&lt;br /&gt;
  ctimer_set(&amp;amp;debug_timer, 20 * CLOCK_SECOND, periodic_debug, NULL);&lt;br /&gt;
  collect_print_stats();&lt;br /&gt;
}&lt;br /&gt;
#endif /* WITH_PERIODIC_DEBUG */&lt;br /&gt;
/*---------------------------------------------------------------------------*/&lt;br /&gt;
PROCESS_THREAD(mysky_shell_process, ev, data)&lt;br /&gt;
{&lt;br /&gt;
  PROCESS_BEGIN();&lt;br /&gt;
&lt;br /&gt;
#if WITH_PERIODIC_DEBUG&lt;br /&gt;
  ctimer_set(&amp;amp;debug_timer, 20 * CLOCK_SECOND, periodic_debug, NULL);&lt;br /&gt;
#endif /* WITH_PERIODIC_DEBUG */&lt;br /&gt;
&lt;br /&gt;
  serial_shell_init();&lt;br /&gt;
  shell_blink_init();&lt;br /&gt;
  shell_file_init();&lt;br /&gt;
&lt;br /&gt;
  shell_ps_init();&lt;br /&gt;
  shell_reboot_init();&lt;br /&gt;
&lt;br /&gt;
  shell_sky_init();&lt;br /&gt;
  shell_power_init();&lt;br /&gt;
  shell_powertrace_init();&lt;br /&gt;
&lt;br /&gt;
  shell_text_init();&lt;br /&gt;
  shell_time_init();&lt;br /&gt;
&lt;br /&gt;
  shell_collect_view_init();&lt;br /&gt;
  shell_try_init();&lt;br /&gt;
  &lt;br /&gt;
  PROCESS_END();&lt;br /&gt;
}&lt;br /&gt;
/*---------------------------------------------------------------------------*/&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Add this Makefile in the project&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang =&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI_PROJECT = mySky-shell&lt;br /&gt;
all: $(CONTIKI_PROJECT)&lt;br /&gt;
&lt;br /&gt;
APPS = serial-shell powertrace collect-view&lt;br /&gt;
CONTIKI = ../..&lt;br /&gt;
&lt;br /&gt;
include $(CONTIKI)/Makefile.include&lt;br /&gt;
-include /home/user/nes/testbed-scripts/Makefile.include&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Add another Makefile.target to the project &amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;source lang =&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
TARGET = sky&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;compile the project&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;user@instant-contiki:~/contiki-2.7/examples/mySky-shell$ make TARGET=sky mySky-shell.upload savetarget&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;After successful compilation, you will see the following messages on the command prompt&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;user@instant-contiki:~/contiki-2.7/examples/mySky-shell$ make TARGET=sky mySky-shell.upload savetarget&lt;br /&gt;
msp430-objcopy mySky-shell.sky -O ihex mySky-shell.ihex&lt;br /&gt;
make IHEXFILE=mySky-shell.ihex sky-reset sky-upload&lt;br /&gt;
make[1]: Entering directory `/home/user/contiki-2.7/examples/mySky-shell&amp;#039;&lt;br /&gt;
make -k -j 20 sky-reset-sequence&lt;br /&gt;
make[2]: Entering directory `/home/user/contiki-2.7/examples/mySky-shell&amp;#039;&lt;br /&gt;
../../tools/sky/msp430-bsl-linux --telosb -c /dev/ttyUSB0 -r&lt;br /&gt;
MSP430 Bootstrap Loader Version: 1.39-telos-7&lt;br /&gt;
Use -h for help&lt;br /&gt;
Reset device ...&lt;br /&gt;
Done&lt;br /&gt;
make[2]: Leaving directory `/home/user/contiki-2.7/examples/mySky-shell&amp;#039;&lt;br /&gt;
make -j 20 sky-upload-sequence&lt;br /&gt;
make[2]: Entering directory `/home/user/contiki-2.7/examples/mySky-shell&amp;#039;&lt;br /&gt;
+++++ Erasing /dev/ttyUSB0&lt;br /&gt;
MSP430 Bootstrap Loader Version: 1.39-telos-7&lt;br /&gt;
Use -h for help&lt;br /&gt;
Mass Erase...&lt;br /&gt;
Transmit default password ...&lt;br /&gt;
+++++ Programming /dev/ttyUSB0&lt;br /&gt;
MSP430 Bootstrap Loader Version: 1.39-telos-7&lt;br /&gt;
Invoking BSL...&lt;br /&gt;
Transmit default password ...&lt;br /&gt;
Current bootstrap loader version: 1.61 (Device ID: f16c)&lt;br /&gt;
Changing baudrate to 38400 ...&lt;br /&gt;
Program ...&lt;br /&gt;
46214 bytes programmed.&lt;br /&gt;
+++++ Resetting /dev/ttyUSB0&lt;br /&gt;
MSP430 Bootstrap Loader Version: 1.39-telos-7&lt;br /&gt;
Use -h for help&lt;br /&gt;
Reset device ...&lt;br /&gt;
Done&lt;br /&gt;
make[2]: Leaving directory `/home/user/contiki-2.7/examples/mySky-shell&amp;#039;&lt;br /&gt;
make[1]: Leaving directory `/home/user/contiki-2.7/examples/mySky-shell&amp;#039;&lt;br /&gt;
saving Makefile.target&lt;br /&gt;
rm mySky-shell.ihex&lt;br /&gt;
user@instant-contiki:~/contiki-2.7/examples/mySky-shell$&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Login in to the mote using the following command&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;make login&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;After logging into the mote, you will see the shell prompt as seen below&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;user@instant-contiki:~/contiki-2.7/examples/mySky-shell$ make login&lt;br /&gt;
using saved target &amp;#039;sky&amp;#039;&lt;br /&gt;
../../tools/sky/serialdump-linux -b115200 /dev/ttyUSB0&lt;br /&gt;
connecting to /dev/ttyUSB0 (115200) [OK]&lt;br /&gt;
��{/#��3�#���#&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Hit Enter, too see shell prompt and Type help on the shell prompt. You will see the two new commands, echo2 and blinkAllonce&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;5.0: Contiki&amp;gt; &lt;br /&gt;
help&lt;br /&gt;
SEND 5 bytes&lt;br /&gt;
Available commands:&lt;br /&gt;
?: shows this help&lt;br /&gt;
append &amp;lt;filename&amp;gt;: append to file&lt;br /&gt;
binprint: print binary data in decimal format&lt;br /&gt;
blink [num]: blink LEDs ([num] times)&lt;br /&gt;
blinkAllOnce: blink all LEDs one time&lt;br /&gt;
collect-view-data: sensor data, power consumption, network stats&lt;br /&gt;
echo &amp;lt;text&amp;gt;: print &amp;lt;text&amp;gt;&lt;br /&gt;
echo2: echoes back entered text twice&lt;br /&gt;
energy: print energy profile&lt;br /&gt;
exit: exit shell&lt;br /&gt;
hd: print binary data in hexadecimal format&lt;br /&gt;
help: shows this help&lt;br /&gt;
kill &amp;lt;command&amp;gt;: stop a specific command&lt;br /&gt;
killall: stop all running commands&lt;br /&gt;
ls: list files&lt;br /&gt;
nodeid: set node ID&lt;br /&gt;
null: discard input&lt;br /&gt;
power: print power profile&lt;br /&gt;
powerconv: convert power profile to human readable output&lt;br /&gt;
powertrace [interval]: turn powertracing on or off, with reporting interval &amp;lt;interval&amp;gt;&lt;br /&gt;
ps: list all running processes&lt;br /&gt;
quit: exit shell&lt;br /&gt;
randwait &amp;lt;maxtime&amp;gt; &amp;lt;command&amp;gt;: wait for a random time before running a command&lt;br /&gt;
read &amp;lt;filename&amp;gt; [offset] [block size]: read from a file, with the offset and the block size as options&lt;br /&gt;
reboot: reboot the system&lt;br /&gt;
repeat &amp;lt;num&amp;gt; &amp;lt;time&amp;gt; &amp;lt;command&amp;gt;: run a command every &amp;lt;time&amp;gt; seconds&lt;br /&gt;
rfchannel &amp;lt;channel&amp;gt;: change CC2420 radio channel (11 - 26)&lt;br /&gt;
rm &amp;lt;filename&amp;gt;: remove the file named filename&lt;br /&gt;
sense: print out sensor data&lt;br /&gt;
senseconv: convert &amp;#039;sense&amp;#039; data to human readable format&lt;br /&gt;
size: print the size of the input&lt;br /&gt;
time [seconds]: output time in binary format, or set time in seconds since 1970&lt;br /&gt;
timestamp: prepend a timestamp to data&lt;br /&gt;
txpower &amp;lt;power&amp;gt;: change CC2420 transmission power (0 - 31)&lt;br /&gt;
write &amp;lt;filename&amp;gt;: write to file&lt;br /&gt;
5.0: Contiki&amp;gt; &lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt;Lets execute the two new shell commands added to the shell&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;5.0: Contiki&amp;gt; &lt;br /&gt;
echo2 Trojan&lt;br /&gt;
SEND 13 bytes&lt;br /&gt;
TrojanTrojan&lt;br /&gt;
5.0: Contiki&amp;gt;&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;blinkAllOnce command will blink all LED&amp;#039;s exactly once one by one&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;5.0: Contiki&amp;gt; &lt;br /&gt;
blinkAllOnce&lt;br /&gt;
SEND 13 bytes&lt;br /&gt;
5.0: Contiki&amp;gt; &amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt;Voila! There it is blinking of LEDs, Notice it ?&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Issues you might face ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;p&amp;gt; You may run out of memory on the mote when more than the possible shell_xx_init()&amp;#039;s are used in the firmware image of Contiki shell. This will result in compilation failure. By Trial and error method, figure out what would be a reasonable set of shell_xx_init()&amp;#039;s that can be registered as part of your Contiki Shell.&amp;lt;/p&amp;gt;&lt;br /&gt;
&amp;lt;p&amp;gt; While running Contiki serial shell, garbage values might be displayed in the shell due to invalid data or commands. In such cases either reboot the serial shell using the &amp;#039;reboot&amp;#039; command or Build the shell again and upload it on the mote to solve the issue.&amp;lt;/p&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
http://www.bithappens.se/blog/2012/07/01/contiki-debugging-in-cooja/&lt;br /&gt;
http://ai.vub.ac.be/Robotics/wiki/index.php/Some_cool_demos&lt;br /&gt;
http://www.nairaland.com/1167922/step-by-step-method-writing-contiki-programs&lt;br /&gt;
http://contiki.sourceforge.net/docs/2.6/a01796.html&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=CFS-Coffee&amp;diff=1832</id>
		<title>CFS-Coffee</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=CFS-Coffee&amp;diff=1832"/>
				<updated>2016-09-28T04:22:08Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
==Permanent Storage of Contiki on Tmote Sky (CFS-Coffee) ==&lt;br /&gt;
&lt;br /&gt;
Contiki provides File System to achieve the goal of permanent storage. The Contiki File System(CFS) works as a Virtual File System and provides some interfaces for different file systems. CFS-POSIX and Coffee are two with full functionalities. CFS-POSIX is used in Contiki platforms that runs in native mode. Coffee, on the other hand, is primarily aimed at sensor devices that equiped with Flash Memories or EEPROM.&lt;br /&gt;
&lt;br /&gt;
==The CFS Programming Interface==&lt;br /&gt;
&lt;br /&gt;
For detail interface introduction, please refer to the below link:&lt;br /&gt;
&lt;br /&gt;
http://contiki.sourceforge.net/docs/2.6/a00128.html&lt;br /&gt;
&lt;br /&gt;
==Introduction to Coffee==&lt;br /&gt;
&lt;br /&gt;
Coffee is a fully functional file system designed specifically for the characteristics of flash memories and EEPROM. Coffee is usually used in sensor devices with flash (e.g Tmote Sky), so two principles are pretty important. First, the metadata stored in RAM for each file should be small because sensor devices have very limited RAM space. Second, bits in flash memories can be toggled from 1 to 0, but not toggled back from 0 to 1 without doing an expensive erase. Thus, when modifying a file, Coffee creates an invisible micro log which is linked to the original file. The micro log contains the lastest data of the file. For user, the micro log and the original file is just one logical file, as if they modify the original file. When the micro log eventually fills up, Coffee transparently merges the content of the original file and the micro log into a new file, and deletes the two former files. &lt;br /&gt;
&lt;br /&gt;
Coffee has a flat directory structure. It only has the root directory. Thus, cfs_opendir() only accept “/” or “.” as the second argument. When removing a file from a Coffee, there are two steps. First, external user calls cfs_remove(). The file mentioned by user will be marked as obsolete. Obsolete files cannot be seen by external users. Coffee will actually delete files only when a new file reservation request cannot be granted.&lt;br /&gt;
&lt;br /&gt;
The implementation of Coffee in core/cfs/cfs-coffee.c is totally platform independent. The specific configuration of Coffee for different platforms is written in csf-coffee-arch.c. We can see many csf-coffee-arch.c files in plarform/./csf-coffee-arch.c, e.g plarform/z1/csf-coffee-arch.c, plarform/esb/csf-coffee-arch.c, plarform/sky/csf-coffee-arch.c. When we decide the platform, such as TARGET=sky, we compile plarform/sky/csf-coffee-arch.c and ignore all other csf-coffee-arch.c files. Macro definitions in plarform/sky/csf-coffee-arch.c configure the details of coffee in Sky Mote. Macro definition contains parameters like COFFEE_SECTOR_SIZE, COFFEE_PAGE_SIZE, COFFEE_FD_SET_SIZE, COFFEE_MICRO_LOGS and so on. Also, it defines COFFEE_WRITE, COFFEE_READ and COFFEE_ERASE to point to the device drivers I/O function. &lt;br /&gt;
&lt;br /&gt;
==CFS-Coffee Interface Extensions==&lt;br /&gt;
&lt;br /&gt;
For detail interface introduction, please refer to the below link:&lt;br /&gt;
&lt;br /&gt;
http://contiki.sourceforge.net/docs/2.6/a00122.html&lt;br /&gt;
&lt;br /&gt;
cfs_coffee_format() may take a few second because all sectors must be erased.  cfs_coffee_reserve() and int cfs_coffee_configure_log() can optimize Coffee’s handling of the file.&lt;br /&gt;
&lt;br /&gt;
==CFS-Coffee Summary==&lt;br /&gt;
&lt;br /&gt;
Here is an example of how everything works. Suppose we want to run Contiki on Sky Mote. First make sure TARGET=sky. Then every file about sky is compiled and you can also see a new folder which is created called sky_obj. Inside contains everything you need to run on Sky Mote. Suppose you want to use Coffee as the File System. Write cfs_coffee_format() before the first time you try to operate on Coffee. This function notice the system that you want to use Coffee and the system will do some formatting. Let’s say then you want to read something from an existing file with the function call cfs_read(). This function is defined in core/cfs/cfs.h. Because we make use of Coffee, the implementation of this function will be in core/cfs/cfs-coffee.c. The function cfs_read() actually calls the macro definition COFFEE_READ(). The definition of COFFEE_READ() is in cfs-coffee-arch.c. Since we make TARGET=sky, the one that was compiled and used by us is platform/sky/cfs-coffee-arch.c. This file defines COFFEE_READ() to point to Sky Mote drivers I/O function. Finally, the external users’ cfs_read() gets to the Sky Driver I/O function.&lt;br /&gt;
&lt;br /&gt;
==An Example of Running Coffee on Tmote Sky==&lt;br /&gt;
&lt;br /&gt;
This example shows the basic way to open, read, write and append files in Coffee. The platform we choose here is Tmote Sky.&lt;br /&gt;
First, open an example provided by Contiki (/home/user/contiki-2.7/example/sky/example- coffee.c).&lt;br /&gt;
Here, it defines two test functions:&lt;br /&gt;
  static int file_test(const char *filename, char *msg) &lt;br /&gt;
opens a file named FILENAME with CFS_WRITE | CFS_APPEND | CFS_READ. It appends the opening file with records and prints out everything in the file.&lt;br /&gt;
  static int dir_test(void) &lt;br /&gt;
opens the root directory and prints out all entries of root.&lt;br /&gt;
The Process calls file_test() twice and dir_test() once. Then the process ends.&lt;br /&gt;
Now, we do some modifications based on this example. First we add a function static int read_test(void). The implementation of this function is shown as below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
  static int&lt;br /&gt;
  read_test(void)&lt;br /&gt;
  {&lt;br /&gt;
	  struct cfs_dir dir;&lt;br /&gt;
	  struct cfs_dirent dirent;&lt;br /&gt;
	  int fd;&lt;br /&gt;
	  int r;&lt;br /&gt;
	  struct record {&lt;br /&gt;
    		  char message[16];&lt;br /&gt;
    		  uint8_t sequence;&lt;br /&gt;
  	  } record;&lt;br /&gt;
	  static int i = 0;&lt;br /&gt;
	  if (cfs_opendir(&amp;amp;dir, &amp;quot;/&amp;quot;) != 0)&lt;br /&gt;
	  {&lt;br /&gt;
		printf(&amp;quot;Fail to open the root directory\n&amp;quot;);&lt;br /&gt;
		return -1;&lt;br /&gt;
	}&lt;br /&gt;
	while(cfs_readdir(&amp;amp;dir, &amp;amp;dirent) == 0)&lt;br /&gt;
	{&lt;br /&gt;
		i ++;&lt;br /&gt;
		printf(&amp;quot;\nFile name: %s\n&amp;quot;, dirent.name);&lt;br /&gt;
		if ((fd =cfs_open(dirent.name, CFS_READ)) != -1)&lt;br /&gt;
		{&lt;br /&gt;
			cfs_seek(fd, 0, CFS_SEEK_SET);&lt;br /&gt;
			for(;;) &lt;br /&gt;
			{&lt;br /&gt;
    				r = cfs_read(fd, &amp;amp;record, sizeof(record));&lt;br /&gt;
    				if(r == 0) {&lt;br /&gt;
      					break;&lt;br /&gt;
    				} else if(r &amp;gt; sizeof(record)) {&lt;br /&gt;
      					printf(&amp;quot;failed to read %d bytes from %s, got %d\n&amp;quot;,(int)sizeof(record), dirent.name, r);&lt;br /&gt;
      					cfs_close(fd);&lt;br /&gt;
      					return -1;&lt;br /&gt;
    				}&lt;br /&gt;
&lt;br /&gt;
    				printf(&amp;quot;Read message \&amp;quot;%s\&amp;quot;, sequence %u\n&amp;quot;,record.message, record.sequence);&lt;br /&gt;
  			}&lt;br /&gt;
		}&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
	if (i == 0)&lt;br /&gt;
	{&lt;br /&gt;
		printf(&amp;quot;\nNo file exists.\n&amp;quot;);&lt;br /&gt;
		return 0;&lt;br /&gt;
	}&lt;br /&gt;
	else&lt;br /&gt;
	{&lt;br /&gt;
		return 1;&lt;br /&gt;
	}&lt;br /&gt;
		&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function of read_test() is quite simple. It opens the root file system and reads all the data in the file system. In the while() loop, it reads an entry of a file. Then it makes use of the entry to open the file and read all the data out.&lt;br /&gt;
&lt;br /&gt;
Also, in the process, we add an etimer at the beginning. The process can only move on either when the timer expired or someone push a button. If the timer expire, the process will run cfs_coffee_format(), which will set all the flash to 1. Otherwise, we don’t do this. Then we call read_test(), which read all the data in the file system. After that, we check whether there are files exist in the system. If there is, we give up wirting. Otherwise, we create two files and write some data in.&lt;br /&gt;
The process then looks like:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
PROCESS_THREAD(example_coffee_process, ev, data)&lt;br /&gt;
{&lt;br /&gt;
  PROCESS_BEGIN();&lt;br /&gt;
&lt;br /&gt;
	static int flag = 0;&lt;br /&gt;
	button_sensor.configure(SENSORS_ACTIVE, 1);&lt;br /&gt;
&lt;br /&gt;
	static struct etimer ak;&lt;br /&gt;
	etimer_set(&amp;amp;ak, CLOCK_SECOND * 6);&lt;br /&gt;
	&lt;br /&gt;
	PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER || ev == sensors_event &amp;amp;&amp;amp; data == &amp;amp;button_sensor);&lt;br /&gt;
&lt;br /&gt;
	if (ev == PROCESS_EVENT_TIMER)&lt;br /&gt;
		cfs_coffee_format();	&lt;br /&gt;
&lt;br /&gt;
	if ((flag = read_test()) == -1)&lt;br /&gt;
		PROCESS_EXIT();&lt;br /&gt;
&lt;br /&gt;
	&lt;br /&gt;
	static struct etimer et;&lt;br /&gt;
	etimer_set(&amp;amp;et, CLOCK_SECOND * 2);&lt;br /&gt;
	PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER);&lt;br /&gt;
&lt;br /&gt;
	if (flag == 1)&lt;br /&gt;
		printf(&amp;quot;\nData exists...No written again...\n&amp;quot;);&lt;br /&gt;
	else&lt;br /&gt;
	{&lt;br /&gt;
		printf(&amp;quot;\nData begin to write...\n&amp;quot;);&lt;br /&gt;
		if(file_test(FILENAME_1, &amp;quot;The first test&amp;quot;) == 0) {&lt;br /&gt;
    			printf(&amp;quot;file test 1 failed\n&amp;quot;);&lt;br /&gt;
  		}&lt;br /&gt;
		if(file_test(FILENAME_1, &amp;quot;The second test&amp;quot;) == 0) {&lt;br /&gt;
    			printf(&amp;quot;file test 2 failed\n&amp;quot;);&lt;br /&gt;
		}&lt;br /&gt;
		if(file_test(FILENAME_2, &amp;quot;The third test&amp;quot;) == 0) {&lt;br /&gt;
    			printf(&amp;quot;file test 3 failed\n&amp;quot;);&lt;br /&gt;
		}&lt;br /&gt;
		if(dir_test() == 0) {&lt;br /&gt;
    			printf(&amp;quot;dir test failed\n&amp;quot;);&lt;br /&gt;
  		}&lt;br /&gt;
&lt;br /&gt;
	}&lt;br /&gt;
&lt;br /&gt;
  PROCESS_END();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, connect your mote to your PC’s USB port and change directory. &lt;br /&gt;
&lt;br /&gt;
  cd contiki-2.7/examples/sky &lt;br /&gt;
&lt;br /&gt;
Compile this Coffee example to Sky Mote.&lt;br /&gt;
  &lt;br /&gt;
  sudo make TARGET=sky example-coffee.upload login&lt;br /&gt;
&lt;br /&gt;
Once login, wait for 6 seconds patiently for the first time out.If it success, we should see terminal has the output like this:&lt;br /&gt;
Here you can see, we create two files and write some data in. Then, disconnect your mote and do whatever you want. Maybe one day later, you come back. Now it’s the time to keep moving. &lt;br /&gt;
Welcome back. Now connect your mote with USB port again and do:&lt;br /&gt;
&lt;br /&gt;
  sudo make TARGET=sky login&lt;br /&gt;
&lt;br /&gt;
You are in the process again and please follow the following two steps:&lt;br /&gt;
1. push “RESET” button;&lt;br /&gt;
2. push “USER” button within 6 seconds.&lt;br /&gt;
Now, the program will not call cfs_coffee_format(). If succeed, you will see: &lt;br /&gt;
As you can see, we read out all the data that you wrote in one day ago. Everything is right there without any change.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=Timers&amp;diff=1831</id>
		<title>Timers</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=Timers&amp;diff=1831"/>
				<updated>2016-09-28T04:21:02Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
Timers can be used to control periodic tasks as well as implement sophisticated algorithms. The implementation of each type of timer is platform-dependent and has different properties that make them useful in specific situations; some timers have low granularity (seconds) and overflow once in tens of years, and others provide high granularity (microseconds), but overflow rapidly. There are 5 types of timers provided by Contiki:&lt;br /&gt;
&lt;br /&gt;
* timer&lt;br /&gt;
* stimer&lt;br /&gt;
* ctimer&lt;br /&gt;
* etimer&lt;br /&gt;
* rtimer&lt;br /&gt;
&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The functions of all types of timers are located inside folder &amp;#039;&amp;#039;&amp;#039;core/sys/{timer, stimer, ctimer, etimer, rtimer}.{c,h}&amp;#039;&amp;#039;&amp;#039;. A complete documentation of Timers in Contiki can be found [https://github.com/contiki-os/contiki/wiki/Timers here].&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;timer&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;stimer&amp;#039;&amp;#039;&amp;#039; are the most basic types of timers and are used to check if a time interval has passed. They do not notice when the time period has elapsed, so the application needs to check periodically if they have expired. The difference between them is the resolution: &amp;#039;&amp;#039;timers&amp;#039;&amp;#039; use system clock ticks, which gives high granularity (order of microseconds) but short overflow periods (order of seconds). On the other hand, &amp;#039;&amp;#039;stimers&amp;#039;&amp;#039; use seconds to allow much longer time periods (order of years), but has lesser granularity. &lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;etimer&amp;#039;&amp;#039;&amp;#039; provides event timers and are used to schedule events to the processes after a period of time. They are used in Contiki processes to wait for a time period while the rest of the system can work or enter low power mode.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;ctimer&amp;#039;&amp;#039;&amp;#039; provides callback timers and are used to schedule calls to functions after a period of time. Like event timers, they are used to wait for some time while the rest of the system can work or enter low power mode. Since the callback timers call a function when a timer expires, they are especially useful in any code that do not have an explicit Contiki process such as protocol implementations.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;&amp;#039;rtimer&amp;#039;&amp;#039;&amp;#039; provides scheduling of real-time tasks. The rtimer library preempts any running Contiki process in order to let the real-time tasks execute at the scheduled time. The real-time tasks are used in time critical codes.&lt;br /&gt;
&lt;br /&gt;
== You will learn ==&lt;br /&gt;
&lt;br /&gt;
*Simple implementations of each type of timer.&lt;br /&gt;
*Tasks each timer can be used for.&lt;br /&gt;
*Some functions in the timer libraries.&lt;br /&gt;
&lt;br /&gt;
=== Step 1 ===&lt;br /&gt;
&lt;br /&gt;
Again, we will have to change folders. We will create a new file for this tutorial, but let&amp;#039;s create it in the same directory we&amp;#039;ve been working with, so type&lt;br /&gt;
&lt;br /&gt;
  cd contiki-2.7/examples/rime&lt;br /&gt;
&lt;br /&gt;
into the terminal. For this tutorial, we will be modifying the example-broadcast.c file we have previously used, so type &lt;br /&gt;
&lt;br /&gt;
  cp example-broadcast.c example-timer.c&lt;br /&gt;
&lt;br /&gt;
into your terminal. You should now see a new file, &amp;quot;example-timer.c&amp;quot; in your directory. Open it up with an editor of your choice (we will use gedit for this tutorial):&lt;br /&gt;
&lt;br /&gt;
  gedit example-timer.c&lt;br /&gt;
&lt;br /&gt;
=== Step 2 ===&lt;br /&gt;
&lt;br /&gt;
There is currently a working example of an etimer in this code, so before we proceed, let&amp;#039;s understand this. At the beginning of the process, it is declared with&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
static struct etimer et;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This creates an etimer instance. Within the while loop, it is set with the function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
etimer_set(&amp;amp;et, CLOCK_SECOND*4 + random_rand() % (CLOCK_SECOND * 4));&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function takes the arguments:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
etimer_set(struct etimer *et, clock_time_t interval);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and sets the etimer et to expire after interval.&lt;br /&gt;
&lt;br /&gt;
After that, we see the line: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&amp;amp;et));&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
which tells the example_broadcast_process to pause until it receives an event, so the program is essentially giving up system control. When an etimer expires, it posts a PROCESS_EVENT_TIMER event to the example_broadcast_process. So this tells the process to continue and begin running again, as the etimer has expired, and an event has been posted. Now that we understand what the etimer is doing here, let&amp;#039;s incorporate some other timers.&lt;br /&gt;
&lt;br /&gt;
=== Step 3 - Adding a timer ===&lt;br /&gt;
&lt;br /&gt;
To start simply, we are going to use a timer to just print out a message once it expires. Just below&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
static struct etimer et;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
declare a timer with the line:  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
static struct timer t;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Before the while loop, set the timer with the line: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
timer_set(&amp;amp;t, CLOCK_SECOND * 10);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This sets the timer to expire after 10 seconds.&lt;br /&gt;
&lt;br /&gt;
Below the line &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
printf(&amp;quot;broadcast message sent\n&amp;quot;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
add in the code:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
  if(timer_expired(&amp;amp;t)){&lt;br /&gt;
   printf(&amp;quot;Timer expired\n&amp;quot;);&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, save and run your code on your Tmote Sky by typing into your terminal:&lt;br /&gt;
&lt;br /&gt;
  make TARGET=sky example-timer.upload&lt;br /&gt;
&lt;br /&gt;
If you don&amp;#039;t have a Sky, run this code on Mspsim with the line&lt;br /&gt;
&lt;br /&gt;
  make TARGET=sky example-timer.mspsim&lt;br /&gt;
&lt;br /&gt;
Among the &amp;quot;broadcast message sent&amp;quot; messages, you should see a &amp;quot;Timer expired&amp;quot; notice. This only occurs one time, however. This is because we never reset the timer. So, return to your example-timer.c file. Underneath your printf(&amp;quot;Timer expired\n&amp;quot;) line, add the code&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
timer_reset(&amp;amp;t);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This will reset the timer to expire again in 10 * CLOCK_SECONDS. Run your code again. You should now see &amp;quot;Timer expired&amp;quot; every few broadcast messages. Now, say we only wanted to send broadcast messages for 30 seconds. Let&amp;#039;s use our timer to exit our process after it expires. Change your original timer_set function to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
timer_set(&amp;amp;t, CLOCK_SECOND * 30);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, just delete the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
timer_reset(&amp;amp;t);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
and replace it with&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
PROCESS_EXIT();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run your code again, and it should stop after the timer expires. You have now learned some basic functionality of the timer.&lt;br /&gt;
&lt;br /&gt;
=== Step 4 - Using ctimer ===&lt;br /&gt;
&lt;br /&gt;
Begin by deleting the &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
PROCESS_EXIT();&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
line that you previously wrote. We are going to implement a ctimer and no longer want to exit the process. At the beginning of the process, where you declared the other timers, include the code &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
static struct ctimer ct;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This declares a ctimer, which has the property of calling a function when it expires. In order to do this, we will need to declare a function for it to call! So, just before the start of the process, underneath the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
static struct broadcast_conn broadcast;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Declare the following function:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
  static void timer_events(void *ptr){ &lt;br /&gt;
   printf(&amp;quot;Congratulations, you called this function&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, go back to the process. Just before the while loop, set the ctimer by typing&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
ctimer_set(&amp;amp;ct, CLOCK_SECOND * 5, timer_events, NULL);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This set function takes in arguments that tell the ctimer which function to call, as well as what to pass into the function (NULL in this case). Run your code. You will see that your function is called one time, but since we never reset the code, it never gets called again. So, let&amp;#039;s reset the ctimer as part of the function. Just before you set the ctimer, include this line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
void *ct_ptr = &amp;amp;ct;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
And change your ctimer_set function to read:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
ctimer_set(&amp;amp;ct, CLOCK_SECOND * 5, timer_events, ct_ptr);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now go up to your function, and change it to read like this: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
  static void timer_events(void *ptr){&lt;br /&gt;
   printf(&amp;quot;Congratulations, you called this function&amp;quot;);&lt;br /&gt;
   struct ctimer* ct_ptr = ptr;&lt;br /&gt;
   ctimer_reset(ct_ptr);&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now run your code again. The function should be called over and over again. The last thing we will do with this function is learn how to terminate the process from the function, using our same ctimer. To begin, change your c_timer set function to&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
ctimer_set(&amp;amp;ct, CLOCK_SECOND * 30, timer_events, ct_ptr);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
so the process runs longer before we exit it. Now go up to your function and delete the ctimer_reset line. Replace it with:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
process_exit(&amp;amp;example_broadcast_process);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run your code again. You will see that the example_broadcast_process stops running after 30 seconds, and you have learned how to do this from a function called by a ctimer!&lt;br /&gt;
&lt;br /&gt;
=== Step 5 - Introduction to rtimer===&lt;br /&gt;
&lt;br /&gt;
The rtimer library provides real-time scheduling mechanisms, typically for applications where a response to an external event is extremely time sensitive. Rtimers use absolute system clock time as a reference.&lt;br /&gt;
&lt;br /&gt;
We will modify our original broadcast process with an rtimer, to demonstrate rtimer&amp;#039;s usefulness for calling a function at an absolute time in response to an event. In this case, our etimer produces random, simulated real-time events.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
  static void function( rtimer *rt, void *ptr) {&lt;br /&gt;
    packetbuf_copyfrom(&amp;quot;Event Occurred&amp;quot;, 15);&lt;br /&gt;
    broadcast_send(&amp;amp;broadcast);&lt;br /&gt;
    printf(&amp;quot;Real Time Task Completed\n&amp;quot;); &lt;br /&gt;
  } &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The function &amp;#039;responds&amp;#039; to the scheduling of a real-time task using rtimer, and can be used to answer an external stimulus.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
  PROCESS_THREAD(example_broadcast_process, ev, data) {&lt;br /&gt;
    static struct etimer et;&lt;br /&gt;
    static struct rtimer rt;&lt;br /&gt;
    PROCESS_EXIT_HANDLER(broadcast_close(&amp;amp;broadcast));&lt;br /&gt;
    PROCESS_BEGIN();&lt;br /&gt;
    while (1) {&lt;br /&gt;
        PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&amp;amp;et)); //Real Time Event &lt;br /&gt;
        printf(&amp;quot;Real Time Event Recorded\n&amp;quot;);&lt;br /&gt;
        rtimer_set(&amp;amp;rt, RTIMER_NOW()+RTIMER_ARCH_SECOND,1,function,NULL);&lt;br /&gt;
    }&lt;br /&gt;
    PROCESS_END();&lt;br /&gt;
  } &lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note the setting method for rtimer takes 4 arguments: &lt;br /&gt;
- real-time task (rtimer)&lt;br /&gt;
- time at which task must be completed&lt;br /&gt;
- duration - UNUSED&lt;br /&gt;
- function to call&lt;br /&gt;
- argument for the function, which is NULL in this case.&lt;br /&gt;
&lt;br /&gt;
Upon simulating this code, you will see the rtimer respond almost immediately to each simulated real-time event. However, using the simulation does not allow for some of the features of rtimer, because rtimer is a low level struct that has significant dependence on physical aspects of the hardware. You will notice that replacing &amp;quot;RTIMER_ARCH_SECOND&amp;quot; with &amp;quot;4096*RTIMER_ARCH_SECOND&amp;quot; does not change the delay in simulation. Code using rtimer should be run on physical tmotes, due to their dependence on physical hardware (as should stimer.)&lt;br /&gt;
&lt;br /&gt;
A complete example of rtimer&amp;#039;s uses that can be run on hardware is &amp;quot;example-rudolph1.c&amp;quot; in rime.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
References:&lt;br /&gt;
[https://github.com/contiki-os/contiki/wiki/Timers#wiki-The_Timer_Library]&lt;br /&gt;
[http://contiki.sourceforge.net/docs/2.6/a01673.html]&lt;br /&gt;
&lt;br /&gt;
Edited by: Leo Linsky, Tim Ferrell&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=Sensor_acquisition&amp;diff=1830</id>
		<title>Sensor acquisition</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=Sensor_acquisition&amp;diff=1830"/>
				<updated>2016-09-28T04:13:43Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* Step 1 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article helps you learn how to create the Sensor Acquisition Program, which can be used to show the Temperature, Humidity and Light Intensity in the vicinity of Tmote Sky.&lt;br /&gt;
&lt;br /&gt;
== How to start an application in Contiki ==&lt;br /&gt;
&lt;br /&gt;
Every process should start with the PROCESS macro. It takes two arguments&lt;br /&gt;
*name: The variable name of the process structure.&lt;br /&gt;
*strname: The string representation of the process name.&lt;br /&gt;
&lt;br /&gt;
  PROCESS(name,strname)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Then comes another macro AUTOSTART_PROCESS(struct process &amp;amp;). AUTOSTART_PROCESSES automatically starts the process(es) given in the argument(s) when the module boots.&lt;br /&gt;
*&amp;amp;name: Reference to the process name.&lt;br /&gt;
&lt;br /&gt;
  AUTOSTART_PROCESS(struct process &amp;amp;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Then we call the PROCESS_THREAD function. This function is used to define the protothread of a process. The process is called whenever an event occurs in the system.Each process in the module requires an event handler under the PROCESS_THREAD macro.&lt;br /&gt;
*name: The variable name of the process structure.&lt;br /&gt;
*process_event_t: The variable of type character.If this variable is same as PROCESS_EVENT_EXIT then PROCESS_EXITHANDLER is invoked.&lt;br /&gt;
&lt;br /&gt;
  PROCESS_THREAD(name, process_event_t, process_data_t)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Then comes the PROCESS_BEGIN macro. This macro defines the beginning of a process, and must always appear in a PROCESS_THREAD() definition.&lt;br /&gt;
&lt;br /&gt;
  PROCESS_BEGIN()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Then we write the set of C statements as per the requirement of the application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
At the end we use another macro PROCESS_END. This macro defines the end of a process. It must appear in a PROCESS_THREAD() definition and must always be included. The process exits when the PROCESS_END() macro is reached.&lt;br /&gt;
&lt;br /&gt;
  PROCESS_END()&lt;br /&gt;
&lt;br /&gt;
== Generic Template for Creating Application in Contiki  ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
PROCESS(name,strname);&lt;br /&gt;
AUTOSTART_PROCESSES(struct process &amp;amp;);&lt;br /&gt;
&lt;br /&gt;
PROCESS_THREAD(name, process_event_t, process_data_t)&lt;br /&gt;
{ &lt;br /&gt;
&lt;br /&gt;
----Initialization of required variables----&lt;br /&gt;
&lt;br /&gt;
PROCESS_BEGIN();&lt;br /&gt;
&lt;br /&gt;
---Set of C statements---&lt;br /&gt;
&lt;br /&gt;
PROCESS_END();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Sensor Acquisition Program on the Tmote Sky! ==&lt;br /&gt;
&lt;br /&gt;
=== Step 1 ===&lt;br /&gt;
&lt;br /&gt;
Using the opened terminal window compile and upload the Sensor Acquisition program on the Tmote Sky.&lt;br /&gt;
&lt;br /&gt;
  make TARGET=sky savetarget    (This saves the target for any future compilations)&lt;br /&gt;
  make sensor-acq.upload    (This will upload the code on the Tmote Sky)&lt;br /&gt;
  make login    (This will enable us to view the output. If permission error occurs, use sudo command at the beginning)&lt;br /&gt;
&lt;br /&gt;
See the following link for troubleshooting - http://anrg.usc.edu/contiki/index.php/Troubleshooting&lt;br /&gt;
&lt;br /&gt;
=== Step 2 ===&lt;br /&gt;
&lt;br /&gt;
Press the reset button on the Tmote Sky. The following message will appear on the terminal window &lt;br /&gt;
&lt;br /&gt;
  Temperature=28.04 C (6764)&lt;br /&gt;
  Humidity=73.20% (2259)&lt;br /&gt;
  Light=38.26 lux (94)&lt;br /&gt;
&lt;br /&gt;
Values inside the round brackets (ex 6764, 2259 and 94) are the actual sensor values and the calibrated values can be obtained by performing calculations on these sensor values. You can find the calculations at&lt;br /&gt;
http://tinyos.stanford.edu/tinyos-wiki/index.php/Boomerang_ADC_Example&lt;br /&gt;
&lt;br /&gt;
=== Step 3 ===&lt;br /&gt;
&lt;br /&gt;
Press Ctrl-C to quit.&lt;br /&gt;
&lt;br /&gt;
== Code ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
PROCESS(sensor_acq_process,&amp;quot;Sensor Acquisition&amp;quot;);&lt;br /&gt;
AUTOSTART_PROCESSES(&amp;amp;sensor_acq_process);&lt;br /&gt;
&lt;br /&gt;
PROCESS_THREAD(sensor_acq_process,ev,data)&lt;br /&gt;
{ &lt;br /&gt;
      static struct etimer et;&lt;br /&gt;
      static int val;&lt;br /&gt;
      static float s = 0;&lt;br /&gt;
      static int dec;&lt;br /&gt;
      static float frac;&lt;br /&gt;
&lt;br /&gt;
      PROCESS_BEGIN();&lt;br /&gt;
&lt;br /&gt;
      printf(&amp;quot;Starting Sensor Example.\n&amp;quot;);&lt;br /&gt;
      &lt;br /&gt;
      while(1)&lt;br /&gt;
      {&lt;br /&gt;
	   etimer_set(&amp;amp;et, CLOCK_SECOND * 2);&lt;br /&gt;
	   SENSORS_ACTIVATE(light_sensor);&lt;br /&gt;
      	   SENSORS_ACTIVATE(sht11_sensor);&lt;br /&gt;
        &lt;br /&gt;
	   PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&amp;amp;et));&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
           val = sht11_sensor.value(SHT11_SENSOR_TEMP);&lt;br /&gt;
      	   if(val != -1) &lt;br /&gt;
      	   {&lt;br /&gt;
		s= ((0.01*val) - 39.60);&lt;br /&gt;
      	  	dec = s;&lt;br /&gt;
      	  	frac = s - dec;&lt;br /&gt;
      	  	printf(&amp;quot;\nTemperature=%d.%02u C (%d)\n&amp;quot;, dec, (unsigned int)(frac * 100),val);               &lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
	   val=sht11_sensor.value(SHT11_SENSOR_HUMIDITY);&lt;br /&gt;
	   if(val != -1) &lt;br /&gt;
      	   {&lt;br /&gt;
		s= (((0.0405*val) - 4) + ((-2.8 * 0.000001)*(pow(val,2))));  &lt;br /&gt;
      	  	dec = s;&lt;br /&gt;
      	  	frac = s - dec;&lt;br /&gt;
      	  	printf(&amp;quot;Humidity=%d.%02u %% (%d)\n&amp;quot;, dec, (unsigned int)(frac * 100),val);               &lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
           val = light_sensor.value(LIGHT_SENSOR_TOTAL_SOLAR);&lt;br /&gt;
      	   if(val != -1) &lt;br /&gt;
      	   {&lt;br /&gt;
      		s = (float)(val * 0.4071);&lt;br /&gt;
      	  	dec = s;&lt;br /&gt;
      	  	frac = s - dec;&lt;br /&gt;
      	  	printf(&amp;quot;Light=%d.%02u lux (%d)\n&amp;quot;, dec, (unsigned int)(frac * 100),val);               &lt;br /&gt;
           } &lt;br /&gt;
	&lt;br /&gt;
	   etimer_reset(&amp;amp;et);&lt;br /&gt;
    	   SENSORS_DEACTIVATE(light_sensor);&lt;br /&gt;
    	   SENSORS_DEACTIVATE(sht11_sensor);&lt;br /&gt;
&lt;br /&gt;
      } //end of while&lt;br /&gt;
    &lt;br /&gt;
      PROCESS_END();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Understanding the Code ==&lt;br /&gt;
&lt;br /&gt;
The following piece of code sets the timer to repeat the iterations every 2 seconds.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
etimer_set(&amp;amp;et, CLOCK_SECOND * 2);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need to activate light_sensor for measuring the light intensity and the sht11_sensor for the measurement of temperature and humidity.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
SENSORS_ACTIVATE(light_sensor);&lt;br /&gt;
SENSORS_ACTIVATE(sht11_sensor);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following line is used to capture the actual sensor value,&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
val = sht11_sensor.value(SHT11_SENSOR_TEMP);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need to calibrate the sensor values by doing some calculations. You can find the calculations here: http://tinyos.stanford.edu/tinyos-wiki/index.php/Boomerang_ADC_Example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
s = ((0.01*val) - 39.60);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After we are done with the calculations, we need to deactivate the sensors.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
SENSORS_DEACTIVATE(light_sensor);&lt;br /&gt;
SENSORS_DEACTIVATE(sht11_sensor);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Source Code ==&lt;br /&gt;
&lt;br /&gt;
To download a copy of &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;sensor-acq.c&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;, click [https://drive.google.com/file/d/0B0-za4nfpimxc2NDX2xzdEhIeGc/view?usp=sharing here]&lt;br /&gt;
&lt;br /&gt;
[[Media:sensor-acq.docx]]&lt;br /&gt;
&lt;br /&gt;
Copy the downloaded .c file in the &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;/contiki/examples/rime&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; folder and follow the steps mentioned above.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
Edited by : Nitin&lt;br /&gt;
&lt;br /&gt;
Source Code added by : Mugdhe, Samarth&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=Contiki_tutorials&amp;diff=1829</id>
		<title>Contiki tutorials</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=Contiki_tutorials&amp;diff=1829"/>
				<updated>2016-09-28T02:18:37Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Main_Page | Back to Main Page]]&lt;br /&gt;
&lt;br /&gt;
== List of Tutorials ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;lt;pre style=&amp;quot;color: red&amp;quot;&amp;gt;Disclaimer: Please note that the following tutorials are a work in progress. Use at your own risk.&amp;lt;/pre&amp;gt;&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Completed&lt;br /&gt;
# [[Installation]]&lt;br /&gt;
# [[Hello World]]&lt;br /&gt;
# [[Broadcast Example]]&lt;br /&gt;
# [[Collect View]]&lt;br /&gt;
# [[Contiki build system]]&lt;br /&gt;
# [[Interfacing with Python]]&lt;br /&gt;
# [[Sensor acquisition]]&lt;br /&gt;
# [[Timers]]&lt;br /&gt;
# [[CFS-Coffee]]&lt;br /&gt;
# [[Cooja Simulator]]&lt;br /&gt;
# [[Network Stack]]&lt;br /&gt;
# [[RPL UDP]]&lt;br /&gt;
# [[Contiki Shell]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--Need review--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Starting&lt;br /&gt;
# [[RSS measurement]]&lt;br /&gt;
# [[RPL objective function &amp;amp; simulation using DGRM model in cooja]]&lt;br /&gt;
# [[MAC protocols in ContikiOS]]&lt;br /&gt;
# [[RPL Border Router]]&lt;br /&gt;
# [[REST example running on Cooja and Sky motes]]&lt;br /&gt;
# [[Trickle library]]&lt;br /&gt;
# [[Packetbuffer Basics]]&lt;br /&gt;
# [[Antelope(Database Management System)]]&lt;br /&gt;
# [[Mobility of Nodes in Cooja]]&lt;br /&gt;
# [[Contiki Coffee File System]]&lt;br /&gt;
# [[Contiki Programming Guide]]&lt;br /&gt;
# [[Analyse of a real 6LoWPAN network using a Contiki-based sniffer module]]&lt;br /&gt;
# [[Build your own application in Contiki]]&lt;br /&gt;
# [[Processes]] Yash&lt;br /&gt;
# [[RPL objective function modification and simulation in cooja]]&lt;br /&gt;
# [[Collect-view Code Details]] Pradipta&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre style=&amp;quot;color: red&amp;quot;&amp;gt;Be sure to include references in your tutorials, especially if you quote material from other sites!&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=Sensor_acquisition&amp;diff=1828</id>
		<title>Sensor acquisition</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=Sensor_acquisition&amp;diff=1828"/>
				<updated>2016-09-28T02:11:35Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article helps you learn how to create the Sensor Acquisition Program, which can be used to show the Temperature, Humidity and Light Intensity in the vicinity of Tmote Sky.&lt;br /&gt;
&lt;br /&gt;
== How to start an application in Contiki ==&lt;br /&gt;
&lt;br /&gt;
Every process should start with the PROCESS macro. It takes two arguments&lt;br /&gt;
*name: The variable name of the process structure.&lt;br /&gt;
*strname: The string representation of the process name.&lt;br /&gt;
&lt;br /&gt;
  PROCESS(name,strname)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Then comes another macro AUTOSTART_PROCESS(struct process &amp;amp;). AUTOSTART_PROCESSES automatically starts the process(es) given in the argument(s) when the module boots.&lt;br /&gt;
*&amp;amp;name: Reference to the process name.&lt;br /&gt;
&lt;br /&gt;
  AUTOSTART_PROCESS(struct process &amp;amp;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Then we call the PROCESS_THREAD function. This function is used to define the protothread of a process. The process is called whenever an event occurs in the system.Each process in the module requires an event handler under the PROCESS_THREAD macro.&lt;br /&gt;
*name: The variable name of the process structure.&lt;br /&gt;
*process_event_t: The variable of type character.If this variable is same as PROCESS_EVENT_EXIT then PROCESS_EXITHANDLER is invoked.&lt;br /&gt;
&lt;br /&gt;
  PROCESS_THREAD(name, process_event_t, process_data_t)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Then comes the PROCESS_BEGIN macro. This macro defines the beginning of a process, and must always appear in a PROCESS_THREAD() definition.&lt;br /&gt;
&lt;br /&gt;
  PROCESS_BEGIN()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Then we write the set of C statements as per the requirement of the application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
At the end we use another macro PROCESS_END. This macro defines the end of a process. It must appear in a PROCESS_THREAD() definition and must always be included. The process exits when the PROCESS_END() macro is reached.&lt;br /&gt;
&lt;br /&gt;
  PROCESS_END()&lt;br /&gt;
&lt;br /&gt;
== Generic Template for Creating Application in Contiki  ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
PROCESS(name,strname);&lt;br /&gt;
AUTOSTART_PROCESSES(struct process &amp;amp;);&lt;br /&gt;
&lt;br /&gt;
PROCESS_THREAD(name, process_event_t, process_data_t)&lt;br /&gt;
{ &lt;br /&gt;
&lt;br /&gt;
----Initialization of required variables----&lt;br /&gt;
&lt;br /&gt;
PROCESS_BEGIN();&lt;br /&gt;
&lt;br /&gt;
---Set of C statements---&lt;br /&gt;
&lt;br /&gt;
PROCESS_END();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Sensor Acquisition Program on the Tmote Sky! ==&lt;br /&gt;
&lt;br /&gt;
=== Step 1 ===&lt;br /&gt;
&lt;br /&gt;
Using the opened terminal window compile and upload the Sensor Acquisition program on the Tmote Sky.&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;code&amp;gt;make TARGET=sky savetarget&amp;lt;/code&amp;gt;    (This saves the target for any future compilations)&lt;br /&gt;
:: &amp;lt;code&amp;gt;make sensor-acq.upload&amp;lt;/code&amp;gt;    (This will upload the code on the Tmote Sky)&lt;br /&gt;
:: &amp;lt;code&amp;gt;make login&amp;lt;/code&amp;gt;    (This will enable us to view the output. If permission error occurs, use sudo command at the beginning)&lt;br /&gt;
See the following link for troubleshooting - http://anrg.usc.edu/contiki/index.php/Troubleshooting&lt;br /&gt;
&lt;br /&gt;
=== Step 2 ===&lt;br /&gt;
&lt;br /&gt;
Press the reset button on the Tmote Sky. The following message will appear on the terminal window &lt;br /&gt;
&lt;br /&gt;
  Temperature=28.04 C (6764)&lt;br /&gt;
  Humidity=73.20% (2259)&lt;br /&gt;
  Light=38.26 lux (94)&lt;br /&gt;
&lt;br /&gt;
Values inside the round brackets (ex 6764, 2259 and 94) are the actual sensor values and the calibrated values can be obtained by performing calculations on these sensor values. You can find the calculations at&lt;br /&gt;
http://tinyos.stanford.edu/tinyos-wiki/index.php/Boomerang_ADC_Example&lt;br /&gt;
&lt;br /&gt;
=== Step 3 ===&lt;br /&gt;
&lt;br /&gt;
Press Ctrl-C to quit.&lt;br /&gt;
&lt;br /&gt;
== Code ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
PROCESS(sensor_acq_process,&amp;quot;Sensor Acquisition&amp;quot;);&lt;br /&gt;
AUTOSTART_PROCESSES(&amp;amp;sensor_acq_process);&lt;br /&gt;
&lt;br /&gt;
PROCESS_THREAD(sensor_acq_process,ev,data)&lt;br /&gt;
{ &lt;br /&gt;
      static struct etimer et;&lt;br /&gt;
      static int val;&lt;br /&gt;
      static float s = 0;&lt;br /&gt;
      static int dec;&lt;br /&gt;
      static float frac;&lt;br /&gt;
&lt;br /&gt;
      PROCESS_BEGIN();&lt;br /&gt;
&lt;br /&gt;
      printf(&amp;quot;Starting Sensor Example.\n&amp;quot;);&lt;br /&gt;
      &lt;br /&gt;
      while(1)&lt;br /&gt;
      {&lt;br /&gt;
	   etimer_set(&amp;amp;et, CLOCK_SECOND * 2);&lt;br /&gt;
	   SENSORS_ACTIVATE(light_sensor);&lt;br /&gt;
      	   SENSORS_ACTIVATE(sht11_sensor);&lt;br /&gt;
        &lt;br /&gt;
	   PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&amp;amp;et));&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
           val = sht11_sensor.value(SHT11_SENSOR_TEMP);&lt;br /&gt;
      	   if(val != -1) &lt;br /&gt;
      	   {&lt;br /&gt;
		s= ((0.01*val) - 39.60);&lt;br /&gt;
      	  	dec = s;&lt;br /&gt;
      	  	frac = s - dec;&lt;br /&gt;
      	  	printf(&amp;quot;\nTemperature=%d.%02u C (%d)\n&amp;quot;, dec, (unsigned int)(frac * 100),val);               &lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
	   val=sht11_sensor.value(SHT11_SENSOR_HUMIDITY);&lt;br /&gt;
	   if(val != -1) &lt;br /&gt;
      	   {&lt;br /&gt;
		s= (((0.0405*val) - 4) + ((-2.8 * 0.000001)*(pow(val,2))));  &lt;br /&gt;
      	  	dec = s;&lt;br /&gt;
      	  	frac = s - dec;&lt;br /&gt;
      	  	printf(&amp;quot;Humidity=%d.%02u %% (%d)\n&amp;quot;, dec, (unsigned int)(frac * 100),val);               &lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
           val = light_sensor.value(LIGHT_SENSOR_TOTAL_SOLAR);&lt;br /&gt;
      	   if(val != -1) &lt;br /&gt;
      	   {&lt;br /&gt;
      		s = (float)(val * 0.4071);&lt;br /&gt;
      	  	dec = s;&lt;br /&gt;
      	  	frac = s - dec;&lt;br /&gt;
      	  	printf(&amp;quot;Light=%d.%02u lux (%d)\n&amp;quot;, dec, (unsigned int)(frac * 100),val);               &lt;br /&gt;
           } &lt;br /&gt;
	&lt;br /&gt;
	   etimer_reset(&amp;amp;et);&lt;br /&gt;
    	   SENSORS_DEACTIVATE(light_sensor);&lt;br /&gt;
    	   SENSORS_DEACTIVATE(sht11_sensor);&lt;br /&gt;
&lt;br /&gt;
      } //end of while&lt;br /&gt;
    &lt;br /&gt;
      PROCESS_END();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Understanding the Code ==&lt;br /&gt;
&lt;br /&gt;
The following piece of code sets the timer to repeat the iterations every 2 seconds.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
etimer_set(&amp;amp;et, CLOCK_SECOND * 2);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need to activate light_sensor for measuring the light intensity and the sht11_sensor for the measurement of temperature and humidity.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
SENSORS_ACTIVATE(light_sensor);&lt;br /&gt;
SENSORS_ACTIVATE(sht11_sensor);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following line is used to capture the actual sensor value,&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
val = sht11_sensor.value(SHT11_SENSOR_TEMP);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We need to calibrate the sensor values by doing some calculations. You can find the calculations here: http://tinyos.stanford.edu/tinyos-wiki/index.php/Boomerang_ADC_Example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
s = ((0.01*val) - 39.60);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After we are done with the calculations, we need to deactivate the sensors.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
SENSORS_DEACTIVATE(light_sensor);&lt;br /&gt;
SENSORS_DEACTIVATE(sht11_sensor);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Source Code ==&lt;br /&gt;
&lt;br /&gt;
To download a copy of &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;sensor-acq.c&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;, click [https://drive.google.com/file/d/0B0-za4nfpimxc2NDX2xzdEhIeGc/view?usp=sharing here]&lt;br /&gt;
&lt;br /&gt;
[[Media:sensor-acq.docx]]&lt;br /&gt;
&lt;br /&gt;
Copy the downloaded .c file in the &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;/contiki/examples/rime&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; folder and follow the steps mentioned above.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
Edited by : Nitin&lt;br /&gt;
&lt;br /&gt;
Source Code added by : Mugdhe, Samarth&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=Interfacing_with_Python&amp;diff=1827</id>
		<title>Interfacing with Python</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=Interfacing_with_Python&amp;diff=1827"/>
				<updated>2016-09-28T02:06:57Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In this tutorial you will learn how to programmatically communicate with the Tmote Sky using the Python programming language. This will enable you to do such things as process data from your mote in real-time. In addition, you can send commands or data back to the mote to impact its operation. The possibilities are endless.&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
This tutorial was written and tested using the following:&lt;br /&gt;
* Contiki 2.7&lt;br /&gt;
* Python 2.7.3 (and [http://pyserial.sourceforge.net/ pySerial])&lt;br /&gt;
* Ubuntu 12.04&lt;br /&gt;
* Tmote Sky&lt;br /&gt;
&lt;br /&gt;
Connect the Tmote Sky to your computer using a USB cable and install the Sky Shell example. See the @Sky Shell@ tutorial for instructions on how to do this. We use this example so that we’ll have data to ensure that we are correctly reading and writing to the mote.&lt;br /&gt;
&lt;br /&gt;
=== Step 1 ===&lt;br /&gt;
&lt;br /&gt;
Create a Python file called serial.py containing the following code:&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/python&lt;br /&gt;
import serial&lt;br /&gt;
ser = serial.Serial(&lt;br /&gt;
   port=&amp;#039;/dev/ttyUSB0&amp;#039;,\&lt;br /&gt;
   baudrate=115200,\&lt;br /&gt;
   parity=serial.PARITY_NONE,\&lt;br /&gt;
   stopbits=serial.STOPBITS_ONE,\&lt;br /&gt;
   bytesize=serial.EIGHTBITS,\&lt;br /&gt;
   timeout=0)&lt;br /&gt;
print(&amp;quot;connected to: &amp;quot; + ser.portstr)&lt;br /&gt;
ser.write(&amp;quot;help\n&amp;quot;);&lt;br /&gt;
while True:&lt;br /&gt;
	line = ser.readline();&lt;br /&gt;
	if line:&lt;br /&gt;
		print(line),&lt;br /&gt;
ser.close()&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: &lt;br /&gt;
This code assumes the mote you are using for this tutorial is connected to &amp;#039;&amp;#039;/dev/ttyUSB0&amp;#039;&amp;#039;. Please check this using the &amp;#039;&amp;#039;make motelist&amp;#039;&amp;#039; command as shown in the figure below. If the mote you are using is connected to a different device file, please modify the Python code accordingly.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Motelist.jpg|center|Sample output of &amp;#039;&amp;#039;make motelist&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Step 2 ===&lt;br /&gt;
&lt;br /&gt;
Run the Python code using the command:&lt;br /&gt;
&lt;br /&gt;
  python serial.py&lt;br /&gt;
&lt;br /&gt;
If you encounter any errors, see the following link for troubleshooting - http://anrg.usc.edu/contiki/index.php/Troubleshooting&lt;br /&gt;
&lt;br /&gt;
You should see output similar to the following:&lt;br /&gt;
  connected to: /dev/ttyUSB0&lt;br /&gt;
  Available commands:&lt;br /&gt;
  ?: shows this help&lt;br /&gt;
  binprint: print binary data in decimal format&lt;br /&gt;
  blink [num]: blink LEDs ([num] times)&lt;br /&gt;
  collect: collect data from the network&lt;br /&gt;
  collect-view-data: sensor data, power consumption, network stats&lt;br /&gt;
  echo &amp;lt;text&amp;gt;: print &amp;lt;text&amp;gt;&lt;br /&gt;
  energy: print energy profile&lt;br /&gt;
  exit: exit shell&lt;br /&gt;
  hd: print binary data in hexadecimal format&lt;br /&gt;
  help: shows this help&lt;br /&gt;
  kill &amp;lt;command&amp;gt;: stop a specific command&lt;br /&gt;
  killall: stop all running commands&lt;br /&gt;
  mac &amp;lt;onoroff&amp;gt;: turn MAC protocol on (1) or off (0)&lt;br /&gt;
  netcmd &amp;lt;command&amp;gt;: run a command on all nodes in the network&lt;br /&gt;
  nodeid: set node ID&lt;br /&gt;
  null: discard input&lt;br /&gt;
  packetize: put data into one packet&lt;br /&gt;
  power: print power profile&lt;br /&gt;
  powerconv: convert power profile to human readable output&lt;br /&gt;
  powertrace [interval]: turn powertracing on or off, with reporting interval &amp;lt;interval&amp;gt;&lt;br /&gt;
  quit: exit shell&lt;br /&gt;
  randwait &amp;lt;maxtime&amp;gt; &amp;lt;command&amp;gt;: wait for a random time before running a command&lt;br /&gt;
  reboot: reboot the system&lt;br /&gt;
  repeat &amp;lt;num&amp;gt; &amp;lt;time&amp;gt; &amp;lt;command&amp;gt;: run a command every &amp;lt;time&amp;gt; seconds&lt;br /&gt;
  rfchannel &amp;lt;channel&amp;gt;: change CC2420 radio channel (11 - 26)&lt;br /&gt;
  routes: dump route list in binary format&lt;br /&gt;
  send &amp;lt;rexmits&amp;gt;: send data to the collector node, with rexmits hop-by-hop retransmissions&lt;br /&gt;
  sense: print out sensor data&lt;br /&gt;
  senseconv: convert &amp;#039;sense&amp;#039; data to human readable format&lt;br /&gt;
  size: print the size of the input&lt;br /&gt;
  time [seconds]: output time in binary format, or set time in seconds since 1970&lt;br /&gt;
  timestamp: prepend a timestamp to data&lt;br /&gt;
  txpower &amp;lt;power&amp;gt;: change CC2420 transmission power (0 - 31)&lt;br /&gt;
  5.0: Contiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Understanding the Code ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
 import serial&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first line will include the module for the serial port API. This API is provided by pySerial.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
ser = serial.Serial(&lt;br /&gt;
    port=&amp;#039;/dev/ttyUSB0&amp;#039;,\&lt;br /&gt;
    baudrate=115200,\&lt;br /&gt;
    parity=serial.PARITY_NONE,\&lt;br /&gt;
    stopbits=serial.STOPBITS_ONE,\&lt;br /&gt;
    bytesize=serial.EIGHTBITS,\&lt;br /&gt;
    timeout=0)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This next line instantiates an object with the appropriate serial port configuration for communication with the Sky mote. The necessary settings are shown in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin: auto;&amp;quot;&lt;br /&gt;
!colspan=&amp;quot;2&amp;quot;|Serial Configuration&lt;br /&gt;
|-&lt;br /&gt;
| Baud rate || 115200&lt;br /&gt;
|-&lt;br /&gt;
| Data bits || 8&lt;br /&gt;
|-&lt;br /&gt;
| Parity || None&lt;br /&gt;
|-&lt;br /&gt;
| Stop bits || 1&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
 print(&amp;quot;connected to: &amp;quot; + ser.portstr)&lt;br /&gt;
&amp;lt;/soource&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The print statement is simply a sanity check. It just reminds us which port we are connected to.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
 ser.write(&amp;quot;help\n&amp;quot;);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This line demonstrates how data can be written to the serial port. In this example we send the command &amp;quot;help&amp;quot; to the Sky Shell so that it will send back the list of shell commands it supports.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
while True:&lt;br /&gt;
     line = ser.readline();&lt;br /&gt;
     if line:&lt;br /&gt;
	print(line)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here, we read data from the serial port indefinitely and print it out, one line at a time. Note that in this case, &amp;#039;&amp;#039;line&amp;#039;&amp;#039; will typically end with a newline.&lt;br /&gt;
&lt;br /&gt;
To terminate the program, you can use Ctrl + C to send an interrupt signal.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
 ser.close()&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This last line never actually gets executed but it shows how the serial resource can be freed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=Interfacing_with_Python&amp;diff=1826</id>
		<title>Interfacing with Python</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=Interfacing_with_Python&amp;diff=1826"/>
				<updated>2016-09-28T02:03:01Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* Understanding the Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
In this tutorial you will learn how to programmatically communicate with the Tmote Sky using the Python programming language. This will enable you to do such things as process data from your mote in real-time. In addition, you can send commands or data back to the mote to impact its operation. The possibilities are endless.&lt;br /&gt;
&lt;br /&gt;
== Setup ==&lt;br /&gt;
This tutorial was written and tested using the following:&lt;br /&gt;
* Contiki 2.7&lt;br /&gt;
* Python 2.7.3 (and [http://pyserial.sourceforge.net/ pySerial])&lt;br /&gt;
* Ubuntu 12.04&lt;br /&gt;
* Tmote Sky&lt;br /&gt;
&lt;br /&gt;
Connect the Tmote Sky to your computer using a USB cable and install the Sky Shell example. See the @Sky Shell@ tutorial for instructions on how to do this. We use this example so that we’ll have data to ensure that we are correctly reading and writing to the mote.&lt;br /&gt;
&lt;br /&gt;
=== Step 1 ===&lt;br /&gt;
&lt;br /&gt;
Create a Python file called serial.py containing the following code:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
#!/usr/bin/python&lt;br /&gt;
import serial&lt;br /&gt;
ser = serial.Serial(&lt;br /&gt;
   port=&amp;#039;/dev/ttyUSB0&amp;#039;,\&lt;br /&gt;
   baudrate=115200,\&lt;br /&gt;
   parity=serial.PARITY_NONE,\&lt;br /&gt;
   stopbits=serial.STOPBITS_ONE,\&lt;br /&gt;
   bytesize=serial.EIGHTBITS,\&lt;br /&gt;
   timeout=0)&lt;br /&gt;
print(&amp;quot;connected to: &amp;quot; + ser.portstr)&lt;br /&gt;
ser.write(&amp;quot;help\n&amp;quot;);&lt;br /&gt;
while True:&lt;br /&gt;
	line = ser.readline();&lt;br /&gt;
	if line:&lt;br /&gt;
		print(line),&lt;br /&gt;
ser.close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Note: &lt;br /&gt;
This code assumes the mote you are using for this tutorial is connected to &amp;#039;&amp;#039;/dev/ttyUSB0&amp;#039;&amp;#039;. Please check this using the &amp;#039;&amp;#039;make motelist&amp;#039;&amp;#039; command as shown in the figure below. If the mote you are using is connected to a different device file, please modify the Python code accordingly.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Motelist.jpg|center|Sample output of &amp;#039;&amp;#039;make motelist&amp;#039;&amp;#039;]]&lt;br /&gt;
&lt;br /&gt;
=== Step 2 ===&lt;br /&gt;
&lt;br /&gt;
Run the Python code using the command:&lt;br /&gt;
&lt;br /&gt;
 python serial.py&lt;br /&gt;
&lt;br /&gt;
If you encounter any errors, see the following link for troubleshooting - http://anrg.usc.edu/contiki/index.php/Troubleshooting&lt;br /&gt;
&lt;br /&gt;
You should see output similar to the following:&lt;br /&gt;
 connected to: /dev/ttyUSB0&lt;br /&gt;
 Available commands:&lt;br /&gt;
 ?: shows this help&lt;br /&gt;
 binprint: print binary data in decimal format&lt;br /&gt;
 blink [num]: blink LEDs ([num] times)&lt;br /&gt;
 collect: collect data from the network&lt;br /&gt;
 collect-view-data: sensor data, power consumption, network stats&lt;br /&gt;
 echo &amp;lt;text&amp;gt;: print &amp;lt;text&amp;gt;&lt;br /&gt;
 energy: print energy profile&lt;br /&gt;
 exit: exit shell&lt;br /&gt;
 hd: print binary data in hexadecimal format&lt;br /&gt;
 help: shows this help&lt;br /&gt;
 kill &amp;lt;command&amp;gt;: stop a specific command&lt;br /&gt;
 killall: stop all running commands&lt;br /&gt;
 mac &amp;lt;onoroff&amp;gt;: turn MAC protocol on (1) or off (0)&lt;br /&gt;
 netcmd &amp;lt;command&amp;gt;: run a command on all nodes in the network&lt;br /&gt;
 nodeid: set node ID&lt;br /&gt;
 null: discard input&lt;br /&gt;
 packetize: put data into one packet&lt;br /&gt;
 power: print power profile&lt;br /&gt;
 powerconv: convert power profile to human readable output&lt;br /&gt;
 powertrace [interval]: turn powertracing on or off, with reporting interval &amp;lt;interval&amp;gt;&lt;br /&gt;
 quit: exit shell&lt;br /&gt;
 randwait &amp;lt;maxtime&amp;gt; &amp;lt;command&amp;gt;: wait for a random time before running a command&lt;br /&gt;
 reboot: reboot the system&lt;br /&gt;
 repeat &amp;lt;num&amp;gt; &amp;lt;time&amp;gt; &amp;lt;command&amp;gt;: run a command every &amp;lt;time&amp;gt; seconds&lt;br /&gt;
 rfchannel &amp;lt;channel&amp;gt;: change CC2420 radio channel (11 - 26)&lt;br /&gt;
 routes: dump route list in binary format&lt;br /&gt;
 send &amp;lt;rexmits&amp;gt;: send data to the collector node, with rexmits hop-by-hop retransmissions&lt;br /&gt;
 sense: print out sensor data&lt;br /&gt;
 senseconv: convert &amp;#039;sense&amp;#039; data to human readable format&lt;br /&gt;
 size: print the size of the input&lt;br /&gt;
 time [seconds]: output time in binary format, or set time in seconds since 1970&lt;br /&gt;
 timestamp: prepend a timestamp to data&lt;br /&gt;
 txpower &amp;lt;power&amp;gt;: change CC2420 transmission power (0 - 31)&lt;br /&gt;
 5.0: Contiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Understanding the Code ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
 import serial&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The first line will include the module for the serial port API. This API is provided by pySerial.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
ser = serial.Serial(&lt;br /&gt;
    port=&amp;#039;/dev/ttyUSB0&amp;#039;,\&lt;br /&gt;
    baudrate=115200,\&lt;br /&gt;
    parity=serial.PARITY_NONE,\&lt;br /&gt;
    stopbits=serial.STOPBITS_ONE,\&lt;br /&gt;
    bytesize=serial.EIGHTBITS,\&lt;br /&gt;
    timeout=0)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This next line instantiates an object with the appropriate serial port configuration for communication with the Sky mote. The necessary settings are shown in the table below.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot; style=&amp;quot;margin: auto;&amp;quot;&lt;br /&gt;
!colspan=&amp;quot;2&amp;quot;|Serial Configuration&lt;br /&gt;
|-&lt;br /&gt;
| Baud rate || 115200&lt;br /&gt;
|-&lt;br /&gt;
| Data bits || 8&lt;br /&gt;
|-&lt;br /&gt;
| Parity || None&lt;br /&gt;
|-&lt;br /&gt;
| Stop bits || 1&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
 print(&amp;quot;connected to: &amp;quot; + ser.portstr)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The print statement is simply a sanity check. It just reminds us which port we are connected to.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
 ser.write(&amp;quot;help\n&amp;quot;);&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This line demonstrates how data can be written to the serial port. In this example we send the command &amp;quot;help&amp;quot; to the Sky Shell so that it will send back the list of shell commands it supports.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
while True:&lt;br /&gt;
     line = ser.readline();&lt;br /&gt;
     if line:&lt;br /&gt;
	print(line)&lt;br /&gt;
&amp;lt;/syntaxhighlight &amp;gt;&lt;br /&gt;
&lt;br /&gt;
Here, we read data from the serial port indefinitely and print it out, one line at a time. Note that in this case, &amp;#039;&amp;#039;line&amp;#039;&amp;#039; will typically end with a newline.&lt;br /&gt;
&lt;br /&gt;
To terminate the program, you can use Ctrl + C to send an interrupt signal.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
 ser.close()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This last line never actually gets executed but it shows how the serial resource can be freed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=Collect_View&amp;diff=1825</id>
		<title>Collect View</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=Collect_View&amp;diff=1825"/>
				<updated>2016-09-28T02:01:19Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This exercise is to familiarize the user with the collect view app provided in the Contiki OS source code. It basically involves a sink mote and one or more than one source motes. The source motes send their sensor, network, power and node properties to the sink. A java app presents all this compiled data in a graphical manner.&lt;br /&gt;
&lt;br /&gt;
== You Will Learn ==&lt;br /&gt;
&lt;br /&gt;
* How to upload an introductory code for UDP source and sink.&lt;br /&gt;
* How to program nodes and collect data through the collect-view GUI.&lt;br /&gt;
* Things to see on the collect-view GUI.&lt;br /&gt;
&lt;br /&gt;
== Relevant Directories ==&lt;br /&gt;
&lt;br /&gt;
* /contiki/examples/collect/ - This has the shell code for collect view and the various targets.&lt;br /&gt;
* /contiki/examples/ipv6/rpl-collect/ - This has the codes which are to be uploaded to the source and sink motes for UDP communication.&lt;br /&gt;
* /contiki/tools/collect-view/ - This contains the java file for the collect view GUI.&lt;br /&gt;
* /contiki/apps/collect-view/ - This contains the codes corresponding to the targets mentioned in the examples folder.&lt;br /&gt;
&lt;br /&gt;
== Connect the Tmote Sky ==&lt;br /&gt;
&lt;br /&gt;
Before continuing with the tutorial, plug 2 Tmote Sky into the computer&amp;#039;s USB port, one for the source and the other for the sink.&lt;br /&gt;
&lt;br /&gt;
In order to transfer the ownership of the contiki directories use the following command. &lt;br /&gt;
&lt;br /&gt;
  sudo chown :user /dev/ttyUSB0&lt;br /&gt;
&lt;br /&gt;
  sudo chown :user /dev/ttyUSB1&lt;br /&gt;
&lt;br /&gt;
== Using the Source Code ==&lt;br /&gt;
&lt;br /&gt;
=== Step 1 ===&lt;br /&gt;
&lt;br /&gt;
Go to /examples/collect/ directory.&lt;br /&gt;
&lt;br /&gt;
=== Step 2 ===&lt;br /&gt;
&lt;br /&gt;
Now we need to program one mote as the sink and the other as source. We will use a predefined target called sky to upload the code in the motes.&lt;br /&gt;
&lt;br /&gt;
For sink:&lt;br /&gt;
&lt;br /&gt;
  make TARGET=sky MOTES=/dev/ttyUSB0 collect-view-shell.upload&lt;br /&gt;
&lt;br /&gt;
For source:&lt;br /&gt;
&lt;br /&gt;
  make TARGET=sky MOTES=/dev/ttyUSB1 collect-view-shell.upload&lt;br /&gt;
&lt;br /&gt;
=== Step 3 ===&lt;br /&gt;
&lt;br /&gt;
Now we would like to see the transfer and depiction of data on the collect-view GUI. For that to happen, go to /tools/collect-view directory and run &amp;quot;ant&amp;quot; to build the collect viewer.&lt;br /&gt;
&lt;br /&gt;
  ant run&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Note&amp;#039;&amp;#039;&amp;#039; - In order to edit GUI java files go to the /src directory.&lt;br /&gt;
&lt;br /&gt;
=== Step 4 ===&lt;br /&gt;
&lt;br /&gt;
To view the data collected at the sink, go to the generated /tools/collect-view/dist and run - &lt;br /&gt;
&lt;br /&gt;
  java -jar collect-view.jar /dev/ttyUSB0&lt;br /&gt;
&lt;br /&gt;
=== Step 5 ===&lt;br /&gt;
&lt;br /&gt;
In the collect window from the Tools menu, select &amp;quot;Run Init script&amp;quot; and in the &amp;quot;Node Control&amp;quot; tab and click on &amp;quot;Start Collect.&amp;quot; This will enable the communication between the source and the sender and you will be able to see the received statistics in a graphical manner.&lt;br /&gt;
&lt;br /&gt;
== Using the GUI ==&lt;br /&gt;
&lt;br /&gt;
=== Step 1 ===&lt;br /&gt;
&lt;br /&gt;
Once the motes are connected to the USB ports, go to /tools/collect-view directory and run &amp;quot;ant&amp;quot; to build the collect viewer.&lt;br /&gt;
&lt;br /&gt;
  ant run&lt;br /&gt;
&lt;br /&gt;
=== Step 2 ===&lt;br /&gt;
&lt;br /&gt;
Press the &amp;quot;Program Nodes...&amp;quot; button as seen on the GUI.&lt;br /&gt;
&lt;br /&gt;
=== Step 3 ===&lt;br /&gt;
&lt;br /&gt;
Disconnect all except one node from the USB ports. Press the &amp;quot;Connect to serial&amp;quot; button. The disconnected motes should be connected to independent power sources.&lt;br /&gt;
&lt;br /&gt;
=== Step 3 ===&lt;br /&gt;
&lt;br /&gt;
Now in order to collect various statistics, press &amp;quot;Send Collect&amp;quot; button.&lt;br /&gt;
&lt;br /&gt;
=== Step 4 ===&lt;br /&gt;
&lt;br /&gt;
Set the various &amp;quot;Collect Settings&amp;quot; based on how frequently the data is to be collected and then press &amp;quot;Send command to nodes&amp;quot; button.&lt;br /&gt;
&lt;br /&gt;
=== Step 5 ===&lt;br /&gt;
&lt;br /&gt;
Now go through the other tabs on the GUI to study and analyze the statistics plotted.&lt;br /&gt;
&lt;br /&gt;
== Using Cooja Simulator ==&lt;br /&gt;
&lt;br /&gt;
=== Step 1 ===&lt;br /&gt;
&lt;br /&gt;
Go to /tools/cooja/ directory and run &amp;quot;ant&amp;quot; to start the simulator.&lt;br /&gt;
&lt;br /&gt;
  ant run&lt;br /&gt;
&lt;br /&gt;
When Cooja is up and running, it will start with a blue window.&lt;br /&gt;
&lt;br /&gt;
=== Step 2 ===&lt;br /&gt;
&lt;br /&gt;
To create a new simulation, click on the File menu, choose &amp;#039;New Simulation&amp;#039;, name the simulation and click on &amp;#039;Create&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
=== Step 3 ===&lt;br /&gt;
&lt;br /&gt;
To add Motes to the simulation, click on the Motes-&amp;gt; Add motes-&amp;gt; Create a new mote type-&amp;gt; and select the type of mote to be added in the network.&lt;br /&gt;
&lt;br /&gt;
In the &amp;#039;Create Mote Type&amp;#039; window, choose a suitable name for the mote type. In the &amp;#039;Contiki process/ Firmware&amp;#039; option, follow the path &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt; examples/collect&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; and select the &amp;#039;collect-view-shell.c&amp;#039; program. Compile and click Create.&lt;br /&gt;
&lt;br /&gt;
In the &amp;#039;Add mote&amp;#039; window that pops up, enter the number of motes and alter the position intervals as required and click &amp;#039;Add motes&amp;#039;. The number of motes added to the network is now shown in the &amp;#039;Network&amp;#039; window.&lt;br /&gt;
&lt;br /&gt;
=== Step 4 ===&lt;br /&gt;
&lt;br /&gt;
To start the simulation, Click on &amp;#039;Start&amp;#039; in the &amp;#039;Simulation Control&amp;#039; window.&lt;br /&gt;
&lt;br /&gt;
Right click on any mote in the &amp;#039;Network&amp;#039; window, click on &amp;#039;Mote tools for created mote type&amp;#039; and select &amp;#039;collect view&amp;#039;. The &amp;#039;Collect View&amp;#039; application starts.&lt;br /&gt;
&lt;br /&gt;
Set the various &amp;#039;Collect Settings&amp;#039; based on how frequently the data is to be collected, click on &amp;#039;Send command to nodes&amp;#039; button and then &amp;#039;Start Collect&amp;#039;. This will enable the communication between the source and the sender and you will be able to see the received statistics in a graphical manner.&lt;br /&gt;
&lt;br /&gt;
== Things to See on the GUI ==&lt;br /&gt;
&lt;br /&gt;
=== Topological graphs ===&lt;br /&gt;
&lt;br /&gt;
* Sensor Map&lt;br /&gt;
* Network Graph&lt;br /&gt;
&lt;br /&gt;
=== Sensor Related Plots ===&lt;br /&gt;
&lt;br /&gt;
These are subject to the availability of a particular type of sensor on the motes.&lt;br /&gt;
&lt;br /&gt;
* Temperature Sensor - Average Temperatue and Temperature.&lt;br /&gt;
* Humidity Sensor - Relative Humidity.&lt;br /&gt;
* Battery Sensor - Battery Indicator and Battery Voltage.&lt;br /&gt;
* Light Sensor - Light 1 and Light 2.&lt;br /&gt;
&lt;br /&gt;
=== Network Metrics Related Plots ===&lt;br /&gt;
&lt;br /&gt;
* Neighbours&lt;br /&gt;
* Beacon Interval&lt;br /&gt;
* Network Hops &lt;br /&gt;
** Over Time&lt;br /&gt;
** Per Node&lt;br /&gt;
* Router Metric (Over Time)&lt;br /&gt;
** Instantaneous&lt;br /&gt;
** Average&lt;br /&gt;
* ETX (Over Time)&lt;br /&gt;
* Next Hop (Over Time)&lt;br /&gt;
* Latency&lt;br /&gt;
* Lost Packets (Over Time)&lt;br /&gt;
* Received Packets&lt;br /&gt;
** Over Time&lt;br /&gt;
** Per Node&lt;br /&gt;
** Every 5 min&lt;br /&gt;
&lt;br /&gt;
=== Power Related Plots ===&lt;br /&gt;
&lt;br /&gt;
* Average Power&lt;br /&gt;
* Instantaneous Power&lt;br /&gt;
* Power History&lt;br /&gt;
* Radio Duty Cycle&lt;br /&gt;
&lt;br /&gt;
=== Other Tabs ===&lt;br /&gt;
&lt;br /&gt;
* Node Info - This gives a summary of all the nodes and their statistics.&lt;br /&gt;
* Serial Console - The user can interact directly with the mote, sending commands to read and transmit data.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Primary contributors:&amp;#039;&amp;#039;&amp;#039; Bhavna, Deepanker, Nikhil, Rahul, Mugdhe, Samarth&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Edited by: Deepanker&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=Broadcast_Example&amp;diff=1824</id>
		<title>Broadcast Example</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=Broadcast_Example&amp;diff=1824"/>
				<updated>2016-09-28T01:53:03Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This exercise is an introduction into programming the [http://www.capsil.org/capsilwiki/index.php/TELOSB/TMote_Sky Tmote Sky]. It uses the Rime stack to communicate with other Contiki nodes over the radio. You will run the broadcast example already found in the Contiki core folders to send your name from a base station via broadcast packets. The exercise sends broadcast packets at random intervals between 4 and 8 seconds such that all Tmote Sky boards nearby will receive the packet.&lt;br /&gt;
&lt;br /&gt;
== You will learn ==&lt;br /&gt;
&lt;br /&gt;
* How to program the Broadcast communication found within the Rime stack&lt;br /&gt;
* How to encapsulate data in packets&lt;br /&gt;
&lt;br /&gt;
== Connect the Tmote Sky ==&lt;br /&gt;
Before continuing with the tutorial, plug a Tmote Sky into the computer’s USB port. The LEDs on the skymote will start blinking to indicate that the mote is connected.&lt;br /&gt;
&lt;br /&gt;
=== Step 1 ===&lt;br /&gt;
&lt;br /&gt;
The first step is to change directories. At this point you should be familiar with the Contiki 2.7 core folders. The Rime stack is located within the examples folder, so type &lt;br /&gt;
&lt;br /&gt;
  cd contiki-2.7/examples/rime&lt;br /&gt;
&lt;br /&gt;
directly into terminal. Once inside the rime folder, use the ls command to view all the files within it. We will be working specifically with example-broadcast.c.&lt;br /&gt;
&lt;br /&gt;
=== Step 2 ===&lt;br /&gt;
&lt;br /&gt;
Next, we will edit the data in the example-broadcast.c file. In order to do this, you will need to open the file with your text editor. In this exercise, we use gedit, but feel free to use whichever text editor you are most comfortable with. Type the following into the terminal:&lt;br /&gt;
&lt;br /&gt;
  gedit example-broadcast.c&lt;br /&gt;
&lt;br /&gt;
This will open your text editor, allowing you to edit the code inside the broadcast packet.&lt;br /&gt;
&lt;br /&gt;
=== Step 3 ===&lt;br /&gt;
&lt;br /&gt;
To edit the actual data that your Tmote Sky will broadcast, scroll down to the line that contains: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
packetbuf_copyfrom(“Hello”,  6)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Change the character string to your name. Then change the number of characters accordingly. Remember to account for the &amp;quot;null character&amp;quot; associated with character strings in C when changing the number of characters. The line should now read&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
packetbuf_copyfrom(“yourName”,  9)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Save and exit the text editor.&lt;br /&gt;
&lt;br /&gt;
=== Step 4 ===&lt;br /&gt;
&lt;br /&gt;
In the next step we configure Tmote Sky. Connect your mote to your computer via USB. If you are using VirtualBox or Parallels, assure that the mote actually connects to your virtual machine and not the host computer. The LEDs on the mote will temporarily blink when connected to the laptop.  &lt;br /&gt;
&lt;br /&gt;
=== Step 5 ===&lt;br /&gt;
&lt;br /&gt;
In this step, we will compile and upload the Broadcast packet to your Tmote Sky. In the terminal window, type&lt;br /&gt;
&lt;br /&gt;
  make TARGET=sky example-broadcast.upload login&lt;br /&gt;
&lt;br /&gt;
You will notice that instead of typing example-broadcast.sky, we use example-broadcast.upload. The .upload tells Contiki to install the binary onto the attached mote. Additionally, we add login to use the terminal to communicate with the mote over the serial interface referred to by /dev/ttyUSB0. Note that login can be specified later in another call to make, i.e. make login.&lt;br /&gt;
&lt;br /&gt;
This should compile and upload the example-broadcast.c packet such that your mote will be able to broadcast your name. After the compilation is complete, the text broadcast message sent...  should appear in terminal every 2 to 4 seconds, indicating that your name (which we added within the text editor) is being broadcast to all other motes in the area.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Congratulations! You have successfully programmed the Tmote Sky! &amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039; If at any point, you would like to exit the compilation/broadcast stage, press CTRL-C&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Tip:&amp;#039;&amp;#039;&amp;#039; You can save the TARGET by typing make TARGET=sky savetarget into terminal to avoid compiling the target repeatedly.&lt;br /&gt;
&lt;br /&gt;
== Using Cooja Simulator ==&lt;br /&gt;
&lt;br /&gt;
Cooja is a network simulator included in the Contiki system that simulates networks of Contiki nodes.&lt;br /&gt;
&lt;br /&gt;
=== Step 1 ===&lt;br /&gt;
&lt;br /&gt;
Go to contiki folder and navigate to the tools/cooja/ directory and run &amp;quot;ant&amp;quot; to start the simulator.&lt;br /&gt;
&lt;br /&gt;
  ant run&lt;br /&gt;
&lt;br /&gt;
When Cooja is up and running, it will start with a blue window.&lt;br /&gt;
&lt;br /&gt;
=== Step 2 ===&lt;br /&gt;
&lt;br /&gt;
To create a new simulation, click on the File menu, choose &amp;#039;New Simulation&amp;#039;, name the simulation and click on &amp;#039;Create&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
The new simulation shows an empty &amp;#039;Network&amp;#039; window at the top left of the screen as we have not yet added any motes to our simulation. The &amp;#039;Simulation Control&amp;#039; window has options to Start, Pause and Reload the simulation. The &amp;#039;Notes&amp;#039; window provides us with a space to add notes for our simulation. The &amp;#039;Mote Output&amp;#039; window prints out all serial port activities for the motes connected in the simulation. The &amp;#039;Timeline&amp;#039; window provides real-time information of the events happening in the simulation.&lt;br /&gt;
&lt;br /&gt;
=== Step 3 ===&lt;br /&gt;
&lt;br /&gt;
To add Motes to the simulation, click on the Motes-&amp;gt; Add motes-&amp;gt; Create a new mote type-&amp;gt; and select the type of mote to be added in the network.&lt;br /&gt;
&lt;br /&gt;
In the &amp;#039;Create Mote Type&amp;#039; window, choose a suitable name for the mote type. In the &amp;#039;Contiki process/ Firmware&amp;#039; option, follow the path &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt; examples/ipv6/simple-udp-rpl&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; and select the &amp;#039;broadcast-example.c&amp;#039; program. &lt;br /&gt;
Compile and click Create.&lt;br /&gt;
&lt;br /&gt;
In the &amp;#039;Add mote&amp;#039; window that pops up, enter the number of motes and alter the position intervals as required and click &amp;#039;Add motes&amp;#039;. The number of motes added to the network is now shown in the &amp;#039;Network&amp;#039; window.&lt;br /&gt;
&lt;br /&gt;
=== Step 4 ===&lt;br /&gt;
&lt;br /&gt;
To start the simulation, Click on &amp;#039;Start&amp;#039; in the &amp;#039;Simulation Control&amp;#039; window.&lt;br /&gt;
&lt;br /&gt;
While the simulation is running, the &amp;#039;Mote output&amp;#039; window prints sender and receiver information.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;View&amp;#039; tab in the Network window can be used to display mote features such as,&lt;br /&gt;
&lt;br /&gt;
* Mote IDs- Displays the mote id&lt;br /&gt;
* Addresses: IP or Rime- Displays the IPv6 address of the mote&lt;br /&gt;
* Radio Traffic- Enables animation showing the communication among the motes&lt;br /&gt;
* Positions- Displays the co-ordinates of the mote&lt;br /&gt;
* Radio Environment- Clicking on a particular mote displays its coverage area&lt;br /&gt;
&lt;br /&gt;
== Understanding the code ==&lt;br /&gt;
The purpose of this example is to test the broadcast layer in Rime. A glance at core/net/rime/broadcast.h and core/net/rime/broadcast.c helps in the understanding underlying concepts of this example.&lt;br /&gt;
In the code, a process called example_broadcast_process is  kicked off by AUTOSTART_PROCESSES.&lt;br /&gt;
&lt;br /&gt;
==== Macros and Structures ====&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
PROCESS(name,strname)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Every [http://anrg.usc.edu/contiki/index.php?title=Process process] should be defined via the PROCESS macro.&lt;br /&gt;
PROCESS has two arguments: the variable of the process structure, and a human readable string name, which is used when debugging.&lt;br /&gt;
&lt;br /&gt;
*name: The variable name of the process structure.&lt;br /&gt;
*strname: The string representation of the process name.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
AUTOSTART_PROCESS(struct process &amp;amp;)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
AUTOSTART_PROCESSES automatically starts the process(es) given in the argument(s) when the module boots.&lt;br /&gt;
&lt;br /&gt;
*&amp;amp;name: Reference to the process name&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
broadcast_recv(struct broadcast_conn *, const rimeaddr_t *)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function parses an incoming packet and displays the message and the address of the sender.&lt;br /&gt;
By setting it as the broadcast&amp;#039;s designated callback function, broadcast_recv is automatically called when a packet is received.&lt;br /&gt;
&lt;br /&gt;
*broadcast_conn *:  This structure which has 2 structures : abc_conn, broadcast_callbacks *. The abc_conn is basic type of connection over which the broadcast connection is developed. And, the broadcast_callbacks point to recv and sent functions (in this example, just recv).&lt;br /&gt;
&lt;br /&gt;
*rimeaddr_t *: This is a union which has a character array u8[RIMEADDR_SIZE].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
PROCESS_THREAD(name, process_event_t, process_data_t)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A process in Contiki consists of a single reference to a &amp;quot;[http://anrg.usc.edu/contiki/index.php?title=Protothreads protothread]&amp;quot;. This function is used to define the protothread of a process. The process is called whenever an event occurs in the system. &lt;br /&gt;
Each process in the module requires an event handler under the PROCESS_THREAD macro.&lt;br /&gt;
&lt;br /&gt;
*name: The variable name of the process structure.&lt;br /&gt;
*process_event_t: The variable of type character.If this variable is same as PROCESS_EVENT_EXIT then PROCESS_EXITHANDLER is invoked.&lt;br /&gt;
&lt;br /&gt;
Within the body of PROCESS_THREAD there are 3 major tasks:&lt;br /&gt;
* Initialization&lt;br /&gt;
** allocate resources&lt;br /&gt;
** define variables&lt;br /&gt;
** begin process&lt;br /&gt;
* Infinite Loop&lt;br /&gt;
** while(1) is used to create an infinite loop in which the actual event-handling response takes place&lt;br /&gt;
* Deallocation&lt;br /&gt;
** end process&lt;br /&gt;
** deallocate resources&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
PROCESS_EXITHANDLER(handler)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Specifies an action when a process exits.&lt;br /&gt;
NOTE: This declaration must come immediately before the PROCESS_BEGIN() macro.&lt;br /&gt;
&lt;br /&gt;
* handler: The action to be performed.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
PROCESS_BEGIN()&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This macro defines the beginning of a process, and must always appear in a PROCESS_THREAD() definition.This macro initiates PT_BEGIN(), which is declared at the starting point of a protothread. All C statements above the PT_BEGIN() invokation will be executed each time the protothread is scheduled.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
broadcast_close(struct broadcast_conn *)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function closes a broadcast connection that has previously been opened with broadcast_open().This function typically is called as an exit handler.&lt;br /&gt;
&lt;br /&gt;
*broadcast_conn :  This is same as the variable from broadcast_recv().&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
PROCESS_END()&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This macro defines the end of a process. It must appear in a PROCESS_THREAD() definition and must always be included. The process exits when the PROCESS_END() macro is reached. This macro initiates PT_END().It must always be used together with a matching PT_BEGIN() macro.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
broadcast_open(struct broadcast_conn *, uint16_t ,const struct broadcast_callbacks *)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets up an identified best-effort broadcast connection. The caller will allocate the memory for the struct broadcast_conn, usually by declaring it as a static variable. The struct broadcast_callbacks pointer  points to a structure containing a pointer to a function that will be called when a packet arrives on the channel. This function opens a connection of type abc_conn and sets the callbacks to structure passed. Also, this points to channel_set_attributes() function.&lt;br /&gt;
&lt;br /&gt;
*broadcast_conn : A pointer to a struct broadcast_conn&lt;br /&gt;
*uint16_t:  The channel on which the connection will operate&lt;br /&gt;
*broadcast_callbacks :  A struct broadcast_callbacks with function pointers to functions that will be called when a packet has been received&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
etimer_set(struct etimer *, clock_time_t)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function is used to set an event timer for a time sometime in the future. When the event timer expires, the event PROCESS_EVENT_TIMER will be posted to the process that called the etimer_set() function.&lt;br /&gt;
&lt;br /&gt;
*etimer : A pointer to the event timer&lt;br /&gt;
*clock_time_t : The interval before the timer expires.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
static struct broadcast_conn&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The broadcast module sends a packet to all local area neighbors with a header that identifies the sender. It also adds a single-hop address as a packet attribute to out going packets. &lt;br /&gt;
broadcast_conn structure consists of two structures&lt;br /&gt;
*abc_conn struct: the abc(Anonymous best effort local area Broadcast) module sends packets to all local area neighbors. It uses one channel.&lt;br /&gt;
*broadcast_callbacks struct: this is called when a packet has been received by the broadcast module. The struct broadcast_callbacks pointer is used in broadcast_open to point  to a function that will be called when a packet arrives on the channel.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Edited by: Rahul, Nikhil, Bhavana, Lakshmi, Mugdhe, Samarth&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=Broadcast_Example&amp;diff=1823</id>
		<title>Broadcast Example</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=Broadcast_Example&amp;diff=1823"/>
				<updated>2016-09-28T01:46:27Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This exercise is an introduction into programming the [http://www.capsil.org/capsilwiki/index.php/TELOSB/TMote_Sky Tmote Sky]. It uses the Rime stack to communicate with other Contiki nodes over the radio. You will run the broadcast example already found in the Contiki core folders to send your name from a base station via broadcast packets. The exercise sends broadcast packets at random intervals between 4 and 8 seconds such that all Tmote Sky boards nearby will receive the packet.&lt;br /&gt;
&lt;br /&gt;
== You will learn ==&lt;br /&gt;
&lt;br /&gt;
* How to program the Broadcast communication found within the Rime stack&lt;br /&gt;
* How to encapsulate data in packets&lt;br /&gt;
&lt;br /&gt;
== Connect the Tmote Sky ==&lt;br /&gt;
Before continuing with the tutorial, plug a Tmote Sky into the computer’s USB port. The LEDs on the skymote will start blinking to indicate that the mote is connected.&lt;br /&gt;
&lt;br /&gt;
=== Step 1 ===&lt;br /&gt;
&lt;br /&gt;
The first step is to change directories. At this point you should be familiar with the Contiki 2.7 core folders. The Rime stack is located within the examples folder, so type &lt;br /&gt;
&lt;br /&gt;
  cd contiki-2.7/examples/rime&lt;br /&gt;
&lt;br /&gt;
directly into terminal. Once inside the rime folder, use the ls command to view all the files within it. We will be working specifically with example-broadcast.c.&lt;br /&gt;
&lt;br /&gt;
=== Step 2 ===&lt;br /&gt;
&lt;br /&gt;
Next, we will edit the data in the example-broadcast.c file. In order to do this, you will need to open the file with your text editor. In this exercise, we use gedit, but feel free to use whichever text editor you are most comfortable with. Type the following into the terminal:&lt;br /&gt;
&lt;br /&gt;
  gedit example-broadcast.c&lt;br /&gt;
&lt;br /&gt;
This will open your text editor, allowing you to edit the code inside the broadcast packet.&lt;br /&gt;
&lt;br /&gt;
=== Step 3 ===&lt;br /&gt;
&lt;br /&gt;
To edit the actual data that your Tmote Sky will broadcast, scroll down to the line that contains: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
packetbuf_copyfrom(“Hello”,  6)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
Change the character string to your name. Then change the number of characters accordingly. Remember to account for the &amp;quot;null character&amp;quot; associated with character strings in C when changing the number of characters. The line should now read&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt; packetbuf_copyfrom(“yourName”,  9) &amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Save and exit the text editor.&lt;br /&gt;
&lt;br /&gt;
=== Step 4 ===&lt;br /&gt;
&lt;br /&gt;
In the next step we configure Tmote Sky. Connect your mote to your computer via USB. If you are using VirtualBox or Parallels, assure that the mote actually connects to your virtual machine and not the host computer. The LEDs on the mote will temporarily blink when connected to the laptop.  &lt;br /&gt;
&lt;br /&gt;
=== Step 5 ===&lt;br /&gt;
&lt;br /&gt;
In this step, we will compile and upload the Broadcast packet to your Tmote Sky. In the terminal window, type&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt; make TARGET=sky example-broadcast.upload login &amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You will notice that instead of typing example-broadcast.sky, we use example-broadcast.upload. The .upload tells Contiki to install the binary onto the attached mote. Additionally, we add login to use the terminal to communicate with the mote over the serial interface referred to by /dev/ttyUSB0. Note that login can be specified later in another call to make, i.e. make login.&lt;br /&gt;
&lt;br /&gt;
This should compile and upload the example-broadcast.c packet such that your mote will be able to broadcast your name. After the compilation is complete, the text broadcast message sent...  should appear in terminal every 2 to 4 seconds, indicating that your name (which we added within the text editor) is being broadcast to all other motes in the area.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Congratulations! You have successfully programmed the Tmote Sky! &amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Note:&amp;#039;&amp;#039;&amp;#039; If at any point, you would like to exit the compilation/broadcast stage, press CTRL-C&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Tip:&amp;#039;&amp;#039;&amp;#039; You can save the TARGET by typing make TARGET=sky savetarget into terminal to avoid compiling the target repeatedly.&lt;br /&gt;
&lt;br /&gt;
== Using Cooja Simulator ==&lt;br /&gt;
&lt;br /&gt;
Cooja is a network simulator included in the Contiki system that simulates networks of Contiki nodes.&lt;br /&gt;
&lt;br /&gt;
=== Step 1 ===&lt;br /&gt;
&lt;br /&gt;
Go to contiki folder and navigate to the tools/cooja/ directory and run &amp;quot;ant&amp;quot; to start the simulator.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt; ant run &amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
When Cooja is up and running, it will start with a blue window.&lt;br /&gt;
&lt;br /&gt;
=== Step 2 ===&lt;br /&gt;
&lt;br /&gt;
To create a new simulation, click on the File menu, choose &amp;#039;New Simulation&amp;#039;, name the simulation and click on &amp;#039;Create&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
The new simulation shows an empty &amp;#039;Network&amp;#039; window at the top left of the screen as we have not yet added any motes to our simulation. The &amp;#039;Simulation Control&amp;#039; window has options to Start, Pause and Reload the simulation. The &amp;#039;Notes&amp;#039; window provides us with a space to add notes for our simulation. The &amp;#039;Mote Output&amp;#039; window prints out all serial port activities for the motes connected in the simulation. The &amp;#039;Timeline&amp;#039; window provides real-time information of the events happening in the simulation.&lt;br /&gt;
&lt;br /&gt;
=== Step 3 ===&lt;br /&gt;
&lt;br /&gt;
To add Motes to the simulation, click on the Motes-&amp;gt; Add motes-&amp;gt; Create a new mote type-&amp;gt; and select the type of mote to be added in the network.&lt;br /&gt;
&lt;br /&gt;
In the &amp;#039;Create Mote Type&amp;#039; window, choose a suitable name for the mote type. In the &amp;#039;Contiki process/ Firmware&amp;#039; option, follow the path &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt; examples/ipv6/simple-udp-rpl&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; and select the &amp;#039;broadcast-example.c&amp;#039; program. &lt;br /&gt;
Compile and click Create.&lt;br /&gt;
&lt;br /&gt;
In the &amp;#039;Add mote&amp;#039; window that pops up, enter the number of motes and alter the position intervals as required and click &amp;#039;Add motes&amp;#039;. The number of motes added to the network is now shown in the &amp;#039;Network&amp;#039; window.&lt;br /&gt;
&lt;br /&gt;
=== Step 4 ===&lt;br /&gt;
&lt;br /&gt;
To start the simulation, Click on &amp;#039;Start&amp;#039; in the &amp;#039;Simulation Control&amp;#039; window.&lt;br /&gt;
&lt;br /&gt;
While the simulation is running, the &amp;#039;Mote output&amp;#039; window prints sender and receiver information.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;View&amp;#039; tab in the Network window can be used to display mote features such as,&lt;br /&gt;
&lt;br /&gt;
* Mote IDs- Displays the mote id&lt;br /&gt;
* Addresses: IP or Rime- Displays the IPv6 address of the mote&lt;br /&gt;
* Radio Traffic- Enables animation showing the communication among the motes&lt;br /&gt;
* Positions- Displays the co-ordinates of the mote&lt;br /&gt;
* Radio Environment- Clicking on a particular mote displays its coverage area&lt;br /&gt;
&lt;br /&gt;
== Understanding the code ==&lt;br /&gt;
The purpose of this example is to test the broadcast layer in Rime. A glance at core/net/rime/broadcast.h and core/net/rime/broadcast.c helps in the understanding underlying concepts of this example.&lt;br /&gt;
In the code, a process called example_broadcast_process is  kicked off by AUTOSTART_PROCESSES.&lt;br /&gt;
&lt;br /&gt;
==== Macros and Structures ====&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt; PROCESS(name,strname) &amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Every [http://anrg.usc.edu/contiki/index.php?title=Process process] should be defined via the PROCESS macro.&lt;br /&gt;
PROCESS has two arguments: the variable of the process structure, and a human readable string name, which is used when debugging.&lt;br /&gt;
&lt;br /&gt;
*name: The variable name of the process structure.&lt;br /&gt;
*strname: The string representation of the process name.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt; AUTOSTART_PROCESS(struct process &amp;amp;) &amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
AUTOSTART_PROCESSES automatically starts the process(es) given in the argument(s) when the module boots.&lt;br /&gt;
&lt;br /&gt;
*&amp;amp;name: Reference to the process name&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt; broadcast_recv(struct broadcast_conn *, const rimeaddr_t *) &amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function parses an incoming packet and displays the message and the address of the sender.&lt;br /&gt;
By setting it as the broadcast&amp;#039;s designated callback function, broadcast_recv is automatically called when a packet is received.&lt;br /&gt;
&lt;br /&gt;
*broadcast_conn *:  This structure which has 2 structures : abc_conn, broadcast_callbacks *. The abc_conn is basic type of connection over which the broadcast connection is developed. And, the broadcast_callbacks point to recv and sent functions (in this example, just recv).&lt;br /&gt;
&lt;br /&gt;
*rimeaddr_t *: This is a union which has a character array u8[RIMEADDR_SIZE].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt; PROCESS_THREAD(name, process_event_t, process_data_t) &amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A process in Contiki consists of a single reference to a &amp;quot;[http://anrg.usc.edu/contiki/index.php?title=Protothreads protothread]&amp;quot;. This function is used to define the protothread of a process. The process is called whenever an event occurs in the system. &lt;br /&gt;
Each process in the module requires an event handler under the PROCESS_THREAD macro.&lt;br /&gt;
&lt;br /&gt;
*name: The variable name of the process structure.&lt;br /&gt;
*process_event_t: The variable of type character.If this variable is same as PROCESS_EVENT_EXIT then PROCESS_EXITHANDLER is invoked.&lt;br /&gt;
&lt;br /&gt;
Within the body of PROCESS_THREAD there are 3 major tasks:&lt;br /&gt;
* Initialization&lt;br /&gt;
** allocate resources&lt;br /&gt;
** define variables&lt;br /&gt;
** begin process&lt;br /&gt;
* Infinite Loop&lt;br /&gt;
** while(1) is used to create an infinite loop in which the actual event-handling response takes place&lt;br /&gt;
* Deallocation&lt;br /&gt;
** end process&lt;br /&gt;
** deallocate resources&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;PROCESS_EXITHANDLER(handler) &amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Specifies an action when a process exits.&lt;br /&gt;
NOTE: This declaration must come immediately before the PROCESS_BEGIN() macro.&lt;br /&gt;
&lt;br /&gt;
* handler: The action to be performed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;PROCESS_BEGIN() &amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This macro defines the beginning of a process, and must always appear in a PROCESS_THREAD() definition.This macro initiates PT_BEGIN(), which is declared at the starting point of a protothread. All C statements above the PT_BEGIN() invokation will be executed each time the protothread is scheduled.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;broadcast_close(struct broadcast_conn *) &amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function closes a broadcast connection that has previously been opened with broadcast_open().This function typically is called as an exit handler.&lt;br /&gt;
&lt;br /&gt;
*broadcast_conn :  This is same as the variable from broadcast_recv().&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt; PROCESS_END() &amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This macro defines the end of a process. It must appear in a PROCESS_THREAD() definition and must always be included. The process exits when the PROCESS_END() macro is reached. This macro initiates PT_END().It must always be used together with a matching PT_BEGIN() macro.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt; broadcast_open(struct broadcast_conn *, uint16_t ,const struct broadcast_callbacks *) &amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sets up an identified best-effort broadcast connection. The caller will allocate the memory for the struct broadcast_conn, usually by declaring it as a static variable. The struct broadcast_callbacks pointer  points to a structure containing a pointer to a function that will be called when a packet arrives on the channel. This function opens a connection of type abc_conn and sets the callbacks to structure passed. Also, this points to channel_set_attributes() function.&lt;br /&gt;
&lt;br /&gt;
*broadcast_conn : A pointer to a struct broadcast_conn&lt;br /&gt;
*uint16_t:  The channel on which the connection will operate&lt;br /&gt;
*broadcast_callbacks :  A struct broadcast_callbacks with function pointers to functions that will be called when a packet has been received&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt; etimer_set(struct etimer *, clock_time_t) &amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This function is used to set an event timer for a time sometime in the future. When the event timer expires, the event PROCESS_EVENT_TIMER will be posted to the process that called the etimer_set() function.&lt;br /&gt;
&lt;br /&gt;
*etimer : A pointer to the event timer&lt;br /&gt;
*clock_time_t : The interval before the timer expires.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt; static struct broadcast_conn &amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The broadcast module sends a packet to all local area neighbors with a header that identifies the sender. It also adds a single-hop address as a packet attribute to out going packets. &lt;br /&gt;
broadcast_conn structure consists of two structures&lt;br /&gt;
*abc_conn struct: the abc(Anonymous best effort local area Broadcast) module sends packets to all local area neighbors. It uses one channel.&lt;br /&gt;
*broadcast_callbacks struct: this is called when a packet has been received by the broadcast module. The struct broadcast_callbacks pointer is used in broadcast_open to point  to a function that will be called when a packet arrives on the channel.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Edited by: Rahul, Nikhil, Bhavana, Lakshmi, Mugdhe, Samarth&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=Hello_World&amp;diff=1822</id>
		<title>Hello World</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=Hello_World&amp;diff=1822"/>
				<updated>2016-09-28T01:43:44Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article is about the Hello World program, which is used to show the compilation and upload steps of a simple program on a Tmote Sky.&lt;br /&gt;
&lt;br /&gt;
== You will learn ==&lt;br /&gt;
&lt;br /&gt;
This tutorial teaches you how to compile a program on Contiki OS and also the most basic syntax of programming in ContikiOS.&lt;br /&gt;
&lt;br /&gt;
=== Step 1 ===&lt;br /&gt;
&lt;br /&gt;
Open the terminal window.&lt;br /&gt;
&lt;br /&gt;
=== Step 2 ===  &lt;br /&gt;
&lt;br /&gt;
In the terminal window, go to the hello world example folder.	&lt;br /&gt;
&lt;br /&gt;
  cd /examples/hello-world&lt;br /&gt;
&lt;br /&gt;
===Step 3=== &lt;br /&gt;
&lt;br /&gt;
Compile the code for the native platform (to be used when no mote is connected to the laptop.)&lt;br /&gt;
&lt;br /&gt;
  make TARGET=native&lt;br /&gt;
&lt;br /&gt;
If there is any bug during compilation which says &amp;quot;#include&amp;lt;curses.h&amp;gt; : No such file or directory&amp;quot; you need to do the following on Ubuntu. &lt;br /&gt;
&lt;br /&gt;
If you are currently in the terminal, type &amp;quot;sudo apt-get install libncurses5-dev&amp;quot;(for which you should have installed the &amp;quot;apt&amp;quot; for 32-bit using &amp;quot;sudo dpkg -i apt_0.8.16~exp12ubuntu10.10_i386.deb&amp;quot; and for 64-bit using sudo dpkg -i apt_0.8.16~exp12ubuntu10.10_amd64.deb&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
You need to also install a version of javac compiler using the command &amp;quot;sudo apt-get install openjdk-7-jdk&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
=== Step 4 ===&lt;br /&gt;
&lt;br /&gt;
Once the compilation is over, run the Hello World program. &lt;br /&gt;
&lt;br /&gt;
  ./hello-world.native&lt;br /&gt;
&lt;br /&gt;
=== Step 5 ===&lt;br /&gt;
You should be seeing the following on the terminal:&lt;br /&gt;
&lt;br /&gt;
  Contiki 3.0 started with IPV6, RPL&lt;br /&gt;
  Rime started with address 1.2.3.4.5.6.7.8&lt;br /&gt;
  MAC nullmac RDC nullrdc NETWORK sicslowpan&lt;br /&gt;
  Tentative link-local IPv6 address fe80:0000:0000:0000:0302:0304:0506:0708&lt;br /&gt;
  Hello, world&lt;br /&gt;
&lt;br /&gt;
The code will appear to hang, however, it is still running on Contiki, but not producing any output as the Hello World program is finished. &lt;br /&gt;
Press Ctrl-C to quit.&lt;br /&gt;
&lt;br /&gt;
== Hello World on the Tmote Sky! ==&lt;br /&gt;
&lt;br /&gt;
=== Step 1 ===&lt;br /&gt;
&lt;br /&gt;
Using the opened terminal window, compile and upload the Hello World program on the Tmote Sky.&lt;br /&gt;
&lt;br /&gt;
  make TARGET=sky savetarget    (This to save the target for any future compilations)&lt;br /&gt;
  make hello-world.upload    (This will upload the code on the Tmote Sky)&lt;br /&gt;
  make login    (This will enable us to view the output. If permission error occurs, use sudo command at the beginning)&lt;br /&gt;
&lt;br /&gt;
- If you are getting errors after running the command &amp;lt;code&amp;gt;make TARGET=sky savetarget&amp;lt;/code&amp;gt; , please make sure you run the following to install a package:&lt;br /&gt;
&lt;br /&gt;
  apt-get install gcc-msp430&lt;br /&gt;
&lt;br /&gt;
- If you get Permission Denied message as below:&lt;br /&gt;
&lt;br /&gt;
could not open port: [Errno 13] Permission denied: &amp;#039;/dev/ttyUSB0&amp;#039;&lt;br /&gt;
make[2]: *** [sky-r._dev_ttyUSB0] Error 1&lt;br /&gt;
make[2]: Target `sky-reset-sequence&amp;#039; not remade because of errors.&lt;br /&gt;
make[2]: Leaving directory `/home/user/contiki-2.7/examples/hello-world&amp;#039;&lt;br /&gt;
make[1]: *** [sky-reset] Error 2&lt;br /&gt;
make[1]: Leaving directory `/home/user/contiki-2.7/examples/hello-world&amp;#039;&lt;br /&gt;
make: *** [hello-world.upload] Error 2&lt;br /&gt;
&lt;br /&gt;
Please make sure you are running as root by typing the following:&lt;br /&gt;
  sudo -s&lt;br /&gt;
&lt;br /&gt;
- If you are getting the following error after running the &amp;lt;code&amp;gt;make login&amp;lt;/code&amp;gt;:&lt;br /&gt;
&lt;br /&gt;
using saved target &amp;#039;sky&amp;#039;&lt;br /&gt;
  ../../tools/sky/serialdump-linux -b115200 /dev/ttyUSB0&lt;br /&gt;
  ../../tools/sky/serialdump-linux: 1: ../../tools/sky/serialdump-linux: Syntax error: Unterminated quoted string&lt;br /&gt;
  make: *** [login] Error 2&lt;br /&gt;
&lt;br /&gt;
Please follow the below steps:&lt;br /&gt;
&lt;br /&gt;
1. Download the new serialdump-linux file from the web page:&lt;br /&gt;
   https://github.com/cmorty/contiki/blob/pull/serialdump/tools/sky/serialdump-linux&lt;br /&gt;
2. Rename the current serialdump-linux file with the name serialdump-linux.bck in the &amp;lt;code&amp;gt;/contiki/tools/sky&amp;lt;/code&amp;gt; directory.&lt;br /&gt;
&lt;br /&gt;
3. Copy the new serialdump-linux file in the directory &amp;lt;code&amp;gt;contiki/tools/sky&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
4. Change the permissions of the new serialdump-linux file with the command&lt;br /&gt;
&lt;br /&gt;
  chmod  775 serialdump-linux&lt;br /&gt;
&lt;br /&gt;
=== Step 2 ===&lt;br /&gt;
&lt;br /&gt;
Press the reset button on the Tmote Sky. The following message will appear on the terminal window &lt;br /&gt;
&lt;br /&gt;
  Contiki 2.7 started. Node id is set to 3.&lt;br /&gt;
  Rime started with address 3.0&lt;br /&gt;
  MAC 00:12:ff:11:65:23:52:ed&lt;br /&gt;
  Starting &amp;#039;Hello world process&amp;#039;&lt;br /&gt;
  Hello, world&lt;br /&gt;
&lt;br /&gt;
: The boot up code of Contiki-OS prints the first four lines whereas the last line is printed by the Hello World program.&lt;br /&gt;
&lt;br /&gt;
=== Step 3 ===&lt;br /&gt;
&lt;br /&gt;
Press Ctrl-C to quit.&lt;br /&gt;
&lt;br /&gt;
=== Try This ===&lt;br /&gt;
&lt;br /&gt;
You can upload and login with one line. You can also point the upload and/or login to a specific device path (use `dmesg` to find the device path of a mote after inserting it to your computer). Try the following command:&lt;br /&gt;
&lt;br /&gt;
  make TARGET=sky MOTES=/dev/ttyUSB0 hello-world.upload login&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=Installation&amp;diff=1821</id>
		<title>Installation</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=Installation&amp;diff=1821"/>
				<updated>2016-09-28T01:36:54Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* Using Github repository */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This guide covers the installation of ContikiOS 2.7.  You will need either a Linux-based operating system, or an operating system that supports [https://www.virtualbox.org/ VirtualBox] or [https://my.vmware.com/web/vmware/downloads VMWare Player].&lt;br /&gt;
&lt;br /&gt;
== You will learn ==&lt;br /&gt;
&lt;br /&gt;
This tutorial will take you through the steps for downloading and installing ContikiOS the following ways:&lt;br /&gt;
&lt;br /&gt;
* Download Instant Contiki [http://sourceforge.net/projects/contiki/files/Instant%20Contiki/]&lt;br /&gt;
* Download ContikiOS from Sourceforge repository [http://sourceforge.net/projects/contiki/files/latest/download]&lt;br /&gt;
* Clone ContikiOS from Github repository [https://github.com/contiki-os/contiki]&lt;br /&gt;
&lt;br /&gt;
=== Using Instant Contiki ===&lt;br /&gt;
&lt;br /&gt;
This is the easiest way of installing and using ContikiOS. You simply need to download Instant Contiki, which is a virtual machine created with all necessary toolchains and software for ContikiOS development.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Link to download Instant Contiki: [http://sourceforge.net/projects/contiki/files/Instant%20Contiki/]&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
After downloading Instant Contiki you need also to download either VMWare Player or VirtualBox, if you are using Windows as your host OS. If you use MacOS X you should download VMWare Fusion, instead of VMWare Player.&lt;br /&gt;
&lt;br /&gt;
IMPORTANT: To login in to Instant Contiki: &amp;#039;&amp;#039;&amp;#039;username&amp;#039;&amp;#039;&amp;#039;: &amp;#039;&amp;#039;user&amp;#039;&amp;#039; &amp;#039;&amp;#039;&amp;#039;password&amp;#039;&amp;#039;&amp;#039;: &amp;#039;&amp;#039;user&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
=== Using Sourceforge repository ===&lt;br /&gt;
&lt;br /&gt;
This method requires an existing installation of a Linux-based operating system. The guide assumes you are using Ubuntu 12.04 but should still be useful for similar versions.&lt;br /&gt;
&lt;br /&gt;
Download contiki-2.7.zip from [http://downloads.sourceforge.net/project/contiki/Contiki/Contiki%202.7/contiki-2.7.zip http://downloads.sourceforge.net/project/contiki/Contiki/Contiki 2.7/contiki-2.7.zip]&lt;br /&gt;
&lt;br /&gt;
  wget http://downloads.sourceforge.net/project/contiki/Contiki/Contiki%202.7/contiki-2.7.zip&lt;br /&gt;
&lt;br /&gt;
Unzip the file. For the purposes of this guide, it is assumed that the file is unzipped into /home/user/&lt;br /&gt;
&lt;br /&gt;
  unzip contiki-2.7.zip&lt;br /&gt;
&lt;br /&gt;
Rename the folder from contiki-2.7 to contiki.&lt;br /&gt;
&lt;br /&gt;
  mv contiki-2.7 contiki&lt;br /&gt;
&lt;br /&gt;
Install all the required packages for compiling and running ContikiOS. &lt;br /&gt;
&lt;br /&gt;
If you are using a MSP430-based platform (such as Tmote) you only need the following packages: &amp;#039;&amp;#039;binutils-msp430&amp;#039;&amp;#039;, &amp;#039;&amp;#039;gcc-msp430&amp;#039;&amp;#039;, &amp;#039;&amp;#039;msp430-libc&amp;#039;&amp;#039;, &amp;#039;&amp;#039;msp430mcu&amp;#039;&amp;#039; and &amp;#039;&amp;#039;mspdebug&amp;#039;&amp;#039;. If you intend to code for multiple platforms, you should also install the following tool chains for other micro-controller: &amp;#039;&amp;#039;binutils-avr&amp;#039;&amp;#039;, &amp;#039;&amp;#039;gcc-avr&amp;#039;&amp;#039;, &amp;#039;&amp;#039;gdb-avr&amp;#039;&amp;#039;, &amp;#039;&amp;#039;avr-libc&amp;#039;&amp;#039; and &amp;#039;&amp;#039;avrdude&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
You can run the following command to install all packages for multiple platforms:&lt;br /&gt;
&lt;br /&gt;
  sudo apt-get install build-essential binutils-msp430 gcc-msp430 msp430-libc msp430mcu mspdebug binutils-avr gcc-avr gdb-avr avr-libc avrdude openjdk-7-jdk openjdk-7-jre ant libncurses5-dev&lt;br /&gt;
&lt;br /&gt;
When working with a 64-bit virtual machine, you may run into problems with the `serialdump-linux` executable because it may have been compiled for 32-bit machines. Install the following package to fix this issue.&lt;br /&gt;
&lt;br /&gt;
  sudo apt-get install lib32ncurses5&lt;br /&gt;
&lt;br /&gt;
You are done! You can now try to compile and run any example, such as [[Hello_World | Hello World]].&lt;br /&gt;
&lt;br /&gt;
=== Using Github repository ===&lt;br /&gt;
&lt;br /&gt;
This method also requires an existing installation of a Linux-based operating system. It is very similar to the previous installation method, but instead of downloading the source code you are going to &amp;#039;&amp;#039;clone&amp;#039;&amp;#039; it from the official Github repository. &lt;br /&gt;
&lt;br /&gt;
This method eases the update of your ContikiOS to the new releases.&lt;br /&gt;
&lt;br /&gt;
You need to &amp;#039;&amp;#039;clone&amp;#039;&amp;#039; the repository using the following command:&lt;br /&gt;
&lt;br /&gt;
  git clone git://github.com/contiki-os/contiki.git contiki&lt;br /&gt;
&lt;br /&gt;
After that you need to download all development tools as described in the previous installation method.&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
Edited by: Kwame, Pedro&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=Installation&amp;diff=1820</id>
		<title>Installation</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=Installation&amp;diff=1820"/>
				<updated>2016-09-28T01:36:13Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* Using Sourceforge repository */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This guide covers the installation of ContikiOS 2.7.  You will need either a Linux-based operating system, or an operating system that supports [https://www.virtualbox.org/ VirtualBox] or [https://my.vmware.com/web/vmware/downloads VMWare Player].&lt;br /&gt;
&lt;br /&gt;
== You will learn ==&lt;br /&gt;
&lt;br /&gt;
This tutorial will take you through the steps for downloading and installing ContikiOS the following ways:&lt;br /&gt;
&lt;br /&gt;
* Download Instant Contiki [http://sourceforge.net/projects/contiki/files/Instant%20Contiki/]&lt;br /&gt;
* Download ContikiOS from Sourceforge repository [http://sourceforge.net/projects/contiki/files/latest/download]&lt;br /&gt;
* Clone ContikiOS from Github repository [https://github.com/contiki-os/contiki]&lt;br /&gt;
&lt;br /&gt;
=== Using Instant Contiki ===&lt;br /&gt;
&lt;br /&gt;
This is the easiest way of installing and using ContikiOS. You simply need to download Instant Contiki, which is a virtual machine created with all necessary toolchains and software for ContikiOS development.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Link to download Instant Contiki: [http://sourceforge.net/projects/contiki/files/Instant%20Contiki/]&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
After downloading Instant Contiki you need also to download either VMWare Player or VirtualBox, if you are using Windows as your host OS. If you use MacOS X you should download VMWare Fusion, instead of VMWare Player.&lt;br /&gt;
&lt;br /&gt;
IMPORTANT: To login in to Instant Contiki: &amp;#039;&amp;#039;&amp;#039;username&amp;#039;&amp;#039;&amp;#039;: &amp;#039;&amp;#039;user&amp;#039;&amp;#039; &amp;#039;&amp;#039;&amp;#039;password&amp;#039;&amp;#039;&amp;#039;: &amp;#039;&amp;#039;user&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
=== Using Sourceforge repository ===&lt;br /&gt;
&lt;br /&gt;
This method requires an existing installation of a Linux-based operating system. The guide assumes you are using Ubuntu 12.04 but should still be useful for similar versions.&lt;br /&gt;
&lt;br /&gt;
Download contiki-2.7.zip from [http://downloads.sourceforge.net/project/contiki/Contiki/Contiki%202.7/contiki-2.7.zip http://downloads.sourceforge.net/project/contiki/Contiki/Contiki 2.7/contiki-2.7.zip]&lt;br /&gt;
&lt;br /&gt;
  wget http://downloads.sourceforge.net/project/contiki/Contiki/Contiki%202.7/contiki-2.7.zip&lt;br /&gt;
&lt;br /&gt;
Unzip the file. For the purposes of this guide, it is assumed that the file is unzipped into /home/user/&lt;br /&gt;
&lt;br /&gt;
  unzip contiki-2.7.zip&lt;br /&gt;
&lt;br /&gt;
Rename the folder from contiki-2.7 to contiki.&lt;br /&gt;
&lt;br /&gt;
  mv contiki-2.7 contiki&lt;br /&gt;
&lt;br /&gt;
Install all the required packages for compiling and running ContikiOS. &lt;br /&gt;
&lt;br /&gt;
If you are using a MSP430-based platform (such as Tmote) you only need the following packages: &amp;#039;&amp;#039;binutils-msp430&amp;#039;&amp;#039;, &amp;#039;&amp;#039;gcc-msp430&amp;#039;&amp;#039;, &amp;#039;&amp;#039;msp430-libc&amp;#039;&amp;#039;, &amp;#039;&amp;#039;msp430mcu&amp;#039;&amp;#039; and &amp;#039;&amp;#039;mspdebug&amp;#039;&amp;#039;. If you intend to code for multiple platforms, you should also install the following tool chains for other micro-controller: &amp;#039;&amp;#039;binutils-avr&amp;#039;&amp;#039;, &amp;#039;&amp;#039;gcc-avr&amp;#039;&amp;#039;, &amp;#039;&amp;#039;gdb-avr&amp;#039;&amp;#039;, &amp;#039;&amp;#039;avr-libc&amp;#039;&amp;#039; and &amp;#039;&amp;#039;avrdude&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
You can run the following command to install all packages for multiple platforms:&lt;br /&gt;
&lt;br /&gt;
  sudo apt-get install build-essential binutils-msp430 gcc-msp430 msp430-libc msp430mcu mspdebug binutils-avr gcc-avr gdb-avr avr-libc avrdude openjdk-7-jdk openjdk-7-jre ant libncurses5-dev&lt;br /&gt;
&lt;br /&gt;
When working with a 64-bit virtual machine, you may run into problems with the `serialdump-linux` executable because it may have been compiled for 32-bit machines. Install the following package to fix this issue.&lt;br /&gt;
&lt;br /&gt;
  sudo apt-get install lib32ncurses5&lt;br /&gt;
&lt;br /&gt;
You are done! You can now try to compile and run any example, such as [[Hello_World | Hello World]].&lt;br /&gt;
&lt;br /&gt;
=== Using Github repository ===&lt;br /&gt;
&lt;br /&gt;
This method also requires an existing installation of a Linux-based operating system. It is very similar to the previous installation method, but instead of downloading the source code you are going to &amp;#039;&amp;#039;clone&amp;#039;&amp;#039; it from the official Github repository. &lt;br /&gt;
&lt;br /&gt;
This method eases the update of your ContikiOS to the new releases.&lt;br /&gt;
&lt;br /&gt;
You need to &amp;#039;&amp;#039;clone&amp;#039;&amp;#039; the repository using the following command:&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;git clone git://github.com/contiki-os/contiki.git contiki&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After that you need to download all development tools as described in the previous installation method.&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
Edited by: Kwame, Pedro&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=Contiki_tutorials&amp;diff=1819</id>
		<title>Contiki tutorials</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=Contiki_tutorials&amp;diff=1819"/>
				<updated>2016-09-28T01:00:33Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* List of Tutorials */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Main_Page | Back to Main Page]]&lt;br /&gt;
&lt;br /&gt;
== List of Tutorials ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;lt;pre style=&amp;quot;color: red&amp;quot;&amp;gt;Disclaimer: Please note that the following tutorials are a work in progress. Use at your own risk.&amp;lt;/pre&amp;gt;&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Completed&lt;br /&gt;
# [[Installation]]&lt;br /&gt;
# [[Hello World]]&lt;br /&gt;
# [[Broadcast Example]]&lt;br /&gt;
# [[Collect View]]&lt;br /&gt;
# [[Contiki build system]]&lt;br /&gt;
# [[Interfacing with Python]]&lt;br /&gt;
# [[Sensor acquisition]]&lt;br /&gt;
# [[Timers]]&lt;br /&gt;
&amp;lt;!-- Need review --&amp;gt;&lt;br /&gt;
# [[CFS-Coffee]]&lt;br /&gt;
# [[Cooja Simulator]]&lt;br /&gt;
# [[Network Stack]]&lt;br /&gt;
# [[RPL UDP]]&lt;br /&gt;
# [[Contiki Shell]]&lt;br /&gt;
&lt;br /&gt;
Starting&lt;br /&gt;
# [[RSS measurement]]&lt;br /&gt;
# [[RPL objective function &amp;amp; simulation using DGRM model in cooja]]&lt;br /&gt;
# [[MAC protocols in ContikiOS]]&lt;br /&gt;
# [[RPL Border Router]]&lt;br /&gt;
# [[REST example running on Cooja and Sky motes]]&lt;br /&gt;
# [[Trickle library]]&lt;br /&gt;
# [[Packetbuffer Basics]]&lt;br /&gt;
# [[Antelope(Database Management System)]]&lt;br /&gt;
# [[Mobility of Nodes in Cooja]]&lt;br /&gt;
# [[Contiki Coffee File System]]&lt;br /&gt;
# [[Contiki Programming Guide]]&lt;br /&gt;
# [[Analyse of a real 6LoWPAN network using a Contiki-based sniffer module]]&lt;br /&gt;
# [[Build your own application in Contiki]]&lt;br /&gt;
# [[Processes]] Yash&lt;br /&gt;
# [[RPL objective function modification and simulation in cooja]]&lt;br /&gt;
# [[Collect-view Code Details]] Pradipta&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre style=&amp;quot;color: red&amp;quot;&amp;gt;Be sure to include references in your tutorials, especially if you quote material from other sites!&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=Contiki_tutorials&amp;diff=1818</id>
		<title>Contiki tutorials</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=Contiki_tutorials&amp;diff=1818"/>
				<updated>2016-09-28T00:52:53Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* List of Tutorials */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Main_Page | Back to Main Page]]&lt;br /&gt;
&lt;br /&gt;
== List of Tutorials ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;lt;pre style=&amp;quot;color: red&amp;quot;&amp;gt;Disclaimer: Please note that the following tutorials are a work in progress. Use at your own risk.&amp;lt;/pre&amp;gt;&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Completed&lt;br /&gt;
# [[Installation]]&lt;br /&gt;
# [[Hello World]]&lt;br /&gt;
# [[Broadcast Example]]&lt;br /&gt;
# [[Collect View]]&lt;br /&gt;
# [[Contiki build system]]&lt;br /&gt;
# [[Interfacing with Python]]&lt;br /&gt;
# [[Sensor acquisition]]&lt;br /&gt;
# [[Timers]]&lt;br /&gt;
&amp;lt;!-- Need review --&amp;gt;&lt;br /&gt;
# [[CFS-Coffee]]&lt;br /&gt;
# [[Cooja Simulator]]&lt;br /&gt;
# [[Network Stack]]&lt;br /&gt;
# [[RPL UDP]]&lt;br /&gt;
# [[Contiki Shell]]&lt;br /&gt;
&lt;br /&gt;
Starting&lt;br /&gt;
# [[RSS measurement]] Nitin&lt;br /&gt;
# [[RPL objective function &amp;amp; simulation using DGRM model in cooja  ]] Ashwini Telang&lt;br /&gt;
# [[MAC protocols in ContikiOS]] Pedro&lt;br /&gt;
# [[RPL Border Router]] Chhavi&lt;br /&gt;
# [[REST example running on Cooja and Sky motes]] Mrunal Muni &lt;br /&gt;
# [[Trickle library]] Subhashini Sundaresan&lt;br /&gt;
# [[Packetbuffer Basics]] Pradipta&lt;br /&gt;
# [[Antelope(Database Management System) - Contiki]] Gopi Krishna&lt;br /&gt;
# [[Mobility of Nodes in Cooja]] Pratyush Deshpande&lt;br /&gt;
# [[Contiki Coffee File System]] Zhikun Liu&lt;br /&gt;
# [[Contiki Programming Guide]] Haimo Bai&lt;br /&gt;
# [[Analyse of a real 6LoWPAN network using a Contiki-based sniffer module]] Yash Goyal&lt;br /&gt;
# [[Build your own application in Contiki]] Nitin&lt;br /&gt;
&amp;lt;!--[[Processes]] Yash --&amp;gt;&lt;br /&gt;
&amp;lt;!--[[RPL objective function modification and simulation in cooja]]--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--# [[Collect-view Code Details]] Pradipta --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre style=&amp;quot;color: red&amp;quot;&amp;gt;Be sure to include references in your tutorials, especially if you quote material from other sites!&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=Contiki_tutorials&amp;diff=1817</id>
		<title>Contiki tutorials</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=Contiki_tutorials&amp;diff=1817"/>
				<updated>2016-09-28T00:50:27Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* List of Tutorials */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Main_Page | Back to Main Page]]&lt;br /&gt;
&lt;br /&gt;
== List of Tutorials ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;lt;pre style=&amp;quot;color: red&amp;quot;&amp;gt;Disclaimer: Please note that the following tutorials are a work in progress. Use at your own risk.&amp;lt;/pre&amp;gt;&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Completed&lt;br /&gt;
# [[Installation]]&lt;br /&gt;
# [[Hello World]]&lt;br /&gt;
# [[Broadcast Example]]&lt;br /&gt;
# [[Collect View]]&lt;br /&gt;
# [[Contiki build system]]&lt;br /&gt;
# [[Interfacing with Python]]&lt;br /&gt;
# [[Sensor acquisition]]&lt;br /&gt;
# [[Timers]]&lt;br /&gt;
&amp;lt;!-- Need review --&amp;gt;&lt;br /&gt;
# [[CFS-Coffee]]&lt;br /&gt;
# [[Cooja Simulator]]&lt;br /&gt;
# [[Network Stack]]&lt;br /&gt;
# [[RPL UDP]]&lt;br /&gt;
# [[Contiki Shell]]&lt;br /&gt;
&lt;br /&gt;
Starting&lt;br /&gt;
# [[CSMA]] Tim, Leo&lt;br /&gt;
# [[RSS measurement]] Nitin&lt;br /&gt;
# [[RPL objective function &amp;amp; simulation using DGRM model in cooja  ]] Ashwini Telang&lt;br /&gt;
# [[MAC protocols in ContikiOS]] Pedro&lt;br /&gt;
# [[RPL Border Router]] Chhavi&lt;br /&gt;
# [[REST example running on Cooja and Sky motes]] Mrunal Muni &lt;br /&gt;
# [[Trickle library]] Subhashini Sundaresan&lt;br /&gt;
# [[Packetbuffer Basics]] Pradipta&lt;br /&gt;
# [[Antelope(Database Management System) - Contiki]] Gopi Krishna&lt;br /&gt;
# [[Mobility of Nodes in Cooja]] Pratyush Deshpande&lt;br /&gt;
# [[Contiki Coffee File System]] Zhikun Liu&lt;br /&gt;
# [[Contiki Programming Guide]] Haimo Bai&lt;br /&gt;
# [[Analyse of a real 6LoWPAN network using a Contiki-based sniffer module]] Yash Goyal&lt;br /&gt;
# [[Build your own application in Contiki]] Nitin&lt;br /&gt;
&amp;lt;!--[[Processes]] Yash --&amp;gt;&lt;br /&gt;
&amp;lt;!--[[RPL objective function modification and simulation in cooja]]--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--# [[Collect-view Code Details]] Pradipta --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre style=&amp;quot;color: red&amp;quot;&amp;gt;Be sure to include references in your tutorials, especially if you quote material from other sites!&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=Contiki_tutorials&amp;diff=1816</id>
		<title>Contiki tutorials</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=Contiki_tutorials&amp;diff=1816"/>
				<updated>2016-09-28T00:26:46Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* List of Tutorials */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Main_Page | Back to Main Page]]&lt;br /&gt;
&lt;br /&gt;
== List of Tutorials ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;lt;pre style=&amp;quot;color: red&amp;quot;&amp;gt;Disclaimer: Please note that the following tutorials are a work in progress. Use at your own risk.&amp;lt;/pre&amp;gt;&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Completed&lt;br /&gt;
# [[Installation]]&lt;br /&gt;
# [[Hello World]]&lt;br /&gt;
# [[Broadcast Example]]&lt;br /&gt;
# [[Collect View]]&lt;br /&gt;
# [[Contiki build system]]&lt;br /&gt;
# [[Interfacing with Python]]&lt;br /&gt;
# [[Sensor acquisition]]&lt;br /&gt;
# [[Timers]]&lt;br /&gt;
&amp;lt;!-- Need review --&amp;gt;&lt;br /&gt;
# [[CFS-Coffee]]&lt;br /&gt;
# [[Cooja Simulator]]&lt;br /&gt;
# [[Network Stack]]&lt;br /&gt;
# [[RPL UDP]]&lt;br /&gt;
# [[Contiki Shell]]&lt;br /&gt;
&lt;br /&gt;
Starting&lt;br /&gt;
# [[Tutornet]] Pedro, Kwame&lt;br /&gt;
# [[CSMA]] Tim, Leo&lt;br /&gt;
# [[RSS measurement]] Nitin&lt;br /&gt;
# [[RPL objective function &amp;amp; simulation using DGRM model in cooja  ]] Ashwini Telang&lt;br /&gt;
# [[MAC protocols in ContikiOS]] Pedro&lt;br /&gt;
# [[RPL Border Router]] Chhavi&lt;br /&gt;
# [[REST example running on Cooja and Sky motes]] Mrunal Muni &lt;br /&gt;
# [[Trickle library]] Subhashini Sundaresan&lt;br /&gt;
# [[Packetbuffer Basics]] Pradipta&lt;br /&gt;
# [[Antelope(Database Management System) - Contiki]] Gopi Krishna&lt;br /&gt;
# [[Mobility of Nodes in Cooja]] Pratyush Deshpande&lt;br /&gt;
# [[Contiki Coffee File System]] Zhikun Liu&lt;br /&gt;
# [[Contiki Programming Guide]] Haimo Bai&lt;br /&gt;
# [[Analyse of a real 6LoWPAN network using a Contiki-based sniffer module]] Yash Goyal&lt;br /&gt;
# [[Build your own application in Contiki]] Nitin&lt;br /&gt;
&amp;lt;!--[[Processes]] Yash --&amp;gt;&lt;br /&gt;
&amp;lt;!--[[RPL objective function modification and simulation in cooja]]--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--# [[Collect-view Code Details]] Pradipta --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre style=&amp;quot;color: red&amp;quot;&amp;gt;Be sure to include references in your tutorials, especially if you quote material from other sites!&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=Contiki_tutorials&amp;diff=1815</id>
		<title>Contiki tutorials</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=Contiki_tutorials&amp;diff=1815"/>
				<updated>2016-09-27T23:58:14Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Main_Page | Back to Main Page]]&lt;br /&gt;
&lt;br /&gt;
== List of Tutorials ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;&amp;lt;pre style=&amp;quot;color: red&amp;quot;&amp;gt;Disclaimer: Please note that the following tutorials are a work in progress. Use at your own risk.&amp;lt;/pre&amp;gt;&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
Completed&lt;br /&gt;
# [[Installation]]&lt;br /&gt;
# [[Hello World]]&lt;br /&gt;
# [[Broadcast Example]]&lt;br /&gt;
# [[Collect View]]&lt;br /&gt;
# [[Contiki build system]]&lt;br /&gt;
# [[Interfacing with Python]]&lt;br /&gt;
# [[Sensor acquisition]] (light, temperature, humidity)&lt;br /&gt;
# [[Timers]] Tim, Leo&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Need review --&amp;gt;&lt;br /&gt;
# [[CFS-Coffee]] Kevin&lt;br /&gt;
# [[Cooja Simulator]] (Getting started, debugging) Pedro&lt;br /&gt;
# [[Network Stack]] Yash&lt;br /&gt;
# [[RPL UDP]] Jiahao Liang&lt;br /&gt;
# [[Contiki Shell]] Abhilash Nagaraj Hegde&lt;br /&gt;
&lt;br /&gt;
Starting&lt;br /&gt;
# [[Tutornet]] Pedro, Kwame&lt;br /&gt;
# [[CSMA]] Tim, Leo&lt;br /&gt;
# [[RSS measurement]] Nitin&lt;br /&gt;
# [[RPL objective function &amp;amp; simulation using DGRM model in cooja  ]] Ashwini Telang&lt;br /&gt;
# [[MAC protocols in ContikiOS]] Pedro&lt;br /&gt;
# [[RPL Border Router]] Chhavi&lt;br /&gt;
# [[REST example running on Cooja and Sky motes]] Mrunal Muni &lt;br /&gt;
# [[Trickle library]] Subhashini Sundaresan&lt;br /&gt;
# [[Packetbuffer Basics]] Pradipta&lt;br /&gt;
# [[Antelope(Database Management System) - Contiki]] Gopi Krishna&lt;br /&gt;
# [[Mobility of Nodes in Cooja]] Pratyush Deshpande&lt;br /&gt;
# [[Contiki Coffee File System]] Zhikun Liu&lt;br /&gt;
# [[Contiki Programming Guide]] Haimo Bai&lt;br /&gt;
# [[Analyse of a real 6LoWPAN network using a Contiki-based sniffer module]] Yash Goyal&lt;br /&gt;
# [[Build your own application in Contiki]] Nitin&lt;br /&gt;
&amp;lt;!--[[Processes]] Yash --&amp;gt;&lt;br /&gt;
&amp;lt;!--[[RPL objective function modification and simulation in cooja]]--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--# [[Collect-view Code Details]] Pradipta --&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre style=&amp;quot;color: red&amp;quot;&amp;gt;Be sure to include references in your tutorials, especially if you quote material from other sites!&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=Sensor_acquisition&amp;diff=1765</id>
		<title>Sensor acquisition</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=Sensor_acquisition&amp;diff=1765"/>
				<updated>2016-08-05T01:27:40Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* Source Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This article helps you learn how to create the Sensor Acquisition Program, which can be used to show the Temperature, Humidity and Light Intensity in the vicinity of Tmote Sky.&lt;br /&gt;
&lt;br /&gt;
== How to start an application in Contiki ==&lt;br /&gt;
&lt;br /&gt;
Every process should start with the PROCESS macro. It takes two arguments&lt;br /&gt;
*name: The variable name of the process structure.&lt;br /&gt;
*strname: The string representation of the process name.&lt;br /&gt;
&lt;br /&gt;
  PROCESS(name,strname)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Then comes another macro AUTOSTART_PROCESS(struct process &amp;amp;). AUTOSTART_PROCESSES automatically starts the process(es) given in the argument(s) when the module boots.&lt;br /&gt;
*&amp;amp;name: Reference to the process name.&lt;br /&gt;
&lt;br /&gt;
  AUTOSTART_PROCESS(struct process &amp;amp;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Then we call the PROCESS_THREAD function. This function is used to define the protothread of a process. The process is called whenever an event occurs in the system.Each process in the module requires an event handler under the PROCESS_THREAD macro.&lt;br /&gt;
*name: The variable name of the process structure.&lt;br /&gt;
*process_event_t: The variable of type character.If this variable is same as PROCESS_EVENT_EXIT then PROCESS_EXITHANDLER is invoked.&lt;br /&gt;
&lt;br /&gt;
  PROCESS_THREAD(name, process_event_t, process_data_t)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Then comes the PROCESS_BEGIN macro. This macro defines the beginning of a process, and must always appear in a PROCESS_THREAD() definition.&lt;br /&gt;
&lt;br /&gt;
  PROCESS_BEGIN()&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Then we write the set of C statements as per the requirement of the application.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
At the end we use another macro PROCESS_END. This macro defines the end of a process. It must appear in a PROCESS_THREAD() definition and must always be included. The process exits when the PROCESS_END() macro is reached.&lt;br /&gt;
&lt;br /&gt;
  PROCESS_END()&lt;br /&gt;
&lt;br /&gt;
== Generic Template for Creating Application in Contiki  ==&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
PROCESS(name,strname);&lt;br /&gt;
AUTOSTART_PROCESSES(struct process &amp;amp;);&lt;br /&gt;
&lt;br /&gt;
PROCESS_THREAD(name, process_event_t, process_data_t)&lt;br /&gt;
{ &lt;br /&gt;
&lt;br /&gt;
----Initialization of required variables----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
PROCESS_BEGIN();&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
---Set of C statements---&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
PROCESS_END();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Sensor Acquisition Program on the Tmote Sky! ==&lt;br /&gt;
&lt;br /&gt;
=== Step 1 ===&lt;br /&gt;
&lt;br /&gt;
Using the opened terminal window compile and upload the Sensor Acquisition program on the Tmote Sky.&lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;code&amp;gt;make TARGET=sky savetarget&amp;lt;/code&amp;gt;    (This saves the target for any future compilations)&lt;br /&gt;
:: &amp;lt;code&amp;gt;make sensor-acq.upload&amp;lt;/code&amp;gt;    (This will upload the code on the Tmote Sky)&lt;br /&gt;
:: &amp;lt;code&amp;gt;make login&amp;lt;/code&amp;gt;    (This will enable us to view the output. If permission error occurs, use sudo command at the beginning)&lt;br /&gt;
See the following link for troubleshooting - http://anrg.usc.edu/contiki/index.php/Troubleshooting&lt;br /&gt;
&lt;br /&gt;
=== Step 2 ===&lt;br /&gt;
&lt;br /&gt;
Press the reset button on the Tmote Sky. The following message will appear on the terminal window &lt;br /&gt;
&lt;br /&gt;
:: &amp;lt;code&amp;gt;Temperature=28.04 C (6764)&amp;lt;/code&amp;gt;&lt;br /&gt;
:: &amp;lt;code&amp;gt;Humidity=73.20% (2259)&amp;lt;/code&amp;gt;&lt;br /&gt;
:: &amp;lt;code&amp;gt;Light=38.26 lux (94)&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Values inside the round brackets (ex 6764, 2259 and 94) are the actual sensor values and the calibrated values can be obtained by performing calculations on these sensor values. You can find the calculations at&lt;br /&gt;
http://tinyos.stanford.edu/tinyos-wiki/index.php/Boomerang_ADC_Example&lt;br /&gt;
&lt;br /&gt;
=== Step 3 ===&lt;br /&gt;
&lt;br /&gt;
Press Ctrl-C to quit.&lt;br /&gt;
&lt;br /&gt;
== Code ==&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;&lt;br /&gt;
&lt;br /&gt;
PROCESS(sensor_acq_process,&amp;quot;Sensor Acquisition&amp;quot;);&lt;br /&gt;
AUTOSTART_PROCESSES(&amp;amp;sensor_acq_process);&lt;br /&gt;
&lt;br /&gt;
PROCESS_THREAD(sensor_acq_process,ev,data)&lt;br /&gt;
{ &lt;br /&gt;
      static struct etimer et;&lt;br /&gt;
      static int val;&lt;br /&gt;
      static struct sensors_sensor *sensor;&lt;br /&gt;
      static float s = 0;&lt;br /&gt;
      static int dec;&lt;br /&gt;
      static float frac;&lt;br /&gt;
&lt;br /&gt;
      PROCESS_BEGIN();&lt;br /&gt;
&lt;br /&gt;
      printf(&amp;quot;Starting Sensor Example.\n&amp;quot;);&lt;br /&gt;
      &lt;br /&gt;
      while(1)&lt;br /&gt;
      {&lt;br /&gt;
	   etimer_set(&amp;amp;et, CLOCK_SECOND * 2);&lt;br /&gt;
	   SENSORS_ACTIVATE(light_sensor);&lt;br /&gt;
      	   SENSORS_ACTIVATE(sht11_sensor);&lt;br /&gt;
        &lt;br /&gt;
	   PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&amp;amp;et));&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
           val = sht11_sensor.value(SHT11_SENSOR_TEMP);&lt;br /&gt;
      	   if(val != -1) &lt;br /&gt;
      	   {&lt;br /&gt;
		s= ((0.01*val) - 39.60);&lt;br /&gt;
      	  	dec = s;&lt;br /&gt;
      	  	frac = s - dec;&lt;br /&gt;
      	  	printf(&amp;quot;\nTemperature=%d.%02u C (%d)\n&amp;quot;, dec, (unsigned int)(frac * 100),val);               &lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
	   val=sht11_sensor.value(SHT11_SENSOR_HUMIDITY);&lt;br /&gt;
	   if(val != -1) &lt;br /&gt;
      	   {&lt;br /&gt;
		s= (((0.0405*val) - 4) + ((-2.8 * 0.000001)*(pow(val,2))));  &lt;br /&gt;
      	  	dec = s;&lt;br /&gt;
      	  	frac = s - dec;&lt;br /&gt;
      	  	printf(&amp;quot;Humidity=%d.%02u % (%d)\n&amp;quot;, dec, (unsigned int)(frac * 100),val);               &lt;br /&gt;
           }&lt;br /&gt;
&lt;br /&gt;
           val = light_sensor.value(LIGHT_SENSOR_TOTAL_SOLAR);&lt;br /&gt;
      	   if(val != -1) &lt;br /&gt;
      	   {&lt;br /&gt;
      		s = (float)(val * 0.4071);&lt;br /&gt;
      	  	dec = s;&lt;br /&gt;
      	  	frac = s - dec;&lt;br /&gt;
      	  	printf(&amp;quot;Light=%d.%02u lux (%d)\n&amp;quot;, dec, (unsigned int)(frac * 100),val);               &lt;br /&gt;
           } &lt;br /&gt;
	&lt;br /&gt;
	   etimer_reset(&amp;amp;et);&lt;br /&gt;
    	   SENSORS_DEACTIVATE(light_sensor);&lt;br /&gt;
    	   SENSORS_DEACTIVATE(sht11_sensor);&lt;br /&gt;
&lt;br /&gt;
      } //end of while&lt;br /&gt;
    &lt;br /&gt;
      PROCESS_END();&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Understanding the Code ==&lt;br /&gt;
&lt;br /&gt;
The following piece of code sets the timer to repeat the iterations every 2 seconds.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;etimer_set(&amp;amp;et, CLOCK_SECOND * 2);&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We need to activate light_sensor for measuring the light intensity and the sht11_sensor for the measurement of temperature and humidity.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;SENSORS_ACTIVATE(light_sensor);&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;SENSORS_ACTIVATE(sht11_sensor);&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The following line is used to capture the actual sensor value,&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;val = sht11_sensor.value(SHT11_SENSOR_TEMP);&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We need to calibrate the sensor values by doing some calculations. You can find the calculations here: http://tinyos.stanford.edu/tinyos-wiki/index.php/Boomerang_ADC_Example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;s = ((0.01*val) - 39.60);&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
After we are done with the calculations, we need to deactivate the sensors.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;SENSORS_DEACTIVATE(light_sensor);&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;SENSORS_DEACTIVATE(sht11_sensor);&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Source Code ==&lt;br /&gt;
&lt;br /&gt;
To download a copy of &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;sensor-acq.c&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt;, click [https://drive.google.com/file/d/0B0-za4nfpimxc2NDX2xzdEhIeGc/view?usp=sharing here]&lt;br /&gt;
&lt;br /&gt;
[[Media:sensor-acq.c]]&lt;br /&gt;
&lt;br /&gt;
Copy the downloaded .c file in the &amp;lt;code&amp;gt;&amp;lt;nowiki&amp;gt;/contiki/examples/rime&amp;lt;/nowiki&amp;gt;&amp;lt;/code&amp;gt; folder and follow the steps mentioned above.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
Edited by : Nitin&lt;br /&gt;
&lt;br /&gt;
Source Code added by : Mugdhe, Samarth&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=EE_652_projects&amp;diff=1661</id>
		<title>EE 652 projects</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=EE_652_projects&amp;diff=1661"/>
				<updated>2014-12-17T02:03:18Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* Pedro - Scheduling algorithms for IEEE 802.15.4e networks */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;EE 652 2014 Projects&lt;br /&gt;
&lt;br /&gt;
== Scheduling algorithms for IEEE 802.15.4e networks - Pedro ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: http://neptune.usc.edu:8081/pdasilva/tsch-schedulers/&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;How to compile&amp;#039;&amp;#039;: gcc -std=gnu99 -o Scheduling util/*.c graphs/*.c mcc/*.c tasa/*.c main.c&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;How to execute&amp;#039;&amp;#039;: ./Scheduling &amp;lt;sink_id&amp;gt; &amp;lt;algorithm&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where: 	&amp;lt;sink_id&amp;gt; = Sink identification (starting at 0) and &amp;lt;algorithm&amp;gt; = 0 if MCC and 1 if TASA&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Output&amp;#039;&amp;#039;: you will find files ext_schedule.h and topology.c, which should be used according to the project report&lt;br /&gt;
&lt;br /&gt;
The execution will consider data/prr55.txt file as input for PRR statistics. You need to create the file with tree description before running TASA. You can easily do that first running MCC for a given sink id (e.g. 3) and then running TASA for the same sink id.&lt;br /&gt;
&lt;br /&gt;
== Backpressure Control Protocol on IPv6 stack of Contiki - Mrunal &amp;amp; Chhavi ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: https://github.com/chhavikapoor/EE652_Final&lt;br /&gt;
&lt;br /&gt;
Use the branch final_project from the repository.&lt;br /&gt;
&lt;br /&gt;
Follow the README-BCP in the final_project repository to simulate the implementation of BCP on IPv6 stack in COOJA.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;: &lt;br /&gt;
&lt;br /&gt;
BCP is an implementation of dynamic backpressure routing in which the routing and forwarding decisions are made on per packet basis that takes into consideration the backpressure weight of each of its neighbors. The ubiquitous use of TCP/IP protocol suite in web applications, peer to peer networking over the internet etc motivated us to implement the Backpressure Collection Protocol on the IPv6 Stack of Contiki. We implemented a functional version of BCP on the IPv6 stack of Contiki OS.&lt;br /&gt;
In our implementation, BCP Sink by default has the node_id = 1.&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=EE_652_projects&amp;diff=1659</id>
		<title>EE 652 projects</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=EE_652_projects&amp;diff=1659"/>
				<updated>2014-12-16T18:49:30Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;EE 652 2014 Projects&lt;br /&gt;
&lt;br /&gt;
== Pedro - Scheduling algorithms for IEEE 802.15.4e networks ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: http://neptune.usc.edu:8081/pdasilva/tsch-schedulers/&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;How to compile&amp;#039;&amp;#039;: gcc -std=gnu99 -o Scheduling util/*.c graphs/*.c mcc/*.c tasa/*.c main.c&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;How to execute&amp;#039;&amp;#039;: ./Scheduling &amp;lt;sink_id&amp;gt; &amp;lt;algorithm&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where: 	&amp;lt;sink_id&amp;gt; = Sink identification (starting at 0) and &amp;lt;algorithm&amp;gt; = 0 if MCC and 1 if TASA&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Output&amp;#039;&amp;#039;: you will find files ext_schedule.h and topology.c, which should be used according to the project report&lt;br /&gt;
&lt;br /&gt;
The execution will consider data/prr55.txt file as input for PRR statistics. You need to create the file with tree description before running TASA. You can easily do that first running MCC for a given sink id (e.g. 3) and then running TASA for the same sink id.&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=EE_652_projects&amp;diff=1658</id>
		<title>EE 652 projects</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=EE_652_projects&amp;diff=1658"/>
				<updated>2014-12-16T18:48:45Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;EE 652 2014 Projects&lt;br /&gt;
&lt;br /&gt;
== Pedro - Scheduling algorithms for IEEE 802.15.4e networks ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: http://neptune.usc.edu:8081/pdasilva/tsch-schedulers/&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;How to compile&amp;#039;&amp;#039;: gcc -std=gnu99 -o Scheduling util/*.c graphs/*.c mcc/*.c tasa/*.c main.c&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;How to execute&amp;#039;&amp;#039;: ./Scheduling &amp;lt;sink_id&amp;gt; &amp;lt;algorithm&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where: 	sink_id = Sink identification (starting at 0)&lt;br /&gt;
	algorithm = 0 if MCC and 1 if TASA&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Output&amp;#039;&amp;#039;: you will find files ext_schedule.h and topology.c, which should be used according to the project report&lt;br /&gt;
&lt;br /&gt;
The execution will consider data/prr55.txt file as input for PRR statistics. You need to create the file with tree description before running TASA. You can easily do that first running MCC for a given sink id (e.g. 3) and then running TASA for the same sink id.&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=EE_652_projects&amp;diff=1657</id>
		<title>EE 652 projects</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=EE_652_projects&amp;diff=1657"/>
				<updated>2014-12-16T18:48:34Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* Pedro - Scheduling algorithms for IEEE 802.15.4e networks */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;EE 652 2014 Projects&lt;br /&gt;
&lt;br /&gt;
== Pedro - Scheduling algorithms for IEEE 802.15.4e networks ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: http://neptune.usc.edu:8081/pdasilva/tsch-schedulers/&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;How to compile&amp;#039;&amp;#039;: gcc -std=gnu99 -o Scheduling util/*.c graphs/*.c mcc/*.c tasa/*.c main.c&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;How to execute&amp;#039;&amp;#039;: ./Scheduling &amp;lt;sink_id&amp;gt; &amp;lt;algorithm&amp;gt;&lt;br /&gt;
&lt;br /&gt;
where: 	sink_id = Sink identification (starting at 0)&lt;br /&gt;
	&lt;br /&gt;
algorithm = 0 if MCC and 1 if TASA&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Output&amp;#039;&amp;#039;: you will find files ext_schedule.h and topology.c, which should be used according to the project report&lt;br /&gt;
&lt;br /&gt;
The execution will consider data/prr55.txt file as input for PRR statistics. You need to create the file with tree description before running TASA. You can easily do that first running MCC for a given sink id (e.g. 3) and then running TASA for the same sink id.&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=EE_652_projects&amp;diff=1656</id>
		<title>EE 652 projects</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=EE_652_projects&amp;diff=1656"/>
				<updated>2014-12-16T18:48:09Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;EE 652 2014 Projects&lt;br /&gt;
&lt;br /&gt;
== Pedro - Scheduling algorithms for IEEE 802.15.4e networks ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: http://neptune.usc.edu:8081/pdasilva/tsch-schedulers/&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;How to compile&amp;#039;&amp;#039;: gcc -std=gnu99 -o Scheduling util/*.c graphs/*.c mcc/*.c tasa/*.c main.c&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;How to execute&amp;#039;&amp;#039;: ./Scheduling &amp;lt;sink_id&amp;gt; &amp;lt;algorithm&amp;gt;&lt;br /&gt;
where: 	sink_id = Sink identification (starting at 0)&lt;br /&gt;
	algorithm = 0 if MCC and 1 if TASA&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Output&amp;#039;&amp;#039;: you will find files ext_schedule.h and topology.c, which should be used according to the project report&lt;br /&gt;
&lt;br /&gt;
The execution will consider data/prr55.txt file as input for PRR statistics. You need to create the file with tree description before running TASA. You can easily do that first running MCC for a given sink id (e.g. 3) and then running TASA for the same sink id.&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=EE_652_projects&amp;diff=1655</id>
		<title>EE 652 projects</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=EE_652_projects&amp;diff=1655"/>
				<updated>2014-12-16T18:47:54Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* Pedro */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;EE 652 2014 Projects&lt;br /&gt;
&lt;br /&gt;
== Pedro - Scheduling algorithms for IEEE 802.15.4e networks ==&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Repository&amp;#039;&amp;#039;&amp;#039;: http://neptune.usc.edu:8081/pdasilva/tsch-schedulers/&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Description&amp;#039;&amp;#039;&amp;#039;:&lt;br /&gt;
&lt;br /&gt;
How to compile: gcc -std=gnu99 -o Scheduling util/*.c graphs/*.c mcc/*.c tasa/*.c main.c&lt;br /&gt;
&lt;br /&gt;
How to execute: ./Scheduling &amp;lt;sink_id&amp;gt; &amp;lt;algorithm&amp;gt;&lt;br /&gt;
where: 	sink_id = Sink identification (starting at 0)&lt;br /&gt;
	algorithm = 0 if MCC and 1 if TASA&lt;br /&gt;
&lt;br /&gt;
Output: you will find files ext_schedule.h and topology.c, which should be used according to the project report&lt;br /&gt;
&lt;br /&gt;
The execution will consider data/prr55.txt file as input for PRR statistics. You need to create the file with tree description before running TASA. You can easily do that first running MCC for a given sink id (e.g. 3) and then running TASA for the same sink id.&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=EE_652_projects&amp;diff=1654</id>
		<title>EE 652 projects</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=EE_652_projects&amp;diff=1654"/>
				<updated>2014-12-16T18:46:03Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* Pedro */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;EE 652 2014 Projects&lt;br /&gt;
&lt;br /&gt;
== Pedro ==&lt;br /&gt;
Repository: http://neptune.usc.edu:8081/pdasilva/tsch-schedulers/&lt;br /&gt;
&lt;br /&gt;
Description:&lt;br /&gt;
&lt;br /&gt;
How to compile: gcc -std=gnu99 -o Scheduling util/*.c graphs/*.c mcc/*.c tasa/*.c main.c&lt;br /&gt;
&lt;br /&gt;
How to execute: ./Scheduling &amp;lt;sink_id&amp;gt; &amp;lt;algorithm&amp;gt;&lt;br /&gt;
where: 	sink_id = Sink identification (starting at 0)&lt;br /&gt;
	algorithm = 0 if MCC and 1 if TASA&lt;br /&gt;
&lt;br /&gt;
Output: you will find files ext_schedule.h and topology.c, which should be used according to the project report&lt;br /&gt;
&lt;br /&gt;
The execution will consider data/prr55.txt file as input for PRR statistics. You need to create the file with tree description before running TASA. You can easily do that first running MCC for a given sink id (e.g. 3) and then running TASA for the same sink id.&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=EE_652_projects&amp;diff=1653</id>
		<title>EE 652 projects</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=EE_652_projects&amp;diff=1653"/>
				<updated>2014-12-16T18:45:48Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;EE 652 2014 Projects&lt;br /&gt;
&lt;br /&gt;
=== Pedro ===&lt;br /&gt;
Repository: http://neptune.usc.edu:8081/pdasilva/tsch-schedulers/&lt;br /&gt;
&lt;br /&gt;
Description:&lt;br /&gt;
&lt;br /&gt;
How to compile: gcc -std=gnu99 -o Scheduling util/*.c graphs/*.c mcc/*.c tasa/*.c main.c&lt;br /&gt;
&lt;br /&gt;
How to execute: ./Scheduling &amp;lt;sink_id&amp;gt; &amp;lt;algorithm&amp;gt;&lt;br /&gt;
where: 	sink_id = Sink identification (starting at 0)&lt;br /&gt;
	algorithm = 0 if MCC and 1 if TASA&lt;br /&gt;
&lt;br /&gt;
Output: you will find files ext_schedule.h and topology.c, which should be used according to the project report&lt;br /&gt;
&lt;br /&gt;
The execution will consider data/prr55.txt file as input for PRR statistics. You need to create the file with tree description before running TASA. You can easily do that first running MCC for a given sink id (e.g. 3) and then running TASA for the same sink id.&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1160</id>
		<title>MAC protocols in ContikiOS</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1160"/>
				<updated>2014-11-08T23:44:00Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* RDC protocols */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This tutorial covers the main features of &amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039; protocols available in &amp;#039;&amp;#039;ContikiOS 2.7&amp;#039;&amp;#039;. Medium Access Control protocols take care of the organization of medium access in wireless networks. They can be interpreted as rules that coordinate when each node is going to transmit/receive packets.&lt;br /&gt;
&lt;br /&gt;
There are a large number of protocols available in the literature, they can usually be classified as contention-based or reservation-based protocols. The first group is based on Carrier Sensing for detecting medium activity and are prone to collisions and lower efficiency; however, they are of easy implementation. The second group is more efficient in terms of throughput and energy, but require precise synchronization and is less adaptable to dynamic traffic. &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; only includes MAC protocols of the first category.&lt;br /&gt;
&lt;br /&gt;
There are about 5 types of MAC layers that can be used in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. They are usually implementation of well-known MAC protocols for &amp;#039;&amp;#039;Wireless Sensor Networks&amp;#039;&amp;#039;; a few of them are implementations of protocols developed exclusively for &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;, but that were based on previous work present in the literature.&lt;br /&gt;
&lt;br /&gt;
== You will learn ==&lt;br /&gt;
&lt;br /&gt;
Here you are going to be to be more familiar with all MAC protocols implemented on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. You are going to understand the organization of MAC protocols, where all files are located and where you should start when you want to modify a protocol. You will also learn the steps you need to take in order to implement your own MAC protocol. And finally, we evaluate two famous MAC protocols: &amp;#039;&amp;#039;&amp;#039;ContikiMAC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;X-MAC&amp;#039;&amp;#039;&amp;#039;, showing a few statistics regarding their performance.&lt;br /&gt;
&lt;br /&gt;
== MAC, RDC and Framer drivers ==&lt;br /&gt;
&lt;br /&gt;
The network stack implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; is a little bit different than the usual 5-layers model typically adopted in TCP/IP. In-between the &amp;#039;&amp;#039;Physical&amp;#039;&amp;#039; and the &amp;#039;&amp;#039;Network&amp;#039;&amp;#039; layers, where usually is located the MAC, we have 3 different layers: &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039;&amp;#039;. The figure below shows the organization of layers in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[File:contiki-layers.jpg|150px|center]]&lt;br /&gt;
&lt;br /&gt;
The network layers can be accessed through the global variables &amp;#039;&amp;#039;&amp;#039;NETSTACK_FRAMER&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;NETSTACK_RDC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;NETSTACK_MAC&amp;#039;&amp;#039;&amp;#039;, which are defined in compilation time. All the variables can be found in &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039;. Every network layer is defined by the pragmas &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; in the beginning of this file.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layer is not a regular layer implementation; it is actually a collection of auxiliary functions that are called for creating a frame with data to be transmited and parsing of data being received. Examples of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; types can be found in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; there are two types of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layers: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-802154.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039; layer takes care of the sleep period of nodes. This is the most important layer because it is the one responsible for deciding exactly when the packets will be transmitted and it is responsible for making sure that the node is awake when packets are to be received. RDC protocols&amp;#039; source codes are also located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Examples of RDC layers that are implemented: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;contikimac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;xmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;lpp.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc-noframer.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Finally, the MAC layer takes care of addressing and retransmission of lost packets. The source files can be located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; we have only two types of MAC layers available: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;csma.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Framers ==&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; consists of a collection of auxiliary functions that are called before transmitting a packet and after their reception. The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; structure, called &amp;#039;&amp;#039;struct framer&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/framer.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
struct framer {&lt;br /&gt;
&lt;br /&gt;
  int (* create)(void);&lt;br /&gt;
  int (* parse)(void);&lt;br /&gt;
&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function &amp;#039;&amp;#039;create&amp;#039;&amp;#039; should create a new frame to be transmitted. While function &amp;#039;&amp;#039;parse&amp;#039;&amp;#039; will be called for parsing a received frame.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-nullmac&amp;#039;&amp;#039; is defined by files &amp;#039;&amp;#039;core/net/mac/framer-nullmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-nullmac.h&amp;#039;&amp;#039;. It should be used together with &amp;#039;&amp;#039;nullmac&amp;#039;&amp;#039; (MAC layer). The auxiliary function simply fill in the 2 fields of &amp;#039;&amp;#039;nullmac_hdr&amp;#039;&amp;#039;, which are: receiver address and sender address. This is the very basic type of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; that can be used on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-802154&amp;#039;&amp;#039; is defined by &amp;#039;&amp;#039;core/net/mac/framer-802154.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-802154.h&amp;#039;&amp;#039;. It creates and parses frames compatible with standard IEEE 802.15.4 (2003). The &amp;#039;&amp;#039;create&amp;#039;&amp;#039; and &amp;#039;&amp;#039;parse&amp;#039;&amp;#039; functions manipulate all header information abstractedly through the &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure. The real work is done by functions located in the file &amp;#039;&amp;#039;core/net/mac/frame802154.c&amp;#039;&amp;#039;, which are responsible for reading all information from receive/transmit buffer and inserting/extracting them appropriately into &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure.&lt;br /&gt;
&lt;br /&gt;
== RDC protocols ==&lt;br /&gt;
&lt;br /&gt;
The RDC structure, called &amp;#039;&amp;#039;struct rdc_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/rdc.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a RDC (radio duty cycling) driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct rdc_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the RDC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet list */&lt;br /&gt;
  void (* send_list)(mac_callback_t sent_callback, void *ptr, struct rdc_buf_list *list);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function &amp;#039;&amp;#039;input&amp;#039;&amp;#039; is called when a new packet has arrived and need to be processed by RDC layer. And functions &amp;#039;&amp;#039;send&amp;#039;&amp;#039; and &amp;#039;&amp;#039;send_list&amp;#039;&amp;#039; are called for transmitting out-going packets.&lt;br /&gt;
&lt;br /&gt;
There are 2 very basic types of RDC layers implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: &amp;#039;&amp;#039;nullrdc-noframer&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc-noframer.c&amp;#039;&amp;#039;) and &amp;#039;&amp;#039;nullrdc&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc.c&amp;#039;&amp;#039;). The first one does not use Framer functions, and transmits/receives data directly to Radio layer (Physical). The developer can take total control over the format of the packets being transmitted. &lt;br /&gt;
&lt;br /&gt;
The second RDC layer (nullrdc) uses the Framer functions for header creation/parsing. It does not save energy and works as a pass-through layer that only transmits a packet and returns the results of such transmission (success or collision).&lt;br /&gt;
&lt;br /&gt;
A third simple RDC layer is &amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/sicslowmac.c&amp;#039;&amp;#039;). This is a RDC layer with no energy savings and that uses IEEE 8021.5.4 frames.&lt;br /&gt;
&lt;br /&gt;
The three last protocols available in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; are implementations of popular asynchronous MAC protocols that aim at energy saving; namely &amp;#039;&amp;#039;&amp;#039;X-MA&amp;#039;&amp;#039;&amp;#039;C, &amp;#039;&amp;#039;&amp;#039;ContikiMac&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;LPP&amp;#039;&amp;#039;&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
X-MAC is a short-preamble protocol from 2006 that was ported to ContikiOS [1]. X-MAC has two different implementation in ContikiOS, which can be found in files &amp;#039;&amp;#039;core/net/mac/cxmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/xmac.c&amp;#039;&amp;#039;; the implementation in cxmac.c is a relaxed implementation that does not require stringent timing precision and work on larger number of platforms. &lt;br /&gt;
&lt;br /&gt;
ContikiMac [2] is a protocol that proposes enhancement over X-MAC. The implementation can be found in file &amp;#039;&amp;#039;core/net/mac/contikimac.c&amp;#039;&amp;#039;. Finally, LPP (Low Power Probing) is a receiver-initiated low power protocol from 2008 that was also ported to ContikiOS [3]; the code can be found in &amp;#039;&amp;#039;core/net/mac/lpp.c&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Each of the last three RDC protocols can be used for energy saving, since they operate in duty-cycle mode. The details of each of these protocols is outside the scope of this tutorial and can be found in the references. One important parameter that need to be defined for the operation of these protocols is the channel check rate. This parameter defines the frequency that nodes will listen to the medium to eventually receive data from their neighbors. When some activity is detected the nodes stay awake to receive data; when there is no activity the nodes go back to sleep mode for another duty-cycling period. This parameter that defines the channel check rate is NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE, and it should be set to a power of 2 (2, 4, 8, 16, 32, etc.); its unit is Hz (how many times the nodes will check medium in second). Below we show how to change this parameter as well as others.&lt;br /&gt;
&lt;br /&gt;
== MAC protocols ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The MAC structure, called &amp;#039;&amp;#039;struct mac_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/mac.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a MAC protocol driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct mac_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the MAC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
  &lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are only two types of MAC protocols in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: nullmac (&amp;#039;&amp;#039;core/net/mac/nullmac.c&amp;#039;&amp;#039;) and csma (&amp;#039;&amp;#039;core/net/mac/csma&amp;#039;&amp;#039;). The first one is a simple pass-through protocol that simply calls the appropriate RDC functions. And the second one implements addressing, sequence number and retransmissions. CSMA protocol keep a list of packets to each of the neighbors and calculate statistics such as number of retransmissions, collisions, deferrals, etc. Even though the name, CSMA mean Carrier-Sense Medium Access, the implementation in ContikiOS does not rely on the carrier sensing, because the medium access is performed by RDC protocol.&lt;br /&gt;
&lt;br /&gt;
== How to choose and change your protocols ==&lt;br /&gt;
&lt;br /&gt;
All three stack layers (Framer, RDC and MAC) need to be defined during compilation time. They are define using pragma &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; either in the Makefile or in the &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file. The definitions of which protocols should be used are used by file &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039; that effectively binds the NETSTACK_FRAMER, NETSTACK_RDC and NETSTACK_MAC to the protocols that are supposed to be used.&lt;br /&gt;
&lt;br /&gt;
Below it is shown the piece of code responsible for setting the MAC layer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_NETWORK&lt;br /&gt;
#ifdef NETSTACK_CONF_NETWORK&lt;br /&gt;
#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK&lt;br /&gt;
#else /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#define NETSTACK_NETWORK rime_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#endif /* NETSTACK_NETWORK */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Initially the code checks if NETSTACK_NETWORK was already define (#ifndef NETSTACK_NETWORK). If it was not, the code then checks if NETSTACK_CONF_NETWORK was define. If NETSTACK_CONF_NETWORK was defined, the desired network protocol is used (#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK); otherwise a default protocol is used, which in this case is &amp;#039;&amp;#039;rime_driver&amp;#039;&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
Hence, if you want to use a particular network protocol, you can either define NETSTACK_NETWORK directly or define NETSTACK_CONF_NETWORK.&lt;br /&gt;
&lt;br /&gt;
If no definitions are made, by default ContikiOS stack layers will be:&lt;br /&gt;
&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Network layer&amp;#039;&amp;#039;&amp;#039; - rime_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;MAC layer&amp;#039;&amp;#039;&amp;#039; - nullmac_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;RDC&amp;#039;&amp;#039;&amp;#039; - nullrdc_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039; - framer_nullmac&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Radio&amp;#039;&amp;#039;&amp;#039; - nullradio_driver&lt;br /&gt;
&lt;br /&gt;
The stack layers are usually define inside file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039;, which overwrites the configuration of &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039;. There is one &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file for each platform; for instance, platform &amp;#039;&amp;#039;sky&amp;#039;&amp;#039; has &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; located inside folder &amp;#039;&amp;#039;platform/sky&amp;#039;&amp;#039;. Below we show the excerpt where stack layers are define:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_CONF_MAC&lt;br /&gt;
#define NETSTACK_CONF_MAC     csma_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_MAC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC&lt;br /&gt;
#define NETSTACK_CONF_RDC     contikimac_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE&lt;br /&gt;
#define NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE 8&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RADIO&lt;br /&gt;
#define NETSTACK_CONF_RADIO   cc2420_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RADIO */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_FRAMER&lt;br /&gt;
#define NETSTACK_CONF_FRAMER  framer_802154&lt;br /&gt;
#endif /* NETSTACK_CONF_FRAMER */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can see that, if we are using Tmote Sky hardware, by default the stack layer will be composed by &amp;#039;&amp;#039;csma_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;contikimac_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;framer_802154&amp;#039;&amp;#039; and &amp;#039;&amp;#039;cc2420_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== Changing the protocols ===&lt;br /&gt;
&lt;br /&gt;
If you want to change the protocols to be used you define them either inside the Makefile or creating a project configuration file.&lt;br /&gt;
&lt;br /&gt;
In the first case you should change Makefile, inputing the definitions in one single line, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
DEFINES = NETSTACK_CONF_RDC=cxmac_driver,NETSTACK_CONF_MAC=null_mac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the protocols that you want to use should be defined in the &amp;#039;&amp;#039;DEFINES&amp;#039;&amp;#039; line, using commas to separate them.&lt;br /&gt;
&lt;br /&gt;
The second option is to create a project configuration file. Let&amp;#039;s assume the file create was named &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039;. This file should be inserted into your Makefile, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
CFLAGS += -DPROJECT_CONF_H=\&amp;quot;project-conf.h\&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file you can define all protocols you want to use, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#define NETSTACK_CONF_MAC nullmac_driver&lt;br /&gt;
#define NETSTACK_CONF_RDC nullrc_driver&lt;br /&gt;
#define NETSTACK_CONF_FRAMER framer_nullmac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color: red&amp;quot;&amp;gt;If you choose to use &amp;#039;&amp;#039;projet-conf.h&amp;#039;&amp;#039; file, you will need to modifed the file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; inside your platform folder. If you are using sky platform, for example, you need to edit file &amp;#039;&amp;#039;platform/sky/contiki-conf.h&amp;#039;&amp;#039;. The last part of &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file should be moved to the beginning of the file, as shown below&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* -*- C -*- */&lt;br /&gt;
&lt;br /&gt;
#ifndef CONTIKI_CONF_H&lt;br /&gt;
#define CONTIKI_CONF_H&lt;br /&gt;
&lt;br /&gt;
/* include the project config */&lt;br /&gt;
/* PROJECT_CONF_H might be defined in the project Makefile */&lt;br /&gt;
#ifdef PROJECT_CONF_H&lt;br /&gt;
#include PROJECT_CONF_H&lt;br /&gt;
#endif /* PROJECT_CONF_H */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to code your own protocol ==&lt;br /&gt;
&lt;br /&gt;
We will explain here how to create a new protocol and integrate it into ContikiOS. The protocol can be a Network, MAC, RDC, Framer or Radio protocol; the steps for creating any one of these protocols are similar. We are going to show in details how to create a new MAC, called &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
First step is to create both header and c files for &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039; protocol. Both files need to be created inside &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Our files will be named &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The file &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039; is shown below. In this file we only declare an external variable that will reference our MAC protocols, called &amp;#039;&amp;#039;modifiedmac_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef __MODIFIEDMAC_H__&lt;br /&gt;
#define __MODIFIEDMAC_H__&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;net/mac/mac.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
extern const struct mac_driver modifiedmac_driver;&lt;br /&gt;
&lt;br /&gt;
#endif /* __MODIFIEDMAC_H__ */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An initial version of file &amp;#039;&amp;#039;modifiedrdc.c&amp;#039;&amp;#039; is shown below. It only contains the declaration of all functions that must be implemented in a MAC driver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;quot;net/mac/modifiedmac.h&amp;quot;&lt;br /&gt;
#include &amp;quot;net/netstack.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static unsigned short&lt;br /&gt;
channel_check_interval(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
init(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
const struct mac_driver modifiedmac_driver = {&lt;br /&gt;
  &amp;quot;modifiedmac&amp;quot;,&lt;br /&gt;
  init,&lt;br /&gt;
  send_packet,&lt;br /&gt;
  packet_input,&lt;br /&gt;
  on,&lt;br /&gt;
  off,&lt;br /&gt;
  channel_check_interval,&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The next step is to include our source code into ContikiOS make system. We just need to edit the file &amp;#039;&amp;#039;core/net/mac/Makefile.mac&amp;#039;&amp;#039; and include the file &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; in the list of Contiki source files. After editing, &amp;#039;&amp;#039;Makefile.mac&amp;#039;&amp;#039; must look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI_SOURCEFILES += cxmac.c xmac.c nullmac.c lpp.c frame802154.c sicslowmac.c nullrdc.c nullrdc-noframer.c mac.c&lt;br /&gt;
CONTIKI_SOURCEFILES += framer-nullmac.c framer-802154.c csma.c contikimac.c phase.c modifiedmac.c&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Below we show the functions that should be changed in order to make our MAC protocol functional. It is going to work exactly as a Null MAC protocol and we are counting the number of packets received and transmitted.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
uint16_t n_pkts_transmitted = 0;&lt;br /&gt;
uint16_t n_pkts_received = 0;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_transmitted++;&lt;br /&gt;
  NETSTACK_RDC.send(sent, ptr);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_received++;&lt;br /&gt;
  NETSTACK_NETWORK.input();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.on();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.off(keep_radio_on);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now you are able to use your create your own rules for addressing, framezation and all functions that should be performed by a MAC protocol.&lt;br /&gt;
&lt;br /&gt;
If you want to use your new protocol, you need to change the MAC layer that will be used by ContikiOS, following the instructions above, and setting definition NETSTACK_CONF_MAC as &amp;#039;&amp;#039;modifiedmac_driver&amp;#039;&amp;#039; (#define NETSTACK_CONF_MAC modifiedmac_driver).&lt;br /&gt;
&lt;br /&gt;
== Evaluating ContikiMAC and XMAC protocols ==&lt;br /&gt;
&lt;br /&gt;
We are going to evaluate the performance of the two most famous RDC protocols in ContikiOS, the &amp;#039;&amp;#039;ContikiMAC&amp;#039;&amp;#039; and &amp;#039;&amp;#039;X-MAC&amp;#039;&amp;#039;. We want to evaluate the Packet Reception Rate (PRR), which is the percentage of received packets in an interval; and also the average duty-cycle of sensor nodes. The PRR is related to the maximum throughput the network is able to handle; with lower PRR more packets are dropped and the total amount of data that can be transfered to the sink node is small. Also, the duty-cycle impacts the average power consumption and the lifetime of nodes, since the largest energy savings are achieved when nodes are sleeping; the lower the duty-cycle the longest will be the node&amp;#039;s lifetime.&lt;br /&gt;
&lt;br /&gt;
We considered the firmware in the folder &amp;#039;&amp;#039;examples/ipv6/rpl-udp/&amp;#039;&amp;#039;, which is a firmware based on IPv6 and RPL for UDP packet transmission. In the same folder there is one simulation file called &amp;#039;&amp;#039;rpl-udp-powertrace.csc&amp;#039;&amp;#039; that is composed of 1 sink node and 30 sensor nodes and traces the energy consumption of all nodes, and also keeps track of PRR values throughout the simulation. We considered the firmwares &amp;#039;&amp;#039;udp-server.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; and reduced the data transmission interval in &amp;#039;&amp;#039;udl-client.c&amp;#039;&amp;#039; to 1 second. The line below shows where the code of &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; was changed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef PERIOD&lt;br /&gt;
#define PERIOD 1&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By default the simulation is executed with ContikiMAC. We let the simulation run for 5 minutes before extracting the desired statistics. After simulation for ContikiMAC, we changed the RDC protocol, as described before, to X-MAC. And the same statistics were obtained. The figures below show the average duty-cycle of each node for both protocols. The figure on the left shows results of ContikiMAC and the one on the right shows results of X-MAC.&lt;br /&gt;
&lt;br /&gt;
[[File:power_contikimac.png|300px]] [[File:power_xmac.png|300px]]&lt;br /&gt;
&lt;br /&gt;
We can see that ContikiMAC reduces the average duty-cycle from 17% to 9%, which drastically impacts the energy consumption of the nodes.&lt;br /&gt;
&lt;br /&gt;
The PRR statistics were also analysed for all 30 sensor nodes. The average PRR of ContikiMAC was &amp;#039;&amp;#039;&amp;#039;50.48%&amp;#039;&amp;#039;&amp;#039; and the PRR of X-MAC was &amp;#039;&amp;#039;&amp;#039;30.38%&amp;#039;&amp;#039;&amp;#039;. We can conclude that ContikiMAC implementation is more energy and throughput efficient than X-MAC.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[http://www.cse.wustl.edu/~lu/cse521s/Papers/x-mac.pdf 1]- Buettner, Michael, Gary V. Yee, Eric Anderson, and Richard Han. &amp;quot;X-MAC: a short preamble MAC protocol for duty-cycled wireless sensor networks.&amp;quot; In Proceedings of the 4th international conference on Embedded networked sensor systems, pp. 307-320. ACM, 2006.&lt;br /&gt;
&lt;br /&gt;
[http://soda.swedish-ict.se/5128/1/contikimac-report.pdf 2] - Dunkels, Adam. &amp;quot;The contikimac radio duty cycling protocol.&amp;quot; (2011).&lt;br /&gt;
&lt;br /&gt;
[http://web.cs.jhu.edu/~cliang4/public/doc/2008_IPSN_Koala.pdf 3] - Liang, Chieh-Jan Mike, and Andreas Terzis. &amp;quot;Koala: Ultra-low power data retrieval in wireless sensor networks.&amp;quot; In Proceedings of the 7th international conference on Information processing in sensor networks, pp. 421-432. IEEE Computer Society, 2008.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
Edited by: Pedro&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1159</id>
		<title>MAC protocols in ContikiOS</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1159"/>
				<updated>2014-11-08T23:42:30Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* RDC protocols */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This tutorial covers the main features of &amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039; protocols available in &amp;#039;&amp;#039;ContikiOS 2.7&amp;#039;&amp;#039;. Medium Access Control protocols take care of the organization of medium access in wireless networks. They can be interpreted as rules that coordinate when each node is going to transmit/receive packets.&lt;br /&gt;
&lt;br /&gt;
There are a large number of protocols available in the literature, they can usually be classified as contention-based or reservation-based protocols. The first group is based on Carrier Sensing for detecting medium activity and are prone to collisions and lower efficiency; however, they are of easy implementation. The second group is more efficient in terms of throughput and energy, but require precise synchronization and is less adaptable to dynamic traffic. &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; only includes MAC protocols of the first category.&lt;br /&gt;
&lt;br /&gt;
There are about 5 types of MAC layers that can be used in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. They are usually implementation of well-known MAC protocols for &amp;#039;&amp;#039;Wireless Sensor Networks&amp;#039;&amp;#039;; a few of them are implementations of protocols developed exclusively for &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;, but that were based on previous work present in the literature.&lt;br /&gt;
&lt;br /&gt;
== You will learn ==&lt;br /&gt;
&lt;br /&gt;
Here you are going to be to be more familiar with all MAC protocols implemented on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. You are going to understand the organization of MAC protocols, where all files are located and where you should start when you want to modify a protocol. You will also learn the steps you need to take in order to implement your own MAC protocol. And finally, we evaluate two famous MAC protocols: &amp;#039;&amp;#039;&amp;#039;ContikiMAC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;X-MAC&amp;#039;&amp;#039;&amp;#039;, showing a few statistics regarding their performance.&lt;br /&gt;
&lt;br /&gt;
== MAC, RDC and Framer drivers ==&lt;br /&gt;
&lt;br /&gt;
The network stack implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; is a little bit different than the usual 5-layers model typically adopted in TCP/IP. In-between the &amp;#039;&amp;#039;Physical&amp;#039;&amp;#039; and the &amp;#039;&amp;#039;Network&amp;#039;&amp;#039; layers, where usually is located the MAC, we have 3 different layers: &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039;&amp;#039;. The figure below shows the organization of layers in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[File:contiki-layers.jpg|150px|center]]&lt;br /&gt;
&lt;br /&gt;
The network layers can be accessed through the global variables &amp;#039;&amp;#039;&amp;#039;NETSTACK_FRAMER&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;NETSTACK_RDC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;NETSTACK_MAC&amp;#039;&amp;#039;&amp;#039;, which are defined in compilation time. All the variables can be found in &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039;. Every network layer is defined by the pragmas &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; in the beginning of this file.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layer is not a regular layer implementation; it is actually a collection of auxiliary functions that are called for creating a frame with data to be transmited and parsing of data being received. Examples of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; types can be found in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; there are two types of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layers: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-802154.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039; layer takes care of the sleep period of nodes. This is the most important layer because it is the one responsible for deciding exactly when the packets will be transmitted and it is responsible for making sure that the node is awake when packets are to be received. RDC protocols&amp;#039; source codes are also located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Examples of RDC layers that are implemented: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;contikimac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;xmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;lpp.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc-noframer.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Finally, the MAC layer takes care of addressing and retransmission of lost packets. The source files can be located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; we have only two types of MAC layers available: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;csma.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Framers ==&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; consists of a collection of auxiliary functions that are called before transmitting a packet and after their reception. The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; structure, called &amp;#039;&amp;#039;struct framer&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/framer.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
struct framer {&lt;br /&gt;
&lt;br /&gt;
  int (* create)(void);&lt;br /&gt;
  int (* parse)(void);&lt;br /&gt;
&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function &amp;#039;&amp;#039;create&amp;#039;&amp;#039; should create a new frame to be transmitted. While function &amp;#039;&amp;#039;parse&amp;#039;&amp;#039; will be called for parsing a received frame.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-nullmac&amp;#039;&amp;#039; is defined by files &amp;#039;&amp;#039;core/net/mac/framer-nullmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-nullmac.h&amp;#039;&amp;#039;. It should be used together with &amp;#039;&amp;#039;nullmac&amp;#039;&amp;#039; (MAC layer). The auxiliary function simply fill in the 2 fields of &amp;#039;&amp;#039;nullmac_hdr&amp;#039;&amp;#039;, which are: receiver address and sender address. This is the very basic type of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; that can be used on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-802154&amp;#039;&amp;#039; is defined by &amp;#039;&amp;#039;core/net/mac/framer-802154.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-802154.h&amp;#039;&amp;#039;. It creates and parses frames compatible with standard IEEE 802.15.4 (2003). The &amp;#039;&amp;#039;create&amp;#039;&amp;#039; and &amp;#039;&amp;#039;parse&amp;#039;&amp;#039; functions manipulate all header information abstractedly through the &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure. The real work is done by functions located in the file &amp;#039;&amp;#039;core/net/mac/frame802154.c&amp;#039;&amp;#039;, which are responsible for reading all information from receive/transmit buffer and inserting/extracting them appropriately into &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure.&lt;br /&gt;
&lt;br /&gt;
== RDC protocols ==&lt;br /&gt;
&lt;br /&gt;
The RDC structure, called &amp;#039;&amp;#039;struct rdc_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/rdc.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a RDC (radio duty cycling) driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct rdc_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the RDC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet list */&lt;br /&gt;
  void (* send_list)(mac_callback_t sent_callback, void *ptr, struct rdc_buf_list *list);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function &amp;#039;&amp;#039;input&amp;#039;&amp;#039; is called when a new packet has arrived and need to be processed by RDC layer. And functions &amp;#039;&amp;#039;send&amp;#039;&amp;#039; and &amp;#039;&amp;#039;send_list&amp;#039;&amp;#039; are called for transmitting out-going packets.&lt;br /&gt;
&lt;br /&gt;
There are 2 very basic types of RDC layers implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: &amp;#039;&amp;#039;nullrdc-noframer&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc-noframer.c&amp;#039;&amp;#039;) and &amp;#039;&amp;#039;nullrdc&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc.c&amp;#039;&amp;#039;). The first one does not use Framer functions, and transmits/receives data directly to Radio layer (Physical). The developer can take total control over the format of the packets being transmitted. &lt;br /&gt;
&lt;br /&gt;
The second RDC layer (nullrdc) uses the Framer functions for header creation/parsing. It does not save energy and works as a pass-through layer that only transmits a packet and returns the results of such transmission (success or collision).&lt;br /&gt;
&lt;br /&gt;
A third simple RDC layer is &amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/sicslowmac.c&amp;#039;&amp;#039;). This is a RDC layer with no energy savings and that uses IEEE 8021.5.4 frames.&lt;br /&gt;
&lt;br /&gt;
The three last protocols available in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; are implementations of popular asynchronous MAC protocols that aim at energy saving; namely &amp;#039;&amp;#039;&amp;#039;X-MA&amp;#039;&amp;#039;&amp;#039;C, &amp;#039;&amp;#039;&amp;#039;ContikiMac&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;LPP&amp;#039;&amp;#039;&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
X-MAC is a short-preamble protocol from 2006 that was ported to ContikiOS [1]. X-MAC has two different implementation in ContikiOS, which can be found in files &amp;#039;&amp;#039;core/net/mac/cxmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/xmac.c&amp;#039;&amp;#039;; the implementation in cxmac.c is a relaxed implementation that does not require stringent timing precision and work on larger number of platforms. &lt;br /&gt;
&lt;br /&gt;
ContikiMac [2] is a protocol that proposes enhancement over X-MAC. The implementation can be found in file &amp;#039;&amp;#039;core/net/mac/contikimac.c&amp;#039;&amp;#039;. Finally, LPP (Low Power Probing) is a receiver-initiated low power protocol from 2008 that was also ported to ContikiOS [3]; the code can be found in &amp;#039;&amp;#039;core/net/mac/lpp.c&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Each of the last three RDC protocols can be used for energy saving, since they operate in duty-cycle mode. The details of each of these protocols is outside the scope of this tutorial and can be found in the references. One important parameter that need to be defined for the operation of these protocols is the channel check rate. This parameter defines the frequency that nodes will listen to the medium to eventually receive data from their neighbors. When some activity is detected the nodes stay awake to receive data; when there is no activity the nodes go back to sleep mode for another duty-cycling period. This parameter that defines the channel check rate is NETSTACK_RDC_CHANNEL_CHECK_RATE, and it should be set as described below.&lt;br /&gt;
&lt;br /&gt;
== MAC protocols ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The MAC structure, called &amp;#039;&amp;#039;struct mac_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/mac.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a MAC protocol driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct mac_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the MAC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
  &lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are only two types of MAC protocols in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: nullmac (&amp;#039;&amp;#039;core/net/mac/nullmac.c&amp;#039;&amp;#039;) and csma (&amp;#039;&amp;#039;core/net/mac/csma&amp;#039;&amp;#039;). The first one is a simple pass-through protocol that simply calls the appropriate RDC functions. And the second one implements addressing, sequence number and retransmissions. CSMA protocol keep a list of packets to each of the neighbors and calculate statistics such as number of retransmissions, collisions, deferrals, etc. Even though the name, CSMA mean Carrier-Sense Medium Access, the implementation in ContikiOS does not rely on the carrier sensing, because the medium access is performed by RDC protocol.&lt;br /&gt;
&lt;br /&gt;
== How to choose and change your protocols ==&lt;br /&gt;
&lt;br /&gt;
All three stack layers (Framer, RDC and MAC) need to be defined during compilation time. They are define using pragma &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; either in the Makefile or in the &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file. The definitions of which protocols should be used are used by file &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039; that effectively binds the NETSTACK_FRAMER, NETSTACK_RDC and NETSTACK_MAC to the protocols that are supposed to be used.&lt;br /&gt;
&lt;br /&gt;
Below it is shown the piece of code responsible for setting the MAC layer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_NETWORK&lt;br /&gt;
#ifdef NETSTACK_CONF_NETWORK&lt;br /&gt;
#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK&lt;br /&gt;
#else /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#define NETSTACK_NETWORK rime_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#endif /* NETSTACK_NETWORK */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Initially the code checks if NETSTACK_NETWORK was already define (#ifndef NETSTACK_NETWORK). If it was not, the code then checks if NETSTACK_CONF_NETWORK was define. If NETSTACK_CONF_NETWORK was defined, the desired network protocol is used (#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK); otherwise a default protocol is used, which in this case is &amp;#039;&amp;#039;rime_driver&amp;#039;&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
Hence, if you want to use a particular network protocol, you can either define NETSTACK_NETWORK directly or define NETSTACK_CONF_NETWORK.&lt;br /&gt;
&lt;br /&gt;
If no definitions are made, by default ContikiOS stack layers will be:&lt;br /&gt;
&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Network layer&amp;#039;&amp;#039;&amp;#039; - rime_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;MAC layer&amp;#039;&amp;#039;&amp;#039; - nullmac_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;RDC&amp;#039;&amp;#039;&amp;#039; - nullrdc_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039; - framer_nullmac&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Radio&amp;#039;&amp;#039;&amp;#039; - nullradio_driver&lt;br /&gt;
&lt;br /&gt;
The stack layers are usually define inside file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039;, which overwrites the configuration of &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039;. There is one &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file for each platform; for instance, platform &amp;#039;&amp;#039;sky&amp;#039;&amp;#039; has &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; located inside folder &amp;#039;&amp;#039;platform/sky&amp;#039;&amp;#039;. Below we show the excerpt where stack layers are define:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_CONF_MAC&lt;br /&gt;
#define NETSTACK_CONF_MAC     csma_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_MAC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC&lt;br /&gt;
#define NETSTACK_CONF_RDC     contikimac_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE&lt;br /&gt;
#define NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE 8&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RADIO&lt;br /&gt;
#define NETSTACK_CONF_RADIO   cc2420_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RADIO */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_FRAMER&lt;br /&gt;
#define NETSTACK_CONF_FRAMER  framer_802154&lt;br /&gt;
#endif /* NETSTACK_CONF_FRAMER */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can see that, if we are using Tmote Sky hardware, by default the stack layer will be composed by &amp;#039;&amp;#039;csma_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;contikimac_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;framer_802154&amp;#039;&amp;#039; and &amp;#039;&amp;#039;cc2420_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== Changing the protocols ===&lt;br /&gt;
&lt;br /&gt;
If you want to change the protocols to be used you define them either inside the Makefile or creating a project configuration file.&lt;br /&gt;
&lt;br /&gt;
In the first case you should change Makefile, inputing the definitions in one single line, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
DEFINES = NETSTACK_CONF_RDC=cxmac_driver,NETSTACK_CONF_MAC=null_mac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the protocols that you want to use should be defined in the &amp;#039;&amp;#039;DEFINES&amp;#039;&amp;#039; line, using commas to separate them.&lt;br /&gt;
&lt;br /&gt;
The second option is to create a project configuration file. Let&amp;#039;s assume the file create was named &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039;. This file should be inserted into your Makefile, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
CFLAGS += -DPROJECT_CONF_H=\&amp;quot;project-conf.h\&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file you can define all protocols you want to use, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#define NETSTACK_CONF_MAC nullmac_driver&lt;br /&gt;
#define NETSTACK_CONF_RDC nullrc_driver&lt;br /&gt;
#define NETSTACK_CONF_FRAMER framer_nullmac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color: red&amp;quot;&amp;gt;If you choose to use &amp;#039;&amp;#039;projet-conf.h&amp;#039;&amp;#039; file, you will need to modifed the file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; inside your platform folder. If you are using sky platform, for example, you need to edit file &amp;#039;&amp;#039;platform/sky/contiki-conf.h&amp;#039;&amp;#039;. The last part of &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file should be moved to the beginning of the file, as shown below&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* -*- C -*- */&lt;br /&gt;
&lt;br /&gt;
#ifndef CONTIKI_CONF_H&lt;br /&gt;
#define CONTIKI_CONF_H&lt;br /&gt;
&lt;br /&gt;
/* include the project config */&lt;br /&gt;
/* PROJECT_CONF_H might be defined in the project Makefile */&lt;br /&gt;
#ifdef PROJECT_CONF_H&lt;br /&gt;
#include PROJECT_CONF_H&lt;br /&gt;
#endif /* PROJECT_CONF_H */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to code your own protocol ==&lt;br /&gt;
&lt;br /&gt;
We will explain here how to create a new protocol and integrate it into ContikiOS. The protocol can be a Network, MAC, RDC, Framer or Radio protocol; the steps for creating any one of these protocols are similar. We are going to show in details how to create a new MAC, called &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
First step is to create both header and c files for &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039; protocol. Both files need to be created inside &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Our files will be named &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The file &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039; is shown below. In this file we only declare an external variable that will reference our MAC protocols, called &amp;#039;&amp;#039;modifiedmac_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef __MODIFIEDMAC_H__&lt;br /&gt;
#define __MODIFIEDMAC_H__&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;net/mac/mac.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
extern const struct mac_driver modifiedmac_driver;&lt;br /&gt;
&lt;br /&gt;
#endif /* __MODIFIEDMAC_H__ */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An initial version of file &amp;#039;&amp;#039;modifiedrdc.c&amp;#039;&amp;#039; is shown below. It only contains the declaration of all functions that must be implemented in a MAC driver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;quot;net/mac/modifiedmac.h&amp;quot;&lt;br /&gt;
#include &amp;quot;net/netstack.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static unsigned short&lt;br /&gt;
channel_check_interval(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
init(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
const struct mac_driver modifiedmac_driver = {&lt;br /&gt;
  &amp;quot;modifiedmac&amp;quot;,&lt;br /&gt;
  init,&lt;br /&gt;
  send_packet,&lt;br /&gt;
  packet_input,&lt;br /&gt;
  on,&lt;br /&gt;
  off,&lt;br /&gt;
  channel_check_interval,&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The next step is to include our source code into ContikiOS make system. We just need to edit the file &amp;#039;&amp;#039;core/net/mac/Makefile.mac&amp;#039;&amp;#039; and include the file &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; in the list of Contiki source files. After editing, &amp;#039;&amp;#039;Makefile.mac&amp;#039;&amp;#039; must look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI_SOURCEFILES += cxmac.c xmac.c nullmac.c lpp.c frame802154.c sicslowmac.c nullrdc.c nullrdc-noframer.c mac.c&lt;br /&gt;
CONTIKI_SOURCEFILES += framer-nullmac.c framer-802154.c csma.c contikimac.c phase.c modifiedmac.c&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Below we show the functions that should be changed in order to make our MAC protocol functional. It is going to work exactly as a Null MAC protocol and we are counting the number of packets received and transmitted.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
uint16_t n_pkts_transmitted = 0;&lt;br /&gt;
uint16_t n_pkts_received = 0;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_transmitted++;&lt;br /&gt;
  NETSTACK_RDC.send(sent, ptr);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_received++;&lt;br /&gt;
  NETSTACK_NETWORK.input();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.on();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.off(keep_radio_on);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now you are able to use your create your own rules for addressing, framezation and all functions that should be performed by a MAC protocol.&lt;br /&gt;
&lt;br /&gt;
If you want to use your new protocol, you need to change the MAC layer that will be used by ContikiOS, following the instructions above, and setting definition NETSTACK_CONF_MAC as &amp;#039;&amp;#039;modifiedmac_driver&amp;#039;&amp;#039; (#define NETSTACK_CONF_MAC modifiedmac_driver).&lt;br /&gt;
&lt;br /&gt;
== Evaluating ContikiMAC and XMAC protocols ==&lt;br /&gt;
&lt;br /&gt;
We are going to evaluate the performance of the two most famous RDC protocols in ContikiOS, the &amp;#039;&amp;#039;ContikiMAC&amp;#039;&amp;#039; and &amp;#039;&amp;#039;X-MAC&amp;#039;&amp;#039;. We want to evaluate the Packet Reception Rate (PRR), which is the percentage of received packets in an interval; and also the average duty-cycle of sensor nodes. The PRR is related to the maximum throughput the network is able to handle; with lower PRR more packets are dropped and the total amount of data that can be transfered to the sink node is small. Also, the duty-cycle impacts the average power consumption and the lifetime of nodes, since the largest energy savings are achieved when nodes are sleeping; the lower the duty-cycle the longest will be the node&amp;#039;s lifetime.&lt;br /&gt;
&lt;br /&gt;
We considered the firmware in the folder &amp;#039;&amp;#039;examples/ipv6/rpl-udp/&amp;#039;&amp;#039;, which is a firmware based on IPv6 and RPL for UDP packet transmission. In the same folder there is one simulation file called &amp;#039;&amp;#039;rpl-udp-powertrace.csc&amp;#039;&amp;#039; that is composed of 1 sink node and 30 sensor nodes and traces the energy consumption of all nodes, and also keeps track of PRR values throughout the simulation. We considered the firmwares &amp;#039;&amp;#039;udp-server.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; and reduced the data transmission interval in &amp;#039;&amp;#039;udl-client.c&amp;#039;&amp;#039; to 1 second. The line below shows where the code of &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; was changed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef PERIOD&lt;br /&gt;
#define PERIOD 1&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By default the simulation is executed with ContikiMAC. We let the simulation run for 5 minutes before extracting the desired statistics. After simulation for ContikiMAC, we changed the RDC protocol, as described before, to X-MAC. And the same statistics were obtained. The figures below show the average duty-cycle of each node for both protocols. The figure on the left shows results of ContikiMAC and the one on the right shows results of X-MAC.&lt;br /&gt;
&lt;br /&gt;
[[File:power_contikimac.png|300px]] [[File:power_xmac.png|300px]]&lt;br /&gt;
&lt;br /&gt;
We can see that ContikiMAC reduces the average duty-cycle from 17% to 9%, which drastically impacts the energy consumption of the nodes.&lt;br /&gt;
&lt;br /&gt;
The PRR statistics were also analysed for all 30 sensor nodes. The average PRR of ContikiMAC was &amp;#039;&amp;#039;&amp;#039;50.48%&amp;#039;&amp;#039;&amp;#039; and the PRR of X-MAC was &amp;#039;&amp;#039;&amp;#039;30.38%&amp;#039;&amp;#039;&amp;#039;. We can conclude that ContikiMAC implementation is more energy and throughput efficient than X-MAC.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[http://www.cse.wustl.edu/~lu/cse521s/Papers/x-mac.pdf 1]- Buettner, Michael, Gary V. Yee, Eric Anderson, and Richard Han. &amp;quot;X-MAC: a short preamble MAC protocol for duty-cycled wireless sensor networks.&amp;quot; In Proceedings of the 4th international conference on Embedded networked sensor systems, pp. 307-320. ACM, 2006.&lt;br /&gt;
&lt;br /&gt;
[http://soda.swedish-ict.se/5128/1/contikimac-report.pdf 2] - Dunkels, Adam. &amp;quot;The contikimac radio duty cycling protocol.&amp;quot; (2011).&lt;br /&gt;
&lt;br /&gt;
[http://web.cs.jhu.edu/~cliang4/public/doc/2008_IPSN_Koala.pdf 3] - Liang, Chieh-Jan Mike, and Andreas Terzis. &amp;quot;Koala: Ultra-low power data retrieval in wireless sensor networks.&amp;quot; In Proceedings of the 7th international conference on Information processing in sensor networks, pp. 421-432. IEEE Computer Society, 2008.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
Edited by: Pedro&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1118</id>
		<title>MAC protocols in ContikiOS</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1118"/>
				<updated>2014-11-08T20:33:03Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* Evaluating ContikiMAC and XMAC protocols */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This tutorial covers the main features of &amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039; protocols available in &amp;#039;&amp;#039;ContikiOS 2.7&amp;#039;&amp;#039;. Medium Access Control protocols take care of the organization of medium access in wireless networks. They can be interpreted as rules that coordinate when each node is going to transmit/receive packets.&lt;br /&gt;
&lt;br /&gt;
There are a large number of protocols available in the literature, they can usually be classified as contention-based or reservation-based protocols. The first group is based on Carrier Sensing for detecting medium activity and are prone to collisions and lower efficiency; however, they are of easy implementation. The second group is more efficient in terms of throughput and energy, but require precise synchronization and is less adaptable to dynamic traffic. &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; only includes MAC protocols of the first category.&lt;br /&gt;
&lt;br /&gt;
There are about 5 types of MAC layers that can be used in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. They are usually implementation of well-known MAC protocols for &amp;#039;&amp;#039;Wireless Sensor Networks&amp;#039;&amp;#039;; a few of them are implementations of protocols developed exclusively for &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;, but that were based on previous work present in the literature.&lt;br /&gt;
&lt;br /&gt;
== You will learn ==&lt;br /&gt;
&lt;br /&gt;
Here you are going to be to be more familiar with all MAC protocols implemented on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. You are going to understand the organization of MAC protocols, where all files are located and where you should start when you want to modify a protocol. You will also learn the steps you need to take in order to implement your own MAC protocol. And finally, we evaluate two famous MAC protocols: &amp;#039;&amp;#039;&amp;#039;ContikiMAC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;X-MAC&amp;#039;&amp;#039;&amp;#039;, showing a few statistics regarding their performance.&lt;br /&gt;
&lt;br /&gt;
== MAC, RDC and Framer drivers ==&lt;br /&gt;
&lt;br /&gt;
The network stack implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; is a little bit different than the usual 5-layers model typically adopted in TCP/IP. In-between the &amp;#039;&amp;#039;Physical&amp;#039;&amp;#039; and the &amp;#039;&amp;#039;Network&amp;#039;&amp;#039; layers, where usually is located the MAC, we have 3 different layers: &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039;&amp;#039;. The figure below shows the organization of layers in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[File:contiki-layers.jpg|150px|center]]&lt;br /&gt;
&lt;br /&gt;
The network layers can be accessed through the global variables &amp;#039;&amp;#039;&amp;#039;NETSTACK_FRAMER&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;NETSTACK_RDC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;NETSTACK_MAC&amp;#039;&amp;#039;&amp;#039;, which are defined in compilation time. All the variables can be found in &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039;. Every network layer is defined by the pragmas &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; in the beginning of this file.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layer is not a regular layer implementation; it is actually a collection of auxiliary functions that are called for creating a frame with data to be transmited and parsing of data being received. Examples of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; types can be found in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; there are two types of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layers: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-802154.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039; layer takes care of the sleep period of nodes. This is the most important layer because it is the one responsible for deciding exactly when the packets will be transmitted and it is responsible for making sure that the node is awake when packets are to be received. RDC protocols&amp;#039; source codes are also located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Examples of RDC layers that are implemented: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;contikimac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;xmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;lpp.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc-noframer.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Finally, the MAC layer takes care of addressing and retransmission of lost packets. The source files can be located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; we have only two types of MAC layers available: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;csma.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Framers ==&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; consists of a collection of auxiliary functions that are called before transmitting a packet and after their reception. The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; structure, called &amp;#039;&amp;#039;struct framer&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/framer.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
struct framer {&lt;br /&gt;
&lt;br /&gt;
  int (* create)(void);&lt;br /&gt;
  int (* parse)(void);&lt;br /&gt;
&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function &amp;#039;&amp;#039;create&amp;#039;&amp;#039; should create a new frame to be transmitted. While function &amp;#039;&amp;#039;parse&amp;#039;&amp;#039; will be called for parsing a received frame.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-nullmac&amp;#039;&amp;#039; is defined by files &amp;#039;&amp;#039;core/net/mac/framer-nullmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-nullmac.h&amp;#039;&amp;#039;. It should be used together with &amp;#039;&amp;#039;nullmac&amp;#039;&amp;#039; (MAC layer). The auxiliary function simply fill in the 2 fields of &amp;#039;&amp;#039;nullmac_hdr&amp;#039;&amp;#039;, which are: receiver address and sender address. This is the very basic type of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; that can be used on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-802154&amp;#039;&amp;#039; is defined by &amp;#039;&amp;#039;core/net/mac/framer-802154.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-802154.h&amp;#039;&amp;#039;. It creates and parses frames compatible with standard IEEE 802.15.4 (2003). The &amp;#039;&amp;#039;create&amp;#039;&amp;#039; and &amp;#039;&amp;#039;parse&amp;#039;&amp;#039; functions manipulate all header information abstractedly through the &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure. The real work is done by functions located in the file &amp;#039;&amp;#039;core/net/mac/frame802154.c&amp;#039;&amp;#039;, which are responsible for reading all information from receive/transmit buffer and inserting/extracting them appropriately into &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure.&lt;br /&gt;
&lt;br /&gt;
== RDC protocols ==&lt;br /&gt;
&lt;br /&gt;
The RDC structure, called &amp;#039;&amp;#039;struct rdc_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/rdc.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a RDC (radio duty cycling) driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct rdc_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the RDC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet list */&lt;br /&gt;
  void (* send_list)(mac_callback_t sent_callback, void *ptr, struct rdc_buf_list *list);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function &amp;#039;&amp;#039;input&amp;#039;&amp;#039; is called when a new packet has arrived and need to be processed by RDC layer. And functions &amp;#039;&amp;#039;send&amp;#039;&amp;#039; and &amp;#039;&amp;#039;send_list&amp;#039;&amp;#039; are called for transmitting out-going packets.&lt;br /&gt;
&lt;br /&gt;
There are 2 very basic types of RDC layers implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: &amp;#039;&amp;#039;nullrdc-noframer&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc-noframer.c&amp;#039;&amp;#039;) and &amp;#039;&amp;#039;nullrdc&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc.c&amp;#039;&amp;#039;). The first one does not use Framer functions, and transmits/receives data directly to Radio layer (Physical). The developer can take total control over the format of the packets being transmitted. &lt;br /&gt;
&lt;br /&gt;
The second RDC layer (nullrdc) uses the Framer functions for header creation/parsing. It does not save energy and works as a pass-through layer that only transmits a packet and returns the results of such transmission (success or collision).&lt;br /&gt;
&lt;br /&gt;
A third simple RDC layer is &amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/sicslowmac.c&amp;#039;&amp;#039;). This is a RDC layer with no energy savings and that uses IEEE 8021.5.4 frames.&lt;br /&gt;
&lt;br /&gt;
The three last protocols available in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; are implementations of popular asynchronous MAC protocols that aim at energy saving; namely &amp;#039;&amp;#039;&amp;#039;X-MA&amp;#039;&amp;#039;&amp;#039;C, &amp;#039;&amp;#039;&amp;#039;ContikiMac&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;LPP&amp;#039;&amp;#039;&amp;#039;. X-MAC is a short-preamble protocol from 2006 that was ported to ContikiOS [1]. X-MAC has two different implementation in ContikiOS, which can be found in files &amp;#039;&amp;#039;core/net/mac/cxmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/xmac.c&amp;#039;&amp;#039;; the implementation in cxmac.c is a relaxed implementation that does not require stringent timing precision and work on larger number of platforms. ContikiMac [2] is a protocol that proposes enhancement over X-MAC. The implementation can be found in file &amp;#039;&amp;#039;core/net/mac/contikimac.c&amp;#039;&amp;#039;. Finally, LPP (Low Power Probing) is a receiver-initiated low power protocol from 2008 that was also ported to ContikiOS [3]; the code can be found in &amp;#039;&amp;#039;core/net/mac/lpp.c&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Each of the last three RDC protocols can be used for energy saving. The details of each of these protocols is outside the scope of this tutorial and can be found in the references.&lt;br /&gt;
&lt;br /&gt;
== MAC protocols ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The MAC structure, called &amp;#039;&amp;#039;struct mac_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/mac.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a MAC protocol driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct mac_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the MAC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
  &lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are only two types of MAC protocols in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: nullmac (&amp;#039;&amp;#039;core/net/mac/nullmac.c&amp;#039;&amp;#039;) and csma (&amp;#039;&amp;#039;core/net/mac/csma&amp;#039;&amp;#039;). The first one is a simple pass-through protocol that simply calls the appropriate RDC functions. And the second one implements addressing, sequence number and retransmissions. CSMA protocol keep a list of packets to each of the neighbors and calculate statistics such as number of retransmissions, collisions, deferrals, etc. Even though the name, CSMA mean Carrier-Sense Medium Access, the implementation in ContikiOS does not rely on the carrier sensing, because the medium access is performed by RDC protocol.&lt;br /&gt;
&lt;br /&gt;
== How to choose and change your protocols ==&lt;br /&gt;
&lt;br /&gt;
All three stack layers (Framer, RDC and MAC) need to be defined during compilation time. They are define using pragma &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; either in the Makefile or in the &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file. The definitions of which protocols should be used are used by file &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039; that effectively binds the NETSTACK_FRAMER, NETSTACK_RDC and NETSTACK_MAC to the protocols that are supposed to be used.&lt;br /&gt;
&lt;br /&gt;
Below it is shown the piece of code responsible for setting the MAC layer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_NETWORK&lt;br /&gt;
#ifdef NETSTACK_CONF_NETWORK&lt;br /&gt;
#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK&lt;br /&gt;
#else /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#define NETSTACK_NETWORK rime_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#endif /* NETSTACK_NETWORK */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Initially the code checks if NETSTACK_NETWORK was already define (#ifndef NETSTACK_NETWORK). If it was not, the code then checks if NETSTACK_CONF_NETWORK was define. If NETSTACK_CONF_NETWORK was defined, the desired network protocol is used (#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK); otherwise a default protocol is used, which in this case is &amp;#039;&amp;#039;rime_driver&amp;#039;&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
Hence, if you want to use a particular network protocol, you can either define NETSTACK_NETWORK directly or define NETSTACK_CONF_NETWORK.&lt;br /&gt;
&lt;br /&gt;
If no definitions are made, by default ContikiOS stack layers will be:&lt;br /&gt;
&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Network layer&amp;#039;&amp;#039;&amp;#039; - rime_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;MAC layer&amp;#039;&amp;#039;&amp;#039; - nullmac_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;RDC&amp;#039;&amp;#039;&amp;#039; - nullrdc_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039; - framer_nullmac&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Radio&amp;#039;&amp;#039;&amp;#039; - nullradio_driver&lt;br /&gt;
&lt;br /&gt;
The stack layers are usually define inside file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039;, which overwrites the configuration of &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039;. There is one &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file for each platform; for instance, platform &amp;#039;&amp;#039;sky&amp;#039;&amp;#039; has &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; located inside folder &amp;#039;&amp;#039;platform/sky&amp;#039;&amp;#039;. Below we show the excerpt where stack layers are define:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_CONF_MAC&lt;br /&gt;
#define NETSTACK_CONF_MAC     csma_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_MAC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC&lt;br /&gt;
#define NETSTACK_CONF_RDC     contikimac_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE&lt;br /&gt;
#define NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE 8&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RADIO&lt;br /&gt;
#define NETSTACK_CONF_RADIO   cc2420_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RADIO */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_FRAMER&lt;br /&gt;
#define NETSTACK_CONF_FRAMER  framer_802154&lt;br /&gt;
#endif /* NETSTACK_CONF_FRAMER */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can see that, if we are using Tmote Sky hardware, by default the stack layer will be composed by &amp;#039;&amp;#039;csma_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;contikimac_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;framer_802154&amp;#039;&amp;#039; and &amp;#039;&amp;#039;cc2420_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== Changing the protocols ===&lt;br /&gt;
&lt;br /&gt;
If you want to change the protocols to be used you define them either inside the Makefile or creating a project configuration file.&lt;br /&gt;
&lt;br /&gt;
In the first case you should change Makefile, inputing the definitions in one single line, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
DEFINES = NETSTACK_CONF_RDC=cxmac_driver,NETSTACK_CONF_MAC=null_mac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the protocols that you want to use should be defined in the &amp;#039;&amp;#039;DEFINES&amp;#039;&amp;#039; line, using commas to separate them.&lt;br /&gt;
&lt;br /&gt;
The second option is to create a project configuration file. Let&amp;#039;s assume the file create was named &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039;. This file should be inserted into your Makefile, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
CFLAGS += -DPROJECT_CONF_H=\&amp;quot;project-conf.h\&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file you can define all protocols you want to use, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#define NETSTACK_CONF_MAC nullmac_driver&lt;br /&gt;
#define NETSTACK_CONF_RDC nullrc_driver&lt;br /&gt;
#define NETSTACK_CONF_FRAMER framer_nullmac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color: red&amp;quot;&amp;gt;If you choose to use &amp;#039;&amp;#039;projet-conf.h&amp;#039;&amp;#039; file, you will need to modifed the file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; inside your platform folder. If you are using sky platform, for example, you need to edit file &amp;#039;&amp;#039;platform/sky/contiki-conf.h&amp;#039;&amp;#039;. The last part of &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file should be moved to the beginning of the file, as shown below&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* -*- C -*- */&lt;br /&gt;
&lt;br /&gt;
#ifndef CONTIKI_CONF_H&lt;br /&gt;
#define CONTIKI_CONF_H&lt;br /&gt;
&lt;br /&gt;
/* include the project config */&lt;br /&gt;
/* PROJECT_CONF_H might be defined in the project Makefile */&lt;br /&gt;
#ifdef PROJECT_CONF_H&lt;br /&gt;
#include PROJECT_CONF_H&lt;br /&gt;
#endif /* PROJECT_CONF_H */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to code your own protocol ==&lt;br /&gt;
&lt;br /&gt;
We will explain here how to create a new protocol and integrate it into ContikiOS. The protocol can be a Network, MAC, RDC, Framer or Radio protocol; the steps for creating any one of these protocols are similar. We are going to show in details how to create a new MAC, called &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
First step is to create both header and c files for &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039; protocol. Both files need to be created inside &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Our files will be named &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The file &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039; is shown below. In this file we only declare an external variable that will reference our MAC protocols, called &amp;#039;&amp;#039;modifiedmac_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef __MODIFIEDMAC_H__&lt;br /&gt;
#define __MODIFIEDMAC_H__&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;net/mac/mac.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
extern const struct mac_driver modifiedmac_driver;&lt;br /&gt;
&lt;br /&gt;
#endif /* __MODIFIEDMAC_H__ */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An initial version of file &amp;#039;&amp;#039;modifiedrdc.c&amp;#039;&amp;#039; is shown below. It only contains the declaration of all functions that must be implemented in a MAC driver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;quot;net/mac/modifiedmac.h&amp;quot;&lt;br /&gt;
#include &amp;quot;net/netstack.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static unsigned short&lt;br /&gt;
channel_check_interval(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
init(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
const struct mac_driver modifiedmac_driver = {&lt;br /&gt;
  &amp;quot;modifiedmac&amp;quot;,&lt;br /&gt;
  init,&lt;br /&gt;
  send_packet,&lt;br /&gt;
  packet_input,&lt;br /&gt;
  on,&lt;br /&gt;
  off,&lt;br /&gt;
  channel_check_interval,&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The next step is to include our source code into ContikiOS make system. We just need to edit the file &amp;#039;&amp;#039;core/net/mac/Makefile.mac&amp;#039;&amp;#039; and include the file &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; in the list of Contiki source files. After editing, &amp;#039;&amp;#039;Makefile.mac&amp;#039;&amp;#039; must look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI_SOURCEFILES += cxmac.c xmac.c nullmac.c lpp.c frame802154.c sicslowmac.c nullrdc.c nullrdc-noframer.c mac.c&lt;br /&gt;
CONTIKI_SOURCEFILES += framer-nullmac.c framer-802154.c csma.c contikimac.c phase.c modifiedmac.c&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Below we show the functions that should be changed in order to make our MAC protocol functional. It is going to work exactly as a Null MAC protocol and we are counting the number of packets received and transmitted.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
uint16_t n_pkts_transmitted = 0;&lt;br /&gt;
uint16_t n_pkts_received = 0;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_transmitted++;&lt;br /&gt;
  NETSTACK_RDC.send(sent, ptr);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_received++;&lt;br /&gt;
  NETSTACK_NETWORK.input();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.on();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.off(keep_radio_on);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now you are able to use your create your own rules for addressing, framezation and all functions that should be performed by a MAC protocol.&lt;br /&gt;
&lt;br /&gt;
If you want to use your new protocol, you need to change the MAC layer that will be used by ContikiOS, following the instructions above, and setting definition NETSTACK_CONF_MAC as &amp;#039;&amp;#039;modifiedmac_driver&amp;#039;&amp;#039; (#define NETSTACK_CONF_MAC modifiedmac_driver).&lt;br /&gt;
&lt;br /&gt;
== Evaluating ContikiMAC and XMAC protocols ==&lt;br /&gt;
&lt;br /&gt;
We are going to evaluate the performance of the two most famous RDC protocols in ContikiOS, the &amp;#039;&amp;#039;ContikiMAC&amp;#039;&amp;#039; and &amp;#039;&amp;#039;X-MAC&amp;#039;&amp;#039;. We want to evaluate the Packet Reception Rate (PRR), which is the percentage of received packets in an interval; and also the average duty-cycle of sensor nodes. The PRR is related to the maximum throughput the network is able to handle; with lower PRR more packets are dropped and the total amount of data that can be transfered to the sink node is small. Also, the duty-cycle impacts the average power consumption and the lifetime of nodes, since the largest energy savings are achieved when nodes are sleeping; the lower the duty-cycle the longest will be the node&amp;#039;s lifetime.&lt;br /&gt;
&lt;br /&gt;
We considered the firmware in the folder &amp;#039;&amp;#039;examples/ipv6/rpl-udp/&amp;#039;&amp;#039;, which is a firmware based on IPv6 and RPL for UDP packet transmission. In the same folder there is one simulation file called &amp;#039;&amp;#039;rpl-udp-powertrace.csc&amp;#039;&amp;#039; that is composed of 1 sink node and 30 sensor nodes and traces the energy consumption of all nodes, and also keeps track of PRR values throughout the simulation. We considered the firmwares &amp;#039;&amp;#039;udp-server.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; and reduced the data transmission interval in &amp;#039;&amp;#039;udl-client.c&amp;#039;&amp;#039; to 1 second. The line below shows where the code of &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; was changed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef PERIOD&lt;br /&gt;
#define PERIOD 1&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By default the simulation is executed with ContikiMAC. We let the simulation run for 5 minutes before extracting the desired statistics. After simulation for ContikiMAC, we changed the RDC protocol, as described before, to X-MAC. And the same statistics were obtained. The figures below show the average duty-cycle of each node for both protocols. The figure on the left shows results of ContikiMAC and the one on the right shows results of X-MAC.&lt;br /&gt;
&lt;br /&gt;
[[File:power_contikimac.png|300px]] [[File:power_xmac.png|300px]]&lt;br /&gt;
&lt;br /&gt;
We can see that ContikiMAC reduces the average duty-cycle from 17% to 9%, which drastically impacts the energy consumption of the nodes.&lt;br /&gt;
&lt;br /&gt;
The PRR statistics were also analysed for all 30 sensor nodes. The average PRR of ContikiMAC was &amp;#039;&amp;#039;&amp;#039;50.48%&amp;#039;&amp;#039;&amp;#039; and the PRR of X-MAC was &amp;#039;&amp;#039;&amp;#039;30.38%&amp;#039;&amp;#039;&amp;#039;. We can conclude that ContikiMAC implementation is more energy and throughput efficient than X-MAC.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[http://www.cse.wustl.edu/~lu/cse521s/Papers/x-mac.pdf 1]- Buettner, Michael, Gary V. Yee, Eric Anderson, and Richard Han. &amp;quot;X-MAC: a short preamble MAC protocol for duty-cycled wireless sensor networks.&amp;quot; In Proceedings of the 4th international conference on Embedded networked sensor systems, pp. 307-320. ACM, 2006.&lt;br /&gt;
&lt;br /&gt;
[http://soda.swedish-ict.se/5128/1/contikimac-report.pdf 2] - Dunkels, Adam. &amp;quot;The contikimac radio duty cycling protocol.&amp;quot; (2011).&lt;br /&gt;
&lt;br /&gt;
[http://web.cs.jhu.edu/~cliang4/public/doc/2008_IPSN_Koala.pdf 3] - Liang, Chieh-Jan Mike, and Andreas Terzis. &amp;quot;Koala: Ultra-low power data retrieval in wireless sensor networks.&amp;quot; In Proceedings of the 7th international conference on Information processing in sensor networks, pp. 421-432. IEEE Computer Society, 2008.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
Edited by: Pedro&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1117</id>
		<title>MAC protocols in ContikiOS</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1117"/>
				<updated>2014-11-08T20:32:31Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* Evaluating ContikiMAC and XMAC protocols */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This tutorial covers the main features of &amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039; protocols available in &amp;#039;&amp;#039;ContikiOS 2.7&amp;#039;&amp;#039;. Medium Access Control protocols take care of the organization of medium access in wireless networks. They can be interpreted as rules that coordinate when each node is going to transmit/receive packets.&lt;br /&gt;
&lt;br /&gt;
There are a large number of protocols available in the literature, they can usually be classified as contention-based or reservation-based protocols. The first group is based on Carrier Sensing for detecting medium activity and are prone to collisions and lower efficiency; however, they are of easy implementation. The second group is more efficient in terms of throughput and energy, but require precise synchronization and is less adaptable to dynamic traffic. &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; only includes MAC protocols of the first category.&lt;br /&gt;
&lt;br /&gt;
There are about 5 types of MAC layers that can be used in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. They are usually implementation of well-known MAC protocols for &amp;#039;&amp;#039;Wireless Sensor Networks&amp;#039;&amp;#039;; a few of them are implementations of protocols developed exclusively for &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;, but that were based on previous work present in the literature.&lt;br /&gt;
&lt;br /&gt;
== You will learn ==&lt;br /&gt;
&lt;br /&gt;
Here you are going to be to be more familiar with all MAC protocols implemented on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. You are going to understand the organization of MAC protocols, where all files are located and where you should start when you want to modify a protocol. You will also learn the steps you need to take in order to implement your own MAC protocol. And finally, we evaluate two famous MAC protocols: &amp;#039;&amp;#039;&amp;#039;ContikiMAC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;X-MAC&amp;#039;&amp;#039;&amp;#039;, showing a few statistics regarding their performance.&lt;br /&gt;
&lt;br /&gt;
== MAC, RDC and Framer drivers ==&lt;br /&gt;
&lt;br /&gt;
The network stack implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; is a little bit different than the usual 5-layers model typically adopted in TCP/IP. In-between the &amp;#039;&amp;#039;Physical&amp;#039;&amp;#039; and the &amp;#039;&amp;#039;Network&amp;#039;&amp;#039; layers, where usually is located the MAC, we have 3 different layers: &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039;&amp;#039;. The figure below shows the organization of layers in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[File:contiki-layers.jpg|150px|center]]&lt;br /&gt;
&lt;br /&gt;
The network layers can be accessed through the global variables &amp;#039;&amp;#039;&amp;#039;NETSTACK_FRAMER&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;NETSTACK_RDC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;NETSTACK_MAC&amp;#039;&amp;#039;&amp;#039;, which are defined in compilation time. All the variables can be found in &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039;. Every network layer is defined by the pragmas &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; in the beginning of this file.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layer is not a regular layer implementation; it is actually a collection of auxiliary functions that are called for creating a frame with data to be transmited and parsing of data being received. Examples of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; types can be found in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; there are two types of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layers: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-802154.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039; layer takes care of the sleep period of nodes. This is the most important layer because it is the one responsible for deciding exactly when the packets will be transmitted and it is responsible for making sure that the node is awake when packets are to be received. RDC protocols&amp;#039; source codes are also located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Examples of RDC layers that are implemented: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;contikimac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;xmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;lpp.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc-noframer.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Finally, the MAC layer takes care of addressing and retransmission of lost packets. The source files can be located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; we have only two types of MAC layers available: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;csma.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Framers ==&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; consists of a collection of auxiliary functions that are called before transmitting a packet and after their reception. The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; structure, called &amp;#039;&amp;#039;struct framer&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/framer.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
struct framer {&lt;br /&gt;
&lt;br /&gt;
  int (* create)(void);&lt;br /&gt;
  int (* parse)(void);&lt;br /&gt;
&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function &amp;#039;&amp;#039;create&amp;#039;&amp;#039; should create a new frame to be transmitted. While function &amp;#039;&amp;#039;parse&amp;#039;&amp;#039; will be called for parsing a received frame.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-nullmac&amp;#039;&amp;#039; is defined by files &amp;#039;&amp;#039;core/net/mac/framer-nullmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-nullmac.h&amp;#039;&amp;#039;. It should be used together with &amp;#039;&amp;#039;nullmac&amp;#039;&amp;#039; (MAC layer). The auxiliary function simply fill in the 2 fields of &amp;#039;&amp;#039;nullmac_hdr&amp;#039;&amp;#039;, which are: receiver address and sender address. This is the very basic type of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; that can be used on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-802154&amp;#039;&amp;#039; is defined by &amp;#039;&amp;#039;core/net/mac/framer-802154.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-802154.h&amp;#039;&amp;#039;. It creates and parses frames compatible with standard IEEE 802.15.4 (2003). The &amp;#039;&amp;#039;create&amp;#039;&amp;#039; and &amp;#039;&amp;#039;parse&amp;#039;&amp;#039; functions manipulate all header information abstractedly through the &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure. The real work is done by functions located in the file &amp;#039;&amp;#039;core/net/mac/frame802154.c&amp;#039;&amp;#039;, which are responsible for reading all information from receive/transmit buffer and inserting/extracting them appropriately into &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure.&lt;br /&gt;
&lt;br /&gt;
== RDC protocols ==&lt;br /&gt;
&lt;br /&gt;
The RDC structure, called &amp;#039;&amp;#039;struct rdc_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/rdc.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a RDC (radio duty cycling) driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct rdc_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the RDC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet list */&lt;br /&gt;
  void (* send_list)(mac_callback_t sent_callback, void *ptr, struct rdc_buf_list *list);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function &amp;#039;&amp;#039;input&amp;#039;&amp;#039; is called when a new packet has arrived and need to be processed by RDC layer. And functions &amp;#039;&amp;#039;send&amp;#039;&amp;#039; and &amp;#039;&amp;#039;send_list&amp;#039;&amp;#039; are called for transmitting out-going packets.&lt;br /&gt;
&lt;br /&gt;
There are 2 very basic types of RDC layers implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: &amp;#039;&amp;#039;nullrdc-noframer&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc-noframer.c&amp;#039;&amp;#039;) and &amp;#039;&amp;#039;nullrdc&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc.c&amp;#039;&amp;#039;). The first one does not use Framer functions, and transmits/receives data directly to Radio layer (Physical). The developer can take total control over the format of the packets being transmitted. &lt;br /&gt;
&lt;br /&gt;
The second RDC layer (nullrdc) uses the Framer functions for header creation/parsing. It does not save energy and works as a pass-through layer that only transmits a packet and returns the results of such transmission (success or collision).&lt;br /&gt;
&lt;br /&gt;
A third simple RDC layer is &amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/sicslowmac.c&amp;#039;&amp;#039;). This is a RDC layer with no energy savings and that uses IEEE 8021.5.4 frames.&lt;br /&gt;
&lt;br /&gt;
The three last protocols available in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; are implementations of popular asynchronous MAC protocols that aim at energy saving; namely &amp;#039;&amp;#039;&amp;#039;X-MA&amp;#039;&amp;#039;&amp;#039;C, &amp;#039;&amp;#039;&amp;#039;ContikiMac&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;LPP&amp;#039;&amp;#039;&amp;#039;. X-MAC is a short-preamble protocol from 2006 that was ported to ContikiOS [1]. X-MAC has two different implementation in ContikiOS, which can be found in files &amp;#039;&amp;#039;core/net/mac/cxmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/xmac.c&amp;#039;&amp;#039;; the implementation in cxmac.c is a relaxed implementation that does not require stringent timing precision and work on larger number of platforms. ContikiMac [2] is a protocol that proposes enhancement over X-MAC. The implementation can be found in file &amp;#039;&amp;#039;core/net/mac/contikimac.c&amp;#039;&amp;#039;. Finally, LPP (Low Power Probing) is a receiver-initiated low power protocol from 2008 that was also ported to ContikiOS [3]; the code can be found in &amp;#039;&amp;#039;core/net/mac/lpp.c&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Each of the last three RDC protocols can be used for energy saving. The details of each of these protocols is outside the scope of this tutorial and can be found in the references.&lt;br /&gt;
&lt;br /&gt;
== MAC protocols ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The MAC structure, called &amp;#039;&amp;#039;struct mac_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/mac.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a MAC protocol driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct mac_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the MAC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
  &lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are only two types of MAC protocols in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: nullmac (&amp;#039;&amp;#039;core/net/mac/nullmac.c&amp;#039;&amp;#039;) and csma (&amp;#039;&amp;#039;core/net/mac/csma&amp;#039;&amp;#039;). The first one is a simple pass-through protocol that simply calls the appropriate RDC functions. And the second one implements addressing, sequence number and retransmissions. CSMA protocol keep a list of packets to each of the neighbors and calculate statistics such as number of retransmissions, collisions, deferrals, etc. Even though the name, CSMA mean Carrier-Sense Medium Access, the implementation in ContikiOS does not rely on the carrier sensing, because the medium access is performed by RDC protocol.&lt;br /&gt;
&lt;br /&gt;
== How to choose and change your protocols ==&lt;br /&gt;
&lt;br /&gt;
All three stack layers (Framer, RDC and MAC) need to be defined during compilation time. They are define using pragma &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; either in the Makefile or in the &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file. The definitions of which protocols should be used are used by file &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039; that effectively binds the NETSTACK_FRAMER, NETSTACK_RDC and NETSTACK_MAC to the protocols that are supposed to be used.&lt;br /&gt;
&lt;br /&gt;
Below it is shown the piece of code responsible for setting the MAC layer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_NETWORK&lt;br /&gt;
#ifdef NETSTACK_CONF_NETWORK&lt;br /&gt;
#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK&lt;br /&gt;
#else /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#define NETSTACK_NETWORK rime_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#endif /* NETSTACK_NETWORK */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Initially the code checks if NETSTACK_NETWORK was already define (#ifndef NETSTACK_NETWORK). If it was not, the code then checks if NETSTACK_CONF_NETWORK was define. If NETSTACK_CONF_NETWORK was defined, the desired network protocol is used (#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK); otherwise a default protocol is used, which in this case is &amp;#039;&amp;#039;rime_driver&amp;#039;&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
Hence, if you want to use a particular network protocol, you can either define NETSTACK_NETWORK directly or define NETSTACK_CONF_NETWORK.&lt;br /&gt;
&lt;br /&gt;
If no definitions are made, by default ContikiOS stack layers will be:&lt;br /&gt;
&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Network layer&amp;#039;&amp;#039;&amp;#039; - rime_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;MAC layer&amp;#039;&amp;#039;&amp;#039; - nullmac_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;RDC&amp;#039;&amp;#039;&amp;#039; - nullrdc_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039; - framer_nullmac&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Radio&amp;#039;&amp;#039;&amp;#039; - nullradio_driver&lt;br /&gt;
&lt;br /&gt;
The stack layers are usually define inside file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039;, which overwrites the configuration of &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039;. There is one &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file for each platform; for instance, platform &amp;#039;&amp;#039;sky&amp;#039;&amp;#039; has &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; located inside folder &amp;#039;&amp;#039;platform/sky&amp;#039;&amp;#039;. Below we show the excerpt where stack layers are define:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_CONF_MAC&lt;br /&gt;
#define NETSTACK_CONF_MAC     csma_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_MAC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC&lt;br /&gt;
#define NETSTACK_CONF_RDC     contikimac_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE&lt;br /&gt;
#define NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE 8&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RADIO&lt;br /&gt;
#define NETSTACK_CONF_RADIO   cc2420_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RADIO */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_FRAMER&lt;br /&gt;
#define NETSTACK_CONF_FRAMER  framer_802154&lt;br /&gt;
#endif /* NETSTACK_CONF_FRAMER */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can see that, if we are using Tmote Sky hardware, by default the stack layer will be composed by &amp;#039;&amp;#039;csma_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;contikimac_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;framer_802154&amp;#039;&amp;#039; and &amp;#039;&amp;#039;cc2420_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== Changing the protocols ===&lt;br /&gt;
&lt;br /&gt;
If you want to change the protocols to be used you define them either inside the Makefile or creating a project configuration file.&lt;br /&gt;
&lt;br /&gt;
In the first case you should change Makefile, inputing the definitions in one single line, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
DEFINES = NETSTACK_CONF_RDC=cxmac_driver,NETSTACK_CONF_MAC=null_mac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the protocols that you want to use should be defined in the &amp;#039;&amp;#039;DEFINES&amp;#039;&amp;#039; line, using commas to separate them.&lt;br /&gt;
&lt;br /&gt;
The second option is to create a project configuration file. Let&amp;#039;s assume the file create was named &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039;. This file should be inserted into your Makefile, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
CFLAGS += -DPROJECT_CONF_H=\&amp;quot;project-conf.h\&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file you can define all protocols you want to use, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#define NETSTACK_CONF_MAC nullmac_driver&lt;br /&gt;
#define NETSTACK_CONF_RDC nullrc_driver&lt;br /&gt;
#define NETSTACK_CONF_FRAMER framer_nullmac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color: red&amp;quot;&amp;gt;If you choose to use &amp;#039;&amp;#039;projet-conf.h&amp;#039;&amp;#039; file, you will need to modifed the file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; inside your platform folder. If you are using sky platform, for example, you need to edit file &amp;#039;&amp;#039;platform/sky/contiki-conf.h&amp;#039;&amp;#039;. The last part of &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file should be moved to the beginning of the file, as shown below&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* -*- C -*- */&lt;br /&gt;
&lt;br /&gt;
#ifndef CONTIKI_CONF_H&lt;br /&gt;
#define CONTIKI_CONF_H&lt;br /&gt;
&lt;br /&gt;
/* include the project config */&lt;br /&gt;
/* PROJECT_CONF_H might be defined in the project Makefile */&lt;br /&gt;
#ifdef PROJECT_CONF_H&lt;br /&gt;
#include PROJECT_CONF_H&lt;br /&gt;
#endif /* PROJECT_CONF_H */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to code your own protocol ==&lt;br /&gt;
&lt;br /&gt;
We will explain here how to create a new protocol and integrate it into ContikiOS. The protocol can be a Network, MAC, RDC, Framer or Radio protocol; the steps for creating any one of these protocols are similar. We are going to show in details how to create a new MAC, called &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
First step is to create both header and c files for &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039; protocol. Both files need to be created inside &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Our files will be named &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The file &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039; is shown below. In this file we only declare an external variable that will reference our MAC protocols, called &amp;#039;&amp;#039;modifiedmac_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef __MODIFIEDMAC_H__&lt;br /&gt;
#define __MODIFIEDMAC_H__&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;net/mac/mac.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
extern const struct mac_driver modifiedmac_driver;&lt;br /&gt;
&lt;br /&gt;
#endif /* __MODIFIEDMAC_H__ */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An initial version of file &amp;#039;&amp;#039;modifiedrdc.c&amp;#039;&amp;#039; is shown below. It only contains the declaration of all functions that must be implemented in a MAC driver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;quot;net/mac/modifiedmac.h&amp;quot;&lt;br /&gt;
#include &amp;quot;net/netstack.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static unsigned short&lt;br /&gt;
channel_check_interval(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
init(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
const struct mac_driver modifiedmac_driver = {&lt;br /&gt;
  &amp;quot;modifiedmac&amp;quot;,&lt;br /&gt;
  init,&lt;br /&gt;
  send_packet,&lt;br /&gt;
  packet_input,&lt;br /&gt;
  on,&lt;br /&gt;
  off,&lt;br /&gt;
  channel_check_interval,&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The next step is to include our source code into ContikiOS make system. We just need to edit the file &amp;#039;&amp;#039;core/net/mac/Makefile.mac&amp;#039;&amp;#039; and include the file &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; in the list of Contiki source files. After editing, &amp;#039;&amp;#039;Makefile.mac&amp;#039;&amp;#039; must look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI_SOURCEFILES += cxmac.c xmac.c nullmac.c lpp.c frame802154.c sicslowmac.c nullrdc.c nullrdc-noframer.c mac.c&lt;br /&gt;
CONTIKI_SOURCEFILES += framer-nullmac.c framer-802154.c csma.c contikimac.c phase.c modifiedmac.c&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Below we show the functions that should be changed in order to make our MAC protocol functional. It is going to work exactly as a Null MAC protocol and we are counting the number of packets received and transmitted.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
uint16_t n_pkts_transmitted = 0;&lt;br /&gt;
uint16_t n_pkts_received = 0;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_transmitted++;&lt;br /&gt;
  NETSTACK_RDC.send(sent, ptr);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_received++;&lt;br /&gt;
  NETSTACK_NETWORK.input();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.on();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.off(keep_radio_on);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now you are able to use your create your own rules for addressing, framezation and all functions that should be performed by a MAC protocol.&lt;br /&gt;
&lt;br /&gt;
If you want to use your new protocol, you need to change the MAC layer that will be used by ContikiOS, following the instructions above, and setting definition NETSTACK_CONF_MAC as &amp;#039;&amp;#039;modifiedmac_driver&amp;#039;&amp;#039; (#define NETSTACK_CONF_MAC modifiedmac_driver).&lt;br /&gt;
&lt;br /&gt;
== Evaluating ContikiMAC and XMAC protocols ==&lt;br /&gt;
&lt;br /&gt;
We are going to evaluate the performance of the two most famous RDC protocols in ContikiOS, the &amp;#039;&amp;#039;ContikiMAC&amp;#039;&amp;#039; and &amp;#039;&amp;#039;X-MAC&amp;#039;&amp;#039;. We want to evaluate the Packet Reception Rate (PRR), which is the percentage of received packets in an interval; and also the average duty-cycle of sensor nodes. The PRR is related to the maximum throughput the network is able to handle; with lower PRR more packets are dropped and the total amount of data that can be transfered to the sink node is small. Also, the duty-cycle impacts the average power consumption and the lifetime of nodes, since the largest energy savings are achieved when nodes are sleeping; the lower the duty-cycle the longest will be the node&amp;#039;s lifetime.&lt;br /&gt;
&lt;br /&gt;
We considered the firmware in the folder &amp;#039;&amp;#039;examples/ipv6/rpl-udp/&amp;#039;&amp;#039;, which is a firmware based on IPv6 and RPL for UDP packet transmission. In the same folder there is one simulation file called &amp;#039;&amp;#039;rpl-udp-powertrace.csc&amp;#039;&amp;#039; that is composed of 1 sink node and 30 sensor nodes and traces the energy consumption of all nodes, and also keeps track of PRR values throughout the simulation. We considered the firmwares &amp;#039;&amp;#039;udp-server.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; and reduced the data transmission interval in &amp;#039;&amp;#039;udl-client.c&amp;#039;&amp;#039; to 1 second. The line below shows where the code of &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; was changed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef PERIOD&lt;br /&gt;
#define PERIOD 1&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By default the simulation is executed with ContikiMAC. We let the simulation run for 5 minutes before extracting the desired statistics. After simulation for ContikiMAC, we changed the RDC protocol, as described before, to X-MAC. And the same statistics were obtained. The figures below show the average duty-cycle of each node for both protocols. The figure on the left shows results of ContikiMAC and the one on the right shows results of X-MAC.&lt;br /&gt;
&lt;br /&gt;
[[File:power_contikimac.png|300px]] [[File:power_xmac.png|300px]]&lt;br /&gt;
&lt;br /&gt;
We can see that ContikiMAC reduces the average duty-cycle from 17% to 9%, which drastically impacts the energy consumption of the nodes.&lt;br /&gt;
&lt;br /&gt;
The PRR statistics were also analysed for all 30 sensor nodes. On average the PRR of ContikiMAC was &amp;#039;&amp;#039;&amp;#039;50.48%&amp;#039;&amp;#039;&amp;#039; and the PRR of X-MAC was &amp;#039;&amp;#039;&amp;#039;30.38%&amp;#039;&amp;#039;&amp;#039;. We can conclude that ContikiMAC implementation is more energy and throughput efficient than X-MAC.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[http://www.cse.wustl.edu/~lu/cse521s/Papers/x-mac.pdf 1]- Buettner, Michael, Gary V. Yee, Eric Anderson, and Richard Han. &amp;quot;X-MAC: a short preamble MAC protocol for duty-cycled wireless sensor networks.&amp;quot; In Proceedings of the 4th international conference on Embedded networked sensor systems, pp. 307-320. ACM, 2006.&lt;br /&gt;
&lt;br /&gt;
[http://soda.swedish-ict.se/5128/1/contikimac-report.pdf 2] - Dunkels, Adam. &amp;quot;The contikimac radio duty cycling protocol.&amp;quot; (2011).&lt;br /&gt;
&lt;br /&gt;
[http://web.cs.jhu.edu/~cliang4/public/doc/2008_IPSN_Koala.pdf 3] - Liang, Chieh-Jan Mike, and Andreas Terzis. &amp;quot;Koala: Ultra-low power data retrieval in wireless sensor networks.&amp;quot; In Proceedings of the 7th international conference on Information processing in sensor networks, pp. 421-432. IEEE Computer Society, 2008.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
Edited by: Pedro&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1114</id>
		<title>MAC protocols in ContikiOS</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1114"/>
				<updated>2014-11-08T20:28:29Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* How to code your own protocol */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This tutorial covers the main features of &amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039; protocols available in &amp;#039;&amp;#039;ContikiOS 2.7&amp;#039;&amp;#039;. Medium Access Control protocols take care of the organization of medium access in wireless networks. They can be interpreted as rules that coordinate when each node is going to transmit/receive packets.&lt;br /&gt;
&lt;br /&gt;
There are a large number of protocols available in the literature, they can usually be classified as contention-based or reservation-based protocols. The first group is based on Carrier Sensing for detecting medium activity and are prone to collisions and lower efficiency; however, they are of easy implementation. The second group is more efficient in terms of throughput and energy, but require precise synchronization and is less adaptable to dynamic traffic. &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; only includes MAC protocols of the first category.&lt;br /&gt;
&lt;br /&gt;
There are about 5 types of MAC layers that can be used in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. They are usually implementation of well-known MAC protocols for &amp;#039;&amp;#039;Wireless Sensor Networks&amp;#039;&amp;#039;; a few of them are implementations of protocols developed exclusively for &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;, but that were based on previous work present in the literature.&lt;br /&gt;
&lt;br /&gt;
== You will learn ==&lt;br /&gt;
&lt;br /&gt;
Here you are going to be to be more familiar with all MAC protocols implemented on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. You are going to understand the organization of MAC protocols, where all files are located and where you should start when you want to modify a protocol. You will also learn the steps you need to take in order to implement your own MAC protocol. And finally, we evaluate two famous MAC protocols: &amp;#039;&amp;#039;&amp;#039;ContikiMAC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;X-MAC&amp;#039;&amp;#039;&amp;#039;, showing a few statistics regarding their performance.&lt;br /&gt;
&lt;br /&gt;
== MAC, RDC and Framer drivers ==&lt;br /&gt;
&lt;br /&gt;
The network stack implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; is a little bit different than the usual 5-layers model typically adopted in TCP/IP. In-between the &amp;#039;&amp;#039;Physical&amp;#039;&amp;#039; and the &amp;#039;&amp;#039;Network&amp;#039;&amp;#039; layers, where usually is located the MAC, we have 3 different layers: &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039;&amp;#039;. The figure below shows the organization of layers in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[File:contiki-layers.jpg|150px|center]]&lt;br /&gt;
&lt;br /&gt;
The network layers can be accessed through the global variables &amp;#039;&amp;#039;&amp;#039;NETSTACK_FRAMER&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;NETSTACK_RDC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;NETSTACK_MAC&amp;#039;&amp;#039;&amp;#039;, which are defined in compilation time. All the variables can be found in &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039;. Every network layer is defined by the pragmas &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; in the beginning of this file.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layer is not a regular layer implementation; it is actually a collection of auxiliary functions that are called for creating a frame with data to be transmited and parsing of data being received. Examples of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; types can be found in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; there are two types of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layers: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-802154.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039; layer takes care of the sleep period of nodes. This is the most important layer because it is the one responsible for deciding exactly when the packets will be transmitted and it is responsible for making sure that the node is awake when packets are to be received. RDC protocols&amp;#039; source codes are also located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Examples of RDC layers that are implemented: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;contikimac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;xmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;lpp.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc-noframer.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Finally, the MAC layer takes care of addressing and retransmission of lost packets. The source files can be located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; we have only two types of MAC layers available: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;csma.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Framers ==&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; consists of a collection of auxiliary functions that are called before transmitting a packet and after their reception. The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; structure, called &amp;#039;&amp;#039;struct framer&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/framer.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
struct framer {&lt;br /&gt;
&lt;br /&gt;
  int (* create)(void);&lt;br /&gt;
  int (* parse)(void);&lt;br /&gt;
&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function &amp;#039;&amp;#039;create&amp;#039;&amp;#039; should create a new frame to be transmitted. While function &amp;#039;&amp;#039;parse&amp;#039;&amp;#039; will be called for parsing a received frame.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-nullmac&amp;#039;&amp;#039; is defined by files &amp;#039;&amp;#039;core/net/mac/framer-nullmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-nullmac.h&amp;#039;&amp;#039;. It should be used together with &amp;#039;&amp;#039;nullmac&amp;#039;&amp;#039; (MAC layer). The auxiliary function simply fill in the 2 fields of &amp;#039;&amp;#039;nullmac_hdr&amp;#039;&amp;#039;, which are: receiver address and sender address. This is the very basic type of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; that can be used on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-802154&amp;#039;&amp;#039; is defined by &amp;#039;&amp;#039;core/net/mac/framer-802154.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-802154.h&amp;#039;&amp;#039;. It creates and parses frames compatible with standard IEEE 802.15.4 (2003). The &amp;#039;&amp;#039;create&amp;#039;&amp;#039; and &amp;#039;&amp;#039;parse&amp;#039;&amp;#039; functions manipulate all header information abstractedly through the &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure. The real work is done by functions located in the file &amp;#039;&amp;#039;core/net/mac/frame802154.c&amp;#039;&amp;#039;, which are responsible for reading all information from receive/transmit buffer and inserting/extracting them appropriately into &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure.&lt;br /&gt;
&lt;br /&gt;
== RDC protocols ==&lt;br /&gt;
&lt;br /&gt;
The RDC structure, called &amp;#039;&amp;#039;struct rdc_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/rdc.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a RDC (radio duty cycling) driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct rdc_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the RDC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet list */&lt;br /&gt;
  void (* send_list)(mac_callback_t sent_callback, void *ptr, struct rdc_buf_list *list);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function &amp;#039;&amp;#039;input&amp;#039;&amp;#039; is called when a new packet has arrived and need to be processed by RDC layer. And functions &amp;#039;&amp;#039;send&amp;#039;&amp;#039; and &amp;#039;&amp;#039;send_list&amp;#039;&amp;#039; are called for transmitting out-going packets.&lt;br /&gt;
&lt;br /&gt;
There are 2 very basic types of RDC layers implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: &amp;#039;&amp;#039;nullrdc-noframer&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc-noframer.c&amp;#039;&amp;#039;) and &amp;#039;&amp;#039;nullrdc&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc.c&amp;#039;&amp;#039;). The first one does not use Framer functions, and transmits/receives data directly to Radio layer (Physical). The developer can take total control over the format of the packets being transmitted. &lt;br /&gt;
&lt;br /&gt;
The second RDC layer (nullrdc) uses the Framer functions for header creation/parsing. It does not save energy and works as a pass-through layer that only transmits a packet and returns the results of such transmission (success or collision).&lt;br /&gt;
&lt;br /&gt;
A third simple RDC layer is &amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/sicslowmac.c&amp;#039;&amp;#039;). This is a RDC layer with no energy savings and that uses IEEE 8021.5.4 frames.&lt;br /&gt;
&lt;br /&gt;
The three last protocols available in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; are implementations of popular asynchronous MAC protocols that aim at energy saving; namely &amp;#039;&amp;#039;&amp;#039;X-MA&amp;#039;&amp;#039;&amp;#039;C, &amp;#039;&amp;#039;&amp;#039;ContikiMac&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;LPP&amp;#039;&amp;#039;&amp;#039;. X-MAC is a short-preamble protocol from 2006 that was ported to ContikiOS [1]. X-MAC has two different implementation in ContikiOS, which can be found in files &amp;#039;&amp;#039;core/net/mac/cxmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/xmac.c&amp;#039;&amp;#039;; the implementation in cxmac.c is a relaxed implementation that does not require stringent timing precision and work on larger number of platforms. ContikiMac [2] is a protocol that proposes enhancement over X-MAC. The implementation can be found in file &amp;#039;&amp;#039;core/net/mac/contikimac.c&amp;#039;&amp;#039;. Finally, LPP (Low Power Probing) is a receiver-initiated low power protocol from 2008 that was also ported to ContikiOS [3]; the code can be found in &amp;#039;&amp;#039;core/net/mac/lpp.c&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Each of the last three RDC protocols can be used for energy saving. The details of each of these protocols is outside the scope of this tutorial and can be found in the references.&lt;br /&gt;
&lt;br /&gt;
== MAC protocols ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The MAC structure, called &amp;#039;&amp;#039;struct mac_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/mac.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a MAC protocol driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct mac_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the MAC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
  &lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are only two types of MAC protocols in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: nullmac (&amp;#039;&amp;#039;core/net/mac/nullmac.c&amp;#039;&amp;#039;) and csma (&amp;#039;&amp;#039;core/net/mac/csma&amp;#039;&amp;#039;). The first one is a simple pass-through protocol that simply calls the appropriate RDC functions. And the second one implements addressing, sequence number and retransmissions. CSMA protocol keep a list of packets to each of the neighbors and calculate statistics such as number of retransmissions, collisions, deferrals, etc. Even though the name, CSMA mean Carrier-Sense Medium Access, the implementation in ContikiOS does not rely on the carrier sensing, because the medium access is performed by RDC protocol.&lt;br /&gt;
&lt;br /&gt;
== How to choose and change your protocols ==&lt;br /&gt;
&lt;br /&gt;
All three stack layers (Framer, RDC and MAC) need to be defined during compilation time. They are define using pragma &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; either in the Makefile or in the &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file. The definitions of which protocols should be used are used by file &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039; that effectively binds the NETSTACK_FRAMER, NETSTACK_RDC and NETSTACK_MAC to the protocols that are supposed to be used.&lt;br /&gt;
&lt;br /&gt;
Below it is shown the piece of code responsible for setting the MAC layer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_NETWORK&lt;br /&gt;
#ifdef NETSTACK_CONF_NETWORK&lt;br /&gt;
#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK&lt;br /&gt;
#else /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#define NETSTACK_NETWORK rime_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#endif /* NETSTACK_NETWORK */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Initially the code checks if NETSTACK_NETWORK was already define (#ifndef NETSTACK_NETWORK). If it was not, the code then checks if NETSTACK_CONF_NETWORK was define. If NETSTACK_CONF_NETWORK was defined, the desired network protocol is used (#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK); otherwise a default protocol is used, which in this case is &amp;#039;&amp;#039;rime_driver&amp;#039;&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
Hence, if you want to use a particular network protocol, you can either define NETSTACK_NETWORK directly or define NETSTACK_CONF_NETWORK.&lt;br /&gt;
&lt;br /&gt;
If no definitions are made, by default ContikiOS stack layers will be:&lt;br /&gt;
&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Network layer&amp;#039;&amp;#039;&amp;#039; - rime_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;MAC layer&amp;#039;&amp;#039;&amp;#039; - nullmac_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;RDC&amp;#039;&amp;#039;&amp;#039; - nullrdc_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039; - framer_nullmac&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Radio&amp;#039;&amp;#039;&amp;#039; - nullradio_driver&lt;br /&gt;
&lt;br /&gt;
The stack layers are usually define inside file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039;, which overwrites the configuration of &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039;. There is one &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file for each platform; for instance, platform &amp;#039;&amp;#039;sky&amp;#039;&amp;#039; has &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; located inside folder &amp;#039;&amp;#039;platform/sky&amp;#039;&amp;#039;. Below we show the excerpt where stack layers are define:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_CONF_MAC&lt;br /&gt;
#define NETSTACK_CONF_MAC     csma_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_MAC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC&lt;br /&gt;
#define NETSTACK_CONF_RDC     contikimac_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE&lt;br /&gt;
#define NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE 8&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RADIO&lt;br /&gt;
#define NETSTACK_CONF_RADIO   cc2420_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RADIO */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_FRAMER&lt;br /&gt;
#define NETSTACK_CONF_FRAMER  framer_802154&lt;br /&gt;
#endif /* NETSTACK_CONF_FRAMER */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can see that, if we are using Tmote Sky hardware, by default the stack layer will be composed by &amp;#039;&amp;#039;csma_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;contikimac_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;framer_802154&amp;#039;&amp;#039; and &amp;#039;&amp;#039;cc2420_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== Changing the protocols ===&lt;br /&gt;
&lt;br /&gt;
If you want to change the protocols to be used you define them either inside the Makefile or creating a project configuration file.&lt;br /&gt;
&lt;br /&gt;
In the first case you should change Makefile, inputing the definitions in one single line, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
DEFINES = NETSTACK_CONF_RDC=cxmac_driver,NETSTACK_CONF_MAC=null_mac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the protocols that you want to use should be defined in the &amp;#039;&amp;#039;DEFINES&amp;#039;&amp;#039; line, using commas to separate them.&lt;br /&gt;
&lt;br /&gt;
The second option is to create a project configuration file. Let&amp;#039;s assume the file create was named &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039;. This file should be inserted into your Makefile, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
CFLAGS += -DPROJECT_CONF_H=\&amp;quot;project-conf.h\&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file you can define all protocols you want to use, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#define NETSTACK_CONF_MAC nullmac_driver&lt;br /&gt;
#define NETSTACK_CONF_RDC nullrc_driver&lt;br /&gt;
#define NETSTACK_CONF_FRAMER framer_nullmac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color: red&amp;quot;&amp;gt;If you choose to use &amp;#039;&amp;#039;projet-conf.h&amp;#039;&amp;#039; file, you will need to modifed the file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; inside your platform folder. If you are using sky platform, for example, you need to edit file &amp;#039;&amp;#039;platform/sky/contiki-conf.h&amp;#039;&amp;#039;. The last part of &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file should be moved to the beginning of the file, as shown below&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* -*- C -*- */&lt;br /&gt;
&lt;br /&gt;
#ifndef CONTIKI_CONF_H&lt;br /&gt;
#define CONTIKI_CONF_H&lt;br /&gt;
&lt;br /&gt;
/* include the project config */&lt;br /&gt;
/* PROJECT_CONF_H might be defined in the project Makefile */&lt;br /&gt;
#ifdef PROJECT_CONF_H&lt;br /&gt;
#include PROJECT_CONF_H&lt;br /&gt;
#endif /* PROJECT_CONF_H */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to code your own protocol ==&lt;br /&gt;
&lt;br /&gt;
We will explain here how to create a new protocol and integrate it into ContikiOS. The protocol can be a Network, MAC, RDC, Framer or Radio protocol; the steps for creating any one of these protocols are similar. We are going to show in details how to create a new MAC, called &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
First step is to create both header and c files for &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039; protocol. Both files need to be created inside &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Our files will be named &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The file &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039; is shown below. In this file we only declare an external variable that will reference our MAC protocols, called &amp;#039;&amp;#039;modifiedmac_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef __MODIFIEDMAC_H__&lt;br /&gt;
#define __MODIFIEDMAC_H__&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;net/mac/mac.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
extern const struct mac_driver modifiedmac_driver;&lt;br /&gt;
&lt;br /&gt;
#endif /* __MODIFIEDMAC_H__ */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An initial version of file &amp;#039;&amp;#039;modifiedrdc.c&amp;#039;&amp;#039; is shown below. It only contains the declaration of all functions that must be implemented in a MAC driver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;quot;net/mac/modifiedmac.h&amp;quot;&lt;br /&gt;
#include &amp;quot;net/netstack.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static unsigned short&lt;br /&gt;
channel_check_interval(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
init(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
const struct mac_driver modifiedmac_driver = {&lt;br /&gt;
  &amp;quot;modifiedmac&amp;quot;,&lt;br /&gt;
  init,&lt;br /&gt;
  send_packet,&lt;br /&gt;
  packet_input,&lt;br /&gt;
  on,&lt;br /&gt;
  off,&lt;br /&gt;
  channel_check_interval,&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The next step is to include our source code into ContikiOS make system. We just need to edit the file &amp;#039;&amp;#039;core/net/mac/Makefile.mac&amp;#039;&amp;#039; and include the file &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; in the list of Contiki source files. After editing, &amp;#039;&amp;#039;Makefile.mac&amp;#039;&amp;#039; must look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI_SOURCEFILES += cxmac.c xmac.c nullmac.c lpp.c frame802154.c sicslowmac.c nullrdc.c nullrdc-noframer.c mac.c&lt;br /&gt;
CONTIKI_SOURCEFILES += framer-nullmac.c framer-802154.c csma.c contikimac.c phase.c modifiedmac.c&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Below we show the functions that should be changed in order to make our MAC protocol functional. It is going to work exactly as a Null MAC protocol and we are counting the number of packets received and transmitted.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
uint16_t n_pkts_transmitted = 0;&lt;br /&gt;
uint16_t n_pkts_received = 0;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_transmitted++;&lt;br /&gt;
  NETSTACK_RDC.send(sent, ptr);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_received++;&lt;br /&gt;
  NETSTACK_NETWORK.input();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.on();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.off(keep_radio_on);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now you are able to use your create your own rules for addressing, framezation and all functions that should be performed by a MAC protocol.&lt;br /&gt;
&lt;br /&gt;
If you want to use your new protocol, you need to change the MAC layer that will be used by ContikiOS, following the instructions above, and setting definition NETSTACK_CONF_MAC as &amp;#039;&amp;#039;modifiedmac_driver&amp;#039;&amp;#039; (#define NETSTACK_CONF_MAC modifiedmac_driver).&lt;br /&gt;
&lt;br /&gt;
== Evaluating ContikiMAC and XMAC protocols ==&lt;br /&gt;
&lt;br /&gt;
We are going to evaluate the performance of the two most famous RDC protocols in ContikiOS, the ContikiMAC and X-MAC. We want to evaluate the Packet Reception Rate (PRR), which is the percentage of received packets in an interval; and also the average duty-cycle of sensor nodes. The PRR is related to the maximum throughput the network is able to handle; with lower PRR more packets are dropped and the total amount of data that can be transfered to the sink node is small. Also, the duty-cycle impacts the average power consumption and the lifetime of nodes, since the biggest energy savings are achieved when nodes are sleeping; the lower the duty-cycle the longest will be the node&amp;#039;s lifetime.&lt;br /&gt;
&lt;br /&gt;
We considered the firmware in the folder &amp;#039;&amp;#039;examples/ipv6/rpl-udp/&amp;#039;&amp;#039;, which is a firmware based on IPv6 and RPL for UDP packet transmission. In the same folder there is one simulation file called &amp;#039;&amp;#039;rpl-udp-powertrace.csc&amp;#039;&amp;#039; that is composed of 1 sink node and 30 sensor nodes and traces the energy consumption of all nodes, and also keeps track of PRR values throughout the simulation time. We considered the firmwares &amp;#039;&amp;#039;udp-server.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; and reduced the data transmission interval in &amp;#039;&amp;#039;udl-client.c&amp;#039;&amp;#039; to 1 second. The line below shows where the code of &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; was changed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef PERIOD&lt;br /&gt;
#define PERIOD 1&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By default the simulation is executed with ContikiMAC. We let the simulation run for 5 minutes before extracting the desired statistics. After simulation for ContikiMAC, we changed the RDC protocols, as described before, to X-MAC. And the same statistics were obtained. The figures below show the average duty-cycle of each node for both protocols. The figure on the left shows results of ContikiMAC and the one on the right shows results of X-MAC.&lt;br /&gt;
&lt;br /&gt;
[[File:power_contikimac.png|300px]] [[File:power_xmac.png|300px]]&lt;br /&gt;
&lt;br /&gt;
The PRR statistics were also analysed for all 30 sensor nodes. On average the PRR of ContikiMAC was &amp;#039;&amp;#039;&amp;#039;50.48%&amp;#039;&amp;#039;&amp;#039; and the PRR of X-MAC was &amp;#039;&amp;#039;&amp;#039;30.38%&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[http://www.cse.wustl.edu/~lu/cse521s/Papers/x-mac.pdf 1]- Buettner, Michael, Gary V. Yee, Eric Anderson, and Richard Han. &amp;quot;X-MAC: a short preamble MAC protocol for duty-cycled wireless sensor networks.&amp;quot; In Proceedings of the 4th international conference on Embedded networked sensor systems, pp. 307-320. ACM, 2006.&lt;br /&gt;
&lt;br /&gt;
[http://soda.swedish-ict.se/5128/1/contikimac-report.pdf 2] - Dunkels, Adam. &amp;quot;The contikimac radio duty cycling protocol.&amp;quot; (2011).&lt;br /&gt;
&lt;br /&gt;
[http://web.cs.jhu.edu/~cliang4/public/doc/2008_IPSN_Koala.pdf 3] - Liang, Chieh-Jan Mike, and Andreas Terzis. &amp;quot;Koala: Ultra-low power data retrieval in wireless sensor networks.&amp;quot; In Proceedings of the 7th international conference on Information processing in sensor networks, pp. 421-432. IEEE Computer Society, 2008.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
Edited by: Pedro&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1113</id>
		<title>MAC protocols in ContikiOS</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1113"/>
				<updated>2014-11-08T20:26:32Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* Changing the protocols */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This tutorial covers the main features of &amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039; protocols available in &amp;#039;&amp;#039;ContikiOS 2.7&amp;#039;&amp;#039;. Medium Access Control protocols take care of the organization of medium access in wireless networks. They can be interpreted as rules that coordinate when each node is going to transmit/receive packets.&lt;br /&gt;
&lt;br /&gt;
There are a large number of protocols available in the literature, they can usually be classified as contention-based or reservation-based protocols. The first group is based on Carrier Sensing for detecting medium activity and are prone to collisions and lower efficiency; however, they are of easy implementation. The second group is more efficient in terms of throughput and energy, but require precise synchronization and is less adaptable to dynamic traffic. &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; only includes MAC protocols of the first category.&lt;br /&gt;
&lt;br /&gt;
There are about 5 types of MAC layers that can be used in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. They are usually implementation of well-known MAC protocols for &amp;#039;&amp;#039;Wireless Sensor Networks&amp;#039;&amp;#039;; a few of them are implementations of protocols developed exclusively for &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;, but that were based on previous work present in the literature.&lt;br /&gt;
&lt;br /&gt;
== You will learn ==&lt;br /&gt;
&lt;br /&gt;
Here you are going to be to be more familiar with all MAC protocols implemented on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. You are going to understand the organization of MAC protocols, where all files are located and where you should start when you want to modify a protocol. You will also learn the steps you need to take in order to implement your own MAC protocol. And finally, we evaluate two famous MAC protocols: &amp;#039;&amp;#039;&amp;#039;ContikiMAC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;X-MAC&amp;#039;&amp;#039;&amp;#039;, showing a few statistics regarding their performance.&lt;br /&gt;
&lt;br /&gt;
== MAC, RDC and Framer drivers ==&lt;br /&gt;
&lt;br /&gt;
The network stack implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; is a little bit different than the usual 5-layers model typically adopted in TCP/IP. In-between the &amp;#039;&amp;#039;Physical&amp;#039;&amp;#039; and the &amp;#039;&amp;#039;Network&amp;#039;&amp;#039; layers, where usually is located the MAC, we have 3 different layers: &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039;&amp;#039;. The figure below shows the organization of layers in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[File:contiki-layers.jpg|150px|center]]&lt;br /&gt;
&lt;br /&gt;
The network layers can be accessed through the global variables &amp;#039;&amp;#039;&amp;#039;NETSTACK_FRAMER&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;NETSTACK_RDC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;NETSTACK_MAC&amp;#039;&amp;#039;&amp;#039;, which are defined in compilation time. All the variables can be found in &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039;. Every network layer is defined by the pragmas &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; in the beginning of this file.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layer is not a regular layer implementation; it is actually a collection of auxiliary functions that are called for creating a frame with data to be transmited and parsing of data being received. Examples of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; types can be found in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; there are two types of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layers: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-802154.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039; layer takes care of the sleep period of nodes. This is the most important layer because it is the one responsible for deciding exactly when the packets will be transmitted and it is responsible for making sure that the node is awake when packets are to be received. RDC protocols&amp;#039; source codes are also located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Examples of RDC layers that are implemented: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;contikimac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;xmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;lpp.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc-noframer.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Finally, the MAC layer takes care of addressing and retransmission of lost packets. The source files can be located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; we have only two types of MAC layers available: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;csma.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Framers ==&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; consists of a collection of auxiliary functions that are called before transmitting a packet and after their reception. The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; structure, called &amp;#039;&amp;#039;struct framer&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/framer.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
struct framer {&lt;br /&gt;
&lt;br /&gt;
  int (* create)(void);&lt;br /&gt;
  int (* parse)(void);&lt;br /&gt;
&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function &amp;#039;&amp;#039;create&amp;#039;&amp;#039; should create a new frame to be transmitted. While function &amp;#039;&amp;#039;parse&amp;#039;&amp;#039; will be called for parsing a received frame.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-nullmac&amp;#039;&amp;#039; is defined by files &amp;#039;&amp;#039;core/net/mac/framer-nullmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-nullmac.h&amp;#039;&amp;#039;. It should be used together with &amp;#039;&amp;#039;nullmac&amp;#039;&amp;#039; (MAC layer). The auxiliary function simply fill in the 2 fields of &amp;#039;&amp;#039;nullmac_hdr&amp;#039;&amp;#039;, which are: receiver address and sender address. This is the very basic type of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; that can be used on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-802154&amp;#039;&amp;#039; is defined by &amp;#039;&amp;#039;core/net/mac/framer-802154.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-802154.h&amp;#039;&amp;#039;. It creates and parses frames compatible with standard IEEE 802.15.4 (2003). The &amp;#039;&amp;#039;create&amp;#039;&amp;#039; and &amp;#039;&amp;#039;parse&amp;#039;&amp;#039; functions manipulate all header information abstractedly through the &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure. The real work is done by functions located in the file &amp;#039;&amp;#039;core/net/mac/frame802154.c&amp;#039;&amp;#039;, which are responsible for reading all information from receive/transmit buffer and inserting/extracting them appropriately into &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure.&lt;br /&gt;
&lt;br /&gt;
== RDC protocols ==&lt;br /&gt;
&lt;br /&gt;
The RDC structure, called &amp;#039;&amp;#039;struct rdc_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/rdc.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a RDC (radio duty cycling) driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct rdc_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the RDC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet list */&lt;br /&gt;
  void (* send_list)(mac_callback_t sent_callback, void *ptr, struct rdc_buf_list *list);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function &amp;#039;&amp;#039;input&amp;#039;&amp;#039; is called when a new packet has arrived and need to be processed by RDC layer. And functions &amp;#039;&amp;#039;send&amp;#039;&amp;#039; and &amp;#039;&amp;#039;send_list&amp;#039;&amp;#039; are called for transmitting out-going packets.&lt;br /&gt;
&lt;br /&gt;
There are 2 very basic types of RDC layers implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: &amp;#039;&amp;#039;nullrdc-noframer&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc-noframer.c&amp;#039;&amp;#039;) and &amp;#039;&amp;#039;nullrdc&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc.c&amp;#039;&amp;#039;). The first one does not use Framer functions, and transmits/receives data directly to Radio layer (Physical). The developer can take total control over the format of the packets being transmitted. &lt;br /&gt;
&lt;br /&gt;
The second RDC layer (nullrdc) uses the Framer functions for header creation/parsing. It does not save energy and works as a pass-through layer that only transmits a packet and returns the results of such transmission (success or collision).&lt;br /&gt;
&lt;br /&gt;
A third simple RDC layer is &amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/sicslowmac.c&amp;#039;&amp;#039;). This is a RDC layer with no energy savings and that uses IEEE 8021.5.4 frames.&lt;br /&gt;
&lt;br /&gt;
The three last protocols available in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; are implementations of popular asynchronous MAC protocols that aim at energy saving; namely &amp;#039;&amp;#039;&amp;#039;X-MA&amp;#039;&amp;#039;&amp;#039;C, &amp;#039;&amp;#039;&amp;#039;ContikiMac&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;LPP&amp;#039;&amp;#039;&amp;#039;. X-MAC is a short-preamble protocol from 2006 that was ported to ContikiOS [1]. X-MAC has two different implementation in ContikiOS, which can be found in files &amp;#039;&amp;#039;core/net/mac/cxmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/xmac.c&amp;#039;&amp;#039;; the implementation in cxmac.c is a relaxed implementation that does not require stringent timing precision and work on larger number of platforms. ContikiMac [2] is a protocol that proposes enhancement over X-MAC. The implementation can be found in file &amp;#039;&amp;#039;core/net/mac/contikimac.c&amp;#039;&amp;#039;. Finally, LPP (Low Power Probing) is a receiver-initiated low power protocol from 2008 that was also ported to ContikiOS [3]; the code can be found in &amp;#039;&amp;#039;core/net/mac/lpp.c&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Each of the last three RDC protocols can be used for energy saving. The details of each of these protocols is outside the scope of this tutorial and can be found in the references.&lt;br /&gt;
&lt;br /&gt;
== MAC protocols ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The MAC structure, called &amp;#039;&amp;#039;struct mac_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/mac.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a MAC protocol driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct mac_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the MAC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
  &lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are only two types of MAC protocols in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: nullmac (&amp;#039;&amp;#039;core/net/mac/nullmac.c&amp;#039;&amp;#039;) and csma (&amp;#039;&amp;#039;core/net/mac/csma&amp;#039;&amp;#039;). The first one is a simple pass-through protocol that simply calls the appropriate RDC functions. And the second one implements addressing, sequence number and retransmissions. CSMA protocol keep a list of packets to each of the neighbors and calculate statistics such as number of retransmissions, collisions, deferrals, etc. Even though the name, CSMA mean Carrier-Sense Medium Access, the implementation in ContikiOS does not rely on the carrier sensing, because the medium access is performed by RDC protocol.&lt;br /&gt;
&lt;br /&gt;
== How to choose and change your protocols ==&lt;br /&gt;
&lt;br /&gt;
All three stack layers (Framer, RDC and MAC) need to be defined during compilation time. They are define using pragma &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; either in the Makefile or in the &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file. The definitions of which protocols should be used are used by file &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039; that effectively binds the NETSTACK_FRAMER, NETSTACK_RDC and NETSTACK_MAC to the protocols that are supposed to be used.&lt;br /&gt;
&lt;br /&gt;
Below it is shown the piece of code responsible for setting the MAC layer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_NETWORK&lt;br /&gt;
#ifdef NETSTACK_CONF_NETWORK&lt;br /&gt;
#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK&lt;br /&gt;
#else /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#define NETSTACK_NETWORK rime_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#endif /* NETSTACK_NETWORK */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Initially the code checks if NETSTACK_NETWORK was already define (#ifndef NETSTACK_NETWORK). If it was not, the code then checks if NETSTACK_CONF_NETWORK was define. If NETSTACK_CONF_NETWORK was defined, the desired network protocol is used (#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK); otherwise a default protocol is used, which in this case is &amp;#039;&amp;#039;rime_driver&amp;#039;&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
Hence, if you want to use a particular network protocol, you can either define NETSTACK_NETWORK directly or define NETSTACK_CONF_NETWORK.&lt;br /&gt;
&lt;br /&gt;
If no definitions are made, by default ContikiOS stack layers will be:&lt;br /&gt;
&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Network layer&amp;#039;&amp;#039;&amp;#039; - rime_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;MAC layer&amp;#039;&amp;#039;&amp;#039; - nullmac_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;RDC&amp;#039;&amp;#039;&amp;#039; - nullrdc_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039; - framer_nullmac&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Radio&amp;#039;&amp;#039;&amp;#039; - nullradio_driver&lt;br /&gt;
&lt;br /&gt;
The stack layers are usually define inside file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039;, which overwrites the configuration of &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039;. There is one &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file for each platform; for instance, platform &amp;#039;&amp;#039;sky&amp;#039;&amp;#039; has &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; located inside folder &amp;#039;&amp;#039;platform/sky&amp;#039;&amp;#039;. Below we show the excerpt where stack layers are define:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_CONF_MAC&lt;br /&gt;
#define NETSTACK_CONF_MAC     csma_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_MAC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC&lt;br /&gt;
#define NETSTACK_CONF_RDC     contikimac_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE&lt;br /&gt;
#define NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE 8&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RADIO&lt;br /&gt;
#define NETSTACK_CONF_RADIO   cc2420_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RADIO */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_FRAMER&lt;br /&gt;
#define NETSTACK_CONF_FRAMER  framer_802154&lt;br /&gt;
#endif /* NETSTACK_CONF_FRAMER */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can see that, if we are using Tmote Sky hardware, by default the stack layer will be composed by &amp;#039;&amp;#039;csma_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;contikimac_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;framer_802154&amp;#039;&amp;#039; and &amp;#039;&amp;#039;cc2420_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== Changing the protocols ===&lt;br /&gt;
&lt;br /&gt;
If you want to change the protocols to be used you define them either inside the Makefile or creating a project configuration file.&lt;br /&gt;
&lt;br /&gt;
In the first case you should change Makefile, inputing the definitions in one single line, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
DEFINES = NETSTACK_CONF_RDC=cxmac_driver,NETSTACK_CONF_MAC=null_mac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the protocols that you want to use should be defined in the &amp;#039;&amp;#039;DEFINES&amp;#039;&amp;#039; line, using commas to separate them.&lt;br /&gt;
&lt;br /&gt;
The second option is to create a project configuration file. Let&amp;#039;s assume the file create was named &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039;. This file should be inserted into your Makefile, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
CFLAGS += -DPROJECT_CONF_H=\&amp;quot;project-conf.h\&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file you can define all protocols you want to use, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#define NETSTACK_CONF_MAC nullmac_driver&lt;br /&gt;
#define NETSTACK_CONF_RDC nullrc_driver&lt;br /&gt;
#define NETSTACK_CONF_FRAMER framer_nullmac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color: red&amp;quot;&amp;gt;If you choose to use &amp;#039;&amp;#039;projet-conf.h&amp;#039;&amp;#039; file, you will need to modifed the file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; inside your platform folder. If you are using sky platform, for example, you need to edit file &amp;#039;&amp;#039;platform/sky/contiki-conf.h&amp;#039;&amp;#039;. The last part of &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file should be moved to the beginning of the file, as shown below&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* -*- C -*- */&lt;br /&gt;
&lt;br /&gt;
#ifndef CONTIKI_CONF_H&lt;br /&gt;
#define CONTIKI_CONF_H&lt;br /&gt;
&lt;br /&gt;
/* include the project config */&lt;br /&gt;
/* PROJECT_CONF_H might be defined in the project Makefile */&lt;br /&gt;
#ifdef PROJECT_CONF_H&lt;br /&gt;
#include PROJECT_CONF_H&lt;br /&gt;
#endif /* PROJECT_CONF_H */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to code your own protocol ==&lt;br /&gt;
&lt;br /&gt;
We will explain here how to create a new protocol and integrate it into ContikiOS. The protocol can be a Network, MAC, RDC, Framer or Radio protocol; the steps for creating any one of these protocols are similar. We are going to show in details how to create a new MAC, called &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
First step is to create both header and c files for &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039; protocol. Both files need to be created inside &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Our files will be named &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The file &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039; is shown below. In this file we only declare an external variable that will reference our MAC protocols, called &amp;#039;&amp;#039;modifiedmac_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef __MODIFIEDMAC_H__&lt;br /&gt;
#define __MODIFIEDMAC_H__&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;net/mac/mac.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
extern const struct mac_driver modifiedmac_driver;&lt;br /&gt;
&lt;br /&gt;
#endif /* __MODIFIEDMAC_H__ */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An initial version of file &amp;#039;&amp;#039;modifiedrdc.c&amp;#039;&amp;#039; is shown below. It only contains the declaration of all functions that must be implemented in a MAC driver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;quot;net/mac/modifiedmac.h&amp;quot;&lt;br /&gt;
#include &amp;quot;net/netstack.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static unsigned short&lt;br /&gt;
channel_check_interval(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
init(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
const struct mac_driver modifiedmac_driver = {&lt;br /&gt;
  &amp;quot;modifiedmac&amp;quot;,&lt;br /&gt;
  init,&lt;br /&gt;
  send_packet,&lt;br /&gt;
  packet_input,&lt;br /&gt;
  on,&lt;br /&gt;
  off,&lt;br /&gt;
  channel_check_interval,&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The next step is to include our source code into ContikiOS make system. We just need to edit the file &amp;#039;&amp;#039;core/net/mac/Makefile.mac&amp;#039;&amp;#039; and include the file &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; in the list of Contiki source files. After editing, &amp;#039;&amp;#039;Makefile.mac&amp;#039;&amp;#039; must look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI_SOURCEFILES += cxmac.c xmac.c nullmac.c lpp.c frame802154.c sicslowmac.c nullrdc.c nullrdc-noframer.c mac.c&lt;br /&gt;
CONTIKI_SOURCEFILES += framer-nullmac.c framer-802154.c csma.c contikimac.c phase.c modifiedmac.c&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Below we show the functions that should be changed in order to make our MAC protocol functional. It is going to work exactly as a Null MAC protocol and we are counting the number of packets received and transmitted.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
uint16_t n_pkts_transmitted = 0;&lt;br /&gt;
uint16_t n_pkts_received = 0;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_transmitted++;&lt;br /&gt;
  NETSTACK_RDC.send(sent, ptr);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_received++;&lt;br /&gt;
  NETSTACK_NETWORK.input();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.on();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.off(keep_radio_on);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now you are able to use your create your own rules for addressing, framezation and all functions that should be performed by a MAC protocol.&lt;br /&gt;
&lt;br /&gt;
== Evaluating ContikiMAC and XMAC protocols ==&lt;br /&gt;
&lt;br /&gt;
We are going to evaluate the performance of the two most famous RDC protocols in ContikiOS, the ContikiMAC and X-MAC. We want to evaluate the Packet Reception Rate (PRR), which is the percentage of received packets in an interval; and also the average duty-cycle of sensor nodes. The PRR is related to the maximum throughput the network is able to handle; with lower PRR more packets are dropped and the total amount of data that can be transfered to the sink node is small. Also, the duty-cycle impacts the average power consumption and the lifetime of nodes, since the biggest energy savings are achieved when nodes are sleeping; the lower the duty-cycle the longest will be the node&amp;#039;s lifetime.&lt;br /&gt;
&lt;br /&gt;
We considered the firmware in the folder &amp;#039;&amp;#039;examples/ipv6/rpl-udp/&amp;#039;&amp;#039;, which is a firmware based on IPv6 and RPL for UDP packet transmission. In the same folder there is one simulation file called &amp;#039;&amp;#039;rpl-udp-powertrace.csc&amp;#039;&amp;#039; that is composed of 1 sink node and 30 sensor nodes and traces the energy consumption of all nodes, and also keeps track of PRR values throughout the simulation time. We considered the firmwares &amp;#039;&amp;#039;udp-server.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; and reduced the data transmission interval in &amp;#039;&amp;#039;udl-client.c&amp;#039;&amp;#039; to 1 second. The line below shows where the code of &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; was changed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef PERIOD&lt;br /&gt;
#define PERIOD 1&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By default the simulation is executed with ContikiMAC. We let the simulation run for 5 minutes before extracting the desired statistics. After simulation for ContikiMAC, we changed the RDC protocols, as described before, to X-MAC. And the same statistics were obtained. The figures below show the average duty-cycle of each node for both protocols. The figure on the left shows results of ContikiMAC and the one on the right shows results of X-MAC.&lt;br /&gt;
&lt;br /&gt;
[[File:power_contikimac.png|300px]] [[File:power_xmac.png|300px]]&lt;br /&gt;
&lt;br /&gt;
The PRR statistics were also analysed for all 30 sensor nodes. On average the PRR of ContikiMAC was &amp;#039;&amp;#039;&amp;#039;50.48%&amp;#039;&amp;#039;&amp;#039; and the PRR of X-MAC was &amp;#039;&amp;#039;&amp;#039;30.38%&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[http://www.cse.wustl.edu/~lu/cse521s/Papers/x-mac.pdf 1]- Buettner, Michael, Gary V. Yee, Eric Anderson, and Richard Han. &amp;quot;X-MAC: a short preamble MAC protocol for duty-cycled wireless sensor networks.&amp;quot; In Proceedings of the 4th international conference on Embedded networked sensor systems, pp. 307-320. ACM, 2006.&lt;br /&gt;
&lt;br /&gt;
[http://soda.swedish-ict.se/5128/1/contikimac-report.pdf 2] - Dunkels, Adam. &amp;quot;The contikimac radio duty cycling protocol.&amp;quot; (2011).&lt;br /&gt;
&lt;br /&gt;
[http://web.cs.jhu.edu/~cliang4/public/doc/2008_IPSN_Koala.pdf 3] - Liang, Chieh-Jan Mike, and Andreas Terzis. &amp;quot;Koala: Ultra-low power data retrieval in wireless sensor networks.&amp;quot; In Proceedings of the 7th international conference on Information processing in sensor networks, pp. 421-432. IEEE Computer Society, 2008.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
Edited by: Pedro&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1112</id>
		<title>MAC protocols in ContikiOS</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1112"/>
				<updated>2014-11-08T20:25:43Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* How to code your own protocol */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This tutorial covers the main features of &amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039; protocols available in &amp;#039;&amp;#039;ContikiOS 2.7&amp;#039;&amp;#039;. Medium Access Control protocols take care of the organization of medium access in wireless networks. They can be interpreted as rules that coordinate when each node is going to transmit/receive packets.&lt;br /&gt;
&lt;br /&gt;
There are a large number of protocols available in the literature, they can usually be classified as contention-based or reservation-based protocols. The first group is based on Carrier Sensing for detecting medium activity and are prone to collisions and lower efficiency; however, they are of easy implementation. The second group is more efficient in terms of throughput and energy, but require precise synchronization and is less adaptable to dynamic traffic. &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; only includes MAC protocols of the first category.&lt;br /&gt;
&lt;br /&gt;
There are about 5 types of MAC layers that can be used in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. They are usually implementation of well-known MAC protocols for &amp;#039;&amp;#039;Wireless Sensor Networks&amp;#039;&amp;#039;; a few of them are implementations of protocols developed exclusively for &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;, but that were based on previous work present in the literature.&lt;br /&gt;
&lt;br /&gt;
== You will learn ==&lt;br /&gt;
&lt;br /&gt;
Here you are going to be to be more familiar with all MAC protocols implemented on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. You are going to understand the organization of MAC protocols, where all files are located and where you should start when you want to modify a protocol. You will also learn the steps you need to take in order to implement your own MAC protocol. And finally, we evaluate two famous MAC protocols: &amp;#039;&amp;#039;&amp;#039;ContikiMAC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;X-MAC&amp;#039;&amp;#039;&amp;#039;, showing a few statistics regarding their performance.&lt;br /&gt;
&lt;br /&gt;
== MAC, RDC and Framer drivers ==&lt;br /&gt;
&lt;br /&gt;
The network stack implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; is a little bit different than the usual 5-layers model typically adopted in TCP/IP. In-between the &amp;#039;&amp;#039;Physical&amp;#039;&amp;#039; and the &amp;#039;&amp;#039;Network&amp;#039;&amp;#039; layers, where usually is located the MAC, we have 3 different layers: &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039;&amp;#039;. The figure below shows the organization of layers in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[File:contiki-layers.jpg|150px|center]]&lt;br /&gt;
&lt;br /&gt;
The network layers can be accessed through the global variables &amp;#039;&amp;#039;&amp;#039;NETSTACK_FRAMER&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;NETSTACK_RDC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;NETSTACK_MAC&amp;#039;&amp;#039;&amp;#039;, which are defined in compilation time. All the variables can be found in &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039;. Every network layer is defined by the pragmas &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; in the beginning of this file.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layer is not a regular layer implementation; it is actually a collection of auxiliary functions that are called for creating a frame with data to be transmited and parsing of data being received. Examples of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; types can be found in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; there are two types of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layers: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-802154.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039; layer takes care of the sleep period of nodes. This is the most important layer because it is the one responsible for deciding exactly when the packets will be transmitted and it is responsible for making sure that the node is awake when packets are to be received. RDC protocols&amp;#039; source codes are also located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Examples of RDC layers that are implemented: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;contikimac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;xmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;lpp.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc-noframer.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Finally, the MAC layer takes care of addressing and retransmission of lost packets. The source files can be located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; we have only two types of MAC layers available: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;csma.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Framers ==&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; consists of a collection of auxiliary functions that are called before transmitting a packet and after their reception. The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; structure, called &amp;#039;&amp;#039;struct framer&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/framer.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
struct framer {&lt;br /&gt;
&lt;br /&gt;
  int (* create)(void);&lt;br /&gt;
  int (* parse)(void);&lt;br /&gt;
&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function &amp;#039;&amp;#039;create&amp;#039;&amp;#039; should create a new frame to be transmitted. While function &amp;#039;&amp;#039;parse&amp;#039;&amp;#039; will be called for parsing a received frame.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-nullmac&amp;#039;&amp;#039; is defined by files &amp;#039;&amp;#039;core/net/mac/framer-nullmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-nullmac.h&amp;#039;&amp;#039;. It should be used together with &amp;#039;&amp;#039;nullmac&amp;#039;&amp;#039; (MAC layer). The auxiliary function simply fill in the 2 fields of &amp;#039;&amp;#039;nullmac_hdr&amp;#039;&amp;#039;, which are: receiver address and sender address. This is the very basic type of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; that can be used on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-802154&amp;#039;&amp;#039; is defined by &amp;#039;&amp;#039;core/net/mac/framer-802154.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-802154.h&amp;#039;&amp;#039;. It creates and parses frames compatible with standard IEEE 802.15.4 (2003). The &amp;#039;&amp;#039;create&amp;#039;&amp;#039; and &amp;#039;&amp;#039;parse&amp;#039;&amp;#039; functions manipulate all header information abstractedly through the &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure. The real work is done by functions located in the file &amp;#039;&amp;#039;core/net/mac/frame802154.c&amp;#039;&amp;#039;, which are responsible for reading all information from receive/transmit buffer and inserting/extracting them appropriately into &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure.&lt;br /&gt;
&lt;br /&gt;
== RDC protocols ==&lt;br /&gt;
&lt;br /&gt;
The RDC structure, called &amp;#039;&amp;#039;struct rdc_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/rdc.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a RDC (radio duty cycling) driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct rdc_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the RDC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet list */&lt;br /&gt;
  void (* send_list)(mac_callback_t sent_callback, void *ptr, struct rdc_buf_list *list);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function &amp;#039;&amp;#039;input&amp;#039;&amp;#039; is called when a new packet has arrived and need to be processed by RDC layer. And functions &amp;#039;&amp;#039;send&amp;#039;&amp;#039; and &amp;#039;&amp;#039;send_list&amp;#039;&amp;#039; are called for transmitting out-going packets.&lt;br /&gt;
&lt;br /&gt;
There are 2 very basic types of RDC layers implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: &amp;#039;&amp;#039;nullrdc-noframer&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc-noframer.c&amp;#039;&amp;#039;) and &amp;#039;&amp;#039;nullrdc&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc.c&amp;#039;&amp;#039;). The first one does not use Framer functions, and transmits/receives data directly to Radio layer (Physical). The developer can take total control over the format of the packets being transmitted. &lt;br /&gt;
&lt;br /&gt;
The second RDC layer (nullrdc) uses the Framer functions for header creation/parsing. It does not save energy and works as a pass-through layer that only transmits a packet and returns the results of such transmission (success or collision).&lt;br /&gt;
&lt;br /&gt;
A third simple RDC layer is &amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/sicslowmac.c&amp;#039;&amp;#039;). This is a RDC layer with no energy savings and that uses IEEE 8021.5.4 frames.&lt;br /&gt;
&lt;br /&gt;
The three last protocols available in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; are implementations of popular asynchronous MAC protocols that aim at energy saving; namely &amp;#039;&amp;#039;&amp;#039;X-MA&amp;#039;&amp;#039;&amp;#039;C, &amp;#039;&amp;#039;&amp;#039;ContikiMac&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;LPP&amp;#039;&amp;#039;&amp;#039;. X-MAC is a short-preamble protocol from 2006 that was ported to ContikiOS [1]. X-MAC has two different implementation in ContikiOS, which can be found in files &amp;#039;&amp;#039;core/net/mac/cxmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/xmac.c&amp;#039;&amp;#039;; the implementation in cxmac.c is a relaxed implementation that does not require stringent timing precision and work on larger number of platforms. ContikiMac [2] is a protocol that proposes enhancement over X-MAC. The implementation can be found in file &amp;#039;&amp;#039;core/net/mac/contikimac.c&amp;#039;&amp;#039;. Finally, LPP (Low Power Probing) is a receiver-initiated low power protocol from 2008 that was also ported to ContikiOS [3]; the code can be found in &amp;#039;&amp;#039;core/net/mac/lpp.c&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Each of the last three RDC protocols can be used for energy saving. The details of each of these protocols is outside the scope of this tutorial and can be found in the references.&lt;br /&gt;
&lt;br /&gt;
== MAC protocols ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The MAC structure, called &amp;#039;&amp;#039;struct mac_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/mac.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a MAC protocol driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct mac_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the MAC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
  &lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are only two types of MAC protocols in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: nullmac (&amp;#039;&amp;#039;core/net/mac/nullmac.c&amp;#039;&amp;#039;) and csma (&amp;#039;&amp;#039;core/net/mac/csma&amp;#039;&amp;#039;). The first one is a simple pass-through protocol that simply calls the appropriate RDC functions. And the second one implements addressing, sequence number and retransmissions. CSMA protocol keep a list of packets to each of the neighbors and calculate statistics such as number of retransmissions, collisions, deferrals, etc. Even though the name, CSMA mean Carrier-Sense Medium Access, the implementation in ContikiOS does not rely on the carrier sensing, because the medium access is performed by RDC protocol.&lt;br /&gt;
&lt;br /&gt;
== How to choose and change your protocols ==&lt;br /&gt;
&lt;br /&gt;
All three stack layers (Framer, RDC and MAC) need to be defined during compilation time. They are define using pragma &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; either in the Makefile or in the &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file. The definitions of which protocols should be used are used by file &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039; that effectively binds the NETSTACK_FRAMER, NETSTACK_RDC and NETSTACK_MAC to the protocols that are supposed to be used.&lt;br /&gt;
&lt;br /&gt;
Below it is shown the piece of code responsible for setting the MAC layer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_NETWORK&lt;br /&gt;
#ifdef NETSTACK_CONF_NETWORK&lt;br /&gt;
#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK&lt;br /&gt;
#else /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#define NETSTACK_NETWORK rime_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#endif /* NETSTACK_NETWORK */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Initially the code checks if NETSTACK_NETWORK was already define (#ifndef NETSTACK_NETWORK). If it was not, the code then checks if NETSTACK_CONF_NETWORK was define. If NETSTACK_CONF_NETWORK was defined, the desired network protocol is used (#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK); otherwise a default protocol is used, which in this case is &amp;#039;&amp;#039;rime_driver&amp;#039;&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
Hence, if you want to use a particular network protocol, you can either define NETSTACK_NETWORK directly or define NETSTACK_CONF_NETWORK.&lt;br /&gt;
&lt;br /&gt;
If no definitions are made, by default ContikiOS stack layers will be:&lt;br /&gt;
&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Network layer&amp;#039;&amp;#039;&amp;#039; - rime_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;MAC layer&amp;#039;&amp;#039;&amp;#039; - nullmac_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;RDC&amp;#039;&amp;#039;&amp;#039; - nullrdc_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039; - framer_nullmac&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Radio&amp;#039;&amp;#039;&amp;#039; - nullradio_driver&lt;br /&gt;
&lt;br /&gt;
The stack layers are usually define inside file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039;, which overwrites the configuration of &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039;. There is one &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file for each platform; for instance, platform &amp;#039;&amp;#039;sky&amp;#039;&amp;#039; has &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; located inside folder &amp;#039;&amp;#039;platform/sky&amp;#039;&amp;#039;. Below we show the excerpt where stack layers are define:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_CONF_MAC&lt;br /&gt;
#define NETSTACK_CONF_MAC     csma_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_MAC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC&lt;br /&gt;
#define NETSTACK_CONF_RDC     contikimac_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE&lt;br /&gt;
#define NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE 8&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RADIO&lt;br /&gt;
#define NETSTACK_CONF_RADIO   cc2420_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RADIO */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_FRAMER&lt;br /&gt;
#define NETSTACK_CONF_FRAMER  framer_802154&lt;br /&gt;
#endif /* NETSTACK_CONF_FRAMER */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can see that, if we are using Tmote Sky hardware, by default the stack layer will be composed by &amp;#039;&amp;#039;csma_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;contikimac_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;framer_802154&amp;#039;&amp;#039; and &amp;#039;&amp;#039;cc2420_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== Changing the protocols ===&lt;br /&gt;
&lt;br /&gt;
If you want to change the protocols to be used you define them either inside the Makefile or creating a project configuration file.&lt;br /&gt;
&lt;br /&gt;
In the first case you should change Makefile, inputing the definitions in one single line, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
DEFINES = NETSTACK_CONF_RDC=cxmac_driver,NETSTACK_CONF_MAC=null_mac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the protocols that you want to use should be defined in the &amp;#039;&amp;#039;DEFINES&amp;#039;&amp;#039; line, using commas to separate them.&lt;br /&gt;
&lt;br /&gt;
The second option is to create a project configuration file. Let&amp;#039;s assume the file create was named &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039;. This file should be inserted into your Makefile, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
CFLAGS += -DPROJECT_CONF_H=\&amp;quot;project-conf.h\&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file you can define all protocols you want to use, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#define NETSTACK_CONF_MAC nullrdc_driver&lt;br /&gt;
#define NETSTACK_CONF_RDC nullmac_driver&lt;br /&gt;
#define NETSTACK_CONF_FRAMER framer_nullmac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color: red&amp;quot;&amp;gt;If you choose to use &amp;#039;&amp;#039;projet-conf.h&amp;#039;&amp;#039; file, you will need to modifed the file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; inside your platform folder. If you are using sky platform, for example, you need to edit file &amp;#039;&amp;#039;platform/sky/contiki-conf.h&amp;#039;&amp;#039;. The last part of &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file should be moved to the beginning of the file, as shown below&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* -*- C -*- */&lt;br /&gt;
&lt;br /&gt;
#ifndef CONTIKI_CONF_H&lt;br /&gt;
#define CONTIKI_CONF_H&lt;br /&gt;
&lt;br /&gt;
/* include the project config */&lt;br /&gt;
/* PROJECT_CONF_H might be defined in the project Makefile */&lt;br /&gt;
#ifdef PROJECT_CONF_H&lt;br /&gt;
#include PROJECT_CONF_H&lt;br /&gt;
#endif /* PROJECT_CONF_H */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to code your own protocol ==&lt;br /&gt;
&lt;br /&gt;
We will explain here how to create a new protocol and integrate it into ContikiOS. The protocol can be a Network, MAC, RDC, Framer or Radio protocol; the steps for creating any one of these protocols are similar. We are going to show in details how to create a new MAC, called &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
First step is to create both header and c files for &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039; protocol. Both files need to be created inside &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Our files will be named &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The file &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039; is shown below. In this file we only declare an external variable that will reference our MAC protocols, called &amp;#039;&amp;#039;modifiedmac_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef __MODIFIEDMAC_H__&lt;br /&gt;
#define __MODIFIEDMAC_H__&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;net/mac/mac.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
extern const struct mac_driver modifiedmac_driver;&lt;br /&gt;
&lt;br /&gt;
#endif /* __MODIFIEDMAC_H__ */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An initial version of file &amp;#039;&amp;#039;modifiedrdc.c&amp;#039;&amp;#039; is shown below. It only contains the declaration of all functions that must be implemented in a MAC driver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;quot;net/mac/modifiedmac.h&amp;quot;&lt;br /&gt;
#include &amp;quot;net/netstack.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static unsigned short&lt;br /&gt;
channel_check_interval(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
init(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
const struct mac_driver modifiedmac_driver = {&lt;br /&gt;
  &amp;quot;modifiedmac&amp;quot;,&lt;br /&gt;
  init,&lt;br /&gt;
  send_packet,&lt;br /&gt;
  packet_input,&lt;br /&gt;
  on,&lt;br /&gt;
  off,&lt;br /&gt;
  channel_check_interval,&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The next step is to include our source code into ContikiOS make system. We just need to edit the file &amp;#039;&amp;#039;core/net/mac/Makefile.mac&amp;#039;&amp;#039; and include the file &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; in the list of Contiki source files. After editing, &amp;#039;&amp;#039;Makefile.mac&amp;#039;&amp;#039; must look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI_SOURCEFILES += cxmac.c xmac.c nullmac.c lpp.c frame802154.c sicslowmac.c nullrdc.c nullrdc-noframer.c mac.c&lt;br /&gt;
CONTIKI_SOURCEFILES += framer-nullmac.c framer-802154.c csma.c contikimac.c phase.c modifiedmac.c&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Below we show the functions that should be changed in order to make our MAC protocol functional. It is going to work exactly as a Null MAC protocol and we are counting the number of packets received and transmitted.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
uint16_t n_pkts_transmitted = 0;&lt;br /&gt;
uint16_t n_pkts_received = 0;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_transmitted++;&lt;br /&gt;
  NETSTACK_RDC.send(sent, ptr);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_received++;&lt;br /&gt;
  NETSTACK_NETWORK.input();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.on();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.off(keep_radio_on);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now you are able to use your create your own rules for addressing, framezation and all functions that should be performed by a MAC protocol.&lt;br /&gt;
&lt;br /&gt;
== Evaluating ContikiMAC and XMAC protocols ==&lt;br /&gt;
&lt;br /&gt;
We are going to evaluate the performance of the two most famous RDC protocols in ContikiOS, the ContikiMAC and X-MAC. We want to evaluate the Packet Reception Rate (PRR), which is the percentage of received packets in an interval; and also the average duty-cycle of sensor nodes. The PRR is related to the maximum throughput the network is able to handle; with lower PRR more packets are dropped and the total amount of data that can be transfered to the sink node is small. Also, the duty-cycle impacts the average power consumption and the lifetime of nodes, since the biggest energy savings are achieved when nodes are sleeping; the lower the duty-cycle the longest will be the node&amp;#039;s lifetime.&lt;br /&gt;
&lt;br /&gt;
We considered the firmware in the folder &amp;#039;&amp;#039;examples/ipv6/rpl-udp/&amp;#039;&amp;#039;, which is a firmware based on IPv6 and RPL for UDP packet transmission. In the same folder there is one simulation file called &amp;#039;&amp;#039;rpl-udp-powertrace.csc&amp;#039;&amp;#039; that is composed of 1 sink node and 30 sensor nodes and traces the energy consumption of all nodes, and also keeps track of PRR values throughout the simulation time. We considered the firmwares &amp;#039;&amp;#039;udp-server.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; and reduced the data transmission interval in &amp;#039;&amp;#039;udl-client.c&amp;#039;&amp;#039; to 1 second. The line below shows where the code of &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; was changed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef PERIOD&lt;br /&gt;
#define PERIOD 1&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By default the simulation is executed with ContikiMAC. We let the simulation run for 5 minutes before extracting the desired statistics. After simulation for ContikiMAC, we changed the RDC protocols, as described before, to X-MAC. And the same statistics were obtained. The figures below show the average duty-cycle of each node for both protocols. The figure on the left shows results of ContikiMAC and the one on the right shows results of X-MAC.&lt;br /&gt;
&lt;br /&gt;
[[File:power_contikimac.png|300px]] [[File:power_xmac.png|300px]]&lt;br /&gt;
&lt;br /&gt;
The PRR statistics were also analysed for all 30 sensor nodes. On average the PRR of ContikiMAC was &amp;#039;&amp;#039;&amp;#039;50.48%&amp;#039;&amp;#039;&amp;#039; and the PRR of X-MAC was &amp;#039;&amp;#039;&amp;#039;30.38%&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[http://www.cse.wustl.edu/~lu/cse521s/Papers/x-mac.pdf 1]- Buettner, Michael, Gary V. Yee, Eric Anderson, and Richard Han. &amp;quot;X-MAC: a short preamble MAC protocol for duty-cycled wireless sensor networks.&amp;quot; In Proceedings of the 4th international conference on Embedded networked sensor systems, pp. 307-320. ACM, 2006.&lt;br /&gt;
&lt;br /&gt;
[http://soda.swedish-ict.se/5128/1/contikimac-report.pdf 2] - Dunkels, Adam. &amp;quot;The contikimac radio duty cycling protocol.&amp;quot; (2011).&lt;br /&gt;
&lt;br /&gt;
[http://web.cs.jhu.edu/~cliang4/public/doc/2008_IPSN_Koala.pdf 3] - Liang, Chieh-Jan Mike, and Andreas Terzis. &amp;quot;Koala: Ultra-low power data retrieval in wireless sensor networks.&amp;quot; In Proceedings of the 7th international conference on Information processing in sensor networks, pp. 421-432. IEEE Computer Society, 2008.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
Edited by: Pedro&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1111</id>
		<title>MAC protocols in ContikiOS</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1111"/>
				<updated>2014-11-08T20:22:49Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* Changing the protocols */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This tutorial covers the main features of &amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039; protocols available in &amp;#039;&amp;#039;ContikiOS 2.7&amp;#039;&amp;#039;. Medium Access Control protocols take care of the organization of medium access in wireless networks. They can be interpreted as rules that coordinate when each node is going to transmit/receive packets.&lt;br /&gt;
&lt;br /&gt;
There are a large number of protocols available in the literature, they can usually be classified as contention-based or reservation-based protocols. The first group is based on Carrier Sensing for detecting medium activity and are prone to collisions and lower efficiency; however, they are of easy implementation. The second group is more efficient in terms of throughput and energy, but require precise synchronization and is less adaptable to dynamic traffic. &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; only includes MAC protocols of the first category.&lt;br /&gt;
&lt;br /&gt;
There are about 5 types of MAC layers that can be used in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. They are usually implementation of well-known MAC protocols for &amp;#039;&amp;#039;Wireless Sensor Networks&amp;#039;&amp;#039;; a few of them are implementations of protocols developed exclusively for &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;, but that were based on previous work present in the literature.&lt;br /&gt;
&lt;br /&gt;
== You will learn ==&lt;br /&gt;
&lt;br /&gt;
Here you are going to be to be more familiar with all MAC protocols implemented on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. You are going to understand the organization of MAC protocols, where all files are located and where you should start when you want to modify a protocol. You will also learn the steps you need to take in order to implement your own MAC protocol. And finally, we evaluate two famous MAC protocols: &amp;#039;&amp;#039;&amp;#039;ContikiMAC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;X-MAC&amp;#039;&amp;#039;&amp;#039;, showing a few statistics regarding their performance.&lt;br /&gt;
&lt;br /&gt;
== MAC, RDC and Framer drivers ==&lt;br /&gt;
&lt;br /&gt;
The network stack implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; is a little bit different than the usual 5-layers model typically adopted in TCP/IP. In-between the &amp;#039;&amp;#039;Physical&amp;#039;&amp;#039; and the &amp;#039;&amp;#039;Network&amp;#039;&amp;#039; layers, where usually is located the MAC, we have 3 different layers: &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039;&amp;#039;. The figure below shows the organization of layers in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[File:contiki-layers.jpg|150px|center]]&lt;br /&gt;
&lt;br /&gt;
The network layers can be accessed through the global variables &amp;#039;&amp;#039;&amp;#039;NETSTACK_FRAMER&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;NETSTACK_RDC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;NETSTACK_MAC&amp;#039;&amp;#039;&amp;#039;, which are defined in compilation time. All the variables can be found in &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039;. Every network layer is defined by the pragmas &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; in the beginning of this file.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layer is not a regular layer implementation; it is actually a collection of auxiliary functions that are called for creating a frame with data to be transmited and parsing of data being received. Examples of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; types can be found in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; there are two types of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layers: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-802154.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039; layer takes care of the sleep period of nodes. This is the most important layer because it is the one responsible for deciding exactly when the packets will be transmitted and it is responsible for making sure that the node is awake when packets are to be received. RDC protocols&amp;#039; source codes are also located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Examples of RDC layers that are implemented: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;contikimac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;xmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;lpp.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc-noframer.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Finally, the MAC layer takes care of addressing and retransmission of lost packets. The source files can be located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; we have only two types of MAC layers available: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;csma.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Framers ==&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; consists of a collection of auxiliary functions that are called before transmitting a packet and after their reception. The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; structure, called &amp;#039;&amp;#039;struct framer&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/framer.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
struct framer {&lt;br /&gt;
&lt;br /&gt;
  int (* create)(void);&lt;br /&gt;
  int (* parse)(void);&lt;br /&gt;
&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function &amp;#039;&amp;#039;create&amp;#039;&amp;#039; should create a new frame to be transmitted. While function &amp;#039;&amp;#039;parse&amp;#039;&amp;#039; will be called for parsing a received frame.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-nullmac&amp;#039;&amp;#039; is defined by files &amp;#039;&amp;#039;core/net/mac/framer-nullmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-nullmac.h&amp;#039;&amp;#039;. It should be used together with &amp;#039;&amp;#039;nullmac&amp;#039;&amp;#039; (MAC layer). The auxiliary function simply fill in the 2 fields of &amp;#039;&amp;#039;nullmac_hdr&amp;#039;&amp;#039;, which are: receiver address and sender address. This is the very basic type of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; that can be used on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-802154&amp;#039;&amp;#039; is defined by &amp;#039;&amp;#039;core/net/mac/framer-802154.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-802154.h&amp;#039;&amp;#039;. It creates and parses frames compatible with standard IEEE 802.15.4 (2003). The &amp;#039;&amp;#039;create&amp;#039;&amp;#039; and &amp;#039;&amp;#039;parse&amp;#039;&amp;#039; functions manipulate all header information abstractedly through the &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure. The real work is done by functions located in the file &amp;#039;&amp;#039;core/net/mac/frame802154.c&amp;#039;&amp;#039;, which are responsible for reading all information from receive/transmit buffer and inserting/extracting them appropriately into &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure.&lt;br /&gt;
&lt;br /&gt;
== RDC protocols ==&lt;br /&gt;
&lt;br /&gt;
The RDC structure, called &amp;#039;&amp;#039;struct rdc_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/rdc.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a RDC (radio duty cycling) driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct rdc_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the RDC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet list */&lt;br /&gt;
  void (* send_list)(mac_callback_t sent_callback, void *ptr, struct rdc_buf_list *list);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function &amp;#039;&amp;#039;input&amp;#039;&amp;#039; is called when a new packet has arrived and need to be processed by RDC layer. And functions &amp;#039;&amp;#039;send&amp;#039;&amp;#039; and &amp;#039;&amp;#039;send_list&amp;#039;&amp;#039; are called for transmitting out-going packets.&lt;br /&gt;
&lt;br /&gt;
There are 2 very basic types of RDC layers implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: &amp;#039;&amp;#039;nullrdc-noframer&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc-noframer.c&amp;#039;&amp;#039;) and &amp;#039;&amp;#039;nullrdc&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc.c&amp;#039;&amp;#039;). The first one does not use Framer functions, and transmits/receives data directly to Radio layer (Physical). The developer can take total control over the format of the packets being transmitted. &lt;br /&gt;
&lt;br /&gt;
The second RDC layer (nullrdc) uses the Framer functions for header creation/parsing. It does not save energy and works as a pass-through layer that only transmits a packet and returns the results of such transmission (success or collision).&lt;br /&gt;
&lt;br /&gt;
A third simple RDC layer is &amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/sicslowmac.c&amp;#039;&amp;#039;). This is a RDC layer with no energy savings and that uses IEEE 8021.5.4 frames.&lt;br /&gt;
&lt;br /&gt;
The three last protocols available in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; are implementations of popular asynchronous MAC protocols that aim at energy saving; namely &amp;#039;&amp;#039;&amp;#039;X-MA&amp;#039;&amp;#039;&amp;#039;C, &amp;#039;&amp;#039;&amp;#039;ContikiMac&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;LPP&amp;#039;&amp;#039;&amp;#039;. X-MAC is a short-preamble protocol from 2006 that was ported to ContikiOS [1]. X-MAC has two different implementation in ContikiOS, which can be found in files &amp;#039;&amp;#039;core/net/mac/cxmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/xmac.c&amp;#039;&amp;#039;; the implementation in cxmac.c is a relaxed implementation that does not require stringent timing precision and work on larger number of platforms. ContikiMac [2] is a protocol that proposes enhancement over X-MAC. The implementation can be found in file &amp;#039;&amp;#039;core/net/mac/contikimac.c&amp;#039;&amp;#039;. Finally, LPP (Low Power Probing) is a receiver-initiated low power protocol from 2008 that was also ported to ContikiOS [3]; the code can be found in &amp;#039;&amp;#039;core/net/mac/lpp.c&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Each of the last three RDC protocols can be used for energy saving. The details of each of these protocols is outside the scope of this tutorial and can be found in the references.&lt;br /&gt;
&lt;br /&gt;
== MAC protocols ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The MAC structure, called &amp;#039;&amp;#039;struct mac_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/mac.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a MAC protocol driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct mac_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the MAC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
  &lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are only two types of MAC protocols in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: nullmac (&amp;#039;&amp;#039;core/net/mac/nullmac.c&amp;#039;&amp;#039;) and csma (&amp;#039;&amp;#039;core/net/mac/csma&amp;#039;&amp;#039;). The first one is a simple pass-through protocol that simply calls the appropriate RDC functions. And the second one implements addressing, sequence number and retransmissions. CSMA protocol keep a list of packets to each of the neighbors and calculate statistics such as number of retransmissions, collisions, deferrals, etc. Even though the name, CSMA mean Carrier-Sense Medium Access, the implementation in ContikiOS does not rely on the carrier sensing, because the medium access is performed by RDC protocol.&lt;br /&gt;
&lt;br /&gt;
== How to choose and change your protocols ==&lt;br /&gt;
&lt;br /&gt;
All three stack layers (Framer, RDC and MAC) need to be defined during compilation time. They are define using pragma &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; either in the Makefile or in the &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file. The definitions of which protocols should be used are used by file &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039; that effectively binds the NETSTACK_FRAMER, NETSTACK_RDC and NETSTACK_MAC to the protocols that are supposed to be used.&lt;br /&gt;
&lt;br /&gt;
Below it is shown the piece of code responsible for setting the MAC layer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_NETWORK&lt;br /&gt;
#ifdef NETSTACK_CONF_NETWORK&lt;br /&gt;
#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK&lt;br /&gt;
#else /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#define NETSTACK_NETWORK rime_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#endif /* NETSTACK_NETWORK */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Initially the code checks if NETSTACK_NETWORK was already define (#ifndef NETSTACK_NETWORK). If it was not, the code then checks if NETSTACK_CONF_NETWORK was define. If NETSTACK_CONF_NETWORK was defined, the desired network protocol is used (#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK); otherwise a default protocol is used, which in this case is &amp;#039;&amp;#039;rime_driver&amp;#039;&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
Hence, if you want to use a particular network protocol, you can either define NETSTACK_NETWORK directly or define NETSTACK_CONF_NETWORK.&lt;br /&gt;
&lt;br /&gt;
If no definitions are made, by default ContikiOS stack layers will be:&lt;br /&gt;
&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Network layer&amp;#039;&amp;#039;&amp;#039; - rime_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;MAC layer&amp;#039;&amp;#039;&amp;#039; - nullmac_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;RDC&amp;#039;&amp;#039;&amp;#039; - nullrdc_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039; - framer_nullmac&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Radio&amp;#039;&amp;#039;&amp;#039; - nullradio_driver&lt;br /&gt;
&lt;br /&gt;
The stack layers are usually define inside file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039;, which overwrites the configuration of &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039;. There is one &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file for each platform; for instance, platform &amp;#039;&amp;#039;sky&amp;#039;&amp;#039; has &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; located inside folder &amp;#039;&amp;#039;platform/sky&amp;#039;&amp;#039;. Below we show the excerpt where stack layers are define:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_CONF_MAC&lt;br /&gt;
#define NETSTACK_CONF_MAC     csma_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_MAC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC&lt;br /&gt;
#define NETSTACK_CONF_RDC     contikimac_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE&lt;br /&gt;
#define NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE 8&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RADIO&lt;br /&gt;
#define NETSTACK_CONF_RADIO   cc2420_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RADIO */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_FRAMER&lt;br /&gt;
#define NETSTACK_CONF_FRAMER  framer_802154&lt;br /&gt;
#endif /* NETSTACK_CONF_FRAMER */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can see that, if we are using Tmote Sky hardware, by default the stack layer will be composed by &amp;#039;&amp;#039;csma_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;contikimac_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;framer_802154&amp;#039;&amp;#039; and &amp;#039;&amp;#039;cc2420_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== Changing the protocols ===&lt;br /&gt;
&lt;br /&gt;
If you want to change the protocols to be used you define them either inside the Makefile or creating a project configuration file.&lt;br /&gt;
&lt;br /&gt;
In the first case you should change Makefile, inputing the definitions in one single line, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
DEFINES = NETSTACK_CONF_RDC=cxmac_driver,NETSTACK_CONF_MAC=null_mac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the protocols that you want to use should be defined in the &amp;#039;&amp;#039;DEFINES&amp;#039;&amp;#039; line, using commas to separate them.&lt;br /&gt;
&lt;br /&gt;
The second option is to create a project configuration file. Let&amp;#039;s assume the file create was named &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039;. This file should be inserted into your Makefile, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
CFLAGS += -DPROJECT_CONF_H=\&amp;quot;project-conf.h\&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file you can define all protocols you want to use, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#define NETSTACK_CONF_MAC nullrdc_driver&lt;br /&gt;
#define NETSTACK_CONF_RDC nullmac_driver&lt;br /&gt;
#define NETSTACK_CONF_FRAMER framer_nullmac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color: red&amp;quot;&amp;gt;If you choose to use &amp;#039;&amp;#039;projet-conf.h&amp;#039;&amp;#039; file, you will need to modifed the file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; inside your platform folder. If you are using sky platform, for example, you need to edit file &amp;#039;&amp;#039;platform/sky/contiki-conf.h&amp;#039;&amp;#039;. The last part of &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file should be moved to the beginning of the file, as shown below&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* -*- C -*- */&lt;br /&gt;
&lt;br /&gt;
#ifndef CONTIKI_CONF_H&lt;br /&gt;
#define CONTIKI_CONF_H&lt;br /&gt;
&lt;br /&gt;
/* include the project config */&lt;br /&gt;
/* PROJECT_CONF_H might be defined in the project Makefile */&lt;br /&gt;
#ifdef PROJECT_CONF_H&lt;br /&gt;
#include PROJECT_CONF_H&lt;br /&gt;
#endif /* PROJECT_CONF_H */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to code your own protocol ==&lt;br /&gt;
&lt;br /&gt;
We will explain here how to create a new protocol and integrate it into ContikiOS. The protocol can be a Network, MAC, RDC, Framer or Radio; the steps for creating any one of these protocols are similar. We are going to show in details how to create a new MAC protocol, called &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
First step is to create both header and c files for &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039; protocol. Both files need to be created inside &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Our files will be named &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The file &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039; is shown below. In this file we only declare an external variable that will reference our MAC protocols, called &amp;#039;&amp;#039;modifiedmac_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef __MODIFIEDMAC_H__&lt;br /&gt;
#define __MODIFIEDMAC_H__&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;net/mac/mac.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
extern const struct mac_driver modifiedmac_driver;&lt;br /&gt;
&lt;br /&gt;
#endif /* __MODIFIEDMAC_H__ */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An initial version of file &amp;#039;&amp;#039;modifiedrdc.c&amp;#039;&amp;#039; is show below. It only contains the declaration of all functions that must be implemented in a RDC driver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;quot;net/mac/modifiedmac.h&amp;quot;&lt;br /&gt;
#include &amp;quot;net/netstack.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static unsigned short&lt;br /&gt;
channel_check_interval(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
init(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
const struct mac_driver modifiedmac_driver = {&lt;br /&gt;
  &amp;quot;modifiedmac&amp;quot;,&lt;br /&gt;
  init,&lt;br /&gt;
  send_packet,&lt;br /&gt;
  packet_input,&lt;br /&gt;
  on,&lt;br /&gt;
  off,&lt;br /&gt;
  channel_check_interval,&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The next step is to include our source code into ContikiOS make system. We just need to edit the file &amp;#039;&amp;#039;core/net/mac/Makefile.mac&amp;#039;&amp;#039; and include the file &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; in the list of Contiki source files. After editing, &amp;#039;&amp;#039;Makefile.mac&amp;#039;&amp;#039; must look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI_SOURCEFILES += cxmac.c xmac.c nullmac.c lpp.c frame802154.c sicslowmac.c nullrdc.c nullrdc-noframer.c mac.c&lt;br /&gt;
CONTIKI_SOURCEFILES += framer-nullmac.c framer-802154.c csma.c contikimac.c phase.c modifiedmac.c&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Below we show the functions that should be changed in order to make our MAC protocol functional. It is going to work exactly as a Null MAC protocol and we are counting the number of packets received and transmitted.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
uint16_t n_pkts_transmitted = 0;&lt;br /&gt;
uint16_t n_pkts_received = 0;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_transmitted++;&lt;br /&gt;
  NETSTACK_RDC.send(sent, ptr);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_received++;&lt;br /&gt;
  NETSTACK_NETWORK.input();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.on();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.off(keep_radio_on);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Evaluating ContikiMAC and XMAC protocols ==&lt;br /&gt;
&lt;br /&gt;
We are going to evaluate the performance of the two most famous RDC protocols in ContikiOS, the ContikiMAC and X-MAC. We want to evaluate the Packet Reception Rate (PRR), which is the percentage of received packets in an interval; and also the average duty-cycle of sensor nodes. The PRR is related to the maximum throughput the network is able to handle; with lower PRR more packets are dropped and the total amount of data that can be transfered to the sink node is small. Also, the duty-cycle impacts the average power consumption and the lifetime of nodes, since the biggest energy savings are achieved when nodes are sleeping; the lower the duty-cycle the longest will be the node&amp;#039;s lifetime.&lt;br /&gt;
&lt;br /&gt;
We considered the firmware in the folder &amp;#039;&amp;#039;examples/ipv6/rpl-udp/&amp;#039;&amp;#039;, which is a firmware based on IPv6 and RPL for UDP packet transmission. In the same folder there is one simulation file called &amp;#039;&amp;#039;rpl-udp-powertrace.csc&amp;#039;&amp;#039; that is composed of 1 sink node and 30 sensor nodes and traces the energy consumption of all nodes, and also keeps track of PRR values throughout the simulation time. We considered the firmwares &amp;#039;&amp;#039;udp-server.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; and reduced the data transmission interval in &amp;#039;&amp;#039;udl-client.c&amp;#039;&amp;#039; to 1 second. The line below shows where the code of &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; was changed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef PERIOD&lt;br /&gt;
#define PERIOD 1&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By default the simulation is executed with ContikiMAC. We let the simulation run for 5 minutes before extracting the desired statistics. After simulation for ContikiMAC, we changed the RDC protocols, as described before, to X-MAC. And the same statistics were obtained. The figures below show the average duty-cycle of each node for both protocols. The figure on the left shows results of ContikiMAC and the one on the right shows results of X-MAC.&lt;br /&gt;
&lt;br /&gt;
[[File:power_contikimac.png|300px]] [[File:power_xmac.png|300px]]&lt;br /&gt;
&lt;br /&gt;
The PRR statistics were also analysed for all 30 sensor nodes. On average the PRR of ContikiMAC was &amp;#039;&amp;#039;&amp;#039;50.48%&amp;#039;&amp;#039;&amp;#039; and the PRR of X-MAC was &amp;#039;&amp;#039;&amp;#039;30.38%&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[http://www.cse.wustl.edu/~lu/cse521s/Papers/x-mac.pdf 1]- Buettner, Michael, Gary V. Yee, Eric Anderson, and Richard Han. &amp;quot;X-MAC: a short preamble MAC protocol for duty-cycled wireless sensor networks.&amp;quot; In Proceedings of the 4th international conference on Embedded networked sensor systems, pp. 307-320. ACM, 2006.&lt;br /&gt;
&lt;br /&gt;
[http://soda.swedish-ict.se/5128/1/contikimac-report.pdf 2] - Dunkels, Adam. &amp;quot;The contikimac radio duty cycling protocol.&amp;quot; (2011).&lt;br /&gt;
&lt;br /&gt;
[http://web.cs.jhu.edu/~cliang4/public/doc/2008_IPSN_Koala.pdf 3] - Liang, Chieh-Jan Mike, and Andreas Terzis. &amp;quot;Koala: Ultra-low power data retrieval in wireless sensor networks.&amp;quot; In Proceedings of the 7th international conference on Information processing in sensor networks, pp. 421-432. IEEE Computer Society, 2008.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
Edited by: Pedro&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1110</id>
		<title>MAC protocols in ContikiOS</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1110"/>
				<updated>2014-11-08T20:21:26Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* How to choose and change your protocols */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This tutorial covers the main features of &amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039; protocols available in &amp;#039;&amp;#039;ContikiOS 2.7&amp;#039;&amp;#039;. Medium Access Control protocols take care of the organization of medium access in wireless networks. They can be interpreted as rules that coordinate when each node is going to transmit/receive packets.&lt;br /&gt;
&lt;br /&gt;
There are a large number of protocols available in the literature, they can usually be classified as contention-based or reservation-based protocols. The first group is based on Carrier Sensing for detecting medium activity and are prone to collisions and lower efficiency; however, they are of easy implementation. The second group is more efficient in terms of throughput and energy, but require precise synchronization and is less adaptable to dynamic traffic. &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; only includes MAC protocols of the first category.&lt;br /&gt;
&lt;br /&gt;
There are about 5 types of MAC layers that can be used in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. They are usually implementation of well-known MAC protocols for &amp;#039;&amp;#039;Wireless Sensor Networks&amp;#039;&amp;#039;; a few of them are implementations of protocols developed exclusively for &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;, but that were based on previous work present in the literature.&lt;br /&gt;
&lt;br /&gt;
== You will learn ==&lt;br /&gt;
&lt;br /&gt;
Here you are going to be to be more familiar with all MAC protocols implemented on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. You are going to understand the organization of MAC protocols, where all files are located and where you should start when you want to modify a protocol. You will also learn the steps you need to take in order to implement your own MAC protocol. And finally, we evaluate two famous MAC protocols: &amp;#039;&amp;#039;&amp;#039;ContikiMAC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;X-MAC&amp;#039;&amp;#039;&amp;#039;, showing a few statistics regarding their performance.&lt;br /&gt;
&lt;br /&gt;
== MAC, RDC and Framer drivers ==&lt;br /&gt;
&lt;br /&gt;
The network stack implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; is a little bit different than the usual 5-layers model typically adopted in TCP/IP. In-between the &amp;#039;&amp;#039;Physical&amp;#039;&amp;#039; and the &amp;#039;&amp;#039;Network&amp;#039;&amp;#039; layers, where usually is located the MAC, we have 3 different layers: &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039;&amp;#039;. The figure below shows the organization of layers in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[File:contiki-layers.jpg|150px|center]]&lt;br /&gt;
&lt;br /&gt;
The network layers can be accessed through the global variables &amp;#039;&amp;#039;&amp;#039;NETSTACK_FRAMER&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;NETSTACK_RDC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;NETSTACK_MAC&amp;#039;&amp;#039;&amp;#039;, which are defined in compilation time. All the variables can be found in &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039;. Every network layer is defined by the pragmas &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; in the beginning of this file.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layer is not a regular layer implementation; it is actually a collection of auxiliary functions that are called for creating a frame with data to be transmited and parsing of data being received. Examples of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; types can be found in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; there are two types of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layers: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-802154.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039; layer takes care of the sleep period of nodes. This is the most important layer because it is the one responsible for deciding exactly when the packets will be transmitted and it is responsible for making sure that the node is awake when packets are to be received. RDC protocols&amp;#039; source codes are also located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Examples of RDC layers that are implemented: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;contikimac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;xmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;lpp.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc-noframer.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Finally, the MAC layer takes care of addressing and retransmission of lost packets. The source files can be located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; we have only two types of MAC layers available: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;csma.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Framers ==&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; consists of a collection of auxiliary functions that are called before transmitting a packet and after their reception. The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; structure, called &amp;#039;&amp;#039;struct framer&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/framer.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
struct framer {&lt;br /&gt;
&lt;br /&gt;
  int (* create)(void);&lt;br /&gt;
  int (* parse)(void);&lt;br /&gt;
&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function &amp;#039;&amp;#039;create&amp;#039;&amp;#039; should create a new frame to be transmitted. While function &amp;#039;&amp;#039;parse&amp;#039;&amp;#039; will be called for parsing a received frame.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-nullmac&amp;#039;&amp;#039; is defined by files &amp;#039;&amp;#039;core/net/mac/framer-nullmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-nullmac.h&amp;#039;&amp;#039;. It should be used together with &amp;#039;&amp;#039;nullmac&amp;#039;&amp;#039; (MAC layer). The auxiliary function simply fill in the 2 fields of &amp;#039;&amp;#039;nullmac_hdr&amp;#039;&amp;#039;, which are: receiver address and sender address. This is the very basic type of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; that can be used on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-802154&amp;#039;&amp;#039; is defined by &amp;#039;&amp;#039;core/net/mac/framer-802154.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-802154.h&amp;#039;&amp;#039;. It creates and parses frames compatible with standard IEEE 802.15.4 (2003). The &amp;#039;&amp;#039;create&amp;#039;&amp;#039; and &amp;#039;&amp;#039;parse&amp;#039;&amp;#039; functions manipulate all header information abstractedly through the &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure. The real work is done by functions located in the file &amp;#039;&amp;#039;core/net/mac/frame802154.c&amp;#039;&amp;#039;, which are responsible for reading all information from receive/transmit buffer and inserting/extracting them appropriately into &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure.&lt;br /&gt;
&lt;br /&gt;
== RDC protocols ==&lt;br /&gt;
&lt;br /&gt;
The RDC structure, called &amp;#039;&amp;#039;struct rdc_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/rdc.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a RDC (radio duty cycling) driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct rdc_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the RDC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet list */&lt;br /&gt;
  void (* send_list)(mac_callback_t sent_callback, void *ptr, struct rdc_buf_list *list);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function &amp;#039;&amp;#039;input&amp;#039;&amp;#039; is called when a new packet has arrived and need to be processed by RDC layer. And functions &amp;#039;&amp;#039;send&amp;#039;&amp;#039; and &amp;#039;&amp;#039;send_list&amp;#039;&amp;#039; are called for transmitting out-going packets.&lt;br /&gt;
&lt;br /&gt;
There are 2 very basic types of RDC layers implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: &amp;#039;&amp;#039;nullrdc-noframer&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc-noframer.c&amp;#039;&amp;#039;) and &amp;#039;&amp;#039;nullrdc&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc.c&amp;#039;&amp;#039;). The first one does not use Framer functions, and transmits/receives data directly to Radio layer (Physical). The developer can take total control over the format of the packets being transmitted. &lt;br /&gt;
&lt;br /&gt;
The second RDC layer (nullrdc) uses the Framer functions for header creation/parsing. It does not save energy and works as a pass-through layer that only transmits a packet and returns the results of such transmission (success or collision).&lt;br /&gt;
&lt;br /&gt;
A third simple RDC layer is &amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/sicslowmac.c&amp;#039;&amp;#039;). This is a RDC layer with no energy savings and that uses IEEE 8021.5.4 frames.&lt;br /&gt;
&lt;br /&gt;
The three last protocols available in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; are implementations of popular asynchronous MAC protocols that aim at energy saving; namely &amp;#039;&amp;#039;&amp;#039;X-MA&amp;#039;&amp;#039;&amp;#039;C, &amp;#039;&amp;#039;&amp;#039;ContikiMac&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;LPP&amp;#039;&amp;#039;&amp;#039;. X-MAC is a short-preamble protocol from 2006 that was ported to ContikiOS [1]. X-MAC has two different implementation in ContikiOS, which can be found in files &amp;#039;&amp;#039;core/net/mac/cxmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/xmac.c&amp;#039;&amp;#039;; the implementation in cxmac.c is a relaxed implementation that does not require stringent timing precision and work on larger number of platforms. ContikiMac [2] is a protocol that proposes enhancement over X-MAC. The implementation can be found in file &amp;#039;&amp;#039;core/net/mac/contikimac.c&amp;#039;&amp;#039;. Finally, LPP (Low Power Probing) is a receiver-initiated low power protocol from 2008 that was also ported to ContikiOS [3]; the code can be found in &amp;#039;&amp;#039;core/net/mac/lpp.c&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Each of the last three RDC protocols can be used for energy saving. The details of each of these protocols is outside the scope of this tutorial and can be found in the references.&lt;br /&gt;
&lt;br /&gt;
== MAC protocols ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The MAC structure, called &amp;#039;&amp;#039;struct mac_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/mac.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a MAC protocol driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct mac_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the MAC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
  &lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are only two types of MAC protocols in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: nullmac (&amp;#039;&amp;#039;core/net/mac/nullmac.c&amp;#039;&amp;#039;) and csma (&amp;#039;&amp;#039;core/net/mac/csma&amp;#039;&amp;#039;). The first one is a simple pass-through protocol that simply calls the appropriate RDC functions. And the second one implements addressing, sequence number and retransmissions. CSMA protocol keep a list of packets to each of the neighbors and calculate statistics such as number of retransmissions, collisions, deferrals, etc. Even though the name, CSMA mean Carrier-Sense Medium Access, the implementation in ContikiOS does not rely on the carrier sensing, because the medium access is performed by RDC protocol.&lt;br /&gt;
&lt;br /&gt;
== How to choose and change your protocols ==&lt;br /&gt;
&lt;br /&gt;
All three stack layers (Framer, RDC and MAC) need to be defined during compilation time. They are define using pragma &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; either in the Makefile or in the &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file. The definitions of which protocols should be used are used by file &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039; that effectively binds the NETSTACK_FRAMER, NETSTACK_RDC and NETSTACK_MAC to the protocols that are supposed to be used.&lt;br /&gt;
&lt;br /&gt;
Below it is shown the piece of code responsible for setting the MAC layer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_NETWORK&lt;br /&gt;
#ifdef NETSTACK_CONF_NETWORK&lt;br /&gt;
#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK&lt;br /&gt;
#else /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#define NETSTACK_NETWORK rime_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#endif /* NETSTACK_NETWORK */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Initially the code checks if NETSTACK_NETWORK was already define (#ifndef NETSTACK_NETWORK). If it was not, the code then checks if NETSTACK_CONF_NETWORK was define. If NETSTACK_CONF_NETWORK was defined, the desired network protocol is used (#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK); otherwise a default protocol is used, which in this case is &amp;#039;&amp;#039;rime_driver&amp;#039;&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
Hence, if you want to use a particular network protocol, you can either define NETSTACK_NETWORK directly or define NETSTACK_CONF_NETWORK.&lt;br /&gt;
&lt;br /&gt;
If no definitions are made, by default ContikiOS stack layers will be:&lt;br /&gt;
&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Network layer&amp;#039;&amp;#039;&amp;#039; - rime_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;MAC layer&amp;#039;&amp;#039;&amp;#039; - nullmac_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;RDC&amp;#039;&amp;#039;&amp;#039; - nullrdc_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039; - framer_nullmac&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Radio&amp;#039;&amp;#039;&amp;#039; - nullradio_driver&lt;br /&gt;
&lt;br /&gt;
The stack layers are usually define inside file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039;, which overwrites the configuration of &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039;. There is one &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file for each platform; for instance, platform &amp;#039;&amp;#039;sky&amp;#039;&amp;#039; has &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; located inside folder &amp;#039;&amp;#039;platform/sky&amp;#039;&amp;#039;. Below we show the excerpt where stack layers are define:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_CONF_MAC&lt;br /&gt;
#define NETSTACK_CONF_MAC     csma_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_MAC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC&lt;br /&gt;
#define NETSTACK_CONF_RDC     contikimac_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE&lt;br /&gt;
#define NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE 8&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RADIO&lt;br /&gt;
#define NETSTACK_CONF_RADIO   cc2420_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RADIO */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_FRAMER&lt;br /&gt;
#define NETSTACK_CONF_FRAMER  framer_802154&lt;br /&gt;
#endif /* NETSTACK_CONF_FRAMER */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can see that, if we are using Tmote Sky hardware, by default the stack layer will be composed by &amp;#039;&amp;#039;csma_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;contikimac_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;framer_802154&amp;#039;&amp;#039; and &amp;#039;&amp;#039;cc2420_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== Changing the protocols ===&lt;br /&gt;
&lt;br /&gt;
If you want to change the protocols to be used you define them either inside the Makefile or creating a project configuration file.&lt;br /&gt;
&lt;br /&gt;
In the first case you should change Makefile, inputing the definitions in one single line, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
DEFINES = NETSTACK_CONF_RDC=cxmac_driver,NETSTACK_CONF_MAC=null_mac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the protocols that you want to use should be defined in the &amp;#039;&amp;#039;DEFINES&amp;#039;&amp;#039; line, using comma to separate them.&lt;br /&gt;
&lt;br /&gt;
The second option is to create a project configuration file. Let&amp;#039;s assume the file create was named &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039;. This file should be inserted into your Makefile, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
CFLAGS += -DPROJECT_CONF_H=\&amp;quot;project-conf.h\&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file you can define all protocols you want to use, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#define NETSTACK_CONF_MAC nullrdc_driver&lt;br /&gt;
#define NETSTACK_CONF_RDC nullmac_driver&lt;br /&gt;
#define NETSTACK_CONF_FRAMER framer_nullmac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color: red&amp;quot;&amp;gt;If you choose to use &amp;#039;&amp;#039;projet-conf.h&amp;#039;&amp;#039; file, you will need to modifed the file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; inside your platform folder. If you are using sky platform, for example, you need to change file &amp;#039;&amp;#039;platform/sky/contiki-conf.h&amp;#039;&amp;#039;. The last part of &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file should be moved to the beginning of the file, as shown below&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* -*- C -*- */&lt;br /&gt;
&lt;br /&gt;
#ifndef CONTIKI_CONF_H&lt;br /&gt;
#define CONTIKI_CONF_H&lt;br /&gt;
&lt;br /&gt;
/* include the project config */&lt;br /&gt;
/* PROJECT_CONF_H might be defined in the project Makefile */&lt;br /&gt;
#ifdef PROJECT_CONF_H&lt;br /&gt;
#include PROJECT_CONF_H&lt;br /&gt;
#endif /* PROJECT_CONF_H */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to code your own protocol ==&lt;br /&gt;
&lt;br /&gt;
We will explain here how to create a new protocol and integrate it into ContikiOS. The protocol can be a Network, MAC, RDC, Framer or Radio; the steps for creating any one of these protocols are similar. We are going to show in details how to create a new MAC protocol, called &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
First step is to create both header and c files for &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039; protocol. Both files need to be created inside &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Our files will be named &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The file &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039; is shown below. In this file we only declare an external variable that will reference our MAC protocols, called &amp;#039;&amp;#039;modifiedmac_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef __MODIFIEDMAC_H__&lt;br /&gt;
#define __MODIFIEDMAC_H__&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;net/mac/mac.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
extern const struct mac_driver modifiedmac_driver;&lt;br /&gt;
&lt;br /&gt;
#endif /* __MODIFIEDMAC_H__ */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An initial version of file &amp;#039;&amp;#039;modifiedrdc.c&amp;#039;&amp;#039; is show below. It only contains the declaration of all functions that must be implemented in a RDC driver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;quot;net/mac/modifiedmac.h&amp;quot;&lt;br /&gt;
#include &amp;quot;net/netstack.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static unsigned short&lt;br /&gt;
channel_check_interval(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
init(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
const struct mac_driver modifiedmac_driver = {&lt;br /&gt;
  &amp;quot;modifiedmac&amp;quot;,&lt;br /&gt;
  init,&lt;br /&gt;
  send_packet,&lt;br /&gt;
  packet_input,&lt;br /&gt;
  on,&lt;br /&gt;
  off,&lt;br /&gt;
  channel_check_interval,&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The next step is to include our source code into ContikiOS make system. We just need to edit the file &amp;#039;&amp;#039;core/net/mac/Makefile.mac&amp;#039;&amp;#039; and include the file &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; in the list of Contiki source files. After editing, &amp;#039;&amp;#039;Makefile.mac&amp;#039;&amp;#039; must look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI_SOURCEFILES += cxmac.c xmac.c nullmac.c lpp.c frame802154.c sicslowmac.c nullrdc.c nullrdc-noframer.c mac.c&lt;br /&gt;
CONTIKI_SOURCEFILES += framer-nullmac.c framer-802154.c csma.c contikimac.c phase.c modifiedmac.c&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Below we show the functions that should be changed in order to make our MAC protocol functional. It is going to work exactly as a Null MAC protocol and we are counting the number of packets received and transmitted.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
uint16_t n_pkts_transmitted = 0;&lt;br /&gt;
uint16_t n_pkts_received = 0;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_transmitted++;&lt;br /&gt;
  NETSTACK_RDC.send(sent, ptr);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_received++;&lt;br /&gt;
  NETSTACK_NETWORK.input();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.on();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.off(keep_radio_on);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Evaluating ContikiMAC and XMAC protocols ==&lt;br /&gt;
&lt;br /&gt;
We are going to evaluate the performance of the two most famous RDC protocols in ContikiOS, the ContikiMAC and X-MAC. We want to evaluate the Packet Reception Rate (PRR), which is the percentage of received packets in an interval; and also the average duty-cycle of sensor nodes. The PRR is related to the maximum throughput the network is able to handle; with lower PRR more packets are dropped and the total amount of data that can be transfered to the sink node is small. Also, the duty-cycle impacts the average power consumption and the lifetime of nodes, since the biggest energy savings are achieved when nodes are sleeping; the lower the duty-cycle the longest will be the node&amp;#039;s lifetime.&lt;br /&gt;
&lt;br /&gt;
We considered the firmware in the folder &amp;#039;&amp;#039;examples/ipv6/rpl-udp/&amp;#039;&amp;#039;, which is a firmware based on IPv6 and RPL for UDP packet transmission. In the same folder there is one simulation file called &amp;#039;&amp;#039;rpl-udp-powertrace.csc&amp;#039;&amp;#039; that is composed of 1 sink node and 30 sensor nodes and traces the energy consumption of all nodes, and also keeps track of PRR values throughout the simulation time. We considered the firmwares &amp;#039;&amp;#039;udp-server.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; and reduced the data transmission interval in &amp;#039;&amp;#039;udl-client.c&amp;#039;&amp;#039; to 1 second. The line below shows where the code of &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; was changed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef PERIOD&lt;br /&gt;
#define PERIOD 1&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By default the simulation is executed with ContikiMAC. We let the simulation run for 5 minutes before extracting the desired statistics. After simulation for ContikiMAC, we changed the RDC protocols, as described before, to X-MAC. And the same statistics were obtained. The figures below show the average duty-cycle of each node for both protocols. The figure on the left shows results of ContikiMAC and the one on the right shows results of X-MAC.&lt;br /&gt;
&lt;br /&gt;
[[File:power_contikimac.png|300px]] [[File:power_xmac.png|300px]]&lt;br /&gt;
&lt;br /&gt;
The PRR statistics were also analysed for all 30 sensor nodes. On average the PRR of ContikiMAC was &amp;#039;&amp;#039;&amp;#039;50.48%&amp;#039;&amp;#039;&amp;#039; and the PRR of X-MAC was &amp;#039;&amp;#039;&amp;#039;30.38%&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[http://www.cse.wustl.edu/~lu/cse521s/Papers/x-mac.pdf 1]- Buettner, Michael, Gary V. Yee, Eric Anderson, and Richard Han. &amp;quot;X-MAC: a short preamble MAC protocol for duty-cycled wireless sensor networks.&amp;quot; In Proceedings of the 4th international conference on Embedded networked sensor systems, pp. 307-320. ACM, 2006.&lt;br /&gt;
&lt;br /&gt;
[http://soda.swedish-ict.se/5128/1/contikimac-report.pdf 2] - Dunkels, Adam. &amp;quot;The contikimac radio duty cycling protocol.&amp;quot; (2011).&lt;br /&gt;
&lt;br /&gt;
[http://web.cs.jhu.edu/~cliang4/public/doc/2008_IPSN_Koala.pdf 3] - Liang, Chieh-Jan Mike, and Andreas Terzis. &amp;quot;Koala: Ultra-low power data retrieval in wireless sensor networks.&amp;quot; In Proceedings of the 7th international conference on Information processing in sensor networks, pp. 421-432. IEEE Computer Society, 2008.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
Edited by: Pedro&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1109</id>
		<title>MAC protocols in ContikiOS</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1109"/>
				<updated>2014-11-08T20:18:52Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* MAC protocols */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This tutorial covers the main features of &amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039; protocols available in &amp;#039;&amp;#039;ContikiOS 2.7&amp;#039;&amp;#039;. Medium Access Control protocols take care of the organization of medium access in wireless networks. They can be interpreted as rules that coordinate when each node is going to transmit/receive packets.&lt;br /&gt;
&lt;br /&gt;
There are a large number of protocols available in the literature, they can usually be classified as contention-based or reservation-based protocols. The first group is based on Carrier Sensing for detecting medium activity and are prone to collisions and lower efficiency; however, they are of easy implementation. The second group is more efficient in terms of throughput and energy, but require precise synchronization and is less adaptable to dynamic traffic. &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; only includes MAC protocols of the first category.&lt;br /&gt;
&lt;br /&gt;
There are about 5 types of MAC layers that can be used in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. They are usually implementation of well-known MAC protocols for &amp;#039;&amp;#039;Wireless Sensor Networks&amp;#039;&amp;#039;; a few of them are implementations of protocols developed exclusively for &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;, but that were based on previous work present in the literature.&lt;br /&gt;
&lt;br /&gt;
== You will learn ==&lt;br /&gt;
&lt;br /&gt;
Here you are going to be to be more familiar with all MAC protocols implemented on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. You are going to understand the organization of MAC protocols, where all files are located and where you should start when you want to modify a protocol. You will also learn the steps you need to take in order to implement your own MAC protocol. And finally, we evaluate two famous MAC protocols: &amp;#039;&amp;#039;&amp;#039;ContikiMAC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;X-MAC&amp;#039;&amp;#039;&amp;#039;, showing a few statistics regarding their performance.&lt;br /&gt;
&lt;br /&gt;
== MAC, RDC and Framer drivers ==&lt;br /&gt;
&lt;br /&gt;
The network stack implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; is a little bit different than the usual 5-layers model typically adopted in TCP/IP. In-between the &amp;#039;&amp;#039;Physical&amp;#039;&amp;#039; and the &amp;#039;&amp;#039;Network&amp;#039;&amp;#039; layers, where usually is located the MAC, we have 3 different layers: &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039;&amp;#039;. The figure below shows the organization of layers in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[File:contiki-layers.jpg|150px|center]]&lt;br /&gt;
&lt;br /&gt;
The network layers can be accessed through the global variables &amp;#039;&amp;#039;&amp;#039;NETSTACK_FRAMER&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;NETSTACK_RDC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;NETSTACK_MAC&amp;#039;&amp;#039;&amp;#039;, which are defined in compilation time. All the variables can be found in &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039;. Every network layer is defined by the pragmas &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; in the beginning of this file.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layer is not a regular layer implementation; it is actually a collection of auxiliary functions that are called for creating a frame with data to be transmited and parsing of data being received. Examples of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; types can be found in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; there are two types of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layers: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-802154.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039; layer takes care of the sleep period of nodes. This is the most important layer because it is the one responsible for deciding exactly when the packets will be transmitted and it is responsible for making sure that the node is awake when packets are to be received. RDC protocols&amp;#039; source codes are also located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Examples of RDC layers that are implemented: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;contikimac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;xmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;lpp.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc-noframer.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Finally, the MAC layer takes care of addressing and retransmission of lost packets. The source files can be located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; we have only two types of MAC layers available: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;csma.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Framers ==&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; consists of a collection of auxiliary functions that are called before transmitting a packet and after their reception. The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; structure, called &amp;#039;&amp;#039;struct framer&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/framer.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
struct framer {&lt;br /&gt;
&lt;br /&gt;
  int (* create)(void);&lt;br /&gt;
  int (* parse)(void);&lt;br /&gt;
&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function &amp;#039;&amp;#039;create&amp;#039;&amp;#039; should create a new frame to be transmitted. While function &amp;#039;&amp;#039;parse&amp;#039;&amp;#039; will be called for parsing a received frame.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-nullmac&amp;#039;&amp;#039; is defined by files &amp;#039;&amp;#039;core/net/mac/framer-nullmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-nullmac.h&amp;#039;&amp;#039;. It should be used together with &amp;#039;&amp;#039;nullmac&amp;#039;&amp;#039; (MAC layer). The auxiliary function simply fill in the 2 fields of &amp;#039;&amp;#039;nullmac_hdr&amp;#039;&amp;#039;, which are: receiver address and sender address. This is the very basic type of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; that can be used on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-802154&amp;#039;&amp;#039; is defined by &amp;#039;&amp;#039;core/net/mac/framer-802154.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-802154.h&amp;#039;&amp;#039;. It creates and parses frames compatible with standard IEEE 802.15.4 (2003). The &amp;#039;&amp;#039;create&amp;#039;&amp;#039; and &amp;#039;&amp;#039;parse&amp;#039;&amp;#039; functions manipulate all header information abstractedly through the &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure. The real work is done by functions located in the file &amp;#039;&amp;#039;core/net/mac/frame802154.c&amp;#039;&amp;#039;, which are responsible for reading all information from receive/transmit buffer and inserting/extracting them appropriately into &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure.&lt;br /&gt;
&lt;br /&gt;
== RDC protocols ==&lt;br /&gt;
&lt;br /&gt;
The RDC structure, called &amp;#039;&amp;#039;struct rdc_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/rdc.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a RDC (radio duty cycling) driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct rdc_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the RDC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet list */&lt;br /&gt;
  void (* send_list)(mac_callback_t sent_callback, void *ptr, struct rdc_buf_list *list);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function &amp;#039;&amp;#039;input&amp;#039;&amp;#039; is called when a new packet has arrived and need to be processed by RDC layer. And functions &amp;#039;&amp;#039;send&amp;#039;&amp;#039; and &amp;#039;&amp;#039;send_list&amp;#039;&amp;#039; are called for transmitting out-going packets.&lt;br /&gt;
&lt;br /&gt;
There are 2 very basic types of RDC layers implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: &amp;#039;&amp;#039;nullrdc-noframer&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc-noframer.c&amp;#039;&amp;#039;) and &amp;#039;&amp;#039;nullrdc&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc.c&amp;#039;&amp;#039;). The first one does not use Framer functions, and transmits/receives data directly to Radio layer (Physical). The developer can take total control over the format of the packets being transmitted. &lt;br /&gt;
&lt;br /&gt;
The second RDC layer (nullrdc) uses the Framer functions for header creation/parsing. It does not save energy and works as a pass-through layer that only transmits a packet and returns the results of such transmission (success or collision).&lt;br /&gt;
&lt;br /&gt;
A third simple RDC layer is &amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/sicslowmac.c&amp;#039;&amp;#039;). This is a RDC layer with no energy savings and that uses IEEE 8021.5.4 frames.&lt;br /&gt;
&lt;br /&gt;
The three last protocols available in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; are implementations of popular asynchronous MAC protocols that aim at energy saving; namely &amp;#039;&amp;#039;&amp;#039;X-MA&amp;#039;&amp;#039;&amp;#039;C, &amp;#039;&amp;#039;&amp;#039;ContikiMac&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;LPP&amp;#039;&amp;#039;&amp;#039;. X-MAC is a short-preamble protocol from 2006 that was ported to ContikiOS [1]. X-MAC has two different implementation in ContikiOS, which can be found in files &amp;#039;&amp;#039;core/net/mac/cxmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/xmac.c&amp;#039;&amp;#039;; the implementation in cxmac.c is a relaxed implementation that does not require stringent timing precision and work on larger number of platforms. ContikiMac [2] is a protocol that proposes enhancement over X-MAC. The implementation can be found in file &amp;#039;&amp;#039;core/net/mac/contikimac.c&amp;#039;&amp;#039;. Finally, LPP (Low Power Probing) is a receiver-initiated low power protocol from 2008 that was also ported to ContikiOS [3]; the code can be found in &amp;#039;&amp;#039;core/net/mac/lpp.c&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Each of the last three RDC protocols can be used for energy saving. The details of each of these protocols is outside the scope of this tutorial and can be found in the references.&lt;br /&gt;
&lt;br /&gt;
== MAC protocols ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The MAC structure, called &amp;#039;&amp;#039;struct mac_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/mac.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a MAC protocol driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct mac_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the MAC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
  &lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are only two types of MAC protocols in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: nullmac (&amp;#039;&amp;#039;core/net/mac/nullmac.c&amp;#039;&amp;#039;) and csma (&amp;#039;&amp;#039;core/net/mac/csma&amp;#039;&amp;#039;). The first one is a simple pass-through protocol that simply calls the appropriate RDC functions. And the second one implements addressing, sequence number and retransmissions. CSMA protocol keep a list of packets to each of the neighbors and calculate statistics such as number of retransmissions, collisions, deferrals, etc. Even though the name, CSMA mean Carrier-Sense Medium Access, the implementation in ContikiOS does not rely on the carrier sensing, because the medium access is performed by RDC protocol.&lt;br /&gt;
&lt;br /&gt;
== How to choose and change your protocols ==&lt;br /&gt;
&lt;br /&gt;
All three stack layers (Framer, RDC and MAC) need to be defined during compilation time. They are define using pragma &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; either in the Makefile or in the &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file. The definitions of which protocols should be used are used by file &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039; that effectively binds the NETSTACK_FRAMER, NETSTACK_RDC and NETSTACK_MAC to the protocols that are supposed to be used.&lt;br /&gt;
&lt;br /&gt;
Below it is shown the piece of code responsible for setting the MAC layer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_NETWORK&lt;br /&gt;
#ifdef NETSTACK_CONF_NETWORK&lt;br /&gt;
#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK&lt;br /&gt;
#else /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#define NETSTACK_NETWORK rime_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#endif /* NETSTACK_NETWORK */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Initially the code checks if NETSTACK_NETWORK was already define (#ifndef NETSTACK_NETWORK). If it was not, the code then checks if NETSTACK_CONF_NETWORK was define. If NETSTACK_CONF_NETWORK was defined, the desired network protocol is used (#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK); otherwise a default protocol is used, which in this case is &amp;#039;&amp;#039;rime_driver&amp;#039;&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
Hence, if you want to use a particular network protocol, you can either define NETSTACK_NETWORK directly or define NETSTACK_CONF_NETWORK.&lt;br /&gt;
&lt;br /&gt;
If no definitions are made, by default ContikiOS stack layers will be:&lt;br /&gt;
&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Network layer&amp;#039;&amp;#039;&amp;#039; - rime_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;MAC layer&amp;#039;&amp;#039;&amp;#039; - nullmac_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;RDC&amp;#039;&amp;#039;&amp;#039; - nullrdc_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039; - framer_nullmac&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Radio&amp;#039;&amp;#039;&amp;#039; - nullradio_driver&lt;br /&gt;
&lt;br /&gt;
The stack layers are usually define inside file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039;. There is one &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file for each platform; for instance, platform &amp;#039;&amp;#039;sky&amp;#039;&amp;#039; has &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; located inside folder &amp;#039;&amp;#039;platform/sky&amp;#039;&amp;#039;. Below we show the excerpt where stack layers are define:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_CONF_MAC&lt;br /&gt;
#define NETSTACK_CONF_MAC     csma_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_MAC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC&lt;br /&gt;
#define NETSTACK_CONF_RDC     contikimac_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE&lt;br /&gt;
#define NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE 8&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RADIO&lt;br /&gt;
#define NETSTACK_CONF_RADIO   cc2420_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RADIO */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_FRAMER&lt;br /&gt;
#define NETSTACK_CONF_FRAMER  framer_802154&lt;br /&gt;
#endif /* NETSTACK_CONF_FRAMER */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can see that, if we are using Tmote Sky hardware, by default the stack layer will be composed by &amp;#039;&amp;#039;csma_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;contikimac_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;framer_802154&amp;#039;&amp;#039; and &amp;#039;&amp;#039;cc2420_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== Changing the protocols ===&lt;br /&gt;
&lt;br /&gt;
If you want to change the protocols to be used you define them either inside the Makefile or creating a project configuration file.&lt;br /&gt;
&lt;br /&gt;
In the first case you should change Makefile, inputing the definitions in one single line, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
DEFINES = NETSTACK_CONF_RDC=cxmac_driver,NETSTACK_CONF_MAC=null_mac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the protocols that you want to use should be defined in the &amp;#039;&amp;#039;DEFINES&amp;#039;&amp;#039; line, using comma to separate them.&lt;br /&gt;
&lt;br /&gt;
The second option is to create a project configuration file. Let&amp;#039;s assume the file create was named &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039;. This file should be inserted into your Makefile, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
CFLAGS += -DPROJECT_CONF_H=\&amp;quot;project-conf.h\&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file you can define all protocols you want to use, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#define NETSTACK_CONF_MAC nullrdc_driver&lt;br /&gt;
#define NETSTACK_CONF_RDC nullmac_driver&lt;br /&gt;
#define NETSTACK_CONF_FRAMER framer_nullmac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color: red&amp;quot;&amp;gt;If you choose to use &amp;#039;&amp;#039;projet-conf.h&amp;#039;&amp;#039; file, you will need to modifed the file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; inside your platform folder. If you are using sky platform, for example, you need to change file &amp;#039;&amp;#039;platform/sky/contiki-conf.h&amp;#039;&amp;#039;. The last part of &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file should be moved to the beginning of the file, as shown below&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* -*- C -*- */&lt;br /&gt;
&lt;br /&gt;
#ifndef CONTIKI_CONF_H&lt;br /&gt;
#define CONTIKI_CONF_H&lt;br /&gt;
&lt;br /&gt;
/* include the project config */&lt;br /&gt;
/* PROJECT_CONF_H might be defined in the project Makefile */&lt;br /&gt;
#ifdef PROJECT_CONF_H&lt;br /&gt;
#include PROJECT_CONF_H&lt;br /&gt;
#endif /* PROJECT_CONF_H */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to code your own protocol ==&lt;br /&gt;
&lt;br /&gt;
We will explain here how to create a new protocol and integrate it into ContikiOS. The protocol can be a Network, MAC, RDC, Framer or Radio; the steps for creating any one of these protocols are similar. We are going to show in details how to create a new MAC protocol, called &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
First step is to create both header and c files for &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039; protocol. Both files need to be created inside &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Our files will be named &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The file &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039; is shown below. In this file we only declare an external variable that will reference our MAC protocols, called &amp;#039;&amp;#039;modifiedmac_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef __MODIFIEDMAC_H__&lt;br /&gt;
#define __MODIFIEDMAC_H__&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;net/mac/mac.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
extern const struct mac_driver modifiedmac_driver;&lt;br /&gt;
&lt;br /&gt;
#endif /* __MODIFIEDMAC_H__ */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An initial version of file &amp;#039;&amp;#039;modifiedrdc.c&amp;#039;&amp;#039; is show below. It only contains the declaration of all functions that must be implemented in a RDC driver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;quot;net/mac/modifiedmac.h&amp;quot;&lt;br /&gt;
#include &amp;quot;net/netstack.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static unsigned short&lt;br /&gt;
channel_check_interval(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
init(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
const struct mac_driver modifiedmac_driver = {&lt;br /&gt;
  &amp;quot;modifiedmac&amp;quot;,&lt;br /&gt;
  init,&lt;br /&gt;
  send_packet,&lt;br /&gt;
  packet_input,&lt;br /&gt;
  on,&lt;br /&gt;
  off,&lt;br /&gt;
  channel_check_interval,&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The next step is to include our source code into ContikiOS make system. We just need to edit the file &amp;#039;&amp;#039;core/net/mac/Makefile.mac&amp;#039;&amp;#039; and include the file &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; in the list of Contiki source files. After editing, &amp;#039;&amp;#039;Makefile.mac&amp;#039;&amp;#039; must look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI_SOURCEFILES += cxmac.c xmac.c nullmac.c lpp.c frame802154.c sicslowmac.c nullrdc.c nullrdc-noframer.c mac.c&lt;br /&gt;
CONTIKI_SOURCEFILES += framer-nullmac.c framer-802154.c csma.c contikimac.c phase.c modifiedmac.c&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Below we show the functions that should be changed in order to make our MAC protocol functional. It is going to work exactly as a Null MAC protocol and we are counting the number of packets received and transmitted.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
uint16_t n_pkts_transmitted = 0;&lt;br /&gt;
uint16_t n_pkts_received = 0;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_transmitted++;&lt;br /&gt;
  NETSTACK_RDC.send(sent, ptr);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_received++;&lt;br /&gt;
  NETSTACK_NETWORK.input();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.on();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.off(keep_radio_on);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Evaluating ContikiMAC and XMAC protocols ==&lt;br /&gt;
&lt;br /&gt;
We are going to evaluate the performance of the two most famous RDC protocols in ContikiOS, the ContikiMAC and X-MAC. We want to evaluate the Packet Reception Rate (PRR), which is the percentage of received packets in an interval; and also the average duty-cycle of sensor nodes. The PRR is related to the maximum throughput the network is able to handle; with lower PRR more packets are dropped and the total amount of data that can be transfered to the sink node is small. Also, the duty-cycle impacts the average power consumption and the lifetime of nodes, since the biggest energy savings are achieved when nodes are sleeping; the lower the duty-cycle the longest will be the node&amp;#039;s lifetime.&lt;br /&gt;
&lt;br /&gt;
We considered the firmware in the folder &amp;#039;&amp;#039;examples/ipv6/rpl-udp/&amp;#039;&amp;#039;, which is a firmware based on IPv6 and RPL for UDP packet transmission. In the same folder there is one simulation file called &amp;#039;&amp;#039;rpl-udp-powertrace.csc&amp;#039;&amp;#039; that is composed of 1 sink node and 30 sensor nodes and traces the energy consumption of all nodes, and also keeps track of PRR values throughout the simulation time. We considered the firmwares &amp;#039;&amp;#039;udp-server.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; and reduced the data transmission interval in &amp;#039;&amp;#039;udl-client.c&amp;#039;&amp;#039; to 1 second. The line below shows where the code of &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; was changed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef PERIOD&lt;br /&gt;
#define PERIOD 1&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By default the simulation is executed with ContikiMAC. We let the simulation run for 5 minutes before extracting the desired statistics. After simulation for ContikiMAC, we changed the RDC protocols, as described before, to X-MAC. And the same statistics were obtained. The figures below show the average duty-cycle of each node for both protocols. The figure on the left shows results of ContikiMAC and the one on the right shows results of X-MAC.&lt;br /&gt;
&lt;br /&gt;
[[File:power_contikimac.png|300px]] [[File:power_xmac.png|300px]]&lt;br /&gt;
&lt;br /&gt;
The PRR statistics were also analysed for all 30 sensor nodes. On average the PRR of ContikiMAC was &amp;#039;&amp;#039;&amp;#039;50.48%&amp;#039;&amp;#039;&amp;#039; and the PRR of X-MAC was &amp;#039;&amp;#039;&amp;#039;30.38%&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[http://www.cse.wustl.edu/~lu/cse521s/Papers/x-mac.pdf 1]- Buettner, Michael, Gary V. Yee, Eric Anderson, and Richard Han. &amp;quot;X-MAC: a short preamble MAC protocol for duty-cycled wireless sensor networks.&amp;quot; In Proceedings of the 4th international conference on Embedded networked sensor systems, pp. 307-320. ACM, 2006.&lt;br /&gt;
&lt;br /&gt;
[http://soda.swedish-ict.se/5128/1/contikimac-report.pdf 2] - Dunkels, Adam. &amp;quot;The contikimac radio duty cycling protocol.&amp;quot; (2011).&lt;br /&gt;
&lt;br /&gt;
[http://web.cs.jhu.edu/~cliang4/public/doc/2008_IPSN_Koala.pdf 3] - Liang, Chieh-Jan Mike, and Andreas Terzis. &amp;quot;Koala: Ultra-low power data retrieval in wireless sensor networks.&amp;quot; In Proceedings of the 7th international conference on Information processing in sensor networks, pp. 421-432. IEEE Computer Society, 2008.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
Edited by: Pedro&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1108</id>
		<title>MAC protocols in ContikiOS</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1108"/>
				<updated>2014-11-08T20:16:15Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* RDC protocols */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This tutorial covers the main features of &amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039; protocols available in &amp;#039;&amp;#039;ContikiOS 2.7&amp;#039;&amp;#039;. Medium Access Control protocols take care of the organization of medium access in wireless networks. They can be interpreted as rules that coordinate when each node is going to transmit/receive packets.&lt;br /&gt;
&lt;br /&gt;
There are a large number of protocols available in the literature, they can usually be classified as contention-based or reservation-based protocols. The first group is based on Carrier Sensing for detecting medium activity and are prone to collisions and lower efficiency; however, they are of easy implementation. The second group is more efficient in terms of throughput and energy, but require precise synchronization and is less adaptable to dynamic traffic. &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; only includes MAC protocols of the first category.&lt;br /&gt;
&lt;br /&gt;
There are about 5 types of MAC layers that can be used in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. They are usually implementation of well-known MAC protocols for &amp;#039;&amp;#039;Wireless Sensor Networks&amp;#039;&amp;#039;; a few of them are implementations of protocols developed exclusively for &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;, but that were based on previous work present in the literature.&lt;br /&gt;
&lt;br /&gt;
== You will learn ==&lt;br /&gt;
&lt;br /&gt;
Here you are going to be to be more familiar with all MAC protocols implemented on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. You are going to understand the organization of MAC protocols, where all files are located and where you should start when you want to modify a protocol. You will also learn the steps you need to take in order to implement your own MAC protocol. And finally, we evaluate two famous MAC protocols: &amp;#039;&amp;#039;&amp;#039;ContikiMAC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;X-MAC&amp;#039;&amp;#039;&amp;#039;, showing a few statistics regarding their performance.&lt;br /&gt;
&lt;br /&gt;
== MAC, RDC and Framer drivers ==&lt;br /&gt;
&lt;br /&gt;
The network stack implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; is a little bit different than the usual 5-layers model typically adopted in TCP/IP. In-between the &amp;#039;&amp;#039;Physical&amp;#039;&amp;#039; and the &amp;#039;&amp;#039;Network&amp;#039;&amp;#039; layers, where usually is located the MAC, we have 3 different layers: &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039;&amp;#039;. The figure below shows the organization of layers in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[File:contiki-layers.jpg|150px|center]]&lt;br /&gt;
&lt;br /&gt;
The network layers can be accessed through the global variables &amp;#039;&amp;#039;&amp;#039;NETSTACK_FRAMER&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;NETSTACK_RDC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;NETSTACK_MAC&amp;#039;&amp;#039;&amp;#039;, which are defined in compilation time. All the variables can be found in &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039;. Every network layer is defined by the pragmas &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; in the beginning of this file.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layer is not a regular layer implementation; it is actually a collection of auxiliary functions that are called for creating a frame with data to be transmited and parsing of data being received. Examples of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; types can be found in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; there are two types of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layers: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-802154.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039; layer takes care of the sleep period of nodes. This is the most important layer because it is the one responsible for deciding exactly when the packets will be transmitted and it is responsible for making sure that the node is awake when packets are to be received. RDC protocols&amp;#039; source codes are also located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Examples of RDC layers that are implemented: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;contikimac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;xmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;lpp.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc-noframer.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Finally, the MAC layer takes care of addressing and retransmission of lost packets. The source files can be located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; we have only two types of MAC layers available: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;csma.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Framers ==&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; consists of a collection of auxiliary functions that are called before transmitting a packet and after their reception. The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; structure, called &amp;#039;&amp;#039;struct framer&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/framer.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
struct framer {&lt;br /&gt;
&lt;br /&gt;
  int (* create)(void);&lt;br /&gt;
  int (* parse)(void);&lt;br /&gt;
&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function &amp;#039;&amp;#039;create&amp;#039;&amp;#039; should create a new frame to be transmitted. While function &amp;#039;&amp;#039;parse&amp;#039;&amp;#039; will be called for parsing a received frame.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-nullmac&amp;#039;&amp;#039; is defined by files &amp;#039;&amp;#039;core/net/mac/framer-nullmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-nullmac.h&amp;#039;&amp;#039;. It should be used together with &amp;#039;&amp;#039;nullmac&amp;#039;&amp;#039; (MAC layer). The auxiliary function simply fill in the 2 fields of &amp;#039;&amp;#039;nullmac_hdr&amp;#039;&amp;#039;, which are: receiver address and sender address. This is the very basic type of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; that can be used on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-802154&amp;#039;&amp;#039; is defined by &amp;#039;&amp;#039;core/net/mac/framer-802154.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-802154.h&amp;#039;&amp;#039;. It creates and parses frames compatible with standard IEEE 802.15.4 (2003). The &amp;#039;&amp;#039;create&amp;#039;&amp;#039; and &amp;#039;&amp;#039;parse&amp;#039;&amp;#039; functions manipulate all header information abstractedly through the &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure. The real work is done by functions located in the file &amp;#039;&amp;#039;core/net/mac/frame802154.c&amp;#039;&amp;#039;, which are responsible for reading all information from receive/transmit buffer and inserting/extracting them appropriately into &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure.&lt;br /&gt;
&lt;br /&gt;
== RDC protocols ==&lt;br /&gt;
&lt;br /&gt;
The RDC structure, called &amp;#039;&amp;#039;struct rdc_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/rdc.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a RDC (radio duty cycling) driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct rdc_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the RDC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet list */&lt;br /&gt;
  void (* send_list)(mac_callback_t sent_callback, void *ptr, struct rdc_buf_list *list);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function &amp;#039;&amp;#039;input&amp;#039;&amp;#039; is called when a new packet has arrived and need to be processed by RDC layer. And functions &amp;#039;&amp;#039;send&amp;#039;&amp;#039; and &amp;#039;&amp;#039;send_list&amp;#039;&amp;#039; are called for transmitting out-going packets.&lt;br /&gt;
&lt;br /&gt;
There are 2 very basic types of RDC layers implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: &amp;#039;&amp;#039;nullrdc-noframer&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc-noframer.c&amp;#039;&amp;#039;) and &amp;#039;&amp;#039;nullrdc&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc.c&amp;#039;&amp;#039;). The first one does not use Framer functions, and transmits/receives data directly to Radio layer (Physical). The developer can take total control over the format of the packets being transmitted. &lt;br /&gt;
&lt;br /&gt;
The second RDC layer (nullrdc) uses the Framer functions for header creation/parsing. It does not save energy and works as a pass-through layer that only transmits a packet and returns the results of such transmission (success or collision).&lt;br /&gt;
&lt;br /&gt;
A third simple RDC layer is &amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/sicslowmac.c&amp;#039;&amp;#039;). This is a RDC layer with no energy savings and that uses IEEE 8021.5.4 frames.&lt;br /&gt;
&lt;br /&gt;
The three last protocols available in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; are implementations of popular asynchronous MAC protocols that aim at energy saving; namely &amp;#039;&amp;#039;&amp;#039;X-MA&amp;#039;&amp;#039;&amp;#039;C, &amp;#039;&amp;#039;&amp;#039;ContikiMac&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;LPP&amp;#039;&amp;#039;&amp;#039;. X-MAC is a short-preamble protocol from 2006 that was ported to ContikiOS [1]. X-MAC has two different implementation in ContikiOS, which can be found in files &amp;#039;&amp;#039;core/net/mac/cxmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/xmac.c&amp;#039;&amp;#039;; the implementation in cxmac.c is a relaxed implementation that does not require stringent timing precision and work on larger number of platforms. ContikiMac [2] is a protocol that proposes enhancement over X-MAC. The implementation can be found in file &amp;#039;&amp;#039;core/net/mac/contikimac.c&amp;#039;&amp;#039;. Finally, LPP (Low Power Probing) is a receiver-initiated low power protocol from 2008 that was also ported to ContikiOS [3]; the code can be found in &amp;#039;&amp;#039;core/net/mac/lpp.c&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Each of the last three RDC protocols can be used for energy saving. The details of each of these protocols is outside the scope of this tutorial and can be found in the references.&lt;br /&gt;
&lt;br /&gt;
== MAC protocols ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The MAC structure, called &amp;#039;&amp;#039;struct mac_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/mac.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a MAC protocol driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct mac_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the MAC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
  &lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are only two types of MAC protocols in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: nullmac (&amp;#039;&amp;#039;core/net/mac/nullmac.c&amp;#039;&amp;#039;) and csma (&amp;#039;&amp;#039;core/net/mac/csma&amp;#039;&amp;#039;). The first one is a simple pass-through protocol that simply calls the appropriate RDC functions. And the second one implements addressing, sequence number and retransmissions. CSMA protocol keep a list of packets to each of the neighbors and calculate statistics such as number of retransmissions, collisions, deferrals, etc.&lt;br /&gt;
&lt;br /&gt;
== How to choose and change your protocols ==&lt;br /&gt;
&lt;br /&gt;
All three stack layers (Framer, RDC and MAC) need to be defined during compilation time. They are define using pragma &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; either in the Makefile or in the &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file. The definitions of which protocols should be used are used by file &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039; that effectively binds the NETSTACK_FRAMER, NETSTACK_RDC and NETSTACK_MAC to the protocols that are supposed to be used.&lt;br /&gt;
&lt;br /&gt;
Below it is shown the piece of code responsible for setting the MAC layer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_NETWORK&lt;br /&gt;
#ifdef NETSTACK_CONF_NETWORK&lt;br /&gt;
#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK&lt;br /&gt;
#else /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#define NETSTACK_NETWORK rime_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#endif /* NETSTACK_NETWORK */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Initially the code checks if NETSTACK_NETWORK was already define (#ifndef NETSTACK_NETWORK). If it was not, the code then checks if NETSTACK_CONF_NETWORK was define. If NETSTACK_CONF_NETWORK was defined, the desired network protocol is used (#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK); otherwise a default protocol is used, which in this case is &amp;#039;&amp;#039;rime_driver&amp;#039;&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
Hence, if you want to use a particular network protocol, you can either define NETSTACK_NETWORK directly or define NETSTACK_CONF_NETWORK.&lt;br /&gt;
&lt;br /&gt;
If no definitions are made, by default ContikiOS stack layers will be:&lt;br /&gt;
&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Network layer&amp;#039;&amp;#039;&amp;#039; - rime_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;MAC layer&amp;#039;&amp;#039;&amp;#039; - nullmac_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;RDC&amp;#039;&amp;#039;&amp;#039; - nullrdc_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039; - framer_nullmac&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Radio&amp;#039;&amp;#039;&amp;#039; - nullradio_driver&lt;br /&gt;
&lt;br /&gt;
The stack layers are usually define inside file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039;. There is one &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file for each platform; for instance, platform &amp;#039;&amp;#039;sky&amp;#039;&amp;#039; has &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; located inside folder &amp;#039;&amp;#039;platform/sky&amp;#039;&amp;#039;. Below we show the excerpt where stack layers are define:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_CONF_MAC&lt;br /&gt;
#define NETSTACK_CONF_MAC     csma_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_MAC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC&lt;br /&gt;
#define NETSTACK_CONF_RDC     contikimac_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE&lt;br /&gt;
#define NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE 8&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RADIO&lt;br /&gt;
#define NETSTACK_CONF_RADIO   cc2420_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RADIO */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_FRAMER&lt;br /&gt;
#define NETSTACK_CONF_FRAMER  framer_802154&lt;br /&gt;
#endif /* NETSTACK_CONF_FRAMER */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can see that, if we are using Tmote Sky hardware, by default the stack layer will be composed by &amp;#039;&amp;#039;csma_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;contikimac_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;framer_802154&amp;#039;&amp;#039; and &amp;#039;&amp;#039;cc2420_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== Changing the protocols ===&lt;br /&gt;
&lt;br /&gt;
If you want to change the protocols to be used you define them either inside the Makefile or creating a project configuration file.&lt;br /&gt;
&lt;br /&gt;
In the first case you should change Makefile, inputing the definitions in one single line, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
DEFINES = NETSTACK_CONF_RDC=cxmac_driver,NETSTACK_CONF_MAC=null_mac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the protocols that you want to use should be defined in the &amp;#039;&amp;#039;DEFINES&amp;#039;&amp;#039; line, using comma to separate them.&lt;br /&gt;
&lt;br /&gt;
The second option is to create a project configuration file. Let&amp;#039;s assume the file create was named &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039;. This file should be inserted into your Makefile, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
CFLAGS += -DPROJECT_CONF_H=\&amp;quot;project-conf.h\&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file you can define all protocols you want to use, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#define NETSTACK_CONF_MAC nullrdc_driver&lt;br /&gt;
#define NETSTACK_CONF_RDC nullmac_driver&lt;br /&gt;
#define NETSTACK_CONF_FRAMER framer_nullmac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color: red&amp;quot;&amp;gt;If you choose to use &amp;#039;&amp;#039;projet-conf.h&amp;#039;&amp;#039; file, you will need to modifed the file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; inside your platform folder. If you are using sky platform, for example, you need to change file &amp;#039;&amp;#039;platform/sky/contiki-conf.h&amp;#039;&amp;#039;. The last part of &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file should be moved to the beginning of the file, as shown below&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* -*- C -*- */&lt;br /&gt;
&lt;br /&gt;
#ifndef CONTIKI_CONF_H&lt;br /&gt;
#define CONTIKI_CONF_H&lt;br /&gt;
&lt;br /&gt;
/* include the project config */&lt;br /&gt;
/* PROJECT_CONF_H might be defined in the project Makefile */&lt;br /&gt;
#ifdef PROJECT_CONF_H&lt;br /&gt;
#include PROJECT_CONF_H&lt;br /&gt;
#endif /* PROJECT_CONF_H */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to code your own protocol ==&lt;br /&gt;
&lt;br /&gt;
We will explain here how to create a new protocol and integrate it into ContikiOS. The protocol can be a Network, MAC, RDC, Framer or Radio; the steps for creating any one of these protocols are similar. We are going to show in details how to create a new MAC protocol, called &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
First step is to create both header and c files for &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039; protocol. Both files need to be created inside &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Our files will be named &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The file &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039; is shown below. In this file we only declare an external variable that will reference our MAC protocols, called &amp;#039;&amp;#039;modifiedmac_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef __MODIFIEDMAC_H__&lt;br /&gt;
#define __MODIFIEDMAC_H__&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;net/mac/mac.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
extern const struct mac_driver modifiedmac_driver;&lt;br /&gt;
&lt;br /&gt;
#endif /* __MODIFIEDMAC_H__ */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An initial version of file &amp;#039;&amp;#039;modifiedrdc.c&amp;#039;&amp;#039; is show below. It only contains the declaration of all functions that must be implemented in a RDC driver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;quot;net/mac/modifiedmac.h&amp;quot;&lt;br /&gt;
#include &amp;quot;net/netstack.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static unsigned short&lt;br /&gt;
channel_check_interval(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
init(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
const struct mac_driver modifiedmac_driver = {&lt;br /&gt;
  &amp;quot;modifiedmac&amp;quot;,&lt;br /&gt;
  init,&lt;br /&gt;
  send_packet,&lt;br /&gt;
  packet_input,&lt;br /&gt;
  on,&lt;br /&gt;
  off,&lt;br /&gt;
  channel_check_interval,&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The next step is to include our source code into ContikiOS make system. We just need to edit the file &amp;#039;&amp;#039;core/net/mac/Makefile.mac&amp;#039;&amp;#039; and include the file &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; in the list of Contiki source files. After editing, &amp;#039;&amp;#039;Makefile.mac&amp;#039;&amp;#039; must look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI_SOURCEFILES += cxmac.c xmac.c nullmac.c lpp.c frame802154.c sicslowmac.c nullrdc.c nullrdc-noframer.c mac.c&lt;br /&gt;
CONTIKI_SOURCEFILES += framer-nullmac.c framer-802154.c csma.c contikimac.c phase.c modifiedmac.c&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Below we show the functions that should be changed in order to make our MAC protocol functional. It is going to work exactly as a Null MAC protocol and we are counting the number of packets received and transmitted.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
uint16_t n_pkts_transmitted = 0;&lt;br /&gt;
uint16_t n_pkts_received = 0;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_transmitted++;&lt;br /&gt;
  NETSTACK_RDC.send(sent, ptr);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_received++;&lt;br /&gt;
  NETSTACK_NETWORK.input();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.on();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.off(keep_radio_on);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Evaluating ContikiMAC and XMAC protocols ==&lt;br /&gt;
&lt;br /&gt;
We are going to evaluate the performance of the two most famous RDC protocols in ContikiOS, the ContikiMAC and X-MAC. We want to evaluate the Packet Reception Rate (PRR), which is the percentage of received packets in an interval; and also the average duty-cycle of sensor nodes. The PRR is related to the maximum throughput the network is able to handle; with lower PRR more packets are dropped and the total amount of data that can be transfered to the sink node is small. Also, the duty-cycle impacts the average power consumption and the lifetime of nodes, since the biggest energy savings are achieved when nodes are sleeping; the lower the duty-cycle the longest will be the node&amp;#039;s lifetime.&lt;br /&gt;
&lt;br /&gt;
We considered the firmware in the folder &amp;#039;&amp;#039;examples/ipv6/rpl-udp/&amp;#039;&amp;#039;, which is a firmware based on IPv6 and RPL for UDP packet transmission. In the same folder there is one simulation file called &amp;#039;&amp;#039;rpl-udp-powertrace.csc&amp;#039;&amp;#039; that is composed of 1 sink node and 30 sensor nodes and traces the energy consumption of all nodes, and also keeps track of PRR values throughout the simulation time. We considered the firmwares &amp;#039;&amp;#039;udp-server.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; and reduced the data transmission interval in &amp;#039;&amp;#039;udl-client.c&amp;#039;&amp;#039; to 1 second. The line below shows where the code of &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; was changed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef PERIOD&lt;br /&gt;
#define PERIOD 1&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By default the simulation is executed with ContikiMAC. We let the simulation run for 5 minutes before extracting the desired statistics. After simulation for ContikiMAC, we changed the RDC protocols, as described before, to X-MAC. And the same statistics were obtained. The figures below show the average duty-cycle of each node for both protocols. The figure on the left shows results of ContikiMAC and the one on the right shows results of X-MAC.&lt;br /&gt;
&lt;br /&gt;
[[File:power_contikimac.png|300px]] [[File:power_xmac.png|300px]]&lt;br /&gt;
&lt;br /&gt;
The PRR statistics were also analysed for all 30 sensor nodes. On average the PRR of ContikiMAC was &amp;#039;&amp;#039;&amp;#039;50.48%&amp;#039;&amp;#039;&amp;#039; and the PRR of X-MAC was &amp;#039;&amp;#039;&amp;#039;30.38%&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[http://www.cse.wustl.edu/~lu/cse521s/Papers/x-mac.pdf 1]- Buettner, Michael, Gary V. Yee, Eric Anderson, and Richard Han. &amp;quot;X-MAC: a short preamble MAC protocol for duty-cycled wireless sensor networks.&amp;quot; In Proceedings of the 4th international conference on Embedded networked sensor systems, pp. 307-320. ACM, 2006.&lt;br /&gt;
&lt;br /&gt;
[http://soda.swedish-ict.se/5128/1/contikimac-report.pdf 2] - Dunkels, Adam. &amp;quot;The contikimac radio duty cycling protocol.&amp;quot; (2011).&lt;br /&gt;
&lt;br /&gt;
[http://web.cs.jhu.edu/~cliang4/public/doc/2008_IPSN_Koala.pdf 3] - Liang, Chieh-Jan Mike, and Andreas Terzis. &amp;quot;Koala: Ultra-low power data retrieval in wireless sensor networks.&amp;quot; In Proceedings of the 7th international conference on Information processing in sensor networks, pp. 421-432. IEEE Computer Society, 2008.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
Edited by: Pedro&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1107</id>
		<title>MAC protocols in ContikiOS</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1107"/>
				<updated>2014-11-08T20:12:24Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* Framers */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This tutorial covers the main features of &amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039; protocols available in &amp;#039;&amp;#039;ContikiOS 2.7&amp;#039;&amp;#039;. Medium Access Control protocols take care of the organization of medium access in wireless networks. They can be interpreted as rules that coordinate when each node is going to transmit/receive packets.&lt;br /&gt;
&lt;br /&gt;
There are a large number of protocols available in the literature, they can usually be classified as contention-based or reservation-based protocols. The first group is based on Carrier Sensing for detecting medium activity and are prone to collisions and lower efficiency; however, they are of easy implementation. The second group is more efficient in terms of throughput and energy, but require precise synchronization and is less adaptable to dynamic traffic. &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; only includes MAC protocols of the first category.&lt;br /&gt;
&lt;br /&gt;
There are about 5 types of MAC layers that can be used in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. They are usually implementation of well-known MAC protocols for &amp;#039;&amp;#039;Wireless Sensor Networks&amp;#039;&amp;#039;; a few of them are implementations of protocols developed exclusively for &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;, but that were based on previous work present in the literature.&lt;br /&gt;
&lt;br /&gt;
== You will learn ==&lt;br /&gt;
&lt;br /&gt;
Here you are going to be to be more familiar with all MAC protocols implemented on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. You are going to understand the organization of MAC protocols, where all files are located and where you should start when you want to modify a protocol. You will also learn the steps you need to take in order to implement your own MAC protocol. And finally, we evaluate two famous MAC protocols: &amp;#039;&amp;#039;&amp;#039;ContikiMAC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;X-MAC&amp;#039;&amp;#039;&amp;#039;, showing a few statistics regarding their performance.&lt;br /&gt;
&lt;br /&gt;
== MAC, RDC and Framer drivers ==&lt;br /&gt;
&lt;br /&gt;
The network stack implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; is a little bit different than the usual 5-layers model typically adopted in TCP/IP. In-between the &amp;#039;&amp;#039;Physical&amp;#039;&amp;#039; and the &amp;#039;&amp;#039;Network&amp;#039;&amp;#039; layers, where usually is located the MAC, we have 3 different layers: &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039;&amp;#039;. The figure below shows the organization of layers in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[File:contiki-layers.jpg|150px|center]]&lt;br /&gt;
&lt;br /&gt;
The network layers can be accessed through the global variables &amp;#039;&amp;#039;&amp;#039;NETSTACK_FRAMER&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;NETSTACK_RDC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;NETSTACK_MAC&amp;#039;&amp;#039;&amp;#039;, which are defined in compilation time. All the variables can be found in &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039;. Every network layer is defined by the pragmas &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; in the beginning of this file.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layer is not a regular layer implementation; it is actually a collection of auxiliary functions that are called for creating a frame with data to be transmited and parsing of data being received. Examples of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; types can be found in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; there are two types of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layers: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-802154.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039; layer takes care of the sleep period of nodes. This is the most important layer because it is the one responsible for deciding exactly when the packets will be transmitted and it is responsible for making sure that the node is awake when packets are to be received. RDC protocols&amp;#039; source codes are also located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Examples of RDC layers that are implemented: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;contikimac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;xmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;lpp.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc-noframer.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Finally, the MAC layer takes care of addressing and retransmission of lost packets. The source files can be located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; we have only two types of MAC layers available: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;csma.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Framers ==&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; consists of a collection of auxiliary functions that are called before transmitting a packet and after their reception. The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; structure, called &amp;#039;&amp;#039;struct framer&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/framer.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
struct framer {&lt;br /&gt;
&lt;br /&gt;
  int (* create)(void);&lt;br /&gt;
  int (* parse)(void);&lt;br /&gt;
&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function &amp;#039;&amp;#039;create&amp;#039;&amp;#039; should create a new frame to be transmitted. While function &amp;#039;&amp;#039;parse&amp;#039;&amp;#039; will be called for parsing a received frame.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-nullmac&amp;#039;&amp;#039; is defined by files &amp;#039;&amp;#039;core/net/mac/framer-nullmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-nullmac.h&amp;#039;&amp;#039;. It should be used together with &amp;#039;&amp;#039;nullmac&amp;#039;&amp;#039; (MAC layer). The auxiliary function simply fill in the 2 fields of &amp;#039;&amp;#039;nullmac_hdr&amp;#039;&amp;#039;, which are: receiver address and sender address. This is the very basic type of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; that can be used on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-802154&amp;#039;&amp;#039; is defined by &amp;#039;&amp;#039;core/net/mac/framer-802154.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-802154.h&amp;#039;&amp;#039;. It creates and parses frames compatible with standard IEEE 802.15.4 (2003). The &amp;#039;&amp;#039;create&amp;#039;&amp;#039; and &amp;#039;&amp;#039;parse&amp;#039;&amp;#039; functions manipulate all header information abstractedly through the &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure. The real work is done by functions located in the file &amp;#039;&amp;#039;core/net/mac/frame802154.c&amp;#039;&amp;#039;, which are responsible for reading all information from receive/transmit buffer and inserting/extracting them appropriately into &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure.&lt;br /&gt;
&lt;br /&gt;
== RDC protocols ==&lt;br /&gt;
&lt;br /&gt;
The RDC structure, called &amp;#039;&amp;#039;struct rdc_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/rdc.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a RDC (radio duty cycling) driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct rdc_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the RDC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet list */&lt;br /&gt;
  void (* send_list)(mac_callback_t sent_callback, void *ptr, struct rdc_buf_list *list);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function input is called when a new packet has arrived and need to be processed by RDC layer. And functions send and send_list are called for transmitting out-going packets.&lt;br /&gt;
&lt;br /&gt;
There are 2 very basic types of RDC layers implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: &amp;#039;&amp;#039;nullrdc-noframer&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc-noframer.c&amp;#039;&amp;#039;) and &amp;#039;&amp;#039;nullrdc&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc.c&amp;#039;&amp;#039;). The first one does not use Framer functions, and transmits/receives data directly to Radio layer (Physical). The developer can take total control over the format of the packets being transmitted. &lt;br /&gt;
&lt;br /&gt;
The second RDC layer (nullrdc) uses the Framer functions for header creation/parsing. It does not save energy and works as a pass-through layer that only transmits a packet and returns the results of such transmission (success or collision).&lt;br /&gt;
&lt;br /&gt;
A third simple RDC layer is &amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/sicslowmac.c&amp;#039;&amp;#039;). This is a RDC layer with no energy savings and that uses IEEE 8021.5.4 frames.&lt;br /&gt;
&lt;br /&gt;
The three last protocols available in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; are implementations of popular asynchronous MAC protocols that aim at energy saving; namely X-MAC, ContikiMac and LPP. X-MAC is a short-preamble protocol from 2006 that was ported to ContikiOS [1]. X-MAC has two different implementation in ContikiOS, which can be found in files &amp;#039;&amp;#039;core/net/mac/cxmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/xmac.c&amp;#039;&amp;#039;. ContikiMac [2] is a protocol that proposes enhancement over X-MAC. The implementation can be found in file &amp;#039;&amp;#039;core/net/mac/contikimac.c&amp;#039;&amp;#039;. Finally, LPP (Low Power Probing) is a low power protocol from 2008 that was also ported to ContikiOS [3]; the code can be found in &amp;#039;&amp;#039;core/net/mac/lpp.c&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Each of the last three RDC protocols can be used for energy saving. The details of each of these protocols is outside the scope of this tutorial and can be found in the references.&lt;br /&gt;
&lt;br /&gt;
== MAC protocols ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The MAC structure, called &amp;#039;&amp;#039;struct mac_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/mac.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a MAC protocol driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct mac_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the MAC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
  &lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are only two types of MAC protocols in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: nullmac (&amp;#039;&amp;#039;core/net/mac/nullmac.c&amp;#039;&amp;#039;) and csma (&amp;#039;&amp;#039;core/net/mac/csma&amp;#039;&amp;#039;). The first one is a simple pass-through protocol that simply calls the appropriate RDC functions. And the second one implements addressing, sequence number and retransmissions. CSMA protocol keep a list of packets to each of the neighbors and calculate statistics such as number of retransmissions, collisions, deferrals, etc.&lt;br /&gt;
&lt;br /&gt;
== How to choose and change your protocols ==&lt;br /&gt;
&lt;br /&gt;
All three stack layers (Framer, RDC and MAC) need to be defined during compilation time. They are define using pragma &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; either in the Makefile or in the &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file. The definitions of which protocols should be used are used by file &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039; that effectively binds the NETSTACK_FRAMER, NETSTACK_RDC and NETSTACK_MAC to the protocols that are supposed to be used.&lt;br /&gt;
&lt;br /&gt;
Below it is shown the piece of code responsible for setting the MAC layer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_NETWORK&lt;br /&gt;
#ifdef NETSTACK_CONF_NETWORK&lt;br /&gt;
#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK&lt;br /&gt;
#else /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#define NETSTACK_NETWORK rime_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#endif /* NETSTACK_NETWORK */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Initially the code checks if NETSTACK_NETWORK was already define (#ifndef NETSTACK_NETWORK). If it was not, the code then checks if NETSTACK_CONF_NETWORK was define. If NETSTACK_CONF_NETWORK was defined, the desired network protocol is used (#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK); otherwise a default protocol is used, which in this case is &amp;#039;&amp;#039;rime_driver&amp;#039;&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
Hence, if you want to use a particular network protocol, you can either define NETSTACK_NETWORK directly or define NETSTACK_CONF_NETWORK.&lt;br /&gt;
&lt;br /&gt;
If no definitions are made, by default ContikiOS stack layers will be:&lt;br /&gt;
&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Network layer&amp;#039;&amp;#039;&amp;#039; - rime_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;MAC layer&amp;#039;&amp;#039;&amp;#039; - nullmac_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;RDC&amp;#039;&amp;#039;&amp;#039; - nullrdc_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039; - framer_nullmac&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Radio&amp;#039;&amp;#039;&amp;#039; - nullradio_driver&lt;br /&gt;
&lt;br /&gt;
The stack layers are usually define inside file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039;. There is one &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file for each platform; for instance, platform &amp;#039;&amp;#039;sky&amp;#039;&amp;#039; has &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; located inside folder &amp;#039;&amp;#039;platform/sky&amp;#039;&amp;#039;. Below we show the excerpt where stack layers are define:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_CONF_MAC&lt;br /&gt;
#define NETSTACK_CONF_MAC     csma_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_MAC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC&lt;br /&gt;
#define NETSTACK_CONF_RDC     contikimac_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE&lt;br /&gt;
#define NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE 8&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RADIO&lt;br /&gt;
#define NETSTACK_CONF_RADIO   cc2420_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RADIO */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_FRAMER&lt;br /&gt;
#define NETSTACK_CONF_FRAMER  framer_802154&lt;br /&gt;
#endif /* NETSTACK_CONF_FRAMER */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can see that, if we are using Tmote Sky hardware, by default the stack layer will be composed by &amp;#039;&amp;#039;csma_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;contikimac_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;framer_802154&amp;#039;&amp;#039; and &amp;#039;&amp;#039;cc2420_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== Changing the protocols ===&lt;br /&gt;
&lt;br /&gt;
If you want to change the protocols to be used you define them either inside the Makefile or creating a project configuration file.&lt;br /&gt;
&lt;br /&gt;
In the first case you should change Makefile, inputing the definitions in one single line, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
DEFINES = NETSTACK_CONF_RDC=cxmac_driver,NETSTACK_CONF_MAC=null_mac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the protocols that you want to use should be defined in the &amp;#039;&amp;#039;DEFINES&amp;#039;&amp;#039; line, using comma to separate them.&lt;br /&gt;
&lt;br /&gt;
The second option is to create a project configuration file. Let&amp;#039;s assume the file create was named &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039;. This file should be inserted into your Makefile, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
CFLAGS += -DPROJECT_CONF_H=\&amp;quot;project-conf.h\&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file you can define all protocols you want to use, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#define NETSTACK_CONF_MAC nullrdc_driver&lt;br /&gt;
#define NETSTACK_CONF_RDC nullmac_driver&lt;br /&gt;
#define NETSTACK_CONF_FRAMER framer_nullmac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color: red&amp;quot;&amp;gt;If you choose to use &amp;#039;&amp;#039;projet-conf.h&amp;#039;&amp;#039; file, you will need to modifed the file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; inside your platform folder. If you are using sky platform, for example, you need to change file &amp;#039;&amp;#039;platform/sky/contiki-conf.h&amp;#039;&amp;#039;. The last part of &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file should be moved to the beginning of the file, as shown below&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* -*- C -*- */&lt;br /&gt;
&lt;br /&gt;
#ifndef CONTIKI_CONF_H&lt;br /&gt;
#define CONTIKI_CONF_H&lt;br /&gt;
&lt;br /&gt;
/* include the project config */&lt;br /&gt;
/* PROJECT_CONF_H might be defined in the project Makefile */&lt;br /&gt;
#ifdef PROJECT_CONF_H&lt;br /&gt;
#include PROJECT_CONF_H&lt;br /&gt;
#endif /* PROJECT_CONF_H */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to code your own protocol ==&lt;br /&gt;
&lt;br /&gt;
We will explain here how to create a new protocol and integrate it into ContikiOS. The protocol can be a Network, MAC, RDC, Framer or Radio; the steps for creating any one of these protocols are similar. We are going to show in details how to create a new MAC protocol, called &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
First step is to create both header and c files for &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039; protocol. Both files need to be created inside &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Our files will be named &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The file &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039; is shown below. In this file we only declare an external variable that will reference our MAC protocols, called &amp;#039;&amp;#039;modifiedmac_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef __MODIFIEDMAC_H__&lt;br /&gt;
#define __MODIFIEDMAC_H__&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;net/mac/mac.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
extern const struct mac_driver modifiedmac_driver;&lt;br /&gt;
&lt;br /&gt;
#endif /* __MODIFIEDMAC_H__ */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An initial version of file &amp;#039;&amp;#039;modifiedrdc.c&amp;#039;&amp;#039; is show below. It only contains the declaration of all functions that must be implemented in a RDC driver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;quot;net/mac/modifiedmac.h&amp;quot;&lt;br /&gt;
#include &amp;quot;net/netstack.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static unsigned short&lt;br /&gt;
channel_check_interval(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
init(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
const struct mac_driver modifiedmac_driver = {&lt;br /&gt;
  &amp;quot;modifiedmac&amp;quot;,&lt;br /&gt;
  init,&lt;br /&gt;
  send_packet,&lt;br /&gt;
  packet_input,&lt;br /&gt;
  on,&lt;br /&gt;
  off,&lt;br /&gt;
  channel_check_interval,&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The next step is to include our source code into ContikiOS make system. We just need to edit the file &amp;#039;&amp;#039;core/net/mac/Makefile.mac&amp;#039;&amp;#039; and include the file &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; in the list of Contiki source files. After editing, &amp;#039;&amp;#039;Makefile.mac&amp;#039;&amp;#039; must look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI_SOURCEFILES += cxmac.c xmac.c nullmac.c lpp.c frame802154.c sicslowmac.c nullrdc.c nullrdc-noframer.c mac.c&lt;br /&gt;
CONTIKI_SOURCEFILES += framer-nullmac.c framer-802154.c csma.c contikimac.c phase.c modifiedmac.c&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Below we show the functions that should be changed in order to make our MAC protocol functional. It is going to work exactly as a Null MAC protocol and we are counting the number of packets received and transmitted.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
uint16_t n_pkts_transmitted = 0;&lt;br /&gt;
uint16_t n_pkts_received = 0;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_transmitted++;&lt;br /&gt;
  NETSTACK_RDC.send(sent, ptr);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_received++;&lt;br /&gt;
  NETSTACK_NETWORK.input();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.on();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.off(keep_radio_on);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Evaluating ContikiMAC and XMAC protocols ==&lt;br /&gt;
&lt;br /&gt;
We are going to evaluate the performance of the two most famous RDC protocols in ContikiOS, the ContikiMAC and X-MAC. We want to evaluate the Packet Reception Rate (PRR), which is the percentage of received packets in an interval; and also the average duty-cycle of sensor nodes. The PRR is related to the maximum throughput the network is able to handle; with lower PRR more packets are dropped and the total amount of data that can be transfered to the sink node is small. Also, the duty-cycle impacts the average power consumption and the lifetime of nodes, since the biggest energy savings are achieved when nodes are sleeping; the lower the duty-cycle the longest will be the node&amp;#039;s lifetime.&lt;br /&gt;
&lt;br /&gt;
We considered the firmware in the folder &amp;#039;&amp;#039;examples/ipv6/rpl-udp/&amp;#039;&amp;#039;, which is a firmware based on IPv6 and RPL for UDP packet transmission. In the same folder there is one simulation file called &amp;#039;&amp;#039;rpl-udp-powertrace.csc&amp;#039;&amp;#039; that is composed of 1 sink node and 30 sensor nodes and traces the energy consumption of all nodes, and also keeps track of PRR values throughout the simulation time. We considered the firmwares &amp;#039;&amp;#039;udp-server.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; and reduced the data transmission interval in &amp;#039;&amp;#039;udl-client.c&amp;#039;&amp;#039; to 1 second. The line below shows where the code of &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; was changed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef PERIOD&lt;br /&gt;
#define PERIOD 1&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By default the simulation is executed with ContikiMAC. We let the simulation run for 5 minutes before extracting the desired statistics. After simulation for ContikiMAC, we changed the RDC protocols, as described before, to X-MAC. And the same statistics were obtained. The figures below show the average duty-cycle of each node for both protocols. The figure on the left shows results of ContikiMAC and the one on the right shows results of X-MAC.&lt;br /&gt;
&lt;br /&gt;
[[File:power_contikimac.png|300px]] [[File:power_xmac.png|300px]]&lt;br /&gt;
&lt;br /&gt;
The PRR statistics were also analysed for all 30 sensor nodes. On average the PRR of ContikiMAC was &amp;#039;&amp;#039;&amp;#039;50.48%&amp;#039;&amp;#039;&amp;#039; and the PRR of X-MAC was &amp;#039;&amp;#039;&amp;#039;30.38%&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[http://www.cse.wustl.edu/~lu/cse521s/Papers/x-mac.pdf 1]- Buettner, Michael, Gary V. Yee, Eric Anderson, and Richard Han. &amp;quot;X-MAC: a short preamble MAC protocol for duty-cycled wireless sensor networks.&amp;quot; In Proceedings of the 4th international conference on Embedded networked sensor systems, pp. 307-320. ACM, 2006.&lt;br /&gt;
&lt;br /&gt;
[http://soda.swedish-ict.se/5128/1/contikimac-report.pdf 2] - Dunkels, Adam. &amp;quot;The contikimac radio duty cycling protocol.&amp;quot; (2011).&lt;br /&gt;
&lt;br /&gt;
[http://web.cs.jhu.edu/~cliang4/public/doc/2008_IPSN_Koala.pdf 3] - Liang, Chieh-Jan Mike, and Andreas Terzis. &amp;quot;Koala: Ultra-low power data retrieval in wireless sensor networks.&amp;quot; In Proceedings of the 7th international conference on Information processing in sensor networks, pp. 421-432. IEEE Computer Society, 2008.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
Edited by: Pedro&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1106</id>
		<title>MAC protocols in ContikiOS</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1106"/>
				<updated>2014-11-08T20:09:17Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* MAC, RDC and Framer drivers */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This tutorial covers the main features of &amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039; protocols available in &amp;#039;&amp;#039;ContikiOS 2.7&amp;#039;&amp;#039;. Medium Access Control protocols take care of the organization of medium access in wireless networks. They can be interpreted as rules that coordinate when each node is going to transmit/receive packets.&lt;br /&gt;
&lt;br /&gt;
There are a large number of protocols available in the literature, they can usually be classified as contention-based or reservation-based protocols. The first group is based on Carrier Sensing for detecting medium activity and are prone to collisions and lower efficiency; however, they are of easy implementation. The second group is more efficient in terms of throughput and energy, but require precise synchronization and is less adaptable to dynamic traffic. &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; only includes MAC protocols of the first category.&lt;br /&gt;
&lt;br /&gt;
There are about 5 types of MAC layers that can be used in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. They are usually implementation of well-known MAC protocols for &amp;#039;&amp;#039;Wireless Sensor Networks&amp;#039;&amp;#039;; a few of them are implementations of protocols developed exclusively for &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;, but that were based on previous work present in the literature.&lt;br /&gt;
&lt;br /&gt;
== You will learn ==&lt;br /&gt;
&lt;br /&gt;
Here you are going to be to be more familiar with all MAC protocols implemented on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. You are going to understand the organization of MAC protocols, where all files are located and where you should start when you want to modify a protocol. You will also learn the steps you need to take in order to implement your own MAC protocol. And finally, we evaluate two famous MAC protocols: &amp;#039;&amp;#039;&amp;#039;ContikiMAC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;X-MAC&amp;#039;&amp;#039;&amp;#039;, showing a few statistics regarding their performance.&lt;br /&gt;
&lt;br /&gt;
== MAC, RDC and Framer drivers ==&lt;br /&gt;
&lt;br /&gt;
The network stack implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; is a little bit different than the usual 5-layers model typically adopted in TCP/IP. In-between the &amp;#039;&amp;#039;Physical&amp;#039;&amp;#039; and the &amp;#039;&amp;#039;Network&amp;#039;&amp;#039; layers, where usually is located the MAC, we have 3 different layers: &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039;&amp;#039;. The figure below shows the organization of layers in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[File:contiki-layers.jpg|150px|center]]&lt;br /&gt;
&lt;br /&gt;
The network layers can be accessed through the global variables &amp;#039;&amp;#039;&amp;#039;NETSTACK_FRAMER&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;NETSTACK_RDC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;NETSTACK_MAC&amp;#039;&amp;#039;&amp;#039;, which are defined in compilation time. All the variables can be found in &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039;. Every network layer is defined by the pragmas &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; in the beginning of this file.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layer is not a regular layer implementation; it is actually a collection of auxiliary functions that are called for creating a frame with data to be transmited and parsing of data being received. Examples of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; types can be found in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; there are two types of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layers: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-802154.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039; layer takes care of the sleep period of nodes. This is the most important layer because it is the one responsible for deciding exactly when the packets will be transmitted and it is responsible for making sure that the node is awake when packets are to be received. RDC protocols&amp;#039; source codes are also located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Examples of RDC layers that are implemented: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;contikimac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;xmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;lpp.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc-noframer.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Finally, the MAC layer takes care of addressing and retransmission of lost packets. The source files can be located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; we have only two types of MAC layers available: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;csma.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Framers ==&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; consists of a collection of auxiliary functions that are called before transmitting a packet and after their reception. The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; structure, called &amp;#039;&amp;#039;struct framer&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/framer.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
struct framer {&lt;br /&gt;
&lt;br /&gt;
  int (* create)(void);&lt;br /&gt;
  int (* parse)(void);&lt;br /&gt;
&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function create should create a new frame to be transmitted. While function parse will be called for parsing a received frame.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-nullmac&amp;#039;&amp;#039; is defined by files &amp;#039;&amp;#039;core/net/mac/framer-nullmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-nullmac.h&amp;#039;&amp;#039;. It should be used together with &amp;#039;&amp;#039;nullmac&amp;#039;&amp;#039; (MAC layer). The auxiliary function simply populate the 2 fields of &amp;#039;&amp;#039;nullmac_hdr&amp;#039;&amp;#039;, which are: receiver address and sender address. This is the very basic type of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; that can be used on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-802154&amp;#039;&amp;#039; is defined by &amp;#039;&amp;#039;core/net/mac/framer-802154.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-802154.h&amp;#039;&amp;#039;. It creates and parses frames compatible with standard IEEE 802.15.4 (2003). The create and parse function manipulate all header information abstractedly through the &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure. The real work is done by functions located in the file &amp;#039;&amp;#039;core/net/mac/frame802154.c&amp;#039;&amp;#039;, which are responsible for extracting all information from receive/transmit buffer and inserting/extracting them appropriately into &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure.&lt;br /&gt;
&lt;br /&gt;
== RDC protocols ==&lt;br /&gt;
&lt;br /&gt;
The RDC structure, called &amp;#039;&amp;#039;struct rdc_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/rdc.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a RDC (radio duty cycling) driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct rdc_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the RDC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet list */&lt;br /&gt;
  void (* send_list)(mac_callback_t sent_callback, void *ptr, struct rdc_buf_list *list);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function input is called when a new packet has arrived and need to be processed by RDC layer. And functions send and send_list are called for transmitting out-going packets.&lt;br /&gt;
&lt;br /&gt;
There are 2 very basic types of RDC layers implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: &amp;#039;&amp;#039;nullrdc-noframer&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc-noframer.c&amp;#039;&amp;#039;) and &amp;#039;&amp;#039;nullrdc&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc.c&amp;#039;&amp;#039;). The first one does not use Framer functions, and transmits/receives data directly to Radio layer (Physical). The developer can take total control over the format of the packets being transmitted. &lt;br /&gt;
&lt;br /&gt;
The second RDC layer (nullrdc) uses the Framer functions for header creation/parsing. It does not save energy and works as a pass-through layer that only transmits a packet and returns the results of such transmission (success or collision).&lt;br /&gt;
&lt;br /&gt;
A third simple RDC layer is &amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/sicslowmac.c&amp;#039;&amp;#039;). This is a RDC layer with no energy savings and that uses IEEE 8021.5.4 frames.&lt;br /&gt;
&lt;br /&gt;
The three last protocols available in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; are implementations of popular asynchronous MAC protocols that aim at energy saving; namely X-MAC, ContikiMac and LPP. X-MAC is a short-preamble protocol from 2006 that was ported to ContikiOS [1]. X-MAC has two different implementation in ContikiOS, which can be found in files &amp;#039;&amp;#039;core/net/mac/cxmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/xmac.c&amp;#039;&amp;#039;. ContikiMac [2] is a protocol that proposes enhancement over X-MAC. The implementation can be found in file &amp;#039;&amp;#039;core/net/mac/contikimac.c&amp;#039;&amp;#039;. Finally, LPP (Low Power Probing) is a low power protocol from 2008 that was also ported to ContikiOS [3]; the code can be found in &amp;#039;&amp;#039;core/net/mac/lpp.c&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Each of the last three RDC protocols can be used for energy saving. The details of each of these protocols is outside the scope of this tutorial and can be found in the references.&lt;br /&gt;
&lt;br /&gt;
== MAC protocols ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The MAC structure, called &amp;#039;&amp;#039;struct mac_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/mac.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a MAC protocol driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct mac_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the MAC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
  &lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are only two types of MAC protocols in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: nullmac (&amp;#039;&amp;#039;core/net/mac/nullmac.c&amp;#039;&amp;#039;) and csma (&amp;#039;&amp;#039;core/net/mac/csma&amp;#039;&amp;#039;). The first one is a simple pass-through protocol that simply calls the appropriate RDC functions. And the second one implements addressing, sequence number and retransmissions. CSMA protocol keep a list of packets to each of the neighbors and calculate statistics such as number of retransmissions, collisions, deferrals, etc.&lt;br /&gt;
&lt;br /&gt;
== How to choose and change your protocols ==&lt;br /&gt;
&lt;br /&gt;
All three stack layers (Framer, RDC and MAC) need to be defined during compilation time. They are define using pragma &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; either in the Makefile or in the &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file. The definitions of which protocols should be used are used by file &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039; that effectively binds the NETSTACK_FRAMER, NETSTACK_RDC and NETSTACK_MAC to the protocols that are supposed to be used.&lt;br /&gt;
&lt;br /&gt;
Below it is shown the piece of code responsible for setting the MAC layer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_NETWORK&lt;br /&gt;
#ifdef NETSTACK_CONF_NETWORK&lt;br /&gt;
#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK&lt;br /&gt;
#else /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#define NETSTACK_NETWORK rime_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#endif /* NETSTACK_NETWORK */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Initially the code checks if NETSTACK_NETWORK was already define (#ifndef NETSTACK_NETWORK). If it was not, the code then checks if NETSTACK_CONF_NETWORK was define. If NETSTACK_CONF_NETWORK was defined, the desired network protocol is used (#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK); otherwise a default protocol is used, which in this case is &amp;#039;&amp;#039;rime_driver&amp;#039;&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
Hence, if you want to use a particular network protocol, you can either define NETSTACK_NETWORK directly or define NETSTACK_CONF_NETWORK.&lt;br /&gt;
&lt;br /&gt;
If no definitions are made, by default ContikiOS stack layers will be:&lt;br /&gt;
&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Network layer&amp;#039;&amp;#039;&amp;#039; - rime_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;MAC layer&amp;#039;&amp;#039;&amp;#039; - nullmac_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;RDC&amp;#039;&amp;#039;&amp;#039; - nullrdc_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039; - framer_nullmac&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Radio&amp;#039;&amp;#039;&amp;#039; - nullradio_driver&lt;br /&gt;
&lt;br /&gt;
The stack layers are usually define inside file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039;. There is one &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file for each platform; for instance, platform &amp;#039;&amp;#039;sky&amp;#039;&amp;#039; has &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; located inside folder &amp;#039;&amp;#039;platform/sky&amp;#039;&amp;#039;. Below we show the excerpt where stack layers are define:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_CONF_MAC&lt;br /&gt;
#define NETSTACK_CONF_MAC     csma_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_MAC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC&lt;br /&gt;
#define NETSTACK_CONF_RDC     contikimac_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE&lt;br /&gt;
#define NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE 8&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RADIO&lt;br /&gt;
#define NETSTACK_CONF_RADIO   cc2420_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RADIO */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_FRAMER&lt;br /&gt;
#define NETSTACK_CONF_FRAMER  framer_802154&lt;br /&gt;
#endif /* NETSTACK_CONF_FRAMER */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can see that, if we are using Tmote Sky hardware, by default the stack layer will be composed by &amp;#039;&amp;#039;csma_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;contikimac_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;framer_802154&amp;#039;&amp;#039; and &amp;#039;&amp;#039;cc2420_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== Changing the protocols ===&lt;br /&gt;
&lt;br /&gt;
If you want to change the protocols to be used you define them either inside the Makefile or creating a project configuration file.&lt;br /&gt;
&lt;br /&gt;
In the first case you should change Makefile, inputing the definitions in one single line, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
DEFINES = NETSTACK_CONF_RDC=cxmac_driver,NETSTACK_CONF_MAC=null_mac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the protocols that you want to use should be defined in the &amp;#039;&amp;#039;DEFINES&amp;#039;&amp;#039; line, using comma to separate them.&lt;br /&gt;
&lt;br /&gt;
The second option is to create a project configuration file. Let&amp;#039;s assume the file create was named &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039;. This file should be inserted into your Makefile, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
CFLAGS += -DPROJECT_CONF_H=\&amp;quot;project-conf.h\&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file you can define all protocols you want to use, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#define NETSTACK_CONF_MAC nullrdc_driver&lt;br /&gt;
#define NETSTACK_CONF_RDC nullmac_driver&lt;br /&gt;
#define NETSTACK_CONF_FRAMER framer_nullmac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color: red&amp;quot;&amp;gt;If you choose to use &amp;#039;&amp;#039;projet-conf.h&amp;#039;&amp;#039; file, you will need to modifed the file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; inside your platform folder. If you are using sky platform, for example, you need to change file &amp;#039;&amp;#039;platform/sky/contiki-conf.h&amp;#039;&amp;#039;. The last part of &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file should be moved to the beginning of the file, as shown below&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* -*- C -*- */&lt;br /&gt;
&lt;br /&gt;
#ifndef CONTIKI_CONF_H&lt;br /&gt;
#define CONTIKI_CONF_H&lt;br /&gt;
&lt;br /&gt;
/* include the project config */&lt;br /&gt;
/* PROJECT_CONF_H might be defined in the project Makefile */&lt;br /&gt;
#ifdef PROJECT_CONF_H&lt;br /&gt;
#include PROJECT_CONF_H&lt;br /&gt;
#endif /* PROJECT_CONF_H */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to code your own protocol ==&lt;br /&gt;
&lt;br /&gt;
We will explain here how to create a new protocol and integrate it into ContikiOS. The protocol can be a Network, MAC, RDC, Framer or Radio; the steps for creating any one of these protocols are similar. We are going to show in details how to create a new MAC protocol, called &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
First step is to create both header and c files for &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039; protocol. Both files need to be created inside &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Our files will be named &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The file &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039; is shown below. In this file we only declare an external variable that will reference our MAC protocols, called &amp;#039;&amp;#039;modifiedmac_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef __MODIFIEDMAC_H__&lt;br /&gt;
#define __MODIFIEDMAC_H__&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;net/mac/mac.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
extern const struct mac_driver modifiedmac_driver;&lt;br /&gt;
&lt;br /&gt;
#endif /* __MODIFIEDMAC_H__ */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An initial version of file &amp;#039;&amp;#039;modifiedrdc.c&amp;#039;&amp;#039; is show below. It only contains the declaration of all functions that must be implemented in a RDC driver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;quot;net/mac/modifiedmac.h&amp;quot;&lt;br /&gt;
#include &amp;quot;net/netstack.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static unsigned short&lt;br /&gt;
channel_check_interval(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
init(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
const struct mac_driver modifiedmac_driver = {&lt;br /&gt;
  &amp;quot;modifiedmac&amp;quot;,&lt;br /&gt;
  init,&lt;br /&gt;
  send_packet,&lt;br /&gt;
  packet_input,&lt;br /&gt;
  on,&lt;br /&gt;
  off,&lt;br /&gt;
  channel_check_interval,&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The next step is to include our source code into ContikiOS make system. We just need to edit the file &amp;#039;&amp;#039;core/net/mac/Makefile.mac&amp;#039;&amp;#039; and include the file &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; in the list of Contiki source files. After editing, &amp;#039;&amp;#039;Makefile.mac&amp;#039;&amp;#039; must look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI_SOURCEFILES += cxmac.c xmac.c nullmac.c lpp.c frame802154.c sicslowmac.c nullrdc.c nullrdc-noframer.c mac.c&lt;br /&gt;
CONTIKI_SOURCEFILES += framer-nullmac.c framer-802154.c csma.c contikimac.c phase.c modifiedmac.c&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Below we show the functions that should be changed in order to make our MAC protocol functional. It is going to work exactly as a Null MAC protocol and we are counting the number of packets received and transmitted.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
uint16_t n_pkts_transmitted = 0;&lt;br /&gt;
uint16_t n_pkts_received = 0;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_transmitted++;&lt;br /&gt;
  NETSTACK_RDC.send(sent, ptr);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_received++;&lt;br /&gt;
  NETSTACK_NETWORK.input();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.on();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.off(keep_radio_on);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Evaluating ContikiMAC and XMAC protocols ==&lt;br /&gt;
&lt;br /&gt;
We are going to evaluate the performance of the two most famous RDC protocols in ContikiOS, the ContikiMAC and X-MAC. We want to evaluate the Packet Reception Rate (PRR), which is the percentage of received packets in an interval; and also the average duty-cycle of sensor nodes. The PRR is related to the maximum throughput the network is able to handle; with lower PRR more packets are dropped and the total amount of data that can be transfered to the sink node is small. Also, the duty-cycle impacts the average power consumption and the lifetime of nodes, since the biggest energy savings are achieved when nodes are sleeping; the lower the duty-cycle the longest will be the node&amp;#039;s lifetime.&lt;br /&gt;
&lt;br /&gt;
We considered the firmware in the folder &amp;#039;&amp;#039;examples/ipv6/rpl-udp/&amp;#039;&amp;#039;, which is a firmware based on IPv6 and RPL for UDP packet transmission. In the same folder there is one simulation file called &amp;#039;&amp;#039;rpl-udp-powertrace.csc&amp;#039;&amp;#039; that is composed of 1 sink node and 30 sensor nodes and traces the energy consumption of all nodes, and also keeps track of PRR values throughout the simulation time. We considered the firmwares &amp;#039;&amp;#039;udp-server.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; and reduced the data transmission interval in &amp;#039;&amp;#039;udl-client.c&amp;#039;&amp;#039; to 1 second. The line below shows where the code of &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; was changed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef PERIOD&lt;br /&gt;
#define PERIOD 1&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By default the simulation is executed with ContikiMAC. We let the simulation run for 5 minutes before extracting the desired statistics. After simulation for ContikiMAC, we changed the RDC protocols, as described before, to X-MAC. And the same statistics were obtained. The figures below show the average duty-cycle of each node for both protocols. The figure on the left shows results of ContikiMAC and the one on the right shows results of X-MAC.&lt;br /&gt;
&lt;br /&gt;
[[File:power_contikimac.png|300px]] [[File:power_xmac.png|300px]]&lt;br /&gt;
&lt;br /&gt;
The PRR statistics were also analysed for all 30 sensor nodes. On average the PRR of ContikiMAC was &amp;#039;&amp;#039;&amp;#039;50.48%&amp;#039;&amp;#039;&amp;#039; and the PRR of X-MAC was &amp;#039;&amp;#039;&amp;#039;30.38%&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[http://www.cse.wustl.edu/~lu/cse521s/Papers/x-mac.pdf 1]- Buettner, Michael, Gary V. Yee, Eric Anderson, and Richard Han. &amp;quot;X-MAC: a short preamble MAC protocol for duty-cycled wireless sensor networks.&amp;quot; In Proceedings of the 4th international conference on Embedded networked sensor systems, pp. 307-320. ACM, 2006.&lt;br /&gt;
&lt;br /&gt;
[http://soda.swedish-ict.se/5128/1/contikimac-report.pdf 2] - Dunkels, Adam. &amp;quot;The contikimac radio duty cycling protocol.&amp;quot; (2011).&lt;br /&gt;
&lt;br /&gt;
[http://web.cs.jhu.edu/~cliang4/public/doc/2008_IPSN_Koala.pdf 3] - Liang, Chieh-Jan Mike, and Andreas Terzis. &amp;quot;Koala: Ultra-low power data retrieval in wireless sensor networks.&amp;quot; In Proceedings of the 7th international conference on Information processing in sensor networks, pp. 421-432. IEEE Computer Society, 2008.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
Edited by: Pedro&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1105</id>
		<title>MAC protocols in ContikiOS</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1105"/>
				<updated>2014-11-08T20:07:00Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* MAC, RDC and Framer drivers */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This tutorial covers the main features of &amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039; protocols available in &amp;#039;&amp;#039;ContikiOS 2.7&amp;#039;&amp;#039;. Medium Access Control protocols take care of the organization of medium access in wireless networks. They can be interpreted as rules that coordinate when each node is going to transmit/receive packets.&lt;br /&gt;
&lt;br /&gt;
There are a large number of protocols available in the literature, they can usually be classified as contention-based or reservation-based protocols. The first group is based on Carrier Sensing for detecting medium activity and are prone to collisions and lower efficiency; however, they are of easy implementation. The second group is more efficient in terms of throughput and energy, but require precise synchronization and is less adaptable to dynamic traffic. &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; only includes MAC protocols of the first category.&lt;br /&gt;
&lt;br /&gt;
There are about 5 types of MAC layers that can be used in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. They are usually implementation of well-known MAC protocols for &amp;#039;&amp;#039;Wireless Sensor Networks&amp;#039;&amp;#039;; a few of them are implementations of protocols developed exclusively for &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;, but that were based on previous work present in the literature.&lt;br /&gt;
&lt;br /&gt;
== You will learn ==&lt;br /&gt;
&lt;br /&gt;
Here you are going to be to be more familiar with all MAC protocols implemented on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. You are going to understand the organization of MAC protocols, where all files are located and where you should start when you want to modify a protocol. You will also learn the steps you need to take in order to implement your own MAC protocol. And finally, we evaluate two famous MAC protocols: &amp;#039;&amp;#039;&amp;#039;ContikiMAC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;X-MAC&amp;#039;&amp;#039;&amp;#039;, showing a few statistics regarding their performance.&lt;br /&gt;
&lt;br /&gt;
== MAC, RDC and Framer drivers ==&lt;br /&gt;
&lt;br /&gt;
The network stack implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; is a little bit different than the usual 5-layers model typically adopted in TCP/IP. In-between the &amp;#039;&amp;#039;Physical&amp;#039;&amp;#039; and the &amp;#039;&amp;#039;Network&amp;#039;&amp;#039; layers, where usually is located the MAC, we have 3 different layers: &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039;&amp;#039;. The figure below shows the organization of layers in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[File:contiki-layers.jpg|150px|center]]&lt;br /&gt;
&lt;br /&gt;
The network layers can be accessed through the global variables &amp;#039;&amp;#039;&amp;#039;NETSTACK_FRAMER&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;NETSTACK_RDC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;NETSTACK_MAC&amp;#039;&amp;#039;&amp;#039;, which are defined in compilation time. All network layers variables can be found in &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039;. Every network layer is defined by the pragmas &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; in the beginning of this file.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layer is not a common layer implementation; it is actually a collection of auxiliary functions that are called for framezation of transmitting data and parsing of data being received. Examples of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; types can be found in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; there are two types of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layers: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-802154.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039; layer takes care of the sleep period of nodes. This is the most important layer because it is the one responsible for deciding exactly when the packets will be transmitted and it is responsible for making sure that the node is awake when packets are to be received. RDC protocols&amp;#039; source codes are also located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Examples of RDC layers that are implemented: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;contikimac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;xmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;lpp.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc-noframer.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Finally, the MAC layer takes care of addressing and retransmission of lost packets. The source files can be located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; we have only two types of MAC layers available: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;csma.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Framers ==&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; consists of a collection of auxiliary functions that are called before transmitting a packet and after their reception. The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; structure, called &amp;#039;&amp;#039;struct framer&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/framer.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
struct framer {&lt;br /&gt;
&lt;br /&gt;
  int (* create)(void);&lt;br /&gt;
  int (* parse)(void);&lt;br /&gt;
&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function create should create a new frame to be transmitted. While function parse will be called for parsing a received frame.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-nullmac&amp;#039;&amp;#039; is defined by files &amp;#039;&amp;#039;core/net/mac/framer-nullmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-nullmac.h&amp;#039;&amp;#039;. It should be used together with &amp;#039;&amp;#039;nullmac&amp;#039;&amp;#039; (MAC layer). The auxiliary function simply populate the 2 fields of &amp;#039;&amp;#039;nullmac_hdr&amp;#039;&amp;#039;, which are: receiver address and sender address. This is the very basic type of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; that can be used on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-802154&amp;#039;&amp;#039; is defined by &amp;#039;&amp;#039;core/net/mac/framer-802154.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-802154.h&amp;#039;&amp;#039;. It creates and parses frames compatible with standard IEEE 802.15.4 (2003). The create and parse function manipulate all header information abstractedly through the &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure. The real work is done by functions located in the file &amp;#039;&amp;#039;core/net/mac/frame802154.c&amp;#039;&amp;#039;, which are responsible for extracting all information from receive/transmit buffer and inserting/extracting them appropriately into &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure.&lt;br /&gt;
&lt;br /&gt;
== RDC protocols ==&lt;br /&gt;
&lt;br /&gt;
The RDC structure, called &amp;#039;&amp;#039;struct rdc_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/rdc.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a RDC (radio duty cycling) driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct rdc_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the RDC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet list */&lt;br /&gt;
  void (* send_list)(mac_callback_t sent_callback, void *ptr, struct rdc_buf_list *list);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function input is called when a new packet has arrived and need to be processed by RDC layer. And functions send and send_list are called for transmitting out-going packets.&lt;br /&gt;
&lt;br /&gt;
There are 2 very basic types of RDC layers implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: &amp;#039;&amp;#039;nullrdc-noframer&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc-noframer.c&amp;#039;&amp;#039;) and &amp;#039;&amp;#039;nullrdc&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc.c&amp;#039;&amp;#039;). The first one does not use Framer functions, and transmits/receives data directly to Radio layer (Physical). The developer can take total control over the format of the packets being transmitted. &lt;br /&gt;
&lt;br /&gt;
The second RDC layer (nullrdc) uses the Framer functions for header creation/parsing. It does not save energy and works as a pass-through layer that only transmits a packet and returns the results of such transmission (success or collision).&lt;br /&gt;
&lt;br /&gt;
A third simple RDC layer is &amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/sicslowmac.c&amp;#039;&amp;#039;). This is a RDC layer with no energy savings and that uses IEEE 8021.5.4 frames.&lt;br /&gt;
&lt;br /&gt;
The three last protocols available in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; are implementations of popular asynchronous MAC protocols that aim at energy saving; namely X-MAC, ContikiMac and LPP. X-MAC is a short-preamble protocol from 2006 that was ported to ContikiOS [1]. X-MAC has two different implementation in ContikiOS, which can be found in files &amp;#039;&amp;#039;core/net/mac/cxmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/xmac.c&amp;#039;&amp;#039;. ContikiMac [2] is a protocol that proposes enhancement over X-MAC. The implementation can be found in file &amp;#039;&amp;#039;core/net/mac/contikimac.c&amp;#039;&amp;#039;. Finally, LPP (Low Power Probing) is a low power protocol from 2008 that was also ported to ContikiOS [3]; the code can be found in &amp;#039;&amp;#039;core/net/mac/lpp.c&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Each of the last three RDC protocols can be used for energy saving. The details of each of these protocols is outside the scope of this tutorial and can be found in the references.&lt;br /&gt;
&lt;br /&gt;
== MAC protocols ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The MAC structure, called &amp;#039;&amp;#039;struct mac_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/mac.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a MAC protocol driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct mac_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the MAC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
  &lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are only two types of MAC protocols in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: nullmac (&amp;#039;&amp;#039;core/net/mac/nullmac.c&amp;#039;&amp;#039;) and csma (&amp;#039;&amp;#039;core/net/mac/csma&amp;#039;&amp;#039;). The first one is a simple pass-through protocol that simply calls the appropriate RDC functions. And the second one implements addressing, sequence number and retransmissions. CSMA protocol keep a list of packets to each of the neighbors and calculate statistics such as number of retransmissions, collisions, deferrals, etc.&lt;br /&gt;
&lt;br /&gt;
== How to choose and change your protocols ==&lt;br /&gt;
&lt;br /&gt;
All three stack layers (Framer, RDC and MAC) need to be defined during compilation time. They are define using pragma &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; either in the Makefile or in the &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file. The definitions of which protocols should be used are used by file &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039; that effectively binds the NETSTACK_FRAMER, NETSTACK_RDC and NETSTACK_MAC to the protocols that are supposed to be used.&lt;br /&gt;
&lt;br /&gt;
Below it is shown the piece of code responsible for setting the MAC layer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_NETWORK&lt;br /&gt;
#ifdef NETSTACK_CONF_NETWORK&lt;br /&gt;
#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK&lt;br /&gt;
#else /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#define NETSTACK_NETWORK rime_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#endif /* NETSTACK_NETWORK */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Initially the code checks if NETSTACK_NETWORK was already define (#ifndef NETSTACK_NETWORK). If it was not, the code then checks if NETSTACK_CONF_NETWORK was define. If NETSTACK_CONF_NETWORK was defined, the desired network protocol is used (#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK); otherwise a default protocol is used, which in this case is &amp;#039;&amp;#039;rime_driver&amp;#039;&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
Hence, if you want to use a particular network protocol, you can either define NETSTACK_NETWORK directly or define NETSTACK_CONF_NETWORK.&lt;br /&gt;
&lt;br /&gt;
If no definitions are made, by default ContikiOS stack layers will be:&lt;br /&gt;
&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Network layer&amp;#039;&amp;#039;&amp;#039; - rime_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;MAC layer&amp;#039;&amp;#039;&amp;#039; - nullmac_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;RDC&amp;#039;&amp;#039;&amp;#039; - nullrdc_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039; - framer_nullmac&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Radio&amp;#039;&amp;#039;&amp;#039; - nullradio_driver&lt;br /&gt;
&lt;br /&gt;
The stack layers are usually define inside file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039;. There is one &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file for each platform; for instance, platform &amp;#039;&amp;#039;sky&amp;#039;&amp;#039; has &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; located inside folder &amp;#039;&amp;#039;platform/sky&amp;#039;&amp;#039;. Below we show the excerpt where stack layers are define:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_CONF_MAC&lt;br /&gt;
#define NETSTACK_CONF_MAC     csma_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_MAC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC&lt;br /&gt;
#define NETSTACK_CONF_RDC     contikimac_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE&lt;br /&gt;
#define NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE 8&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RADIO&lt;br /&gt;
#define NETSTACK_CONF_RADIO   cc2420_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RADIO */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_FRAMER&lt;br /&gt;
#define NETSTACK_CONF_FRAMER  framer_802154&lt;br /&gt;
#endif /* NETSTACK_CONF_FRAMER */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can see that, if we are using Tmote Sky hardware, by default the stack layer will be composed by &amp;#039;&amp;#039;csma_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;contikimac_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;framer_802154&amp;#039;&amp;#039; and &amp;#039;&amp;#039;cc2420_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== Changing the protocols ===&lt;br /&gt;
&lt;br /&gt;
If you want to change the protocols to be used you define them either inside the Makefile or creating a project configuration file.&lt;br /&gt;
&lt;br /&gt;
In the first case you should change Makefile, inputing the definitions in one single line, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
DEFINES = NETSTACK_CONF_RDC=cxmac_driver,NETSTACK_CONF_MAC=null_mac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the protocols that you want to use should be defined in the &amp;#039;&amp;#039;DEFINES&amp;#039;&amp;#039; line, using comma to separate them.&lt;br /&gt;
&lt;br /&gt;
The second option is to create a project configuration file. Let&amp;#039;s assume the file create was named &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039;. This file should be inserted into your Makefile, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
CFLAGS += -DPROJECT_CONF_H=\&amp;quot;project-conf.h\&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file you can define all protocols you want to use, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#define NETSTACK_CONF_MAC nullrdc_driver&lt;br /&gt;
#define NETSTACK_CONF_RDC nullmac_driver&lt;br /&gt;
#define NETSTACK_CONF_FRAMER framer_nullmac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color: red&amp;quot;&amp;gt;If you choose to use &amp;#039;&amp;#039;projet-conf.h&amp;#039;&amp;#039; file, you will need to modifed the file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; inside your platform folder. If you are using sky platform, for example, you need to change file &amp;#039;&amp;#039;platform/sky/contiki-conf.h&amp;#039;&amp;#039;. The last part of &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file should be moved to the beginning of the file, as shown below&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* -*- C -*- */&lt;br /&gt;
&lt;br /&gt;
#ifndef CONTIKI_CONF_H&lt;br /&gt;
#define CONTIKI_CONF_H&lt;br /&gt;
&lt;br /&gt;
/* include the project config */&lt;br /&gt;
/* PROJECT_CONF_H might be defined in the project Makefile */&lt;br /&gt;
#ifdef PROJECT_CONF_H&lt;br /&gt;
#include PROJECT_CONF_H&lt;br /&gt;
#endif /* PROJECT_CONF_H */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to code your own protocol ==&lt;br /&gt;
&lt;br /&gt;
We will explain here how to create a new protocol and integrate it into ContikiOS. The protocol can be a Network, MAC, RDC, Framer or Radio; the steps for creating any one of these protocols are similar. We are going to show in details how to create a new MAC protocol, called &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
First step is to create both header and c files for &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039; protocol. Both files need to be created inside &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Our files will be named &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The file &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039; is shown below. In this file we only declare an external variable that will reference our MAC protocols, called &amp;#039;&amp;#039;modifiedmac_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef __MODIFIEDMAC_H__&lt;br /&gt;
#define __MODIFIEDMAC_H__&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;net/mac/mac.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
extern const struct mac_driver modifiedmac_driver;&lt;br /&gt;
&lt;br /&gt;
#endif /* __MODIFIEDMAC_H__ */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An initial version of file &amp;#039;&amp;#039;modifiedrdc.c&amp;#039;&amp;#039; is show below. It only contains the declaration of all functions that must be implemented in a RDC driver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;quot;net/mac/modifiedmac.h&amp;quot;&lt;br /&gt;
#include &amp;quot;net/netstack.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static unsigned short&lt;br /&gt;
channel_check_interval(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
init(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
const struct mac_driver modifiedmac_driver = {&lt;br /&gt;
  &amp;quot;modifiedmac&amp;quot;,&lt;br /&gt;
  init,&lt;br /&gt;
  send_packet,&lt;br /&gt;
  packet_input,&lt;br /&gt;
  on,&lt;br /&gt;
  off,&lt;br /&gt;
  channel_check_interval,&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The next step is to include our source code into ContikiOS make system. We just need to edit the file &amp;#039;&amp;#039;core/net/mac/Makefile.mac&amp;#039;&amp;#039; and include the file &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; in the list of Contiki source files. After editing, &amp;#039;&amp;#039;Makefile.mac&amp;#039;&amp;#039; must look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI_SOURCEFILES += cxmac.c xmac.c nullmac.c lpp.c frame802154.c sicslowmac.c nullrdc.c nullrdc-noframer.c mac.c&lt;br /&gt;
CONTIKI_SOURCEFILES += framer-nullmac.c framer-802154.c csma.c contikimac.c phase.c modifiedmac.c&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Below we show the functions that should be changed in order to make our MAC protocol functional. It is going to work exactly as a Null MAC protocol and we are counting the number of packets received and transmitted.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
uint16_t n_pkts_transmitted = 0;&lt;br /&gt;
uint16_t n_pkts_received = 0;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_transmitted++;&lt;br /&gt;
  NETSTACK_RDC.send(sent, ptr);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_received++;&lt;br /&gt;
  NETSTACK_NETWORK.input();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.on();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.off(keep_radio_on);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Evaluating ContikiMAC and XMAC protocols ==&lt;br /&gt;
&lt;br /&gt;
We are going to evaluate the performance of the two most famous RDC protocols in ContikiOS, the ContikiMAC and X-MAC. We want to evaluate the Packet Reception Rate (PRR), which is the percentage of received packets in an interval; and also the average duty-cycle of sensor nodes. The PRR is related to the maximum throughput the network is able to handle; with lower PRR more packets are dropped and the total amount of data that can be transfered to the sink node is small. Also, the duty-cycle impacts the average power consumption and the lifetime of nodes, since the biggest energy savings are achieved when nodes are sleeping; the lower the duty-cycle the longest will be the node&amp;#039;s lifetime.&lt;br /&gt;
&lt;br /&gt;
We considered the firmware in the folder &amp;#039;&amp;#039;examples/ipv6/rpl-udp/&amp;#039;&amp;#039;, which is a firmware based on IPv6 and RPL for UDP packet transmission. In the same folder there is one simulation file called &amp;#039;&amp;#039;rpl-udp-powertrace.csc&amp;#039;&amp;#039; that is composed of 1 sink node and 30 sensor nodes and traces the energy consumption of all nodes, and also keeps track of PRR values throughout the simulation time. We considered the firmwares &amp;#039;&amp;#039;udp-server.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; and reduced the data transmission interval in &amp;#039;&amp;#039;udl-client.c&amp;#039;&amp;#039; to 1 second. The line below shows where the code of &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; was changed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef PERIOD&lt;br /&gt;
#define PERIOD 1&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By default the simulation is executed with ContikiMAC. We let the simulation run for 5 minutes before extracting the desired statistics. After simulation for ContikiMAC, we changed the RDC protocols, as described before, to X-MAC. And the same statistics were obtained. The figures below show the average duty-cycle of each node for both protocols. The figure on the left shows results of ContikiMAC and the one on the right shows results of X-MAC.&lt;br /&gt;
&lt;br /&gt;
[[File:power_contikimac.png|300px]] [[File:power_xmac.png|300px]]&lt;br /&gt;
&lt;br /&gt;
The PRR statistics were also analysed for all 30 sensor nodes. On average the PRR of ContikiMAC was &amp;#039;&amp;#039;&amp;#039;50.48%&amp;#039;&amp;#039;&amp;#039; and the PRR of X-MAC was &amp;#039;&amp;#039;&amp;#039;30.38%&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[http://www.cse.wustl.edu/~lu/cse521s/Papers/x-mac.pdf 1]- Buettner, Michael, Gary V. Yee, Eric Anderson, and Richard Han. &amp;quot;X-MAC: a short preamble MAC protocol for duty-cycled wireless sensor networks.&amp;quot; In Proceedings of the 4th international conference on Embedded networked sensor systems, pp. 307-320. ACM, 2006.&lt;br /&gt;
&lt;br /&gt;
[http://soda.swedish-ict.se/5128/1/contikimac-report.pdf 2] - Dunkels, Adam. &amp;quot;The contikimac radio duty cycling protocol.&amp;quot; (2011).&lt;br /&gt;
&lt;br /&gt;
[http://web.cs.jhu.edu/~cliang4/public/doc/2008_IPSN_Koala.pdf 3] - Liang, Chieh-Jan Mike, and Andreas Terzis. &amp;quot;Koala: Ultra-low power data retrieval in wireless sensor networks.&amp;quot; In Proceedings of the 7th international conference on Information processing in sensor networks, pp. 421-432. IEEE Computer Society, 2008.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
Edited by: Pedro&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1104</id>
		<title>MAC protocols in ContikiOS</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1104"/>
				<updated>2014-11-08T20:05:50Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* Evaluating ContikiMAC and XMAC protocols */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This tutorial covers the main features of &amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039; protocols available in &amp;#039;&amp;#039;ContikiOS 2.7&amp;#039;&amp;#039;. Medium Access Control protocols take care of the organization of medium access in wireless networks. They can be interpreted as rules that coordinate when each node is going to transmit/receive packets.&lt;br /&gt;
&lt;br /&gt;
There are a large number of protocols available in the literature, they can usually be classified as contention-based or reservation-based protocols. The first group is based on Carrier Sensing for detecting medium activity and are prone to collisions and lower efficiency; however, they are of easy implementation. The second group is more efficient in terms of throughput and energy, but require precise synchronization and is less adaptable to dynamic traffic. &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; only includes MAC protocols of the first category.&lt;br /&gt;
&lt;br /&gt;
There are about 5 types of MAC layers that can be used in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. They are usually implementation of well-known MAC protocols for &amp;#039;&amp;#039;Wireless Sensor Networks&amp;#039;&amp;#039;; a few of them are implementations of protocols developed exclusively for &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;, but that were based on previous work present in the literature.&lt;br /&gt;
&lt;br /&gt;
== You will learn ==&lt;br /&gt;
&lt;br /&gt;
Here you are going to be to be more familiar with all MAC protocols implemented on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. You are going to understand the organization of MAC protocols, where all files are located and where you should start when you want to modify a protocol. You will also learn the steps you need to take in order to implement your own MAC protocol. And finally, we evaluate two famous MAC protocols: &amp;#039;&amp;#039;&amp;#039;ContikiMAC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;X-MAC&amp;#039;&amp;#039;&amp;#039;, showing a few statistics regarding their performance.&lt;br /&gt;
&lt;br /&gt;
== MAC, RDC and Framer drivers ==&lt;br /&gt;
&lt;br /&gt;
The network stack implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; is a little bit different than the usual 5-layers model typically adopted in TCP/IP. In-between the &amp;#039;&amp;#039;Physical&amp;#039;&amp;#039; and the &amp;#039;&amp;#039;Network&amp;#039;&amp;#039; layers, where usually is located the MAC, we have 3 different layers: &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039;&amp;#039;. The figure below shows the organization of layers in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[File:contiki-layers.jpg|150px|center]]&lt;br /&gt;
&lt;br /&gt;
The network layers can be accessed through the global variables NETSTACK_FRAMER, NETSTACK_RDC and NETSTACK_MAC, which are defined in compilation time. All network layers variables can be found in &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039;. Every network layer is defined by the pragmas &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; in the beginning of this file.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layer is not a common layer implementation; it is actually a collection of auxiliary functions that are called for framezation of transmitting data and parsing of data being received. Examples of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; types can be found in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; there are two types of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layers: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-802154.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039; layer takes care of the sleep period of nodes. This is the most important layer because it is the one responsible for deciding exactly when the packets will be transmitted and it is responsible for making sure that the node is awake when packets are to be received. RDC protocols&amp;#039; source codes are also located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Examples of RDC layers that are implemented: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;contikimac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;xmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;lpp.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc-noframer.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Finally, the MAC layer takes care of addressing and retransmission of lost packets. The source files can be located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; we have only two types of MAC layers available: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;csma.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Framers ==&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; consists of a collection of auxiliary functions that are called before transmitting a packet and after their reception. The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; structure, called &amp;#039;&amp;#039;struct framer&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/framer.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
struct framer {&lt;br /&gt;
&lt;br /&gt;
  int (* create)(void);&lt;br /&gt;
  int (* parse)(void);&lt;br /&gt;
&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function create should create a new frame to be transmitted. While function parse will be called for parsing a received frame.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-nullmac&amp;#039;&amp;#039; is defined by files &amp;#039;&amp;#039;core/net/mac/framer-nullmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-nullmac.h&amp;#039;&amp;#039;. It should be used together with &amp;#039;&amp;#039;nullmac&amp;#039;&amp;#039; (MAC layer). The auxiliary function simply populate the 2 fields of &amp;#039;&amp;#039;nullmac_hdr&amp;#039;&amp;#039;, which are: receiver address and sender address. This is the very basic type of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; that can be used on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-802154&amp;#039;&amp;#039; is defined by &amp;#039;&amp;#039;core/net/mac/framer-802154.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-802154.h&amp;#039;&amp;#039;. It creates and parses frames compatible with standard IEEE 802.15.4 (2003). The create and parse function manipulate all header information abstractedly through the &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure. The real work is done by functions located in the file &amp;#039;&amp;#039;core/net/mac/frame802154.c&amp;#039;&amp;#039;, which are responsible for extracting all information from receive/transmit buffer and inserting/extracting them appropriately into &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure.&lt;br /&gt;
&lt;br /&gt;
== RDC protocols ==&lt;br /&gt;
&lt;br /&gt;
The RDC structure, called &amp;#039;&amp;#039;struct rdc_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/rdc.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a RDC (radio duty cycling) driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct rdc_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the RDC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet list */&lt;br /&gt;
  void (* send_list)(mac_callback_t sent_callback, void *ptr, struct rdc_buf_list *list);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function input is called when a new packet has arrived and need to be processed by RDC layer. And functions send and send_list are called for transmitting out-going packets.&lt;br /&gt;
&lt;br /&gt;
There are 2 very basic types of RDC layers implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: &amp;#039;&amp;#039;nullrdc-noframer&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc-noframer.c&amp;#039;&amp;#039;) and &amp;#039;&amp;#039;nullrdc&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc.c&amp;#039;&amp;#039;). The first one does not use Framer functions, and transmits/receives data directly to Radio layer (Physical). The developer can take total control over the format of the packets being transmitted. &lt;br /&gt;
&lt;br /&gt;
The second RDC layer (nullrdc) uses the Framer functions for header creation/parsing. It does not save energy and works as a pass-through layer that only transmits a packet and returns the results of such transmission (success or collision).&lt;br /&gt;
&lt;br /&gt;
A third simple RDC layer is &amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/sicslowmac.c&amp;#039;&amp;#039;). This is a RDC layer with no energy savings and that uses IEEE 8021.5.4 frames.&lt;br /&gt;
&lt;br /&gt;
The three last protocols available in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; are implementations of popular asynchronous MAC protocols that aim at energy saving; namely X-MAC, ContikiMac and LPP. X-MAC is a short-preamble protocol from 2006 that was ported to ContikiOS [1]. X-MAC has two different implementation in ContikiOS, which can be found in files &amp;#039;&amp;#039;core/net/mac/cxmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/xmac.c&amp;#039;&amp;#039;. ContikiMac [2] is a protocol that proposes enhancement over X-MAC. The implementation can be found in file &amp;#039;&amp;#039;core/net/mac/contikimac.c&amp;#039;&amp;#039;. Finally, LPP (Low Power Probing) is a low power protocol from 2008 that was also ported to ContikiOS [3]; the code can be found in &amp;#039;&amp;#039;core/net/mac/lpp.c&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Each of the last three RDC protocols can be used for energy saving. The details of each of these protocols is outside the scope of this tutorial and can be found in the references.&lt;br /&gt;
&lt;br /&gt;
== MAC protocols ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The MAC structure, called &amp;#039;&amp;#039;struct mac_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/mac.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a MAC protocol driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct mac_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the MAC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
  &lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are only two types of MAC protocols in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: nullmac (&amp;#039;&amp;#039;core/net/mac/nullmac.c&amp;#039;&amp;#039;) and csma (&amp;#039;&amp;#039;core/net/mac/csma&amp;#039;&amp;#039;). The first one is a simple pass-through protocol that simply calls the appropriate RDC functions. And the second one implements addressing, sequence number and retransmissions. CSMA protocol keep a list of packets to each of the neighbors and calculate statistics such as number of retransmissions, collisions, deferrals, etc.&lt;br /&gt;
&lt;br /&gt;
== How to choose and change your protocols ==&lt;br /&gt;
&lt;br /&gt;
All three stack layers (Framer, RDC and MAC) need to be defined during compilation time. They are define using pragma &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; either in the Makefile or in the &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file. The definitions of which protocols should be used are used by file &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039; that effectively binds the NETSTACK_FRAMER, NETSTACK_RDC and NETSTACK_MAC to the protocols that are supposed to be used.&lt;br /&gt;
&lt;br /&gt;
Below it is shown the piece of code responsible for setting the MAC layer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_NETWORK&lt;br /&gt;
#ifdef NETSTACK_CONF_NETWORK&lt;br /&gt;
#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK&lt;br /&gt;
#else /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#define NETSTACK_NETWORK rime_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#endif /* NETSTACK_NETWORK */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Initially the code checks if NETSTACK_NETWORK was already define (#ifndef NETSTACK_NETWORK). If it was not, the code then checks if NETSTACK_CONF_NETWORK was define. If NETSTACK_CONF_NETWORK was defined, the desired network protocol is used (#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK); otherwise a default protocol is used, which in this case is &amp;#039;&amp;#039;rime_driver&amp;#039;&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
Hence, if you want to use a particular network protocol, you can either define NETSTACK_NETWORK directly or define NETSTACK_CONF_NETWORK.&lt;br /&gt;
&lt;br /&gt;
If no definitions are made, by default ContikiOS stack layers will be:&lt;br /&gt;
&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Network layer&amp;#039;&amp;#039;&amp;#039; - rime_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;MAC layer&amp;#039;&amp;#039;&amp;#039; - nullmac_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;RDC&amp;#039;&amp;#039;&amp;#039; - nullrdc_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039; - framer_nullmac&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Radio&amp;#039;&amp;#039;&amp;#039; - nullradio_driver&lt;br /&gt;
&lt;br /&gt;
The stack layers are usually define inside file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039;. There is one &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file for each platform; for instance, platform &amp;#039;&amp;#039;sky&amp;#039;&amp;#039; has &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; located inside folder &amp;#039;&amp;#039;platform/sky&amp;#039;&amp;#039;. Below we show the excerpt where stack layers are define:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_CONF_MAC&lt;br /&gt;
#define NETSTACK_CONF_MAC     csma_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_MAC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC&lt;br /&gt;
#define NETSTACK_CONF_RDC     contikimac_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE&lt;br /&gt;
#define NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE 8&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RADIO&lt;br /&gt;
#define NETSTACK_CONF_RADIO   cc2420_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RADIO */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_FRAMER&lt;br /&gt;
#define NETSTACK_CONF_FRAMER  framer_802154&lt;br /&gt;
#endif /* NETSTACK_CONF_FRAMER */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can see that, if we are using Tmote Sky hardware, by default the stack layer will be composed by &amp;#039;&amp;#039;csma_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;contikimac_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;framer_802154&amp;#039;&amp;#039; and &amp;#039;&amp;#039;cc2420_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== Changing the protocols ===&lt;br /&gt;
&lt;br /&gt;
If you want to change the protocols to be used you define them either inside the Makefile or creating a project configuration file.&lt;br /&gt;
&lt;br /&gt;
In the first case you should change Makefile, inputing the definitions in one single line, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
DEFINES = NETSTACK_CONF_RDC=cxmac_driver,NETSTACK_CONF_MAC=null_mac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the protocols that you want to use should be defined in the &amp;#039;&amp;#039;DEFINES&amp;#039;&amp;#039; line, using comma to separate them.&lt;br /&gt;
&lt;br /&gt;
The second option is to create a project configuration file. Let&amp;#039;s assume the file create was named &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039;. This file should be inserted into your Makefile, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
CFLAGS += -DPROJECT_CONF_H=\&amp;quot;project-conf.h\&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file you can define all protocols you want to use, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#define NETSTACK_CONF_MAC nullrdc_driver&lt;br /&gt;
#define NETSTACK_CONF_RDC nullmac_driver&lt;br /&gt;
#define NETSTACK_CONF_FRAMER framer_nullmac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color: red&amp;quot;&amp;gt;If you choose to use &amp;#039;&amp;#039;projet-conf.h&amp;#039;&amp;#039; file, you will need to modifed the file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; inside your platform folder. If you are using sky platform, for example, you need to change file &amp;#039;&amp;#039;platform/sky/contiki-conf.h&amp;#039;&amp;#039;. The last part of &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file should be moved to the beginning of the file, as shown below&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* -*- C -*- */&lt;br /&gt;
&lt;br /&gt;
#ifndef CONTIKI_CONF_H&lt;br /&gt;
#define CONTIKI_CONF_H&lt;br /&gt;
&lt;br /&gt;
/* include the project config */&lt;br /&gt;
/* PROJECT_CONF_H might be defined in the project Makefile */&lt;br /&gt;
#ifdef PROJECT_CONF_H&lt;br /&gt;
#include PROJECT_CONF_H&lt;br /&gt;
#endif /* PROJECT_CONF_H */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to code your own protocol ==&lt;br /&gt;
&lt;br /&gt;
We will explain here how to create a new protocol and integrate it into ContikiOS. The protocol can be a Network, MAC, RDC, Framer or Radio; the steps for creating any one of these protocols are similar. We are going to show in details how to create a new MAC protocol, called &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
First step is to create both header and c files for &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039; protocol. Both files need to be created inside &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Our files will be named &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The file &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039; is shown below. In this file we only declare an external variable that will reference our MAC protocols, called &amp;#039;&amp;#039;modifiedmac_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef __MODIFIEDMAC_H__&lt;br /&gt;
#define __MODIFIEDMAC_H__&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;net/mac/mac.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
extern const struct mac_driver modifiedmac_driver;&lt;br /&gt;
&lt;br /&gt;
#endif /* __MODIFIEDMAC_H__ */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An initial version of file &amp;#039;&amp;#039;modifiedrdc.c&amp;#039;&amp;#039; is show below. It only contains the declaration of all functions that must be implemented in a RDC driver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;quot;net/mac/modifiedmac.h&amp;quot;&lt;br /&gt;
#include &amp;quot;net/netstack.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static unsigned short&lt;br /&gt;
channel_check_interval(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
init(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
const struct mac_driver modifiedmac_driver = {&lt;br /&gt;
  &amp;quot;modifiedmac&amp;quot;,&lt;br /&gt;
  init,&lt;br /&gt;
  send_packet,&lt;br /&gt;
  packet_input,&lt;br /&gt;
  on,&lt;br /&gt;
  off,&lt;br /&gt;
  channel_check_interval,&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The next step is to include our source code into ContikiOS make system. We just need to edit the file &amp;#039;&amp;#039;core/net/mac/Makefile.mac&amp;#039;&amp;#039; and include the file &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; in the list of Contiki source files. After editing, &amp;#039;&amp;#039;Makefile.mac&amp;#039;&amp;#039; must look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI_SOURCEFILES += cxmac.c xmac.c nullmac.c lpp.c frame802154.c sicslowmac.c nullrdc.c nullrdc-noframer.c mac.c&lt;br /&gt;
CONTIKI_SOURCEFILES += framer-nullmac.c framer-802154.c csma.c contikimac.c phase.c modifiedmac.c&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Below we show the functions that should be changed in order to make our MAC protocol functional. It is going to work exactly as a Null MAC protocol and we are counting the number of packets received and transmitted.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
uint16_t n_pkts_transmitted = 0;&lt;br /&gt;
uint16_t n_pkts_received = 0;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_transmitted++;&lt;br /&gt;
  NETSTACK_RDC.send(sent, ptr);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_received++;&lt;br /&gt;
  NETSTACK_NETWORK.input();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.on();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.off(keep_radio_on);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Evaluating ContikiMAC and XMAC protocols ==&lt;br /&gt;
&lt;br /&gt;
We are going to evaluate the performance of the two most famous RDC protocols in ContikiOS, the ContikiMAC and X-MAC. We want to evaluate the Packet Reception Rate (PRR), which is the percentage of received packets in an interval; and also the average duty-cycle of sensor nodes. The PRR is related to the maximum throughput the network is able to handle; with lower PRR more packets are dropped and the total amount of data that can be transfered to the sink node is small. Also, the duty-cycle impacts the average power consumption and the lifetime of nodes, since the biggest energy savings are achieved when nodes are sleeping; the lower the duty-cycle the longest will be the node&amp;#039;s lifetime.&lt;br /&gt;
&lt;br /&gt;
We considered the firmware in the folder &amp;#039;&amp;#039;examples/ipv6/rpl-udp/&amp;#039;&amp;#039;, which is a firmware based on IPv6 and RPL for UDP packet transmission. In the same folder there is one simulation file called &amp;#039;&amp;#039;rpl-udp-powertrace.csc&amp;#039;&amp;#039; that is composed of 1 sink node and 30 sensor nodes and traces the energy consumption of all nodes, and also keeps track of PRR values throughout the simulation time. We considered the firmwares &amp;#039;&amp;#039;udp-server.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; and reduced the data transmission interval in &amp;#039;&amp;#039;udl-client.c&amp;#039;&amp;#039; to 1 second. The line below shows where the code of &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; was changed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef PERIOD&lt;br /&gt;
#define PERIOD 1&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By default the simulation is executed with ContikiMAC. We let the simulation run for 5 minutes before extracting the desired statistics. After simulation for ContikiMAC, we changed the RDC protocols, as described before, to X-MAC. And the same statistics were obtained. The figures below show the average duty-cycle of each node for both protocols. The figure on the left shows results of ContikiMAC and the one on the right shows results of X-MAC.&lt;br /&gt;
&lt;br /&gt;
[[File:power_contikimac.png|300px]] [[File:power_xmac.png|300px]]&lt;br /&gt;
&lt;br /&gt;
The PRR statistics were also analysed for all 30 sensor nodes. On average the PRR of ContikiMAC was &amp;#039;&amp;#039;&amp;#039;50.48%&amp;#039;&amp;#039;&amp;#039; and the PRR of X-MAC was &amp;#039;&amp;#039;&amp;#039;30.38%&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[http://www.cse.wustl.edu/~lu/cse521s/Papers/x-mac.pdf 1]- Buettner, Michael, Gary V. Yee, Eric Anderson, and Richard Han. &amp;quot;X-MAC: a short preamble MAC protocol for duty-cycled wireless sensor networks.&amp;quot; In Proceedings of the 4th international conference on Embedded networked sensor systems, pp. 307-320. ACM, 2006.&lt;br /&gt;
&lt;br /&gt;
[http://soda.swedish-ict.se/5128/1/contikimac-report.pdf 2] - Dunkels, Adam. &amp;quot;The contikimac radio duty cycling protocol.&amp;quot; (2011).&lt;br /&gt;
&lt;br /&gt;
[http://web.cs.jhu.edu/~cliang4/public/doc/2008_IPSN_Koala.pdf 3] - Liang, Chieh-Jan Mike, and Andreas Terzis. &amp;quot;Koala: Ultra-low power data retrieval in wireless sensor networks.&amp;quot; In Proceedings of the 7th international conference on Information processing in sensor networks, pp. 421-432. IEEE Computer Society, 2008.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
Edited by: Pedro&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1103</id>
		<title>MAC protocols in ContikiOS</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1103"/>
				<updated>2014-11-08T20:05:22Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* How to code your own protocol */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This tutorial covers the main features of &amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039; protocols available in &amp;#039;&amp;#039;ContikiOS 2.7&amp;#039;&amp;#039;. Medium Access Control protocols take care of the organization of medium access in wireless networks. They can be interpreted as rules that coordinate when each node is going to transmit/receive packets.&lt;br /&gt;
&lt;br /&gt;
There are a large number of protocols available in the literature, they can usually be classified as contention-based or reservation-based protocols. The first group is based on Carrier Sensing for detecting medium activity and are prone to collisions and lower efficiency; however, they are of easy implementation. The second group is more efficient in terms of throughput and energy, but require precise synchronization and is less adaptable to dynamic traffic. &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; only includes MAC protocols of the first category.&lt;br /&gt;
&lt;br /&gt;
There are about 5 types of MAC layers that can be used in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. They are usually implementation of well-known MAC protocols for &amp;#039;&amp;#039;Wireless Sensor Networks&amp;#039;&amp;#039;; a few of them are implementations of protocols developed exclusively for &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;, but that were based on previous work present in the literature.&lt;br /&gt;
&lt;br /&gt;
== You will learn ==&lt;br /&gt;
&lt;br /&gt;
Here you are going to be to be more familiar with all MAC protocols implemented on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. You are going to understand the organization of MAC protocols, where all files are located and where you should start when you want to modify a protocol. You will also learn the steps you need to take in order to implement your own MAC protocol. And finally, we evaluate two famous MAC protocols: &amp;#039;&amp;#039;&amp;#039;ContikiMAC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;X-MAC&amp;#039;&amp;#039;&amp;#039;, showing a few statistics regarding their performance.&lt;br /&gt;
&lt;br /&gt;
== MAC, RDC and Framer drivers ==&lt;br /&gt;
&lt;br /&gt;
The network stack implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; is a little bit different than the usual 5-layers model typically adopted in TCP/IP. In-between the &amp;#039;&amp;#039;Physical&amp;#039;&amp;#039; and the &amp;#039;&amp;#039;Network&amp;#039;&amp;#039; layers, where usually is located the MAC, we have 3 different layers: &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039;&amp;#039;. The figure below shows the organization of layers in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[File:contiki-layers.jpg|150px|center]]&lt;br /&gt;
&lt;br /&gt;
The network layers can be accessed through the global variables NETSTACK_FRAMER, NETSTACK_RDC and NETSTACK_MAC, which are defined in compilation time. All network layers variables can be found in &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039;. Every network layer is defined by the pragmas &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; in the beginning of this file.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layer is not a common layer implementation; it is actually a collection of auxiliary functions that are called for framezation of transmitting data and parsing of data being received. Examples of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; types can be found in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; there are two types of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layers: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-802154.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039; layer takes care of the sleep period of nodes. This is the most important layer because it is the one responsible for deciding exactly when the packets will be transmitted and it is responsible for making sure that the node is awake when packets are to be received. RDC protocols&amp;#039; source codes are also located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Examples of RDC layers that are implemented: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;contikimac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;xmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;lpp.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc-noframer.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Finally, the MAC layer takes care of addressing and retransmission of lost packets. The source files can be located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; we have only two types of MAC layers available: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;csma.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Framers ==&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; consists of a collection of auxiliary functions that are called before transmitting a packet and after their reception. The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; structure, called &amp;#039;&amp;#039;struct framer&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/framer.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
struct framer {&lt;br /&gt;
&lt;br /&gt;
  int (* create)(void);&lt;br /&gt;
  int (* parse)(void);&lt;br /&gt;
&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function create should create a new frame to be transmitted. While function parse will be called for parsing a received frame.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-nullmac&amp;#039;&amp;#039; is defined by files &amp;#039;&amp;#039;core/net/mac/framer-nullmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-nullmac.h&amp;#039;&amp;#039;. It should be used together with &amp;#039;&amp;#039;nullmac&amp;#039;&amp;#039; (MAC layer). The auxiliary function simply populate the 2 fields of &amp;#039;&amp;#039;nullmac_hdr&amp;#039;&amp;#039;, which are: receiver address and sender address. This is the very basic type of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; that can be used on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-802154&amp;#039;&amp;#039; is defined by &amp;#039;&amp;#039;core/net/mac/framer-802154.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-802154.h&amp;#039;&amp;#039;. It creates and parses frames compatible with standard IEEE 802.15.4 (2003). The create and parse function manipulate all header information abstractedly through the &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure. The real work is done by functions located in the file &amp;#039;&amp;#039;core/net/mac/frame802154.c&amp;#039;&amp;#039;, which are responsible for extracting all information from receive/transmit buffer and inserting/extracting them appropriately into &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure.&lt;br /&gt;
&lt;br /&gt;
== RDC protocols ==&lt;br /&gt;
&lt;br /&gt;
The RDC structure, called &amp;#039;&amp;#039;struct rdc_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/rdc.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a RDC (radio duty cycling) driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct rdc_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the RDC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet list */&lt;br /&gt;
  void (* send_list)(mac_callback_t sent_callback, void *ptr, struct rdc_buf_list *list);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function input is called when a new packet has arrived and need to be processed by RDC layer. And functions send and send_list are called for transmitting out-going packets.&lt;br /&gt;
&lt;br /&gt;
There are 2 very basic types of RDC layers implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: &amp;#039;&amp;#039;nullrdc-noframer&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc-noframer.c&amp;#039;&amp;#039;) and &amp;#039;&amp;#039;nullrdc&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc.c&amp;#039;&amp;#039;). The first one does not use Framer functions, and transmits/receives data directly to Radio layer (Physical). The developer can take total control over the format of the packets being transmitted. &lt;br /&gt;
&lt;br /&gt;
The second RDC layer (nullrdc) uses the Framer functions for header creation/parsing. It does not save energy and works as a pass-through layer that only transmits a packet and returns the results of such transmission (success or collision).&lt;br /&gt;
&lt;br /&gt;
A third simple RDC layer is &amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/sicslowmac.c&amp;#039;&amp;#039;). This is a RDC layer with no energy savings and that uses IEEE 8021.5.4 frames.&lt;br /&gt;
&lt;br /&gt;
The three last protocols available in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; are implementations of popular asynchronous MAC protocols that aim at energy saving; namely X-MAC, ContikiMac and LPP. X-MAC is a short-preamble protocol from 2006 that was ported to ContikiOS [1]. X-MAC has two different implementation in ContikiOS, which can be found in files &amp;#039;&amp;#039;core/net/mac/cxmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/xmac.c&amp;#039;&amp;#039;. ContikiMac [2] is a protocol that proposes enhancement over X-MAC. The implementation can be found in file &amp;#039;&amp;#039;core/net/mac/contikimac.c&amp;#039;&amp;#039;. Finally, LPP (Low Power Probing) is a low power protocol from 2008 that was also ported to ContikiOS [3]; the code can be found in &amp;#039;&amp;#039;core/net/mac/lpp.c&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Each of the last three RDC protocols can be used for energy saving. The details of each of these protocols is outside the scope of this tutorial and can be found in the references.&lt;br /&gt;
&lt;br /&gt;
== MAC protocols ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The MAC structure, called &amp;#039;&amp;#039;struct mac_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/mac.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a MAC protocol driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct mac_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the MAC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
  &lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are only two types of MAC protocols in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: nullmac (&amp;#039;&amp;#039;core/net/mac/nullmac.c&amp;#039;&amp;#039;) and csma (&amp;#039;&amp;#039;core/net/mac/csma&amp;#039;&amp;#039;). The first one is a simple pass-through protocol that simply calls the appropriate RDC functions. And the second one implements addressing, sequence number and retransmissions. CSMA protocol keep a list of packets to each of the neighbors and calculate statistics such as number of retransmissions, collisions, deferrals, etc.&lt;br /&gt;
&lt;br /&gt;
== How to choose and change your protocols ==&lt;br /&gt;
&lt;br /&gt;
All three stack layers (Framer, RDC and MAC) need to be defined during compilation time. They are define using pragma &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; either in the Makefile or in the &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file. The definitions of which protocols should be used are used by file &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039; that effectively binds the NETSTACK_FRAMER, NETSTACK_RDC and NETSTACK_MAC to the protocols that are supposed to be used.&lt;br /&gt;
&lt;br /&gt;
Below it is shown the piece of code responsible for setting the MAC layer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_NETWORK&lt;br /&gt;
#ifdef NETSTACK_CONF_NETWORK&lt;br /&gt;
#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK&lt;br /&gt;
#else /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#define NETSTACK_NETWORK rime_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#endif /* NETSTACK_NETWORK */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Initially the code checks if NETSTACK_NETWORK was already define (#ifndef NETSTACK_NETWORK). If it was not, the code then checks if NETSTACK_CONF_NETWORK was define. If NETSTACK_CONF_NETWORK was defined, the desired network protocol is used (#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK); otherwise a default protocol is used, which in this case is &amp;#039;&amp;#039;rime_driver&amp;#039;&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
Hence, if you want to use a particular network protocol, you can either define NETSTACK_NETWORK directly or define NETSTACK_CONF_NETWORK.&lt;br /&gt;
&lt;br /&gt;
If no definitions are made, by default ContikiOS stack layers will be:&lt;br /&gt;
&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Network layer&amp;#039;&amp;#039;&amp;#039; - rime_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;MAC layer&amp;#039;&amp;#039;&amp;#039; - nullmac_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;RDC&amp;#039;&amp;#039;&amp;#039; - nullrdc_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039; - framer_nullmac&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Radio&amp;#039;&amp;#039;&amp;#039; - nullradio_driver&lt;br /&gt;
&lt;br /&gt;
The stack layers are usually define inside file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039;. There is one &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file for each platform; for instance, platform &amp;#039;&amp;#039;sky&amp;#039;&amp;#039; has &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; located inside folder &amp;#039;&amp;#039;platform/sky&amp;#039;&amp;#039;. Below we show the excerpt where stack layers are define:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_CONF_MAC&lt;br /&gt;
#define NETSTACK_CONF_MAC     csma_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_MAC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC&lt;br /&gt;
#define NETSTACK_CONF_RDC     contikimac_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE&lt;br /&gt;
#define NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE 8&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RADIO&lt;br /&gt;
#define NETSTACK_CONF_RADIO   cc2420_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RADIO */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_FRAMER&lt;br /&gt;
#define NETSTACK_CONF_FRAMER  framer_802154&lt;br /&gt;
#endif /* NETSTACK_CONF_FRAMER */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can see that, if we are using Tmote Sky hardware, by default the stack layer will be composed by &amp;#039;&amp;#039;csma_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;contikimac_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;framer_802154&amp;#039;&amp;#039; and &amp;#039;&amp;#039;cc2420_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== Changing the protocols ===&lt;br /&gt;
&lt;br /&gt;
If you want to change the protocols to be used you define them either inside the Makefile or creating a project configuration file.&lt;br /&gt;
&lt;br /&gt;
In the first case you should change Makefile, inputing the definitions in one single line, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
DEFINES = NETSTACK_CONF_RDC=cxmac_driver,NETSTACK_CONF_MAC=null_mac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the protocols that you want to use should be defined in the &amp;#039;&amp;#039;DEFINES&amp;#039;&amp;#039; line, using comma to separate them.&lt;br /&gt;
&lt;br /&gt;
The second option is to create a project configuration file. Let&amp;#039;s assume the file create was named &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039;. This file should be inserted into your Makefile, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
CFLAGS += -DPROJECT_CONF_H=\&amp;quot;project-conf.h\&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file you can define all protocols you want to use, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#define NETSTACK_CONF_MAC nullrdc_driver&lt;br /&gt;
#define NETSTACK_CONF_RDC nullmac_driver&lt;br /&gt;
#define NETSTACK_CONF_FRAMER framer_nullmac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color: red&amp;quot;&amp;gt;If you choose to use &amp;#039;&amp;#039;projet-conf.h&amp;#039;&amp;#039; file, you will need to modifed the file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; inside your platform folder. If you are using sky platform, for example, you need to change file &amp;#039;&amp;#039;platform/sky/contiki-conf.h&amp;#039;&amp;#039;. The last part of &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file should be moved to the beginning of the file, as shown below&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* -*- C -*- */&lt;br /&gt;
&lt;br /&gt;
#ifndef CONTIKI_CONF_H&lt;br /&gt;
#define CONTIKI_CONF_H&lt;br /&gt;
&lt;br /&gt;
/* include the project config */&lt;br /&gt;
/* PROJECT_CONF_H might be defined in the project Makefile */&lt;br /&gt;
#ifdef PROJECT_CONF_H&lt;br /&gt;
#include PROJECT_CONF_H&lt;br /&gt;
#endif /* PROJECT_CONF_H */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to code your own protocol ==&lt;br /&gt;
&lt;br /&gt;
We will explain here how to create a new protocol and integrate it into ContikiOS. The protocol can be a Network, MAC, RDC, Framer or Radio; the steps for creating any one of these protocols are similar. We are going to show in details how to create a new MAC protocol, called &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
First step is to create both header and c files for &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039; protocol. Both files need to be created inside &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Our files will be named &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The file &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039; is shown below. In this file we only declare an external variable that will reference our MAC protocols, called &amp;#039;&amp;#039;modifiedmac_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef __MODIFIEDMAC_H__&lt;br /&gt;
#define __MODIFIEDMAC_H__&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;net/mac/mac.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
extern const struct mac_driver modifiedmac_driver;&lt;br /&gt;
&lt;br /&gt;
#endif /* __MODIFIEDMAC_H__ */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An initial version of file &amp;#039;&amp;#039;modifiedrdc.c&amp;#039;&amp;#039; is show below. It only contains the declaration of all functions that must be implemented in a RDC driver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;quot;net/mac/modifiedmac.h&amp;quot;&lt;br /&gt;
#include &amp;quot;net/netstack.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static unsigned short&lt;br /&gt;
channel_check_interval(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
init(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
const struct mac_driver modifiedmac_driver = {&lt;br /&gt;
  &amp;quot;modifiedmac&amp;quot;,&lt;br /&gt;
  init,&lt;br /&gt;
  send_packet,&lt;br /&gt;
  packet_input,&lt;br /&gt;
  on,&lt;br /&gt;
  off,&lt;br /&gt;
  channel_check_interval,&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The next step is to include our source code into ContikiOS make system. We just need to edit the file &amp;#039;&amp;#039;core/net/mac/Makefile.mac&amp;#039;&amp;#039; and include the file &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; in the list of Contiki source files. After editing, &amp;#039;&amp;#039;Makefile.mac&amp;#039;&amp;#039; must look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI_SOURCEFILES += cxmac.c xmac.c nullmac.c lpp.c frame802154.c sicslowmac.c nullrdc.c nullrdc-noframer.c mac.c&lt;br /&gt;
CONTIKI_SOURCEFILES += framer-nullmac.c framer-802154.c csma.c contikimac.c phase.c modifiedmac.c&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Below we show the functions that should be changed in order to make our MAC protocol functional. It is going to work exactly as a Null MAC protocol and we are counting the number of packets received and transmitted.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
uint16_t n_pkts_transmitted = 0;&lt;br /&gt;
uint16_t n_pkts_received = 0;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_transmitted++;&lt;br /&gt;
  NETSTACK_RDC.send(sent, ptr);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_received++;&lt;br /&gt;
  NETSTACK_NETWORK.input();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.on();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.off(keep_radio_on);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Evaluating ContikiMAC and XMAC protocols ==&lt;br /&gt;
&lt;br /&gt;
We are going to evaluate the performance of the two most famous RDC protocols in ContikiOS, the ContikiMAC and X-MAC. We want to evaluate the Packet Reception Rate (PRR), which is the percentage of received packets in an interval; and also the average duty-cycle of sensor nodes. The PRR is related to the maximum throughput the network is able to handle; with lower PRR more packets are dropped and the total amount of data that can be transfered to the sink node is small. Also, the duty-cycle impacts the average power consumption and the lifetime of nodes, since the biggest energy savings are achieved when nodes are sleeping; the lower the duty-cycle the longest will be the node&amp;#039;s lifetime.&lt;br /&gt;
&lt;br /&gt;
We considered the firmware in the folder &amp;#039;&amp;#039;examples/ipv6/rpl-udp/&amp;#039;&amp;#039;, which is a firmware based on IPv6 and RPL for UDP packet transmission. In the same folder there is one simulation file called &amp;#039;&amp;#039;rpl-udp-powertrace.csc&amp;#039;&amp;#039; that is composed of 1 sink node and 30 sensor nodes and traces the energy consumption of all nodes, and also keeps track of PRR values throughout the simulation time. We considered the firmwares &amp;#039;&amp;#039;udp-server.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; and reduced the data transmission interval in &amp;#039;&amp;#039;udl-client.c&amp;#039;&amp;#039; to 1 second. The line below shows where the code of &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; was changed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef PERIOD&lt;br /&gt;
#define PERIOD 1&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By deafult the simulation is executed with ContikiMAC. We let the simulation run for 5 minutes before extracting the desired statistics. After simulatin for ContikiMAC, we changed the RDC protocols, as described before, to X-MAC. And the same statistics were obtained. The figures below show the average duty-cycle of each node for both protocols. The figure on the left shows results of ContikiMAC and the one on the right shows results of X-MAC.&lt;br /&gt;
&lt;br /&gt;
[[File:power_contikimac.png|300px]] [[File:power_xmac.png|300px]]&lt;br /&gt;
&lt;br /&gt;
The PRR statistics were also analysed for all 30 sensor nodes. On average the PRR of ContikiMAC was &amp;#039;&amp;#039;&amp;#039;50.48%&amp;#039;&amp;#039;&amp;#039; and the PRR of X-MAC was &amp;#039;&amp;#039;&amp;#039;30.38%&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[http://www.cse.wustl.edu/~lu/cse521s/Papers/x-mac.pdf 1]- Buettner, Michael, Gary V. Yee, Eric Anderson, and Richard Han. &amp;quot;X-MAC: a short preamble MAC protocol for duty-cycled wireless sensor networks.&amp;quot; In Proceedings of the 4th international conference on Embedded networked sensor systems, pp. 307-320. ACM, 2006.&lt;br /&gt;
&lt;br /&gt;
[http://soda.swedish-ict.se/5128/1/contikimac-report.pdf 2] - Dunkels, Adam. &amp;quot;The contikimac radio duty cycling protocol.&amp;quot; (2011).&lt;br /&gt;
&lt;br /&gt;
[http://web.cs.jhu.edu/~cliang4/public/doc/2008_IPSN_Koala.pdf 3] - Liang, Chieh-Jan Mike, and Andreas Terzis. &amp;quot;Koala: Ultra-low power data retrieval in wireless sensor networks.&amp;quot; In Proceedings of the 7th international conference on Information processing in sensor networks, pp. 421-432. IEEE Computer Society, 2008.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
Edited by: Pedro&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1102</id>
		<title>MAC protocols in ContikiOS</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1102"/>
				<updated>2014-11-08T20:04:41Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* MAC protocols */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This tutorial covers the main features of &amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039; protocols available in &amp;#039;&amp;#039;ContikiOS 2.7&amp;#039;&amp;#039;. Medium Access Control protocols take care of the organization of medium access in wireless networks. They can be interpreted as rules that coordinate when each node is going to transmit/receive packets.&lt;br /&gt;
&lt;br /&gt;
There are a large number of protocols available in the literature, they can usually be classified as contention-based or reservation-based protocols. The first group is based on Carrier Sensing for detecting medium activity and are prone to collisions and lower efficiency; however, they are of easy implementation. The second group is more efficient in terms of throughput and energy, but require precise synchronization and is less adaptable to dynamic traffic. &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; only includes MAC protocols of the first category.&lt;br /&gt;
&lt;br /&gt;
There are about 5 types of MAC layers that can be used in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. They are usually implementation of well-known MAC protocols for &amp;#039;&amp;#039;Wireless Sensor Networks&amp;#039;&amp;#039;; a few of them are implementations of protocols developed exclusively for &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;, but that were based on previous work present in the literature.&lt;br /&gt;
&lt;br /&gt;
== You will learn ==&lt;br /&gt;
&lt;br /&gt;
Here you are going to be to be more familiar with all MAC protocols implemented on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. You are going to understand the organization of MAC protocols, where all files are located and where you should start when you want to modify a protocol. You will also learn the steps you need to take in order to implement your own MAC protocol. And finally, we evaluate two famous MAC protocols: &amp;#039;&amp;#039;&amp;#039;ContikiMAC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;X-MAC&amp;#039;&amp;#039;&amp;#039;, showing a few statistics regarding their performance.&lt;br /&gt;
&lt;br /&gt;
== MAC, RDC and Framer drivers ==&lt;br /&gt;
&lt;br /&gt;
The network stack implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; is a little bit different than the usual 5-layers model typically adopted in TCP/IP. In-between the &amp;#039;&amp;#039;Physical&amp;#039;&amp;#039; and the &amp;#039;&amp;#039;Network&amp;#039;&amp;#039; layers, where usually is located the MAC, we have 3 different layers: &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039;&amp;#039;. The figure below shows the organization of layers in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[File:contiki-layers.jpg|150px|center]]&lt;br /&gt;
&lt;br /&gt;
The network layers can be accessed through the global variables NETSTACK_FRAMER, NETSTACK_RDC and NETSTACK_MAC, which are defined in compilation time. All network layers variables can be found in &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039;. Every network layer is defined by the pragmas &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; in the beginning of this file.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layer is not a common layer implementation; it is actually a collection of auxiliary functions that are called for framezation of transmitting data and parsing of data being received. Examples of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; types can be found in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; there are two types of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layers: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-802154.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039; layer takes care of the sleep period of nodes. This is the most important layer because it is the one responsible for deciding exactly when the packets will be transmitted and it is responsible for making sure that the node is awake when packets are to be received. RDC protocols&amp;#039; source codes are also located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Examples of RDC layers that are implemented: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;contikimac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;xmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;lpp.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc-noframer.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Finally, the MAC layer takes care of addressing and retransmission of lost packets. The source files can be located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; we have only two types of MAC layers available: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;csma.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Framers ==&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; consists of a collection of auxiliary functions that are called before transmitting a packet and after their reception. The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; structure, called &amp;#039;&amp;#039;struct framer&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/framer.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
struct framer {&lt;br /&gt;
&lt;br /&gt;
  int (* create)(void);&lt;br /&gt;
  int (* parse)(void);&lt;br /&gt;
&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function create should create a new frame to be transmitted. While function parse will be called for parsing a received frame.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-nullmac&amp;#039;&amp;#039; is defined by files &amp;#039;&amp;#039;core/net/mac/framer-nullmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-nullmac.h&amp;#039;&amp;#039;. It should be used together with &amp;#039;&amp;#039;nullmac&amp;#039;&amp;#039; (MAC layer). The auxiliary function simply populate the 2 fields of &amp;#039;&amp;#039;nullmac_hdr&amp;#039;&amp;#039;, which are: receiver address and sender address. This is the very basic type of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; that can be used on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-802154&amp;#039;&amp;#039; is defined by &amp;#039;&amp;#039;core/net/mac/framer-802154.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-802154.h&amp;#039;&amp;#039;. It creates and parses frames compatible with standard IEEE 802.15.4 (2003). The create and parse function manipulate all header information abstractedly through the &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure. The real work is done by functions located in the file &amp;#039;&amp;#039;core/net/mac/frame802154.c&amp;#039;&amp;#039;, which are responsible for extracting all information from receive/transmit buffer and inserting/extracting them appropriately into &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure.&lt;br /&gt;
&lt;br /&gt;
== RDC protocols ==&lt;br /&gt;
&lt;br /&gt;
The RDC structure, called &amp;#039;&amp;#039;struct rdc_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/rdc.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a RDC (radio duty cycling) driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct rdc_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the RDC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet list */&lt;br /&gt;
  void (* send_list)(mac_callback_t sent_callback, void *ptr, struct rdc_buf_list *list);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function input is called when a new packet has arrived and need to be processed by RDC layer. And functions send and send_list are called for transmitting out-going packets.&lt;br /&gt;
&lt;br /&gt;
There are 2 very basic types of RDC layers implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: &amp;#039;&amp;#039;nullrdc-noframer&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc-noframer.c&amp;#039;&amp;#039;) and &amp;#039;&amp;#039;nullrdc&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc.c&amp;#039;&amp;#039;). The first one does not use Framer functions, and transmits/receives data directly to Radio layer (Physical). The developer can take total control over the format of the packets being transmitted. &lt;br /&gt;
&lt;br /&gt;
The second RDC layer (nullrdc) uses the Framer functions for header creation/parsing. It does not save energy and works as a pass-through layer that only transmits a packet and returns the results of such transmission (success or collision).&lt;br /&gt;
&lt;br /&gt;
A third simple RDC layer is &amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/sicslowmac.c&amp;#039;&amp;#039;). This is a RDC layer with no energy savings and that uses IEEE 8021.5.4 frames.&lt;br /&gt;
&lt;br /&gt;
The three last protocols available in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; are implementations of popular asynchronous MAC protocols that aim at energy saving; namely X-MAC, ContikiMac and LPP. X-MAC is a short-preamble protocol from 2006 that was ported to ContikiOS [1]. X-MAC has two different implementation in ContikiOS, which can be found in files &amp;#039;&amp;#039;core/net/mac/cxmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/xmac.c&amp;#039;&amp;#039;. ContikiMac [2] is a protocol that proposes enhancement over X-MAC. The implementation can be found in file &amp;#039;&amp;#039;core/net/mac/contikimac.c&amp;#039;&amp;#039;. Finally, LPP (Low Power Probing) is a low power protocol from 2008 that was also ported to ContikiOS [3]; the code can be found in &amp;#039;&amp;#039;core/net/mac/lpp.c&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Each of the last three RDC protocols can be used for energy saving. The details of each of these protocols is outside the scope of this tutorial and can be found in the references.&lt;br /&gt;
&lt;br /&gt;
== MAC protocols ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The MAC structure, called &amp;#039;&amp;#039;struct mac_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/mac.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a MAC protocol driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct mac_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the MAC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
  &lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are only two types of MAC protocols in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: nullmac (&amp;#039;&amp;#039;core/net/mac/nullmac.c&amp;#039;&amp;#039;) and csma (&amp;#039;&amp;#039;core/net/mac/csma&amp;#039;&amp;#039;). The first one is a simple pass-through protocol that simply calls the appropriate RDC functions. And the second one implements addressing, sequence number and retransmissions. CSMA protocol keep a list of packets to each of the neighbors and calculate statistics such as number of retransmissions, collisions, deferrals, etc.&lt;br /&gt;
&lt;br /&gt;
== How to choose and change your protocols ==&lt;br /&gt;
&lt;br /&gt;
All three stack layers (Framer, RDC and MAC) need to be defined during compilation time. They are define using pragma &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; either in the Makefile or in the &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file. The definitions of which protocols should be used are used by file &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039; that effectively binds the NETSTACK_FRAMER, NETSTACK_RDC and NETSTACK_MAC to the protocols that are supposed to be used.&lt;br /&gt;
&lt;br /&gt;
Below it is shown the piece of code responsible for setting the MAC layer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_NETWORK&lt;br /&gt;
#ifdef NETSTACK_CONF_NETWORK&lt;br /&gt;
#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK&lt;br /&gt;
#else /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#define NETSTACK_NETWORK rime_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#endif /* NETSTACK_NETWORK */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Initially the code checks if NETSTACK_NETWORK was already define (#ifndef NETSTACK_NETWORK). If it was not, the code then checks if NETSTACK_CONF_NETWORK was define. If NETSTACK_CONF_NETWORK was defined, the desired network protocol is used (#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK); otherwise a default protocol is used, which in this case is &amp;#039;&amp;#039;rime_driver&amp;#039;&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
Hence, if you want to use a particular network protocol, you can either define NETSTACK_NETWORK directly or define NETSTACK_CONF_NETWORK.&lt;br /&gt;
&lt;br /&gt;
If no definitions are made, by default ContikiOS stack layers will be:&lt;br /&gt;
&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Network layer&amp;#039;&amp;#039;&amp;#039; - rime_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;MAC layer&amp;#039;&amp;#039;&amp;#039; - nullmac_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;RDC&amp;#039;&amp;#039;&amp;#039; - nullrdc_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039; - framer_nullmac&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Radio&amp;#039;&amp;#039;&amp;#039; - nullradio_driver&lt;br /&gt;
&lt;br /&gt;
The stack layers are usually define inside file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039;. There is one &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file for each platform; for instance, platform &amp;#039;&amp;#039;sky&amp;#039;&amp;#039; has &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; located inside folder &amp;#039;&amp;#039;platform/sky&amp;#039;&amp;#039;. Below we show the excerpt where stack layers are define:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_CONF_MAC&lt;br /&gt;
#define NETSTACK_CONF_MAC     csma_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_MAC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC&lt;br /&gt;
#define NETSTACK_CONF_RDC     contikimac_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE&lt;br /&gt;
#define NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE 8&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RADIO&lt;br /&gt;
#define NETSTACK_CONF_RADIO   cc2420_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RADIO */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_FRAMER&lt;br /&gt;
#define NETSTACK_CONF_FRAMER  framer_802154&lt;br /&gt;
#endif /* NETSTACK_CONF_FRAMER */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can see that, if we are using Tmote Sky hardware, by default the stack layer will be composed by &amp;#039;&amp;#039;csma_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;contikimac_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;framer_802154&amp;#039;&amp;#039; and &amp;#039;&amp;#039;cc2420_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== Changing the protocols ===&lt;br /&gt;
&lt;br /&gt;
If you want to change the protocols to be used you define them either inside the Makefile or creating a project configuration file.&lt;br /&gt;
&lt;br /&gt;
In the first case you should change Makefile, inputing the definitions in one single line, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
DEFINES = NETSTACK_CONF_RDC=cxmac_driver,NETSTACK_CONF_MAC=null_mac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the protocols that you want to use should be defined in the &amp;#039;&amp;#039;DEFINES&amp;#039;&amp;#039; line, using comma to separate them.&lt;br /&gt;
&lt;br /&gt;
The second option is to create a project configuration file. Let&amp;#039;s assume the file create was named &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039;. This file should be inserted into your Makefile, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
CFLAGS += -DPROJECT_CONF_H=\&amp;quot;project-conf.h\&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file you can define all protocols you want to use, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#define NETSTACK_CONF_MAC nullrdc_driver&lt;br /&gt;
#define NETSTACK_CONF_RDC nullmac_driver&lt;br /&gt;
#define NETSTACK_CONF_FRAMER framer_nullmac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color: red&amp;quot;&amp;gt;If you choose to use &amp;#039;&amp;#039;projet-conf.h&amp;#039;&amp;#039; file, you will need to modifed the file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; inside your platform folder. If you are using sky platform, for example, you need to change file &amp;#039;&amp;#039;platform/sky/contiki-conf.h&amp;#039;&amp;#039;. The last part of &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file should be moved to the beginning of the file, as shown below&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* -*- C -*- */&lt;br /&gt;
&lt;br /&gt;
#ifndef CONTIKI_CONF_H&lt;br /&gt;
#define CONTIKI_CONF_H&lt;br /&gt;
&lt;br /&gt;
/* include the project config */&lt;br /&gt;
/* PROJECT_CONF_H might be defined in the project Makefile */&lt;br /&gt;
#ifdef PROJECT_CONF_H&lt;br /&gt;
#include PROJECT_CONF_H&lt;br /&gt;
#endif /* PROJECT_CONF_H */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to code your own protocol ==&lt;br /&gt;
&lt;br /&gt;
We will explain here how to create a new protocol and integrate it into ContikiOS. The protocol can be a Network, MAC, RDC, Framer or Radio; the steps for creating any one of these protocols are similar. We are going to show in details how to create a new MAC protocol, called &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
First step is to create both header and c files for &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039; protocol. Both files need to be created inside &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Our files will be named &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The file &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039; is shown below. In this file we only declare an external variable that will reference our MAC protocols, called &amp;#039;&amp;#039;modifiedmac_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef __MODIFIEDMAC_H__&lt;br /&gt;
#define __MODIFIEDMAC_H__&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;net/mac/mac.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
extern const struct mac_driver modifiedmac_driver;&lt;br /&gt;
&lt;br /&gt;
#endif /* __MODIFIEDMAC_H__ */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An initial version of file &amp;#039;&amp;#039;modifiedrdc.c&amp;#039;&amp;#039; is show below. It only contains the declaration of all functions that must be implemented in a RDC driver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;quot;net/mac/modifiedmac.h&amp;quot;&lt;br /&gt;
#include &amp;quot;net/netstack.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static unsigned short&lt;br /&gt;
channel_check_interval(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
init(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
const struct mac_driver modifiedmac_driver = {&lt;br /&gt;
  &amp;quot;modifiedmac&amp;quot;,&lt;br /&gt;
  init,&lt;br /&gt;
  send_packet,&lt;br /&gt;
  packet_input,&lt;br /&gt;
  on,&lt;br /&gt;
  off,&lt;br /&gt;
  channel_check_interval,&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The next step is to include our source code into ContikiOS make system. We just need to edit the file &amp;#039;&amp;#039;core/net/mac/Makefile.mac&amp;#039;&amp;#039; and include the file &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; in the list of Contiki source files. After editing, &amp;#039;&amp;#039;Makefile.mac&amp;#039;&amp;#039; must look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI_SOURCEFILES += cxmac.c xmac.c nullmac.c lpp.c frame802154.c sicslowmac.c nullrdc.c nullrdc-noframer.c mac.c&lt;br /&gt;
CONTIKI_SOURCEFILES += framer-nullmac.c framer-802154.c csma.c contikimac.c phase.c modifiedmac.c&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Below we show the functions that should be changed in order to make our MAC protocol functional. It is going to work exatcly as a Null MAC protocol and we are counting the number of packets received and transmitted.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
uint16_t n_pkts_transmitted = 0;&lt;br /&gt;
uint16_t n_pkts_received = 0;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_transmitted++;&lt;br /&gt;
  NETSTACK_RDC.send(sent, ptr);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_received++;&lt;br /&gt;
  NETSTACK_NETWORK.input();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.on();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.off(keep_radio_on);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Evaluating ContikiMAC and XMAC protocols ==&lt;br /&gt;
&lt;br /&gt;
We are going to evaluate the performance of the two most famous RDC protocols in ContikiOS, the ContikiMAC and X-MAC. We want to evaluate the Packet Reception Rate (PRR), which is the percentage of received packets in an interval; and also the average duty-cycle of sensor nodes. The PRR is related to the maximum throughput the network is able to handle; with lower PRR more packets are dropped and the total amount of data that can be transfered to the sink node is small. Also, the duty-cycle impacts the average power consumption and the lifetime of nodes, since the biggest energy savings are achieved when nodes are sleeping; the lower the duty-cycle the longest will be the node&amp;#039;s lifetime.&lt;br /&gt;
&lt;br /&gt;
We considered the firmware in the folder &amp;#039;&amp;#039;examples/ipv6/rpl-udp/&amp;#039;&amp;#039;, which is a firmware based on IPv6 and RPL for UDP packet transmission. In the same folder there is one simulation file called &amp;#039;&amp;#039;rpl-udp-powertrace.csc&amp;#039;&amp;#039; that is composed of 1 sink node and 30 sensor nodes and traces the energy consumption of all nodes, and also keeps track of PRR values throughout the simulation time. We considered the firmwares &amp;#039;&amp;#039;udp-server.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; and reduced the data transmission interval in &amp;#039;&amp;#039;udl-client.c&amp;#039;&amp;#039; to 1 second. The line below shows where the code of &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; was changed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef PERIOD&lt;br /&gt;
#define PERIOD 1&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By deafult the simulation is executed with ContikiMAC. We let the simulation run for 5 minutes before extracting the desired statistics. After simulatin for ContikiMAC, we changed the RDC protocols, as described before, to X-MAC. And the same statistics were obtained. The figures below show the average duty-cycle of each node for both protocols. The figure on the left shows results of ContikiMAC and the one on the right shows results of X-MAC.&lt;br /&gt;
&lt;br /&gt;
[[File:power_contikimac.png|300px]] [[File:power_xmac.png|300px]]&lt;br /&gt;
&lt;br /&gt;
The PRR statistics were also analysed for all 30 sensor nodes. On average the PRR of ContikiMAC was &amp;#039;&amp;#039;&amp;#039;50.48%&amp;#039;&amp;#039;&amp;#039; and the PRR of X-MAC was &amp;#039;&amp;#039;&amp;#039;30.38%&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[http://www.cse.wustl.edu/~lu/cse521s/Papers/x-mac.pdf 1]- Buettner, Michael, Gary V. Yee, Eric Anderson, and Richard Han. &amp;quot;X-MAC: a short preamble MAC protocol for duty-cycled wireless sensor networks.&amp;quot; In Proceedings of the 4th international conference on Embedded networked sensor systems, pp. 307-320. ACM, 2006.&lt;br /&gt;
&lt;br /&gt;
[http://soda.swedish-ict.se/5128/1/contikimac-report.pdf 2] - Dunkels, Adam. &amp;quot;The contikimac radio duty cycling protocol.&amp;quot; (2011).&lt;br /&gt;
&lt;br /&gt;
[http://web.cs.jhu.edu/~cliang4/public/doc/2008_IPSN_Koala.pdf 3] - Liang, Chieh-Jan Mike, and Andreas Terzis. &amp;quot;Koala: Ultra-low power data retrieval in wireless sensor networks.&amp;quot; In Proceedings of the 7th international conference on Information processing in sensor networks, pp. 421-432. IEEE Computer Society, 2008.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
Edited by: Pedro&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1101</id>
		<title>MAC protocols in ContikiOS</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1101"/>
				<updated>2014-11-08T20:03:51Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* RDC protocols */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This tutorial covers the main features of &amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039; protocols available in &amp;#039;&amp;#039;ContikiOS 2.7&amp;#039;&amp;#039;. Medium Access Control protocols take care of the organization of medium access in wireless networks. They can be interpreted as rules that coordinate when each node is going to transmit/receive packets.&lt;br /&gt;
&lt;br /&gt;
There are a large number of protocols available in the literature, they can usually be classified as contention-based or reservation-based protocols. The first group is based on Carrier Sensing for detecting medium activity and are prone to collisions and lower efficiency; however, they are of easy implementation. The second group is more efficient in terms of throughput and energy, but require precise synchronization and is less adaptable to dynamic traffic. &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; only includes MAC protocols of the first category.&lt;br /&gt;
&lt;br /&gt;
There are about 5 types of MAC layers that can be used in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. They are usually implementation of well-known MAC protocols for &amp;#039;&amp;#039;Wireless Sensor Networks&amp;#039;&amp;#039;; a few of them are implementations of protocols developed exclusively for &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;, but that were based on previous work present in the literature.&lt;br /&gt;
&lt;br /&gt;
== You will learn ==&lt;br /&gt;
&lt;br /&gt;
Here you are going to be to be more familiar with all MAC protocols implemented on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. You are going to understand the organization of MAC protocols, where all files are located and where you should start when you want to modify a protocol. You will also learn the steps you need to take in order to implement your own MAC protocol. And finally, we evaluate two famous MAC protocols: &amp;#039;&amp;#039;&amp;#039;ContikiMAC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;X-MAC&amp;#039;&amp;#039;&amp;#039;, showing a few statistics regarding their performance.&lt;br /&gt;
&lt;br /&gt;
== MAC, RDC and Framer drivers ==&lt;br /&gt;
&lt;br /&gt;
The network stack implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; is a little bit different than the usual 5-layers model typically adopted in TCP/IP. In-between the &amp;#039;&amp;#039;Physical&amp;#039;&amp;#039; and the &amp;#039;&amp;#039;Network&amp;#039;&amp;#039; layers, where usually is located the MAC, we have 3 different layers: &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039;&amp;#039;. The figure below shows the organization of layers in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[File:contiki-layers.jpg|150px|center]]&lt;br /&gt;
&lt;br /&gt;
The network layers can be accessed through the global variables NETSTACK_FRAMER, NETSTACK_RDC and NETSTACK_MAC, which are defined in compilation time. All network layers variables can be found in &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039;. Every network layer is defined by the pragmas &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; in the beginning of this file.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layer is not a common layer implementation; it is actually a collection of auxiliary functions that are called for framezation of transmitting data and parsing of data being received. Examples of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; types can be found in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; there are two types of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layers: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-802154.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039; layer takes care of the sleep period of nodes. This is the most important layer because it is the one responsible for deciding exactly when the packets will be transmitted and it is responsible for making sure that the node is awake when packets are to be received. RDC protocols&amp;#039; source codes are also located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Examples of RDC layers that are implemented: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;contikimac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;xmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;lpp.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc-noframer.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Finally, the MAC layer takes care of addressing and retransmission of lost packets. The source files can be located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; we have only two types of MAC layers available: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;csma.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Framers ==&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; consists of a collection of auxiliary functions that are called before transmitting a packet and after their reception. The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; structure, called &amp;#039;&amp;#039;struct framer&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/framer.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
struct framer {&lt;br /&gt;
&lt;br /&gt;
  int (* create)(void);&lt;br /&gt;
  int (* parse)(void);&lt;br /&gt;
&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function create should create a new frame to be transmitted. While function parse will be called for parsing a received frame.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-nullmac&amp;#039;&amp;#039; is defined by files &amp;#039;&amp;#039;core/net/mac/framer-nullmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-nullmac.h&amp;#039;&amp;#039;. It should be used together with &amp;#039;&amp;#039;nullmac&amp;#039;&amp;#039; (MAC layer). The auxiliary function simply populate the 2 fields of &amp;#039;&amp;#039;nullmac_hdr&amp;#039;&amp;#039;, which are: receiver address and sender address. This is the very basic type of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; that can be used on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-802154&amp;#039;&amp;#039; is defined by &amp;#039;&amp;#039;core/net/mac/framer-802154.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-802154.h&amp;#039;&amp;#039;. It creates and parses frames compatible with standard IEEE 802.15.4 (2003). The create and parse function manipulate all header information abstractedly through the &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure. The real work is done by functions located in the file &amp;#039;&amp;#039;core/net/mac/frame802154.c&amp;#039;&amp;#039;, which are responsible for extracting all information from receive/transmit buffer and inserting/extracting them appropriately into &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure.&lt;br /&gt;
&lt;br /&gt;
== RDC protocols ==&lt;br /&gt;
&lt;br /&gt;
The RDC structure, called &amp;#039;&amp;#039;struct rdc_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/rdc.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a RDC (radio duty cycling) driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct rdc_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the RDC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet list */&lt;br /&gt;
  void (* send_list)(mac_callback_t sent_callback, void *ptr, struct rdc_buf_list *list);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function input is called when a new packet has arrived and need to be processed by RDC layer. And functions send and send_list are called for transmitting out-going packets.&lt;br /&gt;
&lt;br /&gt;
There are 2 very basic types of RDC layers implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: &amp;#039;&amp;#039;nullrdc-noframer&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc-noframer.c&amp;#039;&amp;#039;) and &amp;#039;&amp;#039;nullrdc&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc.c&amp;#039;&amp;#039;). The first one does not use Framer functions, and transmits/receives data directly to Radio layer (Physical). The developer can take total control over the format of the packets being transmitted. &lt;br /&gt;
&lt;br /&gt;
The second RDC layer (nullrdc) uses the Framer functions for header creation/parsing. It does not save energy and works as a pass-through layer that only transmits a packet and returns the results of such transmission (success or collision).&lt;br /&gt;
&lt;br /&gt;
A third simple RDC layer is &amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/sicslowmac.c&amp;#039;&amp;#039;). This is a RDC layer with no energy savings and that uses IEEE 8021.5.4 frames.&lt;br /&gt;
&lt;br /&gt;
The three last protocols available in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; are implementations of popular asynchronous MAC protocols that aim at energy saving; namely X-MAC, ContikiMac and LPP. X-MAC is a short-preamble protocol from 2006 that was ported to ContikiOS [1]. X-MAC has two different implementation in ContikiOS, which can be found in files &amp;#039;&amp;#039;core/net/mac/cxmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/xmac.c&amp;#039;&amp;#039;. ContikiMac [2] is a protocol that proposes enhancement over X-MAC. The implementation can be found in file &amp;#039;&amp;#039;core/net/mac/contikimac.c&amp;#039;&amp;#039;. Finally, LPP (Low Power Probing) is a low power protocol from 2008 that was also ported to ContikiOS [3]; the code can be found in &amp;#039;&amp;#039;core/net/mac/lpp.c&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Each of the last three RDC protocols can be used for energy saving. The details of each of these protocols is outside the scope of this tutorial and can be found in the references.&lt;br /&gt;
&lt;br /&gt;
== MAC protocols ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The MAC struct, called &amp;#039;&amp;#039;struct mac_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/mac.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a MAC protocol driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct mac_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the MAC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
  &lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are only two types of MAC protocols in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: nullmac (&amp;#039;&amp;#039;core/net/mac/nullmac.c&amp;#039;&amp;#039;) and csma (&amp;#039;&amp;#039;core/net/mac/csma&amp;#039;&amp;#039;). The first one is a simple pass-through protocol that simply calls the appropriate RDC functions. And the second one implements addressing, sequence number and retransmissions. CSMA protocol keep a list of packets to each of the neighbors and calculate statistics such as number of retransmissions, collisions, deferrals, etc.&lt;br /&gt;
&lt;br /&gt;
== How to choose and change your protocols ==&lt;br /&gt;
&lt;br /&gt;
All three stack layers (Framer, RDC and MAC) need to be defined during compilation time. They are define using pragma &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; either in the Makefile or in the &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file. The definitions of which protocols should be used are used by file &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039; that effectively binds the NETSTACK_FRAMER, NETSTACK_RDC and NETSTACK_MAC to the protocols that are supposed to be used.&lt;br /&gt;
&lt;br /&gt;
Below it is shown the piece of code responsible for setting the MAC layer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_NETWORK&lt;br /&gt;
#ifdef NETSTACK_CONF_NETWORK&lt;br /&gt;
#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK&lt;br /&gt;
#else /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#define NETSTACK_NETWORK rime_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#endif /* NETSTACK_NETWORK */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Initially the code checks if NETSTACK_NETWORK was already define (#ifndef NETSTACK_NETWORK). If it was not, the code then checks if NETSTACK_CONF_NETWORK was define. If NETSTACK_CONF_NETWORK was defined, the desired network protocol is used (#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK); otherwise a default protocol is used, which in this case is &amp;#039;&amp;#039;rime_driver&amp;#039;&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
Hence, if you want to use a particular network protocol, you can either define NETSTACK_NETWORK directly or define NETSTACK_CONF_NETWORK.&lt;br /&gt;
&lt;br /&gt;
If no definitions are made, by default ContikiOS stack layers will be:&lt;br /&gt;
&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Network layer&amp;#039;&amp;#039;&amp;#039; - rime_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;MAC layer&amp;#039;&amp;#039;&amp;#039; - nullmac_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;RDC&amp;#039;&amp;#039;&amp;#039; - nullrdc_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039; - framer_nullmac&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Radio&amp;#039;&amp;#039;&amp;#039; - nullradio_driver&lt;br /&gt;
&lt;br /&gt;
The stack layers are usually define inside file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039;. There is one &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file for each platform; for instance, platform &amp;#039;&amp;#039;sky&amp;#039;&amp;#039; has &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; located inside folder &amp;#039;&amp;#039;platform/sky&amp;#039;&amp;#039;. Below we show the excerpt where stack layers are define:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_CONF_MAC&lt;br /&gt;
#define NETSTACK_CONF_MAC     csma_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_MAC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC&lt;br /&gt;
#define NETSTACK_CONF_RDC     contikimac_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE&lt;br /&gt;
#define NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE 8&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RADIO&lt;br /&gt;
#define NETSTACK_CONF_RADIO   cc2420_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RADIO */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_FRAMER&lt;br /&gt;
#define NETSTACK_CONF_FRAMER  framer_802154&lt;br /&gt;
#endif /* NETSTACK_CONF_FRAMER */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can see that, if we are using Tmote Sky hardware, by default the stack layer will be composed by &amp;#039;&amp;#039;csma_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;contikimac_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;framer_802154&amp;#039;&amp;#039; and &amp;#039;&amp;#039;cc2420_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== Changing the protocols ===&lt;br /&gt;
&lt;br /&gt;
If you want to change the protocols to be used you define them either inside the Makefile or creating a project configuration file.&lt;br /&gt;
&lt;br /&gt;
In the first case you should change Makefile, inputing the definitions in one single line, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
DEFINES = NETSTACK_CONF_RDC=cxmac_driver,NETSTACK_CONF_MAC=null_mac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the protocols that you want to use should be defined in the &amp;#039;&amp;#039;DEFINES&amp;#039;&amp;#039; line, using comma to separate them.&lt;br /&gt;
&lt;br /&gt;
The second option is to create a project configuration file. Let&amp;#039;s assume the file create was named &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039;. This file should be inserted into your Makefile, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
CFLAGS += -DPROJECT_CONF_H=\&amp;quot;project-conf.h\&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file you can define all protocols you want to use, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#define NETSTACK_CONF_MAC nullrdc_driver&lt;br /&gt;
#define NETSTACK_CONF_RDC nullmac_driver&lt;br /&gt;
#define NETSTACK_CONF_FRAMER framer_nullmac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color: red&amp;quot;&amp;gt;If you choose to use &amp;#039;&amp;#039;projet-conf.h&amp;#039;&amp;#039; file, you will need to modifed the file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; inside your platform folder. If you are using sky platform, for example, you need to change file &amp;#039;&amp;#039;platform/sky/contiki-conf.h&amp;#039;&amp;#039;. The last part of &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file should be moved to the beginning of the file, as shown below&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* -*- C -*- */&lt;br /&gt;
&lt;br /&gt;
#ifndef CONTIKI_CONF_H&lt;br /&gt;
#define CONTIKI_CONF_H&lt;br /&gt;
&lt;br /&gt;
/* include the project config */&lt;br /&gt;
/* PROJECT_CONF_H might be defined in the project Makefile */&lt;br /&gt;
#ifdef PROJECT_CONF_H&lt;br /&gt;
#include PROJECT_CONF_H&lt;br /&gt;
#endif /* PROJECT_CONF_H */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to code your own protocol ==&lt;br /&gt;
&lt;br /&gt;
We will explain here how to create a new protocol and integrate it into ContikiOS. The protocol can be a Network, MAC, RDC, Framer or Radio; the steps for creating any one of these protocols are similar. We are going to show in details how to create a new MAC protocol, called &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
First step is to create both header and c files for &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039; protocol. Both files need to be created inside &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Our files will be named &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The file &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039; is shown below. In this file we only declare an external variable that will reference our MAC protocols, called &amp;#039;&amp;#039;modifiedmac_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef __MODIFIEDMAC_H__&lt;br /&gt;
#define __MODIFIEDMAC_H__&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;net/mac/mac.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
extern const struct mac_driver modifiedmac_driver;&lt;br /&gt;
&lt;br /&gt;
#endif /* __MODIFIEDMAC_H__ */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An initial version of file &amp;#039;&amp;#039;modifiedrdc.c&amp;#039;&amp;#039; is show below. It only contains the declaration of all functions that must be implemented in a RDC driver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;quot;net/mac/modifiedmac.h&amp;quot;&lt;br /&gt;
#include &amp;quot;net/netstack.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static unsigned short&lt;br /&gt;
channel_check_interval(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
init(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
const struct mac_driver modifiedmac_driver = {&lt;br /&gt;
  &amp;quot;modifiedmac&amp;quot;,&lt;br /&gt;
  init,&lt;br /&gt;
  send_packet,&lt;br /&gt;
  packet_input,&lt;br /&gt;
  on,&lt;br /&gt;
  off,&lt;br /&gt;
  channel_check_interval,&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The next step is to include our source code into ContikiOS make system. We just need to edit the file &amp;#039;&amp;#039;core/net/mac/Makefile.mac&amp;#039;&amp;#039; and include the file &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; in the list of Contiki source files. After editing, &amp;#039;&amp;#039;Makefile.mac&amp;#039;&amp;#039; must look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI_SOURCEFILES += cxmac.c xmac.c nullmac.c lpp.c frame802154.c sicslowmac.c nullrdc.c nullrdc-noframer.c mac.c&lt;br /&gt;
CONTIKI_SOURCEFILES += framer-nullmac.c framer-802154.c csma.c contikimac.c phase.c modifiedmac.c&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Below we show the functions that should be changed in order to make our MAC protocol functional. It is going to work exatcly as a Null MAC protocol and we are counting the number of packets received and transmitted.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
uint16_t n_pkts_transmitted = 0;&lt;br /&gt;
uint16_t n_pkts_received = 0;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_transmitted++;&lt;br /&gt;
  NETSTACK_RDC.send(sent, ptr);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_received++;&lt;br /&gt;
  NETSTACK_NETWORK.input();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.on();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.off(keep_radio_on);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Evaluating ContikiMAC and XMAC protocols ==&lt;br /&gt;
&lt;br /&gt;
We are going to evaluate the performance of the two most famous RDC protocols in ContikiOS, the ContikiMAC and X-MAC. We want to evaluate the Packet Reception Rate (PRR), which is the percentage of received packets in an interval; and also the average duty-cycle of sensor nodes. The PRR is related to the maximum throughput the network is able to handle; with lower PRR more packets are dropped and the total amount of data that can be transfered to the sink node is small. Also, the duty-cycle impacts the average power consumption and the lifetime of nodes, since the biggest energy savings are achieved when nodes are sleeping; the lower the duty-cycle the longest will be the node&amp;#039;s lifetime.&lt;br /&gt;
&lt;br /&gt;
We considered the firmware in the folder &amp;#039;&amp;#039;examples/ipv6/rpl-udp/&amp;#039;&amp;#039;, which is a firmware based on IPv6 and RPL for UDP packet transmission. In the same folder there is one simulation file called &amp;#039;&amp;#039;rpl-udp-powertrace.csc&amp;#039;&amp;#039; that is composed of 1 sink node and 30 sensor nodes and traces the energy consumption of all nodes, and also keeps track of PRR values throughout the simulation time. We considered the firmwares &amp;#039;&amp;#039;udp-server.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; and reduced the data transmission interval in &amp;#039;&amp;#039;udl-client.c&amp;#039;&amp;#039; to 1 second. The line below shows where the code of &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; was changed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef PERIOD&lt;br /&gt;
#define PERIOD 1&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By deafult the simulation is executed with ContikiMAC. We let the simulation run for 5 minutes before extracting the desired statistics. After simulatin for ContikiMAC, we changed the RDC protocols, as described before, to X-MAC. And the same statistics were obtained. The figures below show the average duty-cycle of each node for both protocols. The figure on the left shows results of ContikiMAC and the one on the right shows results of X-MAC.&lt;br /&gt;
&lt;br /&gt;
[[File:power_contikimac.png|300px]] [[File:power_xmac.png|300px]]&lt;br /&gt;
&lt;br /&gt;
The PRR statistics were also analysed for all 30 sensor nodes. On average the PRR of ContikiMAC was &amp;#039;&amp;#039;&amp;#039;50.48%&amp;#039;&amp;#039;&amp;#039; and the PRR of X-MAC was &amp;#039;&amp;#039;&amp;#039;30.38%&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[http://www.cse.wustl.edu/~lu/cse521s/Papers/x-mac.pdf 1]- Buettner, Michael, Gary V. Yee, Eric Anderson, and Richard Han. &amp;quot;X-MAC: a short preamble MAC protocol for duty-cycled wireless sensor networks.&amp;quot; In Proceedings of the 4th international conference on Embedded networked sensor systems, pp. 307-320. ACM, 2006.&lt;br /&gt;
&lt;br /&gt;
[http://soda.swedish-ict.se/5128/1/contikimac-report.pdf 2] - Dunkels, Adam. &amp;quot;The contikimac radio duty cycling protocol.&amp;quot; (2011).&lt;br /&gt;
&lt;br /&gt;
[http://web.cs.jhu.edu/~cliang4/public/doc/2008_IPSN_Koala.pdf 3] - Liang, Chieh-Jan Mike, and Andreas Terzis. &amp;quot;Koala: Ultra-low power data retrieval in wireless sensor networks.&amp;quot; In Proceedings of the 7th international conference on Information processing in sensor networks, pp. 421-432. IEEE Computer Society, 2008.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
Edited by: Pedro&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	<entry>
		<id>http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1100</id>
		<title>MAC protocols in ContikiOS</title>
		<link rel="alternate" type="text/html" href="http://anrg.usc.edu/contiki/index.php?title=MAC_protocols_in_ContikiOS&amp;diff=1100"/>
				<updated>2014-11-08T20:02:49Z</updated>
		
		<summary type="html">&lt;p&gt;Pedro: /* Framers */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== Introduction ==&lt;br /&gt;
&lt;br /&gt;
This tutorial covers the main features of &amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039; protocols available in &amp;#039;&amp;#039;ContikiOS 2.7&amp;#039;&amp;#039;. Medium Access Control protocols take care of the organization of medium access in wireless networks. They can be interpreted as rules that coordinate when each node is going to transmit/receive packets.&lt;br /&gt;
&lt;br /&gt;
There are a large number of protocols available in the literature, they can usually be classified as contention-based or reservation-based protocols. The first group is based on Carrier Sensing for detecting medium activity and are prone to collisions and lower efficiency; however, they are of easy implementation. The second group is more efficient in terms of throughput and energy, but require precise synchronization and is less adaptable to dynamic traffic. &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; only includes MAC protocols of the first category.&lt;br /&gt;
&lt;br /&gt;
There are about 5 types of MAC layers that can be used in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. They are usually implementation of well-known MAC protocols for &amp;#039;&amp;#039;Wireless Sensor Networks&amp;#039;&amp;#039;; a few of them are implementations of protocols developed exclusively for &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;, but that were based on previous work present in the literature.&lt;br /&gt;
&lt;br /&gt;
== You will learn ==&lt;br /&gt;
&lt;br /&gt;
Here you are going to be to be more familiar with all MAC protocols implemented on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;. You are going to understand the organization of MAC protocols, where all files are located and where you should start when you want to modify a protocol. You will also learn the steps you need to take in order to implement your own MAC protocol. And finally, we evaluate two famous MAC protocols: &amp;#039;&amp;#039;&amp;#039;ContikiMAC&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;X-MAC&amp;#039;&amp;#039;&amp;#039;, showing a few statistics regarding their performance.&lt;br /&gt;
&lt;br /&gt;
== MAC, RDC and Framer drivers ==&lt;br /&gt;
&lt;br /&gt;
The network stack implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; is a little bit different than the usual 5-layers model typically adopted in TCP/IP. In-between the &amp;#039;&amp;#039;Physical&amp;#039;&amp;#039; and the &amp;#039;&amp;#039;Network&amp;#039;&amp;#039; layers, where usually is located the MAC, we have 3 different layers: &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;Medium Access Control (MAC)&amp;#039;&amp;#039;&amp;#039;. The figure below shows the organization of layers in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
[[File:contiki-layers.jpg|150px|center]]&lt;br /&gt;
&lt;br /&gt;
The network layers can be accessed through the global variables NETSTACK_FRAMER, NETSTACK_RDC and NETSTACK_MAC, which are defined in compilation time. All network layers variables can be found in &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039;. Every network layer is defined by the pragmas &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; in the beginning of this file.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layer is not a common layer implementation; it is actually a collection of auxiliary functions that are called for framezation of transmitting data and parsing of data being received. Examples of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; types can be found in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; there are two types of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; layers: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-802154.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;framer-nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;Radio Duty-Cycle (RDC)&amp;#039;&amp;#039; layer takes care of the sleep period of nodes. This is the most important layer because it is the one responsible for deciding exactly when the packets will be transmitted and it is responsible for making sure that the node is awake when packets are to be received. RDC protocols&amp;#039; source codes are also located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Examples of RDC layers that are implemented: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;contikimac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;xmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;lpp.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc-noframer.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;, &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullrdc.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Finally, the MAC layer takes care of addressing and retransmission of lost packets. The source files can be located in &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;; we have only two types of MAC layers available: &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;csma.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039; and &amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;nullmac.c&amp;#039;&amp;#039;&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Framers ==&lt;br /&gt;
&lt;br /&gt;
A &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; consists of a collection of auxiliary functions that are called before transmitting a packet and after their reception. The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; structure, called &amp;#039;&amp;#039;struct framer&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/framer.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
struct framer {&lt;br /&gt;
&lt;br /&gt;
  int (* create)(void);&lt;br /&gt;
  int (* parse)(void);&lt;br /&gt;
&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function create should create a new frame to be transmitted. While function parse will be called for parsing a received frame.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-nullmac&amp;#039;&amp;#039; is defined by files &amp;#039;&amp;#039;core/net/mac/framer-nullmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-nullmac.h&amp;#039;&amp;#039;. It should be used together with &amp;#039;&amp;#039;nullmac&amp;#039;&amp;#039; (MAC layer). The auxiliary function simply populate the 2 fields of &amp;#039;&amp;#039;nullmac_hdr&amp;#039;&amp;#039;, which are: receiver address and sender address. This is the very basic type of &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; that can be used on &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The &amp;#039;&amp;#039;Framer&amp;#039;&amp;#039; type &amp;#039;&amp;#039;framer-802154&amp;#039;&amp;#039; is defined by &amp;#039;&amp;#039;core/net/mac/framer-802154.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/framer-802154.h&amp;#039;&amp;#039;. It creates and parses frames compatible with standard IEEE 802.15.4 (2003). The create and parse function manipulate all header information abstractedly through the &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure. The real work is done by functions located in the file &amp;#039;&amp;#039;core/net/mac/frame802154.c&amp;#039;&amp;#039;, which are responsible for extracting all information from receive/transmit buffer and inserting/extracting them appropriately into &amp;#039;&amp;#039;packetbuf&amp;#039;&amp;#039; structure.&lt;br /&gt;
&lt;br /&gt;
== RDC protocols ==&lt;br /&gt;
&lt;br /&gt;
The RDC struct, called &amp;#039;&amp;#039;struct rdc_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/rdc.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a RDC (radio duty cycling) driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct rdc_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the RDC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet list */&lt;br /&gt;
  void (* send_list)(mac_callback_t sent_callback, void *ptr, struct rdc_buf_list *list);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Function input is called when a new packet has arrived and need to be processed by RDC layer. And functions send and send_list are called for transmiting out-going packets.&lt;br /&gt;
&lt;br /&gt;
There are 2 very basic types of RDC layers implemented in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: &amp;#039;&amp;#039;nullrdc-noframer&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc-noframer.c&amp;#039;&amp;#039;) and &amp;#039;&amp;#039;nullrdc&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/nullrdc.c&amp;#039;&amp;#039;). The first one does not use Framer functions, and transmits/receives data directly to Radio layer (Physical). The developer can take total control over the format of the packets being transmitted. &lt;br /&gt;
&lt;br /&gt;
The second RDC layer (nullrdc) uses the Framer functions for header creation/parsing. It does not save energy and works as a pass-through layer that only transmits a packet and returns the results of such transmission (success or collision).&lt;br /&gt;
&lt;br /&gt;
A third simple RDC layer is &amp;#039;&amp;#039;sicslowmac.c&amp;#039;&amp;#039; (&amp;#039;&amp;#039;core/net/mac/sicslowmac.c&amp;#039;&amp;#039;). This is a RDC layer with no energy savings and that uses IEEE 8021.5.4 frames.&lt;br /&gt;
&lt;br /&gt;
The three last protocols available in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039; are implementations of popular asynchronous MAC protocols that aim at energy saving; namely X-MAC, ContikiMac and LPP. X-MAC is a short-preambule protocol from 2006 that was ported to ContikiOS [1]. X-MAC has two different implementation in ContikiOS, which can be found in files &amp;#039;&amp;#039;core/net/mac/cxmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;core/net/mac/xmac.c&amp;#039;&amp;#039;. ContikiMac [2] is a protocol that proposes enhancement over X-MAC. The implementation can be found in file &amp;#039;&amp;#039;core/net/mac/contikimac.c&amp;#039;&amp;#039;. Finally, LPP (Low Power Probing) is a low power protocol from 2008 that was also ported to ContikiOS [3]; the code can be found in &amp;#039;&amp;#039;core/net/mac/lpp.c&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
Each of the last three RDC protocols can be used for energy saving. The details of each of these protocols is outside the scope of this tutorial and can be found in the references.&lt;br /&gt;
&lt;br /&gt;
== MAC protocols ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The MAC struct, called &amp;#039;&amp;#039;struct mac_driver&amp;#039;&amp;#039; can be found in file &amp;#039;&amp;#039;core/net/mac/mac.h&amp;#039;&amp;#039;. It is shown below.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/**&lt;br /&gt;
 * The structure of a MAC protocol driver in Contiki.&lt;br /&gt;
 */&lt;br /&gt;
struct mac_driver {&lt;br /&gt;
  char *name;&lt;br /&gt;
&lt;br /&gt;
  /** Initialize the MAC driver */&lt;br /&gt;
  void (* init)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Send a packet from the Rime buffer  */&lt;br /&gt;
  void (* send)(mac_callback_t sent_callback, void *ptr);&lt;br /&gt;
&lt;br /&gt;
  /** Callback for getting notified of incoming packet. */&lt;br /&gt;
  void (* input)(void);&lt;br /&gt;
  &lt;br /&gt;
  /** Turn the MAC layer on. */&lt;br /&gt;
  int (* on)(void);&lt;br /&gt;
&lt;br /&gt;
  /** Turn the MAC layer off. */&lt;br /&gt;
  int (* off)(int keep_radio_on);&lt;br /&gt;
&lt;br /&gt;
  /** Returns the channel check interval, expressed in clock_time_t ticks. */&lt;br /&gt;
  unsigned short (* channel_check_interval)(void);&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
There are only two types of MAC protocols in &amp;#039;&amp;#039;ContikiOS&amp;#039;&amp;#039;: nullmac (&amp;#039;&amp;#039;core/net/mac/nullmac.c&amp;#039;&amp;#039;) and csma (&amp;#039;&amp;#039;core/net/mac/csma&amp;#039;&amp;#039;). The first one is a simple pass-through protocol that simply calls the appropriate RDC functions. And the second one implements addressing, sequence number and retransmissions. CSMA protocol keep a list of packets to each of the neighbors and calculate statistics such as number of retransmissions, collisions, deferrals, etc.&lt;br /&gt;
&lt;br /&gt;
== How to choose and change your protocols ==&lt;br /&gt;
&lt;br /&gt;
All three stack layers (Framer, RDC and MAC) need to be defined during compilation time. They are define using pragma &amp;#039;&amp;#039;#define&amp;#039;&amp;#039; either in the Makefile or in the &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file. The definitions of which protocols should be used are used by file &amp;#039;&amp;#039;core/net/netstack.h&amp;#039;&amp;#039; that effectively binds the NETSTACK_FRAMER, NETSTACK_RDC and NETSTACK_MAC to the protocols that are supposed to be used.&lt;br /&gt;
&lt;br /&gt;
Below it is shown the piece of code responsible for setting the MAC layer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_NETWORK&lt;br /&gt;
#ifdef NETSTACK_CONF_NETWORK&lt;br /&gt;
#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK&lt;br /&gt;
#else /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#define NETSTACK_NETWORK rime_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_NETWORK */&lt;br /&gt;
#endif /* NETSTACK_NETWORK */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Initially the code checks if NETSTACK_NETWORK was already define (#ifndef NETSTACK_NETWORK). If it was not, the code then checks if NETSTACK_CONF_NETWORK was define. If NETSTACK_CONF_NETWORK was defined, the desired network protocol is used (#define NETSTACK_NETWORK NETSTACK_CONF_NETWORK); otherwise a default protocol is used, which in this case is &amp;#039;&amp;#039;rime_driver&amp;#039;&amp;#039;. &lt;br /&gt;
&lt;br /&gt;
Hence, if you want to use a particular network protocol, you can either define NETSTACK_NETWORK directly or define NETSTACK_CONF_NETWORK.&lt;br /&gt;
&lt;br /&gt;
If no definitions are made, by default ContikiOS stack layers will be:&lt;br /&gt;
&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Network layer&amp;#039;&amp;#039;&amp;#039; - rime_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;MAC layer&amp;#039;&amp;#039;&amp;#039; - nullmac_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;RDC&amp;#039;&amp;#039;&amp;#039; - nullrdc_driver&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Framer&amp;#039;&amp;#039;&amp;#039; - framer_nullmac&lt;br /&gt;
# &amp;#039;&amp;#039;&amp;#039;Radio&amp;#039;&amp;#039;&amp;#039; - nullradio_driver&lt;br /&gt;
&lt;br /&gt;
The stack layers are usually define inside file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039;. There is one &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file for each platform; for instance, platform &amp;#039;&amp;#039;sky&amp;#039;&amp;#039; has &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; located inside folder &amp;#039;&amp;#039;platform/sky&amp;#039;&amp;#039;. Below we show the excerpt where stack layers are define:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef NETSTACK_CONF_MAC&lt;br /&gt;
#define NETSTACK_CONF_MAC     csma_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_MAC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC&lt;br /&gt;
#define NETSTACK_CONF_RDC     contikimac_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE&lt;br /&gt;
#define NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE 8&lt;br /&gt;
#endif /* NETSTACK_CONF_RDC_CHANNEL_CHECK_RATE */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_RADIO&lt;br /&gt;
#define NETSTACK_CONF_RADIO   cc2420_driver&lt;br /&gt;
#endif /* NETSTACK_CONF_RADIO */&lt;br /&gt;
&lt;br /&gt;
#ifndef NETSTACK_CONF_FRAMER&lt;br /&gt;
#define NETSTACK_CONF_FRAMER  framer_802154&lt;br /&gt;
#endif /* NETSTACK_CONF_FRAMER */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can see that, if we are using Tmote Sky hardware, by default the stack layer will be composed by &amp;#039;&amp;#039;csma_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;contikimac_driver&amp;#039;&amp;#039;, &amp;#039;&amp;#039;framer_802154&amp;#039;&amp;#039; and &amp;#039;&amp;#039;cc2420_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
=== Changing the protocols ===&lt;br /&gt;
&lt;br /&gt;
If you want to change the protocols to be used you define them either inside the Makefile or creating a project configuration file.&lt;br /&gt;
&lt;br /&gt;
In the first case you should change Makefile, inputing the definitions in one single line, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
DEFINES = NETSTACK_CONF_RDC=cxmac_driver,NETSTACK_CONF_MAC=null_mac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the protocols that you want to use should be defined in the &amp;#039;&amp;#039;DEFINES&amp;#039;&amp;#039; line, using comma to separate them.&lt;br /&gt;
&lt;br /&gt;
The second option is to create a project configuration file. Let&amp;#039;s assume the file create was named &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039;. This file should be inserted into your Makefile, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;sh&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI = /home/user/contiki&lt;br /&gt;
CFLAGS += -DPROJECT_CONF_H=\&amp;quot;project-conf.h\&amp;quot;&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Inside &amp;#039;&amp;#039;project-conf.h&amp;#039;&amp;#039; file you can define all protocols you want to use, as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#define NETSTACK_CONF_MAC nullrdc_driver&lt;br /&gt;
#define NETSTACK_CONF_RDC nullmac_driver&lt;br /&gt;
#define NETSTACK_CONF_FRAMER framer_nullmac&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span style=&amp;quot;color: red&amp;quot;&amp;gt;If you choose to use &amp;#039;&amp;#039;projet-conf.h&amp;#039;&amp;#039; file, you will need to modifed the file &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; inside your platform folder. If you are using sky platform, for example, you need to change file &amp;#039;&amp;#039;platform/sky/contiki-conf.h&amp;#039;&amp;#039;. The last part of &amp;#039;&amp;#039;contiki-conf.h&amp;#039;&amp;#039; file should be moved to the beginning of the file, as shown below&amp;lt;/span&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* -*- C -*- */&lt;br /&gt;
&lt;br /&gt;
#ifndef CONTIKI_CONF_H&lt;br /&gt;
#define CONTIKI_CONF_H&lt;br /&gt;
&lt;br /&gt;
/* include the project config */&lt;br /&gt;
/* PROJECT_CONF_H might be defined in the project Makefile */&lt;br /&gt;
#ifdef PROJECT_CONF_H&lt;br /&gt;
#include PROJECT_CONF_H&lt;br /&gt;
#endif /* PROJECT_CONF_H */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to code your own protocol ==&lt;br /&gt;
&lt;br /&gt;
We will explain here how to create a new protocol and integrate it into ContikiOS. The protocol can be a Network, MAC, RDC, Framer or Radio; the steps for creating any one of these protocols are similar. We are going to show in details how to create a new MAC protocol, called &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
First step is to create both header and c files for &amp;#039;&amp;#039;Modified MAC&amp;#039;&amp;#039; protocol. Both files need to be created inside &amp;#039;&amp;#039;core/net/mac&amp;#039;&amp;#039;. Our files will be named &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
The file &amp;#039;&amp;#039;modifiedmac.h&amp;#039;&amp;#039; is shown below. In this file we only declare an external variable that will reference our MAC protocols, called &amp;#039;&amp;#039;modifiedmac_driver&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef __MODIFIEDMAC_H__&lt;br /&gt;
#define __MODIFIEDMAC_H__&lt;br /&gt;
&lt;br /&gt;
#include &amp;quot;net/mac/mac.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
extern const struct mac_driver modifiedmac_driver;&lt;br /&gt;
&lt;br /&gt;
#endif /* __MODIFIEDMAC_H__ */&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
An initial version of file &amp;#039;&amp;#039;modifiedrdc.c&amp;#039;&amp;#039; is show below. It only contains the declaration of all functions that must be implemented in a RDC driver.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;quot;net/mac/modifiedmac.h&amp;quot;&lt;br /&gt;
#include &amp;quot;net/netstack.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static unsigned short&lt;br /&gt;
channel_check_interval(void)&lt;br /&gt;
{&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
init(void)&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
const struct mac_driver modifiedmac_driver = {&lt;br /&gt;
  &amp;quot;modifiedmac&amp;quot;,&lt;br /&gt;
  init,&lt;br /&gt;
  send_packet,&lt;br /&gt;
  packet_input,&lt;br /&gt;
  on,&lt;br /&gt;
  off,&lt;br /&gt;
  channel_check_interval,&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The next step is to include our source code into ContikiOS make system. We just need to edit the file &amp;#039;&amp;#039;core/net/mac/Makefile.mac&amp;#039;&amp;#039; and include the file &amp;#039;&amp;#039;modifiedmac.c&amp;#039;&amp;#039; in the list of Contiki source files. After editing, &amp;#039;&amp;#039;Makefile.mac&amp;#039;&amp;#039; must look like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
CONTIKI_SOURCEFILES += cxmac.c xmac.c nullmac.c lpp.c frame802154.c sicslowmac.c nullrdc.c nullrdc-noframer.c mac.c&lt;br /&gt;
CONTIKI_SOURCEFILES += framer-nullmac.c framer-802154.c csma.c contikimac.c phase.c modifiedmac.c&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Below we show the functions that should be changed in order to make our MAC protocol functional. It is going to work exatcly as a Null MAC protocol and we are counting the number of packets received and transmitted.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
uint16_t n_pkts_transmitted = 0;&lt;br /&gt;
uint16_t n_pkts_received = 0;&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
send_packet(mac_callback_t sent, void *ptr)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_transmitted++;&lt;br /&gt;
  NETSTACK_RDC.send(sent, ptr);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static void&lt;br /&gt;
packet_input(void)&lt;br /&gt;
{&lt;br /&gt;
  n_pkts_received++;&lt;br /&gt;
  NETSTACK_NETWORK.input();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
on(void)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.on();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
static int&lt;br /&gt;
off(int keep_radio_on)&lt;br /&gt;
{&lt;br /&gt;
  return NETSTACK_RDC.off(keep_radio_on);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Evaluating ContikiMAC and XMAC protocols ==&lt;br /&gt;
&lt;br /&gt;
We are going to evaluate the performance of the two most famous RDC protocols in ContikiOS, the ContikiMAC and X-MAC. We want to evaluate the Packet Reception Rate (PRR), which is the percentage of received packets in an interval; and also the average duty-cycle of sensor nodes. The PRR is related to the maximum throughput the network is able to handle; with lower PRR more packets are dropped and the total amount of data that can be transfered to the sink node is small. Also, the duty-cycle impacts the average power consumption and the lifetime of nodes, since the biggest energy savings are achieved when nodes are sleeping; the lower the duty-cycle the longest will be the node&amp;#039;s lifetime.&lt;br /&gt;
&lt;br /&gt;
We considered the firmware in the folder &amp;#039;&amp;#039;examples/ipv6/rpl-udp/&amp;#039;&amp;#039;, which is a firmware based on IPv6 and RPL for UDP packet transmission. In the same folder there is one simulation file called &amp;#039;&amp;#039;rpl-udp-powertrace.csc&amp;#039;&amp;#039; that is composed of 1 sink node and 30 sensor nodes and traces the energy consumption of all nodes, and also keeps track of PRR values throughout the simulation time. We considered the firmwares &amp;#039;&amp;#039;udp-server.c&amp;#039;&amp;#039; and &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; and reduced the data transmission interval in &amp;#039;&amp;#039;udl-client.c&amp;#039;&amp;#039; to 1 second. The line below shows where the code of &amp;#039;&amp;#039;udp-client.c&amp;#039;&amp;#039; was changed:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#ifndef PERIOD&lt;br /&gt;
#define PERIOD 1&lt;br /&gt;
#endif&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
By deafult the simulation is executed with ContikiMAC. We let the simulation run for 5 minutes before extracting the desired statistics. After simulatin for ContikiMAC, we changed the RDC protocols, as described before, to X-MAC. And the same statistics were obtained. The figures below show the average duty-cycle of each node for both protocols. The figure on the left shows results of ContikiMAC and the one on the right shows results of X-MAC.&lt;br /&gt;
&lt;br /&gt;
[[File:power_contikimac.png|300px]] [[File:power_xmac.png|300px]]&lt;br /&gt;
&lt;br /&gt;
The PRR statistics were also analysed for all 30 sensor nodes. On average the PRR of ContikiMAC was &amp;#039;&amp;#039;&amp;#039;50.48%&amp;#039;&amp;#039;&amp;#039; and the PRR of X-MAC was &amp;#039;&amp;#039;&amp;#039;30.38%&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
[http://www.cse.wustl.edu/~lu/cse521s/Papers/x-mac.pdf 1]- Buettner, Michael, Gary V. Yee, Eric Anderson, and Richard Han. &amp;quot;X-MAC: a short preamble MAC protocol for duty-cycled wireless sensor networks.&amp;quot; In Proceedings of the 4th international conference on Embedded networked sensor systems, pp. 307-320. ACM, 2006.&lt;br /&gt;
&lt;br /&gt;
[http://soda.swedish-ict.se/5128/1/contikimac-report.pdf 2] - Dunkels, Adam. &amp;quot;The contikimac radio duty cycling protocol.&amp;quot; (2011).&lt;br /&gt;
&lt;br /&gt;
[http://web.cs.jhu.edu/~cliang4/public/doc/2008_IPSN_Koala.pdf 3] - Liang, Chieh-Jan Mike, and Andreas Terzis. &amp;quot;Koala: Ultra-low power data retrieval in wireless sensor networks.&amp;quot; In Proceedings of the 7th international conference on Information processing in sensor networks, pp. 421-432. IEEE Computer Society, 2008.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
[[Contiki_tutorials | Back to Contiki Tutorials]]&lt;br /&gt;
&lt;br /&gt;
Edited by: Pedro&lt;/div&gt;</summary>
		<author><name>Pedro</name></author>	</entry>

	</feed>