diff options
Diffstat (limited to 'tmk_core/protocol/iwrap/iwrap.c')
-rw-r--r-- | tmk_core/protocol/iwrap/iwrap.c | 202 |
1 files changed, 76 insertions, 126 deletions
diff --git a/tmk_core/protocol/iwrap/iwrap.c b/tmk_core/protocol/iwrap/iwrap.c index 71ccc493e..05e632da3 100644 --- a/tmk_core/protocol/iwrap/iwrap.c +++ b/tmk_core/protocol/iwrap/iwrap.c @@ -37,43 +37,39 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #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) - +#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; +// static uint8_t channel = 1; /* iWRAP buffer */ #define MUX_BUF_SIZE 64 -static char buf[MUX_BUF_SIZE]; +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 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) -{ +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; + rcv_head = next; } } -static char rcv_deq(void) -{ +static char rcv_deq(void) { char c = 0; if (rcv_head != rcv_tail) { c = rcv_buf[rcv_tail++]; @@ -91,38 +87,33 @@ static char rcv_peek(void) } */ -static void rcv_clear(void) -{ - rcv_tail = rcv_head = 0; -} +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 +ISR(PCINT1_vect, ISR_BLOCK) // recv() runs away in case of ISR_NOBLOCK { - if ((SUART_IN_PIN & (1<<SUART_IN_BIT))) - return; + 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(); + static volatile uint8_t mux_link = 0xff; + uint8_t c = recv(); switch (mux_state) { - case 0xff: // SOF - if (c == 0xbf) - mux_state--; + case 0xff: // SOF + if (c == 0xbf) mux_state--; break; - case 0xfe: // Link + case 0xfe: // Link mux_state--; mux_link = c; break; - case 0xfd: // Flags + case 0xfd: // Flags mux_state--; break; - case 0xfc: // Length + case 0xfc: // Length mux_state = c; break; case 0x00: mux_state = 0xff; - mux_link = 0xff; + mux_link = 0xff; break; default: if (mux_state--) { @@ -132,12 +123,10 @@ ISR(PCINT1_vect, ISR_BLOCK) // recv() runs away in case of ISR_NOBLOCK } } - /*------------------------------------------------------------------* * iWRAP communication *------------------------------------------------------------------*/ -void iwrap_init(void) -{ +void iwrap_init(void) { // reset iWRAP if in already MUX mode after AVR software-reset iwrap_send("RESET"); iwrap_mux_send("RESET"); @@ -147,43 +136,34 @@ void iwrap_init(void) iwrap_check_connection(); } -void iwrap_mux_send(const char *s) -{ +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++); +void iwrap_send(const char *s) { + while (*s) xmit(*s++); } /* send buffer */ -void iwrap_buf_add(uint8_t c) -{ +void iwrap_buf_add(uint8_t c) { // need space for '\0' - if (snd_pos < MUX_BUF_SIZE-1) - buf[snd_pos++] = c; + if (snd_pos < MUX_BUF_SIZE - 1) buf[snd_pos++] = c; } -void iwrap_buf_del(void) -{ - if (snd_pos) - snd_pos--; +void iwrap_buf_del(void) { + if (snd_pos) snd_pos--; } -void iwrap_buf_send(void) -{ +void iwrap_buf_send(void) { buf[snd_pos] = '\0'; - snd_pos = 0; + snd_pos = 0; iwrap_mux_send(buf); } -void iwrap_call(void) -{ +void iwrap_call(void) { char *p; iwrap_mux_send("SET BT PAIR"); @@ -193,7 +173,7 @@ void iwrap_call(void) while (!strncmp(p, "SET BT PAIR", 11)) { p += 7; strncpy(p, "CALL", 4); - strncpy(p+22, " 11 HID\n\0", 9); + strncpy(p + 22, " 11 HID\n\0", 9); print_S(p); iwrap_mux_send(p); // TODO: skip to next line @@ -224,20 +204,21 @@ void iwrap_call(void) iwrap_check_connection(); } -void iwrap_kill(void) -{ +void iwrap_kill(void) { char c; iwrap_mux_send("LIST"); _delay_ms(500); - while ((c = rcv_deq()) && c != '\n') ; + 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 != ' ') ; + while ((c = rcv_deq()) && c != ' ') + ; char *p = rcv_buf + rcv_tail - 5; strncpy(p, "KILL ", 5); @@ -249,47 +230,34 @@ void iwrap_kill(void) iwrap_check_connection(); } -void iwrap_unpair(void) -{ +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); + strncpy(p + 29, "\n\0", 2); print_S(p); iwrap_mux_send(p); } } -void iwrap_sleep(void) -{ - iwrap_mux_send("SLEEP"); -} +void iwrap_sleep(void) { iwrap_mux_send("SLEEP"); } -void iwrap_sniff(void) -{ -} +void iwrap_sniff(void) {} -void iwrap_subrate(void) -{ -} +void iwrap_subrate(void) {} -bool iwrap_failed(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_connected(void) { return connected; } -uint8_t iwrap_check_connection(void) -{ +uint8_t iwrap_check_connection(void) { iwrap_mux_send("LIST"); _delay_ms(100); @@ -300,44 +268,31 @@ uint8_t iwrap_check_connection(void) 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 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 uint8_t keyboard_leds(void) { - return 0; -} +static host_driver_t driver = {keyboard_leds, send_keyboard, send_mouse, send_system, send_consumer}; -static void send_keyboard(report_keyboard_t *report) -{ +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(0x0a); // Length + xmit(0xa1); // DATA(Input) + xmit(0x01); // Report ID xmit(report->mods); - xmit(0x00); // reserved byte(always 0) + xmit(0x00); // reserved byte(always 0) xmit(report->keys[0]); xmit(report->keys[1]); xmit(report->keys[2]); @@ -347,16 +302,15 @@ static void send_keyboard(report_keyboard_t *report) MUX_FOOTER(0x01); } -static void send_mouse(report_mouse_t *report) -{ +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(0x07); // Length + xmit(0xa1); // DATA(Input) + xmit(0x02); // Report ID xmit(report->buttons); xmit(report->x); xmit(report->y); @@ -366,18 +320,14 @@ static void send_mouse(report_mouse_t *report) #endif } -static void send_system(uint16_t data) -{ - /* not supported */ -} +static void send_system(uint16_t data) { /* not supported */ } -static void send_consumer(uint16_t data) -{ +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; + uint8_t bits1 = 0; + uint8_t bits2 = 0; + uint8_t bits3 = 0; if (!iwrap_connected() && !iwrap_check_connection()) return; if (data == last_data) return; @@ -458,9 +408,9 @@ static void send_consumer(uint16_t data) MUX_HEADER(0x01, 0x07); xmit(0x9f); - xmit(0x05); // Length - xmit(0xa1); // DATA(Input) - xmit(0x03); // Report ID + xmit(0x05); // Length + xmit(0xa1); // DATA(Input) + xmit(0x03); // Report ID xmit(bits1); xmit(bits2); xmit(bits3); |