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"
32 void bitmap_test(int mw_fd)
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,
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);
46 mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
50 void bitmap_read(int mw_fd)
54 unsigned int width, height, i, x, y;
55 unsigned int rowlength;
56 unsigned char *bmapbuf;
57 // unsigned char mw_buf[24];
59 ffd = open("test.pbm", O_RDONLY);
64 ret = read(ffd, rbuf, 3);
65 if (rbuf[0] != 'P' || rbuf[1] != '4') {
66 fprintf(stderr, "not a PBM file\n");
72 ret = read(ffd, (rbuf+i), 1);
73 } while (!isspace(rbuf[i++]));
79 ret = read(ffd, (rbuf+i), 1);
80 } while (!isspace(rbuf[i++]));
83 rowlength = ((width / 8) + 1);
85 bmapbuf = malloc(rowlength * height);
87 ret = read(ffd, bmapbuf, rowlength * height);
91 fprintf(stderr, "row length = %d bytes\n", rowlength);
92 fprintf(stderr, "bitmap resolution is %d x %d\n", width, height);
93 fprintf(stderr, "read %d of %d bytes\n", ret, rowlength * height);
94 fprintf(stderr, "\n");
95 for (y=0; y<height; y++) {
96 for (x=0; x<rowlength; x++) {
98 fprintf(stderr, "%c", (bmapbuf[(y*rowlength)+x] & (1<<(7-i))) ? '.' : ' ');
100 fprintf(stderr, "\n");
102 fprintf(stderr, "\n");
105 /* reverse bits and invert the bmap */
106 bmap_buffer_flipinvert(1, 0, bmapbuf, rowlength * height);
107 /* send the buffer to the watch */
108 mw_send_bitmap(mw_fd, MW_SCREEN_MODE_IDLE, width, height, 31, bmapbuf, rowlength * height);
109 /* update the display */
110 mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 1);
115 void print_help(void)
119 void process_cmd(char *cmdline, int clinep, int mw_fd)
121 unsigned char mdata[32];
123 fprintf(stderr, "command: '%s'\n", cmdline);
125 if (strncmp(cmdline, "quit", 4) == 0) {
129 if (strncmp(cmdline, "help", 4) == 0) {
132 if (strncmp(cmdline, "srtc", 4) == 0) {
133 fprintf(stderr, "Setting RTC from system time...");
134 mw_set_rtc(mw_fd, MW_RTC_CLOCK_24HR, MW_RTC_DATE_DDMM);
135 fprintf(stderr, "OK\n");
137 if (strncmp(cmdline, "grtc", 4) == 0) {
138 mw_send_frame(mw_fd, MW_GET_REAL_TIME_CLOCK, 0, NULL, 0);
140 if (strncmp(cmdline, "gistr", 5) == 0) {
142 mw_send_frame(mw_fd, MW_GET_INFORMATION_STRING, 0, mdata, 1);
144 if (strncmp(cmdline, "gdtype", 6) == 0) {
145 mw_send_frame(mw_fd, MW_GET_DEVICE_TYPE, 0, NULL, 0);
147 if (strncmp(cmdline, "rvbat", 5) == 0) {
148 mw_send_frame(mw_fd, MW_READ_BATTERY_VOLTAGE_MSG, 0, NULL, 0);
150 if (strncmp(cmdline, "modecfg", 6) == 0) {
151 mw_configure_watch_mode(mw_fd, MW_SCREEN_MODE_IDLE, 0, 4, 1);
152 mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 0);
154 if (strncmp(cmdline, "rbtcfg", 6) == 0) {
155 mdata[0] = 0; /* idle screen */
156 mdata[1] = 1; /* button index */
157 mdata[2] = 2; /* button press type */
158 mdata[3] = 3; /* callback message type */
159 mdata[4] = 4; /* callback message option */
160 mw_send_frame(mw_fd, MW_READ_BUTTON_CONFIG, 0, NULL, 0);
162 if (strncmp(cmdline, "svib", 4) == 0) {
163 mw_set_vibrate_mode(mw_fd, 1, 300, 300, 5);
165 if (strncmp(cmdline, "tbmp", 4) == 0) {
168 if (strncmp(cmdline, "rbmp", 4) == 0) {
177 struct termios tconfd, tmwfd;
179 unsigned char msg_buf[64];
180 unsigned char clinep = 0;
183 tcgetattr(0, &tconfd);
185 tconfd.c_oflag |= ONLCR | OPOST;
186 tconfd.c_lflag |= ISIG;
187 tcsetattr(0, TCSANOW, &tconfd);
189 tcgetattr(mw_fd, &tmwfd);
191 tmwfd.c_oflag |= ONLCR | OPOST;
192 tmwfd.c_lflag |= ISIG;
193 tcsetattr(mw_fd, TCSANOW, &tmwfd);
197 FD_SET(mw_fd, &mfds);
199 memset(cmdline, 0, 128);
203 if (select(mw_fd+1, &mfds, NULL, NULL, NULL) > 0) {
204 if (FD_ISSET(mw_fd, &mfds)) {
205 rcvd = read(mw_fd, msg_buf, 64);
206 fprintf(stderr, "read %d bytes:\n", rcvd);
208 dump_frame(msg_buf, rcvd);
209 decode_frame(mw_fd, msg_buf, rcvd);
212 if (FD_ISSET(0, &mfds)) {
213 rcvd = read(0, (cmdline+clinep), 1);
215 if (cmdline[clinep] == '\r') {
217 cmdline[clinep--] = '\0';
218 process_cmd(cmdline, clinep, mw_fd);
220 memset(cmdline, 0, 128);
225 printf("\r> %s", cmdline);
234 FD_SET(mw_fd, &mfds);
240 int open_socket(bdaddr_t *bdaddr, uint8_t channel)
242 struct sockaddr_rc addr;
246 sk = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
248 fprintf(stderr, "Can't create socket: %s (%d)\n",
249 strerror(errno), errno);
255 if (setsockopt(sk, SOL_BLUETOOTH, BT_FLUSHABLE, &f, sizeof(f)) < 0) {
256 fprintf(stderr, "Can't set flushable: %s (%d)\n",
257 strerror(errno), errno);
261 memset(&addr, 0, sizeof(addr));
262 addr.rc_family = AF_BLUETOOTH;
263 bacpy(&addr.rc_bdaddr, BDADDR_ANY);
265 if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
266 fprintf(stderr, "Can't bind socket: %s (%d)\n",
267 strerror(errno), errno);
274 opt |= RFCOMM_LM_MASTER;
275 opt |= RFCOMM_LM_AUTH;
277 opt |= RFCOMM_LM_ENCRYPT;
278 opt |= RFCOMM_LM_SECURE;
280 if (opt && setsockopt(sk, SOL_RFCOMM, RFCOMM_LM, &opt, sizeof(opt)) < 0) {
281 fprintf(stderr, "Can't set RFCOMM link mode: %s (%d)",
282 strerror(errno), errno);
287 memset(&addr, 0, sizeof(addr));
288 addr.rc_family = AF_BLUETOOTH;
289 bacpy(&addr.rc_bdaddr, bdaddr);
290 addr.rc_channel = channel;
292 if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
293 fprintf(stderr, "Can't connect: %s (%d)\n",
294 strerror(errno), errno);
302 void baswap(bdaddr_t *dst, const bdaddr_t *src)
304 register unsigned char *d = (unsigned char *) dst;
305 register const unsigned char *s = (const unsigned char *) src;
308 for (i = 0; i < 6; i++)
311 int bachk(const char *str)
316 if (strlen(str) != 17)
320 if (!isxdigit(*str++))
323 if (!isxdigit(*str++))
336 int str2ba(const char *str, bdaddr_t *ba)
341 if (bachk(str) < 0) {
342 memset(ba, 0, sizeof(*ba));
346 for (i = 0; i < 6; i++, str += 3)
347 b.b[i] = strtol(str, NULL, 16);
354 int main(int argc, char **argv)
360 fprintf(stderr, "Usage:\n\t%s <devicename>\n", argv[0]);
367 mw_fd = open(argv[1], O_RDWR);
373 if (str2ba(argv[1], &btaddr))
375 mw_fd = open_socket(&btaddr, 1);