]> err.no Git - yubiyubo/commitdiff
Refactor bits of yubiyubo.c into ykusb.[ch] master
authorTollef Fog Heen <tfheen@err.no>
Tue, 20 Jan 2009 01:40:09 +0000 (02:40 +0100)
committerTollef Fog Heen <tfheen@err.no>
Tue, 20 Jan 2009 01:40:09 +0000 (02:40 +0100)
src/ykusb.c [new file with mode: 0644]
src/ykusb.h [new file with mode: 0644]
src/yubiyubo.c

diff --git a/src/ykusb.c b/src/ykusb.c
new file mode 100644 (file)
index 0000000..2867f99
--- /dev/null
@@ -0,0 +1,91 @@
+/*
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you
+ * may not use this file except in compliance with the License.  You
+ * may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.  See the License for the specific language governing
+ * permissions and limitations under the License.
+ *
+ */
+
+#include <stdbool.h>
+#include "ykusb.h"
+
+#define CYPAK_VID 0x1050
+#define YK_PID 0x0010
+#define FEATURE_RPT_SIZE 9
+
+HIDInterface *yk_open()
+{
+       HIDInterface* hid;
+       HIDInterfaceMatcher matcher = { CYPAK_VID, YK_PID, NULL, NULL, 0 };
+       hid_return ret;
+
+       ret = hid_init();
+       if (ret != HID_RET_SUCCESS) {
+               fprintf(stderr, "hid_init failed with return code %d\n", ret);
+               return NULL;
+       }
+
+       hid = hid_new_HIDInterface();
+       if (hid == NULL) {
+               fprintf(stderr, "hid_new_HIDInterface() failed, out of memory?\n");
+               return NULL;
+       }
+
+       ret = hid_force_open(hid, 0, &matcher, 3);
+       if (ret != HID_RET_SUCCESS) {
+               fprintf(stderr, "hid_force_open failed with return code %d\n", ret);
+               return NULL;
+       }
+
+}
+void yk_close(HIDInterface *hid)
+{
+       hid_return ret;
+
+       ret = hid_close(hid);
+       if (ret != HID_RET_SUCCESS) {
+               fprintf(stderr, "hid_close failed with return code %d\n", ret);
+               exit(1);
+       }
+
+       hid_delete_HIDInterface(&hid);
+
+       ret = hid_cleanup();
+       if (ret != HID_RET_SUCCESS) {
+               fprintf(stderr, "hid_cleanup failed with return code %d\n", ret);
+               exit(1);
+       }
+}
+
+bool yk_send(HIDInterface *hid, char slot, char *data, size_t length)
+{
+
+       char buf[FEATURE_RPT_SIZE], sbuf[FEATURE_RPT_SIZE],
+               usbdata[(FEATURE_RPT_SIZE + 1 * 10)];
+       int i, j;
+       unsigned short crc;
+
+       if (length > 64) {
+               return false;
+       }
+
+       memset(usbdata, 0, sizeof(usbdata));
+       memcpy(usbdata, data, length);
+       crc = crc_get_full(usbdata, 64, false);
+
+       data[64] = slot;
+       data[65] = (crc & 0xff);
+       data[66] = (crc >> 8);
+
+
+}
+
+bool yk_get_status(HIDInterface *hid, STATUS *status, bool force_update);
diff --git a/src/ykusb.h b/src/ykusb.h
new file mode 100644 (file)
index 0000000..20adc1a
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you
+ * may not use this file except in compliance with the License.  You
+ * may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ * implied.  See the License for the specific language governing
+ * permissions and limitations under the License.
+ *
+ */
+
+#ifndef _YKUSB_H
+#define _YKUSB_H
+
+#include <hid.h>
+
+typedef enum { yrFAIL, yrOPENED, yrMORE_THAN_ONE } ykRC;
+
+/* we need some kind of a handle?  FD? */
+
+HIDInterface *yk_open();
+void yk_close(HIDInterface *hid);
+bool yk_send(HIDInterface *hid, char slot, char *data, size_t length);
+bool yk_get_status(HIDInterface *hid, STATUS *status, bool force_update);
+
+#endif /* _YKUSB_H */
index d2cb63fcbd5c7d41e5162698e83119025d823890..5cc71efd4240a35ba5b7ac41231fd9b793548b8f 100644 (file)
 int main(int argc, char **argv)
 {
        HIDInterface* hid;
-       hid_return ret;
        char buf[128];
        int path[2] = {0x00010006, 0xff000020};
        int i;
        bool reset = false;
        HIDInterfaceMatcher matcher = { 0x1050, 0x0010, NULL, NULL, 0 };
 
-       ret = hid_init();
-       if (ret != HID_RET_SUCCESS) {
-               fprintf(stderr, "hid_init failed with return code %d\n", ret);
-               exit(1);
-       }
-
-       hid = hid_new_HIDInterface();
+       hid = yk_open();
        if (hid == NULL) {
-               fprintf(stderr, "hid_new_HIDInterface() failed, out of memory?\n");
                exit(1);
        }
 
-       ret = hid_force_open(hid, 0, &matcher, 3);
-       if (ret != HID_RET_SUCCESS) {
-               fprintf(stderr, "hid_force_open failed with return code %d\n", ret);
-               exit(1);
-       }
-
-       /* Do stuff with hid here */
-
-       ret = hid_close(hid);
-       if (ret != HID_RET_SUCCESS) {
-               fprintf(stderr, "hid_close failed with return code %d\n", ret);
-               exit(1);
-       }
-
-       hid_delete_HIDInterface(&hid);
-
-       ret = hid_cleanup();
-       if (ret != HID_RET_SUCCESS) {
-               fprintf(stderr, "hid_cleanup failed with return code %d\n", ret);
-               exit(1);
-       }
 
        return 0;
 }