summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/bacnetmstp/bacnetutil.cxx
blob: 17d555a44d814abf72dcb01f1c3e0939923e8c3f (plain)
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
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
/*
 * Author: Jon Trulson <jtrulson@ics.com>
 * Copyright (c) 2016 Intel Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <iostream>
#include <stdexcept>
#include <string>

#include "bacnetutil.hpp"

using namespace upm;
using namespace std;


BACNETUTIL::BACNETUTIL(uint32_t targetDeviceObjectID) :
  m_instance(0)
{
  // Save our device's ID
  m_targetDeviceObjectID = targetDeviceObjectID;

  // create the BACNETMSTP instance here if it does not already exist,
  // and store the pointer in our class to save on some typing.

  m_instance = BACNETMSTP::instance();

  // now see if it has been initialized yet for init()
  m_initialized = m_instance->isInitialized();

  setDebug(false);

  // we disable this by default for performance reasons
  checkReliability(false);

  // empty our unit caches
  m_avUnitCache.clear();
  m_aiUnitCache.clear();

  // empty our msv info store
  m_msvInfo.clear();

  // empty our binary info stores
  m_bvInfo.clear();
  m_biInfo.clear();
}

BACNETUTIL::~BACNETUTIL()
{
}

void BACNETUTIL::initMaster(std::string port, int baudRate,
                            int deviceInstanceID, int macAddr, int maxMaster,
                            int maxInfoFrames)
{
  // first we check to see if the bacnetmstp instance has already been
  // initialized (determined in the ctor).  If not, we will do so here
  // with the arguments specified.  If it has already been
  // initialized, then we do not bother calling bacnetmstp's init
  // again as it will just be ignored.

  if (!m_initialized)
    m_instance->initMaster(port, baudRate, deviceInstanceID,
                           macAddr, maxMaster, maxInfoFrames);

  // either it threw an exception, was already initialized or it's
  // initialized now...
  m_initialized = true;
}

void BACNETUTIL::setDebug(bool enable)
{
  m_debugging = enable;

  // we also enable/disable debugging in BACNETMSTP
  m_instance->setDebug(enable);
}

float BACNETUTIL::getAnalogValue(uint32_t objInstance)
{
  // check reliability first, if enabled
  if (m_checkReliability)
    {
      if (m_instance->readProperty(m_targetDeviceObjectID, OBJECT_ANALOG_VALUE,
                                   objInstance, PROP_RELIABILITY))
        {
          if (m_debugging)
            cerr << __FUNCTION__ << ": (reliability): " << getAllErrorString()
                 << endl;

          throw std::runtime_error(std::string(__FUNCTION__)
                                   + getAllErrorString());
        }

      BACNET_RELIABILITY reliable =
        static_cast<BACNET_RELIABILITY>(m_instance->getDataTypeEnum());

      if (reliable != RELIABILITY_NO_FAULT_DETECTED)
        {
          if (m_debugging)
            cerr << __FUNCTION__ << ": Reliability check failed" << endl;

          throw std::runtime_error(std::string(__FUNCTION__)
                                   + ": Reliability check failed");
        }
    }

  // now get the value
  if (m_instance->readProperty(m_targetDeviceObjectID, OBJECT_ANALOG_VALUE,
                               objInstance, PROP_PRESENT_VALUE))
    {
      if (m_debugging)
        cerr << __FUNCTION__ << ": (value): " << getAllErrorString()
             << endl;

      throw std::runtime_error(std::string(__FUNCTION__)
                               + ": "
                               + getAllErrorString());
    }

  return m_instance->getDataTypeReal();
}

void BACNETUTIL::setAnalogValue(uint32_t objInstance,
                                float value)
{
  // Write the value
  BACNET_APPLICATION_DATA_VALUE myData =
    m_instance->createDataReal(value);

  // write it
  if (m_instance->writeProperty(m_targetDeviceObjectID, OBJECT_ANALOG_VALUE,
                                objInstance, PROP_PRESENT_VALUE,
                                &myData))
    {
      // error occurred
      if (m_debugging)
        cerr << __FUNCTION__ << ": " << getAllErrorString() << endl;

      throw std::runtime_error(std::string(__FUNCTION__)
                               + ": "
                               + getAllErrorString());
    }
}

string BACNETUTIL::getAnalogValueUnits(uint32_t objInstance)
{
  // see if it exists
  if (m_avUnitCache.count(objInstance) == 0)
    {
      // then we need to fetch it
      if (m_instance->readProperty(m_targetDeviceObjectID, OBJECT_ANALOG_VALUE,
                                   objInstance, PROP_UNITS))
        {
          if (m_debugging)
            cerr << __FUNCTION__ << ": " << getAllErrorString() << endl;
          // set to empty string
          m_avUnitCache[objInstance] = string("");
        }
      else
        {
          // cache it for future calls
          m_avUnitCache[objInstance] =
            string(bactext_engineering_unit_name(m_instance->getDataTypeEnum()));
        }
    }

  return m_avUnitCache[objInstance];
}

float BACNETUTIL::getAnalogInput(uint32_t objInstance)
{
  // check reliability first, if enabled
  if (m_checkReliability)
    {
      if (m_instance->readProperty(m_targetDeviceObjectID, OBJECT_ANALOG_INPUT,
                                   objInstance, PROP_RELIABILITY))
        {
          if (m_debugging)
            cerr << __FUNCTION__ << ": (reliability): "
                 << getAllErrorString() << endl;

          throw std::runtime_error(std::string(__FUNCTION__)
                                   + getAllErrorString());
        }

      BACNET_RELIABILITY reliable =
        static_cast<BACNET_RELIABILITY>(m_instance->getDataTypeEnum());

      if (reliable != RELIABILITY_NO_FAULT_DETECTED)
        {
          if (m_debugging)
            cerr << __FUNCTION__ << ": Reliability check failed" << endl;

          throw std::runtime_error(std::string(__FUNCTION__)
                                   + ": Reliability check failed");
        }
    }

  // now get the value
  if (m_instance->readProperty(m_targetDeviceObjectID, OBJECT_ANALOG_INPUT,
                               objInstance, PROP_PRESENT_VALUE))
    {
      if (m_debugging)
        cerr << __FUNCTION__ << ": (value): " << getAllErrorString() << endl;

      throw std::runtime_error(std::string(__FUNCTION__)
                               + ": "
                               + getAllErrorString());
    }

  return m_instance->getDataTypeReal();
}

bool BACNETUTIL::getBinaryInput(uint32_t objInstance)
{
  // check the BV info, and update/cache the data if needed
  updateBinaryInputInfo(objInstance);

  // check reliability first, if enabled
  if (m_checkReliability)
    {
      if (m_instance->readProperty(m_targetDeviceObjectID, OBJECT_BINARY_INPUT,
                                   objInstance, PROP_RELIABILITY))
        {
          if (m_debugging)
            cerr << __FUNCTION__ << ": (reliability): "
                 << getAllErrorString() << endl;

          throw std::runtime_error(std::string(__FUNCTION__)
                                   + getAllErrorString());
        }

      BACNET_RELIABILITY reliable =
        static_cast<BACNET_RELIABILITY>(m_instance->getDataTypeEnum());

      if (reliable != RELIABILITY_NO_FAULT_DETECTED)
        {
          if (m_debugging)
            cerr << __FUNCTION__ << ": Reliability check failed" << endl;

          throw std::runtime_error(std::string(__FUNCTION__)
                                   + ": Reliability check failed");
        }
    }

  // now get the value
  if (m_instance->readProperty(m_targetDeviceObjectID, OBJECT_BINARY_INPUT,
                               objInstance, PROP_PRESENT_VALUE))
    {
      if (m_debugging)
        cerr << __FUNCTION__ << ": (value): " << getAllErrorString() << endl;

      throw std::runtime_error(std::string(__FUNCTION__)
                               + ": "
                               + getAllErrorString());
    }

  BACNET_BINARY_PV bpv =
    static_cast<BACNET_BINARY_PV>(m_instance->getDataTypeEnum());

  return (bpv == BINARY_INACTIVE) ? false : true;
}

bool BACNETUTIL::getBinaryValue(uint32_t objInstance)
{
  // check the BV info, and update/cache the data if needed
  updateBinaryValueInfo(objInstance);

  // check reliability first, if enabled
  if (m_checkReliability)
    {
      if (m_instance->readProperty(m_targetDeviceObjectID, OBJECT_BINARY_VALUE,
                                   objInstance, PROP_RELIABILITY))
        {
          if (m_debugging)
            cerr << __FUNCTION__ << ": (reliability): "
                 << getAllErrorString() << endl;

          throw std::runtime_error(std::string(__FUNCTION__)
                                   + getAllErrorString());
        }

      BACNET_RELIABILITY reliable =
        static_cast<BACNET_RELIABILITY>(m_instance->getDataTypeEnum());

      if (reliable != RELIABILITY_NO_FAULT_DETECTED)
        {
          if (m_debugging)
            cerr << __FUNCTION__ << ": Reliability check failed" << endl;

          throw std::runtime_error(std::string(__FUNCTION__)
                                   + ": Reliability check failed");
        }
    }

  // now get the value
  if (m_instance->readProperty(m_targetDeviceObjectID, OBJECT_BINARY_VALUE,
                               objInstance, PROP_PRESENT_VALUE))
    {
      if (m_debugging)
        cerr << __FUNCTION__ << ": (value): " << getAllErrorString() << endl;

      throw std::runtime_error(std::string(__FUNCTION__)
                               + ": "
                               + getAllErrorString());
    }

  BACNET_BINARY_PV bpv =
    static_cast<BACNET_BINARY_PV>(m_instance->getDataTypeEnum());

  return (bpv == BINARY_INACTIVE) ? false : true;
}

void BACNETUTIL::setBinaryValue(uint32_t objInstance,
                                bool value)
{
  BACNET_BINARY_PV bpv = (value) ? BINARY_ACTIVE : BINARY_INACTIVE;

  // Write the value
  BACNET_APPLICATION_DATA_VALUE myData =
    m_instance->createDataEnum(bpv);

  // write it
  if (m_instance->writeProperty(m_targetDeviceObjectID, OBJECT_BINARY_VALUE,
                                objInstance, PROP_PRESENT_VALUE,
                                &myData))
    {
      // error occurred
      if (m_debugging)
        cerr << __FUNCTION__ << ": " << getAllErrorString() << endl;

      throw std::runtime_error(std::string(__FUNCTION__)
                               + ": "
                               + getAllErrorString());
    }
}

string BACNETUTIL::getAnalogInputUnits(uint32_t objInstance)
{
  // see if it exists
  if (m_aiUnitCache.count(objInstance) == 0)
    {
      // then we need to fetch it
      if (m_instance->readProperty(m_targetDeviceObjectID, OBJECT_ANALOG_INPUT,
                                   objInstance, PROP_UNITS))
        {
          if (m_debugging)
            cerr << __FUNCTION__ << ": " << getAllErrorString() << endl;
          // set to empty string
          m_aiUnitCache[objInstance] = string("");
        }
      else
        {
          // cache it for future calls
          m_aiUnitCache[objInstance] =
            string(bactext_engineering_unit_name(m_instance->getDataTypeEnum()));
        }
    }

  return m_aiUnitCache[objInstance];
}

unsigned int BACNETUTIL::getMultiStateValue(uint32_t objInstance)
{
  // check the MSV info, and update/cache the data if needed
  updateMultiStateValueInfo(objInstance);

  // check reliability first, if enabled
  if (m_checkReliability)
    {
      if (m_instance->readProperty(m_targetDeviceObjectID,
                                   OBJECT_MULTI_STATE_VALUE,
                                   objInstance, PROP_RELIABILITY))
        {
          if (m_debugging)
            cerr << __FUNCTION__ << ": (reliability): "
                 << getAllErrorString() << endl;

          throw std::runtime_error(std::string(__FUNCTION__)
                                   + getAllErrorString());
        }

      BACNET_RELIABILITY reliable =
        static_cast<BACNET_RELIABILITY>(m_instance->getDataTypeEnum());

      if (reliable != RELIABILITY_NO_FAULT_DETECTED)
        {
          if (m_debugging)
            cerr << __FUNCTION__ << ": Reliability check failed" << endl;

          throw std::runtime_error(std::string(__FUNCTION__)
                                   + ": Reliability check failed");
        }
    }

  // now get the value
  if (m_instance->readProperty(m_targetDeviceObjectID,
                               OBJECT_MULTI_STATE_VALUE,
                               objInstance, PROP_PRESENT_VALUE))
    {
      if (m_debugging)
        cerr << __FUNCTION__ << ": (value): " << getAllErrorString() << endl;

      throw std::runtime_error(std::string(__FUNCTION__)
                               + ": "
                               + getAllErrorString());
    }

  return m_instance->getDataTypeUnsignedInt();
}

void BACNETUTIL::updateMultiStateValueInfo(uint32_t objInstance)
{
  // bail if we already have information on this msv
  if (m_msvInfo.count(objInstance) != 0)
    return;

  // we need to fetch information on MSV's - number of states, and
  // possibly the state-text, if present

  // get the number of values possible (required)
  if (m_instance->readProperty(m_targetDeviceObjectID,
                               OBJECT_MULTI_STATE_VALUE,
                               objInstance, PROP_NUMBER_OF_STATES))
    {
      if (m_debugging)
        cerr << __FUNCTION__
             << ": (number of states): "
             << getAllErrorString()
             << endl;

      throw std::runtime_error(std::string(__FUNCTION__)
                               + ": "
                               + getAllErrorString());
    }

  // Add the entry...
  m_msvInfo[objInstance].numStates = m_instance->getDataTypeUnsignedInt();

  if (m_debugging)
    cerr << __FUNCTION__
         << ": number of states: "
         << m_msvInfo[objInstance].numStates
         << endl;

  // now get the state-text.  This is optional, so we will not throw
  // here.
  if (m_instance->readProperty(m_targetDeviceObjectID,
                               OBJECT_MULTI_STATE_VALUE,
                               objInstance, PROP_STATE_TEXT))
    {
      if (m_debugging)
        cerr << __FUNCTION__
             << ": (state-text): "
             << getAllErrorString()
             << endl;
    }

  // store them
  int numElements = m_instance->getDataNumElements();

  if (m_debugging)
    cerr << __FUNCTION__ << ": numElements: " << numElements << endl;

  if (numElements > 0)
    {
      for (int i=0; i<numElements; i++)
        {
          m_msvInfo[objInstance].stateList.push_back(m_instance->getDataTypeString(i));

          if (m_debugging)
            cerr << __FUNCTION__ << ": " << int(objInstance) << ", "
                 << i << ": "
                 << "added state text string: "
                 << m_msvInfo[objInstance].stateList.at(i)
                 << endl;
        }
    }

  return;
}

void BACNETUTIL::deleteMultiStateValueInfo(uint32_t objInstance)
{
  // if there is no data stored for this objInstance yet, then we do
  // not need to do anything.
  if (m_msvInfo.count(objInstance) == 0)
    return;

  // Now, we just erase the entry, and it will be updated the next
  // time the MSV is accessed.
  m_msvInfo.erase(objInstance);

  return;
}

string BACNETUTIL::lookupMultiStateValueText(uint32_t objInstance,
                                             unsigned int value)
{
  // verify that we have the relevant object data cached. If not, go
  // get it.
  updateMultiStateValueInfo(objInstance);

  // verify that value is valid for this object
  if (value == 0 || value > m_msvInfo[objInstance].numStates)
    throw std::out_of_range(std::string(__FUNCTION__)
                            + ": value supplied is invalid. Maximum "
                            + "allowed values are 1 to "
                            + std::to_string(m_msvInfo[objInstance].numStates)
                            + " for this object");


  // at this point either it failed or suceeded.  If it suceeded, then
  // we will see if any state text was retrieved.  If no text is
  // available (it is an optional property), then we will simply
  // return the value itself as a string.

  if (m_msvInfo[objInstance].stateList.size() > 0)
    {
      // we have state-text and a usable value.

      // value should never be 0 at this point, so compensate for
      // indexing into stateList which is 0-based.
      value--;

      return m_msvInfo[objInstance].stateList.at(value);
    }

  // no stateList text available, so just return value as a string
  return std::to_string(value);
}

unsigned int BACNETUTIL::getMultiStateValueMaxStates(uint32_t objInstance)
{
  // check the MSV info, and update/cache the data if needed
  updateMultiStateValueInfo(objInstance);

  return m_msvInfo[objInstance].numStates;
}

string BACNETUTIL::getMultiStateValueText(uint32_t objInstance)
{
  unsigned int value = getMultiStateValue(objInstance);

  return lookupMultiStateValueText(objInstance, value);
}

void BACNETUTIL::setMultiStateValue(uint32_t objInstance,
                                    unsigned int value)
{
  // check the MSV info, and update/cache the data if needed
  updateMultiStateValueInfo(objInstance);

  // Check value against the valid limits

  if (value == 0 || value > m_msvInfo[objInstance].numStates)
    throw std::invalid_argument(std::string(__FUNCTION__)
                                + ": value supplied is invalid. Maximum "
                                + "allowed values are 1 to "
                                + std::to_string(m_msvInfo[objInstance].numStates)
                                + " for this object");

  // Write the value
  BACNET_APPLICATION_DATA_VALUE myData =
    m_instance->createDataUnsignedInt(value);

  // write it
  if (m_instance->writeProperty(m_targetDeviceObjectID,
                                OBJECT_MULTI_STATE_VALUE,
                                objInstance, PROP_PRESENT_VALUE,
                                &myData))
    {
      // error occurred
      if (m_debugging)
        cerr << __FUNCTION__ << ": " << getAllErrorString() << endl;

      throw std::runtime_error(std::string(__FUNCTION__)
                               + ": "
                               + getAllErrorString());
    }
}

void BACNETUTIL::updateBinaryValueInfo(uint32_t objInstance)
{
  // bail if we already have information on this object
  if (m_bvInfo.count(objInstance) != 0)
    return;

  // fetch inactive/active text.  These are optional accordingto the
  // spec, so we will not throw if they do not exist.

  // get inactive text
  if (m_instance->readProperty(m_targetDeviceObjectID,
                               OBJECT_BINARY_VALUE,
                               objInstance, PROP_INACTIVE_TEXT))
    {
      if (m_debugging)
        cerr << __FUNCTION__
             << ": (inactive text): "
             << getAllErrorString()
             << endl;

      m_bvInfo[objInstance].inactiveText = "inactive";
    }
  else
    {
      m_bvInfo[objInstance].inactiveText = m_instance->getDataTypeString();
    }

  // get active text
  if (m_instance->readProperty(m_targetDeviceObjectID,
                               OBJECT_BINARY_VALUE,
                               objInstance, PROP_ACTIVE_TEXT))
    {
      if (m_debugging)
        cerr << __FUNCTION__
             << ": (active text): "
             << getAllErrorString()
             << endl;

      m_bvInfo[objInstance].activeText = "active";
    }
  else
    {
      m_bvInfo[objInstance].activeText = m_instance->getDataTypeString();
    }

  return;
}

void BACNETUTIL::deleteBinaryValueInfo(uint32_t objInstance)
{
  // if there is no data stored for this objInstance yet, then we do
  // not need to do anything.
  if (m_bvInfo.count(objInstance) == 0)
    return;

  // Now, we just erase the entry, and it will be updated the next
  // time it is accessed.
  m_bvInfo.erase(objInstance);

  return;
}

void BACNETUTIL::updateBinaryInputInfo(uint32_t objInstance)
{
  // bail if we already have information on this object
  if (m_biInfo.count(objInstance) != 0)
    return;

  // fetch inactive/active text.  These are optional accordingto the
  // spec, so we will not throw if they do not exist.

  // get inactive text
  if (m_instance->readProperty(m_targetDeviceObjectID,
                               OBJECT_BINARY_INPUT,
                               objInstance, PROP_INACTIVE_TEXT))
    {
      if (m_debugging)
        cerr << __FUNCTION__
             << ": (inactive text): "
             << getAllErrorString()
             << endl;

      m_biInfo[objInstance].inactiveText = "inactive";
    }
  else
    {
      m_biInfo[objInstance].inactiveText = m_instance->getDataTypeString();
    }

  // get active text
  if (m_instance->readProperty(m_targetDeviceObjectID,
                               OBJECT_BINARY_INPUT,
                               objInstance, PROP_ACTIVE_TEXT))
    {
      if (m_debugging)
        cerr << __FUNCTION__
             << ": (active text): "
             << getAllErrorString()
             << endl;

      m_biInfo[objInstance].activeText = "active";
    }
  else
    {
      m_biInfo[objInstance].activeText = m_instance->getDataTypeString();
    }

  return;
}

void BACNETUTIL::deleteBinaryInputInfo(uint32_t objInstance)
{
  // if there is no data stored for this objInstance yet, then we do
  // not need to do anything.
  if (m_biInfo.count(objInstance) == 0)
    return;

  // Now, we just erase the entry, and it will be updated the next
  // time it is accessed.
  m_biInfo.erase(objInstance);

  return;
}

string BACNETUTIL::lookupBinaryInputText(uint32_t objInstance, bool value)
{
  // cache relevant data if necessary
  updateBinaryInputInfo(objInstance);

 if (value)
    return m_biInfo[objInstance].activeText;
  else
    return m_biInfo[objInstance].inactiveText;
}

string BACNETUTIL::getBinaryInputText(uint32_t objInstance)
{
  bool value = getBinaryInput(objInstance);

  return lookupBinaryInputText(objInstance, value);
}

string BACNETUTIL::lookupBinaryValueText(uint32_t objInstance, bool value)
{
  // cache relevant data if necessary
  updateBinaryValueInfo(objInstance);

 if (value)
    return m_bvInfo[objInstance].activeText;
  else
    return m_bvInfo[objInstance].inactiveText;
}

string BACNETUTIL::getBinaryValueText(uint32_t objInstance)
{
  bool value = getBinaryValue(objInstance);

  return lookupBinaryValueText(objInstance, value);
}

BACNETMSTP::BACERR_TYPE_T BACNETUTIL::getErrorType()
{
  return m_instance->getErrorType();
}

uint8_t BACNETUTIL::getRejectReason()
{
  return m_instance->getRejectReason();
}

std::string BACNETUTIL::getRejectString()
{
  return m_instance->getRejectString();
}

uint8_t BACNETUTIL::getAbortReason()
{
  return m_instance->getAbortReason();
}

std::string BACNETUTIL::getAbortString()
{
  return m_instance->getAbortString();
}

BACNET_ERROR_CLASS BACNETUTIL::getErrorClass()
{
  return m_instance->getErrorClass();
}

BACNET_ERROR_CODE BACNETUTIL::getErrorCode()
{
  return m_instance->getErrorCode();
}

std::string BACNETUTIL::getUPMErrorString()
{
  return m_instance->getUPMErrorString();
}

std::string BACNETUTIL::getErrorString()
{
  return m_instance->getErrorString();
};

string BACNETUTIL::getAllErrorString()
{
  switch (m_instance->getErrorType())
    {
    case BACNETMSTP::BACERR_TYPE_NONE:
      return string("No Error");
      break;

    case BACNETMSTP::BACERR_TYPE_REJECT:
      return string("Reject: ") + getRejectString();
      break;

    case BACNETMSTP::BACERR_TYPE_ABORT:
      return string("Abort: ") + getAbortString();
      break;

    case BACNETMSTP::BACERR_TYPE_ERROR:
      return string("Error: ") + getErrorString();
      break;

    case BACNETMSTP::BACERR_TYPE_UPM:
      return string("UPM Error: ") + getUPMErrorString();
      break;
    }
}

string BACNETUTIL::getDeviceDescription()
{
  if (m_instance->readProperty(m_targetDeviceObjectID, OBJECT_DEVICE,
                               m_targetDeviceObjectID, PROP_DESCRIPTION))
    {
      // error occurred
      if (m_debugging)
        cerr << __FUNCTION__ << ": " << getAllErrorString() << endl;

      return "";
    }

  return m_instance->getDataTypeString();
}

string BACNETUTIL::getDeviceLocation()
{
  if (m_instance->readProperty(m_targetDeviceObjectID, OBJECT_DEVICE,
                               m_targetDeviceObjectID, PROP_LOCATION))
    {
      // error occurred
      if (m_debugging)
        cerr << __FUNCTION__ << ": " << getAllErrorString() << endl;

      return "";
    }

  return m_instance->getDataTypeString();
}

bool BACNETUTIL::setDeviceLocation(string location)
{
  BACNET_APPLICATION_DATA_VALUE myLocation =
    m_instance->createDataString(location);

  // write the Device Object Location
  if (m_instance->writeProperty(m_targetDeviceObjectID, OBJECT_DEVICE,
                                m_targetDeviceObjectID, PROP_LOCATION,
                                &myLocation))
    {
      // error occurred
      if (m_debugging)
        cerr << __FUNCTION__ << ": " << getAllErrorString() << endl;

      return false;
    }

  return true;
}

string BACNETUTIL::getDeviceName()
{
  if (m_instance->readProperty(m_targetDeviceObjectID, OBJECT_DEVICE,
                               m_targetDeviceObjectID, PROP_OBJECT_NAME))
    {
      // error occurred
      if (m_debugging)
        cerr << __FUNCTION__ << ": " << getAllErrorString() << endl;

      return "";
    }

  return m_instance->getDataTypeString();
}

bool BACNETUTIL::setDeviceName(string name)
{
  if (name.size() < 1)
    {
      throw std::invalid_argument(std::string(__FUNCTION__)
                                  + ": name must have at least one character");
    }

  BACNET_APPLICATION_DATA_VALUE myName =
    m_instance->createDataString(name);

  // write the Device Object Location
  if (m_instance->writeProperty(m_targetDeviceObjectID, OBJECT_DEVICE,
                                m_targetDeviceObjectID, PROP_OBJECT_NAME,
                                &myName))
    {
      // error occurred
      if (m_debugging)
        cerr << __FUNCTION__ << ": " << getAllErrorString() << endl;

      return false;
    }

  return true;
}