upm  0.7.0
Sensor/Actuator repository for libmraa (v1.0.0)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
bacnetmstp.hpp
1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2016 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 #pragma once
25 
26 #include <string>
27 
28 // we only support a BACnet RS-485 MS/TP datalink
29 #define BACDL_MSTP 1
30 #undef BACDL_ALL
31 
32 // get a variety of bacnet-stack includes...
33 #include "bacdef.h"
34 #include "config.h"
35 #include "bactext.h"
36 #include "bacerror.h"
37 #include "iam.h"
38 #include "arf.h"
39 #include "tsm.h"
40 #include "address.h"
41 #include "npdu.h"
42 #include "apdu.h"
43 #include "device.h"
44 #include "datalink.h"
45 #include "whois.h"
46 #include "mstpdef.h"
47 #include "dlmstp.h"
48 
49 
50 namespace upm {
51 
103  class BACNETMSTP {
104  // Constructor and destructor are protected
105 
106  public:
107 
108  // error types
109  typedef enum {
110  BACERR_TYPE_NONE = 0,
111  BACERR_TYPE_REJECT,
112  BACERR_TYPE_ABORT,
113  BACERR_TYPE_ERROR,
114  BACERR_TYPE_UPM
115  } BACERR_TYPE_T;
116 
117  // command types we currently support
118  typedef enum {
119  BACCMD_NONE = 0,
120  BACCMD_READ_PROPERTY,
121  BACCMD_WRITE_PROPERTY
122  } BACCMD_TYPE_T;
123 
131  static BACNETMSTP* instance();
132 
162  void initMaster(std::string port, int baudRate, int deviceInstanceNumber,
163  int macAddr, int maxMaster=DEFAULT_MAX_MASTER,
164  int maxInfoFrames=1);
165 
166 
192  bool readProperty(uint32_t targetDeviceInstanceID,
193  BACNET_OBJECT_TYPE objType,
194  uint32_t objInstance,
195  BACNET_PROPERTY_ID objProperty,
196  uint32_t arrayIndex=BACNET_ARRAY_ALL);
197 
232  bool writeProperty(uint32_t targetDeviceInstanceID,
233  BACNET_OBJECT_TYPE objType,
234  uint32_t objInstance,
235  BACNET_PROPERTY_ID objProperty,
236  BACNET_APPLICATION_DATA_VALUE* propValue,
237  uint8_t propPriority=BACNET_NO_PRIORITY,
238  int32_t arrayIndex=BACNET_ARRAY_ALL);
239 
248  BACNET_APPLICATION_DATA_VALUE getData();
249 
257  uint8_t getDataType();
258 
268  float getDataTypeReal();
269 
279  bool getDataTypeBoolean();
280 
290  unsigned int getDataTypeUnsignedInt();
291 
301  int getDataTypeSignedInt();
302 
313  std::string getDataTypeString();
314 
323  unsigned int getDataTypeEnum();
324 
325 #if defined(BACAPP_DOUBLE)
326 
335  double getDataTypeDouble();
336 #endif // BACAPP_DOUBLE
337 
347  BACNET_APPLICATION_DATA_VALUE createDataReal(float Real);
348 
358  BACNET_APPLICATION_DATA_VALUE createDataBool(bool value);
359 
369  BACNET_APPLICATION_DATA_VALUE createDataSignedInt(int value);
370 
381  BACNET_APPLICATION_DATA_VALUE createDataUnsignedInt(unsigned int value);
382 
393  BACNET_APPLICATION_DATA_VALUE createDataString(std::string value);
394 
402  BACERR_TYPE_T getErrorType()
403  {
404  return m_errorType;
405  };
406 
412  uint8_t getRejectReason()
413  {
414  return m_rejectReason;
415  };
416 
422  std::string getRejectString()
423  {
424  return m_rejectString;
425  };
426 
432  uint8_t getAbortReason()
433  {
434  return m_abortReason;
435  };
436 
442  std::string getAbortString()
443  {
444  return m_abortString;
445  };
446 
452  BACNET_ERROR_CLASS getErrorClass()
453  {
454  return m_errorClass;
455  };
456 
462  BACNET_ERROR_CODE getErrorCode()
463  {
464  return m_errorCode;
465  };
466 
473  std::string getErrorString()
474  {
475  return m_errorString;
476  };
477 
484  std::string getUPMErrorString()
485  {
486  return m_upmErrorString;
487  };
488 
496  {
497  return m_initialized;
498  };
499 
505  std::string getPort()
506  {
507  return m_port;
508  };
509 
517  {
518  return m_deviceInstanceID;
519  };
520 
527  {
528  return m_maxInfoFrames;
529  };
530 
537  {
538  return m_maxMaster;
539  };
540 
547  {
548  return m_baudRate;
549  };
550 
557  {
558  return m_macAddr;
559  };
560 
566  void setDebug(bool enable);
567 
568  protected:
572  BACNETMSTP();
573 
577  ~BACNETMSTP();
578 
579  // clear/reset error states
580  void clearErrors();
581 
582  // error handler
583  static void handlerError(BACNET_ADDRESS * src,
584  uint8_t invoke_id,
585  BACNET_ERROR_CLASS error_class,
586  BACNET_ERROR_CODE error_code);
587 
588  // abort handler
589  static void handlerAbort(BACNET_ADDRESS * src,
590  uint8_t invoke_id,
591  uint8_t abort_reason,
592  bool server);
593 
594  // reject handler
595  static void handlerReject(BACNET_ADDRESS * src,
596  uint8_t invoke_id,
597  uint8_t reject_reason);
598 
599 
600  // our handler for dealing with return data from a ReadProperty call
601  static void handlerReadPropertyAck(uint8_t* service_request,
602  uint16_t service_len,
603  BACNET_ADDRESS* src,
604  BACNET_CONFIRMED_SERVICE_ACK_DATA* service_data);
605 
606  // our handler for writeProp acks
607  static void handlerWritePropertyAck(BACNET_ADDRESS* src,
608  uint8_t invoke_id);
609 
610  // initialize our service handlers
611  void initServiceHandlers();
612 
613  // utility function
614  std::string string2HexString(std::string input);
615 
616  // responsible for dispatching a request to the BACnet network
617  bool dispatchRequest();
618 
619  private:
620  // prevent copying and assignment
621  BACNETMSTP(BACNETMSTP const &) {};
622  BACNETMSTP& operator=(BACNETMSTP const&) {};
623 
624  // our class instance
625  static BACNETMSTP* m_instance;
626 
627  // has the class been created yet?
628  bool m_initialized;
629 
630  // Some items we can set for our master
631  std::string m_port;
632  int m_maxInfoFrames;
633  int m_maxMaster;
634  int m_baudRate;
635  int m_macAddr;
636 
637  // the unique Instance Number of our Master Device Object
638  uint32_t m_deviceInstanceID;
639 
640  // adpu timeout in milliseconds
641  uint16_t m_adpuTimeoutMS;
642 
643  // buffer used for receiving data
644  uint8_t m_rxBuffer[MAX_MPDU];
645 
646  // our error classfication
647  BACERR_TYPE_T m_errorType;
648 
649  // BACnet reject info
650  uint8_t m_rejectReason;
651  std::string m_rejectString;
652 
653  // BACnet abort info
654  uint8_t m_abortReason;
655  std::string m_abortString;
656 
657  // BACnet error info
658  BACNET_ERROR_CLASS m_errorClass;
659  BACNET_ERROR_CODE m_errorCode;
660  std::string m_errorString;
661 
662  // generic UPM related errors - we just set the error text to
663  // something informative.
664  std::string m_upmErrorString;
665 
666  // our returned data from readProperty()
667  BACNET_APPLICATION_DATA_VALUE m_returnedValue;
668 
669  // current bound target address of dispatched service request
670  // (read/write prop, etc)
671  BACNET_ADDRESS m_targetAddress;
672 
673  // current invokeID (for transaction handling)
674  uint8_t m_invokeID;
675 
676  // error detected flag
677  bool m_errorDetected;
678 
679  // Commands - we create a struct to hold the arguments for each
680  // command type we support. Then, we create a command struct
681  // which contains the command type and a union containing the
682  // relevant arguments. This is used by dispatchRequest() to issue
683  // the correct request.
684 
685  // these may generate SWIG warnings, but they can be ignored as we
686  // do not expose these outside the class
687  typedef struct {
688  uint32_t targetDeviceInstanceID;
689  BACNET_OBJECT_TYPE objType;
690  uint32_t objInstance;
691  BACNET_PROPERTY_ID objProperty;
692  uint32_t arrayIndex;
693  } READ_PROPERTY_ARGS_T;
694 
695  typedef struct {
696  uint32_t targetDeviceInstanceID;
697  BACNET_OBJECT_TYPE objType;
698  uint32_t objInstance;
699  BACNET_PROPERTY_ID objProperty;
700  BACNET_APPLICATION_DATA_VALUE* propValue;
701  uint8_t propPriority;
702  int32_t arrayIndex;
703  } WRITE_PROPERTY_ARGS_T;
704 
705  struct {
706  BACCMD_TYPE_T cmd;
707 
708  union {
709  READ_PROPERTY_ARGS_T readPropArgs;
710  WRITE_PROPERTY_ARGS_T writePropArgs;
711  };
712  } m_command;
713 
714  bool m_debugging;
715  };
716 }
UPM API for BACNETMSTP.
Definition: bacnetmstp.hpp:103
int getMaxInfoFrames()
Definition: bacnetmstp.hpp:526
std::string getErrorString()
Definition: bacnetmstp.hpp:473
std::string getDataTypeString()
Definition: bacnetmstp.cxx:720
std::string getUPMErrorString()
Definition: bacnetmstp.hpp:484
unsigned int getDataTypeUnsignedInt()
Definition: bacnetmstp.cxx:656
std::string getRejectString()
Definition: bacnetmstp.hpp:422
uint8_t getDataType()
Definition: bacnetmstp.cxx:613
uint32_t getDeviceInstanceID()
Definition: bacnetmstp.hpp:516
void setDebug(bool enable)
Definition: bacnetmstp.cxx:74
static BACNETMSTP * instance()
Definition: bacnetmstp.cxx:240
bool isInitialized()
Definition: bacnetmstp.hpp:495
BACERR_TYPE_T getErrorType()
Definition: bacnetmstp.hpp:402
uint8_t getAbortReason()
Definition: bacnetmstp.hpp:432
float getDataTypeReal()
Definition: bacnetmstp.cxx:618
BACNET_APPLICATION_DATA_VALUE getData()
Definition: bacnetmstp.cxx:608
int getDataTypeSignedInt()
Definition: bacnetmstp.cxx:667
BACNET_APPLICATION_DATA_VALUE createDataUnsignedInt(unsigned int value)
Definition: bacnetmstp.cxx:830
BACNET_APPLICATION_DATA_VALUE createDataSignedInt(int value)
Definition: bacnetmstp.cxx:818
int getMaxMaster()
Definition: bacnetmstp.hpp:536
bool getDataTypeBoolean()
Definition: bacnetmstp.cxx:645
BACNET_APPLICATION_DATA_VALUE createDataString(std::string value)
Definition: bacnetmstp.cxx:842
std::string getPort()
Definition: bacnetmstp.hpp:505
bool readProperty(uint32_t targetDeviceInstanceID, BACNET_OBJECT_TYPE objType, uint32_t objInstance, BACNET_PROPERTY_ID objProperty, uint32_t arrayIndex=BACNET_ARRAY_ALL)
Definition: bacnetmstp.cxx:536
std::string getAbortString()
Definition: bacnetmstp.hpp:442
BACNET_ERROR_CODE getErrorCode()
Definition: bacnetmstp.hpp:462
BACNET_APPLICATION_DATA_VALUE createDataReal(float Real)
Definition: bacnetmstp.cxx:794
~BACNETMSTP()
Definition: bacnetmstp.cxx:68
uint8_t getRejectReason()
Definition: bacnetmstp.hpp:412
BACNET_ERROR_CLASS getErrorClass()
Definition: bacnetmstp.hpp:452
void initMaster(std::string port, int baudRate, int deviceInstanceNumber, int macAddr, int maxMaster=DEFAULT_MAX_MASTER, int maxInfoFrames=1)
Definition: bacnetmstp.cxx:248
int getBaudRate()
Definition: bacnetmstp.hpp:546
BACNET_APPLICATION_DATA_VALUE createDataBool(bool value)
Definition: bacnetmstp.cxx:806
unsigned int getDataTypeEnum()
Definition: bacnetmstp.cxx:709
bool writeProperty(uint32_t targetDeviceInstanceID, BACNET_OBJECT_TYPE objType, uint32_t objInstance, BACNET_PROPERTY_ID objProperty, BACNET_APPLICATION_DATA_VALUE *propValue, uint8_t propPriority=BACNET_NO_PRIORITY, int32_t arrayIndex=BACNET_ARRAY_ALL)
Definition: bacnetmstp.cxx:570
int getMACAddress()
Definition: bacnetmstp.hpp:556
BACNETMSTP()
Definition: bacnetmstp.cxx:43