2010年6月23日 星期三

C alarm() - 系統經過特定時間之後發sigalarm給程式

http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=/apis/sigalarm.htm

程式在做一些timeout的功能時用alarm()很方便!

syslog - 寫入資訊至系統的log

參考http://blog.csdn.net/yulanarti/archive/2007/09/13/1783839.aspx




#include
void openlog (char*ident,int option ,int facility);
void syslog(int priority,char*format,……)
void closelog();

2010年6月22日 星期二

在程式內執行系統指令並取得回結果

使用popen()

參考http://www.lix.polytechnique.fr/~liberti/public/computing/prog/c/C/FUNCTIONS/popen.html


FILE *popen(const char *command, const char *type);
執行某指令並把stdout的值放入檔案並回傳
若要取得stderr可以在指令後加上 "2>&1"

用完後要使用pclose()關閉檔案


example:

/*
* Purpose: Program to demonstrate the popen function.
*
* to do: Check that the 'popen' was successfull.
*
* Author: M J Leslie.
* Date: 08-Jan-94
*/

#include

main()
{
FILE *fp;
char line[130]; /* line of data from unix command*/

fp = popen("ls -l", "r"); /* Issue the command. */

/* Read a line */
while ( fgets( line, sizeof line, fp))
{
printf("%s", line);
}
pclose(fp);
}

2010年6月14日 星期一

ganttproject - 畫甘特圖的軟體

http://www.ganttproject.biz/

蠻簡單的甘特圖軟體,使用java寫的,檔案使用xml格式儲存


不過發現下載回來的版本中文字型顯示有問題,全部變半形
但是線上版本運作就正常,所以就直接抓線上運作的版本,在本地執行

/usr/lib/jvm/java-6-sun-1.6.0.20/bin/javaws -offline /home/xxxx/ganttproject-2.0.10.jnlp

2010年6月4日 星期五

使用蜂鳴器通知某指令完成

1.先寫一隻能夠使蜂鳴器發出聲音的程式

//b2.c
#include
#include

void play(unsigned int* freq, unsigned int* delay);

int main(int argc, char* argv[])
{
speaker(330, 5);
speaker( 0, 5);
speaker(330, 5);

return 0;
}

int speaker(unsigned int freq,unsigned int delay)
{ static int flag=0,bit;
if(flag==0)
{
flag=1;
iopl(3);
}
outb(0xb6,0x43);
outb((freq & 0xff),0x42);
outb((freq >> 8),0x42);
bit=inb(0x61);
outb(3 | bit,0x61);
usleep(10000*delay);
outb(0xfc | bit,0x61);
return;
}


2.beep:一個shell script使b2在指令完成時執行

$*
sudo ./b2


3.把b2 and beep放至/bin

4.由於b2發出聲音要有root的權限,所以b2擁有者要設為root,然後對beep作setuid
http://www.google.com/url?q=http%3A%2F%2Flinux.vbird.org%2Flinux_basic%2F0220filemanager.php%23suid&sa=D&sntz=1&usg=AFrqEzfx6HcqK_4BpogqAlU2s7Ts5ttsug

debug USB - use usbmon, usbfs


dump urb data

  1. 編kernel時,在driver/usb/下選擇usb monitor
  2. mount -t debugfs debugfs /sys/kernel/debug
  3. 之後在此目錄下就會看到usbmon目錄
  4. 可以在此dump出urb的資料(ascii)

  • 可以使用/dev/usbmon[XX]來dump出raw data,之後自己寫程式去parse
  • 詳細文件參考Documentation/usb/usbmon.txt

查看linux上所有USB裝置的詳細資訊



  1. mount -t usbfs usbfs /proc/bus/usb/
  2. cat /proc/bus/usb/devices
  3. 各攔位介紹
    1. http://www.linux-usb.org/USB-guide/c607.html
  4. 詳細文件: Documentation/usb/proc_usb_info.txt

