summaryrefslogtreecommitdiffstats
path: root/tmk_core/protocol/iwrap/iwrap.c
blob: 4d0ca5756b90f7827f30fe620588af46f8257da9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
Copyright 2011 Jun Wako <wakojun@gmail.com>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/* host driver for Bulegiga iWRAP */
/* Bluegiga BT12
 * Connections
 *    Hardware UART       Software UART            BlueTooth
 * PC=====UART=======AVR=====SUART====iWRAP(BT12)-----------PC
 *
 * - Hardware UART for Debug Console to communicate iWRAP
 * - Software UART for iWRAP control to send keyboard/mouse data
 */

#include <stdint.h>
#include <string.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "keycode.h"
#include "suart.h"
#include "uart.h"
#include "report.h"
#include "host_driver.h"
#include "iwrap.h"
#include "print.h"

/* iWRAP MUX mode utils. 3.10 HID raw mode(iWRAP_HID_Application_Note.pdf) */
#define MUX_HEADER(LINK, LENGTH)   \
    do {                           \
        xmit(0xbf);   /* SOF    */ \
        xmit(LINK);   /* Link   */ \
        xmit(0x00);   /* Flags  */ \
        xmit(LENGTH); /* Length */ \
    } while (0)
#define MUX_FOOTER(LINK) xmit(LINK ^ 0xff)

static uint8_t connected = 0;
// static uint8_t channel = 1;

/* iWRAP buffer */
#define MUX_BUF_SIZE 64
static char    buf[MUX_BUF_SIZE];
static uint8_t snd_pos = 0;

#define MUX_RCV_BUF_SIZE 256
static char    rcv_buf[MUX_RCV_BUF_SIZE];
static uint8_t rcv_head = 0;
static uint8_t rcv_tail = 0;

/* receive buffer */
static void rcv_enq(char c) {
    uint8_t next = (rcv_head + 1) % MUX_RCV_BUF_SIZE;
    if (next != rcv_tail) {
        rcv_buf[rcv_head] = c;
        rcv_head          = next;
    }
}

static char rcv_deq(void) {
    char c = 0;
    if (rcv_head != rcv_tail) {
        c = rcv_buf[rcv_tail++];
        rcv_tail %= MUX_RCV_BUF_SIZE;
    }
    return c;
}

/*
static char rcv_peek(void)
{
    if (rcv_head == rcv_tail)
        return 0;
    return rcv_buf[rcv_tail];
}
*/

static void rcv_clear(void) { rcv_tail = rcv_head = 0; }

/* iWRAP response */
ISR(PCINT1_vect, ISR_BLOCK)  // recv() runs away in case of ISR_NOBLOCK
{
    if ((SUART_IN_PIN & (1 << SUART_IN_BIT))) return;

    static volatile uint8_t mux_state = 0xff;
    static volatile uint8_t mux_link  = 0xff;
    uint8_t                 c         = recv();
    switch (mux_state) {
        case 0xff:  // SOF
            if (c == 0xbf) mux_state--;
            break;
        case 0xfe:  // Link
            mux_state--;
            mux_link = c;
            break;
        case 0xfd:  // Flags
            mux_state--;
            break;
        case 0xfc:  // Length
            mux_state = c;
            break;
        case 0x00:
            mux_state = 0xff;
            mux_link  = 0xff;
            break;
        default:
            if (mux_state--) {
                uart_putchar(c);
                rcv_enq(c);
            }
    }
}

/*------------------------------------------------------------------*
 * iWRAP communication
 *------------------------------------------------------------------*/
void iwrap_init(void) {
    // reset iWRAP if in already MUX mode after AVR software-reset
    iwrap_send("RESET");
    iwrap_mux_send("RESET");
    _delay_ms(3000);
    iwrap_send("\r\nSET CONTROL MUX 1\r\n");
    _delay_ms(500);
    iwrap_check_connection();
}

void iwrap_mux_send(const char *s) {
    rcv_clear();
    MUX_HEADER(0xff, strlen((char *)s));
    iwrap_send(s);
    MUX_FOOTER(0xff);
}

