Skip to content

Commit 7c8a557

Browse files
committed
zephyr: samples: add basic sample from main Zephyr repo
Zephyr upstream repo is not pulling in lz4 as a module anymore and will stop maintaining this code sample. As it's useful for testing lz4 in the context of a working Zephyr application, add it here. Signed-off-by: Benjamin Cabé <benjamin@zephyrproject.org>
1 parent 11b8a1e commit 7c8a557

5 files changed

Lines changed: 132 additions & 0 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(lz4)
7+
8+
target_sources(app PRIVATE src/main.c)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Overview
2+
********
3+
4+
A simple sample that can be used with any Zephyr supported board and
5+
compress & decompress the user data to the console.
6+
7+
Building and Running
8+
********************
9+
10+
The sample can be built and executed on nrf52840dk/nrf52840 as follows::
11+
12+
west build -b nrf52840dk/nrf52840 zephyr/samples/lorem_ipsum
13+
west flash
14+
15+
To build for another board, change "nrf52840dk/nrf52840" above to that board's name.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CONFIG_LZ4=y
2+
CONFIG_REQUIRES_FULL_LIBC=y
3+
CONFIG_HEAP_MEM_POOL_SIZE=16384
4+
CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE=24576
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
sample:
2+
name: lz4 sample
3+
description: lz4 data compression library
4+
common:
5+
tags:
6+
- compression
7+
- lz4
8+
min_ram: 64
9+
filter: CONFIG_FULL_LIBC_SUPPORTED
10+
harness: console
11+
harness_config:
12+
type: one_line
13+
regex:
14+
- "Validation done. (.*)"
15+
modules:
16+
- lz4
17+
tests:
18+
sample.compression.lz4:
19+
integration_platforms:
20+
- mps2/an385
21+
- qemu_riscv64
22+
tags:
23+
- compression
24+
- lz4
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright (c) 2020 Linumiz
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <stdio.h>
8+
#include <zephyr/kernel.h>
9+
#include <zephyr/misc/lorem_ipsum.h>
10+
#include <string.h>
11+
#include <stdlib.h>
12+
#include "lz4.h"
13+
14+
int main(void)
15+
{
16+
static const char *src = LOREM_IPSUM;
17+
18+
const int src_size = (int)(strlen(src) + 1);
19+
const int max_dst_size = LZ4_compressBound(src_size);
20+
21+
char *compressed_data = malloc((size_t)max_dst_size);
22+
23+
if (compressed_data == NULL) {
24+
printk("Failed to allocate memory for compressed data\n");
25+
return 0;
26+
}
27+
28+
const int compressed_data_size = LZ4_compress_default(src,
29+
compressed_data, src_size,
30+
max_dst_size);
31+
if (compressed_data_size <= 0) {
32+
printk("Failed to compress the data\n");
33+
return 0;
34+
}
35+
36+
printk("Original Data size: %d\n", src_size);
37+
printk("Compressed Data size : %d\n", compressed_data_size);
38+
39+
compressed_data = (char *)realloc(compressed_data,
40+
(size_t)compressed_data_size);
41+
if (compressed_data == NULL) {
42+
printk("Failed to re-alloc memory for compressed data\n");
43+
return 0;
44+
}
45+
46+
char * const decompressed_data = malloc(src_size);
47+
48+
if (decompressed_data == NULL) {
49+
printk("Failed to allocate memory to decompress data\n");
50+
return 0;
51+
}
52+
53+
const int decompressed_size = LZ4_decompress_safe(compressed_data,
54+
decompressed_data, compressed_data_size,
55+
src_size);
56+
if (decompressed_size < 0) {
57+
printk("Failed to decompress the data\n");
58+
return 0;
59+
}
60+
61+
free(compressed_data);
62+
63+
if (decompressed_size >= 0) {
64+
printk("Successfully decompressed some data\n");
65+
}
66+
67+
if (decompressed_size != src_size) {
68+
printk("Decompressed data is different from original\n");
69+
return 0;
70+
}
71+
72+
if (memcmp(src, decompressed_data, src_size) != 0) {
73+
printk("Validation failed.\n");
74+
printk("*src and *new_src are not identical\n");
75+
return 0;
76+
}
77+
78+
printk("Validation done. The string we ended up with is:\n%s\n",
79+
decompressed_data);
80+
return 0;
81+
}

0 commit comments

Comments
 (0)