Implemented separate Who-Is and Who-Has handlers for the routed case, where we loop through all the Devices looking for matches.
Tested and improved the routing code. Gateway demo now sends I-Am as each Device is added. Added test for valid Network number when routing, and only send Reject message if this fails (and not if we just can't find a Device match). Test the hop_count and discard the packet if would reach 0 and a destination Network is called for.
This commit is contained in:
@@ -38,6 +38,42 @@
|
||||
|
||||
/** @file h_whohas.c Handles Who-Has requests. */
|
||||
|
||||
/** Local function which responds with either the requested object name
|
||||
* or object ID, if the Device has a match.
|
||||
* @param data [in] The decoded who-has payload from the request.
|
||||
*/
|
||||
static void match_name_or_object(
|
||||
BACNET_WHO_HAS_DATA * data)
|
||||
{
|
||||
char *object_name = NULL;
|
||||
int object_type = 0;
|
||||
uint32_t object_instance = 0;
|
||||
bool found = false;
|
||||
/* do we have such an object? If so, send an I-Have.
|
||||
note: we should have only 1 of such an object */
|
||||
if (data->object_name) {
|
||||
/* valid name in my device? */
|
||||
object_name = characterstring_value(&data->object.name);
|
||||
found =
|
||||
Device_Valid_Object_Name(object_name, &object_type,
|
||||
&object_instance);
|
||||
if (found)
|
||||
Send_I_Have(Device_Object_Instance_Number(),
|
||||
(BACNET_OBJECT_TYPE) object_type, object_instance,
|
||||
object_name);
|
||||
} else {
|
||||
/* valid object in my device? */
|
||||
object_name =
|
||||
Device_Valid_Object_Id(data->object.identifier.type,
|
||||
data->object.identifier.instance);
|
||||
if (object_name)
|
||||
Send_I_Have(Device_Object_Instance_Number(),
|
||||
(BACNET_OBJECT_TYPE) data->object.identifier.type,
|
||||
data->object.identifier.instance, object_name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** Handler for Who-Has requests, with broadcast I-Have response.
|
||||
* Will respond if the device Object ID matches, and we have
|
||||
* the Object or Object Name requested.
|
||||
@@ -55,49 +91,58 @@ void handler_who_has(
|
||||
int len = 0;
|
||||
BACNET_WHO_HAS_DATA data;
|
||||
bool directed_to_me = false;
|
||||
int object_type = 0;
|
||||
uint32_t object_instance = 0;
|
||||
char *object_name = NULL;
|
||||
bool found = false;
|
||||
|
||||
(void) src;
|
||||
len = whohas_decode_service_request(service_request, service_len, &data);
|
||||
if (len > 0) {
|
||||
if ((data.low_limit == -1) || (data.high_limit == -1))
|
||||
directed_to_me = true;
|
||||
#ifdef BAC_ROUTING
|
||||
else if ((Routed_Device_Object_Instance_Number() >= (uint32_t) data.low_limit)
|
||||
&& (Routed_Device_Object_Instance_Number() <= (uint32_t) data.high_limit))
|
||||
#else
|
||||
else if ((Device_Object_Instance_Number() >= (uint32_t) data.low_limit)
|
||||
&& (Device_Object_Instance_Number() <= (uint32_t) data.high_limit))
|
||||
#endif
|
||||
&& (Device_Object_Instance_Number() <= (uint32_t) data.high_limit))
|
||||
directed_to_me = true;
|
||||
if (directed_to_me) {
|
||||
/* do we have such an object? If so, send an I-Have.
|
||||
note: we should have only 1 of such an object */
|
||||
if (data.object_name) {
|
||||
/* valid name in my device? */
|
||||
object_name = characterstring_value(&data.object.name);
|
||||
found =
|
||||
Device_Valid_Object_Name(object_name, &object_type,
|
||||
&object_instance);
|
||||
if (found)
|
||||
Send_I_Have(Device_Object_Instance_Number(),
|
||||
(BACNET_OBJECT_TYPE) object_type, object_instance,
|
||||
object_name);
|
||||
} else {
|
||||
/* valid object in my device? */
|
||||
object_name =
|
||||
Device_Valid_Object_Id(data.object.identifier.type,
|
||||
data.object.identifier.instance);
|
||||
if (object_name)
|
||||
Send_I_Have(Device_Object_Instance_Number(),
|
||||
(BACNET_OBJECT_TYPE) data.object.identifier.type,
|
||||
data.object.identifier.instance, object_name);
|
||||
}
|
||||
match_name_or_object(&data);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
#ifdef BAC_ROUTING
|
||||
/** Handler for Who-Has requests in the virtual routing setup,
|
||||
* with broadcast I-Have response.
|
||||
* Will respond if the device Object ID matches, and we have
|
||||
* the Object or Object Name requested.
|
||||
*
|
||||
* @ingroup DMDOB
|
||||
* @param service_request [in] The received message to be handled.
|
||||
* @param service_len [in] Length of the service_request message.
|
||||
* @param src [in] The BACNET_ADDRESS of the message's source (ignored).
|
||||
*/
|
||||
void handler_who_has_for_routing(
|
||||
uint8_t * service_request,
|
||||
uint16_t service_len,
|
||||
BACNET_ADDRESS * src)
|
||||
{
|
||||
int len = 0;
|
||||
BACNET_WHO_HAS_DATA data;
|
||||
int32_t dev_instance;
|
||||
int cursor = 0; /* Starting hint */
|
||||
int my_list[2] = {0, -1}; /* Not really used, so dummy values */
|
||||
BACNET_ADDRESS bcast_net;
|
||||
|
||||
(void) src;
|
||||
len = whohas_decode_service_request(service_request, service_len, &data);
|
||||
if (len > 0) {
|
||||
/* Go through all devices, starting with the root gateway Device */
|
||||
memset( &bcast_net, 0, sizeof(BACNET_ADDRESS));
|
||||
bcast_net.net = BACNET_BROADCAST_NETWORK; /* That's all we have to set */
|
||||
while ( Routed_Device_GetNext( &bcast_net, my_list, &cursor ) ) {
|
||||
dev_instance = Device_Object_Instance_Number();
|
||||
if ( (data.low_limit == -1) || (data.high_limit == -1) ||
|
||||
((dev_instance >= data.low_limit) &&
|
||||
(dev_instance <= data.high_limit)))
|
||||
match_name_or_object(&data);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif /* BAC_ROUTING */
|
||||
|
||||
Reference in New Issue
Block a user