refactored the Who-Is and I-Am handling, and cleaned up some comments.

This commit is contained in:
skarg
2006-02-20 16:17:46 +00:00
parent 16c2545f45
commit 676ecf77a2
9 changed files with 149 additions and 188 deletions
+10 -7
View File
@@ -33,9 +33,8 @@
#include "bacdcode.h"
#include "whois.h"
#include "device.h"
/* global flag to send an I-Am */
bool I_Am_Request = true;
#include "client.h"
#include "txbuf.h"
void handler_who_is(uint8_t * service_request,
uint16_t service_len, BACNET_ADDRESS * src)
@@ -50,11 +49,15 @@ void handler_who_is(uint8_t * service_request,
/* in our simple system, we just set a flag and let the main loop
send the I-Am request. */
if (len == 0)
I_Am_Request = true;
iam_send(&Handler_Transmit_Buffer[0]);
else if (len != -1) {
if ((Device_Object_Instance_Number() >= (uint32_t) low_limit) &&
(Device_Object_Instance_Number() <= (uint32_t) high_limit))
I_Am_Request = true;
/* is my device id within the limits? */
if (((Device_Object_Instance_Number() >= (uint32_t) low_limit) &&
(Device_Object_Instance_Number() <= (uint32_t) high_limit)) ||
/* BACnet wildcard is the max instance number - everyone responds */
((BACNET_MAX_INSTANCE >= (uint32_t) low_limit) &&
(BACNET_MAX_INSTANCE <= (uint32_t) high_limit)))
iam_send(&Handler_Transmit_Buffer[0]);
}
return;
-3
View File
@@ -32,9 +32,6 @@
#include "apdu.h"
#include "bacapp.h"
/* flag requesting main loop to send an I-Am */
extern bool I_Am_Request;
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
+1 -6
View File
@@ -609,9 +609,6 @@ int Device_Encode_Property_APDU(uint8_t * apdu,
return apdu_len;
}
/* we can send an I-Am when our device ID changes */
extern bool I_Am_Request;
/* returns true if successful */
bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data,
BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code)
@@ -630,7 +627,7 @@ bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data,
if ((wp_data->value.type.Object_Id.type == OBJECT_DEVICE) &&
(Device_Set_Object_Instance_Number(wp_data->value.type.
Object_Id.instance))) {
I_Am_Request = true;
/* we could send an I-Am broadcast to let the world know */
status = true;
} else {
*error_class = ERROR_CLASS_PROPERTY;
@@ -808,8 +805,6 @@ uint32_t bacfile_index_to_instance(unsigned find_index)
return find_index;
}
bool I_Am_Request = false;
int main(void)
{
Test *pTest;
+32 -37
View File
@@ -23,7 +23,7 @@
*
*********************************************************************/
/* READFILE: command line tool that reads a file from a BACnet device. */
/* command line tool that sends a BACnet service, and displays the reply */
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
@@ -335,43 +335,38 @@ int main(int argc, char *argv[])
last_seconds) * 1000));
if (End_Of_File_Detected || Error_Detected)
break;
if (I_Am_Request) {
I_Am_Request = false;
iam_send(&Handler_Transmit_Buffer[0]);
} else {
/* wait until the device is bound, or timeout and quit */
found = address_bind_request(Target_Device_Object_Instance,
&max_apdu, &Target_Address);
if (found) {
/* calculate the smaller of our APDU size or theirs
and remove the overhead of the APDU (about 16 octets max).
note: we could fail if there is a bottle neck (router)
and smaller MPDU in betweeen. */
if (max_apdu < MAX_APDU)
my_max_apdu = max_apdu;
else
my_max_apdu = MAX_APDU;
requestedOctetCount = my_max_apdu - 16;
/* has the previous invoke id expired or returned?
note: invoke ID = 0 is invalid, so it will be idle */
if ((invoke_id == 0) || tsm_invoke_id_free(invoke_id)) {
if (invoke_id != 0)
fileStartPosition += requestedOctetCount;
/* we'll read the file in chunks
less than max_apdu to keep unsegmented */
invoke_id =
Send_Atomic_Read_File_Stream
(Target_Device_Object_Instance,
Target_File_Object_Instance, fileStartPosition,
requestedOctetCount);
Current_Invoke_ID = invoke_id;
}
} else {
/* increment timer - exit if timed out */
elapsed_seconds += (current_seconds - last_seconds);
if (elapsed_seconds > timeout_seconds)
break;
/* wait until the device is bound, or timeout and quit */
found = address_bind_request(Target_Device_Object_Instance,
&max_apdu, &Target_Address);
if (found) {
/* calculate the smaller of our APDU size or theirs
and remove the overhead of the APDU (about 16 octets max).
note: we could fail if there is a bottle neck (router)
and smaller MPDU in betweeen. */
if (max_apdu < MAX_APDU)
my_max_apdu = max_apdu;
else
my_max_apdu = MAX_APDU;
requestedOctetCount = my_max_apdu - 16;
/* has the previous invoke id expired or returned?
note: invoke ID = 0 is invalid, so it will be idle */
if ((invoke_id == 0) || tsm_invoke_id_free(invoke_id)) {
if (invoke_id != 0)
fileStartPosition += requestedOctetCount;
/* we'll read the file in chunks
less than max_apdu to keep unsegmented */
invoke_id =
Send_Atomic_Read_File_Stream
(Target_Device_Object_Instance,
Target_File_Object_Instance, fileStartPosition,
requestedOctetCount);
Current_Invoke_ID = invoke_id;
}
} else {
/* increment timer - exit if timed out */
elapsed_seconds += (current_seconds - last_seconds);
if (elapsed_seconds > timeout_seconds)
break;
}
/* keep track of time for next check */
last_seconds = current_seconds;
+17 -24
View File
@@ -23,7 +23,7 @@
*
*********************************************************************/
/* READPROP: command line tool that reads a property from a BACnet device. */
/* command line tool that sends a BACnet service, and displays the reply */
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
@@ -183,8 +183,6 @@ int main(int argc, char *argv[])
last_seconds = time(NULL);
timeout_seconds = (Device_APDU_Timeout() / 1000) *
Device_Number_Of_APDU_Retries();
/* no need to spam the world */
I_Am_Request = false;
/* try to bind with the device */
Send_WhoIs(Target_Device_Object_Instance,
Target_Device_Object_Instance);
@@ -206,28 +204,23 @@ int main(int argc, char *argv[])
last_seconds) * 1000));
if (Error_Detected)
break;
if (I_Am_Request) {
I_Am_Request = false;
iam_send(&Handler_Transmit_Buffer[0]);
/* wait until the device is bound, or timeout and quit */
found = address_bind_request(Target_Device_Object_Instance,
&max_apdu, &Target_Address);
if (found) {
if (invoke_id == 0) {
invoke_id =
Send_Read_Property_Request
(Target_Device_Object_Instance, Target_Object_Type,
Target_Object_Instance, Target_Object_Property,
Target_Object_Index);
} else if (tsm_invoke_id_free(invoke_id))
break;
} else {
/* wait until the device is bound, or timeout and quit */
found = address_bind_request(Target_Device_Object_Instance,
&max_apdu, &Target_Address);
if (found) {
if (invoke_id == 0) {
invoke_id =
Send_Read_Property_Request
(Target_Device_Object_Instance, Target_Object_Type,
Target_Object_Instance, Target_Object_Property,
Target_Object_Index);
} else if (tsm_invoke_id_free(invoke_id))
break;
} else {
/* increment timer - exit if timed out */
elapsed_seconds += (current_seconds - last_seconds);
if (elapsed_seconds > timeout_seconds)
break;
}
/* increment timer - exit if timed out */
elapsed_seconds += (current_seconds - last_seconds);
if (elapsed_seconds > timeout_seconds)
break;
}
/* keep track of time for next check */
last_seconds = current_seconds;
+16 -23
View File
@@ -23,7 +23,7 @@
*
*********************************************************************/
/* WRITEPROP: command line tool that writes a property to a BACnet device. */
/* command line tool that sends a BACnet service, and displays the reply */
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
@@ -213,8 +213,6 @@ int main(int argc, char *argv[])
last_seconds = time(NULL);
timeout_seconds = (Device_APDU_Timeout() / 1000) *
Device_Number_Of_APDU_Retries();
/* don't send an I-Am unless asked */
I_Am_Request = false;
/* try to bind with the device */
Send_WhoIs(Target_Device_Object_Instance,
Target_Device_Object_Instance);
@@ -236,27 +234,22 @@ int main(int argc, char *argv[])
last_seconds) * 1000));
if (Error_Detected)
break;
if (I_Am_Request) {
I_Am_Request = false;
iam_send(&Handler_Transmit_Buffer[0]);
/* wait until the device is bound, or timeout and quit */
found = address_bind_request(Target_Device_Object_Instance,
&max_apdu, &Target_Address);
if (found) {
if (invoke_id == 0) {
invoke_id =
Send_Reinitialize_Device_Request
(Target_Device_Object_Instance, Reinitialize_State,
Reinitialize_Password);
} else if (tsm_invoke_id_free(invoke_id))
break;
} else {
/* wait until the device is bound, or timeout and quit */
found = address_bind_request(Target_Device_Object_Instance,
&max_apdu, &Target_Address);
if (found) {
if (invoke_id == 0) {
invoke_id =
Send_Reinitialize_Device_Request
(Target_Device_Object_Instance, Reinitialize_State,
Reinitialize_Password);
} else if (tsm_invoke_id_free(invoke_id))
break;
} else {
/* increment timer - exit if timed out */
elapsed_seconds += (current_seconds - last_seconds);
if (elapsed_seconds > timeout_seconds)
break;
}
/* increment timer - exit if timed out */
elapsed_seconds += (current_seconds - last_seconds);
if (elapsed_seconds > timeout_seconds)
break;
}
/* keep track of time for next check */
last_seconds = current_seconds;
+2 -7
View File
@@ -45,7 +45,7 @@
#include "net.h"
#include "txbuf.h"
/* This is an example application using the BACnet Stack */
/* This is an example server application using the BACnet Stack */
/* buffers used for receiving */
static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
@@ -135,7 +135,7 @@ int main(int argc, char *argv[])
/* configure the timeout values */
last_seconds = time(NULL);
/* broadcast an I-Am on startup */
I_Am_Request = true;
iam_send(&Handler_Transmit_Buffer[0]);
/* loop forever */
for (;;) {
/* input */
@@ -151,11 +151,6 @@ int main(int argc, char *argv[])
/* at least one second has passed */
if (current_seconds != last_seconds)
dcc_timer_seconds(current_seconds - last_seconds);
/* send out the I-Am if requested */
if (I_Am_Request) {
I_Am_Request = false;
iam_send(&Handler_Transmit_Buffer[0]);
}
/* output */
/* blink LEDs, Turn on or off outputs, etc */
+52 -57
View File
@@ -23,7 +23,7 @@
*
*********************************************************************/
/* READFILE: command line tool that writes a file to a BACnet device. */
/* command line tool that sends a BACnet service, and displays the reply */
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
@@ -299,64 +299,59 @@ int main(int argc, char *argv[])
printf("\r\n");
break;
}
if (I_Am_Request) {
I_Am_Request = false;
iam_send(&Handler_Transmit_Buffer[0]);
} else {
/* wait until the device is bound, or timeout and quit */
found = address_bind_request(Target_Device_Object_Instance,
&max_apdu, &Target_Address);
if (found) {
/* calculate the smaller of our APDU size or theirs
and remove the overhead of the APDU (varies depending on size).
note: we could fail if there is a bottle neck (router)
and smaller MPDU in betweeen. */
if (max_apdu < MAX_APDU)
my_max_apdu = max_apdu;
else
my_max_apdu = MAX_APDU;
/* Typical sizes are 50, 128, 206, 480, 1024, and 1476 octets */
if (my_max_apdu <= 50)
requestedOctetCount = my_max_apdu - 16;
else if (my_max_apdu <= 480)
requestedOctetCount = my_max_apdu - 32;
else if (my_max_apdu <= 1476)
requestedOctetCount = my_max_apdu - 64;
else
requestedOctetCount = my_max_apdu / 2;
/* has the previous invoke id expired or returned?
note: invoke ID = 0 is invalid, so it will be idle */
if ((invoke_id == 0) || tsm_invoke_id_free(invoke_id)) {
if (invoke_id != 0)
fileStartPosition += requestedOctetCount;
/* we'll read the file in chunks
less than max_apdu to keep unsegmented */
pFile = fopen(Local_File_Name, "rb");
if (pFile) {
(void) fseek(pFile, fileStartPosition, SEEK_SET);
len = fread(octetstring_value(&fileData), 1,
requestedOctetCount, pFile);
if (len < requestedOctetCount)
End_Of_File_Detected = true;
octetstring_truncate(&fileData, len);
fclose(pFile);
} else
/* wait until the device is bound, or timeout and quit */
found = address_bind_request(Target_Device_Object_Instance,
&max_apdu, &Target_Address);
if (found) {
/* calculate the smaller of our APDU size or theirs
and remove the overhead of the APDU (varies depending on size).
note: we could fail if there is a bottle neck (router)
and smaller MPDU in betweeen. */
if (max_apdu < MAX_APDU)
my_max_apdu = max_apdu;
else
my_max_apdu = MAX_APDU;
/* Typical sizes are 50, 128, 206, 480, 1024, and 1476 octets */
if (my_max_apdu <= 50)
requestedOctetCount = my_max_apdu - 16;
else if (my_max_apdu <= 480)
requestedOctetCount = my_max_apdu - 32;
else if (my_max_apdu <= 1476)
requestedOctetCount = my_max_apdu - 64;
else
requestedOctetCount = my_max_apdu / 2;
/* has the previous invoke id expired or returned?
note: invoke ID = 0 is invalid, so it will be idle */
if ((invoke_id == 0) || tsm_invoke_id_free(invoke_id)) {
if (invoke_id != 0)
fileStartPosition += requestedOctetCount;
/* we'll read the file in chunks
less than max_apdu to keep unsegmented */
pFile = fopen(Local_File_Name, "rb");
if (pFile) {
(void) fseek(pFile, fileStartPosition, SEEK_SET);
len = fread(octetstring_value(&fileData), 1,
requestedOctetCount, pFile);
if (len < requestedOctetCount)
End_Of_File_Detected = true;
printf("\rSending %d bytes",
(fileStartPosition + len));
invoke_id =
Send_Atomic_Write_File_Stream
(Target_Device_Object_Instance,
Target_File_Object_Instance, fileStartPosition,
&fileData);
Current_Invoke_ID = invoke_id;
}
} else {
/* increment timer - exit if timed out */
elapsed_seconds += (current_seconds - last_seconds);
if (elapsed_seconds > timeout_seconds)
break;
octetstring_truncate(&fileData, len);
fclose(pFile);
} else
End_Of_File_Detected = true;
printf("\rSending %d bytes",
(fileStartPosition + len));
invoke_id =
Send_Atomic_Write_File_Stream
(Target_Device_Object_Instance,
Target_File_Object_Instance, fileStartPosition,
&fileData);
Current_Invoke_ID = invoke_id;
}
} else {
/* increment timer - exit if timed out */
elapsed_seconds += (current_seconds - last_seconds);
if (elapsed_seconds > timeout_seconds)
break;
}
/* keep track of time for next check */
last_seconds = current_seconds;
+19 -24
View File
@@ -23,7 +23,7 @@
*
*********************************************************************/
/* WRITEPROP: command line tool that writes a property to a BACnet device. */
/* command line tool that sends a BACnet service, and displays the reply */
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
@@ -309,30 +309,25 @@ int main(int argc, char *argv[])
last_seconds) * 1000));
if (Error_Detected)
break;
if (I_Am_Request) {
I_Am_Request = false;
iam_send(&Handler_Transmit_Buffer[0]);
/* wait until the device is bound, or timeout and quit */
found = address_bind_request(Target_Device_Object_Instance,
&max_apdu, &Target_Address);
if (found) {
if (invoke_id == 0) {
invoke_id =
Send_Write_Property_Request
(Target_Device_Object_Instance, Target_Object_Type,
Target_Object_Instance, Target_Object_Property,
&Target_Object_Property_Value,
Target_Object_Property_Priority,
Target_Object_Property_Index);
} else if (tsm_invoke_id_free(invoke_id))
break;
} else {
/* wait until the device is bound, or timeout and quit */
found = address_bind_request(Target_Device_Object_Instance,
&max_apdu, &Target_Address);
if (found) {
if (invoke_id == 0) {
invoke_id =
Send_Write_Property_Request
(Target_Device_Object_Instance, Target_Object_Type,
Target_Object_Instance, Target_Object_Property,
&Target_Object_Property_Value,
Target_Object_Property_Priority,
Target_Object_Property_Index);
} else if (tsm_invoke_id_free(invoke_id))
break;
} else {
/* increment timer - exit if timed out */
elapsed_seconds += (current_seconds - last_seconds);
if (elapsed_seconds > timeout_seconds)
break;
}
/* increment timer - exit if timed out */
elapsed_seconds += (current_seconds - last_seconds);
if (elapsed_seconds > timeout_seconds)
break;
}
/* keep track of time for next check */
last_seconds = current_seconds;