1#ifndef LIGHTMODBUS_BASE_IMPL_H
2#define LIGHTMODBUS_BASE_IMPL_H
33 uint8_t *old_ptr = buffer->
data;
34 buffer->
data = (uint8_t*)realloc(buffer->
data, size);
100 uint16_t size = pduSize;
101 if (pduSize) size += buffer->
padding;
128 uint16_t crc = 0xFFFF;
130 for (uint16_t i = 0; i < length; i++)
132 crc ^= (uint16_t) data[i];
133 for (uint8_t j = 8; j != 0; j--)
135 if ((crc & 0x0001) != 0)
Common types and functions (header)
#define LIGHTMODBUS_WARN_UNUSED
Definition base.h:19
ModbusError
Represtents different kinds of errors.
Definition base.h:137
@ MODBUS_ERROR_ALLOC
Memory allocation error.
Definition base.h:163
@ MODBUS_OK
No error.
Definition base.h:143
#define MODBUS_NO_ERROR()
Construcs a ModbusErrorInfo object for which modbusIsOK() is guaranteed to return true.
Definition base.h:86
#define LIGHTMODBUS_RET_ERROR
Return type for library functions returning ModbusErrorInfo that should be handled properly.
Definition base.h:49
ModbusError(* ModbusAllocator)(struct ModbusBuffer *buffer, uint16_t size, void *context)
Pointer to a memory allocator function.
Definition base.h:259
void modbusBufferFree(ModbusBuffer *buffer, void *context)
Frees memory allocated inside the buffer.
Definition base.impl.h:68
uint16_t modbusCRC(const uint8_t *data, uint16_t length)
Calculates 16-bit Modbus CRC of provided data.
Definition base.impl.h:126
ModbusErrorInfo modbusBufferInit(ModbusBuffer *buffer, ModbusAllocator allocator)
Initializes a buffer for use.
Definition base.impl.h:51
ModbusError modbusBufferAllocateADU(ModbusBuffer *buffer, uint16_t pduSize, void *context)
Allocates memory to hold Modbus ADU.
Definition base.impl.h:98
ModbusError modbusDefaultAllocator(ModbusBuffer *buffer, uint16_t size, void *context)
The default memory allocator based on realloc()
Definition base.impl.h:21
void modbusBufferDestroy(ModbusBuffer *buffer, void *context)
Equivalent of modbusBufferFree() \copydetail modbusBufferFree()
Definition base.impl.h:78
Stores a Modbus frame.
Definition base.h:276
uint16_t length
Length of the entire frame (PDU size + padding)
Definition base.h:282
ModbusAllocator allocator
Pointer to the allocator function.
Definition base.h:278
uint8_t pduOffset
PDU offset relative to the beginning of the frame.
Definition base.h:285
uint8_t * data
Pointer to the frame buffer.
Definition base.h:280
uint8_t * pdu
A pointer to the PDU section of the frame.
Definition base.h:281
uint8_t padding
Number of extra bytes surrounding the PDU.
Definition base.h:284