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");
112 for(i = 0; i < sizeof(protoDesc) / sizeof(*protoDesc); i++)
113 assert(i == protoDesc[i].cmdId);
116 trinketDev = hid_open(0x2047, 0x0301, NULL);
118 if (trinketDev == NULL) {
119 fprintf(stderr, "hid_open fails\n");
123 if ((r = trinketPing()) != ERR_OK)
131 hid_close(trinketDev);
137 const TrinketProtoDesc *proto = &protoDesc[CMD_PING];
138 struct packed_struct {
145 {REPORT_ID, proto->cmdLen},
151 struct packed_struct {
155 uint8_t payload /* = 0 */;
160 assert(proto->cmdId == CMD_PING);
161 assert(sizeof(cmd) == cmd.len + sizeof(ProtoHeader));
162 assert(sizeof(resp) == proto->respLen + sizeof(ProtoHeader));
164 r = hid_write(trinketDev, (unsigned char*)&cmd, sizeof(cmd));
166 if (r < 0 || r != sizeof(cmd)) {
167 fprintf(stderr, "hid_write returns %d instead of %u\n", r, cmd.len);
171 /* must be assigned to 0x3F by the host */
172 resp.header.reportId = REPORT_ID;
174 r = hid_read_timeout(trinketDev, (unsigned char*)&resp, sizeof(resp), DEV_TIMEOUT);
176 fprintf(stderr, "hid_read_timeout fails\n");
177 return ERR_NO_ANSWER;
180 if (r != sizeof(resp)) {
181 fprintf(stderr, "hid_read_timeout returns %d instead of %u\n", r, (unsigned int)sizeof(resp));
185 if (resp.header.reportId != REPORT_ID) {
186 fprintf(stderr, "hid_read_timeout returns report id %02x instead of %02x\n", resp.header.reportId, REPORT_ID);
190 if (resp.header.size != proto->respLen) {
191 fprintf(stderr, "hid_read_timeout returns length %u intsead if %u\n", resp.header.size, proto->respLen);
195 if (resp.len != proto->respLen) {
196 fprintf(stderr, "hid_read_timeout response length %u instead of %u\n", resp.len, proto->respLen);
200 if (!(resp.begin == ACK_ANSWER_OK && resp.payload == 0 && resp.end == END_ANSWER)) {
201 fprintf(stderr, "hid_read_timeout ACK %02x with payload %u and ETB %02x\n", resp.begin, resp.payload, resp.end);
209 trinketGetCumDose(double *value) {
210 const TrinketProtoDesc *proto = &protoDesc[CMD_CUM_DOSE];
218 {REPORT_ID, proto->cmdLen},
224 struct packed_struct {
233 assert(proto->cmdId == CMD_CUM_DOSE);
234 assert(sizeof(cmd) == cmd.len + sizeof(ProtoHeader));
235 assert(sizeof(resp) == proto->respLen + sizeof(ProtoHeader));
237 r = hid_write(trinketDev, (unsigned char*)&cmd, sizeof(cmd));
239 if (r < 0 || r != sizeof(cmd)) {
240 fprintf(stderr, "hid_send_feature_report returns %d instead of %u\n", r, cmd.len);
244 /* must be assigned to 0x3F by the host */
245 resp.header.reportId = REPORT_ID;
247 r = hid_read_timeout(trinketDev, (unsigned char*)&resp, sizeof(resp), DEV_TIMEOUT);
249 fprintf(stderr, "hid_read_timeout fails\n");
250 return ERR_NO_ANSWER;
253 if (r != sizeof(resp)) {
254 fprintf(stderr, "hid_read_timeout returns %d instead of %u\n", r, (unsigned int)sizeof(resp));
258 if (resp.header.reportId != REPORT_ID) {
259 fprintf(stderr, "hid_read_timeout returns report id %02x instead of %02x\n", resp.header.reportId, REPORT_ID);
263 if (resp.header.size != proto->respLen) {
264 fprintf(stderr, "hid_read_timeout returns length %u intsead if %u\n", resp.header.size, proto->respLen);
268 if (resp.len != proto->respLen) {
269 fprintf(stderr, "hid_read_timeout response length %u instead of %u\n", resp.len, proto->respLen);
273 if (!(resp.begin == ACK_ANSWER_OK && resp.end == END_ANSWER)) {
274 fprintf(stderr, "hid_read_timeout ACK %02x and ETB %02x\n", resp.begin, resp.end);
278 *value = resp.payload;