From 676ecf77a238727c0a5f593f883ec7517522bf1c Mon Sep 17 00:00:00 2001 From: skarg Date: Mon, 20 Feb 2006 16:17:46 +0000 Subject: [PATCH] refactored the Who-Is and I-Am handling, and cleaned up some comments. --- bacnet-stack/demo/handler/h_whois.c | 17 ++-- bacnet-stack/demo/handler/handlers.h | 3 - bacnet-stack/demo/object/device.c | 7 +- bacnet-stack/demo/readfile/readfile.c | 69 +++++++-------- bacnet-stack/demo/readprop/readprop.c | 41 ++++----- bacnet-stack/demo/reinit/main.c | 39 ++++----- bacnet-stack/demo/server/server.c | 9 +- bacnet-stack/demo/writefile/writefile.c | 109 +++++++++++------------- bacnet-stack/demo/writeprop/writeprop.c | 43 +++++----- 9 files changed, 149 insertions(+), 188 deletions(-) diff --git a/bacnet-stack/demo/handler/h_whois.c b/bacnet-stack/demo/handler/h_whois.c index c661fcfb..1071ed98 100644 --- a/bacnet-stack/demo/handler/h_whois.c +++ b/bacnet-stack/demo/handler/h_whois.c @@ -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; diff --git a/bacnet-stack/demo/handler/handlers.h b/bacnet-stack/demo/handler/handlers.h index e937e01f..eea70115 100644 --- a/bacnet-stack/demo/handler/handlers.h +++ b/bacnet-stack/demo/handler/handlers.h @@ -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 */ diff --git a/bacnet-stack/demo/object/device.c b/bacnet-stack/demo/object/device.c index ee7ff001..3518bbae 100644 --- a/bacnet-stack/demo/object/device.c +++ b/bacnet-stack/demo/object/device.c @@ -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; diff --git a/bacnet-stack/demo/readfile/readfile.c b/bacnet-stack/demo/readfile/readfile.c index ceb83ec1..27f4ebe4 100644 --- a/bacnet-stack/demo/readfile/readfile.c +++ b/bacnet-stack/demo/readfile/readfile.c @@ -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 #include #include @@ -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; diff --git a/bacnet-stack/demo/readprop/readprop.c b/bacnet-stack/demo/readprop/readprop.c index 31fe44c5..cb064d8f 100644 --- a/bacnet-stack/demo/readprop/readprop.c +++ b/bacnet-stack/demo/readprop/readprop.c @@ -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 #include #include @@ -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; diff --git a/bacnet-stack/demo/reinit/main.c b/bacnet-stack/demo/reinit/main.c index c53223cc..da05ac10 100644 --- a/bacnet-stack/demo/reinit/main.c +++ b/bacnet-stack/demo/reinit/main.c @@ -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 #include #include @@ -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; diff --git a/bacnet-stack/demo/server/server.c b/bacnet-stack/demo/server/server.c index ec3a4076..f57f5c5d 100644 --- a/bacnet-stack/demo/server/server.c +++ b/bacnet-stack/demo/server/server.c @@ -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 */ diff --git a/bacnet-stack/demo/writefile/writefile.c b/bacnet-stack/demo/writefile/writefile.c index 8d57406e..4fbb7cc7 100644 --- a/bacnet-stack/demo/writefile/writefile.c +++ b/bacnet-stack/demo/writefile/writefile.c @@ -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 #include #include @@ -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; diff --git a/bacnet-stack/demo/writeprop/writeprop.c b/bacnet-stack/demo/writeprop/writeprop.c index 7da4b9b8..6ac4dbe5 100644 --- a/bacnet-stack/demo/writeprop/writeprop.c +++ b/bacnet-stack/demo/writeprop/writeprop.c @@ -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 #include #include @@ -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;