rawhid 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3cd8505e05ba9a641cb9add30f6e5a9cde48421a
4
- data.tar.gz: 49f8c62b7e333a9bbf9e3246bad8c38362747639
3
+ metadata.gz: 764040e72e30f56fced1bb0e997f448d2885db63
4
+ data.tar.gz: 956c32657616d57bf0e8cd8ee1434328f41579ea
5
5
  SHA512:
6
- metadata.gz: 0563e92f96e93be0dfd1fb9dcf22b1c701c1c26bd1a2d449fdab6116fbf5ffcb59754d4dbed3c84f17dd755dacec4507fe6b6a1c8bce2e451dc86d3fc9bd727d
7
- data.tar.gz: c4653cb780fe023bcc02cdaf4cfaf86040cbcae5fdbd6d6a4a6c7feddc26b6a579e8cb5b28f8e4e233ffc2490c2d05bc129e8eec3ac9116441deb27973a9ee6c
6
+ metadata.gz: 91da84ed6adbef767cac7d9d0fb37e480694da20c71d0bc40b4509f5e46e8389a245f0048946521b8e23a65ebbe5cd6a07ccf21e6ecd99c8b0861bbe88cb9b17
7
+ data.tar.gz: db084b399cfbc5dc884f651279bf786b2a006f8ec6673b9c3fdc2c12bff8d21c0b4f0f602e9588c4f09b0b3e23ffdad0ae5d3d5dbfa27822a22b11cecd8f2649
@@ -8,12 +8,15 @@ $srcs = ["#{extension_name}.c"]
8
8
  case Gem::Platform.local.os
9
9
  when "linux"
10
10
  have_library "usb", nil, ["usb.h"]
11
- $defs.push("-DOS_LINUX")
11
+ $defs.push "-DOS_LINUX"
12
12
  $srcs << "hid_LINUX.c"
13
+ #when "darwin"
14
+ # raise "can't build for
15
+ # have_library "hid", nil, ["hid.h"]
16
+ # $defs.push "-DOS_MACOSX"
13
17
  else
14
- exit 1
18
+ raise "Don't know ho to build rawhid on #{Gem::Platform.local.os} yet."
15
19
  end
16
20
 
17
-
18
21
  create_header
19
22
  create_makefile "#{extension_name}"
@@ -1,6 +1,9 @@
1
+ #include "hid_LINUX.h"
1
2
 
2
- int rawhid_open(int max, void **devs, int vid, int pid, int usage_page, int usage);
3
- int rawhid_recv(void *dev, void *buf, int len, int timeout);
4
- int rawhid_send(void *dev, void *buf, int len, int timeout);
5
- void rawhid_close(void *dev);
3
+ typedef struct hid_struct hid_t;
4
+
5
+ int rawhid_open(int max, hid_t **devs, int vid, int pid, int usage_page, int usage);
6
+ int rawhid_recv(hid_t *hid, void *buf, int len, int timeout);
7
+ int rawhid_send(hid_t *hid, void *buf, int len, int timeout);
8
+ int rawhid_close(hid_t *hid);
6
9
 
@@ -81,38 +81,23 @@
81
81
 
82
82
  #define printf(...) // comment this out for lots of info
83
83
 
84
-
85
- // a list of all opened HID devices, so the caller can
86
- // simply refer to them by number
87
- typedef struct hid_struct hid_t;
88
- struct hid_struct {
89
- usb_dev_handle *usb;
90
- int open;
91
- int iface;
92
- int ep_in;
93
- int ep_out;
94
- struct hid_struct *prev;
95
- struct hid_struct *next;
96
- };
97
-
98
-
99
84
  // private functions, not intended to be used from outside this file
100
85
  static void hid_close(hid_t *hid);
101
86
  static int hid_parse_item(uint32_t *val, uint8_t **data, const uint8_t *end);
102
87
 
88
+ static volatile int num_open = 0;
103
89
 
104
90
  // rawhid_recv - receive a packet
105
91
  // Inputs:
106
- // dev = pointer to device to receive from
92
+ // rawhid = pointer to device to receive from
107
93
  // buf = buffer to receive packet
108
94
  // len = buffer's size
109
95
  // timeout = time to wait, in milliseconds
110
96
  // Output:
111
97
  // number of bytes received, or -1 on error
112
98
  //
