- added EXPERIMENTAL support for 1-socket mSiS-PM devices.
- fixed some coding style issues
This commit is contained in:
parent
1ae5ef2d99
commit
0b251c6e34
78
src/main.c
78
src/main.c
|
@ -40,19 +40,16 @@
|
||||||
#include "socket.h"
|
#include "socket.h"
|
||||||
#include "main.h"
|
#include "main.h"
|
||||||
|
|
||||||
#define BSIZE 65536
|
#define BSIZE 65536
|
||||||
#define MAXGEMBIRD 32
|
|
||||||
#define VENDOR_ID 0x04B4
|
|
||||||
#define PRODUCT_ID 0xFD11
|
|
||||||
#define USB_DIR_IN 0x80 /* to host */
|
|
||||||
#define USB_DIR_OUT 0 /* to device */
|
|
||||||
|
|
||||||
char* homedir=0;
|
char* homedir=0;
|
||||||
extern int errno;
|
extern int errno;
|
||||||
int debug=0;
|
int debug=0;
|
||||||
|
int verbose=1;
|
||||||
|
|
||||||
|
|
||||||
#ifndef WEBLESS
|
#ifndef WEBLESS
|
||||||
void process(int out,char*v,usb_dev_handle *udev)
|
void process(int out,char*v,usb_dev_handle *udev,int id)
|
||||||
{
|
{
|
||||||
char xbuffer[BSIZE+2];
|
char xbuffer[BSIZE+2];
|
||||||
char filename[1024],*ptr;
|
char filename[1024],*ptr;
|
||||||
|
@ -149,7 +146,7 @@ void process(int out,char*v,usb_dev_handle *udev)
|
||||||
{
|
{
|
||||||
if(debug)
|
if(debug)
|
||||||
fprintf(stderr,"\nON(%s)\n",num);
|
fprintf(stderr,"\nON(%s)\n",num);
|
||||||
if( sispm_switch_on(udev,atoi(num)) !=0)
|
if( sispm_switch_on(udev,id,atoi(num)) !=0)
|
||||||
{ send(out,pos,neg-pos-1,0);
|
{ send(out,pos,neg-pos-1,0);
|
||||||
} else
|
} else
|
||||||
{ send(out,neg,trm-neg,0);
|
{ send(out,neg,trm-neg,0);
|
||||||
|
@ -160,7 +157,7 @@ void process(int out,char*v,usb_dev_handle *udev)
|
||||||
assert(("Command-Format: $$exec(#)?select:forget$$ ERROR at final $",(trm[1]=='$')));
|
assert(("Command-Format: $$exec(#)?select:forget$$ ERROR at final $",(trm[1]=='$')));
|
||||||
if(debug)
|
if(debug)
|
||||||
fprintf(stderr,"\nOFF(%s)\n",num);
|
fprintf(stderr,"\nOFF(%s)\n",num);
|
||||||
if( sispm_switch_off(udev,atoi(num)) !=0)
|
if( sispm_switch_off(udev,id,atoi(num)) !=0)
|
||||||
{ send(out,pos,neg-pos-1,0);
|
{ send(out,pos,neg-pos-1,0);
|
||||||
} else
|
} else
|
||||||
{ send(out,neg,trm-neg,0);
|
{ send(out,neg,trm-neg,0);
|
||||||
|
@ -171,8 +168,8 @@ void process(int out,char*v,usb_dev_handle *udev)
|
||||||
assert(("Command-Format: $$exec(#)?select:forget$$ ERROR at final $",(trm[1]=='$')));
|
assert(("Command-Format: $$exec(#)?select:forget$$ ERROR at final $",(trm[1]=='$')));
|
||||||
if(debug)
|
if(debug)
|
||||||
fprintf(stderr,"\nTOGGLE(%s)\n",num);
|
fprintf(stderr,"\nTOGGLE(%s)\n",num);
|
||||||
if( sispm_switch_getstatus(udev,atoi(num),&status) == 0)
|
if( sispm_switch_getstatus(udev,id,atoi(num),&status) == 0)
|
||||||
{ sispm_switch_on(udev,atoi(num));
|
{ sispm_switch_on(udev,id,atoi(num));
|
||||||
send(out,pos,neg-pos-1,0);
|
send(out,pos,neg-pos-1,0);
|
||||||
} else
|
} else
|
||||||
{ sispm_switch_off(udev,atoi(num));
|
{ sispm_switch_off(udev,atoi(num));
|
||||||
|
@ -184,7 +181,7 @@ void process(int out,char*v,usb_dev_handle *udev)
|
||||||
assert(("Command-Format: $$exec(#)?select:forget$$ ERROR at final $",(trm[1]=='$')));
|
assert(("Command-Format: $$exec(#)?select:forget$$ ERROR at final $",(trm[1]=='$')));
|
||||||
if(debug)
|
if(debug)
|
||||||
fprintf(stderr,"\nSTATUS(%s)\n",num);
|
fprintf(stderr,"\nSTATUS(%s)\n",num);
|
||||||
if( sispm_switch_getstatus(udev,atoi(num),&status) != 0)
|
if( sispm_switch_getstatus(udev,id,atoi(num),&status) != 0)
|
||||||
{ send(out,pos,neg-pos-1,0);
|
{ send(out,pos,neg-pos-1,0);
|
||||||
} else
|
} else
|
||||||
{ send(out,neg,trm-neg,0);
|
{ send(out,neg,trm-neg,0);
|
||||||
|
@ -321,23 +318,26 @@ const char*answer(char*in)
|
||||||
|
|
||||||
void parse_command_line(int argc, char* argv[], int count, struct usb_device*dev[])
|
void parse_command_line(int argc, char* argv[], int count, struct usb_device*dev[])
|
||||||
{
|
{
|
||||||
int verbose=1;
|
|
||||||
int numeric=0;
|
int numeric=0;
|
||||||
int c;
|
int c;
|
||||||
int i;
|
int i;
|
||||||
int von=1, bis=4;
|
int von=1, bis=4;
|
||||||
int status;
|
int status;
|
||||||
usb_dev_handle *udev;
|
usb_dev_handle *udev;
|
||||||
|
unsigned int id; //product id of current device
|
||||||
char*onoff[] = { "off", "on", "0", "1" };
|
char*onoff[] = { "off", "on", "0", "1" };
|
||||||
|
#ifndef WEBLESS
|
||||||
char* bindaddr=0;
|
char* bindaddr=0;
|
||||||
|
#endif
|
||||||
|
unsigned int outlet;
|
||||||
|
|
||||||
#ifdef BINDADDR
|
#ifdef BINDADDR
|
||||||
if (BINDADDR!="")
|
if (BINDADDR!="")
|
||||||
bindaddr=BINDADDR;
|
bindaddr=BINDADDR;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
udev = get_handle( dev[0] );
|
udev = get_handle( dev[0] );
|
||||||
|
id = get_id(dev[0]);
|
||||||
while( (c=getopt(argc, argv,"i:o:f:b:g:lqvhnsd:u:p:")) != -1 )
|
while( (c=getopt(argc, argv,"i:o:f:b:g:lqvhnsd:u:p:")) != -1 )
|
||||||
{
|
{
|
||||||
if( c=='o' || c=='f' || c=='g' )
|
if( c=='o' || c=='f' || c=='g' )
|
||||||
|
@ -376,8 +376,16 @@ void parse_command_line(int argc, char* argv[], int count, struct usb_device*dev
|
||||||
case 's':
|
case 's':
|
||||||
for(status=0; status<count; status++)
|
for(status=0; status<count; status++)
|
||||||
{
|
{
|
||||||
printf("Gembird #%d is USB device %s\n",
|
printf("Gembird #%d is USB device %s.",
|
||||||
status,dev[status]->filename);
|
status,dev[status]->filename);
|
||||||
|
if (id==PRODUCT_ID_SISPM)
|
||||||
|
{
|
||||||
|
printf("This device is a 4-socket SiS-PM.\n");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("This device is a 1-socket mSiS-PM.\n");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
status=0;
|
status=0;
|
||||||
break;
|
break;
|
||||||
|
@ -386,6 +394,7 @@ void parse_command_line(int argc, char* argv[], int count, struct usb_device*dev
|
||||||
status = atoi(optarg);
|
status = atoi(optarg);
|
||||||
if(status>=count) status=count-1;
|
if(status>=count) status=count-1;
|
||||||
udev = get_handle(dev[status]);
|
udev = get_handle(dev[status]);
|
||||||
|
id= get_id(dev[status]);
|
||||||
if(udev==NULL)
|
if(udev==NULL)
|
||||||
fprintf(stderr, "No access to Gembird #%d USB device %s\n",
|
fprintf(stderr, "No access to Gembird #%d USB device %s\n",
|
||||||
status, dev[status]->filename );
|
status, dev[status]->filename );
|
||||||
|
@ -393,16 +402,28 @@ void parse_command_line(int argc, char* argv[], int count, struct usb_device*dev
|
||||||
if(verbose) printf("Accessing Gembird #%d USB device %s\n",
|
if(verbose) printf("Accessing Gembird #%d USB device %s\n",
|
||||||
status, dev[status]->filename );
|
status, dev[status]->filename );
|
||||||
break;
|
break;
|
||||||
case 'o': sispm_switch_on(udev,i);
|
case 'o':
|
||||||
|
{
|
||||||
|
outlet=check_outlet_number(id, i);
|
||||||
|
sispm_switch_on(udev,id,outlet);
|
||||||
if(verbose) printf("Switched outlet %d %s\n",i,onoff[1+numeric]);
|
if(verbose) printf("Switched outlet %d %s\n",i,onoff[1+numeric]);
|
||||||
break;
|
break;
|
||||||
case 'f': sispm_switch_off(udev,i);
|
}
|
||||||
|
case 'f':
|
||||||
|
{
|
||||||
|
outlet=check_outlet_number(id, i);
|
||||||
|
sispm_switch_off(udev,id,outlet);
|
||||||
if(verbose) printf("Switched outlet %d %s\n",i,onoff[0+numeric]);
|
if(verbose) printf("Switched outlet %d %s\n",i,onoff[0+numeric]);
|
||||||
break;
|
break;
|
||||||
case 'g': sispm_switch_getstatus(udev,i,&status);
|
}
|
||||||
|
case 'g':
|
||||||
|
{
|
||||||
|
outlet=check_outlet_number(id, i);
|
||||||
|
sispm_switch_getstatus(udev,id,outlet,&status);
|
||||||
if(verbose) printf("Status of outlet %d:\t",i);
|
if(verbose) printf("Status of outlet %d:\t",i);
|
||||||
printf("%s\n",onoff[status+numeric]);
|
printf("%s\n",onoff[status+numeric]);
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
#ifndef WEBLESS
|
#ifndef WEBLESS
|
||||||
case 'p':
|
case 'p':
|
||||||
listenport=atoi(optarg);
|
listenport=atoi(optarg);
|
||||||
|
@ -425,7 +446,7 @@ void parse_command_line(int argc, char* argv[], int count, struct usb_device*dev
|
||||||
{
|
{
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
l_listen(s,udev);
|
l_listen(s,udev,id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -470,6 +491,8 @@ int main(int argc, char** argv)
|
||||||
struct usb_device *dev, *usbdev[MAXGEMBIRD];
|
struct usb_device *dev, *usbdev[MAXGEMBIRD];
|
||||||
int count=0;
|
int count=0;
|
||||||
|
|
||||||
|
memset(usbdev,0,sizeof(usbdev));
|
||||||
|
|
||||||
usb_init();
|
usb_init();
|
||||||
usb_find_busses();
|
usb_find_busses();
|
||||||
usb_find_devices();
|
usb_find_devices();
|
||||||
|
@ -492,11 +515,24 @@ int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
for (dev = bus->devices; dev; dev = dev->next)
|
for (dev = bus->devices; dev; dev = dev->next)
|
||||||
{
|
{
|
||||||
if (dev->descriptor.idVendor == VENDOR_ID && dev->descriptor.idProduct==PRODUCT_ID)
|
if (dev->descriptor.idVendor == VENDOR_ID && dev->descriptor.idProduct==PRODUCT_ID_SISPM)
|
||||||
{
|
{
|
||||||
usbdev[count++] = dev;
|
usbdev[count++] = dev;
|
||||||
assert((MAXGEMBIRD>count,"Please recompile for that many devices!"));
|
|
||||||
}
|
}
|
||||||
|
if (dev->descriptor.idVendor == VENDOR_ID && dev->descriptor.idProduct==PRODUCT_ID_MSISPM_OLD)
|
||||||
|
{
|
||||||
|
usbdev[count++] = dev;
|
||||||
|
}
|
||||||
|
if (dev->descriptor.idVendor == VENDOR_ID && dev->descriptor.idProduct==PRODUCT_ID_MSISPM_FLASH)
|
||||||
|
{
|
||||||
|
usbdev[count++] = dev;
|
||||||
|
}
|
||||||
|
if (count==MAXGEMBIRD)
|
||||||
|
{
|
||||||
|
fprintf(stderr,"%d devices found. Please recompile if you need to support more devices!\n",count);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(count==0)
|
if(count==0)
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#ifndef MAIN_H
|
#ifndef MAIN_H
|
||||||
#define MAIN_H 1
|
#define MAIN_H
|
||||||
|
|
||||||
#define MAXANSWER 8192
|
#define MAXANSWER 8192
|
||||||
|
|
||||||
|
@ -10,6 +10,8 @@
|
||||||
|
|
||||||
#define HOMEDIR "/usr/local/share/httpd/sispmctl/doc"
|
#define HOMEDIR "/usr/local/share/httpd/sispmctl/doc"
|
||||||
|
|
||||||
|
extern int verbose;
|
||||||
|
|
||||||
const char*answer(char*in);
|
const char*answer(char*in);
|
||||||
void process(int out,char*v,usb_dev_handle *udev);
|
void process(int out,char*v,usb_dev_handle *udev);
|
||||||
extern int debug;
|
extern int debug;
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#ifndef NETHELP_H
|
#ifndef NETHELP_H
|
||||||
#define NETHELP_H 1
|
#define NETHELP_H
|
||||||
|
|
||||||
int sock_write_bytes(int sockfd, const unsigned char *buff, int len);
|
int sock_write_bytes(int sockfd, const unsigned char *buff, int len);
|
||||||
int sock_read_bytes(int sockfd, const unsigned char *buff, int len);
|
int sock_read_bytes(int sockfd, const unsigned char *buff, int len);
|
||||||
|
|
|
@ -35,6 +35,14 @@
|
||||||
#include "sispm_ctl.h"
|
#include "sispm_ctl.h"
|
||||||
|
|
||||||
|
|
||||||
|
extern int verbose;
|
||||||
|
|
||||||
|
int get_id( struct usb_device* dev)
|
||||||
|
{
|
||||||
|
assert(dev!=0);
|
||||||
|
return dev->descriptor.idProduct;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int usb_command(usb_dev_handle *udev, int b1, int b2, int *status )
|
int usb_command(usb_dev_handle *udev, int b1, int b2, int *status )
|
||||||
{
|
{
|
||||||
|
@ -102,3 +110,44 @@ usb_dev_handle*get_handle(struct usb_device*dev)
|
||||||
}
|
}
|
||||||
return udev;
|
return udev;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int check_outlet_number(int id, int outlet)
|
||||||
|
{
|
||||||
|
if (id==PRODUCT_ID_MSISPM_OLD || id==PRODUCT_ID_MSISPM_FLASH)
|
||||||
|
{
|
||||||
|
if (outlet!=1)
|
||||||
|
{
|
||||||
|
outlet=1;
|
||||||
|
if (verbose==1)
|
||||||
|
fprintf(stderr,"mSIS-PM devices only features one outlet. Number changed to 1\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (id==PRODUCT_ID_SISPM)
|
||||||
|
{
|
||||||
|
if (outlet>4)
|
||||||
|
{
|
||||||
|
outlet=1;
|
||||||
|
if (verbose==1)
|
||||||
|
fprintf(stderr,"SIS-PM devices only feature 4 outlets. Number changed to 1\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return outlet;
|
||||||
|
}
|
||||||
|
|
||||||
|
int sispm_switch_on(usb_dev_handle * udev,int id, int outlet)
|
||||||
|
{
|
||||||
|
outlet=check_outlet_number(id,outlet);
|
||||||
|
return usb_command( udev, 3*outlet, 0x03, NULL ) ;
|
||||||
|
}
|
||||||
|
|
||||||
|
int sispm_switch_off(usb_dev_handle * udev,int id, int outlet)
|
||||||
|
{
|
||||||
|
outlet=check_outlet_number(id,outlet);
|
||||||
|
return usb_command( udev, 3*outlet, 0x00, NULL );
|
||||||
|
}
|
||||||
|
|
||||||
|
int sispm_switch_getstatus(usb_dev_handle * udev,int id, int outlet,int *status)
|
||||||
|
{
|
||||||
|
outlet=check_outlet_number(id,outlet);
|
||||||
|
return usb_command( udev, 3*outlet, 0x03, status );
|
||||||
|
}
|
||||||
|
|
|
@ -26,9 +26,24 @@
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#ifndef SISPM_CTL_H
|
||||||
|
#define SISPM_CTL_H
|
||||||
|
|
||||||
#define MAXGEMBIRD 32
|
#define MAXGEMBIRD 32
|
||||||
|
|
||||||
#define VENDOR_ID 0x04B4
|
#define VENDOR_ID 0x04B4
|
||||||
#define PRODUCT_ID 0xFD11
|
|
||||||
|
/* USB Product IDs for different sis-pm devices*/
|
||||||
|
/*#define PRODUCT_ID_SISPM 0xFD11
|
||||||
|
#define PRODUCT_ID_MSISPM_OLD 0xFD10
|
||||||
|
#define PRODUCT_ID_MSISPM_FLASH 0xFD12
|
||||||
|
*/
|
||||||
|
|
||||||
|
#define PRODUCT_ID_SISPM 0xFD10
|
||||||
|
#define PRODUCT_ID_MSISPM_OLD 0xFD11
|
||||||
|
#define PRODUCT_ID_MSISPM_FLASH 0xFD12
|
||||||
|
|
||||||
|
|
||||||
#define USB_DIR_IN 0x80 /* to host */
|
#define USB_DIR_IN 0x80 /* to host */
|
||||||
#define USB_DIR_OUT 0 /* to device */
|
#define USB_DIR_OUT 0 /* to device */
|
||||||
#define cpu_to_le16(a) (a)
|
#define cpu_to_le16(a) (a)
|
||||||
|
@ -38,10 +53,12 @@ int usb_command(usb_dev_handle *udev, int b1, int b2, int *status );
|
||||||
|
|
||||||
#define sispm_buzzer_on(udev) usb_command( udev, 1, 0x01, NULL )
|
#define sispm_buzzer_on(udev) usb_command( udev, 1, 0x01, NULL )
|
||||||
#define sispm_buzzer_off(udev) usb_command( udev, 1, 0x00, NULL )
|
#define sispm_buzzer_off(udev) usb_command( udev, 1, 0x00, NULL )
|
||||||
#define sispm_switch_on(udev,outlet) usb_command( udev, 3*outlet, 0x03, NULL )
|
|
||||||
#define sispm_switch_off(udev,outlet) usb_command( udev, 3*outlet, 0x00, NULL )
|
|
||||||
#define sispm_switch_getstatus(udev,outlet,status) \
|
|
||||||
usb_command( udev, 3*outlet, 0x03, status )
|
|
||||||
|
|
||||||
|
int get_id( struct usb_device* dev);
|
||||||
|
int sispm_switch_on(usb_dev_handle * udev,int id, int outlet);
|
||||||
|
int sispm_switch_off(usb_dev_handle * udev,int id, int outlet);
|
||||||
|
int sispm_switch_getstatus(usb_dev_handle * udev,int id, int outlet,int *status);
|
||||||
|
int check_outlet_number(int id, int outlet);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <usb.h>
|
|
||||||
|
|
|
@ -36,7 +36,7 @@
|
||||||
int listenport=LISTENPORT;
|
int listenport=LISTENPORT;
|
||||||
|
|
||||||
|
|
||||||
void l_listen(int*sock, usb_dev_handle *udev) {
|
void l_listen(int*sock, usb_dev_handle *udev,int id) {
|
||||||
int i;
|
int i;
|
||||||
int s;
|
int s;
|
||||||
int connected=0;
|
int connected=0;
|
||||||
|
@ -104,7 +104,7 @@ void l_listen(int*sock, usb_dev_handle *udev) {
|
||||||
fprintf(stderr,"Buffer contains: »%s«\n",buffer);
|
fprintf(stderr,"Buffer contains: »%s«\n",buffer);
|
||||||
send(s,tosend,strlen(tosend),0);
|
send(s,tosend,strlen(tosend),0);
|
||||||
#else
|
#else
|
||||||
process(s,buffer,udev);
|
process(s,buffer,udev,id);
|
||||||
#endif
|
#endif
|
||||||
memset(buffer,0,BUFFERSIZE);
|
memset(buffer,0,BUFFERSIZE);
|
||||||
close(s);
|
close(s);
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
#ifndef LOCAL_H
|
#ifndef LOCAL_H
|
||||||
#define LOCAL_H 1
|
#define LOCAL_H
|
||||||
|
|
||||||
#define LISTENPORT 2638
|
#define LISTENPORT 2638
|
||||||
extern int listenport;
|
extern int listenport;
|
||||||
int*socket_init(char*bindaddr);
|
int*socket_init(char*bindaddr);
|
||||||
void l_listen(int*sock, usb_dev_handle *udev);
|
void l_listen(int*sock, usb_dev_handle *udev,int id);
|
||||||
|
|
||||||
#endif /* ! LOCAL_H */
|
#endif /* ! LOCAL_H */
|
||||||
|
|
Loading…
Reference in New Issue