diff --git a/src/bacnet/basic/bbmd6/vmac.c b/src/bacnet/basic/bbmd6/vmac.c index 5a69b65b..b8e8be64 100644 --- a/src/bacnet/basic/bbmd6/vmac.c +++ b/src/bacnet/basic/bbmd6/vmac.c @@ -241,10 +241,7 @@ bool VMAC_Find_By_Data(struct vmac_data *vmac, uint32_t *device_id) list_vmac = Keylist_Data_Index(VMAC_List, index); if (list_vmac) { if (VMAC_Match(vmac, list_vmac)) { - if (device_id) { - *device_id = Keylist_Key(VMAC_List, index); - } - status = true; + status = Keylist_Index_Key(VMAC_List, index, device_id); break; } } @@ -260,24 +257,25 @@ bool VMAC_Find_By_Data(struct vmac_data *vmac, uint32_t *device_id) void VMAC_Cleanup(void) { struct vmac_data *pVMAC; - uint32_t device_id; const int index = 0; unsigned i = 0; if (VMAC_List) { do { - device_id = Keylist_Key(VMAC_List, index); -#if !PRINT_ENABLED - UNUSED(device_id); +#if PRINT_ENABLED + uint32_t device_id; + Keylist_Index_Key(VMAC_List, index, &device_id); #endif pVMAC = Keylist_Data_Delete_By_Index(VMAC_List, index); if (pVMAC) { +#if PRINT_ENABLED PRINTF("VMAC List: %lu [", (unsigned long)device_id); /* print the MAC */ for (i = 0; i < pVMAC->mac_len; i++) { PRINTF("%02X", pVMAC->mac[i]); } PRINTF("]\n"); +#endif free(pVMAC); } } while (pVMAC); diff --git a/src/bacnet/basic/object/ao.c b/src/bacnet/basic/object/ao.c index 1de3e1b6..fa2e3713 100644 --- a/src/bacnet/basic/object/ao.c +++ b/src/bacnet/basic/object/ao.c @@ -156,7 +156,11 @@ unsigned Analog_Output_Count(void) */ uint32_t Analog_Output_Index_To_Instance(unsigned index) { - return Keylist_Key(Object_List, index); + KEY key = UINT32_MAX; + + Keylist_Index_Key(Object_List, index, &key); + + return key; } /** diff --git a/src/bacnet/basic/object/bacfile.c b/src/bacnet/basic/object/bacfile.c index 8f0a08b6..d6215f19 100644 --- a/src/bacnet/basic/object/bacfile.c +++ b/src/bacnet/basic/object/bacfile.c @@ -159,18 +159,20 @@ uint32_t bacfile_pathname_instance( struct object_data *pObject; int count = 0; int index = 0; + KEY key = BACNET_MAX_INSTANCE; count = Keylist_Count(Object_List); while (count) { pObject = Keylist_Data_Index(Object_List, index); if (strcmp(pathname, pObject->Pathname) == 0) { - return Keylist_Key(Object_List, index); + Keylist_Index_Key(Object_List, index, &key); + break; } count--; index++; } - return BACNET_MAX_INSTANCE; + return key; } /** @@ -262,7 +264,11 @@ uint32_t bacfile_count(void) */ uint32_t bacfile_index_to_instance(unsigned find_index) { - return Keylist_Key(Object_List, find_index); + KEY key = UINT32_MAX; + + Keylist_Index_Key(Object_List, find_index, &key); + + return key; } /** diff --git a/src/bacnet/basic/object/blo.c b/src/bacnet/basic/object/blo.c index f437473e..8f53e7d4 100644 --- a/src/bacnet/basic/object/blo.c +++ b/src/bacnet/basic/object/blo.c @@ -143,7 +143,11 @@ unsigned Binary_Lighting_Output_Count(void) */ uint32_t Binary_Lighting_Output_Index_To_Instance(unsigned index) { - return Keylist_Key(Object_List, index); + KEY key = UINT32_MAX; + + Keylist_Index_Key(Object_List, index, &key); + + return key; } /** diff --git a/src/bacnet/basic/object/bo.c b/src/bacnet/basic/object/bo.c index 58946afe..a170af37 100644 --- a/src/bacnet/basic/object/bo.c +++ b/src/bacnet/basic/object/bo.c @@ -159,7 +159,11 @@ unsigned Binary_Output_Count(void) */ uint32_t Binary_Output_Index_To_Instance(unsigned index) { - return Keylist_Key(Object_List, index); + KEY key = UINT32_MAX; + + Keylist_Index_Key(Object_List, index, &key); + + return key; } /** diff --git a/src/bacnet/basic/object/calendar.c b/src/bacnet/basic/object/calendar.c index 8379c324..e893c896 100644 --- a/src/bacnet/basic/object/calendar.c +++ b/src/bacnet/basic/object/calendar.c @@ -133,7 +133,11 @@ unsigned Calendar_Count(void) */ uint32_t Calendar_Index_To_Instance(unsigned index) { - return Keylist_Key(Object_List, index); + KEY key = UINT32_MAX; + + Keylist_Index_Key(Object_List, index, &key); + + return key; } /** diff --git a/src/bacnet/basic/object/channel.c b/src/bacnet/basic/object/channel.c index 6a3e6ca7..1e74c8b3 100644 --- a/src/bacnet/basic/object/channel.c +++ b/src/bacnet/basic/object/channel.c @@ -154,7 +154,11 @@ unsigned Channel_Count(void) */ uint32_t Channel_Index_To_Instance(unsigned index) { - return Keylist_Key(Object_List, index); + KEY key = UINT32_MAX; + + Keylist_Index_Key(Object_List, index, &key); + + return key; } /** diff --git a/src/bacnet/basic/object/color_object.c b/src/bacnet/basic/object/color_object.c index 8b7ce2b7..76b31741 100644 --- a/src/bacnet/basic/object/color_object.c +++ b/src/bacnet/basic/object/color_object.c @@ -148,7 +148,11 @@ unsigned Color_Count(void) */ uint32_t Color_Index_To_Instance(unsigned index) { - return Keylist_Key(Object_List, index); + KEY key = UINT32_MAX; + + Keylist_Index_Key(Object_List, index, &key); + + return key; } /** diff --git a/src/bacnet/basic/object/color_temperature.c b/src/bacnet/basic/object/color_temperature.c index 09914b0f..fec53797 100644 --- a/src/bacnet/basic/object/color_temperature.c +++ b/src/bacnet/basic/object/color_temperature.c @@ -144,7 +144,11 @@ unsigned Color_Temperature_Count(void) */ uint32_t Color_Temperature_Index_To_Instance(unsigned index) { - return Keylist_Key(Object_List, index); + KEY key = UINT32_MAX; + + Keylist_Index_Key(Object_List, index, &key); + + return key; } /** diff --git a/src/bacnet/basic/object/lo.c b/src/bacnet/basic/object/lo.c index f44f4618..2e9b9f4a 100644 --- a/src/bacnet/basic/object/lo.c +++ b/src/bacnet/basic/object/lo.c @@ -174,7 +174,11 @@ unsigned Lighting_Output_Count(void) */ uint32_t Lighting_Output_Index_To_Instance(unsigned index) { - return Keylist_Key(Object_List, index); + KEY key = UINT32_MAX; + + Keylist_Index_Key(Object_List, index, &key); + + return key; } /** diff --git a/src/bacnet/basic/object/lsp.c b/src/bacnet/basic/object/lsp.c index a29fa685..e14c60d4 100644 --- a/src/bacnet/basic/object/lsp.c +++ b/src/bacnet/basic/object/lsp.c @@ -133,7 +133,11 @@ unsigned Life_Safety_Point_Count(void) */ uint32_t Life_Safety_Point_Index_To_Instance(unsigned index) { - return Keylist_Key(Object_List, index); + KEY key = UINT32_MAX; + + Keylist_Index_Key(Object_List, index, &key); + + return key; } /** diff --git a/src/bacnet/basic/object/mso.c b/src/bacnet/basic/object/mso.c index d60d3168..ac94de13 100644 --- a/src/bacnet/basic/object/mso.c +++ b/src/bacnet/basic/object/mso.c @@ -150,7 +150,11 @@ unsigned Multistate_Output_Count(void) */ uint32_t Multistate_Output_Index_To_Instance(unsigned index) { - return Keylist_Key(Object_List, index); + KEY key = UINT32_MAX; + + Keylist_Index_Key(Object_List, index, &key); + + return key; } /** diff --git a/src/bacnet/basic/object/time_value.c b/src/bacnet/basic/object/time_value.c index 72af1b3d..05a0ed65 100644 --- a/src/bacnet/basic/object/time_value.c +++ b/src/bacnet/basic/object/time_value.c @@ -136,7 +136,11 @@ unsigned Time_Value_Count(void) */ uint32_t Time_Value_Index_To_Instance(unsigned index) { - return Keylist_Key(Object_List, index); + KEY key = UINT32_MAX; + + Keylist_Index_Key(Object_List, index, &key); + + return key; } /** diff --git a/src/bacnet/basic/sys/keylist.c b/src/bacnet/basic/sys/keylist.c index 99c5ca6a..9e4c6fd5 100644 --- a/src/bacnet/basic/sys/keylist.c +++ b/src/bacnet/basic/sys/keylist.c @@ -41,16 +41,9 @@ /* static data */ #include - -#include "bacnet/basic/sys/keylist.h" /* check for valid prototypes */ - -#ifndef FALSE -#define FALSE 0 -#endif - -#ifndef TRUE -#define TRUE 1 -#endif +#include +#include +#include "bacnet/basic/sys/keylist.h" /******************************************************************** */ /* Generic node routines */ @@ -81,16 +74,16 @@ static struct Keylist *KeylistCreate(void) * * @param list Pointer to the list to be tested. * - * @return Returns TRUE if success, FALSE if failed + * @return Returns true if success, false if failed */ -static int CheckArraySize(OS_Keylist list) +static bool CheckArraySize(OS_Keylist list) { int new_size = 0; /* set it up so that no size change is the default */ const int chunk = 8; /* minimum number of nodes to allocate memory for */ struct Keylist_Node **new_array = NULL; /* new array of nodes, if needed */ int i; /* counter */ if (!list) { - return FALSE; + return false; } /* indicates the need for more memory allocation */ @@ -107,7 +100,7 @@ static int CheckArraySize(OS_Keylist list) /* See if we got the memory we wanted */ if (!new_array) { - return FALSE; + return true; } /* copy the nodes from the old array to the new array */ @@ -121,12 +114,12 @@ static int CheckArraySize(OS_Keylist list) list->size = new_size; } - return TRUE; + return true; } /** Find the index of the key that we are looking for. * Since it is sorted, we can optimize the search. - * returns TRUE if found, and FALSE not found. + * returns true if found, and false not found. * Returns the found key and the index where it was found in parameters. * If the key is not found, the nearest index from the bottom will be returned, * allowing the ability to find where an key should go into the list. @@ -136,24 +129,24 @@ static int CheckArraySize(OS_Keylist list) * @param pIndex Pointer to the variable taking the index were the key * had been found. * - * @return TRUE if found, and FALSE if not + * @return true if found, and false if not */ -static int FindIndex(OS_Keylist list, KEY key, int *pIndex) +static bool FindIndex(OS_Keylist list, KEY key, int *pIndex) { struct Keylist_Node *node; /* holds the new node */ int left = 0; /* the left branch of tree, beginning of list */ int right = 0; /* the right branch on the tree, end of list */ int index = 0; /* our current search place in the array */ KEY current_key = 0; /* place holder for current node key */ - int status = FALSE; /* return value */ + bool status = false; /* return value */ if (!list) { *pIndex = 0; - return (FALSE); + return false; } if (!list->array || !list->count) { *pIndex = 0; - return (FALSE); + return false; } right = list->count - 1; /* assume that the list is sorted */ @@ -174,7 +167,7 @@ static int FindIndex(OS_Keylist list, KEY key, int *pIndex) } while ((key != current_key) && (left <= right)); if (key == current_key) { - status = TRUE; + status = true; *pIndex = index; } else { @@ -186,7 +179,7 @@ static int FindIndex(OS_Keylist list, KEY key, int *pIndex) *pIndex = index; } } - return (status); + return status; } /******************************************************************** */ @@ -398,11 +391,12 @@ void *Keylist_Data_Index(OS_Keylist list, int index) * @param list Pointer to the list * @param index Index that shall be returned * - * @return Key for the index or 0. + * @return Key for the index or UINT32_MAX if not found. + * @deprecated Use Keylist_Index_Key() instead */ KEY Keylist_Key(OS_Keylist list, int index) { - KEY key = 0; /* return value */ + KEY key = UINT32_MAX; /* return value */ struct Keylist_Node *node; if (list) { @@ -417,6 +411,35 @@ KEY Keylist_Key(OS_Keylist list, int index) return key; } +/** + * Determine if there is a node key at the given index. + * + * @param list Pointer to the list + * @param index Index that shall be returned + * @param pKey Pointer to the variable returning the key + * @return True if the key is found, false if not. + */ +bool Keylist_Index_Key(OS_Keylist list, int index, KEY *pKey) +{ + bool status = false; /* return value */ + struct Keylist_Node *node; + + if (list) { + if (list->array && list->count && (index >= 0) && + (index < list->count)) { + node = list->array[index]; + if (node) { + status = true; + if (pKey) { + *pKey = node->key; + } + } + } + } + + return status; +} + /** Returns the next empty key from the list. * * @param list Pointer to the list diff --git a/src/bacnet/basic/sys/keylist.h b/src/bacnet/basic/sys/keylist.h index 940c5117..5be3eff9 100644 --- a/src/bacnet/basic/sys/keylist.h +++ b/src/bacnet/basic/sys/keylist.h @@ -25,6 +25,7 @@ #define KEYLIST_H #include "bacnet/bacnet_stack_exports.h" +#include "bacnet/basic/sys/platform.h" #include "bacnet/basic/sys/key.h" /* This is a key sorted linked list data library that */ @@ -104,12 +105,20 @@ extern "C" { OS_Keylist list, int index); -/* return the key at the given index */ +/* returns the node key specified by the index */ + BACNET_STACK_DEPRECATED("Use Keylist_Index_Key() instead") BACNET_STACK_EXPORT KEY Keylist_Key( OS_Keylist list, int index); +/* returns the node key specified by the index */ + BACNET_STACK_EXPORT + bool Keylist_Index_Key( + OS_Keylist list, + int index, + KEY *pKey); + /* returns the next empty key from the list */ BACNET_STACK_EXPORT KEY Keylist_Next_Empty_Key( diff --git a/test/bacnet/basic/sys/keylist/src/main.c b/test/bacnet/basic/sys/keylist/src/main.c index 787d39b4..b44e012e 100644 --- a/test/bacnet/basic/sys/keylist/src/main.c +++ b/test/bacnet/basic/sys/keylist/src/main.c @@ -122,6 +122,7 @@ ZTEST(keylist_tests, testKeyListDataKey) static void testKeyListDataKey(void) #endif { + bool status = false; OS_Keylist list; KEY key; KEY test_key; @@ -137,19 +138,22 @@ static void testKeyListDataKey(void) key = 1; index = Keylist_Data_Add(list, key, data1); zassert_equal(index, 0, NULL); - test_key = Keylist_Key(list, index); + status = Keylist_Index_Key(list, index, &test_key); + zassert_true(status, NULL); zassert_equal(test_key, key, NULL); key = 2; index = Keylist_Data_Add(list, key, data2); zassert_equal(index, 1, NULL); - test_key = Keylist_Key(list, index); + status = Keylist_Index_Key(list, index, &test_key); + zassert_true(status, NULL); zassert_equal(test_key, key, NULL); key = 3; index = Keylist_Data_Add(list, key, data3); zassert_equal(index, 2, NULL); - test_key = Keylist_Key(list, index); + status = Keylist_Index_Key(list, index, &test_key); + zassert_true(status, NULL); zassert_equal(test_key, key, NULL); zassert_equal(Keylist_Count(list), 3, NULL); @@ -278,7 +282,8 @@ ZTEST(keylist_tests, testKeyListLarge) static void testKeyListLarge(void) #endif { - int data1 = 42; + bool status = false; + int data_list[1024 * 16] = { 0 }; int *data; OS_Keylist list; KEY key; @@ -290,15 +295,18 @@ static void testKeyListLarge(void) return; for (key = 0; key < num_keys; key++) { - index = Keylist_Data_Add(list, key, &data1); + data_list[key] = 42 + key; + index = Keylist_Data_Add(list, key, &data_list[key]); } for (key = 0; key < num_keys; key++) { data = Keylist_Data(list, key); - zassert_equal(*data, data1, NULL); + zassert_equal(*data, data_list[key], NULL); } for (index = 0; index < num_keys; index++) { data = Keylist_Data_Index(list, index); - zassert_equal(*data, data1, NULL); + status = Keylist_Index_Key(list, index, &key); + zassert_true(status, NULL); + zassert_equal(*data, data_list[key], NULL); } Keylist_Delete(list);