3 #include <linux/types.h>
4 #include <linux/hiddev.h>
10 #include "contour-protocol.h"
15 static int send_msg(const struct msg *msg, int fd, int usage_code)
18 if (msg->direction != OUT) {
19 trace(0, "Message direction is not OUT\n");
24 trace(1, "Sending: ");
26 print_hex(msg->data + 1, datalen(msg->data) - 1);
28 print_ascii(msg->data + 1, datalen(msg->data) - 1);
30 ret = hiddev_write(msg->data, fd, usage_code);
37 static int send_msg_with_proggress_note(const struct msg *msg, int fd,
42 if (trace_level < 1 && msg_count <= MAX_MSGS) {
43 trace(0, "\r%d%%", msg_count * 100 / MAX_MSGS);
49 return send_msg(msg, fd, usage_code);
52 static int read_and_verify(struct msg *msg, int fd)
54 unsigned char buf[64];
58 ret = hiddev_read(buf + offset, sizeof(buf) - offset, fd);
66 memcpy(msg->data, buf, sizeof(buf));
67 trace(2, "Got data %d: ", datalen(buf));
69 print_hex(buf, datalen(buf));
71 print_ascii(buf, datalen(buf));
76 static int read_msgs(int fd)
80 read_and_verify(&msg, fd);
81 if (datalen(msg.data) <= 36)
88 #define SET_FIRST_BYTE(byte) \
90 memset(&msg.data, 0, sizeof(msg.data)); \
91 msg.data[4] = (byte); \
95 #define SET_BYTE(idx, byte) \
97 msg.data[(idx) + 4] = (byte); \
101 #define SET_BYTES(byte) msg.data[j++] = (byte)
103 static int send_pattern(int fd, int uc, unsigned char byte1, unsigned char byte2)
110 SET_FIRST_BYTE(0x01);
112 send_msg_with_proggress_note(&msg, fd, uc);
117 send_msg_with_proggress_note(&msg, fd, uc);
122 send_msg_with_proggress_note(&msg, fd, uc);
125 SET_FIRST_BYTE(0x02);
128 send_msg_with_proggress_note(&msg, fd, uc);
133 send_msg_with_proggress_note(&msg, fd, uc);
139 int contour_initialize(int fd, int uc)
146 SET_FIRST_BYTE(0x01);
148 send_msg_with_proggress_note(&msg, fd, uc);
152 send_msg_with_proggress_note(&msg, fd, uc);
156 for (i = 0; i < 6; i++) {
158 send_msg_with_proggress_note(&msg, fd, uc);
163 send_msg_with_proggress_note(&msg, fd, uc);
168 send_msg_with_proggress_note(&msg, fd, uc);
171 send_pattern(fd, uc, 'R', 'A');
172 send_pattern(fd, uc, 'R', 'C');
173 send_pattern(fd, uc, 'R', 'D');
174 send_pattern(fd, uc, 'R', 'G');
175 send_pattern(fd, uc, 'R', 'I');
176 send_pattern(fd, uc, 'R', 'M');
177 send_pattern(fd, uc, 'R', 'P');
178 send_pattern(fd, uc, 'R', 'R');
179 send_pattern(fd, uc, 'R', 'S');
180 send_pattern(fd, uc, 'R', 'T');
181 send_pattern(fd, uc, 'R', 'U');
182 send_pattern(fd, uc, 'R', 'V');
183 send_pattern(fd, uc, 'R', 'W');
184 send_pattern(fd, uc, 'R', 'X');
185 send_pattern(fd, uc, 'W', 'K');
188 SET_FIRST_BYTE(0x08);
197 send_msg_with_proggress_note(&msg, fd, uc);
201 SET_FIRST_BYTE(0x02);
204 send_msg_with_proggress_note(&msg, fd, uc);
209 send_msg_with_proggress_note(&msg, fd, uc);
214 send_msg_with_proggress_note(&msg, fd, uc);
219 send_msg_with_proggress_note(&msg, fd, uc);
224 send_msg_with_proggress_note(&msg, fd, uc);
227 send_pattern(fd, uc, 'R', 'Z');
230 SET_FIRST_BYTE(0x01);
232 send_msg_with_proggress_note(&msg, fd, uc);
237 send_msg_with_proggress_note(&msg, fd, uc);
242 send_msg_with_proggress_note(&msg, fd, uc);
247 send_msg_with_proggress_note(&msg, fd, uc);
251 send_msg_with_proggress_note(&msg, fd, uc);
256 send_msg_with_proggress_note(&msg, fd, uc);
260 send_msg_with_proggress_note(&msg, fd, uc);
264 send_msg_with_proggress_note(&msg, fd, uc);
273 int contour_read_entry(int fd, int uc, struct msg *in)
279 SET_FIRST_BYTE(0x01);
282 send_msg(&msg, fd, uc);
283 read_and_verify(in, fd);
285 return datalen(in->data);
288 int wait_for_device(int vendor, int product, int *usage_code)
292 fd = hiddev_open_by_id(vendor, product, usage_code);
298 "No suitable device found. Please plug in your glucose meter\n");
301 fd = hiddev_open_by_id(vendor, product, usage_code);