1 /*
2  * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3  */
4 
5 #ifndef PDU_H
6 #define PDU_H
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 
12 /* PDU len and payload offset define */
13 #define PDU_LENGTH_MAX     253
14 #define PDU_LENGTH_MIN     1
15 #define PDU_FUNC_FIELD_OFF 0
16 #define PDU_DATA_FIELD_OFF 1
17 
18 /* slave addr define */
19 #define SLAVE_ADDR_BROADCAST 0
20 #define SLAVE_ADDR_MIN       1
21 #define SLAVE_ADDR_MAX       247
22 
23 /* func code */
24 #define FUNC_CODE_READ_COILS               0x01
25 #define FUNC_CODE_READ_DISCRETE_INPUTS     0x02
26 #define FUNC_CODE_READ_HOLDING_REGISTERS   0x03
27 #define FUNC_CODE_READ_INPUT_REGISTERS     0x04
28 #define FUNC_CODE_WRITE_SINGLE_COIL        0x05
29 #define FUNC_CODE_WRITE_SINGLE_REGISTERS   0x06
30 #define FUNC_CODE_WRITE_MULTIPLE_COILS     0x0F
31 #define FUNC_CODE_WRITE_MULTIPLE_REGISTERS 0x10
32 
33 /* quantity max */
34 #define QUANTITY_MAX_FUNC01 2000
35 #define QUANTITY_MAX_FUNC03 125
36 
37 /* exception code */
38 #define EXCEPTION_ILLEGAL_FUNCTION     0x01
39 #define EXCEPTION_ILLEGAL_DATA_ADDRESS 0x02
40 #define EXCEPTION_ILLEGAL_DATA_VALUE   0x03
41 #define EXCEPTION_SLAVE_DEVICE_FAILURE 0x04
42 #define EXCEPTION_ACKNOWLEDGE          0x05
43 #define EXCEPTION_SLAVE_DEVICE_BUSY    0x06
44 #define EXCEPTION_MEMORY_PARITY_ERROR  0x08
45 #define EXCEPTION_GATEWAY_PATH_FAILED  0x0A
46 #define EXCEPTION_GATEWAY_TGT_FAILED   0x0B
47 
48 mb_status_t pdu_type122_assemble(mb_handler_t *req_handler, uint8_t field0, uint16_t field1, uint16_t field2);
49 
50 mb_status_t pdu_type1221n_assemble(mb_handler_t *req_handler, uint8_t function_code, uint16_t start_addr,
51                                    uint16_t quantity, uint8_t byte_count, uint8_t *outputs_buf);
52 
53 mb_status_t pdu_type11n_disassemble(mb_handler_t *req_handler, uint8_t function_code, uint8_t *respond_buf,
54                                     uint8_t *respond_count);
55 
56 mb_status_t pdu_type122_disassemble(mb_handler_t *req_handler, uint8_t function_code, uint16_t *data1,
57                                     uint16_t *data2, uint8_t* exception_code);
58 
59 #ifdef __cplusplus
60 }
61 #endif
62 
63 #endif /* PDU_H */
64