@@ -144,29 +144,41 @@ extern var_uint_t macroTriggerEventLayerCache[];
144
144
// If there is a wrap-around, the buffer is copied.
145
145
// buf_pos - Index position in the buffer
146
146
// len - Length of the desired buffer
147
+ typedef struct HIDIO_Munch {
148
+ uint8_t * buf ; // Pointer to memory of packet
149
+ uint16_t next_pos ; // Position of next packet
150
+ } HIDIO_Munch ;
147
151
//
148
152
// A pointer the data is returned, which may or may not be the provided buffer
149
- uint8_t * HIDIO_buffer_munch ( HIDIO_Buffer * buffer , uint8_t * buf , uint16_t buf_pos , uint16_t len )
153
+ HIDIO_Munch HIDIO_buffer_munch ( HIDIO_Buffer * buffer , uint8_t * buf , uint16_t buf_pos , uint16_t len )
150
154
{
155
+ HIDIO_Munch munch ;
156
+
151
157
// Determine if buffer is contiguous for the length
152
158
if ( buf_pos + len < buffer -> len )
153
159
{
154
160
// We can just set the buffer directly
155
- return & (buffer -> data [ buf_pos ]);
161
+ munch .buf = & (buffer -> data [ buf_pos ]);
162
+ munch .next_pos = buf_pos + len ;
163
+ return munch ;
156
164
}
157
165
158
166
// If just a single byte, just return the first position (wrap-around)
159
167
if ( len == 1 )
160
168
{
161
- return & (buffer -> data [0 ]);
169
+ munch .buf = & (buffer -> data [0 ]);
170
+ munch .next_pos = buf_pos + 1 ;
171
+ return munch ;
162
172
}
163
173
164
174
// Copy into the buffer
165
175
uint16_t cur_len = buffer -> len - buf_pos ;
166
176
memcpy ( buf , & (buffer -> data [ buf_pos ]), cur_len );
167
177
memcpy ( & buf [ cur_len ], buffer -> data , len - cur_len );
168
178
169
- return buf ;
179
+ munch .buf = buf ;
180
+ munch .next_pos = len - cur_len ;
181
+ return munch ;
170
182
}
171
183
172
184
// Push byte to ring buffer
@@ -334,28 +346,30 @@ uint8_t HIDIO_id_width( uint32_t id )
334
346
335
347
// Generate packet
336
348
// This function can be called multiple times to generate a full packet
337
- // Continue to call unitil the value returned equals payload_len
349
+ // Continue to call until the value returned equals payload_len
338
350
// If 0 is returned, there has been an error, and the packet is aborted.
339
351
// To start a new packet, start at pos = 0
340
352
uint16_t HIDIO_buffer_generate_packet (
341
- HIDIO_Buffer * buf ,
342
- uint16_t pos ,
343
- uint16_t payload_len ,
344
- uint8_t * data ,
345
- uint16_t data_len ,
346
- HIDIO_Packet_Type type ,
347
- uint32_t id
353
+ HIDIO_Buffer * buf , // Buffer to use
354
+ uint16_t pos , // Position in the packet
355
+ uint16_t payload_len , // Total length of payload (if larger than packet size, will be a continued packet)
356
+ uint8_t * data , // Pointer to data being copied into buffer
357
+ uint16_t data_len , // Length of buffer being copied in
358
+ HIDIO_Packet_Type type , // Type of packet
359
+ uint32_t id // Packet id
348
360
)
349
361
{
350
- /*print("head: ");
362
+ /*
363
+ print("head: ");
351
364
printInt16( buf->head );
352
365
print(" tail: ");
353
366
printInt16( buf->tail );
354
367
print(" bytes_left: ");
355
368
printInt16( HIDIO_buffer_free_bytes( buf ) );
356
369
print(" request: ");
357
370
printInt16( data_len );
358
- print(NL);*/
371
+ print(NL);
372
+ */
359
373
360
374
// Determine payload max
361
375
uint8_t width = HIDIO_id_width ( id );
@@ -651,7 +665,7 @@ HIDIO_Return HIDIO_test_2_call( uint16_t buf_pos, uint8_t irq )
651
665
652
666
// Munch buffer entry header, not including data
653
667
uint8_t tmpbuf [ sizeof (HIDIO_Buffer_Entry ) ];
654
- uint8_t * buf = HIDIO_buffer_munch ( & HIDIO_assembly_buf , (uint8_t * )& tmpbuf , buf_pos , sizeof (HIDIO_Buffer_Entry ) );
668
+ uint8_t * buf = HIDIO_buffer_munch ( & HIDIO_assembly_buf , (uint8_t * )& tmpbuf , buf_pos , sizeof (HIDIO_Buffer_Entry ) ). buf ;
655
669
HIDIO_Buffer_Entry * entry = (HIDIO_Buffer_Entry * )buf ;
656
670
657
671
// Make sure entry is ready
@@ -667,7 +681,7 @@ HIDIO_Return HIDIO_test_2_call( uint16_t buf_pos, uint8_t irq )
667
681
{
668
682
uint8_t buf ;
669
683
uint16_t calc_buf_pos = HIDIO_buffer_position ( & HIDIO_assembly_buf , buf_pos + sizeof (HIDIO_Buffer_Entry ), pos );
670
- uint8_t * byte = HIDIO_buffer_munch ( & HIDIO_assembly_buf , & buf , calc_buf_pos , 1 );
684
+ uint8_t * byte = HIDIO_buffer_munch ( & HIDIO_assembly_buf , & buf , calc_buf_pos , 1 ). buf ;
671
685
672
686
// Count transitions, should only be 1, from 0 to value
673
687
if ( * byte != last_byte )
@@ -759,7 +773,7 @@ HIDIO_Return HIDIO_terminal_call( uint16_t buf_pos, uint8_t irq )
759
773
760
774
// Munch buffer entry header, not including data
761
775
uint8_t tmpbuf [ sizeof (HIDIO_Buffer_Entry ) ];
762
- uint8_t * buf = HIDIO_buffer_munch ( & HIDIO_assembly_buf , (uint8_t * )& tmpbuf , buf_pos , sizeof (HIDIO_Buffer_Entry ) );
776
+ uint8_t * buf = HIDIO_buffer_munch ( & HIDIO_assembly_buf , (uint8_t * )& tmpbuf , buf_pos , sizeof (HIDIO_Buffer_Entry ) ). buf ;
763
777
HIDIO_Buffer_Entry * entry = (HIDIO_Buffer_Entry * )buf ;
764
778
765
779
// Make sure entry is ready
@@ -786,7 +800,7 @@ HIDIO_Return HIDIO_terminal_call( uint16_t buf_pos, uint8_t irq )
786
800
787
801
uint8_t buf ;
788
802
uint16_t calc_buf_pos = HIDIO_buffer_position ( & HIDIO_assembly_buf , buf_pos + sizeof (HIDIO_Buffer_Entry ), pos );
789
- uint8_t * byte = HIDIO_buffer_munch ( & HIDIO_assembly_buf , & buf , calc_buf_pos , 1 );
803
+ uint8_t * byte = HIDIO_buffer_munch ( & HIDIO_assembly_buf , & buf , calc_buf_pos , 1 ). buf ;
790
804
CLILineBuffer [CLILineBufferCurrent ++ ] = * byte ;
791
805
}
792
806
@@ -895,7 +909,7 @@ HIDIO_Return HIDIO_call_id( uint32_t id, uint16_t buf_pos, uint8_t irq )
895
909
{
896
910
// HIDIO_Return__Ok, we can pop the most recent assembly_buf packet
897
911
case HIDIO_Return__Ok :
898
- entry = (HIDIO_Buffer_Entry * )HIDIO_buffer_munch ( & HIDIO_assembly_buf , tmpdata , HIDIO_assembly_buf .head , sizeof (tmpdata ) );
912
+ entry = (HIDIO_Buffer_Entry * )HIDIO_buffer_munch ( & HIDIO_assembly_buf , tmpdata , HIDIO_assembly_buf .head , sizeof (tmpdata ) ). buf ;
899
913
900
914
// Determine size of data
901
915
datasize = sizeof (HIDIO_Buffer_Entry ) + entry -> size ;
@@ -958,29 +972,44 @@ HIDIO_Return HIDIO_reply_id( uint32_t id, uint8_t *buf, uint8_t irq )
958
972
// Enough space to store header
959
973
uint8_t tmpdata [6 ];
960
974
uint16_t datasize ;
975
+ uint8_t continued ;
961
976
HIDIO_Packet * packet ;
962
977
963
978
switch ( retval )
964
979
{
965
980
// HIDIO_Return__Ok, we can pop the most recent tx_buf packet
966
981
case HIDIO_Return__Ok :
967
- packet = (HIDIO_Packet * )HIDIO_buffer_munch ( & HIDIO_tx_buf , tmpdata , HIDIO_tx_buf .head , sizeof (tmpdata ) );
982
+ // Cleanup until a non-continued packet
983
+ while ( HIDIO_tx_buf .packets_ready > 0 )
984
+ {
985
+ packet = (HIDIO_Packet * )HIDIO_buffer_munch ( & HIDIO_tx_buf , tmpdata , HIDIO_tx_buf .head , sizeof (tmpdata ) ).buf ;
968
986
969
- // Determine size of data
970
- datasize = (packet -> upper_len << 8 ) | packet -> len ;
987
+ // Determine size of data
988
+ datasize = (packet -> upper_len << 8 ) | packet -> len ;
971
989
972
- // Pop bytes and decrement packet ready counter
973
- if ( HIDIO_buffer_pop_bytes ( & HIDIO_tx_buf , datasize + 2 ) )
974
- {
975
- HIDIO_tx_buf .packets_ready -- ;
976
- }
977
- // Failed pop, generally popping more buffer than is available
978
- // (this is very bad, but recovering anyways)
979
- else
980
- {
981
- HIDIO_tx_buf .packets_ready = 0 ;
982
- HIDIO_tx_buf .head = 0 ;
983
- HIDIO_tx_buf .tail = 0 ;
990
+ // Check if continued
991
+ continued = packet -> cont ;
992
+
993
+ // Pop bytes and decrement packet ready counter
994
+ if ( HIDIO_buffer_pop_bytes ( & HIDIO_tx_buf , datasize + 2 ) )
995
+ {
996
+ HIDIO_tx_buf .packets_ready -- ;
997
+ }
998
+ // Failed pop, generally popping more buffer than is available
999
+ // (this is very bad, but recovering anyways)
1000
+ else
1001
+ {
1002
+ HIDIO_tx_buf .packets_ready = 0 ;
1003
+ HIDIO_tx_buf .head = 0 ;
1004
+ HIDIO_tx_buf .tail = 0 ;
1005
+ break ;
1006
+ }
1007
+
1008
+ // If there aren't any more packets (continued packets only), stop
1009
+ if ( !continued )
1010
+ {
1011
+ break ;
1012
+ }
984
1013
}
985
1014
986
1015
// Unset waiting for ACK packet
@@ -1151,7 +1180,7 @@ void HIDIO_process_incoming_packet( uint8_t *buf, uint8_t irq )
1151
1180
// We first determine if we're reassembling in the data or ack buffers
1152
1181
case HIDIO_Packet_Type__Continued :
1153
1182
// Modify type so we know what to do with the payload
1154
- type = ! HIDIO_ack_buf -> done ? HIDIO_Packet_Type__Data : HIDIO_Packet_Type__ACK ;
1183
+ type = HIDIO_assembly_buf . waiting ? HIDIO_Packet_Type__Data : HIDIO_Packet_Type__ACK ;
1155
1184
1156
1185
// Most packet types
1157
1186
default :
@@ -1223,7 +1252,7 @@ void HIDIO_process_incoming_packet( uint8_t *buf, uint8_t irq )
1223
1252
tmpbuf ,
1224
1253
HIDIO_assembly_buf .cur_buf_head ,
1225
1254
sizeof (tmpbuf )
1226
- );
1255
+ ). buf ;
1227
1256
1228
1257
// Update entry
1229
1258
entry -> size += payload_len ;
@@ -1283,11 +1312,6 @@ void HIDIO_process_incoming_packet( uint8_t *buf, uint8_t irq )
1283
1312
break ;
1284
1313
}
1285
1314
}
1286
- // Otherwise do a simple ACK
1287
- else
1288
- {
1289
- //HIDIO_nopayload_ack( id );
1290
- }
1291
1315
break ;
1292
1316
1293
1317
case HIDIO_Packet_Type__ACK :
@@ -1308,7 +1332,6 @@ void HIDIO_process_incoming_packet( uint8_t *buf, uint8_t irq )
1308
1332
HIDIO_reply_id ( id , (uint8_t * )HIDIO_ack_buf , irq );
1309
1333
}
1310
1334
break ;
1311
- break ;
1312
1335
1313
1336
default :
1314
1337
// TODO (HaaTa)
@@ -1355,7 +1378,7 @@ inline void HIDIO_process()
1355
1378
// Prepare 64 byte packet
1356
1379
// TODO (HaaTa): Handle internal max size
1357
1380
uint8_t tmpdata [64 ];
1358
- uint8_t * buf = HIDIO_buffer_munch ( & HIDIO_ack_send_buf , tmpdata , HIDIO_ack_send_buf .head , HIDIO_Packet_Size );
1381
+ uint8_t * buf = HIDIO_buffer_munch ( & HIDIO_ack_send_buf , tmpdata , HIDIO_ack_send_buf .head , HIDIO_Packet_Size ). buf ;
1359
1382
HIDIO_Packet * packet = (HIDIO_Packet * )buf ;
1360
1383
1361
1384
// Send packet
@@ -1385,21 +1408,27 @@ inline void HIDIO_process()
1385
1408
1386
1409
// Send outgoing packet, we can only send one at a time
1387
1410
// and the next one can only be sent after an ACK is recieved
1388
- if ( HIDIO_tx_buf .packets_ready > 0 && HIDIO_tx_buf .waiting == 0 )
1411
+ uint16_t next_packet_pos = HIDIO_tx_buf .head ;
1412
+ uint16_t packets_ready = HIDIO_tx_buf .packets_ready ;
1413
+ while ( packets_ready > 0 && HIDIO_tx_buf .waiting == 0 )
1389
1414
{
1390
1415
// Prepare 64 byte packet
1391
1416
// TODO (HaaTa): Handle internal max size
1392
1417
uint8_t tmpdata [64 ];
1393
- uint8_t * buf = HIDIO_buffer_munch ( & HIDIO_tx_buf , tmpdata , HIDIO_tx_buf .head , HIDIO_Packet_Size );
1394
- HIDIO_Packet * packet = (HIDIO_Packet * )buf ;
1418
+ HIDIO_Munch munch = HIDIO_buffer_munch ( & HIDIO_tx_buf , tmpdata , next_packet_pos , HIDIO_Packet_Size );
1419
+ HIDIO_Packet * packet = (HIDIO_Packet * )munch .buf ;
1420
+ next_packet_pos = munch .next_pos ;
1395
1421
1396
1422
// Send packet
1397
1423
// TODO (HaaTa): Check error?
1398
1424
Output_rawio_sendbuffer ( (char * )packet );
1399
1425
1400
1426
// Indicate waiting for ACK packet
1401
1427
// Once ACK has been received (or NAK) the packet will be released
1402
- HIDIO_tx_buf .waiting = 1 ;
1428
+ HIDIO_tx_buf .waiting = packet -> cont ? 0 : 1 ;
1429
+
1430
+ // Next packet
1431
+ packets_ready -- ;
1403
1432
}
1404
1433
1405
1434
// End latency measurement
@@ -1517,9 +1546,9 @@ void HIDIO_Open_url_capability( TriggerMacro *trigger, uint8_t state, uint8_t st
1517
1546
}
1518
1547
1519
1548
uint16_t payload_len ;
1520
- //uint8_t arg = *(uint8_t*)(&args[0]);
1521
1549
1522
1550
#ifdef url_strings
1551
+ uint8_t arg = * (uint8_t * )(& args [0 ]);
1523
1552
char * text_buf = url_strings [arg ];
1524
1553
#else
1525
1554
char * text_buf = NULL ;
@@ -1559,9 +1588,9 @@ void HIDIO_layout_capability( TriggerMacro *trigger, uint8_t state, uint8_t stat
1559
1588
}
1560
1589
1561
1590
uint16_t payload_len ;
1562
- //uint8_t arg = *(uint8_t*)(&args[0]);
1563
1591
1564
1592
#ifdef layout_strings
1593
+ uint8_t arg = * (uint8_t * )(& args [0 ]);
1565
1594
char * text_buf = layout_strings [arg ];
1566
1595
#else
1567
1596
char * text_buf = NULL ;
0 commit comments