Added function to get the device instance number from the address cache using the MAC address.
This commit is contained in:
@@ -56,6 +56,9 @@ extern "C" {
|
|||||||
bool address_get_by_index(unsigned index,
|
bool address_get_by_index(unsigned index,
|
||||||
uint32_t * device_id, unsigned *max_apdu, BACNET_ADDRESS * src);
|
uint32_t * device_id, unsigned *max_apdu, BACNET_ADDRESS * src);
|
||||||
|
|
||||||
|
bool address_get_device_id(BACNET_ADDRESS * src,
|
||||||
|
uint32_t *device_id);
|
||||||
|
|
||||||
unsigned address_count(void);
|
unsigned address_count(void);
|
||||||
|
|
||||||
bool address_match(BACNET_ADDRESS * dest, BACNET_ADDRESS * src);
|
bool address_match(BACNET_ADDRESS * dest, BACNET_ADDRESS * src);
|
||||||
|
|||||||
+30
-33
@@ -100,6 +100,28 @@ bool address_get_by_device(uint32_t device_id,
|
|||||||
return found;
|
return found;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* find a the device id from a given MAC address */
|
||||||
|
bool address_get_device_id(BACNET_ADDRESS * src,
|
||||||
|
uint32_t *device_id)
|
||||||
|
{
|
||||||
|
unsigned i;
|
||||||
|
bool found = false; /* return value */
|
||||||
|
|
||||||
|
for (i = 0; i < MAX_ADDRESS_CACHE; i++) {
|
||||||
|
if (Address_Cache[i].valid) {
|
||||||
|
if (bacnet_address_same(&Address_Cache[i].address, src)) {
|
||||||
|
if (device_id) {
|
||||||
|
*device_id = Address_Cache[i].device_id;
|
||||||
|
}
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return found;
|
||||||
|
}
|
||||||
|
|
||||||
void address_add(uint32_t device_id,
|
void address_add(uint32_t device_id,
|
||||||
unsigned max_apdu, BACNET_ADDRESS * src)
|
unsigned max_apdu, BACNET_ADDRESS * src)
|
||||||
{
|
{
|
||||||
@@ -233,36 +255,6 @@ unsigned address_count(void)
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool address_match(BACNET_ADDRESS * dest, BACNET_ADDRESS * src)
|
|
||||||
{
|
|
||||||
unsigned i;
|
|
||||||
unsigned max_len;
|
|
||||||
bool match = true; /* return value */
|
|
||||||
|
|
||||||
if (dest->mac_len != src->mac_len)
|
|
||||||
match = false;
|
|
||||||
max_len = dest->mac_len;
|
|
||||||
if (max_len > MAX_MAC_LEN)
|
|
||||||
max_len = MAX_MAC_LEN;
|
|
||||||
for (i = 0; i < max_len; i++) {
|
|
||||||
if (dest->mac[i] != src->mac[i])
|
|
||||||
match = false;
|
|
||||||
}
|
|
||||||
if (dest->net != src->net)
|
|
||||||
match = false;
|
|
||||||
if (dest->len != src->len)
|
|
||||||
match = false;
|
|
||||||
max_len = dest->len;
|
|
||||||
if (max_len > MAX_MAC_LEN)
|
|
||||||
max_len = MAX_MAC_LEN;
|
|
||||||
for (i = 0; i < max_len; i++) {
|
|
||||||
if (dest->adr[i] != src->adr[i])
|
|
||||||
match = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return match;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef TEST
|
#ifdef TEST
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -293,6 +285,7 @@ void testAddress(Test * pTest)
|
|||||||
uint32_t test_device_id = 0;
|
uint32_t test_device_id = 0;
|
||||||
unsigned test_max_apdu = 0;
|
unsigned test_max_apdu = 0;
|
||||||
|
|
||||||
|
/* create a fake address database */
|
||||||
for (i = 0; i < MAX_ADDRESS_CACHE; i++) {
|
for (i = 0; i < MAX_ADDRESS_CACHE; i++) {
|
||||||
set_address(i, &src);
|
set_address(i, &src);
|
||||||
device_id = i * 255;
|
device_id = i * 255;
|
||||||
@@ -303,17 +296,21 @@ void testAddress(Test * pTest)
|
|||||||
|
|
||||||
for (i = 0; i < MAX_ADDRESS_CACHE; i++) {
|
for (i = 0; i < MAX_ADDRESS_CACHE; i++) {
|
||||||
device_id = i * 255;
|
device_id = i * 255;
|
||||||
|
set_address(i, &src);
|
||||||
|
/* test the lookup by device id */
|
||||||
ct_test(pTest, address_get_by_device(device_id, &test_max_apdu,
|
ct_test(pTest, address_get_by_device(device_id, &test_max_apdu,
|
||||||
&test_address));
|
&test_address));
|
||||||
set_address(i, &src);
|
|
||||||
ct_test(pTest, test_max_apdu == max_apdu);
|
ct_test(pTest, test_max_apdu == max_apdu);
|
||||||
ct_test(pTest, address_match(&test_address, &src));
|
ct_test(pTest, bacnet_address_same(&test_address, &src));
|
||||||
ct_test(pTest, address_get_by_index(i, &test_device_id,
|
ct_test(pTest, address_get_by_index(i, &test_device_id,
|
||||||
&test_max_apdu, &test_address));
|
&test_max_apdu, &test_address));
|
||||||
ct_test(pTest, test_device_id == device_id);
|
ct_test(pTest, test_device_id == device_id);
|
||||||
ct_test(pTest, test_max_apdu == max_apdu);
|
ct_test(pTest, test_max_apdu == max_apdu);
|
||||||
ct_test(pTest, address_match(&test_address, &src));
|
ct_test(pTest, bacnet_address_same(&test_address, &src));
|
||||||
ct_test(pTest, address_count() == MAX_ADDRESS_CACHE);
|
ct_test(pTest, address_count() == MAX_ADDRESS_CACHE);
|
||||||
|
/* test the lookup by MAC */
|
||||||
|
ct_test(pTest, address_get_device_id(&src, &test_device_id));
|
||||||
|
ct_test(pTest, test_device_id == device_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < MAX_ADDRESS_CACHE; i++) {
|
for (i = 0; i < MAX_ADDRESS_CACHE; i++) {
|
||||||
|
|||||||
+17
-11
@@ -56,24 +56,30 @@ void bacnet_address_copy(BACNET_ADDRESS * dest, BACNET_ADDRESS * src)
|
|||||||
|
|
||||||
bool bacnet_address_same(BACNET_ADDRESS * dest, BACNET_ADDRESS * src)
|
bool bacnet_address_same(BACNET_ADDRESS * dest, BACNET_ADDRESS * src)
|
||||||
{
|
{
|
||||||
int i = 0;
|
unsigned i;
|
||||||
|
unsigned max_len;
|
||||||
|
bool match = true; /* return value */
|
||||||
|
|
||||||
if (!dest || !src)
|
|
||||||
return false;
|
|
||||||
if (dest->mac_len != src->mac_len)
|
if (dest->mac_len != src->mac_len)
|
||||||
return false;
|
match = false;
|
||||||
for (i = 0; i < dest->mac_len; i++) {
|
max_len = dest->mac_len;
|
||||||
|
if (max_len > MAX_MAC_LEN)
|
||||||
|
max_len = MAX_MAC_LEN;
|
||||||
|
for (i = 0; i < max_len; i++) {
|
||||||
if (dest->mac[i] != src->mac[i])
|
if (dest->mac[i] != src->mac[i])
|
||||||
return false;
|
match = false;
|
||||||
}
|
}
|
||||||
if (dest->net != src->net)
|
if (dest->net != src->net)
|
||||||
return false;
|
match = false;
|
||||||
if (dest->len != src->len)
|
if (dest->len != src->len)
|
||||||
return false;
|
match = false;
|
||||||
for (i = 0; i < dest->len; i++) {
|
max_len = dest->len;
|
||||||
|
if (max_len > MAX_MAC_LEN)
|
||||||
|
max_len = MAX_MAC_LEN;
|
||||||
|
for (i = 0; i < max_len; i++) {
|
||||||
if (dest->adr[i] != src->adr[i])
|
if (dest->adr[i] != src->adr[i])
|
||||||
return false;
|
match = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return match;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,55 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||||
|
<CodeBlocks_project_file>
|
||||||
|
<FileVersion major="1" minor="6" />
|
||||||
|
<Project>
|
||||||
|
<Option title="address" />
|
||||||
|
<Option pch_mode="2" />
|
||||||
|
<Option compiler="gcc" />
|
||||||
|
<Build>
|
||||||
|
<Target title="Debug">
|
||||||
|
<Option output="bin\Debug\address" prefix_auto="1" extension_auto="1" />
|
||||||
|
<Option object_output="obj\Debug\" />
|
||||||
|
<Option type="1" />
|
||||||
|
<Option compiler="gcc" />
|
||||||
|
<Compiler>
|
||||||
|
<Add option="-g" />
|
||||||
|
</Compiler>
|
||||||
|
</Target>
|
||||||
|
<Target title="Release">
|
||||||
|
<Option output="bin\Release\address" prefix_auto="1" extension_auto="1" />
|
||||||
|
<Option object_output="obj\Release\" />
|
||||||
|
<Option type="1" />
|
||||||
|
<Option compiler="gcc" />
|
||||||
|
<Compiler>
|
||||||
|
<Add option="-O2" />
|
||||||
|
</Compiler>
|
||||||
|
<Linker>
|
||||||
|
<Add option="-s" />
|
||||||
|
</Linker>
|
||||||
|
</Target>
|
||||||
|
</Build>
|
||||||
|
<Compiler>
|
||||||
|
<Add option="-Wall" />
|
||||||
|
<Add option="-fexceptions" />
|
||||||
|
<Add option="-DBIG_ENDIAN=0" />
|
||||||
|
<Add option="-DTEST" />
|
||||||
|
<Add option="-DTEST_ADDRESS" />
|
||||||
|
<Add directory="..\include" />
|
||||||
|
<Add directory="." />
|
||||||
|
</Compiler>
|
||||||
|
<Unit filename="..\src\address.c">
|
||||||
|
<Option compilerVar="CC" />
|
||||||
|
</Unit>
|
||||||
|
<Unit filename="..\src\bacaddr.c">
|
||||||
|
<Option compilerVar="CC" />
|
||||||
|
</Unit>
|
||||||
|
<Unit filename="ctest.c">
|
||||||
|
<Option compilerVar="CC" />
|
||||||
|
</Unit>
|
||||||
|
<Extensions>
|
||||||
|
<code_completion />
|
||||||
|
<envvars />
|
||||||
|
<debugger />
|
||||||
|
</Extensions>
|
||||||
|
</Project>
|
||||||
|
</CodeBlocks_project_file>
|
||||||
Reference in New Issue
Block a user