diff rcctl.c @ 2:b16b82dbfe87

massive cleanup
author Yoshiki Yazawa <yaz@honeyplanet.jp>
date Wed, 05 Jan 2011 21:41:39 +0900
parents 05cc06e88a57
children cd846ca24ca4
line wrap: on
line diff
--- a/rcctl.c	Fri Dec 24 15:28:04 2010 +0900
+++ b/rcctl.c	Wed Jan 05 21:41:39 2011 +0900
@@ -1,24 +1,25 @@
 /* rcctl.c ---------------------------------------------
    $Id: rcctl.c,v 1.1 2002/12/21 01:13:28 tosy Exp $
 
-   v0.10  97.08.15  初期版(コミケット52)
-   v0.11  97.08.27  通信タイミング修正
-   v0.12  97.09.12  U-kara-2サポート
-   v0.12a 97.09.15  返り値設定
+   v0.10  97.08.15  (潟宴52)
+   v0.11  97.08.27  篆<帥ゃ潟遺信罩
+   v0.12  97.09.12  U-kara-2泣若
+   v0.12a 97.09.15  菴よ┃絎
    v0.12b 97.09.20  Bug fix (JOY:[SP])
-   v0.20  97.10.01  FreeBSD版
+   v0.20  97.10.01  FreeBSD
    v0.20a 97.10.18  Bug fix (X2k:[SP],[ST])
-   v0.21  97.12.10  ALISA-3シーケンス修正
-   v0.30  97.12.13  コード変換部(cdcnv.c)分離
-   v0.40  02.12.16  USB版対応
-   【cdcnv.c の履歴も参照のこと】
+   v0.21  97.12.10  ALISA-3激若宴潟剛信罩
+   v0.30  97.12.13  潟若紊(cdcnv.c)
+   v0.40  02.12.16  USB絲上
+   v0.50  11.01.06  linux絨吾眼 (yaz)
+   cdcnv.c 絮ユ眼с
    ------------------------------------------------------*/
 #define __LINUX__
 #include <fcntl.h>
 #include <stdio.h>
-#include <termios.h>
-#include <unistd.h>
-#include <sys/ioctl.h>
+//#include <termios.h>
+//#include <unistd.h>
+//#include <sys/ioctl.h>
 
 #define HID_MAX_USAGES 16
 typedef unsigned int __u32;
@@ -35,14 +36,16 @@
 #include <errno.h>
 #include <string.h>
 
+#include "debug.h"
 
 #ifndef S_VERS
-#define S_VERS "0.41"
+#define S_VERS "0.50"
 #endif
 
 /* globals */
 HIDInterface *hidif;
 extern int errno;
+int debug = 0;
 
 /* prototypes */
 int cdcnv(int buf[], char *mak, char *cod);
@@ -52,15 +55,11 @@
 {
     hid_return ret;
 
-//    unsigned int report_id;
-//    unsigned int report_type;
-//    unsigned int size;
-
     /* vendor id and product id of okecon */
     HIDInterfaceMatcher matcher = { 0x0bfe, 0x2022, NULL, NULL, 0 };
 
     /* see include/debug.h for possible values */
-    hid_set_debug(HID_DEBUG_ALL);
+    hid_set_debug(debug);
     hid_set_debug_stream(stderr);
     /* passed directly to libusb */
     hid_set_usb_debug(0);
@@ -83,51 +82,6 @@
         return 1;
     }
 
-
-
-#if 0
-    /* read feature */
-    ret = hid_get_feature_report(hidif, path, depth, buffer, size);
-    if(ret != HID_RET_SUCCESS) {
-        fprintf(stderr, "hid_get_feature_report failed with return code %d\n", ret);
-        return 1;
-    }
-
-    /* parse */
-
-    /* prepare buffers */
-    hid_get_report_size(hidif, report_id, report_type, &size);
-    if(size != 8) {
-        fprintf(stderr, "%s is not 'OKCon/USB'?\n", UDEV);
-        return 1;
-    };
-
-
-// BSD
-    /* read header */
-    if ((rd = hid_get_report_desc(fd)) == 0) {
-        fprintf(stderr, "Failed on USB_GET_REPORT_DESC.\n");
-        return 1;
-    }
-
-    /* parse */
-    hd = hid_start_parse(rd, 1<<hid_output);
-    while ( hid_get_item(hd, &shi) ) {
-        if(shi.kind == hid_output)
-            break;
-    }
-    hid_end_parse(hd);
-
-    /* prepare buffers */
-    hid_get_report_size(&hidif, report_id, report_type, &size);
-    if(size != 8) {
-        fprintf(stderr, "%s is not 'OKCon/USB'?\n", UDEV);
-        return 1;
-    };
-
-    hid_dispose_report_desc(rd);
-#endif
-
     return 0;
 } /* end of init_usb() */
 
