198 } |
198 } |
199 return 0; |
199 return 0; |
200 } |
200 } |
201 |
201 |
202 |
202 |
203 static int __read_inbits (void *mem_map, u16 start_addr, u16 bit_count, u8 *data_bytes) |
203 static int __read_inbits (void *mem_map, u16 start_addr, u16 bit_count, u8 *data_bytes) { |
204 {return __pack_bits(((server_mem_t *)mem_map)->ro_bits, start_addr, bit_count, data_bytes);} |
204 int res = __pack_bits(((server_mem_t *)mem_map)->ro_bits, start_addr, bit_count, data_bytes); |
205 static int __read_outbits (void *mem_map, u16 start_addr, u16 bit_count, u8 *data_bytes) |
205 |
206 {return __pack_bits(((server_mem_t *)mem_map)->rw_bits, start_addr, bit_count, data_bytes);} |
206 if (res >= 0) { |
207 static int __write_outbits (void *mem_map, u16 start_addr, u16 bit_count, u8 *data_bytes) |
207 /* update the flag and counter of Modbus requests we have processed. */ |
208 {return __unpack_bits(((server_mem_t *)mem_map)->rw_bits, start_addr, bit_count, data_bytes); } |
208 ((server_mem_t *)mem_map)->flag_read_req_counter++; |
|
209 ((server_mem_t *)mem_map)->flag_read_req_flag = 1; |
|
210 } |
|
211 |
|
212 return res; |
|
213 } |
|
214 |
|
215 static int __read_outbits (void *mem_map, u16 start_addr, u16 bit_count, u8 *data_bytes) { |
|
216 int res = __pack_bits(((server_mem_t *)mem_map)->rw_bits, start_addr, bit_count, data_bytes); |
|
217 |
|
218 if (res >= 0) { |
|
219 /* update the flag and counter of Modbus requests we have processed. */ |
|
220 ((server_mem_t *)mem_map)->flag_read_req_counter++; |
|
221 ((server_mem_t *)mem_map)->flag_read_req_flag = 1; |
|
222 } |
|
223 |
|
224 return res; |
|
225 } |
|
226 |
|
227 static int __write_outbits (void *mem_map, u16 start_addr, u16 bit_count, u8 *data_bytes) { |
|
228 int res = __unpack_bits(((server_mem_t *)mem_map)->rw_bits, start_addr, bit_count, data_bytes); |
|
229 |
|
230 if (res >= 0) { |
|
231 /* update the flag and counter of Modbus requests we have processed. */ |
|
232 ((server_mem_t *)mem_map)->flag_write_req_counter++; |
|
233 ((server_mem_t *)mem_map)->flag_write_req_flag = 1; |
|
234 } |
|
235 |
|
236 return res; |
|
237 } |
|
238 |
209 |
239 |
210 |
240 |
211 |
241 |
212 static int __read_inwords (void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) { |
242 static int __read_inwords (void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) { |
213 |
243 |
214 if ((start_addr + word_count) > MEM_AREA_SIZE) |
244 if ((start_addr + word_count) > MEM_AREA_SIZE) |
215 return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */ |
245 return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */ |
|
246 |
|
247 /* update the flag and counter of Modbus requests we have processed. */ |
|
248 ((server_mem_t *)mem_map)->flag_read_req_counter++; |
|
249 ((server_mem_t *)mem_map)->flag_read_req_flag = 1; |
216 |
250 |
217 /* use memcpy() because loop with pointers (u16 *) caused alignment problems */ |
251 /* use memcpy() because loop with pointers (u16 *) caused alignment problems */ |
218 memcpy(/* dest */ (void *)data_words, |
252 memcpy(/* dest */ (void *)data_words, |
219 /* src */ (void *)&(((server_mem_t *)mem_map)->ro_words[start_addr]), |
253 /* src */ (void *)&(((server_mem_t *)mem_map)->ro_words[start_addr]), |
220 /* size */ word_count * 2); |
254 /* size */ word_count * 2); |
226 static int __read_outwords (void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) { |
260 static int __read_outwords (void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) { |
227 |
261 |
228 if ((start_addr + word_count) > MEM_AREA_SIZE) |
262 if ((start_addr + word_count) > MEM_AREA_SIZE) |
229 return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */ |
263 return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */ |
230 |
264 |
|
265 /* update the flag and counter of Modbus requests we have processed. */ |
|
266 ((server_mem_t *)mem_map)->flag_read_req_counter++; |
|
267 ((server_mem_t *)mem_map)->flag_read_req_flag = 1; |
|
268 |
231 /* use memcpy() because loop with pointers (u16 *) caused alignment problems */ |
269 /* use memcpy() because loop with pointers (u16 *) caused alignment problems */ |
232 memcpy(/* dest */ (void *)data_words, |
270 memcpy(/* dest */ (void *)data_words, |
233 /* src */ (void *)&(((server_mem_t *)mem_map)->rw_words[start_addr]), |
271 /* src */ (void *)&(((server_mem_t *)mem_map)->rw_words[start_addr]), |
234 /* size */ word_count * 2); |
272 /* size */ word_count * 2); |
235 return 0; |
273 return 0; |
240 |
278 |
241 static int __write_outwords(void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) { |
279 static int __write_outwords(void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) { |
242 |
280 |
243 if ((start_addr + word_count) > MEM_AREA_SIZE) |
281 if ((start_addr + word_count) > MEM_AREA_SIZE) |
244 return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */ |
282 return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */ |
|
283 |
|
284 /* update the flag and counter of Modbus requests we have processed. */ |
|
285 ((server_mem_t *)mem_map)->flag_write_req_counter++; |
|
286 ((server_mem_t *)mem_map)->flag_write_req_flag = 1; |
245 |
287 |
246 /* WARNING: The data returned in the data_words[] array is not guaranteed to be 16 bit aligned. |
288 /* WARNING: The data returned in the data_words[] array is not guaranteed to be 16 bit aligned. |
247 * It is not therefore safe to cast it to an u16 data type. |
289 * It is not therefore safe to cast it to an u16 data type. |
248 * The following code cannot be used. memcpy() is used instead. |
290 * The following code cannot be used. memcpy() is used instead. |
249 */ |
291 */ |
543 // mb_nd with negative numbers indicate how far it has been initialised (or not) |
585 // mb_nd with negative numbers indicate how far it has been initialised (or not) |
544 // -2 --> no modbus node created; no thread created |
586 // -2 --> no modbus node created; no thread created |
545 // -1 --> modbus node created!; no thread created |
587 // -1 --> modbus node created!; no thread created |
546 // >=0 --> modbus node created!; thread created! |
588 // >=0 --> modbus node created!; thread created! |
547 server_nodes[index].mb_nd = -2; |
589 server_nodes[index].mb_nd = -2; |
|
590 server_nodes[index].mem_area.flag_write_req_flag = 0; |
|
591 server_nodes[index].mem_area.flag_write_req_counter = 0; |
|
592 server_nodes[index].mem_area.flag_read_req_counter = 0; |
|
593 server_nodes[index].mem_area.flag_read_req_flag = 0; |
548 /* see comment in mb_runtime.h to understad why we need to initialize these entries */ |
594 /* see comment in mb_runtime.h to understad why we need to initialize these entries */ |
549 switch (server_nodes[index].node_address.naf) { |
595 switch (server_nodes[index].node_address.naf) { |
550 case naf_tcp: |
596 case naf_tcp: |
551 server_nodes[index].node_address.addr.tcp.host = server_nodes[index].str1; |
597 server_nodes[index].node_address.addr.tcp.host = server_nodes[index].str1; |
552 server_nodes[index].node_address.addr.tcp.service = server_nodes[index].str2; |
598 server_nodes[index].node_address.addr.tcp.service = server_nodes[index].str2; |