2 * (c) 2011 Siegen, Germany by Nils Faerber <nils.faerber@kernelconcepts.de>
20 #include <bluetooth/bluetooth.h>
21 #include <bluetooth/rfcomm.h>
24 #include <bluetooth/sdp.h>
25 #include <bluetooth/sdp_lib.h>
28 #include "metawatch.h"
29 #include "crc16ccitt.h"
30 #include "mw_utility.h"
33 void bitmap_test(int mw_fd)
35 /* a nice checker-board pattern */
36 unsigned char checkbuf[24] = {
37 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
38 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
39 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
40 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
42 mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 31, checkbuf, 24);
43 mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 33, checkbuf, 24);
44 mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 35, checkbuf, 24);
45 mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 37, checkbuf, 24);
47 mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
50 void bitmap_test2(int mw_fd)
52 mw_buffer *mwbuf1, *mwbuf2;
56 mwbuf1 = mw_alloc_pbuffer(96, 66, 1);
57 mwbuf2 = mw_alloc_pbuffer(96, 66, 1);
59 mw_buf_clear(mwbuf1, MW_BLACK);
60 mw_buf_clear(mwbuf2, MW_WHITE);
62 for (x=0; x<66; x++) {
63 //mw_buf_clear(mwbuf1, MW_BLACK);
64 mw_buf_draw_pixel(mwbuf1, x, x, MW_WHITE);
65 mw_dump_mw_buffer(mwbuf1);
66 bbuf = mw_make_mw_buffer(mwbuf1, &len);
67 mw_send_bitmap(mw_fd, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
68 mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
72 mw_free_pbuffer(mwbuf1);
73 mw_free_pbuffer(mwbuf2);
76 void text_test(int mw_fd)
82 mwbuf = mw_alloc_pbuffer(96, 66, 1);
83 mw_buf_clear(mwbuf, MW_BLACK);
85 mw_buf_print(mwbuf, 1, 10, "Font 0", 0, MW_WHITE, MW_BLACK);
86 mw_buf_print(mwbuf, 1, 20, "Font 1", 1, MW_WHITE, MW_BLACK);
87 mw_buf_print(mwbuf, 1, 30, "Font 2", 2, MW_WHITE, MW_BLACK);
88 mw_buf_print(mwbuf, 1, 45, "Big Fat Clipping", 2, MW_BLACK, MW_WHITE);
90 bbuf = mw_make_mw_buffer(mwbuf, &len);
91 mw_send_bitmap(mw_fd, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
92 mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
93 mw_free_pbuffer(mwbuf);
96 void line_test(int mw_fd)
102 mwbuf = mw_alloc_pbuffer(96, 66, 1);
103 mw_buf_clear(mwbuf, MW_BLACK);
105 for (p=0; p<=96; p+=8)
106 mw_buf_draw_line_bresenham(mwbuf, p, 0, 95-p, 65, MW_WHITE);
108 bbuf = mw_make_mw_buffer(mwbuf, &len);
109 mw_send_bitmap(mw_fd, MW_SCREEN_MODE_IDLE, 96, 65, 31, bbuf, len);
110 mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
111 mw_free_pbuffer(mwbuf);
114 void bitmap_read(int mw_fd)
118 unsigned int width, height, i;
122 unsigned int rowlength;
123 unsigned char *bmapbuf;
124 // unsigned char mw_buf[24];
126 ffd = open("test.pbm", O_RDONLY);
131 ret = read(ffd, rbuf, 3);
132 if (rbuf[0] != 'P' || rbuf[1] != '4') {
133 fprintf(stderr, "not a PBM file\n");
136 memset(rbuf, 0, 256);
139 ret = read(ffd, (rbuf+i), 1);
140 } while (!isspace(rbuf[i++]));
143 memset(rbuf, 0, 256);
146 ret = read(ffd, (rbuf+i), 1);
147 } while (!isspace(rbuf[i++]));
150 rowlength = ((width / 8) + 1);
152 bmapbuf = malloc(rowlength * height);
154 ret = read(ffd, bmapbuf, rowlength * height);
158 fprintf(stderr, "row length = %d bytes\n", rowlength);
159 fprintf(stderr, "bitmap resolution is %d x %d\n", width, height);
160 fprintf(stderr, "read %d of %d bytes\n", ret, rowlength * height);
161 fprintf(stderr, "\n");
162 for (y=0; y<height; y++) {
163 for (x=0; x<rowlength; x++) {
165 fprintf(stderr, "%c", (bmapbuf[(y*rowlength)+x] & (1<<(7-i))) ? '.' : ' ');
167 fprintf(stderr, "\n");
169 fprintf(stderr, "\n");
172 /* reverse bits and invert the bmap */
173 bmap_buffer_flipinvert(1, 1, bmapbuf, rowlength * height);
174 /* send the buffer to the watch */
175 mw_send_bitmap(mw_fd, MW_SCREEN_MODE_IDLE, width, height, 31, bmapbuf, rowlength * height);
176 /* update the display */
177 mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
182 void print_help(void)
186 void process_cmd(char *cmdline, int clinep, int mw_fd)
188 unsigned char mdata[32];
190 fprintf(stderr, "command: '%s'\n", cmdline);
192 if (strncmp(cmdline, "quit", 4) == 0) {
196 if (strncmp(cmdline, "help", 4) == 0) {
199 if (strncmp(cmdline, "srtc", 4) == 0) {
200 fprintf(stderr, "Setting RTC from system time...");
201 mw_set_rtc(mw_fd, MW_RTC_CLOCK_24HR, MW_RTC_DATE_DDMM);
202 fprintf(stderr, "OK\n");
204 if (strncmp(cmdline, "grtc", 4) == 0) {
205 mw_send_frame(mw_fd, MW_GET_REAL_TIME_CLOCK, 0, NULL, 0);
207 if (strncmp(cmdline, "gistr", 5) == 0) {
208 mw_send_frame(mw_fd, MW_GET_INFORMATION_STRING, 0, NULL, 0);
210 if (strncmp(cmdline, "gdtype", 6) == 0) {
211 mw_send_frame(mw_fd, MW_GET_DEVICE_TYPE, 0, NULL, 0);
213 if (strncmp(cmdline, "rvbat", 5) == 0) {
214 mw_send_frame(mw_fd, MW_READ_BATTERY_VOLTAGE_MSG, 0, NULL, 0);
216 if (strncmp(cmdline, "rlight", 6) == 0) {
217 mw_send_frame(mw_fd, MW_READ_LIGHT_SENSOR_MSG, 0, NULL, 0);
219 if (strncmp(cmdline, "modecfg", 6) == 0) {
220 mw_configure_watch_mode(mw_fd, MW_SCREEN_MODE_IDLE, 0, 4, 1);
221 mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 0);
223 if (strncmp(cmdline, "rbtcfg", 6) == 0) {
224 mdata[0] = 0; /* idle screen */
225 mdata[1] = 1; /* button index */
226 mdata[2] = 2; /* button press type */
227 mdata[3] = 3; /* callback message type */
228 mdata[4] = 4; /* callback message option */
229 mw_send_frame(mw_fd, MW_READ_BUTTON_CONFIG, 0, NULL, 0);
231 if (strncmp(cmdline, "svib", 4) == 0) {
232 mw_set_vibrate_mode(mw_fd, 1, 300, 300, 5);
234 if (strncmp(cmdline, "tbmp", 4) == 0) {
237 if (strncmp(cmdline, "t2bmp", 5) == 0) {
240 if (strncmp(cmdline, "text", 4) == 0) {
243 if (strncmp(cmdline, "tline", 5) == 0) {
246 if (strncmp(cmdline, "rbmp", 4) == 0) {
255 struct termios tconfd, tmwfd;
257 unsigned char msg_buf[64];
258 unsigned char clinep = 0;
261 tcgetattr(0, &tconfd);
263 tconfd.c_oflag |= ONLCR | OPOST;
264 tconfd.c_lflag |= ISIG;
265 tcsetattr(0, TCSANOW, &tconfd);
267 tcgetattr(mw_fd, &tmwfd);
269 tmwfd.c_oflag |= ONLCR | OPOST;
270 tmwfd.c_lflag |= ISIG;
271 tcsetattr(mw_fd, TCSANOW, &tmwfd);
275 FD_SET(mw_fd, &mfds);
277 memset(cmdline, 0, 128);
281 if (select(mw_fd+1, &mfds, NULL, NULL, NULL) > 0) {
282 if (FD_ISSET(mw_fd, &mfds)) {
283 rcvd = read(mw_fd, msg_buf, 64);
285 fprintf(stderr, "read %d bytes:\n", rcvd);
289 dump_frame(msg_buf, rcvd);
291 decode_frame(mw_fd, msg_buf, rcvd);
294 if (FD_ISSET(0, &mfds)) {
295 rcvd = read(0, (cmdline+clinep), 1);
297 if (cmdline[clinep] == '\r') {
299 cmdline[clinep--] = '\0';
300 process_cmd(cmdline, clinep, mw_fd);
302 memset(cmdline, 0, 128);
307 printf("\r> %s", cmdline);
316 FD_SET(mw_fd, &mfds);
322 int open_socket(bdaddr_t *bdaddr, uint8_t channel)
324 struct sockaddr_rc addr;
328 sk = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
330 fprintf(stderr, "Can't create socket: %s (%d)\n",
331 strerror(errno), errno);
337 if (setsockopt(sk, SOL_BLUETOOTH, BT_FLUSHABLE, &f, sizeof(f)) < 0) {
338 fprintf(stderr, "Can't set flushable: %s (%d)\n",
339 strerror(errno), errno);
343 memset(&addr, 0, sizeof(addr));
344 addr.rc_family = AF_BLUETOOTH;
345 bacpy(&addr.rc_bdaddr, BDADDR_ANY);
347 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
348 fprintf(stderr, "Can't bind socket: %s (%d)\n",
349 strerror(errno), errno);
356 opt |= RFCOMM_LM_MASTER;
357 opt |= RFCOMM_LM_AUTH;
359 opt |= RFCOMM_LM_ENCRYPT;
360 opt |= RFCOMM_LM_SECURE;
362 if (opt && setsockopt(sk, SOL_RFCOMM, RFCOMM_LM, &opt, sizeof(opt)) < 0) {
363 fprintf(stderr, "Can't set RFCOMM link mode: %s (%d)",
364 strerror(errno), errno);
369 memset(&addr, 0, sizeof(addr));
370 addr.rc_family = AF_BLUETOOTH;
371 bacpy(&addr.rc_bdaddr, bdaddr);
372 addr.rc_channel = channel;
374 if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
375 fprintf(stderr, "Can't connect: %s (%d)\n",
376 strerror(errno), errno);
384 void baswap(bdaddr_t *dst, const bdaddr_t *src)
386 register unsigned char *d = (unsigned char *) dst;
387 register const unsigned char *s = (const unsigned char *) src;
390 for (i = 0; i < 6; i++)
393 int bachk(const char *str)
398 if (strlen(str) != 17)
402 if (!isxdigit(*str++))
405 if (!isxdigit(*str++))
418 int str2ba(const char *str, bdaddr_t *ba)
423 if (bachk(str) < 0) {
424 memset(ba, 0, sizeof(*ba));
428 for (i = 0; i < 6; i++, str += 3)
429 b.b[i] = strtol(str, NULL, 16);
436 int main(int argc, char **argv)
442 fprintf(stderr, "Usage:\n\t%s <devicename>\n", argv[0]);
449 mw_fd = open(argv[1], O_RDWR);
455 if (str2ba(argv[1], &btaddr))
457 mw_fd = open_socket(&btaddr, 1);
461 fprintf(stderr, "connected to %s\n", argv[1]);