2 * (c) 2011 Siegen, Germany by Nils Faerber <nils.faerber@kernelconcepts.de>
20 #include <bluetooth/bluetooth.h>
21 #include <bluetooth/rfcomm.h>
24 #include <dbus/dbus.h>
25 #include <dbus/dbus-glib.h>
26 #include <dbus/dbus-glib-lowlevel.h>
29 #include <bluetooth/sdp.h>
30 #include <bluetooth/sdp_lib.h>
33 #include "metawatch.h"
34 #include "crc16ccitt.h"
35 #include "mw_utility.h"
37 #include "bt_helper.h"
42 unsigned char rcvbuf[128];
44 int con_fd; /* console input fd */
49 void bitmap_test(mwdevice_t *mwdevice)
51 /* a nice checker-board pattern */
52 unsigned char checkbuf[24] = {
53 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
54 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
55 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
56 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
58 mw_write_buffer(mwdevice, MW_SCREEN_MODE_IDLE, 0, 31, checkbuf, 24);
59 mw_write_buffer(mwdevice, MW_SCREEN_MODE_IDLE, 0, 33, checkbuf, 24);
60 mw_write_buffer(mwdevice, MW_SCREEN_MODE_IDLE, 0, 35, checkbuf, 24);
61 mw_write_buffer(mwdevice, MW_SCREEN_MODE_IDLE, 0, 37, checkbuf, 24);
63 mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
66 void bitmap_test2(mwdevice_t *mwdevice)
68 mw_buffer *mwbuf1, *mwbuf2;
72 mwbuf1 = mw_alloc_pbuffer(96, 66, 1);
73 mwbuf2 = mw_alloc_pbuffer(96, 66, 1);
75 mw_buf_clear(mwbuf1, MW_BLACK);
76 mw_buf_clear(mwbuf2, MW_WHITE);
78 for (x=0; x<66; x++) {
79 //mw_buf_clear(mwbuf1, MW_BLACK);
80 mw_buf_draw_pixel(mwbuf1, x, x, MW_WHITE);
81 mw_dump_mw_buffer(mwbuf1);
82 bbuf = mw_make_mw_buffer(mwbuf1, &len);
83 mw_send_bitmap(mwdevice, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
84 mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
88 mw_free_pbuffer(mwbuf1);
89 mw_free_pbuffer(mwbuf2);
92 void text_test(mwdevice_t *mwdevice)
98 mwbuf = mw_alloc_pbuffer(96, 66, 1);
99 mw_buf_clear(mwbuf, MW_BLACK);
101 mw_buf_print(mwbuf, 1, 10, "Font 0", 0, MW_WHITE, MW_BLACK);
102 mw_buf_print(mwbuf, 1, 20, "Font 1", 1, MW_WHITE, MW_BLACK);
103 mw_buf_print(mwbuf, 1, 30, "Font 2", 2, MW_WHITE, MW_BLACK);
104 mw_buf_print(mwbuf, 1, 45, "Big Fat Clipping", 2, MW_BLACK, MW_WHITE);
106 bbuf = mw_make_mw_buffer(mwbuf, &len);
107 mw_send_bitmap(mwdevice, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
108 mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
109 mw_free_pbuffer(mwbuf);
112 void line_test(mwdevice_t *mwdevice)
118 mwbuf = mw_alloc_pbuffer(96, 66, 1);
119 mw_buf_clear(mwbuf, MW_BLACK);
121 for (p=0; p<=96; p+=8)
122 mw_buf_draw_line_bresenham(mwbuf, p, 0, 95-p, 65, MW_WHITE);
124 bbuf = mw_make_mw_buffer(mwbuf, &len);
125 mw_send_bitmap(mwdevice, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
126 mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
127 mw_free_pbuffer(mwbuf);
130 void bitmap_read(mwdevice_t *mwdevice)
134 unsigned int width, height, i;
138 unsigned int rowlength;
139 unsigned char *bmapbuf;
140 // unsigned char mw_buf[24];
142 ffd = open("test.pbm", O_RDONLY);
147 ret = read(ffd, rbuf, 3);
148 if (rbuf[0] != 'P' || rbuf[1] != '4') {
149 fprintf(stderr, "not a PBM file\n");
152 memset(rbuf, 0, 256);
155 ret = read(ffd, (rbuf+i), 1);
156 } while (!isspace(rbuf[i++]));
159 memset(rbuf, 0, 256);
162 ret = read(ffd, (rbuf+i), 1);
163 } while (!isspace(rbuf[i++]));
166 rowlength = ((width / 8) + 1);
168 bmapbuf = malloc(rowlength * height);
170 ret = read(ffd, bmapbuf, rowlength * height);
174 fprintf(stderr, "row length = %d bytes\n", rowlength);
175 fprintf(stderr, "bitmap resolution is %d x %d\n", width, height);
176 fprintf(stderr, "read %d of %d bytes\n", ret, rowlength * height);
177 fprintf(stderr, "\n");
178 for (y=0; y<height; y++) {
179 for (x=0; x<rowlength; x++) {
181 fprintf(stderr, "%c", (bmapbuf[(y*rowlength)+x] & (1<<(7-i))) ? '.' : ' ');
183 fprintf(stderr, "\n");
185 fprintf(stderr, "\n");
188 /* reverse bits and invert the bmap */
189 bmap_buffer_flipinvert(1, 1, bmapbuf, rowlength * height);
190 /* send the buffer to the watch */
191 mw_send_bitmap(mwdevice, MW_SCREEN_MODE_IDLE, width, height, 31, bmapbuf, rowlength * height);
192 /* update the display */
193 mw_update_display(mwdevice, MW_SCREEN_MODE_IDLE, 1);
198 void test_notification(mwdevice_t *mwdevice)
204 mwbuf = mw_alloc_pbuffer(96, 96, 1);
205 mw_buf_clear(mwbuf, MW_BLACK);
207 mw_buf_draw_line_bresenham(mwbuf, 2, 0, 93, 0, MW_WHITE);
208 mw_buf_draw_line_bresenham(mwbuf, 0, 1, 95, 1, MW_WHITE);
209 mw_buf_draw_line_bresenham(mwbuf, 0, 94, 95, 94, MW_WHITE);
210 mw_buf_draw_line_bresenham(mwbuf, 2, 95, 93, 95, MW_WHITE);
212 mw_buf_draw_line_bresenham(mwbuf, 0, 2, 0, 93, MW_WHITE);
213 mw_buf_draw_line_bresenham(mwbuf, 1, 0, 1, 95, MW_WHITE);
214 mw_buf_draw_line_bresenham(mwbuf, 95, 2, 95, 93, MW_WHITE);
215 mw_buf_draw_line_bresenham(mwbuf, 94, 0, 94, 95, MW_WHITE);
217 mw_buf_print(mwbuf, 4, 4, "012345678901234", 0, MW_WHITE, MW_BLACK);
218 mw_buf_print(mwbuf, 4, 13, "012345678901234", 0, MW_WHITE, MW_BLACK);
219 mw_buf_print(mwbuf, 4, 22, "0123456789g1234", 0, MW_WHITE, MW_BLACK);
220 mw_buf_print(mwbuf, 4, 31, "012345678901234", 0, MW_WHITE, MW_BLACK);
221 mw_buf_print(mwbuf, 4, 40, "012345678901234", 0, MW_WHITE, MW_BLACK);
222 mw_buf_print(mwbuf, 4, 49, "012345678901234", 0, MW_WHITE, MW_BLACK);
223 mw_buf_print(mwbuf, 4, 58, "0123456789g1234", 0, MW_WHITE, MW_BLACK);
224 mw_buf_print(mwbuf, 4, 67, "012345678901234", 0, MW_WHITE, MW_BLACK);
225 mw_buf_print(mwbuf, 4, 76, "012345678901234", 0, MW_WHITE, MW_BLACK);
226 mw_buf_print(mwbuf, 4, 85, "012345678901234", 0, MW_WHITE, MW_BLACK);
228 bbuf = mw_make_mw_buffer(mwbuf, &len);
229 mw_send_bitmap(mwdevice, MW_SCREEN_MODE_NOTIFICATION, 96, 96, 0, bbuf, len);
230 mw_update_display(mwdevice, MW_SCREEN_MODE_NOTIFICATION, 1);
231 mw_free_pbuffer(mwbuf);
233 mw_set_vibrate_mode(mwdevice, 1, 300, 300, 3);
236 void mw_send_notify(mwdevice_t *mwdevice, char *header, char *body)
243 mwbuf = mw_alloc_pbuffer(96, 96, 1);
244 mw_buf_clear(mwbuf, MW_BLACK);
246 mw_buf_print(mwbuf, 0, 0, header, 0, MW_BLACK, MW_WHITE);
251 while (i<strlen(body)) {
252 sstr[c++] = body[i++];
253 if (c>=16 || i>=strlen(body)) {
254 mw_buf_print(mwbuf, 0, r*9, sstr, 0, MW_WHITE, MW_BLACK);
262 bbuf = mw_make_mw_buffer(mwbuf, &len);
263 mw_send_bitmap(mwdevice, MW_SCREEN_MODE_APPLICATION, 96, 96, 0, bbuf, len);
264 mw_update_display(mwdevice, MW_SCREEN_MODE_APPLICATION, 1);
265 mw_free_pbuffer(mwbuf);
267 mw_set_vibrate_mode(mwdevice, 1, 300, 300, 2);
270 void test_application(mwdevice_t *mwdevice)
276 mw_enable_button(mwdevice, MW_SCREEN_MODE_APPLICATION, MW_BUTTON_A, MW_BUTTON_IMMEDIATE, MW_BUTTON_EVENT_MESSAGE, MW_BUTTON_A);
277 mw_configure_watch_mode(mwdevice, MW_SCREEN_MODE_APPLICATION, 0, 30, 0);
279 mwbuf = mw_alloc_pbuffer(96, 96, 1);
280 mw_buf_clear(mwbuf, MW_BLACK);
282 mw_buf_print(mwbuf, 0, 0, " Application ", 0, MW_BLACK, MW_WHITE);
283 mw_buf_print(mwbuf, 0, 9, "0123456789012345", 0, MW_WHITE, MW_BLACK);
284 mw_buf_print(mwbuf, 0, 18, "0123456789g12345", 0, MW_WHITE, MW_BLACK);
285 mw_buf_print(mwbuf, 0, 27, "0123456789012345", 0, MW_WHITE, MW_BLACK);
286 mw_buf_print(mwbuf, 0, 36, "0123456789012345", 0, MW_WHITE, MW_BLACK);
287 mw_buf_print(mwbuf, 0, 45, "0123456789012345", 0, MW_WHITE, MW_BLACK);
288 mw_buf_print(mwbuf, 0, 54, "0123456789g12345", 0, MW_WHITE, MW_BLACK);
289 mw_buf_print(mwbuf, 0, 63, "0123456789012345", 0, MW_WHITE, MW_BLACK);
290 mw_buf_print(mwbuf, 0, 72, "0123456789012345", 0, MW_WHITE, MW_BLACK);
291 mw_buf_print(mwbuf, 0, 81, "0123456789012345", 0, MW_WHITE, MW_BLACK);
293 bbuf = mw_make_mw_buffer(mwbuf, &len);
294 mw_send_bitmap(mwdevice, MW_SCREEN_MODE_APPLICATION, 96, 96, 0, bbuf, len);
295 mw_update_display(mwdevice, MW_SCREEN_MODE_APPLICATION, 1);
296 mw_free_pbuffer(mwbuf);
299 void test_oled(mwdevice_t *mwdevice, unsigned char oled)
305 mwbuf = mw_alloc_pbuffer(80, 16, 1);
306 mw_buf_clear(mwbuf, MW_BLACK);
308 mw_buf_print(mwbuf, 0, 0, "Application", 0, MW_WHITE, MW_BLACK);
309 mw_buf_print(mwbuf, 0, 8, "012345678901234", 0, MW_WHITE, MW_BLACK);
311 bbuf = mw_make_mw_oled_buffer(mwbuf, &len);
312 mw_write_oled_buffer(mwdevice, 0, oled, 80, 0, bbuf, len);
314 mw_free_pbuffer(mwbuf);
317 void print_help(void)
321 void process_cmd(char *cmdline, int clinep, mwdata_t *mwdata)
323 unsigned char mdata[32];
326 fprintf(stderr, "command: '%s'\n", cmdline);
328 if (strncmp(cmdline, "quit", 4) == 0) {
331 g_main_loop_quit (mwdata->mloop);
333 if (strncmp(cmdline, "help", 4) == 0) {
336 if (strncmp(cmdline, "ahand", 5) == 0) {
337 intopt = atoi(cmdline+5);
338 g_print("Advance analog hands by %d minutes\n", intopt);
339 mdata[0] = intopt / 60;
340 mdata[1] = intopt % 60;
342 mw_send_frame(&mwdata->mwdevice, MW_ADVANCE_WATCH_HANDS, 0, mdata, 3);
344 if (strncmp(cmdline, "srtc", 4) == 0) {
345 fprintf(stderr, "Setting RTC from system time...");
346 mw_set_rtc(&mwdata->mwdevice, MW_RTC_CLOCK_24HR, MW_RTC_DATE_DDMM);
347 fprintf(stderr, "OK\n");
349 if (strncmp(cmdline, "grtc", 4) == 0) {
350 mw_send_frame(&mwdata->mwdevice, MW_GET_REAL_TIME_CLOCK, 0, NULL, 0);
352 if (strncmp(cmdline, "gistr", 5) == 0) {
353 intopt = cmdline[6]-0x30;
354 if (intopt>=0 && intopt <=3)
358 mw_send_frame(&mwdata->mwdevice, MW_GET_INFORMATION_STRING, 0, mdata, 1);
360 if (strncmp(cmdline, "gdtype", 6) == 0) {
361 mw_send_frame(&mwdata->mwdevice, MW_GET_DEVICE_TYPE, 0, NULL, 0);
363 if (strncmp(cmdline, "rvbat", 5) == 0) {
364 mw_send_frame(&mwdata->mwdevice, MW_READ_BATTERY_VOLTAGE_MSG, 0, NULL, 0);
366 if (strncmp(cmdline, "rlight", 6) == 0) {
367 mw_send_frame(&mwdata->mwdevice, MW_READ_LIGHT_SENSOR_MSG, 0, NULL, 0);
369 if (strncmp(cmdline, "modecfg", 6) == 0) {
370 mw_configure_watch_mode(&mwdata->mwdevice, MW_SCREEN_MODE_IDLE, 0, 4, 1);
371 mw_update_display(&mwdata->mwdevice, MW_SCREEN_MODE_IDLE, 0);
373 if (strncmp(cmdline, "rbtcfg", 6) == 0) {
374 intopt = cmdline[7]-0x30;
375 mdata[0] = 0; /* idle screen */
376 mdata[1] = intopt; /* button index */
377 /* for reading the config those are unnecessary */
378 mdata[2] = 0; /* button press type */
379 mdata[3] = 0; /* callback message type */
380 mdata[4] = 0; /* callback message option */
381 mw_send_frame(&mwdata->mwdevice, MW_READ_BUTTON_CONFIG, 0, mdata, 5);
383 if (strncmp(cmdline, "svib", 4) == 0) {
384 mw_set_vibrate_mode(&mwdata->mwdevice, 1, 300, 300, 5);
386 if (strncmp(cmdline, "tbmp", 4) == 0) {
387 bitmap_test(&mwdata->mwdevice);
389 if (strncmp(cmdline, "t2bmp", 5) == 0) {
390 bitmap_test2(&mwdata->mwdevice);
392 if (strncmp(cmdline, "text", 4) == 0) {
393 text_test(&mwdata->mwdevice);
395 if (strncmp(cmdline, "tline", 5) == 0) {
396 line_test(&mwdata->mwdevice);
398 if (strncmp(cmdline, "rbmp", 4) == 0) {
399 bitmap_read(&mwdata->mwdevice);
401 if (strncmp(cmdline, "tnote", 5) == 0) {
402 test_notification(&mwdata->mwdevice);
404 if (strncmp(cmdline, "tapp", 4) == 0) {
405 test_application(&mwdata->mwdevice);
407 if (strncmp(cmdline, "eoled", 5) == 0) {
408 intopt = cmdline[6]-0x30;
409 if (intopt == MW_OLED_UPPER || intopt == MW_OLED_LOWER)
410 mw_send_frame(&mwdata->mwdevice, MW_ENABLE_OLED_DISPLAY_MSG, intopt, NULL, 0);
412 if (strncmp(cmdline, "toled", 5) == 0) {
413 intopt = cmdline[6]-0x30;
414 test_oled(&mwdata->mwdevice, intopt);
415 //mw_write_oled_buffer(mwdevice, 0, 80, 0, mdata, 10);
416 //mw_send_frame(mwdevice, MW_UPDATE_OLED_DISPLAY_MSG, 0, NULL, 0);
421 int feed_menu(mwdata_t *mdata)
425 rcvd = read(0, (mdata->cmdline+mdata->cmdline_pos), 1);
427 if (mdata->cmdline[mdata->cmdline_pos] == '\r') {
429 mdata->cmdline[mdata->cmdline_pos--] = '\0';
430 process_cmd(mdata->cmdline, mdata->cmdline_pos, mdata);
431 mdata->cmdline_pos = 0;
432 memset(mdata->cmdline, 0, 128);
434 mdata->cmdline_pos++;
435 if (mdata->cmdline_pos > 75)
436 mdata->cmdline_pos = 75;
437 printf("\r> %s", mdata->cmdline);
445 gboolean handle_mw_io(GIOChannel *mw_io, GIOCondition condition, gpointer udata)
447 mwdata_t *mdata = (mwdata_t *)udata;
450 rcvd = read(mdata->mwdevice.mw_fd, mdata->rcvbuf+mdata->rcvbuf_pos, 64);
452 fprintf(stderr, "read %d bytes:\n", rcvd);
456 dump_frame(mdata->rcvbuf, rcvd);
458 decode_frame(&mdata->mwdevice, mdata->rcvbuf, rcvd);
459 mdata->rcvbuf_pos = 0;
465 gboolean handle_min_io(GIOChannel *mw_io, GIOCondition condition, gpointer udata)
467 mwdata_t *mdata = (mwdata_t *)udata;
474 static DBusHandlerResult
475 signal_filter (DBusConnection *connection, DBusMessage *message, void *user_data)
477 //GMainLoop *loop = user_data;
478 mwdata_t *mdata = (mwdata_t *)user_data;
480 char *app_name, *app_icon, *summary, *body; // **actions;
481 int replace_id; // dict, expire;
483 /* A signal from the bus saying we are about to be disconnected */
484 if (dbus_message_is_signal(message, "org.freedesktop.Local", "Disconnected")) {
485 /* Tell the main loop to quit */
486 g_main_loop_quit (mdata->mloop);
487 /* We have handled this message, don't pass it on */
488 return DBUS_HANDLER_RESULT_HANDLED;
491 if (dbus_message_is_method_call (message,"org.freedesktop.Notifications", "Notify")) {
492 g_print("method call\n");
493 dbus_error_init (&error);
494 if (dbus_message_get_args(message, &error,
495 DBUS_TYPE_STRING, &app_name,
496 DBUS_TYPE_UINT32, &replace_id,
497 DBUS_TYPE_STRING, &app_icon,
498 DBUS_TYPE_STRING, &summary,
499 DBUS_TYPE_STRING, &body,
500 /* G_TYPE_STRV, &actions,
501 DBUS_TYPE_DICT_ENTRY_AS_STRING, &dict,
502 DBUS_TYPE_INT32, &expire, */
503 DBUS_TYPE_INVALID)) {
504 g_print("Notify received: from app %s, icon %s:\nSummary: %s\nBody: %s\n", app_name, app_icon, summary, body);
505 mw_send_notify(&mdata->mwdevice, summary, body);
506 /* we just listen, we do not handle it here */
507 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
509 g_print("Notify received, but error getting message: %s\n", error.message);
510 dbus_error_free (&error);
513 g_print("Not signal received\n");
514 return DBUS_HANDLER_RESULT_HANDLED;
517 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
520 int main(int argc, char **argv)
524 struct termios tconfd, otconfd, tmwfd;
526 GIOChannel *mw_io, *m_in;
531 fprintf(stderr, "Usage:\n\t%s <devicename>\n", argv[0]);
536 mdata.mloop = g_main_loop_new (NULL, FALSE);
541 mw_fd = open(argv[1], O_RDWR);
547 if (str2ba(argv[1], &btaddr))
549 mw_fd = open_socket(&btaddr, 1);
553 fprintf(stderr, "connected to %s\n", argv[1]);
556 /* we have a connection, RFCOMM socket is on mw_fd */
557 /* make the tty raw */
558 tcgetattr(mw_fd, &tmwfd);
560 tmwfd.c_oflag |= ONLCR | OPOST;
561 tmwfd.c_lflag |= ISIG;
562 tcsetattr(mw_fd, TCSANOW, &tmwfd);
564 mdata.mwdevice.mw_fd = mw_fd;
565 mdata.rcvbuf_pos = 0;
566 memset(mdata.rcvbuf, 0, 128);
568 dbus_error_init (&error);
569 bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
572 g_warning ("Failed to connect to the D-BUS daemon: %s", error.message);
573 dbus_error_free (&error);
577 dbus_connection_setup_with_g_main (bus, NULL);
578 dbus_bus_add_match (bus, "interface='org.freedesktop.Notifications'", &error);
580 dbus_connection_add_filter (bus, signal_filter, &mdata, NULL);
583 mw_io = g_io_channel_unix_new(mw_fd);
584 g_io_add_watch(mw_io, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, handle_mw_io, &mdata);
586 tcgetattr(0, &tconfd);
587 tcgetattr(0, &otconfd);
589 tconfd.c_oflag |= ONLCR | OPOST;
590 tconfd.c_lflag |= ISIG;
591 tcsetattr(0, TCSANOW, &tconfd);
595 m_in = g_io_channel_unix_new(0); /* stdin for kbd */
596 g_io_add_watch(m_in, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, handle_min_io, &mdata);
598 memset(mdata.cmdline, 0, 128);
599 mdata.cmdline_pos = 0;
601 g_main_loop_run (mdata.mloop);
606 /* reset the controlling tty again */
607 tcsetattr(0, TCSANOW, &otconfd);