hideakitai / MsgPack

MessagePack implementation for Arduino (compatible with other C++ apps) / msgpack.org[Arduino]

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Examples to pack "bin 8" byte sequences (0xC4 coding)?

FerGT50 opened this issue · comments

Hello,

let's say I have a byte buffer, like

uint8_t buffer[64] = {25, 108, ... etc};

I do not want to encode it as "Array", but as a "Bin 8".
How to I use your MsgPack::bin_t type in practice?
Is there an example you may have (I did not find one in the library package)?

Thanks

As mentioned in README, the following types are encoded to "bin". So uint8_t buffer[64] will be encoded to "bin" as is. Msgpack::bin_t<uint8_t> is also available (alias for std::vector<uint8_t>)

  • unsigned char* (need to serialize(ptr, size) or pack(ptr, size))
  • unsigned char[] (need to serialize(ptr, size) or pack(ptr, size))
  • std::vector<char> (MsgPack::bin_t<char>)
  • std::vector<unsigned char> (MsgPack::bin_t<unsigned char>)
  • std::array<char>
  • std::array<unsigned char>

As mentioned in README, the following types are encoded to "bin". So uint8_t buffer[64] will be encoded to "bin" as is.

That's what I thought, so I tried this simple test:

#include <MsgPack.h>
#include <base64.hpp> // For debug

#define DEBUG_SERIAL Serial

#define MSGPACK_DEBUGLOG_ENABLE

#define SERIAL_DEBUGMODE



/////////////////////////
// Forward Declarations
/////////////////////////

void prepareMessagePack();


//////////
// SETUP
//////////

void setup() 
{  
  #ifdef SERIAL_DEBUGMODE
  DEBUG_SERIAL.begin(115200);
  while (!DEBUG_SERIAL)
  {    
  }
  delay(2000);
  DEBUG_SERIAL.println();
  #endif
  
  delay(2000);

  prepareMessagePack();
}


/////////
// LOOP
/////////

void loop() 
{
}


void prepareMessagePack()
{ 
  uint32_t u32_1 = 10000UL;
  uint16_t u16_1 = 1000U;
  uint32_t u32_2 = 20000UL;
  String s_1 = "Test1";
  uint8_t buffer_1[32] = {100, 247, 101, 203, 134, 82, 87, 156, 248, 88, 46, 95, 26, 26, 58, 124, 165, 123, 139, 160, 11, 191, 161, 7, 157, 2, 154, 35, 74, 130, 144, 170}; 
  MsgPack::Packer msgPacker;

  msgPacker.serialize (u32_1, u16_1, u32_2, s_1, buffer_1);

  const uint8_t* packData = msgPacker.data();
  uint32_t packLen = msgPacker.size();

  char debugB64[1024] = "";
  DEBUG_SERIAL.println("\nMessagePack B64:");
  encode_base64((unsigned char*)(packData), packLen, (unsigned char*)debugB64);
  DEBUG_SERIAL.println((char *)debugB64);
}

Unfortunately, it does not compile (ESP-32), throwing loads of errors.
I cannot copy all the compilation log here (too long), here are the first rows:

In file included from L:\Progetti\Arduino\libraries\MsgPack/MsgPack.h:13,
                 from L:\Progetti\Arduino\ESP32_MessagePackTest1\ESP32_MessagePackTest1.ino:1:
L:\Progetti\Arduino\libraries\MsgPack/MsgPack/Packer.h: In instantiation of 'typename std::enable_if<(((! std::is_pointer<_Tp>::value) || std::is_same<First, const char*>::value) || std::is_same<First, const __FlashStringHelper*>::value)>::type arduino::msgpack::Packer::serialize(const First&, Rest&& ...) [with First = unsigned char [32]; Rest = {}; typename std::enable_if<(((! std::is_pointer<_Tp>::value) || std::is_same<First, const char*>::value) || std::is_same<First, const __FlashStringHelper*>::value)>::type = void]':
L:\Progetti\Arduino\libraries\MsgPack/MsgPack/Packer.h:51:13:   recursively required from 'typename std::enable_if<(((! std::is_pointer<_Tp>::value) || std::is_same<First, const char*>::value) || std::is_same<First, const __FlashStringHelper*>::value)>::type arduino::msgpack::Packer::serialize(const First&, Rest&& ...) [with First = short unsigned int; Rest = {unsigned int&, String&, unsigned char (&)[32]}; typename std::enable_if<(((! std::is_pointer<_Tp>::value) || std::is_same<First, const char*>::value) || std::is_same<First, const __FlashStringHelper*>::value)>::type = void]'
L:\Progetti\Arduino\libraries\MsgPack/MsgPack/Packer.h:51:13:   required from 'typename std::enable_if<(((! std::is_pointer<_Tp>::value) || std::is_same<First, const char*>::value) || std::is_same<First, const __FlashStringHelper*>::value)>::type arduino::msgpack::Packer::serialize(const First&, Rest&& ...) [with First = unsigned int; Rest = {short unsigned int&, unsigned int&, String&, unsigned char (&)[32]}; typename std::enable_if<(((! std::is_pointer<_Tp>::value) || std::is_same<First, const char*>::value) || std::is_same<First, const __FlashStringHelper*>::value)>::type = void]'
L:\Progetti\Arduino\ESP32_MessagePackTest1\ESP32_MessagePackTest1.ino:58:58:   required from here
L:\Progetti\Arduino\libraries\MsgPack/MsgPack/Packer.h:50:13: error: no matching function for call to 'arduino::msgpack::Packer::pack(const unsigned char [32])'
             pack(first);
             ^~~~
