boblight: update to boblight 478
[packages.git] / utils / huaweiaktbbo / src / huaweiAktBbo.c
1 /* HUAWEI 3G modems activator
2    Copyright (C) 2006 bobovsky bobovsky@kanoistika.sk  GPL
3    Copyright (C) 2009 Nuno Goncalves nunojpg@gmail.com GPL
4    This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License2.
5 */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <assert.h>
11 #include <signal.h>
12 #include <ctype.h>
13 #include <usb.h>
14
15 struct usb_dev_handle *devh;
16
17 void release_usb_device(int dummy) {
18         int ret;
19         ret = usb_release_interface(devh, 0);
20
21         if (!ret)
22                 printf("failed to release interface: %d\n", ret);
23
24         usb_close(devh);
25
26         if (!ret)
27                 printf("failed to close interface: %d\n", ret);
28
29         exit(1);
30 }
31
32 void list_devices() {
33         struct usb_bus *bus;
34         for (bus = usb_get_busses(); bus; bus = bus->next) {
35         struct usb_device *dev;
36
37         for (dev = bus->devices; dev; dev = dev->next)
38                 printf("0x%04x 0x%04x\n", dev->descriptor.idVendor, dev->descriptor.idProduct);
39     }
40 }    
41
42 struct usb_device *find_device() {
43         struct usb_bus *bus;
44
45         for (bus = usb_get_busses(); bus; bus = bus->next) {
46                 struct usb_device *dev;
47
48                 for (dev = bus->devices; dev; dev = dev->next) {
49                 if (dev->descriptor.idVendor == 0x12d1 && dev->descriptor.idProduct == 0x1001 || //Huawei E169, Huawei E169G
50                     dev->descriptor.idVendor == 0x12d1 && dev->descriptor.idProduct == 0x1003)   //Huawei E220, Huawei E156G
51                 return dev;
52                 }
53         }
54         return NULL;
55 }
56
57 int main(int argc, char **argv) {
58         int ret, vendor, product;
59         struct usb_device *dev;
60         char buf[65535], *endptr;
61
62         usb_init();
63         usb_find_busses();
64         usb_find_devices();
65
66         printf("Searching modem...");
67         dev = find_device();
68
69         if(dev == NULL){
70                 printf("No supported modem found\n");
71                 return 1;
72         }
73
74         printf("found supported modem!\n");
75
76         assert(dev);
77         devh = usb_open(dev);
78         assert(devh);
79
80         signal(SIGTERM, release_usb_device);
81
82         ret = usb_get_descriptor(devh, 0x0000001, 0x0000000, buf, 0x0000012);
83         usleep(1*1000);
84         ret = usb_get_descriptor(devh, 0x0000002, 0x0000000, buf, 0x0000009);
85         usleep(1*1000);
86         ret = usb_get_descriptor(devh, 0x0000002, 0x0000000, buf, 0x0000020);
87         usleep(1*1000);
88         ret = usb_control_msg(devh, USB_TYPE_STANDARD + USB_RECIP_DEVICE, USB_REQ_SET_FEATURE, 00000001, 0, buf, 0, 1000);
89
90         ret = usb_close(devh);
91         assert(ret == 0);
92         
93         printf("Modem poked!\n");
94         return 0;
95 }