1 /* Copyright 2018 Espressif Systems (Shanghai) PTE LTD
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef _MB_CONTROLLER_MASTER_H
17 #define _MB_CONTROLLER_MASTER_H
18 
19 #include <sys/queue.h>              // for list
20 #include "freertos/FreeRTOS.h"      // for task creation and queue access
21 #include "freertos/task.h"          // for task api access
22 #include "freertos/event_groups.h"  // for event groups
23 #include "driver/uart.h"            // for UART types
24 #include "errno.h"                  // for errno
25 #include "esp_log.h"                // for log write
26 #include "string.h"                 // for strerror()
27 #include "esp_modbus_common.h"      // for common types
28 #include "esp_modbus_master.h"      // for public master types
29 #include "esp_modbus_callbacks.h"
30 
31 /* ----------------------- Defines ------------------------------------------*/
32 
33 /**
34  * @brief Request mode for parameter to use in data dictionary
35  */
36 typedef enum {
37     MB_PARAM_READ, /*!< Read parameter values. */
38     MB_PARAM_WRITE /*!< Write parameter values. */
39 } mb_param_mode_t;
40 
41 /**
42  * @brief Device communication parameters for master
43  */
44 typedef struct {
45     mb_mode_type_t mode;                    /*!< Modbus communication mode */
46     uint8_t dummy;                          /*!< Dummy field */
47     uart_port_t port;                       /*!< Modbus communication port (UART) number */
48     uint32_t baudrate;                      /*!< Modbus baudrate */
49     uart_parity_t parity;                   /*!< Modbus UART parity settings */
50 } mb_master_comm_info_t;
51 
52 #if MB_MASTER_TCP_ENABLED
53 /**
54  * @brief Modbus slave addr list item for the master
55  */
56 typedef struct mb_slave_addr_entry_s{
57     uint16_t index;                             /*!< Index of the slave address */
58     const char* ip_address;                     /*!< IP address string of the slave */
59     uint8_t slave_addr;                         /*!< Short slave address */
60     void* p_data;                               /*!< pointer to data structure */
61     LIST_ENTRY(mb_slave_addr_entry_s) entries;  /*!< The slave address entry */
62 } mb_slave_addr_entry_t;
63 #endif
64 
65 /**
66  * @brief Modbus controller handler structure
67  */
68 typedef struct {
69     mb_port_type_t port_type;                           /*!< Modbus port type */
70     mb_communication_info_t mbm_comm;                   /*!< Modbus communication info */
71     uint8_t* mbm_reg_buffer_ptr;                        /*!< Modbus data buffer pointer */
72     uint16_t mbm_reg_buffer_size;                       /*!< Modbus data buffer size */
73     TaskHandle_t mbm_task_handle;                       /*!< Modbus task handle */
74     EventGroupHandle_t mbm_event_group;                 /*!< Modbus controller event group */
75     const mb_parameter_descriptor_t* mbm_param_descriptor_table; /*!< Modbus controller parameter description table */
76     size_t mbm_param_descriptor_size;                   /*!< Modbus controller parameter description table size*/
77 #if MB_MASTER_TCP_ENABLED
78     LIST_HEAD(mbm_slave_addr_info_, mb_slave_addr_entry_s) mbm_slave_list; /*!< Slave address information list */
79     uint16_t mbm_slave_list_count;
80 #endif
81 } mb_master_options_t;
82 
83 typedef esp_err_t (*iface_get_cid_info)(uint16_t, const mb_parameter_descriptor_t**); /*!< Interface get_cid_info method */
84 typedef esp_err_t (*iface_get_parameter)(uint16_t, char*, uint8_t*, uint8_t*);        /*!< Interface get_parameter method */
85 typedef esp_err_t (*iface_send_request)(mb_param_request_t*, void*);                  /*!< Interface send_request method */
86 typedef esp_err_t (*iface_set_descriptor)(const mb_parameter_descriptor_t*, const uint16_t); /*!< Interface set_descriptor method */
87 typedef esp_err_t (*iface_set_parameter)(uint16_t, char*, uint8_t*, uint8_t*);        /*!< Interface set_parameter method */
88 
89 /**
90  * @brief Modbus controller interface structure
91  */
92 typedef struct {
93     // Master object interface options
94     mb_master_options_t opts;
95 
96     // Public interface methods
97     iface_init init;                        /*!< Interface method init */
98     iface_destroy destroy;                  /*!< Interface method destroy */
99     iface_setup setup;                      /*!< Interface method setup */
100     iface_start start;                      /*!< Interface method start */
101     iface_get_cid_info get_cid_info;        /*!< Interface get_cid_info method */
102     iface_get_parameter get_parameter;      /*!< Interface get_parameter method */
103     iface_send_request send_request;        /*!< Interface send_request method */
104     iface_set_descriptor set_descriptor;    /*!< Interface set_descriptor method */
105     iface_set_parameter set_parameter;      /*!< Interface set_parameter method */
106     // Modbus register calback function pointers
107     reg_discrete_cb master_reg_cb_discrete; /*!< Stack callback discrete rw method */
108     reg_input_cb master_reg_cb_input;       /*!< Stack callback input rw method */
109     reg_holding_cb master_reg_cb_holding;   /*!< Stack callback holding rw method */
110     reg_coils_cb master_reg_cb_coils;       /*!< Stack callback coils rw method */
111 } mb_master_interface_t;
112 
113 #endif //_MB_CONTROLLER_MASTER_H
114