Thursday, September 18, 2014

AWM002 Video capturing via webcam

I was planning to use the AWM002 to control access to my home main door. Previously I was using Raspberry for doing same. As Raspberry cost is around 35$ and AWM002 cost is around 11$ (for 10 boards), I wanted to port my application to AWM002 board.

After booting up AWM002 with own OpenWRT kernel, I wanted to use it as video-cam monitor at door entrance. Initially I planned to use mjpg-streamer for OpenWRT. But mjpg-streamer is sending 0-size image on my system.

To isolate issue (whether it is in mjpg-streamer or my usb-webcam), I compiled motion  and deployed on AWM002 board. Now I can see the video perfectly on lan/smartphone via motion-program's-http-URL. I had to tweek following options of motion:
  • stream_localhost off   
    • So that one can access camera from PC/Smartphone
  • output_pictures off
  • output_all off                                                              
  • output_normal off                                                                        
  • output_motion off 
    • All of above options for disabling  the jpeg file generation

Now the AWM002 is running at 18% CPU loading (so enough juice is left for other tasks) with 320x240 resolution. CPU usage drops to 8% when, I make the resolution to 160x128.

I need to bother about CPU load as CPU load has direct effect on battery usage :)

Now steps are to port my python program (on Raspberry) to AWM002.

Monday, September 15, 2014

Able to deploy POCO framework on OpneWRT on AWM002

After successfully installing the OpenWRT linux kernel on AWM002, I configured the OpenWRT build system to have following functionality:

  • GPIO functionality exposed via Linux Kernel GPIO export route.
  • Configured OpenWRT build system to include POCO C++ framework 
    • Create an directory-entry in OpenWRT build root package/libs/poco/
    • Copied all files (actually 2 files)  from  OpenWRT package repo
  • Configured to Linux menuconfg to include to build following libraries
    • librt
    • libstdc++
    • libPoco*
  • My kernel config file

Friday, September 12, 2014

AWM002 booting OpenWRT kernel via tfftp

After successfully made the AWM002 up, I want to load OpenWRT kernel on the same module.

