source: thomson/elec/CrO2/software/powerSwitch.c@ d46151a

main
Last change on this file since d46151a was d46151a, checked in by Adrien Destugues <pulkomandy@…>, 12 years ago

Load data from K7 file.

git-svn-id: svn://localhost/thomson@7 85ae3b6b-dc8f-4344-a89d-598714f2e4e5

  • Property mode set to 100644
File size: 7.1 KB
Line 
1/* Name: powerSwitch.c
2 * Project: PowerSwitch based on AVR USB driver
3 * Author: Christian Starkjohann
4 * Creation Date: 2005-01-16
5 * Tabsize: 4
6 * Copyright: (c) 2005 by OBJECTIVE DEVELOPMENT Software GmbH
7 * License: GNU GPL v2 (see License.txt) or proprietary (CommercialLicense.txt)
8 * This Revision: $Id: powerSwitch.c 472 2008-01-21 18:21:59Z cs $
9 */
10
11/*
12General Description:
13This program controls the PowerSwitch USB device from the command line.
14It must be linked with libusb, a library for accessing the USB bus from
15Linux, FreeBSD, Mac OS X and other Unix operating systems. Libusb can be
16obtained from http://libusb.sourceforge.net/.
17*/
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <stdint.h>
23#include <lusb0_usb.h> /* this is libusb, see http://libusb.sourceforge.net/ */
24
25#define USBDEV_SHARED_VENDOR 0x16C0 /* VOTI */
26#define USBDEV_SHARED_PRODUCT 0x05DC /* Obdev's free shared PID */
27/* Use obdev's generic shared VID/PID pair and follow the rules outlined
28 * in firmware/usbdrv/USBID-License.txt.
29 */
30
31#define VENDORSTRING "pulkomandy.ath.cx"
32#define PRODUCTSTRING "CrO2"
33
34#define PSCMD_CONFIG 0
35#define PSCMD_GET 1
36#define PSCMD_PUT 2
37/* These are the vendor specific SETUP commands implemented by our USB device */
38
39static void usage(char *name)
40{
41 fprintf(stderr, "usage:\n");
42 fprintf(stderr, " %s status\n", name);
43 fprintf(stderr, " %s on <port> [<duration>]\n", name);
44 fprintf(stderr, " %s off <port> [<duration>]\n", name);
45 fprintf(stderr, " %s test\n\n", name);
46 fprintf(stderr, "Ports are single digits in the range 0...7\n");
47 fprintf(stderr, "The pulse duration for switching temporarily is given in seconds.\n");
48}
49
50
51static int usbGetStringAscii(usb_dev_handle *dev, int index, int langid, char *buf, int buflen)
52{
53char buffer[256];
54int rval, i;
55
56 if((rval = usb_control_msg(dev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) + index, langid, buffer, sizeof(buffer), 1000)) < 0)
57 return rval;
58 if(buffer[1] != USB_DT_STRING)
59 return 0;
60 if((unsigned char)buffer[0] < rval)
61 rval = (unsigned char)buffer[0];
62 rval /= 2;
63 /* lossy conversion to ISO Latin1 */
64 for(i=1;i<rval;i++){
65 if(i > buflen) /* destination buffer overflow */
66 break;
67 buf[i-1] = buffer[2 * i];
68 if(buffer[2 * i + 1] != 0) /* outside of ISO Latin1 range */
69 buf[i-1] = '?';
70 }
71 buf[i-1] = 0;
72 return i-1;
73}
74
75
76/* PowerSwitch uses the free shared default VID/PID. If you want to see an
77 * example device lookup where an individually reserved PID is used, see our
78 * RemoteSensor reference implementation.
79 */
80
81#define USB_ERROR_NOTFOUND 1
82#define USB_ERROR_ACCESS 2
83#define USB_ERROR_IO 3
84
85static int usbOpenDevice(usb_dev_handle **device, int vendor, char *vendorName, int product, char *productName)
86{
87struct usb_bus *bus;
88struct usb_device *dev;
89usb_dev_handle *handle = NULL;
90int errorCode = USB_ERROR_NOTFOUND;
91static int didUsbInit = 0;
92
93 if(!didUsbInit){
94 didUsbInit = 1;
95 usb_init();
96 }
97 usb_find_busses();
98 usb_find_devices();
99 for(bus=usb_get_busses(); bus; bus=bus->next){
100 for(dev=bus->devices; dev; dev=dev->next){
101 if(dev->descriptor.idVendor == vendor && dev->descriptor.idProduct == product){
102 char string[256];
103 int len;
104 handle = usb_open(dev); /* we need to open the device in order to query strings */
105 if(!handle){
106 errorCode = USB_ERROR_ACCESS;
107 fprintf(stderr, "Warning: cannot open USB device: %s\n", usb_strerror());
108 continue;
109 }
110 if(vendorName == NULL && productName == NULL){ /* name does not matter */
111 break;
112 }
113 /* now check whether the names match: */
114 len = usbGetStringAscii(handle, dev->descriptor.iManufacturer, 0x0409, string, sizeof(string));
115 if(len < 0){
116 errorCode = USB_ERROR_IO;
117 fprintf(stderr, "Warning: cannot query manufacturer for device: %s\n", usb_strerror());
118 }else{
119 errorCode = USB_ERROR_NOTFOUND;
120 /* fprintf(stderr, "seen device from vendor ->%s<-\n", string); */
121 if(strcmp(string, vendorName) == 0){
122 len = usbGetStringAscii(handle, dev->descriptor.iProduct, 0x0409, string, sizeof(string));
123 if(len < 0){
124 errorCode = USB_ERROR_IO;
125 fprintf(stderr, "Warning: cannot query product for device: %s\n", usb_strerror());
126 }else{
127 errorCode = USB_ERROR_NOTFOUND;
128 /* fprintf(stderr, "seen product ->%s<-\n", string); */
129 if(strcmp(string, productName) == 0)
130 break;
131 }
132 }
133 }
134 usb_close(handle);
135 handle = NULL;
136 }
137 }
138 if(handle)
139 break;
140 }
141 if(handle != NULL){
142 errorCode = 0;
143 *device = handle;
144 }
145 return errorCode;
146}
147
148void hexdump(unsigned char* bytes, int len)
149{
150 for(int i = 0; i <len; i++)
151 printf("%X ",bytes[i]);
152 puts("");
153}
154
155int main(int argc, char **argv)
156{
157usb_dev_handle *handle = NULL;
158unsigned char buffer[275];
159int nBytes;
160
161 if(argc < 2){
162 usage(argv[0]);
163 exit(1);
164 }
165 usb_init();
166 if(usbOpenDevice(&handle, USBDEV_SHARED_VENDOR, VENDORSTRING, USBDEV_SHARED_PRODUCT, PRODUCTSTRING) != 0){
167 fprintf(stderr, "Could not find USB device \""PRODUCTSTRING"\" with vid=0x%x pid=0x%x\n", USBDEV_SHARED_VENDOR, USBDEV_SHARED_PRODUCT);
168 exit(1);
169 }
170/* We have searched all devices on all busses for our USB device above. Now
171 * try to open it and perform the vendor specific control operations for the
172 * function requested by the user.
173 */
174 if(strcmp(argv[1], "get") == 0){
175 memset(buffer, 0, 275);
176 nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, PSCMD_GET, 0,0, (char*)buffer, 200, 5000);
177 hexdump(buffer, sizeof(buffer));
178 }else if(strcmp(argv[1], "put") == 0){
179
180 FILE* fptr = fopen(argv[2], "rb");
181 int blockid;
182 uint8_t blktype, blksize;
183 sscanf(argv[3], "%d", &blockid);
184
185 do
186 {
187 fread(buffer, 1, 18, fptr); // skip sync header
188 fread(&blktype, 1, 1, fptr);
189 fread(&blksize, 1, 1, fptr);
190 blksize -= 2;
191 fread(buffer, 1, blksize + 1, fptr);
192 }
193 while (blockid --);
194
195 fclose(fptr);
196
197 hexdump(buffer, blksize);
198 int rqtype = (blksize == 0) ? USB_ENDPOINT_IN:USB_ENDPOINT_OUT;
199 nBytes = usb_control_msg(handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | rqtype, PSCMD_PUT, blktype,0 /*checksum*/, (char*)buffer, blksize, 5000);
200 }else{
201 fprintf(stderr,"Unknown command: %s.\n", argv[1]);
202 usage(argv[0]);
203 nBytes = 0;
204 // TODO manage return code
205 }
206
207 if (nBytes < 0) fprintf(stderr, "USB error %s\n", usb_strerror());
208
209 usb_close(handle);
210 return 0;
211}
212
Note: See TracBrowser for help on using the repository browser.