1 /*-------------------------------------------------------------------------
4 * Copyright (c) 2016, Teodor Sigaev <teodor@sigaev.ru>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 *-------------------------------------------------------------------------
32 #include <hidapi/hidapi.h>
35 #define packed_struct __attribute__((packed))
50 #define BEGIN_COMMAND (0x1B)
51 #define END_COMMAND (0x17)
52 #define ACK_ANSWER_OK (0x06)
53 #define ACK_ANSWER_ERR (0x15)
54 #define END_ANSWER (0x17)
56 /* http://uglyduck.ath.cx/PDF/TexasInstruments/MSP430/usblib430/Programmers_Guide_MSP430_USB_API.pdf */
57 #define REPORT_ID (0x3f)
58 typedef struct packed_struct {
64 #define DEV_TIMEOUT (5000 /* milliseconds */)
70 uint8_t cmdLen; /* 0 means variable, > 0 fixed */
75 uint32_t respLen; /* 0 means variable, > 0 fixed */
78 /* hardcoded knowledge from docs/geyger.pdf */
79 static const TrinketProtoDesc protoDesc[] = {
81 {CMD_VCC_VALUE, 4, 8 },
82 {CMD_VCC_ALARM, 8, 8 },
83 {CMD_CLICK_DELAY, 8, 8 },
84 {CMD_ALARM_LEVELS, 24, 24 },
86 {CMD_DAY_ARCH, 4, 4 + 1440 * 2 },
87 {CMD_YEAR_ARCH, 4, 4 + 5840 * 2 },
88 {CMD_CUM_DOSE, 4, 12 },
89 {CMD_INVOKE_BSL, 4, 0 }
92 static hid_device *trinketDev = NULL;
96 dumpBuf(char *msg, uint8_t *buf, int len) {
99 fprintf(stderr,"%s:", msg);
100 for (i=0; i<len; i++)
101 fprintf(stderr, " %02x", buf[i]);
102 fprintf(stderr, "\n");
111 for(i = 0; i < sizeof(protoDesc) / sizeof(*protoDesc); i++)
112 assert(i == protoDesc[i].cmdId);
115 trinketDev = hid_open(0x2047, 0x0301, NULL);
117 if (trinketDev == NULL) {
118 fprintf(stderr, "hid_open fails\n");
122 return trinketPing();
128 hid_close(trinketDev);
134 const TrinketProtoDesc *proto = &protoDesc[CMD_PING];
135 struct packed_struct {
142 {REPORT_ID, proto->cmdLen},
148 struct packed_struct {
152 uint8_t payload /* = 0 */;
157 assert(proto->cmdId == CMD_PING);
158 assert(sizeof(cmd) == cmd.len + sizeof(ProtoHeader));
159 assert(sizeof(resp) == proto->respLen + sizeof(ProtoHeader));
161 r = hid_write(trinketDev, (unsigned char*)&cmd, sizeof(cmd));
163 if (r < 0 || r != sizeof(cmd)) {
164 fprintf(stderr, "hid_write returns %d instead of %u\n", r, cmd.len);
168 /* must be assigned to 0x3F by the host */
169 resp.header.reportId = REPORT_ID;
171 r = hid_read_timeout(trinketDev, (unsigned char*)&resp, sizeof(resp), DEV_TIMEOUT);
173 fprintf(stderr, "hid_read_timeout fails\n");
174 return ERR_NO_ANSWER;
177 if (r != sizeof(resp)) {
178 fprintf(stderr, "hid_read_timeout returns %d instead of %u\n", r, (unsigned int)sizeof(resp));
182 if (resp.header.reportId != REPORT_ID) {
183 fprintf(stderr, "hid_read_timeout returns report id %02x instead of %02x\n", resp.header.reportId, REPORT_ID);
187 if (resp.header.size != proto->respLen) {
188 fprintf(stderr, "hid_read_timeout returns length %u intsead if %u\n", resp.header.size, proto->respLen);
192 if (resp.len != proto->respLen) {
193 fprintf(stderr, "hid_read_timeout response length %u instead of %u\n", resp.len, proto->respLen);
197 if (!(resp.begin == ACK_ANSWER_OK && resp.payload == 0 && resp.end == END_ANSWER)) {
198 fprintf(stderr, "hid_read_timeout ACK %02x with payload %u and ETB %02x\n", resp.begin, resp.payload, resp.end);
206 trinketGetCumDose(double *value) {
207 const TrinketProtoDesc *proto = &protoDesc[CMD_CUM_DOSE];
215 {REPORT_ID, proto->cmdLen},
221 struct packed_struct {
230 assert(proto->cmdId == CMD_CUM_DOSE);
231 assert(sizeof(cmd) == cmd.len + sizeof(ProtoHeader));
232 assert(sizeof(resp) == proto->respLen + sizeof(ProtoHeader));
234 r = hid_write(trinketDev, (unsigned char*)&cmd, sizeof(cmd));
236 if (r < 0 || r != sizeof(cmd)) {
237 fprintf(stderr, "hid_send_feature_report returns %d instead of %u\n", r, cmd.len);
241 /* must be assigned to 0x3F by the host */
242 resp.header.reportId = REPORT_ID;
244 r = hid_read_timeout(trinketDev, (unsigned char*)&resp, sizeof(resp), DEV_TIMEOUT);
246 fprintf(stderr, "hid_read_timeout fails\n");
247 return ERR_NO_ANSWER;
250 if (r != sizeof(resp)) {
251 fprintf(stderr, "hid_read_timeout returns %d instead of %u\n", r, (unsigned int)sizeof(resp));
255 if (resp.header.reportId != REPORT_ID) {
256 fprintf(stderr, "hid_read_timeout returns report id %02x instead of %02x\n", resp.header.reportId, REPORT_ID);
260 if (resp.header.size != proto->respLen) {
261 fprintf(stderr, "hid_read_timeout returns length %u intsead if %u\n", resp.header.size, proto->respLen);
265 if (resp.len != proto->respLen) {
266 fprintf(stderr, "hid_read_timeout response length %u instead of %u\n", resp.len, proto->respLen);
270 if (!(resp.begin == ACK_ANSWER_OK && resp.end == END_ANSWER)) {
271 fprintf(stderr, "hid_read_timeout ACK %02x and ETB %02x\n", resp.begin, resp.end);
275 *value = resp.payload;