196 } |
196 } |
197 return 0; |
197 return 0; |
198 } |
198 } |
199 |
199 |
200 |
200 |
201 static int __read_inbits (void *mem_map, u16 start_addr, u16 bit_count, u8 *data_bytes) |
201 static int __read_inbits (void *mem_map, u16 start_addr, u16 bit_count, u8 *data_bytes) { |
202 {return __pack_bits(((server_mem_t *)mem_map)->ro_bits, start_addr, bit_count, data_bytes);} |
202 int res = __pack_bits(((server_mem_t *)mem_map)->ro_bits, start_addr, bit_count, data_bytes); |
203 static int __read_outbits (void *mem_map, u16 start_addr, u16 bit_count, u8 *data_bytes) |
203 |
204 {return __pack_bits(((server_mem_t *)mem_map)->rw_bits, start_addr, bit_count, data_bytes);} |
204 if (res >= 0) |
205 static int __write_outbits (void *mem_map, u16 start_addr, u16 bit_count, u8 *data_bytes) |
205 /* update the counter of Modbus requests we have processed. */ |
206 {return __unpack_bits(((server_mem_t *)mem_map)->rw_bits, start_addr, bit_count, data_bytes); } |
206 ((server_mem_t *)mem_map)->flag_read_req_counter++; |
|
207 |
|
208 return res; |
|
209 } |
|
210 |
|
211 static int __read_outbits (void *mem_map, u16 start_addr, u16 bit_count, u8 *data_bytes) { |
|
212 int res = __pack_bits(((server_mem_t *)mem_map)->rw_bits, start_addr, bit_count, data_bytes); |
|
213 |
|
214 if (res >= 0) |
|
215 /* update the counter of Modbus requests we have processed. */ |
|
216 ((server_mem_t *)mem_map)->flag_read_req_counter++; |
|
217 |
|
218 return res; |
|
219 } |
|
220 |
|
221 static int __write_outbits (void *mem_map, u16 start_addr, u16 bit_count, u8 *data_bytes) { |
|
222 int res = __unpack_bits(((server_mem_t *)mem_map)->rw_bits, start_addr, bit_count, data_bytes); |
|
223 |
|
224 if (res >= 0) |
|
225 /* update the counter of Modbus requests we have processed. */ |
|
226 ((server_mem_t *)mem_map)->flag_write_req_counter++; |
|
227 |
|
228 return res; |
|
229 } |
|
230 |
207 |
231 |
208 |
232 |
209 |
233 |
210 static int __read_inwords (void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) { |
234 static int __read_inwords (void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) { |
211 |
235 |
212 if ((start_addr + word_count) > MEM_AREA_SIZE) |
236 if ((start_addr + word_count) > MEM_AREA_SIZE) |
213 return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */ |
237 return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */ |
|
238 |
|
239 /* update the counter of Modbus requests we have processed. */ |
|
240 ((server_mem_t *)mem_map)->flag_read_req_counter++; |
214 |
241 |
215 /* use memcpy() because loop with pointers (u16 *) caused alignment problems */ |
242 /* use memcpy() because loop with pointers (u16 *) caused alignment problems */ |
216 memcpy(/* dest */ (void *)data_words, |
243 memcpy(/* dest */ (void *)data_words, |
217 /* src */ (void *)&(((server_mem_t *)mem_map)->ro_words[start_addr]), |
244 /* src */ (void *)&(((server_mem_t *)mem_map)->ro_words[start_addr]), |
218 /* size */ word_count * 2); |
245 /* size */ word_count * 2); |
224 static int __read_outwords (void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) { |
251 static int __read_outwords (void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) { |
225 |
252 |
226 if ((start_addr + word_count) > MEM_AREA_SIZE) |
253 if ((start_addr + word_count) > MEM_AREA_SIZE) |
227 return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */ |
254 return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */ |
228 |
255 |
|
256 /* update the counter of Modbus requests we have processed. */ |
|
257 ((server_mem_t *)mem_map)->flag_read_req_counter++; |
|
258 |
229 /* use memcpy() because loop with pointers (u16 *) caused alignment problems */ |
259 /* use memcpy() because loop with pointers (u16 *) caused alignment problems */ |
230 memcpy(/* dest */ (void *)data_words, |
260 memcpy(/* dest */ (void *)data_words, |
231 /* src */ (void *)&(((server_mem_t *)mem_map)->rw_words[start_addr]), |
261 /* src */ (void *)&(((server_mem_t *)mem_map)->rw_words[start_addr]), |
232 /* size */ word_count * 2); |
262 /* size */ word_count * 2); |
233 return 0; |
263 return 0; |
238 |
268 |
239 static int __write_outwords(void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) { |
269 static int __write_outwords(void *mem_map, u16 start_addr, u16 word_count, u16 *data_words) { |
240 |
270 |
241 if ((start_addr + word_count) > MEM_AREA_SIZE) |
271 if ((start_addr + word_count) > MEM_AREA_SIZE) |
242 return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */ |
272 return -ERR_ILLEGAL_DATA_ADDRESS; /* ERR_ILLEGAL_DATA_ADDRESS defined in mb_util.h */ |
|
273 |
|
274 /* update the counter of Modbus requests we have processed. */ |
|
275 ((server_mem_t *)mem_map)->flag_write_req_counter++; |
243 |
276 |
244 /* WARNING: The data returned in the data_words[] array is not guaranteed to be 16 bit aligned. |
277 /* WARNING: The data returned in the data_words[] array is not guaranteed to be 16 bit aligned. |
245 * It is not therefore safe to cast it to an u16 data type. |
278 * It is not therefore safe to cast it to an u16 data type. |
246 * The following code cannot be used. memcpy() is used instead. |
279 * The following code cannot be used. memcpy() is used instead. |
247 */ |
280 */ |
523 // mb_nd with negative numbers indicate how far it has been initialised (or not) |
556 // mb_nd with negative numbers indicate how far it has been initialised (or not) |
524 // -2 --> no modbus node created; no thread created |
557 // -2 --> no modbus node created; no thread created |
525 // -1 --> modbus node created!; no thread created |
558 // -1 --> modbus node created!; no thread created |
526 // >=0 --> modbus node created!; thread created! |
559 // >=0 --> modbus node created!; thread created! |
527 server_nodes[index].mb_nd = -2; |
560 server_nodes[index].mb_nd = -2; |
|
561 server_nodes[index].mem_area.flag_write_req_counter = 0; |
|
562 server_nodes[index].mem_area.flag_read_req_counter = 0; |
528 /* see comment in mb_runtime.h to understad why we need to initialize these entries */ |
563 /* see comment in mb_runtime.h to understad why we need to initialize these entries */ |
529 switch (server_nodes[index].node_address.naf) { |
564 switch (server_nodes[index].node_address.naf) { |
530 case naf_tcp: |
565 case naf_tcp: |
531 server_nodes[index].node_address.addr.tcp.host = server_nodes[index].str1; |
566 server_nodes[index].node_address.addr.tcp.host = server_nodes[index].str1; |
532 server_nodes[index].node_address.addr.tcp.service = server_nodes[index].str2; |
567 server_nodes[index].node_address.addr.tcp.service = server_nodes[index].str2; |