aboutsummaryrefslogtreecommitdiff
path: root/tmk_core/protocol/iwrap/iwrap.c
diff options
context:
space:
mode:
authorskullY <skullydazed@gmail.com>2019-08-30 11:19:03 -0700
committerskullydazed <skullydazed@users.noreply.github.com>2019-08-30 15:01:52 -0700
commitb624f32f944acdc59dcb130674c09090c5c404cb (patch)
treebc13adbba137d122d9a2c2fb2fafcbb08ac10e25 /tmk_core/protocol/iwrap/iwrap.c
parent61af76a10d00aba185b8338604171de490a13e3b (diff)
downloadqmk_firmware-b624f32f944acdc59dcb130674c09090c5c404cb.tar.gz
qmk_firmware-b624f32f944acdc59dcb130674c09090c5c404cb.zip
clang-format changes
Diffstat (limited to 'tmk_core/protocol/iwrap/iwrap.c')
-rw-r--r--tmk_core/protocol/iwrap/iwrap.c202
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/>.
37#include "iwrap.h" 37#include "iwrap.h"
38#include "print.h" 38#include "print.h"
39 39
40
41/* iWRAP MUX mode utils. 3.10 HID raw mode(iWRAP_HID_Application_Note.pdf) */ 40/* iWRAP MUX mode utils. 3.10 HID raw mode(iWRAP_HID_Application_Note.pdf) */
42#define MUX_HEADER(LINK, LENGTH) do { \ 41#define MUX_HEADER(LINK, LENGTH) \
43 xmit(0xbf); /* SOF */ \ 42 do { \
44 xmit(LINK); /* Link */ \ 43 xmit(0xbf); /* SOF */ \
45 xmit(0x00); /* Flags */ \ 44 xmit(LINK); /* Link */ \
46 xmit(LENGTH); /* Length */ \ 45 xmit(0x00); /* Flags */ \
47} while (0) 46 xmit(LENGTH); /* Length */ \
48#define MUX_FOOTER(LINK) xmit(LINK^0xff) 47 } while (0)
49 48#define MUX_FOOTER(LINK) xmit(LINK ^ 0xff)
50 49
51static uint8_t connected = 0; 50static uint8_t connected = 0;
52//static uint8_t channel = 1; 51// static uint8_t channel = 1;
53 52
54/* iWRAP buffer */ 53/* iWRAP buffer */
55#define MUX_BUF_SIZE 64 54#define MUX_BUF_SIZE 64
56static char buf[MUX_BUF_SIZE]; 55static char buf[MUX_BUF_SIZE];
57static uint8_t snd_pos = 0; 56static uint8_t snd_pos = 0;
58 57
59#define MUX_RCV_BUF_SIZE 256 58#define MUX_RCV_BUF_SIZE 256
60static char rcv_buf[MUX_RCV_BUF_SIZE]; 59static char rcv_buf[MUX_RCV_BUF_SIZE];
61static uint8_t rcv_head = 0; 60static uint8_t rcv_head = 0;
62static uint8_t rcv_tail = 0; 61static uint8_t rcv_tail = 0;
63 62
64
65/* receive buffer */ 63/* receive buffer */
66static void rcv_enq(char c) 64static void rcv_enq(char c) {
67{
68 uint8_t next = (rcv_head + 1) % MUX_RCV_BUF_SIZE; 65 uint8_t next = (rcv_head + 1) % MUX_RCV_BUF_SIZE;
69 if (next != rcv_tail) { 66 if (next != rcv_tail) {
70 rcv_buf[rcv_head] = c; 67 rcv_buf[rcv_head] = c;
71 rcv_head = next; 68 rcv_head = next;
72 } 69 }
73} 70}
74 71
75static char rcv_deq(void) 72static char rcv_deq(void) {
76{
77 char c = 0; 73 char c = 0;
78 if (rcv_head != rcv_tail) { 74 if (rcv_head != rcv_tail) {
79 c = rcv_buf[rcv_tail++]; 75 c = rcv_buf[rcv_tail++];
@@ -91,38 +87,33 @@ static char rcv_peek(void)
91} 87}
92*/ 88*/
93 89
94static void rcv_clear(void) 90static void rcv_clear(void) { rcv_tail = rcv_head = 0; }
95{
96 rcv_tail = rcv_head = 0;
97}
98 91
99/* iWRAP response */ 92/* iWRAP response */
100ISR(PCINT1_vect, ISR_BLOCK) // recv() runs away in case of ISR_NOBLOCK 93ISR(PCINT1_vect, ISR_BLOCK) // recv() runs away in case of ISR_NOBLOCK
101{ 94{
102 if ((SUART_IN_PIN & (1<<SUART_IN_BIT))) 95 if ((SUART_IN_PIN & (1 << SUART_IN_BIT))) return;
103 return;
104 96
105 static volatile uint8_t mux_state = 0xff; 97 static volatile uint8_t mux_state = 0xff;
106 static volatile uint8_t mux_link = 0xff; 98 static volatile uint8_t mux_link = 0xff;
107 uint8_t c = recv(); 99 uint8_t c = recv();
108 switch (mux_state) { 100 switch (mux_state) {
109 case 0xff: // SOF 101 case 0xff: // SOF
110 if (c == 0xbf) 102 if (c == 0xbf) mux_state--;
111 mux_state--;
112 break; 103 break;
113 case 0xfe: // Link 104 case 0xfe: // Link
114 mux_state--; 105 mux_state--;
115 mux_link = c; 106 mux_link = c;
116 break; 107 break;
117 case 0xfd: // Flags 108 case 0xfd: // Flags
118 mux_state--; 109 mux_state--;
119 break; 110 break;
120 case 0xfc: // Length 111 case 0xfc: // Length
121 mux_state = c; 112 mux_state = c;
122 break; 113 break;
123 case 0x00: 114 case 0x00:
124 mux_state = 0xff; 115 mux_state = 0xff;
125 mux_link = 0xff; 116 mux_link = 0xff;
126 break; 117 break;
127 default: 118 default:
128 if (mux_state--) { 119 if (mux_state--) {
@@ -132,12 +123,10 @@ ISR(PCINT1_vect, ISR_BLOCK) // recv() runs away in case of ISR_NOBLOCK
132 } 123 }
133} 124}
134 125
135
136/*------------------------------------------------------------------* 126/*------------------------------------------------------------------*
137 * iWRAP communication 127 * iWRAP communication
138 *------------------------------------------------------------------*/ 128 *------------------------------------------------------------------*/
139void iwrap_init(void) 129void iwrap_init(void) {
140{
141 // reset iWRAP if in already MUX mode after AVR software-reset 130 // reset iWRAP if in already MUX mode after AVR software-reset
142 iwrap_send("RESET"); 131 iwrap_send("RESET");
143 iwrap_mux_send("RESET"); 132 iwrap_mux_send("RESET");
@@ -147,43 +136,34 @@ void iwrap_init(void)
147 iwrap_check_connection(); 136 iwrap_check_connection();
148} 137}
149 138
150void iwrap_mux_send(const char *s) 139void iwrap_mux_send(const char *s) {
151{
152 rcv_clear(); 140 rcv_clear();
153 MUX_HEADER(0xff, strlen((char *)s)); 141 MUX_HEADER(0xff, strlen((char *)s));
154 iwrap_send(s); 142 iwrap_send(s);
155 MUX_FOOTER(0xff); 143 MUX_FOOTER(0xff);
156} 144}
157 145
158void iwrap_send(const char *s) 146void iwrap_send(const char *s) {
159{ 147 while (*s) xmit(*s++);
160 while (*s)
161 xmit(*s++);
162} 148}
163 149
164/* send buffer */ 150/* send buffer */
165void iwrap_buf_add(uint8_t c) 151void iwrap_buf_add(uint8_t c) {
166{
167 // need space for '\0' 152 // need space for '\0'
168 if (snd_pos < MUX_BUF_SIZE-1) 153 if (snd_pos < MUX_BUF_SIZE - 1) buf[snd_pos++] = c;
169 buf[snd_pos++] = c;
170} 154}
171 155
172void iwrap_buf_del(void) 156void iwrap_buf_del(void) {
173{ 157 if (snd_pos) snd_pos--;
174 if (snd_pos)
175 snd_pos--;
176} 158}
177 159
178void iwrap_buf_send(void) 160void iwrap_buf_send(void) {
179{
180 buf[snd_pos] = '\0'; 161 buf[snd_pos] = '\0';
181 snd_pos = 0; 162 snd_pos = 0;
182 iwrap_mux_send(buf); 163 iwrap_mux_send(buf);
183} 164}
184 165
185void iwrap_call(void) 166void iwrap_call(void) {
186{
187 char *p; 167 char *p;
188 168
189 iwrap_mux_send("SET BT PAIR"); 169 iwrap_mux_send("SET BT PAIR");
@@ -193,7 +173,7 @@ void iwrap_call(void)
193 while (!strncmp(p, "SET BT PAIR", 11)) { 173 while (!strncmp(p, "SET BT PAIR", 11)) {
194 p += 7; 174 p += 7;
195 strncpy(p, "CALL", 4); 175 strncpy(p, "CALL", 4);
196 strncpy(p+22, " 11 HID\n\0", 9); 176 strncpy(p + 22, " 11 HID\n\0", 9);
197 print_S(p); 177 print_S(p);
198 iwrap_mux_send(p); 178 iwrap_mux_send(p);
199 // TODO: skip to next line 179 // TODO: skip to next line
@@ -224,20 +204,21 @@ void iwrap_call(void)
224 iwrap_check_connection(); 204 iwrap_check_connection();
225} 205}
226 206
227void iwrap_kill(void) 207void iwrap_kill(void) {
228{
229 char c; 208 char c;
230 iwrap_mux_send("LIST"); 209 iwrap_mux_send("LIST");
231 _delay_ms(500); 210 _delay_ms(500);
232 211
233 while ((c = rcv_deq()) && c != '\n') ; 212 while ((c = rcv_deq()) && c != '\n')
213 ;
234 if (strncmp(rcv_buf + rcv_tail, "LIST ", 5)) { 214 if (strncmp(rcv_buf + rcv_tail, "LIST ", 5)) {
235 print("no connection to kill.\n"); 215 print("no connection to kill.\n");
236 return; 216 return;
237 } 217 }
238 // skip 10 'space' chars 218 // skip 10 'space' chars
239 for (uint8_t i = 10; i; i--) 219 for (uint8_t i = 10; i; i--)
240 while ((c = rcv_deq()) && c != ' ') ; 220 while ((c = rcv_deq()) && c != ' ')
221 ;
241 222
242 char *p = rcv_buf + rcv_tail - 5; 223 char *p = rcv_buf + rcv_tail - 5;
243 strncpy(p, "KILL ", 5); 224 strncpy(p, "KILL ", 5);
@@ -249,47 +230,34 @@ void iwrap_kill(void)
249 iwrap_check_connection(); 230 iwrap_check_connection();
250} 231}
251 232
252void iwrap_unpair(void) 233void iwrap_unpair(void) {
253{
254 iwrap_mux_send("SET BT PAIR"); 234 iwrap_mux_send("SET BT PAIR");
255 _delay_ms(500); 235 _delay_ms(500);
256 236
257 char *p = rcv_buf + rcv_tail; 237 char *p = rcv_buf + rcv_tail;
258 if (!strncmp(p, "SET BT PAIR", 11)) { 238 if (!strncmp(p, "SET BT PAIR", 11)) {
259 strncpy(p+29, "\n\0", 2); 239 strncpy(p + 29, "\n\0", 2);
260 print_S(p); 240 print_S(p);
261 iwrap_mux_send(p); 241 iwrap_mux_send(p);
262 } 242 }
263} 243}
264 244
265void iwrap_sleep(void) 245void iwrap_sleep(void) { iwrap_mux_send("SLEEP"); }
266{
267 iwrap_mux_send("SLEEP");
268}
269 246
270void iwrap_sniff(void) 247void iwrap_sniff(void) {}
271{
272}
273 248
274void iwrap_subrate(void) 249void iwrap_subrate(void) {}
275{
276}
277 250
278bool iwrap_failed(void) 251bool iwrap_failed(void) {
279{
280 if (strncmp(rcv_buf, "SYNTAX ERROR", 12)) 252 if (strncmp(rcv_buf, "SYNTAX ERROR", 12))
281 return true; 253 return true;
282 else 254 else
283 return false; 255 return false;
284} 256}
285 257
286uint8_t iwrap_connected(void) 258uint8_t iwrap_connected(void) { return connected; }
287{
288 return connected;
289}
290 259
291uint8_t iwrap_check_connection(void) 260uint8_t iwrap_check_connection(void) {
292{
293 iwrap_mux_send("LIST"); 261 iwrap_mux_send("LIST");
294 _delay_ms(100); 262 _delay_ms(100);
295 263
@@ -300,44 +268,31 @@ uint8_t iwrap_check_connection(void)
300 return connected; 268 return connected;
301} 269}
302 270
303
304/*------------------------------------------------------------------* 271/*------------------------------------------------------------------*
305 * Host driver 272 * Host driver
306 *------------------------------------------------------------------*/ 273 *------------------------------------------------------------------*/
307static uint8_t keyboard_leds(void); 274static uint8_t keyboard_leds(void);
308static void send_keyboard(report_keyboard_t *report); 275static void send_keyboard(report_keyboard_t *report);
309static void send_mouse(report_mouse_t *report); 276static void send_mouse(report_mouse_t *report);
310static void send_system(uint16_t data); 277static void send_system(uint16_t data);
311static void send_consumer(uint16_t data); 278static void send_consumer(uint16_t data);
312
313static host_driver_t driver = {
314 keyboard_leds,
315 send_keyboard,
316 send_mouse,
317 send_system,
318 send_consumer
319};
320
321host_driver_t *iwrap_driver(void)
322{
323 return &driver;
324}
325 279
326static uint8_t keyboard_leds(void) { 280static host_driver_t driver = {keyboard_leds, send_keyboard, send_mouse, send_system, send_consumer};
327 return 0;
328}
329 281
330static void send_keyboard(report_keyboard_t *report) 282host_driver_t *iwrap_driver(void) { return &driver; }
331{ 283
284static uint8_t keyboard_leds(void) { return 0; }
285
286static void send_keyboard(report_keyboard_t *report) {
332 if (!iwrap_connected() && !iwrap_check_connection()) return; 287 if (!iwrap_connected() && !iwrap_check_connection()) return;
333 MUX_HEADER(0x01, 0x0c); 288 MUX_HEADER(0x01, 0x0c);
334 // HID raw mode header 289 // HID raw mode header
335 xmit(0x9f); 290 xmit(0x9f);
336 xmit(0x0a); // Length 291 xmit(0x0a); // Length
337 xmit(0xa1); // DATA(Input) 292 xmit(0xa1); // DATA(Input)
338 xmit(0x01); // Report ID 293 xmit(0x01); // Report ID
339 xmit(report->mods); 294 xmit(report->mods);
340 xmit(0x00); // reserved byte(always 0) 295 xmit(0x00); // reserved byte(always 0)
341 xmit(report->keys[0]); 296 xmit(report->keys[0]);
342 xmit(report->keys[1]); 297 xmit(report->keys[1]);
343 xmit(report->keys[2]); 298 xmit(report->keys[2]);
@@ -347,16 +302,15 @@ static void send_keyboard(report_keyboard_t *report)
347 MUX_FOOTER(0x01); 302 MUX_FOOTER(0x01);
348} 303}
349 304
350static void send_mouse(report_mouse_t *report) 305static void send_mouse(report_mouse_t *report) {
351{
352#if defined(MOUSEKEY_ENABLE) || defined(PS2_MOUSE_ENABLE) || defined(POINTING_DEVICE_ENABLE) 306#if defined(MOUSEKEY_ENABLE) || defined(PS2_MOUSE_ENABLE) || defined(POINTING_DEVICE_ENABLE)
353 if (!iwrap_connected() && !iwrap_check_connection()) return; 307 if (!iwrap_connected() && !iwrap_check_connection()) return;
354 MUX_HEADER(0x01, 0x09); 308 MUX_HEADER(0x01, 0x09);
355 // HID raw mode header 309 // HID raw mode header
356 xmit(0x9f); 310 xmit(0x9f);
357 xmit(0x07); // Length 311 xmit(0x07); // Length
358 xmit(0xa1); // DATA(Input) 312 xmit(0xa1); // DATA(Input)
359 xmit(0x02); // Report ID 313 xmit(0x02); // Report ID
360 xmit(report->buttons); 314 xmit(report->buttons);
361 xmit(report->x); 315 xmit(report->x);
362 xmit(report->y); 316 xmit(report->y);
@@ -366,18 +320,14 @@ static void send_mouse(report_mouse_t *report)
366#endif 320#endif
367} 321}
368 322
369static void send_system(uint16_t data) 323static void send_system(uint16_t data) { /* not supported */ }
370{
371 /* not supported */
372}
373 324
374static void send_consumer(uint16_t data) 325static void send_consumer(uint16_t data) {
375{
376#ifdef EXTRAKEY_ENABLE 326#ifdef EXTRAKEY_ENABLE
377 static uint16_t last_data = 0; 327 static uint16_t last_data = 0;
378 uint8_t bits1 = 0; 328 uint8_t bits1 = 0;
379 uint8_t bits2 = 0; 329 uint8_t bits2 = 0;
380 uint8_t bits3 = 0; 330 uint8_t bits3 = 0;
381 331
382 if (!iwrap_connected() && !iwrap_check_connection()) return; 332 if (!iwrap_connected() && !iwrap_check_connection()) return;
383 if (data == last_data) return; 333 if (data == last_data) return;
@@ -458,9 +408,9 @@ static void send_consumer(uint16_t data)
458 408
459 MUX_HEADER(0x01, 0x07); 409 MUX_HEADER(0x01, 0x07);
460 xmit(0x9f); 410 xmit(0x9f);
461 xmit(0x05); // Length 411 xmit(0x05); // Length
462 xmit(0xa1); // DATA(Input) 412 xmit(0xa1); // DATA(Input)
463 xmit(0x03); // Report ID 413 xmit(0x03); // Report ID
464 xmit(bits1); 414 xmit(bits1);
465 xmit(bits2); 415 xmit(bits2);
466 xmit(bits3); 416 xmit(bits3);