Moved the demo objects into demo/object directory, and updated the makefiles and unit tests to handle the move.

This commit is contained in:
skarg
2006-01-10 20:36:30 +00:00
parent 3c1cc848a6
commit d7ed1480fe
21 changed files with 53 additions and 54 deletions
+182
View File
@@ -0,0 +1,182 @@
/**************************************************************************
*
* 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.
*
*********************************************************************/
// Analog Input Objects customize for your use
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "bacdef.h"
#include "bacdcode.h"
#include "bacenum.h"
#include "config.h" // the custom stuff
#define MAX_ANALOG_INPUTS 7
// we simply have 0-n object instances. Yours might be
// more complex, and then you need validate that the
// given instance exists
bool Analog_Input_Valid_Instance(uint32_t object_instance)
{
if (object_instance < MAX_ANALOG_INPUTS)
return true;
return false;
}
// we simply have 0-n object instances. Yours might be
// more complex, and then count how many you have
unsigned Analog_Input_Count(void)
{
return MAX_ANALOG_INPUTS;
}
// we simply have 0-n object instances. Yours might be
// more complex, and then you need to return the instance
// that correlates to the correct index
uint32_t Analog_Input_Index_To_Instance(unsigned index)
{
return index;
}
int Analog_Input_Encode_Property_APDU(
uint8_t *apdu,
uint32_t object_instance,
BACNET_PROPERTY_ID property,
int32_t array_index,
BACNET_ERROR_CLASS *error_class,
BACNET_ERROR_CODE *error_code)
{
int apdu_len = 0; // return value
BACNET_BIT_STRING bit_string;
BACNET_CHARACTER_STRING char_string;
char text_string[32] = {""};
float value = 3.14;
(void)array_index;
switch (property)
{
case PROP_OBJECT_IDENTIFIER:
apdu_len = encode_tagged_object_id(&apdu[0], OBJECT_ANALOG_INPUT,
object_instance);
break;
case PROP_OBJECT_NAME:
case PROP_DESCRIPTION:
sprintf(text_string,"ANALOG INPUT %u",object_instance);
characterstring_init_ansi(&char_string, text_string);
apdu_len = encode_tagged_character_string(&apdu[0],
&char_string);
break;
case PROP_OBJECT_TYPE:
apdu_len = encode_tagged_enumerated(&apdu[0], OBJECT_ANALOG_INPUT);
break;
case PROP_PRESENT_VALUE:
apdu_len = encode_tagged_real(&apdu[0], value);
break;
case PROP_STATUS_FLAGS:
bitstring_init(&bit_string);
bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false);
bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false);
bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false);
bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false);
apdu_len = encode_tagged_bitstring(&apdu[0], &bit_string);
break;
case PROP_EVENT_STATE:
apdu_len = encode_tagged_enumerated(&apdu[0],EVENT_STATE_NORMAL);
break;
case PROP_OUT_OF_SERVICE:
apdu_len = encode_tagged_boolean(&apdu[0],false);
break;
case PROP_UNITS:
apdu_len = encode_tagged_enumerated(&apdu[0],UNITS_PERCENT);
break;
default:
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_UNKNOWN_PROPERTY;
break;
}
return apdu_len;
}
#ifdef TEST
#include <assert.h>
#include <string.h>
#include "ctest.h"
void testAnalogInput(Test * pTest)
{
uint8_t apdu[MAX_APDU] = { 0 };
int len = 0;
uint32_t len_value = 0;
uint8_t tag_number = 0;
BACNET_OBJECT_TYPE decoded_type = OBJECT_ANALOG_OUTPUT;
uint32_t decoded_instance = 0;
uint32_t instance = 123;
BACNET_ERROR_CLASS error_class;
BACNET_ERROR_CODE error_code;
// FIXME: we should do a lot more testing here...
len = Analog_Input_Encode_Property_APDU(
&apdu[0],
instance,
PROP_OBJECT_IDENTIFIER,
BACNET_ARRAY_ALL,
&error_class,
&error_code);
ct_test(pTest, len != 0);
len = decode_tag_number_and_value(&apdu[0], &tag_number, &len_value);
ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_OBJECT_ID);
len = decode_object_id(&apdu[len],
(int *) &decoded_type, &decoded_instance);
ct_test(pTest, decoded_type == OBJECT_ANALOG_INPUT);
ct_test(pTest, decoded_instance == instance);
return;
}
#ifdef TEST_ANALOG_INPUT
int main(void)
{
Test *pTest;
bool rc;
pTest = ct_create("BACnet Analog Input", NULL);
/* individual tests */
rc = ct_addTestFunction(pTest, testAnalogInput);
assert(rc);
ct_setStream(pTest, stdout);
ct_run(pTest);
(void) ct_report(pTest);
ct_destroy(pTest);
return 0;
}
#endif /* TEST_ANALOG_INPUT */
#endif /* TEST */
+57
View File
@@ -0,0 +1,57 @@
/**************************************************************************
*
* 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 AI_H
#define AI_H
#include <stdbool.h>
#include <stdint.h>
#include "bacdef.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
bool Analog_Input_Valid_Instance(uint32_t object_instance);
unsigned Analog_Input_Count(void);
uint32_t Analog_Input_Index_To_Instance(unsigned index);
int Analog_Input_Encode_Property_APDU(
uint8_t *apdu,
uint32_t object_instance,
BACNET_PROPERTY_ID property,
int32_t array_index,
BACNET_ERROR_CLASS *error_class,
BACNET_ERROR_CODE *error_code);
#ifdef TEST
#include "ctest.h"
void testAnalogInput(Test * pTest);
#endif
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif
+35
View File
@@ -0,0 +1,35 @@
#Makefile to build test case
CC = gcc
BASEDIR = .
#CFLAGS = -Wall -I.
# -g for debugging with gdb
#CFLAGS = -Wall -I. -g
CFLAGS = -Wall -I. -Itest -DTEST -DTEST_ANALOG_INPUT -g
# NOTE: this file is normally called by the unittest.sh from up directory
SRCS = bacdcode.c \
bacstr.c \
bigend.c \
demo/object/ai.c \
test/ctest.c
OBJS = ${SRCS:.c=.o}
TARGET = analog_input
all: ${TARGET}
${TARGET}: ${OBJS}
${CC} -o $@ ${OBJS}
.c.o:
${CC} -c ${CFLAGS} $*.c -o $@
depend:
rm -f .depend
${CC} -MM ${CFLAGS} *.c >> .depend
clean:
rm -rf core ${TARGET} $(OBJS) *.bak *.1 *.ini
include: .depend
+423
View File
@@ -0,0 +1,423 @@
/**************************************************************************
*
* 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.
*
*********************************************************************/
// Analog Output Objects - customize for your use
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "bacdef.h"
#include "bacdcode.h"
#include "bacenum.h"
#include "config.h" // the custom stuff
#include "wp.h"
#define MAX_ANALOG_OUTPUTS 4
// we choose to have a NULL level in our system represented by
// a particular value. When the priorities are not in use, they
// will be relinquished (i.e. set to the NULL level).
#define AO_LEVEL_NULL 255
// When all the priorities are level null, the present value returns
// the Relinquish Default value
#define AO_RELINQUISH_DEFAULT 0
// Here is our Priority Array. They are supposed to be Real, but
// we don't have that kind of memory, so we will use a single byte
// and load a Real for returning the value when asked.
static uint8_t Analog_Output_Level[MAX_ANALOG_OUTPUTS][BACNET_MAX_PRIORITIES];
// Writable out-of-service allows others to play with our Present Value
// without changing the physical output
static bool Analog_Output_Out_Of_Service[MAX_ANALOG_OUTPUTS];
// we need to have our arrays initialized before answering any calls
static bool Analog_Ouput_Initialized = false;
void Analog_Output_Init(void)
{
unsigned i,j;
if (!Analog_Ouput_Initialized)
{
Analog_Ouput_Initialized = true;
// initialize all the analog output priority arrays to NULL
for (i = 0; i < MAX_ANALOG_OUTPUTS; i++)
{
for (j = 0; j < BACNET_MAX_PRIORITIES; j++)
{
Analog_Output_Level[i][j] = AO_LEVEL_NULL;
}
}
}
return;
}
// we simply have 0-n object instances. Yours might be
// more complex, and then you need validate that the
// given instance exists
bool Analog_Output_Valid_Instance(uint32_t object_instance)
{
Analog_Output_Init();
if (object_instance < MAX_ANALOG_OUTPUTS)
return true;
return false;
}
// we simply have 0-n object instances. Yours might be
// more complex, and then count how many you have
unsigned Analog_Output_Count(void)
{
Analog_Output_Init();
return MAX_ANALOG_OUTPUTS;
}
// we simply have 0-n object instances. Yours might be
// more complex, and then you need to return the instance
// that correlates to the correct index
uint32_t Analog_Output_Index_To_Instance(unsigned index)
{
Analog_Output_Init();
return index;
}
// we simply have 0-n object instances. Yours might be
// more complex, and then you need to return the index
// that correlates to the correct instance number
unsigned Analog_Output_Instance_To_Index(uint32_t object_instance)
{
unsigned index = MAX_ANALOG_OUTPUTS;
Analog_Output_Init();
if (object_instance < MAX_ANALOG_OUTPUTS)
index = object_instance;
return index;
}
static float Analog_Output_Present_Value(uint32_t object_instance)
{
float value = AO_RELINQUISH_DEFAULT;
unsigned index = 0;
unsigned i = 0;
Analog_Output_Init();
index = Analog_Output_Instance_To_Index(object_instance);
if (index < MAX_ANALOG_OUTPUTS)
{
for (i = 0; i < BACNET_MAX_PRIORITIES; i++)
{
if (Analog_Output_Level[index][i] != AO_LEVEL_NULL)
{
value = Analog_Output_Level[index][i];
break;
}
}
}
return value;
}
int Analog_Output_Encode_Property_APDU(
uint8_t *apdu,
uint32_t object_instance,
BACNET_PROPERTY_ID property,
int32_t array_index,
BACNET_ERROR_CLASS *error_class,
BACNET_ERROR_CODE *error_code)
{
int len = 0;
int apdu_len = 0; // return value
BACNET_BIT_STRING bit_string;
BACNET_CHARACTER_STRING char_string;
char text_string[32] = {""};
float real_value = 1.414;
unsigned object_index = 0;
unsigned i = 0;
Analog_Output_Init();
switch (property)
{
case PROP_OBJECT_IDENTIFIER:
apdu_len = encode_tagged_object_id(&apdu[0], OBJECT_ANALOG_OUTPUT,
object_instance);
break;
case PROP_OBJECT_NAME:
case PROP_DESCRIPTION:
// note: the object name must be unique within this device
sprintf(text_string,"ANALOG OUTPUT %u",object_instance);
characterstring_init_ansi(&char_string, text_string);
apdu_len = encode_tagged_character_string(&apdu[0],
&char_string);
break;
case PROP_OBJECT_TYPE:
apdu_len = encode_tagged_enumerated(&apdu[0], OBJECT_ANALOG_OUTPUT);
break;
case PROP_PRESENT_VALUE:
real_value = Analog_Output_Present_Value(object_instance);
apdu_len = encode_tagged_real(&apdu[0], real_value);
break;
case PROP_STATUS_FLAGS:
bitstring_init(&bit_string);
bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false);
bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false);
bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false);
bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false);
apdu_len = encode_tagged_bitstring(&apdu[0], &bit_string);
break;
case PROP_EVENT_STATE:
apdu_len = encode_tagged_enumerated(&apdu[0],EVENT_STATE_NORMAL);
break;
case PROP_OUT_OF_SERVICE:
apdu_len = encode_tagged_boolean(&apdu[0],false);
break;
case PROP_UNITS:
apdu_len = encode_tagged_enumerated(&apdu[0],UNITS_PERCENT);
break;
case PROP_PRIORITY_ARRAY:
// Array element zero is the number of elements in the array
if (array_index == BACNET_ARRAY_LENGTH_INDEX)
apdu_len = encode_tagged_unsigned(&apdu[0], BACNET_MAX_PRIORITIES);
// if no index was specified, then try to encode the entire list
// into one packet.
else if (array_index == BACNET_ARRAY_ALL)
{
object_index = Analog_Output_Instance_To_Index(object_instance);
for (i = 0; i < BACNET_MAX_PRIORITIES; i++)
{
// FIXME: check if we have room before adding it to APDU
if (Analog_Output_Level[object_index][i] == AO_LEVEL_NULL)
len = encode_tagged_null(&apdu[apdu_len]);
else
{
real_value = Analog_Output_Level[object_index][i];
len = encode_tagged_real(&apdu[apdu_len], real_value);
}
// add it if we have room
if ((apdu_len + len) < MAX_APDU)
apdu_len += len;
else
{
*error_class = ERROR_CLASS_SERVICES;
*error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT;
apdu_len = 0;
break;
}
}
}
else
{
object_index = Analog_Output_Instance_To_Index(object_instance);
if (array_index <= BACNET_MAX_PRIORITIES)
{
if (Analog_Output_Level[object_index][array_index] == AO_LEVEL_NULL)
len = encode_tagged_null(&apdu[apdu_len]);
else
{
real_value = Analog_Output_Level[object_index][array_index];
len = encode_tagged_real(&apdu[apdu_len], real_value);
}
}
else
{
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_ARRAY_INDEX;
}
}
break;
case PROP_RELINQUISH_DEFAULT:
real_value = AO_RELINQUISH_DEFAULT;
apdu_len = encode_tagged_real(&apdu[0], real_value);
break;
default:
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_UNKNOWN_PROPERTY;
break;
}
return apdu_len;
}
// returns true if successful
bool Analog_Output_Write_Property(
BACNET_WRITE_PROPERTY_DATA *wp_data,
BACNET_ERROR_CLASS *error_class,
BACNET_ERROR_CODE *error_code)
{
bool status = false; // return value
unsigned int object_index = 0;
unsigned int priority = 0;
uint8_t level = AO_LEVEL_NULL;
Analog_Output_Init();
if (!Analog_Output_Valid_Instance(wp_data->object_instance))
{
*error_class = ERROR_CLASS_OBJECT;
*error_code = ERROR_CODE_UNKNOWN_OBJECT;
return false;
}
// decode the some of the request
switch (wp_data->object_property)
{
case PROP_PRESENT_VALUE:
if (wp_data->value.tag == BACNET_APPLICATION_TAG_REAL)
{
priority = wp_data->priority;
if (priority && (priority <= BACNET_MAX_PRIORITIES) &&
(priority != 6 /* reserved */) &&
(wp_data->value.type.Real >= 0.0) &&
(wp_data->value.type.Real <= 100.0))
{
level = (uint8_t)wp_data->value.type.Real;
object_index = Analog_Output_Instance_To_Index(
wp_data->object_instance);
priority--;
Analog_Output_Level[object_index][priority] = level;
// if Out of Service is TRUE, then don't set the
// physical output. This comment may apply to the
// main loop (i.e. check out of service before changing output)
status = true;
}
else if (priority == 6)
{
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_WRITE_ACCESS_DENIED;
}
else
{
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_VALUE_OUT_OF_RANGE;
}
}
else if (wp_data->value.tag == BACNET_APPLICATION_TAG_NULL)
{
level = AO_LEVEL_NULL;
object_index = Analog_Output_Instance_To_Index(
wp_data->object_instance);
priority = wp_data->priority;
if (priority && (priority <= BACNET_MAX_PRIORITIES))
{
priority--;
Analog_Output_Level[object_index][priority] = level;
status = true;
}
else
{
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_VALUE_OUT_OF_RANGE;
}
}
else
{
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_DATA_TYPE;
}
break;
case PROP_OUT_OF_SERVICE:
if (wp_data->value.tag == BACNET_APPLICATION_TAG_BOOLEAN)
{
object_index = Analog_Output_Instance_To_Index(
wp_data->object_instance);
Analog_Output_Out_Of_Service[object_index] =
wp_data->value.type.Boolean;
status = true;
}
else
{
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_DATA_TYPE;
}
break;
default:
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_WRITE_ACCESS_DENIED;
break;
}
return status;
}
#ifdef TEST
#include <assert.h>
#include <string.h>
#include "ctest.h"
void testAnalogOutput(Test * pTest)
{
uint8_t apdu[MAX_APDU] = { 0 };
int len = 0;
uint32_t len_value = 0;
uint8_t tag_number = 0;
BACNET_OBJECT_TYPE decoded_type = OBJECT_ANALOG_OUTPUT;
uint32_t decoded_instance = 0;
uint32_t instance = 123;
BACNET_ERROR_CLASS error_class;
BACNET_ERROR_CODE error_code;
len = Analog_Output_Encode_Property_APDU(
&apdu[0],
instance,
PROP_OBJECT_IDENTIFIER,
BACNET_ARRAY_ALL,
&error_class,
&error_code);
ct_test(pTest, len != 0);
len = decode_tag_number_and_value(&apdu[0], &tag_number, &len_value);
ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_OBJECT_ID);
len = decode_object_id(&apdu[len],
(int *) &decoded_type, &decoded_instance);
ct_test(pTest, decoded_type == OBJECT_ANALOG_OUTPUT);
ct_test(pTest, decoded_instance == instance);
return;
}
#ifdef TEST_ANALOG_OUTPUT
int main(void)
{
Test *pTest;
bool rc;
pTest = ct_create("BACnet Analog Output", NULL);
/* individual tests */
rc = ct_addTestFunction(pTest, testAnalogOutput);
assert(rc);
ct_setStream(pTest, stdout);
ct_run(pTest);
(void) ct_report(pTest);
ct_destroy(pTest);
return 0;
}
#endif /* TEST_ANALOG_INPUT */
#endif /* TEST */
+64
View File
@@ -0,0 +1,64 @@
/**************************************************************************
*
* 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 AO_H
#define AO_H
#include <stdbool.h>
#include <stdint.h>
#include "bacdef.h"
#include "bacerror.h"
#include "wp.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
bool Analog_Output_Valid_Instance(uint32_t object_instance);
unsigned Analog_Output_Count(void);
uint32_t Analog_Output_Index_To_Instance(unsigned index);
int Analog_Output_Encode_Property_APDU(
uint8_t *apdu,
uint32_t object_instance,
BACNET_PROPERTY_ID property,
int32_t array_index,
BACNET_ERROR_CLASS *error_class,
BACNET_ERROR_CODE *error_code);
bool Analog_Output_Write_Property(
BACNET_WRITE_PROPERTY_DATA *wp_data,
BACNET_ERROR_CLASS *error_class,
BACNET_ERROR_CODE *error_code);
#ifdef TEST
#include "ctest.h"
void testAnalogOutput(Test * pTest);
#endif
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif
+35
View File
@@ -0,0 +1,35 @@
#Makefile to build test case
CC = gcc
BASEDIR = .
#CFLAGS = -Wall -I.
# -g for debugging with gdb
#CFLAGS = -Wall -I. -g
CFLAGS = -Wall -I. -Itest -DTEST -DTEST_ANALOG_OUTPUT -g
# NOTE: this file is normally called by the unittest.sh from up directory
SRCS = bacdcode.c \
bacstr.c \
bigend.c \
demo/object/ao.c \
test/ctest.c
OBJS = ${SRCS:.c=.o}
TARGET = analog_output
all: ${TARGET}
${TARGET}: ${OBJS}
${CC} -o $@ ${OBJS}
.c.o:
${CC} -c ${CFLAGS} $*.c -o $@
depend:
rm -f .depend
${CC} -MM ${CFLAGS} *.c >> .depend
clean:
rm -rf core ${TARGET} $(OBJS) *.bak *.1 *.ini
include: .depend
+344
View File
@@ -0,0 +1,344 @@
/**************************************************************************
*
* 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 <stdlib.h>
#include <string.h>
#include "config.h"
#include "address.h"
#include "bacdef.h"
#include "datalink.h"
#include "bacdcode.h"
#include "npdu.h"
#include "apdu.h"
#include "tsm.h"
#include "device.h"
#include "arf.h"
#include "awf.h"
typedef struct
{
uint32_t instance;
char *filename;
} BACNET_FILE_LISTING;
static BACNET_FILE_LISTING BACnet_File_Listing[] =
{
{0, "test.log"},
{1, "script.txt"},
{0, NULL} // last file indication
};
char *bacfile_name(uint32_t instance)
{
uint32_t index = 0;
char *filename = NULL;
// linear search for file instance match
while (BACnet_File_Listing[index].filename)
{
if (BACnet_File_Listing[index].instance == instance)
{
filename = BACnet_File_Listing[index].filename;
break;
}
index++;
}
return filename;
}
bool bacfile_valid_instance(uint32_t object_instance)
{
return bacfile_name(object_instance) ? true : false;
}
uint32_t bacfile_count(void)
{
uint32_t index = 0;
// linear search for file instance match
while (BACnet_File_Listing[index].filename)
{
index++;
}
return index;
}
uint32_t bacfile_index_to_instance(unsigned find_index)
{
uint32_t instance = BACNET_MAX_INSTANCE + 1;
uint32_t index = 0;
// bounds checking...
while (BACnet_File_Listing[index].filename)
{
if (index == find_index)
{
instance = BACnet_File_Listing[index].instance;
break;
}
index++;
}
return instance;
}
static long fsize(FILE *pFile)
{
long size = 0;
long origin = 0;
if (pFile)
{
origin = ftell(pFile);
fseek(pFile, 0L, SEEK_END);
size = ftell(pFile);
fseek(pFile, origin, SEEK_SET);
}
return (size);
}
static unsigned bacfile_file_size(uint32_t object_instance)
{
char *pFilename = NULL;
FILE *pFile = NULL;
unsigned file_size = 0;
pFilename = bacfile_name(object_instance);
if (pFilename)
{
pFile = fopen(pFilename,"rb");
if (pFile)
{
file_size = fsize(pFile);
fclose(pFile);
}
}
return file_size;
}
int bacfile_encode_property_apdu(
uint8_t *apdu,
uint32_t object_instance,
BACNET_PROPERTY_ID property,
int32_t array_index,
BACNET_ERROR_CLASS *error_class,
BACNET_ERROR_CODE *error_code)
{
int apdu_len = 0; // return value
char text_string[32] = {""};
BACNET_CHARACTER_STRING char_string;
(void)array_index;
switch (property)
{
case PROP_OBJECT_IDENTIFIER:
apdu_len = encode_tagged_object_id(&apdu[0],
OBJECT_FILE,
object_instance);
break;
case PROP_OBJECT_NAME:
sprintf(text_string,"FILE %d",object_instance);
characterstring_init_ansi(&char_string, text_string);
apdu_len = encode_tagged_character_string(&apdu[0],
&char_string);
break;
case PROP_OBJECT_TYPE:
apdu_len = encode_tagged_enumerated(&apdu[0], OBJECT_FILE);
break;
case PROP_DESCRIPTION:
characterstring_init_ansi(&char_string, bacfile_name(object_instance));
apdu_len = encode_tagged_character_string(&apdu[0],
&char_string);
break;
case PROP_FILE_TYPE:
characterstring_init_ansi(&char_string, "TEXT");
apdu_len = encode_tagged_character_string(&apdu[0],
&char_string);
break;
case PROP_FILE_SIZE:
apdu_len = encode_tagged_unsigned(&apdu[0],
bacfile_file_size(object_instance));
break;
case PROP_MODIFICATION_DATE:
// FIXME: get the actual value
apdu_len = encode_tagged_date(&apdu[0],
2005,
12,
25,
7 /* sunday */);
// FIXME: get the actual value
apdu_len += encode_tagged_time(&apdu[apdu_len],
12,
0,
0,
0);
break;
case PROP_ARCHIVE:
// FIXME: get the actual value: note it may be inverse...
apdu_len = encode_tagged_boolean(&apdu[0],true);
break;
case PROP_READ_ONLY:
// FIXME: get the actual value
apdu_len = encode_tagged_boolean(&apdu[0],true);
break;
case PROP_FILE_ACCESS_METHOD:
apdu_len = encode_tagged_enumerated(&apdu[0],FILE_STREAM_ACCESS);
break;
default:
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_UNKNOWN_PROPERTY;
break;
}
return apdu_len;
}
uint32_t bacfile_instance(char *filename)
{
uint32_t index = 0;
uint32_t instance = BACNET_MAX_INSTANCE + 1;
// linear search for filename match
while (BACnet_File_Listing[index].filename)
{
if (strcmp(BACnet_File_Listing[index].filename,filename) == 0)
{
instance = BACnet_File_Listing[index].instance;
break;
}
index++;
}
return instance;
}
// 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
// invokeID and file instance in a list or table
// when the request was sent
uint32_t bacfile_instance_from_tsm(
uint8_t invokeID)
{
BACNET_NPDU_DATA npdu_data = {0}; // dummy for getting npdu length
BACNET_CONFIRMED_SERVICE_DATA service_data = {0};
uint8_t service_choice = 0;
uint8_t *service_request = NULL;
uint16_t service_request_len = 0;
BACNET_ADDRESS dest; // where the original packet was destined
uint8_t pdu[MAX_PDU] = {0}; // original sent packet
uint16_t pdu_len = 0; // original packet length
uint16_t len = 0; // apdu header length
BACNET_ATOMIC_READ_FILE_DATA data = {0};
uint32_t object_instance = BACNET_MAX_INSTANCE + 1; // return value
bool found = false;
int apdu_offset = 0;
found = tsm_get_transaction_pdu(
invokeID,
&dest,
&pdu[0],
&pdu_len);
if (found)
{
apdu_offset = npdu_decode(
&pdu[0], // data to decode
NULL, // destination address - get the DNET/DLEN/DADR if in there
NULL, // source address - get the SNET/SLEN/SADR if in there
&npdu_data); // amount of data to decode
if (!npdu_data.network_layer_message &&
((pdu[apdu_offset] & 0xF0) == PDU_TYPE_CONFIRMED_SERVICE_REQUEST))
{
len = apdu_decode_confirmed_service_request(
&pdu[apdu_offset], // APDU data
pdu_len - apdu_offset,
&service_data,
&service_choice,
&service_request,
&service_request_len);
if (service_choice == SERVICE_CONFIRMED_ATOMIC_READ_FILE)
{
len = arf_decode_service_request(
service_request,
service_request_len,
&data);
if (len > 0)
{
if (data.object_type == OBJECT_FILE)
object_instance = data.object_instance;
}
}
}
}
return object_instance;
}
bool bacfile_read_data(BACNET_ATOMIC_READ_FILE_DATA *data)
{
char *pFilename = NULL;
bool found = false;
FILE *pFile = NULL;
size_t len = 0;
pFilename = bacfile_name(data->object_instance);
if (pFilename)
{
found = true;
pFile = fopen(pFilename,"rb");
if (pFile)
{
(void)fseek(pFile,
data->type.stream.fileStartPosition,
SEEK_SET);
len = fread(octetstring_value(&data->fileData), 1,
data->type.stream.requestedOctetCount, pFile);
if (len < data->type.stream.requestedOctetCount)
data->endOfFile = true;
else
data->endOfFile = false;
octetstring_truncate(&data->fileData,len);
fclose(pFile);
}
else
{
octetstring_truncate(&data->fileData,0);
data->endOfFile = true;
}
}
else
{
octetstring_truncate(&data->fileData,0);
data->endOfFile = true;
}
return found;
}
+77
View File
@@ -0,0 +1,77 @@
/*####COPYRIGHTBEGIN####
-------------------------------------------
Copyright (C) 2005 Steve Karg
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to:
The Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA.
As a special exception, if other files instantiate templates or
use macros or inline functions from this file, or you compile
this file and link it with other works to produce a work based
on this file, this file does not by itself cause the resulting
work to be covered by the GNU General Public License. However
the source code for this file must still be made available in
accordance with section (3) of the GNU General Public License.
This exception does not invalidate any other reasons why a work
based on this file might be covered by the GNU General Public
License.
-------------------------------------------
####COPYRIGHTEND####*/
#ifndef BACFILE_H
#define BACFILE_H
#include <stdbool.h>
#include <stdint.h>
#include "bacdef.h"
#include "bacenum.h"
#include "apdu.h"
#include "arf.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
char *bacfile_name(uint32_t instance);
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);
// 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
// invokeID and file instance in a list or table
// when the request was sent
uint32_t bacfile_instance_from_tsm(
uint8_t invokeID);
// AtomicReadFile ACK helper
bool bacfile_read_data(BACNET_ATOMIC_READ_FILE_DATA *data);
// handling for read property service
int bacfile_encode_property_apdu(
uint8_t *apdu,
uint32_t object_instance,
BACNET_PROPERTY_ID property,
int32_t array_index,
BACNET_ERROR_CLASS *error_class,
BACNET_ERROR_CODE *error_code);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif
+791
View File
@@ -0,0 +1,791 @@
/**************************************************************************
*
* 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 <stdbool.h>
#include <stdint.h>
#include <string.h> /* for memmove*/
#include "bacdef.h"
#include "bacdcode.h"
#include "bacenum.h"
#include "config.h" // the custom stuff
#include "ai.h" // object list dependency
#include "ao.h" // object list dependency
#include "wp.h" // write property handling
#if BACFILE
#include "bacfile.h" // object list dependency
#endif
static uint32_t Object_Instance_Number = 0;
static char Object_Name[16] = "SimpleServer";
static BACNET_DEVICE_STATUS System_Status = STATUS_OPERATIONAL;
static char Vendor_Name[16] = "ASHRAE";
// vendor id assigned by ASHRAE
static uint16_t Vendor_Identifier = 0;
static char Model_Name[16] = "GNU";
static char Firmware_Revision[16] = "1.0";
static char Application_Software_Version[16] = "1.0";
static char Location[16] = "USA";
static char Description[16] = "server";
//static uint8_t Protocol_Version = 1; - constant, not settable
//static uint8_t Protocol_Revision = 4; - constant, not settable
//Protocol_Services_Supported
//Protocol_Object_Types_Supported
//Object_List
//static uint16_t Max_APDU_Length_Accepted = MAX_APDU; - constant
//static BACNET_SEGMENTATION Segmentation_Supported = SEGMENTATION_NONE;
//static uint8_t Max_Segments_Accepted = 0;
//VT_Classes_Supported
//Active_VT_Sessions
//Local_Time - rely on OS, if there is one
//Local_Date - rely on OS, if there is one
//UTC_Offset - rely on OS, if there is one
//Daylight_Savings_Status - rely on OS, if there is one
//APDU_Segment_Timeout
static uint16_t APDU_Timeout = 3000;
static uint8_t Number_Of_APDU_Retries = 3;
//List_Of_Session_Keys
//Time_Synchronization_Recipients
//Max_Master - rely on MS/TP subsystem, if there is one
//Max_Info_Frames - rely on MS/TP subsystem, if there is one
//Device_Address_Binding - required, but relies on binding cache
static uint8_t Database_Revision = 0;
//Configuration_Files
//Last_Restore_Time
//Backup_Failure_Timeout
//Active_COV_Subscriptions
//Slave_Proxy_Enable
//Manual_Slave_Address_Binding
//Auto_Slave_Discovery
//Slave_Address_Binding
//Profile_Name
// methods to manipulate the data
uint32_t Device_Object_Instance_Number(void)
{
return Object_Instance_Number;
}
bool Device_Set_Object_Instance_Number(uint32_t object_id)
{
bool status = true; /* return value */
if (object_id <= BACNET_MAX_INSTANCE)
Object_Instance_Number = object_id;
else
status = false;
return status;
}
bool Device_Valid_Object_Instance_Number(uint32_t object_id)
{
// BACnet allows for a wildcard instance number
return ((Object_Instance_Number == object_id) ||
(object_id == BACNET_MAX_INSTANCE));
}
const char *Device_Object_Name(void)
{
return Object_Name;
}
bool Device_Set_Object_Name(const char *name, size_t length)
{
bool status = false; /*return value*/
if (length < sizeof(Object_Name))
{
memmove(Object_Name,name,length);
Object_Name[length] = 0;
status = true;
}
return status;
}
BACNET_DEVICE_STATUS Device_System_Status(void)
{
return System_Status;
}
void Device_Set_System_Status(BACNET_DEVICE_STATUS status)
{
// FIXME: bounds check?
System_Status = status;
}
const char *Device_Vendor_Name(void)
{
return Vendor_Name;
}
bool Device_Set_Vendor_Name(const char *name, size_t length)
{
bool status = false; /*return value*/
if (length < sizeof(Vendor_Name))
{
memmove(Vendor_Name,name,length);
Vendor_Name[length] = 0;
status = true;
}
return status;
}
uint16_t Device_Vendor_Identifier(void)
{
return Vendor_Identifier;
}
void Device_Set_Vendor_Identifier(uint16_t vendor_id)
{
Vendor_Identifier = vendor_id;
}
const char *Device_Model_Name(void)
{
return Model_Name;
}
bool Device_Set_Model_Name(const char *name, size_t length)
{
bool status = false; /*return value*/
if (length < sizeof(Model_Name))
{
memmove(Model_Name,name,length);
Model_Name[length] = 0;
status = true;
}
return status;
}
const char *Device_Firmware_Revision(void)
{
return Firmware_Revision;
}
bool Device_Set_Firmware_Revision(const char *name, size_t length)
{
bool status = false; /*return value*/
if (length < sizeof(Firmware_Revision))
{
memmove(Firmware_Revision,name,length);
Firmware_Revision[length] = 0;
status = true;
}
return status;
}
const char *Device_Application_Software_Version(void)
{
return Application_Software_Version;
}
bool Device_Set_Application_Software_Version(const char *name, size_t length)
{
bool status = false; /*return value*/
if (length < sizeof(Application_Software_Version))
{
memmove(Application_Software_Version,name,length);
Application_Software_Version[length] = 0;
status = true;
}
return status;
}
const char *Device_Description(void)
{
return Description;
}
bool Device_Set_Description(const char *name, size_t length)
{
bool status = false; /*return value*/
if (length < sizeof(Description))
{
memmove(Description,name,length);
Description[length] = 0;
status = true;
}
return status;
}
const char *Device_Location(void)
{
return Location;
}
bool Device_Set_Location(const char *name, size_t length)
{
bool status = false; /*return value*/
if (length < sizeof(Location))
{
memmove(Location,name,length);
Location[length] = 0;
status = true;
}
return status;
}
uint8_t Device_Protocol_Version(void)
{
return 1;
}
uint8_t Device_Protocol_Revision(void)
{
return 4;
}
uint16_t Device_Max_APDU_Length_Accepted(void)
{
return MAX_APDU;
}
BACNET_SEGMENTATION Device_Segmentation_Supported(void)
{
return SEGMENTATION_NONE;
}
uint16_t Device_APDU_Timeout(void)
{
return APDU_Timeout;
}
// in milliseconds
void Device_Set_APDU_Timeout(uint16_t timeout)
{
APDU_Timeout = timeout;
}
uint8_t Device_Number_Of_APDU_Retries(void)
{
return Number_Of_APDU_Retries;
}
void Device_Set_Number_Of_APDU_Retries(uint8_t retries)
{
Number_Of_APDU_Retries = retries;
}
uint8_t Device_Database_Revision(void)
{
return Database_Revision;
}
void Device_Set_Database_Revision(uint8_t revision)
{
Database_Revision = revision;
}
// Since many network clients depend on the object list
// for discovery, it must be consistent!
unsigned Device_Object_List_Count(void)
{
unsigned count = 1;
count += Analog_Input_Count();
count += Analog_Output_Count();
#if BACFILE
count += bacfile_count();
#endif
return count;
}
bool Device_Object_List_Identifier(unsigned array_index,
int *object_type,
uint32_t *instance)
{
bool status = false;
unsigned object_index = 0;
if (array_index == 1)
{
*object_type = OBJECT_DEVICE;
*instance = Object_Instance_Number;
status = true;
}
if (!status)
{
// array index starts at 1, and 1 for the device object
object_index = array_index - 2;
if (object_index < Analog_Input_Count())
{
*object_type = OBJECT_ANALOG_INPUT;
*instance = Analog_Input_Index_To_Instance(object_index);
status = true;
}
}
if (!status)
{
object_index -= Analog_Input_Count();
if (object_index < Analog_Output_Count())
{
*object_type = OBJECT_ANALOG_OUTPUT;
*instance = Analog_Output_Index_To_Instance(object_index);
status = true;
}
}
#if BACFILE
if (!status)
{
object_index -= Analog_Output_Count();
if (object_index < bacfile_count())
{
*object_type = OBJECT_FILE;
*instance = bacfile_index_to_instance(object_index);
status = true;
}
}
#endif
return status;
}
// return the length of the apdu encoded
int Device_Encode_Property_APDU(
uint8_t *apdu,
BACNET_PROPERTY_ID property,
int32_t array_index,
BACNET_ERROR_CLASS *error_class,
BACNET_ERROR_CODE *error_code)
{
int apdu_len = 0; // return value
int len = 0; // apdu len intermediate value
BACNET_BIT_STRING bit_string;
BACNET_CHARACTER_STRING char_string;
unsigned i = 0;
int object_type = 0;
uint32_t instance = 0;
unsigned count = 0;
switch (property)
{
case PROP_OBJECT_IDENTIFIER:
apdu_len = encode_tagged_object_id(&apdu[0], OBJECT_DEVICE,
Object_Instance_Number);
break;
case PROP_OBJECT_NAME:
characterstring_init_ansi(&char_string, Object_Name);
apdu_len = encode_tagged_character_string(&apdu[0], &char_string);
break;
case PROP_OBJECT_TYPE:
apdu_len = encode_tagged_enumerated(&apdu[0], OBJECT_DEVICE);
break;
case PROP_DESCRIPTION:
characterstring_init_ansi(&char_string, Description);
apdu_len = encode_tagged_character_string(&apdu[0], &char_string);
break;
case PROP_SYSTEM_STATUS:
apdu_len = encode_tagged_enumerated(&apdu[0], System_Status);
break;
case PROP_VENDOR_NAME:
characterstring_init_ansi(&char_string, Vendor_Name);
apdu_len = encode_tagged_character_string(&apdu[0], &char_string);
break;
case PROP_VENDOR_IDENTIFIER:
apdu_len = encode_tagged_unsigned(&apdu[0], Vendor_Identifier);
break;
case PROP_MODEL_NAME:
characterstring_init_ansi(&char_string, Model_Name);
apdu_len = encode_tagged_character_string(&apdu[0], &char_string);
break;
case PROP_FIRMWARE_REVISION:
characterstring_init_ansi(&char_string, Firmware_Revision);
apdu_len = encode_tagged_character_string(&apdu[0], &char_string);
break;
case PROP_APPLICATION_SOFTWARE_VERSION:
characterstring_init_ansi(&char_string, Application_Software_Version);
apdu_len = encode_tagged_character_string(&apdu[0], &char_string);
break;
// if you support time
//case PROP_LOCAL_TIME:
//t = time(NULL);
//my_tm = localtime(&t);
//apdu_len =
// encode_tagged_time(&apdu[0], my_tm->tm_hour, my_tm->tm_min,
// my_tm->tm_sec, 0);
//break;
// if you support date
//case PROP_LOCAL_DATE:
//t = time(NULL);
//my_tm = localtime(&t);
// year = years since 1900
// month 1=Jan
// day = day of month
// wday 1=Monday...7=Sunday
//apdu_len = encode_tagged_date(&apdu[0],
// my_tm->tm_year,
// my_tm->tm_mon + 1,
// my_tm->tm_mday, ((my_tm->tm_wday == 0) ? 7 : my_tm->tm_wday));
//break;
case PROP_PROTOCOL_VERSION:
apdu_len = encode_tagged_unsigned(&apdu[0], Device_Protocol_Version());
break;
// BACnet Legacy Support
case PROP_PROTOCOL_CONFORMANCE_CLASS:
apdu_len = encode_tagged_unsigned(&apdu[0], 1);
break;
case PROP_PROTOCOL_SERVICES_SUPPORTED:
bitstring_init(&bit_string);
for (i = 0; i < MAX_BACNET_SERVICES_SUPPORTED; i++)
{
// bitstring_set_bit(&bit_string, i, apdu_service_supported(i));
// initialize all the services to not-supported
bitstring_set_bit(&bit_string, (uint8_t)i, false);
}
/* FIXME: set only the services that YOUR device executes */
bitstring_set_bit(&bit_string, SERVICE_SUPPORTED_WHO_IS, true);
bitstring_set_bit(&bit_string, SERVICE_SUPPORTED_I_AM, true);
bitstring_set_bit(&bit_string, SERVICE_SUPPORTED_READ_PROPERTY, true);
apdu_len = encode_tagged_bitstring(&apdu[0], &bit_string);
break;
case PROP_PROTOCOL_OBJECT_TYPES_SUPPORTED:
bitstring_init(&bit_string);
for (i = 0; i < MAX_BACNET_OBJECT_TYPES; i++)
{
// initialize all the object types to not-supported
bitstring_set_bit(&bit_string, (uint8_t)i, false);
}
/* FIXME: indicate the objects that YOU support */
bitstring_set_bit(&bit_string, OBJECT_DEVICE, true);
bitstring_set_bit(&bit_string, OBJECT_ANALOG_INPUT, true);
bitstring_set_bit(&bit_string, OBJECT_ANALOG_OUTPUT, true);
apdu_len = encode_tagged_bitstring(&apdu[0], &bit_string);
break;
case PROP_OBJECT_LIST:
count = Device_Object_List_Count();
// Array element zero is the number of objects in the list
if (array_index == BACNET_ARRAY_LENGTH_INDEX)
apdu_len = encode_tagged_unsigned(&apdu[0], count);
// if no index was specified, then try to encode the entire list
// into one packet. Note that more than likely you will have
// to return an error if the number of encoded objects exceeds
// your maximum APDU size.
else if (array_index == BACNET_ARRAY_ALL)
{
for (i = 1; i <= count; i++)
{
if (Device_Object_List_Identifier(i,&object_type,&instance))
{
len = encode_tagged_object_id(&apdu[apdu_len], object_type,
instance);
apdu_len += len;
// assume next one is the same size as this one
// can we all fit into the APDU?
if ((apdu_len + len) >= MAX_APDU)
{
*error_class = ERROR_CLASS_SERVICES;
*error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT;
apdu_len = 0;
break;
}
}
else
{
// error: internal error?
*error_class = ERROR_CLASS_SERVICES;
*error_code = ERROR_CODE_OTHER;
apdu_len = 0;
break;
}
}
}
else
{
if (Device_Object_List_Identifier(array_index,&object_type,&instance))
apdu_len = encode_tagged_object_id(&apdu[0], object_type, instance);
else
{
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_ARRAY_INDEX;
}
}
break;
case PROP_MAX_APDU_LENGTH_ACCEPTED:
apdu_len = encode_tagged_unsigned(&apdu[0],
Device_Max_APDU_Length_Accepted());
break;
case PROP_SEGMENTATION_SUPPORTED:
apdu_len = encode_tagged_enumerated(&apdu[0],
Device_Segmentation_Supported());
break;
case PROP_APDU_TIMEOUT:
apdu_len = encode_tagged_unsigned(&apdu[0], APDU_Timeout);
break;
case PROP_NUMBER_OF_APDU_RETRIES:
apdu_len = encode_tagged_unsigned(&apdu[0], Number_Of_APDU_Retries);
break;
default:
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_UNKNOWN_PROPERTY;
break;
}
return apdu_len;
}
// we can send an I-Am when our device ID changes
extern bool I_Am_Request;
// returns true if successful
bool Device_Write_Property(
BACNET_WRITE_PROPERTY_DATA *wp_data,
BACNET_ERROR_CLASS *error_class,
BACNET_ERROR_CODE *error_code)
{
bool status = false; // return value
if (!Device_Valid_Object_Instance_Number(wp_data->object_instance))
{
*error_class = ERROR_CLASS_OBJECT;
*error_code = ERROR_CODE_UNKNOWN_OBJECT;
return false;
}
// decode the some of the request
switch (wp_data->object_property)
{
case PROP_OBJECT_IDENTIFIER:
if (wp_data->value.tag == BACNET_APPLICATION_TAG_OBJECT_ID)
{
if ((wp_data->value.type.Object_Id.type == OBJECT_DEVICE) &&
(Device_Set_Object_Instance_Number(
wp_data->value.type.Object_Id.instance)))
{
I_Am_Request = true;
status = true;
}
else
{
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_VALUE_OUT_OF_RANGE;
}
}
else
{
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_DATA_TYPE;
}
break;
case PROP_NUMBER_OF_APDU_RETRIES:
if (wp_data->value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT)
{
Device_Set_Number_Of_APDU_Retries(wp_data->value.type.Unsigned_Int);
status = true;
}
else
{
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_DATA_TYPE;
}
break;
case PROP_APDU_TIMEOUT:
if (wp_data->value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT)
{
Device_Set_APDU_Timeout(wp_data->value.type.Unsigned_Int);
status = true;
}
else
{
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_DATA_TYPE;
}
break;
case PROP_VENDOR_IDENTIFIER:
if (wp_data->value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT)
{
Device_Set_Vendor_Identifier(wp_data->value.type.Unsigned_Int);
status = true;
}
else
{
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_DATA_TYPE;
}
break;
case PROP_SYSTEM_STATUS:
if (wp_data->value.tag == BACNET_APPLICATION_TAG_ENUMERATED)
{
Device_Set_System_Status(wp_data->value.type.Enumerated);
status = true;
}
else
{
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_DATA_TYPE;
}
break;
case PROP_OBJECT_NAME:
if (wp_data->value.tag == BACNET_APPLICATION_TAG_CHARACTER_STRING)
{
uint8_t encoding;
encoding = characterstring_encoding(&wp_data->value.type.Character_String);
if (encoding == CHARACTER_ANSI_X34)
{
status = Device_Set_Object_Name(
characterstring_value(&wp_data->value.type.Character_String),
characterstring_length(&wp_data->value.type.Character_String));
if (!status)
{
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_NO_SPACE_TO_WRITE_PROPERTY;
}
}
else
{
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_CHARACTER_SET_NOT_SUPPORTED;
}
}
else
{
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_INVALID_DATA_TYPE;
}
break;
default:
*error_class = ERROR_CLASS_PROPERTY;
*error_code = ERROR_CODE_WRITE_ACCESS_DENIED;
break;
}
return status;
}
#ifdef TEST
#include <assert.h>
#include <string.h>
#include "ctest.h"
void testDevice(Test * pTest)
{
bool status = false;
const char *name = "Patricia";
status = Device_Set_Object_Instance_Number(0);
ct_test(pTest, Device_Object_Instance_Number() == 0);
ct_test(pTest, status == true);
status = Device_Set_Object_Instance_Number(BACNET_MAX_INSTANCE);
ct_test(pTest, Device_Object_Instance_Number() == BACNET_MAX_INSTANCE);
ct_test(pTest, status == true);
status = Device_Set_Object_Instance_Number(BACNET_MAX_INSTANCE/2);
ct_test(pTest, Device_Object_Instance_Number() == (BACNET_MAX_INSTANCE/2));
ct_test(pTest, status == true);
status = Device_Set_Object_Instance_Number(BACNET_MAX_INSTANCE+1);
ct_test(pTest, Device_Object_Instance_Number() != (BACNET_MAX_INSTANCE+1));
ct_test(pTest, status == false);
Device_Set_System_Status(STATUS_NON_OPERATIONAL);
ct_test(pTest, Device_System_Status() == STATUS_NON_OPERATIONAL);
Device_Set_Vendor_Name(name,strlen(name));
ct_test(pTest, strcmp(Device_Vendor_Name(),name) == 0);
Device_Set_Vendor_Identifier(42);
ct_test(pTest, Device_Vendor_Identifier() == 42);
Device_Set_Model_Name(name,strlen(name));
ct_test(pTest, strcmp(Device_Model_Name(),name) == 0);
return;
}
#ifdef TEST_DEVICE
// stubs to dependencies to keep unit test simple
unsigned Analog_Input_Count(void)
{
return 0;
}
uint32_t Analog_Input_Index_To_Instance(unsigned index)
{
return index;
}
unsigned Analog_Output_Count(void)
{
return 0;
}
uint32_t Analog_Output_Index_To_Instance(unsigned index)
{
return index;
}
uint32_t bacfile_count(void)
{
return 0;
}
uint32_t bacfile_index_to_instance(unsigned find_index)
{
return find_index;
}
bool I_Am_Request = false;
int main(void)
{
Test *pTest;
bool rc;
pTest = ct_create("BACnet Device", NULL);
/* individual tests */
rc = ct_addTestFunction(pTest, testDevice);
assert(rc);
ct_setStream(pTest, stdout);
ct_run(pTest);
(void) ct_report(pTest);
ct_destroy(pTest);
return 0;
}
#endif /* TEST_DEVICE */
#endif /* TEST */
+111
View File
@@ -0,0 +1,111 @@
/*####COPYRIGHTBEGIN####
-------------------------------------------
Copyright (C) 2005 Steve Karg
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to:
The Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA.
As a special exception, if other files instantiate templates or
use macros or inline functions from this file, or you compile
this file and link it with other works to produce a work based
on this file, this file does not by itself cause the resulting
work to be covered by the GNU General Public License. However
the source code for this file must still be made available in
accordance with section (3) of the GNU General Public License.
This exception does not invalidate any other reasons why a work
based on this file might be covered by the GNU General Public
License.
-------------------------------------------
####COPYRIGHTEND####*/
#ifndef DEVICE_H
#define DEVICE_H
#include <stdbool.h>
#include <stdint.h>
#include "bacdef.h"
#include "bacenum.h"
#include "wp.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
uint32_t Device_Object_Instance_Number(void);
bool Device_Set_Object_Instance_Number(uint32_t object_id);
bool Device_Valid_Object_Instance_Number(uint32_t object_id);
unsigned Device_Object_List_Count(void);
bool Device_Object_List_Identifier(unsigned array_index,
int *object_type,
uint32_t *instance);
BACNET_DEVICE_STATUS Device_System_Status(void);
void Device_Set_System_Status(BACNET_DEVICE_STATUS status);
const char *Device_Vendor_Name(void);
bool Device_Set_Vendor_Name(const char *name, size_t length);
uint16_t Device_Vendor_Identifier(void);
void Device_Set_Vendor_Identifier(uint16_t vendor_id);
const char *Device_Model_Name(void);
bool Device_Set_Model_Name(const char *name, size_t length);
const char *Device_Firmware_Revision(void);
bool Device_Set_Firmware_Revision(const char *name, size_t length);
const char *Device_Application_Software_Version(void);
bool Device_Set_Application_Software_Version(const char *name, size_t length);
const char *Device_Description(void);
bool Device_Set_Description(const char *name, size_t length);
const char *Device_Location(void);
bool Device_Set_Location(const char *name, size_t length);
// some stack-centric constant values - no set methods
uint8_t Device_Protocol_Version(void);
uint8_t Device_Protocol_Revision(void);
uint16_t Device_Max_APDU_Length_Accepted(void);
BACNET_SEGMENTATION Device_Segmentation_Supported(void);
uint16_t Device_APDU_Timeout(void);
void Device_Set_APDU_Timeout(uint16_t timeout);
uint8_t Device_Number_Of_APDU_Retries(void);
void Device_Set_Number_Of_APDU_Retries(uint8_t retries);
uint8_t Device_Database_Revision(void);
void Device_Set_Database_Revision(uint8_t revision);
int Device_Encode_Property_APDU(
uint8_t *apdu,
BACNET_PROPERTY_ID property,
int32_t array_index,
BACNET_ERROR_CLASS *error_class,
BACNET_ERROR_CODE *error_code);
bool Device_Write_Property(
BACNET_WRITE_PROPERTY_DATA *wp_data,
BACNET_ERROR_CLASS *error_class,
BACNET_ERROR_CODE *error_code);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif
+35
View File
@@ -0,0 +1,35 @@
#Makefile to build test case
CC = gcc
BASEDIR = .
#CFLAGS = -Wall -I.
# -g for debugging with gdb
#CFLAGS = -Wall -I. -g
CFLAGS = -Wall -I. -Itest -DTEST -DTEST_DEVICE -g
# NOTE: this file is normally called by the unittest.sh from up directory
SRCS = bacdcode.c \
bigend.c \
bacstr.c \
demo/object/device.c \
test/ctest.c
OBJS = ${SRCS:.c=.o}
TARGET = device
all: ${TARGET}
${TARGET}: ${OBJS}
${CC} -o $@ ${OBJS}
.c.o:
${CC} -c ${CFLAGS} $*.c -o $@
depend:
rm -f .depend
${CC} -MM ${CFLAGS} *.c >> .depend
clean:
rm -rf core ${TARGET} $(OBJS) *.bak *.1 *.ini
include: .depend