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");
44 trace(1, "Sending: ");
45 print_hex(3, msg->data + 1, datalen(msg->data) - 1);
46 print_ascii(2, msg->data + 1, datalen(msg->data) - 1);
48 ret = hiddev_write(msg->data, fd, usage_code);
55 static int send_msg_with_proggress_note(const struct msg *msg, int fd,
60 if (trace_level < 1 && msg_count <= MAX_MSGS) {
61 trace(0, "\r%d%%", msg_count * 100 / MAX_MSGS);
67 return send_msg(msg, fd, usage_code);
70 static int read_and_verify(struct msg *msg, int fd)
72 unsigned char buf[64];
76 ret = hiddev_read(buf + offset, sizeof(buf) - offset, fd);
84 memcpy(msg->data, buf, sizeof(buf));
85 memset(&msg->data[sizeof(buf)], 0, sizeof(msg->data)-sizeof(buf));
87 trace(2, "Got data %d: ", datalen(buf));
88 print_hex(3, buf, datalen(buf));
89 print_ascii(2, buf, datalen(buf));
94 static int read_msgs(int fd)
98 read_and_verify(&msg, fd);
99 if (datalen(msg.data) <= 36)
106 static void set_first_msg_byte(struct msg *msg, int *idx, char byte)
108 memset(msg->data, 0, sizeof(msg->data));
109 msg->data[4] = (byte);
114 static void set_msg_byte(struct msg *msg, int *idx, int where, char byte)
116 msg->data[where + 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_msg_byte(&msg, &j, 0x01);
131 send_msg_with_proggress_note(&msg, fd, uc);
135 set_msg_byte(&msg, &j, 1, 0x15);
136 send_msg_with_proggress_note(&msg, fd, uc);
140 set_msg_byte(&msg, &j, 1, 0x05);
141 send_msg_with_proggress_note(&msg, fd, uc);
144 set_first_msg_byte(&msg, &j, 0x02);
147 send_msg_with_proggress_note(&msg, fd, uc);
151 set_msg_byte(&msg, &j, 1, byte2);
152 send_msg_with_proggress_note(&msg, fd, uc);
158 int contour_initialize(int fd, int uc)
165 set_first_msg_byte(&msg, &j, 0x01);
167 send_msg_with_proggress_note(&msg, fd, uc);
170 set_msg_byte(&msg, &j, 1, 0x06);
171 send_msg_with_proggress_note(&msg, fd, uc);
174 set_msg_byte(&msg, &j, 1, 0x15);
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);
186 set_msg_byte(&msg, &j, 1, 0x05);
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_msg_byte(&msg, &j, 0x08);
216 send_msg_with_proggress_note(&msg, fd, uc);
220 set_first_msg_byte(&msg, &j, 0x02);
223 send_msg_with_proggress_note(&msg, fd, uc);
227 set_msg_byte(&msg, &j, 1, 'Y');
228 send_msg_with_proggress_note(&msg, fd, uc);
232 set_msg_byte(&msg, &j, 1, 'W');
233 send_msg_with_proggress_note(&msg, fd, uc);
237 set_msg_byte(&msg, &j, 1, 'K');
238 send_msg_with_proggress_note(&msg, fd, uc);
242 set_msg_byte(&msg, &j, 1, 'C');
243 send_msg_with_proggress_note(&msg, fd, uc);
246 send_pattern(fd, uc, 'R', 'Z');
249 set_first_msg_byte(&msg, &j, 0x01);
251 send_msg_with_proggress_note(&msg, fd, uc);
255 set_msg_byte(&msg, &j, 1, 0x15);
256 send_msg_with_proggress_note(&msg, fd, uc);
260 set_msg_byte(&msg, &j, 1, 0x05);
261 send_msg_with_proggress_note(&msg, fd, uc);
265 set_msg_byte(&msg, &j, 1, 0x04);
266 send_msg_with_proggress_note(&msg, fd, uc);
270 send_msg_with_proggress_note(&msg, fd, uc);
274 set_msg_byte(&msg, &j, 1, 0x06);
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_msg_byte(&msg, &j, 0x01);
299 set_msg_byte(&msg, &j, 1, 0x06);
300 send_msg(&msg, fd, uc);
302 read_and_verify(in, fd);
303 return datalen(in->data);
306 int wait_for_device(int vendor, int product, int *usage_code)
310 fd = hiddev_open_by_id(vendor, product, usage_code);
316 "No suitable device found. Please plug in your glucose meter\n");
319 fd = hiddev_open_by_id(vendor, product, usage_code);