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"
39 int mw_fd; /* MetaWatch RFCOMM BT socket */
40 unsigned char rcvbuf[128];
42 int con_fd; /* console input fd */
47 void bitmap_test(int mw_fd)
49 /* a nice checker-board pattern */
50 unsigned char checkbuf[24] = {
51 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
52 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
53 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
54 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
56 mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 31, checkbuf, 24);
57 mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 33, checkbuf, 24);
58 mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 35, checkbuf, 24);
59 mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 37, checkbuf, 24);
61 mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
64 void bitmap_test2(int mw_fd)
66 mw_buffer *mwbuf1, *mwbuf2;
70 mwbuf1 = mw_alloc_pbuffer(96, 66, 1);
71 mwbuf2 = mw_alloc_pbuffer(96, 66, 1);
73 mw_buf_clear(mwbuf1, MW_BLACK);
74 mw_buf_clear(mwbuf2, MW_WHITE);
76 for (x=0; x<66; x++) {
77 //mw_buf_clear(mwbuf1, MW_BLACK);
78 mw_buf_draw_pixel(mwbuf1, x, x, MW_WHITE);
79 mw_dump_mw_buffer(mwbuf1);
80 bbuf = mw_make_mw_buffer(mwbuf1, &len);
81 mw_send_bitmap(mw_fd, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
82 mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
86 mw_free_pbuffer(mwbuf1);
87 mw_free_pbuffer(mwbuf2);
90 void text_test(int mw_fd)
96 mwbuf = mw_alloc_pbuffer(96, 66, 1);
97 mw_buf_clear(mwbuf, MW_BLACK);
99 mw_buf_print(mwbuf, 1, 10, "Font 0", 0, MW_WHITE, MW_BLACK);
100 mw_buf_print(mwbuf, 1, 20, "Font 1", 1, MW_WHITE, MW_BLACK);
101 mw_buf_print(mwbuf, 1, 30, "Font 2", 2, MW_WHITE, MW_BLACK);
102 mw_buf_print(mwbuf, 1, 45, "Big Fat Clipping", 2, MW_BLACK, MW_WHITE);
104 bbuf = mw_make_mw_buffer(mwbuf, &len);
105 mw_send_bitmap(mw_fd, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
106 mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
107 mw_free_pbuffer(mwbuf);
110 void line_test(int mw_fd)
116 mwbuf = mw_alloc_pbuffer(96, 66, 1);
117 mw_buf_clear(mwbuf, MW_BLACK);
119 for (p=0; p<=96; p+=8)
120 mw_buf_draw_line_bresenham(mwbuf, p, 0, 95-p, 65, MW_WHITE);
122 bbuf = mw_make_mw_buffer(mwbuf, &len);
123 mw_send_bitmap(mw_fd, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
124 mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
125 mw_free_pbuffer(mwbuf);
128 void bitmap_read(int mw_fd)
132 unsigned int width, height, i;
136 unsigned int rowlength;
137 unsigned char *bmapbuf;
138 // unsigned char mw_buf[24];
140 ffd = open("test.pbm", O_RDONLY);
145 ret = read(ffd, rbuf, 3);
146 if (rbuf[0] != 'P' || rbuf[1] != '4') {
147 fprintf(stderr, "not a PBM file\n");
150 memset(rbuf, 0, 256);
153 ret = read(ffd, (rbuf+i), 1);
154 } while (!isspace(rbuf[i++]));
157 memset(rbuf, 0, 256);
160 ret = read(ffd, (rbuf+i), 1);
161 } while (!isspace(rbuf[i++]));
164 rowlength = ((width / 8) + 1);
166 bmapbuf = malloc(rowlength * height);
168 ret = read(ffd, bmapbuf, rowlength * height);
172 fprintf(stderr, "row length = %d bytes\n", rowlength);
173 fprintf(stderr, "bitmap resolution is %d x %d\n", width, height);
174 fprintf(stderr, "read %d of %d bytes\n", ret, rowlength * height);
175 fprintf(stderr, "\n");
176 for (y=0; y<height; y++) {
177 for (x=0; x<rowlength; x++) {
179 fprintf(stderr, "%c", (bmapbuf[(y*rowlength)+x] & (1<<(7-i))) ? '.' : ' ');
181 fprintf(stderr, "\n");
183 fprintf(stderr, "\n");
186 /* reverse bits and invert the bmap */
187 bmap_buffer_flipinvert(1, 1, bmapbuf, rowlength * height);
188 /* send the buffer to the watch */
189 mw_send_bitmap(mw_fd, MW_SCREEN_MODE_IDLE, width, height, 31, bmapbuf, rowlength * height);
190 /* update the display */
191 mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
196 void test_notification(int mw_fd)
202 mwbuf = mw_alloc_pbuffer(96, 96, 1);
203 mw_buf_clear(mwbuf, MW_BLACK);
205 mw_buf_draw_line_bresenham(mwbuf, 2, 0, 93, 0, MW_WHITE);
206 mw_buf_draw_line_bresenham(mwbuf, 0, 1, 95, 1, MW_WHITE);
207 mw_buf_draw_line_bresenham(mwbuf, 0, 94, 95, 94, MW_WHITE);
208 mw_buf_draw_line_bresenham(mwbuf, 2, 95, 93, 95, MW_WHITE);
210 mw_buf_draw_line_bresenham(mwbuf, 0, 2, 0, 93, MW_WHITE);
211 mw_buf_draw_line_bresenham(mwbuf, 1, 0, 1, 95, MW_WHITE);
212 mw_buf_draw_line_bresenham(mwbuf, 95, 2, 95, 93, MW_WHITE);
213 mw_buf_draw_line_bresenham(mwbuf, 94, 0, 94, 95, MW_WHITE);
215 mw_buf_print(mwbuf, 4, 4, "012345678901234", 0, MW_WHITE, MW_BLACK);
216 mw_buf_print(mwbuf, 4, 13, "012345678901234", 0, MW_WHITE, MW_BLACK);
217 mw_buf_print(mwbuf, 4, 22, "0123456789g1234", 0, MW_WHITE, MW_BLACK);
218 mw_buf_print(mwbuf, 4, 31, "012345678901234", 0, MW_WHITE, MW_BLACK);
219 mw_buf_print(mwbuf, 4, 40, "012345678901234", 0, MW_WHITE, MW_BLACK);
220 mw_buf_print(mwbuf, 4, 49, "012345678901234", 0, MW_WHITE, MW_BLACK);
221 mw_buf_print(mwbuf, 4, 58, "0123456789g1234", 0, MW_WHITE, MW_BLACK);
222 mw_buf_print(mwbuf, 4, 67, "012345678901234", 0, MW_WHITE, MW_BLACK);
223 mw_buf_print(mwbuf, 4, 76, "012345678901234", 0, MW_WHITE, MW_BLACK);
224 mw_buf_print(mwbuf, 4, 85, "012345678901234", 0, MW_WHITE, MW_BLACK);
226 bbuf = mw_make_mw_buffer(mwbuf, &len);
227 mw_send_bitmap(mw_fd, MW_SCREEN_MODE_NOTIFICATION, 96, 96, 0, bbuf, len);
228 mw_update_display(mw_fd, MW_SCREEN_MODE_NOTIFICATION, 1);
229 mw_free_pbuffer(mwbuf);
231 mw_set_vibrate_mode(mw_fd, 1, 300, 300, 3);
234 void mw_send_notify(int mw_fd, char *header, char *body)
241 mwbuf = mw_alloc_pbuffer(96, 96, 1);
242 mw_buf_clear(mwbuf, MW_BLACK);
244 mw_buf_print(mwbuf, 0, 0, header, 0, MW_BLACK, MW_WHITE);
249 while (i<strlen(body)) {
250 sstr[c++] = body[i++];
251 if (c>=16 || i>=strlen(body)) {
252 mw_buf_print(mwbuf, 0, r*9, sstr, 0, MW_WHITE, MW_BLACK);
260 bbuf = mw_make_mw_buffer(mwbuf, &len);
261 mw_send_bitmap(mw_fd, MW_SCREEN_MODE_APPLICATION, 96, 96, 0, bbuf, len);
262 mw_update_display(mw_fd, MW_SCREEN_MODE_APPLICATION, 1);
263 mw_free_pbuffer(mwbuf);
265 mw_set_vibrate_mode(mw_fd, 1, 300, 300, 2);
268 void test_application(int mw_fd)
274 mw_enable_button(mw_fd, MW_SCREEN_MODE_APPLICATION, MW_BUTTON_A, MW_BUTTON_IMMEDIATE, MW_BUTTON_EVENT_MESSAGE, MW_BUTTON_A);
275 mw_configure_watch_mode(mw_fd, MW_SCREEN_MODE_APPLICATION, 0, 30, 0);
277 mwbuf = mw_alloc_pbuffer(96, 96, 1);
278 mw_buf_clear(mwbuf, MW_BLACK);
280 mw_buf_print(mwbuf, 0, 0, " Application ", 0, MW_BLACK, MW_WHITE);
281 mw_buf_print(mwbuf, 0, 9, "0123456789012345", 0, MW_WHITE, MW_BLACK);
282 mw_buf_print(mwbuf, 0, 18, "0123456789g12345", 0, MW_WHITE, MW_BLACK);
283 mw_buf_print(mwbuf, 0, 27, "0123456789012345", 0, MW_WHITE, MW_BLACK);
284 mw_buf_print(mwbuf, 0, 36, "0123456789012345", 0, MW_WHITE, MW_BLACK);
285 mw_buf_print(mwbuf, 0, 45, "0123456789012345", 0, MW_WHITE, MW_BLACK);
286 mw_buf_print(mwbuf, 0, 54, "0123456789g12345", 0, MW_WHITE, MW_BLACK);
287 mw_buf_print(mwbuf, 0, 63, "0123456789012345", 0, MW_WHITE, MW_BLACK);
288 mw_buf_print(mwbuf, 0, 72, "0123456789012345", 0, MW_WHITE, MW_BLACK);
289 mw_buf_print(mwbuf, 0, 81, "0123456789012345", 0, MW_WHITE, MW_BLACK);
291 bbuf = mw_make_mw_buffer(mwbuf, &len);
292 mw_send_bitmap(mw_fd, MW_SCREEN_MODE_APPLICATION, 96, 96, 0, bbuf, len);
293 mw_update_display(mw_fd, MW_SCREEN_MODE_APPLICATION, 1);
294 mw_free_pbuffer(mwbuf);
297 void print_help(void)
301 void process_cmd(char *cmdline, int clinep, int mw_fd)
303 unsigned char mdata[32];
306 fprintf(stderr, "command: '%s'\n", cmdline);
308 if (strncmp(cmdline, "quit", 4) == 0) {
311 // g_main_loop_quit (mdata->mloop);
313 if (strncmp(cmdline, "help", 4) == 0) {
316 if (strncmp(cmdline, "srtc", 4) == 0) {
317 fprintf(stderr, "Setting RTC from system time...");
318 mw_set_rtc(mw_fd, MW_RTC_CLOCK_24HR, MW_RTC_DATE_DDMM);
319 fprintf(stderr, "OK\n");
321 if (strncmp(cmdline, "grtc", 4) == 0) {
322 mw_send_frame(mw_fd, MW_GET_REAL_TIME_CLOCK, 0, NULL, 0);
324 if (strncmp(cmdline, "gistr", 5) == 0) {
325 intopt = cmdline[6]-0x30;
326 if (intopt>=0 && intopt <=3)
330 mw_send_frame(mw_fd, MW_GET_INFORMATION_STRING, 0, mdata, 1);
332 if (strncmp(cmdline, "gdtype", 6) == 0) {
333 mw_send_frame(mw_fd, MW_GET_DEVICE_TYPE, 0, NULL, 0);
335 if (strncmp(cmdline, "rvbat", 5) == 0) {
336 mw_send_frame(mw_fd, MW_READ_BATTERY_VOLTAGE_MSG, 0, NULL, 0);
338 if (strncmp(cmdline, "rlight", 6) == 0) {
339 mw_send_frame(mw_fd, MW_READ_LIGHT_SENSOR_MSG, 0, NULL, 0);
341 if (strncmp(cmdline, "modecfg", 6) == 0) {
342 mw_configure_watch_mode(mw_fd, MW_SCREEN_MODE_IDLE, 0, 4, 1);
343 mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 0);
345 if (strncmp(cmdline, "rbtcfg", 6) == 0) {
346 intopt = cmdline[7]-0x30;
347 mdata[0] = 0; /* idle screen */
348 mdata[1] = intopt; /* button index */
349 /* for reading the config those are unnecessary */
350 mdata[2] = 0; /* button press type */
351 mdata[3] = 0; /* callback message type */
352 mdata[4] = 0; /* callback message option */
353 mw_send_frame(mw_fd, MW_READ_BUTTON_CONFIG, 0, mdata, 5);
355 if (strncmp(cmdline, "svib", 4) == 0) {
356 mw_set_vibrate_mode(mw_fd, 1, 300, 300, 5);
358 if (strncmp(cmdline, "tbmp", 4) == 0) {
361 if (strncmp(cmdline, "t2bmp", 5) == 0) {
364 if (strncmp(cmdline, "text", 4) == 0) {
367 if (strncmp(cmdline, "tline", 5) == 0) {
370 if (strncmp(cmdline, "rbmp", 4) == 0) {
373 if (strncmp(cmdline, "tnote", 5) == 0) {
374 test_notification(mw_fd);
376 if (strncmp(cmdline, "tapp", 4) == 0) {
377 test_application(mw_fd);
382 int feed_menu(mwdata *mdata)
386 rcvd = read(0, (mdata->cmdline+mdata->cmdline_pos), 1);
388 if (mdata->cmdline[mdata->cmdline_pos] == '\r') {
390 mdata->cmdline[mdata->cmdline_pos--] = '\0';
391 process_cmd(mdata->cmdline, mdata->cmdline_pos, mdata->mw_fd);
392 mdata->cmdline_pos = 0;
393 memset(mdata->cmdline, 0, 128);
395 mdata->cmdline_pos++;
396 if (mdata->cmdline_pos > 75)
397 mdata->cmdline_pos = 75;
398 printf("\r> %s", mdata->cmdline);
405 int open_socket(bdaddr_t *bdaddr, uint8_t channel)
407 struct sockaddr_rc addr;
411 sk = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
413 fprintf(stderr, "Can't create socket: %s (%d)\n",
414 strerror(errno), errno);
420 if (setsockopt(sk, SOL_BLUETOOTH, BT_FLUSHABLE, &f, sizeof(f)) < 0) {
421 fprintf(stderr, "Can't set flushable: %s (%d)\n",
422 strerror(errno), errno);
426 memset(&addr, 0, sizeof(addr));
427 addr.rc_family = AF_BLUETOOTH;
428 bacpy(&addr.rc_bdaddr, BDADDR_ANY);
430 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
431 fprintf(stderr, "Can't bind socket: %s (%d)\n",
432 strerror(errno), errno);
439 opt |= RFCOMM_LM_MASTER;
440 opt |= RFCOMM_LM_AUTH;
442 opt |= RFCOMM_LM_ENCRYPT;
443 opt |= RFCOMM_LM_SECURE;
445 if (opt && setsockopt(sk, SOL_RFCOMM, RFCOMM_LM, &opt, sizeof(opt)) < 0) {
446 fprintf(stderr, "Can't set RFCOMM link mode: %s (%d)",
447 strerror(errno), errno);
452 memset(&addr, 0, sizeof(addr));
453 addr.rc_family = AF_BLUETOOTH;
454 bacpy(&addr.rc_bdaddr, bdaddr);
455 addr.rc_channel = channel;
457 if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
458 fprintf(stderr, "Can't connect: %s (%d)\n",
459 strerror(errno), errno);
467 void baswap(bdaddr_t *dst, const bdaddr_t *src)
469 register unsigned char *d = (unsigned char *) dst;
470 register const unsigned char *s = (const unsigned char *) src;
473 for (i = 0; i < 6; i++)
477 int bachk(const char *str)
482 if (strlen(str) != 17)
486 if (!isxdigit(*str++))
489 if (!isxdigit(*str++))
502 int str2ba(const char *str, bdaddr_t *ba)
507 if (bachk(str) < 0) {
508 memset(ba, 0, sizeof(*ba));
512 for (i = 0; i < 6; i++, str += 3)
513 b.b[i] = strtol(str, NULL, 16);
520 gboolean handle_mw_io(GIOChannel *mw_io, GIOCondition condition, gpointer udata)
522 mwdata *mdata = (mwdata *)udata;
525 rcvd = read(mdata->mw_fd, mdata->rcvbuf+mdata->rcvbuf_pos, 64);
527 fprintf(stderr, "read %d bytes:\n", rcvd);
531 dump_frame(mdata->rcvbuf, rcvd);
533 decode_frame(mdata->mw_fd, mdata->rcvbuf, rcvd);
534 mdata->rcvbuf_pos = 0;
540 gboolean handle_min_io(GIOChannel *mw_io, GIOCondition condition, gpointer udata)
542 mwdata *mdata = (mwdata *)udata;
549 static DBusHandlerResult
550 signal_filter (DBusConnection *connection, DBusMessage *message, void *user_data)
552 //GMainLoop *loop = user_data;
553 mwdata *mdata = (mwdata *)user_data;
555 char *app_name, *app_icon, *summary, *body; // **actions;
556 int replace_id; // dict, expire;
558 /* A signal from the bus saying we are about to be disconnected */
559 if (dbus_message_is_signal(message, "org.freedesktop.Local", "Disconnected")) {
560 /* Tell the main loop to quit */
561 g_main_loop_quit (mdata->mloop);
562 /* We have handled this message, don't pass it on */
563 return DBUS_HANDLER_RESULT_HANDLED;
566 if (dbus_message_is_method_call (message,"org.freedesktop.Notifications", "Notify")) {
567 g_print("method call\n");
568 dbus_error_init (&error);
569 if (dbus_message_get_args(message, &error,
570 DBUS_TYPE_STRING, &app_name,
571 DBUS_TYPE_UINT32, &replace_id,
572 DBUS_TYPE_STRING, &app_icon,
573 DBUS_TYPE_STRING, &summary,
574 DBUS_TYPE_STRING, &body,
575 /* G_TYPE_STRV, &actions,
576 DBUS_TYPE_DICT_ENTRY_AS_STRING, &dict,
577 DBUS_TYPE_INT32, &expire, */
578 DBUS_TYPE_INVALID)) {
579 g_print("Notify received: from app %s, icon %s:\nSummary: %s\nBody: %s\n", app_name, app_icon, summary, body);
580 mw_send_notify(mdata->mw_fd, summary, body);
581 /* we just listen, we do not handle it here */
582 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
584 g_print("Notify received, but error getting message: %s\n", error.message);
585 dbus_error_free (&error);
588 g_print("Not signal received\n");
589 return DBUS_HANDLER_RESULT_HANDLED;
592 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
595 int main(int argc, char **argv)
599 struct termios tconfd, otconfd, tmwfd;
601 GIOChannel *mw_io, *m_in;
606 fprintf(stderr, "Usage:\n\t%s <devicename>\n", argv[0]);
611 mdata.mloop = g_main_loop_new (NULL, FALSE);
616 mw_fd = open(argv[1], O_RDWR);
622 if (str2ba(argv[1], &btaddr))
624 mw_fd = open_socket(&btaddr, 1);
628 fprintf(stderr, "connected to %s\n", argv[1]);
631 /* we have a connection, RFCOMM socket is on mw_fd */
632 /* make the tty raw */
633 tcgetattr(mw_fd, &tmwfd);
635 tmwfd.c_oflag |= ONLCR | OPOST;
636 tmwfd.c_lflag |= ISIG;
637 tcsetattr(mw_fd, TCSANOW, &tmwfd);
640 mdata.rcvbuf_pos = 0;
641 memset(mdata.rcvbuf, 0, 128);
643 dbus_error_init (&error);
644 bus = dbus_bus_get (DBUS_BUS_SESSION, &error);
647 g_warning ("Failed to connect to the D-BUS daemon: %s", error.message);
648 dbus_error_free (&error);
652 dbus_connection_setup_with_g_main (bus, NULL);
653 dbus_bus_add_match (bus, "interface='org.freedesktop.Notifications'", &error);
655 dbus_connection_add_filter (bus, signal_filter, &mdata, NULL);
658 mw_io = g_io_channel_unix_new(mw_fd);
659 g_io_add_watch(mw_io, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, handle_mw_io, &mdata);
661 tcgetattr(0, &tconfd);
662 tcgetattr(0, &otconfd);
664 tconfd.c_oflag |= ONLCR | OPOST;
665 tconfd.c_lflag |= ISIG;
666 tcsetattr(0, TCSANOW, &tconfd);
670 m_in = g_io_channel_unix_new(0); /* stdin for kbd */
671 g_io_add_watch(m_in, G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP, handle_min_io, &mdata);
673 memset(mdata.cmdline, 0, 128);
674 mdata.cmdline_pos = 0;
676 g_main_loop_run (mdata.mloop);
681 /* reset the controlling tty again */
682 tcsetattr(0, TCSANOW, &otconfd);