113
- int rawhid_recv(void *dev, void *buf, int len, int timeout)
99
+ int rawhid_recv(hid_t *hid, void *buf, int len, int timeout)
114
100
  {
115
- hid_t *hid = dev;
116
101
  int r;
117
102
 
118
103
  if (!hid || !hid->open) return -1;
@@ -131,10 +116,8 @@ int rawhid_recv(void *dev, void *buf, int len, int timeout)
131
116
  // Output:
132
117
  // number of bytes sent, or -1 on error
133
118
  //
134
- int rawhid_send(void *dev, void *buf, int len, int timeout)
119
+ int rawhid_send(hid_t *hid, void *buf, int len, int timeout)
135
120
  {
136
- hid_t *hid = dev;
137
-
138
121
  if (!hid || !hid->open) return -1;
139
122
  if (hid->ep_out) {
140
123
  return usb_interrupt_write(hid->usb, hid->ep_out, buf, len, timeout);
@@ -147,7 +130,7 @@ int rawhid_send(void *dev, void *buf, int len, int timeout)
147
130
  //
148
131
  // Inputs:
149
132
  // max = maximum number of devices to open
150
- // dev = pointer to array of pointers to devices (at least of size 'max')
133
+ // rawhids = pointer to array of preallocated rawhid structs
151
134
  // vid = Vendor ID, or -1 if any
152
135
  // pid = Product ID, or -1 if any
153
136
  // usage_page = top level usage page, or -1 if any
@@ -155,8 +138,8 @@ int rawhid_send(void *dev, void *buf, int len, int timeout)
155
138
  // Output:
156
139
  // actual number of devices opened
157
140
  //
158
- int rawhid_open(int max, void **devs, int vid, int pid, int usage_page, int usage)
159
- {
141
+ int rawhid_open(int max, hid_t **hids, int vid, int pid, int usage_page, int usage)
142
+ {
160
143
  struct usb_bus *bus;
161
144
  struct usb_device *dev;
162
145
  struct usb_interface *iface;
@@ -166,7 +149,7 @@ int rawhid_open(int max, void **devs, int vid, int pid, int usage_page, int usag
166
149
  uint8_t buf[1024], *p;
167
150
  int i, n, len, tag, ep_in, ep_out, count=0, claimed;
168
151
  uint32_t val=0, parsed_usage, parsed_usage_page;
169
- hid_t *hid;
152
+ hid_t *hid = *hids;
170
153
 
171
154
  printf("rawhid_open, max=%d\n", max);
172
155
  if (max < 1) return 0;
@@ -245,7 +228,6 @@ int rawhid_open(int max, void **devs, int vid, int pid, int usage_page, int usag
245
228
  usb_release_interface(u, i);
246
229
  continue;
247
230
  }
248
- hid = (struct hid_struct *)malloc(sizeof(struct hid_struct));
249
231
  if (!hid) {
250
232
  usb_release_interface(u, i);
251
233
  continue;
@@ -256,8 +238,7 @@ int rawhid_open(int max, void **devs, int vid, int pid, int usage_page, int usag
256
238
  hid->ep_out = ep_out;
257
239
  hid->open = 1;
258
240
 
259
- *devs = hid;
260
- devs++;
241
+ hid++;
261
242
 
262
243
  claimed++;
263
244
  count++;
@@ -273,16 +254,15 @@ int rawhid_open(int max, void **devs, int vid, int pid, int usage_page, int usag
273
254
  // rawhid_close - close a device, free associated memory
274
255
  //
275
256
  // Inputs:
276
- // dev = pointer to device to close
257
+ // rawhid = pointer to device to close
277
258
  // Output
278
- // (nothing)
259
+ // 1 if device was successfully closed, 0 if it isn't open
279
260
  //
280
- void rawhid_close(void *dev)
261
+ int rawhid_close(hid_t *hid)
281
262
  {
282
- hid_t *hid = dev;
283
-
284
- if (!hid || !hid->open) return;
285
- free(dev);
263
+ if (!hid || !hid->open) return 0;
264
+ hid_close(hid);
265
+ return 1;
286
266
  }
287
267
 
288
268
  // Chuck Robey wrote a real HID report parser
@@ -324,12 +304,10 @@ static int hid_parse_item(uint32_t *val, uint8_t **data, const uint8_t *end)
324
304
 
325
305
  static void hid_close(hid_t *hid)
326
306
  {
327
- hid_t *p;
328
- int others=0;
329
-
330
307
  usb_release_interface(hid->usb, hid->iface);
331
308
  usb_close(hid->usb);
332
309
  hid->usb = NULL;
310
+ hid->open = 0;
333
311
  }
334
312
 
335
313
 
@@ -0,0 +1,10 @@
1
+ #include <usb.h>
2
+ struct hid_struct {
3
+ usb_dev_handle *usb;
4
+ int open;
5
+ int iface;
6
+ int ep_in;
7
+ int ep_out;
8
+ struct hid_struct *prev;
9
+ struct hid_struct *next;
10
+ };
@@ -5,28 +5,43 @@
5
5
  #include "hid.h"
6
6
 
7
7
  #define GET_RAWHID(name, val) \
8
- void *name; \
8
+ void **name; \
9
9
  Data_Get_Struct(val, void, name)
10
10
 
11
11
  VALUE RawHID;
12
12
  VALUE RawHIDError;
13
13
 
14
14
  VALUE RawHID_new(VALUE class, VALUE vendorId, VALUE productId) {
15
- void *dev = NULL;
15
+ hid_t *hid = NULL;
16
16
  int vId = NUM2INT(vendorId);
17
17
  int pId = NUM2INT(productId);
18
- int devices_opened = rawhid_open(1, &dev, vId, pId, -1, -1);
19
- if(devices_opened == 0 || dev == NULL) {
20
- rb_raise(RawHIDError, "Unable to open device");
18
+
19
+ VALUE self = Data_Make_Struct(class, hid_t, NULL, rawhid_close, hid);
20
+ if(hid == NULL) {
21
+ rb_raise(RawHIDError, "Out of memory");
21
22
  }
23
+ rb_iv_set(self, "@vendorId", vendorId);
24
+ rb_iv_set(self, "@productId", productId);
22
25
 
23
- VALUE self = Data_Wrap_Struct(class, NULL, rawhid_close, dev);
26
+ rb_funcall(self, rb_intern("open"), 0);
27
+
28
+ return self;
29
+ }
30
+
31
+ VALUE RawHID_open(VALUE self) {
32
+ GET_RAWHID(hid, self);
33
+ int vId = NUM2INT(rb_iv_get(self, "@vendorId"));
34
+ int pId = NUM2INT(rb_iv_get(self, "@productId"));
35
+ int devices_opened = rawhid_open(1, &hid, vId, pId, -1, -1);
36
+ if(devices_opened == 0) {
37
+ rb_raise(RawHIDError, "Unable to open device");
38
+ }
24
39
  return self;
25
40
  }
26
41
 
27
42
  VALUE RawHID_send(int argc, VALUE *argv, VALUE class) {
28
43
  VALUE data, timeout_val;
29
- GET_RAWHID(dev, class);
44
+ GET_RAWHID(hid, class);
30
45
 
31
46
  rb_scan_args(argc, argv, "11", &data, &timeout_val);
32
47
 
@@ -37,13 +52,18 @@ VALUE RawHID_send(int argc, VALUE *argv, VALUE class) {
37
52
  if(len == 0) {
38
53
  return INT2NUM(0);
39
54
  }
40
- int sent = rawhid_send(dev, buf, len, timeout);
55
+ int sent = rawhid_send(hid, buf, len, timeout);
41
56
  return INT2NUM(sent);
42
57
  }
43
58
 
59
+ VALUE RawHID_close(VALUE class) {
60
+ GET_RAWHID(hid, class);
61
+ rawhid_close(hid) ? Qtrue : Qfalse;
62
+ }
63
+
44
64
  VALUE RawHID_recv(int argc, VALUE *argv, VALUE class) {
45
65
  VALUE data, len_val, timeout_val;
46
- GET_RAWHID(dev, class);
66
+ GET_RAWHID(hid, class);
47
67
 
48
68
  rb_scan_args(argc, argv, "11", &len_val, &timeout_val);
49
69
 
@@ -61,7 +81,7 @@ VALUE RawHID_recv(int argc, VALUE *argv, VALUE class) {
61
81
  rb_raise(RawHIDError, "Out of memory");
62
82
  }
63
83
  }
64
- int received = rawhid_recv(dev, buf, len, timeout);
84
+ int received = rawhid_recv(hid, buf, len, timeout);
65
85
 
66
86
  if(received < 0) {
67
87
  return INT2NUM(received);
@@ -77,6 +97,8 @@ void Init_teensy_rawhid() {
77
97
  rb_define_singleton_method(RawHID, "new", RawHID_new, 2);
78
98
  rb_define_method(RawHID, "raw_send", RawHID_send, -1);
79
99
  rb_define_method(RawHID, "raw_recv", RawHID_recv, -1);
100
+ rb_define_method(RawHID, "open", RawHID_open, 0);
101
+ rb_define_method(RawHID, "close", RawHID_close, 0);
80
102
 
81
103
  }
82
104
 
data/lib/rawhid.rb CHANGED
@@ -11,6 +11,8 @@ class RawHID
11
11
  end
12
12
  private :raw_send, :raw_recv
13
13
 
14
+ attr_reader :vendorId, :productId
15
+
14
16
  def write(data, timeout=0)
15
17
  if Array === data
16
18
  data = data.map(&:chr).join
@@ -1,3 +1,3 @@
1
1
  class RawHID
2
- VERSION = "0.1.7"
2
+ VERSION = "0.1.8"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rawhid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leo P.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-10-08 00:00:00.000000000 Z
11
+ date: 2017-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -88,6 +88,7 @@ files:
88
88
  - ext/teensy_rawhid/extconf.rb
89
89
  - ext/teensy_rawhid/hid.h
90
90
  - ext/teensy_rawhid/hid_LINUX.c
91
+ - ext/teensy_rawhid/hid_LINUX.h
91
92
  - ext/teensy_rawhid/hid_MACOSX.c
92
93
  - ext/teensy_rawhid/hid_WINDOWS.c
93
94
  - ext/teensy_rawhid/rawhid_test.c