Skip to content

Commit

Permalink
samples: compression: lz4: lz4 sample application
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 0 deletions.
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@
/samples/bluetooth/ @jhedberg @Vudentz @joerchan
/samples/boards/intel_s1000_crb/ @sathishkuttan @dcpleung @nashif
/samples/subsys/display/ @vanwinkeljan
/samples/compression/ @Navin-Sankar
/samples/drivers/can/ @alexanderwachter
/samples/drivers/clock_control_litex/ @mateusz-holenko @kgugala @pgielda
/samples/drivers/display/ @vanwinkeljan
Expand Down
3 changes: 3 additions & 0 deletions modules/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ comment "Trusted-firmware-m module not available."
comment "Nanopb module not available."
depends on !ZEPHYR_NANOPB_MODULE

comment "Lz4 module not available."
depends on !ZEPHYR_LZ4_MODULE

# This ensures that symbols are available in Kconfig for dependency checking
# and referencing, while keeping the settings themselves unavailable when the
# modules are not present in the workspace
Expand Down
16 changes: 16 additions & 0 deletions modules/lz4/CMakeLists.txt
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()
11 changes: 11 additions & 0 deletions modules/lz4/Kconfig
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.
10 changes: 10 additions & 0 deletions samples/compression/compression.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.. _compression-samples:

Compression Samples
###################

.. toctree::
:maxdepth: 1
:glob:

**/*
8 changes: 8 additions & 0 deletions samples/compression/lz4/CMakeLists.txt
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)
23 changes: 23 additions & 0 deletions samples/compression/lz4/README.rst
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.
3 changes: 3 additions & 0 deletions samples/compression/lz4/prj.conf
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
14 changes: 14 additions & 0 deletions samples/compression/lz4/sample.yaml
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
98 changes: 98 additions & 0 deletions samples/compression/lz4/src/main.c
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);
}
1 change: 1 addition & 0 deletions samples/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Samples and Demos
kernel/*
tfm_integration/tfm_integration.rst
modules/*
compression/*

.. comment
To add a new sample document, please use the template available under
Expand Down
3 changes: 3 additions & 0 deletions west.yml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ manifest:
repo-path: tensorflow
path: modules/lib/tensorflow
revision: dc70a45a7cc12c25726a32cd91b28be59e7bc596
- name: lz4
path: modules/lib/lz4
revision: 8e303c264fc21c2116dc612658003a22e933124d

self:
path: zephyr
Expand Down

0 comments on commit 273ca64

Please sign in to comment.