Introduction:
BACnet, or Building Automation and Control net, is an Ethernet based, ASHRAE (American Society for Heating, Refrigeration and Air-Conditioning Engineers) standard. It is a modern, complex and powerful communications protocol. Some open source implementations are available to support BACnet over RS-232,
Arcnet, Ethernet TCP/IP or just Ethernet.
This protocol defines data communication services for computer equipments which are used for monitoring and control of HVAC&R. It provides comprehensive set of messages for conveying encoded binary, analog and alphanumeric data between devices.
The objective of this article is to give overview of BACnet protocol and provide details about adding a new service(s) to open source protocol stack. For more details about protocol, refer to BACnet specification.
Keywords: BACnet protocol, Building Automation
Description:
The BACNET protocol is based on a four layer collapsed architecture which corresponds to the physical, data link, network, and application layers of the OSI reference model (as shown in figure).
BACnet Stack
The source code can be downloaded from http://sourceforge.net/projects/bacnet
Application Layer
The common appearance of the application layer protocol control information is shown in figure below.
BACnet App Layer
The PDU Type field is always present and indicates the used service primitives. Six groups of services are possible: Confirmed Service, Unconfirmed Service, Segment Acknowledge, Reject, Abort and Error, each service expressed by a different PDU Type number.
For guaranteeing delivery the Confirmed Service is used, with unique Invoke ID numbers to confirm each request using this number. The Invoke ID will be omitted when the unsecured Unconfirmed Service is selected in the PDU Type field. Segmented messages are indicated by the PDU Flags. Finally, the Reject, Abort and Error Services provide error detection and recovery.
Objects
Objects are modeling of Control Devices as object (as in OO concept) An object is a data structure to store device information. All objects are referenced by their Object Identifier property. Thus, Object Identifier + Device Id provides unique mechanism of referencing every object in Control System Network. Each object type has different set of properties; refer to BACnet specification for more details. Services (Methods) are to be provided to access these properties.
BACnet Object
Some of these objects are the ANALOG-INPUT, ANALOG-OUTPUT, BINARY-INPUT, and BINARY-OUTPUT. A read access of the input object’s Present-Value property results in reading a value that represents the state of a hardware dependent analog or binary input. A write access to the output object’s Present-Value changes the state of a hardware dependent analog or binary output.
Most of the objects and properties are encoded inside PDU to ensure a secure control of the system. To encode these objects, properties and their values inside the PDU a special combination of tags are used.
Services
Services are set of functions used by protocol service provider and protocol service user. The services provided by the application layer are divided into 6 groups. BACnet defines confirmed application services based on a client and server communication model. The BACnet-user that assumes the role of a client is called the requesting BACnet-user” and the BACnet-user that assumes the role of the server is called the “responding BACnet-user.” The client and server
model do not apply to unconfirmed services.
BACnet_Services
A client requests service from a server via a particular service request instance /number. The server provides service to a client and responds to the request. Invoke ID is used to track the service request.
Alarm and Event Services
Sometimes it is necessary to inform a operator if a particular event has occurred, e.g. an alarm. This group of services provides functions to notify a remote device if a specified value has been changed, or a specified event has occurred, to acknowledge a notification and to subscribe to a list of notified remote devices.
If a remote user wants to be notified if a value of a specified property of a particular object has been changed, it has to invoke the subscribeCOV service that adds this remote user into a list. When the value of this property now changes the device itself invokes the COVNotification service for each remote user in the subscriber list. This service can be sent as a confirmed or unconfirmed message, as chosen by the subscriber. For notification on any event, the device uses the Event Notification service. This service can also be sent as a confirmed or unconfirmed message, and some events, e.g., the event of type alarm may require a operator acknowledgment which can be convoyed using the Acknowledge Alarm service. And at last it is possible to get a summary of every event-initiating object of a specified device using the getEnrollmentSummary. The objects can be selected by the use of filters to define search criteria. For a summary of active alarms only, which means only objects in an abnormal state and notification type alarm, the getAlarmSummary service is adequate and faster.
File Access Services
For accessing the object of type file there are two services available, the atomicReadFile service and the atomicWriteFile service. These services perform an open-read-close operation and an open-write-close operation respectively on the contents of the specified file. The file can be accessed as records or as a stream of octets.
Object Access Services
This group provides services to access and manipulate the properties of objects whose properties are accessible. The value of one specified property of object can be requested with the readProperty service. This function allows read access to any property of any object. Modification of property values is conveyed by the writeProperty service. An object property that is a list requires special functions to add and remove one or more list elements from this specified property. These functions are provided by the addListElement and removeListElement services. It is possible to create new instances of an object using the createObject service. The behavior of such created objects depends on the implementation and on the device itself. Initial values for the object’s properties may be provided with this function. Existing objects can be deleted using the deleteObject service. The event enrollment objects, which contain information required for managing events.
Remote Device Management Services
It is used to determine all devices and their network addresses by using the who-Is service. The devices respond using the i-Am service and identify themselves to the requesting device. The I-Have service is a response to the who-Has service, but it can also be used to advertise the existence of a given named object or an object identifier. The Time Synchronization service is used to notify a device of the correct current time, so that the device may synchronize its internal clock with other devices.
Virtual Terminal Services
The maintenance and the advanced diagnostic features of an operating device are supported by using these services. The BACNET defines three services to establish a VT-session, to exchange character-oriented data and to terminate this session. The VTOpen service establishes a VT-session, the VTData service
transfers data and VTClose service terminates a previously established specified session or a list of sessions.
Network Security Services
Network security can be achieved by exchanging and requesting of 56-bit Data Encryption Standard (DES) cryptographic keys. A common session key can be obtained by a specified key server using the requestKey service.
Architecture of BACNET Stack – Single Threaded
The following section talks about the open source protocol stack. It gives understanding of how a given request travels through various code segments. Example:- client requests for a file read.
BACnet Design Flow
How to add a new service
Following steps give brief about adding a new service to BACnet. Before going into details some pre-requisites are as follows.
Pre-requisite #1: You must have good understanding of section 12, 13, 20 and 21 of the BACnet specification. It is must.
Pre-requisite #2: You should know architecture of source code.
Steps
1) Copy source code of similar service. For writing unconfirmed service, copy existing unconfirmed service source code. The source code for a given service ideally consists of 4 files, for example
a. utm.c – containing encoding & decoding of APDU
b. utm.h – data structures and prototype declaration
c. h_utm.c – receive handler code. This is a callback function, which
decodes APDU. Further processing of IDU-ESMS specific should be
done here.
d. s_utm.c – send API for initiator, calls functions to encode APDU.
2) Then use the detailed description of the service entities given in Clause 21 of the BACnet standard. The trick is reading ASN.1 and figuring out the meaning of when to use context or application tags and when to use opening and closing context tags.
3) If the entity is listed with brackets [], then it is context tagged. There are separate functions for encoding application tags and separate functions for encoding of context tags. It is quite obvious that if encoding is done using context tags then decoding should be done using context tags.
4) If there are no brackets, it is application tagged.
5) If there is a complex data type (like ABSTRACT SYNTAX), then it uses opening and closing context tags around it.
6) Everything ends up being encoded with primitive data types using functions from bacdcode.c. This file contains encoding and decoding functions for all types of data e.g. unsigned, string, enumerated etc.
7) Header file should contain structure definition as per Clause 21. This clause also defines types of variables within that structure.
8) After writing the send and receive API, please register (or add) receive handler using “apdu_set_confirmed_handler” or “apdu_set_unconfirmed_handler” or other error handlers
9) Then start unit testing your code.
With this reader should be able to get started with BACnet development and testing.