Use boost:bind

October 25, 2011

http://www.boost.org/doc/libs/1_47_0/libs/bind/bind.html

build a symbol database for vim

August 12, 2011

#!/bin/sh

# find src files
find . ! -path ./exclude_dir_1\* -a           \
! -path ./exclude_dir_2\* -a          \
-regex “.*\.\(c\|h\|hpp\|cc\|cpp\|s\)” >  cscope.files

# number is exact match, or else use search by default

ctags -L cscope.files –language=c:.c.h,c++:.cpp.hpp.cc,Asm:.s –excmd=number
cscope -bqk

serial monitoring under linux

August 27, 2010

On windows, you can use the HHD’s serial monitor professional tools, on Linux,

what can you do to peek the serial port?  Below is a quick start guide for using moni,

1. download moni

http://www.rolf-schroedter.de/moni/#download

2. download tclkit

http://www.equi4.com/starkit/started.html

3. type the following to run moni
gzip -d tclkit-*.gz
mv tclkit-* tclkit
chmod +x tclkit
./tclkit sdx.kit

A Device Monitoring System

July 14, 2010

My current project is a device monitoring system. The system has following features

  • Reliable device communication through specified interfaces
  • Web interface for system administration and device status monitoring (including remote)
  • SQL Database to archive device status data, plot trending etc.
  • Support SNMP interface to network management systems (NMS).

Notes for system implementation details.

1. For rapid development, Python was chosen.
2. Pyserial is the main package used for serial communication over RS485 serial interface.  To use it,

import serial, time

s = serial.Serial(port=’/dev/ttyUSB0′, timeout=1) # open first usb serial port

s.write(“command to send”)

time.sleep(0.2)

response = s.readline()

Since system may have multiple buses, serial driver will run as separate processes. On linux platform (python2.5 or later), To start a process in python, use

pid = Popen(‘path/prog_name’, arg1,arg2,..).pid

To kill a process, use

import os

os.kill(pid,9)

3. Web Server. To install a simple web server, just use thttpd. Be sure to comment out the chroot in your server configuration file.  Server backend is using python cgi.  To turn on cgi debugging, use,

import cgi, cgitb; cgitb.enable()

Another way is to use “import pdb;pdb.set_trace()”

To enable logging, use

import logging

logging.basicConfig(filename=’my.log’, level=logging.debug)

when you need log something, use

logging.debug(“message or vars to log”)

To get values from forms, use

form=cgi.FieldStorage()

form.getvalue(‘key’) or form.getlist(‘key’)

4. Database backend use Sqlite3 since it is included in python packages.

To create a database, you can use Python DB API 2.0

import sqlite3

c = sqlite3.connect(“test.db”,isolation_level=None) # None means immediate commit to DB for any operation

c.text_factory=str  #  let DB convert unicode to ascii for you

c.execute(“”"create table if not exists bus (bus_id INTEGER PRIMARY KEY, port TEXT, polling INTEGER, timeout INTEGER, state TEXT, pid INTEGER default 0)”"”)   # create a bus table and use bus id as primary key.

For sql query, you can embedded variable in two ways, use ? or use named labels, see following,

bus = c.execute(“select * from bus where bus_id==?”, (bus_id,)).fetchone()

c.execute(“update bus set pid=:pid where bus_id==:busid “,{‘busid’:bus[0],’pid’:pid})

5. Use template and tables to simplify web page

from string import Template

tpl = Template(open(“site.html”).read())

print “Content-type: text/html\r\n\r\n”
print tpl.substitute(dict(current=buses,confirm=confirm))

6. Use PySNMP for SNMP agent  (to be continued..)

Setup the thttpd server

May 12, 2010

On embedded system, use apache2 web server could be a luxury. I have used web2py web framework and it did run but crashed my flash drive. So I decided to keep it simple. thttpd has a very small footprint and said to be secure.  To install it in a debian system, just type

aptitude install thttpd

It will install as all the other packages. The next step is to configure the server, on my debian lenny, it is /etc/thttpd/thttpd.conf

