Added demo for BACnet simple server in the demo/server directory.
Added demo for BACnet ReadProperty service. Added demo for BACnet WriteProperty service.
This commit is contained in:
@@ -54,7 +54,7 @@ bool Send_Write_Property_Request(
|
||||
BACNET_OBJECT_TYPE object_type,
|
||||
uint32_t object_instance,
|
||||
BACNET_PROPERTY_ID object_property,
|
||||
BACNET_APPLICATION_DATA_VALUE object_value,
|
||||
BACNET_APPLICATION_DATA_VALUE *object_value,
|
||||
uint8_t priority,
|
||||
int32_t array_index);
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
#include "iam.h"
|
||||
#include "address.h"
|
||||
|
||||
void handler_i_am(
|
||||
void handler_i_am_add(
|
||||
uint8_t *service_request,
|
||||
uint16_t service_len,
|
||||
BACNET_ADDRESS *src)
|
||||
@@ -43,7 +43,6 @@ void handler_i_am(
|
||||
int segmentation = 0;
|
||||
uint16_t vendor_id = 0;
|
||||
|
||||
(void)src;
|
||||
(void)service_len;
|
||||
len = iam_decode_service_request(
|
||||
service_request,
|
||||
@@ -64,3 +63,30 @@ void handler_i_am(
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void handler_i_am_bind(
|
||||
uint8_t *service_request,
|
||||
uint16_t service_len,
|
||||
BACNET_ADDRESS *src)
|
||||
{
|
||||
int len = 0;
|
||||
uint32_t device_id = 0;
|
||||
unsigned max_apdu = 0;
|
||||
int segmentation = 0;
|
||||
uint16_t vendor_id = 0;
|
||||
|
||||
(void)service_len;
|
||||
len = iam_decode_service_request(
|
||||
service_request,
|
||||
&device_id,
|
||||
&max_apdu,
|
||||
&segmentation,
|
||||
&vendor_id);
|
||||
// only add address if requested to bind
|
||||
address_add_binding(device_id,
|
||||
max_apdu,
|
||||
src);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -50,6 +50,16 @@ void handler_who_is(
|
||||
uint16_t service_len,
|
||||
BACNET_ADDRESS *src);
|
||||
|
||||
void handler_i_am_add(
|
||||
uint8_t *service_request,
|
||||
uint16_t service_len,
|
||||
BACNET_ADDRESS *src);
|
||||
|
||||
void handler_i_am_bind(
|
||||
uint8_t *service_request,
|
||||
uint16_t service_len,
|
||||
BACNET_ADDRESS *src);
|
||||
|
||||
void handler_read_property(
|
||||
uint8_t *service_request,
|
||||
uint16_t service_len,
|
||||
|
||||
@@ -41,8 +41,8 @@
|
||||
#include "handlers.h"
|
||||
#include "txbuf.h"
|
||||
|
||||
/* returns false if device is not bound or no tsm available */
|
||||
bool Send_Read_Property_Request(
|
||||
/* returns invoke id of 0 if device is not bound or no tsm available */
|
||||
uint8_t Send_Read_Property_Request(
|
||||
uint32_t device_id, /* destination device */
|
||||
BACNET_OBJECT_TYPE object_type,
|
||||
uint32_t object_instance,
|
||||
@@ -99,9 +99,7 @@ bool Send_Read_Property_Request(
|
||||
&dest, // destination address
|
||||
&Handler_Transmit_Buffer[0],
|
||||
pdu_len); // number of bytes of data
|
||||
if (bytes_sent > 0)
|
||||
fprintf(stderr,"Sent ReadProperty Request!\n");
|
||||
else
|
||||
if (bytes_sent <= 0)
|
||||
fprintf(stderr,"Failed to Send ReadProperty Request (%s)!\n",
|
||||
strerror(errno));
|
||||
}
|
||||
@@ -110,5 +108,5 @@ bool Send_Read_Property_Request(
|
||||
"(exceeds destination maximum APDU)!\n");
|
||||
}
|
||||
|
||||
return status;
|
||||
return invoke_id;
|
||||
}
|
||||
|
||||
@@ -71,8 +71,6 @@ void Send_WhoIs(
|
||||
&dest, // destination address
|
||||
&Handler_Transmit_Buffer[0],
|
||||
pdu_len); // number of bytes of data
|
||||
if (bytes_sent > 0)
|
||||
fprintf(stderr,"Sent Who-Is Request!\n");
|
||||
else
|
||||
if (bytes_sent <= 0)
|
||||
fprintf(stderr,"Failed to Send Who-Is Request (%s)!\n", strerror(errno));
|
||||
}
|
||||
|
||||
@@ -41,13 +41,13 @@
|
||||
#include "handlers.h"
|
||||
#include "txbuf.h"
|
||||
|
||||
/* FIXME: probably should return the invoke ID for confirmed request */
|
||||
bool Send_Write_Property_Request(
|
||||
/* returns the invoke ID for confirmed request, or zero on failure */
|
||||
uint8_t Send_Write_Property_Request(
|
||||
uint32_t device_id, // destination device
|
||||
BACNET_OBJECT_TYPE object_type,
|
||||
uint32_t object_instance,
|
||||
BACNET_PROPERTY_ID object_property,
|
||||
BACNET_APPLICATION_DATA_VALUE object_value,
|
||||
BACNET_APPLICATION_DATA_VALUE *object_value,
|
||||
uint8_t priority,
|
||||
int32_t array_index)
|
||||
{
|
||||
@@ -81,7 +81,7 @@ bool Send_Write_Property_Request(
|
||||
data.object_instance = object_instance;
|
||||
data.object_property = object_property;
|
||||
data.array_index = array_index;
|
||||
data.value = object_value;
|
||||
bacapp_copy(&data.value,object_value);
|
||||
data.priority = priority;
|
||||
pdu_len += wp_encode_apdu(
|
||||
&Handler_Transmit_Buffer[pdu_len],
|
||||
@@ -103,9 +103,7 @@ bool Send_Write_Property_Request(
|
||||
&dest, // destination address
|
||||
&Handler_Transmit_Buffer[0],
|
||||
pdu_len); // number of bytes of data
|
||||
if (bytes_sent > 0)
|
||||
fprintf(stderr,"Sent ReadProperty Request!\n");
|
||||
else
|
||||
if (bytes_sent <= 0)
|
||||
fprintf(stderr,"Failed to Send WriteProperty Request (%s)!\n",
|
||||
strerror(errno));
|
||||
}
|
||||
@@ -114,6 +112,6 @@ bool Send_Write_Property_Request(
|
||||
"(exceeds destination maximum APDU)!\n");
|
||||
}
|
||||
|
||||
return status;
|
||||
return invoke_id;
|
||||
}
|
||||
|
||||
|
||||
@@ -239,6 +239,7 @@ uint32_t bacfile_instance(char *filename)
|
||||
return instance;
|
||||
}
|
||||
|
||||
#if TSM_ENABLED
|
||||
// this is one way to match up the invoke ID with
|
||||
// the file ID from the AtomicReadFile request.
|
||||
// Another way would be to store the
|
||||
@@ -300,6 +301,7 @@ uint32_t bacfile_instance_from_tsm(
|
||||
|
||||
return object_instance;
|
||||
}
|
||||
#endif
|
||||
|
||||
bool bacfile_read_data(BACNET_ATOMIC_READ_FILE_DATA *data)
|
||||
{
|
||||
|
||||
@@ -50,6 +50,7 @@ bool bacfile_valid_instance(uint32_t object_instance);
|
||||
uint32_t bacfile_count(void);
|
||||
uint32_t bacfile_index_to_instance(unsigned find_index);
|
||||
uint32_t bacfile_instance(char *filename);
|
||||
#if TSM_ENABLED
|
||||
// this is one way to match up the invoke ID with
|
||||
// the file ID from the AtomicReadFile request.
|
||||
// Another way would be to store the
|
||||
@@ -57,6 +58,7 @@ uint32_t bacfile_instance(char *filename);
|
||||
// when the request was sent
|
||||
uint32_t bacfile_instance_from_tsm(
|
||||
uint8_t invokeID);
|
||||
#endif
|
||||
|
||||
// AtomicReadFile ACK helper
|
||||
bool bacfile_read_data(BACNET_ATOMIC_READ_FILE_DATA *data);
|
||||
|
||||
@@ -478,7 +478,7 @@ int Device_Encode_Property_APDU(
|
||||
break;
|
||||
case PROP_PROTOCOL_OBJECT_TYPES_SUPPORTED:
|
||||
bitstring_init(&bit_string);
|
||||
for (i = 0; i < MAX_BACNET_OBJECT_TYPES; i++)
|
||||
for (i = 0; i < MAX_ASHRAE_OBJECT_TYPE; i++)
|
||||
{
|
||||
// initialize all the object types to not-supported
|
||||
bitstring_set_bit(&bit_string, (uint8_t)i, false);
|
||||
|
||||
@@ -15,7 +15,7 @@ PRODUCT = bacarf
|
||||
PRODUCT_EXE = $(PRODUCT).exe
|
||||
|
||||
# Choose the Data Link Layer to Enable
|
||||
DEFINES = -DBACDL_BIP=1
|
||||
DEFINES = -DBACDL_BIP=1;TSM_ENABLED=1
|
||||
|
||||
SRCS = readfile.c \
|
||||
..\..\ports\win32\bip-init.c \
|
||||
|
||||
@@ -51,8 +51,8 @@
|
||||
static uint8_t Rx_Buf[MAX_MPDU] = {0};
|
||||
|
||||
/* global variables used in this file */
|
||||
static uint32_t Target_File_Object_Instance = 4194303;
|
||||
static uint32_t Target_Device_Object_Instance = 4194303;
|
||||
static uint32_t Target_File_Object_Instance = BACNET_MAX_INSTANCE;
|
||||
static uint32_t Target_Device_Object_Instance = BACNET_MAX_INSTANCE;
|
||||
static BACNET_ADDRESS Target_Address;
|
||||
static char *Local_File_Name = NULL;
|
||||
static bool End_Of_File_Detected = false;
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
#
|
||||
# Simple makefile to build an executable for Win32
|
||||
#
|
||||
# This makefile assumes Borland bcc32 development environment
|
||||
# on Windows NT/9x/2000/XP
|
||||
#
|
||||
|
||||
!ifndef BORLAND_DIR
|
||||
BORLAND_DIR_Not_Defined:
|
||||
@echo .
|
||||
@echo You must define environment variable BORLAND_DIR to compile.
|
||||
!endif
|
||||
|
||||
PRODUCT = bacrp
|
||||
PRODUCT_EXE = $(PRODUCT).exe
|
||||
|
||||
# Choose the Data Link Layer to Enable
|
||||
DEFINES = -DBACDL_BIP=1;TSM_ENABLED=1
|
||||
|
||||
SRCS = readprop.c \
|
||||
..\..\ports\win32\bip-init.c \
|
||||
..\..\filename.c \
|
||||
..\..\bip.c \
|
||||
..\..\demo\handler\txbuf.c \
|
||||
..\..\demo\handler\noserv.c \
|
||||
..\..\demo\handler\h_whois.c \
|
||||
..\..\demo\handler\h_iam.c \
|
||||
..\..\demo\handler\h_rp.c \
|
||||
..\..\demo\handler\h_rp_a.c \
|
||||
..\..\demo\handler\s_rp.c \
|
||||
..\..\demo\handler\s_whois.c \
|
||||
..\..\bacdcode.c \
|
||||
..\..\bacapp.c \
|
||||
..\..\bacstr.c \
|
||||
..\..\bactext.c \
|
||||
..\..\indtext.c \
|
||||
..\..\bigend.c \
|
||||
..\..\whois.c \
|
||||
..\..\iam.c \
|
||||
..\..\rp.c \
|
||||
..\..\wp.c \
|
||||
..\..\arf.c \
|
||||
..\..\awf.c \
|
||||
..\..\demo\object\bacfile.c \
|
||||
..\..\demo\object\device.c \
|
||||
..\..\demo\object\ai.c \
|
||||
..\..\demo\object\ao.c \
|
||||
..\..\datalink.c \
|
||||
..\..\tsm.c \
|
||||
..\..\address.c \
|
||||
..\..\abort.c \
|
||||
..\..\reject.c \
|
||||
..\..\bacerror.c \
|
||||
..\..\apdu.c \
|
||||
..\..\npdu.c
|
||||
|
||||
OBJS = $(SRCS:.c=.obj)
|
||||
|
||||
# Compiler definitions
|
||||
#
|
||||
CC = $(BORLAND_DIR)\bin\bcc32 +bcc32.cfg
|
||||
#LINK = $(BORLAND_DIR)\bin\tlink32
|
||||
LINK = $(BORLAND_DIR)\bin\ilink32
|
||||
TLIB = $(BORLAND_DIR)\bin\tlib
|
||||
|
||||
#
|
||||
# Include directories
|
||||
#
|
||||
CC_DIR = $(BORLAND_DIR)\BIN
|
||||
INCL_DIRS = -I$(BORLAND_DIR)\include;..\..\;..\..\demo\object\;..\..\demo\handler\;..\..\ports\win32\;.
|
||||
|
||||
CFLAGS = $(INCL_DIRS) $(CS_FLAGS) $(DEFINES)
|
||||
|
||||
# Libraries
|
||||
#
|
||||
C_LIB_DIR = $(BORLAND_DIR)\lib
|
||||
|
||||
LIBS = $(C_LIB_DIR)\IMPORT32.lib \
|
||||
$(C_LIB_DIR)\CW32MT.lib
|
||||
|
||||
#
|
||||
# Main target
|
||||
#
|
||||
# This should be the first one in the makefile
|
||||
|
||||
all : bcc32.cfg $(PRODUCT_EXE)
|
||||
|
||||
# Linker specific: the link below is for BCC linker/compiler. If you link
|
||||
# with a different linker - please change accordingly.
|
||||
#
|
||||
|
||||
# need a temp response file (@&&) because command line is too long
|
||||
$(PRODUCT_EXE) : $(OBJS)
|
||||
@echo Running Linker for $(PRODUCT_EXE)
|
||||
$(LINK) -L$(C_LIB_DIR) -m -c -s -v @&&| # temp response file, starts with |
|
||||
$(BORLAND_DIR)\lib\c0x32.obj $** # $** lists each dependency
|
||||
$<
|
||||
$*.map
|
||||
$(LIBS)
|
||||
| # end of temp response file
|
||||
|
||||
#
|
||||
# Utilities
|
||||
|
||||
clean :
|
||||
@echo Deleting obj files, $(PRODUCT_EXE) and map files.
|
||||
# del $(OBJS) # command too long, bummer!
|
||||
del *.obj
|
||||
del ..\..\*.obj
|
||||
del ..\..\demo\handler\*.obj
|
||||
del ..\..\demo\object\*.obj
|
||||
del ..\..\ports\win32\*.obj
|
||||
del $(PRODUCT_EXE)
|
||||
del *.map
|
||||
del bcc32.cfg
|
||||
|
||||
#
|
||||
# Generic rules
|
||||
#
|
||||
.SUFFIXES: .cpp .c .sbr .obj
|
||||
|
||||
#
|
||||
# cc generic rule
|
||||
#
|
||||
.c.obj:
|
||||
$(CC) -o$@ $<
|
||||
|
||||
# Compiler configuration file
|
||||
bcc32.cfg :
|
||||
Copy &&|
|
||||
$(CFLAGS)
|
||||
-c
|
||||
-y #include line numbers in OBJ's
|
||||
-v #include debug info
|
||||
-w+ #turn on all warnings
|
||||
-Od #disable all optimizations
|
||||
#-a4 #32 bit data alignment
|
||||
#-M # generate link map
|
||||
#-ls # linker options
|
||||
#-WM- #not multithread
|
||||
-WM #multithread
|
||||
-w-aus # ignore warning assigned a value that is never used
|
||||
-w-sig # ignore warning conversion may lose sig digits
|
||||
| $@
|
||||
|
||||
# EOF: makefile
|
||||
@@ -0,0 +1,275 @@
|
||||
/**************************************************************************
|
||||
*
|
||||
* Copyright (C) 2006 Steve Karg <skarg@users.sourceforge.net>
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
*********************************************************************/
|
||||
|
||||
/* READPROP: command line tool that reads a property from a BACnet device. */
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h> /* for time */
|
||||
#include <errno.h>
|
||||
#include "bactext.h"
|
||||
#include "iam.h"
|
||||
#include "arf.h"
|
||||
#include "tsm.h"
|
||||
#include "address.h"
|
||||
#include "config.h"
|
||||
#include "bacdef.h"
|
||||
#include "npdu.h"
|
||||
#include "apdu.h"
|
||||
#include "device.h"
|
||||
#include "net.h"
|
||||
#include "datalink.h"
|
||||
#include "whois.h"
|
||||
/* some demo stuff needed */
|
||||
#include "filename.h"
|
||||
#include "handlers.h"
|
||||
#include "client.h"
|
||||
#include "txbuf.h"
|
||||
|
||||
// buffer used for receive
|
||||
static uint8_t Rx_Buf[MAX_MPDU] = {0};
|
||||
|
||||
/* global variables used in this file */
|
||||
static uint32_t Target_Device_Object_Instance = BACNET_MAX_INSTANCE;
|
||||
static uint32_t Target_Object_Instance = BACNET_MAX_INSTANCE;
|
||||
static BACNET_OBJECT_TYPE Target_Object_Type = OBJECT_ANALOG_INPUT;
|
||||
static BACNET_PROPERTY_ID Target_Object_Property = PROP_ACKED_TRANSITIONS;
|
||||
static int32_t Target_Object_Index = BACNET_ARRAY_ALL;
|
||||
|
||||
static BACNET_ADDRESS Target_Address;
|
||||
static bool Error_Detected = false;
|
||||
|
||||
static void MyErrorHandler(
|
||||
BACNET_ADDRESS *src,
|
||||
uint8_t invoke_id,
|
||||
BACNET_ERROR_CLASS error_class,
|
||||
BACNET_ERROR_CODE error_code)
|
||||
{
|
||||
/* FIXME: verify src and invoke id */
|
||||
(void)src;
|
||||
(void)invoke_id;
|
||||
printf("\r\nBACnet Error!\r\n");
|
||||
printf("Error Class: %s\r\n",
|
||||
bactext_error_class_name(error_class));
|
||||
printf("Error Code: %s\r\n",
|
||||
bactext_error_code_name(error_code));
|
||||
Error_Detected = true;
|
||||
}
|
||||
|
||||
void MyAbortHandler(
|
||||
BACNET_ADDRESS *src,
|
||||
uint8_t invoke_id,
|
||||
uint8_t abort_reason)
|
||||
{
|
||||
/* FIXME: verify src and invoke id */
|
||||
(void)src;
|
||||
(void)invoke_id;
|
||||
printf("\r\nBACnet Abort!\r\n");
|
||||
printf("Abort Reason: %s\r\n",
|
||||
bactext_abort_reason_name(abort_reason));
|
||||
Error_Detected = true;
|
||||
}
|
||||
|
||||
void MyRejectHandler(
|
||||
BACNET_ADDRESS *src,
|
||||
uint8_t invoke_id,
|
||||
uint8_t reject_reason)
|
||||
{
|
||||
/* FIXME: verify src and invoke id */
|
||||
(void)src;
|
||||
(void)invoke_id;
|
||||
printf("\r\nBACnet Reject!\r\n");
|
||||
printf("Reject Reason: %s\r\n",
|
||||
bactext_reject_reason_name(reject_reason));
|
||||
Error_Detected = true;
|
||||
}
|
||||
|
||||
static void Init_Service_Handlers(void)
|
||||
{
|
||||
/* we need to handle who-is
|
||||
to support dynamic device binding to us */
|
||||
apdu_set_unconfirmed_handler(
|
||||
SERVICE_UNCONFIRMED_WHO_IS,
|
||||
handler_who_is);
|
||||
/* handle i-am to support binding to other devices */
|
||||
apdu_set_unconfirmed_handler(
|
||||
SERVICE_UNCONFIRMED_I_AM,
|
||||
handler_i_am_bind);
|
||||
/* set the handler for all the services we don't implement
|
||||
It is required to send the proper reject message... */
|
||||
apdu_set_unrecognized_service_handler_handler(
|
||||
handler_unrecognized_service);
|
||||
/* we must implement read property - it's required! */
|
||||
apdu_set_confirmed_handler(
|
||||
SERVICE_CONFIRMED_READ_PROPERTY,
|
||||
handler_read_property);
|
||||
/* handle the data coming back from confirmed requests */
|
||||
apdu_set_confirmed_ack_handler(
|
||||
SERVICE_CONFIRMED_READ_PROPERTY,
|
||||
handler_read_property_ack);
|
||||
/* handle any errors coming back */
|
||||
apdu_set_error_handler(
|
||||
SERVICE_CONFIRMED_READ_PROPERTY,
|
||||
MyErrorHandler);
|
||||
apdu_set_abort_handler(
|
||||
MyAbortHandler);
|
||||
apdu_set_reject_handler(
|
||||
MyRejectHandler);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
BACNET_ADDRESS src = {0}; // address where message came from
|
||||
uint16_t pdu_len = 0;
|
||||
unsigned timeout = 100; // milliseconds
|
||||
unsigned max_apdu = 0;
|
||||
time_t elapsed_seconds = 0;
|
||||
time_t last_seconds = 0;
|
||||
time_t current_seconds = 0;
|
||||
time_t timeout_seconds = 0;
|
||||
int fileStartPosition = 0;
|
||||
uint8_t invoke_id = 0;
|
||||
bool found = false;
|
||||
|
||||
if (argc < 5)
|
||||
{
|
||||
printf("%s device-instance object-type object-instance property [index]\r\n",
|
||||
filename_remove_path(argv[0]));
|
||||
return 0;
|
||||
}
|
||||
/* decode the command line parameters */
|
||||
Target_Device_Object_Instance = strtol(argv[1],NULL,0);
|
||||
Target_Object_Type = strtol(argv[2],NULL,0);
|
||||
Target_Object_Instance = strtol(argv[3],NULL,0);
|
||||
Target_Object_Property = strtol(argv[4],NULL,0);
|
||||
if (argc > 5)
|
||||
Target_Object_Index = strtol(argv[5],NULL,0);
|
||||
if (Target_Device_Object_Instance >= BACNET_MAX_INSTANCE)
|
||||
{
|
||||
fprintf(stderr,"device-instance=%u - it must be less than %u\r\n",
|
||||
Target_Device_Object_Instance,BACNET_MAX_INSTANCE);
|
||||
return 1;
|
||||
}
|
||||
if (Target_Object_Type > MAX_BACNET_OBJECT_TYPE)
|
||||
{
|
||||
fprintf(stderr,"object-type=%u - it must be less than %u\r\n",
|
||||
Target_Object_Type,MAX_BACNET_OBJECT_TYPE+1);
|
||||
return 1;
|
||||
}
|
||||
if (Target_Object_Instance > BACNET_MAX_INSTANCE)
|
||||
{
|
||||
fprintf(stderr,"object-instance=%u - it must be less than %u\r\n",
|
||||
Target_Object_Instance,BACNET_MAX_INSTANCE+1);
|
||||
return 1;
|
||||
}
|
||||
if (Target_Object_Property > MAX_BACNET_PROPERTY_ID)
|
||||
{
|
||||
fprintf(stderr,"object-type=%u - it must be less than %u\r\n",
|
||||
Target_Object_Property,MAX_BACNET_PROPERTY_ID+1);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* setup my info */
|
||||
Device_Set_Object_Instance_Number(BACNET_MAX_INSTANCE);
|
||||
address_init();
|
||||
Init_Service_Handlers();
|
||||
/* configure standard BACnet/IP port */
|
||||
bip_set_interface("eth0"); /* for linux */
|
||||
bip_set_port(0xBAC0);
|
||||
if (!bip_init())
|
||||
return 1;
|
||||
/* configure the timeout values */
|
||||
last_seconds = time(NULL);
|
||||
timeout_seconds = (Device_APDU_Timeout() / 1000) *
|
||||
Device_Number_Of_APDU_Retries();
|
||||
/* try to bind with the device */
|
||||
Send_WhoIs(Target_Device_Object_Instance,Target_Device_Object_Instance);
|
||||
/* loop forever */
|
||||
for (;;)
|
||||
{
|
||||
/* increment timer - exit if timed out */
|
||||
current_seconds = time(NULL);
|
||||
|
||||
/* returns 0 bytes on timeout */
|
||||
pdu_len = bip_receive(
|
||||
&src,
|
||||
&Rx_Buf[0],
|
||||
MAX_MPDU,
|
||||
timeout);
|
||||
|
||||
/* process */
|
||||
if (pdu_len)
|
||||
{
|
||||
npdu_handler(
|
||||
&src,
|
||||
&Rx_Buf[0],
|
||||
pdu_len);
|
||||
}
|
||||
/* at least one second has passed */
|
||||
if (current_seconds != last_seconds)
|
||||
tsm_timer_milliseconds(((current_seconds - last_seconds) * 1000));
|
||||
if (Error_Detected)
|
||||
break;
|
||||
if (I_Am_Request)
|
||||
{
|
||||
I_Am_Request = false;
|
||||
iam_send(&Handler_Transmit_Buffer[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* wait until the device is bound, or timeout and quit */
|
||||
found = address_bind_request(
|
||||
Target_Device_Object_Instance,
|
||||
&max_apdu,
|
||||
&Target_Address);
|
||||
if (found)
|
||||
{
|
||||
if (invoke_id == 0)
|
||||
{
|
||||
invoke_id = Send_Read_Property_Request(
|
||||
Target_Device_Object_Instance,
|
||||
Target_Object_Type,
|
||||
Target_Object_Instance,
|
||||
Target_Object_Property,
|
||||
Target_Object_Index);
|
||||
}
|
||||
else if (tsm_invoke_id_free(invoke_id))
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* increment timer - exit if timed out */
|
||||
elapsed_seconds += (current_seconds - last_seconds);
|
||||
if (elapsed_seconds > timeout_seconds)
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* keep track of time for next check */
|
||||
last_seconds = current_seconds;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
#
|
||||
# Simple makefile to build an executable for Win32
|
||||
#
|
||||
# This makefile assumes Borland bcc32 development environment
|
||||
# on Windows NT/9x/2000/XP
|
||||
#
|
||||
|
||||
!ifndef BORLAND_DIR
|
||||
BORLAND_DIR_Not_Defined:
|
||||
@echo .
|
||||
@echo You must define environment variable BORLAND_DIR to compile.
|
||||
!endif
|
||||
|
||||
PRODUCT = bacserv
|
||||
PRODUCT_EXE = $(PRODUCT).exe
|
||||
|
||||
# Choose the Data Link Layer to Enable
|
||||
# Note: unless some other drivers are installed, BIP is the only
|
||||
# datalink layer that Win32 supports
|
||||
DEFINES = -DBACDL_BIP=1;USE_INADDR=1
|
||||
|
||||
SRCS = server.c \
|
||||
..\..\ports\win32\bip-init.c \
|
||||
..\..\bip.c \
|
||||
..\..\demo\handler\txbuf.c \
|
||||
..\..\demo\handler\noserv.c \
|
||||
..\..\demo\handler\h_whois.c \
|
||||
..\..\demo\handler\h_rp.c \
|
||||
..\..\demo\handler\h_wp.c \
|
||||
..\..\demo\handler\h_arf.c \
|
||||
..\..\bacdcode.c \
|
||||
..\..\bacapp.c \
|
||||
..\..\bacstr.c \
|
||||
..\..\bactext.c \
|
||||
..\..\indtext.c \
|
||||
..\..\bigend.c \
|
||||
..\..\whois.c \
|
||||
..\..\iam.c \
|
||||
..\..\rp.c \
|
||||
..\..\wp.c \
|
||||
..\..\arf.c \
|
||||
..\..\awf.c \
|
||||
..\..\demo\object\bacfile.c \
|
||||
..\..\demo\object\device.c \
|
||||
..\..\demo\object\ai.c \
|
||||
..\..\demo\object\ao.c \
|
||||
..\..\datalink.c \
|
||||
..\..\abort.c \
|
||||
..\..\reject.c \
|
||||
..\..\bacerror.c \
|
||||
..\..\apdu.c \
|
||||
..\..\npdu.c
|
||||
|
||||
OBJS = $(SRCS:.c=.obj)
|
||||
|
||||
# Compiler definitions
|
||||
#
|
||||
CC = $(BORLAND_DIR)\bin\bcc32 +bcc32.cfg
|
||||
#LINK = $(BORLAND_DIR)\bin\tlink32
|
||||
LINK = $(BORLAND_DIR)\bin\ilink32
|
||||
TLIB = $(BORLAND_DIR)\bin\tlib
|
||||
|
||||
#
|
||||
# Include directories
|
||||
#
|
||||
CC_DIR = $(BORLAND_DIR)\BIN
|
||||
INCL_DIRS = -I$(BORLAND_DIR)\include;..\..\;..\..\demo\object\;..\..\demo\handler\;..\..\ports\win32\;.
|
||||
|
||||
CFLAGS = $(INCL_DIRS) $(CS_FLAGS) $(DEFINES)
|
||||
|
||||
# Libraries
|
||||
#
|
||||
C_LIB_DIR = $(BORLAND_DIR)\lib
|
||||
|
||||
LIBS = $(C_LIB_DIR)\IMPORT32.lib \
|
||||
$(C_LIB_DIR)\CW32MT.lib
|
||||
|
||||
#
|
||||
# Main target
|
||||
#
|
||||
# This should be the first one in the makefile
|
||||
|
||||
all : bcc32.cfg $(PRODUCT_EXE)
|
||||
|
||||
# Linker specific: the link below is for BCC linker/compiler. If you link
|
||||
# with a different linker - please change accordingly.
|
||||
#
|
||||
|
||||
# need a temp response file (@&&) because command line is too long
|
||||
$(PRODUCT_EXE) : $(OBJS)
|
||||
@echo Running Linker for $(PRODUCT_EXE)
|
||||
$(LINK) -L$(C_LIB_DIR) -m -c -s -v @&&| # temp response file, starts with |
|
||||
$(BORLAND_DIR)\lib\c0x32.obj $** # $** lists each dependency
|
||||
$<
|
||||
$*.map
|
||||
$(LIBS)
|
||||
| # end of temp response file
|
||||
|
||||
#
|
||||
# Utilities
|
||||
|
||||
clean :
|
||||
@echo Deleting obj files, $(PRODUCT_EXE) and map files.
|
||||
# del $(OBJS) # command too long, bummer!
|
||||
del *.obj
|
||||
del ..\..\*.obj
|
||||
del ..\..\demo\handler\*.obj
|
||||
del ..\..\demo\object\*.obj
|
||||
del ..\..\ports\win32\*.obj
|
||||
del $(PRODUCT_EXE)
|
||||
del *.map
|
||||
del bcc32.cfg
|
||||
|
||||
#
|
||||
# Generic rules
|
||||
#
|
||||
.SUFFIXES: .cpp .c .sbr .obj
|
||||
|
||||
#
|
||||
# cc generic rule
|
||||
#
|
||||
.c.obj:
|
||||
$(CC) -o$@ $<
|
||||
|
||||
# Compiler configuration file
|
||||
bcc32.cfg :
|
||||
Copy &&|
|
||||
$(CFLAGS)
|
||||
-c
|
||||
-y #include line numbers in OBJ's
|
||||
-v #include debug info
|
||||
-w+ #turn on all warnings
|
||||
-Od #disable all optimizations
|
||||
#-a4 #32 bit data alignment
|
||||
#-M # generate link map
|
||||
#-ls # linker options
|
||||
#-WM- #not multithread
|
||||
-WM #multithread
|
||||
-w-aus # ignore warning assigned a value that is never used
|
||||
-w-sig # ignore warning conversion may lose sig digits
|
||||
| $@
|
||||
|
||||
# EOF: makefile
|
||||
@@ -0,0 +1,165 @@
|
||||
/**************************************************************************
|
||||
*
|
||||
* Copyright (C) 2006 Steve Karg <skarg@users.sourceforge.net>
|
||||
*
|
||||
* 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 <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
#include <time.h>
|
||||
#include "config.h"
|
||||
#include "address.h"
|
||||
#include "bacdef.h"
|
||||
#include "handlers.h"
|
||||
#include "client.h"
|
||||
#include "bacdcode.h"
|
||||
#include "npdu.h"
|
||||
#include "apdu.h"
|
||||
#include "iam.h"
|
||||
#include "tsm.h"
|
||||
#include "device.h"
|
||||
#include "bacfile.h"
|
||||
#include "datalink.h"
|
||||
#include "net.h"
|
||||
#include "txbuf.h"
|
||||
|
||||
/* This is an example application using the BACnet Stack */
|
||||
|
||||
/* buffers used for receiving */
|
||||
static uint8_t Rx_Buf[MAX_MPDU] = {0};
|
||||
|
||||
static void Init_Service_Handlers(void)
|
||||
{
|
||||
/* we need to handle who-is to support dynamic device binding */
|
||||
apdu_set_unconfirmed_handler(
|
||||
SERVICE_UNCONFIRMED_WHO_IS,
|
||||
handler_who_is);
|
||||
|
||||
/* set the handler for all the services we don't implement */
|
||||
/* It is required to send the proper reject message... */
|
||||
apdu_set_unrecognized_service_handler_handler(
|
||||
handler_unrecognized_service);
|
||||
/* Set the handlers for any confirmed services that we support. */
|
||||
/* We must implement read property - it's required! */
|
||||
apdu_set_confirmed_handler(
|
||||
SERVICE_CONFIRMED_READ_PROPERTY,
|
||||
handler_read_property);
|
||||
apdu_set_confirmed_handler(
|
||||
SERVICE_CONFIRMED_WRITE_PROPERTY,
|
||||
handler_write_property);
|
||||
apdu_set_confirmed_handler(
|
||||
SERVICE_CONFIRMED_ATOMIC_READ_FILE,
|
||||
handler_atomic_read_file);
|
||||
}
|
||||
|
||||
static void cleanup(void)
|
||||
{
|
||||
datalink_cleanup();
|
||||
}
|
||||
|
||||
static void print_address(
|
||||
char *name,
|
||||
BACNET_ADDRESS *dest) // destination address
|
||||
{
|
||||
int i = 0; // counter
|
||||
|
||||
if (dest)
|
||||
{
|
||||
printf("%s: ",name);
|
||||
for (i = 0; i < dest->mac_len; i++)
|
||||
{
|
||||
printf("%02X",dest->mac[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
BACNET_ADDRESS src = {0}; // address where message came from
|
||||
uint16_t pdu_len = 0;
|
||||
unsigned timeout = 100; // milliseconds
|
||||
BACNET_ADDRESS my_address, broadcast_address;
|
||||
|
||||
/* allow the device ID to be set */
|
||||
if (argc > 1)
|
||||
Device_Set_Object_Instance_Number(strtol(argv[1],NULL,0));
|
||||
if (argc > 2)
|
||||
bip_set_port(strtol(argv[2],NULL,0));
|
||||
printf("BACnet Server Demo - Device #%lu\r\n",
|
||||
Device_Object_Instance_Number());
|
||||
Init_Service_Handlers();
|
||||
#ifdef BACDL_ETHERNET
|
||||
// init the physical layer
|
||||
if (!ethernet_init("eth0"))
|
||||
return 1;
|
||||
#endif
|
||||
#ifdef BACDL_BIP
|
||||
bip_set_interface("eth0");
|
||||
if (!bip_init())
|
||||
return 1;
|
||||
printf("bip: using port %hu\r\n",bip_get_port());
|
||||
#endif
|
||||
#ifdef BACDL_ARCNET
|
||||
if (!arcnet_init("arc0"))
|
||||
return 1;
|
||||
#endif
|
||||
datalink_get_broadcast_address(&broadcast_address);
|
||||
print_address("Broadcast",&broadcast_address);
|
||||
datalink_get_my_address(&my_address);
|
||||
print_address("Address",&my_address);
|
||||
atexit(cleanup);
|
||||
|
||||
/* broadcast an I-Am on startup */
|
||||
I_Am_Request = true;
|
||||
// loop forever
|
||||
for (;;)
|
||||
{
|
||||
// input
|
||||
|
||||
// returns 0 bytes on timeout
|
||||
pdu_len = datalink_receive(
|
||||
&src,
|
||||
&Rx_Buf[0],
|
||||
MAX_MPDU,
|
||||
timeout);
|
||||
|
||||
// process
|
||||
if (pdu_len)
|
||||
{
|
||||
npdu_handler(
|
||||
&src,
|
||||
&Rx_Buf[0],
|
||||
pdu_len);
|
||||
}
|
||||
if (I_Am_Request)
|
||||
{
|
||||
I_Am_Request = false;
|
||||
iam_send(&Handler_Transmit_Buffer[0]);
|
||||
}
|
||||
// output
|
||||
|
||||
// blink LEDs, Turn on or off outputs, etc
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
#
|
||||
# Simple makefile to build an executable for Win32
|
||||
#
|
||||
# This makefile assumes Borland bcc32 development environment
|
||||
# on Windows NT/9x/2000/XP
|
||||
#
|
||||
|
||||
!ifndef BORLAND_DIR
|
||||
BORLAND_DIR_Not_Defined:
|
||||
@echo .
|
||||
@echo You must define environment variable BORLAND_DIR to compile.
|
||||
!endif
|
||||
|
||||
PRODUCT = bacwp
|
||||
PRODUCT_EXE = $(PRODUCT).exe
|
||||
|
||||
# Choose the Data Link Layer to Enable
|
||||
DEFINES = -DBACDL_BIP=1;TSM_ENABLED=1
|
||||
|
||||
SRCS = writeprop.c \
|
||||
..\..\ports\win32\bip-init.c \
|
||||
..\..\filename.c \
|
||||
..\..\bip.c \
|
||||
..\..\demo\handler\txbuf.c \
|
||||
..\..\demo\handler\noserv.c \
|
||||
..\..\demo\handler\h_whois.c \
|
||||
..\..\demo\handler\h_iam.c \
|
||||
..\..\demo\handler\h_rp.c \
|
||||
..\..\demo\handler\s_wp.c \
|
||||
..\..\demo\handler\s_whois.c \
|
||||
..\..\bacdcode.c \
|
||||
..\..\bacapp.c \
|
||||
..\..\bacstr.c \
|
||||
..\..\bactext.c \
|
||||
..\..\indtext.c \
|
||||
..\..\bigend.c \
|
||||
..\..\whois.c \
|
||||
..\..\iam.c \
|
||||
..\..\rp.c \
|
||||
..\..\wp.c \
|
||||
..\..\arf.c \
|
||||
..\..\awf.c \
|
||||
..\..\demo\object\bacfile.c \
|
||||
..\..\demo\object\device.c \
|
||||
..\..\demo\object\ai.c \
|
||||
..\..\demo\object\ao.c \
|
||||
..\..\datalink.c \
|
||||
..\..\tsm.c \
|
||||
..\..\address.c \
|
||||
..\..\abort.c \
|
||||
..\..\reject.c \
|
||||
..\..\bacerror.c \
|
||||
..\..\apdu.c \
|
||||
..\..\npdu.c
|
||||
|
||||
OBJS = $(SRCS:.c=.obj)
|
||||
|
||||
# Compiler definitions
|
||||
#
|
||||
CC = $(BORLAND_DIR)\bin\bcc32 +bcc32.cfg
|
||||
#LINK = $(BORLAND_DIR)\bin\tlink32
|
||||
LINK = $(BORLAND_DIR)\bin\ilink32
|
||||
TLIB = $(BORLAND_DIR)\bin\tlib
|
||||
|
||||
#
|
||||
# Include directories
|
||||
#
|
||||
CC_DIR = $(BORLAND_DIR)\BIN
|
||||
INCL_DIRS = -I$(BORLAND_DIR)\include;..\..\;..\..\demo\object\;..\..\demo\handler\;..\..\ports\win32\;.
|
||||
|
||||
CFLAGS = $(INCL_DIRS) $(CS_FLAGS) $(DEFINES)
|
||||
|
||||
# Libraries
|
||||
#
|
||||
C_LIB_DIR = $(BORLAND_DIR)\lib
|
||||
|
||||
LIBS = $(C_LIB_DIR)\IMPORT32.lib \
|
||||
$(C_LIB_DIR)\CW32MT.lib
|
||||
|
||||
#
|
||||
# Main target
|
||||
#
|
||||
# This should be the first one in the makefile
|
||||
|
||||
all : bcc32.cfg $(PRODUCT_EXE)
|
||||
|
||||
# Linker specific: the link below is for BCC linker/compiler. If you link
|
||||
# with a different linker - please change accordingly.
|
||||
#
|
||||
|
||||
# need a temp response file (@&&) because command line is too long
|
||||
$(PRODUCT_EXE) : $(OBJS)
|
||||
@echo Running Linker for $(PRODUCT_EXE)
|
||||
$(LINK) -L$(C_LIB_DIR) -m -c -s -v @&&| # temp response file, starts with |
|
||||
$(BORLAND_DIR)\lib\c0x32.obj $** # $** lists each dependency
|
||||
$<
|
||||
$*.map
|
||||
$(LIBS)
|
||||
| # end of temp response file
|
||||
|
||||
#
|
||||
# Utilities
|
||||
|
||||
clean :
|
||||
@echo Deleting obj files, $(PRODUCT_EXE) and map files.
|
||||
# del $(OBJS) # command too long, bummer!
|
||||
del *.obj
|
||||
del ..\..\*.obj
|
||||
del ..\..\demo\handler\*.obj
|
||||
del ..\..\demo\object\*.obj
|
||||
del ..\..\ports\win32\*.obj
|
||||
del $(PRODUCT_EXE)
|
||||
del *.map
|
||||
del bcc32.cfg
|
||||
|
||||
#
|
||||
# Generic rules
|
||||
#
|
||||
.SUFFIXES: .cpp .c .sbr .obj
|
||||
|
||||
#
|
||||
# cc generic rule
|
||||
#
|
||||
.c.obj:
|
||||
$(CC) -o$@ $<
|
||||
|
||||
# Compiler configuration file
|
||||
bcc32.cfg :
|
||||
Copy &&|
|
||||
$(CFLAGS)
|
||||
-c
|
||||
-y #include line numbers in OBJ's
|
||||
-v #include debug info
|
||||
-w+ #turn on all warnings
|
||||
-Od #disable all optimizations
|
||||
#-a4 #32 bit data alignment
|
||||
#-M # generate link map
|
||||
#-ls # linker options
|
||||
#-WM- #not multithread
|
||||
-WM #multithread
|
||||
-w-aus # ignore warning assigned a value that is never used
|
||||
-w-sig # ignore warning conversion may lose sig digits
|
||||
| $@
|
||||
|
||||
# EOF: makefile
|
||||
@@ -0,0 +1,312 @@
|
||||
/**************************************************************************
|
||||
*
|
||||
* Copyright (C) 2006 Steve Karg <skarg@users.sourceforge.net>
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
*********************************************************************/
|
||||
|
||||
/* WRITEPROP: command line tool that writes a property to a BACnet device. */
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h> /* for time */
|
||||
#include <errno.h>
|
||||
#include "bactext.h"
|
||||
#include "iam.h"
|
||||
#include "arf.h"
|
||||
#include "tsm.h"
|
||||
#include "address.h"
|
||||
#include "config.h"
|
||||
#include "bacdef.h"
|
||||
#include "npdu.h"
|
||||
#include "apdu.h"
|
||||
#include "device.h"
|
||||
#include "net.h"
|
||||
#include "datalink.h"
|
||||
#include "whois.h"
|
||||
/* some demo stuff needed */
|
||||
#include "filename.h"
|
||||
#include "handlers.h"
|
||||
#include "client.h"
|
||||
#include "txbuf.h"
|
||||
|
||||
// buffer used for receive
|
||||
static uint8_t Rx_Buf[MAX_MPDU] = {0};
|
||||
|
||||
/* global variables used in this file */
|
||||
static uint32_t Target_Device_Object_Instance = BACNET_MAX_INSTANCE;
|
||||
static uint32_t Target_Object_Instance = BACNET_MAX_INSTANCE;
|
||||
static BACNET_OBJECT_TYPE Target_Object_Type = OBJECT_ANALOG_INPUT;
|
||||
static BACNET_PROPERTY_ID Target_Object_Property = PROP_ACKED_TRANSITIONS;
|
||||
static int32_t Target_Object_Property_Index = BACNET_ARRAY_ALL;
|
||||
static BACNET_APPLICATION_TAG Target_Object_Property_Tag = BACNET_APPLICATION_TAG_NULL;
|
||||
static BACNET_APPLICATION_DATA_VALUE Target_Object_Property_Value = {0};
|
||||
|
||||
static BACNET_ADDRESS Target_Address;
|
||||
static bool Error_Detected = false;
|
||||
|
||||
static void MyErrorHandler(
|
||||
BACNET_ADDRESS *src,
|
||||
uint8_t invoke_id,
|
||||
BACNET_ERROR_CLASS error_class,
|
||||
BACNET_ERROR_CODE error_code)
|
||||
{
|
||||
/* FIXME: verify src and invoke id */
|
||||
(void)src;
|
||||
(void)invoke_id;
|
||||
printf("\r\nBACnet Error!\r\n");
|
||||
printf("Error Class: %s\r\n",
|
||||
bactext_error_class_name(error_class));
|
||||
printf("Error Code: %s\r\n",
|
||||
bactext_error_code_name(error_code));
|
||||
Error_Detected = true;
|
||||
}
|
||||
|
||||
void MyAbortHandler(
|
||||
BACNET_ADDRESS *src,
|
||||
uint8_t invoke_id,
|
||||
uint8_t abort_reason)
|
||||
{
|
||||
/* FIXME: verify src and invoke id */
|
||||
(void)src;
|
||||
(void)invoke_id;
|
||||
printf("\r\nBACnet Abort!\r\n");
|
||||
printf("Abort Reason: %s\r\n",
|
||||
bactext_abort_reason_name(abort_reason));
|
||||
Error_Detected = true;
|
||||
}
|
||||
|
||||
void MyRejectHandler(
|
||||
BACNET_ADDRESS *src,
|
||||
uint8_t invoke_id,
|
||||
uint8_t reject_reason)
|
||||
{
|
||||
/* FIXME: verify src and invoke id */
|
||||
(void)src;
|
||||
(void)invoke_id;
|
||||
printf("\r\nBACnet Reject!\r\n");
|
||||
printf("Reject Reason: %s\r\n",
|
||||
bactext_reject_reason_name(reject_reason));
|
||||
Error_Detected = true;
|
||||
}
|
||||
|
||||
static void Init_Service_Handlers(void)
|
||||
{
|
||||
/* we need to handle who-is
|
||||
to support dynamic device binding to us */
|
||||
apdu_set_unconfirmed_handler(
|
||||
SERVICE_UNCONFIRMED_WHO_IS,
|
||||
handler_who_is);
|
||||
/* handle i-am to support binding to other devices */
|
||||
apdu_set_unconfirmed_handler(
|
||||
SERVICE_UNCONFIRMED_I_AM,
|
||||
handler_i_am_bind);
|
||||
/* set the handler for all the services we don't implement
|
||||
It is required to send the proper reject message... */
|
||||
apdu_set_unrecognized_service_handler_handler(
|
||||
handler_unrecognized_service);
|
||||
/* we must implement read property - it's required! */
|
||||
apdu_set_confirmed_handler(
|
||||
SERVICE_CONFIRMED_READ_PROPERTY,
|
||||
handler_read_property);
|
||||
/* handle the data coming back from confirmed requests */
|
||||
apdu_set_confirmed_ack_handler(
|
||||
SERVICE_CONFIRMED_READ_PROPERTY,
|
||||
handler_read_property_ack);
|
||||
/* handle any errors coming back */
|
||||
apdu_set_error_handler(
|
||||
SERVICE_CONFIRMED_READ_PROPERTY,
|
||||
MyErrorHandler);
|
||||
apdu_set_abort_handler(
|
||||
MyAbortHandler);
|
||||
apdu_set_reject_handler(
|
||||
MyRejectHandler);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
BACNET_ADDRESS src = {0}; // address where message came from
|
||||
uint16_t pdu_len = 0;
|
||||
unsigned timeout = 100; // milliseconds
|
||||
unsigned max_apdu = 0;
|
||||
time_t elapsed_seconds = 0;
|
||||
time_t last_seconds = 0;
|
||||
time_t current_seconds = 0;
|
||||
time_t timeout_seconds = 0;
|
||||
int fileStartPosition = 0;
|
||||
uint8_t invoke_id = 0;
|
||||
bool found = false;
|
||||
char *value_string = NULL;
|
||||
|
||||
if (argc < 7)
|
||||
{
|
||||
printf("%s device-instance object-type object-instance property [index] tag value\r\n",
|
||||
filename_remove_path(argv[0]));
|
||||
return 0;
|
||||
}
|
||||
/* decode the command line parameters */
|
||||
Target_Device_Object_Instance = strtol(argv[1],NULL,0);
|
||||
Target_Object_Type = strtol(argv[2],NULL,0);
|
||||
Target_Object_Instance = strtol(argv[3],NULL,0);
|
||||
Target_Object_Property = strtol(argv[4],NULL,0);
|
||||
/* optional index */
|
||||
if (argc > 7)
|
||||
{
|
||||
Target_Object_Property_Index = strtol(argv[5],NULL,0);
|
||||
Target_Object_Property_Tag = strtol(argv[6],NULL,0);
|
||||
value_string = argv[7];
|
||||
}
|
||||
else
|
||||
{
|
||||
Target_Object_Property_Tag = strtol(argv[5],NULL,0);
|
||||
value_string = argv[6];
|
||||
}
|
||||
|
||||
if (Target_Device_Object_Instance >= BACNET_MAX_INSTANCE)
|
||||
{
|
||||
fprintf(stderr,"device-instance=%u - it must be less than %u\r\n",
|
||||
Target_Device_Object_Instance,BACNET_MAX_INSTANCE);
|
||||
return 1;
|
||||
}
|
||||
if (Target_Object_Type > MAX_BACNET_OBJECT_TYPE)
|
||||
{
|
||||
fprintf(stderr,"object-type=%u - it must be less than %u\r\n",
|
||||
Target_Object_Type,MAX_BACNET_OBJECT_TYPE+1);
|
||||
return 1;
|
||||
}
|
||||
if (Target_Object_Instance > BACNET_MAX_INSTANCE)
|
||||
{
|
||||
fprintf(stderr,"object-instance=%u - it must be less than %u\r\n",
|
||||
Target_Object_Instance,BACNET_MAX_INSTANCE+1);
|
||||
return 1;
|
||||
}
|
||||
if (Target_Object_Property > MAX_BACNET_PROPERTY_ID)
|
||||
{
|
||||
fprintf(stderr,"object-type=%u - it must be less than %u\r\n",
|
||||
Target_Object_Property,MAX_BACNET_PROPERTY_ID+1);
|
||||
return 1;
|
||||
}
|
||||
if (Target_Object_Property_Tag >= MAX_BACNET_APPLICATION_TAG)
|
||||
{
|
||||
fprintf(stderr,"tag=%u - it must be less than %u\r\n",
|
||||
Target_Object_Property_Tag,MAX_BACNET_APPLICATION_TAG);
|
||||
return 1;
|
||||
}
|
||||
status = bacapp_parse_application_data(
|
||||
Target_Object_Property_Tag,
|
||||
value_string,
|
||||
&Target_Object_Property_Value);
|
||||
if (!status)
|
||||
{
|
||||
/* FIXME: show the expected entry format for the tag */
|
||||
fprintf(stderr,"unable to parse the tag value\r\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* setup my info */
|
||||
Device_Set_Object_Instance_Number(BACNET_MAX_INSTANCE);
|
||||
address_init();
|
||||
Init_Service_Handlers();
|
||||
/* configure standard BACnet/IP port */
|
||||
bip_set_interface("eth0"); /* for linux */
|
||||
bip_set_port(0xBAC0);
|
||||
if (!bip_init())
|
||||
return 1;
|
||||
/* configure the timeout values */
|
||||
last_seconds = time(NULL);
|
||||
timeout_seconds = (Device_APDU_Timeout() / 1000) *
|
||||
Device_Number_Of_APDU_Retries();
|
||||
/* try to bind with the device */
|
||||
Send_WhoIs(Target_Device_Object_Instance,Target_Device_Object_Instance);
|
||||
/* loop forever */
|
||||
for (;;)
|
||||
{
|
||||
/* increment timer - exit if timed out */
|
||||
current_seconds = time(NULL);
|
||||
|
||||
/* returns 0 bytes on timeout */
|
||||
pdu_len = bip_receive(
|
||||
&src,
|
||||
&Rx_Buf[0],
|
||||
MAX_MPDU,
|
||||
timeout);
|
||||
|
||||
/* process */
|
||||
if (pdu_len)
|
||||
{
|
||||
npdu_handler(
|
||||
&src,
|
||||
&Rx_Buf[0],
|
||||
pdu_len);
|
||||
}
|
||||
/* at least one second has passed */
|
||||
if (current_seconds != last_seconds)
|
||||
tsm_timer_milliseconds(((current_seconds - last_seconds) * 1000));
|
||||
if (Error_Detected)
|
||||
break;
|
||||
if (I_Am_Request)
|
||||
{
|
||||
I_Am_Request = false;
|
||||
iam_send(&Handler_Transmit_Buffer[0]);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* wait until the device is bound, or timeout and quit */
|
||||
found = address_bind_request(
|
||||
Target_Device_Object_Instance,
|
||||
&max_apdu,
|
||||
&Target_Address);
|
||||
if (found)
|
||||
{
|
||||
if (invoke_id == 0)
|
||||
{
|
||||
invoke_id = Send_Write_Property_Request(
|
||||
uint32_t device_id, // destination device
|
||||
BACNET_OBJECT_TYPE object_type,
|
||||
uint32_t object_instance,
|
||||
BACNET_PROPERTY_ID object_property,
|
||||
BACNET_APPLICATION_DATA_VALUE object_value,
|
||||
uint8_t priority,
|
||||
int32_t array_index)
|
||||
Target_Device_Object_Instance,
|
||||
Target_Object_Type,
|
||||
Target_Object_Instance,
|
||||
Target_Object_Property,
|
||||
Target_Object_Property_Index);
|
||||
}
|
||||
else if (tsm_invoke_id_free(invoke_id))
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* increment timer - exit if timed out */
|
||||
elapsed_seconds += (current_seconds - last_seconds);
|
||||
if (elapsed_seconds > timeout_seconds)
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* keep track of time for next check */
|
||||
last_seconds = current_seconds;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user