Friday, January 31, 2014

OpenWRT for WEMO - Part 3 - Cross compilation and remote debug with Eclipse

Being able to compile and remotely debug applications on the WEMO is very useful in creating custom applications for remotely switching the power, or adding functionality to the WEMO. For remote debugging the WEMO must be connected to the wifi network that the cross compilation machine is on and the machine must be able to ssh into the WEMO. 

Here are some example settings to get the WEMO connected to a typical WPA-PSK wifi network. These settings can be modified over the serial terminal.
  • Edit /etc/config/network with vi and comment every line with # then add the follwing.
    config interface 'lan'
        option ifname 'wlan0'
        option proto 'dhcp'
        option hostname 'WEMO'

  • Edit /etc/config/wireless with vi and change it to the following, note change the SSID and password to that of your Wifi network.

    config wifi-iface
        option device 'radio0'
        option network 'lan'
        option mode 'sta'
        option ssid 'SSID'
        option encryption 'psk2'
        option key 'password'

  • Reboot the router and check over the serial terminal that the wlan0 interface has been assigned an ip address.

Using the custom compiled firmware from part 1 the required packages for remote debugging will be installed. If using the base image use the following command to install the required packages.  
opkg update
opkg install gdb-server openssh-sftp-server

The following steps setup Eclipse to cross compile a simple c project.

  1. Start Eclipse and navigate to "Help" menu and select "Install New Software...". In the install wizard select "--All Available Sites--" from the drop down menu next to "Work with:" at the top. This will load all available software packages that can be install in Eclipse and will take while.
  2. If your version of eclipse does not have CDT installed use the guide here to install it. Navigate to the "Mobile and Device Development" in the tree view and select "C/C++ GCC Cross Compiler Support", "Remote System Explorer End-User Runtime", and "C/C++ Remote Launch" as shown below.

    Click "Next >"  and follow the prompts in the wizard to install the packages. Eclipse may need to restart afterwards.
  3. In this example we will be creating a "C Project" that will cross compile C code only. Select "File" then "New" and finally "Project..." to bring up the "New Project" wizard. Choose the "C/C++" option in the tree view and then select "C Project" as shown below.

    Click "Next >" to proceed.
  4.  Select an "Empty Project" from "Executable" under "Project type:" and "Cross GCC" under "Toolchians:" as shown below.

    Choose a name for the project and enter it at the top then click "Next >" to proceed.
  5. In the final stage of the wizard (the "Finnish button" shown appear) click the "Advanced Settings" button. This will bring up a dialogue to edit the project settings, which can be accessed by right clicking on the project and selecting "Properties". The next steps deal with this dialogue and show the parameters that need to be added to configure the cross compiler.
  6. The staging directory in the openwrt folder contains the cross compiler and gdb for remote debugging. The first step is to configure the cross compiler, and this is under "C/C++ Build" then "Settings" on the left. Choose "Cross Settings" as shown in the image below.

    The toolchains directory depends on the architecture and gcc version. Locate the toolchain directory by looking in the staging directory of openwrt and set it to the path. The prefix is an appended string for the cross compilation tools such as gcc, this can be found by looking in the bin directory of the toolchain. Note all directories must be absolute and directories like "~/openwrt/..." will not work.
  7. The include directories can be specified to enable better code completion by selecting Includes as shown in the image below.

    Select the add button (upper right in the image) and locate the include directory in the toolchain directory.
  8. An environment variable "STAGING_DIR" is required by the cross compiler and can be set by navigating to "C/C++ Build" then "Environment" on the left as shown below.

    To add the environment variable click the "Add" and set the name as "STAGING_DIR" and the value as the path to the OpenWRT staging directory.
  9. Finnaly click "OK" to save the project settings and finally "Finish" to create the project.
  10. To test the cross compiler create a "main.c" file in the project by right clicking the project and selecting "New" then "File". Use the following code as a simple hello world.

    int main()
        printf("hello world\r\n");
        return 0;

  11. Save the main.c file then right click the project and select build. The output of the build (in the "Console" tab down the bottom) show a successful build, for example:

    **** Build of configuration Debug for project hello_world ****

    make all
    Building file: ../main.c
    Invoking: Cross GCC Compiler
    mipsel-openwrt-linux-gcc -I/home/sean/openwrt/openwrt/staging_dir/toolchain-mipsel_24kec+dsp_gcc-4.6-linaro_uClibc- -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"main.d" -MT"main.d" -o "main.o" "../main.c"
    Finished building: ../main.c

    Building target: hello_world
    Invoking: Cross GCC Linker
    mipsel-openwrt-linux-gcc  -o "hello_world"  ./main.o  
    Finished building target: hello_world

    **** Build Finished ****

    If there are any problems with the build, check to ensure the paths to the tools are correct. If the path to the build tools requires changing then change the "PATH" environment variable, as changing the setting in step 6 will not update the environment variable.
To remotely debug a cross compiled project use the steps below.
  1. Ensure the "main.c" file in cross compiled project is selected in the "Project Explorer" then open the debug configuration by clicking the arrow next to the debug button and then selection "Debug Configurations..." as shown below.

    This will open the Debug configuration dialouge.
  2. Create a new "C/C++ Remote Application" configuration by clicking the button shown in the image below.

    This will create a new debug configuration in the left pane.
  3. Set the "Remote Absolute File Path for C/C++ Application:" to a simple path such as /tmp/hello_world. It is better to use the tmp directory as it resides in ram making upload faster and decreases wear on the flash.
  4. Click the "New..." button next to the combo box for "Connection:" to add a connection to the WEMO. Select a "Linux" connection in the wizard as shown below, then click "Next".

  5. Give the the IP address of the WEMO on your WiFi network for the "Host Name:", and optionally a friendly "Connection name:" as shown below.

    Do not click "Finnish", instead click "Next".
  6. Select "ssh.files" from the "Configuration" check list as shown below.

    Proceed by clicking "Next".
  7. Select "" as shown below then click "Next".

  8. Select "ssh.shells" as shown below then "Finish" to complete the wizard.

    This creates a new option in the combo box next to "Connection:" for the WEMO in the debug configuration.
  9. Select the newly created connection from the "Connection:" comb box. Select "Enable auto build" to enable building before debugging.
  10. Navigate to the Debugger tab (at the top), click "Browse..." next to the "GDB debugger" option and select the cross compiled GDB debugger in the toolchains bin dierctory. The GDB debugger is cross compiled as a part of the toolchain, and is located in the bin folder of the toolchain directory used earlier. The GDB executable is prefixed like all the other tools.
  11. Click the "Debug" button in the right hand corner to intiate a remote debug session. A prompt for the ssh login will appear if the connection is successful. After logging in the executable will be uploaded and debugging session started. The program should stop on a break point on the first line of code.
The console tab provides access to the remote shell session but must be selected by clicking the arrow next to the console button and selecting the remote shell option as shown below.

If you step through the hello world program, hello world will appear in this console.

If an "Error during file upload" occurs this can mean an existing debug session has not terminated correctly. This requires the termination of the gdbserver process, which can be done in the "Remote Systems" tab. To enable the "Remote Systems" tab, open the "Window" menu and select "Show View" then "Other...". In the dialogue use the search button to find the "Remote Systems" tab. The gbdserver process can be terminated by selecting the connection to the WEMO in the "Remote Systems" tab, then selecting processes. Find the gdbserver process in the treeview, right click, and select "Kill..." as shown below.

That completes a guide to cross compiling and remote debugging in eclipse. Next I will show how to interface with the gpio pins to switch the relay in the WEMO.


  1. I just bought a WeMo Light Switch. I'd love to continue/help this.

    Is part 2 now blocked by the firmware upgrade?

  2. Did the gpio article ever get finished? I picked up two wall switch wemo's and was going to hack on them a bit. Think they should be pretty similar.