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