Skip to content

Commit 62f8d5d

Browse files
committed
lwl now supports strings as function ids
1 parent c140171 commit 62f8d5d

6 files changed

Lines changed: 149 additions & 55 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,12 @@ lwl_enter_record(
171171
```
172172

173173
#### IDs
174-
`MODULE_ID` and `FUNCTION_ID` must be macros defined in `lwl.h` and must be single byte values. Preferably single character literals to improve readability of the logs.
174+
`MODULE_ID` and `FUNCTION_ID` must be macros defined in `lwl.h`. `MODULE_ID` must be a single byte values, preferably single character literal to improve readability of the logs. `FUNCTION_ID` can be any string.
175175

176176
Example:
177177
```c
178178
#define LIS3DHTR_LWL_ID 'L'
179-
#define LIS3DHTR_READ_LWL_ID 'R'
179+
#define LIS3DHTR_READ_LWL_ID "RX"
180180
```
181181
182182
The `( MODULE_ID, FUNCTION_ID )` combination must be globally unique across the project.

code/CM7/Core/Src/adc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ HAL_StatusTypeDef calc_ADC_temp_reduced_div( int64_t* const result )
257257

258258
HAL_StatusTypeDef calc_ADC_temp_int( int64_t* const result )
259259
{
260-
lwl_enter_record( ADC_LWL_ID , ADC_REDUCED_DIV_LWL_ID , "" );
260+
lwl_enter_record( ADC_LWL_ID , ADC_INT_LWL_ID , "" );
261261

262262
// No adc readings yet
263263
if( latest_adc_buf == NULL )
@@ -284,7 +284,7 @@ HAL_StatusTypeDef calc_ADC_temp_int( int64_t* const result )
284284

285285
HAL_StatusTypeDef calc_ADC_temp_float( float* const result )
286286
{
287-
lwl_enter_record( ADC_LWL_ID , ADC_INT_LWL_ID , "" );
287+
lwl_enter_record( ADC_LWL_ID , ADC_FLOAT_LWL_ID , "" );
288288

289289
// No adc readings yet
290290
if( latest_adc_buf == NULL )

code/CM7/Core/lwl/lwl.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void lwl_init()
4545
lwl_driver.is_initialized = true;
4646
}
4747

48-
void lwl_enter_record( uint8_t module_id , uint8_t functionality_id , const char* fmt , ... )
48+
void lwl_enter_record( uint8_t module_id , char functionality_id[] , const char* fmt , ... )
4949
{
5050
if( lwl_driver.is_initialized == false )
5151
return;
@@ -55,8 +55,11 @@ void lwl_enter_record( uint8_t module_id , uint8_t functionality_id , const char
5555
// If buffer size is not power of 2 then
5656
// lwl_data.next_entry_index = (lwl_data.next_entry_index + 1) % LWL_BUFFER_SIZE;
5757

58-
lwl_data.buffer[lwl_data.next_entry_index] = functionality_id;
59-
lwl_data.next_entry_index = ( lwl_data.next_entry_index + 1 ) & ( LWL_BUFFER_SIZE - 1 );
58+
for( int i = 0 ; functionality_id[i] != '\0' ; i++)
59+
{
60+
lwl_data.buffer[lwl_data.next_entry_index] = (uint8_t)(functionality_id[i]);
61+
lwl_data.next_entry_index = ( lwl_data.next_entry_index + 1 ) & ( LWL_BUFFER_SIZE - 1 );
62+
}
6063

6164
va_list args;
6265

code/CM7/Core/lwl/lwl.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,29 @@
1313

1414
/* Macros ------------------------------------------------------------------*/
1515
#define LIS3DHTR_LWL_ID 'L'
16-
#define LIS3DHTR_WRITE_LWL_ID 'W'
17-
#define LIS3DHTR_READ_LWL_ID 'R'
16+
#define LIS3DHTR_WRITE_LWL_ID "W"
17+
#define LIS3DHTR_READ_LWL_ID "R"
1818

1919
#define ADC_LWL_ID 'A'
20-
#define ADC_REDUCED_DIV_LWL_ID 'R'
21-
#define ADC_INT_LWL_ID 'I'
22-
#define ADC_FLOAT_LWL_ID 'F'
20+
#define ADC_REDUCED_DIV_LWL_ID "R"
21+
#define ADC_INT_LWL_ID "I"
22+
#define ADC_FLOAT_LWL_ID "F"
2323

2424
#define TEST_LWL_ID 'T'
25-
#define TEST_TEST_LWL_ID 'T'
25+
#define TEST_TEST_LWL_ID "TEST"
2626

2727
#define I2C_LWL_ID 'I'
28-
#define I2C_TX_IT_LWL_ID 'T'
29-
#define I2C_RX_IT_LWL_ID 'R'
30-
#define I2C_ER_IT_LWL_ID 'E'
28+
#define I2C_TX_IT_LWL_ID "TX"
29+
#define I2C_RX_IT_LWL_ID "RX"
30+
#define I2C_ER_IT_LWL_ID "ER"
3131

3232
#define SPI_LWL_ID 'S'
33-
#define SPI_RX_IT_LWL_ID 'R'
34-
#define SPI_ER_IT_LWL_ID 'E'
33+
#define SPI_RX_IT_LWL_ID "RX"
34+
#define SPI_ER_IT_LWL_ID "TX"
3535

3636
/* Exported functions prototypes ---------------------------------------------*/
3737
void lwl_init();
38-
void lwl_enter_record( uint8_t module_id , uint8_t functionality_id , const char* fmt , ... );
38+
void lwl_enter_record( uint8_t module_id , char functionality_id[] , const char* fmt , ... );
3939
void dump_log();
4040

4141
#endif /* LWL_LWL_H_ */

code/CM7/sensor_playground_CM7 Debug.launch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,6 @@
8585
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
8686
<listEntry value="4"/>
8787
</listAttribute>
88-
<stringAttribute key="org.eclipse.dsf.launch.MEMORY_BLOCKS" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&lt;memoryBlockExpressionList context=&quot;reserved-for-future-use&quot;&gt;&lt;gdbmemoryBlockExpression address=&quot;603980424&quot; label=&quot;lwl_data.buffer&quot;/&gt;&lt;/memoryBlockExpressionList&gt;"/>
88+
<stringAttribute key="org.eclipse.dsf.launch.MEMORY_BLOCKS" value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;&lt;memoryBlockExpressionList context=&quot;reserved-for-future-use&quot;&gt;&lt;gdbmemoryBlockExpression address=&quot;603980968&quot; label=&quot;lwl_data.buffer&quot;/&gt;&lt;/memoryBlockExpressionList&gt;"/>
8989
<stringAttribute key="process_factory_id" value="com.st.stm32cube.ide.mcu.debug.launch.HardwareDebugProcessFactory"/>
9090
</launchConfiguration>

lwl_decoder.py

Lines changed: 126 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
ROOT_DIR = "."
14-
DUMP_BIN = "./code/debug/test3/dump.bin"
14+
DUMP_BIN = "./code/debug/test5/dump.bin"
1515
INFO_PATH = Path(DUMP_BIN).parent / "info.txt"
1616
LWL_DEFINES_FILE = "./code/CM7/Core/lwl/lwl.h"
1717

@@ -36,7 +36,7 @@
3636
CALL_START = re.compile(r'\blwl_enter_record\s*\(')
3737

3838
DEFINE_REGEX = re.compile(
39-
r'^\s*#define\s+(\w+)\s+\'(.)\'',
39+
r'^\s*#define\s+(\w+)\s+(.+)$',
4040
re.MULTILINE
4141
)
4242

@@ -60,13 +60,24 @@ def clean_code(text):
6060

6161
def parse_defines(filepath):
6262

63-
with open(filepath, 'r', encoding='utf-8', errors='ignore') as f:
63+
defines = {}
64+
65+
with open(filepath,'r',encoding='utf-8',errors='ignore') as f:
6466
content = f.read()
6567

66-
defines = {}
68+
for name,value in DEFINE_REGEX.findall(content):
69+
70+
value = value.strip()
6771

68-
for name, value in DEFINE_REGEX.findall(content):
69-
defines[name] = ord(value)
72+
# char literal
73+
if value.startswith("'") and value.endswith("'"):
74+
75+
defines[name] = eval(value)
76+
77+
# string literal
78+
elif value.startswith('"') and value.endswith('"'):
79+
80+
defines[name] = value[1:-1]
7081

7182
return defines
7283

@@ -174,9 +185,13 @@ def extract_calls(text):
174185
return matches
175186

176187

177-
def calculate_size(args):
188+
def calculate_size(args, id_defines):
189+
190+
size = 1 # module char
178191

179-
size = 2
192+
function_str = id_defines[args[1]]
193+
194+
size += len(function_str)
180195

181196
if len(args) < 3:
182197
return size
@@ -237,7 +252,7 @@ def build_call_database():
237252

238253
entry = {
239254
"args": args,
240-
"size": calculate_size(args),
255+
"size": calculate_size(args , id_defines),
241256
"key": resolved_key
242257
}
243258

@@ -354,19 +369,40 @@ def try_decode(decoded_buffer, decoder_table):
354369

355370
while offset < len(decoded_buffer):
356371

357-
if offset + 2 > len(decoded_buffer):
372+
if offset + 1 > len(decoded_buffer):
358373
return False, decoded_entries, offset
359374

360-
key = (
361-
decoded_buffer[offset],
362-
decoded_buffer[offset+1]
363-
)
375+
module_char = chr(decoded_buffer[offset])
376+
377+
matched_entry = None
378+
best_len = -1
379+
380+
for entry in decoder_table.values():
381+
382+
entry_module, entry_function = entry["key"]
383+
384+
if entry_module != module_char:
385+
continue
386+
387+
function_bytes = entry_function.encode()
364388

365-
if key not in decoder_table:
389+
start = offset + 1
390+
end = start + len(function_bytes)
391+
392+
if end > len(decoded_buffer):
393+
continue
394+
395+
if decoded_buffer[start:end] == function_bytes:
396+
397+
if len(function_bytes) > best_len:
398+
399+
matched_entry = entry
400+
best_len = len(function_bytes)
401+
402+
if matched_entry is None:
366403
return False, decoded_entries, offset
367404

368-
entry = decoder_table[key]
369-
size = entry["size"]
405+
size = matched_entry["size"]
370406

371407
if offset + size > len(decoded_buffer):
372408
return False, decoded_entries, offset
@@ -460,32 +496,58 @@ def decode_buffer(decoded_buffer, decoder_table, decode_point):
460496

461497
while offset < len(decoded_buffer):
462498

463-
if offset + 2 > len(decoded_buffer):
499+
if offset + 1 > len(decoded_buffer):
500+
464501
print(
465-
f"ERROR: truncated entry header "
466-
f"at offset {offset}"
502+
f"ERROR: truncated module ID "
503+
f"at byte {decode_point + offset}"
467504
)
468505
return
469506

470-
key = (
471-
decoded_buffer[offset],
472-
decoded_buffer[offset+1]
473-
)
507+
module_char = chr(decoded_buffer[offset])
508+
509+
matched_entry = None
510+
best_len = -1
511+
512+
for entry in decoder_table.values():
513+
514+
entry_module, entry_function = entry["key"]
474515

475-
if key not in decoder_table:
516+
if entry_module != module_char:
517+
continue
518+
519+
function_bytes = entry_function.encode()
520+
521+
start = offset + 1
522+
end = start + len(function_bytes)
523+
524+
if end > len(decoded_buffer):
525+
continue
526+
527+
if decoded_buffer[start:end] == function_bytes:
528+
529+
if len(function_bytes) > best_len:
530+
531+
matched_entry = entry
532+
best_len = len(function_bytes)
533+
534+
if matched_entry is None:
476535

477536
print(
478537
f"ERROR: unknown entry "
479-
f"{chr(key[0])}{chr(key[1])} "
480-
f"at byte {decode_point + offset}"
538+
f"at byte {decode_point + offset} "
539+
f"(module='{module_char}')"
481540
)
482541

483-
return
542+
print(
543+
f"DEBUG offset={offset} "
544+
f"bytes={decoded_buffer[offset:offset+15]}"
545+
)
484546

485-
entry = decoder_table[key]
547+
return
486548

487-
args = entry["args"]
488-
size = entry["size"]
549+
args = matched_entry["args"]
550+
size = matched_entry["size"]
489551

490552
if offset + size > len(decoded_buffer):
491553

@@ -496,62 +558,91 @@ def decode_buffer(decoded_buffer, decoder_table, decode_point):
496558

497559
return
498560

561+
module_id, function_id = matched_entry["key"]
562+
563+
function_bytes = function_id.encode()
564+
499565
fmt = args[2][1:-1]
500566

501-
pos = offset + 2
567+
pos = (
568+
offset
569+
+ 1
570+
+ len(function_bytes)
571+
)
572+
573+
module_str = (
574+
f"{args[0]} ({module_id})"
575+
)
502576

503-
module_str = f"{args[0]} ({chr(key[0])})"
504-
function_str = f"{args[1]} ({chr(key[1])})"
577+
function_str = (
578+
f'{args[1]} ("{function_id}")'
579+
)
505580

506581
arg_output = []
507582

508583
for fmt_char, name in zip(fmt, args[3:]):
509584

510585
if fmt_char == 'c':
586+
511587
value = decoded_buffer[pos]
512588
pos += 1
513589

514590
elif fmt_char == 's':
591+
515592
value = int.from_bytes(
516593
decoded_buffer[pos:pos+2],
517594
'little',
518595
signed=True
519596
)
597+
520598
pos += 2
521599

522600
elif fmt_char == 'h':
601+
523602
value = int.from_bytes(
524603
decoded_buffer[pos:pos+2],
525604
'little',
526605
signed=False
527606
)
607+
528608
pos += 2
529609

530610
elif fmt_char == 'd':
611+
531612
value = int.from_bytes(
532613
decoded_buffer[pos:pos+4],
533614
'little',
534615
signed=True
535616
)
617+
536618
pos += 4
537619

538620
elif fmt_char == 'u':
621+
539622
value = int.from_bytes(
540623
decoded_buffer[pos:pos+4],
541624
'little',
542625
signed=False
543626
)
627+
544628
pos += 4
545629

546630
elif fmt_char == 'f':
631+
547632
value = struct.unpack(
548633
'<f',
549634
decoded_buffer[pos:pos+4]
550635
)[0]
636+
551637
pos += 4
552638

553639
else:
554-
print(f"ERROR: unknown format '{fmt_char}'")
640+
641+
print(
642+
f"ERROR: unknown format "
643+
f"'{fmt_char}'"
644+
)
645+
555646
return
556647

557648
arg_output.append(

0 commit comments

Comments
 (0)