一、协议栈概述
1.说明
BACnet Stack协议栈一个用于嵌入式系统的开源 BACnet 协议栈。
BACnet Protocol Stack工程是开发者Steve Karg在sourceforge上维护的一个关于BACnet的开源工程,它随着工程的应用与发展而相应的进行更新。
其中已经有例程支持:Linux、win32、rtos等平台,并且也支持实现BACnet服务器或者BACnet路由器的功能,在协议栈中还提供了命令行实现BACnnet客户端服务,用于调试协议栈。
综上所述,通过此协议栈可以实现BACnet路由器、BACnet服务端、BACnet客户端,并且也支持了大多数协议要求的BACnet可支持的常规功能:COV、BBMD、IPV6。
2.优势与特点
BACnet Protocol Stack可以移植在各种嵌入式设备上,兼容了各种平台:linux win32 pic单片机 arm7,且实现的功能较为完善,提供各种服务。
BACnet源码是用 C 语言编写的,可移植,包括单元测试(基于 PC 的单元测试)和示例应用程序代码。 由于代码被设计为可移植的,所以它使用 GCC 和其他编译器进行编译,比如 Borland C++、Visual C++、MinGW、code Warrior、IAR 或 MicroChip C18。 源码也被设计成高质量、可读、可理解和易于使用的代码。
为了保证其灵活以及高移植性,协议栈中将BACnet的核心功能做支持,如:APDU、NPDU的解码,MSTP、BACnet/IP接收发送处理等。而对于更进一步的NPDU、APUD处理、数据点控制解析等更细节的实现都被放在demo中进行实现和展示。
由上便会发现,通过参考demo部分才能理解BACnet应用层的处理,然而其实现完成度不统一,大多数的demo给出的方法并不能商用,有待后续开发。
二、协议栈解析
1.原始文件目录说明