@@ -153,74 +107,74 @@
     return 0;
 }
 
+void usage(char *cmd)
+{
+    printf("'Oke-Con' controller, version " S_VERS ".\n");
+    printf("Copyright (C) 1997-2002 by Tosy / W341IG.\n");
+    printf("Adapted to linux by yaz / honeyplanet development\n");
+    printf("usage: %s <options> <vender> <song number>\n", cmd);
+    printf("options: -d <debug level>\n");
+    exit(0);
+}
 
 
-int main(int ac, char *av[])
+int main(int argc, char **argv)
 {
     int i, u, buf[16];
     unsigned char sbuf[8];
     hid_return ret;
+    int opt;
 
-    if (ac < 3) {
-        printf("'Oke-Con' controller, version " S_VERS ".\n");
-        printf("Copyright (C) 1997-2002 by Tosy / W341IG.\n");
-        printf("Usage: rcctl <maker> <code>\n");
-        return 255;
+    if(argc < 3) {
+        usage(argv[0]);
     }
 
-    if ((u = cdcnv(buf, av[1], av[2])) < 0) {
-        fprintf(stderr, "%s: %s\n", av[0], cverrstr[~u]);
+    while ((opt=getopt(argc, argv, "d:h")) != -1) {
+        switch (opt) {
+        case 'd':
+            debug = atoi(optarg);
+            break;
+        case 'h':
+        default:
+            usage(argv[0]);
+            break;
+        }
+    }
+    argc -= optind;
+    argv += optind;
+
+    if ((u = cdcnv(buf, *argv, *(argv+1))) < 0) {
+        fprintf(stderr, "%s: %s\n", *argv, cverrstr[~u]);
         return 1;
     }
-    printf("cdcnv %d\n", u);
-
-#ifdef VERBOSE
-    printf("Initializing....\n");
-#endif
+    debug(1, "cdcnv %d\n", u);
+    debug(1, "Initializing....\n");
 
     if (init_usb())
         return 255;
 
-#ifdef VERBOSE
-    printf(" done.\n");
-#endif
+    debug(1, " done.\n");
 
-#ifdef VERBOSE
     for(i=0; i<u; i++ ) {
-        printf("%02x ", buf[i]);
+        debug(1, "%02x ", buf[i]);
     }
-    printf("\n");
-#endif
+    debug(1, "\n");
 
-    /* xxx */
 	const int PATH_IN[1] = { 0xffa10004 };
-//    const int PATH_OUT[1] = { 0xffa10008 };
 
     /* u is the total length of the packet to send */
     for(i=1; i<u; i++ ) {
         sbuf[(i-1)%8] = buf[i] & 0x00ff;
-#ifdef VERBOSE
-        printf("%02x ", sbuf[(i-1)%8]);
-#endif
+        debug(1, "%02x ", sbuf[(i-1)%8]);
         if ((i == 8)||(i == (u-1))) {
             ret = hid_set_output_report(hidif, PATH_IN, 2, (char *)sbuf, 8);
             fprintf(stderr, "**WRITE**\n");
             if (ret != HID_RET_SUCCESS) {
                 fprintf(stderr, "hid_set_output_report failed with return code %d\n", ret);
             }
-
-#if 0
-            char packet[8];
-            ret = hid_get_input_report(hidif, PATH_OUT, 2, packet, 8);
-            if (ret != HID_RET_SUCCESS) {
-                fprintf(stderr, "hid_get_input_report failed with return code %d\n", ret);
-            }
-#endif
         }
     }
-#ifdef VERBOSE
-    printf("\n");
-#endif
+    debug(1, "\n");
 
     if(fin_usb())
         return 255;