-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathradiosh.c
357 lines (301 loc) · 11.5 KB
/
radiosh.c
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*
radiosh -- OS X shell control for the Griffin radioSHARK v1/v2 with IOKit
1.0
Based on rslight by Quentin D. Carnicelli ([email protected]) and shark by
Michael Rolig ([email protected]) and Justin Yunke
([email protected]) and shark2 by Hisaaki Shibata ([email protected])
Merged by Cameron Kaiser ([email protected]), cleaned up, warnings
quashed, bugs fixed, and converted to my personal house style.
Copyright (C)2018 Contributors as enumerated. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the product nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <getopt.h>
#include <Availability.h>
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/IOCFPlugIn.h>
#include <IOKit/usb/IOUSBLib.h>
#include <IOKit/usb/USBSpec.h>
#include <IOKit/hid/IOHIDLib.h>
#include <IOKit/hid/IOHIDKeys.h>
enum {
kRadioSharkVendorID = 0x077D,
kRadioSharkProductID = 0x627A,
kRadioSharkv1Version = 0x0001,
kRadioSharkv2Version = 0x0010,
};
typedef struct {
char fBlueLightLevel;
char fBlueLightPulse;
char fRedLightLevel;
char fRadioBand;
unsigned char fRadioFreqHi;
unsigned char fRadioFreqLo;
} rsSettings;
io_name_t deviceName; // "char[128]"
int mode = 0; // 0 = radioshark, non-zero = radioShark 2
int verbose = 0; // 0 = no
void _printUsage()
{
FILE* out = stderr;
fprintf(out, "Usage: radiosh [-v] [-b#] [-p#] [-r#] [-a#] [-f#]\n");
fprintf(out, " -v Verbosity/display detected device version.\n");
fprintf(out, " -b Set the blue light brightness, values are 0 (off) to 127.\n");
fprintf(out, " -p Set the blue light pulse speed, values are 0 (off) to 127 (slow).\n");
fprintf(out, " -r Set the red light brightness, values are 0 (off) to 127.\n");
fprintf(out, " -a Set the radio to AM and tune to frequency in kHz (0=radio off).\n");
fprintf(out, " -f Set the radio to FM and tune to frequency in MHz (0=radio off).\n");
fprintf(out, "Any combination can be specified at once.\n");
fprintf(out, "\n");
fprintf(out, "Copyright (C) 2018 Cameron Kaiser, Quentin D. Carnicelli,\n");
fprintf(out, "Michael Rolig, Justin Yunke and Hisaaki Shibata. All rights reserved.\n");
fprintf(out, "http://www.floodgap.com/software/radiosh/ -- version 1.0\n");
exit(1);
}
int _atoi(const char *s)
{
if (!isdigit(s[0])) {
_printUsage();
return -1; // unreached
}
return atoi(s);
}
int _parseArguments(int argc, char **argv, rsSettings* settings)
{
int opt;
int arg;
float freq;
if(!settings)
return 0;
if(argc < 2)
return 0;
while((opt = getopt(argc, (char* const*)argv, "b:p:r:a:f:?hv")) != -1)
{
switch(opt)
{
case 'v':
verbose = 1;
fprintf(stderr, "radioSHARK version detected: %s\n",
(mode) ? "v2" : "v1");
break;
case 'b':
arg = _atoi(optarg);
arg = arg < 0 ? 0 : arg > 127 ? 127 : arg;
settings->fBlueLightLevel = arg;
break;
case 'p':
arg = _atoi(optarg);
arg = arg < 0 ? 0 : arg > 127 ? 127 : arg;
settings->fBlueLightPulse = arg;
break;
case 'r':
arg = _atoi(optarg);
arg = arg < 0 ? 0 : arg > 127 ? 127 : arg;
settings->fRedLightLevel = arg;
break;
case 'a':
if (mode) {
arg = (_atoi(optarg) * 4) + 16300;
settings->fRadioBand = 0x24;
} else {
arg = _atoi(optarg) + 450;
settings->fRadioBand = 0x12;
}
settings->fRadioFreqHi = (arg >> 8) & 0xff;
settings->fRadioFreqLo = (arg & 0xff);
break;
case 'f':
// This is a float, so we can't use our
// convenience function.
if (!isdigit(optarg[0])) {
_printUsage();
return 0; // unreached
}
freq = atof(optarg);
if (mode) {
arg = ((freq * 10 * 2) - 3);
settings->fRadioBand = 0x28;
} else {
arg = ((freq * 1000) + 10700) / 12.5;
arg += 3;
settings->fRadioBand = 0x00;
}
settings->fRadioFreqHi = (arg >> 8) & 0xff;
settings->fRadioFreqLo = (arg & 0xff);
break;
case '?':
case 'h':
_printUsage();
default:
return 0;
}
}
return 1;
}
CFMutableDictionaryRef _getMatchingDictionary(UInt16 version)
{
CFMutableDictionaryRef matchingDict = NULL;
int val;
CFNumberRef valRef;
matchingDict = IOServiceMatching(kIOHIDDeviceKey);
if(matchingDict)
{
val = kRadioSharkVendorID;
valRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &val);
CFDictionarySetValue(matchingDict, CFSTR(kIOHIDVendorIDKey), valRef);
CFRelease(valRef);
val = kRadioSharkProductID;
valRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &val);
CFDictionarySetValue(matchingDict, CFSTR(kIOHIDProductIDKey), valRef);
CFRelease(valRef);
valRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt16Type, &version);
CFDictionarySetValue(matchingDict, CFSTR(kIOHIDVersionNumberKey), valRef);
CFRelease(valRef);
}
return matchingDict;
}
io_service_t _getIOService(CFMutableDictionaryRef matchingDict)
{
if(!matchingDict) return (io_service_t)0;
#if defined(__MAC_12_0) && __MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_12_0
return IOServiceGetMatchingService(kIOMainPortDefault, matchingDict);
#else
return IOServiceGetMatchingService(kIOMasterPortDefault, matchingDict);
#endif
}
IOHIDDeviceInterface** _getHIDInterface(io_service_t service)
{
IOCFPlugInInterface** iodev = NULL;
IOHIDDeviceInterface** hidInterface = NULL;
kern_return_t result;
SInt32 score = 0;
if(!service) return NULL;
result = IOCreatePlugInInterfaceForService(service, kIOHIDDeviceUserClientTypeID, kIOCFPlugInInterfaceID, &iodev, &score);
if (result == KERN_SUCCESS && iodev)
{
result = (*iodev)->QueryInterface(iodev, CFUUIDGetUUIDBytes(kIOHIDDeviceInterfaceID), (LPVOID) &hidInterface);
if (result != KERN_SUCCESS)
hidInterface = NULL;
(*iodev)->Release(iodev);
}
return hidInterface;
}
/* The v1 "RadioSHARK" uses a 6-byte HID report packet.
The v2 "radioSHARK" (note string) uses a 7-byte packet. */
kern_return_t _setBlueLight(IOHIDDeviceInterface** hidInterface, char level)
{
if (mode) {
char report[7] = { 0x83, level, 0, 0, 0, 0, 0 };
if (level < 0) return KERN_SUCCESS;
return (*hidInterface)->setReport(hidInterface, kIOHIDReportTypeOutput, 0, report, sizeof(report), 1000, NULL, NULL, NULL);
}
char report[6] = { 0xa0, level, 0, 0, 0, 0 };
if (level < 0) return KERN_SUCCESS;
return (*hidInterface)->setReport(hidInterface, kIOHIDReportTypeOutput, 0, report, sizeof(report), 1000, NULL, NULL, NULL);
}
kern_return_t _setBluePulse(IOHIDDeviceInterface** hidInterface, char level)
{
if (mode) {
if (level < 0) return KERN_SUCCESS;
// Tried both 0xa1 and 0x82 and they don't do anything.
fprintf(stderr, "Oops: pulsing not supported on v2 devices.\n");
return KERN_INVALID_ARGUMENT;
}
char report[6] = { 0xa1, level, 0, 0, 0, 0 };
if (level < 0) return KERN_SUCCESS;
return (*hidInterface)->setReport(hidInterface, kIOHIDReportTypeOutput, 0, report, sizeof(report), 1000, NULL, NULL, NULL);
}
kern_return_t _setRedLight(IOHIDDeviceInterface** hidInterface, char level)
{
if (mode) {
char report[7] = { 0x84, level, 0, 0, 0, 0, 0 };
if (level < 0) return KERN_SUCCESS;
return (*hidInterface)->setReport(hidInterface, kIOHIDReportTypeOutput, 0, report, sizeof(report), 1000, NULL, NULL, NULL);
}
char report[6] = { (level > 0 ? 0xa9 : 0xa8), 0, 0, 0, 0, 0 };
if (level < 0) return KERN_SUCCESS;
return (*hidInterface)->setReport(hidInterface, kIOHIDReportTypeOutput, 0, report, sizeof(report), 1000, NULL, NULL, NULL);
}
kern_return_t _setRadio(IOHIDDeviceInterface** hidInterface, rsSettings* settings)
{
if (mode) {
char report[7] = { 0x81, settings->fRadioFreqHi, settings->fRadioFreqLo,
(settings->fRadioBand == 0x24) ? 0xf3 : 0x33,
(settings->fRadioBand == 0x24) ? 0x36 : 0x04,
0x00,
settings->fRadioBand };
if (settings->fRadioBand < 0) return KERN_SUCCESS;
return (*hidInterface)->setReport(hidInterface, kIOHIDReportTypeOutput, 0, report, sizeof(report), 1000, NULL, NULL, NULL);
}
char report[6] = { 0xc0, settings->fRadioBand, settings->fRadioFreqHi, settings->fRadioFreqLo, 0, 0 };
if (settings->fRadioBand < 0) return KERN_SUCCESS;
return (*hidInterface)->setReport(hidInterface, kIOHIDReportTypeOutput, 0, report, sizeof(report), 1000, NULL, NULL, NULL);
}
/* Laziness */
#define DAMMIT(x, y, z) \
x = y; if (!x) { fprintf(stderr, z); return -1; }
#define KDAMMIT(y, z, ...) \
result = y; if (result != KERN_SUCCESS) { fprintf(stderr, z, __VA_ARGS__); return -1; }
int main(int argc, char **argv)
{
rsSettings settings = { -1, -1, -1, -1, 0, 0 };
CFMutableDictionaryRef matchingDict = NULL;
io_service_t service = (io_service_t)0;
IOHIDDeviceInterface** hidInterface = NULL;
kern_return_t result;
if (argc == 1 || (argc == 2 && argv[1][0] == '-' && argv[1][1] == 'v')) {
_printUsage();
return 1;
}
/* XXX: If you have multiple devices connected, one will be opened at random.
If one is a v1 and the other is a v2, the v1 will get precedence. */
DAMMIT(matchingDict, _getMatchingDictionary(kRadioSharkv1Version),
"InternalError: Could not create io service matching dictionary v1\n");
if (!(service = _getIOService(matchingDict))) {
DAMMIT(matchingDict, _getMatchingDictionary(kRadioSharkv2Version),
"InternalError: Could not create io service matching dictionary v2\n");
DAMMIT(service, _getIOService(matchingDict),
"IOError: Could not find attached radioSHARK v1 or v2 device\n");
mode = 1;
}
DAMMIT(hidInterface, _getHIDInterface(service),
"IOError: Could find the HID interface of the radioSHARK device\n");
if(!_parseArguments(argc, argv, &settings)) {
_printUsage();
return 1;
}
KDAMMIT((*hidInterface)->open(hidInterface, 0),
"IOError: Could open the HID interface of the radioSHARK device (%d)\n", result);
KDAMMIT(_setBlueLight(hidInterface, settings.fBlueLightLevel),
"IOError: Setting the blue light failed (%d)\n", result);
KDAMMIT(_setBluePulse(hidInterface, settings.fBlueLightPulse),
"IOError: Setting the blue light pulse failed (%d)\n", result);
KDAMMIT(_setRedLight(hidInterface, settings.fRedLightLevel),
"IOError: Setting the red light failed (%d)\n", result);
KDAMMIT(_setRadio(hidInterface, &settings),
"IOError: Setting the radio failed (%d)\n", result);
(*hidInterface)->close(hidInterface);
(*hidInterface)->Release(hidInterface);
IOObjectRelease(service);
return 0;
}