-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodbus_cache.cpp
688 lines (641 loc) · 18.4 KB
/
modbus_cache.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/*
* Fledge south service plugin
*
* Copyright (c) 2019 OSIsoft, LLC
*
* Released under the Apache 2.0 Licence
*
* Author: Mark Riddoch
*/
#include <modbus_south.h>
#include <logger.h>
using namespace std;
ModbusCacheManager *ModbusCacheManager::instance = 0;
/**
* Constructor for the Modbus Cache Manager. A singleton class that
* manages the cache creation, population and use of the modbus cache
*/
ModbusCacheManager::ModbusCacheManager()
{
}
/**
* Destructor for the Modbus Cache Manager. We only destroy the cache
* manager when the modbus map is changed.
*/
ModbusCacheManager::~ModbusCacheManager()
{
m_slaveCaches.clear();
ModbusCacheManager::instance = 0;
}
/**
* Return the signleton Modbus Cache Manager instance
*/
ModbusCacheManager *ModbusCacheManager::getModbusCacheManager()
{
if (ModbusCacheManager::instance)
return ModbusCacheManager::instance;
else
ModbusCacheManager::instance = new ModbusCacheManager();
return ModbusCacheManager::instance;
}
/**
* Register a modbus register with the cache manager. This is called during the
* processing of the Modbus map and is used to register the ranges in the modbus
* register that are used.
*
* @param slave The modbus slave
* @param source The source of the data, coil, input bits, registers or input registers
*/
void ModbusCacheManager::registerItem(int slave, ModbusSource source, int registerNo)
{
if (m_slaveCaches.find(slave) != m_slaveCaches.end())
{
m_slaveCaches[slave]->addRegister(source, registerNo);
}
else
{
m_slaveCaches.insert(pair<int, SlaveCache *>(slave, new SlaveCache(source, registerNo)));
}
}
/**
* Called once the new modbus map has been processed to create the actual caches themsevles.
*/
void ModbusCacheManager::createCaches()
{
for (map<int, SlaveCache *>::iterator it = m_slaveCaches.begin(); it != m_slaveCaches.end(); it++)
{
it->second->createCaches(it->first);
}
}
/**
* Call by the lower layers of the cache structure to add a cache range to the
* caching structure.
*
* @param slave The modbus slave ID for the cache
* @param source The source of modbus data; coils, input bits, registers or input registers
* @param first The first register in the cache
* @param last The last register in the cache
*/
void ModbusCacheManager::addCache(int slave, ModbusSource source, int first, int last)
{
if (m_slaveCaches.find(slave) == m_slaveCaches.end())
{
Logger::getLogger()->fatal("Unable to find cache for slave %d", slave);
throw runtime_error("Missing cache for slave");
}
m_slaveCaches[slave]->addCache(source, first, last);
}
/**
* Populate the values in the caches
*
* @param modbus The modbus interface
*/
void ModbusCacheManager::populateCaches(modbus_t *modbus)
{
for (map<int, SlaveCache *>::iterator it = m_slaveCaches.begin(); it != m_slaveCaches.end(); it++)
{
it->second->populateCaches(modbus, it->first);
}
}
/**
* Determine if there is a cached value for a given modbus register
*
* @param slave The modbus slave ID
* @param source The data source. Coils, input bits, registers or input registers
* @param registerNo The register number
*/
bool ModbusCacheManager::isCached(int slave, ModbusSource source, int registerNo)
{
if (m_slaveCaches.find(slave) == m_slaveCaches.end())
return false;
return m_slaveCaches[slave]->isCached(source, registerNo);
}
/**
* Return a value out of the cache
*
* @param slave The modbus slave
* @param source The modbus source; coil, input bits, register or input register
* @param registerNo The register no
*/
uint16_t ModbusCacheManager::cachedValue(int slave, ModbusSource source, int registerNo)
{
if (m_slaveCaches.find(slave) == m_slaveCaches.end())
{
throw runtime_error("Value is not cached");
}
return m_slaveCaches[slave]->cachedValue(source, registerNo);
}
/**
* Constructor for a cache related to a particular slave
*
* @param slave The modbus slave
* @param registerNo The register number that triggered the creation of this slave.
*/
ModbusCacheManager::SlaveCache::SlaveCache(ModbusSource source, int registerNo)
{
m_ranges.insert(pair<ModbusSource, RegisterRanges *>(source, new RegisterRanges(registerNo)));
}
/**
* Destructor for a slave cache. destroys the ranges for this slave.
*/
ModbusCacheManager::SlaveCache::~SlaveCache()
{
m_ranges.clear();
}
/**
* Add a source and register to the slave cache.
*
* @param source The modbus data source. Coils, input bits, registers or input registers
* @param registerNo The register number in the modbus map
*/
void ModbusCacheManager::SlaveCache::addRegister(ModbusSource source, int registerNo)
{
if (m_ranges.find(source) != m_ranges.end())
{
m_ranges[source]->addRegister(registerNo);
}
else
{
m_ranges.insert(pair<ModbusSource, RegisterRanges *>(source, new RegisterRanges(registerNo)));
}
}
/**
* Trigger the creation of the caches for a particular modbus slave
*
* @param slave The modbus slave
*/
void ModbusCacheManager::SlaveCache::createCaches(int slave)
{
for (map<ModbusSource, RegisterRanges *>::iterator it = m_ranges.begin(); it != m_ranges.end(); it++)
{
it->second->createCaches(slave, it->first);
}
}
void ModbusCacheManager::SlaveCache::addCache(ModbusSource source, int first, int last)
{
map<ModbusSource, RegisterRanges *>::iterator it = m_ranges.find(source);
if (it != m_ranges.end())
{
it->second->addCache(source, first, last);
}
}
/**
* Populate the caches for a slave
*
* @param modbus The modbus interface
* @param slave The modbus slave ID
*/
void ModbusCacheManager::SlaveCache::populateCaches(modbus_t *modbus, int slave)
{
for (map<ModbusSource, RegisterRanges *>::iterator it = m_ranges.begin(); it != m_ranges.end(); it++)
{
it->second->populateCaches(modbus, slave);
}
}
/**
* Determine if a regsiter is in the cache
*
* @param source The source of modbus data; Coils, Input Bits, Registers and Input Registers
* @param registerNo The modbus register number
*/
bool ModbusCacheManager::SlaveCache::isCached(ModbusSource source, int registerNo)
{
if (m_ranges.find(source) != m_ranges.end())
{
return false;
}
else
{
return m_ranges[source]->isCached(registerNo);
}
}
/**
* Return the cached value of a register on a sopecific slave
*
* @param source The source of modbus data; Coils, Input Bits, Registers and Input Registers
* @param registerNo The register number to return the cached value for
*/
uint16_t ModbusCacheManager::SlaveCache::cachedValue(ModbusSource source, int registerNo)
{
if (m_ranges.find(source) != m_ranges.end())
{
throw runtime_error("Cached value for source is missing");
}
return m_ranges[source]->cachedValue(registerNo);
}
/**
* Create a range of registers for a cache definition
*
* @param registerNo The register number of the register that triggered this creation
*/
ModbusCacheManager::SlaveCache::RegisterRanges::RegisterRanges(int registerNo)
{
m_ranges.insert(pair<int, int>(registerNo, registerNo));
}
/**
* Destructor for register range
*/
ModbusCacheManager::SlaveCache::RegisterRanges::~RegisterRanges()
{
m_ranges.clear();
}
/**
* Add a register to the range of registers.
*
* @param registerNo The regiater number to extend the cache with
*/
void ModbusCacheManager::SlaveCache::RegisterRanges::addRegister(int registerNo)
{
bool done = false;
Logger *log = Logger::getLogger();
log->info("Add register %d", registerNo);
// First deal with extending the start of a range
map<int,int>::iterator it = m_ranges.find(registerNo + 1);
if (it != m_ranges.end())
{
int last = it->second;
m_ranges.erase(it);
done = true;
m_ranges.insert(pair<int, int>(registerNo, last));
log->info("Add to start of range %d -> %d", registerNo, last);
}
else
{
log->info("Looking for range x -> %d", registerNo - 1);
// Deal with the easy case of extending the end of a range
for (map<int,int>::iterator it = m_ranges.begin(); it != m_ranges.end(); it++)
{
if (it->second == registerNo - 1)
{
log->info("Add to end of range %d -> %d", it->first, it->second);
it->second = registerNo;
done = true;
break;
}
else if (registerNo >= it->first && registerNo <= it->second)
{
log->info("%d already in cache %d -> %d", registerNo, it->first, it->second);
done = true;
break;
}
}
}
if (done)
{
// We have extended one range, so we need to check to see if we need to coalese two ranges
bool combined = false;
for (map<int,int>::iterator it1 = m_ranges.begin(); it1 != m_ranges.end() && combined == false; it1++)
{
for (map<int,int>::iterator it2 = m_ranges.begin(); it2 != m_ranges.end(); it2++)
{
if (it2->first == it1->first && it2->second == it1->second)
{
continue; // it1 and it2 point to same range
}
if (it1->second + 1 == it2->first)
{
log->info("Combined range %d -> %d and %d -> %d", it1->first, it1->second, it2->first, it2->second);
it1->second = it2->second;
m_ranges.erase(it2);
combined = true;
break;
}
}
}
}
else
{
// Simply add a new range
m_ranges.insert(pair<int, int>(registerNo, registerNo));
log->info("Insert new range %d -> %d", registerNo, registerNo);
}
}
/**
* Trigger the creation of the caches. We create a cache for every run of contiguous
* registers above a certain threshold.
*
* @param slave The slave ID we are dealign with
* @param source The source of the data (coils, input bits, registers or input registers
*/
void ModbusCacheManager::SlaveCache::RegisterRanges::createCaches(int slave, ModbusSource source)
{
ModbusCacheManager *manager = ModbusCacheManager::getModbusCacheManager();
for (map<int,int>::iterator it = m_ranges.begin(); it != m_ranges.end(); it++)
{
if (it->second - it->first >= CACHE_THRESHOLD)
{
Logger::getLogger()->info("Create cache for slave %d, %s, %d to %d",
slave, sourceToString(source), it->first, it->second);
manager->addCache(slave, source, it->first, it->second);
}
else
{
Logger::getLogger()->info("Too small to cache for slave %d, %s, %d to %d",
slave, sourceToString(source), it->first, it->second);
}
}
}
/**
* Add a cache entry for a set of registers
*
* @param source The modbus source; coils, input bits, registers, input registers
* @param first First register in the cache
* @param last Last register in the cache
*/
void ModbusCacheManager::SlaveCache::RegisterRanges::addCache(ModbusSource source, int first, int last)
{
if (m_ranges.find(first) == m_ranges.end())
{
Logger::getLogger()->fatal("Unable to find range to cache %d %d", first, last);
for (map<int,int>::iterator it = m_ranges.begin(); it != m_ranges.end(); it++)
{
Logger::getLogger()->info("Range %d to %d", first, last);
}
throw runtime_error("Cache range does not exist");
}
Cache *cache;
switch (source)
{
case MODBUS_COIL:
cache = new CoilCache(first, last);
break;
case MODBUS_INPUT:
cache = new InputBitsCache(first, last);
break;
case MODBUS_REGISTER:
cache = new RegisterCache(first, last);
break;
case MODBUS_INPUT_REGISTER:
cache = new InputRegisterCache(first, last);
break;
default:
Logger::getLogger()->fatal("Invalid modbus source for cache");
throw runtime_error("Invalid modbus source for cache creation");
}
m_caches.insert(pair<int, Cache *>(first, cache));
}
/**
* Populate the caches for a single slave and modbus source
*
* @param modbus The modbus interface
* @param slave The modbus slave
*/
void ModbusCacheManager::SlaveCache::RegisterRanges::populateCaches(modbus_t *modbus, int slave)
{
for (map<int, Cache *>::iterator it = m_caches.begin(); it != m_caches.end(); it++)
{
it->second->populateCache(modbus, slave);
}
}
/**
* Check if a register is cached
*
* @param registerNo The register to check
*/
bool ModbusCacheManager::SlaveCache::RegisterRanges::isCached(int registerNo)
{
for (map<int,int>::iterator it = m_ranges.begin(); it != m_ranges.end(); it++)
{
if (it->first <= registerNo && it->second >= registerNo)
{
map<int, Cache *>::iterator entry = m_caches.find(it->first);
if (entry != m_caches.end())
{
return entry->second->isValid();
}
}
}
return false;
}
/**
* Return the cached value for a register
*
* @param registerNo The register to return
*/
uint16_t ModbusCacheManager::SlaveCache::RegisterRanges::cachedValue(int registerNo)
{
for (map<int,int>::iterator it = m_ranges.begin(); it != m_ranges.end(); it++)
{
if (it->first <= registerNo && it->second >= registerNo)
{
map<int, Cache *>::iterator entry = m_caches.find(it->first);
if (entry != m_caches.end())
{
return entry->second->cachedValue(registerNo);
}
}
}
throw runtime_error("Value is not cached");
}
/**
* Create a cache to cache coil values
*
* @param first The first coil to cache
* @param last The last coil to cache
*/
ModbusCacheManager::SlaveCache::RegisterRanges::CoilCache::CoilCache(int first, int last) : Cache(first, last)
{
m_data = new uint8_t[1 + last - first];
}
/**
* Populate the coil cache
*
* @param modbus The modbus interface to use
* @param slave The modbus slave to connect to
*/
void ModbusCacheManager::SlaveCache::RegisterRanges::CoilCache::populateCache(modbus_t *modbus, int slave)
{
int rc;
modbus_set_slave(modbus, slave);
m_valid = false;
errno = 0;
int start = m_first;
uint8_t *ptr = m_data;
while (start < m_last)
{
int count = m_last - start + 1;
if (count > MAX_MODBUS_BLOCK)
count = MAX_MODBUS_BLOCK;
if ((rc = modbus_read_bits(modbus, start, count, ptr)) == -1)
{
Logger::getLogger()->error("Modbus read coil cache %d, %d, %s", start, count, modbus_strerror(errno));
return;
}
else if (rc != count)
{
Logger::getLogger()->error("Modbus read coil cache %d, %d: short read %d", start, count, rc);
return;
}
start += count;
ptr += count;
}
m_valid = true;
}
/**
* Return the cached value of a coil
*
* @param registerNo The register number to return the value for
*/
uint16_t ModbusCacheManager::SlaveCache::RegisterRanges::CoilCache::cachedValue(int registerNo)
{
return (uint16_t)(m_data[registerNo - m_first]);
}
/**
* Create the cache for Input Bits
*
* @param first The first register in the cache
* @param last The last register in the cache
*/
ModbusCacheManager::SlaveCache::RegisterRanges::InputBitsCache::InputBitsCache(int first, int last) : Cache(first, last)
{
m_data = new uint8_t[1 + last - first];
}
/**
* Populate the Input Bits cache
*
* @param modbus The modbus interface
* @param slave The modbus slave
*/
void ModbusCacheManager::SlaveCache::RegisterRanges::InputBitsCache::populateCache(modbus_t *modbus, int slave)
{
int rc;
modbus_set_slave(modbus, slave);
m_valid = false;
errno = 0;
int start = m_first;
uint8_t *ptr = m_data;
while (start < m_last)
{
int count = m_last - start + 1;
if (count > MAX_MODBUS_BLOCK)
count = MAX_MODBUS_BLOCK;
if ((rc = modbus_read_input_bits(modbus, start, count, ptr)) == -1)
{
Logger::getLogger()->error("Modbus read input bits cache %d, %d, %s", start, count, modbus_strerror(errno));
return;
}
else if (rc != count)
{
Logger::getLogger()->error("Modbus read input bits cache %d, %d: short read %d", start, count, rc);
return;
}
start += count;
ptr += count;
}
m_valid = true;
}
/**
* Return the cached value of Input Bits register
*
* @param registerNo The register number to return
*/
uint16_t ModbusCacheManager::SlaveCache::RegisterRanges::InputBitsCache::cachedValue(int registerNo)
{
return (uint16_t)(m_data[registerNo - m_first]);
}
/**
* Create a modbus register cache
*
* @param first The first register in the cache
* @param last The last register in the cache
*/
ModbusCacheManager::SlaveCache::RegisterRanges::RegisterCache::RegisterCache(int first, int last) : Cache(first, last)
{
m_data = new uint16_t[1 + last - first];
}
/**
* Populate the cache of the modbus registers
*
* @param modbus The modbus interface
* @param slave The modbus slave
*/
void ModbusCacheManager::SlaveCache::RegisterRanges::RegisterCache::populateCache(modbus_t *modbus, int slave)
{
int rc;
modbus_set_slave(modbus, slave);
m_valid = false;
errno = 0;
int start = m_first;
uint16_t *ptr = m_data;
while (start < m_last)
{
int count = m_last - start + 1;
if (count > MAX_MODBUS_BLOCK)
count = MAX_MODBUS_BLOCK;
if ((rc = modbus_read_registers(modbus, start, count, ptr)) == -1)
{
Logger::getLogger()->error("Modbus read registers cache %d, %d, %s", start, count, modbus_strerror(errno));
return;
}
else if (rc != count)
{
Logger::getLogger()->error("Modbus read registers cache %d, %d: short read %d", start, count, rc);
return;
}
start += count;
ptr += count;
}
m_valid = true;
}
/**
* Return the cached value of a register
*
* @param registerNo The number of the register to return the cache content of
*/
uint16_t ModbusCacheManager::SlaveCache::RegisterRanges::RegisterCache::cachedValue(int registerNo)
{
return m_data[registerNo - m_first];
}
/**
* Create an Input Register cache
*
* @param first The first register in the cache
* @param last The last register in the cache
*/
ModbusCacheManager::SlaveCache::RegisterRanges::InputRegisterCache::InputRegisterCache(int first, int last) : Cache(first, last)
{
m_data = new uint16_t[1 + last - first];
}
/**
* Populate an input register cache
*
* @param modbus The modbus interface
* @param slave The modbus slave
*/
void ModbusCacheManager::SlaveCache::RegisterRanges::InputRegisterCache::populateCache(modbus_t *modbus, int slave)
{
int rc;
modbus_set_slave(modbus, slave);
m_valid = false;
errno = 0;
int start = m_first;
uint16_t *ptr = m_data;
while (start < m_last)
{
int count = m_last - start + 1;
if (count > MAX_MODBUS_BLOCK)
count = MAX_MODBUS_BLOCK;
if ((rc = modbus_read_input_registers(modbus, start, count, ptr)) == -1)
{
Logger::getLogger()->error("Modbus read input registers cache %d, %d, %s", start, count, modbus_strerror(errno));
return;
}
else if (rc != count)
{
Logger::getLogger()->error("Modbus read input registers cache %d, %d: short read %d", start, count, rc);
return;
}
start += count;
ptr += count;
}
m_valid = true;
}
/**
* Return the cached value of an input register
*
* @param registerNo The register number whose cached value should be returned
*/
uint16_t ModbusCacheManager::SlaveCache::RegisterRanges::InputRegisterCache::cachedValue(int registerNo)
{
return m_data[registerNo - m_first];
}