Skip to content

Commit 158f97d

Browse files
committed
Decompression function working
Added example test file
1 parent 9eae8d1 commit 158f97d

File tree

2 files changed

+40
-76
lines changed

2 files changed

+40
-76
lines changed

source/application/lua_libraries/compression.c

Lines changed: 6 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -29,72 +29,6 @@
2929
#include "lua.h"
3030
#include "watchdog.h"
3131

32-
/*
33-
* Test steps:
34-
*
35-
* 1. Create a test file with some text
36-
*
37-
* 2. Compress the file
38-
*
39-
* lz4 -9 -B32 test.txt test.lz4
40-
*
41-
* 3. Convert to header data
42-
*
43-
* xxd -include test.lz4 test.h
44-
*
45-
* 4. Copy the header data to the test buffer below
46-
*/
47-
48-
const unsigned char test_data[] = {
49-
0x04, 0x22, 0x4d, 0x18, 0x64, 0x40, 0xa7, 0x24, 0x02, 0x00, 0x00, 0xf0,
50-
0x30, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x74, 0x68, 0x69, 0x73, 0x20,
51-
0x69, 0x73, 0x20, 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x66, 0x69,
52-
0x6c, 0x65, 0x20, 0x74, 0x68, 0x61, 0x74, 0x20, 0x63, 0x6f, 0x6e, 0x74,
53-
0x61, 0x69, 0x6e, 0x73, 0x20, 0x73, 0x6f, 0x6d, 0x65, 0x20, 0x74, 0x65,
54-
0x78, 0x74, 0x2e, 0x0a, 0x0a, 0x4c, 0x6f, 0x72, 0x65, 0x6d, 0x20, 0x49,
55-
0x70, 0x73, 0x75, 0x6d, 0x35, 0x00, 0xc1, 0x73, 0x69, 0x6d, 0x70, 0x6c,
56-
0x79, 0x20, 0x64, 0x75, 0x6d, 0x6d, 0x79, 0x23, 0x00, 0xf1, 0x0d, 0x20,
57-
0x6f, 0x66, 0x20, 0x74, 0x68, 0x65, 0x20, 0x70, 0x72, 0x69, 0x6e, 0x74,
58-
0x69, 0x6e, 0x67, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x74, 0x79, 0x70, 0x65,
59-
0x73, 0x65, 0x74, 0x10, 0x00, 0xa8, 0x69, 0x6e, 0x64, 0x75, 0x73, 0x74,
60-
0x72, 0x79, 0x2e, 0x20, 0x4b, 0x00, 0x81, 0x68, 0x61, 0x73, 0x20, 0x62,
61-
0x65, 0x65, 0x6e, 0x3c, 0x00, 0x04, 0x23, 0x00, 0xb8, 0x27, 0x73, 0x20,
62-
0x73, 0x74, 0x61, 0x6e, 0x64, 0x61, 0x72, 0x64, 0x62, 0x00, 0xa1, 0x65,
63-
0x76, 0x65, 0x72, 0x20, 0x73, 0x69, 0x6e, 0x63, 0x65, 0x2e, 0x00, 0xf2,
64-
0x07, 0x31, 0x35, 0x30, 0x30, 0x73, 0x2c, 0x20, 0x77, 0x68, 0x65, 0x6e,
65-
0x20, 0x61, 0x6e, 0x20, 0x75, 0x6e, 0x6b, 0x6e, 0x6f, 0x77, 0x6e, 0x81,
66-
0x00, 0xf1, 0x01, 0x65, 0x72, 0x20, 0x74, 0x6f, 0x6f, 0x6b, 0x20, 0x61,
67-
0x20, 0x67, 0x61, 0x6c, 0x6c, 0x65, 0x79, 0x9e, 0x00, 0x31, 0x79, 0x70,
68-
0x65, 0x96, 0x00, 0xf2, 0x07, 0x73, 0x63, 0x72, 0x61, 0x6d, 0x62, 0x6c,
69-
0x65, 0x64, 0x20, 0x69, 0x74, 0x20, 0x74, 0x6f, 0x20, 0x6d, 0x61, 0x6b,
70-
0x65, 0x20, 0x61, 0x20, 0x00, 0xf1, 0x02, 0x73, 0x70, 0x65, 0x63, 0x69,
71-
0x6d, 0x65, 0x6e, 0x20, 0x62, 0x6f, 0x6f, 0x6b, 0x2e, 0x20, 0x49, 0x74,
72-
0xa2, 0x00, 0xf1, 0x1b, 0x73, 0x75, 0x72, 0x76, 0x69, 0x76, 0x65, 0x64,
73-
0x20, 0x6e, 0x6f, 0x74, 0x20, 0x6f, 0x6e, 0x6c, 0x79, 0x20, 0x66, 0x69,
74-
0x76, 0x65, 0x20, 0x63, 0x65, 0x6e, 0x74, 0x75, 0x72, 0x69, 0x65, 0x73,
75-
0x2c, 0x20, 0x62, 0x75, 0x74, 0x20, 0x61, 0x6c, 0x73, 0x6f, 0x9a, 0x00,
76-
0xf8, 0x05, 0x6c, 0x65, 0x61, 0x70, 0x20, 0x69, 0x6e, 0x74, 0x6f, 0x20,
77-
0x65, 0x6c, 0x65, 0x63, 0x74, 0x72, 0x6f, 0x6e, 0x69, 0x63, 0x0c, 0x01,
78-
0x80, 0x2c, 0x20, 0x72, 0x65, 0x6d, 0x61, 0x69, 0x6e, 0x17, 0x01, 0xf1,
79-
0x06, 0x65, 0x73, 0x73, 0x65, 0x6e, 0x74, 0x69, 0x61, 0x6c, 0x6c, 0x79,
80-
0x20, 0x75, 0x6e, 0x63, 0x68, 0x61, 0x6e, 0x67, 0x65, 0x64, 0x79, 0x00,
81-
0xd0, 0x77, 0x61, 0x73, 0x20, 0x70, 0x6f, 0x70, 0x75, 0x6c, 0x61, 0x72,
82-
0x69, 0x73, 0xad, 0x00, 0x02, 0x25, 0x01, 0xa1, 0x31, 0x39, 0x36, 0x30,
83-
0x73, 0x20, 0x77, 0x69, 0x74, 0x68, 0x0f, 0x00, 0x70, 0x72, 0x65, 0x6c,
84-
0x65, 0x61, 0x73, 0x65, 0xe1, 0x00, 0xf4, 0x00, 0x4c, 0x65, 0x74, 0x72,
85-
0x61, 0x73, 0x65, 0x74, 0x20, 0x73, 0x68, 0x65, 0x65, 0x74, 0x73, 0xc8,
86-
0x01, 0x00, 0x66, 0x00, 0x08, 0x73, 0x01, 0x60, 0x70, 0x61, 0x73, 0x73,
87-
0x61, 0x67, 0xb5, 0x00, 0x00, 0x0d, 0x01, 0x30, 0x6d, 0x6f, 0x72, 0x45,
88-
0x00, 0x00, 0xcb, 0x00, 0x22, 0x6c, 0x79, 0x57, 0x00, 0xf0, 0x00, 0x64,
89-
0x65, 0x73, 0x6b, 0x74, 0x6f, 0x70, 0x20, 0x70, 0x75, 0x62, 0x6c, 0x69,
90-
0x73, 0x68, 0x40, 0x00, 0xf0, 0x15, 0x73, 0x6f, 0x66, 0x74, 0x77, 0x61,
91-
0x72, 0x65, 0x20, 0x6c, 0x69, 0x6b, 0x65, 0x20, 0x41, 0x6c, 0x64, 0x75,
92-
0x73, 0x20, 0x50, 0x61, 0x67, 0x65, 0x4d, 0x61, 0x6b, 0x65, 0x72, 0x20,
93-
0x69, 0x6e, 0x63, 0x6c, 0x75, 0x64, 0x28, 0x00, 0x81, 0x76, 0x65, 0x72,
94-
0x73, 0x69, 0x6f, 0x6e, 0x73, 0x8f, 0x00, 0xb0, 0x6f, 0x72, 0x65, 0x6d,
95-
0x20, 0x49, 0x70, 0x73, 0x75, 0x6d, 0x2e, 0x00, 0x00, 0x00, 0x00, 0x31,
96-
0xb8, 0x0d, 0xa8};
97-
9832
static int registered_function = 0;
9933
static uint8_t decompression_buffer[4096];
10034
static size_t decompression_buffer_size = 0;
@@ -125,8 +59,6 @@ static void process_function_callback(void *context,
12559
decompression_buffer_size = data_size;
12660
memcpy(decompression_buffer, data, data_size);
12761

128-
LOG("decompression_buffer_size: %d", decompression_buffer_size);
129-
13062
lua_sethook(L_global,
13163
decompression_lua_handler,
13264
LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE | LUA_MASKCOUNT,
@@ -154,22 +86,24 @@ static int lua_compression_register_process_function(lua_State *L)
15486

15587
static int lua_compression_decompress(lua_State *L)
15688
{
157-
lua_Integer block_size = luaL_checkinteger(L, 1);
89+
size_t length;
90+
const char *data = luaL_checklstring(L, 1, &length);
91+
92+
lua_Integer block_size = luaL_checkinteger(L, 2);
15893

15994
if (block_size <= 0)
16095
{
16196
luaL_error(L, "bytes must be greater than 0");
16297
}
16398

16499
int status = compression_decompress(block_size,
165-
test_data, // TODO pass the source data to the decompression function
166-
sizeof(test_data), // TODO set the source data size
100+
data,
101+
length,
167102
process_function_callback,
168103
NULL);
169104

170105
if (status)
171106
{
172-
LOG("%d", status);
173107
luaL_error(L, "decompression failed");
174108
}
175109

tests/test_compression.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,47 @@
33

44

55
async def main():
6+
7+
lua_script = """
8+
-- Decompression function
9+
function decomp_func(data)
10+
print(data)
11+
end
12+
13+
-- Register the decompression function
14+
frame.compression.process_function(decomp_func)
15+
16+
-- Variable where the compressed data will be stored
17+
compressed_data = ''
18+
19+
-- Function to handle the compressed data received from Bluetooth
20+
function ble_func(data)
21+
frame.compression.decompress(data, 1024)
22+
end
23+
24+
-- Register the Bluetooth receive callback
25+
frame.bluetooth.receive_callback(ble_func)
26+
27+
"""
28+
29+
compressed_data = bytearray(
30+
b"\x04\x22\x4d\x18\x64\x40\xa7\x6f\x00\x00\x00\xf5\x3d\x48\x65\x6c\x6c\x6f\x21\x20\x49\x20\x77\x61\x73\x20\x73\x6f\x6d\x65\x20\x63\x6f\x6d\x70\x72\x65\x73\x73\x65\x64\x20\x64\x61\x74\x61\x2e\x20\x49\x6e\x20\x74\x68\x69\x73\x20\x63\x61\x73\x65\x2c\x20\x73\x74\x72\x69\x6e\x67\x73\x20\x61\x72\x65\x6e\x27\x74\x20\x70\x61\x72\x74\x69\x63\x75\x6c\x61\x72\x6c\x79\x3b\x00\xf1\x01\x69\x62\x6c\x65\x2c\x20\x62\x75\x74\x20\x73\x70\x72\x69\x74\x65\x49\x00\xa0\x20\x77\x6f\x75\x6c\x64\x20\x62\x65\x2e\x00\x00\x00\x00\x5f\xd0\xa3\x47"
31+
)
32+
633
b = Bluetooth()
734

835
await b.connect(print_response_handler=lambda s: print(s))
936

10-
await b.send_lua("function process_function(data) print(data) end")
11-
await b.send_lua("frame.compression.process_function(process_function)")
12-
await b.send_lua("frame.compression.decompress(1024)")
13-
await b.send_lua("process_function(1)")
37+
# Upload the Lua script
38+
await b.upload_file(lua_script, "main.lua")
39+
await b.send_reset_signal()
1440

1541
await asyncio.sleep(1)
1642

43+
# Send compressed data
44+
await b.send_data(compressed_data)
45+
46+
await b.send_break_signal()
1747
await b.disconnect()
1848

1949

0 commit comments

Comments
 (0)