forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: compression: lz4: lz4 sample application
This PR add sample application for minimal lz4 library. lz4 library RFC requested here: zephyrproject-rtos#28535 Fixes: zephyrproject-rtos#26648 Signed-off-by: NavinSankar Velliangiri <navin@linumiz.com>
- Loading branch information
NavinSankar Velliangiri
committed
Jun 11, 2021
1 parent
75a2ef8
commit 273ca64
Showing
12 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Copyright (c) 2020 Linumiz | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
if(CONFIG_LZ4) | ||
|
||
set(LZ4_DIR ${ZEPHYR_CURRENT_MODULE_DIR}) | ||
|
||
zephyr_library() | ||
|
||
zephyr_include_directories(${LZ4_DIR}/lib) | ||
|
||
zephyr_library_sources( | ||
${LZ4_DIR}/lib/lz4.c | ||
) | ||
|
||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Copyright (c) 2020 Linumiz | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
config ZEPHYR_LZ4_MODULE | ||
bool | ||
|
||
config LZ4 | ||
bool "Enable lz4 data compression and decompression" | ||
help | ||
This option enables lz4 compression & decompression library | ||
support. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
.. _compression-samples: | ||
|
||
Compression Samples | ||
################### | ||
|
||
.. toctree:: | ||
:maxdepth: 1 | ||
:glob: | ||
|
||
**/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
cmake_minimum_required(VERSION 3.13.1) | ||
|
||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) | ||
project(lz4) | ||
|
||
target_sources(app PRIVATE src/main.c) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
.. _lz4: | ||
|
||
LZ4 | ||
### | ||
|
||
Overview | ||
******** | ||
|
||
A simple sample that can be used with any :ref:`supported board <boards>` and | ||
compress & decompress the user data to the console. | ||
|
||
Building and Running | ||
******************** | ||
|
||
The sample can be built and executed on frdm_k64f as follows: | ||
|
||
.. zephyr-app-commands:: | ||
:zephyr-app: samples/compression/lz4 | ||
:board: nrf52840dk_nrf52840 | ||
:goals: build flash | ||
:compact: | ||
|
||
To build for another board, change "nrf52840dk_nrf52840" above to that board's name. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
CONFIG_LZ4=y | ||
CONFIG_NEWLIB_LIBC=y | ||
CONFIG_HEAP_MEM_POOL_SIZE=16384 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
sample: | ||
name: lz4 sample | ||
description: lz4 data compression library | ||
common: | ||
tags: compression lz4 | ||
min_ram: 32 | ||
harness: console | ||
harness_config: | ||
type: one_line | ||
regex: | ||
- "Validation done. (.*)" | ||
tests: | ||
sample.compression.lz4: | ||
tags: compression lz4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright (c) 2020 Linumiz | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <zephyr.h> | ||
#include <string.h> | ||
#include <stdlib.h> | ||
#include "lz4.h" | ||
|
||
void main(void) | ||
{ | ||
static const char *src = "Lorem ipsum dolor sit amet, consectetur " | ||
"adipiscing elit. Quisque sodales lorem lorem, sed congue enim " | ||
"vehicula a. Sed finibus diam sed odio ultrices pharetra. Nullam " | ||
"dictum arcu ultricies turpis congue,vel venenatis turpis venenatis. " | ||
"Nam tempus arcu eros, ac congue libero tristique congue. Proin velit " | ||
"lectus, euismod sit amet quam in, maximus condimentum urna. Cras vel " | ||
"erat luctus, mattis orci ut, varius urna. Nam eu lobortis velit." | ||
"\n" | ||
"Nullam sit amet diam vel odio sodales cursus vehicula eu arcu. Proin " | ||
"fringilla, enim nec consectetur mollis, lorem orci interdum nisi, " | ||
"vitae suscipit nisi mauris eu mi. Proin diam enim, mollis ac rhoncus " | ||
"vitae, placerat et eros. Suspendisse convallis, ipsum nec rhoncus " | ||
"aliquam, ex augue ultrices nisl, id aliquet mi diam quis ante. " | ||
"Pellentesque venenatis ornare ultrices. Quisque et porttitor lectus. " | ||
"Ut venenatis nunc et urna imperdiet porttitor non laoreet massa." | ||
"Donec eleifend eros in mi sagittis egestas. Sed et mi nunc. Nunc " | ||
"vulputate,mauris non ullamcorper viverra, lorem nulla vulputate " | ||
"diam, et congue dui velit non erat. Duis interdum leo et ipsum " | ||
"tempor consequat. In faucibus enim quis purus vulputate nullam." | ||
"\n"; | ||
|
||
const int src_size = (int)(strlen(src) + 1); | ||
const int max_dst_size = LZ4_compressBound(src_size); | ||
|
||
char *compressed_data = malloc((size_t)max_dst_size); | ||
|
||
if (compressed_data == NULL) { | ||
printk("Failed to allocate memory for compressed data\n"); | ||
return; | ||
} | ||
|
||
const int compressed_data_size = LZ4_compress_default(src, | ||
compressed_data, src_size, | ||
max_dst_size); | ||
if (compressed_data_size <= 0) { | ||
printk("Failed to compress the data\n"); | ||
return; | ||
} | ||
|
||
printk("Original Data size: %d\n", src_size); | ||
printk("Compressed Data size : %d\n", compressed_data_size); | ||
|
||
compressed_data = (char *)realloc(compressed_data, | ||
(size_t)compressed_data_size); | ||
if (compressed_data == NULL) { | ||
printk("Failed to re-alloc memory for compressed data\n"); | ||
return; | ||
} | ||
|
||
char * const decompressed_data = malloc(src_size); | ||
|
||
if (decompressed_data == NULL) { | ||
printk("Failed to allocate memory to decompress data\n"); | ||
return; | ||
} | ||
|
||
const int decompressed_size = LZ4_decompress_safe(compressed_data, | ||
decompressed_data, compressed_data_size, | ||
src_size); | ||
if (decompressed_size < 0) { | ||
printk("Failed to decompress the data\n"); | ||
return; | ||
} | ||
|
||
free(compressed_data); | ||
|
||
if (decompressed_size >= 0) { | ||
printk("Successfully decompressed some data\n"); | ||
} | ||
|
||
if (decompressed_size != src_size) { | ||
printk("Decompressed data is different from original\n"); | ||
return; | ||
} | ||
|
||
if (memcmp(src, decompressed_data, src_size) != 0) { | ||
printk("Validation failed.\n"); | ||
printk("*src and *new_src are not identical\n"); | ||
return; | ||
} | ||
|
||
printk("Validation done. The string we ended up with is:\n%s\n", | ||
decompressed_data); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters