Skip to content

Commit

Permalink
Move uzlib_gzip_wrapper, utils, and fallback_log to local libs
Browse files Browse the repository at this point in the history
uzlib_gzip_wrapper changes:
 * Move to local lib.
 * Add callback style decompression support.
 * Add four native decompression unit tests. These need python3 to be
installed.
 * Add workaround for uzlib output buffer needing to be one byte larger
than decompressed file.
 * Add function to check the number of already decompressed bytes.
 * Add function to check whether the decompression is finished.
 * Move to gzip namespace.

Other changes:
 * Move utils and fallback log to local libs.
 * Change utils namespace from "utility" to "utils".
  • Loading branch information
ToMe25 committed Jan 11, 2024
1 parent 4ce7e85 commit e7f185e
Show file tree
Hide file tree
Showing 26 changed files with 873 additions and 239 deletions.
5 changes: 5 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Not all of these are necessarily going to be implemented at all.
* Merge wifissid.txt and wifipass.txt into wificreds.txt and merge mqttuser.txt and mqttpass.txt into mqttcreds.txt
* Improve log messages for when measurements fail
* Use an asynchronous DHT library(for example https://github.com/bertmelis/esp32DHT/)?
* Add (stream?) compression to uzlib_gzip_wrapper
* Change callback based uzlib_ungzip_wrapper to use C++ function objects instead of C function pointers

## Web Interface
* Add error message to web interface if measurement fails
Expand All @@ -19,6 +21,7 @@ Not all of these are necessarily going to be implemented at all.
* Allow caching of static resources using a hash of their content as an ETag
* Add theme switcher to web interface(clientside setting)
* Web server IPv6 support
* Add current commit to prometheus info and HTTP Server header

## Integration
* Find a way to make the kubernetes service(for prometheus) automatically point to the esp(mDNS?)
Expand All @@ -28,3 +31,5 @@ Not all of these are necessarily going to be implemented at all.
* Add MQTT state json
* Cleanup MQTT code
* Add MQTT discovery support(https://www.home-assistant.io/docs/mqtt/discovery/)
* Add WiFi info to prometheus metrics
* Dynamically gzip compress metrics page?
46 changes: 0 additions & 46 deletions lib/README

This file was deleted.

2 changes: 2 additions & 0 deletions lib/fallback_log/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Fallback Log
This is a simple header-only library, containing fallback logging(log_X) macros to use if the framework for the given board doesn't include them.
File renamed without changes.
12 changes: 12 additions & 0 deletions lib/fallback_log/library.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "FallbackLog",
"description": "A header only library containing fallback definitions of the log_X macros.",
"version": "1.0.0",
"license": "MIT",
"dependencies": [
{
"name": "utils",
"version": "utils"
}
]
}
2 changes: 2 additions & 0 deletions lib/utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Utils
This library contains common utilities required for other parts of this project.
12 changes: 6 additions & 6 deletions src/utils.h → lib/utils/include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

#include <string>

namespace utility {
namespace utils {
/**
* A method to get the index of the Most Significant Bit set in a number.
*
Expand Down Expand Up @@ -58,28 +58,28 @@ struct const_expr_value {
constexpr size_t strlen(const char *str) {
return (*str == 0) ? 0 : strlen(str + 1) + 1;
}
}
} /* namespace utils */

/**
* CONST_EXPR_VALUE is an utility macro to get force compile time evaluation of a constexpr value.
*/
#define CONST_EXPR_VALUE(exp) utility::const_expr_value<decltype(exp), exp>::value
#define CONST_EXPR_VALUE(exp) utils::const_expr_value<decltype(exp), exp>::value

/**
* FILE_BASE_NAME is a macro that represents the base name of the current file.
*/
#ifdef __FILE_NAME__
#define FILE_BASE_NAME __FILE_NAME__
#else
#define FILE_BASE_NAME &__FILE__[CONST_EXPR_VALUE(utility::get_base_name_offset(__FILE__))]
#define FILE_BASE_NAME &__FILE__[CONST_EXPR_VALUE(utils::get_base_name_offset(__FILE__))]
#endif

/**
* EXPAND_MACRO is a utility macro to expand a macro to its string form.
*/
#define EXPAND_MACRO(macro) #macro

namespace utility {
namespace utils {
/**
* Converts the given temperature from degrees celsius to degrees fahrenheit.
*
Expand Down Expand Up @@ -115,6 +115,6 @@ std::string float_to_string(const float measurement,
* @return The newly created string.
*/
std::string timespan_to_string(const int64_t time_ms);
}
} /* namespace utils */

#endif /* SRC_UTILS_H_ */
4 changes: 2 additions & 2 deletions src/utils.cpp → lib/utils/src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <cmath>
#include <sstream>

namespace utility {
namespace utils {

uint8_t get_msb(const uint32_t number) {
uint8_t idx = 0;
Expand Down Expand Up @@ -65,4 +65,4 @@ std::string timespan_to_string(const int64_t time_ms) {
return stream.str();
}

} /* namespace utility */
} /* namespace utils */
10 changes: 10 additions & 0 deletions lib/uzlib_gzip_wrapper/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# UZLib GZIP Wrapper
This is a [UZLib](https://github.com/pfalcon/uzlib.git) wrapper specifically used to (de)compress GZIP files.

**IMPORTANT:** The current version of this library cannot handle compression yet.

This wrapper can handle both decompressing a file from a constant byte array, as well as from a callback.

This wrapper can handle a custom window size.

There are no usage examples at this point in time.
141 changes: 141 additions & 0 deletions lib/uzlib_gzip_wrapper/include/uzlib_gzip_wrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* uzlib_gzip_wrapper.h
*
* Created on: May 26, 2023
*
* Copyright (C) 2023 ToMe25.
* This project is licensed under the MIT License.
* The MIT license can be found in the project root and at https://opensource.org/licenses/MIT.
*/

#ifndef SRC_HTML_UZLIB_GZIP_WRAPPER_H_
#define SRC_HTML_UZLIB_GZIP_WRAPPER_H_

#include <uzlib.h>
#include <functional>

namespace gzip {

/**
* Initializes the static variables used by uzlib.
*/
void init();

/**
* A wrapper to help with storing the data associated with decompressing a gzip file using uzlib.
* Can currently only handle the entire compressed file being accessible as a single pointer block.
*/
class uzlib_ungzip_wrapper {
private:
/**
* The internal data store used by uzlib.
*/
uzlib_uncomp *decomp;

/**
* The number of already decompressed bytes.
*/
size_t index = 0;

/**
* The decompressed filesize in bytes module 2^32, if available.
* Or -1 if the filesize is not available.
*/
int32_t dlen = -1;

/**
* A single decompressed byte, decompressed in advance to work around the incorrect file end detection of uzlib.
*/
int16_t dcbuf = -1;

public:
/**
* Creates a new gzip wrapper to decompress the gzip file in the given memory block.
*
* For this the entire compressed file has to be in a single continuous memory block.
*
* With this constructor the uncompressed size is available immediately.
*
* @param cmp_start A pointer to the first byte of the gzip file.
* @param cmp_end A pointer to the first byte after the gzip file.
* @param wsize The window size used for decompression.
* Has to be at least as much as the window size used for compression.
* A pow(2, -wsize) byte buffer is allocated for decompression.
* The range of valid values is from -8 to -15.
* Values outside of this range will be clamped to this range.
*/
uzlib_ungzip_wrapper(const uint8_t *cmp_start, const uint8_t *cmp_end,
int8_t wsize);

/**
* Creates a new gzip wrapper to decompress a gzip file from a callback.
*
* This callback will be called each time `uzlib_uncomp->source` is empty.
* This means by default it will be called for every byte to read.
*
* It is however allowed to modify `uzlib_uncomp->source` and `uzlib_uncomp->source_limit`
* in this callback to allow reading a chunk of data at once.
* In this case the callback still has to return the **FIRST** read byte.
* If `uzlib_uncomp->source` is modified, it must point to the next byte after the returned byte.
*
* Once all bytes are read the callback must return -1.
*
* **WARNING:** This callback **MUST NOT** return the last four bytes of a gzip file, since those can't be decompressed.
*
* With this constructor the uncompressed size is only available once the file is read in its entirety.
*
* @param callback The callback to get the compressed data from.
* @param wsize The window size used for decompression.
* Has to be at least as much as the window size used for compression.
* A pow(2, -wsize) byte buffer is allocated for decompression.
* The range of valid values is from -8 to -15.
* Values outside of this range will be clamped to this range.
*/
uzlib_ungzip_wrapper(int (*callback)(uzlib_uncomp*), int8_t wsize);

/**
* Destroys this gzip wrapper, and removes its internal memory buffer.
*/
~uzlib_ungzip_wrapper();

/**
* Decompresses the next segment of the gzip file to the given memory buffer.
*
* Attempts to decompress a single additional byte in advance,
* to work around uzlib not detecting the file end when the buffer size exactly matches the uncompressed size.
*
* @param buf The memory buffer to write to.
* @param buf_size The max number of bytes to write to the buffer.
* @return The number of bytes written to the buffer.
*/
size_t decompress(uint8_t *buf, const size_t buf_size);

/**
* Gets the uncompressed size of the file to be decompressed, if available.
*
* The uncompressed size is always available once decompression is finished.
* If this wrapper was initialized with a memory block, rather than a callback,
* the uncompressed size is immediately available.
*
* @return The uncompressed file size.
*/
int32_t getDecompressedSize() const;

/**
* Gets the number of bytes that were already decompressed.
*
* @return The number of already decompressed bytes.
*/
uint32_t getDecompressed() const;

/**
* Checks whether the file was decompressed in its entirety.
*
* @return Whether the decompression is done.
*/
bool done() const;
};

} /* namespace gzip */

#endif /* SRC_HTML_UZLIB_GZIP_WRAPPER_H_ */
16 changes: 16 additions & 0 deletions lib/uzlib_gzip_wrapper/library.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "UZLibGzipWrapper",
"description": "A wrapper to help with storing the data associated with (de)compressing a GZIP file using UZLib. Can't handle compressing yet.",
"version": "1.0.0",
"license": "MIT",
"dependencies": [
{
"name": "uzlib",
"version": "https://github.com/pfalcon/uzlib.git"
},
{
"name": "FallbackLog",
"version": "FallbackLog"
}
]
}
Loading

0 comments on commit e7f185e

Please sign in to comment.