To do that we need to do following:

    • Ensure that you have setup LAN and connected to PC 
    • On PC  tftp server shouble be  running at port 69
    • Ensure that you load your OpenWRT kernel image in root directory (let us say your image name is '/linux_sep_11'
    • You may use OpenWRT kernel from following link
    • If you have any firewall, disable it for the time being.
      • In Fedora, you can do that by 'service iptables stop'
  • Ensure that you have setup serial cable and connected to PC . Ensure that you open a serial terminal and be sure that you have connected to device
  • Now power cycle the board
  • At the same time, press 4 in serial terminal. This should lead to U-Boot prompt 'RT5350 # '
  •  Issue following command to see environment:
    • RT5350 # printenv
      • bootcmd=tftp
      • bootdelay=5
      • baudrate=57600
      • ethaddr="00:AA:BB:CC:DD:10"
      • stdin=serial
      • stdout=serial
      • stderr=serial
      • ipaddr= <== this might be different like 192.168.x.x (module ip)
      • serverip=   <== this might be different like 192.168.x.x (PC ip)
    • Now change 'ipaddr' and 'serverip' to reflect above values, by following UBoot commands
      • setenv ipaddr 
      • setenv serverip
    • Also add extra (undocumented help option ) like (this will help to point to  '/linux_sep_11', otherwise tftp booting will point to 'test.bin' file ). OTOH, you can rename your '/linux_sep_11' to test.bin (to avoid this extra bootfile setting)
      • setenv  bootfile /linux_sep_11
      • printenv command should show all changed values
      • Now save values to flash by 'saveenv' command
      • Ensure that these values are persistent (check by using 'printenv' command
  • Now power-cycle and press 2 (on serial console to get U-Boot access) to get image from tftp-server.It will ask for confirmation to overwrite flash. Say yes & finally you see OpenWRT booting up.

  • WARNING:  Please keep in mind that you may end bricking up your module in doing these activities and I am no way responsible for that :) . BTW, with these steps on my environment, I have not bricked my AWM002 module

                  Wednesday, September 10, 2014

                  AWM002 serial bootup output

                  • Ensure that you have cutecom is installed for GUI-based terminal viewer.
                  • Configure for 57600 baud for Serial port parameter and chose appropriate serial port (some thing like /dev/ttyUSB0  -- see dmesg output)
                  • Power up AWM002 module

                • You can see following output from serial console

                  U-Boot 1.1.3 (Jul 29 2013 - 10:29:51)
                  Board: Ralink APS
                  oC DRAM:  32 MB
                  relocate_code Pointer at: 81fb4000
                  Software System Reset Occurred
                  spi_wait_nsec: 42 
                  spi device id: c2 20 17 c2 20 (2017c220)
                  find flash: MX25L6405D
                  raspi_read: from:30000 len:1000 
                  .*** Warning - bad CRC, using default environment
                  Ralink UBoot Version:
                  ASIC 5350_MP (Port5<->None)
                  DRAM_CONF_FROM: Boot-Strapping 
                  DRAM_TYPE: SDRAM 
                  DRAM_SIZE: 256 Mbits
                  DRAM_WIDTH: 16 bits
                  DRAM_TOTAL_WIDTH: 16 bits
                  TOTAL_MEMORY_SIZE: 32 MBytes
                  Flash component: SPI Flash
                  Date:Jul 29 2013  Time:10:29:51
                  icache: sets:256, ways:4, linesz:32 ,total:32768
                  dcache: sets:128, ways:4, linesz:32 ,total:16384 
                   ##### The CPU freq = 360 MHZ #### 
                   estimate memory size =32 Mbytes
                  Please choose the operation: 
                     1: Load system code to SDRAM via TFTP. 
                     2: Load system code then write to Flash via TFTP. 
                     3: Boot system code via Flash (default).
                     4: Entr boot command line interface.
                     7: Load Boot Loader code then write to Flash via Serial. 
                     9: Load Boot Loader code then write to Flash via TFTP. 
                  \0x08\0x08\0x08 4 
                  \0x08\0x08\0x08 3 
                  \0x08\0x08\0x08 2 
                  \0x08\0x08\0x08 1 
                  \0x08\0x08\0x08 0 
                  3: System Boot system code via Flash.
                  ## Booti
                  ng image at bc050000 ...
                  raspi_read: from:50000 len:40 
                  .   Image Name:   Linux Kernel Image
                     Image Type:   MIPS Linux Kernel Image (lzma compressed)
                     Data Size:    3830574 Bytes =  3.7 MB
                     Load Address: 80000000
                     Entry Point:  802cf000
                  raspi_read: from:50040 len:3a732e 
                  .................................   Verifying Checksum ... 
                     Uncompressing Kernel Image ... 
                  No initrd
                  ## Transferring control to Linux (at address 802cf
                  000) ...
                  ## Giving linux memsize in MB, 32
                  Starting kernel ...
                  LINUX started...
                   THIS IS ASIC
                  Linux version 2.6.21 (ray@asiarf-Aspire-S3) (gcc version 3.4.2) #334 Tue Nov 26 14:28:26 CST 2013
                   The CPU frequency set to 360 MHz
                  CPU revision is: 0001964c
                  Determined physical RAM map:
                   memory: 02000000 @ 00000000 (usable)
                  Initrd not found or empty - disabling initrd
                  Built 1 zonelists.  Total pages: 8128
                  Kernel command line: console=ttyS1,57600n8 root=/dev/ram0
                  Primary instruction cache 32kB, physically tagged, 4-way, linesize 32 bytes.
                  Primary data cache 16kB, 4-way, linesize 32 bytes.
                  Synthesized TLB refill handler (20 instructions).
                  Synthesized TLB load handler fastpath (32 instructions).
                  Synthesized TLB store handler fastpath (32 instructions).
                  Synthesized TLB modify handler fastpath (31 instructions).
                  Cache parity protection disabled
                  cause = 40808008, status = 11000000
                  PID hash table entries: 128 (order: 7, 512 bytes)
                  calculating r4koff... 000000c8(200)
                  CPU frequency 0.05 MHz
                  Using 0.050 MHz high precision timer.
                  Dentry cache hash table entries: 4096 (order: 2, 16384 bytes)
                  Inode-cache hash table entries: 2048 (order: 1, 8192 bytes)
                  Memory: 26652k/32768k available (2198k kernel code, 6116k reserved, 673k data, 2856k init, 0k highmem)
                  Mount-cache hash table entries: 512
                  NET: Registered protocol fa
                  mily 16
                  SCSI subsystem initialized
                  usbcore: registered new interface driver usbfs
                  usbcore: registered new interface driver hub
                  usbcore: registered new device driver usb
                  deice id : c2 20 17 c2 20 (2017c220)
                  MX25L6405D(c2 2017c220) (8192 Kbytes)
                  mtd .name = raspi, .size = 0x00800000 (8M) .erasesize = 0x00010000 (64K) .numeraseregions = 0
                  Creating 5 MTD partitions on "raspi":
                  0x00000000-0x00800000 : "ALL"
                  0x00000000-0x00030000 : "Bootloader"
                  0x00030000-0x00040000 : "Config"
                  Time: MIPS clocksource has been installed.
                  0x00040000-0x00050000 : "Factory"
                  0x00050000-0x01000000 : "Kernel"
                  mtd: partition "Kernel" extends beyond the end of device "raspi" -- size truncated to 0x7b0000
                  NET: Registered protocol family 2
                  IP route cache hash table entries: 1024 (order: 0, 4096 bytes)
                  TCP established hash table entries: 1024 (order: 1, 8192 bytes)
                  TCP bind hash table entries: 1024 (order: 0, 4096 bytes)
                  TCP: Hash tables configured (established 1024 bind 1024)
                  TCP reno registered
                  detected lzma initramfs
                  detected lzma initramfs
                  initramfs: LZMA lc=3,lp=0,pb=2,dictSize=1048576,origSize=12314112
                  LZMA initramfs by Ming-Ching Tiew .............
                  .......................................................RT3xxx E
                  HCI/OHCI init.
                  fuse init (API version 7.8)
                  io scheduler noop registered (default)
                  Ralink gpio driver initialized
                  HDLC line discipline: version $Revision: $, maxframe=4096
                  N_HDLC line discipline registered.
                  Serial: 8250/16550 driver $Revision: 1.9 $ 2 ports, IRQ sharing disabled
                  serial8250: ttyS0 at I/O 0xb0000500 (irq = 37) is a 16550A
                  serial8250: ttyS1 at I/O 0xb0000c00 (irq = 12) is a 16550A
                  RAMDISK driver initialized: 16 RAM disks of 16384K size 1024 blocksize
                  loop: loaded (max 8 devices)
                  rdm_major = 253
                  Ralink APSoC Ethernet Driver Initilization. v2.1  256 rx/tx descriptors allocated, mtu = 1500!
                  MAC_ADRH -- : 0x0000000c
                  MAC_ADRL -- : 0x43305077
                  PROC INIT OK!
                  PPP generic driver version 2.4.2
                  PPP BSD Compression module registered
                  NET: Registered protocol family 24
                  PPPoL2TP kernel driver, V0.17
                  PPTP driver version 0.8.1
                  block2mtd: version $Revision: $
                  rt3xxx-ehci rt3xxx-ehci: Ralink EHCI Host Controller
                  rt3xxx-ehci rt3xxx-ehci: new USB bus registered, assigned bus number 1
                  rt3xxx-ehci rt3xxx-ehci: irq 18, io mem 0x101c0000
                  rt3xxx-ehci rt3xxx-ehci: USB 0.0 started, EHCI 1.00, driver 10 Dec 2004
                  usb usb1: configuration #1 chosen from 1 choice
                  hub 1-0:1.0: USB hub found
                  hub 1-0:1.0: 1 port detected
                  rt3xxx-ohci rt3xxx-ohci: RT3xxx OHCI Controller
                  rt3xxx-ohci: new USB bus registered, assigned bus number 2
                  rt3xxx-ohci rt3xxx-ohci: irq 18, io mem 0x101c1000
                  usb usb2: configuration #1 chosen from 1 choice
                  hub 2-0:1.0: USB hub found
                  hub 2-0:1.0: 1 port detected
                  Initializing USB Mass Storage driver...
                  usbcore: registered new interface driver usb-storage
                  USB Mass Storage support registered.
                  nf_conntrack version 0.5.0 (256 buckets, 2048 max)
                  ip_tables: (C) 2000-2006 Netfilter Core Team, Type=Restricted Cone
                  TCP cubic registered
                  NET: Registered protocol family 1
                  NET: Registered protocol family 10
                  NET: Registered protocol family 17
                  802.1Q VLAN Support v1.8 Ben Greear 
                  All bugs added by David S. Miller 
                  Freeing unused kernel memory: 2856k freed
                  init started: BusyB
                  ox v1.12.1 (2013-10-15 14:30Algorithmics/MIPS FPU Emulator v1.5
                  :14 CST)
                  starting pid 629, tty '': '/etc_ro/rcS'
                  devpts: called with bogus options
                  Welcome to
                       _______  _______  ___     __  ____   _  _   ___
                      |  ___  \|   __  ||   |   |__||    \ | || | /  /
                      | |___| ||  |__| ||   |__  __ |     \| || |/  /
                      |   _   /|   _   ||      ||  || |\     ||     \
                      |__| \__\|__| |__||______||__||_| \____||_|\___\
                     \0x09             =System Architecture Department=
                  starting pid 638, tty '/dev/ttyS1': '/bin/sh'
                  BusyBox v1.12.1 (2013-10-15 14:30:14 CST) built-in shell (ash)
                  Enter 'help' for a list of built-in commands.
                  # ralink_gpio: sending a SIGUSR1 to process 633
                  Password for 'admin' changed
                  rmmod: ralink_wdt: No such file or directory
                  rmmod: cls: No such file or directory
                  rmmod: hw_nat: No such file or directory
                  rmmod: raeth: No such file or directory
                  insmod: bridge.ko: module not found
                  insmod: mii.ko: module not found
                  insmod: raeth.ko: module not found
                  ##### disable 1st wireless interface #####
                  rmmod: rt2860v2_a
                  p_net: No such file or directory
                  rmmod: rt2860v2_ap: No such file or directory
                  rmmod: rt2860v2_ap_util: No such file or directory
                  rt2860v2_ap: module license 'unspecified' taints kernel.
                  = pAd = c0002000, size = 805120 ===
                  <-- --="" -="" -f="" -t="" 00="" 01="" 0="" 0x0000000c="" 0x1300="00064380" 0x43305077="" 0x8913="" 0x8914="" 1.="" 1="" 2.="" 20mhz.l="" 2="" 3.="" 3100="" 3900="" 8021q.ko:="" 8021q:="" :="" a0493000="" address="" argument="" assign="" bbp="" be="" blinks="" br0:="" brctl:="" bridge="" bssid="00:0c:43:30:50:90" can="" cannot="" change="" changed="" config="" desc="" detected="" dev_set_promisc="" device="" directory="" do="" does="" done.="" entered="" entering="" error="" esw="" etc="" eth2.1:="" eth2.1="" eth2="" exist="" failed:="" file="" filter="" for="" forwarding="" found="" ifconfig:="" initialize="" insmod:="" insmod="" inva="" invalid="" ioctl="" iptable="" iptables="" is="" kernel="" key1str="" key2str="" key3str="" key4str="" key="" killall:="" killed="" learning="" led="13," length="" lid="" link="" mac_adrh="" mac_adrl="" main="" mangle="" master="" mcs="" mode="" module="" need="" needs="" nf_con="" nf_conntrack_proto_gre:="" nf_nat_pptp:="" nf_nat_proto_gre:="" no="" not="" ntrack_pptp:="" off="4000," offset="70," on="1," or="" partition="" perhaps="" ph="" phy="" phy_rx_ring="" phy_tx_ring="0x016f2000," port="" process="" promiscuous="" propagating="" r4="" ra0="" radvd.conf="" ralink="" reg="" rem:="" remove="" requested="" reset="1," rm:="" rmmod:="" rt28xx_init="" rt305x_esw:="" rtmpallocadapterblock="" rtmpalloctxrxringmemory="" rx="" rx_ring0="0xa16f3000" s="" set:="" set="ff" size="2048" stat="" state="" status="" such="" switch="" sync="" t="" table="" time="4000" to="" topology="" tx_ring="0xa16f2000" type="" udhcpd:="" uity="" upgraded.="" us="" v1.3.5:="" value="ffff506f" vconfig:="" vlan="" write="" x_esw:="" y="" you="" your="">/dev/null 2>&1
                  iptables -D FORWARD -j macipport_filter 1>/dev/null 2>&1
                  iptables -F macipport_filter 1>/dev/null 2>&1
                  iptables -D FORWARD -j web_filter  1>/dev/null 2>&1
                  iptables -F web_filter  1>/dev/null 2>&1
                  iptables -D FORWARD -j malicious_filter 1>/dev/null 2>&1
                  iptables -F malicious_filter  1>/dev/null 2>&1
                  iptables -D INPUT -j malicious_input_filter 1>/dev/null 2>&1
                  iptables -F malicious_input_filter  1>/dev/null 2>&1
                  iptables -P INPUT ACCEPT
                  iptables -P OUTPUT ACCEPT
                  iptables -P FORWARD ACCEPT
                  iptables -t filter -N web_filter 1>/dev/null 2>&1
                  iptables -t filter -N macipport_filter 1>/dev/null 2>&1
                  iptables -t filter -N malicious_filter 1>/dev/null 2>&1
                  iptables -t filter -N synflood_filter 1>/dev/null 2>&1
                  iptables -t filter -N malicious_input_filter 1>/dev/null 2>&1
                  iptables -t filter -N synflood_input_filter 1>/dev/null 2>&1
                  iptables -t filter -A FORWARD -j web_filter 1>/dev/null 2>&1
                  iptables -t filter -A FORWARD -j macipport_filter 1>/dev/null 2>&1
                  iptables -t filter -A FORWARD -j malicious_filter 1>/dev/null 2>&1
                  iptables -t filter -A malicious_filter -p tcp --syn -j synflood_filter 1>/dev/null 2>&1
                  iptables -t filter -A INPUT -j malicious_input_filter 1>/dev/null 2>&1
                  iptables -t filter -A malicious_input_filter -p tcp --syn -j synflood_input_filter 1>/dev/null 2>&1
                  iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu 1>/dev/null 2>&1
                  iptables -A malicious_input_filter -i eth2.2 -p tcp --dport 80 -j DROP
                  /bin/super_dmz -f
                  sh: /bin/super_dmz: not found
                  iptables -t nat -D PREROUTING -j port_forward 1>/dev/null 2>&1
                  iptables -t nat -F port_forward  1>/dev/null 2>&1; iptabl
                  es -t nat -X port_forward  1>/dev/null 2>&1
                  iptables -t nat -D PREROUTING -j DMZ 1>/dev/null 2>&1
                  iptables -t nat -F DMZ 1>/dev/null 2>&1; iptables -t nat -X DMZ  1>/dev/null 2>&1
                  cat /proc/sys/net/netfilter/nf_conntrack_udp_timeout > /var/.udpbackup
                  echo 0 > /proc/sys/net/netfilter/nf_conntrack_udp_timeout
                  cat /var/.udpbackup > /proc/sys/net/netfilter/nf_conntrack_udp_timeout; rm -f /var/.udpbackup
                  cat /proc/sys/net/netfilter/nf_conntrack_tcp_timeout_established > /var/.tcpbackup
                  echo 0 > /proc/sys/net/netfilter/nf_conntrack_tcp_timeout_established
                  cat /var/.tcpbackup > /proc/sys/net/netfilter/nf_conntrack_tcp_timeout_established; rm -f /var/.tcpbackup
                  iptables -t nat -N port_forward 1>/dev/null 2>&1; iptables -t nat -I PREROUTING 1 -j port_forward 1>/dev/null 2>&1
                  iptables -t nat -N DMZ 1>/dev/null 2>&1; iptables -t nat -I PREROUTING 2 -j DMZ 1>/dev/null 2>&1
                  kill -9 `cat /var/run/`
                  cat: can't open '/var/run/': No such file or directory
                  kill: you need to specify whom to kill
                  iwpriv ra0 set WscConfMode=0 1>/dev/null 2>&1
                  route delete 1>/dev/null 2>&1
                  killall -q klogd
                  killall -q syslogd
                  syslogd -C8 1>/dev/null
                  klogd 1>/dev/null 2>&1
                  killall -q zebra
                  killall -q ripd
                  echo 0 > /proc/sys/net/ipv6/conf/all/forwarding
                  webs: Listening for HTTP requests at address
                  fetchSmbConfig: No such file or directory
                  killall -q -USR1 udhcpd

                  Setting up busybox for AWM002 board

                  After successfully booting up AWM002 module, I found that busybox shell provided in default FW is very old and restricted one.

                  # /bin/ls --help
                  BusyBox v1.12.1 (2013-10-15 14:30:14 CST) multi-call binary

                  Usage: ls [-1AaCdilnsTwxk] [filenames...]

                  So I tried to upload latest busybox from busybox-site. After downloading the busybox mips-binary, we need to upload to AWM002 module. On stock FW, no FTP/SSH services are available. So we need to setup AWM002 to have FTP server (from default 

                  # /bin/ server awm002 21 5
                  Some iptable 'raw' module errors (ignore them for the time being)

                  Above command should start proftpd on AWM002 module

                  From PC, FTP the MIPS-binary busybox-mipsel (in binary mode)  to /tmp (for example)

                  Now log into AWM002 module by telnet/serial cable and issue following commands:

                  # cd /tmp
                  # chmod 755 busybox-mipsel
                  # mkdir /root
                  # mkdir /root/bin
                  # /tmp/busybox-mipsel --install -s /root/bin 

                  Copy following contents to /.profile
                  export PATH=/root/bin:$PATH

                  Log off from AWM002 and login & you will have more latest & unrestricted busybox binary on PATH will all commands. 

                  Note: Keep in mind that /.profile, /root/ and  /root/bin and trasient & they will go off once you reboot module

                  Starting the 11$ WiFi AWM002 OpenWRT board

                  Finally I got my 11$ WiFi OpenWRT board ( AWM002 ) with largeboard and Tiny Boards.

                  So here is first boot up of the AWM002 module with Large Base Board.

                  Hardware needed

                  • Large Base Board & AWM002 module
                  • Decent 12V supply
                  • USB cable to connect to PC (optional: for serial port access to AWM002 module)
                    • Not optional, in case you do not connect AWM002 to your LAN 

                  Setup (HW)

                  • Connect Large Base board with 12V socket
                  • Connect LAN cable 
                  • Connect USB cable to computer (for serial port access)
                  • Power on (white switch)

                  Setup (SW) in Linux box

                  • First create a virtual interface in Linux to connect to AWM002 for following command as root-user
                    • ifconfig p10p1:0
                      • Where p10p1 is my NIC-name (in Fedora, in your case it could be 'eth0')
                      • is the IP address of   p10p1:0
                      • BTW, is address of AWM002 by default (see in above AWM002 link's Updates-tab)
                      • Ensure that you have created the interface
                  • Run ping command to check AWM002 is up
                    • ping
                  • If the AWM002 is up then try for telenet login using following credentials
                    • user: admin, password: admin
                    • BTW, admin is having uid/gid as '0' -- admin is su :)
                  • In case you connect PC via USB cable, ensure that Linux box has created /dev/ttyUSB0
                    • check via 'dmesg' command to see exact serial device name

                  Final AWM002 photo