This repository has been archived by the owner on Mar 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
crazyradio.js
157 lines (131 loc) · 4 KB
/
crazyradio.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
var Crazyradio = (function() {
"use strict";
var state = "closed";
var noop = function(state, data) {
return;
};
// Generic helper function to implement the radio control transfer
function controlTransfer(request, value, data, callback) {
var transfer = {
direction: "out",
recipient: "device",
requestType: "vendor",
request: request,
index: 0,
value: value,
data: data
};
chrome.usb.controlTransfer(my.handle, transfer, function(info) {
if (!info || info.resultCode !== 0) {
console.error("Error executing control channel");
callback(false);
} else {
callback(true);
}
});
}
// Public methods and states
var my = {handle: undefined};
my.open = function(openedCb) {
if (typeof(openedCb) !== "function") {
openedCb = noop;
}
if (state !== "closed") {
console.warn("Trying to re-open already openned radio, ignoring");
openedCb(false);
return;
}
chrome.usb.getDevices( {
"vendorId": 6421,
"productId": 30583
}, function(found_devices) {
if (chrome.runtime.lastError !== undefined) {
console.warn('chrome.usb.getDevices error: ' +
chrome.runtime.lastError.message);
openedCb(false);
return;
}
if (found_devices.length > 0) {
var device = found_devices[0];
console.log("Oppening Crazyradio dongle");
chrome.usb.openDevice(device, function(handle) {
console.log(handle);
chrome.usb.claimInterface(handle, 0, function () {
if (chrome.runtime.lastError !== undefined) {
console.warn('chrome.usb.claimInterface error: ' +
chrome.runtime.lastError.message);
openedCb(false);
return;
}
my.handle = handle;
state = "opened";
openedCb(true);
});
});
} else {
console.error("Cannot find Crazyradio dongle!");
openedCb(false);
}
});
};
my.sendPacket = function(buffer, packetSendCb) {
var to = {
'direction': 'out',
'endpoint': 1,
'data' : buffer
};
chrome.usb.bulkTransfer(my.handle, to, function(info) {
//console.log(info);
if (info.resultCode !== 0) {
console.error("Cannot send data to the dongle");
} else {
var ti = {
'direction': 'in',
'endpoint': 1,
'length': 64,
};
chrome.usb.bulkTransfer(my.handle, ti, function(info) {
if (info.resultCode !== 0) {
console.error("Cannot receive data from the dongle");
} else {
var ack = new Uint8Array(info.data);
packetSendCb(ack[0]!==0, ack.subarray(1).buffer);
}
});
}
});
};
my.setChannel = function(channel, callback) {
channel = Number(channel);
if ((channel<0) || (channel>125)) {
callback(false);
my.error = "Error: cannot set a channel outside of [0 125]";
}
controlTransfer(0x01, channel, new ArrayBuffer(0), callback);
};
my.setDatarate = function(datarate_str, callback) {
var datarate;
if (datarate_str === "250Kbps") datarate = 0;
else if (datarate_str === "1Mbps") datarate = 1;
else if (datarate_str === "2Mbps") datarate = 2;
else {
my.error = "Error: Wrong value, not a datarate: " + datarate_str;
callback(false);
console.error(my.error);
return;
}
controlTransfer(0x03, datarate, new ArrayBuffer(0), callback);
};
my.close = function() {
if (state !== "opened") {
return;
}
state = "closing";
chrome.usb.releaseInterface(my.handle, 0, function() {
chrome.usb.closeDevice(my.handle, noop);
state = "closed";
});
};
my.packetSent = noop;
return my;
}());