From 411d6c1b24c57949cb2e4c614ef7ff90c5a8a8ed Mon Sep 17 00:00:00 2001 From: skarg Date: Thu, 29 Nov 2007 15:56:53 +0000 Subject: [PATCH] Indented. --- bacnet-stack/demo/dcc/main.c | 49 +- bacnet-stack/demo/epics/main.c | 99 +- bacnet-stack/demo/handler/h_arf.c | 12 +- bacnet-stack/demo/handler/h_arf_a.c | 9 +- bacnet-stack/demo/handler/h_awf.c | 14 +- bacnet-stack/demo/handler/h_dcc.c | 10 +- bacnet-stack/demo/handler/h_iam.c | 12 +- bacnet-stack/demo/handler/h_ihave.c | 9 +- bacnet-stack/demo/handler/h_rd.c | 10 +- bacnet-stack/demo/handler/h_rp.c | 99 +- bacnet-stack/demo/handler/h_rp_a.c | 12 +- bacnet-stack/demo/handler/h_rpm.c | 333 +- bacnet-stack/demo/handler/h_ts.c | 17 +- bacnet-stack/demo/handler/h_whohas.c | 15 +- bacnet-stack/demo/handler/h_whois.c | 6 +- bacnet-stack/demo/handler/h_wp.c | 435 ++- bacnet-stack/demo/handler/noserv.c | 6 +- bacnet-stack/demo/handler/s_arfs.c | 6 +- bacnet-stack/demo/handler/s_awfs.c | 12 +- bacnet-stack/demo/handler/s_dcc.c | 9 +- bacnet-stack/demo/handler/s_ihave.c | 6 +- bacnet-stack/demo/handler/s_rd.c | 6 +- bacnet-stack/demo/handler/s_rp.c | 9 +- bacnet-stack/demo/handler/s_ts.c | 8 +- bacnet-stack/demo/handler/s_whohas.c | 12 +- bacnet-stack/demo/handler/s_whois.c | 4 +- bacnet-stack/demo/handler/s_wp.c | 24 +- bacnet-stack/demo/object/ai.c | 144 +- bacnet-stack/demo/object/ao.c | 350 ++- bacnet-stack/demo/object/av.c | 356 +-- bacnet-stack/demo/object/bacfile.c | 260 +- bacnet-stack/demo/object/bi.c | 146 +- bacnet-stack/demo/object/bo.c | 379 +-- bacnet-stack/demo/object/bv.c | 364 +-- bacnet-stack/demo/object/device.c | 943 +++--- bacnet-stack/demo/object/lc.c | 1186 ++++---- bacnet-stack/demo/object/lo.c | 443 +-- bacnet-stack/demo/object/lsp.c | 270 +- bacnet-stack/demo/object/mso.c | 391 +-- bacnet-stack/demo/readfile/main.c | 68 +- bacnet-stack/demo/readprop/main.c | 51 +- bacnet-stack/demo/reinit/main.c | 58 +- bacnet-stack/demo/server/main.c | 23 +- bacnet-stack/demo/timesync/main.c | 34 +- bacnet-stack/demo/ucov/main.c | 18 +- bacnet-stack/demo/whohas/main.c | 37 +- bacnet-stack/demo/whois/main.c | 37 +- bacnet-stack/demo/writefile/main.c | 55 +- bacnet-stack/demo/writeprop/main.c | 57 +- bacnet-stack/include/abort.h | 28 +- bacnet-stack/include/address.h | 55 +- bacnet-stack/include/ai.h | 25 +- bacnet-stack/include/ao.h | 47 +- bacnet-stack/include/apdu.h | 93 +- bacnet-stack/include/arcnet.h | 29 +- bacnet-stack/include/arf.h | 50 +- bacnet-stack/include/av.h | 31 +- bacnet-stack/include/awf.h | 50 +- bacnet-stack/include/bacaddr.h | 12 +- bacnet-stack/include/bacapp.h | 79 +- bacnet-stack/include/bacdcode.h | 238 +- bacnet-stack/include/bacdef.h | 4 +- bacnet-stack/include/bacenum.h | 102 +- bacnet-stack/include/bacerror.h | 31 +- bacnet-stack/include/bacfile.h | 40 +- bacnet-stack/include/bacint.h | 64 +- bacnet-stack/include/bacprop.h | 18 +- bacnet-stack/include/bacreal.h | 19 +- bacnet-stack/include/bacstr.h | 110 +- bacnet-stack/include/bactext.h | 63 +- bacnet-stack/include/bi.h | 25 +- bacnet-stack/include/bigend.h | 7 +- bacnet-stack/include/bip.h | 53 +- bacnet-stack/include/bo.h | 31 +- bacnet-stack/include/bv.h | 31 +- bacnet-stack/include/bvlc.h | 18 +- bacnet-stack/include/bytes.h | 2 +- bacnet-stack/include/client.h | 79 +- bacnet-stack/include/config.h | 46 +- bacnet-stack/include/cov.h | 74 +- bacnet-stack/include/crc.h | 12 +- bacnet-stack/include/datalink.h | 36 +- bacnet-stack/include/datetime.h | 84 +- bacnet-stack/include/dcc.h | 44 +- bacnet-stack/include/device.h | 119 +- bacnet-stack/include/dlmstp.h | 56 +- bacnet-stack/include/ethernet.h | 32 +- bacnet-stack/include/filename.h | 7 +- bacnet-stack/include/handlers.h | 82 +- bacnet-stack/include/iam.h | 39 +- bacnet-stack/include/ihave.h | 23 +- bacnet-stack/include/indtext.h | 52 +- bacnet-stack/include/keylist.h | 49 +- bacnet-stack/include/lc.h | 34 +- bacnet-stack/include/lo.h | 51 +- bacnet-stack/include/lsp.h | 30 +- bacnet-stack/include/mso.h | 30 +- bacnet-stack/include/mstp.h | 44 +- bacnet-stack/include/mstptext.h | 14 +- bacnet-stack/include/npdu.h | 37 +- bacnet-stack/include/rd.h | 16 +- bacnet-stack/include/reject.h | 27 +- bacnet-stack/include/ringbuf.h | 27 +- bacnet-stack/include/rp.h | 45 +- bacnet-stack/include/rpm.h | 105 +- bacnet-stack/include/sbuf.h | 42 +- bacnet-stack/include/timesync.h | 40 +- bacnet-stack/include/tsm.h | 44 +- bacnet-stack/include/whohas.h | 27 +- bacnet-stack/include/whois.h | 27 +- bacnet-stack/include/wp.h | 30 +- bacnet-stack/ports/arm7/_stdint.h | 2 +- bacnet-stack/ports/arm7/bip.c | 198 +- bacnet-stack/ports/arm7/net.h | 84 +- bacnet-stack/ports/at91sam7s/ai.c | 141 +- bacnet-stack/ports/at91sam7s/at91sam7s256.h | 3033 ++++++++++--------- bacnet-stack/ports/at91sam7s/av.c | 362 +-- bacnet-stack/ports/at91sam7s/bi.c | 155 +- bacnet-stack/ports/at91sam7s/blinker.c | 20 +- bacnet-stack/ports/at91sam7s/board.h | 52 +- bacnet-stack/ports/at91sam7s/bv.c | 295 +- bacnet-stack/ports/at91sam7s/device.c | 620 ++-- bacnet-stack/ports/at91sam7s/dlmstp.c | 1015 +++---- bacnet-stack/ports/at91sam7s/h_rp.c | 46 +- bacnet-stack/ports/at91sam7s/h_wp.c | 115 +- bacnet-stack/ports/at91sam7s/init.c | 47 +- bacnet-stack/ports/at91sam7s/isr.c | 30 +- bacnet-stack/ports/at91sam7s/main.c | 59 +- bacnet-stack/ports/at91sam7s/rs485.c | 95 +- bacnet-stack/ports/at91sam7s/rs485.h | 25 +- bacnet-stack/ports/at91sam7s/timer.c | 38 +- bacnet-stack/ports/at91sam7s/timer.h | 13 +- bacnet-stack/ports/atmega168/ai.c | 120 +- bacnet-stack/ports/atmega168/apdu.c | 88 +- bacnet-stack/ports/atmega168/av.c | 164 +- bacnet-stack/ports/atmega168/avr035.h | 2 +- bacnet-stack/ports/atmega168/device.c | 505 +-- bacnet-stack/ports/atmega168/dlmstp.c | 919 +++--- bacnet-stack/ports/atmega168/h_rp.c | 53 +- bacnet-stack/ports/atmega168/h_whois.c | 9 +- bacnet-stack/ports/atmega168/h_wp.c | 119 +- bacnet-stack/ports/atmega168/hardware.h | 2 +- bacnet-stack/ports/atmega168/main.c | 68 +- bacnet-stack/ports/atmega168/rs485.c | 116 +- bacnet-stack/ports/atmega168/rs485.h | 28 +- bacnet-stack/ports/atmega168/timer.c | 35 +- bacnet-stack/ports/atmega168/timer.h | 14 +- bacnet-stack/ports/linux/arcnet.c | 52 +- bacnet-stack/ports/linux/bip-init.c | 32 +- bacnet-stack/ports/linux/dlmstp.c | 303 +- bacnet-stack/ports/linux/ethernet.c | 81 +- bacnet-stack/ports/linux/main.c | 51 +- bacnet-stack/ports/linux/net.h | 2 +- bacnet-stack/ports/linux/rs485.c | 96 +- bacnet-stack/ports/linux/rs485.h | 22 +- bacnet-stack/ports/linux/rx_fsm.c | 72 +- bacnet-stack/ports/pic18f6720/ai.c | 135 +- bacnet-stack/ports/pic18f6720/av.c | 356 +-- bacnet-stack/ports/pic18f6720/bi.c | 149 +- bacnet-stack/ports/pic18f6720/bv.c | 289 +- bacnet-stack/ports/pic18f6720/device.c | 636 ++-- bacnet-stack/ports/pic18f6720/dlmstp.c | 82 +- bacnet-stack/ports/pic18f6720/dlmstp.h | 59 +- bacnet-stack/ports/pic18f6720/h_rp.c | 182 +- bacnet-stack/ports/pic18f6720/h_wp.c | 180 +- bacnet-stack/ports/pic18f6720/hardware.h | 38 +- bacnet-stack/ports/pic18f6720/isr.c | 51 +- bacnet-stack/ports/pic18f6720/main.c | 61 +- bacnet-stack/ports/pic18f6720/mstp.c | 1535 +++++----- bacnet-stack/ports/pic18f6720/mstp.h | 25 +- bacnet-stack/ports/pic18f6720/rs485.c | 121 +- bacnet-stack/ports/pic18f6720/rs485.h | 31 +- bacnet-stack/ports/pic18f6720/stdint.h | 2 +- bacnet-stack/ports/rtos32/bip-init.c | 53 +- bacnet-stack/ports/rtos32/dlmstp.c | 69 +- bacnet-stack/ports/rtos32/ethernet.c | 75 +- bacnet-stack/ports/rtos32/init.c | 15 +- bacnet-stack/ports/rtos32/main.c | 36 +- bacnet-stack/ports/rtos32/mstp.c | 1733 +++++------ bacnet-stack/ports/rtos32/mstp.h | 28 +- bacnet-stack/ports/rtos32/netcfg.h | 18 +- bacnet-stack/ports/rtos32/rs485.c | 81 +- bacnet-stack/ports/rtos32/rs485.h | 13 +- bacnet-stack/ports/rtos32/stdint.h | 2 +- bacnet-stack/ports/win32/bip-init.c | 78 +- bacnet-stack/ports/win32/dlmstp.c | 257 +- bacnet-stack/ports/win32/ethernet.c | 101 +- bacnet-stack/ports/win32/main.c | 35 +- bacnet-stack/ports/win32/rs485.c | 190 +- bacnet-stack/ports/win32/rs485.h | 22 +- bacnet-stack/ports/win32/rx_fsm.c | 63 +- bacnet-stack/ports/win32/stdint.h | 10 +- bacnet-stack/src/abort.c | 40 +- bacnet-stack/src/address.c | 73 +- bacnet-stack/src/apdu.c | 448 +-- bacnet-stack/src/arf.c | 143 +- bacnet-stack/src/awf.c | 128 +- bacnet-stack/src/bacaddr.c | 60 +- bacnet-stack/src/bacapp.c | 1007 +++--- bacnet-stack/src/bacdcode.c | 601 ++-- bacnet-stack/src/bacerror.c | 66 +- bacnet-stack/src/bacint.c | 176 +- bacnet-stack/src/bacprop.c | 24 +- bacnet-stack/src/bacreal.c | 23 +- bacnet-stack/src/bacstr.c | 145 +- bacnet-stack/src/bactext.c | 57 +- bacnet-stack/src/bigend.c | 3 +- bacnet-stack/src/bip.c | 74 +- bacnet-stack/src/bvlc.c | 399 ++- bacnet-stack/src/cov.c | 193 +- bacnet-stack/src/crc.c | 51 +- bacnet-stack/src/datalink.c | 77 +- bacnet-stack/src/datetime.c | 154 +- bacnet-stack/src/dcc.c | 65 +- bacnet-stack/src/filename.c | 13 +- bacnet-stack/src/iam.c | 94 +- bacnet-stack/src/ihave.c | 49 +- bacnet-stack/src/indtext.c | 55 +- bacnet-stack/src/key.c | 11 +- bacnet-stack/src/keylist.c | 114 +- bacnet-stack/src/mstp.c | 2256 +++++++------- bacnet-stack/src/mstptext.c | 22 +- bacnet-stack/src/npdu.c | 87 +- bacnet-stack/src/rd.c | 37 +- bacnet-stack/src/reject.c | 32 +- bacnet-stack/src/ringbuf.c | 43 +- bacnet-stack/src/rp.c | 72 +- bacnet-stack/src/rpm.c | 155 +- bacnet-stack/src/sbuf.c | 50 +- bacnet-stack/src/timesync.c | 87 +- bacnet-stack/src/tsm.c | 79 +- bacnet-stack/src/whohas.c | 38 +- bacnet-stack/src/whois.c | 40 +- bacnet-stack/src/wp.c | 134 +- bacnet-stack/test/ctest.c | 71 +- bacnet-stack/test/ctest.h | 61 +- 236 files changed, 17864 insertions(+), 15724 deletions(-) diff --git a/bacnet-stack/demo/dcc/main.c b/bacnet-stack/demo/dcc/main.c index fa925452..e2bfd925 100644 --- a/bacnet-stack/demo/dcc/main.c +++ b/bacnet-stack/demo/dcc/main.c @@ -28,7 +28,7 @@ #include #include #include -#include /* for time */ +#include /* for time */ #include #include "bactext.h" #include "iam.h" @@ -63,9 +63,11 @@ static char *Communication_Password = NULL; static bool Error_Detected = false; -static void MyErrorHandler(BACNET_ADDRESS * src, +static void MyErrorHandler( + BACNET_ADDRESS * src, uint8_t invoke_id, - BACNET_ERROR_CLASS error_class, BACNET_ERROR_CODE error_code) + BACNET_ERROR_CLASS error_class, + BACNET_ERROR_CODE error_code) { /* FIXME: verify src and invoke id */ (void) src; @@ -76,30 +78,34 @@ static void MyErrorHandler(BACNET_ADDRESS * src, Error_Detected = true; } -void MyAbortHandler(BACNET_ADDRESS * src, - uint8_t invoke_id, uint8_t abort_reason, bool server) +void MyAbortHandler( + BACNET_ADDRESS * src, + uint8_t invoke_id, + uint8_t abort_reason, + bool server) { /* FIXME: verify src and invoke id */ (void) src; (void) invoke_id; (void) server; - printf("BACnet Abort: %s\r\n", - bactext_abort_reason_name(abort_reason)); + printf("BACnet Abort: %s\r\n", bactext_abort_reason_name(abort_reason)); Error_Detected = true; } -void MyRejectHandler(BACNET_ADDRESS * src, - uint8_t invoke_id, uint8_t reject_reason) +void MyRejectHandler( + BACNET_ADDRESS * src, + uint8_t invoke_id, + uint8_t reject_reason) { /* FIXME: verify src and invoke id */ (void) src; (void) invoke_id; - printf("BACnet Reject: %s\r\n", - bactext_reject_reason_name(reject_reason)); + printf("BACnet Reject: %s\r\n", bactext_reject_reason_name(reject_reason)); Error_Detected = true; } -void MyDeviceCommunicationControlSimpleAckHandler(BACNET_ADDRESS * src, +void MyDeviceCommunicationControlSimpleAckHandler( + BACNET_ADDRESS * src, uint8_t invoke_id) { (void) src; @@ -107,15 +113,14 @@ void MyDeviceCommunicationControlSimpleAckHandler(BACNET_ADDRESS * src, printf("DeviceCommunicationControl Acknowledged!\r\n"); } -static void Init_Service_Handlers(void) +static void Init_Service_Handlers( + void) { /* we need to handle who-is to support dynamic device binding to us */ - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, - handler_who_is); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is); /* handle i-am to support binding to other devices */ - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, - handler_i_am_bind); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, handler_i_am_bind); /* set the handler for all the services we don't implement It is required to send the proper reject message... */ apdu_set_unrecognized_service_handler_handler @@ -138,7 +143,9 @@ static void Init_Service_Handlers(void) apdu_set_reject_handler(MyRejectHandler); } -int main(int argc, char *argv[]) +int main( + int argc, + char *argv[]) { BACNET_ADDRESS src = { 0 }; /* address where message came from */ uint16_t pdu_len = 0; @@ -192,8 +199,7 @@ int main(int argc, char *argv[]) timeout_seconds = (Device_APDU_Timeout() / 1000) * Device_Number_Of_APDU_Retries(); /* try to bind with the device */ - Send_WhoIs(Target_Device_Object_Instance, - Target_Device_Object_Instance); + Send_WhoIs(Target_Device_Object_Instance, Target_Device_Object_Instance); /* loop forever */ for (;;) { /* increment timer - exit if timed out */ @@ -208,8 +214,7 @@ int main(int argc, char *argv[]) } /* at least one second has passed */ if (current_seconds != last_seconds) - tsm_timer_milliseconds(((current_seconds - - last_seconds) * 1000)); + tsm_timer_milliseconds(((current_seconds - last_seconds) * 1000)); if (Error_Detected) break; /* wait until the device is bound, or timeout and quit */ diff --git a/bacnet-stack/demo/epics/main.c b/bacnet-stack/demo/epics/main.c index 4125efa4..884c6658 100644 --- a/bacnet-stack/demo/epics/main.c +++ b/bacnet-stack/demo/epics/main.c @@ -28,7 +28,7 @@ #include #include #include -#include /* for time */ +#include /* for time */ #include #include "bactext.h" #include "iam.h" @@ -69,56 +69,62 @@ static BACNET_RP_SERVICE_DATA Read_Property_Data; /* FIXME: keep the object list in here */ /* static OS_Keylist Object_List; */ -static void MyErrorHandler(BACNET_ADDRESS * src, +static void MyErrorHandler( + BACNET_ADDRESS * src, uint8_t invoke_id, - BACNET_ERROR_CLASS error_class, BACNET_ERROR_CODE error_code) + BACNET_ERROR_CLASS error_class, + BACNET_ERROR_CODE error_code) { /* FIXME: verify src and invoke id */ (void) src; (void) invoke_id; - #if 1 +#if 1 printf("BACnet Error: %s: %s\r\n", bactext_error_class_name(error_class), bactext_error_code_name(error_code)); - #else +#else (void) error_class; (void) error_code; - #endif +#endif Error_Detected = true; } -void MyAbortHandler(BACNET_ADDRESS * src, - uint8_t invoke_id, uint8_t abort_reason, bool server) +void MyAbortHandler( + BACNET_ADDRESS * src, + uint8_t invoke_id, + uint8_t abort_reason, + bool server) { /* FIXME: verify src and invoke id */ (void) src; (void) invoke_id; (void) server; - #if 1 - printf("BACnet Abort: %s\r\n", - bactext_abort_reason_name(abort_reason)); - #else +#if 1 + printf("BACnet Abort: %s\r\n", bactext_abort_reason_name(abort_reason)); +#else (void) abort_reason; - #endif +#endif Error_Detected = true; } -void MyRejectHandler(BACNET_ADDRESS * src, - uint8_t invoke_id, uint8_t reject_reason) +void MyRejectHandler( + BACNET_ADDRESS * src, + uint8_t invoke_id, + uint8_t reject_reason) { /* FIXME: verify src and invoke id */ (void) src; (void) invoke_id; - #if 1 - printf("BACnet Reject: %s\r\n", - bactext_reject_reason_name(reject_reason)); - #else +#if 1 + printf("BACnet Reject: %s\r\n", bactext_reject_reason_name(reject_reason)); +#else (void) reject_reason; - #endif +#endif Error_Detected = true; } -void PrintReadPropertyData(BACNET_READ_PROPERTY_DATA * data) +void PrintReadPropertyData( + BACNET_READ_PROPERTY_DATA * data) { BACNET_APPLICATION_DATA_VALUE value; /* for decode value data */ int len = 0; @@ -170,16 +176,17 @@ void PrintReadPropertyData(BACNET_READ_PROPERTY_DATA * data) } } -void MyReadPropertyAckHandler(uint8_t * service_request, +void MyReadPropertyAckHandler( + uint8_t * service_request, uint16_t service_len, - BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_ACK_DATA * service_data) + BACNET_ADDRESS * src, + BACNET_CONFIRMED_SERVICE_ACK_DATA * service_data) { int len = 0; BACNET_READ_PROPERTY_DATA data; (void) src; - len = rp_ack_decode_service_request(service_request, - service_len, &data); + len = rp_ack_decode_service_request(service_request, service_len, &data); if (len > 0) { memmove(&Read_Property_Data.service_data, service_data, sizeof(data)); memmove(&Read_Property_Data.data, &data, sizeof(data)); @@ -187,15 +194,14 @@ void MyReadPropertyAckHandler(uint8_t * service_request, } } -static void Init_Service_Handlers(void) +static void Init_Service_Handlers( + void) { /* we need to handle who-is to support dynamic device binding to us */ - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, - handler_who_is); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is); /* handle i-am to support binding to other devices */ - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, - handler_i_am_bind); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, handler_i_am_bind); /* set the handler for all the services we don't implement It is required to send the proper reject message... */ apdu_set_unrecognized_service_handler_handler @@ -207,13 +213,13 @@ static void Init_Service_Handlers(void) apdu_set_confirmed_ack_handler(SERVICE_CONFIRMED_READ_PROPERTY, MyReadPropertyAckHandler); /* handle any errors coming back */ - apdu_set_error_handler(SERVICE_CONFIRMED_READ_PROPERTY, - MyErrorHandler); + apdu_set_error_handler(SERVICE_CONFIRMED_READ_PROPERTY, MyErrorHandler); apdu_set_abort_handler(MyAbortHandler); apdu_set_reject_handler(MyRejectHandler); } -static uint8_t Read_Properties(uint32_t device_instance) +static uint8_t Read_Properties( + uint32_t device_instance) { uint8_t invoke_id = 0; static unsigned index = 0; @@ -226,12 +232,10 @@ static uint8_t Read_Properties(uint32_t device_instance) } if (pRequired[index] != -1) { - printf(" %s: ",bactext_property_name(pRequired[index])); + printf(" %s: ", bactext_property_name(pRequired[index])); invoke_id = Send_Read_Property_Request(device_instance, OBJECT_DEVICE, - device_instance, - pRequired[index], - BACNET_ARRAY_ALL); + device_instance, pRequired[index], BACNET_ARRAY_ALL); if (invoke_id) { index++; } @@ -240,7 +244,9 @@ static uint8_t Read_Properties(uint32_t device_instance) return invoke_id; } -int main(int argc, char *argv[]) +int main( + int argc, + char *argv[]) { BACNET_ADDRESS src = { 0 }; /* address where message came from */ uint16_t pdu_len = 0; @@ -257,9 +263,7 @@ int main(int argc, char *argv[]) /* print help if not enough arguments */ if (argc < 2) { - printf - ("%s device-instance\r\n", - filename_remove_path(argv[0])); + printf("%s device-instance\r\n", filename_remove_path(argv[0])); return 0; } @@ -267,7 +271,7 @@ int main(int argc, char *argv[]) Target_Device_Object_Instance = strtol(argv[1], NULL, 0); if (Target_Device_Object_Instance > BACNET_MAX_INSTANCE) { fprintf(stderr, "device-instance=%u - it must be less than %u\r\n", - Target_Device_Object_Instance, BACNET_MAX_INSTANCE+1); + Target_Device_Object_Instance, BACNET_MAX_INSTANCE + 1); return 1; } /* setup my info */ @@ -281,8 +285,7 @@ int main(int argc, char *argv[]) timeout_seconds = (Device_APDU_Timeout() / 1000) * Device_Number_Of_APDU_Retries(); /* try to bind with the device */ - Send_WhoIs(Target_Device_Object_Instance, - Target_Device_Object_Instance); + Send_WhoIs(Target_Device_Object_Instance, Target_Device_Object_Instance); printf("List of Objects in test device:\r\n"); printf("{\r\n"); /* loop forever */ @@ -299,8 +302,7 @@ int main(int argc, char *argv[]) } /* at least one second has passed */ if (current_seconds != last_seconds) { - tsm_timer_milliseconds(((current_seconds - - last_seconds) * 1000)); + tsm_timer_milliseconds(((current_seconds - last_seconds) * 1000)); } /* wait until the device is bound, or timeout and quit */ found = address_bind_request(Target_Device_Object_Instance, @@ -308,7 +310,7 @@ int main(int argc, char *argv[]) if (found) { /* invoke ID is set to zero when it is not in use */ if (invoke_id == 0) { - invoke_id = Read_Properties(Target_Device_Object_Instance); + invoke_id = Read_Properties(Target_Device_Object_Instance); if (invoke_id == 0) { break; } @@ -321,8 +323,7 @@ int main(int argc, char *argv[]) } } else if (tsm_invoke_id_free(invoke_id)) { invoke_id = 0; - } - else if (tsm_invoke_id_failed(invoke_id)) { + } else if (tsm_invoke_id_failed(invoke_id)) { fprintf(stderr, "\rError: TSM Timeout!\r\n"); tsm_free_invoke_id(invoke_id); invoke_id = 0; diff --git a/bacnet-stack/demo/handler/h_arf.c b/bacnet-stack/demo/handler/h_arf.c index 871a12bf..df575bf9 100644 --- a/bacnet-stack/demo/handler/h_arf.c +++ b/bacnet-stack/demo/handler/h_arf.c @@ -40,7 +40,7 @@ #include "ai.h" #include "ao.h" #if defined(BACFILE) - #include "bacfile.h" +#include "bacfile.h" #endif /* @@ -95,9 +95,11 @@ shall be TRUE, otherwise FALSE. */ #if defined(BACFILE) -void handler_atomic_read_file(uint8_t * service_request, +void handler_atomic_read_file( + uint8_t * service_request, uint16_t service_len, - BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data) + BACNET_ADDRESS * src, + BACNET_CONFIRMED_SERVICE_DATA * service_data) { BACNET_ATOMIC_READ_FILE_DATA data; int len = 0; @@ -125,7 +127,7 @@ void handler_atomic_read_file(uint8_t * service_request, fprintf(stderr, "ARF: Segmented Message. Sending Abort!\n"); #endif goto ARF_ABORT; - } + } len = arf_decode_service_request(service_request, service_len, &data); /* bad decoding - send an abort */ if (len < 0) { @@ -186,7 +188,7 @@ void handler_atomic_read_file(uint8_t * service_request, service_data->invoke_id, SERVICE_CONFIRMED_ATOMIC_READ_FILE, error_class, error_code); } -ARF_ABORT: + ARF_ABORT: pdu_len += len; bytes_sent = datalink_send_pdu(src, &npdu_data, &Handler_Transmit_Buffer[0], pdu_len); diff --git a/bacnet-stack/demo/handler/h_arf_a.c b/bacnet-stack/demo/handler/h_arf_a.c index 994f6086..e6d2a80e 100644 --- a/bacnet-stack/demo/handler/h_arf_a.c +++ b/bacnet-stack/demo/handler/h_arf_a.c @@ -47,9 +47,11 @@ /* that someone can read from us. It is common to */ /* use the description as the file name. */ #if defined(BACFILE) -void handler_atomic_read_file_ack(uint8_t * service_request, +void handler_atomic_read_file_ack( + uint8_t * service_request, uint16_t service_len, - BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_ACK_DATA * service_data) + BACNET_ADDRESS * src, + BACNET_CONFIRMED_SERVICE_ACK_DATA * service_data) { int len = 0; BACNET_ATOMIC_READ_FILE_DATA data; @@ -60,8 +62,7 @@ void handler_atomic_read_file_ack(uint8_t * service_request, (void) src; /* get the file instance from the tsm data before freeing it */ instance = bacfile_instance_from_tsm(service_data->invoke_id); - len = arf_ack_decode_service_request(service_request, - service_len, &data); + len = arf_ack_decode_service_request(service_request, service_len, &data); #if PRINT_ENABLED fprintf(stderr, "Received Read-File Ack!\n"); #endif diff --git a/bacnet-stack/demo/handler/h_awf.c b/bacnet-stack/demo/handler/h_awf.c index 4cdea9f1..021d24a7 100644 --- a/bacnet-stack/demo/handler/h_awf.c +++ b/bacnet-stack/demo/handler/h_awf.c @@ -40,7 +40,7 @@ /* demo objects */ #include "device.h" #if defined(BACFILE) - #include "bacfile.h" +#include "bacfile.h" #endif /* @@ -73,9 +73,11 @@ standard. */ #if defined(BACFILE) -void handler_atomic_write_file(uint8_t * service_request, +void handler_atomic_write_file( + uint8_t * service_request, uint16_t service_len, - BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data) + BACNET_ADDRESS * src, + BACNET_CONFIRMED_SERVICE_DATA * service_data) { BACNET_ATOMIC_WRITE_FILE_DATA data; int len = 0; @@ -113,7 +115,7 @@ void handler_atomic_write_file(uint8_t * service_request, fprintf(stderr, "Bad Encoding. Sending Abort!\n"); #endif goto AWF_ABORT; - } + } if (data.object_type == OBJECT_FILE) { if (!bacfile_valid_instance(data.object_instance)) { error = true; @@ -126,7 +128,7 @@ void handler_atomic_write_file(uint8_t * service_request, #endif len = awf_ack_encode_apdu(&Handler_Transmit_Buffer - [pdu_len], service_data->invoke_id, &data); + [pdu_len], service_data->invoke_id, &data); } else { error = true; error_class = ERROR_CLASS_OBJECT; @@ -151,7 +153,7 @@ void handler_atomic_write_file(uint8_t * service_request, service_data->invoke_id, SERVICE_CONFIRMED_ATOMIC_READ_FILE, error_class, error_code); } -AWF_ABORT: + AWF_ABORT: pdu_len += len; bytes_sent = datalink_send_pdu(src, &npdu_data, &Handler_Transmit_Buffer[0], pdu_len); diff --git a/bacnet-stack/demo/handler/h_dcc.c b/bacnet-stack/demo/handler/h_dcc.c index 504a67de..334afc93 100644 --- a/bacnet-stack/demo/handler/h_dcc.c +++ b/bacnet-stack/demo/handler/h_dcc.c @@ -40,9 +40,11 @@ static char *My_Password = "filister"; -void handler_device_communication_control(uint8_t * service_request, +void handler_device_communication_control( + uint8_t * service_request, uint16_t service_len, - BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data) + BACNET_ADDRESS * src, + BACNET_CONFIRMED_SERVICE_DATA * service_data) { uint16_t timeDuration = 0; BACNET_COMMUNICATION_ENABLE_DISABLE state = COMMUNICATION_ENABLE; @@ -70,7 +72,7 @@ void handler_device_communication_control(uint8_t * service_request, "Sending Abort - segmented message.\n"); #endif goto DCC_ABORT; - } + } /* decode the service request only */ len = dcc_decode_service_request(service_request, service_len, &timeDuration, &state, &password); @@ -121,7 +123,7 @@ void handler_device_communication_control(uint8_t * service_request, #endif } } -DCC_ABORT: + DCC_ABORT: pdu_len += len; bytes_sent = datalink_send_pdu(src, &npdu_data, &Handler_Transmit_Buffer[0], pdu_len); diff --git a/bacnet-stack/demo/handler/h_iam.c b/bacnet-stack/demo/handler/h_iam.c index 58737886..97b20ac9 100644 --- a/bacnet-stack/demo/handler/h_iam.c +++ b/bacnet-stack/demo/handler/h_iam.c @@ -32,8 +32,10 @@ #include "iam.h" #include "address.h" -void handler_i_am_add(uint8_t * service_request, - uint16_t service_len, BACNET_ADDRESS * src) +void handler_i_am_add( + uint8_t * service_request, + uint16_t service_len, + BACNET_ADDRESS * src) { int len = 0; uint32_t device_id = 0; @@ -61,8 +63,10 @@ void handler_i_am_add(uint8_t * service_request, return; } -void handler_i_am_bind(uint8_t * service_request, - uint16_t service_len, BACNET_ADDRESS * src) +void handler_i_am_bind( + uint8_t * service_request, + uint16_t service_len, + BACNET_ADDRESS * src) { int len = 0; uint32_t device_id = 0; diff --git a/bacnet-stack/demo/handler/h_ihave.c b/bacnet-stack/demo/handler/h_ihave.c index d1d39a71..758fc8da 100644 --- a/bacnet-stack/demo/handler/h_ihave.c +++ b/bacnet-stack/demo/handler/h_ihave.c @@ -32,16 +32,17 @@ #include "bactext.h" #include "ihave.h" -void handler_i_have(uint8_t * service_request, - uint16_t service_len, BACNET_ADDRESS * src) +void handler_i_have( + uint8_t * service_request, + uint16_t service_len, + BACNET_ADDRESS * src) { int len = 0; BACNET_I_HAVE_DATA data; (void) service_len; (void) src; - len = ihave_decode_service_request(service_request, - service_len, &data); + len = ihave_decode_service_request(service_request, service_len, &data); if (len != -1) { #if PRINT_ENABLED fprintf(stderr, "I-Have: %s %d from %s %u!\r\n", diff --git a/bacnet-stack/demo/handler/h_rd.c b/bacnet-stack/demo/handler/h_rd.c index 2f352dfe..754f54e6 100644 --- a/bacnet-stack/demo/handler/h_rd.c +++ b/bacnet-stack/demo/handler/h_rd.c @@ -41,9 +41,11 @@ static char *Password = "Jesus"; static BACNET_CHARACTER_STRING My_Password; -void handler_reinitialize_device(uint8_t * service_request, +void handler_reinitialize_device( + uint8_t * service_request, uint16_t service_len, - BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data) + BACNET_ADDRESS * src, + BACNET_CONFIRMED_SERVICE_DATA * service_data) { BACNET_REINITIALIZED_STATE state; BACNET_CHARACTER_STRING their_password; @@ -70,7 +72,7 @@ void handler_reinitialize_device(uint8_t * service_request, "ReinitializeDevice: Sending Abort - segmented message.\n"); #endif goto RD_ABORT; - } + } /* decode the service request only */ len = rd_decode_service_request(service_request, service_len, &state, &their_password); @@ -124,7 +126,7 @@ void handler_reinitialize_device(uint8_t * service_request, #endif } } -RD_ABORT: + RD_ABORT: pdu_len += len; bytes_sent = datalink_send_pdu(src, &npdu_data, &Handler_Transmit_Buffer[0], pdu_len); diff --git a/bacnet-stack/demo/handler/h_rp.c b/bacnet-stack/demo/handler/h_rp.c index 7e5d1295..f1df99c3 100644 --- a/bacnet-stack/demo/handler/h_rp.c +++ b/bacnet-stack/demo/handler/h_rp.c @@ -47,7 +47,7 @@ #include "lsp.h" #include "mso.h" #if defined(BACFILE) - #include "bacfile.h" +#include "bacfile.h" #endif static uint8_t Temp_Buf[MAX_APDU] = { 0 }; @@ -69,115 +69,82 @@ int Encode_Property_APDU( *error_class = ERROR_CLASS_OBJECT; *error_code = ERROR_CODE_UNKNOWN_OBJECT; /* handle each object type */ - switch(object_type) { + switch (object_type) { case OBJECT_DEVICE: if (Device_Valid_Object_Instance_Number(object_instance)) { - apdu_len = Device_Encode_Property_APDU( - &apdu[0], - property, - array_index, - error_class, error_code); + apdu_len = Device_Encode_Property_APDU(&apdu[0], + property, array_index, error_class, error_code); } break; case OBJECT_ANALOG_INPUT: if (Analog_Input_Valid_Instance(object_instance)) { - apdu_len = Analog_Input_Encode_Property_APDU( - &apdu[0], + apdu_len = Analog_Input_Encode_Property_APDU(&apdu[0], object_instance, - property, - array_index, - error_class, error_code); + property, array_index, error_class, error_code); } break; case OBJECT_ANALOG_OUTPUT: if (Analog_Output_Valid_Instance(object_instance)) { - apdu_len = Analog_Output_Encode_Property_APDU( - &apdu[0], + apdu_len = Analog_Output_Encode_Property_APDU(&apdu[0], object_instance, - property, - array_index, - error_class, error_code); + property, array_index, error_class, error_code); } break; case OBJECT_ANALOG_VALUE: if (Analog_Value_Valid_Instance(object_instance)) { - apdu_len = Analog_Value_Encode_Property_APDU( - &apdu[0], + apdu_len = Analog_Value_Encode_Property_APDU(&apdu[0], object_instance, - property, - array_index, - error_class, error_code); + property, array_index, error_class, error_code); } break; case OBJECT_BINARY_INPUT: if (Binary_Input_Valid_Instance(object_instance)) { - apdu_len = Binary_Input_Encode_Property_APDU( - &apdu[0], + apdu_len = Binary_Input_Encode_Property_APDU(&apdu[0], object_instance, - property, - array_index, - error_class, error_code); + property, array_index, error_class, error_code); } break; case OBJECT_BINARY_OUTPUT: if (Binary_Output_Valid_Instance(object_instance)) { - apdu_len = Binary_Output_Encode_Property_APDU( - &apdu[0], + apdu_len = Binary_Output_Encode_Property_APDU(&apdu[0], object_instance, - property, - array_index, - error_class, error_code); + property, array_index, error_class, error_code); } break; case OBJECT_BINARY_VALUE: if (Binary_Value_Valid_Instance(object_instance)) { - apdu_len = Binary_Value_Encode_Property_APDU( - &apdu[0], + apdu_len = Binary_Value_Encode_Property_APDU(&apdu[0], object_instance, - property, - array_index, - error_class, error_code); + property, array_index, error_class, error_code); } break; case OBJECT_LIFE_SAFETY_POINT: if (Life_Safety_Point_Valid_Instance(object_instance)) { - apdu_len = Life_Safety_Point_Encode_Property_APDU( - &apdu[0], + apdu_len = Life_Safety_Point_Encode_Property_APDU(&apdu[0], object_instance, - property, - array_index, - error_class, error_code); + property, array_index, error_class, error_code); } break; case OBJECT_LOAD_CONTROL: if (Load_Control_Valid_Instance(object_instance)) { - apdu_len = Load_Control_Encode_Property_APDU( - &apdu[0], + apdu_len = Load_Control_Encode_Property_APDU(&apdu[0], object_instance, - property, - array_index, - error_class, error_code); + property, array_index, error_class, error_code); } break; case OBJECT_MULTI_STATE_OUTPUT: if (Multistate_Output_Valid_Instance(object_instance)) { - apdu_len = Multistate_Output_Encode_Property_APDU( - &apdu[0], + apdu_len = Multistate_Output_Encode_Property_APDU(&apdu[0], object_instance, - property, - array_index, - error_class, error_code); + property, array_index, error_class, error_code); } break; #if defined(BACFILE) case OBJECT_FILE: if (bacfile_valid_instance(object_instance)) { - apdu_len = bacfile_encode_property_apdu( - &apdu[0], + apdu_len = bacfile_encode_property_apdu(&apdu[0], object_instance, - property, - array_index, - error_class, error_code); + property, array_index, error_class, error_code); } break; #endif @@ -190,9 +157,11 @@ int Encode_Property_APDU( return apdu_len; } -void handler_read_property(uint8_t * service_request, +void handler_read_property( + uint8_t * service_request, uint16_t service_len, - BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data) + BACNET_ADDRESS * src, + BACNET_CONFIRMED_SERVICE_DATA * service_data) { BACNET_READ_PROPERTY_DATA data; int len = 0; @@ -237,13 +206,10 @@ void handler_read_property(uint8_t * service_request, /* assume that there is an error */ error = true; - len = Encode_Property_APDU( - &Temp_Buf[0], + len = Encode_Property_APDU(&Temp_Buf[0], data.object_type, data.object_instance, - data.object_property, - data.array_index, - &error_class, &error_code); + data.object_property, data.array_index, &error_class, &error_code); if (len >= 0) { /* encode the APDU portion of the packet */ data.application_data = &Temp_Buf[0]; @@ -253,8 +219,7 @@ void handler_read_property(uint8_t * service_request, rp_ack_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, &data); #if PRINT_ENABLED - fprintf(stderr, - "RP: Sending Ack!\n"); + fprintf(stderr, "RP: Sending Ack!\n"); #endif error = false; } @@ -276,7 +241,7 @@ void handler_read_property(uint8_t * service_request, #endif } } -RP_ABORT: + RP_ABORT: pdu_len += len; bytes_sent = datalink_send_pdu(src, &npdu_data, &Handler_Transmit_Buffer[0], pdu_len); diff --git a/bacnet-stack/demo/handler/h_rp_a.c b/bacnet-stack/demo/handler/h_rp_a.c index 617229ef..b446df94 100644 --- a/bacnet-stack/demo/handler/h_rp_a.c +++ b/bacnet-stack/demo/handler/h_rp_a.c @@ -42,7 +42,8 @@ #include "txbuf.h" /* for debugging... */ -static void PrintReadPropertyData(BACNET_READ_PROPERTY_DATA * data) +static void PrintReadPropertyData( + BACNET_READ_PROPERTY_DATA * data) { BACNET_APPLICATION_DATA_VALUE value; /* for decode value data */ int len = 0; @@ -95,17 +96,18 @@ static void PrintReadPropertyData(BACNET_READ_PROPERTY_DATA * data) } } -void handler_read_property_ack(uint8_t * service_request, +void handler_read_property_ack( + uint8_t * service_request, uint16_t service_len, - BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_ACK_DATA * service_data) + BACNET_ADDRESS * src, + BACNET_CONFIRMED_SERVICE_ACK_DATA * service_data) { int len = 0; BACNET_READ_PROPERTY_DATA data; (void) src; (void) service_data; /* we could use these... */ - len = rp_ack_decode_service_request(service_request, - service_len, &data); + len = rp_ack_decode_service_request(service_request, service_len, &data); #if 0 fprintf(stderr, "Received Read-Property Ack!\n"); #endif diff --git a/bacnet-stack/demo/handler/h_rpm.c b/bacnet-stack/demo/handler/h_rpm.c index 1cae74be..a477c942 100644 --- a/bacnet-stack/demo/handler/h_rpm.c +++ b/bacnet-stack/demo/handler/h_rpm.c @@ -49,25 +49,24 @@ #include "lsp.h" #include "mso.h" #if defined(BACFILE) - #include "bacfile.h" +#include "bacfile.h" #endif static uint8_t Temp_Buf[MAX_APDU] = { 0 }; -struct property_list_t -{ +struct property_list_t { const int *pList; unsigned count; }; -struct special_property_list_t -{ +struct special_property_list_t { struct property_list_t Required; struct property_list_t Optional; struct property_list_t Proprietary; }; -static unsigned property_list_count(const int *pList) +static unsigned property_list_count( + const int *pList) { unsigned property_count = 0; @@ -90,72 +89,62 @@ static void RPM_Property_List( pPropertyList->Optional.pList = NULL; pPropertyList->Proprietary.pList = NULL; switch (object_type) { - case OBJECT_ANALOG_INPUT: - Analog_Input_Property_Lists( - &pPropertyList->Required.pList, - &pPropertyList->Optional.pList, - &pPropertyList->Proprietary.pList); - break; - case OBJECT_ANALOG_OUTPUT: - Analog_Output_Property_Lists( - &pPropertyList->Required.pList, - &pPropertyList->Optional.pList, - &pPropertyList->Proprietary.pList); - break; - case OBJECT_ANALOG_VALUE: - Analog_Value_Property_Lists( - &pPropertyList->Required.pList, - &pPropertyList->Optional.pList, - &pPropertyList->Proprietary.pList); - break; - case OBJECT_BINARY_INPUT: - Binary_Input_Property_Lists( - &pPropertyList->Required.pList, - &pPropertyList->Optional.pList, - &pPropertyList->Proprietary.pList); - break; - case OBJECT_BINARY_OUTPUT: - Binary_Output_Property_Lists( - &pPropertyList->Required.pList, - &pPropertyList->Optional.pList, - &pPropertyList->Proprietary.pList); - break; - case OBJECT_BINARY_VALUE: - Binary_Value_Property_Lists( - &pPropertyList->Required.pList, - &pPropertyList->Optional.pList, - &pPropertyList->Proprietary.pList); - break; - case OBJECT_LIFE_SAFETY_POINT: - Life_Safety_Point_Property_Lists( - &pPropertyList->Required.pList, - &pPropertyList->Optional.pList, - &pPropertyList->Proprietary.pList); - break; - case OBJECT_LOAD_CONTROL: - Load_Control_Property_Lists( - &pPropertyList->Required.pList, - &pPropertyList->Optional.pList, - &pPropertyList->Proprietary.pList); - break; - case OBJECT_MULTI_STATE_OUTPUT: - break; + case OBJECT_ANALOG_INPUT: + Analog_Input_Property_Lists(&pPropertyList->Required.pList, + &pPropertyList->Optional.pList, + &pPropertyList->Proprietary.pList); + break; + case OBJECT_ANALOG_OUTPUT: + Analog_Output_Property_Lists(&pPropertyList->Required.pList, + &pPropertyList->Optional.pList, + &pPropertyList->Proprietary.pList); + break; + case OBJECT_ANALOG_VALUE: + Analog_Value_Property_Lists(&pPropertyList->Required.pList, + &pPropertyList->Optional.pList, + &pPropertyList->Proprietary.pList); + break; + case OBJECT_BINARY_INPUT: + Binary_Input_Property_Lists(&pPropertyList->Required.pList, + &pPropertyList->Optional.pList, + &pPropertyList->Proprietary.pList); + break; + case OBJECT_BINARY_OUTPUT: + Binary_Output_Property_Lists(&pPropertyList->Required.pList, + &pPropertyList->Optional.pList, + &pPropertyList->Proprietary.pList); + break; + case OBJECT_BINARY_VALUE: + Binary_Value_Property_Lists(&pPropertyList->Required.pList, + &pPropertyList->Optional.pList, + &pPropertyList->Proprietary.pList); + break; + case OBJECT_LIFE_SAFETY_POINT: + Life_Safety_Point_Property_Lists(&pPropertyList->Required.pList, + &pPropertyList->Optional.pList, + &pPropertyList->Proprietary.pList); + break; + case OBJECT_LOAD_CONTROL: + Load_Control_Property_Lists(&pPropertyList->Required.pList, + &pPropertyList->Optional.pList, + &pPropertyList->Proprietary.pList); + break; + case OBJECT_MULTI_STATE_OUTPUT: + break; #if defined(BACFILE) - case OBJECT_FILE: - BACfile_Property_Lists( - &pPropertyList->Required.pList, - &pPropertyList->Optional.pList, - &pPropertyList->Proprietary.pList); - break; + case OBJECT_FILE: + BACfile_Property_Lists(&pPropertyList->Required.pList, + &pPropertyList->Optional.pList, + &pPropertyList->Proprietary.pList); + break; #endif - case OBJECT_DEVICE: - Device_Property_Lists( - &pPropertyList->Required.pList, - &pPropertyList->Optional.pList, - &pPropertyList->Proprietary.pList); - break; - default: - break; + case OBJECT_DEVICE: + Device_Property_Lists(&pPropertyList->Required.pList, + &pPropertyList->Optional.pList, + &pPropertyList->Proprietary.pList); + break; + default: + break; } /* fill the count */ pPropertyList->Required.count = @@ -173,7 +162,7 @@ static int RPM_Object_Property( BACNET_PROPERTY_ID special_property, unsigned index) { - int property = -1; /* return value */ + int property = -1; /* return value */ unsigned required, optional, proprietary; required = pPropertyList->Required.count; @@ -210,8 +199,7 @@ static unsigned RPM_Object_Property_Count( if (special_property == PROP_ALL) { count = pPropertyList->Required.count + - pPropertyList->Optional.count + - pPropertyList->Proprietary.count; + pPropertyList->Optional.count + pPropertyList->Proprietary.count; } else if (special_property == PROP_REQUIRED) { count = pPropertyList->Required.count; } else if (special_property == PROP_OPTIONAL) { @@ -222,14 +210,19 @@ static unsigned RPM_Object_Property_Count( } /* copy len bytes from src to offset of dest if there is enough space. */ -int apdu_copy(uint8_t *dest, uint8_t *src, int offset, int len, int max) +int apdu_copy( + uint8_t * dest, + uint8_t * src, + int offset, + int len, + int max) { int i; int copy_len = 0; - if (len <= (max-offset)) { + if (len <= (max - offset)) { for (i = 0; i < len; i++) { - dest[offset+i] = src[i]; + dest[offset + i] = src[i]; copy_len++; } } @@ -239,7 +232,8 @@ int apdu_copy(uint8_t *dest, uint8_t *src, int offset, int len, int max) /* Encode the RPM property returning the length of the encoding, or 0 if there is no room to fit the encoding. */ -int RPM_Encode_Property(uint8_t *apdu, +int RPM_Encode_Property( + uint8_t * apdu, uint16_t offset, uint16_t max_apdu, BACNET_OBJECT_TYPE object_type, @@ -252,35 +246,30 @@ int RPM_Encode_Property(uint8_t *apdu, BACNET_ERROR_CLASS error_class = ERROR_CLASS_OBJECT; BACNET_ERROR_CODE error_code = ERROR_CODE_UNKNOWN_OBJECT; - len = rpm_ack_encode_apdu_object_property( - &Temp_Buf[0], - object_property, - array_index); + len = rpm_ack_encode_apdu_object_property(&Temp_Buf[0], + object_property, array_index); len = apdu_copy(&apdu[0], &Temp_Buf[0], offset, len, max_apdu); if (!len) return 0; apdu_len += len; - len = Encode_Property_APDU( - &Temp_Buf[0], + len = Encode_Property_APDU(&Temp_Buf[0], object_type, object_instance, - object_property, - array_index, - &error_class, &error_code); + object_property, array_index, &error_class, &error_code); if (len < 0) { /* error was returned - encode that for the response */ - len = rpm_ack_encode_apdu_object_property_error( - &Temp_Buf[0], + len = rpm_ack_encode_apdu_object_property_error(&Temp_Buf[0], error_class, error_code); - len = apdu_copy(&apdu[0], &Temp_Buf[0], offset+apdu_len, len, max_apdu); + len = + apdu_copy(&apdu[0], &Temp_Buf[0], offset + apdu_len, len, + max_apdu); if (!len) return 0; - } else if ((offset+apdu_len+1+len+1) < max_apdu) { + } else if ((offset + apdu_len + 1 + len + 1) < max_apdu) { /* enough room to fit the property value and tags */ - len = rpm_ack_encode_apdu_object_property_value( - &apdu[offset+apdu_len], - &Temp_Buf[0], - len); + len = + rpm_ack_encode_apdu_object_property_value(&apdu[offset + apdu_len], + &Temp_Buf[0], len); } else { /* not enough room - abort! */ return 0; @@ -311,16 +300,14 @@ void handler_read_property_multiple( int32_t array_index = 0; /* jps_debug - see if we are utilizing all the buffer */ - /* memset(&Handler_Transmit_Buffer[0], 0xff, sizeof(Handler_Transmit_Buffer));*/ + /* memset(&Handler_Transmit_Buffer[0], 0xff, sizeof(Handler_Transmit_Buffer)); */ /* encode the NPDU portion of the packet */ datalink_get_my_address(&my_address); npdu_encode_npdu_data(&npdu_data, false, MESSAGE_PRIORITY_NORMAL); - npdu_len = npdu_encode_pdu( - &Handler_Transmit_Buffer[0], + npdu_len = npdu_encode_pdu(&Handler_Transmit_Buffer[0], src, &my_address, &npdu_data); if (service_data->segmented_message) { - apdu_len = abort_encode_apdu( - &Handler_Transmit_Buffer[npdu_len], + apdu_len = abort_encode_apdu(&Handler_Transmit_Buffer[npdu_len], service_data->invoke_id, ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true); #if PRINT_ENABLED @@ -330,102 +317,82 @@ void handler_read_property_multiple( } /* decode apdu request & encode apdu reply encode complex ack, invoke id, service choice */ - apdu_len = rpm_ack_encode_apdu_init( - &Handler_Transmit_Buffer[npdu_len], + apdu_len = rpm_ack_encode_apdu_init(&Handler_Transmit_Buffer[npdu_len], service_data->invoke_id); - do - { - len = rpm_decode_object_id( - &service_request[decode_len], - service_len - decode_len, - &object_type, &object_instance); + do { + len = rpm_decode_object_id(&service_request[decode_len], + service_len - decode_len, &object_type, &object_instance); /* end of object? */ if (len > 0) { decode_len += len; } else { - len = rpm_decode_object_end( - &service_request[decode_len], + len = rpm_decode_object_end(&service_request[decode_len], service_len - decode_len); if (len == 1) { decode_len++; len = rpm_ack_encode_apdu_object_end(&Temp_Buf[0]); - copy_len = apdu_copy( - &Handler_Transmit_Buffer[npdu_len], &Temp_Buf[0], - apdu_len, len, - sizeof(Handler_Transmit_Buffer)); + copy_len = + apdu_copy(&Handler_Transmit_Buffer[npdu_len], &Temp_Buf[0], + apdu_len, len, sizeof(Handler_Transmit_Buffer)); if (!copy_len) { - apdu_len = abort_encode_apdu( - &Handler_Transmit_Buffer[npdu_len], + apdu_len = + abort_encode_apdu(&Handler_Transmit_Buffer[npdu_len], service_data->invoke_id, - ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, - true); + ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true); goto RPM_ABORT; } else { apdu_len += copy_len; } } else { - apdu_len = abort_encode_apdu( - &Handler_Transmit_Buffer[npdu_len], - service_data->invoke_id, - ABORT_REASON_OTHER, true); + apdu_len = + abort_encode_apdu(&Handler_Transmit_Buffer[npdu_len], + service_data->invoke_id, ABORT_REASON_OTHER, true); goto RPM_ABORT; } break; } - len = rpm_ack_encode_apdu_object_begin( - &Temp_Buf[0], + len = rpm_ack_encode_apdu_object_begin(&Temp_Buf[0], object_type, object_instance); - copy_len = apdu_copy( - &Handler_Transmit_Buffer[npdu_len], &Temp_Buf[0], - apdu_len, len, - sizeof(Handler_Transmit_Buffer)); + copy_len = apdu_copy(&Handler_Transmit_Buffer[npdu_len], &Temp_Buf[0], + apdu_len, len, sizeof(Handler_Transmit_Buffer)); if (!copy_len) { - apdu_len = abort_encode_apdu( - &Handler_Transmit_Buffer[npdu_len], + apdu_len = abort_encode_apdu(&Handler_Transmit_Buffer[npdu_len], service_data->invoke_id, - ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, - true); + ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true); goto RPM_ABORT; } else { apdu_len += copy_len; } /* do each property of this object of the RPM request */ - do - { - len = rpm_decode_object_property( - &service_request[decode_len], - service_len - decode_len, - &object_property, - &array_index); + do { + len = rpm_decode_object_property(&service_request[decode_len], + service_len - decode_len, &object_property, &array_index); /* end of property list? */ if (len > 0) { decode_len += len; } else { - len = rpm_decode_object_end( - &service_request[decode_len], + len = rpm_decode_object_end(&service_request[decode_len], service_len - decode_len); if (len == 1) { decode_len++; len = rpm_ack_encode_apdu_object_end(&Temp_Buf[0]); - copy_len = apdu_copy( - &Handler_Transmit_Buffer[npdu_len], &Temp_Buf[0], - apdu_len, len, + copy_len = + apdu_copy(&Handler_Transmit_Buffer[npdu_len], + &Temp_Buf[0], apdu_len, len, sizeof(Handler_Transmit_Buffer)); if (!copy_len) { - apdu_len = abort_encode_apdu( - &Handler_Transmit_Buffer[npdu_len], - service_data->invoke_id, - ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, - true); + apdu_len = + abort_encode_apdu(&Handler_Transmit_Buffer + [npdu_len], service_data->invoke_id, + ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true); goto RPM_ABORT; } else { apdu_len += copy_len; } } else { - apdu_len = abort_encode_apdu( - &Handler_Transmit_Buffer[npdu_len], - service_data->invoke_id, - ABORT_REASON_OTHER, true); + apdu_len = + abort_encode_apdu(&Handler_Transmit_Buffer[npdu_len], + service_data->invoke_id, ABORT_REASON_OTHER, true); goto RPM_ABORT; } /* stop decoding properties */ @@ -434,8 +401,7 @@ void handler_read_property_multiple( /* handle the special properties */ if ((object_property == PROP_ALL) || (object_property == PROP_REQUIRED) || - (object_property == PROP_OPTIONAL)) - { + (object_property == PROP_OPTIONAL)) { struct special_property_list_t property_list; unsigned property_count = 0; unsigned index = 0; @@ -443,63 +409,50 @@ void handler_read_property_multiple( special_object_property = object_property; RPM_Property_List(object_type, &property_list); - property_count = RPM_Object_Property_Count( - &property_list, + property_count = RPM_Object_Property_Count(&property_list, special_object_property); - for (index = 0; index < property_count; index++) - { - object_property = RPM_Object_Property( - &property_list, - special_object_property, - index); - len = RPM_Encode_Property( - &Handler_Transmit_Buffer[0], + for (index = 0; index < property_count; index++) { + object_property = RPM_Object_Property(&property_list, + special_object_property, index); + len = RPM_Encode_Property(&Handler_Transmit_Buffer[0], npdu_len + apdu_len, sizeof(Handler_Transmit_Buffer), object_type, - object_instance, - object_property, - array_index); + object_instance, object_property, array_index); if (len > 0) { apdu_len += len; } else { - apdu_len = abort_encode_apdu( - &Handler_Transmit_Buffer[npdu_len], - service_data->invoke_id, - ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true); - goto RPM_ABORT; + apdu_len = + abort_encode_apdu(&Handler_Transmit_Buffer + [npdu_len], service_data->invoke_id, + ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true); + goto RPM_ABORT; } } } else { /* handle an individual property */ - len = RPM_Encode_Property( - &Handler_Transmit_Buffer[0], + len = RPM_Encode_Property(&Handler_Transmit_Buffer[0], npdu_len + apdu_len, sizeof(Handler_Transmit_Buffer), object_type, - object_instance, - object_property, - array_index); + object_instance, object_property, array_index); if (len > 0) { apdu_len += len; } else { - apdu_len = abort_encode_apdu( - &Handler_Transmit_Buffer[npdu_len], - service_data->invoke_id, - ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true); - goto RPM_ABORT; + apdu_len = + abort_encode_apdu(&Handler_Transmit_Buffer[npdu_len], + service_data->invoke_id, + ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true); + goto RPM_ABORT; } } - } while(1); + } while (1); if (decode_len >= service_len) { break; } - } while(1); -RPM_ABORT: + } while (1); + RPM_ABORT: pdu_len = apdu_len + npdu_len; - bytes_sent = datalink_send_pdu( - src, - &npdu_data, - &Handler_Transmit_Buffer[0], - pdu_len); + bytes_sent = datalink_send_pdu(src, + &npdu_data, &Handler_Transmit_Buffer[0], pdu_len); } diff --git a/bacnet-stack/demo/handler/h_ts.c b/bacnet-stack/demo/handler/h_ts.c index b8dab7de..3f24a270 100644 --- a/bacnet-stack/demo/handler/h_ts.c +++ b/bacnet-stack/demo/handler/h_ts.c @@ -32,7 +32,9 @@ #include "timesync.h" #if PRINT_ENABLED -static void show_bacnet_date_time(BACNET_DATE * bdate, BACNET_TIME * btime) +static void show_bacnet_date_time( + BACNET_DATE * bdate, + BACNET_TIME * btime) { /* show the date received */ fprintf(stderr, "%u", (unsigned) bdate->year); @@ -47,8 +49,10 @@ static void show_bacnet_date_time(BACNET_DATE * bdate, BACNET_TIME * btime) } #endif -void handler_timesync(uint8_t * service_request, - uint16_t service_len, BACNET_ADDRESS * src) +void handler_timesync( + uint8_t * service_request, + uint16_t service_len, + BACNET_ADDRESS * src) { int len = 0; BACNET_DATE bdate; @@ -67,8 +71,10 @@ void handler_timesync(uint8_t * service_request, return; } -void handler_timesync_utc(uint8_t * service_request, - uint16_t service_len, BACNET_ADDRESS * src) +void handler_timesync_utc( + uint8_t * service_request, + uint16_t service_len, + BACNET_ADDRESS * src) { int len = 0; BACNET_DATE bdate; @@ -86,4 +92,3 @@ void handler_timesync_utc(uint8_t * service_request, return; } - diff --git a/bacnet-stack/demo/handler/h_whohas.c b/bacnet-stack/demo/handler/h_whohas.c index 0352aa0c..c2b6ef9a 100644 --- a/bacnet-stack/demo/handler/h_whohas.c +++ b/bacnet-stack/demo/handler/h_whohas.c @@ -35,8 +35,10 @@ #include "device.h" #include "client.h" -void handler_who_has(uint8_t * service_request, - uint16_t service_len, BACNET_ADDRESS * src) +void handler_who_has( + uint8_t * service_request, + uint16_t service_len, + BACNET_ADDRESS * src) { int len = 0; BACNET_WHO_HAS_DATA data; @@ -47,15 +49,12 @@ void handler_who_has(uint8_t * service_request, bool found = false; (void) src; - len = whohas_decode_service_request(service_request, - service_len, &data); + 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; - else if ((Device_Object_Instance_Number() >= - (uint32_t) data.low_limit) - && (Device_Object_Instance_Number() <= - (uint32_t) data.high_limit)) + else if ((Device_Object_Instance_Number() >= (uint32_t) data.low_limit) + && (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. diff --git a/bacnet-stack/demo/handler/h_whois.c b/bacnet-stack/demo/handler/h_whois.c index 28ff41d7..5c7c8532 100644 --- a/bacnet-stack/demo/handler/h_whois.c +++ b/bacnet-stack/demo/handler/h_whois.c @@ -37,8 +37,10 @@ #include "client.h" #include "txbuf.h" -void handler_who_is(uint8_t * service_request, - uint16_t service_len, BACNET_ADDRESS * src) +void handler_who_is( + uint8_t * service_request, + uint16_t service_len, + BACNET_ADDRESS * src) { int len = 0; int32_t low_limit = 0; diff --git a/bacnet-stack/demo/handler/h_wp.c b/bacnet-stack/demo/handler/h_wp.c index 348093f0..5bda2ceb 100644 --- a/bacnet-stack/demo/handler/h_wp.c +++ b/bacnet-stack/demo/handler/h_wp.c @@ -47,12 +47,14 @@ #include "lsp.h" #include "mso.h" #if defined(BACFILE) - #include "bacfile.h" +#include "bacfile.h" #endif -void handler_write_property(uint8_t * service_request, +void handler_write_property( + uint8_t * service_request, uint16_t service_len, - BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data) + BACNET_ADDRESS * src, + BACNET_CONFIRMED_SERVICE_DATA * service_data) { BACNET_WRITE_PROPERTY_DATA wp_data; int len = 0; @@ -79,9 +81,8 @@ void handler_write_property(uint8_t * service_request, fprintf(stderr, "WP: Segmented message. Sending Abort!\n"); #endif goto WP_ABORT; - } /* decode the service request only */ - len = wp_decode_service_request(service_request, - service_len, &wp_data); + } /* decode the service request only */ + len = wp_decode_service_request(service_request, service_len, &wp_data); #if PRINT_ENABLED if (len > 0) fprintf(stderr, "WP: type=%u instance=%u property=%u index=%d\n", @@ -99,232 +100,204 @@ void handler_write_property(uint8_t * service_request, fprintf(stderr, "WP: Bad Encoding. Sending Abort!\n"); #endif goto WP_ABORT; - } - switch (wp_data.object_type) { - case OBJECT_DEVICE: - if (Device_Write_Property(&wp_data, &error_class, &error_code)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY); -#if PRINT_ENABLED - fprintf(stderr, - "WP: Sending Simple Ack for Device!\n"); -#endif - } else { - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, - error_code); -#if PRINT_ENABLED - fprintf(stderr, - "WP: Sending Error for Device!\n"); -#endif - } - break; - case OBJECT_ANALOG_INPUT: - case OBJECT_BINARY_INPUT: - error_class = ERROR_CLASS_PROPERTY; - error_code = ERROR_CODE_WRITE_ACCESS_DENIED; - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, - error_class, error_code); -#if PRINT_ENABLED - fprintf(stderr, "WP: Sending Write Access Error!\n"); -#endif - break; - case OBJECT_BINARY_OUTPUT: - if (Binary_Output_Write_Property(&wp_data, &error_class, - &error_code)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY); -#if PRINT_ENABLED - fprintf(stderr, - "WP: Sending Simple Ack for BO!\n"); -#endif - } else { - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, - error_code); -#if PRINT_ENABLED - fprintf(stderr, "WP: Sending Write Access Error for BO!\n"); -#endif - } - break; - case OBJECT_BINARY_VALUE: - if (Binary_Value_Write_Property(&wp_data, &error_class, - &error_code)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY); -#if PRINT_ENABLED - fprintf(stderr, - "WP: Sending Simple Ack for BV!\n"); -#endif - } else { - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, - error_code); -#if PRINT_ENABLED - fprintf(stderr, "WP: Sending Write Access Error for BV!\n"); -#endif - } - break; - case OBJECT_ANALOG_OUTPUT: - if (Analog_Output_Write_Property(&wp_data, &error_class, - &error_code)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY); -#if PRINT_ENABLED - fprintf(stderr, - "WP: Sending Simple Ack for AO!\n"); -#endif - } else { - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, - error_code); -#if PRINT_ENABLED - fprintf(stderr, "WP: Sending Write Access Error for AO!\n"); -#endif - } - break; - case OBJECT_ANALOG_VALUE: - if (Analog_Value_Write_Property(&wp_data, &error_class, - &error_code)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY); -#if PRINT_ENABLED - fprintf(stderr, - "WP: Sending Simple Ack for AV!\n"); -#endif - } else { - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, - error_code); -#if PRINT_ENABLED - fprintf(stderr, "WP: Sending Write Access Error for AV!\n"); -#endif - } - break; - case OBJECT_LIFE_SAFETY_POINT: - if (Life_Safety_Point_Write_Property(&wp_data, &error_class, - &error_code)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY); -#if PRINT_ENABLED - fprintf(stderr, - "WP: Sending Simple Ack for LSP!\n"); -#endif - } else { - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, - error_code); -#if PRINT_ENABLED - fprintf(stderr, "WP: Sending Write Access Error for LSP!\n"); -#endif - } - break; - case OBJECT_LOAD_CONTROL: - if (Load_Control_Write_Property(&wp_data, &error_class, - &error_code)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY); -#if PRINT_ENABLED - fprintf(stderr, - "WP: Sending Simple Ack for Load Control!\n"); -#endif - } else { - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, - error_code); -#if PRINT_ENABLED - fprintf(stderr, - "WP: Sending Write Access Error for Load Control!\n"); -#endif - } - break; - case OBJECT_MULTI_STATE_OUTPUT: - if (Multistate_Output_Write_Property(&wp_data, &error_class, - &error_code)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY); -#if PRINT_ENABLED - fprintf(stderr, - "WP: Sending Write Property Simple Ack for MSO!\n"); -#endif - } else { - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, - error_code); -#if PRINT_ENABLED - fprintf(stderr, "WP: Sending Write Access Error for MSO!\n"); -#endif - } - break; -#if defined(BACFILE) - case OBJECT_FILE: - if (bacfile_write_property(&wp_data, &error_class, - &error_code)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY); -#if PRINT_ENABLED - fprintf(stderr, - "WP: Sending Simple Ack for File!\n"); -#endif - } else { - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, - error_code); -#if PRINT_ENABLED - fprintf(stderr, "WP: Sending Write Access Error for File!\n"); -#endif - } - break; -#endif /* BACFILE */ - default: - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, - error_class, error_code); -#if PRINT_ENABLED - fprintf(stderr, "WP: Sending Unknown Object Error!\n"); -#endif - break; } -WP_ABORT: + switch (wp_data.object_type) { + case OBJECT_DEVICE: + if (Device_Write_Property(&wp_data, &error_class, &error_code)) { + len = + encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY); +#if PRINT_ENABLED + fprintf(stderr, "WP: Sending Simple Ack for Device!\n"); +#endif + } else { + len = + bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, error_code); +#if PRINT_ENABLED + fprintf(stderr, "WP: Sending Error for Device!\n"); +#endif + } + break; + case OBJECT_ANALOG_INPUT: + case OBJECT_BINARY_INPUT: + error_class = ERROR_CLASS_PROPERTY; + error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + len = + bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, + error_class, error_code); +#if PRINT_ENABLED + fprintf(stderr, "WP: Sending Write Access Error!\n"); +#endif + break; + case OBJECT_BINARY_OUTPUT: + if (Binary_Output_Write_Property(&wp_data, &error_class, + &error_code)) { + len = + encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY); +#if PRINT_ENABLED + fprintf(stderr, "WP: Sending Simple Ack for BO!\n"); +#endif + } else { + len = + bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, error_code); +#if PRINT_ENABLED + fprintf(stderr, "WP: Sending Write Access Error for BO!\n"); +#endif + } + break; + case OBJECT_BINARY_VALUE: + if (Binary_Value_Write_Property(&wp_data, &error_class, + &error_code)) { + len = + encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY); +#if PRINT_ENABLED + fprintf(stderr, "WP: Sending Simple Ack for BV!\n"); +#endif + } else { + len = + bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, error_code); +#if PRINT_ENABLED + fprintf(stderr, "WP: Sending Write Access Error for BV!\n"); +#endif + } + break; + case OBJECT_ANALOG_OUTPUT: + if (Analog_Output_Write_Property(&wp_data, &error_class, + &error_code)) { + len = + encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY); +#if PRINT_ENABLED + fprintf(stderr, "WP: Sending Simple Ack for AO!\n"); +#endif + } else { + len = + bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, error_code); +#if PRINT_ENABLED + fprintf(stderr, "WP: Sending Write Access Error for AO!\n"); +#endif + } + break; + case OBJECT_ANALOG_VALUE: + if (Analog_Value_Write_Property(&wp_data, &error_class, + &error_code)) { + len = + encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY); +#if PRINT_ENABLED + fprintf(stderr, "WP: Sending Simple Ack for AV!\n"); +#endif + } else { + len = + bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, error_code); +#if PRINT_ENABLED + fprintf(stderr, "WP: Sending Write Access Error for AV!\n"); +#endif + } + break; + case OBJECT_LIFE_SAFETY_POINT: + if (Life_Safety_Point_Write_Property(&wp_data, &error_class, + &error_code)) { + len = + encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY); +#if PRINT_ENABLED + fprintf(stderr, "WP: Sending Simple Ack for LSP!\n"); +#endif + } else { + len = + bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, error_code); +#if PRINT_ENABLED + fprintf(stderr, "WP: Sending Write Access Error for LSP!\n"); +#endif + } + break; + case OBJECT_LOAD_CONTROL: + if (Load_Control_Write_Property(&wp_data, &error_class, + &error_code)) { + len = + encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY); +#if PRINT_ENABLED + fprintf(stderr, "WP: Sending Simple Ack for Load Control!\n"); +#endif + } else { + len = + bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, error_code); +#if PRINT_ENABLED + fprintf(stderr, + "WP: Sending Write Access Error for Load Control!\n"); +#endif + } + break; + case OBJECT_MULTI_STATE_OUTPUT: + if (Multistate_Output_Write_Property(&wp_data, &error_class, + &error_code)) { + len = + encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY); +#if PRINT_ENABLED + fprintf(stderr, + "WP: Sending Write Property Simple Ack for MSO!\n"); +#endif + } else { + len = + bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, error_code); +#if PRINT_ENABLED + fprintf(stderr, "WP: Sending Write Access Error for MSO!\n"); +#endif + } + break; +#if defined(BACFILE) + case OBJECT_FILE: + if (bacfile_write_property(&wp_data, &error_class, &error_code)) { + len = + encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY); +#if PRINT_ENABLED + fprintf(stderr, "WP: Sending Simple Ack for File!\n"); +#endif + } else { + len = + bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, error_code); +#if PRINT_ENABLED + fprintf(stderr, "WP: Sending Write Access Error for File!\n"); +#endif + } + break; +#endif /* BACFILE */ + default: + len = + bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, + error_class, error_code); +#if PRINT_ENABLED + fprintf(stderr, "WP: Sending Unknown Object Error!\n"); +#endif + break; + } + WP_ABORT: pdu_len += len; bytes_sent = datalink_send_pdu(src, &npdu_data, &Handler_Transmit_Buffer[0], pdu_len); diff --git a/bacnet-stack/demo/handler/noserv.c b/bacnet-stack/demo/handler/noserv.c index 6de3d71b..07b3cf1e 100644 --- a/bacnet-stack/demo/handler/noserv.c +++ b/bacnet-stack/demo/handler/noserv.c @@ -34,9 +34,11 @@ #include "npdu.h" #include "reject.h" -void handler_unrecognized_service(uint8_t * service_request, +void handler_unrecognized_service( + uint8_t * service_request, uint16_t service_len, - BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data) + BACNET_ADDRESS * src, + BACNET_CONFIRMED_SERVICE_DATA * service_data) { int len = 0; int pdu_len = 0; diff --git a/bacnet-stack/demo/handler/s_arfs.c b/bacnet-stack/demo/handler/s_arfs.c index 4e4f117d..58440b9f 100644 --- a/bacnet-stack/demo/handler/s_arfs.c +++ b/bacnet-stack/demo/handler/s_arfs.c @@ -42,9 +42,11 @@ #include "handlers.h" #include "txbuf.h" -uint8_t Send_Atomic_Read_File_Stream(uint32_t device_id, +uint8_t Send_Atomic_Read_File_Stream( + uint32_t device_id, uint32_t file_instance, - int fileStartPosition, unsigned requestedOctetCount) + int fileStartPosition, + unsigned requestedOctetCount) { BACNET_ADDRESS dest; BACNET_ADDRESS my_address; diff --git a/bacnet-stack/demo/handler/s_awfs.c b/bacnet-stack/demo/handler/s_awfs.c index f116c34a..1f9fe82a 100644 --- a/bacnet-stack/demo/handler/s_awfs.c +++ b/bacnet-stack/demo/handler/s_awfs.c @@ -42,9 +42,11 @@ #include "handlers.h" #include "txbuf.h" -uint8_t Send_Atomic_Write_File_Stream(uint32_t device_id, +uint8_t Send_Atomic_Write_File_Stream( + uint32_t device_id, uint32_t file_instance, - int fileStartPosition, BACNET_OCTET_STRING * fileData) + int fileStartPosition, + BACNET_OCTET_STRING * fileData) { BACNET_ADDRESS dest; BACNET_ADDRESS my_address; @@ -76,8 +78,7 @@ uint8_t Send_Atomic_Write_File_Stream(uint32_t device_id, if (status) { /* encode the NPDU portion of the packet */ datalink_get_my_address(&my_address); - npdu_encode_npdu_data(&npdu_data, true, - MESSAGE_PRIORITY_NORMAL); + npdu_encode_npdu_data(&npdu_data, true, MESSAGE_PRIORITY_NORMAL); pdu_len = npdu_encode_pdu(&Handler_Transmit_Buffer[0], &dest, &my_address, &npdu_data); @@ -116,8 +117,7 @@ uint8_t Send_Atomic_Write_File_Stream(uint32_t device_id, invoke_id = 0; #if PRINT_ENABLED fprintf(stderr, "Failed to Send AtomicWriteFile Request " - "(payload [%d] exceeds octet string capacity)!\n", - pdu_len); + "(payload [%d] exceeds octet string capacity)!\n", pdu_len); #endif } } diff --git a/bacnet-stack/demo/handler/s_dcc.c b/bacnet-stack/demo/handler/s_dcc.c index 1efebe75..e5928be5 100644 --- a/bacnet-stack/demo/handler/s_dcc.c +++ b/bacnet-stack/demo/handler/s_dcc.c @@ -42,9 +42,12 @@ #include "handlers.h" #include "txbuf.h" -uint8_t Send_Device_Communication_Control_Request(uint32_t device_id, uint16_t timeDuration, /* 0=optional */ - BACNET_COMMUNICATION_ENABLE_DISABLE state, char *password) -{ /* NULL=optional */ +uint8_t Send_Device_Communication_Control_Request( + uint32_t device_id, + uint16_t timeDuration, /* 0=optional */ + BACNET_COMMUNICATION_ENABLE_DISABLE state, + char *password) +{ /* NULL=optional */ BACNET_ADDRESS dest; BACNET_ADDRESS my_address; unsigned max_apdu = 0; diff --git a/bacnet-stack/demo/handler/s_ihave.c b/bacnet-stack/demo/handler/s_ihave.c index a50fb726..00685141 100644 --- a/bacnet-stack/demo/handler/s_ihave.c +++ b/bacnet-stack/demo/handler/s_ihave.c @@ -44,9 +44,11 @@ #include "txbuf.h" /* find a specific device, or use -1 for limit if you want unlimited */ -void Send_I_Have(uint32_t device_id, +void Send_I_Have( + uint32_t device_id, BACNET_OBJECT_TYPE object_type, - uint32_t object_instance, char *object_name) + uint32_t object_instance, + char *object_name) { int len = 0; int pdu_len = 0; diff --git a/bacnet-stack/demo/handler/s_rd.c b/bacnet-stack/demo/handler/s_rd.c index f72fe3bf..890226f4 100644 --- a/bacnet-stack/demo/handler/s_rd.c +++ b/bacnet-stack/demo/handler/s_rd.c @@ -42,8 +42,10 @@ #include "handlers.h" #include "txbuf.h" -uint8_t Send_Reinitialize_Device_Request(uint32_t device_id, - BACNET_REINITIALIZED_STATE state, char *password) +uint8_t Send_Reinitialize_Device_Request( + uint32_t device_id, + BACNET_REINITIALIZED_STATE state, + char *password) { BACNET_ADDRESS dest; BACNET_ADDRESS my_address; diff --git a/bacnet-stack/demo/handler/s_rp.c b/bacnet-stack/demo/handler/s_rp.c index 3a5c1f2f..e312ef57 100644 --- a/bacnet-stack/demo/handler/s_rp.c +++ b/bacnet-stack/demo/handler/s_rp.c @@ -44,10 +44,12 @@ #include "txbuf.h" /* returns invoke id of 0 if device is not bound or no tsm available */ -uint8_t Send_Read_Property_Request(uint32_t device_id, /* destination device */ +uint8_t Send_Read_Property_Request( + uint32_t device_id, /* destination device */ BACNET_OBJECT_TYPE object_type, uint32_t object_instance, - BACNET_PROPERTY_ID object_property, int32_t array_index) + BACNET_PROPERTY_ID object_property, + int32_t array_index) { BACNET_ADDRESS dest; BACNET_ADDRESS my_address; @@ -89,8 +91,7 @@ uint8_t Send_Read_Property_Request(uint32_t device_id, /* destination device */ max_apdu in the address binding table. */ if ((unsigned) pdu_len < max_apdu) { tsm_set_confirmed_unsegmented_transaction(invoke_id, &dest, - &npdu_data, &Handler_Transmit_Buffer[0], - (uint16_t) pdu_len); + &npdu_data, &Handler_Transmit_Buffer[0], (uint16_t) pdu_len); bytes_sent = datalink_send_pdu(&dest, &npdu_data, &Handler_Transmit_Buffer[0], pdu_len); diff --git a/bacnet-stack/demo/handler/s_ts.c b/bacnet-stack/demo/handler/s_ts.c index 98130261..54ec5e54 100644 --- a/bacnet-stack/demo/handler/s_ts.c +++ b/bacnet-stack/demo/handler/s_ts.c @@ -42,7 +42,9 @@ #include "handlers.h" #include "txbuf.h" -void Send_TimeSync(BACNET_DATE * bdate, BACNET_TIME * btime) +void Send_TimeSync( + BACNET_DATE * bdate, + BACNET_TIME * btime) { int len = 0; int pdu_len = 0; @@ -74,7 +76,9 @@ void Send_TimeSync(BACNET_DATE * bdate, BACNET_TIME * btime) #endif } -void Send_TimeSyncUTC(BACNET_DATE * bdate, BACNET_TIME * btime) +void Send_TimeSyncUTC( + BACNET_DATE * bdate, + BACNET_TIME * btime) { int pdu_len = 0; BACNET_ADDRESS dest; diff --git a/bacnet-stack/demo/handler/s_whohas.c b/bacnet-stack/demo/handler/s_whohas.c index 5ff4fef8..9a960cfa 100644 --- a/bacnet-stack/demo/handler/s_whohas.c +++ b/bacnet-stack/demo/handler/s_whohas.c @@ -43,8 +43,10 @@ #include "txbuf.h" /* find a specific device, or use -1 for limit if you want unlimited */ -void Send_WhoHas_Name(int32_t low_limit, - int32_t high_limit, char *object_name) +void Send_WhoHas_Name( + int32_t low_limit, + int32_t high_limit, + char *object_name) { int len = 0; int pdu_len = 0; @@ -80,9 +82,11 @@ void Send_WhoHas_Name(int32_t low_limit, } /* find a specific device, or use -1 for limit if you want unlimited */ -void Send_WhoHas_Object(int32_t low_limit, +void Send_WhoHas_Object( + int32_t low_limit, int32_t high_limit, - BACNET_OBJECT_TYPE object_type, uint32_t object_instance) + BACNET_OBJECT_TYPE object_type, + uint32_t object_instance) { int len = 0; int pdu_len = 0; diff --git a/bacnet-stack/demo/handler/s_whois.c b/bacnet-stack/demo/handler/s_whois.c index e67e9a30..8b0107d7 100644 --- a/bacnet-stack/demo/handler/s_whois.c +++ b/bacnet-stack/demo/handler/s_whois.c @@ -44,7 +44,9 @@ #include "txbuf.h" /* find a specific device, or use -1 for limit if you want unlimited */ -void Send_WhoIs(int32_t low_limit, int32_t high_limit) +void Send_WhoIs( + int32_t low_limit, + int32_t high_limit) { int len = 0; int pdu_len = 0; diff --git a/bacnet-stack/demo/handler/s_wp.c b/bacnet-stack/demo/handler/s_wp.c index 9d7e7ec8..7d4e4aab 100644 --- a/bacnet-stack/demo/handler/s_wp.c +++ b/bacnet-stack/demo/handler/s_wp.c @@ -44,12 +44,15 @@ #include "txbuf.h" /* returns the invoke ID for confirmed request, or zero on failure */ -uint8_t Send_Write_Property_Request_Data(uint32_t device_id, +uint8_t Send_Write_Property_Request_Data( + uint32_t device_id, BACNET_OBJECT_TYPE object_type, uint32_t object_instance, BACNET_PROPERTY_ID object_property, uint8_t * application_data, - int application_data_len, uint8_t priority, int32_t array_index) + int application_data_len, + uint8_t priority, + int32_t array_index) { BACNET_ADDRESS dest; BACNET_ADDRESS my_address; @@ -95,8 +98,7 @@ uint8_t Send_Write_Property_Request_Data(uint32_t device_id, max_apdu in the address binding table. */ if ((unsigned) pdu_len < max_apdu) { tsm_set_confirmed_unsegmented_transaction(invoke_id, &dest, - &npdu_data, &Handler_Transmit_Buffer[0], - (uint16_t) pdu_len); + &npdu_data, &Handler_Transmit_Buffer[0], (uint16_t) pdu_len); bytes_sent = datalink_send_pdu(&dest, &npdu_data, &Handler_Transmit_Buffer[0], pdu_len); @@ -119,12 +121,14 @@ uint8_t Send_Write_Property_Request_Data(uint32_t device_id, return invoke_id; } -uint8_t Send_Write_Property_Request(uint32_t device_id, +uint8_t Send_Write_Property_Request( + uint32_t device_id, BACNET_OBJECT_TYPE object_type, uint32_t object_instance, BACNET_PROPERTY_ID object_property, BACNET_APPLICATION_DATA_VALUE * object_value, - uint8_t priority, int32_t array_index) + uint8_t priority, + int32_t array_index) { uint8_t application_data[MAX_APDU] = { 0 }; int apdu_len = 0, len = 0; @@ -133,11 +137,11 @@ uint8_t Send_Write_Property_Request(uint32_t device_id, #if PRINT_ENABLED_DEBUG fprintf(stderr, "WriteProperty service: " "%s tag=%d\n", - (object_value->context_specific?"context":"application"), - (int)(object_value->context_specific?object_value->context_tag:object_value->tag) ); + (object_value->context_specific ? "context" : "application"), + (int) (object_value->context_specific ? object_value-> + context_tag : object_value->tag)); #endif - len = bacapp_encode_data(&application_data[apdu_len], - object_value); + len = bacapp_encode_data(&application_data[apdu_len], object_value); if ((len + apdu_len) < MAX_APDU) { apdu_len += len; } else { diff --git a/bacnet-stack/demo/object/ai.c b/bacnet-stack/demo/object/ai.c index b216c44c..fa21eb85 100644 --- a/bacnet-stack/demo/object/ai.c +++ b/bacnet-stack/demo/object/ai.c @@ -31,13 +31,12 @@ #include "bacdef.h" #include "bacdcode.h" #include "bacenum.h" -#include "config.h" /* the custom stuff */ +#include "config.h" /* the custom stuff */ #define MAX_ANALOG_INPUTS 7 /* These three arrays are used by the ReadPropertyMultiple handler */ -static const int Analog_Input_Properties_Required[] = -{ +static const int Analog_Input_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, PROP_OBJECT_NAME, PROP_OBJECT_TYPE, @@ -49,14 +48,12 @@ static const int Analog_Input_Properties_Required[] = -1 }; -static const int Analog_Input_Properties_Optional[] = -{ +static const int Analog_Input_Properties_Optional[] = { PROP_DESCRIPTION, -1 }; -static const int Analog_Input_Properties_Proprietary[] = -{ +static const int Analog_Input_Properties_Proprietary[] = { 9997, 9998, 9999, @@ -81,7 +78,8 @@ void Analog_Input_Property_Lists( /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need validate that the */ /* given instance exists */ -bool Analog_Input_Valid_Instance(uint32_t object_instance) +bool Analog_Input_Valid_Instance( + uint32_t object_instance) { if (object_instance < MAX_ANALOG_INPUTS) return true; @@ -91,7 +89,8 @@ bool Analog_Input_Valid_Instance(uint32_t object_instance) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then count how many you have */ -unsigned Analog_Input_Count(void) +unsigned Analog_Input_Count( + void) { return MAX_ANALOG_INPUTS; } @@ -99,12 +98,14 @@ unsigned Analog_Input_Count(void) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need to return the instance */ /* that correlates to the correct index */ -uint32_t Analog_Input_Index_To_Instance(unsigned index) +uint32_t Analog_Input_Index_To_Instance( + unsigned index) { return index; } -char *Analog_Input_Name(uint32_t object_instance) +char *Analog_Input_Name( + uint32_t object_instance) { static char text_string[32] = ""; /* okay for single thread */ @@ -118,67 +119,73 @@ char *Analog_Input_Name(uint32_t object_instance) /* return apdu length, or -1 on error */ /* assumption - object has already exists */ -int Analog_Input_Encode_Property_APDU(uint8_t * apdu, +int Analog_Input_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ BACNET_BIT_STRING bit_string; BACNET_CHARACTER_STRING char_string; float value = 3.14159F; (void) array_index; switch (property) { - case PROP_OBJECT_IDENTIFIER: - apdu_len = encode_application_object_id(&apdu[0], OBJECT_ANALOG_INPUT, - object_instance); - break; - case PROP_OBJECT_NAME: - case PROP_DESCRIPTION: - characterstring_init_ansi(&char_string, - Analog_Input_Name(object_instance)); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_OBJECT_TYPE: - apdu_len = encode_application_enumerated(&apdu[0], OBJECT_ANALOG_INPUT); - break; - case PROP_PRESENT_VALUE: - apdu_len = encode_application_real(&apdu[0], value); - break; - case PROP_STATUS_FLAGS: - bitstring_init(&bit_string); - bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); - apdu_len = encode_application_bitstring(&apdu[0], &bit_string); - break; - case PROP_EVENT_STATE: - apdu_len = encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); - break; - case PROP_OUT_OF_SERVICE: - apdu_len = encode_application_boolean(&apdu[0], false); - break; - case PROP_UNITS: - apdu_len = encode_application_enumerated(&apdu[0], UNITS_PERCENT); - break; - case 9997: - apdu_len = encode_application_real(&apdu[0], (float) 90.510); - break; - case 9998: - apdu_len = encode_application_unsigned(&apdu[0], 90); - break; - /* test case for signed encoding-decoding negative value correctly */ - case 9999: - apdu_len = encode_application_signed(&apdu[0], -200); - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_UNKNOWN_PROPERTY; - apdu_len = -1; - break; + case PROP_OBJECT_IDENTIFIER: + apdu_len = + encode_application_object_id(&apdu[0], OBJECT_ANALOG_INPUT, + object_instance); + break; + case PROP_OBJECT_NAME: + case PROP_DESCRIPTION: + characterstring_init_ansi(&char_string, + Analog_Input_Name(object_instance)); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_OBJECT_TYPE: + apdu_len = + encode_application_enumerated(&apdu[0], OBJECT_ANALOG_INPUT); + break; + case PROP_PRESENT_VALUE: + apdu_len = encode_application_real(&apdu[0], value); + break; + case PROP_STATUS_FLAGS: + bitstring_init(&bit_string); + bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); + apdu_len = encode_application_bitstring(&apdu[0], &bit_string); + break; + case PROP_EVENT_STATE: + apdu_len = + encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); + break; + case PROP_OUT_OF_SERVICE: + apdu_len = encode_application_boolean(&apdu[0], false); + break; + case PROP_UNITS: + apdu_len = encode_application_enumerated(&apdu[0], UNITS_PERCENT); + break; + case 9997: + apdu_len = encode_application_real(&apdu[0], (float) 90.510); + break; + case 9998: + apdu_len = encode_application_unsigned(&apdu[0], 90); + break; + /* test case for signed encoding-decoding negative value correctly */ + case 9999: + apdu_len = encode_application_signed(&apdu[0], -200); + break; + default: + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_UNKNOWN_PROPERTY; + apdu_len = -1; + break; } return apdu_len; @@ -189,7 +196,8 @@ int Analog_Input_Encode_Property_APDU(uint8_t * apdu, #include #include "ctest.h" -void testAnalogInput(Test * pTest) +void testAnalogInput( + Test * pTest) { uint8_t apdu[MAX_APDU] = { 0 }; int len = 0; @@ -205,8 +213,7 @@ void testAnalogInput(Test * pTest) /* FIXME: we should do a lot more testing here... */ len = Analog_Input_Encode_Property_APDU(&apdu[0], instance, - PROP_OBJECT_IDENTIFIER, - BACNET_ARRAY_ALL, &error_class, &error_code); + PROP_OBJECT_IDENTIFIER, BACNET_ARRAY_ALL, &error_class, &error_code); ct_test(pTest, len >= 0); len = decode_tag_number_and_value(&apdu[0], &tag_number, &len_value); ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_OBJECT_ID); @@ -219,7 +226,8 @@ void testAnalogInput(Test * pTest) } #ifdef TEST_ANALOG_INPUT -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -236,5 +244,5 @@ int main(void) return 0; } -#endif /* TEST_ANALOG_INPUT */ -#endif /* TEST */ +#endif /* TEST_ANALOG_INPUT */ +#endif /* TEST */ diff --git a/bacnet-stack/demo/object/ao.c b/bacnet-stack/demo/object/ao.c index 9c43e73c..261de0ef 100644 --- a/bacnet-stack/demo/object/ao.c +++ b/bacnet-stack/demo/object/ao.c @@ -32,7 +32,7 @@ #include "bacdcode.h" #include "bacenum.h" #include "bacapp.h" -#include "config.h" /* the custom stuff */ +#include "config.h" /* the custom stuff */ #include "wp.h" #define MAX_ANALOG_OUTPUTS 4 @@ -47,8 +47,7 @@ /* Here is our Priority Array. They are supposed to be Real, but */ /* we don't have that kind of memory, so we will use a single byte */ /* and load a Real for returning the value when asked. */ -static uint8_t - Analog_Output_Level[MAX_ANALOG_OUTPUTS][BACNET_MAX_PRIORITY]; +static uint8_t Analog_Output_Level[MAX_ANALOG_OUTPUTS][BACNET_MAX_PRIORITY]; /* Writable out-of-service allows others to play with our Present Value */ /* without changing the physical output */ static bool Analog_Output_Out_Of_Service[MAX_ANALOG_OUTPUTS]; @@ -57,8 +56,7 @@ static bool Analog_Output_Out_Of_Service[MAX_ANALOG_OUTPUTS]; static bool Analog_Output_Initialized = false; /* These three arrays are used by the ReadPropertyMultiple handler */ -static const int Analog_Output_Properties_Required[] = -{ +static const int Analog_Output_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, PROP_OBJECT_NAME, PROP_OBJECT_TYPE, @@ -72,14 +70,12 @@ static const int Analog_Output_Properties_Required[] = -1 }; -static const int Analog_Output_Properties_Optional[] = -{ +static const int Analog_Output_Properties_Optional[] = { PROP_DESCRIPTION, -1 }; -static const int Analog_Output_Properties_Proprietary[] = -{ +static const int Analog_Output_Properties_Proprietary[] = { -1 }; @@ -98,7 +94,8 @@ void Analog_Output_Property_Lists( return; } -void Analog_Output_Init(void) +void Analog_Output_Init( + void) { unsigned i, j; @@ -119,7 +116,8 @@ void Analog_Output_Init(void) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need validate that the */ /* given instance exists */ -bool Analog_Output_Valid_Instance(uint32_t object_instance) +bool Analog_Output_Valid_Instance( + uint32_t object_instance) { Analog_Output_Init(); if (object_instance < MAX_ANALOG_OUTPUTS) @@ -130,7 +128,8 @@ bool Analog_Output_Valid_Instance(uint32_t object_instance) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then count how many you have */ -unsigned Analog_Output_Count(void) +unsigned Analog_Output_Count( + void) { Analog_Output_Init(); return MAX_ANALOG_OUTPUTS; @@ -139,7 +138,8 @@ unsigned Analog_Output_Count(void) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need to return the instance */ /* that correlates to the correct index */ -uint32_t Analog_Output_Index_To_Instance(unsigned index) +uint32_t Analog_Output_Index_To_Instance( + unsigned index) { Analog_Output_Init(); return index; @@ -148,7 +148,8 @@ uint32_t Analog_Output_Index_To_Instance(unsigned index) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need to return the index */ /* that correlates to the correct instance number */ -unsigned Analog_Output_Instance_To_Index(uint32_t object_instance) +unsigned Analog_Output_Instance_To_Index( + uint32_t object_instance) { unsigned index = MAX_ANALOG_OUTPUTS; @@ -159,7 +160,8 @@ unsigned Analog_Output_Instance_To_Index(uint32_t object_instance) return index; } -float Analog_Output_Present_Value(uint32_t object_instance) +float Analog_Output_Present_Value( + uint32_t object_instance) { float value = AO_RELINQUISH_DEFAULT; unsigned index = 0; @@ -179,10 +181,11 @@ float Analog_Output_Present_Value(uint32_t object_instance) return value; } -unsigned Analog_Output_Present_Value_Priority(uint32_t object_instance) +unsigned Analog_Output_Present_Value_Priority( + uint32_t object_instance) { - unsigned index = 0; /* instance to index conversion */ - unsigned i = 0; /* loop counter */ + unsigned index = 0; /* instance to index conversion */ + unsigned i = 0; /* loop counter */ unsigned priority = 0; /* return value */ Analog_Output_Init(); @@ -199,8 +202,10 @@ unsigned Analog_Output_Present_Value_Priority(uint32_t object_instance) return priority; } -bool Analog_Output_Present_Value_Set(uint32_t object_instance, - float value, unsigned priority) +bool Analog_Output_Present_Value_Set( + uint32_t object_instance, + float value, + unsigned priority) { unsigned index = 0; bool status = false; @@ -210,7 +215,7 @@ bool Analog_Output_Present_Value_Set(uint32_t object_instance, if (priority && (priority <= BACNET_MAX_PRIORITY) && (priority != 6 /* reserved */ ) && (value >= 0.0) && (value <= 100.0)) { - Analog_Output_Level[index][priority-1] = (uint8_t) value; + Analog_Output_Level[index][priority - 1] = (uint8_t) value; /* Note: you could set the physical output here to the next highest priority, or to the relinquish default if no priorities are set. @@ -224,7 +229,8 @@ bool Analog_Output_Present_Value_Set(uint32_t object_instance, return status; } -bool Analog_Output_Present_Value_Relinquish(uint32_t object_instance, +bool Analog_Output_Present_Value_Relinquish( + uint32_t object_instance, unsigned priority) { unsigned index = 0; @@ -234,7 +240,7 @@ bool Analog_Output_Present_Value_Relinquish(uint32_t object_instance, if (index < MAX_ANALOG_OUTPUTS) { if (priority && (priority <= BACNET_MAX_PRIORITY) && (priority != 6 /* reserved */ )) { - Analog_Output_Level[index][priority-1] = AO_LEVEL_NULL; + Analog_Output_Level[index][priority - 1] = AO_LEVEL_NULL; /* Note: you could set the physical output here to the next highest priority, or to the relinquish default if no priorities are set. @@ -249,7 +255,8 @@ bool Analog_Output_Present_Value_Relinquish(uint32_t object_instance, } /* note: the object name must be unique within this device */ -char *Analog_Output_Name(uint32_t object_instance) +char *Analog_Output_Name( + uint32_t object_instance) { static char text_string[32] = ""; /* okay for single thread */ @@ -262,14 +269,16 @@ char *Analog_Output_Name(uint32_t object_instance) } /* return apdu len, or -1 on error */ -int Analog_Output_Encode_Property_APDU(uint8_t * apdu, +int Analog_Output_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { int len = 0; - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ BACNET_BIT_STRING bit_string; BACNET_CHARACTER_STRING char_string; float real_value = (float) 1.414; @@ -279,107 +288,115 @@ int Analog_Output_Encode_Property_APDU(uint8_t * apdu, Analog_Output_Init(); switch (property) { - case PROP_OBJECT_IDENTIFIER: - apdu_len = encode_application_object_id(&apdu[0], OBJECT_ANALOG_OUTPUT, - object_instance); - break; - case PROP_OBJECT_NAME: - case PROP_DESCRIPTION: - characterstring_init_ansi(&char_string, - Analog_Output_Name(object_instance)); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_OBJECT_TYPE: - apdu_len = - encode_application_enumerated(&apdu[0], OBJECT_ANALOG_OUTPUT); - break; - case PROP_PRESENT_VALUE: - real_value = Analog_Output_Present_Value(object_instance); - apdu_len = encode_application_real(&apdu[0], real_value); - break; - case PROP_STATUS_FLAGS: - bitstring_init(&bit_string); - bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); - apdu_len = encode_application_bitstring(&apdu[0], &bit_string); - break; - case PROP_EVENT_STATE: - apdu_len = encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); - break; - case PROP_OUT_OF_SERVICE: - object_index = Analog_Output_Instance_To_Index(object_instance); - state = Analog_Output_Out_Of_Service[object_index]; - apdu_len = encode_application_boolean(&apdu[0], state); - break; - case PROP_UNITS: - apdu_len = encode_application_enumerated(&apdu[0], UNITS_PERCENT); - break; - case PROP_PRIORITY_ARRAY: - /* Array element zero is the number of elements in the array */ - if (array_index == 0) + case PROP_OBJECT_IDENTIFIER: apdu_len = - encode_application_unsigned(&apdu[0], BACNET_MAX_PRIORITY); - /* if no index was specified, then try to encode the entire list */ - /* into one packet. */ - else if (array_index == BACNET_ARRAY_ALL) { - object_index = - Analog_Output_Instance_To_Index(object_instance); - for (i = 0; i < BACNET_MAX_PRIORITY; i++) { - /* FIXME: check if we have room before adding it to APDU */ - if (Analog_Output_Level[object_index][i] == AO_LEVEL_NULL) - len = encode_application_null(&apdu[apdu_len]); - else { - real_value = Analog_Output_Level[object_index][i]; - len = encode_application_real(&apdu[apdu_len], real_value); - } - /* add it if we have room */ - if ((apdu_len + len) < MAX_APDU) - apdu_len += len; - else { - *error_class = ERROR_CLASS_SERVICES; - *error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT; - apdu_len = -1; - break; - } - } - } else { - object_index = - Analog_Output_Instance_To_Index(object_instance); - if (array_index <= BACNET_MAX_PRIORITY) { - if (Analog_Output_Level[object_index][array_index - 1] == - AO_LEVEL_NULL) - apdu_len = encode_application_null(&apdu[0]); - else { - real_value = - Analog_Output_Level[object_index][array_index - 1]; - apdu_len = encode_application_real(&apdu[0], real_value); + encode_application_object_id(&apdu[0], OBJECT_ANALOG_OUTPUT, + object_instance); + break; + case PROP_OBJECT_NAME: + case PROP_DESCRIPTION: + characterstring_init_ansi(&char_string, + Analog_Output_Name(object_instance)); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_OBJECT_TYPE: + apdu_len = + encode_application_enumerated(&apdu[0], OBJECT_ANALOG_OUTPUT); + break; + case PROP_PRESENT_VALUE: + real_value = Analog_Output_Present_Value(object_instance); + apdu_len = encode_application_real(&apdu[0], real_value); + break; + case PROP_STATUS_FLAGS: + bitstring_init(&bit_string); + bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); + apdu_len = encode_application_bitstring(&apdu[0], &bit_string); + break; + case PROP_EVENT_STATE: + apdu_len = + encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); + break; + case PROP_OUT_OF_SERVICE: + object_index = Analog_Output_Instance_To_Index(object_instance); + state = Analog_Output_Out_Of_Service[object_index]; + apdu_len = encode_application_boolean(&apdu[0], state); + break; + case PROP_UNITS: + apdu_len = encode_application_enumerated(&apdu[0], UNITS_PERCENT); + break; + case PROP_PRIORITY_ARRAY: + /* Array element zero is the number of elements in the array */ + if (array_index == 0) + apdu_len = + encode_application_unsigned(&apdu[0], BACNET_MAX_PRIORITY); + /* if no index was specified, then try to encode the entire list */ + /* into one packet. */ + else if (array_index == BACNET_ARRAY_ALL) { + object_index = + Analog_Output_Instance_To_Index(object_instance); + for (i = 0; i < BACNET_MAX_PRIORITY; i++) { + /* FIXME: check if we have room before adding it to APDU */ + if (Analog_Output_Level[object_index][i] == AO_LEVEL_NULL) + len = encode_application_null(&apdu[apdu_len]); + else { + real_value = Analog_Output_Level[object_index][i]; + len = + encode_application_real(&apdu[apdu_len], + real_value); + } + /* add it if we have room */ + if ((apdu_len + len) < MAX_APDU) + apdu_len += len; + else { + *error_class = ERROR_CLASS_SERVICES; + *error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT; + apdu_len = -1; + break; + } } } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_ARRAY_INDEX; - apdu_len = -1; + object_index = + Analog_Output_Instance_To_Index(object_instance); + if (array_index <= BACNET_MAX_PRIORITY) { + if (Analog_Output_Level[object_index][array_index - 1] == + AO_LEVEL_NULL) + apdu_len = encode_application_null(&apdu[0]); + else { + real_value = + Analog_Output_Level[object_index][array_index - 1]; + apdu_len = + encode_application_real(&apdu[0], real_value); + } + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_ARRAY_INDEX; + apdu_len = -1; + } } - } - break; - case PROP_RELINQUISH_DEFAULT: - real_value = AO_RELINQUISH_DEFAULT; - apdu_len = encode_application_real(&apdu[0], real_value); - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_UNKNOWN_PROPERTY; - apdu_len = -1; - break; + break; + case PROP_RELINQUISH_DEFAULT: + real_value = AO_RELINQUISH_DEFAULT; + apdu_len = encode_application_real(&apdu[0], real_value); + break; + default: + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_UNKNOWN_PROPERTY; + apdu_len = -1; + break; } return apdu_len; } /* returns true if successful */ -bool Analog_Output_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) +bool Analog_Output_Write_Property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { bool status = false; /* return value */ unsigned int object_index = 0; @@ -399,56 +416,56 @@ bool Analog_Output_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, /* FIXME: len < application_data_len: more data? */ /* FIXME: len == 0: unable to decode? */ switch (wp_data->object_property) { - case PROP_PRESENT_VALUE: - if (value.tag == BACNET_APPLICATION_TAG_REAL) { - /* Command priority 6 is reserved for use by Minimum On/Off - algorithm and may not be used for other purposes in any - object. */ - status = - Analog_Output_Present_Value_Set(wp_data->object_instance, - value.type.Real, wp_data->priority); - if (wp_data->priority == 6) { + case PROP_PRESENT_VALUE: + if (value.tag == BACNET_APPLICATION_TAG_REAL) { /* Command priority 6 is reserved for use by Minimum On/Off algorithm and may not be used for other purposes in any object. */ + status = + Analog_Output_Present_Value_Set(wp_data->object_instance, + value.type.Real, wp_data->priority); + if (wp_data->priority == 6) { + /* Command priority 6 is reserved for use by Minimum On/Off + algorithm and may not be used for other purposes in any + object. */ + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + } else if (!status) { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } + } else if (value.tag == BACNET_APPLICATION_TAG_NULL) { + level = AO_LEVEL_NULL; + object_index = + Analog_Output_Instance_To_Index(wp_data->object_instance); + status = + Analog_Output_Present_Value_Relinquish(wp_data-> + object_instance, wp_data->priority); + if (!status) { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } + } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; - } else if (!status) { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } - } else if (value.tag == BACNET_APPLICATION_TAG_NULL) { - level = AO_LEVEL_NULL; - object_index = - Analog_Output_Instance_To_Index(wp_data->object_instance); - status = - Analog_Output_Present_Value_Relinquish(wp_data-> - object_instance, wp_data->priority); - if (!status) { + break; + case PROP_OUT_OF_SERVICE: + if (value.tag == BACNET_APPLICATION_TAG_BOOLEAN) { + object_index = + Analog_Output_Instance_To_Index(wp_data->object_instance); + Analog_Output_Out_Of_Service[object_index] = + value.type.Boolean; + status = true; + } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } - } else { + break; + default: *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case PROP_OUT_OF_SERVICE: - if (value.tag == BACNET_APPLICATION_TAG_BOOLEAN) { - object_index = - Analog_Output_Instance_To_Index(wp_data->object_instance); - Analog_Output_Out_Of_Service[object_index] = - value.type.Boolean; - status = true; - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; - break; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + break; } return status; @@ -460,7 +477,8 @@ bool Analog_Output_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, #include #include "ctest.h" -void testAnalogOutput(Test * pTest) +void testAnalogOutput( + Test * pTest) { uint8_t apdu[MAX_APDU] = { 0 }; int len = 0; @@ -475,8 +493,7 @@ void testAnalogOutput(Test * pTest) len = Analog_Output_Encode_Property_APDU(&apdu[0], instance, - PROP_OBJECT_IDENTIFIER, - BACNET_ARRAY_ALL, &error_class, &error_code); + PROP_OBJECT_IDENTIFIER, BACNET_ARRAY_ALL, &error_class, &error_code); ct_test(pTest, len != 0); len = decode_tag_number_and_value(&apdu[0], &tag_number, &len_value); ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_OBJECT_ID); @@ -489,7 +506,8 @@ void testAnalogOutput(Test * pTest) } #ifdef TEST_ANALOG_OUTPUT -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -506,5 +524,5 @@ int main(void) return 0; } -#endif /* TEST_ANALOG_INPUT */ -#endif /* TEST */ +#endif /* TEST_ANALOG_INPUT */ +#endif /* TEST */ diff --git a/bacnet-stack/demo/object/av.c b/bacnet-stack/demo/object/av.c index 08093507..b3ed00b3 100644 --- a/bacnet-stack/demo/object/av.c +++ b/bacnet-stack/demo/object/av.c @@ -32,7 +32,7 @@ #include "bacdcode.h" #include "bacenum.h" #include "bacapp.h" -#include "config.h" /* the custom stuff */ +#include "config.h" /* the custom stuff */ #include "wp.h" #define MAX_ANALOG_VALUES 4 @@ -56,8 +56,7 @@ static bool Analog_Value_Out_Of_Service[MAX_ANALOG_VALUES]; static bool Analog_Value_Initialized = false; /* These three arrays are used by the ReadPropertyMultiple handler */ -static const int Analog_Value_Properties_Required[] = -{ +static const int Analog_Value_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, PROP_OBJECT_NAME, PROP_OBJECT_TYPE, @@ -69,16 +68,14 @@ static const int Analog_Value_Properties_Required[] = -1 }; -static const int Analog_Value_Properties_Optional[] = -{ +static const int Analog_Value_Properties_Optional[] = { PROP_DESCRIPTION, PROP_PRIORITY_ARRAY, PROP_RELINQUISH_DEFAULT, -1 }; -static const int Analog_Value_Properties_Proprietary[] = -{ +static const int Analog_Value_Properties_Proprietary[] = { -1 }; @@ -97,7 +94,8 @@ void Analog_Value_Property_Lists( return; } -void Analog_Value_Init(void) +void Analog_Value_Init( + void) { unsigned i, j; @@ -118,7 +116,8 @@ void Analog_Value_Init(void) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need validate that the */ /* given instance exists */ -bool Analog_Value_Valid_Instance(uint32_t object_instance) +bool Analog_Value_Valid_Instance( + uint32_t object_instance) { Analog_Value_Init(); if (object_instance < MAX_ANALOG_VALUES) @@ -129,7 +128,8 @@ bool Analog_Value_Valid_Instance(uint32_t object_instance) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then count how many you have */ -unsigned Analog_Value_Count(void) +unsigned Analog_Value_Count( + void) { Analog_Value_Init(); return MAX_ANALOG_VALUES; @@ -138,7 +138,8 @@ unsigned Analog_Value_Count(void) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need to return the instance */ /* that correlates to the correct index */ -uint32_t Analog_Value_Index_To_Instance(unsigned index) +uint32_t Analog_Value_Index_To_Instance( + unsigned index) { Analog_Value_Init(); return index; @@ -147,7 +148,8 @@ uint32_t Analog_Value_Index_To_Instance(unsigned index) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need to return the index */ /* that correlates to the correct instance number */ -unsigned Analog_Value_Instance_To_Index(uint32_t object_instance) +unsigned Analog_Value_Instance_To_Index( + uint32_t object_instance) { unsigned index = MAX_ANALOG_VALUES; @@ -158,7 +160,8 @@ unsigned Analog_Value_Instance_To_Index(uint32_t object_instance) return index; } -static float Analog_Value_Present_Value(uint32_t object_instance) +static float Analog_Value_Present_Value( + uint32_t object_instance) { float value = ANALOG_RELINQUISH_DEFAULT; unsigned index = 0; @@ -179,7 +182,8 @@ static float Analog_Value_Present_Value(uint32_t object_instance) } /* note: the object name must be unique within this device */ -char *Analog_Value_Name(uint32_t object_instance) +char *Analog_Value_Name( + uint32_t object_instance) { static char text_string[32] = ""; /* okay for single thread */ @@ -192,14 +196,16 @@ char *Analog_Value_Name(uint32_t object_instance) } /* return apdu len, or -1 on error */ -int Analog_Value_Encode_Property_APDU(uint8_t * apdu, +int Analog_Value_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { int len = 0; - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ BACNET_BIT_STRING bit_string; BACNET_CHARACTER_STRING char_string; float real_value = (float) 1.414; @@ -209,106 +215,115 @@ int Analog_Value_Encode_Property_APDU(uint8_t * apdu, Analog_Value_Init(); switch (property) { - case PROP_OBJECT_IDENTIFIER: - apdu_len = encode_application_object_id(&apdu[0], OBJECT_ANALOG_VALUE, - object_instance); - break; - case PROP_OBJECT_NAME: - case PROP_DESCRIPTION: - characterstring_init_ansi(&char_string, - Analog_Value_Name(object_instance)); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_OBJECT_TYPE: - apdu_len = encode_application_enumerated(&apdu[0], OBJECT_ANALOG_VALUE); - break; - case PROP_PRESENT_VALUE: - real_value = Analog_Value_Present_Value(object_instance); - apdu_len = encode_application_real(&apdu[0], real_value); - break; - case PROP_STATUS_FLAGS: - bitstring_init(&bit_string); - bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); - apdu_len = encode_application_bitstring(&apdu[0], &bit_string); - break; - case PROP_EVENT_STATE: - apdu_len = encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); - break; - case PROP_OUT_OF_SERVICE: - object_index = Analog_Value_Instance_To_Index(object_instance); - state = Analog_Value_Out_Of_Service[object_index]; - apdu_len = encode_application_boolean(&apdu[0], state); - break; - case PROP_UNITS: - apdu_len = encode_application_enumerated(&apdu[0], UNITS_PERCENT); - break; - case PROP_PRIORITY_ARRAY: - /* Array element zero is the number of elements in the array */ - if (array_index == 0) + case PROP_OBJECT_IDENTIFIER: apdu_len = - encode_application_unsigned(&apdu[0], BACNET_MAX_PRIORITY); - /* if no index was specified, then try to encode the entire list */ - /* into one packet. */ - else if (array_index == BACNET_ARRAY_ALL) { + encode_application_object_id(&apdu[0], OBJECT_ANALOG_VALUE, + object_instance); + break; + case PROP_OBJECT_NAME: + case PROP_DESCRIPTION: + characterstring_init_ansi(&char_string, + Analog_Value_Name(object_instance)); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_OBJECT_TYPE: + apdu_len = + encode_application_enumerated(&apdu[0], OBJECT_ANALOG_VALUE); + break; + case PROP_PRESENT_VALUE: + real_value = Analog_Value_Present_Value(object_instance); + apdu_len = encode_application_real(&apdu[0], real_value); + break; + case PROP_STATUS_FLAGS: + bitstring_init(&bit_string); + bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); + apdu_len = encode_application_bitstring(&apdu[0], &bit_string); + break; + case PROP_EVENT_STATE: + apdu_len = + encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); + break; + case PROP_OUT_OF_SERVICE: object_index = Analog_Value_Instance_To_Index(object_instance); - for (i = 0; i < BACNET_MAX_PRIORITY; i++) { - /* FIXME: check if we have room before adding it to APDU */ - if (Analog_Value_Level[object_index][i] == - ANALOG_LEVEL_NULL) - len = encode_application_null(&apdu[apdu_len]); - else { - real_value = Analog_Value_Level[object_index][i]; - len = encode_application_real(&apdu[apdu_len], real_value); - } - /* add it if we have room */ - if ((apdu_len + len) < MAX_APDU) - apdu_len += len; - else { - *error_class = ERROR_CLASS_SERVICES; - *error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT; - apdu_len = -1; - break; - } - } - } else { - object_index = Analog_Value_Instance_To_Index(object_instance); - if (array_index <= BACNET_MAX_PRIORITY) { - if (Analog_Value_Level[object_index][array_index - 1] == - ANALOG_LEVEL_NULL) - apdu_len = encode_application_null(&apdu[0]); - else { - real_value = - Analog_Value_Level[object_index][array_index - 1]; - apdu_len = encode_application_real(&apdu[0], real_value); + state = Analog_Value_Out_Of_Service[object_index]; + apdu_len = encode_application_boolean(&apdu[0], state); + break; + case PROP_UNITS: + apdu_len = encode_application_enumerated(&apdu[0], UNITS_PERCENT); + break; + case PROP_PRIORITY_ARRAY: + /* Array element zero is the number of elements in the array */ + if (array_index == 0) + apdu_len = + encode_application_unsigned(&apdu[0], BACNET_MAX_PRIORITY); + /* if no index was specified, then try to encode the entire list */ + /* into one packet. */ + else if (array_index == BACNET_ARRAY_ALL) { + object_index = Analog_Value_Instance_To_Index(object_instance); + for (i = 0; i < BACNET_MAX_PRIORITY; i++) { + /* FIXME: check if we have room before adding it to APDU */ + if (Analog_Value_Level[object_index][i] == + ANALOG_LEVEL_NULL) + len = encode_application_null(&apdu[apdu_len]); + else { + real_value = Analog_Value_Level[object_index][i]; + len = + encode_application_real(&apdu[apdu_len], + real_value); + } + /* add it if we have room */ + if ((apdu_len + len) < MAX_APDU) + apdu_len += len; + else { + *error_class = ERROR_CLASS_SERVICES; + *error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT; + apdu_len = -1; + break; + } } } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_ARRAY_INDEX; - apdu_len = -1; + object_index = Analog_Value_Instance_To_Index(object_instance); + if (array_index <= BACNET_MAX_PRIORITY) { + if (Analog_Value_Level[object_index][array_index - 1] == + ANALOG_LEVEL_NULL) + apdu_len = encode_application_null(&apdu[0]); + else { + real_value = + Analog_Value_Level[object_index][array_index - 1]; + apdu_len = + encode_application_real(&apdu[0], real_value); + } + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_ARRAY_INDEX; + apdu_len = -1; + } } - } - break; - case PROP_RELINQUISH_DEFAULT: - real_value = ANALOG_RELINQUISH_DEFAULT; - apdu_len = encode_application_real(&apdu[0], real_value); - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_UNKNOWN_PROPERTY; - apdu_len = -1; - break; + break; + case PROP_RELINQUISH_DEFAULT: + real_value = ANALOG_RELINQUISH_DEFAULT; + apdu_len = encode_application_real(&apdu[0], real_value); + break; + default: + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_UNKNOWN_PROPERTY; + apdu_len = -1; + break; } return apdu_len; } /* returns true if successful */ -bool Analog_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) +bool Analog_Value_Write_Property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { bool status = false; /* return value */ unsigned int object_index = 0; @@ -329,76 +344,76 @@ bool Analog_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, /* FIXME: len < application_data_len: more data? */ /* FIXME: len == 0: unable to decode? */ switch (wp_data->object_property) { - case PROP_PRESENT_VALUE: - if (value.tag == BACNET_APPLICATION_TAG_REAL) { - priority = wp_data->priority; - /* Command priority 6 is reserved for use by Minimum On/Off - algorithm and may not be used for other purposes in any - object. */ - if (priority && (priority <= BACNET_MAX_PRIORITY) && - (priority != 6 /* reserved */ ) && - (value.type.Real >= 0.0) && (value.type.Real <= 100.0)) { - level = (uint8_t) value.type.Real; - object_index = - Analog_Value_Instance_To_Index(wp_data-> - object_instance); - priority--; - Analog_Value_Level[object_index][priority] = level; - /* Note: you could set the physical output here if we - are the highest priority. - However, if Out of Service is TRUE, then don't set the - physical output. This comment may apply to the - main loop (i.e. check out of service before changing output) */ - status = true; - } else if (priority == 6) { + case PROP_PRESENT_VALUE: + if (value.tag == BACNET_APPLICATION_TAG_REAL) { + priority = wp_data->priority; /* Command priority 6 is reserved for use by Minimum On/Off algorithm and may not be used for other purposes in any object. */ - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + if (priority && (priority <= BACNET_MAX_PRIORITY) && + (priority != 6 /* reserved */ ) && + (value.type.Real >= 0.0) && (value.type.Real <= 100.0)) { + level = (uint8_t) value.type.Real; + object_index = + Analog_Value_Instance_To_Index(wp_data-> + object_instance); + priority--; + Analog_Value_Level[object_index][priority] = level; + /* Note: you could set the physical output here if we + are the highest priority. + However, if Out of Service is TRUE, then don't set the + physical output. This comment may apply to the + main loop (i.e. check out of service before changing output) */ + status = true; + } else if (priority == 6) { + /* Command priority 6 is reserved for use by Minimum On/Off + algorithm and may not be used for other purposes in any + object. */ + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } + } else if (value.tag == BACNET_APPLICATION_TAG_NULL) { + level = ANALOG_LEVEL_NULL; + object_index = + Analog_Value_Instance_To_Index(wp_data->object_instance); + priority = wp_data->priority; + if (priority && (priority <= BACNET_MAX_PRIORITY)) { + priority--; + Analog_Value_Level[object_index][priority] = level; + /* Note: you could set the physical output here to the next + highest priority, or to the relinquish default if no + priorities are set. + However, if Out of Service is TRUE, then don't set the + physical output. This comment may apply to the + main loop (i.e. check out of service before changing output) */ + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } - } else if (value.tag == BACNET_APPLICATION_TAG_NULL) { - level = ANALOG_LEVEL_NULL; - object_index = - Analog_Value_Instance_To_Index(wp_data->object_instance); - priority = wp_data->priority; - if (priority && (priority <= BACNET_MAX_PRIORITY)) { - priority--; - Analog_Value_Level[object_index][priority] = level; - /* Note: you could set the physical output here to the next - highest priority, or to the relinquish default if no - priorities are set. - However, if Out of Service is TRUE, then don't set the - physical output. This comment may apply to the - main loop (i.e. check out of service before changing output) */ + break; + case PROP_OUT_OF_SERVICE: + if (value.tag == BACNET_APPLICATION_TAG_BOOLEAN) { + object_index = + Analog_Value_Instance_To_Index(wp_data->object_instance); + Analog_Value_Out_Of_Service[object_index] = value.type.Boolean; status = true; } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } - } else { + break; + default: *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case PROP_OUT_OF_SERVICE: - if (value.tag == BACNET_APPLICATION_TAG_BOOLEAN) { - object_index = - Analog_Value_Instance_To_Index(wp_data->object_instance); - Analog_Value_Out_Of_Service[object_index] = value.type.Boolean; - status = true; - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; - break; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + break; } return status; @@ -410,7 +425,8 @@ bool Analog_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, #include #include "ctest.h" -void testAnalog_Value(Test * pTest) +void testAnalog_Value( + Test * pTest) { uint8_t apdu[MAX_APDU] = { 0 }; int len = 0; @@ -425,8 +441,7 @@ void testAnalog_Value(Test * pTest) len = Analog_Value_Encode_Property_APDU(&apdu[0], instance, - PROP_OBJECT_IDENTIFIER, - BACNET_ARRAY_ALL, &error_class, &error_code); + PROP_OBJECT_IDENTIFIER, BACNET_ARRAY_ALL, &error_class, &error_code); ct_test(pTest, len != 0); len = decode_tag_number_and_value(&apdu[0], &tag_number, &len_value); ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_OBJECT_ID); @@ -439,7 +454,8 @@ void testAnalog_Value(Test * pTest) } #ifdef TEST_ANALOG_VALUE -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -456,5 +472,5 @@ int main(void) return 0; } -#endif /* TEST_ANALOG_VALUE */ -#endif /* TEST */ +#endif /* TEST_ANALOG_VALUE */ +#endif /* TEST */ diff --git a/bacnet-stack/demo/object/bacfile.c b/bacnet-stack/demo/object/bacfile.c index d2d2e634..33d43f56 100644 --- a/bacnet-stack/demo/object/bacfile.c +++ b/bacnet-stack/demo/object/bacfile.c @@ -49,12 +49,11 @@ static BACNET_FILE_LISTING BACnet_File_Listing[] = { {0, "temp_0.txt"}, {1, "temp_1.txt"}, {2, "temp_2.txt"}, - {0, NULL} /* last file indication */ + {0, NULL} /* last file indication */ }; /* These three arrays are used by the ReadPropertyMultiple handler */ -static const int bacfile_Properties_Required[] = -{ +static const int bacfile_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, PROP_OBJECT_NAME, PROP_OBJECT_TYPE, @@ -67,14 +66,12 @@ static const int bacfile_Properties_Required[] = -1 }; -static const int bacfile_Properties_Optional[] = -{ +static const int bacfile_Properties_Optional[] = { PROP_DESCRIPTION, -1 }; -static const int bacfile_Properties_Proprietary[] = -{ +static const int bacfile_Properties_Proprietary[] = { -1 }; @@ -94,7 +91,8 @@ void BACfile_Property_Lists( } -char *bacfile_name(uint32_t instance) +char *bacfile_name( + uint32_t instance) { uint32_t index = 0; char *filename = NULL; @@ -111,12 +109,14 @@ char *bacfile_name(uint32_t instance) return filename; } -bool bacfile_valid_instance(uint32_t object_instance) +bool bacfile_valid_instance( + uint32_t object_instance) { return bacfile_name(object_instance) ? true : false; } -uint32_t bacfile_count(void) +uint32_t bacfile_count( + void) { uint32_t index = 0; @@ -128,7 +128,8 @@ uint32_t bacfile_count(void) return index; } -uint32_t bacfile_index_to_instance(unsigned find_index) +uint32_t bacfile_index_to_instance( + unsigned find_index) { uint32_t instance = BACNET_MAX_INSTANCE + 1; uint32_t index = 0; @@ -145,7 +146,8 @@ uint32_t bacfile_index_to_instance(unsigned find_index) return instance; } -static long fsize(FILE * pFile) +static long fsize( + FILE * pFile) { long size = 0; long origin = 0; @@ -159,7 +161,8 @@ static long fsize(FILE * pFile) return (size); } -static unsigned bacfile_file_size(uint32_t object_instance) +static unsigned bacfile_file_size( + uint32_t object_instance) { char *pFilename = NULL; FILE *pFile = NULL; @@ -178,13 +181,15 @@ static unsigned bacfile_file_size(uint32_t object_instance) } /* return the number of bytes used, or -1 on error */ -int bacfile_encode_property_apdu(uint8_t * apdu, +int bacfile_encode_property_apdu( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ char text_string[32] = { "" }; BACNET_CHARACTER_STRING char_string; BACNET_DATE bdate; @@ -192,76 +197,82 @@ int bacfile_encode_property_apdu(uint8_t * apdu, (void) array_index; switch (property) { - case PROP_OBJECT_IDENTIFIER: - apdu_len = encode_application_object_id(&apdu[0], - OBJECT_FILE, object_instance); - break; - case PROP_OBJECT_NAME: - sprintf(text_string, "FILE %d", object_instance); - characterstring_init_ansi(&char_string, text_string); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_OBJECT_TYPE: - apdu_len = encode_application_enumerated(&apdu[0], OBJECT_FILE); - break; - case PROP_DESCRIPTION: - characterstring_init_ansi(&char_string, - bacfile_name(object_instance)); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_FILE_TYPE: - characterstring_init_ansi(&char_string, "TEXT"); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_FILE_SIZE: - apdu_len = encode_application_unsigned(&apdu[0], - bacfile_file_size(object_instance)); - break; - case PROP_MODIFICATION_DATE: - /* FIXME: get the actual value instead of April Fool's Day */ - bdate.year = 2006; /* AD */ - bdate.month = 4; /* 1=Jan */ - bdate.day = 1; /* 1..31 */ - bdate.wday = 6; /* 1=Monday */ - apdu_len = encode_application_date(&apdu[0], &bdate); - /* FIXME: get the actual value */ - btime.hour = 7; - btime.min = 0; - btime.sec = 3; - btime.hundredths = 1; - apdu_len += encode_application_time(&apdu[apdu_len], &btime); - break; - case PROP_ARCHIVE: - /* 12.13.8 Archive - This property, of type BOOLEAN, indicates whether the File - object has been saved for historical or backup purposes. This - property shall be logical TRUE only if no changes have been - made to the file data by internal processes or through File - Access Services since the last time the object was archived. - */ - /* FIXME: get the actual value: note it may be inverse... */ - apdu_len = encode_application_boolean(&apdu[0], true); - break; - case PROP_READ_ONLY: - /* FIXME: get the actual value */ - apdu_len = encode_application_boolean(&apdu[0], true); - break; - case PROP_FILE_ACCESS_METHOD: - apdu_len = encode_application_enumerated(&apdu[0], FILE_STREAM_ACCESS); - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_UNKNOWN_PROPERTY; - apdu_len = -1; - break; + case PROP_OBJECT_IDENTIFIER: + apdu_len = encode_application_object_id(&apdu[0], + OBJECT_FILE, object_instance); + break; + case PROP_OBJECT_NAME: + sprintf(text_string, "FILE %d", object_instance); + characterstring_init_ansi(&char_string, text_string); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_OBJECT_TYPE: + apdu_len = encode_application_enumerated(&apdu[0], OBJECT_FILE); + break; + case PROP_DESCRIPTION: + characterstring_init_ansi(&char_string, + bacfile_name(object_instance)); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_FILE_TYPE: + characterstring_init_ansi(&char_string, "TEXT"); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_FILE_SIZE: + apdu_len = encode_application_unsigned(&apdu[0], + bacfile_file_size(object_instance)); + break; + case PROP_MODIFICATION_DATE: + /* FIXME: get the actual value instead of April Fool's Day */ + bdate.year = 2006; /* AD */ + bdate.month = 4; /* 1=Jan */ + bdate.day = 1; /* 1..31 */ + bdate.wday = 6; /* 1=Monday */ + apdu_len = encode_application_date(&apdu[0], &bdate); + /* FIXME: get the actual value */ + btime.hour = 7; + btime.min = 0; + btime.sec = 3; + btime.hundredths = 1; + apdu_len += encode_application_time(&apdu[apdu_len], &btime); + break; + case PROP_ARCHIVE: + /* 12.13.8 Archive + This property, of type BOOLEAN, indicates whether the File + object has been saved for historical or backup purposes. This + property shall be logical TRUE only if no changes have been + made to the file data by internal processes or through File + Access Services since the last time the object was archived. + */ + /* FIXME: get the actual value: note it may be inverse... */ + apdu_len = encode_application_boolean(&apdu[0], true); + break; + case PROP_READ_ONLY: + /* FIXME: get the actual value */ + apdu_len = encode_application_boolean(&apdu[0], true); + break; + case PROP_FILE_ACCESS_METHOD: + apdu_len = + encode_application_enumerated(&apdu[0], FILE_STREAM_ACCESS); + break; + default: + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_UNKNOWN_PROPERTY; + apdu_len = -1; + break; } return apdu_len; } /* returns true if successful */ -bool bacfile_write_property(BACNET_WRITE_PROPERTY_DATA * wp_data, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) +bool bacfile_write_property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { bool status = false; /* return value */ int len = 0; @@ -279,40 +290,40 @@ bool bacfile_write_property(BACNET_WRITE_PROPERTY_DATA * wp_data, /* FIXME: len < application_data_len: more data? */ /* FIXME: len == 0: unable to decode? */ switch (wp_data->object_property) { - case PROP_ARCHIVE: - /* 12.13.8 Archive - This property, of type BOOLEAN, indicates whether the File - object has been saved for historical or backup purposes. This - property shall be logical TRUE only if no changes have been - made to the file data by internal processes or through File - Access Services since the last time the object was archived. */ - if (value.tag == BACNET_APPLICATION_TAG_BOOLEAN) { - if (value.type.Boolean) { - /* FIXME: do something to wp_data->object_instance */ + case PROP_ARCHIVE: + /* 12.13.8 Archive + This property, of type BOOLEAN, indicates whether the File + object has been saved for historical or backup purposes. This + property shall be logical TRUE only if no changes have been + made to the file data by internal processes or through File + Access Services since the last time the object was archived. */ + if (value.tag == BACNET_APPLICATION_TAG_BOOLEAN) { + if (value.type.Boolean) { + /* FIXME: do something to wp_data->object_instance */ + } else { + /* FIXME: do something to wp_data->object_instance */ + } } else { - /* FIXME: do something to wp_data->object_instance */ + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } - } else { + break; + case PROP_FILE_SIZE: + /* If the file size can be changed by writing to the file, + and File_Access_Method is STREAM_ACCESS, then this property + shall be writable. */ + if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { + /* FIXME: do something with value.type.Unsigned + to wp_data->object_instance */ + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; + } + break; + default: *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case PROP_FILE_SIZE: - /* If the file size can be changed by writing to the file, - and File_Access_Method is STREAM_ACCESS, then this property - shall be writable. */ - if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { - /* FIXME: do something with value.type.Unsigned - to wp_data->object_instance */ - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; - break; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + break; } return status; @@ -320,7 +331,8 @@ bool bacfile_write_property(BACNET_WRITE_PROPERTY_DATA * wp_data, -uint32_t bacfile_instance(char *filename) +uint32_t bacfile_instance( + char *filename) { uint32_t index = 0; uint32_t instance = BACNET_MAX_INSTANCE + 1; @@ -343,7 +355,8 @@ uint32_t bacfile_instance(char *filename) /* Another way would be to store the */ /* invokeID and file instance in a list or table */ /* when the request was sent */ -uint32_t bacfile_instance_from_tsm(uint8_t invokeID) +uint32_t bacfile_instance_from_tsm( + uint8_t invokeID) { BACNET_NPDU_DATA npdu_data = { 0 }; /* dummy for getting npdu length */ BACNET_CONFIRMED_SERVICE_DATA service_data = { 0 }; @@ -353,7 +366,7 @@ uint32_t bacfile_instance_from_tsm(uint8_t invokeID) BACNET_ADDRESS dest; /* where the original packet was destined */ uint8_t apdu[MAX_PDU] = { 0 }; /* original APDU packet */ uint16_t apdu_len = 0; /* original APDU packet length */ - uint16_t len = 0; /* apdu header length */ + uint16_t len = 0; /* apdu header length */ BACNET_ATOMIC_READ_FILE_DATA data = { 0 }; uint32_t object_instance = BACNET_MAX_INSTANCE + 1; /* return value */ bool found = false; @@ -383,7 +396,8 @@ uint32_t bacfile_instance_from_tsm(uint8_t invokeID) } #endif -bool bacfile_read_data(BACNET_ATOMIC_READ_FILE_DATA * data) +bool bacfile_read_data( + BACNET_ATOMIC_READ_FILE_DATA * data) { char *pFilename = NULL; bool found = false; @@ -395,8 +409,7 @@ bool bacfile_read_data(BACNET_ATOMIC_READ_FILE_DATA * data) found = true; pFile = fopen(pFilename, "rb"); if (pFile) { - (void) fseek(pFile, - data->type.stream.fileStartPosition, SEEK_SET); + (void) fseek(pFile, data->type.stream.fileStartPosition, SEEK_SET); len = fread(octetstring_value(&data->fileData), 1, data->type.stream.requestedOctetCount, pFile); if (len < data->type.stream.requestedOctetCount) @@ -417,7 +430,8 @@ bool bacfile_read_data(BACNET_ATOMIC_READ_FILE_DATA * data) return found; } -bool bacfile_write_stream_data(BACNET_ATOMIC_WRITE_FILE_DATA * data) +bool bacfile_write_stream_data( + BACNET_ATOMIC_WRITE_FILE_DATA * data) { char *pFilename = NULL; bool found = false; @@ -428,15 +442,15 @@ bool bacfile_write_stream_data(BACNET_ATOMIC_WRITE_FILE_DATA * data) found = true; /* open the file as a clean slate when starting at 0 */ if (data->type.stream.fileStartPosition == 0) - pFile = fopen(pFilename, "wb"); + pFile = fopen(pFilename, "wb"); else - pFile = fopen(pFilename, "rb+"); + pFile = fopen(pFilename, "rb+"); if (pFile) { - (void)fseek(pFile, data->type.stream.fileStartPosition, SEEK_SET); + (void) fseek(pFile, data->type.stream.fileStartPosition, SEEK_SET); if (fwrite(octetstring_value(&data->fileData), - octetstring_length(&data->fileData),1,pFile) != 1) { + octetstring_length(&data->fileData), 1, pFile) != 1) { - } + } fclose(pFile); } } diff --git a/bacnet-stack/demo/object/bi.c b/bacnet-stack/demo/object/bi.c index 1060645a..0e7ea17c 100644 --- a/bacnet-stack/demo/object/bi.c +++ b/bacnet-stack/demo/object/bi.c @@ -31,15 +31,14 @@ #include "bacdef.h" #include "bacdcode.h" #include "bacenum.h" -#include "config.h" /* the custom stuff */ +#include "config.h" /* the custom stuff */ #define MAX_BINARY_INPUTS 5 static BACNET_BINARY_PV Present_Value[MAX_BINARY_INPUTS]; /* These three arrays are used by the ReadPropertyMultiple handler */ -static const int Binary_Input_Properties_Required[] = -{ +static const int Binary_Input_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, PROP_OBJECT_NAME, PROP_OBJECT_TYPE, @@ -51,14 +50,12 @@ static const int Binary_Input_Properties_Required[] = -1 }; -static const int Binary_Input_Properties_Optional[] = -{ +static const int Binary_Input_Properties_Optional[] = { PROP_DESCRIPTION, -1 }; -static const int Binary_Input_Properties_Proprietary[] = -{ +static const int Binary_Input_Properties_Proprietary[] = { -1 }; @@ -80,7 +77,8 @@ void Binary_Input_Property_Lists( /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need validate that the */ /* given instance exists */ -bool Binary_Input_Valid_Instance(uint32_t object_instance) +bool Binary_Input_Valid_Instance( + uint32_t object_instance) { if (object_instance < MAX_BINARY_INPUTS) return true; @@ -90,7 +88,8 @@ bool Binary_Input_Valid_Instance(uint32_t object_instance) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then count how many you have */ -unsigned Binary_Input_Count(void) +unsigned Binary_Input_Count( + void) { return MAX_BINARY_INPUTS; } @@ -98,12 +97,14 @@ unsigned Binary_Input_Count(void) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need to return the instance */ /* that correlates to the correct index */ -uint32_t Binary_Input_Index_To_Instance(unsigned index) +uint32_t Binary_Input_Index_To_Instance( + unsigned index) { return index; } -void Binary_Input_Init(void) +void Binary_Input_Init( + void) { static bool initialized = false; unsigned i; @@ -123,7 +124,8 @@ void Binary_Input_Init(void) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need to return the index */ /* that correlates to the correct instance number */ -unsigned Binary_Input_Instance_To_Index(uint32_t object_instance) +unsigned Binary_Input_Instance_To_Index( + uint32_t object_instance) { unsigned index = MAX_BINARY_INPUTS; @@ -134,8 +136,8 @@ unsigned Binary_Input_Instance_To_Index(uint32_t object_instance) return index; } -static BACNET_BINARY_PV Binary_Input_Present_Value(uint32_t - object_instance) +static BACNET_BINARY_PV Binary_Input_Present_Value( + uint32_t object_instance) { BACNET_BINARY_PV value = BINARY_INACTIVE; unsigned index = 0; @@ -148,7 +150,8 @@ static BACNET_BINARY_PV Binary_Input_Present_Value(uint32_t return value; } -char *Binary_Input_Name(uint32_t object_instance) +char *Binary_Input_Name( + uint32_t object_instance) { static char text_string[32] = ""; /* okay for single thread */ @@ -163,13 +166,15 @@ char *Binary_Input_Name(uint32_t object_instance) /* return apdu length, or -1 on error */ /* assumption - object already exists, and has been bounds checked */ -int Binary_Input_Encode_Property_APDU(uint8_t * apdu, +int Binary_Input_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ BACNET_BIT_STRING bit_string; BACNET_CHARACTER_STRING char_string; BACNET_POLARITY polarity = POLARITY_NORMAL; @@ -178,50 +183,54 @@ int Binary_Input_Encode_Property_APDU(uint8_t * apdu, (void) array_index; Binary_Input_Init(); switch (property) { - case PROP_OBJECT_IDENTIFIER: - apdu_len = encode_application_object_id(&apdu[0], OBJECT_BINARY_INPUT, - object_instance); - break; - case PROP_OBJECT_NAME: - case PROP_DESCRIPTION: - /* note: object name must be unique in our device */ - characterstring_init_ansi(&char_string, - Binary_Input_Name(object_instance)); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_OBJECT_TYPE: - apdu_len = encode_application_enumerated(&apdu[0], OBJECT_BINARY_INPUT); - break; - case PROP_PRESENT_VALUE: - /* note: you need to look up the actual value */ - apdu_len = - encode_application_enumerated(&apdu[0], - Binary_Input_Present_Value(object_instance)); - break; - case PROP_STATUS_FLAGS: - /* note: see the details in the standard on how to use these */ - bitstring_init(&bit_string); - bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); - apdu_len = encode_application_bitstring(&apdu[0], &bit_string); - break; - case PROP_EVENT_STATE: - /* note: see the details in the standard on how to use this */ - apdu_len = encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); - break; - case PROP_OUT_OF_SERVICE: - apdu_len = encode_application_boolean(&apdu[0], false); - break; - case PROP_POLARITY: - apdu_len = encode_application_enumerated(&apdu[0], polarity); - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_UNKNOWN_PROPERTY; - apdu_len = -1; - break; + case PROP_OBJECT_IDENTIFIER: + apdu_len = + encode_application_object_id(&apdu[0], OBJECT_BINARY_INPUT, + object_instance); + break; + case PROP_OBJECT_NAME: + case PROP_DESCRIPTION: + /* note: object name must be unique in our device */ + characterstring_init_ansi(&char_string, + Binary_Input_Name(object_instance)); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_OBJECT_TYPE: + apdu_len = + encode_application_enumerated(&apdu[0], OBJECT_BINARY_INPUT); + break; + case PROP_PRESENT_VALUE: + /* note: you need to look up the actual value */ + apdu_len = + encode_application_enumerated(&apdu[0], + Binary_Input_Present_Value(object_instance)); + break; + case PROP_STATUS_FLAGS: + /* note: see the details in the standard on how to use these */ + bitstring_init(&bit_string); + bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); + apdu_len = encode_application_bitstring(&apdu[0], &bit_string); + break; + case PROP_EVENT_STATE: + /* note: see the details in the standard on how to use this */ + apdu_len = + encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); + break; + case PROP_OUT_OF_SERVICE: + apdu_len = encode_application_boolean(&apdu[0], false); + break; + case PROP_POLARITY: + apdu_len = encode_application_enumerated(&apdu[0], polarity); + break; + default: + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_UNKNOWN_PROPERTY; + apdu_len = -1; + break; } return apdu_len; @@ -232,7 +241,8 @@ int Binary_Input_Encode_Property_APDU(uint8_t * apdu, #include #include "ctest.h" -void testBinaryInput(Test * pTest) +void testBinaryInput( + Test * pTest) { uint8_t apdu[MAX_APDU] = { 0 }; int len = 0; @@ -248,8 +258,7 @@ void testBinaryInput(Test * pTest) /* FIXME: we should do a lot more testing here... */ len = Binary_Input_Encode_Property_APDU(&apdu[0], instance, - PROP_OBJECT_IDENTIFIER, - BACNET_ARRAY_ALL, &error_class, &error_code); + PROP_OBJECT_IDENTIFIER, BACNET_ARRAY_ALL, &error_class, &error_code); ct_test(pTest, len >= 0); len = decode_tag_number_and_value(&apdu[0], &tag_number, &len_value); ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_OBJECT_ID); @@ -262,7 +271,8 @@ void testBinaryInput(Test * pTest) } #ifdef TEST_BINARY_INPUT -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -279,5 +289,5 @@ int main(void) return 0; } -#endif /* TEST_BINARY_INPUT */ -#endif /* TEST */ +#endif /* TEST_BINARY_INPUT */ +#endif /* TEST */ diff --git a/bacnet-stack/demo/object/bo.c b/bacnet-stack/demo/object/bo.c index 676840c1..b33e4211 100644 --- a/bacnet-stack/demo/object/bo.c +++ b/bacnet-stack/demo/object/bo.c @@ -32,7 +32,7 @@ #include "bacdcode.h" #include "bacenum.h" #include "bacapp.h" -#include "config.h" /* the custom stuff */ +#include "config.h" /* the custom stuff */ #include "wp.h" #define MAX_BINARY_OUTPUTS 6 @@ -48,8 +48,7 @@ static BACNET_BINARY_PV static bool Binary_Output_Out_Of_Service[MAX_BINARY_OUTPUTS]; /* These three arrays are used by the ReadPropertyMultiple handler */ -static const int Binary_Output_Properties_Required[] = -{ +static const int Binary_Output_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, PROP_OBJECT_NAME, PROP_OBJECT_TYPE, @@ -63,14 +62,12 @@ static const int Binary_Output_Properties_Required[] = -1 }; -static const int Binary_Output_Properties_Optional[] = -{ +static const int Binary_Output_Properties_Optional[] = { PROP_DESCRIPTION, -1 }; -static const int Binary_Output_Properties_Proprietary[] = -{ +static const int Binary_Output_Properties_Proprietary[] = { -1 }; @@ -89,7 +86,8 @@ void Binary_Output_Property_Lists( return; } -void Binary_Output_Init(void) +void Binary_Output_Init( + void) { unsigned i, j; static bool initialized = false; @@ -111,7 +109,8 @@ void Binary_Output_Init(void) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need validate that the */ /* given instance exists */ -bool Binary_Output_Valid_Instance(uint32_t object_instance) +bool Binary_Output_Valid_Instance( + uint32_t object_instance) { if (object_instance < MAX_BINARY_OUTPUTS) return true; @@ -121,7 +120,8 @@ bool Binary_Output_Valid_Instance(uint32_t object_instance) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then count how many you have */ -unsigned Binary_Output_Count(void) +unsigned Binary_Output_Count( + void) { return MAX_BINARY_OUTPUTS; } @@ -129,7 +129,8 @@ unsigned Binary_Output_Count(void) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need to return the instance */ /* that correlates to the correct index */ -uint32_t Binary_Output_Index_To_Instance(unsigned index) +uint32_t Binary_Output_Index_To_Instance( + unsigned index) { return index; } @@ -137,7 +138,8 @@ uint32_t Binary_Output_Index_To_Instance(unsigned index) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need to return the index */ /* that correlates to the correct instance number */ -unsigned Binary_Output_Instance_To_Index(uint32_t object_instance) +unsigned Binary_Output_Instance_To_Index( + uint32_t object_instance) { unsigned index = MAX_BINARY_OUTPUTS; @@ -147,8 +149,8 @@ unsigned Binary_Output_Instance_To_Index(uint32_t object_instance) return index; } -static BACNET_BINARY_PV Binary_Output_Present_Value(uint32_t - object_instance) +static BACNET_BINARY_PV Binary_Output_Present_Value( + uint32_t object_instance) { BACNET_BINARY_PV value = RELINQUISH_DEFAULT; unsigned index = 0; @@ -169,7 +171,8 @@ static BACNET_BINARY_PV Binary_Output_Present_Value(uint32_t } /* note: the object name must be unique within this device */ -char *Binary_Output_Name(uint32_t object_instance) +char *Binary_Output_Name( + uint32_t object_instance) { static char text_string[32] = ""; /* okay for single thread */ @@ -182,14 +185,16 @@ char *Binary_Output_Name(uint32_t object_instance) } /* return apdu len, or -1 on error */ -int Binary_Output_Encode_Property_APDU(uint8_t * apdu, +int Binary_Output_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { int len = 0; - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ BACNET_BIT_STRING bit_string; BACNET_CHARACTER_STRING char_string; BACNET_BINARY_PV present_value = BINARY_INACTIVE; @@ -200,116 +205,121 @@ int Binary_Output_Encode_Property_APDU(uint8_t * apdu, Binary_Output_Init(); switch (property) { - case PROP_OBJECT_IDENTIFIER: - apdu_len = encode_application_object_id(&apdu[0], OBJECT_BINARY_OUTPUT, - object_instance); - break; - /* note: Name and Description don't have to be the same. - You could make Description writable and different */ - case PROP_OBJECT_NAME: - case PROP_DESCRIPTION: - characterstring_init_ansi(&char_string, - Binary_Output_Name(object_instance)); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_OBJECT_TYPE: - apdu_len = - encode_application_enumerated(&apdu[0], OBJECT_BINARY_OUTPUT); - break; - case PROP_PRESENT_VALUE: - present_value = Binary_Output_Present_Value(object_instance); - apdu_len = encode_application_enumerated(&apdu[0], present_value); - break; - case PROP_STATUS_FLAGS: - /* note: see the details in the standard on how to use these */ - bitstring_init(&bit_string); - bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); - apdu_len = encode_application_bitstring(&apdu[0], &bit_string); - break; - case PROP_EVENT_STATE: - /* note: see the details in the standard on how to use this */ - apdu_len = encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); - break; - case PROP_OUT_OF_SERVICE: - object_index = Binary_Output_Instance_To_Index(object_instance); - state = Binary_Output_Out_Of_Service[object_index]; - apdu_len = encode_application_boolean(&apdu[0], state); - break; - case PROP_POLARITY: - apdu_len = encode_application_enumerated(&apdu[0], polarity); - break; - case PROP_PRIORITY_ARRAY: - /* Array element zero is the number of elements in the array */ - if (array_index == 0) + case PROP_OBJECT_IDENTIFIER: apdu_len = - encode_application_unsigned(&apdu[0], BACNET_MAX_PRIORITY); - /* if no index was specified, then try to encode the entire list */ - /* into one packet. */ - else if (array_index == BACNET_ARRAY_ALL) { - object_index = - Binary_Output_Instance_To_Index(object_instance); - for (i = 0; i < BACNET_MAX_PRIORITY; i++) { - /* FIXME: check if we have room before adding it to APDU */ - if (Binary_Output_Level[object_index][i] == BINARY_NULL) - len = encode_application_null(&apdu[apdu_len]); - else { - present_value = Binary_Output_Level[object_index][i]; - len = - encode_application_enumerated(&apdu[apdu_len], - present_value); - } - /* add it if we have room */ - if ((apdu_len + len) < MAX_APDU) - apdu_len += len; - else { - *error_class = ERROR_CLASS_SERVICES; - *error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT; - apdu_len = -1; - break; - } - } - } else { - object_index = - Binary_Output_Instance_To_Index(object_instance); - if (array_index <= BACNET_MAX_PRIORITY) { - if (Binary_Output_Level[object_index][array_index] == - BINARY_NULL) - len = encode_application_null(&apdu[apdu_len]); - else { - present_value = - Binary_Output_Level[object_index][array_index]; - len = - encode_application_enumerated(&apdu[apdu_len], - present_value); + encode_application_object_id(&apdu[0], OBJECT_BINARY_OUTPUT, + object_instance); + break; + /* note: Name and Description don't have to be the same. + You could make Description writable and different */ + case PROP_OBJECT_NAME: + case PROP_DESCRIPTION: + characterstring_init_ansi(&char_string, + Binary_Output_Name(object_instance)); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_OBJECT_TYPE: + apdu_len = + encode_application_enumerated(&apdu[0], OBJECT_BINARY_OUTPUT); + break; + case PROP_PRESENT_VALUE: + present_value = Binary_Output_Present_Value(object_instance); + apdu_len = encode_application_enumerated(&apdu[0], present_value); + break; + case PROP_STATUS_FLAGS: + /* note: see the details in the standard on how to use these */ + bitstring_init(&bit_string); + bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); + apdu_len = encode_application_bitstring(&apdu[0], &bit_string); + break; + case PROP_EVENT_STATE: + /* note: see the details in the standard on how to use this */ + apdu_len = + encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); + break; + case PROP_OUT_OF_SERVICE: + object_index = Binary_Output_Instance_To_Index(object_instance); + state = Binary_Output_Out_Of_Service[object_index]; + apdu_len = encode_application_boolean(&apdu[0], state); + break; + case PROP_POLARITY: + apdu_len = encode_application_enumerated(&apdu[0], polarity); + break; + case PROP_PRIORITY_ARRAY: + /* Array element zero is the number of elements in the array */ + if (array_index == 0) + apdu_len = + encode_application_unsigned(&apdu[0], BACNET_MAX_PRIORITY); + /* if no index was specified, then try to encode the entire list */ + /* into one packet. */ + else if (array_index == BACNET_ARRAY_ALL) { + object_index = + Binary_Output_Instance_To_Index(object_instance); + for (i = 0; i < BACNET_MAX_PRIORITY; i++) { + /* FIXME: check if we have room before adding it to APDU */ + if (Binary_Output_Level[object_index][i] == BINARY_NULL) + len = encode_application_null(&apdu[apdu_len]); + else { + present_value = Binary_Output_Level[object_index][i]; + len = + encode_application_enumerated(&apdu[apdu_len], + present_value); + } + /* add it if we have room */ + if ((apdu_len + len) < MAX_APDU) + apdu_len += len; + else { + *error_class = ERROR_CLASS_SERVICES; + *error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT; + apdu_len = -1; + break; + } } } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_ARRAY_INDEX; - apdu_len = -1; + object_index = + Binary_Output_Instance_To_Index(object_instance); + if (array_index <= BACNET_MAX_PRIORITY) { + if (Binary_Output_Level[object_index][array_index] == + BINARY_NULL) + len = encode_application_null(&apdu[apdu_len]); + else { + present_value = + Binary_Output_Level[object_index][array_index]; + len = + encode_application_enumerated(&apdu[apdu_len], + present_value); + } + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_ARRAY_INDEX; + apdu_len = -1; + } } - } - break; - case PROP_RELINQUISH_DEFAULT: - present_value = RELINQUISH_DEFAULT; - apdu_len = encode_application_enumerated(&apdu[0], present_value); - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_UNKNOWN_PROPERTY; - apdu_len = -1; - break; + break; + case PROP_RELINQUISH_DEFAULT: + present_value = RELINQUISH_DEFAULT; + apdu_len = encode_application_enumerated(&apdu[0], present_value); + break; + default: + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_UNKNOWN_PROPERTY; + apdu_len = -1; + break; } return apdu_len; } /* returns true if successful */ -bool Binary_Output_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) +bool Binary_Output_Write_Property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { bool status = false; /* return value */ unsigned int object_index = 0; @@ -330,78 +340,78 @@ bool Binary_Output_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, /* FIXME: len < application_data_len: more data? */ /* FIXME: len == 0: unable to decode? */ switch (wp_data->object_property) { - case PROP_PRESENT_VALUE: - if (value.tag == BACNET_APPLICATION_TAG_ENUMERATED) { - priority = wp_data->priority; - /* Command priority 6 is reserved for use by Minimum On/Off - algorithm and may not be used for other purposes in any - object. */ - if (priority && (priority <= BACNET_MAX_PRIORITY) && - (priority != 6 /* reserved */ ) && - (value.type.Enumerated >= MIN_BINARY_PV) && - (value.type.Enumerated <= MAX_BINARY_PV)) { - level = (BACNET_BINARY_PV)value.type.Enumerated; - object_index = - Binary_Output_Instance_To_Index(wp_data-> - object_instance); - priority--; - Binary_Output_Level[object_index][priority] = level; - /* Note: you could set the physical output here if we - are the highest priority. - However, if Out of Service is TRUE, then don't set the - physical output. This comment may apply to the - main loop (i.e. check out of service before changing output) */ - status = true; - } else if (priority == 6) { + case PROP_PRESENT_VALUE: + if (value.tag == BACNET_APPLICATION_TAG_ENUMERATED) { + priority = wp_data->priority; /* Command priority 6 is reserved for use by Minimum On/Off algorithm and may not be used for other purposes in any object. */ - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + if (priority && (priority <= BACNET_MAX_PRIORITY) && + (priority != 6 /* reserved */ ) && + (value.type.Enumerated >= MIN_BINARY_PV) && + (value.type.Enumerated <= MAX_BINARY_PV)) { + level = (BACNET_BINARY_PV) value.type.Enumerated; + object_index = + Binary_Output_Instance_To_Index(wp_data-> + object_instance); + priority--; + Binary_Output_Level[object_index][priority] = level; + /* Note: you could set the physical output here if we + are the highest priority. + However, if Out of Service is TRUE, then don't set the + physical output. This comment may apply to the + main loop (i.e. check out of service before changing output) */ + status = true; + } else if (priority == 6) { + /* Command priority 6 is reserved for use by Minimum On/Off + algorithm and may not be used for other purposes in any + object. */ + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } + } else if (value.tag == BACNET_APPLICATION_TAG_NULL) { + level = BINARY_NULL; + object_index = + Binary_Output_Instance_To_Index(wp_data->object_instance); + priority = wp_data->priority; + if (priority && (priority <= BACNET_MAX_PRIORITY)) { + priority--; + Binary_Output_Level[object_index][priority] = level; + /* Note: you could set the physical output here to the next + highest priority, or to the relinquish default if no + priorities are set. + However, if Out of Service is TRUE, then don't set the + physical output. This comment may apply to the + main loop (i.e. check out of service before changing output) */ + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } - } else if (value.tag == BACNET_APPLICATION_TAG_NULL) { - level = BINARY_NULL; - object_index = - Binary_Output_Instance_To_Index(wp_data->object_instance); - priority = wp_data->priority; - if (priority && (priority <= BACNET_MAX_PRIORITY)) { - priority--; - Binary_Output_Level[object_index][priority] = level; - /* Note: you could set the physical output here to the next - highest priority, or to the relinquish default if no - priorities are set. - However, if Out of Service is TRUE, then don't set the - physical output. This comment may apply to the - main loop (i.e. check out of service before changing output) */ + break; + case PROP_OUT_OF_SERVICE: + if (value.tag == BACNET_APPLICATION_TAG_BOOLEAN) { + object_index = + Binary_Output_Instance_To_Index(wp_data->object_instance); + Binary_Output_Out_Of_Service[object_index] = + value.type.Boolean; status = true; } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } - } else { + break; + default: *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case PROP_OUT_OF_SERVICE: - if (value.tag == BACNET_APPLICATION_TAG_BOOLEAN) { - object_index = - Binary_Output_Instance_To_Index(wp_data->object_instance); - Binary_Output_Out_Of_Service[object_index] = - value.type.Boolean; - status = true; - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; - break; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + break; } return status; @@ -413,7 +423,8 @@ bool Binary_Output_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, #include #include "ctest.h" -void testBinaryOutput(Test * pTest) +void testBinaryOutput( + Test * pTest) { uint8_t apdu[MAX_APDU] = { 0 }; int len = 0; @@ -428,8 +439,7 @@ void testBinaryOutput(Test * pTest) len = Binary_Output_Encode_Property_APDU(&apdu[0], instance, - PROP_OBJECT_IDENTIFIER, - BACNET_ARRAY_ALL, &error_class, &error_code); + PROP_OBJECT_IDENTIFIER, BACNET_ARRAY_ALL, &error_class, &error_code); ct_test(pTest, len != 0); len = decode_tag_number_and_value(&apdu[0], &tag_number, &len_value); ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_OBJECT_ID); @@ -442,7 +452,8 @@ void testBinaryOutput(Test * pTest) } #ifdef TEST_BINARY_OUTPUT -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -459,5 +470,5 @@ int main(void) return 0; } -#endif /* TEST_BINARY_INPUT */ -#endif /* TEST */ +#endif /* TEST_BINARY_INPUT */ +#endif /* TEST */ diff --git a/bacnet-stack/demo/object/bv.c b/bacnet-stack/demo/object/bv.c index 629533a2..c91c25d4 100644 --- a/bacnet-stack/demo/object/bv.c +++ b/bacnet-stack/demo/object/bv.c @@ -32,7 +32,7 @@ #include "bacdcode.h" #include "bacenum.h" #include "bacapp.h" -#include "config.h" /* the custom stuff */ +#include "config.h" /* the custom stuff */ #include "wp.h" #define MAX_BINARY_VALUES 2 @@ -48,8 +48,7 @@ static BACNET_BINARY_PV static bool Binary_Value_Out_Of_Service[MAX_BINARY_VALUES]; /* These three arrays are used by the ReadPropertyMultiple handler */ -static const int Binary_Value_Properties_Required[] = -{ +static const int Binary_Value_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, PROP_OBJECT_NAME, PROP_OBJECT_TYPE, @@ -60,16 +59,14 @@ static const int Binary_Value_Properties_Required[] = -1 }; -static const int Binary_Value_Properties_Optional[] = -{ +static const int Binary_Value_Properties_Optional[] = { PROP_DESCRIPTION, PROP_PRIORITY_ARRAY, PROP_RELINQUISH_DEFAULT, -1 }; -static const int Binary_Value_Properties_Proprietary[] = -{ +static const int Binary_Value_Properties_Proprietary[] = { -1 }; @@ -88,7 +85,8 @@ void Binary_Value_Property_Lists( return; } -void Binary_Value_Init(void) +void Binary_Value_Init( + void) { unsigned i, j; static bool initialized = false; @@ -110,7 +108,8 @@ void Binary_Value_Init(void) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need validate that the */ /* given instance exists */ -bool Binary_Value_Valid_Instance(uint32_t object_instance) +bool Binary_Value_Valid_Instance( + uint32_t object_instance) { if (object_instance < MAX_BINARY_VALUES) return true; @@ -120,7 +119,8 @@ bool Binary_Value_Valid_Instance(uint32_t object_instance) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then count how many you have */ -unsigned Binary_Value_Count(void) +unsigned Binary_Value_Count( + void) { return MAX_BINARY_VALUES; } @@ -128,7 +128,8 @@ unsigned Binary_Value_Count(void) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need to return the instance */ /* that correlates to the correct index */ -uint32_t Binary_Value_Index_To_Instance(unsigned index) +uint32_t Binary_Value_Index_To_Instance( + unsigned index) { return index; } @@ -136,7 +137,8 @@ uint32_t Binary_Value_Index_To_Instance(unsigned index) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need to return the index */ /* that correlates to the correct instance number */ -unsigned Binary_Value_Instance_To_Index(uint32_t object_instance) +unsigned Binary_Value_Instance_To_Index( + uint32_t object_instance) { unsigned index = MAX_BINARY_VALUES; @@ -146,8 +148,8 @@ unsigned Binary_Value_Instance_To_Index(uint32_t object_instance) return index; } -static BACNET_BINARY_PV Binary_Value_Present_Value(uint32_t - object_instance) +static BACNET_BINARY_PV Binary_Value_Present_Value( + uint32_t object_instance) { BACNET_BINARY_PV value = RELINQUISH_DEFAULT; unsigned index = 0; @@ -168,7 +170,8 @@ static BACNET_BINARY_PV Binary_Value_Present_Value(uint32_t } /* note: the object name must be unique within this device */ -char *Binary_Value_Name(uint32_t object_instance) +char *Binary_Value_Name( + uint32_t object_instance) { static char text_string[32] = ""; /* okay for single thread */ @@ -181,14 +184,16 @@ char *Binary_Value_Name(uint32_t object_instance) } /* return apdu len, or -1 on error */ -int Binary_Value_Encode_Property_APDU(uint8_t * apdu, +int Binary_Value_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { int len = 0; - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ BACNET_BIT_STRING bit_string; BACNET_CHARACTER_STRING char_string; BACNET_BINARY_PV present_value = BINARY_INACTIVE; @@ -198,110 +203,116 @@ int Binary_Value_Encode_Property_APDU(uint8_t * apdu, Binary_Value_Init(); switch (property) { - case PROP_OBJECT_IDENTIFIER: - apdu_len = encode_application_object_id(&apdu[0], OBJECT_BINARY_VALUE, - object_instance); - break; - /* note: Name and Description don't have to be the same. - You could make Description writable and different */ - case PROP_OBJECT_NAME: - case PROP_DESCRIPTION: - characterstring_init_ansi(&char_string, - Binary_Value_Name(object_instance)); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_OBJECT_TYPE: - apdu_len = encode_application_enumerated(&apdu[0], OBJECT_BINARY_VALUE); - break; - case PROP_PRESENT_VALUE: - present_value = Binary_Value_Present_Value(object_instance); - apdu_len = encode_application_enumerated(&apdu[0], present_value); - break; - case PROP_STATUS_FLAGS: - /* note: see the details in the standard on how to use these */ - bitstring_init(&bit_string); - bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); - apdu_len = encode_application_bitstring(&apdu[0], &bit_string); - break; - case PROP_EVENT_STATE: - /* note: see the details in the standard on how to use this */ - apdu_len = encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); - break; - case PROP_OUT_OF_SERVICE: - object_index = Binary_Value_Instance_To_Index(object_instance); - state = Binary_Value_Out_Of_Service[object_index]; - apdu_len = encode_application_boolean(&apdu[0], state); - break; - case PROP_PRIORITY_ARRAY: - /* Array element zero is the number of elements in the array */ - if (array_index == 0) + case PROP_OBJECT_IDENTIFIER: apdu_len = - encode_application_unsigned(&apdu[0], BACNET_MAX_PRIORITY); - /* if no index was specified, then try to encode the entire list */ - /* into one packet. */ - else if (array_index == BACNET_ARRAY_ALL) { + encode_application_object_id(&apdu[0], OBJECT_BINARY_VALUE, + object_instance); + break; + /* note: Name and Description don't have to be the same. + You could make Description writable and different */ + case PROP_OBJECT_NAME: + case PROP_DESCRIPTION: + characterstring_init_ansi(&char_string, + Binary_Value_Name(object_instance)); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_OBJECT_TYPE: + apdu_len = + encode_application_enumerated(&apdu[0], OBJECT_BINARY_VALUE); + break; + case PROP_PRESENT_VALUE: + present_value = Binary_Value_Present_Value(object_instance); + apdu_len = encode_application_enumerated(&apdu[0], present_value); + break; + case PROP_STATUS_FLAGS: + /* note: see the details in the standard on how to use these */ + bitstring_init(&bit_string); + bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); + apdu_len = encode_application_bitstring(&apdu[0], &bit_string); + break; + case PROP_EVENT_STATE: + /* note: see the details in the standard on how to use this */ + apdu_len = + encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); + break; + case PROP_OUT_OF_SERVICE: object_index = Binary_Value_Instance_To_Index(object_instance); - for (i = 0; i < BACNET_MAX_PRIORITY; i++) { - /* FIXME: check if we have room before adding it to APDU */ - if (Binary_Value_Level[object_index][i] == BINARY_NULL) - len = encode_application_null(&apdu[apdu_len]); - else { - present_value = Binary_Value_Level[object_index][i]; - len = - encode_application_enumerated(&apdu[apdu_len], - present_value); - } - /* add it if we have room */ - if ((apdu_len + len) < MAX_APDU) - apdu_len += len; - else { - *error_class = ERROR_CLASS_SERVICES; - *error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT; - apdu_len = -1; - break; - } - } - } else { - object_index = Binary_Value_Instance_To_Index(object_instance); - if (array_index <= BACNET_MAX_PRIORITY) { - if (Binary_Value_Level[object_index][array_index] == - BINARY_NULL) - len = encode_application_null(&apdu[apdu_len]); - else { - present_value = - Binary_Value_Level[object_index][array_index]; - len = - encode_application_enumerated(&apdu[apdu_len], - present_value); + state = Binary_Value_Out_Of_Service[object_index]; + apdu_len = encode_application_boolean(&apdu[0], state); + break; + case PROP_PRIORITY_ARRAY: + /* Array element zero is the number of elements in the array */ + if (array_index == 0) + apdu_len = + encode_application_unsigned(&apdu[0], BACNET_MAX_PRIORITY); + /* if no index was specified, then try to encode the entire list */ + /* into one packet. */ + else if (array_index == BACNET_ARRAY_ALL) { + object_index = Binary_Value_Instance_To_Index(object_instance); + for (i = 0; i < BACNET_MAX_PRIORITY; i++) { + /* FIXME: check if we have room before adding it to APDU */ + if (Binary_Value_Level[object_index][i] == BINARY_NULL) + len = encode_application_null(&apdu[apdu_len]); + else { + present_value = Binary_Value_Level[object_index][i]; + len = + encode_application_enumerated(&apdu[apdu_len], + present_value); + } + /* add it if we have room */ + if ((apdu_len + len) < MAX_APDU) + apdu_len += len; + else { + *error_class = ERROR_CLASS_SERVICES; + *error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT; + apdu_len = -1; + break; + } } } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_ARRAY_INDEX; - apdu_len = -1; + object_index = Binary_Value_Instance_To_Index(object_instance); + if (array_index <= BACNET_MAX_PRIORITY) { + if (Binary_Value_Level[object_index][array_index] == + BINARY_NULL) + len = encode_application_null(&apdu[apdu_len]); + else { + present_value = + Binary_Value_Level[object_index][array_index]; + len = + encode_application_enumerated(&apdu[apdu_len], + present_value); + } + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_ARRAY_INDEX; + apdu_len = -1; + } } - } - break; - case PROP_RELINQUISH_DEFAULT: - present_value = RELINQUISH_DEFAULT; - apdu_len = encode_application_enumerated(&apdu[0], present_value); - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_UNKNOWN_PROPERTY; - apdu_len = -1; - break; + break; + case PROP_RELINQUISH_DEFAULT: + present_value = RELINQUISH_DEFAULT; + apdu_len = encode_application_enumerated(&apdu[0], present_value); + break; + default: + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_UNKNOWN_PROPERTY; + apdu_len = -1; + break; } return apdu_len; } /* returns true if successful */ -bool Binary_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) +bool Binary_Value_Write_Property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { bool status = false; /* return value */ unsigned int object_index = 0; @@ -322,77 +333,77 @@ bool Binary_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, /* FIXME: len < application_data_len: more data? */ /* FIXME: len == 0: unable to decode? */ switch (wp_data->object_property) { - case PROP_PRESENT_VALUE: - if (value.tag == BACNET_APPLICATION_TAG_ENUMERATED) { - priority = wp_data->priority; - /* Command priority 6 is reserved for use by Minimum On/Off - algorithm and may not be used for other purposes in any - object. */ - if (priority && (priority <= BACNET_MAX_PRIORITY) && - (priority != 6 /* reserved */ ) && - (value.type.Enumerated >= MIN_BINARY_PV) && - (value.type.Enumerated <= MAX_BINARY_PV)) { - level = (BACNET_BINARY_PV)value.type.Enumerated; - object_index = - Binary_Value_Instance_To_Index(wp_data-> - object_instance); - priority--; - Binary_Value_Level[object_index][priority] = level; - /* Note: you could set the physical output here if we - are the highest priority. - However, if Out of Service is TRUE, then don't set the - physical output. This comment may apply to the - main loop (i.e. check out of service before changing output) */ - status = true; - } else if (priority == 6) { + case PROP_PRESENT_VALUE: + if (value.tag == BACNET_APPLICATION_TAG_ENUMERATED) { + priority = wp_data->priority; /* Command priority 6 is reserved for use by Minimum On/Off algorithm and may not be used for other purposes in any object. */ - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + if (priority && (priority <= BACNET_MAX_PRIORITY) && + (priority != 6 /* reserved */ ) && + (value.type.Enumerated >= MIN_BINARY_PV) && + (value.type.Enumerated <= MAX_BINARY_PV)) { + level = (BACNET_BINARY_PV) value.type.Enumerated; + object_index = + Binary_Value_Instance_To_Index(wp_data-> + object_instance); + priority--; + Binary_Value_Level[object_index][priority] = level; + /* Note: you could set the physical output here if we + are the highest priority. + However, if Out of Service is TRUE, then don't set the + physical output. This comment may apply to the + main loop (i.e. check out of service before changing output) */ + status = true; + } else if (priority == 6) { + /* Command priority 6 is reserved for use by Minimum On/Off + algorithm and may not be used for other purposes in any + object. */ + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } + } else if (value.tag == BACNET_APPLICATION_TAG_NULL) { + level = BINARY_NULL; + object_index = + Binary_Value_Instance_To_Index(wp_data->object_instance); + priority = wp_data->priority; + if (priority && (priority <= BACNET_MAX_PRIORITY)) { + priority--; + Binary_Value_Level[object_index][priority] = level; + /* Note: you could set the physical output here to the next + highest priority, or to the relinquish default if no + priorities are set. + However, if Out of Service is TRUE, then don't set the + physical output. This comment may apply to the + main loop (i.e. check out of service before changing output) */ + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } - } else if (value.tag == BACNET_APPLICATION_TAG_NULL) { - level = BINARY_NULL; - object_index = - Binary_Value_Instance_To_Index(wp_data->object_instance); - priority = wp_data->priority; - if (priority && (priority <= BACNET_MAX_PRIORITY)) { - priority--; - Binary_Value_Level[object_index][priority] = level; - /* Note: you could set the physical output here to the next - highest priority, or to the relinquish default if no - priorities are set. - However, if Out of Service is TRUE, then don't set the - physical output. This comment may apply to the - main loop (i.e. check out of service before changing output) */ + break; + case PROP_OUT_OF_SERVICE: + if (value.tag == BACNET_APPLICATION_TAG_BOOLEAN) { + object_index = + Binary_Value_Instance_To_Index(wp_data->object_instance); + Binary_Value_Out_Of_Service[object_index] = value.type.Boolean; status = true; } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } - } else { + break; + default: *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case PROP_OUT_OF_SERVICE: - if (value.tag == BACNET_APPLICATION_TAG_BOOLEAN) { - object_index = - Binary_Value_Instance_To_Index(wp_data->object_instance); - Binary_Value_Out_Of_Service[object_index] = value.type.Boolean; - status = true; - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; - break; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + break; } return status; @@ -404,7 +415,8 @@ bool Binary_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, #include #include "ctest.h" -void testBinary_Value(Test * pTest) +void testBinary_Value( + Test * pTest) { uint8_t apdu[MAX_APDU] = { 0 }; int len = 0; @@ -419,8 +431,7 @@ void testBinary_Value(Test * pTest) len = Binary_Value_Encode_Property_APDU(&apdu[0], instance, - PROP_OBJECT_IDENTIFIER, - BACNET_ARRAY_ALL, &error_class, &error_code); + PROP_OBJECT_IDENTIFIER, BACNET_ARRAY_ALL, &error_class, &error_code); ct_test(pTest, len != 0); len = decode_tag_number_and_value(&apdu[0], &tag_number, &len_value); ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_OBJECT_ID); @@ -433,7 +444,8 @@ void testBinary_Value(Test * pTest) } #ifdef TEST_BINARY_VALUE -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -450,5 +462,5 @@ int main(void) return 0; } -#endif /* TEST_BINARY_VALUE */ -#endif /* TEST */ +#endif /* TEST_BINARY_VALUE */ +#endif /* TEST */ diff --git a/bacnet-stack/demo/object/device.c b/bacnet-stack/demo/object/device.c index 1f1824e6..5a63db9e 100644 --- a/bacnet-stack/demo/object/device.c +++ b/bacnet-stack/demo/object/device.c @@ -25,33 +25,32 @@ #include #include -#include /* for memmove */ +#include /* for memmove */ #include "bacdef.h" #include "bacdcode.h" #include "bacenum.h" #include "bacapp.h" -#include "config.h" /* the custom stuff */ +#include "config.h" /* the custom stuff */ #include "apdu.h" -#include "ai.h" /* object list dependency */ -#include "ao.h" /* object list dependency */ -#include "av.h" /* object list dependency */ -#include "bi.h" /* object list dependency */ -#include "bo.h" /* object list dependency */ -#include "bv.h" /* object list dependency */ -#include "lc.h" /* object list dependency */ -#include "lsp.h" /* object list dependency */ -#include "mso.h" /* object list dependency */ -#include "wp.h" /* write property handling */ +#include "ai.h" /* object list dependency */ +#include "ao.h" /* object list dependency */ +#include "av.h" /* object list dependency */ +#include "bi.h" /* object list dependency */ +#include "bo.h" /* object list dependency */ +#include "bv.h" /* object list dependency */ +#include "lc.h" /* object list dependency */ +#include "lsp.h" /* object list dependency */ +#include "mso.h" /* object list dependency */ +#include "wp.h" /* write property handling */ #include "version.h" -#include "device.h" /* me */ +#include "device.h" /* me */ #include "datalink.h" #if defined(BACFILE) - #include "bacfile.h" /* object list dependency */ +#include "bacfile.h" /* object list dependency */ #endif /* These three arrays are used by the ReadPropertyMultiple handler */ -static const int Device_Properties_Required[] = -{ +static const int Device_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, PROP_OBJECT_NAME, PROP_OBJECT_TYPE, @@ -80,8 +79,7 @@ static const int Device_Properties_Required[] = -1 }; -static const int Device_Properties_Optional[] = -{ +static const int Device_Properties_Optional[] = { PROP_DESCRIPTION, PROP_LOCAL_TIME, PROP_UTC_OFFSET, @@ -91,8 +89,7 @@ static const int Device_Properties_Optional[] = -1 }; -static const int Device_Properties_Proprietary[] = -{ +static const int Device_Properties_Proprietary[] = { -1 }; @@ -134,8 +131,8 @@ static char Description[16] = "server"; /* static uint8_t Max_Segments_Accepted = 0; */ /* VT_Classes_Supported */ /* Active_VT_Sessions */ -BACNET_TIME Local_Time; /* rely on OS, if there is one */ -BACNET_DATE Local_Date; /* rely on OS, if there is one */ +BACNET_TIME Local_Time; /* rely on OS, if there is one */ +BACNET_DATE Local_Date; /* rely on OS, if there is one */ /* BACnet UTC is inverse of standard offset - i.e. relative to local */ static int UTC_Offset = 5; static bool Daylight_Savings_Status = false; /* rely on OS */ @@ -159,14 +156,16 @@ static uint8_t Database_Revision = 0; /* Profile_Name */ /* methods to manipulate the data */ -uint32_t Device_Object_Instance_Number(void) +uint32_t Device_Object_Instance_Number( + void) { return Object_Instance_Number; } -bool Device_Set_Object_Instance_Number(uint32_t object_id) +bool Device_Set_Object_Instance_Number( + uint32_t object_id) { - bool status = true; /* return value */ + bool status = true; /* return value */ if (object_id <= BACNET_MAX_INSTANCE) Object_Instance_Number = object_id; @@ -176,19 +175,23 @@ bool Device_Set_Object_Instance_Number(uint32_t object_id) return status; } -bool Device_Valid_Object_Instance_Number(uint32_t object_id) +bool Device_Valid_Object_Instance_Number( + uint32_t object_id) { /* BACnet allows for a wildcard instance number */ return ((Object_Instance_Number == object_id) || (object_id == BACNET_MAX_INSTANCE)); } -const char *Device_Object_Name(void) +const char *Device_Object_Name( + void) { return Object_Name; } -bool Device_Set_Object_Name(const char *name, size_t length) +bool Device_Set_Object_Name( + const char *name, + size_t length) { bool status = false; /*return value */ @@ -204,38 +207,46 @@ bool Device_Set_Object_Name(const char *name, size_t length) return status; } -BACNET_DEVICE_STATUS Device_System_Status(void) +BACNET_DEVICE_STATUS Device_System_Status( + void) { return System_Status; } -void Device_Set_System_Status(BACNET_DEVICE_STATUS status) +void Device_Set_System_Status( + BACNET_DEVICE_STATUS status) { /* FIXME: bounds check? */ System_Status = status; } -const char *Device_Vendor_Name(void) +const char *Device_Vendor_Name( + void) { return Vendor_Name; } -uint16_t Device_Vendor_Identifier(void) +uint16_t Device_Vendor_Identifier( + void) { return Vendor_Identifier; } -void Device_Set_Vendor_Identifier(uint16_t vendor_id) +void Device_Set_Vendor_Identifier( + uint16_t vendor_id) { Vendor_Identifier = vendor_id; } -const char *Device_Model_Name(void) +const char *Device_Model_Name( + void) { return Model_Name; } -bool Device_Set_Model_Name(const char *name, size_t length) +bool Device_Set_Model_Name( + const char *name, + size_t length) { bool status = false; /*return value */ @@ -248,17 +259,20 @@ bool Device_Set_Model_Name(const char *name, size_t length) return status; } -const char *Device_Firmware_Revision(void) +const char *Device_Firmware_Revision( + void) { return BACnet_Version; } -const char *Device_Application_Software_Version(void) +const char *Device_Application_Software_Version( + void) { return Application_Software_Version; } -bool Device_Set_Application_Software_Version(const char *name, +bool Device_Set_Application_Software_Version( + const char *name, size_t length) { bool status = false; /*return value */ @@ -272,12 +286,15 @@ bool Device_Set_Application_Software_Version(const char *name, return status; } -const char *Device_Description(void) +const char *Device_Description( + void) { return Description; } -bool Device_Set_Description(const char *name, size_t length) +bool Device_Set_Description( + const char *name, + size_t length) { bool status = false; /*return value */ @@ -290,12 +307,15 @@ bool Device_Set_Description(const char *name, size_t length) return status; } -const char *Device_Location(void) +const char *Device_Location( + void) { return Location; } -bool Device_Set_Location(const char *name, size_t length) +bool Device_Set_Location( + const char *name, + size_t length) { bool status = false; /*return value */ @@ -308,60 +328,71 @@ bool Device_Set_Location(const char *name, size_t length) return status; } -uint8_t Device_Protocol_Version(void) +uint8_t Device_Protocol_Version( + void) { return BACNET_PROTOCOL_VERSION; } -uint8_t Device_Protocol_Revision(void) +uint8_t Device_Protocol_Revision( + void) { return BACNET_PROTOCOL_REVISION; } -uint16_t Device_Max_APDU_Length_Accepted(void) +uint16_t Device_Max_APDU_Length_Accepted( + void) { return MAX_APDU; } -BACNET_SEGMENTATION Device_Segmentation_Supported(void) +BACNET_SEGMENTATION Device_Segmentation_Supported( + void) { return SEGMENTATION_NONE; } -uint16_t Device_APDU_Timeout(void) +uint16_t Device_APDU_Timeout( + void) { return APDU_Timeout; } /* in milliseconds */ -void Device_Set_APDU_Timeout(uint16_t timeout) +void Device_Set_APDU_Timeout( + uint16_t timeout) { APDU_Timeout = timeout; } -uint8_t Device_Number_Of_APDU_Retries(void) +uint8_t Device_Number_Of_APDU_Retries( + void) { return Number_Of_APDU_Retries; } -void Device_Set_Number_Of_APDU_Retries(uint8_t retries) +void Device_Set_Number_Of_APDU_Retries( + uint8_t retries) { Number_Of_APDU_Retries = retries; } -uint8_t Device_Database_Revision(void) +uint8_t Device_Database_Revision( + void) { return Database_Revision; } -void Device_Set_Database_Revision(uint8_t revision) +void Device_Set_Database_Revision( + uint8_t revision) { Database_Revision = revision; } /* Since many network clients depend on the object list */ /* for discovery, it must be consistent! */ -unsigned Device_Object_List_Count(void) +unsigned Device_Object_List_Count( + void) { unsigned count = 1; @@ -381,8 +412,10 @@ unsigned Device_Object_List_Count(void) return count; } -bool Device_Object_List_Identifier(unsigned array_index, - int *object_type, uint32_t * instance) +bool Device_Object_List_Identifier( + unsigned array_index, + int *object_type, + uint32_t * instance) { bool status = false; unsigned object_index = 0; @@ -528,8 +561,10 @@ bool Device_Object_List_Identifier(unsigned array_index, return status; } -bool Device_Valid_Object_Name(const char *object_name, - int *object_type, uint32_t * object_instance) +bool Device_Valid_Object_Name( + const char *object_name, + int *object_type, + uint32_t * object_instance) { bool found = false; int type = 0; @@ -558,49 +593,51 @@ bool Device_Valid_Object_Name(const char *object_name, } /* returns the name or NULL if not found */ -char *Device_Valid_Object_Id(int object_type, uint32_t object_instance) +char *Device_Valid_Object_Id( + int object_type, + uint32_t object_instance) { - char *name = NULL; /* return value */ + char *name = NULL; /* return value */ switch (object_type) { - case OBJECT_ANALOG_INPUT: - name = Analog_Input_Name(object_instance); - break; - case OBJECT_ANALOG_OUTPUT: - name = Analog_Output_Name(object_instance); - break; - case OBJECT_ANALOG_VALUE: - name = Analog_Value_Name(object_instance); - break; - case OBJECT_BINARY_INPUT: - name = Binary_Input_Name(object_instance); - break; - case OBJECT_BINARY_OUTPUT: - name = Binary_Output_Name(object_instance); - break; - case OBJECT_BINARY_VALUE: - name = Binary_Value_Name(object_instance); - break; - case OBJECT_LIFE_SAFETY_POINT: - name = Life_Safety_Point_Name(object_instance); - break; - case OBJECT_LOAD_CONTROL: - name = Load_Control_Name(object_instance); - break; - case OBJECT_MULTI_STATE_OUTPUT: - name = Multistate_Output_Name(object_instance); - break; + case OBJECT_ANALOG_INPUT: + name = Analog_Input_Name(object_instance); + break; + case OBJECT_ANALOG_OUTPUT: + name = Analog_Output_Name(object_instance); + break; + case OBJECT_ANALOG_VALUE: + name = Analog_Value_Name(object_instance); + break; + case OBJECT_BINARY_INPUT: + name = Binary_Input_Name(object_instance); + break; + case OBJECT_BINARY_OUTPUT: + name = Binary_Output_Name(object_instance); + break; + case OBJECT_BINARY_VALUE: + name = Binary_Value_Name(object_instance); + break; + case OBJECT_LIFE_SAFETY_POINT: + name = Life_Safety_Point_Name(object_instance); + break; + case OBJECT_LOAD_CONTROL: + name = Load_Control_Name(object_instance); + break; + case OBJECT_MULTI_STATE_OUTPUT: + name = Multistate_Output_Name(object_instance); + break; #if defined(BACFILE) - case OBJECT_FILE: - name = bacfile_name(object_instance); - break; + case OBJECT_FILE: + name = bacfile_name(object_instance); + break; #endif - case OBJECT_DEVICE: - if (object_instance == Object_Instance_Number) - name = Object_Name; - break; - default: - break; + case OBJECT_DEVICE: + if (object_instance == Object_Instance_Number) + name = Object_Name; + break; + default: + break; } return name; @@ -608,13 +645,15 @@ char *Device_Valid_Object_Id(int object_type, uint32_t object_instance) /* return the length of the apdu encoded or -1 for error or -2 for abort message */ -int Device_Encode_Property_APDU(uint8_t * apdu, +int Device_Encode_Property_APDU( + uint8_t * apdu, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { - int apdu_len = 0; /* return value */ - int len = 0; /* apdu len intermediate value */ + int apdu_len = 0; /* return value */ + int len = 0; /* apdu len intermediate value */ BACNET_BIT_STRING bit_string; BACNET_CHARACTER_STRING char_string; unsigned i = 0; @@ -623,216 +662,230 @@ int Device_Encode_Property_APDU(uint8_t * apdu, unsigned count = 0; switch (property) { - case PROP_OBJECT_IDENTIFIER: - apdu_len = encode_application_object_id(&apdu[0], OBJECT_DEVICE, - Object_Instance_Number); - break; - case PROP_OBJECT_NAME: - characterstring_init_ansi(&char_string, Object_Name); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_OBJECT_TYPE: - apdu_len = encode_application_enumerated(&apdu[0], OBJECT_DEVICE); - break; - case PROP_DESCRIPTION: - characterstring_init_ansi(&char_string, Description); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_SYSTEM_STATUS: - apdu_len = encode_application_enumerated(&apdu[0], System_Status); - break; - case PROP_VENDOR_NAME: - characterstring_init_ansi(&char_string, Vendor_Name); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_VENDOR_IDENTIFIER: - apdu_len = encode_application_unsigned(&apdu[0], Vendor_Identifier); - break; - case PROP_MODEL_NAME: - characterstring_init_ansi(&char_string, Model_Name); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_FIRMWARE_REVISION: - characterstring_init_ansi(&char_string, BACnet_Version); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_APPLICATION_SOFTWARE_VERSION: - characterstring_init_ansi(&char_string, - Application_Software_Version); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - /* FIXME: if you support time */ - case PROP_LOCAL_TIME: - /* FIXME: get the actual value */ - Local_Time.hour = 7; - Local_Time.min = 0; - Local_Time.sec = 3; - Local_Time.hundredths = 1; - apdu_len = encode_application_time(&apdu[0], &Local_Time); - break; - /* FIXME: if you support time */ - case PROP_UTC_OFFSET: - /* NOTE: if your UTC offset is -5, then BACnet UTC offset is 5 */ - apdu_len = encode_application_signed(&apdu[0], UTC_Offset); - break; - /* FIXME: if you support date */ - case PROP_LOCAL_DATE: - /* FIXME: get the actual value instead of April Fool's Day */ - Local_Date.year = 2006; /* AD */ - Local_Date.month = 4; /* 1=Jan */ - Local_Date.day = 1; /* 1..31 */ - Local_Date.wday = 6; /* 1=Monday */ - apdu_len = encode_application_date(&apdu[0], &Local_Date); - break; - case PROP_DAYLIGHT_SAVINGS_STATUS: - apdu_len = - encode_application_boolean(&apdu[0], Daylight_Savings_Status); - break; - case PROP_PROTOCOL_VERSION: - apdu_len = - encode_application_unsigned(&apdu[0], Device_Protocol_Version()); - break; - case PROP_PROTOCOL_REVISION: - apdu_len = - encode_application_unsigned(&apdu[0], Device_Protocol_Revision()); - break; - /* BACnet Legacy Support */ - case PROP_PROTOCOL_CONFORMANCE_CLASS: - apdu_len = encode_application_unsigned(&apdu[0], 1); - break; - case PROP_PROTOCOL_SERVICES_SUPPORTED: - /* Note: list of services that are executed, not initiated. */ - bitstring_init(&bit_string); - for (i = 0; i < MAX_BACNET_SERVICES_SUPPORTED; i++) { - /* automatic lookup based on handlers set */ - bitstring_set_bit(&bit_string, (uint8_t) i, - apdu_service_supported((BACNET_SERVICES_SUPPORTED)i)); - } - apdu_len = encode_application_bitstring(&apdu[0], &bit_string); - break; - case PROP_PROTOCOL_OBJECT_TYPES_SUPPORTED: - /* Note: this is the list of objects that can be in this device, - not a list of objects that this device can access */ - bitstring_init(&bit_string); - for (i = 0; i < MAX_ASHRAE_OBJECT_TYPE; i++) { - /* initialize all the object types to not-supported */ - bitstring_set_bit(&bit_string, (uint8_t) i, false); - } - /* FIXME: indicate the objects that YOU support */ - bitstring_set_bit(&bit_string, OBJECT_DEVICE, true); - if (Analog_Input_Count()) - bitstring_set_bit(&bit_string, OBJECT_ANALOG_INPUT, true); - if (Analog_Output_Count()) - bitstring_set_bit(&bit_string, OBJECT_ANALOG_OUTPUT, true); - if (Analog_Value_Count()) - bitstring_set_bit(&bit_string, OBJECT_ANALOG_VALUE, true); - if (Binary_Input_Count()) - bitstring_set_bit(&bit_string, OBJECT_BINARY_INPUT, true); - if (Binary_Output_Count()) - bitstring_set_bit(&bit_string, OBJECT_BINARY_OUTPUT, true); - if (Binary_Value_Count()) - bitstring_set_bit(&bit_string, OBJECT_BINARY_VALUE, true); - if (Life_Safety_Point_Count()) - bitstring_set_bit(&bit_string, OBJECT_LIFE_SAFETY_POINT, true); - if (Load_Control_Count()) - bitstring_set_bit(&bit_string, OBJECT_LOAD_CONTROL, true); - if (Multistate_Output_Count()) - bitstring_set_bit(&bit_string, OBJECT_MULTI_STATE_OUTPUT, - true); + case PROP_OBJECT_IDENTIFIER: + apdu_len = encode_application_object_id(&apdu[0], OBJECT_DEVICE, + Object_Instance_Number); + break; + case PROP_OBJECT_NAME: + characterstring_init_ansi(&char_string, Object_Name); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_OBJECT_TYPE: + apdu_len = encode_application_enumerated(&apdu[0], OBJECT_DEVICE); + break; + case PROP_DESCRIPTION: + characterstring_init_ansi(&char_string, Description); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_SYSTEM_STATUS: + apdu_len = encode_application_enumerated(&apdu[0], System_Status); + break; + case PROP_VENDOR_NAME: + characterstring_init_ansi(&char_string, Vendor_Name); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_VENDOR_IDENTIFIER: + apdu_len = + encode_application_unsigned(&apdu[0], Vendor_Identifier); + break; + case PROP_MODEL_NAME: + characterstring_init_ansi(&char_string, Model_Name); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_FIRMWARE_REVISION: + characterstring_init_ansi(&char_string, BACnet_Version); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_APPLICATION_SOFTWARE_VERSION: + characterstring_init_ansi(&char_string, + Application_Software_Version); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + /* FIXME: if you support time */ + case PROP_LOCAL_TIME: + /* FIXME: get the actual value */ + Local_Time.hour = 7; + Local_Time.min = 0; + Local_Time.sec = 3; + Local_Time.hundredths = 1; + apdu_len = encode_application_time(&apdu[0], &Local_Time); + break; + /* FIXME: if you support time */ + case PROP_UTC_OFFSET: + /* NOTE: if your UTC offset is -5, then BACnet UTC offset is 5 */ + apdu_len = encode_application_signed(&apdu[0], UTC_Offset); + break; + /* FIXME: if you support date */ + case PROP_LOCAL_DATE: + /* FIXME: get the actual value instead of April Fool's Day */ + Local_Date.year = 2006; /* AD */ + Local_Date.month = 4; /* 1=Jan */ + Local_Date.day = 1; /* 1..31 */ + Local_Date.wday = 6; /* 1=Monday */ + apdu_len = encode_application_date(&apdu[0], &Local_Date); + break; + case PROP_DAYLIGHT_SAVINGS_STATUS: + apdu_len = + encode_application_boolean(&apdu[0], Daylight_Savings_Status); + break; + case PROP_PROTOCOL_VERSION: + apdu_len = + encode_application_unsigned(&apdu[0], + Device_Protocol_Version()); + break; + case PROP_PROTOCOL_REVISION: + apdu_len = + encode_application_unsigned(&apdu[0], + Device_Protocol_Revision()); + break; + /* BACnet Legacy Support */ + case PROP_PROTOCOL_CONFORMANCE_CLASS: + apdu_len = encode_application_unsigned(&apdu[0], 1); + break; + case PROP_PROTOCOL_SERVICES_SUPPORTED: + /* Note: list of services that are executed, not initiated. */ + bitstring_init(&bit_string); + for (i = 0; i < MAX_BACNET_SERVICES_SUPPORTED; i++) { + /* automatic lookup based on handlers set */ + bitstring_set_bit(&bit_string, (uint8_t) i, + apdu_service_supported((BACNET_SERVICES_SUPPORTED) i)); + } + apdu_len = encode_application_bitstring(&apdu[0], &bit_string); + break; + case PROP_PROTOCOL_OBJECT_TYPES_SUPPORTED: + /* Note: this is the list of objects that can be in this device, + not a list of objects that this device can access */ + bitstring_init(&bit_string); + for (i = 0; i < MAX_ASHRAE_OBJECT_TYPE; i++) { + /* initialize all the object types to not-supported */ + bitstring_set_bit(&bit_string, (uint8_t) i, false); + } + /* FIXME: indicate the objects that YOU support */ + bitstring_set_bit(&bit_string, OBJECT_DEVICE, true); + if (Analog_Input_Count()) + bitstring_set_bit(&bit_string, OBJECT_ANALOG_INPUT, true); + if (Analog_Output_Count()) + bitstring_set_bit(&bit_string, OBJECT_ANALOG_OUTPUT, true); + if (Analog_Value_Count()) + bitstring_set_bit(&bit_string, OBJECT_ANALOG_VALUE, true); + if (Binary_Input_Count()) + bitstring_set_bit(&bit_string, OBJECT_BINARY_INPUT, true); + if (Binary_Output_Count()) + bitstring_set_bit(&bit_string, OBJECT_BINARY_OUTPUT, true); + if (Binary_Value_Count()) + bitstring_set_bit(&bit_string, OBJECT_BINARY_VALUE, true); + if (Life_Safety_Point_Count()) + bitstring_set_bit(&bit_string, OBJECT_LIFE_SAFETY_POINT, true); + if (Load_Control_Count()) + bitstring_set_bit(&bit_string, OBJECT_LOAD_CONTROL, true); + if (Multistate_Output_Count()) + bitstring_set_bit(&bit_string, OBJECT_MULTI_STATE_OUTPUT, + true); #if defined(BACFILE) - if (bacfile_count()) - bitstring_set_bit(&bit_string, OBJECT_FILE, true); + if (bacfile_count()) + bitstring_set_bit(&bit_string, OBJECT_FILE, true); #endif - apdu_len = encode_application_bitstring(&apdu[0], &bit_string); - break; - case PROP_OBJECT_LIST: - count = Device_Object_List_Count(); - /* Array element zero is the number of objects in the list */ - if (array_index == 0) - apdu_len = encode_application_unsigned(&apdu[0], count); - /* if no index was specified, then try to encode the entire list */ - /* into one packet. Note that more than likely you will have */ - /* to return an error if the number of encoded objects exceeds */ - /* your maximum APDU size. */ - else if (array_index == BACNET_ARRAY_ALL) { - for (i = 1; i <= count; i++) { - if (Device_Object_List_Identifier(i, &object_type, - &instance)) { - len = - encode_application_object_id(&apdu[apdu_len], - object_type, instance); - apdu_len += len; - /* assume next one is the same size as this one */ - /* can we all fit into the APDU? */ - if ((apdu_len + len) >= MAX_APDU) { - /* reject message */ - apdu_len = -2; + apdu_len = encode_application_bitstring(&apdu[0], &bit_string); + break; + case PROP_OBJECT_LIST: + count = Device_Object_List_Count(); + /* Array element zero is the number of objects in the list */ + if (array_index == 0) + apdu_len = encode_application_unsigned(&apdu[0], count); + /* if no index was specified, then try to encode the entire list */ + /* into one packet. Note that more than likely you will have */ + /* to return an error if the number of encoded objects exceeds */ + /* your maximum APDU size. */ + else if (array_index == BACNET_ARRAY_ALL) { + for (i = 1; i <= count; i++) { + if (Device_Object_List_Identifier(i, &object_type, + &instance)) { + len = + encode_application_object_id(&apdu[apdu_len], + object_type, instance); + apdu_len += len; + /* assume next one is the same size as this one */ + /* can we all fit into the APDU? */ + if ((apdu_len + len) >= MAX_APDU) { + /* reject message */ + apdu_len = -2; + break; + } + } else { + /* error: internal error? */ + *error_class = ERROR_CLASS_SERVICES; + *error_code = ERROR_CODE_OTHER; + apdu_len = -1; break; } - } else { - /* error: internal error? */ - *error_class = ERROR_CLASS_SERVICES; - *error_code = ERROR_CODE_OTHER; + } + } else { + if (Device_Object_List_Identifier(array_index, &object_type, + &instance)) + apdu_len = + encode_application_object_id(&apdu[0], object_type, + instance); + else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_ARRAY_INDEX; apdu_len = -1; - break; } } - } else { - if (Device_Object_List_Identifier(array_index, &object_type, - &instance)) - apdu_len = - encode_application_object_id(&apdu[0], object_type, - instance); - else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_ARRAY_INDEX; - apdu_len = -1; - } - } - break; - case PROP_MAX_APDU_LENGTH_ACCEPTED: - apdu_len = encode_application_unsigned(&apdu[0], - Device_Max_APDU_Length_Accepted()); - break; - case PROP_SEGMENTATION_SUPPORTED: - apdu_len = encode_application_enumerated(&apdu[0], - Device_Segmentation_Supported()); - break; - case PROP_APDU_TIMEOUT: - apdu_len = encode_application_unsigned(&apdu[0], APDU_Timeout); - break; - case PROP_NUMBER_OF_APDU_RETRIES: - apdu_len = - encode_application_unsigned(&apdu[0], Number_Of_APDU_Retries); - break; - case PROP_DEVICE_ADDRESS_BINDING: - /* FIXME: encode the list here, if it exists */ - break; - case PROP_DATABASE_REVISION: - apdu_len = encode_application_unsigned(&apdu[0], Database_Revision); - break; + break; + case PROP_MAX_APDU_LENGTH_ACCEPTED: + apdu_len = encode_application_unsigned(&apdu[0], + Device_Max_APDU_Length_Accepted()); + break; + case PROP_SEGMENTATION_SUPPORTED: + apdu_len = encode_application_enumerated(&apdu[0], + Device_Segmentation_Supported()); + break; + case PROP_APDU_TIMEOUT: + apdu_len = encode_application_unsigned(&apdu[0], APDU_Timeout); + break; + case PROP_NUMBER_OF_APDU_RETRIES: + apdu_len = + encode_application_unsigned(&apdu[0], Number_Of_APDU_Retries); + break; + case PROP_DEVICE_ADDRESS_BINDING: + /* FIXME: encode the list here, if it exists */ + break; + case PROP_DATABASE_REVISION: + apdu_len = + encode_application_unsigned(&apdu[0], Database_Revision); + break; #if defined(BACDL_MSTP) - case PROP_MAX_INFO_FRAMES: - apdu_len = - encode_application_unsigned(&apdu[0], dlmstp_max_info_frames()); - break; - case PROP_MAX_MASTER: - apdu_len = encode_application_unsigned(&apdu[0], dlmstp_max_master()); - break; + case PROP_MAX_INFO_FRAMES: + apdu_len = + encode_application_unsigned(&apdu[0], + dlmstp_max_info_frames()); + break; + case PROP_MAX_MASTER: + apdu_len = + encode_application_unsigned(&apdu[0], dlmstp_max_master()); + break; #endif - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_UNKNOWN_PROPERTY; - apdu_len = -1; - break; + default: + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_UNKNOWN_PROPERTY; + apdu_len = -1; + break; } return apdu_len; } /* returns true if successful */ -bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) +bool Device_Write_Property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { bool status = false; /* return value */ int len = 0; @@ -849,122 +902,123 @@ bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, /* FIXME: len < application_data_len: more data? */ /* FIXME: len == 0: unable to decode? */ switch (wp_data->object_property) { - case PROP_OBJECT_IDENTIFIER: - if (value.tag == BACNET_APPLICATION_TAG_OBJECT_ID) { - if ((value.type.Object_Id.type == OBJECT_DEVICE) && - (Device_Set_Object_Instance_Number(value.type.Object_Id. - instance))) { - /* FIXME: we could send an I-Am broadcast to let the world know */ - status = true; - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; - } - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case PROP_NUMBER_OF_APDU_RETRIES: - if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { - /* FIXME: bounds check? */ - Device_Set_Number_Of_APDU_Retries((uint8_t) value. - type.Unsigned_Int); - status = true; - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case PROP_APDU_TIMEOUT: - if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { - /* FIXME: bounds check? */ - Device_Set_APDU_Timeout((uint16_t) value.type.Unsigned_Int); - status = true; - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case PROP_VENDOR_IDENTIFIER: - if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { - /* FIXME: bounds check? */ - Device_Set_Vendor_Identifier((uint16_t) value.type. - Unsigned_Int); - status = true; - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case PROP_SYSTEM_STATUS: - if (value.tag == BACNET_APPLICATION_TAG_ENUMERATED) { - /* FIXME: bounds check? */ - Device_Set_System_Status((BACNET_DEVICE_STATUS)value.type.Enumerated); - status = true; - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case PROP_OBJECT_NAME: - if (value.tag == BACNET_APPLICATION_TAG_CHARACTER_STRING) { - uint8_t encoding; - encoding = - characterstring_encoding(&value.type.Character_String); - if (encoding == CHARACTER_ANSI_X34) { - status = - Device_Set_Object_Name(characterstring_value(&value. - type.Character_String), - characterstring_length(&value.type.Character_String)); - if (!status) { + case PROP_OBJECT_IDENTIFIER: + if (value.tag == BACNET_APPLICATION_TAG_OBJECT_ID) { + if ((value.type.Object_Id.type == OBJECT_DEVICE) && + (Device_Set_Object_Instance_Number(value.type.Object_Id. + instance))) { + /* FIXME: we could send an I-Am broadcast to let the world know */ + status = true; + } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_NO_SPACE_TO_WRITE_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; } } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_CHARACTER_SET_NOT_SUPPORTED; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; + break; + case PROP_NUMBER_OF_APDU_RETRIES: + if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { + /* FIXME: bounds check? */ + Device_Set_Number_Of_APDU_Retries((uint8_t) value. + type.Unsigned_Int); + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; + } + break; + case PROP_APDU_TIMEOUT: + if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { + /* FIXME: bounds check? */ + Device_Set_APDU_Timeout((uint16_t) value.type.Unsigned_Int); + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; + } + break; + case PROP_VENDOR_IDENTIFIER: + if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { + /* FIXME: bounds check? */ + Device_Set_Vendor_Identifier((uint16_t) value.type. + Unsigned_Int); + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; + } + break; + case PROP_SYSTEM_STATUS: + if (value.tag == BACNET_APPLICATION_TAG_ENUMERATED) { + /* FIXME: bounds check? */ + Device_Set_System_Status((BACNET_DEVICE_STATUS) value.type. + Enumerated); + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; + } + break; + case PROP_OBJECT_NAME: + if (value.tag == BACNET_APPLICATION_TAG_CHARACTER_STRING) { + uint8_t encoding; + encoding = + characterstring_encoding(&value.type.Character_String); + if (encoding == CHARACTER_ANSI_X34) { + status = + Device_Set_Object_Name(characterstring_value(&value. + type.Character_String), + characterstring_length(&value.type.Character_String)); + if (!status) { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_NO_SPACE_TO_WRITE_PROPERTY; + } + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_CHARACTER_SET_NOT_SUPPORTED; + } + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; + } + break; #if defined(BACDL_MSTP) - case PROP_MAX_INFO_FRAMES: - if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { - if (value.type.Unsigned_Int <= 255) { - dlmstp_set_max_info_frames(value.type.Unsigned_Int); - status = true; + case PROP_MAX_INFO_FRAMES: + if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { + if (value.type.Unsigned_Int <= 255) { + dlmstp_set_max_info_frames(value.type.Unsigned_Int); + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case PROP_MAX_MASTER: - if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { - if ((value.type.Unsigned_Int > 0) && - (value.type.Unsigned_Int <= 127)) { - dlmstp_set_max_master(value.type.Unsigned_Int); - status = true; + break; + case PROP_MAX_MASTER: + if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { + if ((value.type.Unsigned_Int > 0) && + (value.type.Unsigned_Int <= 127)) { + dlmstp_set_max_master(value.type.Unsigned_Int); + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; + break; #endif default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; - break; + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + break; } return status; @@ -975,7 +1029,8 @@ bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, #include #include "ctest.h" -void testDevice(Test * pTest) +void testDevice( + Test * pTest) { bool status = false; const char *name = "Patricia"; @@ -1009,165 +1064,195 @@ void testDevice(Test * pTest) #ifdef TEST_DEVICE /* stubs to dependencies to keep unit test simple */ -char *Analog_Input_Name(uint32_t object_instance) +char *Analog_Input_Name( + uint32_t object_instance) { (void) object_instance; return ""; } -unsigned Analog_Input_Count(void) +unsigned Analog_Input_Count( + void) { return 0; } -uint32_t Analog_Input_Index_To_Instance(unsigned index) +uint32_t Analog_Input_Index_To_Instance( + unsigned index) { return index; } -char *Analog_Output_Name(uint32_t object_instance) +char *Analog_Output_Name( + uint32_t object_instance) { (void) object_instance; return ""; } -unsigned Analog_Output_Count(void) +unsigned Analog_Output_Count( + void) { return 0; } -uint32_t Analog_Output_Index_To_Instance(unsigned index) +uint32_t Analog_Output_Index_To_Instance( + unsigned index) { return index; } -char *Analog_Value_Name(uint32_t object_instance) +char *Analog_Value_Name( + uint32_t object_instance) { (void) object_instance; return ""; } -unsigned Analog_Value_Count(void) +unsigned Analog_Value_Count( + void) { return 0; } -uint32_t Analog_Value_Index_To_Instance(unsigned index) +uint32_t Analog_Value_Index_To_Instance( + unsigned index) { return index; } -char *Binary_Input_Name(uint32_t object_instance) +char *Binary_Input_Name( + uint32_t object_instance) { (void) object_instance; return ""; } -unsigned Binary_Input_Count(void) +unsigned Binary_Input_Count( + void) { return 0; } -uint32_t Binary_Input_Index_To_Instance(unsigned index) +uint32_t Binary_Input_Index_To_Instance( + unsigned index) { return index; } -char *Binary_Output_Name(uint32_t object_instance) +char *Binary_Output_Name( + uint32_t object_instance) { (void) object_instance; return ""; } -unsigned Binary_Output_Count(void) +unsigned Binary_Output_Count( + void) { return 0; } -uint32_t Binary_Output_Index_To_Instance(unsigned index) +uint32_t Binary_Output_Index_To_Instance( + unsigned index) { return index; } -char *Binary_Value_Name(uint32_t object_instance) +char *Binary_Value_Name( + uint32_t object_instance) { (void) object_instance; return ""; } -unsigned Binary_Value_Count(void) +unsigned Binary_Value_Count( + void) { return 0; } -uint32_t Binary_Value_Index_To_Instance(unsigned index) +uint32_t Binary_Value_Index_To_Instance( + unsigned index) { return index; } -char *Life_Safety_Point_Name(uint32_t object_instance) +char *Life_Safety_Point_Name( + uint32_t object_instance) { (void) object_instance; return ""; } -unsigned Life_Safety_Point_Count(void) +unsigned Life_Safety_Point_Count( + void) { return 0; } -uint32_t Life_Safety_Point_Index_To_Instance(unsigned index) +uint32_t Life_Safety_Point_Index_To_Instance( + unsigned index) { return index; } -char *Load_Control_Name(uint32_t object_instance) +char *Load_Control_Name( + uint32_t object_instance) { (void) object_instance; return ""; } -unsigned Load_Control_Count(void) +unsigned Load_Control_Count( + void) { return 0; } -uint32_t Load_Control_Index_To_Instance(unsigned index) +uint32_t Load_Control_Index_To_Instance( + unsigned index) { return index; } -char *Multistate_Output_Name(uint32_t object_instance) +char *Multistate_Output_Name( + uint32_t object_instance) { (void) object_instance; return ""; } -unsigned Multistate_Output_Count(void) +unsigned Multistate_Output_Count( + void) { return 0; } -uint32_t Multistate_Output_Index_To_Instance(unsigned index) +uint32_t Multistate_Output_Index_To_Instance( + unsigned index) { return index; } #if defined(BACFILE) -uint32_t bacfile_count(void) +uint32_t bacfile_count( + void) { return 0; } #endif #if defined(BACFILE) -uint32_t bacfile_index_to_instance(unsigned find_index) +uint32_t bacfile_index_to_instance( + unsigned find_index) { return find_index; } #endif -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -1184,5 +1269,5 @@ int main(void) return 0; } -#endif /* TEST_DEVICE */ -#endif /* TEST */ +#endif /* TEST_DEVICE */ +#endif /* TEST */ diff --git a/bacnet-stack/demo/object/lc.c b/bacnet-stack/demo/object/lc.c index 51c6fa97..9cf87902 100644 --- a/bacnet-stack/demo/object/lc.c +++ b/bacnet-stack/demo/object/lc.c @@ -29,13 +29,13 @@ #include #include #include -#include /* for memcpy */ +#include /* for memcpy */ #include #include "bacdef.h" #include "bacdcode.h" #include "datetime.h" #include "bacenum.h" -#include "config.h" /* the custom stuff */ +#include "config.h" /* the custom stuff */ #include "lc.h" #include "ao.h" #include "wp.h" @@ -138,8 +138,7 @@ static float Shed_Level_Values[MAX_SHED_LEVELS] = { static bool Load_Control_Initialized = false; /* These three arrays are used by the ReadPropertyMultiple handler */ -static const int Load_Control_Properties_Required[] = -{ +static const int Load_Control_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, PROP_OBJECT_NAME, PROP_OBJECT_TYPE, @@ -158,15 +157,13 @@ static const int Load_Control_Properties_Required[] = -1 }; -static const int Load_Control_Properties_Optional[] = -{ +static const int Load_Control_Properties_Optional[] = { PROP_DESCRIPTION, PROP_FULL_DUTY_BASELINE, -1 }; -static const int Load_Control_Properties_Proprietary[] = -{ +static const int Load_Control_Properties_Proprietary[] = { -1 }; @@ -185,7 +182,8 @@ void Load_Control_Property_Lists( return; } -void Load_Control_Init(void) +void Load_Control_Init( + void) { unsigned i, j; @@ -221,7 +219,8 @@ void Load_Control_Init(void) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need validate that the */ /* given instance exists */ -bool Load_Control_Valid_Instance(uint32_t object_instance) +bool Load_Control_Valid_Instance( + uint32_t object_instance) { Load_Control_Init(); if (object_instance < MAX_LOAD_CONTROLS) @@ -232,7 +231,8 @@ bool Load_Control_Valid_Instance(uint32_t object_instance) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then count how many you have */ -unsigned Load_Control_Count(void) +unsigned Load_Control_Count( + void) { Load_Control_Init(); return MAX_LOAD_CONTROLS; @@ -241,7 +241,8 @@ unsigned Load_Control_Count(void) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need to return the instance */ /* that correlates to the correct index */ -uint32_t Load_Control_Index_To_Instance(unsigned index) +uint32_t Load_Control_Index_To_Instance( + unsigned index) { Load_Control_Init(); return index; @@ -250,7 +251,8 @@ uint32_t Load_Control_Index_To_Instance(unsigned index) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need to return the index */ /* that correlates to the correct instance number */ -unsigned Load_Control_Instance_To_Index(uint32_t object_instance) +unsigned Load_Control_Instance_To_Index( + uint32_t object_instance) { unsigned index = MAX_LOAD_CONTROLS; @@ -261,8 +263,8 @@ unsigned Load_Control_Instance_To_Index(uint32_t object_instance) return index; } -static BACNET_SHED_STATE Load_Control_Present_Value(uint32_t - object_instance) +static BACNET_SHED_STATE Load_Control_Present_Value( + uint32_t object_instance) { BACNET_SHED_STATE value = BACNET_SHED_INACTIVE; unsigned index = 0; @@ -277,7 +279,8 @@ static BACNET_SHED_STATE Load_Control_Present_Value(uint32_t } /* note: the object name must be unique within this device */ -char *Load_Control_Name(uint32_t object_instance) +char *Load_Control_Name( + uint32_t object_instance) { static char text_string[32] = ""; /* okay for single thread */ @@ -289,7 +292,8 @@ char *Load_Control_Name(uint32_t object_instance) return NULL; } -static void Update_Current_Time(BACNET_DATE_TIME * bdatetime) +static void Update_Current_Time( + BACNET_DATE_TIME * bdatetime) { time_t timer; struct tm *tblock; @@ -319,80 +323,84 @@ struct tm { } /* convert the shed level request into an Analog Output Present_Value */ -static float Requested_Shed_Level_Value(int object_index) +static float Requested_Shed_Level_Value( + int object_index) { unsigned shed_level_index = 0; unsigned i = 0; float requested_level = 0.0; switch (Requested_Shed_Level[object_index].type) { - case BACNET_SHED_TYPE_PERCENT: - requested_level = - (float) Requested_Shed_Level[object_index].value.percent; - break; - case BACNET_SHED_TYPE_AMOUNT: - /* Assumptions: wattage is linear with analog output level */ - requested_level = - Full_Duty_Baseline[object_index] - - Requested_Shed_Level[object_index].value.amount; - requested_level /= Full_Duty_Baseline[object_index]; - requested_level *= 100.0; - break; - case BACNET_SHED_TYPE_LEVEL: - default: - for (i = 0; i < MAX_SHED_LEVELS; i++) { - if (Shed_Levels[object_index][i] <= - Requested_Shed_Level[object_index].value.level) - shed_level_index = i; - } - requested_level = Shed_Level_Values[shed_level_index]; - break; + case BACNET_SHED_TYPE_PERCENT: + requested_level = + (float) Requested_Shed_Level[object_index].value.percent; + break; + case BACNET_SHED_TYPE_AMOUNT: + /* Assumptions: wattage is linear with analog output level */ + requested_level = + Full_Duty_Baseline[object_index] - + Requested_Shed_Level[object_index].value.amount; + requested_level /= Full_Duty_Baseline[object_index]; + requested_level *= 100.0; + break; + case BACNET_SHED_TYPE_LEVEL: + default: + for (i = 0; i < MAX_SHED_LEVELS; i++) { + if (Shed_Levels[object_index][i] <= + Requested_Shed_Level[object_index].value.level) + shed_level_index = i; + } + requested_level = Shed_Level_Values[shed_level_index]; + break; } return requested_level; } -static void Shed_Level_Copy(BACNET_SHED_LEVEL * dest, +static void Shed_Level_Copy( + BACNET_SHED_LEVEL * dest, BACNET_SHED_LEVEL * src) { if (dest && src) { dest->type = src->type; switch (src->type) { - case BACNET_SHED_TYPE_PERCENT: - dest->value.percent = src->value.percent; - break; - case BACNET_SHED_TYPE_AMOUNT: - dest->value.amount = src->value.amount; - break; - case BACNET_SHED_TYPE_LEVEL: - default: - dest->value.level = src->value.level; - break; + case BACNET_SHED_TYPE_PERCENT: + dest->value.percent = src->value.percent; + break; + case BACNET_SHED_TYPE_AMOUNT: + dest->value.amount = src->value.amount; + break; + case BACNET_SHED_TYPE_LEVEL: + default: + dest->value.level = src->value.level; + break; } } } -static void Shed_Level_Default_Set(BACNET_SHED_LEVEL * dest, +static void Shed_Level_Default_Set( + BACNET_SHED_LEVEL * dest, BACNET_SHED_LEVEL_TYPE type) { if (dest) { dest->type = type; switch (type) { - case BACNET_SHED_TYPE_PERCENT: - dest->value.percent = 100; - break; - case BACNET_SHED_TYPE_AMOUNT: - dest->value.amount = 0.0; - break; - case BACNET_SHED_TYPE_LEVEL: - default: - dest->value.level = 0; - break; + case BACNET_SHED_TYPE_PERCENT: + dest->value.percent = 100; + break; + case BACNET_SHED_TYPE_AMOUNT: + dest->value.amount = 0.0; + break; + case BACNET_SHED_TYPE_LEVEL: + default: + dest->value.level = 0; + break; } } } -static bool Able_To_Meet_Shed_Request(int object_index) +static bool Able_To_Meet_Shed_Request( + int object_index) { float level = 0.0; float requested_level = 0.0; @@ -427,7 +435,8 @@ static LOAD_CONTROL_STATE Load_Control_State[MAX_LOAD_CONTROLS]; static LOAD_CONTROL_STATE Load_Control_State_Previously[MAX_LOAD_CONTROLS]; #if PRINT_ENABLED_DEBUG -static void Print_Load_Control_State(int object_index) +static void Print_Load_Control_State( + int object_index) { char *Load_Control_State_Text[MAX_LOAD_CONTROLS] = { "SHED_INACTIVE", @@ -445,10 +454,11 @@ static void Print_Load_Control_State(int object_index) } #endif -void Load_Control_State_Machine(int object_index) +void Load_Control_State_Machine( + int object_index) { - unsigned i = 0; /* loop counter */ - int diff = 0; /* used for datetime comparison */ + unsigned i = 0; /* loop counter */ + int diff = 0; /* used for datetime comparison */ /* is the state machine enabled? */ if (!Load_Control_Enable[object_index]) { @@ -457,82 +467,129 @@ void Load_Control_State_Machine(int object_index) } switch (Load_Control_State[object_index]) { - case SHED_REQUEST_PENDING: - if (Load_Control_Request_Written[object_index]) { - Load_Control_Request_Written[object_index] = false; - /* request to cancel using default values? */ - switch (Requested_Shed_Level[object_index].type) { - case BACNET_SHED_TYPE_PERCENT: - if (Requested_Shed_Level[object_index].value.percent == - DEFAULT_VALUE_PERCENT) - Load_Control_State[object_index] = SHED_INACTIVE; - break; - case BACNET_SHED_TYPE_AMOUNT: - if (Requested_Shed_Level[object_index].value.amount == - DEFAULT_VALUE_AMOUNT) - Load_Control_State[object_index] = SHED_INACTIVE; - break; - case BACNET_SHED_TYPE_LEVEL: - default: - if (Requested_Shed_Level[object_index].value.level == - DEFAULT_VALUE_LEVEL) - Load_Control_State[object_index] = SHED_INACTIVE; - break; - } - if (Load_Control_State[object_index] == SHED_INACTIVE) { + case SHED_REQUEST_PENDING: + if (Load_Control_Request_Written[object_index]) { + Load_Control_Request_Written[object_index] = false; + /* request to cancel using default values? */ + switch (Requested_Shed_Level[object_index].type) { + case BACNET_SHED_TYPE_PERCENT: + if (Requested_Shed_Level[object_index].value.percent == + DEFAULT_VALUE_PERCENT) + Load_Control_State[object_index] = SHED_INACTIVE; + break; + case BACNET_SHED_TYPE_AMOUNT: + if (Requested_Shed_Level[object_index].value.amount == + DEFAULT_VALUE_AMOUNT) + Load_Control_State[object_index] = SHED_INACTIVE; + break; + case BACNET_SHED_TYPE_LEVEL: + default: + if (Requested_Shed_Level[object_index].value.level == + DEFAULT_VALUE_LEVEL) + Load_Control_State[object_index] = SHED_INACTIVE; + break; + } + if (Load_Control_State[object_index] == SHED_INACTIVE) { #if PRINT_ENABLED_DEBUG - printf("Load Control[%d]:Requested Shed Level=Default\n", - object_index); + printf("Load Control[%d]:Requested Shed Level=Default\n", + object_index); #endif - break; + break; + } } - } - /* clear the flag for Start time if it is written */ - if (Start_Time_Property_Written[object_index]) { - Start_Time_Property_Written[object_index] = false; - /* request to cancel using wildcards in start time? */ - if (datetime_wildcard(&Start_Time[object_index])) { + /* clear the flag for Start time if it is written */ + if (Start_Time_Property_Written[object_index]) { + Start_Time_Property_Written[object_index] = false; + /* request to cancel using wildcards in start time? */ + if (datetime_wildcard(&Start_Time[object_index])) { + Load_Control_State[object_index] = SHED_INACTIVE; +#if PRINT_ENABLED_DEBUG + printf("Load Control[%d]:Start Time=Wildcard\n", + object_index); +#endif + break; + } + } + /* cancel because current time is after start time + duration? */ + datetime_copy(&End_Time[object_index], &Start_Time[object_index]); + datetime_add_minutes(&End_Time[object_index], + Shed_Duration[object_index]); + diff = datetime_compare(&End_Time[object_index], &Current_Time); + if (diff < 0) { + /* CancelShed */ + /* FIXME: stop shedding! i.e. relinquish */ +#if PRINT_ENABLED_DEBUG + printf("Load Control[%d]:Current Time" + " is after Start Time + Duration\n", object_index); +#endif Load_Control_State[object_index] = SHED_INACTIVE; -#if PRINT_ENABLED_DEBUG - printf("Load Control[%d]:Start Time=Wildcard\n", - object_index); -#endif break; } - } - /* cancel because current time is after start time + duration? */ - datetime_copy(&End_Time[object_index], &Start_Time[object_index]); - datetime_add_minutes(&End_Time[object_index], - Shed_Duration[object_index]); - diff = datetime_compare(&End_Time[object_index], &Current_Time); - if (diff < 0) { - /* CancelShed */ - /* FIXME: stop shedding! i.e. relinquish */ + diff = datetime_compare(&Current_Time, &Start_Time[object_index]); + if (diff < 0) { + /* current time prior to start time */ + /* ReconfigurePending */ + Shed_Level_Copy(&Expected_Shed_Level[object_index], + &Requested_Shed_Level[object_index]); + Shed_Level_Default_Set(&Actual_Shed_Level[object_index], + Requested_Shed_Level[object_index].type); + } else if (diff > 0) { + /* current time after to start time */ #if PRINT_ENABLED_DEBUG - printf("Load Control[%d]:Current Time" - " is after Start Time + Duration\n", - object_index); + printf("Load Control[%d]:Current Time" + " is after Start Time\n", object_index); #endif - Load_Control_State[object_index] = SHED_INACTIVE; + /* AbleToMeetShed */ + if (Able_To_Meet_Shed_Request(object_index)) { + Shed_Level_Copy(&Expected_Shed_Level[object_index], + &Requested_Shed_Level[object_index]); + Analog_Output_Present_Value_Set(object_index, + Requested_Shed_Level_Value(object_index), 4); + Shed_Level_Copy(&Actual_Shed_Level[object_index], + &Requested_Shed_Level[object_index]); + Load_Control_State[object_index] = SHED_COMPLIANT; + } else { + /* CannotMeetShed */ + Shed_Level_Default_Set(&Expected_Shed_Level[object_index], + Requested_Shed_Level[object_index].type); + Shed_Level_Default_Set(&Actual_Shed_Level[object_index], + Requested_Shed_Level[object_index].type); + Load_Control_State[object_index] = SHED_NON_COMPLIANT; + } + } break; - } - diff = datetime_compare(&Current_Time, &Start_Time[object_index]); - if (diff < 0) { - /* current time prior to start time */ - /* ReconfigurePending */ - Shed_Level_Copy(&Expected_Shed_Level[object_index], - &Requested_Shed_Level[object_index]); - Shed_Level_Default_Set(&Actual_Shed_Level[object_index], - Requested_Shed_Level[object_index].type); - } else if (diff > 0) { - /* current time after to start time */ + case SHED_NON_COMPLIANT: + datetime_copy(&End_Time[object_index], &Start_Time[object_index]); + datetime_add_minutes(&End_Time[object_index], + Shed_Duration[object_index]); + diff = datetime_compare(&End_Time[object_index], &Current_Time); + if (diff < 0) { + /* FinishedUnsuccessfulShed */ #if PRINT_ENABLED_DEBUG - printf("Load Control[%d]:Current Time" - " is after Start Time\n", - object_index); + printf + ("Load Control[%d]:Current Time is after Start Time + Duration\n", + object_index); #endif - /* AbleToMeetShed */ + Load_Control_State[object_index] = SHED_INACTIVE; + break; + } + if (Load_Control_Request_Written[object_index] || + Start_Time_Property_Written[object_index]) { + /* UnsuccessfulShedReconfigured */ +#if PRINT_ENABLED_DEBUG + printf("Load Control[%d]:Control Property written\n", + object_index); +#endif + /* The Written flags will cleared in the next state */ + Load_Control_State[object_index] = SHED_REQUEST_PENDING; + break; + } if (Able_To_Meet_Shed_Request(object_index)) { + /* CanNowComplyWithShed */ +#if PRINT_ENABLED_DEBUG + printf("Load Control[%d]:Able to meet Shed Request\n", + object_index); +#endif Shed_Level_Copy(&Expected_Shed_Level[object_index], &Requested_Shed_Level[object_index]); Analog_Output_Present_Value_Set(object_index, @@ -540,119 +597,71 @@ void Load_Control_State_Machine(int object_index) Shed_Level_Copy(&Actual_Shed_Level[object_index], &Requested_Shed_Level[object_index]); Load_Control_State[object_index] = SHED_COMPLIANT; - } else { - /* CannotMeetShed */ + } + break; + case SHED_COMPLIANT: + datetime_copy(&End_Time[object_index], &Start_Time[object_index]); + datetime_add_minutes(&End_Time[object_index], + Shed_Duration[object_index]); + diff = datetime_compare(&End_Time[object_index], &Current_Time); + if (diff < 0) { + /* FinishedSuccessfulShed */ +#if PRINT_ENABLED_DEBUG + printf + ("Load Control[%d]:Current Time is after Start Time + Duration\n", + object_index); +#endif + datetime_wildcard_set(&Start_Time[i]); + Analog_Output_Present_Value_Relinquish(object_index, 4); + Load_Control_State[object_index] = SHED_INACTIVE; + break; + } + if (Load_Control_Request_Written[object_index] || + Start_Time_Property_Written[object_index]) { + /* UnsuccessfulShedReconfigured */ +#if PRINT_ENABLED_DEBUG + printf("Load Control[%d]:Control Property written\n", + object_index); +#endif + /* The Written flags will cleared in the next state */ + Load_Control_State[object_index] = SHED_REQUEST_PENDING; + break; + } + if (!Able_To_Meet_Shed_Request(object_index)) { + /* CanNoLongerComplyWithShed */ +#if PRINT_ENABLED_DEBUG + printf("Load Control[%d]:Not able to meet Shed Request\n", + object_index); +#endif Shed_Level_Default_Set(&Expected_Shed_Level[object_index], Requested_Shed_Level[object_index].type); Shed_Level_Default_Set(&Actual_Shed_Level[object_index], Requested_Shed_Level[object_index].type); Load_Control_State[object_index] = SHED_NON_COMPLIANT; } - } - break; - case SHED_NON_COMPLIANT: - datetime_copy(&End_Time[object_index], &Start_Time[object_index]); - datetime_add_minutes(&End_Time[object_index], - Shed_Duration[object_index]); - diff = datetime_compare(&End_Time[object_index], &Current_Time); - if (diff < 0) { - /* FinishedUnsuccessfulShed */ -#if PRINT_ENABLED_DEBUG - printf - ("Load Control[%d]:Current Time is after Start Time + Duration\n", - object_index); -#endif - Load_Control_State[object_index] = SHED_INACTIVE; break; - } - if (Load_Control_Request_Written[object_index] || - Start_Time_Property_Written[object_index]) { - /* UnsuccessfulShedReconfigured */ + case SHED_INACTIVE: + default: + if (Start_Time_Property_Written[object_index]) { #if PRINT_ENABLED_DEBUG - printf("Load Control[%d]:Control Property written\n", - object_index); + printf("Load Control[%d]:Start Time written\n", object_index); #endif - /* The Written flags will cleared in the next state */ - Load_Control_State[object_index] = SHED_REQUEST_PENDING; + /* The Written flag will cleared in the next state */ + Shed_Level_Copy(&Expected_Shed_Level[object_index], + &Requested_Shed_Level[object_index]); + Shed_Level_Default_Set(&Actual_Shed_Level[object_index], + Requested_Shed_Level[object_index].type); + Load_Control_State[object_index] = SHED_REQUEST_PENDING; + } break; - } - if (Able_To_Meet_Shed_Request(object_index)) { - /* CanNowComplyWithShed */ -#if PRINT_ENABLED_DEBUG - printf("Load Control[%d]:Able to meet Shed Request\n", - object_index); -#endif - Shed_Level_Copy(&Expected_Shed_Level[object_index], - &Requested_Shed_Level[object_index]); - Analog_Output_Present_Value_Set(object_index, - Requested_Shed_Level_Value(object_index), 4); - Shed_Level_Copy(&Actual_Shed_Level[object_index], - &Requested_Shed_Level[object_index]); - Load_Control_State[object_index] = SHED_COMPLIANT; - } - break; - case SHED_COMPLIANT: - datetime_copy(&End_Time[object_index], &Start_Time[object_index]); - datetime_add_minutes(&End_Time[object_index], - Shed_Duration[object_index]); - diff = datetime_compare(&End_Time[object_index], &Current_Time); - if (diff < 0) { - /* FinishedSuccessfulShed */ -#if PRINT_ENABLED_DEBUG - printf - ("Load Control[%d]:Current Time is after Start Time + Duration\n", - object_index); -#endif - datetime_wildcard_set(&Start_Time[i]); - Analog_Output_Present_Value_Relinquish(object_index, 4); - Load_Control_State[object_index] = SHED_INACTIVE; - break; - } - if (Load_Control_Request_Written[object_index] || - Start_Time_Property_Written[object_index]) { - /* UnsuccessfulShedReconfigured */ -#if PRINT_ENABLED_DEBUG - printf("Load Control[%d]:Control Property written\n", - object_index); -#endif - /* The Written flags will cleared in the next state */ - Load_Control_State[object_index] = SHED_REQUEST_PENDING; - break; - } - if (!Able_To_Meet_Shed_Request(object_index)) { - /* CanNoLongerComplyWithShed */ -#if PRINT_ENABLED_DEBUG - printf("Load Control[%d]:Not able to meet Shed Request\n", - object_index); -#endif - Shed_Level_Default_Set(&Expected_Shed_Level[object_index], - Requested_Shed_Level[object_index].type); - Shed_Level_Default_Set(&Actual_Shed_Level[object_index], - Requested_Shed_Level[object_index].type); - Load_Control_State[object_index] = SHED_NON_COMPLIANT; - } - break; - case SHED_INACTIVE: - default: - if (Start_Time_Property_Written[object_index]) { -#if PRINT_ENABLED_DEBUG - printf("Load Control[%d]:Start Time written\n", object_index); -#endif - /* The Written flag will cleared in the next state */ - Shed_Level_Copy(&Expected_Shed_Level[object_index], - &Requested_Shed_Level[object_index]); - Shed_Level_Default_Set(&Actual_Shed_Level[object_index], - Requested_Shed_Level[object_index].type); - Load_Control_State[object_index] = SHED_REQUEST_PENDING; - } - break; } return; } /* call every second or so */ -void Load_Control_State_Machine_Handler(void) +void Load_Control_State_Machine_Handler( + void) { unsigned i = 0; static bool initialized = false; @@ -680,14 +689,16 @@ void Load_Control_State_Machine_Handler(void) } /* return apdu len, or -1 on error */ -int Load_Control_Encode_Property_APDU(uint8_t * apdu, +int Load_Control_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { int len = 0; - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ BACNET_BIT_STRING bit_string; BACNET_CHARACTER_STRING char_string; int enumeration = 0; @@ -698,206 +709,216 @@ int Load_Control_Encode_Property_APDU(uint8_t * apdu, Load_Control_Init(); object_index = Load_Control_Instance_To_Index(object_instance); switch (property) { - case PROP_OBJECT_IDENTIFIER: - apdu_len = encode_application_object_id(&apdu[0], OBJECT_LOAD_CONTROL, - object_instance); - break; - case PROP_OBJECT_NAME: - characterstring_init_ansi(&char_string, - Load_Control_Name(object_instance)); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_OBJECT_TYPE: - apdu_len = encode_application_enumerated(&apdu[0], OBJECT_LOAD_CONTROL); - break; - /* optional property - case PROP_DESCRIPTION: - characterstring_init_ansi(&char_string,"optional description"); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - */ - case PROP_PRESENT_VALUE: - enumeration = Load_Control_Present_Value(object_instance); - apdu_len = encode_application_enumerated(&apdu[0], enumeration); - break; - case PROP_STATUS_FLAGS: - bitstring_init(&bit_string); - /* IN_ALARM - Logical FALSE (0) if the Event_State property - has a value of NORMAL, otherwise logical TRUE (1). */ - bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); - /* FAULT - Logical TRUE (1) if the Reliability property is - present and does not have a value of NO_FAULT_DETECTED, - otherwise logical FALSE (0). */ - bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); - /* OVERRIDDEN - Logical TRUE (1) if the point has been - overridden by some mechanism local to the BACnet Device, - otherwise logical FALSE (0). */ - bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); - /* OUT_OF_SERVICE - This bit shall always be Logical FALSE (0). */ - bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); - apdu_len = encode_application_bitstring(&apdu[0], &bit_string); - break; - case PROP_EVENT_STATE: - apdu_len = encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); - break; - case PROP_REQUESTED_SHED_LEVEL: - switch (Requested_Shed_Level[object_index].type) { - case BACNET_SHED_TYPE_PERCENT: - apdu_len = encode_context_unsigned(&apdu[0], 0, - Requested_Shed_Level[object_index].value.percent); + case PROP_OBJECT_IDENTIFIER: + apdu_len = + encode_application_object_id(&apdu[0], OBJECT_LOAD_CONTROL, + object_instance); break; - case BACNET_SHED_TYPE_AMOUNT: - apdu_len = encode_context_real(&apdu[0], 2, - Requested_Shed_Level[object_index].value.amount); + case PROP_OBJECT_NAME: + characterstring_init_ansi(&char_string, + Load_Control_Name(object_instance)); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); break; - case BACNET_SHED_TYPE_LEVEL: - default: - apdu_len = encode_context_unsigned(&apdu[0], 1, - Requested_Shed_Level[object_index].value.level); + case PROP_OBJECT_TYPE: + apdu_len = + encode_application_enumerated(&apdu[0], OBJECT_LOAD_CONTROL); break; - } - break; - case PROP_START_TIME: - len = encode_application_date(&apdu[0], &Start_Time[object_index].date); - apdu_len = len; - len = encode_application_time(&apdu[apdu_len], - &Start_Time[object_index].time); - apdu_len += len; - break; - case PROP_SHED_DURATION: - apdu_len = encode_application_unsigned(&apdu[0], - Shed_Duration[object_index]); - break; - case PROP_DUTY_WINDOW: - apdu_len = encode_application_unsigned(&apdu[0], - Duty_Window[object_index]); - break; - case PROP_ENABLE: - state = Load_Control_Enable[object_index]; - apdu_len = encode_application_boolean(&apdu[0], state); - break; - case PROP_FULL_DUTY_BASELINE: /* optional */ - apdu_len = encode_application_real(&apdu[0], - Full_Duty_Baseline[object_index]); - break; - case PROP_EXPECTED_SHED_LEVEL: - switch (Expected_Shed_Level[object_index].type) { - case BACNET_SHED_TYPE_PERCENT: - apdu_len = encode_context_unsigned(&apdu[0], 0, - Expected_Shed_Level[object_index].value.percent); + /* optional property + case PROP_DESCRIPTION: + characterstring_init_ansi(&char_string,"optional description"); + apdu_len = encode_application_character_string(&apdu[0], &char_string); + break; + */ + case PROP_PRESENT_VALUE: + enumeration = Load_Control_Present_Value(object_instance); + apdu_len = encode_application_enumerated(&apdu[0], enumeration); break; - case BACNET_SHED_TYPE_AMOUNT: - apdu_len = encode_context_real(&apdu[0], 2, - Expected_Shed_Level[object_index].value.amount); + case PROP_STATUS_FLAGS: + bitstring_init(&bit_string); + /* IN_ALARM - Logical FALSE (0) if the Event_State property + has a value of NORMAL, otherwise logical TRUE (1). */ + bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); + /* FAULT - Logical TRUE (1) if the Reliability property is + present and does not have a value of NO_FAULT_DETECTED, + otherwise logical FALSE (0). */ + bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); + /* OVERRIDDEN - Logical TRUE (1) if the point has been + overridden by some mechanism local to the BACnet Device, + otherwise logical FALSE (0). */ + bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); + /* OUT_OF_SERVICE - This bit shall always be Logical FALSE (0). */ + bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); + apdu_len = encode_application_bitstring(&apdu[0], &bit_string); break; - case BACNET_SHED_TYPE_LEVEL: - default: - apdu_len = encode_context_unsigned(&apdu[0], 1, - Expected_Shed_Level[object_index].value.level); + case PROP_EVENT_STATE: + apdu_len = + encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); break; - } - break; - case PROP_ACTUAL_SHED_LEVEL: - switch (Actual_Shed_Level[object_index].type) { - case BACNET_SHED_TYPE_PERCENT: - apdu_len = encode_context_unsigned(&apdu[0], 0, - Actual_Shed_Level[object_index].value.percent); - break; - case BACNET_SHED_TYPE_AMOUNT: - apdu_len = encode_context_real(&apdu[0], 2, - Actual_Shed_Level[object_index].value.amount); - break; - case BACNET_SHED_TYPE_LEVEL: - default: - apdu_len = encode_context_unsigned(&apdu[0], 1, - Actual_Shed_Level[object_index].value.level); - break; - } - break; - case PROP_SHED_LEVELS: - /* Array element zero is the number of elements in the array */ - if (array_index == 0) - apdu_len = encode_application_unsigned(&apdu[0], MAX_SHED_LEVELS); - /* if no index was specified, then try to encode the entire list */ - /* into one packet. */ - else if (array_index == BACNET_ARRAY_ALL) { - apdu_len = 0; - for (i = 0; i < MAX_SHED_LEVELS; i++) { - /* FIXME: check if we have room before adding it to APDU */ - len = - encode_application_unsigned(&apdu[apdu_len], - Shed_Levels[object_index][i]); - /* add it if we have room */ - if ((apdu_len + len) < MAX_APDU) - apdu_len += len; - else { - *error_class = ERROR_CLASS_SERVICES; - *error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT; - apdu_len = -1; + case PROP_REQUESTED_SHED_LEVEL: + switch (Requested_Shed_Level[object_index].type) { + case BACNET_SHED_TYPE_PERCENT: + apdu_len = encode_context_unsigned(&apdu[0], 0, + Requested_Shed_Level[object_index].value.percent); break; + case BACNET_SHED_TYPE_AMOUNT: + apdu_len = encode_context_real(&apdu[0], 2, + Requested_Shed_Level[object_index].value.amount); + break; + case BACNET_SHED_TYPE_LEVEL: + default: + apdu_len = encode_context_unsigned(&apdu[0], 1, + Requested_Shed_Level[object_index].value.level); + break; + } + break; + case PROP_START_TIME: + len = + encode_application_date(&apdu[0], + &Start_Time[object_index].date); + apdu_len = len; + len = encode_application_time(&apdu[apdu_len], + &Start_Time[object_index].time); + apdu_len += len; + break; + case PROP_SHED_DURATION: + apdu_len = encode_application_unsigned(&apdu[0], + Shed_Duration[object_index]); + break; + case PROP_DUTY_WINDOW: + apdu_len = encode_application_unsigned(&apdu[0], + Duty_Window[object_index]); + break; + case PROP_ENABLE: + state = Load_Control_Enable[object_index]; + apdu_len = encode_application_boolean(&apdu[0], state); + break; + case PROP_FULL_DUTY_BASELINE: /* optional */ + apdu_len = encode_application_real(&apdu[0], + Full_Duty_Baseline[object_index]); + break; + case PROP_EXPECTED_SHED_LEVEL: + switch (Expected_Shed_Level[object_index].type) { + case BACNET_SHED_TYPE_PERCENT: + apdu_len = encode_context_unsigned(&apdu[0], 0, + Expected_Shed_Level[object_index].value.percent); + break; + case BACNET_SHED_TYPE_AMOUNT: + apdu_len = encode_context_real(&apdu[0], 2, + Expected_Shed_Level[object_index].value.amount); + break; + case BACNET_SHED_TYPE_LEVEL: + default: + apdu_len = encode_context_unsigned(&apdu[0], 1, + Expected_Shed_Level[object_index].value.level); + break; + } + break; + case PROP_ACTUAL_SHED_LEVEL: + switch (Actual_Shed_Level[object_index].type) { + case BACNET_SHED_TYPE_PERCENT: + apdu_len = encode_context_unsigned(&apdu[0], 0, + Actual_Shed_Level[object_index].value.percent); + break; + case BACNET_SHED_TYPE_AMOUNT: + apdu_len = encode_context_real(&apdu[0], 2, + Actual_Shed_Level[object_index].value.amount); + break; + case BACNET_SHED_TYPE_LEVEL: + default: + apdu_len = encode_context_unsigned(&apdu[0], 1, + Actual_Shed_Level[object_index].value.level); + break; + } + break; + case PROP_SHED_LEVELS: + /* Array element zero is the number of elements in the array */ + if (array_index == 0) + apdu_len = + encode_application_unsigned(&apdu[0], MAX_SHED_LEVELS); + /* if no index was specified, then try to encode the entire list */ + /* into one packet. */ + else if (array_index == BACNET_ARRAY_ALL) { + apdu_len = 0; + for (i = 0; i < MAX_SHED_LEVELS; i++) { + /* FIXME: check if we have room before adding it to APDU */ + len = + encode_application_unsigned(&apdu[apdu_len], + Shed_Levels[object_index][i]); + /* add it if we have room */ + if ((apdu_len + len) < MAX_APDU) + apdu_len += len; + else { + *error_class = ERROR_CLASS_SERVICES; + *error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT; + apdu_len = -1; + break; + } + } + } else { + if (array_index <= MAX_SHED_LEVELS) { + apdu_len = encode_application_unsigned(&apdu[0], + Shed_Levels[object_index][array_index - 1]); + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_ARRAY_INDEX; + apdu_len = -1; } } - } else { - if (array_index <= MAX_SHED_LEVELS) { - apdu_len = encode_application_unsigned(&apdu[0], - Shed_Levels[object_index][array_index - 1]); + break; + case PROP_SHED_LEVEL_DESCRIPTIONS: + /* Array element zero is the number of elements in the array */ + if (array_index == 0) + apdu_len = + encode_application_unsigned(&apdu[0], MAX_SHED_LEVELS); + /* if no index was specified, then try to encode the entire list */ + /* into one packet. */ + else if (array_index == BACNET_ARRAY_ALL) { + apdu_len = 0; + for (i = 0; i < MAX_SHED_LEVELS; i++) { + /* FIXME: check if we have room before adding it to APDU */ + characterstring_init_ansi(&char_string, + Shed_Level_Descriptions[i]); + len = encode_application_character_string(&apdu[apdu_len], + &char_string); + /* add it if we have room */ + if ((apdu_len + len) < MAX_APDU) + apdu_len += len; + else { + *error_class = ERROR_CLASS_SERVICES; + *error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT; + apdu_len = -1; + break; + } + } } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_ARRAY_INDEX; - apdu_len = -1; - } - } - break; - case PROP_SHED_LEVEL_DESCRIPTIONS: - /* Array element zero is the number of elements in the array */ - if (array_index == 0) - apdu_len = encode_application_unsigned(&apdu[0], MAX_SHED_LEVELS); - /* if no index was specified, then try to encode the entire list */ - /* into one packet. */ - else if (array_index == BACNET_ARRAY_ALL) { - apdu_len = 0; - for (i = 0; i < MAX_SHED_LEVELS; i++) { - /* FIXME: check if we have room before adding it to APDU */ - characterstring_init_ansi(&char_string, - Shed_Level_Descriptions[i]); - len = encode_application_character_string(&apdu[apdu_len], - &char_string); - /* add it if we have room */ - if ((apdu_len + len) < MAX_APDU) - apdu_len += len; - else { - *error_class = ERROR_CLASS_SERVICES; - *error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT; + if (array_index <= MAX_SHED_LEVELS) { + characterstring_init_ansi(&char_string, + Shed_Level_Descriptions[array_index - 1]); + apdu_len = encode_application_character_string(&apdu[0], + &char_string); + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_ARRAY_INDEX; apdu_len = -1; - break; } } - } else { - if (array_index <= MAX_SHED_LEVELS) { - characterstring_init_ansi(&char_string, - Shed_Level_Descriptions[array_index - 1]); - apdu_len = encode_application_character_string(&apdu[0], - &char_string); - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_ARRAY_INDEX; - apdu_len = -1; - } - } - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_UNKNOWN_PROPERTY; - apdu_len = -1; - break; + break; + default: + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_UNKNOWN_PROPERTY; + apdu_len = -1; + break; } return apdu_len; } /* returns true if successful */ -bool Load_Control_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) +bool Load_Control_Write_Property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { bool status = false; /* return value */ unsigned int object_index = 0; @@ -915,121 +936,120 @@ bool Load_Control_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, wp_data->application_data_len, &value); /* FIXME: len < application_data_len: more data? */ /* FIXME: len == 0: unable to decode? */ - object_index = - Load_Control_Instance_To_Index(wp_data->object_instance); + object_index = Load_Control_Instance_To_Index(wp_data->object_instance); switch (wp_data->object_property) { - case PROP_REQUESTED_SHED_LEVEL: - len = bacapp_decode_context_data(wp_data->application_data, - wp_data->application_data_len, - &value, PROP_REQUESTED_SHED_LEVEL); - if (value.context_tag == 0) { - /* percent - Unsigned */ - Requested_Shed_Level[object_index].type = - BACNET_SHED_TYPE_PERCENT; - Requested_Shed_Level[object_index].value.percent = - value.type.Unsigned_Int; - status = true; - } else if (value.context_tag == 1) { - /* level - Unsigned */ - Requested_Shed_Level[object_index].type = - BACNET_SHED_TYPE_LEVEL; - Requested_Shed_Level[object_index].value.level = - value.type.Unsigned_Int; - status = true; - } else if (value.context_tag == 2) { - /* amount - REAL */ - Requested_Shed_Level[object_index].type = - BACNET_SHED_TYPE_AMOUNT; - Requested_Shed_Level[object_index].value.amount = - value.type.Real; - status = true; - } else { - /* error! */ - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - if (status) { - Load_Control_Request_Written[object_index] = true; - } - break; - case PROP_START_TIME: - if (value.tag == BACNET_APPLICATION_TAG_DATE) { - memcpy(&Start_Time[object_index].date, - &value.type.Date, sizeof(value.type.Date)); - Start_Time_Property_Written[object_index] = true; - status = true; - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - if (!status) - break; - len = - bacapp_decode_application_data(wp_data->application_data + len, - wp_data->application_data_len - len, &value); - if (len && value.tag == BACNET_APPLICATION_TAG_TIME) { - memcpy(&Start_Time[object_index].time, - &value.type.Time, sizeof(value.type.Time)); - status = true; - } else { - status = false; - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case PROP_SHED_DURATION: - if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { - Shed_Duration[object_index] = value.type.Unsigned_Int; - Load_Control_Request_Written[object_index] = true; - status = true; - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case PROP_DUTY_WINDOW: - if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { - Duty_Window[object_index] = value.type.Unsigned_Int; - Load_Control_Request_Written[object_index] = true; - status = true; - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case PROP_SHED_LEVELS: - if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { - /* re-write the size of the array? */ - if (wp_data->array_index == 0) { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; - } else if (wp_data->array_index == BACNET_ARRAY_ALL) { - /* FIXME: write entire array */ - status = true; - } else if (wp_data->array_index <= MAX_SHED_LEVELS) { - Shed_Levels[object_index][wp_data->array_index - 1] = + case PROP_REQUESTED_SHED_LEVEL: + len = bacapp_decode_context_data(wp_data->application_data, + wp_data->application_data_len, + &value, PROP_REQUESTED_SHED_LEVEL); + if (value.context_tag == 0) { + /* percent - Unsigned */ + Requested_Shed_Level[object_index].type = + BACNET_SHED_TYPE_PERCENT; + Requested_Shed_Level[object_index].value.percent = value.type.Unsigned_Int; status = true; + } else if (value.context_tag == 1) { + /* level - Unsigned */ + Requested_Shed_Level[object_index].type = + BACNET_SHED_TYPE_LEVEL; + Requested_Shed_Level[object_index].value.level = + value.type.Unsigned_Int; + status = true; + } else if (value.context_tag == 2) { + /* amount - REAL */ + Requested_Shed_Level[object_index].type = + BACNET_SHED_TYPE_AMOUNT; + Requested_Shed_Level[object_index].value.amount = + value.type.Real; + status = true; } else { + /* error! */ + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } - } else { + if (status) { + Load_Control_Request_Written[object_index] = true; + } + break; + case PROP_START_TIME: + if (value.tag == BACNET_APPLICATION_TAG_DATE) { + memcpy(&Start_Time[object_index].date, + &value.type.Date, sizeof(value.type.Date)); + Start_Time_Property_Written[object_index] = true; + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; + } + if (!status) + break; + len = + bacapp_decode_application_data(wp_data->application_data + len, + wp_data->application_data_len - len, &value); + if (len && value.tag == BACNET_APPLICATION_TAG_TIME) { + memcpy(&Start_Time[object_index].time, + &value.type.Time, sizeof(value.type.Time)); + status = true; + } else { + status = false; + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; + } + break; + case PROP_SHED_DURATION: + if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { + Shed_Duration[object_index] = value.type.Unsigned_Int; + Load_Control_Request_Written[object_index] = true; + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; + } + break; + case PROP_DUTY_WINDOW: + if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { + Duty_Window[object_index] = value.type.Unsigned_Int; + Load_Control_Request_Written[object_index] = true; + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; + } + break; + case PROP_SHED_LEVELS: + if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { + /* re-write the size of the array? */ + if (wp_data->array_index == 0) { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + } else if (wp_data->array_index == BACNET_ARRAY_ALL) { + /* FIXME: write entire array */ + status = true; + } else if (wp_data->array_index <= MAX_SHED_LEVELS) { + Shed_Levels[object_index][wp_data->array_index - 1] = + value.type.Unsigned_Int; + status = true; + } else { + } + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; + } + break; + case PROP_ENABLE: + if (value.tag == BACNET_APPLICATION_TAG_BOOLEAN) { + Load_Control_Enable[object_index] = value.type.Boolean; + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; + } + break; + default: *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case PROP_ENABLE: - if (value.tag == BACNET_APPLICATION_TAG_BOOLEAN) { - Load_Control_Enable[object_index] = value.type.Boolean; - status = true; - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; - break; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + break; } return status; @@ -1041,8 +1061,10 @@ bool Load_Control_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, #include "ctest.h" #if 0 -static void Load_Control_WriteProperty_Request_Shed_Percent(Test * pTest, - int instance, unsigned percent) +static void Load_Control_WriteProperty_Request_Shed_Percent( + Test * pTest, + int instance, + unsigned percent) { bool status = false; BACNET_APPLICATION_DATA_VALUE value; @@ -1062,14 +1084,15 @@ static void Load_Control_WriteProperty_Request_Shed_Percent(Test * pTest, wp_data.application_data_len = bacapp_encode_data(&wp_data.application_data[0], &value); ct_test(pTest, wp_data.application_data_len > 0); - status = Load_Control_Write_Property(&wp_data, - &error_class, &error_code); + status = Load_Control_Write_Property(&wp_data, &error_class, &error_code); ct_test(pTest, status == true); } #endif -static void Load_Control_WriteProperty_Request_Shed_Level(Test * pTest, - int instance, unsigned level) +static void Load_Control_WriteProperty_Request_Shed_Level( + Test * pTest, + int instance, + unsigned level) { bool status = false; BACNET_APPLICATION_DATA_VALUE value; @@ -1089,14 +1112,15 @@ static void Load_Control_WriteProperty_Request_Shed_Level(Test * pTest, wp_data.application_data_len = bacapp_encode_data(&wp_data.application_data[0], &value); ct_test(pTest, wp_data.application_data_len > 0); - status = Load_Control_Write_Property(&wp_data, - &error_class, &error_code); + status = Load_Control_Write_Property(&wp_data, &error_class, &error_code); ct_test(pTest, status == true); } #if 0 -static void Load_Control_WriteProperty_Request_Shed_Amount(Test * pTest, - int instance, float amount) +static void Load_Control_WriteProperty_Request_Shed_Amount( + Test * pTest, + int instance, + float amount) { bool status = false; BACNET_APPLICATION_DATA_VALUE value; @@ -1116,13 +1140,14 @@ static void Load_Control_WriteProperty_Request_Shed_Amount(Test * pTest, wp_data.application_data_len = bacapp_encode_data(&wp_data.application_data[0], &value); ct_test(pTest, wp_data.application_data_len > 0); - status = Load_Control_Write_Property(&wp_data, - &error_class, &error_code); + status = Load_Control_Write_Property(&wp_data, &error_class, &error_code); ct_test(pTest, status == true); } #endif -static void Load_Control_WriteProperty_Enable(Test * pTest, int instance, +static void Load_Control_WriteProperty_Enable( + Test * pTest, + int instance, bool enable) { bool status = false; @@ -1144,13 +1169,14 @@ static void Load_Control_WriteProperty_Enable(Test * pTest, int instance, wp_data.application_data_len = bacapp_encode_data(&wp_data.application_data[0], &value); ct_test(pTest, wp_data.application_data_len > 0); - status = Load_Control_Write_Property(&wp_data, - &error_class, &error_code); + status = Load_Control_Write_Property(&wp_data, &error_class, &error_code); ct_test(pTest, status == true); } -static void Load_Control_WriteProperty_Shed_Duration(Test * pTest, - int instance, unsigned duration) +static void Load_Control_WriteProperty_Shed_Duration( + Test * pTest, + int instance, + unsigned duration) { bool status = false; BACNET_APPLICATION_DATA_VALUE value; @@ -1170,13 +1196,14 @@ static void Load_Control_WriteProperty_Shed_Duration(Test * pTest, wp_data.application_data_len = bacapp_encode_data(&wp_data.application_data[0], &value); ct_test(pTest, wp_data.application_data_len > 0); - status = Load_Control_Write_Property(&wp_data, - &error_class, &error_code); + status = Load_Control_Write_Property(&wp_data, &error_class, &error_code); ct_test(pTest, status == true); } -static void Load_Control_WriteProperty_Duty_Window(Test * pTest, - int instance, unsigned duration) +static void Load_Control_WriteProperty_Duty_Window( + Test * pTest, + int instance, + unsigned duration) { bool status = false; BACNET_APPLICATION_DATA_VALUE value; @@ -1196,12 +1223,12 @@ static void Load_Control_WriteProperty_Duty_Window(Test * pTest, wp_data.application_data_len = bacapp_encode_data(&wp_data.application_data[0], &value); ct_test(pTest, wp_data.application_data_len > 0); - status = Load_Control_Write_Property(&wp_data, - &error_class, &error_code); + status = Load_Control_Write_Property(&wp_data, &error_class, &error_code); ct_test(pTest, status == true); } -static void Load_Control_WriteProperty_Start_Time_Wildcards(Test * pTest, +static void Load_Control_WriteProperty_Start_Time_Wildcards( + Test * pTest, int instance) { int len = 0; @@ -1230,14 +1257,20 @@ static void Load_Control_WriteProperty_Start_Time_Wildcards(Test * pTest, bacapp_encode_data(&wp_data.application_data[len], &value); ct_test(pTest, wp_data.application_data_len > 0); wp_data.application_data_len += len; - status = Load_Control_Write_Property(&wp_data, - &error_class, &error_code); + status = Load_Control_Write_Property(&wp_data, &error_class, &error_code); ct_test(pTest, status == true); } -static void Load_Control_WriteProperty_Start_Time(Test * pTest, int instance, - uint16_t year, uint8_t month, uint8_t day, - uint8_t hour, uint8_t minute, uint8_t seconds, uint8_t hundredths) +static void Load_Control_WriteProperty_Start_Time( + Test * pTest, + int instance, + uint16_t year, + uint8_t month, + uint8_t day, + uint8_t hour, + uint8_t minute, + uint8_t seconds, + uint8_t hundredths) { int len = 0; bool status = false; @@ -1265,12 +1298,12 @@ static void Load_Control_WriteProperty_Start_Time(Test * pTest, int instance, bacapp_encode_data(&wp_data.application_data[len], &value); ct_test(pTest, wp_data.application_data_len > 0); wp_data.application_data_len += len; - status = Load_Control_Write_Property(&wp_data, - &error_class, &error_code); + status = Load_Control_Write_Property(&wp_data, &error_class, &error_code); ct_test(pTest, status == true); } -void testLoadControlStateMachine(Test * pTest) +void testLoadControlStateMachine( + Test * pTest) { unsigned i = 0, j = 0; uint8_t level = 0; @@ -1296,9 +1329,9 @@ void testLoadControlStateMachine(Test * pTest) /* CancelShed - Requested_Shed_Level equal to default value */ Load_Control_Init(); Load_Control_WriteProperty_Request_Shed_Level(pTest, 0, 0); - Load_Control_WriteProperty_Start_Time(pTest, 0, 2007,2,27,15,0,0,0); + Load_Control_WriteProperty_Start_Time(pTest, 0, 2007, 2, 27, 15, 0, 0, 0); Load_Control_WriteProperty_Shed_Duration(pTest, 0, 5); - datetime_set_values(&Current_Time, 2007,2,27,15,0,0,0); + datetime_set_values(&Current_Time, 2007, 2, 27, 15, 0, 0, 0); Load_Control_State_Machine(0); ct_test(pTest, Load_Control_State[0] == SHED_REQUEST_PENDING); Load_Control_State_Machine(0); @@ -1309,8 +1342,8 @@ void testLoadControlStateMachine(Test * pTest) Load_Control_WriteProperty_Enable(pTest, 0, true); Load_Control_WriteProperty_Request_Shed_Level(pTest, 0, 1); Load_Control_WriteProperty_Shed_Duration(pTest, 0, 5); - Load_Control_WriteProperty_Start_Time(pTest, 0, 2007,2,27,15,0,0,0); - datetime_set_values(&Current_Time, 2007,2,28,15,0,0,0); + Load_Control_WriteProperty_Start_Time(pTest, 0, 2007, 2, 27, 15, 0, 0, 0); + datetime_set_values(&Current_Time, 2007, 2, 28, 15, 0, 0, 0); Load_Control_State_Machine(0); ct_test(pTest, Load_Control_State[0] == SHED_REQUEST_PENDING); Load_Control_State_Machine(0); @@ -1321,8 +1354,8 @@ void testLoadControlStateMachine(Test * pTest) Load_Control_WriteProperty_Enable(pTest, 0, true); Load_Control_WriteProperty_Request_Shed_Level(pTest, 0, 1); Load_Control_WriteProperty_Shed_Duration(pTest, 0, 5); - Load_Control_WriteProperty_Start_Time(pTest, 0, 2007,2,27,15,0,0,0); - datetime_set_values(&Current_Time, 2007,2,27,5,0,0,0); + Load_Control_WriteProperty_Start_Time(pTest, 0, 2007, 2, 27, 15, 0, 0, 0); + datetime_set_values(&Current_Time, 2007, 2, 27, 5, 0, 0, 0); Load_Control_State_Machine(0); ct_test(pTest, Load_Control_State[0] == SHED_REQUEST_PENDING); Load_Control_State_Machine(0); @@ -1336,7 +1369,7 @@ void testLoadControlStateMachine(Test * pTest) Load_Control_WriteProperty_Duty_Window(pTest, 0, 60); Load_Control_State_Machine(0); ct_test(pTest, Load_Control_State[0] == SHED_REQUEST_PENDING); - Load_Control_WriteProperty_Start_Time(pTest, 0, 2007,2,27,15,0,0,1); + Load_Control_WriteProperty_Start_Time(pTest, 0, 2007, 2, 27, 15, 0, 0, 1); Load_Control_State_Machine(0); ct_test(pTest, Load_Control_State[0] == SHED_REQUEST_PENDING); Load_Control_State_Machine(0); @@ -1349,21 +1382,21 @@ void testLoadControlStateMachine(Test * pTest) Load_Control_WriteProperty_Enable(pTest, 0, true); Load_Control_WriteProperty_Request_Shed_Level(pTest, 0, 1); Load_Control_WriteProperty_Shed_Duration(pTest, 0, 120); - Load_Control_WriteProperty_Start_Time(pTest, 0, 2007,2,27,15,0,0,0); - datetime_set_values(&Current_Time, 2007,2,27,5,0,0,0); + Load_Control_WriteProperty_Start_Time(pTest, 0, 2007, 2, 27, 15, 0, 0, 0); + datetime_set_values(&Current_Time, 2007, 2, 27, 5, 0, 0, 0); Load_Control_State_Machine(0); ct_test(pTest, Load_Control_State[0] == SHED_REQUEST_PENDING); Load_Control_State_Machine(0); ct_test(pTest, Load_Control_State[0] == SHED_REQUEST_PENDING); /* set to lowest value so we cannot meet the shed level */ - datetime_set_values(&Current_Time, 2007,2,27,16,0,0,0); + datetime_set_values(&Current_Time, 2007, 2, 27, 16, 0, 0, 0); Analog_Output_Present_Value_Set(0, 0, 16); Load_Control_State_Machine(0); ct_test(pTest, Load_Control_State[0] == SHED_NON_COMPLIANT); Load_Control_State_Machine(0); ct_test(pTest, Load_Control_State[0] == SHED_NON_COMPLIANT); /* FinishedUnsuccessfulShed */ - datetime_set_values(&Current_Time, 2007,2,27,23,0,0,0); + datetime_set_values(&Current_Time, 2007, 2, 27, 23, 0, 0, 0); Load_Control_State_Machine(0); ct_test(pTest, Load_Control_State[0] == SHED_INACTIVE); @@ -1372,44 +1405,45 @@ void testLoadControlStateMachine(Test * pTest) Load_Control_WriteProperty_Enable(pTest, 0, true); Load_Control_WriteProperty_Request_Shed_Level(pTest, 0, 1); Load_Control_WriteProperty_Shed_Duration(pTest, 0, 120); - Load_Control_WriteProperty_Start_Time(pTest, 0, 2007,2,27,15,0,0,0); - datetime_set_values(&Current_Time, 2007,2,27,5,0,0,0); + Load_Control_WriteProperty_Start_Time(pTest, 0, 2007, 2, 27, 15, 0, 0, 0); + datetime_set_values(&Current_Time, 2007, 2, 27, 5, 0, 0, 0); Load_Control_State_Machine(0); ct_test(pTest, Load_Control_State[0] == SHED_REQUEST_PENDING); Load_Control_State_Machine(0); ct_test(pTest, Load_Control_State[0] == SHED_REQUEST_PENDING); /* set to lowest value so we cannot meet the shed level */ - datetime_set_values(&Current_Time, 2007,2,27,16,0,0,0); + datetime_set_values(&Current_Time, 2007, 2, 27, 16, 0, 0, 0); Analog_Output_Present_Value_Set(0, 0, 16); Load_Control_State_Machine(0); ct_test(pTest, Load_Control_State[0] == SHED_NON_COMPLIANT); Load_Control_State_Machine(0); ct_test(pTest, Load_Control_State[0] == SHED_NON_COMPLIANT); /* FinishedUnsuccessfulShed */ - Load_Control_WriteProperty_Start_Time(pTest, 0, 2007,2,27,16,0,0,0); + Load_Control_WriteProperty_Start_Time(pTest, 0, 2007, 2, 27, 16, 0, 0, 0); Load_Control_State_Machine(0); ct_test(pTest, Load_Control_State[0] == SHED_REQUEST_PENDING); Load_Control_State_Machine(0); ct_test(pTest, Load_Control_State[0] == SHED_REQUEST_PENDING); - datetime_set_values(&Current_Time, 2007,2,27,16,0,1,0); + datetime_set_values(&Current_Time, 2007, 2, 27, 16, 0, 1, 0); Load_Control_State_Machine(0); ct_test(pTest, Load_Control_State[0] == SHED_NON_COMPLIANT); /* CanNowComplyWithShed */ Analog_Output_Present_Value_Set(0, 100, 16); - datetime_set_values(&Current_Time, 2007,2,27,16,0,2,0); + datetime_set_values(&Current_Time, 2007, 2, 27, 16, 0, 2, 0); Load_Control_State_Machine(0); ct_test(pTest, Load_Control_State[0] == SHED_COMPLIANT); level = Analog_Output_Present_Value(0); ct_test(pTest, level == 90); /* FinishedSuccessfulShed */ - datetime_set_values(&Current_Time, 2007,2,27,23,0,0,0); + datetime_set_values(&Current_Time, 2007, 2, 27, 23, 0, 0, 0); Load_Control_State_Machine(0); ct_test(pTest, Load_Control_State[0] == SHED_INACTIVE); level = Analog_Output_Present_Value(0); ct_test(pTest, level == 100); } -void testLoadControl(Test * pTest) +void testLoadControl( + Test * pTest) { uint8_t apdu[MAX_APDU] = { 0 }; int len = 0; @@ -1423,8 +1457,7 @@ void testLoadControl(Test * pTest) len = Load_Control_Encode_Property_APDU(&apdu[0], instance, - PROP_OBJECT_IDENTIFIER, - BACNET_ARRAY_ALL, &error_class, &error_code); + PROP_OBJECT_IDENTIFIER, BACNET_ARRAY_ALL, &error_class, &error_code); ct_test(pTest, len != 0); len = decode_tag_number_and_value(&apdu[0], &tag_number, &len_value); ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_OBJECT_ID); @@ -1437,7 +1470,8 @@ void testLoadControl(Test * pTest) } #ifdef TEST_LOAD_CONTROL -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -1456,5 +1490,5 @@ int main(void) return 0; } -#endif /* TEST_LOAD_CONTROL */ -#endif /* TEST */ +#endif /* TEST_LOAD_CONTROL */ +#endif /* TEST */ diff --git a/bacnet-stack/demo/object/lo.c b/bacnet-stack/demo/object/lo.c index b69d1b41..9374feb6 100644 --- a/bacnet-stack/demo/object/lo.c +++ b/bacnet-stack/demo/object/lo.c @@ -32,7 +32,7 @@ #include "bacdcode.h" #include "bacenum.h" #include "bacapp.h" -#include "config.h" /* the custom stuff */ +#include "config.h" /* the custom stuff */ #include "wp.h" #define MAX_LIGHTING_OUTPUTS 5 @@ -49,12 +49,12 @@ of the optional parameters, we represent them interally as integers. */ typedef struct LightingCommand { - BACNET_LIGHTING_OPERATION operation; - uint8_t level; /* 0..100 percent, 255=not used */ - uint8_t ramp_rate; /* 0..100 percent-per-second, 255=not used */ - uint8_t step_increment; /* 0..100 amount to step, 255=not used */ - uint16_t fade_time; /* 1..65535 seconds to transition, 0=not used */ - uint16_t duration; /* 1..65535 minutes until relinquish, 0=not used */ + BACNET_LIGHTING_OPERATION operation; + uint8_t level; /* 0..100 percent, 255=not used */ + uint8_t ramp_rate; /* 0..100 percent-per-second, 255=not used */ + uint8_t step_increment; /* 0..100 amount to step, 255=not used */ + uint16_t fade_time; /* 1..65535 seconds to transition, 0=not used */ + uint16_t duration; /* 1..65535 minutes until relinquish, 0=not used */ } BACNET_LIGHTING_COMMAND; /* Here is our Priority Array. They are supposed to be Real, but */ @@ -76,51 +76,46 @@ static BACNET_LIGHTING_COMMAND Lighting_Command[MAX_LIGHTING_OUTPUTS]; /* we need to have our arrays initialized before answering any calls */ static bool Lighting_Output_Initialized = false; -int Lighting_Output_Encode_Lighting_Command(uint8_t * apdu, +int Lighting_Output_Encode_Lighting_Command( + uint8_t * apdu, BACNET_LIGHTING_COMMAND * data) { - int apdu_len = 0; /* total length of the apdu, return value */ - int len = 0; /* total length of the apdu, return value */ + int apdu_len = 0; /* total length of the apdu, return value */ + int len = 0; /* total length of the apdu, return value */ float real_value = 0.0; uint32_t unsigned_value = 0; if (apdu) { - len = encode_context_enumerated(&apdu[apdu_len], 0, - data->operation); + len = encode_context_enumerated(&apdu[apdu_len], 0, data->operation); apdu_len += len; /* optional level? */ if (data->level != 255) { real_value = data->level; - len = encode_context_real(&apdu[apdu_len], 1, - real_value); + len = encode_context_real(&apdu[apdu_len], 1, real_value); apdu_len += len; } /* optional ramp-rate */ if (data->ramp_rate != 255) { real_value = data->ramp_rate; - len = encode_context_real(&apdu[apdu_len], 2, - real_value); + len = encode_context_real(&apdu[apdu_len], 2, real_value); apdu_len += len; } /* optional step increment */ if (data->step_increment != 255) { real_value = data->step_increment; - len = encode_context_real(&apdu[apdu_len], 3, - real_value); + len = encode_context_real(&apdu[apdu_len], 3, real_value); apdu_len += len; } /* optional fade time */ if (data->fade_time != 0) { real_value = data->fade_time; - len = encode_context_real(&apdu[apdu_len], 4, - real_value); + len = encode_context_real(&apdu[apdu_len], 4, real_value); apdu_len += len; } /* optional duration */ if (data->duration != 0) { unsigned_value = data->duration; - len = encode_context_unsigned(&apdu[apdu_len], 5, - unsigned_value); + len = encode_context_unsigned(&apdu[apdu_len], 5, unsigned_value); apdu_len += len; } } @@ -128,18 +123,20 @@ int Lighting_Output_Encode_Lighting_Command(uint8_t * apdu, return apdu_len; } -int Lighting_Output_Decode_Lighting_Command(uint8_t * apdu, - unsigned apdu_max_len, BACNET_LIGHTING_COMMAND * data) +int Lighting_Output_Decode_Lighting_Command( + uint8_t * apdu, + unsigned apdu_max_len, + BACNET_LIGHTING_COMMAND * data) { int len = 0; int apdu_len = 0; int tag_len = 0; uint8_t tag_number = 0; uint32_t len_value_type = 0; - int type = 0; /* for decoding */ - int property = 0; /* for decoding */ + int type = 0; /* for decoding */ + int property = 0; /* for decoding */ uint32_t unsigned_value = 0; - int i = 0; /* loop counter */ + int i = 0; /* loop counter */ float real_value = 0.0; /* check for value pointers */ @@ -150,7 +147,9 @@ int Lighting_Output_Decode_Lighting_Command(uint8_t * apdu, len = decode_tag_number_and_value(&apdu[apdu_len], &tag_number, &len_value_type); apdu_len += len; - len = decode_enumerated(&apdu[apdu_len], len_value_type, &data->operation); + len = + decode_enumerated(&apdu[apdu_len], len_value_type, + &data->operation); apdu_len += len; /* Tag 1: level - OPTIONAL */ if (decode_is_context_tag(&apdu[apdu_len], 1)) { @@ -171,7 +170,8 @@ int Lighting_Output_Decode_Lighting_Command(uint8_t * apdu, } -void Lighting_Output_Init(void) +void Lighting_Output_Init( + void) { unsigned i, j; @@ -197,7 +197,8 @@ void Lighting_Output_Init(void) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need validate that the */ /* given instance exists */ -bool Lighting_Output_Valid_Instance(uint32_t object_instance) +bool Lighting_Output_Valid_Instance( + uint32_t object_instance) { Lighting_Output_Init(); if (object_instance < MAX_LIGHTING_OUTPUTS) @@ -208,7 +209,8 @@ bool Lighting_Output_Valid_Instance(uint32_t object_instance) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then count how many you have */ -unsigned Lighting_Output_Count(void) +unsigned Lighting_Output_Count( + void) { Lighting_Output_Init(); return MAX_LIGHTING_OUTPUTS; @@ -217,7 +219,8 @@ unsigned Lighting_Output_Count(void) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need to return the instance */ /* that correlates to the correct index */ -uint32_t Lighting_Output_Index_To_Instance(unsigned index) +uint32_t Lighting_Output_Index_To_Instance( + unsigned index) { Lighting_Output_Init(); return index; @@ -226,7 +229,8 @@ uint32_t Lighting_Output_Index_To_Instance(unsigned index) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need to return the index */ /* that correlates to the correct instance number */ -unsigned Lighting_Output_Instance_To_Index(uint32_t object_instance) +unsigned Lighting_Output_Instance_To_Index( + uint32_t object_instance) { unsigned index = MAX_LIGHTING_OUTPUTS; @@ -237,7 +241,8 @@ unsigned Lighting_Output_Instance_To_Index(uint32_t object_instance) return index; } -float Lighting_Output_Present_Value(uint32_t object_instance) +float Lighting_Output_Present_Value( + uint32_t object_instance) { float value = LIGHTING_RELINQUISH_DEFAULT; unsigned index = 0; @@ -257,10 +262,11 @@ float Lighting_Output_Present_Value(uint32_t object_instance) return value; } -unsigned Lighting_Output_Present_Value_Priority(uint32_t object_instance) +unsigned Lighting_Output_Present_Value_Priority( + uint32_t object_instance) { - unsigned index = 0; /* instance to index conversion */ - unsigned i = 0; /* loop counter */ + unsigned index = 0; /* instance to index conversion */ + unsigned i = 0; /* loop counter */ unsigned priority = 0; /* return value */ Lighting_Output_Init(); @@ -277,8 +283,10 @@ unsigned Lighting_Output_Present_Value_Priority(uint32_t object_instance) return priority; } -bool Lighting_Output_Present_Value_Set(uint32_t object_instance, - float value, unsigned priority) +bool Lighting_Output_Present_Value_Set( + uint32_t object_instance, + float value, + unsigned priority) { unsigned index = 0; bool status = false; @@ -288,7 +296,7 @@ bool Lighting_Output_Present_Value_Set(uint32_t object_instance, if (priority && (priority <= BACNET_MAX_PRIORITY) && (priority != 6 /* reserved */ ) && (value >= 0.0) && (value <= 100.0)) { - Lighting_Output_Level[index][priority-1] = (uint8_t) value; + Lighting_Output_Level[index][priority - 1] = (uint8_t) value; /* Note: you could set the physical output here to the next highest priority, or to the relinquish default if no priorities are set. @@ -302,7 +310,8 @@ bool Lighting_Output_Present_Value_Set(uint32_t object_instance, return status; } -bool Lighting_Output_Present_Value_Relinquish(uint32_t object_instance, +bool Lighting_Output_Present_Value_Relinquish( + uint32_t object_instance, int priority) { unsigned index = 0; @@ -312,7 +321,7 @@ bool Lighting_Output_Present_Value_Relinquish(uint32_t object_instance, if (index < MAX_LIGHTING_OUTPUTS) { if (priority && (priority <= BACNET_MAX_PRIORITY) && (priority != 6 /* reserved */ )) { - Lighting_Output_Level[index][priority-1] = LIGHTING_LEVEL_NULL; + Lighting_Output_Level[index][priority - 1] = LIGHTING_LEVEL_NULL; /* Note: you could set the physical output here to the next highest priority, or to the relinquish default if no priorities are set. @@ -326,7 +335,8 @@ bool Lighting_Output_Present_Value_Relinquish(uint32_t object_instance, return status; } -float Lighting_Output_Progress_Value(uint32_t object_instance) +float Lighting_Output_Progress_Value( + uint32_t object_instance) { float value = LIGHTING_RELINQUISH_DEFAULT; unsigned index = 0; @@ -341,7 +351,8 @@ float Lighting_Output_Progress_Value(uint32_t object_instance) } /* note: the object name must be unique within this device */ -char *Lighting_Output_Name(uint32_t object_instance) +char *Lighting_Output_Name( + uint32_t object_instance) { static char text_string[32] = ""; /* okay for single thread */ @@ -354,14 +365,16 @@ char *Lighting_Output_Name(uint32_t object_instance) } /* return apdu len, or -1 on error */ -int Lighting_Output_Encode_Property_APDU(uint8_t * apdu, +int Lighting_Output_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { int len = 0; - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ BACNET_BIT_STRING bit_string; BACNET_CHARACTER_STRING char_string; float real_value = (float) 1.414; @@ -371,118 +384,129 @@ int Lighting_Output_Encode_Property_APDU(uint8_t * apdu, Lighting_Output_Init(); switch (property) { - case PROP_OBJECT_IDENTIFIER: - apdu_len = encode_application_object_id(&apdu[0], OBJECT_LIGHTING_OUTPUT, - object_instance); - break; - case PROP_OBJECT_NAME: - case PROP_DESCRIPTION: - /* object name must be unique in this device. */ - /* FIXME: description could be writable and different than object name */ - characterstring_init_ansi(&char_string, - Lighting_Output_Name(object_instance)); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_OBJECT_TYPE: - apdu_len = - encode_application_enumerated(&apdu[0], OBJECT_LIGHTING_OUTPUT); - break; - case PROP_PRESENT_VALUE: - real_value = Lighting_Output_Present_Value(object_instance); - apdu_len = encode_application_real(&apdu[0], real_value); - break; - case PROP_PROGRESS_VALUE: - real_value = Lighting_Output_Progress_Value(object_instance); - apdu_len = encode_application_real(&apdu[0], real_value); - break; - case PROP_LIGHTING_COMMAND: - apdu_len = Lighting_Output_Encode_Lighting_Command(&apdu[0], - &Lighting_Command[object_instance]); - break; - case PROP_STATUS_FLAGS: - bitstring_init(&bit_string); - bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); - apdu_len = encode_application_bitstring(&apdu[0], &bit_string); - break; - case PROP_EVENT_STATE: - apdu_len = encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); - break; - case PROP_OUT_OF_SERVICE: - object_index = Lighting_Output_Instance_To_Index(object_instance); - state = Lighting_Output_Out_Of_Service[object_index]; - apdu_len = encode_application_boolean(&apdu[0], state); - break; - case PROP_UNITS: - apdu_len = encode_application_enumerated(&apdu[0], UNITS_PERCENT); - break; - case PROP_PRIORITY_ARRAY: - /* Array element zero is the number of elements in the array */ - if (array_index == 0) + case PROP_OBJECT_IDENTIFIER: apdu_len = - encode_application_unsigned(&apdu[0], BACNET_MAX_PRIORITY); - /* if no index was specified, then try to encode the entire list */ - /* into one packet. */ - else if (array_index == BACNET_ARRAY_ALL) { - object_index = - Lighting_Output_Instance_To_Index(object_instance); - for (i = 0; i < BACNET_MAX_PRIORITY; i++) { - /* FIXME: check if we have room before adding it to APDU */ - if (Lighting_Output_Level[object_index][i] == LIGHTING_LEVEL_NULL) - len = encode_application_null(&apdu[apdu_len]); - else { - real_value = Lighting_Output_Level[object_index][i]; - len = encode_application_real(&apdu[apdu_len], real_value); - } - /* add it if we have room */ - if ((apdu_len + len) < MAX_APDU) - apdu_len += len; - else { - *error_class = ERROR_CLASS_SERVICES; - *error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT; - apdu_len = -1; - break; - } - } - } else { - object_index = - Lighting_Output_Instance_To_Index(object_instance); - if (array_index <= BACNET_MAX_PRIORITY) { - if (Lighting_Output_Level[object_index][array_index - 1] == - LIGHTING_LEVEL_NULL) - apdu_len = encode_application_null(&apdu[0]); - else { - real_value = - Lighting_Output_Level[object_index][array_index - 1]; - apdu_len = encode_application_real(&apdu[0], real_value); + encode_application_object_id(&apdu[0], OBJECT_LIGHTING_OUTPUT, + object_instance); + break; + case PROP_OBJECT_NAME: + case PROP_DESCRIPTION: + /* object name must be unique in this device. */ + /* FIXME: description could be writable and different than object name */ + characterstring_init_ansi(&char_string, + Lighting_Output_Name(object_instance)); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_OBJECT_TYPE: + apdu_len = + encode_application_enumerated(&apdu[0], + OBJECT_LIGHTING_OUTPUT); + break; + case PROP_PRESENT_VALUE: + real_value = Lighting_Output_Present_Value(object_instance); + apdu_len = encode_application_real(&apdu[0], real_value); + break; + case PROP_PROGRESS_VALUE: + real_value = Lighting_Output_Progress_Value(object_instance); + apdu_len = encode_application_real(&apdu[0], real_value); + break; + case PROP_LIGHTING_COMMAND: + apdu_len = Lighting_Output_Encode_Lighting_Command(&apdu[0], + &Lighting_Command[object_instance]); + break; + case PROP_STATUS_FLAGS: + bitstring_init(&bit_string); + bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); + apdu_len = encode_application_bitstring(&apdu[0], &bit_string); + break; + case PROP_EVENT_STATE: + apdu_len = + encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); + break; + case PROP_OUT_OF_SERVICE: + object_index = Lighting_Output_Instance_To_Index(object_instance); + state = Lighting_Output_Out_Of_Service[object_index]; + apdu_len = encode_application_boolean(&apdu[0], state); + break; + case PROP_UNITS: + apdu_len = encode_application_enumerated(&apdu[0], UNITS_PERCENT); + break; + case PROP_PRIORITY_ARRAY: + /* Array element zero is the number of elements in the array */ + if (array_index == 0) + apdu_len = + encode_application_unsigned(&apdu[0], BACNET_MAX_PRIORITY); + /* if no index was specified, then try to encode the entire list */ + /* into one packet. */ + else if (array_index == BACNET_ARRAY_ALL) { + object_index = + Lighting_Output_Instance_To_Index(object_instance); + for (i = 0; i < BACNET_MAX_PRIORITY; i++) { + /* FIXME: check if we have room before adding it to APDU */ + if (Lighting_Output_Level[object_index][i] == + LIGHTING_LEVEL_NULL) + len = encode_application_null(&apdu[apdu_len]); + else { + real_value = Lighting_Output_Level[object_index][i]; + len = + encode_application_real(&apdu[apdu_len], + real_value); + } + /* add it if we have room */ + if ((apdu_len + len) < MAX_APDU) + apdu_len += len; + else { + *error_class = ERROR_CLASS_SERVICES; + *error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT; + apdu_len = -1; + break; + } } } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_ARRAY_INDEX; - apdu_len = -1; + object_index = + Lighting_Output_Instance_To_Index(object_instance); + if (array_index <= BACNET_MAX_PRIORITY) { + if (Lighting_Output_Level[object_index][array_index - 1] == + LIGHTING_LEVEL_NULL) + apdu_len = encode_application_null(&apdu[0]); + else { + real_value = + Lighting_Output_Level[object_index][array_index - + 1]; + apdu_len = + encode_application_real(&apdu[0], real_value); + } + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_ARRAY_INDEX; + apdu_len = -1; + } } - } - break; - case PROP_RELINQUISH_DEFAULT: - real_value = LIGHTING_RELINQUISH_DEFAULT; - apdu_len = encode_application_real(&apdu[0], real_value); - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_UNKNOWN_PROPERTY; - apdu_len = -1; - break; + break; + case PROP_RELINQUISH_DEFAULT: + real_value = LIGHTING_RELINQUISH_DEFAULT; + apdu_len = encode_application_real(&apdu[0], real_value); + break; + default: + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_UNKNOWN_PROPERTY; + apdu_len = -1; + break; } return apdu_len; } /* returns true if successful */ -bool Lighting_Output_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) +bool Lighting_Output_Write_Property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { bool status = false; /* return value */ unsigned int object_index = 0; @@ -502,68 +526,70 @@ bool Lighting_Output_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, /* FIXME: len < application_data_len: more data? */ /* FIXME: len == 0: unable to decode? */ switch (wp_data->object_property) { - case PROP_PRESENT_VALUE: - if (value.tag == BACNET_APPLICATION_TAG_REAL) { - /* Command priority 6 is reserved for use by Minimum On/Off - algorithm and may not be used for other purposes in any - object. */ - status = - Lighting_Output_Present_Value_Set(wp_data->object_instance, - value.type.Real, wp_data->priority); - if (wp_data->priority == 6) { + case PROP_PRESENT_VALUE: + if (value.tag == BACNET_APPLICATION_TAG_REAL) { /* Command priority 6 is reserved for use by Minimum On/Off algorithm and may not be used for other purposes in any object. */ + status = + Lighting_Output_Present_Value_Set(wp_data->object_instance, + value.type.Real, wp_data->priority); + if (wp_data->priority == 6) { + /* Command priority 6 is reserved for use by Minimum On/Off + algorithm and may not be used for other purposes in any + object. */ + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + } else if (!status) { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } + } else if (value.tag == BACNET_APPLICATION_TAG_NULL) { + level = LIGHTING_LEVEL_NULL; + object_index = + Lighting_Output_Instance_To_Index(wp_data-> + object_instance); + status = + Lighting_Output_Present_Value_Relinquish(wp_data-> + object_instance, wp_data->priority); + if (wp_data->priority == 6) { + /* Command priority 6 is reserved for use by Minimum On/Off + algorithm and may not be used for other purposes in any + object. */ + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + } else if (!status) { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } + } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; - } else if (!status) { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } - } else if (value.tag == BACNET_APPLICATION_TAG_NULL) { - level = LIGHTING_LEVEL_NULL; - object_index = - Lighting_Output_Instance_To_Index(wp_data->object_instance); - status = - Lighting_Output_Present_Value_Relinquish(wp_data-> - object_instance, wp_data->priority); - if (wp_data->priority == 6) { - /* Command priority 6 is reserved for use by Minimum On/Off - algorithm and may not be used for other purposes in any - object. */ + break; + case PROP_LIGHTING_COMMAND: + /* FIXME: error checking? */ + Lighting_Output_Decode_Lighting_Command(wp_data->application_data, + wp_data->application_data_len, + &Lighting_Command[wp_data->object_instance]); + break; + case PROP_OUT_OF_SERVICE: + if (value.tag == BACNET_APPLICATION_TAG_BOOLEAN) { + object_index = + Lighting_Output_Instance_To_Index(wp_data-> + object_instance); + Lighting_Output_Out_Of_Service[object_index] = + value.type.Boolean; + status = true; + } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; - } else if (!status) { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } - } else { + break; + default: *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case PROP_LIGHTING_COMMAND: - /* FIXME: error checking? */ - Lighting_Output_Decode_Lighting_Command(wp_data->application_data, - wp_data->application_data_len, - &Lighting_Command[wp_data->object_instance]); - break; - case PROP_OUT_OF_SERVICE: - if (value.tag == BACNET_APPLICATION_TAG_BOOLEAN) { - object_index = - Lighting_Output_Instance_To_Index(wp_data->object_instance); - Lighting_Output_Out_Of_Service[object_index] = - value.type.Boolean; - status = true; - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; - break; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + break; } return status; @@ -575,7 +601,8 @@ bool Lighting_Output_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, #include #include "ctest.h" -void testLightingOutput(Test * pTest) +void testLightingOutput( + Test * pTest) { uint8_t apdu[MAX_APDU] = { 0 }; int len = 0; @@ -590,8 +617,7 @@ void testLightingOutput(Test * pTest) len = Lighting_Output_Encode_Property_APDU(&apdu[0], instance, - PROP_OBJECT_IDENTIFIER, - BACNET_ARRAY_ALL, &error_class, &error_code); + PROP_OBJECT_IDENTIFIER, BACNET_ARRAY_ALL, &error_class, &error_code); ct_test(pTest, len != 0); len = decode_tag_number_and_value(&apdu[0], &tag_number, &len_value); ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_OBJECT_ID); @@ -604,7 +630,8 @@ void testLightingOutput(Test * pTest) } #ifdef TEST_LIGHTING_OUTPUT -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -621,5 +648,5 @@ int main(void) return 0; } -#endif /* TEST_LIGHTING_INPUT */ -#endif /* TEST */ +#endif /* TEST_LIGHTING_INPUT */ +#endif /* TEST */ diff --git a/bacnet-stack/demo/object/lsp.c b/bacnet-stack/demo/object/lsp.c index 4c4ac7e7..fa4f70c6 100644 --- a/bacnet-stack/demo/object/lsp.c +++ b/bacnet-stack/demo/object/lsp.c @@ -32,14 +32,13 @@ #include "bacdcode.h" #include "bacenum.h" #include "bacapp.h" -#include "config.h" /* the custom stuff */ +#include "config.h" /* the custom stuff */ #include "wp.h" #define MAX_LIFE_SAFETY_POINTS 7 /* Here are our stored levels.*/ -static BACNET_LIFE_SAFETY_MODE - Life_Safety_Point_Mode[MAX_LIFE_SAFETY_POINTS]; +static BACNET_LIFE_SAFETY_MODE Life_Safety_Point_Mode[MAX_LIFE_SAFETY_POINTS]; static BACNET_LIFE_SAFETY_STATE Life_Safety_Point_State[MAX_LIFE_SAFETY_POINTS]; static BACNET_SILENCED_STATE @@ -51,8 +50,7 @@ static BACNET_LIFE_SAFETY_OPERATION static bool Life_Safety_Point_Out_Of_Service[MAX_LIFE_SAFETY_POINTS]; /* These three arrays are used by the ReadPropertyMultiple handler */ -static const int Life_Safety_Point_Properties_Required[] = -{ +static const int Life_Safety_Point_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, PROP_OBJECT_NAME, PROP_OBJECT_TYPE, @@ -68,14 +66,12 @@ static const int Life_Safety_Point_Properties_Required[] = -1 }; -static const int Life_Safety_Point_Properties_Optional[] = -{ +static const int Life_Safety_Point_Properties_Optional[] = { PROP_DESCRIPTION, -1 }; -static const int Life_Safety_Point_Properties_Proprietary[] = -{ +static const int Life_Safety_Point_Properties_Proprietary[] = { -1 }; @@ -94,7 +90,8 @@ void Life_Safety_Point_Property_Lists( return; } -void Life_Safety_Point_Init(void) +void Life_Safety_Point_Init( + void) { static bool initialized = false; unsigned i; @@ -106,8 +103,7 @@ void Life_Safety_Point_Init(void) for (i = 0; i < MAX_LIFE_SAFETY_POINTS; i++) { Life_Safety_Point_Mode[i] = LIFE_SAFETY_MODE_DEFAULT; Life_Safety_Point_State[i] = LIFE_SAFETY_STATE_QUIET; - Life_Safety_Point_Silenced_State[i] = - SILENCED_STATE_UNSILENCED; + Life_Safety_Point_Silenced_State[i] = SILENCED_STATE_UNSILENCED; Life_Safety_Point_Operation[i] = LIFE_SAFETY_OPERATION_NONE; } } @@ -118,7 +114,8 @@ void Life_Safety_Point_Init(void) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need validate that the */ /* given instance exists */ -bool Life_Safety_Point_Valid_Instance(uint32_t object_instance) +bool Life_Safety_Point_Valid_Instance( + uint32_t object_instance) { Life_Safety_Point_Init(); if (object_instance < MAX_LIFE_SAFETY_POINTS) @@ -129,7 +126,8 @@ bool Life_Safety_Point_Valid_Instance(uint32_t object_instance) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then count how many you have */ -unsigned Life_Safety_Point_Count(void) +unsigned Life_Safety_Point_Count( + void) { Life_Safety_Point_Init(); return MAX_LIFE_SAFETY_POINTS; @@ -138,7 +136,8 @@ unsigned Life_Safety_Point_Count(void) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need to return the instance */ /* that correlates to the correct index */ -uint32_t Life_Safety_Point_Index_To_Instance(unsigned index) +uint32_t Life_Safety_Point_Index_To_Instance( + unsigned index) { Life_Safety_Point_Init(); return index; @@ -147,7 +146,8 @@ uint32_t Life_Safety_Point_Index_To_Instance(unsigned index) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need to return the index */ /* that correlates to the correct instance number */ -unsigned Life_Safety_Point_Instance_To_Index(uint32_t object_instance) +unsigned Life_Safety_Point_Instance_To_Index( + uint32_t object_instance) { unsigned index = MAX_LIFE_SAFETY_POINTS; @@ -158,8 +158,8 @@ unsigned Life_Safety_Point_Instance_To_Index(uint32_t object_instance) return index; } -static BACNET_LIFE_SAFETY_STATE Life_Safety_Point_Present_Value(uint32_t - object_instance) +static BACNET_LIFE_SAFETY_STATE Life_Safety_Point_Present_Value( + uint32_t object_instance) { BACNET_LIFE_SAFETY_STATE present_value = LIFE_SAFETY_STATE_QUIET; unsigned index = 0; @@ -173,7 +173,8 @@ static BACNET_LIFE_SAFETY_STATE Life_Safety_Point_Present_Value(uint32_t } /* note: the object name must be unique within this device */ -char *Life_Safety_Point_Name(uint32_t object_instance) +char *Life_Safety_Point_Name( + uint32_t object_instance) { static char text_string[32] = ""; /* okay for single thread */ @@ -186,14 +187,16 @@ char *Life_Safety_Point_Name(uint32_t object_instance) } /* return apdu len, or -1 on error */ -int Life_Safety_Point_Encode_Property_APDU(uint8_t * apdu, +int Life_Safety_Point_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { int len = 0; - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ BACNET_BIT_STRING bit_string; BACNET_CHARACTER_STRING char_string; BACNET_LIFE_SAFETY_STATE present_value = LIFE_SAFETY_STATE_QUIET; @@ -204,88 +207,93 @@ int Life_Safety_Point_Encode_Property_APDU(uint8_t * apdu, bool state = false; BACNET_RELIABILITY reliability = RELIABILITY_NO_FAULT_DETECTED; - (void) array_index; /* currently not used */ + (void) array_index; /* currently not used */ Life_Safety_Point_Init(); switch (property) { - case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], OBJECT_LIFE_SAFETY_POINT, - object_instance); - break; - case PROP_OBJECT_NAME: - case PROP_DESCRIPTION: - characterstring_init_ansi(&char_string, - Life_Safety_Point_Name(object_instance)); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_OBJECT_TYPE: - apdu_len = - encode_application_enumerated(&apdu[0], OBJECT_LIFE_SAFETY_POINT); - break; - case PROP_PRESENT_VALUE: - present_value = Life_Safety_Point_Present_Value(object_instance); - apdu_len = encode_application_enumerated(&apdu[0], present_value); - break; - case PROP_STATUS_FLAGS: - bitstring_init(&bit_string); - bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); - apdu_len = encode_application_bitstring(&apdu[0], &bit_string); - break; - case PROP_EVENT_STATE: - apdu_len = encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); - break; - case PROP_OUT_OF_SERVICE: - object_index = - Life_Safety_Point_Instance_To_Index(object_instance); - state = Life_Safety_Point_Out_Of_Service[object_index]; - apdu_len = encode_application_boolean(&apdu[0], state); - break; - case PROP_RELIABILITY: - /* see standard for details about this property */ - reliability = RELIABILITY_NO_FAULT_DETECTED; - apdu_len = encode_application_enumerated(&apdu[0], reliability); - break; - case PROP_MODE: - object_index = - Life_Safety_Point_Instance_To_Index(object_instance); - mode = Life_Safety_Point_Mode[object_index]; - apdu_len = encode_application_enumerated(&apdu[0], mode); - break; - case PROP_ACCEPTED_MODES: - for (mode = MIN_LIFE_SAFETY_MODE; mode < MAX_LIFE_SAFETY_MODE; - mode++) { - len = encode_application_enumerated(&apdu[apdu_len], mode); - apdu_len += len; - } - break; - case PROP_SILENCED: - object_index = - Life_Safety_Point_Instance_To_Index(object_instance); - silenced_state = Life_Safety_Point_Silenced_State[object_index]; - apdu_len = encode_application_enumerated(&apdu[0], silenced_state); - break; - case PROP_OPERATION_EXPECTED: - object_index = - Life_Safety_Point_Instance_To_Index(object_instance); - operation = Life_Safety_Point_Operation[object_index]; - apdu_len = encode_application_enumerated(&apdu[0], operation); - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_UNKNOWN_PROPERTY; - apdu_len = -1; - break; + case PROP_OBJECT_IDENTIFIER: + apdu_len = + encode_application_object_id(&apdu[0], + OBJECT_LIFE_SAFETY_POINT, object_instance); + break; + case PROP_OBJECT_NAME: + case PROP_DESCRIPTION: + characterstring_init_ansi(&char_string, + Life_Safety_Point_Name(object_instance)); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_OBJECT_TYPE: + apdu_len = + encode_application_enumerated(&apdu[0], + OBJECT_LIFE_SAFETY_POINT); + break; + case PROP_PRESENT_VALUE: + present_value = Life_Safety_Point_Present_Value(object_instance); + apdu_len = encode_application_enumerated(&apdu[0], present_value); + break; + case PROP_STATUS_FLAGS: + bitstring_init(&bit_string); + bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); + apdu_len = encode_application_bitstring(&apdu[0], &bit_string); + break; + case PROP_EVENT_STATE: + apdu_len = + encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); + break; + case PROP_OUT_OF_SERVICE: + object_index = + Life_Safety_Point_Instance_To_Index(object_instance); + state = Life_Safety_Point_Out_Of_Service[object_index]; + apdu_len = encode_application_boolean(&apdu[0], state); + break; + case PROP_RELIABILITY: + /* see standard for details about this property */ + reliability = RELIABILITY_NO_FAULT_DETECTED; + apdu_len = encode_application_enumerated(&apdu[0], reliability); + break; + case PROP_MODE: + object_index = + Life_Safety_Point_Instance_To_Index(object_instance); + mode = Life_Safety_Point_Mode[object_index]; + apdu_len = encode_application_enumerated(&apdu[0], mode); + break; + case PROP_ACCEPTED_MODES: + for (mode = MIN_LIFE_SAFETY_MODE; mode < MAX_LIFE_SAFETY_MODE; + mode++) { + len = encode_application_enumerated(&apdu[apdu_len], mode); + apdu_len += len; + } + break; + case PROP_SILENCED: + object_index = + Life_Safety_Point_Instance_To_Index(object_instance); + silenced_state = Life_Safety_Point_Silenced_State[object_index]; + apdu_len = encode_application_enumerated(&apdu[0], silenced_state); + break; + case PROP_OPERATION_EXPECTED: + object_index = + Life_Safety_Point_Instance_To_Index(object_instance); + operation = Life_Safety_Point_Operation[object_index]; + apdu_len = encode_application_enumerated(&apdu[0], operation); + break; + default: + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_UNKNOWN_PROPERTY; + apdu_len = -1; + break; } return apdu_len; } /* returns true if successful */ -bool Life_Safety_Point_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) +bool Life_Safety_Point_Write_Property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { bool status = false; /* return value */ unsigned int object_index = 0; @@ -304,42 +312,42 @@ bool Life_Safety_Point_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, /* FIXME: len < application_data_len: more data? */ /* FIXME: len == 0: unable to decode? */ switch (wp_data->object_property) { - case PROP_MODE: - if (value.tag == BACNET_APPLICATION_TAG_ENUMERATED) { - if ((value.type.Enumerated >= MIN_LIFE_SAFETY_MODE) && - (value.type.Enumerated <= MIN_LIFE_SAFETY_MODE)) { + case PROP_MODE: + if (value.tag == BACNET_APPLICATION_TAG_ENUMERATED) { + if ((value.type.Enumerated >= MIN_LIFE_SAFETY_MODE) && + (value.type.Enumerated <= MIN_LIFE_SAFETY_MODE)) { + object_index = + Life_Safety_Point_Instance_To_Index(wp_data-> + object_instance); + Life_Safety_Point_Mode[object_index] = + value.type.Enumerated; + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; + } + break; + case PROP_OUT_OF_SERVICE: + if (value.tag == BACNET_APPLICATION_TAG_BOOLEAN) { object_index = Life_Safety_Point_Instance_To_Index(wp_data-> object_instance); - Life_Safety_Point_Mode[object_index] = - value.type.Enumerated; + Life_Safety_Point_Out_Of_Service[object_index] = + value.type.Boolean; status = true; } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } - } else { + break; + default: *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case PROP_OUT_OF_SERVICE: - if (value.tag == BACNET_APPLICATION_TAG_BOOLEAN) { - object_index = - Life_Safety_Point_Instance_To_Index(wp_data-> - object_instance); - Life_Safety_Point_Out_Of_Service[object_index] = - value.type.Boolean; - status = true; - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; - break; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + break; } return status; @@ -351,7 +359,8 @@ bool Life_Safety_Point_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, #include #include "ctest.h" -void testLifeSafetyPoint(Test * pTest) +void testLifeSafetyPoint( + Test * pTest) { uint8_t apdu[MAX_APDU] = { 0 }; int len = 0; @@ -366,8 +375,7 @@ void testLifeSafetyPoint(Test * pTest) len = Life_Safety_Point_Encode_Property_APDU(&apdu[0], instance, - PROP_OBJECT_IDENTIFIER, - BACNET_ARRAY_ALL, &error_class, &error_code); + PROP_OBJECT_IDENTIFIER, BACNET_ARRAY_ALL, &error_class, &error_code); ct_test(pTest, len != 0); len = decode_tag_number_and_value(&apdu[0], &tag_number, &len_value); ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_OBJECT_ID); @@ -380,7 +388,8 @@ void testLifeSafetyPoint(Test * pTest) } #ifdef TEST_LIFE_SAFETY_POINT -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -397,6 +406,5 @@ int main(void) return 0; } -#endif /* TEST_LIFE_SAFETY_POINT */ -#endif /* TEST */ - +#endif /* TEST_LIFE_SAFETY_POINT */ +#endif /* TEST */ diff --git a/bacnet-stack/demo/object/mso.c b/bacnet-stack/demo/object/mso.c index 56b10ffb..1c459c48 100644 --- a/bacnet-stack/demo/object/mso.c +++ b/bacnet-stack/demo/object/mso.c @@ -32,7 +32,7 @@ #include "bacdcode.h" #include "bacenum.h" #include "bacapp.h" -#include "config.h" /* the custom stuff */ +#include "config.h" /* the custom stuff */ #include "wp.h" #define MAX_MULTISTATE_OUTPUTS 4 @@ -53,8 +53,7 @@ static uint8_t static bool Multistate_Output_Out_Of_Service[MAX_MULTISTATE_OUTPUTS]; /* These three arrays are used by the ReadPropertyMultiple handler */ -static const int Multistate_Output_Properties_Required[] = -{ +static const int Multistate_Output_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, PROP_OBJECT_NAME, PROP_OBJECT_TYPE, @@ -68,14 +67,12 @@ static const int Multistate_Output_Properties_Required[] = -1 }; -static const int Multistate_Output_Properties_Optional[] = -{ +static const int Multistate_Output_Properties_Optional[] = { PROP_DESCRIPTION, -1 }; -static const int Multistate_Output_Properties_Proprietary[] = -{ +static const int Multistate_Output_Properties_Proprietary[] = { -1 }; @@ -94,7 +91,8 @@ void Multistate_Output_Property_Lists( return; } -void Multistate_Output_Init(void) +void Multistate_Output_Init( + void) { unsigned i, j; static bool initialized = false; @@ -116,7 +114,8 @@ void Multistate_Output_Init(void) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need validate that the */ /* given instance exists */ -bool Multistate_Output_Valid_Instance(uint32_t object_instance) +bool Multistate_Output_Valid_Instance( + uint32_t object_instance) { if (object_instance < MAX_MULTISTATE_OUTPUTS) return true; @@ -126,7 +125,8 @@ bool Multistate_Output_Valid_Instance(uint32_t object_instance) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then count how many you have */ -unsigned Multistate_Output_Count(void) +unsigned Multistate_Output_Count( + void) { return MAX_MULTISTATE_OUTPUTS; } @@ -134,7 +134,8 @@ unsigned Multistate_Output_Count(void) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need to return the instance */ /* that correlates to the correct index */ -uint32_t Multistate_Output_Index_To_Instance(unsigned index) +uint32_t Multistate_Output_Index_To_Instance( + unsigned index) { return index; } @@ -142,7 +143,8 @@ uint32_t Multistate_Output_Index_To_Instance(unsigned index) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need to return the index */ /* that correlates to the correct instance number */ -unsigned Multistate_Output_Instance_To_Index(uint32_t object_instance) +unsigned Multistate_Output_Instance_To_Index( + uint32_t object_instance) { unsigned index = MAX_MULTISTATE_OUTPUTS; @@ -152,7 +154,8 @@ unsigned Multistate_Output_Instance_To_Index(uint32_t object_instance) return index; } -static uint32_t Multistate_Output_Present_Value(uint32_t object_instance) +static uint32_t Multistate_Output_Present_Value( + uint32_t object_instance) { uint32_t value = MULTISTATE_RELINQUISH_DEFAULT; unsigned index = 0; @@ -173,7 +176,8 @@ static uint32_t Multistate_Output_Present_Value(uint32_t object_instance) } /* note: the object name must be unique within this device */ -char *Multistate_Output_Name(uint32_t object_instance) +char *Multistate_Output_Name( + uint32_t object_instance) { static char text_string[32] = ""; /* okay for single thread */ @@ -186,14 +190,16 @@ char *Multistate_Output_Name(uint32_t object_instance) } /* return apdu len, or -1 on error */ -int Multistate_Output_Encode_Property_APDU(uint8_t * apdu, +int Multistate_Output_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { int len = 0; - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ BACNET_BIT_STRING bit_string; BACNET_CHARACTER_STRING char_string; uint32_t present_value = 0; @@ -203,122 +209,128 @@ int Multistate_Output_Encode_Property_APDU(uint8_t * apdu, Multistate_Output_Init(); switch (property) { - case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], OBJECT_MULTI_STATE_OUTPUT, - object_instance); - break; - /* note: Name and Description don't have to be the same. - You could make Description writable and different */ - case PROP_OBJECT_NAME: - case PROP_DESCRIPTION: - characterstring_init_ansi(&char_string, - Multistate_Output_Name(object_instance)); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_OBJECT_TYPE: - apdu_len = - encode_application_enumerated(&apdu[0], OBJECT_MULTI_STATE_OUTPUT); - break; - case PROP_PRESENT_VALUE: - present_value = Multistate_Output_Present_Value(object_instance); - apdu_len = encode_application_unsigned(&apdu[0], present_value); - break; - case PROP_STATUS_FLAGS: - /* note: see the details in the standard on how to use these */ - bitstring_init(&bit_string); - bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); - apdu_len = encode_application_bitstring(&apdu[0], &bit_string); - break; - case PROP_EVENT_STATE: - /* note: see the details in the standard on how to use this */ - apdu_len = encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); - break; - case PROP_OUT_OF_SERVICE: - object_index = - Multistate_Output_Instance_To_Index(object_instance); - state = Multistate_Output_Out_Of_Service[object_index]; - apdu_len = encode_application_boolean(&apdu[0], state); - break; - case PROP_PRIORITY_ARRAY: - /* Array element zero is the number of elements in the array */ - if (array_index == 0) + case PROP_OBJECT_IDENTIFIER: apdu_len = - encode_application_unsigned(&apdu[0], BACNET_MAX_PRIORITY); - /* if no index was specified, then try to encode the entire list */ - /* into one packet. */ - else if (array_index == BACNET_ARRAY_ALL) { + encode_application_object_id(&apdu[0], + OBJECT_MULTI_STATE_OUTPUT, object_instance); + break; + /* note: Name and Description don't have to be the same. + You could make Description writable and different */ + case PROP_OBJECT_NAME: + case PROP_DESCRIPTION: + characterstring_init_ansi(&char_string, + Multistate_Output_Name(object_instance)); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_OBJECT_TYPE: + apdu_len = + encode_application_enumerated(&apdu[0], + OBJECT_MULTI_STATE_OUTPUT); + break; + case PROP_PRESENT_VALUE: + present_value = Multistate_Output_Present_Value(object_instance); + apdu_len = encode_application_unsigned(&apdu[0], present_value); + break; + case PROP_STATUS_FLAGS: + /* note: see the details in the standard on how to use these */ + bitstring_init(&bit_string); + bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); + apdu_len = encode_application_bitstring(&apdu[0], &bit_string); + break; + case PROP_EVENT_STATE: + /* note: see the details in the standard on how to use this */ + apdu_len = + encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); + break; + case PROP_OUT_OF_SERVICE: object_index = Multistate_Output_Instance_To_Index(object_instance); - for (i = 0; i < BACNET_MAX_PRIORITY; i++) { - /* FIXME: check if we have room before adding it to APDU */ - if (Multistate_Output_Level[object_index][i] == - MULTISTATE_NULL) - len = encode_application_null(&apdu[apdu_len]); - else { - present_value = - Multistate_Output_Level[object_index][i]; - len = - encode_application_unsigned(&apdu[apdu_len], - present_value); - } - /* add it if we have room */ - if ((apdu_len + len) < MAX_APDU) - apdu_len += len; - else { - *error_class = ERROR_CLASS_SERVICES; - *error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT; - apdu_len = -1; - break; - } - } - } else { - object_index = - Multistate_Output_Instance_To_Index(object_instance); - if (array_index <= BACNET_MAX_PRIORITY) { - if (Multistate_Output_Level[object_index][array_index - - 1] == MULTISTATE_NULL) - apdu_len = encode_application_null(&apdu[0]); - else { - present_value = - Multistate_Output_Level[object_index][array_index - - 1]; - apdu_len = - encode_application_unsigned(&apdu[0], present_value); + state = Multistate_Output_Out_Of_Service[object_index]; + apdu_len = encode_application_boolean(&apdu[0], state); + break; + case PROP_PRIORITY_ARRAY: + /* Array element zero is the number of elements in the array */ + if (array_index == 0) + apdu_len = + encode_application_unsigned(&apdu[0], BACNET_MAX_PRIORITY); + /* if no index was specified, then try to encode the entire list */ + /* into one packet. */ + else if (array_index == BACNET_ARRAY_ALL) { + object_index = + Multistate_Output_Instance_To_Index(object_instance); + for (i = 0; i < BACNET_MAX_PRIORITY; i++) { + /* FIXME: check if we have room before adding it to APDU */ + if (Multistate_Output_Level[object_index][i] == + MULTISTATE_NULL) + len = encode_application_null(&apdu[apdu_len]); + else { + present_value = + Multistate_Output_Level[object_index][i]; + len = + encode_application_unsigned(&apdu[apdu_len], + present_value); + } + /* add it if we have room */ + if ((apdu_len + len) < MAX_APDU) + apdu_len += len; + else { + *error_class = ERROR_CLASS_SERVICES; + *error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT; + apdu_len = -1; + break; + } } } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_ARRAY_INDEX; - apdu_len = -1; + object_index = + Multistate_Output_Instance_To_Index(object_instance); + if (array_index <= BACNET_MAX_PRIORITY) { + if (Multistate_Output_Level[object_index][array_index - + 1] == MULTISTATE_NULL) + apdu_len = encode_application_null(&apdu[0]); + else { + present_value = + Multistate_Output_Level[object_index][array_index - + 1]; + apdu_len = + encode_application_unsigned(&apdu[0], + present_value); + } + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_ARRAY_INDEX; + apdu_len = -1; + } } - } - break; - case PROP_RELINQUISH_DEFAULT: - present_value = MULTISTATE_RELINQUISH_DEFAULT; - apdu_len = encode_application_enumerated(&apdu[0], present_value); - break; - case PROP_NUMBER_OF_STATES: - apdu_len = encode_application_unsigned(&apdu[apdu_len], - MULTISTATE_NUMBER_OF_STATES); - break; + break; + case PROP_RELINQUISH_DEFAULT: + present_value = MULTISTATE_RELINQUISH_DEFAULT; + apdu_len = encode_application_enumerated(&apdu[0], present_value); + break; + case PROP_NUMBER_OF_STATES: + apdu_len = encode_application_unsigned(&apdu[apdu_len], + MULTISTATE_NUMBER_OF_STATES); + break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_UNKNOWN_PROPERTY; - apdu_len = -1; - break; + default: + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_UNKNOWN_PROPERTY; + apdu_len = -1; + break; } return apdu_len; } /* returns true if successful */ -bool Multistate_Output_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) +bool Multistate_Output_Write_Property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { bool status = false; /* return value */ unsigned int object_index = 0; @@ -339,81 +351,81 @@ bool Multistate_Output_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, /* FIXME: len < application_data_len: more data? */ /* FIXME: len == 0: unable to decode? */ switch (wp_data->object_property) { - case PROP_PRESENT_VALUE: - if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { - priority = wp_data->priority; - /* Command priority 6 is reserved for use by Minimum On/Off - algorithm and may not be used for other purposes in any - object. */ - if (priority && (priority <= BACNET_MAX_PRIORITY) && - (priority != 6 /* reserved */ ) && - (value.type.Unsigned_Int <= MULTISTATE_NUMBER_OF_STATES)) { - level = value.type.Unsigned_Int; - object_index = - Multistate_Output_Instance_To_Index(wp_data-> - object_instance); - priority--; - Multistate_Output_Level[object_index][priority] = - (uint8_t) level; - /* Note: you could set the physical output here if we - are the highest priority. - However, if Out of Service is TRUE, then don't set the - physical output. This comment may apply to the - main loop (i.e. check out of service before changing output) */ - status = true; - } else if (priority == 6) { + case PROP_PRESENT_VALUE: + if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { + priority = wp_data->priority; /* Command priority 6 is reserved for use by Minimum On/Off algorithm and may not be used for other purposes in any object. */ - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + if (priority && (priority <= BACNET_MAX_PRIORITY) && + (priority != 6 /* reserved */ ) && + (value.type.Unsigned_Int <= MULTISTATE_NUMBER_OF_STATES)) { + level = value.type.Unsigned_Int; + object_index = + Multistate_Output_Instance_To_Index(wp_data-> + object_instance); + priority--; + Multistate_Output_Level[object_index][priority] = + (uint8_t) level; + /* Note: you could set the physical output here if we + are the highest priority. + However, if Out of Service is TRUE, then don't set the + physical output. This comment may apply to the + main loop (i.e. check out of service before changing output) */ + status = true; + } else if (priority == 6) { + /* Command priority 6 is reserved for use by Minimum On/Off + algorithm and may not be used for other purposes in any + object. */ + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } + } else if (value.tag == BACNET_APPLICATION_TAG_NULL) { + level = MULTISTATE_NULL; + object_index = + Multistate_Output_Instance_To_Index(wp_data-> + object_instance); + priority = wp_data->priority; + if (priority && (priority <= BACNET_MAX_PRIORITY)) { + priority--; + Multistate_Output_Level[object_index][priority] = + (uint8_t) level; + /* Note: you could set the physical output here to the next + highest priority, or to the relinquish default if no + priorities are set. + However, if Out of Service is TRUE, then don't set the + physical output. This comment may apply to the + main loop (i.e. check out of service before changing output) */ + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } - } else if (value.tag == BACNET_APPLICATION_TAG_NULL) { - level = MULTISTATE_NULL; - object_index = - Multistate_Output_Instance_To_Index(wp_data-> - object_instance); - priority = wp_data->priority; - if (priority && (priority <= BACNET_MAX_PRIORITY)) { - priority--; - Multistate_Output_Level[object_index][priority] = - (uint8_t) level; - /* Note: you could set the physical output here to the next - highest priority, or to the relinquish default if no - priorities are set. - However, if Out of Service is TRUE, then don't set the - physical output. This comment may apply to the - main loop (i.e. check out of service before changing output) */ + break; + case PROP_OUT_OF_SERVICE: + if (value.tag == BACNET_APPLICATION_TAG_BOOLEAN) { + object_index = + Multistate_Output_Instance_To_Index(wp_data-> + object_instance); + Multistate_Output_Out_Of_Service[object_index] = + value.type.Boolean; status = true; } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } - } else { + break; + default: *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case PROP_OUT_OF_SERVICE: - if (value.tag == BACNET_APPLICATION_TAG_BOOLEAN) { - object_index = - Multistate_Output_Instance_To_Index(wp_data-> - object_instance); - Multistate_Output_Out_Of_Service[object_index] = - value.type.Boolean; - status = true; - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; - break; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + break; } return status; @@ -425,7 +437,8 @@ bool Multistate_Output_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, #include #include "ctest.h" -void testMultistateOutput(Test * pTest) +void testMultistateOutput( + Test * pTest) { uint8_t apdu[MAX_APDU] = { 0 }; int len = 0; @@ -440,8 +453,7 @@ void testMultistateOutput(Test * pTest) len = Multistate_Output_Encode_Property_APDU(&apdu[0], instance, - PROP_OBJECT_IDENTIFIER, - BACNET_ARRAY_ALL, &error_class, &error_code); + PROP_OBJECT_IDENTIFIER, BACNET_ARRAY_ALL, &error_class, &error_code); ct_test(pTest, len != 0); len = decode_tag_number_and_value(&apdu[0], &tag_number, &len_value); ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_OBJECT_ID); @@ -454,7 +466,8 @@ void testMultistateOutput(Test * pTest) } #ifdef TEST_MULTISTATE_OUTPUT -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -471,5 +484,5 @@ int main(void) return 0; } -#endif /* TEST_BINARY_INPUT */ -#endif /* TEST */ +#endif /* TEST_BINARY_INPUT */ +#endif /* TEST */ diff --git a/bacnet-stack/demo/readfile/main.c b/bacnet-stack/demo/readfile/main.c index f7b6ece3..ada9e6ca 100644 --- a/bacnet-stack/demo/readfile/main.c +++ b/bacnet-stack/demo/readfile/main.c @@ -28,7 +28,7 @@ #include #include #include -#include /* for time */ +#include /* for time */ #include #include "bactext.h" #include "iam.h" @@ -61,9 +61,11 @@ static bool End_Of_File_Detected = false; static bool Error_Detected = false; static uint8_t Current_Invoke_ID = 0; -static void Atomic_Read_File_Error_Handler(BACNET_ADDRESS * src, +static void Atomic_Read_File_Error_Handler( + BACNET_ADDRESS * src, uint8_t invoke_id, - BACNET_ERROR_CLASS error_class, BACNET_ERROR_CODE error_code) + BACNET_ERROR_CLASS error_class, + BACNET_ERROR_CODE error_code) { /* FIXME: verify src and invoke id */ (void) src; @@ -74,43 +76,47 @@ static void Atomic_Read_File_Error_Handler(BACNET_ADDRESS * src, Error_Detected = true; } -void MyAbortHandler(BACNET_ADDRESS * src, - uint8_t invoke_id, uint8_t abort_reason, bool server) +void MyAbortHandler( + BACNET_ADDRESS * src, + uint8_t invoke_id, + uint8_t abort_reason, + bool server) { /* FIXME: verify src and invoke id */ (void) src; (void) invoke_id; (void) server; printf("\r\nBACnet Abort!\r\n"); - printf("Abort Reason: %s\r\n", - bactext_abort_reason_name(abort_reason)); + printf("Abort Reason: %s\r\n", bactext_abort_reason_name(abort_reason)); Error_Detected = true; } -void MyRejectHandler(BACNET_ADDRESS * src, - uint8_t invoke_id, uint8_t reject_reason) +void MyRejectHandler( + BACNET_ADDRESS * src, + uint8_t invoke_id, + uint8_t reject_reason) { /* FIXME: verify src and invoke id */ (void) src; (void) invoke_id; printf("\r\nBACnet Reject!\r\n"); - printf("Reject Reason: %s\r\n", - bactext_reject_reason_name(reject_reason)); + printf("Reject Reason: %s\r\n", bactext_reject_reason_name(reject_reason)); Error_Detected = true; } -static void AtomicReadFileAckHandler(uint8_t * service_request, +static void AtomicReadFileAckHandler( + uint8_t * service_request, uint16_t service_len, - BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_ACK_DATA * service_data) + BACNET_ADDRESS * src, + BACNET_CONFIRMED_SERVICE_ACK_DATA * service_data) { int len = 0; BACNET_ATOMIC_READ_FILE_DATA data; - FILE *pFile = NULL; /* stream pointer */ + FILE *pFile = NULL; /* stream pointer */ size_t octets_written = 0; - (void) src; /* FIXME: validate the source address matches */ - len = arf_ack_decode_service_request(service_request, - service_len, &data); + (void) src; /* FIXME: validate the source address matches */ + len = arf_ack_decode_service_request(service_request, service_len, &data); if (len > 0) { /* validate the parameters before storing data */ if ((data.access == FILE_STREAM_ACCESS) && @@ -131,8 +137,7 @@ static void AtomicReadFileAckHandler(uint8_t * service_request, Local_File_Name); else printf("\r%u bytes", - (data.type.stream.fileStartPosition + - octets_written)); + (data.type.stream.fileStartPosition + octets_written)); fclose(pFile); } if (data.endOfFile) { @@ -143,8 +148,10 @@ static void AtomicReadFileAckHandler(uint8_t * service_request, } } -static void LocalIAmHandler(uint8_t * service_request, - uint16_t service_len, BACNET_ADDRESS * src) +static void LocalIAmHandler( + uint8_t * service_request, + uint16_t service_len, + BACNET_ADDRESS * src) { int len = 0; uint32_t device_id = 0; @@ -164,15 +171,14 @@ static void LocalIAmHandler(uint8_t * service_request, return; } -static void Init_Service_Handlers(void) +static void Init_Service_Handlers( + void) { /* we need to handle who-is to support dynamic device binding to us */ - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, - handler_who_is); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is); /* handle i-am to support binding to other devices */ - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, - LocalIAmHandler); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, LocalIAmHandler); /* set the handler for all the services we don't implement It is required to send the proper reject message... */ apdu_set_unrecognized_service_handler_handler @@ -190,7 +196,9 @@ static void Init_Service_Handlers(void) apdu_set_reject_handler(MyRejectHandler); } -int main(int argc, char *argv[]) +int main( + int argc, + char *argv[]) { BACNET_ADDRESS src = { 0 }; /* address where message came from */ uint16_t pdu_len = 0; @@ -237,8 +245,7 @@ int main(int argc, char *argv[]) timeout_seconds = (Device_APDU_Timeout() / 1000) * Device_Number_Of_APDU_Retries(); /* try to bind with the device */ - Send_WhoIs(Target_Device_Object_Instance, - Target_Device_Object_Instance); + Send_WhoIs(Target_Device_Object_Instance, Target_Device_Object_Instance); /* loop forever */ for (;;) { /* increment timer - exit if timed out */ @@ -253,8 +260,7 @@ int main(int argc, char *argv[]) } /* at least one second has passed */ if (current_seconds != last_seconds) - tsm_timer_milliseconds(((current_seconds - - last_seconds) * 1000)); + tsm_timer_milliseconds(((current_seconds - last_seconds) * 1000)); if (End_Of_File_Detected || Error_Detected) break; /* wait until the device is bound, or timeout and quit */ diff --git a/bacnet-stack/demo/readprop/main.c b/bacnet-stack/demo/readprop/main.c index 90c9856c..b64050ef 100644 --- a/bacnet-stack/demo/readprop/main.c +++ b/bacnet-stack/demo/readprop/main.c @@ -29,7 +29,7 @@ #include #include #include -#include /* for time */ +#include /* for time */ #define PRINT_ENABLED 1 @@ -66,51 +66,57 @@ static int32_t Target_Object_Index = BACNET_ARRAY_ALL; static BACNET_ADDRESS Target_Address; static bool Error_Detected = false; -static void MyErrorHandler(BACNET_ADDRESS * src, +static void MyErrorHandler( + BACNET_ADDRESS * src, uint8_t invoke_id, - BACNET_ERROR_CLASS error_class, BACNET_ERROR_CODE error_code) + BACNET_ERROR_CLASS error_class, + BACNET_ERROR_CODE error_code) { /* FIXME: verify src and invoke id */ (void) src; (void) invoke_id; printf("BACnet Error: %s: %s\r\n", - bactext_error_class_name((int)error_class), - bactext_error_code_name((int)error_code)); + bactext_error_class_name((int) error_class), + bactext_error_code_name((int) error_code)); Error_Detected = true; } -void MyAbortHandler(BACNET_ADDRESS * src, - uint8_t invoke_id, uint8_t abort_reason, bool server) +void MyAbortHandler( + BACNET_ADDRESS * src, + uint8_t invoke_id, + uint8_t abort_reason, + bool server) { /* FIXME: verify src and invoke id */ (void) src; (void) invoke_id; (void) server; printf("BACnet Abort: %s\r\n", - bactext_abort_reason_name((int)abort_reason)); + bactext_abort_reason_name((int) abort_reason)); Error_Detected = true; } -void MyRejectHandler(BACNET_ADDRESS * src, - uint8_t invoke_id, uint8_t reject_reason) +void MyRejectHandler( + BACNET_ADDRESS * src, + uint8_t invoke_id, + uint8_t reject_reason) { /* FIXME: verify src and invoke id */ (void) src; (void) invoke_id; printf("BACnet Reject: %s\r\n", - bactext_reject_reason_name((int)reject_reason)); + bactext_reject_reason_name((int) reject_reason)); Error_Detected = true; } -static void Init_Service_Handlers(void) +static void Init_Service_Handlers( + void) { /* we need to handle who-is to support dynamic device binding to us */ - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, - handler_who_is); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is); /* handle i-am to support binding to other devices */ - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, - handler_i_am_bind); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, handler_i_am_bind); /* set the handler for all the services we don't implement It is required to send the proper reject message... */ apdu_set_unrecognized_service_handler_handler @@ -122,13 +128,14 @@ static void Init_Service_Handlers(void) apdu_set_confirmed_ack_handler(SERVICE_CONFIRMED_READ_PROPERTY, handler_read_property_ack); /* handle any errors coming back */ - apdu_set_error_handler(SERVICE_CONFIRMED_READ_PROPERTY, - MyErrorHandler); + apdu_set_error_handler(SERVICE_CONFIRMED_READ_PROPERTY, MyErrorHandler); apdu_set_abort_handler(MyAbortHandler); apdu_set_reject_handler(MyRejectHandler); } -int main(int argc, char *argv[]) +int main( + int argc, + char *argv[]) { BACNET_ADDRESS src = { 0 }; /* address where message came from */ uint16_t pdu_len = 0; @@ -186,8 +193,7 @@ int main(int argc, char *argv[]) timeout_seconds = (Device_APDU_Timeout() / 1000) * Device_Number_Of_APDU_Retries(); /* try to bind with the device */ - Send_WhoIs(Target_Device_Object_Instance, - Target_Device_Object_Instance); + Send_WhoIs(Target_Device_Object_Instance, Target_Device_Object_Instance); /* loop forever */ for (;;) { /* increment timer - exit if timed out */ @@ -202,8 +208,7 @@ int main(int argc, char *argv[]) } /* at least one second has passed */ if (current_seconds != last_seconds) - tsm_timer_milliseconds(((current_seconds - - last_seconds) * 1000)); + tsm_timer_milliseconds(((current_seconds - last_seconds) * 1000)); if (Error_Detected) break; /* wait until the device is bound, or timeout and quit */ diff --git a/bacnet-stack/demo/reinit/main.c b/bacnet-stack/demo/reinit/main.c index 6dd9d7bb..44101aea 100644 --- a/bacnet-stack/demo/reinit/main.c +++ b/bacnet-stack/demo/reinit/main.c @@ -28,7 +28,7 @@ #include #include #include -#include /* for time */ +#include /* for time */ #include #include "bactext.h" #include "iam.h" @@ -56,15 +56,16 @@ static uint8_t Rx_Buf[MAX_MPDU] = { 0 }; /* global variables used in this file */ static uint32_t Target_Device_Object_Instance = BACNET_MAX_INSTANCE; static BACNET_ADDRESS Target_Address; -static BACNET_REINITIALIZED_STATE Reinitialize_State = - BACNET_REINIT_COLDSTART; +static BACNET_REINITIALIZED_STATE Reinitialize_State = BACNET_REINIT_COLDSTART; static char *Reinitialize_Password = NULL; static bool Error_Detected = false; -static void MyErrorHandler(BACNET_ADDRESS * src, +static void MyErrorHandler( + BACNET_ADDRESS * src, uint8_t invoke_id, - BACNET_ERROR_CLASS error_class, BACNET_ERROR_CODE error_code) + BACNET_ERROR_CLASS error_class, + BACNET_ERROR_CODE error_code) { /* FIXME: verify src and invoke id */ (void) src; @@ -75,30 +76,34 @@ static void MyErrorHandler(BACNET_ADDRESS * src, Error_Detected = true; } -void MyAbortHandler(BACNET_ADDRESS * src, - uint8_t invoke_id, uint8_t abort_reason, bool server) +void MyAbortHandler( + BACNET_ADDRESS * src, + uint8_t invoke_id, + uint8_t abort_reason, + bool server) { /* FIXME: verify src and invoke id */ (void) src; (void) invoke_id; (void) server; - printf("BACnet Abort: %s\r\n", - bactext_abort_reason_name(abort_reason)); + printf("BACnet Abort: %s\r\n", bactext_abort_reason_name(abort_reason)); Error_Detected = true; } -void MyRejectHandler(BACNET_ADDRESS * src, - uint8_t invoke_id, uint8_t reject_reason) +void MyRejectHandler( + BACNET_ADDRESS * src, + uint8_t invoke_id, + uint8_t reject_reason) { /* FIXME: verify src and invoke id */ (void) src; (void) invoke_id; - printf("BACnet Reject: %s\r\n", - bactext_reject_reason_name(reject_reason)); + printf("BACnet Reject: %s\r\n", bactext_reject_reason_name(reject_reason)); Error_Detected = true; } -void MyReinitializeDeviceSimpleAckHandler(BACNET_ADDRESS * src, +void MyReinitializeDeviceSimpleAckHandler( + BACNET_ADDRESS * src, uint8_t invoke_id) { (void) src; @@ -106,15 +111,14 @@ void MyReinitializeDeviceSimpleAckHandler(BACNET_ADDRESS * src, printf("ReinitializeDevice Acknowledged!\r\n"); } -static void Init_Service_Handlers(void) +static void Init_Service_Handlers( + void) { /* we need to handle who-is to support dynamic device binding to us */ - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, - handler_who_is); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is); /* handle i-am to support binding to other devices */ - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, - handler_i_am_bind); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, handler_i_am_bind); /* set the handler for all the services we don't implement It is required to send the proper reject message... */ apdu_set_unrecognized_service_handler_handler @@ -133,7 +137,9 @@ static void Init_Service_Handlers(void) apdu_set_reject_handler(MyRejectHandler); } -int main(int argc, char *argv[]) +int main( + int argc, + char *argv[]) { BACNET_ADDRESS src = { 0 }; /* address where message came from */ uint16_t pdu_len = 0; @@ -188,19 +194,14 @@ int main(int argc, char *argv[]) timeout_seconds = (Device_APDU_Timeout() / 1000) * Device_Number_Of_APDU_Retries(); /* try to bind with the device */ - Send_WhoIs(Target_Device_Object_Instance, - Target_Device_Object_Instance); + Send_WhoIs(Target_Device_Object_Instance, Target_Device_Object_Instance); /* loop forever */ for (;;) { /* increment timer - exit if timed out */ current_seconds = time(NULL); /* returns 0 bytes on timeout */ - pdu_len = datalink_receive( - &src, - &Rx_Buf[0], - MAX_MPDU, - timeout); + pdu_len = datalink_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout); /* process */ if (pdu_len) { @@ -208,8 +209,7 @@ int main(int argc, char *argv[]) } /* at least one second has passed */ if (current_seconds != last_seconds) - tsm_timer_milliseconds(((current_seconds - - last_seconds) * 1000)); + tsm_timer_milliseconds(((current_seconds - last_seconds) * 1000)); if (Error_Detected) break; /* wait until the device is bound, or timeout and quit */ diff --git a/bacnet-stack/demo/server/main.c b/bacnet-stack/demo/server/main.c index 98e77987..0b28d4cf 100644 --- a/bacnet-stack/demo/server/main.c +++ b/bacnet-stack/demo/server/main.c @@ -55,13 +55,12 @@ /* buffers used for receiving */ static uint8_t Rx_Buf[MAX_MPDU] = { 0 }; -static void Init_Service_Handlers(void) +static void Init_Service_Handlers( + void) { /* we need to handle who-is to support dynamic device binding */ - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, - handler_who_is); - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_HAS, - handler_who_has); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_HAS, handler_who_has); /* set the handler for all the services we don't implement */ /* It is required to send the proper reject message... */ apdu_set_unrecognized_service_handler_handler @@ -83,8 +82,7 @@ static void Init_Service_Handlers(void) apdu_set_confirmed_handler(SERVICE_CONFIRMED_REINITIALIZE_DEVICE, handler_reinitialize_device); apdu_set_unconfirmed_handler - (SERVICE_UNCONFIRMED_UTC_TIME_SYNCHRONIZATION, - handler_timesync_utc); + (SERVICE_UNCONFIRMED_UTC_TIME_SYNCHRONIZATION, handler_timesync_utc); apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_TIME_SYNCHRONIZATION, handler_timesync); /* handle communication so we can shutup when asked */ @@ -93,12 +91,15 @@ static void Init_Service_Handlers(void) handler_device_communication_control); } -static void cleanup(void) +static void cleanup( + void) { datalink_cleanup(); } -int main(int argc, char *argv[]) +int main( + int argc, + char *argv[]) { BACNET_ADDRESS src = { 0 }; /* address where message came from */ uint16_t pdu_len = 0; @@ -147,9 +148,7 @@ int main(int argc, char *argv[]) "BACnet Stack Version %s\n" "BACnet Device ID: %u\n" "Max APDU: %d\n", - BACnet_Version, - Device_Object_Instance_Number(), - MAX_APDU); + BACnet_Version, Device_Object_Instance_Number(), MAX_APDU); Init_Service_Handlers(); BIP_Debug = true; if (!datalink_init(getenv("BACNET_IFACE"))) diff --git a/bacnet-stack/demo/timesync/main.c b/bacnet-stack/demo/timesync/main.c index 07737b9d..6e66fe8f 100644 --- a/bacnet-stack/demo/timesync/main.c +++ b/bacnet-stack/demo/timesync/main.c @@ -28,7 +28,7 @@ #include #include #include -#include /* for time */ +#include /* for time */ #include #include "bactext.h" #include "config.h" @@ -56,35 +56,38 @@ static int32_t Target_Object_Instance_Max = BACNET_MAX_INSTANCE; static bool Error_Detected = false; -void MyAbortHandler(BACNET_ADDRESS * src, - uint8_t invoke_id, uint8_t abort_reason, bool server) +void MyAbortHandler( + BACNET_ADDRESS * src, + uint8_t invoke_id, + uint8_t abort_reason, + bool server) { /* FIXME: verify src and invoke id */ (void) src; (void) invoke_id; (void) server; - printf("BACnet Abort: %s\r\n", - bactext_abort_reason_name(abort_reason)); + printf("BACnet Abort: %s\r\n", bactext_abort_reason_name(abort_reason)); Error_Detected = true; } -void MyRejectHandler(BACNET_ADDRESS * src, - uint8_t invoke_id, uint8_t reject_reason) +void MyRejectHandler( + BACNET_ADDRESS * src, + uint8_t invoke_id, + uint8_t reject_reason) { /* FIXME: verify src and invoke id */ (void) src; (void) invoke_id; - printf("BACnet Reject: %s\r\n", - bactext_reject_reason_name(reject_reason)); + printf("BACnet Reject: %s\r\n", bactext_reject_reason_name(reject_reason)); Error_Detected = true; } -static void Init_Service_Handlers(void) +static void Init_Service_Handlers( + void) { /* we need to handle who-is to support dynamic device binding to us */ - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, - handler_who_is); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is); /* set the handler for all the services we don't implement It is required to send the proper reject message... */ apdu_set_unrecognized_service_handler_handler @@ -94,8 +97,7 @@ static void Init_Service_Handlers(void) handler_read_property); /* handle the reply (request) coming in */ apdu_set_unconfirmed_handler - (SERVICE_UNCONFIRMED_UTC_TIME_SYNCHRONIZATION, - handler_timesync_utc); + (SERVICE_UNCONFIRMED_UTC_TIME_SYNCHRONIZATION, handler_timesync_utc); apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_TIME_SYNCHRONIZATION, handler_timesync); /* handle any errors coming back */ @@ -103,7 +105,9 @@ static void Init_Service_Handlers(void) apdu_set_reject_handler(MyRejectHandler); } -int main(int argc, char *argv[]) +int main( + int argc, + char *argv[]) { BACNET_ADDRESS src = { 0 }; /* address where message came from */ uint16_t pdu_len = 0; diff --git a/bacnet-stack/demo/ucov/main.c b/bacnet-stack/demo/ucov/main.c index d2cf4365..1a461fd6 100644 --- a/bacnet-stack/demo/ucov/main.c +++ b/bacnet-stack/demo/ucov/main.c @@ -28,7 +28,7 @@ #include #include #include -#include /* for time */ +#include /* for time */ #include #include "bactext.h" #include "iam.h" @@ -49,15 +49,14 @@ #include "client.h" #include "txbuf.h" -static void Init_Service_Handlers(void) +static void Init_Service_Handlers( + void) { /* we need to handle who-is to support dynamic device binding to us */ - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, - handler_who_is); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is); /* handle i-am to support binding to other devices */ - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, - handler_i_am_bind); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, handler_i_am_bind); /* set the handler for all the services we don't implement It is required to send the proper reject message... */ apdu_set_unrecognized_service_handler_handler @@ -67,7 +66,9 @@ static void Init_Service_Handlers(void) handler_read_property); } -int main(int argc, char *argv[]) +int main( + int argc, + char *argv[]) { char *value_string = NULL; bool status = false; @@ -162,8 +163,7 @@ int main(int argc, char *argv[]) cov_data.listOfValues.priority = BACNET_NO_PRIORITY; /* optional index */ if (argc > 10) - cov_data.listOfValues.propertyArrayIndex = - strtol(argv[10], NULL, 0); + cov_data.listOfValues.propertyArrayIndex = strtol(argv[10], NULL, 0); else cov_data.listOfValues.propertyArrayIndex = BACNET_ARRAY_ALL; diff --git a/bacnet-stack/demo/whohas/main.c b/bacnet-stack/demo/whohas/main.c index 95cfed99..62292458 100644 --- a/bacnet-stack/demo/whohas/main.c +++ b/bacnet-stack/demo/whohas/main.c @@ -28,7 +28,7 @@ #include #include #include -#include /* for time */ +#include /* for time */ #include #include "bactext.h" #include "iam.h" @@ -59,35 +59,38 @@ static char *Target_Object_Name = NULL; static bool Error_Detected = false; -void MyAbortHandler(BACNET_ADDRESS * src, - uint8_t invoke_id, uint8_t abort_reason, bool server) +void MyAbortHandler( + BACNET_ADDRESS * src, + uint8_t invoke_id, + uint8_t abort_reason, + bool server) { /* FIXME: verify src and invoke id */ (void) src; (void) invoke_id; (void) server; - printf("BACnet Abort: %s\r\n", - bactext_abort_reason_name(abort_reason)); + printf("BACnet Abort: %s\r\n", bactext_abort_reason_name(abort_reason)); Error_Detected = true; } -void MyRejectHandler(BACNET_ADDRESS * src, - uint8_t invoke_id, uint8_t reject_reason) +void MyRejectHandler( + BACNET_ADDRESS * src, + uint8_t invoke_id, + uint8_t reject_reason) { /* FIXME: verify src and invoke id */ (void) src; (void) invoke_id; - printf("BACnet Reject: %s\r\n", - bactext_reject_reason_name(reject_reason)); + printf("BACnet Reject: %s\r\n", bactext_reject_reason_name(reject_reason)); Error_Detected = true; } -static void Init_Service_Handlers(void) +static void Init_Service_Handlers( + void) { /* we need to handle who-is to support dynamic device binding to us */ - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, - handler_who_is); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is); /* set the handler for all the services we don't implement It is required to send the proper reject message... */ apdu_set_unrecognized_service_handler_handler @@ -96,14 +99,15 @@ static void Init_Service_Handlers(void) apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY, handler_read_property); /* handle the reply (request) coming back */ - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_HAVE, - handler_i_have); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_HAVE, handler_i_have); /* handle any errors coming back */ apdu_set_abort_handler(MyAbortHandler); apdu_set_reject_handler(MyRejectHandler); } -int main(int argc, char *argv[]) +int main( + int argc, + char *argv[]) { BACNET_ADDRESS src = { 0 }; /* address where message came from */ uint16_t pdu_len = 0; @@ -157,8 +161,7 @@ int main(int argc, char *argv[]) if (argc < 3) Send_WhoHas_Name(-1, -1, Target_Object_Name); else - Send_WhoHas_Object(-1, -1, - Target_Object_Type, Target_Object_Instance); + Send_WhoHas_Object(-1, -1, Target_Object_Type, Target_Object_Instance); /* loop forever */ for (;;) { /* increment timer - exit if timed out */ diff --git a/bacnet-stack/demo/whois/main.c b/bacnet-stack/demo/whois/main.c index fe1f004f..225bba8b 100644 --- a/bacnet-stack/demo/whois/main.c +++ b/bacnet-stack/demo/whois/main.c @@ -28,7 +28,7 @@ #include #include #include -#include /* for time */ +#include /* for time */ #include #include "bactext.h" #include "iam.h" @@ -55,35 +55,38 @@ static int32_t Target_Object_Instance_Max = BACNET_MAX_INSTANCE; static bool Error_Detected = false; -void MyAbortHandler(BACNET_ADDRESS * src, - uint8_t invoke_id, uint8_t abort_reason, bool server) +void MyAbortHandler( + BACNET_ADDRESS * src, + uint8_t invoke_id, + uint8_t abort_reason, + bool server) { /* FIXME: verify src and invoke id */ (void) src; (void) invoke_id; (void) server; - printf("BACnet Abort: %s\r\n", - bactext_abort_reason_name(abort_reason)); + printf("BACnet Abort: %s\r\n", bactext_abort_reason_name(abort_reason)); Error_Detected = true; } -void MyRejectHandler(BACNET_ADDRESS * src, - uint8_t invoke_id, uint8_t reject_reason) +void MyRejectHandler( + BACNET_ADDRESS * src, + uint8_t invoke_id, + uint8_t reject_reason) { /* FIXME: verify src and invoke id */ (void) src; (void) invoke_id; - printf("BACnet Reject: %s\r\n", - bactext_reject_reason_name(reject_reason)); + printf("BACnet Reject: %s\r\n", bactext_reject_reason_name(reject_reason)); Error_Detected = true; } -static void Init_Service_Handlers(void) +static void Init_Service_Handlers( + void) { /* we need to handle who-is to support dynamic device binding to us */ - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, - handler_who_is); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is); /* set the handler for all the services we don't implement It is required to send the proper reject message... */ apdu_set_unrecognized_service_handler_handler @@ -92,14 +95,14 @@ static void Init_Service_Handlers(void) apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY, handler_read_property); /* handle the reply (request) coming back */ - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, - handler_i_am_add); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, handler_i_am_add); /* handle any errors coming back */ apdu_set_abort_handler(MyAbortHandler); apdu_set_reject_handler(MyRejectHandler); } -static void print_address_cache(void) +static void print_address_cache( + void) { int i, j; BACNET_ADDRESS address; @@ -120,7 +123,9 @@ static void print_address_cache(void) } } -int main(int argc, char *argv[]) +int main( + int argc, + char *argv[]) { BACNET_ADDRESS src = { 0 }; /* address where message came from */ uint16_t pdu_len = 0; diff --git a/bacnet-stack/demo/writefile/main.c b/bacnet-stack/demo/writefile/main.c index 0c63ddfb..ae1be39a 100644 --- a/bacnet-stack/demo/writefile/main.c +++ b/bacnet-stack/demo/writefile/main.c @@ -28,7 +28,7 @@ #include #include #include -#include /* for time */ +#include /* for time */ #include #include "bactext.h" #include "iam.h" @@ -61,9 +61,11 @@ static bool End_Of_File_Detected = false; static bool Error_Detected = false; static uint8_t Current_Invoke_ID = 0; -static void Atomic_Read_File_Error_Handler(BACNET_ADDRESS * src, +static void Atomic_Read_File_Error_Handler( + BACNET_ADDRESS * src, uint8_t invoke_id, - BACNET_ERROR_CLASS error_class, BACNET_ERROR_CODE error_code) + BACNET_ERROR_CLASS error_class, + BACNET_ERROR_CODE error_code) { /* FIXME: verify src and invoke id */ (void) src; @@ -74,33 +76,38 @@ static void Atomic_Read_File_Error_Handler(BACNET_ADDRESS * src, Error_Detected = true; } -void MyAbortHandler(BACNET_ADDRESS * src, - uint8_t invoke_id, uint8_t abort_reason, bool server) +void MyAbortHandler( + BACNET_ADDRESS * src, + uint8_t invoke_id, + uint8_t abort_reason, + bool server) { /* FIXME: verify src and invoke id */ (void) src; (void) invoke_id; (void) server; printf("\r\nBACnet Abort!\r\n"); - printf("Abort Reason: %s\r\n", - bactext_abort_reason_name(abort_reason)); + printf("Abort Reason: %s\r\n", bactext_abort_reason_name(abort_reason)); Error_Detected = true; } -void MyRejectHandler(BACNET_ADDRESS * src, - uint8_t invoke_id, uint8_t reject_reason) +void MyRejectHandler( + BACNET_ADDRESS * src, + uint8_t invoke_id, + uint8_t reject_reason) { /* FIXME: verify src and invoke id */ (void) src; (void) invoke_id; printf("\r\nBACnet Reject!\r\n"); - printf("Reject Reason: %s\r\n", - bactext_reject_reason_name(reject_reason)); + printf("Reject Reason: %s\r\n", bactext_reject_reason_name(reject_reason)); Error_Detected = true; } -static void LocalIAmHandler(uint8_t * service_request, - uint16_t service_len, BACNET_ADDRESS * src) +static void LocalIAmHandler( + uint8_t * service_request, + uint16_t service_len, + BACNET_ADDRESS * src) { int len = 0; uint32_t device_id = 0; @@ -120,15 +127,14 @@ static void LocalIAmHandler(uint8_t * service_request, return; } -static void Init_Service_Handlers(void) +static void Init_Service_Handlers( + void) { /* we need to handle who-is to support dynamic device binding to us */ - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, - handler_who_is); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is); /* handle i-am to support binding to other devices */ - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, - LocalIAmHandler); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, LocalIAmHandler); /* set the handler for all the services we don't implement It is required to send the proper reject message... */ apdu_set_unrecognized_service_handler_handler @@ -143,7 +149,9 @@ static void Init_Service_Handlers(void) apdu_set_reject_handler(MyRejectHandler); } -int main(int argc, char *argv[]) +int main( + int argc, + char *argv[]) { BACNET_ADDRESS src = { 0 }; /* address where message came from */ uint16_t pdu_len = 0; @@ -193,8 +201,7 @@ int main(int argc, char *argv[]) timeout_seconds = (Device_APDU_Timeout() / 1000) * Device_Number_Of_APDU_Retries(); /* try to bind with the device */ - Send_WhoIs(Target_Device_Object_Instance, - Target_Device_Object_Instance); + Send_WhoIs(Target_Device_Object_Instance, Target_Device_Object_Instance); /* loop forever */ for (;;) { /* increment timer - exit if timed out */ @@ -209,8 +216,7 @@ int main(int argc, char *argv[]) } /* at least one second has passed */ if (current_seconds != last_seconds) - tsm_timer_milliseconds(((current_seconds - - last_seconds) * 1000)); + tsm_timer_milliseconds(((current_seconds - last_seconds) * 1000)); if (End_Of_File_Detected || Error_Detected) { printf("\r\n"); break; @@ -258,8 +264,7 @@ int main(int argc, char *argv[]) invoke_id = Send_Atomic_Write_File_Stream (Target_Device_Object_Instance, - Target_File_Object_Instance, fileStartPosition, - &fileData); + Target_File_Object_Instance, fileStartPosition, &fileData); Current_Invoke_ID = invoke_id; } else if (tsm_invoke_id_failed(invoke_id)) { fprintf(stderr, "\rError: TSM Timeout!\r\n"); diff --git a/bacnet-stack/demo/writeprop/main.c b/bacnet-stack/demo/writeprop/main.c index 00a9a25e..5c4d577a 100644 --- a/bacnet-stack/demo/writeprop/main.c +++ b/bacnet-stack/demo/writeprop/main.c @@ -28,10 +28,10 @@ #include #include #include -#include /* for time */ +#include /* for time */ #include #include -#include /* toupper */ +#include /* toupper */ #include "bactext.h" #include "iam.h" #include "arf.h" @@ -71,9 +71,11 @@ static uint8_t Target_Object_Property_Priority = 0; static BACNET_ADDRESS Target_Address; static bool Error_Detected = false; -static void MyErrorHandler(BACNET_ADDRESS * src, +static void MyErrorHandler( + BACNET_ADDRESS * src, uint8_t invoke_id, - BACNET_ERROR_CLASS error_class, BACNET_ERROR_CODE error_code) + BACNET_ERROR_CLASS error_class, + BACNET_ERROR_CODE error_code) { /* FIXME: verify src and invoke id */ (void) src; @@ -84,32 +86,36 @@ static void MyErrorHandler(BACNET_ADDRESS * src, Error_Detected = true; } -void MyAbortHandler(BACNET_ADDRESS * src, - uint8_t invoke_id, uint8_t abort_reason, bool server) +void MyAbortHandler( + BACNET_ADDRESS * src, + uint8_t invoke_id, + uint8_t abort_reason, + bool server) { /* FIXME: verify src and invoke id */ (void) src; (void) invoke_id; (void) server; printf("\r\nBACnet Abort!\r\n"); - printf("Abort Reason: %s\r\n", - bactext_abort_reason_name(abort_reason)); + printf("Abort Reason: %s\r\n", bactext_abort_reason_name(abort_reason)); Error_Detected = true; } -void MyRejectHandler(BACNET_ADDRESS * src, - uint8_t invoke_id, uint8_t reject_reason) +void MyRejectHandler( + BACNET_ADDRESS * src, + uint8_t invoke_id, + uint8_t reject_reason) { /* FIXME: verify src and invoke id */ (void) src; (void) invoke_id; printf("\r\nBACnet Reject!\r\n"); - printf("Reject Reason: %s\r\n", - bactext_reject_reason_name(reject_reason)); + printf("Reject Reason: %s\r\n", bactext_reject_reason_name(reject_reason)); Error_Detected = true; } -void MyWritePropertySimpleAckHandler(BACNET_ADDRESS * src, +void MyWritePropertySimpleAckHandler( + BACNET_ADDRESS * src, uint8_t invoke_id) { (void) src; @@ -117,15 +123,14 @@ void MyWritePropertySimpleAckHandler(BACNET_ADDRESS * src, printf("\r\nWriteProperty Acknowledged!\r\n"); } -static void Init_Service_Handlers(void) +static void Init_Service_Handlers( + void) { /* we need to handle who-is to support dynamic device binding to us */ - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, - handler_who_is); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is); /* handle i-am to support binding to other devices */ - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, - handler_i_am_bind); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, handler_i_am_bind); /* set the handler for all the services we don't implement It is required to send the proper reject message... */ apdu_set_unrecognized_service_handler_handler @@ -137,13 +142,14 @@ static void Init_Service_Handlers(void) apdu_set_confirmed_simple_ack_handler(SERVICE_CONFIRMED_WRITE_PROPERTY, MyWritePropertySimpleAckHandler); /* handle any errors coming back */ - apdu_set_error_handler(SERVICE_CONFIRMED_WRITE_PROPERTY, - MyErrorHandler); + apdu_set_error_handler(SERVICE_CONFIRMED_WRITE_PROPERTY, MyErrorHandler); apdu_set_abort_handler(MyAbortHandler); apdu_set_reject_handler(MyRejectHandler); } -int main(int argc, char *argv[]) +int main( + int argc, + char *argv[]) { BACNET_ADDRESS src = { 0 }; /* address where message came from */ uint16_t pdu_len = 0; @@ -234,8 +240,7 @@ int main(int argc, char *argv[]) "%s 123 1 0 85 4 100\r\n" "You could also send a relinquish command:\r\n" "%s 123 1 0 85 0 0\r\n", - filename_remove_path(argv[0]), - filename_remove_path(argv[0])); + filename_remove_path(argv[0]), filename_remove_path(argv[0])); } return 0; } @@ -317,8 +322,7 @@ int main(int argc, char *argv[]) timeout_seconds = (Device_APDU_Timeout() / 1000) * Device_Number_Of_APDU_Retries(); /* try to bind with the device */ - Send_WhoIs(Target_Device_Object_Instance, - Target_Device_Object_Instance); + Send_WhoIs(Target_Device_Object_Instance, Target_Device_Object_Instance); /* loop forever */ for (;;) { /* increment timer - exit if timed out */ @@ -333,8 +337,7 @@ int main(int argc, char *argv[]) } /* at least one second has passed */ if (current_seconds != last_seconds) - tsm_timer_milliseconds(((current_seconds - - last_seconds) * 1000)); + tsm_timer_milliseconds(((current_seconds - last_seconds) * 1000)); if (Error_Detected) break; /* wait until the device is bound, or timeout and quit */ diff --git a/bacnet-stack/include/abort.h b/bacnet-stack/include/abort.h index 6e3fd160..ed00cd26 100644 --- a/bacnet-stack/include/abort.h +++ b/bacnet-stack/include/abort.h @@ -39,24 +39,34 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - int abort_encode_apdu(uint8_t * apdu, - uint8_t invoke_id, uint8_t abort_reason, bool server); + int abort_encode_apdu( + uint8_t * apdu, + uint8_t invoke_id, + uint8_t abort_reason, + bool server); - int abort_decode_service_request(uint8_t * apdu, - unsigned apdu_len, uint8_t * invoke_id, uint8_t * abort_reason); + int abort_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + uint8_t * invoke_id, + uint8_t * abort_reason); #ifdef TEST #include "ctest.h" - int abort_decode_apdu(uint8_t * apdu, - unsigned apdu_len, uint8_t * invoke_id, uint8_t * abort_reason, + int abort_decode_apdu( + uint8_t * apdu, + unsigned apdu_len, + uint8_t * invoke_id, + uint8_t * abort_reason, bool * server); - void testAbort(Test * pTest); + void testAbort( + Test * pTest); #endif #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/address.h b/bacnet-stack/include/address.h index 3acb85ad..11aa21d2 100644 --- a/bacnet-stack/include/address.h +++ b/bacnet-stack/include/address.h @@ -41,35 +41,52 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - void address_init(void); + void address_init( + void); - void address_add(uint32_t device_id, - unsigned max_apdu, BACNET_ADDRESS * src); + void address_add( + uint32_t device_id, + unsigned max_apdu, + BACNET_ADDRESS * src); - void address_remove_device(uint32_t device_id); + void address_remove_device( + uint32_t device_id); - bool address_get_by_device(uint32_t device_id, - unsigned *max_apdu, BACNET_ADDRESS * src); + bool address_get_by_device( + uint32_t device_id, + unsigned *max_apdu, + BACNET_ADDRESS * src); - bool address_get_by_index(unsigned index, - uint32_t * device_id, unsigned *max_apdu, BACNET_ADDRESS * src); - - bool address_get_device_id(BACNET_ADDRESS * src, - uint32_t *device_id); + bool address_get_by_index( + unsigned index, + uint32_t * device_id, + unsigned *max_apdu, + BACNET_ADDRESS * src); - unsigned address_count(void); + bool address_get_device_id( + BACNET_ADDRESS * src, + uint32_t * device_id); - bool address_match(BACNET_ADDRESS * dest, BACNET_ADDRESS * src); + unsigned address_count( + void); - bool address_bind_request(uint32_t device_id, - unsigned *max_apdu, BACNET_ADDRESS * src); + bool address_match( + BACNET_ADDRESS * dest, + BACNET_ADDRESS * src); - void address_add_binding(uint32_t device_id, - unsigned max_apdu, BACNET_ADDRESS * src); + bool address_bind_request( + uint32_t device_id, + unsigned *max_apdu, + BACNET_ADDRESS * src); + + void address_add_binding( + uint32_t device_id, + unsigned max_apdu, + BACNET_ADDRESS * src); #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/ai.h b/bacnet-stack/include/ai.h index 5f4920f9..11c5a01f 100644 --- a/bacnet-stack/include/ai.h +++ b/bacnet-stack/include/ai.h @@ -31,28 +31,35 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ void Analog_Input_Property_Lists( const int **pRequired, const int **pOptional, const int **pProprietary); - bool Analog_Input_Valid_Instance(uint32_t object_instance); - unsigned Analog_Input_Count(void); - uint32_t Analog_Input_Index_To_Instance(unsigned index); - char *Analog_Input_Name(uint32_t object_instance); + bool Analog_Input_Valid_Instance( + uint32_t object_instance); + unsigned Analog_Input_Count( + void); + uint32_t Analog_Input_Index_To_Instance( + unsigned index); + char *Analog_Input_Name( + uint32_t object_instance); - int Analog_Input_Encode_Property_APDU(uint8_t * apdu, + int Analog_Input_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code); + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code); #ifdef TEST #include "ctest.h" - void testAnalogInput(Test * pTest); + void testAnalogInput( + Test * pTest); #endif #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/ao.h b/bacnet-stack/include/ao.h index d28ba410..25254d03 100644 --- a/bacnet-stack/include/ao.h +++ b/bacnet-stack/include/ao.h @@ -33,40 +33,53 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ void Analog_Output_Property_Lists( const int **pRequired, const int **pOptional, const int **pProprietary); - bool Analog_Output_Valid_Instance(uint32_t object_instance); - unsigned Analog_Output_Count(void); - uint32_t Analog_Output_Index_To_Instance(unsigned index); - char *Analog_Output_Name(uint32_t object_instance); - float Analog_Output_Present_Value(uint32_t object_instance); - unsigned Analog_Output_Present_Value_Priority(uint32_t - object_instance); - bool Analog_Output_Present_Value_Set(uint32_t object_instance, - float value, unsigned priority); - bool Analog_Output_Present_Value_Relinquish(uint32_t object_instance, + bool Analog_Output_Valid_Instance( + uint32_t object_instance); + unsigned Analog_Output_Count( + void); + uint32_t Analog_Output_Index_To_Instance( + unsigned index); + char *Analog_Output_Name( + uint32_t object_instance); + float Analog_Output_Present_Value( + uint32_t object_instance); + unsigned Analog_Output_Present_Value_Priority( + uint32_t object_instance); + bool Analog_Output_Present_Value_Set( + uint32_t object_instance, + float value, + unsigned priority); + bool Analog_Output_Present_Value_Relinquish( + uint32_t object_instance, int priority); - int Analog_Output_Encode_Property_APDU(uint8_t * apdu, + int Analog_Output_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code); + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code); - bool Analog_Output_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code); + bool Analog_Output_Write_Property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code); #ifdef TEST #include "ctest.h" - void testAnalogOutput(Test * pTest); + void testAnalogOutput( + Test * pTest); #endif #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/apdu.h b/bacnet-stack/include/apdu.h index b37d46a7..59895251 100644 --- a/bacnet-stack/include/apdu.h +++ b/bacnet-stack/include/apdu.h @@ -60,7 +60,7 @@ typedef struct _confirmed_service_ack_data { #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ /* generic unconfirmed function handler */ /* Suitable to handle the following services: */ @@ -68,8 +68,11 @@ extern "C" { /* Unconfirmed_Event_Notification, Unconfirmed_Private_Transfer, */ /* Unconfirmed_Text_Message, Time_Synchronization, Who_Has, */ /* UTC_Time_Synchronization */ - typedef void (*unconfirmed_function) (uint8_t * service_request, - uint16_t len, BACNET_ADDRESS * src); + typedef void ( + *unconfirmed_function) ( + uint8_t * service_request, + uint16_t len, + BACNET_ADDRESS * src); /* generic confirmed function handler */ /* Suitable to handle the following services: */ @@ -87,72 +90,98 @@ extern "C" { /* Confirmed_Text_Message, Reinitialize_Device, */ /* VT_Open, VT_Close, VT_Data_Handler, */ /* Authenticate, Request_Key */ - typedef void (*confirmed_function) (uint8_t * service_request, + typedef void ( + *confirmed_function) ( + uint8_t * service_request, uint16_t service_len, BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data); /* generic confirmed simple ack function handler */ - typedef void (*confirmed_simple_ack_function) (BACNET_ADDRESS * src, + typedef void ( + *confirmed_simple_ack_function) ( + BACNET_ADDRESS * src, uint8_t invoke_id); /* generic confirmed ack function handler */ - typedef void (*confirmed_ack_function) (uint8_t * service_request, + typedef void ( + *confirmed_ack_function) ( + uint8_t * service_request, uint16_t service_len, BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_ACK_DATA * service_data); /* generic error reply function */ - typedef void (*error_function) (BACNET_ADDRESS * src, + typedef void ( + *error_function) ( + BACNET_ADDRESS * src, uint8_t invoke_id, - BACNET_ERROR_CLASS error_class, BACNET_ERROR_CODE error_code); + BACNET_ERROR_CLASS error_class, + BACNET_ERROR_CODE error_code); /* generic abort reply function */ - typedef void (*abort_function) (BACNET_ADDRESS * src, - uint8_t invoke_id, uint8_t abort_reason, bool server); + typedef void ( + *abort_function) ( + BACNET_ADDRESS * src, + uint8_t invoke_id, + uint8_t abort_reason, + bool server); /* generic reject reply function */ - typedef void (*reject_function) (BACNET_ADDRESS * src, - uint8_t invoke_id, uint8_t reject_reason); + typedef void ( + *reject_function) ( + BACNET_ADDRESS * src, + uint8_t invoke_id, + uint8_t reject_reason); - void apdu_set_confirmed_ack_handler(BACNET_CONFIRMED_SERVICE - service_choice, confirmed_ack_function pFunction); + void apdu_set_confirmed_ack_handler( + BACNET_CONFIRMED_SERVICE service_choice, + confirmed_ack_function pFunction); - void apdu_set_confirmed_simple_ack_handler(BACNET_CONFIRMED_SERVICE - service_choice, confirmed_simple_ack_function pFunction); + void apdu_set_confirmed_simple_ack_handler( + BACNET_CONFIRMED_SERVICE service_choice, + confirmed_simple_ack_function pFunction); /* configure reject for confirmed services that are not supported */ - void apdu_set_unrecognized_service_handler_handler(confirmed_function - pFunction); + void apdu_set_unrecognized_service_handler_handler( + confirmed_function pFunction); - void apdu_set_confirmed_handler(BACNET_CONFIRMED_SERVICE - service_choice, confirmed_function pFunction); + void apdu_set_confirmed_handler( + BACNET_CONFIRMED_SERVICE service_choice, + confirmed_function pFunction); - void apdu_set_unconfirmed_handler(BACNET_UNCONFIRMED_SERVICE - service_choice, unconfirmed_function pFunction); + void apdu_set_unconfirmed_handler( + BACNET_UNCONFIRMED_SERVICE service_choice, + unconfirmed_function pFunction); /* returns true if the service is supported by a handler */ - bool apdu_service_supported(BACNET_SERVICES_SUPPORTED - service_supported); + bool apdu_service_supported( + BACNET_SERVICES_SUPPORTED service_supported); - void apdu_set_error_handler(BACNET_CONFIRMED_SERVICE service_choice, + void apdu_set_error_handler( + BACNET_CONFIRMED_SERVICE service_choice, error_function pFunction); - void apdu_set_abort_handler(abort_function pFunction); + void apdu_set_abort_handler( + abort_function pFunction); - void apdu_set_reject_handler(reject_function pFunction); + void apdu_set_reject_handler( + reject_function pFunction); - uint16_t apdu_decode_confirmed_service_request(uint8_t * apdu, /* APDU data */ + uint16_t apdu_decode_confirmed_service_request( + uint8_t * apdu, /* APDU data */ uint16_t apdu_len, BACNET_CONFIRMED_SERVICE_DATA * service_data, uint8_t * service_choice, - uint8_t ** service_request, uint16_t * service_request_len); + uint8_t ** service_request, + uint16_t * service_request_len); - void apdu_handler(BACNET_ADDRESS * src, /* source address */ - uint8_t * apdu, /* APDU data */ + void apdu_handler( + BACNET_ADDRESS * src, /* source address */ + uint8_t * apdu, /* APDU data */ uint16_t pdu_len); /* for confirmed messages */ #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/arcnet.h b/bacnet-stack/include/arcnet.h index 324a0b77..ea01686c 100644 --- a/bacnet-stack/include/arcnet.h +++ b/bacnet-stack/include/arcnet.h @@ -46,30 +46,37 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - bool arcnet_valid(void); - void arcnet_cleanup(void); - bool arcnet_init(char *interface_name); + bool arcnet_valid( + void); + void arcnet_cleanup( + void); + bool arcnet_init( + char *interface_name); /* function to send a packet out the 802.2 socket */ /* returns zero on success, non-zero on failure */ - int arcnet_send_pdu(BACNET_ADDRESS * dest, /* destination address */ + int arcnet_send_pdu( + BACNET_ADDRESS * dest, /* destination address */ BACNET_NPDU_DATA * npdu_data, /* network information */ - uint8_t * pdu, /* any data to be sent - may be null */ + uint8_t * pdu, /* any data to be sent - may be null */ unsigned pdu_len); /* number of bytes of data */ /* receives an framed packet */ /* returns the number of octets in the PDU, or zero on failure */ - uint16_t arcnet_receive(BACNET_ADDRESS * src, /* source address */ - uint8_t * pdu, /* PDU data */ + uint16_t arcnet_receive( + BACNET_ADDRESS * src, /* source address */ + uint8_t * pdu, /* PDU data */ uint16_t max_pdu, /* amount of space available in the PDU */ unsigned timeout); /* milliseconds to wait for a packet */ - void arcnet_get_my_address(BACNET_ADDRESS * my_address); - void arcnet_get_broadcast_address(BACNET_ADDRESS * dest); /* destination address */ + void arcnet_get_my_address( + BACNET_ADDRESS * my_address); + void arcnet_get_broadcast_address( + BACNET_ADDRESS * dest); /* destination address */ #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/arf.h b/bacnet-stack/include/arf.h index b693e737..776f6aea 100644 --- a/bacnet-stack/include/arf.h +++ b/bacnet-stack/include/arf.h @@ -60,43 +60,57 @@ typedef struct BACnet_Atomic_Read_File_Data { #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ /* Atomic Read File */ /* encode service */ - int arf_encode_apdu(uint8_t * apdu, - uint8_t invoke_id, BACNET_ATOMIC_READ_FILE_DATA * data); + int arf_encode_apdu( + uint8_t * apdu, + uint8_t invoke_id, + BACNET_ATOMIC_READ_FILE_DATA * data); /* decode the service request only */ - int arf_decode_service_request(uint8_t * apdu, - unsigned apdu_len, BACNET_ATOMIC_READ_FILE_DATA * data); - - int arf_decode_apdu(uint8_t * apdu, + int arf_decode_service_request( + uint8_t * apdu, unsigned apdu_len, - uint8_t * invoke_id, BACNET_ATOMIC_READ_FILE_DATA * data); + BACNET_ATOMIC_READ_FILE_DATA * data); + + int arf_decode_apdu( + uint8_t * apdu, + unsigned apdu_len, + uint8_t * invoke_id, + BACNET_ATOMIC_READ_FILE_DATA * data); /* Atomic Read File Ack */ /* encode service */ - int arf_ack_encode_apdu(uint8_t * apdu, - uint8_t invoke_id, BACNET_ATOMIC_READ_FILE_DATA * data); + int arf_ack_encode_apdu( + uint8_t * apdu, + uint8_t invoke_id, + BACNET_ATOMIC_READ_FILE_DATA * data); /* decode the service request only */ - int arf_ack_decode_service_request(uint8_t * apdu, - unsigned apdu_len, BACNET_ATOMIC_READ_FILE_DATA * data); - - int arf_ack_decode_apdu(uint8_t * apdu, + int arf_ack_decode_service_request( + uint8_t * apdu, unsigned apdu_len, - uint8_t * invoke_id, BACNET_ATOMIC_READ_FILE_DATA * data); + BACNET_ATOMIC_READ_FILE_DATA * data); + + int arf_ack_decode_apdu( + uint8_t * apdu, + unsigned apdu_len, + uint8_t * invoke_id, + BACNET_ATOMIC_READ_FILE_DATA * data); #ifdef TEST #include "ctest.h" - void test_AtomicReadFile(Test * pTest); - void test_AtomicReadFileAck(Test * pTest); + void test_AtomicReadFile( + Test * pTest); + void test_AtomicReadFileAck( + Test * pTest); #endif #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/av.h b/bacnet-stack/include/av.h index ffd978b2..d271366a 100644 --- a/bacnet-stack/include/av.h +++ b/bacnet-stack/include/av.h @@ -33,31 +33,40 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ void Analog_Value_Property_Lists( const int **pRequired, const int **pOptional, const int **pProprietary); - bool Analog_Value_Valid_Instance(uint32_t object_instance); - unsigned Analog_Value_Count(void); - uint32_t Analog_Value_Index_To_Instance(unsigned index); - char *Analog_Value_Name(uint32_t object_instance); + bool Analog_Value_Valid_Instance( + uint32_t object_instance); + unsigned Analog_Value_Count( + void); + uint32_t Analog_Value_Index_To_Instance( + unsigned index); + char *Analog_Value_Name( + uint32_t object_instance); - int Analog_Value_Encode_Property_APDU(uint8_t * apdu, + int Analog_Value_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code); + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code); - bool Analog_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code); + bool Analog_Value_Write_Property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code); #ifdef TEST #include "ctest.h" - void testAnalog_Value(Test * pTest); + void testAnalog_Value( + Test * pTest); #endif #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/awf.h b/bacnet-stack/include/awf.h index 639a8c98..4a768d60 100644 --- a/bacnet-stack/include/awf.h +++ b/bacnet-stack/include/awf.h @@ -56,42 +56,56 @@ typedef struct BACnet_Atomic_Write_File_Data { #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ /* Atomic Write File */ /* encode service */ - int awf_encode_apdu(uint8_t * apdu, - uint8_t invoke_id, BACNET_ATOMIC_WRITE_FILE_DATA * data); + int awf_encode_apdu( + uint8_t * apdu, + uint8_t invoke_id, + BACNET_ATOMIC_WRITE_FILE_DATA * data); /* decode the service request only */ - int awf_decode_service_request(uint8_t * apdu, - unsigned apdu_len, BACNET_ATOMIC_WRITE_FILE_DATA * data); - - int awf_decode_apdu(uint8_t * apdu, + int awf_decode_service_request( + uint8_t * apdu, unsigned apdu_len, - uint8_t * invoke_id, BACNET_ATOMIC_WRITE_FILE_DATA * data); + BACNET_ATOMIC_WRITE_FILE_DATA * data); + + int awf_decode_apdu( + uint8_t * apdu, + unsigned apdu_len, + uint8_t * invoke_id, + BACNET_ATOMIC_WRITE_FILE_DATA * data); /* Atomic Write File Ack */ /* encode service */ - int awf_ack_encode_apdu(uint8_t * apdu, - uint8_t invoke_id, BACNET_ATOMIC_WRITE_FILE_DATA * data); + int awf_ack_encode_apdu( + uint8_t * apdu, + uint8_t invoke_id, + BACNET_ATOMIC_WRITE_FILE_DATA * data); /* decode the service request only */ - int awf_ack_decode_service_request(uint8_t * apdu, - unsigned apdu_len, BACNET_ATOMIC_WRITE_FILE_DATA * data); - - int awf_ack_decode_apdu(uint8_t * apdu, + int awf_ack_decode_service_request( + uint8_t * apdu, unsigned apdu_len, - uint8_t * invoke_id, BACNET_ATOMIC_WRITE_FILE_DATA * data); + BACNET_ATOMIC_WRITE_FILE_DATA * data); + + int awf_ack_decode_apdu( + uint8_t * apdu, + unsigned apdu_len, + uint8_t * invoke_id, + BACNET_ATOMIC_WRITE_FILE_DATA * data); #ifdef TEST #include "ctest.h" - void test_AtomicWriteFile(Test * pTest); - void test_AtomicWriteFileAck(Test * pTest); + void test_AtomicWriteFile( + Test * pTest); + void test_AtomicWriteFileAck( + Test * pTest); #endif #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/bacaddr.h b/bacnet-stack/include/bacaddr.h index b73405f4..d4db05aa 100644 --- a/bacnet-stack/include/bacaddr.h +++ b/bacnet-stack/include/bacaddr.h @@ -41,12 +41,16 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - void bacnet_address_copy(BACNET_ADDRESS * dest, BACNET_ADDRESS * src); - bool bacnet_address_same(BACNET_ADDRESS * dest, BACNET_ADDRESS * src); + void bacnet_address_copy( + BACNET_ADDRESS * dest, + BACNET_ADDRESS * src); + bool bacnet_address_same( + BACNET_ADDRESS * dest, + BACNET_ADDRESS * src); #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/bacapp.h b/bacnet-stack/include/bacapp.h index 9aa8f2a5..ab34040b 100644 --- a/bacnet-stack/include/bacapp.h +++ b/bacnet-stack/include/bacapp.h @@ -45,7 +45,7 @@ struct BACnet_Application_Data_Value; typedef struct BACnet_Application_Data_Value { bool context_specific; /* true if context specific data */ uint8_t context_tag; /* only used for context specific data */ - uint8_t tag; /* application tag data type */ + uint8_t tag; /* application tag data type */ union { /* NULL - not needed as it is encoded in the tag alone */ #if defined (BACAPP_BOOLEAN) @@ -63,25 +63,25 @@ typedef struct BACnet_Application_Data_Value { #if defined (BACAPP_DOUBLE) double Double; #endif -#if defined (BACAPP_OCTET_STRING) +#if defined (BACAPP_OCTET_STRING) BACNET_OCTET_STRING Octet_String; #endif -#if defined (BACAPP_CHARACTER_STRING) +#if defined (BACAPP_CHARACTER_STRING) BACNET_CHARACTER_STRING Character_String; #endif -#if defined (BACAPP_BIT_STRING) +#if defined (BACAPP_BIT_STRING) BACNET_BIT_STRING Bit_String; #endif #if defined (BACAPP_ENUMERATED) int Enumerated; #endif -#if defined (BACAPP_DATE) +#if defined (BACAPP_DATE) BACNET_DATE Date; #endif -#if defined (BACAPP_TIME) +#if defined (BACAPP_TIME) BACNET_TIME Time; #endif -#if defined (BACAPP_OBJECT_ID) +#if defined (BACAPP_OBJECT_ID) BACNET_OBJECT_ID Object_Id; #endif } type; @@ -91,38 +91,51 @@ typedef struct BACnet_Application_Data_Value { #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ - int bacapp_encode_data(uint8_t * apdu, +#endif /* __cplusplus */ + int bacapp_encode_data( + uint8_t * apdu, BACNET_APPLICATION_DATA_VALUE * value); - int bacapp_decode_application_data(uint8_t * apdu, - int max_apdu_len, BACNET_APPLICATION_DATA_VALUE * value); - - int bacapp_encode_application_data(uint8_t * apdu, + int bacapp_decode_application_data( + uint8_t * apdu, + int max_apdu_len, BACNET_APPLICATION_DATA_VALUE * value); - int bacapp_decode_context_data(uint8_t * apdu, - int max_apdu_len, BACNET_APPLICATION_DATA_VALUE * value, - BACNET_PROPERTY_ID property); + int bacapp_encode_application_data( + uint8_t * apdu, + BACNET_APPLICATION_DATA_VALUE * value); - int bacapp_encode_context_data(uint8_t * apdu, + int bacapp_decode_context_data( + uint8_t * apdu, + int max_apdu_len, BACNET_APPLICATION_DATA_VALUE * value, BACNET_PROPERTY_ID property); - int bacapp_encode_context_data_value(uint8_t * apdu, - uint8_t context_tag_number, BACNET_APPLICATION_DATA_VALUE * value); + int bacapp_encode_context_data( + uint8_t * apdu, + BACNET_APPLICATION_DATA_VALUE * value, + BACNET_PROPERTY_ID property); - BACNET_APPLICATION_TAG bacapp_context_tag_type(BACNET_PROPERTY_ID - property, uint8_t tag_number); + int bacapp_encode_context_data_value( + uint8_t * apdu, + uint8_t context_tag_number, + BACNET_APPLICATION_DATA_VALUE * value); - bool bacapp_copy(BACNET_APPLICATION_DATA_VALUE * dest_value, + BACNET_APPLICATION_TAG bacapp_context_tag_type( + BACNET_PROPERTY_ID property, + uint8_t tag_number); + + bool bacapp_copy( + BACNET_APPLICATION_DATA_VALUE * dest_value, BACNET_APPLICATION_DATA_VALUE * src_value); /* returns the length of data between an opening tag and a closing tag. Expects that the first octet contain the opening tag. Include a value property identifier for context specific data such as the value received in a WriteProperty request */ - int bacapp_data_len(uint8_t * apdu, int max_apdu_len, + int bacapp_data_len( + uint8_t * apdu, + int max_apdu_len, BACNET_PROPERTY_ID property); #if PRINT_ENABLED @@ -134,9 +147,12 @@ extern "C" { #endif #ifdef BACAPP_PRINT_ENABLED - bool bacapp_parse_application_data(BACNET_APPLICATION_TAG tag_number, - const char *argv, BACNET_APPLICATION_DATA_VALUE * value); - bool bacapp_print_value(FILE * stream, + bool bacapp_parse_application_data( + BACNET_APPLICATION_TAG tag_number, + const char *argv, + BACNET_APPLICATION_DATA_VALUE * value); + bool bacapp_print_value( + FILE * stream, BACNET_APPLICATION_DATA_VALUE * value, BACNET_PROPERTY_ID property); #else @@ -147,14 +163,17 @@ extern "C" { #ifdef TEST #include "ctest.h" #include "datetime.h" - bool bacapp_same_value(BACNET_APPLICATION_DATA_VALUE * value, + bool bacapp_same_value( + BACNET_APPLICATION_DATA_VALUE * value, BACNET_APPLICATION_DATA_VALUE * test_value); - void testBACnetApplicationDataLength(Test * pTest); - void testBACnetApplicationData(Test * pTest); + void testBACnetApplicationDataLength( + Test * pTest); + void testBACnetApplicationData( + Test * pTest); #endif #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/bacdcode.h b/bacnet-stack/include/bacdcode.h index dec68ea6..7eefb65b 100644 --- a/bacnet-stack/include/bacdcode.h +++ b/bacnet-stack/include/bacdcode.h @@ -45,133 +45,229 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ /* from clause 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ - int encode_tag(uint8_t * apdu, uint8_t tag_number, - bool context_specific, uint32_t len_value_type); + int encode_tag( + uint8_t * apdu, + uint8_t tag_number, + bool context_specific, + uint32_t len_value_type); /* from clause 20.2.1.3.2 Constructed Data */ /* returns the number of apdu bytes consumed */ - int encode_opening_tag(uint8_t * apdu, uint8_t tag_number); - int encode_closing_tag(uint8_t * apdu, uint8_t tag_number); - int decode_tag_number(uint8_t * apdu, uint8_t * tag_number); - int decode_tag_number_and_value(uint8_t * apdu, uint8_t * tag_number, + int encode_opening_tag( + uint8_t * apdu, + uint8_t tag_number); + int encode_closing_tag( + uint8_t * apdu, + uint8_t tag_number); + int decode_tag_number( + uint8_t * apdu, + uint8_t * tag_number); + int decode_tag_number_and_value( + uint8_t * apdu, + uint8_t * tag_number, uint32_t * value); /* returns true if the tag is context specific */ - bool decode_is_context_specific(uint8_t * apdu); + bool decode_is_context_specific( + uint8_t * apdu); /* returns true if the tag is an opening tag and matches */ - bool decode_is_opening_tag_number(uint8_t * apdu, uint8_t tag_number); + bool decode_is_opening_tag_number( + uint8_t * apdu, + uint8_t tag_number); /* returns true if the tag is a closing tag and matches */ - bool decode_is_closing_tag_number(uint8_t * apdu, uint8_t tag_number); + bool decode_is_closing_tag_number( + uint8_t * apdu, + uint8_t tag_number); /* returns true if the tag is context specific and matches */ - bool decode_is_context_tag(uint8_t * apdu, uint8_t tag_number); + bool decode_is_context_tag( + uint8_t * apdu, + uint8_t tag_number); /* returns true if the tag is an opening tag */ - bool decode_is_opening_tag(uint8_t * apdu); + bool decode_is_opening_tag( + uint8_t * apdu); /* returns true if the tag is a closing tag */ - bool decode_is_closing_tag(uint8_t * apdu); + bool decode_is_closing_tag( + uint8_t * apdu); /* from clause 20.2.2 Encoding of a Null Value */ - int encode_application_null(uint8_t * apdu); - int encode_context_null(uint8_t * apdu, int tag_number); + int encode_application_null( + uint8_t * apdu); + int encode_context_null( + uint8_t * apdu, + int tag_number); /* from clause 20.2.3 Encoding of a Boolean Value */ - int encode_application_boolean(uint8_t * apdu, bool boolean_value); - bool decode_boolean(uint32_t len_value); - int encode_context_boolean(uint8_t * apdu, int tag_number, + int encode_application_boolean( + uint8_t * apdu, bool boolean_value); - bool decode_context_boolean(uint8_t * apdu); + bool decode_boolean( + uint32_t len_value); + int encode_context_boolean( + uint8_t * apdu, + int tag_number, + bool boolean_value); + bool decode_context_boolean( + uint8_t * apdu); /* from clause 20.2.10 Encoding of a Bit String Value */ /* returns the number of apdu bytes consumed */ - int decode_bitstring(uint8_t * apdu, uint32_t len_value, + int decode_bitstring( + uint8_t * apdu, + uint32_t len_value, BACNET_BIT_STRING * bit_string); /* returns the number of apdu bytes consumed */ - int encode_bitstring(uint8_t * apdu, BACNET_BIT_STRING * bit_string); - int encode_application_bitstring(uint8_t * apdu, + int encode_bitstring( + uint8_t * apdu, BACNET_BIT_STRING * bit_string); - int encode_context_bitstring(uint8_t * apdu, int tag_number, + int encode_application_bitstring( + uint8_t * apdu, + BACNET_BIT_STRING * bit_string); + int encode_context_bitstring( + uint8_t * apdu, + int tag_number, BACNET_BIT_STRING * bit_string); /* from clause 20.2.6 Encoding of a Real Number Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ - int encode_application_real(uint8_t * apdu, float value); - int encode_context_real(uint8_t * apdu, int tag_number, float value); + int encode_application_real( + uint8_t * apdu, + float value); + int encode_context_real( + uint8_t * apdu, + int tag_number, + float value); /* from clause 20.2.14 Encoding of an Object Identifier Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ - int decode_object_id(uint8_t * apdu, int *object_type, + int decode_object_id( + uint8_t * apdu, + int *object_type, uint32_t * instance); - int encode_bacnet_object_id(uint8_t * apdu, int object_type, + int encode_bacnet_object_id( + uint8_t * apdu, + int object_type, uint32_t instance); - int encode_context_object_id(uint8_t * apdu, int tag_number, - int object_type, uint32_t instance); - int encode_application_object_id(uint8_t * apdu, int object_type, + int encode_context_object_id( + uint8_t * apdu, + int tag_number, + int object_type, + uint32_t instance); + int encode_application_object_id( + uint8_t * apdu, + int object_type, uint32_t instance); /* from clause 20.2.8 Encoding of an Octet String Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ - int encode_octet_string(uint8_t * apdu, + int encode_octet_string( + uint8_t * apdu, BACNET_OCTET_STRING * octet_string); - int encode_application_octet_string(uint8_t * apdu, + int encode_application_octet_string( + uint8_t * apdu, BACNET_OCTET_STRING * octet_string); - int encode_context_octet_string(uint8_t * apdu, - int tag_number, BACNET_OCTET_STRING * octet_string); - int decode_octet_string(uint8_t * apdu, uint32_t len_value, + int encode_context_octet_string( + uint8_t * apdu, + int tag_number, + BACNET_OCTET_STRING * octet_string); + int decode_octet_string( + uint8_t * apdu, + uint32_t len_value, BACNET_OCTET_STRING * octet_string); /* from clause 20.2.9 Encoding of a Character String Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ - int encode_bacnet_character_string(uint8_t * apdu, + int encode_bacnet_character_string( + uint8_t * apdu, BACNET_CHARACTER_STRING * char_string); - int encode_application_character_string(uint8_t * apdu, + int encode_application_character_string( + uint8_t * apdu, BACNET_CHARACTER_STRING * char_string); - int encode_context_character_string(uint8_t * apdu, int tag_number, + int encode_context_character_string( + uint8_t * apdu, + int tag_number, BACNET_CHARACTER_STRING * char_string); - int decode_character_string(uint8_t * apdu, uint32_t len_value, + int decode_character_string( + uint8_t * apdu, + uint32_t len_value, BACNET_CHARACTER_STRING * char_string); /* from clause 20.2.4 Encoding of an Unsigned Integer Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ - int encode_bacnet_unsigned(uint8_t * apdu, uint32_t value); - int encode_context_unsigned(uint8_t * apdu, int tag_number, + int encode_bacnet_unsigned( + uint8_t * apdu, uint32_t value); - int encode_application_unsigned(uint8_t * apdu, uint32_t value); - int decode_unsigned(uint8_t * apdu, uint32_t len_value, + int encode_context_unsigned( + uint8_t * apdu, + int tag_number, + uint32_t value); + int encode_application_unsigned( + uint8_t * apdu, + uint32_t value); + int decode_unsigned( + uint8_t * apdu, + uint32_t len_value, uint32_t * value); /* from clause 20.2.5 Encoding of a Signed Integer Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ - int encode_bacnet_signed(uint8_t * apdu, int32_t value); - int encode_application_signed(uint8_t * apdu, int32_t value); - int encode_context_signed(uint8_t * apdu, int tag_number, + int encode_bacnet_signed( + uint8_t * apdu, int32_t value); - int decode_signed(uint8_t * apdu, uint32_t len_value, int32_t * value); + int encode_application_signed( + uint8_t * apdu, + int32_t value); + int encode_context_signed( + uint8_t * apdu, + int tag_number, + int32_t value); + int decode_signed( + uint8_t * apdu, + uint32_t len_value, + int32_t * value); /* from clause 20.2.11 Encoding of an Enumerated Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ - int decode_enumerated(uint8_t * apdu, uint32_t len_value, int *value); - int encode_bacnet_enumerated(uint8_t * apdu, int value); - int encode_application_enumerated(uint8_t * apdu, int value); - int encode_context_enumerated(uint8_t * apdu, int tag_number, + int decode_enumerated( + uint8_t * apdu, + uint32_t len_value, + int *value); + int encode_bacnet_enumerated( + uint8_t * apdu, + int value); + int encode_application_enumerated( + uint8_t * apdu, + int value); + int encode_context_enumerated( + uint8_t * apdu, + int tag_number, int value); /* from clause 20.2.13 Encoding of a Time Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ - int encode_bacnet_time(uint8_t * apdu, BACNET_TIME * btime); - int encode_application_time(uint8_t * apdu, BACNET_TIME * btime); - int decode_bacnet_time(uint8_t * apdu, BACNET_TIME * btime); - int encode_context_time(uint8_t * apdu, int tag_number, + int encode_bacnet_time( + uint8_t * apdu, + BACNET_TIME * btime); + int encode_application_time( + uint8_t * apdu, + BACNET_TIME * btime); + int decode_bacnet_time( + uint8_t * apdu, + BACNET_TIME * btime); + int encode_context_time( + uint8_t * apdu, + int tag_number, BACNET_TIME * btime); /* BACnet Date */ @@ -183,24 +279,38 @@ extern "C" { /* from clause 20.2.12 Encoding of a Date Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ - int encode_bacnet_date(uint8_t * apdu, BACNET_DATE * bdate); - int encode_application_date(uint8_t * apdu, BACNET_DATE * bdate); - int encode_context_date(uint8_t * apdu, int tag_number, + int encode_bacnet_date( + uint8_t * apdu, + BACNET_DATE * bdate); + int encode_application_date( + uint8_t * apdu, + BACNET_DATE * bdate); + int encode_context_date( + uint8_t * apdu, + int tag_number, + BACNET_DATE * bdate); + int decode_date( + uint8_t * apdu, BACNET_DATE * bdate); - int decode_date(uint8_t * apdu, BACNET_DATE * bdate); /* from clause 20.1.2.4 max-segments-accepted */ /* and clause 20.1.2.5 max-APDU-length-accepted */ /* returns the encoded octet */ - uint8_t encode_max_segs_max_apdu(int max_segs, int max_apdu); - int decode_max_segs(uint8_t octet); - int decode_max_apdu(uint8_t octet); + uint8_t encode_max_segs_max_apdu( + int max_segs, + int max_apdu); + int decode_max_segs( + uint8_t octet); + int decode_max_apdu( + uint8_t octet); /* returns the number of apdu bytes consumed */ - int encode_simple_ack(uint8_t * apdu, uint8_t invoke_id, + int encode_simple_ack( + uint8_t * apdu, + uint8_t invoke_id, uint8_t service_choice); #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/bacdef.h b/bacnet-stack/include/bacdef.h index 24e80d84..929a1830 100644 --- a/bacnet-stack/include/bacdef.h +++ b/bacnet-stack/include/bacdef.h @@ -71,10 +71,10 @@ struct BACnet_Device_Address { /* DNET,DLEN,DADR or SNET,SLEN,SADR */ /* the following are used if the device is behind a router */ /* net = 0 indicates local */ - uint16_t net; /* BACnet network number */ + uint16_t net; /* BACnet network number */ /* LEN = 0 denotes broadcast MAC ADR and ADR field is absent */ /* LEN > 0 specifies length of ADR field */ - uint8_t len; /* length of MAC address */ + uint8_t len; /* length of MAC address */ uint8_t adr[MAX_MAC_LEN]; /* hwaddr (MAC) address */ }; typedef struct BACnet_Device_Address BACNET_ADDRESS; diff --git a/bacnet-stack/include/bacenum.h b/bacnet-stack/include/bacenum.h index ff822ee7..f853e805 100644 --- a/bacnet-stack/include/bacenum.h +++ b/bacnet-stack/include/bacenum.h @@ -279,12 +279,12 @@ typedef enum { PROP_RAMP_RATE = 241, PROP_STEP_INCREMENT = 242, PROP_SYSTEM_FAILURE_VALUE = 243 - /* The special property identifiers all, optional, and required */ - /* are reserved for use in the ReadPropertyConditional and */ - /* ReadPropertyMultiple services or services not defined in this standard. */ - /* Enumerated values 0-511 are reserved for definition by ASHRAE. */ - /* Enumerated values 512-4194303 may be used by others subject to the */ - /* procedures and constraints described in Clause 23. */ + /* The special property identifiers all, optional, and required */ + /* are reserved for use in the ReadPropertyConditional and */ + /* ReadPropertyMultiple services or services not defined in this standard. */ + /* Enumerated values 0-511 are reserved for definition by ASHRAE. */ + /* Enumerated values 512-4194303 may be used by others subject to the */ + /* procedures and constraints described in Clause 23. */ } BACNET_PROPERTY_ID; #define MAX_BACNET_PROPERTY_ID 4194303 @@ -294,11 +294,11 @@ typedef enum { } BACNET_ACTION; typedef enum { - MIN_BINARY_PV = 0, /* for validating incoming values */ + MIN_BINARY_PV = 0, /* for validating incoming values */ BINARY_INACTIVE = 0, BINARY_ACTIVE = 1, - MAX_BINARY_PV = 1, /* for validating incoming values */ - BINARY_NULL = 2 /* our homemade way of storing this info */ + MAX_BINARY_PV = 1, /* for validating incoming values */ + BINARY_NULL = 2 /* our homemade way of storing this info */ } BACNET_BINARY_PV; typedef enum { @@ -538,10 +538,10 @@ typedef enum { UNITS_SQUARE_METERS_PER_NEWTON = 185, UNITS_WATTS_PER_METER_PER_DEGREE_KELVIN = 189, UNITS_WATTS_PER_SQUARE_METER_DEGREE_KELVIN = 141 - /* Enumerated values 0-255 are reserved for definition by ASHRAE. */ - /* Enumerated values 256-65535 may be used by others subject to */ - /* the procedures and constraints described in Clause 23. */ - /* The last enumeration used in this version is 189. */ + /* Enumerated values 0-255 are reserved for definition by ASHRAE. */ + /* Enumerated values 256-65535 may be used by others subject to */ + /* the procedures and constraints described in Clause 23. */ + /* The last enumeration used in this version is 189. */ } BACNET_ENGINEERING_UNITS; typedef enum { @@ -573,9 +573,9 @@ typedef enum { PROGRAM_ERROR_INTERNAL = 2, PROGRAM_ERROR_PROGRAM = 3, PROGRAM_ERROR_OTHER = 4 - /* Enumerated values 0-63 are reserved for definition by ASHRAE. */ - /* Enumerated values 64-65535 may be used by others subject to */ - /* the procedures and constraints described in Clause 23. */ + /* Enumerated values 0-63 are reserved for definition by ASHRAE. */ + /* Enumerated values 64-65535 may be used by others subject to */ + /* the procedures and constraints described in Clause 23. */ } BACNET_PROGRAM_ERROR; typedef enum { @@ -592,9 +592,9 @@ typedef enum { RELIABILITY_CONFIGURATION_ERROR = 10, RELIABILITY_COMMUNICATION_FAILURE = 12, RELIABILITY_TRIPPED = 13 - /* Enumerated values 0-63 are reserved for definition by ASHRAE. */ - /* Enumerated values 64-65535 may be used by others subject to */ - /* the procedures and constraints described in Clause 23. */ + /* Enumerated values 0-63 are reserved for definition by ASHRAE. */ + /* Enumerated values 64-65535 may be used by others subject to */ + /* the procedures and constraints described in Clause 23. */ } BACNET_RELIABILITY; typedef enum { @@ -610,13 +610,13 @@ typedef enum { EVENT_EXTENDED = 9, EVENT_BUFFER_READY = 10, EVENT_UNSIGNED_RANGE = 11 - /* Enumerated values 0-63 are reserved for definition by ASHRAE. */ - /* Enumerated values 64-65535 may be used by others subject to */ - /* the procedures and constraints described in Clause 23. */ - /* It is expected that these enumerated values will correspond to */ - /* the use of the complex-event-type CHOICE [6] of the */ - /* BACnetNotificationParameters production. */ - /* The last enumeration used in this version is 11. */ + /* Enumerated values 0-63 are reserved for definition by ASHRAE. */ + /* Enumerated values 64-65535 may be used by others subject to */ + /* the procedures and constraints described in Clause 23. */ + /* It is expected that these enumerated values will correspond to */ + /* the use of the complex-event-type CHOICE [6] of the */ + /* BACnetNotificationParameters production. */ + /* The last enumeration used in this version is 11. */ } BACNET_EVENT_TYPE; typedef enum { @@ -643,9 +643,9 @@ typedef enum { LIFE_SAFETY_MODE_AUTOMATIC_RELEASE_DISABLED = 13, LIFE_SAFETY_MODE_DEFAULT = 14, MAX_LIFE_SAFETY_MODE = 14 - /* Enumerated values 0-255 are reserved for definition by ASHRAE. */ - /* Enumerated values 256-65535 may be used by others subject to */ - /* procedures and constraints described in Clause 23. */ + /* Enumerated values 0-255 are reserved for definition by ASHRAE. */ + /* Enumerated values 256-65535 may be used by others subject to */ + /* procedures and constraints described in Clause 23. */ } BACNET_LIFE_SAFETY_MODE; typedef enum { @@ -691,9 +691,9 @@ typedef enum { LIFE_SAFETY_STATE_SUPERVISORY = 22, LIFE_SAFETY_STATE_TEST_SUPERVISORY = 23, MAX_LIFE_SAFETY_STATE = 0 - /* Enumerated values 0-255 are reserved for definition by ASHRAE. */ - /* Enumerated values 256-65535 may be used by others subject to */ - /* procedures and constraints described in Clause 23. */ + /* Enumerated values 0-255 are reserved for definition by ASHRAE. */ + /* Enumerated values 256-65535 may be used by others subject to */ + /* procedures and constraints described in Clause 23. */ } BACNET_LIFE_SAFETY_STATE; typedef enum { @@ -1035,8 +1035,8 @@ typedef enum { NETWORK_MESSAGE_INITIALIZE_ROUTING_TABLE_ACK = 7, NETWORK_MESSAGE_ESTABLISH_CONNECTION_TO_NETWORK = 8, NETWORK_MESSAGE_DISCONNECT_CONNECTION_TO_NETWORK = 9, - /* X'0A' to X'7F': Reserved for use by ASHRAE, */ - /* X'80' to X'FF': Available for vendor proprietary messages */ + /* X'0A' to X'7F': Reserved for use by ASHRAE, */ + /* X'80' to X'FF': Available for vendor proprietary messages */ NETWORK_MESSAGE_INVALID = 0x100 } BACNET_NETWORK_MESSAGE_TYPE; @@ -1221,21 +1221,21 @@ typedef enum BACnetShedState { } BACNET_SHED_STATE; typedef enum BACnetLightingOperation { - BACNET_LIGHTS_STOP = 0, - BACNET_LIGHTS_FADE_TO = 1, - BACNET_LIGHTS_FADE_TO_OVER = 2, - BACNET_LIGHTS_RAMP_TO = 3, - BACNET_LIGHTS_RAMP_TO_AT_RATE = 4, - BACNET_LIGHTS_RAMP_UP = 5, - BACNET_LIGHTS_RAMP_UP_AT_RATE = 6, - BACNET_LIGHTS_RAMP_DOWN = 7, - BACNET_LIGHTS_RAMP_DOWN_AT_RATE = 8, - BACNET_LIGHTS_STEP_UP = 9, - BACNET_LIGHTS_STEP_DOWN = 10, - BACNET_LIGHTS_STEP_UP_BY = 11, - BACNET_LIGHTS_STEP_DOWN_BY = 12, - BACNET_LIGHTS_GOTO_LEVEL = 13, - BACNET_LIGHTS_RELINQUISH = 14 + BACNET_LIGHTS_STOP = 0, + BACNET_LIGHTS_FADE_TO = 1, + BACNET_LIGHTS_FADE_TO_OVER = 2, + BACNET_LIGHTS_RAMP_TO = 3, + BACNET_LIGHTS_RAMP_TO_AT_RATE = 4, + BACNET_LIGHTS_RAMP_UP = 5, + BACNET_LIGHTS_RAMP_UP_AT_RATE = 6, + BACNET_LIGHTS_RAMP_DOWN = 7, + BACNET_LIGHTS_RAMP_DOWN_AT_RATE = 8, + BACNET_LIGHTS_STEP_UP = 9, + BACNET_LIGHTS_STEP_DOWN = 10, + BACNET_LIGHTS_STEP_UP_BY = 11, + BACNET_LIGHTS_STEP_DOWN_BY = 12, + BACNET_LIGHTS_GOTO_LEVEL = 13, + BACNET_LIGHTS_RELINQUISH = 14 } BACNET_LIGHTING_OPERATION; /* NOTE: BACNET_DAYS_OF_WEEK is different than BACNET_WEEKDAY */ @@ -1247,7 +1247,7 @@ typedef enum BACnetDaysOfWeek { BACNET_DAYS_OF_WEEK_THURSDAY = 3, BACNET_DAYS_OF_WEEK_FRIDAY = 4, BACNET_DAYS_OF_WEEK_SATURDAY = 5, - BACNET_DAYS_OF_WEEK_SUNDAY = 6 + BACNET_DAYS_OF_WEEK_SUNDAY = 6 } BACNET_DAYS_OF_WEEK; -#endif /* end of BACENUM_H */ +#endif /* end of BACENUM_H */ diff --git a/bacnet-stack/include/bacerror.h b/bacnet-stack/include/bacerror.h index 60b3c883..b4a2ba89 100644 --- a/bacnet-stack/include/bacerror.h +++ b/bacnet-stack/include/bacerror.h @@ -40,35 +40,44 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - int bacerror_encode_apdu(uint8_t * apdu, + int bacerror_encode_apdu( + uint8_t * apdu, uint8_t invoke_id, BACNET_CONFIRMED_SERVICE service, - BACNET_ERROR_CLASS error_class, BACNET_ERROR_CODE error_code); + BACNET_ERROR_CLASS error_class, + BACNET_ERROR_CODE error_code); - int bacerror_decode_service_request(uint8_t * apdu, + int bacerror_decode_service_request( + uint8_t * apdu, unsigned apdu_len, uint8_t * invoke_id, BACNET_CONFIRMED_SERVICE * service, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code); + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code); - int bacerror_decode_error_class_and_code(uint8_t * apdu, + int bacerror_decode_error_class_and_code( + uint8_t * apdu, unsigned apdu_len, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code); + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code); #ifdef TEST #include "ctest.h" - int bacerror_decode_apdu(uint8_t * apdu, + int bacerror_decode_apdu( + uint8_t * apdu, unsigned apdu_len, uint8_t * invoke_id, BACNET_CONFIRMED_SERVICE * service, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code); + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code); - void testBACError(Test * pTest); + void testBACError( + Test * pTest); #endif #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/bacfile.h b/bacnet-stack/include/bacfile.h index 530395cc..653f1121 100644 --- a/bacnet-stack/include/bacfile.h +++ b/bacnet-stack/include/bacfile.h @@ -44,40 +44,52 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ void BACfile_Property_Lists( const int **pRequired, const int **pOptional, const int **pProprietary); - char *bacfile_name(uint32_t instance); - bool bacfile_valid_instance(uint32_t object_instance); - uint32_t bacfile_count(void); - uint32_t bacfile_index_to_instance(unsigned find_index); - uint32_t bacfile_instance(char *filename); + char *bacfile_name( + uint32_t instance); + bool bacfile_valid_instance( + uint32_t object_instance); + uint32_t bacfile_count( + void); + uint32_t bacfile_index_to_instance( + unsigned find_index); + uint32_t bacfile_instance( + char *filename); /* this is one way to match up the invoke ID with */ /* the file ID from the AtomicReadFile request. */ /* Another way would be to store the */ /* invokeID and file instance in a list or table */ /* when the request was sent */ - uint32_t bacfile_instance_from_tsm(uint8_t invokeID); + uint32_t bacfile_instance_from_tsm( + uint8_t invokeID); /* handler ACK helper */ - bool bacfile_read_data(BACNET_ATOMIC_READ_FILE_DATA * data); - bool bacfile_write_stream_data(BACNET_ATOMIC_WRITE_FILE_DATA * data); + bool bacfile_read_data( + BACNET_ATOMIC_READ_FILE_DATA * data); + bool bacfile_write_stream_data( + BACNET_ATOMIC_WRITE_FILE_DATA * data); /* handling for read property service */ - int bacfile_encode_property_apdu(uint8_t * apdu, + int bacfile_encode_property_apdu( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code); + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code); /* handling for write property service */ - bool bacfile_write_property(BACNET_WRITE_PROPERTY_DATA * wp_data, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code); + bool bacfile_write_property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code); #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/bacint.h b/bacnet-stack/include/bacint.h index 8580d551..d40d9fc0 100644 --- a/bacnet-stack/include/bacint.h +++ b/bacnet-stack/include/bacint.h @@ -40,29 +40,55 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ /* unsigned value encoding and decoding */ - int encode_unsigned16(uint8_t * apdu, uint16_t value); - int decode_unsigned16(uint8_t * apdu, uint16_t * value); - int encode_unsigned24(uint8_t * apdu, uint32_t value); - int decode_unsigned24(uint8_t * apdu, uint32_t * value); - int encode_unsigned32(uint8_t * apdu, uint32_t value); - int decode_unsigned32(uint8_t * apdu, uint32_t * value); + int encode_unsigned16( + uint8_t * apdu, + uint16_t value); + int decode_unsigned16( + uint8_t * apdu, + uint16_t * value); + int encode_unsigned24( + uint8_t * apdu, + uint32_t value); + int decode_unsigned24( + uint8_t * apdu, + uint32_t * value); + int encode_unsigned32( + uint8_t * apdu, + uint32_t value); + int decode_unsigned32( + uint8_t * apdu, + uint32_t * value); /* signed value encoding and decoding */ - int encode_signed8(uint8_t * apdu, int8_t value); - int decode_signed8(uint8_t * apdu, int32_t * value); - int encode_signed16(uint8_t * apdu, int16_t value); - int decode_signed16(uint8_t * apdu, int32_t * value); - int encode_signed24(uint8_t * apdu, int32_t value); - int decode_signed24(uint8_t * apdu, int32_t * value); - int encode_signed32(uint8_t * apdu, int32_t value); - int decode_signed32(uint8_t * apdu, int32_t * value); - + int encode_signed8( + uint8_t * apdu, + int8_t value); + int decode_signed8( + uint8_t * apdu, + int32_t * value); + int encode_signed16( + uint8_t * apdu, + int16_t value); + int decode_signed16( + uint8_t * apdu, + int32_t * value); + int encode_signed24( + uint8_t * apdu, + int32_t value); + int decode_signed24( + uint8_t * apdu, + int32_t * value); + int encode_signed32( + uint8_t * apdu, + int32_t value); + int decode_signed32( + uint8_t * apdu, + int32_t * value); + #ifdef __cplusplus } -#endif /* __cplusplus */ - +#endif /* __cplusplus */ #endif - diff --git a/bacnet-stack/include/bacprop.h b/bacnet-stack/include/bacprop.h index 9fb34dc8..60ba6e34 100644 --- a/bacnet-stack/include/bacprop.h +++ b/bacnet-stack/include/bacprop.h @@ -39,20 +39,24 @@ #include "bacenum.h" typedef struct { - signed prop_id; /* index number that matches the text */ - signed tag_id; /* text pair - use NULL to end the list */ + signed prop_id; /* index number that matches the text */ + signed tag_id; /* text pair - use NULL to end the list */ } PROP_TAG_DATA; #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - signed bacprop_tag_by_index_default(PROP_TAG_DATA * data_list, - signed index, signed default_ret); + signed bacprop_tag_by_index_default( + PROP_TAG_DATA * data_list, + signed index, + signed default_ret); - signed bacprop_property_tag(BACNET_OBJECT_TYPE type, signed prop); + signed bacprop_property_tag( + BACNET_OBJECT_TYPE type, + signed prop); #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/bacreal.h b/bacnet-stack/include/bacreal.h index 1e8a81be..c215bfae 100644 --- a/bacnet-stack/include/bacreal.h +++ b/bacnet-stack/include/bacreal.h @@ -40,20 +40,23 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - int decode_real(uint8_t * apdu, float *real_value); - int encode_bacnet_real(float value, uint8_t * apdu); + int decode_real( + uint8_t * apdu, + float *real_value); + int encode_bacnet_real( + float value, + uint8_t * apdu); #ifdef TEST #include "ctest.h" - void testBACreal(Test * pTest); + void testBACreal( + Test * pTest); #endif - + #ifdef __cplusplus } -#endif /* __cplusplus */ - +#endif /* __cplusplus */ #endif - diff --git a/bacnet-stack/include/bacstr.h b/bacnet-stack/include/bacstr.h index 8b21def8..6abd4f4f 100644 --- a/bacnet-stack/include/bacstr.h +++ b/bacnet-stack/include/bacstr.h @@ -65,23 +65,36 @@ typedef struct BACnet_Octet_String { #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - void bitstring_init(BACNET_BIT_STRING * bit_string); - void bitstring_set_bit(BACNET_BIT_STRING * bit_string, uint8_t bit_number, + void bitstring_init( + BACNET_BIT_STRING * bit_string); + void bitstring_set_bit( + BACNET_BIT_STRING * bit_string, + uint8_t bit_number, bool value); - bool bitstring_bit(BACNET_BIT_STRING * bit_string, uint8_t bit_number); - uint8_t bitstring_bits_used(BACNET_BIT_STRING * bit_string); + bool bitstring_bit( + BACNET_BIT_STRING * bit_string, + uint8_t bit_number); + uint8_t bitstring_bits_used( + BACNET_BIT_STRING * bit_string); /* returns the number of bytes that a bit string is using */ - int bitstring_bytes_used(BACNET_BIT_STRING * bit_string); - uint8_t bitstring_bits_capacity(BACNET_BIT_STRING * bit_string); + int bitstring_bytes_used( + BACNET_BIT_STRING * bit_string); + uint8_t bitstring_bits_capacity( + BACNET_BIT_STRING * bit_string); /* used for encoding and decoding from the APDU */ - uint8_t bitstring_octet(BACNET_BIT_STRING * bit_string, + uint8_t bitstring_octet( + BACNET_BIT_STRING * bit_string, uint8_t octet_index); - bool bitstring_set_octet(BACNET_BIT_STRING * bit_string, uint8_t index, + bool bitstring_set_octet( + BACNET_BIT_STRING * bit_string, + uint8_t index, uint8_t octet); - bool bitstring_set_bits_used(BACNET_BIT_STRING * bit_string, - uint8_t bytes_used, uint8_t unused_bits); + bool bitstring_set_bits_used( + BACNET_BIT_STRING * bit_string, + uint8_t bytes_used, + uint8_t unused_bits); bool bitstring_copy( BACNET_BIT_STRING * dest, BACNET_BIT_STRING * src); @@ -89,57 +102,80 @@ extern "C" { /* returns false if the string exceeds capacity initialize by using length=0 */ - bool characterstring_init(BACNET_CHARACTER_STRING * char_string, - uint8_t encoding, char *value, size_t length); + bool characterstring_init( + BACNET_CHARACTER_STRING * char_string, + uint8_t encoding, + char *value, + size_t length); /* used for ANSI C-Strings */ - bool characterstring_init_ansi(BACNET_CHARACTER_STRING * char_string, + bool characterstring_init_ansi( + BACNET_CHARACTER_STRING * char_string, char *value); - bool characterstring_copy(BACNET_CHARACTER_STRING * dest, + bool characterstring_copy( + BACNET_CHARACTER_STRING * dest, BACNET_CHARACTER_STRING * src); /* returns true if the strings are the same length, encoding, value */ - bool characterstring_same(BACNET_CHARACTER_STRING * dest, + bool characterstring_same( + BACNET_CHARACTER_STRING * dest, BACNET_CHARACTER_STRING * src); - bool characterstring_ansi_same(BACNET_CHARACTER_STRING * dest, + bool characterstring_ansi_same( + BACNET_CHARACTER_STRING * dest, const char *src); /* returns false if the string exceeds capacity */ - bool characterstring_append(BACNET_CHARACTER_STRING * char_string, - char *value, size_t length); + bool characterstring_append( + BACNET_CHARACTER_STRING * char_string, + char *value, + size_t length); /* This function sets a new length without changing the value. If length exceeds capacity, no modification happens and function returns false. */ - bool characterstring_truncate(BACNET_CHARACTER_STRING * char_string, + bool characterstring_truncate( + BACNET_CHARACTER_STRING * char_string, size_t length); - bool characterstring_set_encoding(BACNET_CHARACTER_STRING * - char_string, uint8_t encoding); + bool characterstring_set_encoding( + BACNET_CHARACTER_STRING * char_string, + uint8_t encoding); /* Returns the value */ - char *characterstring_value(BACNET_CHARACTER_STRING * char_string); + char *characterstring_value( + BACNET_CHARACTER_STRING * char_string); /* returns the length */ - size_t characterstring_length(BACNET_CHARACTER_STRING * char_string); - uint8_t characterstring_encoding(BACNET_CHARACTER_STRING * - char_string); - size_t characterstring_capacity(BACNET_CHARACTER_STRING * char_string); + size_t characterstring_length( + BACNET_CHARACTER_STRING * char_string); + uint8_t characterstring_encoding( + BACNET_CHARACTER_STRING * char_string); + size_t characterstring_capacity( + BACNET_CHARACTER_STRING * char_string); /* returns false if the string exceeds capacity initialize by using length=0 */ - bool octetstring_init(BACNET_OCTET_STRING * octet_string, - uint8_t * value, size_t length); - bool octetstring_copy(BACNET_OCTET_STRING * dest, + bool octetstring_init( + BACNET_OCTET_STRING * octet_string, + uint8_t * value, + size_t length); + bool octetstring_copy( + BACNET_OCTET_STRING * dest, BACNET_OCTET_STRING * src); /* returns false if the string exceeds capacity */ - bool octetstring_append(BACNET_OCTET_STRING * octet_string, - uint8_t * value, size_t length); + bool octetstring_append( + BACNET_OCTET_STRING * octet_string, + uint8_t * value, + size_t length); /* This function sets a new length without changing the value. If length exceeds capacity, no modification happens and function returns false. */ - bool octetstring_truncate(BACNET_OCTET_STRING * octet_string, + bool octetstring_truncate( + BACNET_OCTET_STRING * octet_string, size_t length); /* Returns the value */ - uint8_t *octetstring_value(BACNET_OCTET_STRING * octet_string); + uint8_t *octetstring_value( + BACNET_OCTET_STRING * octet_string); /* Returns the length.*/ - size_t octetstring_length(BACNET_OCTET_STRING * octet_string); - size_t octetstring_capacity(BACNET_OCTET_STRING * octet_string); + size_t octetstring_length( + BACNET_OCTET_STRING * octet_string); + size_t octetstring_capacity( + BACNET_OCTET_STRING * octet_string); #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/bactext.h b/bacnet-stack/include/bactext.h index 2e961c7e..48f1c2aa 100644 --- a/bacnet-stack/include/bactext.h +++ b/bacnet-stack/include/bactext.h @@ -51,31 +51,50 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - const char *bactext_confirmed_service_name(int index); - const char *bactext_unconfirmed_service_name(int index); - const char *bactext_application_tag_name(int index); - const char *bactext_object_type_name(int index); - const char *bactext_property_name(int index); - const char *bactext_engineering_unit_name(int index); - const char *bactext_reject_reason_name(int index); - const char *bactext_abort_reason_name(int index); - const char *bactext_error_class_name(int index); - const char *bactext_error_code_name(int index); - unsigned bactext_property_id(const char *name); - const char *bactext_month_name(int index); - const char *bactext_week_of_month_name(int index); - const char *bactext_day_of_week_name(int index); - const char *bactext_event_state_name(int index); - const char *bactext_binary_present_value_name(int index); - const char *bactext_reliability_name(int index); - const char *bactext_device_status_name(int index); - const char *bactext_segmentation_name(int index); + const char *bactext_confirmed_service_name( + int index); + const char *bactext_unconfirmed_service_name( + int index); + const char *bactext_application_tag_name( + int index); + const char *bactext_object_type_name( + int index); + const char *bactext_property_name( + int index); + const char *bactext_engineering_unit_name( + int index); + const char *bactext_reject_reason_name( + int index); + const char *bactext_abort_reason_name( + int index); + const char *bactext_error_class_name( + int index); + const char *bactext_error_code_name( + int index); + unsigned bactext_property_id( + const char *name); + const char *bactext_month_name( + int index); + const char *bactext_week_of_month_name( + int index); + const char *bactext_day_of_week_name( + int index); + const char *bactext_event_state_name( + int index); + const char *bactext_binary_present_value_name( + int index); + const char *bactext_reliability_name( + int index); + const char *bactext_device_status_name( + int index); + const char *bactext_segmentation_name( + int index); #ifdef __cplusplus } -#endif /* __cplusplus */ -#endif /* BACTEXT_PRINT_ENABLED */ +#endif /* __cplusplus */ +#endif /* BACTEXT_PRINT_ENABLED */ #endif diff --git a/bacnet-stack/include/bi.h b/bacnet-stack/include/bi.h index 5725889f..e29548d1 100644 --- a/bacnet-stack/include/bi.h +++ b/bacnet-stack/include/bi.h @@ -31,29 +31,36 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ void Binary_Input_Property_Lists( const int **pRequired, const int **pOptional, const int **pProprietary); - bool Binary_Input_Valid_Instance(uint32_t object_instance); - unsigned Binary_Input_Count(void); - uint32_t Binary_Input_Index_To_Instance(unsigned index); - char *Binary_Input_Name(uint32_t object_instance); + bool Binary_Input_Valid_Instance( + uint32_t object_instance); + unsigned Binary_Input_Count( + void); + uint32_t Binary_Input_Index_To_Instance( + unsigned index); + char *Binary_Input_Name( + uint32_t object_instance); - int Binary_Input_Encode_Property_APDU(uint8_t * apdu, + int Binary_Input_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code); + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code); #ifdef TEST #include "ctest.h" - void testBinaryInput(Test * pTest); + void testBinaryInput( + Test * pTest); #endif #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/bigend.h b/bacnet-stack/include/bigend.h index c82b3a65..9d5eb316 100644 --- a/bacnet-stack/include/bigend.h +++ b/bacnet-stack/include/bigend.h @@ -3,7 +3,7 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ /* Big-Endian systems save the most significant byte first. */ /* Sun and Motorola processors, IBM-370s and PDP-10s are big-endian. */ @@ -21,9 +21,10 @@ extern "C" { /* x[2] = 0x03 */ /* x[3] = 0x04 */ - int big_endian(void); + int big_endian( + void); #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/bip.h b/bacnet-stack/include/bip.h index 0a2a66b8..4700353b 100644 --- a/bacnet-stack/include/bip.h +++ b/bacnet-stack/include/bip.h @@ -51,51 +51,66 @@ extern bool BIP_Debug; #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ /* note: define init and cleanup in your ports section */ /* on Linux, ifname is eth0, ath0, arc0, and others. on Windows, ifname is the dotted ip address of the interface */ - bool bip_init(char *ifname); + bool bip_init( + char *ifname); /* normal functions... */ - void bip_cleanup(void); - void bip_set_socket(int sock_fd); - int bip_socket(void); - bool bip_valid(void); - void bip_get_broadcast_address(BACNET_ADDRESS * dest); /* destination address */ - void bip_get_my_address(BACNET_ADDRESS * my_address); + void bip_cleanup( + void); + void bip_set_socket( + int sock_fd); + int bip_socket( + void); + bool bip_valid( + void); + void bip_get_broadcast_address( + BACNET_ADDRESS * dest); /* destination address */ + void bip_get_my_address( + BACNET_ADDRESS * my_address); /* function to send a packet out the BACnet/IP socket */ /* returns zero on success, non-zero on failure */ - int bip_send_pdu(BACNET_ADDRESS * dest, /* destination address */ + int bip_send_pdu( + BACNET_ADDRESS * dest, /* destination address */ BACNET_NPDU_DATA * npdu_data, /* network information */ - uint8_t * pdu, /* any data to be sent - may be null */ + uint8_t * pdu, /* any data to be sent - may be null */ unsigned pdu_len); /* number of bytes of data */ /* receives a BACnet/IP packet */ /* returns the number of octets in the PDU, or zero on failure */ - uint16_t bip_receive(BACNET_ADDRESS * src, /* source address */ - uint8_t * pdu, /* PDU data */ + uint16_t bip_receive( + BACNET_ADDRESS * src, /* source address */ + uint8_t * pdu, /* PDU data */ uint16_t max_pdu, /* amount of space available in the PDU */ unsigned timeout); /* milliseconds to wait for a packet */ /* use host byte order for setting */ - void bip_set_port(uint16_t port); + void bip_set_port( + uint16_t port); /* returns host byte order */ - uint16_t bip_get_port(void); + uint16_t bip_get_port( + void); /* use network byte order for setting */ - void bip_set_addr(uint32_t net_address); + void bip_set_addr( + uint32_t net_address); /* returns host byte order */ - uint32_t bip_get_addr(void); + uint32_t bip_get_addr( + void); /* use network byte order for setting */ - void bip_set_broadcast_addr(uint32_t net_address); + void bip_set_broadcast_addr( + uint32_t net_address); /* returns host byte order */ - uint32_t bip_get_broadcast_addr(void); + uint32_t bip_get_broadcast_addr( + void); #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/bo.h b/bacnet-stack/include/bo.h index 2fd1117f..846ab657 100644 --- a/bacnet-stack/include/bo.h +++ b/bacnet-stack/include/bo.h @@ -33,32 +33,41 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ void Binary_Output_Property_Lists( const int **pRequired, const int **pOptional, const int **pProprietary); - bool Binary_Output_Valid_Instance(uint32_t object_instance); - unsigned Binary_Output_Count(void); - uint32_t Binary_Output_Index_To_Instance(unsigned index); - char *Binary_Output_Name(uint32_t object_instance); + bool Binary_Output_Valid_Instance( + uint32_t object_instance); + unsigned Binary_Output_Count( + void); + uint32_t Binary_Output_Index_To_Instance( + unsigned index); + char *Binary_Output_Name( + uint32_t object_instance); - int Binary_Output_Encode_Property_APDU(uint8_t * apdu, + int Binary_Output_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code); + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code); - bool Binary_Output_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code); + bool Binary_Output_Write_Property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code); #ifdef TEST #include "ctest.h" - void testBinaryOutput(Test * pTest); + void testBinaryOutput( + Test * pTest); #endif #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/bv.h b/bacnet-stack/include/bv.h index b50a444e..80e9ae72 100644 --- a/bacnet-stack/include/bv.h +++ b/bacnet-stack/include/bv.h @@ -33,32 +33,41 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ void Binary_Value_Property_Lists( const int **pRequired, const int **pOptional, const int **pProprietary); - bool Binary_Value_Valid_Instance(uint32_t object_instance); - unsigned Binary_Value_Count(void); - uint32_t Binary_Value_Index_To_Instance(unsigned index); - char *Binary_Value_Name(uint32_t object_instance); + bool Binary_Value_Valid_Instance( + uint32_t object_instance); + unsigned Binary_Value_Count( + void); + uint32_t Binary_Value_Index_To_Instance( + unsigned index); + char *Binary_Value_Name( + uint32_t object_instance); - int Binary_Value_Encode_Property_APDU(uint8_t * apdu, + int Binary_Value_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code); + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code); - bool Binary_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code); + bool Binary_Value_Write_Property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code); #ifdef TEST #include "ctest.h" - void testBinary_Value(Test * pTest); + void testBinary_Value( + Test * pTest); #endif #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/bvlc.h b/bacnet-stack/include/bvlc.h index b78a91de..c2a81d00 100644 --- a/bacnet-stack/include/bvlc.h +++ b/bacnet-stack/include/bvlc.h @@ -43,21 +43,21 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ uint16_t bvlc_receive( BACNET_ADDRESS * src, /* returns the source address */ - uint8_t * npdu, /* returns the NPDU */ + uint8_t * npdu, /* returns the NPDU */ uint16_t max_npdu, /* amount of space available in the NPDU */ - unsigned timeout); /* number of milliseconds to wait for a packet */ + unsigned timeout); /* number of milliseconds to wait for a packet */ - int bvlc_send_pdu(BACNET_ADDRESS * dest, /* destination address */ - BACNET_NPDU_DATA * npdu_data, /* network information */ - uint8_t * pdu, /* any data to be sent - may be null */ + int bvlc_send_pdu( + BACNET_ADDRESS * dest, /* destination address */ + BACNET_NPDU_DATA * npdu_data, /* network information */ + uint8_t * pdu, /* any data to be sent - may be null */ unsigned pdu_len); #ifdef __cplusplus } -#endif /* __cplusplus */ -#endif /* */ - +#endif /* __cplusplus */ +#endif /* */ diff --git a/bacnet-stack/include/bytes.h b/bacnet-stack/include/bytes.h index f1c360ab..a9684e6c 100644 --- a/bacnet-stack/include/bytes.h +++ b/bacnet-stack/include/bytes.h @@ -67,4 +67,4 @@ -#endif /* end of header file */ +#endif /* end of header file */ diff --git a/bacnet-stack/include/client.h b/bacnet-stack/include/client.h index 459efee2..e57b6cbd 100644 --- a/bacnet-stack/include/client.h +++ b/bacnet-stack/include/client.h @@ -35,56 +35,81 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ /* unconfirmed requests */ - void Send_WhoIs(int32_t low_limit, int32_t high_limit); + void Send_WhoIs( + int32_t low_limit, + int32_t high_limit); - void Send_WhoHas_Object(int32_t low_limit, + void Send_WhoHas_Object( + int32_t low_limit, int32_t high_limit, - BACNET_OBJECT_TYPE object_type, uint32_t object_instance); - - void Send_WhoHas_Name(int32_t low_limit, - int32_t high_limit, char *object_name); - - void Send_I_Have(uint32_t device_id, BACNET_OBJECT_TYPE object_type, - uint32_t object_instance, char *object_name); + uint32_t object_instance); -/* returns the invoke ID for confirmed request, or 0 if failed */ - uint8_t Send_Read_Property_Request(uint32_t device_id, /* destination device */ + void Send_WhoHas_Name( + int32_t low_limit, + int32_t high_limit, + char *object_name); + + void Send_I_Have( + uint32_t device_id, BACNET_OBJECT_TYPE object_type, uint32_t object_instance, - BACNET_PROPERTY_ID object_property, int32_t array_index); + char *object_name); /* returns the invoke ID for confirmed request, or 0 if failed */ - uint8_t Send_Write_Property_Request(uint32_t device_id, /* destination device */ + uint8_t Send_Read_Property_Request( + uint32_t device_id, /* destination device */ + BACNET_OBJECT_TYPE object_type, + uint32_t object_instance, + BACNET_PROPERTY_ID object_property, + int32_t array_index); + +/* returns the invoke ID for confirmed request, or 0 if failed */ + uint8_t Send_Write_Property_Request( + uint32_t device_id, /* destination device */ BACNET_OBJECT_TYPE object_type, uint32_t object_instance, BACNET_PROPERTY_ID object_property, BACNET_APPLICATION_DATA_VALUE * object_value, - uint8_t priority, int32_t array_index); + uint8_t priority, + int32_t array_index); /* returns the invoke ID for confirmed request, or 0 if failed */ - uint8_t Send_Reinitialize_Device_Request(uint32_t device_id, - BACNET_REINITIALIZED_STATE state, char *password); + uint8_t Send_Reinitialize_Device_Request( + uint32_t device_id, + BACNET_REINITIALIZED_STATE state, + char *password); /* returns the invoke ID for confirmed request, or 0 if failed */ - uint8_t Send_Device_Communication_Control_Request(uint32_t device_id, uint16_t timeDuration, /* 0=optional */ - BACNET_COMMUNICATION_ENABLE_DISABLE state, char *password); /* NULL=optional */ + uint8_t Send_Device_Communication_Control_Request( + uint32_t device_id, + uint16_t timeDuration, /* 0=optional */ + BACNET_COMMUNICATION_ENABLE_DISABLE state, + char *password); /* NULL=optional */ - void Send_TimeSync(BACNET_DATE * bdate, BACNET_TIME * btime); - void Send_TimeSyncUTC(BACNET_DATE * bdate, BACNET_TIME * btime); + void Send_TimeSync( + BACNET_DATE * bdate, + BACNET_TIME * btime); + void Send_TimeSyncUTC( + BACNET_DATE * bdate, + BACNET_TIME * btime); - uint8_t Send_Atomic_Read_File_Stream(uint32_t device_id, - uint32_t file_instance, int fileStartPosition, - unsigned requestedOctetCount); - uint8_t Send_Atomic_Write_File_Stream(uint32_t device_id, + uint8_t Send_Atomic_Read_File_Stream( + uint32_t device_id, uint32_t file_instance, - int fileStartPosition, BACNET_OCTET_STRING * fileData); + int fileStartPosition, + unsigned requestedOctetCount); + uint8_t Send_Atomic_Write_File_Stream( + uint32_t device_id, + uint32_t file_instance, + int fileStartPosition, + BACNET_OCTET_STRING * fileData); #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/config.h b/bacnet-stack/include/config.h index 5766ca1d..1510b317 100644 --- a/bacnet-stack/include/config.h +++ b/bacnet-stack/include/config.h @@ -7,29 +7,29 @@ /* declare a single physical layer using your compiler define. see datalink.h for possible defines. */ #if !(defined(BACDL_ETHERNET) || defined(BACDL_ARCNET) || defined(BACDL_MSTP) || defined(BACDL_BIP) || defined(BACDL_TEST)) - #define BACDL_BIP +#define BACDL_BIP #endif /* optional debug info for BACnet/IP datalink layers */ #if defined(BACDL_BIP) - #if !defined(USE_INADDR) - #define USE_INADDR 1 - #endif +#if !defined(USE_INADDR) +#define USE_INADDR 1 +#endif #endif /* Define your processor architecture as Big Endian (PowerPC,68K,Sparc) or Little Endian (Intel,AVR) ARM and MIPS can be either - what is your setup? */ #if !defined(BIG_ENDIAN) - #define BIG_ENDIAN 0 +#define BIG_ENDIAN 0 #endif /* Define your Vendor Identifier assigned by ASHRAE */ #if !defined(BACNET_VENDOR_ID) - #define BACNET_VENDOR_ID 260 +#define BACNET_VENDOR_ID 260 #endif #if !defined(BACNET_VENDOR_NAME) - #define BACNET_VENDOR_NAME "BACnet Stack at SourceForge" +#define BACNET_VENDOR_NAME "BACnet Stack at SourceForge" #endif /* Max number of bytes in an APDU. */ @@ -39,7 +39,7 @@ /* Lon=206, MS/TP=480, ARCNET=480, Ethernet=1476, BACnet/IP=64K */ #if !defined(MAX_APDU) /* #define MAX_APDU 50 */ - #define MAX_APDU 480 +#define MAX_APDU 480 /* #define MAX_APDU 1476 */ #endif @@ -49,7 +49,7 @@ /* Configure from 1..255 for number of outstanding confirmed */ /* requests available. */ #if !defined(MAX_TSM_TRANSACTIONS) - #define MAX_TSM_TRANSACTIONS 255 +#define MAX_TSM_TRANSACTIONS 255 #endif /* The address cache is used for binding to BACnet devices */ /* The number of entries corresponds to the number of */ @@ -57,30 +57,30 @@ /* If your device is a simple server and does not need to bind, */ /* then you don't need to use this. */ #if !defined(MAX_ADDRESS_CACHE) - #define MAX_ADDRESS_CACHE 255 +#define MAX_ADDRESS_CACHE 255 #endif /* some modules have debugging enabled using PRINT_ENABLED */ #if !defined(PRINT_ENABLED) - #define PRINT_ENABLED 0 +#define PRINT_ENABLED 0 #endif /* BACAPP decodes WriteProperty service requests Choose the datatypes that your application supports */ #if defined (BACAPP_ALL) - #define BACAPP_NULL - #define BACAPP_BOOLEAN - #define BACAPP_UNSIGNED - #define BACAPP_SIGNED - #define BACAPP_REAL +#define BACAPP_NULL +#define BACAPP_BOOLEAN +#define BACAPP_UNSIGNED +#define BACAPP_SIGNED +#define BACAPP_REAL /* FIXME: not implemented #define BACAPP_DOUBLE */ - #define BACAPP_OCTET_STRING - #define BACAPP_CHARACTER_STRING - #define BACAPP_BIT_STRING - #define BACAPP_ENUMERATED - #define BACAPP_DATE - #define BACAPP_TIME - #define BACAPP_OBJECT_ID +#define BACAPP_OCTET_STRING +#define BACAPP_CHARACTER_STRING +#define BACAPP_BIT_STRING +#define BACAPP_ENUMERATED +#define BACAPP_DATE +#define BACAPP_TIME +#define BACAPP_OBJECT_ID #endif #endif diff --git a/bacnet-stack/include/cov.h b/bacnet-stack/include/cov.h index f70e3372..2e43835e 100644 --- a/bacnet-stack/include/cov.h +++ b/bacnet-stack/include/cov.h @@ -67,54 +67,78 @@ typedef struct BACnet_Subscribe_COV_Data { BACNET_OBJECT_ID monitoredObjectIdentifier; bool cancellationRequest; /* true if this is a cancellation request */ bool issueConfirmedNotifications; /* optional */ - unsigned lifetime; /* optional */ + unsigned lifetime; /* optional */ BACNET_PROPERTY_REFERENCE monitoredProperty; bool covIncrementPresent; /* true if present */ - float covIncrement; /* optional */ + float covIncrement; /* optional */ } BACNET_SUBSCRIBE_COV_DATA; #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - int ucov_notify_encode_apdu(uint8_t * apdu, BACNET_COV_DATA * data); + int ucov_notify_encode_apdu( + uint8_t * apdu, + BACNET_COV_DATA * data); - int ucov_notify_decode_apdu(uint8_t * apdu, - unsigned apdu_len, BACNET_COV_DATA * data); + int ucov_notify_decode_apdu( + uint8_t * apdu, + unsigned apdu_len, + BACNET_COV_DATA * data); - int ucov_notify_send(uint8_t * buffer, BACNET_COV_DATA * data); + int ucov_notify_send( + uint8_t * buffer, + BACNET_COV_DATA * data); - int ccov_notify_encode_apdu(uint8_t * apdu, - uint8_t invoke_id, BACNET_COV_DATA * data); + int ccov_notify_encode_apdu( + uint8_t * apdu, + uint8_t invoke_id, + BACNET_COV_DATA * data); - int ccov_notify_decode_apdu(uint8_t * apdu, - unsigned apdu_len, uint8_t * invoke_id, BACNET_COV_DATA * data); + int ccov_notify_decode_apdu( + uint8_t * apdu, + unsigned apdu_len, + uint8_t * invoke_id, + BACNET_COV_DATA * data); /* common for both confirmed and unconfirmed */ - int cov_notify_decode_service_request(uint8_t * apdu, - unsigned apdu_len, BACNET_COV_DATA * data); + int cov_notify_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + BACNET_COV_DATA * data); - int cov_subscribe_property_decode_service_request(uint8_t * apdu, - unsigned apdu_len, BACNET_SUBSCRIBE_COV_DATA * data); + int cov_subscribe_property_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + BACNET_SUBSCRIBE_COV_DATA * data); - int cov_subscribe_property_encode_adpu(uint8_t * apdu, - uint8_t invoke_id, BACNET_SUBSCRIBE_COV_DATA * data); + int cov_subscribe_property_encode_adpu( + uint8_t * apdu, + uint8_t invoke_id, + BACNET_SUBSCRIBE_COV_DATA * data); - int cov_subscribe_decode_service_request(uint8_t * apdu, - unsigned apdu_len, BACNET_SUBSCRIBE_COV_DATA * data); + int cov_subscribe_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + BACNET_SUBSCRIBE_COV_DATA * data); - int cov_subscribe_encode_adpu(uint8_t * apdu, - uint8_t invoke_id, BACNET_SUBSCRIBE_COV_DATA * data); + int cov_subscribe_encode_adpu( + uint8_t * apdu, + uint8_t invoke_id, + BACNET_SUBSCRIBE_COV_DATA * data); #ifdef TEST #include "ctest.h" - void testCOVNotify(Test * pTest); - void testCOVSubscribeProperty(Test * pTest); - void testCOVSubscribe(Test * pTest); + void testCOVNotify( + Test * pTest); + void testCOVSubscribeProperty( + Test * pTest); + void testCOVSubscribe( + Test * pTest); #endif #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/crc.h b/bacnet-stack/include/crc.h index c0af37fc..96ce28d2 100644 --- a/bacnet-stack/include/crc.h +++ b/bacnet-stack/include/crc.h @@ -40,12 +40,16 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - uint8_t CRC_Calc_Header(uint8_t dataValue, uint8_t crcValue); - uint16_t CRC_Calc_Data(uint8_t dataValue, uint16_t crcValue); + uint8_t CRC_Calc_Header( + uint8_t dataValue, + uint8_t crcValue); + uint16_t CRC_Calc_Data( + uint8_t dataValue, + uint16_t crcValue); #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/datalink.h b/bacnet-stack/include/datalink.h index de75b5af..fcfd195c 100644 --- a/bacnet-stack/include/datalink.h +++ b/bacnet-stack/include/datalink.h @@ -69,16 +69,16 @@ #elif defined(BACDL_BIP) #include "bip.h" #ifdef BBMD_ENABLED - #include "bvlc.h" +#include "bvlc.h" #endif #define datalink_init bip_init #ifdef BBMD_ENABLED - #define datalink_send_pdu bvlc_send_pdu - #define datalink_receive bvlc_receive +#define datalink_send_pdu bvlc_send_pdu +#define datalink_receive bvlc_receive #else - #define datalink_send_pdu bip_send_pdu - #define datalink_receive bip_receive +#define datalink_send_pdu bip_send_pdu +#define datalink_receive bip_receive #endif #define datalink_cleanup bip_cleanup #define datalink_get_broadcast_address bip_get_broadcast_address @@ -87,14 +87,24 @@ #else #include "npdu.h" -extern int datalink_send_pdu(BACNET_ADDRESS * dest, - BACNET_NPDU_DATA * npdu_data, uint8_t * pdu, unsigned pdu_len); -extern uint16_t datalink_receive(BACNET_ADDRESS * src, - uint8_t * pdu, uint16_t max_pdu, unsigned timeout); -extern void datalink_cleanup(void); -extern void datalink_get_broadcast_address(BACNET_ADDRESS * dest); -extern void datalink_get_my_address(BACNET_ADDRESS * my_address); -extern void datalink_set_interface(char *ifname); +extern int datalink_send_pdu( + BACNET_ADDRESS * dest, + BACNET_NPDU_DATA * npdu_data, + uint8_t * pdu, + unsigned pdu_len); +extern uint16_t datalink_receive( + BACNET_ADDRESS * src, + uint8_t * pdu, + uint16_t max_pdu, + unsigned timeout); +extern void datalink_cleanup( + void); +extern void datalink_get_broadcast_address( + BACNET_ADDRESS * dest); +extern void datalink_get_my_address( + BACNET_ADDRESS * my_address); +extern void datalink_set_interface( + char *ifname); #endif diff --git a/bacnet-stack/include/datetime.h b/bacnet-stack/include/datetime.h index 66aa96f9..43907289 100644 --- a/bacnet-stack/include/datetime.h +++ b/bacnet-stack/include/datetime.h @@ -49,10 +49,10 @@ typedef enum BACnet_Weekday { /* date */ typedef struct BACnet_Date { - uint16_t year; /* AD */ - uint8_t month; /* 1=Jan */ - uint8_t day; /* 1..31 */ - uint8_t wday; /* 1=Monday-7=Sunday */ + uint16_t year; /* AD */ + uint8_t month; /* 1=Jan */ + uint8_t day; /* 1..31 */ + uint8_t wday; /* 1=Monday-7=Sunday */ } BACNET_DATE; /* time */ @@ -70,45 +70,75 @@ typedef struct BACnet_DateTime { #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ /* utility initialization functions */ - void datetime_set_date(BACNET_DATE * bdate, - uint16_t year, uint8_t month, uint8_t day); - void datetime_set_time(BACNET_TIME * btime, - uint8_t hour, uint8_t minute, uint8_t seconds, uint8_t hundredths); - void datetime_set(BACNET_DATE_TIME * bdatetime, - BACNET_DATE * bdate, BACNET_TIME * btime); - void datetime_set_values(BACNET_DATE_TIME * bdatetime, - uint16_t year, uint8_t month, uint8_t day, - uint8_t hour, uint8_t minute, uint8_t seconds, uint8_t hundredths); + void datetime_set_date( + BACNET_DATE * bdate, + uint16_t year, + uint8_t month, + uint8_t day); + void datetime_set_time( + BACNET_TIME * btime, + uint8_t hour, + uint8_t minute, + uint8_t seconds, + uint8_t hundredths); + void datetime_set( + BACNET_DATE_TIME * bdatetime, + BACNET_DATE * bdate, + BACNET_TIME * btime); + void datetime_set_values( + BACNET_DATE_TIME * bdatetime, + uint16_t year, + uint8_t month, + uint8_t day, + uint8_t hour, + uint8_t minute, + uint8_t seconds, + uint8_t hundredths); /* utility comparison functions: if the date/times are the same, return is 0 if date1 is before date2, returns negative if date1 is after date2, returns positive */ - int datetime_compare_date(BACNET_DATE * date1, BACNET_DATE * date2); - int datetime_compare_time(BACNET_TIME * time1, BACNET_TIME * time2); - int datetime_compare(BACNET_DATE_TIME * datetime1, + int datetime_compare_date( + BACNET_DATE * date1, + BACNET_DATE * date2); + int datetime_compare_time( + BACNET_TIME * time1, + BACNET_TIME * time2); + int datetime_compare( + BACNET_DATE_TIME * datetime1, BACNET_DATE_TIME * datetime2); /* utility copy functions */ - void datetime_copy_date(BACNET_DATE * date1, BACNET_DATE * date2); - void datetime_copy_time(BACNET_TIME * time1, BACNET_TIME * time2); - void datetime_copy(BACNET_DATE_TIME * datetime1, + void datetime_copy_date( + BACNET_DATE * date1, + BACNET_DATE * date2); + void datetime_copy_time( + BACNET_TIME * time1, + BACNET_TIME * time2); + void datetime_copy( + BACNET_DATE_TIME * datetime1, BACNET_DATE_TIME * datetime2); /* utility add function */ - void datetime_add_minutes(BACNET_DATE_TIME * bdatetime, + void datetime_add_minutes( + BACNET_DATE_TIME * bdatetime, uint32_t minutes); /* date and time wildcards */ - bool datetime_wildcard(BACNET_DATE_TIME * bdatetime); - void datetime_wildcard_set(BACNET_DATE_TIME * bdatetime); - void datetime_date_wildcard_set(BACNET_DATE * bdate); - void datetime_time_wildcard_set(BACNET_TIME * btime); + bool datetime_wildcard( + BACNET_DATE_TIME * bdatetime); + void datetime_wildcard_set( + BACNET_DATE_TIME * bdatetime); + void datetime_date_wildcard_set( + BACNET_DATE * bdate); + void datetime_time_wildcard_set( + BACNET_TIME * btime); #ifdef __cplusplus } -#endif /* __cplusplus */ -#endif /* DATE_TIME_H */ +#endif /* __cplusplus */ +#endif /* DATE_TIME_H */ diff --git a/bacnet-stack/include/dcc.h b/bacnet-stack/include/dcc.h index c4557120..448d07e5 100644 --- a/bacnet-stack/include/dcc.h +++ b/bacnet-stack/include/dcc.h @@ -41,28 +41,40 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ /* return the status */ - BACNET_COMMUNICATION_ENABLE_DISABLE dcc_enable_status(void); - bool dcc_communication_enabled(void); - bool dcc_communication_disabled(void); - bool dcc_communication_initiation_disabled(void); + BACNET_COMMUNICATION_ENABLE_DISABLE dcc_enable_status( + void); + bool dcc_communication_enabled( + void); + bool dcc_communication_disabled( + void); + bool dcc_communication_initiation_disabled( + void); /* return the time */ - uint32_t dcc_duration_seconds(void); + uint32_t dcc_duration_seconds( + void); /* called every second or so. If more than one second, then seconds should be the number of seconds to tick away */ - void dcc_timer_seconds(uint32_t seconds); + void dcc_timer_seconds( + uint32_t seconds); /* setup the communication values */ - bool dcc_set_status_duration(BACNET_COMMUNICATION_ENABLE_DISABLE - status, uint16_t minutes); + bool dcc_set_status_duration( + BACNET_COMMUNICATION_ENABLE_DISABLE status, + uint16_t minutes); /* encode service */ - int dcc_encode_apdu(uint8_t * apdu, uint8_t invoke_id, uint16_t timeDuration, /* 0=optional */ - BACNET_COMMUNICATION_ENABLE_DISABLE enable_disable, BACNET_CHARACTER_STRING * password); /* NULL=optional */ + int dcc_encode_apdu( + uint8_t * apdu, + uint8_t invoke_id, + uint16_t timeDuration, /* 0=optional */ + BACNET_COMMUNICATION_ENABLE_DISABLE enable_disable, + BACNET_CHARACTER_STRING * password); /* NULL=optional */ /* decode the service request only */ - int dcc_decode_service_request(uint8_t * apdu, + int dcc_decode_service_request( + uint8_t * apdu, unsigned apdu_len, uint16_t * timeDuration, BACNET_COMMUNICATION_ENABLE_DISABLE * enable_disable, @@ -70,17 +82,19 @@ extern "C" { #ifdef TEST #include "ctest.h" - int dcc_decode_apdu(uint8_t * apdu, + int dcc_decode_apdu( + uint8_t * apdu, unsigned apdu_len, uint8_t * invoke_id, uint16_t * timeDuration, BACNET_COMMUNICATION_ENABLE_DISABLE * enable_disable, BACNET_CHARACTER_STRING * password); - void test_DeviceCommunicationControl(Test * pTest); + void test_DeviceCommunicationControl( + Test * pTest); #endif #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/device.h b/bacnet-stack/include/device.h index 64033842..18dd5b07 100644 --- a/bacnet-stack/include/device.h +++ b/bacnet-stack/include/device.h @@ -42,71 +42,110 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ void Device_Property_Lists( const int **pRequired, const int **pOptional, const int **pProprietary); - uint32_t Device_Object_Instance_Number(void); - bool Device_Set_Object_Instance_Number(uint32_t object_id); - bool Device_Valid_Object_Instance_Number(uint32_t object_id); - unsigned Device_Object_List_Count(void); - bool Device_Object_List_Identifier(unsigned array_index, - int *object_type, uint32_t * instance); + uint32_t Device_Object_Instance_Number( + void); + bool Device_Set_Object_Instance_Number( + uint32_t object_id); + bool Device_Valid_Object_Instance_Number( + uint32_t object_id); + unsigned Device_Object_List_Count( + void); + bool Device_Object_List_Identifier( + unsigned array_index, + int *object_type, + uint32_t * instance); - BACNET_DEVICE_STATUS Device_System_Status(void); - void Device_Set_System_Status(BACNET_DEVICE_STATUS status); + BACNET_DEVICE_STATUS Device_System_Status( + void); + void Device_Set_System_Status( + BACNET_DEVICE_STATUS status); - const char *Device_Vendor_Name(void); + const char *Device_Vendor_Name( + void); - uint16_t Device_Vendor_Identifier(void); + uint16_t Device_Vendor_Identifier( + void); - const char *Device_Model_Name(void); - bool Device_Set_Model_Name(const char *name, size_t length); - - const char *Device_Firmware_Revision(void); - - const char *Device_Application_Software_Version(void); - bool Device_Set_Application_Software_Version(const char *name, + const char *Device_Model_Name( + void); + bool Device_Set_Model_Name( + const char *name, size_t length); - const char *Device_Description(void); - bool Device_Set_Description(const char *name, size_t length); + const char *Device_Firmware_Revision( + void); - const char *Device_Location(void); - bool Device_Set_Location(const char *name, size_t length); + const char *Device_Application_Software_Version( + void); + bool Device_Set_Application_Software_Version( + const char *name, + size_t length); + + const char *Device_Description( + void); + bool Device_Set_Description( + const char *name, + size_t length); + + const char *Device_Location( + void); + bool Device_Set_Location( + const char *name, + size_t length); /* some stack-centric constant values - no set methods */ - uint8_t Device_Protocol_Version(void); - uint8_t Device_Protocol_Revision(void); - uint16_t Device_Max_APDU_Length_Accepted(void); - BACNET_SEGMENTATION Device_Segmentation_Supported(void); + uint8_t Device_Protocol_Version( + void); + uint8_t Device_Protocol_Revision( + void); + uint16_t Device_Max_APDU_Length_Accepted( + void); + BACNET_SEGMENTATION Device_Segmentation_Supported( + void); - uint16_t Device_APDU_Timeout(void); - void Device_Set_APDU_Timeout(uint16_t timeout); + uint16_t Device_APDU_Timeout( + void); + void Device_Set_APDU_Timeout( + uint16_t timeout); - uint8_t Device_Number_Of_APDU_Retries(void); - void Device_Set_Number_Of_APDU_Retries(uint8_t retries); + uint8_t Device_Number_Of_APDU_Retries( + void); + void Device_Set_Number_Of_APDU_Retries( + uint8_t retries); - uint8_t Device_Database_Revision(void); - void Device_Set_Database_Revision(uint8_t revision); + uint8_t Device_Database_Revision( + void); + void Device_Set_Database_Revision( + uint8_t revision); - bool Device_Valid_Object_Name(const char *object_name, - int *object_type, uint32_t * object_instance); - char *Device_Valid_Object_Id(int object_type, + bool Device_Valid_Object_Name( + const char *object_name, + int *object_type, + uint32_t * object_instance); + char *Device_Valid_Object_Id( + int object_type, uint32_t object_instance); - int Device_Encode_Property_APDU(uint8_t * apdu, + int Device_Encode_Property_APDU( + uint8_t * apdu, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code); + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code); - bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code); + bool Device_Write_Property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code); #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/dlmstp.h b/bacnet-stack/include/dlmstp.h index 81726a9a..77572aaf 100644 --- a/bacnet-stack/include/dlmstp.h +++ b/bacnet-stack/include/dlmstp.h @@ -46,29 +46,33 @@ #define MAX_MPDU (MAX_HEADER+MAX_PDU) typedef struct dlmstp_packet { - bool ready; /* true if ready to be sent or received */ + bool ready; /* true if ready to be sent or received */ BACNET_ADDRESS address; /* source address */ - uint8_t frame_type; /* type of message */ - uint16_t pdu_len; /* packet length */ + uint8_t frame_type; /* type of message */ + uint16_t pdu_len; /* packet length */ uint8_t pdu[MAX_MPDU]; /* packet */ } DLMSTP_PACKET; #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - bool dlmstp_init(char *ifname); - void dlmstp_cleanup(void); + bool dlmstp_init( + char *ifname); + void dlmstp_cleanup( + void); /* returns number of bytes sent on success, negative on failure */ - int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */ + int dlmstp_send_pdu( + BACNET_ADDRESS * dest, /* destination address */ BACNET_NPDU_DATA * npdu_data, /* network information */ - uint8_t * pdu, /* any data to be sent - may be null */ + uint8_t * pdu, /* any data to be sent - may be null */ unsigned pdu_len); /* number of bytes of data */ /* returns the number of octets in the PDU, or zero on failure */ - uint16_t dlmstp_receive(BACNET_ADDRESS * src, /* source address */ - uint8_t * pdu, /* PDU data */ + uint16_t dlmstp_receive( + BACNET_ADDRESS * src, /* source address */ + uint8_t * pdu, /* PDU data */ uint16_t max_pdu, /* amount of space available in the PDU */ unsigned timeout); /* milliseconds to wait for a packet */ @@ -79,29 +83,39 @@ extern "C" { /* nodes. This may be used to allocate more or less of the available link */ /* bandwidth to particular nodes. If Max_Info_Frames is not writable in a */ /* node, its value shall be 1. */ - void dlmstp_set_max_info_frames(uint8_t max_info_frames); - uint8_t dlmstp_max_info_frames(void); + void dlmstp_set_max_info_frames( + uint8_t max_info_frames); + uint8_t dlmstp_max_info_frames( + void); /* This parameter represents the value of the Max_Master property of the */ /* node's Device object. The value of Max_Master specifies the highest */ /* allowable address for master nodes. The value of Max_Master shall be */ /* less than or equal to 127. If Max_Master is not writable in a node, */ /* its value shall be 127. */ - void dlmstp_set_max_master(uint8_t max_master); - uint8_t dlmstp_max_master(void); + void dlmstp_set_max_master( + uint8_t max_master); + uint8_t dlmstp_max_master( + void); /* MAC address 0-127 */ - void dlmstp_set_mac_address(uint8_t my_address); - uint8_t dlmstp_mac_address(void); + void dlmstp_set_mac_address( + uint8_t my_address); + uint8_t dlmstp_mac_address( + void); - void dlmstp_get_my_address(BACNET_ADDRESS * my_address); - void dlmstp_get_broadcast_address(BACNET_ADDRESS * dest); /* destination address */ + void dlmstp_get_my_address( + BACNET_ADDRESS * my_address); + void dlmstp_get_broadcast_address( + BACNET_ADDRESS * dest); /* destination address */ /* RS485 Baud Rate 9600, 19200, 38400, 57600, 115200 */ - void dlmstp_set_baud_rate(uint32_t baud); - uint32_t dlmstp_baud_rate(void); + void dlmstp_set_baud_rate( + uint32_t baud); + uint32_t dlmstp_baud_rate( + void); #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/ethernet.h b/bacnet-stack/include/ethernet.h index 12ed8632..95979380 100644 --- a/bacnet-stack/include/ethernet.h +++ b/bacnet-stack/include/ethernet.h @@ -46,31 +46,39 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - bool ethernet_valid(void); - void ethernet_cleanup(void); - bool ethernet_init(char *interface_name); + bool ethernet_valid( + void); + void ethernet_cleanup( + void); + bool ethernet_init( + char *interface_name); /* function to send a packet out the 802.2 socket */ /* returns number of bytes sent on success, negative on failure */ - int ethernet_send_pdu(BACNET_ADDRESS * dest, /* destination address */ + int ethernet_send_pdu( + BACNET_ADDRESS * dest, /* destination address */ BACNET_NPDU_DATA * npdu_data, /* network information */ - uint8_t * pdu, /* any data to be sent - may be null */ + uint8_t * pdu, /* any data to be sent - may be null */ unsigned pdu_len); /* number of bytes of data */ /* receives an 802.2 framed packet */ /* returns the number of octets in the PDU, or zero on failure */ - uint16_t ethernet_receive(BACNET_ADDRESS * src, /* source address */ - uint8_t * pdu, /* PDU data */ + uint16_t ethernet_receive( + BACNET_ADDRESS * src, /* source address */ + uint8_t * pdu, /* PDU data */ uint16_t max_pdu, /* amount of space available in the PDU */ unsigned timeout); /* milliseconds to wait for a packet */ - void ethernet_set_my_address(BACNET_ADDRESS * my_address); - void ethernet_get_my_address(BACNET_ADDRESS * my_address); - void ethernet_get_broadcast_address(BACNET_ADDRESS * dest); /* destination address */ + void ethernet_set_my_address( + BACNET_ADDRESS * my_address); + void ethernet_get_my_address( + BACNET_ADDRESS * my_address); + void ethernet_get_broadcast_address( + BACNET_ADDRESS * dest); /* destination address */ #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/filename.h b/bacnet-stack/include/filename.h index 38ad0cd6..c6382255 100644 --- a/bacnet-stack/include/filename.h +++ b/bacnet-stack/include/filename.h @@ -36,11 +36,12 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - char *filename_remove_path(const char *filename_in); + char *filename_remove_path( + const char *filename_in); #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/handlers.h b/bacnet-stack/include/handlers.h index bdfac08e..0938d76c 100644 --- a/bacnet-stack/include/handlers.h +++ b/bacnet-stack/include/handlers.h @@ -34,72 +34,96 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - void handler_unrecognized_service(uint8_t * service_request, + void handler_unrecognized_service( + uint8_t * service_request, uint16_t service_len, BACNET_ADDRESS * dest, BACNET_CONFIRMED_SERVICE_DATA * service_data); - void handler_who_is(uint8_t * service_request, - uint16_t service_len, BACNET_ADDRESS * src); + void handler_who_is( + uint8_t * service_request, + uint16_t service_len, + BACNET_ADDRESS * src); - void handler_who_has(uint8_t * service_request, - uint16_t service_len, BACNET_ADDRESS * src); + void handler_who_has( + uint8_t * service_request, + uint16_t service_len, + BACNET_ADDRESS * src); - void handler_i_am_add(uint8_t * service_request, - uint16_t service_len, BACNET_ADDRESS * src); + void handler_i_am_add( + uint8_t * service_request, + uint16_t service_len, + BACNET_ADDRESS * src); - void handler_i_am_bind(uint8_t * service_request, - uint16_t service_len, BACNET_ADDRESS * src); + void handler_i_am_bind( + uint8_t * service_request, + uint16_t service_len, + BACNET_ADDRESS * src); - void handler_read_property(uint8_t * service_request, + void handler_read_property( + uint8_t * service_request, uint16_t service_len, BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data); - void handler_read_property_ack(uint8_t * service_request, + void handler_read_property_ack( + uint8_t * service_request, uint16_t service_len, BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_ACK_DATA * service_data); - void handler_write_property(uint8_t * service_request, + void handler_write_property( + uint8_t * service_request, uint16_t service_len, BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data); - void handler_atomic_read_file(uint8_t * service_request, + void handler_atomic_read_file( + uint8_t * service_request, uint16_t service_len, BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data); - void handler_atomic_read_file_ack(uint8_t * service_request, + void handler_atomic_read_file_ack( + uint8_t * service_request, uint16_t service_len, BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_ACK_DATA * service_data); - void handler_atomic_write_file(uint8_t * service_request, - uint16_t service_len, - BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data); - - void handler_reinitialize_device(uint8_t * service_request, + void handler_atomic_write_file( + uint8_t * service_request, uint16_t service_len, BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data); - void handler_device_communication_control(uint8_t * service_request, + void handler_reinitialize_device( + uint8_t * service_request, uint16_t service_len, BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data); - void handler_i_have(uint8_t * service_request, - uint16_t service_len, BACNET_ADDRESS * src); + void handler_device_communication_control( + uint8_t * service_request, + uint16_t service_len, + BACNET_ADDRESS * src, + BACNET_CONFIRMED_SERVICE_DATA * service_data); - void handler_timesync(uint8_t * service_request, - uint16_t service_len, BACNET_ADDRESS * src); + void handler_i_have( + uint8_t * service_request, + uint16_t service_len, + BACNET_ADDRESS * src); - void handler_timesync_utc(uint8_t * service_request, - uint16_t service_len, BACNET_ADDRESS * src); + void handler_timesync( + uint8_t * service_request, + uint16_t service_len, + BACNET_ADDRESS * src); + + void handler_timesync_utc( + uint8_t * service_request, + uint16_t service_len, + BACNET_ADDRESS * src); void handler_read_property_multiple( uint8_t * service_request, @@ -108,7 +132,7 @@ extern "C" { BACNET_CONFIRMED_SERVICE_DATA * service_data); /* Encodes the property APDU and returns the length, - or sets the error, and returns -1 */ + or sets the error, and returns -1 */ /* resides in h_rp.c */ int Encode_Property_APDU( uint8_t * apdu, @@ -123,5 +147,5 @@ extern "C" { #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/iam.h b/bacnet-stack/include/iam.h index fb223182..ba1f9c03 100644 --- a/bacnet-stack/include/iam.h +++ b/bacnet-stack/include/iam.h @@ -42,33 +42,44 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - int iam_encode_apdu(uint8_t * apdu, + int iam_encode_apdu( + uint8_t * apdu, uint32_t device_id, - unsigned max_apdu, int segmentation, uint16_t vendor_id); + unsigned max_apdu, + int segmentation, + uint16_t vendor_id); - int iam_decode_service_request(uint8_t * apdu, + int iam_decode_service_request( + uint8_t * apdu, uint32_t * pDevice_id, - unsigned *pMax_apdu, int *pSegmentation, uint16_t * pVendor_id); + unsigned *pMax_apdu, + int *pSegmentation, + uint16_t * pVendor_id); int iam_encode_pdu( - uint8_t * buffer, - BACNET_ADDRESS *dest, - BACNET_NPDU_DATA *npdu_data); - - int iam_send(uint8_t * buffer); + uint8_t * buffer, + BACNET_ADDRESS * dest, + BACNET_NPDU_DATA * npdu_data); + + int iam_send( + uint8_t * buffer); #ifdef TEST #include "ctest.h" - int iam_decode_apdu(uint8_t * apdu, + int iam_decode_apdu( + uint8_t * apdu, uint32_t * pDevice_id, - unsigned *pMax_apdu, int *pSegmentation, uint16_t * pVendor_id); + unsigned *pMax_apdu, + int *pSegmentation, + uint16_t * pVendor_id); - void testIAm(Test * pTest); + void testIAm( + Test * pTest); #endif #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/ihave.h b/bacnet-stack/include/ihave.h index 827815d4..67c68b6b 100644 --- a/bacnet-stack/include/ihave.h +++ b/bacnet-stack/include/ihave.h @@ -46,22 +46,29 @@ typedef struct BACnet_I_Have_Data { #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - int ihave_encode_apdu(uint8_t * apdu, BACNET_I_HAVE_DATA * data); + int ihave_encode_apdu( + uint8_t * apdu, + BACNET_I_HAVE_DATA * data); - int ihave_decode_service_request(uint8_t * apdu, - unsigned apdu_len, BACNET_I_HAVE_DATA * data); + int ihave_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + BACNET_I_HAVE_DATA * data); - int ihave_decode_apdu(uint8_t * apdu, - unsigned apdu_len, BACNET_I_HAVE_DATA * data); + int ihave_decode_apdu( + uint8_t * apdu, + unsigned apdu_len, + BACNET_I_HAVE_DATA * data); #ifdef TEST #include "ctest.h" - void testIHave(Test * pTest); + void testIHave( + Test * pTest); #endif #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/indtext.h b/bacnet-stack/include/indtext.h index 01f61f70..345fdec3 100644 --- a/bacnet-stack/include/indtext.h +++ b/bacnet-stack/include/indtext.h @@ -40,56 +40,72 @@ /* index and text pairs */ typedef struct { - unsigned index; /* index number that matches the text */ + unsigned index; /* index number that matches the text */ const char *pString; /* text pair - use NULL to end the list */ } INDTEXT_DATA; #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ /* Searches for a matching string and returns the index to the string in the parameter found_index. If the string is not found, false is returned If the string is found, true is returned and the found_index contains the first index where the string was found. */ - bool indtext_by_string(INDTEXT_DATA * data_list, - const char *search_name, unsigned *found_index); + bool indtext_by_string( + INDTEXT_DATA * data_list, + const char *search_name, + unsigned *found_index); /* case insensitive version */ - bool indtext_by_istring(INDTEXT_DATA * data_list, - const char *search_name, unsigned *found_index); + bool indtext_by_istring( + INDTEXT_DATA * data_list, + const char *search_name, + unsigned *found_index); /* Searches for a matching string and returns the index to the string or the default_index if the string is not found. */ - unsigned indtext_by_string_default(INDTEXT_DATA * data_list, - const char *search_name, unsigned default_index); + unsigned indtext_by_string_default( + INDTEXT_DATA * data_list, + const char *search_name, + unsigned default_index); /* case insensitive version */ - unsigned indtext_by_istring_default(INDTEXT_DATA * data_list, - const char *search_name, unsigned default_index); + unsigned indtext_by_istring_default( + INDTEXT_DATA * data_list, + const char *search_name, + unsigned default_index); /* for a given index, return the matching string, or NULL if not found */ - const char *indtext_by_index(INDTEXT_DATA * data_list, unsigned index); + const char *indtext_by_index( + INDTEXT_DATA * data_list, + unsigned index); /* for a given index, return the matching string, or default_name if not found */ - const char *indtext_by_index_default(INDTEXT_DATA * data_list, - unsigned index, const char *default_name); + const char *indtext_by_index_default( + INDTEXT_DATA * data_list, + unsigned index, + const char *default_name); /* for a given index, return the matching string, or default_name if not found. if the index is before the split, the before_split_default_name is used */ - const char *indtext_by_index_split_default(INDTEXT_DATA * data_list, + const char *indtext_by_index_split_default( + INDTEXT_DATA * data_list, int index, int split_index, - const char *before_split_default_name, const char *default_name); + const char *before_split_default_name, + const char *default_name); /* returns the number of elements in the list */ - unsigned indtext_count(INDTEXT_DATA * data_list); + unsigned indtext_count( + INDTEXT_DATA * data_list); #ifdef TEST #include "ctest.h" - void testIndexText(Test * pTest); + void testIndexText( + Test * pTest); #endif #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/keylist.h b/bacnet-stack/include/keylist.h index 206e8d6c..951a3ddb 100644 --- a/bacnet-stack/include/keylist.h +++ b/bacnet-stack/include/keylist.h @@ -42,52 +42,71 @@ /* list data and datatype */ struct Keylist_Node { - KEY key; /* unique number that is sorted in the list */ - void *data; /* pointer to some data that is stored */ + KEY key; /* unique number that is sorted in the list */ + void *data; /* pointer to some data that is stored */ }; typedef struct Keylist { struct Keylist_Node **array; /* array of nodes */ - int count; /* number of nodes in this list - more effecient than loop */ - int size; /* number of available nodes on this list - can grow or shrink */ + int count; /* number of nodes in this list - more effecient than loop */ + int size; /* number of available nodes on this list - can grow or shrink */ } KEYLIST_TYPE; typedef KEYLIST_TYPE *OS_Keylist; /* returns head of the list or NULL on failure. */ -OS_Keylist Keylist_Create(void); +OS_Keylist Keylist_Create( + void); /* delete specified list */ /* note: you should pop all the nodes off the list first. */ -void Keylist_Delete(OS_Keylist list); +void Keylist_Delete( + OS_Keylist list); /* inserts a node into its sorted position */ /* returns the index where it was added */ -int Keylist_Data_Add(OS_Keylist list, KEY key, void *data); +int Keylist_Data_Add( + OS_Keylist list, + KEY key, + void *data); /* deletes a node specified by its key */ /* returns the data from the node */ -void *Keylist_Data_Delete(OS_Keylist list, KEY key); +void *Keylist_Data_Delete( + OS_Keylist list, + KEY key); /* deletes a node specified by its index */ /* returns the data from the node */ -void *Keylist_Data_Delete_By_Index(OS_Keylist list, int index); +void *Keylist_Data_Delete_By_Index( + OS_Keylist list, + int index); /* returns the data from last node, and removes it from the list */ -void *Keylist_Data_Pop(OS_Keylist list); +void *Keylist_Data_Pop( + OS_Keylist list); /* returns the data from the node specified by key */ -void *Keylist_Data(OS_Keylist list, KEY key); +void *Keylist_Data( + OS_Keylist list, + KEY key); /* returns the data specified by key */ -void *Keylist_Data_Index(OS_Keylist list, int index); +void *Keylist_Data_Index( + OS_Keylist list, + int index); /* return the key at the given index */ -KEY Keylist_Key(OS_Keylist list, int index); +KEY Keylist_Key( + OS_Keylist list, + int index); /* returns the next empty key from the list */ -KEY Keylist_Next_Empty_Key(OS_Keylist list, KEY key); +KEY Keylist_Next_Empty_Key( + OS_Keylist list, + KEY key); /* returns the number of items in the list */ -int Keylist_Count(OS_Keylist list); +int Keylist_Count( + OS_Keylist list); #endif diff --git a/bacnet-stack/include/lc.h b/bacnet-stack/include/lc.h index b8059919..84352023 100644 --- a/bacnet-stack/include/lc.h +++ b/bacnet-stack/include/lc.h @@ -33,34 +33,44 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ void Load_Control_Property_Lists( const int **pRequired, const int **pOptional, const int **pProprietary); - void Load_Control_State_Machine_Handler(void); + void Load_Control_State_Machine_Handler( + void); - bool Load_Control_Valid_Instance(uint32_t object_instance); - unsigned Load_Control_Count(void); - uint32_t Load_Control_Index_To_Instance(unsigned index); - char *Load_Control_Name(uint32_t object_instance); + bool Load_Control_Valid_Instance( + uint32_t object_instance); + unsigned Load_Control_Count( + void); + uint32_t Load_Control_Index_To_Instance( + unsigned index); + char *Load_Control_Name( + uint32_t object_instance); - int Load_Control_Encode_Property_APDU(uint8_t * apdu, + int Load_Control_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code); + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code); - bool Load_Control_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code); + bool Load_Control_Write_Property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code); #ifdef TEST #include "ctest.h" - void testLoadControl(Test * pTest); + void testLoadControl( + Test * pTest); #endif #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/lo.h b/bacnet-stack/include/lo.h index 81fe3a6b..bcfba597 100644 --- a/bacnet-stack/include/lo.h +++ b/bacnet-stack/include/lo.h @@ -33,37 +33,50 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - bool Analog_Output_Valid_Instance(uint32_t object_instance); - unsigned Lighting_Output_Count(void); - uint32_t Lighting_Output_Index_To_Instance(unsigned index); - char *Lighting_Output_Name(uint32_t object_instance); - float Lighting_Output_Present_Value(uint32_t object_instance); - unsigned Lighting_Output_Present_Value_Priority(uint32_t - object_instance); - bool Lighting_Output_Present_Value_Set(uint32_t object_instance, - float value, unsigned priority); - bool Lighting_Output_Present_Value_Relinquish(uint32_t object_instance, + bool Analog_Output_Valid_Instance( + uint32_t object_instance); + unsigned Lighting_Output_Count( + void); + uint32_t Lighting_Output_Index_To_Instance( + unsigned index); + char *Lighting_Output_Name( + uint32_t object_instance); + float Lighting_Output_Present_Value( + uint32_t object_instance); + unsigned Lighting_Output_Present_Value_Priority( + uint32_t object_instance); + bool Lighting_Output_Present_Value_Set( + uint32_t object_instance, + float value, + unsigned priority); + bool Lighting_Output_Present_Value_Relinquish( + uint32_t object_instance, int priority); - /* ReadProperty service support */ - int Lighting_Output_Encode_Property_APDU(uint8_t * apdu, + /* ReadProperty service support */ + int Lighting_Output_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code); - /* WriteProperty service support */ - bool Lighting_Output_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); + /* WriteProperty service support */ + bool Lighting_Output_Write_Property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code); #ifdef TEST #include "ctest.h" - void testLightingOutput(Test * pTest); + void testLightingOutput( + Test * pTest); #endif #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/lsp.h b/bacnet-stack/include/lsp.h index 42135f62..78fbc86b 100644 --- a/bacnet-stack/include/lsp.h +++ b/bacnet-stack/include/lsp.h @@ -33,33 +33,41 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ void Life_Safety_Point_Property_Lists( const int **pRequired, const int **pOptional, const int **pProprietary); - bool Life_Safety_Point_Valid_Instance(uint32_t object_instance); - unsigned Life_Safety_Point_Count(void); - uint32_t Life_Safety_Point_Index_To_Instance(unsigned index); - char *Life_Safety_Point_Name(uint32_t object_instance); + bool Life_Safety_Point_Valid_Instance( + uint32_t object_instance); + unsigned Life_Safety_Point_Count( + void); + uint32_t Life_Safety_Point_Index_To_Instance( + unsigned index); + char *Life_Safety_Point_Name( + uint32_t object_instance); - int Life_Safety_Point_Encode_Property_APDU(uint8_t * apdu, + int Life_Safety_Point_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code); + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code); - bool Life_Safety_Point_Write_Property(BACNET_WRITE_PROPERTY_DATA * - wp_data, BACNET_ERROR_CLASS * error_class, + bool Life_Safety_Point_Write_Property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code); #ifdef TEST #include "ctest.h" - void testLifeSafetyPoint(Test * pTest); + void testLifeSafetyPoint( + Test * pTest); #endif #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/mso.h b/bacnet-stack/include/mso.h index 52043c1f..e34570b3 100644 --- a/bacnet-stack/include/mso.h +++ b/bacnet-stack/include/mso.h @@ -33,33 +33,41 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ void Multistate_Output_Property_Lists( const int **pRequired, const int **pOptional, const int **pProprietary); - bool Multistate_Output_Valid_Instance(uint32_t object_instance); - unsigned Multistate_Output_Count(void); - uint32_t Multistate_Output_Index_To_Instance(unsigned index); - char *Multistate_Output_Name(uint32_t object_instance); + bool Multistate_Output_Valid_Instance( + uint32_t object_instance); + unsigned Multistate_Output_Count( + void); + uint32_t Multistate_Output_Index_To_Instance( + unsigned index); + char *Multistate_Output_Name( + uint32_t object_instance); - int Multistate_Output_Encode_Property_APDU(uint8_t * apdu, + int Multistate_Output_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code); + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code); - bool Multistate_Output_Write_Property(BACNET_WRITE_PROPERTY_DATA * - wp_data, BACNET_ERROR_CLASS * error_class, + bool Multistate_Output_Write_Property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code); #ifdef TEST #include "ctest.h" - void testMultistateOutput(Test * pTest); + void testMultistateOutput( + Test * pTest); #endif #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/mstp.h b/bacnet-stack/include/mstp.h index cafe9fd2..08dc756c 100644 --- a/bacnet-stack/include/mstp.h +++ b/bacnet-stack/include/mstp.h @@ -194,14 +194,18 @@ struct mstp_port_struct_t { /* denote intervals between N-1 and N */ /* Note: done here as functions - put into timer task or ISR so that you can be atomic on 8 bit microcontrollers */ - uint16_t (*SilenceTimer)(void); - void (*SilenceTimerReset)(void); + uint16_t( + *SilenceTimer) ( + void); + void ( + *SilenceTimerReset) ( + void); /* A timer used to measure and generate Reply Postponed frames. It is */ /* incremented by a timer process and is cleared by the Master Node State */ /* Machine when a Data Expecting Reply Answer activity is completed. */ /* note: we always send a reply postponed since a message other than - the reply may be in the transmit queue */ + the reply may be in the transmit queue */ /* uint16_t ReplyPostponedTimer; */ /* Used to store the Source Address of a received frame. */ @@ -256,32 +260,36 @@ struct mstp_port_struct_t { #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - void MSTP_Init(volatile struct mstp_port_struct_t *mstp_port); - void MSTP_Receive_Frame_FSM(volatile struct mstp_port_struct_t + void MSTP_Init( + volatile struct mstp_port_struct_t *mstp_port); + void MSTP_Receive_Frame_FSM( + volatile struct mstp_port_struct_t *mstp_port); - bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t + bool MSTP_Master_Node_FSM( + volatile struct mstp_port_struct_t *mstp_port); /* returns true if line is active */ - bool MSTP_Line_Active(volatile struct mstp_port_struct_t *mstp_port); + bool MSTP_Line_Active( + volatile struct mstp_port_struct_t *mstp_port); uint16_t MSTP_Create_Frame( - uint8_t * buffer, /* where frame is loaded */ + uint8_t * buffer, /* where frame is loaded */ uint16_t buffer_len, /* amount of space available */ uint8_t frame_type, /* type of frame to send - see defines */ uint8_t destination, /* destination address */ - uint8_t source, /* source address */ - uint8_t * data, /* any data to be sent - may be null */ + uint8_t source, /* source address */ + uint8_t * data, /* any data to be sent - may be null */ uint16_t data_len); /* number of bytes of data (up to 501) */ void MSTP_Create_And_Send_Frame( volatile struct mstp_port_struct_t *mstp_port, /* port to send from */ - uint8_t frame_type, /* type of frame to send - see defines */ - uint8_t destination, /* destination address */ - uint8_t source, /* source address */ - uint8_t * data, /* any data to be sent - may be null */ + uint8_t frame_type, /* type of frame to send - see defines */ + uint8_t destination, /* destination address */ + uint8_t source, /* source address */ + uint8_t * data, /* any data to be sent - may be null */ uint16_t data_len); /* functions used by the MS/TP state machine to put or get data */ @@ -293,15 +301,15 @@ extern "C" { /* Return: amount of PDU data */ uint16_t MSTP_Get_Send( volatile struct mstp_port_struct_t *mstp_port, - unsigned timeout); /* milliseconds to wait for a packet */ + unsigned timeout); /* milliseconds to wait for a packet */ /* for the MS/TP state machine to use for getting the reply for Data-Expecting-Reply Frame */ /* Return: amount of PDU data */ uint16_t MSTP_Get_Reply( volatile struct mstp_port_struct_t *mstp_port, - unsigned timeout); /* milliseconds to wait for a packet */ + unsigned timeout); /* milliseconds to wait for a packet */ #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/mstptext.h b/bacnet-stack/include/mstptext.h index 4564cc6c..62a8de41 100644 --- a/bacnet-stack/include/mstptext.h +++ b/bacnet-stack/include/mstptext.h @@ -37,14 +37,16 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ -const char *mstptext_receive_state(int index); -const char *mstptext_master_state(int index); -const char *mstptext_frame_type(int index); + const char *mstptext_receive_state( + int index); + const char *mstptext_master_state( + int index); + const char *mstptext_frame_type( + int index); #ifdef __cplusplus } -#endif /* __cplusplus */ - +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/npdu.h b/bacnet-stack/include/npdu.h index 2ae54606..3da5ec42 100644 --- a/bacnet-stack/include/npdu.h +++ b/bacnet-stack/include/npdu.h @@ -48,34 +48,45 @@ typedef struct bacnet_npdu_data_t { BACNET_MESSAGE_PRIORITY priority; /* optional network message info */ BACNET_NETWORK_MESSAGE_TYPE network_message_type; /* optional */ - uint16_t vendor_id; /* optional, if net message type is > 0x80 */ + uint16_t vendor_id; /* optional, if net message type is > 0x80 */ uint8_t hop_count; } BACNET_NPDU_DATA; #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - uint8_t npdu_encode_max_seg_max_apdu(int max_segs, int max_apdu); + uint8_t npdu_encode_max_seg_max_apdu( + int max_segs, + int max_apdu); - int npdu_encode_pdu(uint8_t * npdu, + int npdu_encode_pdu( + uint8_t * npdu, BACNET_ADDRESS * dest, - BACNET_ADDRESS * src, BACNET_NPDU_DATA * npdu_data); + BACNET_ADDRESS * src, + BACNET_NPDU_DATA * npdu_data); - void npdu_encode_npdu_data(BACNET_NPDU_DATA * npdu, - bool data_expecting_reply, BACNET_MESSAGE_PRIORITY priority); + void npdu_encode_npdu_data( + BACNET_NPDU_DATA * npdu, + bool data_expecting_reply, + BACNET_MESSAGE_PRIORITY priority); - void npdu_copy_data(BACNET_NPDU_DATA * dest, BACNET_NPDU_DATA * src); + void npdu_copy_data( + BACNET_NPDU_DATA * dest, + BACNET_NPDU_DATA * src); - int npdu_decode(uint8_t * npdu, + int npdu_decode( + uint8_t * npdu, BACNET_ADDRESS * dest, - BACNET_ADDRESS * src, BACNET_NPDU_DATA * npdu_data); + BACNET_ADDRESS * src, + BACNET_NPDU_DATA * npdu_data); - void npdu_handler(BACNET_ADDRESS * src, /* source address */ - uint8_t * pdu, /* PDU data */ + void npdu_handler( + BACNET_ADDRESS * src, /* source address */ + uint8_t * pdu, /* PDU data */ uint16_t pdu_len); /* length PDU */ #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/rd.h b/bacnet-stack/include/rd.h index ad16a64c..9aaecc57 100644 --- a/bacnet-stack/include/rd.h +++ b/bacnet-stack/include/rd.h @@ -39,32 +39,36 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ /* encode service */ - int rd_encode_apdu(uint8_t * apdu, + int rd_encode_apdu( + uint8_t * apdu, uint8_t invoke_id, BACNET_REINITIALIZED_STATE state, BACNET_CHARACTER_STRING * password); /* decode the service request only */ - int rd_decode_service_request(uint8_t * apdu, + int rd_decode_service_request( + uint8_t * apdu, unsigned apdu_len, BACNET_REINITIALIZED_STATE * state, BACNET_CHARACTER_STRING * password); #ifdef TEST #include "ctest.h" - int rd_decode_apdu(uint8_t * apdu, + int rd_decode_apdu( + uint8_t * apdu, unsigned apdu_len, uint8_t * invoke_id, BACNET_REINITIALIZED_STATE * state, BACNET_CHARACTER_STRING * password); - void test_ReinitializeDevice(Test * pTest); + void test_ReinitializeDevice( + Test * pTest); #endif #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/reject.h b/bacnet-stack/include/reject.h index 83d11d1d..55b2ea03 100644 --- a/bacnet-stack/include/reject.h +++ b/bacnet-stack/include/reject.h @@ -39,22 +39,31 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - int reject_encode_apdu(uint8_t * apdu, - uint8_t invoke_id, uint8_t reject_reason); + int reject_encode_apdu( + uint8_t * apdu, + uint8_t invoke_id, + uint8_t reject_reason); - int reject_decode_service_request(uint8_t * apdu, - unsigned apdu_len, uint8_t * invoke_id, uint8_t * reject_reason); + int reject_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + uint8_t * invoke_id, + uint8_t * reject_reason); #ifdef TEST - int reject_decode_apdu(uint8_t * apdu, - unsigned apdu_len, uint8_t * invoke_id, uint8_t * reject_reason); + int reject_decode_apdu( + uint8_t * apdu, + unsigned apdu_len, + uint8_t * invoke_id, + uint8_t * reject_reason); - void testReject(Test * pTest); + void testReject( + Test * pTest); #endif #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/ringbuf.h b/bacnet-stack/include/ringbuf.h index 6a6e4f72..02b0fe92 100644 --- a/bacnet-stack/include/ringbuf.h +++ b/bacnet-stack/include/ringbuf.h @@ -43,29 +43,34 @@ #include struct ring_buffer_t { - char *data; /* block of memory or array of data */ + char *data; /* block of memory or array of data */ unsigned element_size; /* how many bytes for each chunk */ unsigned element_count; /* number of chunks of data */ - unsigned head; /* first chunk of data */ - unsigned count; /* number of chunks in use */ + unsigned head; /* first chunk of data */ + unsigned count; /* number of chunks in use */ }; typedef struct ring_buffer_t RING_BUFFER; #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - bool Ringbuf_Empty(RING_BUFFER const *b); - char *Ringbuf_Get_Front(RING_BUFFER const *b); - char *Ringbuf_Pop_Front(RING_BUFFER * b); - bool Ringbuf_Put(RING_BUFFER * b, /* ring buffer structure */ + bool Ringbuf_Empty( + RING_BUFFER const *b); + char *Ringbuf_Get_Front( + RING_BUFFER const *b); + char *Ringbuf_Pop_Front( + RING_BUFFER * b); + bool Ringbuf_Put( + RING_BUFFER * b, /* ring buffer structure */ char *data_element); /* one element to add to the ring */ - void Ringbuf_Init(RING_BUFFER * b, /* ring buffer structure */ - char *data, /* data block or array of data */ + void Ringbuf_Init( + RING_BUFFER * b, /* ring buffer structure */ + char *data, /* data block or array of data */ unsigned element_size, /* size of one element in the data block */ unsigned element_count); /* number of elements in the data block */ #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/rp.h b/bacnet-stack/include/rp.h index ce8f2b45..ea9f006b 100644 --- a/bacnet-stack/include/rp.h +++ b/bacnet-stack/include/rp.h @@ -48,37 +48,52 @@ typedef struct BACnet_Read_Property_Data { #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ /* encode service */ - int rp_encode_apdu(uint8_t * apdu, - uint8_t invoke_id, BACNET_READ_PROPERTY_DATA * rpdata); + int rp_encode_apdu( + uint8_t * apdu, + uint8_t invoke_id, + BACNET_READ_PROPERTY_DATA * rpdata); /* decode the service request only */ - int rp_decode_service_request(uint8_t * apdu, - unsigned apdu_len, BACNET_READ_PROPERTY_DATA * rpdata); + int rp_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + BACNET_READ_PROPERTY_DATA * rpdata); - int rp_ack_encode_apdu(uint8_t * apdu, - uint8_t invoke_id, BACNET_READ_PROPERTY_DATA * rpdata); + int rp_ack_encode_apdu( + uint8_t * apdu, + uint8_t invoke_id, + BACNET_READ_PROPERTY_DATA * rpdata); - int rp_ack_decode_service_request(uint8_t * apdu, int apdu_len, /* total length of the apdu */ + int rp_ack_decode_service_request( + uint8_t * apdu, + int apdu_len, /* total length of the apdu */ BACNET_READ_PROPERTY_DATA * rpdata); #ifdef TEST #include "ctest.h" - int rp_decode_apdu(uint8_t * apdu, + int rp_decode_apdu( + uint8_t * apdu, unsigned apdu_len, - uint8_t * invoke_id, BACNET_READ_PROPERTY_DATA * rpdata); + uint8_t * invoke_id, + BACNET_READ_PROPERTY_DATA * rpdata); - int rp_ack_decode_apdu(uint8_t * apdu, int apdu_len, /* total length of the apdu */ - uint8_t * invoke_id, BACNET_READ_PROPERTY_DATA * rpdata); + int rp_ack_decode_apdu( + uint8_t * apdu, + int apdu_len, /* total length of the apdu */ + uint8_t * invoke_id, + BACNET_READ_PROPERTY_DATA * rpdata); - void test_ReadProperty(Test * pTest); - void test_ReadPropertyAck(Test * pTest); + void test_ReadProperty( + Test * pTest); + void test_ReadPropertyAck( + Test * pTest); #endif #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/rpm.h b/bacnet-stack/include/rpm.h index 2f5391b8..e907db21 100644 --- a/bacnet-stack/include/rpm.h +++ b/bacnet-stack/include/rpm.h @@ -42,7 +42,7 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ /* encode functions */ /* Start with the Init function, and then add an object, @@ -51,71 +51,108 @@ extern "C" { until the APDU is full.*/ /* RPM */ - int rpm_encode_apdu_init(uint8_t * apdu, uint8_t invoke_id); + int rpm_encode_apdu_init( + uint8_t * apdu, + uint8_t invoke_id); - int rpm_encode_apdu_object_begin(uint8_t * apdu, - BACNET_OBJECT_TYPE object_type, uint32_t object_instance); + int rpm_encode_apdu_object_begin( + uint8_t * apdu, + BACNET_OBJECT_TYPE object_type, + uint32_t object_instance); - int rpm_encode_apdu_object_property(uint8_t * apdu, - BACNET_PROPERTY_ID object_property, int32_t array_index); + int rpm_encode_apdu_object_property( + uint8_t * apdu, + BACNET_PROPERTY_ID object_property, + int32_t array_index); - int rpm_encode_apdu_object_end(uint8_t * apdu); + int rpm_encode_apdu_object_end( + uint8_t * apdu); /* decode the object portion of the service request only */ - int rpm_decode_object_id(uint8_t * apdu, + int rpm_decode_object_id( + uint8_t * apdu, unsigned apdu_len, - BACNET_OBJECT_TYPE * object_type, uint32_t * object_instance); + BACNET_OBJECT_TYPE * object_type, + uint32_t * object_instance); /* is this the end of this object property list? */ - int rpm_decode_object_end(uint8_t * apdu, unsigned apdu_len); + int rpm_decode_object_end( + uint8_t * apdu, + unsigned apdu_len); /* decode the object property portion of the service request only */ - int rpm_decode_object_property(uint8_t * apdu, + int rpm_decode_object_property( + uint8_t * apdu, unsigned apdu_len, - BACNET_PROPERTY_ID * object_property, int32_t * array_index); + BACNET_PROPERTY_ID * object_property, + int32_t * array_index); /* RPM Ack - reply from server */ - int rpm_ack_encode_apdu_init(uint8_t * apdu, uint8_t invoke_id); + int rpm_ack_encode_apdu_init( + uint8_t * apdu, + uint8_t invoke_id); - int rpm_ack_encode_apdu_object_begin(uint8_t * apdu, - BACNET_OBJECT_TYPE object_type, uint32_t object_instance); + int rpm_ack_encode_apdu_object_begin( + uint8_t * apdu, + BACNET_OBJECT_TYPE object_type, + uint32_t object_instance); - int rpm_ack_encode_apdu_object_property(uint8_t * apdu, - BACNET_PROPERTY_ID object_property, int32_t array_index); + int rpm_ack_encode_apdu_object_property( + uint8_t * apdu, + BACNET_PROPERTY_ID object_property, + int32_t array_index); - int rpm_ack_encode_apdu_object_property_value(uint8_t * apdu, - uint8_t * application_data, unsigned application_data_len); + int rpm_ack_encode_apdu_object_property_value( + uint8_t * apdu, + uint8_t * application_data, + unsigned application_data_len); - int rpm_ack_encode_apdu_object_property_error(uint8_t * apdu, - BACNET_ERROR_CLASS error_class, BACNET_ERROR_CODE error_code); + int rpm_ack_encode_apdu_object_property_error( + uint8_t * apdu, + BACNET_ERROR_CLASS error_class, + BACNET_ERROR_CODE error_code); - int rpm_ack_encode_apdu_object_end(uint8_t * apdu); + int rpm_ack_encode_apdu_object_end( + uint8_t * apdu); - int rpm_ack_decode_object_id(uint8_t * apdu, + int rpm_ack_decode_object_id( + uint8_t * apdu, unsigned apdu_len, - BACNET_OBJECT_TYPE * object_type, uint32_t * object_instance); + BACNET_OBJECT_TYPE * object_type, + uint32_t * object_instance); /* is this the end of the list of this objects properties values? */ - int rpm_ack_decode_object_end(uint8_t * apdu, unsigned apdu_len); - int rpm_ack_decode_object_property(uint8_t * apdu, + int rpm_ack_decode_object_end( + uint8_t * apdu, + unsigned apdu_len); + int rpm_ack_decode_object_property( + uint8_t * apdu, unsigned apdu_len, - BACNET_PROPERTY_ID * object_property, int32_t * array_index); + BACNET_PROPERTY_ID * object_property, + int32_t * array_index); #ifdef TEST #include "ctest.h" - int rpm_decode_apdu(uint8_t * apdu, + int rpm_decode_apdu( + uint8_t * apdu, unsigned apdu_len, uint8_t * invoke_id, - uint8_t ** service_request, unsigned *service_request_len); + uint8_t ** service_request, + unsigned *service_request_len); - int rpm_ack_decode_apdu(uint8_t * apdu, int apdu_len, /* total length of the apdu */ + int rpm_ack_decode_apdu( + uint8_t * apdu, + int apdu_len, /* total length of the apdu */ uint8_t * invoke_id, - uint8_t ** service_request, unsigned *service_request_len); + uint8_t ** service_request, + unsigned *service_request_len); - void testReadPropertyMultiple(Test * pTest); - void testReadPropertyMultipleAck(Test * pTest); + void testReadPropertyMultiple( + Test * pTest); + void testReadPropertyMultipleAck( + Test * pTest); #endif #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/sbuf.h b/bacnet-stack/include/sbuf.h index ffcad78a..cab19197 100644 --- a/bacnet-stack/include/sbuf.h +++ b/bacnet-stack/include/sbuf.h @@ -43,38 +43,46 @@ #include struct static_buffer_t { - char *data; /* block of memory or array of data */ - unsigned size; /* actual size, in bytes, of the block of data */ - unsigned count; /* number of bytes in use */ + char *data; /* block of memory or array of data */ + unsigned size; /* actual size, in bytes, of the block of data */ + unsigned count; /* number of bytes in use */ }; typedef struct static_buffer_t STATIC_BUFFER; #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - void sbuf_init(STATIC_BUFFER * b, /* static buffer structure */ - char *data, /* actual size, in bytes, of the data block or array of data */ - unsigned size); /* number of bytes used */ + void sbuf_init( + STATIC_BUFFER * b, /* static buffer structure */ + char *data, /* actual size, in bytes, of the data block or array of data */ + unsigned size); /* number of bytes used */ /* returns true if size==0, false if size > 0 */ - bool sbuf_empty(STATIC_BUFFER const *b); - char *sbuf_data(STATIC_BUFFER const *b); - unsigned sbuf_size(STATIC_BUFFER * b); - unsigned sbuf_count(STATIC_BUFFER * b); + bool sbuf_empty( + STATIC_BUFFER const *b); + char *sbuf_data( + STATIC_BUFFER const *b); + unsigned sbuf_size( + STATIC_BUFFER * b); + unsigned sbuf_count( + STATIC_BUFFER * b); /* returns true if successful, false if not enough room to append data */ - bool sbuf_put(STATIC_BUFFER * b, /* static buffer structure */ + bool sbuf_put( + STATIC_BUFFER * b, /* static buffer structure */ unsigned offset, /* where to start */ - char *data, /* number of bytes used */ + char *data, /* number of bytes used */ unsigned data_size); /* how many to add */ /* returns true if successful, false if not enough room to append data */ - bool sbuf_append(STATIC_BUFFER * b, /* static buffer structure */ - char *data, /* number of bytes used */ + bool sbuf_append( + STATIC_BUFFER * b, /* static buffer structure */ + char *data, /* number of bytes used */ unsigned data_size); /* how many to add */ /* returns true if successful, false if not enough room to append data */ - bool sbuf_truncate(STATIC_BUFFER * b, /* static buffer structure */ + bool sbuf_truncate( + STATIC_BUFFER * b, /* static buffer structure */ unsigned count); /* total number of bytes in use */ #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/timesync.h b/bacnet-stack/include/timesync.h index f54a3c26..433bfa7f 100644 --- a/bacnet-stack/include/timesync.h +++ b/bacnet-stack/include/timesync.h @@ -40,27 +40,41 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ /* encode service */ - int timesync_utc_encode_apdu(uint8_t * apdu, - BACNET_DATE * my_date, BACNET_TIME * my_time); - int timesync_encode_apdu(uint8_t * apdu, - BACNET_DATE * my_date, BACNET_TIME * my_time); + int timesync_utc_encode_apdu( + uint8_t * apdu, + BACNET_DATE * my_date, + BACNET_TIME * my_time); + int timesync_encode_apdu( + uint8_t * apdu, + BACNET_DATE * my_date, + BACNET_TIME * my_time); /* decode the service request only */ - int timesync_decode_service_request(uint8_t * apdu, - unsigned apdu_len, BACNET_DATE * my_date, BACNET_TIME * my_time); - int timesync_utc_decode_apdu(uint8_t * apdu, - unsigned apdu_len, BACNET_DATE * my_date, BACNET_TIME * my_time); - int timesync_decode_apdu(uint8_t * apdu, - unsigned apdu_len, BACNET_DATE * my_date, BACNET_TIME * my_time); + int timesync_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + BACNET_DATE * my_date, + BACNET_TIME * my_time); + int timesync_utc_decode_apdu( + uint8_t * apdu, + unsigned apdu_len, + BACNET_DATE * my_date, + BACNET_TIME * my_time); + int timesync_decode_apdu( + uint8_t * apdu, + unsigned apdu_len, + BACNET_DATE * my_date, + BACNET_TIME * my_time); #ifdef TEST #include "ctest.h" - void testTimeSync(Test * pTest); + void testTimeSync( + Test * pTest); #endif #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/tsm.h b/bacnet-stack/include/tsm.h index 595400f9..0be22434 100644 --- a/bacnet-stack/include/tsm.h +++ b/bacnet-stack/include/tsm.h @@ -92,31 +92,43 @@ typedef struct BACnet_TSM_Data { #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - bool tsm_transaction_available(void); - uint8_t tsm_transaction_idle_count(void); - void tsm_timer_milliseconds(uint16_t milliseconds); + bool tsm_transaction_available( + void); + uint8_t tsm_transaction_idle_count( + void); + void tsm_timer_milliseconds( + uint16_t milliseconds); /* free the invoke ID when the reply comes back */ - void tsm_free_invoke_id(uint8_t invokeID); + void tsm_free_invoke_id( + uint8_t invokeID); /* use these in tandem */ - uint8_t tsm_next_free_invokeID(void); + uint8_t tsm_next_free_invokeID( + void); /* returns the same invoke ID that was given */ - void tsm_set_confirmed_unsegmented_transaction(uint8_t invokeID, - BACNET_ADDRESS * dest, BACNET_NPDU_DATA * ndpu_data, - uint8_t * apdu, uint16_t apdu_len); + void tsm_set_confirmed_unsegmented_transaction( + uint8_t invokeID, + BACNET_ADDRESS * dest, + BACNET_NPDU_DATA * ndpu_data, + uint8_t * apdu, + uint16_t apdu_len); /* returns true if transaction is found */ - bool tsm_get_transaction_pdu(uint8_t invokeID, - BACNET_ADDRESS * dest, BACNET_NPDU_DATA * ndpu_data, - uint8_t * apdu, uint16_t * apdu_len); + bool tsm_get_transaction_pdu( + uint8_t invokeID, + BACNET_ADDRESS * dest, + BACNET_NPDU_DATA * ndpu_data, + uint8_t * apdu, + uint16_t * apdu_len); - bool tsm_invoke_id_free(uint8_t invokeID); - bool tsm_invoke_id_failed(uint8_t invokeID); + bool tsm_invoke_id_free( + uint8_t invokeID); + bool tsm_invoke_id_failed( + uint8_t invokeID); #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ /* define out any functions necessary for compile */ #endif - #endif diff --git a/bacnet-stack/include/whohas.h b/bacnet-stack/include/whohas.h index c51e2312..1ea54db6 100644 --- a/bacnet-stack/include/whohas.h +++ b/bacnet-stack/include/whohas.h @@ -39,9 +39,9 @@ #include "bacstr.h" typedef struct BACnet_Who_Has_Data { - int32_t low_limit; /* deviceInstanceRange */ + int32_t low_limit; /* deviceInstanceRange */ int32_t high_limit; - bool object_name; /* true if a string */ + bool object_name; /* true if a string */ union { BACNET_OBJECT_ID identifier; BACNET_CHARACTER_STRING name; @@ -50,23 +50,30 @@ typedef struct BACnet_Who_Has_Data { #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ /* encode service - use -1 for limit if you want unlimited */ - int whohas_encode_apdu(uint8_t * apdu, BACNET_WHO_HAS_DATA * data); + int whohas_encode_apdu( + uint8_t * apdu, + BACNET_WHO_HAS_DATA * data); - int whohas_decode_service_request(uint8_t * apdu, - unsigned apdu_len, BACNET_WHO_HAS_DATA * data); + int whohas_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + BACNET_WHO_HAS_DATA * data); - int whohas_decode_apdu(uint8_t * apdu, - unsigned apdu_len, BACNET_WHO_HAS_DATA * data); + int whohas_decode_apdu( + uint8_t * apdu, + unsigned apdu_len, + BACNET_WHO_HAS_DATA * data); #ifdef TEST #include "ctest.h" - void testWhoHas(Test * pTest); + void testWhoHas( + Test * pTest); #endif #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/whois.h b/bacnet-stack/include/whois.h index aa2cb853..d5f4ca0e 100644 --- a/bacnet-stack/include/whois.h +++ b/bacnet-stack/include/whois.h @@ -39,23 +39,32 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ /* encode service - use -1 for limit if you want unlimited */ - int whois_encode_apdu(uint8_t * apdu, - int32_t low_limit, int32_t high_limit); + int whois_encode_apdu( + uint8_t * apdu, + int32_t low_limit, + int32_t high_limit); - int whois_decode_service_request(uint8_t * apdu, - unsigned apdu_len, int32_t * pLow_limit, int32_t * pHigh_limit); + int whois_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + int32_t * pLow_limit, + int32_t * pHigh_limit); #ifdef TEST - int whois_decode_apdu(uint8_t * apdu, - unsigned apdu_len, int32_t * pLow_limit, int32_t * pHigh_limit); + int whois_decode_apdu( + uint8_t * apdu, + unsigned apdu_len, + int32_t * pLow_limit, + int32_t * pHigh_limit); - void testWhoIs(Test * pTest); + void testWhoIs( + Test * pTest); #endif #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/include/wp.h b/bacnet-stack/include/wp.h index ffecad77..3d19523c 100644 --- a/bacnet-stack/include/wp.h +++ b/bacnet-stack/include/wp.h @@ -50,32 +50,40 @@ typedef struct BACnet_Write_Property_Data { int32_t array_index; /* use BACNET_ARRAY_ALL when not setting */ uint8_t application_data[MAX_APDU]; int application_data_len; - uint8_t priority; /* use BACNET_NO_PRIORITY if no priority */ + uint8_t priority; /* use BACNET_NO_PRIORITY if no priority */ } BACNET_WRITE_PROPERTY_DATA; #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ /* encode service */ - int wp_encode_apdu(uint8_t * apdu, - uint8_t invoke_id, BACNET_WRITE_PROPERTY_DATA * wp_data); + int wp_encode_apdu( + uint8_t * apdu, + uint8_t invoke_id, + BACNET_WRITE_PROPERTY_DATA * wp_data); /* decode the service request only */ - int wp_decode_service_request(uint8_t * apdu, - unsigned apdu_len, BACNET_WRITE_PROPERTY_DATA * wp_data); + int wp_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + BACNET_WRITE_PROPERTY_DATA * wp_data); #ifdef TEST #include "ctest.h" - int wp_decode_apdu(uint8_t * apdu, + int wp_decode_apdu( + uint8_t * apdu, unsigned apdu_len, - uint8_t * invoke_id, BACNET_WRITE_PROPERTY_DATA * wp_data); + uint8_t * invoke_id, + BACNET_WRITE_PROPERTY_DATA * wp_data); - void test_ReadProperty(Test * pTest); - void test_ReadPropertyAck(Test * pTest); + void test_ReadProperty( + Test * pTest); + void test_ReadPropertyAck( + Test * pTest); #endif #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/ports/arm7/_stdint.h b/bacnet-stack/ports/arm7/_stdint.h index 1ace7221..f7f58ffe 100644 --- a/bacnet-stack/ports/arm7/_stdint.h +++ b/bacnet-stack/ports/arm7/_stdint.h @@ -15,4 +15,4 @@ typedef signed long int32_t; /* 4 bytes -2147483647 to 2147483647 */ /* typedef signed long long int64_t; */ /* typedef unsigned long long uint64_t; */ -#endif /* STDINT_H */ +#endif /* STDINT_H */ diff --git a/bacnet-stack/ports/arm7/bip.c b/bacnet-stack/ports/arm7/bip.c index 40519495..d5a0f429 100644 --- a/bacnet-stack/ports/arm7/bip.c +++ b/bacnet-stack/ports/arm7/bip.c @@ -32,12 +32,12 @@ ------------------------------------------- ####COPYRIGHTEND####*/ -#include /* for standard integer types uint8_t etc. */ -#include /* for the standard bool type. */ +#include /* for standard integer types uint8_t etc. */ +#include /* for the standard bool type. */ #include "bacdcode.h" #include "bip.h" #include "eth.h" -#include "net.h" /* custom per port */ +#include "net.h" /* custom per port */ static int BIP_Socket = -1; /* port to use - stored in host byte order */ @@ -47,22 +47,26 @@ static struct in_addr BIP_Address; /* Broadcast Address - stored in host byte order */ static struct in_addr BIP_Broadcast_Address; -void bip_set_socket(int sock_fd) +void bip_set_socket( + int sock_fd) { BIP_Socket = sock_fd; } -int bip_socket(void) +int bip_socket( + void) { return BIP_Socket; } -bool bip_valid(void) +bool bip_valid( + void) { return (BIP_Socket != -1); } -void bip_cleanup(void) +void bip_cleanup( + void) { /* if (bip_valid()) */ /* close(BIP_Socket); */ @@ -72,142 +76,149 @@ void bip_cleanup(void) } /* set using network byte order */ -void bip_set_addr(uint32_t net_address) +void bip_set_addr( + uint32_t net_address) { /* BIP_Address.s_addr = ntohl(net_address); */ BIP_Address.s_addr = net_address; } /* returns host byte order */ -uint32_t bip_get_addr(void) +uint32_t bip_get_addr( + void) { return BIP_Address.s_addr; } /* set using network byte order */ -void bip_set_broadcast_addr(uint32_t net_address) +void bip_set_broadcast_addr( + uint32_t net_address) { /* BIP_Broadcast_Address.s_addr = ntohl(net_address); */ BIP_Broadcast_Address.s_addr = net_address; } /* returns host byte order */ -uint32_t bip_get_broadcast_addr(void) +uint32_t bip_get_broadcast_addr( + void) { return BIP_Broadcast_Address.s_addr; } /* set using host byte order */ -void bip_set_port(uint16_t port) +void bip_set_port( + uint16_t port) { BIP_Port = port; } /* returns host byte order */ -uint16_t bip_get_port(void) +uint16_t bip_get_port( + void) { return BIP_Port; } /* function to send a packet out the BACnet/IP socket (Annex J) */ /* returns number of bytes sent on success, negative number on failure */ -int bip_send_pdu(BACNET_ADDRESS * dest, /* destination address */ +int bip_send_pdu( + BACNET_ADDRESS * dest, /* destination address */ BACNET_NPDU_DATA * npdu_data, /* network information */ - uint8_t * pdu, /* any data to be sent - may be null */ + uint8_t * pdu, /* any data to be sent - may be null */ unsigned pdu_len) -{ /* number of bytes of data */ +{ /* number of bytes of data */ - struct sockaddr_in bip_dest; - uint8_t mtu[4]; - int mtu_len = 0; - int bytes_sent = 0; - UDP_HDR udphdr; - IP_HDR iphdr; - uint8_t mac[6]; + struct sockaddr_in bip_dest; + uint8_t mtu[4]; + int mtu_len = 0; + int bytes_sent = 0; + UDP_HDR udphdr; + IP_HDR iphdr; + uint8_t mac[6]; - (void) npdu_data; - - /* assumes that the driver has already been initialized */ + (void) npdu_data; + + /* assumes that the driver has already been initialized */ /* if (BIP_Socket < 0) */ /* return BIP_Socket; */ - - mtu[0] = BVLL_TYPE_BACNET_IP; - bip_dest.sin_family = AF_INET; - if (dest->mac_len == 6) - { - memcpy(&(bip_dest.sin_addr.s_addr),&dest->mac[0],4); - decode_unsigned16(&dest->mac[4], &(bip_dest.sin_port)); - memset(&(bip_dest.sin_zero), '\0', 8); - mtu[1] = BVLC_ORIGINAL_UNICAST_NPDU; - } - /* broadcast */ - else if (dest->mac_len == 0) - { - bip_dest.sin_addr.s_addr = BIP_Broadcast_Address.s_addr; - bip_dest.sin_port = htons(BIP_Port); - memset(&(bip_dest.sin_zero), '\0', 8); - mtu[1] = BVLC_ORIGINAL_BROADCAST_NPDU; - } - else - return -1; - - mtu_len = 2; - mtu_len += encode_unsigned16(&mtu[mtu_len], (uint16_t) (pdu_len + 4 /*inclusive */ )); - mtu_len += pdu_len; - - /* IP address should be in network byte order */ - ARPIsResolved(bip_dest.sin_addr.s_addr, mac); + mtu[0] = BVLL_TYPE_BACNET_IP; + bip_dest.sin_family = AF_INET; - iphdr.vhl = 0x45; - iphdr.tos = 0; - iphdr.iplen = htons(mtu_len + sizeof(IP_HDR) + sizeof(UDP_HDR)); - ++ipid; - iphdr.ipid = htons(ipid); - iphdr.ipoffset[0] = iphdr.ipoffset[1] = 0; - iphdr.ttl = UIP_TTL; - iphdr.proto = UIP_PROTO_UDP; - iphdr.ipchksum = 0; - iphdr.srcipaddr = BIP_Address.s_addr; - iphdr.destipaddr = bip_dest.sin_addr.s_addr; - - /* Calculate IP checksum. */ - iphdr.ipchksum = ~(ip_getchksum((uint8_t*)&iphdr)); + if (dest->mac_len == 6) { + memcpy(&(bip_dest.sin_addr.s_addr), &dest->mac[0], 4); + decode_unsigned16(&dest->mac[4], &(bip_dest.sin_port)); + memset(&(bip_dest.sin_zero), '\0', 8); + mtu[1] = BVLC_ORIGINAL_UNICAST_NPDU; + } + /* broadcast */ + else if (dest->mac_len == 0) { + bip_dest.sin_addr.s_addr = BIP_Broadcast_Address.s_addr; + bip_dest.sin_port = htons(BIP_Port); + memset(&(bip_dest.sin_zero), '\0', 8); + mtu[1] = BVLC_ORIGINAL_BROADCAST_NPDU; + } else + return -1; - /* Ports are sent in Network byte order. BIP_Port is stored in Host byte order */ - udphdr.srcport = htons(BIP_Port); - udphdr.destport = bip_dest.sin_port; - udphdr.udplen = htons(mtu_len + sizeof(UDP_HDR)); - /* Not using UDP checksums */ - udphdr.udpchksum = 0; + mtu_len = 2; + mtu_len += + encode_unsigned16(&mtu[mtu_len], + (uint16_t) (pdu_len + 4 /*inclusive */ )); + mtu_len += pdu_len; + + /* IP address should be in network byte order */ + ARPIsResolved(bip_dest.sin_addr.s_addr, mac); + + iphdr.vhl = 0x45; + iphdr.tos = 0; + iphdr.iplen = htons(mtu_len + sizeof(IP_HDR) + sizeof(UDP_HDR)); + ++ipid; + iphdr.ipid = htons(ipid); + iphdr.ipoffset[0] = iphdr.ipoffset[1] = 0; + iphdr.ttl = UIP_TTL; + iphdr.proto = UIP_PROTO_UDP; + iphdr.ipchksum = 0; + iphdr.srcipaddr = BIP_Address.s_addr; + iphdr.destipaddr = bip_dest.sin_addr.s_addr; + + /* Calculate IP checksum. */ + iphdr.ipchksum = ~(ip_getchksum((uint8_t *) & iphdr)); + + /* Ports are sent in Network byte order. BIP_Port is stored in Host byte order */ + udphdr.srcport = htons(BIP_Port); + udphdr.destport = bip_dest.sin_port; + udphdr.udplen = htons(mtu_len + sizeof(UDP_HDR)); + /* Not using UDP checksums */ + udphdr.udpchksum = 0; + + EthernetSendHeader(mac, UIP_ETHTYPE_IP); + EthernetSend((uint8_t *) & iphdr, sizeof(IP_HDR)); + EthernetSend((uint8_t *) & udphdr, sizeof(UDP_HDR)); + EthernetSend(mtu, 4); + EthernetSend(pdu, pdu_len); + EthernetFlush(); + uip_stat.ip.sent++; - EthernetSendHeader(mac, UIP_ETHTYPE_IP); - EthernetSend((uint8_t*)&iphdr, sizeof(IP_HDR)); - EthernetSend((uint8_t*)&udphdr, sizeof(UDP_HDR)); - EthernetSend(mtu, 4); - EthernetSend(pdu, pdu_len); - EthernetFlush(); - uip_stat.ip.sent++; - } /* receives a BACnet/IP packet */ /* returns the number of octets in the PDU, or zero on failure */ -uint16_t bip_receive(BACNET_ADDRESS * src, /* source address */ - uint8_t * pdu, /* PDU data */ - uint16_t max_pdu, /* amount of space available in the PDU */ +uint16_t bip_receive( + BACNET_ADDRESS * src, /* source address */ + uint8_t * pdu, /* PDU data */ + uint16_t max_pdu, /* amount of space available in the PDU */ unsigned timeout) -{ /* number of milliseconds to wait for a packet */ - return 0; +{ /* number of milliseconds to wait for a packet */ + return 0; } -void bip_get_my_address(BACNET_ADDRESS * my_address) +void bip_get_my_address( + BACNET_ADDRESS * my_address) { int i = 0; my_address->mac_len = 6; - (void) encode_unsigned32(&my_address->mac[0], - htonl(BIP_Address.s_addr)); + (void) encode_unsigned32(&my_address->mac[0], htonl(BIP_Address.s_addr)); (void) encode_unsigned16(&my_address->mac[4], htons(BIP_Port)); my_address->net = 0; /* local only, no routing */ my_address->len = 0; /* no SLEN */ @@ -219,9 +230,10 @@ void bip_get_my_address(BACNET_ADDRESS * my_address) return; } -void bip_get_broadcast_address(BACNET_ADDRESS * dest) -{ /* destination address */ - int i = 0; /* counter */ +void bip_get_broadcast_address( + BACNET_ADDRESS * dest) +{ /* destination address */ + int i = 0; /* counter */ if (dest) { dest->mac_len = 6; @@ -229,7 +241,7 @@ void bip_get_broadcast_address(BACNET_ADDRESS * dest) htonl(BIP_Broadcast_Address.s_addr)); (void) encode_unsigned16(&dest->mac[4], htons(BIP_Port)); dest->net = BACNET_BROADCAST_NETWORK; - dest->len = 0; /* no SLEN */ + dest->len = 0; /* no SLEN */ for (i = 0; i < MAX_MAC_LEN; i++) { /* no SADR */ dest->adr[i] = 0; diff --git a/bacnet-stack/ports/arm7/net.h b/bacnet-stack/ports/arm7/net.h index 2e81e570..99de084c 100644 --- a/bacnet-stack/ports/arm7/net.h +++ b/bacnet-stack/ports/arm7/net.h @@ -29,21 +29,20 @@ #include "stdint.h" struct sockaddr { - uint16_t sa_family; - char sa_data[14]; + uint16_t sa_family; + char sa_data[14]; }; struct in_addr { - uint32_t s_addr; /* load with inet_aton() */ + uint32_t s_addr; /* load with inet_aton() */ }; -struct sockaddr_in -{ - int16_t sin_family; /* e.g. AF_INET */ - uint16_t sin_port; /* e.g. htons(3490) */ - struct in_addr sin_addr; /* see struct in_addr, below */ - char sin_zero[8]; /* zero this if you want to */ +struct sockaddr_in { + int16_t sin_family; /* e.g. AF_INET */ + uint16_t sin_port; /* e.g. htons(3490) */ + struct in_addr sin_addr; /* see struct in_addr, below */ + char sin_zero[8]; /* zero this if you want to */ }; @@ -74,42 +73,47 @@ typedef int socklen_t; ((((uint32_t)(n) & 0xFF000000)) >> 24)) -#define AF_UNIX 1 /* local to host (pipes, portals) */ -#define AF_INET 2 /* internetwork: UDP, TCP, etc. */ -#define AF_IMPLINK 3 /* arpanet imp addresses */ -#define AF_PUP 4 /* pup protocols: e.g. BSP */ -#define AF_CHAOS 5 /* mit CHAOS protocols */ -#define AF_NS 6 /* XEROX NS protocols */ -#define AF_IPX AF_NS /* IPX protocols: IPX, SPX, etc. */ -#define AF_ISO 7 /* ISO protocols */ -#define AF_OSI AF_ISO /* OSI is ISO */ -#define AF_ECMA 8 /* european computer manufacturers */ -#define AF_DATAKIT 9 /* datakit protocols */ -#define AF_CCITT 10 /* CCITT protocols, X.25 etc */ -#define AF_SNA 11 /* IBM SNA */ -#define AF_DECnet 12 /* DECnet */ -#define AF_DLI 13 /* Direct data link interface */ -#define AF_LAT 14 /* LAT */ -#define AF_HYLINK 15 /* NSC Hyperchannel */ -#define AF_APPLETALK 16 /* AppleTalk */ -#define AF_NETBIOS 17 /* NetBios-style addresses */ -#define AF_VOICEVIEW 18 /* VoiceView */ -#define AF_FIREFOX 19 /* Protocols from Firefox */ -#define AF_UNKNOWN1 20 /* Somebody is using this! */ -#define AF_BAN 21 /* Banyan */ -#define AF_ATM 22 /* Native ATM Services */ -#define AF_INET6 23 /* Internetwork Version 6 */ -#define AF_CLUSTER 24 /* Microsoft Wolfpack */ -#define AF_12844 25 /* IEEE 1284.4 WG AF */ -#define AF_IRDA 26 /* IrDA */ -#define AF_NETDES 28 /* Network Designers OSI & gateway - enabled protocols */ +#define AF_UNIX 1 /* local to host (pipes, portals) */ +#define AF_INET 2 /* internetwork: UDP, TCP, etc. */ +#define AF_IMPLINK 3 /* arpanet imp addresses */ +#define AF_PUP 4 /* pup protocols: e.g. BSP */ +#define AF_CHAOS 5 /* mit CHAOS protocols */ +#define AF_NS 6 /* XEROX NS protocols */ +#define AF_IPX AF_NS /* IPX protocols: IPX, SPX, etc. */ +#define AF_ISO 7 /* ISO protocols */ +#define AF_OSI AF_ISO /* OSI is ISO */ +#define AF_ECMA 8 /* european computer manufacturers */ +#define AF_DATAKIT 9 /* datakit protocols */ +#define AF_CCITT 10 /* CCITT protocols, X.25 etc */ +#define AF_SNA 11 /* IBM SNA */ +#define AF_DECnet 12 /* DECnet */ +#define AF_DLI 13 /* Direct data link interface */ +#define AF_LAT 14 /* LAT */ +#define AF_HYLINK 15 /* NSC Hyperchannel */ +#define AF_APPLETALK 16 /* AppleTalk */ +#define AF_NETBIOS 17 /* NetBios-style addresses */ +#define AF_VOICEVIEW 18 /* VoiceView */ +#define AF_FIREFOX 19 /* Protocols from Firefox */ +#define AF_UNKNOWN1 20 /* Somebody is using this! */ +#define AF_BAN 21 /* Banyan */ +#define AF_ATM 22 /* Native ATM Services */ +#define AF_INET6 23 /* Internetwork Version 6 */ +#define AF_CLUSTER 24 /* Microsoft Wolfpack */ +#define AF_12844 25 /* IEEE 1284.4 WG AF */ +#define AF_IRDA 26 /* IrDA */ +#define AF_NETDES 28 /* Network Designers OSI & gateway + enabled protocols */ #define AF_TCNPROCESS 29 #define AF_TCNMESSAGE 30 #define AF_ICLFXBM 31 #define AF_MAX 32 -extern void set_address(uint32_t *net_address, uint8_t octet1, uint8_t octet2, uint8_t octet3, uint8_t octet4); +extern void set_address( + uint32_t * net_address, + uint8_t octet1, + uint8_t octet2, + uint8_t octet3, + uint8_t octet4); #endif diff --git a/bacnet-stack/ports/at91sam7s/ai.c b/bacnet-stack/ports/at91sam7s/ai.c index f47af3f4..ca91a782 100644 --- a/bacnet-stack/ports/at91sam7s/ai.c +++ b/bacnet-stack/ports/at91sam7s/ai.c @@ -36,7 +36,7 @@ /* Analog Input = Photocell */ #define MAX_ANALOG_INPUTS 2 #if (MAX_ANALOG_INPUTS > 9) - #error Modify the Analog_Input_Name to handle multiple digits +#error Modify the Analog_Input_Name to handle multiple digits #endif static uint8_t Present_Value[MAX_ANALOG_INPUTS]; @@ -44,7 +44,8 @@ static uint8_t Present_Value[MAX_ANALOG_INPUTS]; /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need validate that the */ /* given instance exists */ -bool Analog_Input_Valid_Instance(uint32_t object_instance) +bool Analog_Input_Valid_Instance( + uint32_t object_instance) { if (object_instance < MAX_ANALOG_INPUTS) return true; @@ -53,95 +54,104 @@ bool Analog_Input_Valid_Instance(uint32_t object_instance) } /* we simply have 0-n object instances. */ -unsigned Analog_Input_Count(void) +unsigned Analog_Input_Count( + void) { return MAX_ANALOG_INPUTS; } /* we simply have 0-n object instances. */ -uint32_t Analog_Input_Index_To_Instance(unsigned index) +uint32_t Analog_Input_Index_To_Instance( + unsigned index) { return index; } -char *Analog_Input_Name(uint32_t object_instance) +char *Analog_Input_Name( + uint32_t object_instance) { - static char text_string[16] = "AI-0"; /* okay for single thread */ + static char text_string[16] = "AI-0"; /* okay for single thread */ if (object_instance < MAX_ANALOG_INPUTS) { - text_string[3] = '0' + (uint8_t)object_instance; + text_string[3] = '0' + (uint8_t) object_instance; return text_string; } return NULL; } -static float Analog_Input_Present_Value(uint32_t object_instance) +static float Analog_Input_Present_Value( + uint32_t object_instance) { - float value = 0.0; - - if (object_instance < MAX_ANALOG_INPUTS) - value = Present_Value[object_instance]; - - return value; + float value = 0.0; + + if (object_instance < MAX_ANALOG_INPUTS) + value = Present_Value[object_instance]; + + return value; } /* return apdu length, or -1 on error */ /* assumption - object has already exists */ -int Analog_Input_Encode_Property_APDU(uint8_t * apdu, +int Analog_Input_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ BACNET_BIT_STRING bit_string; BACNET_CHARACTER_STRING char_string; (void) array_index; switch (property) { - case PROP_OBJECT_IDENTIFIER: - apdu_len = encode_application_object_id(&apdu[0], OBJECT_ANALOG_INPUT, - object_instance); - break; - /* note: Name and Description don't have to be the same. - You could make Description writable and different */ - case PROP_OBJECT_NAME: - case PROP_DESCRIPTION: - characterstring_init_ansi(&char_string, - Analog_Input_Name(object_instance)); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_OBJECT_TYPE: - apdu_len = encode_application_enumerated(&apdu[0], - OBJECT_ANALOG_INPUT); - break; - case PROP_PRESENT_VALUE: - apdu_len = encode_application_real(&apdu[0], - Analog_Input_Present_Value(object_instance)); - break; - case PROP_STATUS_FLAGS: - bitstring_init(&bit_string); - bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); - apdu_len = encode_application_bitstring(&apdu[0], &bit_string); - break; - case PROP_EVENT_STATE: - apdu_len = encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); - break; - case PROP_OUT_OF_SERVICE: - apdu_len = encode_application_boolean(&apdu[0], false); - break; - case PROP_UNITS: - apdu_len = encode_application_enumerated(&apdu[0], UNITS_PERCENT); - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_UNKNOWN_PROPERTY; - apdu_len = -1; - break; + case PROP_OBJECT_IDENTIFIER: + apdu_len = + encode_application_object_id(&apdu[0], OBJECT_ANALOG_INPUT, + object_instance); + break; + /* note: Name and Description don't have to be the same. + You could make Description writable and different */ + case PROP_OBJECT_NAME: + case PROP_DESCRIPTION: + characterstring_init_ansi(&char_string, + Analog_Input_Name(object_instance)); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_OBJECT_TYPE: + apdu_len = encode_application_enumerated(&apdu[0], + OBJECT_ANALOG_INPUT); + break; + case PROP_PRESENT_VALUE: + apdu_len = encode_application_real(&apdu[0], + Analog_Input_Present_Value(object_instance)); + break; + case PROP_STATUS_FLAGS: + bitstring_init(&bit_string); + bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); + apdu_len = encode_application_bitstring(&apdu[0], &bit_string); + break; + case PROP_EVENT_STATE: + apdu_len = + encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); + break; + case PROP_OUT_OF_SERVICE: + apdu_len = encode_application_boolean(&apdu[0], false); + break; + case PROP_UNITS: + apdu_len = encode_application_enumerated(&apdu[0], UNITS_PERCENT); + break; + default: + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_UNKNOWN_PROPERTY; + apdu_len = -1; + break; } return apdu_len; @@ -152,7 +162,8 @@ int Analog_Input_Encode_Property_APDU(uint8_t * apdu, #include #include "ctest.h" -void testAnalogInput(Test * pTest) +void testAnalogInput( + Test * pTest) { uint8_t apdu[MAX_APDU] = { 0 }; int len = 0; @@ -168,8 +179,7 @@ void testAnalogInput(Test * pTest) /* FIXME: we should do a lot more testing here... */ len = Analog_Input_Encode_Property_APDU(&apdu[0], instance, - PROP_OBJECT_IDENTIFIER, - BACNET_ARRAY_ALL, &error_class, &error_code); + PROP_OBJECT_IDENTIFIER, BACNET_ARRAY_ALL, &error_class, &error_code); ct_test(pTest, len >= 0); len = decode_tag_number_and_value(&apdu[0], &tag_number, &len_value); ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_OBJECT_ID); @@ -182,7 +192,8 @@ void testAnalogInput(Test * pTest) } #ifdef TEST_ANALOG_INPUT -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -199,5 +210,5 @@ int main(void) return 0; } -#endif /* TEST_ANALOG_INPUT */ -#endif /* TEST */ +#endif /* TEST_ANALOG_INPUT */ +#endif /* TEST */ diff --git a/bacnet-stack/ports/at91sam7s/at91sam7s256.h b/bacnet-stack/ports/at91sam7s/at91sam7s256.h index 1d34b5bc..014b95a1 100644 --- a/bacnet-stack/ports/at91sam7s/at91sam7s256.h +++ b/bacnet-stack/ports/at91sam7s/at91sam7s256.h @@ -42,403 +42,410 @@ #ifndef AT91SAM7S256_H #define AT91SAM7S256_H -typedef volatile unsigned int AT91_REG;// Hardware register definition +typedef volatile unsigned int AT91_REG; // Hardware register definition // ***************************************************************************** // SOFTWARE API DEFINITION FOR System Peripherals // ***************************************************************************** typedef struct _AT91S_SYS { - AT91_REG AIC_SMR[32]; // Source Mode Register - AT91_REG AIC_SVR[32]; // Source Vector Register - AT91_REG AIC_IVR; // IRQ Vector Register - AT91_REG AIC_FVR; // FIQ Vector Register - AT91_REG AIC_ISR; // Interrupt Status Register - AT91_REG AIC_IPR; // Interrupt Pending Register - AT91_REG AIC_IMR; // Interrupt Mask Register - AT91_REG AIC_CISR; // Core Interrupt Status Register - AT91_REG Reserved0[2]; // - AT91_REG AIC_IECR; // Interrupt Enable Command Register - AT91_REG AIC_IDCR; // Interrupt Disable Command Register - AT91_REG AIC_ICCR; // Interrupt Clear Command Register - AT91_REG AIC_ISCR; // Interrupt Set Command Register - AT91_REG AIC_EOICR; // End of Interrupt Command Register - AT91_REG AIC_SPU; // Spurious Vector Register - AT91_REG AIC_DCR; // Debug Control Register (Protect) - AT91_REG Reserved1[1]; // - AT91_REG AIC_FFER; // Fast Forcing Enable Register - AT91_REG AIC_FFDR; // Fast Forcing Disable Register - AT91_REG AIC_FFSR; // Fast Forcing Status Register - AT91_REG Reserved2[45]; // - AT91_REG DBGU_CR; // Control Register - AT91_REG DBGU_MR; // Mode Register - AT91_REG DBGU_IER; // Interrupt Enable Register - AT91_REG DBGU_IDR; // Interrupt Disable Register - AT91_REG DBGU_IMR; // Interrupt Mask Register - AT91_REG DBGU_CSR; // Channel Status Register - AT91_REG DBGU_RHR; // Receiver Holding Register - AT91_REG DBGU_THR; // Transmitter Holding Register - AT91_REG DBGU_BRGR; // Baud Rate Generator Register - AT91_REG Reserved3[7]; // - AT91_REG DBGU_CIDR; // Chip ID Register - AT91_REG DBGU_EXID; // Chip ID Extension Register - AT91_REG DBGU_FNTR; // Force NTRST Register - AT91_REG Reserved4[45]; // - AT91_REG DBGU_RPR; // Receive Pointer Register - AT91_REG DBGU_RCR; // Receive Counter Register - AT91_REG DBGU_TPR; // Transmit Pointer Register - AT91_REG DBGU_TCR; // Transmit Counter Register - AT91_REG DBGU_RNPR; // Receive Next Pointer Register - AT91_REG DBGU_RNCR; // Receive Next Counter Register - AT91_REG DBGU_TNPR; // Transmit Next Pointer Register - AT91_REG DBGU_TNCR; // Transmit Next Counter Register - AT91_REG DBGU_PTCR; // PDC Transfer Control Register - AT91_REG DBGU_PTSR; // PDC Transfer Status Register - AT91_REG Reserved5[54]; // - AT91_REG PIOA_PER; // PIO Enable Register - AT91_REG PIOA_PDR; // PIO Disable Register - AT91_REG PIOA_PSR; // PIO Status Register - AT91_REG Reserved6[1]; // - AT91_REG PIOA_OER; // Output Enable Register - AT91_REG PIOA_ODR; // Output Disable Registerr - AT91_REG PIOA_OSR; // Output Status Register - AT91_REG Reserved7[1]; // - AT91_REG PIOA_IFER; // Input Filter Enable Register - AT91_REG PIOA_IFDR; // Input Filter Disable Register - AT91_REG PIOA_IFSR; // Input Filter Status Register - AT91_REG Reserved8[1]; // - AT91_REG PIOA_SODR; // Set Output Data Register - AT91_REG PIOA_CODR; // Clear Output Data Register - AT91_REG PIOA_ODSR; // Output Data Status Register - AT91_REG PIOA_PDSR; // Pin Data Status Register - AT91_REG PIOA_IER; // Interrupt Enable Register - AT91_REG PIOA_IDR; // Interrupt Disable Register - AT91_REG PIOA_IMR; // Interrupt Mask Register - AT91_REG PIOA_ISR; // Interrupt Status Register - AT91_REG PIOA_MDER; // Multi-driver Enable Register - AT91_REG PIOA_MDDR; // Multi-driver Disable Register - AT91_REG PIOA_MDSR; // Multi-driver Status Register - AT91_REG Reserved9[1]; // - AT91_REG PIOA_PPUDR; // Pull-up Disable Register - AT91_REG PIOA_PPUER; // Pull-up Enable Register - AT91_REG PIOA_PPUSR; // Pull-up Status Register - AT91_REG Reserved10[1]; // - AT91_REG PIOA_ASR; // Select A Register - AT91_REG PIOA_BSR; // Select B Register - AT91_REG PIOA_ABSR; // AB Select Status Register - AT91_REG Reserved11[9]; // - AT91_REG PIOA_OWER; // Output Write Enable Register - AT91_REG PIOA_OWDR; // Output Write Disable Register - AT91_REG PIOA_OWSR; // Output Write Status Register - AT91_REG Reserved12[469]; // - AT91_REG PMC_SCER; // System Clock Enable Register - AT91_REG PMC_SCDR; // System Clock Disable Register - AT91_REG PMC_SCSR; // System Clock Status Register - AT91_REG Reserved13[1]; // - AT91_REG PMC_PCER; // Peripheral Clock Enable Register - AT91_REG PMC_PCDR; // Peripheral Clock Disable Register - AT91_REG PMC_PCSR; // Peripheral Clock Status Register - AT91_REG Reserved14[1]; // - AT91_REG PMC_MOR; // Main Oscillator Register - AT91_REG PMC_MCFR; // Main Clock Frequency Register - AT91_REG Reserved15[1]; // - AT91_REG PMC_PLLR; // PLL Register - AT91_REG PMC_MCKR; // Master Clock Register - AT91_REG Reserved16[3]; // - AT91_REG PMC_PCKR[3]; // Programmable Clock Register - AT91_REG Reserved17[5]; // - AT91_REG PMC_IER; // Interrupt Enable Register - AT91_REG PMC_IDR; // Interrupt Disable Register - AT91_REG PMC_SR; // Status Register - AT91_REG PMC_IMR; // Interrupt Mask Register - AT91_REG Reserved18[36]; // - AT91_REG RSTC_RCR; // Reset Control Register - AT91_REG RSTC_RSR; // Reset Status Register - AT91_REG RSTC_RMR; // Reset Mode Register - AT91_REG Reserved19[5]; // - AT91_REG RTTC_RTMR; // Real-time Mode Register - AT91_REG RTTC_RTAR; // Real-time Alarm Register - AT91_REG RTTC_RTVR; // Real-time Value Register - AT91_REG RTTC_RTSR; // Real-time Status Register - AT91_REG PITC_PIMR; // Period Interval Mode Register - AT91_REG PITC_PISR; // Period Interval Status Register - AT91_REG PITC_PIVR; // Period Interval Value Register - AT91_REG PITC_PIIR; // Period Interval Image Register - AT91_REG WDTC_WDCR; // Watchdog Control Register - AT91_REG WDTC_WDMR; // Watchdog Mode Register - AT91_REG WDTC_WDSR; // Watchdog Status Register - AT91_REG Reserved20[5]; // - AT91_REG VREG_MR; // Voltage Regulator Mode Register -} AT91S_SYS, *AT91PS_SYS; + AT91_REG AIC_SMR[32]; // Source Mode Register + AT91_REG AIC_SVR[32]; // Source Vector Register + AT91_REG AIC_IVR; // IRQ Vector Register + AT91_REG AIC_FVR; // FIQ Vector Register + AT91_REG AIC_ISR; // Interrupt Status Register + AT91_REG AIC_IPR; // Interrupt Pending Register + AT91_REG AIC_IMR; // Interrupt Mask Register + AT91_REG AIC_CISR; // Core Interrupt Status Register + AT91_REG Reserved0[2]; // + AT91_REG AIC_IECR; // Interrupt Enable Command Register + AT91_REG AIC_IDCR; // Interrupt Disable Command Register + AT91_REG AIC_ICCR; // Interrupt Clear Command Register + AT91_REG AIC_ISCR; // Interrupt Set Command Register + AT91_REG AIC_EOICR; // End of Interrupt Command Register + AT91_REG AIC_SPU; // Spurious Vector Register + AT91_REG AIC_DCR; // Debug Control Register (Protect) + AT91_REG Reserved1[1]; // + AT91_REG AIC_FFER; // Fast Forcing Enable Register + AT91_REG AIC_FFDR; // Fast Forcing Disable Register + AT91_REG AIC_FFSR; // Fast Forcing Status Register + AT91_REG Reserved2[45]; // + AT91_REG DBGU_CR; // Control Register + AT91_REG DBGU_MR; // Mode Register + AT91_REG DBGU_IER; // Interrupt Enable Register + AT91_REG DBGU_IDR; // Interrupt Disable Register + AT91_REG DBGU_IMR; // Interrupt Mask Register + AT91_REG DBGU_CSR; // Channel Status Register + AT91_REG DBGU_RHR; // Receiver Holding Register + AT91_REG DBGU_THR; // Transmitter Holding Register + AT91_REG DBGU_BRGR; // Baud Rate Generator Register + AT91_REG Reserved3[7]; // + AT91_REG DBGU_CIDR; // Chip ID Register + AT91_REG DBGU_EXID; // Chip ID Extension Register + AT91_REG DBGU_FNTR; // Force NTRST Register + AT91_REG Reserved4[45]; // + AT91_REG DBGU_RPR; // Receive Pointer Register + AT91_REG DBGU_RCR; // Receive Counter Register + AT91_REG DBGU_TPR; // Transmit Pointer Register + AT91_REG DBGU_TCR; // Transmit Counter Register + AT91_REG DBGU_RNPR; // Receive Next Pointer Register + AT91_REG DBGU_RNCR; // Receive Next Counter Register + AT91_REG DBGU_TNPR; // Transmit Next Pointer Register + AT91_REG DBGU_TNCR; // Transmit Next Counter Register + AT91_REG DBGU_PTCR; // PDC Transfer Control Register + AT91_REG DBGU_PTSR; // PDC Transfer Status Register + AT91_REG Reserved5[54]; // + AT91_REG PIOA_PER; // PIO Enable Register + AT91_REG PIOA_PDR; // PIO Disable Register + AT91_REG PIOA_PSR; // PIO Status Register + AT91_REG Reserved6[1]; // + AT91_REG PIOA_OER; // Output Enable Register + AT91_REG PIOA_ODR; // Output Disable Registerr + AT91_REG PIOA_OSR; // Output Status Register + AT91_REG Reserved7[1]; // + AT91_REG PIOA_IFER; // Input Filter Enable Register + AT91_REG PIOA_IFDR; // Input Filter Disable Register + AT91_REG PIOA_IFSR; // Input Filter Status Register + AT91_REG Reserved8[1]; // + AT91_REG PIOA_SODR; // Set Output Data Register + AT91_REG PIOA_CODR; // Clear Output Data Register + AT91_REG PIOA_ODSR; // Output Data Status Register + AT91_REG PIOA_PDSR; // Pin Data Status Register + AT91_REG PIOA_IER; // Interrupt Enable Register + AT91_REG PIOA_IDR; // Interrupt Disable Register + AT91_REG PIOA_IMR; // Interrupt Mask Register + AT91_REG PIOA_ISR; // Interrupt Status Register + AT91_REG PIOA_MDER; // Multi-driver Enable Register + AT91_REG PIOA_MDDR; // Multi-driver Disable Register + AT91_REG PIOA_MDSR; // Multi-driver Status Register + AT91_REG Reserved9[1]; // + AT91_REG PIOA_PPUDR; // Pull-up Disable Register + AT91_REG PIOA_PPUER; // Pull-up Enable Register + AT91_REG PIOA_PPUSR; // Pull-up Status Register + AT91_REG Reserved10[1]; // + AT91_REG PIOA_ASR; // Select A Register + AT91_REG PIOA_BSR; // Select B Register + AT91_REG PIOA_ABSR; // AB Select Status Register + AT91_REG Reserved11[9]; // + AT91_REG PIOA_OWER; // Output Write Enable Register + AT91_REG PIOA_OWDR; // Output Write Disable Register + AT91_REG PIOA_OWSR; // Output Write Status Register + AT91_REG Reserved12[469]; // + AT91_REG PMC_SCER; // System Clock Enable Register + AT91_REG PMC_SCDR; // System Clock Disable Register + AT91_REG PMC_SCSR; // System Clock Status Register + AT91_REG Reserved13[1]; // + AT91_REG PMC_PCER; // Peripheral Clock Enable Register + AT91_REG PMC_PCDR; // Peripheral Clock Disable Register + AT91_REG PMC_PCSR; // Peripheral Clock Status Register + AT91_REG Reserved14[1]; // + AT91_REG PMC_MOR; // Main Oscillator Register + AT91_REG PMC_MCFR; // Main Clock Frequency Register + AT91_REG Reserved15[1]; // + AT91_REG PMC_PLLR; // PLL Register + AT91_REG PMC_MCKR; // Master Clock Register + AT91_REG Reserved16[3]; // + AT91_REG PMC_PCKR[3]; // Programmable Clock Register + AT91_REG Reserved17[5]; // + AT91_REG PMC_IER; // Interrupt Enable Register + AT91_REG PMC_IDR; // Interrupt Disable Register + AT91_REG PMC_SR; // Status Register + AT91_REG PMC_IMR; // Interrupt Mask Register + AT91_REG Reserved18[36]; // + AT91_REG RSTC_RCR; // Reset Control Register + AT91_REG RSTC_RSR; // Reset Status Register + AT91_REG RSTC_RMR; // Reset Mode Register + AT91_REG Reserved19[5]; // + AT91_REG RTTC_RTMR; // Real-time Mode Register + AT91_REG RTTC_RTAR; // Real-time Alarm Register + AT91_REG RTTC_RTVR; // Real-time Value Register + AT91_REG RTTC_RTSR; // Real-time Status Register + AT91_REG PITC_PIMR; // Period Interval Mode Register + AT91_REG PITC_PISR; // Period Interval Status Register + AT91_REG PITC_PIVR; // Period Interval Value Register + AT91_REG PITC_PIIR; // Period Interval Image Register + AT91_REG WDTC_WDCR; // Watchdog Control Register + AT91_REG WDTC_WDMR; // Watchdog Mode Register + AT91_REG WDTC_WDSR; // Watchdog Status Register + AT91_REG Reserved20[5]; // + AT91_REG VREG_MR; // Voltage Regulator Mode Register +} AT91S_SYS, + *AT91PS_SYS; // ***************************************************************************** // SOFTWARE API DEFINITION FOR Advanced Interrupt Controller // ***************************************************************************** typedef struct _AT91S_AIC { - AT91_REG AIC_SMR[32]; // Source Mode Register - AT91_REG AIC_SVR[32]; // Source Vector Register - AT91_REG AIC_IVR; // IRQ Vector Register - AT91_REG AIC_FVR; // FIQ Vector Register - AT91_REG AIC_ISR; // Interrupt Status Register - AT91_REG AIC_IPR; // Interrupt Pending Register - AT91_REG AIC_IMR; // Interrupt Mask Register - AT91_REG AIC_CISR; // Core Interrupt Status Register - AT91_REG Reserved0[2]; // - AT91_REG AIC_IECR; // Interrupt Enable Command Register - AT91_REG AIC_IDCR; // Interrupt Disable Command Register - AT91_REG AIC_ICCR; // Interrupt Clear Command Register - AT91_REG AIC_ISCR; // Interrupt Set Command Register - AT91_REG AIC_EOICR; // End of Interrupt Command Register - AT91_REG AIC_SPU; // Spurious Vector Register - AT91_REG AIC_DCR; // Debug Control Register (Protect) - AT91_REG Reserved1[1]; // - AT91_REG AIC_FFER; // Fast Forcing Enable Register - AT91_REG AIC_FFDR; // Fast Forcing Disable Register - AT91_REG AIC_FFSR; // Fast Forcing Status Register -} AT91S_AIC, *AT91PS_AIC; + AT91_REG AIC_SMR[32]; // Source Mode Register + AT91_REG AIC_SVR[32]; // Source Vector Register + AT91_REG AIC_IVR; // IRQ Vector Register + AT91_REG AIC_FVR; // FIQ Vector Register + AT91_REG AIC_ISR; // Interrupt Status Register + AT91_REG AIC_IPR; // Interrupt Pending Register + AT91_REG AIC_IMR; // Interrupt Mask Register + AT91_REG AIC_CISR; // Core Interrupt Status Register + AT91_REG Reserved0[2]; // + AT91_REG AIC_IECR; // Interrupt Enable Command Register + AT91_REG AIC_IDCR; // Interrupt Disable Command Register + AT91_REG AIC_ICCR; // Interrupt Clear Command Register + AT91_REG AIC_ISCR; // Interrupt Set Command Register + AT91_REG AIC_EOICR; // End of Interrupt Command Register + AT91_REG AIC_SPU; // Spurious Vector Register + AT91_REG AIC_DCR; // Debug Control Register (Protect) + AT91_REG Reserved1[1]; // + AT91_REG AIC_FFER; // Fast Forcing Enable Register + AT91_REG AIC_FFDR; // Fast Forcing Disable Register + AT91_REG AIC_FFSR; // Fast Forcing Status Register +} AT91S_AIC, + *AT91PS_AIC; // -------- AIC_SMR : (AIC Offset: 0x0) Control Register -------- -#define AT91C_AIC_PRIOR ((unsigned int) 0x7 << 0) // (AIC) Priority Level -#define AT91C_AIC_PRIOR_LOWEST ((unsigned int) 0x0) // (AIC) Lowest priority level -#define AT91C_AIC_PRIOR_HIGHEST ((unsigned int) 0x7) // (AIC) Highest priority level -#define AT91C_AIC_SRCTYPE ((unsigned int) 0x3 << 5) // (AIC) Interrupt Source Type -#define AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE ((unsigned int) 0x0 << 5) // (AIC) Internal Sources Code Label Level Sensitive -#define AT91C_AIC_SRCTYPE_INT_EDGE_TRIGGERED ((unsigned int) 0x1 << 5) // (AIC) Internal Sources Code Label Edge triggered -#define AT91C_AIC_SRCTYPE_EXT_HIGH_LEVEL ((unsigned int) 0x2 << 5) // (AIC) External Sources Code Label High-level Sensitive -#define AT91C_AIC_SRCTYPE_EXT_POSITIVE_EDGE ((unsigned int) 0x3 << 5) // (AIC) External Sources Code Label Positive Edge triggered +#define AT91C_AIC_PRIOR ((unsigned int) 0x7 << 0) // (AIC) Priority Level +#define AT91C_AIC_PRIOR_LOWEST ((unsigned int) 0x0) // (AIC) Lowest priority level +#define AT91C_AIC_PRIOR_HIGHEST ((unsigned int) 0x7) // (AIC) Highest priority level +#define AT91C_AIC_SRCTYPE ((unsigned int) 0x3 << 5) // (AIC) Interrupt Source Type +#define AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE ((unsigned int) 0x0 << 5) // (AIC) Internal Sources Code Label Level Sensitive +#define AT91C_AIC_SRCTYPE_INT_EDGE_TRIGGERED ((unsigned int) 0x1 << 5) // (AIC) Internal Sources Code Label Edge triggered +#define AT91C_AIC_SRCTYPE_EXT_HIGH_LEVEL ((unsigned int) 0x2 << 5) // (AIC) External Sources Code Label High-level Sensitive +#define AT91C_AIC_SRCTYPE_EXT_POSITIVE_EDGE ((unsigned int) 0x3 << 5) // (AIC) External Sources Code Label Positive Edge triggered // -------- AIC_CISR : (AIC Offset: 0x114) AIC Core Interrupt Status Register -------- -#define AT91C_AIC_NFIQ ((unsigned int) 0x1 << 0) // (AIC) NFIQ Status -#define AT91C_AIC_NIRQ ((unsigned int) 0x1 << 1) // (AIC) NIRQ Status +#define AT91C_AIC_NFIQ ((unsigned int) 0x1 << 0) // (AIC) NFIQ Status +#define AT91C_AIC_NIRQ ((unsigned int) 0x1 << 1) // (AIC) NIRQ Status // -------- AIC_DCR : (AIC Offset: 0x138) AIC Debug Control Register (Protect) -------- -#define AT91C_AIC_DCR_PROT ((unsigned int) 0x1 << 0) // (AIC) Protection Mode -#define AT91C_AIC_DCR_GMSK ((unsigned int) 0x1 << 1) // (AIC) General Mask +#define AT91C_AIC_DCR_PROT ((unsigned int) 0x1 << 0) // (AIC) Protection Mode +#define AT91C_AIC_DCR_GMSK ((unsigned int) 0x1 << 1) // (AIC) General Mask // ***************************************************************************** // SOFTWARE API DEFINITION FOR Peripheral DMA Controller // ***************************************************************************** typedef struct _AT91S_PDC { - AT91_REG PDC_RPR; // Receive Pointer Register - AT91_REG PDC_RCR; // Receive Counter Register - AT91_REG PDC_TPR; // Transmit Pointer Register - AT91_REG PDC_TCR; // Transmit Counter Register - AT91_REG PDC_RNPR; // Receive Next Pointer Register - AT91_REG PDC_RNCR; // Receive Next Counter Register - AT91_REG PDC_TNPR; // Transmit Next Pointer Register - AT91_REG PDC_TNCR; // Transmit Next Counter Register - AT91_REG PDC_PTCR; // PDC Transfer Control Register - AT91_REG PDC_PTSR; // PDC Transfer Status Register -} AT91S_PDC, *AT91PS_PDC; + AT91_REG PDC_RPR; // Receive Pointer Register + AT91_REG PDC_RCR; // Receive Counter Register + AT91_REG PDC_TPR; // Transmit Pointer Register + AT91_REG PDC_TCR; // Transmit Counter Register + AT91_REG PDC_RNPR; // Receive Next Pointer Register + AT91_REG PDC_RNCR; // Receive Next Counter Register + AT91_REG PDC_TNPR; // Transmit Next Pointer Register + AT91_REG PDC_TNCR; // Transmit Next Counter Register + AT91_REG PDC_PTCR; // PDC Transfer Control Register + AT91_REG PDC_PTSR; // PDC Transfer Status Register +} AT91S_PDC, + *AT91PS_PDC; // -------- PDC_PTCR : (PDC Offset: 0x20) PDC Transfer Control Register -------- -#define AT91C_PDC_RXTEN ((unsigned int) 0x1 << 0) // (PDC) Receiver Transfer Enable -#define AT91C_PDC_RXTDIS ((unsigned int) 0x1 << 1) // (PDC) Receiver Transfer Disable -#define AT91C_PDC_TXTEN ((unsigned int) 0x1 << 8) // (PDC) Transmitter Transfer Enable -#define AT91C_PDC_TXTDIS ((unsigned int) 0x1 << 9) // (PDC) Transmitter Transfer Disable +#define AT91C_PDC_RXTEN ((unsigned int) 0x1 << 0) // (PDC) Receiver Transfer Enable +#define AT91C_PDC_RXTDIS ((unsigned int) 0x1 << 1) // (PDC) Receiver Transfer Disable +#define AT91C_PDC_TXTEN ((unsigned int) 0x1 << 8) // (PDC) Transmitter Transfer Enable +#define AT91C_PDC_TXTDIS ((unsigned int) 0x1 << 9) // (PDC) Transmitter Transfer Disable // -------- PDC_PTSR : (PDC Offset: 0x24) PDC Transfer Status Register -------- // ***************************************************************************** // SOFTWARE API DEFINITION FOR Debug Unit // ***************************************************************************** typedef struct _AT91S_DBGU { - AT91_REG DBGU_CR; // Control Register - AT91_REG DBGU_MR; // Mode Register - AT91_REG DBGU_IER; // Interrupt Enable Register - AT91_REG DBGU_IDR; // Interrupt Disable Register - AT91_REG DBGU_IMR; // Interrupt Mask Register - AT91_REG DBGU_CSR; // Channel Status Register - AT91_REG DBGU_RHR; // Receiver Holding Register - AT91_REG DBGU_THR; // Transmitter Holding Register - AT91_REG DBGU_BRGR; // Baud Rate Generator Register - AT91_REG Reserved0[7]; // - AT91_REG DBGU_CIDR; // Chip ID Register - AT91_REG DBGU_EXID; // Chip ID Extension Register - AT91_REG DBGU_FNTR; // Force NTRST Register - AT91_REG Reserved1[45]; // - AT91_REG DBGU_RPR; // Receive Pointer Register - AT91_REG DBGU_RCR; // Receive Counter Register - AT91_REG DBGU_TPR; // Transmit Pointer Register - AT91_REG DBGU_TCR; // Transmit Counter Register - AT91_REG DBGU_RNPR; // Receive Next Pointer Register - AT91_REG DBGU_RNCR; // Receive Next Counter Register - AT91_REG DBGU_TNPR; // Transmit Next Pointer Register - AT91_REG DBGU_TNCR; // Transmit Next Counter Register - AT91_REG DBGU_PTCR; // PDC Transfer Control Register - AT91_REG DBGU_PTSR; // PDC Transfer Status Register -} AT91S_DBGU, *AT91PS_DBGU; + AT91_REG DBGU_CR; // Control Register + AT91_REG DBGU_MR; // Mode Register + AT91_REG DBGU_IER; // Interrupt Enable Register + AT91_REG DBGU_IDR; // Interrupt Disable Register + AT91_REG DBGU_IMR; // Interrupt Mask Register + AT91_REG DBGU_CSR; // Channel Status Register + AT91_REG DBGU_RHR; // Receiver Holding Register + AT91_REG DBGU_THR; // Transmitter Holding Register + AT91_REG DBGU_BRGR; // Baud Rate Generator Register + AT91_REG Reserved0[7]; // + AT91_REG DBGU_CIDR; // Chip ID Register + AT91_REG DBGU_EXID; // Chip ID Extension Register + AT91_REG DBGU_FNTR; // Force NTRST Register + AT91_REG Reserved1[45]; // + AT91_REG DBGU_RPR; // Receive Pointer Register + AT91_REG DBGU_RCR; // Receive Counter Register + AT91_REG DBGU_TPR; // Transmit Pointer Register + AT91_REG DBGU_TCR; // Transmit Counter Register + AT91_REG DBGU_RNPR; // Receive Next Pointer Register + AT91_REG DBGU_RNCR; // Receive Next Counter Register + AT91_REG DBGU_TNPR; // Transmit Next Pointer Register + AT91_REG DBGU_TNCR; // Transmit Next Counter Register + AT91_REG DBGU_PTCR; // PDC Transfer Control Register + AT91_REG DBGU_PTSR; // PDC Transfer Status Register +} AT91S_DBGU, + *AT91PS_DBGU; // -------- DBGU_CR : (DBGU Offset: 0x0) Debug Unit Control Register -------- -#define AT91C_US_RSTRX ((unsigned int) 0x1 << 2) // (DBGU) Reset Receiver -#define AT91C_US_RSTTX ((unsigned int) 0x1 << 3) // (DBGU) Reset Transmitter -#define AT91C_US_RXEN ((unsigned int) 0x1 << 4) // (DBGU) Receiver Enable -#define AT91C_US_RXDIS ((unsigned int) 0x1 << 5) // (DBGU) Receiver Disable -#define AT91C_US_TXEN ((unsigned int) 0x1 << 6) // (DBGU) Transmitter Enable -#define AT91C_US_TXDIS ((unsigned int) 0x1 << 7) // (DBGU) Transmitter Disable -#define AT91C_US_RSTSTA ((unsigned int) 0x1 << 8) // (DBGU) Reset Status Bits +#define AT91C_US_RSTRX ((unsigned int) 0x1 << 2) // (DBGU) Reset Receiver +#define AT91C_US_RSTTX ((unsigned int) 0x1 << 3) // (DBGU) Reset Transmitter +#define AT91C_US_RXEN ((unsigned int) 0x1 << 4) // (DBGU) Receiver Enable +#define AT91C_US_RXDIS ((unsigned int) 0x1 << 5) // (DBGU) Receiver Disable +#define AT91C_US_TXEN ((unsigned int) 0x1 << 6) // (DBGU) Transmitter Enable +#define AT91C_US_TXDIS ((unsigned int) 0x1 << 7) // (DBGU) Transmitter Disable +#define AT91C_US_RSTSTA ((unsigned int) 0x1 << 8) // (DBGU) Reset Status Bits // -------- DBGU_MR : (DBGU Offset: 0x4) Debug Unit Mode Register -------- -#define AT91C_US_PAR ((unsigned int) 0x7 << 9) // (DBGU) Parity type -#define AT91C_US_PAR_EVEN ((unsigned int) 0x0 << 9) // (DBGU) Even Parity -#define AT91C_US_PAR_ODD ((unsigned int) 0x1 << 9) // (DBGU) Odd Parity -#define AT91C_US_PAR_SPACE ((unsigned int) 0x2 << 9) // (DBGU) Parity forced to 0 (Space) -#define AT91C_US_PAR_MARK ((unsigned int) 0x3 << 9) // (DBGU) Parity forced to 1 (Mark) -#define AT91C_US_PAR_NONE ((unsigned int) 0x4 << 9) // (DBGU) No Parity -#define AT91C_US_PAR_MULTI_DROP ((unsigned int) 0x6 << 9) // (DBGU) Multi-drop mode -#define AT91C_US_CHMODE ((unsigned int) 0x3 << 14) // (DBGU) Channel Mode +#define AT91C_US_PAR ((unsigned int) 0x7 << 9) // (DBGU) Parity type +#define AT91C_US_PAR_EVEN ((unsigned int) 0x0 << 9) // (DBGU) Even Parity +#define AT91C_US_PAR_ODD ((unsigned int) 0x1 << 9) // (DBGU) Odd Parity +#define AT91C_US_PAR_SPACE ((unsigned int) 0x2 << 9) // (DBGU) Parity forced to 0 (Space) +#define AT91C_US_PAR_MARK ((unsigned int) 0x3 << 9) // (DBGU) Parity forced to 1 (Mark) +#define AT91C_US_PAR_NONE ((unsigned int) 0x4 << 9) // (DBGU) No Parity +#define AT91C_US_PAR_MULTI_DROP ((unsigned int) 0x6 << 9) // (DBGU) Multi-drop mode +#define AT91C_US_CHMODE ((unsigned int) 0x3 << 14) // (DBGU) Channel Mode #define AT91C_US_CHMODE_NORMAL ((unsigned int) 0x0 << 14) // (DBGU) Normal Mode: The USART channel operates as an RX/TX USART. #define AT91C_US_CHMODE_AUTO ((unsigned int) 0x1 << 14) // (DBGU) Automatic Echo: Receiver Data Input is connected to the TXD pin. #define AT91C_US_CHMODE_LOCAL ((unsigned int) 0x2 << 14) // (DBGU) Local Loopback: Transmitter Output Signal is connected to Receiver Input Signal. #define AT91C_US_CHMODE_REMOTE ((unsigned int) 0x3 << 14) // (DBGU) Remote Loopback: RXD pin is internally connected to TXD pin. // -------- DBGU_IER : (DBGU Offset: 0x8) Debug Unit Interrupt Enable Register -------- -#define AT91C_US_RXRDY ((unsigned int) 0x1 << 0) // (DBGU) RXRDY Interrupt -#define AT91C_US_TXRDY ((unsigned int) 0x1 << 1) // (DBGU) TXRDY Interrupt -#define AT91C_US_ENDRX ((unsigned int) 0x1 << 3) // (DBGU) End of Receive Transfer Interrupt -#define AT91C_US_ENDTX ((unsigned int) 0x1 << 4) // (DBGU) End of Transmit Interrupt -#define AT91C_US_OVRE ((unsigned int) 0x1 << 5) // (DBGU) Overrun Interrupt -#define AT91C_US_FRAME ((unsigned int) 0x1 << 6) // (DBGU) Framing Error Interrupt -#define AT91C_US_PARE ((unsigned int) 0x1 << 7) // (DBGU) Parity Error Interrupt -#define AT91C_US_TXEMPTY ((unsigned int) 0x1 << 9) // (DBGU) TXEMPTY Interrupt -#define AT91C_US_TXBUFE ((unsigned int) 0x1 << 11) // (DBGU) TXBUFE Interrupt -#define AT91C_US_RXBUFF ((unsigned int) 0x1 << 12) // (DBGU) RXBUFF Interrupt -#define AT91C_US_COMM_TX ((unsigned int) 0x1 << 30) // (DBGU) COMM_TX Interrupt -#define AT91C_US_COMM_RX ((unsigned int) 0x1 << 31) // (DBGU) COMM_RX Interrupt +#define AT91C_US_RXRDY ((unsigned int) 0x1 << 0) // (DBGU) RXRDY Interrupt +#define AT91C_US_TXRDY ((unsigned int) 0x1 << 1) // (DBGU) TXRDY Interrupt +#define AT91C_US_ENDRX ((unsigned int) 0x1 << 3) // (DBGU) End of Receive Transfer Interrupt +#define AT91C_US_ENDTX ((unsigned int) 0x1 << 4) // (DBGU) End of Transmit Interrupt +#define AT91C_US_OVRE ((unsigned int) 0x1 << 5) // (DBGU) Overrun Interrupt +#define AT91C_US_FRAME ((unsigned int) 0x1 << 6) // (DBGU) Framing Error Interrupt +#define AT91C_US_PARE ((unsigned int) 0x1 << 7) // (DBGU) Parity Error Interrupt +#define AT91C_US_TXEMPTY ((unsigned int) 0x1 << 9) // (DBGU) TXEMPTY Interrupt +#define AT91C_US_TXBUFE ((unsigned int) 0x1 << 11) // (DBGU) TXBUFE Interrupt +#define AT91C_US_RXBUFF ((unsigned int) 0x1 << 12) // (DBGU) RXBUFF Interrupt +#define AT91C_US_COMM_TX ((unsigned int) 0x1 << 30) // (DBGU) COMM_TX Interrupt +#define AT91C_US_COMM_RX ((unsigned int) 0x1 << 31) // (DBGU) COMM_RX Interrupt // -------- DBGU_IDR : (DBGU Offset: 0xc) Debug Unit Interrupt Disable Register -------- // -------- DBGU_IMR : (DBGU Offset: 0x10) Debug Unit Interrupt Mask Register -------- // -------- DBGU_CSR : (DBGU Offset: 0x14) Debug Unit Channel Status Register -------- // -------- DBGU_FNTR : (DBGU Offset: 0x48) Debug Unit FORCE_NTRST Register -------- -#define AT91C_US_FORCE_NTRST ((unsigned int) 0x1 << 0) // (DBGU) Force NTRST in JTAG +#define AT91C_US_FORCE_NTRST ((unsigned int) 0x1 << 0) // (DBGU) Force NTRST in JTAG // ***************************************************************************** // SOFTWARE API DEFINITION FOR Parallel Input Output Controler // ***************************************************************************** typedef struct _AT91S_PIO { - AT91_REG PIO_PER; // PIO Enable Register - AT91_REG PIO_PDR; // PIO Disable Register - AT91_REG PIO_PSR; // PIO Status Register - AT91_REG Reserved0[1]; // - AT91_REG PIO_OER; // Output Enable Register - AT91_REG PIO_ODR; // Output Disable Registerr - AT91_REG PIO_OSR; // Output Status Register - AT91_REG Reserved1[1]; // - AT91_REG PIO_IFER; // Input Filter Enable Register - AT91_REG PIO_IFDR; // Input Filter Disable Register - AT91_REG PIO_IFSR; // Input Filter Status Register - AT91_REG Reserved2[1]; // - AT91_REG PIO_SODR; // Set Output Data Register - AT91_REG PIO_CODR; // Clear Output Data Register - AT91_REG PIO_ODSR; // Output Data Status Register - AT91_REG PIO_PDSR; // Pin Data Status Register - AT91_REG PIO_IER; // Interrupt Enable Register - AT91_REG PIO_IDR; // Interrupt Disable Register - AT91_REG PIO_IMR; // Interrupt Mask Register - AT91_REG PIO_ISR; // Interrupt Status Register - AT91_REG PIO_MDER; // Multi-driver Enable Register - AT91_REG PIO_MDDR; // Multi-driver Disable Register - AT91_REG PIO_MDSR; // Multi-driver Status Register - AT91_REG Reserved3[1]; // - AT91_REG PIO_PPUDR; // Pull-up Disable Register - AT91_REG PIO_PPUER; // Pull-up Enable Register - AT91_REG PIO_PPUSR; // Pull-up Status Register - AT91_REG Reserved4[1]; // - AT91_REG PIO_ASR; // Select A Register - AT91_REG PIO_BSR; // Select B Register - AT91_REG PIO_ABSR; // AB Select Status Register - AT91_REG Reserved5[9]; // - AT91_REG PIO_OWER; // Output Write Enable Register - AT91_REG PIO_OWDR; // Output Write Disable Register - AT91_REG PIO_OWSR; // Output Write Status Register -} AT91S_PIO, *AT91PS_PIO; + AT91_REG PIO_PER; // PIO Enable Register + AT91_REG PIO_PDR; // PIO Disable Register + AT91_REG PIO_PSR; // PIO Status Register + AT91_REG Reserved0[1]; // + AT91_REG PIO_OER; // Output Enable Register + AT91_REG PIO_ODR; // Output Disable Registerr + AT91_REG PIO_OSR; // Output Status Register + AT91_REG Reserved1[1]; // + AT91_REG PIO_IFER; // Input Filter Enable Register + AT91_REG PIO_IFDR; // Input Filter Disable Register + AT91_REG PIO_IFSR; // Input Filter Status Register + AT91_REG Reserved2[1]; // + AT91_REG PIO_SODR; // Set Output Data Register + AT91_REG PIO_CODR; // Clear Output Data Register + AT91_REG PIO_ODSR; // Output Data Status Register + AT91_REG PIO_PDSR; // Pin Data Status Register + AT91_REG PIO_IER; // Interrupt Enable Register + AT91_REG PIO_IDR; // Interrupt Disable Register + AT91_REG PIO_IMR; // Interrupt Mask Register + AT91_REG PIO_ISR; // Interrupt Status Register + AT91_REG PIO_MDER; // Multi-driver Enable Register + AT91_REG PIO_MDDR; // Multi-driver Disable Register + AT91_REG PIO_MDSR; // Multi-driver Status Register + AT91_REG Reserved3[1]; // + AT91_REG PIO_PPUDR; // Pull-up Disable Register + AT91_REG PIO_PPUER; // Pull-up Enable Register + AT91_REG PIO_PPUSR; // Pull-up Status Register + AT91_REG Reserved4[1]; // + AT91_REG PIO_ASR; // Select A Register + AT91_REG PIO_BSR; // Select B Register + AT91_REG PIO_ABSR; // AB Select Status Register + AT91_REG Reserved5[9]; // + AT91_REG PIO_OWER; // Output Write Enable Register + AT91_REG PIO_OWDR; // Output Write Disable Register + AT91_REG PIO_OWSR; // Output Write Status Register +} AT91S_PIO, + *AT91PS_PIO; // ***************************************************************************** // SOFTWARE API DEFINITION FOR Clock Generator Controler // ***************************************************************************** typedef struct _AT91S_CKGR { - AT91_REG CKGR_MOR; // Main Oscillator Register - AT91_REG CKGR_MCFR; // Main Clock Frequency Register - AT91_REG Reserved0[1]; // - AT91_REG CKGR_PLLR; // PLL Register -} AT91S_CKGR, *AT91PS_CKGR; + AT91_REG CKGR_MOR; // Main Oscillator Register + AT91_REG CKGR_MCFR; // Main Clock Frequency Register + AT91_REG Reserved0[1]; // + AT91_REG CKGR_PLLR; // PLL Register +} AT91S_CKGR, + *AT91PS_CKGR; // -------- CKGR_MOR : (CKGR Offset: 0x0) Main Oscillator Register -------- -#define AT91C_CKGR_MOSCEN ((unsigned int) 0x1 << 0) // (CKGR) Main Oscillator Enable -#define AT91C_CKGR_OSCBYPASS ((unsigned int) 0x1 << 1) // (CKGR) Main Oscillator Bypass -#define AT91C_CKGR_OSCOUNT ((unsigned int) 0xFF << 8) // (CKGR) Main Oscillator Start-up Time +#define AT91C_CKGR_MOSCEN ((unsigned int) 0x1 << 0) // (CKGR) Main Oscillator Enable +#define AT91C_CKGR_OSCBYPASS ((unsigned int) 0x1 << 1) // (CKGR) Main Oscillator Bypass +#define AT91C_CKGR_OSCOUNT ((unsigned int) 0xFF << 8) // (CKGR) Main Oscillator Start-up Time // -------- CKGR_MCFR : (CKGR Offset: 0x4) Main Clock Frequency Register -------- -#define AT91C_CKGR_MAINF ((unsigned int) 0xFFFF << 0) // (CKGR) Main Clock Frequency -#define AT91C_CKGR_MAINRDY ((unsigned int) 0x1 << 16) // (CKGR) Main Clock Ready +#define AT91C_CKGR_MAINF ((unsigned int) 0xFFFF << 0) // (CKGR) Main Clock Frequency +#define AT91C_CKGR_MAINRDY ((unsigned int) 0x1 << 16) // (CKGR) Main Clock Ready // -------- CKGR_PLLR : (CKGR Offset: 0xc) PLL B Register -------- -#define AT91C_CKGR_DIV ((unsigned int) 0xFF << 0) // (CKGR) Divider Selected -#define AT91C_CKGR_DIV_0 ((unsigned int) 0x0) // (CKGR) Divider output is 0 -#define AT91C_CKGR_DIV_BYPASS ((unsigned int) 0x1) // (CKGR) Divider is bypassed -#define AT91C_CKGR_PLLCOUNT ((unsigned int) 0x3F << 8) // (CKGR) PLL Counter -#define AT91C_CKGR_OUT ((unsigned int) 0x3 << 14) // (CKGR) PLL Output Frequency Range -#define AT91C_CKGR_OUT_0 ((unsigned int) 0x0 << 14) // (CKGR) Please refer to the PLL datasheet -#define AT91C_CKGR_OUT_1 ((unsigned int) 0x1 << 14) // (CKGR) Please refer to the PLL datasheet -#define AT91C_CKGR_OUT_2 ((unsigned int) 0x2 << 14) // (CKGR) Please refer to the PLL datasheet -#define AT91C_CKGR_OUT_3 ((unsigned int) 0x3 << 14) // (CKGR) Please refer to the PLL datasheet -#define AT91C_CKGR_MUL ((unsigned int) 0x7FF << 16) // (CKGR) PLL Multiplier -#define AT91C_CKGR_USBDIV ((unsigned int) 0x3 << 28) // (CKGR) Divider for USB Clocks -#define AT91C_CKGR_USBDIV_0 ((unsigned int) 0x0 << 28) // (CKGR) Divider output is PLL clock output -#define AT91C_CKGR_USBDIV_1 ((unsigned int) 0x1 << 28) // (CKGR) Divider output is PLL clock output divided by 2 -#define AT91C_CKGR_USBDIV_2 ((unsigned int) 0x2 << 28) // (CKGR) Divider output is PLL clock output divided by 4 +#define AT91C_CKGR_DIV ((unsigned int) 0xFF << 0) // (CKGR) Divider Selected +#define AT91C_CKGR_DIV_0 ((unsigned int) 0x0) // (CKGR) Divider output is 0 +#define AT91C_CKGR_DIV_BYPASS ((unsigned int) 0x1) // (CKGR) Divider is bypassed +#define AT91C_CKGR_PLLCOUNT ((unsigned int) 0x3F << 8) // (CKGR) PLL Counter +#define AT91C_CKGR_OUT ((unsigned int) 0x3 << 14) // (CKGR) PLL Output Frequency Range +#define AT91C_CKGR_OUT_0 ((unsigned int) 0x0 << 14) // (CKGR) Please refer to the PLL datasheet +#define AT91C_CKGR_OUT_1 ((unsigned int) 0x1 << 14) // (CKGR) Please refer to the PLL datasheet +#define AT91C_CKGR_OUT_2 ((unsigned int) 0x2 << 14) // (CKGR) Please refer to the PLL datasheet +#define AT91C_CKGR_OUT_3 ((unsigned int) 0x3 << 14) // (CKGR) Please refer to the PLL datasheet +#define AT91C_CKGR_MUL ((unsigned int) 0x7FF << 16) // (CKGR) PLL Multiplier +#define AT91C_CKGR_USBDIV ((unsigned int) 0x3 << 28) // (CKGR) Divider for USB Clocks +#define AT91C_CKGR_USBDIV_0 ((unsigned int) 0x0 << 28) // (CKGR) Divider output is PLL clock output +#define AT91C_CKGR_USBDIV_1 ((unsigned int) 0x1 << 28) // (CKGR) Divider output is PLL clock output divided by 2 +#define AT91C_CKGR_USBDIV_2 ((unsigned int) 0x2 << 28) // (CKGR) Divider output is PLL clock output divided by 4 // ***************************************************************************** // SOFTWARE API DEFINITION FOR Power Management Controler // ***************************************************************************** typedef struct _AT91S_PMC { - AT91_REG PMC_SCER; // System Clock Enable Register - AT91_REG PMC_SCDR; // System Clock Disable Register - AT91_REG PMC_SCSR; // System Clock Status Register - AT91_REG Reserved0[1]; // - AT91_REG PMC_PCER; // Peripheral Clock Enable Register - AT91_REG PMC_PCDR; // Peripheral Clock Disable Register - AT91_REG PMC_PCSR; // Peripheral Clock Status Register - AT91_REG Reserved1[1]; // - AT91_REG PMC_MOR; // Main Oscillator Register - AT91_REG PMC_MCFR; // Main Clock Frequency Register - AT91_REG Reserved2[1]; // - AT91_REG PMC_PLLR; // PLL Register - AT91_REG PMC_MCKR; // Master Clock Register - AT91_REG Reserved3[3]; // - AT91_REG PMC_PCKR[3]; // Programmable Clock Register - AT91_REG Reserved4[5]; // - AT91_REG PMC_IER; // Interrupt Enable Register - AT91_REG PMC_IDR; // Interrupt Disable Register - AT91_REG PMC_SR; // Status Register - AT91_REG PMC_IMR; // Interrupt Mask Register -} AT91S_PMC, *AT91PS_PMC; + AT91_REG PMC_SCER; // System Clock Enable Register + AT91_REG PMC_SCDR; // System Clock Disable Register + AT91_REG PMC_SCSR; // System Clock Status Register + AT91_REG Reserved0[1]; // + AT91_REG PMC_PCER; // Peripheral Clock Enable Register + AT91_REG PMC_PCDR; // Peripheral Clock Disable Register + AT91_REG PMC_PCSR; // Peripheral Clock Status Register + AT91_REG Reserved1[1]; // + AT91_REG PMC_MOR; // Main Oscillator Register + AT91_REG PMC_MCFR; // Main Clock Frequency Register + AT91_REG Reserved2[1]; // + AT91_REG PMC_PLLR; // PLL Register + AT91_REG PMC_MCKR; // Master Clock Register + AT91_REG Reserved3[3]; // + AT91_REG PMC_PCKR[3]; // Programmable Clock Register + AT91_REG Reserved4[5]; // + AT91_REG PMC_IER; // Interrupt Enable Register + AT91_REG PMC_IDR; // Interrupt Disable Register + AT91_REG PMC_SR; // Status Register + AT91_REG PMC_IMR; // Interrupt Mask Register +} AT91S_PMC, + *AT91PS_PMC; // -------- PMC_SCER : (PMC Offset: 0x0) System Clock Enable Register -------- -#define AT91C_PMC_PCK ((unsigned int) 0x1 << 0) // (PMC) Processor Clock -#define AT91C_PMC_UDP ((unsigned int) 0x1 << 7) // (PMC) USB Device Port Clock -#define AT91C_PMC_PCK0 ((unsigned int) 0x1 << 8) // (PMC) Programmable Clock Output -#define AT91C_PMC_PCK1 ((unsigned int) 0x1 << 9) // (PMC) Programmable Clock Output -#define AT91C_PMC_PCK2 ((unsigned int) 0x1 << 10) // (PMC) Programmable Clock Output +#define AT91C_PMC_PCK ((unsigned int) 0x1 << 0) // (PMC) Processor Clock +#define AT91C_PMC_UDP ((unsigned int) 0x1 << 7) // (PMC) USB Device Port Clock +#define AT91C_PMC_PCK0 ((unsigned int) 0x1 << 8) // (PMC) Programmable Clock Output +#define AT91C_PMC_PCK1 ((unsigned int) 0x1 << 9) // (PMC) Programmable Clock Output +#define AT91C_PMC_PCK2 ((unsigned int) 0x1 << 10) // (PMC) Programmable Clock Output // -------- PMC_SCDR : (PMC Offset: 0x4) System Clock Disable Register -------- // -------- PMC_SCSR : (PMC Offset: 0x8) System Clock Status Register -------- // -------- CKGR_MOR : (PMC Offset: 0x20) Main Oscillator Register -------- // -------- CKGR_MCFR : (PMC Offset: 0x24) Main Clock Frequency Register -------- // -------- CKGR_PLLR : (PMC Offset: 0x2c) PLL B Register -------- // -------- PMC_MCKR : (PMC Offset: 0x30) Master Clock Register -------- -#define AT91C_PMC_CSS ((unsigned int) 0x3 << 0) // (PMC) Programmable Clock Selection +#define AT91C_PMC_CSS ((unsigned int) 0x3 << 0) // (PMC) Programmable Clock Selection #define AT91C_PMC_CSS_SLOW_CLK ((unsigned int) 0x0) // (PMC) Slow Clock is selected #define AT91C_PMC_CSS_MAIN_CLK ((unsigned int) 0x1) // (PMC) Main Clock is selected #define AT91C_PMC_CSS_PLL_CLK ((unsigned int) 0x3) // (PMC) Clock from PLL is selected -#define AT91C_PMC_PRES ((unsigned int) 0x7 << 2) // (PMC) Programmable Clock Prescaler -#define AT91C_PMC_PRES_CLK ((unsigned int) 0x0 << 2) // (PMC) Selected clock -#define AT91C_PMC_PRES_CLK_2 ((unsigned int) 0x1 << 2) // (PMC) Selected clock divided by 2 -#define AT91C_PMC_PRES_CLK_4 ((unsigned int) 0x2 << 2) // (PMC) Selected clock divided by 4 -#define AT91C_PMC_PRES_CLK_8 ((unsigned int) 0x3 << 2) // (PMC) Selected clock divided by 8 -#define AT91C_PMC_PRES_CLK_16 ((unsigned int) 0x4 << 2) // (PMC) Selected clock divided by 16 -#define AT91C_PMC_PRES_CLK_32 ((unsigned int) 0x5 << 2) // (PMC) Selected clock divided by 32 -#define AT91C_PMC_PRES_CLK_64 ((unsigned int) 0x6 << 2) // (PMC) Selected clock divided by 64 +#define AT91C_PMC_PRES ((unsigned int) 0x7 << 2) // (PMC) Programmable Clock Prescaler +#define AT91C_PMC_PRES_CLK ((unsigned int) 0x0 << 2) // (PMC) Selected clock +#define AT91C_PMC_PRES_CLK_2 ((unsigned int) 0x1 << 2) // (PMC) Selected clock divided by 2 +#define AT91C_PMC_PRES_CLK_4 ((unsigned int) 0x2 << 2) // (PMC) Selected clock divided by 4 +#define AT91C_PMC_PRES_CLK_8 ((unsigned int) 0x3 << 2) // (PMC) Selected clock divided by 8 +#define AT91C_PMC_PRES_CLK_16 ((unsigned int) 0x4 << 2) // (PMC) Selected clock divided by 16 +#define AT91C_PMC_PRES_CLK_32 ((unsigned int) 0x5 << 2) // (PMC) Selected clock divided by 32 +#define AT91C_PMC_PRES_CLK_64 ((unsigned int) 0x6 << 2) // (PMC) Selected clock divided by 64 // -------- PMC_PCKR : (PMC Offset: 0x40) Programmable Clock Register -------- // -------- PMC_IER : (PMC Offset: 0x60) PMC Interrupt Enable Register -------- -#define AT91C_PMC_MOSCS ((unsigned int) 0x1 << 0) // (PMC) MOSC Status/Enable/Disable/Mask -#define AT91C_PMC_LOCK ((unsigned int) 0x1 << 2) // (PMC) PLL Status/Enable/Disable/Mask -#define AT91C_PMC_MCKRDY ((unsigned int) 0x1 << 3) // (PMC) MCK_RDY Status/Enable/Disable/Mask -#define AT91C_PMC_PCK0RDY ((unsigned int) 0x1 << 8) // (PMC) PCK0_RDY Status/Enable/Disable/Mask -#define AT91C_PMC_PCK1RDY ((unsigned int) 0x1 << 9) // (PMC) PCK1_RDY Status/Enable/Disable/Mask -#define AT91C_PMC_PCK2RDY ((unsigned int) 0x1 << 10) // (PMC) PCK2_RDY Status/Enable/Disable/Mask +#define AT91C_PMC_MOSCS ((unsigned int) 0x1 << 0) // (PMC) MOSC Status/Enable/Disable/Mask +#define AT91C_PMC_LOCK ((unsigned int) 0x1 << 2) // (PMC) PLL Status/Enable/Disable/Mask +#define AT91C_PMC_MCKRDY ((unsigned int) 0x1 << 3) // (PMC) MCK_RDY Status/Enable/Disable/Mask +#define AT91C_PMC_PCK0RDY ((unsigned int) 0x1 << 8) // (PMC) PCK0_RDY Status/Enable/Disable/Mask +#define AT91C_PMC_PCK1RDY ((unsigned int) 0x1 << 9) // (PMC) PCK1_RDY Status/Enable/Disable/Mask +#define AT91C_PMC_PCK2RDY ((unsigned int) 0x1 << 10) // (PMC) PCK2_RDY Status/Enable/Disable/Mask // -------- PMC_IDR : (PMC Offset: 0x64) PMC Interrupt Disable Register -------- // -------- PMC_SR : (PMC Offset: 0x68) PMC Status Register -------- // -------- PMC_IMR : (PMC Offset: 0x6c) PMC Interrupt Mask Register -------- @@ -447,156 +454,162 @@ typedef struct _AT91S_PMC { // SOFTWARE API DEFINITION FOR Reset Controller Interface // ***************************************************************************** typedef struct _AT91S_RSTC { - AT91_REG RSTC_RCR; // Reset Control Register - AT91_REG RSTC_RSR; // Reset Status Register - AT91_REG RSTC_RMR; // Reset Mode Register -} AT91S_RSTC, *AT91PS_RSTC; + AT91_REG RSTC_RCR; // Reset Control Register + AT91_REG RSTC_RSR; // Reset Status Register + AT91_REG RSTC_RMR; // Reset Mode Register +} AT91S_RSTC, + *AT91PS_RSTC; // -------- RSTC_RCR : (RSTC Offset: 0x0) Reset Control Register -------- -#define AT91C_RSTC_PROCRST ((unsigned int) 0x1 << 0) // (RSTC) Processor Reset -#define AT91C_RSTC_PERRST ((unsigned int) 0x1 << 2) // (RSTC) Peripheral Reset -#define AT91C_RSTC_EXTRST ((unsigned int) 0x1 << 3) // (RSTC) External Reset -#define AT91C_RSTC_KEY ((unsigned int) 0xFF << 24) // (RSTC) Password +#define AT91C_RSTC_PROCRST ((unsigned int) 0x1 << 0) // (RSTC) Processor Reset +#define AT91C_RSTC_PERRST ((unsigned int) 0x1 << 2) // (RSTC) Peripheral Reset +#define AT91C_RSTC_EXTRST ((unsigned int) 0x1 << 3) // (RSTC) External Reset +#define AT91C_RSTC_KEY ((unsigned int) 0xFF << 24) // (RSTC) Password // -------- RSTC_RSR : (RSTC Offset: 0x4) Reset Status Register -------- -#define AT91C_RSTC_URSTS ((unsigned int) 0x1 << 0) // (RSTC) User Reset Status -#define AT91C_RSTC_BODSTS ((unsigned int) 0x1 << 1) // (RSTC) Brownout Detection Status -#define AT91C_RSTC_RSTTYP ((unsigned int) 0x7 << 8) // (RSTC) Reset Type -#define AT91C_RSTC_RSTTYP_POWERUP ((unsigned int) 0x0 << 8) // (RSTC) Power-up Reset. VDDCORE rising. -#define AT91C_RSTC_RSTTYP_WAKEUP ((unsigned int) 0x1 << 8) // (RSTC) WakeUp Reset. VDDCORE rising. -#define AT91C_RSTC_RSTTYP_WATCHDOG ((unsigned int) 0x2 << 8) // (RSTC) Watchdog Reset. Watchdog overflow occured. -#define AT91C_RSTC_RSTTYP_SOFTWARE ((unsigned int) 0x3 << 8) // (RSTC) Software Reset. Processor reset required by the software. -#define AT91C_RSTC_RSTTYP_USER ((unsigned int) 0x4 << 8) // (RSTC) User Reset. NRST pin detected low. -#define AT91C_RSTC_RSTTYP_BROWNOUT ((unsigned int) 0x5 << 8) // (RSTC) Brownout Reset occured. -#define AT91C_RSTC_NRSTL ((unsigned int) 0x1 << 16) // (RSTC) NRST pin level -#define AT91C_RSTC_SRCMP ((unsigned int) 0x1 << 17) // (RSTC) Software Reset Command in Progress. +#define AT91C_RSTC_URSTS ((unsigned int) 0x1 << 0) // (RSTC) User Reset Status +#define AT91C_RSTC_BODSTS ((unsigned int) 0x1 << 1) // (RSTC) Brownout Detection Status +#define AT91C_RSTC_RSTTYP ((unsigned int) 0x7 << 8) // (RSTC) Reset Type +#define AT91C_RSTC_RSTTYP_POWERUP ((unsigned int) 0x0 << 8) // (RSTC) Power-up Reset. VDDCORE rising. +#define AT91C_RSTC_RSTTYP_WAKEUP ((unsigned int) 0x1 << 8) // (RSTC) WakeUp Reset. VDDCORE rising. +#define AT91C_RSTC_RSTTYP_WATCHDOG ((unsigned int) 0x2 << 8) // (RSTC) Watchdog Reset. Watchdog overflow occured. +#define AT91C_RSTC_RSTTYP_SOFTWARE ((unsigned int) 0x3 << 8) // (RSTC) Software Reset. Processor reset required by the software. +#define AT91C_RSTC_RSTTYP_USER ((unsigned int) 0x4 << 8) // (RSTC) User Reset. NRST pin detected low. +#define AT91C_RSTC_RSTTYP_BROWNOUT ((unsigned int) 0x5 << 8) // (RSTC) Brownout Reset occured. +#define AT91C_RSTC_NRSTL ((unsigned int) 0x1 << 16) // (RSTC) NRST pin level +#define AT91C_RSTC_SRCMP ((unsigned int) 0x1 << 17) // (RSTC) Software Reset Command in Progress. // -------- RSTC_RMR : (RSTC Offset: 0x8) Reset Mode Register -------- -#define AT91C_RSTC_URSTEN ((unsigned int) 0x1 << 0) // (RSTC) User Reset Enable -#define AT91C_RSTC_URSTIEN ((unsigned int) 0x1 << 4) // (RSTC) User Reset Interrupt Enable -#define AT91C_RSTC_ERSTL ((unsigned int) 0xF << 8) // (RSTC) User Reset Enable -#define AT91C_RSTC_BODIEN ((unsigned int) 0x1 << 16) // (RSTC) Brownout Detection Interrupt Enable +#define AT91C_RSTC_URSTEN ((unsigned int) 0x1 << 0) // (RSTC) User Reset Enable +#define AT91C_RSTC_URSTIEN ((unsigned int) 0x1 << 4) // (RSTC) User Reset Interrupt Enable +#define AT91C_RSTC_ERSTL ((unsigned int) 0xF << 8) // (RSTC) User Reset Enable +#define AT91C_RSTC_BODIEN ((unsigned int) 0x1 << 16) // (RSTC) Brownout Detection Interrupt Enable // ***************************************************************************** // SOFTWARE API DEFINITION FOR Real Time Timer Controller Interface // ***************************************************************************** typedef struct _AT91S_RTTC { - AT91_REG RTTC_RTMR; // Real-time Mode Register - AT91_REG RTTC_RTAR; // Real-time Alarm Register - AT91_REG RTTC_RTVR; // Real-time Value Register - AT91_REG RTTC_RTSR; // Real-time Status Register -} AT91S_RTTC, *AT91PS_RTTC; + AT91_REG RTTC_RTMR; // Real-time Mode Register + AT91_REG RTTC_RTAR; // Real-time Alarm Register + AT91_REG RTTC_RTVR; // Real-time Value Register + AT91_REG RTTC_RTSR; // Real-time Status Register +} AT91S_RTTC, + *AT91PS_RTTC; // -------- RTTC_RTMR : (RTTC Offset: 0x0) Real-time Mode Register -------- -#define AT91C_RTTC_RTPRES ((unsigned int) 0xFFFF << 0) // (RTTC) Real-time Timer Prescaler Value -#define AT91C_RTTC_ALMIEN ((unsigned int) 0x1 << 16) // (RTTC) Alarm Interrupt Enable -#define AT91C_RTTC_RTTINCIEN ((unsigned int) 0x1 << 17) // (RTTC) Real Time Timer Increment Interrupt Enable -#define AT91C_RTTC_RTTRST ((unsigned int) 0x1 << 18) // (RTTC) Real Time Timer Restart +#define AT91C_RTTC_RTPRES ((unsigned int) 0xFFFF << 0) // (RTTC) Real-time Timer Prescaler Value +#define AT91C_RTTC_ALMIEN ((unsigned int) 0x1 << 16) // (RTTC) Alarm Interrupt Enable +#define AT91C_RTTC_RTTINCIEN ((unsigned int) 0x1 << 17) // (RTTC) Real Time Timer Increment Interrupt Enable +#define AT91C_RTTC_RTTRST ((unsigned int) 0x1 << 18) // (RTTC) Real Time Timer Restart // -------- RTTC_RTAR : (RTTC Offset: 0x4) Real-time Alarm Register -------- -#define AT91C_RTTC_ALMV ((unsigned int) 0x0 << 0) // (RTTC) Alarm Value +#define AT91C_RTTC_ALMV ((unsigned int) 0x0 << 0) // (RTTC) Alarm Value // -------- RTTC_RTVR : (RTTC Offset: 0x8) Current Real-time Value Register -------- -#define AT91C_RTTC_CRTV ((unsigned int) 0x0 << 0) // (RTTC) Current Real-time Value +#define AT91C_RTTC_CRTV ((unsigned int) 0x0 << 0) // (RTTC) Current Real-time Value // -------- RTTC_RTSR : (RTTC Offset: 0xc) Real-time Status Register -------- -#define AT91C_RTTC_ALMS ((unsigned int) 0x1 << 0) // (RTTC) Real-time Alarm Status -#define AT91C_RTTC_RTTINC ((unsigned int) 0x1 << 1) // (RTTC) Real-time Timer Increment +#define AT91C_RTTC_ALMS ((unsigned int) 0x1 << 0) // (RTTC) Real-time Alarm Status +#define AT91C_RTTC_RTTINC ((unsigned int) 0x1 << 1) // (RTTC) Real-time Timer Increment // ***************************************************************************** // SOFTWARE API DEFINITION FOR Periodic Interval Timer Controller Interface // ***************************************************************************** typedef struct _AT91S_PITC { - AT91_REG PITC_PIMR; // Period Interval Mode Register - AT91_REG PITC_PISR; // Period Interval Status Register - AT91_REG PITC_PIVR; // Period Interval Value Register - AT91_REG PITC_PIIR; // Period Interval Image Register -} AT91S_PITC, *AT91PS_PITC; + AT91_REG PITC_PIMR; // Period Interval Mode Register + AT91_REG PITC_PISR; // Period Interval Status Register + AT91_REG PITC_PIVR; // Period Interval Value Register + AT91_REG PITC_PIIR; // Period Interval Image Register +} AT91S_PITC, + *AT91PS_PITC; // -------- PITC_PIMR : (PITC Offset: 0x0) Periodic Interval Mode Register -------- -#define AT91C_PITC_PIV ((unsigned int) 0xFFFFF << 0) // (PITC) Periodic Interval Value -#define AT91C_PITC_PITEN ((unsigned int) 0x1 << 24) // (PITC) Periodic Interval Timer Enabled -#define AT91C_PITC_PITIEN ((unsigned int) 0x1 << 25) // (PITC) Periodic Interval Timer Interrupt Enable +#define AT91C_PITC_PIV ((unsigned int) 0xFFFFF << 0) // (PITC) Periodic Interval Value +#define AT91C_PITC_PITEN ((unsigned int) 0x1 << 24) // (PITC) Periodic Interval Timer Enabled +#define AT91C_PITC_PITIEN ((unsigned int) 0x1 << 25) // (PITC) Periodic Interval Timer Interrupt Enable // -------- PITC_PISR : (PITC Offset: 0x4) Periodic Interval Status Register -------- -#define AT91C_PITC_PITS ((unsigned int) 0x1 << 0) // (PITC) Periodic Interval Timer Status +#define AT91C_PITC_PITS ((unsigned int) 0x1 << 0) // (PITC) Periodic Interval Timer Status // -------- PITC_PIVR : (PITC Offset: 0x8) Periodic Interval Value Register -------- -#define AT91C_PITC_CPIV ((unsigned int) 0xFFFFF << 0) // (PITC) Current Periodic Interval Value -#define AT91C_PITC_PICNT ((unsigned int) 0xFFF << 20) // (PITC) Periodic Interval Counter +#define AT91C_PITC_CPIV ((unsigned int) 0xFFFFF << 0) // (PITC) Current Periodic Interval Value +#define AT91C_PITC_PICNT ((unsigned int) 0xFFF << 20) // (PITC) Periodic Interval Counter // -------- PITC_PIIR : (PITC Offset: 0xc) Periodic Interval Image Register -------- // ***************************************************************************** // SOFTWARE API DEFINITION FOR Watchdog Timer Controller Interface // ***************************************************************************** typedef struct _AT91S_WDTC { - AT91_REG WDTC_WDCR; // Watchdog Control Register - AT91_REG WDTC_WDMR; // Watchdog Mode Register - AT91_REG WDTC_WDSR; // Watchdog Status Register -} AT91S_WDTC, *AT91PS_WDTC; + AT91_REG WDTC_WDCR; // Watchdog Control Register + AT91_REG WDTC_WDMR; // Watchdog Mode Register + AT91_REG WDTC_WDSR; // Watchdog Status Register +} AT91S_WDTC, + *AT91PS_WDTC; // -------- WDTC_WDCR : (WDTC Offset: 0x0) Periodic Interval Image Register -------- -#define AT91C_WDTC_WDRSTT ((unsigned int) 0x1 << 0) // (WDTC) Watchdog Restart -#define AT91C_WDTC_KEY ((unsigned int) 0xFF << 24) // (WDTC) Watchdog KEY Password +#define AT91C_WDTC_WDRSTT ((unsigned int) 0x1 << 0) // (WDTC) Watchdog Restart +#define AT91C_WDTC_KEY ((unsigned int) 0xFF << 24) // (WDTC) Watchdog KEY Password // -------- WDTC_WDMR : (WDTC Offset: 0x4) Watchdog Mode Register -------- -#define AT91C_WDTC_WDV ((unsigned int) 0xFFF << 0) // (WDTC) Watchdog Timer Restart -#define AT91C_WDTC_WDFIEN ((unsigned int) 0x1 << 12) // (WDTC) Watchdog Fault Interrupt Enable -#define AT91C_WDTC_WDRSTEN ((unsigned int) 0x1 << 13) // (WDTC) Watchdog Reset Enable -#define AT91C_WDTC_WDRPROC ((unsigned int) 0x1 << 14) // (WDTC) Watchdog Timer Restart -#define AT91C_WDTC_WDDIS ((unsigned int) 0x1 << 15) // (WDTC) Watchdog Disable -#define AT91C_WDTC_WDD ((unsigned int) 0xFFF << 16) // (WDTC) Watchdog Delta Value -#define AT91C_WDTC_WDDBGHLT ((unsigned int) 0x1 << 28) // (WDTC) Watchdog Debug Halt -#define AT91C_WDTC_WDIDLEHLT ((unsigned int) 0x1 << 29) // (WDTC) Watchdog Idle Halt +#define AT91C_WDTC_WDV ((unsigned int) 0xFFF << 0) // (WDTC) Watchdog Timer Restart +#define AT91C_WDTC_WDFIEN ((unsigned int) 0x1 << 12) // (WDTC) Watchdog Fault Interrupt Enable +#define AT91C_WDTC_WDRSTEN ((unsigned int) 0x1 << 13) // (WDTC) Watchdog Reset Enable +#define AT91C_WDTC_WDRPROC ((unsigned int) 0x1 << 14) // (WDTC) Watchdog Timer Restart +#define AT91C_WDTC_WDDIS ((unsigned int) 0x1 << 15) // (WDTC) Watchdog Disable +#define AT91C_WDTC_WDD ((unsigned int) 0xFFF << 16) // (WDTC) Watchdog Delta Value +#define AT91C_WDTC_WDDBGHLT ((unsigned int) 0x1 << 28) // (WDTC) Watchdog Debug Halt +#define AT91C_WDTC_WDIDLEHLT ((unsigned int) 0x1 << 29) // (WDTC) Watchdog Idle Halt // -------- WDTC_WDSR : (WDTC Offset: 0x8) Watchdog Status Register -------- -#define AT91C_WDTC_WDUNF ((unsigned int) 0x1 << 0) // (WDTC) Watchdog Underflow -#define AT91C_WDTC_WDERR ((unsigned int) 0x1 << 1) // (WDTC) Watchdog Error +#define AT91C_WDTC_WDUNF ((unsigned int) 0x1 << 0) // (WDTC) Watchdog Underflow +#define AT91C_WDTC_WDERR ((unsigned int) 0x1 << 1) // (WDTC) Watchdog Error // ***************************************************************************** // SOFTWARE API DEFINITION FOR Voltage Regulator Mode Controller Interface // ***************************************************************************** typedef struct _AT91S_VREG { - AT91_REG VREG_MR; // Voltage Regulator Mode Register -} AT91S_VREG, *AT91PS_VREG; + AT91_REG VREG_MR; // Voltage Regulator Mode Register +} AT91S_VREG, + *AT91PS_VREG; // -------- VREG_MR : (VREG Offset: 0x0) Voltage Regulator Mode Register -------- -#define AT91C_VREG_PSTDBY ((unsigned int) 0x1 << 0) // (VREG) Voltage Regulator Power Standby Mode +#define AT91C_VREG_PSTDBY ((unsigned int) 0x1 << 0) // (VREG) Voltage Regulator Power Standby Mode // ***************************************************************************** // SOFTWARE API DEFINITION FOR Memory Controller Interface // ***************************************************************************** typedef struct _AT91S_MC { - AT91_REG MC_RCR; // MC Remap Control Register - AT91_REG MC_ASR; // MC Abort Status Register - AT91_REG MC_AASR; // MC Abort Address Status Register - AT91_REG Reserved0[21]; // - AT91_REG MC_FMR; // MC Flash Mode Register - AT91_REG MC_FCR; // MC Flash Command Register - AT91_REG MC_FSR; // MC Flash Status Register -} AT91S_MC, *AT91PS_MC; + AT91_REG MC_RCR; // MC Remap Control Register + AT91_REG MC_ASR; // MC Abort Status Register + AT91_REG MC_AASR; // MC Abort Address Status Register + AT91_REG Reserved0[21]; // + AT91_REG MC_FMR; // MC Flash Mode Register + AT91_REG MC_FCR; // MC Flash Command Register + AT91_REG MC_FSR; // MC Flash Status Register +} AT91S_MC, + *AT91PS_MC; // -------- MC_RCR : (MC Offset: 0x0) MC Remap Control Register -------- -#define AT91C_MC_RCB ((unsigned int) 0x1 << 0) // (MC) Remap Command Bit +#define AT91C_MC_RCB ((unsigned int) 0x1 << 0) // (MC) Remap Command Bit // -------- MC_ASR : (MC Offset: 0x4) MC Abort Status Register -------- -#define AT91C_MC_UNDADD ((unsigned int) 0x1 << 0) // (MC) Undefined Addess Abort Status -#define AT91C_MC_MISADD ((unsigned int) 0x1 << 1) // (MC) Misaligned Addess Abort Status -#define AT91C_MC_ABTSZ ((unsigned int) 0x3 << 8) // (MC) Abort Size Status -#define AT91C_MC_ABTSZ_BYTE ((unsigned int) 0x0 << 8) // (MC) Byte -#define AT91C_MC_ABTSZ_HWORD ((unsigned int) 0x1 << 8) // (MC) Half-word -#define AT91C_MC_ABTSZ_WORD ((unsigned int) 0x2 << 8) // (MC) Word -#define AT91C_MC_ABTTYP ((unsigned int) 0x3 << 10) // (MC) Abort Type Status +#define AT91C_MC_UNDADD ((unsigned int) 0x1 << 0) // (MC) Undefined Addess Abort Status +#define AT91C_MC_MISADD ((unsigned int) 0x1 << 1) // (MC) Misaligned Addess Abort Status +#define AT91C_MC_ABTSZ ((unsigned int) 0x3 << 8) // (MC) Abort Size Status +#define AT91C_MC_ABTSZ_BYTE ((unsigned int) 0x0 << 8) // (MC) Byte +#define AT91C_MC_ABTSZ_HWORD ((unsigned int) 0x1 << 8) // (MC) Half-word +#define AT91C_MC_ABTSZ_WORD ((unsigned int) 0x2 << 8) // (MC) Word +#define AT91C_MC_ABTTYP ((unsigned int) 0x3 << 10) // (MC) Abort Type Status #define AT91C_MC_ABTTYP_DATAR ((unsigned int) 0x0 << 10) // (MC) Data Read #define AT91C_MC_ABTTYP_DATAW ((unsigned int) 0x1 << 10) // (MC) Data Write #define AT91C_MC_ABTTYP_FETCH ((unsigned int) 0x2 << 10) // (MC) Code Fetch -#define AT91C_MC_MST0 ((unsigned int) 0x1 << 16) // (MC) Master 0 Abort Source -#define AT91C_MC_MST1 ((unsigned int) 0x1 << 17) // (MC) Master 1 Abort Source -#define AT91C_MC_SVMST0 ((unsigned int) 0x1 << 24) // (MC) Saved Master 0 Abort Source -#define AT91C_MC_SVMST1 ((unsigned int) 0x1 << 25) // (MC) Saved Master 1 Abort Source +#define AT91C_MC_MST0 ((unsigned int) 0x1 << 16) // (MC) Master 0 Abort Source +#define AT91C_MC_MST1 ((unsigned int) 0x1 << 17) // (MC) Master 1 Abort Source +#define AT91C_MC_SVMST0 ((unsigned int) 0x1 << 24) // (MC) Saved Master 0 Abort Source +#define AT91C_MC_SVMST1 ((unsigned int) 0x1 << 25) // (MC) Saved Master 1 Abort Source // -------- MC_FMR : (MC Offset: 0x60) MC Flash Mode Register -------- -#define AT91C_MC_FRDY ((unsigned int) 0x1 << 0) // (MC) Flash Ready -#define AT91C_MC_LOCKE ((unsigned int) 0x1 << 2) // (MC) Lock Error -#define AT91C_MC_PROGE ((unsigned int) 0x1 << 3) // (MC) Programming Error -#define AT91C_MC_NEBP ((unsigned int) 0x1 << 7) // (MC) No Erase Before Programming -#define AT91C_MC_FWS ((unsigned int) 0x3 << 8) // (MC) Flash Wait State -#define AT91C_MC_FWS_0FWS ((unsigned int) 0x0 << 8) // (MC) 1 cycle for Read, 2 for Write operations -#define AT91C_MC_FWS_1FWS ((unsigned int) 0x1 << 8) // (MC) 2 cycles for Read, 3 for Write operations -#define AT91C_MC_FWS_2FWS ((unsigned int) 0x2 << 8) // (MC) 3 cycles for Read, 4 for Write operations -#define AT91C_MC_FWS_3FWS ((unsigned int) 0x3 << 8) // (MC) 4 cycles for Read, 4 for Write operations -#define AT91C_MC_FMCN ((unsigned int) 0xFF << 16) // (MC) Flash Microsecond Cycle Number +#define AT91C_MC_FRDY ((unsigned int) 0x1 << 0) // (MC) Flash Ready +#define AT91C_MC_LOCKE ((unsigned int) 0x1 << 2) // (MC) Lock Error +#define AT91C_MC_PROGE ((unsigned int) 0x1 << 3) // (MC) Programming Error +#define AT91C_MC_NEBP ((unsigned int) 0x1 << 7) // (MC) No Erase Before Programming +#define AT91C_MC_FWS ((unsigned int) 0x3 << 8) // (MC) Flash Wait State +#define AT91C_MC_FWS_0FWS ((unsigned int) 0x0 << 8) // (MC) 1 cycle for Read, 2 for Write operations +#define AT91C_MC_FWS_1FWS ((unsigned int) 0x1 << 8) // (MC) 2 cycles for Read, 3 for Write operations +#define AT91C_MC_FWS_2FWS ((unsigned int) 0x2 << 8) // (MC) 3 cycles for Read, 4 for Write operations +#define AT91C_MC_FWS_3FWS ((unsigned int) 0x3 << 8) // (MC) 4 cycles for Read, 4 for Write operations +#define AT91C_MC_FMCN ((unsigned int) 0xFF << 16) // (MC) Flash Microsecond Cycle Number // -------- MC_FCR : (MC Offset: 0x64) MC Flash Command Register -------- -#define AT91C_MC_FCMD ((unsigned int) 0xF << 0) // (MC) Flash Command +#define AT91C_MC_FCMD ((unsigned int) 0xF << 0) // (MC) Flash Command #define AT91C_MC_FCMD_START_PROG ((unsigned int) 0x1) // (MC) Starts the programming of th epage specified by PAGEN. #define AT91C_MC_FCMD_LOCK ((unsigned int) 0x2) // (MC) Starts a lock sequence of the sector defined by the bits 4 to 7 of the field PAGEN. #define AT91C_MC_FCMD_PROG_AND_LOCK ((unsigned int) 0x3) // (MC) The lock sequence automatically happens after the programming sequence is completed. @@ -605,216 +618,218 @@ typedef struct _AT91S_MC { #define AT91C_MC_FCMD_SET_GP_NVM ((unsigned int) 0xB) // (MC) Set General Purpose NVM bits. #define AT91C_MC_FCMD_CLR_GP_NVM ((unsigned int) 0xD) // (MC) Clear General Purpose NVM bits. #define AT91C_MC_FCMD_SET_SECURITY ((unsigned int) 0xF) // (MC) Set Security Bit. -#define AT91C_MC_PAGEN ((unsigned int) 0x3FF << 8) // (MC) Page Number -#define AT91C_MC_KEY ((unsigned int) 0xFF << 24) // (MC) Writing Protect Key +#define AT91C_MC_PAGEN ((unsigned int) 0x3FF << 8) // (MC) Page Number +#define AT91C_MC_KEY ((unsigned int) 0xFF << 24) // (MC) Writing Protect Key // -------- MC_FSR : (MC Offset: 0x68) MC Flash Command Register -------- -#define AT91C_MC_SECURITY ((unsigned int) 0x1 << 4) // (MC) Security Bit Status -#define AT91C_MC_GPNVM0 ((unsigned int) 0x1 << 8) // (MC) Sector 0 Lock Status -#define AT91C_MC_GPNVM1 ((unsigned int) 0x1 << 9) // (MC) Sector 1 Lock Status -#define AT91C_MC_GPNVM2 ((unsigned int) 0x1 << 10) // (MC) Sector 2 Lock Status -#define AT91C_MC_GPNVM3 ((unsigned int) 0x1 << 11) // (MC) Sector 3 Lock Status -#define AT91C_MC_GPNVM4 ((unsigned int) 0x1 << 12) // (MC) Sector 4 Lock Status -#define AT91C_MC_GPNVM5 ((unsigned int) 0x1 << 13) // (MC) Sector 5 Lock Status -#define AT91C_MC_GPNVM6 ((unsigned int) 0x1 << 14) // (MC) Sector 6 Lock Status -#define AT91C_MC_GPNVM7 ((unsigned int) 0x1 << 15) // (MC) Sector 7 Lock Status -#define AT91C_MC_LOCKS0 ((unsigned int) 0x1 << 16) // (MC) Sector 0 Lock Status -#define AT91C_MC_LOCKS1 ((unsigned int) 0x1 << 17) // (MC) Sector 1 Lock Status -#define AT91C_MC_LOCKS2 ((unsigned int) 0x1 << 18) // (MC) Sector 2 Lock Status -#define AT91C_MC_LOCKS3 ((unsigned int) 0x1 << 19) // (MC) Sector 3 Lock Status -#define AT91C_MC_LOCKS4 ((unsigned int) 0x1 << 20) // (MC) Sector 4 Lock Status -#define AT91C_MC_LOCKS5 ((unsigned int) 0x1 << 21) // (MC) Sector 5 Lock Status -#define AT91C_MC_LOCKS6 ((unsigned int) 0x1 << 22) // (MC) Sector 6 Lock Status -#define AT91C_MC_LOCKS7 ((unsigned int) 0x1 << 23) // (MC) Sector 7 Lock Status -#define AT91C_MC_LOCKS8 ((unsigned int) 0x1 << 24) // (MC) Sector 8 Lock Status -#define AT91C_MC_LOCKS9 ((unsigned int) 0x1 << 25) // (MC) Sector 9 Lock Status -#define AT91C_MC_LOCKS10 ((unsigned int) 0x1 << 26) // (MC) Sector 10 Lock Status -#define AT91C_MC_LOCKS11 ((unsigned int) 0x1 << 27) // (MC) Sector 11 Lock Status -#define AT91C_MC_LOCKS12 ((unsigned int) 0x1 << 28) // (MC) Sector 12 Lock Status -#define AT91C_MC_LOCKS13 ((unsigned int) 0x1 << 29) // (MC) Sector 13 Lock Status -#define AT91C_MC_LOCKS14 ((unsigned int) 0x1 << 30) // (MC) Sector 14 Lock Status -#define AT91C_MC_LOCKS15 ((unsigned int) 0x1 << 31) // (MC) Sector 15 Lock Status +#define AT91C_MC_SECURITY ((unsigned int) 0x1 << 4) // (MC) Security Bit Status +#define AT91C_MC_GPNVM0 ((unsigned int) 0x1 << 8) // (MC) Sector 0 Lock Status +#define AT91C_MC_GPNVM1 ((unsigned int) 0x1 << 9) // (MC) Sector 1 Lock Status +#define AT91C_MC_GPNVM2 ((unsigned int) 0x1 << 10) // (MC) Sector 2 Lock Status +#define AT91C_MC_GPNVM3 ((unsigned int) 0x1 << 11) // (MC) Sector 3 Lock Status +#define AT91C_MC_GPNVM4 ((unsigned int) 0x1 << 12) // (MC) Sector 4 Lock Status +#define AT91C_MC_GPNVM5 ((unsigned int) 0x1 << 13) // (MC) Sector 5 Lock Status +#define AT91C_MC_GPNVM6 ((unsigned int) 0x1 << 14) // (MC) Sector 6 Lock Status +#define AT91C_MC_GPNVM7 ((unsigned int) 0x1 << 15) // (MC) Sector 7 Lock Status +#define AT91C_MC_LOCKS0 ((unsigned int) 0x1 << 16) // (MC) Sector 0 Lock Status +#define AT91C_MC_LOCKS1 ((unsigned int) 0x1 << 17) // (MC) Sector 1 Lock Status +#define AT91C_MC_LOCKS2 ((unsigned int) 0x1 << 18) // (MC) Sector 2 Lock Status +#define AT91C_MC_LOCKS3 ((unsigned int) 0x1 << 19) // (MC) Sector 3 Lock Status +#define AT91C_MC_LOCKS4 ((unsigned int) 0x1 << 20) // (MC) Sector 4 Lock Status +#define AT91C_MC_LOCKS5 ((unsigned int) 0x1 << 21) // (MC) Sector 5 Lock Status +#define AT91C_MC_LOCKS6 ((unsigned int) 0x1 << 22) // (MC) Sector 6 Lock Status +#define AT91C_MC_LOCKS7 ((unsigned int) 0x1 << 23) // (MC) Sector 7 Lock Status +#define AT91C_MC_LOCKS8 ((unsigned int) 0x1 << 24) // (MC) Sector 8 Lock Status +#define AT91C_MC_LOCKS9 ((unsigned int) 0x1 << 25) // (MC) Sector 9 Lock Status +#define AT91C_MC_LOCKS10 ((unsigned int) 0x1 << 26) // (MC) Sector 10 Lock Status +#define AT91C_MC_LOCKS11 ((unsigned int) 0x1 << 27) // (MC) Sector 11 Lock Status +#define AT91C_MC_LOCKS12 ((unsigned int) 0x1 << 28) // (MC) Sector 12 Lock Status +#define AT91C_MC_LOCKS13 ((unsigned int) 0x1 << 29) // (MC) Sector 13 Lock Status +#define AT91C_MC_LOCKS14 ((unsigned int) 0x1 << 30) // (MC) Sector 14 Lock Status +#define AT91C_MC_LOCKS15 ((unsigned int) 0x1 << 31) // (MC) Sector 15 Lock Status // ***************************************************************************** // SOFTWARE API DEFINITION FOR Serial Parallel Interface // ***************************************************************************** typedef struct _AT91S_SPI { - AT91_REG SPI_CR; // Control Register - AT91_REG SPI_MR; // Mode Register - AT91_REG SPI_RDR; // Receive Data Register - AT91_REG SPI_TDR; // Transmit Data Register - AT91_REG SPI_SR; // Status Register - AT91_REG SPI_IER; // Interrupt Enable Register - AT91_REG SPI_IDR; // Interrupt Disable Register - AT91_REG SPI_IMR; // Interrupt Mask Register - AT91_REG Reserved0[4]; // - AT91_REG SPI_CSR[4]; // Chip Select Register - AT91_REG Reserved1[48]; // - AT91_REG SPI_RPR; // Receive Pointer Register - AT91_REG SPI_RCR; // Receive Counter Register - AT91_REG SPI_TPR; // Transmit Pointer Register - AT91_REG SPI_TCR; // Transmit Counter Register - AT91_REG SPI_RNPR; // Receive Next Pointer Register - AT91_REG SPI_RNCR; // Receive Next Counter Register - AT91_REG SPI_TNPR; // Transmit Next Pointer Register - AT91_REG SPI_TNCR; // Transmit Next Counter Register - AT91_REG SPI_PTCR; // PDC Transfer Control Register - AT91_REG SPI_PTSR; // PDC Transfer Status Register -} AT91S_SPI, *AT91PS_SPI; + AT91_REG SPI_CR; // Control Register + AT91_REG SPI_MR; // Mode Register + AT91_REG SPI_RDR; // Receive Data Register + AT91_REG SPI_TDR; // Transmit Data Register + AT91_REG SPI_SR; // Status Register + AT91_REG SPI_IER; // Interrupt Enable Register + AT91_REG SPI_IDR; // Interrupt Disable Register + AT91_REG SPI_IMR; // Interrupt Mask Register + AT91_REG Reserved0[4]; // + AT91_REG SPI_CSR[4]; // Chip Select Register + AT91_REG Reserved1[48]; // + AT91_REG SPI_RPR; // Receive Pointer Register + AT91_REG SPI_RCR; // Receive Counter Register + AT91_REG SPI_TPR; // Transmit Pointer Register + AT91_REG SPI_TCR; // Transmit Counter Register + AT91_REG SPI_RNPR; // Receive Next Pointer Register + AT91_REG SPI_RNCR; // Receive Next Counter Register + AT91_REG SPI_TNPR; // Transmit Next Pointer Register + AT91_REG SPI_TNCR; // Transmit Next Counter Register + AT91_REG SPI_PTCR; // PDC Transfer Control Register + AT91_REG SPI_PTSR; // PDC Transfer Status Register +} AT91S_SPI, + *AT91PS_SPI; // -------- SPI_CR : (SPI Offset: 0x0) SPI Control Register -------- -#define AT91C_SPI_SPIEN ((unsigned int) 0x1 << 0) // (SPI) SPI Enable -#define AT91C_SPI_SPIDIS ((unsigned int) 0x1 << 1) // (SPI) SPI Disable -#define AT91C_SPI_SWRST ((unsigned int) 0x1 << 7) // (SPI) SPI Software reset -#define AT91C_SPI_LASTXFER ((unsigned int) 0x1 << 24) // (SPI) SPI Last Transfer +#define AT91C_SPI_SPIEN ((unsigned int) 0x1 << 0) // (SPI) SPI Enable +#define AT91C_SPI_SPIDIS ((unsigned int) 0x1 << 1) // (SPI) SPI Disable +#define AT91C_SPI_SWRST ((unsigned int) 0x1 << 7) // (SPI) SPI Software reset +#define AT91C_SPI_LASTXFER ((unsigned int) 0x1 << 24) // (SPI) SPI Last Transfer // -------- SPI_MR : (SPI Offset: 0x4) SPI Mode Register -------- -#define AT91C_SPI_MSTR ((unsigned int) 0x1 << 0) // (SPI) Master/Slave Mode -#define AT91C_SPI_PS ((unsigned int) 0x1 << 1) // (SPI) Peripheral Select -#define AT91C_SPI_PS_FIXED ((unsigned int) 0x0 << 1) // (SPI) Fixed Peripheral Select -#define AT91C_SPI_PS_VARIABLE ((unsigned int) 0x1 << 1) // (SPI) Variable Peripheral Select -#define AT91C_SPI_PCSDEC ((unsigned int) 0x1 << 2) // (SPI) Chip Select Decode -#define AT91C_SPI_FDIV ((unsigned int) 0x1 << 3) // (SPI) Clock Selection -#define AT91C_SPI_MODFDIS ((unsigned int) 0x1 << 4) // (SPI) Mode Fault Detection -#define AT91C_SPI_LLB ((unsigned int) 0x1 << 7) // (SPI) Clock Selection -#define AT91C_SPI_PCS ((unsigned int) 0xF << 16) // (SPI) Peripheral Chip Select -#define AT91C_SPI_DLYBCS ((unsigned int) 0xFF << 24) // (SPI) Delay Between Chip Selects +#define AT91C_SPI_MSTR ((unsigned int) 0x1 << 0) // (SPI) Master/Slave Mode +#define AT91C_SPI_PS ((unsigned int) 0x1 << 1) // (SPI) Peripheral Select +#define AT91C_SPI_PS_FIXED ((unsigned int) 0x0 << 1) // (SPI) Fixed Peripheral Select +#define AT91C_SPI_PS_VARIABLE ((unsigned int) 0x1 << 1) // (SPI) Variable Peripheral Select +#define AT91C_SPI_PCSDEC ((unsigned int) 0x1 << 2) // (SPI) Chip Select Decode +#define AT91C_SPI_FDIV ((unsigned int) 0x1 << 3) // (SPI) Clock Selection +#define AT91C_SPI_MODFDIS ((unsigned int) 0x1 << 4) // (SPI) Mode Fault Detection +#define AT91C_SPI_LLB ((unsigned int) 0x1 << 7) // (SPI) Clock Selection +#define AT91C_SPI_PCS ((unsigned int) 0xF << 16) // (SPI) Peripheral Chip Select +#define AT91C_SPI_DLYBCS ((unsigned int) 0xFF << 24) // (SPI) Delay Between Chip Selects // -------- SPI_RDR : (SPI Offset: 0x8) Receive Data Register -------- -#define AT91C_SPI_RD ((unsigned int) 0xFFFF << 0) // (SPI) Receive Data -#define AT91C_SPI_RPCS ((unsigned int) 0xF << 16) // (SPI) Peripheral Chip Select Status +#define AT91C_SPI_RD ((unsigned int) 0xFFFF << 0) // (SPI) Receive Data +#define AT91C_SPI_RPCS ((unsigned int) 0xF << 16) // (SPI) Peripheral Chip Select Status // -------- SPI_TDR : (SPI Offset: 0xc) Transmit Data Register -------- -#define AT91C_SPI_TD ((unsigned int) 0xFFFF << 0) // (SPI) Transmit Data -#define AT91C_SPI_TPCS ((unsigned int) 0xF << 16) // (SPI) Peripheral Chip Select Status +#define AT91C_SPI_TD ((unsigned int) 0xFFFF << 0) // (SPI) Transmit Data +#define AT91C_SPI_TPCS ((unsigned int) 0xF << 16) // (SPI) Peripheral Chip Select Status // -------- SPI_SR : (SPI Offset: 0x10) Status Register -------- -#define AT91C_SPI_RDRF ((unsigned int) 0x1 << 0) // (SPI) Receive Data Register Full -#define AT91C_SPI_TDRE ((unsigned int) 0x1 << 1) // (SPI) Transmit Data Register Empty -#define AT91C_SPI_MODF ((unsigned int) 0x1 << 2) // (SPI) Mode Fault Error -#define AT91C_SPI_OVRES ((unsigned int) 0x1 << 3) // (SPI) Overrun Error Status -#define AT91C_SPI_ENDRX ((unsigned int) 0x1 << 4) // (SPI) End of Receiver Transfer -#define AT91C_SPI_ENDTX ((unsigned int) 0x1 << 5) // (SPI) End of Receiver Transfer -#define AT91C_SPI_RXBUFF ((unsigned int) 0x1 << 6) // (SPI) RXBUFF Interrupt -#define AT91C_SPI_TXBUFE ((unsigned int) 0x1 << 7) // (SPI) TXBUFE Interrupt -#define AT91C_SPI_NSSR ((unsigned int) 0x1 << 8) // (SPI) NSSR Interrupt -#define AT91C_SPI_TXEMPTY ((unsigned int) 0x1 << 9) // (SPI) TXEMPTY Interrupt -#define AT91C_SPI_SPIENS ((unsigned int) 0x1 << 16) // (SPI) Enable Status +#define AT91C_SPI_RDRF ((unsigned int) 0x1 << 0) // (SPI) Receive Data Register Full +#define AT91C_SPI_TDRE ((unsigned int) 0x1 << 1) // (SPI) Transmit Data Register Empty +#define AT91C_SPI_MODF ((unsigned int) 0x1 << 2) // (SPI) Mode Fault Error +#define AT91C_SPI_OVRES ((unsigned int) 0x1 << 3) // (SPI) Overrun Error Status +#define AT91C_SPI_ENDRX ((unsigned int) 0x1 << 4) // (SPI) End of Receiver Transfer +#define AT91C_SPI_ENDTX ((unsigned int) 0x1 << 5) // (SPI) End of Receiver Transfer +#define AT91C_SPI_RXBUFF ((unsigned int) 0x1 << 6) // (SPI) RXBUFF Interrupt +#define AT91C_SPI_TXBUFE ((unsigned int) 0x1 << 7) // (SPI) TXBUFE Interrupt +#define AT91C_SPI_NSSR ((unsigned int) 0x1 << 8) // (SPI) NSSR Interrupt +#define AT91C_SPI_TXEMPTY ((unsigned int) 0x1 << 9) // (SPI) TXEMPTY Interrupt +#define AT91C_SPI_SPIENS ((unsigned int) 0x1 << 16) // (SPI) Enable Status // -------- SPI_IER : (SPI Offset: 0x14) Interrupt Enable Register -------- // -------- SPI_IDR : (SPI Offset: 0x18) Interrupt Disable Register -------- // -------- SPI_IMR : (SPI Offset: 0x1c) Interrupt Mask Register -------- // -------- SPI_CSR : (SPI Offset: 0x30) Chip Select Register -------- -#define AT91C_SPI_CPOL ((unsigned int) 0x1 << 0) // (SPI) Clock Polarity -#define AT91C_SPI_NCPHA ((unsigned int) 0x1 << 1) // (SPI) Clock Phase -#define AT91C_SPI_CSAAT ((unsigned int) 0x1 << 3) // (SPI) Chip Select Active After Transfer -#define AT91C_SPI_BITS ((unsigned int) 0xF << 4) // (SPI) Bits Per Transfer -#define AT91C_SPI_BITS_8 ((unsigned int) 0x0 << 4) // (SPI) 8 Bits Per transfer -#define AT91C_SPI_BITS_9 ((unsigned int) 0x1 << 4) // (SPI) 9 Bits Per transfer -#define AT91C_SPI_BITS_10 ((unsigned int) 0x2 << 4) // (SPI) 10 Bits Per transfer -#define AT91C_SPI_BITS_11 ((unsigned int) 0x3 << 4) // (SPI) 11 Bits Per transfer -#define AT91C_SPI_BITS_12 ((unsigned int) 0x4 << 4) // (SPI) 12 Bits Per transfer -#define AT91C_SPI_BITS_13 ((unsigned int) 0x5 << 4) // (SPI) 13 Bits Per transfer -#define AT91C_SPI_BITS_14 ((unsigned int) 0x6 << 4) // (SPI) 14 Bits Per transfer -#define AT91C_SPI_BITS_15 ((unsigned int) 0x7 << 4) // (SPI) 15 Bits Per transfer -#define AT91C_SPI_BITS_16 ((unsigned int) 0x8 << 4) // (SPI) 16 Bits Per transfer -#define AT91C_SPI_SCBR ((unsigned int) 0xFF << 8) // (SPI) Serial Clock Baud Rate -#define AT91C_SPI_DLYBS ((unsigned int) 0xFF << 16) // (SPI) Serial Clock Baud Rate -#define AT91C_SPI_DLYBCT ((unsigned int) 0xFF << 24) // (SPI) Delay Between Consecutive Transfers +#define AT91C_SPI_CPOL ((unsigned int) 0x1 << 0) // (SPI) Clock Polarity +#define AT91C_SPI_NCPHA ((unsigned int) 0x1 << 1) // (SPI) Clock Phase +#define AT91C_SPI_CSAAT ((unsigned int) 0x1 << 3) // (SPI) Chip Select Active After Transfer +#define AT91C_SPI_BITS ((unsigned int) 0xF << 4) // (SPI) Bits Per Transfer +#define AT91C_SPI_BITS_8 ((unsigned int) 0x0 << 4) // (SPI) 8 Bits Per transfer +#define AT91C_SPI_BITS_9 ((unsigned int) 0x1 << 4) // (SPI) 9 Bits Per transfer +#define AT91C_SPI_BITS_10 ((unsigned int) 0x2 << 4) // (SPI) 10 Bits Per transfer +#define AT91C_SPI_BITS_11 ((unsigned int) 0x3 << 4) // (SPI) 11 Bits Per transfer +#define AT91C_SPI_BITS_12 ((unsigned int) 0x4 << 4) // (SPI) 12 Bits Per transfer +#define AT91C_SPI_BITS_13 ((unsigned int) 0x5 << 4) // (SPI) 13 Bits Per transfer +#define AT91C_SPI_BITS_14 ((unsigned int) 0x6 << 4) // (SPI) 14 Bits Per transfer +#define AT91C_SPI_BITS_15 ((unsigned int) 0x7 << 4) // (SPI) 15 Bits Per transfer +#define AT91C_SPI_BITS_16 ((unsigned int) 0x8 << 4) // (SPI) 16 Bits Per transfer +#define AT91C_SPI_SCBR ((unsigned int) 0xFF << 8) // (SPI) Serial Clock Baud Rate +#define AT91C_SPI_DLYBS ((unsigned int) 0xFF << 16) // (SPI) Serial Clock Baud Rate +#define AT91C_SPI_DLYBCT ((unsigned int) 0xFF << 24) // (SPI) Delay Between Consecutive Transfers // ***************************************************************************** // SOFTWARE API DEFINITION FOR Analog to Digital Convertor // ***************************************************************************** typedef struct _AT91S_ADC { - AT91_REG ADC_CR; // ADC Control Register - AT91_REG ADC_MR; // ADC Mode Register - AT91_REG Reserved0[2]; // - AT91_REG ADC_CHER; // ADC Channel Enable Register - AT91_REG ADC_CHDR; // ADC Channel Disable Register - AT91_REG ADC_CHSR; // ADC Channel Status Register - AT91_REG ADC_SR; // ADC Status Register - AT91_REG ADC_LCDR; // ADC Last Converted Data Register - AT91_REG ADC_IER; // ADC Interrupt Enable Register - AT91_REG ADC_IDR; // ADC Interrupt Disable Register - AT91_REG ADC_IMR; // ADC Interrupt Mask Register - AT91_REG ADC_CDR0; // ADC Channel Data Register 0 - AT91_REG ADC_CDR1; // ADC Channel Data Register 1 - AT91_REG ADC_CDR2; // ADC Channel Data Register 2 - AT91_REG ADC_CDR3; // ADC Channel Data Register 3 - AT91_REG ADC_CDR4; // ADC Channel Data Register 4 - AT91_REG ADC_CDR5; // ADC Channel Data Register 5 - AT91_REG ADC_CDR6; // ADC Channel Data Register 6 - AT91_REG ADC_CDR7; // ADC Channel Data Register 7 - AT91_REG Reserved1[44]; // - AT91_REG ADC_RPR; // Receive Pointer Register - AT91_REG ADC_RCR; // Receive Counter Register - AT91_REG ADC_TPR; // Transmit Pointer Register - AT91_REG ADC_TCR; // Transmit Counter Register - AT91_REG ADC_RNPR; // Receive Next Pointer Register - AT91_REG ADC_RNCR; // Receive Next Counter Register - AT91_REG ADC_TNPR; // Transmit Next Pointer Register - AT91_REG ADC_TNCR; // Transmit Next Counter Register - AT91_REG ADC_PTCR; // PDC Transfer Control Register - AT91_REG ADC_PTSR; // PDC Transfer Status Register -} AT91S_ADC, *AT91PS_ADC; + AT91_REG ADC_CR; // ADC Control Register + AT91_REG ADC_MR; // ADC Mode Register + AT91_REG Reserved0[2]; // + AT91_REG ADC_CHER; // ADC Channel Enable Register + AT91_REG ADC_CHDR; // ADC Channel Disable Register + AT91_REG ADC_CHSR; // ADC Channel Status Register + AT91_REG ADC_SR; // ADC Status Register + AT91_REG ADC_LCDR; // ADC Last Converted Data Register + AT91_REG ADC_IER; // ADC Interrupt Enable Register + AT91_REG ADC_IDR; // ADC Interrupt Disable Register + AT91_REG ADC_IMR; // ADC Interrupt Mask Register + AT91_REG ADC_CDR0; // ADC Channel Data Register 0 + AT91_REG ADC_CDR1; // ADC Channel Data Register 1 + AT91_REG ADC_CDR2; // ADC Channel Data Register 2 + AT91_REG ADC_CDR3; // ADC Channel Data Register 3 + AT91_REG ADC_CDR4; // ADC Channel Data Register 4 + AT91_REG ADC_CDR5; // ADC Channel Data Register 5 + AT91_REG ADC_CDR6; // ADC Channel Data Register 6 + AT91_REG ADC_CDR7; // ADC Channel Data Register 7 + AT91_REG Reserved1[44]; // + AT91_REG ADC_RPR; // Receive Pointer Register + AT91_REG ADC_RCR; // Receive Counter Register + AT91_REG ADC_TPR; // Transmit Pointer Register + AT91_REG ADC_TCR; // Transmit Counter Register + AT91_REG ADC_RNPR; // Receive Next Pointer Register + AT91_REG ADC_RNCR; // Receive Next Counter Register + AT91_REG ADC_TNPR; // Transmit Next Pointer Register + AT91_REG ADC_TNCR; // Transmit Next Counter Register + AT91_REG ADC_PTCR; // PDC Transfer Control Register + AT91_REG ADC_PTSR; // PDC Transfer Status Register +} AT91S_ADC, + *AT91PS_ADC; // -------- ADC_CR : (ADC Offset: 0x0) ADC Control Register -------- -#define AT91C_ADC_SWRST ((unsigned int) 0x1 << 0) // (ADC) Software Reset -#define AT91C_ADC_START ((unsigned int) 0x1 << 1) // (ADC) Start Conversion +#define AT91C_ADC_SWRST ((unsigned int) 0x1 << 0) // (ADC) Software Reset +#define AT91C_ADC_START ((unsigned int) 0x1 << 1) // (ADC) Start Conversion // -------- ADC_MR : (ADC Offset: 0x4) ADC Mode Register -------- -#define AT91C_ADC_TRGEN ((unsigned int) 0x1 << 0) // (ADC) Trigger Enable -#define AT91C_ADC_TRGEN_DIS ((unsigned int) 0x0) // (ADC) Hradware triggers are disabled. Starting a conversion is only possible by software -#define AT91C_ADC_TRGEN_EN ((unsigned int) 0x1) // (ADC) Hardware trigger selected by TRGSEL field is enabled. -#define AT91C_ADC_TRGSEL ((unsigned int) 0x7 << 1) // (ADC) Trigger Selection -#define AT91C_ADC_TRGSEL_TIOA0 ((unsigned int) 0x0 << 1) // (ADC) Selected TRGSEL = TIAO0 -#define AT91C_ADC_TRGSEL_TIOA1 ((unsigned int) 0x1 << 1) // (ADC) Selected TRGSEL = TIAO1 -#define AT91C_ADC_TRGSEL_TIOA2 ((unsigned int) 0x2 << 1) // (ADC) Selected TRGSEL = TIAO2 -#define AT91C_ADC_TRGSEL_TIOA3 ((unsigned int) 0x3 << 1) // (ADC) Selected TRGSEL = TIAO3 -#define AT91C_ADC_TRGSEL_TIOA4 ((unsigned int) 0x4 << 1) // (ADC) Selected TRGSEL = TIAO4 -#define AT91C_ADC_TRGSEL_TIOA5 ((unsigned int) 0x5 << 1) // (ADC) Selected TRGSEL = TIAO5 -#define AT91C_ADC_TRGSEL_EXT ((unsigned int) 0x6 << 1) // (ADC) Selected TRGSEL = External Trigger -#define AT91C_ADC_LOWRES ((unsigned int) 0x1 << 4) // (ADC) Resolution. -#define AT91C_ADC_LOWRES_10_BIT ((unsigned int) 0x0 << 4) // (ADC) 10-bit resolution -#define AT91C_ADC_LOWRES_8_BIT ((unsigned int) 0x1 << 4) // (ADC) 8-bit resolution -#define AT91C_ADC_SLEEP ((unsigned int) 0x1 << 5) // (ADC) Sleep Mode +#define AT91C_ADC_TRGEN ((unsigned int) 0x1 << 0) // (ADC) Trigger Enable +#define AT91C_ADC_TRGEN_DIS ((unsigned int) 0x0) // (ADC) Hradware triggers are disabled. Starting a conversion is only possible by software +#define AT91C_ADC_TRGEN_EN ((unsigned int) 0x1) // (ADC) Hardware trigger selected by TRGSEL field is enabled. +#define AT91C_ADC_TRGSEL ((unsigned int) 0x7 << 1) // (ADC) Trigger Selection +#define AT91C_ADC_TRGSEL_TIOA0 ((unsigned int) 0x0 << 1) // (ADC) Selected TRGSEL = TIAO0 +#define AT91C_ADC_TRGSEL_TIOA1 ((unsigned int) 0x1 << 1) // (ADC) Selected TRGSEL = TIAO1 +#define AT91C_ADC_TRGSEL_TIOA2 ((unsigned int) 0x2 << 1) // (ADC) Selected TRGSEL = TIAO2 +#define AT91C_ADC_TRGSEL_TIOA3 ((unsigned int) 0x3 << 1) // (ADC) Selected TRGSEL = TIAO3 +#define AT91C_ADC_TRGSEL_TIOA4 ((unsigned int) 0x4 << 1) // (ADC) Selected TRGSEL = TIAO4 +#define AT91C_ADC_TRGSEL_TIOA5 ((unsigned int) 0x5 << 1) // (ADC) Selected TRGSEL = TIAO5 +#define AT91C_ADC_TRGSEL_EXT ((unsigned int) 0x6 << 1) // (ADC) Selected TRGSEL = External Trigger +#define AT91C_ADC_LOWRES ((unsigned int) 0x1 << 4) // (ADC) Resolution. +#define AT91C_ADC_LOWRES_10_BIT ((unsigned int) 0x0 << 4) // (ADC) 10-bit resolution +#define AT91C_ADC_LOWRES_8_BIT ((unsigned int) 0x1 << 4) // (ADC) 8-bit resolution +#define AT91C_ADC_SLEEP ((unsigned int) 0x1 << 5) // (ADC) Sleep Mode #define AT91C_ADC_SLEEP_NORMAL_MODE ((unsigned int) 0x0 << 5) // (ADC) Normal Mode #define AT91C_ADC_SLEEP_MODE ((unsigned int) 0x1 << 5) // (ADC) Sleep Mode -#define AT91C_ADC_PRESCAL ((unsigned int) 0x3F << 8) // (ADC) Prescaler rate selection -#define AT91C_ADC_STARTUP ((unsigned int) 0x1F << 16) // (ADC) Startup Time -#define AT91C_ADC_SHTIM ((unsigned int) 0xF << 24) // (ADC) Sample & Hold Time -// -------- ADC_CHER : (ADC Offset: 0x10) ADC Channel Enable Register -------- -#define AT91C_ADC_CH0 ((unsigned int) 0x1 << 0) // (ADC) Channel 0 -#define AT91C_ADC_CH1 ((unsigned int) 0x1 << 1) // (ADC) Channel 1 -#define AT91C_ADC_CH2 ((unsigned int) 0x1 << 2) // (ADC) Channel 2 -#define AT91C_ADC_CH3 ((unsigned int) 0x1 << 3) // (ADC) Channel 3 -#define AT91C_ADC_CH4 ((unsigned int) 0x1 << 4) // (ADC) Channel 4 -#define AT91C_ADC_CH5 ((unsigned int) 0x1 << 5) // (ADC) Channel 5 -#define AT91C_ADC_CH6 ((unsigned int) 0x1 << 6) // (ADC) Channel 6 -#define AT91C_ADC_CH7 ((unsigned int) 0x1 << 7) // (ADC) Channel 7 -// -------- ADC_CHDR : (ADC Offset: 0x14) ADC Channel Disable Register -------- -// -------- ADC_CHSR : (ADC Offset: 0x18) ADC Channel Status Register -------- +#define AT91C_ADC_PRESCAL ((unsigned int) 0x3F << 8) // (ADC) Prescaler rate selection +#define AT91C_ADC_STARTUP ((unsigned int) 0x1F << 16) // (ADC) Startup Time +#define AT91C_ADC_SHTIM ((unsigned int) 0xF << 24) // (ADC) Sample & Hold Time +// -------- ADC_CHER : (ADC Offset: 0x10) ADC Channel Enable Register -------- +#define AT91C_ADC_CH0 ((unsigned int) 0x1 << 0) // (ADC) Channel 0 +#define AT91C_ADC_CH1 ((unsigned int) 0x1 << 1) // (ADC) Channel 1 +#define AT91C_ADC_CH2 ((unsigned int) 0x1 << 2) // (ADC) Channel 2 +#define AT91C_ADC_CH3 ((unsigned int) 0x1 << 3) // (ADC) Channel 3 +#define AT91C_ADC_CH4 ((unsigned int) 0x1 << 4) // (ADC) Channel 4 +#define AT91C_ADC_CH5 ((unsigned int) 0x1 << 5) // (ADC) Channel 5 +#define AT91C_ADC_CH6 ((unsigned int) 0x1 << 6) // (ADC) Channel 6 +#define AT91C_ADC_CH7 ((unsigned int) 0x1 << 7) // (ADC) Channel 7 +// -------- ADC_CHDR : (ADC Offset: 0x14) ADC Channel Disable Register -------- +// -------- ADC_CHSR : (ADC Offset: 0x18) ADC Channel Status Register -------- // -------- ADC_SR : (ADC Offset: 0x1c) ADC Status Register -------- -#define AT91C_ADC_EOC0 ((unsigned int) 0x1 << 0) // (ADC) End of Conversion -#define AT91C_ADC_EOC1 ((unsigned int) 0x1 << 1) // (ADC) End of Conversion -#define AT91C_ADC_EOC2 ((unsigned int) 0x1 << 2) // (ADC) End of Conversion -#define AT91C_ADC_EOC3 ((unsigned int) 0x1 << 3) // (ADC) End of Conversion -#define AT91C_ADC_EOC4 ((unsigned int) 0x1 << 4) // (ADC) End of Conversion -#define AT91C_ADC_EOC5 ((unsigned int) 0x1 << 5) // (ADC) End of Conversion -#define AT91C_ADC_EOC6 ((unsigned int) 0x1 << 6) // (ADC) End of Conversion -#define AT91C_ADC_EOC7 ((unsigned int) 0x1 << 7) // (ADC) End of Conversion -#define AT91C_ADC_OVRE0 ((unsigned int) 0x1 << 8) // (ADC) Overrun Error -#define AT91C_ADC_OVRE1 ((unsigned int) 0x1 << 9) // (ADC) Overrun Error -#define AT91C_ADC_OVRE2 ((unsigned int) 0x1 << 10) // (ADC) Overrun Error -#define AT91C_ADC_OVRE3 ((unsigned int) 0x1 << 11) // (ADC) Overrun Error -#define AT91C_ADC_OVRE4 ((unsigned int) 0x1 << 12) // (ADC) Overrun Error -#define AT91C_ADC_OVRE5 ((unsigned int) 0x1 << 13) // (ADC) Overrun Error -#define AT91C_ADC_OVRE6 ((unsigned int) 0x1 << 14) // (ADC) Overrun Error -#define AT91C_ADC_OVRE7 ((unsigned int) 0x1 << 15) // (ADC) Overrun Error -#define AT91C_ADC_DRDY ((unsigned int) 0x1 << 16) // (ADC) Data Ready -#define AT91C_ADC_GOVRE ((unsigned int) 0x1 << 17) // (ADC) General Overrun -#define AT91C_ADC_ENDRX ((unsigned int) 0x1 << 18) // (ADC) End of Receiver Transfer -#define AT91C_ADC_RXBUFF ((unsigned int) 0x1 << 19) // (ADC) RXBUFF Interrupt +#define AT91C_ADC_EOC0 ((unsigned int) 0x1 << 0) // (ADC) End of Conversion +#define AT91C_ADC_EOC1 ((unsigned int) 0x1 << 1) // (ADC) End of Conversion +#define AT91C_ADC_EOC2 ((unsigned int) 0x1 << 2) // (ADC) End of Conversion +#define AT91C_ADC_EOC3 ((unsigned int) 0x1 << 3) // (ADC) End of Conversion +#define AT91C_ADC_EOC4 ((unsigned int) 0x1 << 4) // (ADC) End of Conversion +#define AT91C_ADC_EOC5 ((unsigned int) 0x1 << 5) // (ADC) End of Conversion +#define AT91C_ADC_EOC6 ((unsigned int) 0x1 << 6) // (ADC) End of Conversion +#define AT91C_ADC_EOC7 ((unsigned int) 0x1 << 7) // (ADC) End of Conversion +#define AT91C_ADC_OVRE0 ((unsigned int) 0x1 << 8) // (ADC) Overrun Error +#define AT91C_ADC_OVRE1 ((unsigned int) 0x1 << 9) // (ADC) Overrun Error +#define AT91C_ADC_OVRE2 ((unsigned int) 0x1 << 10) // (ADC) Overrun Error +#define AT91C_ADC_OVRE3 ((unsigned int) 0x1 << 11) // (ADC) Overrun Error +#define AT91C_ADC_OVRE4 ((unsigned int) 0x1 << 12) // (ADC) Overrun Error +#define AT91C_ADC_OVRE5 ((unsigned int) 0x1 << 13) // (ADC) Overrun Error +#define AT91C_ADC_OVRE6 ((unsigned int) 0x1 << 14) // (ADC) Overrun Error +#define AT91C_ADC_OVRE7 ((unsigned int) 0x1 << 15) // (ADC) Overrun Error +#define AT91C_ADC_DRDY ((unsigned int) 0x1 << 16) // (ADC) Data Ready +#define AT91C_ADC_GOVRE ((unsigned int) 0x1 << 17) // (ADC) General Overrun +#define AT91C_ADC_ENDRX ((unsigned int) 0x1 << 18) // (ADC) End of Receiver Transfer +#define AT91C_ADC_RXBUFF ((unsigned int) 0x1 << 19) // (ADC) RXBUFF Interrupt // -------- ADC_LCDR : (ADC Offset: 0x20) ADC Last Converted Data Register -------- -#define AT91C_ADC_LDATA ((unsigned int) 0x3FF << 0) // (ADC) Last Data Converted +#define AT91C_ADC_LDATA ((unsigned int) 0x3FF << 0) // (ADC) Last Data Converted // -------- ADC_IER : (ADC Offset: 0x24) ADC Interrupt Enable Register -------- // -------- ADC_IDR : (ADC Offset: 0x28) ADC Interrupt Disable Register -------- // -------- ADC_IMR : (ADC Offset: 0x2c) ADC Interrupt Mask Register -------- // -------- ADC_CDR0 : (ADC Offset: 0x30) ADC Channel Data Register 0 -------- -#define AT91C_ADC_DATA ((unsigned int) 0x3FF << 0) // (ADC) Converted Data +#define AT91C_ADC_DATA ((unsigned int) 0x3FF << 0) // (ADC) Converted Data // -------- ADC_CDR1 : (ADC Offset: 0x34) ADC Channel Data Register 1 -------- // -------- ADC_CDR2 : (ADC Offset: 0x38) ADC Channel Data Register 2 -------- // -------- ADC_CDR3 : (ADC Offset: 0x3c) ADC Channel Data Register 3 -------- @@ -827,53 +842,54 @@ typedef struct _AT91S_ADC { // SOFTWARE API DEFINITION FOR Synchronous Serial Controller Interface // ***************************************************************************** typedef struct _AT91S_SSC { - AT91_REG SSC_CR; // Control Register - AT91_REG SSC_CMR; // Clock Mode Register - AT91_REG Reserved0[2]; // - AT91_REG SSC_RCMR; // Receive Clock ModeRegister - AT91_REG SSC_RFMR; // Receive Frame Mode Register - AT91_REG SSC_TCMR; // Transmit Clock Mode Register - AT91_REG SSC_TFMR; // Transmit Frame Mode Register - AT91_REG SSC_RHR; // Receive Holding Register - AT91_REG SSC_THR; // Transmit Holding Register - AT91_REG Reserved1[2]; // - AT91_REG SSC_RSHR; // Receive Sync Holding Register - AT91_REG SSC_TSHR; // Transmit Sync Holding Register - AT91_REG Reserved2[2]; // - AT91_REG SSC_SR; // Status Register - AT91_REG SSC_IER; // Interrupt Enable Register - AT91_REG SSC_IDR; // Interrupt Disable Register - AT91_REG SSC_IMR; // Interrupt Mask Register - AT91_REG Reserved3[44]; // - AT91_REG SSC_RPR; // Receive Pointer Register - AT91_REG SSC_RCR; // Receive Counter Register - AT91_REG SSC_TPR; // Transmit Pointer Register - AT91_REG SSC_TCR; // Transmit Counter Register - AT91_REG SSC_RNPR; // Receive Next Pointer Register - AT91_REG SSC_RNCR; // Receive Next Counter Register - AT91_REG SSC_TNPR; // Transmit Next Pointer Register - AT91_REG SSC_TNCR; // Transmit Next Counter Register - AT91_REG SSC_PTCR; // PDC Transfer Control Register - AT91_REG SSC_PTSR; // PDC Transfer Status Register -} AT91S_SSC, *AT91PS_SSC; + AT91_REG SSC_CR; // Control Register + AT91_REG SSC_CMR; // Clock Mode Register + AT91_REG Reserved0[2]; // + AT91_REG SSC_RCMR; // Receive Clock ModeRegister + AT91_REG SSC_RFMR; // Receive Frame Mode Register + AT91_REG SSC_TCMR; // Transmit Clock Mode Register + AT91_REG SSC_TFMR; // Transmit Frame Mode Register + AT91_REG SSC_RHR; // Receive Holding Register + AT91_REG SSC_THR; // Transmit Holding Register + AT91_REG Reserved1[2]; // + AT91_REG SSC_RSHR; // Receive Sync Holding Register + AT91_REG SSC_TSHR; // Transmit Sync Holding Register + AT91_REG Reserved2[2]; // + AT91_REG SSC_SR; // Status Register + AT91_REG SSC_IER; // Interrupt Enable Register + AT91_REG SSC_IDR; // Interrupt Disable Register + AT91_REG SSC_IMR; // Interrupt Mask Register + AT91_REG Reserved3[44]; // + AT91_REG SSC_RPR; // Receive Pointer Register + AT91_REG SSC_RCR; // Receive Counter Register + AT91_REG SSC_TPR; // Transmit Pointer Register + AT91_REG SSC_TCR; // Transmit Counter Register + AT91_REG SSC_RNPR; // Receive Next Pointer Register + AT91_REG SSC_RNCR; // Receive Next Counter Register + AT91_REG SSC_TNPR; // Transmit Next Pointer Register + AT91_REG SSC_TNCR; // Transmit Next Counter Register + AT91_REG SSC_PTCR; // PDC Transfer Control Register + AT91_REG SSC_PTSR; // PDC Transfer Status Register +} AT91S_SSC, + *AT91PS_SSC; // -------- SSC_CR : (SSC Offset: 0x0) SSC Control Register -------- -#define AT91C_SSC_RXEN ((unsigned int) 0x1 << 0) // (SSC) Receive Enable -#define AT91C_SSC_RXDIS ((unsigned int) 0x1 << 1) // (SSC) Receive Disable -#define AT91C_SSC_TXEN ((unsigned int) 0x1 << 8) // (SSC) Transmit Enable -#define AT91C_SSC_TXDIS ((unsigned int) 0x1 << 9) // (SSC) Transmit Disable -#define AT91C_SSC_SWRST ((unsigned int) 0x1 << 15) // (SSC) Software Reset +#define AT91C_SSC_RXEN ((unsigned int) 0x1 << 0) // (SSC) Receive Enable +#define AT91C_SSC_RXDIS ((unsigned int) 0x1 << 1) // (SSC) Receive Disable +#define AT91C_SSC_TXEN ((unsigned int) 0x1 << 8) // (SSC) Transmit Enable +#define AT91C_SSC_TXDIS ((unsigned int) 0x1 << 9) // (SSC) Transmit Disable +#define AT91C_SSC_SWRST ((unsigned int) 0x1 << 15) // (SSC) Software Reset // -------- SSC_RCMR : (SSC Offset: 0x10) SSC Receive Clock Mode Register -------- -#define AT91C_SSC_CKS ((unsigned int) 0x3 << 0) // (SSC) Receive/Transmit Clock Selection +#define AT91C_SSC_CKS ((unsigned int) 0x3 << 0) // (SSC) Receive/Transmit Clock Selection #define AT91C_SSC_CKS_DIV ((unsigned int) 0x0) // (SSC) Divided Clock #define AT91C_SSC_CKS_TK ((unsigned int) 0x1) // (SSC) TK Clock signal #define AT91C_SSC_CKS_RK ((unsigned int) 0x2) // (SSC) RK pin -#define AT91C_SSC_CKO ((unsigned int) 0x7 << 2) // (SSC) Receive/Transmit Clock Output Mode Selection -#define AT91C_SSC_CKO_NONE ((unsigned int) 0x0 << 2) // (SSC) Receive/Transmit Clock Output Mode: None RK pin: Input-only -#define AT91C_SSC_CKO_CONTINOUS ((unsigned int) 0x1 << 2) // (SSC) Continuous Receive/Transmit Clock RK pin: Output -#define AT91C_SSC_CKO_DATA_TX ((unsigned int) 0x2 << 2) // (SSC) Receive/Transmit Clock only during data transfers RK pin: Output -#define AT91C_SSC_CKI ((unsigned int) 0x1 << 5) // (SSC) Receive/Transmit Clock Inversion -#define AT91C_SSC_START ((unsigned int) 0xF << 8) // (SSC) Receive/Transmit Start Selection +#define AT91C_SSC_CKO ((unsigned int) 0x7 << 2) // (SSC) Receive/Transmit Clock Output Mode Selection +#define AT91C_SSC_CKO_NONE ((unsigned int) 0x0 << 2) // (SSC) Receive/Transmit Clock Output Mode: None RK pin: Input-only +#define AT91C_SSC_CKO_CONTINOUS ((unsigned int) 0x1 << 2) // (SSC) Continuous Receive/Transmit Clock RK pin: Output +#define AT91C_SSC_CKO_DATA_TX ((unsigned int) 0x2 << 2) // (SSC) Receive/Transmit Clock only during data transfers RK pin: Output +#define AT91C_SSC_CKI ((unsigned int) 0x1 << 5) // (SSC) Receive/Transmit Clock Inversion +#define AT91C_SSC_START ((unsigned int) 0xF << 8) // (SSC) Receive/Transmit Start Selection #define AT91C_SSC_START_CONTINOUS ((unsigned int) 0x0 << 8) // (SSC) Continuous, as soon as the receiver is enabled, and immediately after the end of transfer of the previous data. #define AT91C_SSC_START_TX ((unsigned int) 0x1 << 8) // (SSC) Transmit/Receive start #define AT91C_SSC_START_LOW_RF ((unsigned int) 0x2 << 8) // (SSC) Detection of a low level on RF input @@ -883,39 +899,39 @@ typedef struct _AT91S_SSC { #define AT91C_SSC_START_LEVEL_RF ((unsigned int) 0x6 << 8) // (SSC) Detection of any level change on RF input #define AT91C_SSC_START_EDGE_RF ((unsigned int) 0x7 << 8) // (SSC) Detection of any edge on RF input #define AT91C_SSC_START_0 ((unsigned int) 0x8 << 8) // (SSC) Compare 0 -#define AT91C_SSC_STTDLY ((unsigned int) 0xFF << 16) // (SSC) Receive/Transmit Start Delay -#define AT91C_SSC_PERIOD ((unsigned int) 0xFF << 24) // (SSC) Receive/Transmit Period Divider Selection +#define AT91C_SSC_STTDLY ((unsigned int) 0xFF << 16) // (SSC) Receive/Transmit Start Delay +#define AT91C_SSC_PERIOD ((unsigned int) 0xFF << 24) // (SSC) Receive/Transmit Period Divider Selection // -------- SSC_RFMR : (SSC Offset: 0x14) SSC Receive Frame Mode Register -------- -#define AT91C_SSC_DATLEN ((unsigned int) 0x1F << 0) // (SSC) Data Length -#define AT91C_SSC_LOOP ((unsigned int) 0x1 << 5) // (SSC) Loop Mode -#define AT91C_SSC_MSBF ((unsigned int) 0x1 << 7) // (SSC) Most Significant Bit First -#define AT91C_SSC_DATNB ((unsigned int) 0xF << 8) // (SSC) Data Number per Frame -#define AT91C_SSC_FSLEN ((unsigned int) 0xF << 16) // (SSC) Receive/Transmit Frame Sync length -#define AT91C_SSC_FSOS ((unsigned int) 0x7 << 20) // (SSC) Receive/Transmit Frame Sync Output Selection -#define AT91C_SSC_FSOS_NONE ((unsigned int) 0x0 << 20) // (SSC) Selected Receive/Transmit Frame Sync Signal: None RK pin Input-only -#define AT91C_SSC_FSOS_NEGATIVE ((unsigned int) 0x1 << 20) // (SSC) Selected Receive/Transmit Frame Sync Signal: Negative Pulse -#define AT91C_SSC_FSOS_POSITIVE ((unsigned int) 0x2 << 20) // (SSC) Selected Receive/Transmit Frame Sync Signal: Positive Pulse -#define AT91C_SSC_FSOS_LOW ((unsigned int) 0x3 << 20) // (SSC) Selected Receive/Transmit Frame Sync Signal: Driver Low during data transfer -#define AT91C_SSC_FSOS_HIGH ((unsigned int) 0x4 << 20) // (SSC) Selected Receive/Transmit Frame Sync Signal: Driver High during data transfer -#define AT91C_SSC_FSOS_TOGGLE ((unsigned int) 0x5 << 20) // (SSC) Selected Receive/Transmit Frame Sync Signal: Toggling at each start of data transfer -#define AT91C_SSC_FSEDGE ((unsigned int) 0x1 << 24) // (SSC) Frame Sync Edge Detection +#define AT91C_SSC_DATLEN ((unsigned int) 0x1F << 0) // (SSC) Data Length +#define AT91C_SSC_LOOP ((unsigned int) 0x1 << 5) // (SSC) Loop Mode +#define AT91C_SSC_MSBF ((unsigned int) 0x1 << 7) // (SSC) Most Significant Bit First +#define AT91C_SSC_DATNB ((unsigned int) 0xF << 8) // (SSC) Data Number per Frame +#define AT91C_SSC_FSLEN ((unsigned int) 0xF << 16) // (SSC) Receive/Transmit Frame Sync length +#define AT91C_SSC_FSOS ((unsigned int) 0x7 << 20) // (SSC) Receive/Transmit Frame Sync Output Selection +#define AT91C_SSC_FSOS_NONE ((unsigned int) 0x0 << 20) // (SSC) Selected Receive/Transmit Frame Sync Signal: None RK pin Input-only +#define AT91C_SSC_FSOS_NEGATIVE ((unsigned int) 0x1 << 20) // (SSC) Selected Receive/Transmit Frame Sync Signal: Negative Pulse +#define AT91C_SSC_FSOS_POSITIVE ((unsigned int) 0x2 << 20) // (SSC) Selected Receive/Transmit Frame Sync Signal: Positive Pulse +#define AT91C_SSC_FSOS_LOW ((unsigned int) 0x3 << 20) // (SSC) Selected Receive/Transmit Frame Sync Signal: Driver Low during data transfer +#define AT91C_SSC_FSOS_HIGH ((unsigned int) 0x4 << 20) // (SSC) Selected Receive/Transmit Frame Sync Signal: Driver High during data transfer +#define AT91C_SSC_FSOS_TOGGLE ((unsigned int) 0x5 << 20) // (SSC) Selected Receive/Transmit Frame Sync Signal: Toggling at each start of data transfer +#define AT91C_SSC_FSEDGE ((unsigned int) 0x1 << 24) // (SSC) Frame Sync Edge Detection // -------- SSC_TCMR : (SSC Offset: 0x18) SSC Transmit Clock Mode Register -------- // -------- SSC_TFMR : (SSC Offset: 0x1c) SSC Transmit Frame Mode Register -------- -#define AT91C_SSC_DATDEF ((unsigned int) 0x1 << 5) // (SSC) Data Default Value -#define AT91C_SSC_FSDEN ((unsigned int) 0x1 << 23) // (SSC) Frame Sync Data Enable +#define AT91C_SSC_DATDEF ((unsigned int) 0x1 << 5) // (SSC) Data Default Value +#define AT91C_SSC_FSDEN ((unsigned int) 0x1 << 23) // (SSC) Frame Sync Data Enable // -------- SSC_SR : (SSC Offset: 0x40) SSC Status Register -------- -#define AT91C_SSC_TXRDY ((unsigned int) 0x1 << 0) // (SSC) Transmit Ready -#define AT91C_SSC_TXEMPTY ((unsigned int) 0x1 << 1) // (SSC) Transmit Empty -#define AT91C_SSC_ENDTX ((unsigned int) 0x1 << 2) // (SSC) End Of Transmission -#define AT91C_SSC_TXBUFE ((unsigned int) 0x1 << 3) // (SSC) Transmit Buffer Empty -#define AT91C_SSC_RXRDY ((unsigned int) 0x1 << 4) // (SSC) Receive Ready -#define AT91C_SSC_OVRUN ((unsigned int) 0x1 << 5) // (SSC) Receive Overrun -#define AT91C_SSC_ENDRX ((unsigned int) 0x1 << 6) // (SSC) End of Reception -#define AT91C_SSC_RXBUFF ((unsigned int) 0x1 << 7) // (SSC) Receive Buffer Full -#define AT91C_SSC_TXSYN ((unsigned int) 0x1 << 10) // (SSC) Transmit Sync -#define AT91C_SSC_RXSYN ((unsigned int) 0x1 << 11) // (SSC) Receive Sync -#define AT91C_SSC_TXENA ((unsigned int) 0x1 << 16) // (SSC) Transmit Enable -#define AT91C_SSC_RXENA ((unsigned int) 0x1 << 17) // (SSC) Receive Enable +#define AT91C_SSC_TXRDY ((unsigned int) 0x1 << 0) // (SSC) Transmit Ready +#define AT91C_SSC_TXEMPTY ((unsigned int) 0x1 << 1) // (SSC) Transmit Empty +#define AT91C_SSC_ENDTX ((unsigned int) 0x1 << 2) // (SSC) End Of Transmission +#define AT91C_SSC_TXBUFE ((unsigned int) 0x1 << 3) // (SSC) Transmit Buffer Empty +#define AT91C_SSC_RXRDY ((unsigned int) 0x1 << 4) // (SSC) Receive Ready +#define AT91C_SSC_OVRUN ((unsigned int) 0x1 << 5) // (SSC) Receive Overrun +#define AT91C_SSC_ENDRX ((unsigned int) 0x1 << 6) // (SSC) End of Reception +#define AT91C_SSC_RXBUFF ((unsigned int) 0x1 << 7) // (SSC) Receive Buffer Full +#define AT91C_SSC_TXSYN ((unsigned int) 0x1 << 10) // (SSC) Transmit Sync +#define AT91C_SSC_RXSYN ((unsigned int) 0x1 << 11) // (SSC) Receive Sync +#define AT91C_SSC_TXENA ((unsigned int) 0x1 << 16) // (SSC) Transmit Enable +#define AT91C_SSC_RXENA ((unsigned int) 0x1 << 17) // (SSC) Receive Enable // -------- SSC_IER : (SSC Offset: 0x44) SSC Interrupt Enable Register -------- // -------- SSC_IDR : (SSC Offset: 0x48) SSC Interrupt Disable Register -------- // -------- SSC_IMR : (SSC Offset: 0x4c) SSC Interrupt Mask Register -------- @@ -924,140 +940,142 @@ typedef struct _AT91S_SSC { // SOFTWARE API DEFINITION FOR Usart // ***************************************************************************** typedef struct _AT91S_USART { - AT91_REG US_CR; // Control Register - AT91_REG US_MR; // Mode Register - AT91_REG US_IER; // Interrupt Enable Register - AT91_REG US_IDR; // Interrupt Disable Register - AT91_REG US_IMR; // Interrupt Mask Register - AT91_REG US_CSR; // Channel Status Register - AT91_REG US_RHR; // Receiver Holding Register - AT91_REG US_THR; // Transmitter Holding Register - AT91_REG US_BRGR; // Baud Rate Generator Register - AT91_REG US_RTOR; // Receiver Time-out Register - AT91_REG US_TTGR; // Transmitter Time-guard Register - AT91_REG Reserved0[5]; // - AT91_REG US_FIDI; // FI_DI_Ratio Register - AT91_REG US_NER; // Nb Errors Register - AT91_REG Reserved1[1]; // - AT91_REG US_IF; // IRDA_FILTER Register - AT91_REG Reserved2[44]; // - AT91_REG US_RPR; // Receive Pointer Register - AT91_REG US_RCR; // Receive Counter Register - AT91_REG US_TPR; // Transmit Pointer Register - AT91_REG US_TCR; // Transmit Counter Register - AT91_REG US_RNPR; // Receive Next Pointer Register - AT91_REG US_RNCR; // Receive Next Counter Register - AT91_REG US_TNPR; // Transmit Next Pointer Register - AT91_REG US_TNCR; // Transmit Next Counter Register - AT91_REG US_PTCR; // PDC Transfer Control Register - AT91_REG US_PTSR; // PDC Transfer Status Register -} AT91S_USART, *AT91PS_USART; + AT91_REG US_CR; // Control Register + AT91_REG US_MR; // Mode Register + AT91_REG US_IER; // Interrupt Enable Register + AT91_REG US_IDR; // Interrupt Disable Register + AT91_REG US_IMR; // Interrupt Mask Register + AT91_REG US_CSR; // Channel Status Register + AT91_REG US_RHR; // Receiver Holding Register + AT91_REG US_THR; // Transmitter Holding Register + AT91_REG US_BRGR; // Baud Rate Generator Register + AT91_REG US_RTOR; // Receiver Time-out Register + AT91_REG US_TTGR; // Transmitter Time-guard Register + AT91_REG Reserved0[5]; // + AT91_REG US_FIDI; // FI_DI_Ratio Register + AT91_REG US_NER; // Nb Errors Register + AT91_REG Reserved1[1]; // + AT91_REG US_IF; // IRDA_FILTER Register + AT91_REG Reserved2[44]; // + AT91_REG US_RPR; // Receive Pointer Register + AT91_REG US_RCR; // Receive Counter Register + AT91_REG US_TPR; // Transmit Pointer Register + AT91_REG US_TCR; // Transmit Counter Register + AT91_REG US_RNPR; // Receive Next Pointer Register + AT91_REG US_RNCR; // Receive Next Counter Register + AT91_REG US_TNPR; // Transmit Next Pointer Register + AT91_REG US_TNCR; // Transmit Next Counter Register + AT91_REG US_PTCR; // PDC Transfer Control Register + AT91_REG US_PTSR; // PDC Transfer Status Register +} AT91S_USART, + *AT91PS_USART; // -------- US_CR : (USART Offset: 0x0) Debug Unit Control Register -------- -#define AT91C_US_STTBRK ((unsigned int) 0x1 << 9) // (USART) Start Break -#define AT91C_US_STPBRK ((unsigned int) 0x1 << 10) // (USART) Stop Break -#define AT91C_US_STTTO ((unsigned int) 0x1 << 11) // (USART) Start Time-out -#define AT91C_US_SENDA ((unsigned int) 0x1 << 12) // (USART) Send Address -#define AT91C_US_RSTIT ((unsigned int) 0x1 << 13) // (USART) Reset Iterations -#define AT91C_US_RSTNACK ((unsigned int) 0x1 << 14) // (USART) Reset Non Acknowledge -#define AT91C_US_RETTO ((unsigned int) 0x1 << 15) // (USART) Rearm Time-out -#define AT91C_US_DTREN ((unsigned int) 0x1 << 16) // (USART) Data Terminal ready Enable -#define AT91C_US_DTRDIS ((unsigned int) 0x1 << 17) // (USART) Data Terminal ready Disable -#define AT91C_US_RTSEN ((unsigned int) 0x1 << 18) // (USART) Request to Send enable -#define AT91C_US_RTSDIS ((unsigned int) 0x1 << 19) // (USART) Request to Send Disable +#define AT91C_US_STTBRK ((unsigned int) 0x1 << 9) // (USART) Start Break +#define AT91C_US_STPBRK ((unsigned int) 0x1 << 10) // (USART) Stop Break +#define AT91C_US_STTTO ((unsigned int) 0x1 << 11) // (USART) Start Time-out +#define AT91C_US_SENDA ((unsigned int) 0x1 << 12) // (USART) Send Address +#define AT91C_US_RSTIT ((unsigned int) 0x1 << 13) // (USART) Reset Iterations +#define AT91C_US_RSTNACK ((unsigned int) 0x1 << 14) // (USART) Reset Non Acknowledge +#define AT91C_US_RETTO ((unsigned int) 0x1 << 15) // (USART) Rearm Time-out +#define AT91C_US_DTREN ((unsigned int) 0x1 << 16) // (USART) Data Terminal ready Enable +#define AT91C_US_DTRDIS ((unsigned int) 0x1 << 17) // (USART) Data Terminal ready Disable +#define AT91C_US_RTSEN ((unsigned int) 0x1 << 18) // (USART) Request to Send enable +#define AT91C_US_RTSDIS ((unsigned int) 0x1 << 19) // (USART) Request to Send Disable // -------- US_MR : (USART Offset: 0x4) Debug Unit Mode Register -------- -#define AT91C_US_USMODE ((unsigned int) 0xF << 0) // (USART) Usart mode -#define AT91C_US_USMODE_NORMAL ((unsigned int) 0x0) // (USART) Normal -#define AT91C_US_USMODE_RS485 ((unsigned int) 0x1) // (USART) RS485 -#define AT91C_US_USMODE_HWHSH ((unsigned int) 0x2) // (USART) Hardware Handshaking -#define AT91C_US_USMODE_MODEM ((unsigned int) 0x3) // (USART) Modem -#define AT91C_US_USMODE_ISO7816_0 ((unsigned int) 0x4) // (USART) ISO7816 protocol: T = 0 -#define AT91C_US_USMODE_ISO7816_1 ((unsigned int) 0x6) // (USART) ISO7816 protocol: T = 1 -#define AT91C_US_USMODE_IRDA ((unsigned int) 0x8) // (USART) IrDA -#define AT91C_US_USMODE_SWHSH ((unsigned int) 0xC) // (USART) Software Handshaking -#define AT91C_US_CLKS ((unsigned int) 0x3 << 4) // (USART) Clock Selection (Baud Rate generator Input Clock -#define AT91C_US_CLKS_CLOCK ((unsigned int) 0x0 << 4) // (USART) Clock -#define AT91C_US_CLKS_FDIV1 ((unsigned int) 0x1 << 4) // (USART) fdiv1 -#define AT91C_US_CLKS_SLOW ((unsigned int) 0x2 << 4) // (USART) slow_clock (ARM) -#define AT91C_US_CLKS_EXT ((unsigned int) 0x3 << 4) // (USART) External (SCK) -#define AT91C_US_CHRL ((unsigned int) 0x3 << 6) // (USART) Clock Selection (Baud Rate generator Input Clock -#define AT91C_US_CHRL_5_BITS ((unsigned int) 0x0 << 6) // (USART) Character Length: 5 bits -#define AT91C_US_CHRL_6_BITS ((unsigned int) 0x1 << 6) // (USART) Character Length: 6 bits -#define AT91C_US_CHRL_7_BITS ((unsigned int) 0x2 << 6) // (USART) Character Length: 7 bits -#define AT91C_US_CHRL_8_BITS ((unsigned int) 0x3 << 6) // (USART) Character Length: 8 bits -#define AT91C_US_SYNC ((unsigned int) 0x1 << 8) // (USART) Synchronous Mode Select -#define AT91C_US_NBSTOP ((unsigned int) 0x3 << 12) // (USART) Number of Stop bits +#define AT91C_US_USMODE ((unsigned int) 0xF << 0) // (USART) Usart mode +#define AT91C_US_USMODE_NORMAL ((unsigned int) 0x0) // (USART) Normal +#define AT91C_US_USMODE_RS485 ((unsigned int) 0x1) // (USART) RS485 +#define AT91C_US_USMODE_HWHSH ((unsigned int) 0x2) // (USART) Hardware Handshaking +#define AT91C_US_USMODE_MODEM ((unsigned int) 0x3) // (USART) Modem +#define AT91C_US_USMODE_ISO7816_0 ((unsigned int) 0x4) // (USART) ISO7816 protocol: T = 0 +#define AT91C_US_USMODE_ISO7816_1 ((unsigned int) 0x6) // (USART) ISO7816 protocol: T = 1 +#define AT91C_US_USMODE_IRDA ((unsigned int) 0x8) // (USART) IrDA +#define AT91C_US_USMODE_SWHSH ((unsigned int) 0xC) // (USART) Software Handshaking +#define AT91C_US_CLKS ((unsigned int) 0x3 << 4) // (USART) Clock Selection (Baud Rate generator Input Clock +#define AT91C_US_CLKS_CLOCK ((unsigned int) 0x0 << 4) // (USART) Clock +#define AT91C_US_CLKS_FDIV1 ((unsigned int) 0x1 << 4) // (USART) fdiv1 +#define AT91C_US_CLKS_SLOW ((unsigned int) 0x2 << 4) // (USART) slow_clock (ARM) +#define AT91C_US_CLKS_EXT ((unsigned int) 0x3 << 4) // (USART) External (SCK) +#define AT91C_US_CHRL ((unsigned int) 0x3 << 6) // (USART) Clock Selection (Baud Rate generator Input Clock +#define AT91C_US_CHRL_5_BITS ((unsigned int) 0x0 << 6) // (USART) Character Length: 5 bits +#define AT91C_US_CHRL_6_BITS ((unsigned int) 0x1 << 6) // (USART) Character Length: 6 bits +#define AT91C_US_CHRL_7_BITS ((unsigned int) 0x2 << 6) // (USART) Character Length: 7 bits +#define AT91C_US_CHRL_8_BITS ((unsigned int) 0x3 << 6) // (USART) Character Length: 8 bits +#define AT91C_US_SYNC ((unsigned int) 0x1 << 8) // (USART) Synchronous Mode Select +#define AT91C_US_NBSTOP ((unsigned int) 0x3 << 12) // (USART) Number of Stop bits #define AT91C_US_NBSTOP_1_BIT ((unsigned int) 0x0 << 12) // (USART) 1 stop bit #define AT91C_US_NBSTOP_15_BIT ((unsigned int) 0x1 << 12) // (USART) Asynchronous (SYNC=0) 2 stop bits Synchronous (SYNC=1) 2 stop bits #define AT91C_US_NBSTOP_2_BIT ((unsigned int) 0x2 << 12) // (USART) 2 stop bits -#define AT91C_US_MSBF ((unsigned int) 0x1 << 16) // (USART) Bit Order -#define AT91C_US_MODE9 ((unsigned int) 0x1 << 17) // (USART) 9-bit Character length -#define AT91C_US_CKLO ((unsigned int) 0x1 << 18) // (USART) Clock Output Select -#define AT91C_US_OVER ((unsigned int) 0x1 << 19) // (USART) Over Sampling Mode -#define AT91C_US_INACK ((unsigned int) 0x1 << 20) // (USART) Inhibit Non Acknowledge -#define AT91C_US_DSNACK ((unsigned int) 0x1 << 21) // (USART) Disable Successive NACK -#define AT91C_US_MAX_ITER ((unsigned int) 0x1 << 24) // (USART) Number of Repetitions -#define AT91C_US_FILTER ((unsigned int) 0x1 << 28) // (USART) Receive Line Filter +#define AT91C_US_MSBF ((unsigned int) 0x1 << 16) // (USART) Bit Order +#define AT91C_US_MODE9 ((unsigned int) 0x1 << 17) // (USART) 9-bit Character length +#define AT91C_US_CKLO ((unsigned int) 0x1 << 18) // (USART) Clock Output Select +#define AT91C_US_OVER ((unsigned int) 0x1 << 19) // (USART) Over Sampling Mode +#define AT91C_US_INACK ((unsigned int) 0x1 << 20) // (USART) Inhibit Non Acknowledge +#define AT91C_US_DSNACK ((unsigned int) 0x1 << 21) // (USART) Disable Successive NACK +#define AT91C_US_MAX_ITER ((unsigned int) 0x1 << 24) // (USART) Number of Repetitions +#define AT91C_US_FILTER ((unsigned int) 0x1 << 28) // (USART) Receive Line Filter // -------- US_IER : (USART Offset: 0x8) Debug Unit Interrupt Enable Register -------- -#define AT91C_US_RXBRK ((unsigned int) 0x1 << 2) // (USART) Break Received/End of Break -#define AT91C_US_TIMEOUT ((unsigned int) 0x1 << 8) // (USART) Receiver Time-out -#define AT91C_US_ITERATION ((unsigned int) 0x1 << 10) // (USART) Max number of Repetitions Reached -#define AT91C_US_NACK ((unsigned int) 0x1 << 13) // (USART) Non Acknowledge -#define AT91C_US_RIIC ((unsigned int) 0x1 << 16) // (USART) Ring INdicator Input Change Flag -#define AT91C_US_DSRIC ((unsigned int) 0x1 << 17) // (USART) Data Set Ready Input Change Flag -#define AT91C_US_DCDIC ((unsigned int) 0x1 << 18) // (USART) Data Carrier Flag -#define AT91C_US_CTSIC ((unsigned int) 0x1 << 19) // (USART) Clear To Send Input Change Flag +#define AT91C_US_RXBRK ((unsigned int) 0x1 << 2) // (USART) Break Received/End of Break +#define AT91C_US_TIMEOUT ((unsigned int) 0x1 << 8) // (USART) Receiver Time-out +#define AT91C_US_ITERATION ((unsigned int) 0x1 << 10) // (USART) Max number of Repetitions Reached +#define AT91C_US_NACK ((unsigned int) 0x1 << 13) // (USART) Non Acknowledge +#define AT91C_US_RIIC ((unsigned int) 0x1 << 16) // (USART) Ring INdicator Input Change Flag +#define AT91C_US_DSRIC ((unsigned int) 0x1 << 17) // (USART) Data Set Ready Input Change Flag +#define AT91C_US_DCDIC ((unsigned int) 0x1 << 18) // (USART) Data Carrier Flag +#define AT91C_US_CTSIC ((unsigned int) 0x1 << 19) // (USART) Clear To Send Input Change Flag // -------- US_IDR : (USART Offset: 0xc) Debug Unit Interrupt Disable Register -------- // -------- US_IMR : (USART Offset: 0x10) Debug Unit Interrupt Mask Register -------- // -------- US_CSR : (USART Offset: 0x14) Debug Unit Channel Status Register -------- -#define AT91C_US_RI ((unsigned int) 0x1 << 20) // (USART) Image of RI Input -#define AT91C_US_DSR ((unsigned int) 0x1 << 21) // (USART) Image of DSR Input -#define AT91C_US_DCD ((unsigned int) 0x1 << 22) // (USART) Image of DCD Input -#define AT91C_US_CTS ((unsigned int) 0x1 << 23) // (USART) Image of CTS Input +#define AT91C_US_RI ((unsigned int) 0x1 << 20) // (USART) Image of RI Input +#define AT91C_US_DSR ((unsigned int) 0x1 << 21) // (USART) Image of DSR Input +#define AT91C_US_DCD ((unsigned int) 0x1 << 22) // (USART) Image of DCD Input +#define AT91C_US_CTS ((unsigned int) 0x1 << 23) // (USART) Image of CTS Input // ***************************************************************************** // SOFTWARE API DEFINITION FOR Two-wire Interface // ***************************************************************************** typedef struct _AT91S_TWI { - AT91_REG TWI_CR; // Control Register - AT91_REG TWI_MMR; // Master Mode Register - AT91_REG Reserved0[1]; // - AT91_REG TWI_IADR; // Internal Address Register - AT91_REG TWI_CWGR; // Clock Waveform Generator Register - AT91_REG Reserved1[3]; // - AT91_REG TWI_SR; // Status Register - AT91_REG TWI_IER; // Interrupt Enable Register - AT91_REG TWI_IDR; // Interrupt Disable Register - AT91_REG TWI_IMR; // Interrupt Mask Register - AT91_REG TWI_RHR; // Receive Holding Register - AT91_REG TWI_THR; // Transmit Holding Register -} AT91S_TWI, *AT91PS_TWI; + AT91_REG TWI_CR; // Control Register + AT91_REG TWI_MMR; // Master Mode Register + AT91_REG Reserved0[1]; // + AT91_REG TWI_IADR; // Internal Address Register + AT91_REG TWI_CWGR; // Clock Waveform Generator Register + AT91_REG Reserved1[3]; // + AT91_REG TWI_SR; // Status Register + AT91_REG TWI_IER; // Interrupt Enable Register + AT91_REG TWI_IDR; // Interrupt Disable Register + AT91_REG TWI_IMR; // Interrupt Mask Register + AT91_REG TWI_RHR; // Receive Holding Register + AT91_REG TWI_THR; // Transmit Holding Register +} AT91S_TWI, + *AT91PS_TWI; // -------- TWI_CR : (TWI Offset: 0x0) TWI Control Register -------- -#define AT91C_TWI_START ((unsigned int) 0x1 << 0) // (TWI) Send a START Condition -#define AT91C_TWI_STOP ((unsigned int) 0x1 << 1) // (TWI) Send a STOP Condition -#define AT91C_TWI_MSEN ((unsigned int) 0x1 << 2) // (TWI) TWI Master Transfer Enabled -#define AT91C_TWI_MSDIS ((unsigned int) 0x1 << 3) // (TWI) TWI Master Transfer Disabled -#define AT91C_TWI_SWRST ((unsigned int) 0x1 << 7) // (TWI) Software Reset +#define AT91C_TWI_START ((unsigned int) 0x1 << 0) // (TWI) Send a START Condition +#define AT91C_TWI_STOP ((unsigned int) 0x1 << 1) // (TWI) Send a STOP Condition +#define AT91C_TWI_MSEN ((unsigned int) 0x1 << 2) // (TWI) TWI Master Transfer Enabled +#define AT91C_TWI_MSDIS ((unsigned int) 0x1 << 3) // (TWI) TWI Master Transfer Disabled +#define AT91C_TWI_SWRST ((unsigned int) 0x1 << 7) // (TWI) Software Reset // -------- TWI_MMR : (TWI Offset: 0x4) TWI Master Mode Register -------- -#define AT91C_TWI_IADRSZ ((unsigned int) 0x3 << 8) // (TWI) Internal Device Address Size -#define AT91C_TWI_IADRSZ_NO ((unsigned int) 0x0 << 8) // (TWI) No internal device address -#define AT91C_TWI_IADRSZ_1_BYTE ((unsigned int) 0x1 << 8) // (TWI) One-byte internal device address -#define AT91C_TWI_IADRSZ_2_BYTE ((unsigned int) 0x2 << 8) // (TWI) Two-byte internal device address -#define AT91C_TWI_IADRSZ_3_BYTE ((unsigned int) 0x3 << 8) // (TWI) Three-byte internal device address -#define AT91C_TWI_MREAD ((unsigned int) 0x1 << 12) // (TWI) Master Read Direction -#define AT91C_TWI_DADR ((unsigned int) 0x7F << 16) // (TWI) Device Address +#define AT91C_TWI_IADRSZ ((unsigned int) 0x3 << 8) // (TWI) Internal Device Address Size +#define AT91C_TWI_IADRSZ_NO ((unsigned int) 0x0 << 8) // (TWI) No internal device address +#define AT91C_TWI_IADRSZ_1_BYTE ((unsigned int) 0x1 << 8) // (TWI) One-byte internal device address +#define AT91C_TWI_IADRSZ_2_BYTE ((unsigned int) 0x2 << 8) // (TWI) Two-byte internal device address +#define AT91C_TWI_IADRSZ_3_BYTE ((unsigned int) 0x3 << 8) // (TWI) Three-byte internal device address +#define AT91C_TWI_MREAD ((unsigned int) 0x1 << 12) // (TWI) Master Read Direction +#define AT91C_TWI_DADR ((unsigned int) 0x7F << 16) // (TWI) Device Address // -------- TWI_CWGR : (TWI Offset: 0x10) TWI Clock Waveform Generator Register -------- -#define AT91C_TWI_CLDIV ((unsigned int) 0xFF << 0) // (TWI) Clock Low Divider -#define AT91C_TWI_CHDIV ((unsigned int) 0xFF << 8) // (TWI) Clock High Divider -#define AT91C_TWI_CKDIV ((unsigned int) 0x7 << 16) // (TWI) Clock Divider +#define AT91C_TWI_CLDIV ((unsigned int) 0xFF << 0) // (TWI) Clock Low Divider +#define AT91C_TWI_CHDIV ((unsigned int) 0xFF << 8) // (TWI) Clock High Divider +#define AT91C_TWI_CKDIV ((unsigned int) 0x7 << 16) // (TWI) Clock Divider // -------- TWI_SR : (TWI Offset: 0x20) TWI Status Register -------- -#define AT91C_TWI_TXCOMP ((unsigned int) 0x1 << 0) // (TWI) Transmission Completed -#define AT91C_TWI_RXRDY ((unsigned int) 0x1 << 1) // (TWI) Receive holding register ReaDY -#define AT91C_TWI_TXRDY ((unsigned int) 0x1 << 2) // (TWI) Transmit holding register ReaDY -#define AT91C_TWI_OVRE ((unsigned int) 0x1 << 6) // (TWI) Overrun Error -#define AT91C_TWI_UNRE ((unsigned int) 0x1 << 7) // (TWI) Underrun Error -#define AT91C_TWI_NACK ((unsigned int) 0x1 << 8) // (TWI) Not Acknowledged +#define AT91C_TWI_TXCOMP ((unsigned int) 0x1 << 0) // (TWI) Transmission Completed +#define AT91C_TWI_RXRDY ((unsigned int) 0x1 << 1) // (TWI) Receive holding register ReaDY +#define AT91C_TWI_TXRDY ((unsigned int) 0x1 << 2) // (TWI) Transmit holding register ReaDY +#define AT91C_TWI_OVRE ((unsigned int) 0x1 << 6) // (TWI) Overrun Error +#define AT91C_TWI_UNRE ((unsigned int) 0x1 << 7) // (TWI) Underrun Error +#define AT91C_TWI_NACK ((unsigned int) 0x1 << 8) // (TWI) Not Acknowledged // -------- TWI_IER : (TWI Offset: 0x24) TWI Interrupt Enable Register -------- // -------- TWI_IDR : (TWI Offset: 0x28) TWI Interrupt Disable Register -------- // -------- TWI_IMR : (TWI Offset: 0x2c) TWI Interrupt Mask Register -------- @@ -1066,25 +1084,26 @@ typedef struct _AT91S_TWI { // SOFTWARE API DEFINITION FOR Timer Counter Channel Interface // ***************************************************************************** typedef struct _AT91S_TC { - AT91_REG TC_CCR; // Channel Control Register - AT91_REG TC_CMR; // Channel Mode Register (Capture Mode / Waveform Mode) - AT91_REG Reserved0[2]; // - AT91_REG TC_CV; // Counter Value - AT91_REG TC_RA; // Register A - AT91_REG TC_RB; // Register B - AT91_REG TC_RC; // Register C - AT91_REG TC_SR; // Status Register - AT91_REG TC_IER; // Interrupt Enable Register - AT91_REG TC_IDR; // Interrupt Disable Register - AT91_REG TC_IMR; // Interrupt Mask Register -} AT91S_TC, *AT91PS_TC; + AT91_REG TC_CCR; // Channel Control Register + AT91_REG TC_CMR; // Channel Mode Register (Capture Mode / Waveform Mode) + AT91_REG Reserved0[2]; // + AT91_REG TC_CV; // Counter Value + AT91_REG TC_RA; // Register A + AT91_REG TC_RB; // Register B + AT91_REG TC_RC; // Register C + AT91_REG TC_SR; // Status Register + AT91_REG TC_IER; // Interrupt Enable Register + AT91_REG TC_IDR; // Interrupt Disable Register + AT91_REG TC_IMR; // Interrupt Mask Register +} AT91S_TC, + *AT91PS_TC; // -------- TC_CCR : (TC Offset: 0x0) TC Channel Control Register -------- -#define AT91C_TC_CLKEN ((unsigned int) 0x1 << 0) // (TC) Counter Clock Enable Command -#define AT91C_TC_CLKDIS ((unsigned int) 0x1 << 1) // (TC) Counter Clock Disable Command -#define AT91C_TC_SWTRG ((unsigned int) 0x1 << 2) // (TC) Software Trigger Command +#define AT91C_TC_CLKEN ((unsigned int) 0x1 << 0) // (TC) Counter Clock Enable Command +#define AT91C_TC_CLKDIS ((unsigned int) 0x1 << 1) // (TC) Counter Clock Disable Command +#define AT91C_TC_SWTRG ((unsigned int) 0x1 << 2) // (TC) Software Trigger Command // -------- TC_CMR : (TC Offset: 0x4) TC Channel Mode Register: Capture Mode / Waveform Mode -------- -#define AT91C_TC_CLKS ((unsigned int) 0x7 << 0) // (TC) Clock Selection +#define AT91C_TC_CLKS ((unsigned int) 0x7 << 0) // (TC) Clock Selection #define AT91C_TC_CLKS_TIMER_DIV1_CLOCK ((unsigned int) 0x0) // (TC) Clock selected: TIMER_DIV1_CLOCK #define AT91C_TC_CLKS_TIMER_DIV2_CLOCK ((unsigned int) 0x1) // (TC) Clock selected: TIMER_DIV2_CLOCK #define AT91C_TC_CLKS_TIMER_DIV3_CLOCK ((unsigned int) 0x2) // (TC) Clock selected: TIMER_DIV3_CLOCK @@ -1093,102 +1112,102 @@ typedef struct _AT91S_TC { #define AT91C_TC_CLKS_XC0 ((unsigned int) 0x5) // (TC) Clock selected: XC0 #define AT91C_TC_CLKS_XC1 ((unsigned int) 0x6) // (TC) Clock selected: XC1 #define AT91C_TC_CLKS_XC2 ((unsigned int) 0x7) // (TC) Clock selected: XC2 -#define AT91C_TC_CLKI ((unsigned int) 0x1 << 3) // (TC) Clock Invert -#define AT91C_TC_BURST ((unsigned int) 0x3 << 4) // (TC) Burst Signal Selection -#define AT91C_TC_BURST_NONE ((unsigned int) 0x0 << 4) // (TC) The clock is not gated by an external signal -#define AT91C_TC_BURST_XC0 ((unsigned int) 0x1 << 4) // (TC) XC0 is ANDed with the selected clock -#define AT91C_TC_BURST_XC1 ((unsigned int) 0x2 << 4) // (TC) XC1 is ANDed with the selected clock -#define AT91C_TC_BURST_XC2 ((unsigned int) 0x3 << 4) // (TC) XC2 is ANDed with the selected clock -#define AT91C_TC_CPCSTOP ((unsigned int) 0x1 << 6) // (TC) Counter Clock Stopped with RC Compare -#define AT91C_TC_LDBSTOP ((unsigned int) 0x1 << 6) // (TC) Counter Clock Stopped with RB Loading -#define AT91C_TC_CPCDIS ((unsigned int) 0x1 << 7) // (TC) Counter Clock Disable with RC Compare -#define AT91C_TC_LDBDIS ((unsigned int) 0x1 << 7) // (TC) Counter Clock Disabled with RB Loading -#define AT91C_TC_ETRGEDG ((unsigned int) 0x3 << 8) // (TC) External Trigger Edge Selection -#define AT91C_TC_ETRGEDG_NONE ((unsigned int) 0x0 << 8) // (TC) Edge: None -#define AT91C_TC_ETRGEDG_RISING ((unsigned int) 0x1 << 8) // (TC) Edge: rising edge -#define AT91C_TC_ETRGEDG_FALLING ((unsigned int) 0x2 << 8) // (TC) Edge: falling edge -#define AT91C_TC_ETRGEDG_BOTH ((unsigned int) 0x3 << 8) // (TC) Edge: each edge -#define AT91C_TC_EEVTEDG ((unsigned int) 0x3 << 8) // (TC) External Event Edge Selection -#define AT91C_TC_EEVTEDG_NONE ((unsigned int) 0x0 << 8) // (TC) Edge: None -#define AT91C_TC_EEVTEDG_RISING ((unsigned int) 0x1 << 8) // (TC) Edge: rising edge -#define AT91C_TC_EEVTEDG_FALLING ((unsigned int) 0x2 << 8) // (TC) Edge: falling edge -#define AT91C_TC_EEVTEDG_BOTH ((unsigned int) 0x3 << 8) // (TC) Edge: each edge -#define AT91C_TC_EEVT ((unsigned int) 0x3 << 10) // (TC) External Event Selection -#define AT91C_TC_EEVT_TIOB ((unsigned int) 0x0 << 10) // (TC) Signal selected as external event: TIOB TIOB direction: input -#define AT91C_TC_EEVT_XC0 ((unsigned int) 0x1 << 10) // (TC) Signal selected as external event: XC0 TIOB direction: output -#define AT91C_TC_EEVT_XC1 ((unsigned int) 0x2 << 10) // (TC) Signal selected as external event: XC1 TIOB direction: output -#define AT91C_TC_EEVT_XC2 ((unsigned int) 0x3 << 10) // (TC) Signal selected as external event: XC2 TIOB direction: output -#define AT91C_TC_ABETRG ((unsigned int) 0x1 << 10) // (TC) TIOA or TIOB External Trigger Selection -#define AT91C_TC_ENETRG ((unsigned int) 0x1 << 12) // (TC) External Event Trigger enable -#define AT91C_TC_WAVESEL ((unsigned int) 0x3 << 13) // (TC) Waveform Selection -#define AT91C_TC_WAVESEL_UP ((unsigned int) 0x0 << 13) // (TC) UP mode without atomatic trigger on RC Compare -#define AT91C_TC_WAVESEL_UPDOWN ((unsigned int) 0x1 << 13) // (TC) UPDOWN mode without automatic trigger on RC Compare -#define AT91C_TC_WAVESEL_UP_AUTO ((unsigned int) 0x2 << 13) // (TC) UP mode with automatic trigger on RC Compare -#define AT91C_TC_WAVESEL_UPDOWN_AUTO ((unsigned int) 0x3 << 13) // (TC) UPDOWN mode with automatic trigger on RC Compare -#define AT91C_TC_CPCTRG ((unsigned int) 0x1 << 14) // (TC) RC Compare Trigger Enable -#define AT91C_TC_WAVE ((unsigned int) 0x1 << 15) // (TC) -#define AT91C_TC_ACPA ((unsigned int) 0x3 << 16) // (TC) RA Compare Effect on TIOA -#define AT91C_TC_ACPA_NONE ((unsigned int) 0x0 << 16) // (TC) Effect: none -#define AT91C_TC_ACPA_SET ((unsigned int) 0x1 << 16) // (TC) Effect: set -#define AT91C_TC_ACPA_CLEAR ((unsigned int) 0x2 << 16) // (TC) Effect: clear -#define AT91C_TC_ACPA_TOGGLE ((unsigned int) 0x3 << 16) // (TC) Effect: toggle -#define AT91C_TC_LDRA ((unsigned int) 0x3 << 16) // (TC) RA Loading Selection -#define AT91C_TC_LDRA_NONE ((unsigned int) 0x0 << 16) // (TC) Edge: None -#define AT91C_TC_LDRA_RISING ((unsigned int) 0x1 << 16) // (TC) Edge: rising edge of TIOA -#define AT91C_TC_LDRA_FALLING ((unsigned int) 0x2 << 16) // (TC) Edge: falling edge of TIOA -#define AT91C_TC_LDRA_BOTH ((unsigned int) 0x3 << 16) // (TC) Edge: each edge of TIOA -#define AT91C_TC_ACPC ((unsigned int) 0x3 << 18) // (TC) RC Compare Effect on TIOA -#define AT91C_TC_ACPC_NONE ((unsigned int) 0x0 << 18) // (TC) Effect: none -#define AT91C_TC_ACPC_SET ((unsigned int) 0x1 << 18) // (TC) Effect: set -#define AT91C_TC_ACPC_CLEAR ((unsigned int) 0x2 << 18) // (TC) Effect: clear -#define AT91C_TC_ACPC_TOGGLE ((unsigned int) 0x3 << 18) // (TC) Effect: toggle -#define AT91C_TC_LDRB ((unsigned int) 0x3 << 18) // (TC) RB Loading Selection -#define AT91C_TC_LDRB_NONE ((unsigned int) 0x0 << 18) // (TC) Edge: None -#define AT91C_TC_LDRB_RISING ((unsigned int) 0x1 << 18) // (TC) Edge: rising edge of TIOA -#define AT91C_TC_LDRB_FALLING ((unsigned int) 0x2 << 18) // (TC) Edge: falling edge of TIOA -#define AT91C_TC_LDRB_BOTH ((unsigned int) 0x3 << 18) // (TC) Edge: each edge of TIOA -#define AT91C_TC_AEEVT ((unsigned int) 0x3 << 20) // (TC) External Event Effect on TIOA -#define AT91C_TC_AEEVT_NONE ((unsigned int) 0x0 << 20) // (TC) Effect: none -#define AT91C_TC_AEEVT_SET ((unsigned int) 0x1 << 20) // (TC) Effect: set -#define AT91C_TC_AEEVT_CLEAR ((unsigned int) 0x2 << 20) // (TC) Effect: clear -#define AT91C_TC_AEEVT_TOGGLE ((unsigned int) 0x3 << 20) // (TC) Effect: toggle -#define AT91C_TC_ASWTRG ((unsigned int) 0x3 << 22) // (TC) Software Trigger Effect on TIOA +#define AT91C_TC_CLKI ((unsigned int) 0x1 << 3) // (TC) Clock Invert +#define AT91C_TC_BURST ((unsigned int) 0x3 << 4) // (TC) Burst Signal Selection +#define AT91C_TC_BURST_NONE ((unsigned int) 0x0 << 4) // (TC) The clock is not gated by an external signal +#define AT91C_TC_BURST_XC0 ((unsigned int) 0x1 << 4) // (TC) XC0 is ANDed with the selected clock +#define AT91C_TC_BURST_XC1 ((unsigned int) 0x2 << 4) // (TC) XC1 is ANDed with the selected clock +#define AT91C_TC_BURST_XC2 ((unsigned int) 0x3 << 4) // (TC) XC2 is ANDed with the selected clock +#define AT91C_TC_CPCSTOP ((unsigned int) 0x1 << 6) // (TC) Counter Clock Stopped with RC Compare +#define AT91C_TC_LDBSTOP ((unsigned int) 0x1 << 6) // (TC) Counter Clock Stopped with RB Loading +#define AT91C_TC_CPCDIS ((unsigned int) 0x1 << 7) // (TC) Counter Clock Disable with RC Compare +#define AT91C_TC_LDBDIS ((unsigned int) 0x1 << 7) // (TC) Counter Clock Disabled with RB Loading +#define AT91C_TC_ETRGEDG ((unsigned int) 0x3 << 8) // (TC) External Trigger Edge Selection +#define AT91C_TC_ETRGEDG_NONE ((unsigned int) 0x0 << 8) // (TC) Edge: None +#define AT91C_TC_ETRGEDG_RISING ((unsigned int) 0x1 << 8) // (TC) Edge: rising edge +#define AT91C_TC_ETRGEDG_FALLING ((unsigned int) 0x2 << 8) // (TC) Edge: falling edge +#define AT91C_TC_ETRGEDG_BOTH ((unsigned int) 0x3 << 8) // (TC) Edge: each edge +#define AT91C_TC_EEVTEDG ((unsigned int) 0x3 << 8) // (TC) External Event Edge Selection +#define AT91C_TC_EEVTEDG_NONE ((unsigned int) 0x0 << 8) // (TC) Edge: None +#define AT91C_TC_EEVTEDG_RISING ((unsigned int) 0x1 << 8) // (TC) Edge: rising edge +#define AT91C_TC_EEVTEDG_FALLING ((unsigned int) 0x2 << 8) // (TC) Edge: falling edge +#define AT91C_TC_EEVTEDG_BOTH ((unsigned int) 0x3 << 8) // (TC) Edge: each edge +#define AT91C_TC_EEVT ((unsigned int) 0x3 << 10) // (TC) External Event Selection +#define AT91C_TC_EEVT_TIOB ((unsigned int) 0x0 << 10) // (TC) Signal selected as external event: TIOB TIOB direction: input +#define AT91C_TC_EEVT_XC0 ((unsigned int) 0x1 << 10) // (TC) Signal selected as external event: XC0 TIOB direction: output +#define AT91C_TC_EEVT_XC1 ((unsigned int) 0x2 << 10) // (TC) Signal selected as external event: XC1 TIOB direction: output +#define AT91C_TC_EEVT_XC2 ((unsigned int) 0x3 << 10) // (TC) Signal selected as external event: XC2 TIOB direction: output +#define AT91C_TC_ABETRG ((unsigned int) 0x1 << 10) // (TC) TIOA or TIOB External Trigger Selection +#define AT91C_TC_ENETRG ((unsigned int) 0x1 << 12) // (TC) External Event Trigger enable +#define AT91C_TC_WAVESEL ((unsigned int) 0x3 << 13) // (TC) Waveform Selection +#define AT91C_TC_WAVESEL_UP ((unsigned int) 0x0 << 13) // (TC) UP mode without atomatic trigger on RC Compare +#define AT91C_TC_WAVESEL_UPDOWN ((unsigned int) 0x1 << 13) // (TC) UPDOWN mode without automatic trigger on RC Compare +#define AT91C_TC_WAVESEL_UP_AUTO ((unsigned int) 0x2 << 13) // (TC) UP mode with automatic trigger on RC Compare +#define AT91C_TC_WAVESEL_UPDOWN_AUTO ((unsigned int) 0x3 << 13) // (TC) UPDOWN mode with automatic trigger on RC Compare +#define AT91C_TC_CPCTRG ((unsigned int) 0x1 << 14) // (TC) RC Compare Trigger Enable +#define AT91C_TC_WAVE ((unsigned int) 0x1 << 15) // (TC) +#define AT91C_TC_ACPA ((unsigned int) 0x3 << 16) // (TC) RA Compare Effect on TIOA +#define AT91C_TC_ACPA_NONE ((unsigned int) 0x0 << 16) // (TC) Effect: none +#define AT91C_TC_ACPA_SET ((unsigned int) 0x1 << 16) // (TC) Effect: set +#define AT91C_TC_ACPA_CLEAR ((unsigned int) 0x2 << 16) // (TC) Effect: clear +#define AT91C_TC_ACPA_TOGGLE ((unsigned int) 0x3 << 16) // (TC) Effect: toggle +#define AT91C_TC_LDRA ((unsigned int) 0x3 << 16) // (TC) RA Loading Selection +#define AT91C_TC_LDRA_NONE ((unsigned int) 0x0 << 16) // (TC) Edge: None +#define AT91C_TC_LDRA_RISING ((unsigned int) 0x1 << 16) // (TC) Edge: rising edge of TIOA +#define AT91C_TC_LDRA_FALLING ((unsigned int) 0x2 << 16) // (TC) Edge: falling edge of TIOA +#define AT91C_TC_LDRA_BOTH ((unsigned int) 0x3 << 16) // (TC) Edge: each edge of TIOA +#define AT91C_TC_ACPC ((unsigned int) 0x3 << 18) // (TC) RC Compare Effect on TIOA +#define AT91C_TC_ACPC_NONE ((unsigned int) 0x0 << 18) // (TC) Effect: none +#define AT91C_TC_ACPC_SET ((unsigned int) 0x1 << 18) // (TC) Effect: set +#define AT91C_TC_ACPC_CLEAR ((unsigned int) 0x2 << 18) // (TC) Effect: clear +#define AT91C_TC_ACPC_TOGGLE ((unsigned int) 0x3 << 18) // (TC) Effect: toggle +#define AT91C_TC_LDRB ((unsigned int) 0x3 << 18) // (TC) RB Loading Selection +#define AT91C_TC_LDRB_NONE ((unsigned int) 0x0 << 18) // (TC) Edge: None +#define AT91C_TC_LDRB_RISING ((unsigned int) 0x1 << 18) // (TC) Edge: rising edge of TIOA +#define AT91C_TC_LDRB_FALLING ((unsigned int) 0x2 << 18) // (TC) Edge: falling edge of TIOA +#define AT91C_TC_LDRB_BOTH ((unsigned int) 0x3 << 18) // (TC) Edge: each edge of TIOA +#define AT91C_TC_AEEVT ((unsigned int) 0x3 << 20) // (TC) External Event Effect on TIOA +#define AT91C_TC_AEEVT_NONE ((unsigned int) 0x0 << 20) // (TC) Effect: none +#define AT91C_TC_AEEVT_SET ((unsigned int) 0x1 << 20) // (TC) Effect: set +#define AT91C_TC_AEEVT_CLEAR ((unsigned int) 0x2 << 20) // (TC) Effect: clear +#define AT91C_TC_AEEVT_TOGGLE ((unsigned int) 0x3 << 20) // (TC) Effect: toggle +#define AT91C_TC_ASWTRG ((unsigned int) 0x3 << 22) // (TC) Software Trigger Effect on TIOA #define AT91C_TC_ASWTRG_NONE ((unsigned int) 0x0 << 22) // (TC) Effect: none #define AT91C_TC_ASWTRG_SET ((unsigned int) 0x1 << 22) // (TC) Effect: set #define AT91C_TC_ASWTRG_CLEAR ((unsigned int) 0x2 << 22) // (TC) Effect: clear #define AT91C_TC_ASWTRG_TOGGLE ((unsigned int) 0x3 << 22) // (TC) Effect: toggle -#define AT91C_TC_BCPB ((unsigned int) 0x3 << 24) // (TC) RB Compare Effect on TIOB -#define AT91C_TC_BCPB_NONE ((unsigned int) 0x0 << 24) // (TC) Effect: none -#define AT91C_TC_BCPB_SET ((unsigned int) 0x1 << 24) // (TC) Effect: set -#define AT91C_TC_BCPB_CLEAR ((unsigned int) 0x2 << 24) // (TC) Effect: clear -#define AT91C_TC_BCPB_TOGGLE ((unsigned int) 0x3 << 24) // (TC) Effect: toggle -#define AT91C_TC_BCPC ((unsigned int) 0x3 << 26) // (TC) RC Compare Effect on TIOB -#define AT91C_TC_BCPC_NONE ((unsigned int) 0x0 << 26) // (TC) Effect: none -#define AT91C_TC_BCPC_SET ((unsigned int) 0x1 << 26) // (TC) Effect: set -#define AT91C_TC_BCPC_CLEAR ((unsigned int) 0x2 << 26) // (TC) Effect: clear -#define AT91C_TC_BCPC_TOGGLE ((unsigned int) 0x3 << 26) // (TC) Effect: toggle -#define AT91C_TC_BEEVT ((unsigned int) 0x3 << 28) // (TC) External Event Effect on TIOB -#define AT91C_TC_BEEVT_NONE ((unsigned int) 0x0 << 28) // (TC) Effect: none -#define AT91C_TC_BEEVT_SET ((unsigned int) 0x1 << 28) // (TC) Effect: set -#define AT91C_TC_BEEVT_CLEAR ((unsigned int) 0x2 << 28) // (TC) Effect: clear -#define AT91C_TC_BEEVT_TOGGLE ((unsigned int) 0x3 << 28) // (TC) Effect: toggle -#define AT91C_TC_BSWTRG ((unsigned int) 0x3 << 30) // (TC) Software Trigger Effect on TIOB +#define AT91C_TC_BCPB ((unsigned int) 0x3 << 24) // (TC) RB Compare Effect on TIOB +#define AT91C_TC_BCPB_NONE ((unsigned int) 0x0 << 24) // (TC) Effect: none +#define AT91C_TC_BCPB_SET ((unsigned int) 0x1 << 24) // (TC) Effect: set +#define AT91C_TC_BCPB_CLEAR ((unsigned int) 0x2 << 24) // (TC) Effect: clear +#define AT91C_TC_BCPB_TOGGLE ((unsigned int) 0x3 << 24) // (TC) Effect: toggle +#define AT91C_TC_BCPC ((unsigned int) 0x3 << 26) // (TC) RC Compare Effect on TIOB +#define AT91C_TC_BCPC_NONE ((unsigned int) 0x0 << 26) // (TC) Effect: none +#define AT91C_TC_BCPC_SET ((unsigned int) 0x1 << 26) // (TC) Effect: set +#define AT91C_TC_BCPC_CLEAR ((unsigned int) 0x2 << 26) // (TC) Effect: clear +#define AT91C_TC_BCPC_TOGGLE ((unsigned int) 0x3 << 26) // (TC) Effect: toggle +#define AT91C_TC_BEEVT ((unsigned int) 0x3 << 28) // (TC) External Event Effect on TIOB +#define AT91C_TC_BEEVT_NONE ((unsigned int) 0x0 << 28) // (TC) Effect: none +#define AT91C_TC_BEEVT_SET ((unsigned int) 0x1 << 28) // (TC) Effect: set +#define AT91C_TC_BEEVT_CLEAR ((unsigned int) 0x2 << 28) // (TC) Effect: clear +#define AT91C_TC_BEEVT_TOGGLE ((unsigned int) 0x3 << 28) // (TC) Effect: toggle +#define AT91C_TC_BSWTRG ((unsigned int) 0x3 << 30) // (TC) Software Trigger Effect on TIOB #define AT91C_TC_BSWTRG_NONE ((unsigned int) 0x0 << 30) // (TC) Effect: none #define AT91C_TC_BSWTRG_SET ((unsigned int) 0x1 << 30) // (TC) Effect: set #define AT91C_TC_BSWTRG_CLEAR ((unsigned int) 0x2 << 30) // (TC) Effect: clear #define AT91C_TC_BSWTRG_TOGGLE ((unsigned int) 0x3 << 30) // (TC) Effect: toggle // -------- TC_SR : (TC Offset: 0x20) TC Channel Status Register -------- -#define AT91C_TC_COVFS ((unsigned int) 0x1 << 0) // (TC) Counter Overflow -#define AT91C_TC_LOVRS ((unsigned int) 0x1 << 1) // (TC) Load Overrun -#define AT91C_TC_CPAS ((unsigned int) 0x1 << 2) // (TC) RA Compare -#define AT91C_TC_CPBS ((unsigned int) 0x1 << 3) // (TC) RB Compare -#define AT91C_TC_CPCS ((unsigned int) 0x1 << 4) // (TC) RC Compare -#define AT91C_TC_LDRAS ((unsigned int) 0x1 << 5) // (TC) RA Loading -#define AT91C_TC_LDRBS ((unsigned int) 0x1 << 6) // (TC) RB Loading -#define AT91C_TC_ETRGS ((unsigned int) 0x1 << 7) // (TC) External Trigger -#define AT91C_TC_CLKSTA ((unsigned int) 0x1 << 16) // (TC) Clock Enabling -#define AT91C_TC_MTIOA ((unsigned int) 0x1 << 17) // (TC) TIOA Mirror -#define AT91C_TC_MTIOB ((unsigned int) 0x1 << 18) // (TC) TIOA Mirror +#define AT91C_TC_COVFS ((unsigned int) 0x1 << 0) // (TC) Counter Overflow +#define AT91C_TC_LOVRS ((unsigned int) 0x1 << 1) // (TC) Load Overrun +#define AT91C_TC_CPAS ((unsigned int) 0x1 << 2) // (TC) RA Compare +#define AT91C_TC_CPBS ((unsigned int) 0x1 << 3) // (TC) RB Compare +#define AT91C_TC_CPCS ((unsigned int) 0x1 << 4) // (TC) RC Compare +#define AT91C_TC_LDRAS ((unsigned int) 0x1 << 5) // (TC) RA Loading +#define AT91C_TC_LDRBS ((unsigned int) 0x1 << 6) // (TC) RB Loading +#define AT91C_TC_ETRGS ((unsigned int) 0x1 << 7) // (TC) External Trigger +#define AT91C_TC_CLKSTA ((unsigned int) 0x1 << 16) // (TC) Clock Enabling +#define AT91C_TC_MTIOA ((unsigned int) 0x1 << 17) // (TC) TIOA Mirror +#define AT91C_TC_MTIOB ((unsigned int) 0x1 << 18) // (TC) TIOA Mirror // -------- TC_IER : (TC Offset: 0x24) TC Channel Interrupt Enable Register -------- // -------- TC_IDR : (TC Offset: 0x28) TC Channel Interrupt Disable Register -------- // -------- TC_IMR : (TC Offset: 0x2c) TC Channel Interrupt Mask Register -------- @@ -1197,98 +1216,101 @@ typedef struct _AT91S_TC { // SOFTWARE API DEFINITION FOR Timer Counter Interface // ***************************************************************************** typedef struct _AT91S_TCB { - AT91S_TC TCB_TC0; // TC Channel 0 - AT91_REG Reserved0[4]; // - AT91S_TC TCB_TC1; // TC Channel 1 - AT91_REG Reserved1[4]; // - AT91S_TC TCB_TC2; // TC Channel 2 - AT91_REG Reserved2[4]; // - AT91_REG TCB_BCR; // TC Block Control Register - AT91_REG TCB_BMR; // TC Block Mode Register -} AT91S_TCB, *AT91PS_TCB; + AT91S_TC TCB_TC0; // TC Channel 0 + AT91_REG Reserved0[4]; // + AT91S_TC TCB_TC1; // TC Channel 1 + AT91_REG Reserved1[4]; // + AT91S_TC TCB_TC2; // TC Channel 2 + AT91_REG Reserved2[4]; // + AT91_REG TCB_BCR; // TC Block Control Register + AT91_REG TCB_BMR; // TC Block Mode Register +} AT91S_TCB, + *AT91PS_TCB; // -------- TCB_BCR : (TCB Offset: 0xc0) TC Block Control Register -------- -#define AT91C_TCB_SYNC ((unsigned int) 0x1 << 0) // (TCB) Synchro Command +#define AT91C_TCB_SYNC ((unsigned int) 0x1 << 0) // (TCB) Synchro Command // -------- TCB_BMR : (TCB Offset: 0xc4) TC Block Mode Register -------- -#define AT91C_TCB_TC0XC0S ((unsigned int) 0x3 << 0) // (TCB) External Clock Signal 0 Selection -#define AT91C_TCB_TC0XC0S_TCLK0 ((unsigned int) 0x0) // (TCB) TCLK0 connected to XC0 -#define AT91C_TCB_TC0XC0S_NONE ((unsigned int) 0x1) // (TCB) None signal connected to XC0 -#define AT91C_TCB_TC0XC0S_TIOA1 ((unsigned int) 0x2) // (TCB) TIOA1 connected to XC0 -#define AT91C_TCB_TC0XC0S_TIOA2 ((unsigned int) 0x3) // (TCB) TIOA2 connected to XC0 -#define AT91C_TCB_TC1XC1S ((unsigned int) 0x3 << 2) // (TCB) External Clock Signal 1 Selection -#define AT91C_TCB_TC1XC1S_TCLK1 ((unsigned int) 0x0 << 2) // (TCB) TCLK1 connected to XC1 -#define AT91C_TCB_TC1XC1S_NONE ((unsigned int) 0x1 << 2) // (TCB) None signal connected to XC1 -#define AT91C_TCB_TC1XC1S_TIOA0 ((unsigned int) 0x2 << 2) // (TCB) TIOA0 connected to XC1 -#define AT91C_TCB_TC1XC1S_TIOA2 ((unsigned int) 0x3 << 2) // (TCB) TIOA2 connected to XC1 -#define AT91C_TCB_TC2XC2S ((unsigned int) 0x3 << 4) // (TCB) External Clock Signal 2 Selection -#define AT91C_TCB_TC2XC2S_TCLK2 ((unsigned int) 0x0 << 4) // (TCB) TCLK2 connected to XC2 -#define AT91C_TCB_TC2XC2S_NONE ((unsigned int) 0x1 << 4) // (TCB) None signal connected to XC2 -#define AT91C_TCB_TC2XC2S_TIOA0 ((unsigned int) 0x2 << 4) // (TCB) TIOA0 connected to XC2 -#define AT91C_TCB_TC2XC2S_TIOA1 ((unsigned int) 0x3 << 4) // (TCB) TIOA2 connected to XC2 +#define AT91C_TCB_TC0XC0S ((unsigned int) 0x3 << 0) // (TCB) External Clock Signal 0 Selection +#define AT91C_TCB_TC0XC0S_TCLK0 ((unsigned int) 0x0) // (TCB) TCLK0 connected to XC0 +#define AT91C_TCB_TC0XC0S_NONE ((unsigned int) 0x1) // (TCB) None signal connected to XC0 +#define AT91C_TCB_TC0XC0S_TIOA1 ((unsigned int) 0x2) // (TCB) TIOA1 connected to XC0 +#define AT91C_TCB_TC0XC0S_TIOA2 ((unsigned int) 0x3) // (TCB) TIOA2 connected to XC0 +#define AT91C_TCB_TC1XC1S ((unsigned int) 0x3 << 2) // (TCB) External Clock Signal 1 Selection +#define AT91C_TCB_TC1XC1S_TCLK1 ((unsigned int) 0x0 << 2) // (TCB) TCLK1 connected to XC1 +#define AT91C_TCB_TC1XC1S_NONE ((unsigned int) 0x1 << 2) // (TCB) None signal connected to XC1 +#define AT91C_TCB_TC1XC1S_TIOA0 ((unsigned int) 0x2 << 2) // (TCB) TIOA0 connected to XC1 +#define AT91C_TCB_TC1XC1S_TIOA2 ((unsigned int) 0x3 << 2) // (TCB) TIOA2 connected to XC1 +#define AT91C_TCB_TC2XC2S ((unsigned int) 0x3 << 4) // (TCB) External Clock Signal 2 Selection +#define AT91C_TCB_TC2XC2S_TCLK2 ((unsigned int) 0x0 << 4) // (TCB) TCLK2 connected to XC2 +#define AT91C_TCB_TC2XC2S_NONE ((unsigned int) 0x1 << 4) // (TCB) None signal connected to XC2 +#define AT91C_TCB_TC2XC2S_TIOA0 ((unsigned int) 0x2 << 4) // (TCB) TIOA0 connected to XC2 +#define AT91C_TCB_TC2XC2S_TIOA1 ((unsigned int) 0x3 << 4) // (TCB) TIOA2 connected to XC2 // ***************************************************************************** // SOFTWARE API DEFINITION FOR PWMC Channel Interface // ***************************************************************************** typedef struct _AT91S_PWMC_CH { - AT91_REG PWMC_CMR; // Channel Mode Register - AT91_REG PWMC_CDTYR; // Channel Duty Cycle Register - AT91_REG PWMC_CPRDR; // Channel Period Register - AT91_REG PWMC_CCNTR; // Channel Counter Register - AT91_REG PWMC_CUPDR; // Channel Update Register - AT91_REG PWMC_Reserved[3]; // Reserved -} AT91S_PWMC_CH, *AT91PS_PWMC_CH; + AT91_REG PWMC_CMR; // Channel Mode Register + AT91_REG PWMC_CDTYR; // Channel Duty Cycle Register + AT91_REG PWMC_CPRDR; // Channel Period Register + AT91_REG PWMC_CCNTR; // Channel Counter Register + AT91_REG PWMC_CUPDR; // Channel Update Register + AT91_REG PWMC_Reserved[3]; // Reserved +} AT91S_PWMC_CH, + *AT91PS_PWMC_CH; // -------- PWMC_CMR : (PWMC_CH Offset: 0x0) PWMC Channel Mode Register -------- -#define AT91C_PWMC_CPRE ((unsigned int) 0xF << 0) // (PWMC_CH) Channel Pre-scaler : PWMC_CLKx -#define AT91C_PWMC_CPRE_MCK ((unsigned int) 0x0) // (PWMC_CH) -#define AT91C_PWMC_CPRE_MCKA ((unsigned int) 0xB) // (PWMC_CH) -#define AT91C_PWMC_CPRE_MCKB ((unsigned int) 0xC) // (PWMC_CH) -#define AT91C_PWMC_CALG ((unsigned int) 0x1 << 8) // (PWMC_CH) Channel Alignment -#define AT91C_PWMC_CPOL ((unsigned int) 0x1 << 9) // (PWMC_CH) Channel Polarity -#define AT91C_PWMC_CPD ((unsigned int) 0x1 << 10) // (PWMC_CH) Channel Update Period +#define AT91C_PWMC_CPRE ((unsigned int) 0xF << 0) // (PWMC_CH) Channel Pre-scaler : PWMC_CLKx +#define AT91C_PWMC_CPRE_MCK ((unsigned int) 0x0) // (PWMC_CH) +#define AT91C_PWMC_CPRE_MCKA ((unsigned int) 0xB) // (PWMC_CH) +#define AT91C_PWMC_CPRE_MCKB ((unsigned int) 0xC) // (PWMC_CH) +#define AT91C_PWMC_CALG ((unsigned int) 0x1 << 8) // (PWMC_CH) Channel Alignment +#define AT91C_PWMC_CPOL ((unsigned int) 0x1 << 9) // (PWMC_CH) Channel Polarity +#define AT91C_PWMC_CPD ((unsigned int) 0x1 << 10) // (PWMC_CH) Channel Update Period // -------- PWMC_CDTYR : (PWMC_CH Offset: 0x4) PWMC Channel Duty Cycle Register -------- -#define AT91C_PWMC_CDTY ((unsigned int) 0x0 << 0) // (PWMC_CH) Channel Duty Cycle +#define AT91C_PWMC_CDTY ((unsigned int) 0x0 << 0) // (PWMC_CH) Channel Duty Cycle // -------- PWMC_CPRDR : (PWMC_CH Offset: 0x8) PWMC Channel Period Register -------- -#define AT91C_PWMC_CPRD ((unsigned int) 0x0 << 0) // (PWMC_CH) Channel Period +#define AT91C_PWMC_CPRD ((unsigned int) 0x0 << 0) // (PWMC_CH) Channel Period // -------- PWMC_CCNTR : (PWMC_CH Offset: 0xc) PWMC Channel Counter Register -------- -#define AT91C_PWMC_CCNT ((unsigned int) 0x0 << 0) // (PWMC_CH) Channel Counter +#define AT91C_PWMC_CCNT ((unsigned int) 0x0 << 0) // (PWMC_CH) Channel Counter // -------- PWMC_CUPDR : (PWMC_CH Offset: 0x10) PWMC Channel Update Register -------- -#define AT91C_PWMC_CUPD ((unsigned int) 0x0 << 0) // (PWMC_CH) Channel Update +#define AT91C_PWMC_CUPD ((unsigned int) 0x0 << 0) // (PWMC_CH) Channel Update // ***************************************************************************** // SOFTWARE API DEFINITION FOR Pulse Width Modulation Controller Interface // ***************************************************************************** typedef struct _AT91S_PWMC { - AT91_REG PWMC_MR; // PWMC Mode Register - AT91_REG PWMC_ENA; // PWMC Enable Register - AT91_REG PWMC_DIS; // PWMC Disable Register - AT91_REG PWMC_SR; // PWMC Status Register - AT91_REG PWMC_IER; // PWMC Interrupt Enable Register - AT91_REG PWMC_IDR; // PWMC Interrupt Disable Register - AT91_REG PWMC_IMR; // PWMC Interrupt Mask Register - AT91_REG PWMC_ISR; // PWMC Interrupt Status Register - AT91_REG Reserved0[55]; // - AT91_REG PWMC_VR; // PWMC Version Register - AT91_REG Reserved1[64]; // - AT91S_PWMC_CH PWMC_CH[32]; // PWMC Channel 0 -} AT91S_PWMC, *AT91PS_PWMC; + AT91_REG PWMC_MR; // PWMC Mode Register + AT91_REG PWMC_ENA; // PWMC Enable Register + AT91_REG PWMC_DIS; // PWMC Disable Register + AT91_REG PWMC_SR; // PWMC Status Register + AT91_REG PWMC_IER; // PWMC Interrupt Enable Register + AT91_REG PWMC_IDR; // PWMC Interrupt Disable Register + AT91_REG PWMC_IMR; // PWMC Interrupt Mask Register + AT91_REG PWMC_ISR; // PWMC Interrupt Status Register + AT91_REG Reserved0[55]; // + AT91_REG PWMC_VR; // PWMC Version Register + AT91_REG Reserved1[64]; // + AT91S_PWMC_CH PWMC_CH[32]; // PWMC Channel 0 +} AT91S_PWMC, + *AT91PS_PWMC; // -------- PWMC_MR : (PWMC Offset: 0x0) PWMC Mode Register -------- -#define AT91C_PWMC_DIVA ((unsigned int) 0xFF << 0) // (PWMC) CLKA divide factor. -#define AT91C_PWMC_PREA ((unsigned int) 0xF << 8) // (PWMC) Divider Input Clock Prescaler A -#define AT91C_PWMC_PREA_MCK ((unsigned int) 0x0 << 8) // (PWMC) -#define AT91C_PWMC_DIVB ((unsigned int) 0xFF << 16) // (PWMC) CLKB divide factor. -#define AT91C_PWMC_PREB ((unsigned int) 0xF << 24) // (PWMC) Divider Input Clock Prescaler B -#define AT91C_PWMC_PREB_MCK ((unsigned int) 0x0 << 24) // (PWMC) +#define AT91C_PWMC_DIVA ((unsigned int) 0xFF << 0) // (PWMC) CLKA divide factor. +#define AT91C_PWMC_PREA ((unsigned int) 0xF << 8) // (PWMC) Divider Input Clock Prescaler A +#define AT91C_PWMC_PREA_MCK ((unsigned int) 0x0 << 8) // (PWMC) +#define AT91C_PWMC_DIVB ((unsigned int) 0xFF << 16) // (PWMC) CLKB divide factor. +#define AT91C_PWMC_PREB ((unsigned int) 0xF << 24) // (PWMC) Divider Input Clock Prescaler B +#define AT91C_PWMC_PREB_MCK ((unsigned int) 0x0 << 24) // (PWMC) // -------- PWMC_ENA : (PWMC Offset: 0x4) PWMC Enable Register -------- -#define AT91C_PWMC_CHID0 ((unsigned int) 0x1 << 0) // (PWMC) Channel ID 0 -#define AT91C_PWMC_CHID1 ((unsigned int) 0x1 << 1) // (PWMC) Channel ID 1 -#define AT91C_PWMC_CHID2 ((unsigned int) 0x1 << 2) // (PWMC) Channel ID 2 -#define AT91C_PWMC_CHID3 ((unsigned int) 0x1 << 3) // (PWMC) Channel ID 3 -#define AT91C_PWMC_CHID4 ((unsigned int) 0x1 << 4) // (PWMC) Channel ID 4 -#define AT91C_PWMC_CHID5 ((unsigned int) 0x1 << 5) // (PWMC) Channel ID 5 -#define AT91C_PWMC_CHID6 ((unsigned int) 0x1 << 6) // (PWMC) Channel ID 6 -#define AT91C_PWMC_CHID7 ((unsigned int) 0x1 << 7) // (PWMC) Channel ID 7 +#define AT91C_PWMC_CHID0 ((unsigned int) 0x1 << 0) // (PWMC) Channel ID 0 +#define AT91C_PWMC_CHID1 ((unsigned int) 0x1 << 1) // (PWMC) Channel ID 1 +#define AT91C_PWMC_CHID2 ((unsigned int) 0x1 << 2) // (PWMC) Channel ID 2 +#define AT91C_PWMC_CHID3 ((unsigned int) 0x1 << 3) // (PWMC) Channel ID 3 +#define AT91C_PWMC_CHID4 ((unsigned int) 0x1 << 4) // (PWMC) Channel ID 4 +#define AT91C_PWMC_CHID5 ((unsigned int) 0x1 << 5) // (PWMC) Channel ID 5 +#define AT91C_PWMC_CHID6 ((unsigned int) 0x1 << 6) // (PWMC) Channel ID 6 +#define AT91C_PWMC_CHID7 ((unsigned int) 0x1 << 7) // (PWMC) Channel ID 7 // -------- PWMC_DIS : (PWMC Offset: 0x8) PWMC Disable Register -------- // -------- PWMC_SR : (PWMC Offset: 0xc) PWMC Status Register -------- // -------- PWMC_IER : (PWMC Offset: 0x10) PWMC Interrupt Enable Register -------- @@ -1300,621 +1322,622 @@ typedef struct _AT91S_PWMC { // SOFTWARE API DEFINITION FOR USB Device Interface // ***************************************************************************** typedef struct _AT91S_UDP { - AT91_REG UDP_NUM; // Frame Number Register - AT91_REG UDP_GLBSTATE; // Global State Register - AT91_REG UDP_FADDR; // Function Address Register - AT91_REG Reserved0[1]; // - AT91_REG UDP_IER; // Interrupt Enable Register - AT91_REG UDP_IDR; // Interrupt Disable Register - AT91_REG UDP_IMR; // Interrupt Mask Register - AT91_REG UDP_ISR; // Interrupt Status Register - AT91_REG UDP_ICR; // Interrupt Clear Register - AT91_REG Reserved1[1]; // - AT91_REG UDP_RSTEP; // Reset Endpoint Register - AT91_REG Reserved2[1]; // - AT91_REG UDP_CSR[8]; // Endpoint Control and Status Register - AT91_REG UDP_FDR[8]; // Endpoint FIFO Data Register - AT91_REG Reserved3[1]; // - AT91_REG UDP_TXVC; // Transceiver Control Register -} AT91S_UDP, *AT91PS_UDP; + AT91_REG UDP_NUM; // Frame Number Register + AT91_REG UDP_GLBSTATE; // Global State Register + AT91_REG UDP_FADDR; // Function Address Register + AT91_REG Reserved0[1]; // + AT91_REG UDP_IER; // Interrupt Enable Register + AT91_REG UDP_IDR; // Interrupt Disable Register + AT91_REG UDP_IMR; // Interrupt Mask Register + AT91_REG UDP_ISR; // Interrupt Status Register + AT91_REG UDP_ICR; // Interrupt Clear Register + AT91_REG Reserved1[1]; // + AT91_REG UDP_RSTEP; // Reset Endpoint Register + AT91_REG Reserved2[1]; // + AT91_REG UDP_CSR[8]; // Endpoint Control and Status Register + AT91_REG UDP_FDR[8]; // Endpoint FIFO Data Register + AT91_REG Reserved3[1]; // + AT91_REG UDP_TXVC; // Transceiver Control Register +} AT91S_UDP, + *AT91PS_UDP; // -------- UDP_FRM_NUM : (UDP Offset: 0x0) USB Frame Number Register -------- -#define AT91C_UDP_FRM_NUM ((unsigned int) 0x7FF << 0) // (UDP) Frame Number as Defined in the Packet Field Formats -#define AT91C_UDP_FRM_ERR ((unsigned int) 0x1 << 16) // (UDP) Frame Error -#define AT91C_UDP_FRM_OK ((unsigned int) 0x1 << 17) // (UDP) Frame OK +#define AT91C_UDP_FRM_NUM ((unsigned int) 0x7FF << 0) // (UDP) Frame Number as Defined in the Packet Field Formats +#define AT91C_UDP_FRM_ERR ((unsigned int) 0x1 << 16) // (UDP) Frame Error +#define AT91C_UDP_FRM_OK ((unsigned int) 0x1 << 17) // (UDP) Frame OK // -------- UDP_GLB_STATE : (UDP Offset: 0x4) USB Global State Register -------- -#define AT91C_UDP_FADDEN ((unsigned int) 0x1 << 0) // (UDP) Function Address Enable -#define AT91C_UDP_CONFG ((unsigned int) 0x1 << 1) // (UDP) Configured -#define AT91C_UDP_ESR ((unsigned int) 0x1 << 2) // (UDP) Enable Send Resume -#define AT91C_UDP_RSMINPR ((unsigned int) 0x1 << 3) // (UDP) A Resume Has Been Sent to the Host -#define AT91C_UDP_RMWUPE ((unsigned int) 0x1 << 4) // (UDP) Remote Wake Up Enable +#define AT91C_UDP_FADDEN ((unsigned int) 0x1 << 0) // (UDP) Function Address Enable +#define AT91C_UDP_CONFG ((unsigned int) 0x1 << 1) // (UDP) Configured +#define AT91C_UDP_ESR ((unsigned int) 0x1 << 2) // (UDP) Enable Send Resume +#define AT91C_UDP_RSMINPR ((unsigned int) 0x1 << 3) // (UDP) A Resume Has Been Sent to the Host +#define AT91C_UDP_RMWUPE ((unsigned int) 0x1 << 4) // (UDP) Remote Wake Up Enable // -------- UDP_FADDR : (UDP Offset: 0x8) USB Function Address Register -------- -#define AT91C_UDP_FADD ((unsigned int) 0xFF << 0) // (UDP) Function Address Value -#define AT91C_UDP_FEN ((unsigned int) 0x1 << 8) // (UDP) Function Enable +#define AT91C_UDP_FADD ((unsigned int) 0xFF << 0) // (UDP) Function Address Value +#define AT91C_UDP_FEN ((unsigned int) 0x1 << 8) // (UDP) Function Enable // -------- UDP_IER : (UDP Offset: 0x10) USB Interrupt Enable Register -------- -#define AT91C_UDP_EPINT0 ((unsigned int) 0x1 << 0) // (UDP) Endpoint 0 Interrupt -#define AT91C_UDP_EPINT1 ((unsigned int) 0x1 << 1) // (UDP) Endpoint 0 Interrupt -#define AT91C_UDP_EPINT2 ((unsigned int) 0x1 << 2) // (UDP) Endpoint 2 Interrupt -#define AT91C_UDP_EPINT3 ((unsigned int) 0x1 << 3) // (UDP) Endpoint 3 Interrupt -#define AT91C_UDP_EPINT4 ((unsigned int) 0x1 << 4) // (UDP) Endpoint 4 Interrupt -#define AT91C_UDP_EPINT5 ((unsigned int) 0x1 << 5) // (UDP) Endpoint 5 Interrupt -#define AT91C_UDP_EPINT6 ((unsigned int) 0x1 << 6) // (UDP) Endpoint 6 Interrupt -#define AT91C_UDP_EPINT7 ((unsigned int) 0x1 << 7) // (UDP) Endpoint 7 Interrupt -#define AT91C_UDP_RXSUSP ((unsigned int) 0x1 << 8) // (UDP) USB Suspend Interrupt -#define AT91C_UDP_RXRSM ((unsigned int) 0x1 << 9) // (UDP) USB Resume Interrupt -#define AT91C_UDP_EXTRSM ((unsigned int) 0x1 << 10) // (UDP) USB External Resume Interrupt -#define AT91C_UDP_SOFINT ((unsigned int) 0x1 << 11) // (UDP) USB Start Of frame Interrupt -#define AT91C_UDP_WAKEUP ((unsigned int) 0x1 << 13) // (UDP) USB Resume Interrupt +#define AT91C_UDP_EPINT0 ((unsigned int) 0x1 << 0) // (UDP) Endpoint 0 Interrupt +#define AT91C_UDP_EPINT1 ((unsigned int) 0x1 << 1) // (UDP) Endpoint 0 Interrupt +#define AT91C_UDP_EPINT2 ((unsigned int) 0x1 << 2) // (UDP) Endpoint 2 Interrupt +#define AT91C_UDP_EPINT3 ((unsigned int) 0x1 << 3) // (UDP) Endpoint 3 Interrupt +#define AT91C_UDP_EPINT4 ((unsigned int) 0x1 << 4) // (UDP) Endpoint 4 Interrupt +#define AT91C_UDP_EPINT5 ((unsigned int) 0x1 << 5) // (UDP) Endpoint 5 Interrupt +#define AT91C_UDP_EPINT6 ((unsigned int) 0x1 << 6) // (UDP) Endpoint 6 Interrupt +#define AT91C_UDP_EPINT7 ((unsigned int) 0x1 << 7) // (UDP) Endpoint 7 Interrupt +#define AT91C_UDP_RXSUSP ((unsigned int) 0x1 << 8) // (UDP) USB Suspend Interrupt +#define AT91C_UDP_RXRSM ((unsigned int) 0x1 << 9) // (UDP) USB Resume Interrupt +#define AT91C_UDP_EXTRSM ((unsigned int) 0x1 << 10) // (UDP) USB External Resume Interrupt +#define AT91C_UDP_SOFINT ((unsigned int) 0x1 << 11) // (UDP) USB Start Of frame Interrupt +#define AT91C_UDP_WAKEUP ((unsigned int) 0x1 << 13) // (UDP) USB Resume Interrupt // -------- UDP_IDR : (UDP Offset: 0x14) USB Interrupt Disable Register -------- // -------- UDP_IMR : (UDP Offset: 0x18) USB Interrupt Mask Register -------- // -------- UDP_ISR : (UDP Offset: 0x1c) USB Interrupt Status Register -------- -#define AT91C_UDP_ENDBUSRES ((unsigned int) 0x1 << 12) // (UDP) USB End Of Bus Reset Interrupt +#define AT91C_UDP_ENDBUSRES ((unsigned int) 0x1 << 12) // (UDP) USB End Of Bus Reset Interrupt // -------- UDP_ICR : (UDP Offset: 0x20) USB Interrupt Clear Register -------- // -------- UDP_RST_EP : (UDP Offset: 0x28) USB Reset Endpoint Register -------- -#define AT91C_UDP_EP0 ((unsigned int) 0x1 << 0) // (UDP) Reset Endpoint 0 -#define AT91C_UDP_EP1 ((unsigned int) 0x1 << 1) // (UDP) Reset Endpoint 1 -#define AT91C_UDP_EP2 ((unsigned int) 0x1 << 2) // (UDP) Reset Endpoint 2 -#define AT91C_UDP_EP3 ((unsigned int) 0x1 << 3) // (UDP) Reset Endpoint 3 -#define AT91C_UDP_EP4 ((unsigned int) 0x1 << 4) // (UDP) Reset Endpoint 4 -#define AT91C_UDP_EP5 ((unsigned int) 0x1 << 5) // (UDP) Reset Endpoint 5 -#define AT91C_UDP_EP6 ((unsigned int) 0x1 << 6) // (UDP) Reset Endpoint 6 -#define AT91C_UDP_EP7 ((unsigned int) 0x1 << 7) // (UDP) Reset Endpoint 7 +#define AT91C_UDP_EP0 ((unsigned int) 0x1 << 0) // (UDP) Reset Endpoint 0 +#define AT91C_UDP_EP1 ((unsigned int) 0x1 << 1) // (UDP) Reset Endpoint 1 +#define AT91C_UDP_EP2 ((unsigned int) 0x1 << 2) // (UDP) Reset Endpoint 2 +#define AT91C_UDP_EP3 ((unsigned int) 0x1 << 3) // (UDP) Reset Endpoint 3 +#define AT91C_UDP_EP4 ((unsigned int) 0x1 << 4) // (UDP) Reset Endpoint 4 +#define AT91C_UDP_EP5 ((unsigned int) 0x1 << 5) // (UDP) Reset Endpoint 5 +#define AT91C_UDP_EP6 ((unsigned int) 0x1 << 6) // (UDP) Reset Endpoint 6 +#define AT91C_UDP_EP7 ((unsigned int) 0x1 << 7) // (UDP) Reset Endpoint 7 // -------- UDP_CSR : (UDP Offset: 0x30) USB Endpoint Control and Status Register -------- -#define AT91C_UDP_TXCOMP ((unsigned int) 0x1 << 0) // (UDP) Generates an IN packet with data previously written in the DPR -#define AT91C_UDP_RX_DATA_BK0 ((unsigned int) 0x1 << 1) // (UDP) Receive Data Bank 0 -#define AT91C_UDP_RXSETUP ((unsigned int) 0x1 << 2) // (UDP) Sends STALL to the Host (Control endpoints) -#define AT91C_UDP_ISOERROR ((unsigned int) 0x1 << 3) // (UDP) Isochronous error (Isochronous endpoints) -#define AT91C_UDP_TXPKTRDY ((unsigned int) 0x1 << 4) // (UDP) Transmit Packet Ready -#define AT91C_UDP_FORCESTALL ((unsigned int) 0x1 << 5) // (UDP) Force Stall (used by Control, Bulk and Isochronous endpoints). -#define AT91C_UDP_RX_DATA_BK1 ((unsigned int) 0x1 << 6) // (UDP) Receive Data Bank 1 (only used by endpoints with ping-pong attributes). -#define AT91C_UDP_DIR ((unsigned int) 0x1 << 7) // (UDP) Transfer Direction -#define AT91C_UDP_EPTYPE ((unsigned int) 0x7 << 8) // (UDP) Endpoint type -#define AT91C_UDP_EPTYPE_CTRL ((unsigned int) 0x0 << 8) // (UDP) Control -#define AT91C_UDP_EPTYPE_ISO_OUT ((unsigned int) 0x1 << 8) // (UDP) Isochronous OUT -#define AT91C_UDP_EPTYPE_BULK_OUT ((unsigned int) 0x2 << 8) // (UDP) Bulk OUT -#define AT91C_UDP_EPTYPE_INT_OUT ((unsigned int) 0x3 << 8) // (UDP) Interrupt OUT -#define AT91C_UDP_EPTYPE_ISO_IN ((unsigned int) 0x5 << 8) // (UDP) Isochronous IN -#define AT91C_UDP_EPTYPE_BULK_IN ((unsigned int) 0x6 << 8) // (UDP) Bulk IN -#define AT91C_UDP_EPTYPE_INT_IN ((unsigned int) 0x7 << 8) // (UDP) Interrupt IN -#define AT91C_UDP_DTGLE ((unsigned int) 0x1 << 11) // (UDP) Data Toggle -#define AT91C_UDP_EPEDS ((unsigned int) 0x1 << 15) // (UDP) Endpoint Enable Disable -#define AT91C_UDP_RXBYTECNT ((unsigned int) 0x7FF << 16) // (UDP) Number Of Bytes Available in the FIFO +#define AT91C_UDP_TXCOMP ((unsigned int) 0x1 << 0) // (UDP) Generates an IN packet with data previously written in the DPR +#define AT91C_UDP_RX_DATA_BK0 ((unsigned int) 0x1 << 1) // (UDP) Receive Data Bank 0 +#define AT91C_UDP_RXSETUP ((unsigned int) 0x1 << 2) // (UDP) Sends STALL to the Host (Control endpoints) +#define AT91C_UDP_ISOERROR ((unsigned int) 0x1 << 3) // (UDP) Isochronous error (Isochronous endpoints) +#define AT91C_UDP_TXPKTRDY ((unsigned int) 0x1 << 4) // (UDP) Transmit Packet Ready +#define AT91C_UDP_FORCESTALL ((unsigned int) 0x1 << 5) // (UDP) Force Stall (used by Control, Bulk and Isochronous endpoints). +#define AT91C_UDP_RX_DATA_BK1 ((unsigned int) 0x1 << 6) // (UDP) Receive Data Bank 1 (only used by endpoints with ping-pong attributes). +#define AT91C_UDP_DIR ((unsigned int) 0x1 << 7) // (UDP) Transfer Direction +#define AT91C_UDP_EPTYPE ((unsigned int) 0x7 << 8) // (UDP) Endpoint type +#define AT91C_UDP_EPTYPE_CTRL ((unsigned int) 0x0 << 8) // (UDP) Control +#define AT91C_UDP_EPTYPE_ISO_OUT ((unsigned int) 0x1 << 8) // (UDP) Isochronous OUT +#define AT91C_UDP_EPTYPE_BULK_OUT ((unsigned int) 0x2 << 8) // (UDP) Bulk OUT +#define AT91C_UDP_EPTYPE_INT_OUT ((unsigned int) 0x3 << 8) // (UDP) Interrupt OUT +#define AT91C_UDP_EPTYPE_ISO_IN ((unsigned int) 0x5 << 8) // (UDP) Isochronous IN +#define AT91C_UDP_EPTYPE_BULK_IN ((unsigned int) 0x6 << 8) // (UDP) Bulk IN +#define AT91C_UDP_EPTYPE_INT_IN ((unsigned int) 0x7 << 8) // (UDP) Interrupt IN +#define AT91C_UDP_DTGLE ((unsigned int) 0x1 << 11) // (UDP) Data Toggle +#define AT91C_UDP_EPEDS ((unsigned int) 0x1 << 15) // (UDP) Endpoint Enable Disable +#define AT91C_UDP_RXBYTECNT ((unsigned int) 0x7FF << 16) // (UDP) Number Of Bytes Available in the FIFO // -------- UDP_TXVC : (UDP Offset: 0x74) Transceiver Control Register -------- -#define AT91C_UDP_TXVDIS ((unsigned int) 0x1 << 8) // (UDP) -#define AT91C_UDP_PUON ((unsigned int) 0x1 << 9) // (UDP) Pull-up ON +#define AT91C_UDP_TXVDIS ((unsigned int) 0x1 << 8) // (UDP) +#define AT91C_UDP_PUON ((unsigned int) 0x1 << 9) // (UDP) Pull-up ON // ***************************************************************************** // REGISTER ADDRESS DEFINITION FOR AT91SAM7S256 // ***************************************************************************** // ========== Register definition for SYS peripheral ========== // ========== Register definition for AIC peripheral ========== -#define AT91C_AIC_IVR ((AT91_REG *) 0xFFFFF100) // (AIC) IRQ Vector Register -#define AT91C_AIC_SMR ((AT91_REG *) 0xFFFFF000) // (AIC) Source Mode Register -#define AT91C_AIC_FVR ((AT91_REG *) 0xFFFFF104) // (AIC) FIQ Vector Register -#define AT91C_AIC_DCR ((AT91_REG *) 0xFFFFF138) // (AIC) Debug Control Register (Protect) -#define AT91C_AIC_EOICR ((AT91_REG *) 0xFFFFF130) // (AIC) End of Interrupt Command Register -#define AT91C_AIC_SVR ((AT91_REG *) 0xFFFFF080) // (AIC) Source Vector Register -#define AT91C_AIC_FFSR ((AT91_REG *) 0xFFFFF148) // (AIC) Fast Forcing Status Register -#define AT91C_AIC_ICCR ((AT91_REG *) 0xFFFFF128) // (AIC) Interrupt Clear Command Register -#define AT91C_AIC_ISR ((AT91_REG *) 0xFFFFF108) // (AIC) Interrupt Status Register -#define AT91C_AIC_IMR ((AT91_REG *) 0xFFFFF110) // (AIC) Interrupt Mask Register -#define AT91C_AIC_IPR ((AT91_REG *) 0xFFFFF10C) // (AIC) Interrupt Pending Register -#define AT91C_AIC_FFER ((AT91_REG *) 0xFFFFF140) // (AIC) Fast Forcing Enable Register -#define AT91C_AIC_IECR ((AT91_REG *) 0xFFFFF120) // (AIC) Interrupt Enable Command Register -#define AT91C_AIC_ISCR ((AT91_REG *) 0xFFFFF12C) // (AIC) Interrupt Set Command Register -#define AT91C_AIC_FFDR ((AT91_REG *) 0xFFFFF144) // (AIC) Fast Forcing Disable Register -#define AT91C_AIC_CISR ((AT91_REG *) 0xFFFFF114) // (AIC) Core Interrupt Status Register -#define AT91C_AIC_IDCR ((AT91_REG *) 0xFFFFF124) // (AIC) Interrupt Disable Command Register -#define AT91C_AIC_SPU ((AT91_REG *) 0xFFFFF134) // (AIC) Spurious Vector Register +#define AT91C_AIC_IVR ((AT91_REG *) 0xFFFFF100) // (AIC) IRQ Vector Register +#define AT91C_AIC_SMR ((AT91_REG *) 0xFFFFF000) // (AIC) Source Mode Register +#define AT91C_AIC_FVR ((AT91_REG *) 0xFFFFF104) // (AIC) FIQ Vector Register +#define AT91C_AIC_DCR ((AT91_REG *) 0xFFFFF138) // (AIC) Debug Control Register (Protect) +#define AT91C_AIC_EOICR ((AT91_REG *) 0xFFFFF130) // (AIC) End of Interrupt Command Register +#define AT91C_AIC_SVR ((AT91_REG *) 0xFFFFF080) // (AIC) Source Vector Register +#define AT91C_AIC_FFSR ((AT91_REG *) 0xFFFFF148) // (AIC) Fast Forcing Status Register +#define AT91C_AIC_ICCR ((AT91_REG *) 0xFFFFF128) // (AIC) Interrupt Clear Command Register +#define AT91C_AIC_ISR ((AT91_REG *) 0xFFFFF108) // (AIC) Interrupt Status Register +#define AT91C_AIC_IMR ((AT91_REG *) 0xFFFFF110) // (AIC) Interrupt Mask Register +#define AT91C_AIC_IPR ((AT91_REG *) 0xFFFFF10C) // (AIC) Interrupt Pending Register +#define AT91C_AIC_FFER ((AT91_REG *) 0xFFFFF140) // (AIC) Fast Forcing Enable Register +#define AT91C_AIC_IECR ((AT91_REG *) 0xFFFFF120) // (AIC) Interrupt Enable Command Register +#define AT91C_AIC_ISCR ((AT91_REG *) 0xFFFFF12C) // (AIC) Interrupt Set Command Register +#define AT91C_AIC_FFDR ((AT91_REG *) 0xFFFFF144) // (AIC) Fast Forcing Disable Register +#define AT91C_AIC_CISR ((AT91_REG *) 0xFFFFF114) // (AIC) Core Interrupt Status Register +#define AT91C_AIC_IDCR ((AT91_REG *) 0xFFFFF124) // (AIC) Interrupt Disable Command Register +#define AT91C_AIC_SPU ((AT91_REG *) 0xFFFFF134) // (AIC) Spurious Vector Register // ========== Register definition for PDC_DBGU peripheral ========== -#define AT91C_DBGU_TCR ((AT91_REG *) 0xFFFFF30C) // (PDC_DBGU) Transmit Counter Register -#define AT91C_DBGU_RNPR ((AT91_REG *) 0xFFFFF310) // (PDC_DBGU) Receive Next Pointer Register -#define AT91C_DBGU_TNPR ((AT91_REG *) 0xFFFFF318) // (PDC_DBGU) Transmit Next Pointer Register -#define AT91C_DBGU_TPR ((AT91_REG *) 0xFFFFF308) // (PDC_DBGU) Transmit Pointer Register -#define AT91C_DBGU_RPR ((AT91_REG *) 0xFFFFF300) // (PDC_DBGU) Receive Pointer Register -#define AT91C_DBGU_RCR ((AT91_REG *) 0xFFFFF304) // (PDC_DBGU) Receive Counter Register -#define AT91C_DBGU_RNCR ((AT91_REG *) 0xFFFFF314) // (PDC_DBGU) Receive Next Counter Register -#define AT91C_DBGU_PTCR ((AT91_REG *) 0xFFFFF320) // (PDC_DBGU) PDC Transfer Control Register -#define AT91C_DBGU_PTSR ((AT91_REG *) 0xFFFFF324) // (PDC_DBGU) PDC Transfer Status Register -#define AT91C_DBGU_TNCR ((AT91_REG *) 0xFFFFF31C) // (PDC_DBGU) Transmit Next Counter Register +#define AT91C_DBGU_TCR ((AT91_REG *) 0xFFFFF30C) // (PDC_DBGU) Transmit Counter Register +#define AT91C_DBGU_RNPR ((AT91_REG *) 0xFFFFF310) // (PDC_DBGU) Receive Next Pointer Register +#define AT91C_DBGU_TNPR ((AT91_REG *) 0xFFFFF318) // (PDC_DBGU) Transmit Next Pointer Register +#define AT91C_DBGU_TPR ((AT91_REG *) 0xFFFFF308) // (PDC_DBGU) Transmit Pointer Register +#define AT91C_DBGU_RPR ((AT91_REG *) 0xFFFFF300) // (PDC_DBGU) Receive Pointer Register +#define AT91C_DBGU_RCR ((AT91_REG *) 0xFFFFF304) // (PDC_DBGU) Receive Counter Register +#define AT91C_DBGU_RNCR ((AT91_REG *) 0xFFFFF314) // (PDC_DBGU) Receive Next Counter Register +#define AT91C_DBGU_PTCR ((AT91_REG *) 0xFFFFF320) // (PDC_DBGU) PDC Transfer Control Register +#define AT91C_DBGU_PTSR ((AT91_REG *) 0xFFFFF324) // (PDC_DBGU) PDC Transfer Status Register +#define AT91C_DBGU_TNCR ((AT91_REG *) 0xFFFFF31C) // (PDC_DBGU) Transmit Next Counter Register // ========== Register definition for DBGU peripheral ========== -#define AT91C_DBGU_EXID ((AT91_REG *) 0xFFFFF244) // (DBGU) Chip ID Extension Register -#define AT91C_DBGU_BRGR ((AT91_REG *) 0xFFFFF220) // (DBGU) Baud Rate Generator Register -#define AT91C_DBGU_IDR ((AT91_REG *) 0xFFFFF20C) // (DBGU) Interrupt Disable Register -#define AT91C_DBGU_CSR ((AT91_REG *) 0xFFFFF214) // (DBGU) Channel Status Register -#define AT91C_DBGU_CIDR ((AT91_REG *) 0xFFFFF240) // (DBGU) Chip ID Register -#define AT91C_DBGU_MR ((AT91_REG *) 0xFFFFF204) // (DBGU) Mode Register -#define AT91C_DBGU_IMR ((AT91_REG *) 0xFFFFF210) // (DBGU) Interrupt Mask Register -#define AT91C_DBGU_CR ((AT91_REG *) 0xFFFFF200) // (DBGU) Control Register -#define AT91C_DBGU_FNTR ((AT91_REG *) 0xFFFFF248) // (DBGU) Force NTRST Register -#define AT91C_DBGU_THR ((AT91_REG *) 0xFFFFF21C) // (DBGU) Transmitter Holding Register -#define AT91C_DBGU_RHR ((AT91_REG *) 0xFFFFF218) // (DBGU) Receiver Holding Register -#define AT91C_DBGU_IER ((AT91_REG *) 0xFFFFF208) // (DBGU) Interrupt Enable Register +#define AT91C_DBGU_EXID ((AT91_REG *) 0xFFFFF244) // (DBGU) Chip ID Extension Register +#define AT91C_DBGU_BRGR ((AT91_REG *) 0xFFFFF220) // (DBGU) Baud Rate Generator Register +#define AT91C_DBGU_IDR ((AT91_REG *) 0xFFFFF20C) // (DBGU) Interrupt Disable Register +#define AT91C_DBGU_CSR ((AT91_REG *) 0xFFFFF214) // (DBGU) Channel Status Register +#define AT91C_DBGU_CIDR ((AT91_REG *) 0xFFFFF240) // (DBGU) Chip ID Register +#define AT91C_DBGU_MR ((AT91_REG *) 0xFFFFF204) // (DBGU) Mode Register +#define AT91C_DBGU_IMR ((AT91_REG *) 0xFFFFF210) // (DBGU) Interrupt Mask Register +#define AT91C_DBGU_CR ((AT91_REG *) 0xFFFFF200) // (DBGU) Control Register +#define AT91C_DBGU_FNTR ((AT91_REG *) 0xFFFFF248) // (DBGU) Force NTRST Register +#define AT91C_DBGU_THR ((AT91_REG *) 0xFFFFF21C) // (DBGU) Transmitter Holding Register +#define AT91C_DBGU_RHR ((AT91_REG *) 0xFFFFF218) // (DBGU) Receiver Holding Register +#define AT91C_DBGU_IER ((AT91_REG *) 0xFFFFF208) // (DBGU) Interrupt Enable Register // ========== Register definition for PIOA peripheral ========== -#define AT91C_PIOA_ODR ((AT91_REG *) 0xFFFFF414) // (PIOA) Output Disable Registerr -#define AT91C_PIOA_SODR ((AT91_REG *) 0xFFFFF430) // (PIOA) Set Output Data Register -#define AT91C_PIOA_ISR ((AT91_REG *) 0xFFFFF44C) // (PIOA) Interrupt Status Register -#define AT91C_PIOA_ABSR ((AT91_REG *) 0xFFFFF478) // (PIOA) AB Select Status Register -#define AT91C_PIOA_IER ((AT91_REG *) 0xFFFFF440) // (PIOA) Interrupt Enable Register -#define AT91C_PIOA_PPUDR ((AT91_REG *) 0xFFFFF460) // (PIOA) Pull-up Disable Register -#define AT91C_PIOA_IMR ((AT91_REG *) 0xFFFFF448) // (PIOA) Interrupt Mask Register -#define AT91C_PIOA_PER ((AT91_REG *) 0xFFFFF400) // (PIOA) PIO Enable Register -#define AT91C_PIOA_IFDR ((AT91_REG *) 0xFFFFF424) // (PIOA) Input Filter Disable Register -#define AT91C_PIOA_OWDR ((AT91_REG *) 0xFFFFF4A4) // (PIOA) Output Write Disable Register -#define AT91C_PIOA_MDSR ((AT91_REG *) 0xFFFFF458) // (PIOA) Multi-driver Status Register -#define AT91C_PIOA_IDR ((AT91_REG *) 0xFFFFF444) // (PIOA) Interrupt Disable Register -#define AT91C_PIOA_ODSR ((AT91_REG *) 0xFFFFF438) // (PIOA) Output Data Status Register -#define AT91C_PIOA_PPUSR ((AT91_REG *) 0xFFFFF468) // (PIOA) Pull-up Status Register -#define AT91C_PIOA_OWSR ((AT91_REG *) 0xFFFFF4A8) // (PIOA) Output Write Status Register -#define AT91C_PIOA_BSR ((AT91_REG *) 0xFFFFF474) // (PIOA) Select B Register -#define AT91C_PIOA_OWER ((AT91_REG *) 0xFFFFF4A0) // (PIOA) Output Write Enable Register -#define AT91C_PIOA_IFER ((AT91_REG *) 0xFFFFF420) // (PIOA) Input Filter Enable Register -#define AT91C_PIOA_PDSR ((AT91_REG *) 0xFFFFF43C) // (PIOA) Pin Data Status Register -#define AT91C_PIOA_PPUER ((AT91_REG *) 0xFFFFF464) // (PIOA) Pull-up Enable Register -#define AT91C_PIOA_OSR ((AT91_REG *) 0xFFFFF418) // (PIOA) Output Status Register -#define AT91C_PIOA_ASR ((AT91_REG *) 0xFFFFF470) // (PIOA) Select A Register -#define AT91C_PIOA_MDDR ((AT91_REG *) 0xFFFFF454) // (PIOA) Multi-driver Disable Register -#define AT91C_PIOA_CODR ((AT91_REG *) 0xFFFFF434) // (PIOA) Clear Output Data Register -#define AT91C_PIOA_MDER ((AT91_REG *) 0xFFFFF450) // (PIOA) Multi-driver Enable Register -#define AT91C_PIOA_PDR ((AT91_REG *) 0xFFFFF404) // (PIOA) PIO Disable Register -#define AT91C_PIOA_IFSR ((AT91_REG *) 0xFFFFF428) // (PIOA) Input Filter Status Register -#define AT91C_PIOA_OER ((AT91_REG *) 0xFFFFF410) // (PIOA) Output Enable Register -#define AT91C_PIOA_PSR ((AT91_REG *) 0xFFFFF408) // (PIOA) PIO Status Register +#define AT91C_PIOA_ODR ((AT91_REG *) 0xFFFFF414) // (PIOA) Output Disable Registerr +#define AT91C_PIOA_SODR ((AT91_REG *) 0xFFFFF430) // (PIOA) Set Output Data Register +#define AT91C_PIOA_ISR ((AT91_REG *) 0xFFFFF44C) // (PIOA) Interrupt Status Register +#define AT91C_PIOA_ABSR ((AT91_REG *) 0xFFFFF478) // (PIOA) AB Select Status Register +#define AT91C_PIOA_IER ((AT91_REG *) 0xFFFFF440) // (PIOA) Interrupt Enable Register +#define AT91C_PIOA_PPUDR ((AT91_REG *) 0xFFFFF460) // (PIOA) Pull-up Disable Register +#define AT91C_PIOA_IMR ((AT91_REG *) 0xFFFFF448) // (PIOA) Interrupt Mask Register +#define AT91C_PIOA_PER ((AT91_REG *) 0xFFFFF400) // (PIOA) PIO Enable Register +#define AT91C_PIOA_IFDR ((AT91_REG *) 0xFFFFF424) // (PIOA) Input Filter Disable Register +#define AT91C_PIOA_OWDR ((AT91_REG *) 0xFFFFF4A4) // (PIOA) Output Write Disable Register +#define AT91C_PIOA_MDSR ((AT91_REG *) 0xFFFFF458) // (PIOA) Multi-driver Status Register +#define AT91C_PIOA_IDR ((AT91_REG *) 0xFFFFF444) // (PIOA) Interrupt Disable Register +#define AT91C_PIOA_ODSR ((AT91_REG *) 0xFFFFF438) // (PIOA) Output Data Status Register +#define AT91C_PIOA_PPUSR ((AT91_REG *) 0xFFFFF468) // (PIOA) Pull-up Status Register +#define AT91C_PIOA_OWSR ((AT91_REG *) 0xFFFFF4A8) // (PIOA) Output Write Status Register +#define AT91C_PIOA_BSR ((AT91_REG *) 0xFFFFF474) // (PIOA) Select B Register +#define AT91C_PIOA_OWER ((AT91_REG *) 0xFFFFF4A0) // (PIOA) Output Write Enable Register +#define AT91C_PIOA_IFER ((AT91_REG *) 0xFFFFF420) // (PIOA) Input Filter Enable Register +#define AT91C_PIOA_PDSR ((AT91_REG *) 0xFFFFF43C) // (PIOA) Pin Data Status Register +#define AT91C_PIOA_PPUER ((AT91_REG *) 0xFFFFF464) // (PIOA) Pull-up Enable Register +#define AT91C_PIOA_OSR ((AT91_REG *) 0xFFFFF418) // (PIOA) Output Status Register +#define AT91C_PIOA_ASR ((AT91_REG *) 0xFFFFF470) // (PIOA) Select A Register +#define AT91C_PIOA_MDDR ((AT91_REG *) 0xFFFFF454) // (PIOA) Multi-driver Disable Register +#define AT91C_PIOA_CODR ((AT91_REG *) 0xFFFFF434) // (PIOA) Clear Output Data Register +#define AT91C_PIOA_MDER ((AT91_REG *) 0xFFFFF450) // (PIOA) Multi-driver Enable Register +#define AT91C_PIOA_PDR ((AT91_REG *) 0xFFFFF404) // (PIOA) PIO Disable Register +#define AT91C_PIOA_IFSR ((AT91_REG *) 0xFFFFF428) // (PIOA) Input Filter Status Register +#define AT91C_PIOA_OER ((AT91_REG *) 0xFFFFF410) // (PIOA) Output Enable Register +#define AT91C_PIOA_PSR ((AT91_REG *) 0xFFFFF408) // (PIOA) PIO Status Register // ========== Register definition for CKGR peripheral ========== -#define AT91C_CKGR_MOR ((AT91_REG *) 0xFFFFFC20) // (CKGR) Main Oscillator Register -#define AT91C_CKGR_PLLR ((AT91_REG *) 0xFFFFFC2C) // (CKGR) PLL Register -#define AT91C_CKGR_MCFR ((AT91_REG *) 0xFFFFFC24) // (CKGR) Main Clock Frequency Register +#define AT91C_CKGR_MOR ((AT91_REG *) 0xFFFFFC20) // (CKGR) Main Oscillator Register +#define AT91C_CKGR_PLLR ((AT91_REG *) 0xFFFFFC2C) // (CKGR) PLL Register +#define AT91C_CKGR_MCFR ((AT91_REG *) 0xFFFFFC24) // (CKGR) Main Clock Frequency Register // ========== Register definition for PMC peripheral ========== -#define AT91C_PMC_IDR ((AT91_REG *) 0xFFFFFC64) // (PMC) Interrupt Disable Register -#define AT91C_PMC_MOR ((AT91_REG *) 0xFFFFFC20) // (PMC) Main Oscillator Register -#define AT91C_PMC_PLLR ((AT91_REG *) 0xFFFFFC2C) // (PMC) PLL Register -#define AT91C_PMC_PCER ((AT91_REG *) 0xFFFFFC10) // (PMC) Peripheral Clock Enable Register -#define AT91C_PMC_PCKR ((AT91_REG *) 0xFFFFFC40) // (PMC) Programmable Clock Register -#define AT91C_PMC_MCKR ((AT91_REG *) 0xFFFFFC30) // (PMC) Master Clock Register -#define AT91C_PMC_SCDR ((AT91_REG *) 0xFFFFFC04) // (PMC) System Clock Disable Register -#define AT91C_PMC_PCDR ((AT91_REG *) 0xFFFFFC14) // (PMC) Peripheral Clock Disable Register -#define AT91C_PMC_SCSR ((AT91_REG *) 0xFFFFFC08) // (PMC) System Clock Status Register -#define AT91C_PMC_PCSR ((AT91_REG *) 0xFFFFFC18) // (PMC) Peripheral Clock Status Register -#define AT91C_PMC_MCFR ((AT91_REG *) 0xFFFFFC24) // (PMC) Main Clock Frequency Register -#define AT91C_PMC_SCER ((AT91_REG *) 0xFFFFFC00) // (PMC) System Clock Enable Register -#define AT91C_PMC_IMR ((AT91_REG *) 0xFFFFFC6C) // (PMC) Interrupt Mask Register -#define AT91C_PMC_IER ((AT91_REG *) 0xFFFFFC60) // (PMC) Interrupt Enable Register -#define AT91C_PMC_SR ((AT91_REG *) 0xFFFFFC68) // (PMC) Status Register +#define AT91C_PMC_IDR ((AT91_REG *) 0xFFFFFC64) // (PMC) Interrupt Disable Register +#define AT91C_PMC_MOR ((AT91_REG *) 0xFFFFFC20) // (PMC) Main Oscillator Register +#define AT91C_PMC_PLLR ((AT91_REG *) 0xFFFFFC2C) // (PMC) PLL Register +#define AT91C_PMC_PCER ((AT91_REG *) 0xFFFFFC10) // (PMC) Peripheral Clock Enable Register +#define AT91C_PMC_PCKR ((AT91_REG *) 0xFFFFFC40) // (PMC) Programmable Clock Register +#define AT91C_PMC_MCKR ((AT91_REG *) 0xFFFFFC30) // (PMC) Master Clock Register +#define AT91C_PMC_SCDR ((AT91_REG *) 0xFFFFFC04) // (PMC) System Clock Disable Register +#define AT91C_PMC_PCDR ((AT91_REG *) 0xFFFFFC14) // (PMC) Peripheral Clock Disable Register +#define AT91C_PMC_SCSR ((AT91_REG *) 0xFFFFFC08) // (PMC) System Clock Status Register +#define AT91C_PMC_PCSR ((AT91_REG *) 0xFFFFFC18) // (PMC) Peripheral Clock Status Register +#define AT91C_PMC_MCFR ((AT91_REG *) 0xFFFFFC24) // (PMC) Main Clock Frequency Register +#define AT91C_PMC_SCER ((AT91_REG *) 0xFFFFFC00) // (PMC) System Clock Enable Register +#define AT91C_PMC_IMR ((AT91_REG *) 0xFFFFFC6C) // (PMC) Interrupt Mask Register +#define AT91C_PMC_IER ((AT91_REG *) 0xFFFFFC60) // (PMC) Interrupt Enable Register +#define AT91C_PMC_SR ((AT91_REG *) 0xFFFFFC68) // (PMC) Status Register // ========== Register definition for RSTC peripheral ========== -#define AT91C_RSTC_RCR ((AT91_REG *) 0xFFFFFD00) // (RSTC) Reset Control Register -#define AT91C_RSTC_RMR ((AT91_REG *) 0xFFFFFD08) // (RSTC) Reset Mode Register -#define AT91C_RSTC_RSR ((AT91_REG *) 0xFFFFFD04) // (RSTC) Reset Status Register +#define AT91C_RSTC_RCR ((AT91_REG *) 0xFFFFFD00) // (RSTC) Reset Control Register +#define AT91C_RSTC_RMR ((AT91_REG *) 0xFFFFFD08) // (RSTC) Reset Mode Register +#define AT91C_RSTC_RSR ((AT91_REG *) 0xFFFFFD04) // (RSTC) Reset Status Register // ========== Register definition for RTTC peripheral ========== -#define AT91C_RTTC_RTSR ((AT91_REG *) 0xFFFFFD2C) // (RTTC) Real-time Status Register -#define AT91C_RTTC_RTMR ((AT91_REG *) 0xFFFFFD20) // (RTTC) Real-time Mode Register -#define AT91C_RTTC_RTVR ((AT91_REG *) 0xFFFFFD28) // (RTTC) Real-time Value Register -#define AT91C_RTTC_RTAR ((AT91_REG *) 0xFFFFFD24) // (RTTC) Real-time Alarm Register +#define AT91C_RTTC_RTSR ((AT91_REG *) 0xFFFFFD2C) // (RTTC) Real-time Status Register +#define AT91C_RTTC_RTMR ((AT91_REG *) 0xFFFFFD20) // (RTTC) Real-time Mode Register +#define AT91C_RTTC_RTVR ((AT91_REG *) 0xFFFFFD28) // (RTTC) Real-time Value Register +#define AT91C_RTTC_RTAR ((AT91_REG *) 0xFFFFFD24) // (RTTC) Real-time Alarm Register // ========== Register definition for PITC peripheral ========== -#define AT91C_PITC_PIVR ((AT91_REG *) 0xFFFFFD38) // (PITC) Period Interval Value Register -#define AT91C_PITC_PISR ((AT91_REG *) 0xFFFFFD34) // (PITC) Period Interval Status Register -#define AT91C_PITC_PIIR ((AT91_REG *) 0xFFFFFD3C) // (PITC) Period Interval Image Register -#define AT91C_PITC_PIMR ((AT91_REG *) 0xFFFFFD30) // (PITC) Period Interval Mode Register +#define AT91C_PITC_PIVR ((AT91_REG *) 0xFFFFFD38) // (PITC) Period Interval Value Register +#define AT91C_PITC_PISR ((AT91_REG *) 0xFFFFFD34) // (PITC) Period Interval Status Register +#define AT91C_PITC_PIIR ((AT91_REG *) 0xFFFFFD3C) // (PITC) Period Interval Image Register +#define AT91C_PITC_PIMR ((AT91_REG *) 0xFFFFFD30) // (PITC) Period Interval Mode Register // ========== Register definition for WDTC peripheral ========== -#define AT91C_WDTC_WDCR ((AT91_REG *) 0xFFFFFD40) // (WDTC) Watchdog Control Register -#define AT91C_WDTC_WDSR ((AT91_REG *) 0xFFFFFD48) // (WDTC) Watchdog Status Register -#define AT91C_WDTC_WDMR ((AT91_REG *) 0xFFFFFD44) // (WDTC) Watchdog Mode Register +#define AT91C_WDTC_WDCR ((AT91_REG *) 0xFFFFFD40) // (WDTC) Watchdog Control Register +#define AT91C_WDTC_WDSR ((AT91_REG *) 0xFFFFFD48) // (WDTC) Watchdog Status Register +#define AT91C_WDTC_WDMR ((AT91_REG *) 0xFFFFFD44) // (WDTC) Watchdog Mode Register // ========== Register definition for VREG peripheral ========== -#define AT91C_VREG_MR ((AT91_REG *) 0xFFFFFD60) // (VREG) Voltage Regulator Mode Register +#define AT91C_VREG_MR ((AT91_REG *) 0xFFFFFD60) // (VREG) Voltage Regulator Mode Register // ========== Register definition for MC peripheral ========== -#define AT91C_MC_ASR ((AT91_REG *) 0xFFFFFF04) // (MC) MC Abort Status Register -#define AT91C_MC_RCR ((AT91_REG *) 0xFFFFFF00) // (MC) MC Remap Control Register -#define AT91C_MC_FCR ((AT91_REG *) 0xFFFFFF64) // (MC) MC Flash Command Register -#define AT91C_MC_AASR ((AT91_REG *) 0xFFFFFF08) // (MC) MC Abort Address Status Register -#define AT91C_MC_FSR ((AT91_REG *) 0xFFFFFF68) // (MC) MC Flash Status Register -#define AT91C_MC_FMR ((AT91_REG *) 0xFFFFFF60) // (MC) MC Flash Mode Register +#define AT91C_MC_ASR ((AT91_REG *) 0xFFFFFF04) // (MC) MC Abort Status Register +#define AT91C_MC_RCR ((AT91_REG *) 0xFFFFFF00) // (MC) MC Remap Control Register +#define AT91C_MC_FCR ((AT91_REG *) 0xFFFFFF64) // (MC) MC Flash Command Register +#define AT91C_MC_AASR ((AT91_REG *) 0xFFFFFF08) // (MC) MC Abort Address Status Register +#define AT91C_MC_FSR ((AT91_REG *) 0xFFFFFF68) // (MC) MC Flash Status Register +#define AT91C_MC_FMR ((AT91_REG *) 0xFFFFFF60) // (MC) MC Flash Mode Register // ========== Register definition for PDC_SPI peripheral ========== -#define AT91C_SPI_PTCR ((AT91_REG *) 0xFFFE0120) // (PDC_SPI) PDC Transfer Control Register -#define AT91C_SPI_TPR ((AT91_REG *) 0xFFFE0108) // (PDC_SPI) Transmit Pointer Register -#define AT91C_SPI_TCR ((AT91_REG *) 0xFFFE010C) // (PDC_SPI) Transmit Counter Register -#define AT91C_SPI_RCR ((AT91_REG *) 0xFFFE0104) // (PDC_SPI) Receive Counter Register -#define AT91C_SPI_PTSR ((AT91_REG *) 0xFFFE0124) // (PDC_SPI) PDC Transfer Status Register -#define AT91C_SPI_RNPR ((AT91_REG *) 0xFFFE0110) // (PDC_SPI) Receive Next Pointer Register -#define AT91C_SPI_RPR ((AT91_REG *) 0xFFFE0100) // (PDC_SPI) Receive Pointer Register -#define AT91C_SPI_TNCR ((AT91_REG *) 0xFFFE011C) // (PDC_SPI) Transmit Next Counter Register -#define AT91C_SPI_RNCR ((AT91_REG *) 0xFFFE0114) // (PDC_SPI) Receive Next Counter Register -#define AT91C_SPI_TNPR ((AT91_REG *) 0xFFFE0118) // (PDC_SPI) Transmit Next Pointer Register +#define AT91C_SPI_PTCR ((AT91_REG *) 0xFFFE0120) // (PDC_SPI) PDC Transfer Control Register +#define AT91C_SPI_TPR ((AT91_REG *) 0xFFFE0108) // (PDC_SPI) Transmit Pointer Register +#define AT91C_SPI_TCR ((AT91_REG *) 0xFFFE010C) // (PDC_SPI) Transmit Counter Register +#define AT91C_SPI_RCR ((AT91_REG *) 0xFFFE0104) // (PDC_SPI) Receive Counter Register +#define AT91C_SPI_PTSR ((AT91_REG *) 0xFFFE0124) // (PDC_SPI) PDC Transfer Status Register +#define AT91C_SPI_RNPR ((AT91_REG *) 0xFFFE0110) // (PDC_SPI) Receive Next Pointer Register +#define AT91C_SPI_RPR ((AT91_REG *) 0xFFFE0100) // (PDC_SPI) Receive Pointer Register +#define AT91C_SPI_TNCR ((AT91_REG *) 0xFFFE011C) // (PDC_SPI) Transmit Next Counter Register +#define AT91C_SPI_RNCR ((AT91_REG *) 0xFFFE0114) // (PDC_SPI) Receive Next Counter Register +#define AT91C_SPI_TNPR ((AT91_REG *) 0xFFFE0118) // (PDC_SPI) Transmit Next Pointer Register // ========== Register definition for SPI peripheral ========== -#define AT91C_SPI_IER ((AT91_REG *) 0xFFFE0014) // (SPI) Interrupt Enable Register -#define AT91C_SPI_SR ((AT91_REG *) 0xFFFE0010) // (SPI) Status Register -#define AT91C_SPI_IDR ((AT91_REG *) 0xFFFE0018) // (SPI) Interrupt Disable Register -#define AT91C_SPI_CR ((AT91_REG *) 0xFFFE0000) // (SPI) Control Register -#define AT91C_SPI_MR ((AT91_REG *) 0xFFFE0004) // (SPI) Mode Register -#define AT91C_SPI_IMR ((AT91_REG *) 0xFFFE001C) // (SPI) Interrupt Mask Register -#define AT91C_SPI_TDR ((AT91_REG *) 0xFFFE000C) // (SPI) Transmit Data Register -#define AT91C_SPI_RDR ((AT91_REG *) 0xFFFE0008) // (SPI) Receive Data Register -#define AT91C_SPI_CSR ((AT91_REG *) 0xFFFE0030) // (SPI) Chip Select Register +#define AT91C_SPI_IER ((AT91_REG *) 0xFFFE0014) // (SPI) Interrupt Enable Register +#define AT91C_SPI_SR ((AT91_REG *) 0xFFFE0010) // (SPI) Status Register +#define AT91C_SPI_IDR ((AT91_REG *) 0xFFFE0018) // (SPI) Interrupt Disable Register +#define AT91C_SPI_CR ((AT91_REG *) 0xFFFE0000) // (SPI) Control Register +#define AT91C_SPI_MR ((AT91_REG *) 0xFFFE0004) // (SPI) Mode Register +#define AT91C_SPI_IMR ((AT91_REG *) 0xFFFE001C) // (SPI) Interrupt Mask Register +#define AT91C_SPI_TDR ((AT91_REG *) 0xFFFE000C) // (SPI) Transmit Data Register +#define AT91C_SPI_RDR ((AT91_REG *) 0xFFFE0008) // (SPI) Receive Data Register +#define AT91C_SPI_CSR ((AT91_REG *) 0xFFFE0030) // (SPI) Chip Select Register // ========== Register definition for PDC_ADC peripheral ========== -#define AT91C_ADC_PTSR ((AT91_REG *) 0xFFFD8124) // (PDC_ADC) PDC Transfer Status Register -#define AT91C_ADC_PTCR ((AT91_REG *) 0xFFFD8120) // (PDC_ADC) PDC Transfer Control Register -#define AT91C_ADC_TNPR ((AT91_REG *) 0xFFFD8118) // (PDC_ADC) Transmit Next Pointer Register -#define AT91C_ADC_TNCR ((AT91_REG *) 0xFFFD811C) // (PDC_ADC) Transmit Next Counter Register -#define AT91C_ADC_RNPR ((AT91_REG *) 0xFFFD8110) // (PDC_ADC) Receive Next Pointer Register -#define AT91C_ADC_RNCR ((AT91_REG *) 0xFFFD8114) // (PDC_ADC) Receive Next Counter Register -#define AT91C_ADC_RPR ((AT91_REG *) 0xFFFD8100) // (PDC_ADC) Receive Pointer Register -#define AT91C_ADC_TCR ((AT91_REG *) 0xFFFD810C) // (PDC_ADC) Transmit Counter Register -#define AT91C_ADC_TPR ((AT91_REG *) 0xFFFD8108) // (PDC_ADC) Transmit Pointer Register -#define AT91C_ADC_RCR ((AT91_REG *) 0xFFFD8104) // (PDC_ADC) Receive Counter Register +#define AT91C_ADC_PTSR ((AT91_REG *) 0xFFFD8124) // (PDC_ADC) PDC Transfer Status Register +#define AT91C_ADC_PTCR ((AT91_REG *) 0xFFFD8120) // (PDC_ADC) PDC Transfer Control Register +#define AT91C_ADC_TNPR ((AT91_REG *) 0xFFFD8118) // (PDC_ADC) Transmit Next Pointer Register +#define AT91C_ADC_TNCR ((AT91_REG *) 0xFFFD811C) // (PDC_ADC) Transmit Next Counter Register +#define AT91C_ADC_RNPR ((AT91_REG *) 0xFFFD8110) // (PDC_ADC) Receive Next Pointer Register +#define AT91C_ADC_RNCR ((AT91_REG *) 0xFFFD8114) // (PDC_ADC) Receive Next Counter Register +#define AT91C_ADC_RPR ((AT91_REG *) 0xFFFD8100) // (PDC_ADC) Receive Pointer Register +#define AT91C_ADC_TCR ((AT91_REG *) 0xFFFD810C) // (PDC_ADC) Transmit Counter Register +#define AT91C_ADC_TPR ((AT91_REG *) 0xFFFD8108) // (PDC_ADC) Transmit Pointer Register +#define AT91C_ADC_RCR ((AT91_REG *) 0xFFFD8104) // (PDC_ADC) Receive Counter Register // ========== Register definition for ADC peripheral ========== -#define AT91C_ADC_CDR2 ((AT91_REG *) 0xFFFD8038) // (ADC) ADC Channel Data Register 2 -#define AT91C_ADC_CDR3 ((AT91_REG *) 0xFFFD803C) // (ADC) ADC Channel Data Register 3 -#define AT91C_ADC_CDR0 ((AT91_REG *) 0xFFFD8030) // (ADC) ADC Channel Data Register 0 -#define AT91C_ADC_CDR5 ((AT91_REG *) 0xFFFD8044) // (ADC) ADC Channel Data Register 5 -#define AT91C_ADC_CHDR ((AT91_REG *) 0xFFFD8014) // (ADC) ADC Channel Disable Register -#define AT91C_ADC_SR ((AT91_REG *) 0xFFFD801C) // (ADC) ADC Status Register -#define AT91C_ADC_CDR4 ((AT91_REG *) 0xFFFD8040) // (ADC) ADC Channel Data Register 4 -#define AT91C_ADC_CDR1 ((AT91_REG *) 0xFFFD8034) // (ADC) ADC Channel Data Register 1 -#define AT91C_ADC_LCDR ((AT91_REG *) 0xFFFD8020) // (ADC) ADC Last Converted Data Register -#define AT91C_ADC_IDR ((AT91_REG *) 0xFFFD8028) // (ADC) ADC Interrupt Disable Register -#define AT91C_ADC_CR ((AT91_REG *) 0xFFFD8000) // (ADC) ADC Control Register -#define AT91C_ADC_CDR7 ((AT91_REG *) 0xFFFD804C) // (ADC) ADC Channel Data Register 7 -#define AT91C_ADC_CDR6 ((AT91_REG *) 0xFFFD8048) // (ADC) ADC Channel Data Register 6 -#define AT91C_ADC_IER ((AT91_REG *) 0xFFFD8024) // (ADC) ADC Interrupt Enable Register -#define AT91C_ADC_CHER ((AT91_REG *) 0xFFFD8010) // (ADC) ADC Channel Enable Register -#define AT91C_ADC_CHSR ((AT91_REG *) 0xFFFD8018) // (ADC) ADC Channel Status Register -#define AT91C_ADC_MR ((AT91_REG *) 0xFFFD8004) // (ADC) ADC Mode Register -#define AT91C_ADC_IMR ((AT91_REG *) 0xFFFD802C) // (ADC) ADC Interrupt Mask Register +#define AT91C_ADC_CDR2 ((AT91_REG *) 0xFFFD8038) // (ADC) ADC Channel Data Register 2 +#define AT91C_ADC_CDR3 ((AT91_REG *) 0xFFFD803C) // (ADC) ADC Channel Data Register 3 +#define AT91C_ADC_CDR0 ((AT91_REG *) 0xFFFD8030) // (ADC) ADC Channel Data Register 0 +#define AT91C_ADC_CDR5 ((AT91_REG *) 0xFFFD8044) // (ADC) ADC Channel Data Register 5 +#define AT91C_ADC_CHDR ((AT91_REG *) 0xFFFD8014) // (ADC) ADC Channel Disable Register +#define AT91C_ADC_SR ((AT91_REG *) 0xFFFD801C) // (ADC) ADC Status Register +#define AT91C_ADC_CDR4 ((AT91_REG *) 0xFFFD8040) // (ADC) ADC Channel Data Register 4 +#define AT91C_ADC_CDR1 ((AT91_REG *) 0xFFFD8034) // (ADC) ADC Channel Data Register 1 +#define AT91C_ADC_LCDR ((AT91_REG *) 0xFFFD8020) // (ADC) ADC Last Converted Data Register +#define AT91C_ADC_IDR ((AT91_REG *) 0xFFFD8028) // (ADC) ADC Interrupt Disable Register +#define AT91C_ADC_CR ((AT91_REG *) 0xFFFD8000) // (ADC) ADC Control Register +#define AT91C_ADC_CDR7 ((AT91_REG *) 0xFFFD804C) // (ADC) ADC Channel Data Register 7 +#define AT91C_ADC_CDR6 ((AT91_REG *) 0xFFFD8048) // (ADC) ADC Channel Data Register 6 +#define AT91C_ADC_IER ((AT91_REG *) 0xFFFD8024) // (ADC) ADC Interrupt Enable Register +#define AT91C_ADC_CHER ((AT91_REG *) 0xFFFD8010) // (ADC) ADC Channel Enable Register +#define AT91C_ADC_CHSR ((AT91_REG *) 0xFFFD8018) // (ADC) ADC Channel Status Register +#define AT91C_ADC_MR ((AT91_REG *) 0xFFFD8004) // (ADC) ADC Mode Register +#define AT91C_ADC_IMR ((AT91_REG *) 0xFFFD802C) // (ADC) ADC Interrupt Mask Register // ========== Register definition for PDC_SSC peripheral ========== -#define AT91C_SSC_TNCR ((AT91_REG *) 0xFFFD411C) // (PDC_SSC) Transmit Next Counter Register -#define AT91C_SSC_RPR ((AT91_REG *) 0xFFFD4100) // (PDC_SSC) Receive Pointer Register -#define AT91C_SSC_RNCR ((AT91_REG *) 0xFFFD4114) // (PDC_SSC) Receive Next Counter Register -#define AT91C_SSC_TPR ((AT91_REG *) 0xFFFD4108) // (PDC_SSC) Transmit Pointer Register -#define AT91C_SSC_PTCR ((AT91_REG *) 0xFFFD4120) // (PDC_SSC) PDC Transfer Control Register -#define AT91C_SSC_TCR ((AT91_REG *) 0xFFFD410C) // (PDC_SSC) Transmit Counter Register -#define AT91C_SSC_RCR ((AT91_REG *) 0xFFFD4104) // (PDC_SSC) Receive Counter Register -#define AT91C_SSC_RNPR ((AT91_REG *) 0xFFFD4110) // (PDC_SSC) Receive Next Pointer Register -#define AT91C_SSC_TNPR ((AT91_REG *) 0xFFFD4118) // (PDC_SSC) Transmit Next Pointer Register -#define AT91C_SSC_PTSR ((AT91_REG *) 0xFFFD4124) // (PDC_SSC) PDC Transfer Status Register +#define AT91C_SSC_TNCR ((AT91_REG *) 0xFFFD411C) // (PDC_SSC) Transmit Next Counter Register +#define AT91C_SSC_RPR ((AT91_REG *) 0xFFFD4100) // (PDC_SSC) Receive Pointer Register +#define AT91C_SSC_RNCR ((AT91_REG *) 0xFFFD4114) // (PDC_SSC) Receive Next Counter Register +#define AT91C_SSC_TPR ((AT91_REG *) 0xFFFD4108) // (PDC_SSC) Transmit Pointer Register +#define AT91C_SSC_PTCR ((AT91_REG *) 0xFFFD4120) // (PDC_SSC) PDC Transfer Control Register +#define AT91C_SSC_TCR ((AT91_REG *) 0xFFFD410C) // (PDC_SSC) Transmit Counter Register +#define AT91C_SSC_RCR ((AT91_REG *) 0xFFFD4104) // (PDC_SSC) Receive Counter Register +#define AT91C_SSC_RNPR ((AT91_REG *) 0xFFFD4110) // (PDC_SSC) Receive Next Pointer Register +#define AT91C_SSC_TNPR ((AT91_REG *) 0xFFFD4118) // (PDC_SSC) Transmit Next Pointer Register +#define AT91C_SSC_PTSR ((AT91_REG *) 0xFFFD4124) // (PDC_SSC) PDC Transfer Status Register // ========== Register definition for SSC peripheral ========== -#define AT91C_SSC_RHR ((AT91_REG *) 0xFFFD4020) // (SSC) Receive Holding Register -#define AT91C_SSC_RSHR ((AT91_REG *) 0xFFFD4030) // (SSC) Receive Sync Holding Register -#define AT91C_SSC_TFMR ((AT91_REG *) 0xFFFD401C) // (SSC) Transmit Frame Mode Register -#define AT91C_SSC_IDR ((AT91_REG *) 0xFFFD4048) // (SSC) Interrupt Disable Register -#define AT91C_SSC_THR ((AT91_REG *) 0xFFFD4024) // (SSC) Transmit Holding Register -#define AT91C_SSC_RCMR ((AT91_REG *) 0xFFFD4010) // (SSC) Receive Clock ModeRegister -#define AT91C_SSC_IER ((AT91_REG *) 0xFFFD4044) // (SSC) Interrupt Enable Register -#define AT91C_SSC_TSHR ((AT91_REG *) 0xFFFD4034) // (SSC) Transmit Sync Holding Register -#define AT91C_SSC_SR ((AT91_REG *) 0xFFFD4040) // (SSC) Status Register -#define AT91C_SSC_CMR ((AT91_REG *) 0xFFFD4004) // (SSC) Clock Mode Register -#define AT91C_SSC_TCMR ((AT91_REG *) 0xFFFD4018) // (SSC) Transmit Clock Mode Register -#define AT91C_SSC_CR ((AT91_REG *) 0xFFFD4000) // (SSC) Control Register -#define AT91C_SSC_IMR ((AT91_REG *) 0xFFFD404C) // (SSC) Interrupt Mask Register -#define AT91C_SSC_RFMR ((AT91_REG *) 0xFFFD4014) // (SSC) Receive Frame Mode Register +#define AT91C_SSC_RHR ((AT91_REG *) 0xFFFD4020) // (SSC) Receive Holding Register +#define AT91C_SSC_RSHR ((AT91_REG *) 0xFFFD4030) // (SSC) Receive Sync Holding Register +#define AT91C_SSC_TFMR ((AT91_REG *) 0xFFFD401C) // (SSC) Transmit Frame Mode Register +#define AT91C_SSC_IDR ((AT91_REG *) 0xFFFD4048) // (SSC) Interrupt Disable Register +#define AT91C_SSC_THR ((AT91_REG *) 0xFFFD4024) // (SSC) Transmit Holding Register +#define AT91C_SSC_RCMR ((AT91_REG *) 0xFFFD4010) // (SSC) Receive Clock ModeRegister +#define AT91C_SSC_IER ((AT91_REG *) 0xFFFD4044) // (SSC) Interrupt Enable Register +#define AT91C_SSC_TSHR ((AT91_REG *) 0xFFFD4034) // (SSC) Transmit Sync Holding Register +#define AT91C_SSC_SR ((AT91_REG *) 0xFFFD4040) // (SSC) Status Register +#define AT91C_SSC_CMR ((AT91_REG *) 0xFFFD4004) // (SSC) Clock Mode Register +#define AT91C_SSC_TCMR ((AT91_REG *) 0xFFFD4018) // (SSC) Transmit Clock Mode Register +#define AT91C_SSC_CR ((AT91_REG *) 0xFFFD4000) // (SSC) Control Register +#define AT91C_SSC_IMR ((AT91_REG *) 0xFFFD404C) // (SSC) Interrupt Mask Register +#define AT91C_SSC_RFMR ((AT91_REG *) 0xFFFD4014) // (SSC) Receive Frame Mode Register // ========== Register definition for PDC_US1 peripheral ========== -#define AT91C_US1_RNCR ((AT91_REG *) 0xFFFC4114) // (PDC_US1) Receive Next Counter Register -#define AT91C_US1_PTCR ((AT91_REG *) 0xFFFC4120) // (PDC_US1) PDC Transfer Control Register -#define AT91C_US1_TCR ((AT91_REG *) 0xFFFC410C) // (PDC_US1) Transmit Counter Register -#define AT91C_US1_PTSR ((AT91_REG *) 0xFFFC4124) // (PDC_US1) PDC Transfer Status Register -#define AT91C_US1_TNPR ((AT91_REG *) 0xFFFC4118) // (PDC_US1) Transmit Next Pointer Register -#define AT91C_US1_RCR ((AT91_REG *) 0xFFFC4104) // (PDC_US1) Receive Counter Register -#define AT91C_US1_RNPR ((AT91_REG *) 0xFFFC4110) // (PDC_US1) Receive Next Pointer Register -#define AT91C_US1_RPR ((AT91_REG *) 0xFFFC4100) // (PDC_US1) Receive Pointer Register -#define AT91C_US1_TNCR ((AT91_REG *) 0xFFFC411C) // (PDC_US1) Transmit Next Counter Register -#define AT91C_US1_TPR ((AT91_REG *) 0xFFFC4108) // (PDC_US1) Transmit Pointer Register +#define AT91C_US1_RNCR ((AT91_REG *) 0xFFFC4114) // (PDC_US1) Receive Next Counter Register +#define AT91C_US1_PTCR ((AT91_REG *) 0xFFFC4120) // (PDC_US1) PDC Transfer Control Register +#define AT91C_US1_TCR ((AT91_REG *) 0xFFFC410C) // (PDC_US1) Transmit Counter Register +#define AT91C_US1_PTSR ((AT91_REG *) 0xFFFC4124) // (PDC_US1) PDC Transfer Status Register +#define AT91C_US1_TNPR ((AT91_REG *) 0xFFFC4118) // (PDC_US1) Transmit Next Pointer Register +#define AT91C_US1_RCR ((AT91_REG *) 0xFFFC4104) // (PDC_US1) Receive Counter Register +#define AT91C_US1_RNPR ((AT91_REG *) 0xFFFC4110) // (PDC_US1) Receive Next Pointer Register +#define AT91C_US1_RPR ((AT91_REG *) 0xFFFC4100) // (PDC_US1) Receive Pointer Register +#define AT91C_US1_TNCR ((AT91_REG *) 0xFFFC411C) // (PDC_US1) Transmit Next Counter Register +#define AT91C_US1_TPR ((AT91_REG *) 0xFFFC4108) // (PDC_US1) Transmit Pointer Register // ========== Register definition for US1 peripheral ========== -#define AT91C_US1_IF ((AT91_REG *) 0xFFFC404C) // (US1) IRDA_FILTER Register -#define AT91C_US1_NER ((AT91_REG *) 0xFFFC4044) // (US1) Nb Errors Register -#define AT91C_US1_RTOR ((AT91_REG *) 0xFFFC4024) // (US1) Receiver Time-out Register -#define AT91C_US1_CSR ((AT91_REG *) 0xFFFC4014) // (US1) Channel Status Register -#define AT91C_US1_IDR ((AT91_REG *) 0xFFFC400C) // (US1) Interrupt Disable Register -#define AT91C_US1_IER ((AT91_REG *) 0xFFFC4008) // (US1) Interrupt Enable Register -#define AT91C_US1_THR ((AT91_REG *) 0xFFFC401C) // (US1) Transmitter Holding Register -#define AT91C_US1_TTGR ((AT91_REG *) 0xFFFC4028) // (US1) Transmitter Time-guard Register -#define AT91C_US1_RHR ((AT91_REG *) 0xFFFC4018) // (US1) Receiver Holding Register -#define AT91C_US1_BRGR ((AT91_REG *) 0xFFFC4020) // (US1) Baud Rate Generator Register -#define AT91C_US1_IMR ((AT91_REG *) 0xFFFC4010) // (US1) Interrupt Mask Register -#define AT91C_US1_FIDI ((AT91_REG *) 0xFFFC4040) // (US1) FI_DI_Ratio Register -#define AT91C_US1_CR ((AT91_REG *) 0xFFFC4000) // (US1) Control Register -#define AT91C_US1_MR ((AT91_REG *) 0xFFFC4004) // (US1) Mode Register +#define AT91C_US1_IF ((AT91_REG *) 0xFFFC404C) // (US1) IRDA_FILTER Register +#define AT91C_US1_NER ((AT91_REG *) 0xFFFC4044) // (US1) Nb Errors Register +#define AT91C_US1_RTOR ((AT91_REG *) 0xFFFC4024) // (US1) Receiver Time-out Register +#define AT91C_US1_CSR ((AT91_REG *) 0xFFFC4014) // (US1) Channel Status Register +#define AT91C_US1_IDR ((AT91_REG *) 0xFFFC400C) // (US1) Interrupt Disable Register +#define AT91C_US1_IER ((AT91_REG *) 0xFFFC4008) // (US1) Interrupt Enable Register +#define AT91C_US1_THR ((AT91_REG *) 0xFFFC401C) // (US1) Transmitter Holding Register +#define AT91C_US1_TTGR ((AT91_REG *) 0xFFFC4028) // (US1) Transmitter Time-guard Register +#define AT91C_US1_RHR ((AT91_REG *) 0xFFFC4018) // (US1) Receiver Holding Register +#define AT91C_US1_BRGR ((AT91_REG *) 0xFFFC4020) // (US1) Baud Rate Generator Register +#define AT91C_US1_IMR ((AT91_REG *) 0xFFFC4010) // (US1) Interrupt Mask Register +#define AT91C_US1_FIDI ((AT91_REG *) 0xFFFC4040) // (US1) FI_DI_Ratio Register +#define AT91C_US1_CR ((AT91_REG *) 0xFFFC4000) // (US1) Control Register +#define AT91C_US1_MR ((AT91_REG *) 0xFFFC4004) // (US1) Mode Register // ========== Register definition for PDC_US0 peripheral ========== -#define AT91C_US0_TNPR ((AT91_REG *) 0xFFFC0118) // (PDC_US0) Transmit Next Pointer Register -#define AT91C_US0_RNPR ((AT91_REG *) 0xFFFC0110) // (PDC_US0) Receive Next Pointer Register -#define AT91C_US0_TCR ((AT91_REG *) 0xFFFC010C) // (PDC_US0) Transmit Counter Register -#define AT91C_US0_PTCR ((AT91_REG *) 0xFFFC0120) // (PDC_US0) PDC Transfer Control Register -#define AT91C_US0_PTSR ((AT91_REG *) 0xFFFC0124) // (PDC_US0) PDC Transfer Status Register -#define AT91C_US0_TNCR ((AT91_REG *) 0xFFFC011C) // (PDC_US0) Transmit Next Counter Register -#define AT91C_US0_TPR ((AT91_REG *) 0xFFFC0108) // (PDC_US0) Transmit Pointer Register -#define AT91C_US0_RCR ((AT91_REG *) 0xFFFC0104) // (PDC_US0) Receive Counter Register -#define AT91C_US0_RPR ((AT91_REG *) 0xFFFC0100) // (PDC_US0) Receive Pointer Register -#define AT91C_US0_RNCR ((AT91_REG *) 0xFFFC0114) // (PDC_US0) Receive Next Counter Register +#define AT91C_US0_TNPR ((AT91_REG *) 0xFFFC0118) // (PDC_US0) Transmit Next Pointer Register +#define AT91C_US0_RNPR ((AT91_REG *) 0xFFFC0110) // (PDC_US0) Receive Next Pointer Register +#define AT91C_US0_TCR ((AT91_REG *) 0xFFFC010C) // (PDC_US0) Transmit Counter Register +#define AT91C_US0_PTCR ((AT91_REG *) 0xFFFC0120) // (PDC_US0) PDC Transfer Control Register +#define AT91C_US0_PTSR ((AT91_REG *) 0xFFFC0124) // (PDC_US0) PDC Transfer Status Register +#define AT91C_US0_TNCR ((AT91_REG *) 0xFFFC011C) // (PDC_US0) Transmit Next Counter Register +#define AT91C_US0_TPR ((AT91_REG *) 0xFFFC0108) // (PDC_US0) Transmit Pointer Register +#define AT91C_US0_RCR ((AT91_REG *) 0xFFFC0104) // (PDC_US0) Receive Counter Register +#define AT91C_US0_RPR ((AT91_REG *) 0xFFFC0100) // (PDC_US0) Receive Pointer Register +#define AT91C_US0_RNCR ((AT91_REG *) 0xFFFC0114) // (PDC_US0) Receive Next Counter Register // ========== Register definition for US0 peripheral ========== -#define AT91C_US0_BRGR ((AT91_REG *) 0xFFFC0020) // (US0) Baud Rate Generator Register -#define AT91C_US0_NER ((AT91_REG *) 0xFFFC0044) // (US0) Nb Errors Register -#define AT91C_US0_CR ((AT91_REG *) 0xFFFC0000) // (US0) Control Register -#define AT91C_US0_IMR ((AT91_REG *) 0xFFFC0010) // (US0) Interrupt Mask Register -#define AT91C_US0_FIDI ((AT91_REG *) 0xFFFC0040) // (US0) FI_DI_Ratio Register -#define AT91C_US0_TTGR ((AT91_REG *) 0xFFFC0028) // (US0) Transmitter Time-guard Register -#define AT91C_US0_MR ((AT91_REG *) 0xFFFC0004) // (US0) Mode Register -#define AT91C_US0_RTOR ((AT91_REG *) 0xFFFC0024) // (US0) Receiver Time-out Register -#define AT91C_US0_CSR ((AT91_REG *) 0xFFFC0014) // (US0) Channel Status Register -#define AT91C_US0_RHR ((AT91_REG *) 0xFFFC0018) // (US0) Receiver Holding Register -#define AT91C_US0_IDR ((AT91_REG *) 0xFFFC000C) // (US0) Interrupt Disable Register -#define AT91C_US0_THR ((AT91_REG *) 0xFFFC001C) // (US0) Transmitter Holding Register -#define AT91C_US0_IF ((AT91_REG *) 0xFFFC004C) // (US0) IRDA_FILTER Register -#define AT91C_US0_IER ((AT91_REG *) 0xFFFC0008) // (US0) Interrupt Enable Register +#define AT91C_US0_BRGR ((AT91_REG *) 0xFFFC0020) // (US0) Baud Rate Generator Register +#define AT91C_US0_NER ((AT91_REG *) 0xFFFC0044) // (US0) Nb Errors Register +#define AT91C_US0_CR ((AT91_REG *) 0xFFFC0000) // (US0) Control Register +#define AT91C_US0_IMR ((AT91_REG *) 0xFFFC0010) // (US0) Interrupt Mask Register +#define AT91C_US0_FIDI ((AT91_REG *) 0xFFFC0040) // (US0) FI_DI_Ratio Register +#define AT91C_US0_TTGR ((AT91_REG *) 0xFFFC0028) // (US0) Transmitter Time-guard Register +#define AT91C_US0_MR ((AT91_REG *) 0xFFFC0004) // (US0) Mode Register +#define AT91C_US0_RTOR ((AT91_REG *) 0xFFFC0024) // (US0) Receiver Time-out Register +#define AT91C_US0_CSR ((AT91_REG *) 0xFFFC0014) // (US0) Channel Status Register +#define AT91C_US0_RHR ((AT91_REG *) 0xFFFC0018) // (US0) Receiver Holding Register +#define AT91C_US0_IDR ((AT91_REG *) 0xFFFC000C) // (US0) Interrupt Disable Register +#define AT91C_US0_THR ((AT91_REG *) 0xFFFC001C) // (US0) Transmitter Holding Register +#define AT91C_US0_IF ((AT91_REG *) 0xFFFC004C) // (US0) IRDA_FILTER Register +#define AT91C_US0_IER ((AT91_REG *) 0xFFFC0008) // (US0) Interrupt Enable Register // ========== Register definition for TWI peripheral ========== -#define AT91C_TWI_IER ((AT91_REG *) 0xFFFB8024) // (TWI) Interrupt Enable Register -#define AT91C_TWI_CR ((AT91_REG *) 0xFFFB8000) // (TWI) Control Register -#define AT91C_TWI_SR ((AT91_REG *) 0xFFFB8020) // (TWI) Status Register -#define AT91C_TWI_IMR ((AT91_REG *) 0xFFFB802C) // (TWI) Interrupt Mask Register -#define AT91C_TWI_THR ((AT91_REG *) 0xFFFB8034) // (TWI) Transmit Holding Register -#define AT91C_TWI_IDR ((AT91_REG *) 0xFFFB8028) // (TWI) Interrupt Disable Register -#define AT91C_TWI_IADR ((AT91_REG *) 0xFFFB800C) // (TWI) Internal Address Register -#define AT91C_TWI_MMR ((AT91_REG *) 0xFFFB8004) // (TWI) Master Mode Register -#define AT91C_TWI_CWGR ((AT91_REG *) 0xFFFB8010) // (TWI) Clock Waveform Generator Register -#define AT91C_TWI_RHR ((AT91_REG *) 0xFFFB8030) // (TWI) Receive Holding Register +#define AT91C_TWI_IER ((AT91_REG *) 0xFFFB8024) // (TWI) Interrupt Enable Register +#define AT91C_TWI_CR ((AT91_REG *) 0xFFFB8000) // (TWI) Control Register +#define AT91C_TWI_SR ((AT91_REG *) 0xFFFB8020) // (TWI) Status Register +#define AT91C_TWI_IMR ((AT91_REG *) 0xFFFB802C) // (TWI) Interrupt Mask Register +#define AT91C_TWI_THR ((AT91_REG *) 0xFFFB8034) // (TWI) Transmit Holding Register +#define AT91C_TWI_IDR ((AT91_REG *) 0xFFFB8028) // (TWI) Interrupt Disable Register +#define AT91C_TWI_IADR ((AT91_REG *) 0xFFFB800C) // (TWI) Internal Address Register +#define AT91C_TWI_MMR ((AT91_REG *) 0xFFFB8004) // (TWI) Master Mode Register +#define AT91C_TWI_CWGR ((AT91_REG *) 0xFFFB8010) // (TWI) Clock Waveform Generator Register +#define AT91C_TWI_RHR ((AT91_REG *) 0xFFFB8030) // (TWI) Receive Holding Register // ========== Register definition for TC0 peripheral ========== -#define AT91C_TC0_SR ((AT91_REG *) 0xFFFA0020) // (TC0) Status Register -#define AT91C_TC0_RC ((AT91_REG *) 0xFFFA001C) // (TC0) Register C -#define AT91C_TC0_RB ((AT91_REG *) 0xFFFA0018) // (TC0) Register B -#define AT91C_TC0_CCR ((AT91_REG *) 0xFFFA0000) // (TC0) Channel Control Register -#define AT91C_TC0_CMR ((AT91_REG *) 0xFFFA0004) // (TC0) Channel Mode Register (Capture Mode / Waveform Mode) -#define AT91C_TC0_IER ((AT91_REG *) 0xFFFA0024) // (TC0) Interrupt Enable Register -#define AT91C_TC0_RA ((AT91_REG *) 0xFFFA0014) // (TC0) Register A -#define AT91C_TC0_IDR ((AT91_REG *) 0xFFFA0028) // (TC0) Interrupt Disable Register -#define AT91C_TC0_CV ((AT91_REG *) 0xFFFA0010) // (TC0) Counter Value -#define AT91C_TC0_IMR ((AT91_REG *) 0xFFFA002C) // (TC0) Interrupt Mask Register +#define AT91C_TC0_SR ((AT91_REG *) 0xFFFA0020) // (TC0) Status Register +#define AT91C_TC0_RC ((AT91_REG *) 0xFFFA001C) // (TC0) Register C +#define AT91C_TC0_RB ((AT91_REG *) 0xFFFA0018) // (TC0) Register B +#define AT91C_TC0_CCR ((AT91_REG *) 0xFFFA0000) // (TC0) Channel Control Register +#define AT91C_TC0_CMR ((AT91_REG *) 0xFFFA0004) // (TC0) Channel Mode Register (Capture Mode / Waveform Mode) +#define AT91C_TC0_IER ((AT91_REG *) 0xFFFA0024) // (TC0) Interrupt Enable Register +#define AT91C_TC0_RA ((AT91_REG *) 0xFFFA0014) // (TC0) Register A +#define AT91C_TC0_IDR ((AT91_REG *) 0xFFFA0028) // (TC0) Interrupt Disable Register +#define AT91C_TC0_CV ((AT91_REG *) 0xFFFA0010) // (TC0) Counter Value +#define AT91C_TC0_IMR ((AT91_REG *) 0xFFFA002C) // (TC0) Interrupt Mask Register // ========== Register definition for TC1 peripheral ========== -#define AT91C_TC1_RB ((AT91_REG *) 0xFFFA0058) // (TC1) Register B -#define AT91C_TC1_CCR ((AT91_REG *) 0xFFFA0040) // (TC1) Channel Control Register -#define AT91C_TC1_IER ((AT91_REG *) 0xFFFA0064) // (TC1) Interrupt Enable Register -#define AT91C_TC1_IDR ((AT91_REG *) 0xFFFA0068) // (TC1) Interrupt Disable Register -#define AT91C_TC1_SR ((AT91_REG *) 0xFFFA0060) // (TC1) Status Register -#define AT91C_TC1_CMR ((AT91_REG *) 0xFFFA0044) // (TC1) Channel Mode Register (Capture Mode / Waveform Mode) -#define AT91C_TC1_RA ((AT91_REG *) 0xFFFA0054) // (TC1) Register A -#define AT91C_TC1_RC ((AT91_REG *) 0xFFFA005C) // (TC1) Register C -#define AT91C_TC1_IMR ((AT91_REG *) 0xFFFA006C) // (TC1) Interrupt Mask Register -#define AT91C_TC1_CV ((AT91_REG *) 0xFFFA0050) // (TC1) Counter Value +#define AT91C_TC1_RB ((AT91_REG *) 0xFFFA0058) // (TC1) Register B +#define AT91C_TC1_CCR ((AT91_REG *) 0xFFFA0040) // (TC1) Channel Control Register +#define AT91C_TC1_IER ((AT91_REG *) 0xFFFA0064) // (TC1) Interrupt Enable Register +#define AT91C_TC1_IDR ((AT91_REG *) 0xFFFA0068) // (TC1) Interrupt Disable Register +#define AT91C_TC1_SR ((AT91_REG *) 0xFFFA0060) // (TC1) Status Register +#define AT91C_TC1_CMR ((AT91_REG *) 0xFFFA0044) // (TC1) Channel Mode Register (Capture Mode / Waveform Mode) +#define AT91C_TC1_RA ((AT91_REG *) 0xFFFA0054) // (TC1) Register A +#define AT91C_TC1_RC ((AT91_REG *) 0xFFFA005C) // (TC1) Register C +#define AT91C_TC1_IMR ((AT91_REG *) 0xFFFA006C) // (TC1) Interrupt Mask Register +#define AT91C_TC1_CV ((AT91_REG *) 0xFFFA0050) // (TC1) Counter Value // ========== Register definition for TC2 peripheral ========== -#define AT91C_TC2_CMR ((AT91_REG *) 0xFFFA0084) // (TC2) Channel Mode Register (Capture Mode / Waveform Mode) -#define AT91C_TC2_CCR ((AT91_REG *) 0xFFFA0080) // (TC2) Channel Control Register -#define AT91C_TC2_CV ((AT91_REG *) 0xFFFA0090) // (TC2) Counter Value -#define AT91C_TC2_RA ((AT91_REG *) 0xFFFA0094) // (TC2) Register A -#define AT91C_TC2_RB ((AT91_REG *) 0xFFFA0098) // (TC2) Register B -#define AT91C_TC2_IDR ((AT91_REG *) 0xFFFA00A8) // (TC2) Interrupt Disable Register -#define AT91C_TC2_IMR ((AT91_REG *) 0xFFFA00AC) // (TC2) Interrupt Mask Register -#define AT91C_TC2_RC ((AT91_REG *) 0xFFFA009C) // (TC2) Register C -#define AT91C_TC2_IER ((AT91_REG *) 0xFFFA00A4) // (TC2) Interrupt Enable Register -#define AT91C_TC2_SR ((AT91_REG *) 0xFFFA00A0) // (TC2) Status Register +#define AT91C_TC2_CMR ((AT91_REG *) 0xFFFA0084) // (TC2) Channel Mode Register (Capture Mode / Waveform Mode) +#define AT91C_TC2_CCR ((AT91_REG *) 0xFFFA0080) // (TC2) Channel Control Register +#define AT91C_TC2_CV ((AT91_REG *) 0xFFFA0090) // (TC2) Counter Value +#define AT91C_TC2_RA ((AT91_REG *) 0xFFFA0094) // (TC2) Register A +#define AT91C_TC2_RB ((AT91_REG *) 0xFFFA0098) // (TC2) Register B +#define AT91C_TC2_IDR ((AT91_REG *) 0xFFFA00A8) // (TC2) Interrupt Disable Register +#define AT91C_TC2_IMR ((AT91_REG *) 0xFFFA00AC) // (TC2) Interrupt Mask Register +#define AT91C_TC2_RC ((AT91_REG *) 0xFFFA009C) // (TC2) Register C +#define AT91C_TC2_IER ((AT91_REG *) 0xFFFA00A4) // (TC2) Interrupt Enable Register +#define AT91C_TC2_SR ((AT91_REG *) 0xFFFA00A0) // (TC2) Status Register // ========== Register definition for TCB peripheral ========== -#define AT91C_TCB_BMR ((AT91_REG *) 0xFFFA00C4) // (TCB) TC Block Mode Register -#define AT91C_TCB_BCR ((AT91_REG *) 0xFFFA00C0) // (TCB) TC Block Control Register +#define AT91C_TCB_BMR ((AT91_REG *) 0xFFFA00C4) // (TCB) TC Block Mode Register +#define AT91C_TCB_BCR ((AT91_REG *) 0xFFFA00C0) // (TCB) TC Block Control Register // ========== Register definition for PWMC_CH3 peripheral ========== -#define AT91C_PWMC_CH3_CUPDR ((AT91_REG *) 0xFFFCC270) // (PWMC_CH3) Channel Update Register -#define AT91C_PWMC_CH3_Reserved ((AT91_REG *) 0xFFFCC274) // (PWMC_CH3) Reserved -#define AT91C_PWMC_CH3_CPRDR ((AT91_REG *) 0xFFFCC268) // (PWMC_CH3) Channel Period Register -#define AT91C_PWMC_CH3_CDTYR ((AT91_REG *) 0xFFFCC264) // (PWMC_CH3) Channel Duty Cycle Register -#define AT91C_PWMC_CH3_CCNTR ((AT91_REG *) 0xFFFCC26C) // (PWMC_CH3) Channel Counter Register -#define AT91C_PWMC_CH3_CMR ((AT91_REG *) 0xFFFCC260) // (PWMC_CH3) Channel Mode Register +#define AT91C_PWMC_CH3_CUPDR ((AT91_REG *) 0xFFFCC270) // (PWMC_CH3) Channel Update Register +#define AT91C_PWMC_CH3_Reserved ((AT91_REG *) 0xFFFCC274) // (PWMC_CH3) Reserved +#define AT91C_PWMC_CH3_CPRDR ((AT91_REG *) 0xFFFCC268) // (PWMC_CH3) Channel Period Register +#define AT91C_PWMC_CH3_CDTYR ((AT91_REG *) 0xFFFCC264) // (PWMC_CH3) Channel Duty Cycle Register +#define AT91C_PWMC_CH3_CCNTR ((AT91_REG *) 0xFFFCC26C) // (PWMC_CH3) Channel Counter Register +#define AT91C_PWMC_CH3_CMR ((AT91_REG *) 0xFFFCC260) // (PWMC_CH3) Channel Mode Register // ========== Register definition for PWMC_CH2 peripheral ========== -#define AT91C_PWMC_CH2_Reserved ((AT91_REG *) 0xFFFCC254) // (PWMC_CH2) Reserved -#define AT91C_PWMC_CH2_CMR ((AT91_REG *) 0xFFFCC240) // (PWMC_CH2) Channel Mode Register -#define AT91C_PWMC_CH2_CCNTR ((AT91_REG *) 0xFFFCC24C) // (PWMC_CH2) Channel Counter Register -#define AT91C_PWMC_CH2_CPRDR ((AT91_REG *) 0xFFFCC248) // (PWMC_CH2) Channel Period Register -#define AT91C_PWMC_CH2_CUPDR ((AT91_REG *) 0xFFFCC250) // (PWMC_CH2) Channel Update Register -#define AT91C_PWMC_CH2_CDTYR ((AT91_REG *) 0xFFFCC244) // (PWMC_CH2) Channel Duty Cycle Register +#define AT91C_PWMC_CH2_Reserved ((AT91_REG *) 0xFFFCC254) // (PWMC_CH2) Reserved +#define AT91C_PWMC_CH2_CMR ((AT91_REG *) 0xFFFCC240) // (PWMC_CH2) Channel Mode Register +#define AT91C_PWMC_CH2_CCNTR ((AT91_REG *) 0xFFFCC24C) // (PWMC_CH2) Channel Counter Register +#define AT91C_PWMC_CH2_CPRDR ((AT91_REG *) 0xFFFCC248) // (PWMC_CH2) Channel Period Register +#define AT91C_PWMC_CH2_CUPDR ((AT91_REG *) 0xFFFCC250) // (PWMC_CH2) Channel Update Register +#define AT91C_PWMC_CH2_CDTYR ((AT91_REG *) 0xFFFCC244) // (PWMC_CH2) Channel Duty Cycle Register // ========== Register definition for PWMC_CH1 peripheral ========== -#define AT91C_PWMC_CH1_Reserved ((AT91_REG *) 0xFFFCC234) // (PWMC_CH1) Reserved -#define AT91C_PWMC_CH1_CUPDR ((AT91_REG *) 0xFFFCC230) // (PWMC_CH1) Channel Update Register -#define AT91C_PWMC_CH1_CPRDR ((AT91_REG *) 0xFFFCC228) // (PWMC_CH1) Channel Period Register -#define AT91C_PWMC_CH1_CCNTR ((AT91_REG *) 0xFFFCC22C) // (PWMC_CH1) Channel Counter Register -#define AT91C_PWMC_CH1_CDTYR ((AT91_REG *) 0xFFFCC224) // (PWMC_CH1) Channel Duty Cycle Register -#define AT91C_PWMC_CH1_CMR ((AT91_REG *) 0xFFFCC220) // (PWMC_CH1) Channel Mode Register +#define AT91C_PWMC_CH1_Reserved ((AT91_REG *) 0xFFFCC234) // (PWMC_CH1) Reserved +#define AT91C_PWMC_CH1_CUPDR ((AT91_REG *) 0xFFFCC230) // (PWMC_CH1) Channel Update Register +#define AT91C_PWMC_CH1_CPRDR ((AT91_REG *) 0xFFFCC228) // (PWMC_CH1) Channel Period Register +#define AT91C_PWMC_CH1_CCNTR ((AT91_REG *) 0xFFFCC22C) // (PWMC_CH1) Channel Counter Register +#define AT91C_PWMC_CH1_CDTYR ((AT91_REG *) 0xFFFCC224) // (PWMC_CH1) Channel Duty Cycle Register +#define AT91C_PWMC_CH1_CMR ((AT91_REG *) 0xFFFCC220) // (PWMC_CH1) Channel Mode Register // ========== Register definition for PWMC_CH0 peripheral ========== -#define AT91C_PWMC_CH0_Reserved ((AT91_REG *) 0xFFFCC214) // (PWMC_CH0) Reserved -#define AT91C_PWMC_CH0_CPRDR ((AT91_REG *) 0xFFFCC208) // (PWMC_CH0) Channel Period Register -#define AT91C_PWMC_CH0_CDTYR ((AT91_REG *) 0xFFFCC204) // (PWMC_CH0) Channel Duty Cycle Register -#define AT91C_PWMC_CH0_CMR ((AT91_REG *) 0xFFFCC200) // (PWMC_CH0) Channel Mode Register -#define AT91C_PWMC_CH0_CUPDR ((AT91_REG *) 0xFFFCC210) // (PWMC_CH0) Channel Update Register -#define AT91C_PWMC_CH0_CCNTR ((AT91_REG *) 0xFFFCC20C) // (PWMC_CH0) Channel Counter Register +#define AT91C_PWMC_CH0_Reserved ((AT91_REG *) 0xFFFCC214) // (PWMC_CH0) Reserved +#define AT91C_PWMC_CH0_CPRDR ((AT91_REG *) 0xFFFCC208) // (PWMC_CH0) Channel Period Register +#define AT91C_PWMC_CH0_CDTYR ((AT91_REG *) 0xFFFCC204) // (PWMC_CH0) Channel Duty Cycle Register +#define AT91C_PWMC_CH0_CMR ((AT91_REG *) 0xFFFCC200) // (PWMC_CH0) Channel Mode Register +#define AT91C_PWMC_CH0_CUPDR ((AT91_REG *) 0xFFFCC210) // (PWMC_CH0) Channel Update Register +#define AT91C_PWMC_CH0_CCNTR ((AT91_REG *) 0xFFFCC20C) // (PWMC_CH0) Channel Counter Register // ========== Register definition for PWMC peripheral ========== -#define AT91C_PWMC_IDR ((AT91_REG *) 0xFFFCC014) // (PWMC) PWMC Interrupt Disable Register -#define AT91C_PWMC_DIS ((AT91_REG *) 0xFFFCC008) // (PWMC) PWMC Disable Register -#define AT91C_PWMC_IER ((AT91_REG *) 0xFFFCC010) // (PWMC) PWMC Interrupt Enable Register -#define AT91C_PWMC_VR ((AT91_REG *) 0xFFFCC0FC) // (PWMC) PWMC Version Register -#define AT91C_PWMC_ISR ((AT91_REG *) 0xFFFCC01C) // (PWMC) PWMC Interrupt Status Register -#define AT91C_PWMC_SR ((AT91_REG *) 0xFFFCC00C) // (PWMC) PWMC Status Register -#define AT91C_PWMC_IMR ((AT91_REG *) 0xFFFCC018) // (PWMC) PWMC Interrupt Mask Register -#define AT91C_PWMC_MR ((AT91_REG *) 0xFFFCC000) // (PWMC) PWMC Mode Register -#define AT91C_PWMC_ENA ((AT91_REG *) 0xFFFCC004) // (PWMC) PWMC Enable Register +#define AT91C_PWMC_IDR ((AT91_REG *) 0xFFFCC014) // (PWMC) PWMC Interrupt Disable Register +#define AT91C_PWMC_DIS ((AT91_REG *) 0xFFFCC008) // (PWMC) PWMC Disable Register +#define AT91C_PWMC_IER ((AT91_REG *) 0xFFFCC010) // (PWMC) PWMC Interrupt Enable Register +#define AT91C_PWMC_VR ((AT91_REG *) 0xFFFCC0FC) // (PWMC) PWMC Version Register +#define AT91C_PWMC_ISR ((AT91_REG *) 0xFFFCC01C) // (PWMC) PWMC Interrupt Status Register +#define AT91C_PWMC_SR ((AT91_REG *) 0xFFFCC00C) // (PWMC) PWMC Status Register +#define AT91C_PWMC_IMR ((AT91_REG *) 0xFFFCC018) // (PWMC) PWMC Interrupt Mask Register +#define AT91C_PWMC_MR ((AT91_REG *) 0xFFFCC000) // (PWMC) PWMC Mode Register +#define AT91C_PWMC_ENA ((AT91_REG *) 0xFFFCC004) // (PWMC) PWMC Enable Register // ========== Register definition for UDP peripheral ========== -#define AT91C_UDP_IMR ((AT91_REG *) 0xFFFB0018) // (UDP) Interrupt Mask Register -#define AT91C_UDP_FADDR ((AT91_REG *) 0xFFFB0008) // (UDP) Function Address Register -#define AT91C_UDP_NUM ((AT91_REG *) 0xFFFB0000) // (UDP) Frame Number Register -#define AT91C_UDP_FDR ((AT91_REG *) 0xFFFB0050) // (UDP) Endpoint FIFO Data Register -#define AT91C_UDP_ISR ((AT91_REG *) 0xFFFB001C) // (UDP) Interrupt Status Register -#define AT91C_UDP_CSR ((AT91_REG *) 0xFFFB0030) // (UDP) Endpoint Control and Status Register -#define AT91C_UDP_IDR ((AT91_REG *) 0xFFFB0014) // (UDP) Interrupt Disable Register -#define AT91C_UDP_ICR ((AT91_REG *) 0xFFFB0020) // (UDP) Interrupt Clear Register -#define AT91C_UDP_RSTEP ((AT91_REG *) 0xFFFB0028) // (UDP) Reset Endpoint Register -#define AT91C_UDP_TXVC ((AT91_REG *) 0xFFFB0074) // (UDP) Transceiver Control Register -#define AT91C_UDP_GLBSTATE ((AT91_REG *) 0xFFFB0004) // (UDP) Global State Register -#define AT91C_UDP_IER ((AT91_REG *) 0xFFFB0010) // (UDP) Interrupt Enable Register +#define AT91C_UDP_IMR ((AT91_REG *) 0xFFFB0018) // (UDP) Interrupt Mask Register +#define AT91C_UDP_FADDR ((AT91_REG *) 0xFFFB0008) // (UDP) Function Address Register +#define AT91C_UDP_NUM ((AT91_REG *) 0xFFFB0000) // (UDP) Frame Number Register +#define AT91C_UDP_FDR ((AT91_REG *) 0xFFFB0050) // (UDP) Endpoint FIFO Data Register +#define AT91C_UDP_ISR ((AT91_REG *) 0xFFFB001C) // (UDP) Interrupt Status Register +#define AT91C_UDP_CSR ((AT91_REG *) 0xFFFB0030) // (UDP) Endpoint Control and Status Register +#define AT91C_UDP_IDR ((AT91_REG *) 0xFFFB0014) // (UDP) Interrupt Disable Register +#define AT91C_UDP_ICR ((AT91_REG *) 0xFFFB0020) // (UDP) Interrupt Clear Register +#define AT91C_UDP_RSTEP ((AT91_REG *) 0xFFFB0028) // (UDP) Reset Endpoint Register +#define AT91C_UDP_TXVC ((AT91_REG *) 0xFFFB0074) // (UDP) Transceiver Control Register +#define AT91C_UDP_GLBSTATE ((AT91_REG *) 0xFFFB0004) // (UDP) Global State Register +#define AT91C_UDP_IER ((AT91_REG *) 0xFFFB0010) // (UDP) Interrupt Enable Register // ***************************************************************************** // PIO DEFINITIONS FOR AT91SAM7S256 // ***************************************************************************** -#define AT91C_PIO_PA0 ((unsigned int) 1 << 0) // Pin Controlled by PA0 -#define AT91C_PA0_PWM0 ((unsigned int) AT91C_PIO_PA0) // PWM Channel 0 -#define AT91C_PA0_TIOA0 ((unsigned int) AT91C_PIO_PA0) // Timer Counter 0 Multipurpose Timer I/O Pin A -#define AT91C_PIO_PA1 ((unsigned int) 1 << 1) // Pin Controlled by PA1 -#define AT91C_PA1_PWM1 ((unsigned int) AT91C_PIO_PA1) // PWM Channel 1 -#define AT91C_PA1_TIOB0 ((unsigned int) AT91C_PIO_PA1) // Timer Counter 0 Multipurpose Timer I/O Pin B -#define AT91C_PIO_PA10 ((unsigned int) 1 << 10) // Pin Controlled by PA10 -#define AT91C_PA10_DTXD ((unsigned int) AT91C_PIO_PA10) // DBGU Debug Transmit Data -#define AT91C_PA10_NPCS2 ((unsigned int) AT91C_PIO_PA10) // SPI Peripheral Chip Select 2 -#define AT91C_PIO_PA11 ((unsigned int) 1 << 11) // Pin Controlled by PA11 -#define AT91C_PA11_NPCS0 ((unsigned int) AT91C_PIO_PA11) // SPI Peripheral Chip Select 0 -#define AT91C_PA11_PWM0 ((unsigned int) AT91C_PIO_PA11) // PWM Channel 0 -#define AT91C_PIO_PA12 ((unsigned int) 1 << 12) // Pin Controlled by PA12 -#define AT91C_PA12_MISO ((unsigned int) AT91C_PIO_PA12) // SPI Master In Slave -#define AT91C_PA12_PWM1 ((unsigned int) AT91C_PIO_PA12) // PWM Channel 1 -#define AT91C_PIO_PA13 ((unsigned int) 1 << 13) // Pin Controlled by PA13 -#define AT91C_PA13_MOSI ((unsigned int) AT91C_PIO_PA13) // SPI Master Out Slave -#define AT91C_PA13_PWM2 ((unsigned int) AT91C_PIO_PA13) // PWM Channel 2 -#define AT91C_PIO_PA14 ((unsigned int) 1 << 14) // Pin Controlled by PA14 -#define AT91C_PA14_SPCK ((unsigned int) AT91C_PIO_PA14) // SPI Serial Clock -#define AT91C_PA14_PWM3 ((unsigned int) AT91C_PIO_PA14) // PWM Channel 3 -#define AT91C_PIO_PA15 ((unsigned int) 1 << 15) // Pin Controlled by PA15 -#define AT91C_PA15_TF ((unsigned int) AT91C_PIO_PA15) // SSC Transmit Frame Sync -#define AT91C_PA15_TIOA1 ((unsigned int) AT91C_PIO_PA15) // Timer Counter 1 Multipurpose Timer I/O Pin A -#define AT91C_PIO_PA16 ((unsigned int) 1 << 16) // Pin Controlled by PA16 -#define AT91C_PA16_TK ((unsigned int) AT91C_PIO_PA16) // SSC Transmit Clock -#define AT91C_PA16_TIOB1 ((unsigned int) AT91C_PIO_PA16) // Timer Counter 1 Multipurpose Timer I/O Pin B -#define AT91C_PIO_PA17 ((unsigned int) 1 << 17) // Pin Controlled by PA17 -#define AT91C_PA17_TD ((unsigned int) AT91C_PIO_PA17) // SSC Transmit data -#define AT91C_PA17_PCK1 ((unsigned int) AT91C_PIO_PA17) // PMC Programmable Clock Output 1 -#define AT91C_PIO_PA18 ((unsigned int) 1 << 18) // Pin Controlled by PA18 -#define AT91C_PA18_RD ((unsigned int) AT91C_PIO_PA18) // SSC Receive Data -#define AT91C_PA18_PCK2 ((unsigned int) AT91C_PIO_PA18) // PMC Programmable Clock Output 2 -#define AT91C_PIO_PA19 ((unsigned int) 1 << 19) // Pin Controlled by PA19 -#define AT91C_PA19_RK ((unsigned int) AT91C_PIO_PA19) // SSC Receive Clock -#define AT91C_PA19_FIQ ((unsigned int) AT91C_PIO_PA19) // AIC Fast Interrupt Input -#define AT91C_PIO_PA2 ((unsigned int) 1 << 2) // Pin Controlled by PA2 -#define AT91C_PA2_PWM2 ((unsigned int) AT91C_PIO_PA2) // PWM Channel 2 -#define AT91C_PA2_SCK0 ((unsigned int) AT91C_PIO_PA2) // USART 0 Serial Clock -#define AT91C_PIO_PA20 ((unsigned int) 1 << 20) // Pin Controlled by PA20 -#define AT91C_PA20_RF ((unsigned int) AT91C_PIO_PA20) // SSC Receive Frame Sync -#define AT91C_PA20_IRQ0 ((unsigned int) AT91C_PIO_PA20) // External Interrupt 0 -#define AT91C_PIO_PA21 ((unsigned int) 1 << 21) // Pin Controlled by PA21 -#define AT91C_PA21_RXD1 ((unsigned int) AT91C_PIO_PA21) // USART 1 Receive Data -#define AT91C_PA21_PCK1 ((unsigned int) AT91C_PIO_PA21) // PMC Programmable Clock Output 1 -#define AT91C_PIO_PA22 ((unsigned int) 1 << 22) // Pin Controlled by PA22 -#define AT91C_PA22_TXD1 ((unsigned int) AT91C_PIO_PA22) // USART 1 Transmit Data -#define AT91C_PA22_NPCS3 ((unsigned int) AT91C_PIO_PA22) // SPI Peripheral Chip Select 3 -#define AT91C_PIO_PA23 ((unsigned int) 1 << 23) // Pin Controlled by PA23 -#define AT91C_PA23_SCK1 ((unsigned int) AT91C_PIO_PA23) // USART 1 Serial Clock -#define AT91C_PA23_PWM0 ((unsigned int) AT91C_PIO_PA23) // PWM Channel 0 -#define AT91C_PIO_PA24 ((unsigned int) 1 << 24) // Pin Controlled by PA24 -#define AT91C_PA24_RTS1 ((unsigned int) AT91C_PIO_PA24) // USART 1 Ready To Send -#define AT91C_PA24_PWM1 ((unsigned int) AT91C_PIO_PA24) // PWM Channel 1 -#define AT91C_PIO_PA25 ((unsigned int) 1 << 25) // Pin Controlled by PA25 -#define AT91C_PA25_CTS1 ((unsigned int) AT91C_PIO_PA25) // USART 1 Clear To Send -#define AT91C_PA25_PWM2 ((unsigned int) AT91C_PIO_PA25) // PWM Channel 2 -#define AT91C_PIO_PA26 ((unsigned int) 1 << 26) // Pin Controlled by PA26 -#define AT91C_PA26_DCD1 ((unsigned int) AT91C_PIO_PA26) // USART 1 Data Carrier Detect -#define AT91C_PA26_TIOA2 ((unsigned int) AT91C_PIO_PA26) // Timer Counter 2 Multipurpose Timer I/O Pin A -#define AT91C_PIO_PA27 ((unsigned int) 1 << 27) // Pin Controlled by PA27 -#define AT91C_PA27_DTR1 ((unsigned int) AT91C_PIO_PA27) // USART 1 Data Terminal ready -#define AT91C_PA27_TIOB2 ((unsigned int) AT91C_PIO_PA27) // Timer Counter 2 Multipurpose Timer I/O Pin B -#define AT91C_PIO_PA28 ((unsigned int) 1 << 28) // Pin Controlled by PA28 -#define AT91C_PA28_DSR1 ((unsigned int) AT91C_PIO_PA28) // USART 1 Data Set ready -#define AT91C_PA28_TCLK1 ((unsigned int) AT91C_PIO_PA28) // Timer Counter 1 external clock input -#define AT91C_PIO_PA29 ((unsigned int) 1 << 29) // Pin Controlled by PA29 -#define AT91C_PA29_RI1 ((unsigned int) AT91C_PIO_PA29) // USART 1 Ring Indicator -#define AT91C_PA29_TCLK2 ((unsigned int) AT91C_PIO_PA29) // Timer Counter 2 external clock input -#define AT91C_PIO_PA3 ((unsigned int) 1 << 3) // Pin Controlled by PA3 -#define AT91C_PA3_TWD ((unsigned int) AT91C_PIO_PA3) // TWI Two-wire Serial Data -#define AT91C_PA3_NPCS3 ((unsigned int) AT91C_PIO_PA3) // SPI Peripheral Chip Select 3 -#define AT91C_PIO_PA30 ((unsigned int) 1 << 30) // Pin Controlled by PA30 -#define AT91C_PA30_IRQ1 ((unsigned int) AT91C_PIO_PA30) // External Interrupt 1 -#define AT91C_PA30_NPCS2 ((unsigned int) AT91C_PIO_PA30) // SPI Peripheral Chip Select 2 -#define AT91C_PIO_PA31 ((unsigned int) 1 << 31) // Pin Controlled by PA31 -#define AT91C_PA31_NPCS1 ((unsigned int) AT91C_PIO_PA31) // SPI Peripheral Chip Select 1 -#define AT91C_PA31_PCK2 ((unsigned int) AT91C_PIO_PA31) // PMC Programmable Clock Output 2 -#define AT91C_PIO_PA4 ((unsigned int) 1 << 4) // Pin Controlled by PA4 -#define AT91C_PA4_TWCK ((unsigned int) AT91C_PIO_PA4) // TWI Two-wire Serial Clock -#define AT91C_PA4_TCLK0 ((unsigned int) AT91C_PIO_PA4) // Timer Counter 0 external clock input -#define AT91C_PIO_PA5 ((unsigned int) 1 << 5) // Pin Controlled by PA5 -#define AT91C_PA5_RXD0 ((unsigned int) AT91C_PIO_PA5) // USART 0 Receive Data -#define AT91C_PA5_NPCS3 ((unsigned int) AT91C_PIO_PA5) // SPI Peripheral Chip Select 3 -#define AT91C_PIO_PA6 ((unsigned int) 1 << 6) // Pin Controlled by PA6 -#define AT91C_PA6_TXD0 ((unsigned int) AT91C_PIO_PA6) // USART 0 Transmit Data -#define AT91C_PA6_PCK0 ((unsigned int) AT91C_PIO_PA6) // PMC Programmable Clock Output 0 -#define AT91C_PIO_PA7 ((unsigned int) 1 << 7) // Pin Controlled by PA7 -#define AT91C_PA7_RTS0 ((unsigned int) AT91C_PIO_PA7) // USART 0 Ready To Send -#define AT91C_PA7_PWM3 ((unsigned int) AT91C_PIO_PA7) // PWM Channel 3 -#define AT91C_PIO_PA8 ((unsigned int) 1 << 8) // Pin Controlled by PA8 -#define AT91C_PA8_CTS0 ((unsigned int) AT91C_PIO_PA8) // USART 0 Clear To Send -#define AT91C_PA8_ADTRG ((unsigned int) AT91C_PIO_PA8) // ADC External Trigger -#define AT91C_PIO_PA9 ((unsigned int) 1 << 9) // Pin Controlled by PA9 -#define AT91C_PA9_DRXD ((unsigned int) AT91C_PIO_PA9) // DBGU Debug Receive Data -#define AT91C_PA9_NPCS1 ((unsigned int) AT91C_PIO_PA9) // SPI Peripheral Chip Select 1 +#define AT91C_PIO_PA0 ((unsigned int) 1 << 0) // Pin Controlled by PA0 +#define AT91C_PA0_PWM0 ((unsigned int) AT91C_PIO_PA0) // PWM Channel 0 +#define AT91C_PA0_TIOA0 ((unsigned int) AT91C_PIO_PA0) // Timer Counter 0 Multipurpose Timer I/O Pin A +#define AT91C_PIO_PA1 ((unsigned int) 1 << 1) // Pin Controlled by PA1 +#define AT91C_PA1_PWM1 ((unsigned int) AT91C_PIO_PA1) // PWM Channel 1 +#define AT91C_PA1_TIOB0 ((unsigned int) AT91C_PIO_PA1) // Timer Counter 0 Multipurpose Timer I/O Pin B +#define AT91C_PIO_PA10 ((unsigned int) 1 << 10) // Pin Controlled by PA10 +#define AT91C_PA10_DTXD ((unsigned int) AT91C_PIO_PA10) // DBGU Debug Transmit Data +#define AT91C_PA10_NPCS2 ((unsigned int) AT91C_PIO_PA10) // SPI Peripheral Chip Select 2 +#define AT91C_PIO_PA11 ((unsigned int) 1 << 11) // Pin Controlled by PA11 +#define AT91C_PA11_NPCS0 ((unsigned int) AT91C_PIO_PA11) // SPI Peripheral Chip Select 0 +#define AT91C_PA11_PWM0 ((unsigned int) AT91C_PIO_PA11) // PWM Channel 0 +#define AT91C_PIO_PA12 ((unsigned int) 1 << 12) // Pin Controlled by PA12 +#define AT91C_PA12_MISO ((unsigned int) AT91C_PIO_PA12) // SPI Master In Slave +#define AT91C_PA12_PWM1 ((unsigned int) AT91C_PIO_PA12) // PWM Channel 1 +#define AT91C_PIO_PA13 ((unsigned int) 1 << 13) // Pin Controlled by PA13 +#define AT91C_PA13_MOSI ((unsigned int) AT91C_PIO_PA13) // SPI Master Out Slave +#define AT91C_PA13_PWM2 ((unsigned int) AT91C_PIO_PA13) // PWM Channel 2 +#define AT91C_PIO_PA14 ((unsigned int) 1 << 14) // Pin Controlled by PA14 +#define AT91C_PA14_SPCK ((unsigned int) AT91C_PIO_PA14) // SPI Serial Clock +#define AT91C_PA14_PWM3 ((unsigned int) AT91C_PIO_PA14) // PWM Channel 3 +#define AT91C_PIO_PA15 ((unsigned int) 1 << 15) // Pin Controlled by PA15 +#define AT91C_PA15_TF ((unsigned int) AT91C_PIO_PA15) // SSC Transmit Frame Sync +#define AT91C_PA15_TIOA1 ((unsigned int) AT91C_PIO_PA15) // Timer Counter 1 Multipurpose Timer I/O Pin A +#define AT91C_PIO_PA16 ((unsigned int) 1 << 16) // Pin Controlled by PA16 +#define AT91C_PA16_TK ((unsigned int) AT91C_PIO_PA16) // SSC Transmit Clock +#define AT91C_PA16_TIOB1 ((unsigned int) AT91C_PIO_PA16) // Timer Counter 1 Multipurpose Timer I/O Pin B +#define AT91C_PIO_PA17 ((unsigned int) 1 << 17) // Pin Controlled by PA17 +#define AT91C_PA17_TD ((unsigned int) AT91C_PIO_PA17) // SSC Transmit data +#define AT91C_PA17_PCK1 ((unsigned int) AT91C_PIO_PA17) // PMC Programmable Clock Output 1 +#define AT91C_PIO_PA18 ((unsigned int) 1 << 18) // Pin Controlled by PA18 +#define AT91C_PA18_RD ((unsigned int) AT91C_PIO_PA18) // SSC Receive Data +#define AT91C_PA18_PCK2 ((unsigned int) AT91C_PIO_PA18) // PMC Programmable Clock Output 2 +#define AT91C_PIO_PA19 ((unsigned int) 1 << 19) // Pin Controlled by PA19 +#define AT91C_PA19_RK ((unsigned int) AT91C_PIO_PA19) // SSC Receive Clock +#define AT91C_PA19_FIQ ((unsigned int) AT91C_PIO_PA19) // AIC Fast Interrupt Input +#define AT91C_PIO_PA2 ((unsigned int) 1 << 2) // Pin Controlled by PA2 +#define AT91C_PA2_PWM2 ((unsigned int) AT91C_PIO_PA2) // PWM Channel 2 +#define AT91C_PA2_SCK0 ((unsigned int) AT91C_PIO_PA2) // USART 0 Serial Clock +#define AT91C_PIO_PA20 ((unsigned int) 1 << 20) // Pin Controlled by PA20 +#define AT91C_PA20_RF ((unsigned int) AT91C_PIO_PA20) // SSC Receive Frame Sync +#define AT91C_PA20_IRQ0 ((unsigned int) AT91C_PIO_PA20) // External Interrupt 0 +#define AT91C_PIO_PA21 ((unsigned int) 1 << 21) // Pin Controlled by PA21 +#define AT91C_PA21_RXD1 ((unsigned int) AT91C_PIO_PA21) // USART 1 Receive Data +#define AT91C_PA21_PCK1 ((unsigned int) AT91C_PIO_PA21) // PMC Programmable Clock Output 1 +#define AT91C_PIO_PA22 ((unsigned int) 1 << 22) // Pin Controlled by PA22 +#define AT91C_PA22_TXD1 ((unsigned int) AT91C_PIO_PA22) // USART 1 Transmit Data +#define AT91C_PA22_NPCS3 ((unsigned int) AT91C_PIO_PA22) // SPI Peripheral Chip Select 3 +#define AT91C_PIO_PA23 ((unsigned int) 1 << 23) // Pin Controlled by PA23 +#define AT91C_PA23_SCK1 ((unsigned int) AT91C_PIO_PA23) // USART 1 Serial Clock +#define AT91C_PA23_PWM0 ((unsigned int) AT91C_PIO_PA23) // PWM Channel 0 +#define AT91C_PIO_PA24 ((unsigned int) 1 << 24) // Pin Controlled by PA24 +#define AT91C_PA24_RTS1 ((unsigned int) AT91C_PIO_PA24) // USART 1 Ready To Send +#define AT91C_PA24_PWM1 ((unsigned int) AT91C_PIO_PA24) // PWM Channel 1 +#define AT91C_PIO_PA25 ((unsigned int) 1 << 25) // Pin Controlled by PA25 +#define AT91C_PA25_CTS1 ((unsigned int) AT91C_PIO_PA25) // USART 1 Clear To Send +#define AT91C_PA25_PWM2 ((unsigned int) AT91C_PIO_PA25) // PWM Channel 2 +#define AT91C_PIO_PA26 ((unsigned int) 1 << 26) // Pin Controlled by PA26 +#define AT91C_PA26_DCD1 ((unsigned int) AT91C_PIO_PA26) // USART 1 Data Carrier Detect +#define AT91C_PA26_TIOA2 ((unsigned int) AT91C_PIO_PA26) // Timer Counter 2 Multipurpose Timer I/O Pin A +#define AT91C_PIO_PA27 ((unsigned int) 1 << 27) // Pin Controlled by PA27 +#define AT91C_PA27_DTR1 ((unsigned int) AT91C_PIO_PA27) // USART 1 Data Terminal ready +#define AT91C_PA27_TIOB2 ((unsigned int) AT91C_PIO_PA27) // Timer Counter 2 Multipurpose Timer I/O Pin B +#define AT91C_PIO_PA28 ((unsigned int) 1 << 28) // Pin Controlled by PA28 +#define AT91C_PA28_DSR1 ((unsigned int) AT91C_PIO_PA28) // USART 1 Data Set ready +#define AT91C_PA28_TCLK1 ((unsigned int) AT91C_PIO_PA28) // Timer Counter 1 external clock input +#define AT91C_PIO_PA29 ((unsigned int) 1 << 29) // Pin Controlled by PA29 +#define AT91C_PA29_RI1 ((unsigned int) AT91C_PIO_PA29) // USART 1 Ring Indicator +#define AT91C_PA29_TCLK2 ((unsigned int) AT91C_PIO_PA29) // Timer Counter 2 external clock input +#define AT91C_PIO_PA3 ((unsigned int) 1 << 3) // Pin Controlled by PA3 +#define AT91C_PA3_TWD ((unsigned int) AT91C_PIO_PA3) // TWI Two-wire Serial Data +#define AT91C_PA3_NPCS3 ((unsigned int) AT91C_PIO_PA3) // SPI Peripheral Chip Select 3 +#define AT91C_PIO_PA30 ((unsigned int) 1 << 30) // Pin Controlled by PA30 +#define AT91C_PA30_IRQ1 ((unsigned int) AT91C_PIO_PA30) // External Interrupt 1 +#define AT91C_PA30_NPCS2 ((unsigned int) AT91C_PIO_PA30) // SPI Peripheral Chip Select 2 +#define AT91C_PIO_PA31 ((unsigned int) 1 << 31) // Pin Controlled by PA31 +#define AT91C_PA31_NPCS1 ((unsigned int) AT91C_PIO_PA31) // SPI Peripheral Chip Select 1 +#define AT91C_PA31_PCK2 ((unsigned int) AT91C_PIO_PA31) // PMC Programmable Clock Output 2 +#define AT91C_PIO_PA4 ((unsigned int) 1 << 4) // Pin Controlled by PA4 +#define AT91C_PA4_TWCK ((unsigned int) AT91C_PIO_PA4) // TWI Two-wire Serial Clock +#define AT91C_PA4_TCLK0 ((unsigned int) AT91C_PIO_PA4) // Timer Counter 0 external clock input +#define AT91C_PIO_PA5 ((unsigned int) 1 << 5) // Pin Controlled by PA5 +#define AT91C_PA5_RXD0 ((unsigned int) AT91C_PIO_PA5) // USART 0 Receive Data +#define AT91C_PA5_NPCS3 ((unsigned int) AT91C_PIO_PA5) // SPI Peripheral Chip Select 3 +#define AT91C_PIO_PA6 ((unsigned int) 1 << 6) // Pin Controlled by PA6 +#define AT91C_PA6_TXD0 ((unsigned int) AT91C_PIO_PA6) // USART 0 Transmit Data +#define AT91C_PA6_PCK0 ((unsigned int) AT91C_PIO_PA6) // PMC Programmable Clock Output 0 +#define AT91C_PIO_PA7 ((unsigned int) 1 << 7) // Pin Controlled by PA7 +#define AT91C_PA7_RTS0 ((unsigned int) AT91C_PIO_PA7) // USART 0 Ready To Send +#define AT91C_PA7_PWM3 ((unsigned int) AT91C_PIO_PA7) // PWM Channel 3 +#define AT91C_PIO_PA8 ((unsigned int) 1 << 8) // Pin Controlled by PA8 +#define AT91C_PA8_CTS0 ((unsigned int) AT91C_PIO_PA8) // USART 0 Clear To Send +#define AT91C_PA8_ADTRG ((unsigned int) AT91C_PIO_PA8) // ADC External Trigger +#define AT91C_PIO_PA9 ((unsigned int) 1 << 9) // Pin Controlled by PA9 +#define AT91C_PA9_DRXD ((unsigned int) AT91C_PIO_PA9) // DBGU Debug Receive Data +#define AT91C_PA9_NPCS1 ((unsigned int) AT91C_PIO_PA9) // SPI Peripheral Chip Select 1 // ***************************************************************************** // PERIPHERAL ID DEFINITIONS FOR AT91SAM7S256 // ***************************************************************************** -#define AT91C_ID_FIQ ((unsigned int) 0) // Advanced Interrupt Controller (FIQ) -#define AT91C_ID_SYS ((unsigned int) 1) // System Peripheral -#define AT91C_ID_PIOA ((unsigned int) 2) // Parallel IO Controller +#define AT91C_ID_FIQ ((unsigned int) 0) // Advanced Interrupt Controller (FIQ) +#define AT91C_ID_SYS ((unsigned int) 1) // System Peripheral +#define AT91C_ID_PIOA ((unsigned int) 2) // Parallel IO Controller #define AT91C_ID_3_Reserved ((unsigned int) 3) // Reserved -#define AT91C_ID_ADC ((unsigned int) 4) // Analog-to-Digital Converter -#define AT91C_ID_SPI ((unsigned int) 5) // Serial Peripheral Interface -#define AT91C_ID_US0 ((unsigned int) 6) // USART 0 -#define AT91C_ID_US1 ((unsigned int) 7) // USART 1 -#define AT91C_ID_SSC ((unsigned int) 8) // Serial Synchronous Controller -#define AT91C_ID_TWI ((unsigned int) 9) // Two-Wire Interface -#define AT91C_ID_PWMC ((unsigned int) 10) // PWM Controller -#define AT91C_ID_UDP ((unsigned int) 11) // USB Device Port -#define AT91C_ID_TC0 ((unsigned int) 12) // Timer Counter 0 -#define AT91C_ID_TC1 ((unsigned int) 13) // Timer Counter 1 -#define AT91C_ID_TC2 ((unsigned int) 14) // Timer Counter 2 -#define AT91C_ID_15_Reserved ((unsigned int) 15) // Reserved -#define AT91C_ID_16_Reserved ((unsigned int) 16) // Reserved -#define AT91C_ID_17_Reserved ((unsigned int) 17) // Reserved -#define AT91C_ID_18_Reserved ((unsigned int) 18) // Reserved -#define AT91C_ID_19_Reserved ((unsigned int) 19) // Reserved -#define AT91C_ID_20_Reserved ((unsigned int) 20) // Reserved -#define AT91C_ID_21_Reserved ((unsigned int) 21) // Reserved -#define AT91C_ID_22_Reserved ((unsigned int) 22) // Reserved -#define AT91C_ID_23_Reserved ((unsigned int) 23) // Reserved -#define AT91C_ID_24_Reserved ((unsigned int) 24) // Reserved -#define AT91C_ID_25_Reserved ((unsigned int) 25) // Reserved -#define AT91C_ID_26_Reserved ((unsigned int) 26) // Reserved -#define AT91C_ID_27_Reserved ((unsigned int) 27) // Reserved -#define AT91C_ID_28_Reserved ((unsigned int) 28) // Reserved -#define AT91C_ID_29_Reserved ((unsigned int) 29) // Reserved -#define AT91C_ID_IRQ0 ((unsigned int) 30) // Advanced Interrupt Controller (IRQ0) -#define AT91C_ID_IRQ1 ((unsigned int) 31) // Advanced Interrupt Controller (IRQ1) +#define AT91C_ID_ADC ((unsigned int) 4) // Analog-to-Digital Converter +#define AT91C_ID_SPI ((unsigned int) 5) // Serial Peripheral Interface +#define AT91C_ID_US0 ((unsigned int) 6) // USART 0 +#define AT91C_ID_US1 ((unsigned int) 7) // USART 1 +#define AT91C_ID_SSC ((unsigned int) 8) // Serial Synchronous Controller +#define AT91C_ID_TWI ((unsigned int) 9) // Two-Wire Interface +#define AT91C_ID_PWMC ((unsigned int) 10) // PWM Controller +#define AT91C_ID_UDP ((unsigned int) 11) // USB Device Port +#define AT91C_ID_TC0 ((unsigned int) 12) // Timer Counter 0 +#define AT91C_ID_TC1 ((unsigned int) 13) // Timer Counter 1 +#define AT91C_ID_TC2 ((unsigned int) 14) // Timer Counter 2 +#define AT91C_ID_15_Reserved ((unsigned int) 15) // Reserved +#define AT91C_ID_16_Reserved ((unsigned int) 16) // Reserved +#define AT91C_ID_17_Reserved ((unsigned int) 17) // Reserved +#define AT91C_ID_18_Reserved ((unsigned int) 18) // Reserved +#define AT91C_ID_19_Reserved ((unsigned int) 19) // Reserved +#define AT91C_ID_20_Reserved ((unsigned int) 20) // Reserved +#define AT91C_ID_21_Reserved ((unsigned int) 21) // Reserved +#define AT91C_ID_22_Reserved ((unsigned int) 22) // Reserved +#define AT91C_ID_23_Reserved ((unsigned int) 23) // Reserved +#define AT91C_ID_24_Reserved ((unsigned int) 24) // Reserved +#define AT91C_ID_25_Reserved ((unsigned int) 25) // Reserved +#define AT91C_ID_26_Reserved ((unsigned int) 26) // Reserved +#define AT91C_ID_27_Reserved ((unsigned int) 27) // Reserved +#define AT91C_ID_28_Reserved ((unsigned int) 28) // Reserved +#define AT91C_ID_29_Reserved ((unsigned int) 29) // Reserved +#define AT91C_ID_IRQ0 ((unsigned int) 30) // Advanced Interrupt Controller (IRQ0) +#define AT91C_ID_IRQ1 ((unsigned int) 31) // Advanced Interrupt Controller (IRQ1) // ***************************************************************************** // BASE ADDRESS DEFINITIONS FOR AT91SAM7S256 // ***************************************************************************** -#define AT91C_BASE_SYS ((AT91PS_SYS) 0xFFFFF000) // (SYS) Base Address -#define AT91C_BASE_AIC ((AT91PS_AIC) 0xFFFFF000) // (AIC) Base Address -#define AT91C_BASE_PDC_DBGU ((AT91PS_PDC) 0xFFFFF300) // (PDC_DBGU) Base Address -#define AT91C_BASE_DBGU ((AT91PS_DBGU) 0xFFFFF200) // (DBGU) Base Address -#define AT91C_BASE_PIOA ((AT91PS_PIO) 0xFFFFF400) // (PIOA) Base Address -#define AT91C_BASE_CKGR ((AT91PS_CKGR) 0xFFFFFC20) // (CKGR) Base Address -#define AT91C_BASE_PMC ((AT91PS_PMC) 0xFFFFFC00) // (PMC) Base Address -#define AT91C_BASE_RSTC ((AT91PS_RSTC) 0xFFFFFD00) // (RSTC) Base Address -#define AT91C_BASE_RTTC ((AT91PS_RTTC) 0xFFFFFD20) // (RTTC) Base Address -#define AT91C_BASE_PITC ((AT91PS_PITC) 0xFFFFFD30) // (PITC) Base Address -#define AT91C_BASE_WDTC ((AT91PS_WDTC) 0xFFFFFD40) // (WDTC) Base Address -#define AT91C_BASE_VREG ((AT91PS_VREG) 0xFFFFFD60) // (VREG) Base Address -#define AT91C_BASE_MC ((AT91PS_MC) 0xFFFFFF00) // (MC) Base Address -#define AT91C_BASE_PDC_SPI ((AT91PS_PDC) 0xFFFE0100) // (PDC_SPI) Base Address -#define AT91C_BASE_SPI ((AT91PS_SPI) 0xFFFE0000) // (SPI) Base Address -#define AT91C_BASE_PDC_ADC ((AT91PS_PDC) 0xFFFD8100) // (PDC_ADC) Base Address -#define AT91C_BASE_ADC ((AT91PS_ADC) 0xFFFD8000) // (ADC) Base Address -#define AT91C_BASE_PDC_SSC ((AT91PS_PDC) 0xFFFD4100) // (PDC_SSC) Base Address -#define AT91C_BASE_SSC ((AT91PS_SSC) 0xFFFD4000) // (SSC) Base Address -#define AT91C_BASE_PDC_US1 ((AT91PS_PDC) 0xFFFC4100) // (PDC_US1) Base Address -#define AT91C_BASE_US1 ((AT91PS_USART) 0xFFFC4000) // (US1) Base Address -#define AT91C_BASE_PDC_US0 ((AT91PS_PDC) 0xFFFC0100) // (PDC_US0) Base Address -#define AT91C_BASE_US0 ((AT91PS_USART) 0xFFFC0000) // (US0) Base Address -#define AT91C_BASE_TWI ((AT91PS_TWI) 0xFFFB8000) // (TWI) Base Address -#define AT91C_BASE_TC0 ((AT91PS_TC) 0xFFFA0000) // (TC0) Base Address -#define AT91C_BASE_TC1 ((AT91PS_TC) 0xFFFA0040) // (TC1) Base Address -#define AT91C_BASE_TC2 ((AT91PS_TC) 0xFFFA0080) // (TC2) Base Address -#define AT91C_BASE_TCB ((AT91PS_TCB) 0xFFFA0000) // (TCB) Base Address -#define AT91C_BASE_PWMC_CH3 ((AT91PS_PWMC_CH) 0xFFFCC260) // (PWMC_CH3) Base Address -#define AT91C_BASE_PWMC_CH2 ((AT91PS_PWMC_CH) 0xFFFCC240) // (PWMC_CH2) Base Address -#define AT91C_BASE_PWMC_CH1 ((AT91PS_PWMC_CH) 0xFFFCC220) // (PWMC_CH1) Base Address -#define AT91C_BASE_PWMC_CH0 ((AT91PS_PWMC_CH) 0xFFFCC200) // (PWMC_CH0) Base Address -#define AT91C_BASE_PWMC ((AT91PS_PWMC) 0xFFFCC000) // (PWMC) Base Address -#define AT91C_BASE_UDP ((AT91PS_UDP) 0xFFFB0000) // (UDP) Base Address +#define AT91C_BASE_SYS ((AT91PS_SYS) 0xFFFFF000) // (SYS) Base Address +#define AT91C_BASE_AIC ((AT91PS_AIC) 0xFFFFF000) // (AIC) Base Address +#define AT91C_BASE_PDC_DBGU ((AT91PS_PDC) 0xFFFFF300) // (PDC_DBGU) Base Address +#define AT91C_BASE_DBGU ((AT91PS_DBGU) 0xFFFFF200) // (DBGU) Base Address +#define AT91C_BASE_PIOA ((AT91PS_PIO) 0xFFFFF400) // (PIOA) Base Address +#define AT91C_BASE_CKGR ((AT91PS_CKGR) 0xFFFFFC20) // (CKGR) Base Address +#define AT91C_BASE_PMC ((AT91PS_PMC) 0xFFFFFC00) // (PMC) Base Address +#define AT91C_BASE_RSTC ((AT91PS_RSTC) 0xFFFFFD00) // (RSTC) Base Address +#define AT91C_BASE_RTTC ((AT91PS_RTTC) 0xFFFFFD20) // (RTTC) Base Address +#define AT91C_BASE_PITC ((AT91PS_PITC) 0xFFFFFD30) // (PITC) Base Address +#define AT91C_BASE_WDTC ((AT91PS_WDTC) 0xFFFFFD40) // (WDTC) Base Address +#define AT91C_BASE_VREG ((AT91PS_VREG) 0xFFFFFD60) // (VREG) Base Address +#define AT91C_BASE_MC ((AT91PS_MC) 0xFFFFFF00) // (MC) Base Address +#define AT91C_BASE_PDC_SPI ((AT91PS_PDC) 0xFFFE0100) // (PDC_SPI) Base Address +#define AT91C_BASE_SPI ((AT91PS_SPI) 0xFFFE0000) // (SPI) Base Address +#define AT91C_BASE_PDC_ADC ((AT91PS_PDC) 0xFFFD8100) // (PDC_ADC) Base Address +#define AT91C_BASE_ADC ((AT91PS_ADC) 0xFFFD8000) // (ADC) Base Address +#define AT91C_BASE_PDC_SSC ((AT91PS_PDC) 0xFFFD4100) // (PDC_SSC) Base Address +#define AT91C_BASE_SSC ((AT91PS_SSC) 0xFFFD4000) // (SSC) Base Address +#define AT91C_BASE_PDC_US1 ((AT91PS_PDC) 0xFFFC4100) // (PDC_US1) Base Address +#define AT91C_BASE_US1 ((AT91PS_USART) 0xFFFC4000) // (US1) Base Address +#define AT91C_BASE_PDC_US0 ((AT91PS_PDC) 0xFFFC0100) // (PDC_US0) Base Address +#define AT91C_BASE_US0 ((AT91PS_USART) 0xFFFC0000) // (US0) Base Address +#define AT91C_BASE_TWI ((AT91PS_TWI) 0xFFFB8000) // (TWI) Base Address +#define AT91C_BASE_TC0 ((AT91PS_TC) 0xFFFA0000) // (TC0) Base Address +#define AT91C_BASE_TC1 ((AT91PS_TC) 0xFFFA0040) // (TC1) Base Address +#define AT91C_BASE_TC2 ((AT91PS_TC) 0xFFFA0080) // (TC2) Base Address +#define AT91C_BASE_TCB ((AT91PS_TCB) 0xFFFA0000) // (TCB) Base Address +#define AT91C_BASE_PWMC_CH3 ((AT91PS_PWMC_CH) 0xFFFCC260) // (PWMC_CH3) Base Address +#define AT91C_BASE_PWMC_CH2 ((AT91PS_PWMC_CH) 0xFFFCC240) // (PWMC_CH2) Base Address +#define AT91C_BASE_PWMC_CH1 ((AT91PS_PWMC_CH) 0xFFFCC220) // (PWMC_CH1) Base Address +#define AT91C_BASE_PWMC_CH0 ((AT91PS_PWMC_CH) 0xFFFCC200) // (PWMC_CH0) Base Address +#define AT91C_BASE_PWMC ((AT91PS_PWMC) 0xFFFCC000) // (PWMC) Base Address +#define AT91C_BASE_UDP ((AT91PS_UDP) 0xFFFB0000) // (UDP) Base Address // ***************************************************************************** // MEMORY MAPPING DEFINITIONS FOR AT91SAM7S256 // ***************************************************************************** -#define AT91C_ISRAM ((char *) 0x00200000) // Internal SRAM base address -#define AT91C_ISRAM_SIZE ((unsigned int) 0x00010000) // Internal SRAM size in byte (64 Kbyte) -#define AT91C_IFLASH ((char *) 0x00100000) // Internal ROM base address -#define AT91C_IFLASH_SIZE ((unsigned int) 0x00040000) // Internal ROM size in byte (256 Kbyte) +#define AT91C_ISRAM ((char *) 0x00200000) // Internal SRAM base address +#define AT91C_ISRAM_SIZE ((unsigned int) 0x00010000) // Internal SRAM size in byte (64 Kbyte) +#define AT91C_IFLASH ((char *) 0x00100000) // Internal ROM base address +#define AT91C_IFLASH_SIZE ((unsigned int) 0x00040000) // Internal ROM size in byte (256 Kbyte) #endif diff --git a/bacnet-stack/ports/at91sam7s/av.c b/bacnet-stack/ports/at91sam7s/av.c index da344cd8..59c0f044 100644 --- a/bacnet-stack/ports/at91sam7s/av.c +++ b/bacnet-stack/ports/at91sam7s/av.c @@ -31,12 +31,12 @@ #include "bacdcode.h" #include "bacenum.h" #include "bacapp.h" -#include "config.h" /* the custom stuff */ +#include "config.h" /* the custom stuff */ #include "wp.h" #define MAX_ANALOG_VALUES 4 #if (MAX_ANALOG_VALUES > 9) - #error Modify the Analog_Value_Name to handle multiple digits +#error Modify the Analog_Value_Name to handle multiple digits #endif /* we choose to have a NULL level in our system represented by */ @@ -54,7 +54,8 @@ static uint8_t Present_Value[MAX_ANALOG_VALUES]; /* we need to have our arrays initialized before answering any calls */ static bool Analog_Value_Initialized = false; -void Analog_Value_Init(void) +void Analog_Value_Init( + void) { unsigned i; @@ -73,7 +74,8 @@ void Analog_Value_Init(void) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need validate that the */ /* given instance exists */ -bool Analog_Value_Valid_Instance(uint32_t object_instance) +bool Analog_Value_Valid_Instance( + uint32_t object_instance) { Analog_Value_Init(); if (object_instance < MAX_ANALOG_VALUES) @@ -84,7 +86,8 @@ bool Analog_Value_Valid_Instance(uint32_t object_instance) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then count how many you have */ -unsigned Analog_Value_Count(void) +unsigned Analog_Value_Count( + void) { Analog_Value_Init(); return MAX_ANALOG_VALUES; @@ -93,7 +96,8 @@ unsigned Analog_Value_Count(void) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need to return the instance */ /* that correlates to the correct index */ -uint32_t Analog_Value_Index_To_Instance(unsigned index) +uint32_t Analog_Value_Index_To_Instance( + unsigned index) { Analog_Value_Init(); return index; @@ -102,7 +106,8 @@ uint32_t Analog_Value_Index_To_Instance(unsigned index) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need to return the index */ /* that correlates to the correct instance number */ -unsigned Analog_Value_Instance_To_Index(uint32_t object_instance) +unsigned Analog_Value_Instance_To_Index( + uint32_t object_instance) { unsigned index = MAX_ANALOG_VALUES; @@ -113,7 +118,8 @@ unsigned Analog_Value_Instance_To_Index(uint32_t object_instance) return index; } -static float Analog_Value_Present_Value(uint32_t object_instance) +static float Analog_Value_Present_Value( + uint32_t object_instance) { float value = ANALOG_RELINQUISH_DEFAULT; unsigned index = 0; @@ -128,12 +134,13 @@ static float Analog_Value_Present_Value(uint32_t object_instance) } /* note: the object name must be unique within this device */ -char *Analog_Value_Name(uint32_t object_instance) +char *Analog_Value_Name( + uint32_t object_instance) { - static char text_string[16] = "AV-0"; /* okay for single thread */ + static char text_string[16] = "AV-0"; /* okay for single thread */ if (object_instance < MAX_ANALOG_VALUES) { - text_string[3] = '0' + (uint8_t)object_instance; + text_string[3] = '0' + (uint8_t) object_instance; return text_string; } @@ -141,13 +148,15 @@ char *Analog_Value_Name(uint32_t object_instance) } /* return apdu len, or -1 on error */ -int Analog_Value_Encode_Property_APDU(uint8_t * apdu, +int Analog_Value_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ BACNET_BIT_STRING bit_string; BACNET_CHARACTER_STRING char_string; float real_value = (float) 1.414; @@ -160,110 +169,118 @@ int Analog_Value_Encode_Property_APDU(uint8_t * apdu, Analog_Value_Init(); switch (property) { - case PROP_OBJECT_IDENTIFIER: - apdu_len = encode_application_object_id(&apdu[0], OBJECT_ANALOG_VALUE, - object_instance); - break; - case PROP_OBJECT_NAME: - case PROP_DESCRIPTION: - characterstring_init_ansi(&char_string, - Analog_Value_Name(object_instance)); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_OBJECT_TYPE: - apdu_len = encode_application_enumerated(&apdu[0], OBJECT_ANALOG_VALUE); - break; - case PROP_PRESENT_VALUE: - real_value = Analog_Value_Present_Value(object_instance); - apdu_len = encode_application_real(&apdu[0], real_value); - break; - case PROP_STATUS_FLAGS: - bitstring_init(&bit_string); - bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); - apdu_len = encode_application_bitstring(&apdu[0], &bit_string); - break; - case PROP_EVENT_STATE: - apdu_len = encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); - break; - case PROP_OUT_OF_SERVICE: -#if 0 - object_index = Analog_Value_Instance_To_Index(object_instance); - state = Analog_Value_Out_Of_Service[object_index]; -#endif - apdu_len = encode_application_boolean(&apdu[0], false); - break; - case PROP_UNITS: - apdu_len = encode_application_enumerated(&apdu[0], UNITS_PERCENT); - break; -#if 0 - case PROP_PRIORITY_ARRAY: - /* Array element zero is the number of elements in the array */ - if (array_index == 0) + case PROP_OBJECT_IDENTIFIER: apdu_len = - encode_application_unsigned(&apdu[0], BACNET_MAX_PRIORITY); - /* if no index was specified, then try to encode the entire list */ - /* into one packet. */ - else if (array_index == BACNET_ARRAY_ALL) { + encode_application_object_id(&apdu[0], OBJECT_ANALOG_VALUE, + object_instance); + break; + case PROP_OBJECT_NAME: + case PROP_DESCRIPTION: + characterstring_init_ansi(&char_string, + Analog_Value_Name(object_instance)); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_OBJECT_TYPE: + apdu_len = + encode_application_enumerated(&apdu[0], OBJECT_ANALOG_VALUE); + break; + case PROP_PRESENT_VALUE: + real_value = Analog_Value_Present_Value(object_instance); + apdu_len = encode_application_real(&apdu[0], real_value); + break; + case PROP_STATUS_FLAGS: + bitstring_init(&bit_string); + bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); + apdu_len = encode_application_bitstring(&apdu[0], &bit_string); + break; + case PROP_EVENT_STATE: + apdu_len = + encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); + break; + case PROP_OUT_OF_SERVICE: +#if 0 object_index = Analog_Value_Instance_To_Index(object_instance); - for (i = 0; i < BACNET_MAX_PRIORITY; i++) { - /* FIXME: check if we have room before adding it to APDU */ - if (Present_Value[object_index][i] == - ANALOG_LEVEL_NULL) - len = encode_application_null(&apdu[apdu_len]); - else { - real_value = Present_Value[object_index][i]; - len = encode_application_real(&apdu[apdu_len], real_value); - } - /* add it if we have room */ - if ((apdu_len + len) < MAX_APDU) - apdu_len += len; - else { - *error_class = ERROR_CLASS_SERVICES; - *error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT; - apdu_len = -1; - break; - } - } - } else { - object_index = Analog_Value_Instance_To_Index(object_instance); - if (array_index <= BACNET_MAX_PRIORITY) { - if (Present_Value[object_index][array_index - 1] == - ANALOG_LEVEL_NULL) - apdu_len = encode_application_null(&apdu[0]); - else { - real_value = - Present_Value[object_index][array_index - 1]; - apdu_len = encode_application_real(&apdu[0], real_value); + state = Analog_Value_Out_Of_Service[object_index]; +#endif + apdu_len = encode_application_boolean(&apdu[0], false); + break; + case PROP_UNITS: + apdu_len = encode_application_enumerated(&apdu[0], UNITS_PERCENT); + break; +#if 0 + case PROP_PRIORITY_ARRAY: + /* Array element zero is the number of elements in the array */ + if (array_index == 0) + apdu_len = + encode_application_unsigned(&apdu[0], BACNET_MAX_PRIORITY); + /* if no index was specified, then try to encode the entire list */ + /* into one packet. */ + else if (array_index == BACNET_ARRAY_ALL) { + object_index = Analog_Value_Instance_To_Index(object_instance); + for (i = 0; i < BACNET_MAX_PRIORITY; i++) { + /* FIXME: check if we have room before adding it to APDU */ + if (Present_Value[object_index][i] == ANALOG_LEVEL_NULL) + len = encode_application_null(&apdu[apdu_len]); + else { + real_value = Present_Value[object_index][i]; + len = + encode_application_real(&apdu[apdu_len], + real_value); + } + /* add it if we have room */ + if ((apdu_len + len) < MAX_APDU) + apdu_len += len; + else { + *error_class = ERROR_CLASS_SERVICES; + *error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT; + apdu_len = -1; + break; + } } } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_ARRAY_INDEX; - apdu_len = -1; + object_index = Analog_Value_Instance_To_Index(object_instance); + if (array_index <= BACNET_MAX_PRIORITY) { + if (Present_Value[object_index][array_index - 1] == + ANALOG_LEVEL_NULL) + apdu_len = encode_application_null(&apdu[0]); + else { + real_value = + Present_Value[object_index][array_index - 1]; + apdu_len = + encode_application_real(&apdu[0], real_value); + } + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_ARRAY_INDEX; + apdu_len = -1; + } } - } - break; - case PROP_RELINQUISH_DEFAULT: - real_value = ANALOG_RELINQUISH_DEFAULT; - apdu_len = encode_application_real(&apdu[0], real_value); - break; + break; + case PROP_RELINQUISH_DEFAULT: + real_value = ANALOG_RELINQUISH_DEFAULT; + apdu_len = encode_application_real(&apdu[0], real_value); + break; #endif - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_UNKNOWN_PROPERTY; - apdu_len = -1; - break; + default: + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_UNKNOWN_PROPERTY; + apdu_len = -1; + break; } return apdu_len; } /* returns true if successful */ -bool Analog_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) +bool Analog_Value_Write_Property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { bool status = false; /* return value */ unsigned int object_index = 0; @@ -284,80 +301,80 @@ bool Analog_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, /* FIXME: len < application_data_len: more data? */ /* FIXME: len == 0: unable to decode? */ switch (wp_data->object_property) { - case PROP_PRESENT_VALUE: - if (value.tag == BACNET_APPLICATION_TAG_REAL) { - priority = wp_data->priority; - /* Command priority 6 is reserved for use by Minimum On/Off - algorithm and may not be used for other purposes in any - object. */ - if (priority && (priority <= BACNET_MAX_PRIORITY) && - (priority != 6 /* reserved */ ) && - (value.type.Real >= 0.0) && (value.type.Real <= 100.0)) { - level = (uint8_t) value.type.Real; - object_index = - Analog_Value_Instance_To_Index(wp_data-> - object_instance); - priority--; - Present_Value[object_index] = level; - /* Note: you could set the physical output here if we - are the highest priority. - However, if Out of Service is TRUE, then don't set the - physical output. This comment may apply to the - main loop (i.e. check out of service before changing output) */ - status = true; - } else if (priority == 6) { + case PROP_PRESENT_VALUE: + if (value.tag == BACNET_APPLICATION_TAG_REAL) { + priority = wp_data->priority; /* Command priority 6 is reserved for use by Minimum On/Off algorithm and may not be used for other purposes in any object. */ - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + if (priority && (priority <= BACNET_MAX_PRIORITY) && + (priority != 6 /* reserved */ ) && + (value.type.Real >= 0.0) && (value.type.Real <= 100.0)) { + level = (uint8_t) value.type.Real; + object_index = + Analog_Value_Instance_To_Index(wp_data-> + object_instance); + priority--; + Present_Value[object_index] = level; + /* Note: you could set the physical output here if we + are the highest priority. + However, if Out of Service is TRUE, then don't set the + physical output. This comment may apply to the + main loop (i.e. check out of service before changing output) */ + status = true; + } else if (priority == 6) { + /* Command priority 6 is reserved for use by Minimum On/Off + algorithm and may not be used for other purposes in any + object. */ + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } +#if 0 + } else if (value.tag == BACNET_APPLICATION_TAG_NULL) { + level = ANALOG_LEVEL_NULL; + object_index = + Analog_Value_Instance_To_Index(wp_data->object_instance); + priority = wp_data->priority; + if (priority && (priority <= BACNET_MAX_PRIORITY)) { + priority--; + Present_Value[object_index][priority] = level; + /* Note: you could set the physical output here to the next + highest priority, or to the relinquish default if no + priorities are set. + However, if Out of Service is TRUE, then don't set the + physical output. This comment may apply to the + main loop (i.e. check out of service before changing output) */ + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } +#endif } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } + break; #if 0 - } else if (value.tag == BACNET_APPLICATION_TAG_NULL) { - level = ANALOG_LEVEL_NULL; - object_index = - Analog_Value_Instance_To_Index(wp_data->object_instance); - priority = wp_data->priority; - if (priority && (priority <= BACNET_MAX_PRIORITY)) { - priority--; - Present_Value[object_index][priority] = level; - /* Note: you could set the physical output here to the next - highest priority, or to the relinquish default if no - priorities are set. - However, if Out of Service is TRUE, then don't set the - physical output. This comment may apply to the - main loop (i.e. check out of service before changing output) */ + case PROP_OUT_OF_SERVICE: + if (value.tag == BACNET_APPLICATION_TAG_BOOLEAN) { + object_index = + Analog_Value_Instance_To_Index(wp_data->object_instance); + Analog_Value_Out_Of_Service[object_index] = value.type.Boolean; status = true; } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } + break; #endif - } else { + default: *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; -#if 0 - case PROP_OUT_OF_SERVICE: - if (value.tag == BACNET_APPLICATION_TAG_BOOLEAN) { - object_index = - Analog_Value_Instance_To_Index(wp_data->object_instance); - Analog_Value_Out_Of_Service[object_index] = value.type.Boolean; - status = true; - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; -#endif - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; - break; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + break; } return status; @@ -369,7 +386,8 @@ bool Analog_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, #include #include "ctest.h" -void testAnalog_Value(Test * pTest) +void testAnalog_Value( + Test * pTest) { uint8_t apdu[MAX_APDU] = { 0 }; int len = 0; @@ -384,8 +402,7 @@ void testAnalog_Value(Test * pTest) len = Analog_Value_Encode_Property_APDU(&apdu[0], instance, - PROP_OBJECT_IDENTIFIER, - BACNET_ARRAY_ALL, &error_class, &error_code); + PROP_OBJECT_IDENTIFIER, BACNET_ARRAY_ALL, &error_class, &error_code); ct_test(pTest, len != 0); len = decode_tag_number_and_value(&apdu[0], &tag_number, &len_value); ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_OBJECT_ID); @@ -398,7 +415,8 @@ void testAnalog_Value(Test * pTest) } #ifdef TEST_ANALOG_VALUE -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -415,5 +433,5 @@ int main(void) return 0; } -#endif /* TEST_ANALOG_VALUE */ -#endif /* TEST */ +#endif /* TEST_ANALOG_VALUE */ +#endif /* TEST */ diff --git a/bacnet-stack/ports/at91sam7s/bi.c b/bacnet-stack/ports/at91sam7s/bi.c index e17e5568..b08b7d31 100644 --- a/bacnet-stack/ports/at91sam7s/bi.c +++ b/bacnet-stack/ports/at91sam7s/bi.c @@ -35,26 +35,28 @@ #define MAX_BINARY_INPUTS 8 #if (MAX_BINARY_INPUTS > 9) - #error Modify the Binary_Input_Name to handle multiple digits +#error Modify the Binary_Input_Name to handle multiple digits #endif static BACNET_BINARY_PV Present_Value[MAX_BINARY_INPUTS]; -static void Binary_Input_Initialize(void) +static void Binary_Input_Initialize( + void) { - static bool initialized = false; - unsigned i; - - if (!initialized) { - initialized = true; - for (i = 0; i < MAX_BINARY_INPUTS; i++) { - Present_Value[i] = BINARY_INACTIVE; + static bool initialized = false; + unsigned i; + + if (!initialized) { + initialized = true; + for (i = 0; i < MAX_BINARY_INPUTS; i++) { + Present_Value[i] = BINARY_INACTIVE; + } } - } } /* we simply have 0-n object instances. */ -bool Binary_Input_Valid_Instance(uint32_t object_instance) +bool Binary_Input_Valid_Instance( + uint32_t object_instance) { if (object_instance < MAX_BINARY_INPUTS) return true; @@ -63,13 +65,15 @@ bool Binary_Input_Valid_Instance(uint32_t object_instance) } /* we simply have 0-n object instances. */ -unsigned Binary_Input_Count(void) +unsigned Binary_Input_Count( + void) { return MAX_BINARY_INPUTS; } /* we simply have 0-n object instances.*/ -uint32_t Binary_Input_Index_To_Instance(unsigned index) +uint32_t Binary_Input_Index_To_Instance( + unsigned index) { return index; } @@ -77,7 +81,8 @@ uint32_t Binary_Input_Index_To_Instance(unsigned index) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need to return the index */ /* that correlates to the correct instance number */ -unsigned Binary_Input_Instance_To_Index(uint32_t object_instance) +unsigned Binary_Input_Instance_To_Index( + uint32_t object_instance) { unsigned index = MAX_BINARY_INPUTS; @@ -87,8 +92,8 @@ unsigned Binary_Input_Instance_To_Index(uint32_t object_instance) return index; } -static BACNET_BINARY_PV Binary_Input_Present_Value(uint32_t - object_instance) +static BACNET_BINARY_PV Binary_Input_Present_Value( + uint32_t object_instance) { BACNET_BINARY_PV value = BINARY_INACTIVE; unsigned index = 0; @@ -96,18 +101,19 @@ static BACNET_BINARY_PV Binary_Input_Present_Value(uint32_t Binary_Input_Initialize(); index = Binary_Input_Instance_To_Index(object_instance); if (index < MAX_BINARY_INPUTS) { - value = Present_Value[index]; + value = Present_Value[index]; } return value; } -char *Binary_Input_Name(uint32_t object_instance) +char *Binary_Input_Name( + uint32_t object_instance) { - static char text_string[16] = "BI-0"; /* okay for single thread */ + static char text_string[16] = "BI-0"; /* okay for single thread */ if (object_instance < MAX_BINARY_INPUTS) { - text_string[3] = '0' + (uint8_t)object_instance; + text_string[3] = '0' + (uint8_t) object_instance; return text_string; } @@ -116,13 +122,15 @@ char *Binary_Input_Name(uint32_t object_instance) /* return apdu length, or -1 on error */ /* assumption - object already exists, and has been bounds checked */ -int Binary_Input_Encode_Property_APDU(uint8_t * apdu, +int Binary_Input_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ BACNET_BIT_STRING bit_string; BACNET_CHARACTER_STRING char_string; BACNET_POLARITY polarity = POLARITY_NORMAL; @@ -132,48 +140,52 @@ int Binary_Input_Encode_Property_APDU(uint8_t * apdu, (void) array_index; Binary_Input_Initialize(); switch (property) { - case PROP_OBJECT_IDENTIFIER: - apdu_len = encode_application_object_id(&apdu[0], OBJECT_BINARY_INPUT, - object_instance); - break; - case PROP_OBJECT_NAME: - case PROP_DESCRIPTION: - /* note: object name must be unique in our device */ - characterstring_init_ansi(&char_string, - Binary_Input_Name(object_instance)); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_OBJECT_TYPE: - apdu_len = encode_application_enumerated(&apdu[0], OBJECT_BINARY_INPUT); - break; - case PROP_PRESENT_VALUE: - value = Binary_Input_Present_Value(object_instance); - apdu_len = encode_application_enumerated(&apdu[0],value); - break; - case PROP_STATUS_FLAGS: - /* note: see the details in the standard on how to use these */ - bitstring_init(&bit_string); - bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); - apdu_len = encode_application_bitstring(&apdu[0], &bit_string); - break; - case PROP_EVENT_STATE: - /* note: see the details in the standard on how to use this */ - apdu_len = encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); - break; - case PROP_OUT_OF_SERVICE: - apdu_len = encode_application_boolean(&apdu[0], false); - break; - case PROP_POLARITY: - apdu_len = encode_application_enumerated(&apdu[0], polarity); - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_UNKNOWN_PROPERTY; - apdu_len = -1; - break; + case PROP_OBJECT_IDENTIFIER: + apdu_len = + encode_application_object_id(&apdu[0], OBJECT_BINARY_INPUT, + object_instance); + break; + case PROP_OBJECT_NAME: + case PROP_DESCRIPTION: + /* note: object name must be unique in our device */ + characterstring_init_ansi(&char_string, + Binary_Input_Name(object_instance)); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_OBJECT_TYPE: + apdu_len = + encode_application_enumerated(&apdu[0], OBJECT_BINARY_INPUT); + break; + case PROP_PRESENT_VALUE: + value = Binary_Input_Present_Value(object_instance); + apdu_len = encode_application_enumerated(&apdu[0], value); + break; + case PROP_STATUS_FLAGS: + /* note: see the details in the standard on how to use these */ + bitstring_init(&bit_string); + bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); + apdu_len = encode_application_bitstring(&apdu[0], &bit_string); + break; + case PROP_EVENT_STATE: + /* note: see the details in the standard on how to use this */ + apdu_len = + encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); + break; + case PROP_OUT_OF_SERVICE: + apdu_len = encode_application_boolean(&apdu[0], false); + break; + case PROP_POLARITY: + apdu_len = encode_application_enumerated(&apdu[0], polarity); + break; + default: + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_UNKNOWN_PROPERTY; + apdu_len = -1; + break; } return apdu_len; @@ -184,7 +196,8 @@ int Binary_Input_Encode_Property_APDU(uint8_t * apdu, #include #include "ctest.h" -void testBinaryInput(Test * pTest) +void testBinaryInput( + Test * pTest) { uint8_t apdu[MAX_APDU] = { 0 }; int len = 0; @@ -200,8 +213,7 @@ void testBinaryInput(Test * pTest) /* FIXME: we should do a lot more testing here... */ len = Binary_Input_Encode_Property_APDU(&apdu[0], instance, - PROP_OBJECT_IDENTIFIER, - BACNET_ARRAY_ALL, &error_class, &error_code); + PROP_OBJECT_IDENTIFIER, BACNET_ARRAY_ALL, &error_class, &error_code); ct_test(pTest, len >= 0); len = decode_tag_number_and_value(&apdu[0], &tag_number, &len_value); ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_OBJECT_ID); @@ -214,7 +226,8 @@ void testBinaryInput(Test * pTest) } #ifdef TEST_BINARY_INPUT -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -231,5 +244,5 @@ int main(void) return 0; } -#endif /* TEST_BINARY_INPUT */ -#endif /* TEST */ +#endif /* TEST_BINARY_INPUT */ +#endif /* TEST */ diff --git a/bacnet-stack/ports/at91sam7s/blinker.c b/bacnet-stack/ports/at91sam7s/blinker.c index e6d8ddd3..42f29679 100644 --- a/bacnet-stack/ports/at91sam7s/blinker.c +++ b/bacnet-stack/ports/at91sam7s/blinker.c @@ -14,28 +14,30 @@ #include "board.h" /* global variables */ -unsigned long blinkcount; +unsigned long blinkcount; + +void blinker( + unsigned char code) +{ -void blinker(unsigned char code) { - volatile AT91PS_PIO pPIO = AT91C_BASE_PIOA; /* pointer to PIO register structure */ - volatile unsigned int j,k; /* loop counters */ + volatile unsigned int j, k; /* loop counters */ /* endless loop */ - while (1) { + while (1) { /* count out the proper number of blinks */ - for (j = code; j != 0; j--) { + for (j = code; j != 0; j--) { /* turn LED1 (DS1) on */ pPIO->PIO_CODR = LED1; /* wait 250 msec */ - for (k = 600000; k != 0; k-- ); + for (k = 600000; k != 0; k--); /* turn LED1 (DS1) off */ pPIO->PIO_SODR = LED1; /* wait 250 msec */ - for (k = 600000; k != 0; k-- ); + for (k = 600000; k != 0; k--); } /* wait 2 seconds */ - for (k = 5000000; (code != 0) && (k != 0); k-- ); + for (k = 5000000; (code != 0) && (k != 0); k--); blinkcount++; } } diff --git a/bacnet-stack/ports/at91sam7s/board.h b/bacnet-stack/ports/at91sam7s/board.h index 2b0bda8d..d99610dd 100644 --- a/bacnet-stack/ports/at91sam7s/board.h +++ b/bacnet-stack/ports/at91sam7s/board.h @@ -7,10 +7,10 @@ // fitness for any particular purpose, or against the infringements of // intellectual property rights of others. //---------------------------------------------------------------------------------------------------- -// File Name: Board.h -// Object: AT91SAM7S Evaluation Board Features Definition File. +// File Name: Board.h +// Object: AT91SAM7S Evaluation Board Features Definition File. // -// Creation: JPP 16/June/2004 +// Creation: JPP 16/June/2004 //---------------------------------------------------------------------------------------------------- #ifndef Board_h #define Board_h @@ -35,10 +35,10 @@ //------------------------ // Leds Definition //------------------------ -#define LED1 (1<<0) // PA0 -#define LED2 (1<<1) // PA1 -#define LED3 (1<<2) // PA2 -#define LED4 (1<<3) // PA3 +#define LED1 (1<<0) // PA0 +#define LED2 (1<<1) // PA1 +#define LED3 (1<<2) // PA2 +#define LED4 (1<<3) // PA3 #define NB_LEB 4 #define LED_MASK (LED1|LED2|LED3|LED4) @@ -46,34 +46,34 @@ //---------------------------------- // Push Buttons Definition //----------------------------------- -#define SW1_MASK (1<<19) // PA19 -#define SW2_MASK (1<<20) // PA20 -#define SW3_MASK (1<<15) // PA15 -#define SW4_MASK (1<<14) // PA14 +#define SW1_MASK (1<<19) // PA19 +#define SW2_MASK (1<<20) // PA20 +#define SW3_MASK (1<<15) // PA15 +#define SW4_MASK (1<<14) // PA14 #define SW_MASK (SW1_MASK|SW2_MASK|SW3_MASK|SW4_MASK) -#define SW1 (1<<19) // PA19 -#define SW2 (1<<20) // PA20 -#define SW3 (1<<15) // PA15 -#define SW4 (1<<14) // PA14 +#define SW1 (1<<19) // PA19 +#define SW2 (1<<20) // PA20 +#define SW3 (1<<15) // PA15 +#define SW4 (1<<14) // PA14 //------------------------- // USART Definition //------------------------- // SUB-D 9 points J3 DBGU -#define DBGU_RXD AT91C_PA9_DRXD // JP11 must be close -#define DBGU_TXD AT91C_PA10_DTXD // JP12 must be close -#define AT91C_DBGU_BAUD 115200 // Baud rate -#define US_RXD_PIN AT91C_PA5_RXD0 // JP9 must be close -#define US_TXD_PIN AT91C_PA6_TXD0 // JP7 must be close -#define US_RTS_PIN AT91C_PA7_RTS0 // JP8 must be close -#define US_CTS_PIN AT91C_PA8_CTS0 // JP6 must be close +#define DBGU_RXD AT91C_PA9_DRXD // JP11 must be close +#define DBGU_TXD AT91C_PA10_DTXD // JP12 must be close +#define AT91C_DBGU_BAUD 115200 // Baud rate +#define US_RXD_PIN AT91C_PA5_RXD0 // JP9 must be close +#define US_TXD_PIN AT91C_PA6_TXD0 // JP7 must be close +#define US_RTS_PIN AT91C_PA7_RTS0 // JP8 must be close +#define US_CTS_PIN AT91C_PA8_CTS0 // JP6 must be close //-------------- // Master Clock //-------------- -#define EXT_OC 18432000 // Exetrnal ocilator MAINCK -#define MCK 47923200 // MCK (PLLRC div by 2) -#define MCKKHz (MCK/1000) // +#define EXT_OC 18432000 // Exetrnal ocilator MAINCK +#define MCK 47923200 // MCK (PLLRC div by 2) +#define MCKKHz (MCK/1000) // -#endif // Board_h +#endif // Board_h diff --git a/bacnet-stack/ports/at91sam7s/bv.c b/bacnet-stack/ports/at91sam7s/bv.c index 85a03cd9..3cb3e061 100644 --- a/bacnet-stack/ports/at91sam7s/bv.c +++ b/bacnet-stack/ports/at91sam7s/bv.c @@ -31,31 +31,33 @@ #include "bacdef.h" #include "bacdcode.h" #include "bacenum.h" -#include "config.h" /* the custom stuff */ +#include "config.h" /* the custom stuff */ #include "wp.h" #define MAX_BINARY_VALUES 8 #if (MAX_BINARY_VALUES > 9) - #error Modify the Binary_Value_Name to handle multiple digits +#error Modify the Binary_Value_Name to handle multiple digits #endif static BACNET_BINARY_PV Present_Value[MAX_BINARY_VALUES]; -static void Binary_Value_Initialize(void) +static void Binary_Value_Initialize( + void) { - static bool initialized = false; - unsigned i; - - if (!initialized) { - initialized = true; - for (i = 0; i < MAX_BINARY_VALUES; i++) { - Present_Value[i] = BINARY_INACTIVE; + static bool initialized = false; + unsigned i; + + if (!initialized) { + initialized = true; + for (i = 0; i < MAX_BINARY_VALUES; i++) { + Present_Value[i] = BINARY_INACTIVE; + } } - } } /* we simply have 0-n object instances. */ -bool Binary_Value_Valid_Instance(uint32_t object_instance) +bool Binary_Value_Valid_Instance( + uint32_t object_instance) { if (object_instance < MAX_BINARY_VALUES) return true; @@ -64,19 +66,22 @@ bool Binary_Value_Valid_Instance(uint32_t object_instance) } /* we simply have 0-n object instances. */ -unsigned Binary_Value_Count(void) +unsigned Binary_Value_Count( + void) { return MAX_BINARY_VALUES; } /* we simply have 0-n object instances. */ -uint32_t Binary_Value_Index_To_Instance(unsigned index) +uint32_t Binary_Value_Index_To_Instance( + unsigned index) { return index; } /* we simply have 0-n object instances. */ -unsigned Binary_Value_Instance_To_Index(uint32_t object_instance) +unsigned Binary_Value_Instance_To_Index( + uint32_t object_instance) { unsigned index = MAX_BINARY_VALUES; @@ -86,8 +91,8 @@ unsigned Binary_Value_Instance_To_Index(uint32_t object_instance) return index; } -static BACNET_BINARY_PV Binary_Value_Present_Value(uint32_t - object_instance) +static BACNET_BINARY_PV Binary_Value_Present_Value( + uint32_t object_instance) { BACNET_BINARY_PV value = BINARY_INACTIVE; @@ -100,12 +105,13 @@ static BACNET_BINARY_PV Binary_Value_Present_Value(uint32_t } /* note: the object name must be unique within this device */ -char *Binary_Value_Name(uint32_t object_instance) +char *Binary_Value_Name( + uint32_t object_instance) { - static char text_string[16] = "BV-0"; /* okay for single thread */ + static char text_string[16] = "BV-0"; /* okay for single thread */ if (object_instance < MAX_BINARY_VALUES) { - text_string[3] = '0' + (uint8_t)object_instance; + text_string[3] = '0' + (uint8_t) object_instance; return text_string; } @@ -113,13 +119,15 @@ char *Binary_Value_Name(uint32_t object_instance) } /* return apdu len, or -1 on error */ -int Binary_Value_Encode_Property_APDU(uint8_t * apdu, +int Binary_Value_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ BACNET_BIT_STRING bit_string; BACNET_CHARACTER_STRING char_string; BACNET_BINARY_PV present_value = BINARY_INACTIVE; @@ -127,58 +135,64 @@ int Binary_Value_Encode_Property_APDU(uint8_t * apdu, Binary_Value_Initialize(); switch (property) { - case PROP_OBJECT_IDENTIFIER: - apdu_len = encode_application_object_id(&apdu[0], OBJECT_BINARY_VALUE, - object_instance); - break; - /* note: Name and Description don't have to be the same. - You could make Description writable and different */ - case PROP_OBJECT_NAME: - case PROP_DESCRIPTION: - characterstring_init_ansi(&char_string, - Binary_Value_Name(object_instance)); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_OBJECT_TYPE: - apdu_len = encode_application_enumerated(&apdu[0], OBJECT_BINARY_VALUE); - break; - case PROP_PRESENT_VALUE: - present_value = Binary_Value_Present_Value(object_instance); - apdu_len = encode_application_enumerated(&apdu[0], present_value); - break; - case PROP_STATUS_FLAGS: - /* note: see the details in the standard on how to use these */ - bitstring_init(&bit_string); - bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); - apdu_len = encode_application_bitstring(&apdu[0], &bit_string); - break; - case PROP_EVENT_STATE: - /* note: see the details in the standard on how to use this */ - apdu_len = encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); - break; - case PROP_OUT_OF_SERVICE: - apdu_len = encode_application_boolean(&apdu[0], false); - break; - case PROP_POLARITY: - /* FIXME: figure out the polarity */ - apdu_len = encode_application_enumerated(&apdu[0], polarity); - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_UNKNOWN_PROPERTY; - apdu_len = -1; - break; + case PROP_OBJECT_IDENTIFIER: + apdu_len = + encode_application_object_id(&apdu[0], OBJECT_BINARY_VALUE, + object_instance); + break; + /* note: Name and Description don't have to be the same. + You could make Description writable and different */ + case PROP_OBJECT_NAME: + case PROP_DESCRIPTION: + characterstring_init_ansi(&char_string, + Binary_Value_Name(object_instance)); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_OBJECT_TYPE: + apdu_len = + encode_application_enumerated(&apdu[0], OBJECT_BINARY_VALUE); + break; + case PROP_PRESENT_VALUE: + present_value = Binary_Value_Present_Value(object_instance); + apdu_len = encode_application_enumerated(&apdu[0], present_value); + break; + case PROP_STATUS_FLAGS: + /* note: see the details in the standard on how to use these */ + bitstring_init(&bit_string); + bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); + apdu_len = encode_application_bitstring(&apdu[0], &bit_string); + break; + case PROP_EVENT_STATE: + /* note: see the details in the standard on how to use this */ + apdu_len = + encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); + break; + case PROP_OUT_OF_SERVICE: + apdu_len = encode_application_boolean(&apdu[0], false); + break; + case PROP_POLARITY: + /* FIXME: figure out the polarity */ + apdu_len = encode_application_enumerated(&apdu[0], polarity); + break; + default: + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_UNKNOWN_PROPERTY; + apdu_len = -1; + break; } return apdu_len; } /* returns true if successful */ -bool Binary_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) +bool Binary_Value_Write_Property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { bool status = false; /* return value */ unsigned int object_index = 0; @@ -198,85 +212,85 @@ bool Binary_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, /* FIXME: len < application_data_len: more data? */ /* FIXME: len == 0: unable to decode? */ switch (wp_data->object_property) { - case PROP_PRESENT_VALUE: - if (value.tag == BACNET_APPLICATION_TAG_ENUMERATED) { - priority = wp_data->priority; - /* Command priority 6 is reserved for use by Minimum On/Off - algorithm and may not be used for other purposes in any - object. */ - if (priority && (priority <= BACNET_MAX_PRIORITY) && - (priority != 6 /* reserved */ ) && - (value.type.Enumerated >= MIN_BINARY_PV) && - (value.type.Enumerated <= MAX_BINARY_PV)) { - level = value.type.Enumerated; - object_index = - Binary_Value_Instance_To_Index(wp_data-> - object_instance); - priority--; - /* NOTE: this Binary value has no priority array */ - Present_Value[object_index] = level; - /* Note: you could set the physical output here if we - are the highest priority. - However, if Out of Service is TRUE, then don't set the - physical output. */ - status = true; - } else if (priority == 6) { + case PROP_PRESENT_VALUE: + if (value.tag == BACNET_APPLICATION_TAG_ENUMERATED) { + priority = wp_data->priority; /* Command priority 6 is reserved for use by Minimum On/Off algorithm and may not be used for other purposes in any object. */ + if (priority && (priority <= BACNET_MAX_PRIORITY) && + (priority != 6 /* reserved */ ) && + (value.type.Enumerated >= MIN_BINARY_PV) && + (value.type.Enumerated <= MAX_BINARY_PV)) { + level = value.type.Enumerated; + object_index = + Binary_Value_Instance_To_Index(wp_data-> + object_instance); + priority--; + /* NOTE: this Binary value has no priority array */ + Present_Value[object_index] = level; + /* Note: you could set the physical output here if we + are the highest priority. + However, if Out of Service is TRUE, then don't set the + physical output. */ + status = true; + } else if (priority == 6) { + /* Command priority 6 is reserved for use by Minimum On/Off + algorithm and may not be used for other purposes in any + object. */ + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } + } else if (value.tag == BACNET_APPLICATION_TAG_NULL) { +#if 0 + /* NOTE: this Binary Value has no priority array */ + level = BINARY_NULL; + object_index = + Binary_Value_Instance_To_Index(wp_data->object_instance); + priority = wp_data->priority; + if (priority && (priority <= BACNET_MAX_PRIORITY)) { + priority--; + Binary_Value_Level[object_index][priority] = level; + /* Note: you could set the physical output here to the next + highest priority, or to the relinquish default if no + priorities are set. + However, if Out of Service is TRUE, then don't set the + physical output. This comment may apply to the + main loop (i.e. check out of service before changing output) */ + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } +#else *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; +#endif } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } - } else if (value.tag == BACNET_APPLICATION_TAG_NULL) { + break; #if 0 - /* NOTE: this Binary Value has no priority array */ - level = BINARY_NULL; - object_index = - Binary_Value_Instance_To_Index(wp_data->object_instance); - priority = wp_data->priority; - if (priority && (priority <= BACNET_MAX_PRIORITY)) { - priority--; - Binary_Value_Level[object_index][priority] = level; - /* Note: you could set the physical output here to the next - highest priority, or to the relinquish default if no - priorities are set. - However, if Out of Service is TRUE, then don't set the - physical output. This comment may apply to the - main loop (i.e. check out of service before changing output) */ + case PROP_OUT_OF_SERVICE: + if (value.tag == BACNET_APPLICATION_TAG_BOOLEAN) { + object_index = + Binary_Value_Instance_To_Index(wp_data->object_instance); + Binary_Value_Out_Of_Service[object_index] = value.type.Boolean; status = true; } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } -#else - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; + break; #endif - } else { + default: *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; -#if 0 - case PROP_OUT_OF_SERVICE: - if (value.tag == BACNET_APPLICATION_TAG_BOOLEAN) { - object_index = - Binary_Value_Instance_To_Index(wp_data->object_instance); - Binary_Value_Out_Of_Service[object_index] = value.type.Boolean; - status = true; - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; -#endif - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; - break; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + break; } return status; @@ -287,7 +301,8 @@ bool Binary_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, #include #include "ctest.h" -void testBinary_Value(Test * pTest) +void testBinary_Value( + Test * pTest) { uint8_t apdu[MAX_APDU] = { 0 }; int len = 0; @@ -302,8 +317,7 @@ void testBinary_Value(Test * pTest) len = Binary_Value_Encode_Property_APDU(&apdu[0], instance, - PROP_OBJECT_IDENTIFIER, - BACNET_ARRAY_ALL, &error_class, &error_code); + PROP_OBJECT_IDENTIFIER, BACNET_ARRAY_ALL, &error_class, &error_code); ct_test(pTest, len != 0); len = decode_tag_number_and_value(&apdu[0], &tag_number, &len_value); ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_OBJECT_ID); @@ -316,7 +330,8 @@ void testBinary_Value(Test * pTest) } #ifdef TEST_BINARY_VALUE -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -333,5 +348,5 @@ int main(void) return 0; } -#endif /* TEST_BINARY_VALUE */ -#endif /* TEST */ +#endif /* TEST_BINARY_VALUE */ +#endif /* TEST */ diff --git a/bacnet-stack/ports/at91sam7s/device.c b/bacnet-stack/ports/at91sam7s/device.c index 10abfe3b..317216d6 100644 --- a/bacnet-stack/ports/at91sam7s/device.c +++ b/bacnet-stack/ports/at91sam7s/device.c @@ -53,13 +53,15 @@ static BACNET_DEVICE_STATUS System_Status = STATUS_OPERATIONAL; BACNET_REINITIALIZED_STATE_OF_DEVICE Reinitialize_State = REINITIALIZED_STATE_IDLE; -void Device_Reinit(void) +void Device_Reinit( + void) { dcc_set_status_duration(COMMUNICATION_ENABLE, 0); Device_Set_Object_Instance_Number(BACNET_MAX_INSTANCE); } -void Device_Init(void) +void Device_Init( + void) { Reinitialize_State = REINITIALIZED_STATE_IDLE; dcc_set_status_duration(COMMUNICATION_ENABLE, 0); @@ -71,14 +73,16 @@ void Device_Init(void) } /* methods to manipulate the data */ -uint32_t Device_Object_Instance_Number(void) +uint32_t Device_Object_Instance_Number( + void) { return Object_Instance_Number; } -bool Device_Set_Object_Instance_Number(uint32_t object_id) +bool Device_Set_Object_Instance_Number( + uint32_t object_id) { - bool status = true; /* return value */ + bool status = true; /* return value */ if (object_id <= BACNET_MAX_INSTANCE) { Object_Instance_Number = object_id; @@ -94,69 +98,81 @@ bool Device_Set_Object_Instance_Number(uint32_t object_id) return status; } -bool Device_Valid_Object_Instance_Number(uint32_t object_id) +bool Device_Valid_Object_Instance_Number( + uint32_t object_id) { /* BACnet allows for a wildcard instance number */ return ((Object_Instance_Number == object_id) || (object_id == BACNET_MAX_INSTANCE)); } -BACNET_DEVICE_STATUS Device_System_Status(void) +BACNET_DEVICE_STATUS Device_System_Status( + void) { return System_Status; } -void Device_Set_System_Status(BACNET_DEVICE_STATUS status) +void Device_Set_System_Status( + BACNET_DEVICE_STATUS status) { if (status < MAX_DEVICE_STATUS) System_Status = status; } -uint16_t Device_Vendor_Identifier(void) +uint16_t Device_Vendor_Identifier( + void) { return BACNET_VENDOR_ID; } -uint8_t Device_Protocol_Version(void) +uint8_t Device_Protocol_Version( + void) { return 1; } -uint8_t Device_Protocol_Revision(void) +uint8_t Device_Protocol_Revision( + void) { return 5; } /* FIXME: MAX_APDU is defined in config.ini - set it! */ -uint16_t Device_Max_APDU_Length_Accepted(void) +uint16_t Device_Max_APDU_Length_Accepted( + void) { return MAX_APDU; } -BACNET_SEGMENTATION Device_Segmentation_Supported(void) +BACNET_SEGMENTATION Device_Segmentation_Supported( + void) { return SEGMENTATION_NONE; } -uint16_t Device_APDU_Timeout(void) +uint16_t Device_APDU_Timeout( + void) { return 60000; } -uint8_t Device_Number_Of_APDU_Retries(void) +uint8_t Device_Number_Of_APDU_Retries( + void) { return 0; } -uint8_t Device_Database_Revision(void) +uint8_t Device_Database_Revision( + void) { return 0; } -unsigned Device_Object_List_Count(void) +unsigned Device_Object_List_Count( + void) { - unsigned count = 1; /* at least 1 for device object */ + unsigned count = 1; /* at least 1 for device object */ /* FIXME: add objects as needed */ count += Binary_Input_Count(); @@ -167,8 +183,10 @@ unsigned Device_Object_List_Count(void) return count; } -bool Device_Object_List_Identifier(unsigned array_index, - int *object_type, uint32_t * instance) +bool Device_Object_List_Identifier( + unsigned array_index, + int *object_type, + uint32_t * instance) { bool status = false; unsigned object_index = 0; @@ -238,13 +256,15 @@ bool Device_Object_List_Identifier(unsigned array_index, } /* return the length of the apdu encoded or -1 for error */ -int Device_Encode_Property_APDU(uint8_t * apdu, +int Device_Encode_Property_APDU( + uint8_t * apdu, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { - int apdu_len = 0; /* return value */ - int len = 0; /* apdu len intermediate value */ + int apdu_len = 0; /* return value */ + int len = 0; /* apdu len intermediate value */ BACNET_BIT_STRING bit_string; BACNET_CHARACTER_STRING char_string; unsigned i = 0; @@ -256,203 +276,221 @@ int Device_Encode_Property_APDU(uint8_t * apdu, /* FIXME: change the hardcoded names to suit your application */ switch (property) { - case PROP_OBJECT_IDENTIFIER: - apdu_len = encode_application_object_id(&apdu[0], OBJECT_DEVICE, - Object_Instance_Number); - break; - case PROP_OBJECT_NAME: - characterstring_init_ansi(&char_string, Object_Name); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_OBJECT_TYPE: - apdu_len = encode_application_enumerated(&apdu[0], OBJECT_DEVICE); - break; - case PROP_DESCRIPTION: - characterstring_init_ansi(&char_string, "BACnet Demo"); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_SYSTEM_STATUS: - apdu_len = - encode_application_enumerated(&apdu[0], Device_System_Status()); - break; - case PROP_VENDOR_NAME: - characterstring_init_ansi(&char_string, BACNET_VENDOR_NAME); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_VENDOR_IDENTIFIER: - apdu_len = - encode_application_unsigned(&apdu[0], Device_Vendor_Identifier()); - break; - case PROP_MODEL_NAME: - characterstring_init_ansi(&char_string, "GNU Demo"); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_FIRMWARE_REVISION: - characterstring_init_ansi(&char_string, BACnet_Version); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_APPLICATION_SOFTWARE_VERSION: - characterstring_init_ansi(&char_string, "1.0"); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_LOCATION: - characterstring_init_ansi(&char_string, "USA"); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_PROTOCOL_VERSION: - apdu_len = - encode_application_unsigned(&apdu[0], Device_Protocol_Version()); - break; - case PROP_PROTOCOL_REVISION: - apdu_len = - encode_application_unsigned(&apdu[0], Device_Protocol_Revision()); - break; - /* BACnet Legacy Support */ - case PROP_PROTOCOL_CONFORMANCE_CLASS: - apdu_len = encode_application_unsigned(&apdu[0], 1); - break; - case PROP_PROTOCOL_SERVICES_SUPPORTED: - /* Note: list of services that are executed, not initiated. */ - bitstring_init(&bit_string); - for (i = 0; i < MAX_BACNET_SERVICES_SUPPORTED; i++) { - /* automatic lookup based on handlers set */ - bitstring_set_bit(&bit_string, (uint8_t) i, - apdu_service_supported(i)); - } - apdu_len = encode_application_bitstring(&apdu[0], &bit_string); - break; - case PROP_PROTOCOL_OBJECT_TYPES_SUPPORTED: - /* Note: this is the list of objects that can be in this device, - not a list of objects that this device can access */ - bitstring_init(&bit_string); - for (i = 0; i < MAX_ASHRAE_OBJECT_TYPE; i++) { - /* FIXME: if ReadProperty used an array of Functions... */ - /* initialize all the object types to not-supported */ - bitstring_set_bit(&bit_string, (uint8_t) i, false); - } - /* FIXME: indicate the objects that YOU support */ - bitstring_set_bit(&bit_string, OBJECT_DEVICE, true); - bitstring_set_bit(&bit_string, OBJECT_ANALOG_VALUE, true); - bitstring_set_bit(&bit_string, OBJECT_BINARY_VALUE, true); - bitstring_set_bit(&bit_string, OBJECT_ANALOG_INPUT, true); - bitstring_set_bit(&bit_string, OBJECT_BINARY_INPUT, true); - apdu_len = encode_application_bitstring(&apdu[0], &bit_string); - break; - case PROP_OBJECT_LIST: - count = Device_Object_List_Count(); - /* Array element zero is the number of objects in the list */ - if (array_index == 0) - apdu_len = encode_application_unsigned(&apdu[0], count); - /* if no index was specified, then try to encode the entire list */ - /* into one packet. Note that more than likely you will have */ - /* to return an error if the number of encoded objects exceeds */ - /* your maximum APDU size. */ - else if (array_index == BACNET_ARRAY_ALL) { - for (i = 1; i <= count; i++) { - if (Device_Object_List_Identifier(i, &object_type, - &instance)) { - len = - encode_application_object_id(&apdu[apdu_len], - object_type, instance); - apdu_len += len; - /* assume next one is the same size as this one */ - /* can we all fit into the APDU? */ - if ((apdu_len + len) >= MAX_APDU) { + case PROP_OBJECT_IDENTIFIER: + apdu_len = encode_application_object_id(&apdu[0], OBJECT_DEVICE, + Object_Instance_Number); + break; + case PROP_OBJECT_NAME: + characterstring_init_ansi(&char_string, Object_Name); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_OBJECT_TYPE: + apdu_len = encode_application_enumerated(&apdu[0], OBJECT_DEVICE); + break; + case PROP_DESCRIPTION: + characterstring_init_ansi(&char_string, "BACnet Demo"); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_SYSTEM_STATUS: + apdu_len = + encode_application_enumerated(&apdu[0], + Device_System_Status()); + break; + case PROP_VENDOR_NAME: + characterstring_init_ansi(&char_string, BACNET_VENDOR_NAME); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_VENDOR_IDENTIFIER: + apdu_len = + encode_application_unsigned(&apdu[0], + Device_Vendor_Identifier()); + break; + case PROP_MODEL_NAME: + characterstring_init_ansi(&char_string, "GNU Demo"); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_FIRMWARE_REVISION: + characterstring_init_ansi(&char_string, BACnet_Version); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_APPLICATION_SOFTWARE_VERSION: + characterstring_init_ansi(&char_string, "1.0"); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_LOCATION: + characterstring_init_ansi(&char_string, "USA"); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_PROTOCOL_VERSION: + apdu_len = + encode_application_unsigned(&apdu[0], + Device_Protocol_Version()); + break; + case PROP_PROTOCOL_REVISION: + apdu_len = + encode_application_unsigned(&apdu[0], + Device_Protocol_Revision()); + break; + /* BACnet Legacy Support */ + case PROP_PROTOCOL_CONFORMANCE_CLASS: + apdu_len = encode_application_unsigned(&apdu[0], 1); + break; + case PROP_PROTOCOL_SERVICES_SUPPORTED: + /* Note: list of services that are executed, not initiated. */ + bitstring_init(&bit_string); + for (i = 0; i < MAX_BACNET_SERVICES_SUPPORTED; i++) { + /* automatic lookup based on handlers set */ + bitstring_set_bit(&bit_string, (uint8_t) i, + apdu_service_supported(i)); + } + apdu_len = encode_application_bitstring(&apdu[0], &bit_string); + break; + case PROP_PROTOCOL_OBJECT_TYPES_SUPPORTED: + /* Note: this is the list of objects that can be in this device, + not a list of objects that this device can access */ + bitstring_init(&bit_string); + for (i = 0; i < MAX_ASHRAE_OBJECT_TYPE; i++) { + /* FIXME: if ReadProperty used an array of Functions... */ + /* initialize all the object types to not-supported */ + bitstring_set_bit(&bit_string, (uint8_t) i, false); + } + /* FIXME: indicate the objects that YOU support */ + bitstring_set_bit(&bit_string, OBJECT_DEVICE, true); + bitstring_set_bit(&bit_string, OBJECT_ANALOG_VALUE, true); + bitstring_set_bit(&bit_string, OBJECT_BINARY_VALUE, true); + bitstring_set_bit(&bit_string, OBJECT_ANALOG_INPUT, true); + bitstring_set_bit(&bit_string, OBJECT_BINARY_INPUT, true); + apdu_len = encode_application_bitstring(&apdu[0], &bit_string); + break; + case PROP_OBJECT_LIST: + count = Device_Object_List_Count(); + /* Array element zero is the number of objects in the list */ + if (array_index == 0) + apdu_len = encode_application_unsigned(&apdu[0], count); + /* if no index was specified, then try to encode the entire list */ + /* into one packet. Note that more than likely you will have */ + /* to return an error if the number of encoded objects exceeds */ + /* your maximum APDU size. */ + else if (array_index == BACNET_ARRAY_ALL) { + for (i = 1; i <= count; i++) { + if (Device_Object_List_Identifier(i, &object_type, + &instance)) { + len = + encode_application_object_id(&apdu[apdu_len], + object_type, instance); + apdu_len += len; + /* assume next one is the same size as this one */ + /* can we all fit into the APDU? */ + if ((apdu_len + len) >= MAX_APDU) { + *error_class = ERROR_CLASS_SERVICES; + *error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT; + apdu_len = -1; + break; + } + } else { + /* error: internal error? */ *error_class = ERROR_CLASS_SERVICES; - *error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT; + *error_code = ERROR_CODE_OTHER; apdu_len = -1; break; } - } else { - /* error: internal error? */ - *error_class = ERROR_CLASS_SERVICES; - *error_code = ERROR_CODE_OTHER; + } + } else { + if (Device_Object_List_Identifier(array_index, &object_type, + &instance)) + apdu_len = + encode_application_object_id(&apdu[0], object_type, + instance); + else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_ARRAY_INDEX; apdu_len = -1; - break; } } - } else { - if (Device_Object_List_Identifier(array_index, &object_type, - &instance)) - apdu_len = - encode_application_object_id(&apdu[0], object_type, - instance); - else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_ARRAY_INDEX; - apdu_len = -1; - } - } - break; - case PROP_MAX_APDU_LENGTH_ACCEPTED: - apdu_len = encode_application_unsigned(&apdu[0], - Device_Max_APDU_Length_Accepted()); - break; - case PROP_SEGMENTATION_SUPPORTED: - apdu_len = encode_application_enumerated(&apdu[0], - Device_Segmentation_Supported()); - break; - case PROP_APDU_TIMEOUT: - apdu_len = encode_application_unsigned(&apdu[0], Device_APDU_Timeout()); - break; - case PROP_NUMBER_OF_APDU_RETRIES: - apdu_len = - encode_application_unsigned(&apdu[0], - Device_Number_Of_APDU_Retries()); - break; - case PROP_DEVICE_ADDRESS_BINDING: - /* FIXME: encode the list here, if it exists */ - break; - case PROP_DATABASE_REVISION: - apdu_len = - encode_application_unsigned(&apdu[0], Device_Database_Revision()); - break; - case PROP_MAX_INFO_FRAMES: - apdu_len = - encode_application_unsigned(&apdu[0], dlmstp_max_info_frames()); - break; - case PROP_MAX_MASTER: - apdu_len = encode_application_unsigned(&apdu[0], dlmstp_max_master()); - break; - case PROP_LOCAL_TIME: - /* FIXME: if you support time */ - local_time.hour = 0; - local_time.min = 0; - local_time.sec = 0; - local_time.hundredths = 0; - apdu_len = encode_application_time(&apdu[0], &local_time); - break; - case PROP_UTC_OFFSET: - /* Note: BACnet Time Zone is inverse of everybody else */ - apdu_len = encode_application_signed(&apdu[0], 5 /* EST */ ); - break; - case PROP_LOCAL_DATE: - /* FIXME: if you support date */ - local_date.year = 2006; /* AD */ - local_date.month = 4; /* Jan=1..Dec=12 */ - local_date.day = 11; /* 1..31 */ - local_date.wday = 0; /* 1=Mon..7=Sun */ - apdu_len = encode_application_date(&apdu[0], &local_date); - break; - case PROP_DAYLIGHT_SAVINGS_STATUS: - /* FIXME: if you support time/date */ - apdu_len = encode_application_boolean(&apdu[0], false); - break; - case 9600: - apdu_len = encode_application_unsigned(&apdu[0], RS485_Get_Baud_Rate()); - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_UNKNOWN_PROPERTY; - apdu_len = -1; - break; + break; + case PROP_MAX_APDU_LENGTH_ACCEPTED: + apdu_len = encode_application_unsigned(&apdu[0], + Device_Max_APDU_Length_Accepted()); + break; + case PROP_SEGMENTATION_SUPPORTED: + apdu_len = encode_application_enumerated(&apdu[0], + Device_Segmentation_Supported()); + break; + case PROP_APDU_TIMEOUT: + apdu_len = + encode_application_unsigned(&apdu[0], Device_APDU_Timeout()); + break; + case PROP_NUMBER_OF_APDU_RETRIES: + apdu_len = + encode_application_unsigned(&apdu[0], + Device_Number_Of_APDU_Retries()); + break; + case PROP_DEVICE_ADDRESS_BINDING: + /* FIXME: encode the list here, if it exists */ + break; + case PROP_DATABASE_REVISION: + apdu_len = + encode_application_unsigned(&apdu[0], + Device_Database_Revision()); + break; + case PROP_MAX_INFO_FRAMES: + apdu_len = + encode_application_unsigned(&apdu[0], + dlmstp_max_info_frames()); + break; + case PROP_MAX_MASTER: + apdu_len = + encode_application_unsigned(&apdu[0], dlmstp_max_master()); + break; + case PROP_LOCAL_TIME: + /* FIXME: if you support time */ + local_time.hour = 0; + local_time.min = 0; + local_time.sec = 0; + local_time.hundredths = 0; + apdu_len = encode_application_time(&apdu[0], &local_time); + break; + case PROP_UTC_OFFSET: + /* Note: BACnet Time Zone is inverse of everybody else */ + apdu_len = encode_application_signed(&apdu[0], 5 /* EST */ ); + break; + case PROP_LOCAL_DATE: + /* FIXME: if you support date */ + local_date.year = 2006; /* AD */ + local_date.month = 4; /* Jan=1..Dec=12 */ + local_date.day = 11; /* 1..31 */ + local_date.wday = 0; /* 1=Mon..7=Sun */ + apdu_len = encode_application_date(&apdu[0], &local_date); + break; + case PROP_DAYLIGHT_SAVINGS_STATUS: + /* FIXME: if you support time/date */ + apdu_len = encode_application_boolean(&apdu[0], false); + break; + case 9600: + apdu_len = + encode_application_unsigned(&apdu[0], RS485_Get_Baud_Rate()); + break; + default: + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_UNKNOWN_PROPERTY; + apdu_len = -1; + break; } return apdu_len; } -bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) +bool Device_Write_Property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { bool status = false; /* return value */ int len = 0; @@ -469,98 +507,98 @@ bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, /* FIXME: len < application_data_len: more data? */ /* FIXME: len == 0: unable to decode? */ switch (wp_data->object_property) { - case PROP_OBJECT_IDENTIFIER: - if (value.tag == BACNET_APPLICATION_TAG_OBJECT_ID) { - if ((value.type.Object_Id.type == OBJECT_DEVICE) && - (Device_Set_Object_Instance_Number(value.type. - Object_Id.instance))) { - /* we could send an I-Am broadcast to let the world know */ - status = true; - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; - } - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case PROP_MAX_INFO_FRAMES: - if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { - if (value.type.Unsigned_Int <= 255) { - dlmstp_set_max_info_frames(value.type.Unsigned_Int); - status = true; - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; - } - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case PROP_MAX_MASTER: - if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { - if ((value.type.Unsigned_Int > 0) && - (value.type.Unsigned_Int <= 127)) { - dlmstp_set_max_master(value.type.Unsigned_Int); - status = true; - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; - } - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case PROP_OBJECT_NAME: - if (value.tag == BACNET_APPLICATION_TAG_CHARACTER_STRING) { - uint8_t encoding; - size_t len; - - encoding = - characterstring_encoding(&value.type.Character_String); - len = characterstring_length(&value.type.Character_String); - if (encoding == CHARACTER_ANSI_X34) { - if (len <= 20) { - /* FIXME: set the name */ - /* Display_Set_Name( - characterstring_value(&value.type.Character_String)); */ - /* FIXME: All the object names in a device must be unique. - Disallow setting the Device Object Name to any objects in - the device. */ + case PROP_OBJECT_IDENTIFIER: + if (value.tag == BACNET_APPLICATION_TAG_OBJECT_ID) { + if ((value.type.Object_Id.type == OBJECT_DEVICE) && + (Device_Set_Object_Instance_Number(value.type. + Object_Id.instance))) { + /* we could send an I-Am broadcast to let the world know */ + status = true; } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_NO_SPACE_TO_WRITE_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; } } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_CHARACTER_SET_NOT_SUPPORTED; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case 9600: - if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { - if (value.type.Unsigned_Int > 115200) { - RS485_Set_Baud_Rate(value.type.Unsigned_Int); - status = true; + break; + case PROP_MAX_INFO_FRAMES: + if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { + if (value.type.Unsigned_Int <= 255) { + dlmstp_set_max_info_frames(value.type.Unsigned_Int); + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } - } else { + break; + case PROP_MAX_MASTER: + if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { + if ((value.type.Unsigned_Int > 0) && + (value.type.Unsigned_Int <= 127)) { + dlmstp_set_max_master(value.type.Unsigned_Int); + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; + } + break; + case PROP_OBJECT_NAME: + if (value.tag == BACNET_APPLICATION_TAG_CHARACTER_STRING) { + uint8_t encoding; + size_t len; + + encoding = + characterstring_encoding(&value.type.Character_String); + len = characterstring_length(&value.type.Character_String); + if (encoding == CHARACTER_ANSI_X34) { + if (len <= 20) { + /* FIXME: set the name */ + /* Display_Set_Name( + characterstring_value(&value.type.Character_String)); */ + /* FIXME: All the object names in a device must be unique. + Disallow setting the Device Object Name to any objects in + the device. */ + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_NO_SPACE_TO_WRITE_PROPERTY; + } + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_CHARACTER_SET_NOT_SUPPORTED; + } + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; + } + break; + case 9600: + if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { + if (value.type.Unsigned_Int > 115200) { + RS485_Set_Baud_Rate(value.type.Unsigned_Int); + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; + } + break; + default: *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; - break; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + break; } return status; diff --git a/bacnet-stack/ports/at91sam7s/dlmstp.c b/bacnet-stack/ports/at91sam7s/dlmstp.c index d0078eb6..e6832754 100644 --- a/bacnet-stack/ports/at91sam7s/dlmstp.c +++ b/bacnet-stack/ports/at91sam7s/dlmstp.c @@ -237,21 +237,25 @@ static uint8_t TransmitPacketDest; /* we need to be able to increment without rolling over */ #define INCREMENT_AND_LIMIT_UINT8(x) {if (x < 0xFF) x++;} -bool dlmstp_init(char *ifname) +bool dlmstp_init( + char *ifname) { - (void)ifname; + (void) ifname; /* initialize hardware */ RS485_Initialize(); - + return true; } -void dlmstp_cleanup(void) +void dlmstp_cleanup( + void) { /* nothing to do for static buffers */ } -void dlmstp_fill_bacnet_address(BACNET_ADDRESS * src, uint8_t mstp_address) +void dlmstp_fill_bacnet_address( + BACNET_ADDRESS * src, + uint8_t mstp_address) { int i = 0; @@ -275,10 +279,10 @@ void dlmstp_fill_bacnet_address(BACNET_ADDRESS * src, uint8_t mstp_address) } static bool dlmstp_compare_data_expecting_reply( - uint8_t *request_pdu, + uint8_t * request_pdu, uint16_t request_pdu_len, uint8_t src_address, - uint8_t *reply_pdu, + uint8_t * reply_pdu, uint16_t reply_pdu_len, uint8_t dest_address) { @@ -299,7 +303,7 @@ static bool dlmstp_compare_data_expecting_reply( /* decode the request data */ request.address.mac[0] = src_address; request.address.mac_len = 1; - offset = npdu_decode(&request_pdu[0], + offset = npdu_decode(&request_pdu[0], NULL, &request.address, &request.npdu_data); if (request.npdu_data.network_layer_message) { return false; @@ -308,16 +312,16 @@ static bool dlmstp_compare_data_expecting_reply( if (request.pdu_type != PDU_TYPE_CONFIRMED_SERVICE_REQUEST) { return false; } - request.invoke_id = request_pdu[offset+2]; + request.invoke_id = request_pdu[offset + 2]; /* segmented message? */ if (request_pdu[offset] & BIT3) - request.service_choice = request_pdu[offset+5]; + request.service_choice = request_pdu[offset + 5]; else - request.service_choice = request_pdu[offset+3]; + request.service_choice = request_pdu[offset + 3]; /* decode the reply data */ reply.address.mac[0] = dest_address; reply.address.mac_len = 1; - offset = npdu_decode(&reply_pdu[0], + offset = npdu_decode(&reply_pdu[0], &reply.address, NULL, &reply.npdu_data); if (reply.npdu_data.network_layer_message) { return false; @@ -327,32 +331,32 @@ static bool dlmstp_compare_data_expecting_reply( reply.pdu_type = reply_pdu[offset] & 0xF0; switch (reply.pdu_type) { case PDU_TYPE_CONFIRMED_SERVICE_REQUEST: - reply.invoke_id = reply_pdu[offset+2]; + reply.invoke_id = reply_pdu[offset + 2]; /* segmented message? */ if (reply_pdu[offset] & BIT3) - reply.service_choice = reply_pdu[offset+5]; + reply.service_choice = reply_pdu[offset + 5]; else - reply.service_choice = reply_pdu[offset+3]; + reply.service_choice = reply_pdu[offset + 3]; break; case PDU_TYPE_SIMPLE_ACK: - reply.invoke_id = reply_pdu[offset+1]; - reply.service_choice = reply_pdu[offset+2]; + reply.invoke_id = reply_pdu[offset + 1]; + reply.service_choice = reply_pdu[offset + 2]; break; case PDU_TYPE_COMPLEX_ACK: - reply.invoke_id = reply_pdu[offset+1]; + reply.invoke_id = reply_pdu[offset + 1]; /* segmented message? */ if (reply_pdu[offset] & BIT3) - reply.service_choice = reply_pdu[offset+4]; + reply.service_choice = reply_pdu[offset + 4]; else - reply.service_choice = reply_pdu[offset+2]; + reply.service_choice = reply_pdu[offset + 2]; break; case PDU_TYPE_ERROR: - reply.invoke_id = reply_pdu[offset+1]; - reply.service_choice = reply_pdu[offset+2]; + reply.invoke_id = reply_pdu[offset + 1]; + reply.service_choice = reply_pdu[offset + 2]; break; case PDU_TYPE_REJECT: case PDU_TYPE_ABORT: - reply.invoke_id = reply_pdu[offset+1]; + reply.invoke_id = reply_pdu[offset + 1]; break; default: return false; @@ -376,7 +380,7 @@ static bool dlmstp_compare_data_expecting_reply( if (!bacnet_address_same(&request.address, &reply.address)) { return false; } - + return true; } @@ -394,16 +398,16 @@ static bool dlmstp_compare_data_expecting_reply( /* least significant octet first */ /* (pad): (optional) at most one octet of padding: X'FF' */ static void MSTP_Send_Frame( - uint8_t frame_type, /* type of frame to send - see defines */ + uint8_t frame_type, /* type of frame to send - see defines */ uint8_t destination, /* destination address */ - uint8_t source, /* source address */ - uint8_t * data, /* any data to be sent - may be null */ + uint8_t source, /* source address */ + uint8_t * data, /* any data to be sent - may be null */ uint16_t data_len) -{ /* number of bytes of data (up to 501) */ +{ /* number of bytes of data (up to 501) */ uint8_t crc8 = 0xFF; /* used to calculate the crc value */ uint16_t crc16 = 0xFFFF; /* used to calculate the crc value */ - uint8_t buffer[8]; /* stores the header and data crc */ - uint16_t i = 0; /* used to calculate CRC for data */ + uint8_t buffer[8]; /* stores the header and data crc */ + uint16_t i = 0; /* used to calculate CRC for data */ /* create the MS/TP header */ buffer[0] = 0x55; @@ -421,12 +425,11 @@ static void MSTP_Send_Frame( buffer[7] = ~crc8; RS485_Turnaround_Delay(); RS485_Transmitter_Enable(true); - RS485_Send_Data(buffer,8); + RS485_Send_Data(buffer, 8); /* send any data */ if (data_len) { /* calculate CRC for any data */ - for (i = 0; i < data_len; i++) - { + for (i = 0; i < data_len; i++) { crc16 = CRC_Calc_Data(data[i], crc16); } crc16 = ~crc16; @@ -438,7 +441,8 @@ static void MSTP_Send_Frame( RS485_Transmitter_Enable(false); } -static void MSTP_Receive_Frame_FSM(void) +static void MSTP_Receive_Frame_FSM( + void) { /* stores the latest received data octet */ uint8_t DataRegister = 0; @@ -549,7 +553,7 @@ static void MSTP_Receive_Frame_FSM(void) /* HeaderCRC */ HeaderCRC = CRC_Calc_Header(DataRegister, HeaderCRC); /* In the HEADER_CRC state, the node validates the CRC - on the fixed message header. */ + on the fixed message header. */ if (HeaderCRC != 0x55) { /* BadCRC */ /* indicate that an error has occurred during @@ -559,11 +563,10 @@ static void MSTP_Receive_Frame_FSM(void) Receive_State = MSTP_RECEIVE_STATE_IDLE; } else { /* Note: proposed change to BACnet MSTP state machine! - If we don't decode data that is not for us, we could - get confused about the start if the Preamble 55 FF - is part of the data. */ - if ((DataLength) && - (DataLength <= InputBufferSize)) { + If we don't decode data that is not for us, we could + get confused about the start if the Preamble 55 FF + is part of the data. */ + if ((DataLength) && (DataLength <= InputBufferSize)) { /* Data */ Index = 0; DataCRC = 0xFFFF; @@ -572,11 +575,12 @@ static void MSTP_Receive_Frame_FSM(void) } else { if (DataLength == 0) { /* NoData */ - if ((DestinationAddress == This_Station) || - (DestinationAddress == MSTP_BROADCAST_ADDRESS)) { + if ((DestinationAddress == This_Station) || + (DestinationAddress == + MSTP_BROADCAST_ADDRESS)) { /* ForUs */ /* indicate that a frame with - no data has been received */ + no data has been received */ MSTP_Flag.ReceivedValidFrame = true; } else { /* NotForUs - drop */ @@ -635,7 +639,7 @@ static void MSTP_Receive_Frame_FSM(void) /* STATE DATA CRC - no need for new state */ /* indicate the complete reception of a valid frame */ if (DataCRC == 0xF0B8) { - if ((DestinationAddress == This_Station) || + if ((DestinationAddress == This_Station) || (DestinationAddress == MSTP_BROADCAST_ADDRESS)) { /* ForUs */ /* indicate that a frame with no data @@ -659,7 +663,8 @@ static void MSTP_Receive_Frame_FSM(void) } /* returns true if we need to transition immediately */ -static bool MSTP_Master_Node_FSM(void) +static bool MSTP_Master_Node_FSM( + void) { /* The number of frames sent by this node during a single token hold. */ /* When this counter reaches the value Nmax_info_frames, the node must */ @@ -695,494 +700,455 @@ static bool MSTP_Master_Node_FSM(void) next_this_station = (This_Station + 1) % (Nmax_master + 1); next_next_station = (Next_Station + 1) % (Nmax_master + 1); switch (Master_State) { - case MSTP_MASTER_STATE_INITIALIZE: - /* DoneInitializing */ - /* indicate that the next station is unknown */ - Next_Station = This_Station; - Poll_Station = This_Station; - /* cause a Poll For Master to be sent when this node first */ - /* receives the token */ - TokenCount = Npoll; - MSTP_Flag.SoleMaster = false; - Master_State = MSTP_MASTER_STATE_IDLE; - transition_now = true; - break; - case MSTP_MASTER_STATE_IDLE: - /* In the IDLE state, the node waits for a frame. */ - if (Timer_Silence() >= Tno_token) { - /* LostToken */ - /* assume that the token has been lost */ - EventCount = 0; /* Addendum 135-2004d-8 */ - Master_State = MSTP_MASTER_STATE_NO_TOKEN; + case MSTP_MASTER_STATE_INITIALIZE: + /* DoneInitializing */ + /* indicate that the next station is unknown */ + Next_Station = This_Station; + Poll_Station = This_Station; + /* cause a Poll For Master to be sent when this node first */ + /* receives the token */ + TokenCount = Npoll; + MSTP_Flag.SoleMaster = false; + Master_State = MSTP_MASTER_STATE_IDLE; transition_now = true; - } else if (MSTP_Flag.ReceivedInvalidFrame == true) { - /* ReceivedInvalidFrame */ - /* invalid frame was received */ - MSTP_Flag.ReceivedInvalidFrame = false; - /* wait for the next frame - remain in IDLE */ - } else if (MSTP_Flag.ReceivedValidFrame == true) { - switch (FrameType) { - case FRAME_TYPE_TOKEN: - /* ReceivedToken */ - /* tokens can't be broadcast */ - if (DestinationAddress == MSTP_BROADCAST_ADDRESS) - break; - MSTP_Flag.ReceivedValidFrame = false; - FrameCount = 0; - MSTP_Flag.SoleMaster = false; - Master_State = MSTP_MASTER_STATE_USE_TOKEN; - transition_now = true; - break; - case FRAME_TYPE_POLL_FOR_MASTER: - /* ReceivedPFM */ - MSTP_Send_Frame( - FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER, - SourceAddress, This_Station, - NULL, 0); - break; - case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: - /* indicate successful reception to the higher layers */ - MSTP_Flag.ReceivePacketPending = true; - break; - case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: - /* indicate successful reception to the higher layers */ - MSTP_Flag.ReceivePacketPending = true; - /* broadcast DER just remains IDLE */ - if (DestinationAddress != - MSTP_BROADCAST_ADDRESS) { - Master_State = - MSTP_MASTER_STATE_ANSWER_DATA_REQUEST; - } - break; - case FRAME_TYPE_TEST_REQUEST: - MSTP_Send_Frame( - FRAME_TYPE_TEST_RESPONSE, - SourceAddress, This_Station, - NULL, 0); - break; - case FRAME_TYPE_TEST_RESPONSE: - default: - break; - } - /* For DATA_EXPECTING_REPLY, we will keep the Rx Frame for - reference, and the flag will be cleared in the next state */ - if (Master_State != - MSTP_MASTER_STATE_ANSWER_DATA_REQUEST) { - MSTP_Flag.ReceivedValidFrame = false; - } - } - break; - /* In the USE_TOKEN state, the node is allowed to send one or */ - /* more data frames. These may be BACnet Data frames or */ - /* proprietary frames. */ - case MSTP_MASTER_STATE_USE_TOKEN: - /* Note: We could wait for up to Tusage_delay */ - if (!MSTP_Flag.TransmitPacketPending) { - /* NothingToSend */ - FrameCount = Nmax_info_frames; - Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; - transition_now = true; - } else { - uint8_t frame_type; - if (MSTP_Flag.TransmitPacketDER) { - frame_type = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY; - } else { - frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; - } - MSTP_Send_Frame( - frame_type, - TransmitPacketDest, - This_Station, - (uint8_t *) & TransmitPacket[0], - TransmitPacketLen); - MSTP_Flag.TransmitPacketPending = false; - FrameCount++; - switch (frame_type) { - case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: - /* SendAndWait */ - if (TransmitPacketDest == MSTP_BROADCAST_ADDRESS) - Master_State = - MSTP_MASTER_STATE_DONE_WITH_TOKEN; - else - Master_State = - MSTP_MASTER_STATE_WAIT_FOR_REPLY; - break; - case FRAME_TYPE_TEST_REQUEST: - Master_State = MSTP_MASTER_STATE_WAIT_FOR_REPLY; - break; - case FRAME_TYPE_TEST_RESPONSE: - case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: - default: - /* SendNoWait */ - Master_State = - MSTP_MASTER_STATE_DONE_WITH_TOKEN; - break; - } - } - break; - case MSTP_MASTER_STATE_WAIT_FOR_REPLY: - /* In the WAIT_FOR_REPLY state, the node waits for */ - /* a reply from another node. */ - if (Timer_Silence() >= Treply_timeout) { - /* ReplyTimeout */ - /* assume that the request has failed */ - FrameCount = Nmax_info_frames; - Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; - /* Any retry of the data frame shall await the next entry */ - /* to the USE_TOKEN state. (Because of the length of the timeout, */ - /* this transition will cause the token to be passed regardless */ - /* of the initial value of FrameCount.) */ - transition_now = true; - } else { - if (MSTP_Flag.ReceivedInvalidFrame == true) { - /* InvalidFrame */ - /* error in frame reception */ - MSTP_Flag.ReceivedInvalidFrame = false; - Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; + break; + case MSTP_MASTER_STATE_IDLE: + /* In the IDLE state, the node waits for a frame. */ + if (Timer_Silence() >= Tno_token) { + /* LostToken */ + /* assume that the token has been lost */ + EventCount = 0; /* Addendum 135-2004d-8 */ + Master_State = MSTP_MASTER_STATE_NO_TOKEN; transition_now = true; + } else if (MSTP_Flag.ReceivedInvalidFrame == true) { + /* ReceivedInvalidFrame */ + /* invalid frame was received */ + MSTP_Flag.ReceivedInvalidFrame = false; + /* wait for the next frame - remain in IDLE */ } else if (MSTP_Flag.ReceivedValidFrame == true) { - if (DestinationAddress == This_Station) { - /* What did we receive? */ - switch (FrameType) { - case FRAME_TYPE_REPLY_POSTPONED: - /* ReceivedReplyPostponed */ - Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; + switch (FrameType) { + case FRAME_TYPE_TOKEN: + /* ReceivedToken */ + /* tokens can't be broadcast */ + if (DestinationAddress == MSTP_BROADCAST_ADDRESS) + break; + MSTP_Flag.ReceivedValidFrame = false; + FrameCount = 0; + MSTP_Flag.SoleMaster = false; + Master_State = MSTP_MASTER_STATE_USE_TOKEN; + transition_now = true; break; - case FRAME_TYPE_TEST_RESPONSE: - Master_State = MSTP_MASTER_STATE_IDLE; + case FRAME_TYPE_POLL_FOR_MASTER: + /* ReceivedPFM */ + MSTP_Send_Frame(FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER, + SourceAddress, This_Station, NULL, 0); break; case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: - /* ReceivedReply */ - /* or a proprietary type that indicates a reply */ /* indicate successful reception to the higher layers */ MSTP_Flag.ReceivePacketPending = true; + break; + case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: + /* indicate successful reception to the higher layers */ + MSTP_Flag.ReceivePacketPending = true; + /* broadcast DER just remains IDLE */ + if (DestinationAddress != MSTP_BROADCAST_ADDRESS) { + Master_State = + MSTP_MASTER_STATE_ANSWER_DATA_REQUEST; + } + break; + case FRAME_TYPE_TEST_REQUEST: + MSTP_Send_Frame(FRAME_TYPE_TEST_RESPONSE, + SourceAddress, This_Station, NULL, 0); + break; + case FRAME_TYPE_TEST_RESPONSE: + default: + break; + } + /* For DATA_EXPECTING_REPLY, we will keep the Rx Frame for + reference, and the flag will be cleared in the next state */ + if (Master_State != MSTP_MASTER_STATE_ANSWER_DATA_REQUEST) { + MSTP_Flag.ReceivedValidFrame = false; + } + } + break; + /* In the USE_TOKEN state, the node is allowed to send one or */ + /* more data frames. These may be BACnet Data frames or */ + /* proprietary frames. */ + case MSTP_MASTER_STATE_USE_TOKEN: + /* Note: We could wait for up to Tusage_delay */ + if (!MSTP_Flag.TransmitPacketPending) { + /* NothingToSend */ + FrameCount = Nmax_info_frames; + Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; + transition_now = true; + } else { + uint8_t frame_type; + if (MSTP_Flag.TransmitPacketDER) { + frame_type = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY; + } else { + frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; + } + MSTP_Send_Frame(frame_type, + TransmitPacketDest, + This_Station, + (uint8_t *) & TransmitPacket[0], TransmitPacketLen); + MSTP_Flag.TransmitPacketPending = false; + FrameCount++; + switch (frame_type) { + case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: + /* SendAndWait */ + if (TransmitPacketDest == MSTP_BROADCAST_ADDRESS) + Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; + else + Master_State = MSTP_MASTER_STATE_WAIT_FOR_REPLY; + break; + case FRAME_TYPE_TEST_REQUEST: + Master_State = MSTP_MASTER_STATE_WAIT_FOR_REPLY; + break; + case FRAME_TYPE_TEST_RESPONSE: + case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: + default: + /* SendNoWait */ Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; break; - default: - /* if proprietary frame was expected, you might - need to transition to DONE WITH TOKEN */ - Master_State = MSTP_MASTER_STATE_IDLE; - break; - } - } else { - /* ReceivedUnexpectedFrame */ - /* an unexpected frame was received */ - /* This may indicate the presence of multiple tokens */ - /* or a device that didn't see activity after passing */ - /* a token (how lame!). */ - /* Synchronize with the network. */ - /* This action drops the token. */ - Master_State = MSTP_MASTER_STATE_IDLE; } - MSTP_Flag.ReceivedValidFrame = false; - transition_now = true; } - } - break; - /* The DONE_WITH_TOKEN state either sends another data frame, */ - /* passes the token, or initiates a Poll For Master cycle. */ - case MSTP_MASTER_STATE_DONE_WITH_TOKEN: - /* SendAnotherFrame */ - if (FrameCount < Nmax_info_frames) { - /* then this node may send another information frame */ - /* before passing the token. */ - Master_State = MSTP_MASTER_STATE_USE_TOKEN; - transition_now = true; - } - /* Npoll changed in Errata SSPC-135-2004 */ - else if (TokenCount < (Npoll - 1)) { - if ((MSTP_Flag.SoleMaster == true) && - (Next_Station != next_this_station)) { - /* SoleMaster */ - /* there are no other known master nodes to */ - /* which the token may be sent (true master-slave operation). */ - FrameCount = 0; - TokenCount++; + break; + case MSTP_MASTER_STATE_WAIT_FOR_REPLY: + /* In the WAIT_FOR_REPLY state, the node waits for */ + /* a reply from another node. */ + if (Timer_Silence() >= Treply_timeout) { + /* ReplyTimeout */ + /* assume that the request has failed */ + FrameCount = Nmax_info_frames; + Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; + /* Any retry of the data frame shall await the next entry */ + /* to the USE_TOKEN state. (Because of the length of the timeout, */ + /* this transition will cause the token to be passed regardless */ + /* of the initial value of FrameCount.) */ + transition_now = true; + } else { + if (MSTP_Flag.ReceivedInvalidFrame == true) { + /* InvalidFrame */ + /* error in frame reception */ + MSTP_Flag.ReceivedInvalidFrame = false; + Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; + transition_now = true; + } else if (MSTP_Flag.ReceivedValidFrame == true) { + if (DestinationAddress == This_Station) { + /* What did we receive? */ + switch (FrameType) { + case FRAME_TYPE_REPLY_POSTPONED: + /* ReceivedReplyPostponed */ + Master_State = + MSTP_MASTER_STATE_DONE_WITH_TOKEN; + break; + case FRAME_TYPE_TEST_RESPONSE: + Master_State = MSTP_MASTER_STATE_IDLE; + break; + case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: + /* ReceivedReply */ + /* or a proprietary type that indicates a reply */ + /* indicate successful reception to the higher layers */ + MSTP_Flag.ReceivePacketPending = true; + Master_State = + MSTP_MASTER_STATE_DONE_WITH_TOKEN; + break; + default: + /* if proprietary frame was expected, you might + need to transition to DONE WITH TOKEN */ + Master_State = MSTP_MASTER_STATE_IDLE; + break; + } + } else { + /* ReceivedUnexpectedFrame */ + /* an unexpected frame was received */ + /* This may indicate the presence of multiple tokens */ + /* or a device that didn't see activity after passing */ + /* a token (how lame!). */ + /* Synchronize with the network. */ + /* This action drops the token. */ + Master_State = MSTP_MASTER_STATE_IDLE; + } + MSTP_Flag.ReceivedValidFrame = false; + transition_now = true; + } + } + break; + /* The DONE_WITH_TOKEN state either sends another data frame, */ + /* passes the token, or initiates a Poll For Master cycle. */ + case MSTP_MASTER_STATE_DONE_WITH_TOKEN: + /* SendAnotherFrame */ + if (FrameCount < Nmax_info_frames) { + /* then this node may send another information frame */ + /* before passing the token. */ Master_State = MSTP_MASTER_STATE_USE_TOKEN; transition_now = true; - } else { - /* SendToken */ - /* Npoll changed in Errata SSPC-135-2004 */ - /* The comparison of NS and TS+1 eliminates the Poll For Master */ - /* if there are no addresses between TS and NS, since there is no */ - /* address at which a new master node may be found in that case. */ - TokenCount++; - /* transmit a Token frame to NS */ - MSTP_Send_Frame( - FRAME_TYPE_TOKEN, - Next_Station, - This_Station, NULL, 0); - RetryCount = 0; - EventCount = 0; - Master_State = MSTP_MASTER_STATE_PASS_TOKEN; } - } else if (next_poll_station == Next_Station) { - if (MSTP_Flag.SoleMaster == true) { - /* SoleMasterRestartMaintenancePFM */ - Poll_Station = next_next_station; - MSTP_Send_Frame( - FRAME_TYPE_POLL_FOR_MASTER, Poll_Station, - This_Station, NULL, 0); - /* no known successor node */ - Next_Station = This_Station; - RetryCount = 0; - TokenCount = 1; /* changed in Errata SSPC-135-2004 */ - /* EventCount = 0; removed in Addendum 135-2004d-8 */ - /* find a new successor to TS */ - Master_State = - MSTP_MASTER_STATE_POLL_FOR_MASTER; - } else { - /* ResetMaintenancePFM */ - Poll_Station = This_Station; - /* transmit a Token frame to NS */ - MSTP_Send_Frame( - FRAME_TYPE_TOKEN, - Next_Station, - This_Station, NULL, 0); - RetryCount = 0; - TokenCount = 1; /* changed in Errata SSPC-135-2004 */ - EventCount = 0; - Master_State = MSTP_MASTER_STATE_PASS_TOKEN; - } - } else { - /* SendMaintenancePFM */ - Poll_Station = next_poll_station; - MSTP_Send_Frame( - FRAME_TYPE_POLL_FOR_MASTER, - Poll_Station, This_Station, NULL, 0); - RetryCount = 0; - Master_State = MSTP_MASTER_STATE_POLL_FOR_MASTER; - } - break; - /* The PASS_TOKEN state listens for a successor to begin using */ - /* the token that this node has just attempted to pass. */ - case MSTP_MASTER_STATE_PASS_TOKEN: - if (Timer_Silence() <= Tusage_timeout) { - if (EventCount > Nmin_octets) { - /* SawTokenUser */ - /* Assume that a frame has been sent by the new token user. */ - /* Enter the IDLE state to process the frame. */ - Master_State = MSTP_MASTER_STATE_IDLE; - transition_now = true; - } - } else { - if (RetryCount < Nretry_token) { - /* RetrySendToken */ - RetryCount++; - /* Transmit a Token frame to NS */ - MSTP_Send_Frame( - FRAME_TYPE_TOKEN, - Next_Station, This_Station, NULL, - 0); - EventCount = 0; - /* re-enter the current state to listen for NS */ - /* to begin using the token. */ - } else { - /* FindNewSuccessor */ - /* Assume that NS has failed. */ - Poll_Station = next_next_station; - /* Transmit a Poll For Master frame to PS. */ - MSTP_Send_Frame( - FRAME_TYPE_POLL_FOR_MASTER, - Poll_Station, This_Station, NULL, - 0); - /* no known successor node */ - Next_Station = This_Station; - RetryCount = 0; - TokenCount = 0; - /* EventCount = 0; removed in Addendum 135-2004d-8 */ - /* find a new successor to TS */ - Master_State = - MSTP_MASTER_STATE_POLL_FOR_MASTER; - } - } - break; - /* The NO_TOKEN state is entered if Timer_Silence() becomes greater */ - /* than Tno_token, indicating that there has been no network activity */ - /* for that period of time. The timeout is continued to determine */ - /* whether or not this node may create a token. */ - case MSTP_MASTER_STATE_NO_TOKEN: - my_timeout = Tno_token + (Tslot * This_Station); - if (Timer_Silence() < my_timeout) { - if (EventCount > Nmin_octets) { - /* SawFrame */ - /* Some other node exists at a lower address. */ - /* Enter the IDLE state to receive and process the incoming frame. */ - Master_State = MSTP_MASTER_STATE_IDLE; - transition_now = true; - } - } else { - ns_timeout = - Tno_token + (Tslot * (This_Station + 1)); - if (Timer_Silence() < ns_timeout) { - /* GenerateToken */ - /* Assume that this node is the lowest numerical address */ - /* on the network and is empowered to create a token. */ - Poll_Station = next_this_station; - /* Transmit a Poll For Master frame to PS. */ - MSTP_Send_Frame( - FRAME_TYPE_POLL_FOR_MASTER, - Poll_Station, This_Station, NULL, - 0); - /* indicate that the next station is unknown */ - Next_Station = This_Station; - RetryCount = 0; - TokenCount = 0; - /* EventCount = 0; removed Addendum 135-2004d-8 */ - /* enter the POLL_FOR_MASTER state to find a new successor to TS. */ - Master_State = - MSTP_MASTER_STATE_POLL_FOR_MASTER; - } - } - break; - /* In the POLL_FOR_MASTER state, the node listens for a reply to */ - /* a previously sent Poll For Master frame in order to find */ - /* a successor node. */ - case MSTP_MASTER_STATE_POLL_FOR_MASTER: - if (MSTP_Flag.ReceivedValidFrame == true) { - if ((DestinationAddress == This_Station) - && (FrameType == - FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER)) { - /* ReceivedReplyToPFM */ - MSTP_Flag.SoleMaster = false; - Next_Station = SourceAddress; - EventCount = 0; - /* Transmit a Token frame to NS */ - MSTP_Send_Frame( - FRAME_TYPE_TOKEN, - Next_Station, This_Station, NULL, - 0); - Poll_Station = This_Station; - TokenCount = 0; - RetryCount = 0; - Master_State = MSTP_MASTER_STATE_PASS_TOKEN; - } else { - /* ReceivedUnexpectedFrame */ - /* An unexpected frame was received. */ - /* This may indicate the presence of multiple tokens. */ - /* enter the IDLE state to synchronize with the network. */ - /* This action drops the token. */ - Master_State = MSTP_MASTER_STATE_IDLE; - transition_now = true; - } - MSTP_Flag.ReceivedValidFrame = false; - } else if ((Timer_Silence() > Tusage_timeout) || - (MSTP_Flag.ReceivedInvalidFrame == true)) { - if (MSTP_Flag.SoleMaster == true) { - /* SoleMaster */ - /* There was no valid reply to the periodic poll */ - /* by the sole known master for other masters. */ - FrameCount = 0; - /* TokenCount++; removed in 2004 */ - Master_State = MSTP_MASTER_STATE_USE_TOKEN; - transition_now = true; - } else { - if (Next_Station != This_Station) { - /* DoneWithPFM */ - /* There was no valid reply to the maintenance */ - /* poll for a master at address PS. */ - EventCount = 0; + /* Npoll changed in Errata SSPC-135-2004 */ + else if (TokenCount < (Npoll - 1)) { + if ((MSTP_Flag.SoleMaster == true) && + (Next_Station != next_this_station)) { + /* SoleMaster */ + /* there are no other known master nodes to */ + /* which the token may be sent (true master-slave operation). */ + FrameCount = 0; + TokenCount++; + Master_State = MSTP_MASTER_STATE_USE_TOKEN; + transition_now = true; + } else { + /* SendToken */ + /* Npoll changed in Errata SSPC-135-2004 */ + /* The comparison of NS and TS+1 eliminates the Poll For Master */ + /* if there are no addresses between TS and NS, since there is no */ + /* address at which a new master node may be found in that case. */ + TokenCount++; /* transmit a Token frame to NS */ - MSTP_Send_Frame( - FRAME_TYPE_TOKEN, - Next_Station, This_Station, - NULL, 0); + MSTP_Send_Frame(FRAME_TYPE_TOKEN, + Next_Station, This_Station, NULL, 0); + RetryCount = 0; + EventCount = 0; + Master_State = MSTP_MASTER_STATE_PASS_TOKEN; + } + } else if (next_poll_station == Next_Station) { + if (MSTP_Flag.SoleMaster == true) { + /* SoleMasterRestartMaintenancePFM */ + Poll_Station = next_next_station; + MSTP_Send_Frame(FRAME_TYPE_POLL_FOR_MASTER, Poll_Station, + This_Station, NULL, 0); + /* no known successor node */ + Next_Station = This_Station; + RetryCount = 0; + TokenCount = 1; /* changed in Errata SSPC-135-2004 */ + /* EventCount = 0; removed in Addendum 135-2004d-8 */ + /* find a new successor to TS */ + Master_State = MSTP_MASTER_STATE_POLL_FOR_MASTER; + } else { + /* ResetMaintenancePFM */ + Poll_Station = This_Station; + /* transmit a Token frame to NS */ + MSTP_Send_Frame(FRAME_TYPE_TOKEN, + Next_Station, This_Station, NULL, 0); + RetryCount = 0; + TokenCount = 1; /* changed in Errata SSPC-135-2004 */ + EventCount = 0; + Master_State = MSTP_MASTER_STATE_PASS_TOKEN; + } + } else { + /* SendMaintenancePFM */ + Poll_Station = next_poll_station; + MSTP_Send_Frame(FRAME_TYPE_POLL_FOR_MASTER, + Poll_Station, This_Station, NULL, 0); + RetryCount = 0; + Master_State = MSTP_MASTER_STATE_POLL_FOR_MASTER; + } + break; + /* The PASS_TOKEN state listens for a successor to begin using */ + /* the token that this node has just attempted to pass. */ + case MSTP_MASTER_STATE_PASS_TOKEN: + if (Timer_Silence() <= Tusage_timeout) { + if (EventCount > Nmin_octets) { + /* SawTokenUser */ + /* Assume that a frame has been sent by the new token user. */ + /* Enter the IDLE state to process the frame. */ + Master_State = MSTP_MASTER_STATE_IDLE; + transition_now = true; + } + } else { + if (RetryCount < Nretry_token) { + /* RetrySendToken */ + RetryCount++; + /* Transmit a Token frame to NS */ + MSTP_Send_Frame(FRAME_TYPE_TOKEN, + Next_Station, This_Station, NULL, 0); + EventCount = 0; + /* re-enter the current state to listen for NS */ + /* to begin using the token. */ + } else { + /* FindNewSuccessor */ + /* Assume that NS has failed. */ + Poll_Station = next_next_station; + /* Transmit a Poll For Master frame to PS. */ + MSTP_Send_Frame(FRAME_TYPE_POLL_FOR_MASTER, + Poll_Station, This_Station, NULL, 0); + /* no known successor node */ + Next_Station = This_Station; + RetryCount = 0; + TokenCount = 0; + /* EventCount = 0; removed in Addendum 135-2004d-8 */ + /* find a new successor to TS */ + Master_State = MSTP_MASTER_STATE_POLL_FOR_MASTER; + } + } + break; + /* The NO_TOKEN state is entered if Timer_Silence() becomes greater */ + /* than Tno_token, indicating that there has been no network activity */ + /* for that period of time. The timeout is continued to determine */ + /* whether or not this node may create a token. */ + case MSTP_MASTER_STATE_NO_TOKEN: + my_timeout = Tno_token + (Tslot * This_Station); + if (Timer_Silence() < my_timeout) { + if (EventCount > Nmin_octets) { + /* SawFrame */ + /* Some other node exists at a lower address. */ + /* Enter the IDLE state to receive and process the incoming frame. */ + Master_State = MSTP_MASTER_STATE_IDLE; + transition_now = true; + } + } else { + ns_timeout = Tno_token + (Tslot * (This_Station + 1)); + if (Timer_Silence() < ns_timeout) { + /* GenerateToken */ + /* Assume that this node is the lowest numerical address */ + /* on the network and is empowered to create a token. */ + Poll_Station = next_this_station; + /* Transmit a Poll For Master frame to PS. */ + MSTP_Send_Frame(FRAME_TYPE_POLL_FOR_MASTER, + Poll_Station, This_Station, NULL, 0); + /* indicate that the next station is unknown */ + Next_Station = This_Station; + RetryCount = 0; + TokenCount = 0; + /* EventCount = 0; removed Addendum 135-2004d-8 */ + /* enter the POLL_FOR_MASTER state to find a new successor to TS. */ + Master_State = MSTP_MASTER_STATE_POLL_FOR_MASTER; + } + } + break; + /* In the POLL_FOR_MASTER state, the node listens for a reply to */ + /* a previously sent Poll For Master frame in order to find */ + /* a successor node. */ + case MSTP_MASTER_STATE_POLL_FOR_MASTER: + if (MSTP_Flag.ReceivedValidFrame == true) { + if ((DestinationAddress == This_Station) + && (FrameType == FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER)) { + /* ReceivedReplyToPFM */ + MSTP_Flag.SoleMaster = false; + Next_Station = SourceAddress; + EventCount = 0; + /* Transmit a Token frame to NS */ + MSTP_Send_Frame(FRAME_TYPE_TOKEN, + Next_Station, This_Station, NULL, 0); + Poll_Station = This_Station; + TokenCount = 0; RetryCount = 0; Master_State = MSTP_MASTER_STATE_PASS_TOKEN; } else { - if (next_poll_station != This_Station) { - /* SendNextPFM */ - Poll_Station = next_poll_station; - /* Transmit a Poll For Master frame to PS. */ - MSTP_Send_Frame( - FRAME_TYPE_POLL_FOR_MASTER, - Poll_Station, - This_Station, NULL, 0); + /* ReceivedUnexpectedFrame */ + /* An unexpected frame was received. */ + /* This may indicate the presence of multiple tokens. */ + /* enter the IDLE state to synchronize with the network. */ + /* This action drops the token. */ + Master_State = MSTP_MASTER_STATE_IDLE; + transition_now = true; + } + MSTP_Flag.ReceivedValidFrame = false; + } else if ((Timer_Silence() > Tusage_timeout) || + (MSTP_Flag.ReceivedInvalidFrame == true)) { + if (MSTP_Flag.SoleMaster == true) { + /* SoleMaster */ + /* There was no valid reply to the periodic poll */ + /* by the sole known master for other masters. */ + FrameCount = 0; + /* TokenCount++; removed in 2004 */ + Master_State = MSTP_MASTER_STATE_USE_TOKEN; + transition_now = true; + } else { + if (Next_Station != This_Station) { + /* DoneWithPFM */ + /* There was no valid reply to the maintenance */ + /* poll for a master at address PS. */ + EventCount = 0; + /* transmit a Token frame to NS */ + MSTP_Send_Frame(FRAME_TYPE_TOKEN, + Next_Station, This_Station, NULL, 0); RetryCount = 0; - /* Re-enter the current state. */ + Master_State = MSTP_MASTER_STATE_PASS_TOKEN; } else { - /* DeclareSoleMaster */ - /* to indicate that this station is the only master */ - MSTP_Flag.SoleMaster = true; - FrameCount = 0; - Master_State = - MSTP_MASTER_STATE_USE_TOKEN; - transition_now = true; + if (next_poll_station != This_Station) { + /* SendNextPFM */ + Poll_Station = next_poll_station; + /* Transmit a Poll For Master frame to PS. */ + MSTP_Send_Frame(FRAME_TYPE_POLL_FOR_MASTER, + Poll_Station, This_Station, NULL, 0); + RetryCount = 0; + /* Re-enter the current state. */ + } else { + /* DeclareSoleMaster */ + /* to indicate that this station is the only master */ + MSTP_Flag.SoleMaster = true; + FrameCount = 0; + Master_State = MSTP_MASTER_STATE_USE_TOKEN; + transition_now = true; + } } } + MSTP_Flag.ReceivedInvalidFrame = false; } - MSTP_Flag.ReceivedInvalidFrame = false; - } - break; - /* The ANSWER_DATA_REQUEST state is entered when a */ - /* BACnet Data Expecting Reply, a Test_Request, or */ - /* a proprietary frame that expects a reply is received. */ - case MSTP_MASTER_STATE_ANSWER_DATA_REQUEST: - /* Note: we could wait for up to Treply_delay */ - if (MSTP_Flag.TransmitPacketPending) { - matched = dlmstp_compare_data_expecting_reply( - &InputBuffer[0], - DataLength, - SourceAddress, - &TransmitPacket[0], - TransmitPacketLen, - TransmitPacketDest); - } - if (MSTP_Flag.TransmitPacketPending && matched) { - /* Reply */ - /* If a reply is available from the higher layers */ - /* within Treply_delay after the reception of the */ - /* final octet of the requesting frame */ - /* (the mechanism used to determine this is a local matter), */ - /* then call MSTP_Send_Frame to transmit the reply frame */ - /* and enter the IDLE state to wait for the next frame. */ - uint8_t frame_type; - if (MSTP_Flag.TransmitPacketDER) { - frame_type = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY; + break; + /* The ANSWER_DATA_REQUEST state is entered when a */ + /* BACnet Data Expecting Reply, a Test_Request, or */ + /* a proprietary frame that expects a reply is received. */ + case MSTP_MASTER_STATE_ANSWER_DATA_REQUEST: + /* Note: we could wait for up to Treply_delay */ + if (MSTP_Flag.TransmitPacketPending) { + matched = dlmstp_compare_data_expecting_reply(&InputBuffer[0], + DataLength, + SourceAddress, + &TransmitPacket[0], TransmitPacketLen, TransmitPacketDest); + } + if (MSTP_Flag.TransmitPacketPending && matched) { + /* Reply */ + /* If a reply is available from the higher layers */ + /* within Treply_delay after the reception of the */ + /* final octet of the requesting frame */ + /* (the mechanism used to determine this is a local matter), */ + /* then call MSTP_Send_Frame to transmit the reply frame */ + /* and enter the IDLE state to wait for the next frame. */ + uint8_t frame_type; + if (MSTP_Flag.TransmitPacketDER) { + frame_type = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY; + } else { + frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; + } + MSTP_Send_Frame(frame_type, + TransmitPacketDest, + This_Station, + (uint8_t *) & TransmitPacket[0], TransmitPacketLen); + MSTP_Flag.TransmitPacketPending = false; + Master_State = MSTP_MASTER_STATE_IDLE; } else { - frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; + /* DeferredReply */ + /* If no reply will be available from the higher layers */ + /* within Treply_delay after the reception of the */ + /* final octet of the requesting frame (the mechanism */ + /* used to determine this is a local matter), */ + /* then an immediate reply is not possible. */ + /* Any reply shall wait until this node receives the token. */ + /* Call MSTP_Send_Frame to transmit a Reply Postponed frame, */ + /* and enter the IDLE state. */ + MSTP_Send_Frame(FRAME_TYPE_REPLY_POSTPONED, + SourceAddress, This_Station, NULL, 0); + Master_State = MSTP_MASTER_STATE_IDLE; } - MSTP_Send_Frame( - frame_type, - TransmitPacketDest, - This_Station, - (uint8_t *) & TransmitPacket[0], - TransmitPacketLen); - MSTP_Flag.TransmitPacketPending = false; + /* clear our flag we were holding for comparison */ + MSTP_Flag.ReceivedValidFrame = false; + break; + default: Master_State = MSTP_MASTER_STATE_IDLE; - } else { - /* DeferredReply */ - /* If no reply will be available from the higher layers */ - /* within Treply_delay after the reception of the */ - /* final octet of the requesting frame (the mechanism */ - /* used to determine this is a local matter), */ - /* then an immediate reply is not possible. */ - /* Any reply shall wait until this node receives the token. */ - /* Call MSTP_Send_Frame to transmit a Reply Postponed frame, */ - /* and enter the IDLE state. */ - MSTP_Send_Frame( - FRAME_TYPE_REPLY_POSTPONED, - SourceAddress, - This_Station, NULL, 0); - Master_State = MSTP_MASTER_STATE_IDLE; - } - /* clear our flag we were holding for comparison */ - MSTP_Flag.ReceivedValidFrame = false; - break; - default: - Master_State = MSTP_MASTER_STATE_IDLE; - break; + break; } return transition_now; } /* returns number of bytes sent on success, zero on failure */ -int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */ - BACNET_NPDU_DATA * npdu_data, /* network information */ - uint8_t * pdu, /* any data to be sent - may be null */ - unsigned pdu_len) /* number of bytes of data */ -{ +int dlmstp_send_pdu( + BACNET_ADDRESS * dest, /* destination address */ + BACNET_NPDU_DATA * npdu_data, /* network information */ + uint8_t * pdu, /* any data to be sent - may be null */ + unsigned pdu_len) +{ /* number of bytes of data */ int bytes_sent = 0; if (MSTP_Flag.TransmitPacketPending == false) { @@ -1199,12 +1165,12 @@ int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */ /* Return the length of the packet */ uint16_t dlmstp_receive( - BACNET_ADDRESS * src, /* source address */ - uint8_t * pdu, /* PDU data */ - uint16_t max_pdu, /* amount of space available in the PDU */ - unsigned timeout) /* milliseconds to wait for a packet */ -{ - uint16_t pdu_len = 0; /* return value */ + BACNET_ADDRESS * src, /* source address */ + uint8_t * pdu, /* PDU data */ + uint16_t max_pdu, /* amount of space available in the PDU */ + unsigned timeout) +{ /* milliseconds to wait for a packet */ + uint16_t pdu_len = 0; /* return value */ /* set the input buffer to the same data storage for zero copy */ if (!InputBuffer) { @@ -1216,8 +1182,7 @@ uint16_t dlmstp_receive( (MSTP_Flag.ReceivedInvalidFrame == false)) { for (;;) { MSTP_Receive_Frame_FSM(); - if (MSTP_Flag.ReceivedValidFrame || - MSTP_Flag.ReceivedInvalidFrame) + if (MSTP_Flag.ReceivedValidFrame || MSTP_Flag.ReceivedInvalidFrame) break; /* if we are not idle, then we are receiving a frame or timing out */ @@ -1230,7 +1195,7 @@ uint16_t dlmstp_receive( while (MSTP_Master_Node_FSM()) { /* do nothing while some states fast transition */ }; - } + } /* if there is a packet that needs processed, do it now. */ if (MSTP_Flag.ReceivePacketPending) { MSTP_Flag.ReceivePacketPending = false; @@ -1243,7 +1208,8 @@ uint16_t dlmstp_receive( return pdu_len; } -void dlmstp_set_mac_address(uint8_t mac_address) +void dlmstp_set_mac_address( + uint8_t mac_address) { /* Master Nodes can only have address 0-127 */ if (mac_address <= 127) { @@ -1260,7 +1226,8 @@ void dlmstp_set_mac_address(uint8_t mac_address) return; } -uint8_t dlmstp_mac_address(void) +uint8_t dlmstp_mac_address( + void) { return This_Station; } @@ -1272,7 +1239,8 @@ uint8_t dlmstp_mac_address(void) /* nodes. This may be used to allocate more or less of the available link */ /* bandwidth to particular nodes. If Max_Info_Frames is not writable in a */ /* node, its value shall be 1. */ -void dlmstp_set_max_info_frames(uint8_t max_info_frames) +void dlmstp_set_max_info_frames( + uint8_t max_info_frames) { if (max_info_frames >= 1) { Nmax_info_frames = max_info_frames; @@ -1286,7 +1254,8 @@ void dlmstp_set_max_info_frames(uint8_t max_info_frames) return; } -uint8_t dlmstp_max_info_frames(void) +uint8_t dlmstp_max_info_frames( + void) { return Nmax_info_frames; } @@ -1296,7 +1265,8 @@ uint8_t dlmstp_max_info_frames(void) /* allowable address for master nodes. The value of Max_Master shall be */ /* less than or equal to 127. If Max_Master is not writable in a node, */ /* its value shall be 127. */ -void dlmstp_set_max_master(uint8_t max_master) +void dlmstp_set_max_master( + uint8_t max_master) { if (max_master <= 127) { if (This_Station <= max_master) { @@ -1312,14 +1282,16 @@ void dlmstp_set_max_master(uint8_t max_master) return; } -uint8_t dlmstp_max_master(void) +uint8_t dlmstp_max_master( + void) { return Nmax_master; } -void dlmstp_get_my_address(BACNET_ADDRESS * my_address) +void dlmstp_get_my_address( + BACNET_ADDRESS * my_address) { - int i = 0; /* counter */ + int i = 0; /* counter */ my_address->mac_len = 1; my_address->mac[0] = This_Station; @@ -1332,15 +1304,16 @@ void dlmstp_get_my_address(BACNET_ADDRESS * my_address) return; } -void dlmstp_get_broadcast_address(BACNET_ADDRESS * dest) -{ /* destination address */ - int i = 0; /* counter */ +void dlmstp_get_broadcast_address( + BACNET_ADDRESS * dest) +{ /* destination address */ + int i = 0; /* counter */ if (dest) { dest->mac_len = 1; dest->mac[0] = MSTP_BROADCAST_ADDRESS; dest->net = BACNET_BROADCAST_NETWORK; - dest->len = 0; /* always zero when DNET is broadcast */ + dest->len = 0; /* always zero when DNET is broadcast */ for (i = 0; i < MAX_MAC_LEN; i++) { dest->adr[i] = 0; } diff --git a/bacnet-stack/ports/at91sam7s/h_rp.c b/bacnet-stack/ports/at91sam7s/h_rp.c index ac012ba6..a7c32396 100644 --- a/bacnet-stack/ports/at91sam7s/h_rp.c +++ b/bacnet-stack/ports/at91sam7s/h_rp.c @@ -62,52 +62,39 @@ int Encode_Property_APDU( *error_class = ERROR_CLASS_OBJECT; *error_code = ERROR_CODE_UNKNOWN_OBJECT; /* handle each object type */ - switch(object_type) { + switch (object_type) { case OBJECT_DEVICE: if (Device_Valid_Object_Instance_Number(object_instance)) { - apdu_len = Device_Encode_Property_APDU( - &apdu[0], - property, - array_index, - error_class, error_code); + apdu_len = Device_Encode_Property_APDU(&apdu[0], + property, array_index, error_class, error_code); } break; case OBJECT_ANALOG_INPUT: if (Analog_Input_Valid_Instance(object_instance)) { - apdu_len = Analog_Input_Encode_Property_APDU( - &apdu[0], + apdu_len = Analog_Input_Encode_Property_APDU(&apdu[0], object_instance, - property, - array_index, - error_class, error_code); + property, array_index, error_class, error_code); } break; case OBJECT_ANALOG_VALUE: if (Analog_Value_Valid_Instance(object_instance)) { apdu_len = Analog_Value_Encode_Property_APDU(&Temp_Buf[0], object_instance, - property, - array_index, - error_class, error_code); + property, array_index, error_class, error_code); } break; case OBJECT_BINARY_INPUT: if (Binary_Input_Valid_Instance(object_instance)) { - apdu_len = Binary_Input_Encode_Property_APDU( - &apdu[0], + apdu_len = Binary_Input_Encode_Property_APDU(&apdu[0], object_instance, - property, - array_index, - error_class, error_code); + property, array_index, error_class, error_code); } break; case OBJECT_BINARY_VALUE: if (Binary_Value_Valid_Instance(object_instance)) { apdu_len = Binary_Value_Encode_Property_APDU(&Temp_Buf[0], object_instance, - property, - array_index, - error_class, error_code); + property, array_index, error_class, error_code); } break; default: @@ -119,9 +106,11 @@ int Encode_Property_APDU( return apdu_len; } -void handler_read_property(uint8_t * service_request, +void handler_read_property( + uint8_t * service_request, uint16_t service_len, - BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data) + BACNET_ADDRESS * src, + BACNET_CONFIRMED_SERVICE_DATA * service_data) { BACNET_READ_PROPERTY_DATA data; int len = 0; @@ -154,13 +143,10 @@ void handler_read_property(uint8_t * service_request, } /* most cases will be error */ error = true; - len = Encode_Property_APDU( - &Temp_Buf[0], + len = Encode_Property_APDU(&Temp_Buf[0], data.object_type, data.object_instance, - data.object_property, - data.array_index, - &error_class, &error_code); + data.object_property, data.array_index, &error_class, &error_code); if (len >= 0) { /* encode the APDU portion of the packet */ data.application_data = &Temp_Buf[0]; @@ -183,7 +169,7 @@ void handler_read_property(uint8_t * service_request, service_data->invoke_id, SERVICE_CONFIRMED_READ_PROPERTY, error_class, error_code); } -RP_ABORT: + RP_ABORT: pdu_len += len; bytes_sent = datalink_send_pdu(src, &npdu_data, &Handler_Transmit_Buffer[0], pdu_len); diff --git a/bacnet-stack/ports/at91sam7s/h_wp.c b/bacnet-stack/ports/at91sam7s/h_wp.c index c10fca80..46771934 100644 --- a/bacnet-stack/ports/at91sam7s/h_wp.c +++ b/bacnet-stack/ports/at91sam7s/h_wp.c @@ -46,9 +46,11 @@ /* too big to reside on stack frame for PIC */ static BACNET_WRITE_PROPERTY_DATA wp_data; -void handler_write_property(uint8_t * service_request, +void handler_write_property( + uint8_t * service_request, uint16_t service_len, - BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data) + BACNET_ADDRESS * src, + BACNET_CONFIRMED_SERVICE_DATA * service_data) { int len = 0; int pdu_len = 0; @@ -70,8 +72,7 @@ void handler_write_property(uint8_t * service_request, goto WP_ABORT; } /* decode the service request only */ - len = wp_decode_service_request(service_request, - service_len, &wp_data); + len = wp_decode_service_request(service_request, service_len, &wp_data); /* bad decoding or something we didn't understand - send an abort */ if (len <= 0) { len = abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], @@ -80,67 +81,61 @@ void handler_write_property(uint8_t * service_request, } /* handle the object type */ switch (wp_data.object_type) { - case OBJECT_DEVICE: - if (Device_Write_Property(&wp_data, &error_class, &error_code)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY); - } else { + case OBJECT_DEVICE: + if (Device_Write_Property(&wp_data, &error_class, &error_code)) { + len = + encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY); + } else { + len = + bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, error_code); + } + break; + case OBJECT_ANALOG_INPUT: + case OBJECT_BINARY_INPUT: + error_class = ERROR_CLASS_PROPERTY; + error_code = ERROR_CODE_WRITE_ACCESS_DENIED; len = bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, - error_code); - } - break; - case OBJECT_ANALOG_INPUT: - case OBJECT_BINARY_INPUT: - error_class = ERROR_CLASS_PROPERTY; - error_code = ERROR_CODE_WRITE_ACCESS_DENIED; - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, - error_class, error_code); - break; - case OBJECT_BINARY_VALUE: - if (Binary_Value_Write_Property(&wp_data, &error_class, - &error_code)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY); - } else { + service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, + error_class, error_code); + break; + case OBJECT_BINARY_VALUE: + if (Binary_Value_Write_Property(&wp_data, &error_class, + &error_code)) { + len = + encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY); + } else { + len = + bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, error_code); + } + break; + case OBJECT_ANALOG_VALUE: + if (Analog_Value_Write_Property(&wp_data, &error_class, + &error_code)) { + len = + encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY); + } else { + len = + bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, error_code); + } + break; + default: len = bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, - error_code); - } - break; - case OBJECT_ANALOG_VALUE: - if (Analog_Value_Write_Property(&wp_data, &error_class, - &error_code)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY); - } else { - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, - error_code); - } - break; - default: - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, - error_class, error_code); - break; + service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, + error_class, error_code); + break; } -WP_ABORT: + WP_ABORT: pdu_len += len; bytes_sent = datalink_send_pdu(src, &npdu_data, &Handler_Transmit_Buffer[0], pdu_len); diff --git a/bacnet-stack/ports/at91sam7s/init.c b/bacnet-stack/ports/at91sam7s/init.c index 28d70cef..aee5819f 100644 --- a/bacnet-stack/ports/at91sam7s/init.c +++ b/bacnet-stack/ports/at91sam7s/init.c @@ -20,9 +20,12 @@ // The following functions must be write in ARM mode this function called directly // by exception vector -extern void AT91F_Spurious_handler(void); -extern void AT91F_Default_IRQ_handler(void); -extern void AT91F_Default_FIQ_handler(void); +extern void AT91F_Spurious_handler( + void); +extern void AT91F_Default_IRQ_handler( + void); +extern void AT91F_Default_FIQ_handler( + void); //*---------------------------------------------------------------------------- //* \fn AT91F_LowLevelInit @@ -30,31 +33,33 @@ extern void AT91F_Default_FIQ_handler(void); //* this function can be use a Stack, depending the compilation //* optimization mode //*---------------------------------------------------------------------------- -void LowLevelInit(void) +void LowLevelInit( + void) { - int i; - AT91PS_PMC pPMC = AT91C_BASE_PMC; - + int i; + AT91PS_PMC pPMC = AT91C_BASE_PMC; + //* Set Flash Wait sate // Single Cycle Access at Up to 30 MHz, or 40 // if MCK = 48054841 I have 50 Cycle for 1 usecond ( flied MC_FMR->FMCN // result: AT91C_MC_FMR = 0x00320100 (MC Flash Mode Register) - AT91C_BASE_MC->MC_FMR = (((AT91C_MC_FMCN) & (50 <<16)) | AT91C_MC_FWS_1FWS); + AT91C_BASE_MC->MC_FMR = + (((AT91C_MC_FMCN) & (50 << 16)) | AT91C_MC_FWS_1FWS); //* Watchdog Disable // result: AT91C_WDTC_WDMR = 0x00008000 (Watchdog Mode Register) - AT91C_BASE_WDTC->WDTC_WDMR= AT91C_WDTC_WDDIS; + AT91C_BASE_WDTC->WDTC_WDMR = AT91C_WDTC_WDDIS; //* Set MCK at 48 054 841 // 1 Enabling the Main Oscillator: // SCK = 1/32768 = 30.51 uSecond // Start up time = 8 * 6 / SCK = 56 * 30.51 = 1,46484375 ms // result: AT91C_CKGR_MOR = 0x00000601 (Main Oscillator Register) - pPMC->PMC_MOR = ((AT91C_CKGR_OSCOUNT & (0x06<<8)) | AT91C_CKGR_MOSCEN); - + pPMC->PMC_MOR = ((AT91C_CKGR_OSCOUNT & (0x06 << 8)) | AT91C_CKGR_MOSCEN); + // Wait the startup time - while(!(pPMC->PMC_SR & AT91C_PMC_MOSCS)); - + while (!(pPMC->PMC_SR & AT91C_PMC_MOSCS)); + // PMC Clock Generator PLL Register setup // // The following settings are used: DIV = 14 @@ -73,12 +78,11 @@ void LowLevelInit(void) // OUT = 0 (not used) // result: AT91C_CKGR_PLLR = 0x00000000480A0E (PLL Register) pPMC->PMC_PLLR = ((AT91C_CKGR_DIV & 14) | - (AT91C_CKGR_PLLCOUNT & (10<<8)) | - (AT91C_CKGR_MUL & (72<<16))); + (AT91C_CKGR_PLLCOUNT & (10 << 8)) | (AT91C_CKGR_MUL & (72 << 16))); // Wait the startup time (until PMC Status register LOCK bit is set) - while(!(pPMC->PMC_SR & AT91C_PMC_LOCK)); - + while (!(pPMC->PMC_SR & AT91C_PMC_LOCK)); + // PMC Master Clock (MCK) Register setup // // CSS = 3 (PLLCK clock selected) @@ -88,14 +92,11 @@ void LowLevelInit(void) // Note: Master Clock MCK = 48054841 hz (this is the CPU clock speed) // result: AT91C_PMC_MCKR = 0x00000007 (Master Clock Register) pPMC->PMC_MCKR = AT91C_PMC_CSS_PLL_CLK | AT91C_PMC_PRES_CLK_2; - + // Set up the default interrupts handler vectors AT91C_BASE_AIC->AIC_SVR[0] = (int) AT91F_Default_FIQ_handler; - for (i = 1; i < 31; i++) - { + for (i = 1; i < 31; i++) { AT91C_BASE_AIC->AIC_SVR[i] = (int) AT91F_Default_IRQ_handler; } - AT91C_BASE_AIC->AIC_SPU = (int) AT91F_Spurious_handler; + AT91C_BASE_AIC->AIC_SPU = (int) AT91F_Spurious_handler; } - - diff --git a/bacnet-stack/ports/at91sam7s/isr.c b/bacnet-stack/ports/at91sam7s/isr.c index b65041b3..de907a78 100644 --- a/bacnet-stack/ports/at91sam7s/isr.c +++ b/bacnet-stack/ports/at91sam7s/isr.c @@ -21,19 +21,24 @@ #define FIQ_MASK 0x00000040 #define INT_MASK (IRQ_MASK | FIQ_MASK) -static inline unsigned __get_cpsr(void) +static inline unsigned __get_cpsr( + void) { unsigned long retval; - asm volatile (" mrs %0, cpsr" : "=r" (retval) : /* no inputs */ ); + asm volatile ( + " mrs %0, cpsr":"=r" (retval): /* no inputs */ ); return retval; } -static inline void __set_cpsr(unsigned val) +static inline void __set_cpsr( + unsigned val) { - asm volatile (" msr cpsr, %0" : /* no outputs */ : "r" (val) ); + asm volatile ( + " msr cpsr, %0": /* no outputs */ :"r" (val)); } -unsigned disableIRQ(void) +unsigned disableIRQ( + void) { unsigned _cpsr; _cpsr = __get_cpsr(); @@ -41,7 +46,8 @@ unsigned disableIRQ(void) return _cpsr; } -unsigned restoreIRQ(unsigned oldCPSR) +unsigned restoreIRQ( + unsigned oldCPSR) { unsigned _cpsr; @@ -50,7 +56,8 @@ unsigned restoreIRQ(unsigned oldCPSR) return _cpsr; } -unsigned enableIRQ(void) +unsigned enableIRQ( + void) { unsigned _cpsr; @@ -59,7 +66,8 @@ unsigned enableIRQ(void) return _cpsr; } -unsigned disableFIQ(void) +unsigned disableFIQ( + void) { unsigned _cpsr; @@ -68,7 +76,8 @@ unsigned disableFIQ(void) return _cpsr; } -unsigned restoreFIQ(unsigned oldCPSR) +unsigned restoreFIQ( + unsigned oldCPSR) { unsigned _cpsr; @@ -77,7 +86,8 @@ unsigned restoreFIQ(unsigned oldCPSR) return _cpsr; } -unsigned enableFIQ(void) +unsigned enableFIQ( + void) { unsigned _cpsr; diff --git a/bacnet-stack/ports/at91sam7s/main.c b/bacnet-stack/ports/at91sam7s/main.c index da05fb8f..3a3ce7e7 100644 --- a/bacnet-stack/ports/at91sam7s/main.c +++ b/bacnet-stack/ports/at91sam7s/main.c @@ -48,9 +48,12 @@ // ******************************************************* // FIXME: use header files? External References // ******************************************************* -extern void LowLevelInit(void); -extern unsigned enableIRQ(void); -extern unsigned enableFIQ(void); +extern void LowLevelInit( + void); +extern unsigned enableIRQ( + void); +extern unsigned enableFIQ( + void); // ******************************************************* // FIXME: use header files? Global Variables @@ -64,7 +67,8 @@ static unsigned long LED_Timer_3 = 0; static unsigned long LED_Timer_4 = 1000; static unsigned long DCC_Timer = 1000; -static void millisecond_timer(void) +static void millisecond_timer( + void) { while (Timer_Milliseconds) { Timer_Milliseconds--; @@ -87,11 +91,12 @@ static void millisecond_timer(void) /* note: MS/TP silence timer is updated in ISR */ } -static void init(void) +static void init( + void) { /* Initialize the Parallel I/O Controller A Peripheral Clock */ - volatile AT91PS_PMC pPMC = AT91C_BASE_PMC; - pPMC->PMC_PCER = pPMC->PMC_PCSR | (1<PMC_PCER = pPMC->PMC_PCSR | (1 << AT91C_ID_PIOA); // Set up the LEDs (PA0 - PA3) volatile AT91PS_PIO pPIO = AT91C_BASE_PIOA; @@ -112,23 +117,23 @@ static void init(void) volatile AT91PS_AIC pAIC = AT91C_BASE_AIC; // Disable FIQ interrupt in // AIC Interrupt Disable Command Register - pAIC->AIC_IDCR = (1<AIC_IDCR = (1 << AT91C_ID_FIQ); // Set the interrupt source type in // AIC Source Mode Register[0] - pAIC->AIC_SMR[AT91C_ID_FIQ] = - (AT91C_AIC_SRCTYPE_INT_EDGE_TRIGGERED); + pAIC->AIC_SMR[AT91C_ID_FIQ] = (AT91C_AIC_SRCTYPE_INT_EDGE_TRIGGERED); // Clear the FIQ interrupt in // AIC Interrupt Clear Command Register - pAIC->AIC_ICCR = (1<AIC_ICCR = (1 << AT91C_ID_FIQ); // Remove disable FIQ interrupt in // AIC Interrupt Disable Command Register - pAIC->AIC_IDCR = (0<AIC_IDCR = (0 << AT91C_ID_FIQ); // Enable the FIQ interrupt in // AIC Interrupt Enable Command Register - pAIC->AIC_IECR = (1<AIC_IECR = (1 << AT91C_ID_FIQ); } -static void bacnet_init(void) +static void bacnet_init( + void) { #if defined(BACDL_MSTP) RS485_Set_Baud_Rate(38400); @@ -140,8 +145,7 @@ static void bacnet_init(void) Device_Set_Object_Instance_Number(22222); #ifndef DLMSTP_TEST /* we need to handle who-is to support dynamic device binding */ - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, - handler_who_is); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is); /* Set the handlers for any confirmed services that we support. */ /* We must implement read property - it's required! */ apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY, @@ -157,14 +161,16 @@ static void bacnet_init(void) #endif } -int main (void) { - unsigned long IdleCount = 0; // idle loop blink counter +int main( + void) +{ + unsigned long IdleCount = 0; // idle loop blink counter bool LED1_Off_Enabled = true; bool LED2_Off_Enabled = true; bool LED3_Off_Enabled = true; uint16_t pdu_len = 0; BACNET_ADDRESS src; /* source address */ - uint8_t pdu[MAX_MPDU]; /* PDU data */ + uint8_t pdu[MAX_MPDU]; /* PDU data */ // Set up the LEDs (PA0 - PA3) volatile AT91PS_PIO pPIO = AT91C_BASE_PIOA; @@ -177,9 +183,9 @@ int main (void) { // enable interrupts enableIRQ(); enableFIQ(); - /* broadcast an I-Am on startup */ + /* broadcast an I-Am on startup */ iam_send(&Handler_Transmit_Buffer[0]); - // endless blink loop + // endless blink loop while (1) { millisecond_timer(); if (!DCC_Timer) { @@ -187,8 +193,7 @@ int main (void) { DCC_Timer = 1000; } /* USART Tx turns the LED on, we turn it off */ - if (((pPIO->PIO_ODSR & LED1) == LED1) && (LED1_Off_Enabled)) - { + if (((pPIO->PIO_ODSR & LED1) == LED1) && (LED1_Off_Enabled)) { LED1_Off_Enabled = false; /* wait */ LED_Timer_1 = 20; @@ -199,8 +204,7 @@ int main (void) { LED1_Off_Enabled = true; } /* USART Rx turns the LED on, we turn it off */ - if (((pPIO->PIO_ODSR & LED2) == LED2) && (LED2_Off_Enabled)) - { + if (((pPIO->PIO_ODSR & LED2) == LED2) && (LED2_Off_Enabled)) { LED2_Off_Enabled = false; /* wait */ LED_Timer_2 = 20; @@ -211,8 +215,7 @@ int main (void) { LED2_Off_Enabled = true; } /* switch or NPDU turns on the LED, we turn it off */ - if (((pPIO->PIO_ODSR & LED3) == LED3) && (LED3_Off_Enabled)) - { + if (((pPIO->PIO_ODSR & LED3) == LED3) && (LED3_Off_Enabled)) { LED3_Off_Enabled = false; /* wait */ LED_Timer_3 = 500; @@ -224,7 +227,7 @@ int main (void) { } /* Blink LED every second */ if (!LED_Timer_4) { - if ((pPIO->PIO_ODSR & LED4) == LED4) { + if ((pPIO->PIO_ODSR & LED4) == LED4) { /* turn on */ pPIO->PIO_CODR = LED4; } else { diff --git a/bacnet-stack/ports/at91sam7s/rs485.c b/bacnet-stack/ports/at91sam7s/rs485.c index 4ee1681c..4e4da882 100644 --- a/bacnet-stack/ports/at91sam7s/rs485.c +++ b/bacnet-stack/ports/at91sam7s/rs485.c @@ -62,52 +62,51 @@ static int RS485_Baud = 38400; * ALGORITHM: none * NOTES: none *****************************************************************************/ -void RS485_Initialize(void) +void RS485_Initialize( + void) { /* Enable the USART0 clock in the Power Management Controller */ volatile AT91PS_PMC pPMC = AT91C_BASE_PMC; - pPMC->PMC_PCER = pPMC->PMC_PCSR | (1<PMC_PCER = pPMC->PMC_PCSR | (1 << AT91C_ID_US0); /* Disable and clear USART0 interrupt in AIC Interrupt Disable Command Register */ volatile AT91PS_AIC pAIC = AT91C_BASE_AIC; - pAIC->AIC_IDCR = (1<AIC_ICCR = (1<AIC_IDCR = (1 << AT91C_ID_US0); + pAIC->AIC_ICCR = (1 << AT91C_ID_US0); + /* enable the peripheral by disabling the pin in the PIO controller */ *AT91C_PIOA_PDR = AT91C_PA5_RXD0 | AT91C_PA6_TXD0 | AT91C_PA7_RTS0; - RS485_Interface->US_CR = - AT91C_US_RSTRX | /* Reset Receiver */ - AT91C_US_RSTTX | /* Reset Transmitter */ - AT91C_US_RSTSTA | /* Clear status register */ - AT91C_US_RXDIS | /* Receiver Disable */ - AT91C_US_TXDIS; /* Transmitter Disable */ + RS485_Interface->US_CR = AT91C_US_RSTRX | /* Reset Receiver */ + AT91C_US_RSTTX | /* Reset Transmitter */ + AT91C_US_RSTSTA | /* Clear status register */ + AT91C_US_RXDIS | /* Receiver Disable */ + AT91C_US_TXDIS; /* Transmitter Disable */ - RS485_Interface->US_MR = - AT91C_US_USMODE_RS485 | /* RS-485 Mode - RTS auto assert */ - AT91C_US_CLKS_CLOCK | /* Clock = MCK */ - AT91C_US_CHRL_8_BITS | /* 8-bit Data */ - AT91C_US_PAR_NONE | /* No Parity */ - AT91C_US_NBSTOP_1_BIT; /* 1 Stop Bit */ + RS485_Interface->US_MR = AT91C_US_USMODE_RS485 | /* RS-485 Mode - RTS auto assert */ + AT91C_US_CLKS_CLOCK | /* Clock = MCK */ + AT91C_US_CHRL_8_BITS | /* 8-bit Data */ + AT91C_US_PAR_NONE | /* No Parity */ + AT91C_US_NBSTOP_1_BIT; /* 1 Stop Bit */ /* set the Time Guard to release RTS after x bit times */ RS485_Interface->US_TTGR = 1; - + /* Receiver Time-out disabled */ RS485_Interface->US_RTOR = 0; /* baud rate */ - RS485_Interface->US_BRGR = MCK/16/RS485_Baud; + RS485_Interface->US_BRGR = MCK / 16 / RS485_Baud; + + RS485_Interface->US_CR = AT91C_US_RXEN | /* Receiver Enable */ + AT91C_US_TXEN; /* Transmitter Enable */ - RS485_Interface->US_CR = - AT91C_US_RXEN | /* Receiver Enable */ - AT91C_US_TXEN; /* Transmitter Enable */ - return; } -void RS485_Cleanup(void) +void RS485_Cleanup( + void) { } @@ -118,7 +117,8 @@ void RS485_Cleanup(void) * ALGORITHM: none * NOTES: none *****************************************************************************/ -uint32_t RS485_Get_Baud_Rate(void) +uint32_t RS485_Get_Baud_Rate( + void) { return RS485_Baud; } @@ -129,7 +129,8 @@ uint32_t RS485_Get_Baud_Rate(void) * ALGORITHM: none * NOTES: none *****************************************************************************/ -bool RS485_Set_Baud_Rate(uint32_t baud) +bool RS485_Set_Baud_Rate( + uint32_t baud) { bool valid = true; @@ -141,7 +142,7 @@ bool RS485_Set_Baud_Rate(uint32_t baud) case 76800: case 115200: RS485_Baud = baud; - RS485_Interface->US_BRGR = MCK/16/baud; + RS485_Interface->US_BRGR = MCK / 16 / baud; /* FIXME: store the baud rate */ break; default: @@ -158,14 +159,15 @@ bool RS485_Set_Baud_Rate(uint32_t baud) * ALGORITHM: none * NOTES: none *****************************************************************************/ -void RS485_Turnaround_Delay(void) +void RS485_Turnaround_Delay( + void) { uint16_t turnaround_time; - + /* delay after reception before trasmitting - per MS/TP spec */ /* wait a minimum 40 bit times since reception */ /* at least 1 ms for errors: rounding, clock tick */ - turnaround_time = 1 + ((Tturnaround*1000UL)/RS485_Baud); + turnaround_time = 1 + ((Tturnaround * 1000UL) / RS485_Baud); while (Timer_Silence() < turnaround_time) { /* do nothing - wait for timer to increment */ }; @@ -177,9 +179,10 @@ void RS485_Turnaround_Delay(void) * ALGORITHM: none * NOTES: The Atmel ARM7 has an automatic enable/disable in RS485 mode. *****************************************************************************/ -void RS485_Transmitter_Enable(bool enable) +void RS485_Transmitter_Enable( + bool enable) { - (void)enable; + (void) enable; } /**************************************************************************** @@ -189,9 +192,9 @@ void RS485_Transmitter_Enable(bool enable) * NOTES: none *****************************************************************************/ void RS485_Send_Data( - uint8_t * buffer, /* data to send */ - uint16_t nbytes) /* number of bytes of data */ -{ + uint8_t * buffer, /* data to send */ + uint16_t nbytes) +{ /* number of bytes of data */ /* LED on send */ volatile AT91PS_PIO pPIO = AT91C_BASE_PIOA; /* LED ON */ @@ -218,7 +221,8 @@ void RS485_Send_Data( * ALGORITHM: none * NOTES: Clears any error flags. *****************************************************************************/ -bool RS485_ReceiveError(void) +bool RS485_ReceiveError( + void) { bool ReceiveError = false; /* LED on send */ @@ -242,25 +246,27 @@ bool RS485_ReceiveError(void) * ALGORITHM: none * NOTES: none *****************************************************************************/ -bool RS485_DataAvailable(uint8_t *DataRegister) +bool RS485_DataAvailable( + uint8_t * DataRegister) { bool DataAvailable = false; /* LED on send */ volatile AT91PS_PIO pPIO = AT91C_BASE_PIOA; - - if ( RS485_Interface->US_CSR & AT91C_US_RXRDY) { + + if (RS485_Interface->US_CSR & AT91C_US_RXRDY) { /* data is available */ *DataRegister = RS485_Interface->US_RHR; DataAvailable = true; /* LED ON */ pPIO->PIO_CODR = LED2; } - + return DataAvailable; } #ifdef TEST_RS485 -int main(void) +int main( + void) { unsigned i = 0; uint8_t DataRegister; @@ -270,11 +276,10 @@ int main(void) /* receive task */ for (;;) { if (RS485_ReceiveError()) { - fprintf(stderr,"ERROR "); + fprintf(stderr, "ERROR "); } else if (RS485_DataAvailable(&DataRegister)) { - fprintf(stderr,"%02X ",DataRegister); + fprintf(stderr, "%02X ", DataRegister); } } } -#endif - +#endif diff --git a/bacnet-stack/ports/at91sam7s/rs485.h b/bacnet-stack/ports/at91sam7s/rs485.h index afd2de4f..ae622daa 100644 --- a/bacnet-stack/ports/at91sam7s/rs485.h +++ b/bacnet-stack/ports/at91sam7s/rs485.h @@ -30,24 +30,31 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - void RS485_Initialize(void); + void RS485_Initialize( + void); - void RS485_Transmitter_Enable(bool enable); + void RS485_Transmitter_Enable( + bool enable); void RS485_Send_Data( uint8_t * buffer, /* data to send */ uint16_t nbytes); /* number of bytes of data */ - bool RS485_ReceiveError(void); - bool RS485_DataAvailable(uint8_t *data); + bool RS485_ReceiveError( + void); + bool RS485_DataAvailable( + uint8_t * data); - void RS485_Turnaround_Delay(void); - uint32_t RS485_Get_Baud_Rate(void); - bool RS485_Set_Baud_Rate(uint32_t baud); + void RS485_Turnaround_Delay( + void); + uint32_t RS485_Get_Baud_Rate( + void); + bool RS485_Set_Baud_Rate( + uint32_t baud); #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/ports/at91sam7s/timer.c b/bacnet-stack/ports/at91sam7s/timer.c index 04a33725..a18c98de 100644 --- a/bacnet-stack/ports/at91sam7s/timer.c +++ b/bacnet-stack/ports/at91sam7s/timer.c @@ -50,7 +50,9 @@ volatile unsigned long Timer_Milliseconds; /* MS/TP Silence Timer */ static volatile int SilenceTime; -static void Timer0_Setup(int milliseconds) { +static void Timer0_Setup( + int milliseconds) +{ // TC Block Control Register TC_BCR (read/write) // // |------------------------------------------------------------------|------| @@ -214,7 +216,7 @@ static void Timer0_Setup(int milliseconds) { // // TIMER_CLOCK5 = (MCK / 1024) / 1000 // = 48054841 / 1024 / 1000 = 46.928 - pTC->TC_RC = ((MCK/1024/1000)+1)*milliseconds; + pTC->TC_RC = ((MCK / 1024 / 1000) + 1) * milliseconds; // TC Interrupt Enable Register TC_IER (write-only) // @@ -297,9 +299,11 @@ static void Timer0_Setup(int milliseconds) { // Modified by Steve Karg // simplified and changed to a millisecond count-up timer // ***************************************************************************** -static void Timer0IrqHandler (void) { +static void Timer0IrqHandler( + void) +{ - volatile AT91PS_TC pTC = AT91C_BASE_TC0; // pointer to timer channel 0 register structure + volatile AT91PS_TC pTC = AT91C_BASE_TC0; // pointer to timer channel 0 register structure unsigned int dummy; // temporary // read TC0 Status Register to clear interrupt @@ -310,12 +314,14 @@ static void Timer0IrqHandler (void) { SilenceTime++; } -int Timer_Silence(void) +int Timer_Silence( + void) { return SilenceTime; } -void Timer_Silence_Reset(void) +void Timer_Silence_Reset( + void) { SilenceTime = 0; } @@ -330,33 +336,33 @@ void Timer_Silence_Reset(void) // Moved timer startup code from main // modified the peripheral clock init // ***************************************************************************** -void TimerInit(void) +void TimerInit( + void) { // enable the Timer0 peripheral clock volatile AT91PS_PMC pPMC = AT91C_BASE_PMC; - pPMC->PMC_PCER = pPMC->PMC_PCSR | (1<PMC_PCER = pPMC->PMC_PCSR | (1 << AT91C_ID_TC0); // Set up the AIC registers for Timer 0 - volatile AT91PS_AIC pAIC = AT91C_BASE_AIC; + volatile AT91PS_AIC pAIC = AT91C_BASE_AIC; // Disable timer 0 interrupt // in AIC Interrupt Disable Command Register - pAIC->AIC_IDCR = (1<AIC_IDCR = (1 << AT91C_ID_TC0); // Set the TC0 IRQ handler address in // AIC Source Vector Register[12] - pAIC->AIC_SVR[AT91C_ID_TC0] = - (unsigned int)Timer0IrqHandler; + pAIC->AIC_SVR[AT91C_ID_TC0] = (unsigned int) Timer0IrqHandler; // Set the interrupt source type and priority // in AIC Source Mode Register[12] pAIC->AIC_SMR[AT91C_ID_TC0] = - (AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE | 0x4 ); + (AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE | 0x4); // Clear the TC0 interrupt // in AIC Interrupt Clear Command Register - pAIC->AIC_ICCR = (1<AIC_ICCR = (1 << AT91C_ID_TC0); // Remove disable timer 0 interrupt // in AIC Interrupt Disable Command Reg - pAIC->AIC_IDCR = (0<AIC_IDCR = (0 << AT91C_ID_TC0); // Enable the TC0 interrupt // in AIC Interrupt Enable Command Register - pAIC->AIC_IECR = (1<AIC_IECR = (1 << AT91C_ID_TC0); // Setup timer0 to generate a 1 msec periodic interrupt Timer0_Setup(1); } diff --git a/bacnet-stack/ports/at91sam7s/timer.h b/bacnet-stack/ports/at91sam7s/timer.h index 76c3c62b..cc9da564 100644 --- a/bacnet-stack/ports/at91sam7s/timer.h +++ b/bacnet-stack/ports/at91sam7s/timer.h @@ -32,13 +32,16 @@ extern volatile unsigned long Timer_Milliseconds; #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - void TimerInit(void); - int Timer_Silence(void); - void Timer_Silence_Reset(void); + void TimerInit( + void); + int Timer_Silence( + void); + void Timer_Silence_Reset( + void); #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/ports/atmega168/ai.c b/bacnet-stack/ports/atmega168/ai.c index 399c0264..438cd2ac 100644 --- a/bacnet-stack/ports/atmega168/ai.c +++ b/bacnet-stack/ports/atmega168/ai.c @@ -36,7 +36,7 @@ /* Analog Input = Photocell */ #define MAX_ANALOG_INPUTS 9 #if (MAX_ANALOG_INPUTS > 9) - #error Modify the Analog_Input_Name to handle multiple digits +#error Modify the Analog_Input_Name to handle multiple digits #endif float Present_Value[MAX_ANALOG_INPUTS]; @@ -44,7 +44,8 @@ float Present_Value[MAX_ANALOG_INPUTS]; /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need validate that the */ /* given instance exists */ -bool Analog_Input_Valid_Instance(uint32_t object_instance) +bool Analog_Input_Valid_Instance( + uint32_t object_instance) { if (object_instance < MAX_ANALOG_INPUTS) return true; @@ -53,30 +54,34 @@ bool Analog_Input_Valid_Instance(uint32_t object_instance) } /* we simply have 0-n object instances. */ -unsigned Analog_Input_Count(void) +unsigned Analog_Input_Count( + void) { return MAX_ANALOG_INPUTS; } /* we simply have 0-n object instances. */ -uint32_t Analog_Input_Index_To_Instance(unsigned index) +uint32_t Analog_Input_Index_To_Instance( + unsigned index) { return index; } /* we simply have 0-n object instances. */ -unsigned Analog_Input_Instance_To_Index(uint32_t object_instance) +unsigned Analog_Input_Instance_To_Index( + uint32_t object_instance) { return object_instance; } -char *Analog_Input_Name(uint32_t object_instance) +char *Analog_Input_Name( + uint32_t object_instance) { - static char text_string[5] = "AI-0"; /* okay for single thread */ + static char text_string[5] = "AI-0"; /* okay for single thread */ if (object_instance < MAX_ANALOG_INPUTS) { - text_string[3] = '0' + (uint8_t)object_instance; + text_string[3] = '0' + (uint8_t) object_instance; return text_string; } @@ -85,13 +90,15 @@ char *Analog_Input_Name(uint32_t object_instance) /* return apdu length, or -1 on error */ /* assumption - object has already exists */ -int Analog_Input_Encode_Property_APDU(uint8_t * apdu, +int Analog_Input_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ BACNET_BIT_STRING bit_string; BACNET_CHARACTER_STRING char_string; unsigned object_index; @@ -99,50 +106,53 @@ int Analog_Input_Encode_Property_APDU(uint8_t * apdu, (void) array_index; switch (property) { - case PROP_OBJECT_IDENTIFIER: - apdu_len = encode_application_object_id(&apdu[0], OBJECT_ANALOG_INPUT, - object_instance); - break; - /* note: Name and Description don't have to be the same. - You could make Description writable and different. - Note that Object-Name must be unique in this device */ - case PROP_OBJECT_NAME: - case PROP_DESCRIPTION: - characterstring_init_ansi(&char_string, - Analog_Input_Name(object_instance)); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_OBJECT_TYPE: - apdu_len = encode_application_enumerated(&apdu[0], - OBJECT_ANALOG_INPUT); - break; - case PROP_PRESENT_VALUE: - object_index = Analog_Input_Instance_To_Index(object_instance); - apdu_len = encode_application_real(&apdu[0], - Present_Value[object_index]); - break; - case PROP_STATUS_FLAGS: - bitstring_init(&bit_string); - bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); - apdu_len = encode_application_bitstring(&apdu[0], &bit_string); - break; - case PROP_EVENT_STATE: - apdu_len = encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); - break; - case PROP_OUT_OF_SERVICE: - apdu_len = encode_application_boolean(&apdu[0], false); - break; - case PROP_UNITS: - apdu_len = encode_application_enumerated(&apdu[0], UNITS_PERCENT); - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_UNKNOWN_PROPERTY; - apdu_len = -1; - break; + case PROP_OBJECT_IDENTIFIER: + apdu_len = + encode_application_object_id(&apdu[0], OBJECT_ANALOG_INPUT, + object_instance); + break; + /* note: Name and Description don't have to be the same. + You could make Description writable and different. + Note that Object-Name must be unique in this device */ + case PROP_OBJECT_NAME: + case PROP_DESCRIPTION: + characterstring_init_ansi(&char_string, + Analog_Input_Name(object_instance)); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_OBJECT_TYPE: + apdu_len = encode_application_enumerated(&apdu[0], + OBJECT_ANALOG_INPUT); + break; + case PROP_PRESENT_VALUE: + object_index = Analog_Input_Instance_To_Index(object_instance); + apdu_len = encode_application_real(&apdu[0], + Present_Value[object_index]); + break; + case PROP_STATUS_FLAGS: + bitstring_init(&bit_string); + bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); + apdu_len = encode_application_bitstring(&apdu[0], &bit_string); + break; + case PROP_EVENT_STATE: + apdu_len = + encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); + break; + case PROP_OUT_OF_SERVICE: + apdu_len = encode_application_boolean(&apdu[0], false); + break; + case PROP_UNITS: + apdu_len = encode_application_enumerated(&apdu[0], UNITS_PERCENT); + break; + default: + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_UNKNOWN_PROPERTY; + apdu_len = -1; + break; } return apdu_len; diff --git a/bacnet-stack/ports/atmega168/apdu.c b/bacnet-stack/ports/atmega168/apdu.c index b03e1115..6ea7afbc 100644 --- a/bacnet-stack/ports/atmega168/apdu.c +++ b/bacnet-stack/ports/atmega168/apdu.c @@ -41,7 +41,8 @@ #include "bacenum.h" #include "handlers.h" -bool apdu_service_supported(BACNET_SERVICES_SUPPORTED service_supported) +bool apdu_service_supported( + BACNET_SERVICES_SUPPORTED service_supported) { bool status = false; @@ -57,13 +58,15 @@ bool apdu_service_supported(BACNET_SERVICES_SUPPORTED service_supported) return status; } -uint16_t apdu_decode_confirmed_service_request(uint8_t * apdu, /* APDU data */ +uint16_t apdu_decode_confirmed_service_request( + uint8_t * apdu, /* APDU data */ uint16_t apdu_len, BACNET_CONFIRMED_SERVICE_DATA * service_data, uint8_t * service_choice, - uint8_t ** service_request, uint16_t * service_request_len) + uint8_t ** service_request, + uint16_t * service_request_len) { - uint16_t len = 0; /* counts where we are in PDU */ + uint16_t len = 0; /* counts where we are in PDU */ service_data->segmented_message = (apdu[0] & BIT3) ? true : false; service_data->more_follows = (apdu[0] & BIT2) ? true : false; @@ -84,55 +87,56 @@ uint16_t apdu_decode_confirmed_service_request(uint8_t * apdu, /* APDU data */ return len; } -void apdu_handler(BACNET_ADDRESS * src, uint8_t * apdu, /* APDU data */ +void apdu_handler( + BACNET_ADDRESS * src, + uint8_t * apdu, /* APDU data */ uint16_t apdu_len) { BACNET_CONFIRMED_SERVICE_DATA service_data = { 0 }; uint8_t service_choice = 0; uint8_t *service_request = NULL; uint16_t service_request_len = 0; - uint16_t len = 0; /* counts where we are in PDU */ + uint16_t len = 0; /* counts where we are in PDU */ if (apdu) { /* PDU Type */ switch (apdu[0] & 0xF0) { - case PDU_TYPE_CONFIRMED_SERVICE_REQUEST: - len = apdu_decode_confirmed_service_request(&apdu[0], /* APDU data */ - apdu_len, - &service_data, - &service_choice, &service_request, &service_request_len); - if (service_choice == SERVICE_CONFIRMED_READ_PROPERTY) { - handler_read_property(service_request, - service_request_len, src, &service_data); - } -#if 0 - else if (service_choice == SERVICE_CONFIRMED_WRITE_PROPERTY) { - handler_write_property(service_request, - service_request_len, src, &service_data); - } + case PDU_TYPE_CONFIRMED_SERVICE_REQUEST: + len = apdu_decode_confirmed_service_request(&apdu[0], /* APDU data */ + apdu_len, + &service_data, + &service_choice, &service_request, &service_request_len); + if (service_choice == SERVICE_CONFIRMED_READ_PROPERTY) { + handler_read_property(service_request, + service_request_len, src, &service_data); + } +#if 0 + else if (service_choice == SERVICE_CONFIRMED_WRITE_PROPERTY) { + handler_write_property(service_request, + service_request_len, src, &service_data); + } #endif - else { - handler_unrecognized_service(service_request, - service_request_len, src, &service_data); - } - break; - case PDU_TYPE_UNCONFIRMED_SERVICE_REQUEST: - service_choice = apdu[1]; - service_request = &apdu[2]; - service_request_len = apdu_len - 2; - if (service_choice == SERVICE_UNCONFIRMED_WHO_IS) { - handler_who_is(service_request, - service_request_len, src); - } - break; - case PDU_TYPE_SIMPLE_ACK: - case PDU_TYPE_COMPLEX_ACK: - case PDU_TYPE_SEGMENT_ACK: - case PDU_TYPE_ERROR: - case PDU_TYPE_REJECT: - case PDU_TYPE_ABORT: - default: - break; + else { + handler_unrecognized_service(service_request, + service_request_len, src, &service_data); + } + break; + case PDU_TYPE_UNCONFIRMED_SERVICE_REQUEST: + service_choice = apdu[1]; + service_request = &apdu[2]; + service_request_len = apdu_len - 2; + if (service_choice == SERVICE_UNCONFIRMED_WHO_IS) { + handler_who_is(service_request, service_request_len, src); + } + break; + case PDU_TYPE_SIMPLE_ACK: + case PDU_TYPE_COMPLEX_ACK: + case PDU_TYPE_SEGMENT_ACK: + case PDU_TYPE_ERROR: + case PDU_TYPE_REJECT: + case PDU_TYPE_ABORT: + default: + break; } } return; diff --git a/bacnet-stack/ports/atmega168/av.c b/bacnet-stack/ports/atmega168/av.c index 151d2af7..e2d587bd 100644 --- a/bacnet-stack/ports/atmega168/av.c +++ b/bacnet-stack/ports/atmega168/av.c @@ -31,12 +31,12 @@ #include "bacdcode.h" #include "bacenum.h" #include "bacapp.h" -#include "config.h" /* the custom stuff */ +#include "config.h" /* the custom stuff */ #include "wp.h" #define MAX_ANALOG_VALUES 9 #if (MAX_ANALOG_VALUES > 9) - #error Modify the Analog_Value_Name to handle multiple digits +#error Modify the Analog_Value_Name to handle multiple digits #endif float Present_Value[MAX_ANALOG_VALUES]; @@ -44,7 +44,8 @@ float Present_Value[MAX_ANALOG_VALUES]; /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need validate that the */ /* given instance exists */ -bool Analog_Value_Valid_Instance(uint32_t object_instance) +bool Analog_Value_Valid_Instance( + uint32_t object_instance) { if (object_instance < MAX_ANALOG_VALUES) return true; @@ -54,7 +55,8 @@ bool Analog_Value_Valid_Instance(uint32_t object_instance) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then count how many you have */ -unsigned Analog_Value_Count(void) +unsigned Analog_Value_Count( + void) { return MAX_ANALOG_VALUES; } @@ -62,7 +64,8 @@ unsigned Analog_Value_Count(void) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need to return the instance */ /* that correlates to the correct index */ -uint32_t Analog_Value_Index_To_Instance(unsigned index) +uint32_t Analog_Value_Index_To_Instance( + unsigned index) { return index; } @@ -70,18 +73,20 @@ uint32_t Analog_Value_Index_To_Instance(unsigned index) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need to return the index */ /* that correlates to the correct instance number */ -unsigned Analog_Value_Instance_To_Index(uint32_t object_instance) +unsigned Analog_Value_Instance_To_Index( + uint32_t object_instance) { return object_instance; } /* note: the object name must be unique within this device */ -char *Analog_Value_Name(uint32_t object_instance) +char *Analog_Value_Name( + uint32_t object_instance) { - static char text_string[5] = "AV-0"; /* okay for single thread */ + static char text_string[5] = "AV-0"; /* okay for single thread */ if (object_instance < MAX_ANALOG_VALUES) { - text_string[3] = '0' + (uint8_t)object_instance; + text_string[3] = '0' + (uint8_t) object_instance; return text_string; } @@ -89,65 +94,74 @@ char *Analog_Value_Name(uint32_t object_instance) } /* return apdu len, or -1 on error */ -int Analog_Value_Encode_Property_APDU(uint8_t * apdu, +int Analog_Value_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ BACNET_BIT_STRING bit_string; BACNET_CHARACTER_STRING char_string; unsigned object_index; - + switch (property) { - case PROP_OBJECT_IDENTIFIER: - apdu_len = encode_application_object_id(&apdu[0], OBJECT_ANALOG_VALUE, - object_instance); - break; - case PROP_OBJECT_NAME: - case PROP_DESCRIPTION: - characterstring_init_ansi(&char_string, - Analog_Value_Name(object_instance)); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_OBJECT_TYPE: - apdu_len = encode_application_enumerated(&apdu[0], OBJECT_ANALOG_VALUE); - break; - case PROP_PRESENT_VALUE: - object_index = Analog_Value_Instance_To_Index(object_instance); - apdu_len = encode_application_real(&apdu[0], Present_Value[object_index]); - break; - case PROP_STATUS_FLAGS: - bitstring_init(&bit_string); - bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); - apdu_len = encode_application_bitstring(&apdu[0], &bit_string); - break; - case PROP_EVENT_STATE: - apdu_len = encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); - break; - case PROP_OUT_OF_SERVICE: - apdu_len = encode_application_boolean(&apdu[0], false); - break; - case PROP_UNITS: - apdu_len = encode_application_enumerated(&apdu[0], UNITS_PERCENT); - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_UNKNOWN_PROPERTY; - apdu_len = -1; - break; + case PROP_OBJECT_IDENTIFIER: + apdu_len = + encode_application_object_id(&apdu[0], OBJECT_ANALOG_VALUE, + object_instance); + break; + case PROP_OBJECT_NAME: + case PROP_DESCRIPTION: + characterstring_init_ansi(&char_string, + Analog_Value_Name(object_instance)); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_OBJECT_TYPE: + apdu_len = + encode_application_enumerated(&apdu[0], OBJECT_ANALOG_VALUE); + break; + case PROP_PRESENT_VALUE: + object_index = Analog_Value_Instance_To_Index(object_instance); + apdu_len = + encode_application_real(&apdu[0], Present_Value[object_index]); + break; + case PROP_STATUS_FLAGS: + bitstring_init(&bit_string); + bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); + apdu_len = encode_application_bitstring(&apdu[0], &bit_string); + break; + case PROP_EVENT_STATE: + apdu_len = + encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); + break; + case PROP_OUT_OF_SERVICE: + apdu_len = encode_application_boolean(&apdu[0], false); + break; + case PROP_UNITS: + apdu_len = encode_application_enumerated(&apdu[0], UNITS_PERCENT); + break; + default: + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_UNKNOWN_PROPERTY; + apdu_len = -1; + break; } return apdu_len; } /* returns true if successful */ -bool Analog_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) +bool Analog_Value_Write_Property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { bool status = false; /* return value */ unsigned int object_index = 0; @@ -166,22 +180,21 @@ bool Analog_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, /* FIXME: len < application_data_len: more data? */ /* FIXME: len == 0: unable to decode? */ switch (wp_data->object_property) { - case PROP_PRESENT_VALUE: - if (value.tag == BACNET_APPLICATION_TAG_REAL) { - object_index = - Analog_Value_Instance_To_Index(wp_data-> - object_instance); - Present_Value[object_index] = value.type.Real; - status = true; - } else { + case PROP_PRESENT_VALUE: + if (value.tag == BACNET_APPLICATION_TAG_REAL) { + object_index = + Analog_Value_Instance_To_Index(wp_data->object_instance); + Present_Value[object_index] = value.type.Real; + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; + } + break; + default: *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; - break; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + break; } return status; @@ -192,7 +205,8 @@ bool Analog_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, #include #include "ctest.h" -void testAnalog_Value(Test * pTest) +void testAnalog_Value( + Test * pTest) { uint8_t apdu[MAX_APDU] = { 0 }; int len = 0; @@ -207,8 +221,7 @@ void testAnalog_Value(Test * pTest) len = Analog_Value_Encode_Property_APDU(&apdu[0], instance, - PROP_OBJECT_IDENTIFIER, - BACNET_ARRAY_ALL, &error_class, &error_code); + PROP_OBJECT_IDENTIFIER, BACNET_ARRAY_ALL, &error_class, &error_code); ct_test(pTest, len != 0); len = decode_tag_number_and_value(&apdu[0], &tag_number, &len_value); ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_OBJECT_ID); @@ -221,7 +234,8 @@ void testAnalog_Value(Test * pTest) } #ifdef TEST_ANALOG_VALUE -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -238,5 +252,5 @@ int main(void) return 0; } -#endif /* TEST_ANALOG_VALUE */ -#endif /* TEST */ +#endif /* TEST_ANALOG_VALUE */ +#endif /* TEST */ diff --git a/bacnet-stack/ports/atmega168/avr035.h b/bacnet-stack/ports/atmega168/avr035.h index 0dd766ab..d53db995 100644 --- a/bacnet-stack/ports/atmega168/avr035.h +++ b/bacnet-stack/ports/atmega168/avr035.h @@ -15,4 +15,4 @@ #define BITMASK_FLIP(x,y) ((x) ^= (y)) #define BITMASK_CHECK(x,y) ((x) & (y)) -#endif +#endif diff --git a/bacnet-stack/ports/atmega168/device.c b/bacnet-stack/ports/atmega168/device.c index 607eaa6b..6aaaf660 100644 --- a/bacnet-stack/ports/atmega168/device.c +++ b/bacnet-stack/ports/atmega168/device.c @@ -53,7 +53,8 @@ static uint32_t Object_Instance_Number = 260001; static char Object_Name[32] = "ATmega168 Device"; static BACNET_DEVICE_STATUS System_Status = STATUS_OPERATIONAL; -void Device_Init(void) +void Device_Init( + void) { /* Reinitialize_State = REINITIALIZED_STATE_IDLE; */ /* dcc_set_status_duration(COMMUNICATION_ENABLE, 0); */ @@ -65,14 +66,16 @@ void Device_Init(void) } /* methods to manipulate the data */ -uint32_t Device_Object_Instance_Number(void) +uint32_t Device_Object_Instance_Number( + void) { return Object_Instance_Number; } -bool Device_Set_Object_Instance_Number(uint32_t object_id) +bool Device_Set_Object_Instance_Number( + uint32_t object_id) { - bool status = true; /* return value */ + bool status = true; /* return value */ if (object_id <= BACNET_MAX_INSTANCE) { Object_Instance_Number = object_id; @@ -88,21 +91,24 @@ bool Device_Set_Object_Instance_Number(uint32_t object_id) return status; } -bool Device_Valid_Object_Instance_Number(uint32_t object_id) +bool Device_Valid_Object_Instance_Number( + uint32_t object_id) { /* BACnet allows for a wildcard instance number */ return ((Object_Instance_Number == object_id) || (object_id == BACNET_MAX_INSTANCE)); } -uint16_t Device_Vendor_Identifier(void) +uint16_t Device_Vendor_Identifier( + void) { return BACNET_VENDOR_ID; } -unsigned Device_Object_List_Count(void) +unsigned Device_Object_List_Count( + void) { - unsigned count = 1; /* at least 1 for device object */ + unsigned count = 1; /* at least 1 for device object */ /* FIXME: add objects as needed */ #if 0 @@ -115,8 +121,10 @@ unsigned Device_Object_List_Count(void) return count; } -bool Device_Object_List_Identifier(unsigned array_index, - int *object_type, uint32_t * instance) +bool Device_Object_List_Identifier( + unsigned array_index, + int *object_type, + uint32_t * instance) { bool status = false; unsigned object_index = 0; @@ -185,18 +193,20 @@ bool Device_Object_List_Identifier(unsigned array_index, } } #endif - + return status; } /* return the length of the apdu encoded or -1 for error */ -int Device_Encode_Property_APDU(uint8_t * apdu, +int Device_Encode_Property_APDU( + uint8_t * apdu, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { - int apdu_len = 0; /* return value */ - int len = 0; /* apdu len intermediate value */ + int apdu_len = 0; /* return value */ + int len = 0; /* apdu len intermediate value */ BACNET_BIT_STRING bit_string; BACNET_CHARACTER_STRING char_string; unsigned i = 0; @@ -206,168 +216,175 @@ int Device_Encode_Property_APDU(uint8_t * apdu, /* FIXME: change the hardcoded names to suit your application */ switch (property) { - case PROP_OBJECT_IDENTIFIER: - apdu_len = encode_application_object_id(&apdu[0], OBJECT_DEVICE, - Object_Instance_Number); - break; - case PROP_OBJECT_NAME: - case PROP_DESCRIPTION: - characterstring_init_ansi(&char_string, Object_Name); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_OBJECT_TYPE: - apdu_len = encode_application_enumerated(&apdu[0], OBJECT_DEVICE); - break; - case PROP_SYSTEM_STATUS: - apdu_len = - encode_application_enumerated(&apdu[0], System_Status); - break; - case PROP_VENDOR_NAME: - characterstring_init_ansi(&char_string, BACNET_VENDOR_NAME); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_VENDOR_IDENTIFIER: - apdu_len = - encode_application_unsigned(&apdu[0], Device_Vendor_Identifier()); - break; - case PROP_MODEL_NAME: - characterstring_init_ansi(&char_string, "GNU Demo"); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_FIRMWARE_REVISION: - characterstring_init_ansi(&char_string, BACnet_Version); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_APPLICATION_SOFTWARE_VERSION: - characterstring_init_ansi(&char_string, "1.0"); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_PROTOCOL_VERSION: - apdu_len = encode_application_unsigned(&apdu[0], 1); - break; - case PROP_PROTOCOL_REVISION: - apdu_len = encode_application_unsigned(&apdu[0], 5); - break; - case PROP_PROTOCOL_SERVICES_SUPPORTED: - /* Note: list of services that are executed, not initiated. */ - bitstring_init(&bit_string); - for (i = 0; i < MAX_BACNET_SERVICES_SUPPORTED; i++) { - /* automatic lookup based on handlers set */ - bitstring_set_bit(&bit_string, (uint8_t) i, - apdu_service_supported(i)); - } - apdu_len = encode_application_bitstring(&apdu[0], &bit_string); - break; - case PROP_PROTOCOL_OBJECT_TYPES_SUPPORTED: - /* Note: this is the list of objects that can be in this device, - not a list of objects that this device can access */ - bitstring_init(&bit_string); - /* must have the bit string as big as it can be */ - for (i = 0; i < MAX_ASHRAE_OBJECT_TYPE; i++) { - /* initialize all the object types to not-supported */ - bitstring_set_bit(&bit_string, (uint8_t) i, false); - } - /* FIXME: indicate the objects that YOU support */ - bitstring_set_bit(&bit_string, OBJECT_DEVICE, true); - bitstring_set_bit(&bit_string, OBJECT_ANALOG_VALUE, true); + case PROP_OBJECT_IDENTIFIER: + apdu_len = encode_application_object_id(&apdu[0], OBJECT_DEVICE, + Object_Instance_Number); + break; + case PROP_OBJECT_NAME: + case PROP_DESCRIPTION: + characterstring_init_ansi(&char_string, Object_Name); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_OBJECT_TYPE: + apdu_len = encode_application_enumerated(&apdu[0], OBJECT_DEVICE); + break; + case PROP_SYSTEM_STATUS: + apdu_len = encode_application_enumerated(&apdu[0], System_Status); + break; + case PROP_VENDOR_NAME: + characterstring_init_ansi(&char_string, BACNET_VENDOR_NAME); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_VENDOR_IDENTIFIER: + apdu_len = + encode_application_unsigned(&apdu[0], + Device_Vendor_Identifier()); + break; + case PROP_MODEL_NAME: + characterstring_init_ansi(&char_string, "GNU Demo"); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_FIRMWARE_REVISION: + characterstring_init_ansi(&char_string, BACnet_Version); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_APPLICATION_SOFTWARE_VERSION: + characterstring_init_ansi(&char_string, "1.0"); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_PROTOCOL_VERSION: + apdu_len = encode_application_unsigned(&apdu[0], 1); + break; + case PROP_PROTOCOL_REVISION: + apdu_len = encode_application_unsigned(&apdu[0], 5); + break; + case PROP_PROTOCOL_SERVICES_SUPPORTED: + /* Note: list of services that are executed, not initiated. */ + bitstring_init(&bit_string); + for (i = 0; i < MAX_BACNET_SERVICES_SUPPORTED; i++) { + /* automatic lookup based on handlers set */ + bitstring_set_bit(&bit_string, (uint8_t) i, + apdu_service_supported(i)); + } + apdu_len = encode_application_bitstring(&apdu[0], &bit_string); + break; + case PROP_PROTOCOL_OBJECT_TYPES_SUPPORTED: + /* Note: this is the list of objects that can be in this device, + not a list of objects that this device can access */ + bitstring_init(&bit_string); + /* must have the bit string as big as it can be */ + for (i = 0; i < MAX_ASHRAE_OBJECT_TYPE; i++) { + /* initialize all the object types to not-supported */ + bitstring_set_bit(&bit_string, (uint8_t) i, false); + } + /* FIXME: indicate the objects that YOU support */ + bitstring_set_bit(&bit_string, OBJECT_DEVICE, true); + bitstring_set_bit(&bit_string, OBJECT_ANALOG_VALUE, true); #if 0 - bitstring_set_bit(&bit_string, OBJECT_ANALOG_INPUT, true); - bitstring_set_bit(&bit_string, OBJECT_BINARY_VALUE, true); - bitstring_set_bit(&bit_string, OBJECT_BINARY_INPUT, true); + bitstring_set_bit(&bit_string, OBJECT_ANALOG_INPUT, true); + bitstring_set_bit(&bit_string, OBJECT_BINARY_VALUE, true); + bitstring_set_bit(&bit_string, OBJECT_BINARY_INPUT, true); #endif - apdu_len = encode_application_bitstring(&apdu[0], &bit_string); - break; - case PROP_OBJECT_LIST: - count = Device_Object_List_Count(); - /* Array element zero is the number of objects in the list */ - if (array_index == 0) - apdu_len = encode_application_unsigned(&apdu[0], count); - /* if no index was specified, then try to encode the entire list */ - /* into one packet. Note that more than likely you will have */ - /* to return an error if the number of encoded objects exceeds */ - /* your maximum APDU size. */ - else if (array_index == BACNET_ARRAY_ALL) { - for (i = 1; i <= count; i++) { - if (Device_Object_List_Identifier(i, &object_type, - &instance)) { - len = - encode_application_object_id(&apdu[apdu_len], - object_type, instance); - apdu_len += len; - /* assume next one is the same size as this one */ - /* can we all fit into the APDU? */ - if ((apdu_len + len) >= MAX_APDU) { + apdu_len = encode_application_bitstring(&apdu[0], &bit_string); + break; + case PROP_OBJECT_LIST: + count = Device_Object_List_Count(); + /* Array element zero is the number of objects in the list */ + if (array_index == 0) + apdu_len = encode_application_unsigned(&apdu[0], count); + /* if no index was specified, then try to encode the entire list */ + /* into one packet. Note that more than likely you will have */ + /* to return an error if the number of encoded objects exceeds */ + /* your maximum APDU size. */ + else if (array_index == BACNET_ARRAY_ALL) { + for (i = 1; i <= count; i++) { + if (Device_Object_List_Identifier(i, &object_type, + &instance)) { + len = + encode_application_object_id(&apdu[apdu_len], + object_type, instance); + apdu_len += len; + /* assume next one is the same size as this one */ + /* can we all fit into the APDU? */ + if ((apdu_len + len) >= MAX_APDU) { + *error_class = ERROR_CLASS_SERVICES; + *error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT; + apdu_len = -1; + break; + } + } else { + /* error: internal error? */ *error_class = ERROR_CLASS_SERVICES; - *error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT; + *error_code = ERROR_CODE_OTHER; apdu_len = -1; break; } - } else { - /* error: internal error? */ - *error_class = ERROR_CLASS_SERVICES; - *error_code = ERROR_CODE_OTHER; + } + } else { + if (Device_Object_List_Identifier(array_index, &object_type, + &instance)) + apdu_len = + encode_application_object_id(&apdu[0], object_type, + instance); + else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_ARRAY_INDEX; apdu_len = -1; - break; } } - } else { - if (Device_Object_List_Identifier(array_index, &object_type, - &instance)) - apdu_len = - encode_application_object_id(&apdu[0], object_type, - instance); - else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_ARRAY_INDEX; - apdu_len = -1; - } - } - break; - case PROP_MAX_APDU_LENGTH_ACCEPTED: - apdu_len = encode_application_unsigned(&apdu[0], - MAX_APDU); - break; - case PROP_SEGMENTATION_SUPPORTED: - apdu_len = encode_application_enumerated(&apdu[0], - SEGMENTATION_NONE); - break; - case PROP_APDU_TIMEOUT: - apdu_len = encode_application_unsigned(&apdu[0], 60000); - break; - case PROP_NUMBER_OF_APDU_RETRIES: - apdu_len = - encode_application_unsigned(&apdu[0], 0); - break; - case PROP_DEVICE_ADDRESS_BINDING: - /* FIXME: encode the list here, if it exists */ - break; - case PROP_DATABASE_REVISION: - apdu_len = - encode_application_unsigned(&apdu[0], 0); - break; - case PROP_MAX_INFO_FRAMES: - apdu_len = - encode_application_unsigned(&apdu[0], dlmstp_max_info_frames()); - break; - case PROP_MAX_MASTER: - apdu_len = encode_application_unsigned(&apdu[0], dlmstp_max_master()); - break; - case 9600: - apdu_len = encode_application_unsigned(&apdu[0], RS485_Get_Baud_Rate()); - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_UNKNOWN_PROPERTY; - apdu_len = -1; - break; + break; + case PROP_MAX_APDU_LENGTH_ACCEPTED: + apdu_len = encode_application_unsigned(&apdu[0], MAX_APDU); + break; + case PROP_SEGMENTATION_SUPPORTED: + apdu_len = encode_application_enumerated(&apdu[0], + SEGMENTATION_NONE); + break; + case PROP_APDU_TIMEOUT: + apdu_len = encode_application_unsigned(&apdu[0], 60000); + break; + case PROP_NUMBER_OF_APDU_RETRIES: + apdu_len = encode_application_unsigned(&apdu[0], 0); + break; + case PROP_DEVICE_ADDRESS_BINDING: + /* FIXME: encode the list here, if it exists */ + break; + case PROP_DATABASE_REVISION: + apdu_len = encode_application_unsigned(&apdu[0], 0); + break; + case PROP_MAX_INFO_FRAMES: + apdu_len = + encode_application_unsigned(&apdu[0], + dlmstp_max_info_frames()); + break; + case PROP_MAX_MASTER: + apdu_len = + encode_application_unsigned(&apdu[0], dlmstp_max_master()); + break; + case 9600: + apdu_len = + encode_application_unsigned(&apdu[0], RS485_Get_Baud_Rate()); + break; + default: + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_UNKNOWN_PROPERTY; + apdu_len = -1; + break; } return apdu_len; } #if 0 -bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) +bool Device_Write_Property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { bool status = false; /* return value */ int len = 0; @@ -384,98 +401,98 @@ bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, /* FIXME: len < application_data_len: more data? */ /* FIXME: len == 0: unable to decode? */ switch (wp_data->object_property) { - case PROP_OBJECT_IDENTIFIER: - if (value.tag == BACNET_APPLICATION_TAG_OBJECT_ID) { - if ((value.type.Object_Id.type == OBJECT_DEVICE) && - (Device_Set_Object_Instance_Number(value.type. - Object_Id.instance))) { - /* we could send an I-Am broadcast to let the world know */ - status = true; - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; - } - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case PROP_MAX_INFO_FRAMES: - if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { - if (value.type.Unsigned_Int <= 255) { - dlmstp_set_max_info_frames(value.type.Unsigned_Int); - status = true; - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; - } - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case PROP_MAX_MASTER: - if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { - if ((value.type.Unsigned_Int > 0) && - (value.type.Unsigned_Int <= 127)) { - dlmstp_set_max_master(value.type.Unsigned_Int); - status = true; - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; - } - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case PROP_OBJECT_NAME: - if (value.tag == BACNET_APPLICATION_TAG_CHARACTER_STRING) { - uint8_t encoding; - size_t len; - - encoding = - characterstring_encoding(&value.type.Character_String); - len = characterstring_length(&value.type.Character_String); - if (encoding == CHARACTER_ANSI_X34) { - if (len <= 20) { - /* FIXME: set the name */ - /* Display_Set_Name( - characterstring_value(&value.type.Character_String)); */ - /* FIXME: All the object names in a device must be unique. - Disallow setting the Device Object Name to any objects in - the device. */ + case PROP_OBJECT_IDENTIFIER: + if (value.tag == BACNET_APPLICATION_TAG_OBJECT_ID) { + if ((value.type.Object_Id.type == OBJECT_DEVICE) && + (Device_Set_Object_Instance_Number(value.type. + Object_Id.instance))) { + /* we could send an I-Am broadcast to let the world know */ + status = true; } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_NO_SPACE_TO_WRITE_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; } } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_CHARACTER_SET_NOT_SUPPORTED; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case 9600: - if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { - if (value.type.Unsigned_Int > 115200) { - RS485_Set_Baud_Rate(value.type.Unsigned_Int); - status = true; + break; + case PROP_MAX_INFO_FRAMES: + if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { + if (value.type.Unsigned_Int <= 255) { + dlmstp_set_max_info_frames(value.type.Unsigned_Int); + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } - } else { + break; + case PROP_MAX_MASTER: + if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { + if ((value.type.Unsigned_Int > 0) && + (value.type.Unsigned_Int <= 127)) { + dlmstp_set_max_master(value.type.Unsigned_Int); + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; + } + break; + case PROP_OBJECT_NAME: + if (value.tag == BACNET_APPLICATION_TAG_CHARACTER_STRING) { + uint8_t encoding; + size_t len; + + encoding = + characterstring_encoding(&value.type.Character_String); + len = characterstring_length(&value.type.Character_String); + if (encoding == CHARACTER_ANSI_X34) { + if (len <= 20) { + /* FIXME: set the name */ + /* Display_Set_Name( + characterstring_value(&value.type.Character_String)); */ + /* FIXME: All the object names in a device must be unique. + Disallow setting the Device Object Name to any objects in + the device. */ + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_NO_SPACE_TO_WRITE_PROPERTY; + } + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_CHARACTER_SET_NOT_SUPPORTED; + } + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; + } + break; + case 9600: + if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { + if (value.type.Unsigned_Int > 115200) { + RS485_Set_Baud_Rate(value.type.Unsigned_Int); + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; + } + break; + default: *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; - break; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + break; } return status; diff --git a/bacnet-stack/ports/atmega168/dlmstp.c b/bacnet-stack/ports/atmega168/dlmstp.c index 4c59f1e3..931d6eb6 100644 --- a/bacnet-stack/ports/atmega168/dlmstp.c +++ b/bacnet-stack/ports/atmega168/dlmstp.c @@ -236,21 +236,25 @@ static uint8_t TransmitPacketDest; /* we need to be able to increment without rolling over */ #define INCREMENT_AND_LIMIT_UINT8(x) {if (x < 0xFF) x++;} -bool dlmstp_init(char *ifname) +bool dlmstp_init( + char *ifname) { ifname = ifname; /* initialize hardware */ RS485_Initialize(); - + return true; } -void dlmstp_cleanup(void) +void dlmstp_cleanup( + void) { /* nothing to do for static buffers */ } -void dlmstp_fill_bacnet_address(BACNET_ADDRESS * src, uint8_t mstp_address) +void dlmstp_fill_bacnet_address( + BACNET_ADDRESS * src, + uint8_t mstp_address) { int i = 0; @@ -277,7 +281,8 @@ void dlmstp_fill_bacnet_address(BACNET_ADDRESS * src, uint8_t mstp_address) extern bool Send_I_Am; /* look at any of the unconfirmed message bits and encode if set */ -static uint16_t dlmstp_encode_unconfirmed_frame(void) +static uint16_t dlmstp_encode_unconfirmed_frame( + void) { BACNET_ADDRESS dest; BACNET_NPDU_DATA npdu_data; @@ -286,12 +291,9 @@ static uint16_t dlmstp_encode_unconfirmed_frame(void) if (Send_I_Am) { Send_I_Am = false; TransmitPacket = Handler_Transmit_Buffer; - len = iam_encode_pdu( - &TransmitPacket[0], - &dest, - &npdu_data); + len = iam_encode_pdu(&TransmitPacket[0], &dest, &npdu_data); } - + return len; } @@ -309,17 +311,17 @@ static uint16_t dlmstp_encode_unconfirmed_frame(void) /* least significant octet first */ /* (pad): (optional) at most one octet of padding: X'FF' */ static void MSTP_Send_Frame( - uint8_t frame_type, /* type of frame to send - see defines */ + uint8_t frame_type, /* type of frame to send - see defines */ uint8_t destination, /* destination address */ - uint8_t source, /* source address */ - uint8_t * pdu, /* any data to be sent - may be null */ + uint8_t source, /* source address */ + uint8_t * pdu, /* any data to be sent - may be null */ uint16_t pdu_len) -{ /* number of bytes of data (up to 501) */ +{ /* number of bytes of data (up to 501) */ uint8_t crc8 = 0xFF; /* used to calculate the crc value */ uint16_t crc16 = 0xFFFF; /* used to calculate the crc value */ - uint8_t buffer[8]; /* stores the header and crc */ + uint8_t buffer[8]; /* stores the header and crc */ uint8_t datacrc[2]; /* stores the data crc */ - uint16_t i = 0; /* used to calculate CRC for data */ + uint16_t i = 0; /* used to calculate CRC for data */ /* create the MS/TP header */ buffer[0] = 0x55; @@ -337,8 +339,7 @@ static void MSTP_Send_Frame( buffer[7] = ~crc8; if (pdu_len) { /* calculate CRC for any data */ - for (i = 0; i < pdu_len; i++) - { + for (i = 0; i < pdu_len; i++) { crc16 = CRC_Calc_Data(pdu[i], crc16); } crc16 = ~crc16; @@ -348,7 +349,7 @@ static void MSTP_Send_Frame( /* now transmit the frame */ RS485_Turnaround_Delay(); RS485_Transmitter_Enable(true); - RS485_Send_Data(buffer,8); + RS485_Send_Data(buffer, 8); /* send any data */ if (pdu_len) { RS485_Send_Data(pdu, pdu_len); @@ -357,7 +358,8 @@ static void MSTP_Send_Frame( RS485_Transmitter_Enable(false); } -static void MSTP_Receive_Frame_FSM(void) +static void MSTP_Receive_Frame_FSM( + void) { /* stores the latest received data octet */ uint8_t DataRegister = 0; @@ -468,7 +470,7 @@ static void MSTP_Receive_Frame_FSM(void) /* HeaderCRC */ HeaderCRC = CRC_Calc_Header(DataRegister, HeaderCRC); /* In the HEADER_CRC state, the node validates the CRC - on the fixed message header. */ + on the fixed message header. */ if (HeaderCRC != 0x55) { /* BadCRC */ /* indicate that an error has occurred during @@ -478,11 +480,10 @@ static void MSTP_Receive_Frame_FSM(void) Receive_State = MSTP_RECEIVE_STATE_IDLE; } else { /* Note: proposed change to BACnet MSTP state machine! - If we don't decode data that is not for us, we could - get confused about the start if the Preamble 55 FF - is part of the data. */ - if ((DataLength) && - (DataLength <= InputBufferSize)) { + If we don't decode data that is not for us, we could + get confused about the start if the Preamble 55 FF + is part of the data. */ + if ((DataLength) && (DataLength <= InputBufferSize)) { /* Data */ Index = 0; DataCRC = 0xFFFF; @@ -491,11 +492,12 @@ static void MSTP_Receive_Frame_FSM(void) } else { if (DataLength == 0) { /* NoData */ - if ((DestinationAddress == This_Station) || - (DestinationAddress == MSTP_BROADCAST_ADDRESS)) { + if ((DestinationAddress == This_Station) || + (DestinationAddress == + MSTP_BROADCAST_ADDRESS)) { /* ForUs */ /* indicate that a frame with - no data has been received */ + no data has been received */ MSTP_Flag.ReceivedValidFrame = true; } else { /* NotForUs - drop */ @@ -554,7 +556,7 @@ static void MSTP_Receive_Frame_FSM(void) /* STATE DATA CRC - no need for new state */ /* indicate the complete reception of a valid frame */ if (DataCRC == 0xF0B8) { - if ((DestinationAddress == This_Station) || + if ((DestinationAddress == This_Station) || (DestinationAddress == MSTP_BROADCAST_ADDRESS)) { /* ForUs */ /* indicate that a frame with no data @@ -578,7 +580,8 @@ static void MSTP_Receive_Frame_FSM(void) } /* returns true if we need to transition immediately */ -static bool MSTP_Master_Node_FSM(void) +static bool MSTP_Master_Node_FSM( + void) { /* The number of frames sent by this node during a single token hold. */ /* When this counter reaches the value Nmax_info_frames, the node must */ @@ -613,456 +616,423 @@ static bool MSTP_Master_Node_FSM(void) next_this_station = (This_Station + 1) % (Nmax_master + 1); next_next_station = (Next_Station + 1) % (Nmax_master + 1); switch (Master_State) { - case MSTP_MASTER_STATE_INITIALIZE: - /* DoneInitializing */ - /* indicate that the next station is unknown */ - Next_Station = This_Station; - Poll_Station = This_Station; - /* cause a Poll For Master to be sent when this node first */ - /* receives the token */ - TokenCount = Npoll; - MSTP_Flag.SoleMaster = false; - Master_State = MSTP_MASTER_STATE_IDLE; - transition_now = true; - break; - case MSTP_MASTER_STATE_IDLE: - /* In the IDLE state, the node waits for a frame. */ - if (Timer_Silence() >= Tno_token) { - /* LostToken */ - /* assume that the token has been lost */ - EventCount = 0; /* Addendum 135-2004d-8 */ - Master_State = MSTP_MASTER_STATE_NO_TOKEN; + case MSTP_MASTER_STATE_INITIALIZE: + /* DoneInitializing */ + /* indicate that the next station is unknown */ + Next_Station = This_Station; + Poll_Station = This_Station; + /* cause a Poll For Master to be sent when this node first */ + /* receives the token */ + TokenCount = Npoll; + MSTP_Flag.SoleMaster = false; + Master_State = MSTP_MASTER_STATE_IDLE; transition_now = true; - } else if (MSTP_Flag.ReceivedInvalidFrame == true) { - /* ReceivedInvalidFrame */ - /* invalid frame was received */ - MSTP_Flag.ReceivedInvalidFrame = false; - /* wait for the next frame - remain in IDLE */ - } else if (MSTP_Flag.ReceivedValidFrame == true) { - switch (FrameType) { - case FRAME_TYPE_TOKEN: - /* ReceivedToken */ - /* tokens can't be broadcast */ - if (DestinationAddress == MSTP_BROADCAST_ADDRESS) - break; - MSTP_Flag.ReceivedValidFrame = false; - FrameCount = 0; - MSTP_Flag.SoleMaster = false; - Master_State = MSTP_MASTER_STATE_USE_TOKEN; - transition_now = true; - break; - case FRAME_TYPE_POLL_FOR_MASTER: - /* ReceivedPFM */ - MSTP_Send_Frame( - FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER, - SourceAddress, This_Station, - NULL, 0); - break; - case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: - /* indicate successful reception to the higher layers */ - MSTP_Flag.ReceivePacketPending = true; - break; - case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: - /* indicate successful reception to the higher layers */ - MSTP_Flag.ReceivePacketPending = true; - /* broadcast DER just remains IDLE */ - if (DestinationAddress != - MSTP_BROADCAST_ADDRESS) { - Master_State = - MSTP_MASTER_STATE_ANSWER_DATA_REQUEST; - } - break; - case FRAME_TYPE_TEST_REQUEST: - MSTP_Send_Frame( - FRAME_TYPE_TEST_RESPONSE, - SourceAddress, This_Station, - NULL, 0); - break; - case FRAME_TYPE_TEST_RESPONSE: - default: - break; - } - /* For DATA_EXPECTING_REPLY, we will keep the Rx Frame for - reference, and the flag will be cleared in the next state */ - if (Master_State != - MSTP_MASTER_STATE_ANSWER_DATA_REQUEST) { - MSTP_Flag.ReceivedValidFrame = false; - } - } - break; - /* In the USE_TOKEN state, the node is allowed to send one or */ - /* more data frames. These may be BACnet Data frames or */ - /* proprietary frames. */ - case MSTP_MASTER_STATE_USE_TOKEN: - /* Note: optimized for minimal server: - we only send unconfirmed frames when we get the token */ - /* Note: We could wait for up to Tusage_delay */ - TransmitPacketLen = dlmstp_encode_unconfirmed_frame(); - if (TransmitPacketLen) { - MSTP_Send_Frame( - FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY, - MSTP_BROADCAST_ADDRESS, - This_Station, - (uint8_t *) & TransmitPacket[0], - TransmitPacketLen); - FrameCount++; - } else { - /* NothingToSend */ - FrameCount = Nmax_info_frames; - transition_now = true; - } - Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; - break; - case MSTP_MASTER_STATE_WAIT_FOR_REPLY: - /* In the WAIT_FOR_REPLY state, the node waits for */ - /* a reply from another node. */ - if (Timer_Silence() >= Treply_timeout) { - /* ReplyTimeout */ - /* assume that the request has failed */ - FrameCount = Nmax_info_frames; - Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; - /* Any retry of the data frame shall await the next entry */ - /* to the USE_TOKEN state. (Because of the length of the timeout, */ - /* this transition will cause the token to be passed regardless */ - /* of the initial value of FrameCount.) */ - transition_now = true; - } else { - if (MSTP_Flag.ReceivedInvalidFrame == true) { - /* InvalidFrame */ - /* error in frame reception */ - MSTP_Flag.ReceivedInvalidFrame = false; - Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; + break; + case MSTP_MASTER_STATE_IDLE: + /* In the IDLE state, the node waits for a frame. */ + if (Timer_Silence() >= Tno_token) { + /* LostToken */ + /* assume that the token has been lost */ + EventCount = 0; /* Addendum 135-2004d-8 */ + Master_State = MSTP_MASTER_STATE_NO_TOKEN; transition_now = true; + } else if (MSTP_Flag.ReceivedInvalidFrame == true) { + /* ReceivedInvalidFrame */ + /* invalid frame was received */ + MSTP_Flag.ReceivedInvalidFrame = false; + /* wait for the next frame - remain in IDLE */ } else if (MSTP_Flag.ReceivedValidFrame == true) { - if (DestinationAddress == This_Station) { - /* What did we receive? */ - switch (FrameType) { - case FRAME_TYPE_REPLY_POSTPONED: - /* ReceivedReplyPostponed */ - Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; + switch (FrameType) { + case FRAME_TYPE_TOKEN: + /* ReceivedToken */ + /* tokens can't be broadcast */ + if (DestinationAddress == MSTP_BROADCAST_ADDRESS) + break; + MSTP_Flag.ReceivedValidFrame = false; + FrameCount = 0; + MSTP_Flag.SoleMaster = false; + Master_State = MSTP_MASTER_STATE_USE_TOKEN; + transition_now = true; break; - case FRAME_TYPE_TEST_RESPONSE: - Master_State = MSTP_MASTER_STATE_IDLE; + case FRAME_TYPE_POLL_FOR_MASTER: + /* ReceivedPFM */ + MSTP_Send_Frame(FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER, + SourceAddress, This_Station, NULL, 0); break; case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: - /* ReceivedReply */ - /* or a proprietary type that indicates a reply */ /* indicate successful reception to the higher layers */ MSTP_Flag.ReceivePacketPending = true; - Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; break; + case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: + /* indicate successful reception to the higher layers */ + MSTP_Flag.ReceivePacketPending = true; + /* broadcast DER just remains IDLE */ + if (DestinationAddress != MSTP_BROADCAST_ADDRESS) { + Master_State = + MSTP_MASTER_STATE_ANSWER_DATA_REQUEST; + } + break; + case FRAME_TYPE_TEST_REQUEST: + MSTP_Send_Frame(FRAME_TYPE_TEST_RESPONSE, + SourceAddress, This_Station, NULL, 0); + break; + case FRAME_TYPE_TEST_RESPONSE: default: - /* if proprietary frame was expected, you might - need to transition to DONE WITH TOKEN */ - Master_State = MSTP_MASTER_STATE_IDLE; break; - } - } else { - /* ReceivedUnexpectedFrame */ - /* an unexpected frame was received */ - /* This may indicate the presence of multiple tokens */ - /* or a device that didn't see activity after passing */ - /* a token (how lame!). */ - /* Synchronize with the network. */ - /* This action drops the token. */ - Master_State = MSTP_MASTER_STATE_IDLE; } - MSTP_Flag.ReceivedValidFrame = false; + /* For DATA_EXPECTING_REPLY, we will keep the Rx Frame for + reference, and the flag will be cleared in the next state */ + if (Master_State != MSTP_MASTER_STATE_ANSWER_DATA_REQUEST) { + MSTP_Flag.ReceivedValidFrame = false; + } + } + break; + /* In the USE_TOKEN state, the node is allowed to send one or */ + /* more data frames. These may be BACnet Data frames or */ + /* proprietary frames. */ + case MSTP_MASTER_STATE_USE_TOKEN: + /* Note: optimized for minimal server: + we only send unconfirmed frames when we get the token */ + /* Note: We could wait for up to Tusage_delay */ + TransmitPacketLen = dlmstp_encode_unconfirmed_frame(); + if (TransmitPacketLen) { + MSTP_Send_Frame(FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY, + MSTP_BROADCAST_ADDRESS, + This_Station, + (uint8_t *) & TransmitPacket[0], TransmitPacketLen); + FrameCount++; + } else { + /* NothingToSend */ + FrameCount = Nmax_info_frames; transition_now = true; } - } - break; - /* The DONE_WITH_TOKEN state either sends another data frame, */ - /* passes the token, or initiates a Poll For Master cycle. */ - case MSTP_MASTER_STATE_DONE_WITH_TOKEN: - /* SendAnotherFrame */ - if (FrameCount < Nmax_info_frames) { - /* then this node may send another information frame */ - /* before passing the token. */ - Master_State = MSTP_MASTER_STATE_USE_TOKEN; - transition_now = true; - } - /* Npoll changed in Errata SSPC-135-2004 */ - else if (TokenCount < (Npoll - 1)) { - if ((MSTP_Flag.SoleMaster == true) && - (Next_Station != next_this_station)) { - /* SoleMaster */ - /* there are no other known master nodes to */ - /* which the token may be sent (true master-slave operation). */ - FrameCount = 0; - TokenCount++; + Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; + break; + case MSTP_MASTER_STATE_WAIT_FOR_REPLY: + /* In the WAIT_FOR_REPLY state, the node waits for */ + /* a reply from another node. */ + if (Timer_Silence() >= Treply_timeout) { + /* ReplyTimeout */ + /* assume that the request has failed */ + FrameCount = Nmax_info_frames; + Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; + /* Any retry of the data frame shall await the next entry */ + /* to the USE_TOKEN state. (Because of the length of the timeout, */ + /* this transition will cause the token to be passed regardless */ + /* of the initial value of FrameCount.) */ + transition_now = true; + } else { + if (MSTP_Flag.ReceivedInvalidFrame == true) { + /* InvalidFrame */ + /* error in frame reception */ + MSTP_Flag.ReceivedInvalidFrame = false; + Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; + transition_now = true; + } else if (MSTP_Flag.ReceivedValidFrame == true) { + if (DestinationAddress == This_Station) { + /* What did we receive? */ + switch (FrameType) { + case FRAME_TYPE_REPLY_POSTPONED: + /* ReceivedReplyPostponed */ + Master_State = + MSTP_MASTER_STATE_DONE_WITH_TOKEN; + break; + case FRAME_TYPE_TEST_RESPONSE: + Master_State = MSTP_MASTER_STATE_IDLE; + break; + case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: + /* ReceivedReply */ + /* or a proprietary type that indicates a reply */ + /* indicate successful reception to the higher layers */ + MSTP_Flag.ReceivePacketPending = true; + Master_State = + MSTP_MASTER_STATE_DONE_WITH_TOKEN; + break; + default: + /* if proprietary frame was expected, you might + need to transition to DONE WITH TOKEN */ + Master_State = MSTP_MASTER_STATE_IDLE; + break; + } + } else { + /* ReceivedUnexpectedFrame */ + /* an unexpected frame was received */ + /* This may indicate the presence of multiple tokens */ + /* or a device that didn't see activity after passing */ + /* a token (how lame!). */ + /* Synchronize with the network. */ + /* This action drops the token. */ + Master_State = MSTP_MASTER_STATE_IDLE; + } + MSTP_Flag.ReceivedValidFrame = false; + transition_now = true; + } + } + break; + /* The DONE_WITH_TOKEN state either sends another data frame, */ + /* passes the token, or initiates a Poll For Master cycle. */ + case MSTP_MASTER_STATE_DONE_WITH_TOKEN: + /* SendAnotherFrame */ + if (FrameCount < Nmax_info_frames) { + /* then this node may send another information frame */ + /* before passing the token. */ Master_State = MSTP_MASTER_STATE_USE_TOKEN; transition_now = true; - } else { - /* SendToken */ - /* Npoll changed in Errata SSPC-135-2004 */ - /* The comparison of NS and TS+1 eliminates the Poll For Master */ - /* if there are no addresses between TS and NS, since there is no */ - /* address at which a new master node may be found in that case. */ - TokenCount++; - /* transmit a Token frame to NS */ - MSTP_Send_Frame( - FRAME_TYPE_TOKEN, - Next_Station, - This_Station, NULL, 0); - RetryCount = 0; - EventCount = 0; - Master_State = MSTP_MASTER_STATE_PASS_TOKEN; } - } else if (next_poll_station == Next_Station) { - if (MSTP_Flag.SoleMaster == true) { - /* SoleMasterRestartMaintenancePFM */ - Poll_Station = next_next_station; - MSTP_Send_Frame( - FRAME_TYPE_POLL_FOR_MASTER, Poll_Station, - This_Station, NULL, 0); - /* no known successor node */ - Next_Station = This_Station; - RetryCount = 0; - TokenCount = 1; /* changed in Errata SSPC-135-2004 */ - /* EventCount = 0; removed in Addendum 135-2004d-8 */ - /* find a new successor to TS */ - Master_State = - MSTP_MASTER_STATE_POLL_FOR_MASTER; - } else { - /* ResetMaintenancePFM */ - Poll_Station = This_Station; - /* transmit a Token frame to NS */ - MSTP_Send_Frame( - FRAME_TYPE_TOKEN, - Next_Station, - This_Station, NULL, 0); - RetryCount = 0; - TokenCount = 1; /* changed in Errata SSPC-135-2004 */ - EventCount = 0; - Master_State = MSTP_MASTER_STATE_PASS_TOKEN; - } - } else { - /* SendMaintenancePFM */ - Poll_Station = next_poll_station; - MSTP_Send_Frame( - FRAME_TYPE_POLL_FOR_MASTER, - Poll_Station, This_Station, NULL, 0); - RetryCount = 0; - Master_State = MSTP_MASTER_STATE_POLL_FOR_MASTER; - } - break; - /* The PASS_TOKEN state listens for a successor to begin using */ - /* the token that this node has just attempted to pass. */ - case MSTP_MASTER_STATE_PASS_TOKEN: - if (Timer_Silence() <= Tusage_timeout) { - if (EventCount > Nmin_octets) { - /* SawTokenUser */ - /* Assume that a frame has been sent by the new token user. */ - /* Enter the IDLE state to process the frame. */ - Master_State = MSTP_MASTER_STATE_IDLE; - transition_now = true; - } - } else { - if (RetryCount < Nretry_token) { - /* RetrySendToken */ - RetryCount++; - /* Transmit a Token frame to NS */ - MSTP_Send_Frame( - FRAME_TYPE_TOKEN, - Next_Station, This_Station, NULL, - 0); - EventCount = 0; - /* re-enter the current state to listen for NS */ - /* to begin using the token. */ - } else { - /* FindNewSuccessor */ - /* Assume that NS has failed. */ - Poll_Station = next_next_station; - /* Transmit a Poll For Master frame to PS. */ - MSTP_Send_Frame( - FRAME_TYPE_POLL_FOR_MASTER, - Poll_Station, This_Station, NULL, - 0); - /* no known successor node */ - Next_Station = This_Station; - RetryCount = 0; - TokenCount = 0; - /* EventCount = 0; removed in Addendum 135-2004d-8 */ - /* find a new successor to TS */ - Master_State = - MSTP_MASTER_STATE_POLL_FOR_MASTER; - } - } - break; - /* The NO_TOKEN state is entered if Timer_Silence() becomes greater */ - /* than Tno_token, indicating that there has been no network activity */ - /* for that period of time. The timeout is continued to determine */ - /* whether or not this node may create a token. */ - case MSTP_MASTER_STATE_NO_TOKEN: - my_timeout = Tno_token + (Tslot * This_Station); - if (Timer_Silence() < my_timeout) { - if (EventCount > Nmin_octets) { - /* SawFrame */ - /* Some other node exists at a lower address. */ - /* Enter the IDLE state to receive and process the incoming frame. */ - Master_State = MSTP_MASTER_STATE_IDLE; - transition_now = true; - } - } else { - ns_timeout = - Tno_token + (Tslot * (This_Station + 1)); - if (Timer_Silence() < ns_timeout) { - /* GenerateToken */ - /* Assume that this node is the lowest numerical address */ - /* on the network and is empowered to create a token. */ - Poll_Station = next_this_station; - /* Transmit a Poll For Master frame to PS. */ - MSTP_Send_Frame( - FRAME_TYPE_POLL_FOR_MASTER, - Poll_Station, This_Station, NULL, - 0); - /* indicate that the next station is unknown */ - Next_Station = This_Station; - RetryCount = 0; - TokenCount = 0; - /* EventCount = 0; removed Addendum 135-2004d-8 */ - /* enter the POLL_FOR_MASTER state to find a new successor to TS. */ - Master_State = - MSTP_MASTER_STATE_POLL_FOR_MASTER; - } - } - break; - /* In the POLL_FOR_MASTER state, the node listens for a reply to */ - /* a previously sent Poll For Master frame in order to find */ - /* a successor node. */ - case MSTP_MASTER_STATE_POLL_FOR_MASTER: - if (MSTP_Flag.ReceivedValidFrame == true) { - if ((DestinationAddress == This_Station) - && (FrameType == - FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER)) { - /* ReceivedReplyToPFM */ - MSTP_Flag.SoleMaster = false; - Next_Station = SourceAddress; - EventCount = 0; - /* Transmit a Token frame to NS */ - MSTP_Send_Frame( - FRAME_TYPE_TOKEN, - Next_Station, This_Station, NULL, - 0); - Poll_Station = This_Station; - TokenCount = 0; - RetryCount = 0; - Master_State = MSTP_MASTER_STATE_PASS_TOKEN; - } else { - /* ReceivedUnexpectedFrame */ - /* An unexpected frame was received. */ - /* This may indicate the presence of multiple tokens. */ - /* enter the IDLE state to synchronize with the network. */ - /* This action drops the token. */ - Master_State = MSTP_MASTER_STATE_IDLE; - transition_now = true; - } - MSTP_Flag.ReceivedValidFrame = false; - } else if ((Timer_Silence() > Tusage_timeout) || - (MSTP_Flag.ReceivedInvalidFrame == true)) { - if (MSTP_Flag.SoleMaster == true) { - /* SoleMaster */ - /* There was no valid reply to the periodic poll */ - /* by the sole known master for other masters. */ - FrameCount = 0; - /* TokenCount++; removed in 2004 */ - Master_State = MSTP_MASTER_STATE_USE_TOKEN; - transition_now = true; - } else { - if (Next_Station != This_Station) { - /* DoneWithPFM */ - /* There was no valid reply to the maintenance */ - /* poll for a master at address PS. */ - EventCount = 0; + /* Npoll changed in Errata SSPC-135-2004 */ + else if (TokenCount < (Npoll - 1)) { + if ((MSTP_Flag.SoleMaster == true) && + (Next_Station != next_this_station)) { + /* SoleMaster */ + /* there are no other known master nodes to */ + /* which the token may be sent (true master-slave operation). */ + FrameCount = 0; + TokenCount++; + Master_State = MSTP_MASTER_STATE_USE_TOKEN; + transition_now = true; + } else { + /* SendToken */ + /* Npoll changed in Errata SSPC-135-2004 */ + /* The comparison of NS and TS+1 eliminates the Poll For Master */ + /* if there are no addresses between TS and NS, since there is no */ + /* address at which a new master node may be found in that case. */ + TokenCount++; /* transmit a Token frame to NS */ - MSTP_Send_Frame( - FRAME_TYPE_TOKEN, - Next_Station, This_Station, - NULL, 0); + MSTP_Send_Frame(FRAME_TYPE_TOKEN, + Next_Station, This_Station, NULL, 0); + RetryCount = 0; + EventCount = 0; + Master_State = MSTP_MASTER_STATE_PASS_TOKEN; + } + } else if (next_poll_station == Next_Station) { + if (MSTP_Flag.SoleMaster == true) { + /* SoleMasterRestartMaintenancePFM */ + Poll_Station = next_next_station; + MSTP_Send_Frame(FRAME_TYPE_POLL_FOR_MASTER, Poll_Station, + This_Station, NULL, 0); + /* no known successor node */ + Next_Station = This_Station; + RetryCount = 0; + TokenCount = 1; /* changed in Errata SSPC-135-2004 */ + /* EventCount = 0; removed in Addendum 135-2004d-8 */ + /* find a new successor to TS */ + Master_State = MSTP_MASTER_STATE_POLL_FOR_MASTER; + } else { + /* ResetMaintenancePFM */ + Poll_Station = This_Station; + /* transmit a Token frame to NS */ + MSTP_Send_Frame(FRAME_TYPE_TOKEN, + Next_Station, This_Station, NULL, 0); + RetryCount = 0; + TokenCount = 1; /* changed in Errata SSPC-135-2004 */ + EventCount = 0; + Master_State = MSTP_MASTER_STATE_PASS_TOKEN; + } + } else { + /* SendMaintenancePFM */ + Poll_Station = next_poll_station; + MSTP_Send_Frame(FRAME_TYPE_POLL_FOR_MASTER, + Poll_Station, This_Station, NULL, 0); + RetryCount = 0; + Master_State = MSTP_MASTER_STATE_POLL_FOR_MASTER; + } + break; + /* The PASS_TOKEN state listens for a successor to begin using */ + /* the token that this node has just attempted to pass. */ + case MSTP_MASTER_STATE_PASS_TOKEN: + if (Timer_Silence() <= Tusage_timeout) { + if (EventCount > Nmin_octets) { + /* SawTokenUser */ + /* Assume that a frame has been sent by the new token user. */ + /* Enter the IDLE state to process the frame. */ + Master_State = MSTP_MASTER_STATE_IDLE; + transition_now = true; + } + } else { + if (RetryCount < Nretry_token) { + /* RetrySendToken */ + RetryCount++; + /* Transmit a Token frame to NS */ + MSTP_Send_Frame(FRAME_TYPE_TOKEN, + Next_Station, This_Station, NULL, 0); + EventCount = 0; + /* re-enter the current state to listen for NS */ + /* to begin using the token. */ + } else { + /* FindNewSuccessor */ + /* Assume that NS has failed. */ + Poll_Station = next_next_station; + /* Transmit a Poll For Master frame to PS. */ + MSTP_Send_Frame(FRAME_TYPE_POLL_FOR_MASTER, + Poll_Station, This_Station, NULL, 0); + /* no known successor node */ + Next_Station = This_Station; + RetryCount = 0; + TokenCount = 0; + /* EventCount = 0; removed in Addendum 135-2004d-8 */ + /* find a new successor to TS */ + Master_State = MSTP_MASTER_STATE_POLL_FOR_MASTER; + } + } + break; + /* The NO_TOKEN state is entered if Timer_Silence() becomes greater */ + /* than Tno_token, indicating that there has been no network activity */ + /* for that period of time. The timeout is continued to determine */ + /* whether or not this node may create a token. */ + case MSTP_MASTER_STATE_NO_TOKEN: + my_timeout = Tno_token + (Tslot * This_Station); + if (Timer_Silence() < my_timeout) { + if (EventCount > Nmin_octets) { + /* SawFrame */ + /* Some other node exists at a lower address. */ + /* Enter the IDLE state to receive and process the incoming frame. */ + Master_State = MSTP_MASTER_STATE_IDLE; + transition_now = true; + } + } else { + ns_timeout = Tno_token + (Tslot * (This_Station + 1)); + if (Timer_Silence() < ns_timeout) { + /* GenerateToken */ + /* Assume that this node is the lowest numerical address */ + /* on the network and is empowered to create a token. */ + Poll_Station = next_this_station; + /* Transmit a Poll For Master frame to PS. */ + MSTP_Send_Frame(FRAME_TYPE_POLL_FOR_MASTER, + Poll_Station, This_Station, NULL, 0); + /* indicate that the next station is unknown */ + Next_Station = This_Station; + RetryCount = 0; + TokenCount = 0; + /* EventCount = 0; removed Addendum 135-2004d-8 */ + /* enter the POLL_FOR_MASTER state to find a new successor to TS. */ + Master_State = MSTP_MASTER_STATE_POLL_FOR_MASTER; + } + } + break; + /* In the POLL_FOR_MASTER state, the node listens for a reply to */ + /* a previously sent Poll For Master frame in order to find */ + /* a successor node. */ + case MSTP_MASTER_STATE_POLL_FOR_MASTER: + if (MSTP_Flag.ReceivedValidFrame == true) { + if ((DestinationAddress == This_Station) + && (FrameType == FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER)) { + /* ReceivedReplyToPFM */ + MSTP_Flag.SoleMaster = false; + Next_Station = SourceAddress; + EventCount = 0; + /* Transmit a Token frame to NS */ + MSTP_Send_Frame(FRAME_TYPE_TOKEN, + Next_Station, This_Station, NULL, 0); + Poll_Station = This_Station; + TokenCount = 0; RetryCount = 0; Master_State = MSTP_MASTER_STATE_PASS_TOKEN; } else { - if (next_poll_station != This_Station) { - /* SendNextPFM */ - Poll_Station = next_poll_station; - /* Transmit a Poll For Master frame to PS. */ - MSTP_Send_Frame( - FRAME_TYPE_POLL_FOR_MASTER, - Poll_Station, - This_Station, NULL, 0); + /* ReceivedUnexpectedFrame */ + /* An unexpected frame was received. */ + /* This may indicate the presence of multiple tokens. */ + /* enter the IDLE state to synchronize with the network. */ + /* This action drops the token. */ + Master_State = MSTP_MASTER_STATE_IDLE; + transition_now = true; + } + MSTP_Flag.ReceivedValidFrame = false; + } else if ((Timer_Silence() > Tusage_timeout) || + (MSTP_Flag.ReceivedInvalidFrame == true)) { + if (MSTP_Flag.SoleMaster == true) { + /* SoleMaster */ + /* There was no valid reply to the periodic poll */ + /* by the sole known master for other masters. */ + FrameCount = 0; + /* TokenCount++; removed in 2004 */ + Master_State = MSTP_MASTER_STATE_USE_TOKEN; + transition_now = true; + } else { + if (Next_Station != This_Station) { + /* DoneWithPFM */ + /* There was no valid reply to the maintenance */ + /* poll for a master at address PS. */ + EventCount = 0; + /* transmit a Token frame to NS */ + MSTP_Send_Frame(FRAME_TYPE_TOKEN, + Next_Station, This_Station, NULL, 0); RetryCount = 0; - /* Re-enter the current state. */ + Master_State = MSTP_MASTER_STATE_PASS_TOKEN; } else { - /* DeclareSoleMaster */ - /* to indicate that this station is the only master */ - MSTP_Flag.SoleMaster = true; - FrameCount = 0; - Master_State = - MSTP_MASTER_STATE_USE_TOKEN; - transition_now = true; + if (next_poll_station != This_Station) { + /* SendNextPFM */ + Poll_Station = next_poll_station; + /* Transmit a Poll For Master frame to PS. */ + MSTP_Send_Frame(FRAME_TYPE_POLL_FOR_MASTER, + Poll_Station, This_Station, NULL, 0); + RetryCount = 0; + /* Re-enter the current state. */ + } else { + /* DeclareSoleMaster */ + /* to indicate that this station is the only master */ + MSTP_Flag.SoleMaster = true; + FrameCount = 0; + Master_State = MSTP_MASTER_STATE_USE_TOKEN; + transition_now = true; + } } } + MSTP_Flag.ReceivedInvalidFrame = false; } - MSTP_Flag.ReceivedInvalidFrame = false; - } - break; - /* The ANSWER_DATA_REQUEST state is entered when a */ - /* BACnet Data Expecting Reply, a Test_Request, or */ - /* a proprietary frame that expects a reply is received. */ - case MSTP_MASTER_STATE_ANSWER_DATA_REQUEST: - /* Note: we could wait for up to Treply_delay */ - /* Note: the only packets pending are confirmed data requests */ - if (MSTP_Flag.TransmitPacketPending) { - /* Reply */ - /* If a reply is available from the higher layers */ - /* within Treply_delay after the reception of the */ - /* final octet of the requesting frame */ - /* (the mechanism used to determine this is a local matter), */ - /* then call MSTP_Send_Frame to transmit the reply frame */ - /* and enter the IDLE state to wait for the next frame. */ - /* Note: optimized such that we are never a client */ - MSTP_Send_Frame( - FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY, - TransmitPacketDest, - This_Station, - (uint8_t *) & TransmitPacket[0], - TransmitPacketLen); - MSTP_Flag.TransmitPacketPending = false; + break; + /* The ANSWER_DATA_REQUEST state is entered when a */ + /* BACnet Data Expecting Reply, a Test_Request, or */ + /* a proprietary frame that expects a reply is received. */ + case MSTP_MASTER_STATE_ANSWER_DATA_REQUEST: + /* Note: we could wait for up to Treply_delay */ + /* Note: the only packets pending are confirmed data requests */ + if (MSTP_Flag.TransmitPacketPending) { + /* Reply */ + /* If a reply is available from the higher layers */ + /* within Treply_delay after the reception of the */ + /* final octet of the requesting frame */ + /* (the mechanism used to determine this is a local matter), */ + /* then call MSTP_Send_Frame to transmit the reply frame */ + /* and enter the IDLE state to wait for the next frame. */ + /* Note: optimized such that we are never a client */ + MSTP_Send_Frame(FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY, + TransmitPacketDest, + This_Station, + (uint8_t *) & TransmitPacket[0], TransmitPacketLen); + MSTP_Flag.TransmitPacketPending = false; + Master_State = MSTP_MASTER_STATE_IDLE; + } else { + /* DeferredReply */ + /* If no reply will be available from the higher layers */ + /* within Treply_delay after the reception of the */ + /* final octet of the requesting frame (the mechanism */ + /* used to determine this is a local matter), */ + /* then an immediate reply is not possible. */ + /* Any reply shall wait until this node receives the token. */ + /* Call MSTP_Send_Frame to transmit a Reply Postponed frame, */ + /* and enter the IDLE state. */ + MSTP_Send_Frame(FRAME_TYPE_REPLY_POSTPONED, + SourceAddress, This_Station, NULL, 0); + Master_State = MSTP_MASTER_STATE_IDLE; + } + /* clear our flag we were holding for comparison */ + MSTP_Flag.ReceivedValidFrame = false; + break; + default: Master_State = MSTP_MASTER_STATE_IDLE; - } else { - /* DeferredReply */ - /* If no reply will be available from the higher layers */ - /* within Treply_delay after the reception of the */ - /* final octet of the requesting frame (the mechanism */ - /* used to determine this is a local matter), */ - /* then an immediate reply is not possible. */ - /* Any reply shall wait until this node receives the token. */ - /* Call MSTP_Send_Frame to transmit a Reply Postponed frame, */ - /* and enter the IDLE state. */ - MSTP_Send_Frame( - FRAME_TYPE_REPLY_POSTPONED, - SourceAddress, - This_Station, NULL, 0); - Master_State = MSTP_MASTER_STATE_IDLE; - } - /* clear our flag we were holding for comparison */ - MSTP_Flag.ReceivedValidFrame = false; - break; - default: - Master_State = MSTP_MASTER_STATE_IDLE; - break; + break; } return transition_now; } /* returns number of bytes sent on success, zero on failure */ -int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */ - BACNET_NPDU_DATA * npdu_data, /* network information */ - uint8_t * pdu, /* any data to be sent - may be null */ - unsigned pdu_len) /* number of bytes of data */ -{ +int dlmstp_send_pdu( + BACNET_ADDRESS * dest, /* destination address */ + BACNET_NPDU_DATA * npdu_data, /* network information */ + uint8_t * pdu, /* any data to be sent - may be null */ + unsigned pdu_len) +{ /* number of bytes of data */ int bytes_sent = 0; if (MSTP_Flag.TransmitPacketPending == false) { @@ -1079,15 +1049,15 @@ int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */ /* Return the length of the packet */ uint16_t dlmstp_receive( - BACNET_ADDRESS * src, /* source address */ - uint8_t * pdu, /* PDU data */ - uint16_t max_pdu, /* amount of space available in the PDU */ - unsigned timeout) /* milliseconds to wait for a packet */ -{ - uint16_t pdu_len = 0; /* return value */ + BACNET_ADDRESS * src, /* source address */ + uint8_t * pdu, /* PDU data */ + uint16_t max_pdu, /* amount of space available in the PDU */ + unsigned timeout) +{ /* milliseconds to wait for a packet */ + uint16_t pdu_len = 0; /* return value */ - /* dummy - unused parameter */ - timeout = timeout; + /* dummy - unused parameter */ + timeout = timeout; /* set the input buffer to the same data storage for zero copy */ if (!InputBuffer) { InputBuffer = pdu; @@ -1098,8 +1068,7 @@ uint16_t dlmstp_receive( (MSTP_Flag.ReceivedInvalidFrame == false)) { for (;;) { MSTP_Receive_Frame_FSM(); - if (MSTP_Flag.ReceivedValidFrame || - MSTP_Flag.ReceivedInvalidFrame) + if (MSTP_Flag.ReceivedValidFrame || MSTP_Flag.ReceivedInvalidFrame) break; /* if we are not idle, then we are receiving a frame or timing out */ @@ -1112,7 +1081,7 @@ uint16_t dlmstp_receive( while (MSTP_Master_Node_FSM()) { /* do nothing while some states fast transition */ }; - } + } /* if there is a packet that needs processed, do it now. */ if (MSTP_Flag.ReceivePacketPending) { MSTP_Flag.ReceivePacketPending = false; @@ -1125,7 +1094,8 @@ uint16_t dlmstp_receive( return pdu_len; } -void dlmstp_set_mac_address(uint8_t mac_address) +void dlmstp_set_mac_address( + uint8_t mac_address) { /* Master Nodes can only have address 0-127 */ if (mac_address <= 127) { @@ -1142,7 +1112,8 @@ void dlmstp_set_mac_address(uint8_t mac_address) return; } -uint8_t dlmstp_mac_address(void) +uint8_t dlmstp_mac_address( + void) { return This_Station; } @@ -1154,7 +1125,8 @@ uint8_t dlmstp_mac_address(void) /* nodes. This may be used to allocate more or less of the available link */ /* bandwidth to particular nodes. If Max_Info_Frames is not writable in a */ /* node, its value shall be 1. */ -void dlmstp_set_max_info_frames(uint8_t max_info_frames) +void dlmstp_set_max_info_frames( + uint8_t max_info_frames) { if (max_info_frames >= 1) { Nmax_info_frames = max_info_frames; @@ -1168,7 +1140,8 @@ void dlmstp_set_max_info_frames(uint8_t max_info_frames) return; } -uint8_t dlmstp_max_info_frames(void) +uint8_t dlmstp_max_info_frames( + void) { return Nmax_info_frames; } @@ -1178,7 +1151,8 @@ uint8_t dlmstp_max_info_frames(void) /* allowable address for master nodes. The value of Max_Master shall be */ /* less than or equal to 127. If Max_Master is not writable in a node, */ /* its value shall be 127. */ -void dlmstp_set_max_master(uint8_t max_master) +void dlmstp_set_max_master( + uint8_t max_master) { if (max_master <= 127) { if (This_Station <= max_master) { @@ -1194,14 +1168,16 @@ void dlmstp_set_max_master(uint8_t max_master) return; } -uint8_t dlmstp_max_master(void) +uint8_t dlmstp_max_master( + void) { return Nmax_master; } -void dlmstp_get_my_address(BACNET_ADDRESS * my_address) +void dlmstp_get_my_address( + BACNET_ADDRESS * my_address) { - int i = 0; /* counter */ + int i = 0; /* counter */ my_address->mac_len = 1; my_address->mac[0] = This_Station; @@ -1214,15 +1190,16 @@ void dlmstp_get_my_address(BACNET_ADDRESS * my_address) return; } -void dlmstp_get_broadcast_address(BACNET_ADDRESS * dest) -{ /* destination address */ - int i = 0; /* counter */ +void dlmstp_get_broadcast_address( + BACNET_ADDRESS * dest) +{ /* destination address */ + int i = 0; /* counter */ if (dest) { dest->mac_len = 1; dest->mac[0] = MSTP_BROADCAST_ADDRESS; dest->net = BACNET_BROADCAST_NETWORK; - dest->len = 0; /* always zero when DNET is broadcast */ + dest->len = 0; /* always zero when DNET is broadcast */ for (i = 0; i < MAX_MAC_LEN; i++) { dest->adr[i] = 0; } diff --git a/bacnet-stack/ports/atmega168/h_rp.c b/bacnet-stack/ports/atmega168/h_rp.c index 61966941..5a8f8fbe 100644 --- a/bacnet-stack/ports/atmega168/h_rp.c +++ b/bacnet-stack/ports/atmega168/h_rp.c @@ -64,24 +64,18 @@ int Encode_Property_APDU( *error_class = ERROR_CLASS_OBJECT; *error_code = ERROR_CODE_UNKNOWN_OBJECT; /* handle each object type */ - switch(object_type) { + switch (object_type) { case OBJECT_DEVICE: if (Device_Valid_Object_Instance_Number(object_instance)) { - apdu_len = Device_Encode_Property_APDU( - &apdu[0], - property, - array_index, - error_class, error_code); + apdu_len = Device_Encode_Property_APDU(&apdu[0], + property, array_index, error_class, error_code); } break; case OBJECT_ANALOG_VALUE: if (Analog_Value_Valid_Instance(object_instance)) { - apdu_len = Analog_Value_Encode_Property_APDU( - &apdu[0], + apdu_len = Analog_Value_Encode_Property_APDU(&apdu[0], object_instance, - property, - array_index, - error_class, error_code); + property, array_index, error_class, error_code); } break; default: @@ -93,9 +87,11 @@ int Encode_Property_APDU( return apdu_len; } -void handler_read_property(uint8_t * service_request, +void handler_read_property( + uint8_t * service_request, uint16_t service_len, - BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data) + BACNET_ADDRESS * src, + BACNET_CONFIRMED_SERVICE_DATA * service_data) { BACNET_READ_PROPERTY_DATA data; int len = 0; @@ -125,13 +121,10 @@ void handler_read_property(uint8_t * service_request, } else { /* most cases will be error */ error = true; - len = Encode_Property_APDU( - &Temp_Buf[0], + len = Encode_Property_APDU(&Temp_Buf[0], data.object_type, data.object_instance, - data.object_property, - data.array_index, - &error_class, &error_code); + data.object_property, data.array_index, &error_class, &error_code); if (len >= 0) { /* encode the APDU portion of the packet */ data.application_data = &Temp_Buf[0]; @@ -145,18 +138,18 @@ void handler_read_property(uint8_t * service_request, } if (error) { switch (len) { - /* BACnet APDU too small to fit data, so proper response is Abort */ - case -2: - len = abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true); - break; - case -1: - default: - len = bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_READ_PROPERTY, error_class, error_code); - break; + /* BACnet APDU too small to fit data, so proper response is Abort */ + case -2: + len = abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, + ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true); + break; + case -1: + default: + len = bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_READ_PROPERTY, error_class, error_code); + break; } } pdu_len += len; diff --git a/bacnet-stack/ports/atmega168/h_whois.c b/bacnet-stack/ports/atmega168/h_whois.c index ad21077c..d39046b1 100644 --- a/bacnet-stack/ports/atmega168/h_whois.c +++ b/bacnet-stack/ports/atmega168/h_whois.c @@ -39,8 +39,10 @@ bool Send_I_Am = true; -void handler_who_is(uint8_t * service_request, - uint16_t service_len, BACNET_ADDRESS * src) +void handler_who_is( + uint8_t * service_request, + uint16_t service_len, + BACNET_ADDRESS * src) { int len = 0; int32_t low_limit = 0; @@ -55,8 +57,7 @@ void handler_who_is(uint8_t * service_request, } else if (len != -1) { /* is my device id within the limits? */ target_device = Device_Object_Instance_Number(); - if (((target_device >= low_limit) && - (target_device <= high_limit)) + if (((target_device >= low_limit) && (target_device <= high_limit)) || /* BACnet wildcard is the max instance number - everyone responds */ ((BACNET_MAX_INSTANCE >= (uint32_t) low_limit) && diff --git a/bacnet-stack/ports/atmega168/h_wp.c b/bacnet-stack/ports/atmega168/h_wp.c index 4d0ef07f..18335548 100644 --- a/bacnet-stack/ports/atmega168/h_wp.c +++ b/bacnet-stack/ports/atmega168/h_wp.c @@ -48,9 +48,11 @@ /* too big to reside on stack frame for PIC */ static BACNET_WRITE_PROPERTY_DATA wp_data; -void handler_write_property(uint8_t * service_request, +void handler_write_property( + uint8_t * service_request, uint16_t service_len, - BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data) + BACNET_ADDRESS * src, + BACNET_CONFIRMED_SERVICE_DATA * service_data) { int len = 0; int pdu_len = 0; @@ -61,8 +63,7 @@ void handler_write_property(uint8_t * service_request, BACNET_ADDRESS my_address; /* decode the service request only */ - len = wp_decode_service_request(service_request, - service_len, &wp_data); + len = wp_decode_service_request(service_request, service_len, &wp_data); /* encode the NPDU portion of the packet */ datalink_get_my_address(&my_address); npdu_encode_npdu_data(&npdu_data, false, MESSAGE_PRIORITY_NORMAL); @@ -79,66 +80,66 @@ void handler_write_property(uint8_t * service_request, } else { switch (wp_data.object_type) { #if 0 - case OBJECT_DEVICE: - if (Device_Write_Property(&wp_data, &error_class, &error_code)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY); - } else { + case OBJECT_DEVICE: + if (Device_Write_Property(&wp_data, &error_class, &error_code)) { + len = + encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_WRITE_PROPERTY); + } else { + len = + bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, + error_code); + } + break; + case OBJECT_ANALOG_INPUT: + case OBJECT_BINARY_INPUT: + error_class = ERROR_CLASS_PROPERTY; + error_code = ERROR_CODE_WRITE_ACCESS_DENIED; len = bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, - error_code); - } - break; - case OBJECT_ANALOG_INPUT: - case OBJECT_BINARY_INPUT: - error_class = ERROR_CLASS_PROPERTY; - error_code = ERROR_CODE_WRITE_ACCESS_DENIED; - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, - error_class, error_code); - break; - case OBJECT_BINARY_VALUE: - if (Binary_Value_Write_Property(&wp_data, &error_class, - &error_code)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY); - } else { - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, - error_code); - } - break; + service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, + error_class, error_code); + break; + case OBJECT_BINARY_VALUE: + if (Binary_Value_Write_Property(&wp_data, &error_class, + &error_code)) { + len = + encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_WRITE_PROPERTY); + } else { + len = + bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, + error_code); + } + break; #endif - case OBJECT_ANALOG_VALUE: - if (Analog_Value_Write_Property(&wp_data, &error_class, - &error_code)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY); - } else { + case OBJECT_ANALOG_VALUE: + if (Analog_Value_Write_Property(&wp_data, &error_class, + &error_code)) { + len = + encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_WRITE_PROPERTY); + } else { + len = + bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, + error_code); + } + break; + default: len = bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, - error_code); - } - break; - default: - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, - error_class, error_code); - break; + service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, + error_class, error_code); + break; } } pdu_len += len; diff --git a/bacnet-stack/ports/atmega168/hardware.h b/bacnet-stack/ports/atmega168/hardware.h index 50895531..dbbef325 100644 --- a/bacnet-stack/ports/atmega168/hardware.h +++ b/bacnet-stack/ports/atmega168/hardware.h @@ -26,7 +26,7 @@ #define HARDWARE_H /* The processor clock frequency */ -#define F_CPU (7372800) +#define F_CPU (7372800) #include #include diff --git a/bacnet-stack/ports/atmega168/main.c b/bacnet-stack/ports/atmega168/main.c index 76584d0d..f41e2406 100644 --- a/bacnet-stack/ports/atmega168/main.c +++ b/bacnet-stack/ports/atmega168/main.c @@ -39,11 +39,14 @@ http://www.avrfreaks.net/wiki/index.php/Documentation:AVR_GCC/IarToAvrgcc*/ /* dummy function */ -bool dcc_communication_enabled(void) { +bool dcc_communication_enabled( + void) +{ return true; } -void init(void) +void init( + void) { /* Initialize the Clock Prescaler for ATmega48/88/168 */ /* The default CLKPSx bits are factory set to 0011 */ @@ -51,17 +54,17 @@ void init(void) CLKPR = _BV(CLKPCE); /* CLKPS3 CLKPS2 CLKPS1 CLKPS0 Clock Division Factor ------ ------ ------ ------ --------------------- - 0 0 0 0 1 - 0 0 0 1 2 - 0 0 1 0 4 - 0 0 1 1 8 - 0 1 0 0 16 - 0 1 0 1 32 - 0 1 1 0 64 - 0 1 1 1 128 - 1 0 0 0 256 - 1 x x x Reserved - */ + 0 0 0 0 1 + 0 0 0 1 2 + 0 0 1 0 4 + 0 0 1 1 8 + 0 1 0 0 16 + 0 1 0 1 32 + 0 1 1 0 64 + 0 1 1 1 128 + 1 0 0 0 256 + 1 x x x Reserved + */ /* Set the CLKPS3..0 bits to Prescaler of 1 */ CLKPR = 0; /* Initialize I/O ports */ @@ -75,7 +78,7 @@ void init(void) PORTD = 0; /* Configure the watchdog timer - Disabled for testing */ - BIT_CLEAR(MCUSR,WDRF); + BIT_CLEAR(MCUSR, WDRF); WDTCSR = 0; /* Configure USART */ @@ -85,11 +88,11 @@ void init(void) Timer_Initialize(); /* Set the LED ports OFF */ - BIT_SET(PORTD,PD4); - BIT_SET(PORTD,PD5); + BIT_SET(PORTD, PD4); + BIT_SET(PORTD, PD5); /* Configure the LED ports as outputs */ - BIT_SET(DDRD,DDD4); - BIT_SET(DDRD,DDD5); + BIT_SET(DDRD, DDD4); + BIT_SET(DDRD, DDD5); /* Enable global interrupts */ sei(); @@ -97,23 +100,26 @@ void init(void) static uint8_t NPDU_Timer; -static void NDPU_Timers(void) +static void NDPU_Timers( + void) { if (NPDU_Timer) { NPDU_Timer--; if (NPDU_Timer == 0) { - BIT_SET(PORTD,PD5); + BIT_SET(PORTD, PD5); } } } -static void NPDU_LED_On(void) +static void NPDU_LED_On( + void) { - BIT_CLEAR(PORTD,PD5); + BIT_CLEAR(PORTD, PD5); NPDU_Timer = 20; } -void task_milliseconds(void) +void task_milliseconds( + void) { while (Timer_Milliseconds) { Timer_Milliseconds--; @@ -125,31 +131,33 @@ void task_milliseconds(void) static uint8_t Address_Switch; -void input_switch_read(void) +void input_switch_read( + void) { uint8_t value; static uint8_t old_value = 0; - value = BITMASK_CHECK(PINC,0x0F) | (BITMASK_CHECK(PINB,0x07)<<4); + value = BITMASK_CHECK(PINC, 0x0F) | (BITMASK_CHECK(PINB, 0x07) << 4); if (value != old_value) { old_value = value; } else { if (old_value != Address_Switch) { - Address_Switch = old_value; + Address_Switch = old_value; #if defined(BACDL_MSTP) - dlmstp_set_mac_address(Address_Switch); + dlmstp_set_mac_address(Address_Switch); #endif - Device_Set_Object_Instance_Number(86000 + Address_Switch); + Device_Set_Object_Instance_Number(86000 + Address_Switch); } } } static uint8_t PDUBuffer[MAX_MPDU]; -int main(void) +int main( + void) { uint16_t pdu_len = 0; BACNET_ADDRESS src; /* source address */ - + init(); #if defined(BACDL_MSTP) RS485_Set_Baud_Rate(38400); diff --git a/bacnet-stack/ports/atmega168/rs485.c b/bacnet-stack/ports/atmega168/rs485.c index 3e0664cf..b4a94b83 100644 --- a/bacnet-stack/ports/atmega168/rs485.c +++ b/bacnet-stack/ports/atmega168/rs485.c @@ -70,10 +70,11 @@ static uint32_t RS485_Baud = 9600; * ALGORITHM: none * NOTES: none *****************************************************************************/ -void RS485_Initialize(void) +void RS485_Initialize( + void) { /* enable Transmit and Receive */ - UCSR0B = _BV(TXEN0) | _BV(RXEN0); + UCSR0B = _BV(TXEN0) | _BV(RXEN0); /* Set USART Control and Status Register n C */ /* Asynchronous USART 8-bit data, No parity, 1 stop */ @@ -84,16 +85,16 @@ void RS485_Initialize(void) /* Clock Polarity: UCPOLn = 0 when asynchronous mode is used. */ UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* Clear Power Reduction USART0 */ - BIT_CLEAR(PRR,PRUSART0); + BIT_CLEAR(PRR, PRUSART0); /* Use port PD2 for RTS - enable and disable of Transceiver Tx/Rx */ /* Set port bit as Output - initially receiving */ - BIT_CLEAR(PORTD,PD2); - BIT_SET(DDRD,DDD2); + BIT_CLEAR(PORTD, PD2); + BIT_SET(DDRD, DDD2); /* Configure Transmit and Receive LEDs - initially off */ - BIT_SET(PORTD,PD6); - BIT_SET(PORTD,PD7); - BIT_SET(DDRD,DDD6); - BIT_SET(DDRD,DDD7); + BIT_SET(PORTD, PD6); + BIT_SET(PORTD, PD7); + BIT_SET(DDRD, DDD6); + BIT_SET(DDRD, DDD7); return; } @@ -104,7 +105,8 @@ void RS485_Initialize(void) * ALGORITHM: none * NOTES: none *****************************************************************************/ -uint32_t RS485_Get_Baud_Rate(void) +uint32_t RS485_Get_Baud_Rate( + void) { return RS485_Baud; } @@ -115,7 +117,8 @@ uint32_t RS485_Get_Baud_Rate(void) * ALGORITHM: none * NOTES: none *****************************************************************************/ -bool RS485_Set_Baud_Rate(uint32_t baud) +bool RS485_Set_Baud_Rate( + uint32_t baud) { bool valid = true; @@ -128,7 +131,7 @@ bool RS485_Set_Baud_Rate(uint32_t baud) case 115200: RS485_Baud = baud; /* 2x speed mode */ - BIT_SET(UCSR0A,U2X0); + BIT_SET(UCSR0A, U2X0); /* configure baud rate */ UBRR0 = (F_CPU / (8UL * RS485_Baud)) - 1; /* FIXME: store the baud rate */ @@ -147,14 +150,15 @@ bool RS485_Set_Baud_Rate(uint32_t baud) * ALGORITHM: none * NOTES: none *****************************************************************************/ -void RS485_Turnaround_Delay(void) +void RS485_Turnaround_Delay( + void) { uint16_t turnaround_time; - + /* delay after reception before trasmitting - per MS/TP spec */ /* wait a minimum 40 bit times since reception */ /* at least 1 ms for errors: rounding, clock tick */ - turnaround_time = 1 + ((Tturnaround*1000UL)/RS485_Baud); + turnaround_time = 1 + ((Tturnaround * 1000UL) / RS485_Baud); while (Timer_Silence() < turnaround_time) { /* do nothing - wait for timer to increment */ }; @@ -166,18 +170,19 @@ void RS485_Turnaround_Delay(void) * ALGORITHM: none * NOTES: none *****************************************************************************/ -void RS485_Transmitter_Enable(bool enable) +void RS485_Transmitter_Enable( + bool enable) { if (enable) { - BIT_SET(PORTD,PD2); + BIT_SET(PORTD, PD2); } else { - #if Tpostdrive1 - _delay_us(Tpostdrive1/RS485_Baud); - #endif - #if Tpostdrive2 - _delay_us(Tpostdrive2/RS485_Baud); - #endif - BIT_CLEAR(PORTD,PD2); +#if Tpostdrive1 + _delay_us(Tpostdrive1 / RS485_Baud); +#endif +#if Tpostdrive2 + _delay_us(Tpostdrive2 / RS485_Baud); +#endif + BIT_CLEAR(PORTD, PD2); } } @@ -187,18 +192,19 @@ void RS485_Transmitter_Enable(bool enable) * ALGORITHM: none * NOTES: expected to be called once a millisecond *****************************************************************************/ -void RS485_LED_Timers(void) +void RS485_LED_Timers( + void) { if (LED1_Off_Timer) { LED1_Off_Timer--; if (LED1_Off_Timer == 0) { - BIT_SET(PORTD,PD6); + BIT_SET(PORTD, PD6); } } if (LED3_Off_Timer) { LED3_Off_Timer--; if (LED3_Off_Timer == 0) { - BIT_SET(PORTD,PD7); + BIT_SET(PORTD, PD7); } } } @@ -209,9 +215,10 @@ void RS485_LED_Timers(void) * ALGORITHM: none * NOTES: none *****************************************************************************/ -static void RS485_LED1_On(void) +static void RS485_LED1_On( + void) { - BIT_CLEAR(PORTD,PD6); + BIT_CLEAR(PORTD, PD6); LED1_Off_Timer = 20; } @@ -221,9 +228,10 @@ static void RS485_LED1_On(void) * ALGORITHM: none * NOTES: none *****************************************************************************/ -static void RS485_LED3_On(void) +static void RS485_LED3_On( + void) { - BIT_CLEAR(PORTD,PD7); + BIT_CLEAR(PORTD, PD7); LED3_Off_Timer = 20; } @@ -234,27 +242,27 @@ static void RS485_LED3_On(void) * NOTES: none *****************************************************************************/ void RS485_Send_Data( - uint8_t * buffer, /* data to send */ - uint16_t nbytes) /* number of bytes of data */ -{ + uint8_t * buffer, /* data to send */ + uint16_t nbytes) +{ /* number of bytes of data */ RS485_LED3_On(); while (nbytes) { - while (!BIT_CHECK(UCSR0A,UDRE0)) { + while (!BIT_CHECK(UCSR0A, UDRE0)) { /* do nothing - wait until Tx buffer is empty */ } UDR0 = *buffer; buffer++; nbytes--; } - while (!BIT_CHECK(UCSR0A,UDRE0)) { + while (!BIT_CHECK(UCSR0A, UDRE0)) { /* do nothing - wait until Tx buffer is empty */ } /* is the frame sent? */ - while (!BIT_CHECK(UCSR0A,TXC0)) { + while (!BIT_CHECK(UCSR0A, TXC0)) { /* do nothing - wait until the entire frame in the Transmit Shift Register has been shifted out */ } - BIT_CLEAR(UCSR0A,TXC0); + BIT_CLEAR(UCSR0A, TXC0); /* per MSTP spec, sort of */ Timer_Silence_Reset(); } @@ -266,31 +274,32 @@ void RS485_Send_Data( * ALGORITHM: autobaud - if there are a lot of errors, switch baud rate * NOTES: Clears any error flags. *****************************************************************************/ -bool RS485_ReceiveError(void) +bool RS485_ReceiveError( + void) { bool ReceiveError = false; uint8_t dummy_data; /* check for framing error */ - #if 0 - if (BIT_CHECK(UCSR0A,FE0)) { +#if 0 + if (BIT_CHECK(UCSR0A, FE0)) { /* FIXME: how do I clear the error flags? */ - BITMASK_CLEAR(UCSR0A,(_BV(FE0) | _BV(DOR0) | _BV(UPE0))); + BITMASK_CLEAR(UCSR0A, (_BV(FE0) | _BV(DOR0) | _BV(UPE0))); ReceiveError = true; } - #endif +#endif /* check for overrun error */ - if (BIT_CHECK(UCSR0A,DOR0)) { + if (BIT_CHECK(UCSR0A, DOR0)) { /* flush the receive buffer */ do { dummy_data = UDR0; - } while (BIT_CHECK(UCSR0A,RXC0)); + } while (BIT_CHECK(UCSR0A, RXC0)); ReceiveError = true; } if (ReceiveError) { RS485_LED1_On(); } - + return ReceiveError; } @@ -300,12 +309,13 @@ bool RS485_ReceiveError(void) * ALGORITHM: none * NOTES: none *****************************************************************************/ -bool RS485_DataAvailable(uint8_t *data) +bool RS485_DataAvailable( + uint8_t * data) { bool DataAvailable = false; /* check for data */ - if (BIT_CHECK(UCSR0A,RXC0)) { + if (BIT_CHECK(UCSR0A, RXC0)) { *data = UDR0; DataAvailable = true; RS485_LED1_On(); @@ -315,7 +325,8 @@ bool RS485_DataAvailable(uint8_t *data) } #ifdef TEST_RS485 -int main(void) +int main( + void) { unsigned i = 0; uint8_t DataRegister; @@ -325,11 +336,10 @@ int main(void) /* receive task */ for (;;) { if (RS485_ReceiveError()) { - fprintf(stderr,"ERROR "); + fprintf(stderr, "ERROR "); } else if (RS485_DataAvailable(&DataRegister)) { - fprintf(stderr,"%02X ",DataRegister); + fprintf(stderr, "%02X ", DataRegister); } } } -#endif /* TEST_RS485 */ - +#endif /* TEST_RS485 */ diff --git a/bacnet-stack/ports/atmega168/rs485.h b/bacnet-stack/ports/atmega168/rs485.h index f14320c8..5725b1f7 100644 --- a/bacnet-stack/ports/atmega168/rs485.h +++ b/bacnet-stack/ports/atmega168/rs485.h @@ -40,26 +40,34 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - void RS485_Initialize(void); + void RS485_Initialize( + void); - void RS485_Transmitter_Enable(bool enable); + void RS485_Transmitter_Enable( + bool enable); void RS485_Send_Data( uint8_t * buffer, /* data to send */ uint16_t nbytes); /* number of bytes of data */ - bool RS485_ReceiveError(void); - bool RS485_DataAvailable(uint8_t *data); + bool RS485_ReceiveError( + void); + bool RS485_DataAvailable( + uint8_t * data); - void RS485_Turnaround_Delay(void); - uint32_t RS485_Get_Baud_Rate(void); - bool RS485_Set_Baud_Rate(uint32_t baud); + void RS485_Turnaround_Delay( + void); + uint32_t RS485_Get_Baud_Rate( + void); + bool RS485_Set_Baud_Rate( + uint32_t baud); - void RS485_LED_Timers(void); + void RS485_LED_Timers( + void); #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/ports/atmega168/timer.c b/bacnet-stack/ports/atmega168/timer.c index c9ff3933..eec1b6be 100644 --- a/bacnet-stack/ports/atmega168/timer.c +++ b/bacnet-stack/ports/atmega168/timer.c @@ -41,30 +41,31 @@ volatile uint8_t Timer_Milliseconds = 0; static volatile uint16_t SilenceTime; /* Configure the Timer */ -void Timer_Initialize(void) +void Timer_Initialize( + void) { /* Normal Operation */ TCCR1A = 0; /* CSn2 CSn1 CSn0 Description ---- ---- ---- ----------- - 0 0 0 No Clock Source - 0 0 1 No prescaling - 0 1 0 CLKio/8 - 0 1 1 CLKio/64 - 1 0 0 CLKio/256 - 1 0 1 CLKio/1024 - 1 1 0 Falling Edge of T0 (external) - 1 1 1 Rising Edge of T0 (external) - */ + 0 0 0 No Clock Source + 0 0 1 No prescaling + 0 1 0 CLKio/8 + 0 1 1 CLKio/64 + 1 0 0 CLKio/256 + 1 0 1 CLKio/1024 + 1 1 0 Falling Edge of T0 (external) + 1 1 1 Rising Edge of T0 (external) + */ TCCR0B = _BV(CS01) | _BV(CS00); /* Clear any TOV1 Flag set when the timer overflowed */ - BIT_CLEAR(TIFR0,TOV0); + BIT_CLEAR(TIFR0, TOV0); /* Initial value */ TCNT0 = TIMER_COUNT; /* Enable the overflow interrupt */ - BIT_SET(TIMSK0,TOIE0); + BIT_SET(TIMSK0, TOIE0); /* Clear the Power Reduction Timer/Counter0 */ - BIT_CLEAR(PRR,PRTIM0); + BIT_CLEAR(PRR, PRTIM0); } /* Timer interupt */ @@ -77,13 +78,14 @@ ISR(TIMER0_OVF_vect) /* Overflow Flag is automatically cleared */ /* Update the global timer */ if (Timer_Milliseconds < 0xFF) - Timer_Milliseconds++; + Timer_Milliseconds++; if (SilenceTime < 0xFFFF) SilenceTime++; } /* Public access to the Silence Timer */ -uint16_t Timer_Silence(void) +uint16_t Timer_Silence( + void) { uint16_t timer; uint8_t sreg; @@ -97,7 +99,8 @@ uint16_t Timer_Silence(void) } /* Public reset of the Silence Timer */ -void Timer_Silence_Reset(void) +void Timer_Silence_Reset( + void) { uint8_t sreg; diff --git a/bacnet-stack/ports/atmega168/timer.h b/bacnet-stack/ports/atmega168/timer.h index b5efbcc1..00c9684a 100644 --- a/bacnet-stack/ports/atmega168/timer.h +++ b/bacnet-stack/ports/atmega168/timer.h @@ -29,14 +29,16 @@ extern volatile uint8_t Timer_Milliseconds; #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ -void Timer_Initialize(void); -uint16_t Timer_Silence(void); -void Timer_Silence_Reset(void); + void Timer_Initialize( + void); + uint16_t Timer_Silence( + void); + void Timer_Silence_Reset( + void); #ifdef __cplusplus } -#endif /* __cplusplus */ - +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/ports/linux/arcnet.c b/bacnet-stack/ports/linux/arcnet.c index 99691583..a553fb59 100644 --- a/bacnet-stack/ports/linux/arcnet.c +++ b/bacnet-stack/ports/linux/arcnet.c @@ -80,12 +80,14 @@ clockp Clock Prescaler DataRate */ -bool arcnet_valid(void) +bool arcnet_valid( + void) { return (ARCNET_Sock_FD >= 0); } -void arcnet_cleanup(void) +void arcnet_cleanup( + void) { if (arcnet_valid()) close(ARCNET_Sock_FD); @@ -94,11 +96,12 @@ void arcnet_cleanup(void) return; } -static int arcnet_bind(char *interface_name) +static int arcnet_bind( + char *interface_name) { - int sock_fd = -1; /* return value */ + int sock_fd = -1; /* return value */ struct ifreq ifr; - int rv; /* return value - error value from df or ioctl call */ + int rv; /* return value - error value from df or ioctl call */ int uid = 0; /* check to see if we are being run as root */ @@ -116,8 +119,7 @@ static int arcnet_bind(char *interface_name) /* Then follow it by: # modprobe af_packet */ if ((sock_fd = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ALL))) < 0) { /* Error occured */ - fprintf(stderr, - "arcnet: Error opening socket: %s\n", strerror(errno)); + fprintf(stderr, "arcnet: Error opening socket: %s\n", strerror(errno)); fprintf(stderr, "You might need to add the following to modules.conf\n" "(or in /etc/modutils/alias on Debian with update-modules):\n" @@ -157,7 +159,7 @@ static int arcnet_bind(char *interface_name) } strncpy(ifr.ifr_name, interface_name, sizeof(ifr.ifr_name)); rv = ioctl(sock_fd, SIOCGIFHWADDR, &ifr); - if (rv != -1) /* worked okay */ + if (rv != -1) /* worked okay */ ARCNET_MAC_Address = ifr.ifr_hwaddr.sa_data[0]; /* copy this info into the local copy since bind wiped it out */ ARCNET_Socket_Address.sa_family = ARPHRD_ARCNET; @@ -176,7 +178,8 @@ static int arcnet_bind(char *interface_name) return sock_fd; } -bool arcnet_init(char *interface_name) +bool arcnet_init( + char *interface_name) { if (interface_name) ARCNET_Sock_FD = arcnet_bind(interface_name); @@ -188,11 +191,12 @@ bool arcnet_init(char *interface_name) /* function to send a PDU out the socket */ /* returns number of bytes sent on success, negative on failure */ -int arcnet_send_pdu(BACNET_ADDRESS * dest, /* destination address */ +int arcnet_send_pdu( + BACNET_ADDRESS * dest, /* destination address */ BACNET_NPDU_DATA * npdu_data, /* network information */ - uint8_t * pdu, /* any data to be sent - may be null */ + uint8_t * pdu, /* any data to be sent - may be null */ unsigned pdu_len) -{ /* number of bytes of data */ +{ /* number of bytes of data */ BACNET_ADDRESS src = { 0 }; /* source address */ int bytes = 0; uint8_t mtu[512] = { 0 }; @@ -240,19 +244,19 @@ int arcnet_send_pdu(BACNET_ADDRESS * dest, /* destination address */ sizeof(ARCNET_Socket_Address)); /* did it get sent? */ if (bytes < 0) - fprintf(stderr, "arcnet: Error sending packet: %s\n", - strerror(errno)); + fprintf(stderr, "arcnet: Error sending packet: %s\n", strerror(errno)); return bytes; } /* receives an framed packet */ /* returns the number of octets in the PDU, or zero on failure */ -uint16_t arcnet_receive(BACNET_ADDRESS * src, /* source address */ - uint8_t * pdu, /* PDU data */ - uint16_t max_pdu, /* amount of space available in the PDU */ +uint16_t arcnet_receive( + BACNET_ADDRESS * src, /* source address */ + uint8_t * pdu, /* PDU data */ + uint16_t max_pdu, /* amount of space available in the PDU */ unsigned timeout) -{ /* milliseconds to wait for a packet */ +{ /* milliseconds to wait for a packet */ int received_bytes; uint8_t buf[512] = { 0 }; /* data */ uint16_t pdu_len = 0; /* return value */ @@ -347,7 +351,8 @@ uint16_t arcnet_receive(BACNET_ADDRESS * src, /* source address */ return pdu_len; } -void arcnet_get_my_address(BACNET_ADDRESS * my_address) +void arcnet_get_my_address( + BACNET_ADDRESS * my_address) { int i = 0; @@ -362,15 +367,16 @@ void arcnet_get_my_address(BACNET_ADDRESS * my_address) return; } -void arcnet_get_broadcast_address(BACNET_ADDRESS * dest) -{ /* destination address */ - int i = 0; /* counter */ +void arcnet_get_broadcast_address( + BACNET_ADDRESS * dest) +{ /* destination address */ + int i = 0; /* counter */ if (dest) { dest->mac[0] = ARCNET_BROADCAST; dest->mac_len = 1; dest->net = BACNET_BROADCAST_NETWORK; - dest->len = 0; /* always zero when DNET is broadcast */ + dest->len = 0; /* always zero when DNET is broadcast */ for (i = 0; i < MAX_MAC_LEN; i++) { dest->adr[i] = 0; } diff --git a/bacnet-stack/ports/linux/bip-init.c b/bacnet-stack/ports/linux/bip-init.c index 6867e5ac..61a8a571 100644 --- a/bacnet-stack/ports/linux/bip-init.c +++ b/bacnet-stack/ports/linux/bip-init.c @@ -32,19 +32,21 @@ ------------------------------------------- ####COPYRIGHTEND####*/ -#include /* for standard integer types uint8_t etc. */ -#include /* for the standard bool type. */ +#include /* for standard integer types uint8_t etc. */ +#include /* for the standard bool type. */ #include "bacdcode.h" #include "bip.h" #include "net.h" bool BIP_Debug = false; -static int get_local_ifr_ioctl(char *ifname, struct ifreq *ifr, +static int get_local_ifr_ioctl( + char *ifname, + struct ifreq *ifr, int request) { int fd; - int rv; /* return value */ + int rv; /* return value */ strncpy(ifr->ifr_name, ifname, sizeof(ifr->ifr_name)); fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP); @@ -56,12 +58,14 @@ static int get_local_ifr_ioctl(char *ifname, struct ifreq *ifr, return rv; } -static int get_local_address_ioctl(char *ifname, - struct in_addr *addr, int request) +static int get_local_address_ioctl( + char *ifname, + struct in_addr *addr, + int request) { struct ifreq ifr = { {{0}} }; struct sockaddr_in *tcpip_address; - int rv; /* return value */ + int rv; /* return value */ rv = get_local_ifr_ioctl(ifname, &ifr, request); if (rv >= 0) { @@ -73,7 +77,8 @@ static int get_local_address_ioctl(char *ifname, } /* on Linux, ifname is eth0, ath0, arc0, and others. */ -static void bip_set_interface(char *ifname) +static void bip_set_interface( + char *ifname) { struct in_addr local_address; struct in_addr broadcast_address; @@ -90,8 +95,7 @@ static void bip_set_interface(char *ifname) fprintf(stderr, "IP Address: %s\n", inet_ntoa(local_address)); } /* setup local broadcast address */ - rv = get_local_address_ioctl(ifname, &broadcast_address, - SIOCGIFBRDADDR); + rv = get_local_address_ioctl(ifname, &broadcast_address, SIOCGIFBRDADDR); if (rv < 0) { broadcast_address.s_addr = ~0; } @@ -100,14 +104,14 @@ static void bip_set_interface(char *ifname) fprintf(stderr, "IP Broadcast Address: %s\n", inet_ntoa(broadcast_address)); fprintf(stderr, "UDP Port: 0x%04X [%hu]\n", - bip_get_port(), - bip_get_port()); + bip_get_port(), bip_get_port()); } } -bool bip_init(char *ifname) +bool bip_init( + char *ifname) { - int status = 0; /* return from socket lib calls */ + int status = 0; /* return from socket lib calls */ struct sockaddr_in sin; int sockopt = 0; int sock_fd = -1; diff --git a/bacnet-stack/ports/linux/dlmstp.c b/bacnet-stack/ports/linux/dlmstp.c index e68c86dd..09dccdfc 100644 --- a/bacnet-stack/ports/linux/dlmstp.c +++ b/bacnet-stack/ports/linux/dlmstp.c @@ -63,26 +63,30 @@ static uint8_t RxBuffer[MAX_MPDU]; /* Timer that indicates line silence - and functions */ static uint16_t SilenceTime; #define INCREMENT_AND_LIMIT_UINT16(x) {if (x < 0xFFFF) x++;} -static uint16_t Timer_Silence(void) +static uint16_t Timer_Silence( + void) { return SilenceTime; } -static void Timer_Silence_Reset(void) +static void Timer_Silence_Reset( + void) { SilenceTime = 0; } -static void dlmstp_millisecond_timer(void) +static void dlmstp_millisecond_timer( + void) { INCREMENT_AND_LIMIT_UINT16(SilenceTime); } -static void *dlmstp_milliseconds_task(void *pArg) +static void *dlmstp_milliseconds_task( + void *pArg) { - struct timespec timeOut,remains; + struct timespec timeOut, remains; timeOut.tv_sec = 0; - timeOut.tv_nsec = 1000000; /* 1 millisecond */ + timeOut.tv_nsec = 1000000; /* 1 millisecond */ for (;;) { nanosleep(&timeOut, &remains); @@ -92,7 +96,8 @@ static void *dlmstp_milliseconds_task(void *pArg) return NULL; } -void dlmstp_reinit(void) +void dlmstp_reinit( + void) { /*RS485_Reinit(); */ dlmstp_set_mac_address(DEFAULT_MAC_ADDRESS); @@ -101,22 +106,21 @@ void dlmstp_reinit(void) } /* returns number of bytes sent on success, zero on failure */ -int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */ +int dlmstp_send_pdu( + BACNET_ADDRESS * dest, /* destination address */ BACNET_NPDU_DATA * npdu_data, /* network information */ - uint8_t * pdu, /* any data to be sent - may be null */ + uint8_t * pdu, /* any data to be sent - may be null */ unsigned pdu_len) -{ /* number of bytes of data */ +{ /* number of bytes of data */ DLMSTP_PACKET packet; int bytes_sent = 0; ssize_t rc = 0; if (pdu_len) { if (npdu_data->data_expecting_reply) { - packet.frame_type = - FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY; + packet.frame_type = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY; } else { - packet.frame_type = - FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; + packet.frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; } packet.pdu_len = pdu_len; memmove(&packet.pdu[0], &pdu[0], pdu_len); @@ -132,11 +136,11 @@ int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */ /* copy the packet if one is received. Return the length of the packet */ uint16_t dlmstp_receive( - BACNET_ADDRESS * src, /* source address */ - uint8_t * pdu, /* PDU data */ - uint16_t max_pdu, /* amount of space available in the PDU */ - unsigned timeout) /* milliseconds to wait for a packet */ -{ + BACNET_ADDRESS * src, /* source address */ + uint8_t * pdu, /* PDU data */ + uint16_t max_pdu, /* amount of space available in the PDU */ + unsigned timeout) +{ /* milliseconds to wait for a packet */ uint16_t pdu_len = 0; int received_bytes = 0; DLMSTP_PACKET packet; @@ -161,10 +165,7 @@ uint16_t dlmstp_receive( max = Receive_Client_SockFD; if (select(max + 1, &read_fds, NULL, NULL, &select_timeout) > 0) { - received_bytes = read( - Receive_Client_SockFD, - &packet, - sizeof(packet)); + received_bytes = read(Receive_Client_SockFD, &packet, sizeof(packet)); } else { return 0; } @@ -198,7 +199,8 @@ uint16_t dlmstp_receive( return pdu_len; } -static void *dlmstp_fsm_receive_task(void *pArg) +static void *dlmstp_fsm_receive_task( + void *pArg) { bool received_frame = false; @@ -221,12 +223,13 @@ static void *dlmstp_fsm_receive_task(void *pArg) return NULL; } -static void *dlmstp_fsm_master_task(void *pArg) +static void *dlmstp_fsm_master_task( + void *pArg) { - struct timespec timeOut,remains; + struct timespec timeOut, remains; timeOut.tv_sec = 0; - timeOut.tv_nsec = 100000; /* .1 millisecond */ + timeOut.tv_nsec = 100000; /* .1 millisecond */ for (;;) { nanosleep(&timeOut, &remains); @@ -241,7 +244,9 @@ static void *dlmstp_fsm_master_task(void *pArg) return NULL; } -void dlmstp_fill_bacnet_address(BACNET_ADDRESS * src, uint8_t mstp_address) +void dlmstp_fill_bacnet_address( + BACNET_ADDRESS * src, + uint8_t mstp_address) { int i = 0; @@ -266,7 +271,7 @@ void dlmstp_fill_bacnet_address(BACNET_ADDRESS * src, uint8_t mstp_address) /* for the MS/TP state machine to use for putting received data */ uint16_t MSTP_Put_Receive( - volatile struct mstp_port_struct_t *mstp_port) + volatile struct mstp_port_struct_t *mstp_port) { DLMSTP_PACKET packet; uint16_t pdu_len = mstp_port->DataLength; @@ -276,11 +281,8 @@ uint16_t MSTP_Put_Receive( pdu_len = sizeof(packet.pdu); if (pdu_len) { MSTP_Packets++; - memmove(&packet.pdu[0], - (void *) & mstp_port->InputBuffer[0], - pdu_len); - dlmstp_fill_bacnet_address(&packet.address, - mstp_port->SourceAddress); + memmove(&packet.pdu[0], (void *) &mstp_port->InputBuffer[0], pdu_len); + dlmstp_fill_bacnet_address(&packet.address, mstp_port->SourceAddress); packet.pdu_len = pdu_len; /* ready is not used in this scheme */ packet.ready = true; @@ -293,10 +295,10 @@ uint16_t MSTP_Put_Receive( /* for the MS/TP state machine to use for getting data to send */ /* Return: amount of PDU data */ int dlmstp_get_transmit_packet( - DLMSTP_PACKET *packet, - unsigned timeout) /* milliseconds to wait for a packet */ -{ - int received_bytes = 0; /* return value */ + DLMSTP_PACKET * packet, + unsigned timeout) +{ /* milliseconds to wait for a packet */ + int received_bytes = 0; /* return value */ struct timeval select_timeout; fd_set read_fds; int max = 0; @@ -318,10 +320,8 @@ int dlmstp_get_transmit_packet( max = Transmit_Client_SockFD; if (select(max + 1, &read_fds, NULL, NULL, &select_timeout) > 0) { - received_bytes = read( - Transmit_Client_SockFD, - packet, - sizeof(DLMSTP_PACKET)); + received_bytes = read(Transmit_Client_SockFD, + packet, sizeof(DLMSTP_PACKET)); } else { return 0; } @@ -347,10 +347,10 @@ int dlmstp_get_transmit_packet( /* for the MS/TP state machine to use for getting data to send */ /* Return: amount of PDU data */ uint16_t MSTP_Get_Send( - volatile struct mstp_port_struct_t *mstp_port, - unsigned timeout) /* milliseconds to wait for a packet */ -{ - uint16_t pdu_len = 0; /* return value */ + volatile struct mstp_port_struct_t * mstp_port, + unsigned timeout) +{ /* milliseconds to wait for a packet */ + uint16_t pdu_len = 0; /* return value */ int received_bytes = 0; DLMSTP_PACKET packet; uint8_t destination = 0; /* destination address */ @@ -368,25 +368,21 @@ uint16_t MSTP_Get_Send( return 0; } /* convert the PDU into the MSTP Frame */ - pdu_len = MSTP_Create_Frame( - &mstp_port->OutputBuffer[0], /* <-- loading this */ + pdu_len = MSTP_Create_Frame(&mstp_port->OutputBuffer[0], /* <-- loading this */ mstp_port->OutputBufferSize, packet.frame_type, - destination, - mstp_port->This_Station, - &packet.pdu[0], - packet.pdu_len); + destination, mstp_port->This_Station, &packet.pdu[0], packet.pdu_len); return pdu_len; } bool dlmstp_compare_data_expecting_reply( - uint8_t *request_pdu, + uint8_t * request_pdu, uint16_t request_pdu_len, uint8_t src_address, - uint8_t *reply_pdu, + uint8_t * reply_pdu, uint16_t reply_pdu_len, - BACNET_ADDRESS *dest_address) + BACNET_ADDRESS * dest_address) { uint16_t offset; /* One way to check the message is to compare NPDU @@ -414,12 +410,12 @@ bool dlmstp_compare_data_expecting_reply( if (request.pdu_type != PDU_TYPE_CONFIRMED_SERVICE_REQUEST) { return false; } - request.invoke_id = request_pdu[offset+2]; + request.invoke_id = request_pdu[offset + 2]; /* segmented message? */ if (request_pdu[offset] & BIT3) - request.service_choice = request_pdu[offset+5]; + request.service_choice = request_pdu[offset + 5]; else - request.service_choice = request_pdu[offset+3]; + request.service_choice = request_pdu[offset + 3]; /* decode the reply data */ bacnet_address_copy(&reply.address, dest_address); offset = npdu_decode(&reply_pdu[0], @@ -432,32 +428,32 @@ bool dlmstp_compare_data_expecting_reply( reply.pdu_type = reply_pdu[offset] & 0xF0; switch (reply.pdu_type) { case PDU_TYPE_CONFIRMED_SERVICE_REQUEST: - reply.invoke_id = reply_pdu[offset+2]; + reply.invoke_id = reply_pdu[offset + 2]; /* segmented message? */ if (reply_pdu[offset] & BIT3) - reply.service_choice = reply_pdu[offset+5]; + reply.service_choice = reply_pdu[offset + 5]; else - reply.service_choice = reply_pdu[offset+3]; + reply.service_choice = reply_pdu[offset + 3]; break; case PDU_TYPE_SIMPLE_ACK: - reply.invoke_id = reply_pdu[offset+1]; - reply.service_choice = reply_pdu[offset+2]; + reply.invoke_id = reply_pdu[offset + 1]; + reply.service_choice = reply_pdu[offset + 2]; break; case PDU_TYPE_COMPLEX_ACK: - reply.invoke_id = reply_pdu[offset+1]; + reply.invoke_id = reply_pdu[offset + 1]; /* segmented message? */ if (reply_pdu[offset] & BIT3) - reply.service_choice = reply_pdu[offset+4]; + reply.service_choice = reply_pdu[offset + 4]; else - reply.service_choice = reply_pdu[offset+2]; + reply.service_choice = reply_pdu[offset + 2]; break; case PDU_TYPE_ERROR: - reply.invoke_id = reply_pdu[offset+1]; - reply.service_choice = reply_pdu[offset+2]; + reply.invoke_id = reply_pdu[offset + 1]; + reply.service_choice = reply_pdu[offset + 2]; break; case PDU_TYPE_REJECT: case PDU_TYPE_ABORT: - reply.invoke_id = reply_pdu[offset+1]; + reply.invoke_id = reply_pdu[offset + 1]; break; default: return false; @@ -490,12 +486,12 @@ bool dlmstp_compare_data_expecting_reply( } uint16_t MSTP_Get_Reply( - volatile struct mstp_port_struct_t *mstp_port, - unsigned timeout) /* milliseconds to wait for a packet */ -{ + volatile struct mstp_port_struct_t * mstp_port, + unsigned timeout) +{ /* milliseconds to wait for a packet */ int received_bytes = 0; DLMSTP_PACKET Transmit_Packet; - uint16_t pdu_len = 0; /* return value */ + uint16_t pdu_len = 0; /* return value */ uint8_t destination = 0; /* destination address */ bool matched = false; @@ -508,39 +504,36 @@ uint16_t MSTP_Get_Reply( } else { return 0; } - if ((MAX_HEADER + Transmit_Packet.pdu_len) > MAX_MPDU) { + if ((MAX_HEADER + Transmit_Packet.pdu_len) > MAX_MPDU) { return 0; } /* is this the reply to the DER? */ - matched = dlmstp_compare_data_expecting_reply( - &mstp_port->InputBuffer[0], + matched = dlmstp_compare_data_expecting_reply(&mstp_port->InputBuffer[0], mstp_port->DataLength, mstp_port->SourceAddress, &Transmit_Packet.pdu[0], - Transmit_Packet.pdu_len, - &Transmit_Packet.address); + Transmit_Packet.pdu_len, &Transmit_Packet.address); if (matched) { /* convert the PDU into the MSTP Frame */ - pdu_len = MSTP_Create_Frame( - &mstp_port->OutputBuffer[0], /* <-- loading this */ + pdu_len = MSTP_Create_Frame(&mstp_port->OutputBuffer[0], /* <-- loading this */ mstp_port->OutputBufferSize, Transmit_Packet.frame_type, destination, mstp_port->This_Station, - &Transmit_Packet.pdu[0], - Transmit_Packet.pdu_len); + &Transmit_Packet.pdu[0], Transmit_Packet.pdu_len); /* not used here, but setting it anyway */ Transmit_Packet.ready = false; } else { /* put it back into the queue */ - (void)write(Transmit_Server_SockFD, - &Transmit_Packet, sizeof(Transmit_Packet)); + (void) write(Transmit_Server_SockFD, + &Transmit_Packet, sizeof(Transmit_Packet)); } return pdu_len; } -void dlmstp_set_mac_address(uint8_t mac_address) +void dlmstp_set_mac_address( + uint8_t mac_address) { /* Master Nodes can only have address 0-127 */ if (mac_address <= 127) { @@ -557,7 +550,8 @@ void dlmstp_set_mac_address(uint8_t mac_address) return; } -uint8_t dlmstp_my_address(void) +uint8_t dlmstp_my_address( + void) { return MSTP_Port.This_Station; } @@ -569,7 +563,8 @@ uint8_t dlmstp_my_address(void) /* nodes. This may be used to allocate more or less of the available link */ /* bandwidth to particular nodes. If Max_Info_Frames is not writable in a */ /* node, its value shall be 1. */ -void dlmstp_set_max_info_frames(uint8_t max_info_frames) +void dlmstp_set_max_info_frames( + uint8_t max_info_frames) { if (max_info_frames >= 1) { MSTP_Port.Nmax_info_frames = max_info_frames; @@ -583,7 +578,8 @@ void dlmstp_set_max_info_frames(uint8_t max_info_frames) return; } -uint8_t dlmstp_max_info_frames(void) +uint8_t dlmstp_max_info_frames( + void) { return MSTP_Port.Nmax_info_frames; } @@ -593,7 +589,8 @@ uint8_t dlmstp_max_info_frames(void) /* allowable address for master nodes. The value of Max_Master shall be */ /* less than or equal to 127. If Max_Master is not writable in a node, */ /* its value shall be 127. */ -void dlmstp_set_max_master(uint8_t max_master) +void dlmstp_set_max_master( + uint8_t max_master) { if (max_master <= 127) { if (MSTP_Port.This_Station <= max_master) { @@ -609,14 +606,16 @@ void dlmstp_set_max_master(uint8_t max_master) return; } -uint8_t dlmstp_max_master(void) +uint8_t dlmstp_max_master( + void) { return MSTP_Port.Nmax_master; } -void dlmstp_get_my_address(BACNET_ADDRESS * my_address) +void dlmstp_get_my_address( + BACNET_ADDRESS * my_address) { - int i = 0; /* counter */ + int i = 0; /* counter */ my_address->mac_len = 1; my_address->mac[0] = MSTP_Port.This_Station; @@ -629,15 +628,16 @@ void dlmstp_get_my_address(BACNET_ADDRESS * my_address) return; } -void dlmstp_get_broadcast_address(BACNET_ADDRESS * dest) -{ /* destination address */ - int i = 0; /* counter */ +void dlmstp_get_broadcast_address( + BACNET_ADDRESS * dest) +{ /* destination address */ + int i = 0; /* counter */ if (dest) { dest->mac_len = 1; dest->mac[0] = MSTP_BROADCAST_ADDRESS; dest->net = BACNET_BROADCAST_NETWORK; - dest->len = 0; /* always zero when DNET is broadcast */ + dest->len = 0; /* always zero when DNET is broadcast */ for (i = 0; i < MAX_MAC_LEN; i++) { dest->adr[i] = 0; } @@ -647,84 +647,90 @@ void dlmstp_get_broadcast_address(BACNET_ADDRESS * dest) } /* RS485 Baud Rate 9600, 19200, 38400, 57600, 115200 */ -void dlmstp_set_baud_rate(uint32_t baud) +void dlmstp_set_baud_rate( + uint32_t baud) { RS485_Set_Baud_Rate(baud); } -uint32_t dlmstp_baud_rate(void) +uint32_t dlmstp_baud_rate( + void) { - return RS485_Get_Baud_Rate(); + return RS485_Get_Baud_Rate(); } -static int create_named_server_socket (const char *filename) +static int create_named_server_socket( + const char *filename) { struct sockaddr_un name; int sock; size_t size; /* Create the socket. */ - sock = socket (PF_LOCAL, SOCK_DGRAM, 0); + sock = socket(PF_LOCAL, SOCK_DGRAM, 0); if (sock < 0) { #if PRINT_ENABLED - perror ("socket"); + perror("socket"); #endif - exit (EXIT_FAILURE); + exit(EXIT_FAILURE); } setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &sock, sizeof(sock)); /* Bind a name to the socket. */ name.sun_family = AF_LOCAL; - strncpy (name.sun_path, filename, sizeof (name.sun_path)); + strncpy(name.sun_path, filename, sizeof(name.sun_path)); /* The size of the address is - the offset of the start of the filename, - plus its length, - plus one for the terminating null byte. - Alternatively you can just do: - size = SUN_LEN (&name); - */ - size = (offsetof (struct sockaddr_un, sun_path) - + strlen (name.sun_path) + 1); - if (bind (sock, (struct sockaddr *) &name, size) < 0) { + the offset of the start of the filename, + plus its length, + plus one for the terminating null byte. + Alternatively you can just do: + size = SUN_LEN (&name); + */ + size = (offsetof(struct sockaddr_un, + sun_path) + + strlen (name.sun_path) + 1); + if (bind(sock, (struct sockaddr *) &name, size) < 0) { #if PRINT_ENABLED - perror ("bind"); + perror("bind"); #endif - exit (EXIT_FAILURE); + exit(EXIT_FAILURE); } return sock; } /* used by the client */ -static int connect_named_server_socket (const char *filename) +static int connect_named_server_socket( + const char *filename) { struct sockaddr_un name; int sock; size_t size; /* Create the socket. */ - sock = socket (PF_LOCAL, SOCK_DGRAM, 0); + sock = socket(PF_LOCAL, SOCK_DGRAM, 0); if (sock < 0) { #if PRINT_ENABLED - perror ("socket"); + perror("socket"); #endif - exit (EXIT_FAILURE); + exit(EXIT_FAILURE); } - strncpy (name.sun_path, filename, sizeof (name.sun_path)); + strncpy(name.sun_path, filename, sizeof(name.sun_path)); /* The size of the address is - the offset of the start of the filename, - plus its length, - plus one for the terminating null byte. - Alternatively you can just do: - size = SUN_LEN (&name); - */ - size = (offsetof (struct sockaddr_un, sun_path) - + strlen (name.sun_path) + 1); + the offset of the start of the filename, + plus its length, + plus one for the terminating null byte. + Alternatively you can just do: + size = SUN_LEN (&name); + */ + size = (offsetof(struct sockaddr_un, + sun_path) + + strlen (name.sun_path) + 1); if (connect(sock, (struct sockaddr *) &name, size) < 0) { #if PRINT_ENABLED - perror ("client: can't connect to socket"); + perror("client: can't connect to socket"); #endif - exit (EXIT_FAILURE); + exit(EXIT_FAILURE); } return sock; @@ -733,7 +739,8 @@ static int connect_named_server_socket (const char *filename) static char *dlmstp_transmit_socket_name = "/tmp/BACnet_MSTP_Tx"; static char *dlmstp_receive_socket_name = "/tmp/BACnet_MSTP_Rx"; -void dlmstp_cleanup(void) +void dlmstp_cleanup( + void) { remove(dlmstp_transmit_socket_name); remove(dlmstp_receive_socket_name); @@ -741,7 +748,8 @@ void dlmstp_cleanup(void) /* nothing to do for static buffers */ } -bool dlmstp_init(char *ifname) +bool dlmstp_init( + char *ifname) { int rc = 0; pthread_t hThread; @@ -763,7 +771,7 @@ bool dlmstp_init(char *ifname) if (ifname) { RS485_Set_Interface(ifname); #if PRINT_ENABLED - fprintf(stderr,"MS/TP Interface: %s\n", ifname); + fprintf(stderr, "MS/TP Interface: %s\n", ifname); #endif } RS485_Initialize(); @@ -790,12 +798,9 @@ bool dlmstp_init(char *ifname) dlmstp_set_max_info_frames(DEFAULT_MAX_INFO_FRAMES); #endif #if PRINT_ENABLED - fprintf(stderr,"MS/TP MAC: %02X\n", - MSTP_Port.This_Station); - fprintf(stderr,"MS/TP Max_Master: %02X\n", - MSTP_Port.Nmax_master); - fprintf(stderr,"MS/TP Max_Info_Frames: %u\n", - MSTP_Port.Nmax_info_frames); + fprintf(stderr, "MS/TP MAC: %02X\n", MSTP_Port.This_Station); + fprintf(stderr, "MS/TP Max_Master: %02X\n", MSTP_Port.Nmax_master); + fprintf(stderr, "MS/TP Max_Info_Frames: %u\n", MSTP_Port.Nmax_info_frames); #endif /* start our MilliSec task */ @@ -812,16 +817,18 @@ bool dlmstp_init(char *ifname) static char *Network_Interface = NULL; -int main(int argc, char *argv[]) +int main( + int argc, + char *argv[]) { uint16_t bytes_received = 0; BACNET_ADDRESS src; /* source address */ - uint8_t pdu[MAX_APDU]; /* PDU data */ + uint8_t pdu[MAX_APDU]; /* PDU data */ #if (defined(MSTP_TEST_REQUEST) && MSTP_TEST_REQUEST) struct timespec timeOut, remains; timeOut.tv_sec = 1; - timeOut.tv_nsec = 0; /* 1 millisecond */ + timeOut.tv_nsec = 0; /* 1 millisecond */ #endif /* argv has the "/dev/ttyS0" or some other device */ if (argc > 1) { @@ -835,18 +842,14 @@ int main(int argc, char *argv[]) /* forever task */ for (;;) { #if (defined(MSTP_TEST_REQUEST) && MSTP_TEST_REQUEST) - MSTP_Create_And_Send_Frame( - &MSTP_Port, + MSTP_Create_And_Send_Frame(&MSTP_Port, FRAME_TYPE_TEST_REQUEST, - MSTP_Port.SourceAddress, - MSTP_Port.This_Station, - NULL, 0); + MSTP_Port.SourceAddress, MSTP_Port.This_Station, NULL, 0); nanosleep(&timeOut, &remains); #endif - bytes_received = - dlmstp_receive(&src, &pdu[0], sizeof(pdu), 10000); + bytes_received = dlmstp_receive(&src, &pdu[0], sizeof(pdu), 10000); if (bytes_received) { - fprintf(stderr,"Received NPDU!\n"); + fprintf(stderr, "Received NPDU!\n"); } } return 0; diff --git a/bacnet-stack/ports/linux/ethernet.c b/bacnet-stack/ports/linux/ethernet.c index 640d23e1..866099be 100644 --- a/bacnet-stack/ports/linux/ethernet.c +++ b/bacnet-stack/ports/linux/ethernet.c @@ -32,8 +32,8 @@ ------------------------------------------- ####COPYRIGHTEND####*/ -#include /* for standard integer types uint8_t etc. */ -#include /* for the standard bool type. */ +#include /* for standard integer types uint8_t etc. */ +#include /* for the standard bool type. */ #include "net.h" #include "bacdef.h" @@ -52,12 +52,14 @@ uint8_t Ethernet_MAC_Address[MAX_MAC_LEN] = { 0 }; static int eth802_sockfd = -1; /* 802.2 file handle */ static struct sockaddr eth_addr = { 0 }; /* used for binding 802.2 */ -bool ethernet_valid(void) +bool ethernet_valid( + void) { return (eth802_sockfd >= 0); } -void ethernet_cleanup(void) +void ethernet_cleanup( + void) { if (ethernet_valid()) close(eth802_sockfd); @@ -75,7 +77,8 @@ void ethernet_cleanup(void) to EAGAIN (or EWOULDBLOCK). Thanks to Bjorn Reese for this code. ----------------------------------------------------------------------*/ -int setNonblocking(int fd) +int setNonblocking( + int fd) { int flags; @@ -85,9 +88,11 @@ int setNonblocking(int fd) } /* opens an 802.2 socket to receive and send packets */ -static int ethernet_bind(struct sockaddr *eth_addr, char *interface_name) +static int ethernet_bind( + struct sockaddr *eth_addr, + char *interface_name) { - int sock_fd = -1; /* return value */ + int sock_fd = -1; /* return value */ int uid = 0; fprintf(stderr, "ethernet: opening \"%s\"\n", interface_name); @@ -122,15 +127,13 @@ static int ethernet_bind(struct sockaddr *eth_addr, char *interface_name) /* Clear the memory before copying */ memset(eth_addr->sa_data, '\0', sizeof(eth_addr->sa_data)); /* Strcpy the interface name into the address */ - strncpy(eth_addr->sa_data, interface_name, - sizeof(eth_addr->sa_data) - 1); + strncpy(eth_addr->sa_data, interface_name, sizeof(eth_addr->sa_data) - 1); fprintf(stderr, "ethernet: binding \"%s\"\n", eth_addr->sa_data); /* Attempt to bind the socket to the interface */ if (bind(sock_fd, eth_addr, sizeof(struct sockaddr)) != 0) { /* Bind problem, close socket and return */ fprintf(stderr, - "ethernet: Unable to bind 802.2 socket : %s\n", - strerror(errno)); + "ethernet: Unable to bind 802.2 socket : %s\n", strerror(errno)); fprintf(stderr, "You might need to add the following to modules.conf\n" "(or in /etc/modutils/alias on Debian with update-modules):\n" @@ -148,11 +151,13 @@ static int ethernet_bind(struct sockaddr *eth_addr, char *interface_name) } /* function to find the local ethernet MAC address */ -static int get_local_hwaddr(const char *ifname, unsigned char *mac) +static int get_local_hwaddr( + const char *ifname, + unsigned char *mac) { struct ifreq ifr; int fd; - int rv; /* return value - error value from df or ioctl call */ + int rv; /* return value - error value from df or ioctl call */ /* determine the local MAC address */ strcpy(ifr.ifr_name, ifname); @@ -161,14 +166,15 @@ static int get_local_hwaddr(const char *ifname, unsigned char *mac) rv = fd; else { rv = ioctl(fd, SIOCGIFHWADDR, &ifr); - if (rv >= 0) /* worked okay */ + if (rv >= 0) /* worked okay */ memcpy(mac, ifr.ifr_hwaddr.sa_data, IFHWADDRLEN); } return rv; } -bool ethernet_init(char *interface_name) +bool ethernet_init( + char *interface_name) { if (interface_name) { get_local_hwaddr(interface_name, Ethernet_MAC_Address); @@ -183,12 +189,13 @@ bool ethernet_init(char *interface_name) /* function to send a packet out the 802.2 socket */ /* returns number of bytes sent on success, negative on failure */ -int ethernet_send_pdu(BACNET_ADDRESS * dest, /* destination address */ +int ethernet_send_pdu( + BACNET_ADDRESS * dest, /* destination address */ BACNET_NPDU_DATA * npdu_data, /* network information */ - uint8_t * pdu, /* any data to be sent - may be null */ + uint8_t * pdu, /* any data to be sent - may be null */ unsigned pdu_len) -{ /* number of bytes of data */ - int i = 0; /* counter */ +{ /* number of bytes of data */ + int i = 0; /* counter */ int bytes = 0; BACNET_ADDRESS src = { 0 }; /* source address for npdu */ uint8_t mtu[MAX_MPDU] = { 0 }; /* our buffer */ @@ -226,9 +233,9 @@ int ethernet_send_pdu(BACNET_ADDRESS * dest, /* destination address */ return -3; } /* Logical PDU portion */ - mtu[14] = 0x82; /* DSAP for BACnet */ - mtu[15] = 0x82; /* SSAP for BACnet */ - mtu[16] = 0x03; /* Control byte in header */ + mtu[14] = 0x82; /* DSAP for BACnet */ + mtu[15] = 0x82; /* SSAP for BACnet */ + mtu[16] = 0x03; /* Control byte in header */ mtu_len = 17; if ((mtu_len + pdu_len) > MAX_MPDU) { fprintf(stderr, "ethernet: PDU is too big to send!\n"); @@ -253,11 +260,12 @@ int ethernet_send_pdu(BACNET_ADDRESS * dest, /* destination address */ /* receives an 802.2 framed packet */ /* returns the number of octets in the PDU, or zero on failure */ -uint16_t ethernet_receive(BACNET_ADDRESS * src, /* source address */ - uint8_t * pdu, /* PDU data */ - uint16_t max_pdu, /* amount of space available in the PDU */ +uint16_t ethernet_receive( + BACNET_ADDRESS * src, /* source address */ + uint8_t * pdu, /* PDU data */ + uint16_t max_pdu, /* amount of space available in the PDU */ unsigned timeout) -{ /* number of milliseconds to wait for a packet */ +{ /* number of milliseconds to wait for a packet */ int received_bytes; uint8_t buf[MAX_MPDU] = { 0 }; /* data */ uint16_t pdu_len = 0; /* return value */ @@ -334,7 +342,8 @@ uint16_t ethernet_receive(BACNET_ADDRESS * src, /* source address */ return pdu_len; } -void ethernet_set_my_address(BACNET_ADDRESS * my_address) +void ethernet_set_my_address( + BACNET_ADDRESS * my_address) { int i = 0; @@ -345,7 +354,8 @@ void ethernet_set_my_address(BACNET_ADDRESS * my_address) return; } -void ethernet_get_my_address(BACNET_ADDRESS * my_address) +void ethernet_get_my_address( + BACNET_ADDRESS * my_address) { int i = 0; @@ -363,9 +373,10 @@ void ethernet_get_my_address(BACNET_ADDRESS * my_address) return; } -void ethernet_get_broadcast_address(BACNET_ADDRESS * dest) -{ /* destination address */ - int i = 0; /* counter */ +void ethernet_get_broadcast_address( + BACNET_ADDRESS * dest) +{ /* destination address */ + int i = 0; /* counter */ if (dest) { for (i = 0; i < 6; i++) { @@ -373,7 +384,7 @@ void ethernet_get_broadcast_address(BACNET_ADDRESS * dest) } dest->mac_len = 6; dest->net = BACNET_BROADCAST_NETWORK; - dest->len = 0; /* always zero when DNET is broadcast */ + dest->len = 0; /* always zero when DNET is broadcast */ for (i = 0; i < MAX_MAC_LEN; i++) { dest->adr[i] = 0; } @@ -382,9 +393,11 @@ void ethernet_get_broadcast_address(BACNET_ADDRESS * dest) return; } -void ethernet_debug_address(const char *info, BACNET_ADDRESS * dest) +void ethernet_debug_address( + const char *info, + BACNET_ADDRESS * dest) { - int i = 0; /* counter */ + int i = 0; /* counter */ if (info) fprintf(stderr, "%s", info); diff --git a/bacnet-stack/ports/linux/main.c b/bacnet-stack/ports/linux/main.c index 7075ea49..b80727aa 100644 --- a/bacnet-stack/ports/linux/main.c +++ b/bacnet-stack/ports/linux/main.c @@ -50,8 +50,10 @@ bool Who_Is_Request = true; /* buffers used for receiving */ static uint8_t Rx_Buf[MAX_MPDU] = { 0 }; -static void LocalIAmHandler(uint8_t * service_request, - uint16_t service_len, BACNET_ADDRESS * src) +static void LocalIAmHandler( + uint8_t * service_request, + uint16_t service_len, + BACNET_ADDRESS * src) { int len = 0; uint32_t device_id = 0; @@ -73,7 +75,8 @@ static void LocalIAmHandler(uint8_t * service_request, return; } -static void Read_Properties(void) +static void Read_Properties( + void) { uint32_t device_id = 0; bool status = false; @@ -154,13 +157,12 @@ static void Read_Properties(void) return; } -static void Init_Service_Handlers(void) +static void Init_Service_Handlers( + void) { /* we need to handle who-is to support dynamic device binding */ - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, - handler_who_is); - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, - LocalIAmHandler); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, LocalIAmHandler); /* set the handler for all the services we don't implement */ /* It is required to send the proper reject message... */ @@ -181,7 +183,8 @@ static void Init_Service_Handlers(void) handler_atomic_read_file_ack); } -static void print_address_cache(void) +static void print_address_cache( + void) { unsigned i, j; BACNET_ADDRESS address; @@ -202,7 +205,8 @@ static void print_address_cache(void) } } -static void print_tsm_stats(void) +static void print_tsm_stats( + void) { int idle = 0; int total = 0; @@ -212,7 +216,8 @@ static void print_tsm_stats(void) fprintf(stderr, "TSM: %d idle of %d transactions\n", idle, total); } -static void sig_handler(int signo) +static void sig_handler( + int signo) { datalink_cleanup(); print_address_cache(); @@ -221,12 +226,14 @@ static void sig_handler(int signo) exit(0); } -int main(int argc, char *argv[]) +int main( + int argc, + char *argv[]) { BACNET_ADDRESS src = { 0 }; /* address where message came from */ uint16_t pdu_len = 0; unsigned timeout = 100; /* milliseconds */ - unsigned count = 0; /* milliseconds */ + unsigned count = 0; /* milliseconds */ time_t start_time; time_t new_time = 0; @@ -280,15 +287,15 @@ int main(int argc, char *argv[]) /* some round robin task switching */ count++; switch (count) { - case 1: - /* used for testing, but kind of noisy on the network */ - /*Read_Properties(); */ - break; - case 2: - break; - default: - count = 0; - break; + case 1: + /* used for testing, but kind of noisy on the network */ + /*Read_Properties(); */ + break; + case 2: + break; + default: + count = 0; + break; } /* blink LEDs, Turn on or off outputs, etc */ diff --git a/bacnet-stack/ports/linux/net.h b/bacnet-stack/ports/linux/net.h index 1afeabce..fce5948f 100644 --- a/bacnet-stack/ports/linux/net.h +++ b/bacnet-stack/ports/linux/net.h @@ -80,7 +80,7 @@ #include #include #include -#include /* for the glibc version number */ +#include /* for the glibc version number */ #if __GLIBC__ >= 2 && __GLIBC_MINOR >= 1 #include #include /* the L2 protocols */ diff --git a/bacnet-stack/ports/linux/rs485.c b/bacnet-stack/ports/linux/rs485.c index 355fb204..78854943 100644 --- a/bacnet-stack/ports/linux/rs485.c +++ b/bacnet-stack/ports/linux/rs485.c @@ -76,7 +76,8 @@ static struct termios RS485_oldtio; * ALGORITHM: none * NOTES: none *********************************************************************/ -void RS485_Set_Interface(char *ifname) +void RS485_Set_Interface( + char *ifname) { /* note: expects a constant char, or char from the heap */ RS485_Port_Name = ifname; @@ -88,15 +89,21 @@ void RS485_Set_Interface(char *ifname) * ALGORITHM: none * NOTES: none *****************************************************************************/ -uint32_t RS485_Get_Baud_Rate(void) +uint32_t RS485_Get_Baud_Rate( + void) { switch (RS485_Baud) { - case B19200: return 19200; - case B38400: return 38400; - case B57600: return 57600; - case B115200: return 115200; + case B19200: + return 19200; + case B38400: + return 38400; + case B57600: + return 57600; + case B115200: + return 115200; default: - case B9600: return 9600; + case B9600: + return 9600; } } @@ -106,7 +113,8 @@ uint32_t RS485_Get_Baud_Rate(void) * ALGORITHM: none * NOTES: none *****************************************************************************/ -bool RS485_Set_Baud_Rate(uint32_t baud) +bool RS485_Set_Baud_Rate( + uint32_t baud) { bool valid = true; @@ -140,10 +148,10 @@ bool RS485_Set_Baud_Rate(uint32_t baud) /* Transmits a Frame on the wire */ void RS485_Send_Frame( - struct mstp_port_struct_t *mstp_port, /* port specific data */ - uint8_t * buffer, /* frame to send (up to 501 bytes of data) */ - uint16_t nbytes) /* number of bytes of data (up to 501) */ -{ + struct mstp_port_struct_t *mstp_port, /* port specific data */ + uint8_t * buffer, /* frame to send (up to 501 bytes of data) */ + uint16_t nbytes) +{ /* number of bytes of data (up to 501) */ uint8_t turnaround_time; uint32_t baud; ssize_t written = 0; @@ -163,12 +171,12 @@ void RS485_Send_Frame( }; } /* - On success, the number of bytes written are returned (zero indicates - nothing was written). On error, -1 is returned, and errno is set - appropriately. If count is zero and the file descriptor refers to a - regular file, 0 will be returned without causing any other effect. For - a special file, the results are not portable. - */ + On success, the number of bytes written are returned (zero indicates + nothing was written). On error, -1 is returned, and errno is set + appropriately. If count is zero and the file descriptor refers to a + regular file, 0 will be returned without causing any other effect. For + a special file, the results are not portable. + */ written = write(RS485_Handle, buffer, nbytes); /* per MSTP spec, sort of */ @@ -180,18 +188,19 @@ void RS485_Send_Frame( } /* called by timer, interrupt(?) or other thread */ -void RS485_Check_UART_Data(struct mstp_port_struct_t *mstp_port) +void RS485_Check_UART_Data( + struct mstp_port_struct_t *mstp_port) { uint8_t buf[1]; int count; - + if (mstp_port->ReceiveError == true) { /* wait for state machine to clear this */ } /* wait for state machine to read from the DataRegister */ else if (mstp_port->DataAvailable == false) { /* check for data */ - count = read(RS485_Handle,buf,sizeof(buf)); + count = read(RS485_Handle, buf, sizeof(buf)); /* if error, */ /* ReceiveError = TRUE; */ /* return; */ @@ -203,23 +212,24 @@ void RS485_Check_UART_Data(struct mstp_port_struct_t *mstp_port) } } -void RS485_Cleanup(void) +void RS485_Cleanup( + void) { /* restore the old port settings */ - tcsetattr(RS485_Handle,TCSANOW,&RS485_oldtio); + tcsetattr(RS485_Handle, TCSANOW, &RS485_oldtio); close(RS485_Handle); } -void RS485_Initialize(void) +void RS485_Initialize( + void) { struct termios newtio; /* - Open device for reading and writing. - */ - RS485_Handle = open(RS485_Port_Name, - O_RDWR | O_NOCTTY | O_NDELAY ); + Open device for reading and writing. + */ + RS485_Handle = open(RS485_Port_Name, O_RDWR | O_NOCTTY | O_NDELAY); if (RS485_Handle < 0) { perror(RS485_Port_Name); exit(-1); @@ -232,17 +242,17 @@ void RS485_Initialize(void) fcntl(RS485_Handle, F_SETFL, 0); #endif /* save current serial port settings */ - tcgetattr(RS485_Handle,&RS485_oldtio); + tcgetattr(RS485_Handle, &RS485_oldtio); /* clear struct for new port settings */ bzero(&newtio, sizeof(newtio)); /* - BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed. - CRTSCTS : output hardware flow control (only used if the cable has - all necessary lines. See sect. 7 of Serial-HOWTO) - CS8 : 8n1 (8bit,no parity,1 stopbit) - CLOCAL : local connection, no modem contol - CREAD : enable receiving characters - */ + BAUDRATE: Set bps rate. You could also use cfsetispeed and cfsetospeed. + CRTSCTS : output hardware flow control (only used if the cable has + all necessary lines. See sect. 7 of Serial-HOWTO) + CS8 : 8n1 (8bit,no parity,1 stopbit) + CLOCAL : local connection, no modem contol + CREAD : enable receiving characters + */ newtio.c_cflag = RS485_Baud | CS8 | CLOCAL | CREAD; /* Raw input */ newtio.c_iflag = 0; @@ -251,18 +261,20 @@ void RS485_Initialize(void) /* no processing */ newtio.c_lflag = 0; /* activate the settings for the port after flushing I/O */ - tcsetattr(RS485_Handle,TCSAFLUSH,&newtio); + tcsetattr(RS485_Handle, TCSAFLUSH, &newtio); /* destructor */ atexit(RS485_Cleanup); } #ifdef TEST_RS485 #include -int main(int argc, char *argv[]) +int main( + int argc, + char *argv[]) { uint8_t buf[8]; - char *wbuf = {"BACnet!"}; - size_t wlen = strlen(wbuf)+1; + char *wbuf = { "BACnet!" }; + size_t wlen = strlen(wbuf) + 1; unsigned i = 0; size_t written = 0; int rlen; @@ -276,11 +288,11 @@ int main(int argc, char *argv[]) for (;;) { written = write(RS485_Handle, wbuf, wlen); - rlen = read(RS485_Handle,buf,sizeof(buf)); + rlen = read(RS485_Handle, buf, sizeof(buf)); /* print any characters received */ if (rlen > 0) { for (i = 0; i < rlen; i++) { - fprintf(stderr,"%02X ",buf[i]); + fprintf(stderr, "%02X ", buf[i]); } } } diff --git a/bacnet-stack/ports/linux/rs485.h b/bacnet-stack/ports/linux/rs485.h index e52e4dcf..81e5b269 100644 --- a/bacnet-stack/ports/linux/rs485.h +++ b/bacnet-stack/ports/linux/rs485.h @@ -41,23 +41,29 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - void RS485_Set_Interface(char *ifname); + void RS485_Set_Interface( + char *ifname); - void RS485_Initialize(void); + void RS485_Initialize( + void); - void RS485_Send_Frame(volatile struct mstp_port_struct_t *mstp_port, /* port specific data */ + void RS485_Send_Frame( + volatile struct mstp_port_struct_t *mstp_port, /* port specific data */ uint8_t * buffer, /* frame to send (up to 501 bytes of data) */ uint16_t nbytes); /* number of bytes of data (up to 501) */ void RS485_Check_UART_Data( volatile struct mstp_port_struct_t *mstp_port); /* port specific data */ - uint32_t RS485_Get_Baud_Rate(void); - bool RS485_Set_Baud_Rate(uint32_t baud); - void RS485_Cleanup(void); + uint32_t RS485_Get_Baud_Rate( + void); + bool RS485_Set_Baud_Rate( + uint32_t baud); + void RS485_Cleanup( + void); #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/ports/linux/rx_fsm.c b/bacnet-stack/ports/linux/rx_fsm.c index 80141e36..ca7a1c8b 100644 --- a/bacnet-stack/ports/linux/rx_fsm.c +++ b/bacnet-stack/ports/linux/rx_fsm.c @@ -58,23 +58,27 @@ static uint8_t RxBuffer[MAX_MPDU]; static uint8_t TxBuffer[MAX_MPDU]; static uint16_t SilenceTime; #define INCREMENT_AND_LIMIT_UINT16(x) {if (x < 0xFFFF) x++;} -static uint16_t Timer_Silence(void) +static uint16_t Timer_Silence( + void) { return SilenceTime; } -static void Timer_Silence_Reset(void) +static void Timer_Silence_Reset( + void) { SilenceTime = 0; } -static void dlmstp_millisecond_timer(void) +static void dlmstp_millisecond_timer( + void) { INCREMENT_AND_LIMIT_UINT16(SilenceTime); } - -void *milliseconds_task(void *pArg) + +void *milliseconds_task( + void *pArg) { - struct timespec timeOut,remains; + struct timespec timeOut, remains; timeOut.tv_sec = 0; timeOut.tv_nsec = 10000000; /* 1 milliseconds */ @@ -89,35 +93,36 @@ void *milliseconds_task(void *pArg) /* functions used by the MS/TP state machine to put or get data */ uint16_t MSTP_Put_Receive( - volatile struct mstp_port_struct_t *mstp_port) + volatile struct mstp_port_struct_t * mstp_port) { - (void)mstp_port; - + (void) mstp_port; + return 0; } /* for the MS/TP state machine to use for getting data to send */ /* Return: amount of PDU data */ uint16_t MSTP_Get_Send( - volatile struct mstp_port_struct_t *mstp_port, - unsigned timeout) /* milliseconds to wait for a packet */ -{ - (void)mstp_port; - (void)timeout; + volatile struct mstp_port_struct_t * mstp_port, + unsigned timeout) +{ /* milliseconds to wait for a packet */ + (void) mstp_port; + (void) timeout; return 0; } uint16_t MSTP_Get_Reply( - volatile struct mstp_port_struct_t *mstp_port, - unsigned timeout) /* milliseconds to wait for a packet */ -{ - (void)mstp_port; - (void)timeout; + volatile struct mstp_port_struct_t * mstp_port, + unsigned timeout) +{ /* milliseconds to wait for a packet */ + (void) mstp_port; + (void) timeout; return 0; } /* returns a delta timestamp */ -int timestamp_ms(void) +int timestamp_ms( + void) { struct timeval tv; int delta_ticks = 0; @@ -125,7 +130,7 @@ int timestamp_ms(void) static long last_ticks = 0; int rv = 0; - rv = gettimeofday(&tv,NULL); + rv = gettimeofday(&tv, NULL); if (rv == -1) ticks = 0; else @@ -144,7 +149,7 @@ static void print_received_packet( int timestamp = 0; timestamp = timestamp_ms(); - fprintf(stderr,"%03d ",timestamp); + fprintf(stderr, "%03d ", timestamp); /* Preamble: two octet preamble: X`55', X`FF' */ /* Frame Type: one octet */ /* Destination Address: one octet address */ @@ -161,25 +166,23 @@ static void print_received_packet( mstp_port->DestinationAddress, mstp_port->SourceAddress, HI_BYTE(mstp_port->DataLength), - LO_BYTE(mstp_port->DataLength), - mstp_port->HeaderCRCActual); + LO_BYTE(mstp_port->DataLength), mstp_port->HeaderCRCActual); if (mstp_port->DataLength) { for (i = 0; i < mstp_port->DataLength; i++) { - fprintf(stderr,"%02X ", - mstp_port->InputBuffer[i]); + fprintf(stderr, "%02X ", mstp_port->InputBuffer[i]); } fprintf(stderr, - "%02X %02X ", - mstp_port->DataCRCActualMSB, - mstp_port->DataCRCActualLSB); + "%02X %02X ", + mstp_port->DataCRCActualMSB, mstp_port->DataCRCActualLSB); } - fprintf(stderr,"%s", - mstptext_frame_type(mstp_port->FrameType)); - fprintf(stderr,"\n"); + fprintf(stderr, "%s", mstptext_frame_type(mstp_port->FrameType)); + fprintf(stderr, "\n"); } /* simple test to packetize the data and print it */ -int main(int argc, char *argv[]) +int main( + int argc, + char *argv[]) { volatile struct mstp_port_struct_t *mstp_port; int rc = 0; @@ -195,7 +198,7 @@ int main(int argc, char *argv[]) if (argc > 2) { my_mac = strtol(argv[2], NULL, 0); if (my_mac > 127) - my_mac = 127; + my_mac = 127; } RS485_Set_Baud_Rate(38400); RS485_Initialize(); @@ -229,4 +232,3 @@ int main(int argc, char *argv[]) return 0; } - diff --git a/bacnet-stack/ports/pic18f6720/ai.c b/bacnet-stack/ports/pic18f6720/ai.c index 17a646e3..c9a13544 100644 --- a/bacnet-stack/ports/pic18f6720/ai.c +++ b/bacnet-stack/ports/pic18f6720/ai.c @@ -41,7 +41,8 @@ static uint8_t Present_Value[MAX_ANALOG_INPUTS]; /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need validate that the */ /* given instance exists */ -bool Analog_Input_Valid_Instance(uint32_t object_instance) +bool Analog_Input_Valid_Instance( + uint32_t object_instance) { if (object_instance < MAX_ANALOG_INPUTS) return true; @@ -50,18 +51,21 @@ bool Analog_Input_Valid_Instance(uint32_t object_instance) } /* we simply have 0-n object instances. */ -unsigned Analog_Input_Count(void) +unsigned Analog_Input_Count( + void) { return MAX_ANALOG_INPUTS; } /* we simply have 0-n object instances. */ -uint32_t Analog_Input_Index_To_Instance(unsigned index) +uint32_t Analog_Input_Index_To_Instance( + unsigned index) { return index; } -char *Analog_Input_Name(uint32_t object_instance) +char *Analog_Input_Name( + uint32_t object_instance) { static char text_string[16] = ""; /* okay for single thread */ @@ -73,72 +77,78 @@ char *Analog_Input_Name(uint32_t object_instance) return NULL; } -static float Analog_Input_Present_Value(uint32_t object_instance) +static float Analog_Input_Present_Value( + uint32_t object_instance) { - float value = 0.0; - - if (object_instance < MAX_ANALOG_INPUTS) - value = Present_Value[object_instance]; - - return value; + float value = 0.0; + + if (object_instance < MAX_ANALOG_INPUTS) + value = Present_Value[object_instance]; + + return value; } /* return apdu length, or -1 on error */ /* assumption - object has already exists */ -int Analog_Input_Encode_Property_APDU(uint8_t * apdu, +int Analog_Input_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ BACNET_BIT_STRING bit_string; BACNET_CHARACTER_STRING char_string; (void) array_index; switch (property) { - case PROP_OBJECT_IDENTIFIER: - apdu_len = encode_application_object_id(&apdu[0], OBJECT_ANALOG_INPUT, - object_instance); - break; - /* note: Name and Description don't have to be the same. - You could make Description writable and different */ - case PROP_OBJECT_NAME: - case PROP_DESCRIPTION: - characterstring_init_ansi(&char_string, - Analog_Input_Name(object_instance)); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_OBJECT_TYPE: - apdu_len = encode_application_enumerated(&apdu[0], - OBJECT_ANALOG_INPUT); - break; - case PROP_PRESENT_VALUE: - apdu_len = encode_application_real(&apdu[0], - Analog_Input_Present_Value(object_instance)); - break; - case PROP_STATUS_FLAGS: - bitstring_init(&bit_string); - bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); - apdu_len = encode_application_bitstring(&apdu[0], &bit_string); - break; - case PROP_EVENT_STATE: - apdu_len = encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); - break; - case PROP_OUT_OF_SERVICE: - apdu_len = encode_application_boolean(&apdu[0], false); - break; - case PROP_UNITS: - apdu_len = encode_application_enumerated(&apdu[0], UNITS_PERCENT); - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_UNKNOWN_PROPERTY; - apdu_len = -1; - break; + case PROP_OBJECT_IDENTIFIER: + apdu_len = + encode_application_object_id(&apdu[0], OBJECT_ANALOG_INPUT, + object_instance); + break; + /* note: Name and Description don't have to be the same. + You could make Description writable and different */ + case PROP_OBJECT_NAME: + case PROP_DESCRIPTION: + characterstring_init_ansi(&char_string, + Analog_Input_Name(object_instance)); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_OBJECT_TYPE: + apdu_len = encode_application_enumerated(&apdu[0], + OBJECT_ANALOG_INPUT); + break; + case PROP_PRESENT_VALUE: + apdu_len = encode_application_real(&apdu[0], + Analog_Input_Present_Value(object_instance)); + break; + case PROP_STATUS_FLAGS: + bitstring_init(&bit_string); + bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); + apdu_len = encode_application_bitstring(&apdu[0], &bit_string); + break; + case PROP_EVENT_STATE: + apdu_len = + encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); + break; + case PROP_OUT_OF_SERVICE: + apdu_len = encode_application_boolean(&apdu[0], false); + break; + case PROP_UNITS: + apdu_len = encode_application_enumerated(&apdu[0], UNITS_PERCENT); + break; + default: + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_UNKNOWN_PROPERTY; + apdu_len = -1; + break; } return apdu_len; @@ -149,7 +159,8 @@ int Analog_Input_Encode_Property_APDU(uint8_t * apdu, #include #include "ctest.h" -void testAnalogInput(Test * pTest) +void testAnalogInput( + Test * pTest) { uint8_t apdu[MAX_APDU] = { 0 }; int len = 0; @@ -165,8 +176,7 @@ void testAnalogInput(Test * pTest) /* FIXME: we should do a lot more testing here... */ len = Analog_Input_Encode_Property_APDU(&apdu[0], instance, - PROP_OBJECT_IDENTIFIER, - BACNET_ARRAY_ALL, &error_class, &error_code); + PROP_OBJECT_IDENTIFIER, BACNET_ARRAY_ALL, &error_class, &error_code); ct_test(pTest, len >= 0); len = decode_tag_number_and_value(&apdu[0], &tag_number, &len_value); ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_OBJECT_ID); @@ -179,7 +189,8 @@ void testAnalogInput(Test * pTest) } #ifdef TEST_ANALOG_INPUT -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -196,5 +207,5 @@ int main(void) return 0; } -#endif /* TEST_ANALOG_INPUT */ -#endif /* TEST */ +#endif /* TEST_ANALOG_INPUT */ +#endif /* TEST */ diff --git a/bacnet-stack/ports/pic18f6720/av.c b/bacnet-stack/ports/pic18f6720/av.c index 5fee37e4..5e53e6d9 100644 --- a/bacnet-stack/ports/pic18f6720/av.c +++ b/bacnet-stack/ports/pic18f6720/av.c @@ -32,7 +32,7 @@ #include "bacdcode.h" #include "bacenum.h" #include "bacapp.h" -#include "config.h" /* the custom stuff */ +#include "config.h" /* the custom stuff */ #include "wp.h" #define MAX_ANALOG_VALUES 4 @@ -52,7 +52,8 @@ static uint8_t Present_Value[MAX_ANALOG_VALUES]; /* we need to have our arrays initialized before answering any calls */ static bool Analog_Value_Initialized = false; -void Analog_Value_Init(void) +void Analog_Value_Init( + void) { unsigned i; @@ -71,7 +72,8 @@ void Analog_Value_Init(void) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need validate that the */ /* given instance exists */ -bool Analog_Value_Valid_Instance(uint32_t object_instance) +bool Analog_Value_Valid_Instance( + uint32_t object_instance) { Analog_Value_Init(); if (object_instance < MAX_ANALOG_VALUES) @@ -82,7 +84,8 @@ bool Analog_Value_Valid_Instance(uint32_t object_instance) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then count how many you have */ -unsigned Analog_Value_Count(void) +unsigned Analog_Value_Count( + void) { Analog_Value_Init(); return MAX_ANALOG_VALUES; @@ -91,7 +94,8 @@ unsigned Analog_Value_Count(void) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need to return the instance */ /* that correlates to the correct index */ -uint32_t Analog_Value_Index_To_Instance(unsigned index) +uint32_t Analog_Value_Index_To_Instance( + unsigned index) { Analog_Value_Init(); return index; @@ -100,7 +104,8 @@ uint32_t Analog_Value_Index_To_Instance(unsigned index) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need to return the index */ /* that correlates to the correct instance number */ -unsigned Analog_Value_Instance_To_Index(uint32_t object_instance) +unsigned Analog_Value_Instance_To_Index( + uint32_t object_instance) { unsigned index = MAX_ANALOG_VALUES; @@ -111,7 +116,8 @@ unsigned Analog_Value_Instance_To_Index(uint32_t object_instance) return index; } -static float Analog_Value_Present_Value(uint32_t object_instance) +static float Analog_Value_Present_Value( + uint32_t object_instance) { float value = ANALOG_RELINQUISH_DEFAULT; unsigned index = 0; @@ -127,7 +133,8 @@ static float Analog_Value_Present_Value(uint32_t object_instance) } /* note: the object name must be unique within this device */ -char *Analog_Value_Name(uint32_t object_instance) +char *Analog_Value_Name( + uint32_t object_instance) { static char text_string[32] = ""; /* okay for single thread */ @@ -140,14 +147,16 @@ char *Analog_Value_Name(uint32_t object_instance) } /* return apdu len, or -1 on error */ -int Analog_Value_Encode_Property_APDU(uint8_t * apdu, +int Analog_Value_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { int len = 0; - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ BACNET_BIT_STRING bit_string; BACNET_CHARACTER_STRING char_string; float real_value = (float) 1.414; @@ -157,110 +166,118 @@ int Analog_Value_Encode_Property_APDU(uint8_t * apdu, Analog_Value_Init(); switch (property) { - case PROP_OBJECT_IDENTIFIER: - apdu_len = encode_application_object_id(&apdu[0], OBJECT_ANALOG_VALUE, - object_instance); - break; - case PROP_OBJECT_NAME: - case PROP_DESCRIPTION: - characterstring_init_ansi(&char_string, - Analog_Value_Name(object_instance)); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_OBJECT_TYPE: - apdu_len = encode_application_enumerated(&apdu[0], OBJECT_ANALOG_VALUE); - break; - case PROP_PRESENT_VALUE: - real_value = Analog_Value_Present_Value(object_instance); - apdu_len = encode_application_real(&apdu[0], real_value); - break; - case PROP_STATUS_FLAGS: - bitstring_init(&bit_string); - bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); - apdu_len = encode_application_bitstring(&apdu[0], &bit_string); - break; - case PROP_EVENT_STATE: - apdu_len = encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); - break; - case PROP_OUT_OF_SERVICE: -#if 0 - object_index = Analog_Value_Instance_To_Index(object_instance); - state = Analog_Value_Out_Of_Service[object_index]; -#endif - apdu_len = encode_application_boolean(&apdu[0], false); - break; - case PROP_UNITS: - apdu_len = encode_application_enumerated(&apdu[0], UNITS_PERCENT); - break; -#if 0 - case PROP_PRIORITY_ARRAY: - /* Array element zero is the number of elements in the array */ - if (array_index == 0) + case PROP_OBJECT_IDENTIFIER: apdu_len = - encode_application_unsigned(&apdu[0], BACNET_MAX_PRIORITY); - /* if no index was specified, then try to encode the entire list */ - /* into one packet. */ - else if (array_index == BACNET_ARRAY_ALL) { + encode_application_object_id(&apdu[0], OBJECT_ANALOG_VALUE, + object_instance); + break; + case PROP_OBJECT_NAME: + case PROP_DESCRIPTION: + characterstring_init_ansi(&char_string, + Analog_Value_Name(object_instance)); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_OBJECT_TYPE: + apdu_len = + encode_application_enumerated(&apdu[0], OBJECT_ANALOG_VALUE); + break; + case PROP_PRESENT_VALUE: + real_value = Analog_Value_Present_Value(object_instance); + apdu_len = encode_application_real(&apdu[0], real_value); + break; + case PROP_STATUS_FLAGS: + bitstring_init(&bit_string); + bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); + apdu_len = encode_application_bitstring(&apdu[0], &bit_string); + break; + case PROP_EVENT_STATE: + apdu_len = + encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); + break; + case PROP_OUT_OF_SERVICE: +#if 0 object_index = Analog_Value_Instance_To_Index(object_instance); - for (i = 0; i < BACNET_MAX_PRIORITY; i++) { - /* FIXME: check if we have room before adding it to APDU */ - if (Present_Value[object_index][i] == - ANALOG_LEVEL_NULL) - len = encode_application_null(&apdu[apdu_len]); - else { - real_value = Present_Value[object_index][i]; - len = encode_application_real(&apdu[apdu_len], real_value); - } - /* add it if we have room */ - if ((apdu_len + len) < MAX_APDU) - apdu_len += len; - else { - *error_class = ERROR_CLASS_SERVICES; - *error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT; - apdu_len = -1; - break; - } - } - } else { - object_index = Analog_Value_Instance_To_Index(object_instance); - if (array_index <= BACNET_MAX_PRIORITY) { - if (Present_Value[object_index][array_index - 1] == - ANALOG_LEVEL_NULL) - apdu_len = encode_application_null(&apdu[0]); - else { - real_value = - Present_Value[object_index][array_index - 1]; - apdu_len = encode_application_real(&apdu[0], real_value); + state = Analog_Value_Out_Of_Service[object_index]; +#endif + apdu_len = encode_application_boolean(&apdu[0], false); + break; + case PROP_UNITS: + apdu_len = encode_application_enumerated(&apdu[0], UNITS_PERCENT); + break; +#if 0 + case PROP_PRIORITY_ARRAY: + /* Array element zero is the number of elements in the array */ + if (array_index == 0) + apdu_len = + encode_application_unsigned(&apdu[0], BACNET_MAX_PRIORITY); + /* if no index was specified, then try to encode the entire list */ + /* into one packet. */ + else if (array_index == BACNET_ARRAY_ALL) { + object_index = Analog_Value_Instance_To_Index(object_instance); + for (i = 0; i < BACNET_MAX_PRIORITY; i++) { + /* FIXME: check if we have room before adding it to APDU */ + if (Present_Value[object_index][i] == ANALOG_LEVEL_NULL) + len = encode_application_null(&apdu[apdu_len]); + else { + real_value = Present_Value[object_index][i]; + len = + encode_application_real(&apdu[apdu_len], + real_value); + } + /* add it if we have room */ + if ((apdu_len + len) < MAX_APDU) + apdu_len += len; + else { + *error_class = ERROR_CLASS_SERVICES; + *error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT; + apdu_len = -1; + break; + } } } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_ARRAY_INDEX; - apdu_len = -1; + object_index = Analog_Value_Instance_To_Index(object_instance); + if (array_index <= BACNET_MAX_PRIORITY) { + if (Present_Value[object_index][array_index - 1] == + ANALOG_LEVEL_NULL) + apdu_len = encode_application_null(&apdu[0]); + else { + real_value = + Present_Value[object_index][array_index - 1]; + apdu_len = + encode_application_real(&apdu[0], real_value); + } + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_ARRAY_INDEX; + apdu_len = -1; + } } - } - break; - case PROP_RELINQUISH_DEFAULT: - real_value = ANALOG_RELINQUISH_DEFAULT; - apdu_len = encode_application_real(&apdu[0], real_value); - break; + break; + case PROP_RELINQUISH_DEFAULT: + real_value = ANALOG_RELINQUISH_DEFAULT; + apdu_len = encode_application_real(&apdu[0], real_value); + break; #endif - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_UNKNOWN_PROPERTY; - apdu_len = -1; - break; + default: + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_UNKNOWN_PROPERTY; + apdu_len = -1; + break; } return apdu_len; } /* returns true if successful */ -bool Analog_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) +bool Analog_Value_Write_Property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { bool status = false; /* return value */ unsigned int object_index = 0; @@ -281,80 +298,80 @@ bool Analog_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, /* FIXME: len < application_data_len: more data? */ /* FIXME: len == 0: unable to decode? */ switch (wp_data->object_property) { - case PROP_PRESENT_VALUE: - if (value.tag == BACNET_APPLICATION_TAG_REAL) { - priority = wp_data->priority; - /* Command priority 6 is reserved for use by Minimum On/Off - algorithm and may not be used for other purposes in any - object. */ - if (priority && (priority <= BACNET_MAX_PRIORITY) && - (priority != 6 /* reserved */ ) && - (value.type.Real >= 0.0) && (value.type.Real <= 100.0)) { - level = (uint8_t) value.type.Real; - object_index = - Analog_Value_Instance_To_Index(wp_data-> - object_instance); - priority--; - Present_Value[object_index] = level; - /* Note: you could set the physical output here if we - are the highest priority. - However, if Out of Service is TRUE, then don't set the - physical output. This comment may apply to the - main loop (i.e. check out of service before changing output) */ - status = true; - } else if (priority == 6) { + case PROP_PRESENT_VALUE: + if (value.tag == BACNET_APPLICATION_TAG_REAL) { + priority = wp_data->priority; /* Command priority 6 is reserved for use by Minimum On/Off algorithm and may not be used for other purposes in any object. */ - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + if (priority && (priority <= BACNET_MAX_PRIORITY) && + (priority != 6 /* reserved */ ) && + (value.type.Real >= 0.0) && (value.type.Real <= 100.0)) { + level = (uint8_t) value.type.Real; + object_index = + Analog_Value_Instance_To_Index(wp_data-> + object_instance); + priority--; + Present_Value[object_index] = level; + /* Note: you could set the physical output here if we + are the highest priority. + However, if Out of Service is TRUE, then don't set the + physical output. This comment may apply to the + main loop (i.e. check out of service before changing output) */ + status = true; + } else if (priority == 6) { + /* Command priority 6 is reserved for use by Minimum On/Off + algorithm and may not be used for other purposes in any + object. */ + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } +#if 0 + } else if (value.tag == BACNET_APPLICATION_TAG_NULL) { + level = ANALOG_LEVEL_NULL; + object_index = + Analog_Value_Instance_To_Index(wp_data->object_instance); + priority = wp_data->priority; + if (priority && (priority <= BACNET_MAX_PRIORITY)) { + priority--; + Present_Value[object_index][priority] = level; + /* Note: you could set the physical output here to the next + highest priority, or to the relinquish default if no + priorities are set. + However, if Out of Service is TRUE, then don't set the + physical output. This comment may apply to the + main loop (i.e. check out of service before changing output) */ + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } +#endif } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } + break; #if 0 - } else if (value.tag == BACNET_APPLICATION_TAG_NULL) { - level = ANALOG_LEVEL_NULL; - object_index = - Analog_Value_Instance_To_Index(wp_data->object_instance); - priority = wp_data->priority; - if (priority && (priority <= BACNET_MAX_PRIORITY)) { - priority--; - Present_Value[object_index][priority] = level; - /* Note: you could set the physical output here to the next - highest priority, or to the relinquish default if no - priorities are set. - However, if Out of Service is TRUE, then don't set the - physical output. This comment may apply to the - main loop (i.e. check out of service before changing output) */ + case PROP_OUT_OF_SERVICE: + if (value.tag == BACNET_APPLICATION_TAG_BOOLEAN) { + object_index = + Analog_Value_Instance_To_Index(wp_data->object_instance); + Analog_Value_Out_Of_Service[object_index] = value.type.Boolean; status = true; } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } + break; #endif - } else { + default: *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; -#if 0 - case PROP_OUT_OF_SERVICE: - if (value.tag == BACNET_APPLICATION_TAG_BOOLEAN) { - object_index = - Analog_Value_Instance_To_Index(wp_data->object_instance); - Analog_Value_Out_Of_Service[object_index] = value.type.Boolean; - status = true; - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; -#endif - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; - break; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + break; } return status; @@ -366,7 +383,8 @@ bool Analog_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, #include #include "ctest.h" -void testAnalog_Value(Test * pTest) +void testAnalog_Value( + Test * pTest) { uint8_t apdu[MAX_APDU] = { 0 }; int len = 0; @@ -381,8 +399,7 @@ void testAnalog_Value(Test * pTest) len = Analog_Value_Encode_Property_APDU(&apdu[0], instance, - PROP_OBJECT_IDENTIFIER, - BACNET_ARRAY_ALL, &error_class, &error_code); + PROP_OBJECT_IDENTIFIER, BACNET_ARRAY_ALL, &error_class, &error_code); ct_test(pTest, len != 0); len = decode_tag_number_and_value(&apdu[0], &tag_number, &len_value); ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_OBJECT_ID); @@ -395,7 +412,8 @@ void testAnalog_Value(Test * pTest) } #ifdef TEST_ANALOG_VALUE -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -412,5 +430,5 @@ int main(void) return 0; } -#endif /* TEST_ANALOG_VALUE */ -#endif /* TEST */ +#endif /* TEST_ANALOG_VALUE */ +#endif /* TEST */ diff --git a/bacnet-stack/ports/pic18f6720/bi.c b/bacnet-stack/ports/pic18f6720/bi.c index 5cb9fdb0..a2a7e221 100644 --- a/bacnet-stack/ports/pic18f6720/bi.c +++ b/bacnet-stack/ports/pic18f6720/bi.c @@ -37,21 +37,23 @@ static BACNET_BINARY_PV Present_Value[MAX_BINARY_INPUTS]; -static void Binary_Input_Initialize(void) +static void Binary_Input_Initialize( + void) { - static bool initialized = false; - unsigned i; - - if (!initialized) { - initialized = true; - for (i = 0; i < MAX_BINARY_INPUTS; i++) { - Present_Value[i] = BINARY_INACTIVE; + static bool initialized = false; + unsigned i; + + if (!initialized) { + initialized = true; + for (i = 0; i < MAX_BINARY_INPUTS; i++) { + Present_Value[i] = BINARY_INACTIVE; + } } - } } /* we simply have 0-n object instances. */ -bool Binary_Input_Valid_Instance(uint32_t object_instance) +bool Binary_Input_Valid_Instance( + uint32_t object_instance) { if (object_instance < MAX_BINARY_INPUTS) return true; @@ -60,13 +62,15 @@ bool Binary_Input_Valid_Instance(uint32_t object_instance) } /* we simply have 0-n object instances. */ -unsigned Binary_Input_Count(void) +unsigned Binary_Input_Count( + void) { return MAX_BINARY_INPUTS; } /* we simply have 0-n object instances.*/ -uint32_t Binary_Input_Index_To_Instance(unsigned index) +uint32_t Binary_Input_Index_To_Instance( + unsigned index) { return index; } @@ -74,7 +78,8 @@ uint32_t Binary_Input_Index_To_Instance(unsigned index) /* we simply have 0-n object instances. Yours might be */ /* more complex, and then you need to return the index */ /* that correlates to the correct instance number */ -unsigned Binary_Input_Instance_To_Index(uint32_t object_instance) +unsigned Binary_Input_Instance_To_Index( + uint32_t object_instance) { unsigned index = MAX_BINARY_INPUTS; @@ -84,8 +89,8 @@ unsigned Binary_Input_Instance_To_Index(uint32_t object_instance) return index; } -static BACNET_BINARY_PV Binary_Input_Present_Value(uint32_t - object_instance) +static BACNET_BINARY_PV Binary_Input_Present_Value( + uint32_t object_instance) { BACNET_BINARY_PV value = BINARY_INACTIVE; unsigned index = 0; @@ -93,13 +98,14 @@ static BACNET_BINARY_PV Binary_Input_Present_Value(uint32_t Binary_Input_Initialize(); index = Binary_Input_Instance_To_Index(object_instance); if (index < MAX_BINARY_INPUTS) { - value = Present_Value[index]; + value = Present_Value[index]; } return value; } -char *Binary_Input_Name(uint32_t object_instance) +char *Binary_Input_Name( + uint32_t object_instance) { static char text_string[16] = ""; /* okay for single thread */ @@ -113,13 +119,15 @@ char *Binary_Input_Name(uint32_t object_instance) /* return apdu length, or -1 on error */ /* assumption - object already exists, and has been bounds checked */ -int Binary_Input_Encode_Property_APDU(uint8_t * apdu, +int Binary_Input_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ BACNET_BIT_STRING bit_string; BACNET_CHARACTER_STRING char_string; BACNET_POLARITY polarity = POLARITY_NORMAL; @@ -129,48 +137,52 @@ int Binary_Input_Encode_Property_APDU(uint8_t * apdu, (void) array_index; Binary_Input_Initialize(); switch (property) { - case PROP_OBJECT_IDENTIFIER: - apdu_len = encode_application_object_id(&apdu[0], OBJECT_BINARY_INPUT, - object_instance); - break; - case PROP_OBJECT_NAME: - case PROP_DESCRIPTION: - /* note: object name must be unique in our device */ - characterstring_init_ansi(&char_string, - Binary_Input_Name(object_instance)); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_OBJECT_TYPE: - apdu_len = encode_application_enumerated(&apdu[0], OBJECT_BINARY_INPUT); - break; - case PROP_PRESENT_VALUE: - value = Binary_Input_Present_Value(object_instance); - apdu_len = encode_application_enumerated(&apdu[0],value); - break; - case PROP_STATUS_FLAGS: - /* note: see the details in the standard on how to use these */ - bitstring_init(&bit_string); - bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); - apdu_len = encode_application_bitstring(&apdu[0], &bit_string); - break; - case PROP_EVENT_STATE: - /* note: see the details in the standard on how to use this */ - apdu_len = encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); - break; - case PROP_OUT_OF_SERVICE: - apdu_len = encode_application_boolean(&apdu[0], false); - break; - case PROP_POLARITY: - apdu_len = encode_application_enumerated(&apdu[0], polarity); - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_UNKNOWN_PROPERTY; - apdu_len = -1; - break; + case PROP_OBJECT_IDENTIFIER: + apdu_len = + encode_application_object_id(&apdu[0], OBJECT_BINARY_INPUT, + object_instance); + break; + case PROP_OBJECT_NAME: + case PROP_DESCRIPTION: + /* note: object name must be unique in our device */ + characterstring_init_ansi(&char_string, + Binary_Input_Name(object_instance)); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_OBJECT_TYPE: + apdu_len = + encode_application_enumerated(&apdu[0], OBJECT_BINARY_INPUT); + break; + case PROP_PRESENT_VALUE: + value = Binary_Input_Present_Value(object_instance); + apdu_len = encode_application_enumerated(&apdu[0], value); + break; + case PROP_STATUS_FLAGS: + /* note: see the details in the standard on how to use these */ + bitstring_init(&bit_string); + bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); + apdu_len = encode_application_bitstring(&apdu[0], &bit_string); + break; + case PROP_EVENT_STATE: + /* note: see the details in the standard on how to use this */ + apdu_len = + encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); + break; + case PROP_OUT_OF_SERVICE: + apdu_len = encode_application_boolean(&apdu[0], false); + break; + case PROP_POLARITY: + apdu_len = encode_application_enumerated(&apdu[0], polarity); + break; + default: + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_UNKNOWN_PROPERTY; + apdu_len = -1; + break; } return apdu_len; @@ -181,7 +193,8 @@ int Binary_Input_Encode_Property_APDU(uint8_t * apdu, #include #include "ctest.h" -void testBinaryInput(Test * pTest) +void testBinaryInput( + Test * pTest) { uint8_t apdu[MAX_APDU] = { 0 }; int len = 0; @@ -197,8 +210,7 @@ void testBinaryInput(Test * pTest) /* FIXME: we should do a lot more testing here... */ len = Binary_Input_Encode_Property_APDU(&apdu[0], instance, - PROP_OBJECT_IDENTIFIER, - BACNET_ARRAY_ALL, &error_class, &error_code); + PROP_OBJECT_IDENTIFIER, BACNET_ARRAY_ALL, &error_class, &error_code); ct_test(pTest, len >= 0); len = decode_tag_number_and_value(&apdu[0], &tag_number, &len_value); ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_OBJECT_ID); @@ -211,7 +223,8 @@ void testBinaryInput(Test * pTest) } #ifdef TEST_BINARY_INPUT -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -228,5 +241,5 @@ int main(void) return 0; } -#endif /* TEST_BINARY_INPUT */ -#endif /* TEST */ +#endif /* TEST_BINARY_INPUT */ +#endif /* TEST */ diff --git a/bacnet-stack/ports/pic18f6720/bv.c b/bacnet-stack/ports/pic18f6720/bv.c index 5a228748..b85ae06c 100644 --- a/bacnet-stack/ports/pic18f6720/bv.c +++ b/bacnet-stack/ports/pic18f6720/bv.c @@ -31,28 +31,30 @@ #include "bacdef.h" #include "bacdcode.h" #include "bacenum.h" -#include "config.h" /* the custom stuff */ +#include "config.h" /* the custom stuff */ #include "wp.h" #define MAX_BINARY_VALUES 8 static BACNET_BINARY_PV Present_Value[MAX_BINARY_VALUES]; -static void Binary_Value_Initialize(void) +static void Binary_Value_Initialize( + void) { - static bool initialized = false; - unsigned i; - - if (!initialized) { - initialized = true; - for (i = 0; i < MAX_BINARY_VALUES; i++) { - Present_Value[i] = BINARY_INACTIVE; + static bool initialized = false; + unsigned i; + + if (!initialized) { + initialized = true; + for (i = 0; i < MAX_BINARY_VALUES; i++) { + Present_Value[i] = BINARY_INACTIVE; + } } - } } /* we simply have 0-n object instances. */ -bool Binary_Value_Valid_Instance(uint32_t object_instance) +bool Binary_Value_Valid_Instance( + uint32_t object_instance) { if (object_instance < MAX_BINARY_VALUES) return true; @@ -61,19 +63,22 @@ bool Binary_Value_Valid_Instance(uint32_t object_instance) } /* we simply have 0-n object instances. */ -unsigned Binary_Value_Count(void) +unsigned Binary_Value_Count( + void) { return MAX_BINARY_VALUES; } /* we simply have 0-n object instances. */ -uint32_t Binary_Value_Index_To_Instance(unsigned index) +uint32_t Binary_Value_Index_To_Instance( + unsigned index) { return index; } /* we simply have 0-n object instances. */ -unsigned Binary_Value_Instance_To_Index(uint32_t object_instance) +unsigned Binary_Value_Instance_To_Index( + uint32_t object_instance) { unsigned index = MAX_BINARY_VALUES; @@ -83,8 +88,8 @@ unsigned Binary_Value_Instance_To_Index(uint32_t object_instance) return index; } -static BACNET_BINARY_PV Binary_Value_Present_Value(uint32_t - object_instance) +static BACNET_BINARY_PV Binary_Value_Present_Value( + uint32_t object_instance) { BACNET_BINARY_PV value = BINARY_INACTIVE; @@ -97,7 +102,8 @@ static BACNET_BINARY_PV Binary_Value_Present_Value(uint32_t } /* note: the object name must be unique within this device */ -char *Binary_Value_Name(uint32_t object_instance) +char *Binary_Value_Name( + uint32_t object_instance) { static char text_string[16] = ""; /* okay for single thread */ @@ -110,14 +116,16 @@ char *Binary_Value_Name(uint32_t object_instance) } /* return apdu len, or -1 on error */ -int Binary_Value_Encode_Property_APDU(uint8_t * apdu, +int Binary_Value_Encode_Property_APDU( + uint8_t * apdu, uint32_t object_instance, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { int len = 0; - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ BACNET_BIT_STRING bit_string; BACNET_CHARACTER_STRING char_string; BACNET_BINARY_PV present_value = BINARY_INACTIVE; @@ -128,58 +136,64 @@ int Binary_Value_Encode_Property_APDU(uint8_t * apdu, Binary_Value_Initialize(); switch (property) { - case PROP_OBJECT_IDENTIFIER: - apdu_len = encode_application_object_id(&apdu[0], OBJECT_BINARY_VALUE, - object_instance); - break; - /* note: Name and Description don't have to be the same. - You could make Description writable and different */ - case PROP_OBJECT_NAME: - case PROP_DESCRIPTION: - characterstring_init_ansi(&char_string, - Binary_Value_Name(object_instance)); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_OBJECT_TYPE: - apdu_len = encode_application_enumerated(&apdu[0], OBJECT_BINARY_VALUE); - break; - case PROP_PRESENT_VALUE: - present_value = Binary_Value_Present_Value(object_instance); - apdu_len = encode_application_enumerated(&apdu[0], present_value); - break; - case PROP_STATUS_FLAGS: - /* note: see the details in the standard on how to use these */ - bitstring_init(&bit_string); - bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); - bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); - apdu_len = encode_application_bitstring(&apdu[0], &bit_string); - break; - case PROP_EVENT_STATE: - /* note: see the details in the standard on how to use this */ - apdu_len = encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); - break; - case PROP_OUT_OF_SERVICE: - apdu_len = encode_application_boolean(&apdu[0], false); - break; - case PROP_POLARITY: - /* FIXME: figure out the polarity */ - apdu_len = encode_application_enumerated(&apdu[0], polarity); - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_UNKNOWN_PROPERTY; - apdu_len = -1; - break; + case PROP_OBJECT_IDENTIFIER: + apdu_len = + encode_application_object_id(&apdu[0], OBJECT_BINARY_VALUE, + object_instance); + break; + /* note: Name and Description don't have to be the same. + You could make Description writable and different */ + case PROP_OBJECT_NAME: + case PROP_DESCRIPTION: + characterstring_init_ansi(&char_string, + Binary_Value_Name(object_instance)); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_OBJECT_TYPE: + apdu_len = + encode_application_enumerated(&apdu[0], OBJECT_BINARY_VALUE); + break; + case PROP_PRESENT_VALUE: + present_value = Binary_Value_Present_Value(object_instance); + apdu_len = encode_application_enumerated(&apdu[0], present_value); + break; + case PROP_STATUS_FLAGS: + /* note: see the details in the standard on how to use these */ + bitstring_init(&bit_string); + bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); + bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, false); + apdu_len = encode_application_bitstring(&apdu[0], &bit_string); + break; + case PROP_EVENT_STATE: + /* note: see the details in the standard on how to use this */ + apdu_len = + encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); + break; + case PROP_OUT_OF_SERVICE: + apdu_len = encode_application_boolean(&apdu[0], false); + break; + case PROP_POLARITY: + /* FIXME: figure out the polarity */ + apdu_len = encode_application_enumerated(&apdu[0], polarity); + break; + default: + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_UNKNOWN_PROPERTY; + apdu_len = -1; + break; } return apdu_len; } /* returns true if successful */ -bool Binary_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) +bool Binary_Value_Write_Property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { bool status = false; /* return value */ unsigned int object_index = 0; @@ -199,85 +213,85 @@ bool Binary_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, /* FIXME: len < application_data_len: more data? */ /* FIXME: len == 0: unable to decode? */ switch (wp_data->object_property) { - case PROP_PRESENT_VALUE: - if (value.tag == BACNET_APPLICATION_TAG_ENUMERATED) { - priority = wp_data->priority; - /* Command priority 6 is reserved for use by Minimum On/Off - algorithm and may not be used for other purposes in any - object. */ - if (priority && (priority <= BACNET_MAX_PRIORITY) && - (priority != 6 /* reserved */ ) && - (value.type.Enumerated >= MIN_BINARY_PV) && - (value.type.Enumerated <= MAX_BINARY_PV)) { - level = value.type.Enumerated; - object_index = - Binary_Value_Instance_To_Index(wp_data-> - object_instance); - priority--; - /* NOTE: this Binary value has no priority array */ - Present_Value[object_index] = level; - /* Note: you could set the physical output here if we - are the highest priority. - However, if Out of Service is TRUE, then don't set the - physical output. */ - status = true; - } else if (priority == 6) { + case PROP_PRESENT_VALUE: + if (value.tag == BACNET_APPLICATION_TAG_ENUMERATED) { + priority = wp_data->priority; /* Command priority 6 is reserved for use by Minimum On/Off algorithm and may not be used for other purposes in any object. */ + if (priority && (priority <= BACNET_MAX_PRIORITY) && + (priority != 6 /* reserved */ ) && + (value.type.Enumerated >= MIN_BINARY_PV) && + (value.type.Enumerated <= MAX_BINARY_PV)) { + level = value.type.Enumerated; + object_index = + Binary_Value_Instance_To_Index(wp_data-> + object_instance); + priority--; + /* NOTE: this Binary value has no priority array */ + Present_Value[object_index] = level; + /* Note: you could set the physical output here if we + are the highest priority. + However, if Out of Service is TRUE, then don't set the + physical output. */ + status = true; + } else if (priority == 6) { + /* Command priority 6 is reserved for use by Minimum On/Off + algorithm and may not be used for other purposes in any + object. */ + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } + } else if (value.tag == BACNET_APPLICATION_TAG_NULL) { +#if 0 + /* NOTE: this Binary Value has no priority array */ + level = BINARY_NULL; + object_index = + Binary_Value_Instance_To_Index(wp_data->object_instance); + priority = wp_data->priority; + if (priority && (priority <= BACNET_MAX_PRIORITY)) { + priority--; + Binary_Value_Level[object_index][priority] = level; + /* Note: you could set the physical output here to the next + highest priority, or to the relinquish default if no + priorities are set. + However, if Out of Service is TRUE, then don't set the + physical output. This comment may apply to the + main loop (i.e. check out of service before changing output) */ + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } +#else *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; +#endif } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } - } else if (value.tag == BACNET_APPLICATION_TAG_NULL) { + break; #if 0 - /* NOTE: this Binary Value has no priority array */ - level = BINARY_NULL; - object_index = - Binary_Value_Instance_To_Index(wp_data->object_instance); - priority = wp_data->priority; - if (priority && (priority <= BACNET_MAX_PRIORITY)) { - priority--; - Binary_Value_Level[object_index][priority] = level; - /* Note: you could set the physical output here to the next - highest priority, or to the relinquish default if no - priorities are set. - However, if Out of Service is TRUE, then don't set the - physical output. This comment may apply to the - main loop (i.e. check out of service before changing output) */ + case PROP_OUT_OF_SERVICE: + if (value.tag == BACNET_APPLICATION_TAG_BOOLEAN) { + object_index = + Binary_Value_Instance_To_Index(wp_data->object_instance); + Binary_Value_Out_Of_Service[object_index] = value.type.Boolean; status = true; } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } -#else - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; + break; #endif - } else { + default: *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; -#if 0 - case PROP_OUT_OF_SERVICE: - if (value.tag == BACNET_APPLICATION_TAG_BOOLEAN) { - object_index = - Binary_Value_Instance_To_Index(wp_data->object_instance); - Binary_Value_Out_Of_Service[object_index] = value.type.Boolean; - status = true; - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; -#endif - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; - break; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + break; } return status; @@ -288,7 +302,8 @@ bool Binary_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, #include #include "ctest.h" -void testBinary_Value(Test * pTest) +void testBinary_Value( + Test * pTest) { uint8_t apdu[MAX_APDU] = { 0 }; int len = 0; @@ -303,8 +318,7 @@ void testBinary_Value(Test * pTest) len = Binary_Value_Encode_Property_APDU(&apdu[0], instance, - PROP_OBJECT_IDENTIFIER, - BACNET_ARRAY_ALL, &error_class, &error_code); + PROP_OBJECT_IDENTIFIER, BACNET_ARRAY_ALL, &error_class, &error_code); ct_test(pTest, len != 0); len = decode_tag_number_and_value(&apdu[0], &tag_number, &len_value); ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_OBJECT_ID); @@ -317,7 +331,8 @@ void testBinary_Value(Test * pTest) } #ifdef TEST_BINARY_VALUE -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -334,5 +349,5 @@ int main(void) return 0; } -#endif /* TEST_BINARY_VALUE */ -#endif /* TEST */ +#endif /* TEST_BINARY_VALUE */ +#endif /* TEST */ diff --git a/bacnet-stack/ports/pic18f6720/device.c b/bacnet-stack/ports/pic18f6720/device.c index a8af24f0..fc9a9f91 100644 --- a/bacnet-stack/ports/pic18f6720/device.c +++ b/bacnet-stack/ports/pic18f6720/device.c @@ -25,14 +25,14 @@ #include #include -#include /* for memmove */ +#include /* for memmove */ #include "bacdef.h" #include "bacdcode.h" #include "bacstr.h" #include "bacenum.h" -#include "config.h" /* the custom stuff */ +#include "config.h" /* the custom stuff */ #include "apdu.h" -#include "device.h" /* me */ +#include "device.h" /* me */ #include "dlmstp.h" #include "rs485.h" #include "ai.h" @@ -53,13 +53,15 @@ static BACNET_DEVICE_STATUS System_Status = STATUS_OPERATIONAL; BACNET_REINITIALIZED_STATE_OF_DEVICE Reinitialize_State = REINITIALIZED_STATE_IDLE; -void Device_Reinit(void) +void Device_Reinit( + void) { dcc_set_status_duration(COMMUNICATION_ENABLE, 0); Device_Set_Object_Instance_Number(BACNET_MAX_INSTANCE); } -void Device_Init(void) +void Device_Init( + void) { Reinitialize_State = REINITIALIZED_STATE_IDLE; dcc_set_status_duration(COMMUNICATION_ENABLE, 0); @@ -71,14 +73,16 @@ void Device_Init(void) } /* methods to manipulate the data */ -uint32_t Device_Object_Instance_Number(void) +uint32_t Device_Object_Instance_Number( + void) { return Object_Instance_Number; } -bool Device_Set_Object_Instance_Number(uint32_t object_id) +bool Device_Set_Object_Instance_Number( + uint32_t object_id) { - bool status = true; /* return value */ + bool status = true; /* return value */ if (object_id <= BACNET_MAX_INSTANCE) { Object_Instance_Number = object_id; @@ -94,71 +98,83 @@ bool Device_Set_Object_Instance_Number(uint32_t object_id) return status; } -bool Device_Valid_Object_Instance_Number(uint32_t object_id) +bool Device_Valid_Object_Instance_Number( + uint32_t object_id) { /* BACnet allows for a wildcard instance number */ return ((Object_Instance_Number == object_id) || (object_id == BACNET_MAX_INSTANCE)); } -BACNET_DEVICE_STATUS Device_System_Status(void) +BACNET_DEVICE_STATUS Device_System_Status( + void) { return System_Status; } -void Device_Set_System_Status(BACNET_DEVICE_STATUS status) +void Device_Set_System_Status( + BACNET_DEVICE_STATUS status) { if (status < MAX_DEVICE_STATUS) System_Status = status; } -uint16_t Device_Vendor_Identifier(void) +uint16_t Device_Vendor_Identifier( + void) { return BACNET_VENDOR_ID; } -uint8_t Device_Protocol_Version(void) +uint8_t Device_Protocol_Version( + void) { return 1; } -uint8_t Device_Protocol_Revision(void) +uint8_t Device_Protocol_Revision( + void) { return 5; } /* FIXME: MAX_APDU is defined in config.ini - set it! */ -uint16_t Device_Max_APDU_Length_Accepted(void) +uint16_t Device_Max_APDU_Length_Accepted( + void) { return MAX_APDU; } -BACNET_SEGMENTATION Device_Segmentation_Supported(void) +BACNET_SEGMENTATION Device_Segmentation_Supported( + void) { return SEGMENTATION_NONE; } -uint16_t Device_APDU_Timeout(void) +uint16_t Device_APDU_Timeout( + void) { return 60000; } -uint8_t Device_Number_Of_APDU_Retries(void) +uint8_t Device_Number_Of_APDU_Retries( + void) { return 0; } -uint8_t Device_Database_Revision(void) +uint8_t Device_Database_Revision( + void) { return 0; } /* Since many network clients depend on the object list */ /* for discovery, it must be consistent! */ -unsigned Device_Object_List_Count(void) +unsigned Device_Object_List_Count( + void) { - unsigned count = 1; /* at least 1 for device object */ + unsigned count = 1; /* at least 1 for device object */ /* FIXME: add objects as needed */ count += Binary_Value_Count(); @@ -171,8 +187,10 @@ unsigned Device_Object_List_Count(void) /* Since many network clients depend on the object list */ /* for discovery, it must be consistent! */ -bool Device_Object_List_Identifier(unsigned array_index, - int *object_type, uint32_t * instance) +bool Device_Object_List_Identifier( + unsigned array_index, + int *object_type, + uint32_t * instance) { bool status = false; unsigned object_index = 0; @@ -242,13 +260,15 @@ bool Device_Object_List_Identifier(unsigned array_index, } /* return the length of the apdu encoded or -1 for error */ -int Device_Encode_Property_APDU(uint8_t * apdu, +int Device_Encode_Property_APDU( + uint8_t * apdu, BACNET_PROPERTY_ID property, int32_t array_index, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { - int apdu_len = 0; /* return value */ - int len = 0; /* apdu len intermediate value */ + int apdu_len = 0; /* return value */ + int len = 0; /* apdu len intermediate value */ BACNET_BIT_STRING bit_string; BACNET_CHARACTER_STRING char_string; unsigned i = 0; @@ -263,208 +283,226 @@ int Device_Encode_Property_APDU(uint8_t * apdu, /* FIXME: change the hardcoded names to suit your application */ switch (property) { - case PROP_OBJECT_IDENTIFIER: - apdu_len = encode_application_object_id(&apdu[0], OBJECT_DEVICE, - Object_Instance_Number); - break; - case PROP_OBJECT_NAME: - (void) strcpypgm2ram(&string_buffer[0], "PIC18F6720 Device"); - characterstring_init_ansi(&char_string, string_buffer); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_OBJECT_TYPE: - apdu_len = encode_application_enumerated(&apdu[0], OBJECT_DEVICE); - break; - case PROP_DESCRIPTION: - (void) strcpypgm2ram(&string_buffer[0], "BACnet Demo"); - characterstring_init_ansi(&char_string, string_buffer); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_SYSTEM_STATUS: - apdu_len = - encode_application_enumerated(&apdu[0], Device_System_Status()); - break; - case PROP_VENDOR_NAME: - (void) strcpypgm2ram(&string_buffer[0], BACNET_VENDOR_NAME); - characterstring_init_ansi(&char_string, string_buffer); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_VENDOR_IDENTIFIER: - apdu_len = - encode_application_unsigned(&apdu[0], Device_Vendor_Identifier()); - break; - case PROP_MODEL_NAME: - (void) strcpypgm2ram(&string_buffer[0], "GNU Demo"); - characterstring_init_ansi(&char_string, string_buffer); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_FIRMWARE_REVISION: - characterstring_init_ansi(&char_string, BACnet_Version); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_APPLICATION_SOFTWARE_VERSION: - (void) strcpypgm2ram(&string_buffer[0], "1.0"); - characterstring_init_ansi(&char_string, string_buffer); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_LOCATION: - (void) strcpypgm2ram(&string_buffer[0], "USA"); - characterstring_init_ansi(&char_string, string_buffer); - apdu_len = encode_application_character_string(&apdu[0], &char_string); - break; - case PROP_PROTOCOL_VERSION: - apdu_len = - encode_application_unsigned(&apdu[0], Device_Protocol_Version()); - break; - case PROP_PROTOCOL_REVISION: - apdu_len = - encode_application_unsigned(&apdu[0], Device_Protocol_Revision()); - break; - /* BACnet Legacy Support */ - case PROP_PROTOCOL_CONFORMANCE_CLASS: - apdu_len = encode_application_unsigned(&apdu[0], 1); - break; - case PROP_PROTOCOL_SERVICES_SUPPORTED: - /* Note: list of services that are executed, not initiated. */ - bitstring_init(&bit_string); - for (i = 0; i < MAX_BACNET_SERVICES_SUPPORTED; i++) { - /* automatic lookup based on handlers set */ - bitstring_set_bit(&bit_string, (uint8_t) i, - apdu_service_supported(i)); - } - apdu_len = encode_application_bitstring(&apdu[0], &bit_string); - break; - case PROP_PROTOCOL_OBJECT_TYPES_SUPPORTED: - /* Note: this is the list of objects that can be in this device, - not a list of objects that this device can access */ - bitstring_init(&bit_string); - for (i = 0; i < MAX_ASHRAE_OBJECT_TYPE; i++) { - /* initialize all the object types to not-supported */ - bitstring_set_bit(&bit_string, (uint8_t) i, false); - } - /* FIXME: indicate the objects that YOU support */ - bitstring_set_bit(&bit_string, OBJECT_DEVICE, true); - bitstring_set_bit(&bit_string, OBJECT_ANALOG_VALUE, true); - bitstring_set_bit(&bit_string, OBJECT_BINARY_VALUE, true); - bitstring_set_bit(&bit_string, OBJECT_ANALOG_INPUT, true); - bitstring_set_bit(&bit_string, OBJECT_BINARY_INPUT, true); - apdu_len = encode_application_bitstring(&apdu[0], &bit_string); - break; - case PROP_OBJECT_LIST: - count = Device_Object_List_Count(); - /* Array element zero is the number of objects in the list */ - if (array_index == 0) - apdu_len = encode_application_unsigned(&apdu[0], count); - /* if no index was specified, then try to encode the entire list */ - /* into one packet. Note that more than likely you will have */ - /* to return an error if the number of encoded objects exceeds */ - /* your maximum APDU size. */ - else if (array_index == BACNET_ARRAY_ALL) { - for (i = 1; i <= count; i++) { - if (Device_Object_List_Identifier(i, &object_type, - &instance)) { - len = - encode_application_object_id(&apdu[apdu_len], - object_type, instance); - apdu_len += len; - /* assume next one is the same size as this one */ - /* can we all fit into the APDU? */ - if ((apdu_len + len) >= MAX_APDU) { + case PROP_OBJECT_IDENTIFIER: + apdu_len = encode_application_object_id(&apdu[0], OBJECT_DEVICE, + Object_Instance_Number); + break; + case PROP_OBJECT_NAME: + (void) strcpypgm2ram(&string_buffer[0], "PIC18F6720 Device"); + characterstring_init_ansi(&char_string, string_buffer); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_OBJECT_TYPE: + apdu_len = encode_application_enumerated(&apdu[0], OBJECT_DEVICE); + break; + case PROP_DESCRIPTION: + (void) strcpypgm2ram(&string_buffer[0], "BACnet Demo"); + characterstring_init_ansi(&char_string, string_buffer); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_SYSTEM_STATUS: + apdu_len = + encode_application_enumerated(&apdu[0], + Device_System_Status()); + break; + case PROP_VENDOR_NAME: + (void) strcpypgm2ram(&string_buffer[0], BACNET_VENDOR_NAME); + characterstring_init_ansi(&char_string, string_buffer); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_VENDOR_IDENTIFIER: + apdu_len = + encode_application_unsigned(&apdu[0], + Device_Vendor_Identifier()); + break; + case PROP_MODEL_NAME: + (void) strcpypgm2ram(&string_buffer[0], "GNU Demo"); + characterstring_init_ansi(&char_string, string_buffer); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_FIRMWARE_REVISION: + characterstring_init_ansi(&char_string, BACnet_Version); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_APPLICATION_SOFTWARE_VERSION: + (void) strcpypgm2ram(&string_buffer[0], "1.0"); + characterstring_init_ansi(&char_string, string_buffer); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_LOCATION: + (void) strcpypgm2ram(&string_buffer[0], "USA"); + characterstring_init_ansi(&char_string, string_buffer); + apdu_len = + encode_application_character_string(&apdu[0], &char_string); + break; + case PROP_PROTOCOL_VERSION: + apdu_len = + encode_application_unsigned(&apdu[0], + Device_Protocol_Version()); + break; + case PROP_PROTOCOL_REVISION: + apdu_len = + encode_application_unsigned(&apdu[0], + Device_Protocol_Revision()); + break; + /* BACnet Legacy Support */ + case PROP_PROTOCOL_CONFORMANCE_CLASS: + apdu_len = encode_application_unsigned(&apdu[0], 1); + break; + case PROP_PROTOCOL_SERVICES_SUPPORTED: + /* Note: list of services that are executed, not initiated. */ + bitstring_init(&bit_string); + for (i = 0; i < MAX_BACNET_SERVICES_SUPPORTED; i++) { + /* automatic lookup based on handlers set */ + bitstring_set_bit(&bit_string, (uint8_t) i, + apdu_service_supported(i)); + } + apdu_len = encode_application_bitstring(&apdu[0], &bit_string); + break; + case PROP_PROTOCOL_OBJECT_TYPES_SUPPORTED: + /* Note: this is the list of objects that can be in this device, + not a list of objects that this device can access */ + bitstring_init(&bit_string); + for (i = 0; i < MAX_ASHRAE_OBJECT_TYPE; i++) { + /* initialize all the object types to not-supported */ + bitstring_set_bit(&bit_string, (uint8_t) i, false); + } + /* FIXME: indicate the objects that YOU support */ + bitstring_set_bit(&bit_string, OBJECT_DEVICE, true); + bitstring_set_bit(&bit_string, OBJECT_ANALOG_VALUE, true); + bitstring_set_bit(&bit_string, OBJECT_BINARY_VALUE, true); + bitstring_set_bit(&bit_string, OBJECT_ANALOG_INPUT, true); + bitstring_set_bit(&bit_string, OBJECT_BINARY_INPUT, true); + apdu_len = encode_application_bitstring(&apdu[0], &bit_string); + break; + case PROP_OBJECT_LIST: + count = Device_Object_List_Count(); + /* Array element zero is the number of objects in the list */ + if (array_index == 0) + apdu_len = encode_application_unsigned(&apdu[0], count); + /* if no index was specified, then try to encode the entire list */ + /* into one packet. Note that more than likely you will have */ + /* to return an error if the number of encoded objects exceeds */ + /* your maximum APDU size. */ + else if (array_index == BACNET_ARRAY_ALL) { + for (i = 1; i <= count; i++) { + if (Device_Object_List_Identifier(i, &object_type, + &instance)) { + len = + encode_application_object_id(&apdu[apdu_len], + object_type, instance); + apdu_len += len; + /* assume next one is the same size as this one */ + /* can we all fit into the APDU? */ + if ((apdu_len + len) >= MAX_APDU) { + *error_class = ERROR_CLASS_SERVICES; + *error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT; + apdu_len = -1; + break; + } + } else { + /* error: internal error? */ *error_class = ERROR_CLASS_SERVICES; - *error_code = ERROR_CODE_NO_SPACE_FOR_OBJECT; + *error_code = ERROR_CODE_OTHER; apdu_len = -1; break; } - } else { - /* error: internal error? */ - *error_class = ERROR_CLASS_SERVICES; - *error_code = ERROR_CODE_OTHER; + } + } else { + if (Device_Object_List_Identifier(array_index, &object_type, + &instance)) + apdu_len = + encode_application_object_id(&apdu[0], object_type, + instance); + else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_ARRAY_INDEX; apdu_len = -1; - break; } } - } else { - if (Device_Object_List_Identifier(array_index, &object_type, - &instance)) - apdu_len = - encode_application_object_id(&apdu[0], object_type, - instance); - else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_ARRAY_INDEX; - apdu_len = -1; - } - } - break; - case PROP_MAX_APDU_LENGTH_ACCEPTED: - apdu_len = encode_application_unsigned(&apdu[0], - Device_Max_APDU_Length_Accepted()); - break; - case PROP_SEGMENTATION_SUPPORTED: - apdu_len = encode_application_enumerated(&apdu[0], - Device_Segmentation_Supported()); - break; - case PROP_APDU_TIMEOUT: - apdu_len = encode_application_unsigned(&apdu[0], Device_APDU_Timeout()); - break; - case PROP_NUMBER_OF_APDU_RETRIES: - apdu_len = - encode_application_unsigned(&apdu[0], - Device_Number_Of_APDU_Retries()); - break; - case PROP_DEVICE_ADDRESS_BINDING: - /* FIXME: encode the list here, if it exists */ - break; - case PROP_DATABASE_REVISION: - apdu_len = - encode_application_unsigned(&apdu[0], Device_Database_Revision()); - break; - case PROP_MAX_INFO_FRAMES: - apdu_len = - encode_application_unsigned(&apdu[0], dlmstp_max_info_frames()); - break; - case PROP_MAX_MASTER: - apdu_len = encode_application_unsigned(&apdu[0], dlmstp_max_master()); - break; - case PROP_LOCAL_TIME: - /* FIXME: if you support time */ - local_time.hour = 0; - local_time.min = 0; - local_time.sec = 0; - local_time.hundredths = 0; - apdu_len = encode_application_time(&apdu[0], &local_time); - break; - case PROP_UTC_OFFSET: - /* Note: BACnet Time Zone is inverse of everybody else */ - apdu_len = encode_application_signed(&apdu[0], 5 /* EST */ ); - break; - case PROP_LOCAL_DATE: - /* FIXME: if you support date */ - local_date.year = 2006; /* AD */ - local_date.month = 4; /* Jan=1..Dec=12 */ - local_date.day = 11; /* 1..31 */ - local_date.wday = 0; /* 1=Mon..7=Sun */ - apdu_len = encode_application_date(&apdu[0], &local_date); - break; - case PROP_DAYLIGHT_SAVINGS_STATUS: - /* FIXME: if you support time/date */ - apdu_len = encode_application_boolean(&apdu[0], false); - break; - case 9600: - apdu_len = encode_application_unsigned(&apdu[0], RS485_Get_Baud_Rate()); - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_UNKNOWN_PROPERTY; - apdu_len = -1; - break; + break; + case PROP_MAX_APDU_LENGTH_ACCEPTED: + apdu_len = encode_application_unsigned(&apdu[0], + Device_Max_APDU_Length_Accepted()); + break; + case PROP_SEGMENTATION_SUPPORTED: + apdu_len = encode_application_enumerated(&apdu[0], + Device_Segmentation_Supported()); + break; + case PROP_APDU_TIMEOUT: + apdu_len = + encode_application_unsigned(&apdu[0], Device_APDU_Timeout()); + break; + case PROP_NUMBER_OF_APDU_RETRIES: + apdu_len = + encode_application_unsigned(&apdu[0], + Device_Number_Of_APDU_Retries()); + break; + case PROP_DEVICE_ADDRESS_BINDING: + /* FIXME: encode the list here, if it exists */ + break; + case PROP_DATABASE_REVISION: + apdu_len = + encode_application_unsigned(&apdu[0], + Device_Database_Revision()); + break; + case PROP_MAX_INFO_FRAMES: + apdu_len = + encode_application_unsigned(&apdu[0], + dlmstp_max_info_frames()); + break; + case PROP_MAX_MASTER: + apdu_len = + encode_application_unsigned(&apdu[0], dlmstp_max_master()); + break; + case PROP_LOCAL_TIME: + /* FIXME: if you support time */ + local_time.hour = 0; + local_time.min = 0; + local_time.sec = 0; + local_time.hundredths = 0; + apdu_len = encode_application_time(&apdu[0], &local_time); + break; + case PROP_UTC_OFFSET: + /* Note: BACnet Time Zone is inverse of everybody else */ + apdu_len = encode_application_signed(&apdu[0], 5 /* EST */ ); + break; + case PROP_LOCAL_DATE: + /* FIXME: if you support date */ + local_date.year = 2006; /* AD */ + local_date.month = 4; /* Jan=1..Dec=12 */ + local_date.day = 11; /* 1..31 */ + local_date.wday = 0; /* 1=Mon..7=Sun */ + apdu_len = encode_application_date(&apdu[0], &local_date); + break; + case PROP_DAYLIGHT_SAVINGS_STATUS: + /* FIXME: if you support time/date */ + apdu_len = encode_application_boolean(&apdu[0], false); + break; + case 9600: + apdu_len = + encode_application_unsigned(&apdu[0], RS485_Get_Baud_Rate()); + break; + default: + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_UNKNOWN_PROPERTY; + apdu_len = -1; + break; } return apdu_len; } -bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) +bool Device_Write_Property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { bool status = false; /* return value */ int len = 0; @@ -481,98 +519,98 @@ bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, /* FIXME: len < application_data_len: more data? */ /* FIXME: len == 0: unable to decode? */ switch (wp_data->object_property) { - case PROP_OBJECT_IDENTIFIER: - if (value.tag == BACNET_APPLICATION_TAG_OBJECT_ID) { - if ((value.type.Object_Id.type == OBJECT_DEVICE) && - (Device_Set_Object_Instance_Number(value.type. - Object_Id.instance))) { - /* we could send an I-Am broadcast to let the world know */ - status = true; - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; - } - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case PROP_MAX_INFO_FRAMES: - if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { - if (value.type.Unsigned_Int <= 255) { - dlmstp_set_max_info_frames(value.type.Unsigned_Int); - status = true; - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; - } - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case PROP_MAX_MASTER: - if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { - if ((value.type.Unsigned_Int > 0) && - (value.type.Unsigned_Int <= 127)) { - dlmstp_set_max_master(value.type.Unsigned_Int); - status = true; - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; - } - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case PROP_OBJECT_NAME: - if (value.tag == BACNET_APPLICATION_TAG_CHARACTER_STRING) { - uint8_t encoding; - size_t len; - - encoding = - characterstring_encoding(&value.type.Character_String); - len = characterstring_length(&value.type.Character_String); - if (encoding == CHARACTER_ANSI_X34) { - if (len <= 20) { - /* FIXME: set the name */ - /* Display_Set_Name( - characterstring_value(&value.type.Character_String)); */ - /* FIXME: All the object names in a device must be unique. - Disallow setting the Device Object Name to any objects in - the device. */ + case PROP_OBJECT_IDENTIFIER: + if (value.tag == BACNET_APPLICATION_TAG_OBJECT_ID) { + if ((value.type.Object_Id.type == OBJECT_DEVICE) && + (Device_Set_Object_Instance_Number(value.type. + Object_Id.instance))) { + /* we could send an I-Am broadcast to let the world know */ + status = true; } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_NO_SPACE_TO_WRITE_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; } } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_CHARACTER_SET_NOT_SUPPORTED; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } - } else { - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - case 9600: - if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { - if (value.type.Unsigned_Int > 115200) { - RS485_Set_Baud_Rate(value.type.Unsigned_Int); - status = true; + break; + case PROP_MAX_INFO_FRAMES: + if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { + if (value.type.Unsigned_Int <= 255) { + dlmstp_set_max_info_frames(value.type.Unsigned_Int); + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } } else { *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; } - } else { + break; + case PROP_MAX_MASTER: + if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { + if ((value.type.Unsigned_Int > 0) && + (value.type.Unsigned_Int <= 127)) { + dlmstp_set_max_master(value.type.Unsigned_Int); + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; + } + break; + case PROP_OBJECT_NAME: + if (value.tag == BACNET_APPLICATION_TAG_CHARACTER_STRING) { + uint8_t encoding; + size_t len; + + encoding = + characterstring_encoding(&value.type.Character_String); + len = characterstring_length(&value.type.Character_String); + if (encoding == CHARACTER_ANSI_X34) { + if (len <= 20) { + /* FIXME: set the name */ + /* Display_Set_Name( + characterstring_value(&value.type.Character_String)); */ + /* FIXME: All the object names in a device must be unique. + Disallow setting the Device Object Name to any objects in + the device. */ + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_NO_SPACE_TO_WRITE_PROPERTY; + } + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_CHARACTER_SET_NOT_SUPPORTED; + } + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; + } + break; + case 9600: + if (value.tag == BACNET_APPLICATION_TAG_UNSIGNED_INT) { + if (value.type.Unsigned_Int > 115200) { + RS485_Set_Baud_Rate(value.type.Unsigned_Int); + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; + } + break; + default: *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_INVALID_DATA_TYPE; - } - break; - default: - *error_class = ERROR_CLASS_PROPERTY; - *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; - break; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + break; } return status; diff --git a/bacnet-stack/ports/pic18f6720/dlmstp.c b/bacnet-stack/ports/pic18f6720/dlmstp.c index 0c045024..926557f4 100644 --- a/bacnet-stack/ports/pic18f6720/dlmstp.c +++ b/bacnet-stack/ports/pic18f6720/dlmstp.c @@ -49,12 +49,14 @@ volatile struct mstp_port_struct_t MSTP_Port; #define INCREMENT_AND_LIMIT_UINT16(x) {if (x < 0xFFFF) x++;} -void dlmstp_millisecond_timer(void) +void dlmstp_millisecond_timer( + void) { INCREMENT_AND_LIMIT_UINT16(MSTP_Port.SilenceTimer); } -void dlmstp_reinit(void) +void dlmstp_reinit( + void) { RS485_Reinit(); dlmstp_set_my_address(DEFAULT_MAC_ADDRESS); @@ -62,7 +64,8 @@ void dlmstp_reinit(void) dlmstp_set_max_master(DEFAULT_MAX_MASTER); } -void dlmstp_init(void) +void dlmstp_init( + void) { uint8_t data; @@ -74,17 +77,17 @@ void dlmstp_init(void) MSTP_Port.InputBuffer = &Receive_Buffer.pdu[0]; MSTP_Init(&MSTP_Port); /* FIXME: implement your data storage */ - data = 64; /* I2C_Read_Byte( - EEPROM_DEVICE_ADDRESS, - EEPROM_MSTP_MAC_ADDR); */ + data = 64; /* I2C_Read_Byte( + EEPROM_DEVICE_ADDRESS, + EEPROM_MSTP_MAC_ADDR); */ if (data <= 127) MSTP_Port.This_Station = data; else dlmstp_set_my_address(DEFAULT_MAC_ADDRESS); /* FIXME: implement your data storage */ - data = 127; /* I2C_Read_Byte( - EEPROM_DEVICE_ADDRESS, - EEPROM_MSTP_MAX_MASTER_ADDR); */ + data = 127; /* I2C_Read_Byte( + EEPROM_DEVICE_ADDRESS, + EEPROM_MSTP_MAX_MASTER_ADDR); */ if ((data <= 127) && (data >= MSTP_Port.This_Station)) MSTP_Port.Nmax_master = data; else @@ -100,30 +103,31 @@ void dlmstp_init(void) dlmstp_set_max_info_frames(DEFAULT_MAX_INFO_FRAMES); } -void dlmstp_cleanup(void) +void dlmstp_cleanup( + void) { /* nothing to do for static buffers */ } /* returns number of bytes sent on success, zero on failure */ -int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */ +int dlmstp_send_pdu( + BACNET_ADDRESS * dest, /* destination address */ BACNET_NPDU_DATA * npdu_data, /* network information */ - uint8_t * pdu, /* any data to be sent - may be null */ + uint8_t * pdu, /* any data to be sent - may be null */ unsigned pdu_len) -{ /* number of bytes of data */ +{ /* number of bytes of data */ int bytes_sent = 0; unsigned npdu_len = 0; uint8_t frame_type = 0; uint8_t destination = 0; /* destination address */ BACNET_ADDRESS src; - unsigned i = 0; /* loop counter */ + unsigned i = 0; /* loop counter */ if (MSTP_Port.TxReady == false) { if (npdu_data->data_expecting_reply) MSTP_Port.TxFrameType = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY; else - MSTP_Port.TxFrameType = - FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; + MSTP_Port.TxFrameType = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; /* load destination MAC address */ if (dest && dest->mac_len == 1) { @@ -148,7 +152,8 @@ int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */ return bytes_sent; } -void dlmstp_task(void) +void dlmstp_task( + void) { uint8_t bytes_remaining; bool received_frame; @@ -184,7 +189,9 @@ void dlmstp_task(void) return; } -void dlmstp_fill_bacnet_address(BACNET_ADDRESS * src, uint8_t mstp_address) +void dlmstp_fill_bacnet_address( + BACNET_ADDRESS * src, + uint8_t mstp_address) { int i = 0; @@ -208,17 +215,19 @@ void dlmstp_fill_bacnet_address(BACNET_ADDRESS * src, uint8_t mstp_address) } /* for the MS/TP state machine to use for putting received data */ -uint16_t dlmstp_put_receive(uint8_t src, /* source MS/TP address */ - uint8_t * pdu, /* PDU data */ +uint16_t dlmstp_put_receive( + uint8_t src, /* source MS/TP address */ + uint8_t * pdu, /* PDU data */ uint16_t pdu_len) -{ /* amount of PDU data */ +{ /* amount of PDU data */ /* PDU is already in the Receive_Buffer */ dlmstp_fill_bacnet_address(&Receive_Buffer.address, src); Receive_Buffer.pdu_len = pdu_len; Receive_Buffer.ready = true; } -void dlmstp_set_my_address(uint8_t mac_address) +void dlmstp_set_my_address( + uint8_t mac_address) { /* Master Nodes can only have address 0-127 */ if (mac_address <= 127) { @@ -235,7 +244,8 @@ void dlmstp_set_my_address(uint8_t mac_address) return; } -uint8_t dlmstp_my_address(void) +uint8_t dlmstp_my_address( + void) { return MSTP_Port.This_Station; } @@ -247,7 +257,8 @@ uint8_t dlmstp_my_address(void) /* nodes. This may be used to allocate more or less of the available link */ /* bandwidth to particular nodes. If Max_Info_Frames is not writable in a */ /* node, its value shall be 1. */ -void dlmstp_set_max_info_frames(uint8_t max_info_frames) +void dlmstp_set_max_info_frames( + uint8_t max_info_frames) { if (max_info_frames >= 1) { MSTP_Port.Nmax_info_frames = max_info_frames; @@ -261,7 +272,8 @@ void dlmstp_set_max_info_frames(uint8_t max_info_frames) return; } -unsigned dlmstp_max_info_frames(void) +unsigned dlmstp_max_info_frames( + void) { return MSTP_Port.Nmax_info_frames; } @@ -271,7 +283,8 @@ unsigned dlmstp_max_info_frames(void) /* allowable address for master nodes. The value of Max_Master shall be */ /* less than or equal to 127. If Max_Master is not writable in a node, */ /* its value shall be 127. */ -void dlmstp_set_max_master(uint8_t max_master) +void dlmstp_set_max_master( + uint8_t max_master) { if (max_master <= 127) { if (MSTP_Port.This_Station <= max_master) { @@ -287,14 +300,16 @@ void dlmstp_set_max_master(uint8_t max_master) return; } -uint8_t dlmstp_max_master(void) +uint8_t dlmstp_max_master( + void) { return MSTP_Port.Nmax_master; } -void dlmstp_get_my_address(BACNET_ADDRESS * my_address) +void dlmstp_get_my_address( + BACNET_ADDRESS * my_address) { - int i = 0; /* counter */ + int i = 0; /* counter */ my_address->mac_len = 1; my_address->mac[0] = MSTP_Port.This_Station; @@ -307,15 +322,16 @@ void dlmstp_get_my_address(BACNET_ADDRESS * my_address) return; } -void dlmstp_get_broadcast_address(BACNET_ADDRESS * dest) -{ /* destination address */ - int i = 0; /* counter */ +void dlmstp_get_broadcast_address( + BACNET_ADDRESS * dest) +{ /* destination address */ + int i = 0; /* counter */ if (dest) { dest->mac_len = 1; dest->mac[0] = MSTP_BROADCAST_ADDRESS; dest->net = BACNET_BROADCAST_NETWORK; - dest->len = 0; /* always zero when DNET is broadcast */ + dest->len = 0; /* always zero when DNET is broadcast */ for (i = 0; i < MAX_MAC_LEN; i++) { dest->adr[i] = 0; } diff --git a/bacnet-stack/ports/pic18f6720/dlmstp.h b/bacnet-stack/ports/pic18f6720/dlmstp.h index 1343d7a8..ea316090 100644 --- a/bacnet-stack/ports/pic18f6720/dlmstp.h +++ b/bacnet-stack/ports/pic18f6720/dlmstp.h @@ -46,10 +46,10 @@ #define MAX_MPDU (MAX_HEADER+MAX_PDU) typedef struct dlmstp_packet { - bool ready; /* true if ready to be sent or received */ + bool ready; /* true if ready to be sent or received */ BACNET_ADDRESS address; /* source address */ - uint8_t frame_type; /* type of message */ - unsigned pdu_len; /* packet length */ + uint8_t frame_type; /* type of message */ + unsigned pdu_len; /* packet length */ uint8_t pdu[MAX_MPDU]; /* packet */ } DLMSTP_PACKET; @@ -58,18 +58,24 @@ extern uint16_t MSTP_Packets; #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - void dlmstp_reinit(void); - void dlmstp_init(void); - void dlmstp_cleanup(void); - void dlmstp_millisecond_timer(void); - void dlmstp_task(void); + void dlmstp_reinit( + void); + void dlmstp_init( + void); + void dlmstp_cleanup( + void); + void dlmstp_millisecond_timer( + void); + void dlmstp_task( + void); /* returns number of bytes sent on success, negative on failure */ - int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */ + int dlmstp_send_pdu( + BACNET_ADDRESS * dest, /* destination address */ BACNET_NPDU_DATA * npdu_data, /* network information */ - uint8_t * pdu, /* any data to be sent - may be null */ + uint8_t * pdu, /* any data to be sent - may be null */ unsigned pdu_len); /* number of bytes of data */ /* This parameter represents the value of the Max_Info_Frames property of */ @@ -79,32 +85,41 @@ extern "C" { /* nodes. This may be used to allocate more or less of the available link */ /* bandwidth to particular nodes. If Max_Info_Frames is not writable in a */ /* node, its value shall be 1. */ - void dlmstp_set_max_info_frames(uint8_t max_info_frames); - unsigned dlmstp_max_info_frames(void); + void dlmstp_set_max_info_frames( + uint8_t max_info_frames); + unsigned dlmstp_max_info_frames( + void); /* This parameter represents the value of the Max_Master property of the */ /* node's Device object. The value of Max_Master specifies the highest */ /* allowable address for master nodes. The value of Max_Master shall be */ /* less than or equal to 127. If Max_Master is not writable in a node, */ /* its value shall be 127. */ - void dlmstp_set_max_master(uint8_t max_master); - uint8_t dlmstp_max_master(void); + void dlmstp_set_max_master( + uint8_t max_master); + uint8_t dlmstp_max_master( + void); /* MAC address for MS/TP */ - void dlmstp_set_my_address(uint8_t my_address); - uint8_t dlmstp_my_address(void); + void dlmstp_set_my_address( + uint8_t my_address); + uint8_t dlmstp_my_address( + void); /* BACnet address used in datalink */ - void dlmstp_get_my_address(BACNET_ADDRESS * my_address); - void dlmstp_get_broadcast_address(BACNET_ADDRESS * dest); /* destination address */ + void dlmstp_get_my_address( + BACNET_ADDRESS * my_address); + void dlmstp_get_broadcast_address( + BACNET_ADDRESS * dest); /* destination address */ /* MS/TP state machine functions */ - uint16_t dlmstp_put_receive(uint8_t src, /* source MS/TP address */ - uint8_t * pdu, /* PDU data */ + uint16_t dlmstp_put_receive( + uint8_t src, /* source MS/TP address */ + uint8_t * pdu, /* PDU data */ uint16_t pdu_len); #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/ports/pic18f6720/h_rp.c b/bacnet-stack/ports/pic18f6720/h_rp.c index 4a8acd48..cc50cdb8 100644 --- a/bacnet-stack/ports/pic18f6720/h_rp.c +++ b/bacnet-stack/ports/pic18f6720/h_rp.c @@ -46,9 +46,11 @@ static uint8_t Temp_Buf[MAX_APDU] = { 0 }; -void handler_read_property(uint8_t * service_request, +void handler_read_property( + uint8_t * service_request, uint16_t service_len, - BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data) + BACNET_ADDRESS * src, + BACNET_CONFIRMED_SERVICE_DATA * service_data) { BACNET_READ_PROPERTY_DATA data; int len = 0; @@ -83,98 +85,98 @@ void handler_read_property(uint8_t * service_request, /* most cases will be error */ error = true; switch (data.object_type) { - case OBJECT_DEVICE: - /* FIXME: probably need a length limitation sent with encode */ - if (Device_Valid_Object_Instance_Number(data.object_instance)) { - len = Device_Encode_Property_APDU(&Temp_Buf[0], - data.object_property, - data.array_index, &error_class, &error_code); - if (len >= 0) { - /* encode the APDU portion of the packet */ - data.application_data = &Temp_Buf[0]; - data.application_data_len = len; - /* FIXME: probably need a length limitation sent with encode */ - len = - rp_ack_encode_apdu(&Handler_Transmit_Buffer - [pdu_len], service_data->invoke_id, &data); - error = false; + case OBJECT_DEVICE: + /* FIXME: probably need a length limitation sent with encode */ + if (Device_Valid_Object_Instance_Number(data.object_instance)) { + len = Device_Encode_Property_APDU(&Temp_Buf[0], + data.object_property, + data.array_index, &error_class, &error_code); + if (len >= 0) { + /* encode the APDU portion of the packet */ + data.application_data = &Temp_Buf[0]; + data.application_data_len = len; + /* FIXME: probably need a length limitation sent with encode */ + len = + rp_ack_encode_apdu(&Handler_Transmit_Buffer + [pdu_len], service_data->invoke_id, &data); + error = false; + } } - } - break; - case OBJECT_ANALOG_INPUT: - if (Analog_Input_Valid_Instance(data.object_instance)) { - len = Analog_Input_Encode_Property_APDU(&Temp_Buf[0], - data.object_instance, - data.object_property, - data.array_index, &error_class, &error_code); - if (len >= 0) { - /* encode the APDU portion of the packet */ - data.application_data = &Temp_Buf[0]; - data.application_data_len = len; - /* FIXME: probably need a length limitation sent with encode */ - len = - rp_ack_encode_apdu(&Handler_Transmit_Buffer - [pdu_len], service_data->invoke_id, &data); - error = false; + break; + case OBJECT_ANALOG_INPUT: + if (Analog_Input_Valid_Instance(data.object_instance)) { + len = Analog_Input_Encode_Property_APDU(&Temp_Buf[0], + data.object_instance, + data.object_property, + data.array_index, &error_class, &error_code); + if (len >= 0) { + /* encode the APDU portion of the packet */ + data.application_data = &Temp_Buf[0]; + data.application_data_len = len; + /* FIXME: probably need a length limitation sent with encode */ + len = + rp_ack_encode_apdu(&Handler_Transmit_Buffer + [pdu_len], service_data->invoke_id, &data); + error = false; + } } - } - break; - case OBJECT_BINARY_INPUT: - if (Binary_Input_Valid_Instance(data.object_instance)) { - len = Binary_Input_Encode_Property_APDU(&Temp_Buf[0], - data.object_instance, - data.object_property, - data.array_index, &error_class, &error_code); - if (len >= 0) { - /* encode the APDU portion of the packet */ - data.application_data = &Temp_Buf[0]; - data.application_data_len = len; - /* FIXME: probably need a length limitation sent with encode */ - len = - rp_ack_encode_apdu(&Handler_Transmit_Buffer - [pdu_len], service_data->invoke_id, &data); - error = false; + break; + case OBJECT_BINARY_INPUT: + if (Binary_Input_Valid_Instance(data.object_instance)) { + len = Binary_Input_Encode_Property_APDU(&Temp_Buf[0], + data.object_instance, + data.object_property, + data.array_index, &error_class, &error_code); + if (len >= 0) { + /* encode the APDU portion of the packet */ + data.application_data = &Temp_Buf[0]; + data.application_data_len = len; + /* FIXME: probably need a length limitation sent with encode */ + len = + rp_ack_encode_apdu(&Handler_Transmit_Buffer + [pdu_len], service_data->invoke_id, &data); + error = false; + } } - } - break; - case OBJECT_BINARY_VALUE: - if (Binary_Value_Valid_Instance(data.object_instance)) { - len = Binary_Value_Encode_Property_APDU(&Temp_Buf[0], - data.object_instance, - data.object_property, - data.array_index, &error_class, &error_code); - if (len >= 0) { - /* encode the APDU portion of the packet */ - data.application_data = &Temp_Buf[0]; - data.application_data_len = len; - /* FIXME: probably need a length limitation sent with encode */ - len = - rp_ack_encode_apdu(&Handler_Transmit_Buffer - [pdu_len], service_data->invoke_id, &data); - error = false; + break; + case OBJECT_BINARY_VALUE: + if (Binary_Value_Valid_Instance(data.object_instance)) { + len = Binary_Value_Encode_Property_APDU(&Temp_Buf[0], + data.object_instance, + data.object_property, + data.array_index, &error_class, &error_code); + if (len >= 0) { + /* encode the APDU portion of the packet */ + data.application_data = &Temp_Buf[0]; + data.application_data_len = len; + /* FIXME: probably need a length limitation sent with encode */ + len = + rp_ack_encode_apdu(&Handler_Transmit_Buffer + [pdu_len], service_data->invoke_id, &data); + error = false; + } } - } - break; - case OBJECT_ANALOG_VALUE: - if (Analog_Value_Valid_Instance(data.object_instance)) { - len = Analog_Value_Encode_Property_APDU(&Temp_Buf[0], - data.object_instance, - data.object_property, - data.array_index, &error_class, &error_code); - if (len >= 0) { - /* encode the APDU portion of the packet */ - data.application_data = &Temp_Buf[0]; - data.application_data_len = len; - /* FIXME: probably need a length limitation sent with encode */ - len = - rp_ack_encode_apdu(&Handler_Transmit_Buffer - [pdu_len], service_data->invoke_id, &data); - error = false; + break; + case OBJECT_ANALOG_VALUE: + if (Analog_Value_Valid_Instance(data.object_instance)) { + len = Analog_Value_Encode_Property_APDU(&Temp_Buf[0], + data.object_instance, + data.object_property, + data.array_index, &error_class, &error_code); + if (len >= 0) { + /* encode the APDU portion of the packet */ + data.application_data = &Temp_Buf[0]; + data.application_data_len = len; + /* FIXME: probably need a length limitation sent with encode */ + len = + rp_ack_encode_apdu(&Handler_Transmit_Buffer + [pdu_len], service_data->invoke_id, &data); + error = false; + } } - } - break; - default: - break; + break; + default: + break; } if (error) { if (len == -2) { @@ -188,7 +190,7 @@ void handler_read_property(uint8_t * service_request, service_data->invoke_id, SERVICE_CONFIRMED_READ_PROPERTY, error_class, error_code); } -RP_ABORT: + RP_ABORT: pdu_len += len; bytes_sent = datalink_send_pdu(src, &npdu_data, &Handler_Transmit_Buffer[0], pdu_len); diff --git a/bacnet-stack/ports/pic18f6720/h_wp.c b/bacnet-stack/ports/pic18f6720/h_wp.c index 36259072..7dc80640 100644 --- a/bacnet-stack/ports/pic18f6720/h_wp.c +++ b/bacnet-stack/ports/pic18f6720/h_wp.c @@ -45,9 +45,11 @@ /* too big to reside on stack frame for PIC */ static BACNET_WRITE_PROPERTY_DATA wp_data; -void handler_write_property(uint8_t * service_request, +void handler_write_property( + uint8_t * service_request, uint16_t service_len, - BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data) + BACNET_ADDRESS * src, + BACNET_CONFIRMED_SERVICE_DATA * service_data) { int len = 0; int pdu_len = 0; @@ -69,104 +71,94 @@ void handler_write_property(uint8_t * service_request, goto WP_ABORT; } /* decode the service request only */ - len = wp_decode_service_request(service_request, - service_len, &wp_data); + len = wp_decode_service_request(service_request, service_len, &wp_data); /* bad decoding or something we didn't understand - send an abort */ if (len <= 0) { len = abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, ABORT_REASON_OTHER, true); goto WP_ABORT; - } - switch (wp_data.object_type) { - case OBJECT_DEVICE: - if (Device_Write_Property(&wp_data, &error_class, &error_code)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY); -#if PRINT_ENABLED - fprintf(stderr, - "Sending Write Property Simple Ack for Device!\n"); -#endif - } else { - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, - error_code); -#if PRINT_ENABLED - fprintf(stderr, - "Sending Write Property Error for Device!\n"); -#endif - } - break; - case OBJECT_ANALOG_INPUT: - case OBJECT_BINARY_INPUT: - error_class = ERROR_CLASS_PROPERTY; - error_code = ERROR_CODE_WRITE_ACCESS_DENIED; - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, - error_class, error_code); -#if PRINT_ENABLED - fprintf(stderr, "Sending Write Access Error!\n"); -#endif - break; - case OBJECT_BINARY_VALUE: - if (Binary_Value_Write_Property(&wp_data, &error_class, - &error_code)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY); -#if PRINT_ENABLED - fprintf(stderr, - "Sending Write Property Simple Ack for BV!\n"); -#endif - } else { - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, - error_code); -#if PRINT_ENABLED - fprintf(stderr, "Sending Write Access Error for BV!\n"); -#endif - } - break; - case OBJECT_ANALOG_VALUE: - if (Analog_Value_Write_Property(&wp_data, &error_class, - &error_code)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY); -#if PRINT_ENABLED - fprintf(stderr, - "Sending Write Property Simple Ack for AV!\n"); -#endif - } else { - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, - SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, - error_code); -#if PRINT_ENABLED - fprintf(stderr, "Sending Write Access Error for AV!\n"); -#endif - } - break; - default: - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, - error_class, error_code); -#if PRINT_ENABLED - fprintf(stderr, "Sending Unknown Object Error!\n"); -#endif - break; } -WP_ABORT: + switch (wp_data.object_type) { + case OBJECT_DEVICE: + if (Device_Write_Property(&wp_data, &error_class, &error_code)) { + len = + encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY); +#if PRINT_ENABLED + fprintf(stderr, + "Sending Write Property Simple Ack for Device!\n"); +#endif + } else { + len = + bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, error_code); +#if PRINT_ENABLED + fprintf(stderr, "Sending Write Property Error for Device!\n"); +#endif + } + break; + case OBJECT_ANALOG_INPUT: + case OBJECT_BINARY_INPUT: + error_class = ERROR_CLASS_PROPERTY; + error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + len = + bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, + error_class, error_code); +#if PRINT_ENABLED + fprintf(stderr, "Sending Write Access Error!\n"); +#endif + break; + case OBJECT_BINARY_VALUE: + if (Binary_Value_Write_Property(&wp_data, &error_class, + &error_code)) { + len = + encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY); +#if PRINT_ENABLED + fprintf(stderr, "Sending Write Property Simple Ack for BV!\n"); +#endif + } else { + len = + bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, error_code); +#if PRINT_ENABLED + fprintf(stderr, "Sending Write Access Error for BV!\n"); +#endif + } + break; + case OBJECT_ANALOG_VALUE: + if (Analog_Value_Write_Property(&wp_data, &error_class, + &error_code)) { + len = + encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY); +#if PRINT_ENABLED + fprintf(stderr, "Sending Write Property Simple Ack for AV!\n"); +#endif + } else { + len = + bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, error_code); +#if PRINT_ENABLED + fprintf(stderr, "Sending Write Access Error for AV!\n"); +#endif + } + break; + default: + len = + bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, + error_class, error_code); +#if PRINT_ENABLED + fprintf(stderr, "Sending Unknown Object Error!\n"); +#endif + break; + } + WP_ABORT: pdu_len += len; bytes_sent = datalink_send_pdu(src, &npdu_data, &Handler_Transmit_Buffer[0], pdu_len); diff --git a/bacnet-stack/ports/pic18f6720/hardware.h b/bacnet-stack/ports/pic18f6720/hardware.h index a60744d5..a048e830 100644 --- a/bacnet-stack/ports/pic18f6720/hardware.h +++ b/bacnet-stack/ports/pic18f6720/hardware.h @@ -132,8 +132,8 @@ typedef union { uint8_t Program:1; uint8_t Run:1; - uint8_t:1; - uint8_t:1; + uint8_t:1; + uint8_t:1; uint8_t Input1:1; uint8_t Input2:1; uint8_t Input3:1; @@ -141,35 +141,35 @@ typedef union { uint8_t Input5:1; uint8_t Input6:1; - uint8_t:1; - uint8_t:1; - uint8_t:1; + uint8_t:1; + uint8_t:1; + uint8_t:1; uint8_t Input7:1; - uint8_t:1; - uint8_t:1; + uint8_t:1; + uint8_t:1; uint8_t Input8:1; uint8_t Photocell:1; - uint8_t:1; - uint8_t:1; - uint8_t:1; - uint8_t:1; - uint8_t:1; - uint8_t:1; + uint8_t:1; + uint8_t:1; + uint8_t:1; + uint8_t:1; + uint8_t:1; + uint8_t:1; uint8_t Remote:1; uint8_t Relay8:1; - uint8_t:1; - uint8_t:1; - uint8_t:1; + uint8_t:1; + uint8_t:1; + uint8_t:1; uint8_t Relay7:1; uint8_t Relay6:1; uint8_t Relay5:1; uint8_t Relay4:1; uint8_t Relay3:1; - uint8_t:1; - uint8_t:1; + uint8_t:1; + uint8_t:1; uint8_t Relay2:1; uint8_t Relay1:1; uint8_t Holiday:1; @@ -268,4 +268,4 @@ extern volatile LED_REGS Blink; extern uint8_t Piezo_Timer; extern volatile bool DataPortLocked; -#endif /* HARDWARE_H */ +#endif /* HARDWARE_H */ diff --git a/bacnet-stack/ports/pic18f6720/isr.c b/bacnet-stack/ports/pic18f6720/isr.c index 5a84fc46..e09b145c 100644 --- a/bacnet-stack/ports/pic18f6720/isr.c +++ b/bacnet-stack/ports/pic18f6720/isr.c @@ -31,30 +31,42 @@ /* from main.c */ extern volatile uint8_t Milliseconds; -void InterruptHandlerHigh(void); -void InterruptHandlerLow(void); -void Interrupt_Timer2(void); -void Interrupt_Timer3(void); -void Interrupt_Timer4(void); -void Interrupt_USART_Rx(void); -void Interrupt_USART_Tx(void); -void Interrupt_CCP2(void); -void INT0_Interrupt(void); +void InterruptHandlerHigh( + void); +void InterruptHandlerLow( + void); +void Interrupt_Timer2( + void); +void Interrupt_Timer3( + void); +void Interrupt_Timer4( + void); +void Interrupt_USART_Rx( + void); +void Interrupt_USART_Tx( + void); +void Interrupt_CCP2( + void); +void INT0_Interrupt( + void); #pragma code InterruptVectorHigh = 0x08 -void InterruptVectorHigh(void) +void InterruptVectorHigh( + void) { /* jump to interrupt routine */ _asm goto InterruptHandlerHigh _endasm} #pragma code #pragma code InterruptVectorLow = 0x18 -void InterruptVectorLow(void) +void InterruptVectorLow( + void) { /* jump to interrupt routine */ _asm goto InterruptHandlerLow _endasm} #pragma code #pragma interrupt InterruptHandlerHigh -void InterruptHandlerHigh(void) +void InterruptHandlerHigh( + void) { #if 0 /* check for USART Rx int */ @@ -84,7 +96,8 @@ void InterruptHandlerHigh(void) #pragma interruptlow InterruptHandlerLow save = PROD, section(".tmpdata"), TABLAT, TBLPTR, section \ ("MATH_DATA") -void InterruptHandlerLow(void) +void InterruptHandlerLow( + void) { /* check for timer2 int */ if ((PIR1bits.TMR2IF) && (PIE1bits.TMR2IE)) { @@ -168,23 +181,27 @@ void InterruptHandlerLow(void) */ } -void Interrupt_Timer2(void) +void Interrupt_Timer2( + void) { } -void Interrupt_Timer3(void) +void Interrupt_Timer3( + void) { } /* Timer4 is set to go off every 1ms. This is our system tick */ -void Interrupt_Timer4(void) +void Interrupt_Timer4( + void) { /* Milisecond is our system tick */ if (Milliseconds < 0xFF) ++Milliseconds; } -void Interrupt_CCP2(void) +void Interrupt_CCP2( + void) { } diff --git a/bacnet-stack/ports/pic18f6720/main.c b/bacnet-stack/ports/pic18f6720/main.c index abaeb5be..0bd15ecb 100644 --- a/bacnet-stack/ports/pic18f6720/main.c +++ b/bacnet-stack/ports/pic18f6720/main.c @@ -25,7 +25,7 @@ #include #include -#include /* for memmove */ +#include /* for memmove */ #include #include #include @@ -87,16 +87,16 @@ #else #pragma config WDT = ON, WDTPS = 128 #pragma config DEBUG = OFF -#endif /* USE_ICD */ +#endif /* USE_ICD */ volatile uint8_t Milliseconds = 0; volatile uint8_t Zero_Cross_Timeout = 0; -static void BACnet_Service_Handlers_Init(void) +static void BACnet_Service_Handlers_Init( + void) { /* we need to handle who-is to support dynamic device binding */ - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, - handler_who_is); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is); /* Set the handlers for any confirmed services that we support. */ /* We must implement read property - it's required! */ apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY, @@ -107,8 +107,7 @@ static void BACnet_Service_Handlers_Init(void) handler_write_property); #if 0 apdu_set_unconfirmed_handler - (SERVICE_UNCONFIRMED_UTC_TIME_SYNCHRONIZATION, - handler_timesync_utc); + (SERVICE_UNCONFIRMED_UTC_TIME_SYNCHRONIZATION, handler_timesync_utc); apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_TIME_SYNCHRONIZATION, handler_timesync); #endif @@ -118,7 +117,8 @@ static void BACnet_Service_Handlers_Init(void) handler_device_communication_control); } -void Reinitialize(void) +void Reinitialize( + void) { uint8_t i; char name = 0; @@ -126,30 +126,32 @@ void Reinitialize(void) _asm reset _endasm return; } -void Global_Int(enum INT_STATE state) +void Global_Int( + enum INT_STATE state) { static uint8_t intstate = 0; switch (state) { - case INT_DISABLED: - intstate >>= 2; - intstate |= (INTCON & 0xC0); - break; - case INT_ENABLED: - INTCONbits.GIE = 1; - INTCONbits.PEIE = 1; - intstate <<= 2; - break; - case INT_RESTORE: - INTCON |= (intstate & 0xC0); - intstate <<= 2; - break; - default: - break; + case INT_DISABLED: + intstate >>= 2; + intstate |= (INTCON & 0xC0); + break; + case INT_ENABLED: + INTCONbits.GIE = 1; + INTCONbits.PEIE = 1; + intstate <<= 2; + break; + case INT_RESTORE: + INTCON |= (intstate & 0xC0); + intstate <<= 2; + break; + default: + break; } } -void Hardware_Initialize(void) +void Hardware_Initialize( + void) { /* PORTA.0 Input - Photocell PORTA.1 Output - LED Row6 PORTA.2 Output * - LED Row5 PORTA.3 Output - LED Row4 PORTA.4 Input - Square Wave @@ -247,7 +249,8 @@ void Hardware_Initialize(void) Global_Int(INT_ENABLED); } -void Initialize_Variables(void) +void Initialize_Variables( + void) { /* Check to see if we need to initialize our eeproms */ ENABLE_TIMER4_INT(); @@ -259,7 +262,8 @@ void Initialize_Variables(void) Milliseconds = 0; } -void MainTasks(void) +void MainTasks( + void) { static uint16_t millisecond_counter = 0; /* Handle our millisecond counters */ @@ -274,7 +278,8 @@ void MainTasks(void) } } -void main(void) +void main( + void) { RCONbits.NOT_POR = 1; RCONbits.NOT_RI = 1; diff --git a/bacnet-stack/ports/pic18f6720/mstp.c b/bacnet-stack/ports/pic18f6720/mstp.c index ff93ab5e..2c980154 100644 --- a/bacnet-stack/ports/pic18f6720/mstp.c +++ b/bacnet-stack/ports/pic18f6720/mstp.c @@ -150,22 +150,24 @@ /* we need to be able to increment without rolling over */ #define INCREMENT_AND_LIMIT_UINT8(x) {if (x < 0xFF) x++;} -bool MSTP_Line_Active(volatile struct mstp_port_struct_t *mstp_port) +bool MSTP_Line_Active( + volatile struct mstp_port_struct_t *mstp_port) { return (mstp_port->EventCount > Nmin_octets); } -unsigned MSTP_Create_Frame(uint8_t * buffer, /* where frame is loaded */ +unsigned MSTP_Create_Frame( + uint8_t * buffer, /* where frame is loaded */ unsigned buffer_len, /* amount of space available */ - uint8_t frame_type, /* type of frame to send - see defines */ + uint8_t frame_type, /* type of frame to send - see defines */ uint8_t destination, /* destination address */ - uint8_t source, /* source address */ - uint8_t * data, /* any data to be sent - may be null */ + uint8_t source, /* source address */ + uint8_t * data, /* any data to be sent - may be null */ unsigned data_len) -{ /* number of bytes of data (up to 501) */ +{ /* number of bytes of data (up to 501) */ uint8_t crc8 = 0xFF; /* used to calculate the crc value */ uint16_t crc16 = 0xFFFF; /* used to calculate the crc value */ - unsigned index = 0; /* used to load the data portion of the frame */ + unsigned index = 0; /* used to load the data portion of the frame */ /* not enough to do a header */ if (buffer_len < 8) @@ -205,32 +207,34 @@ unsigned MSTP_Create_Frame(uint8_t * buffer, /* where frame is loaded */ return 0; } - return index; /* returns the frame length */ + return index; /* returns the frame length */ } -void MSTP_Create_And_Send_Frame(volatile struct mstp_port_struct_t *mstp_port, /* port to send from */ - uint8_t frame_type, /* type of frame to send - see defines */ +void MSTP_Create_And_Send_Frame( + volatile struct mstp_port_struct_t *mstp_port, /* port to send from */ + uint8_t frame_type, /* type of frame to send - see defines */ uint8_t destination, /* destination address */ - uint8_t source, /* source address */ - uint8_t * data, /* any data to be sent - may be null */ + uint8_t source, /* source address */ + uint8_t * data, /* any data to be sent - may be null */ unsigned data_len) -{ /* number of bytes of data (up to 501) */ +{ /* number of bytes of data (up to 501) */ uint8_t buffer[MAX_MPDU] = { 0 }; /* buffer for sending */ - uint16_t len = 0; /* number of bytes to send */ + uint16_t len = 0; /* number of bytes to send */ len = (uint16_t) MSTP_Create_Frame(&buffer[0], /* where frame is loaded */ - sizeof(buffer), /* amount of space available */ - frame_type, /* type of frame to send - see defines */ - destination, /* destination address */ - source, /* source address */ - data, /* any data to be sent - may be null */ - data_len); /* number of bytes of data (up to 501) */ + sizeof(buffer), /* amount of space available */ + frame_type, /* type of frame to send - see defines */ + destination, /* destination address */ + source, /* source address */ + data, /* any data to be sent - may be null */ + data_len); /* number of bytes of data (up to 501) */ RS485_Send_Frame(mstp_port, &buffer[0], len); /* FIXME: be sure to reset SilenceTimer after each octet is sent! */ } -void MSTP_Receive_Frame_FSM(volatile struct mstp_port_struct_t *mstp_port) +void MSTP_Receive_Frame_FSM( + volatile struct mstp_port_struct_t *mstp_port) { #if PRINT_ENABLED_RECEIVE_DATA static MSTP_RECEIVE_STATE receive_state = MSTP_RECEIVE_STATE_IDLE; @@ -240,303 +244,302 @@ void MSTP_Receive_Frame_FSM(volatile struct mstp_port_struct_t *mstp_port) "MSTP Rx: State=%s Data=%02X hCRC=%02X Index=%u EC=%u DateLen=%u Silence=%u\n", mstptext_receive_state(mstp_port->receive_state), mstp_port->DataRegister, mstp_port->HeaderCRC, mstp_port->Index, - mstp_port->EventCount, mstp_port->DataLength, - mstp_port->SilenceTimer); + mstp_port->EventCount, mstp_port->DataLength, mstp_port->SilenceTimer); #endif switch (mstp_port->receive_state) { - /* In the IDLE state, the node waits for the beginning of a frame. */ - case MSTP_RECEIVE_STATE_IDLE: - /* EatAnError */ - if (mstp_port->ReceiveError == true) { - mstp_port->ReceiveError = false; - mstp_port->SilenceTimer = 0; - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - /* wait for the start of a frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } else if (mstp_port->DataAvailable == true) { -#if PRINT_ENABLED_RECEIVE_DATA - fprintf(stderr, "MSTP Rx: %02X ", mstp_port->DataRegister); -#endif - /* Preamble1 */ - if (mstp_port->DataRegister == 0x55) { - mstp_port->DataAvailable = false; - mstp_port->SilenceTimer = 0; - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - /* receive the remainder of the frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_PREAMBLE; - } - /* EatAnOctet */ - else { -#if PRINT_ENABLED_RECEIVE_DATA - fprintf(stderr, "\n"); -#endif - mstp_port->DataAvailable = false; - mstp_port->SilenceTimer = 0; - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - /* wait for the start of a frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } - } - break; - /* In the PREAMBLE state, the node waits for the second octet of the preamble. */ - case MSTP_RECEIVE_STATE_PREAMBLE: - /* Timeout */ - if (mstp_port->SilenceTimer > Tframe_abort) { - /* a correct preamble has not been received */ - /* wait for the start of a frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } - /* Error */ - else if (mstp_port->ReceiveError == true) { - mstp_port->ReceiveError = false; - mstp_port->SilenceTimer = 0; - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - /* wait for the start of a frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } else if (mstp_port->DataAvailable == true) { -#if PRINT_ENABLED_RECEIVE_DATA - fprintf(stderr, "%02X ", mstp_port->DataRegister); -#endif - /* Preamble2 */ - if (mstp_port->DataRegister == 0xFF) { - mstp_port->DataAvailable = false; - mstp_port->SilenceTimer = 0; - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - mstp_port->Index = 0; - mstp_port->HeaderCRC = 0xFF; - /* receive the remainder of the frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER; - } - /* ignore RepeatedPreamble1 */ - else if (mstp_port->DataRegister == 0x55) { - mstp_port->DataAvailable = false; - mstp_port->SilenceTimer = 0; - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - /* wait for the second preamble octet. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_PREAMBLE; - } - /* NotPreamble */ - else { - mstp_port->DataAvailable = false; - mstp_port->SilenceTimer = 0; - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - /* wait for the start of a frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } - } - break; - /* In the HEADER state, the node waits for the fixed message header. */ - case MSTP_RECEIVE_STATE_HEADER: - /* Timeout */ - if (mstp_port->SilenceTimer > Tframe_abort) { - /* indicate that an error has occurred during the reception of a frame */ - mstp_port->ReceivedInvalidFrame = true; - /* wait for the start of a frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } - /* Error */ - else if (mstp_port->ReceiveError == true) { - mstp_port->ReceiveError = false; - mstp_port->SilenceTimer = 0; - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - /* indicate that an error has occurred during the reception of a frame */ - mstp_port->ReceivedInvalidFrame = true; - /* wait for the start of a frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } else if (mstp_port->DataAvailable == true) { -#if PRINT_ENABLED_RECEIVE_DATA - fprintf(stderr, "%02X ", mstp_port->DataRegister); -#endif - /* FrameType */ - if (mstp_port->Index == 0) { - mstp_port->SilenceTimer = 0; - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - mstp_port->HeaderCRC = - CRC_Calc_Header(mstp_port->DataRegister, - mstp_port->HeaderCRC); - mstp_port->FrameType = mstp_port->DataRegister; - mstp_port->DataAvailable = false; - mstp_port->Index = 1; - mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER; - } - /* Destination */ - else if (mstp_port->Index == 1) { - mstp_port->SilenceTimer = 0; - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - mstp_port->HeaderCRC = - CRC_Calc_Header(mstp_port->DataRegister, - mstp_port->HeaderCRC); - mstp_port->DestinationAddress = mstp_port->DataRegister; - mstp_port->DataAvailable = false; - mstp_port->Index = 2; - mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER; - } - /* Source */ - else if (mstp_port->Index == 2) { - mstp_port->SilenceTimer = 0; - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - mstp_port->HeaderCRC = - CRC_Calc_Header(mstp_port->DataRegister, - mstp_port->HeaderCRC); - mstp_port->SourceAddress = mstp_port->DataRegister; - mstp_port->DataAvailable = false; - mstp_port->Index = 3; - mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER; - } - /* Length1 */ - else if (mstp_port->Index == 3) { - mstp_port->SilenceTimer = 0; - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - mstp_port->HeaderCRC = - CRC_Calc_Header(mstp_port->DataRegister, - mstp_port->HeaderCRC); - mstp_port->DataLength = mstp_port->DataRegister * 256; - mstp_port->DataAvailable = false; - mstp_port->Index = 4; - mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER; - } - /* Length2 */ - else if (mstp_port->Index == 4) { - mstp_port->SilenceTimer = 0; - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - mstp_port->HeaderCRC = - CRC_Calc_Header(mstp_port->DataRegister, - mstp_port->HeaderCRC); - mstp_port->DataLength += mstp_port->DataRegister; - mstp_port->DataAvailable = false; - mstp_port->Index = 5; - mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER; - } - /* HeaderCRC */ - else if (mstp_port->Index == 5) { - mstp_port->SilenceTimer = 0; - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - mstp_port->HeaderCRC = - CRC_Calc_Header(mstp_port->DataRegister, - mstp_port->HeaderCRC); - mstp_port->DataAvailable = false; - /* don't wait for next state - do it here */ - /* MSTP_RECEIVE_STATE_HEADER_CRC */ - if (mstp_port->HeaderCRC != 0x55) { - /* BadCRC */ - /* indicate that an error has occurred during the reception of a frame */ - mstp_port->ReceivedInvalidFrame = true; - /* wait for the start of the next frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } else { - if ((mstp_port->DestinationAddress == - mstp_port->This_Station) - || (mstp_port->DestinationAddress == - MSTP_BROADCAST_ADDRESS)) { - /* FrameTooLong */ - if (mstp_port->DataLength > MAX_MPDU) { - /* indicate that a frame with an illegal or */ - /* unacceptable data length has been received */ - mstp_port->ReceivedInvalidFrame = true; - /* wait for the start of the next frame. */ - mstp_port->receive_state = - MSTP_RECEIVE_STATE_IDLE; - } - /* NoData */ - else if (mstp_port->DataLength == 0) { - /* indicate that a frame with no data has been received */ - mstp_port->ReceivedValidFrame = true; - /* wait for the start of the next frame. */ - mstp_port->receive_state = - MSTP_RECEIVE_STATE_IDLE; - } - /* Data */ - else { - mstp_port->Index = 0; - mstp_port->DataCRC = 0xFFFF; - /* receive the data portion of the frame. */ - mstp_port->receive_state = - MSTP_RECEIVE_STATE_DATA; - } - } - /* NotForUs */ - else { - /* wait for the start of the next frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } - } - - - } - /* not per MS/TP standard, but it is a case not covered */ - else { + /* In the IDLE state, the node waits for the beginning of a frame. */ + case MSTP_RECEIVE_STATE_IDLE: + /* EatAnError */ + if (mstp_port->ReceiveError == true) { mstp_port->ReceiveError = false; mstp_port->SilenceTimer = 0; INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - /* indicate that an error has occurred during */ - /* the reception of a frame */ + /* wait for the start of a frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + } else if (mstp_port->DataAvailable == true) { +#if PRINT_ENABLED_RECEIVE_DATA + fprintf(stderr, "MSTP Rx: %02X ", mstp_port->DataRegister); +#endif + /* Preamble1 */ + if (mstp_port->DataRegister == 0x55) { + mstp_port->DataAvailable = false; + mstp_port->SilenceTimer = 0; + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + /* receive the remainder of the frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_PREAMBLE; + } + /* EatAnOctet */ + else { +#if PRINT_ENABLED_RECEIVE_DATA + fprintf(stderr, "\n"); +#endif + mstp_port->DataAvailable = false; + mstp_port->SilenceTimer = 0; + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + /* wait for the start of a frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + } + } + break; + /* In the PREAMBLE state, the node waits for the second octet of the preamble. */ + case MSTP_RECEIVE_STATE_PREAMBLE: + /* Timeout */ + if (mstp_port->SilenceTimer > Tframe_abort) { + /* a correct preamble has not been received */ + /* wait for the start of a frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + } + /* Error */ + else if (mstp_port->ReceiveError == true) { + mstp_port->ReceiveError = false; + mstp_port->SilenceTimer = 0; + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + /* wait for the start of a frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + } else if (mstp_port->DataAvailable == true) { +#if PRINT_ENABLED_RECEIVE_DATA + fprintf(stderr, "%02X ", mstp_port->DataRegister); +#endif + /* Preamble2 */ + if (mstp_port->DataRegister == 0xFF) { + mstp_port->DataAvailable = false; + mstp_port->SilenceTimer = 0; + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + mstp_port->Index = 0; + mstp_port->HeaderCRC = 0xFF; + /* receive the remainder of the frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER; + } + /* ignore RepeatedPreamble1 */ + else if (mstp_port->DataRegister == 0x55) { + mstp_port->DataAvailable = false; + mstp_port->SilenceTimer = 0; + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + /* wait for the second preamble octet. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_PREAMBLE; + } + /* NotPreamble */ + else { + mstp_port->DataAvailable = false; + mstp_port->SilenceTimer = 0; + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + /* wait for the start of a frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + } + } + break; + /* In the HEADER state, the node waits for the fixed message header. */ + case MSTP_RECEIVE_STATE_HEADER: + /* Timeout */ + if (mstp_port->SilenceTimer > Tframe_abort) { + /* indicate that an error has occurred during the reception of a frame */ mstp_port->ReceivedInvalidFrame = true; /* wait for the start of a frame. */ mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; } - } - break; - /* In the HEADER_CRC state, the node validates the CRC on the fixed */ - /* message header. */ - case MSTP_RECEIVE_STATE_HEADER_CRC: - break; - /* In the DATA state, the node waits for the data portion of a frame. */ - case MSTP_RECEIVE_STATE_DATA: - /* Timeout */ - if (mstp_port->SilenceTimer > Tframe_abort) { - /* indicate that an error has occurred during the reception of a frame */ - mstp_port->ReceivedInvalidFrame = true; - /* wait for the start of the next frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } - /* Error */ - else if (mstp_port->ReceiveError == true) { - mstp_port->ReceiveError = false; - mstp_port->SilenceTimer = 0; - /* indicate that an error has occurred during the reception of a frame */ - mstp_port->ReceivedInvalidFrame = true; - /* wait for the start of the next frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } else if (mstp_port->DataAvailable == true) { + /* Error */ + else if (mstp_port->ReceiveError == true) { + mstp_port->ReceiveError = false; + mstp_port->SilenceTimer = 0; + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + /* indicate that an error has occurred during the reception of a frame */ + mstp_port->ReceivedInvalidFrame = true; + /* wait for the start of a frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + } else if (mstp_port->DataAvailable == true) { #if PRINT_ENABLED_RECEIVE_DATA - fprintf(stderr, "%02X ", mstp_port->DataRegister); + fprintf(stderr, "%02X ", mstp_port->DataRegister); #endif - /* DataOctet */ - if (mstp_port->Index < mstp_port->DataLength) { - mstp_port->DataCRC = CRC_Calc_Data(mstp_port->DataRegister, - mstp_port->DataCRC); - mstp_port->InputBuffer[mstp_port->Index] = - mstp_port->DataRegister; - mstp_port->Index++; - mstp_port->receive_state = MSTP_RECEIVE_STATE_DATA; - } - /* CRC1 */ - else if (mstp_port->Index == mstp_port->DataLength) { - mstp_port->DataCRC = CRC_Calc_Data(mstp_port->DataRegister, - mstp_port->DataCRC); - mstp_port->Index++; - mstp_port->receive_state = MSTP_RECEIVE_STATE_DATA; - } - /* CRC2 */ - else if (mstp_port->Index == (mstp_port->DataLength + 1)) { - mstp_port->DataCRC = CRC_Calc_Data(mstp_port->DataRegister, - mstp_port->DataCRC); - /* STATE DATA CRC - no need for new state */ - /* indicate the complete reception of a valid frame */ - if (mstp_port->DataCRC == 0xF0B8) - mstp_port->ReceivedValidFrame = true; - else + /* FrameType */ + if (mstp_port->Index == 0) { + mstp_port->SilenceTimer = 0; + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + mstp_port->HeaderCRC = + CRC_Calc_Header(mstp_port->DataRegister, + mstp_port->HeaderCRC); + mstp_port->FrameType = mstp_port->DataRegister; + mstp_port->DataAvailable = false; + mstp_port->Index = 1; + mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER; + } + /* Destination */ + else if (mstp_port->Index == 1) { + mstp_port->SilenceTimer = 0; + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + mstp_port->HeaderCRC = + CRC_Calc_Header(mstp_port->DataRegister, + mstp_port->HeaderCRC); + mstp_port->DestinationAddress = mstp_port->DataRegister; + mstp_port->DataAvailable = false; + mstp_port->Index = 2; + mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER; + } + /* Source */ + else if (mstp_port->Index == 2) { + mstp_port->SilenceTimer = 0; + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + mstp_port->HeaderCRC = + CRC_Calc_Header(mstp_port->DataRegister, + mstp_port->HeaderCRC); + mstp_port->SourceAddress = mstp_port->DataRegister; + mstp_port->DataAvailable = false; + mstp_port->Index = 3; + mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER; + } + /* Length1 */ + else if (mstp_port->Index == 3) { + mstp_port->SilenceTimer = 0; + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + mstp_port->HeaderCRC = + CRC_Calc_Header(mstp_port->DataRegister, + mstp_port->HeaderCRC); + mstp_port->DataLength = mstp_port->DataRegister * 256; + mstp_port->DataAvailable = false; + mstp_port->Index = 4; + mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER; + } + /* Length2 */ + else if (mstp_port->Index == 4) { + mstp_port->SilenceTimer = 0; + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + mstp_port->HeaderCRC = + CRC_Calc_Header(mstp_port->DataRegister, + mstp_port->HeaderCRC); + mstp_port->DataLength += mstp_port->DataRegister; + mstp_port->DataAvailable = false; + mstp_port->Index = 5; + mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER; + } + /* HeaderCRC */ + else if (mstp_port->Index == 5) { + mstp_port->SilenceTimer = 0; + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + mstp_port->HeaderCRC = + CRC_Calc_Header(mstp_port->DataRegister, + mstp_port->HeaderCRC); + mstp_port->DataAvailable = false; + /* don't wait for next state - do it here */ + /* MSTP_RECEIVE_STATE_HEADER_CRC */ + if (mstp_port->HeaderCRC != 0x55) { + /* BadCRC */ + /* indicate that an error has occurred during the reception of a frame */ + mstp_port->ReceivedInvalidFrame = true; + /* wait for the start of the next frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + } else { + if ((mstp_port->DestinationAddress == + mstp_port->This_Station) + || (mstp_port->DestinationAddress == + MSTP_BROADCAST_ADDRESS)) { + /* FrameTooLong */ + if (mstp_port->DataLength > MAX_MPDU) { + /* indicate that a frame with an illegal or */ + /* unacceptable data length has been received */ + mstp_port->ReceivedInvalidFrame = true; + /* wait for the start of the next frame. */ + mstp_port->receive_state = + MSTP_RECEIVE_STATE_IDLE; + } + /* NoData */ + else if (mstp_port->DataLength == 0) { + /* indicate that a frame with no data has been received */ + mstp_port->ReceivedValidFrame = true; + /* wait for the start of the next frame. */ + mstp_port->receive_state = + MSTP_RECEIVE_STATE_IDLE; + } + /* Data */ + else { + mstp_port->Index = 0; + mstp_port->DataCRC = 0xFFFF; + /* receive the data portion of the frame. */ + mstp_port->receive_state = + MSTP_RECEIVE_STATE_DATA; + } + } + /* NotForUs */ + else { + /* wait for the start of the next frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + } + } + + + } + /* not per MS/TP standard, but it is a case not covered */ + else { + mstp_port->ReceiveError = false; + mstp_port->SilenceTimer = 0; + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + /* indicate that an error has occurred during */ + /* the reception of a frame */ mstp_port->ReceivedInvalidFrame = true; + /* wait for the start of a frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + } + } + break; + /* In the HEADER_CRC state, the node validates the CRC on the fixed */ + /* message header. */ + case MSTP_RECEIVE_STATE_HEADER_CRC: + break; + /* In the DATA state, the node waits for the data portion of a frame. */ + case MSTP_RECEIVE_STATE_DATA: + /* Timeout */ + if (mstp_port->SilenceTimer > Tframe_abort) { + /* indicate that an error has occurred during the reception of a frame */ + mstp_port->ReceivedInvalidFrame = true; + /* wait for the start of the next frame. */ mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; } - mstp_port->DataAvailable = false; - mstp_port->SilenceTimer = 0; - } - break; - default: - /* shouldn't get here - but if we do... */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - break; + /* Error */ + else if (mstp_port->ReceiveError == true) { + mstp_port->ReceiveError = false; + mstp_port->SilenceTimer = 0; + /* indicate that an error has occurred during the reception of a frame */ + mstp_port->ReceivedInvalidFrame = true; + /* wait for the start of the next frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + } else if (mstp_port->DataAvailable == true) { +#if PRINT_ENABLED_RECEIVE_DATA + fprintf(stderr, "%02X ", mstp_port->DataRegister); +#endif + /* DataOctet */ + if (mstp_port->Index < mstp_port->DataLength) { + mstp_port->DataCRC = CRC_Calc_Data(mstp_port->DataRegister, + mstp_port->DataCRC); + mstp_port->InputBuffer[mstp_port->Index] = + mstp_port->DataRegister; + mstp_port->Index++; + mstp_port->receive_state = MSTP_RECEIVE_STATE_DATA; + } + /* CRC1 */ + else if (mstp_port->Index == mstp_port->DataLength) { + mstp_port->DataCRC = CRC_Calc_Data(mstp_port->DataRegister, + mstp_port->DataCRC); + mstp_port->Index++; + mstp_port->receive_state = MSTP_RECEIVE_STATE_DATA; + } + /* CRC2 */ + else if (mstp_port->Index == (mstp_port->DataLength + 1)) { + mstp_port->DataCRC = CRC_Calc_Data(mstp_port->DataRegister, + mstp_port->DataCRC); + /* STATE DATA CRC - no need for new state */ + /* indicate the complete reception of a valid frame */ + if (mstp_port->DataCRC == 0xF0B8) + mstp_port->ReceivedValidFrame = true; + else + mstp_port->ReceivedInvalidFrame = true; + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + } + mstp_port->DataAvailable = false; + mstp_port->SilenceTimer = 0; + } + break; + default: + /* shouldn't get here - but if we do... */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + break; } #if PRINT_ENABLED_RECEIVE_DATA if ((receive_state != MSTP_RECEIVE_STATE_IDLE) && @@ -551,7 +554,8 @@ void MSTP_Receive_Frame_FSM(volatile struct mstp_port_struct_t *mstp_port) } /* returns true if we need to transition immediately */ -bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t * mstp_port) +bool MSTP_Master_Node_FSM( + volatile struct mstp_port_struct_t * mstp_port) { int mtu_len = 0; int frame_type = 0; @@ -591,489 +595,493 @@ bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t * mstp_port) #endif switch (mstp_port->master_state) { - case MSTP_MASTER_STATE_INITIALIZE: - /* DoneInitializing */ - /* indicate that the next station is unknown */ - mstp_port->Next_Station = mstp_port->This_Station; - mstp_port->Poll_Station = mstp_port->This_Station; - /* cause a Poll For Master to be sent when this node first */ - /* receives the token */ - mstp_port->TokenCount = Npoll; - mstp_port->SoleMaster = false; - mstp_port->master_state = MSTP_MASTER_STATE_IDLE; - transition_now = true; - break; - /* In the IDLE state, the node waits for a frame. */ - case MSTP_MASTER_STATE_IDLE: - /* LostToken */ - if (mstp_port->SilenceTimer >= Tno_token) { - /* assume that the token has been lost */ - mstp_port->EventCount = 0; /* Addendum 135-2004d-8 */ - mstp_port->master_state = MSTP_MASTER_STATE_NO_TOKEN; - transition_now = true; - } - /* ReceivedInvalidFrame */ - else if (mstp_port->ReceivedInvalidFrame == true) { - /* invalid frame was received */ - mstp_port->ReceivedInvalidFrame = false; - /* wait for the next frame - remain in IDLE */ - } else if (mstp_port->ReceivedValidFrame == true) { -#if PRINT_ENABLED_MASTER - fprintf(stderr, - "MSTP: ReceivedValidFrame Src=%02X Dest=%02X DataLen=%u FC=%u ST=%u Type=%s\n", - mstp_port->SourceAddress, - mstp_port->DestinationAddress, - mstp_port->DataLength, - mstp_port->FrameCount, - mstp_port->SilenceTimer, - mstptext_frame_type(mstp_port->FrameType)); -#endif - /* destined for me! */ - if ((mstp_port->DestinationAddress == - mstp_port->This_Station) || - (mstp_port->DestinationAddress == - MSTP_BROADCAST_ADDRESS)) { - switch (mstp_port->FrameType) { - /* ReceivedToken */ - case FRAME_TYPE_TOKEN: - /* tokens can't be broadcast */ - if (mstp_port->DestinationAddress == - MSTP_BROADCAST_ADDRESS) - break; - mstp_port->ReceivedValidFrame = false; - mstp_port->FrameCount = 0; - mstp_port->SoleMaster = false; - mstp_port->master_state = MSTP_MASTER_STATE_USE_TOKEN; - transition_now = true; - break; - /* ReceivedPFM */ - case FRAME_TYPE_POLL_FOR_MASTER: - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER, - mstp_port->SourceAddress, mstp_port->This_Station, - NULL, 0); - break; - case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: - /* indicate successful reception to the higher layers */ - dlmstp_put_receive(mstp_port->SourceAddress, - (uint8_t *) & mstp_port->InputBuffer[0], - mstp_port->DataLength); - break; - case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: - /*mstp_port->ReplyPostponedTimer = 0; */ - /* indicate successful reception to the higher layers */ - dlmstp_put_receive(mstp_port->SourceAddress, - (uint8_t *) & mstp_port->InputBuffer[0], - mstp_port->DataLength); - /* broadcast DER just remains IDLE */ - if (mstp_port->DestinationAddress != - MSTP_BROADCAST_ADDRESS) { - mstp_port->master_state = - MSTP_MASTER_STATE_ANSWER_DATA_REQUEST; - } - break; - case FRAME_TYPE_TEST_REQUEST: - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_TEST_RESPONSE, - mstp_port->SourceAddress, mstp_port->This_Station, - NULL, 0); - break; - case FRAME_TYPE_TEST_RESPONSE: - default: - break; - } - } - mstp_port->ReceivedValidFrame = false; - } - break; - /* In the USE_TOKEN state, the node is allowed to send one or */ - /* more data frames. These may be BACnet Data frames or */ - /* proprietary frames. */ - case MSTP_MASTER_STATE_USE_TOKEN: - if (!mstp_port->TxReady) { - /* NothingToSend */ - mstp_port->FrameCount = mstp_port->Nmax_info_frames; - mstp_port->master_state = MSTP_MASTER_STATE_DONE_WITH_TOKEN; - transition_now = true; - } else if (mstp_port->SilenceTimer > Tusage_delay) { - /* if we missed our timing deadline, another token will be sent */ + case MSTP_MASTER_STATE_INITIALIZE: + /* DoneInitializing */ + /* indicate that the next station is unknown */ + mstp_port->Next_Station = mstp_port->This_Station; + mstp_port->Poll_Station = mstp_port->This_Station; + /* cause a Poll For Master to be sent when this node first */ + /* receives the token */ + mstp_port->TokenCount = Npoll; + mstp_port->SoleMaster = false; mstp_port->master_state = MSTP_MASTER_STATE_IDLE; - } else { - /* don't send it if we are too late in getting out */ - uint8_t destination = mstp_port->TxBuffer[3]; - RS485_Send_Frame(mstp_port, - (uint8_t *) & mstp_port->TxBuffer[0], mstp_port->TxLength); - mstp_port->FrameCount++; - switch (mstp_port->TxFrameType) { - case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: - /* SendAndWait */ - if (destination == MSTP_BROADCAST_ADDRESS) - mstp_port->master_state = - MSTP_MASTER_STATE_DONE_WITH_TOKEN; - else - mstp_port->master_state = - MSTP_MASTER_STATE_WAIT_FOR_REPLY; - break; - case FRAME_TYPE_TEST_REQUEST: - mstp_port->master_state = MSTP_MASTER_STATE_WAIT_FOR_REPLY; - break; - case FRAME_TYPE_TEST_RESPONSE: - case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: - default: - /* SendNoWait */ - mstp_port->master_state = - MSTP_MASTER_STATE_DONE_WITH_TOKEN; - break; - } - mstp_port->TxReady = false; - } - break; - /* In the WAIT_FOR_REPLY state, the node waits for */ - /* a reply from another node. */ - case MSTP_MASTER_STATE_WAIT_FOR_REPLY: - if (mstp_port->SilenceTimer >= Treply_timeout) { - /* ReplyTimeout */ - /* assume that the request has failed */ - mstp_port->FrameCount = mstp_port->Nmax_info_frames; - mstp_port->master_state = MSTP_MASTER_STATE_DONE_WITH_TOKEN; - /* Any retry of the data frame shall await the next entry */ - /* to the USE_TOKEN state. (Because of the length of the timeout, */ - /* this transition will cause the token to be passed regardless */ - /* of the initial value of FrameCount.) */ transition_now = true; - } else { - if (mstp_port->ReceivedInvalidFrame == true) { - /* InvalidFrame */ - /* error in frame reception */ - mstp_port->ReceivedInvalidFrame = false; - mstp_port->master_state = - MSTP_MASTER_STATE_DONE_WITH_TOKEN; + break; + /* In the IDLE state, the node waits for a frame. */ + case MSTP_MASTER_STATE_IDLE: + /* LostToken */ + if (mstp_port->SilenceTimer >= Tno_token) { + /* assume that the token has been lost */ + mstp_port->EventCount = 0; /* Addendum 135-2004d-8 */ + mstp_port->master_state = MSTP_MASTER_STATE_NO_TOKEN; transition_now = true; + } + /* ReceivedInvalidFrame */ + else if (mstp_port->ReceivedInvalidFrame == true) { + /* invalid frame was received */ + mstp_port->ReceivedInvalidFrame = false; + /* wait for the next frame - remain in IDLE */ } else if (mstp_port->ReceivedValidFrame == true) { - if (mstp_port->DestinationAddress == - mstp_port->This_Station) { - switch (mstp_port->TxFrameType) { - case FRAME_TYPE_REPLY_POSTPONED: - /* ReceivedReplyPostponed */ - mstp_port->master_state = - MSTP_MASTER_STATE_DONE_WITH_TOKEN; - break; - case FRAME_TYPE_TEST_RESPONSE: - mstp_port->master_state = MSTP_MASTER_STATE_IDLE; - break; - case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: - /* ReceivedReply */ - /* or a proprietary type that indicates a reply */ - /* indicate successful reception to the higher layers */ - dlmstp_put_receive(mstp_port->SourceAddress, /* source MS/TP address */ - (uint8_t *) & mstp_port->InputBuffer[0], - mstp_port->DataLength); - mstp_port->master_state = - MSTP_MASTER_STATE_DONE_WITH_TOKEN; - break; - default: - /* if proprietary frame was expected, you might - need to transition to DONE WITH TOKEN */ - mstp_port->master_state = MSTP_MASTER_STATE_IDLE; - break; +#if PRINT_ENABLED_MASTER + fprintf(stderr, + "MSTP: ReceivedValidFrame Src=%02X Dest=%02X DataLen=%u FC=%u ST=%u Type=%s\n", + mstp_port->SourceAddress, + mstp_port->DestinationAddress, + mstp_port->DataLength, + mstp_port->FrameCount, + mstp_port->SilenceTimer, + mstptext_frame_type(mstp_port->FrameType)); +#endif + /* destined for me! */ + if ((mstp_port->DestinationAddress == + mstp_port->This_Station) || + (mstp_port->DestinationAddress == + MSTP_BROADCAST_ADDRESS)) { + switch (mstp_port->FrameType) { + /* ReceivedToken */ + case FRAME_TYPE_TOKEN: + /* tokens can't be broadcast */ + if (mstp_port->DestinationAddress == + MSTP_BROADCAST_ADDRESS) + break; + mstp_port->ReceivedValidFrame = false; + mstp_port->FrameCount = 0; + mstp_port->SoleMaster = false; + mstp_port->master_state = + MSTP_MASTER_STATE_USE_TOKEN; + transition_now = true; + break; + /* ReceivedPFM */ + case FRAME_TYPE_POLL_FOR_MASTER: + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER, + mstp_port->SourceAddress, + mstp_port->This_Station, NULL, 0); + break; + case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: + /* indicate successful reception to the higher layers */ + dlmstp_put_receive(mstp_port->SourceAddress, + (uint8_t *) & mstp_port->InputBuffer[0], + mstp_port->DataLength); + break; + case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: + /*mstp_port->ReplyPostponedTimer = 0; */ + /* indicate successful reception to the higher layers */ + dlmstp_put_receive(mstp_port->SourceAddress, + (uint8_t *) & mstp_port->InputBuffer[0], + mstp_port->DataLength); + /* broadcast DER just remains IDLE */ + if (mstp_port->DestinationAddress != + MSTP_BROADCAST_ADDRESS) { + mstp_port->master_state = + MSTP_MASTER_STATE_ANSWER_DATA_REQUEST; + } + break; + case FRAME_TYPE_TEST_REQUEST: + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_TEST_RESPONSE, + mstp_port->SourceAddress, + mstp_port->This_Station, NULL, 0); + break; + case FRAME_TYPE_TEST_RESPONSE: + default: + break; } - } else { - /* ReceivedUnexpectedFrame */ - /* an unexpected frame was received */ - /* This may indicate the presence of multiple tokens. */ - /* Synchronize with the network. */ - /* This action drops the token. */ - mstp_port->master_state = MSTP_MASTER_STATE_IDLE; } mstp_port->ReceivedValidFrame = false; - transition_now = true; } - } - break; - /* The DONE_WITH_TOKEN state either sends another data frame, */ - /* passes the token, or initiates a Poll For Master cycle. */ - case MSTP_MASTER_STATE_DONE_WITH_TOKEN: - /* SendAnotherFrame */ - if (mstp_port->FrameCount < mstp_port->Nmax_info_frames) { - /* then this node may send another information frame */ - /* before passing the token. */ - mstp_port->master_state = MSTP_MASTER_STATE_USE_TOKEN; - transition_now = true; - } - /* Npoll changed in Errata SSPC-135-2004 */ - else if (mstp_port->TokenCount < (Npoll - 1)) { - if ((mstp_port->SoleMaster == true) && - (mstp_port->Next_Station != next_this_station)) { - /* SoleMaster */ - /* there are no other known master nodes to */ - /* which the token may be sent (true master-slave operation). */ - mstp_port->FrameCount = 0; - mstp_port->TokenCount++; + break; + /* In the USE_TOKEN state, the node is allowed to send one or */ + /* more data frames. These may be BACnet Data frames or */ + /* proprietary frames. */ + case MSTP_MASTER_STATE_USE_TOKEN: + if (!mstp_port->TxReady) { + /* NothingToSend */ + mstp_port->FrameCount = mstp_port->Nmax_info_frames; + mstp_port->master_state = MSTP_MASTER_STATE_DONE_WITH_TOKEN; + transition_now = true; + } else if (mstp_port->SilenceTimer > Tusage_delay) { + /* if we missed our timing deadline, another token will be sent */ + mstp_port->master_state = MSTP_MASTER_STATE_IDLE; + } else { + /* don't send it if we are too late in getting out */ + uint8_t destination = mstp_port->TxBuffer[3]; + RS485_Send_Frame(mstp_port, + (uint8_t *) & mstp_port->TxBuffer[0], mstp_port->TxLength); + mstp_port->FrameCount++; + switch (mstp_port->TxFrameType) { + case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: + /* SendAndWait */ + if (destination == MSTP_BROADCAST_ADDRESS) + mstp_port->master_state = + MSTP_MASTER_STATE_DONE_WITH_TOKEN; + else + mstp_port->master_state = + MSTP_MASTER_STATE_WAIT_FOR_REPLY; + break; + case FRAME_TYPE_TEST_REQUEST: + mstp_port->master_state = + MSTP_MASTER_STATE_WAIT_FOR_REPLY; + break; + case FRAME_TYPE_TEST_RESPONSE: + case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: + default: + /* SendNoWait */ + mstp_port->master_state = + MSTP_MASTER_STATE_DONE_WITH_TOKEN; + break; + } + mstp_port->TxReady = false; + } + break; + /* In the WAIT_FOR_REPLY state, the node waits for */ + /* a reply from another node. */ + case MSTP_MASTER_STATE_WAIT_FOR_REPLY: + if (mstp_port->SilenceTimer >= Treply_timeout) { + /* ReplyTimeout */ + /* assume that the request has failed */ + mstp_port->FrameCount = mstp_port->Nmax_info_frames; + mstp_port->master_state = MSTP_MASTER_STATE_DONE_WITH_TOKEN; + /* Any retry of the data frame shall await the next entry */ + /* to the USE_TOKEN state. (Because of the length of the timeout, */ + /* this transition will cause the token to be passed regardless */ + /* of the initial value of FrameCount.) */ + transition_now = true; + } else { + if (mstp_port->ReceivedInvalidFrame == true) { + /* InvalidFrame */ + /* error in frame reception */ + mstp_port->ReceivedInvalidFrame = false; + mstp_port->master_state = + MSTP_MASTER_STATE_DONE_WITH_TOKEN; + transition_now = true; + } else if (mstp_port->ReceivedValidFrame == true) { + if (mstp_port->DestinationAddress == + mstp_port->This_Station) { + switch (mstp_port->TxFrameType) { + case FRAME_TYPE_REPLY_POSTPONED: + /* ReceivedReplyPostponed */ + mstp_port->master_state = + MSTP_MASTER_STATE_DONE_WITH_TOKEN; + break; + case FRAME_TYPE_TEST_RESPONSE: + mstp_port->master_state = + MSTP_MASTER_STATE_IDLE; + break; + case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: + /* ReceivedReply */ + /* or a proprietary type that indicates a reply */ + /* indicate successful reception to the higher layers */ + dlmstp_put_receive(mstp_port->SourceAddress, /* source MS/TP address */ + (uint8_t *) & mstp_port->InputBuffer[0], + mstp_port->DataLength); + mstp_port->master_state = + MSTP_MASTER_STATE_DONE_WITH_TOKEN; + break; + default: + /* if proprietary frame was expected, you might + need to transition to DONE WITH TOKEN */ + mstp_port->master_state = + MSTP_MASTER_STATE_IDLE; + break; + } + } else { + /* ReceivedUnexpectedFrame */ + /* an unexpected frame was received */ + /* This may indicate the presence of multiple tokens. */ + /* Synchronize with the network. */ + /* This action drops the token. */ + mstp_port->master_state = MSTP_MASTER_STATE_IDLE; + } + mstp_port->ReceivedValidFrame = false; + transition_now = true; + } + } + break; + /* The DONE_WITH_TOKEN state either sends another data frame, */ + /* passes the token, or initiates a Poll For Master cycle. */ + case MSTP_MASTER_STATE_DONE_WITH_TOKEN: + /* SendAnotherFrame */ + if (mstp_port->FrameCount < mstp_port->Nmax_info_frames) { + /* then this node may send another information frame */ + /* before passing the token. */ mstp_port->master_state = MSTP_MASTER_STATE_USE_TOKEN; transition_now = true; - } else { - /* SendToken */ - /* Npoll changed in Errata SSPC-135-2004 */ - /* The comparison of NS and TS+1 eliminates the Poll For Master */ - /* if there are no addresses between TS and NS, since there is no */ - /* address at which a new master node may be found in that case. */ - mstp_port->TokenCount++; - /* transmit a Token frame to NS */ - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_TOKEN, - mstp_port->Next_Station, - mstp_port->This_Station, NULL, 0); - mstp_port->RetryCount = 0; - mstp_port->EventCount = 0; - mstp_port->master_state = MSTP_MASTER_STATE_PASS_TOKEN; } - } else if (next_poll_station == mstp_port->Next_Station) { - if (mstp_port->SoleMaster == true) { - /* SoleMasterRestartMaintenancePFM */ - mstp_port->Poll_Station = next_next_station; - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_POLL_FOR_MASTER, mstp_port->Poll_Station, - mstp_port->This_Station, NULL, 0); - /* no known successor node */ - mstp_port->Next_Station = mstp_port->This_Station; - mstp_port->RetryCount = 0; - mstp_port->TokenCount = 1; /* changed in Errata SSPC-135-2004 */ - /* mstp_port->EventCount = 0; removed in Addendum 135-2004d-8 */ - /* find a new successor to TS */ - mstp_port->master_state = - MSTP_MASTER_STATE_POLL_FOR_MASTER; - } else { - /* ResetMaintenancePFM */ - mstp_port->Poll_Station = mstp_port->This_Station; - /* transmit a Token frame to NS */ - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_TOKEN, - mstp_port->Next_Station, - mstp_port->This_Station, NULL, 0); - mstp_port->RetryCount = 0; - mstp_port->TokenCount = 1; /* changed in Errata SSPC-135-2004 */ - mstp_port->EventCount = 0; - mstp_port->master_state = MSTP_MASTER_STATE_PASS_TOKEN; - } - } else { - /* SendMaintenancePFM */ - mstp_port->Poll_Station = next_poll_station; - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_POLL_FOR_MASTER, - mstp_port->Poll_Station, mstp_port->This_Station, NULL, 0); - mstp_port->RetryCount = 0; - mstp_port->master_state = MSTP_MASTER_STATE_POLL_FOR_MASTER; - } - break; - /* The PASS_TOKEN state listens for a successor to begin using */ - /* the token that this node has just attempted to pass. */ - case MSTP_MASTER_STATE_PASS_TOKEN: - if (mstp_port->SilenceTimer < Tusage_timeout) { - if (mstp_port->EventCount > Nmin_octets) { - /* SawTokenUser */ - /* Assume that a frame has been sent by the new token user. */ - /* Enter the IDLE state to process the frame. */ - mstp_port->master_state = MSTP_MASTER_STATE_IDLE; - transition_now = true; - } - } else { - if (mstp_port->RetryCount < Nretry_token) { - /* RetrySendToken */ - mstp_port->RetryCount++; - /* Transmit a Token frame to NS */ - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_TOKEN, - mstp_port->Next_Station, mstp_port->This_Station, NULL, - 0); - mstp_port->EventCount = 0; - /* re-enter the current state to listen for NS */ - /* to begin using the token. */ - } else { - /* FindNewSuccessor */ - /* Assume that NS has failed. */ - mstp_port->Poll_Station = next_next_station; - /* Transmit a Poll For Master frame to PS. */ - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_POLL_FOR_MASTER, - mstp_port->Poll_Station, mstp_port->This_Station, NULL, - 0); - /* no known successor node */ - mstp_port->Next_Station = mstp_port->This_Station; - mstp_port->RetryCount = 0; - mstp_port->TokenCount = 0; - /* mstp_port->EventCount = 0; removed in Addendum 135-2004d-8 */ - /* find a new successor to TS */ - mstp_port->master_state = - MSTP_MASTER_STATE_POLL_FOR_MASTER; - } - } - break; - /* The NO_TOKEN state is entered if mstp_port->SilenceTimer becomes greater */ - /* than Tno_token, indicating that there has been no network activity */ - /* for that period of time. The timeout is continued to determine */ - /* whether or not this node may create a token. */ - case MSTP_MASTER_STATE_NO_TOKEN: - my_timeout = Tno_token + (Tslot * mstp_port->This_Station); - if (mstp_port->SilenceTimer < my_timeout) { - if (mstp_port->EventCount > Nmin_octets) { - /* SawFrame */ - /* Some other node exists at a lower address. */ - /* Enter the IDLE state to receive and process the incoming frame. */ - mstp_port->master_state = MSTP_MASTER_STATE_IDLE; - transition_now = true; - } - } else { - ns_timeout = - Tno_token + (Tslot * (mstp_port->This_Station + 1)); - if (mstp_port->SilenceTimer < ns_timeout) { - /* GenerateToken */ - /* Assume that this node is the lowest numerical address */ - /* on the network and is empowered to create a token. */ - mstp_port->Poll_Station = next_this_station; - /* Transmit a Poll For Master frame to PS. */ - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_POLL_FOR_MASTER, - mstp_port->Poll_Station, mstp_port->This_Station, NULL, - 0); - /* indicate that the next station is unknown */ - mstp_port->Next_Station = mstp_port->This_Station; - mstp_port->RetryCount = 0; - mstp_port->TokenCount = 0; - /* mstp_port->EventCount = 0; removed Addendum 135-2004d-8 */ - /* enter the POLL_FOR_MASTER state to find a new successor to TS. */ - mstp_port->master_state = - MSTP_MASTER_STATE_POLL_FOR_MASTER; - } - } - break; - /* In the POLL_FOR_MASTER state, the node listens for a reply to */ - /* a previously sent Poll For Master frame in order to find */ - /* a successor node. */ - case MSTP_MASTER_STATE_POLL_FOR_MASTER: - if (mstp_port->ReceivedValidFrame == true) { - if ((mstp_port->DestinationAddress == mstp_port->This_Station) - && (mstp_port->FrameType == - FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER)) { - /* ReceivedReplyToPFM */ - mstp_port->SoleMaster = false; - mstp_port->Next_Station = mstp_port->SourceAddress; - mstp_port->EventCount = 0; - /* Transmit a Token frame to NS */ - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_TOKEN, - mstp_port->Next_Station, mstp_port->This_Station, NULL, - 0); - mstp_port->Poll_Station = mstp_port->This_Station; - mstp_port->TokenCount = 0; - mstp_port->RetryCount = 0; - mstp_port->master_state = MSTP_MASTER_STATE_PASS_TOKEN; - } else { - /* ReceivedUnexpectedFrame */ - /* An unexpected frame was received. */ - /* This may indicate the presence of multiple tokens. */ - /* enter the IDLE state to synchronize with the network. */ - /* This action drops the token. */ - mstp_port->master_state = MSTP_MASTER_STATE_IDLE; - transition_now = true; - } - mstp_port->ReceivedValidFrame = false; - } else if ((mstp_port->SilenceTimer >= Tusage_timeout) || - (mstp_port->ReceivedInvalidFrame == true)) { - if (mstp_port->SoleMaster == true) { - /* SoleMaster */ - /* There was no valid reply to the periodic poll */ - /* by the sole known master for other masters. */ - mstp_port->FrameCount = 0; - /* mstp_port->TokenCount++; removed in 2004 */ - mstp_port->master_state = MSTP_MASTER_STATE_USE_TOKEN; - transition_now = true; - } else { - if (mstp_port->Next_Station != mstp_port->This_Station) { - /* DoneWithPFM */ - /* There was no valid reply to the maintenance */ - /* poll for a master at address PS. */ - mstp_port->EventCount = 0; + /* Npoll changed in Errata SSPC-135-2004 */ + else if (mstp_port->TokenCount < (Npoll - 1)) { + if ((mstp_port->SoleMaster == true) && + (mstp_port->Next_Station != next_this_station)) { + /* SoleMaster */ + /* there are no other known master nodes to */ + /* which the token may be sent (true master-slave operation). */ + mstp_port->FrameCount = 0; + mstp_port->TokenCount++; + mstp_port->master_state = MSTP_MASTER_STATE_USE_TOKEN; + transition_now = true; + } else { + /* SendToken */ + /* Npoll changed in Errata SSPC-135-2004 */ + /* The comparison of NS and TS+1 eliminates the Poll For Master */ + /* if there are no addresses between TS and NS, since there is no */ + /* address at which a new master node may be found in that case. */ + mstp_port->TokenCount++; /* transmit a Token frame to NS */ MSTP_Create_And_Send_Frame(mstp_port, FRAME_TYPE_TOKEN, - mstp_port->Next_Station, mstp_port->This_Station, - NULL, 0); + mstp_port->Next_Station, + mstp_port->This_Station, NULL, 0); + mstp_port->RetryCount = 0; + mstp_port->EventCount = 0; + mstp_port->master_state = MSTP_MASTER_STATE_PASS_TOKEN; + } + } else if (next_poll_station == mstp_port->Next_Station) { + if (mstp_port->SoleMaster == true) { + /* SoleMasterRestartMaintenancePFM */ + mstp_port->Poll_Station = next_next_station; + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_POLL_FOR_MASTER, mstp_port->Poll_Station, + mstp_port->This_Station, NULL, 0); + /* no known successor node */ + mstp_port->Next_Station = mstp_port->This_Station; + mstp_port->RetryCount = 0; + mstp_port->TokenCount = 1; /* changed in Errata SSPC-135-2004 */ + /* mstp_port->EventCount = 0; removed in Addendum 135-2004d-8 */ + /* find a new successor to TS */ + mstp_port->master_state = + MSTP_MASTER_STATE_POLL_FOR_MASTER; + } else { + /* ResetMaintenancePFM */ + mstp_port->Poll_Station = mstp_port->This_Station; + /* transmit a Token frame to NS */ + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_TOKEN, + mstp_port->Next_Station, + mstp_port->This_Station, NULL, 0); + mstp_port->RetryCount = 0; + mstp_port->TokenCount = 1; /* changed in Errata SSPC-135-2004 */ + mstp_port->EventCount = 0; + mstp_port->master_state = MSTP_MASTER_STATE_PASS_TOKEN; + } + } else { + /* SendMaintenancePFM */ + mstp_port->Poll_Station = next_poll_station; + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_POLL_FOR_MASTER, + mstp_port->Poll_Station, mstp_port->This_Station, NULL, 0); + mstp_port->RetryCount = 0; + mstp_port->master_state = MSTP_MASTER_STATE_POLL_FOR_MASTER; + } + break; + /* The PASS_TOKEN state listens for a successor to begin using */ + /* the token that this node has just attempted to pass. */ + case MSTP_MASTER_STATE_PASS_TOKEN: + if (mstp_port->SilenceTimer < Tusage_timeout) { + if (mstp_port->EventCount > Nmin_octets) { + /* SawTokenUser */ + /* Assume that a frame has been sent by the new token user. */ + /* Enter the IDLE state to process the frame. */ + mstp_port->master_state = MSTP_MASTER_STATE_IDLE; + transition_now = true; + } + } else { + if (mstp_port->RetryCount < Nretry_token) { + /* RetrySendToken */ + mstp_port->RetryCount++; + /* Transmit a Token frame to NS */ + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_TOKEN, + mstp_port->Next_Station, mstp_port->This_Station, NULL, + 0); + mstp_port->EventCount = 0; + /* re-enter the current state to listen for NS */ + /* to begin using the token. */ + } else { + /* FindNewSuccessor */ + /* Assume that NS has failed. */ + mstp_port->Poll_Station = next_next_station; + /* Transmit a Poll For Master frame to PS. */ + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_POLL_FOR_MASTER, + mstp_port->Poll_Station, mstp_port->This_Station, NULL, + 0); + /* no known successor node */ + mstp_port->Next_Station = mstp_port->This_Station; + mstp_port->RetryCount = 0; + mstp_port->TokenCount = 0; + /* mstp_port->EventCount = 0; removed in Addendum 135-2004d-8 */ + /* find a new successor to TS */ + mstp_port->master_state = + MSTP_MASTER_STATE_POLL_FOR_MASTER; + } + } + break; + /* The NO_TOKEN state is entered if mstp_port->SilenceTimer becomes greater */ + /* than Tno_token, indicating that there has been no network activity */ + /* for that period of time. The timeout is continued to determine */ + /* whether or not this node may create a token. */ + case MSTP_MASTER_STATE_NO_TOKEN: + my_timeout = Tno_token + (Tslot * mstp_port->This_Station); + if (mstp_port->SilenceTimer < my_timeout) { + if (mstp_port->EventCount > Nmin_octets) { + /* SawFrame */ + /* Some other node exists at a lower address. */ + /* Enter the IDLE state to receive and process the incoming frame. */ + mstp_port->master_state = MSTP_MASTER_STATE_IDLE; + transition_now = true; + } + } else { + ns_timeout = + Tno_token + (Tslot * (mstp_port->This_Station + 1)); + if (mstp_port->SilenceTimer < ns_timeout) { + /* GenerateToken */ + /* Assume that this node is the lowest numerical address */ + /* on the network and is empowered to create a token. */ + mstp_port->Poll_Station = next_this_station; + /* Transmit a Poll For Master frame to PS. */ + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_POLL_FOR_MASTER, + mstp_port->Poll_Station, mstp_port->This_Station, NULL, + 0); + /* indicate that the next station is unknown */ + mstp_port->Next_Station = mstp_port->This_Station; + mstp_port->RetryCount = 0; + mstp_port->TokenCount = 0; + /* mstp_port->EventCount = 0; removed Addendum 135-2004d-8 */ + /* enter the POLL_FOR_MASTER state to find a new successor to TS. */ + mstp_port->master_state = + MSTP_MASTER_STATE_POLL_FOR_MASTER; + } + } + break; + /* In the POLL_FOR_MASTER state, the node listens for a reply to */ + /* a previously sent Poll For Master frame in order to find */ + /* a successor node. */ + case MSTP_MASTER_STATE_POLL_FOR_MASTER: + if (mstp_port->ReceivedValidFrame == true) { + if ((mstp_port->DestinationAddress == mstp_port->This_Station) + && (mstp_port->FrameType == + FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER)) { + /* ReceivedReplyToPFM */ + mstp_port->SoleMaster = false; + mstp_port->Next_Station = mstp_port->SourceAddress; + mstp_port->EventCount = 0; + /* Transmit a Token frame to NS */ + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_TOKEN, + mstp_port->Next_Station, mstp_port->This_Station, NULL, + 0); + mstp_port->Poll_Station = mstp_port->This_Station; + mstp_port->TokenCount = 0; mstp_port->RetryCount = 0; mstp_port->master_state = MSTP_MASTER_STATE_PASS_TOKEN; } else { - if (next_poll_station != mstp_port->This_Station) { - /* SendNextPFM */ - mstp_port->Poll_Station = next_poll_station; - /* Transmit a Poll For Master frame to PS. */ + /* ReceivedUnexpectedFrame */ + /* An unexpected frame was received. */ + /* This may indicate the presence of multiple tokens. */ + /* enter the IDLE state to synchronize with the network. */ + /* This action drops the token. */ + mstp_port->master_state = MSTP_MASTER_STATE_IDLE; + transition_now = true; + } + mstp_port->ReceivedValidFrame = false; + } else if ((mstp_port->SilenceTimer >= Tusage_timeout) || + (mstp_port->ReceivedInvalidFrame == true)) { + if (mstp_port->SoleMaster == true) { + /* SoleMaster */ + /* There was no valid reply to the periodic poll */ + /* by the sole known master for other masters. */ + mstp_port->FrameCount = 0; + /* mstp_port->TokenCount++; removed in 2004 */ + mstp_port->master_state = MSTP_MASTER_STATE_USE_TOKEN; + transition_now = true; + } else { + if (mstp_port->Next_Station != mstp_port->This_Station) { + /* DoneWithPFM */ + /* There was no valid reply to the maintenance */ + /* poll for a master at address PS. */ + mstp_port->EventCount = 0; + /* transmit a Token frame to NS */ MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_POLL_FOR_MASTER, - mstp_port->Poll_Station, - mstp_port->This_Station, NULL, 0); + FRAME_TYPE_TOKEN, + mstp_port->Next_Station, mstp_port->This_Station, + NULL, 0); mstp_port->RetryCount = 0; - /* Re-enter the current state. */ + mstp_port->master_state = MSTP_MASTER_STATE_PASS_TOKEN; } else { - /* DeclareSoleMaster */ - /* to indicate that this station is the only master */ - mstp_port->SoleMaster = true; - mstp_port->FrameCount = 0; - mstp_port->master_state = - MSTP_MASTER_STATE_USE_TOKEN; - transition_now = true; + if (next_poll_station != mstp_port->This_Station) { + /* SendNextPFM */ + mstp_port->Poll_Station = next_poll_station; + /* Transmit a Poll For Master frame to PS. */ + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_POLL_FOR_MASTER, + mstp_port->Poll_Station, + mstp_port->This_Station, NULL, 0); + mstp_port->RetryCount = 0; + /* Re-enter the current state. */ + } else { + /* DeclareSoleMaster */ + /* to indicate that this station is the only master */ + mstp_port->SoleMaster = true; + mstp_port->FrameCount = 0; + mstp_port->master_state = + MSTP_MASTER_STATE_USE_TOKEN; + transition_now = true; + } } } + mstp_port->ReceivedInvalidFrame = false; } - mstp_port->ReceivedInvalidFrame = false; - } - break; - /* The ANSWER_DATA_REQUEST state is entered when a */ - /* BACnet Data Expecting Reply, a Test_Request, or */ - /* a proprietary frame that expects a reply is received. */ - case MSTP_MASTER_STATE_ANSWER_DATA_REQUEST: + break; + /* The ANSWER_DATA_REQUEST state is entered when a */ + /* BACnet Data Expecting Reply, a Test_Request, or */ + /* a proprietary frame that expects a reply is received. */ + case MSTP_MASTER_STATE_ANSWER_DATA_REQUEST: #if 0 - /* FIXME: we always defer the reply to be safe */ - /* FIXME: if we knew the APDU type received, we could - see if the next message was that same APDU type - along with the matching src/dest and invoke ID */ - if ((mstp_port->SilenceTimer <= Treply_delay) && - mstp_port->TxReady) { - /* Reply */ - /* If a reply is available from the higher layers */ - /* within Treply_delay after the reception of the */ - /* final octet of the requesting frame */ - /* (the mechanism used to determine this is a local matter), */ - /* then call MSTP_Create_And_Send_Frame to transmit the reply frame */ - /* and enter the IDLE state to wait for the next frame. */ - if ((mstp_port->FrameType == - FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY) - && (mstp_port->TxReady)) { - RS485_Send_Frame(mstp_port, - (uint8_t *) & mstp_port->TxBuffer[0], - mstp_port->TxLength); - mstp_port->TxReady = false; + /* FIXME: we always defer the reply to be safe */ + /* FIXME: if we knew the APDU type received, we could + see if the next message was that same APDU type + along with the matching src/dest and invoke ID */ + if ((mstp_port->SilenceTimer <= Treply_delay) && + mstp_port->TxReady) { + /* Reply */ + /* If a reply is available from the higher layers */ + /* within Treply_delay after the reception of the */ + /* final octet of the requesting frame */ + /* (the mechanism used to determine this is a local matter), */ + /* then call MSTP_Create_And_Send_Frame to transmit the reply frame */ + /* and enter the IDLE state to wait for the next frame. */ + if ((mstp_port->FrameType == + FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY) + && (mstp_port->TxReady)) { + RS485_Send_Frame(mstp_port, + (uint8_t *) & mstp_port->TxBuffer[0], + mstp_port->TxLength); + mstp_port->TxReady = false; + mstp_port->master_state = MSTP_MASTER_STATE_IDLE; + } + } else +#endif + /* DeferredReply */ + /* If no reply will be available from the higher layers */ + /* within Treply_delay after the reception of the */ + /* final octet of the requesting frame (the mechanism */ + /* used to determine this is a local matter), */ + /* then an immediate reply is not possible. */ + /* Any reply shall wait until this node receives the token. */ + /* Call MSTP_Create_And_Send_Frame to transmit a Reply Postponed frame, */ + /* and enter the IDLE state. */ + { + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_REPLY_POSTPONED, + mstp_port->SourceAddress, + mstp_port->This_Station, NULL, 0); mstp_port->master_state = MSTP_MASTER_STATE_IDLE; } - } else -#endif - /* DeferredReply */ - /* If no reply will be available from the higher layers */ - /* within Treply_delay after the reception of the */ - /* final octet of the requesting frame (the mechanism */ - /* used to determine this is a local matter), */ - /* then an immediate reply is not possible. */ - /* Any reply shall wait until this node receives the token. */ - /* Call MSTP_Create_And_Send_Frame to transmit a Reply Postponed frame, */ - /* and enter the IDLE state. */ - { - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_REPLY_POSTPONED, - mstp_port->SourceAddress, - mstp_port->This_Station, NULL, 0); + break; + default: mstp_port->master_state = MSTP_MASTER_STATE_IDLE; - } - break; - default: - mstp_port->master_state = MSTP_MASTER_STATE_IDLE; - break; + break; } return transition_now; @@ -1082,9 +1090,10 @@ bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t * mstp_port) /* note: This_Station should be set with the MAC address */ /* note: Nmax_info_frames should be set */ /* note: Nmax_master should be set */ -void MSTP_Init(volatile struct mstp_port_struct_t *mstp_port) +void MSTP_Init( + volatile struct mstp_port_struct_t *mstp_port) { - int i; /*loop counter */ + int i; /*loop counter */ if (mstp_port) { mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; diff --git a/bacnet-stack/ports/pic18f6720/mstp.h b/bacnet-stack/ports/pic18f6720/mstp.h index 7ff3e79e..fac03588 100644 --- a/bacnet-stack/ports/pic18f6720/mstp.h +++ b/bacnet-stack/ports/pic18f6720/mstp.h @@ -199,7 +199,7 @@ struct mstp_port_struct_t { /* This array is only used for APDU messages */ uint8_t TxBuffer[MAX_MPDU]; unsigned TxLength; - bool TxReady; /* true if ready to be sent or received */ + bool TxReady; /* true if ready to be sent or received */ uint8_t TxFrameType; /* type of message - needed by MS/TP */ }; @@ -220,27 +220,32 @@ struct mstp_port_struct_t { #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - void MSTP_Init(volatile struct mstp_port_struct_t *mstp_port); - void MSTP_Receive_Frame_FSM(volatile struct mstp_port_struct_t + void MSTP_Init( + volatile struct mstp_port_struct_t *mstp_port); + void MSTP_Receive_Frame_FSM( + volatile struct mstp_port_struct_t *mstp_port); - bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t + bool MSTP_Master_Node_FSM( + volatile struct mstp_port_struct_t *mstp_port); /* returns true if line is active */ - bool MSTP_Line_Active(volatile struct mstp_port_struct_t *mstp_port); + bool MSTP_Line_Active( + volatile struct mstp_port_struct_t *mstp_port); - unsigned MSTP_Create_Frame(uint8_t * buffer, /* where frame is loaded */ + unsigned MSTP_Create_Frame( + uint8_t * buffer, /* where frame is loaded */ unsigned buffer_len, /* amount of space available */ uint8_t frame_type, /* type of frame to send - see defines */ uint8_t destination, /* destination address */ - uint8_t source, /* source address */ - uint8_t * data, /* any data to be sent - may be null */ + uint8_t source, /* source address */ + uint8_t * data, /* any data to be sent - may be null */ unsigned data_len); /* number of bytes of data (up to 501) */ #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/ports/pic18f6720/rs485.c b/bacnet-stack/ports/pic18f6720/rs485.c index 074c546f..d70c484f 100644 --- a/bacnet-stack/ports/pic18f6720/rs485.c +++ b/bacnet-stack/ports/pic18f6720/rs485.c @@ -57,11 +57,12 @@ volatile uint8_t RS485_Tx_Buffer[MAX_MPDU]; * ALGORITHM: none * NOTES: none *****************************************************************************/ -void RS485_Send_Frame(volatile struct mstp_port_struct_t *mstp_port, /* port specific data */ - uint8_t * buffer, /* frame to send (up to 501 bytes of data) */ +void RS485_Send_Frame( + volatile struct mstp_port_struct_t *mstp_port, /* port specific data */ + uint8_t * buffer, /* frame to send (up to 501 bytes of data) */ uint16_t nbytes) -{ /* number of bytes of data (up to 501) */ - uint16_t i = 0; /* loop counter */ +{ /* number of bytes of data (up to 501) */ + uint16_t i = 0; /* loop counter */ uint8_t turnaround_time; if (!buffer) @@ -117,8 +118,8 @@ void RS485_Send_Frame(volatile struct mstp_port_struct_t *mstp_port, /* port * ALGORITHM: none * NOTES: none *****************************************************************************/ -uint8_t RS485_Check_UART_Data(volatile struct mstp_port_struct_t * - mstp_port) +uint8_t RS485_Check_UART_Data( + volatile struct mstp_port_struct_t * mstp_port) { /* check for data */ if (RS485_Comstat.Rx_Bytes) { @@ -151,7 +152,8 @@ uint8_t RS485_Check_UART_Data(volatile struct mstp_port_struct_t * NOTES: none *************************************************************************** */ -void RS485_Interrupt_Rx(void) +void RS485_Interrupt_Rx( + void) { char dummy; @@ -184,7 +186,8 @@ void RS485_Interrupt_Rx(void) NOTES: none *************************************************************************** */ -void RS485_Interrupt_Tx(void) +void RS485_Interrupt_Tx( + void) { if (RS485_Comstat.Tx_Bytes) { /* Get the data byte */ @@ -214,7 +217,8 @@ void RS485_Interrupt_Tx(void) * ALGORITHM: none * NOTES: none *****************************************************************************/ -uint32_t RS485_Get_Baud_Rate(void) +uint32_t RS485_Get_Baud_Rate( + void) { return RS485_Baud_Rate; } @@ -225,22 +229,23 @@ uint32_t RS485_Get_Baud_Rate(void) * ALGORITHM: none * NOTES: none *****************************************************************************/ -bool RS485_Set_Baud_Rate(uint32_t baud) +bool RS485_Set_Baud_Rate( + uint32_t baud) { bool valid = true; switch (baud) { - case 9600: - case 19200: - case 38400: - case 57600: - case 76800: - case 115200: - RS485_Baud_Rate = baud; - break; - default: - valid = false; - break; + case 9600: + case 19200: + case 38400: + case 57600: + case 76800: + case 115200: + RS485_Baud_Rate = baud; + break; + default: + valid = false; + break; } if (valid) { @@ -262,7 +267,8 @@ bool RS485_Set_Baud_Rate(uint32_t baud) * ALGORITHM: none * NOTES: none *****************************************************************************/ -void RS485_Initialize_Port(void) +void RS485_Initialize_Port( + void) { /* Reset USART registers to POR state */ @@ -292,35 +298,35 @@ void RS485_Initialize_Port(void) 1250000 0 */ switch (RS485_Baud_Rate) { - case 19200: - SPBRG2 = 64; - TXSTA2bits.BRGH = 1; - break; - case 38400: - SPBRG2 = 32; - TXSTA2bits.BRGH = 1; - break; - case 57600: - SPBRG2 = 21; - TXSTA2bits.BRGH = 1; - break; - case 76800: - SPBRG2 = 3; - TXSTA2bits.BRGH = 0; - break; - case 115200: - SPBRG2 = 10; - TXSTA2bits.BRGH = 1; - break; - case 9600: - SPBRG2 = 129; - TXSTA2bits.BRGH = 1; - break; - default: - SPBRG2 = 129; - TXSTA2bits.BRGH = 1; - RS485_Set_Baud_Rate(9600); - break; + case 19200: + SPBRG2 = 64; + TXSTA2bits.BRGH = 1; + break; + case 38400: + SPBRG2 = 32; + TXSTA2bits.BRGH = 1; + break; + case 57600: + SPBRG2 = 21; + TXSTA2bits.BRGH = 1; + break; + case 76800: + SPBRG2 = 3; + TXSTA2bits.BRGH = 0; + break; + case 115200: + SPBRG2 = 10; + TXSTA2bits.BRGH = 1; + break; + case 9600: + SPBRG2 = 129; + TXSTA2bits.BRGH = 1; + break; + default: + SPBRG2 = 129; + TXSTA2bits.BRGH = 1; + RS485_Set_Baud_Rate(9600); + break; } /* select async mode */ TXSTA2bits.SYNC = 0; @@ -341,14 +347,16 @@ void RS485_Initialize_Port(void) * ALGORITHM: none * NOTES: none *****************************************************************************/ -void RS485_Disable_Port(void) +void RS485_Disable_Port( + void) { - RCSTA2 &= 0x4F; /* Disable the receiver */ + RCSTA2 &= 0x4F; /* Disable the receiver */ TXSTA2bits.TXEN = 0; /* and transmitter */ - PIE3 &= 0xCF; /* Disable both interrupts */ + PIE3 &= 0xCF; /* Disable both interrupts */ } -void RS485_Reinit(void) +void RS485_Reinit( + void) { RS485_Set_Baud_Rate(38400); } @@ -359,7 +367,8 @@ void RS485_Reinit(void) * ALGORITHM: none * NOTES: none *****************************************************************************/ -void RS485_Initialize(void) +void RS485_Initialize( + void) { /* Init the Rs485 buffers */ RS485_Comstat.RxHead = 0; diff --git a/bacnet-stack/ports/pic18f6720/rs485.h b/bacnet-stack/ports/pic18f6720/rs485.h index b4dc293c..f5444233 100644 --- a/bacnet-stack/ports/pic18f6720/rs485.h +++ b/bacnet-stack/ports/pic18f6720/rs485.h @@ -58,27 +58,36 @@ extern uint32_t RS485_Baud_Rate; #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - void RS485_Reinit(void); - void RS485_Initialize(void); + void RS485_Reinit( + void); + void RS485_Initialize( + void); - void RS485_Disable(void); + void RS485_Disable( + void); - void RS485_Send_Frame(volatile struct mstp_port_struct_t *mstp_port, /* port specific data */ + void RS485_Send_Frame( + volatile struct mstp_port_struct_t *mstp_port, /* port specific data */ uint8_t * buffer, /* frame to send (up to 501 bytes of data) */ uint16_t nbytes); /* number of bytes of data (up to 501) */ - uint8_t RS485_Check_UART_Data(volatile struct mstp_port_struct_t *mstp_port); /* port specific data */ + uint8_t RS485_Check_UART_Data( + volatile struct mstp_port_struct_t *mstp_port); /* port specific data */ - void RS485_Interrupt_Rx(void); + void RS485_Interrupt_Rx( + void); - void RS485_Interrupt_Tx(void); + void RS485_Interrupt_Tx( + void); - uint32_t RS485_Get_Baud_Rate(void); - bool RS485_Set_Baud_Rate(uint32_t baud); + uint32_t RS485_Get_Baud_Rate( + void); + bool RS485_Set_Baud_Rate( + uint32_t baud); #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/ports/pic18f6720/stdint.h b/bacnet-stack/ports/pic18f6720/stdint.h index e9629b20..c6426c17 100644 --- a/bacnet-stack/ports/pic18f6720/stdint.h +++ b/bacnet-stack/ports/pic18f6720/stdint.h @@ -15,4 +15,4 @@ typedef signed long int32_t; /* 4 bytes -2147483647 to 2147483647 */ /* typedef signed long long int64_t; */ /* typedef unsigned long long uint64_t; */ -#endif /* STDINT_H */ +#endif /* STDINT_H */ diff --git a/bacnet-stack/ports/rtos32/bip-init.c b/bacnet-stack/ports/rtos32/bip-init.c index ed56face..0bdec1c5 100644 --- a/bacnet-stack/ports/rtos32/bip-init.c +++ b/bacnet-stack/ports/rtos32/bip-init.c @@ -32,15 +32,16 @@ ------------------------------------------- ####COPYRIGHTEND####*/ -#include /* for standard integer types uint8_t etc. */ -#include /* for the standard bool type. */ +#include /* for standard integer types uint8_t etc. */ +#include /* for the standard bool type. */ #include "bacdcode.h" #include "bip.h" static int interface = SOCKET_ERROR; /* SOCKET_ERROR means no open interface */ /*-----------------------------------*/ -static void Error(const char *Msg) +static void Error( + const char *Msg) { int Code = WSAGetLastError(); #ifdef HOST @@ -53,7 +54,8 @@ static void Error(const char *Msg) #ifndef HOST /*-----------------------------------*/ -void InterfaceCleanup(void) +void InterfaceCleanup( + void) { if (interface != SOCKET_ERROR) { xn_interface_close(interface); @@ -64,7 +66,8 @@ void InterfaceCleanup(void) } } -static void NetInitialize(void) +static void NetInitialize( + void) /* initialize the TCP/IP stack */ { int Result; @@ -78,7 +81,7 @@ static void NetInitialize(void) RTURegisterCallback(USBAX772); RTURegisterCallback(USBKeyboard); /* support USB keyboards */ FindUSBControllers(); /* install USB host controllers */ - Sleep(2000); /* give the USB stack time to enumerate devices */ + Sleep(2000); /* give the USB stack time to enumerate devices */ #endif #ifdef DHCP @@ -129,16 +132,16 @@ static void NetInitialize(void) #if DEVICE_ID == PRISM_PCMCIA_DEVICE || DEVICE_ID == PRISM_DEVICE xn_wlan_setup(interface, /* iface_no: value returned by xn_interface_open_config() */ - "network name", /* SSID : network name set in the access point */ - "station name", /* Name : name of this node */ - 0, /* Channel : 0 for access points, 1..14 for ad-hoc */ - 0, /* KeyIndex: 0 .. 3 */ - "12345", /* WEP Key : key to use (5 or 13 bytes) */ - 0); /* Flags : see manual and Wlanapi.h for details */ - Sleep(1000); /* wireless devices need a little time before they can be used */ -#endif /* WLAN device */ + "network name", /* SSID : network name set in the access point */ + "station name", /* Name : name of this node */ + 0, /* Channel : 0 for access points, 1..14 for ad-hoc */ + 0, /* KeyIndex: 0 .. 3 */ + "12345", /* WEP Key : key to use (5 or 13 bytes) */ + 0); /* Flags : see manual and Wlanapi.h for details */ + Sleep(1000); /* wireless devices need a little time before they can be used */ +#endif /* WLAN device */ -#if defined(AUTO_IP) /* use xn_autoip() to get an IP address */ +#if defined(AUTO_IP) /* use xn_autoip() to get an IP address */ Result = xn_autoip(interface, MinIP, MaxIP, NetMask, TargetIP); if (Result == SOCKET_ERROR) Error("xn_autoip failed"); @@ -146,11 +149,10 @@ static void NetInitialize(void) printf("Auto-assigned IP address %i.%i.%i.%i\n", TargetIP[0], TargetIP[1], TargetIP[2], TargetIP[3]); /* define default gateway and DNS server */ - xn_rt_add(RT_DEFAULT, ip_ffaddr, DefaultGateway, 1, interface, - RT_INF); + xn_rt_add(RT_DEFAULT, ip_ffaddr, DefaultGateway, 1, interface, RT_INF); xn_set_server_list((DWORD *) DNSServer, 1); } -#elif defined(DHCP) /* use DHCP */ +#elif defined(DHCP) /* use DHCP */ { DHCP_param param[] = { {SUBNET_MASK, 1} , {DNS_OP, 1} @@ -180,7 +182,7 @@ static void NetInitialize(void) xn_set_server_list((DWORD *) DNSServer, 1); #endif -#else /* HOST defined, run on Windows */ +#else /* HOST defined, run on Windows */ WSADATA wd; Result = WSAStartup(0x0101, &wd); @@ -198,7 +200,8 @@ static void NetInitialize(void) * ALGORITHM: none * NOTES: none ******************************************************************/ -static void RTIP_To_Network_Address(BYTE * octet_address, +static void RTIP_To_Network_Address( + BYTE * octet_address, struct in_addr *addr) { uint32_t ip_address = 0; /* for decoding the subnet mask */ @@ -209,7 +212,8 @@ static void RTIP_To_Network_Address(BYTE * octet_address, return; } -static void set_broadcast_address(uint32_t net_address) +static void set_broadcast_address( + uint32_t net_address) { long broadcast_address = 0; long mask = 0; @@ -238,15 +242,16 @@ static void set_broadcast_address(uint32_t net_address) #endif } -bool bip_init(char *ifname) +bool bip_init( + char *ifname) { - int rv = 0; /* return from socket lib calls */ + int rv = 0; /* return from socket lib calls */ struct sockaddr_in sin = { -1 }; int value = 1; int sock_fd = -1; struct in_addr my_addr; - (void)ifname; + (void) ifname; NetInitialize(); diff --git a/bacnet-stack/ports/rtos32/dlmstp.c b/bacnet-stack/ports/rtos32/dlmstp.c index 7dcec4a1..f05d5a11 100644 --- a/bacnet-stack/ports/rtos32/dlmstp.c +++ b/bacnet-stack/ports/rtos32/dlmstp.c @@ -42,9 +42,10 @@ static uint8_t PDU_Buffer[MAX_MPDU]; /* local MS/TP port data */ static volatile struct mstp_port_struct_t MSTP_Port; -void dlmstp_init(char *ifname) +void dlmstp_init( + char *ifname) { - (void)ifname; + (void) ifname; /* initialize buffer */ Receive_Buffer.ready = false; Receive_Buffer.pdu_len = 0; @@ -53,17 +54,19 @@ void dlmstp_init(char *ifname) MSTP_Init(&MSTP_Port, MSTP_Port.This_Station); } -void dlmstp_cleanup(void) +void dlmstp_cleanup( + void) { /* nothing to do for static buffers */ } /* returns number of bytes sent on success, zero on failure */ -int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */ +int dlmstp_send_pdu( + BACNET_ADDRESS * dest, /* destination address */ BACNET_NPDU_DATA * npdu_data, /* network information */ - uint8_t * pdu, /* any data to be sent - may be null */ + uint8_t * pdu, /* any data to be sent - may be null */ unsigned pdu_len) -{ /* number of bytes of data */ +{ /* number of bytes of data */ int bytes_sent = 0; uint8_t frame_type = 0; uint8_t destination = 0; /* destination address */ @@ -74,8 +77,7 @@ int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */ if (npdu_data->confirmed_message) MSTP_Port.TxFrameType = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY; else - MSTP_Port.TxFrameType = - FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; + MSTP_Port.TxFrameType = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; /* load destination MAC address */ if (dest && dest->mac_len == 1) { @@ -109,16 +111,18 @@ int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */ } /* called about once a millisecond */ -void dlmstp_millisecond_timer(void) +void dlmstp_millisecond_timer( + void) { MSTP_Millisecond_Timer(&MSTP_Port); } /* returns the number of octets in the PDU, or zero on failure */ /* This function is expecting to be polled. */ -uint16_t dlmstp_receive(BACNET_ADDRESS * src, /* source address */ - uint8_t * pdu, /* PDU data */ - uint16_t max_pdu, /* amount of space available in the PDU */ +uint16_t dlmstp_receive( + BACNET_ADDRESS * src, /* source address */ + uint8_t * pdu, /* PDU data */ + uint16_t max_pdu, /* amount of space available in the PDU */ unsigned timeout) { uint16_t pdu_len = 0; @@ -137,8 +141,7 @@ uint16_t dlmstp_receive(BACNET_ADDRESS * src, /* source address */ } /* see if there is a packet available */ if (Receive_Buffer.ready) { - memmove(src, &Receive_Buffer.address, - sizeof(Receive_Buffer.address)); + memmove(src, &Receive_Buffer.address, sizeof(Receive_Buffer.address)); pdu_len = Receive_Buffer.pdu_len; memmove(&pdu[0], &Receive_Buffer.pdu[0], max_pdu); Receive_Buffer.ready = false; @@ -147,7 +150,9 @@ uint16_t dlmstp_receive(BACNET_ADDRESS * src, /* source address */ return pdu_len; } -void dlmstp_fill_bacnet_address(BACNET_ADDRESS * src, uint8_t mstp_address) +void dlmstp_fill_bacnet_address( + BACNET_ADDRESS * src, + uint8_t mstp_address) { int i = 0; @@ -171,8 +176,9 @@ void dlmstp_fill_bacnet_address(BACNET_ADDRESS * src, uint8_t mstp_address) } /* for the MS/TP state machine to use for putting received data */ -uint16_t dlmstp_put_receive(uint8_t src, /* source MS/TP address */ - uint8_t * pdu, /* PDU data */ +uint16_t dlmstp_put_receive( + uint8_t src, /* source MS/TP address */ + uint8_t * pdu, /* PDU data */ uint16_t pdu_len) { if (Receive_Buffer.ready) { @@ -191,7 +197,8 @@ uint16_t dlmstp_put_receive(uint8_t src, /* source MS/TP address */ return pdu_len; } -void dlmstp_set_my_address(uint8_t mac_address) +void dlmstp_set_my_address( + uint8_t mac_address) { /* FIXME: Master Nodes can only have address 1-127 */ MSTP_Port.This_Station = mac_address; @@ -206,14 +213,16 @@ void dlmstp_set_my_address(uint8_t mac_address) /* nodes. This may be used to allocate more or less of the available link */ /* bandwidth to particular nodes. If Max_Info_Frames is not writable in a */ /* node, its value shall be 1. */ -void dlmstp_set_max_info_frames(unsigned max_info_frames) +void dlmstp_set_max_info_frames( + unsigned max_info_frames) { MSTP_Port.Nmax_info_frames = max_info_frames; return; } -unsigned dlmstp_max_info_frames(void) +unsigned dlmstp_max_info_frames( + void) { return MSTP_Port.Nmax_info_frames; } @@ -223,21 +232,24 @@ unsigned dlmstp_max_info_frames(void) /* allowable address for master nodes. The value of Max_Master shall be */ /* less than or equal to 127. If Max_Master is not writable in a node, */ /* its value shall be 127. */ -void dlmstp_set_max_master(uint8_t max_master) +void dlmstp_set_max_master( + uint8_t max_master) { MSTP_Port.Nmax_master = max_master; return; } -uint8_t dlmstp_max_master(void) +uint8_t dlmstp_max_master( + void) { return MSTP_Port.Nmax_master; } -void dlmstp_get_my_address(BACNET_ADDRESS * my_address) +void dlmstp_get_my_address( + BACNET_ADDRESS * my_address) { - int i = 0; /* counter */ + int i = 0; /* counter */ my_address->mac_len = 1; my_address->mac[0] = MSTP_Port.This_Station; @@ -250,15 +262,16 @@ void dlmstp_get_my_address(BACNET_ADDRESS * my_address) return; } -void dlmstp_get_broadcast_address(BACNET_ADDRESS * dest) -{ /* destination address */ - int i = 0; /* counter */ +void dlmstp_get_broadcast_address( + BACNET_ADDRESS * dest) +{ /* destination address */ + int i = 0; /* counter */ if (dest) { dest->mac_len = 1; dest->mac[0] = MSTP_BROADCAST_ADDRESS; dest->net = BACNET_BROADCAST_NETWORK; - dest->len = 0; /* len=0 denotes broadcast address */ + dest->len = 0; /* len=0 denotes broadcast address */ for (i = 0; i < MAX_MAC_LEN; i++) { dest->adr[i] = 0; } diff --git a/bacnet-stack/ports/rtos32/ethernet.c b/bacnet-stack/ports/rtos32/ethernet.c index 5b61f510..4962048a 100644 --- a/bacnet-stack/ports/rtos32/ethernet.c +++ b/bacnet-stack/ports/rtos32/ethernet.c @@ -23,10 +23,10 @@ * *********************************************************************/ -#include /* for standard integer types uint8_t etc. */ -#include /* for the standard bool type. */ -#include /* for the standard bool type. */ -#include /* for the standard bool type. */ +#include /* for standard integer types uint8_t etc. */ +#include /* for the standard bool type. */ +#include /* for the standard bool type. */ +#include /* for the standard bool type. */ #include #include #include @@ -49,12 +49,14 @@ static SOCKET Ethernet_Socket = -1; /* used for binding 802.2 */ static struct sockaddr Ethernet_Address = { 0 }; -bool ethernet_valid(void) +bool ethernet_valid( + void) { return (Ethernet_Socket != -1); } -void ethernet_cleanup(void) +void ethernet_cleanup( + void) { if (ethernet_valid()) closesocket(Ethernet_Socket); @@ -63,7 +65,8 @@ void ethernet_cleanup(void) return; } -bool ethernet_init(char *interface_name) +bool ethernet_init( + char *interface_name) { int value = 1; @@ -85,12 +88,13 @@ bool ethernet_init(char *interface_name) /* function to send a packet out the 802.2 socket */ /* returns bytes sent on success, negative number on failure */ -int ethernet_send(BACNET_ADDRESS * dest, /* destination address */ +int ethernet_send( + BACNET_ADDRESS * dest, /* destination address */ BACNET_ADDRESS * src, /* source address */ BACNET_NPDU_DATA * npdu_data, /* network information */ - uint8_t * pdu, /* any data to be sent - may be null */ + uint8_t * pdu, /* any data to be sent - may be null */ unsigned pdu_len) -{ /* number of bytes of data */ +{ /* number of bytes of data */ int bytes = 0; uint8_t mtu[MAX_MPDU] = { 0 }; int mtu_len = 0; @@ -128,8 +132,7 @@ int ethernet_send(BACNET_ADDRESS * dest, /* destination address */ return -4; } /* packet length */ - mtu_len += encode_unsigned16(&mtu[12], - 3 /*DSAP,SSAP,LLC */ + pdu_len); + mtu_len += encode_unsigned16(&mtu[12], 3 /*DSAP,SSAP,LLC */ + pdu_len); /* Logical PDU portion */ mtu[mtu_len++] = 0x82; /* DSAP for BACnet */ mtu[mtu_len++] = 0x82; /* SSAP for BACnet */ @@ -156,12 +159,13 @@ int ethernet_send(BACNET_ADDRESS * dest, /* destination address */ /* function to send a packet out the 802.2 socket */ /* returns bytes sent on success, negative number on failure */ -int ethernet_send_pdu(BACNET_ADDRESS * dest, /* destination address */ +int ethernet_send_pdu( + BACNET_ADDRESS * dest, /* destination address */ BACNET_NPDU_DATA * npdu_data, /* network information */ - uint8_t * pdu, /* any data to be sent - may be null */ + uint8_t * pdu, /* any data to be sent - may be null */ unsigned pdu_len) -{ /* number of bytes of data */ - int i = 0; /* counter */ +{ /* number of bytes of data */ + int i = 0; /* counter */ BACNET_ADDRESS src = { 0 }; /* source address */ for (i = 0; i < 6; i++) { @@ -173,18 +177,19 @@ int ethernet_send_pdu(BACNET_ADDRESS * dest, /* destination address */ /* function to send a packet out the 802.2 socket */ /* returns 1 on success, 0 on failure */ return ethernet_send(dest, /* destination address */ - &src, /* source address */ - npdu_data, pdu, /* any data to be sent - may be null */ - pdu_len); /* number of bytes of data */ + &src, /* source address */ + npdu_data, pdu, /* any data to be sent - may be null */ + pdu_len); /* number of bytes of data */ } /* receives an 802.2 framed packet */ /* returns the number of octets in the PDU, or zero on failure */ -uint16_t ethernet_receive(BACNET_ADDRESS * src, /* source address */ - uint8_t * pdu, /* PDU data */ - uint16_t max_pdu, /* amount of space available in the PDU */ +uint16_t ethernet_receive( + BACNET_ADDRESS * src, /* source address */ + uint8_t * pdu, /* PDU data */ + uint16_t max_pdu, /* amount of space available in the PDU */ unsigned timeout) -{ /* number of milliseconds to wait for a packet */ +{ /* number of milliseconds to wait for a packet */ int received_bytes; uint8_t buf[MAX_MPDU] = { 0 }; /* data */ uint16_t pdu_len = 0; /* return value */ @@ -212,8 +217,7 @@ uint16_t ethernet_receive(BACNET_ADDRESS * src, /* source address */ max = Ethernet_Socket; if (select(max + 1, &read_fds, NULL, NULL, &select_timeout) > 0) - received_bytes = - recv(Ethernet_Socket, (char *) &buf[0], MAX_MPDU, 0); + received_bytes = recv(Ethernet_Socket, (char *) &buf[0], MAX_MPDU, 0); else return 0; @@ -262,7 +266,8 @@ uint16_t ethernet_receive(BACNET_ADDRESS * src, /* source address */ return pdu_len; } -void ethernet_get_my_address(BACNET_ADDRESS * my_address) +void ethernet_get_my_address( + BACNET_ADDRESS * my_address) { int i = 0; @@ -280,7 +285,8 @@ void ethernet_get_my_address(BACNET_ADDRESS * my_address) return; } -void ethernet_set_my_address(BACNET_ADDRESS * my_address) +void ethernet_set_my_address( + BACNET_ADDRESS * my_address) { int i = 0; @@ -291,9 +297,10 @@ void ethernet_set_my_address(BACNET_ADDRESS * my_address) return; } -void ethernet_get_broadcast_address(BACNET_ADDRESS * dest) -{ /* destination address */ - int i = 0; /* counter */ +void ethernet_get_broadcast_address( + BACNET_ADDRESS * dest) +{ /* destination address */ + int i = 0; /* counter */ if (dest) { for (i = 0; i < 6; i++) { @@ -301,7 +308,7 @@ void ethernet_get_broadcast_address(BACNET_ADDRESS * dest) } dest->mac_len = 6; dest->net = BACNET_BROADCAST_NETWORK; - dest->len = 0; /* denotes broadcast address */ + dest->len = 0; /* denotes broadcast address */ for (i = 0; i < MAX_MAC_LEN; i++) { dest->adr[i] = 0; } @@ -310,9 +317,11 @@ void ethernet_get_broadcast_address(BACNET_ADDRESS * dest) return; } -void ethernet_debug_address(const char *info, BACNET_ADDRESS * dest) +void ethernet_debug_address( + const char *info, + BACNET_ADDRESS * dest) { - int i = 0; /* counter */ + int i = 0; /* counter */ if (info) fprintf(stderr, "%s", info); diff --git a/bacnet-stack/ports/rtos32/init.c b/bacnet-stack/ports/rtos32/init.c index e4da3c1b..3e2653aa 100644 --- a/bacnet-stack/ports/rtos32/init.c +++ b/bacnet-stack/ports/rtos32/init.c @@ -27,7 +27,8 @@ #include #include -extern void RTEmuInit(void); +extern void RTEmuInit( + void); #ifdef _MSC_VER #define VOIDEXPORT _declspec(dllexport) void __cdecl @@ -36,11 +37,11 @@ extern void RTEmuInit(void); #endif /* DISK SYSTEM */ -#ifdef DOC /* include DiskOnChip driver */ +#ifdef DOC /* include DiskOnChip driver */ #include #define RTF_MAX_FILES 16 /* support for more open files (default is 8) */ #define RTF_BUFFERS_IN_BSS /* we do not need file I/O before the run-time */ -#include /* system is initialized */ +#include /* system is initialized */ /*#define READ_HEAD_BUFFER_SIZE 2048+4 */ @@ -89,8 +90,7 @@ RTFileSystem Console = { RT_FS_CONSOLE, 0, 0, &RTConsoleFileSystem }; RTFileSystem LPTFiles = { RT_FS_LPT_DEVICE, 0, 0, &RTLPTFileSystem }; /* logical drive Z: can be used to access the RAM drive */ -RTFileSystem RAMFiles = - { RT_FS_FILE, 1 << ('Z' - 'A'), 0, &RTRAMFileSystem }; +RTFileSystem RAMFiles = { RT_FS_FILE, 1 << ('Z' - 'A'), 0, &RTRAMFileSystem }; /* logical drive A: through D: are reserved for FAT */ RTFileSystem FATFiles = @@ -106,12 +106,13 @@ RTFileSystem *RTFileSystemList[] = { #endif /*-----------------------------------*/ -VOIDEXPORT Init(void) +VOIDEXPORT Init( + void) { (void) RTSetFlags(RT_MM_VIRTUAL, 1); /* this is the better method */ (void) RTCMOSExtendHeap(); /* get as much memory as we can */ RTCMOSSetSystemTime(); /* get the right date and time */ - RTEmuInit(); /* set up floating point emulation */ + RTEmuInit(); /* set up floating point emulation */ /* pizza - RTHaltCPL3 appears to cause problems with file handling */ /*RTIdleHandler = (void RTTAPI *)RTHaltCPL3; // low power when idle */ /* not needed with pre-emptive */ diff --git a/bacnet-stack/ports/rtos32/main.c b/bacnet-stack/ports/rtos32/main.c index 9807c704..82bc7d38 100644 --- a/bacnet-stack/ports/rtos32/main.c +++ b/bacnet-stack/ports/rtos32/main.c @@ -28,7 +28,7 @@ #include #include #include -#include /* for kbhit */ +#include /* for kbhit */ #include "config.h" #include "bacdef.h" #include "npdu.h" @@ -45,23 +45,23 @@ #define _USER32_ #define _KERNEL32_ #include -#include /* for RTCMOSSetSystemTime */ -#include /* file system */ -#include /* file system */ +#include /* for RTCMOSSetSystemTime */ +#include /* file system */ +#include /* file system */ #include #endif -#include /* serial port driver */ -#include /* time measurement & timer interrupt rate control */ -#include /* interrupt handler for the keyboard */ +#include /* serial port driver */ +#include /* time measurement & timer interrupt rate control */ +#include /* interrupt handler for the keyboard */ /* buffers used for transmit and receive */ static uint8_t Rx_Buf[MAX_MPDU] = { 0 }; -static void Init_Service_Handlers(void) +static void Init_Service_Handlers( + void) { /* we need to handle who-is to support dynamic device binding */ - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, - handler_who_is); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is); /* set the handler for all the services we don't implement */ /* It is required to send the proper reject message... */ apdu_set_unrecognized_service_handler_handler @@ -73,10 +73,11 @@ static void Init_Service_Handlers(void) handler_write_property); } -void millisecond_task(void) +void millisecond_task( + void) { - Time ticks = 0; /* task cycle */ - int i = 0; /* loop counter */ + Time ticks = 0; /* task cycle */ + int i = 0; /* loop counter */ ticks = RTKGetTime() + MilliSecsToTicks(1); while (TRUE) { @@ -86,11 +87,12 @@ void millisecond_task(void) } } -void RTOS_Initialize(void) +void RTOS_Initialize( + void) { /* allow OS to setup IRQ 1 by using a dummy call */ (void) kbhit(); - RTKernelInit(5); /* get the kernel going */ + RTKernelInit(5); /* get the kernel going */ RTKeybrdInit(); /*(void)CPUMoniInit(); /* not needed - just monitor idle task */ */ RTComInit(); @@ -128,7 +130,9 @@ void RTOS_Initialize(void) RTKCreateTask(millisecond_task, 16, 1024 * 8, "millisec task"); } -int main(int argc, char *argv[]) +int main( + int argc, + char *argv[]) { BACNET_ADDRESS src = { 0 }; /* address where message came from */ uint16_t pdu_len = 0; diff --git a/bacnet-stack/ports/rtos32/mstp.c b/bacnet-stack/ports/rtos32/mstp.c index 9eaca0e8..b5d25bf7 100644 --- a/bacnet-stack/ports/rtos32/mstp.c +++ b/bacnet-stack/ports/rtos32/mstp.c @@ -147,22 +147,24 @@ const uint16_t Tusage_timeout = 30; #define INCREMENT_AND_LIMIT_UINT16(x) {if (x < 0xFFFF) x++;} -bool MSTP_Line_Active(volatile struct mstp_port_struct_t *mstp_port) +bool MSTP_Line_Active( + volatile struct mstp_port_struct_t *mstp_port) { return (mstp_port->EventCount > Nmin_octets); } -unsigned MSTP_Create_Frame(uint8_t * buffer, /* where frame is loaded */ +unsigned MSTP_Create_Frame( + uint8_t * buffer, /* where frame is loaded */ unsigned buffer_len, /* amount of space available */ - uint8_t frame_type, /* type of frame to send - see defines */ + uint8_t frame_type, /* type of frame to send - see defines */ uint8_t destination, /* destination address */ - uint8_t source, /* source address */ - uint8_t * data, /* any data to be sent - may be null */ + uint8_t source, /* source address */ + uint8_t * data, /* any data to be sent - may be null */ unsigned data_len) -{ /* number of bytes of data (up to 501) */ +{ /* number of bytes of data (up to 501) */ uint8_t crc8 = 0xFF; /* used to calculate the crc value */ uint16_t crc16 = 0xFFFF; /* used to calculate the crc value */ - unsigned index = 0; /* used to load the data portion of the frame */ + unsigned index = 0; /* used to load the data portion of the frame */ /* not enough to do a header */ if (buffer_len < 8) @@ -202,33 +204,35 @@ unsigned MSTP_Create_Frame(uint8_t * buffer, /* where frame is loaded */ return 0; } - return index; /* returns the frame length */ + return index; /* returns the frame length */ } -void MSTP_Create_And_Send_Frame(volatile struct mstp_port_struct_t *mstp_port, /* port to send from */ - uint8_t frame_type, /* type of frame to send - see defines */ +void MSTP_Create_And_Send_Frame( + volatile struct mstp_port_struct_t *mstp_port, /* port to send from */ + uint8_t frame_type, /* type of frame to send - see defines */ uint8_t destination, /* destination address */ - uint8_t source, /* source address */ - uint8_t * data, /* any data to be sent - may be null */ + uint8_t source, /* source address */ + uint8_t * data, /* any data to be sent - may be null */ unsigned data_len) -{ /* number of bytes of data (up to 501) */ +{ /* number of bytes of data (up to 501) */ uint8_t buffer[MAX_MPDU] = { 0 }; /* buffer for sending */ - uint16_t len = 0; /* number of bytes to send */ + uint16_t len = 0; /* number of bytes to send */ len = (uint16_t) MSTP_Create_Frame(&buffer[0], /* where frame is loaded */ - sizeof(buffer), /* amount of space available */ - frame_type, /* type of frame to send - see defines */ - destination, /* destination address */ - source, /* source address */ - data, /* any data to be sent - may be null */ - data_len); /* number of bytes of data (up to 501) */ + sizeof(buffer), /* amount of space available */ + frame_type, /* type of frame to send - see defines */ + destination, /* destination address */ + source, /* source address */ + data, /* any data to be sent - may be null */ + data_len); /* number of bytes of data (up to 501) */ RS485_Send_Frame(mstp_port, &buffer[0], len); /* FIXME: be sure to reset SilenceTimer after each octet is sent! */ } /* Millisecond Timer - called every millisecond */ -void MSTP_Millisecond_Timer(volatile struct mstp_port_struct_t *mstp_port) +void MSTP_Millisecond_Timer( + volatile struct mstp_port_struct_t *mstp_port) { INCREMENT_AND_LIMIT_UINT16(mstp_port->SilenceTimer); INCREMENT_AND_LIMIT_UINT16(mstp_port->ReplyPostponedTimer); @@ -236,35 +240,37 @@ void MSTP_Millisecond_Timer(volatile struct mstp_port_struct_t *mstp_port) } #if PRINT_ENABLED_RECEIVE -char *mstp_receive_state_text(int state) +char *mstp_receive_state_text( + int state) { char *text = "unknown"; switch (state) { - case MSTP_RECEIVE_STATE_IDLE: - text = "IDLE"; - break; - case MSTP_RECEIVE_STATE_PREAMBLE: - text = "PREAMBLE"; - break; - case MSTP_RECEIVE_STATE_HEADER: - text = "HEADER"; - break; - case MSTP_RECEIVE_STATE_HEADER_CRC: - text = "HEADER_CRC"; - break; - case MSTP_RECEIVE_STATE_DATA: - text = "DATA"; - break; - default: - break; + case MSTP_RECEIVE_STATE_IDLE: + text = "IDLE"; + break; + case MSTP_RECEIVE_STATE_PREAMBLE: + text = "PREAMBLE"; + break; + case MSTP_RECEIVE_STATE_HEADER: + text = "HEADER"; + break; + case MSTP_RECEIVE_STATE_HEADER_CRC: + text = "HEADER_CRC"; + break; + case MSTP_RECEIVE_STATE_DATA: + text = "DATA"; + break; + default: + break; } return text; } #endif -void MSTP_Receive_Frame_FSM(volatile struct mstp_port_struct_t *mstp_port) +void MSTP_Receive_Frame_FSM( + volatile struct mstp_port_struct_t *mstp_port) { #if PRINT_ENABLED_RECEIVE_DATA static MSTP_RECEIVE_STATE receive_state = MSTP_RECEIVE_STATE_IDLE; @@ -274,321 +280,320 @@ void MSTP_Receive_Frame_FSM(volatile struct mstp_port_struct_t *mstp_port) "MSTP Rx: State=%s Data=%02X hCRC=%02X Index=%u EC=%u DateLen=%u Silence=%u\n", mstp_receive_state_text(mstp_port->receive_state), mstp_port->DataRegister, mstp_port->HeaderCRC, mstp_port->Index, - mstp_port->EventCount, mstp_port->DataLength, - mstp_port->SilenceTimer); + mstp_port->EventCount, mstp_port->DataLength, mstp_port->SilenceTimer); #endif switch (mstp_port->receive_state) { - /* In the IDLE state, the node waits for the beginning of a frame. */ - case MSTP_RECEIVE_STATE_IDLE: - /* EatAnError */ - if (mstp_port->ReceiveError == true) { - mstp_port->ReceiveError = false; - mstp_port->SilenceTimer = 0; - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - /* wait for the start of a frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } else if (mstp_port->DataAvailable == true) { -#if PRINT_ENABLED_RECEIVE_DATA - fprintf(stderr, "MSTP Rx: %02X ", mstp_port->DataRegister); -#endif - /* Preamble1 */ - if (mstp_port->DataRegister == 0x55) { - mstp_port->DataAvailable = false; - mstp_port->SilenceTimer = 0; - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - /* receive the remainder of the frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_PREAMBLE; - } - /* EatAnOctet */ - else { -#if PRINT_ENABLED_RECEIVE_DATA - fprintf(stderr, "\n"); -#endif - mstp_port->DataAvailable = false; - mstp_port->SilenceTimer = 0; - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - /* wait for the start of a frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } - } - break; - /* In the PREAMBLE state, the node waits for the second octet of the preamble. */ - case MSTP_RECEIVE_STATE_PREAMBLE: - /* Timeout */ - if (mstp_port->SilenceTimer > Tframe_abort) { - /* a correct preamble has not been received */ - /* wait for the start of a frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } - /* Error */ - else if (mstp_port->ReceiveError == true) { - mstp_port->ReceiveError = false; - mstp_port->SilenceTimer = 0; - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - /* wait for the start of a frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } else if (mstp_port->DataAvailable == true) { -#if PRINT_ENABLED_RECEIVE_DATA - fprintf(stderr, "%02X ", mstp_port->DataRegister); -#endif - /* Preamble2 */ - if (mstp_port->DataRegister == 0xFF) { - mstp_port->DataAvailable = false; - mstp_port->SilenceTimer = 0; - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - mstp_port->Index = 0; - mstp_port->HeaderCRC = 0xFF; - /* receive the remainder of the frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER; - } - /* ignore RepeatedPreamble1 */ - else if (mstp_port->DataRegister == 0x55) { - mstp_port->DataAvailable = false; - mstp_port->SilenceTimer = 0; - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - /* wait for the second preamble octet. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_PREAMBLE; - } - /* NotPreamble */ - else { - mstp_port->DataAvailable = false; - mstp_port->SilenceTimer = 0; - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - /* wait for the start of a frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } - } - break; - /* In the HEADER state, the node waits for the fixed message header. */ - case MSTP_RECEIVE_STATE_HEADER: - /* Timeout */ - if (mstp_port->SilenceTimer > Tframe_abort) { - /* indicate that an error has occurred during the reception of a frame */ - mstp_port->ReceivedInvalidFrame = true; - /* wait for the start of a frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } - /* Error */ - else if (mstp_port->ReceiveError == true) { - mstp_port->ReceiveError = false; - mstp_port->SilenceTimer = 0; - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - /* indicate that an error has occurred during the reception of a frame */ - mstp_port->ReceivedInvalidFrame = true; - /* wait for the start of a frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } else if (mstp_port->DataAvailable == true) { -#if PRINT_ENABLED_RECEIVE_DATA - fprintf(stderr, "%02X ", mstp_port->DataRegister); -#endif - /* FrameType */ - if (mstp_port->Index == 0) { - mstp_port->SilenceTimer = 0; - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - mstp_port->HeaderCRC = - CRC_Calc_Header(mstp_port->DataRegister, - mstp_port->HeaderCRC); - mstp_port->FrameType = mstp_port->DataRegister; - mstp_port->DataAvailable = false; - mstp_port->Index = 1; - mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER; - } - /* Destination */ - else if (mstp_port->Index == 1) { - mstp_port->SilenceTimer = 0; - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - mstp_port->HeaderCRC = - CRC_Calc_Header(mstp_port->DataRegister, - mstp_port->HeaderCRC); - mstp_port->DestinationAddress = mstp_port->DataRegister; - mstp_port->DataAvailable = false; - mstp_port->Index = 2; - mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER; - } - /* Source */ - else if (mstp_port->Index == 2) { - mstp_port->SilenceTimer = 0; - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - mstp_port->HeaderCRC = - CRC_Calc_Header(mstp_port->DataRegister, - mstp_port->HeaderCRC); - mstp_port->SourceAddress = mstp_port->DataRegister; - mstp_port->DataAvailable = false; - mstp_port->Index = 3; - mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER; - } - /* Length1 */ - else if (mstp_port->Index == 3) { - mstp_port->SilenceTimer = 0; - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - mstp_port->HeaderCRC = - CRC_Calc_Header(mstp_port->DataRegister, - mstp_port->HeaderCRC); - mstp_port->DataLength = mstp_port->DataRegister * 256; - mstp_port->DataAvailable = false; - mstp_port->Index = 4; - mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER; - } - /* Length2 */ - else if (mstp_port->Index == 4) { - mstp_port->SilenceTimer = 0; - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - mstp_port->HeaderCRC = - CRC_Calc_Header(mstp_port->DataRegister, - mstp_port->HeaderCRC); - mstp_port->DataLength += mstp_port->DataRegister; - mstp_port->DataAvailable = false; - mstp_port->Index = 5; - mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER; - } - /* HeaderCRC */ - else if (mstp_port->Index == 5) { - mstp_port->SilenceTimer = 0; - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - mstp_port->HeaderCRC = - CRC_Calc_Header(mstp_port->DataRegister, - mstp_port->HeaderCRC); - mstp_port->DataAvailable = false; - /* don't wait for next state - do it here */ - /* MSTP_RECEIVE_STATE_HEADER_CRC */ - if (mstp_port->HeaderCRC != 0x55) { - /* BadCRC */ - /* indicate that an error has occurred during the reception of a frame */ - mstp_port->ReceivedInvalidFrame = true; - /* wait for the start of the next frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } else { - if ((mstp_port->DestinationAddress == - mstp_port->This_Station) - || (mstp_port->DestinationAddress == - MSTP_BROADCAST_ADDRESS)) { - /* FrameTooLong */ - if (mstp_port->DataLength > MAX_MPDU) { - /* indicate that a frame with an illegal or */ - /* unacceptable data length has been received */ - mstp_port->ReceivedInvalidFrame = true; - /* wait for the start of the next frame. */ - mstp_port->receive_state = - MSTP_RECEIVE_STATE_IDLE; - } - /* NoData */ - else if (mstp_port->DataLength == 0) { - /* CHEAT: it is very difficult to respond to - poll for master in the Master Node state machine - before Tusage_timeout, so we will do it here. */ - if ((mstp_port->FrameType == - FRAME_TYPE_POLL_FOR_MASTER) - && (mstp_port->DestinationAddress == - mstp_port->This_Station) - && (mstp_port->master_state == - MSTP_MASTER_STATE_IDLE)) { - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER, - mstp_port->SourceAddress, - mstp_port->This_Station, NULL, 0); - /* don't indicate that a frame has been received */ - mstp_port->ReceivedInvalidFrame = false; - mstp_port->ReceivedValidFrame = false; - } else { - /* indicate that a frame with no data has been received */ - mstp_port->ReceivedValidFrame = true; - } - /* wait for the start of the next frame. */ - mstp_port->receive_state = - MSTP_RECEIVE_STATE_IDLE; - } - /* Data */ - else { - mstp_port->Index = 0; - mstp_port->DataCRC = 0xFFFF; - /* receive the data portion of the frame. */ - mstp_port->receive_state = - MSTP_RECEIVE_STATE_DATA; - } - } - /* NotForUs */ - else { - /* wait for the start of the next frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } - } - - - } - /* not per MS/TP standard, but it is a case not covered */ - else { + /* In the IDLE state, the node waits for the beginning of a frame. */ + case MSTP_RECEIVE_STATE_IDLE: + /* EatAnError */ + if (mstp_port->ReceiveError == true) { mstp_port->ReceiveError = false; mstp_port->SilenceTimer = 0; INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - /* indicate that an error has occurred during */ - /* the reception of a frame */ + /* wait for the start of a frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + } else if (mstp_port->DataAvailable == true) { +#if PRINT_ENABLED_RECEIVE_DATA + fprintf(stderr, "MSTP Rx: %02X ", mstp_port->DataRegister); +#endif + /* Preamble1 */ + if (mstp_port->DataRegister == 0x55) { + mstp_port->DataAvailable = false; + mstp_port->SilenceTimer = 0; + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + /* receive the remainder of the frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_PREAMBLE; + } + /* EatAnOctet */ + else { +#if PRINT_ENABLED_RECEIVE_DATA + fprintf(stderr, "\n"); +#endif + mstp_port->DataAvailable = false; + mstp_port->SilenceTimer = 0; + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + /* wait for the start of a frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + } + } + break; + /* In the PREAMBLE state, the node waits for the second octet of the preamble. */ + case MSTP_RECEIVE_STATE_PREAMBLE: + /* Timeout */ + if (mstp_port->SilenceTimer > Tframe_abort) { + /* a correct preamble has not been received */ + /* wait for the start of a frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + } + /* Error */ + else if (mstp_port->ReceiveError == true) { + mstp_port->ReceiveError = false; + mstp_port->SilenceTimer = 0; + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + /* wait for the start of a frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + } else if (mstp_port->DataAvailable == true) { +#if PRINT_ENABLED_RECEIVE_DATA + fprintf(stderr, "%02X ", mstp_port->DataRegister); +#endif + /* Preamble2 */ + if (mstp_port->DataRegister == 0xFF) { + mstp_port->DataAvailable = false; + mstp_port->SilenceTimer = 0; + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + mstp_port->Index = 0; + mstp_port->HeaderCRC = 0xFF; + /* receive the remainder of the frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER; + } + /* ignore RepeatedPreamble1 */ + else if (mstp_port->DataRegister == 0x55) { + mstp_port->DataAvailable = false; + mstp_port->SilenceTimer = 0; + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + /* wait for the second preamble octet. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_PREAMBLE; + } + /* NotPreamble */ + else { + mstp_port->DataAvailable = false; + mstp_port->SilenceTimer = 0; + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + /* wait for the start of a frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + } + } + break; + /* In the HEADER state, the node waits for the fixed message header. */ + case MSTP_RECEIVE_STATE_HEADER: + /* Timeout */ + if (mstp_port->SilenceTimer > Tframe_abort) { + /* indicate that an error has occurred during the reception of a frame */ mstp_port->ReceivedInvalidFrame = true; /* wait for the start of a frame. */ mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; } - } - break; - /* In the HEADER_CRC state, the node validates the CRC on the fixed */ - /* message header. */ - case MSTP_RECEIVE_STATE_HEADER_CRC: - break; - /* In the DATA state, the node waits for the data portion of a frame. */ - case MSTP_RECEIVE_STATE_DATA: - /* Timeout */ - if (mstp_port->SilenceTimer > Tframe_abort) { - /* indicate that an error has occurred during the reception of a frame */ - mstp_port->ReceivedInvalidFrame = true; - /* wait for the start of the next frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } - /* Error */ - else if (mstp_port->ReceiveError == true) { - mstp_port->ReceiveError = false; - mstp_port->SilenceTimer = 0; - /* indicate that an error has occurred during the reception of a frame */ - mstp_port->ReceivedInvalidFrame = true; - /* wait for the start of the next frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } else if (mstp_port->DataAvailable == true) { + /* Error */ + else if (mstp_port->ReceiveError == true) { + mstp_port->ReceiveError = false; + mstp_port->SilenceTimer = 0; + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + /* indicate that an error has occurred during the reception of a frame */ + mstp_port->ReceivedInvalidFrame = true; + /* wait for the start of a frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + } else if (mstp_port->DataAvailable == true) { #if PRINT_ENABLED_RECEIVE_DATA - fprintf(stderr, "%02X ", mstp_port->DataRegister); + fprintf(stderr, "%02X ", mstp_port->DataRegister); #endif - /* DataOctet */ - if (mstp_port->Index < mstp_port->DataLength) { - mstp_port->DataCRC = CRC_Calc_Data(mstp_port->DataRegister, - mstp_port->DataCRC); - mstp_port->InputBuffer[mstp_port->Index] = - mstp_port->DataRegister; - mstp_port->Index++; - mstp_port->receive_state = MSTP_RECEIVE_STATE_DATA; - } - /* CRC1 */ - else if (mstp_port->Index == mstp_port->DataLength) { - mstp_port->DataCRC = CRC_Calc_Data(mstp_port->DataRegister, - mstp_port->DataCRC); - mstp_port->Index++; - mstp_port->receive_state = MSTP_RECEIVE_STATE_DATA; - } - /* CRC2 */ - else if (mstp_port->Index == (mstp_port->DataLength + 1)) { - mstp_port->DataCRC = CRC_Calc_Data(mstp_port->DataRegister, - mstp_port->DataCRC); - /* STATE DATA CRC - no need for new state */ - /* indicate the complete reception of a valid frame */ - if (mstp_port->DataCRC == 0xF0B8) - mstp_port->ReceivedValidFrame = true; - else + /* FrameType */ + if (mstp_port->Index == 0) { + mstp_port->SilenceTimer = 0; + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + mstp_port->HeaderCRC = + CRC_Calc_Header(mstp_port->DataRegister, + mstp_port->HeaderCRC); + mstp_port->FrameType = mstp_port->DataRegister; + mstp_port->DataAvailable = false; + mstp_port->Index = 1; + mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER; + } + /* Destination */ + else if (mstp_port->Index == 1) { + mstp_port->SilenceTimer = 0; + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + mstp_port->HeaderCRC = + CRC_Calc_Header(mstp_port->DataRegister, + mstp_port->HeaderCRC); + mstp_port->DestinationAddress = mstp_port->DataRegister; + mstp_port->DataAvailable = false; + mstp_port->Index = 2; + mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER; + } + /* Source */ + else if (mstp_port->Index == 2) { + mstp_port->SilenceTimer = 0; + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + mstp_port->HeaderCRC = + CRC_Calc_Header(mstp_port->DataRegister, + mstp_port->HeaderCRC); + mstp_port->SourceAddress = mstp_port->DataRegister; + mstp_port->DataAvailable = false; + mstp_port->Index = 3; + mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER; + } + /* Length1 */ + else if (mstp_port->Index == 3) { + mstp_port->SilenceTimer = 0; + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + mstp_port->HeaderCRC = + CRC_Calc_Header(mstp_port->DataRegister, + mstp_port->HeaderCRC); + mstp_port->DataLength = mstp_port->DataRegister * 256; + mstp_port->DataAvailable = false; + mstp_port->Index = 4; + mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER; + } + /* Length2 */ + else if (mstp_port->Index == 4) { + mstp_port->SilenceTimer = 0; + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + mstp_port->HeaderCRC = + CRC_Calc_Header(mstp_port->DataRegister, + mstp_port->HeaderCRC); + mstp_port->DataLength += mstp_port->DataRegister; + mstp_port->DataAvailable = false; + mstp_port->Index = 5; + mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER; + } + /* HeaderCRC */ + else if (mstp_port->Index == 5) { + mstp_port->SilenceTimer = 0; + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + mstp_port->HeaderCRC = + CRC_Calc_Header(mstp_port->DataRegister, + mstp_port->HeaderCRC); + mstp_port->DataAvailable = false; + /* don't wait for next state - do it here */ + /* MSTP_RECEIVE_STATE_HEADER_CRC */ + if (mstp_port->HeaderCRC != 0x55) { + /* BadCRC */ + /* indicate that an error has occurred during the reception of a frame */ + mstp_port->ReceivedInvalidFrame = true; + /* wait for the start of the next frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + } else { + if ((mstp_port->DestinationAddress == + mstp_port->This_Station) + || (mstp_port->DestinationAddress == + MSTP_BROADCAST_ADDRESS)) { + /* FrameTooLong */ + if (mstp_port->DataLength > MAX_MPDU) { + /* indicate that a frame with an illegal or */ + /* unacceptable data length has been received */ + mstp_port->ReceivedInvalidFrame = true; + /* wait for the start of the next frame. */ + mstp_port->receive_state = + MSTP_RECEIVE_STATE_IDLE; + } + /* NoData */ + else if (mstp_port->DataLength == 0) { + /* CHEAT: it is very difficult to respond to + poll for master in the Master Node state machine + before Tusage_timeout, so we will do it here. */ + if ((mstp_port->FrameType == + FRAME_TYPE_POLL_FOR_MASTER) + && (mstp_port->DestinationAddress == + mstp_port->This_Station) + && (mstp_port->master_state == + MSTP_MASTER_STATE_IDLE)) { + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER, + mstp_port->SourceAddress, + mstp_port->This_Station, NULL, 0); + /* don't indicate that a frame has been received */ + mstp_port->ReceivedInvalidFrame = false; + mstp_port->ReceivedValidFrame = false; + } else { + /* indicate that a frame with no data has been received */ + mstp_port->ReceivedValidFrame = true; + } + /* wait for the start of the next frame. */ + mstp_port->receive_state = + MSTP_RECEIVE_STATE_IDLE; + } + /* Data */ + else { + mstp_port->Index = 0; + mstp_port->DataCRC = 0xFFFF; + /* receive the data portion of the frame. */ + mstp_port->receive_state = + MSTP_RECEIVE_STATE_DATA; + } + } + /* NotForUs */ + else { + /* wait for the start of the next frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + } + } + + + } + /* not per MS/TP standard, but it is a case not covered */ + else { + mstp_port->ReceiveError = false; + mstp_port->SilenceTimer = 0; + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + /* indicate that an error has occurred during */ + /* the reception of a frame */ mstp_port->ReceivedInvalidFrame = true; + /* wait for the start of a frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + } + } + break; + /* In the HEADER_CRC state, the node validates the CRC on the fixed */ + /* message header. */ + case MSTP_RECEIVE_STATE_HEADER_CRC: + break; + /* In the DATA state, the node waits for the data portion of a frame. */ + case MSTP_RECEIVE_STATE_DATA: + /* Timeout */ + if (mstp_port->SilenceTimer > Tframe_abort) { + /* indicate that an error has occurred during the reception of a frame */ + mstp_port->ReceivedInvalidFrame = true; + /* wait for the start of the next frame. */ mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; } - mstp_port->DataAvailable = false; - mstp_port->SilenceTimer = 0; - } - break; - default: - /* shouldn't get here - but if we do... */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - break; + /* Error */ + else if (mstp_port->ReceiveError == true) { + mstp_port->ReceiveError = false; + mstp_port->SilenceTimer = 0; + /* indicate that an error has occurred during the reception of a frame */ + mstp_port->ReceivedInvalidFrame = true; + /* wait for the start of the next frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + } else if (mstp_port->DataAvailable == true) { +#if PRINT_ENABLED_RECEIVE_DATA + fprintf(stderr, "%02X ", mstp_port->DataRegister); +#endif + /* DataOctet */ + if (mstp_port->Index < mstp_port->DataLength) { + mstp_port->DataCRC = CRC_Calc_Data(mstp_port->DataRegister, + mstp_port->DataCRC); + mstp_port->InputBuffer[mstp_port->Index] = + mstp_port->DataRegister; + mstp_port->Index++; + mstp_port->receive_state = MSTP_RECEIVE_STATE_DATA; + } + /* CRC1 */ + else if (mstp_port->Index == mstp_port->DataLength) { + mstp_port->DataCRC = CRC_Calc_Data(mstp_port->DataRegister, + mstp_port->DataCRC); + mstp_port->Index++; + mstp_port->receive_state = MSTP_RECEIVE_STATE_DATA; + } + /* CRC2 */ + else if (mstp_port->Index == (mstp_port->DataLength + 1)) { + mstp_port->DataCRC = CRC_Calc_Data(mstp_port->DataRegister, + mstp_port->DataCRC); + /* STATE DATA CRC - no need for new state */ + /* indicate the complete reception of a valid frame */ + if (mstp_port->DataCRC == 0xF0B8) + mstp_port->ReceivedValidFrame = true; + else + mstp_port->ReceivedInvalidFrame = true; + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + } + mstp_port->DataAvailable = false; + mstp_port->SilenceTimer = 0; + } + break; + default: + /* shouldn't get here - but if we do... */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + break; } #if PRINT_ENABLED_RECEIVE_DATA if ((receive_state != MSTP_RECEIVE_STATE_IDLE) && @@ -603,40 +608,41 @@ void MSTP_Receive_Frame_FSM(volatile struct mstp_port_struct_t *mstp_port) } #if PRINT_ENABLED -char *mstp_master_state_text(int state) +char *mstp_master_state_text( + int state) { char *text = "unknown"; switch (state) { - case MSTP_MASTER_STATE_INITIALIZE: - text = "INITIALIZE"; - break; - case MSTP_MASTER_STATE_IDLE: - text = "IDLE"; - break; - case MSTP_MASTER_STATE_USE_TOKEN: - text = "USE_TOKEN"; - break; - case MSTP_MASTER_STATE_WAIT_FOR_REPLY: - text = "WAIT_FOR_REPLY"; - break; - case MSTP_MASTER_STATE_DONE_WITH_TOKEN: - text = "IDLE"; - break; - case MSTP_MASTER_STATE_PASS_TOKEN: - text = "DONE_WITH_TOKEN"; - break; - case MSTP_MASTER_STATE_NO_TOKEN: - text = "NO_TOKEN"; - break; - case MSTP_MASTER_STATE_POLL_FOR_MASTER: - text = "POLL_FOR_MASTER"; - break; - case MSTP_MASTER_STATE_ANSWER_DATA_REQUEST: - text = "ANSWER_DATA_REQUEST"; - break; - default: - break; + case MSTP_MASTER_STATE_INITIALIZE: + text = "INITIALIZE"; + break; + case MSTP_MASTER_STATE_IDLE: + text = "IDLE"; + break; + case MSTP_MASTER_STATE_USE_TOKEN: + text = "USE_TOKEN"; + break; + case MSTP_MASTER_STATE_WAIT_FOR_REPLY: + text = "WAIT_FOR_REPLY"; + break; + case MSTP_MASTER_STATE_DONE_WITH_TOKEN: + text = "IDLE"; + break; + case MSTP_MASTER_STATE_PASS_TOKEN: + text = "DONE_WITH_TOKEN"; + break; + case MSTP_MASTER_STATE_NO_TOKEN: + text = "NO_TOKEN"; + break; + case MSTP_MASTER_STATE_POLL_FOR_MASTER: + text = "POLL_FOR_MASTER"; + break; + case MSTP_MASTER_STATE_ANSWER_DATA_REQUEST: + text = "ANSWER_DATA_REQUEST"; + break; + default: + break; } return text; @@ -644,40 +650,41 @@ char *mstp_master_state_text(int state) #endif #if PRINT_ENABLED -char *mstp_frame_type_text(int type) +char *mstp_frame_type_text( + int type) { char *text = "unknown"; switch (type) { - case FRAME_TYPE_TOKEN: - text = "TOKEN"; - break; - case FRAME_TYPE_POLL_FOR_MASTER: - text = "POLL_FOR_MASTER"; - break; - case FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER: - text = "REPLY_TO_POLL_FOR_MASTER"; - break; - case FRAME_TYPE_TEST_REQUEST: - text = "TEST_REQUEST"; - break; - case FRAME_TYPE_TEST_RESPONSE: - text = "TEST_RESPONSE"; - break; - case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: - text = "BACNET_DATA_EXPECTING_REPLY"; - break; - case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: - text = "BACNET_DATA_NOT_EXPECTING_REPLY"; - break; - case FRAME_TYPE_REPLY_POSTPONED: - text = "REPLY_POSTPONED"; - break; - default: - if ((type >= FRAME_TYPE_PROPRIETARY_MIN) && - (type <= FRAME_TYPE_PROPRIETARY_MAX)) - text = "PROPRIETARY"; - break; + case FRAME_TYPE_TOKEN: + text = "TOKEN"; + break; + case FRAME_TYPE_POLL_FOR_MASTER: + text = "POLL_FOR_MASTER"; + break; + case FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER: + text = "REPLY_TO_POLL_FOR_MASTER"; + break; + case FRAME_TYPE_TEST_REQUEST: + text = "TEST_REQUEST"; + break; + case FRAME_TYPE_TEST_RESPONSE: + text = "TEST_RESPONSE"; + break; + case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: + text = "BACNET_DATA_EXPECTING_REPLY"; + break; + case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: + text = "BACNET_DATA_NOT_EXPECTING_REPLY"; + break; + case FRAME_TYPE_REPLY_POSTPONED: + text = "REPLY_POSTPONED"; + break; + default: + if ((type >= FRAME_TYPE_PROPRIETARY_MIN) && + (type <= FRAME_TYPE_PROPRIETARY_MAX)) + text = "PROPRIETARY"; + break; } return text; @@ -685,7 +692,8 @@ char *mstp_frame_type_text(int type) #endif /* returns true if we need to transition immediately */ -bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t * mstp_port) +bool MSTP_Master_Node_FSM( + volatile struct mstp_port_struct_t * mstp_port) { int mtu_len = 0; int frame_type = 0; @@ -725,502 +733,507 @@ bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t * mstp_port) #endif switch (mstp_port->master_state) { - case MSTP_MASTER_STATE_INITIALIZE: - /* DoneInitializing */ - /* indicate that the next station is unknown */ - mstp_port->Next_Station = mstp_port->This_Station; - mstp_port->Poll_Station = mstp_port->This_Station; - /* cause a Poll For Master to be sent when this node first */ - /* receives the token */ - mstp_port->TokenCount = Npoll; - mstp_port->SoleMaster = false; - mstp_port->master_state = MSTP_MASTER_STATE_IDLE; - transition_now = true; - break; - /* In the IDLE state, the node waits for a frame. */ - case MSTP_MASTER_STATE_IDLE: - /* LostToken */ - if (mstp_port->SilenceTimer >= Tno_token) { - /* assume that the token has been lost */ - mstp_port->EventCount = 0; /* Addendum 135-2004d-8 */ - mstp_port->master_state = MSTP_MASTER_STATE_NO_TOKEN; + case MSTP_MASTER_STATE_INITIALIZE: + /* DoneInitializing */ + /* indicate that the next station is unknown */ + mstp_port->Next_Station = mstp_port->This_Station; + mstp_port->Poll_Station = mstp_port->This_Station; + /* cause a Poll For Master to be sent when this node first */ + /* receives the token */ + mstp_port->TokenCount = Npoll; + mstp_port->SoleMaster = false; + mstp_port->master_state = MSTP_MASTER_STATE_IDLE; transition_now = true; - } - /* ReceivedInvalidFrame */ - else if (mstp_port->ReceivedInvalidFrame == true) { - /* invalid frame was received */ - mstp_port->ReceivedInvalidFrame = false; - /* wait for the next frame - remain in IDLE */ - } else if (mstp_port->ReceivedValidFrame == true) { -#if PRINT_ENABLED_MASTER - fprintf(stderr, - "MSTP: ReceivedValidFrame Src=%02X Dest=%02X DataLen=%u FC=%u ST=%u Type=%s\n", - mstp_port->SourceAddress, - mstp_port->DestinationAddress, - mstp_port->DataLength, - mstp_port->FrameCount, - mstp_port->SilenceTimer, - mstp_frame_type_text(mstp_port->FrameType)); -#endif - /* destined for me! */ - if ((mstp_port->DestinationAddress == - mstp_port->This_Station) || - (mstp_port->DestinationAddress == - MSTP_BROADCAST_ADDRESS)) { - switch (mstp_port->FrameType) { - /* ReceivedToken */ - case FRAME_TYPE_TOKEN: - /* tokens can't be broadcast */ - if (mstp_port->DestinationAddress == - MSTP_BROADCAST_ADDRESS) - break; - mstp_port->ReceivedValidFrame = false; - mstp_port->FrameCount = 0; - mstp_port->SoleMaster = false; - mstp_port->master_state = MSTP_MASTER_STATE_USE_TOKEN; - transition_now = true; - break; - /* ReceivedPFM */ - case FRAME_TYPE_POLL_FOR_MASTER: - /* CHEAT: we cheat a little and this is really handled in the - receive state machine since it is difficult to respond - quick enough (i.e. faster than Tusage_timeout of the - other node which could be 20ms). */ - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER, - mstp_port->SourceAddress, mstp_port->This_Station, - NULL, 0); - break; - case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: - /* indicate successful reception to the higher layers */ - dlmstp_put_receive(mstp_port->SourceAddress, - (uint8_t *) & mstp_port->InputBuffer[0], - mstp_port->DataLength); - break; - case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: - mstp_port->ReplyPostponedTimer = 0; - /* indicate successful reception to the higher layers */ - dlmstp_put_receive(mstp_port->SourceAddress, /* source MS/TP address */ - (uint8_t *) & mstp_port->InputBuffer[0], - mstp_port->DataLength); - /* broadcast DER just remains IDLE */ - if (mstp_port->DestinationAddress != - MSTP_BROADCAST_ADDRESS) { - mstp_port->master_state = - MSTP_MASTER_STATE_ANSWER_DATA_REQUEST; - transition_now = true; - } - break; - case FRAME_TYPE_TEST_REQUEST: - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_TEST_RESPONSE, - mstp_port->SourceAddress, mstp_port->This_Station, - NULL, 0); - break; - case FRAME_TYPE_TEST_RESPONSE: - default: - break; - } - } - mstp_port->ReceivedValidFrame = false; - } - break; - /* In the USE_TOKEN state, the node is allowed to send one or */ - /* more data frames. These may be BACnet Data frames or */ - /* proprietary frames. */ - case MSTP_MASTER_STATE_USE_TOKEN: - if (!mstp_port->TxReady) { - /* NothingToSend */ - mstp_port->FrameCount = mstp_port->Nmax_info_frames; - mstp_port->master_state = MSTP_MASTER_STATE_DONE_WITH_TOKEN; - } else { - uint8_t destination = mstp_port->TxBuffer[3]; - RS485_Send_Frame(mstp_port, - (uint8_t *) & mstp_port->TxBuffer[0], mstp_port->TxLength); - mstp_port->FrameCount++; - switch (mstp_port->TxFrameType) { - case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: - /* SendAndWait */ - if (destination == MSTP_BROADCAST_ADDRESS) - mstp_port->master_state = - MSTP_MASTER_STATE_DONE_WITH_TOKEN; - else - mstp_port->master_state = - MSTP_MASTER_STATE_WAIT_FOR_REPLY; - break; - case FRAME_TYPE_TEST_REQUEST: - mstp_port->master_state = MSTP_MASTER_STATE_WAIT_FOR_REPLY; - break; - case FRAME_TYPE_TEST_RESPONSE: - case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: - default: - /* SendNoWait */ - mstp_port->master_state = - MSTP_MASTER_STATE_DONE_WITH_TOKEN; - break; - } - mstp_port->TxReady = false; - } - transition_now = true; - break; - /* In the WAIT_FOR_REPLY state, the node waits for */ - /* a reply from another node. */ - case MSTP_MASTER_STATE_WAIT_FOR_REPLY: - if (mstp_port->SilenceTimer >= Treply_timeout) { - /* ReplyTimeout */ - /* assume that the request has failed */ - mstp_port->FrameCount = mstp_port->Nmax_info_frames; - mstp_port->master_state = MSTP_MASTER_STATE_DONE_WITH_TOKEN; - /* Any retry of the data frame shall await the next entry */ - /* to the USE_TOKEN state. (Because of the length of the timeout, */ - /* this transition will cause the token to be passed regardless */ - /* of the initial value of FrameCount.) */ - transition_now = true; - } else { - if (mstp_port->ReceivedInvalidFrame == true) { - /* InvalidFrame */ - /* error in frame reception */ - mstp_port->ReceivedInvalidFrame = false; - mstp_port->master_state = - MSTP_MASTER_STATE_DONE_WITH_TOKEN; + break; + /* In the IDLE state, the node waits for a frame. */ + case MSTP_MASTER_STATE_IDLE: + /* LostToken */ + if (mstp_port->SilenceTimer >= Tno_token) { + /* assume that the token has been lost */ + mstp_port->EventCount = 0; /* Addendum 135-2004d-8 */ + mstp_port->master_state = MSTP_MASTER_STATE_NO_TOKEN; transition_now = true; + } + /* ReceivedInvalidFrame */ + else if (mstp_port->ReceivedInvalidFrame == true) { + /* invalid frame was received */ + mstp_port->ReceivedInvalidFrame = false; + /* wait for the next frame - remain in IDLE */ } else if (mstp_port->ReceivedValidFrame == true) { - if (mstp_port->DestinationAddress == - mstp_port->This_Station) { - switch (mstp_port->TxFrameType) { - case FRAME_TYPE_REPLY_POSTPONED: - /* ReceivedReplyPostponed */ +#if PRINT_ENABLED_MASTER + fprintf(stderr, + "MSTP: ReceivedValidFrame Src=%02X Dest=%02X DataLen=%u FC=%u ST=%u Type=%s\n", + mstp_port->SourceAddress, + mstp_port->DestinationAddress, + mstp_port->DataLength, + mstp_port->FrameCount, + mstp_port->SilenceTimer, + mstp_frame_type_text(mstp_port->FrameType)); +#endif + /* destined for me! */ + if ((mstp_port->DestinationAddress == + mstp_port->This_Station) || + (mstp_port->DestinationAddress == + MSTP_BROADCAST_ADDRESS)) { + switch (mstp_port->FrameType) { + /* ReceivedToken */ + case FRAME_TYPE_TOKEN: + /* tokens can't be broadcast */ + if (mstp_port->DestinationAddress == + MSTP_BROADCAST_ADDRESS) + break; + mstp_port->ReceivedValidFrame = false; + mstp_port->FrameCount = 0; + mstp_port->SoleMaster = false; + mstp_port->master_state = + MSTP_MASTER_STATE_USE_TOKEN; + transition_now = true; + break; + /* ReceivedPFM */ + case FRAME_TYPE_POLL_FOR_MASTER: + /* CHEAT: we cheat a little and this is really handled in the + receive state machine since it is difficult to respond + quick enough (i.e. faster than Tusage_timeout of the + other node which could be 20ms). */ + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER, + mstp_port->SourceAddress, + mstp_port->This_Station, NULL, 0); + break; + case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: + /* indicate successful reception to the higher layers */ + dlmstp_put_receive(mstp_port->SourceAddress, + (uint8_t *) & mstp_port->InputBuffer[0], + mstp_port->DataLength); + break; + case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: + mstp_port->ReplyPostponedTimer = 0; + /* indicate successful reception to the higher layers */ + dlmstp_put_receive(mstp_port->SourceAddress, /* source MS/TP address */ + (uint8_t *) & mstp_port->InputBuffer[0], + mstp_port->DataLength); + /* broadcast DER just remains IDLE */ + if (mstp_port->DestinationAddress != + MSTP_BROADCAST_ADDRESS) { + mstp_port->master_state = + MSTP_MASTER_STATE_ANSWER_DATA_REQUEST; + transition_now = true; + } + break; + case FRAME_TYPE_TEST_REQUEST: + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_TEST_RESPONSE, + mstp_port->SourceAddress, + mstp_port->This_Station, NULL, 0); + break; + case FRAME_TYPE_TEST_RESPONSE: + default: + break; + } + } + mstp_port->ReceivedValidFrame = false; + } + break; + /* In the USE_TOKEN state, the node is allowed to send one or */ + /* more data frames. These may be BACnet Data frames or */ + /* proprietary frames. */ + case MSTP_MASTER_STATE_USE_TOKEN: + if (!mstp_port->TxReady) { + /* NothingToSend */ + mstp_port->FrameCount = mstp_port->Nmax_info_frames; + mstp_port->master_state = MSTP_MASTER_STATE_DONE_WITH_TOKEN; + } else { + uint8_t destination = mstp_port->TxBuffer[3]; + RS485_Send_Frame(mstp_port, + (uint8_t *) & mstp_port->TxBuffer[0], mstp_port->TxLength); + mstp_port->FrameCount++; + switch (mstp_port->TxFrameType) { + case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: + /* SendAndWait */ + if (destination == MSTP_BROADCAST_ADDRESS) + mstp_port->master_state = + MSTP_MASTER_STATE_DONE_WITH_TOKEN; + else + mstp_port->master_state = + MSTP_MASTER_STATE_WAIT_FOR_REPLY; + break; + case FRAME_TYPE_TEST_REQUEST: mstp_port->master_state = - MSTP_MASTER_STATE_DONE_WITH_TOKEN; + MSTP_MASTER_STATE_WAIT_FOR_REPLY; break; case FRAME_TYPE_TEST_RESPONSE: - mstp_port->master_state = MSTP_MASTER_STATE_IDLE; - break; case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: - /* ReceivedReply */ - /* or a proprietary type that indicates a reply */ - /* indicate successful reception to the higher layers */ - dlmstp_put_receive(mstp_port->SourceAddress, /* source MS/TP address */ - (uint8_t *) & mstp_port->InputBuffer[0], - mstp_port->DataLength); + default: + /* SendNoWait */ mstp_port->master_state = MSTP_MASTER_STATE_DONE_WITH_TOKEN; break; - default: - /* if proprietary frame was expected, you might - need to transition to DONE WITH TOKEN */ + } + mstp_port->TxReady = false; + } + transition_now = true; + break; + /* In the WAIT_FOR_REPLY state, the node waits for */ + /* a reply from another node. */ + case MSTP_MASTER_STATE_WAIT_FOR_REPLY: + if (mstp_port->SilenceTimer >= Treply_timeout) { + /* ReplyTimeout */ + /* assume that the request has failed */ + mstp_port->FrameCount = mstp_port->Nmax_info_frames; + mstp_port->master_state = MSTP_MASTER_STATE_DONE_WITH_TOKEN; + /* Any retry of the data frame shall await the next entry */ + /* to the USE_TOKEN state. (Because of the length of the timeout, */ + /* this transition will cause the token to be passed regardless */ + /* of the initial value of FrameCount.) */ + transition_now = true; + } else { + if (mstp_port->ReceivedInvalidFrame == true) { + /* InvalidFrame */ + /* error in frame reception */ + mstp_port->ReceivedInvalidFrame = false; + mstp_port->master_state = + MSTP_MASTER_STATE_DONE_WITH_TOKEN; + transition_now = true; + } else if (mstp_port->ReceivedValidFrame == true) { + if (mstp_port->DestinationAddress == + mstp_port->This_Station) { + switch (mstp_port->TxFrameType) { + case FRAME_TYPE_REPLY_POSTPONED: + /* ReceivedReplyPostponed */ + mstp_port->master_state = + MSTP_MASTER_STATE_DONE_WITH_TOKEN; + break; + case FRAME_TYPE_TEST_RESPONSE: + mstp_port->master_state = + MSTP_MASTER_STATE_IDLE; + break; + case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: + /* ReceivedReply */ + /* or a proprietary type that indicates a reply */ + /* indicate successful reception to the higher layers */ + dlmstp_put_receive(mstp_port->SourceAddress, /* source MS/TP address */ + (uint8_t *) & mstp_port->InputBuffer[0], + mstp_port->DataLength); + mstp_port->master_state = + MSTP_MASTER_STATE_DONE_WITH_TOKEN; + break; + default: + /* if proprietary frame was expected, you might + need to transition to DONE WITH TOKEN */ + mstp_port->master_state = + MSTP_MASTER_STATE_IDLE; + break; + } + } else { + /* ReceivedUnexpectedFrame */ + /* an unexpected frame was received */ + /* This may indicate the presence of multiple tokens. */ + /* Synchronize with the network. */ + /* This action drops the token. */ mstp_port->master_state = MSTP_MASTER_STATE_IDLE; - break; } + mstp_port->ReceivedValidFrame = false; + transition_now = true; + } + } + break; + /* The DONE_WITH_TOKEN state either sends another data frame, */ + /* passes the token, or initiates a Poll For Master cycle. */ + case MSTP_MASTER_STATE_DONE_WITH_TOKEN: + /* SendAnotherFrame */ + if (mstp_port->FrameCount < mstp_port->Nmax_info_frames) { + /* then this node may send another information frame */ + /* before passing the token. */ + mstp_port->master_state = MSTP_MASTER_STATE_USE_TOKEN; + } + /* Npoll changed in Errata SSPC-135-2004 */ + else if (mstp_port->TokenCount < (Npoll - 1)) { + if ((mstp_port->SoleMaster == true) && + (mstp_port->Next_Station != next_this_station)) { + /* SoleMaster */ + /* there are no other known master nodes to */ + /* which the token may be sent (true master-slave operation). */ + mstp_port->FrameCount = 0; + mstp_port->TokenCount++; + mstp_port->master_state = MSTP_MASTER_STATE_USE_TOKEN; + } else { + /* SendToken */ + /* Npoll changed in Errata SSPC-135-2004 */ + /* The comparison of NS and TS+1 eliminates the Poll For Master */ + /* if there are no addresses between TS and NS, since there is no */ + /* address at which a new master node may be found in that case. */ + mstp_port->TokenCount++; + /* transmit a Token frame to NS */ + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_TOKEN, + mstp_port->Next_Station, + mstp_port->This_Station, NULL, 0); + mstp_port->RetryCount = 0; + mstp_port->EventCount = 0; + mstp_port->master_state = MSTP_MASTER_STATE_PASS_TOKEN; + } + } else if (next_poll_station == mstp_port->Next_Station) { + if (mstp_port->SoleMaster == true) { + /* SoleMasterRestartMaintenancePFM */ + mstp_port->Poll_Station = next_next_station; + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_POLL_FOR_MASTER, mstp_port->Poll_Station, + mstp_port->This_Station, NULL, 0); + /* no known successor node */ + mstp_port->Next_Station = mstp_port->This_Station; + mstp_port->RetryCount = 0; + mstp_port->TokenCount = 1; /* changed in Errata SSPC-135-2004 */ + /* mstp_port->EventCount = 0; removed in Addendum 135-2004d-8 */ + /* find a new successor to TS */ + mstp_port->master_state = + MSTP_MASTER_STATE_POLL_FOR_MASTER; + } else { + /* ResetMaintenancePFM */ + mstp_port->Poll_Station = mstp_port->This_Station; + /* transmit a Token frame to NS */ + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_TOKEN, + mstp_port->Next_Station, + mstp_port->This_Station, NULL, 0); + mstp_port->RetryCount = 0; + mstp_port->TokenCount = 1; /* changed in Errata SSPC-135-2004 */ + mstp_port->EventCount = 0; + mstp_port->master_state = MSTP_MASTER_STATE_PASS_TOKEN; + } + } else { + /* SendMaintenancePFM */ + mstp_port->Poll_Station = next_poll_station; + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_POLL_FOR_MASTER, + mstp_port->Poll_Station, mstp_port->This_Station, NULL, 0); + mstp_port->RetryCount = 0; + mstp_port->master_state = MSTP_MASTER_STATE_POLL_FOR_MASTER; + } + transition_now = true; + break; + /* The PASS_TOKEN state listens for a successor to begin using */ + /* the token that this node has just attempted to pass. */ + case MSTP_MASTER_STATE_PASS_TOKEN: + if (mstp_port->SilenceTimer < Tusage_timeout) { + if (mstp_port->EventCount > Nmin_octets) { + /* SawTokenUser */ + /* Assume that a frame has been sent by the new token user. */ + /* Enter the IDLE state to process the frame. */ + mstp_port->master_state = MSTP_MASTER_STATE_IDLE; + transition_now = true; + } + } else { + if (mstp_port->RetryCount < Nretry_token) { + /* RetrySendToken */ + mstp_port->RetryCount++; + /* Transmit a Token frame to NS */ + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_TOKEN, + mstp_port->Next_Station, mstp_port->This_Station, NULL, + 0); + mstp_port->EventCount = 0; + /* re-enter the current state to listen for NS */ + /* to begin using the token. */ + } else { + /* FindNewSuccessor */ + /* Assume that NS has failed. */ + mstp_port->Poll_Station = next_next_station; + /* Transmit a Poll For Master frame to PS. */ + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_POLL_FOR_MASTER, + mstp_port->Poll_Station, mstp_port->This_Station, NULL, + 0); + /* no known successor node */ + mstp_port->Next_Station = mstp_port->This_Station; + mstp_port->RetryCount = 0; + mstp_port->TokenCount = 0; + /* mstp_port->EventCount = 0; removed in Addendum 135-2004d-8 */ + /* find a new successor to TS */ + mstp_port->master_state = + MSTP_MASTER_STATE_POLL_FOR_MASTER; + transition_now = true; + } + } + break; + /* The NO_TOKEN state is entered if mstp_port->SilenceTimer becomes greater */ + /* than Tno_token, indicating that there has been no network activity */ + /* for that period of time. The timeout is continued to determine */ + /* whether or not this node may create a token. */ + case MSTP_MASTER_STATE_NO_TOKEN: + my_timeout = Tno_token + (Tslot * mstp_port->This_Station); + if (mstp_port->SilenceTimer < my_timeout) { + if (mstp_port->EventCount > Nmin_octets) { + /* SawFrame */ + /* Some other node exists at a lower address. */ + /* Enter the IDLE state to receive and process the incoming frame. */ + mstp_port->master_state = MSTP_MASTER_STATE_IDLE; + transition_now = true; + } + } else { + ns_timeout = + Tno_token + (Tslot * (mstp_port->This_Station + 1)); + if (mstp_port->SilenceTimer < ns_timeout) { + /* GenerateToken */ + /* Assume that this node is the lowest numerical address */ + /* on the network and is empowered to create a token. */ + mstp_port->Poll_Station = next_this_station; + /* Transmit a Poll For Master frame to PS. */ + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_POLL_FOR_MASTER, + mstp_port->Poll_Station, mstp_port->This_Station, NULL, + 0); + /* indicate that the next station is unknown */ + mstp_port->Next_Station = mstp_port->This_Station; + mstp_port->RetryCount = 0; + mstp_port->TokenCount = 0; + /* mstp_port->EventCount = 0; removed Addendum 135-2004d-8 */ + /* enter the POLL_FOR_MASTER state to find a new successor to TS. */ + mstp_port->master_state = + MSTP_MASTER_STATE_POLL_FOR_MASTER; + transition_now = true; + } + } + break; + /* In the POLL_FOR_MASTER state, the node listens for a reply to */ + /* a previously sent Poll For Master frame in order to find */ + /* a successor node. */ + case MSTP_MASTER_STATE_POLL_FOR_MASTER: + if (mstp_port->ReceivedValidFrame == true) { + if ((mstp_port->DestinationAddress == mstp_port->This_Station) + && (mstp_port->FrameType == + FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER)) { + /* ReceivedReplyToPFM */ + mstp_port->SoleMaster = false; + mstp_port->Next_Station = mstp_port->SourceAddress; + mstp_port->EventCount = 0; + /* Transmit a Token frame to NS */ + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_TOKEN, + mstp_port->Next_Station, mstp_port->This_Station, NULL, + 0); + mstp_port->Poll_Station = mstp_port->This_Station; + mstp_port->TokenCount = 0; + mstp_port->RetryCount = 0; + mstp_port->master_state = MSTP_MASTER_STATE_PASS_TOKEN; } else { /* ReceivedUnexpectedFrame */ - /* an unexpected frame was received */ - /* This may indicate the presence of multiple tokens. */ - /* Synchronize with the network. */ + /* An unexpected frame was received. */ + /* This may indicate the presence of multiple tokens. */ + /* enter the IDLE state to synchronize with the network. */ /* This action drops the token. */ mstp_port->master_state = MSTP_MASTER_STATE_IDLE; } mstp_port->ReceivedValidFrame = false; transition_now = true; - } - } - break; - /* The DONE_WITH_TOKEN state either sends another data frame, */ - /* passes the token, or initiates a Poll For Master cycle. */ - case MSTP_MASTER_STATE_DONE_WITH_TOKEN: - /* SendAnotherFrame */ - if (mstp_port->FrameCount < mstp_port->Nmax_info_frames) { - /* then this node may send another information frame */ - /* before passing the token. */ - mstp_port->master_state = MSTP_MASTER_STATE_USE_TOKEN; - } - /* Npoll changed in Errata SSPC-135-2004 */ - else if (mstp_port->TokenCount < (Npoll - 1)) { - if ((mstp_port->SoleMaster == true) && - (mstp_port->Next_Station != next_this_station)) { - /* SoleMaster */ - /* there are no other known master nodes to */ - /* which the token may be sent (true master-slave operation). */ - mstp_port->FrameCount = 0; - mstp_port->TokenCount++; - mstp_port->master_state = MSTP_MASTER_STATE_USE_TOKEN; - } else { - /* SendToken */ - /* Npoll changed in Errata SSPC-135-2004 */ - /* The comparison of NS and TS+1 eliminates the Poll For Master */ - /* if there are no addresses between TS and NS, since there is no */ - /* address at which a new master node may be found in that case. */ - mstp_port->TokenCount++; - /* transmit a Token frame to NS */ - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_TOKEN, - mstp_port->Next_Station, - mstp_port->This_Station, NULL, 0); - mstp_port->RetryCount = 0; - mstp_port->EventCount = 0; - mstp_port->master_state = MSTP_MASTER_STATE_PASS_TOKEN; - } - } else if (next_poll_station == mstp_port->Next_Station) { - if (mstp_port->SoleMaster == true) { - /* SoleMasterRestartMaintenancePFM */ - mstp_port->Poll_Station = next_next_station; - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_POLL_FOR_MASTER, mstp_port->Poll_Station, - mstp_port->This_Station, NULL, 0); - /* no known successor node */ - mstp_port->Next_Station = mstp_port->This_Station; - mstp_port->RetryCount = 0; - mstp_port->TokenCount = 1; /* changed in Errata SSPC-135-2004 */ - /* mstp_port->EventCount = 0; removed in Addendum 135-2004d-8 */ - /* find a new successor to TS */ - mstp_port->master_state = - MSTP_MASTER_STATE_POLL_FOR_MASTER; - } else { - /* ResetMaintenancePFM */ - mstp_port->Poll_Station = mstp_port->This_Station; - /* transmit a Token frame to NS */ - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_TOKEN, - mstp_port->Next_Station, - mstp_port->This_Station, NULL, 0); - mstp_port->RetryCount = 0; - mstp_port->TokenCount = 1; /* changed in Errata SSPC-135-2004 */ - mstp_port->EventCount = 0; - mstp_port->master_state = MSTP_MASTER_STATE_PASS_TOKEN; - } - } else { - /* SendMaintenancePFM */ - mstp_port->Poll_Station = next_poll_station; - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_POLL_FOR_MASTER, - mstp_port->Poll_Station, mstp_port->This_Station, NULL, 0); - mstp_port->RetryCount = 0; - mstp_port->master_state = MSTP_MASTER_STATE_POLL_FOR_MASTER; - } - transition_now = true; - break; - /* The PASS_TOKEN state listens for a successor to begin using */ - /* the token that this node has just attempted to pass. */ - case MSTP_MASTER_STATE_PASS_TOKEN: - if (mstp_port->SilenceTimer < Tusage_timeout) { - if (mstp_port->EventCount > Nmin_octets) { - /* SawTokenUser */ - /* Assume that a frame has been sent by the new token user. */ - /* Enter the IDLE state to process the frame. */ - mstp_port->master_state = MSTP_MASTER_STATE_IDLE; - transition_now = true; - } - } else { - if (mstp_port->RetryCount < Nretry_token) { - /* RetrySendToken */ - mstp_port->RetryCount++; - /* Transmit a Token frame to NS */ - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_TOKEN, - mstp_port->Next_Station, mstp_port->This_Station, NULL, - 0); - mstp_port->EventCount = 0; - /* re-enter the current state to listen for NS */ - /* to begin using the token. */ - } else { - /* FindNewSuccessor */ - /* Assume that NS has failed. */ - mstp_port->Poll_Station = next_next_station; - /* Transmit a Poll For Master frame to PS. */ - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_POLL_FOR_MASTER, - mstp_port->Poll_Station, mstp_port->This_Station, NULL, - 0); - /* no known successor node */ - mstp_port->Next_Station = mstp_port->This_Station; - mstp_port->RetryCount = 0; - mstp_port->TokenCount = 0; - /* mstp_port->EventCount = 0; removed in Addendum 135-2004d-8 */ - /* find a new successor to TS */ - mstp_port->master_state = - MSTP_MASTER_STATE_POLL_FOR_MASTER; - transition_now = true; - } - } - break; - /* The NO_TOKEN state is entered if mstp_port->SilenceTimer becomes greater */ - /* than Tno_token, indicating that there has been no network activity */ - /* for that period of time. The timeout is continued to determine */ - /* whether or not this node may create a token. */ - case MSTP_MASTER_STATE_NO_TOKEN: - my_timeout = Tno_token + (Tslot * mstp_port->This_Station); - if (mstp_port->SilenceTimer < my_timeout) { - if (mstp_port->EventCount > Nmin_octets) { - /* SawFrame */ - /* Some other node exists at a lower address. */ - /* Enter the IDLE state to receive and process the incoming frame. */ - mstp_port->master_state = MSTP_MASTER_STATE_IDLE; - transition_now = true; - } - } else { - ns_timeout = - Tno_token + (Tslot * (mstp_port->This_Station + 1)); - if (mstp_port->SilenceTimer < ns_timeout) { - /* GenerateToken */ - /* Assume that this node is the lowest numerical address */ - /* on the network and is empowered to create a token. */ - mstp_port->Poll_Station = next_this_station; - /* Transmit a Poll For Master frame to PS. */ - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_POLL_FOR_MASTER, - mstp_port->Poll_Station, mstp_port->This_Station, NULL, - 0); - /* indicate that the next station is unknown */ - mstp_port->Next_Station = mstp_port->This_Station; - mstp_port->RetryCount = 0; - mstp_port->TokenCount = 0; - /* mstp_port->EventCount = 0; removed Addendum 135-2004d-8 */ - /* enter the POLL_FOR_MASTER state to find a new successor to TS. */ - mstp_port->master_state = - MSTP_MASTER_STATE_POLL_FOR_MASTER; - transition_now = true; - } - } - break; - /* In the POLL_FOR_MASTER state, the node listens for a reply to */ - /* a previously sent Poll For Master frame in order to find */ - /* a successor node. */ - case MSTP_MASTER_STATE_POLL_FOR_MASTER: - if (mstp_port->ReceivedValidFrame == true) { - if ((mstp_port->DestinationAddress == mstp_port->This_Station) - && (mstp_port->FrameType == - FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER)) { - /* ReceivedReplyToPFM */ - mstp_port->SoleMaster = false; - mstp_port->Next_Station = mstp_port->SourceAddress; - mstp_port->EventCount = 0; - /* Transmit a Token frame to NS */ - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_TOKEN, - mstp_port->Next_Station, mstp_port->This_Station, NULL, - 0); - mstp_port->Poll_Station = mstp_port->This_Station; - mstp_port->TokenCount = 0; - mstp_port->RetryCount = 0; - mstp_port->master_state = MSTP_MASTER_STATE_PASS_TOKEN; - } else { - /* ReceivedUnexpectedFrame */ - /* An unexpected frame was received. */ - /* This may indicate the presence of multiple tokens. */ - /* enter the IDLE state to synchronize with the network. */ - /* This action drops the token. */ - mstp_port->master_state = MSTP_MASTER_STATE_IDLE; - } - mstp_port->ReceivedValidFrame = false; - transition_now = true; - } else if ((mstp_port->SilenceTimer >= Tusage_timeout) || - (mstp_port->ReceivedInvalidFrame == true)) { - if (mstp_port->SoleMaster == true) { - /* SoleMaster */ - /* There was no valid reply to the periodic poll */ - /* by the sole known master for other masters. */ - mstp_port->FrameCount = 0; - /* mstp_port->TokenCount++; removed in 2004 */ - mstp_port->master_state = MSTP_MASTER_STATE_USE_TOKEN; - } else { - if (mstp_port->Next_Station != mstp_port->This_Station) { - /* DoneWithPFM */ - /* There was no valid reply to the maintenance */ - /* poll for a master at address PS. */ - mstp_port->EventCount = 0; - /* transmit a Token frame to NS */ - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_TOKEN, - mstp_port->Next_Station, mstp_port->This_Station, - NULL, 0); - mstp_port->RetryCount = 0; - mstp_port->master_state = MSTP_MASTER_STATE_PASS_TOKEN; + } else if ((mstp_port->SilenceTimer >= Tusage_timeout) || + (mstp_port->ReceivedInvalidFrame == true)) { + if (mstp_port->SoleMaster == true) { + /* SoleMaster */ + /* There was no valid reply to the periodic poll */ + /* by the sole known master for other masters. */ + mstp_port->FrameCount = 0; + /* mstp_port->TokenCount++; removed in 2004 */ + mstp_port->master_state = MSTP_MASTER_STATE_USE_TOKEN; } else { - if (next_poll_station != mstp_port->This_Station) { - /* SendNextPFM */ - mstp_port->Poll_Station = next_poll_station; - /* Transmit a Poll For Master frame to PS. */ + if (mstp_port->Next_Station != mstp_port->This_Station) { + /* DoneWithPFM */ + /* There was no valid reply to the maintenance */ + /* poll for a master at address PS. */ + mstp_port->EventCount = 0; + /* transmit a Token frame to NS */ MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_POLL_FOR_MASTER, - mstp_port->Poll_Station, - mstp_port->This_Station, NULL, 0); + FRAME_TYPE_TOKEN, + mstp_port->Next_Station, mstp_port->This_Station, + NULL, 0); mstp_port->RetryCount = 0; - /* Re-enter the current state. */ + mstp_port->master_state = MSTP_MASTER_STATE_PASS_TOKEN; } else { - /* DeclareSoleMaster */ - /* to indicate that this station is the only master */ - mstp_port->SoleMaster = true; - mstp_port->FrameCount = 0; - mstp_port->master_state = - MSTP_MASTER_STATE_USE_TOKEN; + if (next_poll_station != mstp_port->This_Station) { + /* SendNextPFM */ + mstp_port->Poll_Station = next_poll_station; + /* Transmit a Poll For Master frame to PS. */ + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_POLL_FOR_MASTER, + mstp_port->Poll_Station, + mstp_port->This_Station, NULL, 0); + mstp_port->RetryCount = 0; + /* Re-enter the current state. */ + } else { + /* DeclareSoleMaster */ + /* to indicate that this station is the only master */ + mstp_port->SoleMaster = true; + mstp_port->FrameCount = 0; + mstp_port->master_state = + MSTP_MASTER_STATE_USE_TOKEN; + } } } + mstp_port->ReceivedInvalidFrame = false; + transition_now = true; } - mstp_port->ReceivedInvalidFrame = false; - transition_now = true; - } - break; - /* The ANSWER_DATA_REQUEST state is entered when a */ - /* BACnet Data Expecting Reply, a Test_Request, or */ - /* a proprietary frame that expects a reply is received. */ - case MSTP_MASTER_STATE_ANSWER_DATA_REQUEST: + break; + /* The ANSWER_DATA_REQUEST state is entered when a */ + /* BACnet Data Expecting Reply, a Test_Request, or */ + /* a proprietary frame that expects a reply is received. */ + case MSTP_MASTER_STATE_ANSWER_DATA_REQUEST: #if 0 - if (mstp_port->ReplyPostponedTimer <= Treply_delay) { - /* FIXME: we always defer the reply to be safe */ - /* FIXME: if we knew the APDU type received, we could - see if the next message was that same APDU type - along with the matching src/dest and invoke ID */ - if ((mstp_port->FrameType == - FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY) - && (mstp_port->TxReady)) { - /* Reply */ - /* If a reply is available from the higher layers */ - /* within Treply_delay after the reception of the */ - /* final octet of the requesting frame */ - /* (the mechanism used to determine this is a local matter), */ - /* then call MSTP_Create_And_Send_Frame to transmit the reply frame */ - /* and enter the IDLE state to wait for the next frame. */ - RS485_Send_Frame(mstp_port, - (uint8_t *) & mstp_port->TxBuffer[0], - mstp_port->TxLength); - mstp_port->TxReady = false; + if (mstp_port->ReplyPostponedTimer <= Treply_delay) { + /* FIXME: we always defer the reply to be safe */ + /* FIXME: if we knew the APDU type received, we could + see if the next message was that same APDU type + along with the matching src/dest and invoke ID */ + if ((mstp_port->FrameType == + FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY) + && (mstp_port->TxReady)) { + /* Reply */ + /* If a reply is available from the higher layers */ + /* within Treply_delay after the reception of the */ + /* final octet of the requesting frame */ + /* (the mechanism used to determine this is a local matter), */ + /* then call MSTP_Create_And_Send_Frame to transmit the reply frame */ + /* and enter the IDLE state to wait for the next frame. */ + RS485_Send_Frame(mstp_port, + (uint8_t *) & mstp_port->TxBuffer[0], + mstp_port->TxLength); + mstp_port->TxReady = false; + mstp_port->master_state = MSTP_MASTER_STATE_IDLE; + transition_now = true; + } else { + /* Test Request - handled directly in IDLE state */ + } + } else +#endif + /* DeferredReply */ + /* If no reply will be available from the higher layers */ + /* within Treply_delay after the reception of the */ + /* final octet of the requesting frame (the mechanism */ + /* used to determine this is a local matter), */ + /* then an immediate reply is not possible. */ + /* Any reply shall wait until this node receives the token. */ + /* Call MSTP_Create_And_Send_Frame to transmit a Reply Postponed frame, */ + /* and enter the IDLE state. */ + { + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_REPLY_POSTPONED, + mstp_port->SourceAddress, + mstp_port->This_Station, NULL, 0); mstp_port->master_state = MSTP_MASTER_STATE_IDLE; transition_now = true; - } else { - /* Test Request - handled directly in IDLE state */ } - } else -#endif - /* DeferredReply */ - /* If no reply will be available from the higher layers */ - /* within Treply_delay after the reception of the */ - /* final octet of the requesting frame (the mechanism */ - /* used to determine this is a local matter), */ - /* then an immediate reply is not possible. */ - /* Any reply shall wait until this node receives the token. */ - /* Call MSTP_Create_And_Send_Frame to transmit a Reply Postponed frame, */ - /* and enter the IDLE state. */ - { - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_REPLY_POSTPONED, - mstp_port->SourceAddress, - mstp_port->This_Station, NULL, 0); + break; + default: mstp_port->master_state = MSTP_MASTER_STATE_IDLE; - transition_now = true; - } - break; - default: - mstp_port->master_state = MSTP_MASTER_STATE_IDLE; - break; + break; } return transition_now; } -void MSTP_Init(volatile struct mstp_port_struct_t *mstp_port, +void MSTP_Init( + volatile struct mstp_port_struct_t *mstp_port, uint8_t this_station_mac) { - int i; /*loop counter */ + int i; /*loop counter */ if (mstp_port) { mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; diff --git a/bacnet-stack/ports/rtos32/mstp.h b/bacnet-stack/ports/rtos32/mstp.h index d7c74bfd..da53b704 100644 --- a/bacnet-stack/ports/rtos32/mstp.h +++ b/bacnet-stack/ports/rtos32/mstp.h @@ -201,7 +201,7 @@ struct mstp_port_struct_t { /* This array is only used for APDU messages */ uint8_t TxBuffer[MAX_MPDU]; unsigned TxLength; - bool TxReady; /* true if ready to be sent or received */ + bool TxReady; /* true if ready to be sent or received */ uint8_t TxFrameType; /* type of message - needed by MS/TP */ }; @@ -215,30 +215,36 @@ struct mstp_port_struct_t { #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - void MSTP_Init(volatile struct mstp_port_struct_t *mstp_port, + void MSTP_Init( + volatile struct mstp_port_struct_t *mstp_port, uint8_t this_station_mac); - void MSTP_Millisecond_Timer(volatile struct mstp_port_struct_t + void MSTP_Millisecond_Timer( + volatile struct mstp_port_struct_t *mstp_port); - void MSTP_Receive_Frame_FSM(volatile struct mstp_port_struct_t + void MSTP_Receive_Frame_FSM( + volatile struct mstp_port_struct_t *mstp_port); - bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t + bool MSTP_Master_Node_FSM( + volatile struct mstp_port_struct_t *mstp_port); /* returns true if line is active */ - bool MSTP_Line_Active(volatile struct mstp_port_struct_t *mstp_port); + bool MSTP_Line_Active( + volatile struct mstp_port_struct_t *mstp_port); - unsigned MSTP_Create_Frame(uint8_t * buffer, /* where frame is loaded */ + unsigned MSTP_Create_Frame( + uint8_t * buffer, /* where frame is loaded */ unsigned buffer_len, /* amount of space available */ uint8_t frame_type, /* type of frame to send - see defines */ uint8_t destination, /* destination address */ - uint8_t source, /* source address */ - uint8_t * data, /* any data to be sent - may be null */ + uint8_t source, /* source address */ + uint8_t * data, /* any data to be sent - may be null */ unsigned data_len); /* number of bytes of data (up to 501) */ #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/ports/rtos32/netcfg.h b/bacnet-stack/ports/rtos32/netcfg.h index 48e85000..304ddea1 100644 --- a/bacnet-stack/ports/rtos32/netcfg.h +++ b/bacnet-stack/ports/rtos32/netcfg.h @@ -31,17 +31,17 @@ /* #define AUTO_IP // use xn_autoip() to get an IP address */ /* #define DHCP // if you enable this, you must also link library dhcpc.lib */ -#if defined(AUTO_IP) /* use xn_autoip() to get an IP address */ +#if defined(AUTO_IP) /* use xn_autoip() to get an IP address */ static BYTE TargetIP[] = { 0, 0, 0, 0 }; /* will be filled at run-time */ static BYTE NetMask[] = { 255, 255, 255, 0 }; static BYTE MinIP[] = { 192, 168, 0, 128 }; static BYTE MaxIP[] = { 192, 168, 0, 255 }; static BYTE DefaultGateway[] = { 192, 168, 0, 1 }; /* set to zero if not available or required */ static BYTE DNSServer[] = { 192, 168, 0, 1 }; /* ditto */ -#elif defined(DHCP) /* use DHCP */ +#elif defined(DHCP) /* use DHCP */ #include static BYTE TargetIP[] = { 0, 0, 0, 0 }; /* will be filled at run-time */ -#else /* static IP address assignment (default) */ +#else /* static IP address assignment (default) */ static BYTE TargetIP[] = { 192, 168, 0, 50 }; static BYTE NetMask[] = { 255, 255, 255, 0 }; static BYTE DefaultGateway[] = { 192, 168, 0, 1 }; /* set to zero if not available or required */ @@ -59,8 +59,8 @@ static BYTE DNSServer[] = { 192, 168, 0, 1 }; /* ditto */ /* PCI devices if you do not have a BIOS */ #define ED_IO_ADD 0x300 /* I/O address of the device */ -#define ED_IRQ 5 /* IRQ of the device */ -#define ED_MEM_ADD 0 /* Memory Window (only some devices) */ +#define ED_IRQ 5 /* IRQ of the device */ +#define ED_MEM_ADD 0 /* Memory Window (only some devices) */ /* Define function to pull in the required driver */ @@ -87,17 +87,17 @@ static BYTE DNSServer[] = { 192, 168, 0, 1 }; /* ditto */ #elif DEVICE_ID == RHINE_DEVICE #define BIND_DRIVER xn_bind_rhine #elif DEVICE_ID == AX172_DEVICE -#include /* must also link Rtusb.lib and UsbInit.cpp */ +#include /* must also link Rtusb.lib and UsbInit.cpp */ #define BIND_DRIVER xn_bind_ax172 #elif DEVICE_ID == AX772_DEVICE -#include /* must also link Rtusb.lib and UsbInit.cpp */ +#include /* must also link Rtusb.lib and UsbInit.cpp */ #define BIND_DRIVER xn_bind_ax772 #elif DEVICE_ID == PRISM_DEVICE -#include /* must also link Wlan.lib */ +#include /* must also link Wlan.lib */ #define BIND_DRIVER xn_bind_prism #elif DEVICE_ID == PRISM_PCMCIA_DEVICE #include -#include /* must also link Wlan.lib */ +#include /* must also link Wlan.lib */ #define BIND_DRIVER xn_bind_prism_pcmcia #else #error Invalid DEVICE_ID value diff --git a/bacnet-stack/ports/rtos32/rs485.c b/bacnet-stack/ports/rtos32/rs485.c index fa5dde73..7f0a035b 100644 --- a/bacnet-stack/ports/rtos32/rs485.c +++ b/bacnet-stack/ports/rtos32/rs485.c @@ -57,10 +57,13 @@ static FineTime RS485_Debug_Transmit_Timer; #endif #if PRINT_ENABLED_RS485 -void RS485_Print_Frame(int port, FineTime timer, uint8_t * buffer, /* frame to send (up to 501 bytes of data) */ +void RS485_Print_Frame( + int port, + FineTime timer, + uint8_t * buffer, /* frame to send (up to 501 bytes of data) */ uint16_t nbytes) { - uint16_t i; /* byte counter */ + uint16_t i; /* byte counter */ unsigned long duration; /* measures the time from last output to this one */ unsigned long seconds; unsigned long milliseconds; @@ -79,33 +82,36 @@ void RS485_Print_Frame(int port, FineTime timer, uint8_t * buffer, /* frame } #endif -static void RS485_Standard_Port_Settings(long port, long *pIRQ, +static void RS485_Standard_Port_Settings( + long port, + long *pIRQ, long *pBase) { switch (port) { - case COM1: - *pBase = (long) 0x3F8; - *pIRQ = 4L; - break; - case COM2: - *pBase = (long) 0x2F8; - *pIRQ = 3L; - break; - case COM3: - *pBase = (long) 0x3E8; - *pIRQ = 4L; - break; - case COM4: - *pBase = (long) 0x2E8; - *pIRQ = 3L; - break; - default: - break; + case COM1: + *pBase = (long) 0x3F8; + *pIRQ = 4L; + break; + case COM2: + *pBase = (long) 0x2F8; + *pIRQ = 3L; + break; + case COM3: + *pBase = (long) 0x3E8; + *pIRQ = 4L; + break; + case COM4: + *pBase = (long) 0x2E8; + *pIRQ = 3L; + break; + default: + break; } } -static int TestCOMPort(int Base) -{ /* base address of UART */ +static int TestCOMPort( + int Base) +{ /* base address of UART */ int i; for (i = 0; i < 256; i++) { @@ -117,11 +123,12 @@ static int TestCOMPort(int Base) return TRUE; } -static RS485_Open_Port(int port, /* COM port number - COM1 = 0 */ - long baud, /* baud rate */ - unsigned base, /* io base address */ +static RS485_Open_Port( + int port, /* COM port number - COM1 = 0 */ + long baud, /* baud rate */ + unsigned base, /* io base address */ int irq) -{ /* hardware IRQ number */ +{ /* hardware IRQ number */ if (!TestCOMPort(base)) return; @@ -148,21 +155,22 @@ static RS485_Open_Port(int port, /* COM port number - COM1 = 0 */ return; } -void RS485_Initialize(void) +void RS485_Initialize( + void) { #if PRINT_ENABLED_RS485 MarkTime(&RS485_Debug_Transmit_Timer); #endif - RS485_Standard_Port_Settings(RS485_Port, &RS485_IRQ_Number, - &RS485_Base); + RS485_Standard_Port_Settings(RS485_Port, &RS485_IRQ_Number, &RS485_Base); RS485_Open_Port(RS485_Port, RS485_Baud, RS485_Base, RS485_IRQ_Number); } -void RS485_Send_Frame(volatile struct mstp_port_struct_t *mstp_port, /* port specific data */ - uint8_t * buffer, /* frame to send (up to 501 bytes of data) */ +void RS485_Send_Frame( + volatile struct mstp_port_struct_t *mstp_port, /* port specific data */ + uint8_t * buffer, /* frame to send (up to 501 bytes of data) */ uint16_t nbytes) -{ /* number of bytes of data (up to 501) */ - bool status = true; /* return value */ +{ /* number of bytes of data (up to 501) */ + bool status = true; /* return value */ RS485_TRANSMIT_ENABLE(RS485_Port); SendBlock(RS485_Port, (char *) buffer, nbytes); @@ -184,8 +192,9 @@ void RS485_Send_Frame(volatile struct mstp_port_struct_t *mstp_port, /* port return; } -void RS485_Check_UART_Data(volatile struct mstp_port_struct_t *mstp_port) -{ /* port specific data */ +void RS485_Check_UART_Data( + volatile struct mstp_port_struct_t *mstp_port) +{ /* port specific data */ COMData com_data = 0; /* byte from COM driver */ unsigned timeout = 1; /* milliseconds to wait for a character */ static Duration ticks = 0; /* duration to wait for data */ diff --git a/bacnet-stack/ports/rtos32/rs485.h b/bacnet-stack/ports/rtos32/rs485.h index 1f0c18db..40a3fd3b 100644 --- a/bacnet-stack/ports/rtos32/rs485.h +++ b/bacnet-stack/ports/rtos32/rs485.h @@ -41,17 +41,20 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - void RS485_Initialize(void); + void RS485_Initialize( + void); - void RS485_Send_Frame(volatile struct mstp_port_struct_t *mstp_port, /* port specific data */ + void RS485_Send_Frame( + volatile struct mstp_port_struct_t *mstp_port, /* port specific data */ uint8_t * buffer, /* frame to send (up to 501 bytes of data) */ uint16_t nbytes); /* number of bytes of data (up to 501) */ - void RS485_Check_UART_Data(volatile struct mstp_port_struct_t *mstp_port); /* port specific data */ + void RS485_Check_UART_Data( + volatile struct mstp_port_struct_t *mstp_port); /* port specific data */ #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/ports/rtos32/stdint.h b/bacnet-stack/ports/rtos32/stdint.h index 9c003d0a..c094e202 100644 --- a/bacnet-stack/ports/rtos32/stdint.h +++ b/bacnet-stack/ports/rtos32/stdint.h @@ -16,4 +16,4 @@ typedef signed long int32_t; /* 4 bytes -2147483647 to 2147483647 */ /* typedef signed long long int64_t; */ /* typedef unsigned long long uint64_t; */ -#endif /* STDINT_H */ +#endif /* STDINT_H */ diff --git a/bacnet-stack/ports/win32/bip-init.c b/bacnet-stack/ports/win32/bip-init.c index 4bfb79a9..98e79eae 100644 --- a/bacnet-stack/ports/win32/bip-init.c +++ b/bacnet-stack/ports/win32/bip-init.c @@ -34,15 +34,16 @@ #include #include -#include /* for standard integer types uint8_t etc. */ -#include /* for the standard bool type. */ +#include /* for standard integer types uint8_t etc. */ +#include /* for the standard bool type. */ #include "bacdcode.h" #include "bip.h" #include "net.h" bool BIP_Debug = false; /* To fill a need, we invent the gethostaddr() function. */ -static long gethostaddr(void) +static long gethostaddr( + void) { struct hostent *host_ent; char host_name[255]; @@ -54,16 +55,17 @@ static long gethostaddr(void) return -1; if (BIP_Debug) { printf("host: %s at %u.%u.%u.%u\n", host_name, - ((uint8_t*)host_ent->h_addr)[0], - ((uint8_t*)host_ent->h_addr)[1], - ((uint8_t*)host_ent->h_addr)[2], - ((uint8_t*)host_ent->h_addr)[3]); + ((uint8_t *) host_ent->h_addr)[0], + ((uint8_t *) host_ent->h_addr)[1], + ((uint8_t *) host_ent->h_addr)[2], + ((uint8_t *) host_ent->h_addr)[3]); } /* note: network byte order */ return *(long *) host_ent->h_addr; } -static void set_broadcast_address(uint32_t net_address) +static void set_broadcast_address( + uint32_t net_address) { #if USE_INADDR /* Note: sometimes INADDR_BROADCAST does not let me get @@ -91,14 +93,16 @@ static void set_broadcast_address(uint32_t net_address) #endif } -static void cleanup(void) +static void cleanup( + void) { WSACleanup(); } /* on Windows, ifname is the dotted ip address of the interface */ -void bip_set_interface(char *ifname) +void bip_set_interface( + char *ifname) { struct in_addr address; @@ -117,7 +121,8 @@ void bip_set_interface(char *ifname) } } -static char *winsock_error_code_text(int code) +static char *winsock_error_code_text( + int code) { switch (code) { case WSAEACCES: @@ -135,7 +140,8 @@ static char *winsock_error_code_text(int code) case WSAEWOULDBLOCK: return "Operation would block."; case WSAEINPROGRESS: - return "Operation now in progress. This error is returned if any Windows Sockets API function is called while a blocking function is in progress."; + return + "Operation now in progress. This error is returned if any Windows Sockets API function is called while a blocking function is in progress."; case WSAENOTSOCK: return "Socket operation on nonsocket."; case WSAEDESTADDRREQ: @@ -161,7 +167,8 @@ static char *winsock_error_code_text(int code) case WSAEADDRNOTAVAIL: return "Cannot assign requested address."; case WSAENETDOWN: - return "Network is down. This error may be reported at any time if the Windows Sockets implementation detects an underlying failure."; + return + "Network is down. This error may be reported at any time if the Windows Sockets implementation detects an underlying failure."; case WSAENETUNREACH: return "Network is unreachable."; case WSAENETRESET: @@ -194,41 +201,43 @@ static char *winsock_error_code_text(int code) return "No route to host."; case WSASYSNOTREADY: return "Returned by WSAStartup(), " - "indicating that the network subsystem is unusable."; + "indicating that the network subsystem is unusable."; case WSAVERNOTSUPPORTED: return "Returned by WSAStartup(), " - "indicating that the Windows Sockets DLL cannot support " - "this application."; + "indicating that the Windows Sockets DLL cannot support " + "this application."; case WSANOTINITIALISED: return "Winsock not initialized. " - "This message is returned by any function except WSAStartup(), " - "indicating that a successful WSAStartup() has not yet " - "been performed."; + "This message is returned by any function except WSAStartup(), " + "indicating that a successful WSAStartup() has not yet " + "been performed."; case WSAEDISCON: return "Disconnect."; case WSAHOST_NOT_FOUND: return "Host not found. " - "This message indicates that the key " - "(name, address, and so on) was not found."; + "This message indicates that the key " + "(name, address, and so on) was not found."; case WSATRY_AGAIN: return "Nonauthoritative host not found. " - "This error may suggest that the name service itself " - "is not functioning."; + "This error may suggest that the name service itself " + "is not functioning."; case WSANO_RECOVERY: return "Nonrecoverable error. " - "This error may suggest that the name service itself " - "is not functioning."; + "This error may suggest that the name service itself " + "is not functioning."; case WSANO_DATA: return "Valid name, no data record of requested type. " - "This error indicates that the key " - "(name, address, and so on) was not found."; - default: return "unknown"; + "This error indicates that the key " + "(name, address, and so on) was not found."; + default: + return "unknown"; } } -bool bip_init(char *ifname) +bool bip_init( + char *ifname) { - int rv = 0; /* return from socket lib calls */ + int rv = 0; /* return from socket lib calls */ struct sockaddr_in sin = { -1 }; int value = 1; int sock_fd = -1; @@ -243,8 +252,7 @@ bool bip_init(char *ifname) if (Result != 0) { Code = WSAGetLastError(); printf("TCP/IP stack initialization failed\n" - " error code: %i %s\n", - Code, winsock_error_code_text(Code)); + " error code: %i %s\n", Code, winsock_error_code_text(Code)); exit(1); } atexit(cleanup); @@ -258,8 +266,7 @@ bool bip_init(char *ifname) if (address.s_addr == (unsigned) -1) { Code = WSAGetLastError(); printf("Get host address failed\n" - " error code: %i %s\n", - Code, winsock_error_code_text(Code)); + " error code: %i %s\n", Code, winsock_error_code_text(Code)); exit(1); } bip_set_addr(address.s_addr); @@ -276,8 +283,7 @@ bool bip_init(char *ifname) fprintf(stderr, "IP Broadcast Address: %s\n", inet_ntoa(broadcast_address)); fprintf(stderr, "UDP Port: 0x%04X [%hu]\n", - bip_get_port(), - bip_get_port()); + bip_get_port(), bip_get_port()); } /* assumes that the driver has already been initialized */ sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); diff --git a/bacnet-stack/ports/win32/dlmstp.c b/bacnet-stack/ports/win32/dlmstp.c index 3b4f8218..5b77a9ad 100644 --- a/bacnet-stack/ports/win32/dlmstp.c +++ b/bacnet-stack/ports/win32/dlmstp.c @@ -58,21 +58,25 @@ static uint8_t RxBuffer[MAX_MPDU]; /* Timer that indicates line silence - and functions */ static uint16_t SilenceTime; #define INCREMENT_AND_LIMIT_UINT16(x) {if (x < 0xFFFF) x++;} -static uint16_t Timer_Silence(void) +static uint16_t Timer_Silence( + void) { return SilenceTime; } -static void Timer_Silence_Reset(void) +static void Timer_Silence_Reset( + void) { SilenceTime = 0; } -void dlmstp_millisecond_timer(void) +void dlmstp_millisecond_timer( + void) { INCREMENT_AND_LIMIT_UINT16(SilenceTime); } -void dlmstp_reinit(void) +void dlmstp_reinit( + void) { /*RS485_Reinit(); */ dlmstp_set_mac_address(DEFAULT_MAC_ADDRESS); @@ -80,7 +84,8 @@ void dlmstp_reinit(void) dlmstp_set_max_master(DEFAULT_MAX_MASTER); } -void dlmstp_cleanup(void) +void dlmstp_cleanup( + void) { /* nothing to do for static buffers */ if (Received_Frame_Flag) { @@ -92,11 +97,12 @@ void dlmstp_cleanup(void) } /* returns number of bytes sent on success, zero on failure */ -int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */ +int dlmstp_send_pdu( + BACNET_ADDRESS * dest, /* destination address */ BACNET_NPDU_DATA * npdu_data, /* network information */ - uint8_t * pdu, /* any data to be sent - may be null */ + uint8_t * pdu, /* any data to be sent - may be null */ unsigned pdu_len) -{ /* number of bytes of data */ +{ /* number of bytes of data */ int bytes_sent = 0; unsigned i = 0; @@ -121,18 +127,18 @@ int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */ } uint16_t dlmstp_receive( - BACNET_ADDRESS * src, /* source address */ - uint8_t * pdu, /* PDU data */ - uint16_t max_pdu, /* amount of space available in the PDU */ - unsigned timeout) /* milliseconds to wait for a packet */ -{ + BACNET_ADDRESS * src, /* source address */ + uint8_t * pdu, /* PDU data */ + uint16_t max_pdu, /* amount of space available in the PDU */ + unsigned timeout) +{ /* milliseconds to wait for a packet */ uint16_t pdu_len = 0; DWORD wait_status = 0; - (void)max_pdu; + (void) max_pdu; /* see if there is a packet available, and a place to put the reply (if necessary) and process it */ - wait_status = WaitForSingleObject(Receive_Packet_Flag,timeout); + wait_status = WaitForSingleObject(Receive_Packet_Flag, timeout); if (wait_status == WAIT_OBJECT_0) { if (Receive_Packet.ready) { if (Receive_Packet.pdu_len) { @@ -144,8 +150,7 @@ uint16_t dlmstp_receive( } if (pdu) { memmove(pdu, - &Receive_Packet.pdu, - sizeof(Receive_Packet.pdu)); + &Receive_Packet.pdu, sizeof(Receive_Packet.pdu)); } pdu_len = Receive_Packet.pdu_len; } @@ -156,12 +161,14 @@ uint16_t dlmstp_receive( return pdu_len; } -static void dlmstp_receive_fsm_task(void *pArg) +static void dlmstp_receive_fsm_task( + void *pArg) { bool received_frame; - (void)pArg; - (void)SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); + (void) pArg; + (void) SetThreadPriority(GetCurrentThread(), + THREAD_PRIORITY_TIME_CRITICAL); while (TRUE) { /* only do receive state machine while we don't have a frame */ if ((MSTP_Port.ReceivedValidFrame == false) && @@ -180,12 +187,14 @@ static void dlmstp_receive_fsm_task(void *pArg) } } -static void dlmstp_master_fsm_task(void *pArg) +static void dlmstp_master_fsm_task( + void *pArg) { DWORD dwMilliseconds = 0; - (void)pArg; - (void)SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); + (void) pArg; + (void) SetThreadPriority(GetCurrentThread(), + THREAD_PRIORITY_TIME_CRITICAL); while (TRUE) { switch (MSTP_Port.master_state) { case MSTP_MASTER_STATE_IDLE: @@ -202,22 +211,26 @@ static void dlmstp_master_fsm_task(void *pArg) break; } if (dwMilliseconds) - WaitForSingleObject(Received_Frame_Flag,dwMilliseconds); + WaitForSingleObject(Received_Frame_Flag, dwMilliseconds); MSTP_Master_Node_FSM(&MSTP_Port); } } -static void dlmstp_millisecond_task(void *pArg) +static void dlmstp_millisecond_task( + void *pArg) { - (void)pArg; - (void)SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); + (void) pArg; + (void) SetThreadPriority(GetCurrentThread(), + THREAD_PRIORITY_TIME_CRITICAL); for (;;) { dlmstp_millisecond_timer(); Sleep(1); } } -void dlmstp_fill_bacnet_address(BACNET_ADDRESS * src, uint8_t mstp_address) +void dlmstp_fill_bacnet_address( + BACNET_ADDRESS * src, + uint8_t mstp_address) { int i = 0; @@ -242,7 +255,7 @@ void dlmstp_fill_bacnet_address(BACNET_ADDRESS * src, uint8_t mstp_address) /* for the MS/TP state machine to use for putting received data */ uint16_t MSTP_Put_Receive( - volatile struct mstp_port_struct_t *mstp_port) + volatile struct mstp_port_struct_t *mstp_port) { uint16_t pdu_len = 0; BOOL rc; @@ -252,9 +265,8 @@ uint16_t MSTP_Put_Receive( pdu_len = mstp_port->DataLength; if (pdu_len > sizeof(Receive_Packet.pdu)) pdu_len = sizeof(Receive_Packet.pdu); - memmove((void *) & Receive_Packet.pdu[0], - (void *) & mstp_port->InputBuffer[0], - pdu_len); + memmove((void *) &Receive_Packet.pdu[0], + (void *) &mstp_port->InputBuffer[0], pdu_len); dlmstp_fill_bacnet_address(&Receive_Packet.address, mstp_port->SourceAddress); Receive_Packet.pdu_len = mstp_port->DataLength; @@ -268,13 +280,13 @@ uint16_t MSTP_Put_Receive( /* for the MS/TP state machine to use for getting data to send */ /* Return: amount of PDU data */ uint16_t MSTP_Get_Send( - volatile struct mstp_port_struct_t *mstp_port, - unsigned timeout) /* milliseconds to wait for a packet */ -{ + volatile struct mstp_port_struct_t * mstp_port, + unsigned timeout) +{ /* milliseconds to wait for a packet */ uint16_t pdu_len = 0; uint8_t destination = 0; /* destination address */ - (void)timeout; + (void) timeout; if (!Transmit_Packet.ready) { return 0; } @@ -288,26 +300,24 @@ uint16_t MSTP_Get_Send( return 0; } /* convert the PDU into the MSTP Frame */ - pdu_len = MSTP_Create_Frame( - &mstp_port->OutputBuffer[0], /* <-- loading this */ + pdu_len = MSTP_Create_Frame(&mstp_port->OutputBuffer[0], /* <-- loading this */ mstp_port->OutputBufferSize, Transmit_Packet.frame_type, destination, mstp_port->This_Station, - &Transmit_Packet.pdu[0], - Transmit_Packet.pdu_len); + &Transmit_Packet.pdu[0], Transmit_Packet.pdu_len); Transmit_Packet.ready = false; return pdu_len; } bool dlmstp_compare_data_expecting_reply( - uint8_t *request_pdu, + uint8_t * request_pdu, uint16_t request_pdu_len, uint8_t src_address, - uint8_t *reply_pdu, + uint8_t * reply_pdu, uint16_t reply_pdu_len, - BACNET_ADDRESS *dest_address) + BACNET_ADDRESS * dest_address) { uint16_t offset; /* One way to check the message is to compare NPDU @@ -335,12 +345,12 @@ bool dlmstp_compare_data_expecting_reply( if (request.pdu_type != PDU_TYPE_CONFIRMED_SERVICE_REQUEST) { return false; } - request.invoke_id = request_pdu[offset+2]; + request.invoke_id = request_pdu[offset + 2]; /* segmented message? */ if (request_pdu[offset] & BIT3) - request.service_choice = request_pdu[offset+5]; + request.service_choice = request_pdu[offset + 5]; else - request.service_choice = request_pdu[offset+3]; + request.service_choice = request_pdu[offset + 3]; /* decode the reply data */ bacnet_address_copy(&reply.address, dest_address); offset = npdu_decode(&reply_pdu[0], @@ -353,32 +363,32 @@ bool dlmstp_compare_data_expecting_reply( reply.pdu_type = reply_pdu[offset] & 0xF0; switch (reply.pdu_type) { case PDU_TYPE_CONFIRMED_SERVICE_REQUEST: - reply.invoke_id = reply_pdu[offset+2]; + reply.invoke_id = reply_pdu[offset + 2]; /* segmented message? */ if (reply_pdu[offset] & BIT3) - reply.service_choice = reply_pdu[offset+5]; + reply.service_choice = reply_pdu[offset + 5]; else - reply.service_choice = reply_pdu[offset+3]; + reply.service_choice = reply_pdu[offset + 3]; break; case PDU_TYPE_SIMPLE_ACK: - reply.invoke_id = reply_pdu[offset+1]; - reply.service_choice = reply_pdu[offset+2]; + reply.invoke_id = reply_pdu[offset + 1]; + reply.service_choice = reply_pdu[offset + 2]; break; case PDU_TYPE_COMPLEX_ACK: - reply.invoke_id = reply_pdu[offset+1]; + reply.invoke_id = reply_pdu[offset + 1]; /* segmented message? */ if (reply_pdu[offset] & BIT3) - reply.service_choice = reply_pdu[offset+4]; + reply.service_choice = reply_pdu[offset + 4]; else - reply.service_choice = reply_pdu[offset+2]; + reply.service_choice = reply_pdu[offset + 2]; break; case PDU_TYPE_ERROR: - reply.invoke_id = reply_pdu[offset+1]; - reply.service_choice = reply_pdu[offset+2]; + reply.invoke_id = reply_pdu[offset + 1]; + reply.service_choice = reply_pdu[offset + 2]; break; case PDU_TYPE_REJECT: case PDU_TYPE_ABORT: - reply.invoke_id = reply_pdu[offset+1]; + reply.invoke_id = reply_pdu[offset + 1]; break; default: return false; @@ -412,14 +422,14 @@ bool dlmstp_compare_data_expecting_reply( /* Get the reply to a DATA_EXPECTING_REPLY frame, or nothing */ uint16_t MSTP_Get_Reply( - volatile struct mstp_port_struct_t *mstp_port, - unsigned timeout) /* milliseconds to wait for a packet */ -{ - uint16_t pdu_len = 0; /* return value */ + volatile struct mstp_port_struct_t * mstp_port, + unsigned timeout) +{ /* milliseconds to wait for a packet */ + uint16_t pdu_len = 0; /* return value */ uint8_t destination = 0; /* destination address */ bool matched = false; - (void)timeout; + (void) timeout; if (!Transmit_Packet.ready) { return 0; } @@ -433,29 +443,26 @@ uint16_t MSTP_Get_Reply( return 0; } /* is this the reply to the DER? */ - matched = dlmstp_compare_data_expecting_reply( - &mstp_port->InputBuffer[0], + matched = dlmstp_compare_data_expecting_reply(&mstp_port->InputBuffer[0], mstp_port->DataLength, mstp_port->SourceAddress, &Transmit_Packet.pdu[0], - Transmit_Packet.pdu_len, - &Transmit_Packet.address); + Transmit_Packet.pdu_len, &Transmit_Packet.address); if (!matched) return 0; /* convert the PDU into the MSTP Frame */ - pdu_len = MSTP_Create_Frame( - &mstp_port->OutputBuffer[0], /* <-- loading this */ + pdu_len = MSTP_Create_Frame(&mstp_port->OutputBuffer[0], /* <-- loading this */ mstp_port->OutputBufferSize, Transmit_Packet.frame_type, destination, mstp_port->This_Station, - &Transmit_Packet.pdu[0], - Transmit_Packet.pdu_len); + &Transmit_Packet.pdu[0], Transmit_Packet.pdu_len); Transmit_Packet.ready = false; return pdu_len; } -void dlmstp_set_mac_address(uint8_t mac_address) +void dlmstp_set_mac_address( + uint8_t mac_address) { /* Master Nodes can only have address 0-127 */ if (mac_address <= 127) { @@ -472,7 +479,8 @@ void dlmstp_set_mac_address(uint8_t mac_address) return; } -uint8_t dlmstp_mac_address(void) +uint8_t dlmstp_mac_address( + void) { return MSTP_Port.This_Station; } @@ -484,7 +492,8 @@ uint8_t dlmstp_mac_address(void) /* nodes. This may be used to allocate more or less of the available link */ /* bandwidth to particular nodes. If Max_Info_Frames is not writable in a */ /* node, its value shall be 1. */ -void dlmstp_set_max_info_frames(uint8_t max_info_frames) +void dlmstp_set_max_info_frames( + uint8_t max_info_frames) { if (max_info_frames >= 1) { MSTP_Port.Nmax_info_frames = max_info_frames; @@ -498,7 +507,8 @@ void dlmstp_set_max_info_frames(uint8_t max_info_frames) return; } -uint8_t dlmstp_max_info_frames(void) +uint8_t dlmstp_max_info_frames( + void) { return MSTP_Port.Nmax_info_frames; } @@ -508,7 +518,8 @@ uint8_t dlmstp_max_info_frames(void) /* allowable address for master nodes. The value of Max_Master shall be */ /* less than or equal to 127. If Max_Master is not writable in a node, */ /* its value shall be 127. */ -void dlmstp_set_max_master(uint8_t max_master) +void dlmstp_set_max_master( + uint8_t max_master) { if (max_master <= 127) { if (MSTP_Port.This_Station <= max_master) { @@ -524,25 +535,29 @@ void dlmstp_set_max_master(uint8_t max_master) return; } -uint8_t dlmstp_max_master(void) +uint8_t dlmstp_max_master( + void) { return MSTP_Port.Nmax_master; } /* RS485 Baud Rate 9600, 19200, 38400, 57600, 115200 */ -void dlmstp_set_baud_rate(uint32_t baud) +void dlmstp_set_baud_rate( + uint32_t baud) { RS485_Set_Baud_Rate(baud); } -uint32_t dlmstp_baud_rate(void) +uint32_t dlmstp_baud_rate( + void) { - return RS485_Get_Baud_Rate(); + return RS485_Get_Baud_Rate(); } -void dlmstp_get_my_address(BACNET_ADDRESS * my_address) +void dlmstp_get_my_address( + BACNET_ADDRESS * my_address) { - int i = 0; /* counter */ + int i = 0; /* counter */ my_address->mac_len = 1; my_address->mac[0] = MSTP_Port.This_Station; @@ -555,15 +570,16 @@ void dlmstp_get_my_address(BACNET_ADDRESS * my_address) return; } -void dlmstp_get_broadcast_address(BACNET_ADDRESS * dest) -{ /* destination address */ - int i = 0; /* counter */ +void dlmstp_get_broadcast_address( + BACNET_ADDRESS * dest) +{ /* destination address */ + int i = 0; /* counter */ if (dest) { dest->mac_len = 1; dest->mac[0] = MSTP_BROADCAST_ADDRESS; dest->net = BACNET_BROADCAST_NETWORK; - dest->len = 0; /* always zero when DNET is broadcast */ + dest->len = 0; /* always zero when DNET is broadcast */ for (i = 0; i < MAX_MAC_LEN; i++) { dest->adr[i] = 0; } @@ -572,7 +588,8 @@ void dlmstp_get_broadcast_address(BACNET_ADDRESS * dest) return; } -bool dlmstp_init(char *ifname) +bool dlmstp_init( + char *ifname) { unsigned long hThread = 0; uint32_t arg_value = 0; @@ -580,10 +597,10 @@ bool dlmstp_init(char *ifname) /* initialize packet queue */ Receive_Packet.ready = false; Receive_Packet.pdu_len = 0; - Receive_Packet_Flag = CreateSemaphore (NULL,0,1,"dlmstpReceivePacket"); + Receive_Packet_Flag = CreateSemaphore(NULL, 0, 1, "dlmstpReceivePacket"); if (Receive_Packet_Flag == NULL) exit(1); - Received_Frame_Flag = CreateSemaphore (NULL,0,1,"dlsmtpReceiveFrame"); + Received_Frame_Flag = CreateSemaphore(NULL, 0, 1, "dlsmtpReceiveFrame"); if (Received_Frame_Flag == NULL) { CloseHandle(Receive_Packet_Flag); exit(1); @@ -593,7 +610,7 @@ bool dlmstp_init(char *ifname) if (ifname) { RS485_Set_Interface(ifname); #if PRINT_ENABLED - fprintf(stderr,"MS/TP Interface: %s\n", ifname); + fprintf(stderr, "MS/TP Interface: %s\n", ifname); #endif } RS485_Initialize(); @@ -608,17 +625,17 @@ bool dlmstp_init(char *ifname) uint8_t data; /* FIXME: implement your data storage */ - data = 64; /* I2C_Read_Byte( - EEPROM_DEVICE_ADDRESS, - EEPROM_MSTP_MAC_ADDR); */ + data = 64; /* I2C_Read_Byte( + EEPROM_DEVICE_ADDRESS, + EEPROM_MSTP_MAC_ADDR); */ if (data <= 127) MSTP_Port.This_Station = data; else dlmstp_set_my_address(DEFAULT_MAC_ADDRESS); /* FIXME: implement your data storage */ - data = 127; /* I2C_Read_Byte( - EEPROM_DEVICE_ADDRESS, - EEPROM_MSTP_MAX_MASTER_ADDR); */ + data = 127; /* I2C_Read_Byte( + EEPROM_DEVICE_ADDRESS, + EEPROM_MSTP_MAX_MASTER_ADDR); */ if ((data <= 127) && (data >= MSTP_Port.This_Station)) MSTP_Port.Nmax_master = data; else @@ -634,23 +651,20 @@ bool dlmstp_init(char *ifname) dlmstp_set_max_info_frames(DEFAULT_MAX_INFO_FRAMES); #endif #if PRINT_ENABLED - fprintf(stderr,"MS/TP MAC: %02X\n", - MSTP_Port.This_Station); - fprintf(stderr,"MS/TP Max_Master: %02X\n", - MSTP_Port.Nmax_master); - fprintf(stderr,"MS/TP Max_Info_Frames: %u\n", - MSTP_Port.Nmax_info_frames); + fprintf(stderr, "MS/TP MAC: %02X\n", MSTP_Port.This_Station); + fprintf(stderr, "MS/TP Max_Master: %02X\n", MSTP_Port.Nmax_master); + fprintf(stderr, "MS/TP Max_Info_Frames: %u\n", MSTP_Port.Nmax_info_frames); #endif /* start the threads */ - hThread = _beginthread(dlmstp_millisecond_task,4096,&arg_value); + hThread = _beginthread(dlmstp_millisecond_task, 4096, &arg_value); if (hThread == 0) { fprintf(stderr, "Failed to start timer task\n"); } - hThread = _beginthread(dlmstp_receive_fsm_task,4096,&arg_value); + hThread = _beginthread(dlmstp_receive_fsm_task, 4096, &arg_value); if (hThread == 0) { fprintf(stderr, "Failed to start recive FSM task\n"); } - hThread = _beginthread(dlmstp_master_fsm_task,4096,&arg_value); + hThread = _beginthread(dlmstp_master_fsm_task, 4096, &arg_value); if (hThread == 0) { fprintf(stderr, "Failed to start Master Node FSM task\n"); } @@ -661,26 +675,26 @@ bool dlmstp_init(char *ifname) #ifdef TEST_DLMSTP #include -void apdu_handler(BACNET_ADDRESS * src, /* source address */ - uint8_t * apdu, /* APDU data */ - uint16_t pdu_len) /* for confirmed messages */ -{ - (void)src; - (void)apdu; - (void)pdu_len; +void apdu_handler( + BACNET_ADDRESS * src, /* source address */ + uint8_t * apdu, /* APDU data */ + uint16_t pdu_len) +{ /* for confirmed messages */ + (void) src; + (void) apdu; + (void) pdu_len; } /* returns a delta timestamp */ -uint32_t timestamp_ms(void) +uint32_t timestamp_ms( + void) { DWORD ticks = 0, delta_ticks = 0; static DWORD last_ticks = 0; ticks = GetTickCount(); delta_ticks = - (ticks >= last_ticks ? - ticks - last_ticks : - MAXDWORD - last_ticks); + (ticks >= last_ticks ? ticks - last_ticks : MAXDWORD - last_ticks); last_ticks = ticks; return delta_ticks; @@ -688,7 +702,9 @@ uint32_t timestamp_ms(void) static char *Network_Interface = NULL; -int main(int argc, char *argv[]) +int main( + int argc, + char *argv[]) { uint16_t pdu_len = 0; @@ -703,14 +719,11 @@ int main(int argc, char *argv[]) dlmstp_init(Network_Interface); /* forever task */ for (;;) { - pdu_len = dlmstp_receive(NULL,NULL,0,INFINITE); + pdu_len = dlmstp_receive(NULL, NULL, 0, INFINITE); #if 0 - MSTP_Create_And_Send_Frame( - &MSTP_Port, + MSTP_Create_And_Send_Frame(&MSTP_Port, FRAME_TYPE_TEST_REQUEST, - MSTP_Port.SourceAddress, - MSTP_Port.This_Station, - NULL, 0); + MSTP_Port.SourceAddress, MSTP_Port.This_Station, NULL, 0); #endif } diff --git a/bacnet-stack/ports/win32/ethernet.c b/bacnet-stack/ports/win32/ethernet.c index aee013da..9a4f339c 100644 --- a/bacnet-stack/ports/win32/ethernet.c +++ b/bacnet-stack/ports/win32/ethernet.c @@ -32,8 +32,8 @@ ------------------------------------------- ####COPYRIGHTEND####*/ -#include /* for standard integer types uint8_t etc. */ -#include /* for the standard bool type. */ +#include /* for standard integer types uint8_t etc. */ +#include /* for the standard bool type. */ #include #include #include @@ -78,19 +78,24 @@ static unsigned eth_timeout = 100; /* couple of external func for runtime error logging, you can simply */ /* replace them with standard "printf(...)" */ /* Logging extern functions: Info level */ -extern void LogInfo(const char *msg); +extern void LogInfo( + const char *msg); /* Logging extern functions: Error level*/ -extern void LogError(const char *msg); +extern void LogError( + const char *msg); /* Logging extern functions: Debug level*/ -extern void LogDebug(const char *msg); +extern void LogDebug( + const char *msg); -bool ethernet_valid(void) +bool ethernet_valid( + void) { return (pcap_eth802_fp != NULL); } -void ethernet_cleanup(void) +void ethernet_cleanup( + void) { if (pcap_eth802_fp) { pcap_close(pcap_eth802_fp); @@ -99,7 +104,8 @@ void ethernet_cleanup(void) LogInfo("ethernet.c: ethernet_cleanup() ok.\n"); } -void ethernet_set_timeout(unsigned timeout) +void ethernet_set_timeout( + unsigned timeout) { eth_timeout = timeout; } @@ -129,7 +135,8 @@ int setNonblocking(int fd) } */ -bool ethernet_init(char *if_name) +bool ethernet_init( + char *if_name) { PPACKET_OID_DATA pOidData; LPADAPTER lpAdapter; @@ -197,11 +204,11 @@ bool ethernet_init(char *if_name) */ /* Open the output device */ pcap_eth802_fp = pcap_open(if_name, /* name of the device */ - MAX_MPDU, /* portion of the packet to capture */ + MAX_MPDU, /* portion of the packet to capture */ PCAP_OPENFLAG_PROMISCUOUS, /* promiscuous mode */ - eth_timeout, /* read timeout */ - NULL, /* authentication on the remote machine */ - pcap_errbuf /* error buffer */ + eth_timeout, /* read timeout */ + NULL, /* authentication on the remote machine */ + pcap_errbuf /* error buffer */ ); if (pcap_eth802_fp == NULL) { PacketCloseAdapter(lpAdapter); @@ -222,10 +229,11 @@ bool ethernet_init(char *if_name) /* function to send a packet out the 802.2 socket */ /* returns bytes sent success, negative on failure */ -int ethernet_send(BACNET_ADDRESS * dest, /* destination address */ +int ethernet_send( + BACNET_ADDRESS * dest, /* destination address */ BACNET_ADDRESS * src, /* source address */ - uint8_t * pdu, /* any data to be sent - may be null */ - unsigned pdu_len /* number of bytes of data */ + uint8_t * pdu, /* any data to be sent - may be null */ + unsigned pdu_len /* number of bytes of data */ ) { int bytes = 0; @@ -235,8 +243,7 @@ int ethernet_send(BACNET_ADDRESS * dest, /* destination address */ /* don't waste time if the socket is not valid */ if (!ethernet_valid()) { - LogError - ("ethernet.c: invalid 802.2 ethernet interface descriptor!\n"); + LogError("ethernet.c: invalid 802.2 ethernet interface descriptor!\n"); return -1; } /* load destination ethernet MAC address */ @@ -265,8 +272,7 @@ int ethernet_send(BACNET_ADDRESS * dest, /* destination address */ return -4; } /* packet length */ - mtu_len += - encode_unsigned16(&mtu[12], 3 /*DSAP,SSAP,LLC */ + pdu_len); + mtu_len += encode_unsigned16(&mtu[12], 3 /*DSAP,SSAP,LLC */ + pdu_len); /* Logical PDU portion */ mtu[mtu_len++] = 0x82; /* DSAP for BACnet */ mtu[mtu_len++] = 0x82; /* SSAP for BACnet */ @@ -290,12 +296,13 @@ int ethernet_send(BACNET_ADDRESS * dest, /* destination address */ /* function to send a packet out the 802.2 socket */ /* returns number of bytes sent on success, negative on failure */ -int ethernet_send_pdu(BACNET_ADDRESS * dest, /* destination address */ - uint8_t * pdu, /* any data to be sent - may be null */ - unsigned pdu_len /* number of bytes of data */ +int ethernet_send_pdu( + BACNET_ADDRESS * dest, /* destination address */ + uint8_t * pdu, /* any data to be sent - may be null */ + unsigned pdu_len /* number of bytes of data */ ) { - int i = 0; /* counter */ + int i = 0; /* counter */ BACNET_ADDRESS src = { 0 }; /* source address */ for (i = 0; i < 6; i++) { @@ -305,18 +312,19 @@ int ethernet_send_pdu(BACNET_ADDRESS * dest, /* destination address */ /* function to send a packet out the 802.2 socket */ /* returns 1 on success, 0 on failure */ return ethernet_send(dest, /* destination address */ - &src, /* source address */ - pdu, /* any data to be sent - may be null */ - pdu_len /* number of bytes of data */ + &src, /* source address */ + pdu, /* any data to be sent - may be null */ + pdu_len /* number of bytes of data */ ); } /* receives an 802.2 framed packet */ /* returns the number of octets in the PDU, or zero on failure */ -uint16_t ethernet_receive(BACNET_ADDRESS * src, /* source address */ - uint8_t * pdu, /* PDU data */ - uint16_t max_pdu, /* amount of space available in the PDU */ - unsigned timeout /* number of milliseconds to wait for a packet. we ommit it due to winpcap API. */ +uint16_t ethernet_receive( + BACNET_ADDRESS * src, /* source address */ + uint8_t * pdu, /* PDU data */ + uint16_t max_pdu, /* amount of space available in the PDU */ + unsigned timeout /* number of milliseconds to wait for a packet. we ommit it due to winpcap API. */ ) { struct pcap_pkthdr *header; @@ -326,8 +334,7 @@ uint16_t ethernet_receive(BACNET_ADDRESS * src, /* source address */ /* Make sure the socket is open */ if (!ethernet_valid()) { - LogError - ("ethernet.c: invalid 802.2 ethernet interface descriptor!\n"); + LogError("ethernet.c: invalid 802.2 ethernet interface descriptor!\n"); return 0; } @@ -374,7 +381,8 @@ uint16_t ethernet_receive(BACNET_ADDRESS * src, /* source address */ return pdu_len; } -void ethernet_set_my_address(BACNET_ADDRESS * my_address) +void ethernet_set_my_address( + BACNET_ADDRESS * my_address) { int i = 0; @@ -385,7 +393,8 @@ void ethernet_set_my_address(BACNET_ADDRESS * my_address) return; } -void ethernet_get_my_address(BACNET_ADDRESS * my_address) +void ethernet_get_my_address( + BACNET_ADDRESS * my_address) { int i = 0; @@ -403,9 +412,10 @@ void ethernet_get_my_address(BACNET_ADDRESS * my_address) return; } -void ethernet_get_broadcast_address(BACNET_ADDRESS * dest) -{ /* destination address */ - int i = 0; /* counter */ +void ethernet_get_broadcast_address( + BACNET_ADDRESS * dest) +{ /* destination address */ + int i = 0; /* counter */ if (dest) { for (i = 0; i < 6; i++) { @@ -413,7 +423,7 @@ void ethernet_get_broadcast_address(BACNET_ADDRESS * dest) } dest->mac_len = 6; dest->net = BACNET_BROADCAST_NETWORK; - dest->len = 0; /* denotes broadcast address */ + dest->len = 0; /* denotes broadcast address */ for (i = 0; i < MAX_MAC_LEN; i++) { dest->adr[i] = 0; } @@ -422,9 +432,11 @@ void ethernet_get_broadcast_address(BACNET_ADDRESS * dest) return; } -void ethernet_debug_address(const char *info, BACNET_ADDRESS * dest) +void ethernet_debug_address( + const char *info, + BACNET_ADDRESS * dest) { - int i = 0; /* counter */ + int i = 0; /* counter */ char msgBuf[200]; if (info) { @@ -439,15 +451,14 @@ void ethernet_debug_address(const char *info, BACNET_ADDRESS * dest) for (i = 0; i < MAX_MAC_LEN; i++) { sprintf(msgBuf, "%02X ", (unsigned) dest->mac[i]); LogInfo(msgBuf); - } /* for */ + } /* for */ LogInfo("\n"); - sprintf(msgBuf, - " Net=%hu\n Len=%d\n Adr=", dest->net, dest->len); + sprintf(msgBuf, " Net=%hu\n Len=%d\n Adr=", dest->net, dest->len); LogInfo(msgBuf); for (i = 0; i < MAX_MAC_LEN; i++) { sprintf(msgBuf, "%02X ", (unsigned) dest->adr[i]); LogInfo(msgBuf); - } /* for */ + } /* for */ LogInfo("\n"); } /* if ( dest ) */ diff --git a/bacnet-stack/ports/win32/main.c b/bacnet-stack/ports/win32/main.c index ee8a84e5..fce0c32b 100644 --- a/bacnet-stack/ports/win32/main.c +++ b/bacnet-stack/ports/win32/main.c @@ -29,7 +29,7 @@ #include #include #include -#include /* for kbhit and getch */ +#include /* for kbhit and getch */ #include "iam.h" #include "address.h" #include "config.h" @@ -49,7 +49,8 @@ static uint8_t Rx_Buf[MAX_MPDU] = { 0 }; static bool Who_Is_Request = true; bool I_Am_Request = true; -static void Read_Properties(void) +static void Read_Properties( + void) { uint32_t device_id = 0; bool status = false; @@ -123,8 +124,10 @@ static void Read_Properties(void) return; } -static void LocalIAmHandler(uint8_t * service_request, - uint16_t service_len, BACNET_ADDRESS * src) +static void LocalIAmHandler( + uint8_t * service_request, + uint16_t service_len, + BACNET_ADDRESS * src) { int len = 0; uint32_t device_id = 0; @@ -146,13 +149,12 @@ static void LocalIAmHandler(uint8_t * service_request, return; } -static void Init_Service_Handlers(void) +static void Init_Service_Handlers( + void) { /* we need to handle who-is to support dynamic device binding */ - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, - handler_who_is); - apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, - LocalIAmHandler); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, handler_who_is); + apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_I_AM, LocalIAmHandler); /* set the handler for all the services we don't implement */ /* It is required to send the proper reject message... */ @@ -168,9 +170,11 @@ static void Init_Service_Handlers(void) handler_read_property_ack); } -static void print_address(char *name, BACNET_ADDRESS * dest) -{ /* destination address */ - int i = 0; /* counter */ +static void print_address( + char *name, + BACNET_ADDRESS * dest) +{ /* destination address */ + int i = 0; /* counter */ if (dest) { printf("%s: ", name); @@ -181,7 +185,8 @@ static void print_address(char *name, BACNET_ADDRESS * dest) } } -static void print_address_cache(void) +static void print_address_cache( + void) { int i, j; BACNET_ADDRESS address; @@ -202,7 +207,9 @@ static void print_address_cache(void) } } -int main(int argc, char *argv[]) +int main( + int argc, + char *argv[]) { BACNET_ADDRESS src = { 0 }; /* address where message came from */ uint16_t pdu_len = 0; diff --git a/bacnet-stack/ports/win32/rs485.c b/bacnet-stack/ports/win32/rs485.c index 683a72e7..fe497033 100644 --- a/bacnet-stack/ports/win32/rs485.c +++ b/bacnet-stack/ports/win32/rs485.c @@ -40,7 +40,7 @@ B&B Electronics USOPTL4 SerialGear USB-COMi-SI-M */ - + #include #include #include @@ -88,41 +88,44 @@ static DWORD RS485_RTSControl = RTS_CONTROL_DISABLE; * ALGORITHM: none * NOTES: none *****************************************************************************/ -void RS485_Set_Interface(char *ifname) +void RS485_Set_Interface( + char *ifname) { /* note: expects a constant char, or char from the heap */ RS485_Port_Name = ifname; } -static void RS485_Print_Error(void) +static void RS485_Print_Error( + void) { - char * szExtended = ""; /* error string translated from error code */ + char *szExtended = ""; /* error string translated from error code */ DWORD dwExtSize; DWORD dwErr; dwErr = GetLastError(); - fprintf(stderr,"Error %lu:\r\n", dwErr); + fprintf(stderr, "Error %lu:\r\n", dwErr); /* Get error string from system */ - dwExtSize = FormatMessage( - FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | 80 , - NULL, dwErr, - MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), - (LPTSTR) szExtended, 0, NULL); - fprintf(stderr,"%s\r\n", szExtended); + dwExtSize = + FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | + FORMAT_MESSAGE_ALLOCATE_BUFFER | 80, NULL, dwErr, + MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (LPTSTR) szExtended, 0, + NULL); + fprintf(stderr, "%s\r\n", szExtended); return; } -static void RS485_Configure_Status(void) +static void RS485_Configure_Status( + void) { - DCB dcb = {0}; + DCB dcb = { 0 }; COMMTIMEOUTS ctNew; dcb.DCBlength = sizeof(dcb); /* get current DCB settings */ if (!GetCommState(RS485_Handle, &dcb)) { - fprintf(stderr,"Unable to get status from %s\n", RS485_Port_Name); + fprintf(stderr, "Unable to get status from %s\n", RS485_Port_Name); RS485_Print_Error(); exit(1); } @@ -130,49 +133,49 @@ static void RS485_Configure_Status(void) /* update DCB rate, byte size, parity, and stop bits size */ dcb.BaudRate = RS485_Baud; dcb.ByteSize = RS485_ByteSize; - dcb.Parity = RS485_Parity; + dcb.Parity = RS485_Parity; dcb.StopBits = RS485_StopBits; /* update flow control settings */ - dcb.fDtrControl = RS485_DTRControl; - dcb.fRtsControl = RS485_RTSControl; + dcb.fDtrControl = RS485_DTRControl; + dcb.fRtsControl = RS485_RTSControl; /* - dcb.fOutxCtsFlow = CTSOUTFLOW(TTYInfo); - dcb.fOutxDsrFlow = DSROUTFLOW(TTYInfo); - dcb.fDsrSensitivity = DSRINFLOW(TTYInfo); - dcb.fOutX = XONXOFFOUTFLOW(TTYInfo); - dcb.fInX = XONXOFFINFLOW(TTYInfo); - dcb.fTXContinueOnXoff = TXAFTERXOFFSENT(TTYInfo); - dcb.XonChar = XONCHAR(TTYInfo); - dcb.XoffChar = XOFFCHAR(TTYInfo); - dcb.XonLim = XONLIMIT(TTYInfo); - dcb.XoffLim = XOFFLIMIT(TTYInfo); - // DCB settings not in the user's control - dcb.fParity = TRUE; - */ + dcb.fOutxCtsFlow = CTSOUTFLOW(TTYInfo); + dcb.fOutxDsrFlow = DSROUTFLOW(TTYInfo); + dcb.fDsrSensitivity = DSRINFLOW(TTYInfo); + dcb.fOutX = XONXOFFOUTFLOW(TTYInfo); + dcb.fInX = XONXOFFINFLOW(TTYInfo); + dcb.fTXContinueOnXoff = TXAFTERXOFFSENT(TTYInfo); + dcb.XonChar = XONCHAR(TTYInfo); + dcb.XoffChar = XOFFCHAR(TTYInfo); + dcb.XonLim = XONLIMIT(TTYInfo); + dcb.XoffLim = XOFFLIMIT(TTYInfo); + // DCB settings not in the user's control + dcb.fParity = TRUE; + */ if (!SetCommState(RS485_Handle, &dcb)) { - fprintf(stderr,"Unable to set status on %s\n", RS485_Port_Name); + fprintf(stderr, "Unable to set status on %s\n", RS485_Port_Name); RS485_Print_Error(); } /* configure the COM port timeout values */ - ctNew.ReadIntervalTimeout = MAXDWORD; - ctNew.ReadTotalTimeoutMultiplier = MAXDWORD; - ctNew.ReadTotalTimeoutConstant = 1000; + ctNew.ReadIntervalTimeout = MAXDWORD; + ctNew.ReadTotalTimeoutMultiplier = MAXDWORD; + ctNew.ReadTotalTimeoutConstant = 1000; ctNew.WriteTotalTimeoutMultiplier = 0; - ctNew.WriteTotalTimeoutConstant = 0; + ctNew.WriteTotalTimeoutConstant = 0; if (!SetCommTimeouts(RS485_Handle, &ctNew)) { RS485_Print_Error(); } /* Get rid of any stray characters */ if (!PurgeComm(RS485_Handle, PURGE_TXABORT | PURGE_RXABORT)) { - fprintf(stderr,"Unable to purge %s\n", RS485_Port_Name); + fprintf(stderr, "Unable to purge %s\n", RS485_Port_Name); RS485_Print_Error(); } /* Set the Comm buffer size */ SetupComm(RS485_Handle, MAX_MPDU, MAX_MPDU); /* raise DTR */ if (!EscapeCommFunction(RS485_Handle, SETDTR)) { - fprintf(stderr,"Unable to set DTR on %s\n", RS485_Port_Name); + fprintf(stderr, "Unable to set DTR on %s\n", RS485_Port_Name); RS485_Print_Error(); } } @@ -184,22 +187,19 @@ static void RS485_Configure_Status(void) * ALGORITHM: none * NOTES: none *****************************************************************************/ -void RS485_Initialize(void) +void RS485_Initialize( + void) { - RS485_Handle = CreateFile( - RS485_Port_Name, - GENERIC_READ | GENERIC_WRITE, - 0, - 0, - OPEN_EXISTING, - /*FILE_FLAG_OVERLAPPED*/0, + RS485_Handle = CreateFile(RS485_Port_Name, + GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, + /*FILE_FLAG_OVERLAPPED */ 0, 0); if (RS485_Handle == INVALID_HANDLE_VALUE) { - fprintf(stderr,"Unable to open %s\n", RS485_Port_Name); + fprintf(stderr, "Unable to open %s\n", RS485_Port_Name); RS485_Print_Error(); exit(1); } - if (!GetCommTimeouts( RS485_Handle, &RS485_Timeouts)) { + if (!GetCommTimeouts(RS485_Handle, &RS485_Timeouts)) { RS485_Print_Error(); } RS485_Configure_Status(); @@ -207,13 +207,14 @@ void RS485_Initialize(void) return; } -void RS485_Cleanup(void) +void RS485_Cleanup( + void) { if (!EscapeCommFunction(RS485_Handle, CLRDTR)) { RS485_Print_Error(); } - if (!SetCommTimeouts(RS485_Handle, &RS485_Timeouts)) { + if (!SetCommTimeouts(RS485_Handle, &RS485_Timeouts)) { RS485_Print_Error(); } @@ -226,15 +227,21 @@ void RS485_Cleanup(void) * ALGORITHM: none * NOTES: none *****************************************************************************/ -uint32_t RS485_Get_Baud_Rate(void) +uint32_t RS485_Get_Baud_Rate( + void) { switch (RS485_Baud) { - case CBR_19200: return 19200; - case CBR_38400: return 38400; - case CBR_57600: return 57600; - case CBR_115200: return 115200; + case CBR_19200: + return 19200; + case CBR_38400: + return 38400; + case CBR_57600: + return 57600; + case CBR_115200: + return 115200; default: - case CBR_9600: return 9600; + case CBR_9600: + return 9600; } } @@ -244,29 +251,30 @@ uint32_t RS485_Get_Baud_Rate(void) * ALGORITHM: none * NOTES: none *****************************************************************************/ -bool RS485_Set_Baud_Rate(uint32_t baud) +bool RS485_Set_Baud_Rate( + uint32_t baud) { bool valid = true; switch (baud) { - case 9600: - RS485_Baud = CBR_9600; - break; - case 19200: - RS485_Baud = CBR_19200; - break; - case 38400: - RS485_Baud = CBR_38400; - break; - case 57600: - RS485_Baud = CBR_57600; - break; - case 115200: - RS485_Baud = CBR_115200; - break; - default: - valid = false; - break; + case 9600: + RS485_Baud = CBR_9600; + break; + case 19200: + RS485_Baud = CBR_19200; + break; + case 38400: + RS485_Baud = CBR_38400; + break; + case 57600: + RS485_Baud = CBR_57600; + break; + case 115200: + RS485_Baud = CBR_115200; + break; + default: + valid = false; + break; } if (valid) { @@ -278,11 +286,11 @@ bool RS485_Set_Baud_Rate(uint32_t baud) /* Transmits a Frame on the wire */ void RS485_Send_Frame( - volatile struct mstp_port_struct_t *mstp_port, /* port specific data */ - uint8_t * buffer, /* frame to send (up to 501 bytes of data) */ - uint16_t nbytes) /* number of bytes of data (up to 501) */ -{ - DWORD dwWritten = 0; + volatile struct mstp_port_struct_t *mstp_port, /* port specific data */ + uint8_t * buffer, /* frame to send (up to 501 bytes of data) */ + uint16_t nbytes) +{ /* number of bytes of data (up to 501) */ + DWORD dwWritten = 0; if (mstp_port) { uint32_t baud; @@ -310,7 +318,8 @@ void RS485_Send_Frame( } /* called by timer, interrupt(?) or other thread */ -void RS485_Check_UART_Data(volatile struct mstp_port_struct_t *mstp_port) +void RS485_Check_UART_Data( + volatile struct mstp_port_struct_t *mstp_port) { char lpBuf[1]; DWORD dwRead = 0; @@ -322,7 +331,7 @@ void RS485_Check_UART_Data(volatile struct mstp_port_struct_t *mstp_port) else if (mstp_port->DataAvailable == false) { /* check for data */ if (!ReadFile(RS485_Handle, lpBuf, sizeof(lpBuf), &dwRead, NULL)) { - if (GetLastError() != ERROR_IO_PENDING) { + if (GetLastError() != ERROR_IO_PENDING) { mstp_port->ReceiveError = TRUE; } } else { @@ -335,10 +344,11 @@ void RS485_Check_UART_Data(volatile struct mstp_port_struct_t *mstp_port) } #ifdef TEST_RS485 -static void test_transmit_task(void *pArg) +static void test_transmit_task( + void *pArg) { char *TxBuf = "BACnet MS/TP"; - size_t len = strlen(TxBuf)+1; + size_t len = strlen(TxBuf) + 1; while (TRUE) { Sleep(1000); @@ -346,7 +356,8 @@ static void test_transmit_task(void *pArg) } } -int main(void) +int main( + void) { unsigned long hThread = 0; uint32_t arg_value = 0; @@ -357,13 +368,13 @@ int main(void) RS485_Set_Interface("COM4"); RS485_Set_Baud_Rate(38400); RS485_Initialize(); - #if 0 +#if 0 /* create a task for synchronous transmit */ - hThread = _beginthread(test_transmit_task,4096,&arg_value); + hThread = _beginthread(test_transmit_task, 4096, &arg_value); if (hThread == 0) { fprintf(stderr, "Failed to start transmit task\n"); } - #endif +#endif /* receive task */ for (;;) { if (!ReadFile(RS485_Handle, lpBuf, sizeof(lpBuf), &dwRead, NULL)) { @@ -374,12 +385,11 @@ int main(void) /* print any characters received */ if (dwRead) { for (i = 0; i < dwRead; i++) { - fprintf(stderr,"%02X ",lpBuf[i]); + fprintf(stderr, "%02X ", lpBuf[i]); } } dwRead = 0; } } } -#endif /* TEST_ABORT */ - +#endif /* TEST_ABORT */ diff --git a/bacnet-stack/ports/win32/rs485.h b/bacnet-stack/ports/win32/rs485.h index 078e319c..10ac1f36 100644 --- a/bacnet-stack/ports/win32/rs485.h +++ b/bacnet-stack/ports/win32/rs485.h @@ -41,22 +41,28 @@ #ifdef __cplusplus extern "C" { -#endif /* __cplusplus */ +#endif /* __cplusplus */ - void RS485_Set_Interface(char *ifname); + void RS485_Set_Interface( + char *ifname); - void RS485_Initialize(void); + void RS485_Initialize( + void); - void RS485_Send_Frame(volatile struct mstp_port_struct_t *mstp_port, /* port specific data */ + void RS485_Send_Frame( + volatile struct mstp_port_struct_t *mstp_port, /* port specific data */ uint8_t * buffer, /* frame to send (up to 501 bytes of data) */ uint16_t nbytes); /* number of bytes of data (up to 501) */ - void RS485_Check_UART_Data(volatile struct mstp_port_struct_t *mstp_port); /* port specific data */ + void RS485_Check_UART_Data( + volatile struct mstp_port_struct_t *mstp_port); /* port specific data */ - uint32_t RS485_Get_Baud_Rate(void); - bool RS485_Set_Baud_Rate(uint32_t baud); + uint32_t RS485_Get_Baud_Rate( + void); + bool RS485_Set_Baud_Rate( + uint32_t baud); #ifdef __cplusplus } -#endif /* __cplusplus */ +#endif /* __cplusplus */ #endif diff --git a/bacnet-stack/ports/win32/rx_fsm.c b/bacnet-stack/ports/win32/rx_fsm.c index 34ebecb4..586d8d26 100644 --- a/bacnet-stack/ports/win32/rx_fsm.c +++ b/bacnet-stack/ports/win32/rx_fsm.c @@ -56,23 +56,27 @@ static uint8_t RxBuffer[MAX_MPDU]; static uint8_t TxBuffer[MAX_MPDU]; static uint16_t SilenceTime; #define INCREMENT_AND_LIMIT_UINT16(x) {if (x < 0xFFFF) x++;} -static uint16_t Timer_Silence(void) +static uint16_t Timer_Silence( + void) { return SilenceTime; } -static void Timer_Silence_Reset(void) +static void Timer_Silence_Reset( + void) { SilenceTime = 0; } -static void dlmstp_millisecond_timer(void) +static void dlmstp_millisecond_timer( + void) { INCREMENT_AND_LIMIT_UINT16(SilenceTime); } -void *milliseconds_task(void *pArg) +void *milliseconds_task( + void *pArg) { - (void)pArg; + (void) pArg; for (;;) { Sleep(1); dlmstp_millisecond_timer(); @@ -85,28 +89,28 @@ void *milliseconds_task(void *pArg) uint16_t MSTP_Put_Receive( volatile struct mstp_port_struct_t *mstp_port) { - (void)mstp_port; - + (void) mstp_port; + return 0; } /* for the MS/TP state machine to use for getting data to send */ /* Return: amount of PDU data */ uint16_t MSTP_Get_Send( - volatile struct mstp_port_struct_t *mstp_port, - unsigned timeout) /* milliseconds to wait for a packet */ -{ - (void)mstp_port; - (void)timeout; + volatile struct mstp_port_struct_t * mstp_port, + unsigned timeout) +{ /* milliseconds to wait for a packet */ + (void) mstp_port; + (void) timeout; return 0; } uint16_t MSTP_Get_Reply( - volatile struct mstp_port_struct_t *mstp_port, - unsigned timeout) /* milliseconds to wait for a packet */ -{ - (void)mstp_port; - (void)timeout; + volatile struct mstp_port_struct_t * mstp_port, + unsigned timeout) +{ /* milliseconds to wait for a packet */ + (void) mstp_port; + (void) timeout; return 0; } @@ -131,27 +135,25 @@ static void print_received_packet( mstp_port->DestinationAddress, mstp_port->SourceAddress, HI_BYTE(mstp_port->DataLength), - LO_BYTE(mstp_port->DataLength), - mstp_port->HeaderCRCActual); + LO_BYTE(mstp_port->DataLength), mstp_port->HeaderCRCActual); if (mstp_port->DataLength) { for (i = 0; i < mstp_port->DataLength; i++) { - fprintf(stderr,"%02X ", - mstp_port->InputBuffer[i]); + fprintf(stderr, "%02X ", mstp_port->InputBuffer[i]); } fprintf(stderr, - "%02X %02X ", - mstp_port->DataCRCActualMSB, - mstp_port->DataCRCActualLSB); + "%02X %02X ", + mstp_port->DataCRCActualMSB, mstp_port->DataCRCActualLSB); } - fprintf(stderr,"%s", - mstptext_frame_type(mstp_port->FrameType)); - fprintf(stderr,"\n"); + fprintf(stderr, "%s", mstptext_frame_type(mstp_port->FrameType)); + fprintf(stderr, "\n"); } static char *Network_Interface = NULL; /* simple test to packetize the data and print it */ -int main(int argc, char *argv[]) +int main( + int argc, + char *argv[]) { volatile struct mstp_port_struct_t *mstp_port; int rc = 0; @@ -168,7 +170,7 @@ int main(int argc, char *argv[]) if (argc > 2) { my_mac = strtol(argv[2], NULL, 0); if (my_mac > 127) - my_mac = 127; + my_mac = 127; } /* initialize our interface */ RS485_Set_Interface(Network_Interface); @@ -186,7 +188,7 @@ int main(int argc, char *argv[]) MSTP_Init(&MSTP_Port); mstp_port->Lurking = true; /* start our MilliSec task */ - hThread = _beginthread(milliseconds_task,4096,&arg_value); + hThread = _beginthread(milliseconds_task, 4096, &arg_value); if (hThread == 0) { fprintf(stderr, "Failed to start timer task\n"); } @@ -207,4 +209,3 @@ int main(int argc, char *argv[]) //return 0; } - diff --git a/bacnet-stack/ports/win32/stdint.h b/bacnet-stack/ports/win32/stdint.h index 93f1a85b..ffb0d7c5 100644 --- a/bacnet-stack/ports/win32/stdint.h +++ b/bacnet-stack/ports/win32/stdint.h @@ -11,7 +11,7 @@ typedef signed char int8_t; /* 1 byte -127 to 127 */ typedef unsigned short uint16_t; /* 2 bytes 0 to 65535 */ typedef signed short int16_t; /* 2 bytes -32767 to 32767 */ /*typedef unsigned short long uint24_t; // 3 bytes 0 to 16777215 */ -typedef unsigned uint32_t; /* 4 bytes 0 to 4294967295 */ +typedef unsigned uint32_t; /* 4 bytes 0 to 4294967295 */ typedef int int32_t; /* 4 bytes -2147483647 to 2147483647 */ /* typedef signed long long int64_t; */ /* typedef unsigned long long uint64_t; */ @@ -24,8 +24,8 @@ typedef int int32_t; /* 4 bytes -2147483647 to 2147483647 */ #define INT16_MAX 32767 #define INT32_MAX 2147483647 -#define UINT8_MAX 0xff /* 255U */ -#define UINT16_MAX 0xffff /* 65535U */ -#define UINT32_MAX 0xffffffff /* 4294967295U */ +#define UINT8_MAX 0xff /* 255U */ +#define UINT16_MAX 0xffff /* 65535U */ +#define UINT32_MAX 0xffffffff /* 4294967295U */ -#endif /* STDINT_H */ +#endif /* STDINT_H */ diff --git a/bacnet-stack/src/abort.c b/bacnet-stack/src/abort.c index 6f998b64..877535b2 100644 --- a/bacnet-stack/src/abort.c +++ b/bacnet-stack/src/abort.c @@ -37,10 +37,13 @@ #include "bacdef.h" /* encode service */ -int abort_encode_apdu(uint8_t * apdu, - uint8_t invoke_id, uint8_t abort_reason, bool server) +int abort_encode_apdu( + uint8_t * apdu, + uint8_t invoke_id, + uint8_t abort_reason, + bool server) { - int apdu_len = 0; /* total length of the apdu, return value */ + int apdu_len = 0; /* total length of the apdu, return value */ if (apdu) { if (server) @@ -56,8 +59,11 @@ int abort_encode_apdu(uint8_t * apdu, } /* decode the service request only */ -int abort_decode_service_request(uint8_t * apdu, - unsigned apdu_len, uint8_t * invoke_id, uint8_t * abort_reason) +int abort_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + uint8_t * invoke_id, + uint8_t * abort_reason) { int len = 0; @@ -77,8 +83,11 @@ int abort_decode_service_request(uint8_t * apdu, #include "ctest.h" /* decode the whole APDU - mainly used for unit testing */ -int abort_decode_apdu(uint8_t * apdu, - unsigned apdu_len, uint8_t * invoke_id, uint8_t * abort_reason, +int abort_decode_apdu( + uint8_t * apdu, + unsigned apdu_len, + uint8_t * invoke_id, + uint8_t * abort_reason, bool * server) { int len = 0; @@ -102,8 +111,11 @@ int abort_decode_apdu(uint8_t * apdu, return len; } -void testAbortAPDU(Test * pTest, - uint8_t invoke_id, uint8_t abort_reason, bool server) +void testAbortAPDU( + Test * pTest, + uint8_t invoke_id, + uint8_t abort_reason, + bool server) { uint8_t apdu[480] = { 0 }; int len = 0; @@ -126,7 +138,8 @@ void testAbortAPDU(Test * pTest, } -void testAbort(Test * pTest) +void testAbort( + Test * pTest) { uint8_t apdu[480] = { 0 }; int len = 0; @@ -174,7 +187,8 @@ void testAbort(Test * pTest) } #ifdef TEST_ABORT -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -191,5 +205,5 @@ int main(void) return 0; } -#endif /* TEST_ABORT */ -#endif /* TEST */ +#endif /* TEST_ABORT */ +#endif /* TEST */ diff --git a/bacnet-stack/src/address.c b/bacnet-stack/src/address.c index 744cbb25..f3d95c0c 100644 --- a/bacnet-stack/src/address.c +++ b/bacnet-stack/src/address.c @@ -53,7 +53,8 @@ static struct Address_Cache_Entry { BACNET_ADDRESS address; } Address_Cache[MAX_ADDRESS_CACHE]; -void address_remove_device(uint32_t device_id) +void address_remove_device( + uint32_t device_id) { unsigned i; @@ -69,7 +70,8 @@ void address_remove_device(uint32_t device_id) return; } -void address_init(void) +void address_init( + void) { unsigned i; @@ -81,11 +83,13 @@ void address_init(void) return; } -bool address_get_by_device(uint32_t device_id, - unsigned *max_apdu, BACNET_ADDRESS * src) +bool address_get_by_device( + uint32_t device_id, + unsigned *max_apdu, + BACNET_ADDRESS * src) { unsigned i; - bool found = false; /* return value */ + bool found = false; /* return value */ for (i = 0; i < MAX_ADDRESS_CACHE; i++) { if (Address_Cache[i].valid && @@ -101,11 +105,12 @@ bool address_get_by_device(uint32_t device_id, } /* find a device id from a given MAC address */ -bool address_get_device_id(BACNET_ADDRESS * src, - uint32_t *device_id) +bool address_get_device_id( + BACNET_ADDRESS * src, + uint32_t * device_id) { unsigned i; - bool found = false; /* return value */ + bool found = false; /* return value */ for (i = 0; i < MAX_ADDRESS_CACHE; i++) { if (Address_Cache[i].valid) { @@ -122,11 +127,13 @@ bool address_get_device_id(BACNET_ADDRESS * src, return found; } -void address_add(uint32_t device_id, - unsigned max_apdu, BACNET_ADDRESS * src) +void address_add( + uint32_t device_id, + unsigned max_apdu, + BACNET_ADDRESS * src) { unsigned i; - bool found = false; /* return value */ + bool found = false; /* return value */ /* existing device - update address */ for (i = 0; i < MAX_ADDRESS_CACHE; i++) { @@ -156,11 +163,13 @@ void address_add(uint32_t device_id, /* returns true if device is already bound */ /* also returns the address and max apdu if already bound */ -bool address_bind_request(uint32_t device_id, - unsigned *max_apdu, BACNET_ADDRESS * src) +bool address_bind_request( + uint32_t device_id, + unsigned *max_apdu, + BACNET_ADDRESS * src) { unsigned i; - bool found = false; /* return value */ + bool found = false; /* return value */ /* existing device - update address */ for (i = 0; i < MAX_ADDRESS_CACHE; i++) { @@ -192,11 +201,13 @@ bool address_bind_request(uint32_t device_id, return found; } -void address_add_binding(uint32_t device_id, - unsigned max_apdu, BACNET_ADDRESS * src) +void address_add_binding( + uint32_t device_id, + unsigned max_apdu, + BACNET_ADDRESS * src) { unsigned i; - bool found = false; /* return value */ + bool found = false; /* return value */ /* existing device - update address */ for (i = 0; i < MAX_ADDRESS_CACHE; i++) { @@ -225,10 +236,13 @@ void address_add_binding(uint32_t device_id, return; } -bool address_get_by_index(unsigned index, - uint32_t * device_id, unsigned *max_apdu, BACNET_ADDRESS * src) +bool address_get_by_index( + unsigned index, + uint32_t * device_id, + unsigned *max_apdu, + BACNET_ADDRESS * src) { - bool found = false; /* return value */ + bool found = false; /* return value */ if (index < MAX_ADDRESS_CACHE) { if (Address_Cache[index].valid) { @@ -242,10 +256,11 @@ bool address_get_by_index(unsigned index, return found; } -unsigned address_count(void) +unsigned address_count( + void) { unsigned i; - unsigned count = 0; /* return value */ + unsigned count = 0; /* return value */ for (i = 0; i < MAX_ADDRESS_CACHE; i++) { if (Address_Cache[i].valid) @@ -260,7 +275,9 @@ unsigned address_count(void) #include #include "ctest.h" -static void set_address(unsigned index, BACNET_ADDRESS * dest) +static void set_address( + unsigned index, + BACNET_ADDRESS * dest) { unsigned i; @@ -275,7 +292,8 @@ static void set_address(unsigned index, BACNET_ADDRESS * dest) } } -void testAddress(Test * pTest) +void testAddress( + Test * pTest) { unsigned i, count; BACNET_ADDRESS src; @@ -324,7 +342,8 @@ void testAddress(Test * pTest) } #ifdef TEST_ADDRESS -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -341,5 +360,5 @@ int main(void) return 0; } -#endif /* TEST_ADDRESS */ -#endif /* TEST */ +#endif /* TEST_ADDRESS */ +#endif /* TEST */ diff --git a/bacnet-stack/src/apdu.c b/bacnet-stack/src/apdu.c index 9269b542..4ac321e3 100644 --- a/bacnet-stack/src/apdu.c +++ b/bacnet-stack/src/apdu.c @@ -97,7 +97,8 @@ static BACNET_SERVICES_SUPPORTED /* If they are not set, they are handled by a reject message */ static confirmed_function Confirmed_Function[MAX_BACNET_CONFIRMED_SERVICE]; -void apdu_set_confirmed_handler(BACNET_CONFIRMED_SERVICE service_choice, +void apdu_set_confirmed_handler( + BACNET_CONFIRMED_SERVICE service_choice, confirmed_function pFunction) { if (service_choice < MAX_BACNET_CONFIRMED_SERVICE) @@ -107,8 +108,8 @@ void apdu_set_confirmed_handler(BACNET_CONFIRMED_SERVICE service_choice, /* Allow the APDU handler to automatically reject */ static confirmed_function Unrecognized_Service_Handler; -void apdu_set_unrecognized_service_handler_handler(confirmed_function - pFunction) +void apdu_set_unrecognized_service_handler_handler( + confirmed_function pFunction) { Unrecognized_Service_Handler = pFunction; } @@ -118,14 +119,16 @@ void apdu_set_unrecognized_service_handler_handler(confirmed_function static unconfirmed_function Unconfirmed_Function[MAX_BACNET_UNCONFIRMED_SERVICE]; -void apdu_set_unconfirmed_handler(BACNET_UNCONFIRMED_SERVICE - service_choice, unconfirmed_function pFunction) +void apdu_set_unconfirmed_handler( + BACNET_UNCONFIRMED_SERVICE service_choice, + unconfirmed_function pFunction) { if (service_choice < MAX_BACNET_UNCONFIRMED_SERVICE) Unconfirmed_Function[service_choice] = pFunction; } -bool apdu_service_supported(BACNET_SERVICES_SUPPORTED service_supported) +bool apdu_service_supported( + BACNET_SERVICES_SUPPORTED service_supported) { int i = 0; bool status = false; @@ -159,70 +162,73 @@ bool apdu_service_supported(BACNET_SERVICES_SUPPORTED service_supported) /* Confirmed ACK Function Handlers */ static void *Confirmed_ACK_Function[MAX_BACNET_CONFIRMED_SERVICE]; -void apdu_set_confirmed_simple_ack_handler(BACNET_CONFIRMED_SERVICE - service_choice, confirmed_simple_ack_function pFunction) +void apdu_set_confirmed_simple_ack_handler( + BACNET_CONFIRMED_SERVICE service_choice, + confirmed_simple_ack_function pFunction) { switch (service_choice) { - case SERVICE_CONFIRMED_ACKNOWLEDGE_ALARM: - case SERVICE_CONFIRMED_COV_NOTIFICATION: - case SERVICE_CONFIRMED_EVENT_NOTIFICATION: - case SERVICE_CONFIRMED_SUBSCRIBE_COV: - case SERVICE_CONFIRMED_SUBSCRIBE_COV_PROPERTY: - case SERVICE_CONFIRMED_LIFE_SAFETY_OPERATION: - /* Object Access Services */ - case SERVICE_CONFIRMED_ADD_LIST_ELEMENT: - case SERVICE_CONFIRMED_REMOVE_LIST_ELEMENT: - case SERVICE_CONFIRMED_DELETE_OBJECT: - case SERVICE_CONFIRMED_WRITE_PROPERTY: - case SERVICE_CONFIRMED_WRITE_PROPERTY_MULTIPLE: - /* Remote Device Management Services */ - case SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL: - case SERVICE_CONFIRMED_TEXT_MESSAGE: - case SERVICE_CONFIRMED_REINITIALIZE_DEVICE: - /* Virtual Terminal Services */ - case SERVICE_CONFIRMED_VT_CLOSE: - /* Security Services */ - case SERVICE_CONFIRMED_REQUEST_KEY: - Confirmed_ACK_Function[service_choice] = (void *) pFunction; - break; - default: - break; + case SERVICE_CONFIRMED_ACKNOWLEDGE_ALARM: + case SERVICE_CONFIRMED_COV_NOTIFICATION: + case SERVICE_CONFIRMED_EVENT_NOTIFICATION: + case SERVICE_CONFIRMED_SUBSCRIBE_COV: + case SERVICE_CONFIRMED_SUBSCRIBE_COV_PROPERTY: + case SERVICE_CONFIRMED_LIFE_SAFETY_OPERATION: + /* Object Access Services */ + case SERVICE_CONFIRMED_ADD_LIST_ELEMENT: + case SERVICE_CONFIRMED_REMOVE_LIST_ELEMENT: + case SERVICE_CONFIRMED_DELETE_OBJECT: + case SERVICE_CONFIRMED_WRITE_PROPERTY: + case SERVICE_CONFIRMED_WRITE_PROPERTY_MULTIPLE: + /* Remote Device Management Services */ + case SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL: + case SERVICE_CONFIRMED_TEXT_MESSAGE: + case SERVICE_CONFIRMED_REINITIALIZE_DEVICE: + /* Virtual Terminal Services */ + case SERVICE_CONFIRMED_VT_CLOSE: + /* Security Services */ + case SERVICE_CONFIRMED_REQUEST_KEY: + Confirmed_ACK_Function[service_choice] = (void *) pFunction; + break; + default: + break; } } -void apdu_set_confirmed_ack_handler(BACNET_CONFIRMED_SERVICE - service_choice, confirmed_ack_function pFunction) +void apdu_set_confirmed_ack_handler( + BACNET_CONFIRMED_SERVICE service_choice, + confirmed_ack_function pFunction) { switch (service_choice) { - case SERVICE_CONFIRMED_GET_ALARM_SUMMARY: - case SERVICE_CONFIRMED_GET_ENROLLMENT_SUMMARY: - case SERVICE_CONFIRMED_GET_EVENT_INFORMATION: - /* File Access Services */ - case SERVICE_CONFIRMED_ATOMIC_READ_FILE: - case SERVICE_CONFIRMED_ATOMIC_WRITE_FILE: - /* Object Access Services */ - case SERVICE_CONFIRMED_CREATE_OBJECT: - case SERVICE_CONFIRMED_READ_PROPERTY: - case SERVICE_CONFIRMED_READ_PROPERTY_CONDITIONAL: - case SERVICE_CONFIRMED_READ_PROPERTY_MULTIPLE: - case SERVICE_CONFIRMED_READ_RANGE: - /* Remote Device Management Services */ - case SERVICE_CONFIRMED_PRIVATE_TRANSFER: - /* Virtual Terminal Services */ - case SERVICE_CONFIRMED_VT_OPEN: - case SERVICE_CONFIRMED_VT_DATA: - /* Security Services */ - case SERVICE_CONFIRMED_AUTHENTICATE: - Confirmed_ACK_Function[service_choice] = (void *) pFunction; - break; - default: - break; + case SERVICE_CONFIRMED_GET_ALARM_SUMMARY: + case SERVICE_CONFIRMED_GET_ENROLLMENT_SUMMARY: + case SERVICE_CONFIRMED_GET_EVENT_INFORMATION: + /* File Access Services */ + case SERVICE_CONFIRMED_ATOMIC_READ_FILE: + case SERVICE_CONFIRMED_ATOMIC_WRITE_FILE: + /* Object Access Services */ + case SERVICE_CONFIRMED_CREATE_OBJECT: + case SERVICE_CONFIRMED_READ_PROPERTY: + case SERVICE_CONFIRMED_READ_PROPERTY_CONDITIONAL: + case SERVICE_CONFIRMED_READ_PROPERTY_MULTIPLE: + case SERVICE_CONFIRMED_READ_RANGE: + /* Remote Device Management Services */ + case SERVICE_CONFIRMED_PRIVATE_TRANSFER: + /* Virtual Terminal Services */ + case SERVICE_CONFIRMED_VT_OPEN: + case SERVICE_CONFIRMED_VT_DATA: + /* Security Services */ + case SERVICE_CONFIRMED_AUTHENTICATE: + Confirmed_ACK_Function[service_choice] = (void *) pFunction; + break; + default: + break; } } static error_function Error_Function[MAX_BACNET_CONFIRMED_SERVICE]; -void apdu_set_error_handler(BACNET_CONFIRMED_SERVICE service_choice, +void apdu_set_error_handler( + BACNET_CONFIRMED_SERVICE service_choice, error_function pFunction) { if (service_choice < MAX_BACNET_CONFIRMED_SERVICE) @@ -231,25 +237,29 @@ void apdu_set_error_handler(BACNET_CONFIRMED_SERVICE service_choice, static abort_function Abort_Function; -void apdu_set_abort_handler(abort_function pFunction) +void apdu_set_abort_handler( + abort_function pFunction) { Abort_Function = pFunction; } static reject_function Reject_Function; -void apdu_set_reject_handler(reject_function pFunction) +void apdu_set_reject_handler( + reject_function pFunction) { Reject_Function = pFunction; } -uint16_t apdu_decode_confirmed_service_request(uint8_t * apdu, /* APDU data */ +uint16_t apdu_decode_confirmed_service_request( + uint8_t * apdu, /* APDU data */ uint16_t apdu_len, BACNET_CONFIRMED_SERVICE_DATA * service_data, uint8_t * service_choice, - uint8_t ** service_request, uint16_t * service_request_len) + uint8_t ** service_request, + uint16_t * service_request_len) { - uint16_t len = 0; /* counts where we are in PDU */ + uint16_t len = 0; /* counts where we are in PDU */ service_data->segmented_message = (apdu[0] & BIT3) ? true : false; service_data->more_follows = (apdu[0] & BIT2) ? true : false; @@ -270,7 +280,9 @@ uint16_t apdu_decode_confirmed_service_request(uint8_t * apdu, /* APDU data */ return len; } -void apdu_handler(BACNET_ADDRESS * src, uint8_t * apdu, /* APDU data */ +void apdu_handler( + BACNET_ADDRESS * src, + uint8_t * apdu, /* APDU data */ uint16_t apdu_len) { BACNET_CONFIRMED_SERVICE_DATA service_data = { 0 }; @@ -279,7 +291,7 @@ void apdu_handler(BACNET_ADDRESS * src, uint8_t * apdu, /* APDU data */ uint8_t service_choice = 0; uint8_t *service_request = NULL; uint16_t service_request_len = 0; - uint16_t len = 0; /* counts where we are in PDU */ + uint16_t len = 0; /* counts where we are in PDU */ uint8_t tag_number = 0; uint32_t len_value = 0; int error_code = 0; @@ -290,165 +302,165 @@ void apdu_handler(BACNET_ADDRESS * src, uint8_t * apdu, /* APDU data */ if (apdu) { /* PDU Type */ switch (apdu[0] & 0xF0) { - case PDU_TYPE_CONFIRMED_SERVICE_REQUEST: - len = apdu_decode_confirmed_service_request(&apdu[0], /* APDU data */ - apdu_len, - &service_data, - &service_choice, &service_request, &service_request_len); - /* When network communications are completely disabled, - only DeviceCommunicationControl and ReinitializeDevice APDUs - shall be processed and no messages shall be initiated. */ - if (dcc_communication_disabled() && - ((service_choice != - SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL) - && (service_choice != - SERVICE_CONFIRMED_REINITIALIZE_DEVICE))) + case PDU_TYPE_CONFIRMED_SERVICE_REQUEST: + len = apdu_decode_confirmed_service_request(&apdu[0], /* APDU data */ + apdu_len, + &service_data, + &service_choice, &service_request, &service_request_len); + /* When network communications are completely disabled, + only DeviceCommunicationControl and ReinitializeDevice APDUs + shall be processed and no messages shall be initiated. */ + if (dcc_communication_disabled() && + ((service_choice != + SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL) + && (service_choice != + SERVICE_CONFIRMED_REINITIALIZE_DEVICE))) + break; + if ((service_choice < MAX_BACNET_CONFIRMED_SERVICE) && + (Confirmed_Function[service_choice])) + Confirmed_Function[service_choice] (service_request, + service_request_len, src, &service_data); + else if (Unrecognized_Service_Handler) + Unrecognized_Service_Handler(service_request, + service_request_len, src, &service_data); break; - if ((service_choice < MAX_BACNET_CONFIRMED_SERVICE) && - (Confirmed_Function[service_choice])) - Confirmed_Function[service_choice] (service_request, - service_request_len, src, &service_data); - else if (Unrecognized_Service_Handler) - Unrecognized_Service_Handler(service_request, - service_request_len, src, &service_data); - break; - case PDU_TYPE_UNCONFIRMED_SERVICE_REQUEST: - if (dcc_communication_disabled()) - break; - service_choice = apdu[1]; - service_request = &apdu[2]; - service_request_len = apdu_len - 2; - if (service_choice < MAX_BACNET_UNCONFIRMED_SERVICE) { - if (Unconfirmed_Function[service_choice]) - Unconfirmed_Function[service_choice] (service_request, - service_request_len, src); - } - break; - case PDU_TYPE_SIMPLE_ACK: - invoke_id = apdu[1]; - service_choice = apdu[2]; - switch (service_choice) { - case SERVICE_CONFIRMED_ACKNOWLEDGE_ALARM: - case SERVICE_CONFIRMED_COV_NOTIFICATION: - case SERVICE_CONFIRMED_EVENT_NOTIFICATION: - case SERVICE_CONFIRMED_SUBSCRIBE_COV: - case SERVICE_CONFIRMED_SUBSCRIBE_COV_PROPERTY: - case SERVICE_CONFIRMED_LIFE_SAFETY_OPERATION: - /* Object Access Services */ - case SERVICE_CONFIRMED_ADD_LIST_ELEMENT: - case SERVICE_CONFIRMED_REMOVE_LIST_ELEMENT: - case SERVICE_CONFIRMED_DELETE_OBJECT: - case SERVICE_CONFIRMED_WRITE_PROPERTY: - case SERVICE_CONFIRMED_WRITE_PROPERTY_MULTIPLE: - /* Remote Device Management Services */ - case SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL: - case SERVICE_CONFIRMED_REINITIALIZE_DEVICE: - case SERVICE_CONFIRMED_TEXT_MESSAGE: - /* Virtual Terminal Services */ - case SERVICE_CONFIRMED_VT_CLOSE: - /* Security Services */ - case SERVICE_CONFIRMED_REQUEST_KEY: - if (Confirmed_ACK_Function[service_choice]) { - ((confirmed_simple_ack_function) - Confirmed_ACK_Function[service_choice]) (src, - invoke_id); + case PDU_TYPE_UNCONFIRMED_SERVICE_REQUEST: + if (dcc_communication_disabled()) + break; + service_choice = apdu[1]; + service_request = &apdu[2]; + service_request_len = apdu_len - 2; + if (service_choice < MAX_BACNET_UNCONFIRMED_SERVICE) { + if (Unconfirmed_Function[service_choice]) + Unconfirmed_Function[service_choice] (service_request, + service_request_len, src); } + break; + case PDU_TYPE_SIMPLE_ACK: + invoke_id = apdu[1]; + service_choice = apdu[2]; + switch (service_choice) { + case SERVICE_CONFIRMED_ACKNOWLEDGE_ALARM: + case SERVICE_CONFIRMED_COV_NOTIFICATION: + case SERVICE_CONFIRMED_EVENT_NOTIFICATION: + case SERVICE_CONFIRMED_SUBSCRIBE_COV: + case SERVICE_CONFIRMED_SUBSCRIBE_COV_PROPERTY: + case SERVICE_CONFIRMED_LIFE_SAFETY_OPERATION: + /* Object Access Services */ + case SERVICE_CONFIRMED_ADD_LIST_ELEMENT: + case SERVICE_CONFIRMED_REMOVE_LIST_ELEMENT: + case SERVICE_CONFIRMED_DELETE_OBJECT: + case SERVICE_CONFIRMED_WRITE_PROPERTY: + case SERVICE_CONFIRMED_WRITE_PROPERTY_MULTIPLE: + /* Remote Device Management Services */ + case SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL: + case SERVICE_CONFIRMED_REINITIALIZE_DEVICE: + case SERVICE_CONFIRMED_TEXT_MESSAGE: + /* Virtual Terminal Services */ + case SERVICE_CONFIRMED_VT_CLOSE: + /* Security Services */ + case SERVICE_CONFIRMED_REQUEST_KEY: + if (Confirmed_ACK_Function[service_choice]) { + ((confirmed_simple_ack_function) + Confirmed_ACK_Function[service_choice]) (src, + invoke_id); + } + tsm_free_invoke_id(invoke_id); + break; + default: + break; + } + break; + case PDU_TYPE_COMPLEX_ACK: + service_ack_data.segmented_message = + (apdu[0] & BIT3) ? true : false; + service_ack_data.more_follows = + (apdu[0] & BIT2) ? true : false; + invoke_id = service_ack_data.invoke_id = apdu[1]; + len = 2; + if (service_ack_data.segmented_message) { + service_ack_data.sequence_number = apdu[len++]; + service_ack_data.proposed_window_number = apdu[len++]; + } + service_choice = apdu[len++]; + service_request = &apdu[len]; + service_request_len = apdu_len - len; + switch (service_choice) { + case SERVICE_CONFIRMED_GET_ALARM_SUMMARY: + case SERVICE_CONFIRMED_GET_ENROLLMENT_SUMMARY: + case SERVICE_CONFIRMED_GET_EVENT_INFORMATION: + /* File Access Services */ + case SERVICE_CONFIRMED_ATOMIC_READ_FILE: + case SERVICE_CONFIRMED_ATOMIC_WRITE_FILE: + /* Object Access Services */ + case SERVICE_CONFIRMED_CREATE_OBJECT: + case SERVICE_CONFIRMED_READ_PROPERTY: + case SERVICE_CONFIRMED_READ_PROPERTY_CONDITIONAL: + case SERVICE_CONFIRMED_READ_PROPERTY_MULTIPLE: + case SERVICE_CONFIRMED_READ_RANGE: + case SERVICE_CONFIRMED_PRIVATE_TRANSFER: + /* Virtual Terminal Services */ + case SERVICE_CONFIRMED_VT_OPEN: + case SERVICE_CONFIRMED_VT_DATA: + /* Security Services */ + case SERVICE_CONFIRMED_AUTHENTICATE: + if (Confirmed_ACK_Function[service_choice]) { + ((confirmed_ack_function) + Confirmed_ACK_Function[service_choice]) + (service_request, service_request_len, src, + &service_ack_data); + } + tsm_free_invoke_id(invoke_id); + break; + default: + break; + } + break; + case PDU_TYPE_SEGMENT_ACK: + /* FIXME: what about a denial of service attack here? + we could check src to see if that matched the tsm */ + tsm_free_invoke_id(invoke_id); + break; + case PDU_TYPE_ERROR: + invoke_id = apdu[1]; + service_choice = apdu[2]; + len = 3; + len += + decode_tag_number_and_value(&apdu[len], &tag_number, + &len_value); + /* FIXME: we could validate that the tag is enumerated... */ + len += decode_enumerated(&apdu[len], len_value, &error_class); + len += + decode_tag_number_and_value(&apdu[len], &tag_number, + &len_value); + /* FIXME: we could validate that the tag is enumerated... */ + len += decode_enumerated(&apdu[len], len_value, &error_code); + if (service_choice < MAX_BACNET_CONFIRMED_SERVICE) { + if (Error_Function[service_choice]) + Error_Function[service_choice] (src, + invoke_id, + (BACNET_ERROR_CLASS) error_class, + (BACNET_ERROR_CODE) error_code); + } + tsm_free_invoke_id(invoke_id); + break; + case PDU_TYPE_REJECT: + invoke_id = apdu[1]; + reason = apdu[2]; + if (Reject_Function) + Reject_Function(src, invoke_id, reason); + tsm_free_invoke_id(invoke_id); + break; + case PDU_TYPE_ABORT: + server = apdu[0] & 0x01; + invoke_id = apdu[1]; + reason = apdu[2]; + if (Abort_Function) + Abort_Function(src, invoke_id, reason, server); tsm_free_invoke_id(invoke_id); break; default: break; - } - break; - case PDU_TYPE_COMPLEX_ACK: - service_ack_data.segmented_message = - (apdu[0] & BIT3) ? true : false; - service_ack_data.more_follows = - (apdu[0] & BIT2) ? true : false; - invoke_id = service_ack_data.invoke_id = apdu[1]; - len = 2; - if (service_ack_data.segmented_message) { - service_ack_data.sequence_number = apdu[len++]; - service_ack_data.proposed_window_number = apdu[len++]; - } - service_choice = apdu[len++]; - service_request = &apdu[len]; - service_request_len = apdu_len - len; - switch (service_choice) { - case SERVICE_CONFIRMED_GET_ALARM_SUMMARY: - case SERVICE_CONFIRMED_GET_ENROLLMENT_SUMMARY: - case SERVICE_CONFIRMED_GET_EVENT_INFORMATION: - /* File Access Services */ - case SERVICE_CONFIRMED_ATOMIC_READ_FILE: - case SERVICE_CONFIRMED_ATOMIC_WRITE_FILE: - /* Object Access Services */ - case SERVICE_CONFIRMED_CREATE_OBJECT: - case SERVICE_CONFIRMED_READ_PROPERTY: - case SERVICE_CONFIRMED_READ_PROPERTY_CONDITIONAL: - case SERVICE_CONFIRMED_READ_PROPERTY_MULTIPLE: - case SERVICE_CONFIRMED_READ_RANGE: - case SERVICE_CONFIRMED_PRIVATE_TRANSFER: - /* Virtual Terminal Services */ - case SERVICE_CONFIRMED_VT_OPEN: - case SERVICE_CONFIRMED_VT_DATA: - /* Security Services */ - case SERVICE_CONFIRMED_AUTHENTICATE: - if (Confirmed_ACK_Function[service_choice]) { - ((confirmed_ack_function) - Confirmed_ACK_Function[service_choice]) - (service_request, service_request_len, src, - &service_ack_data); - } - tsm_free_invoke_id(invoke_id); - break; - default: - break; - } - break; - case PDU_TYPE_SEGMENT_ACK: - /* FIXME: what about a denial of service attack here? - we could check src to see if that matched the tsm */ - tsm_free_invoke_id(invoke_id); - break; - case PDU_TYPE_ERROR: - invoke_id = apdu[1]; - service_choice = apdu[2]; - len = 3; - len += - decode_tag_number_and_value(&apdu[len], &tag_number, - &len_value); - /* FIXME: we could validate that the tag is enumerated... */ - len += decode_enumerated(&apdu[len], len_value, &error_class); - len += - decode_tag_number_and_value(&apdu[len], &tag_number, - &len_value); - /* FIXME: we could validate that the tag is enumerated... */ - len += decode_enumerated(&apdu[len], len_value, &error_code); - if (service_choice < MAX_BACNET_CONFIRMED_SERVICE) { - if (Error_Function[service_choice]) - Error_Function[service_choice] (src, - invoke_id, - (BACNET_ERROR_CLASS)error_class, - (BACNET_ERROR_CODE)error_code); - } - tsm_free_invoke_id(invoke_id); - break; - case PDU_TYPE_REJECT: - invoke_id = apdu[1]; - reason = apdu[2]; - if (Reject_Function) - Reject_Function(src, invoke_id, reason); - tsm_free_invoke_id(invoke_id); - break; - case PDU_TYPE_ABORT: - server = apdu[0] & 0x01; - invoke_id = apdu[1]; - reason = apdu[2]; - if (Abort_Function) - Abort_Function(src, invoke_id, reason, server); - tsm_free_invoke_id(invoke_id); - break; - default: - break; } } return; diff --git a/bacnet-stack/src/arf.c b/bacnet-stack/src/arf.c index d8ee27fa..eb8e663f 100644 --- a/bacnet-stack/src/arf.c +++ b/bacnet-stack/src/arf.c @@ -41,10 +41,12 @@ /* Atomic Read File */ /* encode service */ -int arf_encode_apdu(uint8_t * apdu, - uint8_t invoke_id, BACNET_ATOMIC_READ_FILE_DATA * data) +int arf_encode_apdu( + uint8_t * apdu, + uint8_t invoke_id, + BACNET_ATOMIC_READ_FILE_DATA * data) { - int apdu_len = 0; /* total length of the apdu, return value */ + int apdu_len = 0; /* total length of the apdu, return value */ if (apdu) { apdu[0] = PDU_TYPE_CONFIRMED_SERVICE_REQUEST; @@ -56,24 +58,24 @@ int arf_encode_apdu(uint8_t * apdu, apdu_len += encode_application_object_id(&apdu[apdu_len], data->object_type, data->object_instance); switch (data->access) { - case FILE_STREAM_ACCESS: - apdu_len += encode_opening_tag(&apdu[apdu_len], 0); - apdu_len += encode_application_signed(&apdu[apdu_len], - data->type.stream.fileStartPosition); - apdu_len += encode_application_unsigned(&apdu[apdu_len], - data->type.stream.requestedOctetCount); - apdu_len += encode_closing_tag(&apdu[apdu_len], 0); - break; - case FILE_RECORD_ACCESS: - apdu_len += encode_opening_tag(&apdu[apdu_len], 1); - apdu_len += encode_application_signed(&apdu[apdu_len], - data->type.record.fileStartRecord); - apdu_len += encode_application_unsigned(&apdu[apdu_len], - data->type.record.RecordCount); - apdu_len += encode_closing_tag(&apdu[apdu_len], 1); - break; - default: - break; + case FILE_STREAM_ACCESS: + apdu_len += encode_opening_tag(&apdu[apdu_len], 0); + apdu_len += encode_application_signed(&apdu[apdu_len], + data->type.stream.fileStartPosition); + apdu_len += encode_application_unsigned(&apdu[apdu_len], + data->type.stream.requestedOctetCount); + apdu_len += encode_closing_tag(&apdu[apdu_len], 0); + break; + case FILE_RECORD_ACCESS: + apdu_len += encode_opening_tag(&apdu[apdu_len], 1); + apdu_len += encode_application_signed(&apdu[apdu_len], + data->type.record.fileStartRecord); + apdu_len += encode_application_unsigned(&apdu[apdu_len], + data->type.record.RecordCount); + apdu_len += encode_closing_tag(&apdu[apdu_len], 1); + break; + default: + break; } } @@ -81,14 +83,16 @@ int arf_encode_apdu(uint8_t * apdu, } /* decode the service request only */ -int arf_decode_service_request(uint8_t * apdu, - unsigned apdu_len, BACNET_ATOMIC_READ_FILE_DATA * data) +int arf_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + BACNET_ATOMIC_READ_FILE_DATA * data) { int len = 0; int tag_len = 0; uint8_t tag_number = 0; uint32_t len_value_type = 0; - int type = 0; /* for decoding */ + int type = 0; /* for decoding */ /* check for value pointers */ if (apdu_len && data) { @@ -154,9 +158,11 @@ int arf_decode_service_request(uint8_t * apdu, return len; } -int arf_decode_apdu(uint8_t * apdu, +int arf_decode_apdu( + uint8_t * apdu, unsigned apdu_len, - uint8_t * invoke_id, BACNET_ATOMIC_READ_FILE_DATA * data) + uint8_t * invoke_id, + BACNET_ATOMIC_READ_FILE_DATA * data) { int len = 0; unsigned offset = 0; @@ -181,10 +187,12 @@ int arf_decode_apdu(uint8_t * apdu, } /* encode service */ -int arf_ack_encode_apdu(uint8_t * apdu, - uint8_t invoke_id, BACNET_ATOMIC_READ_FILE_DATA * data) +int arf_ack_encode_apdu( + uint8_t * apdu, + uint8_t invoke_id, + BACNET_ATOMIC_READ_FILE_DATA * data) { - int apdu_len = 0; /* total length of the apdu, return value */ + int apdu_len = 0; /* total length of the apdu, return value */ if (apdu) { apdu[0] = PDU_TYPE_COMPLEX_ACK; @@ -195,26 +203,26 @@ int arf_ack_encode_apdu(uint8_t * apdu, apdu_len += encode_application_boolean(&apdu[apdu_len], data->endOfFile); switch (data->access) { - case FILE_STREAM_ACCESS: - apdu_len += encode_opening_tag(&apdu[apdu_len], 0); - apdu_len += encode_application_signed(&apdu[apdu_len], - data->type.stream.fileStartPosition); - apdu_len += encode_application_octet_string(&apdu[apdu_len], - &data->fileData); - apdu_len += encode_closing_tag(&apdu[apdu_len], 0); - break; - case FILE_RECORD_ACCESS: - apdu_len += encode_opening_tag(&apdu[apdu_len], 1); - apdu_len += encode_application_signed(&apdu[apdu_len], - data->type.record.fileStartRecord); - apdu_len += encode_application_unsigned(&apdu[apdu_len], - data->type.record.RecordCount); - apdu_len += encode_application_octet_string(&apdu[apdu_len], - &data->fileData); - apdu_len += encode_closing_tag(&apdu[apdu_len], 1); - break; - default: - break; + case FILE_STREAM_ACCESS: + apdu_len += encode_opening_tag(&apdu[apdu_len], 0); + apdu_len += encode_application_signed(&apdu[apdu_len], + data->type.stream.fileStartPosition); + apdu_len += encode_application_octet_string(&apdu[apdu_len], + &data->fileData); + apdu_len += encode_closing_tag(&apdu[apdu_len], 0); + break; + case FILE_RECORD_ACCESS: + apdu_len += encode_opening_tag(&apdu[apdu_len], 1); + apdu_len += encode_application_signed(&apdu[apdu_len], + data->type.record.fileStartRecord); + apdu_len += encode_application_unsigned(&apdu[apdu_len], + data->type.record.RecordCount); + apdu_len += encode_application_octet_string(&apdu[apdu_len], + &data->fileData); + apdu_len += encode_closing_tag(&apdu[apdu_len], 1); + break; + default: + break; } } @@ -222,8 +230,10 @@ int arf_ack_encode_apdu(uint8_t * apdu, } /* decode the service request only */ -int arf_ack_decode_service_request(uint8_t * apdu, - unsigned apdu_len, BACNET_ATOMIC_READ_FILE_DATA * data) +int arf_ack_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + BACNET_ATOMIC_READ_FILE_DATA * data) { int len = 0; int tag_len = 0; @@ -301,9 +311,11 @@ int arf_ack_decode_service_request(uint8_t * apdu, return len; } -int arf_ack_decode_apdu(uint8_t * apdu, +int arf_ack_decode_apdu( + uint8_t * apdu, unsigned apdu_len, - uint8_t * invoke_id, BACNET_ATOMIC_READ_FILE_DATA * data) + uint8_t * invoke_id, + BACNET_ATOMIC_READ_FILE_DATA * data) { int len = 0; unsigned offset = 0; @@ -332,7 +344,8 @@ int arf_ack_decode_apdu(uint8_t * apdu, #include #include "ctest.h" -void testAtomicReadFileAckAccess(Test * pTest, +void testAtomicReadFileAckAccess( + Test * pTest, BACNET_ATOMIC_READ_FILE_DATA * data) { BACNET_ATOMIC_READ_FILE_DATA test_data = { 0 }; @@ -346,8 +359,7 @@ void testAtomicReadFileAckAccess(Test * pTest, ct_test(pTest, len != 0); apdu_len = len; - len = arf_ack_decode_apdu(&apdu[0], - apdu_len, &test_invoke_id, &test_data); + len = arf_ack_decode_apdu(&apdu[0], apdu_len, &test_invoke_id, &test_data); ct_test(pTest, len != -1); ct_test(pTest, test_data.endOfFile == data->endOfFile); ct_test(pTest, test_data.access == data->access); @@ -367,7 +379,8 @@ void testAtomicReadFileAckAccess(Test * pTest, octetstring_length(&test_data.fileData)) == 0); } -void testAtomicReadFileAck(Test * pTest) +void testAtomicReadFileAck( + Test * pTest) { BACNET_ATOMIC_READ_FILE_DATA data = { 0 }; uint8_t test_octet_string[32] = "Joshua-Mary-Anna-Christopher"; @@ -391,7 +404,8 @@ void testAtomicReadFileAck(Test * pTest) return; } -void testAtomicReadFileAccess(Test * pTest, +void testAtomicReadFileAccess( + Test * pTest, BACNET_ATOMIC_READ_FILE_DATA * data) { BACNET_ATOMIC_READ_FILE_DATA test_data = { 0 }; @@ -423,7 +437,8 @@ void testAtomicReadFileAccess(Test * pTest, } } -void testAtomicReadFile(Test * pTest) +void testAtomicReadFile( + Test * pTest) { BACNET_ATOMIC_READ_FILE_DATA data = { 0 }; @@ -445,12 +460,14 @@ void testAtomicReadFile(Test * pTest) } #ifdef TEST_ATOMIC_READ_FILE -uint16_t Device_Max_APDU_Length_Accepted(void) +uint16_t Device_Max_APDU_Length_Accepted( + void) { return MAX_APDU; } -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -469,5 +486,5 @@ int main(void) return 0; } -#endif /* TEST_xxx */ -#endif /* TEST */ +#endif /* TEST_xxx */ +#endif /* TEST */ diff --git a/bacnet-stack/src/awf.c b/bacnet-stack/src/awf.c index d1a78925..10315489 100644 --- a/bacnet-stack/src/awf.c +++ b/bacnet-stack/src/awf.c @@ -41,10 +41,12 @@ /* Atomic Write File */ /* encode service */ -int awf_encode_apdu(uint8_t * apdu, - uint8_t invoke_id, BACNET_ATOMIC_WRITE_FILE_DATA * data) +int awf_encode_apdu( + uint8_t * apdu, + uint8_t invoke_id, + BACNET_ATOMIC_WRITE_FILE_DATA * data) { - int apdu_len = 0; /* total length of the apdu, return value */ + int apdu_len = 0; /* total length of the apdu, return value */ if (apdu) { apdu[0] = PDU_TYPE_CONFIRMED_SERVICE_REQUEST; @@ -56,26 +58,26 @@ int awf_encode_apdu(uint8_t * apdu, apdu_len += encode_application_object_id(&apdu[apdu_len], data->object_type, data->object_instance); switch (data->access) { - case FILE_STREAM_ACCESS: - apdu_len += encode_opening_tag(&apdu[apdu_len], 0); - apdu_len += encode_application_signed(&apdu[apdu_len], - data->type.stream.fileStartPosition); - apdu_len += encode_application_octet_string(&apdu[apdu_len], - &data->fileData); - apdu_len += encode_closing_tag(&apdu[apdu_len], 0); - break; - case FILE_RECORD_ACCESS: - apdu_len += encode_opening_tag(&apdu[apdu_len], 1); - apdu_len += encode_application_signed(&apdu[apdu_len], - data->type.record.fileStartRecord); - apdu_len += encode_application_unsigned(&apdu[apdu_len], - data->type.record.returnedRecordCount); - apdu_len += encode_application_octet_string(&apdu[apdu_len], - &data->fileData); - apdu_len += encode_closing_tag(&apdu[apdu_len], 1); - break; - default: - break; + case FILE_STREAM_ACCESS: + apdu_len += encode_opening_tag(&apdu[apdu_len], 0); + apdu_len += encode_application_signed(&apdu[apdu_len], + data->type.stream.fileStartPosition); + apdu_len += encode_application_octet_string(&apdu[apdu_len], + &data->fileData); + apdu_len += encode_closing_tag(&apdu[apdu_len], 0); + break; + case FILE_RECORD_ACCESS: + apdu_len += encode_opening_tag(&apdu[apdu_len], 1); + apdu_len += encode_application_signed(&apdu[apdu_len], + data->type.record.fileStartRecord); + apdu_len += encode_application_unsigned(&apdu[apdu_len], + data->type.record.returnedRecordCount); + apdu_len += encode_application_octet_string(&apdu[apdu_len], + &data->fileData); + apdu_len += encode_closing_tag(&apdu[apdu_len], 1); + break; + default: + break; } } @@ -83,8 +85,10 @@ int awf_encode_apdu(uint8_t * apdu, } /* decode the service request only */ -int awf_decode_service_request(uint8_t * apdu, - unsigned apdu_len, BACNET_ATOMIC_WRITE_FILE_DATA * data) +int awf_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + BACNET_ATOMIC_WRITE_FILE_DATA * data) { int len = 0; int tag_len = 0; @@ -92,7 +96,7 @@ int awf_decode_service_request(uint8_t * apdu, uint32_t len_value_type = 0; int32_t signed_value = 0; uint32_t unsigned_value = 0; - int type = 0; /* for decoding */ + int type = 0; /* for decoding */ /* check for value pointers */ if (apdu_len && data) { @@ -113,7 +117,7 @@ int awf_decode_service_request(uint8_t * apdu, len += tag_len; if (tag_number != BACNET_APPLICATION_TAG_SIGNED_INT) return -1; - len += decode_signed(&apdu[len], len_value_type, &signed_value); + len += decode_signed(&apdu[len], len_value_type, &signed_value); data->type.stream.fileStartPosition = signed_value; /* fileData */ tag_len = decode_tag_number_and_value(&apdu[len], @@ -145,7 +149,7 @@ int awf_decode_service_request(uint8_t * apdu, len += tag_len; if (tag_number != BACNET_APPLICATION_TAG_UNSIGNED_INT) return -1; - len += decode_unsigned(&apdu[len], len_value_type, + len += decode_unsigned(&apdu[len], len_value_type, &unsigned_value); data->type.record.returnedRecordCount = unsigned_value; /* fileData */ @@ -167,9 +171,11 @@ int awf_decode_service_request(uint8_t * apdu, return len; } -int awf_decode_apdu(uint8_t * apdu, +int awf_decode_apdu( + uint8_t * apdu, unsigned apdu_len, - uint8_t * invoke_id, BACNET_ATOMIC_WRITE_FILE_DATA * data) + uint8_t * invoke_id, + BACNET_ATOMIC_WRITE_FILE_DATA * data) { int len = 0; unsigned offset = 0; @@ -193,10 +199,12 @@ int awf_decode_apdu(uint8_t * apdu, return len; } -int awf_ack_encode_apdu(uint8_t * apdu, - uint8_t invoke_id, BACNET_ATOMIC_WRITE_FILE_DATA * data) +int awf_ack_encode_apdu( + uint8_t * apdu, + uint8_t invoke_id, + BACNET_ATOMIC_WRITE_FILE_DATA * data) { - int apdu_len = 0; /* total length of the apdu, return value */ + int apdu_len = 0; /* total length of the apdu, return value */ if (apdu) { apdu[0] = PDU_TYPE_COMPLEX_ACK; @@ -204,16 +212,16 @@ int awf_ack_encode_apdu(uint8_t * apdu, apdu[2] = SERVICE_CONFIRMED_ATOMIC_WRITE_FILE; /* service choice */ apdu_len = 3; switch (data->access) { - case FILE_STREAM_ACCESS: - apdu_len += encode_context_signed(&apdu[apdu_len], 0, - data->type.stream.fileStartPosition); - break; - case FILE_RECORD_ACCESS: - apdu_len += encode_context_signed(&apdu[apdu_len], 1, - data->type.record.fileStartRecord); - break; - default: - break; + case FILE_STREAM_ACCESS: + apdu_len += encode_context_signed(&apdu[apdu_len], 0, + data->type.stream.fileStartPosition); + break; + case FILE_RECORD_ACCESS: + apdu_len += encode_context_signed(&apdu[apdu_len], 1, + data->type.record.fileStartRecord); + break; + default: + break; } } @@ -221,8 +229,10 @@ int awf_ack_encode_apdu(uint8_t * apdu, } /* decode the service request only */ -int awf_ack_decode_service_request(uint8_t * apdu, - unsigned apdu_len, BACNET_ATOMIC_WRITE_FILE_DATA * data) +int awf_ack_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + BACNET_ATOMIC_WRITE_FILE_DATA * data) { int len = 0; uint8_t tag_number = 0; @@ -248,9 +258,11 @@ int awf_ack_decode_service_request(uint8_t * apdu, return len; } -int awf_ack_decode_apdu(uint8_t * apdu, +int awf_ack_decode_apdu( + uint8_t * apdu, unsigned apdu_len, - uint8_t * invoke_id, BACNET_ATOMIC_WRITE_FILE_DATA * data) + uint8_t * invoke_id, + BACNET_ATOMIC_WRITE_FILE_DATA * data) { int len = 0; unsigned offset = 0; @@ -278,7 +290,8 @@ int awf_ack_decode_apdu(uint8_t * apdu, #include #include "ctest.h" -void testAtomicWriteFileAccess(Test * pTest, +void testAtomicWriteFileAccess( + Test * pTest, BACNET_ATOMIC_WRITE_FILE_DATA * data) { BACNET_ATOMIC_WRITE_FILE_DATA test_data = { 0 }; @@ -313,7 +326,8 @@ void testAtomicWriteFileAccess(Test * pTest, octetstring_length(&test_data.fileData)) == 0); } -void testAtomicWriteFile(Test * pTest) +void testAtomicWriteFile( + Test * pTest) { BACNET_ATOMIC_WRITE_FILE_DATA data = { 0 }; uint8_t test_octet_string[32] = "Joshua-Mary-Anna-Christopher"; @@ -338,7 +352,8 @@ void testAtomicWriteFile(Test * pTest) return; } -void testAtomicWriteFileAckAccess(Test * pTest, +void testAtomicWriteFileAckAccess( + Test * pTest, BACNET_ATOMIC_WRITE_FILE_DATA * data) { BACNET_ATOMIC_WRITE_FILE_DATA test_data = { 0 }; @@ -364,7 +379,8 @@ void testAtomicWriteFileAckAccess(Test * pTest, } } -void testAtomicWriteFileAck(Test * pTest) +void testAtomicWriteFileAck( + Test * pTest) { BACNET_ATOMIC_WRITE_FILE_DATA data = { 0 }; @@ -380,12 +396,14 @@ void testAtomicWriteFileAck(Test * pTest) } #ifdef TEST_ATOMIC_WRITE_FILE -uint16_t Device_Max_APDU_Length_Accepted(void) +uint16_t Device_Max_APDU_Length_Accepted( + void) { return MAX_APDU; } -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -404,5 +422,5 @@ int main(void) return 0; } -#endif /* TEST_WRITE_PROPERTY */ -#endif /* TEST */ +#endif /* TEST_WRITE_PROPERTY */ +#endif /* TEST */ diff --git a/bacnet-stack/src/bacaddr.c b/bacnet-stack/src/bacaddr.c index 8e9cc2ad..a1af1486 100644 --- a/bacnet-stack/src/bacaddr.c +++ b/bacnet-stack/src/bacaddr.c @@ -37,7 +37,9 @@ #include "config.h" #include "bacdef.h" -void bacnet_address_copy(BACNET_ADDRESS * dest, BACNET_ADDRESS * src) +void bacnet_address_copy( + BACNET_ADDRESS * dest, + BACNET_ADDRESS * src) { int i = 0; @@ -54,32 +56,34 @@ void bacnet_address_copy(BACNET_ADDRESS * dest, BACNET_ADDRESS * src) } } -bool bacnet_address_same(BACNET_ADDRESS * dest, BACNET_ADDRESS * src) +bool bacnet_address_same( + BACNET_ADDRESS * dest, + BACNET_ADDRESS * src) { - unsigned i; - unsigned max_len; - bool match = true; /* return value */ - - if (dest->mac_len != src->mac_len) - match = false; - max_len = dest->mac_len; - if (max_len > MAX_MAC_LEN) - max_len = MAX_MAC_LEN; - for (i = 0; i < max_len; i++) { - if (dest->mac[i] != src->mac[i]) - match = false; - } - if (dest->net != src->net) - match = false; - if (dest->len != src->len) - match = false; - max_len = dest->len; - if (max_len > MAX_MAC_LEN) - max_len = MAX_MAC_LEN; - for (i = 0; i < max_len; i++) { - if (dest->adr[i] != src->adr[i]) - match = false; - } - - return match; + unsigned i; + unsigned max_len; + bool match = true; /* return value */ + + if (dest->mac_len != src->mac_len) + match = false; + max_len = dest->mac_len; + if (max_len > MAX_MAC_LEN) + max_len = MAX_MAC_LEN; + for (i = 0; i < max_len; i++) { + if (dest->mac[i] != src->mac[i]) + match = false; + } + if (dest->net != src->net) + match = false; + if (dest->len != src->len) + match = false; + max_len = dest->len; + if (max_len > MAX_MAC_LEN) + max_len = MAX_MAC_LEN; + for (i = 0; i < max_len; i++) { + if (dest->adr[i] != src->adr[i]) + match = false; + } + + return match; } diff --git a/bacnet-stack/src/bacapp.c b/bacnet-stack/src/bacapp.c index c3eac161..3102a6d0 100644 --- a/bacnet-stack/src/bacapp.c +++ b/bacnet-stack/src/bacapp.c @@ -46,92 +46,95 @@ #include "bactext.h" #include "datetime.h" -int bacapp_encode_application_data(uint8_t * apdu, +int bacapp_encode_application_data( + uint8_t * apdu, BACNET_APPLICATION_DATA_VALUE * value) { - int apdu_len = 0; /* total length of the apdu, return value */ + int apdu_len = 0; /* total length of the apdu, return value */ if (value && apdu) { switch (value->tag) { #if defined (BACAPP_NULL) - case BACNET_APPLICATION_TAG_NULL: - apdu[0] = value->tag; - apdu_len++; - break; + case BACNET_APPLICATION_TAG_NULL: + apdu[0] = value->tag; + apdu_len++; + break; #endif #if defined (BACAPP_BOOLEAN) - case BACNET_APPLICATION_TAG_BOOLEAN: - apdu_len = encode_application_boolean(&apdu[0], - value->type.Boolean); - break; + case BACNET_APPLICATION_TAG_BOOLEAN: + apdu_len = encode_application_boolean(&apdu[0], + value->type.Boolean); + break; #endif #if defined (BACAPP_UNSIGNED) - case BACNET_APPLICATION_TAG_UNSIGNED_INT: - apdu_len = encode_application_unsigned(&apdu[0], - value->type.Unsigned_Int); - break; + case BACNET_APPLICATION_TAG_UNSIGNED_INT: + apdu_len = encode_application_unsigned(&apdu[0], + value->type.Unsigned_Int); + break; #endif #if defined (BACAPP_SIGNED) - case BACNET_APPLICATION_TAG_SIGNED_INT: - apdu_len = encode_application_signed(&apdu[0], - value->type.Signed_Int); - break; + case BACNET_APPLICATION_TAG_SIGNED_INT: + apdu_len = encode_application_signed(&apdu[0], + value->type.Signed_Int); + break; #endif #if defined (BACAPP_REAL) - case BACNET_APPLICATION_TAG_REAL: - apdu_len = encode_application_real(&apdu[0], value->type.Real); - break; + case BACNET_APPLICATION_TAG_REAL: + apdu_len = encode_application_real(&apdu[0], value->type.Real); + break; #endif #if defined (BACAPP_DOUBLE) - case BACNET_APPLICATION_TAG_DOUBLE: - /* FIXME: double is not implemented yet. */ - apdu_len = encode_application_double(&apdu[0], - value->type.Double); - break; + case BACNET_APPLICATION_TAG_DOUBLE: + /* FIXME: double is not implemented yet. */ + apdu_len = encode_application_double(&apdu[0], + value->type.Double); + break; #endif -#if defined (BACAPP_OCTET_STRING) - case BACNET_APPLICATION_TAG_OCTET_STRING: - apdu_len = encode_application_octet_string(&apdu[0], - &value->type.Octet_String); - break; +#if defined (BACAPP_OCTET_STRING) + case BACNET_APPLICATION_TAG_OCTET_STRING: + apdu_len = encode_application_octet_string(&apdu[0], + &value->type.Octet_String); + break; #endif -#if defined (BACAPP_CHARACTER_STRING) - case BACNET_APPLICATION_TAG_CHARACTER_STRING: - apdu_len = encode_application_character_string(&apdu[0], - &value->type.Character_String); - break; +#if defined (BACAPP_CHARACTER_STRING) + case BACNET_APPLICATION_TAG_CHARACTER_STRING: + apdu_len = encode_application_character_string(&apdu[0], + &value->type.Character_String); + break; #endif -#if defined (BACAPP_BIT_STRING) - case BACNET_APPLICATION_TAG_BIT_STRING: - apdu_len = encode_application_bitstring(&apdu[0], - &value->type.Bit_String); - break; +#if defined (BACAPP_BIT_STRING) + case BACNET_APPLICATION_TAG_BIT_STRING: + apdu_len = encode_application_bitstring(&apdu[0], + &value->type.Bit_String); + break; #endif #if defined (BACAPP_ENUMERATED) - case BACNET_APPLICATION_TAG_ENUMERATED: - apdu_len = encode_application_enumerated(&apdu[0], - value->type.Enumerated); - break; + case BACNET_APPLICATION_TAG_ENUMERATED: + apdu_len = encode_application_enumerated(&apdu[0], + value->type.Enumerated); + break; #endif -#if defined (BACAPP_DATE) - case BACNET_APPLICATION_TAG_DATE: - apdu_len = encode_application_date(&apdu[0], &value->type.Date); - break; +#if defined (BACAPP_DATE) + case BACNET_APPLICATION_TAG_DATE: + apdu_len = + encode_application_date(&apdu[0], &value->type.Date); + break; #endif -#if defined (BACAPP_TIME) - case BACNET_APPLICATION_TAG_TIME: - apdu_len = encode_application_time(&apdu[0], &value->type.Time); - break; +#if defined (BACAPP_TIME) + case BACNET_APPLICATION_TAG_TIME: + apdu_len = + encode_application_time(&apdu[0], &value->type.Time); + break; #endif -#if defined (BACAPP_OBJECT_ID) - case BACNET_APPLICATION_TAG_OBJECT_ID: - apdu_len = encode_application_object_id(&apdu[0], - value->type.Object_Id.type, - value->type.Object_Id.instance); - break; +#if defined (BACAPP_OBJECT_ID) + case BACNET_APPLICATION_TAG_OBJECT_ID: + apdu_len = encode_application_object_id(&apdu[0], + value->type.Object_Id.type, + value->type.Object_Id.instance); + break; #endif - default: - break; + default: + break; } } @@ -140,9 +143,11 @@ int bacapp_encode_application_data(uint8_t * apdu, /* decode the data and store it into value. Return the number of octets consumed. */ -int bacapp_decode_data(uint8_t * apdu, +int bacapp_decode_data( + uint8_t * apdu, uint8_t tag_data_type, - uint32_t len_value_type, BACNET_APPLICATION_DATA_VALUE * value) + uint32_t len_value_type, + BACNET_APPLICATION_DATA_VALUE * value) { int len = 0; @@ -150,92 +155,94 @@ int bacapp_decode_data(uint8_t * apdu, if (apdu && value) { switch (tag_data_type) { #if defined (BACAPP_NULL) - case BACNET_APPLICATION_TAG_NULL: - /* nothing else to do */ - break; + case BACNET_APPLICATION_TAG_NULL: + /* nothing else to do */ + break; #endif #if defined (BACAPP_BOOLEAN) - case BACNET_APPLICATION_TAG_BOOLEAN: - value->type.Boolean = decode_boolean(len_value_type); - break; + case BACNET_APPLICATION_TAG_BOOLEAN: + value->type.Boolean = decode_boolean(len_value_type); + break; #endif #if defined (BACAPP_UNSIGNED) - case BACNET_APPLICATION_TAG_UNSIGNED_INT: - len = decode_unsigned(&apdu[0], - len_value_type, &value->type.Unsigned_Int); - break; + case BACNET_APPLICATION_TAG_UNSIGNED_INT: + len = decode_unsigned(&apdu[0], + len_value_type, &value->type.Unsigned_Int); + break; #endif #if defined (BACAPP_SIGNED) - case BACNET_APPLICATION_TAG_SIGNED_INT: - len = decode_signed(&apdu[0], - len_value_type, &value->type.Signed_Int); - break; + case BACNET_APPLICATION_TAG_SIGNED_INT: + len = decode_signed(&apdu[0], + len_value_type, &value->type.Signed_Int); + break; #endif #if defined (BACAPP_REAL) - case BACNET_APPLICATION_TAG_REAL: - len = decode_real(&apdu[0], &(value->type.Real)); - break; + case BACNET_APPLICATION_TAG_REAL: + len = decode_real(&apdu[0], &(value->type.Real)); + break; #endif #if defined (BACAPP_DOUBLE) - case BACNET_APPLICATION_TAG_DOUBLE: - len = decode_double(&apdu[0], &(value->type.Double)); - break; + case BACNET_APPLICATION_TAG_DOUBLE: + len = decode_double(&apdu[0], &(value->type.Double)); + break; #endif -#if defined (BACAPP_OCTET_STRING) - case BACNET_APPLICATION_TAG_OCTET_STRING: - len = decode_octet_string(&apdu[0], - len_value_type, &value->type.Octet_String); - break; +#if defined (BACAPP_OCTET_STRING) + case BACNET_APPLICATION_TAG_OCTET_STRING: + len = decode_octet_string(&apdu[0], + len_value_type, &value->type.Octet_String); + break; #endif -#if defined (BACAPP_CHARACTER_STRING) - case BACNET_APPLICATION_TAG_CHARACTER_STRING: - len = decode_character_string(&apdu[0], - len_value_type, &value->type.Character_String); - break; +#if defined (BACAPP_CHARACTER_STRING) + case BACNET_APPLICATION_TAG_CHARACTER_STRING: + len = decode_character_string(&apdu[0], + len_value_type, &value->type.Character_String); + break; #endif -#if defined (BACAPP_BIT_STRING) - case BACNET_APPLICATION_TAG_BIT_STRING: - len = decode_bitstring(&apdu[0], - len_value_type, &value->type.Bit_String); - break; +#if defined (BACAPP_BIT_STRING) + case BACNET_APPLICATION_TAG_BIT_STRING: + len = decode_bitstring(&apdu[0], + len_value_type, &value->type.Bit_String); + break; #endif #if defined (BACAPP_ENUMERATED) - case BACNET_APPLICATION_TAG_ENUMERATED: - len = decode_enumerated(&apdu[0], - len_value_type, &value->type.Enumerated); - break; + case BACNET_APPLICATION_TAG_ENUMERATED: + len = decode_enumerated(&apdu[0], + len_value_type, &value->type.Enumerated); + break; #endif -#if defined (BACAPP_DATE) - case BACNET_APPLICATION_TAG_DATE: - len = decode_date(&apdu[0], &value->type.Date); - break; +#if defined (BACAPP_DATE) + case BACNET_APPLICATION_TAG_DATE: + len = decode_date(&apdu[0], &value->type.Date); + break; #endif -#if defined (BACAPP_TIME) - case BACNET_APPLICATION_TAG_TIME: - len = decode_bacnet_time(&apdu[0], &value->type.Time); - break; +#if defined (BACAPP_TIME) + case BACNET_APPLICATION_TAG_TIME: + len = decode_bacnet_time(&apdu[0], &value->type.Time); + break; #endif -#if defined (BACAPP_OBJECT_ID) - case BACNET_APPLICATION_TAG_OBJECT_ID: - { - int object_type = 0; - uint32_t instance = 0; - len = decode_object_id(&apdu[0], &object_type, &instance); - value->type.Object_Id.type = object_type; - value->type.Object_Id.instance = instance; - } - break; +#if defined (BACAPP_OBJECT_ID) + case BACNET_APPLICATION_TAG_OBJECT_ID: + { + int object_type = 0; + uint32_t instance = 0; + len = decode_object_id(&apdu[0], &object_type, &instance); + value->type.Object_Id.type = object_type; + value->type.Object_Id.instance = instance; + } + break; #endif - default: - break; + default: + break; } } return len; } -int bacapp_decode_application_data(uint8_t * apdu, - int max_apdu_len, BACNET_APPLICATION_DATA_VALUE * value) +int bacapp_decode_application_data( + uint8_t * apdu, + int max_apdu_len, + BACNET_APPLICATION_DATA_VALUE * value) { int len = 0; int tag_len = 0; @@ -260,100 +267,102 @@ int bacapp_decode_application_data(uint8_t * apdu, return len; } -int bacapp_encode_context_data_value(uint8_t * apdu, - uint8_t context_tag_number, BACNET_APPLICATION_DATA_VALUE * value) +int bacapp_encode_context_data_value( + uint8_t * apdu, + uint8_t context_tag_number, + BACNET_APPLICATION_DATA_VALUE * value) { - int apdu_len = 0; /* total length of the apdu, return value */ + int apdu_len = 0; /* total length of the apdu, return value */ if (value && apdu) { switch (value->tag) { #if defined (BACAPP_NULL) - case BACNET_APPLICATION_TAG_NULL: - apdu_len = encode_context_null(&apdu[0], context_tag_number); - break; + case BACNET_APPLICATION_TAG_NULL: + apdu_len = encode_context_null(&apdu[0], context_tag_number); + break; #endif #if defined (BACAPP_BOOLEAN) - case BACNET_APPLICATION_TAG_BOOLEAN: - apdu_len = encode_context_boolean(&apdu[0], context_tag_number, - value->type.Boolean); - break; + case BACNET_APPLICATION_TAG_BOOLEAN: + apdu_len = encode_context_boolean(&apdu[0], context_tag_number, + value->type.Boolean); + break; #endif #if defined (BACAPP_UNSIGNED) - case BACNET_APPLICATION_TAG_UNSIGNED_INT: - apdu_len = - encode_context_unsigned(&apdu[0], context_tag_number, - value->type.Unsigned_Int); - break; + case BACNET_APPLICATION_TAG_UNSIGNED_INT: + apdu_len = + encode_context_unsigned(&apdu[0], context_tag_number, + value->type.Unsigned_Int); + break; #endif #if defined (BACAPP_SIGNED) - case BACNET_APPLICATION_TAG_SIGNED_INT: - apdu_len = encode_context_signed(&apdu[0], context_tag_number, - value->type.Signed_Int); - break; + case BACNET_APPLICATION_TAG_SIGNED_INT: + apdu_len = encode_context_signed(&apdu[0], context_tag_number, + value->type.Signed_Int); + break; #endif #if defined (BACAPP_REAL) - case BACNET_APPLICATION_TAG_REAL: - apdu_len = encode_context_real(&apdu[0], context_tag_number, - value->type.Real); - break; + case BACNET_APPLICATION_TAG_REAL: + apdu_len = encode_context_real(&apdu[0], context_tag_number, + value->type.Real); + break; #endif #if defined (BACAPP_DOUBLE) - case BACNET_APPLICATION_TAG_DOUBLE: - /* FIXME: double is not implemented yet. */ - apdu_len = encode_context_double(&apdu[0], context_tag_number, - value->type.Double); - break; + case BACNET_APPLICATION_TAG_DOUBLE: + /* FIXME: double is not implemented yet. */ + apdu_len = encode_context_double(&apdu[0], context_tag_number, + value->type.Double); + break; #endif -#if defined (BACAPP_OCTET_STRING) - case BACNET_APPLICATION_TAG_OCTET_STRING: - apdu_len = - encode_context_octet_string(&apdu[0], context_tag_number, - &value->type.Octet_String); - break; +#if defined (BACAPP_OCTET_STRING) + case BACNET_APPLICATION_TAG_OCTET_STRING: + apdu_len = + encode_context_octet_string(&apdu[0], context_tag_number, + &value->type.Octet_String); + break; #endif -#if defined (BACAPP_CHARACTER_STRING) - case BACNET_APPLICATION_TAG_CHARACTER_STRING: - apdu_len = - encode_context_character_string(&apdu[0], - context_tag_number, &value->type.Character_String); - break; +#if defined (BACAPP_CHARACTER_STRING) + case BACNET_APPLICATION_TAG_CHARACTER_STRING: + apdu_len = + encode_context_character_string(&apdu[0], + context_tag_number, &value->type.Character_String); + break; #endif -#if defined (BACAPP_BIT_STRING) - case BACNET_APPLICATION_TAG_BIT_STRING: - apdu_len = - encode_context_bitstring(&apdu[0], context_tag_number, - &value->type.Bit_String); - break; +#if defined (BACAPP_BIT_STRING) + case BACNET_APPLICATION_TAG_BIT_STRING: + apdu_len = + encode_context_bitstring(&apdu[0], context_tag_number, + &value->type.Bit_String); + break; #endif #if defined (BACAPP_ENUMERATED) - case BACNET_APPLICATION_TAG_ENUMERATED: - apdu_len = - encode_context_enumerated(&apdu[0], context_tag_number, - value->type.Enumerated); - break; + case BACNET_APPLICATION_TAG_ENUMERATED: + apdu_len = + encode_context_enumerated(&apdu[0], context_tag_number, + value->type.Enumerated); + break; #endif -#if defined (BACAPP_DATE) - case BACNET_APPLICATION_TAG_DATE: - apdu_len = encode_context_date(&apdu[0], context_tag_number, - &value->type.Date); - break; +#if defined (BACAPP_DATE) + case BACNET_APPLICATION_TAG_DATE: + apdu_len = encode_context_date(&apdu[0], context_tag_number, + &value->type.Date); + break; #endif -#if defined (BACAPP_TIME) - case BACNET_APPLICATION_TAG_TIME: - apdu_len = encode_context_time(&apdu[0], context_tag_number, - &value->type.Time); - break; +#if defined (BACAPP_TIME) + case BACNET_APPLICATION_TAG_TIME: + apdu_len = encode_context_time(&apdu[0], context_tag_number, + &value->type.Time); + break; #endif -#if defined (BACAPP_OBJECT_ID) - case BACNET_APPLICATION_TAG_OBJECT_ID: - apdu_len = - encode_context_object_id(&apdu[0], context_tag_number, - value->type.Object_Id.type, - value->type.Object_Id.instance); - break; +#if defined (BACAPP_OBJECT_ID) + case BACNET_APPLICATION_TAG_OBJECT_ID: + apdu_len = + encode_context_object_id(&apdu[0], context_tag_number, + value->type.Object_Id.type, + value->type.Object_Id.instance); + break; #endif - default: - break; + default: + break; } } @@ -361,78 +370,80 @@ int bacapp_encode_context_data_value(uint8_t * apdu, } /* returns the fixed tag type for certain context tagged properties */ -BACNET_APPLICATION_TAG bacapp_context_tag_type(BACNET_PROPERTY_ID property, +BACNET_APPLICATION_TAG bacapp_context_tag_type( + BACNET_PROPERTY_ID property, uint8_t tag_number) { BACNET_APPLICATION_TAG tag = MAX_BACNET_APPLICATION_TAG; switch (property) { - case PROP_REQUESTED_SHED_LEVEL: - switch (tag_number) { - case 0: - case 1: - tag = BACNET_APPLICATION_TAG_UNSIGNED_INT; + case PROP_REQUESTED_SHED_LEVEL: + switch (tag_number) { + case 0: + case 1: + tag = BACNET_APPLICATION_TAG_UNSIGNED_INT; + break; + case 2: + tag = BACNET_APPLICATION_TAG_REAL; + break; + default: + break; + } break; - case 2: - tag = BACNET_APPLICATION_TAG_REAL; + case PROP_ACTION: + switch (tag_number) { + case 0: + case 1: + tag = BACNET_APPLICATION_TAG_OBJECT_ID; + break; + case 2: + tag = BACNET_APPLICATION_TAG_ENUMERATED; + break; + case 3: + case 5: + case 6: + tag = BACNET_APPLICATION_TAG_UNSIGNED_INT; + break; + case 7: + case 8: + tag = BACNET_APPLICATION_TAG_BOOLEAN; + break; + case 4: /* propertyValue: abstract syntax */ + default: + break; + } + break; + case PROP_EXCEPTION_SCHEDULE: + switch (tag_number) { + case 1: + tag = BACNET_APPLICATION_TAG_OBJECT_ID; + break; + case 3: + tag = BACNET_APPLICATION_TAG_UNSIGNED_INT; + break; + case 0: /* calendarEntry: abstract syntax + context */ + case 2: /* list of BACnetTimeValue: abstract syntax */ + default: + break; + } break; default: break; - } - break; - case PROP_ACTION: - switch (tag_number) { - case 0: - case 1: - tag = BACNET_APPLICATION_TAG_OBJECT_ID; - break; - case 2: - tag = BACNET_APPLICATION_TAG_ENUMERATED; - break; - case 3: - case 5: - case 6: - tag = BACNET_APPLICATION_TAG_UNSIGNED_INT; - break; - case 7: - case 8: - tag = BACNET_APPLICATION_TAG_BOOLEAN; - break; - case 4: /* propertyValue: abstract syntax */ - default: - break; - } - break; - case PROP_EXCEPTION_SCHEDULE: - switch (tag_number) { - case 1: - tag = BACNET_APPLICATION_TAG_OBJECT_ID; - break; - case 3: - tag = BACNET_APPLICATION_TAG_UNSIGNED_INT; - break; - case 0: /* calendarEntry: abstract syntax + context */ - case 2: /* list of BACnetTimeValue: abstract syntax */ - default: - break; - } - break; - default: - break; } return tag; } -int bacapp_encode_context_data(uint8_t * apdu, - BACNET_APPLICATION_DATA_VALUE * value, BACNET_PROPERTY_ID property) +int bacapp_encode_context_data( + uint8_t * apdu, + BACNET_APPLICATION_DATA_VALUE * value, + BACNET_PROPERTY_ID property) { int apdu_len = 0; BACNET_APPLICATION_TAG tag_data_type; if (value && apdu) { - tag_data_type = - bacapp_context_tag_type(property, value->context_tag); + tag_data_type = bacapp_context_tag_type(property, value->context_tag); if (tag_data_type < MAX_BACNET_APPLICATION_TAG) { apdu_len = bacapp_encode_context_data_value(&apdu[0], value->context_tag, value); @@ -446,8 +457,10 @@ int bacapp_encode_context_data(uint8_t * apdu, return apdu_len; } -int bacapp_decode_context_data(uint8_t * apdu, - int max_apdu_len, BACNET_APPLICATION_DATA_VALUE * value, +int bacapp_decode_context_data( + uint8_t * apdu, + int max_apdu_len, + BACNET_APPLICATION_DATA_VALUE * value, BACNET_PROPERTY_ID property) { int apdu_len = 0, len = 0; @@ -480,10 +493,11 @@ int bacapp_decode_context_data(uint8_t * apdu, return apdu_len; } -int bacapp_encode_data(uint8_t * apdu, +int bacapp_encode_data( + uint8_t * apdu, BACNET_APPLICATION_DATA_VALUE * value) { - int apdu_len = 0; /* total length of the apdu, return value */ + int apdu_len = 0; /* total length of the apdu, return value */ if (value && apdu) { if (value->context_specific) { @@ -498,89 +512,90 @@ int bacapp_encode_data(uint8_t * apdu, } -bool bacapp_copy(BACNET_APPLICATION_DATA_VALUE * dest_value, +bool bacapp_copy( + BACNET_APPLICATION_DATA_VALUE * dest_value, BACNET_APPLICATION_DATA_VALUE * src_value) { - bool status = true; /*return value */ + bool status = true; /*return value */ if (dest_value && src_value) { dest_value->tag = src_value->tag; switch (src_value->tag) { #if defined (BACAPP_NULL) - case BACNET_APPLICATION_TAG_NULL: - break; + case BACNET_APPLICATION_TAG_NULL: + break; #endif #if defined (BACAPP_BOOLEAN) - case BACNET_APPLICATION_TAG_BOOLEAN: - dest_value->type.Boolean = src_value->type.Boolean; - break; + case BACNET_APPLICATION_TAG_BOOLEAN: + dest_value->type.Boolean = src_value->type.Boolean; + break; #endif #if defined (BACAPP_UNSIGNED) - case BACNET_APPLICATION_TAG_UNSIGNED_INT: - dest_value->type.Unsigned_Int = src_value->type.Unsigned_Int; - break; + case BACNET_APPLICATION_TAG_UNSIGNED_INT: + dest_value->type.Unsigned_Int = src_value->type.Unsigned_Int; + break; #endif #if defined (BACAPP_SIGNED) - case BACNET_APPLICATION_TAG_SIGNED_INT: - dest_value->type.Signed_Int = src_value->type.Signed_Int; - break; + case BACNET_APPLICATION_TAG_SIGNED_INT: + dest_value->type.Signed_Int = src_value->type.Signed_Int; + break; #endif #if defined (BACAPP_REAL) - case BACNET_APPLICATION_TAG_REAL: - dest_value->type.Real = src_value->type.Real; - break; + case BACNET_APPLICATION_TAG_REAL: + dest_value->type.Real = src_value->type.Real; + break; #endif #if defined (BACAPP_DOUBLE) - case BACNET_APPLICATION_TAG_DOUBLE: - dest_value->type.Double = src_value->type.Double; - break; + case BACNET_APPLICATION_TAG_DOUBLE: + dest_value->type.Double = src_value->type.Double; + break; #endif -#if defined (BACAPP_OCTET_STRING) - case BACNET_APPLICATION_TAG_OCTET_STRING: - octetstring_copy(&dest_value->type.Octet_String, - &src_value->type.Octet_String); - break; +#if defined (BACAPP_OCTET_STRING) + case BACNET_APPLICATION_TAG_OCTET_STRING: + octetstring_copy(&dest_value->type.Octet_String, + &src_value->type.Octet_String); + break; #endif -#if defined (BACAPP_CHARACTER_STRING) - case BACNET_APPLICATION_TAG_CHARACTER_STRING: - characterstring_copy(&dest_value->type.Character_String, - &src_value->type.Character_String); - break; +#if defined (BACAPP_CHARACTER_STRING) + case BACNET_APPLICATION_TAG_CHARACTER_STRING: + characterstring_copy(&dest_value->type.Character_String, + &src_value->type.Character_String); + break; #endif -#if defined (BACAPP_BIT_STRING) - case BACNET_APPLICATION_TAG_BIT_STRING: - bitstring_copy(&dest_value->type.Bit_String, - &src_value->type.Bit_String); - break; +#if defined (BACAPP_BIT_STRING) + case BACNET_APPLICATION_TAG_BIT_STRING: + bitstring_copy(&dest_value->type.Bit_String, + &src_value->type.Bit_String); + break; #endif #if defined (BACAPP_ENUMERATED) - case BACNET_APPLICATION_TAG_ENUMERATED: - dest_value->type.Enumerated = src_value->type.Enumerated; - break; + case BACNET_APPLICATION_TAG_ENUMERATED: + dest_value->type.Enumerated = src_value->type.Enumerated; + break; #endif -#if defined (BACAPP_DATE) - case BACNET_APPLICATION_TAG_DATE: - datetime_copy_date(&dest_value->type.Date, - &src_value->type.Date); - break; +#if defined (BACAPP_DATE) + case BACNET_APPLICATION_TAG_DATE: + datetime_copy_date(&dest_value->type.Date, + &src_value->type.Date); + break; #endif -#if defined (BACAPP_TIME) - case BACNET_APPLICATION_TAG_TIME: - datetime_copy_time(&dest_value->type.Time, - &src_value->type.Time); - break; +#if defined (BACAPP_TIME) + case BACNET_APPLICATION_TAG_TIME: + datetime_copy_time(&dest_value->type.Time, + &src_value->type.Time); + break; #endif -#if defined (BACAPP_OBJECT_ID) - case BACNET_APPLICATION_TAG_OBJECT_ID: - dest_value->type.Object_Id.type = - src_value->type.Object_Id.type; - dest_value->type.Object_Id.instance = - src_value->type.Object_Id.instance; - break; +#if defined (BACAPP_OBJECT_ID) + case BACNET_APPLICATION_TAG_OBJECT_ID: + dest_value->type.Object_Id.type = + src_value->type.Object_Id.type; + dest_value->type.Object_Id.instance = + src_value->type.Object_Id.instance; + break; #endif - default: - status = false; - break; + default: + status = false; + break; } dest_value->next = src_value->next; } @@ -592,7 +607,9 @@ bool bacapp_copy(BACNET_APPLICATION_DATA_VALUE * dest_value, Expects that the first octet contain the opening tag. Include a value property identifier for context specific data such as the value received in a WriteProperty request */ -int bacapp_data_len(uint8_t * apdu, int max_apdu_len, +int bacapp_data_len( + uint8_t * apdu, + int max_apdu_len, BACNET_PROPERTY_ID property) { int len = 0; @@ -652,128 +669,132 @@ int bacapp_data_len(uint8_t * apdu, int max_apdu_len, } #ifdef BACAPP_PRINT_ENABLED -bool bacapp_print_value(FILE * stream, - BACNET_APPLICATION_DATA_VALUE * value, BACNET_PROPERTY_ID property) +bool bacapp_print_value( + FILE * stream, + BACNET_APPLICATION_DATA_VALUE * value, + BACNET_PROPERTY_ID property) { - bool status = true; /*return value */ + bool status = true; /*return value */ size_t len = 0, i = 0; char *char_str; uint8_t *octet_str; if (value) { switch (value->tag) { - case BACNET_APPLICATION_TAG_NULL: - fprintf(stream, "Null"); - break; - case BACNET_APPLICATION_TAG_BOOLEAN: - fprintf(stream, "%s", value->type.Boolean ? "TRUE" : "FALSE"); - break; - case BACNET_APPLICATION_TAG_UNSIGNED_INT: - fprintf(stream, "%u", value->type.Unsigned_Int); - break; - case BACNET_APPLICATION_TAG_SIGNED_INT: - fprintf(stream, "%d", value->type.Signed_Int); - break; - case BACNET_APPLICATION_TAG_REAL: - fprintf(stream, "%f", (double) value->type.Real); - break; + case BACNET_APPLICATION_TAG_NULL: + fprintf(stream, "Null"); + break; + case BACNET_APPLICATION_TAG_BOOLEAN: + fprintf(stream, "%s", value->type.Boolean ? "TRUE" : "FALSE"); + break; + case BACNET_APPLICATION_TAG_UNSIGNED_INT: + fprintf(stream, "%u", value->type.Unsigned_Int); + break; + case BACNET_APPLICATION_TAG_SIGNED_INT: + fprintf(stream, "%d", value->type.Signed_Int); + break; + case BACNET_APPLICATION_TAG_REAL: + fprintf(stream, "%f", (double) value->type.Real); + break; #if defined (BACAPP_DOUBLE) - case BACNET_APPLICATION_TAG_DOUBLE: - fprintf(stream, "%f", value->type.Double); - break; + case BACNET_APPLICATION_TAG_DOUBLE: + fprintf(stream, "%f", value->type.Double); + break; #endif - case BACNET_APPLICATION_TAG_OCTET_STRING: - len = octetstring_length(&value->type.Octet_String); - octet_str = octetstring_value(&value->type.Octet_String); - for (i = 0; i < len; i++) { - fprintf(stream, "%02X", *octet_str); - octet_str++; - } - break; - case BACNET_APPLICATION_TAG_CHARACTER_STRING: - len = characterstring_length(&value->type.Character_String); - char_str = - characterstring_value(&value->type.Character_String); - fprintf(stream, "\""); - for (i = 0; i < len; i++) { - if (isprint(*char_str)) - fprintf(stream, "%c", *char_str); - else - fprintf(stream, "."); - char_str++; - } - fprintf(stream, "\""); - break; - case BACNET_APPLICATION_TAG_BIT_STRING: - len = bitstring_bits_used(&value->type.Bit_String); - fprintf(stream, "{"); - for (i = 0; i < len; i++) { - fprintf(stream, "%s", - bitstring_bit(&value->type.Bit_String, - (uint8_t) i) ? "true" : "false"); - if (i < len - 1) - fprintf(stream, ","); - } - fprintf(stream, "}"); - break; - case BACNET_APPLICATION_TAG_ENUMERATED: - switch (property) { - case PROP_OBJECT_TYPE: - fprintf(stream, "%s", - bactext_object_type_name(value->type.Enumerated)); + case BACNET_APPLICATION_TAG_OCTET_STRING: + len = octetstring_length(&value->type.Octet_String); + octet_str = octetstring_value(&value->type.Octet_String); + for (i = 0; i < len; i++) { + fprintf(stream, "%02X", *octet_str); + octet_str++; + } break; - case PROP_EVENT_STATE: - fprintf(stream, "%s", - bactext_event_state_name(value->type.Enumerated)); + case BACNET_APPLICATION_TAG_CHARACTER_STRING: + len = characterstring_length(&value->type.Character_String); + char_str = + characterstring_value(&value->type.Character_String); + fprintf(stream, "\""); + for (i = 0; i < len; i++) { + if (isprint(*char_str)) + fprintf(stream, "%c", *char_str); + else + fprintf(stream, "."); + char_str++; + } + fprintf(stream, "\""); break; - case PROP_UNITS: - fprintf(stream, "%s", - bactext_engineering_unit_name(value->type.Enumerated)); + case BACNET_APPLICATION_TAG_BIT_STRING: + len = bitstring_bits_used(&value->type.Bit_String); + fprintf(stream, "{"); + for (i = 0; i < len; i++) { + fprintf(stream, "%s", + bitstring_bit(&value->type.Bit_String, + (uint8_t) i) ? "true" : "false"); + if (i < len - 1) + fprintf(stream, ","); + } + fprintf(stream, "}"); break; - case PROP_PRESENT_VALUE: - fprintf(stream, "%s", - bactext_binary_present_value_name(value->type. - Enumerated)); + case BACNET_APPLICATION_TAG_ENUMERATED: + switch (property) { + case PROP_OBJECT_TYPE: + fprintf(stream, "%s", + bactext_object_type_name(value->type.Enumerated)); + break; + case PROP_EVENT_STATE: + fprintf(stream, "%s", + bactext_event_state_name(value->type.Enumerated)); + break; + case PROP_UNITS: + fprintf(stream, "%s", + bactext_engineering_unit_name(value->type. + Enumerated)); + break; + case PROP_PRESENT_VALUE: + fprintf(stream, "%s", + bactext_binary_present_value_name(value->type. + Enumerated)); + break; + case PROP_RELIABILITY: + fprintf(stream, "%s", + bactext_reliability_name(value->type.Enumerated)); + break; + case PROP_SYSTEM_STATUS: + fprintf(stream, "%s", + bactext_device_status_name(value->type. + Enumerated)); + break; + case PROP_SEGMENTATION_SUPPORTED: + fprintf(stream, "%s", + bactext_segmentation_name(value->type.Enumerated)); + break; + default: + fprintf(stream, "%u", value->type.Enumerated); + break; + } break; - case PROP_RELIABILITY: - fprintf(stream, "%s", - bactext_reliability_name(value->type.Enumerated)); + case BACNET_APPLICATION_TAG_DATE: + fprintf(stream, "%s, %s %u, %u", + bactext_day_of_week_name(value->type.Date.wday), + bactext_month_name(value->type.Date.month), + (unsigned) value->type.Date.day, + (unsigned) value->type.Date.year); break; - case PROP_SYSTEM_STATUS: - fprintf(stream, "%s", - bactext_device_status_name(value->type.Enumerated)); + case BACNET_APPLICATION_TAG_TIME: + fprintf(stream, "%02u:%02u:%02u.%03u", + (unsigned) value->type.Time.hour, + (unsigned) value->type.Time.min, + (unsigned) value->type.Time.sec, + (unsigned) value->type.Time.hundredths); break; - case PROP_SEGMENTATION_SUPPORTED: - fprintf(stream, "%s", - bactext_segmentation_name(value->type.Enumerated)); + case BACNET_APPLICATION_TAG_OBJECT_ID: + fprintf(stream, "%s %u", + bactext_object_type_name(value->type.Object_Id.type), + value->type.Object_Id.instance); break; default: - fprintf(stream, "%u", value->type.Enumerated); + status = false; break; - } - break; - case BACNET_APPLICATION_TAG_DATE: - fprintf(stream, "%s, %s %u, %u", - bactext_day_of_week_name(value->type.Date.wday), - bactext_month_name(value->type.Date.month), - (unsigned) value->type.Date.day, - (unsigned) value->type.Date.year); - break; - case BACNET_APPLICATION_TAG_TIME: - fprintf(stream, "%02u:%02u:%02u.%03u", - (unsigned) value->type.Time.hour, - (unsigned) value->type.Time.min, - (unsigned) value->type.Time.sec, - (unsigned) value->type.Time.hundredths); - break; - case BACNET_APPLICATION_TAG_OBJECT_ID: - fprintf(stream, "%s %u", - bactext_object_type_name(value->type.Object_Id.type), - value->type.Object_Id.instance); - break; - default: - status = false; - break; } } @@ -784,8 +805,10 @@ bool bacapp_print_value(FILE * stream, #ifdef BACAPP_PRINT_ENABLED /* used to load the app data struct with the proper data converted from a command line argument */ -bool bacapp_parse_application_data(BACNET_APPLICATION_TAG tag_number, - const char *argv, BACNET_APPLICATION_DATA_VALUE * value) +bool bacapp_parse_application_data( + BACNET_APPLICATION_TAG tag_number, + const char *argv, + BACNET_APPLICATION_DATA_VALUE * value) { int hour, min, sec, hundredths; int year, month, day, wday; @@ -808,7 +831,7 @@ bool bacapp_parse_application_data(BACNET_APPLICATION_TAG tag_number, else value->type.Boolean = false; break; - case BACNET_APPLICATION_TAG_UNSIGNED_INT: + case BACNET_APPLICATION_TAG_UNSIGNED_INT: unsigned_long_value = strtoul(argv, NULL, 0); value->type.Unsigned_Int = unsigned_long_value; break; @@ -848,8 +871,8 @@ bool bacapp_parse_application_data(BACNET_APPLICATION_TAG tag_number, count = sscanf(argv, "%d/%d/%d:%d", &year, &month, &day, &wday); if (count == 3) { - datetime_set_date(&value->type.Date, - (uint16_t)year, (uint8_t)month, (uint8_t)day); + datetime_set_date(&value->type.Date, + (uint16_t) year, (uint8_t) month, (uint8_t) day); } else if (count == 4) { value->type.Date.year = year; value->type.Date.month = month; @@ -882,7 +905,7 @@ bool bacapp_parse_application_data(BACNET_APPLICATION_TAG tag_number, status = false; } break; - case BACNET_APPLICATION_TAG_OBJECT_ID: + case BACNET_APPLICATION_TAG_OBJECT_ID: count = sscanf(argv, "%d:%d", &object_type, &instance); if (count == 2) { value->type.Object_Id.type = object_type; @@ -908,10 +931,11 @@ bool bacapp_parse_application_data(BACNET_APPLICATION_TAG tag_number, /* generic - can be used by other unit tests returns true if matching or same, false if different */ -bool bacapp_same_value(BACNET_APPLICATION_DATA_VALUE * value, +bool bacapp_same_value( + BACNET_APPLICATION_DATA_VALUE * value, BACNET_APPLICATION_DATA_VALUE * test_value) { - bool status = false; /*return value */ + bool status = false; /*return value */ /* does the tag match? */ if (test_value->tag == value->tag) @@ -922,92 +946,93 @@ bool bacapp_same_value(BACNET_APPLICATION_DATA_VALUE * value, /* does the value match? */ switch (test_value->tag) { #if defined (BACAPP_NULL) - case BACNET_APPLICATION_TAG_NULL: - status = true; - break; + case BACNET_APPLICATION_TAG_NULL: + status = true; + break; #endif #if defined (BACAPP_BOOLEAN) - case BACNET_APPLICATION_TAG_BOOLEAN: - if (test_value->type.Boolean == value->type.Boolean) - status = true; - break; + case BACNET_APPLICATION_TAG_BOOLEAN: + if (test_value->type.Boolean == value->type.Boolean) + status = true; + break; #endif #if defined (BACAPP_UNSIGNED) - case BACNET_APPLICATION_TAG_UNSIGNED_INT: - if (test_value->type.Unsigned_Int == value->type.Unsigned_Int) - status = true; - break; + case BACNET_APPLICATION_TAG_UNSIGNED_INT: + if (test_value->type.Unsigned_Int == value->type.Unsigned_Int) + status = true; + break; #endif #if defined (BACAPP_SIGNED) - case BACNET_APPLICATION_TAG_SIGNED_INT: - if (test_value->type.Signed_Int == value->type.Signed_Int) - status = true; - break; + case BACNET_APPLICATION_TAG_SIGNED_INT: + if (test_value->type.Signed_Int == value->type.Signed_Int) + status = true; + break; #endif #if defined (BACAPP_REAL) - case BACNET_APPLICATION_TAG_REAL: - if (test_value->type.Real == value->type.Real) - status = true; - break; + case BACNET_APPLICATION_TAG_REAL: + if (test_value->type.Real == value->type.Real) + status = true; + break; #endif #if defined (BACAPP_DOUBLE) - case BACNET_APPLICATION_TAG_DOUBLE: - if (test_value->type.Double == value->type.Double) - status = true; - break; + case BACNET_APPLICATION_TAG_DOUBLE: + if (test_value->type.Double == value->type.Double) + status = true; + break; #endif #if defined (BACAPP_ENUMERATED) - case BACNET_APPLICATION_TAG_ENUMERATED: - if (test_value->type.Enumerated == value->type.Enumerated) - status = true; - break; + case BACNET_APPLICATION_TAG_ENUMERATED: + if (test_value->type.Enumerated == value->type.Enumerated) + status = true; + break; #endif -#if defined (BACAPP_DATE) - case BACNET_APPLICATION_TAG_DATE: - if (datetime_compare_date(&test_value->type.Date, - &value->type.Date) == 0) - status = true; - break; +#if defined (BACAPP_DATE) + case BACNET_APPLICATION_TAG_DATE: + if (datetime_compare_date(&test_value->type.Date, + &value->type.Date) == 0) + status = true; + break; #endif -#if defined (BACAPP_TIME) - case BACNET_APPLICATION_TAG_TIME: - if (datetime_compare_time(&test_value->type.Time, - &value->type.Time) == 0) - status = true; - break; +#if defined (BACAPP_TIME) + case BACNET_APPLICATION_TAG_TIME: + if (datetime_compare_time(&test_value->type.Time, + &value->type.Time) == 0) + status = true; + break; #endif -#if defined (BACAPP_OBJECT_ID) - case BACNET_APPLICATION_TAG_OBJECT_ID: - if ((test_value->type.Object_Id.type == - value->type.Object_Id.type) && - (test_value->type.Object_Id.instance == - value->type.Object_Id.instance)) { - status = true; - } - break; +#if defined (BACAPP_OBJECT_ID) + case BACNET_APPLICATION_TAG_OBJECT_ID: + if ((test_value->type.Object_Id.type == + value->type.Object_Id.type) && + (test_value->type.Object_Id.instance == + value->type.Object_Id.instance)) { + status = true; + } + break; #endif -#if defined (BACAPP_CHARACTER_STRING) - case BACNET_APPLICATION_TAG_CHARACTER_STRING: - status = characterstring_same(&value->type.Character_String, - &test_value->type.Character_String); - break; +#if defined (BACAPP_CHARACTER_STRING) + case BACNET_APPLICATION_TAG_CHARACTER_STRING: + status = characterstring_same(&value->type.Character_String, + &test_value->type.Character_String); + break; #endif -#if defined (BACAPP_BIT_STRING) - case BACNET_APPLICATION_TAG_BIT_STRING: - default: - status = false; - break; +#if defined (BACAPP_BIT_STRING) + case BACNET_APPLICATION_TAG_BIT_STRING: + default: + status = false; + break; } } #endif return status; } -void testBACnetApplicationDataLength(Test * pTest) +void testBACnetApplicationDataLength( + Test * pTest) { - int apdu_len = 0; /* total length of the apdu, return value */ - int len = 0; /* total length of the apdu, return value */ - int test_len = 0; /* length of the data */ + int apdu_len = 0; /* total length of the apdu, return value */ + int len = 0; /* total length of the apdu, return value */ + int test_len = 0; /* length of the data */ uint8_t apdu[480] = { 0 }; BACNET_TIME local_time; BACNET_DATE local_date; @@ -1108,7 +1133,7 @@ void testBACnetApplicationDataLength(Test * pTest) apdu_len += len; local_date.year = 2006; /* AD */ local_date.month = 4; /* 1=Jan */ - local_date.day = 1; /* 1..31 */ + local_date.day = 1; /* 1..31 */ local_date.wday = 6; /* 1=Monday */ len = encode_application_date(&apdu[apdu_len], &local_date); test_len += len; @@ -1148,8 +1173,8 @@ void testBACnetApplicationDataLength(Test * pTest) ct_test(pTest, test_len == len); } -static bool testBACnetApplicationDataValue(BACNET_APPLICATION_DATA_VALUE * - value) +static bool testBACnetApplicationDataValue( + BACNET_APPLICATION_DATA_VALUE * value) { uint8_t apdu[480] = { 0 }; int len = 0; @@ -1162,7 +1187,8 @@ static bool testBACnetApplicationDataValue(BACNET_APPLICATION_DATA_VALUE * return bacapp_same_value(value, &test_value); } -void testBACnetApplicationData(Test * pTest) +void testBACnetApplicationData( + Test * pTest) { BACNET_APPLICATION_DATA_VALUE value; bool status = false; @@ -1344,7 +1370,8 @@ void testBACnetApplicationData(Test * pTest) } #ifdef TEST_BACNET_APPLICATION_DATA -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -1362,5 +1389,5 @@ int main(void) return 0; } -#endif /* TEST_BACNET_APPLICATION_DATA */ -#endif /* TEST */ +#endif /* TEST_BACNET_APPLICATION_DATA */ +#endif /* TEST */ diff --git a/bacnet-stack/src/bacdcode.c b/bacnet-stack/src/bacdcode.c index 3e73782e..b3c6d04b 100644 --- a/bacnet-stack/src/bacdcode.c +++ b/bacnet-stack/src/bacdcode.c @@ -74,7 +74,9 @@ /* from clause 20.1.2.4 max-segments-accepted */ /* and clause 20.1.2.5 max-APDU-length-accepted */ /* returns the encoded octet */ -uint8_t encode_max_segs_max_apdu(int max_segs, int max_apdu) +uint8_t encode_max_segs_max_apdu( + int max_segs, + int max_apdu) { uint8_t octet = 0; @@ -118,67 +120,69 @@ uint8_t encode_max_segs_max_apdu(int max_segs, int max_apdu) /* from clause 20.1.2.4 max-segments-accepted */ /* and clause 20.1.2.5 max-APDU-length-accepted */ /* returns the encoded octet */ -int decode_max_segs(uint8_t octet) +int decode_max_segs( + uint8_t octet) { int max_segs = 0; switch (octet & 0xF0) { - case 0: - max_segs = 0; - break; - case 0x10: - max_segs = 2; - break; - case 0x20: - max_segs = 4; - break; - case 0x30: - max_segs = 8; - break; - case 0x40: - max_segs = 16; - break; - case 0x50: - max_segs = 32; - break; - case 0x60: - max_segs = 64; - break; - case 0x70: - max_segs = 65; - break; - default: - break; + case 0: + max_segs = 0; + break; + case 0x10: + max_segs = 2; + break; + case 0x20: + max_segs = 4; + break; + case 0x30: + max_segs = 8; + break; + case 0x40: + max_segs = 16; + break; + case 0x50: + max_segs = 32; + break; + case 0x60: + max_segs = 64; + break; + case 0x70: + max_segs = 65; + break; + default: + break; } return max_segs; } -int decode_max_apdu(uint8_t octet) +int decode_max_apdu( + uint8_t octet) { int max_apdu = 0; switch (octet & 0x0F) { - case 0: - max_apdu = 50; - break; - case 1: - max_apdu = 128; - break; - case 2: - max_apdu = 206; - break; - case 3: - max_apdu = 480; - break; - case 4: - max_apdu = 1024; - break; - case 5: - max_apdu = 1476; - break; - default: - break; + case 0: + max_apdu = 50; + break; + case 1: + max_apdu = 128; + break; + case 2: + max_apdu = 206; + break; + case 3: + max_apdu = 480; + break; + case 4: + max_apdu = 1024; + break; + case 5: + max_apdu = 1476; + break; + default: + break; } return max_apdu; @@ -186,10 +190,13 @@ int decode_max_apdu(uint8_t octet) /* from clause 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -int encode_tag(uint8_t * apdu, uint8_t tag_number, bool context_specific, +int encode_tag( + uint8_t * apdu, + uint8_t tag_number, + bool context_specific, uint32_t len_value_type) { - int len = 1; /* return value */ + int len = 1; /* return value */ apdu[0] = 0; if (context_specific) @@ -215,8 +222,7 @@ int encode_tag(uint8_t * apdu, uint8_t tag_number, bool context_specific, apdu[len++] = (uint8_t) len_value_type; } else if (len_value_type <= 65535) { apdu[len++] = 254; - len += - encode_unsigned16(&apdu[len], (uint16_t) len_value_type); + len += encode_unsigned16(&apdu[len], (uint16_t) len_value_type); } else { apdu[len++] = 255; len += encode_unsigned32(&apdu[len], len_value_type); @@ -228,7 +234,9 @@ int encode_tag(uint8_t * apdu, uint8_t tag_number, bool context_specific, /* from clause 20.2.1.3.2 Constructed Data */ /* returns the number of apdu bytes consumed */ -int encode_opening_tag(uint8_t * apdu, uint8_t tag_number) +int encode_opening_tag( + uint8_t * apdu, + uint8_t tag_number) { int len = 1; @@ -250,7 +258,9 @@ int encode_opening_tag(uint8_t * apdu, uint8_t tag_number) /* from clause 20.2.1.3.2 Constructed Data */ /* returns the number of apdu bytes consumed */ -int encode_closing_tag(uint8_t * apdu, uint8_t tag_number) +int encode_closing_tag( + uint8_t * apdu, + uint8_t tag_number) { int len = 1; @@ -272,28 +282,33 @@ int encode_closing_tag(uint8_t * apdu, uint8_t tag_number) /* from clause 20.2.1.3.2 Constructed Data */ /* returns true if extended tag numbering is used */ -static bool decode_is_extended_tag_number(uint8_t * apdu) +static bool decode_is_extended_tag_number( + uint8_t * apdu) { return ((apdu[0] & 0xF0) == 0xF0); } /* from clause 20.2.1.3.2 Constructed Data */ /* returns true if the extended value is used */ -static bool decode_is_extended_value(uint8_t * apdu) +static bool decode_is_extended_value( + uint8_t * apdu) { return ((apdu[0] & 0x07) == 5); } /* from clause 20.2.1.3.2 Constructed Data */ /* returns true if the tag is context specific */ -bool decode_is_context_specific(uint8_t * apdu) +bool decode_is_context_specific( + uint8_t * apdu) { return ((apdu[0] & BIT3) == BIT3); } -int decode_tag_number(uint8_t * apdu, uint8_t * tag_number) +int decode_tag_number( + uint8_t * apdu, + uint8_t * tag_number) { - int len = 1; /* return value */ + int len = 1; /* return value */ /* decode the tag number first */ if (decode_is_extended_tag_number(&apdu[0])) { @@ -309,22 +324,26 @@ int decode_tag_number(uint8_t * apdu, uint8_t * tag_number) return len; } -bool decode_is_opening_tag(uint8_t * apdu) +bool decode_is_opening_tag( + uint8_t * apdu) { return ((apdu[0] & 0x07) == 6); } /* from clause 20.2.1.3.2 Constructed Data */ /* returns the number of apdu bytes consumed */ -bool decode_is_closing_tag(uint8_t * apdu) +bool decode_is_closing_tag( + uint8_t * apdu) { return ((apdu[0] & 0x07) == 7); } /* from clause 20.2.1.3.2 Constructed Data */ /* returns the number of apdu bytes consumed */ -int decode_tag_number_and_value(uint8_t * apdu, - uint8_t * tag_number, uint32_t * value) +int decode_tag_number_and_value( + uint8_t * apdu, + uint8_t * tag_number, + uint32_t * value) { int len = 1; uint16_t value16; @@ -367,7 +386,9 @@ int decode_tag_number_and_value(uint8_t * apdu, /* from clause 20.2.1.3.2 Constructed Data */ /* returns true if the tag is context specific and matches */ -bool decode_is_context_tag(uint8_t * apdu, uint8_t tag_number) +bool decode_is_context_tag( + uint8_t * apdu, + uint8_t tag_number) { uint8_t my_tag_number = 0; bool context_specific = false; @@ -380,7 +401,9 @@ bool decode_is_context_tag(uint8_t * apdu, uint8_t tag_number) /* from clause 20.2.1.3.2 Constructed Data */ /* returns the number of apdu bytes consumed */ -bool decode_is_opening_tag_number(uint8_t * apdu, uint8_t tag_number) +bool decode_is_opening_tag_number( + uint8_t * apdu, + uint8_t tag_number) { uint8_t my_tag_number = 0; bool opening_tag = false; @@ -393,7 +416,9 @@ bool decode_is_opening_tag_number(uint8_t * apdu, uint8_t tag_number) /* from clause 20.2.1.3.2 Constructed Data */ /* returns the number of apdu bytes consumed */ -bool decode_is_closing_tag_number(uint8_t * apdu, uint8_t tag_number) +bool decode_is_closing_tag_number( + uint8_t * apdu, + uint8_t tag_number) { uint8_t my_tag_number = 0; bool closing_tag = false; @@ -407,7 +432,9 @@ bool decode_is_closing_tag_number(uint8_t * apdu, uint8_t tag_number) /* from clause 20.2.3 Encoding of a Boolean Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -int encode_application_boolean(uint8_t * apdu, bool boolean_value) +int encode_application_boolean( + uint8_t * apdu, + bool boolean_value) { int len = 0; uint32_t len_value = 0; @@ -422,10 +449,12 @@ int encode_application_boolean(uint8_t * apdu, bool boolean_value) } /* context tagged is encoded differently */ -int encode_context_boolean(uint8_t * apdu, int tag_number, +int encode_context_boolean( + uint8_t * apdu, + int tag_number, bool boolean_value) { - int len = 0; /* return value */ + int len = 0; /* return value */ len = encode_tag(&apdu[0], (uint8_t) tag_number, true, 1); apdu[len] = boolean_value ? 1 : 0; @@ -434,7 +463,8 @@ int encode_context_boolean(uint8_t * apdu, int tag_number, return len; } -bool decode_context_boolean(uint8_t * apdu) +bool decode_context_boolean( + uint8_t * apdu) { bool boolean_value = false; @@ -447,7 +477,8 @@ bool decode_context_boolean(uint8_t * apdu) /* from clause 20.2.3 Encoding of a Boolean Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -bool decode_boolean(uint32_t len_value) +bool decode_boolean( + uint32_t len_value) { bool boolean_value = false; @@ -460,17 +491,21 @@ bool decode_boolean(uint32_t len_value) /* from clause 20.2.2 Encoding of a Null Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -int encode_application_null(uint8_t * apdu) +int encode_application_null( + uint8_t * apdu) { return encode_tag(&apdu[0], BACNET_APPLICATION_TAG_NULL, false, 0); } -int encode_context_null(uint8_t * apdu, int tag_number) +int encode_context_null( + uint8_t * apdu, + int tag_number) { - return encode_tag(&apdu[0], (uint8_t)tag_number, true, 0); + return encode_tag(&apdu[0], (uint8_t) tag_number, true, 0); } -static uint8_t byte_reverse_bits(uint8_t in_byte) +static uint8_t byte_reverse_bits( + uint8_t in_byte) { uint8_t out_byte = 0; @@ -496,7 +531,9 @@ static uint8_t byte_reverse_bits(uint8_t in_byte) /* from clause 20.2.10 Encoding of a Bit String Value */ /* returns the number of apdu bytes consumed */ -int decode_bitstring(uint8_t * apdu, uint32_t len_value, +int decode_bitstring( + uint8_t * apdu, + uint32_t len_value, BACNET_BIT_STRING * bit_string) { int len = 0; @@ -526,7 +563,9 @@ int decode_bitstring(uint8_t * apdu, uint32_t len_value, /* from clause 20.2.10 Encoding of a Bit String Value */ /* returns the number of apdu bytes consumed */ -int encode_bitstring(uint8_t * apdu, BACNET_BIT_STRING * bit_string) +int encode_bitstring( + uint8_t * apdu, + BACNET_BIT_STRING * bit_string) { int len = 0; uint8_t remaining_used_bits = 0; @@ -543,15 +582,16 @@ int encode_bitstring(uint8_t * apdu, BACNET_BIT_STRING * bit_string) /* number of unused bits in the subsequent final octet */ apdu[len++] = 8 - remaining_used_bits; for (i = 0; i < used_bytes; i++) { - apdu[len++] = - byte_reverse_bits(bitstring_octet(bit_string, i)); + apdu[len++] = byte_reverse_bits(bitstring_octet(bit_string, i)); } } return len; } -int encode_application_bitstring(uint8_t * apdu, BACNET_BIT_STRING * bit_string) +int encode_application_bitstring( + uint8_t * apdu, + BACNET_BIT_STRING * bit_string) { int len = 0; int bit_string_encoded_length = 1; /* 1 for the bits remaining octet */ @@ -565,7 +605,9 @@ int encode_application_bitstring(uint8_t * apdu, BACNET_BIT_STRING * bit_string) return len; } -int encode_context_bitstring(uint8_t * apdu, int tag_number, +int encode_context_bitstring( + uint8_t * apdu, + int tag_number, BACNET_BIT_STRING * bit_string) { int len = 0; @@ -574,7 +616,8 @@ int encode_context_bitstring(uint8_t * apdu, int tag_number, /* bit string may use more than 1 octet for the tag, so find out how many */ bit_string_encoded_length += bitstring_bytes_used(bit_string); len = - encode_tag(&apdu[0], (uint8_t)tag_number, true, bit_string_encoded_length); + encode_tag(&apdu[0], (uint8_t) tag_number, true, + bit_string_encoded_length); len += encode_bitstring(&apdu[len], bit_string); return len; @@ -582,7 +625,10 @@ int encode_context_bitstring(uint8_t * apdu, int tag_number, /* from clause 20.2.14 Encoding of an Object Identifier Value */ /* returns the number of apdu bytes consumed */ -int decode_object_id(uint8_t * apdu, int *object_type, uint32_t * instance) +int decode_object_id( + uint8_t * apdu, + int *object_type, + uint32_t * instance) { uint32_t value = 0; int len = 0; @@ -596,8 +642,10 @@ int decode_object_id(uint8_t * apdu, int *object_type, uint32_t * instance) /* from clause 20.2.14 Encoding of an Object Identifier Value */ /* returns the number of apdu bytes consumed */ -int encode_bacnet_object_id(uint8_t * apdu, - int object_type, uint32_t instance) +int encode_bacnet_object_id( + uint8_t * apdu, + int object_type, + uint32_t instance) { uint32_t value = 0; uint32_t type = 0; @@ -614,8 +662,11 @@ int encode_bacnet_object_id(uint8_t * apdu, /* from clause 20.2.14 Encoding of an Object Identifier Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -int encode_context_object_id(uint8_t * apdu, - int tag_number, int object_type, uint32_t instance) +int encode_context_object_id( + uint8_t * apdu, + int tag_number, + int object_type, + uint32_t instance) { int len = 0; @@ -633,26 +684,29 @@ int encode_context_object_id(uint8_t * apdu, /* from clause 20.2.14 Encoding of an Object Identifier Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -int encode_application_object_id(uint8_t * apdu, - int object_type, uint32_t instance) +int encode_application_object_id( + uint8_t * apdu, + int object_type, + uint32_t instance) { int len = 0; /* assumes that the tag only consumes 1 octet */ len = encode_bacnet_object_id(&apdu[1], object_type, instance); - len += encode_tag(&apdu[0], BACNET_APPLICATION_TAG_OBJECT_ID, - false, len); + len += encode_tag(&apdu[0], BACNET_APPLICATION_TAG_OBJECT_ID, false, len); return len; } /* from clause 20.2.8 Encoding of an Octet String Value */ /* returns the number of apdu bytes consumed */ -int encode_octet_string(uint8_t * apdu, BACNET_OCTET_STRING * octet_string) +int encode_octet_string( + uint8_t * apdu, + BACNET_OCTET_STRING * octet_string) { - int len = 0; /* return value */ + int len = 0; /* return value */ uint8_t *value; - int i = 0; /* loop counter */ + int i = 0; /* loop counter */ if (octet_string) { /* FIXME: might need to pass in the length of the APDU @@ -670,7 +724,8 @@ int encode_octet_string(uint8_t * apdu, BACNET_OCTET_STRING * octet_string) /* from clause 20.2.8 Encoding of an Octet String Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -int encode_application_octet_string(uint8_t * apdu, +int encode_application_octet_string( + uint8_t * apdu, BACNET_OCTET_STRING * octet_string) { int apdu_len = 0; @@ -693,8 +748,10 @@ int encode_application_octet_string(uint8_t * apdu, /* from clause 20.2.8 Encoding of an Octet String Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -int encode_context_octet_string(uint8_t * apdu, - int tag_number, BACNET_OCTET_STRING * octet_string) +int encode_context_octet_string( + uint8_t * apdu, + int tag_number, + BACNET_OCTET_STRING * octet_string) { int apdu_len = 0; @@ -713,10 +770,12 @@ int encode_context_octet_string(uint8_t * apdu, /* from clause 20.2.8 Encoding of an Octet String Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -int decode_octet_string(uint8_t * apdu, uint32_t len_value, +int decode_octet_string( + uint8_t * apdu, + uint32_t len_value, BACNET_OCTET_STRING * octet_string) { - int len = 0; /* return value */ + int len = 0; /* return value */ bool status = false; status = octetstring_init(octet_string, &apdu[0], len_value); @@ -728,7 +787,8 @@ int decode_octet_string(uint8_t * apdu, uint32_t len_value, /* from clause 20.2.9 Encoding of a Character String Value */ /* returns the number of apdu bytes consumed */ -int encode_bacnet_character_string(uint8_t * apdu, +int encode_bacnet_character_string( + uint8_t * apdu, BACNET_CHARACTER_STRING * char_string) { int len, i; @@ -747,14 +807,14 @@ int encode_bacnet_character_string(uint8_t * apdu, /* from clause 20.2.9 Encoding of a Character String Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -int encode_application_character_string(uint8_t * apdu, +int encode_application_character_string( + uint8_t * apdu, BACNET_CHARACTER_STRING * char_string) { int len = 0; int string_len = 0; - string_len = - characterstring_length(char_string) + 1 /* for encoding */ ; + string_len = characterstring_length(char_string) + 1 /* for encoding */ ; len = encode_tag(&apdu[0], BACNET_APPLICATION_TAG_CHARACTER_STRING, false, string_len); @@ -766,14 +826,15 @@ int encode_application_character_string(uint8_t * apdu, return len; } -int encode_context_character_string(uint8_t * apdu, int tag_number, +int encode_context_character_string( + uint8_t * apdu, + int tag_number, BACNET_CHARACTER_STRING * char_string) { int len = 0; int string_len = 0; - string_len = - characterstring_length(char_string) + 1 /* for encoding */ ; + string_len = characterstring_length(char_string) + 1 /* for encoding */ ; len += encode_tag(&apdu[0], (uint8_t) tag_number, true, string_len); if ((len + string_len) < MAX_APDU) len += encode_bacnet_character_string(&apdu[len], char_string); @@ -786,10 +847,12 @@ int encode_context_character_string(uint8_t * apdu, int tag_number, /* from clause 20.2.9 Encoding of a Character String Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -int decode_character_string(uint8_t * apdu, uint32_t len_value, +int decode_character_string( + uint8_t * apdu, + uint32_t len_value, BACNET_CHARACTER_STRING * char_string) { - int len = 0; /* return value */ + int len = 0; /* return value */ bool status = false; status = characterstring_init(char_string, apdu[0], (char *) &apdu[1], @@ -803,28 +866,31 @@ int decode_character_string(uint8_t * apdu, uint32_t len_value, /* from clause 20.2.4 Encoding of an Unsigned Integer Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -int decode_unsigned(uint8_t * apdu, uint32_t len_value, uint32_t * value) +int decode_unsigned( + uint8_t * apdu, + uint32_t len_value, + uint32_t * value) { uint16_t unsigned16_value = 0; if (value) { switch (len_value) { - case 1: - *value = apdu[0]; - break; - case 2: - decode_unsigned16(&apdu[0], &unsigned16_value); - *value = unsigned16_value; - break; - case 3: - decode_unsigned24(&apdu[0], value); - break; - case 4: - decode_unsigned32(&apdu[0], value); - break; - default: - *value = 0; - break; + case 1: + *value = apdu[0]; + break; + case 2: + decode_unsigned16(&apdu[0], &unsigned16_value); + *value = unsigned16_value; + break; + case 3: + decode_unsigned24(&apdu[0], value); + break; + case 4: + decode_unsigned32(&apdu[0], value); + break; + default: + *value = 0; + break; } } @@ -834,9 +900,11 @@ int decode_unsigned(uint8_t * apdu, uint32_t len_value, uint32_t * value) /* from clause 20.2.4 Encoding of an Unsigned Integer Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -int encode_bacnet_unsigned(uint8_t * apdu, uint32_t value) +int encode_bacnet_unsigned( + uint8_t * apdu, + uint32_t value) { - int len = 0; /* return value */ + int len = 0; /* return value */ if (value < 0x100) { apdu[0] = (uint8_t) value; @@ -855,7 +923,10 @@ int encode_bacnet_unsigned(uint8_t * apdu, uint32_t value) /* from clause 20.2.4 Encoding of an Unsigned Integer Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -int encode_context_unsigned(uint8_t * apdu, int tag_number, uint32_t value) +int encode_context_unsigned( + uint8_t * apdu, + int tag_number, + uint32_t value) { int len = 0; @@ -872,7 +943,9 @@ int encode_context_unsigned(uint8_t * apdu, int tag_number, uint32_t value) /* from clause 20.2.4 Encoding of an Unsigned Integer Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -int encode_application_unsigned(uint8_t * apdu, uint32_t value) +int encode_application_unsigned( + uint8_t * apdu, + uint32_t value) { int len = 0; @@ -886,7 +959,10 @@ int encode_application_unsigned(uint8_t * apdu, uint32_t value) /* from clause 20.2.11 Encoding of an Enumerated Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -int decode_enumerated(uint8_t * apdu, uint32_t len_value, int *value) +int decode_enumerated( + uint8_t * apdu, + uint32_t len_value, + int *value) { uint32_t unsigned_value = 0; int len; @@ -901,7 +977,9 @@ int decode_enumerated(uint8_t * apdu, uint32_t len_value, int *value) /* from clause 20.2.11 Encoding of an Enumerated Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -int encode_bacnet_enumerated(uint8_t * apdu, int value) +int encode_bacnet_enumerated( + uint8_t * apdu, + int value) { return encode_bacnet_unsigned(apdu, value); } @@ -909,14 +987,15 @@ int encode_bacnet_enumerated(uint8_t * apdu, int value) /* from clause 20.2.11 Encoding of an Enumerated Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -int encode_application_enumerated(uint8_t * apdu, int value) +int encode_application_enumerated( + uint8_t * apdu, + int value) { - int len = 0; /* return value */ + int len = 0; /* return value */ /* assumes that the tag only consumes 1 octet */ len = encode_bacnet_enumerated(&apdu[1], value); - len += encode_tag(&apdu[0], BACNET_APPLICATION_TAG_ENUMERATED, - false, len); + len += encode_tag(&apdu[0], BACNET_APPLICATION_TAG_ENUMERATED, false, len); return len; } @@ -924,9 +1003,12 @@ int encode_application_enumerated(uint8_t * apdu, int value) /* from clause 20.2.11 Encoding of an Enumerated Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -int encode_context_enumerated(uint8_t * apdu, int tag_number, int value) +int encode_context_enumerated( + uint8_t * apdu, + int tag_number, + int value) { - int len = 0; /* return value */ + int len = 0; /* return value */ /* assumes that the tag only consumes 1 octet */ len = encode_bacnet_enumerated(&apdu[1], value); @@ -942,25 +1024,28 @@ int encode_context_enumerated(uint8_t * apdu, int tag_number, int value) /* from clause 20.2.5 Encoding of a Signed Integer Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -int decode_signed(uint8_t * apdu, uint32_t len_value, int32_t * value) +int decode_signed( + uint8_t * apdu, + uint32_t len_value, + int32_t * value) { if (value) { switch (len_value) { - case 1: - decode_signed8(&apdu[0], value); - break; - case 2: - decode_signed16(&apdu[0], value); - break; - case 3: - decode_signed24(&apdu[0], value); - break; - case 4: - decode_signed32(&apdu[0], value); - break; - default: - *value = 0; - break; + case 1: + decode_signed8(&apdu[0], value); + break; + case 2: + decode_signed16(&apdu[0], value); + break; + case 3: + decode_signed24(&apdu[0], value); + break; + case 4: + decode_signed32(&apdu[0], value); + break; + default: + *value = 0; + break; } } @@ -970,9 +1055,11 @@ int decode_signed(uint8_t * apdu, uint32_t len_value, int32_t * value) /* from clause 20.2.5 Encoding of a Signed Integer Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -int encode_bacnet_signed(uint8_t * apdu, int32_t value) +int encode_bacnet_signed( + uint8_t * apdu, + int32_t value) { - int len = 0; /* return value */ + int len = 0; /* return value */ /* don't encode the leading X'FF' or X'00' of the two's compliment. That is, the first octet of any multi-octet encoded value shall @@ -995,14 +1082,15 @@ int encode_bacnet_signed(uint8_t * apdu, int32_t value) /* from clause 20.2.5 Encoding of a Signed Integer Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -int encode_application_signed(uint8_t * apdu, int32_t value) +int encode_application_signed( + uint8_t * apdu, + int32_t value) { - int len = 0; /* return value */ + int len = 0; /* return value */ /* assumes that the tag only consumes 1 octet */ len = encode_bacnet_signed(&apdu[1], value); - len += encode_tag(&apdu[0], BACNET_APPLICATION_TAG_SIGNED_INT, - false, len); + len += encode_tag(&apdu[0], BACNET_APPLICATION_TAG_SIGNED_INT, false, len); return len; } @@ -1010,9 +1098,12 @@ int encode_application_signed(uint8_t * apdu, int32_t value) /* from clause 20.2.5 Encoding of a Signed Integer Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -int encode_context_signed(uint8_t * apdu, int tag_number, int32_t value) +int encode_context_signed( + uint8_t * apdu, + int tag_number, + int32_t value) { - int len = 0; /* return value */ + int len = 0; /* return value */ /* assumes that the tag only consumes 1 octet */ len = encode_bacnet_signed(&apdu[1], value); @@ -1028,7 +1119,9 @@ int encode_context_signed(uint8_t * apdu, int tag_number, int32_t value) /* from clause 20.2.6 Encoding of a Real Number Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -int encode_application_real(uint8_t * apdu, float value) +int encode_application_real( + uint8_t * apdu, + float value) { int len = 0; @@ -1039,7 +1132,10 @@ int encode_application_real(uint8_t * apdu, float value) return len; } -int encode_context_real(uint8_t * apdu, int tag_number, float value) +int encode_context_real( + uint8_t * apdu, + int tag_number, + float value) { int len = 0; @@ -1057,7 +1153,9 @@ int encode_context_real(uint8_t * apdu, int tag_number, float value) /* from clause 20.2.13 Encoding of a Time Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -int encode_bacnet_time(uint8_t * apdu, BACNET_TIME * btime) +int encode_bacnet_time( + uint8_t * apdu, + BACNET_TIME * btime) { apdu[0] = btime->hour; apdu[1] = btime->min; @@ -1070,7 +1168,9 @@ int encode_bacnet_time(uint8_t * apdu, BACNET_TIME * btime) /* from clause 20.2.13 Encoding of a Time Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -int encode_application_time(uint8_t * apdu, BACNET_TIME * btime) +int encode_application_time( + uint8_t * apdu, + BACNET_TIME * btime) { int len = 0; @@ -1081,10 +1181,12 @@ int encode_application_time(uint8_t * apdu, BACNET_TIME * btime) return len; } -int encode_context_time(uint8_t * apdu, int tag_number, +int encode_context_time( + uint8_t * apdu, + int tag_number, BACNET_TIME * btime) { - int len = 0; /* return value */ + int len = 0; /* return value */ /* assumes that the tag only consumes 1 octet */ len = encode_bacnet_time(&apdu[1], btime); @@ -1100,7 +1202,9 @@ int encode_context_time(uint8_t * apdu, int tag_number, /* from clause 20.2.13 Encoding of a Time Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -int decode_bacnet_time(uint8_t * apdu, BACNET_TIME * btime) +int decode_bacnet_time( + uint8_t * apdu, + BACNET_TIME * btime) { btime->hour = apdu[0]; btime->min = apdu[1]; @@ -1119,7 +1223,9 @@ int decode_bacnet_time(uint8_t * apdu, BACNET_TIME * btime) /* from clause 20.2.12 Encoding of a Date Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -int encode_bacnet_date(uint8_t * apdu, BACNET_DATE * bdate) +int encode_bacnet_date( + uint8_t * apdu, + BACNET_DATE * bdate) { /* allow 2 digit years */ if (bdate->year < 1900) { @@ -1139,7 +1245,9 @@ int encode_bacnet_date(uint8_t * apdu, BACNET_DATE * bdate) /* from clause 20.2.12 Encoding of a Date Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -int encode_application_date(uint8_t * apdu, BACNET_DATE * bdate) +int encode_application_date( + uint8_t * apdu, + BACNET_DATE * bdate) { int len = 0; @@ -1151,10 +1259,12 @@ int encode_application_date(uint8_t * apdu, BACNET_DATE * bdate) } -int encode_context_date(uint8_t * apdu, int tag_number, +int encode_context_date( + uint8_t * apdu, + int tag_number, BACNET_DATE * bdate) { - int len = 0; /* return value */ + int len = 0; /* return value */ /* assumes that the tag only consumes 1 octet */ len = encode_bacnet_date(&apdu[1], bdate); @@ -1170,7 +1280,9 @@ int encode_context_date(uint8_t * apdu, int tag_number, /* from clause 20.2.12 Encoding of a Date Value */ /* and 20.2.1 General Rules for Encoding BACnet Tags */ /* returns the number of apdu bytes consumed */ -int decode_date(uint8_t * apdu, BACNET_DATE * bdate) +int decode_date( + uint8_t * apdu, + BACNET_DATE * bdate) { bdate->year = apdu[0] + 1900; bdate->month = apdu[1]; @@ -1181,7 +1293,9 @@ int decode_date(uint8_t * apdu, BACNET_DATE * bdate) } /* returns the number of apdu bytes consumed */ -int encode_simple_ack(uint8_t * apdu, uint8_t invoke_id, +int encode_simple_ack( + uint8_t * apdu, + uint8_t invoke_id, uint8_t service_choice) { apdu[0] = PDU_TYPE_SIMPLE_ACK; @@ -1198,14 +1312,16 @@ int encode_simple_ack(uint8_t * apdu, uint8_t invoke_id, #include #include "ctest.h" -static int get_apdu_len(bool extended_tag, uint32_t value) +static int get_apdu_len( + bool extended_tag, + uint32_t value) { int test_len = 1; if (extended_tag) test_len++; if (value <= 4) - test_len += 0; /* do nothing... */ + test_len += 0; /* do nothing... */ else if (value <= 253) test_len += 1; else if (value <= 65535) @@ -1216,17 +1332,19 @@ static int get_apdu_len(bool extended_tag, uint32_t value) return test_len; } -static void print_apdu(uint8_t * pBlock, uint32_t num) +static void print_apdu( + uint8_t * pBlock, + uint32_t num) { - size_t lines = 0; /* number of lines to print */ - size_t line = 0; /* line of text counter */ + size_t lines = 0; /* number of lines to print */ + size_t line = 0; /* line of text counter */ size_t last_line = 0; /* line on which the last text resided */ unsigned long count = 0; /* address to print */ - unsigned int i = 0; /* counter */ + unsigned int i = 0; /* counter */ if (pBlock && num) { /* how many lines to print? */ - num--; /* adjust */ + num--; /* adjust */ lines = (num / 16) + 1; last_line = num % 16; @@ -1263,7 +1381,8 @@ static void print_apdu(uint8_t * pBlock, uint32_t num) return; } -void testBACDCodeTags(Test * pTest) +void testBACDCodeTags( + Test * pTest) { uint8_t apdu[MAX_APDU] = { 0 }; uint8_t tag_number = 0, test_tag_number = 0; @@ -1272,12 +1391,9 @@ void testBACDCodeTags(Test * pTest) for (tag_number = 0;; tag_number++) { len = encode_opening_tag(&apdu[0], tag_number); - test_len = - get_apdu_len(decode_is_extended_tag_number(&apdu[0]), 0); + test_len = get_apdu_len(decode_is_extended_tag_number(&apdu[0]), 0); ct_test(pTest, len == test_len); - len = - decode_tag_number_and_value(&apdu[0], &test_tag_number, - &value); + len = decode_tag_number_and_value(&apdu[0], &test_tag_number, &value); ct_test(pTest, value == 0); ct_test(pTest, len == test_len); ct_test(pTest, tag_number == test_tag_number); @@ -1285,9 +1401,7 @@ void testBACDCodeTags(Test * pTest) ct_test(pTest, decode_is_closing_tag(&apdu[0]) == false); len = encode_closing_tag(&apdu[0], tag_number); ct_test(pTest, len == test_len); - len = - decode_tag_number_and_value(&apdu[0], &test_tag_number, - &value); + len = decode_tag_number_and_value(&apdu[0], &test_tag_number, &value); ct_test(pTest, len == test_len); ct_test(pTest, value == 0); ct_test(pTest, tag_number == test_tag_number); @@ -1301,8 +1415,7 @@ void testBACDCodeTags(Test * pTest) ct_test(pTest, tag_number == test_tag_number); ct_test(pTest, value == test_value); test_len = - get_apdu_len(decode_is_extended_tag_number(&apdu[0]), - value); + get_apdu_len(decode_is_extended_tag_number(&apdu[0]), value); ct_test(pTest, len == test_len); /* stop at the the last value */ if (value & BIT31) @@ -1316,7 +1429,8 @@ void testBACDCodeTags(Test * pTest) return; } -void testBACDCodeEnumerated(Test * pTest) +void testBACDCodeEnumerated( + Test * pTest) { uint8_t array[5] = { 0 }; uint8_t encoded_array[5] = { 0 }; @@ -1330,9 +1444,7 @@ void testBACDCodeEnumerated(Test * pTest) for (i = 0; i < 31; i++) { apdu_len = encode_application_enumerated(&array[0], value); - len = - decode_tag_number_and_value(&array[0], &tag_number, - &len_value); + len = decode_tag_number_and_value(&array[0], &tag_number, &len_value); len += decode_enumerated(&array[len], len_value, &decoded_value); ct_test(pTest, decoded_value == value); ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_ENUMERATED); @@ -1361,7 +1473,8 @@ void testBACDCodeEnumerated(Test * pTest) return; } -void testBACDCodeReal(Test * pTest) +void testBACDCodeReal( + Test * pTest) { uint8_t real_array[4] = { 0 }; uint8_t encoded_array[4] = { 0 }; @@ -1394,7 +1507,9 @@ void testBACDCodeReal(Test * pTest) return; } -void testBACDCodeUnsignedValue(Test * pTest, uint32_t value) +void testBACDCodeUnsignedValue( + Test * pTest, + uint32_t value) { uint8_t array[5] = { 0 }; uint8_t encoded_array[5] = { 0 }; @@ -1413,8 +1528,7 @@ void testBACDCodeUnsignedValue(Test * pTest, uint32_t value) print_apdu(&array[0], sizeof(array)); } encode_application_unsigned(&encoded_array[0], decoded_value); - ct_test(pTest, memcmp(&array[0], &encoded_array[0], - sizeof(array)) == 0); + ct_test(pTest, memcmp(&array[0], &encoded_array[0], sizeof(array)) == 0); /* an unsigned will take up to 4 octects */ /* plus a one octet for the tag */ apdu_len = encode_application_unsigned(&apdu[0], value); @@ -1426,7 +1540,8 @@ void testBACDCodeUnsignedValue(Test * pTest, uint32_t value) ct_test(pTest, decode_is_context_specific(&apdu[0]) == false); } -void testBACDCodeUnsigned(Test * pTest) +void testBACDCodeUnsigned( + Test * pTest) { uint32_t value = 1; int i; @@ -1441,13 +1556,14 @@ void testBACDCodeUnsigned(Test * pTest) return; } -void testBACnetUnsigned(Test * pTest) +void testBACnetUnsigned( + Test * pTest) { uint8_t apdu[32] = { 0 }; uint32_t value = 0, test_value = 0; int len = 0, test_len = 0; - for (value = 0; ;value+=0xFF) { + for (value = 0;; value += 0xFF) { len = encode_bacnet_unsigned(&apdu[0], value); test_len = decode_unsigned(&apdu[0], len, &test_value); ct_test(pTest, len == test_len); @@ -1457,7 +1573,9 @@ void testBACnetUnsigned(Test * pTest) } } -void testBACDCodeSignedValue(Test * pTest, int32_t value) +void testBACDCodeSignedValue( + Test * pTest, + int32_t value) { uint8_t array[5] = { 0 }; uint8_t encoded_array[5] = { 0 }; @@ -1495,7 +1613,8 @@ void testBACDCodeSignedValue(Test * pTest, int32_t value) return; } -void testBACDCodeSigned(Test * pTest) +void testBACDCodeSigned( + Test * pTest) { int value = 1; int i = 0; @@ -1519,19 +1638,20 @@ void testBACDCodeSigned(Test * pTest) return; } -void testBACnetSigned(Test * pTest) +void testBACnetSigned( + Test * pTest) { uint8_t apdu[32] = { 0 }; int32_t value = 0, test_value = 0; int len = 0, test_len = 0; - for (value = -2147483647; value < 0; value+=127) { + for (value = -2147483647; value < 0; value += 127) { len = encode_bacnet_signed(&apdu[0], value); test_len = decode_signed(&apdu[0], len, &test_value); ct_test(pTest, len == test_len); ct_test(pTest, value == test_value); } - for (value = 2147483647; value > 0; value-=127) { + for (value = 2147483647; value > 0; value -= 127) { len = encode_bacnet_signed(&apdu[0], value); test_len = decode_signed(&apdu[0], len, &test_value); ct_test(pTest, len == test_len); @@ -1539,20 +1659,21 @@ void testBACnetSigned(Test * pTest) } } -void testBACDCodeOctetString(Test * pTest) +void testBACDCodeOctetString( + Test * pTest) { uint8_t array[MAX_APDU] = { 0 }; uint8_t encoded_array[MAX_APDU] = { 0 }; BACNET_OCTET_STRING octet_string; BACNET_OCTET_STRING test_octet_string; uint8_t test_value[MAX_APDU] = { "" }; - int i; /* for loop counter */ + int i; /* for loop counter */ int apdu_len; int len; uint8_t tag_number = 0; uint32_t len_value = 0; bool status = false; - int diff = 0; /* for memcmp */ + int diff = 0; /* for memcmp */ status = octetstring_init(&octet_string, NULL, 0); ct_test(pTest, status == true); @@ -1592,29 +1713,28 @@ void testBACDCodeOctetString(Test * pTest) return; } -void testBACDCodeCharacterString(Test * pTest) +void testBACDCodeCharacterString( + Test * pTest) { uint8_t array[MAX_APDU] = { 0 }; uint8_t encoded_array[MAX_APDU] = { 0 }; BACNET_CHARACTER_STRING char_string; BACNET_CHARACTER_STRING test_char_string; char test_value[MAX_APDU] = { "" }; - int i; /* for loop counter */ + int i; /* for loop counter */ int apdu_len; int len; uint8_t tag_number = 0; uint32_t len_value = 0; - int diff = 0; /* for comparison */ + int diff = 0; /* for comparison */ bool status = false; - status = characterstring_init(&char_string, - CHARACTER_ANSI_X34, NULL, 0); + status = characterstring_init(&char_string, CHARACTER_ANSI_X34, NULL, 0); ct_test(pTest, status == true); apdu_len = encode_application_character_string(&array[0], &char_string); len = decode_tag_number_and_value(&array[0], &tag_number, &len_value); ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_CHARACTER_STRING); - len += - decode_character_string(&array[len], len_value, &test_char_string); + len += decode_character_string(&array[len], len_value, &test_char_string); ct_test(pTest, apdu_len == len); diff = memcmp(characterstring_value(&char_string), &test_value[0], characterstring_length(&char_string)); @@ -1630,14 +1750,12 @@ void testBACDCodeCharacterString(Test * pTest) len = decode_tag_number_and_value(&encoded_array[0], &tag_number, &len_value); - ct_test(pTest, - tag_number == BACNET_APPLICATION_TAG_CHARACTER_STRING); + ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_CHARACTER_STRING); len += decode_character_string(&encoded_array[len], len_value, &test_char_string); if (apdu_len != len) { - printf("test string=#%d apdu_len=%d len=%d\n", i, apdu_len, - len); + printf("test string=#%d apdu_len=%d len=%d\n", i, apdu_len, len); } ct_test(pTest, apdu_len == len); diff = memcmp(characterstring_value(&char_string), &test_value[0], @@ -1651,7 +1769,8 @@ void testBACDCodeCharacterString(Test * pTest) return; } -void testBACDCodeObject(Test * pTest) +void testBACDCodeObject( + Test * pTest) { uint8_t object_array[4] = { 0 @@ -1673,8 +1792,7 @@ void testBACDCodeObject(Test * pTest) ct_test(pTest, memcmp(&object_array[0], &encoded_array[0], sizeof(object_array)) == 0); for (type = 0; type < 1024; type++) { - for (instance = 0; instance <= BACNET_MAX_INSTANCE; - instance += 1024) { + for (instance = 0; instance <= BACNET_MAX_INSTANCE; instance += 1024) { encode_bacnet_object_id(&encoded_array[0], type, instance); decode_object_id(&encoded_array[0], (int *) &decoded_type, &decoded_instance); @@ -1689,7 +1807,8 @@ void testBACDCodeObject(Test * pTest) return; } -void testBACDCodeMaxSegsApdu(Test * pTest) +void testBACDCodeMaxSegsApdu( + Test * pTest) { int max_segs[8] = { 0, 2, 4, 8, 16, 32, 64, 65 }; int max_apdu[6] = { 50, 128, 206, 480, 1024, 1476 }; @@ -1707,7 +1826,8 @@ void testBACDCodeMaxSegsApdu(Test * pTest) } } -void testBACDCodeBitString(Test * pTest) +void testBACDCodeBitString( + Test * pTest) { uint8_t bit = 0; BACNET_BIT_STRING bit_string; @@ -1731,13 +1851,10 @@ void testBACDCodeBitString(Test * pTest) /* encode */ len = encode_application_bitstring(&apdu[0], &bit_string); /* decode */ - len = - decode_tag_number_and_value(&apdu[0], &tag_number, &len_value); + len = decode_tag_number_and_value(&apdu[0], &tag_number, &len_value); ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_BIT_STRING); - len += - decode_bitstring(&apdu[len], len_value, &decoded_bit_string); - ct_test(pTest, - bitstring_bits_used(&decoded_bit_string) == (bit + 1)); + len += decode_bitstring(&apdu[len], len_value, &decoded_bit_string); + ct_test(pTest, bitstring_bits_used(&decoded_bit_string) == (bit + 1)); ct_test(pTest, bitstring_bit(&decoded_bit_string, bit) == true); } /* test encode/decode -- false */ @@ -1749,19 +1866,17 @@ void testBACDCodeBitString(Test * pTest) /* encode */ len = encode_application_bitstring(&apdu[0], &bit_string); /* decode */ - len = - decode_tag_number_and_value(&apdu[0], &tag_number, &len_value); + len = decode_tag_number_and_value(&apdu[0], &tag_number, &len_value); ct_test(pTest, tag_number == BACNET_APPLICATION_TAG_BIT_STRING); - len += - decode_bitstring(&apdu[len], len_value, &decoded_bit_string); - ct_test(pTest, - bitstring_bits_used(&decoded_bit_string) == (bit + 1)); + len += decode_bitstring(&apdu[len], len_value, &decoded_bit_string); + ct_test(pTest, bitstring_bits_used(&decoded_bit_string) == (bit + 1)); ct_test(pTest, bitstring_bit(&decoded_bit_string, bit) == false); } } #ifdef TEST_DECODE -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -1800,5 +1915,5 @@ int main(void) return 0; } -#endif /* TEST_DECODE */ -#endif /* TEST */ +#endif /* TEST_DECODE */ +#endif /* TEST */ diff --git a/bacnet-stack/src/bacerror.c b/bacnet-stack/src/bacerror.c index e15c02df..6ed22e51 100644 --- a/bacnet-stack/src/bacerror.c +++ b/bacnet-stack/src/bacerror.c @@ -37,12 +37,14 @@ #include "bacdef.h" /* encode service */ -int bacerror_encode_apdu(uint8_t * apdu, +int bacerror_encode_apdu( + uint8_t * apdu, uint8_t invoke_id, BACNET_CONFIRMED_SERVICE service, - BACNET_ERROR_CLASS error_class, BACNET_ERROR_CODE error_code) + BACNET_ERROR_CLASS error_class, + BACNET_ERROR_CODE error_code) { - int apdu_len = 0; /* total length of the apdu, return value */ + int apdu_len = 0; /* total length of the apdu, return value */ if (apdu) { apdu[0] = PDU_TYPE_ERROR; @@ -50,7 +52,8 @@ int bacerror_encode_apdu(uint8_t * apdu, apdu[2] = service; apdu_len = 3; /* service parameters */ - apdu_len += encode_application_enumerated(&apdu[apdu_len], error_class); + apdu_len += + encode_application_enumerated(&apdu[apdu_len], error_class); apdu_len += encode_application_enumerated(&apdu[apdu_len], error_code); } @@ -58,9 +61,11 @@ int bacerror_encode_apdu(uint8_t * apdu, } /* decode the application class and code */ -int bacerror_decode_error_class_and_code(uint8_t * apdu, +int bacerror_decode_error_class_and_code( + uint8_t * apdu, unsigned apdu_len, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { int len = 0; uint8_t tag_number = 0; @@ -73,30 +78,30 @@ int bacerror_decode_error_class_and_code(uint8_t * apdu, &tag_number, &len_value_type); if (tag_number != BACNET_APPLICATION_TAG_ENUMERATED) return 0; - len += - decode_enumerated(&apdu[len], len_value_type, &decoded_value); + len += decode_enumerated(&apdu[len], len_value_type, &decoded_value); if (error_class) - *error_class = (BACNET_ERROR_CLASS)decoded_value; + *error_class = (BACNET_ERROR_CLASS) decoded_value; /* error code */ len += decode_tag_number_and_value(&apdu[len], &tag_number, &len_value_type); if (tag_number != BACNET_APPLICATION_TAG_ENUMERATED) return 0; - len += - decode_enumerated(&apdu[len], len_value_type, &decoded_value); + len += decode_enumerated(&apdu[len], len_value_type, &decoded_value); if (error_code) - *error_code = (BACNET_ERROR_CODE)decoded_value; + *error_code = (BACNET_ERROR_CODE) decoded_value; } return len; } /* decode the service request only */ -int bacerror_decode_service_request(uint8_t * apdu, +int bacerror_decode_service_request( + uint8_t * apdu, unsigned apdu_len, uint8_t * invoke_id, BACNET_CONFIRMED_SERVICE * service, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { int len = 0; @@ -104,7 +109,7 @@ int bacerror_decode_service_request(uint8_t * apdu, if (invoke_id) *invoke_id = apdu[0]; if (service) - *service = (BACNET_CONFIRMED_SERVICE)apdu[1]; + *service = (BACNET_CONFIRMED_SERVICE) apdu[1]; /* decode the application class and code */ len = bacerror_decode_error_class_and_code(&apdu[2], apdu_len - 2, error_class, error_code); @@ -119,11 +124,13 @@ int bacerror_decode_service_request(uint8_t * apdu, #include "ctest.h" /* decode the whole APDU - mainly used for unit testing */ -int bacerror_decode_apdu(uint8_t * apdu, +int bacerror_decode_apdu( + uint8_t * apdu, unsigned apdu_len, uint8_t * invoke_id, BACNET_CONFIRMED_SERVICE * service, - BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code) + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) { int len = 0; @@ -142,7 +149,8 @@ int bacerror_decode_apdu(uint8_t * apdu, return len; } -void testBACError(Test * pTest) +void testBACError( + Test * pTest) { uint8_t apdu[480] = { 0 }; int len = 0; @@ -163,8 +171,7 @@ void testBACError(Test * pTest) len = bacerror_decode_apdu(&apdu[0], apdu_len, - &test_invoke_id, - &test_service, &test_error_class, &test_error_code); + &test_invoke_id, &test_service, &test_error_class, &test_error_code); ct_test(pTest, len != -1); ct_test(pTest, test_invoke_id == invoke_id); ct_test(pTest, test_service == service); @@ -175,22 +182,19 @@ void testBACError(Test * pTest) apdu[0] = PDU_TYPE_ABORT; len = bacerror_decode_apdu(&apdu[0], apdu_len, - &test_invoke_id, - &test_service, &test_error_class, &test_error_code); + &test_invoke_id, &test_service, &test_error_class, &test_error_code); ct_test(pTest, len == -1); /* test NULL APDU */ len = bacerror_decode_apdu(NULL, apdu_len, - &test_invoke_id, - &test_service, &test_error_class, &test_error_code); + &test_invoke_id, &test_service, &test_error_class, &test_error_code); ct_test(pTest, len == -1); /* force a zero length */ len = bacerror_decode_apdu(&apdu[0], 0, - &test_invoke_id, - &test_service, &test_error_class, &test_error_code); + &test_invoke_id, &test_service, &test_error_class, &test_error_code); ct_test(pTest, len == 0); @@ -227,8 +231,7 @@ void testBACError(Test * pTest) ct_test(pTest, len != 0); len = bacerror_decode_apdu(&apdu[0], apdu_len, - &test_invoke_id, - &test_service, &test_error_class, &test_error_code); + &test_invoke_id, &test_service, &test_error_class, &test_error_code); ct_test(pTest, len != -1); ct_test(pTest, test_invoke_id == invoke_id); ct_test(pTest, test_service == service); @@ -238,7 +241,8 @@ void testBACError(Test * pTest) } #ifdef TEST_BACERROR -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -255,5 +259,5 @@ int main(void) return 0; } -#endif /* TEST_ERROR */ -#endif /* TEST */ +#endif /* TEST_ERROR */ +#endif /* TEST */ diff --git a/bacnet-stack/src/bacint.c b/bacnet-stack/src/bacint.c index 69c0a02b..c42e38e2 100644 --- a/bacnet-stack/src/bacint.c +++ b/bacnet-stack/src/bacint.c @@ -38,74 +38,90 @@ #include #include "config.h" -int encode_unsigned16(uint8_t * apdu, uint16_t value) +int encode_unsigned16( + uint8_t * apdu, + uint16_t value) { - apdu[0] = (uint8_t)((value & 0xff00) >> 8); - apdu[1] = (uint8_t)(value & 0x00ff); + apdu[0] = (uint8_t) ((value & 0xff00) >> 8); + apdu[1] = (uint8_t) (value & 0x00ff); return 2; } -int decode_unsigned16(uint8_t * apdu, uint16_t * value) +int decode_unsigned16( + uint8_t * apdu, + uint16_t * value) { if (value) { - *value = (uint16_t)((((uint16_t)apdu[0]) << 8) & 0xff00); - *value |= ((uint16_t)(((uint16_t)apdu[1]) & 0x00ff)); + *value = (uint16_t) ((((uint16_t) apdu[0]) << 8) & 0xff00); + *value |= ((uint16_t) (((uint16_t) apdu[1]) & 0x00ff)); } return 2; } -int encode_unsigned24(uint8_t * apdu, uint32_t value) +int encode_unsigned24( + uint8_t * apdu, + uint32_t value) { - apdu[0] = (uint8_t)((value & 0xff0000) >> 16); - apdu[1] = (uint8_t)((value & 0x00ff00) >> 8); - apdu[2] = (uint8_t)(value & 0x0000ff); + apdu[0] = (uint8_t) ((value & 0xff0000) >> 16); + apdu[1] = (uint8_t) ((value & 0x00ff00) >> 8); + apdu[2] = (uint8_t) (value & 0x0000ff); return 3; } -int decode_unsigned24(uint8_t * apdu, uint32_t * value) +int decode_unsigned24( + uint8_t * apdu, + uint32_t * value) { if (value) { - *value = ((uint32_t)((((uint32_t)apdu[0]) << 16) & 0x00ff0000)); - *value |= (uint32_t)((((uint32_t)apdu[1]) << 8) & 0x0000ff00); - *value |= ((uint32_t)(((uint32_t)apdu[2]) & 0x000000ff)); + *value = ((uint32_t) ((((uint32_t) apdu[0]) << 16) & 0x00ff0000)); + *value |= (uint32_t) ((((uint32_t) apdu[1]) << 8) & 0x0000ff00); + *value |= ((uint32_t) (((uint32_t) apdu[2]) & 0x000000ff)); } return 3; } -int encode_unsigned32(uint8_t * apdu, uint32_t value) +int encode_unsigned32( + uint8_t * apdu, + uint32_t value) { - apdu[0] = (uint8_t)((value & 0xff000000) >> 24); - apdu[1] = (uint8_t)((value & 0x00ff0000) >> 16); - apdu[2] = (uint8_t)((value & 0x0000ff00) >> 8); - apdu[3] = (uint8_t)(value & 0x000000ff); + apdu[0] = (uint8_t) ((value & 0xff000000) >> 24); + apdu[1] = (uint8_t) ((value & 0x00ff0000) >> 16); + apdu[2] = (uint8_t) ((value & 0x0000ff00) >> 8); + apdu[3] = (uint8_t) (value & 0x000000ff); return 4; } -int decode_unsigned32(uint8_t * apdu, uint32_t * value) +int decode_unsigned32( + uint8_t * apdu, + uint32_t * value) { if (value) { - *value = ((uint32_t)((((uint32_t)apdu[0]) << 24) & 0xff000000)); - *value |= ((uint32_t)((((uint32_t)apdu[1]) << 16) & 0x00ff0000)); - *value |= ((uint32_t)((((uint32_t)apdu[2]) << 8) & 0x0000ff00)); - *value |= ((uint32_t)(((uint32_t)apdu[3]) & 0x000000ff)); + *value = ((uint32_t) ((((uint32_t) apdu[0]) << 24) & 0xff000000)); + *value |= ((uint32_t) ((((uint32_t) apdu[1]) << 16) & 0x00ff0000)); + *value |= ((uint32_t) ((((uint32_t) apdu[2]) << 8) & 0x0000ff00)); + *value |= ((uint32_t) (((uint32_t) apdu[3]) & 0x000000ff)); } return 4; } -int encode_signed8(uint8_t * apdu, int8_t value) +int encode_signed8( + uint8_t * apdu, + int8_t value) { - apdu[0] = (uint8_t)value; + apdu[0] = (uint8_t) value; return 1; } -int decode_signed8(uint8_t * apdu, int32_t * value) +int decode_signed8( + uint8_t * apdu, + int32_t * value) { if (value) { /* negative - bit 7 is set */ @@ -113,21 +129,25 @@ int decode_signed8(uint8_t * apdu, int32_t * value) *value = 0xFFFFFF00; else *value = 0; - *value |= ((int32_t)(((int32_t)apdu[0]) & 0x000000ff)); + *value |= ((int32_t) (((int32_t) apdu[0]) & 0x000000ff)); } return 1; } -int encode_signed16(uint8_t * apdu, int16_t value) +int encode_signed16( + uint8_t * apdu, + int16_t value) { - apdu[0] = (uint8_t)((value & 0xff00) >> 8); - apdu[1] = (uint8_t)(value & 0x00ff); + apdu[0] = (uint8_t) ((value & 0xff00) >> 8); + apdu[1] = (uint8_t) (value & 0x00ff); return 2; } -int decode_signed16(uint8_t * apdu, int32_t * value) +int decode_signed16( + uint8_t * apdu, + int32_t * value) { if (value) { /* negative - bit 7 is set */ @@ -135,23 +155,27 @@ int decode_signed16(uint8_t * apdu, int32_t * value) *value = 0xFFFF0000; else *value = 0; - *value |= ((int32_t)((((int32_t)apdu[0]) << 8) & 0x0000ff00)); - *value |= ((int32_t)(((int32_t)apdu[1]) & 0x000000ff)); + *value |= ((int32_t) ((((int32_t) apdu[0]) << 8) & 0x0000ff00)); + *value |= ((int32_t) (((int32_t) apdu[1]) & 0x000000ff)); } return 2; } -int encode_signed24(uint8_t * apdu, int32_t value) +int encode_signed24( + uint8_t * apdu, + int32_t value) { - apdu[0] = (uint8_t)((value & 0xff0000) >> 16); - apdu[1] = (uint8_t)((value & 0x00ff00) >> 8); - apdu[2] = (uint8_t)(value & 0x0000ff); + apdu[0] = (uint8_t) ((value & 0xff0000) >> 16); + apdu[1] = (uint8_t) ((value & 0x00ff00) >> 8); + apdu[2] = (uint8_t) (value & 0x0000ff); return 3; } -int decode_signed24(uint8_t * apdu, int32_t * value) +int decode_signed24( + uint8_t * apdu, + int32_t * value) { if (value) { /* negative - bit 7 is set */ @@ -159,31 +183,35 @@ int decode_signed24(uint8_t * apdu, int32_t * value) *value = 0xFF000000; else *value = 0; - *value |= ((int32_t)((((int32_t)apdu[0]) << 16) & 0x00ff0000)); - *value |= ((int32_t)((((int32_t)apdu[1]) << 8) & 0x0000ff00)); - *value |= ((int32_t)(((int32_t)apdu[2]) & 0x000000ff)); + *value |= ((int32_t) ((((int32_t) apdu[0]) << 16) & 0x00ff0000)); + *value |= ((int32_t) ((((int32_t) apdu[1]) << 8) & 0x0000ff00)); + *value |= ((int32_t) (((int32_t) apdu[2]) & 0x000000ff)); } return 3; } -int encode_signed32(uint8_t * apdu, int32_t value) +int encode_signed32( + uint8_t * apdu, + int32_t value) { - apdu[0] = (uint8_t)((value & 0xff000000) >> 24); - apdu[1] = (uint8_t)((value & 0x00ff0000) >> 16); - apdu[2] = (uint8_t)((value & 0x0000ff00) >> 8); - apdu[3] = (uint8_t)(value & 0x000000ff); + apdu[0] = (uint8_t) ((value & 0xff000000) >> 24); + apdu[1] = (uint8_t) ((value & 0x00ff0000) >> 16); + apdu[2] = (uint8_t) ((value & 0x0000ff00) >> 8); + apdu[3] = (uint8_t) (value & 0x000000ff); return 4; } -int decode_signed32(uint8_t * apdu, int32_t * value) +int decode_signed32( + uint8_t * apdu, + int32_t * value) { if (value) { - *value = ((int32_t)((((int32_t)apdu[0]) << 24) & 0xff000000)); - *value |= ((int32_t)((((int32_t)apdu[1]) << 16) & 0x00ff0000)); - *value |= ((int32_t)((((int32_t)apdu[2]) << 8) & 0x0000ff00)); - *value |= ((int32_t)(((int32_t)apdu[3]) & 0x000000ff)); + *value = ((int32_t) ((((int32_t) apdu[0]) << 24) & 0xff000000)); + *value |= ((int32_t) ((((int32_t) apdu[1]) << 16) & 0x00ff0000)); + *value |= ((int32_t) ((((int32_t) apdu[2]) << 8) & 0x0000ff00)); + *value |= ((int32_t) (((int32_t) apdu[3]) & 0x000000ff)); } return 4; @@ -196,13 +224,14 @@ int decode_signed32(uint8_t * apdu, int32_t * value) #include #include "ctest.h" -void testBACnetUnsigned16(Test * pTest) +void testBACnetUnsigned16( + Test * pTest) { uint8_t apdu[32] = { 0 }; uint16_t value = 0, test_value = 0; int len = 0; - for (value = 0; ; value++) { + for (value = 0;; value++) { len = encode_unsigned16(&apdu[0], value); ct_test(pTest, len == 2); len = decode_unsigned16(&apdu[0], &test_value); @@ -212,13 +241,14 @@ void testBACnetUnsigned16(Test * pTest) } } -void testBACnetUnsigned24(Test * pTest) +void testBACnetUnsigned24( + Test * pTest) { uint8_t apdu[32] = { 0 }; uint32_t value = 0, test_value = 0; int len = 0; - for (value = 0; ; value+=0xf) { + for (value = 0;; value += 0xf) { len = encode_unsigned24(&apdu[0], value); ct_test(pTest, len == 3); len = decode_unsigned24(&apdu[0], &test_value); @@ -228,13 +258,14 @@ void testBACnetUnsigned24(Test * pTest) } } -void testBACnetUnsigned32(Test * pTest) +void testBACnetUnsigned32( + Test * pTest) { uint8_t apdu[32] = { 0 }; uint32_t value = 0, test_value = 0; int len = 0; - for (value = 0; ; value+=0xff) { + for (value = 0;; value += 0xff) { len = encode_unsigned32(&apdu[0], value); ct_test(pTest, len == 4); len = decode_unsigned32(&apdu[0], &test_value); @@ -244,13 +275,14 @@ void testBACnetUnsigned32(Test * pTest) } } -void testBACnetSigned8(Test * pTest) +void testBACnetSigned8( + Test * pTest) { uint8_t apdu[32] = { 0 }; int32_t value = 0, test_value = 0; int len = 0; - for (value = -127; ; value++) { + for (value = -127;; value++) { len = encode_signed8(&apdu[0], value); ct_test(pTest, len == 1); len = decode_signed8(&apdu[0], &test_value); @@ -260,13 +292,14 @@ void testBACnetSigned8(Test * pTest) } } -void testBACnetSigned16(Test * pTest) +void testBACnetSigned16( + Test * pTest) { uint8_t apdu[32] = { 0 }; int32_t value = 0, test_value = 0; int len = 0; - for (value = -32767; ; value++) { + for (value = -32767;; value++) { len = encode_signed16(&apdu[0], value); ct_test(pTest, len == 2); len = decode_signed16(&apdu[0], &test_value); @@ -276,13 +309,14 @@ void testBACnetSigned16(Test * pTest) } } -void testBACnetSigned24(Test * pTest) +void testBACnetSigned24( + Test * pTest) { uint8_t apdu[32] = { 0 }; int32_t value = 0, test_value = 0; int len = 0; - for (value = -8388607; value <= 8388607; value+=15) { + for (value = -8388607; value <= 8388607; value += 15) { len = encode_signed24(&apdu[0], value); ct_test(pTest, len == 3); len = decode_signed24(&apdu[0], &test_value); @@ -290,19 +324,20 @@ void testBACnetSigned24(Test * pTest) } } -void testBACnetSigned32(Test * pTest) +void testBACnetSigned32( + Test * pTest) { uint8_t apdu[32] = { 0 }; int32_t value = 0, test_value = 0; int len = 0; - for (value = -2147483647; value < 0; value+=127) { + for (value = -2147483647; value < 0; value += 127) { len = encode_signed32(&apdu[0], value); ct_test(pTest, len == 4); len = decode_signed32(&apdu[0], &test_value); ct_test(pTest, value == test_value); } - for (value = 2147483647; value > 0; value-=127) { + for (value = 2147483647; value > 0; value -= 127) { len = encode_signed32(&apdu[0], value); ct_test(pTest, len == 4); len = decode_signed32(&apdu[0], &test_value); @@ -311,7 +346,8 @@ void testBACnetSigned32(Test * pTest) } #ifdef TEST_BACINT -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -340,5 +376,5 @@ int main(void) return 0; } -#endif /* TEST_BACINT */ -#endif /* TEST */ +#endif /* TEST_BACINT */ +#endif /* TEST */ diff --git a/bacnet-stack/src/bacprop.c b/bacnet-stack/src/bacprop.c index 7bfa0013..67b259c3 100644 --- a/bacnet-stack/src/bacprop.c +++ b/bacnet-stack/src/bacprop.c @@ -76,8 +76,10 @@ PROP_TAG_DATA bacnet_object_device_property_tag_map[] = { {-1, -1} }; -signed bacprop_tag_by_index_default(PROP_TAG_DATA * data_list, - signed index, signed default_ret) +signed bacprop_tag_by_index_default( + PROP_TAG_DATA * data_list, + signed index, + signed default_ret) { signed pUnsigned = 0; @@ -95,16 +97,18 @@ signed bacprop_tag_by_index_default(PROP_TAG_DATA * data_list, } -signed bacprop_property_tag(BACNET_OBJECT_TYPE type, signed prop) +signed bacprop_property_tag( + BACNET_OBJECT_TYPE type, + signed prop) { switch (type) { - case OBJECT_DEVICE: - return - bacprop_tag_by_index_default - (bacnet_object_device_property_tag_map, prop, -1); - default: - fprintf(stderr, "Unsupported object type"); - break; + case OBJECT_DEVICE: + return + bacprop_tag_by_index_default + (bacnet_object_device_property_tag_map, prop, -1); + default: + fprintf(stderr, "Unsupported object type"); + break; } return -1; diff --git a/bacnet-stack/src/bacreal.c b/bacnet-stack/src/bacreal.c index d5bd5018..9a29bea4 100644 --- a/bacnet-stack/src/bacreal.c +++ b/bacnet-stack/src/bacreal.c @@ -44,12 +44,14 @@ /* NOTE: byte order plays a role in decoding multibyte values */ /* http://www.unixpapa.com/incnote/byteorder.html */ #ifndef BIG_ENDIAN - #error Define BIG_ENDIAN=0 or BIG_ENDIAN=1 for BACnet Stack in compiler settings +#error Define BIG_ENDIAN=0 or BIG_ENDIAN=1 for BACnet Stack in compiler settings #endif /* from clause 20.2.6 Encoding of a Real Number Value */ /* returns the number of apdu bytes consumed */ -int decode_real(uint8_t * apdu, float *real_value) +int decode_real( + uint8_t * apdu, + float *real_value) { union { uint8_t byte[4]; @@ -76,7 +78,9 @@ int decode_real(uint8_t * apdu, float *real_value) /* from clause 20.2.6 Encoding of a Real Number Value */ /* returns the number of apdu bytes consumed */ -int encode_bacnet_real(float value, uint8_t * apdu) +int encode_bacnet_real( + float value, + uint8_t * apdu) { union { uint8_t byte[4]; @@ -107,12 +111,13 @@ int encode_bacnet_real(float value, uint8_t * apdu) #include #include "ctest.h" -void testBACreal(Test * pTest) +void testBACreal( + Test * pTest) { float real_value = 3.14159F, test_real_value = 0.0; uint8_t apdu[MAX_APDU] = { 0 }; int len = 0, test_len = 0; - + len = encode_bacnet_real(real_value, &apdu[0]); ct_test(pTest, len == 4); test_len = decode_real(&apdu[0], &test_real_value); @@ -120,7 +125,8 @@ void testBACreal(Test * pTest) } #ifdef TEST_BACNET_REAL -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -138,6 +144,5 @@ int main(void) return 0; } -#endif /* TEST_BACNET_REAL */ -#endif /* TEST */ - +#endif /* TEST_BACNET_REAL */ +#endif /* TEST */ diff --git a/bacnet-stack/src/bacstr.c b/bacnet-stack/src/bacstr.c index 3c0ed66d..6b492683 100644 --- a/bacnet-stack/src/bacstr.c +++ b/bacnet-stack/src/bacstr.c @@ -34,11 +34,12 @@ #include #include -#include /* for strlen */ +#include /* for strlen */ #include "bacstr.h" #include "bits.h" -void bitstring_init(BACNET_BIT_STRING * bit_string) +void bitstring_init( + BACNET_BIT_STRING * bit_string) { int i; @@ -48,7 +49,9 @@ void bitstring_init(BACNET_BIT_STRING * bit_string) } } -void bitstring_set_bit(BACNET_BIT_STRING * bit_string, uint8_t bit_number, +void bitstring_set_bit( + BACNET_BIT_STRING * bit_string, + uint8_t bit_number, bool value) { uint8_t byte_number = bit_number / 8; @@ -66,7 +69,9 @@ void bitstring_set_bit(BACNET_BIT_STRING * bit_string, uint8_t bit_number, } } -bool bitstring_bit(BACNET_BIT_STRING * bit_string, uint8_t bit_number) +bool bitstring_bit( + BACNET_BIT_STRING * bit_string, + uint8_t bit_number) { bool value = false; uint8_t byte_number = bit_number / 8; @@ -81,15 +86,17 @@ bool bitstring_bit(BACNET_BIT_STRING * bit_string, uint8_t bit_number) return value; } -uint8_t bitstring_bits_used(BACNET_BIT_STRING * bit_string) +uint8_t bitstring_bits_used( + BACNET_BIT_STRING * bit_string) { return bit_string->bits_used; } /* returns the number of bytes that a bit string is using */ -int bitstring_bytes_used(BACNET_BIT_STRING * bit_string) +int bitstring_bytes_used( + BACNET_BIT_STRING * bit_string) { - int len = 0; /* return value */ + int len = 0; /* return value */ uint8_t used_bytes = 0; uint8_t last_bit = 0; @@ -104,7 +111,9 @@ int bitstring_bytes_used(BACNET_BIT_STRING * bit_string) return len; } -uint8_t bitstring_octet(BACNET_BIT_STRING * bit_string, uint8_t index) +uint8_t bitstring_octet( + BACNET_BIT_STRING * bit_string, + uint8_t index) { uint8_t octet = 0; @@ -117,8 +126,10 @@ uint8_t bitstring_octet(BACNET_BIT_STRING * bit_string, uint8_t index) return octet; } -bool bitstring_set_octet(BACNET_BIT_STRING * bit_string, - uint8_t index, uint8_t octet) +bool bitstring_set_octet( + BACNET_BIT_STRING * bit_string, + uint8_t index, + uint8_t octet) { bool status = false; @@ -132,8 +143,10 @@ bool bitstring_set_octet(BACNET_BIT_STRING * bit_string, return status; } -bool bitstring_set_bits_used(BACNET_BIT_STRING * bit_string, - uint8_t bytes_used, uint8_t unused_bits) +bool bitstring_set_bits_used( + BACNET_BIT_STRING * bit_string, + uint8_t bytes_used, + uint8_t unused_bits) { bool status = false; @@ -147,7 +160,8 @@ bool bitstring_set_bits_used(BACNET_BIT_STRING * bit_string, return status; } -uint8_t bitstring_bits_capacity(BACNET_BIT_STRING * bit_string) +uint8_t bitstring_bits_capacity( + BACNET_BIT_STRING * bit_string) { if (bit_string) return (sizeof(bit_string->value) * 8); @@ -161,7 +175,7 @@ bool bitstring_copy( { unsigned i; bool status = false; - + if (dest && src) { dest->bits_used = src->bits_used; for (i = 0; i < MAX_BITSTRING_BYTES; i++) { @@ -169,18 +183,21 @@ bool bitstring_copy( } status = true; } - + return status; } #define CHARACTER_STRING_CAPACITY (MAX_CHARACTER_STRING_BYTES - 1) /* returns false if the string exceeds capacity initialize by using length=0 */ -bool characterstring_init(BACNET_CHARACTER_STRING * char_string, - uint8_t encoding, char *value, size_t length) +bool characterstring_init( + BACNET_CHARACTER_STRING * char_string, + uint8_t encoding, + char *value, + size_t length) { bool status = false; /* return value */ - size_t i; /* counter */ + size_t i; /* counter */ if (char_string) { char_string->length = 0; @@ -208,14 +225,16 @@ bool characterstring_init(BACNET_CHARACTER_STRING * char_string, return status; } -bool characterstring_init_ansi(BACNET_CHARACTER_STRING * char_string, +bool characterstring_init_ansi( + BACNET_CHARACTER_STRING * char_string, char *value) { return characterstring_init(char_string, CHARACTER_ANSI_X34, value, value ? strlen(value) : 0); } -bool characterstring_copy(BACNET_CHARACTER_STRING * dest, +bool characterstring_copy( + BACNET_CHARACTER_STRING * dest, BACNET_CHARACTER_STRING * src) { return characterstring_init(dest, @@ -224,15 +243,15 @@ bool characterstring_copy(BACNET_CHARACTER_STRING * dest, } /* returns true if the character encoding and string contents are the same */ -bool characterstring_same(BACNET_CHARACTER_STRING * dest, +bool characterstring_same( + BACNET_CHARACTER_STRING * dest, BACNET_CHARACTER_STRING * src) { - size_t i; /* counter */ + size_t i; /* counter */ bool same_status = false; if (src && dest) { - if ((src->length == dest->length) && - (src->encoding == dest->encoding)) { + if ((src->length == dest->length) && (src->encoding == dest->encoding)) { same_status = true; for (i = 0; i < src->length; i++) { if (src->value[i] != dest->value[i]) { @@ -252,10 +271,11 @@ bool characterstring_same(BACNET_CHARACTER_STRING * dest, return same_status; } -bool characterstring_ansi_same(BACNET_CHARACTER_STRING * dest, +bool characterstring_ansi_same( + BACNET_CHARACTER_STRING * dest, const char *src) { - size_t i; /* counter */ + size_t i; /* counter */ bool same_status = false; if (src && dest) { @@ -283,10 +303,12 @@ bool characterstring_ansi_same(BACNET_CHARACTER_STRING * dest, } /* returns false if the string exceeds capacity */ -bool characterstring_append(BACNET_CHARACTER_STRING * char_string, - char *value, size_t length) +bool characterstring_append( + BACNET_CHARACTER_STRING * char_string, + char *value, + size_t length) { - size_t i; /* counter */ + size_t i; /* counter */ bool status = false; /* return value */ if (char_string) { @@ -305,7 +327,8 @@ bool characterstring_append(BACNET_CHARACTER_STRING * char_string, /* This function sets a new length without changing the value. If length exceeds capacity, no modification happens and function returns false. */ -bool characterstring_truncate(BACNET_CHARACTER_STRING * char_string, +bool characterstring_truncate( + BACNET_CHARACTER_STRING * char_string, size_t length) { bool status = false; /* return value */ @@ -321,7 +344,8 @@ bool characterstring_truncate(BACNET_CHARACTER_STRING * char_string, } /* Returns the value. */ -char *characterstring_value(BACNET_CHARACTER_STRING * char_string) +char *characterstring_value( + BACNET_CHARACTER_STRING * char_string) { char *value = NULL; @@ -333,7 +357,8 @@ char *characterstring_value(BACNET_CHARACTER_STRING * char_string) } /* returns the length. */ -size_t characterstring_length(BACNET_CHARACTER_STRING * char_string) +size_t characterstring_length( + BACNET_CHARACTER_STRING * char_string) { size_t length = 0; @@ -345,7 +370,8 @@ size_t characterstring_length(BACNET_CHARACTER_STRING * char_string) return length; } -size_t characterstring_capacity(BACNET_CHARACTER_STRING * char_string) +size_t characterstring_capacity( + BACNET_CHARACTER_STRING * char_string) { size_t length = 0; @@ -357,7 +383,8 @@ size_t characterstring_capacity(BACNET_CHARACTER_STRING * char_string) } /* returns the encoding. */ -uint8_t characterstring_encoding(BACNET_CHARACTER_STRING * char_string) +uint8_t characterstring_encoding( + BACNET_CHARACTER_STRING * char_string) { uint8_t encoding = 0; @@ -370,11 +397,13 @@ uint8_t characterstring_encoding(BACNET_CHARACTER_STRING * char_string) /* returns false if the string exceeds capacity initialize by using length=0 */ -bool octetstring_init(BACNET_OCTET_STRING * octet_string, - uint8_t * value, size_t length) +bool octetstring_init( + BACNET_OCTET_STRING * octet_string, + uint8_t * value, + size_t length) { bool status = false; /* return value */ - size_t i; /* counter */ + size_t i; /* counter */ if (octet_string) { octet_string->length = 0; @@ -396,7 +425,8 @@ bool octetstring_init(BACNET_OCTET_STRING * octet_string, return status; } -bool octetstring_copy(BACNET_OCTET_STRING * dest, +bool octetstring_copy( + BACNET_OCTET_STRING * dest, BACNET_OCTET_STRING * src) { return octetstring_init(dest, @@ -404,10 +434,12 @@ bool octetstring_copy(BACNET_OCTET_STRING * dest, } /* returns false if the string exceeds capacity */ -bool octetstring_append(BACNET_OCTET_STRING * octet_string, - uint8_t * value, size_t length) +bool octetstring_append( + BACNET_OCTET_STRING * octet_string, + uint8_t * value, + size_t length) { - size_t i; /* counter */ + size_t i; /* counter */ bool status = false; /* return value */ if (octet_string) { @@ -426,7 +458,8 @@ bool octetstring_append(BACNET_OCTET_STRING * octet_string, /* This function sets a new length without changing the value. If length exceeds capacity, no modification happens and function returns false. */ -bool octetstring_truncate(BACNET_OCTET_STRING * octet_string, +bool octetstring_truncate( + BACNET_OCTET_STRING * octet_string, size_t length) { bool status = false; /* return value */ @@ -442,7 +475,8 @@ bool octetstring_truncate(BACNET_OCTET_STRING * octet_string, } /* returns the length. Returns the value in parameter. */ -uint8_t *octetstring_value(BACNET_OCTET_STRING * octet_string) +uint8_t *octetstring_value( + BACNET_OCTET_STRING * octet_string) { uint8_t *value = NULL; @@ -454,7 +488,8 @@ uint8_t *octetstring_value(BACNET_OCTET_STRING * octet_string) } /* returns the length. */ -size_t octetstring_length(BACNET_OCTET_STRING * octet_string) +size_t octetstring_length( + BACNET_OCTET_STRING * octet_string) { size_t length = 0; @@ -467,7 +502,8 @@ size_t octetstring_length(BACNET_OCTET_STRING * octet_string) } /* returns the length. */ -size_t octetstring_capacity(BACNET_OCTET_STRING * octet_string) +size_t octetstring_capacity( + BACNET_OCTET_STRING * octet_string) { size_t length = 0; @@ -484,7 +520,8 @@ size_t octetstring_capacity(BACNET_OCTET_STRING * octet_string) #include #include "ctest.h" -void testBitString(Test * pTest) +void testBitString( + Test * pTest) { uint8_t bit = 0; BACNET_BIT_STRING bit_string; @@ -511,7 +548,8 @@ void testBitString(Test * pTest) } } -void testCharacterString(Test * pTest) +void testCharacterString( + Test * pTest) { BACNET_CHARACTER_STRING bacnet_string; char *value = "Joshua,Mary,Anna,Christopher"; @@ -524,8 +562,7 @@ void testCharacterString(Test * pTest) size_t i = 0; /* verify initialization */ - status = - characterstring_init(&bacnet_string, CHARACTER_ANSI_X34, NULL, 0); + status = characterstring_init(&bacnet_string, CHARACTER_ANSI_X34, NULL, 0); ct_test(pTest, status == true); ct_test(pTest, characterstring_length(&bacnet_string) == 0); ct_test(pTest, @@ -569,7 +606,8 @@ void testCharacterString(Test * pTest) } } -void testOctetString(Test * pTest) +void testOctetString( + Test * pTest) { BACNET_OCTET_STRING bacnet_string; uint8_t *value = NULL; @@ -631,7 +669,8 @@ void testOctetString(Test * pTest) } #ifdef TEST_BACSTR -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -652,5 +691,5 @@ int main(void) return 0; } -#endif /* TEST_BACSTR */ -#endif /* TEST */ +#endif /* TEST_BACSTR */ +#endif /* TEST */ diff --git a/bacnet-stack/src/bactext.c b/bacnet-stack/src/bactext.c index 46a8f4ff..c0c86ea1 100644 --- a/bacnet-stack/src/bactext.c +++ b/bacnet-stack/src/bactext.c @@ -75,7 +75,8 @@ INDTEXT_DATA bacnet_confirmed_service_names[] = { {0, NULL} }; -const char *bactext_confirmed_service_name(int index) +const char *bactext_confirmed_service_name( + int index) { return indtext_by_index_default(bacnet_confirmed_service_names, index, ASHRAE_Reserved_String); @@ -106,7 +107,8 @@ INDTEXT_DATA bacnet_unconfirmed_service_names[] = { {0, NULL} }; -const char *bactext_unconfirmed_service_name(int index) +const char *bactext_unconfirmed_service_name( + int index) { return indtext_by_index_default(bacnet_unconfirmed_service_names, index, ASHRAE_Reserved_String); @@ -148,7 +150,8 @@ INDTEXT_DATA bacnet_application_tag_names[] = { {0, NULL} }; -const char *bactext_application_tag_name(int index) +const char *bactext_application_tag_name( + int index) { return indtext_by_index_default(bacnet_application_tag_names, index, ASHRAE_Reserved_String); @@ -223,7 +226,8 @@ INDTEXT_DATA bacnet_object_type_names[] = { the procedures and constraints described in Clause 23. */ }; -const char *bactext_object_type_name(int index) +const char *bactext_object_type_name( + int index) { return indtext_by_index_split_default(bacnet_object_type_names, index, 128, ASHRAE_Reserved_String, Vendor_Proprietary_String); @@ -685,13 +689,15 @@ INDTEXT_DATA bacnet_property_names[] = { procedures and constraints described in Clause 23. */ }; -const char *bactext_property_name(int index) +const char *bactext_property_name( + int index) { return indtext_by_index_split_default(bacnet_property_names, index, 512, ASHRAE_Reserved_String, Vendor_Proprietary_String); } -unsigned bactext_property_id(const char *name) +unsigned bactext_property_id( + const char *name) { return indtext_by_istring_default(bacnet_property_names, name, 0); } @@ -1090,7 +1096,8 @@ INDTEXT_DATA bacnet_engineering_unit_names[] = { the procedures and constraints described in Clause 23. */ }; -const char *bactext_engineering_unit_name(int index) +const char *bactext_engineering_unit_name( + int index) { return indtext_by_index_split_default(bacnet_engineering_unit_names, index, 256, ASHRAE_Reserved_String, Vendor_Proprietary_String); @@ -1121,7 +1128,8 @@ INDTEXT_DATA bacnet_reject_reason_names[] = { {0, NULL} }; -const char *bactext_reject_reason_name(int index) +const char *bactext_reject_reason_name( + int index) { return indtext_by_index_split_default(bacnet_reject_reason_names, index, @@ -1144,7 +1152,8 @@ INDTEXT_DATA bacnet_abort_reason_names[] = { {0, NULL} }; -const char *bactext_abort_reason_name(int index) +const char *bactext_abort_reason_name( + int index) { return indtext_by_index_split_default(bacnet_abort_reason_names, index, @@ -1170,7 +1179,8 @@ INDTEXT_DATA bacnet_error_class_names[] = { {0, NULL} }; -const char *bactext_error_class_name(int index) +const char *bactext_error_class_name( + int index) { return indtext_by_index_split_default(bacnet_error_class_names, index, @@ -1294,7 +1304,8 @@ INDTEXT_DATA bacnet_error_code_names[] = { {0, NULL} }; -const char *bactext_error_code_name(int index) +const char *bactext_error_code_name( + int index) { return indtext_by_index_split_default(bacnet_error_code_names, index, @@ -1332,7 +1343,8 @@ INDTEXT_DATA bacnet_month_names[] = { {0, NULL} }; -const char *bactext_month_name(int index) +const char *bactext_month_name( + int index) { return indtext_by_index_default(bacnet_month_names, index, ASHRAE_Reserved_String); @@ -1356,7 +1368,8 @@ INDTEXT_DATA bacnet_week_of_month_names[] = { {0, NULL} }; -const char *bactext_week_of_month_name(int index) +const char *bactext_week_of_month_name( + int index) { return indtext_by_index_default(bacnet_week_of_month_names, index, ASHRAE_Reserved_String); @@ -1383,7 +1396,8 @@ INDTEXT_DATA bacnet_day_of_week_names[] = { {0, NULL} }; -const char *bactext_day_of_week_name(int index) +const char *bactext_day_of_week_name( + int index) { return indtext_by_index_default(bacnet_day_of_week_names, index, ASHRAE_Reserved_String); @@ -1403,7 +1417,8 @@ INDTEXT_DATA bacnet_event_state_names[] = { {0, NULL} }; -const char *bactext_event_state_name(int index) +const char *bactext_event_state_name( + int index) { return indtext_by_index_default(bacnet_event_state_names, index, ASHRAE_Reserved_String); @@ -1417,7 +1432,8 @@ INDTEXT_DATA bacnet_binary_present_value_names[] = { {0, NULL} }; -const char *bactext_binary_present_value_name(int index) +const char *bactext_binary_present_value_name( + int index) { return indtext_by_index_default(bacnet_binary_present_value_names, index, ASHRAE_Reserved_String); @@ -1449,7 +1465,8 @@ INDTEXT_DATA bacnet_reliability_names[] = { {0, NULL} }; -const char *bactext_reliability_name(int index) +const char *bactext_reliability_name( + int index) { return indtext_by_index_default(bacnet_reliability_names, index, ASHRAE_Reserved_String); @@ -1469,7 +1486,8 @@ INDTEXT_DATA bacnet_device_status_names[] = { {0, NULL} }; -const char *bactext_device_status_name(int index) +const char *bactext_device_status_name( + int index) { return indtext_by_index_default(bacnet_device_status_names, index, ASHRAE_Reserved_String); @@ -1487,7 +1505,8 @@ INDTEXT_DATA bacnet_segmentation_names[] = { {0, NULL} }; -const char *bactext_segmentation_name(int index) +const char *bactext_segmentation_name( + int index) { return indtext_by_index_default(bacnet_segmentation_names, index, ASHRAE_Reserved_String); diff --git a/bacnet-stack/src/bigend.c b/bacnet-stack/src/bigend.c index 222327b9..db66b4e9 100644 --- a/bacnet-stack/src/bigend.c +++ b/bacnet-stack/src/bigend.c @@ -15,7 +15,8 @@ /* x[2] = 0x03 */ /* x[3] = 0x04 */ -int big_endian(void) +int big_endian( + void) { union { long l; diff --git a/bacnet-stack/src/bip.c b/bacnet-stack/src/bip.c index 8dcd5294..32aaf0fc 100644 --- a/bacnet-stack/src/bip.c +++ b/bacnet-stack/src/bip.c @@ -32,12 +32,12 @@ ------------------------------------------- ####COPYRIGHTEND####*/ -#include /* for standard integer types uint8_t etc. */ -#include /* for the standard bool type. */ +#include /* for standard integer types uint8_t etc. */ +#include /* for the standard bool type. */ #include "bacdcode.h" #include "bacint.h" #include "bip.h" -#include "net.h" /* custom per port */ +#include "net.h" /* custom per port */ static int BIP_Socket = -1; /* port to use - stored in host byte order */ @@ -47,22 +47,26 @@ static struct in_addr BIP_Address; /* Broadcast Address - stored in host byte order */ static struct in_addr BIP_Broadcast_Address; -void bip_set_socket(int sock_fd) +void bip_set_socket( + int sock_fd) { BIP_Socket = sock_fd; } -int bip_socket(void) +int bip_socket( + void) { return BIP_Socket; } -bool bip_valid(void) +bool bip_valid( + void) { return (BIP_Socket != -1); } -void bip_cleanup(void) +void bip_cleanup( + void) { if (bip_valid()) close(BIP_Socket); @@ -72,48 +76,55 @@ void bip_cleanup(void) } /* set using network byte order */ -void bip_set_addr(uint32_t net_address) +void bip_set_addr( + uint32_t net_address) { BIP_Address.s_addr = ntohl(net_address); } /* returns host byte order */ -uint32_t bip_get_addr(void) +uint32_t bip_get_addr( + void) { return BIP_Address.s_addr; } /* set using network byte order */ -void bip_set_broadcast_addr(uint32_t net_address) +void bip_set_broadcast_addr( + uint32_t net_address) { BIP_Broadcast_Address.s_addr = ntohl(net_address); } /* returns host byte order */ -uint32_t bip_get_broadcast_addr(void) +uint32_t bip_get_broadcast_addr( + void) { return BIP_Broadcast_Address.s_addr; } /* set using host byte order */ -void bip_set_port(uint16_t port) +void bip_set_port( + uint16_t port) { BIP_Port = port; } /* returns host byte order */ -uint16_t bip_get_port(void) +uint16_t bip_get_port( + void) { return BIP_Port; } /* function to send a packet out the BACnet/IP socket (Annex J) */ /* returns number of bytes sent on success, negative number on failure */ -int bip_send_pdu(BACNET_ADDRESS * dest, /* destination address */ +int bip_send_pdu( + BACNET_ADDRESS * dest, /* destination address */ BACNET_NPDU_DATA * npdu_data, /* network information */ - uint8_t * pdu, /* any data to be sent - may be null */ + uint8_t * pdu, /* any data to be sent - may be null */ unsigned pdu_len) -{ /* number of bytes of data */ +{ /* number of bytes of data */ struct sockaddr_in bip_dest; uint8_t mtu[MAX_MPDU] = { 0 }; int mtu_len = 0; @@ -135,7 +146,7 @@ int bip_send_pdu(BACNET_ADDRESS * dest, /* destination address */ } else if (dest->mac_len == 6) { /* valid unicast */ (void) decode_unsigned32(&dest->mac[0], - (uint32_t *)&(bip_dest.sin_addr.s_addr)); + (uint32_t *) & (bip_dest.sin_addr.s_addr)); (void) decode_unsigned16(&dest->mac[4], &(bip_dest.sin_port)); memset(&(bip_dest.sin_zero), '\0', 8); mtu[1] = BVLC_ORIGINAL_UNICAST_NPDU; @@ -160,11 +171,12 @@ int bip_send_pdu(BACNET_ADDRESS * dest, /* destination address */ /* receives a BACnet/IP packet */ /* returns the number of octets in the PDU, or zero on failure */ -uint16_t bip_receive(BACNET_ADDRESS * src, /* source address */ - uint8_t * pdu, /* PDU data */ - uint16_t max_pdu, /* amount of space available in the PDU */ +uint16_t bip_receive( + BACNET_ADDRESS * src, /* source address */ + uint8_t * pdu, /* PDU data */ + uint16_t max_pdu, /* amount of space available in the PDU */ unsigned timeout) -{ /* number of milliseconds to wait for a packet */ +{ /* number of milliseconds to wait for a packet */ int received_bytes = 0; uint16_t pdu_len = 0; /* return value */ fd_set read_fds; @@ -195,8 +207,7 @@ uint16_t bip_receive(BACNET_ADDRESS * src, /* source address */ /* see if there is a packet for us */ if (select(max + 1, &read_fds, NULL, NULL, &select_timeout) > 0) received_bytes = recvfrom(BIP_Socket, - (char *) &pdu[0], max_pdu, 0, - (struct sockaddr *) &sin, &sin_len); + (char *) &pdu[0], max_pdu, 0, (struct sockaddr *) &sin, &sin_len); else return 0; @@ -233,7 +244,7 @@ uint16_t bip_receive(BACNET_ADDRESS * src, /* source address */ if (pdu_len < max_pdu) { /* shift the buffer to return a valid PDU */ for (i = 0; i < pdu_len; i++) { - pdu[i] = pdu[i+4]; + pdu[i] = pdu[i + 4]; } } /* ignore packets that are too large */ @@ -246,13 +257,13 @@ uint16_t bip_receive(BACNET_ADDRESS * src, /* source address */ return pdu_len; } -void bip_get_my_address(BACNET_ADDRESS * my_address) +void bip_get_my_address( + BACNET_ADDRESS * my_address) { int i = 0; my_address->mac_len = 6; - (void) encode_unsigned32(&my_address->mac[0], - htonl(BIP_Address.s_addr)); + (void) encode_unsigned32(&my_address->mac[0], htonl(BIP_Address.s_addr)); (void) encode_unsigned16(&my_address->mac[4], htons(BIP_Port)); my_address->net = 0; /* local only, no routing */ my_address->len = 0; /* no SLEN */ @@ -264,9 +275,10 @@ void bip_get_my_address(BACNET_ADDRESS * my_address) return; } -void bip_get_broadcast_address(BACNET_ADDRESS * dest) -{ /* destination address */ - int i = 0; /* counter */ +void bip_get_broadcast_address( + BACNET_ADDRESS * dest) +{ /* destination address */ + int i = 0; /* counter */ if (dest) { dest->mac_len = 6; @@ -274,7 +286,7 @@ void bip_get_broadcast_address(BACNET_ADDRESS * dest) htonl(BIP_Broadcast_Address.s_addr)); (void) encode_unsigned16(&dest->mac[4], htons(BIP_Port)); dest->net = BACNET_BROADCAST_NETWORK; - dest->len = 0; /* no SLEN */ + dest->len = 0; /* no SLEN */ for (i = 0; i < MAX_MAC_LEN; i++) { /* no SADR */ dest->adr[i] = 0; diff --git a/bacnet-stack/src/bvlc.c b/bacnet-stack/src/bvlc.c index 1377c3bf..7caa0492 100644 --- a/bacnet-stack/src/bvlc.c +++ b/bacnet-stack/src/bvlc.c @@ -32,9 +32,9 @@ ------------------------------------------- ####COPYRIGHTEND####*/ -#include /* for standard integer types uint8_t etc. */ -#include /* for the standard bool type. */ -#include /* for the standard bool type. */ +#include /* for standard integer types uint8_t etc. */ +#include /* for the standard bool type. */ +#include /* for the standard bool type. */ #include "bacdcode.h" #include "bacint.h" #include "bvlc.h" @@ -45,8 +45,7 @@ Broadcast Distribution Table, and Foreign Device Registration */ -typedef struct -{ +typedef struct { /* true if valid entry - false if not */ bool valid; /* BACnet/IP address */ @@ -69,8 +68,7 @@ seconds remaining before the BBMD will purge the registrant's FDT entry if no re-registration occurs. This value will be initialized to the 2-octet Time-to-Live value supplied at the time of registration.*/ -typedef struct -{ +typedef struct { bool valid; /* BACnet/IP address */ struct in_addr dest_address; @@ -79,7 +77,7 @@ typedef struct /* seconds for valid entry lifetime */ uint16_t time_to_live; /* our counter */ - time_t seconds_remaining; /* includes 30 second grace period */ + time_t seconds_remaining; /* includes 30 second grace period */ } FD_TABLE_ENTRY; #define MAX_FD_ENTRIES 128 @@ -88,7 +86,8 @@ static FD_TABLE_ENTRY FD_Table[MAX_FD_ENTRIES]; /* result from a client request */ BACNET_BVLC_RESULT BVLC_Result_Code = BVLC_RESULT_SUCCESSFUL_COMPLETION; -void bvlc_maintenance_timer(unsigned seconds) +void bvlc_maintenance_timer( + unsigned seconds) { unsigned i = 0; @@ -109,8 +108,8 @@ void bvlc_maintenance_timer(unsigned seconds) } int bvlc_encode_bip_address( - uint8_t * pdu, /* buffer to store encoding */ - struct in_addr *address, /* in host format */ + uint8_t * pdu, /* buffer to store encoding */ + struct in_addr *address, /* in host format */ uint16_t port) { int len = 0; @@ -124,8 +123,8 @@ int bvlc_encode_bip_address( } int bvlc_decode_bip_address( - uint8_t * pdu, /* buffer to extract encoded address */ - struct in_addr * address, /* in host format */ + uint8_t * pdu, /* buffer to extract encoded address */ + struct in_addr *address, /* in host format */ uint16_t * port) { int len = 0; @@ -142,7 +141,8 @@ int bvlc_decode_bip_address( } /* used for both read and write entries */ -int bvlc_encode_address_entry(uint8_t * pdu, +int bvlc_encode_address_entry( + uint8_t * pdu, struct in_addr *address, uint16_t port, struct in_addr *mask) @@ -234,7 +234,7 @@ int bvlc_encode_read_bdt_ack( uint8_t * pdu, uint16_t max_pdu) { - int pdu_len = 0; /* return value */ + int pdu_len = 0; /* return value */ int len = 0; unsigned count = 0; unsigned i; @@ -244,20 +244,18 @@ int bvlc_encode_read_bdt_ack( count++; } } - len = bvlc_encode_read_bdt_ack_init(&pdu[0],count); + len = bvlc_encode_read_bdt_ack_init(&pdu[0], count); pdu_len += len; for (i = 0; i < MAX_BBMD_ENTRIES; i++) { if (BBMD_Table[i].valid) { /* too much to send */ - if ((pdu_len+10) > max_pdu) { + if ((pdu_len + 10) > max_pdu) { pdu_len = 0; break; } - len = bvlc_encode_address_entry( - &pdu[pdu_len], + len = bvlc_encode_address_entry(&pdu[pdu_len], &BBMD_Table[i].dest_address, - BBMD_Table[i].dest_port, - &BBMD_Table[i].broadcast_mask); + BBMD_Table[i].dest_port, &BBMD_Table[i].broadcast_mask); pdu_len += len; } } @@ -266,14 +264,15 @@ int bvlc_encode_read_bdt_ack( } -int bvlc_encode_forwarded_npdu(uint8_t * pdu, +int bvlc_encode_forwarded_npdu( + uint8_t * pdu, BACNET_ADDRESS * src, uint8_t * npdu, unsigned npdu_length) { int len = 0; - unsigned i; /* for loop counter */ + unsigned i; /* for loop counter */ if (pdu) { pdu[0] = BVLL_TYPE_BACNET_IP; @@ -296,7 +295,8 @@ int bvlc_encode_forwarded_npdu(uint8_t * pdu, return len; } -int bvlc_encode_register_foreign_device(uint8_t * pdu, +int bvlc_encode_register_foreign_device( + uint8_t * pdu, uint16_t time_to_live_seconds) { int len = 0; @@ -356,7 +356,7 @@ int bvlc_encode_read_fdt_ack( uint8_t * pdu, uint16_t max_pdu) { - int pdu_len = 0; /* return value */ + int pdu_len = 0; /* return value */ int len = 0; unsigned count = 0; unsigned i; @@ -366,19 +366,17 @@ int bvlc_encode_read_fdt_ack( count++; } } - len = bvlc_encode_read_fdt_ack_init(&pdu[0],count); + len = bvlc_encode_read_fdt_ack_init(&pdu[0], count); pdu_len += len; for (i = 0; i < MAX_FD_ENTRIES; i++) { if (FD_Table[i].valid) { /* too much to send */ - if ((pdu_len+10) > max_pdu) { + if ((pdu_len + 10) > max_pdu) { pdu_len = 0; break; } - len = bvlc_encode_bip_address( - &pdu[pdu_len], - &FD_Table[i].dest_address, - FD_Table[i].dest_port); + len = bvlc_encode_bip_address(&pdu[pdu_len], + &FD_Table[i].dest_address, FD_Table[i].dest_port); pdu_len += len; encode_unsigned16(&pdu[pdu_len], FD_Table[i].time_to_live); pdu_len += len; @@ -391,7 +389,8 @@ int bvlc_encode_read_fdt_ack( } -int bvlc_encode_delete_fdt_entry(uint8_t * pdu, +int bvlc_encode_delete_fdt_entry( + uint8_t * pdu, struct in_addr *address, uint16_t port) { @@ -413,12 +412,13 @@ int bvlc_encode_delete_fdt_entry(uint8_t * pdu, return len; } -int bvlc_encode_distribute_broadcast_to_network(uint8_t * pdu, +int bvlc_encode_distribute_broadcast_to_network( + uint8_t * pdu, uint8_t * npdu, unsigned npdu_length) { - int len = 0; /* return value */ - unsigned i; /* for loop counter */ + int len = 0; /* return value */ + unsigned i; /* for loop counter */ if (pdu) { pdu[0] = BVLL_TYPE_BACNET_IP; @@ -436,12 +436,13 @@ int bvlc_encode_distribute_broadcast_to_network(uint8_t * pdu, return len; } -int bvlc_encode_original_unicast_npdu(uint8_t * pdu, +int bvlc_encode_original_unicast_npdu( + uint8_t * pdu, uint8_t * npdu, unsigned npdu_length) { - int len = 0; /* return value */ - unsigned i = 0; /* loop counter */ + int len = 0; /* return value */ + unsigned i = 0; /* loop counter */ if (pdu) { pdu[0] = BVLL_TYPE_BACNET_IP; @@ -460,12 +461,13 @@ int bvlc_encode_original_unicast_npdu(uint8_t * pdu, } -int bvlc_encode_original_broadcast_npdu(uint8_t * pdu, +int bvlc_encode_original_broadcast_npdu( + uint8_t * pdu, uint8_t * npdu, unsigned npdu_length) { - int len = 0; /* return value */ - unsigned i = 0; /* loop counter */ + int len = 0; /* return value */ + unsigned i = 0; /* loop counter */ if (pdu) { pdu[0] = BVLL_TYPE_BACNET_IP; @@ -486,9 +488,9 @@ int bvlc_encode_original_broadcast_npdu(uint8_t * pdu, /* copy the source internet address to the BACnet address */ /* FIXME: IPv6? */ void bvlc_internet_to_bacnet_address( - BACNET_ADDRESS * src, /* returns the BACnet source address */ + BACNET_ADDRESS * src, /* returns the BACnet source address */ struct sockaddr_in *sin) -{ /* source internet address */ +{ /* source internet address */ int len = 0; uint32_t address; uint16_t port; @@ -510,9 +512,9 @@ void bvlc_internet_to_bacnet_address( /* FIXME: IPv6? */ /* FIXME: is sockaddr_in host or network order? */ void bvlc_bacnet_to_internet_address( - struct sockaddr_in *sin, /* source internet address */ - BACNET_ADDRESS * src) /* returns the BACnet source address */ -{ + struct sockaddr_in *sin, /* source internet address */ + BACNET_ADDRESS * src) +{ /* returns the BACnet source address */ int len = 0; uint32_t address; uint16_t port; @@ -520,7 +522,7 @@ void bvlc_bacnet_to_internet_address( if (src && sin) { if (src->mac_len == 6) { len = decode_unsigned32(&src->mac[0], &address); - len += decode_unsigned16(&src->mac[4], &port ); + len += decode_unsigned16(&src->mac[4], &port); sin->sin_addr.s_addr = htonl(address); sin->sin_port = htons(port); } @@ -567,16 +569,17 @@ bool bvlc_create_bdt( } bool bvlc_register_foreign_device( - struct sockaddr_in *sin, /* the source address */ - uint16_t time_to_live) /* time in seconds */ -{ + struct sockaddr_in * sin, /* the source address */ + uint16_t time_to_live) +{ /* time in seconds */ unsigned i = 0; bool status = false; /* am I here already? If so, update my time to live... */ for (i = 0; i < MAX_FD_ENTRIES; i++) { if (FD_Table[i].valid) { - if ((FD_Table[i].dest_address.s_addr == ntohl(sin->sin_addr.s_addr)) && + if ((FD_Table[i].dest_address.s_addr == + ntohl(sin->sin_addr.s_addr)) && (FD_Table[i].dest_port == ntohs(sin->sin_port))) { status = true; FD_Table[i].time_to_live = time_to_live; @@ -602,11 +605,12 @@ bool bvlc_register_foreign_device( return status; } -bool bvlc_delete_foreign_device(uint8_t * pdu) +bool bvlc_delete_foreign_device( + uint8_t * pdu) { - struct sockaddr_in sin = {0}; /* the ip address */ - uint16_t port = 0; /* the decoded port */ - bool status = false; /* return value */ + struct sockaddr_in sin = { 0 }; /* the ip address */ + uint16_t port = 0; /* the decoded port */ + bool status = false; /* return value */ unsigned i = 0; bvlc_decode_bip_address(pdu, &sin.sin_addr, &port); @@ -625,14 +629,14 @@ bool bvlc_delete_foreign_device(uint8_t * pdu) } void bvlc_bdt_forward_npdu( - struct sockaddr_in *sin, /* the source address */ - uint8_t * npdu, /* the NPDU */ + struct sockaddr_in *sin, /* the source address */ + uint8_t * npdu, /* the NPDU */ uint16_t npdu_length) -{ /* length of the NPDU */ - uint8_t mtu[MAX_MPDU] = {0}; +{ /* length of the NPDU */ + uint8_t mtu[MAX_MPDU] = { 0 }; int mtu_len = 0; int bytes_sent = 0; - unsigned i = 0; /* loop counter */ + unsigned i = 0; /* loop counter */ struct sockaddr_in bip_dest; BACNET_ADDRESS src; @@ -641,11 +645,7 @@ void bvlc_bdt_forward_npdu( return; } bvlc_internet_to_bacnet_address(&src, sin); - mtu_len = bvlc_encode_forwarded_npdu( - &mtu[0], - &src, - npdu, - npdu_length); + mtu_len = bvlc_encode_forwarded_npdu(&mtu[0], &src, npdu, npdu_length); /* load destination IP address */ bip_dest.sin_family = AF_INET; /* loop through the BDT and send one to each entry, except us */ @@ -660,8 +660,8 @@ void bvlc_bdt_forward_npdu( BBMD_Table[i].dest_address.s_addr)); bip_dest.sin_port = htons(BBMD_Table[i].dest_port); /* don't send to my broadcast address and same port */ - if ((bip_dest.sin_addr.s_addr == htonl(bip_get_broadcast_addr())) && - (bip_dest.sin_port == htons(bip_get_port()))) { + if ((bip_dest.sin_addr.s_addr == htonl(bip_get_broadcast_addr())) + && (bip_dest.sin_port == htons(bip_get_port()))) { continue; } /* don't send to my ip address and same port */ @@ -672,8 +672,7 @@ void bvlc_bdt_forward_npdu( /* Send the packet */ bytes_sent = sendto(bip_socket(), (char *) mtu, mtu_len, 0, - (struct sockaddr *) &bip_dest, - sizeof(struct sockaddr)); + (struct sockaddr *) &bip_dest, sizeof(struct sockaddr)); } } @@ -681,22 +680,22 @@ void bvlc_bdt_forward_npdu( } void bvlc_broadcast_forward_npdu( - struct sockaddr_in *sin, /* the source address */ - uint8_t * npdu, /* returns the NPDU */ - uint16_t npdu_len) /* amount of space available in the NPDU */ -{ + struct sockaddr_in *sin, /* the source address */ + uint8_t * npdu, /* returns the NPDU */ + uint16_t npdu_len) +{ /* amount of space available in the NPDU */ /* FIXME: write the code... */ } void bvlc_fdt_forward_npdu( - struct sockaddr_in *sin, /* the source address */ - uint8_t * npdu, /* returns the NPDU */ - uint16_t max_npdu) /* amount of space available in the NPDU */ -{ - uint8_t mtu[MAX_MPDU] = {0}; + struct sockaddr_in *sin, /* the source address */ + uint8_t * npdu, /* returns the NPDU */ + uint16_t max_npdu) +{ /* amount of space available in the NPDU */ + uint8_t mtu[MAX_MPDU] = { 0 }; int mtu_len = 0; int bytes_sent = 0; - unsigned i = 0; /* loop counter */ + unsigned i = 0; /* loop counter */ struct sockaddr_in bvlc_dest; BACNET_ADDRESS src; @@ -705,24 +704,18 @@ void bvlc_fdt_forward_npdu( return; } bvlc_internet_to_bacnet_address(&src, sin); - mtu_len = bvlc_encode_forwarded_npdu( - &mtu[0], - &src, - npdu, - max_npdu); + mtu_len = bvlc_encode_forwarded_npdu(&mtu[0], &src, npdu, max_npdu); /* load destination IP address */ bvlc_dest.sin_family = AF_INET; /* loop through the FDT and send one to each entry */ for (i = 0; i < MAX_FD_ENTRIES; i++) { if (FD_Table[i].valid && FD_Table[i].seconds_remaining) { - bvlc_dest.sin_addr.s_addr = - htonl(FD_Table[i].dest_address.s_addr); + bvlc_dest.sin_addr.s_addr = htonl(FD_Table[i].dest_address.s_addr); bvlc_dest.sin_port = htons(FD_Table[i].dest_port); /* Send the packet */ bytes_sent = sendto(bip_socket(), (char *) mtu, mtu_len, 0, - (struct sockaddr *) &bvlc_dest, - sizeof(struct sockaddr)); + (struct sockaddr *) &bvlc_dest, sizeof(struct sockaddr)); } } @@ -730,10 +723,10 @@ void bvlc_fdt_forward_npdu( } void bvlc_send_mpdu( - struct sockaddr_in *dest, /* the destination address */ - uint8_t * mtu, /* the data */ - uint16_t mtu_len) /* amount of data to send */ -{ + struct sockaddr_in *dest, /* the destination address */ + uint8_t * mtu, /* the data */ + uint16_t mtu_len) +{ /* amount of data to send */ int bytes_sent = 0; struct sockaddr_in bvlc_dest; @@ -748,17 +741,16 @@ void bvlc_send_mpdu( /* Send the packet */ bytes_sent = sendto(bip_socket(), (char *) mtu, mtu_len, 0, - (struct sockaddr *) &bvlc_dest, - sizeof(struct sockaddr)); + (struct sockaddr *) &bvlc_dest, sizeof(struct sockaddr)); return; } void bvlc_send_result( - struct sockaddr_in *dest, /* the destination address */ + struct sockaddr_in *dest, /* the destination address */ BACNET_BVLC_RESULT result_code) { - uint8_t mtu[MAX_MPDU] = {0}; + uint8_t mtu[MAX_MPDU] = { 0 }; int mtu_len = 0; mtu_len = bvlc_encode_bvlc_result(&mtu[0], result_code); @@ -767,9 +759,10 @@ void bvlc_send_result( return; } -int bvlc_send_bdt(struct sockaddr_in *dest) +int bvlc_send_bdt( + struct sockaddr_in *dest) { - uint8_t mtu[MAX_MPDU] = {0}; + uint8_t mtu[MAX_MPDU] = { 0 }; int mtu_len = 0; mtu_len = bvlc_encode_read_bdt_ack(&mtu[0], sizeof(mtu)); @@ -780,9 +773,10 @@ int bvlc_send_bdt(struct sockaddr_in *dest) return mtu_len; } -int bvlc_send_fdt(struct sockaddr_in *dest) +int bvlc_send_fdt( + struct sockaddr_in *dest) { - uint8_t mtu[MAX_MPDU] = {0}; + uint8_t mtu[MAX_MPDU] = { 0 }; int mtu_len = 0; mtu_len = bvlc_encode_read_fdt_ack(&mtu[0], sizeof(mtu)); @@ -794,8 +788,8 @@ int bvlc_send_fdt(struct sockaddr_in *dest) } bool bvlc_broadcast_address_same( - struct sockaddr_in *sin) /* network order address */ -{ + struct sockaddr_in * sin) +{ /* network order address */ bool same = false; if ((sin->sin_addr.s_addr == htonl(bip_get_broadcast_addr())) && @@ -809,12 +803,12 @@ bool bvlc_broadcast_address_same( /* returns: Number of bytes received, or 0 if none or timeout. */ uint16_t bvlc_receive( - BACNET_ADDRESS * src, /* returns the source address */ - uint8_t * npdu, /* returns the NPDU */ - uint16_t max_npdu, /* amount of space available in the NPDU */ - unsigned timeout) /* number of milliseconds to wait for a packet */ -{ - uint16_t npdu_len = 0; /* return value */ + BACNET_ADDRESS * src, /* returns the source address */ + uint8_t * npdu, /* returns the NPDU */ + uint16_t max_npdu, /* amount of space available in the NPDU */ + unsigned timeout) +{ /* number of milliseconds to wait for a packet */ + uint16_t npdu_len = 0; /* return value */ fd_set read_fds; int max = 0; struct timeval select_timeout; @@ -824,7 +818,7 @@ uint16_t bvlc_receive( int function_type = 0; int received_bytes = 0; uint16_t result_code = 0; - unsigned i =0; + unsigned i = 0; bool status = false; uint16_t time_to_live = 0; @@ -839,7 +833,7 @@ uint16_t bvlc_receive( if (timeout >= 1000) { select_timeout.tv_sec = timeout / 1000; select_timeout.tv_usec = - 1000 * (timeout - select_timeout.tv_sec * 1000); + 1000 * (timeout - select_timeout.tv_sec * 1000); } else { select_timeout.tv_sec = 0; select_timeout.tv_usec = 1000 * timeout; @@ -849,11 +843,9 @@ uint16_t bvlc_receive( max = bip_socket(); /* see if there is a packet for us */ if (select(max + 1, &read_fds, NULL, NULL, &select_timeout) > 0) { - received_bytes = recvfrom( - bip_socket(), + received_bytes = recvfrom(bip_socket(), (char *) &npdu[0], - max_npdu, 0, - (struct sockaddr *) &sin, &sin_len); + max_npdu, 0, (struct sockaddr *) &sin, &sin_len); } else { return 0; } @@ -877,40 +869,41 @@ uint16_t bvlc_receive( switch (function_type) { case BVLC_RESULT: /* Upon receipt of a BVLC-Result message containing a result code - of X'0000' indicating the successful completion of the - registration, a foreign device shall start a timer with a value - equal to the Time-to-Live parameter of the preceding Register- - Foreign-Device message. At the expiration of the timer, the - foreign device shall re-register with the BBMD by sending a BVLL - Register-Foreign-Device message */ + of X'0000' indicating the successful completion of the + registration, a foreign device shall start a timer with a value + equal to the Time-to-Live parameter of the preceding Register- + Foreign-Device message. At the expiration of the timer, the + foreign device shall re-register with the BBMD by sending a BVLL + Register-Foreign-Device message */ /* FIXME: clients may need this result */ (void) decode_unsigned16(&npdu[4], &result_code); BVLC_Result_Code = result_code; break; case BVLC_WRITE_BROADCAST_DISTRIBUTION_TABLE: /* Upon receipt of a BVLL Write-Broadcast-Distribution-Table - message, a BBMD shall attempt to create or replace its BDT, - depending on whether or not a BDT has previously existed. - If the creation or replacement of the BDT is successful, the BBMD - shall return a BVLC-Result message to the originating device with - a result code of X'0000'. Otherwise, the BBMD shall return a - BVLC-Result message to the originating device with a result code - of X'0010' indicating that the write attempt has failed. */ - status = bvlc_create_bdt(&npdu[4],npdu_len); + message, a BBMD shall attempt to create or replace its BDT, + depending on whether or not a BDT has previously existed. + If the creation or replacement of the BDT is successful, the BBMD + shall return a BVLC-Result message to the originating device with + a result code of X'0000'. Otherwise, the BBMD shall return a + BVLC-Result message to the originating device with a result code + of X'0010' indicating that the write attempt has failed. */ + status = bvlc_create_bdt(&npdu[4], npdu_len); if (status) { bvlc_send_result(&sin, BVLC_RESULT_SUCCESSFUL_COMPLETION); } else { - bvlc_send_result(&sin, BVLC_RESULT_WRITE_BROADCAST_DISTRIBUTION_TABLE_NAK); + bvlc_send_result(&sin, + BVLC_RESULT_WRITE_BROADCAST_DISTRIBUTION_TABLE_NAK); } break; case BVLC_READ_BROADCAST_DISTRIBUTION_TABLE: /* Upon receipt of a BVLL Read-Broadcast-Distribution-Table - message, a BBMD shall load the contents of its BDT into a BVLL - Read-Broadcast-Distribution-Table-Ack message and send it to the - originating device. If the BBMD is unable to perform the - read of its BDT, it shall return a BVLC-Result message to the - originating device with a result code of X'0020' indicating that - the read attempt has failed.*/ + message, a BBMD shall load the contents of its BDT into a BVLL + Read-Broadcast-Distribution-Table-Ack message and send it to the + originating device. If the BBMD is unable to perform the + read of its BDT, it shall return a BVLC-Result message to the + originating device with a result code of X'0020' indicating that + the read attempt has failed. */ if (bvlc_send_bdt(&sin) <= 0) { bvlc_send_result(&sin, BVLC_RESULT_READ_BROADCAST_DISTRIBUTION_TABLE_NAK); @@ -921,23 +914,23 @@ uint16_t bvlc_receive( break; case BVLC_FORWARDED_NPDU: /* Upon receipt of a BVLL Forwarded-NPDU message, a BBMD shall - process it according to whether it was received from a peer - BBMD as the result of a directed broadcast or a unicast - transmission. A BBMD may ascertain the method by which Forwarded- - NPDU messages will arrive by inspecting the broadcast distribution - mask field in its own BDT entry since all BDTs are required - to be identical. If the message arrived via directed broadcast, - it was also received by the other devices on the BBMD's subnet. In - this case the BBMD merely retransmits the message directly to each - foreign device currently in the BBMD's FDT. If the - message arrived via a unicast transmission it has not yet been - received by the other devices on the BBMD's subnet. In this case, - the message is sent to the devices on the BBMD's subnet using the - B/IP broadcast address as well as to each foreign device - currently in the BBMD's FDT. A BBMD on a subnet with no other - BACnet devices may omit the broadcast using the B/IP - broadcast address. The method by which a BBMD determines whether - or not other BACnet devices are present is a local matter. */ + process it according to whether it was received from a peer + BBMD as the result of a directed broadcast or a unicast + transmission. A BBMD may ascertain the method by which Forwarded- + NPDU messages will arrive by inspecting the broadcast distribution + mask field in its own BDT entry since all BDTs are required + to be identical. If the message arrived via directed broadcast, + it was also received by the other devices on the BBMD's subnet. In + this case the BBMD merely retransmits the message directly to each + foreign device currently in the BBMD's FDT. If the + message arrived via a unicast transmission it has not yet been + received by the other devices on the BBMD's subnet. In this case, + the message is sent to the devices on the BBMD's subnet using the + B/IP broadcast address as well as to each foreign device + currently in the BBMD's FDT. A BBMD on a subnet with no other + BACnet devices may omit the broadcast using the B/IP + broadcast address. The method by which a BBMD determines whether + or not other BACnet devices are present is a local matter. */ /* if this was received via Broadcast, don't broadcast it */ /* FIXME: how do I know if I received a unicast or broadcast? */ @@ -950,18 +943,17 @@ uint16_t bvlc_receive( break; case BVLC_REGISTER_FOREIGN_DEVICE: /* Upon receipt of a BVLL Register-Foreign-Device message, a BBMD - shall start a timer with a value equal to the Time-to-Live - parameter supplied plus a fixed grace period of 30 seconds. If, - within the period during which the timer is active, another BVLL - Register-Foreign-Device message from the same device is received, - the timer shall be reset and restarted. If the time expires - without the receipt of another BVLL Register-Foreign-Device - message from the same foreign device, the FDT entry for this - device shall be cleared.*/ + shall start a timer with a value equal to the Time-to-Live + parameter supplied plus a fixed grace period of 30 seconds. If, + within the period during which the timer is active, another BVLL + Register-Foreign-Device message from the same device is received, + the timer shall be reset and restarted. If the time expires + without the receipt of another BVLL Register-Foreign-Device + message from the same foreign device, the FDT entry for this + device shall be cleared. */ (void) decode_unsigned16(&npdu[4], &time_to_live); if (bvlc_register_foreign_device(&sin, time_to_live)) { - bvlc_send_result(&sin, - BVLC_RESULT_SUCCESSFUL_COMPLETION); + bvlc_send_result(&sin, BVLC_RESULT_SUCCESSFUL_COMPLETION); } else { bvlc_send_result(&sin, BVLC_RESULT_REGISTER_FOREIGN_DEVICE_NAK); @@ -969,12 +961,12 @@ uint16_t bvlc_receive( break; case BVLC_READ_FOREIGN_DEVICE_TABLE: /* Upon receipt of a BVLL Read-Foreign-Device-Table message, a - BBMD shall load the contents of its FDT into a BVLL Read- - Foreign-Device-Table-Ack message and send it to the originating - device. If the BBMD is unable to perform the read of its FDT, - it shall return a BVLC-Result message to the originating device - with a result code of X'0040' indicating that the read attempt has - failed.*/ + BBMD shall load the contents of its FDT into a BVLL Read- + Foreign-Device-Table-Ack message and send it to the originating + device. If the BBMD is unable to perform the read of its FDT, + it shall return a BVLC-Result message to the originating device + with a result code of X'0040' indicating that the read attempt has + failed. */ if (bvlc_send_fdt(&sin) <= 0) { bvlc_send_result(&sin, BVLC_RESULT_READ_FOREIGN_DEVICE_TABLE_NAK); @@ -985,16 +977,15 @@ uint16_t bvlc_receive( break; case BVLC_DELETE_FOREIGN_DEVICE_TABLE_ENTRY: /* Upon receipt of a BVLL Delete-Foreign-Device-Table-Entry - message, a BBMD shall search its foreign device table for an entry - corresponding to the B/IP address supplied in the message. If an - entry is found, it shall be deleted and the BBMD shall return a - BVLC-Result message to the originating device with a result code - of X'0000'. Otherwise, the BBMD shall return a BVLCResult - message to the originating device with a result code of X'0050' - indicating that the deletion attempt has failed.*/ + message, a BBMD shall search its foreign device table for an entry + corresponding to the B/IP address supplied in the message. If an + entry is found, it shall be deleted and the BBMD shall return a + BVLC-Result message to the originating device with a result code + of X'0000'. Otherwise, the BBMD shall return a BVLCResult + message to the originating device with a result code of X'0050' + indicating that the deletion attempt has failed. */ if (bvlc_delete_foreign_device(&npdu[4])) { - bvlc_send_result(&sin, - BVLC_RESULT_SUCCESSFUL_COMPLETION); + bvlc_send_result(&sin, BVLC_RESULT_SUCCESSFUL_COMPLETION); } else { bvlc_send_result(&sin, BVLC_RESULT_DELETE_FOREIGN_DEVICE_TABLE_ENTRY_NAK); @@ -1002,17 +993,17 @@ uint16_t bvlc_receive( break; case BVLC_DISTRIBUTE_BROADCAST_TO_NETWORK: /* Upon receipt of a BVLL Distribute-Broadcast-To-Network message - from a foreign device, the receiving BBMD shall transmit a - BVLL Forwarded-NPDU message on its local IP subnet using the - local B/IP broadcast address as the destination address. In - addition, a Forwarded-NPDU message shall be sent to each entry - in its BDT as described above in the case of the receipt of a - BVLL Original-Broadcast-NPDU as well as directly to each foreign - device currently in the BBMD's FDT except the originating - node. If the BBMD is unable to perform the forwarding function, - it shall return a BVLC-Result message to the foreign device - with a result code of X'0060' indicating that the forwarding - attempt was unsuccessful*/ + from a foreign device, the receiving BBMD shall transmit a + BVLL Forwarded-NPDU message on its local IP subnet using the + local B/IP broadcast address as the destination address. In + addition, a Forwarded-NPDU message shall be sent to each entry + in its BDT as described above in the case of the receipt of a + BVLL Original-Broadcast-NPDU as well as directly to each foreign + device currently in the BBMD's FDT except the originating + node. If the BBMD is unable to perform the forwarding function, + it shall return a BVLC-Result message to the foreign device + with a result code of X'0060' indicating that the forwarding + attempt was unsuccessful */ /* FIXME: complete the function code */ bvlc_broadcast_forward_npdu(&sin, &npdu[4], npdu_len); bvlc_fdt_forward_npdu(&sin, &npdu[4], npdu_len); @@ -1027,7 +1018,7 @@ uint16_t bvlc_receive( if (npdu_len < max_npdu) { /* shift the buffer to return a valid PDU */ for (i = 0; i < npdu_len; i++) { - npdu[i] = npdu[i+4]; + npdu[i] = npdu[i + 4]; } } else { /* ignore packets that are too large */ @@ -1054,7 +1045,7 @@ uint16_t bvlc_receive( if (npdu_len < max_npdu) { /* shift the buffer to return a valid PDU */ for (i = 0; i < npdu_len; i++) { - npdu[i] = npdu[i+4]; + npdu[i] = npdu[i + 4]; } /* if BDT or FDT entries exist, Forward the NPDU */ bvlc_bdt_forward_npdu(&sin, &npdu[0], npdu_len); @@ -1073,11 +1064,12 @@ uint16_t bvlc_receive( /* function to send a packet out the BACnet/IP socket (Annex J) */ /* returns number of bytes sent on success, negative number on failure */ -int bvlc_send_pdu(BACNET_ADDRESS * dest, /* destination address */ +int bvlc_send_pdu( + BACNET_ADDRESS * dest, /* destination address */ BACNET_NPDU_DATA * npdu_data, /* network information */ - uint8_t * pdu, /* any data to be sent - may be null */ + uint8_t * pdu, /* any data to be sent - may be null */ unsigned pdu_len) -{ /* number of bytes of data */ +{ /* number of bytes of data */ struct sockaddr_in bvlc_dest; uint8_t mtu[MAX_MPDU] = { 0 }; int mtu_len = 0; @@ -1129,7 +1121,8 @@ int bvlc_send_pdu(BACNET_ADDRESS * dest, /* destination address */ #include #include "ctest.h" -void testBIPAddress(Test * pTest) +void testBIPAddress( + Test * pTest) { uint8_t apdu[50] = { 0 }; uint32_t value = 0, test_value = 0; @@ -1139,16 +1132,15 @@ void testBIPAddress(Test * pTest) uint16_t port = 0, test_port = 0; address.s_addr = 42; - len = bvlc_encode_bip_address(&apdu[0], - &address, port); - test_len = bvlc_decode_bip_address(&apdu[0], - &test_address, &test_port); + len = bvlc_encode_bip_address(&apdu[0], &address, port); + test_len = bvlc_decode_bip_address(&apdu[0], &test_address, &test_port); ct_test(pTest, len == test_len); ct_test(pTest, address.s_addr == test_address.s_addr); ct_test(pTest, port == test_port); } -void testInternetAddress(Test * pTest) +void testInternetAddress( + Test * pTest) { BACNET_ADDRESS src; BACNET_ADDRESS test_src; @@ -1164,9 +1156,10 @@ void testInternetAddress(Test * pTest) } #ifdef TEST_BVLC -int main(void) +int main( + void) { - Test * pTest; + Test *pTest; bool rc; pTest = ct_create("BACnet Virtual Link Control", NULL); @@ -1184,5 +1177,5 @@ int main(void) return 0; } -#endif /* TEST_BBMD */ -#endif /* TEST */ +#endif /* TEST_BBMD */ +#endif /* TEST */ diff --git a/bacnet-stack/src/cov.c b/bacnet-stack/src/cov.c index 82a1829a..a567492f 100644 --- a/bacnet-stack/src/cov.c +++ b/bacnet-stack/src/cov.c @@ -49,10 +49,12 @@ COV Subscribe Property COV Notification Unconfirmed COV Notification */ -static int notify_encode_adpu(uint8_t * apdu, BACNET_COV_DATA * data) +static int notify_encode_adpu( + uint8_t * apdu, + BACNET_COV_DATA * data) { - int len = 0; /* length of each encoding */ - int apdu_len = 0; /* total length of the apdu, return value */ + int len = 0; /* length of each encoding */ + int apdu_len = 0; /* total length of the apdu, return value */ BACNET_PROPERTY_VALUE *value = NULL; /* value in list */ if (apdu) { @@ -71,8 +73,7 @@ static int notify_encode_adpu(uint8_t * apdu, BACNET_COV_DATA * data) data->monitoredObjectIdentifier.instance); apdu_len += len; /* tag 3 - timeRemaining */ - len = encode_context_unsigned(&apdu[apdu_len], - 3, data->timeRemaining); + len = encode_context_unsigned(&apdu[apdu_len], 3, data->timeRemaining); apdu_len += len; /* tag 4 - listOfValues */ len = encode_opening_tag(&apdu[apdu_len], 4); @@ -119,11 +120,13 @@ static int notify_encode_adpu(uint8_t * apdu, BACNET_COV_DATA * data) return apdu_len; } -int ccov_notify_encode_apdu(uint8_t * apdu, - uint8_t invoke_id, BACNET_COV_DATA * data) +int ccov_notify_encode_apdu( + uint8_t * apdu, + uint8_t invoke_id, + BACNET_COV_DATA * data) { - int len = 0; /* length of each encoding */ - int apdu_len = 0; /* total length of the apdu, return value */ + int len = 0; /* length of each encoding */ + int apdu_len = 0; /* total length of the apdu, return value */ uint16_t max_apdu = Device_Max_APDU_Length_Accepted(); if (apdu) { @@ -139,10 +142,12 @@ int ccov_notify_encode_apdu(uint8_t * apdu, return apdu_len; } -int ucov_notify_encode_apdu(uint8_t * apdu, BACNET_COV_DATA * data) +int ucov_notify_encode_apdu( + uint8_t * apdu, + BACNET_COV_DATA * data) { - int len = 0; /* length of each encoding */ - int apdu_len = 0; /* total length of the apdu, return value */ + int len = 0; /* length of each encoding */ + int apdu_len = 0; /* total length of the apdu, return value */ if (apdu && data) { apdu[0] = PDU_TYPE_UNCONFIRMED_SERVICE_REQUEST; @@ -157,15 +162,17 @@ int ucov_notify_encode_apdu(uint8_t * apdu, BACNET_COV_DATA * data) /* decode the service request only */ /* COV and Unconfirmed COV are the same */ -int cov_notify_decode_service_request(uint8_t * apdu, - unsigned apdu_len, BACNET_COV_DATA * data) +int cov_notify_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + BACNET_COV_DATA * data) { - int len = 0; /* return value */ + int len = 0; /* return value */ uint8_t tag_number = 0; uint32_t len_value = 0; uint32_t decoded_value = 0; /* for decoding */ int decoded_type = 0; /* for decoding */ - int property = 0; /* for decoding */ + int property = 0; /* for decoding */ BACNET_PROPERTY_VALUE *value = NULL; /* value in list */ if (apdu_len && data) { @@ -232,8 +239,7 @@ int cov_notify_decode_service_request(uint8_t * apdu, len += decode_tag_number_and_value(&apdu[len], &tag_number, &len_value); - len += - decode_unsigned(&apdu[len], len_value, &decoded_value); + len += decode_unsigned(&apdu[len], len_value, &decoded_value); value->propertyArrayIndex = decoded_value; } else value->propertyArrayIndex = BACNET_ARRAY_ALL; @@ -255,8 +261,7 @@ int cov_notify_decode_service_request(uint8_t * apdu, len += decode_tag_number_and_value(&apdu[len], &tag_number, &len_value); - len += - decode_unsigned(&apdu[len], len_value, &decoded_value); + len += decode_unsigned(&apdu[len], len_value, &decoded_value); value->priority = decoded_value; } else value->priority = BACNET_NO_PRIORITY; @@ -292,11 +297,13 @@ SubscribeCOV-Request ::= SEQUENCE { } */ -int cov_subscribe_encode_adpu(uint8_t * apdu, - uint8_t invoke_id, BACNET_SUBSCRIBE_COV_DATA * data) +int cov_subscribe_encode_adpu( + uint8_t * apdu, + uint8_t invoke_id, + BACNET_SUBSCRIBE_COV_DATA * data) { - int len = 0; /* length of each encoding */ - int apdu_len = 0; /* total length of the apdu, return value */ + int len = 0; /* length of each encoding */ + int apdu_len = 0; /* total length of the apdu, return value */ uint16_t max_apdu = Device_Max_APDU_Length_Accepted(); if (apdu && data) { @@ -326,8 +333,7 @@ int cov_subscribe_encode_adpu(uint8_t * apdu, 2, data->issueConfirmedNotifications); apdu_len += len; /* tag 3 - lifetime */ - len = encode_context_unsigned(&apdu[apdu_len], - 3, data->lifetime); + len = encode_context_unsigned(&apdu[apdu_len], 3, data->lifetime); apdu_len += len; } } @@ -336,10 +342,12 @@ int cov_subscribe_encode_adpu(uint8_t * apdu, } /* decode the service request only */ -int cov_subscribe_decode_service_request(uint8_t * apdu, - unsigned apdu_len, BACNET_SUBSCRIBE_COV_DATA * data) +int cov_subscribe_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + BACNET_SUBSCRIBE_COV_DATA * data) { - int len = 0; /* return value */ + int len = 0; /* return value */ uint8_t tag_number = 0; uint32_t len_value = 0; uint32_t decoded_value = 0; /* for decoding */ @@ -367,7 +375,7 @@ int cov_subscribe_decode_service_request(uint8_t * apdu, } else return -1; /* optional parameters - if missing, means cancellation */ - if ((unsigned)len < apdu_len) { + if ((unsigned) len < apdu_len) { /* tag 2 - issueConfirmedNotifications - optional */ if (decode_is_context_tag(&apdu[len], 2)) { data->cancellationRequest = false; @@ -417,11 +425,13 @@ BACnetPropertyReference ::= SEQUENCE { */ -int cov_subscribe_property_encode_adpu(uint8_t * apdu, - uint8_t invoke_id, BACNET_SUBSCRIBE_COV_DATA * data) +int cov_subscribe_property_encode_adpu( + uint8_t * apdu, + uint8_t invoke_id, + BACNET_SUBSCRIBE_COV_DATA * data) { - int len = 0; /* length of each encoding */ - int apdu_len = 0; /* total length of the apdu, return value */ + int len = 0; /* length of each encoding */ + int apdu_len = 0; /* total length of the apdu, return value */ uint16_t max_apdu = Device_Max_APDU_Length_Accepted(); if (apdu && data) { @@ -446,8 +456,7 @@ int cov_subscribe_property_encode_adpu(uint8_t * apdu, 2, data->issueConfirmedNotifications); apdu_len += len; /* tag 3 - lifetime */ - len = encode_context_unsigned(&apdu[apdu_len], - 3, data->lifetime); + len = encode_context_unsigned(&apdu[apdu_len], 3, data->lifetime); apdu_len += len; } /* tag 4 - monitoredPropertyIdentifier */ @@ -467,8 +476,7 @@ int cov_subscribe_property_encode_adpu(uint8_t * apdu, /* tag 5 - covIncrement */ if (data->covIncrementPresent) { - len = encode_context_real(&apdu[apdu_len], - 5, data->covIncrement); + len = encode_context_real(&apdu[apdu_len], 5, data->covIncrement); apdu_len += len; } } @@ -477,15 +485,17 @@ int cov_subscribe_property_encode_adpu(uint8_t * apdu, } /* decode the service request only */ -int cov_subscribe_property_decode_service_request(uint8_t * apdu, - unsigned apdu_len, BACNET_SUBSCRIBE_COV_DATA * data) +int cov_subscribe_property_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + BACNET_SUBSCRIBE_COV_DATA * data) { - int len = 0; /* return value */ + int len = 0; /* return value */ uint8_t tag_number = 0; uint32_t len_value = 0; uint32_t decoded_value = 0; /* for decoding */ int decoded_type = 0; /* for decoding */ - int property = 0; /* for decoding */ + int property = 0; /* for decoding */ if (apdu_len && data) { /* tag 0 - subscriberProcessIdentifier */ @@ -571,7 +581,9 @@ int cov_subscribe_property_decode_service_request(uint8_t * apdu, return len; } -int ucov_notify_send(uint8_t * buffer, BACNET_COV_DATA * data) +int ucov_notify_send( + uint8_t * buffer, + BACNET_COV_DATA * data) { int len = 0; int pdu_len = 0; @@ -599,8 +611,11 @@ int ucov_notify_send(uint8_t * buffer, BACNET_COV_DATA * data) #include "ctest.h" #include "bacapp.h" -int ccov_notify_decode_apdu(uint8_t * apdu, - unsigned apdu_len, uint8_t * invoke_id, BACNET_COV_DATA * data) +int ccov_notify_decode_apdu( + uint8_t * apdu, + unsigned apdu_len, + uint8_t * invoke_id, + BACNET_COV_DATA * data) { int len = 0; unsigned offset = 0; @@ -626,8 +641,10 @@ int ccov_notify_decode_apdu(uint8_t * apdu, return len; } -int ucov_notify_decode_apdu(uint8_t * apdu, - unsigned apdu_len, BACNET_COV_DATA * data) +int ucov_notify_decode_apdu( + uint8_t * apdu, + unsigned apdu_len, + BACNET_COV_DATA * data) { int len = 0; unsigned offset = 0; @@ -650,8 +667,10 @@ int ucov_notify_decode_apdu(uint8_t * apdu, return len; } -int cov_subscribe_decode_apdu(uint8_t * apdu, - unsigned apdu_len, uint8_t * invoke_id, +int cov_subscribe_decode_apdu( + uint8_t * apdu, + unsigned apdu_len, + uint8_t * invoke_id, BACNET_SUBSCRIBE_COV_DATA * data) { int len = 0; @@ -678,8 +697,10 @@ int cov_subscribe_decode_apdu(uint8_t * apdu, return len; } -int cov_subscribe_property_decode_apdu(uint8_t * apdu, - unsigned apdu_len, uint8_t * invoke_id, +int cov_subscribe_property_decode_apdu( + uint8_t * apdu, + unsigned apdu_len, + uint8_t * invoke_id, BACNET_SUBSCRIBE_COV_DATA * data) { int len = 0; @@ -707,22 +728,29 @@ int cov_subscribe_property_decode_apdu(uint8_t * apdu, } /* dummy function stubs */ -int npdu_encode_pdu(uint8_t * npdu, +int npdu_encode_pdu( + uint8_t * npdu, BACNET_ADDRESS * dest, - BACNET_ADDRESS * src, BACNET_NPDU_DATA * npdu_data) + BACNET_ADDRESS * src, + BACNET_NPDU_DATA * npdu_data) { return 0; } -void npdu_encode_npdu_data(BACNET_NPDU_DATA * npdu, - bool data_expecting_reply, BACNET_MESSAGE_PRIORITY priority) +void npdu_encode_npdu_data( + BACNET_NPDU_DATA * npdu, + bool data_expecting_reply, + BACNET_MESSAGE_PRIORITY priority) { } /* dummy function stubs */ -int datalink_send_pdu(BACNET_ADDRESS * dest, - BACNET_NPDU_DATA * npdu_data, uint8_t * pdu, unsigned pdu_len) +int datalink_send_pdu( + BACNET_ADDRESS * dest, + BACNET_NPDU_DATA * npdu_data, + uint8_t * pdu, + unsigned pdu_len) { (void) dest; (void) npdu_data; @@ -733,19 +761,23 @@ int datalink_send_pdu(BACNET_ADDRESS * dest, } /* dummy function stubs */ -void datalink_get_broadcast_address(BACNET_ADDRESS * dest) +void datalink_get_broadcast_address( + BACNET_ADDRESS * dest) { (void) dest; } /* dummy function stubs */ -uint16_t Device_Max_APDU_Length_Accepted(void) +uint16_t Device_Max_APDU_Length_Accepted( + void) { return MAX_APDU; } -void testCOVNotifyData(Test * pTest, - BACNET_COV_DATA * data, BACNET_COV_DATA * test_data) +void testCOVNotifyData( + Test * pTest, + BACNET_COV_DATA * data, + BACNET_COV_DATA * test_data) { ct_test(pTest, test_data->subscriberProcessIdentifier == @@ -763,7 +795,9 @@ void testCOVNotifyData(Test * pTest, /* FIXME: test the listOfValues in some clever manner */ } -void testUCOVNotifyData(Test * pTest, BACNET_COV_DATA * data) +void testUCOVNotifyData( + Test * pTest, + BACNET_COV_DATA * data) { uint8_t apdu[480] = { 0 }; int len = 0; @@ -780,7 +814,9 @@ void testUCOVNotifyData(Test * pTest, BACNET_COV_DATA * data) testCOVNotifyData(pTest, data, &test_data); } -void testCCOVNotifyData(Test * pTest, uint8_t invoke_id, +void testCCOVNotifyData( + Test * pTest, + uint8_t invoke_id, BACNET_COV_DATA * data) { uint8_t apdu[480] = { 0 }; @@ -801,7 +837,8 @@ void testCCOVNotifyData(Test * pTest, uint8_t invoke_id, testCOVNotifyData(pTest, data, &test_data); } -void testCOVNotify(Test * pTest) +void testCOVNotify( + Test * pTest) { uint8_t invoke_id = 12; BACNET_COV_DATA data; @@ -826,7 +863,8 @@ void testCOVNotify(Test * pTest) /* FIXME: add more values to the list of values */ } -void testCOVSubscribeData(Test * pTest, +void testCOVSubscribeData( + Test * pTest, BACNET_SUBSCRIBE_COV_DATA * data, BACNET_SUBSCRIBE_COV_DATA * test_data) { @@ -852,7 +890,8 @@ void testCOVSubscribeData(Test * pTest, } } -void testCOVSubscribePropertyData(Test * pTest, +void testCOVSubscribePropertyData( + Test * pTest, BACNET_SUBSCRIBE_COV_DATA * data, BACNET_SUBSCRIBE_COV_DATA * test_data) { @@ -870,7 +909,9 @@ void testCOVSubscribePropertyData(Test * pTest, } } -void testCOVSubscribeEncoding(Test * pTest, uint8_t invoke_id, +void testCOVSubscribeEncoding( + Test * pTest, + uint8_t invoke_id, BACNET_SUBSCRIBE_COV_DATA * data) { uint8_t apdu[480] = { 0 }; @@ -890,7 +931,9 @@ void testCOVSubscribeEncoding(Test * pTest, uint8_t invoke_id, testCOVSubscribeData(pTest, data, &test_data); } -void testCOVSubscribePropertyEncoding(Test * pTest, uint8_t invoke_id, +void testCOVSubscribePropertyEncoding( + Test * pTest, + uint8_t invoke_id, BACNET_SUBSCRIBE_COV_DATA * data) { uint8_t apdu[480] = { 0 }; @@ -910,7 +953,8 @@ void testCOVSubscribePropertyEncoding(Test * pTest, uint8_t invoke_id, testCOVSubscribePropertyData(pTest, data, &test_data); } -void testCOVSubscribe(Test * pTest) +void testCOVSubscribe( + Test * pTest) { uint8_t invoke_id = 12; BACNET_SUBSCRIBE_COV_DATA data; @@ -927,7 +971,8 @@ void testCOVSubscribe(Test * pTest) testCOVSubscribeEncoding(pTest, invoke_id, &data); } -void testCOVSubscribeProperty(Test * pTest) +void testCOVSubscribeProperty( + Test * pTest) { uint8_t invoke_id = 12; BACNET_SUBSCRIBE_COV_DATA data; @@ -954,7 +999,9 @@ void testCOVSubscribeProperty(Test * pTest) } #ifdef TEST_COV -int main(int argc, char *argv[]) +int main( + int argc, + char *argv[]) { Test *pTest; bool rc; @@ -975,5 +1022,5 @@ int main(int argc, char *argv[]) return 0; } -#endif /* TEST_COV */ -#endif /* TEST */ +#endif /* TEST_COV */ +#endif /* TEST */ diff --git a/bacnet-stack/src/crc.c b/bacnet-stack/src/crc.c index 4ee72a07..39d9a778 100644 --- a/bacnet-stack/src/crc.c +++ b/bacnet-stack/src/crc.c @@ -38,8 +38,7 @@ #if defined(CRC_USE_TABLE) /* note: table is created using unit test below */ -static const uint8_t HeaderCRC[256] = -{ +static const uint8_t HeaderCRC[256] = { 0x00, 0xfe, 0xff, 0x01, 0xfd, 0x03, 0x02, 0xfc, 0xf9, 0x07, 0x06, 0xf8, 0x04, 0xfa, 0xfb, 0x05, 0xf1, 0x0f, 0x0e, 0xf0, 0x0c, 0xf2, 0xf3, 0x0d, @@ -74,14 +73,15 @@ static const uint8_t HeaderCRC[256] = 0xa9, 0x57, 0x56, 0xa8, 0x54, 0xaa, 0xab, 0x55 }; -uint8_t CRC_Calc_Header(uint8_t dataValue, uint8_t crcValue) +uint8_t CRC_Calc_Header( + uint8_t dataValue, + uint8_t crcValue) { return HeaderCRC[crcValue ^ dataValue]; } /* note: table is created using unit test below */ -static const uint16_t DataCRC[256] = -{ +static const uint16_t DataCRC[256] = { 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, @@ -116,7 +116,9 @@ static const uint16_t DataCRC[256] = 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 }; -uint16_t CRC_Calc_Data(uint8_t dataValue, uint16_t crcValue) +uint16_t CRC_Calc_Data( + uint8_t dataValue, + uint16_t crcValue) { return ((crcValue >> 8) ^ DataCRC[(crcValue & 0x00FF) ^ dataValue]); @@ -127,7 +129,9 @@ uint16_t CRC_Calc_Data(uint8_t dataValue, uint16_t crcValue) /* */ /* The ^ operator means exclusive OR. */ /* Note: This function is copied directly from the BACnet standard. */ -uint8_t CRC_Calc_Header(uint8_t dataValue, uint8_t crcValue) +uint8_t CRC_Calc_Header( + uint8_t dataValue, + uint8_t crcValue) { uint16_t crc; @@ -147,7 +151,9 @@ uint8_t CRC_Calc_Header(uint8_t dataValue, uint8_t crcValue) /* */ /* The ^ operator means exclusive OR. */ /* Note: This function is copied directly from the BACnet standard. */ -uint16_t CRC_Calc_Data(uint8_t dataValue, uint16_t crcValue) +uint16_t CRC_Calc_Data( + uint8_t dataValue, + uint16_t crcValue) { uint16_t crcLow; @@ -167,10 +173,11 @@ uint16_t CRC_Calc_Data(uint8_t dataValue, uint16_t crcValue) #include "bytes.h" /* test from Annex G 1.0 of BACnet Standard */ -void testCRC8(Test * pTest) +void testCRC8( + Test * pTest) { - uint8_t crc = 0xff; /* accumulates the crc value */ - uint8_t frame_crc; /* appended to the end of the frame */ + uint8_t crc = 0xff; /* accumulates the crc value */ + uint8_t frame_crc; /* appended to the end of the frame */ crc = CRC_Calc_Header(0x00, crc); ct_test(pTest, crc == 0x55); @@ -192,7 +199,8 @@ void testCRC8(Test * pTest) } /* test from Annex G 2.0 of BACnet Standard */ -void testCRC16(Test * pTest) +void testCRC16( + Test * pTest) { uint16_t crc = 0xffff; uint16_t data_crc; @@ -213,19 +221,20 @@ void testCRC16(Test * pTest) ct_test(pTest, crc == 0xF0B8); } -void testCRC8CreateTable(Test *pTest) +void testCRC8CreateTable( + Test * pTest) { - uint8_t crc = 0xff; /* accumulates the crc value */ + uint8_t crc = 0xff; /* accumulates the crc value */ int i; - (void)pTest; + (void) pTest; printf("static const uint8_t HeaderCRC[256] =\n"); printf("{\n"); printf(" "); for (i = 0; i < 256; i++) { crc = CRC_Calc_Header(i, 0); printf("0x%02x, ", crc); - if (!((i+1)%8)) { + if (!((i + 1) % 8)) { printf("\n"); if (i != 255) { printf(" "); @@ -235,19 +244,20 @@ void testCRC8CreateTable(Test *pTest) printf("};\n"); } -void testCRC16CreateTable(Test *pTest) +void testCRC16CreateTable( + Test * pTest) { uint16_t crc; int i; - (void)pTest; + (void) pTest; printf("static const uint16_t DataCRC[256] =\n"); printf("{\n"); printf(" "); for (i = 0; i < 256; i++) { crc = CRC_Calc_Data(i, 0); printf("0x%04x, ", crc); - if (!((i+1)%8)) { + if (!((i + 1) % 8)) { printf("\n"); if (i != 255) { printf(" "); @@ -260,7 +270,8 @@ void testCRC16CreateTable(Test *pTest) #endif #ifdef TEST_CRC -int main(void) +int main( + void) { Test *pTest; bool rc; diff --git a/bacnet-stack/src/datalink.c b/bacnet-stack/src/datalink.c index 6fa505c0..66f95ce9 100644 --- a/bacnet-stack/src/datalink.c +++ b/bacnet-stack/src/datalink.c @@ -34,50 +34,61 @@ #include "datalink.h" /* Function pointers - point to your datalink */ -bool (*datalink_init)(char *ifname); +bool(*datalink_init) (char *ifname); -int (*datalink_send_pdu)(BACNET_ADDRESS * dest, - BACNET_NPDU_DATA * npdu_data, uint8_t * pdu, unsigned pdu_len); +int ( + *datalink_send_pdu) ( + BACNET_ADDRESS * dest, + BACNET_NPDU_DATA * npdu_data, + uint8_t * pdu, + unsigned pdu_len); -uint16_t (*datalink_receive)(BACNET_ADDRESS * src, +uint16_t(*datalink_receive) (BACNET_ADDRESS * src, uint8_t * pdu, uint16_t max_pdu, unsigned timeout); -void (*datalink_cleanup)(void); +void ( + *datalink_cleanup) ( + void); -void (*datalink_get_broadcast_address)(BACNET_ADDRESS * dest); +void ( + *datalink_get_broadcast_address) ( + BACNET_ADDRESS * dest); -void (*datalink_get_my_address)(BACNET_ADDRESS * my_address); +void ( + *datalink_get_my_address) ( + BACNET_ADDRESS * my_address); -void datalink_configure(void) +void datalink_configure( + void) { #if defined(BACDL_ETHERNET) - datalink_init = ethernet_init; - datalink_send_pdu = ethernet_send_pdu; - datalink_receive = ethernet_receive; - datalink_cleanup = ethernet_cleanup; - datalink_get_broadcast_address = ethernet_get_broadcast_address; - datalink_get_my_address = ethernet_get_my_address; + datalink_init = ethernet_init; + datalink_send_pdu = ethernet_send_pdu; + datalink_receive = ethernet_receive; + datalink_cleanup = ethernet_cleanup; + datalink_get_broadcast_address = ethernet_get_broadcast_address; + datalink_get_my_address = ethernet_get_my_address; #elif defined(BACDL_ARCNET) - datalink_init = arcnet_init; - datalink_send_pdu = arcnet_send_pdu; - datalink_receive = arcnet_receive; - datalink_cleanup = arcnet_cleanup; - datalink_get_broadcast_address = arcnet_get_broadcast_address; - datalink_get_my_address = arcnet_get_my_address; + datalink_init = arcnet_init; + datalink_send_pdu = arcnet_send_pdu; + datalink_receive = arcnet_receive; + datalink_cleanup = arcnet_cleanup; + datalink_get_broadcast_address = arcnet_get_broadcast_address; + datalink_get_my_address = arcnet_get_my_address; #elif defined(BACDL_MSTP) - datalink_init = dlmstp_init; - datalink_send_pdu = dlmstp_send_pdu; - datalink_receive = dlmstp_receive; - datalink_cleanup = dlmstp_cleanup; - datalink_get_broadcast_address = dlmstp_get_broadcast_address; - datalink_get_my_address = dlmstp_get_my_address; + datalink_init = dlmstp_init; + datalink_send_pdu = dlmstp_send_pdu; + datalink_receive = dlmstp_receive; + datalink_cleanup = dlmstp_cleanup; + datalink_get_broadcast_address = dlmstp_get_broadcast_address; + datalink_get_my_address = dlmstp_get_my_address; #elif defined(BACDL_BIP) - datalink_init = bip_init; - datalink_send_pdu = bip_send_pdu; - datalink_receive = bip_receive; - datalink_cleanup = bip_cleanup; - datalink_get_broadcast_address = bip_get_broadcast_address; - datalink_get_my_address = bip_get_my_address; + datalink_init = bip_init; + datalink_send_pdu = bip_send_pdu; + datalink_receive = bip_receive; + datalink_cleanup = bip_cleanup; + datalink_get_broadcast_address = bip_get_broadcast_address; + datalink_get_my_address = bip_get_my_address; #endif - } +} } diff --git a/bacnet-stack/src/datetime.c b/bacnet-stack/src/datetime.c index fdbf60a6..490b528e 100644 --- a/bacnet-stack/src/datetime.c +++ b/bacnet-stack/src/datetime.c @@ -52,7 +52,8 @@ time or date may be interpreted as "any" or "don't care" */ -static bool is_leap_year(uint16_t year) +static bool is_leap_year( + uint16_t year) { if ((year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0)) return (true); @@ -60,12 +61,13 @@ static bool is_leap_year(uint16_t year) return (false); } -static uint8_t month_days(uint16_t year, uint8_t month) +static uint8_t month_days( + uint16_t year, + uint8_t month) { /* note: start with a zero in the first element to save us from a month - 1 calculation in the lookup */ - int month_days[13] = - { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; + int month_days[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; /* February */ if ((month == 2) && is_leap_year(year)) @@ -76,12 +78,15 @@ static uint8_t month_days(uint16_t year, uint8_t month) return 0; } -static uint32_t days_since_epoch(uint16_t year, uint8_t month, uint8_t day) +static uint32_t days_since_epoch( + uint16_t year, + uint8_t month, + uint8_t day) { - uint32_t days = 0; /* return value */ - uint8_t monthdays; /* days in a month */ - uint16_t years = 0; /* loop counter for years */ - uint8_t months = 0; /* loop counter for months */ + uint32_t days = 0; /* return value */ + uint8_t monthdays; /* days in a month */ + uint16_t years = 0; /* loop counter for years */ + uint8_t months = 0; /* loop counter for months */ monthdays = month_days(year, month); if ((year >= 1900) && (monthdays) && (day >= 1) && (day <= monthdays)) { @@ -99,8 +104,11 @@ static uint32_t days_since_epoch(uint16_t year, uint8_t month, uint8_t day) return (days); } -static void days_since_epoch_into_ymd(uint32_t days, - uint16_t * pYear, uint8_t * pMonth, uint8_t * pDay) +static void days_since_epoch_into_ymd( + uint32_t days, + uint16_t * pYear, + uint8_t * pMonth, + uint8_t * pDay) { uint16_t year = 1900; uint8_t month = 1; @@ -135,7 +143,10 @@ static void days_since_epoch_into_ymd(uint32_t days, /* Jan 1, 1900 is a Monday */ /* wday 1=Monday...7=Sunday */ -static uint8_t day_of_week(uint16_t year, uint8_t month, uint8_t day) +static uint8_t day_of_week( + uint16_t year, + uint8_t month, + uint8_t day) { return ((uint8_t) (days_since_epoch(year, month, day) % 7) + 1); } @@ -143,7 +154,9 @@ static uint8_t day_of_week(uint16_t year, uint8_t month, uint8_t day) /* if the date1 is the same as date2, return is 0 if date1 is after date2, returns positive if date1 is before date2, returns negative */ -int datetime_compare_date(BACNET_DATE * date1, BACNET_DATE * date2) +int datetime_compare_date( + BACNET_DATE * date1, + BACNET_DATE * date2) { int diff = 0; @@ -163,7 +176,9 @@ int datetime_compare_date(BACNET_DATE * date1, BACNET_DATE * date2) /* if the time1 is the same as time2, return is 0 if time1 is after time2, returns positive if time1 is before time2, returns negative */ -int datetime_compare_time(BACNET_TIME * time1, BACNET_TIME * time2) +int datetime_compare_time( + BACNET_TIME * time1, + BACNET_TIME * time2) { int diff = 0; @@ -174,8 +189,7 @@ int datetime_compare_time(BACNET_TIME * time1, BACNET_TIME * time2) if (diff == 0) { diff = (int) time1->sec - (int) time2->sec; if (diff == 0) { - diff = - (int) time1->hundredths - (int) time2->hundredths; + diff = (int) time1->hundredths - (int) time2->hundredths; } } } @@ -187,7 +201,8 @@ int datetime_compare_time(BACNET_TIME * time1, BACNET_TIME * time2) /* if the datetime1 is the same as datetime2, return is 0 if datetime1 is before datetime2, returns negative if datetime1 is after datetime2, returns positive */ -int datetime_compare(BACNET_DATE_TIME * datetime1, +int datetime_compare( + BACNET_DATE_TIME * datetime1, BACNET_DATE_TIME * datetime2) { int diff = 0; @@ -200,7 +215,9 @@ int datetime_compare(BACNET_DATE_TIME * datetime1, return diff; } -void datetime_copy_date(BACNET_DATE * dest_date, BACNET_DATE * src_date) +void datetime_copy_date( + BACNET_DATE * dest_date, + BACNET_DATE * src_date) { if (dest_date && src_date) { dest_date->year = src_date->year; @@ -210,7 +227,9 @@ void datetime_copy_date(BACNET_DATE * dest_date, BACNET_DATE * src_date) } } -void datetime_copy_time(BACNET_TIME * dest_time, BACNET_TIME * src_time) +void datetime_copy_time( + BACNET_TIME * dest_time, + BACNET_TIME * src_time) { if (dest_time && src_time) { dest_time->hour = src_time->hour; @@ -220,15 +239,19 @@ void datetime_copy_time(BACNET_TIME * dest_time, BACNET_TIME * src_time) } } -void datetime_copy(BACNET_DATE_TIME * dest_datetime, +void datetime_copy( + BACNET_DATE_TIME * dest_datetime, BACNET_DATE_TIME * src_datetime) { datetime_copy_time(&dest_datetime->time, &src_datetime->time); datetime_copy_date(&dest_datetime->date, &src_datetime->date); } -void datetime_set_date(BACNET_DATE * bdate, - uint16_t year, uint8_t month, uint8_t day) +void datetime_set_date( + BACNET_DATE * bdate, + uint16_t year, + uint8_t month, + uint8_t day) { if (bdate) { bdate->year = year; @@ -238,8 +261,12 @@ void datetime_set_date(BACNET_DATE * bdate, } } -void datetime_set_time(BACNET_TIME * btime, - uint8_t hour, uint8_t minute, uint8_t seconds, uint8_t hundredths) +void datetime_set_time( + BACNET_TIME * btime, + uint8_t hour, + uint8_t minute, + uint8_t seconds, + uint8_t hundredths) { if (btime) { btime->hour = hour; @@ -249,8 +276,10 @@ void datetime_set_time(BACNET_TIME * btime, } } -void datetime_set(BACNET_DATE_TIME * bdatetime, - BACNET_DATE * bdate, BACNET_TIME * btime) +void datetime_set( + BACNET_DATE_TIME * bdatetime, + BACNET_DATE * bdate, + BACNET_TIME * btime) { if (bdate && btime && bdatetime) { bdatetime->time.hour = btime->hour; @@ -264,9 +293,15 @@ void datetime_set(BACNET_DATE_TIME * bdatetime, } } -void datetime_set_values(BACNET_DATE_TIME * bdatetime, - uint16_t year, uint8_t month, uint8_t day, - uint8_t hour, uint8_t minute, uint8_t seconds, uint8_t hundredths) +void datetime_set_values( + BACNET_DATE_TIME * bdatetime, + uint16_t year, + uint8_t month, + uint8_t day, + uint8_t hour, + uint8_t minute, + uint8_t seconds, + uint8_t hundredths) { if (bdatetime) { bdatetime->date.year = year; @@ -280,14 +315,19 @@ void datetime_set_values(BACNET_DATE_TIME * bdatetime, } } -static uint32_t seconds_since_midnight(uint8_t hours, uint8_t minutes, +static uint32_t seconds_since_midnight( + uint8_t hours, + uint8_t minutes, uint8_t seconds) { return ((hours * 60 * 60) + (minutes * 60) + seconds); } -static void seconds_since_midnight_into_hms(uint32_t seconds, - uint8_t * pHours, uint8_t * pMinutes, uint8_t * pSeconds) +static void seconds_since_midnight_into_hms( + uint32_t seconds, + uint8_t * pHours, + uint8_t * pMinutes, + uint8_t * pSeconds) { uint8_t hour = 0; uint8_t minute = 0; @@ -305,7 +345,9 @@ static void seconds_since_midnight_into_hms(uint32_t seconds, *pSeconds = (uint8_t) seconds; } -void datetime_add_minutes(BACNET_DATE_TIME * bdatetime, uint32_t minutes) +void datetime_add_minutes( + BACNET_DATE_TIME * bdatetime, + uint32_t minutes) { uint32_t bdatetime_minutes = 0; uint32_t bdatetime_days = 0; @@ -329,13 +371,13 @@ void datetime_add_minutes(BACNET_DATE_TIME * bdatetime, uint32_t minutes) seconds_since_midnight_into_hms(bdatetime_minutes * 60, &bdatetime->time.hour, &bdatetime->time.min, &bdatetime->time.sec); days_since_epoch_into_ymd(bdatetime_days, - &bdatetime->date.year, - &bdatetime->date.month, &bdatetime->date.day); + &bdatetime->date.year, &bdatetime->date.month, &bdatetime->date.day); bdatetime->date.wday = day_of_week(bdatetime->date.year, bdatetime->date.month, bdatetime->date.day); } -bool datetime_wildcard(BACNET_DATE_TIME * bdatetime) +bool datetime_wildcard( + BACNET_DATE_TIME * bdatetime) { bool wildcard_present = false; @@ -355,7 +397,8 @@ bool datetime_wildcard(BACNET_DATE_TIME * bdatetime) return wildcard_present; } -void datetime_date_wildcard_set(BACNET_DATE * bdate) +void datetime_date_wildcard_set( + BACNET_DATE * bdate) { if (bdate) { bdate->year = 1900 + 0xFF; @@ -365,7 +408,8 @@ void datetime_date_wildcard_set(BACNET_DATE * bdate) } } -void datetime_time_wildcard_set(BACNET_TIME * btime) +void datetime_time_wildcard_set( + BACNET_TIME * btime) { if (btime) { btime->hour = 0xFF; @@ -375,7 +419,8 @@ void datetime_time_wildcard_set(BACNET_TIME * btime) } } -void datetime_wildcard_set(BACNET_DATE_TIME * bdatetime) +void datetime_wildcard_set( + BACNET_DATE_TIME * bdatetime) { if (bdatetime) { datetime_date_wildcard_set(&bdatetime->date); @@ -389,7 +434,8 @@ void datetime_wildcard_set(BACNET_DATE_TIME * bdatetime) #include #include "ctest.h" -void testBACnetDateTimeWildcard(Test * pTest) +void testBACnetDateTimeWildcard( + Test * pTest) { BACNET_DATE_TIME bdatetime; bool status = false; @@ -403,7 +449,8 @@ void testBACnetDateTimeWildcard(Test * pTest) ct_test(pTest, status == true); } -void testBACnetDateTimeAdd(Test * pTest) +void testBACnetDateTimeAdd( + Test * pTest) { BACNET_DATE_TIME bdatetime, test_bdatetime; uint32_t minutes = 0; @@ -436,7 +483,8 @@ void testBACnetDateTimeAdd(Test * pTest) -void testBACnetDateTimeSeconds(Test * pTest) +void testBACnetDateTimeSeconds( + Test * pTest) { uint8_t hour = 0, minute = 0, second = 0; uint8_t test_hour = 0, test_minute = 0, test_second = 0; @@ -457,7 +505,8 @@ void testBACnetDateTimeSeconds(Test * pTest) } } -void testBACnetDate(Test * pTest) +void testBACnetDate( + Test * pTest) { BACNET_DATE bdate1, bdate2; int diff = 0; @@ -518,7 +567,8 @@ void testBACnetDate(Test * pTest) return; } -void testBACnetTime(Test * pTest) +void testBACnetTime( + Test * pTest) { BACNET_TIME btime1, btime2; int diff = 0; @@ -567,7 +617,8 @@ void testBACnetTime(Test * pTest) return; } -void testBACnetDateTime(Test * pTest) +void testBACnetDateTime( + Test * pTest) { BACNET_DATE_TIME bdatetime1, bdatetime2; BACNET_DATE bdate; @@ -634,7 +685,8 @@ void testBACnetDateTime(Test * pTest) return; } -void testDateEpoch(Test * pTest) +void testDateEpoch( + Test * pTest) { uint32_t days = 0; uint16_t year = 0, test_year = 0; @@ -663,7 +715,8 @@ void testDateEpoch(Test * pTest) } } -void testBACnetDayOfWeek(Test * pTest) +void testBACnetDayOfWeek( + Test * pTest) { uint8_t dow = 0; @@ -692,7 +745,8 @@ void testBACnetDayOfWeek(Test * pTest) } #ifdef TEST_DATE_TIME -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -723,5 +777,5 @@ int main(void) return 0; } -#endif /* TEST_DATE_TIME */ -#endif /* TEST */ +#endif /* TEST_DATE_TIME */ +#endif /* TEST */ diff --git a/bacnet-stack/src/dcc.c b/bacnet-stack/src/dcc.c index ecc25e9c..4e4f0310 100644 --- a/bacnet-stack/src/dcc.c +++ b/bacnet-stack/src/dcc.c @@ -46,12 +46,14 @@ static BACNET_COMMUNICATION_ENABLE_DISABLE DCC_Enable_Disable = COMMUNICATION_ENABLE; /* password is optionally supported */ -BACNET_COMMUNICATION_ENABLE_DISABLE dcc_enable_status(void) +BACNET_COMMUNICATION_ENABLE_DISABLE dcc_enable_status( + void) { return DCC_Enable_Disable; } -bool dcc_communication_enabled(void) +bool dcc_communication_enabled( + void) { return (DCC_Enable_Disable == COMMUNICATION_ENABLE); } @@ -59,7 +61,8 @@ bool dcc_communication_enabled(void) /* When network communications are completely disabled, only DeviceCommunicationControl and ReinitializeDevice APDUs shall be processed and no messages shall be initiated.*/ -bool dcc_communication_disabled(void) +bool dcc_communication_disabled( + void) { return (DCC_Enable_Disable == COMMUNICATION_DISABLE); } @@ -73,19 +76,22 @@ bool dcc_communication_disabled(void) for any Who-Is request that is received if and only if the Who-Is request does not contain an address range or the device is included in the address range. */ -bool dcc_communication_initiation_disabled(void) +bool dcc_communication_initiation_disabled( + void) { return (DCC_Enable_Disable == COMMUNICATION_DISABLE_INITIATION); } -uint32_t dcc_duration_seconds(void) +uint32_t dcc_duration_seconds( + void) { return DCC_Time_Duration_Seconds; } /* called every second or so. If more than one second, then seconds should be the number of seconds to tick away */ -void dcc_timer_seconds(uint32_t seconds) +void dcc_timer_seconds( + uint32_t seconds) { if (DCC_Time_Duration_Seconds) { if (DCC_Time_Duration_Seconds > seconds) @@ -98,7 +104,8 @@ void dcc_timer_seconds(uint32_t seconds) } } -bool dcc_set_status_duration(BACNET_COMMUNICATION_ENABLE_DISABLE status, +bool dcc_set_status_duration( + BACNET_COMMUNICATION_ENABLE_DISABLE status, uint16_t minutes) { bool valid = false; @@ -117,12 +124,15 @@ bool dcc_set_status_duration(BACNET_COMMUNICATION_ENABLE_DISABLE status, } /* encode service */ -int dcc_encode_apdu(uint8_t * apdu, uint8_t invoke_id, uint16_t timeDuration, /* 0=optional */ +int dcc_encode_apdu( + uint8_t * apdu, + uint8_t invoke_id, + uint16_t timeDuration, /* 0=optional */ BACNET_COMMUNICATION_ENABLE_DISABLE enable_disable, BACNET_CHARACTER_STRING * password) -{ /* NULL=optional */ - int len = 0; /* length of each encoding */ - int apdu_len = 0; /* total length of the apdu, return value */ +{ /* NULL=optional */ + int len = 0; /* length of each encoding */ + int apdu_len = 0; /* total length of the apdu, return value */ if (apdu) { apdu[0] = PDU_TYPE_CONFIRMED_SERVICE_REQUEST; @@ -132,20 +142,17 @@ int dcc_encode_apdu(uint8_t * apdu, uint8_t invoke_id, uint16_t timeDuration, apdu_len = 4; /* optional timeDuration */ if (timeDuration) { - len = - encode_context_unsigned(&apdu[apdu_len], 0, timeDuration); + len = encode_context_unsigned(&apdu[apdu_len], 0, timeDuration); apdu_len += len; } /* enable disable */ - len = - encode_context_enumerated(&apdu[apdu_len], 1, enable_disable); + len = encode_context_enumerated(&apdu[apdu_len], 1, enable_disable); apdu_len += len; /* optional password */ if (password) { /* FIXME: must be at least 1 character, limited to 20 characters */ len = - encode_context_character_string(&apdu[apdu_len], 2, - password); + encode_context_character_string(&apdu[apdu_len], 2, password); apdu_len += len; } } @@ -154,7 +161,8 @@ int dcc_encode_apdu(uint8_t * apdu, uint8_t invoke_id, uint16_t timeDuration, } /* decode the service request only */ -int dcc_decode_service_request(uint8_t * apdu, +int dcc_decode_service_request( + uint8_t * apdu, unsigned apdu_len, uint16_t * timeDuration, BACNET_COMMUNICATION_ENABLE_DISABLE * enable_disable, @@ -184,7 +192,7 @@ int dcc_decode_service_request(uint8_t * apdu, &tag_number, &len_value_type); len += decode_enumerated(&apdu[len], len_value_type, &value); if (enable_disable) - *enable_disable = (BACNET_COMMUNICATION_ENABLE_DISABLE)value; + *enable_disable = (BACNET_COMMUNICATION_ENABLE_DISABLE) value; /* Tag 2: password --optional-- */ if (len < apdu_len) { if (!decode_is_context_tag(&apdu[len], 2)) @@ -192,8 +200,7 @@ int dcc_decode_service_request(uint8_t * apdu, len += decode_tag_number_and_value(&apdu[len], &tag_number, &len_value_type); len += - decode_character_string(&apdu[len], len_value_type, - password); + decode_character_string(&apdu[len], len_value_type, password); } else if (password) characterstring_init_ansi(password, NULL); } @@ -206,7 +213,8 @@ int dcc_decode_service_request(uint8_t * apdu, #include #include "ctest.h" -int dcc_decode_apdu(uint8_t * apdu, +int dcc_decode_apdu( + uint8_t * apdu, unsigned apdu_len, uint8_t * invoke_id, uint16_t * timeDuration, @@ -235,7 +243,8 @@ int dcc_decode_apdu(uint8_t * apdu, return len; } -void test_DeviceCommunicationControlData(Test * pTest, +void test_DeviceCommunicationControlData( + Test * pTest, uint8_t invoke_id, uint16_t timeDuration, BACNET_COMMUNICATION_ENABLE_DISABLE enable_disable, @@ -265,7 +274,8 @@ void test_DeviceCommunicationControlData(Test * pTest, ct_test(pTest, characterstring_same(&test_password, password)); } -void test_DeviceCommunicationControl(Test * pTest) +void test_DeviceCommunicationControl( + Test * pTest) { uint8_t invoke_id = 128; uint16_t timeDuration = 0; @@ -287,7 +297,8 @@ void test_DeviceCommunicationControl(Test * pTest) } #ifdef TEST_DEVICE_COMMUNICATION_CONTROL -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -304,5 +315,5 @@ int main(void) return 0; } -#endif /* TEST_DEVICE_COMMUNICATION_CONTROL */ -#endif /* TEST */ +#endif /* TEST_DEVICE_COMMUNICATION_CONTROL */ +#endif /* TEST */ diff --git a/bacnet-stack/src/filename.c b/bacnet-stack/src/filename.c index 449ad3f8..71d1b4b3 100644 --- a/bacnet-stack/src/filename.c +++ b/bacnet-stack/src/filename.c @@ -34,7 +34,8 @@ #include #include -char *filename_remove_path(const char *filename_in) +char *filename_remove_path( + const char *filename_in) { char *filename_out = NULL; @@ -57,7 +58,8 @@ char *filename_remove_path(const char *filename_in) #include "ctest.h" -void testFilename(Test * pTest) +void testFilename( + Test * pTest) { char *data1 = "c:\\Joshua\\run"; char *data2 = "/home/Anna/run"; @@ -78,7 +80,8 @@ void testFilename(Test * pTest) } #ifdef TEST_FILENAME -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -97,5 +100,5 @@ int main(void) return 0; } -#endif /* TEST_FILENAME */ -#endif /* TEST */ +#endif /* TEST_FILENAME */ +#endif /* TEST */ diff --git a/bacnet-stack/src/iam.c b/bacnet-stack/src/iam.c index 2c5519f1..888c20f7 100755 --- a/bacnet-stack/src/iam.c +++ b/bacnet-stack/src/iam.c @@ -42,12 +42,15 @@ #include "address.h" /* encode I-Am service */ -int iam_encode_apdu(uint8_t * apdu, +int iam_encode_apdu( + uint8_t * apdu, uint32_t device_id, - unsigned max_apdu, int segmentation, uint16_t vendor_id) + unsigned max_apdu, + int segmentation, + uint16_t vendor_id) { - int len = 0; /* length of each encoding */ - int apdu_len = 0; /* total length of the apdu, return value */ + int len = 0; /* length of each encoding */ + int apdu_len = 0; /* total length of the apdu, return value */ if (apdu) { apdu[0] = PDU_TYPE_UNCONFIRMED_SERVICE_REQUEST; @@ -67,12 +70,15 @@ int iam_encode_apdu(uint8_t * apdu, return apdu_len; } -int iam_decode_service_request(uint8_t * apdu, +int iam_decode_service_request( + uint8_t * apdu, uint32_t * pDevice_id, - unsigned *pMax_apdu, int *pSegmentation, uint16_t * pVendor_id) + unsigned *pMax_apdu, + int *pSegmentation, + uint16_t * pVendor_id) { int len = 0; - int apdu_len = 0; /* total length of the apdu, return value */ + int apdu_len = 0; /* total length of the apdu, return value */ int object_type = 0; /* should be a Device Object */ uint32_t object_instance = 0; uint8_t tag_number = 0; @@ -82,13 +88,11 @@ int iam_decode_service_request(uint8_t * apdu, /* OBJECT ID - object id */ len = - decode_tag_number_and_value(&apdu[apdu_len], &tag_number, - &len_value); + decode_tag_number_and_value(&apdu[apdu_len], &tag_number, &len_value); apdu_len += len; if (tag_number != BACNET_APPLICATION_TAG_OBJECT_ID) return -1; - len = - decode_object_id(&apdu[apdu_len], &object_type, &object_instance); + len = decode_object_id(&apdu[apdu_len], &object_type, &object_instance); apdu_len += len; if (object_type != OBJECT_DEVICE) return -1; @@ -96,8 +100,7 @@ int iam_decode_service_request(uint8_t * apdu, *pDevice_id = object_instance; /* MAX APDU - unsigned */ len = - decode_tag_number_and_value(&apdu[apdu_len], &tag_number, - &len_value); + decode_tag_number_and_value(&apdu[apdu_len], &tag_number, &len_value); apdu_len += len; if (tag_number != BACNET_APPLICATION_TAG_UNSIGNED_INT) return -1; @@ -107,8 +110,7 @@ int iam_decode_service_request(uint8_t * apdu, *pMax_apdu = decoded_value; /* Segmentation - enumerated */ len = - decode_tag_number_and_value(&apdu[apdu_len], &tag_number, - &len_value); + decode_tag_number_and_value(&apdu[apdu_len], &tag_number, &len_value); apdu_len += len; if (tag_number != BACNET_APPLICATION_TAG_ENUMERATED) return -1; @@ -120,8 +122,7 @@ int iam_decode_service_request(uint8_t * apdu, *pSegmentation = decoded_integer; /* Vendor ID - unsigned16 */ len = - decode_tag_number_and_value(&apdu[apdu_len], &tag_number, - &len_value); + decode_tag_number_and_value(&apdu[apdu_len], &tag_number, &len_value); apdu_len += len; if (tag_number != BACNET_APPLICATION_TAG_UNSIGNED_INT) return -1; @@ -135,9 +136,10 @@ int iam_decode_service_request(uint8_t * apdu, return apdu_len; } -int iam_encode_pdu(uint8_t * buffer, - BACNET_ADDRESS *dest, - BACNET_NPDU_DATA *npdu_data) +int iam_encode_pdu( + uint8_t * buffer, + BACNET_ADDRESS * dest, + BACNET_NPDU_DATA * npdu_data) { int len = 0; int pdu_len = 0; @@ -156,7 +158,8 @@ int iam_encode_pdu(uint8_t * buffer, return pdu_len; } -int iam_send(uint8_t * buffer) +int iam_send( + uint8_t * buffer) { int pdu_len = 0; BACNET_ADDRESS dest; @@ -179,11 +182,14 @@ int iam_send(uint8_t * buffer) #include #include "ctest.h" -int iam_decode_apdu(uint8_t * apdu, +int iam_decode_apdu( + uint8_t * apdu, uint32_t * pDevice_id, - unsigned *pMax_apdu, int *pSegmentation, uint16_t * pVendor_id) + unsigned *pMax_apdu, + int *pSegmentation, + uint16_t * pVendor_id) { - int apdu_len = 0; /* total length of the apdu, return value */ + int apdu_len = 0; /* total length of the apdu, return value */ /* valid data? */ if (!apdu) @@ -199,7 +205,8 @@ int iam_decode_apdu(uint8_t * apdu, return apdu_len; } -void testIAm(Test * pTest) +void testIAm( + Test * pTest) { uint8_t apdu[480] = { 0 }; int len = 0; @@ -217,8 +224,7 @@ void testIAm(Test * pTest) ct_test(pTest, len != 0); len = iam_decode_apdu(&apdu[0], - &test_device_id, - &test_max_apdu, &test_segmentation, &test_vendor_id); + &test_device_id, &test_max_apdu, &test_segmentation, &test_vendor_id); ct_test(pTest, len != -1); ct_test(pTest, test_device_id == device_id); @@ -229,8 +235,11 @@ void testIAm(Test * pTest) #ifdef TEST_IAM /* dummy function stubs */ -int datalink_send_pdu(BACNET_ADDRESS * dest, - BACNET_NPDU_DATA * npdu_data, uint8_t * pdu, unsigned pdu_len) +int datalink_send_pdu( + BACNET_ADDRESS * dest, + BACNET_NPDU_DATA * npdu_data, + uint8_t * pdu, + unsigned pdu_len) { (void) dest; (void) npdu_data; @@ -240,23 +249,28 @@ int datalink_send_pdu(BACNET_ADDRESS * dest, return 0; } -void datalink_get_broadcast_address(BACNET_ADDRESS * dest) -{ /* destination address */ +void datalink_get_broadcast_address( + BACNET_ADDRESS * dest) +{ /* destination address */ (void) dest; } -uint16_t Device_Vendor_Identifier(void) +uint16_t Device_Vendor_Identifier( + void) { return 0; } -uint32_t Device_Object_Instance_Number(void) +uint32_t Device_Object_Instance_Number( + void) { return 0; } -void address_add_binding(uint32_t device_id, - unsigned max_apdu, BACNET_ADDRESS * src) +void address_add_binding( + uint32_t device_id, + unsigned max_apdu, + BACNET_ADDRESS * src) { (void) device_id; (void) max_apdu; @@ -264,14 +278,16 @@ void address_add_binding(uint32_t device_id, } /* dummy for apdu dependency */ -void tsm_free_invoke_id(uint8_t invokeID) +void tsm_free_invoke_id( + uint8_t invokeID) { /* dummy stub for testing */ (void) invokeID; } -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -288,5 +304,5 @@ int main(void) return 0; } -#endif /* TEST_IAM */ -#endif /* TEST */ +#endif /* TEST_IAM */ +#endif /* TEST */ diff --git a/bacnet-stack/src/ihave.c b/bacnet-stack/src/ihave.c index ea9dd14f..673bd2ed 100644 --- a/bacnet-stack/src/ihave.c +++ b/bacnet-stack/src/ihave.c @@ -37,10 +37,12 @@ #include "bacdef.h" #include "ihave.h" -int ihave_encode_apdu(uint8_t * apdu, BACNET_I_HAVE_DATA * data) +int ihave_encode_apdu( + uint8_t * apdu, + BACNET_I_HAVE_DATA * data) { - int len = 0; /* length of each encoding */ - int apdu_len = 0; /* total length of the apdu, return value */ + int len = 0; /* length of each encoding */ + int apdu_len = 0; /* total length of the apdu, return value */ if (apdu && data) { apdu[0] = PDU_TYPE_UNCONFIRMED_SERVICE_REQUEST; @@ -64,8 +66,10 @@ int ihave_encode_apdu(uint8_t * apdu, BACNET_I_HAVE_DATA * data) } /* decode the service request only */ -int ihave_decode_service_request(uint8_t * apdu, - unsigned apdu_len, BACNET_I_HAVE_DATA * data) +int ihave_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + BACNET_I_HAVE_DATA * data) { int len = 0; uint8_t tag_number = 0; @@ -75,8 +79,7 @@ int ihave_decode_service_request(uint8_t * apdu, if (apdu_len && data) { /* deviceIdentifier */ len += - decode_tag_number_and_value(&apdu[len], &tag_number, - &len_value); + decode_tag_number_and_value(&apdu[len], &tag_number, &len_value); if (tag_number == BACNET_APPLICATION_TAG_OBJECT_ID) { len += decode_object_id(&apdu[len], &decoded_type, &data->device_id.instance); @@ -85,8 +88,7 @@ int ihave_decode_service_request(uint8_t * apdu, return -1; /* objectIdentifier */ len += - decode_tag_number_and_value(&apdu[len], &tag_number, - &len_value); + decode_tag_number_and_value(&apdu[len], &tag_number, &len_value); if (tag_number == BACNET_APPLICATION_TAG_OBJECT_ID) { len += decode_object_id(&apdu[len], &decoded_type, &data->object_id.instance); @@ -95,8 +97,7 @@ int ihave_decode_service_request(uint8_t * apdu, return -1; /* objectName */ len += - decode_tag_number_and_value(&apdu[len], &tag_number, - &len_value); + decode_tag_number_and_value(&apdu[len], &tag_number, &len_value); if (tag_number == BACNET_APPLICATION_TAG_CHARACTER_STRING) { len += decode_character_string(&apdu[len], len_value, &data->object_name); @@ -108,8 +109,10 @@ int ihave_decode_service_request(uint8_t * apdu, return len; } -int ihave_decode_apdu(uint8_t * apdu, - unsigned apdu_len, BACNET_I_HAVE_DATA * data) +int ihave_decode_apdu( + uint8_t * apdu, + unsigned apdu_len, + BACNET_I_HAVE_DATA * data) { int len = 0; @@ -130,7 +133,9 @@ int ihave_decode_apdu(uint8_t * apdu, #include #include "ctest.h" -void testIHaveData(Test * pTest, BACNET_I_HAVE_DATA * data) +void testIHaveData( + Test * pTest, + BACNET_I_HAVE_DATA * data) { uint8_t apdu[480] = { 0 }; int len = 0; @@ -144,16 +149,15 @@ void testIHaveData(Test * pTest, BACNET_I_HAVE_DATA * data) len = ihave_decode_apdu(&apdu[0], apdu_len, &test_data); ct_test(pTest, len != -1); ct_test(pTest, test_data.device_id.type == data->device_id.type); - ct_test(pTest, test_data.device_id.instance == - data->device_id.instance); + ct_test(pTest, test_data.device_id.instance == data->device_id.instance); ct_test(pTest, test_data.object_id.type == data->object_id.type); - ct_test(pTest, test_data.object_id.instance == - data->object_id.instance); + ct_test(pTest, test_data.object_id.instance == data->object_id.instance); ct_test(pTest, characterstring_same(&test_data.object_name, &data->object_name)); } -void testIHave(Test * pTest) +void testIHave( + Test * pTest) { BACNET_I_HAVE_DATA data; @@ -175,7 +179,8 @@ void testIHave(Test * pTest) } #ifdef TEST_I_HAVE -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -192,5 +197,5 @@ int main(void) return 0; } -#endif /* TEST_WHOIS */ -#endif /* TEST */ +#endif /* TEST_WHOIS */ +#endif /* TEST */ diff --git a/bacnet-stack/src/indtext.c b/bacnet-stack/src/indtext.c index 02cd41c6..bfe645cc 100644 --- a/bacnet-stack/src/indtext.c +++ b/bacnet-stack/src/indtext.c @@ -39,8 +39,10 @@ #define strcasecmp stricmp #endif -bool indtext_by_string(INDTEXT_DATA * data_list, - const char *search_name, unsigned *found_index) +bool indtext_by_string( + INDTEXT_DATA * data_list, + const char *search_name, + unsigned *found_index) { bool found = false; unsigned index = 0; @@ -63,8 +65,10 @@ bool indtext_by_string(INDTEXT_DATA * data_list, } /* case insensitive version */ -bool indtext_by_istring(INDTEXT_DATA * data_list, - const char *search_name, unsigned *found_index) +bool indtext_by_istring( + INDTEXT_DATA * data_list, + const char *search_name, + unsigned *found_index) { bool found = false; unsigned index = 0; @@ -86,8 +90,10 @@ bool indtext_by_istring(INDTEXT_DATA * data_list, return found; } -unsigned indtext_by_string_default(INDTEXT_DATA * data_list, - const char *search_name, unsigned default_index) +unsigned indtext_by_string_default( + INDTEXT_DATA * data_list, + const char *search_name, + unsigned default_index) { unsigned index = 0; @@ -97,8 +103,10 @@ unsigned indtext_by_string_default(INDTEXT_DATA * data_list, return index; } -unsigned indtext_by_istring_default(INDTEXT_DATA * data_list, - const char *search_name, unsigned default_index) +unsigned indtext_by_istring_default( + INDTEXT_DATA * data_list, + const char *search_name, + unsigned default_index) { unsigned index = 0; @@ -108,8 +116,10 @@ unsigned indtext_by_istring_default(INDTEXT_DATA * data_list, return index; } -const char *indtext_by_index_default(INDTEXT_DATA * data_list, - unsigned index, const char *default_string) +const char *indtext_by_index_default( + INDTEXT_DATA * data_list, + unsigned index, + const char *default_string) { const char *pString = NULL; @@ -126,10 +136,12 @@ const char *indtext_by_index_default(INDTEXT_DATA * data_list, return pString ? pString : default_string; } -const char *indtext_by_index_split_default(INDTEXT_DATA * data_list, +const char *indtext_by_index_split_default( + INDTEXT_DATA * data_list, int index, int split_index, - const char *before_split_default_name, const char *default_name) + const char *before_split_default_name, + const char *default_name) { if (index < split_index) return indtext_by_index_default(data_list, index, @@ -139,14 +151,17 @@ const char *indtext_by_index_split_default(INDTEXT_DATA * data_list, }; -const char *indtext_by_index(INDTEXT_DATA * data_list, unsigned index) +const char *indtext_by_index( + INDTEXT_DATA * data_list, + unsigned index) { return indtext_by_index_default(data_list, index, NULL); } -unsigned indtext_count(INDTEXT_DATA * data_list) +unsigned indtext_count( + INDTEXT_DATA * data_list) { - unsigned count = 0; /* return value */ + unsigned count = 0; /* return value */ if (data_list) { while (data_list->pString) { @@ -170,9 +185,10 @@ static INDTEXT_DATA data_list[] = { {0, NULL} }; -void testIndexText(Test * pTest) +void testIndexText( + Test * pTest) { - unsigned i; /*counter */ + unsigned i; /*counter */ const char *pString; unsigned index; bool valid; @@ -206,7 +222,8 @@ void testIndexText(Test * pTest) #endif #ifdef TEST_INDEX_TEXT -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -225,4 +242,4 @@ int main(void) return 0; } -#endif /* TEST_INDEX_TEXT */ +#endif /* TEST_INDEX_TEXT */ diff --git a/bacnet-stack/src/key.c b/bacnet-stack/src/key.c index 06047ace..59ca8793 100644 --- a/bacnet-stack/src/key.c +++ b/bacnet-stack/src/key.c @@ -42,7 +42,8 @@ #include "ctest.h" /* test the encode and decode macros */ -void testKeys(Test * pTest) +void testKeys( + Test * pTest) { int type, id; int decoded_type, decoded_id; @@ -62,7 +63,8 @@ void testKeys(Test * pTest) } /* test the encode and decode macros */ -void testKeySample(Test * pTest) +void testKeySample( + Test * pTest) { int type, id; int type_list[] = { 0, 1, KEY_TYPE_MAX / 2, KEY_TYPE_MAX - 1, -1 }; @@ -92,7 +94,8 @@ void testKeySample(Test * pTest) } #ifdef TEST_KEY -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -112,5 +115,5 @@ int main(void) return 0; } -#endif /* LOCAL_TEST */ +#endif /* LOCAL_TEST */ #endif diff --git a/bacnet-stack/src/keylist.c b/bacnet-stack/src/keylist.c index 511c8124..b5f09675 100644 --- a/bacnet-stack/src/keylist.c +++ b/bacnet-stack/src/keylist.c @@ -41,7 +41,7 @@ #include -#include "keylist.h" /* check for valid prototypes */ +#include "keylist.h" /* check for valid prototypes */ #ifndef FALSE #define FALSE 0 @@ -56,13 +56,15 @@ /*/////////////////////////////////////////////////////////////////// */ /* grab memory for a node */ -static struct Keylist_Node *NodeCreate(void) +static struct Keylist_Node *NodeCreate( + void) { return calloc(1, sizeof(struct Keylist_Node)); } /* grab memory for a list */ -static struct Keylist *KeylistCreate(void) +static struct Keylist *KeylistCreate( + void) { return calloc(1, sizeof(struct Keylist)); } @@ -70,12 +72,13 @@ static struct Keylist *KeylistCreate(void) /* check to see if the array is big enough for an addition */ /* or is too big when we are deleting and we can shrink */ /* returns TRUE if success, FALSE if failed */ -static int CheckArraySize(OS_Keylist list) +static int CheckArraySize( + OS_Keylist list) { - int new_size = 0; /* set it up so that no size change is the default */ + int new_size = 0; /* set it up so that no size change is the default */ const int chunk = 8; /* minimum number of nodes to allocate memory for */ struct Keylist_Node **new_array; /* new array of nodes, if needed */ - int i; /* counter */ + int i; /* counter */ if (!list) return FALSE; @@ -115,14 +118,17 @@ static int CheckArraySize(OS_Keylist list) /* returns the found key and the index where it was found in parameters */ /* If the key is not found, the nearest index from the bottom will be returned, */ /* allowing the ability to find where an key should go into the list. */ -static int FindIndex(OS_Keylist list, KEY key, int *pIndex) +static int FindIndex( + OS_Keylist list, + KEY key, + int *pIndex) { struct Keylist_Node *node; /* holds the new node */ - int left = 0; /* the left branch of tree, beginning of list */ - int right = 0; /* the right branch on the tree, end of list */ - int index = 0; /* our current search place in the array */ + int left = 0; /* the left branch of tree, beginning of list */ + int right = 0; /* the right branch on the tree, end of list */ + int index = 0; /* our current search place in the array */ KEY current_key = 0; /* place holder for current node key */ - int status = FALSE; /* return value */ + int status = FALSE; /* return value */ if (!list || !list->array || !list->count) { *pIndex = 0; return (FALSE); @@ -166,11 +172,14 @@ static int FindIndex(OS_Keylist list, KEY key, int *pIndex) /* list data functions */ /*/////////////////////////////////////////////////////////////////// */ /* inserts a node into its sorted position */ -int Keylist_Data_Add(OS_Keylist list, KEY key, void *data) +int Keylist_Data_Add( + OS_Keylist list, + KEY key, + void *data) { struct Keylist_Node *node; /* holds the new node */ - int index = -1; /* return value */ - int i; /* counts through the array */ + int index = -1; /* return value */ + int i; /* counts through the array */ if (list && CheckArraySize(list)) { /* figure out where to put the new node */ @@ -208,7 +217,9 @@ int Keylist_Data_Add(OS_Keylist list, KEY key, void *data) /* deletes a node specified by its index */ /* returns the data from the node */ -void *Keylist_Data_Delete_By_Index(OS_Keylist list, int index) +void *Keylist_Data_Delete_By_Index( + OS_Keylist list, + int index) { struct Keylist_Node *node; void *data = NULL; @@ -231,7 +242,7 @@ void *Keylist_Data_Delete_By_Index(OS_Keylist list, int index) } /* Move all the nodes down one */ else { - int i; /* counter */ + int i; /* counter */ int count = list->count - 1; for (i = index; i < count; i++) { list->array[i] = list->array[i + 1]; @@ -250,10 +261,12 @@ void *Keylist_Data_Delete_By_Index(OS_Keylist list, int index) /* deletes a node specified by its key */ /* returns the data from the node */ -void *Keylist_Data_Delete(OS_Keylist list, KEY key) +void *Keylist_Data_Delete( + OS_Keylist list, + KEY key) { - void *data = NULL; /* return value */ - int index; /* where the node is in the array */ + void *data = NULL; /* return value */ + int index; /* where the node is in the array */ if (list) { if (FindIndex(list, key, &index)) @@ -264,10 +277,11 @@ void *Keylist_Data_Delete(OS_Keylist list, KEY key) } /* returns the data from last node, and removes it from the list */ -void *Keylist_Data_Pop(OS_Keylist list) +void *Keylist_Data_Pop( + OS_Keylist list) { - void *data = NULL; /* return value */ - int index; /* position in the array */ + void *data = NULL; /* return value */ + int index; /* position in the array */ if (list && list->count) { index = list->count - 1; @@ -278,10 +292,12 @@ void *Keylist_Data_Pop(OS_Keylist list) } /* returns the data from the node specified by key */ -void *Keylist_Data(OS_Keylist list, KEY key) +void *Keylist_Data( + OS_Keylist list, + KEY key) { struct Keylist_Node *node = NULL; - int index = 0; /* used to look up the index of node */ + int index = 0; /* used to look up the index of node */ if (list && list->array && list->count) { if (FindIndex(list, key, &index)) @@ -292,7 +308,9 @@ void *Keylist_Data(OS_Keylist list, KEY key) } /* returns the data specified by key */ -void *Keylist_Data_Index(OS_Keylist list, int index) +void *Keylist_Data_Index( + OS_Keylist list, + int index) { struct Keylist_Node *node = NULL; @@ -304,9 +322,11 @@ void *Keylist_Data_Index(OS_Keylist list, int index) } /* return the key at the given index */ -KEY Keylist_Key(OS_Keylist list, int index) +KEY Keylist_Key( + OS_Keylist list, + int index) { - KEY key = 0; /* return value */ + KEY key = 0; /* return value */ struct Keylist_Node *node; if (list && list->array && list->count && @@ -320,7 +340,9 @@ KEY Keylist_Key(OS_Keylist list, int index) } /* returns the next empty key from the list */ -KEY Keylist_Next_Empty_Key(OS_Keylist list, KEY key) +KEY Keylist_Next_Empty_Key( + OS_Keylist list, + KEY key) { int index; @@ -336,7 +358,8 @@ KEY Keylist_Next_Empty_Key(OS_Keylist list, KEY key) } /* return the number of nodes in this list */ -int Keylist_Count(OS_Keylist list) +int Keylist_Count( + OS_Keylist list) { return list->count; } @@ -346,7 +369,8 @@ int Keylist_Count(OS_Keylist list) /*/////////////////////////////////////////////////////////////////// */ /* returns head of the list or NULL on failure. */ -OS_Keylist Keylist_Create(void) +OS_Keylist Keylist_Create( + void) { struct Keylist *list; @@ -358,8 +382,9 @@ OS_Keylist Keylist_Create(void) } /* delete specified list */ -void Keylist_Delete(OS_Keylist list) /* list number to be deleted */ -{ +void Keylist_Delete( + OS_Keylist list) +{ /* list number to be deleted */ if (list) { /* clean out the list */ while (list->count) { @@ -380,7 +405,8 @@ void Keylist_Delete(OS_Keylist list) /* list number to be deleted */ #include "ctest.h" /* test the encode and decode macros */ -void testKeySample(Test * pTest) +void testKeySample( + Test * pTest) { int type, id; int type_list[] = @@ -412,7 +438,8 @@ void testKeySample(Test * pTest) } /* test the FIFO */ -void testKeyListFIFO(Test * pTest) +void testKeyListFIFO( + Test * pTest) { OS_Keylist list; KEY key; @@ -455,7 +482,8 @@ void testKeyListFIFO(Test * pTest) } /* test the FILO */ -void testKeyListFILO(Test * pTest) +void testKeyListFILO( + Test * pTest) { OS_Keylist list; KEY key; @@ -501,7 +529,8 @@ void testKeyListFILO(Test * pTest) return; } -void testKeyListDataKey(Test * pTest) +void testKeyListDataKey( + Test * pTest) { OS_Keylist list; KEY key; @@ -581,7 +610,8 @@ void testKeyListDataKey(Test * pTest) return; } -void testKeyListDataIndex(Test * pTest) +void testKeyListDataIndex( + Test * pTest) { OS_Keylist list; KEY key; @@ -652,7 +682,8 @@ void testKeyListDataIndex(Test * pTest) } /* test access of a lot of entries */ -void testKeyListLarge(Test * pTest) +void testKeyListLarge( + Test * pTest) { int data1 = 42; int *data; @@ -683,7 +714,8 @@ void testKeyListLarge(Test * pTest) } #ifdef TEST_KEYLIST -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -712,5 +744,5 @@ int main(void) return 0; } -#endif /* TEST_KEYLIST */ -#endif /* TEST */ +#endif /* TEST_KEYLIST */ +#endif /* TEST */ diff --git a/bacnet-stack/src/mstp.c b/bacnet-stack/src/mstp.c index cb927abe..8853b456 100644 --- a/bacnet-stack/src/mstp.c +++ b/bacnet-stack/src/mstp.c @@ -134,22 +134,24 @@ /* we need to be able to increment without rolling over */ #define INCREMENT_AND_LIMIT_UINT8(x) {if (x < 0xFF) x++;} -bool MSTP_Line_Active(volatile struct mstp_port_struct_t *mstp_port) +bool MSTP_Line_Active( + volatile struct mstp_port_struct_t *mstp_port) { return (mstp_port->EventCount > Nmin_octets); } -uint16_t MSTP_Create_Frame(uint8_t * buffer, /* where frame is loaded */ +uint16_t MSTP_Create_Frame( + uint8_t * buffer, /* where frame is loaded */ uint16_t buffer_len, /* amount of space available */ - uint8_t frame_type, /* type of frame to send - see defines */ + uint8_t frame_type, /* type of frame to send - see defines */ uint8_t destination, /* destination address */ - uint8_t source, /* source address */ - uint8_t * data, /* any data to be sent - may be null */ + uint8_t source, /* source address */ + uint8_t * data, /* any data to be sent - may be null */ uint16_t data_len) -{ /* number of bytes of data (up to 501) */ +{ /* number of bytes of data (up to 501) */ uint8_t crc8 = 0xFF; /* used to calculate the crc value */ uint16_t crc16 = 0xFFFF; /* used to calculate the crc value */ - uint16_t index = 0; /* used to load the data portion of the frame */ + uint16_t index = 0; /* used to load the data portion of the frame */ /* not enough to do a header */ if (buffer_len < 8) @@ -189,34 +191,33 @@ uint16_t MSTP_Create_Frame(uint8_t * buffer, /* where frame is loaded */ return 0; } - return index; /* returns the frame length */ + return index; /* returns the frame length */ } -void MSTP_Create_And_Send_Frame(volatile struct mstp_port_struct_t *mstp_port, /* port to send from */ - uint8_t frame_type, /* type of frame to send - see defines */ +void MSTP_Create_And_Send_Frame( + volatile struct mstp_port_struct_t *mstp_port, /* port to send from */ + uint8_t frame_type, /* type of frame to send - see defines */ uint8_t destination, /* destination address */ - uint8_t source, /* source address */ - uint8_t * data, /* any data to be sent - may be null */ + uint8_t source, /* source address */ + uint8_t * data, /* any data to be sent - may be null */ uint16_t data_len) -{ /* number of bytes of data (up to 501) */ - uint16_t len = 0; /* number of bytes to send */ +{ /* number of bytes of data (up to 501) */ + uint16_t len = 0; /* number of bytes to send */ - len = MSTP_Create_Frame( - (uint8_t *)&mstp_port->OutputBuffer[0], /* where frame is loaded */ - mstp_port->OutputBufferSize, /* amount of space available */ - frame_type, /* type of frame to send - see defines */ - destination, /* destination address */ - source, /* source address */ - data, /* any data to be sent - may be null */ - data_len); /* number of bytes of data (up to 501) */ + len = MSTP_Create_Frame((uint8_t *) & mstp_port->OutputBuffer[0], /* where frame is loaded */ + mstp_port->OutputBufferSize, /* amount of space available */ + frame_type, /* type of frame to send - see defines */ + destination, /* destination address */ + source, /* source address */ + data, /* any data to be sent - may be null */ + data_len); /* number of bytes of data (up to 501) */ - RS485_Send_Frame(mstp_port, - (uint8_t *)&mstp_port->OutputBuffer[0], - len); + RS485_Send_Frame(mstp_port, (uint8_t *) & mstp_port->OutputBuffer[0], len); /* FIXME: be sure to reset SilenceTimer() after each octet is sent! */ } -void MSTP_Receive_Frame_FSM(volatile struct mstp_port_struct_t *mstp_port) +void MSTP_Receive_Frame_FSM( + volatile struct mstp_port_struct_t *mstp_port) { #if PRINT_ENABLED_RECEIVE_DATA static MSTP_RECEIVE_STATE receive_state = MSTP_RECEIVE_STATE_IDLE; @@ -229,22 +230,23 @@ void MSTP_Receive_Frame_FSM(volatile struct mstp_port_struct_t *mstp_port) mstp_port->EventCount, mstp_port->DataLength, mstp_port->SilenceTimer(); #endif - switch (mstp_port->receive_state) { - /* In the IDLE state, the node waits for the beginning of a frame. */ - case MSTP_RECEIVE_STATE_IDLE: - /* EatAnError */ - if (mstp_port->ReceiveError == true) { - mstp_port->ReceiveError = false; - mstp_port->SilenceTimerReset(); - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - /* wait for the start of a frame. */ - } else if (mstp_port->DataAvailable == true) { + switch (mstp_port->receive_state) { + /* In the IDLE state, the node waits for the beginning of a frame. */ +case MSTP_RECEIVE_STATE_IDLE: + /* EatAnError */ + if (mstp_port->ReceiveError == true) { + mstp_port->ReceiveError = false; + mstp_port->SilenceTimerReset(); + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + /* wait for the start of a frame. */ + } + else + if (mstp_port->DataAvailable == true) { #if PRINT_ENABLED_RECEIVE_DATA #if (defined(_WIN32) && defined(TEST_DLMSTP)) extern uint32_t timestamp_ms(void); fprintf(stderr, "MSTP Rx %03u: %02X ", - (unsigned)timestamp_ms(), - mstp_port->DataRegister); + (unsigned) timestamp_ms(), mstp_port->DataRegister); #else fprintf(stderr, "MSTP Rx: %02X ", mstp_port->DataRegister); #endif @@ -252,8 +254,7 @@ void MSTP_Receive_Frame_FSM(volatile struct mstp_port_struct_t *mstp_port) /* Preamble1 */ if (mstp_port->DataRegister == 0x55) { /* receive the remainder of the frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_PREAMBLE; - } + mstp_port->receive_state = MSTP_RECEIVE_STATE_PREAMBLE;} /* EatAnOctet */ else { #if PRINT_ENABLED_RECEIVE_DATA @@ -261,790 +262,678 @@ void MSTP_Receive_Frame_FSM(volatile struct mstp_port_struct_t *mstp_port) #endif /* wait for the start of a frame. */ } - mstp_port->DataAvailable = false; + mstp_port->DataAvailable = false; mstp_port->SilenceTimerReset(); - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - } + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount);} break; /* In the PREAMBLE state, the node waits for the second octet of the preamble. */ - case MSTP_RECEIVE_STATE_PREAMBLE: +case MSTP_RECEIVE_STATE_PREAMBLE: /* Timeout */ if (mstp_port->SilenceTimer() > Tframe_abort) { /* a correct preamble has not been received */ /* wait for the start of a frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE;} /* Error */ - else if (mstp_port->ReceiveError == true) { - mstp_port->ReceiveError = false; - mstp_port->SilenceTimerReset(); - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - /* wait for the start of a frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } else if (mstp_port->DataAvailable == true) { + else + if (mstp_port->ReceiveError == true) { + mstp_port->ReceiveError = false; + mstp_port->SilenceTimerReset(); + INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); + /* wait for the start of a frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE;} + else +if (mstp_port->DataAvailable == true) { #if PRINT_ENABLED_RECEIVE_DATA - fprintf(stderr, "%02X ", mstp_port->DataRegister); + fprintf(stderr, "%02X ", mstp_port->DataRegister); #endif - /* Preamble2 */ - if (mstp_port->DataRegister == 0xFF) { - mstp_port->Index = 0; - mstp_port->HeaderCRC = 0xFF; - /* receive the remainder of the frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER; - } + /* Preamble2 */ + if (mstp_port->DataRegister == 0xFF) { + mstp_port->Index = 0; mstp_port->HeaderCRC = 0xFF; + /* receive the remainder of the frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_HEADER;} /* ignore RepeatedPreamble1 */ - else if (mstp_port->DataRegister == 0x55) { + else +if (mstp_port->DataRegister == 0x55) { /* wait for the second preamble octet. */ - } +} /* NotPreamble */ - else { +else { /* wait for the start of a frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } - mstp_port->DataAvailable = false; - mstp_port->SilenceTimerReset(); - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - } - break; - /* In the HEADER state, the node waits for the fixed message header. */ - case MSTP_RECEIVE_STATE_HEADER: +mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE;} +mstp_port->DataAvailable = false; +mstp_port->SilenceTimerReset(); +INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount);} +break; +/* In the HEADER state, the node waits for the fixed message header. */ +case MSTP_RECEIVE_STATE_HEADER: /* Timeout */ - if (mstp_port->SilenceTimer() > Tframe_abort) { +if (mstp_port->SilenceTimer() > Tframe_abort) { /* indicate that an error has occurred during the reception of a frame */ - mstp_port->ReceivedInvalidFrame = true; - /* wait for the start of a frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + mstp_port->ReceivedInvalidFrame = true; + /* wait for the start of a frame. */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; #if PRINT_ENABLED_RECEIVE_ERRORS - fprintf(stderr,"MSTP: Rx Header: SilenceTimer %d > %d\n", - mstp_port->SilenceTimer(), Tframe_abort); + fprintf(stderr, "MSTP: Rx Header: SilenceTimer %d > %d\n", + mstp_port->SilenceTimer(), Tframe_abort); #endif - } +} /* Error */ - else if (mstp_port->ReceiveError == true) { - mstp_port->ReceiveError = false; - mstp_port->SilenceTimerReset(); - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - /* indicate that an error has occurred during the reception of a frame */ - mstp_port->ReceivedInvalidFrame = true; +else +if (mstp_port->ReceiveError == true) { +mstp_port->ReceiveError = false; +mstp_port->SilenceTimerReset(); +INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); +/* indicate that an error has occurred during the reception of a frame */ +mstp_port->ReceivedInvalidFrame = true; #if PRINT_ENABLED_RECEIVE_ERRORS - fprintf(stderr,"MSTP: Rx Header: ReceiveError\n"); +fprintf(stderr, "MSTP: Rx Header: ReceiveError\n"); #endif - /* wait for the start of a frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } else if (mstp_port->DataAvailable == true) { +/* wait for the start of a frame. */ +mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE;} +else +if (mstp_port->DataAvailable == true) { #if PRINT_ENABLED_RECEIVE_DATA - fprintf(stderr, "%02X ", mstp_port->DataRegister); +fprintf(stderr, "%02X ", mstp_port->DataRegister); #endif - /* FrameType */ - if (mstp_port->Index == 0) { - mstp_port->HeaderCRC = - CRC_Calc_Header(mstp_port->DataRegister, - mstp_port->HeaderCRC); - mstp_port->FrameType = mstp_port->DataRegister; - mstp_port->Index = 1; - } +/* FrameType */ +if (mstp_port->Index == 0) { +mstp_port->HeaderCRC = +CRC_Calc_Header(mstp_port->DataRegister, + mstp_port->HeaderCRC); +mstp_port->FrameType = mstp_port->DataRegister; mstp_port->Index = 1;} /* Destination */ - else if (mstp_port->Index == 1) { - mstp_port->HeaderCRC = - CRC_Calc_Header(mstp_port->DataRegister, - mstp_port->HeaderCRC); - mstp_port->DestinationAddress = mstp_port->DataRegister; - mstp_port->Index = 2; - } +else +if (mstp_port->Index == 1) { +mstp_port->HeaderCRC = +CRC_Calc_Header(mstp_port->DataRegister, +mstp_port->HeaderCRC); +mstp_port->DestinationAddress = mstp_port->DataRegister; mstp_port->Index = 2;} /* Source */ - else if (mstp_port->Index == 2) { - mstp_port->HeaderCRC = - CRC_Calc_Header(mstp_port->DataRegister, - mstp_port->HeaderCRC); - mstp_port->SourceAddress = mstp_port->DataRegister; - mstp_port->Index = 3; - } +else +if (mstp_port->Index == 2) { +mstp_port->HeaderCRC = +CRC_Calc_Header(mstp_port->DataRegister, +mstp_port->HeaderCRC); +mstp_port->SourceAddress = mstp_port->DataRegister; mstp_port->Index = 3;} /* Length1 */ - else if (mstp_port->Index == 3) { - mstp_port->HeaderCRC = - CRC_Calc_Header(mstp_port->DataRegister, - mstp_port->HeaderCRC); - mstp_port->DataLength = mstp_port->DataRegister * 256; - mstp_port->Index = 4; - } +else +if (mstp_port->Index == 3) { +mstp_port->HeaderCRC = +CRC_Calc_Header(mstp_port->DataRegister, +mstp_port->HeaderCRC); +mstp_port->DataLength = mstp_port->DataRegister * 256; mstp_port->Index = 4;} /* Length2 */ - else if (mstp_port->Index == 4) { - mstp_port->HeaderCRC = - CRC_Calc_Header(mstp_port->DataRegister, - mstp_port->HeaderCRC); - mstp_port->DataLength += mstp_port->DataRegister; - mstp_port->Index = 5; - } +else +if (mstp_port->Index == 4) { +mstp_port->HeaderCRC = +CRC_Calc_Header(mstp_port->DataRegister, +mstp_port->HeaderCRC); +mstp_port->DataLength += mstp_port->DataRegister; mstp_port->Index = 5;} /* HeaderCRC */ - else if (mstp_port->Index == 5) { - mstp_port->HeaderCRC = - CRC_Calc_Header(mstp_port->DataRegister, - mstp_port->HeaderCRC); - mstp_port->HeaderCRCActual = mstp_port->DataRegister; - /* don't wait for next state - do it here */ - /* MSTP_RECEIVE_STATE_HEADER_CRC */ - if (mstp_port->HeaderCRC != 0x55) { +else +if (mstp_port->Index == 5) { +mstp_port->HeaderCRC = +CRC_Calc_Header(mstp_port->DataRegister, +mstp_port->HeaderCRC); mstp_port->HeaderCRCActual = mstp_port->DataRegister; +/* don't wait for next state - do it here */ +/* MSTP_RECEIVE_STATE_HEADER_CRC */ +if (mstp_port->HeaderCRC != 0x55) { /* BadCRC */ /* indicate that an error has occurred during the reception of a frame */ - mstp_port->ReceivedInvalidFrame = true; +mstp_port->ReceivedInvalidFrame = true; #if PRINT_ENABLED_RECEIVE_ERRORS - fprintf(stderr,"MSTP: Rx Header: BadCRC [%02X]\n", - mstp_port->DataRegister); +fprintf(stderr, "MSTP: Rx Header: BadCRC [%02X]\n", mstp_port->DataRegister); #endif - /* wait for the start of the next frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } else { +/* wait for the start of the next frame. */ +mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE;} +else { /* Note: proposed change to BACnet MSTP state machine! - If we don't decode data that is not for us, we could - get confused about the start if the Preamble 55 FF - is part of the data. */ + If we don't decode data that is not for us, we could + get confused about the start if the Preamble 55 FF + is part of the data. */ /* Data */ - if ((mstp_port->DataLength) && - (mstp_port->DataLength <= mstp_port->InputBufferSize)) { +if ((mstp_port->DataLength) && (mstp_port->DataLength <= mstp_port->InputBufferSize)) { /* Data - decode anyway to keep from false */ - mstp_port->Index = 0; - mstp_port->DataCRC = 0xFFFF; - /* receive the data portion of the frame. */ - mstp_port->receive_state = - MSTP_RECEIVE_STATE_DATA; - } else { +mstp_port->Index = 0; mstp_port->DataCRC = 0xFFFF; +/* receive the data portion of the frame. */ +mstp_port->receive_state = MSTP_RECEIVE_STATE_DATA;} +else { /* FrameTooLong */ - if (mstp_port->DataLength) { +if (mstp_port->DataLength) { #if PRINT_ENABLED_RECEIVE_ERRORS - fprintf(stderr,"MSTP: Rx Header: FrameTooLong %d\n", - mstp_port->DataLength); +fprintf(stderr, "MSTP: Rx Header: FrameTooLong %d\n", mstp_port->DataLength); #endif - /* indicate that a frame with an illegal or */ - /* unacceptable data length has been received */ - mstp_port->ReceivedInvalidFrame = true; - } +/* indicate that a frame with an illegal or */ +/* unacceptable data length has been received */ +mstp_port->ReceivedInvalidFrame = true;} /* NoData */ - else if (mstp_port->DataLength == 0) { +else +if (mstp_port->DataLength == 0) { #if PRINT_ENABLED_RECEIVE_DATA - fprintf(stderr, "%s", - mstptext_frame_type(mstp_port->FrameType)); +fprintf(stderr, "%s", mstptext_frame_type(mstp_port->FrameType)); #endif - if ((mstp_port->DestinationAddress == - mstp_port->This_Station) - || (mstp_port->DestinationAddress == - MSTP_BROADCAST_ADDRESS) - || (mstp_port->Lurking)) { +if ((mstp_port->DestinationAddress == mstp_port->This_Station) +|| (mstp_port->DestinationAddress == MSTP_BROADCAST_ADDRESS) +|| (mstp_port->Lurking)) { /* ForUs */ /* indicate that a frame with no data has been received */ - mstp_port->ReceivedValidFrame = true; - } else { +mstp_port->ReceivedValidFrame = true;} +else { /* NotForUs - drop */ - } - } +} +} /* wait for the start of the next frame. */ - mstp_port->receive_state = - MSTP_RECEIVE_STATE_IDLE; - } - } - } +mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE;} +} +} /* not per MS/TP standard, but it is a case not covered */ - else { - mstp_port->ReceiveError = false; - /* indicate that an error has occurred during */ - /* the reception of a frame */ - mstp_port->ReceivedInvalidFrame = true; +else { +mstp_port->ReceiveError = false; +/* indicate that an error has occurred during */ +/* the reception of a frame */ +mstp_port->ReceivedInvalidFrame = true; #if PRINT_ENABLED_RECEIVE_ERRORS - fprintf(stderr,"MSTP: Rx Data: BadIndex %d\n", - mstp_port->Index); +fprintf(stderr, "MSTP: Rx Data: BadIndex %d\n", mstp_port->Index); #endif - /* wait for the start of a frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } - mstp_port->SilenceTimerReset(); - INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); - mstp_port->DataAvailable = false; - } - break; - /* In the HEADER_CRC state, the node validates the CRC on the fixed */ - /* message header. */ - case MSTP_RECEIVE_STATE_HEADER_CRC: +/* wait for the start of a frame. */ +mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE;} +mstp_port->SilenceTimerReset(); +INCREMENT_AND_LIMIT_UINT8(mstp_port->EventCount); mstp_port->DataAvailable = false;} +break; +/* In the HEADER_CRC state, the node validates the CRC on the fixed */ +/* message header. */ +case MSTP_RECEIVE_STATE_HEADER_CRC: /* note: we should never get to this state since we shortcut it earlier in the state machine, and never set this state */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - break; - /* In the DATA state, the node waits for the data portion of a frame. */ - case MSTP_RECEIVE_STATE_DATA: +mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; break; +/* In the DATA state, the node waits for the data portion of a frame. */ +case MSTP_RECEIVE_STATE_DATA: /* Timeout */ - if (mstp_port->SilenceTimer() > Tframe_abort) { +if (mstp_port->SilenceTimer() > Tframe_abort) { /* indicate that an error has occurred during the reception of a frame */ - mstp_port->ReceivedInvalidFrame = true; +mstp_port->ReceivedInvalidFrame = true; #if PRINT_ENABLED_RECEIVE_ERRORS - fprintf(stderr,"MSTP: Rx Data: SilenceTimer %d > %d\n", - mstp_port->SilenceTimer(), Tframe_abort); +fprintf(stderr, "MSTP: Rx Data: SilenceTimer %d > %d\n", +mstp_port->SilenceTimer(), Tframe_abort); #endif - /* wait for the start of the next frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } +/* wait for the start of the next frame. */ +mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE;} /* Error */ - else if (mstp_port->ReceiveError == true) { - mstp_port->ReceiveError = false; - mstp_port->SilenceTimerReset(); - /* indicate that an error has occurred during the reception of a frame */ - mstp_port->ReceivedInvalidFrame = true; +else +if (mstp_port->ReceiveError == true) { +mstp_port->ReceiveError = false; mstp_port->SilenceTimerReset(); +/* indicate that an error has occurred during the reception of a frame */ +mstp_port->ReceivedInvalidFrame = true; #if PRINT_ENABLED_RECEIVE_ERRORS - fprintf(stderr,"MSTP: Rx Data: ReceiveError\n"); +fprintf(stderr, "MSTP: Rx Data: ReceiveError\n"); #endif - /* wait for the start of the next frame. */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } else if (mstp_port->DataAvailable == true) { +/* wait for the start of the next frame. */ +mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE;} +else +if (mstp_port->DataAvailable == true) { #if PRINT_ENABLED_RECEIVE_DATA - fprintf(stderr, "%02X ", mstp_port->DataRegister); +fprintf(stderr, "%02X ", mstp_port->DataRegister); #endif - /* DataOctet */ - if (mstp_port->Index < mstp_port->DataLength) { - mstp_port->DataCRC = CRC_Calc_Data(mstp_port->DataRegister, - mstp_port->DataCRC); - mstp_port->InputBuffer[mstp_port->Index] = - mstp_port->DataRegister; - mstp_port->Index++; - mstp_port->receive_state = MSTP_RECEIVE_STATE_DATA; - } +/* DataOctet */ +if (mstp_port->Index < mstp_port->DataLength) { +mstp_port->DataCRC = CRC_Calc_Data(mstp_port->DataRegister, +mstp_port->DataCRC); +mstp_port->InputBuffer[mstp_port->Index] = +mstp_port->DataRegister; +mstp_port->Index++; mstp_port->receive_state = MSTP_RECEIVE_STATE_DATA;} /* CRC1 */ - else if (mstp_port->Index == mstp_port->DataLength) { - mstp_port->DataCRC = CRC_Calc_Data(mstp_port->DataRegister, - mstp_port->DataCRC); - mstp_port->DataCRCActualMSB = mstp_port->DataRegister; - mstp_port->Index++; - mstp_port->receive_state = MSTP_RECEIVE_STATE_DATA; - } +else +if (mstp_port->Index == mstp_port->DataLength) { +mstp_port->DataCRC = CRC_Calc_Data(mstp_port->DataRegister, +mstp_port->DataCRC); +mstp_port->DataCRCActualMSB = mstp_port->DataRegister; +mstp_port->Index++; mstp_port->receive_state = MSTP_RECEIVE_STATE_DATA;} /* CRC2 */ - else if (mstp_port->Index == (mstp_port->DataLength + 1)) { - mstp_port->DataCRC = CRC_Calc_Data(mstp_port->DataRegister, - mstp_port->DataCRC); - mstp_port->DataCRCActualLSB = mstp_port->DataRegister; +else +if (mstp_port->Index == (mstp_port->DataLength + 1)) { +mstp_port->DataCRC = CRC_Calc_Data(mstp_port->DataRegister, +mstp_port->DataCRC); mstp_port->DataCRCActualLSB = mstp_port->DataRegister; #if PRINT_ENABLED_RECEIVE_DATA - fprintf(stderr, "%s", - mstptext_frame_type(mstp_port->FrameType)); +fprintf(stderr, "%s", mstptext_frame_type(mstp_port->FrameType)); #endif - /* STATE DATA CRC - no need for new state */ - /* indicate the complete reception of a valid frame */ - if (mstp_port->DataCRC == 0xF0B8) { - if ((mstp_port->DestinationAddress == - mstp_port->This_Station) - || (mstp_port->DestinationAddress == - MSTP_BROADCAST_ADDRESS) - || (mstp_port->Lurking)) { +/* STATE DATA CRC - no need for new state */ +/* indicate the complete reception of a valid frame */ +if (mstp_port->DataCRC == 0xF0B8) { +if ((mstp_port->DestinationAddress == mstp_port->This_Station) +|| (mstp_port->DestinationAddress == MSTP_BROADCAST_ADDRESS) +|| (mstp_port->Lurking)) { /* ForUs */ /* indicate that a frame with no data has been received */ - mstp_port->ReceivedValidFrame = true; - } else { +mstp_port->ReceivedValidFrame = true;} +else { /* NotForUs - drop */ - } - } - else { - mstp_port->ReceivedInvalidFrame = true; -#if PRINT_ENABLED_RECEIVE_ERRORS - fprintf(stderr,"MSTP: Rx Data: BadCRC [%02X]\n", - mstp_port->DataRegister); -#endif - } - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - } - mstp_port->DataAvailable = false; - mstp_port->SilenceTimerReset(); - } - break; - default: - /* shouldn't get here - but if we do... */ - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - break; - } -#if PRINT_ENABLED_RECEIVE_DATA - if ((receive_state != MSTP_RECEIVE_STATE_IDLE) && - (mstp_port->receive_state == MSTP_RECEIVE_STATE_IDLE)) { - fprintf(stderr, "\n"); - fflush(stderr); - } - receive_state = mstp_port->receive_state; -#endif - - return; } +} +else { +mstp_port->ReceivedInvalidFrame = true; +#if PRINT_ENABLED_RECEIVE_ERRORS +fprintf(stderr, "MSTP: Rx Data: BadCRC [%02X]\n", mstp_port->DataRegister); +#endif +} +mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE;} +mstp_port->DataAvailable = false; mstp_port->SilenceTimerReset();} +break; default: + /* shouldn't get here - but if we do... */ + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; break;} +#if PRINT_ENABLED_RECEIVE_DATA + if ((receive_state != MSTP_RECEIVE_STATE_IDLE) && + (mstp_port->receive_state == MSTP_RECEIVE_STATE_IDLE)) { + fprintf(stderr, "\n"); fflush(stderr);} + receive_state = mstp_port->receive_state; +#endif + return;} /* returns true if we need to transition immediately */ -bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t * mstp_port) -{ - unsigned length = 0; - uint8_t next_poll_station = 0; - uint8_t next_this_station = 0; - uint8_t next_next_station = 0; - uint16_t my_timeout = 10, ns_timeout = 0; - /* transition immediately to the next state */ - bool transition_now = false; + bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t * mstp_port) { + unsigned length = 0; + uint8_t next_poll_station = 0; + uint8_t next_this_station = 0; + uint8_t next_next_station = 0; + uint16_t my_timeout = 10, ns_timeout = 0; + /* transition immediately to the next state */ + bool transition_now = false; #if PRINT_ENABLED_MASTER - static MSTP_MASTER_STATE master_state = MSTP_MASTER_STATE_INITIALIZE; + static MSTP_MASTER_STATE master_state = MSTP_MASTER_STATE_INITIALIZE; #endif - - /* some calculations that several states need */ - next_poll_station = (mstp_port->Poll_Station + 1) % + /* some calculations that several states need */ + next_poll_station = (mstp_port->Poll_Station + 1) % (mstp_port->Nmax_master + 1); - next_this_station = (mstp_port->This_Station + 1) % + next_this_station = (mstp_port->This_Station + 1) % (mstp_port->Nmax_master + 1); - next_next_station = (mstp_port->Next_Station + 1) % + next_next_station = (mstp_port->Next_Station + 1) % (mstp_port->Nmax_master + 1); #if PRINT_ENABLED_MASTER - if (mstp_port->master_state != master_state) { + if (mstp_port->master_state != master_state) { master_state = mstp_port->master_state; - fprintf(stderr, - "MSTP: TS=%02X[%02X] NS=%02X[%02X] PS=%02X[%02X] EC=%u TC=%u ST=%u %s\n", - mstp_port->This_Station, - next_this_station, - mstp_port->Next_Station, - next_next_station, - mstp_port->Poll_Station, - next_poll_station, - mstp_port->EventCount, - mstp_port->TokenCount, - mstp_port->SilenceTimer(), - mstptext_master_state(mstp_port->master_state)); - } + fprintf(stderr, + "MSTP: TS=%02X[%02X] NS=%02X[%02X] PS=%02X[%02X] EC=%u TC=%u ST=%u %s\n", + mstp_port->This_Station, + next_this_station, + mstp_port->Next_Station, + next_next_station, + mstp_port->Poll_Station, + next_poll_station, + mstp_port->EventCount, + mstp_port->TokenCount, + mstp_port->SilenceTimer(), + mstptext_master_state(mstp_port->master_state));} #endif - switch (mstp_port->master_state) { - case MSTP_MASTER_STATE_INITIALIZE: - /* DoneInitializing */ - /* indicate that the next station is unknown */ - mstp_port->Next_Station = mstp_port->This_Station; - mstp_port->Poll_Station = mstp_port->This_Station; - /* cause a Poll For Master to be sent when this node first */ - /* receives the token */ - mstp_port->TokenCount = Npoll; - mstp_port->SoleMaster = false; - mstp_port->master_state = MSTP_MASTER_STATE_IDLE; - transition_now = true; - break; - /* In the IDLE state, the node waits for a frame. */ - case MSTP_MASTER_STATE_IDLE: - /* LostToken */ - if (mstp_port->SilenceTimer() >= Tno_token) { - /* assume that the token has been lost */ - mstp_port->EventCount = 0; /* Addendum 135-2004d-8 */ + switch (mstp_port->master_state) { +case MSTP_MASTER_STATE_INITIALIZE: + /* DoneInitializing */ + /* indicate that the next station is unknown */ + mstp_port->Next_Station = mstp_port->This_Station; + mstp_port->Poll_Station = mstp_port->This_Station; + /* cause a Poll For Master to be sent when this node first */ + /* receives the token */ + mstp_port->TokenCount = Npoll; + mstp_port->SoleMaster = false; + mstp_port->master_state = MSTP_MASTER_STATE_IDLE; + transition_now = true; break; + /* In the IDLE state, the node waits for a frame. */ +case MSTP_MASTER_STATE_IDLE: + /* LostToken */ + if (mstp_port->SilenceTimer() >= Tno_token) { + /* assume that the token has been lost */ + mstp_port->EventCount = 0; /* Addendum 135-2004d-8 */ mstp_port->master_state = MSTP_MASTER_STATE_NO_TOKEN; - transition_now = true; - } - /* ReceivedInvalidFrame */ - else if (mstp_port->ReceivedInvalidFrame == true) { + transition_now = true;} + /* ReceivedInvalidFrame */ + else + if (mstp_port->ReceivedInvalidFrame == true) { /* invalid frame was received */ mstp_port->ReceivedInvalidFrame = false; /* wait for the next frame - remain in IDLE */ - } else if (mstp_port->ReceivedValidFrame == true) { + } + else + if (mstp_port->ReceivedValidFrame == true) { #if PRINT_ENABLED_MASTER - fprintf(stderr, - "MSTP: ReceivedValidFrame Src=%02X Dest=%02X DataLen=%u FC=%u ST=%u Type=%s\n", - mstp_port->SourceAddress, - mstp_port->DestinationAddress, - mstp_port->DataLength, - mstp_port->FrameCount, - mstp_port->SilenceTimer(), - mstptext_frame_type(mstp_port->FrameType)); + fprintf(stderr, + "MSTP: ReceivedValidFrame Src=%02X Dest=%02X DataLen=%u FC=%u ST=%u Type=%s\n", + mstp_port->SourceAddress, + mstp_port->DestinationAddress, + mstp_port->DataLength, + mstp_port->FrameCount, + mstp_port->SilenceTimer(), + mstptext_frame_type(mstp_port->FrameType)); #endif - /* destined for me! */ - if ((mstp_port->DestinationAddress == - mstp_port->This_Station) || - (mstp_port->DestinationAddress == - MSTP_BROADCAST_ADDRESS)) { - switch (mstp_port->FrameType) { - /* ReceivedToken */ - case FRAME_TYPE_TOKEN: - /* tokens can't be broadcast */ - if (mstp_port->DestinationAddress == - MSTP_BROADCAST_ADDRESS) - break; - mstp_port->ReceivedValidFrame = false; - mstp_port->FrameCount = 0; - mstp_port->SoleMaster = false; - mstp_port->master_state = MSTP_MASTER_STATE_USE_TOKEN; - transition_now = true; - break; - /* ReceivedPFM */ - case FRAME_TYPE_POLL_FOR_MASTER: - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER, - mstp_port->SourceAddress, mstp_port->This_Station, - NULL, 0); - break; - case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: - /* indicate successful reception to the higher layers */ - MSTP_Put_Receive(mstp_port); - break; - case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: - /*mstp_port->ReplyPostponedTimer = 0; */ - /* indicate successful reception to the higher layers */ - MSTP_Put_Receive(mstp_port); - /* broadcast DER just remains IDLE */ - if (mstp_port->DestinationAddress != - MSTP_BROADCAST_ADDRESS) { - mstp_port->master_state = - MSTP_MASTER_STATE_ANSWER_DATA_REQUEST; - } - break; - case FRAME_TYPE_TEST_REQUEST: - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_TEST_RESPONSE, - mstp_port->SourceAddress, mstp_port->This_Station, - NULL, 0); - break; - case FRAME_TYPE_TEST_RESPONSE: - default: - break; - } - } - /* For DATA_EXPECTING_REPLY, we will keep the Rx Frame for - reference, and the flag will be cleared in the next state */ - if (mstp_port->master_state != - MSTP_MASTER_STATE_ANSWER_DATA_REQUEST) { + /* destined for me! */ + if ((mstp_port->DestinationAddress == + mstp_port->This_Station) || + (mstp_port->DestinationAddress == MSTP_BROADCAST_ADDRESS)) { + switch (mstp_port->FrameType) { + /* ReceivedToken */ +case FRAME_TYPE_TOKEN: + /* tokens can't be broadcast */ + if (mstp_port->DestinationAddress == MSTP_BROADCAST_ADDRESS) + break; mstp_port->ReceivedValidFrame = false; - } - } - break; - /* In the USE_TOKEN state, the node is allowed to send one or */ - /* more data frames. These may be BACnet Data frames or */ - /* proprietary frames. */ - case MSTP_MASTER_STATE_USE_TOKEN: - /* FIXME: We could wait for up to Tusage_delay */ - length = MSTP_Get_Send(mstp_port, 0); - if (length < 1) { - /* NothingToSend */ - mstp_port->FrameCount = mstp_port->Nmax_info_frames; - mstp_port->master_state = MSTP_MASTER_STATE_DONE_WITH_TOKEN; - transition_now = true; - } else { - uint8_t frame_type = mstp_port->OutputBuffer[2]; - uint8_t destination = mstp_port->OutputBuffer[3]; - RS485_Send_Frame(mstp_port, - (uint8_t *) & mstp_port->OutputBuffer[0], length); - mstp_port->FrameCount++; - switch (frame_type) { - case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: - /* SendAndWait */ - if (destination == MSTP_BROADCAST_ADDRESS) - mstp_port->master_state = - MSTP_MASTER_STATE_DONE_WITH_TOKEN; - else - mstp_port->master_state = - MSTP_MASTER_STATE_WAIT_FOR_REPLY; - break; - case FRAME_TYPE_TEST_REQUEST: - mstp_port->master_state = MSTP_MASTER_STATE_WAIT_FOR_REPLY; - break; - case FRAME_TYPE_TEST_RESPONSE: - case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: - default: - /* SendNoWait */ - mstp_port->master_state = - MSTP_MASTER_STATE_DONE_WITH_TOKEN; - break; - } - } - break; - /* In the WAIT_FOR_REPLY state, the node waits for */ - /* a reply from another node. */ - case MSTP_MASTER_STATE_WAIT_FOR_REPLY: - if (mstp_port->SilenceTimer() >= Treply_timeout) { - /* ReplyTimeout */ - /* assume that the request has failed */ - mstp_port->FrameCount = mstp_port->Nmax_info_frames; - mstp_port->master_state = MSTP_MASTER_STATE_DONE_WITH_TOKEN; - /* Any retry of the data frame shall await the next entry */ - /* to the USE_TOKEN state. (Because of the length of the timeout, */ - /* this transition will cause the token to be passed regardless */ - /* of the initial value of FrameCount.) */ - transition_now = true; - } else { - if (mstp_port->ReceivedInvalidFrame == true) { - /* InvalidFrame */ - /* error in frame reception */ - mstp_port->ReceivedInvalidFrame = false; - mstp_port->master_state = - MSTP_MASTER_STATE_DONE_WITH_TOKEN; - transition_now = true; - } else if (mstp_port->ReceivedValidFrame == true) { - if (mstp_port->DestinationAddress == - mstp_port->This_Station) { - switch (mstp_port->FrameType) { - case FRAME_TYPE_REPLY_POSTPONED: - /* ReceivedReplyPostponed */ - mstp_port->master_state = - MSTP_MASTER_STATE_DONE_WITH_TOKEN; - break; - case FRAME_TYPE_TEST_RESPONSE: - mstp_port->master_state = MSTP_MASTER_STATE_IDLE; - break; - case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: - /* ReceivedReply */ - /* or a proprietary type that indicates a reply */ - /* indicate successful reception to the higher layers */ - MSTP_Put_Receive(mstp_port); - mstp_port->master_state = - MSTP_MASTER_STATE_DONE_WITH_TOKEN; - break; - default: - /* if proprietary frame was expected, you might - need to transition to DONE WITH TOKEN */ - mstp_port->master_state = MSTP_MASTER_STATE_IDLE; - break; - } - } else { - /* ReceivedUnexpectedFrame */ - /* an unexpected frame was received */ - /* This may indicate the presence of multiple tokens. */ - /* Synchronize with the network. */ - /* This action drops the token. */ - mstp_port->master_state = MSTP_MASTER_STATE_IDLE; - } - mstp_port->ReceivedValidFrame = false; - transition_now = true; - } - } - break; - /* The DONE_WITH_TOKEN state either sends another data frame, */ - /* passes the token, or initiates a Poll For Master cycle. */ - case MSTP_MASTER_STATE_DONE_WITH_TOKEN: - /* SendAnotherFrame */ - if (mstp_port->FrameCount < mstp_port->Nmax_info_frames) { - /* then this node may send another information frame */ - /* before passing the token. */ - mstp_port->master_state = MSTP_MASTER_STATE_USE_TOKEN; - transition_now = true; - } - /* Npoll changed in Errata SSPC-135-2004 */ - else if (mstp_port->TokenCount < (Npoll - 1)) { - if ((mstp_port->SoleMaster == true) && - (mstp_port->Next_Station != next_this_station)) { - /* SoleMaster */ - /* there are no other known master nodes to */ - /* which the token may be sent (true master-slave operation). */ mstp_port->FrameCount = 0; - mstp_port->TokenCount++; + mstp_port->SoleMaster = false; mstp_port->master_state = MSTP_MASTER_STATE_USE_TOKEN; - transition_now = true; - } else { - /* SendToken */ - /* Npoll changed in Errata SSPC-135-2004 */ - /* The comparison of NS and TS+1 eliminates the Poll For Master */ - /* if there are no addresses between TS and NS, since there is no */ - /* address at which a new master node may be found in that case. */ - mstp_port->TokenCount++; - /* transmit a Token frame to NS */ - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_TOKEN, - mstp_port->Next_Station, - mstp_port->This_Station, NULL, 0); - mstp_port->RetryCount = 0; - mstp_port->EventCount = 0; - mstp_port->master_state = MSTP_MASTER_STATE_PASS_TOKEN; - } - } else if (next_poll_station == mstp_port->Next_Station) { - if (mstp_port->SoleMaster == true) { - /* SoleMasterRestartMaintenancePFM */ - mstp_port->Poll_Station = next_next_station; - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_POLL_FOR_MASTER, mstp_port->Poll_Station, - mstp_port->This_Station, NULL, 0); - /* no known successor node */ - mstp_port->Next_Station = mstp_port->This_Station; - mstp_port->RetryCount = 0; - mstp_port->TokenCount = 1; /* changed in Errata SSPC-135-2004 */ - /* mstp_port->EventCount = 0; removed in Addendum 135-2004d-8 */ - /* find a new successor to TS */ + transition_now = true; break; + /* ReceivedPFM */ +case FRAME_TYPE_POLL_FOR_MASTER: +MSTP_Create_And_Send_Frame(mstp_port, FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER, mstp_port->SourceAddress, mstp_port->This_Station, NULL, 0); break; case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: + /* indicate successful reception to the higher layers */ +MSTP_Put_Receive(mstp_port); break; case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: + /*mstp_port->ReplyPostponedTimer = 0; */ + /* indicate successful reception to the higher layers */ + MSTP_Put_Receive(mstp_port); + /* broadcast DER just remains IDLE */ + if (mstp_port->DestinationAddress != MSTP_BROADCAST_ADDRESS) { mstp_port->master_state = - MSTP_MASTER_STATE_POLL_FOR_MASTER; - } else { - /* ResetMaintenancePFM */ - mstp_port->Poll_Station = mstp_port->This_Station; - /* transmit a Token frame to NS */ - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_TOKEN, - mstp_port->Next_Station, - mstp_port->This_Station, NULL, 0); - mstp_port->RetryCount = 0; - mstp_port->TokenCount = 1; /* changed in Errata SSPC-135-2004 */ - mstp_port->EventCount = 0; - mstp_port->master_state = MSTP_MASTER_STATE_PASS_TOKEN; - } - } else { - /* SendMaintenancePFM */ - mstp_port->Poll_Station = next_poll_station; - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_POLL_FOR_MASTER, - mstp_port->Poll_Station, mstp_port->This_Station, NULL, 0); - mstp_port->RetryCount = 0; - mstp_port->master_state = MSTP_MASTER_STATE_POLL_FOR_MASTER; + MSTP_MASTER_STATE_ANSWER_DATA_REQUEST;} +break; case FRAME_TYPE_TEST_REQUEST: +MSTP_Create_And_Send_Frame(mstp_port, FRAME_TYPE_TEST_RESPONSE, mstp_port->SourceAddress, mstp_port->This_Station, NULL, 0); break; case FRAME_TYPE_TEST_RESPONSE: +default: + break;} } - break; - /* The PASS_TOKEN state listens for a successor to begin using */ - /* the token that this node has just attempted to pass. */ - case MSTP_MASTER_STATE_PASS_TOKEN: - if (mstp_port->SilenceTimer() <= Tusage_timeout) { - if (mstp_port->EventCount > Nmin_octets) { + /* For DATA_EXPECTING_REPLY, we will keep the Rx Frame for + reference, and the flag will be cleared in the next state */ + if (mstp_port->master_state != MSTP_MASTER_STATE_ANSWER_DATA_REQUEST) { + mstp_port->ReceivedValidFrame = false;} + } + break; + /* In the USE_TOKEN state, the node is allowed to send one or */ + /* more data frames. These may be BACnet Data frames or */ + /* proprietary frames. */ +case MSTP_MASTER_STATE_USE_TOKEN: + /* FIXME: We could wait for up to Tusage_delay */ + length = MSTP_Get_Send(mstp_port, 0); if (length < 1) { + /* NothingToSend */ + mstp_port->FrameCount = mstp_port->Nmax_info_frames; + mstp_port->master_state = MSTP_MASTER_STATE_DONE_WITH_TOKEN; + transition_now = true;} + else { + uint8_t frame_type = mstp_port->OutputBuffer[2]; + uint8_t destination = mstp_port->OutputBuffer[3]; + RS485_Send_Frame(mstp_port, + (uint8_t *) & mstp_port->OutputBuffer[0], length); + mstp_port->FrameCount++; switch (frame_type) { +case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: + /* SendAndWait */ + if (destination == MSTP_BROADCAST_ADDRESS) + mstp_port->master_state = MSTP_MASTER_STATE_DONE_WITH_TOKEN; + else +mstp_port->master_state = MSTP_MASTER_STATE_WAIT_FOR_REPLY; break; case FRAME_TYPE_TEST_REQUEST: +mstp_port->master_state = MSTP_MASTER_STATE_WAIT_FOR_REPLY; break; case FRAME_TYPE_TEST_RESPONSE: +case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: +default: + /* SendNoWait */ + mstp_port->master_state = MSTP_MASTER_STATE_DONE_WITH_TOKEN; break;} + } + break; + /* In the WAIT_FOR_REPLY state, the node waits for */ + /* a reply from another node. */ +case MSTP_MASTER_STATE_WAIT_FOR_REPLY: + if (mstp_port->SilenceTimer() >= Treply_timeout) { + /* ReplyTimeout */ + /* assume that the request has failed */ + mstp_port->FrameCount = mstp_port->Nmax_info_frames; + mstp_port->master_state = MSTP_MASTER_STATE_DONE_WITH_TOKEN; + /* Any retry of the data frame shall await the next entry */ + /* to the USE_TOKEN state. (Because of the length of the timeout, */ + /* this transition will cause the token to be passed regardless */ + /* of the initial value of FrameCount.) */ + transition_now = true;} + else { + if (mstp_port->ReceivedInvalidFrame == true) { + /* InvalidFrame */ + /* error in frame reception */ + mstp_port->ReceivedInvalidFrame = false; + mstp_port->master_state = + MSTP_MASTER_STATE_DONE_WITH_TOKEN; transition_now = true;} + else + if (mstp_port->ReceivedValidFrame == true) { + if (mstp_port->DestinationAddress == mstp_port->This_Station) { + switch (mstp_port->FrameType) { +case FRAME_TYPE_REPLY_POSTPONED: + /* ReceivedReplyPostponed */ +mstp_port->master_state = MSTP_MASTER_STATE_DONE_WITH_TOKEN; break; case FRAME_TYPE_TEST_RESPONSE: +mstp_port->master_state = MSTP_MASTER_STATE_IDLE; break; case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: + /* ReceivedReply */ + /* or a proprietary type that indicates a reply */ + /* indicate successful reception to the higher layers */ +MSTP_Put_Receive(mstp_port); mstp_port->master_state = MSTP_MASTER_STATE_DONE_WITH_TOKEN; break; default: + /* if proprietary frame was expected, you might + need to transition to DONE WITH TOKEN */ + mstp_port->master_state = MSTP_MASTER_STATE_IDLE; break;} + } + else { + /* ReceivedUnexpectedFrame */ + /* an unexpected frame was received */ + /* This may indicate the presence of multiple tokens. */ + /* Synchronize with the network. */ + /* This action drops the token. */ + mstp_port->master_state = MSTP_MASTER_STATE_IDLE;} + mstp_port->ReceivedValidFrame = false; transition_now = true;} + } + break; + /* The DONE_WITH_TOKEN state either sends another data frame, */ + /* passes the token, or initiates a Poll For Master cycle. */ +case MSTP_MASTER_STATE_DONE_WITH_TOKEN: + /* SendAnotherFrame */ + if (mstp_port->FrameCount < mstp_port->Nmax_info_frames) { + /* then this node may send another information frame */ + /* before passing the token. */ + mstp_port->master_state = MSTP_MASTER_STATE_USE_TOKEN; + transition_now = true;} + /* Npoll changed in Errata SSPC-135-2004 */ + else +if (mstp_port->TokenCount < (Npoll - 1)) { + if ((mstp_port->SoleMaster == true) && + (mstp_port->Next_Station != next_this_station)) { + /* SoleMaster */ + /* there are no other known master nodes to */ + /* which the token may be sent (true master-slave operation). */ + mstp_port->FrameCount = 0; + mstp_port->TokenCount++; + mstp_port->master_state = MSTP_MASTER_STATE_USE_TOKEN; + transition_now = true;} + else { + /* SendToken */ + /* Npoll changed in Errata SSPC-135-2004 */ + /* The comparison of NS and TS+1 eliminates the Poll For Master */ + /* if there are no addresses between TS and NS, since there is no */ + /* address at which a new master node may be found in that case. */ + mstp_port->TokenCount++; + /* transmit a Token frame to NS */ + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_TOKEN, + mstp_port->Next_Station, + mstp_port->This_Station, NULL, 0); + mstp_port->RetryCount = 0; + mstp_port->EventCount = 0; + mstp_port->master_state = MSTP_MASTER_STATE_PASS_TOKEN;} +} +else +if (next_poll_station == mstp_port->Next_Station) { +if (mstp_port->SoleMaster == true) { + /* SoleMasterRestartMaintenancePFM */ + mstp_port->Poll_Station = next_next_station; + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_POLL_FOR_MASTER, mstp_port->Poll_Station, + mstp_port->This_Station, NULL, 0); + /* no known successor node */ + mstp_port->Next_Station = mstp_port->This_Station; mstp_port->RetryCount = 0; mstp_port->TokenCount = 1; /* changed in Errata SSPC-135-2004 */ + /* mstp_port->EventCount = 0; removed in Addendum 135-2004d-8 */ + /* find a new successor to TS */ +mstp_port->master_state = MSTP_MASTER_STATE_POLL_FOR_MASTER;} +else { + /* ResetMaintenancePFM */ + mstp_port->Poll_Station = mstp_port->This_Station; + /* transmit a Token frame to NS */ + MSTP_Create_And_Send_Frame(mstp_port, FRAME_TYPE_TOKEN, mstp_port->Next_Station, mstp_port->This_Station, NULL, 0); mstp_port->RetryCount = 0; mstp_port->TokenCount = 1; /* changed in Errata SSPC-135-2004 */ +mstp_port->EventCount = 0; + mstp_port->master_state = MSTP_MASTER_STATE_PASS_TOKEN;} +} +else { + /* SendMaintenancePFM */ +mstp_port->Poll_Station = next_poll_station; +MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_POLL_FOR_MASTER, + mstp_port->Poll_Station, mstp_port->This_Station, NULL, 0); +mstp_port->RetryCount = 0; +mstp_port->master_state = MSTP_MASTER_STATE_POLL_FOR_MASTER;} +break; +/* The PASS_TOKEN state listens for a successor to begin using */ +/* the token that this node has just attempted to pass. */ +case MSTP_MASTER_STATE_PASS_TOKEN: +if (mstp_port->SilenceTimer() <= Tusage_timeout) { +if (mstp_port->EventCount > Nmin_octets) { /* SawTokenUser */ /* Assume that a frame has been sent by the new token user. */ /* Enter the IDLE state to process the frame. */ - mstp_port->master_state = MSTP_MASTER_STATE_IDLE; - transition_now = true; - } - } else { - if (mstp_port->RetryCount < Nretry_token) { +mstp_port->master_state = MSTP_MASTER_STATE_IDLE; transition_now = true;} +} +else { +if (mstp_port->RetryCount < Nretry_token) { /* RetrySendToken */ - mstp_port->RetryCount++; - /* Transmit a Token frame to NS */ - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_TOKEN, - mstp_port->Next_Station, mstp_port->This_Station, NULL, - 0); - mstp_port->EventCount = 0; - /* re-enter the current state to listen for NS */ - /* to begin using the token. */ - } else { + mstp_port->RetryCount++; + /* Transmit a Token frame to NS */ + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_TOKEN, + mstp_port->Next_Station, mstp_port->This_Station, NULL, + 0); mstp_port->EventCount = 0; + /* re-enter the current state to listen for NS */ + /* to begin using the token. */ +} +else { /* FindNewSuccessor */ /* Assume that NS has failed. */ - mstp_port->Poll_Station = next_next_station; - /* Transmit a Poll For Master frame to PS. */ - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_POLL_FOR_MASTER, - mstp_port->Poll_Station, mstp_port->This_Station, NULL, - 0); - /* no known successor node */ - mstp_port->Next_Station = mstp_port->This_Station; - mstp_port->RetryCount = 0; - mstp_port->TokenCount = 0; - /* mstp_port->EventCount = 0; removed in Addendum 135-2004d-8 */ - /* find a new successor to TS */ - mstp_port->master_state = - MSTP_MASTER_STATE_POLL_FOR_MASTER; - } - } - break; - /* The NO_TOKEN state is entered if mstp_port->SilenceTimer() becomes greater */ - /* than Tno_token, indicating that there has been no network activity */ - /* for that period of time. The timeout is continued to determine */ - /* whether or not this node may create a token. */ - case MSTP_MASTER_STATE_NO_TOKEN: - my_timeout = Tno_token + (Tslot * mstp_port->This_Station); - if (mstp_port->SilenceTimer() < my_timeout) { - if (mstp_port->EventCount > Nmin_octets) { + mstp_port->Poll_Station = next_next_station; + /* Transmit a Poll For Master frame to PS. */ + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_POLL_FOR_MASTER, + mstp_port->Poll_Station, mstp_port->This_Station, NULL, 0); + /* no known successor node */ + mstp_port->Next_Station = mstp_port->This_Station; + mstp_port->RetryCount = 0; mstp_port->TokenCount = 0; + /* mstp_port->EventCount = 0; removed in Addendum 135-2004d-8 */ + /* find a new successor to TS */ +mstp_port->master_state = MSTP_MASTER_STATE_POLL_FOR_MASTER;} +} +break; +/* The NO_TOKEN state is entered if mstp_port->SilenceTimer() becomes greater */ +/* than Tno_token, indicating that there has been no network activity */ +/* for that period of time. The timeout is continued to determine */ +/* whether or not this node may create a token. */ +case MSTP_MASTER_STATE_NO_TOKEN: +my_timeout = Tno_token + (Tslot * mstp_port->This_Station); +if (mstp_port->SilenceTimer() < my_timeout) { +if (mstp_port->EventCount > Nmin_octets) { /* SawFrame */ /* Some other node exists at a lower address. */ /* Enter the IDLE state to receive and process the incoming frame. */ - mstp_port->master_state = MSTP_MASTER_STATE_IDLE; - transition_now = true; - } - } else { - ns_timeout = - Tno_token + (Tslot * (mstp_port->This_Station + 1)); - if (mstp_port->SilenceTimer() < ns_timeout) { +mstp_port->master_state = MSTP_MASTER_STATE_IDLE; transition_now = true;} +} +else { +ns_timeout = +Tno_token + (Tslot * (mstp_port->This_Station + 1)); +if (mstp_port->SilenceTimer() < ns_timeout) { /* GenerateToken */ /* Assume that this node is the lowest numerical address */ /* on the network and is empowered to create a token. */ - mstp_port->Poll_Station = next_this_station; - /* Transmit a Poll For Master frame to PS. */ - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_POLL_FOR_MASTER, - mstp_port->Poll_Station, mstp_port->This_Station, NULL, - 0); - /* indicate that the next station is unknown */ - mstp_port->Next_Station = mstp_port->This_Station; - mstp_port->RetryCount = 0; - mstp_port->TokenCount = 0; - /* mstp_port->EventCount = 0; removed Addendum 135-2004d-8 */ - /* enter the POLL_FOR_MASTER state to find a new successor to TS. */ - mstp_port->master_state = - MSTP_MASTER_STATE_POLL_FOR_MASTER; - } - } - break; - /* In the POLL_FOR_MASTER state, the node listens for a reply to */ - /* a previously sent Poll For Master frame in order to find */ - /* a successor node. */ - case MSTP_MASTER_STATE_POLL_FOR_MASTER: - if (mstp_port->ReceivedValidFrame == true) { - if ((mstp_port->DestinationAddress == mstp_port->This_Station) - && (mstp_port->FrameType == - FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER)) { + mstp_port->Poll_Station = next_this_station; + /* Transmit a Poll For Master frame to PS. */ + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_POLL_FOR_MASTER, + mstp_port->Poll_Station, mstp_port->This_Station, NULL, 0); + /* indicate that the next station is unknown */ + mstp_port->Next_Station = mstp_port->This_Station; + mstp_port->RetryCount = 0; mstp_port->TokenCount = 0; + /* mstp_port->EventCount = 0; removed Addendum 135-2004d-8 */ + /* enter the POLL_FOR_MASTER state to find a new successor to TS. */ +mstp_port->master_state = MSTP_MASTER_STATE_POLL_FOR_MASTER;} +} +break; +/* In the POLL_FOR_MASTER state, the node listens for a reply to */ +/* a previously sent Poll For Master frame in order to find */ +/* a successor node. */ +case MSTP_MASTER_STATE_POLL_FOR_MASTER: +if (mstp_port->ReceivedValidFrame == true) { +if ((mstp_port->DestinationAddress == mstp_port->This_Station) + && (mstp_port->FrameType == FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER)) { /* ReceivedReplyToPFM */ - mstp_port->SoleMaster = false; - mstp_port->Next_Station = mstp_port->SourceAddress; - mstp_port->EventCount = 0; - /* Transmit a Token frame to NS */ - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_TOKEN, - mstp_port->Next_Station, mstp_port->This_Station, NULL, - 0); - mstp_port->Poll_Station = mstp_port->This_Station; - mstp_port->TokenCount = 0; - mstp_port->RetryCount = 0; - mstp_port->master_state = MSTP_MASTER_STATE_PASS_TOKEN; - } else { + mstp_port->SoleMaster = false; + mstp_port->Next_Station = mstp_port->SourceAddress; + mstp_port->EventCount = 0; + /* Transmit a Token frame to NS */ +MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_TOKEN, + mstp_port->Next_Station, mstp_port->This_Station, NULL, + 0); + mstp_port->Poll_Station = mstp_port->This_Station; + mstp_port->TokenCount = 0; + mstp_port->RetryCount = 0; + mstp_port->master_state = MSTP_MASTER_STATE_PASS_TOKEN;} +else { /* ReceivedUnexpectedFrame */ /* An unexpected frame was received. */ /* This may indicate the presence of multiple tokens. */ /* enter the IDLE state to synchronize with the network. */ /* This action drops the token. */ - mstp_port->master_state = MSTP_MASTER_STATE_IDLE; - transition_now = true; - } - mstp_port->ReceivedValidFrame = false; - } else if ((mstp_port->SilenceTimer() > Tusage_timeout) || - (mstp_port->ReceivedInvalidFrame == true)) { - if (mstp_port->SoleMaster == true) { +mstp_port->master_state = MSTP_MASTER_STATE_IDLE; transition_now = true;} +mstp_port->ReceivedValidFrame = false;} +else +if ((mstp_port->SilenceTimer() > Tusage_timeout) || +(mstp_port->ReceivedInvalidFrame == true)) { +if (mstp_port->SoleMaster == true) { /* SoleMaster */ /* There was no valid reply to the periodic poll */ /* by the sole known master for other masters. */ - mstp_port->FrameCount = 0; - /* mstp_port->TokenCount++; removed in 2004 */ - mstp_port->master_state = MSTP_MASTER_STATE_USE_TOKEN; - transition_now = true; - } else { - if (mstp_port->Next_Station != mstp_port->This_Station) { +mstp_port->FrameCount = 0; +/* mstp_port->TokenCount++; removed in 2004 */ +mstp_port->master_state = MSTP_MASTER_STATE_USE_TOKEN; transition_now = true;} +else { +if (mstp_port->Next_Station != mstp_port->This_Station) { /* DoneWithPFM */ /* There was no valid reply to the maintenance */ /* poll for a master at address PS. */ - mstp_port->EventCount = 0; - /* transmit a Token frame to NS */ - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_TOKEN, - mstp_port->Next_Station, mstp_port->This_Station, - NULL, 0); - mstp_port->RetryCount = 0; - mstp_port->master_state = MSTP_MASTER_STATE_PASS_TOKEN; - } else { - if (next_poll_station != mstp_port->This_Station) { - /* SendNextPFM */ - mstp_port->Poll_Station = next_poll_station; - /* Transmit a Poll For Master frame to PS. */ - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_POLL_FOR_MASTER, - mstp_port->Poll_Station, - mstp_port->This_Station, NULL, 0); - mstp_port->RetryCount = 0; - /* Re-enter the current state. */ - } else { - /* DeclareSoleMaster */ - /* to indicate that this station is the only master */ - mstp_port->SoleMaster = true; - mstp_port->FrameCount = 0; - mstp_port->master_state = - MSTP_MASTER_STATE_USE_TOKEN; - transition_now = true; - } - } - } - mstp_port->ReceivedInvalidFrame = false; - } - break; - /* The ANSWER_DATA_REQUEST state is entered when a */ - /* BACnet Data Expecting Reply, a Test_Request, or */ - /* a proprietary frame that expects a reply is received. */ - case MSTP_MASTER_STATE_ANSWER_DATA_REQUEST: + mstp_port->EventCount = 0; + /* transmit a Token frame to NS */ +MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_TOKEN, + mstp_port->Next_Station, mstp_port->This_Station, + NULL, 0); + mstp_port->RetryCount = 0; + mstp_port->master_state = MSTP_MASTER_STATE_PASS_TOKEN;} +else { + if (next_poll_station != mstp_port->This_Station) { + /* SendNextPFM */ + mstp_port->Poll_Station = next_poll_station; + /* Transmit a Poll For Master frame to PS. */ + MSTP_Create_And_Send_Frame(mstp_port, + FRAME_TYPE_POLL_FOR_MASTER, + mstp_port->Poll_Station, + mstp_port->This_Station, NULL, 0); mstp_port->RetryCount = 0; + /* Re-enter the current state. */ + } + else { + /* DeclareSoleMaster */ + /* to indicate that this station is the only master */ + mstp_port->SoleMaster = true; + mstp_port->FrameCount = 0; + mstp_port->master_state = + MSTP_MASTER_STATE_USE_TOKEN; transition_now = true;} +} +} +mstp_port->ReceivedInvalidFrame = false;} +break; +/* The ANSWER_DATA_REQUEST state is entered when a */ +/* BACnet Data Expecting Reply, a Test_Request, or */ +/* a proprietary frame that expects a reply is received. */ +case MSTP_MASTER_STATE_ANSWER_DATA_REQUEST: /* FIXME: we could wait for up to Treply_delay */ - length = MSTP_Get_Reply(mstp_port, 0); - if (length > 0) { +length = MSTP_Get_Reply(mstp_port, 0); if (length > 0) { /* Reply */ /* If a reply is available from the higher layers */ /* within Treply_delay after the reception of the */ @@ -1052,11 +941,10 @@ bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t * mstp_port) /* (the mechanism used to determine this is a local matter), */ /* then call MSTP_Create_And_Send_Frame to transmit the reply frame */ /* and enter the IDLE state to wait for the next frame. */ - RS485_Send_Frame(mstp_port, - (uint8_t *) & mstp_port->OutputBuffer[0], - length); - mstp_port->master_state = MSTP_MASTER_STATE_IDLE; - } else { +RS485_Send_Frame(mstp_port, +(uint8_t *) & mstp_port->OutputBuffer[0], +length); mstp_port->master_state = MSTP_MASTER_STATE_IDLE;} +else { /* DeferredReply */ /* If no reply will be available from the higher layers */ /* within Treply_delay after the reception of the */ @@ -1066,22 +954,16 @@ bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t * mstp_port) /* Any reply shall wait until this node receives the token. */ /* Call MSTP_Create_And_Send_Frame to transmit a Reply Postponed frame, */ /* and enter the IDLE state. */ - MSTP_Create_And_Send_Frame(mstp_port, - FRAME_TYPE_REPLY_POSTPONED, - mstp_port->SourceAddress, - mstp_port->This_Station, NULL, 0); - mstp_port->master_state = MSTP_MASTER_STATE_IDLE; - } +MSTP_Create_And_Send_Frame(mstp_port, +FRAME_TYPE_REPLY_POSTPONED, +mstp_port->SourceAddress, +mstp_port->This_Station, NULL, 0); +mstp_port->master_state = MSTP_MASTER_STATE_IDLE;} /* clear our flag we were holding for comparison */ - mstp_port->ReceivedValidFrame = false; - break; - default: - mstp_port->master_state = MSTP_MASTER_STATE_IDLE; - break; - } +mstp_port->ReceivedValidFrame = false; break; default: + mstp_port->master_state = MSTP_MASTER_STATE_IDLE; break;} - return transition_now; -} + return transition_now;} /* note: This_Station assumed to be set with the MAC address */ /* note: Nmax_info_frames assumed to be set (default=1) */ @@ -1089,50 +971,48 @@ bool MSTP_Master_Node_FSM(volatile struct mstp_port_struct_t * mstp_port) /* note: InputBuffer and InputBufferSize assumed to be set */ /* note: OutputBuffer and OutputBufferSize assumed to be set */ /* note: SilenceTimer and SilenceTimerReset assumed to be set */ -void MSTP_Init(volatile struct mstp_port_struct_t *mstp_port) -{ - if (mstp_port) { - mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; - mstp_port->master_state = MSTP_MASTER_STATE_INITIALIZE; - mstp_port->ReceiveError = false; - mstp_port->DataAvailable = false; - mstp_port->DataRegister = 0; - mstp_port->DataCRC = 0; - mstp_port->DataCRC = 0; - mstp_port->DataLength = 0; - mstp_port->DestinationAddress = 0; - mstp_port->EventCount = 0; - mstp_port->FrameType = FRAME_TYPE_TOKEN; - mstp_port->FrameCount = 0; - mstp_port->HeaderCRC = 0; - mstp_port->Index = 0; - mstp_port->Index = 0; - mstp_port->Next_Station = mstp_port->This_Station; - mstp_port->Poll_Station = mstp_port->This_Station; - mstp_port->ReceivedInvalidFrame = false; - mstp_port->ReceivedValidFrame = false; - mstp_port->RetryCount = 0; - mstp_port->SilenceTimerReset(); - mstp_port->SoleMaster = false; - mstp_port->SourceAddress = 0; - mstp_port->TokenCount = 0; - mstp_port->Lurking = false; + void MSTP_Init(volatile struct mstp_port_struct_t *mstp_port) { + if (mstp_port) { + mstp_port->receive_state = MSTP_RECEIVE_STATE_IDLE; + mstp_port->master_state = MSTP_MASTER_STATE_INITIALIZE; + mstp_port->ReceiveError = false; + mstp_port->DataAvailable = false; + mstp_port->DataRegister = 0; + mstp_port->DataCRC = 0; + mstp_port->DataCRC = 0; + mstp_port->DataLength = 0; + mstp_port->DestinationAddress = 0; + mstp_port->EventCount = 0; + mstp_port->FrameType = FRAME_TYPE_TOKEN; + mstp_port->FrameCount = 0; + mstp_port->HeaderCRC = 0; + mstp_port->Index = 0; + mstp_port->Index = 0; + mstp_port->Next_Station = mstp_port->This_Station; + mstp_port->Poll_Station = mstp_port->This_Station; + mstp_port->ReceivedInvalidFrame = false; + mstp_port->ReceivedValidFrame = false; + mstp_port->RetryCount = 0; + mstp_port->SilenceTimerReset(); + mstp_port->SoleMaster = false; + mstp_port->SourceAddress = 0; + mstp_port->TokenCount = 0; mstp_port->Lurking = false; #if 0 - /* FIXME: you must point these buffers to actual byte buckets - in the dlmstp function before or after calling this init. */ - mstp_port->InputBuffer = &InputBuffer[0]; - mstp_port->InputBufferSize = sizeof(InputBuffer); - mstp_port->OutputBuffer = &OutputBuffer[0]; - mstp_port->OutputBufferSize = sizeof(OutputBuffer); - /* FIXME: these are adjustable, so you must set these in dlmstp */ - mstp_port->Nmax_info_frames = DEFAULT_MAX_INFO_FRAMES; - mstp_port->Nmax_master = DEFAULT_MAX_MASTER; - /* FIXME: point to functions */ - mstp_port->SilenceTimer = Timer_Silence; - mstp_port=>SilenceTimerReset = Timer_Silence_Reset; + /* FIXME: you must point these buffers to actual byte buckets + in the dlmstp function before or after calling this init. */ + mstp_port->InputBuffer = &InputBuffer[0]; + mstp_port->InputBufferSize = sizeof(InputBuffer); + mstp_port->OutputBuffer = &OutputBuffer[0]; + mstp_port->OutputBufferSize = sizeof(OutputBuffer); + /* FIXME: these are adjustable, so you must set these in dlmstp */ + mstp_port->Nmax_info_frames = DEFAULT_MAX_INFO_FRAMES; + mstp_port->Nmax_master = DEFAULT_MAX_MASTER; + /* FIXME: point to functions */ + mstp_port->SilenceTimer = Timer_Silence; + mstp_port = >SilenceTimerReset = Timer_Silence_Reset; #endif + } } -} #ifdef TEST #include @@ -1140,508 +1020,426 @@ void MSTP_Init(volatile struct mstp_port_struct_t *mstp_port) #include "ringbuf.h" #include "ctest.h" -static uint8_t RxBuffer[MAX_MPDU]; -static uint8_t TxBuffer[MAX_MPDU]; - + static uint8_t RxBuffer[MAX_MPDU]; static uint8_t TxBuffer[MAX_MPDU]; /* test stub functions */ -void RS485_Send_Frame(volatile struct mstp_port_struct_t *mstp_port, /* port specific data */ - uint8_t * buffer, /* frame to send (up to 501 bytes of data) */ - uint16_t nbytes) -{ /* number of bytes of data (up to 501) */ - (void) mstp_port; - (void) buffer; - (void) nbytes; -} + void RS485_Send_Frame(volatile struct mstp_port_struct_t *mstp_port, /* port specific data */ + uint8_t * buffer, /* frame to send (up to 501 bytes of data) */ + uint16_t nbytes) { /* number of bytes of data (up to 501) */ + (void) mstp_port; (void) buffer; (void) nbytes;} #define RING_BUFFER_DATA_SIZE 1 #define RING_BUFFER_SIZE MAX_MPDU -static RING_BUFFER Test_Buffer; -static uint8_t Test_Buffer_Data[RING_BUFFER_DATA_SIZE * RING_BUFFER_SIZE]; -static void Load_Input_Buffer(uint8_t * buffer, size_t len) -{ - static bool initialized = false; /* tracks our init */ - - - if (!initialized) { + static RING_BUFFER Test_Buffer; + static uint8_t Test_Buffer_Data[RING_BUFFER_DATA_SIZE * RING_BUFFER_SIZE]; + static void Load_Input_Buffer(uint8_t * buffer, + size_t len) { + static bool initialized = false; /* tracks our init */ + if (!initialized) { initialized = true; - Ringbuf_Init(&Test_Buffer, - (char *) Test_Buffer_Data, - RING_BUFFER_DATA_SIZE, RING_BUFFER_SIZE); - } - /* empty any the existing data */ - while (!Ringbuf_Empty(&Test_Buffer)) { - (void) Ringbuf_Pop_Front(&Test_Buffer); - } + Ringbuf_Init(&Test_Buffer, + (char *) Test_Buffer_Data, + RING_BUFFER_DATA_SIZE, RING_BUFFER_SIZE);} + /* empty any the existing data */ + while (!Ringbuf_Empty(&Test_Buffer)) { + (void) Ringbuf_Pop_Front(&Test_Buffer);} - if (buffer) { - while (len) { + if (buffer) { + while (len) { (void) Ringbuf_Put(&Test_Buffer, (char *) buffer); - len--; - buffer++; + len--; buffer++;} } } -} -void RS485_Check_UART_Data(volatile struct mstp_port_struct_t *mstp_port) -{ /* port specific data */ - char *data; - if (!Ringbuf_Empty(&Test_Buffer) && mstp_port && - (mstp_port->DataAvailable == false)) { - data = Ringbuf_Pop_Front(&Test_Buffer); - if (data) { + void RS485_Check_UART_Data(volatile struct mstp_port_struct_t *mstp_port) { /* port specific data */ + char *data; + if (!Ringbuf_Empty(&Test_Buffer) && mstp_port && + (mstp_port->DataAvailable == false)) { + data = Ringbuf_Pop_Front(&Test_Buffer); if (data) { mstp_port->DataRegister = *data; - mstp_port->DataAvailable = true; + mstp_port->DataAvailable = true;} } } -} -uint16_t MSTP_Put_Receive( - volatile struct mstp_port_struct_t *mstp_port) -{ - return mstp_port->DataLength; -} + uint16_t MSTP_Put_Receive(volatile struct mstp_port_struct_t *mstp_port) { + return mstp_port->DataLength;} /* for the MS/TP state machine to use for getting data to send */ /* Return: amount of PDU data */ -uint16_t MSTP_Get_Send( - volatile struct mstp_port_struct_t *mstp_port, - unsigned timeout) /* milliseconds to wait for a packet */ -{ - return 0; -} + uint16_t MSTP_Get_Send(volatile struct mstp_port_struct_t * mstp_port, unsigned timeout) { /* milliseconds to wait for a packet */ + return 0;} -uint16_t MSTP_Get_Reply( - volatile struct mstp_port_struct_t *mstp_port, - unsigned timeout) /* milliseconds to wait for a packet */ -{ - return 0; -} + uint16_t MSTP_Get_Reply(volatile struct mstp_port_struct_t * mstp_port, unsigned timeout) { /* milliseconds to wait for a packet */ + return 0;} -uint16_t SilenceTime = 0; -static uint16_t Timer_Silence(void) -{ - return SilenceTime; -} -static void Timer_Silence_Reset(void) -{ - SilenceTime = 0; -} + uint16_t SilenceTime = 0; static uint16_t Timer_Silence(void) { + return SilenceTime;} + static void Timer_Silence_Reset(void) { + SilenceTime = 0;} -void testReceiveNodeFSM(Test * pTest) -{ - volatile struct mstp_port_struct_t mstp_port; /* port data */ - unsigned EventCount = 0; /* local counter */ - uint8_t my_mac = 0x05; /* local MAC address */ - uint8_t HeaderCRC = 0; /* for local CRC calculation */ - uint8_t FrameType = 0; /* type of packet that was sent */ - unsigned len; /* used for the size of the message packet */ - size_t i; /* used to loop through the message bytes */ - uint8_t buffer[MAX_MPDU] = { 0 }; - uint8_t data[MAX_PDU] = { 0 }; - - mstp_port.InputBuffer = &RxBuffer[0]; - mstp_port.InputBufferSize = sizeof(RxBuffer); - mstp_port.OutputBuffer = &TxBuffer[0]; - mstp_port.OutputBufferSize = sizeof(TxBuffer); - mstp_port.SilenceTimer = Timer_Silence; - mstp_port.SilenceTimerReset = Timer_Silence_Reset; - mstp_port.This_Station = my_mac; - mstp_port.Nmax_info_frames = 1; - mstp_port.Nmax_master = 127; - MSTP_Init(&mstp_port); - - /* check the receive error during idle */ - mstp_port.receive_state = MSTP_RECEIVE_STATE_IDLE; - mstp_port.ReceiveError = true; - SilenceTime = 255; - mstp_port.EventCount = 0; - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.ReceiveError == false); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); - - /* check for bad packet header */ - mstp_port.DataAvailable = true; - mstp_port.DataRegister = 0x11; - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); - - /* check for good packet header, but timeout */ - mstp_port.DataAvailable = true; - mstp_port.DataRegister = 0x55; - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); - /* force the timeout */ - SilenceTime = Tframe_abort + 1; - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); - - /* check for good packet header preamble, but receive error */ - mstp_port.DataAvailable = true; - mstp_port.DataRegister = 0x55; - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); - /* force the error */ - mstp_port.ReceiveError = true; - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.ReceiveError == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); - - /* check for good packet header preamble1, but bad preamble2 */ - mstp_port.DataAvailable = true; - mstp_port.DataRegister = 0x55; - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); - MSTP_Receive_Frame_FSM(&mstp_port); - /* no change of state if no data yet */ - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); - /* repeated preamble1 */ - mstp_port.DataAvailable = true; - mstp_port.DataRegister = 0x55; - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); - /* repeated preamble1 */ - mstp_port.DataAvailable = true; - mstp_port.DataRegister = 0x55; - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); - /* bad data */ - mstp_port.DataAvailable = true; - mstp_port.DataRegister = 0x11; - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.ReceiveError == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); - - /* check for good packet header preamble, but timeout in packet */ - mstp_port.DataAvailable = true; - mstp_port.DataRegister = 0x55; - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); - MSTP_Receive_Frame_FSM(&mstp_port); - /* preamble2 */ - mstp_port.DataAvailable = true; - mstp_port.DataRegister = 0xFF; - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.Index == 0); - ct_test(pTest, mstp_port.HeaderCRC == 0xFF); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); - /* force the timeout */ - SilenceTime = Tframe_abort + 1; - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); - ct_test(pTest, mstp_port.ReceivedInvalidFrame == true); - - /* check for good packet header preamble, but error in packet */ - mstp_port.DataAvailable = true; - mstp_port.DataRegister = 0x55; - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); - MSTP_Receive_Frame_FSM(&mstp_port); - /* preamble2 */ - mstp_port.DataAvailable = true; - mstp_port.DataRegister = 0xFF; - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.Index == 0); - ct_test(pTest, mstp_port.HeaderCRC == 0xFF); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); - /* force the error */ - mstp_port.ReceiveError = true; - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.ReceiveError == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); - - /* check for good packet header preamble */ - mstp_port.DataAvailable = true; - mstp_port.DataRegister = 0x55; - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); - MSTP_Receive_Frame_FSM(&mstp_port); - /* preamble2 */ - mstp_port.DataAvailable = true; - mstp_port.DataRegister = 0xFF; - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.Index == 0); - ct_test(pTest, mstp_port.HeaderCRC == 0xFF); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); - /* no change of state if no data yet */ - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); - /* Data is received - index is incremented */ - /* FrameType */ - mstp_port.DataAvailable = true; - mstp_port.DataRegister = FRAME_TYPE_TOKEN; - HeaderCRC = 0xFF; - HeaderCRC = CRC_Calc_Header(mstp_port.DataRegister, HeaderCRC); - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.Index == 1); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); - ct_test(pTest, FrameType == FRAME_TYPE_TOKEN); - /* Destination */ - mstp_port.DataAvailable = true; - mstp_port.DataRegister = 0x10; - HeaderCRC = CRC_Calc_Header(mstp_port.DataRegister, HeaderCRC); - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.Index == 2); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); - ct_test(pTest, mstp_port.DestinationAddress == 0x10); - /* Source */ - mstp_port.DataAvailable = true; - mstp_port.DataRegister = my_mac; - HeaderCRC = CRC_Calc_Header(mstp_port.DataRegister, HeaderCRC); - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.Index == 3); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); - ct_test(pTest, mstp_port.SourceAddress == my_mac); - /* Length1 = length*256 */ - mstp_port.DataAvailable = true; - mstp_port.DataRegister = 0; - HeaderCRC = CRC_Calc_Header(mstp_port.DataRegister, HeaderCRC); - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.Index == 4); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); - ct_test(pTest, mstp_port.DataLength == 0); - /* Length2 */ - mstp_port.DataAvailable = true; - mstp_port.DataRegister = 0; - HeaderCRC = CRC_Calc_Header(mstp_port.DataRegister, HeaderCRC); - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.Index == 5); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); - ct_test(pTest, mstp_port.DataLength == 0); - /* HeaderCRC */ - mstp_port.DataAvailable = true; - ct_test(pTest, HeaderCRC == 0x73); /* per Annex G example */ - mstp_port.DataRegister = ~HeaderCRC; /* one's compliment of CRC is sent */ - INCREMENT_AND_LIMIT_UINT8(EventCount); - MSTP_Receive_Frame_FSM(&mstp_port); - ct_test(pTest, mstp_port.DataAvailable == false); - ct_test(pTest, mstp_port.SilenceTimer() == 0); - ct_test(pTest, mstp_port.EventCount == EventCount); - ct_test(pTest, mstp_port.Index == 5); - ct_test(pTest, - mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); - ct_test(pTest, mstp_port.HeaderCRC == 0x55); - /* BadCRC in header check */ - mstp_port.ReceivedInvalidFrame = false; - mstp_port.ReceivedValidFrame = false; - len = MSTP_Create_Frame(buffer, sizeof(buffer), FRAME_TYPE_TOKEN, 0x10, /* destination */ - my_mac, /* source */ - NULL, /* data */ - 0); /* data size */ - ct_test(pTest, len > 0); - /* make the header CRC bad */ - buffer[7] = 0x00; - Load_Input_Buffer(buffer, len); - for (i = 0; i < len; i++) { - RS485_Check_UART_Data(&mstp_port); + void testReceiveNodeFSM(Test * pTest) { + volatile struct mstp_port_struct_t mstp_port; /* port data */ + unsigned EventCount = 0; /* local counter */ + uint8_t my_mac = 0x05; /* local MAC address */ + uint8_t HeaderCRC = 0; /* for local CRC calculation */ + uint8_t FrameType = 0; /* type of packet that was sent */ + unsigned len; /* used for the size of the message packet */ + size_t i; /* used to loop through the message bytes */ + uint8_t buffer[MAX_MPDU] = { + 0}; uint8_t data[MAX_PDU] = { + 0}; + mstp_port.InputBuffer = &RxBuffer[0]; + mstp_port.InputBufferSize = sizeof(RxBuffer); + mstp_port.OutputBuffer = &TxBuffer[0]; + mstp_port.OutputBufferSize = sizeof(TxBuffer); + mstp_port.SilenceTimer = Timer_Silence; + mstp_port.SilenceTimerReset = Timer_Silence_Reset; + mstp_port.This_Station = my_mac; + mstp_port.Nmax_info_frames = 1; + mstp_port.Nmax_master = 127; MSTP_Init(&mstp_port); + /* check the receive error during idle */ + mstp_port.receive_state = MSTP_RECEIVE_STATE_IDLE; + mstp_port.ReceiveError = true; + SilenceTime = 255; + mstp_port.EventCount = 0; + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.ReceiveError == false); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); + /* check for bad packet header */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0x11; INCREMENT_AND_LIMIT_UINT8(EventCount); MSTP_Receive_Frame_FSM(&mstp_port); ct_test(pTest, mstp_port.DataAvailable == false); ct_test(pTest, mstp_port.SilenceTimer() == 0); ct_test(pTest, mstp_port.EventCount == EventCount); - } - ct_test(pTest, mstp_port.ReceivedInvalidFrame == true); - ct_test(pTest, mstp_port.ReceivedValidFrame == false); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); - - /* NoData for us */ - mstp_port.ReceivedInvalidFrame = false; - mstp_port.ReceivedValidFrame = false; - len = MSTP_Create_Frame(buffer, sizeof(buffer), FRAME_TYPE_TOKEN, my_mac, /* destination */ - my_mac, /* source */ - NULL, /* data */ - 0); /* data size */ - ct_test(pTest, len > 0); - Load_Input_Buffer(buffer, len); - for (i = 0; i < len; i++) { - RS485_Check_UART_Data(&mstp_port); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); + /* check for good packet header, but timeout */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0x55; INCREMENT_AND_LIMIT_UINT8(EventCount); MSTP_Receive_Frame_FSM(&mstp_port); ct_test(pTest, mstp_port.DataAvailable == false); ct_test(pTest, mstp_port.SilenceTimer() == 0); ct_test(pTest, mstp_port.EventCount == EventCount); - } - ct_test(pTest, mstp_port.ReceivedInvalidFrame == false); - ct_test(pTest, mstp_port.ReceivedValidFrame == true); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); - - /* FrameTooLong */ - mstp_port.ReceivedInvalidFrame = false; - mstp_port.ReceivedValidFrame = false; - len = MSTP_Create_Frame(buffer, sizeof(buffer), - FRAME_TYPE_TOKEN, - my_mac, /* destination */ - my_mac, /* source */ - NULL, /* data */ - 0); /* data size */ - ct_test(pTest, len > 0); - /* make the header data length bad */ - buffer[5] = 0x02; - Load_Input_Buffer(buffer, len); - for (i = 0; i < len; i++) { - RS485_Check_UART_Data(&mstp_port); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); + /* force the timeout */ + SilenceTime = Tframe_abort + 1; + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); + /* check for good packet header preamble, but receive error */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0x55; INCREMENT_AND_LIMIT_UINT8(EventCount); MSTP_Receive_Frame_FSM(&mstp_port); ct_test(pTest, mstp_port.DataAvailable == false); ct_test(pTest, mstp_port.SilenceTimer() == 0); ct_test(pTest, mstp_port.EventCount == EventCount); - } - ct_test(pTest, mstp_port.ReceivedInvalidFrame == true); - ct_test(pTest, mstp_port.ReceivedValidFrame == false); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); - - /* Data */ - mstp_port.ReceivedInvalidFrame = false; - mstp_port.ReceivedValidFrame = false; - memset(data, 0, sizeof(data)); - len = MSTP_Create_Frame(buffer, sizeof(buffer), - FRAME_TYPE_PROPRIETARY_MIN, - my_mac, /* destination */ - my_mac, /* source */ - data, /* data */ - sizeof(data)); /* data size */ - ct_test(pTest, len > 0); - Load_Input_Buffer(buffer, len); - RS485_Check_UART_Data(&mstp_port); - MSTP_Receive_Frame_FSM(&mstp_port); - while (mstp_port.receive_state != MSTP_RECEIVE_STATE_IDLE) { + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); + /* force the error */ + mstp_port.ReceiveError = true; + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.ReceiveError == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); + /* check for good packet header preamble1, but bad preamble2 */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0x55; + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); + MSTP_Receive_Frame_FSM(&mstp_port); + /* no change of state if no data yet */ + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); + /* repeated preamble1 */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0x55; + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); + /* repeated preamble1 */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0x55; + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); + /* bad data */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0x11; + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.ReceiveError == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); + /* check for good packet header preamble, but timeout in packet */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0x55; + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); + MSTP_Receive_Frame_FSM(&mstp_port); + /* preamble2 */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0xFF; + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.Index == 0); + ct_test(pTest, mstp_port.HeaderCRC == 0xFF); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); + /* force the timeout */ + SilenceTime = Tframe_abort + 1; + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); + ct_test(pTest, mstp_port.ReceivedInvalidFrame == true); + /* check for good packet header preamble, but error in packet */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0x55; + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); + MSTP_Receive_Frame_FSM(&mstp_port); + /* preamble2 */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0xFF; + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.Index == 0); + ct_test(pTest, mstp_port.HeaderCRC == 0xFF); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); + /* force the error */ + mstp_port.ReceiveError = true; + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.ReceiveError == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); + /* check for good packet header preamble */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0x55; + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_PREAMBLE); + MSTP_Receive_Frame_FSM(&mstp_port); + /* preamble2 */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0xFF; + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.Index == 0); + ct_test(pTest, mstp_port.HeaderCRC == 0xFF); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); + /* no change of state if no data yet */ + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); + /* Data is received - index is incremented */ + /* FrameType */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = FRAME_TYPE_TOKEN; + HeaderCRC = 0xFF; + HeaderCRC = CRC_Calc_Header(mstp_port.DataRegister, HeaderCRC); + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.Index == 1); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); + ct_test(pTest, FrameType == FRAME_TYPE_TOKEN); + /* Destination */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0x10; + HeaderCRC = CRC_Calc_Header(mstp_port.DataRegister, HeaderCRC); + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.Index == 2); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); + ct_test(pTest, mstp_port.DestinationAddress == 0x10); + /* Source */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = my_mac; + HeaderCRC = CRC_Calc_Header(mstp_port.DataRegister, HeaderCRC); + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.Index == 3); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); + ct_test(pTest, mstp_port.SourceAddress == my_mac); + /* Length1 = length*256 */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0; + HeaderCRC = CRC_Calc_Header(mstp_port.DataRegister, HeaderCRC); + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.Index == 4); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); + ct_test(pTest, mstp_port.DataLength == 0); + /* Length2 */ + mstp_port.DataAvailable = true; + mstp_port.DataRegister = 0; + HeaderCRC = CRC_Calc_Header(mstp_port.DataRegister, HeaderCRC); + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.Index == 5); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_HEADER); + ct_test(pTest, mstp_port.DataLength == 0); + /* HeaderCRC */ + mstp_port.DataAvailable = true; ct_test(pTest, HeaderCRC == 0x73); /* per Annex G example */ + mstp_port.DataRegister = ~HeaderCRC; /* one's compliment of CRC is sent */ + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount); + ct_test(pTest, mstp_port.Index == 5); + ct_test(pTest, + mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); + ct_test(pTest, mstp_port.HeaderCRC == 0x55); + /* BadCRC in header check */ + mstp_port.ReceivedInvalidFrame = false; mstp_port.ReceivedValidFrame = false; len = MSTP_Create_Frame(buffer, sizeof(buffer), FRAME_TYPE_TOKEN, 0x10, /* destination */ + my_mac, /* source */ + NULL, /* data */ + 0); /* data size */ + ct_test(pTest, len > 0); + /* make the header CRC bad */ + buffer[7] = 0x00; + Load_Input_Buffer(buffer, len); for (i = 0; i < len; i++) { + RS485_Check_UART_Data(&mstp_port); + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount);} + ct_test(pTest, mstp_port.ReceivedInvalidFrame == true); + ct_test(pTest, mstp_port.ReceivedValidFrame == false); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); + /* NoData for us */ + mstp_port.ReceivedInvalidFrame = false; mstp_port.ReceivedValidFrame = false; len = MSTP_Create_Frame(buffer, sizeof(buffer), FRAME_TYPE_TOKEN, my_mac, /* destination */ + my_mac, /* source */ + NULL, /* data */ + 0); /* data size */ + ct_test(pTest, len > 0); + Load_Input_Buffer(buffer, len); for (i = 0; i < len; i++) { + RS485_Check_UART_Data(&mstp_port); + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount);} + ct_test(pTest, mstp_port.ReceivedInvalidFrame == false); + ct_test(pTest, mstp_port.ReceivedValidFrame == true); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); + /* FrameTooLong */ + mstp_port.ReceivedInvalidFrame = false; mstp_port.ReceivedValidFrame = false; len = MSTP_Create_Frame(buffer, sizeof(buffer), FRAME_TYPE_TOKEN, my_mac, /* destination */ + my_mac, /* source */ + NULL, /* data */ + 0); /* data size */ + ct_test(pTest, len > 0); + /* make the header data length bad */ + buffer[5] = 0x02; + Load_Input_Buffer(buffer, len); for (i = 0; i < len; i++) { + RS485_Check_UART_Data(&mstp_port); + INCREMENT_AND_LIMIT_UINT8(EventCount); + MSTP_Receive_Frame_FSM(&mstp_port); + ct_test(pTest, mstp_port.DataAvailable == false); + ct_test(pTest, mstp_port.SilenceTimer() == 0); + ct_test(pTest, mstp_port.EventCount == EventCount);} + ct_test(pTest, mstp_port.ReceivedInvalidFrame == true); + ct_test(pTest, mstp_port.ReceivedValidFrame == false); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); + /* Data */ + mstp_port.ReceivedInvalidFrame = false; mstp_port.ReceivedValidFrame = false; memset(data, 0, sizeof(data)); len = MSTP_Create_Frame(buffer, sizeof(buffer), FRAME_TYPE_PROPRIETARY_MIN, my_mac, /* destination */ + my_mac, /* source */ + data, /* data */ + sizeof(data)); /* data size */ + ct_test(pTest, len > 0); + Load_Input_Buffer(buffer, len); RS485_Check_UART_Data(&mstp_port); MSTP_Receive_Frame_FSM(&mstp_port); - } + while (mstp_port.receive_state != MSTP_RECEIVE_STATE_IDLE) { + RS485_Check_UART_Data(&mstp_port); + MSTP_Receive_Frame_FSM(&mstp_port);} ct_test(pTest, mstp_port.DataLength == sizeof(data)); - ct_test(pTest, mstp_port.ReceivedInvalidFrame == false); - ct_test(pTest, mstp_port.ReceivedValidFrame == true); - ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); + ct_test(pTest, mstp_port.ReceivedInvalidFrame == false); + ct_test(pTest, mstp_port.ReceivedValidFrame == true); + ct_test(pTest, mstp_port.receive_state == MSTP_RECEIVE_STATE_IDLE); + return;} - return; -} - -void testMasterNodeFSM(Test * pTest) -{ - volatile struct mstp_port_struct_t MSTP_Port; /* port data */ - uint8_t my_mac = 0x05; /* local MAC address */ - - MSTP_Port.InputBuffer = &RxBuffer[0]; - MSTP_Port.InputBufferSize = sizeof(RxBuffer); - MSTP_Port.OutputBuffer = &TxBuffer[0]; - MSTP_Port.OutputBufferSize = sizeof(TxBuffer); - MSTP_Port.This_Station = my_mac; - MSTP_Port.Nmax_info_frames = 1; - MSTP_Port.Nmax_master = 127; - MSTP_Port.SilenceTimer = Timer_Silence; - MSTP_Port.SilenceTimerReset = Timer_Silence_Reset; - MSTP_Init(&MSTP_Port); - ct_test(pTest, MSTP_Port.master_state == MSTP_MASTER_STATE_INITIALIZE); - /* FIXME: write a unit test for the Master Node State Machine */ - -} + void testMasterNodeFSM(Test * pTest) { + volatile struct mstp_port_struct_t MSTP_Port; /* port data */ + uint8_t my_mac = 0x05; /* local MAC address */ + MSTP_Port.InputBuffer = &RxBuffer[0]; + MSTP_Port.InputBufferSize = sizeof(RxBuffer); + MSTP_Port.OutputBuffer = &TxBuffer[0]; + MSTP_Port.OutputBufferSize = sizeof(TxBuffer); + MSTP_Port.This_Station = my_mac; + MSTP_Port.Nmax_info_frames = 1; + MSTP_Port.Nmax_master = 127; + MSTP_Port.SilenceTimer = Timer_Silence; + MSTP_Port.SilenceTimerReset = Timer_Silence_Reset; + MSTP_Init(&MSTP_Port); + ct_test(pTest, MSTP_Port.master_state == MSTP_MASTER_STATE_INITIALIZE); + /* FIXME: write a unit test for the Master Node State Machine */ + } #endif #ifdef TEST_MSTP -int main(void) -{ - Test *pTest; - bool rc; - - pTest = ct_create("mstp", NULL); - - /* individual tests */ + int main(void) { + Test * pTest; bool rc; pTest = ct_create("mstp", NULL); + /* individual tests */ rc = ct_addTestFunction(pTest, testReceiveNodeFSM); - assert(rc); - rc = ct_addTestFunction(pTest, testMasterNodeFSM); - assert(rc); - - ct_setStream(pTest, stdout); - ct_run(pTest); - (void) ct_report(pTest); - - ct_destroy(pTest); - - return 0; -} + assert(rc); + rc = ct_addTestFunction(pTest, testMasterNodeFSM); + assert(rc); + ct_setStream(pTest, stdout); + ct_run(pTest); (void) ct_report(pTest); ct_destroy(pTest); return 0;} #endif diff --git a/bacnet-stack/src/mstptext.c b/bacnet-stack/src/mstptext.c index 83ad1076..0234e228 100644 --- a/bacnet-stack/src/mstptext.c +++ b/bacnet-stack/src/mstptext.c @@ -12,10 +12,10 @@ static INDTEXT_DATA mstp_receive_state_text[] = { {0, NULL} }; -const char *mstptext_receive_state(int index) +const char *mstptext_receive_state( + int index) { - return indtext_by_index_default(mstp_receive_state_text, - index, "unknown"); + return indtext_by_index_default(mstp_receive_state_text, index, "unknown"); } static INDTEXT_DATA mstp_master_state_text[] = { @@ -31,10 +31,10 @@ static INDTEXT_DATA mstp_master_state_text[] = { {0, NULL} }; -const char *mstptext_master_state(int index) +const char *mstptext_master_state( + int index) { - return indtext_by_index_default(mstp_master_state_text, - index, "unknown"); + return indtext_by_index_default(mstp_master_state_text, index, "unknown"); } static INDTEXT_DATA mstp_frame_type_text[] = { @@ -44,15 +44,15 @@ static INDTEXT_DATA mstp_frame_type_text[] = { {FRAME_TYPE_TEST_REQUEST, "TEST_REQUEST"}, {FRAME_TYPE_TEST_RESPONSE, "TEST_RESPONSE"}, {FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY, "BACNET_DATA_EXPECTING_REPLY"}, - {FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY, "BACNET_DATA_NOT_EXPECTING_REPLY"}, + {FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY, + "BACNET_DATA_NOT_EXPECTING_REPLY"}, {FRAME_TYPE_REPLY_POSTPONED, "REPLY_POSTPONED"}, {0, NULL} }; -const char *mstptext_frame_type(int index) +const char *mstptext_frame_type( + int index) { return indtext_by_index_split_default(mstp_frame_type_text, - index, - FRAME_TYPE_PROPRIETARY_MIN, - "UNKNOWN", "PROPRIETARY"); + index, FRAME_TYPE_PROPRIETARY_MIN, "UNKNOWN", "PROPRIETARY"); } diff --git a/bacnet-stack/src/npdu.c b/bacnet-stack/src/npdu.c index a71144d5..49c21762 100644 --- a/bacnet-stack/src/npdu.c +++ b/bacnet-stack/src/npdu.c @@ -41,7 +41,9 @@ #include "npdu.h" #include "apdu.h" -void npdu_copy_data(BACNET_NPDU_DATA * dest, BACNET_NPDU_DATA * src) +void npdu_copy_data( + BACNET_NPDU_DATA * dest, + BACNET_NPDU_DATA * src) { if (dest && src) { dest->protocol_version = src->protocol_version; @@ -94,12 +96,14 @@ ABORT.request Yes No Yes No ABORT.indication Yes Yes Yes No */ -int npdu_encode_pdu(uint8_t * npdu, +int npdu_encode_pdu( + uint8_t * npdu, BACNET_ADDRESS * dest, - BACNET_ADDRESS * src, BACNET_NPDU_DATA * npdu_data) + BACNET_ADDRESS * src, + BACNET_NPDU_DATA * npdu_data) { - int len = 0; /* return value - number of octets loaded in this function */ - int i = 0; /* counter */ + int len = 0; /* return value - number of octets loaded in this function */ + int i = 0; /* counter */ if (npdu && npdu_data) { @@ -221,26 +225,30 @@ described in 6.2.2. whether (TRUE) or not (FALSE) a reply service primitive is expected for the service being issued. */ -void npdu_encode_npdu_data(BACNET_NPDU_DATA * npdu_data, - bool data_expecting_reply, BACNET_MESSAGE_PRIORITY priority) +void npdu_encode_npdu_data( + BACNET_NPDU_DATA * npdu_data, + bool data_expecting_reply, + BACNET_MESSAGE_PRIORITY priority) { if (npdu_data) { npdu_data->data_expecting_reply = data_expecting_reply; npdu_data->protocol_version = BACNET_PROTOCOL_VERSION; npdu_data->network_layer_message = false; /* false if APDU */ - npdu_data->network_message_type = NETWORK_MESSAGE_INVALID; /* optional */ + npdu_data->network_message_type = NETWORK_MESSAGE_INVALID; /* optional */ npdu_data->vendor_id = 0; /* optional, if net message type is > 0x80 */ npdu_data->priority = priority; npdu_data->hop_count = 0; } } -int npdu_decode(uint8_t * npdu, +int npdu_decode( + uint8_t * npdu, BACNET_ADDRESS * dest, - BACNET_ADDRESS * src, BACNET_NPDU_DATA * npdu_data) + BACNET_ADDRESS * src, + BACNET_NPDU_DATA * npdu_data) { - int len = 0; /* return value - number of octets loaded in this function */ - int i = 0; /* counter */ + int len = 0; /* return value - number of octets loaded in this function */ + int i = 0; /* counter */ uint16_t src_net = 0; uint16_t dest_net = 0; uint8_t address_len = 0; @@ -271,7 +279,7 @@ int npdu_decode(uint8_t * npdu, /* B'10' = Critical Equipment message */ /* B'01' = Urgent message */ /* B'00' = Normal message */ - npdu_data->priority = (BACNET_MESSAGE_PRIORITY)(npdu[1] & 0x03); + npdu_data->priority = (BACNET_MESSAGE_PRIORITY) (npdu[1] & 0x03); /* set the offset to where the optional stuff starts */ len = 2; /*Bit 5: Destination specifier where: */ @@ -342,14 +350,14 @@ int npdu_decode(uint8_t * npdu, /* Indicates that the NSDU conveys a network layer message. */ /* Message Type field is present. */ if (npdu_data->network_layer_message) { - npdu_data->network_message_type = (BACNET_NETWORK_MESSAGE_TYPE)npdu[len++]; + npdu_data->network_message_type = + (BACNET_NETWORK_MESSAGE_TYPE) npdu[len++]; /* Message Type field contains a value in the range 0x80 - 0xFF, */ /* then a Vendor ID field shall be present */ if (npdu_data->network_message_type >= 0x80) - len += - decode_unsigned16(&npdu[len], &npdu_data->vendor_id); + len += decode_unsigned16(&npdu[len], &npdu_data->vendor_id); } else { - /* FIXME: another value for this? */ + /* FIXME: another value for this? */ npdu_data->network_message_type = NETWORK_MESSAGE_INVALID; } } @@ -357,10 +365,11 @@ int npdu_decode(uint8_t * npdu, return len; } -void npdu_handler(BACNET_ADDRESS * src, /* source address */ - uint8_t * pdu, /* PDU data */ +void npdu_handler( + BACNET_ADDRESS * src, /* source address */ + uint8_t * pdu, /* PDU data */ uint16_t pdu_len) -{ /* length PDU */ +{ /* length PDU */ int apdu_offset = 0; BACNET_ADDRESS dest = { 0 }; BACNET_NPDU_DATA npdu_data = { 0 }; @@ -384,7 +393,8 @@ void npdu_handler(BACNET_ADDRESS * src, /* source address */ #include #include "ctest.h" -void testNPDU2(Test * pTest) +void testNPDU2( + Test * pTest) { uint8_t pdu[480] = { 0 }; BACNET_ADDRESS dest = { 0 }; @@ -395,7 +405,7 @@ void testNPDU2(Test * pTest) bool data_expecting_reply = true; BACNET_MESSAGE_PRIORITY priority = MESSAGE_PRIORITY_NORMAL; BACNET_NPDU_DATA npdu_data = { 0 }; - int i = 0; /* counter */ + int i = 0; /* counter */ int npdu_len = 0; bool network_layer_message = false; /* false if APDU */ BACNET_NETWORK_MESSAGE_TYPE network_message_type = 0; /* optional */ @@ -428,11 +438,9 @@ void testNPDU2(Test * pTest) npdu_len = npdu_decode(&pdu[0], &npdu_dest, &npdu_src, &npdu_data); ct_test(pTest, npdu_len != 0); ct_test(pTest, npdu_data.data_expecting_reply == data_expecting_reply); - ct_test(pTest, - npdu_data.network_layer_message == network_layer_message); + ct_test(pTest, npdu_data.network_layer_message == network_layer_message); if (npdu_data.network_layer_message) { - ct_test(pTest, - npdu_data.network_message_type == network_message_type); + ct_test(pTest, npdu_data.network_message_type == network_message_type); } ct_test(pTest, npdu_data.vendor_id == vendor_id); ct_test(pTest, npdu_data.priority == priority); @@ -450,7 +458,8 @@ void testNPDU2(Test * pTest) } } -void testNPDU1(Test * pTest) +void testNPDU1( + Test * pTest) { uint8_t pdu[480] = { 0 }; BACNET_ADDRESS dest = { 0 }; @@ -461,7 +470,7 @@ void testNPDU1(Test * pTest) bool data_expecting_reply = false; BACNET_MESSAGE_PRIORITY priority = MESSAGE_PRIORITY_NORMAL; BACNET_NPDU_DATA npdu_data = { 0 }; - int i = 0; /* counter */ + int i = 0; /* counter */ int npdu_len = 0; bool network_layer_message = false; /* false if APDU */ BACNET_NETWORK_MESSAGE_TYPE network_message_type = 0; /* optional */ @@ -495,11 +504,9 @@ void testNPDU1(Test * pTest) npdu_len = npdu_decode(&pdu[0], &npdu_dest, &npdu_src, &npdu_data); ct_test(pTest, npdu_len != 0); ct_test(pTest, npdu_data.data_expecting_reply == data_expecting_reply); - ct_test(pTest, - npdu_data.network_layer_message == network_layer_message); + ct_test(pTest, npdu_data.network_layer_message == network_layer_message); if (npdu_data.network_layer_message) { - ct_test(pTest, - npdu_data.network_message_type == network_message_type); + ct_test(pTest, npdu_data.network_message_type == network_message_type); } ct_test(pTest, npdu_data.vendor_id == vendor_id); ct_test(pTest, npdu_data.priority == priority); @@ -509,20 +516,24 @@ void testNPDU1(Test * pTest) #ifdef TEST_NPDU /* dummy stub for testing */ -void tsm_free_invoke_id(uint8_t invokeID) +void tsm_free_invoke_id( + uint8_t invokeID) { (void) invokeID; } -void iam_handler(uint8_t * service_request, - uint16_t service_len, BACNET_ADDRESS * src) +void iam_handler( + uint8_t * service_request, + uint16_t service_len, + BACNET_ADDRESS * src) { (void) service_request; (void) service_len; (void) src; } -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -541,5 +552,5 @@ int main(void) return 0; } -#endif /* TEST_NPDU */ -#endif /* TEST */ +#endif /* TEST_NPDU */ +#endif /* TEST */ diff --git a/bacnet-stack/src/rd.c b/bacnet-stack/src/rd.c index 001fb206..43fa9d25 100644 --- a/bacnet-stack/src/rd.c +++ b/bacnet-stack/src/rd.c @@ -38,12 +38,14 @@ #include "rd.h" /* encode service */ -int rd_encode_apdu(uint8_t * apdu, +int rd_encode_apdu( + uint8_t * apdu, uint8_t invoke_id, - BACNET_REINITIALIZED_STATE state, BACNET_CHARACTER_STRING * password) + BACNET_REINITIALIZED_STATE state, + BACNET_CHARACTER_STRING * password) { - int len = 0; /* length of each encoding */ - int apdu_len = 0; /* total length of the apdu, return value */ + int len = 0; /* length of each encoding */ + int apdu_len = 0; /* total length of the apdu, return value */ if (apdu) { apdu[0] = PDU_TYPE_CONFIRMED_SERVICE_REQUEST; @@ -66,9 +68,11 @@ int rd_encode_apdu(uint8_t * apdu, } /* decode the service request only */ -int rd_decode_service_request(uint8_t * apdu, +int rd_decode_service_request( + uint8_t * apdu, unsigned apdu_len, - BACNET_REINITIALIZED_STATE * state, BACNET_CHARACTER_STRING * password) + BACNET_REINITIALIZED_STATE * state, + BACNET_CHARACTER_STRING * password) { unsigned len = 0; uint8_t tag_number = 0; @@ -84,7 +88,7 @@ int rd_decode_service_request(uint8_t * apdu, &tag_number, &len_value_type); len += decode_enumerated(&apdu[len], len_value_type, &value); if (state) - *state = (BACNET_REINITIALIZED_STATE)value; + *state = (BACNET_REINITIALIZED_STATE) value; /* Tag 1: password - optional */ if (len < apdu_len) { if (!decode_is_context_tag(&apdu[len], 1)) @@ -92,8 +96,7 @@ int rd_decode_service_request(uint8_t * apdu, len += decode_tag_number_and_value(&apdu[len], &tag_number, &len_value_type); len += - decode_character_string(&apdu[len], len_value_type, - password); + decode_character_string(&apdu[len], len_value_type, password); } } @@ -105,10 +108,12 @@ int rd_decode_service_request(uint8_t * apdu, #include #include "ctest.h" -int rd_decode_apdu(uint8_t * apdu, +int rd_decode_apdu( + uint8_t * apdu, unsigned apdu_len, uint8_t * invoke_id, - BACNET_REINITIALIZED_STATE * state, BACNET_CHARACTER_STRING * password) + BACNET_REINITIALIZED_STATE * state, + BACNET_CHARACTER_STRING * password) { int len = 0; unsigned offset = 0; @@ -132,7 +137,8 @@ int rd_decode_apdu(uint8_t * apdu, return len; } -void test_ReinitializeDevice(Test * pTest) +void test_ReinitializeDevice( + Test * pTest) { uint8_t apdu[480] = { 0 }; int len = 0; @@ -161,7 +167,8 @@ void test_ReinitializeDevice(Test * pTest) } #ifdef TEST_REINITIALIZE_DEVICE -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -178,5 +185,5 @@ int main(void) return 0; } -#endif /* TEST_REINITIALIZE_DEVICE */ -#endif /* TEST */ +#endif /* TEST_REINITIALIZE_DEVICE */ +#endif /* TEST */ diff --git a/bacnet-stack/src/reject.c b/bacnet-stack/src/reject.c index 8edc441d..be7ba9e4 100644 --- a/bacnet-stack/src/reject.c +++ b/bacnet-stack/src/reject.c @@ -37,10 +37,12 @@ #include "bacdef.h" /* encode service */ -int reject_encode_apdu(uint8_t * apdu, - uint8_t invoke_id, uint8_t reject_reason) +int reject_encode_apdu( + uint8_t * apdu, + uint8_t invoke_id, + uint8_t reject_reason) { - int apdu_len = 0; /* total length of the apdu, return value */ + int apdu_len = 0; /* total length of the apdu, return value */ if (apdu) { apdu[0] = PDU_TYPE_REJECT; @@ -53,8 +55,11 @@ int reject_encode_apdu(uint8_t * apdu, } /* decode the service request only */ -int reject_decode_service_request(uint8_t * apdu, - unsigned apdu_len, uint8_t * invoke_id, uint8_t * reject_reason) +int reject_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + uint8_t * invoke_id, + uint8_t * reject_reason) { int len = 0; @@ -74,8 +79,11 @@ int reject_decode_service_request(uint8_t * apdu, #include "ctest.h" /* decode the whole APDU - mainly used for unit testing */ -int reject_decode_apdu(uint8_t * apdu, - unsigned apdu_len, uint8_t * invoke_id, uint8_t * reject_reason) +int reject_decode_apdu( + uint8_t * apdu, + unsigned apdu_len, + uint8_t * invoke_id, + uint8_t * reject_reason) { int len = 0; @@ -94,7 +102,8 @@ int reject_decode_apdu(uint8_t * apdu, return len; } -void testReject(Test * pTest) +void testReject( + Test * pTest) { uint8_t apdu[480] = { 0 }; int len = 0; @@ -147,7 +156,8 @@ void testReject(Test * pTest) } #ifdef TEST_REJECT -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -164,5 +174,5 @@ int main(void) return 0; } -#endif /* TEST_REJECT */ -#endif /* TEST */ +#endif /* TEST_REJECT */ +#endif /* TEST */ diff --git a/bacnet-stack/src/ringbuf.c b/bacnet-stack/src/ringbuf.c index c5675d1b..3a9568aa 100644 --- a/bacnet-stack/src/ringbuf.c +++ b/bacnet-stack/src/ringbuf.c @@ -46,7 +46,8 @@ * ALGORITHM: none * NOTES: none *****************************************************************************/ -bool Ringbuf_Empty(RING_BUFFER const *b) +bool Ringbuf_Empty( + RING_BUFFER const *b) { return (b->count == 0); } @@ -57,7 +58,8 @@ bool Ringbuf_Empty(RING_BUFFER const *b) * ALGORITHM: none * NOTES: none *****************************************************************************/ -char *Ringbuf_Get_Front(RING_BUFFER const *b) +char *Ringbuf_Get_Front( + RING_BUFFER const *b) { return (b->count ? &(b->data[b->head * b->element_size]) : NULL); } @@ -68,9 +70,10 @@ char *Ringbuf_Get_Front(RING_BUFFER const *b) * ALGORITHM: none * NOTES: none *****************************************************************************/ -char *Ringbuf_Pop_Front(RING_BUFFER * b) +char *Ringbuf_Pop_Front( + RING_BUFFER * b) { - char *data = NULL; /* return value */ + char *data = NULL; /* return value */ if (b->count) { data = &(b->data[b->head * b->element_size]); @@ -89,13 +92,14 @@ char *Ringbuf_Pop_Front(RING_BUFFER * b) * ALGORITHM: none * NOTES: none *****************************************************************************/ -bool Ringbuf_Put(RING_BUFFER * b, /* ring buffer structure */ +bool Ringbuf_Put( + RING_BUFFER * b, /* ring buffer structure */ char *data_element) -{ /* one element to add to the ring */ +{ /* one element to add to the ring */ bool status = false; /* return value */ unsigned offset = 0; /* offset into array of data */ char *ring_data = NULL; /* used to help point ring data */ - unsigned i; /* loop counter */ + unsigned i; /* loop counter */ if (b && data_element) { /* limit the amount of data that we accept */ @@ -121,11 +125,12 @@ bool Ringbuf_Put(RING_BUFFER * b, /* ring buffer structure */ * ALGORITHM: none * NOTES: none *****************************************************************************/ -void Ringbuf_Init(RING_BUFFER * b, /* ring buffer structure */ - char *data, /* data block or array of data */ +void Ringbuf_Init( + RING_BUFFER * b, /* ring buffer structure */ + char *data, /* data block or array of data */ unsigned element_size, /* size of one element in the data block */ unsigned element_count) -{ /* number of elements in the data block */ +{ /* number of elements in the data block */ b->head = 0; b->count = 0; b->data = data; @@ -144,7 +149,8 @@ void Ringbuf_Init(RING_BUFFER * b, /* ring buffer structure */ /* test the FIFO */ #define RING_BUFFER_DATA_SIZE 5 #define RING_BUFFER_COUNT 16 -void testRingBuf(Test * pTest) +void testRingBuf( + Test * pTest) { RING_BUFFER test_buffer; char data_store[RING_BUFFER_DATA_SIZE * RING_BUFFER_COUNT]; @@ -181,8 +187,7 @@ void testRingBuf(Test * pTest) /* fill to max */ for (index = 0; index < RING_BUFFER_COUNT; index++) { - for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; - data_index++) { + for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; data_index++) { data[data_index] = index; } status = Ringbuf_Put(&test_buffer, data); @@ -191,8 +196,7 @@ void testRingBuf(Test * pTest) } /* verify actions on full buffer */ for (index = 0; index < RING_BUFFER_COUNT; index++) { - for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; - data_index++) { + for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; data_index++) { data[data_index] = index; } status = Ringbuf_Put(&test_buffer, data); @@ -203,14 +207,12 @@ void testRingBuf(Test * pTest) /* check buffer full */ for (index = 0; index < RING_BUFFER_COUNT; index++) { test_data = Ringbuf_Get_Front(&test_buffer); - for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; - data_index++) { + for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; data_index++) { ct_test(pTest, test_data[data_index] == index); } test_data = Ringbuf_Pop_Front(&test_buffer); - for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; - data_index++) { + for (data_index = 0; data_index < RING_BUFFER_DATA_SIZE; data_index++) { ct_test(pTest, test_data[data_index] == index); } } @@ -250,7 +252,8 @@ void testRingBuf(Test * pTest) } #ifdef TEST_RINGBUF -int main(void) +int main( + void) { Test *pTest; bool rc; diff --git a/bacnet-stack/src/rp.c b/bacnet-stack/src/rp.c index a81550cb..f4b38c8e 100644 --- a/bacnet-stack/src/rp.c +++ b/bacnet-stack/src/rp.c @@ -38,11 +38,13 @@ #include "rp.h" /* encode service */ -int rp_encode_apdu(uint8_t * apdu, - uint8_t invoke_id, BACNET_READ_PROPERTY_DATA * rpdata) +int rp_encode_apdu( + uint8_t * apdu, + uint8_t invoke_id, + BACNET_READ_PROPERTY_DATA * rpdata) { - int len = 0; /* length of each encoding */ - int apdu_len = 0; /* total length of the apdu, return value */ + int len = 0; /* length of each encoding */ + int apdu_len = 0; /* total length of the apdu, return value */ if (apdu) { apdu[0] = PDU_TYPE_CONFIRMED_SERVICE_REQUEST; @@ -68,14 +70,16 @@ int rp_encode_apdu(uint8_t * apdu, } /* decode the service request only */ -int rp_decode_service_request(uint8_t * apdu, - unsigned apdu_len, BACNET_READ_PROPERTY_DATA * rpdata) +int rp_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + BACNET_READ_PROPERTY_DATA * rpdata) { unsigned len = 0; uint8_t tag_number = 0; uint32_t len_value_type = 0; - int type = 0; /* for decoding */ - int property = 0; /* for decoding */ + int type = 0; /* for decoding */ + int property = 0; /* for decoding */ uint32_t array_value = 0; /* for decoding */ /* check for value pointers */ @@ -84,14 +88,14 @@ int rp_decode_service_request(uint8_t * apdu, if (!decode_is_context_tag(&apdu[len++], 0)) return -1; len += decode_object_id(&apdu[len], &type, &rpdata->object_instance); - rpdata->object_type = (BACNET_OBJECT_TYPE)type; + rpdata->object_type = (BACNET_OBJECT_TYPE) type; /* Tag 1: Property ID */ len += decode_tag_number_and_value(&apdu[len], &tag_number, &len_value_type); if (tag_number != 1) return -1; len += decode_enumerated(&apdu[len], len_value_type, &property); - rpdata->object_property = (BACNET_PROPERTY_ID)property; + rpdata->object_property = (BACNET_PROPERTY_ID) property; /* Tag 2: Optional Array Index */ if (len < apdu_len) { len += decode_tag_number_and_value(&apdu[len], &tag_number, @@ -109,11 +113,13 @@ int rp_decode_service_request(uint8_t * apdu, return (int) len; } -int rp_ack_encode_apdu(uint8_t * apdu, - uint8_t invoke_id, BACNET_READ_PROPERTY_DATA * rpdata) +int rp_ack_encode_apdu( + uint8_t * apdu, + uint8_t invoke_id, + BACNET_READ_PROPERTY_DATA * rpdata) { - int len = 0; /* length of each encoding */ - int apdu_len = 0; /* total length of the apdu, return value */ + int len = 0; /* length of each encoding */ + int apdu_len = 0; /* total length of the apdu, return value */ if (apdu) { apdu[0] = PDU_TYPE_COMPLEX_ACK; /* complex ACK service */ @@ -141,13 +147,15 @@ int rp_ack_encode_apdu(uint8_t * apdu, return apdu_len; } -int rp_ack_decode_service_request(uint8_t * apdu, int apdu_len, /* total length of the apdu */ +int rp_ack_decode_service_request( + uint8_t * apdu, + int apdu_len, /* total length of the apdu */ BACNET_READ_PROPERTY_DATA * rpdata) { uint8_t tag_number = 0; uint32_t len_value_type = 0; - int tag_len = 0; /* length of tag decode */ - int len = 0; /* total length of decodes */ + int tag_len = 0; /* length of tag decode */ + int len = 0; /* total length of decodes */ int object = 0, property = 0; /* for decoding */ uint32_t array_value = 0; /* for decoding */ @@ -157,14 +165,14 @@ int rp_ack_decode_service_request(uint8_t * apdu, int apdu_len, /* total length return -1; len = 1; len += decode_object_id(&apdu[len], &object, &rpdata->object_instance); - rpdata->object_type = (BACNET_OBJECT_TYPE)object; + rpdata->object_type = (BACNET_OBJECT_TYPE) object; /* Tag 1: Property ID */ len += decode_tag_number_and_value(&apdu[len], &tag_number, &len_value_type); if (tag_number != 1) return -1; len += decode_enumerated(&apdu[len], len_value_type, &property); - rpdata->object_property = (BACNET_PROPERTY_ID)property; + rpdata->object_property = (BACNET_PROPERTY_ID) property; /* Tag 2: Optional Array Index */ tag_len = decode_tag_number_and_value(&apdu[len], &tag_number, &len_value_type); @@ -192,9 +200,11 @@ int rp_ack_decode_service_request(uint8_t * apdu, int apdu_len, /* total length #include #include "ctest.h" -int rp_decode_apdu(uint8_t * apdu, +int rp_decode_apdu( + uint8_t * apdu, unsigned apdu_len, - uint8_t * invoke_id, BACNET_READ_PROPERTY_DATA * rpdata) + uint8_t * invoke_id, + BACNET_READ_PROPERTY_DATA * rpdata) { int len = 0; unsigned offset = 0; @@ -218,8 +228,11 @@ int rp_decode_apdu(uint8_t * apdu, return len; } -int rp_ack_decode_apdu(uint8_t * apdu, int apdu_len, /* total length of the apdu */ - uint8_t * invoke_id, BACNET_READ_PROPERTY_DATA * rpdata) +int rp_ack_decode_apdu( + uint8_t * apdu, + int apdu_len, /* total length of the apdu */ + uint8_t * invoke_id, + BACNET_READ_PROPERTY_DATA * rpdata) { int len = 0; int offset = 0; @@ -241,7 +254,8 @@ int rp_ack_decode_apdu(uint8_t * apdu, int apdu_len, /* total length of the a return len; } -void testReadPropertyAck(Test * pTest) +void testReadPropertyAck( + Test * pTest) { uint8_t apdu[480] = { 0 }; uint8_t apdu2[480] = { 0 }; @@ -289,7 +303,8 @@ void testReadPropertyAck(Test * pTest) ct_test(pTest, object_instance == rpdata.object_instance); } -void testReadProperty(Test * pTest) +void testReadProperty( + Test * pTest) { uint8_t apdu[480] = { 0 }; int len = 0; @@ -318,7 +333,8 @@ void testReadProperty(Test * pTest) } #ifdef TEST_READ_PROPERTY -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -337,5 +353,5 @@ int main(void) return 0; } -#endif /* TEST_READ_PROPERTY */ -#endif /* TEST */ +#endif /* TEST_READ_PROPERTY */ +#endif /* TEST */ diff --git a/bacnet-stack/src/rpm.c b/bacnet-stack/src/rpm.c index ba4d7f61..687f62bb 100644 --- a/bacnet-stack/src/rpm.c +++ b/bacnet-stack/src/rpm.c @@ -40,9 +40,11 @@ #include "rpm.h" /* encode the initial portion of the service */ -int rpm_encode_apdu_init(uint8_t * apdu, uint8_t invoke_id) +int rpm_encode_apdu_init( + uint8_t * apdu, + uint8_t invoke_id) { - int apdu_len = 0; /* total length of the apdu, return value */ + int apdu_len = 0; /* total length of the apdu, return value */ if (apdu) { apdu[0] = PDU_TYPE_CONFIRMED_SERVICE_REQUEST; @@ -55,10 +57,12 @@ int rpm_encode_apdu_init(uint8_t * apdu, uint8_t invoke_id) return apdu_len; } -int rpm_encode_apdu_object_begin(uint8_t * apdu, - BACNET_OBJECT_TYPE object_type, uint32_t object_instance) +int rpm_encode_apdu_object_begin( + uint8_t * apdu, + BACNET_OBJECT_TYPE object_type, + uint32_t object_instance) { - int apdu_len = 0; /* total length of the apdu, return value */ + int apdu_len = 0; /* total length of the apdu, return value */ if (apdu) { apdu_len = encode_context_object_id(&apdu[0], 0, @@ -70,10 +74,12 @@ int rpm_encode_apdu_object_begin(uint8_t * apdu, return apdu_len; } -int rpm_encode_apdu_object_property(uint8_t * apdu, - BACNET_PROPERTY_ID object_property, int32_t array_index) +int rpm_encode_apdu_object_property( + uint8_t * apdu, + BACNET_PROPERTY_ID object_property, + int32_t array_index) { - int apdu_len = 0; /* total length of the apdu, return value */ + int apdu_len = 0; /* total length of the apdu, return value */ if (apdu) { apdu_len = encode_context_enumerated(&apdu[0], 0, object_property); @@ -86,9 +92,10 @@ int rpm_encode_apdu_object_property(uint8_t * apdu, return apdu_len; } -int rpm_encode_apdu_object_end(uint8_t * apdu) +int rpm_encode_apdu_object_end( + uint8_t * apdu) { - int apdu_len = 0; /* total length of the apdu, return value */ + int apdu_len = 0; /* total length of the apdu, return value */ if (apdu) { apdu_len = encode_closing_tag(&apdu[0], 1); @@ -98,12 +105,14 @@ int rpm_encode_apdu_object_end(uint8_t * apdu) } /* decode the object portion of the service request only */ -int rpm_decode_object_id(uint8_t * apdu, +int rpm_decode_object_id( + uint8_t * apdu, unsigned apdu_len, - BACNET_OBJECT_TYPE * object_type, uint32_t * object_instance) + BACNET_OBJECT_TYPE * object_type, + uint32_t * object_instance) { unsigned len = 0; - int type = 0; /* for decoding */ + int type = 0; /* for decoding */ /* check for value pointers */ if (apdu && apdu_len && object_type && object_instance) { @@ -112,19 +121,21 @@ int rpm_decode_object_id(uint8_t * apdu, return -1; len += decode_object_id(&apdu[len], &type, object_instance); if (object_type) - *object_type = (BACNET_OBJECT_TYPE)type; + *object_type = (BACNET_OBJECT_TYPE) type; /* Tag 1: sequence of ReadAccessSpecification */ if (!decode_is_opening_tag_number(&apdu[len], 1)) return -1; - len++; /* opening tag is only one octet */ + len++; /* opening tag is only one octet */ } return (int) len; } -int rpm_decode_object_end(uint8_t * apdu, unsigned apdu_len) +int rpm_decode_object_end( + uint8_t * apdu, + unsigned apdu_len) { - int len = 0; /* total length of the apdu, return value */ + int len = 0; /* total length of the apdu, return value */ if (apdu && apdu_len) { if (decode_is_closing_tag_number(apdu, 1)) @@ -142,15 +153,17 @@ int rpm_decode_object_end(uint8_t * apdu, unsigned apdu_len) -- if omitted with an array the entire array is referenced } */ -int rpm_decode_object_property(uint8_t * apdu, +int rpm_decode_object_property( + uint8_t * apdu, unsigned apdu_len, - BACNET_PROPERTY_ID * object_property, int32_t * array_index) + BACNET_PROPERTY_ID * object_property, + int32_t * array_index) { unsigned len = 0; unsigned option_len = 0; uint8_t tag_number = 0; uint32_t len_value_type = 0; - int property = 0; /* for decoding */ + int property = 0; /* for decoding */ uint32_t array_value = 0; /* for decoding */ /* check for valid pointers */ @@ -164,7 +177,7 @@ int rpm_decode_object_property(uint8_t * apdu, return -1; len += decode_enumerated(&apdu[len], len_value_type, &property); if (object_property) - *object_property = (BACNET_PROPERTY_ID)property; + *object_property = (BACNET_PROPERTY_ID) property; /* Tag 1: Optional propertyArrayIndex */ if ((len < apdu_len) && decode_is_context_specific(&apdu[len]) && @@ -186,9 +199,11 @@ int rpm_decode_object_property(uint8_t * apdu, return (int) len; } -int rpm_ack_encode_apdu_init(uint8_t * apdu, uint8_t invoke_id) +int rpm_ack_encode_apdu_init( + uint8_t * apdu, + uint8_t invoke_id) { - int apdu_len = 0; /* total length of the apdu, return value */ + int apdu_len = 0; /* total length of the apdu, return value */ if (apdu) { apdu[0] = PDU_TYPE_COMPLEX_ACK; /* complex ACK service */ @@ -200,10 +215,12 @@ int rpm_ack_encode_apdu_init(uint8_t * apdu, uint8_t invoke_id) return apdu_len; } -int rpm_ack_encode_apdu_object_begin(uint8_t * apdu, - BACNET_OBJECT_TYPE object_type, uint32_t object_instance) +int rpm_ack_encode_apdu_object_begin( + uint8_t * apdu, + BACNET_OBJECT_TYPE object_type, + uint32_t object_instance) { - int apdu_len = 0; /* total length of the apdu, return value */ + int apdu_len = 0; /* total length of the apdu, return value */ if (apdu) { /* Tag 0: objectIdentifier */ @@ -216,10 +233,12 @@ int rpm_ack_encode_apdu_object_begin(uint8_t * apdu, return apdu_len; } -int rpm_ack_encode_apdu_object_property(uint8_t * apdu, - BACNET_PROPERTY_ID object_property, int32_t array_index) +int rpm_ack_encode_apdu_object_property( + uint8_t * apdu, + BACNET_PROPERTY_ID object_property, + int32_t array_index) { - int apdu_len = 0; /* total length of the apdu, return value */ + int apdu_len = 0; /* total length of the apdu, return value */ if (apdu) { /* Tag 2: propertyIdentifier */ @@ -233,10 +252,12 @@ int rpm_ack_encode_apdu_object_property(uint8_t * apdu, return apdu_len; } -int rpm_ack_encode_apdu_object_property_value(uint8_t * apdu, - uint8_t * application_data, unsigned application_data_len) +int rpm_ack_encode_apdu_object_property_value( + uint8_t * apdu, + uint8_t * application_data, + unsigned application_data_len) { - int apdu_len = 0; /* total length of the apdu, return value */ + int apdu_len = 0; /* total length of the apdu, return value */ unsigned len = 0; if (apdu) { @@ -251,15 +272,18 @@ int rpm_ack_encode_apdu_object_property_value(uint8_t * apdu, return apdu_len; } -int rpm_ack_encode_apdu_object_property_error(uint8_t * apdu, - BACNET_ERROR_CLASS error_class, BACNET_ERROR_CODE error_code) +int rpm_ack_encode_apdu_object_property_error( + uint8_t * apdu, + BACNET_ERROR_CLASS error_class, + BACNET_ERROR_CODE error_code) { - int apdu_len = 0; /* total length of the apdu, return value */ + int apdu_len = 0; /* total length of the apdu, return value */ if (apdu) { /* Tag 5: propertyAccessError */ apdu_len += encode_opening_tag(&apdu[apdu_len], 5); - apdu_len += encode_application_enumerated(&apdu[apdu_len], error_class); + apdu_len += + encode_application_enumerated(&apdu[apdu_len], error_class); apdu_len += encode_application_enumerated(&apdu[apdu_len], error_code); apdu_len += encode_closing_tag(&apdu[apdu_len], 5); } @@ -267,9 +291,10 @@ int rpm_ack_encode_apdu_object_property_error(uint8_t * apdu, return apdu_len; } -int rpm_ack_encode_apdu_object_end(uint8_t * apdu) +int rpm_ack_encode_apdu_object_end( + uint8_t * apdu) { - int apdu_len = 0; /* total length of the apdu, return value */ + int apdu_len = 0; /* total length of the apdu, return value */ if (apdu) { apdu_len = encode_closing_tag(&apdu[0], 1); @@ -279,12 +304,14 @@ int rpm_ack_encode_apdu_object_end(uint8_t * apdu) } /* decode the object portion of the service request only */ -int rpm_ack_decode_object_id(uint8_t * apdu, +int rpm_ack_decode_object_id( + uint8_t * apdu, unsigned apdu_len, - BACNET_OBJECT_TYPE * object_type, uint32_t * object_instance) + BACNET_OBJECT_TYPE * object_type, + uint32_t * object_instance) { unsigned len = 0; - int type = 0; /* for decoding */ + int type = 0; /* for decoding */ /* check for value pointers */ if (apdu && apdu_len && object_type && object_instance) { @@ -293,20 +320,22 @@ int rpm_ack_decode_object_id(uint8_t * apdu, return -1; len += decode_object_id(&apdu[len], &type, object_instance); if (object_type) - *object_type = (BACNET_OBJECT_TYPE)type; + *object_type = (BACNET_OBJECT_TYPE) type; /* Tag 1: listOfResults */ if (!decode_is_opening_tag_number(&apdu[len], 1)) return -1; - len++; /* opening tag is only one octet */ + len++; /* opening tag is only one octet */ } return (int) len; } /* is this the end of the list of this objects properties values? */ -int rpm_ack_decode_object_end(uint8_t * apdu, unsigned apdu_len) +int rpm_ack_decode_object_end( + uint8_t * apdu, + unsigned apdu_len) { - int len = 0; /* total length of the apdu, return value */ + int len = 0; /* total length of the apdu, return value */ if (apdu && apdu_len) { if (decode_is_closing_tag_number(apdu, 1)) @@ -316,15 +345,17 @@ int rpm_ack_decode_object_end(uint8_t * apdu, unsigned apdu_len) return len; } -int rpm_ack_decode_object_property(uint8_t * apdu, +int rpm_ack_decode_object_property( + uint8_t * apdu, unsigned apdu_len, - BACNET_PROPERTY_ID * object_property, int32_t * array_index) + BACNET_PROPERTY_ID * object_property, + int32_t * array_index) { unsigned len = 0; unsigned tag_len = 0; uint8_t tag_number = 0; uint32_t len_value_type = 0; - int property = 0; /* for decoding */ + int property = 0; /* for decoding */ uint32_t array_value = 0; /* for decoding */ /* check for valid pointers */ @@ -338,7 +369,7 @@ int rpm_ack_decode_object_property(uint8_t * apdu, return -1; len += decode_enumerated(&apdu[len], len_value_type, &property); if (object_property) - *object_property = (BACNET_PROPERTY_ID)property; + *object_property = (BACNET_PROPERTY_ID) property; /* Tag 3: Optional propertyArrayIndex */ if ((len < apdu_len) && decode_is_context_specific(&apdu[len]) && @@ -366,9 +397,12 @@ int rpm_ack_decode_object_property(uint8_t * apdu, #include #include "ctest.h" -int rpm_ack_decode_apdu(uint8_t * apdu, int apdu_len, /* total length of the apdu */ +int rpm_ack_decode_apdu( + uint8_t * apdu, + int apdu_len, /* total length of the apdu */ uint8_t * invoke_id, - uint8_t ** service_request, unsigned *service_request_len) + uint8_t ** service_request, + unsigned *service_request_len) { int offset = 0; @@ -391,10 +425,12 @@ int rpm_ack_decode_apdu(uint8_t * apdu, int apdu_len, /* total length of the a return offset; } -int rpm_decode_apdu(uint8_t * apdu, +int rpm_decode_apdu( + uint8_t * apdu, unsigned apdu_len, uint8_t * invoke_id, - uint8_t ** service_request, unsigned *service_request_len) + uint8_t ** service_request, + unsigned *service_request_len) { unsigned offset = 0; @@ -419,7 +455,8 @@ int rpm_decode_apdu(uint8_t * apdu, return offset; } -void testReadPropertyMultiple(Test * pTest) +void testReadPropertyMultiple( + Test * pTest) { uint8_t apdu[480] = { 0 }; int len = 0; @@ -527,7 +564,8 @@ void testReadPropertyMultiple(Test * pTest) ct_test(pTest, len == service_request_len); } -void testReadPropertyMultipleAck(Test * pTest) +void testReadPropertyMultipleAck( + Test * pTest) { uint8_t apdu[480] = { 0 }; int len = 0; @@ -740,7 +778,8 @@ void testReadPropertyMultipleAck(Test * pTest) } #ifdef TEST_READ_PROPERTY_MULTIPLE -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -759,6 +798,6 @@ int main(void) return 0; } -#endif /* TEST_READ_PROPERTY_MULTIPLE */ +#endif /* TEST_READ_PROPERTY_MULTIPLE */ -#endif /* TEST */ +#endif /* TEST */ diff --git a/bacnet-stack/src/sbuf.c b/bacnet-stack/src/sbuf.c index cbb5d954..19174e7a 100644 --- a/bacnet-stack/src/sbuf.c +++ b/bacnet-stack/src/sbuf.c @@ -40,10 +40,11 @@ #include #include "sbuf.h" -void sbuf_init(STATIC_BUFFER * b, /* static buffer structure */ - char *data, /* actual size, in bytes, of the data block or array of data */ +void sbuf_init( + STATIC_BUFFER * b, /* static buffer structure */ + char *data, /* actual size, in bytes, of the data block or array of data */ unsigned size) -{ /* number of bytes used */ +{ /* number of bytes used */ if (b) { b->data = data; b->size = size; @@ -54,32 +55,37 @@ void sbuf_init(STATIC_BUFFER * b, /* static buffer structure */ } /* returns true if count==0, false if count > 0 */ -bool sbuf_empty(STATIC_BUFFER const *b) +bool sbuf_empty( + STATIC_BUFFER const *b) { return (b ? (b->count == 0) : false); } -char *sbuf_data(STATIC_BUFFER const *b) +char *sbuf_data( + STATIC_BUFFER const *b) { return (b ? b->data : NULL); } -unsigned sbuf_size(STATIC_BUFFER * b) +unsigned sbuf_size( + STATIC_BUFFER * b) { return (b ? b->size : 0); } -unsigned sbuf_count(STATIC_BUFFER * b) +unsigned sbuf_count( + STATIC_BUFFER * b) { return (b ? b->count : 0); } /* returns true if successful, false if not enough room to append data */ -bool sbuf_put(STATIC_BUFFER * b, /* static buffer structure */ - unsigned offset, /* where to start */ - char *data, /* number of bytes used */ +bool sbuf_put( + STATIC_BUFFER * b, /* static buffer structure */ + unsigned offset, /* where to start */ + char *data, /* number of bytes used */ unsigned data_size) -{ /* how many to add */ +{ /* how many to add */ bool status = false; /* return value */ if (b && b->data) { @@ -99,10 +105,11 @@ bool sbuf_put(STATIC_BUFFER * b, /* static buffer structure */ } /* returns true if successful, false if not enough room to append data */ -bool sbuf_append(STATIC_BUFFER * b, /* static buffer structure */ - char *data, /* number of bytes used */ +bool sbuf_append( + STATIC_BUFFER * b, /* static buffer structure */ + char *data, /* number of bytes used */ unsigned data_size) -{ /* how many to add */ +{ /* how many to add */ unsigned count = 0; if (b) @@ -112,9 +119,10 @@ bool sbuf_append(STATIC_BUFFER * b, /* static buffer structure */ } /* returns true if successful, false if not enough room to append data */ -bool sbuf_truncate(STATIC_BUFFER * b, /* static buffer structure */ +bool sbuf_truncate( + STATIC_BUFFER * b, /* static buffer structure */ unsigned count) -{ /* total number of bytes in use */ +{ /* total number of bytes in use */ bool status = false; /* return value */ if (b) { @@ -133,7 +141,8 @@ bool sbuf_truncate(STATIC_BUFFER * b, /* static buffer structure */ #include "ctest.h" -void testStaticBuffer(Test * pTest) +void testStaticBuffer( + Test * pTest) { STATIC_BUFFER sbuffer; char *data1 = "Joshua"; @@ -186,7 +195,8 @@ void testStaticBuffer(Test * pTest) } #ifdef TEST_STATIC_BUFFER -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -205,5 +215,5 @@ int main(void) return 0; } -#endif /* TEST_STATIC_BUFFER */ -#endif /* TEST */ +#endif /* TEST_STATIC_BUFFER */ +#endif /* TEST */ diff --git a/bacnet-stack/src/timesync.c b/bacnet-stack/src/timesync.c index e285c2e3..1760ef47 100644 --- a/bacnet-stack/src/timesync.c +++ b/bacnet-stack/src/timesync.c @@ -39,12 +39,14 @@ #include "timesync.h" /* encode service */ -int timesync_encode_apdu_service(uint8_t * apdu, +int timesync_encode_apdu_service( + uint8_t * apdu, BACNET_UNCONFIRMED_SERVICE service, - BACNET_DATE * my_date, BACNET_TIME * my_time) + BACNET_DATE * my_date, + BACNET_TIME * my_time) { - int len = 0; /* length of each encoding */ - int apdu_len = 0; /* total length of the apdu, return value */ + int len = 0; /* length of each encoding */ + int apdu_len = 0; /* total length of the apdu, return value */ if (apdu && my_date && my_time) { apdu[0] = PDU_TYPE_UNCONFIRMED_SERVICE_REQUEST; @@ -59,23 +61,30 @@ int timesync_encode_apdu_service(uint8_t * apdu, return apdu_len; } -int timesync_utc_encode_apdu(uint8_t * apdu, - BACNET_DATE * my_date, BACNET_TIME * my_time) +int timesync_utc_encode_apdu( + uint8_t * apdu, + BACNET_DATE * my_date, + BACNET_TIME * my_time) { return timesync_encode_apdu_service(apdu, SERVICE_UNCONFIRMED_UTC_TIME_SYNCHRONIZATION, my_date, my_time); } -int timesync_encode_apdu(uint8_t * apdu, - BACNET_DATE * my_date, BACNET_TIME * my_time) +int timesync_encode_apdu( + uint8_t * apdu, + BACNET_DATE * my_date, + BACNET_TIME * my_time) { return timesync_encode_apdu_service(apdu, SERVICE_UNCONFIRMED_TIME_SYNCHRONIZATION, my_date, my_time); } /* decode the service request only */ -int timesync_decode_service_request(uint8_t * apdu, - unsigned apdu_len, BACNET_DATE * my_date, BACNET_TIME * my_time) +int timesync_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + BACNET_DATE * my_date, + BACNET_TIME * my_time) { int len = 0; uint8_t tag_number = 0; @@ -84,16 +93,14 @@ int timesync_decode_service_request(uint8_t * apdu, if (apdu_len && my_date && my_time) { /* date */ len += - decode_tag_number_and_value(&apdu[len], &tag_number, - &len_value); + decode_tag_number_and_value(&apdu[len], &tag_number, &len_value); if (tag_number == BACNET_APPLICATION_TAG_DATE) { len += decode_date(&apdu[len], my_date); } else return -1; /* time */ len += - decode_tag_number_and_value(&apdu[len], &tag_number, - &len_value); + decode_tag_number_and_value(&apdu[len], &tag_number, &len_value); if (tag_number == BACNET_APPLICATION_TAG_TIME) { len += decode_bacnet_time(&apdu[len], my_time); } else @@ -108,9 +115,12 @@ int timesync_decode_service_request(uint8_t * apdu, #include #include "ctest.h" -int timesync_decode_apdu_service(uint8_t * apdu, +int timesync_decode_apdu_service( + uint8_t * apdu, BACNET_UNCONFIRMED_SERVICE service, - unsigned apdu_len, BACNET_DATE * my_date, BACNET_TIME * my_time) + unsigned apdu_len, + BACNET_DATE * my_date, + BACNET_TIME * my_time) { int len = 0; @@ -130,24 +140,31 @@ int timesync_decode_apdu_service(uint8_t * apdu, return len; } -int timesync_utc_decode_apdu(uint8_t * apdu, - unsigned apdu_len, BACNET_DATE * my_date, BACNET_TIME * my_time) +int timesync_utc_decode_apdu( + uint8_t * apdu, + unsigned apdu_len, + BACNET_DATE * my_date, + BACNET_TIME * my_time) { return timesync_decode_apdu_service(apdu, SERVICE_UNCONFIRMED_UTC_TIME_SYNCHRONIZATION, apdu_len, my_date, my_time); } -int timesync_decode_apdu(uint8_t * apdu, - unsigned apdu_len, BACNET_DATE * my_date, BACNET_TIME * my_time) +int timesync_decode_apdu( + uint8_t * apdu, + unsigned apdu_len, + BACNET_DATE * my_date, + BACNET_TIME * my_time) { return timesync_decode_apdu_service(apdu, - SERVICE_UNCONFIRMED_TIME_SYNCHRONIZATION, - apdu_len, my_date, my_time); + SERVICE_UNCONFIRMED_TIME_SYNCHRONIZATION, apdu_len, my_date, my_time); } -void testTimeSyncData(Test * pTest, - BACNET_DATE * my_date, BACNET_TIME * my_time) +void testTimeSyncData( + Test * pTest, + BACNET_DATE * my_date, + BACNET_TIME * my_time) { uint8_t apdu[480] = { 0 }; int len = 0; @@ -166,23 +183,22 @@ void testTimeSyncData(Test * pTest, len = timesync_utc_encode_apdu(&apdu[0], my_date, my_time); ct_test(pTest, len != 0); apdu_len = len; - len = - timesync_utc_decode_apdu(&apdu[0], apdu_len, &test_date, - &test_time); + len = timesync_utc_decode_apdu(&apdu[0], apdu_len, &test_date, &test_time); ct_test(pTest, len != -1); ct_test(pTest, datetime_compare_time(my_time, &test_time) == 0); ct_test(pTest, datetime_compare_date(my_date, &test_date) == 0); } -void testTimeSync(Test * pTest) +void testTimeSync( + Test * pTest) { BACNET_DATE bdate; BACNET_TIME btime; - bdate.year = 2006; /* AD */ - bdate.month = 4; /* 1=Jan */ - bdate.day = 11; /* 1..31 */ - bdate.wday = 1; /* 1=Monday */ + bdate.year = 2006; /* AD */ + bdate.month = 4; /* 1=Jan */ + bdate.day = 11; /* 1..31 */ + bdate.wday = 1; /* 1=Monday */ btime.hour = 7; btime.min = 0; @@ -193,7 +209,8 @@ void testTimeSync(Test * pTest) } #ifdef TEST_TIMESYNC -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -210,5 +227,5 @@ int main(void) return 0; } -#endif /* TEST_WHOIS */ -#endif /* TEST */ +#endif /* TEST_WHOIS */ +#endif /* TEST */ diff --git a/bacnet-stack/src/tsm.c b/bacnet-stack/src/tsm.c index bb554b2c..ba8edcb9 100644 --- a/bacnet-stack/src/tsm.c +++ b/bacnet-stack/src/tsm.c @@ -62,9 +62,10 @@ static BACNET_TSM_DATA TSM_List[MAX_TSM_TRANSACTIONS]; /* returns MAX_TSM_TRANSACTIONS if not found */ -static uint8_t tsm_find_invokeID_index(uint8_t invokeID) +static uint8_t tsm_find_invokeID_index( + uint8_t invokeID) { - unsigned i = 0; /* counter */ + unsigned i = 0; /* counter */ uint8_t index = MAX_TSM_TRANSACTIONS; /* return value */ for (i = 0; i < MAX_TSM_TRANSACTIONS; i++) { @@ -77,9 +78,10 @@ static uint8_t tsm_find_invokeID_index(uint8_t invokeID) return index; } -static uint8_t tsm_find_first_free_index(void) +static uint8_t tsm_find_first_free_index( + void) { - unsigned i = 0; /* counter */ + unsigned i = 0; /* counter */ uint8_t index = MAX_TSM_TRANSACTIONS; /* return value */ for (i = 0; i < MAX_TSM_TRANSACTIONS; i++) { @@ -92,10 +94,11 @@ static uint8_t tsm_find_first_free_index(void) return index; } -bool tsm_transaction_available(void) +bool tsm_transaction_available( + void) { bool status = false; /* return value */ - unsigned i = 0; /* counter */ + unsigned i = 0; /* counter */ for (i = 0; i < MAX_TSM_TRANSACTIONS; i++) { if (TSM_List[i].InvokeID == 0) { @@ -108,10 +111,11 @@ bool tsm_transaction_available(void) return status; } -uint8_t tsm_transaction_idle_count(void) +uint8_t tsm_transaction_idle_count( + void) { - uint8_t count = 0; /* return value */ - unsigned i = 0; /* counter */ + uint8_t count = 0; /* return value */ + unsigned i = 0; /* counter */ for (i = 0; i < MAX_TSM_TRANSACTIONS; i++) { if ((TSM_List[i].InvokeID == 0) && @@ -127,7 +131,8 @@ uint8_t tsm_transaction_idle_count(void) /* gets the next free invokeID, and reserves a spot in the table returns 0 if none are available */ -uint8_t tsm_next_free_invokeID(void) +uint8_t tsm_next_free_invokeID( + void) { static uint8_t current_invokeID = 1; /* incremented... */ uint8_t index = 0; @@ -154,7 +159,7 @@ uint8_t tsm_next_free_invokeID(void) current_invokeID = 1; } } - } else { + } else { /* found! This invokeID is already used */ /* try next one */ current_invokeID++; @@ -169,9 +174,12 @@ uint8_t tsm_next_free_invokeID(void) return invokeID; } -void tsm_set_confirmed_unsegmented_transaction(uint8_t invokeID, - BACNET_ADDRESS * dest, BACNET_NPDU_DATA * ndpu_data, - uint8_t * apdu, uint16_t apdu_len) +void tsm_set_confirmed_unsegmented_transaction( + uint8_t invokeID, + BACNET_ADDRESS * dest, + BACNET_NPDU_DATA * ndpu_data, + uint8_t * apdu, + uint16_t apdu_len) { uint16_t j = 0; uint8_t index; @@ -199,9 +207,12 @@ void tsm_set_confirmed_unsegmented_transaction(uint8_t invokeID, /* used to retrieve the transaction payload */ /* if we wanted to find out what we sent (i.e. when we get an ack) */ -bool tsm_get_transaction_pdu(uint8_t invokeID, - BACNET_ADDRESS * dest, BACNET_NPDU_DATA * ndpu_data, - uint8_t * apdu, uint16_t * apdu_len) +bool tsm_get_transaction_pdu( + uint8_t invokeID, + BACNET_ADDRESS * dest, + BACNET_NPDU_DATA * ndpu_data, + uint8_t * apdu, + uint16_t * apdu_len) { uint16_t j = 0; uint8_t index; @@ -228,9 +239,10 @@ bool tsm_get_transaction_pdu(uint8_t invokeID, } /* called once a millisecond or slower */ -void tsm_timer_milliseconds(uint16_t milliseconds) +void tsm_timer_milliseconds( + uint16_t milliseconds) { - unsigned i = 0; /* counter */ + unsigned i = 0; /* counter */ int bytes_sent = 0; for (i = 0; i < MAX_TSM_TRANSACTIONS; i++) { @@ -259,7 +271,8 @@ void tsm_timer_milliseconds(uint16_t milliseconds) } /* frees the invokeID and sets its state to IDLE */ -void tsm_free_invoke_id(uint8_t invokeID) +void tsm_free_invoke_id( + uint8_t invokeID) { uint8_t index; @@ -271,7 +284,8 @@ void tsm_free_invoke_id(uint8_t invokeID) } /* check if the invoke ID has been made free */ -bool tsm_invoke_id_free(uint8_t invokeID) +bool tsm_invoke_id_free( + uint8_t invokeID) { bool status = true; uint8_t index; @@ -284,7 +298,8 @@ bool tsm_invoke_id_free(uint8_t invokeID) } /* see if the invoke ID has failed get a confirmation */ -bool tsm_invoke_id_failed(uint8_t invokeID) +bool tsm_invoke_id_failed( + uint8_t invokeID) { bool status = false; uint8_t index; @@ -310,8 +325,11 @@ bool tsm_invoke_id_failed(uint8_t invokeID) bool I_Am_Request = true; /* dummy function stubs */ -int datalink_send_pdu(BACNET_ADDRESS * dest, - BACNET_NPDU_DATA * npdu_data, uint8_t * pdu, unsigned pdu_len) +int datalink_send_pdu( + BACNET_ADDRESS * dest, + BACNET_NPDU_DATA * npdu_data, + uint8_t * pdu, + unsigned pdu_len) { (void) dest; (void) npdu_data; @@ -322,19 +340,22 @@ int datalink_send_pdu(BACNET_ADDRESS * dest, } /* dummy function stubs */ -void datalink_get_broadcast_address(BACNET_ADDRESS * dest) +void datalink_get_broadcast_address( + BACNET_ADDRESS * dest) { (void) dest; } -void testTSM(Test * pTest) +void testTSM( + Test * pTest) { /* FIXME: add some unit testing... */ return; } #ifdef TEST_TSM -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -351,6 +372,6 @@ int main(void) return 0; } -#endif /* TEST_TSM */ -#endif /* TEST */ +#endif /* TEST_TSM */ +#endif /* TEST */ #endif /* MAX_TSM_TRANSACTIONS */ diff --git a/bacnet-stack/src/whohas.c b/bacnet-stack/src/whohas.c index 928b80a5..2a13efaf 100644 --- a/bacnet-stack/src/whohas.c +++ b/bacnet-stack/src/whohas.c @@ -39,10 +39,12 @@ /* encode service - use -1 for limit for unlimited */ -int whohas_encode_apdu(uint8_t * apdu, BACNET_WHO_HAS_DATA * data) +int whohas_encode_apdu( + uint8_t * apdu, + BACNET_WHO_HAS_DATA * data) { - int len = 0; /* length of each encoding */ - int apdu_len = 0; /* total length of the apdu, return value */ + int len = 0; /* length of each encoding */ + int apdu_len = 0; /* total length of the apdu, return value */ if (apdu && data) { apdu[0] = PDU_TYPE_UNCONFIRMED_SERVICE_REQUEST; @@ -53,9 +55,7 @@ int whohas_encode_apdu(uint8_t * apdu, BACNET_WHO_HAS_DATA * data) && (data->low_limit <= BACNET_MAX_INSTANCE) && (data->high_limit >= 0) && (data->high_limit <= BACNET_MAX_INSTANCE)) { - len = - encode_context_unsigned(&apdu[apdu_len], 0, - data->low_limit); + len = encode_context_unsigned(&apdu[apdu_len], 0, data->low_limit); apdu_len += len; len = encode_context_unsigned(&apdu[apdu_len], 1, data->high_limit); @@ -78,8 +78,10 @@ int whohas_encode_apdu(uint8_t * apdu, BACNET_WHO_HAS_DATA * data) } /* decode the service request only */ -int whohas_decode_service_request(uint8_t * apdu, - unsigned apdu_len, BACNET_WHO_HAS_DATA * data) +int whohas_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + BACNET_WHO_HAS_DATA * data) { int len = 0; uint8_t tag_number = 0; @@ -142,8 +144,10 @@ int whohas_decode_service_request(uint8_t * apdu, #include #include "ctest.h" -int whohas_decode_apdu(uint8_t * apdu, - unsigned apdu_len, BACNET_WHO_HAS_DATA * data) +int whohas_decode_apdu( + uint8_t * apdu, + unsigned apdu_len, + BACNET_WHO_HAS_DATA * data) { int len = 0; @@ -162,7 +166,9 @@ int whohas_decode_apdu(uint8_t * apdu, return len; } -void testWhoHasData(Test * pTest, BACNET_WHO_HAS_DATA * data) +void testWhoHasData( + Test * pTest, + BACNET_WHO_HAS_DATA * data) { uint8_t apdu[480] = { 0 }; int len = 0; @@ -192,7 +198,8 @@ void testWhoHasData(Test * pTest, BACNET_WHO_HAS_DATA * data) } } -void testWhoHas(Test * pTest) +void testWhoHas( + Test * pTest) { BACNET_WHO_HAS_DATA data; @@ -227,7 +234,8 @@ void testWhoHas(Test * pTest) } #ifdef TEST_WHOHAS -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -244,5 +252,5 @@ int main(void) return 0; } -#endif /* TEST_WHOIS */ -#endif /* TEST */ +#endif /* TEST_WHOIS */ +#endif /* TEST */ diff --git a/bacnet-stack/src/whois.c b/bacnet-stack/src/whois.c index 4e7b78d7..0b6107f5 100644 --- a/bacnet-stack/src/whois.c +++ b/bacnet-stack/src/whois.c @@ -37,11 +37,13 @@ #include "bacdef.h" /* encode I-Am service - use -1 for limit if you want unlimited */ -int whois_encode_apdu(uint8_t * apdu, - int32_t low_limit, int32_t high_limit) +int whois_encode_apdu( + uint8_t * apdu, + int32_t low_limit, + int32_t high_limit) { - int len = 0; /* length of each encoding */ - int apdu_len = 0; /* total length of the apdu, return value */ + int len = 0; /* length of each encoding */ + int apdu_len = 0; /* total length of the apdu, return value */ if (apdu) { apdu[0] = PDU_TYPE_UNCONFIRMED_SERVICE_REQUEST; @@ -61,8 +63,11 @@ int whois_encode_apdu(uint8_t * apdu, } /* decode the service request only */ -int whois_decode_service_request(uint8_t * apdu, - unsigned apdu_len, int32_t * pLow_limit, int32_t * pHigh_limit) +int whois_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + int32_t * pLow_limit, + int32_t * pHigh_limit) { int len = 0; uint8_t tag_number = 0; @@ -72,8 +77,7 @@ int whois_decode_service_request(uint8_t * apdu, /* optional limits - must be used as a pair */ if (apdu_len) { len += - decode_tag_number_and_value(&apdu[len], &tag_number, - &len_value); + decode_tag_number_and_value(&apdu[len], &tag_number, &len_value); if (tag_number != 0) return -1; len += decode_unsigned(&apdu[len], len_value, &decoded_value); @@ -82,8 +86,7 @@ int whois_decode_service_request(uint8_t * apdu, *pLow_limit = decoded_value; } len += - decode_tag_number_and_value(&apdu[len], &tag_number, - &len_value); + decode_tag_number_and_value(&apdu[len], &tag_number, &len_value); if (tag_number != 1) return -1; len += decode_unsigned(&apdu[len], len_value, &decoded_value); @@ -101,8 +104,11 @@ int whois_decode_service_request(uint8_t * apdu, #include #include "ctest.h" -int whois_decode_apdu(uint8_t * apdu, - unsigned apdu_len, int32_t * pLow_limit, int32_t * pHigh_limit) +int whois_decode_apdu( + uint8_t * apdu, + unsigned apdu_len, + int32_t * pLow_limit, + int32_t * pHigh_limit) { int len = 0; @@ -122,7 +128,8 @@ int whois_decode_apdu(uint8_t * apdu, return len; } -void testWhoIs(Test * pTest) +void testWhoIs( + Test * pTest) { uint8_t apdu[480] = { 0 }; int len = 0; @@ -161,7 +168,8 @@ void testWhoIs(Test * pTest) } #ifdef TEST_WHOIS -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -178,5 +186,5 @@ int main(void) return 0; } -#endif /* TEST_WHOIS */ -#endif /* TEST */ +#endif /* TEST_WHOIS */ +#endif /* TEST */ diff --git a/bacnet-stack/src/wp.c b/bacnet-stack/src/wp.c index c71d45e8..a98d74b5 100644 --- a/bacnet-stack/src/wp.c +++ b/bacnet-stack/src/wp.c @@ -39,11 +39,13 @@ #include "wp.h" /* encode service */ -int wp_encode_apdu(uint8_t * apdu, - uint8_t invoke_id, BACNET_WRITE_PROPERTY_DATA * wpdata) +int wp_encode_apdu( + uint8_t * apdu, + uint8_t invoke_id, + BACNET_WRITE_PROPERTY_DATA * wpdata) { - int apdu_len = 0; /* total length of the apdu, return value */ - int len = 0; /* total length of the apdu, return value */ + int apdu_len = 0; /* total length of the apdu, return value */ + int len = 0; /* total length of the apdu, return value */ if (apdu) { apdu[0] = PDU_TYPE_CONFIRMED_SERVICE_REQUEST; @@ -87,17 +89,19 @@ int wp_encode_apdu(uint8_t * apdu, /* decode the service request only */ /* FIXME: there could be various error messages returned using unique values less than zero */ -int wp_decode_service_request(uint8_t * apdu, - unsigned apdu_len, BACNET_WRITE_PROPERTY_DATA * wpdata) +int wp_decode_service_request( + uint8_t * apdu, + unsigned apdu_len, + BACNET_WRITE_PROPERTY_DATA * wpdata) { int len = 0; int tag_len = 0; uint8_t tag_number = 0; uint32_t len_value_type = 0; - int type = 0; /* for decoding */ - int property = 0; /* for decoding */ + int type = 0; /* for decoding */ + int property = 0; /* for decoding */ uint32_t unsigned_value = 0; - int i = 0; /* loop counter */ + int i = 0; /* loop counter */ /* check for value pointers */ if (apdu_len && wpdata) { @@ -105,14 +109,14 @@ int wp_decode_service_request(uint8_t * apdu, if (!decode_is_context_tag(&apdu[len++], 0)) return -1; len += decode_object_id(&apdu[len], &type, &wpdata->object_instance); - wpdata->object_type = (BACNET_OBJECT_TYPE)type; + wpdata->object_type = (BACNET_OBJECT_TYPE) type; /* Tag 1: Property ID */ len += decode_tag_number_and_value(&apdu[len], &tag_number, &len_value_type); if (tag_number != 1) return -1; len += decode_enumerated(&apdu[len], len_value_type, &property); - wpdata->object_property = (BACNET_PROPERTY_ID)property; + wpdata->object_property = (BACNET_PROPERTY_ID) property; /* Tag 2: Optional Array Index */ /* note: decode without incrementing len so we can check for opening tag */ tag_len = decode_tag_number_and_value(&apdu[len], @@ -129,7 +133,7 @@ int wp_decode_service_request(uint8_t * apdu, return -1; /* determine the length of the data blob */ wpdata->application_data_len = bacapp_data_len(&apdu[len], - apdu_len - len, (BACNET_PROPERTY_ID)property); + apdu_len - len, (BACNET_PROPERTY_ID) property); /* a tag number of 3 is not extended so only one octet */ len++; /* copy the data from the APDU */ @@ -169,9 +173,11 @@ int wp_decode_service_request(uint8_t * apdu, #include #include "ctest.h" -int wp_decode_apdu(uint8_t * apdu, +int wp_decode_apdu( + uint8_t * apdu, unsigned apdu_len, - uint8_t * invoke_id, BACNET_WRITE_PROPERTY_DATA * wpdata) + uint8_t * invoke_id, + BACNET_WRITE_PROPERTY_DATA * wpdata) { int len = 0; unsigned offset = 0; @@ -195,7 +201,8 @@ int wp_decode_apdu(uint8_t * apdu, return len; } -void testWritePropertyTag(Test * pTest, +void testWritePropertyTag( + Test * pTest, BACNET_APPLICATION_DATA_VALUE * value) { BACNET_WRITE_PROPERTY_DATA wpdata = { 0 }; @@ -224,52 +231,53 @@ void testWritePropertyTag(Test * pTest, test_data.application_data_len, &test_value); ct_test(pTest, test_value.tag == value->tag); switch (test_value.tag) { - case BACNET_APPLICATION_TAG_NULL: - break; - case BACNET_APPLICATION_TAG_BOOLEAN: - ct_test(pTest, test_value.type.Boolean == value->type.Boolean); - break; - case BACNET_APPLICATION_TAG_UNSIGNED_INT: - ct_test(pTest, test_value.type.Unsigned_Int == - value->type.Unsigned_Int); - break; - case BACNET_APPLICATION_TAG_SIGNED_INT: - ct_test(pTest, test_value.type.Signed_Int == - value->type.Signed_Int); - break; - case BACNET_APPLICATION_TAG_REAL: - ct_test(pTest, test_value.type.Real == value->type.Real); - break; - case BACNET_APPLICATION_TAG_ENUMERATED: - ct_test(pTest, test_value.type.Enumerated == - value->type.Enumerated); - break; - case BACNET_APPLICATION_TAG_DATE: - ct_test(pTest, test_value.type.Date.year == value->type.Date.year); - ct_test(pTest, test_value.type.Date.month == - value->type.Date.month); - ct_test(pTest, test_value.type.Date.day == value->type.Date.day); - ct_test(pTest, test_value.type.Date.wday == value->type.Date.wday); - break; - case BACNET_APPLICATION_TAG_TIME: - ct_test(pTest, test_value.type.Time.hour == value->type.Time.hour); - ct_test(pTest, test_value.type.Time.min == value->type.Time.min); - ct_test(pTest, test_value.type.Time.sec == value->type.Time.sec); - ct_test(pTest, test_value.type.Time.hundredths == - value->type.Time.hundredths); - break; - case BACNET_APPLICATION_TAG_OBJECT_ID: - ct_test(pTest, test_value.type.Object_Id.type == - value->type.Object_Id.type); - ct_test(pTest, test_value.type.Object_Id.instance == - value->type.Object_Id.instance); - break; - default: - break; + case BACNET_APPLICATION_TAG_NULL: + break; + case BACNET_APPLICATION_TAG_BOOLEAN: + ct_test(pTest, test_value.type.Boolean == value->type.Boolean); + break; + case BACNET_APPLICATION_TAG_UNSIGNED_INT: + ct_test(pTest, test_value.type.Unsigned_Int == + value->type.Unsigned_Int); + break; + case BACNET_APPLICATION_TAG_SIGNED_INT: + ct_test(pTest, test_value.type.Signed_Int == + value->type.Signed_Int); + break; + case BACNET_APPLICATION_TAG_REAL: + ct_test(pTest, test_value.type.Real == value->type.Real); + break; + case BACNET_APPLICATION_TAG_ENUMERATED: + ct_test(pTest, test_value.type.Enumerated == + value->type.Enumerated); + break; + case BACNET_APPLICATION_TAG_DATE: + ct_test(pTest, test_value.type.Date.year == value->type.Date.year); + ct_test(pTest, test_value.type.Date.month == + value->type.Date.month); + ct_test(pTest, test_value.type.Date.day == value->type.Date.day); + ct_test(pTest, test_value.type.Date.wday == value->type.Date.wday); + break; + case BACNET_APPLICATION_TAG_TIME: + ct_test(pTest, test_value.type.Time.hour == value->type.Time.hour); + ct_test(pTest, test_value.type.Time.min == value->type.Time.min); + ct_test(pTest, test_value.type.Time.sec == value->type.Time.sec); + ct_test(pTest, test_value.type.Time.hundredths == + value->type.Time.hundredths); + break; + case BACNET_APPLICATION_TAG_OBJECT_ID: + ct_test(pTest, test_value.type.Object_Id.type == + value->type.Object_Id.type); + ct_test(pTest, test_value.type.Object_Id.instance == + value->type.Object_Id.instance); + break; + default: + break; } } -void testWriteProperty(Test * pTest) +void testWriteProperty( + Test * pTest) { BACNET_APPLICATION_DATA_VALUE value; @@ -346,12 +354,14 @@ void testWriteProperty(Test * pTest) } #ifdef TEST_WRITE_PROPERTY -uint16_t Device_Max_APDU_Length_Accepted(void) +uint16_t Device_Max_APDU_Length_Accepted( + void) { return MAX_APDU; } -int main(void) +int main( + void) { Test *pTest; bool rc; @@ -368,5 +378,5 @@ int main(void) return 0; } -#endif /* TEST_WRITE_PROPERTY */ -#endif /* TEST */ +#endif /* TEST_WRITE_PROPERTY */ +#endif /* TEST */ diff --git a/bacnet-stack/test/ctest.c b/bacnet-stack/test/ctest.c index 94739b79..f8626e55 100644 --- a/bacnet-stack/test/ctest.c +++ b/bacnet-stack/test/ctest.c @@ -9,7 +9,9 @@ /* Number of tests to hold incrementally */ enum { CHUNK = 10 }; -Test *ct_create(const char *name, void (*init) (Test *)) +Test *ct_create( + const char *name, + void (*init) (Test *)) { int backOutLevel = 0; Test *pTest = malloc(sizeof(Test)); @@ -36,12 +38,12 @@ Test *ct_create(const char *name, void (*init) (Test *)) /* Back-out allocations if memory failed: */ if (backOutLevel) { switch (backOutLevel) { - case 2: - free(pTest->pTestFuns); - pTest->pTestFuns = NULL; - case 1: - free(pTest); - pTest = NULL; + case 2: + free(pTest->pTestFuns); + pTest->pTestFuns = NULL; + case 1: + free(pTest); + pTest = NULL; } } else if (init) { assert(pTest); @@ -50,7 +52,8 @@ Test *ct_create(const char *name, void (*init) (Test *)) return pTest; } -void ct_destroy(Test * pTest) +void ct_destroy( + Test * pTest) { assert(pTest); assert(pTest->pTestFuns); @@ -62,7 +65,9 @@ void ct_destroy(Test * pTest) free(pTest); } -bool ct_addTestFunction(Test * pTest, TestFunc tfun) +bool ct_addTestFunction( + Test * pTest, + TestFunc tfun) { assert(pTest); assert(pTest->pTestFuns); @@ -80,17 +85,21 @@ bool ct_addTestFunction(Test * pTest, TestFunc tfun) return true; } -void ct_setStream(Test * pTest, FILE * pStream) +void ct_setStream( + Test * pTest, + FILE * pStream) { pTest->pStream = pStream; } -FILE *ct_getStream(Test * pTest) +FILE *ct_getStream( + Test * pTest) { return pTest->pStream; } -long ct_report(Test * pTest) +long ct_report( + Test * pTest) { assert(pTest); if (pTest->pStream) { @@ -102,14 +111,19 @@ long ct_report(Test * pTest) } -void ct_succeed(Test * pTest) +void ct_succeed( + Test * pTest) { assert(pTest); ++pTest->nPass; } -void ct_do_test(Test * pTest, const char *str, - bool cond, const char *file, long line) +void ct_do_test( + Test * pTest, + const char *str, + bool cond, + const char *file, + long line) { assert(pTest); if (!cond) @@ -118,30 +132,36 @@ void ct_do_test(Test * pTest, const char *str, ct_succeed(pTest); } -void ct_do_fail(Test * pTest, const char *str, const char *file, long line) +void ct_do_fail( + Test * pTest, + const char *str, + const char *file, + long line) { assert(pTest); ++pTest->nFail; if (pTest->pStream) { fprintf(pTest->pStream, - "%s failure: (%s), %s (line %ld)\n", - pTest->name, str, file, line); + "%s failure: (%s), %s (line %ld)\n", pTest->name, str, file, line); } } -long ct_getNumPassed(Test * pTest) +long ct_getNumPassed( + Test * pTest) { assert(pTest); return pTest->nPass; } -long ct_getNumFailed(Test * pTest) +long ct_getNumFailed( + Test * pTest) { assert(pTest); return pTest->nFail; } -long ct_run(Test * pTest) +long ct_run( + Test * pTest) { size_t testNum; assert(pTest); @@ -150,19 +170,22 @@ long ct_run(Test * pTest) return pTest->nFail; } -void ct_reset(Test * pTest) +void ct_reset( + Test * pTest) { assert(pTest); pTest->nFail = pTest->nPass = 0; } -const char *ct_getName(Test * pTest) +const char *ct_getName( + Test * pTest) { assert(pTest); return (pTest->name); } -long ct_getNumTests(Test * pTest) +long ct_getNumTests( + Test * pTest) { assert(pTest); return pTest->nTests; diff --git a/bacnet-stack/test/ctest.h b/bacnet-stack/test/ctest.h index 9be0bbe6..ddb0d059 100644 --- a/bacnet-stack/test/ctest.h +++ b/bacnet-stack/test/ctest.h @@ -15,7 +15,9 @@ typedef struct _Test Test; -typedef void (*TestFunc) (Test *); +typedef void ( + *TestFunc) ( + Test *); struct _Test { char *name; @@ -31,27 +33,50 @@ struct _Test { extern "C" { #endif - Test *ct_create(const char *name, void (*init) (Test *)); - void ct_destroy(Test * pTest); + Test *ct_create( + const char *name, + void (*init) (Test *)); + void ct_destroy( + Test * pTest); - const char *ct_getName(Test * pTest); - long ct_getNumPassed(Test * pTest); - long ct_getNumFailed(Test * pTest); - long ct_getNumTests(Test * pTest); - FILE *ct_getStream(Test * pTest); - void ct_setStream(Test * pTest, FILE * stream); + const char *ct_getName( + Test * pTest); + long ct_getNumPassed( + Test * pTest); + long ct_getNumFailed( + Test * pTest); + long ct_getNumTests( + Test * pTest); + FILE *ct_getStream( + Test * pTest); + void ct_setStream( + Test * pTest, + FILE * stream); - bool ct_addTestFunction(Test * pTest, TestFunc tfun); - void ct_succeed(Test * pTest); - long ct_run(Test * pTest); - long ct_report(Test * pTest); - void ct_reset(Test * pTest); + bool ct_addTestFunction( + Test * pTest, + TestFunc tfun); + void ct_succeed( + Test * pTest); + long ct_run( + Test * pTest); + long ct_report( + Test * pTest); + void ct_reset( + Test * pTest); /* Not intended for end-users: */ - void ct_do_test(Test * pTest, const char *str, - bool cond, const char *file, long line); - void ct_do_fail(Test * pTest, const char *str, - const char *file, long line); + void ct_do_test( + Test * pTest, + const char *str, + bool cond, + const char *file, + long line); + void ct_do_fail( + Test * pTest, + const char *str, + const char *file, + long line); #ifdef __cplusplus }