This is the multi-page printable view of this section. Click here to print...

Return to the regular view of this page

As of 2025-07-24

pack_bytes()

Generate a byte sequence by concatenating element data
    Generates a byte sequence by concatenating element data.
    uint8_t* pack_bytes(uint8_t* b, uint8_t* e, ...)

    pack_bytes takes container class begin(), end() iterators as parameters and writes the data specified by the following parameters into the container as a byte sequence.

    The data types given as variadic arguments are as follows.

    Data TypeBytesDescription
    uint8_t1
    uint16_t2Stored in big-endian order
    uint32_t4Stored in big-endian order
    uint8_t[N]NFixed-length array of uint8_t
    std::pair<char*,N>NPair of array and length for char* or uint8_t* arrays. Can be created with make_pair().
    smplbuf_u8& pack_bytes(smplbuf_u8& c, ...)

    pack_bytes takes a container object as a parameter and writes the data specified by the following parameters into the container as a byte sequence. It appends to the end using the container’s .push_back() method.

    The data types given as variadic arguments are as follows.

    Data TypeBytesDescription
    uint8_t1
    uint16_t2Stored in big-endian order
    uint32_t4Stored in big-endian order
    uint8_t[N]NFixed-length array of uint8_t
    std::pair<char*,N>NPair of array and length for char* or uint8_t* arrays. Can be created with make_pair().
    smplbuf_u8?.size()smplbuf<> container of uint8_t type. Stores data of container length (.size()).

    Example

    auto&& rx = the_twelite.receiver.read();
    
    smplbuf<uint8_t, 128> buf;
    mwx::pack_bytes(buf
    	, uint8_t(rx.get_addr_src_lid())	// src addr (LID)
    	, uint8_t(0xCC)							      // cmd id (0xCC, fixed)
    	, uint8_t(rx.get_psRxDataApp()->u8Seq)	// sequence number
    	, uint32_t(rx.get_addr_src_long())		// src addr (long)
    	, uint32_t(rx.get_addr_dst())			// dst addr
    	, uint8_t(rx.get_lqi())					  // LQI
    	, uint16_t(rx.get_length())				// payload length
    	, rx.get_payload() 						    // payload
    );

    In this example, attributes and payload of the received packet are re-stored into another buffer buf.

    Background

    To simplify the description of byte arrays of type uint8_t used for generating data payloads of wireless packets and extracting data.

    auto&& rx = the_twelite.receiver.read();
    
    uint8_t data[128];
    data[0] = rx.get_addr_src_lid();
    data[1] = 0xCC;
    data[2] = rx.get_psRxDataApp()->u8Seq;
    data[4] = rx.get_addr_src_long() & 0x000000FF;
    data[5] = (rx.get_addr_src_long() & 0x0000FF00) >> 8;
    data[6] = (rx.get_addr_src_long() & 0x00FF0000) >> 16;
    data[7] = (rx.get_addr_src_long() & 0xFF000000) >> 24;
    ...

    The above is the simplest description, but a byte array can be generated using Byte array utils as follows.

    auto&& rx = the_twelite.receiver.read();
    
    uint8_t data[128], *q = data;
    S_OCTET(q, rx.get_addr_src_lid());
    S_OCTET(q, 0xCC);
    S_OCTET(q, rx.get_psRxDataApp()->u8Seq);
    S_DWORD(q, rx.get_addr_src_long());
    S_DWORD(q, rx.get_addr_dst());
    S_OCTET(q, rx.get_lqi());
    S_WORD(q, rx.get_length());
    for (auto x : rx.get_payload()) {
      S_OCTET(q, x);
    }