Added init functions to object header files. Finished refactoring the demo read and write property handlers to use function tables.

This commit is contained in:
skarg
2009-08-23 03:26:30 +00:00
parent 3dc3da5735
commit a28760bf56
37 changed files with 449 additions and 1133 deletions
+10 -6
View File
@@ -40,7 +40,7 @@
static float Present_Value[MAX_ANALOG_INPUTS];
/* These three arrays are used by the ReadPropertyMultiple handler */
static const int Analog_Input_Properties_Required[] = {
static const int Properties_Required[] = {
PROP_OBJECT_IDENTIFIER,
PROP_OBJECT_NAME,
PROP_OBJECT_TYPE,
@@ -52,12 +52,12 @@ static const int Analog_Input_Properties_Required[] = {
-1
};
static const int Analog_Input_Properties_Optional[] = {
static const int Properties_Optional[] = {
PROP_DESCRIPTION,
-1
};
static const int Analog_Input_Properties_Proprietary[] = {
static const int Properties_Proprietary[] = {
9997,
9998,
9999,
@@ -70,11 +70,11 @@ void Analog_Input_Property_Lists(
const int **pProprietary)
{
if (pRequired)
*pRequired = Analog_Input_Properties_Required;
*pRequired = Properties_Required;
if (pOptional)
*pOptional = Analog_Input_Properties_Optional;
*pOptional = Properties_Optional;
if (pProprietary)
*pProprietary = Analog_Input_Properties_Proprietary;
*pProprietary = Properties_Proprietary;
return;
}
@@ -217,6 +217,10 @@ int Analog_Input_Encode_Property_APDU(
return apdu_len;
}
void Analog_Input_Init(void)
{
}
#ifdef TEST
#include <assert.h>
#include <string.h>
+6 -14
View File
@@ -57,7 +57,7 @@ static bool Analog_Output_Out_Of_Service[MAX_ANALOG_OUTPUTS];
static bool Analog_Output_Initialized = false;
/* These three arrays are used by the ReadPropertyMultiple handler */
static const int Analog_Output_Properties_Required[] = {
static const int Properties_Required[] = {
PROP_OBJECT_IDENTIFIER,
PROP_OBJECT_NAME,
PROP_OBJECT_TYPE,
@@ -71,12 +71,12 @@ static const int Analog_Output_Properties_Required[] = {
-1
};
static const int Analog_Output_Properties_Optional[] = {
static const int Properties_Optional[] = {
PROP_DESCRIPTION,
-1
};
static const int Analog_Output_Properties_Proprietary[] = {
static const int Properties_Proprietary[] = {
-1
};
@@ -86,11 +86,11 @@ void Analog_Output_Property_Lists(
const int **pProprietary)
{
if (pRequired)
*pRequired = Analog_Output_Properties_Required;
*pRequired = Properties_Required;
if (pOptional)
*pOptional = Analog_Output_Properties_Optional;
*pOptional = Properties_Optional;
if (pProprietary)
*pProprietary = Analog_Output_Properties_Proprietary;
*pProprietary = Properties_Proprietary;
return;
}
@@ -120,7 +120,6 @@ void Analog_Output_Init(
bool Analog_Output_Valid_Instance(
uint32_t object_instance)
{
Analog_Output_Init();
if (object_instance < MAX_ANALOG_OUTPUTS)
return true;
@@ -132,7 +131,6 @@ bool Analog_Output_Valid_Instance(
unsigned Analog_Output_Count(
void)
{
Analog_Output_Init();
return MAX_ANALOG_OUTPUTS;
}
@@ -142,7 +140,6 @@ unsigned Analog_Output_Count(
uint32_t Analog_Output_Index_To_Instance(
unsigned index)
{
Analog_Output_Init();
return index;
}
@@ -154,7 +151,6 @@ unsigned Analog_Output_Instance_To_Index(
{
unsigned index = MAX_ANALOG_OUTPUTS;
Analog_Output_Init();
if (object_instance < MAX_ANALOG_OUTPUTS)
index = object_instance;
@@ -168,7 +164,6 @@ float Analog_Output_Present_Value(
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_PRIORITY; i++) {
@@ -189,7 +184,6 @@ unsigned Analog_Output_Present_Value_Priority(
unsigned i = 0; /* loop counter */
unsigned priority = 0; /* return value */
Analog_Output_Init();
index = Analog_Output_Instance_To_Index(object_instance);
if (index < MAX_ANALOG_OUTPUTS) {
for (i = 0; i < BACNET_MAX_PRIORITY; i++) {
@@ -287,7 +281,6 @@ int Analog_Output_Encode_Property_APDU(
unsigned i = 0;
bool state = false;
Analog_Output_Init();
switch (property) {
case PROP_OBJECT_IDENTIFIER:
apdu_len =
@@ -405,7 +398,6 @@ bool Analog_Output_Write_Property(
int len = 0;
BACNET_APPLICATION_DATA_VALUE value;
Analog_Output_Init();
if (!Analog_Output_Valid_Instance(wp_data->object_instance)) {
*error_class = ERROR_CLASS_OBJECT;
*error_code = ERROR_CODE_UNKNOWN_OBJECT;
+4
View File
@@ -462,3 +462,7 @@ bool bacfile_write_stream_data(
return found;
}
void bacfile_init(void)
{
}
-8
View File
@@ -140,7 +140,6 @@ unsigned Binary_Input_Instance_To_Index(
{
unsigned index = MAX_BINARY_INPUTS;
Binary_Input_Init();
if (object_instance < MAX_BINARY_INPUTS) {
index = object_instance;
}
@@ -154,7 +153,6 @@ static BACNET_BINARY_PV Binary_Input_Present_Value(
BACNET_BINARY_PV value = BINARY_INACTIVE;
unsigned index = 0;
Binary_Input_Init();
index = Binary_Input_Instance_To_Index(object_instance);
if (index < MAX_BINARY_INPUTS) {
value = Present_Value[index];
@@ -169,7 +167,6 @@ static bool Binary_Input_Out_Of_Service(
bool value = false;
unsigned index = 0;
Binary_Input_Init();
index = Binary_Input_Instance_To_Index(object_instance);
if (index < MAX_BINARY_INPUTS) {
value = Out_Of_Service[index];
@@ -248,7 +245,6 @@ static void Binary_Input_Present_Value_Set(
{
unsigned index = 0;
Binary_Input_Init();
index = Binary_Input_Instance_To_Index(object_instance);
if (index < MAX_BINARY_INPUTS) {
if (Present_Value[index] != value) {
@@ -266,7 +262,6 @@ static void Binary_Input_Out_Of_Service_Set(
{
unsigned index = 0;
Binary_Input_Init();
index = Binary_Input_Instance_To_Index(object_instance);
if (index < MAX_BINARY_INPUTS) {
if (Out_Of_Service[index] != value) {
@@ -283,7 +278,6 @@ char *Binary_Input_Name(
{
static char text_string[32] = ""; /* okay for single thread */
Binary_Input_Init();
if (object_instance < MAX_BINARY_INPUTS) {
sprintf(text_string, "BINARY INPUT %u", object_instance);
return text_string;
@@ -308,7 +302,6 @@ int Binary_Input_Encode_Property_APDU(
BACNET_POLARITY polarity = POLARITY_NORMAL;
(void) array_index;
Binary_Input_Init();
switch (property) {
case PROP_OBJECT_IDENTIFIER:
apdu_len =
@@ -381,7 +374,6 @@ bool Binary_Input_Write_Property(
int len = 0;
BACNET_APPLICATION_DATA_VALUE value;
Binary_Input_Init();
if (!Binary_Input_Valid_Instance(wp_data->object_instance)) {
*error_class = ERROR_CLASS_OBJECT;
*error_code = ERROR_CODE_UNKNOWN_OBJECT;
+78 -418
View File
@@ -32,15 +32,6 @@
#include "bacapp.h"
#include "config.h" /* the custom stuff */
#include "apdu.h"
#include "ai.h" /* object list dependency */
#include "ao.h" /* object list dependency */
#include "av.h" /* object list dependency */
#include "bi.h" /* object list dependency */
#include "bo.h" /* object list dependency */
#include "bv.h" /* object list dependency */
#include "lc.h" /* object list dependency */
#include "lsp.h" /* object list dependency */
#include "mso.h" /* object list dependency */
#include "wp.h" /* write property handling */
#include "version.h"
#include "device.h" /* me */
@@ -50,6 +41,26 @@
#include "bacfile.h" /* object list dependency */
#endif
static object_count_function
Object_Count[MAX_BACNET_OBJECT_TYPE];
static object_index_to_instance_function
Object_Index_To_Instance[MAX_BACNET_OBJECT_TYPE];
static object_name_function
Object_Name[MAX_BACNET_OBJECT_TYPE];
void Device_Object_Function_Set(
BACNET_OBJECT_TYPE object_type,
object_count_function count_function,
object_index_to_instance_function index_function,
object_name_function name_function)
{
if (object_type < MAX_BACNET_OBJECT_TYPE) {
Object_Count[object_type] = count_function;
Object_Index_To_Instance[object_type] = index_function;
Object_Name[object_type] = name_function;
}
}
/* These three arrays are used by the ReadPropertyMultiple handler */
static const int Device_Properties_Required[] = {
PROP_OBJECT_IDENTIFIER,
@@ -114,7 +125,7 @@ void Device_Property_Lists(
The properties that are constant can be hard coded
into the read-property encoding. */
static uint32_t Object_Instance_Number = 260001;
static char Object_Name[16] = "SimpleServer";
static char My_Object_Name[16] = "SimpleServer";
static BACNET_DEVICE_STATUS System_Status = STATUS_OPERATIONAL;
static char *Vendor_Name = BACNET_VENDOR_NAME;
static uint16_t Vendor_Identifier = BACNET_VENDOR_ID;
@@ -186,7 +197,7 @@ bool Device_Valid_Object_Instance_Number(
const char *Device_Object_Name(
void)
{
return Object_Name;
return My_Object_Name;
}
bool Device_Set_Object_Name(
@@ -200,7 +211,7 @@ bool Device_Set_Object_Name(
the device. */
if (length < sizeof(Object_Name)) {
memmove(Object_Name, name, length);
Object_Name[length] = 0;
My_Object_Name[length] = 0;
status = true;
}
@@ -363,20 +374,14 @@ void Device_Set_Database_Revision(
unsigned Device_Object_List_Count(
void)
{
unsigned count = 1;
count += Analog_Input_Count();
count += Analog_Output_Count();
count += Analog_Value_Count();
count += Binary_Input_Count();
count += Binary_Output_Count();
count += Binary_Value_Count();
count += Life_Safety_Point_Count();
count += Load_Control_Count();
count += Multistate_Output_Count();
#if defined(BACFILE)
count += bacfile_count();
#endif
unsigned count = 1; /* 1 for the device object */
unsigned i = 0; /* loop counter */
for (i = 0; i < MAX_BACNET_OBJECT_TYPE; i++) {
if (Object_Count[i]) {
count += Object_Count[i]();
}
}
return count;
}
@@ -388,144 +393,38 @@ bool Device_Object_List_Identifier(
{
bool status = false;
unsigned object_index = 0;
unsigned object_count = 0;
unsigned count = 0;
unsigned i = 0; /* loop counter */
/* device object */
if (array_index == 0) {
return status;
}
/* device object */
if (array_index == 1) {
*object_type = OBJECT_DEVICE;
*instance = Object_Instance_Number;
status = true;
}
/* analog input objects */
if (!status) {
/* array index starts at 1, and 1 for the device object */
/* array index starts at 1, and if we are this far,
we are not the device object, so array_index must
be at least 2. */
object_index = array_index - 2;
object_count = Analog_Input_Count();
if (object_index < object_count) {
*object_type = OBJECT_ANALOG_INPUT;
*instance = Analog_Input_Index_To_Instance(object_index);
status = true;
/* look through the objects to find the right index */
for (i = 0; i < MAX_BACNET_OBJECT_TYPE; i++) {
if (Object_Count[i] && Object_Index_To_Instance[i]) {
object_index -= count;
count = Object_Count[i]();
if (object_index < count) {
*object_type = i;
*instance = Object_Index_To_Instance[i](object_index);
status = true;
break;
}
}
}
}
/* analog output objects */
if (!status) {
/* normalize the index since
we know it is not the previous objects */
object_index -= object_count;
object_count = Analog_Output_Count();
/* is it a valid index for this object? */
if (object_index < object_count) {
*object_type = OBJECT_ANALOG_OUTPUT;
*instance = Analog_Output_Index_To_Instance(object_index);
status = true;
}
}
/* analog value objects */
if (!status) {
/* normalize the index since
we know it is not the previous objects */
object_index -= object_count;
object_count = Analog_Value_Count();
/* is it a valid index for this object? */
if (object_index < object_count) {
*object_type = OBJECT_ANALOG_VALUE;
*instance = Analog_Value_Index_To_Instance(object_index);
status = true;
}
}
/* binary input objects */
if (!status) {
/* normalize the index since
we know it is not the previous objects */
object_index -= object_count;
object_count = Binary_Input_Count();
/* is it a valid index for this object? */
if (object_index < object_count) {
*object_type = OBJECT_BINARY_INPUT;
*instance = Binary_Input_Index_To_Instance(object_index);
status = true;
}
}
/* binary output objects */
if (!status) {
/* normalize the index since
we know it is not the previous objects */
object_index -= object_count;
object_count = Binary_Output_Count();
/* is it a valid index for this object? */
if (object_index < object_count) {
*object_type = OBJECT_BINARY_OUTPUT;
*instance = Binary_Output_Index_To_Instance(object_index);
status = true;
}
}
/* binary value objects */
if (!status) {
/* normalize the index since
we know it is not the previous objects */
object_index -= object_count;
object_count = Binary_Value_Count();
/* is it a valid index for this object? */
if (object_index < object_count) {
*object_type = OBJECT_BINARY_VALUE;
*instance = Binary_Value_Index_To_Instance(object_index);
status = true;
}
}
/* life safety point objects */
if (!status) {
/* normalize the index since
we know it is not the previous objects */
object_index -= object_count;
object_count = Life_Safety_Point_Count();
/* is it a valid index for this object? */
if (object_index < object_count) {
*object_type = OBJECT_LIFE_SAFETY_POINT;
*instance = Life_Safety_Point_Index_To_Instance(object_index);
status = true;
}
}
/* load control objects */
if (!status) {
/* normalize the index since
we know it is not the previous objects */
object_index -= object_count;
object_count = Load_Control_Count();
/* is it a valid index for this object? */
if (object_index < object_count) {
*object_type = OBJECT_LOAD_CONTROL;
*instance = Load_Control_Index_To_Instance(object_index);
status = true;
}
}
/* multi-state output objects */
if (!status) {
/* normalize the index since
we know it is not the previous objects */
object_index -= object_count;
object_count = Multistate_Output_Count();
/* is it a valid index for this object? */
if (object_index < object_count) {
*object_type = OBJECT_MULTI_STATE_OUTPUT;
*instance = Multistate_Output_Index_To_Instance(object_index);
status = true;
}
}
/* file objects */
#if defined(BACFILE)
if (!status) {
/* normalize the index since
we know it is not the previous objects */
object_index -= object_count;
object_count = bacfile_count();
/* is it a valid index for this object? */
if (object_index < object_count) {
*object_type = OBJECT_FILE;
*instance = bacfile_index_to_instance(object_index);
status = true;
}
}
#endif
return status;
}
@@ -549,10 +448,12 @@ bool Device_Valid_Object_Name(
name = Device_Valid_Object_Id(type, instance);
if (strcmp(name, object_name) == 0) {
found = true;
if (object_type)
if (object_type) {
*object_type = type;
if (object_instance)
}
if (object_instance) {
*object_instance = instance;
}
break;
}
}
@@ -567,46 +468,18 @@ char *Device_Valid_Object_Id(
uint32_t object_instance)
{
char *name = NULL; /* return value */
object_name_function name_function = NULL;
switch (object_type) {
case OBJECT_ANALOG_INPUT:
name = Analog_Input_Name(object_instance);
break;
case OBJECT_ANALOG_OUTPUT:
name = Analog_Output_Name(object_instance);
break;
case OBJECT_ANALOG_VALUE:
name = Analog_Value_Name(object_instance);
break;
case OBJECT_BINARY_INPUT:
name = Binary_Input_Name(object_instance);
break;
case OBJECT_BINARY_OUTPUT:
name = Binary_Output_Name(object_instance);
break;
case OBJECT_BINARY_VALUE:
name = Binary_Value_Name(object_instance);
break;
case OBJECT_LIFE_SAFETY_POINT:
name = Life_Safety_Point_Name(object_instance);
break;
case OBJECT_LOAD_CONTROL:
name = Load_Control_Name(object_instance);
break;
case OBJECT_MULTI_STATE_OUTPUT:
name = Multistate_Output_Name(object_instance);
break;
#if defined(BACFILE)
case OBJECT_FILE:
name = bacfile_name(object_instance);
break;
#endif
case OBJECT_DEVICE:
if (object_instance == Object_Instance_Number)
name = Object_Name;
break;
default:
break;
if (object_type < MAX_BACNET_OBJECT_TYPE) {
name_function = Object_Name[object_type];
}
if (name_function) {
name = name_function(object_instance);
} else {
if ((object_type == OBJECT_DEVICE) &&
(object_instance == Object_Instance_Number)) {
name = My_Object_Name;
}
}
return name;
@@ -616,6 +489,7 @@ char *Device_Valid_Object_Id(
-2 for abort message */
int Device_Encode_Property_APDU(
uint8_t * apdu,
uint32_t object_instance,
BACNET_PROPERTY_ID property,
int32_t array_index,
BACNET_ERROR_CLASS * error_class,
@@ -629,7 +503,8 @@ int Device_Encode_Property_APDU(
int object_type = 0;
uint32_t instance = 0;
unsigned count = 0;
object_instance = object_instance;
switch (property) {
case PROP_OBJECT_IDENTIFIER:
apdu_len =
@@ -637,7 +512,7 @@ int Device_Encode_Property_APDU(
Object_Instance_Number);
break;
case PROP_OBJECT_NAME:
characterstring_init_ansi(&char_string, Object_Name);
characterstring_init_ansi(&char_string, My_Object_Name);
apdu_len =
encode_application_character_string(&apdu[0], &char_string);
break;
@@ -737,34 +612,13 @@ int Device_Encode_Property_APDU(
not a list of objects that this device can access */
bitstring_init(&bit_string);
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);
if ((i == OBJECT_DEVICE) || Object_Count[i]) {
bitstring_set_bit(&bit_string, i, true);
} else {
/* 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);
if (Analog_Input_Count())
bitstring_set_bit(&bit_string, OBJECT_ANALOG_INPUT, true);
if (Analog_Output_Count())
bitstring_set_bit(&bit_string, OBJECT_ANALOG_OUTPUT, true);
if (Analog_Value_Count())
bitstring_set_bit(&bit_string, OBJECT_ANALOG_VALUE, true);
if (Binary_Input_Count())
bitstring_set_bit(&bit_string, OBJECT_BINARY_INPUT, true);
if (Binary_Output_Count())
bitstring_set_bit(&bit_string, OBJECT_BINARY_OUTPUT, true);
if (Binary_Value_Count())
bitstring_set_bit(&bit_string, OBJECT_BINARY_VALUE, true);
if (Life_Safety_Point_Count())
bitstring_set_bit(&bit_string, OBJECT_LIFE_SAFETY_POINT, true);
if (Load_Control_Count())
bitstring_set_bit(&bit_string, OBJECT_LOAD_CONTROL, true);
if (Multistate_Output_Count())
bitstring_set_bit(&bit_string, OBJECT_MULTI_STATE_OUTPUT,
true);
#if defined(BACFILE)
if (bacfile_count())
bitstring_set_bit(&bit_string, OBJECT_FILE, true);
#endif
apdu_len = encode_application_bitstring(&apdu[0], &bit_string);
break;
case PROP_OBJECT_LIST:
@@ -1004,12 +858,6 @@ bool Device_Write_Property(
void Device_Init(void)
{
handler_write_property_object_set(
OBJECT_DEVICE,
Device_Write_Property);
handler_read_property_object_set(
OBJECT_DEVICE,
Device_Encode_Property_APDU);
}
#ifdef TEST
@@ -1051,194 +899,6 @@ void testDevice(
}
#ifdef TEST_DEVICE
/* stubs to dependencies to keep unit test simple */
char *Analog_Input_Name(
uint32_t object_instance)
{
(void) object_instance;
return "";
}
unsigned Analog_Input_Count(
void)
{
return 0;
}
uint32_t Analog_Input_Index_To_Instance(
unsigned index)
{
return index;
}
char *Analog_Output_Name(
uint32_t object_instance)
{
(void) object_instance;
return "";
}
unsigned Analog_Output_Count(
void)
{
return 0;
}
uint32_t Analog_Output_Index_To_Instance(
unsigned index)
{
return index;
}
char *Analog_Value_Name(
uint32_t object_instance)
{
(void) object_instance;
return "";
}
unsigned Analog_Value_Count(
void)
{
return 0;
}
uint32_t Analog_Value_Index_To_Instance(
unsigned index)
{
return index;
}
char *Binary_Input_Name(
uint32_t object_instance)
{
(void) object_instance;
return "";
}
unsigned Binary_Input_Count(
void)
{
return 0;
}
uint32_t Binary_Input_Index_To_Instance(
unsigned index)
{
return index;
}
char *Binary_Output_Name(
uint32_t object_instance)
{
(void) object_instance;
return "";
}
unsigned Binary_Output_Count(
void)
{
return 0;
}
uint32_t Binary_Output_Index_To_Instance(
unsigned index)
{
return index;
}
char *Binary_Value_Name(
uint32_t object_instance)
{
(void) object_instance;
return "";
}
unsigned Binary_Value_Count(
void)
{
return 0;
}
uint32_t Binary_Value_Index_To_Instance(
unsigned index)
{
return index;
}
char *Life_Safety_Point_Name(
uint32_t object_instance)
{
(void) object_instance;
return "";
}
unsigned Life_Safety_Point_Count(
void)
{
return 0;
}
uint32_t Life_Safety_Point_Index_To_Instance(
unsigned index)
{
return index;
}
char *Load_Control_Name(
uint32_t object_instance)
{
(void) object_instance;
return "";
}
unsigned Load_Control_Count(
void)
{
return 0;
}
uint32_t Load_Control_Index_To_Instance(
unsigned index)
{
return index;
}
char *Multistate_Output_Name(
uint32_t object_instance)
{
(void) object_instance;
return "";
}
unsigned Multistate_Output_Count(
void)
{
return 0;
}
uint32_t Multistate_Output_Index_To_Instance(
unsigned index)
{
return index;
}
#if defined(BACFILE)
uint32_t bacfile_count(
void)
{
return 0;
}
#endif
#if defined(BACFILE)
uint32_t bacfile_index_to_instance(
unsigned find_index)
{
return find_index;
}
#endif
int main(
void)
{