From: Tollef Fog Heen Date: Tue, 20 Jan 2009 01:40:09 +0000 (+0100) Subject: Refactor bits of yubiyubo.c into ykusb.[ch] X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e3c9da061f6ea2219a1f59022508d1a46d48c400;p=yubiyubo Refactor bits of yubiyubo.c into ykusb.[ch] --- diff --git a/src/ykusb.c b/src/ykusb.c new file mode 100644 index 0000000..2867f99 --- /dev/null +++ b/src/ykusb.c @@ -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 +#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 index 0000000..20adc1a --- /dev/null +++ b/src/ykusb.h @@ -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 + +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 */ diff --git a/src/yubiyubo.c b/src/yubiyubo.c index d2cb63f..5cc71ef 100644 --- a/src/yubiyubo.c +++ b/src/yubiyubo.c @@ -24,46 +24,17 @@ 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; }