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 static const TrinketProtoDesc protoDesc[] = {
80 {CMD_VCC_VALUE, 4, 8 },
81 {CMD_VCC_ALARM, 8, 8 },
82 {CMD_CLICK_DELAY, 8, 8 },
83 {CMD_ALARM_LEVELS, 24, 24 },
85 {CMD_DAY_ARCH, 4, 4 + 1440 * 2 },
86 {CMD_YEAR_ARCH, 4, 4 + 5840 * 2 },
87 {CMD_CUM_DOSE, 4, 12 },
88 {CMD_INVOKE_BSL, 4, 0 }
91 static hid_device *trinketDev = NULL;
95 dumpBuf(char *msg, uint8_t *buf, int len) {
98 fprintf(stderr,"%s:", msg);
100 fprintf(stderr, " %02x", buf[i]);
101 fprintf(stderr, "\n");
110 for(i = 0; i < sizeof(protoDesc) / sizeof(*protoDesc); i++)
111 assert(i == protoDesc[i].cmdId);
114 trinketDev = hid_open(0x2047, 0x0301, NULL);
116 if (trinketDev == NULL) {
117 fprintf(stderr, "hid_open fails\n");
121 return trinketPing();
127 hid_close(trinketDev);
133 const TrinketProtoDesc *proto = &protoDesc[CMD_PING];
134 struct packed_struct {
141 {REPORT_ID, proto->cmdLen},
147 struct packed_struct {
151 uint8_t payload /* = 0 */;
156 assert(proto->cmdId == CMD_PING);
157 assert(sizeof(cmd) == cmd.len + sizeof(ProtoHeader));
158 assert(sizeof(resp) == proto->respLen + sizeof(ProtoHeader));
160 r = hid_write(trinketDev, (unsigned char*)&cmd, sizeof(cmd));
162 if (r < 0 || r != sizeof(cmd)) {
163 fprintf(stderr, "hid_write returns %d instead of %u\n", r, cmd.len);
167 /* must be assigned to 0x3F by the host */
168 resp.header.reportId = REPORT_ID;
170 r = hid_read_timeout(trinketDev, (unsigned char*)&resp, sizeof(resp), DEV_TIMEOUT);
172 fprintf(stderr, "hid_read_timeout fails\n");
173 return ERR_NO_ANSWER;
176 if (r != sizeof(resp)) {
177 fprintf(stderr, "hid_read_timeout returns %d instead of %u\n", r, sizeof(resp));
181 if (resp.header.reportId != REPORT_ID) {
182 fprintf(stderr, "hid_read_timeout returns report id %02x instead of %02x\n", resp.header.reportId, REPORT_ID);
186 if (resp.header.size != proto->respLen) {
187 fprintf(stderr, "hid_read_timeout returns length %u intsead if %u\n", resp.header.size, proto->respLen);
191 if (resp.len != proto->respLen) {
192 fprintf(stderr, "hid_read_timeout response length %u instead of %u\n", resp.len, proto->respLen);
196 if (!(resp.begin == ACK_ANSWER_OK && resp.payload == 0 && resp.end == END_ANSWER)) {
197 fprintf(stderr, "hid_read_timeout ACK %02x with payload %u and ETB %02x\n", resp.begin, resp.payload, resp.end);
205 trinketGetCumDose(double *value) {
206 const TrinketProtoDesc *proto = &protoDesc[CMD_CUM_DOSE];
214 {REPORT_ID, proto->cmdLen},
220 struct packed_struct {
229 assert(proto->cmdId == CMD_CUM_DOSE);
230 assert(sizeof(cmd) == cmd.len + sizeof(ProtoHeader));
231 assert(sizeof(resp) == proto->respLen + sizeof(ProtoHeader));
233 r = hid_write(trinketDev, (unsigned char*)&cmd, sizeof(cmd));
235 if (r < 0 || r != sizeof(cmd)) {
236 fprintf(stderr, "hid_send_feature_report returns %d instead of %u\n", r, cmd.len);
240 /* must be assigned to 0x3F by the host */
241 resp.header.reportId = REPORT_ID;
243 r = hid_read_timeout(trinketDev, (unsigned char*)&resp, sizeof(resp), DEV_TIMEOUT);
245 fprintf(stderr, "hid_read_timeout fails\n");
246 return ERR_NO_ANSWER;
249 if (r != sizeof(resp)) {
250 fprintf(stderr, "hid_read_timeout returns %d instead of %u\n", r, sizeof(resp));
254 if (resp.header.reportId != REPORT_ID) {
255 fprintf(stderr, "hid_read_timeout returns report id %02x instead of %02x\n", resp.header.reportId, REPORT_ID);
259 if (resp.header.size != proto->respLen) {
260 fprintf(stderr, "hid_read_timeout returns length %u intsead if %u\n", resp.header.size, proto->respLen);
264 if (resp.len != proto->respLen) {
265 fprintf(stderr, "hid_read_timeout response length %u instead of %u\n", resp.len, proto->respLen);
269 if (!(resp.begin == ACK_ANSWER_OK && resp.end == END_ANSWER)) {
270 fprintf(stderr, "hid_read_timeout ACK %02x and ETB %02x\n", resp.begin, resp.end);
274 *value = resp.payload;