Skip to content

Commit

Permalink
powerusb.c: add initialize() and finalize() from main().
Browse files Browse the repository at this point in the history
  • Loading branch information
nakj committed Mar 11, 2012
1 parent 40f1497 commit fc5c9f2
Showing 1 changed file with 29 additions and 17 deletions.
46 changes: 29 additions & 17 deletions powerusb.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,19 @@
#define Dprintf(fmt, ...) while(0){}
#endif

libusb_context *ctx = NULL;
libusb_device *dev;
libusb_device **devs;
struct libusb_device_handle *devh = NULL;

void send_cmd(struct libusb_device_handle *devh,int cmd)

uint8_t* send_cmd(struct libusb_device_handle *devh,int cmd)
{
int i;
uint8_t buf[64];
uint8_t buf[64],ret[2];

int size=0;

Dprintf("send_cmd:%x\n",cmd);

memset(buf, 0xff, sizeof(buf));
Expand All @@ -50,23 +57,23 @@ void send_cmd(struct libusb_device_handle *devh,int cmd)

Dprintf("send_cmd:read:");
for(i=0;i<2;i++){
ret[i] = buf[i];
Dprintf("%02x",buf[i]);
}
Dprintf("\n");


return ret;
}

void finalize(void){
libusb_close(devh);
libusb_exit(ctx);
}

int main(int argc, char **argv)
{
libusb_context *ctx = NULL;
libusb_device *dev;
libusb_device **devs;
struct libusb_device_handle *devh = NULL;
int initialize(void){
int r=1;
int i=0;
int cnt=0;

/* libusb initialize*/
if ((r = libusb_init(&ctx)) < 0) {
perror("libusb_init\n");
Expand All @@ -76,8 +83,8 @@ int main(int argc, char **argv)
Dprintf("init done\n");
}

/* confirm powerusb device*/
/* list up all usb devices*/
/* confirm powerusb device */
/* list up all usb devices */
if((libusb_get_device_list(ctx,&devs)) < 0) {
perror("no usb device found");
exit(1);
Expand All @@ -89,7 +96,7 @@ int main(int argc, char **argv)
perror("failed to get device descriptor\n");
return 1;
}
/* count how many PowerUSB device connect*/
/* count how many PowerUSB device connected */
if (desc.idVendor == USB_VENDOR_ID &&
desc.idProduct == USB_PRODUCT_ID) {
cnt++;
Expand All @@ -113,7 +120,8 @@ int main(int argc, char **argv)
if ((devh = libusb_open_device_with_vid_pid(ctx,USB_VENDOR_ID,
USB_PRODUCT_ID)) < 0 ) {
perror("can't find PowerUSB device\n");
goto out;
finalize();
exit(1);
} else {
Dprintf("PowerUSB device opened\n");
}
Expand All @@ -129,7 +137,13 @@ int main(int argc, char **argv)
exit(1);
}
}
return 0;
}

int main(int argc, char **argv)
{

initialize();
send_cmd(devh,CMD_GET_MODEL);
send_cmd(devh,CMD_GET_FIRM_VER);
send_cmd(devh,CMD_GET_STATE1);
Expand All @@ -148,10 +162,8 @@ int main(int argc, char **argv)
send_cmd(devh,0xb1);
send_cmd(devh,0xb2);
#endif
finalize();


out:
libusb_close(devh);
libusb_exit(ctx);
exit(0);
}

0 comments on commit fc5c9f2

Please sign in to comment.