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 trace(2, "Got data %d: ", datalen(buf));
88 print_hex(buf, datalen(buf));
90 print_ascii(buf, datalen(buf));
95 static int read_msgs(int fd)
99 read_and_verify(&msg, fd);
100 if (datalen(msg.data) <= 36)
107 #define SET_FIRST_BYTE(byte) \
109 memset(&msg.data, 0, sizeof(msg.data)); \
110 msg.data[4] = (byte); \
114 #define SET_BYTE(idx, byte) \
116 msg.data[(idx) + 4] = (byte); \
120 #define SET_BYTES(byte) msg.data[j++] = (byte)
122 static int send_pattern(int fd, int uc, unsigned char byte1, unsigned char byte2)
129 SET_FIRST_BYTE(0x01);
131 send_msg_with_proggress_note(&msg, fd, uc);
136 send_msg_with_proggress_note(&msg, fd, uc);
141 send_msg_with_proggress_note(&msg, fd, uc);
144 SET_FIRST_BYTE(0x02);
147 send_msg_with_proggress_note(&msg, fd, uc);
152 send_msg_with_proggress_note(&msg, fd, uc);
158 int contour_initialize(int fd, int uc)
165 SET_FIRST_BYTE(0x01);
167 send_msg_with_proggress_note(&msg, fd, uc);
171 send_msg_with_proggress_note(&msg, fd, uc);
175 for (i = 0; i < 6; i++) {
177 send_msg_with_proggress_note(&msg, fd, uc);
182 send_msg_with_proggress_note(&msg, fd, uc);
187 send_msg_with_proggress_note(&msg, fd, uc);
190 send_pattern(fd, uc, 'R', 'A');
191 send_pattern(fd, uc, 'R', 'C');
192 send_pattern(fd, uc, 'R', 'D');
193 send_pattern(fd, uc, 'R', 'G');
194 send_pattern(fd, uc, 'R', 'I');
195 send_pattern(fd, uc, 'R', 'M');
196 send_pattern(fd, uc, 'R', 'P');
197 send_pattern(fd, uc, 'R', 'R');
198 send_pattern(fd, uc, 'R', 'S');
199 send_pattern(fd, uc, 'R', 'T');
200 send_pattern(fd, uc, 'R', 'U');
201 send_pattern(fd, uc, 'R', 'V');
202 send_pattern(fd, uc, 'R', 'W');
203 send_pattern(fd, uc, 'R', 'X');
204 send_pattern(fd, uc, 'W', 'K');
207 SET_FIRST_BYTE(0x08);
216 send_msg_with_proggress_note(&msg, fd, uc);
220 SET_FIRST_BYTE(0x02);
223 send_msg_with_proggress_note(&msg, fd, uc);
228 send_msg_with_proggress_note(&msg, fd, uc);
233 send_msg_with_proggress_note(&msg, fd, uc);
238 send_msg_with_proggress_note(&msg, fd, uc);
243 send_msg_with_proggress_note(&msg, fd, uc);
246 send_pattern(fd, uc, 'R', 'Z');
249 SET_FIRST_BYTE(0x01);
251 send_msg_with_proggress_note(&msg, fd, uc);
256 send_msg_with_proggress_note(&msg, fd, uc);
261 send_msg_with_proggress_note(&msg, fd, uc);
266 send_msg_with_proggress_note(&msg, fd, uc);
270 send_msg_with_proggress_note(&msg, fd, uc);
275 send_msg_with_proggress_note(&msg, fd, uc);
279 send_msg_with_proggress_note(&msg, fd, uc);
283 send_msg_with_proggress_note(&msg, fd, uc);
292 int contour_read_entry(int fd, int uc, struct msg *in)
298 SET_FIRST_BYTE(0x01);
301 send_msg(&msg, fd, uc);
302 read_and_verify(in, fd);
304 return datalen(in->data);
307 int wait_for_device(int vendor, int product, int *usage_code)
311 fd = hiddev_open_by_id(vendor, product, usage_code);
317 "No suitable device found. Please plug in your glucose meter\n");
320 fd = hiddev_open_by_id(vendor, product, usage_code);