Added I-Am-Router-To-Network demo. Untested.
This commit is contained in:
@@ -41,8 +41,24 @@
|
||||
#include "handlers.h"
|
||||
#include "txbuf.h"
|
||||
|
||||
/* find a specific device, or use -1 for limit if you want unlimited */
|
||||
void Send_WhoIsRouterToNetwork(
|
||||
static void npdu_encode_npdu_network(
|
||||
BACNET_NPDU_DATA * npdu_data,
|
||||
BACNET_NETWORK_MESSAGE_TYPE network_message_type,
|
||||
BACNET_MESSAGE_PRIORITY priority)
|
||||
{
|
||||
if (npdu_data) {
|
||||
npdu_data->data_expecting_reply = false;
|
||||
npdu_data->protocol_version = BACNET_PROTOCOL_VERSION;
|
||||
npdu_data->network_layer_message = true; /* false if APDU */
|
||||
npdu_data->network_message_type = network_message_type; /* optional */
|
||||
npdu_data->vendor_id = 0; /* optional, if net message type is > 0x80 */
|
||||
npdu_data->priority = priority;
|
||||
npdu_data->hop_count = 255;
|
||||
}
|
||||
}
|
||||
|
||||
/* find a specific router, or use -1 for limit if you want unlimited */
|
||||
void Send_Who_Is_Router_To_Network(
|
||||
int dnet)
|
||||
{
|
||||
int len = 0;
|
||||
@@ -51,14 +67,9 @@ void Send_WhoIsRouterToNetwork(
|
||||
int bytes_sent = 0;
|
||||
BACNET_NPDU_DATA npdu_data;
|
||||
|
||||
/* encode the NPDU portion of the packet */
|
||||
npdu_data.data_expecting_reply = false;
|
||||
npdu_data.protocol_version = BACNET_PROTOCOL_VERSION;
|
||||
npdu_data.network_layer_message = true; /* false if APDU */
|
||||
npdu_data.network_message_type = NETWORK_MESSAGE_WHO_IS_ROUTER_TO_NETWORK;
|
||||
npdu_data.vendor_id = 0; /* optional, if net message type is > 0x80 */
|
||||
npdu_data.priority = MESSAGE_PRIORITY_NORMAL;
|
||||
npdu_data.hop_count = 0;
|
||||
npdu_encode_npdu_network(&npdu_data,
|
||||
NETWORK_MESSAGE_WHO_IS_ROUTER_TO_NETWORK,
|
||||
MESSAGE_PRIORITY_NORMAL);
|
||||
pdu_len =
|
||||
npdu_encode_pdu(&Handler_Transmit_Buffer[0], NULL, NULL, &npdu_data);
|
||||
/* encode the optional DNET portion of the packet */
|
||||
@@ -66,14 +77,14 @@ void Send_WhoIsRouterToNetwork(
|
||||
len = encode_unsigned16(&Handler_Transmit_Buffer[pdu_len], dnet);
|
||||
pdu_len += len;
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr, "Send Who-Is-Router-To-Network Request to %u\n",dnet);
|
||||
fprintf(stderr, "Send Who-Is-Router-To-Network message to %u\n",dnet);
|
||||
#endif
|
||||
} else {
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr, "Send Who-Is-Router-To-Network Request\n");
|
||||
fprintf(stderr, "Send Who-Is-Router-To-Network message\n");
|
||||
#endif
|
||||
}
|
||||
/* Who-Is-Router-To-Network is a global broadcast */
|
||||
/* Who-Is-Router-To-Network may be unicast or broadcast */
|
||||
datalink_get_broadcast_address(&dest);
|
||||
bytes_sent =
|
||||
datalink_send_pdu(&dest, &npdu_data, &Handler_Transmit_Buffer[0],
|
||||
@@ -84,3 +95,127 @@ void Send_WhoIsRouterToNetwork(
|
||||
strerror(errno));
|
||||
#endif
|
||||
}
|
||||
|
||||
/* pDNET_list: list of networks for which I am a router,
|
||||
terminated with -1 */
|
||||
void Send_I_Am_Router_To_Network(
|
||||
const int *pDNET_list)
|
||||
{
|
||||
int len = 0;
|
||||
int pdu_len = 0;
|
||||
BACNET_ADDRESS dest;
|
||||
int bytes_sent = 0;
|
||||
BACNET_NPDU_DATA npdu_data;
|
||||
uint16_t dnet = 0;
|
||||
|
||||
npdu_encode_npdu_network(&npdu_data,
|
||||
NETWORK_MESSAGE_I_AM_ROUTER_TO_NETWORK,
|
||||
MESSAGE_PRIORITY_NORMAL);
|
||||
pdu_len =
|
||||
npdu_encode_pdu(&Handler_Transmit_Buffer[0], NULL, NULL, &npdu_data);
|
||||
/* encode the optional DNET list portion of the packet */
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr, "Send I-Am-Router-To-Network message to:\n");
|
||||
#endif
|
||||
while ((*pDNET_list) != -1) {
|
||||
dnet = (*pDNET_list);
|
||||
len = encode_unsigned16(&Handler_Transmit_Buffer[pdu_len], dnet);
|
||||
pdu_len += len;
|
||||
pDNET_list++;
|
||||
#if PRINT_ENABLED
|
||||
fprintf(stderr, "%u\n",dnet);
|
||||
#endif
|
||||
}
|
||||
/* I-Am-Router-To-Network shall always be transmitted with
|
||||
a broadcast MAC address. */
|
||||
datalink_get_broadcast_address(&dest);
|
||||
bytes_sent =
|
||||
datalink_send_pdu(&dest, &npdu_data, &Handler_Transmit_Buffer[0],
|
||||
pdu_len);
|
||||
#if PRINT_ENABLED
|
||||
if (bytes_sent <= 0)
|
||||
fprintf(stderr, "Failed to send I-Am-Router-To-Network message (%s)!\n",
|
||||
strerror(errno));
|
||||
#endif
|
||||
}
|
||||
|
||||
/* */
|
||||
void Send_Initialize_Routing_Table(
|
||||
BACNET_ROUTER_PORT *router_port_list)
|
||||
{
|
||||
int len = 0;
|
||||
int pdu_len = 0;
|
||||
BACNET_ADDRESS dest;
|
||||
int bytes_sent = 0;
|
||||
BACNET_NPDU_DATA npdu_data;
|
||||
uint8_t number_of_ports = 0;
|
||||
BACNET_ROUTER_PORT *router_port;
|
||||
unsigned i = 0; /* counter */
|
||||
|
||||
npdu_encode_npdu_network(&npdu_data,
|
||||
NETWORK_MESSAGE_INIT_RT_TABLE,
|
||||
MESSAGE_PRIORITY_NORMAL);
|
||||
pdu_len =
|
||||
npdu_encode_pdu(&Handler_Transmit_Buffer[0], NULL, NULL, &npdu_data);
|
||||
/* encode the optional port_info list portion of the packet */
|
||||
router_port = router_port_list;
|
||||
while (router_port) {
|
||||
number_of_ports++;
|
||||
router_port = router_port->next;
|
||||
}
|
||||
Handler_Transmit_Buffer[pdu_len++] = number_of_ports;
|
||||
if (number_of_ports) {
|
||||
router_port = router_port_list;
|
||||
while (router_port) {
|
||||
len = encode_unsigned16(
|
||||
&Handler_Transmit_Buffer[pdu_len],
|
||||
router_port->dnet);
|
||||
pdu_len += len;
|
||||
Handler_Transmit_Buffer[pdu_len++] = router_port->id;
|
||||
Handler_Transmit_Buffer[pdu_len++] = router_port->info_len;
|
||||
for (i = 0; i < router_port->info_len; i++) {
|
||||
Handler_Transmit_Buffer[pdu_len++] = router_port->info[i];
|
||||
}
|
||||
router_port = router_port->next;
|
||||
}
|
||||
}
|
||||
/* Init Routing Table shall always be transmitted with
|
||||
a broadcast MAC address. */
|
||||
datalink_get_broadcast_address(&dest);
|
||||
bytes_sent =
|
||||
datalink_send_pdu(&dest, &npdu_data, &Handler_Transmit_Buffer[0],
|
||||
pdu_len);
|
||||
#if PRINT_ENABLED
|
||||
if (bytes_sent <= 0)
|
||||
fprintf(stderr, "Failed to send Initialize-Routing-Table message (%s)!\n",
|
||||
strerror(errno));
|
||||
#endif
|
||||
}
|
||||
|
||||
/* */
|
||||
void Send_Initialize_Routing_Table_Ack(
|
||||
const int *port_info)
|
||||
{
|
||||
int len = 0;
|
||||
int pdu_len = 0;
|
||||
BACNET_ADDRESS dest;
|
||||
int bytes_sent = 0;
|
||||
BACNET_NPDU_DATA npdu_data;
|
||||
uint16_t dnet = 0;
|
||||
|
||||
npdu_encode_npdu_network(&npdu_data,
|
||||
NETWORK_MESSAGE_INIT_RT_TABLE_ACK,
|
||||
MESSAGE_PRIORITY_NORMAL);
|
||||
pdu_len =
|
||||
npdu_encode_pdu(&Handler_Transmit_Buffer[0], NULL, NULL, &npdu_data);
|
||||
/* encode the optional DNET list portion of the packet */
|
||||
datalink_get_broadcast_address(&dest);
|
||||
bytes_sent =
|
||||
datalink_send_pdu(&dest, &npdu_data, &Handler_Transmit_Buffer[0],
|
||||
pdu_len);
|
||||
#if PRINT_ENABLED
|
||||
if (bytes_sent <= 0)
|
||||
fprintf(stderr, "Failed to Send I-Am-Router-To-Network message (%s)!\n",
|
||||
strerror(errno));
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/**************************************************************************
|
||||
*
|
||||
* Copyright (C) 2006 Steve Karg <skarg@users.sourceforge.net>
|
||||
* Copyright (C) 2008 Steve Karg <skarg@users.sourceforge.net>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
@@ -102,44 +102,6 @@ static void Init_Service_Handlers(
|
||||
apdu_set_reject_handler(MyRejectHandler);
|
||||
}
|
||||
|
||||
static void print_address_cache(
|
||||
void)
|
||||
{
|
||||
int i, j;
|
||||
BACNET_ADDRESS address;
|
||||
uint32_t device_id = 0;
|
||||
unsigned max_apdu = 0;
|
||||
|
||||
printf("%-7s %-14s %-4s %-5s %-14s\n", "Device", "MAC", "APDU", "SNET",
|
||||
"SADR");
|
||||
printf("------- -------------- ---- ----- --------------\n");
|
||||
for (i = 0; i < MAX_ADDRESS_CACHE; i++) {
|
||||
if (address_get_by_index(i, &device_id, &max_apdu, &address)) {
|
||||
printf("%7u ", device_id);
|
||||
for (j = 0; j < 7; j++) {
|
||||
if (j < address.mac_len) {
|
||||
printf("%02X", address.mac[j]);
|
||||
} else {
|
||||
printf(" ");
|
||||
}
|
||||
}
|
||||
printf(" %4hu ", max_apdu);
|
||||
printf("%5hu ", address.net);
|
||||
if (address.net) {
|
||||
for (j = 0; j < 7; j++) {
|
||||
if (j < address.len) {
|
||||
printf("%02X", address.adr[j]);
|
||||
} else {
|
||||
printf(" ");
|
||||
}
|
||||
printf(" ");
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void Init_DataLink(
|
||||
void)
|
||||
{
|
||||
@@ -274,7 +236,7 @@ int main(int argc, char *argv[]) {
|
||||
last_seconds = time(NULL);
|
||||
timeout_seconds = apdu_timeout() / 1000;
|
||||
/* send the request */
|
||||
Send_WhoIsRouterToNetwork(Target_Router_Network);
|
||||
Send_Who_Is_Router_To_Network(Target_Router_Network);
|
||||
/* loop forever */
|
||||
for (;;) {
|
||||
/* increment timer - exit if timed out */
|
||||
@@ -300,7 +262,6 @@ int main(int argc, char *argv[]) {
|
||||
/* keep track of time for next check */
|
||||
last_seconds = current_seconds;
|
||||
}
|
||||
print_address_cache();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user