liblightmodbus 3.0
A lightweight, header-only, hardware-agnostic Modbus RTU/TCP library
Loading...
Searching...
No Matches
base.impl.h
Go to the documentation of this file.
1#ifndef LIGHTMODBUS_BASE_IMPL_H
2#define LIGHTMODBUS_BASE_IMPL_H
3
4#include "base.h"
5#include <stdlib.h>
6
22{
23 // Make sure to handle the case when *ptr = NULL and size = 0
24 // We don't want to allocate any memory then, but realloc(NULL, 0) would
25 // result in malloc(0)
26 if (!size)
27 {
28 free(buffer->data);
29 buffer->data = NULL;
30 }
31 else
32 {
33 uint8_t *old_ptr = buffer->data;
34 buffer->data = (uint8_t*)realloc(buffer->data, size);
35
36 if (!buffer->data)
37 {
38 free(old_ptr);
39 return MODBUS_ERROR_ALLOC;
40 }
41 }
42
43 return MODBUS_OK;
44}
45
52{
53 *buffer = (ModbusBuffer){
54 .allocator = allocator,
55 .data = NULL,
56 .pdu = NULL,
57 .length = 0,
58 .padding = 0,
59 .pduOffset = 0,
60 };
61 return MODBUS_NO_ERROR();
62}
63
68void modbusBufferFree(ModbusBuffer *buffer, void *context)
69{
70 ModbusError err = modbusBufferAllocateADU(buffer, 0, context);
71 (void) err;
72}
73
78void modbusBufferDestroy(ModbusBuffer *buffer, void *context)
79{
80 modbusBufferFree(buffer, context);
81}
82
99{
100 uint16_t size = pduSize;
101 if (pduSize) size += buffer->padding;
102
103 ModbusError err = buffer->allocator(buffer, size, context);
104
105 if (err == MODBUS_ERROR_ALLOC || size == 0)
106 {
107 buffer->data = NULL;
108 buffer->pdu = NULL;
109 buffer->length = 0;
110 }
111 else
112 {
113 buffer->pdu = buffer->data + buffer->pduOffset;
114 buffer->length = size;
115 }
116
117 return err;
118}
119
126LIGHTMODBUS_WARN_UNUSED uint16_t modbusCRC(const uint8_t *data, uint16_t length)
127{
128 uint16_t crc = 0xFFFF;
129
130 for (uint16_t i = 0; i < length; i++)
131 {
132 crc ^= (uint16_t) data[i];
133 for (uint8_t j = 8; j != 0; j--)
134 {
135 if ((crc & 0x0001) != 0)
136 {
137 crc >>= 1;
138 crc ^= 0xA001;
139 }
140 else
141 crc >>= 1;
142 }
143 }
144
145 return crc;
146}
147
148#endif
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