|
0
|
1 /* rcctl.c ---------------------------------------------
|
|
|
2 $Id: rcctl.c,v 1.1 2002/12/21 01:13:28 tosy Exp $
|
|
|
3
|
|
|
4 v0.10 97.08.15 初期版(コミケット52)
|
|
|
5 v0.11 97.08.27 通信タイミング修正
|
|
|
6 v0.12 97.09.12 U-kara-2サポート
|
|
|
7 v0.12a 97.09.15 返り値設定
|
|
|
8 v0.12b 97.09.20 Bug fix (JOY:[SP])
|
|
|
9 v0.20 97.10.01 FreeBSD版
|
|
|
10 v0.20a 97.10.18 Bug fix (X2k:[SP],[ST])
|
|
|
11 v0.21 97.12.10 ALISA-3シーケンス修正
|
|
|
12 v0.30 97.12.13 コード変換部(cdcnv.c)分離
|
|
|
13 v0.40 02.12.16 USB版対応
|
|
|
14 【cdcnv.c の履歴も参照のこと】
|
|
|
15 ------------------------------------------------------*/
|
|
|
16
|
|
|
17 #include <fcntl.h>
|
|
|
18 #include <stdio.h>
|
|
|
19 #include <termios.h>
|
|
|
20 #include <unistd.h>
|
|
|
21 #include <libusb.h>
|
|
|
22 #include <sys/ioctl.h>
|
|
|
23 #include <dev/usb/usb.h>
|
|
|
24 #include <dev/usb/usbhid.h>
|
|
|
25
|
|
|
26 /* #define VERBOSE */
|
|
|
27 /* #define NOCTSCHK */
|
|
|
28
|
|
|
29 #ifndef SDEV
|
|
|
30 #define SDEV "/dev/cuaa0"
|
|
|
31 #endif
|
|
|
32
|
|
|
33 #ifndef UDEV
|
|
|
34 #define UDEV "/dev/uhid0"
|
|
|
35 #endif
|
|
|
36
|
|
|
37 #ifndef S_VERS
|
|
|
38 #define S_VERS "0.40"
|
|
|
39 #endif
|
|
|
40
|
|
|
41 int fd;
|
|
|
42
|
|
|
43 int cdcnv(int buf[], char *mak, char *cod);
|
|
|
44 extern char *cverrstr[];
|
|
|
45
|
|
|
46 int init_sio(void)
|
|
|
47 {
|
|
|
48 #ifndef NOCTSCHK
|
|
|
49 int tcnt = 0;
|
|
|
50 #endif
|
|
|
51 struct termios tios;
|
|
|
52 int md;
|
|
|
53
|
|
|
54 if ((fd = open(SDEV, O_RDWR)) == -1) {
|
|
|
55 fprintf(stderr, "Cannot open %s.\n", SDEV);
|
|
|
56 return 255;
|
|
|
57 }
|
|
|
58
|
|
|
59 tcgetattr(fd, &tios);
|
|
|
60 tios.c_iflag = 0;
|
|
|
61 tios.c_oflag = 0;
|
|
|
62 tios.c_cflag = B9600|CS8|CLOCAL;
|
|
|
63 tios.c_lflag = 0;
|
|
|
64 /*
|
|
|
65 cfmakeraw(&tios);
|
|
|
66 cfsetspeed(&tios, B9600);
|
|
|
67 */
|
|
|
68 tcsetattr(fd, TCSANOW, &tios);
|
|
|
69 tcflush(fd, TCOFLUSH);
|
|
|
70
|
|
|
71 md = TIOCM_LE|TIOCM_DTR|TIOCM_RTS;
|
|
|
72 ioctl(fd, TIOCMBIS, &md);
|
|
|
73
|
|
|
74 /* wait for CTS: 充電待ち */
|
|
|
75 #ifndef NOCTSCHK
|
|
|
76 for(;;) {
|
|
|
77 ioctl(fd, TIOCMGET, &md);
|
|
|
78 if (md & TIOCM_CTS)
|
|
|
79 break;
|
|
|
80 if (tcnt == 0) {
|
|
|
81 fprintf(stderr, "Waiting....");
|
|
|
82 }
|
|
|
83 if (tcnt++ >= 30) {
|
|
|
84 fprintf(stderr, "Device timeout.\n");
|
|
|
85 close(fd);
|
|
|
86 return 1;
|
|
|
87 }
|
|
|
88 sleep(1);
|
|
|
89 }
|
|
|
90 #endif /* NOCTSCHK */
|
|
|
91
|
|
|
92 md = TIOCM_RTS;
|
|
|
93 ioctl(fd, TIOCMBIC, &md);
|
|
|
94 usleep(10000); /* 10ms */
|
|
|
95 ioctl(fd, TIOCMBIS, &md);
|
|
|
96 usleep(50000); /* 50ms */
|
|
|
97
|
|
|
98 return 0;
|
|
|
99 }
|
|
|
100
|
|
|
101 /* CTS が 0.5 秒以上連続して ON になるまで待つ */
|
|
|
102 void charge(void)
|
|
|
103 {
|
|
|
104 #ifndef NOCTSCHK
|
|
|
105 int i, md;
|
|
|
106
|
|
|
107 for( i=0; i<50; i++ ) {
|
|
|
108 usleep(10000); /* 10ms */
|
|
|
109 ioctl(fd, TIOCMGET, &md);
|
|
|
110 if (!(md & TIOCM_CTS))
|
|
|
111 i = 0;
|
|
|
112 }
|
|
|
113 #endif /* NOCTSCHK */
|
|
|
114 }
|
|
|
115
|
|
|
116 int init_usb(void)
|
|
|
117 {
|
|
|
118 report_desc_t rd;
|
|
|
119 hid_data_t hd;
|
|
|
120 hid_item_t shi;
|
|
|
121
|
|
|
122 hid_init(NULL);
|
|
|
123
|
|
|
124 if((fd = open(UDEV, O_RDWR)) < 0) {
|
|
|
125 fprintf(stderr, "Cannot open %s.\n", UDEV);
|
|
|
126 return 1;
|
|
|
127 }
|
|
|
128
|
|
|
129 /* read header */
|
|
|
130 if ((rd = hid_get_report_desc(fd)) == 0) {
|
|
|
131 fprintf(stderr, "Failed on USB_GET_REPORT_DESC.\n");
|
|
|
132 return 1;
|
|
|
133 }
|
|
|
134
|
|
|
135 /* parse */
|
|
|
136 hd = hid_start_parse(rd, 1<<hid_output);
|
|
|
137 while ( hid_get_item(hd, &shi) ) {
|
|
|
138 if(shi.kind == hid_output)
|
|
|
139 break;
|
|
|
140 }
|
|
|
141 hid_end_parse(hd);
|
|
|
142
|
|
|
143 /* prepare buffers */
|
|
|
144 if (hid_report_size(rd, hid_output, 0) != 8) {
|
|
|
145 fprintf(stderr, "%s is not 'OKCon/USB'?\n", UDEV);
|
|
|
146 return 1;
|
|
|
147 };
|
|
|
148
|
|
|
149 hid_dispose_report_desc(rd);
|
|
|
150
|
|
|
151 return 0;
|
|
|
152 }
|
|
|
153
|
|
|
154 int main(int ac, char *av[])
|
|
|
155 {
|
|
|
156 int i, u, buf[16];
|
|
|
157 u_char sbuf[8];
|
|
|
158
|
|
|
159 if (ac < 3) {
|
|
|
160 printf("'Oke-Con' controller, version " S_VERS ".\n");
|
|
|
161 printf("Copyright (C) 1997-2002 by Tosy / W341IG.\n");
|
|
|
162 printf("Usage: rcctl <maker> <code>\n");
|
|
|
163 return 255;
|
|
|
164 }
|
|
|
165
|
|
|
166 if ((u = cdcnv(buf, av[1], av[2])) < 0) {
|
|
|
167 fprintf(stderr, "%s: %s\n", av[0], cverrstr[~u]);
|
|
|
168 return 1;
|
|
|
169 }
|
|
|
170
|
|
|
171 #ifdef VERBOSE
|
|
|
172 printf("Initializing....");
|
|
|
173 #endif
|
|
|
174 if (init_usb())
|
|
|
175 /* if (init_sio()) */
|
|
|
176 return 255;
|
|
|
177 #ifdef VERBOSE
|
|
|
178 printf(" done.\n");
|
|
|
179 #endif
|
|
|
180
|
|
|
181 #ifdef VERBOSE
|
|
|
182 /* printf("maker %d (%c).\n", maker, mks[maker]); */
|
|
|
183 for(i=0; i<u; i++ ) {
|
|
|
184 printf("%02x ", buf[i]);
|
|
|
185 }
|
|
|
186 printf("\n");
|
|
|
187 #endif
|
|
|
188
|
|
|
189 for(i=1; i<u; i++ ) {
|
|
|
190 sbuf[(i-1)%8] = buf[i] & 0x00ff;
|
|
|
191 #ifdef VERBOSE
|
|
|
192 printf("%02x ", sbuf[(i-1)%8]);
|
|
|
193 #endif
|
|
|
194 if ((i == 8)||(i == (u-1))) {
|
|
|
195 write(fd, sbuf, 8);
|
|
|
196 #ifdef VERBOSE
|
|
|
197 printf(" --- got %d chars.\n", read(fd, sbuf, 8));
|
|
|
198 #else
|
|
|
199 read(fd, sbuf, 8);
|
|
|
200 #endif
|
|
|
201 }
|
|
|
202 }
|
|
|
203
|
|
|
204 /* charge(); */
|
|
|
205 close(fd);
|
|
|
206 return 0;
|
|
|
207 }
|