I want to run python cgi programs, so the cgipattern /cgi-bin/*  seems fit my needs.

The default cgi script directory is /var/www/cgi-bin and default html docs are under /var/www

You need make sure the cgi scripts have 755 permission and chown to www-data.

cgi program need print two empty lines , so the simplest cgi will be

#!/usr/bin/python

print “Content-type: text/plain\n\n”

And this works in apache, however it doesn’t work on thttpd.  After struggled several hours in reading the manual, options and digging through the mail list archives, check the thttpd server log and syslog for errors. I still have no clue.

Somehow, before I give up, I find a chroot option in configuration file. I change it to nochroot, because if I do the chroot, python couldn’t run for I haven’t duplicated the /usr/python into /cgi-bin directory.  That’s it! Now it runs.  I have to admit : thttpd is too secure…

How to netboot debian from ubuntu based system

April 15, 2010

1. setup tftp server.

- You need a server with tsize support.
sudo aptitude install tftp-hpa tftpd-hpa

- set tftp directory
sudo mkdir /tftpboot
sudo chown nobody.nogroup /tftpboot
sudo chmod 777 /tftpboot

- configure tftp
sudo gedit /etc/default/tftpd-hpa
#Defaults for tftpd-hpa
RUN_DAEMON=”yes”
OPTIONS=”-l -s /tftpboot”

- start tftp
sudo /etc/init.d/tftpd-hpa start

-verify
netstat -a |grep tftp
touch /tftpboot/testfile
tftp localhost -c get testfile

2. setup a DHCP server and enable PXE booting
sudo aptitude install dhcp3-server
sudo gedit /etc/dhcp3/dhcpd.conf

- content looks like

default-lease-time 600;
max-lease-time 7200;
allow booting;
allow bootp;

# The next paragraph needs to be modified to fit your case
subnet 192.168.1.0 netmask 255.255.255.0 {
range 192.168.1.101 192.168.1.253;  # depends on your router
option broadcast-address 192.168.1.255;
# the gateway address which can be different
# (access to the internet for instance)
option routers 192.168.1.1;
# indicate the dns you want to use
option domain-name-servers 192.168.1.1;
}

group {
#  next-server 192.168.1.3;
host tftpclient {
# tftp client hardware address
hardware ethernet  <MAC_ADDR_SEPARATE_BY_:>;
filename “pxelinux.0″;
}
}

3. move tftp images into tftp folder
goto

http://www.debian.org/releases/stable/i386/ch04s02.html.en#where-files

and get the netboot.tar.gz tarball and extract it into /tftpboot

reference

http://www.debian.org/releases/stable/i386/ch04s05.html.en

http://blog.tuxcoder.com/2008/06/configure-tftp-server-in-ubuntu.html

A useful tool — devmem2

March 25, 2010

Devmem2 is a simple utility to read/write to any memory mapped locations.  It is not available in debian etch but I find the source code (free GNU license) , compiled and run it perfectly on my gesbc debian eth environment.

Build:

$ gcc devmem2.c -o devmem2

To use devmem2 to toggle RTS register of the Serial Port 4 to high (RS485 mode).

./devmem2 0xfffd0004 w 0xc00008c1

How to boot to usb flash drive and use a debian system

March 24, 2010

Glomation SBC board is by default configured to boot to an emdedian file system that stored in Flash. I think it’s preferable to boot to a USB drive that has a debian system. Here is how I did it after consulting the posts in the forum.

Prepare the usb drive on your PC
===================================
My PC is running linux Mint 8,

- Format your flash drive to ext3
sudo fdisk /dev/sdb1

create a new primary partition and use w to save it.

sudo mkfs.ext3 /dev/sdb1

- mount your flash disk
sudo mount /dev/sdb1 /mnt

- download debian etch image for gesbc
http://www.glomationinc.com/download/debian-etch-arm-base.tar.gz

- load the debian file system to your usb drive
sudo tar xzvf debian-etch-arm-base.tar.gz /mnt

Change U-boot environment setting
=================================
Now insert your usb drive to your sbc then power up the board. stop the U-boot by pressing any key,

The printenv command can print out all current U-boot environment variable settings.   The set command can be used to set the environment variable for example,

set bootargs console=ttyS0,115200 root=/dev/sda1 rootdelay=10 mtdparts=atmel_nand:1M(bootloader),3M(kernel),-(rootfs)

to set the boot argument to use USB as root file system. Note the sbc recognize the usb drive as /dev/sda1 on my board (while it was recognized /dev/sdb1 on my PC). Please note the MTD device name of the NAND FLASH is at91_nand for kernel version 2.6.25.x and atmel_nand for kernel version 2.6.27.x and up.

You can save the setting use saveenv command so the board can boot using USB root file system as default.

Other Useful Tips
==================

- Route debug message to UART (P1)

I find someone in forum says “I was shocked by the fact that the debug message is not routed to the UART (P1) port of the board, but to a 3-pin P0.”. I have  the same feeling here.

You can remove this inconvenience by changing the U-boot environment setting to route it to UART (P1)
set bootargs console=ttyS1,115200 root=/dev/sda1 rootdelay=10 mtdparts=atmel_nand:1M(bootloader),3M(kernel),-(rootfs)

remember to save the setting use saveenv command. then type boot and connect your NULL modem from your PC to the sbc board.

In production, you can easily switch it back.

- Setup a SSH server

Actually, you rarely need look at the debug message. If you setup the SSH server on sbc board, you can even get rid of the NULL modem cable, just connect the board using ssh.

To install ssh server, type
apt-get install ssh

Issues
============
I find the default dhcp client shipped with emdebian has a short timeout. When dhcp server is slower in response, the board ends up with “discover… discover… fails”. However, the debian system ‘s DHCP client wait longer which then correctly configures the network, thus allows for ssh access without any user intervention.

Setup a TFTP server

March 22, 2010

I need setup a tftp server to upgrade the file system from Emdebian to debian on my board.
Since I have been using linux mint, it’s quite easy to setup ,

1. Download tftp
$ sudo apt-get install xinetd tftpd tftp

2. Configuration
sudo gedit /etc/xinetd.conf, add the following,

service tftp
{
protocol        = udp
port            = 69
socket_type     = dgram
wait            = yes
user            = nobody
server          = /usr/sbin/in.tftpd
server_args     = /tftpboot
disable         = no
}

3. Make /tftpboot directory
$ sudo mkdir /tftpboot
$ sudo chmod -R 777 /tftpboot
$ sudo chown -R nobody /tftpboot

4. Start tftpd through xinetd
$ sudo /etc/init.d/xinetd restart

5. Test tftp
cd
touch /tftpboot/testfile
tftp 127.0.0.1
tftp > get testfile

Note: tftp server is also need for U-boot to update the linux kernel of the board.

Using ssh and scp

March 19, 2010

SSH

ssh enables you to remote login and operate the PC. In linux, you usually have ssh client installed by default, in windows, you need download putty.

To login, type

$ssh user@host

then you will need to enter password at prompt.

SCP

scp enables you exchange files between your PC and remote computer:

fetch remote files to your pc
scp user@host:path/filenames .

upload files from your pc to remote server
scp filenames user@host:path

scp supports wildcard in filenames and recursive  (-r)

linux probably has it installed, on windows you can use winscp.

Note:  to allow new user to ssh to the  system, in debian, need add user in /etc/ssh/sshd_coonfig contain AllowUsers <user_name>


Follow

Get every new post delivered to your Inbox.