29#ifndef PROTOCORE_DNP3_H
30#define PROTOCORE_DNP3_H
34#if PROTOCORE_ENABLE_DNP3
38#define DNP3_START0 0x05
39#define DNP3_START1 0x64
40#define DNP3_MAX_USER_DATA 250
44#define DNP3_BLOCK_LEN 16
46#define DNP3_HEADER_LEN 8
47#define DNP3_HEADER_BLOCK_LEN 10
48#define DNP3_LEN_OVERHEAD 5
51uint16_t protocore_dnp3_crc(
const uint8_t *data,
size_t len);
60size_t protocore_dnp3_build_frame(uint8_t *buf,
size_t cap, uint8_t control, uint16_t dest, uint16_t src,
61 const uint8_t *user_data,
size_t user_data_len);
80proto_bool protocore_dnp3_parse_frame(
const uint8_t *buf,
size_t len, Dnp3Frame *out, uint8_t *out_user,
size_t out_cap,
81 size_t *out_user_len);
90#define DNP3_TR_FIN 0x80u
91#define DNP3_TR_FIR 0x40u
92#define DNP3_TR_SEQ_MASK 0x3Fu
93#define DNP3_TR_MAX_APP 249
102size_t protocore_dnp3_build_transport_segment(uint8_t *out,
size_t cap,
proto_bool fir,
proto_bool fin, uint8_t seq,
103 const uint8_t *app_data,
size_t app_len);
106enum Dnp3TransportResult
126void protocore_dnp3_transport_rx_init(Dnp3TransportRx *r, uint8_t *buf,
size_t cap);
132int protocore_dnp3_transport_feed(Dnp3TransportRx *r,
const uint8_t *user,
size_t user_len);
142#define DNP3_AC_FIR 0x80u
143#define DNP3_AC_FIN 0x40u
144#define DNP3_AC_CON 0x20u
145#define DNP3_AC_UNS 0x10u
146#define DNP3_AC_SEQ_MASK 0x0Fu
149#define DNP3_FC_CONFIRM 0x00u
150#define DNP3_FC_READ 0x01u
151#define DNP3_FC_WRITE 0x02u
152#define DNP3_FC_SELECT 0x03u
153#define DNP3_FC_OPERATE 0x04u
154#define DNP3_FC_DIRECT_OPERATE 0x05u
155#define DNP3_FC_DIRECT_OPERATE_NR 0x06u
156#define DNP3_FC_COLD_RESTART 0x0Du
157#define DNP3_FC_WARM_RESTART 0x0Eu
158#define DNP3_FC_RESPONSE 0x81u
159#define DNP3_FC_UNSOLICITED_RESPONSE 0x82u
162#define DNP3_IIN_BROADCAST 0x0001u
163#define DNP3_IIN_CLASS1_EVENTS 0x0002u
164#define DNP3_IIN_CLASS2_EVENTS 0x0004u
165#define DNP3_IIN_CLASS3_EVENTS 0x0008u
166#define DNP3_IIN_NEED_TIME 0x0010u
167#define DNP3_IIN_LOCAL_CONTROL 0x0020u
168#define DNP3_IIN_DEVICE_TROUBLE 0x0040u
169#define DNP3_IIN_DEVICE_RESTART 0x0080u
170#define DNP3_IIN_FUNC_NOT_SUPPORTED 0x0100u
171#define DNP3_IIN_OBJECT_UNKNOWN 0x0200u
172#define DNP3_IIN_PARAM_ERROR 0x0400u
173#define DNP3_IIN_EVENT_OVERFLOW 0x0800u
174#define DNP3_IIN_ALREADY_EXECUTING 0x1000u
175#define DNP3_IIN_CONFIG_CORRUPT 0x2000u
189 const uint8_t *objects;
200size_t protocore_dnp3_build_app_request(uint8_t *out,
size_t cap, uint8_t app_control, uint8_t fc,
201 const uint8_t *objects,
size_t obj_len);
207size_t protocore_dnp3_build_app_response(uint8_t *out,
size_t cap, uint8_t app_control, uint8_t fc, uint16_t iin,
208 const uint8_t *objects,
size_t obj_len);
214proto_bool protocore_dnp3_parse_app_header(
const uint8_t *frag,
size_t len, Dnp3AppHeader *out);
220#define DNP3_QUAL_PREFIX_MASK 0x70u
221#define DNP3_QUAL_PREFIX_SHIFT 4u
222#define DNP3_QUAL_RANGE_MASK 0x0Fu
225#define DNP3_RANGE_START_STOP_1 0x00u
226#define DNP3_RANGE_START_STOP_2 0x01u
227#define DNP3_RANGE_START_STOP_4 0x02u
228#define DNP3_RANGE_NO_RANGE 0x06u
229#define DNP3_RANGE_COUNT_1 0x07u
230#define DNP3_RANGE_COUNT_2 0x08u
231#define DNP3_RANGE_COUNT_4 0x09u
245 const uint8_t *objects;
254proto_bool protocore_dnp3_parse_object_header(
const uint8_t *buf,
size_t len, Dnp3ObjectHeader *out);
262size_t protocore_dnp3_build_object_header_range(uint8_t *buf,
size_t cap, uint8_t group, uint8_t variation,
263 uint32_t start, uint32_t stop);
269size_t protocore_dnp3_build_object_header_all(uint8_t *buf,
size_t cap, uint8_t group, uint8_t variation);
274#define DNP3_CROB_LEN 11
277#define DNP3_CROB_OP_NUL 0x00u
278#define DNP3_CROB_OP_PULSE_ON 0x01u
279#define DNP3_CROB_OP_PULSE_OFF 0x02u
280#define DNP3_CROB_OP_LATCH_ON 0x03u
281#define DNP3_CROB_OP_LATCH_OFF 0x04u
284#define DNP3_CROB_TCC_NUL 0x00u
285#define DNP3_CROB_TCC_CLOSE 0x01u
286#define DNP3_CROB_TCC_TRIP 0x02u
295size_t protocore_dnp3_build_crob(uint8_t *buf,
size_t cap, uint8_t op_type, uint8_t tcc,
proto_bool clear,
296 uint8_t count, uint32_t on_time_ms, uint32_t off_time_ms);
302#define DNP3_AOB_LEN 5
310size_t protocore_dnp3_build_aob32(uint8_t *buf,
size_t cap, int32_t value);
317size_t protocore_dnp3_build_aob_float(uint8_t *buf,
size_t cap,
float value);
#define PROTOCORE_BEGIN_DECLS
Give a header's declarations C linkage, so their symbol names carry no parameter types.
_Bool proto_bool
The truth value.
#define PROTOCORE_END_DECLS