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 *-------------------------------------------------------------------------
31 #include <hidapi/hidapi.h>
36 #define packed_struct __attribute__((packed))
51 #define BEGIN_COMMAND (0x1B)
52 #define END_COMMAND (0x17)
53 #define ACK_ANSWER_OK (0x06)
54 #define ACK_ANSWER_ERR (0x15)
55 #define END_ANSWER (0x17)
57 /* http://uglyduck.ath.cx/PDF/TexasInstruments/MSP430/usblib430/Programmers_Guide_MSP430_USB_API.pdf */
58 #define REPORT_ID (0x3f)
59 typedef struct packed_struct {
65 #define DEV_TIMEOUT (5000 /* milliseconds */)
71 uint8_t cmdLen; /* 0 means variable, > 0 fixed */
76 uint32_t respLen; /* 0 means variable, > 0 fixed */
79 /* hardcoded knowledge from docs/geyger.pdf */
80 static const TrinketProtoDesc protoDesc[] = {
82 {CMD_VCC_VALUE, 4, 8 },
83 {CMD_VCC_ALARM, 8, 8 },
84 {CMD_CLICK_DELAY, 8, 8 },
85 {CMD_ALARM_LEVELS, 24, 24 },
87 {CMD_DAY_ARCH, 4, 4 + 1440 * 2 },
88 {CMD_YEAR_ARCH, 4, 4 + 5840 * 2 },
89 {CMD_CUM_DOSE, 4, 12 },
90 {CMD_INVOKE_BSL, 4, 0 }
93 static hid_device *trinketDev = NULL;
97 dumpBuf(char *msg, uint8_t *buf, int len) {
100 fprintf(stderr,"%s:", msg);
101 for (i=0; i<len; i++)
102 fprintf(stderr, " %02x", buf[i]);
103 fprintf(stderr, "\n");
112 for(i = 0; i < sizeof(protoDesc) / sizeof(*protoDesc); i++)
113 tassert(i == protoDesc[i].cmdId);
115 trinketDev = hid_open(0x2047, 0x0301, NULL);
117 if (trinketDev == NULL) {
118 tlog(TL_WARN, "hid_open fails");
122 if ((r = trinketPing()) != ERR_OK)
130 hid_close(trinketDev);
136 const TrinketProtoDesc *proto = &protoDesc[CMD_PING];
137 struct packed_struct {
144 {REPORT_ID, proto->cmdLen},
150 struct packed_struct {
154 uint8_t payload /* = 0 */;
159 tassert(proto->cmdId == CMD_PING);
160 tassert(sizeof(cmd) == cmd.len + sizeof(ProtoHeader));
161 tassert(sizeof(resp) == proto->respLen + sizeof(ProtoHeader));
163 r = hid_write(trinketDev, (unsigned char*)&cmd, sizeof(cmd));
165 if (r < 0 || r != sizeof(cmd)) {
166 tlog(TL_WARN, "hid_write returns %d instead of %u", r, cmd.len);
170 /* must be assigned to 0x3F by the host */
171 resp.header.reportId = REPORT_ID;
173 r = hid_read_timeout(trinketDev, (unsigned char*)&resp, sizeof(resp), DEV_TIMEOUT);
175 tlog(TL_WARN, "hid_read_timeout fails");
176 return ERR_NO_ANSWER;
179 if (r != sizeof(resp)) {
180 tlog(TL_WARN, "hid_read_timeout returns %d instead of %u", r, (unsigned int)sizeof(resp));
184 if (resp.header.reportId != REPORT_ID) {
185 tlog(TL_WARN, "hid_read_timeout returns report id %02x instead of %02x", resp.header.reportId, REPORT_ID);
189 if (resp.header.size != proto->respLen) {
190 tlog(TL_WARN, "hid_read_timeout returns length %u intsead if %u", resp.header.size, proto->respLen);
194 if (resp.len != proto->respLen) {
195 tlog(TL_WARN, "hid_read_timeout response length %u instead of %u", resp.len, proto->respLen);
199 if (!(resp.begin == ACK_ANSWER_OK && resp.payload == 0 && resp.end == END_ANSWER)) {
200 tlog(TL_WARN, "hid_read_timeout ACK %02x with payload %u and ETB %02x", resp.begin, resp.payload, resp.end);
208 trinketGetCumDose(double *value) {
209 const TrinketProtoDesc *proto = &protoDesc[CMD_CUM_DOSE];
217 {REPORT_ID, proto->cmdLen},
223 struct packed_struct {
232 tassert(proto->cmdId == CMD_CUM_DOSE);
233 tassert(sizeof(cmd) == cmd.len + sizeof(ProtoHeader));
234 tassert(sizeof(resp) == proto->respLen + sizeof(ProtoHeader));
236 r = hid_write(trinketDev, (unsigned char*)&cmd, sizeof(cmd));
238 if (r < 0 || r != sizeof(cmd)) {
239 tlog(TL_WARN, "hid_send_feature_report returns %d instead of %u", r, cmd.len);
243 /* must be assigned to 0x3F by the host */
244 resp.header.reportId = REPORT_ID;
246 r = hid_read_timeout(trinketDev, (unsigned char*)&resp, sizeof(resp), DEV_TIMEOUT);
248 tlog(TL_WARN, "hid_read_timeout fails");
249 return ERR_NO_ANSWER;
252 if (r != sizeof(resp)) {
253 tlog(TL_WARN, "hid_read_timeout returns %d instead of %u", r, (unsigned int)sizeof(resp));
257 if (resp.header.reportId != REPORT_ID) {
258 tlog(TL_WARN, "hid_read_timeout returns report id %02x instead of %02x", resp.header.reportId, REPORT_ID);
262 if (resp.header.size != proto->respLen) {
263 tlog(TL_WARN, "hid_read_timeout returns length %u intsead if %u", resp.header.size, proto->respLen);
267 if (resp.len != proto->respLen) {
268 tlog(TL_WARN, "hid_read_timeout response length %u instead of %u", resp.len, proto->respLen);
272 if (!(resp.begin == ACK_ANSWER_OK && resp.end == END_ANSWER)) {
273 tlog(TL_WARN, "hid_read_timeout ACK %02x and ETB %02x", resp.begin, resp.end);
277 *value = resp.payload;