]> git.kernelconcepts.de Git - metawatch.git/blob - mw_main.c
Fixes and enhancements
[metawatch.git] / mw_main.c
1 /*
2  * (c) 2011 Siegen, Germany by Nils Faerber <nils.faerber@kernelconcepts.de>
3  *
4  * license LGPL
5  */
6
7 #include <stdio.h>
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <time.h>
15 #include <termios.h>
16 #include <ctype.h>
17
18 #include <errno.h>
19
20 #include <bluetooth/bluetooth.h>
21 #include <bluetooth/rfcomm.h>
22
23 /*
24 #include <bluetooth/sdp.h>
25 #include <bluetooth/sdp_lib.h>
26 */
27
28 #include "metawatch.h"
29 #include "crc16ccitt.h"
30
31
32 void bitmap_test(int mw_fd)
33 {
34         /* a nice checker-board pattern */
35         unsigned char checkbuf[24] = {
36                 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
37                 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa,
38                 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
39                 0x55, 0x55, 0x55, 0x55, 0x55, 0x55,
40         };
41         mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 31, checkbuf, 24);
42         mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 33, checkbuf, 24);
43         mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 35, checkbuf, 24);
44         mw_write_buffer(mw_fd, MW_SCREEN_MODE_IDLE, 0, 37, checkbuf, 24);
45
46         mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
47 }
48
49
50 void bitmap_read(int mw_fd)
51 {
52         int ffd, ret;
53         char rbuf[256];
54         unsigned int width, height, i;
55 #ifdef DEBUG
56         unsigned int x, y;
57 #endif
58         unsigned int rowlength;
59         unsigned char *bmapbuf;
60         // unsigned char mw_buf[24];
61
62         ffd = open("test.pbm", O_RDONLY);
63         if (ffd < 0) {
64                 perror("open");
65                 return;
66         };
67         ret = read(ffd, rbuf, 3);
68         if (rbuf[0] != 'P' || rbuf[1] != '4') {
69                 fprintf(stderr, "not a PBM file\n");
70                 return;
71         }
72         memset(rbuf, 0, 256);
73         i = 0;
74         do {
75                 ret = read(ffd, (rbuf+i), 1);
76         } while (!isspace(rbuf[i++]));
77         width = atoi(rbuf);
78
79         memset(rbuf, 0, 256);
80         i = 0;
81         do {
82                 ret = read(ffd, (rbuf+i), 1);
83         } while (!isspace(rbuf[i++]));
84         height = atoi(rbuf);
85
86         rowlength = ((width / 8) + 1);
87
88         bmapbuf = malloc(rowlength * height);
89
90         ret = read(ffd, bmapbuf, rowlength * height);
91         close(ffd);
92
93 #ifdef DEBUG
94         fprintf(stderr, "row length = %d bytes\n", rowlength);
95         fprintf(stderr, "bitmap resolution is %d x %d\n", width, height);
96         fprintf(stderr, "read %d of %d bytes\n", ret, rowlength * height);
97         fprintf(stderr, "\n");
98         for (y=0; y<height; y++) {
99                 for (x=0; x<rowlength; x++) {
100                         for (i=0; i<8; i++)
101                                 fprintf(stderr, "%c", (bmapbuf[(y*rowlength)+x] & (1<<(7-i))) ? '.' : ' ');
102                 }
103                 fprintf(stderr, "\n");
104         }
105         fprintf(stderr, "\n");
106 #endif
107
108         /* reverse bits and invert the bmap */
109         bmap_buffer_flipinvert(1, 1, bmapbuf, rowlength * height);
110         /* send the buffer to the watch */
111         mw_send_bitmap(mw_fd, MW_SCREEN_MODE_IDLE, width, height, 31, bmapbuf, rowlength * height);
112         /* update the display */
113         mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
114
115         free(bmapbuf);
116 }
117
118 void print_help(void)
119 {
120 }
121
122 void process_cmd(char *cmdline, int clinep, int mw_fd)
123 {
124         unsigned char mdata[32];
125
126         fprintf(stderr, "command: '%s'\n", cmdline);
127
128         if (strncmp(cmdline, "quit", 4) == 0) {
129                 close(mw_fd);
130                 exit(0);
131         }
132         if (strncmp(cmdline, "help", 4) == 0) {
133                 print_help();
134         }
135         if (strncmp(cmdline, "srtc", 4) == 0) {
136                 fprintf(stderr, "Setting RTC from system time...");
137                 mw_set_rtc(mw_fd, MW_RTC_CLOCK_24HR, MW_RTC_DATE_DDMM);
138                 fprintf(stderr, "OK\n");
139         }
140         if (strncmp(cmdline, "grtc", 4) == 0) {
141                 mw_send_frame(mw_fd, MW_GET_REAL_TIME_CLOCK, 0, NULL, 0);
142         }
143         if (strncmp(cmdline, "gistr", 5) == 0) {
144                 mw_send_frame(mw_fd, MW_GET_INFORMATION_STRING, 0, NULL, 0);
145         }
146         if (strncmp(cmdline, "gdtype", 6) == 0) {
147                 mw_send_frame(mw_fd, MW_GET_DEVICE_TYPE, 0, NULL, 0);
148         }
149         if (strncmp(cmdline, "rvbat", 5) == 0) {
150                 mw_send_frame(mw_fd, MW_READ_BATTERY_VOLTAGE_MSG, 0, NULL, 0);
151         }
152         if (strncmp(cmdline, "rlight", 6) == 0) {
153                 mw_send_frame(mw_fd, MW_READ_LIGHT_SENSOR_MSG, 0, NULL, 0);
154         }
155         if (strncmp(cmdline, "modecfg", 6) == 0) {
156                 mw_configure_watch_mode(mw_fd, MW_SCREEN_MODE_IDLE, 0, 4, 1);
157                 mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 0);
158         }
159         if (strncmp(cmdline, "rbtcfg", 6) == 0) {
160                 mdata[0] = 0; /* idle screen */
161                 mdata[1] = 1; /* button index */
162                 mdata[2] = 2; /* button press type */
163                 mdata[3] = 3; /* callback message type */
164                 mdata[4] = 4; /* callback message option */
165                 mw_send_frame(mw_fd, MW_READ_BUTTON_CONFIG, 0, NULL, 0);
166         }
167         if (strncmp(cmdline, "svib", 4) == 0) {
168                 mw_set_vibrate_mode(mw_fd, 1, 300, 300, 5);
169         }
170         if (strncmp(cmdline, "tbmp", 4) == 0) {
171                 bitmap_test(mw_fd);
172         }
173         if (strncmp(cmdline, "rbmp", 4) == 0) {
174                 bitmap_read(mw_fd);
175         }
176 }
177
178
179 int menu(int mw_fd)
180 {
181         fd_set mfds;
182         struct termios tconfd, tmwfd;
183         char cmdline[128];
184         unsigned char msg_buf[64];
185         unsigned char clinep = 0;
186         int rcvd;
187
188         tcgetattr(0, &tconfd);
189         cfmakeraw(&tconfd);
190         tconfd.c_oflag |= ONLCR | OPOST;
191         tconfd.c_lflag |= ISIG;
192         tcsetattr(0, TCSANOW, &tconfd);
193
194         tcgetattr(mw_fd, &tmwfd);
195         cfmakeraw(&tmwfd);
196         tmwfd.c_oflag |= ONLCR | OPOST;
197         tmwfd.c_lflag |= ISIG;
198         tcsetattr(mw_fd, TCSANOW, &tmwfd);
199
200         FD_ZERO(&mfds);
201         FD_SET(0, &mfds);
202         FD_SET(mw_fd, &mfds);
203
204         memset(cmdline, 0, 128);
205
206         do {
207                 rcvd = 0;
208                 if (select(mw_fd+1, &mfds, NULL, NULL, NULL) > 0) {
209                         if (FD_ISSET(mw_fd, &mfds)) {
210                                 rcvd = read(mw_fd, msg_buf, 64);
211 #ifdef DEBUG
212                                 fprintf(stderr, "read %d bytes:\n", rcvd);
213 #endif
214                                 if (rcvd > 0) {
215 #ifdef DEBUG
216                                         dump_frame(msg_buf, rcvd);
217 #endif
218                                         decode_frame(mw_fd, msg_buf, rcvd);
219                                 }
220                         };
221                         if (FD_ISSET(0, &mfds)) {
222                                 rcvd = read(0, (cmdline+clinep), 1);
223                                 if (rcvd > 0) {
224                                         if (cmdline[clinep] == '\r') {
225                                                 printf("\n");
226                                                 cmdline[clinep--] = '\0';
227                                                 process_cmd(cmdline, clinep, mw_fd);
228                                                 clinep = 0;
229                                                 memset(cmdline, 0, 128);
230                                         } else {
231                                                 clinep++;
232                                                 if (clinep > 75)
233                                                         clinep = 75;
234                                                 printf("\r> %s", cmdline);
235                                                 fflush(stdout);
236                                         }
237                                 }
238                         };
239                 } else
240                         break;
241                 FD_ZERO(&mfds);
242                 FD_SET(0, &mfds);
243                 FD_SET(mw_fd, &mfds);
244         } while (rcvd > 0);
245
246         return 0;
247 }
248
249 int open_socket(bdaddr_t *bdaddr, uint8_t channel)
250 {
251         struct sockaddr_rc addr;
252         int sk, opt;
253         //int f;
254
255         sk = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
256         if (sk < 0) {
257                 fprintf(stderr, "Can't create socket: %s (%d)\n",
258                         strerror(errno), errno);
259                 return -1;
260         }
261
262 /*
263         f = 1;
264         if (setsockopt(sk, SOL_BLUETOOTH, BT_FLUSHABLE, &f, sizeof(f)) < 0) {
265                 fprintf(stderr, "Can't set flushable: %s (%d)\n",
266                         strerror(errno), errno);
267                 return -1;
268         }
269 */
270         memset(&addr, 0, sizeof(addr));
271         addr.rc_family = AF_BLUETOOTH;
272         bacpy(&addr.rc_bdaddr, BDADDR_ANY);
273
274         if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
275                 fprintf(stderr, "Can't bind socket: %s (%d)\n",
276                                                         strerror(errno), errno);
277                 close(sk);
278                 return -1;
279         }
280
281        /* Set link mode */
282         opt = 0;
283         opt |= RFCOMM_LM_MASTER;
284         opt |= RFCOMM_LM_AUTH;
285 /*
286         opt |= RFCOMM_LM_ENCRYPT;
287         opt |= RFCOMM_LM_SECURE;
288 */
289         if (opt && setsockopt(sk, SOL_RFCOMM, RFCOMM_LM, &opt, sizeof(opt)) < 0) {
290                 fprintf(stderr, "Can't set RFCOMM link mode: %s (%d)",
291                                                         strerror(errno), errno);
292                 close(sk);
293                 return -1;
294         }
295
296         memset(&addr, 0, sizeof(addr));
297         addr.rc_family = AF_BLUETOOTH;
298         bacpy(&addr.rc_bdaddr, bdaddr);
299         addr.rc_channel = channel;
300
301         if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
302                 fprintf(stderr, "Can't connect: %s (%d)\n",
303                                                         strerror(errno), errno);
304                 close(sk);
305                 return -1;
306         }
307
308         return sk;
309 }
310
311 void baswap(bdaddr_t *dst, const bdaddr_t *src)
312 {
313         register unsigned char *d = (unsigned char *) dst;
314         register const unsigned char *s = (const unsigned char *) src;
315         register int i;
316
317         for (i = 0; i < 6; i++)
318                 d[i] = s[5-i];
319 }
320 int bachk(const char *str)
321 {
322         if (!str)
323                 return -1;
324
325         if (strlen(str) != 17)
326                 return -1;
327
328         while (*str) {
329                 if (!isxdigit(*str++))
330                         return -1;
331
332                 if (!isxdigit(*str++))
333                         return -1;
334
335                 if (*str == 0)
336                         break;
337
338                 if (*str++ != ':')
339                         return -1;
340         }
341
342         return 0;
343 }
344
345 int str2ba(const char *str, bdaddr_t *ba)
346 {
347         bdaddr_t b;
348         int i;
349
350         if (bachk(str) < 0) {
351                 memset(ba, 0, sizeof(*ba));
352                 return -1;
353         }
354
355         for (i = 0; i < 6; i++, str += 3)
356                 b.b[i] = strtol(str, NULL, 16);
357
358         baswap(ba, &b);
359
360         return 0;
361 }
362
363 int main(int argc, char **argv)
364 {
365         int mw_fd;
366         bdaddr_t btaddr;
367
368         if (argc != 2) {
369                 fprintf(stderr, "Usage:\n\t%s <devicename>\n", argv[0]);
370                 return 1;
371         };
372   
373         crc16ccitt_init();
374
375 #if USE_RFCOMM_FILE
376         mw_fd = open(argv[1], O_RDWR);
377         if (mw_fd < 0) {
378                 perror("open");
379                 return 1;
380         };
381 #else
382         if (str2ba(argv[1], &btaddr))
383                 return 1;
384         mw_fd = open_socket(&btaddr, 1);
385         if (mw_fd < 0) {
386                 return 1;
387         } else {
388                 fprintf(stderr, "connected to %s\n", argv[1]);
389         };
390 #endif
391
392         menu(mw_fd);
393
394         fsync(mw_fd);
395
396         close(mw_fd);
397
398         return 0;
399 };
400