How to install 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

1. Follow steps B thru E in the guide below, and enable WPA2 encryption:

Guide: https://dl.dropboxusercontent.com/u/57289645/blog/dir-505l_openwrt_setup_guide.txt

uci set network.wan=interface
uci set network.wan.proto=dhcp
uci set network.wan.ifname=eth1
uci del network.lan.ifname
uci set wireless.@wifi-device[0].disabled=0
uci set wireless.@wifi-iface[0].encryption=psk2
uci set wireless.@wifi-iface[0].key="your_password"
uci commit


2. For USB storage support (assuming NTFS formatted USB stick):

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


3. For USB-RS232 (serial) adapter support (assuming adapter is Prolific PL2303 based):

opkg update
opkg install kmod-usb-serial kmod-usb-serial-pl2303 coreutils-stty
# reboot here
cat /dev/ttyUSB0 # see data coming to the serial port
stty -F /dev/ttyUSB0 # see serial port settings



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

   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!")


! 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


#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++)


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)
  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));