IP multicasting


IP multicasting is used to target a group of hosts by sending a single datagram. IP addresses in the range 224.0.0.0 through 239.255.255.255 are reserved for multicasting.

To find out which hosts on your subnet support multicasting, try

ping 224.0.0.1

Here’s a Node.js code snippet that sends UDP datagrams to multicast group 225.0.0.1 at port 8001

var dgram = require('dgram');
var s = dgram.createSocket('udp4');
s.bind(8000);
var b = new Buffer("Hello");
s.send(b, 0, b.length, 8001, "225.0.0.1", function(err, bytes) {
  console.log("Sent " + bytes + " bytes");
  s.close();
});

A host that desires to receive a datagram sent to a multicast group, must first request membership to that group. Here’s a Node.js code snippet that receives datagram sent by the code above

var dgram = require('dgram');
var s = dgram.createSocket('udp4');
s.bind(8001, function() {
  s.addMembership('225.0.0.1');
});
s.on("message", function (msg, rinfo) {
  console.log("server got: " + msg + " from " +
    rinfo.address + ":" + rinfo.port);
});

.NET code that does something similar can be found in the UDP Tool at GitHUb.

Receiving multicasts on Linux does not work when you bind the socket to a specific interface, for instance s.bind(8001, 192.168.1.1… does not work. It looks like a Linux-only quirk because it happens with both the Mono .NET runtime and Node.js.

Another quirk observed on Linux is the need to add a route to forward multicast IP packets to a wireless LAN interface that is used in access point mode, in tandem with hostapd

route add -net 225.0.0.0 netmask 255.0.0.0 gw 192.168.1.1
Posted in .NET, JavaScript, Network

Mono .NET runtime on Raspberry Pi with Buildroot


Mono is a popular way to run .NET applications on platforms other than Windows. In this post I’ll show how to build and copy Mono onto a Raspberry Pi, with Buildroot.

Obtain source code for Mono

Let’s start by obtaining Mono from GitHub. I suggest using a folder outside of Buildroot. Later, we’ll prepare Buildroot to fetch the source code from this folder.

git clone https://github.com/mono/mono.git

Let’s head into the cloned repo. We’ll need to switch to a different branch. After much trial and error I’ve discovered that branch mono-3.6.0-branch builds without any error

cd mono
git checkout mono-3.6.0-branch

Create a package for Mono in Buildroot

We’ll create a new package called mono in Buildroot, that will fetch the source code for Mono from the cloned repo, and prepare and build it.

The location of source code for the mono package needs to be specified in file local.mk in Buildroot’s root folder

MONO_OVERRIDE_SRCDIR = /home/parallels/mono/

Add a new package called mono to package/Config.in. I’ve added it under menu “Interpreter languages and scripting”, but you can choose another menu

menu "Interpreter languages and scripting"
        source "package/mono/Config.in"

We’ll create a new folder called mono under folder package, and make a configuration file called Config.in

mkdir package/mono
vi package/mono/Config.in

Here’s the content of Config.in

config BR2_PACKAGE_MONO
	bool "mono"
	help
	  Sponsored by Xamarin, Mono is an open source implementation of Microsoft's .NET Framework based on the ECMA standards for C# and the Common Language Runtime.

https://github.com/mono/mono

Next, we’ll tell Buildroot how to build the mono package

vi package/mono/mono.mk

Here’s the content for package/mono/mono.mk

################################################################################
#
# mono
#
################################################################################

MONO_AUTORECONF = YES
MONO_DEPENDENCIES = libglib2

$(eval $(autotools-package))

Configure Buildroot and perform the build

Now that package mono has been created, select it by invoking the configuration menu

make menuconfig

You’ll also need to switch to glibc under Toolchain. Build with uClibc fails with

os_dep.c:3990:23: execinfo.h: No such file or directory

glibc

Now, all that’s left to do is to perform the build

make clean
make

After the build, prepare the SD card. Mono’s build does not copy .NET core libraries to the target filesystem. You’ll need to copy those manually, like so

sudo cp -R output/build/mono-custom/mcs/class/lib/basic/* /media/parallels/System/usr/lib/mono/2.0

If you forget to do that, .NET applications will fail with a message such as

The assembly mscorlib.dll was not found or could not be loaded.
It should have been installed in the `/usr/lib/mono/2.0/mscorlib.dll' directory.

Other notes

Builds on branches mono-3.8.0-branch and mono-3.10.0-branch fail with link time error

mini.c:(.text+0x7878): undefined reference to `mono_cross_helpers_run'
Posted in .NET, Linux, Raspberry Pi

Enable Debugging on Raspberry Pi with Buildroot


This is a quick post that shows how to enable gdb and/or gdbserver on the Raspberry Pi when using Buildroot.

Enable option Build packages with debugging symbols, under Build Options

debugging symbols

Enable the following options under Toolchain

  • Build cross gdb for the host - to facilitate remote debugging
  • Thread library debugging – to enable adding gdb and gdbserver packages to the device’s root file system

cross gdb on host

Thread library debugging

Select gdb and gdbserver (useful for remote debugging), under Target packages, “Debugging, profiling and benchmark”

gdb and gdbserver

Posted in Buildroot, Raspberry Pi

Getting Started with Bluetooth Low Energy by Kevin Townsend et al; O’Reilly Media


Getting Started with Bluetooth Low Energy, Tools and Techniques for Low-Power Networking

Bluetooth LE, or Bluetooth Smart as it is officially known, has generated a lot of interest for all the good reasons. Getting Started with Bluetooth Low Energy by Kevin Townsend et al is a solid guide to the topic, along with other good books such as Bluetooth Low Energy: The Developer’s Handbook.

The book begins by discussing the key characteristics, limitations, and use cases of Bluetooth LE technology, in chapter 1. The following four chapters take a deep dive into the protocols that comprise Bluetooth LE, beginning with an overview in Chapter 2. Chapter 3 delves into the Generic Attribute Profile or GAP (advertising and connections). Chapter 4 delves into Generic Attribute Profile or GATT (services and characteristics).

Makers need to select hardware to leverage Bluetooth LE. It may come in the form of a module that you program to act as a peripheral, or a USB dongle that you may plug into a USB host. Chapter 5 discusses several such options. Chapters 6 and 7 delve into debugging and design tools that aid developers during development and troubleshooting.

Chapter 8 shows how to leverage Bluetooth LE on Android using the Bluetooth Application Accelerator library from the Bluetooth SIG. You’ll learn how to establish connection and communicate with a remote device. Chapter 9 delves into Bluetooth LE programming on iOS by demonstrating several practical applications.

Chapter 10, the concluding chapter, shows how to leverage Bluetooth LE on embedded devices. It uses a hardware module introduced in chapter 5, in conjunction with ARM’s mbed platform, to build a peripheral that can be used with Android and iOS devices.

All source code shown in the book can be forked from the author’s GitHub repo. The text has occasional spelling mistakes that don’t affect readability.

I’d like to thank O’Reilly Media for providing an e-book for review.

Posted in Bluetooth, Book, Hardware, Review

Wireless Router with Buildroot and Raspberry Pi


This post shows how to build a basic wireless router, using Raspberry Pi. It is driven by a custom Linux system built with Buildroot 2014.08, that boots up in no time at all.

I refer you to the following posts

Add all appropriate Kernel modules and Buildroot packages mentioned in these posts before proceeding.

Kernel Configuration

I’m using a different USB Wi-Fi adapter here. It has the RTL8188CUS chipset that supports access point mode. The Kernel driver is Realtek 8192C USB WiFi, available under Device Drivers, Network device support, Wireless LAN

Realtek 8192C USB WiFi

Package Configuration

Invoke make menuconfig within the buildroot folder from a command prompt. Under Target Packages, select option Show packages that are also provided by busybox

BR2_PACKAGE_BUSYBOX_SHOW_OTHERS

Under Target packages, Networking application, select dhcp and its sub-packages

BR2_PACKAGE_DHCP

If you’re using a different wireless adapter, select hostapd and its sub-packages

BR2_PACKAGE_HOSTAPD

Perform build by invoking make, copy the newly minted system to an SD card, and use it to boot up your Raspberry Pi.

Custom hostapd

The hostapd built by Buildroot does not work with the chosen Wi-Fi adapter. We need to build the hostapd module provided by Realtek for RTL8188CUS.

Download the driver package from Realtek. It contains the GPL source code for hostapd. In my case, I expanded RTL8188C_8192C_USB_linux_v4.0.2_9000.20130911.zip, then expanded wpa_supplicant_hostapd-0.8_rtw_r7475.20130812.tar.gz under folder wpa_supplicant_hostapd.

Proceed to folder hostapd from the command line, and use the toolchain built by Buildroot to build hostapd

export PATH=/home/parallels/buildroot-2014.08/output/host/usr/bin:$PATH
make CC=arm-buildroot-linux-uclibcgnueabi-gcc

Copy hostapd binary to SD card.

Router Setup

Bring up wireless interface with a static IP address

ifconfig wlan0 up 192.168.2.1 netmask 255.255.255.0

Configure and bring up dhcpd. Edit /etc/dhcp/dhcpd.conf. Edit following lines so that they are commented (as shown)

#option domain-name "example.org";
#option domain-name-servers ns1.example.org, ns2.example.org;

Edit following line so that it is uncommented (as shown)

authoritative;

Add following lines at the end

subnet 192.168.2.0 netmask 255.255.255.0 {
  range 192.168.2.10 192.168.2.50;
  option broadcast-address 192.168.2.255;
  option routers 192.168.2.1;
  default-lease-time 600;
  max-lease-time 7200;
  option domain-name "local";
  option domain-name-servers 8.8.8.8, 8.8.4.4;
}

Create leases file

touch /var/lib/dhcp/dhcpd.leases

Instantiate dhcpd

dhcpd -cf /etc/dhcp/dhcpd.conf

Configure and bring up hostapd. Create file /etc/hostapd/hostapd.conf with

interface=wlan0
driver=rtl871xdrv
ssid=pi
hw_mode=g
channel=6
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=raspberry
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP

Run hostapd

hostapd -B /etc/hostapd/hostapd.conf

Try driver=nl80211 if you’re using a new netlink interface compatible driver. If you get the following error when starting hostapd, you probably need a different driver

Configuration file: /etc/hostapd/hostapd.conf
Line 2: invalid/unknown driver 'nl80211'
1 errors found in configuration file '/etc/hostapd/hostapd.conf'

If you get the following error using the Wi-Fi adapter mentioned earlier, you need to use the Realtek hostapd compiled above, with driver=rtl871xdrv in hostapd.conf

rfkill: Cannot open RFKILL control device
nl80211: Could not configure driver mode
nl80211 driver initialization failed.
hostapd_free_hapd_data: Interface wlan0 wasn't started

Join the pi network from any other device using password raspberry, and you’re good to go. Configure NAT routing, if you want to access internet over the wired ethernet interface.

Posted in Buildroot, Hardware, Linux, Raspberry Pi

Bluetooth on Raspberry Pi with Buildroot


This post shows how to build Bluetooth support with Buildroot for a Raspberry Pi. You’ll need a Bluetooth adapter such as the Bluetooth 4.0 USB Module from Adafruit. That adapter is also Bluetooth Smart (LE) ready.

I recommend using Buildroot 2014.08 because bluez-tools such as bt-adapter and bt-agent work with its version of Bluez.

Kernel Configuration

Enable Bluetooth subsystem support under Networking support

Bluetooth subsystem support

Enable RFCOMM  protocol support under Bluetooth subsystem support, if you want Bluetooth SPP support. Enable BNEP protocol support, if you want support for networking over Bluetooth. Enable HIDP protocol support, if you want support for Bluetooth enabled keyboard and mice.

RFCOMM and BNEP support

Since we’re using a USB adapter, enable HCI USB driver under Bluetooth subsystem support, Bluetooth device drivers

HCI USB driver

Buildroot Packages

The Kernel Bluetooth stack is called BlueZ. BlueZ also provides a set of utilities that can be used from the command line. To add those, select bluez-utils under Target packages, Networking applications

bluez-utils

If using Buildroot 2014.08, select bluez-utils 5.x package instead

bluez5_utils

make the Linux system and copy to SD card.

Useful commands

Look for interfaces

hciconfig -a

Bring up HCI interface

hciconfig hci0 up

Make the device discoverable

hciconfig hci0 piscan

Scan available devices

hcitool scan

Browse capabilities of a device

sdptool browse id

Run bluetooth daemon

bluetoothd

If using Buildroot 2014.08

/usr/libexec/bluetooth/bluetoothd&

Browse local capabilities

sdptool browse local

Connect and pair with a device

bt-device -c 5C:0E:8B:03:6E:4E

Bluetooth LE commands

Scan for Bluetooth LE devices

hcitool lescan

Access services and characteristics provided by an LE peripheral

gatttool -b 7F:AE:48:2B:00:0C -t random -I

gatttool prompt supports a set of commands to interact with the peripheral

connect
primary
char-desc
char-read-hnd handle
char-write-req handle data

Enable advertising as an iBeacon

hciconfig hci0 leadv
hciconfig hci0 noscan
hcitool -i hci0 cmd 0x08 0x0008 1E 02 01 1A 1A FF 4C 00 02 15 E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61 00

Bluetooth is big. Go ahead and do something with it.

Posted in Bluetooth, Buildroot, Linux, Raspberry Pi

Customize Buildroot to build bluez-tools


I am building Bluetooth support into my custom embedded Linux system with Buildroot, but couldn’t find basic command line tools that work with newer versions of BlueZ. That is when I came across bluez-tools, a GSoC project that is able to fill the gap.

To build bluez-tools requires adding my own package to Buildroot. What follows is a brief description of how to do that.

Download source code of bluez-tools. I did this outside of the buildroot folder, by cloning from the author’s GitHub repo

git clone https://github.com/khvzak/bluez-tools.git

Add an override rule to file local.mk, in the root folder of buildroot. That tells buildroot not to download package source from the internet, but to copy the source from the folder created above

BLUEZ_TOOLS_OVERRIDE_SRCDIR = /home/parallels/github/bluez-tools/

Create a folder for bluez-tools package

mkdir package/bluez-tools

Create package/bluez-tools/bluez-tools.mk file for the package with

################################################################################
#
# bluez-tools
#
################################################################################

BLUEZ_TOOLS_DEPENDENCIES = dbus libglib2 readline
BLUEZ_TOOLS_AUTORECONF = YES

$(eval $(autotools-package))

BLUEZ_TOOLS_AUTORECONF tells buildroot to generate configure script and Makefile.in by invoking autoconf et al.

Create configuration file package/bluez-tools/Config.in for the package with

config BR2_PACKAGE_BLUEZ_TOOLS
	bool "bluez-tools"
	help
	  This is a GSoC'10 project to implement a new command line tools for bluez (bluetooth stack for linux). The project implemented in C and uses the D-Bus interface of bluez. 

https://code.google.com/p/bluez-tools/

Add the package to package/Config.in

source "package/bluez-tools/Config.in"

Enter configuration menu and select the package

make menuconfig

bluez-tools

Note that bluez-tools package is under Networking applications because I added it to menu “Networking applications” in package/Config.in

Invoke make to perform the build

make

If you get any build errors try

make clean
make

make clean will delete Linux kernel config. Either save the config, or do it all over again.

With Buildroot 2015.05, a command such as

bt-adapter -l

Fails with

bt-adapter: bluez service is not found
Did you forget to run bluetoothd?

bluetoothd is running just fine. Got to figure that one out.

It works all right with Buildroot 2014.08, but have noted the following issue with bt-device

# bt-device -c 5C:0E:8B:03:6E:4E
Connecting to: 5C:0E:8B:03:6E:4E
Device: CS3070:10172522500886 (5C:0E:8B:03:6E:4E)
Enter passkey: 1234
Segmentation fault

bt-device with -c option seems buggy and faults every single time. Debugging with gdb reveals the following backtrace

#0  0xb6e77d00 in g_utf8_validate () from /usr/lib/libglib-2.0.so.0
#1  0xb6e7b5c8 in g_variant_new_string () from /usr/lib/libglib-2.0.so.0
#2  0x0000c0b8 in _bt_agent_method_call_func (connection=<optimized out>,
    sender=<optimized out>, object_path=<optimized out>,
    interface_name=<optimized out>, method_name=0x18fc228 "RequestPinCode",
    parameters=0x1905f00, invocation=0x18fdab0, user_data=0x0)
    at lib/agent-helper.c:310

Analyzing line 310 in lib/agent-helper.c indicates a possible cause, passkey is being read into an uninitialized gchar pointer

if (scanf("%s", ret) == EOF && errno)

Should not be hard to fix.

Posted in Bluetooth, Linux, Raspberry Pi
Follow

Get every new post delivered to your Inbox.

Join 69 other followers

%d bloggers like this: