7.01.2014

How to install/set-up OpenWrt on a D-Link DIR-505L wireless router

This guide is more for myself, but feel free to ask questions.

OpenWrt wiki page for the DIR-505: http://wiki.openwrt.org/toh/d-link/dir-505

A. Basic setup and housekeeping:

Follow steps B thru E in this guide: https://dl.dropboxusercontent.com/u/57289645/blog/dir-505l_openwrt_setup_guide.txt

# ethernet
uci set network.wan=interface
uci set network.wan.proto=dhcp
uci set network.wan.ifname=eth1
uci del network.lan.ifname
# wifi
uci set wireless.@wifi-device[0].disabled=0
uci set wireless.@wifi-iface[0].ssid="your_ssid"
uci set wireless.@wifi-iface[0].encryption="psk2"
uci set wireless.@wifi-iface[0].key="your_password"
# system
uci set system.@system[0].hostname="your_hostname"
uci set system.@system[0].timezone="PST8PDT,M3.2.0,M11.1.0" # Los Angeles
# commit changes
uci commit
wifi

Sources:
https://forum.openwrt.org/viewtopic.php?pid=230861#p230861
http://wiki.openwrt.org/doc/uci/wireless/encryption#configure.wpa2.psk
http://wiki.openwrt.org/doc/uci/system

B. USB storage support (assuming NTFS-formatted USB stick):

opkg update
opkg install kmod-usb-storage kmod-fs-ntfs ntfs-3g
mkdir -p /mnt/usb_drive
ntfs-3g /dev/sda1 /mnt/usb_drive -o rw,sync
# do stuff
umount /dev/sda1

Sources:
http://wiki.openwrt.org/doc/howto/usb.essentials
http://wiki.openwrt.org/doc/howto/usb.storage
http://wiki.openwrt.org/doc/howto/writable_ntfs

C. USB serial adapter support:

opkg update
opkg install kmod-usb-serial coreutils-stty
opkg install kmod-usb-serial-pl2303 # for Prolific PL2303-based devices
opkg install kmod-usb-acm # for devices using Abstract Control Model (ACM)
# reboot here
cat /dev/ttyUSB0 # view data coming to the serial port
stty -F /dev/ttyUSB0 -a # view serial port settings

Source:
http://wiki.openwrt.org/doc/hardware/port.serial#usb.enabled.routers

D. Minimal Python install with pyserial support

opkg update
opkg install python-mini pyserial
# termios.so is missing so we need to copy it from the full package
cd /tmp
opkg download python
tar -xzf python_*.ipk ./data.tar.gz
tar -xzf data.tar.gz ./usr/lib/python2.7/lib-dynload/termios.so -C /

Source:
http://kelvinsthunderstorm.com/omnimaopenwrt-and-xbee/

6.09.2014

Simple Fortran/C interoperability example using Fortran 2003

The following is a simple example of how Fortran (2003) and C computer languages can mix. Fortran 2003 introduced an intrinsic module called ISO_C_BINDING that provides kind constants for common C types, e.g. double == C_DOUBLE kind. By using an interface block and linking to a C object file, you can use C code in your Fortran projects. The "bind(C)" statement and "value" attribute are also parts of Fortran 2003; the first tells the compiler to bind that subroutine to the C function, and the second tells the compiler to use call by value instead of call by reference (the latter is the default for Fortran). test.f90:

program main

implicit none

interface
   subroutine print_string(string, length) bind(C)
      use, intrinsic :: iso_c_binding, only: C_CHAR, C_INT
      implicit none
      character(kind=C_CHAR), intent(in) :: string(*)
      ! value attribute == call by value instead of by reference
      integer(kind=C_INT), value, intent(in) :: length
   end subroutine print_string
end interface

call my_print_string("Hello, world!")

contains

! wrapper function to pass string and its length to C function
subroutine my_print_string(string)
   use, intrinsic :: iso_c_binding, only: C_CHAR
   implicit none
   character(kind=C_CHAR, len=*), intent(in) :: string
   call print_string(string, len(string))
end subroutine my_print_string

end program main

test.c:

#include <stdio.h>

void print_string(const char* str, const int len)
{
   // Fortran doesn't use null-terminated strings
   int i;
   for (i = 0; i < len; i++)
      putchar(str[i]);
}

Compile:

cc -c test.c
f95 -std=f2003 test.f90 test.o

$ ./a.out
Hello, world!

How to print or convert a Unicode code point to UTF-8 in C

I had to figure out the best way to unescape and print a Unicode code point in C this week. There are many different Unicode encodings (UTF-8, UTF-16, UTF-32, UCS-2, UCS-4, etc.) but UTF-8 is the most common. I whipped out some quick code and voila. The following can easily be changed to output the multi-byte sequence instead of putchar-ing it. If your console supports UTF-8, the following should properly display Unicode characters. Try print_code_point_as_utf8(0x20AC), which should be a Euro currency sign.

#include <stdio.h>

static void print_code_point_as_utf8(const unsigned int cp)
{
  if (cp < 128)
    putchar(cp);
  else if (cp < 2048)
  {
    putchar(192 | (cp >> 6));
    putchar(128 | (cp & 63));
  }
  else if (cp < 65536)
  {
    putchar(224 | (cp >> 12));
    putchar(128 | ((cp >> 6) & 63));
    putchar(128 | (cp & 63));
  }
  else if (cp < 1114112)
  {
    putchar(240 | (cp >> 18));
    putchar(128 | ((cp >> 12) & 63));
    putchar(128 | ((cp >> 6) & 63));
    putchar(128 | (cp & 63));
  }
}