上图所示为工程的一级目录,由于该协议栈支持多平台,因此在开发者只使用单个开发平台的情况,只需要包含相应需要的文件即可。
| 目录 |
说明 |
| bin |
存放生成的二进制应用程序 |
| demo |
存放工程服务例程文件夹,其中存放针对不同系统实现的BACnet服务处理例程 |
| doc |
工程说明文档 |
| include |
工程头文件统一集合存放于此,其中定义宏、结构体、共用体用来表示报文类型、服务类型等 |
| lib |
通用静态库 |
| ports |
存放平台相关代码,下包含:Linux、rtos32、win32、stm32f103等子目录,通过选择配置到工程中。该文件夹与demo文件夹不同之处在于其实现程度更高,功能集成程度更好,demo目录是ports目录的子集。 |
| src |
此文件夹存放协议栈具体实现源码,其中功能包括NPDU与APDU的处理、对象模型、MSTP状态机、BACnet/IP中的虚拟链路层。 |
为了方便理解该协议栈工作原理,可以从使用makefile构建工程的过程中,了解各个文件目录的应用关系,本文不便继续深入。
2.功能模块说明
该章节简明描述,要移植实现BACnet协议栈,必要的功能模块或者文件。
注意:该部分并不是按照目录说明进行拆分的,因为协议栈是包含多个平台的并不适合按照目录进行说明,因此本人参考其中几个平台的实现,将其总结为以下几种方式。
(1)src
该工程目录是协议栈处理逻辑的具体实现,且内部较为完善,若非必要,不应对其进行修改。src本身不涉及物理接口的配置与实现,只是作为报文解析处理功能,若要实现具体的功能还需基于此进行封装开发。
src中包含对协议栈的配置,在config.h中可以定义要使用的物理层、APDU最大长度、BBMD功能等。
在BACnet协议栈的设计中,同一时间仅能使用一个物理层接口,此处可以参考datalink.h的实现:
#if defined(BACDL_ETHERNET)
#include "ethernet.h"
#define datalink_init ethernet_init
#define datalink_send_pdu ethernet_send_pdu
#define datalink_receive ethernet_receive
#define datalink_cleanup ethernet_cleanup
#define datalink_get_broadcast_address ethernet_get_broadcast_address
#define datalink_get_my_address ethernet_get_my_address
#elif defined(BACDL_ARCNET)
#include "arcnet.h"
#define datalink_init arcnet_init
#define datalink_send_pdu arcnet_send_pdu
#define datalink_receive arcnet_receive
#define datalink_cleanup arcnet_cleanup
#define datalink_get_broadcast_address arcnet_get_broadcast_address
#define datalink_get_my_address arcnet_get_my_address
#elif defined(BACDL_MSTP)
#include "dlmstp.h"
#define datalink_init dlmstp_init
#define datalink_send_pdu dlmstp_send_pdu
#define datalink_receive dlmstp_receive
#define datalink_cleanup dlmstp_cleanup
#define datalink_get_broadcast_address dlmstp_get_broadcast_address
#define datalink_get_my_address dlmstp_get_my_address
#elif defined(BACDL_BIP)
#include "bip.h"
#include "bvlc.h"
#define datalink_init bip_init
#if defined(BBMD_ENABLED) && BBMD_ENABLED
#define datalink_send_pdu bvlc_send_pdu
#define datalink_receive bvlc_receive
#else
#define datalink_send_pdu bip_send_
#define datalink_receive bip_receive
#endif
#define datalink_cleanup bip_cleanup
#define datalink_get_broadcast_address bip_get_broadcast_address
#ifdef BAC_ROUTING
extern void routed_get_my_address(
BACNET_ADDRESS * my_address);
#define datalink_get_my_address routed_get_my_address
#else
#define datalink_get_my_address bip_get_my_address
#endif
#elif defined(BACDL_BIP6)
#include "bip6.h"
#include "bvlc6.h"
#define datalink_init bip6_init
#define datalink_send_pdu bip6_send_pdu
#define datalink_receive bip6_receive
#define datalink_cleanup bip6_cleanup
#define datalink_get_broadcast_address bip6_get_broadcast_address
#define datalink_get_my_address bip6_get_my_address
#elif defined(BACDL_ALL) || defined(BACDL_NONE)
#include "npdu.h"
#define MAX_HEADER (8)
#define MAX_MPDU (MAX_HEADER+MAX_PDU)
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
int datalink_send_pdu(
BACNET_ADDRESS * dest,
BACNET_NPDU_DATA * npdu_data,
uint8_t * pdu,
unsigned pdu_len);
extern uint16_t datalink_receive(
BACNET_ADDRESS * src,
uint8_t * pdu,
uint16_t max_pdu,
unsigned timeout);
extern void datalink_cleanup(
void);
extern void datalink_get_broadcast_address(
BACNET_ADDRESS * dest);
extern void datalink_get_my_address(
BACNET_ADDRESS * my_address);
extern void datalink_set_interface(
char *ifname);
extern void datalink_set(
char *datalink_string);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif
/** @defgroup DataLink The BACnet Network (DataLink) Layer
* <b>6 THE NETWORK LAYER </b><br>
* The purpose of the BACnet network layer is to provide the means by which
* messages can be relayed from one BACnet network to another, regardless of
* the BACnet data link technology in use on that network. Whereas the data
* link layer provides the capability to address messages to a single device
* or broadcast them to all devices on the local network, the network layer
* allows messages to be directed to a single remote device, broadcast on a
* remote network, or broadcast globally to all devices on all networks.
* A BACnet Device is uniquely located by a network number and a MAC address.
*
* Each client or server application must define exactly one of these
* DataLink settings, which will control which parts of the code will be built:
* - BACDL_ETHERNET -- for Clause 7 ISO 8802-3 ("Ethernet") LAN
* - BACDL_ARCNET -- for Clause 8 ARCNET LAN
* - BACDL_MSTP -- for Clause 9 MASTER-SLAVE/TOKEN PASSING (MS/TP) LAN
* - BACDL_BIP -- for ANNEX J - BACnet/IP
* - BACDL_ALL -- Unspecified for the build, so the transport can be
* chosen at runtime from among these choices.
* - Clause 10 POINT-TO-POINT (PTP) and Clause 11 EIA/CEA-709.1 ("LonTalk") LAN
* are not currently supported by this project.
*//** @defgroup DLTemplates DataLink Template Functions
* @ingroup DataLink
* Most of the functions in this group are function templates which are assigned
* to a specific DataLink network layer implementation either at compile time or
* at runtime.
*/
因为datalink对只能使用一种物理层的限制,这样就导致若用此开发BACnet路由器,则不能使用datalink.h的接口。在协议栈中,BACnet路由器的开发与网关开发是不同的,待后续预研路由器功能再对此进行丰富。
(2)平台处理
该部分是BACnet协议栈基于src实现的接口函数,src中的处理是对协议的实现,而该部分处理则是对功能的实现。
device
在前文中,我们已经有了数据解析的接口,但BACnet在实际使用中设计数据点的添加配置读写,如何实现设备不同数据读写的填充,协议栈将其通过拆分,通过实现device用作功能管理的接口分层。
该部分实现基础内容如下:
- 必须实现一个device读写,该device是指的BACnet设备对象,是必须具备的。
- 根据所需数据类型:AI、AO、BI、BV等,添加数据点操作函数。
- 接收数据后,会调用应用层的回调函数,根据协议解析再分配到对应的数据点读写函数上。
除此之外还需要兼容适配一些特殊数据属性:
- 支持COV
- 设备参数配置
每一个对应平台都应实现各自对应的device,每个对应的device便是包含了设备操作以及各种标签类型(ai/ao/di/do/bi/bo/...)的初始化、读、写等操作。
协议栈中提供的device并不能完全适配我们当前开发的工程,预研开发阶段便发现有大量的device接口需要进行再次适配,这也进一步说明,该协议栈上层接口仅对平台提供示例,基于我们目前的工程还需优化适配。
handlers
handlers是BACnet基于src以及平台特性将src与object结合再次封装的处理接口,handler着重于功能的实现,向应用层提供功能接口,用于具体功能的实现。
BACnet数据服务处理函数在此中实现,按照实际使用需要进行移植适配,其中包含的必要处理操作如:
- npdu的解析
- whohas、whois、iam、ihave处理报文
- whohas、whois、iam、ihave报文发送
- 读写数据处理
- ......
其余有各种功能的具体实现:
- COV处理接口
- 路由器功能接口
- ......
总而言之,涉及具体的报文交互以及功能部分,都在此进行了具体实现。
该部分是对src的进一步封装,目的是为了适配更丰富的功能,在开发时根据需要的功能进行载入即可。
数据操作
协议栈中需要开发者去实现数据点处理函数,device中的数据点的功能函数便是在此实现,根据所开发功能不同,进行不同的配置。
对于数据点的实现可以参考结构体定义,从而编写对应功能函数:
/** Defines the group of object helper functions for any supported Object.
* @ingroup ObjHelpers
* Each Object must provide some implementation of each of these helpers
* in order to properly support the handlers. Eg, the ReadProperty handler
* handler_read_property() relies on the instance of Object_Read_Property
* for each Object type, or configure the function as NULL.
* In both appearance and operation, this group of functions acts like
* they are member functions of a C++ Object base class.
*/
typedef struct object_functions {
BACNET_OBJECT_TYPE Object_Type;
object_init_function Object_Init;
object_count_function Object_Count;
object_index_to_instance_function Object_Index_To_Instance;
object_valid_instance_function Object_Valid_Instance;
object_name_function Object_Name;
read_property_function Object_Read_Property;
write_property_function Object_Write_Property;
rpm_property_lists_function Object_RPM_List;
rr_info_function Object_RR_Info;
object_iterate_function Object_Iterator;
object_value_list_function Object_Value_List;
object_cov_function Object_COV;
object_cov_clear_function Object_COV_Clear;
object_intrinsic_reporting_function Object_Intrinsic_Reporting;
} object_functions_t;
以下为不同平台下device.c中对应数据列表添加示例:
static object_functions_t My_Object_Table[] = {
{OBJECT_DEVICE,
NULL /* Init - don't init Device or it will recourse! */ ,
Device_Count,
Device_Index_To_Instance,
Device_Valid_Object_Instance_Number,
Device_Object_Name,
Device_Read_Property_Local,
Device_Write_Property_Local,
Device_Property_Lists,
DeviceGetRRInfo,
NULL /* Iterator */ ,
NULL /* Value_Lists */ ,
NULL /* COV */ ,
NULL /* COV Clear */ ,
NULL /* Intrinsic Reporting */ },
{OBJECT_ANALOG_INPUT,
Analog_Input_Init,
Analog_Input_Count,
Analog_Input_Index_To_Instance,
Analog_Input_Valid_Instance,
Analog_Input_Object_Name,
Analog_Input_Read_Property,
Analog_Input_Write_Property,
Analog_Input_Property_Lists,
NULL /* ReadRangeInfo */ ,
NULL /* Iterator */ ,
NULL /* Value_Lists */ ,
NULL /* COV */ ,
NULL /* COV Clear */ ,
Analog_Input_Intrinsic_Reporting},
{OBJECT_ANALOG_OUTPUT,
Analog_Output_Init,
Analog_Output_Count,
Analog_Output_Index_To_Instance,
Analog_Output_Valid_Instance,
Analog_Output_Object_Name,
Analog_Output_Read_Property,
Analog_Output_Write_Property,
Analog_Output_Property_Lists,
NULL /* ReadRangeInfo */ ,
NULL /* Iterator */ ,
NULL /* Value_Lists */ ,
NULL /* COV */ ,
NULL /* COV Clear */ ,
NULL /* Intrinsic Reporting */ },
{OBJECT_ANALOG_VALUE,
Analog_Value_Init,
Analog_Value_Count,
Analog_Value_Index_To_Instance,
Analog_Value_Valid_Instance,
Analog_Value_Object_Name,
Analog_Value_Read_Property,
Analog_Value_Write_Property,
Analog_Value_Property_Lists,
NULL /* ReadRangeInfo */ ,
NULL /* Iterator */ ,
NULL /* Value_Lists */ ,
NULL /* COV */ ,
NULL /* COV Clear */ ,
Analog_Value_Intrinsic_Reporting},
{OBJECT_BINARY_INPUT,
Binary_Input_Init,
Binary_Input_Count,
Binary_Input_Index_To_Instance,
Binary_Input_Valid_Instance,
Binary_Input_Object_Name,
Binary_Input_Read_Property,
Binary_Input_Write_Property,
Binary_Input_Property_Lists,
NULL /* ReadRangeInfo */ ,
NULL /* Iterator */ ,
Binary_Input_Encode_Value_List,
Binary_Input_Change_Of_Value,
Binary_Input_Change_Of_Value_Clear,
NULL /* Intrinsic Reporting */ },
{OBJECT_BINARY_OUTPUT,
Binary_Output_Init,
Binary_Output_Count,
Binary_Output_Index_To_Instance,
Binary_Output_Valid_Instance,
Binary_Output_Object_Name,
Binary_Output_Read_Property,
Binary_Output_Write_Property,
Binary_Output_Property_Lists,
NULL /* ReadRangeInfo */ ,
NULL /* Iterator */ ,
NULL /* Value_Lists */ ,
NULL /* COV */ ,
NULL /* COV Clear */ ,
NULL /* Intrinsic Reporting */ },
{OBJECT_BINARY_VALUE,
Binary_Value_Init,
Binary_Value_Count,
Binary_Value_Index_To_Instance,
Binary_Value_Valid_Instance,
Binary_Value_Object_Name,
Binary_Value_Read_Property,
Binary_Value_Write_Property,
Binary_Value_Property_Lists,
NULL /* ReadRangeInfo */ ,
NULL /* Iterator */ ,
NULL /* Value_Lists */ ,
NULL /* COV */ ,
NULL /* COV Clear */ ,
NULL /* Intrinsic Reporting */ },
#if 0
{OBJECT_CHARACTERSTRING_VALUE,
CharacterString_Value_Init,
CharacterString_Value_Count,
CharacterString_Value_Index_To_Instance,
CharacterString_Value_Valid_Instance,
CharacterString_Value_Object_Name,
CharacterString_Value_Read_Property,
CharacterString_Value_Write_Property,
CharacterString_Value_Property_Lists,
NULL /* ReadRangeInfo */ ,
NULL /* Iterator */ ,
NULL /* Value_Lists */ ,
NULL /* COV */ ,
NULL /* COV Clear */ ,
NULL /* Intrinsic Reporting */ },
#endif
#if defined(INTRINSIC_REPORTING)
{OBJECT_NOTIFICATION_CLASS,
Notification_Class_Init,
Notification_Class_Count,
Notification_Class_Index_To_Instance,
Notification_Class_Valid_Instance,
Notification_Class_Object_Name,
Notification_Class_Read_Property,
Notification_Class_Write_Property,
Notification_Class_Property_Lists,
NULL /* ReadRangeInfo */ ,
NULL /* Iterator */ ,
NULL /* Value_Lists */ ,
NULL /* COV */ ,
NULL /* COV Clear */ ,
NULL /* Intrinsic Reporting */ },
#endif
{OBJECT_LIFE_SAFETY_POINT,
Life_Safety_Point_Init,
Life_Safety_Point_Count,
Life_Safety_Point_Index_To_Instance,
Life_Safety_Point_Valid_Instance,
Life_Safety_Point_Object_Name,
Life_Safety_Point_Read_Property,
Life_Safety_Point_Write_Property,
Life_Safety_Point_Property_Lists,
NULL /* ReadRangeInfo */ ,
NULL /* Iterator */ ,
NULL /* Value_Lists */ ,
NULL /* COV */ ,
NULL /* COV Clear */ ,
NULL /* Intrinsic Reporting */ },
{OBJECT_LOAD_CONTROL,
Load_Control_Init,
Load_Control_Count,
Load_Control_Index_To_Instance,
Load_Control_Valid_Instance,
Load_Control_Object_Name,
Load_Control_Read_Property,
Load_Control_Write_Property,
Load_Control_Property_Lists,
NULL /* ReadRangeInfo */ ,
NULL /* Iterator */ ,
NULL /* Value_Lists */ ,
NULL /* COV */ ,
NULL /* COV Clear */ ,
NULL /* Intrinsic Reporting */ },
{OBJECT_MULTI_STATE_INPUT,
Multistate_Input_Init,
Multistate_Input_Count,
Multistate_Input_Index_To_Instance,
Multistate_Input_Valid_Instance,
Multistate_Input_Object_Name,
Multistate_Input_Read_Property,
Multistate_Input_Write_Property,
Multistate_Input_Property_Lists,
NULL /* ReadRangeInfo */ ,
NULL /* Iterator */ ,
NULL /* Value_Lists */ ,
NULL /* COV */ ,
NULL /* COV Clear */ ,
NULL /* Intrinsic Reporting */ },
{OBJECT_MULTI_STATE_OUTPUT,
Multistate_Output_Init,
Multistate_Output_Count,
Multistate_Output_Index_To_Instance,
Multistate_Output_Valid_Instance,
Multistate_Output_Object_Name,
Multistate_Output_Read_Property,
Multistate_Output_Write_Property,
Multistate_Output_Property_Lists,
NULL /* ReadRangeInfo */ ,
NULL /* Iterator */ ,
NULL /* Value_Lists */ ,
NULL /* COV */ ,
NULL /* COV Clear */ ,
NULL /* Intrinsic Reporting */ },
{OBJECT_MULTI_STATE_VALUE,
Multistate_Value_Init,
Multistate_Value_Count,
Multistate_Value_Index_To_Instance,
Multistate_Value_Valid_Instance,
Multistate_Value_Object_Name,
Multistate_Value_Read_Property,
Multistate_Value_Write_Property,
Multistate_Value_Property_Lists,
NULL /* ReadRangeInfo */ ,
NULL /* Iterator */ ,
NULL /* Value_Lists */ ,
NULL /* COV */ ,
NULL /* COV Clear */ ,
NULL /* Intrinsic Reporting */ },
{OBJECT_TRENDLOG,
Trend_Log_Init,
Trend_Log_Count,
Trend_Log_Index_To_Instance,
Trend_Log_Valid_Instance,
Trend_Log_Object_Name,
Trend_Log_Read_Property,
Trend_Log_Write_Property,
Trend_Log_Property_Lists,
TrendLogGetRRInfo,
NULL /* Iterator */ ,
NULL /* Value_Lists */ ,
NULL /* COV */ ,
NULL /* COV Clear */ ,
NULL /* Intrinsic Reporting */ },
#if defined(BACFILE)
{OBJECT_FILE,
bacfile_init,
bacfile_count,
bacfile_index_to_instance,
bacfile_valid_instance,
bacfile_object_name,
bacfile_read_property,
bacfile_write_property,
BACfile_Property_Lists,
NULL /* ReadRangeInfo */ ,
NULL /* Iterator */ ,
NULL /* Value_Lists */ ,
NULL /* COV */ ,
NULL /* COV Clear */ ,
NULL /* Intrinsic Reporting */ },
#endif
{OBJECT_OCTETSTRING_VALUE,
OctetString_Value_Init,
OctetString_Value_Count,
OctetString_Value_Index_To_Instance,
OctetString_Value_Valid_Instance,
OctetString_Value_Object_Name,
OctetString_Value_Read_Property,
OctetString_Value_Write_Property,
OctetString_Value_Property_Lists,
NULL /* ReadRangeInfo */ ,
NULL /* Iterator */ ,
NULL /* Value_Lists */ ,
NULL /* COV */ ,
NULL /* COV Clear */ ,
NULL /* Intrinsic Reporting */ },
{OBJECT_POSITIVE_INTEGER_VALUE,
PositiveInteger_Value_Init,
PositiveInteger_Value_Count,
PositiveInteger_Value_Index_To_Instance,
PositiveInteger_Value_Valid_Instance,
PositiveInteger_Value_Object_Name,
PositiveInteger_Value_Read_Property,
PositiveInteger_Value_Write_Property,
PositiveInteger_Value_Property_Lists,
NULL /* ReadRangeInfo */ ,
NULL /* Iterator */ ,
NULL /* Value_Lists */ ,
NULL /* COV */ ,
NULL /* COV Clear */ ,
NULL /* Intrinsic Reporting */ },
{OBJECT_SCHEDULE,
Schedule_Init,
Schedule_Count,
Schedule_Index_To_Instance,
Schedule_Valid_Instance,
Schedule_Object_Name,
Schedule_Read_Property,
Schedule_Write_Property,
Schedule_Property_Lists,
NULL /* ReadRangeInfo */ ,
NULL /* Iterator */ ,
NULL /* Value_Lists */ ,
NULL /* COV */ ,
NULL /* COV Clear */ ,
NULL /* Intrinsic Reporting */ },
{MAX_BACNET_OBJECT_TYPE,
NULL /* Init */ ,
NULL /* Count */ ,
NULL /* Index_To_Instance */ ,
NULL /* Valid_Instance */ ,
NULL /* Object_Name */ ,
NULL /* Read_Property */ ,
NULL /* Write_Property */ ,
NULL /* Property_Lists */ ,
NULL /* ReadRangeInfo */ ,
NULL /* Iterator */ ,
NULL /* Value_Lists */ ,
NULL /* COV */ ,
NULL /* COV Clear */ ,
NULL /* Intrinsic Reporting */ }
};
static struct my_object_functions {
BACNET_OBJECT_TYPE Object_Type;
object_init_function Object_Init;
object_count_function Object_Count;
object_index_to_instance_function Object_Index_To_Instance;
object_valid_instance_function Object_Valid_Instance;
object_name_function Object_Name;
read_property_function Object_Read_Property;
write_property_function Object_Write_P
2224



被折叠的 条评论
为什么被折叠?



