compression using lz family on zephyr

hello Nordic

i am using nrf52832 and nrf52840, working with ncs v2.1.2

i wish to compress data packets that are sent via BLE to reduce the transmitted data size.

i saw there is lz4 implementation in the ncs, i tried to use the example  at /zephyr/samples/compression/lz4/

since i got less then 10% compression, (and since i don't think speed will be an issue cause reading the data chunks from flash takes much longer then the compression) i tried to use more compress oriented API

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) {
		LOG_INF("Failed to allocate memory for compressed data\n");
		return;
	}

    LOG_INF("src ptr before %p, %p", (int*)src, src);
    int srcptr = 0;
	const int compressed_data_size = LZ4_compress_destSize(src, compressed_data, &srcptr, 256);
	// const int compressed_data_size = LZ4_compress_default(src,
	// 				compressed_data, src_size,
	// 				max_dst_size);
	if (compressed_data_size <= 0) {
		LOG_INF("Failed to compress the data\n");
		return;
	}

    LOG_INF("-----------------------ZIV-------------------------\n");
    LOG_INF("src ptr after %d,srcptr);
	LOG_INF("Original Data size: %d\n", src_size);
    LOG_INF("planned compres size %d\n", max_dst_size);
	LOG_INF("Compressed Data size : %d\n", compressed_data_size);

	compressed_data = (char *)realloc(compressed_data,
					  (size_t)compressed_data_size);
	if (compressed_data == NULL) {
		LOG_INF("Failed to re-alloc memory for compressed data\n");
		return;
	}

	char * const decompressed_data = malloc(src_size);

	if (decompressed_data == NULL) {
		LOG_INF("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) {
		LOG_INF("Failed to decompress the data\n");
		return;
	}

	free(compressed_data);

	if (decompressed_size >= 0) {
		LOG_INF("Successfully decompressed some data\n");
	}

	if (decompressed_size != src_size) {
		LOG_INF("Decompressed data is different from original decompress size %d, src size %d\n", decompressed_size, src_size);
		return;
	}

	if (memcmp(src, decompressed_data, src_size) != 0) {
		LOG_INF("Validation failed.\n");
		LOG_INF("*src and *new_src are not identical\n");
		return;
	}

	LOG_INF("Validation done. The string we ended up with is:\n%s\n",
			decompressed_data);
    //  for ( int i = 0; i< 512; ++i)
    //  {
    //     LOG_INF("%d,", decompressed_data[i]);
    //  }       
    LOG_INF("------------------------ZIV------------------------\n");

but its or i am not using it right or it is not so efficient with target size of 200, with target size of 256 a little bit better, but maybe it is more relevant to the type of data in each area, i am not sure.. also if there is an optimal size to compress to .. (i add the prints below the code part)

00> [00000000] <inf> MAIN: -----------------------ZIV-------------------------
00> 
00> [00000000] <inf> MAIN: src ptr after 0
00> [00000000] <inf> MAIN: Original Data size: 512
00> 
00> [00000000] <inf> MAIN: planned compres size 530
00> 
00> [00000000] <inf> MAIN: Compressed Data size : 1
00> 
00> [00000000] <inf> MAIN: Successfully decompressed some data
00> 
00> [00000000] <inf> MAIN: Decompressed data is different from original decompress size 0, src size 512

why srcPtr does not change ?

what am i missing?

i also saw there is a module in the ncs lz4hc that is supposed to be more compression oriented then speed but not sure how to use it, is there some example or guide on that ?

also there is lzio,  lz4frame, lz4frame_static, lz77 , lz4hc, compress_Frame_Fuzzer, compress_hc_Fuzzer, is there some guide to which strength each of them brings, and how to use ?

(i know lz77 is the father of the family, and hc means high compression but that's it) 

hope to read you soon

best regards

Ziv

Related