update keylist to use bool and stdint value for key not found. (#572)

* Updated keylist to use bool and stdint value for key not found.

* Added Keylist_Index_Key to deprecate Keylist_Key function

* Changed the deprecated Keylist_Key API to Keylist_Index_Key

* Added unit test for Keylist_Index_Key API
This commit is contained in:
Steve Karg
2024-02-12 17:35:15 -06:00
committed by GitHub
parent 7eedaa406d
commit 190183966c
16 changed files with 143 additions and 55 deletions
+6 -8
View File
@@ -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);
+5 -1
View File
@@ -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;
}
/**
+9 -3
View File
@@ -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;
}
/**
+5 -1
View File
@@ -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;
}
/**
+5 -1
View File
@@ -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;
}
/**
+5 -1
View File
@@ -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;
}
/**
+5 -1
View File
@@ -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;
}
/**
+5 -1
View File
@@ -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;
}
/**
+5 -1
View File
@@ -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;
}
/**
+5 -1
View File
@@ -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;
}
/**
+5 -1
View File
@@ -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;
}
/**
+5 -1
View File
@@ -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;
}
/**
+5 -1
View File
@@ -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;
}
/**
+48 -25
View File
@@ -41,16 +41,9 @@
/* static data */
#include <stdlib.h>
#include "bacnet/basic/sys/keylist.h" /* check for valid prototypes */
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
#include <stdint.h>
#include <stdbool.h>
#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
+10 -1
View File
@@ -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(
+15 -7
View File
@@ -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);