Index: usbb2k-main.c =================================================================== RCS file: /sources/usbb2k-api/usbb2k-api/src/usbb2k-main.c,v retrieving revision 1.12 diff -u -r1.12 usbb2k-main.c --- usbb2k-main.c 2 Nov 2005 10:15:15 -0000 1.12 +++ usbb2k-main.c 31 Jul 2006 07:16:59 -0000 @@ -28,8 +28,10 @@ #include #include #include +#include #include "usbb2k-api.h" + #define USBB2K_SOCKET "/tmp/usbb2k.sock" #define USBB2K_PID "/var/run/usbb2k_api.pid" @@ -91,12 +93,14 @@ { int err; char data[URB_LENGTH]; + sem_wait(&usb_sem_device) ; err=usb_control_msg(dev_h,USB_MSG_OUT,0x9,0x200,0x3,URB_CMD[URB_HANDSET],URB_LENGTH,TIME_OUT); if (err!=URB_LENGTH) { syslog(LOG_ERR,"error send check_handset:%d [%x]",err,dev_h); } err=usb_interrupt_read(dev_h,0x81,data,URB_LENGTH,TIME_OUT); + sem_post(&usb_sem_device) ; if (err==URB_LENGTH) { if (data[4]==0x02) @@ -117,8 +121,10 @@ { int err; char data[URB_LENGTH]; + sem_wait(&usb_sem_device) ; err=usb_control_msg(dev_h,USB_MSG_OUT,0x9,0x200,0x3,URB_CMD[URB_KEYPRESS],URB_LENGTH,TIME_OUT); err=usb_interrupt_read(dev_h,0x81,data,URB_LENGTH,TIME_OUT); + sem_post(&usb_sem_device) ; if (err==URB_LENGTH) { return data[4]; } @@ -143,8 +149,10 @@ data_copy(data,URB_CMD[URB_KEYCODE],URB_LENGTH); data[3]=keynum; data[15]=0x7e - keynum; + sem_wait(&usb_sem_device) ; err=usb_control_msg(dev_h,USB_MSG_OUT,0x9,0x200,0x3,data,URB_LENGTH,TIME_OUT); err=usb_interrupt_read(dev_h,0x81,data,URB_LENGTH,TIME_OUT); + sem_post(&usb_sem_device) ; if (err==URB_LENGTH) { //if (data[0]!=URB_CMD[URB_KEYCODE][0]) return -2; @@ -213,6 +221,16 @@ sprintf(arg,"%s",cmd_str+5); return CMD_RING; } + if (buffcmp(cmd_str,"DIAL TONE")==0) + { + if (strlen(cmd_str)<10) + { + sprintf(arg,"0"); + return CMD_DIAL_TONE; + } + sprintf(arg,"%s",cmd_str+10); + return CMD_DIAL_TONE; + } if (buffcmp(cmd_str,"Q")==0) { return CMD_QUIT; @@ -234,14 +252,18 @@ char data[URB_LENGTH]; if (status==ON) { + sem_wait(&usb_sem_device) ; err=usb_control_msg(dev_h,USB_MSG_OUT,0x9,0x200,0x03,URB_CMD[URB_TONE_ON],URB_LENGTH,TIME_OUT); err=usb_interrupt_read(dev_h,0x81,data,URB_LENGTH,TIME_OUT); + sem_post(&usb_sem_device) ; return ON; } else { + sem_wait(&usb_sem_device) ; err=usb_control_msg(dev_h,USB_MSG_OUT,0x9,0x200,0x03,URB_CMD[URB_TONE_OFF],URB_LENGTH,TIME_OUT); err=usb_interrupt_read(dev_h,0x81,data,URB_LENGTH,TIME_OUT); + sem_post(&usb_sem_device) ; return OFF; } } @@ -254,14 +276,18 @@ if (status==ON) { + sem_wait(&usb_sem_device) ; err=usb_control_msg(dev_h,USB_MSG_OUT,0x9,0x200,0x03,URB_CMD[URB_RING_ON],URB_LENGTH,TIME_OUT); err=usb_interrupt_read(dev_h,0x81,data,URB_LENGTH,TIME_OUT); + sem_post(&usb_sem_device) ; return ON; } else { + sem_wait(&usb_sem_device) ; err=usb_control_msg(dev_h,USB_MSG_OUT,0x9,0x200,0x03,URB_CMD[URB_RING_OFF],URB_LENGTH,TIME_OUT); err=usb_interrupt_read(dev_h,0x81,data,URB_LENGTH,TIME_OUT); + sem_post(&usb_sem_device) ; return OFF; } } @@ -274,18 +300,22 @@ char data[URB_LENGTH]; if (mode==USB_MODE) { + sem_wait(&usb_sem_device) ; err=usb_control_msg(dev_h,USB_MSG_OUT,0x9,0x200,0x03,URB_CMD[URB_LED_USB_ON],URB_LENGTH,TIME_OUT); err=usb_interrupt_read(dev_h,0x81,data,URB_LENGTH,TIME_OUT); err=usb_control_msg(dev_h,USB_MSG_OUT,0x9,0x200,0x03,URB_CMD[URB_USB_ON],URB_LENGTH,TIME_OUT); err=usb_interrupt_read(dev_h,0x81,data,URB_LENGTH,TIME_OUT); + sem_post(&usb_sem_device) ; return USB_MODE; } if (mode==PSTN_MODE) { + sem_wait(&usb_sem_device) ; err=usb_control_msg(dev_h,USB_MSG_OUT,0x9,0x200,0x03,URB_CMD[URB_LED_USB_OFF],URB_LENGTH,TIME_OUT); err=usb_interrupt_read(dev_h,0x81,data,URB_LENGTH,TIME_OUT); err=usb_control_msg(dev_h,USB_MSG_OUT,0x9,0x200,0x03,URB_CMD[URB_USB_OFF],URB_LENGTH,TIME_OUT); err=usb_interrupt_read(dev_h,0x81,data,URB_LENGTH,TIME_OUT); + sem_post(&usb_sem_device) ; return PSTN_MODE; } return -1; @@ -340,7 +370,7 @@ // Wait client instruction void * client_th(void *client_arg){ - int msgl,ring_type; + int msgl,ring_type,dial_tone_type ; int client,ret; usb_dev_handle *dev_h; char msg[MSG_MAXL+1],arg_cmd[MSG_MAXL+1]; @@ -366,6 +396,16 @@ case CMD_QUIT: msgl=-2; break; + case CMD_DIAL_TONE: + dial_tone_type=atoi(arg_cmd); + if ( handset_state==ON ) { + if ( dial_tone_type > 0 ) { + tone_state=usbb2k_tone(dev_h,ON) ; + } else { + tone_state=usbb2k_tone(dev_h,OFF) ; + } + } + break; case CMD_RING: ring_type=atoi(arg_cmd); if (ring_type>0 && ringing_state==OFF && handset_state==OFF) { @@ -403,7 +443,7 @@ usblect_th(void *client_arg) { int client; - int keypress_state,tone_flag; + int keypress_state; int handset_state_flag,keypress_state_flag,key_code; int ringing_mode_flag; usb_dev_handle *dev_h; @@ -442,15 +482,15 @@ } else { - tone_flag=usbb2k_tone(dev_h,ON); + tone_state=usbb2k_tone(dev_h,ON); } } else { /* RACROCHE */ sprintf(msg,"HANDSET OFF\n"); - if (tone_flag==ON) + if (tone_state==ON) { - tone_flag=usbb2k_tone(dev_h,OFF); + tone_state=usbb2k_tone(dev_h,OFF); } } msgl=write(client,msg,strlen(msg)); @@ -473,9 +513,9 @@ msgl=write(client,msg,strlen(msg)); } else syslog(LOG_ERR,"Key ERROR %d : %d ",key_code,keypress_state); - if (tone_flag==ON) + if (tone_state==ON) { - tone_flag=usbb2k_tone(dev_h,OFF); + tone_state=usbb2k_tone(dev_h,OFF); } } } @@ -497,7 +537,7 @@ } } } - //usleep(100); + usleep(100); } /* usb loop stop -> close client */ sprintf(msg,"USB DISCONNECT\n"); @@ -651,6 +691,15 @@ close(sock_client); continue; } + + // Init sempaphore for usb device + if (sem_init(&usb_sem_device, 0, 1) < 0) { + sprintf(msg,"SEMAPHORE INIT ERROR\n"); + fprintf(stderr,msg); + write(sock_client,msg,strlen(msg)); + close(sock_client); + continue; + } // Thread ECOUTE USB if ((ret=pthread_create(&lect_thread,NULL,usblect_th,(void *)&client_arg))!=0){