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