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 #include "esp_err.h"            // for esp_err_t
17 #include "mbc_master.h"         // for master interface define
18 #include "esp_modbus_master.h"  // for public interface defines
19 #include "esp_modbus_callbacks.h"   // for callback functions
20 
21 static const char TAG[] __attribute__((unused)) = "MB_CONTROLLER_MASTER";
22 
23 // This file implements public API for Modbus master controller.
24 // These functions are wrappers for interface functions of the controller
25 static mb_master_interface_t* master_interface_ptr = NULL;
26 
mbc_master_init_iface(void * handler)27 void mbc_master_init_iface(void* handler)
28 {
29     master_interface_ptr = (mb_master_interface_t*) handler;
30 }
31 
32 /**
33  * Modbus controller destroy function
34  */
mbc_master_destroy(void)35 esp_err_t mbc_master_destroy(void)
36 {
37     esp_err_t error = ESP_OK;
38     MB_MASTER_CHECK((master_interface_ptr != NULL),
39                     ESP_ERR_INVALID_STATE,
40                     "Master interface is not correctly initialized.");
41     MB_MASTER_CHECK((master_interface_ptr->destroy != NULL),
42                     ESP_ERR_INVALID_STATE,
43                     "Master interface is not correctly initialized.");
44     error = master_interface_ptr->destroy();
45     MB_MASTER_CHECK((error == ESP_OK),
46                     error,
47                     "Master destroy failure, error=(0x%x).",
48                     error);
49     return error;
50 }
51 
mbc_master_get_cid_info(uint16_t cid,const mb_parameter_descriptor_t ** param_info)52 esp_err_t mbc_master_get_cid_info(uint16_t cid, const mb_parameter_descriptor_t** param_info)
53 {
54     esp_err_t error = ESP_OK;
55     MB_MASTER_CHECK((master_interface_ptr != NULL),
56                     ESP_ERR_INVALID_STATE,
57                     "Master interface is not correctly initialized.");
58     MB_MASTER_CHECK((master_interface_ptr->get_cid_info != NULL),
59                     ESP_ERR_INVALID_STATE,
60                     "Master interface is not correctly initialized.");
61     error = master_interface_ptr->get_cid_info(cid, param_info);
62     MB_MASTER_CHECK((error == ESP_OK),
63                     error,
64                     "Master get cid info failure, error=(0x%x).",
65                     error);
66     return error;
67 }
68 
69 /**
70  * Get parameter data for corresponding characteristic
71  */
mbc_master_get_parameter(uint16_t cid,char * name,uint8_t * value,uint8_t * type)72 esp_err_t mbc_master_get_parameter(uint16_t cid, char* name, uint8_t* value, uint8_t* type)
73 {
74     esp_err_t error = ESP_OK;
75     MB_MASTER_CHECK((master_interface_ptr != NULL),
76                     ESP_ERR_INVALID_STATE,
77                     "Master interface is not correctly initialized.");
78     MB_MASTER_CHECK((master_interface_ptr->get_parameter != NULL),
79                     ESP_ERR_INVALID_STATE,
80                     "Master interface is not correctly initialized.");
81     error = master_interface_ptr->get_parameter(cid, name, value, type);
82     MB_MASTER_CHECK((error == ESP_OK),
83                     error,
84                     "Master get parameter failure, error=(0x%x) (%s).",
85                     error, esp_err_to_name(error));
86     return error;
87 }
88 
89 /**
90  * Send custom Modbus request defined as mb_param_request_t structure
91  */
mbc_master_send_request(mb_param_request_t * request,void * data_ptr)92 esp_err_t mbc_master_send_request(mb_param_request_t* request, void* data_ptr)
93 {
94     esp_err_t error = ESP_OK;
95     MB_MASTER_CHECK((master_interface_ptr != NULL),
96                     ESP_ERR_INVALID_STATE,
97                     "Master interface is not correctly initialized.");
98     MB_MASTER_CHECK((master_interface_ptr->send_request != NULL),
99                     ESP_ERR_INVALID_STATE,
100                     "Master interface is not correctly initialized.");
101     error = master_interface_ptr->send_request(request, data_ptr);
102     MB_MASTER_CHECK((error == ESP_OK),
103                     error,
104                     "Master send request failure error=(0x%x) (%s).",
105                     error, esp_err_to_name(error));
106     return ESP_OK;
107 }
108 
109 /**
110  * Set Modbus parameter description table
111  */
mbc_master_set_descriptor(const mb_parameter_descriptor_t * descriptor,const uint16_t num_elements)112 esp_err_t mbc_master_set_descriptor(const mb_parameter_descriptor_t* descriptor,
113                                         const uint16_t num_elements)
114 {
115     esp_err_t error = ESP_OK;
116     MB_MASTER_CHECK((master_interface_ptr != NULL),
117                     ESP_ERR_INVALID_STATE,
118                     "Master interface is not correctly initialized.");
119     MB_MASTER_CHECK((master_interface_ptr->set_descriptor != NULL),
120                     ESP_ERR_INVALID_STATE,
121                     "Master interface is not correctly initialized.");
122     error = master_interface_ptr->set_descriptor(descriptor, num_elements);
123     MB_MASTER_CHECK((error == ESP_OK),
124                     error,
125                     "Master set descriptor failure, error=(0x%x) (%s).",
126                     error, esp_err_to_name(error));
127     return ESP_OK;
128 }
129 
130 /**
131  * Set parameter value for characteristic selected by name and cid
132  */
mbc_master_set_parameter(uint16_t cid,char * name,uint8_t * value,uint8_t * type)133 esp_err_t mbc_master_set_parameter(uint16_t cid, char* name, uint8_t* value, uint8_t* type)
134 {
135     esp_err_t error = ESP_OK;
136     MB_MASTER_CHECK((master_interface_ptr != NULL),
137                     ESP_ERR_INVALID_STATE,
138                     "Master interface is not correctly initialized.");
139     MB_MASTER_CHECK((master_interface_ptr->set_parameter != NULL),
140                     ESP_ERR_INVALID_STATE,
141                     "Master interface is not correctly initialized.");
142     error = master_interface_ptr->set_parameter(cid, name, value, type);
143     MB_MASTER_CHECK((error == ESP_OK),
144                     error,
145                     "Master set parameter failure, error=(0x%x) (%s).",
146                     error, esp_err_to_name(error));
147     return ESP_OK;
148 }
149 
150 /**
151  * Setup Modbus controller parameters
152  */
mbc_master_setup(void * comm_info)153 esp_err_t mbc_master_setup(void* comm_info)
154 {
155     esp_err_t error = ESP_OK;
156     MB_MASTER_CHECK((master_interface_ptr != NULL),
157                     ESP_ERR_INVALID_STATE,
158                     "Master interface is not correctly initialized.");
159     MB_MASTER_CHECK((master_interface_ptr->setup != NULL),
160                     ESP_ERR_INVALID_STATE,
161                     "Master interface is not correctly initialized.");
162     error = master_interface_ptr->setup(comm_info);
163     MB_MASTER_CHECK((error == ESP_OK),
164                     error,
165                     "Master setup failure, error=(0x%x) (%s).",
166                     error, esp_err_to_name(error));
167     return ESP_OK;
168 }
169 
170 /**
171  * Modbus controller stack start function
172  */
mbc_master_start(void)173 esp_err_t mbc_master_start(void)
174 {
175     esp_err_t error = ESP_OK;
176     MB_MASTER_CHECK((master_interface_ptr != NULL),
177                     ESP_ERR_INVALID_STATE,
178                     "Master interface is not correctly initialized.");
179     MB_MASTER_CHECK((master_interface_ptr->start != NULL),
180                     ESP_ERR_INVALID_STATE,
181                     "Master interface is not correctly initialized.");
182     error = master_interface_ptr->start();
183     MB_MASTER_CHECK((error == ESP_OK),
184                     error,
185                     "Master start failure, error=(0x%x) (%s).",
186                     error, esp_err_to_name(error));
187     return ESP_OK;
188 }
189 
eMBMasterRegDiscreteCB(UCHAR * pucRegBuffer,USHORT usAddress,USHORT usNDiscrete)190 eMBErrorCode eMBMasterRegDiscreteCB(UCHAR * pucRegBuffer, USHORT usAddress,
191                             USHORT usNDiscrete)
192 {
193     eMBErrorCode error = MB_ENOERR;
194     MB_MASTER_CHECK((master_interface_ptr != NULL),
195                     ESP_ERR_INVALID_STATE,
196                     "Master interface is not correctly initialized.");
197     MB_MASTER_CHECK((master_interface_ptr->master_reg_cb_discrete != NULL),
198                     ESP_ERR_INVALID_STATE,
199                     "Master interface is not correctly initialized.");
200     error = master_interface_ptr->master_reg_cb_discrete(pucRegBuffer, usAddress, usNDiscrete);
201     return error;
202 }
203 
eMBMasterRegCoilsCB(UCHAR * pucRegBuffer,USHORT usAddress,USHORT usNCoils,eMBRegisterMode eMode)204 eMBErrorCode eMBMasterRegCoilsCB(UCHAR* pucRegBuffer, USHORT usAddress,
205         USHORT usNCoils, eMBRegisterMode eMode)
206 {
207     eMBErrorCode error = MB_ENOERR;
208     MB_MASTER_CHECK((master_interface_ptr != NULL),
209                     ESP_ERR_INVALID_STATE,
210                     "Master interface is not correctly initialized.");
211     MB_MASTER_CHECK((master_interface_ptr->master_reg_cb_coils != NULL),
212                     ESP_ERR_INVALID_STATE,
213                     "Master interface is not correctly initialized.");
214     error = master_interface_ptr->master_reg_cb_coils(pucRegBuffer, usAddress,
215                                                         usNCoils, eMode);
216     return error;
217 }
218 
eMBMasterRegHoldingCB(UCHAR * pucRegBuffer,USHORT usAddress,USHORT usNRegs,eMBRegisterMode eMode)219 eMBErrorCode eMBMasterRegHoldingCB(UCHAR * pucRegBuffer, USHORT usAddress,
220         USHORT usNRegs, eMBRegisterMode eMode)
221 {
222     eMBErrorCode error = MB_ENOERR;
223     MB_MASTER_CHECK((master_interface_ptr != NULL),
224                     ESP_ERR_INVALID_STATE,
225                     "Master interface is not correctly initialized.");
226     MB_MASTER_CHECK((master_interface_ptr->master_reg_cb_holding != NULL),
227                     ESP_ERR_INVALID_STATE,
228                     "Master interface is not correctly initialized.");
229     error = master_interface_ptr->master_reg_cb_holding(pucRegBuffer, usAddress,
230                                                         usNRegs, eMode);
231     return error;
232 }
233 
eMBMasterRegInputCB(UCHAR * pucRegBuffer,USHORT usAddress,USHORT usNRegs)234 eMBErrorCode eMBMasterRegInputCB(UCHAR * pucRegBuffer, USHORT usAddress,
235                                 USHORT usNRegs)
236 {
237     eMBErrorCode error = MB_ENOERR;
238     MB_MASTER_CHECK((master_interface_ptr != NULL),
239                     ESP_ERR_INVALID_STATE,
240                     "Master interface is not correctly initialized.");
241     MB_MASTER_CHECK((master_interface_ptr->master_reg_cb_input != NULL),
242                     ESP_ERR_INVALID_STATE,
243                     "Master interface is not correctly initialized.");
244     error = master_interface_ptr->master_reg_cb_input(pucRegBuffer, usAddress, usNRegs);
245     return error;
246 }
247