void iwrap_send(const char *s) {
    while (*s) xmit(*s++);
}

/* send buffer */
void iwrap_buf_add(uint8_t c) {
    // need space for '\0'
    if (snd_pos < MUX_BUF_SIZE - 1) buf[snd_pos++] = c;
}

void iwrap_buf_del(void) {
    if (snd_pos) snd_pos--;
}

void iwrap_buf_send(void) {
    buf[snd_pos] = '\0';
    snd_pos      = 0;
    iwrap_mux_send(buf);
}

void iwrap_call(void) {
    char *p;

    iwrap_mux_send("SET BT PAIR");
    _delay_ms(500);

    p = rcv_buf + rcv_tail;
    while (!strncmp(p, "SET BT PAIR", 11)) {
        p += 7;
        strncpy(p, "CALL", 4);
        strncpy(p + 22, " 11 HID\n\0", 9);
        print_S(p);
        iwrap_mux_send(p);
        // TODO: skip to next line
        p += 57;

        DEBUG_LED_CONFIG;
        DEBUG_LED_ON;
        _delay_ms(500);
        DEBUG_LED_OFF;
        _delay_ms(500);
        DEBUG_LED_ON;
        _delay_ms(500);
        DEBUG_LED_OFF;
        _delay_ms(500);
        DEBUG_LED_ON;
        _delay_ms(500);
        DEBUG_LED_OFF;
        _delay_ms(500);
        DEBUG_LED_ON;
        _delay_ms(500);
        DEBUG_LED_OFF;
        _delay_ms(500);
        DEBUG_LED_ON;
        _delay_ms(500);
        DEBUG_LED_OFF;
        _delay_ms(500);
    }
    iwrap_check_connection();
}

void iwrap_kill(void) {
    char c;
    iwrap_mux_send("LIST");
    _delay_ms(500);

    while ((c = rcv_deq()) && c != '\n')
        ;
    if (strncmp(rcv_buf + rcv_tail, "LIST ", 5)) {
        print("no connection to kill.\n");
        return;
    }
    // skip 10 'space' chars
    for (uint8_t i = 10; i; i--)
        while ((c = rcv_deq()) && c != ' ')
            ;

    char *p = rcv_buf + rcv_tail - 5;
    strncpy(p, "KILL ", 5);
    strncpy(p + 22, "\n\0", 2);
    print_S(p);
    iwrap_mux_send(p);
    _delay_ms(500);

    iwrap_check_connection();
}

void iwrap_unpair(void) {
    iwrap_mux_send("SET BT PAIR");
    _delay_ms(500);

    char *p = rcv_buf + rcv_tail;
    if (!strncmp(p, "SET BT PAIR", 11)) {
        strncpy(p + 29, "\n\0", 2);
        print_S(p);
        iwrap_mux_send(p);
    }
}

void iwrap_sleep(void) { iwrap_mux_send("SLEEP"); }

void iwrap_sniff(void) {}

void iwrap_subrate(void) {}

bool iwrap_failed(void) {
    if (strncmp(rcv_buf, "SYNTAX ERROR", 12))
        return true;
    else
        return false;
}

uint8_t iwrap_connected(void) { return connected; }

uint8_t iwrap_check_connection(void) {
    iwrap_mux_send("LIST");
    _delay_ms(100);

    if (strncmp(rcv_buf, "LIST ", 5) || !strncmp(rcv_buf, "LIST 0", 6))
        connected = 0;
    else
        connected = 1;
    return connected;
}

/*------------------------------------------------------------------*
 * Host driver
 *------------------------------------------------------------------*/
static uint8_t keyboard_leds(void);
static void    send_keyboard(report_keyboard_t *report);
static void    send_mouse(report_mouse_t *report);
static void    send_system(uint16_t data);
static void    send_consumer(uint16_t data);

static host_driver_t driver = {keyboard_leds, send_keyboard, send_mouse, send_system, send_consumer};

host_driver_t *iwrap_driver(void) { return &driver; }

static uint8_t keyboard_leds(void) { return 0; }

