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 communicate(int fd, int uc)
144 trace(0, "Initializing..\n");
147 SET_FIRST_BYTE(0x01);
149 send_msg_with_proggress_note(&msg, fd, uc);
153 send_msg_with_proggress_note(&msg, fd, uc);
157 for (i = 0; i < 6; i++) {
159 send_msg_with_proggress_note(&msg, fd, uc);
164 send_msg_with_proggress_note(&msg, fd, uc);
169 send_msg_with_proggress_note(&msg, fd, uc);
172 send_pattern(fd, uc, 'R', 'A');
173 send_pattern(fd, uc, 'R', 'C');
174 send_pattern(fd, uc, 'R', 'D');
175 send_pattern(fd, uc, 'R', 'G');
176 send_pattern(fd, uc, 'R', 'I');
177 send_pattern(fd, uc, 'R', 'M');
178 send_pattern(fd, uc, 'R', 'P');
179 send_pattern(fd, uc, 'R', 'R');
180 send_pattern(fd, uc, 'R', 'S');
181 send_pattern(fd, uc, 'R', 'T');
182 send_pattern(fd, uc, 'R', 'U');
183 send_pattern(fd, uc, 'R', 'V');
184 send_pattern(fd, uc, 'R', 'W');
185 send_pattern(fd, uc, 'R', 'X');
186 send_pattern(fd, uc, 'W', 'K');
189 SET_FIRST_BYTE(0x08);
198 send_msg_with_proggress_note(&msg, fd, uc);
202 SET_FIRST_BYTE(0x02);
205 send_msg_with_proggress_note(&msg, fd, uc);
210 send_msg_with_proggress_note(&msg, fd, uc);
215 send_msg_with_proggress_note(&msg, fd, uc);
220 send_msg_with_proggress_note(&msg, fd, uc);
225 send_msg_with_proggress_note(&msg, fd, uc);
228 send_pattern(fd, uc, 'R', 'Z');
231 SET_FIRST_BYTE(0x01);
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);
248 send_msg_with_proggress_note(&msg, fd, uc);
252 send_msg_with_proggress_note(&msg, fd, uc);
257 send_msg_with_proggress_note(&msg, fd, uc);
261 send_msg_with_proggress_note(&msg, fd, uc);
265 send_msg_with_proggress_note(&msg, fd, uc);
268 trace(0, "\nGlucose readings:\n");
271 send_msg(&msg, fd, uc);
272 read_and_verify(&in, fd);
273 print_ascii(in.data, datalen(in.data));
274 } while (datalen(in.data) > 45);
279 int wait_for_device(int vendor, int product, int *usage_code)
283 fd = hiddev_open_by_id(vendor, product, usage_code);
289 "No suitable device found. Please plug in your glucose meter\n");
292 fd = hiddev_open_by_id(vendor, product, usage_code);