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