/proc/bus/usb/devices各欄位資料

  • T開頭(topology)
    • Bus: which bus the device is on
    • Lev: the level of the device
      • level 00 for the root hub
      • level 01 for any device attached to the root hub
      • level 02 for devices attached to hubs at level 01, and so on.
    • Prnt: the parent device for this device
      • always 00 for the root hub
      • 01 for the devices attached to the root hub
    • Port: the port on the parent device
      • starting at 00 for the first port on each device.
      • Prnt/Port is unique per bus.
    • Cnt: what number device this is, based on the enumeration order within that level of the topology, starting at 01 for the first device.
    • Dev#: what number device this is, irrespective of level, based on the bus enumeration order. This is unique per bus
    • Spd: what speed this device is running at, in Mbps (either 1.5 or 12 with the current version of USB).
    • MxCh: how many devices can be connected to this device
      • 00 for anything except a hub.
    • Driver: which device driver is being used for this device
      • an entry of (none) indicates that no driver is being used.
  • D開頭(device descriptor)
    • Ver: which USB specification version the device claims to meet.
    • Cls: which device class the device is claiming to meet, in both hexadecimal and as a string.
      • A Cls entry of 00(>ifc) indicates that the device class specification compliance is interface dependent, and the interface descriptor should be read for device class information.
    • Sub: which sub-class (within the Cls entry), the device meets.
    • Prot: which protocol within a class or sub-class the device claims to meet.
    • MxPS: how big the packets from Endpoint 0 are.
    • #Cfgs: how many configurations this device has.
  • P開頭(pid/vid)
    • Vendor: the Vendor Identification code for the device
    • ProdID: the Product Identification code for the device.
    • Rev: the product revision number.
  • S開頭: vendor and product strings that the device returned.
  • C開頭(configuration descriptor)

    • the number of C:lines per device is given by #Cfgs, and the entry followed by an asterisk is the current configuration.

    • #If: how many interfaces the device has.

    • Cfg#: which configuration is being described.

    • Atr: hexadecimal indication of the device attributes

      • 0x80 for bus-powered

      • 0x40 for self-powered

      • 0x20 for remote wake-up capable

    • MxPwr: maximum power draw for this device configuration, in milliamps(mA).

  • I開頭(interface descriptor)

    • If#: which interface is being described within a given device configuration.

    • Alt: which alternate setting of this interface is being described.

    • #EPs: how many endpoints there are within the alternate setting for this endpoint.

    • Cls: which class the alternate setting of the interface corresponds to, in both hexadecimal and as a character string.

    • Sub: which sub-class the alternate setting of the interface belongs to.

    • Prot: which interface protocol (within a class and sub-class tuple) the alternate setting of the interface conforms to.

    • Driver: which of the various USB drivers has claimed this interface.

  • E開頭(endpoint descriptor)

    • Endpoint 0 is not displayed.

    • Ad: endpoint address, with a letter to indicate whether the endpoint is an In or Out endpoint.

    • Atr: the attribute (transfer type) associated with the endpoint, followed by a string translating the transfer type.

    • MxPS: maximum packet size this endpoint is capable of sending or receiving, as appropriate.

    • Ivl: the interval, in milliseconds, between polling of interrupt endpoints.

      • ignored for bulk and control transfers, and is set to 1 for isochronous transfers.







=================================================

example

root@ocgod-ubuntu9:/proc/bus/usb# cat devices

T: Bus=05 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
B: Alloc= 0/900 us ( 0%), #Int= 0, #Iso= 0
D: Ver= 1.10 Cls=09(hub ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=1d6b ProdID=0001 Rev= 2.06
S: Manufacturer=Linux 2.6.28-18-generic uhci_hcd
S: Product=UHCI Host Controller
S: SerialNumber=0000:00:10.3
C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr= 0mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub
E: Ad=81(I) Atr=03(Int.) MxPS= 2 Ivl=255ms

T: Bus=04 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
B: Alloc= 0/900 us ( 0%), #Int= 0, #Iso= 0
D: Ver= 1.10 Cls=09(hub ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=1d6b ProdID=0001 Rev= 2.06
S: Manufacturer=Linux 2.6.28-18-generic uhci_hcd
S: Product=UHCI Host Controller
S: SerialNumber=0000:00:10.2
C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr= 0mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub
E: Ad=81(I) Atr=03(Int.) MxPS= 2 Ivl=255ms

T: Bus=03 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
B: Alloc= 0/900 us ( 0%), #Int= 0, #Iso= 0
D: Ver= 1.10 Cls=09(hub ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=1d6b ProdID=0001 Rev= 2.06
S: Manufacturer=Linux 2.6.28-18-generic uhci_hcd
S: Product=UHCI Host Controller
S: SerialNumber=0000:00:10.1
C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr= 0mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub
E: Ad=81(I) Atr=03(Int.) MxPS= 2 Ivl=255ms

T: Bus=02 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=12 MxCh= 2
B: Alloc= 34/900 us ( 4%), #Int= 3, #Iso= 0
D: Ver= 1.10 Cls=09(hub ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=1d6b ProdID=0001 Rev= 2.06
S: Manufacturer=Linux 2.6.28-18-generic uhci_hcd
S: Product=UHCI Host Controller
S: SerialNumber=0000:00:10.0
C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr= 0mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub
E: Ad=81(I) Atr=03(Int.) MxPS= 2 Ivl=255ms

T: Bus=02 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#= 2 Spd=1.5 MxCh= 0
D: Ver= 1.10 Cls=00(>ifc ) Sub=00 Prot=00 MxPS= 8 #Cfgs= 1
P: Vendor=13ba ProdID=0017 Rev= 0.01
S: Product=Generic USB K/B
C:* #Ifs= 2 Cfg#= 1 Atr=a0 MxPwr=100mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=03(HID ) Sub=01 Prot=01 Driver=usbhid
E: Ad=81(I) Atr=03(Int.) MxPS= 8 Ivl=24ms
I:* If#= 1 Alt= 0 #EPs= 1 Cls=03(HID ) Sub=01 Prot=02 Driver=usbhid
E: Ad=82(I) Atr=03(Int.) MxPS= 5 Ivl=10ms

T: Bus=02 Lev=01 Prnt=01 Port=01 Cnt=02 Dev#= 3 Spd=1.5 MxCh= 0
D: Ver= 1.10 Cls=00(>ifc ) Sub=00 Prot=00 MxPS= 8 #Cfgs= 1
P: Vendor=04d9 ProdID=0499 Rev= 2.90
C:* #Ifs= 1 Cfg#= 1 Atr=a0 MxPwr=100mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=03(HID ) Sub=01 Prot=02 Driver=usbhid
E: Ad=81(I) Atr=03(Int.) MxPS= 8 Ivl=10ms

T: Bus=01 Lev=00 Prnt=00 Port=00 Cnt=00 Dev#= 1 Spd=480 MxCh= 8
B: Alloc= 0/800 us ( 0%), #Int= 0, #Iso= 0
D: Ver= 2.00 Cls=09(hub ) Sub=00 Prot=00 MxPS=64 #Cfgs= 1
P: Vendor=1d6b ProdID=0002 Rev= 2.06
S: Manufacturer=Linux 2.6.28-18-generic ehci_hcd
S: Product=EHCI Host Controller
S: SerialNumber=0000:00:10.4
C:* #Ifs= 1 Cfg#= 1 Atr=e0 MxPwr= 0mA
I:* If#= 0 Alt= 0 #EPs= 1 Cls=09(hub ) Sub=00 Prot=00 Driver=hub
E: Ad=81(I) Atr=03(Int.) MxPS= 4 Ivl=256ms

2010年6月3日 星期四

在linux 下使用rdesktop連線windows 遠端桌面

在ubuntu中可以使用內建的圖形化介面:tsclient
不過他預設的解析度只有固定幾個,無法改變
(就直接修改tsclient存下來的設定檔,也只會跳到最接近的選項)

所以可以使用rdesktop指令:
rdesktop -u cyl -p passwd -g 1280x1000 -a 24 192.168.0.XXX -x nr



rdesktop: A Remote Desktop Protocol client.
Version 1.6.0. Copyright (C) 1999-2008 Matthew Chapman.
See http://www.rdesktop.org/ for more information.

Usage: rdesktop [options] server[:port]
-u: user name
-d: domain
-s: shell
-c: working directory
-p: password (- to prompt)
-n: client hostname
-k: keyboard layout on server (en-us, de, sv, etc.)
-g: desktop geometry (WxH)
-f: full-screen mode
-b: force bitmap updates
-L: local codepage
-A: enable SeamlessRDP mode
-B: use BackingStore of X-server (if available)
-e: disable encryption (French TS)
-E: disable encryption from client to server
-m: do not send motion events
-C: use private colour map
-D: hide window manager decorations
-K: keep window manager key bindings
-S: caption button size (single application mode)
-T: window title
-N: enable numlock syncronization
-X: embed into another window with a given id.
-a: connection colour depth
-z: enable rdp compression
-x: RDP5 experience (m[odem 28.8], b[roadband], l[an] or hex nr.)
-P: use persistent bitmap caching
-r: enable specified device redirection (this flag can be repeated)
'-r comport:COM1=/dev/ttyS0': enable serial redirection of /dev/ttyS0 to COM1
or COM1=/dev/ttyS0,COM2=/dev/ttyS1
'-r disk:floppy=/mnt/floppy': enable redirection of /mnt/floppy to 'floppy' share
or 'floppy=/mnt/floppy,cdrom=/mnt/cdrom'
'-r clientname=': Set the client name displayed
for redirected disks
'-r lptport:LPT1=/dev/lp0': enable parallel redirection of /dev/lp0 to LPT1
or LPT1=/dev/lp0,LPT2=/dev/lp1
'-r printer:mydeskjet': enable printer redirection
or mydeskjet="HP LaserJet IIIP" to enter server driver as well
'-r sound:[local[:driver[:device]]|off|remote]': enable sound redirection
remote would leave sound on server
available drivers for 'local':
alsa: ALSA output driver, default device: default
oss: OSS output driver, default device: /dev/dsp or $AUDIODEV
'-r clipboard:[off|PRIMARYCLIPBOARD|CLIPBOARD]': enable clipboard
redirection.
'PRIMARYCLIPBOARD' looks at both PRIMARY and CLIPBOARD
when sending data to server.
'CLIPBOARD' looks at only CLIPBOARD.
-0: attach to console
-4: use RDP version 4
-5: use RDP version 5 (default)
-y: use raw keyboard (default no)

2010年6月2日 星期三

簡易連線uart的程式

本來是想要把minicom搬到android上,不過他的source code很多,也蠻複雜的,
後來在網路上找到tick_minicom,只有幾個檔案,應該很容易讓他編進android

tick_minicom:簡單的uart程式
http://linuxocarina.blogspot.com/2007/09/minicom.html

不過我的連線設定跟他不一樣,所以參考以下網站對程式做了一些修改之後就可以使用了
http://book.51cto.com/art/200711/59758.htm



int
set_baud (int fd, int rate)
{
int i, rt;

u_int32_t bd = 0;

struct termios ti;

//select baudrate
for (i = 0; i < ARRAY_SIZE (bdrts); i++) {
if (bdrts[i].bps == rate) {
bd = bdrts[i].b;
debug("select baudrate: %d\n",rate);
break;
}
}
if (bd == 0) {
error ("Cannot set Baud Rate!!\n");
return -1;
}

//set baudrate
rt = tcgetattr (fd, &ti);
if (rt < 0) {
error ("Cannot get attr i=%d\n", rt);
return rt;
}

debug ("Open uart input with speed %d\n", bdrts[i].bps);
//rt = cfsetispeed (&ti, B0);
rt = cfsetispeed (&ti, bd);
if (rt < 0) {
//error ("Cannot set input baud to B0!!\n");
error ("Cannot set input baud to bd!!\n");
return rt;
}

debug ("Open uart output with speed %d\n", bdrts[i].bps);
rt = cfsetospeed (&ti, bd);
if (rt < 0) {
error ("Cannot set baud rate %d\n", bdrts[i].bps);
return rt;
}

rt=tcsetattr (fd, 0, &ti);
if(rt<0){
return rt;
}

//set no hareware conrtol
rt = tcgetattr (fd, &ti);
//ti.c_cflag |= CRTSCTS; // hardware control
ti.c_cflag &= ~CRTSCTS; // no hardware control
rt=tcsetattr (fd, 0, &ti);
if(rt<0){
return rt;
}


//set databit:8
rt = tcgetattr (fd, &ti);
ti.c_cflag&=~CSIZE;
ti.c_cflag |=CS8;
rt=tcsetattr (fd, 0, &ti);
if(rt<0){
return rt;
}


//set no parity bit
rt = tcgetattr (fd, &ti);
ti.c_cflag &= ~PARENB;
rt=tcsetattr (fd, 0, &ti);
if(rt<0){
return rt;
}


return rt;
}

解決ubuntu 10.04 中minicom放大到全螢幕會閃爍的問題

參考 http://blog.xuite.net/yctseng/notes/26134176

使用:
alias minicom='TERM=linux /usr/bin/minicom'