2 * Copyright (C) 2012 Timo Kokkonen <timo.t.kokkonen@iki.fi>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 #include <linux/types.h>
23 #include <linux/hiddev.h>
29 #include "contour-protocol.h"
34 static int send_msg(const struct msg *msg, int fd, int usage_code)
37 if (msg->direction != OUT) {
38 trace(0, "Message direction is not OUT\n");
43 trace(1, "Sending: ");
45 print_hex(msg->data + 1, datalen(msg->data) - 1);
47 print_ascii(msg->data + 1, datalen(msg->data) - 1);
49 ret = hiddev_write(msg->data, fd, usage_code);
56 static int send_msg_with_proggress_note(const struct msg *msg, int fd,
61 if (trace_level < 1 && msg_count <= MAX_MSGS) {
62 trace(0, "\r%d%%", msg_count * 100 / MAX_MSGS);
68 return send_msg(msg, fd, usage_code);
71 static int read_and_verify(struct msg *msg, int fd)
73 unsigned char buf[64];
77 ret = hiddev_read(buf + offset, sizeof(buf) - offset, fd);
85 memcpy(msg->data, buf, sizeof(buf));
86 memset(&msg->data[sizeof(buf)], 0, sizeof(msg->data)-sizeof(buf));
87 trace(2, "Got data %d: ", datalen(buf));
89 print_hex(buf, datalen(buf));
91 print_ascii(buf, datalen(buf));
96 static int read_msgs(int fd)
100 read_and_verify(&msg, fd);
101 if (datalen(msg.data) <= 36)
108 static void set_first_msg_byte(struct msg *msg, int *idx, char byte)
110 memset(msg->data, 0, sizeof(msg->data));
111 msg->data[4] = (byte);
116 static void set_msg_byte(struct msg *msg, int *idx, int where, char byte)
118 msg->data[where + 4] = byte;
122 #define SET_BYTES(byte) msg.data[j++] = (byte)
124 static int send_pattern(int fd, int uc, unsigned char byte1, unsigned char byte2)
131 set_first_msg_byte(&msg, &j, 0x01);
133 send_msg_with_proggress_note(&msg, fd, uc);
137 set_msg_byte(&msg, &j, 1, 0x15);
138 send_msg_with_proggress_note(&msg, fd, uc);
142 set_msg_byte(&msg, &j, 1, 0x05);
143 send_msg_with_proggress_note(&msg, fd, uc);
146 set_first_msg_byte(&msg, &j, 0x02);
149 send_msg_with_proggress_note(&msg, fd, uc);
153 set_msg_byte(&msg, &j, 1, byte2);
154 send_msg_with_proggress_note(&msg, fd, uc);
160 int contour_initialize(int fd, int uc)
167 set_first_msg_byte(&msg, &j, 0x01);
169 send_msg_with_proggress_note(&msg, fd, uc);
172 set_msg_byte(&msg, &j, 1, 0x06);
173 send_msg_with_proggress_note(&msg, fd, uc);
176 set_msg_byte(&msg, &j, 1, 0x15);
177 for (i = 0; i < 6; i++) {
179 send_msg_with_proggress_note(&msg, fd, uc);
184 send_msg_with_proggress_note(&msg, fd, uc);
188 set_msg_byte(&msg, &j, 1, 0x05);
189 send_msg_with_proggress_note(&msg, fd, uc);
192 send_pattern(fd, uc, 'R', 'A');
193 send_pattern(fd, uc, 'R', 'C');
194 send_pattern(fd, uc, 'R', 'D');
195 send_pattern(fd, uc, 'R', 'G');
196 send_pattern(fd, uc, 'R', 'I');
197 send_pattern(fd, uc, 'R', 'M');
198 send_pattern(fd, uc, 'R', 'P');
199 send_pattern(fd, uc, 'R', 'R');
200 send_pattern(fd, uc, 'R', 'S');
201 send_pattern(fd, uc, 'R', 'T');
202 send_pattern(fd, uc, 'R', 'U');
203 send_pattern(fd, uc, 'R', 'V');
204 send_pattern(fd, uc, 'R', 'W');
205 send_pattern(fd, uc, 'R', 'X');
206 send_pattern(fd, uc, 'W', 'K');
209 set_first_msg_byte(&msg, &j, 0x08);
218 send_msg_with_proggress_note(&msg, fd, uc);
222 set_first_msg_byte(&msg, &j, 0x02);
225 send_msg_with_proggress_note(&msg, fd, uc);
229 set_msg_byte(&msg, &j, 1, 'Y');
230 send_msg_with_proggress_note(&msg, fd, uc);
234 set_msg_byte(&msg, &j, 1, 'W');
235 send_msg_with_proggress_note(&msg, fd, uc);
239 set_msg_byte(&msg, &j, 1, 'K');
240 send_msg_with_proggress_note(&msg, fd, uc);
244 set_msg_byte(&msg, &j, 1, 'C');
245 send_msg_with_proggress_note(&msg, fd, uc);
248 send_pattern(fd, uc, 'R', 'Z');
251 set_first_msg_byte(&msg, &j, 0x01);
253 send_msg_with_proggress_note(&msg, fd, uc);
257 set_msg_byte(&msg, &j, 1, 0x15);
258 send_msg_with_proggress_note(&msg, fd, uc);
262 set_msg_byte(&msg, &j, 1, 0x05);
263 send_msg_with_proggress_note(&msg, fd, uc);
267 set_msg_byte(&msg, &j, 1, 0x04);
268 send_msg_with_proggress_note(&msg, fd, uc);
272 send_msg_with_proggress_note(&msg, fd, uc);
276 set_msg_byte(&msg, &j, 1, 0x06);
277 send_msg_with_proggress_note(&msg, fd, uc);
281 send_msg_with_proggress_note(&msg, fd, uc);
285 send_msg_with_proggress_note(&msg, fd, uc);
294 int contour_read_entry(int fd, int uc, struct msg *in)
300 set_first_msg_byte(&msg, &j, 0x01);
301 set_msg_byte(&msg, &j, 1, 0x06);
302 send_msg(&msg, fd, uc);
304 read_and_verify(in, fd);
305 return datalen(in->data);
308 int wait_for_device(int vendor, int product, int *usage_code)
312 fd = hiddev_open_by_id(vendor, product, usage_code);
318 "No suitable device found. Please plug in your glucose meter\n");
321 fd = hiddev_open_by_id(vendor, product, usage_code);