Skip to content

Instantly share code, notes, and snippets.

@tcheneau
Last active December 18, 2015 08:49
Show Gist options
  • Save tcheneau/5757352 to your computer and use it in GitHub Desktop.
Save tcheneau/5757352 to your computer and use it in GitHub Desktop.
This script is an helper that sets up a IEEE 802.15.4 device on a Linux. It will properly initialize the 6LoWPAN stack and can even assign IPv6 addresses. This script is mostly useful when deploying a batch of devices.
#!/bin/sh
# Conditions Of Use
#
# This software was developed by employees of the National Institute of
# Standards and Technology (NIST), and others.
# This software has been contributed to the public domain.
# Pursuant to title 15 Untied States Code Section 105, works of NIST
# employees are not subject to copyright protection in the United States
# and are considered to be in the public domain.
# As a result, a formal license is not needed to use this software.
#
# This software is provided "AS IS."
# NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
# OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
# AND DATA ACCURACY. NIST does not warrant or make any representations
# regarding the use of the software or the results thereof, including but
# not limited to the correctness, accuracy, reliability or usefulness of
# this software.
# Tony Cheneau <tony.cheneau@nist.gov>
# Set the network specific parameters (this parameter could be overloaded with
# a CLI parameters)
IFACE="wpan0"
SERIFACE="/dev/ttyUSB0"
# Link Layer
CHANNEL="26"
PANID="0xaaaa"
SHORTADDRESS="1"
LONGADDRESS="aa:aa:aa:aa:aa:aa:aa:aa"
# IP layer
IPV6ADDRSUFFIX=""
print_usage() {
echo "Set a IEEE 802.15.4 Serial device" >&2
echo >&2
echo "-h this help message" >&2
echo "-c <channel> set the channel (default $CHANNEL)" >&2
echo "-p <panid> set the PAN Identifier, in hex (default $PANID)" >&2
echo "-s <shortaddress> set the short address of the node (default $SHORTADDRESS)" >&2
echo "-l <longaddress> set the long address of the node (default $LONGADDRESS)" >&2
echo "-a <suffix> set an IPv6 suffix and configure two addresses" >&2
echo " for the fe80::/64 and 2001::/64 prefix" >&2
echo " (if none is passed, no such address is assigned)" >&2
echo "-i <ifname> set the wireless interface name (default $IFACE)" >&2
echo "-d <devicename> set the serial port device name (default $SERIFACE)" >&2
echo >&2
echo "Example of use:"
echo "$0 -d /dev/ttyUSB1 -a 1 # this will assign the address fe80::1 and 2001::1 to the serial device /dev/ttyUSB1"
}
assertintnumber() {
if ! [[ "$1" =~ ^[0-9]+$ ]] ; then
echo "$1 is not a number, expected a number (e.g. 16)"; exit 1
fi
}
asserthexnumber() {
if ! [[ "$1" =~ ^0x[0-9a-fA-F]+$ ]] ; then
echo "$1 is not a hexadecimal number, expected a number (e.g. 0xff)"; exit 1
fi
}
assertlongaddr() {
if ! [[ "$1" =~ ^[[:xdigit:]]{1,2}(:[[:xdigit:]]{1,2}){7}$ ]] ; then
echo "$1 is not a hexadecimal number, expected a number (e.g. aa:bb:cc:dd:ee:ff:00:11)"; exit 1
fi
}
while getopts ":c:p:s:l:a:i:d:h" opt; do
case $opt in
c)
echo "Will set the channel to $OPTARG" >&2
assertintnumber $OPTARG
CHANNEL=$OPTARG
;;
p)
echo "Will set the PAN ID to $OPTARG" >&2
asserthexnumber $OPTARG
PANID=$OPTARG
;;
s)
echo "Will set the short address to $OPTARG" >&2
assertintnumber $OPTARG
SHORTADDRESS=$OPTARG
;;
l)
echo "Will set the long address to $OPTARG" >&2
assertlongaddr $OPTARG
LONGADDRESS=$OPTARG
;;
a)
echo "Will set the address suffix to $OPTARG" >&2
# TODO: perhaps an input checking method here as well
IPV6ADDRSUFFIX=$OPTARG
;;
i)
echo "Will set up a new network interface: $OPTARG" >&2
IFACE=$OPTARG
;;
d)
echo "Will read from the device $OPTARG" >&2
SERIFACE=$OPTARG
;;
h)
print_usage
exit 1
;;
\?)
echo "Invalid option: -$OPTARG, use -h for help" >&2
exit 1
;;
esac
done
#disable firewall
echo "purging IPv6 firewalling rules"
ip6tables -F
hash set-econotag.py > /dev/null 2>&1 && \
python $(which set-econotag.py) $SERIFACE $PANID $SHORTADDRESS $LONGADDRESS || \
echo "set-econotag.py not found in PATH, this can cause some trouble when using a real device"
izattach -b 921600 $SERIFACE
# this one is automatically assigned by the kernel when the device driver is loaded
PHYIFACE=`iz listphy|head -n 1| cut -d " " -f 1`
if [[ -z $PHYIFACE ]] ; then
echo "Unable to find a suitable device phy" >&2
echo "Did the device driver loaded properly ?" >&2
exit -1
fi
iz add $PHYIFACE
ip link set $IFACE address $LONGADDRESS
ifconfig $IFACE up
iz set $IFACE $PANID $SHORTADDRESS $CHANNEL
ip link add link $IFACE name lo$IFACE type lowpan
ip link set lo$IFACE address $LONGADDRESS
ip link set lo$IFACE up
if ! [[ -z $IPV6ADDRSUFFIX ]] ; then
# set extra IPv6 address
ip -6 addr add dev lowpan0 fe80::$IPV6ADDRSUFFIX/64
ip -6 addr add dev lowpan0 2001::$IPV6ADDRSUFFIX/64
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment