2008 (mizuuchi@i.u-tokyo.ac.jp) http://www.jsk.t.u-tokyo.ac.jp/ http://www.jsk.t.u-tokyo.ac.jp/ ikuo/enshu/keisanki/ 2008 5 16 6 23 1 2 1 GUI(Graphical User Interface) 2 2.1 CD USB ( LSI ) (hardware abstraction) ( ) OS OS (Windows Linux ) Linux OS OS /sbin/lsmod
2 2. % /sbin/lsmod OS ( OS ) Module Size Used by Not tainted Module Size Used by rfcomm 40216 0... ( )... psmouse 36100 0 floppy 62148 0 snd_intel8x0 33692 0 snd_ac97_codec 93088 1 snd_intel8x0 snd_ac97_bus 2304 1 snd_ac97_codec snd_pcm_oss 53664 0 snd_mixer_oss 18688 1 snd_pcm_oss snd_pcm 89864 3 snd_intel8x0,snd_ac97_codec,snd_pcm_oss snd_timer 25220 1 snd_pcm e1000 118840 0 snd 55268 6 snd_intel8x0,snd_ac97_codec,snd_pcm_oss,snd_mixer_oss,snd_pcm,snd_timer soundcore 10208 1 snd snd_page_alloc 10632 2 snd_intel8x0,snd_pcm rtc 13492 0... ( )... soundcore e1000 2.2 UNIX % ls -l /dev /dev 0 lrwxrwxrwx 1 root root 13 2007-06-07 22:50 MAKEDEV -> /sbin/makedev* crw-rw---- 1 root root 10, 62 2007-06-07 13:50 acpi crw-rw---- 1 root audio 14, 12 2007-06-07 22:50 adsp crw-rw---- 1 root video 10, 175 2007-06-07 22:50 agpgart crw-rw-rw- 1 root audio 14, 4 2007-06-07 22:50 audio drwxr-xr-x 3 root root 60 2007-06-07 22:50 bus/ crw-rw---- 1 root video 226, 0 2007-06-07 13:50 card0 lrwxrwxrwx 1 root root 3 2007-06-07 22:50 cdrom -> hdc crw------- 1 root root 5, 1 2007-06-07 13:50 console lrwxrwxrwx 1 root root 11 2007-06-07 22:50 core -> /proc/kcore drwxr-xr-x 5 root root 100 2007-06-07 22:50 disk/ drwxr-xr-x 2 root root 60 2007-06-07 13:50 dri/ crw-rw-rw- 1 root audio 14, 3 2007-06-07 22:50 dsp drwxr-xr-x 4 root root 140 2007-06-07 13:50 evms/ crw-rw---- 1 root video 29, 0 2007-06-07 22:49 fb0 lrwxrwxrwx 1 root root 13 2007-06-07 22:50 fd -> /proc/self/fd/ brw-rw---- 1 root floppy 2, 0 2007-06-07 22:50 fd0 crw-rw-rw- 1 root root 1, 7 2007-06-07 22:49 full brw-rw---- 1 root disk 3, 0 2007-06-07 22:50 hda brw-rw---- 1 root disk 3, 1 2007-06-07 22:50 hda1 : :
3 b c /dev read() write() 3 Linux OSS/Free(Open Sound System Free) 3.1 PC 3.2 Web (http://www.jsk.t.u-tokyo.ac.jp/ ikuo/enshu/keisanki/) chat-appli1.tar.gz % tar xvzf chat-appli1.tar.gz % cd chat-appli/device1/sound 3.3 OSS /dev/audio( /dev/dsp) % cat test.raw > /dev/audio
4 3. test.raw % cat /dev/audio > another.raw another.raw Ctrl-C C % gnome-volume-control & gnome-volume-control ( 1) (Alsa mixer) (F) (C) 1:Analog devices ADD1981B (OSS Mixer) 1 1: gnome-volume-control /dev/audio cat
3.4. 5 3.4 sample1.c % make %./play play./play play PATH /usr/bin/play./ 3.5 2 2: open() ioctl() write() close()
6 3. open() #include <fcntl.h> int open(char *path, int oflag); path oflag : : open() 1 path 2 oflag oflag 1 1: O RDONLY O WRONLY O RDWR O NDELAY O APPEND O SYNC O CREAT O TRUNC O EXCL 0 O CREAT 1 ioctl() #include <sys/ioctl.h> int ioctl(ind fd, int request, char *arg); fd request arg : : :
3.5. 7 ioctl() 1 fd 2 request 3 arg 1 2 request PCM (Pulse Code Modulation) 2 #include <linux/soundcard.h> 2: PCM SOUND PCM WRITE BITS SOUND PCM WRITE CHANNELS SOUND PCM WRITE RATE (8 bit 16 bit) ( : 2 : 1) 3 arg int write() #include <fcntl.h> int write(int fd, char *buf, size_t nbyte); fd buf nbyte : : : write() 1 open() fd 2 3 1 close() #include <fcntl.h> int close(int fd); fd :
8 3. close() open() fd 1 1 1. sample1.c 2. sample1.c 3.6 3 3: open() ioctl() read() close() read() #include <fcntl.h> int read(int fd, char *buf, unsigned nbyte); fd buf nbyte : : :
9 read() 1 open() fd 2 3 1 2 1. sample1.c record.c record Makefile sample1 ( fwrite()) ( 1) gmix 2. sample1.c ( 2) 4 1 PC 1 2 9 (25 ) /dev/ttys0 4.1 4 open() ioctl() write() read() close()
10 4. 4: ioctl() ioctl 2 ( ) 3 TCGETA TCSETA TCSETAW TCSETAF 3: 3 3 TCSETAW 3 ( )
4.2. 11 #define NCC 8 struct termio{ unsigned short int c_iflag; unsigned short int c_oflag; unsigned short int c_cflag; unsigned short int c_lflag; unsigned char c_line; unsigned char c_cc[ncc]; }; c_iflag c_oflag c_cflag c_lflag c_line c_cc : : : : : : 4.2 sample2.c sample3.c server client client server 4.3 sample2.c sample3.c sample2 sample3 % cd../serial % make
12 5. 3 1. sample2.c sample3.c /* TODO 2 1 1 3 4 1. sample2.c sample3.c 2. Client ( : fgets() ) 5 5.1 man C man read fread % man 2 read % man 3 fread man mech-unix man read read 2
5.2. 13 4: man 1 2 3 4 /dev 5 /etc/passwd 6 7 man 8 root 9 % man man % whatis read 5.2 Linux JF (Japanese FAQ) Linux http://www.linux.or.jp/jf/ LDP (Linux Documentation Project) http://tldp.org/ Linux Sound HOWTO http://www.linux.or.jp/jf/jfdocs/sound-howto.html Linux Serial HOWTO http://www.linux.or.jp/jf/jfdocs/serial-howto.html 6 5 1. 2. A,B,C,D
14 A. A A.1 sample1.c 1 /*---------------------------------------------- 57 2 58 /* 3 59 arg = RATE; 4 sound/sample1.c 60 status = ioctl(fd, SOUND_PCM_WRITE_RATE, &arg); 5 ---------------------------------------------- 61 if (status == -1){ /* 6 62 perror("sound_pcm_write_write ioctl failed"); 7 #include <unistd.h> 63 return 1; 8 #include <fcntl.h> 64 } 9 #include <sys/types.h> 65 10 #include <sys/ioctl.h> 66 /*************************** 11 #include <stdlib.h> 67 /* 12 #include <stdio.h> 68 /*************************** 13 #include <linux/soundcard.h> 69 14 15 #define LENGTH 5 /* ( ) 71 fp = fopen("test.raw", "rb"); 16 #define RATE 16000 /* 72 if(fp == NULL){ /* 17 #define SIZE 16 /* 18 #define CHANNELS 2 /* 1 = mono 2 = stereo 19 20 int main() 21 { 77 /* 22 int fd; /* 78 count = fread(buf, sizeof(short), LENGTH*RATE*CHANNELS, fp); 23 int arg; /* ioctl calls 79 if(count!= LENGTH*RATE*CHANNELS){ /* 24 int status; /* 25 int count; /* 80 printf("read count is %d\n", count); 81 // return 1; 26 unsigned short buf[length*rate*channels]; 82 } 27 char *bufp; /* 83 28 FILE *fp; /* 84 /* 85 fclose(fp); 29 86 30 /* 87 /*************************** 31 /* (O_WRONLY) 32 fd = open("/dev/audio", O_WRONLY); 33 if(fd < 0){ /* 34 perror("open of /dev/audio failed"); 35 return 1; 36 } 37 38 /*************************** 39 /* 40 /*************************** 41 42 /* 43 arg = SIZE; 44 status = ioctl(fd, SOUND_PCM_WRITE_BITS, &arg); 45 if (status == -1){ /* 102 46 perror("sound_pcm_write_bits ioctl failed"); 103 /* 47 return 1; 104 status = close(fd); 48 } 105 if(status == -1){ /* 49 106 perror("device close failed"); 50 /* 107 return 1; 51 arg = CHANNELS; 108 } 52 status = ioctl(fd, SOUND_PCM_WRITE_CHANNELS, &arg); 109 53 if (status == -1){ /* 54 perror("sound_pcm_write_channels ioctl failed"); 55 return 1; 56 } 70 /* 73 perror("file open failed"); 74 return 1; 75 } 76 88 /* 89 /*************************** 90 count *= sizeof(unsigned short); 91 /* write 92 bufp = (char *)buf; 93 while (count>0) { 94 status = write(fd, bufp, count); 95 if (status==-1) { /* 96 perror("wrote wrong number of bytes"); 97 return 1; 98 } 99 count -= status; 100 bufp += status; 101 } 110 return 1; 111 } 112 113 //end of file
A.2. sample2.c 15 A.2 sample2.c 1 /*---------------------------------------------- 64 perror("ioctl (TCSETA) failed"); 2 65 return 1; 3 66 } 4 serial/sample2.c 67 5 ---------------------------------------------- 68 /*************************** 6 69 /* client 7 #include <sys/types.h> 70 /*************************** 8 #include <sys/stat.h> 71 9 #include <termio.h> 72 while(1) { 10 #include <sys/file.h> 11 #include <stdio.h> 74 printf("waiting for client to send characters.\n"); 12 #include <stdlib.h> 75 /* 13 #include <fcntl.h> 76 total_count = 0; 14 #include <unistd.h> 77 ptr = buffer; 15 #include <signal.h> 78 16 #include <string.h> 79 /* \0 17 read 18 int main() 80 while(1) { 19 { 81 /* 1 20 int fd; /* 82 count = read(fd, &c, 1); 21 struct termio tio; /* 83 if(count < 0) { 22 int count, total_count;/* 84 perror("read failed"); 23 int status; /* 85 return 1; 24 char buffer[100]; /* 86 } 25 char c; /* 87 total_count += count; 26 char *ptr; /* 27 89 /* TODO = c; /* 28 /*************************** 29 /* 30 /*************************** 31 32 /* (O_RDWR) 33 fd = open("/dev/ttys0", O_RDWR); 34 if(fd < 0) { 35 perror("open of /dev/ttys0 failed"); 36 return 1; 37 } 38 39 /*************************** 40 /* 100 /* 41 /*************************** 101 printf("read %d bytes\n", total_count); 42 102 printf("buffer is\n\n %s \n\n", buffer); 43 /* TCGETA 103 break; 104 } 105 }/* 44 status = ioctl(fd, TCGETA, &tio); 45 if(status < 0) { 46 perror("ioctl (TCGETA) failed"); 47 return 1; 48 } 49 50 /* 111 } 51 tio.c_cflag = B9600; 112 }/* while 52 /* 113 53 tio.c_lflag &= ~ECHO; 114 /* 54 /* 115 status = close(fd); 55 tio.c_lflag &= ~ICANON; 56 57 /* 1 read 58 tio.c_cc[vmin] = 1; 59 tio.c_cc[vtime] = 0; 60 121 return 1; 61 /* TCSETA 122 } 62 status = ioctl(fd, TCSETA, &tio); 63 if(status < 0) { 73 /* client \n 88 /* :TODO 90 /* 91 ptr++; 92 93 if(c == \n c == \0 ) { 94 /* 95 tcflush(fd, TCIFLUSH); 96 97 /* \0 98 *ptr = \0 ; 99 106 107 /* 1 q 108 if(buffer[0] == q ) { 109 printf("quit servicing!\n"); 110 break; 116 if(status == -1) { 117 perror("device close failed"); 118 return 1; 119 } 120 123 124 /* end of file
16 A. A.3 sample3.c 1 /*---------------------------------------------- 46 } 2 47 3 48 /* 4 serial/sample3.c 49 tio.c_cflag = B9600; 5 ---------------------------------------------- 50 /* 6 51 tio.c_lflag &= ~ECHO; 7 #include <sys/types.h> 52 /* 8 #include <sys/stat.h> 53 tio.c_lflag &= ~ICANON; 9 #include <termio.h> 54 10 #include <sys/file.h> 55 /* 1 read 11 #include <stdio.h> 56 tio.c_cc[vmin] = 1; 12 #include <stdlib.h> 57 tio.c_cc[vtime] = 0; 13 #include <string.h> 58 14 #include <fcntl.h> 59 /* TCSETA 15 #include <unistd.h> 60 status = ioctl(fd, TCSETA, &tio); 16 #include <signal.h> 61 if(status < 0) { 17 62 perror("ioctl (TCSETA) failed"); 18 int main() 63 return 1; 19 { 64 } 20 int fd; /* 65 21 struct termio tio; /* 66 /*************************** 22 int count; /* 67 /* server 23 int status; /* 68 /*************************** 24 char buffer[100]; /* 69 25 70 /* 26 /*************************** 27 /* 28 /*************************** 29 30 /* (O_RDWR) 31 fd = open("/dev/ttys0", O_RDWR); 32 if(fd < 0) { 33 perror("open of /dev/ttys0 failed"); 34 return 1; 35 } 36 37 /*************************** 38 /* 39 /*************************** 40 85 return 1; 41 /* TCGETA 86 } 42 status = ioctl(fd, TCGETA, &tio); 43 if(status < 0) { 44 perror("ioctl (TCGETA) failed"); 45 return 1; 71 /* 72 printf("please input some words and return.\n"); 73 sprintf(buffer, "Hello! JSK Lab.\n"); 74 75 /* server 76 count = write(/* TODO, buffer, strlen(buffer)); 77 78 /* 79 printf("write %d bytes\n", /* TODO ); 80 81 /* 82 status = close(fd); 83 if(status == -1) { 84 perror("device close failed"); 87 88 return 1; 89 } 90 91 /* end of file