mirror of
https://github.com/sle118/squeezelite-esp32.git
synced 2025-12-09 21:17:18 +03:00
more nanopb
This commit is contained in:
46
components/spotify/cspot/bell/include/NanoPBHelper.h
Normal file
46
components/spotify/cspot/bell/include/NanoPBHelper.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#ifndef BELL_NANOPB_HELPER_H
|
||||
#define BELL_NANOPB_HELPER_H
|
||||
|
||||
#include <vector>
|
||||
#include "pb_encode.h"
|
||||
#include "pb_decode.h"
|
||||
#include <string>
|
||||
|
||||
std::vector<uint8_t> pbEncode(const pb_msgdesc_t *fields, const void *src_struct);
|
||||
|
||||
pb_bytes_array_t* vectorToPbArray(const std::vector<uint8_t>& vectorToPack);
|
||||
|
||||
void packString(char* &dst, std::string stringToPack);
|
||||
|
||||
std::vector<uint8_t> pbArrayToVector(pb_bytes_array_t* pbArray);
|
||||
|
||||
template <typename T>
|
||||
T pbDecode(const pb_msgdesc_t *fields, std::vector<uint8_t> &data)
|
||||
{
|
||||
|
||||
T result = {};
|
||||
// Create stream
|
||||
pb_istream_t stream = pb_istream_from_buffer(&data[0], data.size());
|
||||
|
||||
// Decode the message
|
||||
if (pb_decode(&stream, fields, &result) == false) {
|
||||
printf("Decode failed: %s\n", PB_GET_ERROR(&stream));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void pbDecode(T &result, const pb_msgdesc_t *fields, std::vector<uint8_t> &data)
|
||||
{
|
||||
// Create stream
|
||||
pb_istream_t stream = pb_istream_from_buffer(&data[0], data.size());
|
||||
|
||||
// Decode the message
|
||||
if (pb_decode(&stream, fields, &result) == false) {
|
||||
printf("Decode failed: %s\n", PB_GET_ERROR(&stream));
|
||||
}
|
||||
}
|
||||
|
||||
void pbFree(const pb_msgdesc_t *fields, void *src_struct);
|
||||
|
||||
#endif
|
||||
56
components/spotify/cspot/bell/src/NanoPBHelper.cpp
Normal file
56
components/spotify/cspot/bell/src/NanoPBHelper.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include "NanoPBHelper.h"
|
||||
|
||||
static bool vectorWrite(pb_ostream_t *stream, const pb_byte_t *buf, size_t count)
|
||||
{
|
||||
size_t i;
|
||||
auto *dest = reinterpret_cast<std::vector<uint8_t> *>(stream->state);
|
||||
|
||||
dest->insert(dest->end(), buf, buf + count);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
pb_ostream_t pb_ostream_from_vector(std::vector<uint8_t> &vec)
|
||||
{
|
||||
pb_ostream_t stream;
|
||||
|
||||
stream.callback = &vectorWrite;
|
||||
stream.state = &vec;
|
||||
stream.max_size = 100000;
|
||||
stream.bytes_written = 0;
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> pbEncode(const pb_msgdesc_t *fields, const void *src_struct)
|
||||
{
|
||||
std::vector<uint8_t> vecData(0);
|
||||
pb_ostream_t stream = pb_ostream_from_vector(vecData);
|
||||
pb_encode(&stream, fields, src_struct);
|
||||
|
||||
return vecData;
|
||||
}
|
||||
|
||||
void packString(char *&dst, std::string stringToPack)
|
||||
{
|
||||
dst = (char *)malloc((strlen(stringToPack.c_str()) + 1) * sizeof(char));
|
||||
strcpy(dst, stringToPack.c_str());
|
||||
}
|
||||
|
||||
void pbFree(const pb_msgdesc_t *fields, void *src_struct) {
|
||||
pb_release(fields, src_struct);
|
||||
}
|
||||
|
||||
pb_bytes_array_t* vectorToPbArray(const std::vector<uint8_t>& vectorToPack)
|
||||
{
|
||||
auto size = static_cast<pb_size_t>(vectorToPack.size());
|
||||
auto result = static_cast<pb_bytes_array_t *>(
|
||||
malloc(PB_BYTES_ARRAY_T_ALLOCSIZE(size)));
|
||||
result->size = size;
|
||||
memcpy(result->bytes, vectorToPack.data(), size);
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> pbArrayToVector(pb_bytes_array_t* pbArray) {
|
||||
return std::vector<uint8_t>(pbArray->bytes, pbArray->bytes + pbArray->size);
|
||||
}
|
||||
Reference in New Issue
Block a user