-
Notifications
You must be signed in to change notification settings - Fork 271
/
Copy pathhidio_com.c
1819 lines (1550 loc) · 42.4 KB
/
hidio_com.c
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2017-2020 by Jacob Alexander
*
* This file is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This file 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this file. If not, see <http://www.gnu.org/licenses/>.
*/
// ----- Includes -----
// Compiler Includes
#include <Lib/OutputLib.h>
// Project Includes
#include <cli.h>
#include <latency.h>
#include <led.h>
#include <output_com.h>
#include <print.h>
#if Output_UARTEnabled_define == 1
#include <arm/uart_serial.h>
#endif
// KLL
#include <kll_defs.h>
#include <kll.h>
// Local Includes
#include "hidio_com.h"
extern const char* UTF8_Strings[];
#include <kll_defs.h>
#if 0
const char* url_strings[] = { URLList_define };
const char* strings_list[] = { StringList_define };
const char* layout_strings[] = { LayoutList_define };
#endif
// ----- Defines -----
#if enableRawIO_define == 1
#if defined(_mk20dx128vlf5_)
#define HIDIO_Max_Payload 200
#define HIDIO_Max_Tx_Payload 1000
#else
#define HIDIO_Max_Payload 200
#define HIDIO_Max_Tx_Payload 8000
#endif
#define HIDIO_Id_List_MaxSize 20
#define HIDIO_Max_ACK_Payload 70
#else
#define HIDIO_Id_List_MaxSize 0
#define HIDIO_Max_ACK_Payload 0
#define HIDIO_Max_Payload 0
#define HIDIO_Max_Tx_Payload 0
#endif
// Enable debugging over ttyACM, UART, or other secondary channel
//#define HIDIO_DEBUG 1
// ----- Macros -----
// ----- Enumerations -----
typedef enum HIDIO_Info_1_Property {
HIDIO_Info_1_Property__Major_Verion = 0,
HIDIO_Info_1_Property__Minor_Verion = 1,
HIDIO_Info_1_Property__Patch_Verion = 2,
HIDIO_Info_1_Property__OS_Type = 3,
HIDIO_Info_1_Property__OS_Version = 4,
HIDIO_Info_1_Property__Host_Software = 4,
} HIDIO_Info_1_Property;
// ----- Structs -----
typedef struct HIDIO_Id_Entry {
uint32_t id;
void *call_func;
void *reply_func;
} HIDIO_Id_Entry;
// ----- Function Declarations -----
// ----- Variables -----
CLIDict_Def( hidioCLIDict, "HID-IO Module Commands" ) = {
{ 0, 0, 0 } // Null entry for dictionary end
};
// Latency Resource
static uint8_t hidioLatencyResource;
// Id List
HIDIO_Id_Entry HIDIO_Id_List[ HIDIO_Id_List_MaxSize ];
uint32_t HIDIO_Id_List_Size;
// Packet information
uint16_t HIDIO_Packet_Size = 0;
// Assembly Ring Buffer
uint8_t HIDIO_assembly_buf_data[HIDIO_Max_Payload + sizeof(HIDIO_Buffer_Entry)];
HIDIO_Buffer HIDIO_assembly_buf;
// ACK Send Buffer
uint8_t HIDIO_ack_send_data[HIDIO_Max_Tx_Payload + sizeof(HIDIO_Buffer_Entry)];
HIDIO_Buffer HIDIO_ack_send_buf;
// ACK Receive Buffer
uint8_t HIDIO_ack_buf_data[HIDIO_Max_ACK_Payload + sizeof(HIDIO_Buffer_Entry)];
HIDIO_Buffer_Entry *HIDIO_ack_buf;
// Packet Tx Buffer
uint8_t HIDIO_tx_buf_data[HIDIO_Max_Tx_Payload + sizeof(HIDIO_Packet)];
HIDIO_Buffer HIDIO_tx_buf;
// VT Print Butter
char HIDIO_print_buf_data[50]; // TODO: Figure out >64
uint8_t HIDIO_print_buf_count;
uint8_t HIDIO_print_buffer_mode;
uint8_t HIDIO_VT_Connected;
// Incoming Trigger Event Buffer
extern TriggerEvent macroTriggerEventBuffer[];
extern var_uint_t macroTriggerEventBufferSize;
extern var_uint_t macroTriggerEventLayerCache[];
// ----- Capabilities -----
// ----- Utility Functions -----
// Munch off the given length of the HIDIO packet buffer
// Does not modify head or tail pointers
// buf - Must pass an array of at least len in size
// Sometimes an allocation is not necessary and the buffer memory is used "in-place"
// If there is a wrap-around, the buffer is copied.
// buf_pos - Index position in the buffer
// len - Length of the desired buffer
typedef struct HIDIO_Munch {
uint8_t *buf; // Pointer to memory of packet
uint16_t next_pos; // Position of next packet
} HIDIO_Munch;
//
// A pointer the data is returned, which may or may not be the provided buffer
HIDIO_Munch HIDIO_buffer_munch( HIDIO_Buffer *buffer, uint8_t *buf, uint16_t buf_pos, uint16_t len )
{
HIDIO_Munch munch;
// Determine if buffer is contiguous for the length
if ( buf_pos + len < buffer->len )
{
// We can just set the buffer directly
munch.buf = &(buffer->data[ buf_pos ]);
munch.next_pos = buf_pos + len;
return munch;
}
// If just a single byte, just return the first position (wrap-around)
if ( len == 1 )
{
munch.buf = &(buffer->data[0]);
munch.next_pos = buf_pos + 1;
return munch;
}
// Copy into the buffer
uint16_t cur_len = buffer->len - buf_pos;
memcpy( buf, &(buffer->data[ buf_pos ]), cur_len );
memcpy( &buf[ cur_len ], buffer->data, len - cur_len );
munch.buf = buf;
munch.next_pos = len - cur_len;
return munch;
}
// Push byte to ring buffer
// XXX (HaaTa): Does not check if full, that needs to be validated ahead of time
void HIDIO_buffer_push_byte( HIDIO_Buffer *buffer, uint8_t byte )
{
// Check if wrap-around case
if ( buffer->tail == buffer->len )
{
buffer->tail = 0;
}
// Add byte at tail
buffer->data[ buffer->tail++ ] = byte;
}
// Modify buffer in place
// XXX (HaaTa): Does not check tail bounds, responsibility of caller to verify bonuds are correct
void HIDIO_modify_buffer( HIDIO_Buffer *buffer, uint16_t start, uint8_t *data, uint16_t len )
{
for ( uint16_t c = 0; c < len; c++ )
{
uint16_t pos = c + start;
// Wrap-around case
if ( pos >= buffer->len )
{
pos -= buffer->len;
}
buffer->data[pos] = data[c];
}
}
// Pop bytes from ring buffer, just drops them, nothing returned
// XXX (HaaTa): Does not check if empty, that needs to be validated ahead of time
uint8_t HIDIO_buffer_pop_bytes( HIDIO_Buffer *buffer, uint16_t len )
{
// Check if len is longer than total buffer
if ( len > buffer->len )
{
uint16_t connected = HIDIO_VT_Connected;
HIDIO_VT_Connected = 0;
erro_print("Requested HIDIO buffer pop larger than entire buffer: ");
printInt16( len );
print(":");
printInt16( buffer->len );
print(NL);
HIDIO_VT_Connected = connected;
return 0;
}
// Adjust head position, check for wrap-around
if ( len + buffer->head > buffer->len )
{
buffer->head = len + buffer->head - buffer->len;
}
else
{
buffer->head += len;
}
return 1;
}
// Determines max payload with given id width
uint16_t HIDIO_max_payload( uint8_t id_width )
{
return HIDIO_Packet_Size - sizeof(HIDIO_Packet) - id_width;
}
// Determines available space in ring buffer
uint16_t HIDIO_buffer_free_bytes( HIDIO_Buffer *buffer )
{
if ( buffer->head <= buffer->tail )
{
return buffer->len - (buffer->tail - buffer->head);
}
else
{
return buffer->head - buffer->tail;
}
}
// Determines buffer position (index) after given length
// To munch the buffer, we need the starting buffer position.
// This function gives the position after a given length, handles wrap-around for you.
uint16_t HIDIO_buffer_position( HIDIO_Buffer *buffer, uint16_t cur_pos, uint16_t distance )
{
if ( cur_pos + distance >= buffer->len )
{
return buffer->len - (cur_pos + distance);
}
else
{
return cur_pos + distance;
}
}
// Get id from data stream
// May be either 16 bits or 32 bits wide
uint32_t HIDIO_buffer_id( HIDIO_Packet *packet )
{
uint32_t id = 0;
// 16 bit Id
if ( packet->id_width == 0 )
{
HIDIO_Packet16 *pkt = (HIDIO_Packet16*)packet;
id = pkt->id;
}
// 32 bit Id
else
{
HIDIO_Packet32 *pkt = (HIDIO_Packet32*)packet;
id = pkt->id;
}
return id;
}
// Get data payload start of packet
uint8_t *HIDIO_payload_start( HIDIO_Packet *packet )
{
uint8_t *data = 0;
// 16 bit Id
if ( packet->id_width == 0 )
{
HIDIO_Packet16 *pkt = (HIDIO_Packet16*)packet;
data = pkt->data;
}
// 32 bit Id
else
{
HIDIO_Packet32 *pkt = (HIDIO_Packet32*)packet;
data = pkt->data;
}
return data;
}
// Return width in bytes
uint8_t HIDIO_buffer_id_width( uint8_t id_width )
{
// If 1, 32bit; 0, 16bit
return id_width ? 4 : 2;
}
// Calculate Id width in bytes
uint8_t HIDIO_id_width( uint32_t id )
{
// Calculate width of id
uint8_t width = 0;
if ( id <= 0xFFFF )
{
width = 2;
}
else
{
width = 4;
}
return width;
}
// Generate packet
// This function can be called multiple times to generate a full packet
// Continue to call until the value returned equals payload_len
// If 0 is returned, there has been an error, and the packet is aborted.
// To start a new packet, start at pos = 0
uint16_t HIDIO_buffer_generate_packet(
HIDIO_Buffer *buf, // Buffer to use
uint16_t pos, // Position in the packet
uint16_t payload_len, // Total length of payload (if larger than packet size, will be a continued packet)
uint8_t *data, // Pointer to data being copied into buffer
uint16_t data_len, // Length of buffer being copied in
HIDIO_Packet_Type type, // Type of packet
uint32_t id // Packet id
)
{
#if HIDIO_DEBUG
uint16_t connected = HIDIO_VT_Connected;
HIDIO_VT_Connected = 0;
print("head: ");
printInt16( buf->head );
print(" tail: ");
printInt16( buf->tail );
print(" bytes_left: ");
printInt16( HIDIO_buffer_free_bytes( buf ) );
print(" request: ");
printInt16( data_len );
print(NL);
HIDIO_VT_Connected = connected;
#endif
// Determine payload max
uint8_t width = HIDIO_id_width( id );
uint16_t max_payload = HIDIO_max_payload( width );
// Number of packets and current packet number
// Ignore calculation for zero-length packet
uint16_t packet_count = 1;
uint16_t cur_packet = 0;
if ( payload_len != 0 )
{
packet_count = payload_len / max_payload;
packet_count += payload_len % max_payload != 0 ? 1 : 0;
cur_packet = pos / max_payload;
cur_packet += pos % max_payload != 0 ? 1 : 0;
}
// Determine if there is enough room in tx buffer
uint16_t requested = payload_len - pos + sizeof(HIDIO_Packet) * ( packet_count - cur_packet );
if ( requested > HIDIO_buffer_free_bytes( buf ) )
{
uint16_t connected = HIDIO_VT_Connected;
HIDIO_VT_Connected = 0;
erro_print("Not enough bytes in HIDIO buffer: ");
printInt16( HIDIO_buffer_free_bytes( buf ) );
print(" bytes left, ");
printInt16( buf->len );
print(" bytes total ");
printInt16( requested );
print(" bytes requested");
print(NL);
HIDIO_flush();
HIDIO_VT_Connected = connected;
return payload_len;
}
// Generate a payload of given length, repeating the payload value
// Also proceed if building a zero-length packet
for ( ; pos < payload_len || payload_len == 0; pos++ )
{
// Check if we need to start a new packet
uint16_t bytes_left = pos % max_payload;
if ( bytes_left == 0 )
{
// Reset bytes left
bytes_left = max_payload;
// Start of a new packet, signal previous one is ready
// Do not increment for the very first packet in the sequence
if ( pos != 0 )
{
buf->packets_ready++;
}
// Increment current packet counter
cur_packet++;
// Determine length of this new packet
// Start with total bytes left, plus id width
uint16_t packet_len = bytes_left + width;
// If larger than payload_len, reduce
uint16_t cur_payload_len = payload_len - pos + width;
if ( packet_len > cur_payload_len )
{
packet_len = cur_payload_len;
}
// Get info for packet header
uint8_t p_type = cur_packet == 1 ? type : HIDIO_Packet_Type__Continued;
uint8_t p_cont = cur_packet == packet_count ? 0 : 1;
// Start new packet and copy into buffer
// Use 16 bit Ids if possible (more efficient)
if ( id <= 0xFFFF )
{
HIDIO_Packet16 packet = {
.type = p_type,
.cont = p_cont,
.id = (uint16_t)id,
.id_width = 0,
.upper_len = (packet_len >> 8) & 0x3,
.len = (packet_len & 0xFF),
};
// Copy packet header data to buffer
for ( uint8_t byte = 0; byte < sizeof(HIDIO_Packet16); byte++ )
{
HIDIO_buffer_push_byte( buf, ((uint8_t*)&packet)[ byte ] );
}
// There's always enough room for header
bytes_left -= sizeof(HIDIO_Packet16);
}
else
{
HIDIO_Packet32 packet = {
.type = p_type,
.cont = p_cont,
.id = id,
.id_width = 1,
.upper_len = (packet_len >> 8) & 0x3,
.len = (packet_len & 0xFF),
};
// Copy packet header data to buffer
for ( uint8_t byte = 0; byte < sizeof(HIDIO_Packet32); byte++ )
{
HIDIO_buffer_push_byte( buf, ((uint8_t*)&packet)[ byte ] );
}
// There's always enough room for header
bytes_left -= sizeof(HIDIO_Packet32);
}
}
// Push payload
// Monitor:
// - bytes_left (payload left in packet)
// - data_len (input data buffer length)
// - Current overall payload position
uint16_t byte = 0;
for ( ; byte < data_len && bytes_left > 0; byte++, pos++, bytes_left-- )
{
HIDIO_buffer_push_byte( buf, data[ byte ] );
}
// If we are out of data in the data buffer, return the current position
if ( byte >= data_len )
{
break;
}
}
// Finished packet
if ( pos == payload_len )
{
buf->packets_ready++;
}
// Current current (or final) position in the payload
return pos;
}
void HIDIO_nopayload_ack( uint32_t id )
{
// Generate a no payload ACK
HIDIO_buffer_generate_packet(
&HIDIO_ack_send_buf,
0,
0,
0,
0,
HIDIO_Packet_Type__ACK,
id
);
}
// ----- Internal Id Functions -----
// Supported Ids Request
void HIDIO_supported_0_request()
{
// TODO
}
// Supported Ids call
HIDIO_Return HIDIO_supported_0_call( uint16_t buf_pos, uint8_t irq )
{
// TODO
return HIDIO_Return__Ok;
}
// Supported Ids reply
HIDIO_Return HIDIO_supported_0_reply( HIDIO_Buffer_Entry *buf, uint8_t irq )
{
// TODO
return HIDIO_Return__Ok;
}
// Info Request
void HIDIO_info_1_request( uint8_t property )
{
// TODO
}
// Info call
HIDIO_Return HIDIO_info_1_call( uint16_t buf_pos, uint8_t irq )
{
// TODO
#if 0
// TODO (HaaTa) - Add option to process optionally inside irqs
if ( irq )
{
return HIDIO_Return__Delay;
}
// Munch buffer entry header, not including data
uint8_t tmpbuf[ sizeof(HIDIO_Buffer_Entry) ];
uint8_t *buf = HIDIO_buffer_munch( &HIDIO_assembly_buf, (uint8_t*)&tmpbuf, buf_pos, sizeof(HIDIO_Buffer_Entry) );
HIDIO_Buffer_Entry *entry = (HIDIO_Buffer_Entry*)buf;
// Make sure entry is ready
if ( !entry->done )
{
return HIDIO_Return__Delay;
}
// Get size and iterate through payload
uint16_t transitions = 0;
uint8_t last_byte = 0;
uint8_t prev_buf_pos = CLILineBufferCurrent;
for ( uint16_t pos = 0; pos < entry->size; pos++ )
{
uint16_t pos = 0;
uint16_t calc_buf_pos = HIDIO_buffer_position( &HIDIO_assembly_buf, buf_pos + sizeof(HIDIO_Buffer_Entry), pos );
uint8_t *byte = HIDIO_buffer_munch( &HIDIO_assembly_buf, &buf, calc_buf_pos, 1 );
switch (*byte) {
case HIDIO_Info_1_Property__Major_Verion:
break;
case HIDIO_Info_1_Property__Minor_Verion:
break;
case HIDIO_Info_1_Property__Patch_Verion:
break;
case HIDIO_Info_1_Property__OS_Type:
break;
case HIDIO_Info_1_Property__OS_Version:
break;
}
}
// Prepare ACK
uint16_t pos = 0;
while ( pos < entry->size )
{
pos = HIDIO_buffer_generate_packet(
&HIDIO_ack_send_buf,
pos,
entry->size,
&last_byte,
1,
HIDIO_Packet_Type__ACK,
2
);
}
#endif
// Buffer is automatically released for us
return HIDIO_Return__Ok;
}
// Info reply
HIDIO_Return HIDIO_info_1_reply( HIDIO_Buffer_Entry *buf, uint8_t irq )
{
// TODO
return HIDIO_Return__Ok;
}
// Test Packet
void HIDIO_test_2_request( uint16_t payload_len, uint8_t payload_value )
{
uint16_t pos = 0;
// Push the same byte, payload_len times to the tx buffer
while ( pos < payload_len )
{
pos = HIDIO_buffer_generate_packet(
&HIDIO_tx_buf,
pos,
payload_len,
&payload_value,
1,
HIDIO_Packet_Type__Data,
2
);
}
}
// Test call
HIDIO_Return HIDIO_test_2_call( uint16_t buf_pos, uint8_t irq )
{
// TODO (HaaTa) - Add option to process optionally inside irqs
if ( irq )
{
return HIDIO_Return__Delay;
}
// Munch buffer entry header, not including data
uint8_t tmpbuf[ sizeof(HIDIO_Buffer_Entry) ];
uint8_t *buf = HIDIO_buffer_munch( &HIDIO_assembly_buf, (uint8_t*)&tmpbuf, buf_pos, sizeof(HIDIO_Buffer_Entry) ).buf;
HIDIO_Buffer_Entry *entry = (HIDIO_Buffer_Entry*)buf;
// Make sure entry is ready
if ( !entry->done )
{
return HIDIO_Return__Delay;
}
// Get size and iterate through payload
uint16_t transitions = 0;
uint8_t last_byte = 0;
for ( uint16_t pos = 0; pos < entry->size; pos++ )
{
uint8_t buf;
uint16_t calc_buf_pos = HIDIO_buffer_position( &HIDIO_assembly_buf, buf_pos + sizeof(HIDIO_Buffer_Entry), pos );
uint8_t *byte = HIDIO_buffer_munch( &HIDIO_assembly_buf, &buf, calc_buf_pos, 1 ).buf;
// Count transitions, should only be 1, from 0 to value
if ( *byte != last_byte )
{
transitions++;
last_byte = *byte;
}
}
// Check if there was any data corruption
if ( transitions > 1 )
{
// This will automatically NAK for us
return HIDIO_Return__InBuffer_Fail;
}
// TODO (HaaTa): Delay/segment transfer if ACK send buffer is not large enough
// Prepare ACK
uint16_t pos = 0;
while ( pos < entry->size )
{
pos = HIDIO_buffer_generate_packet(
&HIDIO_ack_send_buf,
pos,
entry->size,
&last_byte,
1,
HIDIO_Packet_Type__ACK,
2
);
}
// Buffer is automatically released for us
//print("[TEST OK]" NL);
return HIDIO_Return__Ok;
}
// Test reply
HIDIO_Return HIDIO_test_2_reply( HIDIO_Buffer_Entry *buf, uint8_t irq )
{
// TODO (HaaTa) - Add option to process optionally inside irqs
if ( irq )
{
return HIDIO_Return__Delay;
}
// Make sure entry is ready
if ( !buf->done )
{
return HIDIO_Return__Delay;
}
// Get size and iterate through payload, start after id
uint16_t transitions = 0;
uint8_t last_byte = 0;
for ( uint16_t pos = 0; pos < buf->size; pos++ )
{
uint8_t byte = buf->data[ pos ];
// Count transitions, should only be 1, from 0 to value
if ( byte != last_byte )
{
transitions++;
last_byte = byte;
}
}
// Check if there was any data corruption
if ( transitions > 1 )
{
// This will automatically NAK for us
return HIDIO_Return__InBuffer_Fail;
}
// Buffer is automatically released for us
return HIDIO_Return__Ok;
}
HIDIO_Return HIDIO_terminal_call( uint16_t buf_pos, uint8_t irq )
{
HIDIO_VT_Connected = 1;
// TODO (HaaTa) - Add option to process optionally inside irqs
if ( irq )
{
return HIDIO_Return__Delay;
}
// Munch buffer entry header, not including data
uint8_t tmpbuf[ sizeof(HIDIO_Buffer_Entry) ];
uint8_t *buf = HIDIO_buffer_munch( &HIDIO_assembly_buf, (uint8_t*)&tmpbuf, buf_pos, sizeof(HIDIO_Buffer_Entry) ).buf;
HIDIO_Buffer_Entry *entry = (HIDIO_Buffer_Entry*)buf;
// Make sure entry is ready
if ( !entry->done )
{
return HIDIO_Return__Delay;
}
// Get size and iterate through payload
uint8_t last_byte = 0;
uint8_t prev_buf_pos = CLILineBufferCurrent;
for ( uint16_t pos = 0; pos < entry->size; pos++ )
{
if ( CLILineBufferCurrent >= CLILineBufferMaxSize ) {
uint16_t connected = HIDIO_VT_Connected;
HIDIO_VT_Connected = 0;
erro_printNL("Serial line buffer is full, dropping character and resetting...");
HIDIO_VT_Connected = connected;
// This will automatically NAK for us
return HIDIO_Return__InBuffer_Fail;
}
uint8_t buf;
uint16_t calc_buf_pos = HIDIO_buffer_position( &HIDIO_assembly_buf, buf_pos + sizeof(HIDIO_Buffer_Entry), pos );
uint8_t *byte = HIDIO_buffer_munch( &HIDIO_assembly_buf, &buf, calc_buf_pos, 1 ).buf;
CLILineBuffer[CLILineBufferCurrent++] = *byte;
}
// Prepare ACK
uint16_t pos = 0;
while ( pos < entry->size )
{
pos = HIDIO_buffer_generate_packet(
&HIDIO_ack_send_buf,
pos,
entry->size,
&last_byte,
1,
HIDIO_Packet_Type__ACK,
2
);
}
// Flag CLI as updated
CLILineBufferPrev = prev_buf_pos;
// Buffer is automatically released for us
return HIDIO_Return__Ok;
}
// Test reply
HIDIO_Return HIDIO_terminal_reply( HIDIO_Buffer_Entry *buf, uint8_t irq )
{
return HIDIO_Return__Ok;
}
// Invalid Id Request
void HIDIO_invalid_65535_request()
{
// Generate invalid Id 0-length data packet
HIDIO_buffer_generate_packet(
&HIDIO_ack_send_buf,
0,
0,
0,
0,
HIDIO_Packet_Type__Data,
65535
);
}
// ----- Functions -----
// Register HID-IO callbacks
// If an Id is not registered, it is ignored and automatically NAK'd
void HIDIO_register_id( uint32_t id, void* incoming_call_func, void* incoming_reply_func )
{
// Check if there is any room left in the list
if ( HIDIO_Id_List_Size >= HIDIO_Id_List_MaxSize )
{
erro_print("HIDIO_Id_List is full, cannot register Id: ");
printInt32( id );
print( NL );
return;
}
// Add id to unsorted list (no reason to sort, as Ids may not be contiguous)
HIDIO_Id_Entry *entry = &HIDIO_Id_List[ HIDIO_Id_List_Size++ ];
entry->id = id;
entry->call_func = incoming_call_func;
entry->reply_func = incoming_reply_func;
}
// Initiate registered call function
// id - Function id
// buf_pos - Index in ring buffer
// irq - Set to 1 if called from an IRQ
HIDIO_Return HIDIO_call_id( uint32_t id, uint16_t buf_pos, uint8_t irq )
{
#ifdef HIDIO_DEBUG
print("HIDIO CALL: ");
printInt8(id);
print(NL);
#endif
HIDIO_Return retval = HIDIO_Return__Unknown;
// Find id
for ( uint16_t pos = 0; pos < HIDIO_Id_List_Size; pos++ )
{
// Match id
if ( HIDIO_Id_List[ pos ].id == id )
{
// Map function pointer
HIDIO_Return (*func)(uint16_t, uint8_t) = \
(HIDIO_Return(*)(uint16_t, uint8_t))(HIDIO_Id_List[ pos ].call_func);
// Call function
retval = func( buf_pos, irq );
}
}
// Enough space to store header
uint8_t tmpdata[sizeof(HIDIO_Buffer_Entry)];
uint16_t datasize;
HIDIO_Buffer_Entry *entry;
switch ( retval )
{
// HIDIO_Return__Ok, we can pop the most recent assembly_buf packet
case HIDIO_Return__Ok:
entry = (HIDIO_Buffer_Entry*)HIDIO_buffer_munch( &HIDIO_assembly_buf, tmpdata, HIDIO_assembly_buf.head, sizeof(tmpdata) ).buf;
// Determine size of data
datasize = sizeof(HIDIO_Buffer_Entry) + entry->size;
// Pop bytes and decrement packet ready counter
if ( HIDIO_buffer_pop_bytes( &HIDIO_assembly_buf, datasize ) )
{
HIDIO_assembly_buf.packets_ready--;
}
// Failed pop, generally popping more buffer than is available
// (this is very bad, but recovering anyways)
else
{
HIDIO_assembly_buf.packets_ready = 0;
HIDIO_assembly_buf.head = 0;
HIDIO_assembly_buf.tail = 0;
}
// Unset waiting for ACK packet
HIDIO_assembly_buf.waiting = 0;
break;
// HIDIO_Return__InBuffer_Fail, Nak (or related), drop all continued packets if necessary
case HIDIO_Return__InBuffer_Fail:
// TODO (HaaTa)
print("FAIL"NL);
break;
default:
break;
}
return retval;
}
// Initiate registered reply function
// id - Function id
// buf - Pointer to the ack buffer
// irq - Set to 1 if called from an IRQ
HIDIO_Return HIDIO_reply_id( uint32_t id, uint8_t *buf, uint8_t irq )
{
HIDIO_Return retval = HIDIO_Return__Unknown;
// Find id
for ( uint16_t pos = 0; pos < HIDIO_Id_List_Size; pos++ )
{
// Match id
if ( HIDIO_Id_List[ pos ].id == id )
{
// Map function pointer
HIDIO_Return (*func)(HIDIO_Buffer_Entry*, uint8_t) = \
(HIDIO_Return(*)(HIDIO_Buffer_Entry*, uint8_t))(HIDIO_Id_List[ pos ].reply_func);
// Call function
retval = func( (HIDIO_Buffer_Entry*)buf, irq );
break;
}
}
// Enough space to store header
uint8_t tmpdata[6];
uint16_t datasize;
uint8_t continued;
HIDIO_Packet *packet;
switch ( retval )
{
// HIDIO_Return__Ok, we can pop the most recent tx_buf packet
case HIDIO_Return__Ok:
// Cleanup until a non-continued packet
while ( HIDIO_tx_buf.packets_ready > 0 )
{
packet = (HIDIO_Packet*)HIDIO_buffer_munch( &HIDIO_tx_buf, tmpdata, HIDIO_tx_buf.head, sizeof(tmpdata) ).buf;
// Determine size of data
datasize = (packet->upper_len << 8) | packet->len;
// Check if continued