static void send_keyboard(report_keyboard_t *report) {
    if (!iwrap_connected() && !iwrap_check_connection()) return;
    MUX_HEADER(0x01, 0x0c);
    // HID raw mode header
    xmit(0x9f);
    xmit(0x0a);  // Length
    xmit(0xa1);  // DATA(Input)
    xmit(0x01);  // Report ID
    xmit(report->mods);
    xmit(0x00);  // reserved byte(always 0)
    xmit(report->keys[0]);
    xmit(report->keys[1]);
    xmit(report->keys[2]);
    xmit(report->keys[3]);
    xmit(report->keys[4]);
    xmit(report->keys[5]);
    MUX_FOOTER(0x01);
}

static void send_mouse(report_mouse_t *report) {
#if defined(MOUSEKEY_ENABLE) || defined(PS2_MOUSE_ENABLE) || defined(POINTING_DEVICE_ENABLE)
    if (!iwrap_connected() && !iwrap_check_connection()) return;
    MUX_HEADER(0x01, 0x09);
    // HID raw mode header
    xmit(0x9f);
    xmit(0x07);  // Length
    xmit(0xa1);  // DATA(Input)
    xmit(0x02);  // Report ID
    xmit(report->buttons);
    xmit(report->x);
    xmit(report->y);
    xmit(report->v);
    xmit(report->h);
    MUX_FOOTER(0x01);
#endif
}

static void send_system(uint16_t data) { /* not supported */
}

static void send_consumer(uint16_t data) {
#ifdef EXTRAKEY_ENABLE
    static uint16_t last_data = 0;
    uint8_t         bits1     = 0;
    uint8_t         bits2     = 0;
    uint8_t         bits3     = 0;

    if (!iwrap_connected() && !iwrap_check_connection()) return;
    if (data == last_data) return;
    last_data = data;

    // 3.10 HID raw mode(iWRAP_HID_Application_Note.pdf)
    switch (data) {
        case AUDIO_VOL_UP:
            bits1 = 0x01;
            break;
        case AUDIO_VOL_DOWN:
            bits1 = 0x02;
            break;
        case AUDIO_MUTE:
            bits1 = 0x04;
            break;
        case TRANSPORT_PLAY_PAUSE:
            bits1 = 0x08;
            break;
        case TRANSPORT_NEXT_TRACK:
            bits1 = 0x10;
            break;
        case TRANSPORT_PREV_TRACK:
            bits1 = 0x20;
            break;
        case TRANSPORT_STOP:
            bits1 = 0x40;
            break;
        case TRANSPORT_EJECT:
            bits1 = 0x80;
            break;
        case AL_EMAIL:
            bits2 = 0x01;
            break;
        case AC_SEARCH:
            bits2 = 0x02;
            break;
        case AC_BOOKMARKS:
            bits2 = 0x04;
            break;
        case AC_HOME:
            bits2 = 0x08;
            break;
        case AC_BACK:
            bits2 = 0x10;
            break;
        case AC_FORWARD:
            bits2 = 0x20;
            break;
        case AC_STOP:
            bits2 = 0x40;
            break;
        case AC_REFRESH:
            bits2 = 0x80;
            break;
        case AL_CC_CONFIG:
            bits3 = 0x01;
            break;
        case AL_CALCULATOR:
            bits3 = 0x04;
            break;
        case AL_LOCK:
            bits3 = 0x08;
            break;
        case AL_LOCAL_BROWSER:
            bits3 = 0x10;
            break;
        case AC_MINIMIZE:
            bits3 = 0x20;
            break;
        case TRANSPORT_RECORD:
            bits3 = 0x40;
            break;
        case TRANSPORT_REWIND:
            bits3 = 0x80;
            break;
    }

    MUX_HEADER(0x01, 0x07);
    xmit(0x9f);
    xmit(0x05);  // Length
    xmit(0xa1);  // DATA(Input)
    xmit(0x03);  // Report ID
    xmit(bits1);
    xmit(bits2);
    xmit(bits3);
    MUX_FOOTER(0x01);
#endif
}