L:\Progetti\Arduino\libraries\MsgPack/MsgPack/Packer.h:136:14: note: candidate: 'template<class T> typename std::enable_if<(std::is_same<T, arduino::msgpack::object::nil_t>::value || std::is_same<T, std::nullptr_t>::value)>::type arduino::msgpack::Packer::pack(const T&)'
         auto pack(const T& n) -> typename std::enable_if<
              ^~~~
L:\Progetti\Arduino\libraries\MsgPack/MsgPack/Packer.h:136:14: note:   template argument deduction/substitution failed:
L:\Progetti\Arduino\libraries\MsgPack/MsgPack/Packer.h: In substitution of 'template<class T> typename std::enable_if<(std::is_same<T, arduino::msgpack::object::nil_t>::value || std::is_same<T, std::nullptr_t>::value)>::type arduino::msgpack::Packer::pack(const T&) [with T = unsigned char [32]]':
L:\Progetti\Arduino\libraries\MsgPack/MsgPack/Packer.h:51:13:   recursively required from 'typename std::enable_if<(((! std::is_pointer<_Tp>::value) || std::is_same<First, const char*>::value) || std::is_same<First, const __FlashStringHelper*>::value)>::type arduino::msgpack::Packer::serialize(const First&, Rest&& ...) [with First = short unsigned int; Rest = {unsigned int&, String&, unsigned char (&)[32]}; typename std::enable_if<(((! std::is_pointer<_Tp>::value) || std::is_same<First, const char*>::value) || std::is_same<First, const __FlashStringHelper*>::value)>::type = void]'
L:\Progetti\Arduino\libraries\MsgPack/MsgPack/Packer.h:51:13:   required from 'typename std::enable_if<(((! std::is_pointer<_Tp>::value) || std::is_same<First, const char*>::value) || std::is_same<First, const __FlashStringHelper*>::value)>::type arduino::msgpack::Packer::serialize(const First&, Rest&& ...) [with First = unsigned int; Rest = {short unsigned int&, unsigned int&, String&, unsigned char (&)[32]}; typename std::enable_if<(((! std::is_pointer<_Tp>::value) || std::is_same<First, const char*>::value) || std::is_same<First, const __FlashStringHelper*>::value)>::type = void]'
L:\Progetti\Arduino\ESP32_MessagePackTest1\ESP32_MessagePackTest1.ino:58:58:   required from here
L:\Progetti\Arduino\libraries\MsgPack/MsgPack/Packer.h:136:14: error: no type named 'type' in 'struct std::enable_if<false, void>'
L:\Progetti\Arduino\libraries\MsgPack/MsgPack/Packer.h: In instantiation of 'typename std::enable_if<(((! std::is_pointer<_Tp>::value) || std::is_same<First, const char*>::value) || std::is_same<First, const __FlashStringHelper*>::value)>::type arduino::msgpack::Packer::serialize(const First&, Rest&& ...) [with First = unsigned char [32]; Rest = {}; typename std::enable_if<(((! std::is_pointer<_Tp>::value) || std::is_same<First, const char*>::value) || std::is_same<First, const __FlashStringHelper*>::value)>::type = void]':
L:\Progetti\Arduino\libraries\MsgPack/MsgPack/Packer.h:51:13:   recursively required from 'typename std::enable_if<(((! std::is_pointer<_Tp>::value) || std::is_same<First, const char*>::value) || std::is_same<First, const __FlashStringHelper*>::value)>::type arduino::msgpack::Packer::serialize(const First&, Rest&& ...) [with First = short unsigned int; Rest = {unsigned int&, String&, unsigned char (&)[32]}; typename std::enable_if<(((! std::is_pointer<_Tp>::value) || std::is_same<First, const char*>::value) || std::is_same<First, const __FlashStringHelper*>::value)>::type = void]'
L:\Progetti\Arduino\libraries\MsgPack/MsgPack/Packer.h:51:13:   required from 'typename std::enable_if<(((! std::is_pointer<_Tp>::value) || std::is_same<First, const char*>::value) || std::is_same<First, const __FlashStringHelper*>::value)>::type arduino::msgpack::Packer::serialize(const First&, Rest&& ...) [with First = unsigned int; Rest = {short unsigned int&, unsigned int&, String&, unsigned char (&)[32]}; typename std::enable_if<(((! std::is_pointer<_Tp>::value) || std::is_same<First, const char*>::value) || std::is_same<First, const __FlashStringHelper*>::value)>::type = void]'
L:\Progetti\Arduino\ESP32_MessagePackTest1\ESP32_MessagePackTest1.ino:58:58:   required from here
L:\Progetti\Arduino\libraries\MsgPack/MsgPack/Packer.h:146:14: note: candidate: 'template<class T> typename std::enable_if<std::is_same<T, bool>::value>::type arduino::msgpack::Packer::pack(const T&)'
         auto pack(const T& b) -> typename std::enable_if<std::is_same<T, bool>::value>::type {
              ^~~~

What am I doing wrong?

Thanks for your support

You should use packer.pack(buffer_1, sizeof(buffer_1)) manually because the pointer itself doesn’t have the information of its size. You can serialize() it by using std::vector or std::array.