Adding an AtomicReadFille command line demo, and splitting the handlers out of handlers.c.

This commit is contained in:
skarg
2006-01-13 21:53:46 +00:00
parent 4556df7083
commit 857a7ab736
13 changed files with 1240 additions and 24 deletions
+63
View File
@@ -0,0 +1,63 @@
/**************************************************************************
*
* Copyright (C) 2005 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 "config.h"
#include "txbuf.h"
#include "bacdef.h"
#include "bacdcode.h"
void handler_i_am(
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)src;
(void)service_len;
len = iam_decode_service_request(
service_request,
&device_id,
&max_apdu,
&segmentation,
&vendor_id);
fprintf(stderr,"Received I-Am Request");
if (len != -1)
{
fprintf(stderr," from %u!\n",device_id);
address_add(device_id,
max_apdu,
src);
}
else
fprintf(stderr,"!\n");
return;
}
+248
View File
@@ -0,0 +1,248 @@
/**************************************************************************
*
* Copyright (C) 2005 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 <string.h>
#include <errno.h>
#include "config.h"
#include "txbuf.h"
#include "bacdef.h"
#include "bacdcode.h"
#include "apdu.h"
#include "npdu.h"
#include "abort.h"
#include "rp.h"
/* demo objects */
#include "device.h"
#include "ai.h"
#include "ao.h"
#if BACFILE
#include "bacfile.h"
#endif
static uint8_t Temp_Buf[MAX_APDU] = {0};
void handler_read_property(
uint8_t *service_request,
uint16_t service_len,
BACNET_ADDRESS *src,
BACNET_CONFIRMED_SERVICE_DATA *service_data)
{
BACNET_READ_PROPERTY_DATA data;
int len = 0;
int pdu_len = 0;
BACNET_ADDRESS my_address;
bool send = false;
bool error = false;
int bytes_sent = 0;
BACNET_ERROR_CLASS error_class = ERROR_CLASS_OBJECT;
BACNET_ERROR_CODE error_code = ERROR_CODE_UNKNOWN_OBJECT;
len = rp_decode_service_request(
service_request,
service_len,
&data);
if (len <= 0)
fprintf(stderr,"Unable to decode Read-Property Request!\n");
// prepare a reply
datalink_get_my_address(&my_address);
// encode the NPDU portion of the packet
pdu_len = npdu_encode_apdu(
&Handler_Transmit_Buffer[0],
src,
&my_address,
false, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL);
// bad decoding - send an abort
if (len == -1)
{
pdu_len += abort_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
ABORT_REASON_OTHER);
fprintf(stderr,"Sending Abort!\n");
send = true;
}
else if (service_data->segmented_message)
{
pdu_len += abort_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
ABORT_REASON_SEGMENTATION_NOT_SUPPORTED);
fprintf(stderr,"Sending Abort!\n");
send = true;
}
else
{
switch (data.object_type)
{
case OBJECT_DEVICE:
// FIXME: probably need a length limitation sent with encode
if (data.object_instance == Device_Object_Instance_Number())
{
len = Device_Encode_Property_APDU(
&Temp_Buf[0],
data.object_property,
data.array_index,
&error_class,
&error_code);
if (len > 0)
{
// encode the APDU portion of the packet
data.application_data = &Temp_Buf[0];
data.application_data_len = len;
// FIXME: probably need a length limitation sent with encode
pdu_len += rp_ack_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
&data);
fprintf(stderr,"Sending Read Property Ack!\n");
send = true;
}
else
error = true;
}
else
error = true;
break;
case OBJECT_ANALOG_INPUT:
if (Analog_Input_Valid_Instance(data.object_instance))
{
len = Analog_Input_Encode_Property_APDU(
&Temp_Buf[0],
data.object_instance,
data.object_property,
data.array_index,
&error_class,
&error_code);
if (len > 0)
{
// encode the APDU portion of the packet
data.application_data = &Temp_Buf[0];
data.application_data_len = len;
// FIXME: probably need a length limitation sent with encode
pdu_len += rp_ack_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
&data);
fprintf(stderr,"Sending Read Property Ack!\n");
send = true;
}
else
error = true;
}
else
error = true;
break;
case OBJECT_ANALOG_OUTPUT:
if (Analog_Output_Valid_Instance(data.object_instance))
{
len = Analog_Output_Encode_Property_APDU(
&Temp_Buf[0],
data.object_instance,
data.object_property,
data.array_index,
&error_class,
&error_code);
if (len > 0)
{
// encode the APDU portion of the packet
data.application_data = &Temp_Buf[0];
data.application_data_len = len;
// FIXME: probably need a length limitation sent with encode
pdu_len += rp_ack_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
&data);
fprintf(stderr,"Sending Read Property Ack!\n");
send = true;
}
else
error = true;
}
else
error = true;
break;
#if BACFILE
case OBJECT_FILE:
if (bacfile_valid_instance(data.object_instance))
{
len = bacfile_encode_property_apdu(
&Temp_Buf[0],
data.object_instance,
data.object_property,
data.array_index,
&error_class,
&error_code);
if (len > 0)
{
// encode the APDU portion of the packet
data.application_data = &Temp_Buf[0];
data.application_data_len = len;
// FIXME: probably need a length limitation sent with encode
pdu_len += rp_ack_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
&data);
fprintf(stderr,"Sending Read Property Ack!\n");
send = true;
}
else
error = true;
}
else
error = true;
break;
#endif
default:
error = true;
break;
}
}
if (error)
{
pdu_len += bacerror_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
SERVICE_CONFIRMED_READ_PROPERTY,
error_class,
error_code);
fprintf(stderr,"Sending Read Property Error!\n");
send = true;
}
if (send)
{
bytes_sent = datalink_send_pdu(
src, // destination address
&Handler_Transmit_Buffer[0],
pdu_len); // number of bytes of data
if (bytes_sent <= 0)
fprintf(stderr,"Failed to send PDU (%s)!\n", strerror(errno));
}
return;
}
+67
View File
@@ -0,0 +1,67 @@
/**************************************************************************
*
* Copyright (C) 2005 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 <string.h>
#include <errno.h>
#include "config.h"
#include "txbuf.h"
#include "bacdef.h"
#include "bacdcode.h"
#include "whois.h"
#include "device.h"
/* global flag to send an I-Am */
bool I_Am_Request = true;
void handler_who_is(
uint8_t *service_request,
uint16_t service_len,
BACNET_ADDRESS *src)
{
int len = 0;
int32_t low_limit = 0;
int32_t high_limit = 0;
(void)src;
len = whois_decode_service_request(
service_request,
service_len,
&low_limit,
&high_limit);
/* in our simple system, we just set a flag and let the main loop
send the I-Am request. */
if (len == 0)
I_Am_Request = true;
else if (len != -1)
{
if ((Device_Object_Instance_Number() >= (uint32_t)low_limit) &&
(Device_Object_Instance_Number() <= (uint32_t)high_limit))
I_Am_Request = true;
}
return;
}
+63
View File
@@ -0,0 +1,63 @@
/**************************************************************************
*
* Copyright (C) 2005 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.
*
*********************************************************************/
#ifndef HANDLERS_H
#define HANDLERS_H
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include "bacdef.h"
#include "apdu.h"
#include "bacapp.h"
/* flag requesting main loop to send an I-Am */
extern bool I_Am_Request;
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
void handler_unrecognized_service(
uint8_t *service_request,
uint16_t service_len,
BACNET_ADDRESS *dest,
BACNET_CONFIRMED_SERVICE_DATA *service_data);
void handler_who_is(
uint8_t *service_request,
uint16_t service_len,
BACNET_ADDRESS *src);
void handler_read_property(
uint8_t *service_request,
uint16_t service_len,
BACNET_ADDRESS *src,
BACNET_CONFIRMED_SERVICE_DATA *service_data);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif
+73
View File
@@ -0,0 +1,73 @@
/**************************************************************************
*
* Copyright (C) 2005 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 <string.h>
#include <errno.h>
#include "txbuf.h"
#include "bacdef.h"
#include "bacdcode.h"
#include "apdu.h"
#include "npdu.h"
#include "reject.h"
void handler_unrecognized_service(
uint8_t *service_request,
uint16_t service_len,
BACNET_ADDRESS *dest,
BACNET_CONFIRMED_SERVICE_DATA *service_data)
{
BACNET_ADDRESS src;
int pdu_len = 0;
int bytes_sent = 0;
(void)service_request;
(void)service_len;
datalink_get_my_address(&src);
// encode the NPDU portion of the packet
pdu_len = npdu_encode_apdu(
&Handler_Transmit_Buffer[0],
dest,
&src,
false, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL);
// encode the APDU portion of the packet
pdu_len += reject_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
service_data->invoke_id,
REJECT_REASON_UNRECOGNIZED_SERVICE);
bytes_sent = datalink_send_pdu(
dest, // destination address
&Handler_Transmit_Buffer[0],
pdu_len); // number of bytes of data
if (bytes_sent > 0)
fprintf(stderr,"Sent Reject!\n");
else
fprintf(stderr,"Failed to Send Reject (%s)!\n", strerror(errno));
}
+31
View File
@@ -0,0 +1,31 @@
/**************************************************************************
*
* Copyright (C) 2005 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 "config.h"
#include "datalink.h"
uint8_t Handler_Transmit_Buffer[MAX_MPDU] = {0};
+35
View File
@@ -0,0 +1,35 @@
/**************************************************************************
*
* Copyright (C) 2005 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.
*
*********************************************************************/
#ifndef TXBUF_H
#define TXBUF_H
#include <stddef.h>
#include <stdint.h>
#include "config.h"
#include "datalink.h"
extern uint8_t Handler_Transmit_Buffer[MAX_MPDU];
#endif
+137
View File
@@ -0,0 +1,137 @@
#
# 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 = readfile
PRODUCT_EXE = $(PRODUCT).exe
# Choose the Data Link Layer to Enable
DEFINES = -DBACDL_BIP=1
SRCS = readfile.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 \
..\..\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 *.obj
del ..\..\*.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
+451
View File
@@ -0,0 +1,451 @@
/**************************************************************************
*
* 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.
*
*********************************************************************/
/* READFILE: command line tool that reads a file from a BACnet device. */
#include <winsock2.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <conio.h> /* for kbhit and getch */
#include <time.h> /* for time */
#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 "datalink.h"
#include "whois.h"
/* some demo stuff needed */
#include "handlers.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_File_Object_Instance = 4194303;
static uint32_t Target_Device_Object_Instance = 4194303;
static BACNET_ADDRESS Target_Address;
static char *Local_File_Name = NULL;
static bool End_Of_File_Detected = false;
static bool Error_Detected = false;
static uint8_t Current_Invoke_ID = 0;
static void Atomic_Read_File_Error_Handler(
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 uint8_t Send_Atomic_Read_File_Stream(
uint32_t device_id,
uint32_t file_instance,
int fileStartPosition,
unsigned requestedOctetCount)
{
BACNET_ADDRESS dest;
BACNET_ADDRESS my_address;
unsigned max_apdu = 0;
uint8_t invoke_id = 0;
bool status = false;
int pdu_len = 0;
int bytes_sent = 0;
BACNET_ATOMIC_READ_FILE_DATA data;
/* is the device bound? */
status = address_get_by_device(device_id, &max_apdu, &dest);
/* is there a tsm available? */
if (status)
status = tsm_transaction_available();
if (status)
{
datalink_get_my_address(&my_address);
pdu_len = npdu_encode_apdu(
&Handler_Transmit_Buffer[0],
&dest,
&my_address,
true, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL);
invoke_id = tsm_next_free_invokeID();
// load the data for the encoding
data.object_type = OBJECT_FILE;
data.object_instance = file_instance;
data.access = FILE_STREAM_ACCESS;
data.type.stream.fileStartPosition = fileStartPosition;
data.type.stream.requestedOctetCount = requestedOctetCount;
pdu_len += arf_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
invoke_id,
&data);
/* will the APDU fit the target device?
note: if there is a bottleneck router in between
us and the destination, we won't know unless
we have a way to check for that and update the
max_apdu in the address binding table. */
if ((unsigned)pdu_len < max_apdu)
{
tsm_set_confirmed_unsegmented_transaction(
invoke_id,
&dest,
&Handler_Transmit_Buffer[0],
pdu_len);
bytes_sent = datalink_send_pdu(
&dest, // destination address
&Handler_Transmit_Buffer[0],
pdu_len); // number of bytes of data
if (bytes_sent <= 0)
fprintf(stderr,"Failed to Send AtomicReadFile Request (%s)!\n",
strerror(errno));
}
else
fprintf(stderr,"Failed to Send AtomicReadFile Request "
"(payload exceeds destination maximum APDU)!\n");
}
return invoke_id;
}
static void Send_WhoIs(uint32_t device_id)
{
int pdu_len = 0;
BACNET_ADDRESS dest;
int bytes_sent = 0;
/* Who-Is is a global broadcast */
datalink_get_broadcast_address(&dest);
/* encode the NPDU portion of the packet */
pdu_len = npdu_encode_apdu(
&Handler_Transmit_Buffer[0],
&dest,
NULL,
false, // true for confirmed messages
MESSAGE_PRIORITY_NORMAL);
/* encode the APDU portion of the packet */
pdu_len += whois_encode_apdu(
&Handler_Transmit_Buffer[pdu_len],
device_id,
device_id);
bytes_sent = datalink_send_pdu(
&dest, /* destination address */
&Handler_Transmit_Buffer[0],
pdu_len); /* number of bytes of data */
if (bytes_sent <= 0)
fprintf(stderr,"Failed to Send Who-Is Request (%s)!\n", strerror(errno));
}
static void AtomicReadFileAckHandler(
uint8_t *service_request,
uint16_t service_len,
BACNET_ADDRESS *src,
BACNET_CONFIRMED_SERVICE_ACK_DATA *service_data)
{
int len = 0;
BACNET_ATOMIC_READ_FILE_DATA data;
FILE *pFile = NULL; /* stream pointer */
size_t octets_written = 0;
(void)src; /* FIXME: validate the source address matches */
len = arf_ack_decode_service_request(
service_request,
service_len,
&data);
if (len > 0)
{
/* validate the parameters before storing data */
if ((data.access == FILE_STREAM_ACCESS) &&
(service_data->invoke_id == Current_Invoke_ID))
{
if (data.type.stream.fileStartPosition == 0)
pFile = fopen(Local_File_Name, "wb");
else
pFile = fopen(Local_File_Name, "rb+");
if (pFile)
{
/* is there anything to do with this? data.stream.requestedOctetCount */
(void)fseek(pFile, data.type.stream.fileStartPosition, SEEK_SET);
octets_written = fwrite(
octetstring_value(&data.fileData),
1, /* unit to write in bytes - in our case, an octet is one byte */
octetstring_length(&data.fileData),
pFile);
if (octets_written != octetstring_length(&data.fileData))
fprintf(stderr,"Unable to write data to file \"%s\".\n",
Local_File_Name);
else
printf("\r%u bytes",
(data.type.stream.fileStartPosition + octets_written));
fclose(pFile);
}
if (data.endOfFile)
{
End_Of_File_Detected = true;
printf("\r\n");
}
}
}
}
static void LocalIAmHandler(
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)src;
(void)service_len;
len = iam_decode_service_request(
service_request,
&device_id,
&max_apdu,
&segmentation,
&vendor_id);
if (len != -1)
{
address_add(device_id,
max_apdu,
src);
}
else
fprintf(stderr,"!\n");
return;
}
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,
LocalIAmHandler);
/* 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_ATOMIC_READ_FILE,
AtomicReadFileAckHandler);
/* handle any errors coming back */
apdu_set_error_handler(
SERVICE_CONFIRMED_ATOMIC_READ_FILE,
Atomic_Read_File_Error_Handler);
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;
bool status = false;
time_t elapsed_seconds = 0;
time_t last_seconds = 0;
time_t current_seconds = 0;
time_t timeout_seconds = 0;
int fileStartPosition = 0;
unsigned requestedOctetCount = 0;
uint8_t invoke_id = 0;
bool found = false;
if (argc < 4)
{
/* FIXME: what about access method - record or stream? */
printf("%s device-instance file-instance local-name\r\n",argv[0]);
return 0;
}
/* decode the command line parameters */
Target_Device_Object_Instance = strtol(argv[1],NULL,0);
Target_File_Object_Instance = strtol(argv[2],NULL,0);
Local_File_Name = argv[3];
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_File_Object_Instance >= BACNET_MAX_INSTANCE)
{
fprintf(stderr,"file-instance=%u - it must be less than %u\r\n",
Target_File_Object_Instance,BACNET_MAX_INSTANCE+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_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);
/* 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 (End_Of_File_Detected || 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)
{
/* calculate the smaller of our APDU size or theirs
and remove the overhead of the APDU (about 14 octets max).
note: we could fail if there is a bottle neck (router)
and smaller MPDU in betweeen. */
requestedOctetCount = min(max_apdu,MAX_APDU) - 14;
/* has the previous invoke id expired or returned?
note: invoke ID = 0 is invalid, so it will be idle */
if ((invoke_id == 0) || tsm_invoke_id_free(invoke_id))
{
if (invoke_id != 0)
fileStartPosition += requestedOctetCount;
/* we'll read the file in chunks
less than max_apdu to keep unsegmented */
invoke_id = Send_Atomic_Read_File_Stream(
Target_Device_Object_Instance,
Target_File_Object_Instance,
fileStartPosition,
requestedOctetCount);
Current_Invoke_ID = invoke_id;
}
}
else
{
/* increment timer - exit if timed out */
elapsed_seconds += (current_seconds - last_seconds);
if (elapsed_seconds > timeout_seconds)
break;
}
}
/* wait for ESC from keyboard before quitting */
if (kbhit() && (getch() == 0x1B))
break;
/* keep track of time for next check */
last_seconds = current_seconds;
}
return 0;
}
Binary file not shown.
-2
View File
@@ -210,8 +210,6 @@ bool Send_Read_Property_Request(
return status;
}
bool Send_Write_Property_Request(
uint32_t device_id, // destination device
BACNET_OBJECT_TYPE object_type,
+70 -22
View File
@@ -56,18 +56,35 @@
// FIXME: not coded for segmentation
// declare space for the TSM transactions, and set it up in the init.
/* table rules: an Invoke ID = 0 is an unused spot in the table */
static BACNET_TSM_DATA TSM_List[MAX_TSM_TRANSACTIONS] = {{0}};
// returns MAX_TSM_TRANSACTIONS if not found
uint8_t tsm_find_invokeID_index(uint8_t invokeID)
static uint8_t tsm_find_invokeID_index(uint8_t invokeID)
{
unsigned i = 0; // counter
uint8_t index = MAX_TSM_TRANSACTIONS; // return value
for (i = 0; i < MAX_TSM_TRANSACTIONS; i++)
{
if ((TSM_List[i].state != TSM_STATE_IDLE) &&
(TSM_List[i].InvokeID == invokeID))
if (TSM_List[i].InvokeID == invokeID)
{
index = i;
break;
}
}
return index;
}
static uint8_t tsm_find_first_free_index(void)
{
unsigned i = 0; // counter
uint8_t index = MAX_TSM_TRANSACTIONS; // return value
for (i = 0; i < MAX_TSM_TRANSACTIONS; i++)
{
if (TSM_List[i].InvokeID == 0)
{
index = i;
break;
@@ -81,10 +98,10 @@ bool tsm_transaction_available(void)
{
bool status = false; // return value
unsigned i = 0; // counter
for (i = 0; i < MAX_TSM_TRANSACTIONS; i++)
{
if (TSM_List[i].state == TSM_STATE_IDLE)
if (TSM_List[i].InvokeID == 0)
{
// one is available!
status = true;
@@ -99,10 +116,11 @@ uint8_t tsm_transaction_idle_count(void)
{
uint8_t count = 0; // return value
unsigned i = 0; // counter
for (i = 0; i < MAX_TSM_TRANSACTIONS; i++)
{
if (TSM_List[i].state == TSM_STATE_IDLE)
if ((TSM_List[i].InvokeID == 0) &&
(TSM_List[i].state == TSM_STATE_IDLE))
{
// one is available!
count++;
@@ -112,6 +130,9 @@ uint8_t tsm_transaction_idle_count(void)
return count;
}
/* gets the next free invokeID,
and reserves a spot in the table
returns 0 if none are available */
uint8_t tsm_next_free_invokeID(void)
{
static uint8_t current_invokeID = 1; // incremented...
@@ -122,18 +143,26 @@ uint8_t tsm_next_free_invokeID(void)
while (!found)
{
index = tsm_find_invokeID_index(current_invokeID);
/* not found - that is good! */
if (index == MAX_TSM_TRANSACTIONS)
{
found = true;
invokeID = current_invokeID;
/* set this id into the table */
index = tsm_find_first_free_index();
if (index != MAX_TSM_TRANSACTIONS)
{
TSM_List[index].InvokeID = invokeID = current_invokeID;
TSM_List[index].state = TSM_STATE_IDLE;
TSM_List[index].RequestTimer = Device_APDU_Timeout();
/* update for the next call or check */
current_invokeID++;
// skip zero - we treat that internally as invalid or no free
if (current_invokeID == 0)
current_invokeID = 1;
}
}
/* update for the next call or check */
current_invokeID++;
// skip zero - we treat that internally as invalid or no free
if (current_invokeID == 0)
current_invokeID++;
}
return invokeID;
}
@@ -166,7 +195,7 @@ void tsm_set_confirmed_unsegmented_transaction(
}
}
return;
return;
}
// used to retrieve the transaction payload
@@ -200,15 +229,15 @@ bool tsm_get_transaction_pdu(
}
}
return found;
return found;
}
// called once a millisecond
/* called once a millisecond or slower */
void tsm_timer_milliseconds(uint16_t milliseconds)
{
unsigned i = 0; // counter
int bytes_sent = 0;
for (i = 0; i < MAX_TSM_TRANSACTIONS; i++)
{
if (TSM_List[i].state == TSM_STATE_AWAIT_CONFIRMATION)
@@ -217,7 +246,7 @@ void tsm_timer_milliseconds(uint16_t milliseconds)
TSM_List[i].RequestTimer -= milliseconds;
else
TSM_List[i].RequestTimer = 0;
// timeout. retry?
/* timeout. retry? */
if (TSM_List[i].RequestTimer == 0)
{
TSM_List[i].RetryCount--;
@@ -225,12 +254,15 @@ void tsm_timer_milliseconds(uint16_t milliseconds)
if (TSM_List[i].RetryCount)
{
bytes_sent = datalink_send_pdu(
&TSM_List[i].dest, // destination address
&TSM_List[i].dest, /* destination address */
&TSM_List[i].pdu[0],
TSM_List[i].pdu_len); // number of bytes of data
TSM_List[i].pdu_len); /* number of bytes of data */
}
else
{
TSM_List[i].InvokeID = 0;
TSM_List[i].state = TSM_STATE_IDLE;
}
}
}
}
@@ -239,10 +271,26 @@ void tsm_timer_milliseconds(uint16_t milliseconds)
void tsm_free_invoke_id(uint8_t invokeID)
{
uint8_t index;
index = tsm_find_invokeID_index(invokeID);
if (index < MAX_TSM_TRANSACTIONS)
{
TSM_List[index].state = TSM_STATE_IDLE;
TSM_List[index].InvokeID = 0;
}
}
/* see if the invoke ID has been made free */
bool tsm_invoke_id_free(uint8_t invokeID)
{
bool status = true;
uint8_t index;
index = tsm_find_invokeID_index(invokeID);
if (index < MAX_TSM_TRANSACTIONS)
status = false;
return status;
}
#ifdef TEST
+2
View File
@@ -108,6 +108,8 @@ bool tsm_get_transaction_pdu(
uint8_t *pdu,
uint16_t *pdu_len);
bool tsm_invoke_id_free(uint8_t invokeID);
#ifdef __cplusplus
}
#endif /* __cplusplus */