]> git.karo-electronics.de Git - metawatch.git/blob - metawatch.c
Update to spec
[metawatch.git] / metawatch.c
1 /*
2  * (c) 2011 Siegen, Germany by Nils Faerber <nils.faerber@kernelconcepts.de>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <stdio.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
28 #include <termios.h>
29 #include <ctype.h>
30
31 #include "metawatch.h"
32 #include "metawatch_protocol.h"
33 #include "crc16ccitt.h"
34
35
36 #ifdef DEBUG
37 const char *mw_screen_mode_names[] = {
38         "idle screen",
39         "application screen",
40         "notification screen",
41         "scroll"
42 };
43
44 const char *mw_status_string[] = {
45         "Reserved",
46         "Mode Change",
47         "Display Timeout"
48 };
49 #endif
50
51
52 #define MW_FRAME_DELAY          0x00
53
54 /* ----------------------------------------------------------------------
55  * Debugging helpers
56  * ---------------------------------------------------------------------- */
57
58 void dump_frame(unsigned char *frame, int len)
59 {
60         int i;
61
62         for (i=0; i<len; i++)
63                 fprintf(stderr, "0x%02x ", frame[i]);
64         fprintf(stderr, "\n");
65 }
66
67
68 int mw_send_frame(mwdevice_t *mwdevice, unsigned char msg_type, unsigned char options, unsigned char *data, unsigned char len)
69 {
70         unsigned short crc;
71         unsigned char frame[64];
72         int tlen = len + 6; /* payload + 6 bytes frameing */
73         int ret;
74
75         memset(frame, 0, 64);
76         frame[0] = MW_SOF;
77         frame[1] = len + 6;
78         frame[2] = msg_type;
79         frame[3] = options;
80         if (data != NULL && len > 0)
81                 memcpy(frame+4, data, len);
82
83         crc = crc16ccitt(frame, len+4);
84         *(unsigned short *)(frame+len+4) = crc;
85
86 #ifdef DEBUG
87         dump_frame(frame, tlen);
88 #endif
89
90         while (((ret = write(mwdevice->mw_fd, frame, tlen)) >= 0) && (tlen > 0))
91                 tlen -= ret;
92
93         if (MW_FRAME_DELAY)
94                 usleep(MW_FRAME_DELAY);
95
96         if (tlen == 0 && ret >= 0)
97                 return 0;
98         else
99                 return ret;
100 }
101
102
103 /* ----------------------------------------------------------------------
104  * Host to watch commands
105  * ---------------------------------------------------------------------- */
106
107
108 int mw_set_rtc(mwdevice_t *mwdevice, struct tm *mw_tm)
109 {
110         unsigned short year;
111         unsigned char data[32];
112
113         year = mw_tm->tm_year + 1900;  
114         data[0] = (year & 0x0f00) >> 8;
115         data[1] = (year & 0x00ff);
116         data[2] = mw_tm->tm_mon + 1;
117         data[3] = mw_tm->tm_mday;
118         data[4] = mw_tm->tm_wday;
119         data[5] = mw_tm->tm_hour;
120         data[6] = mw_tm->tm_min;
121         data[7] = mw_tm->tm_sec;
122
123         return mw_send_frame(mwdevice, MW_SET_REAL_TIME_CLOCK, 0, data, 8);
124 }
125
126 int mw_nval_operation(mwdevice_t *mwdevice, unsigned char operation, unsigned short identifier, unsigned char size, void *mdata)
127 {
128         unsigned char data[32];
129
130         data[0] = (identifier & 0x00ff);
131         data[1] = (identifier & 0xff00) >> 8;
132         data[2] = size;
133         if (size > 0 && mdata != NULL)
134                 memcpy((data+3), mdata, size);
135
136         if (operation == MW_NVAL_OPERATION_READ || operation == MW_NVAL_OPERATION_WRITE) {
137                 return mw_send_frame(mwdevice, MW_NVAL_OPERATION, operation, data, 3+size);
138         } else
139                 return -1;
140 }
141
142 int mw_set_vibrate_mode(mwdevice_t *mwdevice, unsigned char enable, unsigned short on_time, unsigned short off_time, unsigned char cycles)
143 {
144         unsigned char mdata[7];
145
146         mdata[0] = enable;
147         *(unsigned short *)(mdata+1) = on_time; /* miliseconds */
148         *(unsigned short *)(mdata+3) = off_time; /* miliseconds */
149         mdata[5] = cycles;
150
151         return mw_send_frame(mwdevice, MW_SET_VIBRATE_MODE, 0, mdata, 6);
152 }
153
154 int mw_configure_watch_mode(mwdevice_t *mwdevice, unsigned char mode, unsigned char save, unsigned char timeout, unsigned char invert)
155 {
156         unsigned char mdata[3];
157
158         mdata[0] = timeout; /* seconds */
159         mdata[1] = invert; /* 0=normal, 1=invert */
160
161         return mw_send_frame(mwdevice, MW_CONFIGURE_MODE, (mode & 0x0f) | ((save & 0x01) << 4), mdata, 2);
162 }
163
164 int mw_update_display(mwdevice_t *mwdevice, unsigned char mode, unsigned char copy)
165 {
166         mwdevice->screen_mode = (mode & 0x0f);
167         return mw_send_frame(mwdevice, MW_UPDATE_DISPLAY, (mode & 0x0f) | ((copy & 0x01) << 4), NULL, 0);
168 }
169
170 int mw_load_template(mwdevice_t *mwdevice, unsigned char mode, unsigned char template_select)
171 {
172         return mw_send_frame(mwdevice, MW_LOAD_TEMPLATE, (mode & 0x0f), &template_select, 1);
173 }
174
175 /*
176  * send line for screen-mode mode from *buffer to watch, starting at display row row_offset
177  * this is only for digital LCD watch
178  */
179 int mw_write_buffer(mwdevice_t *mwdevice,
180                 unsigned char mode,
181                 unsigned char numlines,         /* number of lines, 0=two lines or 1=one line */
182                 unsigned char row_offset,       /* start at row_offset in display, e.g. lower part in idle @31 */
183                 unsigned char *buffer, int buflen)
184 {
185         unsigned char mdata[32];
186
187         if (mwdevice->devtype != MW_DEVICE_TYPE_DIGITAL && mwdevice->devtype != MW_DEVICE_TYPE_DEVB_DIGI)
188                 return -1;
189
190         buflen = 12 * (buflen / 12);    /* crop to 12 bytes */
191         if ((numlines == 0 && buflen < 12) || (numlines == 1 && buflen < 24)) {
192                 fprintf(stderr, "mw_write_buffer: bufferlen does not match number of lines\n");
193                 return -1;
194         };
195         memset(mdata, 0, 32);
196         mdata[0] = row_offset;
197         memcpy((mdata+1), buffer, 12);
198         if (numlines == 0) {
199                 mdata[13] = row_offset+1;
200                 memcpy((mdata+14), (buffer+12), 12);
201         };
202
203         return mw_send_frame(mwdevice, MW_WRITE_BUFFER, (mode & 0x0f) | (((numlines & 0x01)<< 4) & 0x10), mdata, numlines ? 13 : 26);
204 }
205
206 /*
207    Options:
208         B0   : row select, 0 first row, 1 second row
209         B1   : display select, 0 upper OLED, 1 lower OLED
210         B2   : if 1 send an event upon completion
211         B3..4: scroll type,
212         B5..7: unused
213    Scroll types:
214         B0: First buffer in a chain of scroll buffers
215         B1: Any buffer except first or last
216         B2: Last buffer of a chain
217         B3: reserved / unused
218    Payload:
219         0: Start index col
220         1..: data
221 */
222 int mw_write_oled_buffer(mwdevice_t *mwdevice,
223         unsigned char mode, /* idle or scroll */
224         unsigned char oled, /* which OLED */
225         unsigned char numcols,
226         unsigned char col_index, /* starting index */
227         unsigned char *buffer, int buflen)
228 {
229         unsigned char mdata[32];
230         int i;
231
232         if (mwdevice->devtype != MW_DEVICE_TYPE_ANA_DIGI && mwdevice->devtype != MW_DEVICE_TYPE_DEVB_ANA_DIGI)
233                 return -1;
234         
235         fprintf(stderr, "write oled buf len = %d\n", buflen);
236         /* lower row first since display wil be updated after completion of upper row */
237         if (buflen > 80) {
238                 for (i=80; i<buflen; i+=20) {
239                         mdata[0] = (i-80);
240                         memcpy((mdata+1), (buffer+i), 20);
241                         mw_send_frame(mwdevice, MW_WRITE_OLED_IDLE_DISPLAY_MSG, 0 | (oled ? 2 : 0), mdata, 21);
242                 }
243         }
244         for (i=0; i<80; i+=20) {
245                 mdata[0] = i;
246                 memcpy((mdata+1), (buffer+i), 20);
247                 mw_send_frame(mwdevice, MW_WRITE_OLED_IDLE_DISPLAY_MSG, 1 | (oled ? 2 : 0), mdata, 21);
248         }
249
250         return 0;
251 }
252
253 int mw_enable_button(mwdevice_t *mwdevice,
254                 unsigned char mode,
255                 unsigned char button_index,
256                 unsigned char press_type,
257                 unsigned char callback_type,
258                 unsigned char callback_option)
259 {
260         unsigned char mdata[32];
261
262         memset(mdata, 0, 32);
263         mdata[0] = mode;
264         mdata[1] = button_index;
265         mdata[2] = press_type;
266         mdata[3] = callback_type;
267         mdata[4] = callback_option;
268
269         return mw_send_frame(mwdevice, MW_ENABLE_BUTTON, 0, mdata, 5);
270 }
271
272 int mw_disable_button(mwdevice_t *mwdevice,
273                 unsigned char mode,
274                 unsigned char button_index,
275                 unsigned char press_type)
276 {
277         unsigned char mdata[32];
278
279         memset(mdata, 0, 32);
280         mdata[0] = mode;
281         mdata[1] = button_index;
282         mdata[2] = press_type;
283
284         return mw_send_frame(mwdevice, MW_ENABLE_BUTTON, 0, mdata, 3);
285 }
286
287 int mw_advance_watch_hands(mwdevice_t *mwdevice, unsigned char hours, unsigned char minutes, unsigned char seconds)
288 {
289         unsigned char mdata[4];
290         
291         if (hours > 12)
292                 return -1;
293         if (minutes > 60)
294                 return -1;
295         if (seconds > 60)
296                 return -1;
297
298         mdata[0] = hours;
299         mdata[1] = minutes;
300         mdata[2] = seconds;
301
302         return mw_send_frame(mwdevice, MW_ADVANCE_WATCH_HANDS, 0, mdata, 3);
303 }
304
305 int mw_battery_configuration(mwdevice_t *mwdevice, unsigned char warn_lvl, unsigned char bt_off_lvl)
306 {
307         unsigned char mdata[4];
308         
309         if (warn_lvl < 28 || warn_lvl > 42)
310                 return -1;
311         if (bt_off_lvl < 28 || bt_off_lvl > 42)
312                 return -1;
313
314         mdata[0] = warn_lvl;
315         mdata[1] = bt_off_lvl;
316
317         return mw_send_frame(mwdevice, MW_BATTERY_CONFIG_MSG, 0, mdata, 2);
318 }
319
320
321 /* ----------------------------------------------------------------------
322  * Watch responses, events or notifications
323  * ---------------------------------------------------------------------- */
324
325 int mw_get_device_type_response(mwdevice_t *mwdevice, unsigned char devtype)
326 {
327 #ifdef DEBUG
328         fprintf(stderr, "Got device type ");
329         switch(devtype) {
330                 case 0:
331                         fprintf(stderr, "Reserved\n");
332                         break;
333                 case 1:
334                         fprintf(stderr, "Ana-Digi\n");
335                         break;
336                 case 2:
337                         fprintf(stderr, "Digital\n");
338                         break;
339                 case 3:
340                         fprintf(stderr, "Development Board Digital\n");
341                         break;
342                 case 4:
343                         fprintf(stderr, "Development Board Ana-Digi\n");
344                         break;
345                 default:
346                         fprintf(stderr, "unknown %d\n", devtype);
347                         break;
348         };
349 #endif
350         mwdevice->devtype = devtype;
351         if (mwdevice->mw_get_device_type_response_cb != NULL)
352                 mwdevice->mw_get_device_type_response_cb(mwdevice, devtype, mwdevice->mw_gdtypersp_data);
353         return 0;
354 }
355
356 void mw_set_get_device_type_response_cb(mwdevice_t *mwdevice, void (*mw_get_device_type_response_cb) (mwdevice_t *mwdevice, unsigned char devtype, void *user_data), void *user_data)
357 {
358         if (mw_get_device_type_response_cb != NULL)
359                 mwdevice->mw_get_device_type_response_cb = mw_get_device_type_response_cb;
360         if (user_data != NULL)
361                 mwdevice->mw_gdtypersp_data = user_data;
362 }
363
364 int mw_nval_operation_response(mwdevice_t *mwdevice, unsigned char operation, unsigned short identifier, unsigned char size, void *mdata)
365 {
366         return -1;
367 }
368
369 int mw_get_real_time_clock_response(mwdevice_t *mwdevice, unsigned char *rtcrsp, int len)
370 {
371         struct tm mtm;
372         unsigned short year;
373
374         if (len != 8) {
375                 fprintf(stderr, "get real time clock response length wrong %d != 10\n", len);
376                 return -1;
377         }
378
379         year = *(unsigned short *)rtcrsp;
380         mtm.tm_year = year - 1900;
381         mtm.tm_mon = rtcrsp[2] - 1;
382         mtm.tm_mday = rtcrsp[3];
383         mtm.tm_wday = rtcrsp[4];
384         mtm.tm_hour = rtcrsp[5];
385         mtm.tm_min = rtcrsp[6];
386         mtm.tm_sec = rtcrsp[7];
387
388 #ifdef DEBUG
389         fprintf(stderr, "watch RTC is %s\n", asctime(&mtm));
390 #endif
391         if (mwdevice->mw_get_real_time_clock_response_cb != NULL)
392                 mwdevice->mw_get_real_time_clock_response_cb(mwdevice, &mtm, mwdevice->mw_grtcrsp_data);
393
394         return 0;
395 }
396
397 void mw_set_get_real_time_clock_response_cb(mwdevice_t *mwdevice, void (*mw_get_real_time_clock_response_cb) (mwdevice_t *mwdevice, struct tm *mw_tm, void *user_data), void *user_data)
398 {
399         if (mw_get_real_time_clock_response_cb != NULL)
400                 mwdevice->mw_get_real_time_clock_response_cb = mw_get_real_time_clock_response_cb;
401         if (user_data != NULL)
402                 mwdevice->mw_grtcrsp_data = user_data;
403 }
404
405
406 int mw_get_battery_voltage_response(mwdevice_t *mwdevice, unsigned char *batrsp, int len)
407 {
408         unsigned char power_good = batrsp[0];
409         unsigned char bat_charging = batrsp[1];
410         unsigned short voltage = *(unsigned short *)(batrsp+2);
411         unsigned short voltage_avg = *(unsigned short *)(batrsp+4);
412
413 #ifdef DEBUG
414         fprintf(stderr, "battery is at %dmV (average is %dmV), %s and %s\n", voltage, voltage_avg, power_good ? "power is good" : "power fault", bat_charging ? "charging" : "not charging");
415 #endif
416
417         if (mwdevice->mw_get_battery_voltage_response_cb != NULL)
418                 mwdevice->mw_get_battery_voltage_response_cb(mwdevice, &voltage, &voltage_avg, &power_good, &bat_charging, mwdevice->mw_gbatvrsp_data);
419
420         return 0;
421 }
422
423 void mw_set_get_battery_voltage_response_cb(mwdevice_t *mwdevice, void (*mw_get_battery_voltage_response_cb) (mwdevice_t *mwdevice, unsigned short *voltage, unsigned short *voltage_avg, unsigned char *pgood, unsigned char *charging, void *user_data), void *user_data)
424 {
425         if (mw_get_battery_voltage_response_cb != NULL)
426                 mwdevice->mw_get_battery_voltage_response_cb = mw_get_battery_voltage_response_cb;
427         if (user_data != NULL)
428                 mwdevice->mw_gbatvrsp_data = user_data;
429 }
430
431 #if 0
432 int mw_read_button_config_response(mwdevice_t *mwdevice, unsigned char *btnrsp, int len)
433 {
434 #ifdef DEBUG
435         fprintf(stderr, "read button config response\n");
436         fprintf(stderr, "screen mode      : 0x%02x\n", btnrsp[0]);
437         fprintf(stderr, "button index     : 0x%02x\n", btnrsp[1]);
438         fprintf(stderr, "mask table       : 0x%02x (", btnrsp[2]);
439         fprintf(stderr, "%s ", (btnrsp[2] & 0x01) ? "Absolute, " : "");
440         fprintf(stderr, "%s ", (btnrsp[2] & 0x02) ? "Press&Release, " : "");
441         fprintf(stderr, "%s ", (btnrsp[2] & 0x04) ? "Press&Hold, " : "");
442         fprintf(stderr, "%s ", (btnrsp[2] & 0x08) ? "Press&LongHold, " : "");
443         fprintf(stderr, "%s ", (btnrsp[2] & 0x10) ? "Immediate" : "");
444         fprintf(stderr, ")\n");
445         fprintf(stderr, "callback msg type: 0x%02x\n", btnrsp[3]);
446         fprintf(stderr, "callback msg opts: 0x%02d\n", btnrsp[4]);
447 #endif
448
449         return 0;
450 }
451 #endif
452
453 void mw_set_read_button_config_response_cb(mwdevice_t *mwdevice, void (*mw_read_button_config_response_cb) (mwdevice_t *mwdevice, void *user_data), void *user_data)
454 {
455         if (mw_read_button_config_response_cb != NULL)
456                 mwdevice->mw_read_button_config_response_cb = mw_read_button_config_response_cb;
457         if (user_data != NULL)
458                 mwdevice->mw_rbtncnfrsp_data = user_data;
459 }
460
461 int mw_button_event_message(mwdevice_t *mwdevice, unsigned char *btnevt, int len, unsigned char opts)
462 {
463 #ifdef DEBUG
464         fprintf(stderr, "Button event message, button %d options %d\n", btnevt[0], opts);
465 #endif
466
467         if (mwdevice->mw_button_event_message_cb != NULL)
468                 mwdevice->mw_button_event_message_cb(mwdevice, btnevt[0], opts, mwdevice->mw_btnevmsg_data);
469
470         return 0;
471 }
472
473 void mw_set_button_event_message_cb(mwdevice_t *mwdevice, void (*mw_button_event_message_cb) (mwdevice_t *mwdevice, unsigned char buttons, unsigned char options, void *user_data), void *user_data)
474 {
475         if (mw_button_event_message_cb != NULL)
476                 mwdevice->mw_button_event_message_cb = mw_button_event_message_cb;
477         if (user_data != NULL)
478                 mwdevice->mw_btnevmsg_data = user_data;
479 }
480
481 int mw_read_light_sensor_response(mwdevice_t *mwdevice, unsigned char *lightrsp, int len)
482 {
483         unsigned short light_level = *(unsigned short *)(lightrsp);
484         unsigned short light_level_avg = *(unsigned short *)(lightrsp+2);
485
486 #ifdef DEBUG
487         fprintf(stderr, "light sensor is at %d, average at %d\n", light_level, light_level_avg);
488 #endif
489
490         if (mwdevice->mw_read_light_sensor_response_cb != NULL)
491                 mwdevice->mw_read_light_sensor_response_cb(mwdevice, &light_level, &light_level_avg, mwdevice->mw_rlsrsp_data);
492
493         return 0;
494 }
495
496 void mw_set_read_light_sensor_response_cb(mwdevice_t *mwdevice, void (*mw_read_light_sensor_response_cb) (mwdevice_t *mwdevice, unsigned short *light_level, unsigned short *light_level_avg, void *user_data), void *user_data)
497 {
498         if (mw_read_light_sensor_response_cb != NULL)
499                 mwdevice->mw_read_light_sensor_response_cb = mw_read_light_sensor_response_cb;
500         if (user_data != NULL)
501                 mwdevice->mw_rlsrsp_data = user_data;
502 }
503
504 int mw_status_change_event(mwdevice_t *mwdevice, unsigned char option, unsigned char *statrsp, int len)
505 {
506         unsigned char mode = (option & 0x0f);
507         unsigned char status = statrsp[0];
508 #ifdef DEBUG
509         fprintf(stderr, "Status change event for mode %s: %s\n", mw_screen_mode_names[mode], mw_status_string[status]); 
510 #endif
511
512         // mwdevice->screen_mode = mode;
513         if (mwdevice->mw_status_change_event_cb != NULL)
514                 mwdevice->mw_status_change_event_cb(mwdevice, &mode, &status, mwdevice->mw_stchev_data);
515
516         return 0;
517 }
518
519 void mw_set_status_change_event_cb(mwdevice_t *mwdevice, void (*mw_status_change_event_cb) (mwdevice_t *mwdevice, unsigned char *scrmode, unsigned char *status, void *user_data), void *user_data)
520 {
521         if (mw_status_change_event_cb != NULL)
522                 mwdevice->mw_status_change_event_cb = mw_status_change_event_cb;
523         if (user_data != NULL)
524                 mwdevice->mw_stchev_data = user_data;
525 }
526
527 int mw_low_battery_warning_message(mwdevice_t *mwdevice)
528 {
529 #ifdef DEBUG
530         fprintf(stderr, "Watch battery low, please connect charger\n");
531 #endif
532         if (mwdevice->mw_low_battery_warning_message_cb != NULL)
533                 mwdevice->mw_low_battery_warning_message_cb(mwdevice, mwdevice->mw_lbatwarnmsg_data);
534
535         return 0;
536 }
537
538 void mw_set_low_battery_warning_message_cb(mwdevice_t *mwdevice, void (*mw_low_battery_warning_message_cb) (mwdevice_t *mwdevice, void *user_data), void *user_data)
539 {
540         if (mw_low_battery_warning_message_cb != NULL)
541                 mwdevice->mw_low_battery_warning_message_cb = mw_low_battery_warning_message_cb;
542         if (user_data != NULL)
543                 mwdevice->mw_lbatwarnmsg_data = user_data;
544 }
545
546 int mw_low_battery_bt_off_message(mwdevice_t *mwdevice)
547 {
548 #ifdef DEBUG
549         fprintf(stderr, "Watch battery extremely low - radio will turn off\n");
550 #endif
551         if (mwdevice->mw_low_battery_bt_off_message_cb != NULL)
552                 mwdevice->mw_low_battery_bt_off_message_cb(mwdevice, mwdevice->mw_lbatbtoff_data);
553
554         return 0;
555 }
556
557 void mw_set_low_battery_bt_off_message_cb(mwdevice_t *mwdevice, void (*mw_low_battery_bt_off_message_cb) (mwdevice_t *mwdevice, void *user_data), void *user_data)
558 {
559         if (mw_low_battery_bt_off_message_cb != NULL)
560                 mwdevice->mw_low_battery_bt_off_message_cb = mw_low_battery_bt_off_message_cb;
561         if (user_data != NULL)
562                 mwdevice->mw_lbatbtoff_data = user_data;
563 }
564
565 /* ----------------------------------------------------------------------
566  * Protocol handling
567  * ---------------------------------------------------------------------- */
568
569 int mw_decode_frame(mwdevice_t *mwdevice, unsigned char *buf, int len)
570 {
571         unsigned short crc;
572         unsigned char msglen;
573         unsigned char msgtype;
574         unsigned char msgopt;
575         unsigned char *msgdata;
576         unsigned char msgdatalen;
577
578         /* check frame */
579         crc = *(unsigned short *)(buf+len-2);
580         if (crc != crc16ccitt(buf, len-2)) {
581                 fprintf(stderr, "decode frame CRC error\n");
582                 return -1;
583         }
584         if (buf[0] != MW_SOF) {
585                 fprintf(stderr, "decode frame SOF not found\n");
586                 return -1;
587         }
588
589         msglen = buf[1];
590         msgtype = buf[2];
591         msgopt = buf[3];
592         msgdata = (buf+4);
593         msgdatalen = msglen - 4 - 2;
594
595         switch (msgtype) {
596                 case MW_GET_DEVICE_TYPE_RSP:
597                         mw_get_device_type_response(mwdevice, msgdata[0]);
598                         break;
599                 case MW_GET_INFORMATION_STRING_RSP:
600                         msgdata[len-2] = 0;
601                         fprintf(stderr, "Got info string '%s'\n", msgdata);
602                         break;
603                 case MW_GET_REAL_TIME_CLOCK_RSP:
604                         mw_get_real_time_clock_response(mwdevice, msgdata, msgdatalen);
605                         break;
606                 case MW_READ_BATTERY_VOLTAGE_RSP:
607                         mw_get_battery_voltage_response(mwdevice, msgdata, msgdatalen);
608                         break;
609                 case MW_READ_LIGHT_SENSOR_RSP:
610                         mw_read_light_sensor_response(mwdevice, msgdata, msgdatalen);
611                         break;
612 #if 0
613                 case MW_READ_BUTTON_CONFIG_RSP:
614                         mw_read_button_config_response(mwdevice, msgdata, msgdatalen);
615                         break;
616 #endif
617                 case MW_BUTTON_EVENT_MESSAGE:
618                         mw_button_event_message(mwdevice, msgdata, msgdatalen, msgopt);
619                         break;
620                 case MW_LOW_BATTERY_WARNING_MSG:
621                         mw_low_battery_warning_message(mwdevice);
622                         break;
623                 case MW_LOW_BATTERY_BT_OFF_MSG:
624                         mw_low_battery_bt_off_message(mwdevice);
625                         break;
626                 case MW_NVAL_OPERATION_RSP:
627                         fprintf(stderr, "NVAL operation response - ");
628                         switch (msgopt) {
629                                 case 0x00:
630                                         fprintf(stderr, "success\n");
631                                         break;
632                                 case 0x01:
633                                         fprintf(stderr, "failure!\n");
634                                         break;
635                                 case 0x09:
636                                         fprintf(stderr, "Item Not initialized (Identifier Not found\n");
637                                         break;
638                                 case 0x0a:
639                                         fprintf(stderr, "Operation failed\n");
640                                         break;
641                                 case 0x0c:
642                                         fprintf(stderr, "Bad Item length\n");
643                                         break;
644                                 default:
645                                         fprintf(stderr, "unknown status 0x%02x\n", msgopt);
646                                         break;
647                         };
648                         break;
649                 case MW_STATUS_CHANGE_EVENT:
650                         mw_status_change_event(mwdevice, msgopt, msgdata, msgdatalen);
651                         break;
652                 default:
653 #ifdef DEBUG
654                         fprintf(stderr, "Unkown msgtype 0x%02x\n", msgtype);
655 #endif
656                         break;
657         };
658
659         return msglen;
660 }
661
662
663 int mw_feed_msg_buffer(mwdevice_t *mwdevice, unsigned char *buf, int len)
664 {
665         char msgbuf[64];
666         int tlen;
667
668         if (len <= 0)
669                 return -1;
670
671         memcpy((mwdevice->pbuf+mwdevice->pbuf_len), buf, len);
672         mwdevice->pbuf_len += len;
673         
674         while (mwdevice->pbuf_len > 0) {
675                 /* scan for MW_SOF */
676                 while (mwdevice->pbuf[0] != MW_SOF) {
677                         memmove(mwdevice->pbuf, (mwdevice->pbuf+1), --mwdevice->pbuf_len);
678                 }
679                 tlen = mwdevice->pbuf[1];
680                 /* OK, there is an SOF but the message is too short */
681                 if (tlen > mwdevice->pbuf_len) {
682                         /* we have to wait for more data to come in */
683                         break;
684                 }
685                 /* here we have a complete msg */
686                 memcpy(msgbuf, mwdevice->pbuf, tlen);
687                 mw_decode_frame(mwdevice, (unsigned char *)msgbuf, tlen);
688                 memmove(mwdevice->pbuf, (mwdevice->pbuf+tlen), tlen);
689                 mwdevice->pbuf_len -= tlen;
690         }
691         return 0;
692 }
693
694
695 /* ----------------------------------------------------------------------
696  * General code usage
697  * ---------------------------------------------------------------------- */
698
699 int mw_init(mwdevice_t *mwdevice, int mw_fd)
700 {
701         memset(mwdevice, 0, sizeof(mwdevice_t));
702         mwdevice->mw_fd = mw_fd;
703         memset(mwdevice->pbuf, 0, MW_PBUF_LEN);
704         mwdevice->pbuf_len = 0;
705
706         /* figure out which device we run with */
707         mw_send_frame(mwdevice, MW_GET_DEVICE_TYPE, 0, NULL, 0);
708
709         return 0;
710 }
711
712 /* ----------------------------------------------------------------------
713  * Convenience functions not strictly part of the protocol
714  * ---------------------------------------------------------------------- */
715
716 int mw_get_resolution(mwdevice_t *mwdevice, unsigned int *width, unsigned int *height)
717 {
718         if (width == NULL || height == NULL)
719                 return -1;
720
721         switch (mwdevice->devtype) {
722                 case MW_DEVICE_TYPE_RESERVED:
723                         return -1;
724                 case MW_DEVICE_TYPE_ANA_DIGI:
725                 case MW_DEVICE_TYPE_DEVB_ANA_DIGI:
726                         *width = 80;
727                         *height = 16;
728                         break;
729                 case MW_DEVICE_TYPE_DIGITAL:
730                 case MW_DEVICE_TYPE_DEVB_DIGI:
731                         *width = 96;
732                         *height = 96;
733                         break;
734                 default:
735                         break;
736         };
737
738         return 0;
739 }
740
741
742 /* if flip=1 bits in each byte are inverted 7->1, 6->2, 5->3,...
743    if invert=1 each byte is inverted
744  */
745 void bmap_buffer_flipinvert(unsigned char flip, unsigned char invert, unsigned char *buf, int len)
746 {
747         int i;
748         unsigned char tmp;
749
750         while (len--) {
751                 tmp = 0;
752                 if (flip) {
753                         for (i=0; i<8; i++)
754                                 tmp |= ((*buf & (1<<i)) >> i) << (7-i);
755                         // fprintf(stderr, "0x%02x -> 0x%02x\n", *buf, tmp);
756                 } else
757                         tmp = *buf;
758                 *buf = invert ? ~tmp : tmp;
759                 buf++;
760         }
761 }
762
763
764 void mw_send_bitmap(mwdevice_t *mwdevice, unsigned char mode, int width, int height, int offset, unsigned char *bmapbuf, int buflen)
765 {
766 #ifdef DEBUG
767         unsigned int i, x;
768 #endif
769         unsigned int y, rowlength;
770         unsigned char mw_buf[24];
771
772         rowlength = (((width-1) / 8) + 1);
773         if ((height + offset) > 96)
774                 height = 96 - offset;
775
776 #ifdef DEBUG
777         fprintf(stderr, "row length = %d bytes\n", rowlength);
778         fprintf(stderr, "bitmap resolution is %d x %d\n", width, height);
779         fprintf(stderr, "\n");
780         for (y=0; y<height; y++) {
781                 for (x=0; x<rowlength; x++) {
782                         for (i=0; i<8; i++)
783                                 fprintf(stderr, "%c", (bmapbuf[(y*rowlength)+x] & (1<<(7-i))) ? '.' : ' ');
784                 }
785                 fprintf(stderr, "\n");
786         }
787         fprintf(stderr, "\n");
788 #endif
789
790         for (y=0; y<height; y+=2) {
791                 memset(mw_buf, 0, 24);
792                 memcpy(mw_buf, (bmapbuf+(y*rowlength)), (rowlength > 12) ? 12 : rowlength);
793                 memcpy((mw_buf+12), (bmapbuf+((y+1)*rowlength)), (rowlength > 12) ? 12 : rowlength);
794                 mw_write_buffer(mwdevice, mode, 0, offset+y, mw_buf, 24);
795         }
796 }
797