From fdd49f1791e36b84f7a421ee0edbeedc72619d2c Mon Sep 17 00:00:00 2001 From: Steve Karg Date: Thu, 30 Apr 2020 10:13:11 -0500 Subject: [PATCH] Feature/make pretty apps and ports (#80) * Added pretty-apps and pretty-ports make targets * pretty-fied apps folder C files * Pretty-fied ports folder C and H files Co-authored-by: Steve Karg --- Makefile | 10 + apps/piface/device.c | 4 +- apps/ptransfer/main.c | 3 +- apps/readfdt/main.c | 21 +- apps/router/ipmodule.c | 4 +- ports/arduino_uno/apdu.c | 25 +- ports/arduino_uno/av.c | 95 ++- ports/arduino_uno/bip-init.c | 13 +- ports/arduino_uno/bip.c | 111 ++- ports/arduino_uno/bv.c | 104 ++- ports/arduino_uno/bvlc-arduino.c | 38 +- ports/arduino_uno/device.c | 242 ++++--- ports/arduino_uno/h_rp.c | 105 ++- ports/arduino_uno/h_whois.c | 61 +- ports/arduino_uno/h_wp.c | 104 ++- ports/arduino_uno/main.c | 8 +- ports/arduino_uno/uart.c | 9 +- ports/at91sam7s/ai.c | 114 ++- ports/at91sam7s/av.c | 135 ++-- ports/at91sam7s/bi.c | 108 ++- ports/at91sam7s/blinker.c | 16 +- ports/at91sam7s/bv.c | 129 ++-- ports/at91sam7s/device.c | 355 ++++------ ports/at91sam7s/dlmstp.c | 183 +++-- ports/at91sam7s/init.c | 60 +- ports/at91sam7s/irq.c | 42 +- ports/at91sam7s/isr.c | 41 +- ports/at91sam7s/main.c | 92 ++- ports/at91sam7s/rs485.c | 190 +++-- ports/at91sam7s/timer.c | 159 +++-- ports/atmega168/ai.c | 87 ++- ports/atmega168/apdu.c | 30 +- ports/atmega168/av.c | 115 ++- ports/atmega168/bv.c | 116 ++- ports/atmega168/device.c | 133 ++-- ports/atmega168/dlmstp.c | 213 +++--- ports/atmega168/h_rp.c | 99 ++- ports/atmega168/h_whois.c | 57 +- ports/atmega168/h_wp.c | 99 ++- ports/atmega168/main.c | 61 +- ports/atmega168/rs485.c | 196 +++--- ports/atmega168/stack.c | 78 ++- ports/atmega168/timer.c | 59 +- ports/bdk-atxx4-mstp/adc-block.c | 122 ++-- ports/bdk-atxx4-mstp/adc.c | 88 ++- ports/bdk-atxx4-mstp/ai.c | 111 ++- ports/bdk-atxx4-mstp/av.c | 132 ++-- ports/bdk-atxx4-mstp/bacnet.c | 89 ++- ports/bdk-atxx4-mstp/bi.c | 108 ++- ports/bdk-atxx4-mstp/bname.c | 106 ++- ports/bdk-atxx4-mstp/bo.c | 230 +++--- ports/bdk-atxx4-mstp/bootloader/main.c | 296 ++++---- ports/bdk-atxx4-mstp/bootloader/serial.c | 59 +- ports/bdk-atxx4-mstp/device.c | 351 ++++------ ports/bdk-atxx4-mstp/dlmstp.c | 193 +++-- ports/bdk-atxx4-mstp/eeprom.c | 60 +- ports/bdk-atxx4-mstp/fuses.c | 112 +-- ports/bdk-atxx4-mstp/init.c | 49 +- ports/bdk-atxx4-mstp/input.c | 69 +- ports/bdk-atxx4-mstp/led.c | 152 ++-- ports/bdk-atxx4-mstp/main.c | 49 +- ports/bdk-atxx4-mstp/mstimer-init.c | 106 ++- ports/bdk-atxx4-mstp/rs485.c | 227 +++--- ports/bdk-atxx4-mstp/seeprom.c | 176 ++--- ports/bdk-atxx4-mstp/serial.c | 84 +-- ports/bdk-atxx4-mstp/stack.c | 91 ++- ports/bdk-atxx4-mstp/test.c | 142 ++-- ports/bdk-atxx4-mstp/watchdog.c | 75 +- ports/bsd/bip-init.c | 71 +- ports/bsd/datetime-init.c | 16 +- ports/bsd/main.c | 146 ++-- ports/bsd/mstimer-init.c | 67 +- ports/esp32/src/ai.c | 599 +++++++--------- ports/esp32/src/bip_init.c | 15 +- ports/esp32/src/bo.c | 211 +++--- ports/esp32/src/device.c | 668 +++++++----------- ports/esp32/src/main.c | 147 ++-- ports/linux/arcnet.c | 106 ++- ports/linux/bip6.c | 132 ++-- ports/linux/datetime-init.c | 16 +- ports/linux/dlmstp.c | 302 ++++---- ports/linux/dlmstp_linux.c | 450 ++++++------ ports/linux/ethernet.c | 141 ++-- ports/linux/mstimer-init.c | 67 +- ports/linux/mstpsnap.c | 113 ++- ports/linux/rs485.c | 198 +++--- ports/linux/rx_fsm.c | 111 ++- ports/lwip/bip.c | 146 ++-- ports/pic18f6720/ai.c | 88 ++- ports/pic18f6720/apdu.c | 55 +- ports/pic18f6720/av.c | 109 ++- ports/pic18f6720/bi.c | 83 +-- ports/pic18f6720/bv.c | 105 ++- ports/pic18f6720/device.c | 225 +++--- ports/pic18f6720/dlmstp.c | 130 ++-- ports/pic18f6720/isr.c | 184 +++-- ports/pic18f6720/main.c | 66 +- ports/pic18f6720/mstp.c | 306 ++++---- ports/pic18f6720/rs485.c | 173 +++-- ports/pic18f97j60/ai.c | 88 ++- ports/pic18f97j60/apdu.c | 55 +- ports/pic18f97j60/av.c | 109 ++- ports/pic18f97j60/bi.c | 83 +-- ports/pic18f97j60/bv.c | 105 ++- ports/pic18f97j60/device.c | 225 +++--- ports/pic18f97j60/dlmstp.c | 130 ++-- ports/pic18f97j60/isr.c | 184 +++-- ports/pic18f97j60/main.c | 66 +- ports/pic18f97j60/mstp.c | 306 ++++---- ports/pic18f97j60/rs485.c | 173 +++-- ports/rx62n/bacnet.c | 73 +- ports/rx62n/bo.c | 206 +++--- ports/rx62n/device.c | 369 ++++------ ports/rx62n/ethernet.c | 134 ++-- ports/rx62n/led.c | 139 ++-- ports/rx62n/main.c | 49 +- ports/rx62n/mstimer-init.c | 96 ++- ports/stm32f10x/CMSIS/core_cm3.c | 415 +++++------ ports/stm32f10x/automac.c | 266 ++++--- ports/stm32f10x/bacnet.c | 73 +- ports/stm32f10x/bo.c | 207 +++--- ports/stm32f10x/device.c | 329 ++++----- ports/stm32f10x/dlmstp.c | 192 +++-- ports/stm32f10x/led.c | 264 ++++--- ports/stm32f10x/main.c | 86 ++- ports/stm32f10x/mstimer-init.c | 77 +- ports/stm32f10x/rs485.c | 248 ++++--- ports/stm32f10x/stm32f10x_it.c | 205 +++--- ports/stm32f10x/system_stm32f10x.c | 854 +++++++++++------------ ports/uip/bip.c | 109 ++- ports/win32/bip-init.c | 114 ++- ports/win32/bip6.c | 145 ++-- ports/win32/datetime-init.c | 54 +- ports/win32/dlmstp-mm.c | 247 +++---- ports/win32/dlmstp.c | 243 +++---- ports/win32/ethernet.c | 159 ++--- ports/win32/main.c | 158 ++--- ports/win32/mstimer-init.c | 71 +- ports/win32/rs485.c | 166 ++--- ports/win32/rx_fsm.c | 116 ++- ports/xplained/adc-hdw.c | 120 ++-- ports/xplained/ai.c | 223 +++--- ports/xplained/bacnet.c | 146 ++-- ports/xplained/bname.c | 149 ++-- ports/xplained/device.c | 322 ++++----- ports/xplained/dlmstp.c | 165 +++-- ports/xplained/led.c | 146 ++-- ports/xplained/main.c | 2 +- ports/xplained/mstimer-init.c | 3 +- ports/xplained/nvmdata.c | 18 +- ports/xplained/rs485.c | 70 +- ports/xplained/stack.c | 143 ++-- 152 files changed, 9668 insertions(+), 11674 deletions(-) diff --git a/Makefile b/Makefile index f3b8f46e..2dcd1731 100644 --- a/Makefile +++ b/Makefile @@ -137,6 +137,16 @@ pretty: find ./apps -iname *.h -o -iname *.c -exec \ clang-format -i -style=file -fallback-style=none {} \; +.PHONY: pretty-apps +pretty-apps: + find ./apps -iname *.h -o -iname *.c -exec \ + clang-format -i -style=file -fallback-style=none {} \; + +.PHONY: pretty-ports +pretty-ports: + find ./ports -maxdepth 2 -type f -iname *.h -o -iname *.c -exec \ + clang-format -i -style=file -fallback-style=none {} \; + .PHONY: tidy tidy: find ./src -iname *.h -o -iname *.c -exec \ diff --git a/apps/piface/device.c b/apps/piface/device.c index 814e6f42..c43fd039 100644 --- a/apps/piface/device.c +++ b/apps/piface/device.c @@ -742,8 +742,8 @@ bool Device_Valid_Object_Name(BACNET_CHARACTER_STRING *object_name1, * @param object_instance [in] The object instance number to be looked up. * @return True if found, else False if no such Object in this device. */ -bool Device_Valid_Object_Id - (BACNET_OBJECT_TYPE object_type, uint32_t object_instance) +bool Device_Valid_Object_Id( + BACNET_OBJECT_TYPE object_type, uint32_t object_instance) { bool status = false; /* return value */ struct object_functions *pObject = NULL; diff --git a/apps/ptransfer/main.c b/apps/ptransfer/main.c index 6ab215af..a8c84d05 100644 --- a/apps/ptransfer/main.c +++ b/apps/ptransfer/main.c @@ -249,8 +249,7 @@ int main(int argc, char *argv[]) for (;;) { /* increment timer - exit if timed out */ current_seconds = time(NULL); - if (current_seconds != last_seconds) { - } + if (current_seconds != last_seconds) { } /* returns 0 bytes on timeout */ pdu_len = datalink_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout); diff --git a/apps/readfdt/main.c b/apps/readfdt/main.c index 17e5e75b..b70ab155 100644 --- a/apps/readfdt/main.c +++ b/apps/readfdt/main.c @@ -117,17 +117,16 @@ int main(int argc, char *argv[]) return 0; } if ((argc > 1) && (strcmp(argv[1], "--help") == 0)) { - printf( - "Send a Read-Foreign-Device-Table message to a BBMD.\r\n" - "\r\n" - "IP:\r\n" - "IP address of the BBMD in dotted decimal notation\r\n" - "[port]\r\n" - "optional BACnet/IP port number (default=47808=0xBAC0)\r\n" - "\r\n" - "To send a Read-Foreign-Device-Table message to a BBMD\r\n" - "at 192.168.0.1 using port 47808:\r\n" - "%s 192.168.0.1 47808\r\n", + printf("Send a Read-Foreign-Device-Table message to a BBMD.\r\n" + "\r\n" + "IP:\r\n" + "IP address of the BBMD in dotted decimal notation\r\n" + "[port]\r\n" + "optional BACnet/IP port number (default=47808=0xBAC0)\r\n" + "\r\n" + "To send a Read-Foreign-Device-Table message to a BBMD\r\n" + "at 192.168.0.1 using port 47808:\r\n" + "%s 192.168.0.1 47808\r\n", filename_remove_path(argv[0])); return 0; } diff --git a/apps/router/ipmodule.c b/apps/router/ipmodule.c index 9f4c7d04..2b3a4fd6 100644 --- a/apps/router/ipmodule.c +++ b/apps/router/ipmodule.c @@ -143,8 +143,8 @@ bool dl_ip_init(ROUTER_PORT *port, IP_DATA *ip_data) ip_data->port = htons(port->params.bip_params.port); /* get local address */ - status = - bip_get_local_address_ioctl(port->iface, &ip_data->local_addr, SIOCGIFADDR); + status = bip_get_local_address_ioctl( + port->iface, &ip_data->local_addr, SIOCGIFADDR); if (status < 0) { return false; } diff --git a/ports/arduino_uno/apdu.c b/ports/arduino_uno/apdu.c index c56e4c34..d7b9b978 100644 --- a/ports/arduino_uno/apdu.c +++ b/ports/arduino_uno/apdu.c @@ -60,15 +60,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) + BACNET_CONFIRMED_SERVICE_DATA *service_data, + uint8_t *service_choice, + 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] & BIT(3)) ? true : false; service_data->more_follows = (apdu[0] & BIT(2)) ? true : false; @@ -89,8 +89,8 @@ 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) { @@ -98,13 +98,14 @@ void apdu_handler(BACNET_ADDRESS * src, 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 */ + 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) { @@ -113,8 +114,8 @@ void apdu_handler(BACNET_ADDRESS * src, } #ifdef WRITE_PROPERTY else if (service_choice == SERVICE_CONFIRMED_WRITE_PROPERTY) { - handler_write_property(service_request, - service_request_len, src, &service_data); + handler_write_property(service_request, service_request_len, + src, &service_data); } #endif else { diff --git a/ports/arduino_uno/av.c b/ports/arduino_uno/av.c index d7094e10..14704df1 100644 --- a/ports/arduino_uno/av.c +++ b/ports/arduino_uno/av.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2006 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2006 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* Analog Value Objects - customize for your use */ @@ -31,7 +31,7 @@ #include "bacnet/bacdcode.h" #include "bacnet/bacenum.h" #include "bacnet/bacapp.h" -#include "bacnet/config.h" /* the custom stuff */ +#include "bacnet/config.h" /* the custom stuff */ #include "bacnet/wp.h" #include "bacnet/basic/object/av.h" @@ -80,33 +80,32 @@ char *Analog_Value_Name(uint32_t object_instance) { static char text_string[5] = "AV-"; /* okay for single thread */ - text_string[3] = '0' + (uint8_t) object_instance; + text_string[3] = '0' + (uint8_t)object_instance; return text_string; } /* 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, uint32_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); + apdu_len = encode_application_object_id( + &apdu[0], OBJECT_ANALOG_VALUE, object_instance); break; case PROP_OBJECT_NAME: - characterstring_init_ansi(&char_string, - Analog_Value_Name(object_instance)); + characterstring_init_ansi( + &char_string, Analog_Value_Name(object_instance)); apdu_len = encode_application_character_string(&apdu[0], &char_string); break; @@ -116,9 +115,8 @@ int Analog_Value_Encode_Property_APDU(uint8_t * apdu, break; case PROP_PRESENT_VALUE: object_index = Analog_Value_Instance_To_Index(object_instance); - apdu_len = - encode_application_real(&apdu[0], - AV_Present_Value[object_index]); + apdu_len = encode_application_real( + &apdu[0], AV_Present_Value[object_index]); break; case PROP_STATUS_FLAGS: bitstring_init(&bit_string); @@ -155,11 +153,11 @@ int Analog_Value_Encode_Property_APDU(uint8_t * apdu, } /* 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 */ + bool status = false; /* return value */ unsigned int object_index = 0; int len = 0; BACNET_APPLICATION_DATA_VALUE value; @@ -170,9 +168,8 @@ bool Analog_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, return false; } /* decode the some of the request */ - len = - bacapp_decode_application_data(wp_data->application_data, - wp_data->application_data_len, &value); + len = bacapp_decode_application_data( + wp_data->application_data, wp_data->application_data_len, &value); /* FIXME: len < application_data_len: more data? */ if (len < 0) { /* error while decoding - a value larger than we can handle */ @@ -206,7 +203,7 @@ 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; @@ -218,14 +215,12 @@ void testAnalog_Value(Test * pTest) BACNET_ERROR_CLASS error_class; BACNET_ERROR_CODE error_code; - len = - Analog_Value_Encode_Property_APDU(&apdu[0], instance, + len = Analog_Value_Encode_Property_APDU(&apdu[0], instance, 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); - len = - decode_object_id(&apdu[len], (int *) &decoded_type, &decoded_instance); + len = decode_object_id(&apdu[len], (int *)&decoded_type, &decoded_instance); ct_test(pTest, decoded_type == OBJECT_ANALOG_VALUE); ct_test(pTest, decoded_instance == instance); @@ -245,7 +240,7 @@ int main(void) ct_setStream(pTest, stdout); ct_run(pTest); - (void) ct_report(pTest); + (void)ct_report(pTest); ct_destroy(pTest); return 0; diff --git a/ports/arduino_uno/bip-init.c b/ports/arduino_uno/bip-init.c index 4ea12e5e..3c1165b0 100644 --- a/ports/arduino_uno/bip-init.c +++ b/ports/arduino_uno/bip-init.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 "bacnet/bacdcode.h" #include "bacnet/datalink/bip.h" @@ -71,7 +71,6 @@ long bip_getaddrbyname(const char *host_name) */ void bip_set_interface(char *ifname) { - uint8_t local_address[] = { 0, 0, 0, 0 }; uint8_t broadcast_address[] = { 0, 0, 0, 0 }; uint8_t netmask[] = { 0, 0, 0, 0 }; @@ -87,7 +86,7 @@ void bip_set_interface(char *ifname) /* setup local broadcast address */ getSubnetMask_func(CW5100Class_new(), netmask); - for (int i = 0; i < 4; i++) { //FIXME: IPv4 ? + for (int i = 0; i < 4; i++) { // FIXME: IPv4 ? invertedNetmask[i] = ~netmask[i]; broadcast_address[i] = (local_address[i] | invertedNetmask[i]); } @@ -130,7 +129,7 @@ bool bip_init(char *ifname) /* assumes that the driver has already been initialized */ for (sock_fd = 0; sock_fd < MAX_SOCK_NUM; sock_fd++) { if (readSnSR_func(CW5100Class_new(), sock_fd) == SnSR_CLOSED()) { - socket_func(sock_fd, SnMR_UDP(), (uint16_t) 47808, 0); + socket_func(sock_fd, SnMR_UDP(), (uint16_t)47808, 0); listen_func(sock_fd); isOpen = true; break; @@ -149,7 +148,7 @@ bool bip_init(char *ifname) /** Cleanup and close out the BACnet/IP services by closing the socket. * @ingroup DLBIP - */ + */ void bip_cleanup(void) { int sock_fd = 0; @@ -167,7 +166,7 @@ void bip_cleanup(void) * @param netmask [out] The netmask, in host order. * @return 0 on success, else the error from the ioctl() call. */ -int bip_get_local_netmask(uint8_t * netmask) +int bip_get_local_netmask(uint8_t *netmask) { getSubnetMask_func(CW5100Class_new(), netmask); return 0; diff --git a/ports/arduino_uno/bip.c b/ports/arduino_uno/bip.c index 825a8291..a24d4f1e 100644 --- a/ports/arduino_uno/bip.c +++ b/ports/arduino_uno/bip.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 "bacnet/bacdcode.h" #include "bacnet/bacint.h" @@ -42,42 +42,40 @@ #include "socketWrapper.h" #include "w5100Wrapper.h" - #if PRINT_ENABLED | DEBUG -#include /* for standard i/o, like printing */ +#include /* for standard i/o, like printing */ #endif /** @file bip.c Configuration and Operations for BACnet/IP */ static uint8_t BIP_Socket = MAX_SOCK_NUM; /* port to use - stored in network byte order */ -static uint16_t BIP_Port = 0; /* this will force initialization in demos */ +static uint16_t BIP_Port = 0; /* this will force initialization in demos */ /* IP Address - stored in network byte order */ -//static struct in_addr BIP_Address; +// static struct in_addr BIP_Address; static uint8_t BIP_Address[4] = { 0, 0, 0, 0 }; /* Broadcast Address - stored in network byte order */ -//static struct in_addr BIP_Broadcast_Address; +// static struct in_addr BIP_Broadcast_Address; static uint8_t BIP_Broadcast_Address[4] = { 0, 0, 0, 0 }; /** Converter from uint8_t[4] type address to uint32_t * */ -uint32_t convertBIP_Address2uint32(uint8_t * bip_address) +uint32_t convertBIP_Address2uint32(uint8_t *bip_address) { - return (uint32_t) ((bip_address[0] * 2 ^ 24) + (bip_address[1] * 2 ^ 16) + + return (uint32_t)((bip_address[0] * 2 ^ 24) + (bip_address[1] * 2 ^ 16) + (bip_address[2] * 2 ^ 8) + bip_address[3]); } /** Convert from uint32_t IPv4 address to uint8_t[4] address * */ -void convertUint32Address_2_uint8Address(uint32_t ip, - uint8_t * address) +void convertUint32Address_2_uint8Address(uint32_t ip, uint8_t *address) { - address[0] = (uint8_t) (ip >> 24); - address[1] = (uint8_t) (ip >> 16); - address[2] = (uint8_t) (ip >> 8); - address[3] = (uint8_t) (ip >> 0); + address[0] = (uint8_t)(ip >> 24); + address[1] = (uint8_t)(ip >> 16); + address[2] = (uint8_t)(ip >> 8); + address[3] = (uint8_t)(ip >> 0); } /** Setter for the BACnet/IP socket handle. @@ -103,8 +101,8 @@ bool bip_valid(void) return (BIP_Socket < MAX_SOCK_NUM); } -void bip_set_addr(uint8_t * net_address) -{ /* in network byte order */ +void bip_set_addr(uint8_t *net_address) +{ /* in network byte order */ for (uint8_t i = 0; i < 4; i++) BIP_Address[i] = net_address[i]; } @@ -115,8 +113,8 @@ uint8_t *bip_get_addr(void) return BIP_Address; } -void bip_set_broadcast_addr(uint8_t * net_address) -{ /* in network byte order */ +void bip_set_broadcast_addr(uint8_t *net_address) +{ /* in network byte order */ for (uint8_t i = 0; i < 4; i++) BIP_Broadcast_Address[i] = net_address[i]; } @@ -127,9 +125,8 @@ uint8_t *bip_get_broadcast_addr(void) return BIP_Broadcast_Address; } - void bip_set_port(uint16_t port) -{ /* in network byte order */ +{ /* in network byte order */ BIP_Port = htons(port); } @@ -139,11 +136,11 @@ uint16_t bip_get_port(void) return ntohs(BIP_Port); } -static int bip_decode_bip_address(BACNET_ADDRESS * bac_addr, - uint8_t * address, /* in network format */ +static int bip_decode_bip_address(BACNET_ADDRESS *bac_addr, + uint8_t *address, /* in network format */ - uint16_t * port) -{ /* in network format */ + uint16_t *port) +{ /* in network format */ int len = 0; if (bac_addr) { @@ -164,14 +161,14 @@ static int bip_decode_bip_address(BACNET_ADDRESS * bac_addr, * @param pdu_len [in] Number of bytes in the pdu buffer. * @return 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 */ + 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 */ uint8_t mtu[MAX_MPDU] = { 0 }; int mtu_len = 0; @@ -180,15 +177,15 @@ int bip_send_pdu(BACNET_ADDRESS * dest, /* destination address */ uint8_t address[] = { 0, 0, 0, 0 }; uint16_t port = 0; - (void) npdu_data; + (void)npdu_data; /* assumes that the driver has already been initialized */ if (BIP_Socket < 0) { return BIP_Socket; } mtu[0] = BVLL_TYPE_BACNET_IP; - if ((dest->net == BACNET_BROADCAST_NETWORK) || ((dest->net > 0) && - (dest->len == 0)) || (dest->mac_len == 0)) { + if ((dest->net == BACNET_BROADCAST_NETWORK) || + ((dest->net > 0) && (dest->len == 0)) || (dest->mac_len == 0)) { /* broadcast */ for (uint8_t i = 0; i < 4; i++) address[i] = BIP_Broadcast_Address[i]; @@ -196,16 +193,14 @@ int bip_send_pdu(BACNET_ADDRESS * dest, /* destination address */ mtu[1] = BVLC_ORIGINAL_BROADCAST_NPDU; #ifdef DEBUG fprintf(stderr, "Send Broadcast NPDU to %d.%d.%d.%d:%d\n", address[0], - address[1] - , address[2], address[3], port); + address[1], address[2], address[3], port); #endif } else if (dest->mac_len == 6) { bip_decode_bip_address(dest, address, &port); mtu[1] = BVLC_ORIGINAL_UNICAST_NPDU; #ifdef DEBUG fprintf(stderr, "Send Unicast NPDU to %d.%d.%d.%d:%d\n", address[0], - address[1] - , address[2], address[3], port); + address[1], address[2], address[3], port); #endif } else { /* invalid address */ @@ -213,9 +208,8 @@ int bip_send_pdu(BACNET_ADDRESS * dest, /* destination address */ } mtu_len = 2; - mtu_len += - encode_unsigned16(&mtu[mtu_len], - (uint16_t) (pdu_len + 4 /*inclusive */ )); + mtu_len += encode_unsigned16( + &mtu[mtu_len], (uint16_t)(pdu_len + 4 /*inclusive */)); memcpy(&mtu[mtu_len], pdu, pdu_len); mtu_len += pdu_len; @@ -224,8 +218,7 @@ int bip_send_pdu(BACNET_ADDRESS * dest, /* destination address */ #endif /* Send the packet */ - bytes_sent = - sendto_func(BIP_Socket, mtu, (uint16_t) mtu_len, address, port); + bytes_sent = sendto_func(BIP_Socket, mtu, (uint16_t)mtu_len, address, port); return bytes_sent; } @@ -236,21 +229,22 @@ int bip_send_pdu(BACNET_ADDRESS * dest, /* destination address */ * * @param src [out] Source of the packet - who should receive any response. * @param pdu [out] A buffer to hold the PDU portion of the received packet, - * after the BVLC portion has been stripped off. + * after the BVLC portion has been stripped + * off. * @param max_pdu [in] Size of the pdu[] buffer. * @param timeout [in] The number of milliseconds to wait for a packet. * @return The number of octets (remaining) in the PDU, or zero on failure. */ -uint16_t bip_receive(BACNET_ADDRESS * src, /* source address */ +uint16_t bip_receive(BACNET_ADDRESS *src, /* source address */ - uint8_t * pdu, /* PDU data */ + uint8_t *pdu, /* PDU data */ - uint16_t max_pdu, /* amount of space available in the PDU */ + uint16_t max_pdu, /* amount of space available in the PDU */ unsigned timeout) { int received_bytes = 0; - uint16_t pdu_len = 0; /* return value */ + uint16_t pdu_len = 0; /* return value */ uint8_t src_addr[] = { 0, 0, 0, 0 }; uint16_t src_port = 0; uint16_t i = 0; @@ -263,9 +257,8 @@ uint16_t bip_receive(BACNET_ADDRESS * src, /* source address */ if (getRXReceivedSize_func(CW5100Class_new(), BIP_Socket)) { memcpy(&src_addr, &src->mac[0], 4); memcpy(&src_port, &src->mac[4], 2); - received_bytes = - (int) recvfrom_func(BIP_Socket, &pdu[0], max_pdu, src_addr, - &src_port); + received_bytes = (int)recvfrom_func( + BIP_Socket, &pdu[0], max_pdu, src_addr, &src_port); } /* See if there is a problem */ @@ -289,7 +282,7 @@ uint16_t bip_receive(BACNET_ADDRESS * src, /* source address */ return 0; } - function = bvlc_get_function_code(); /* aka, pdu[1] */ + function = bvlc_get_function_code(); /* aka, pdu[1] */ if ((function == BVLC_ORIGINAL_UNICAST_NPDU) || (function == BVLC_ORIGINAL_BROADCAST_NPDU)) { /* ignore messages from me */ @@ -312,7 +305,7 @@ uint16_t bip_receive(BACNET_ADDRESS * src, /* source address */ /* FIXME: check destination address */ /* see if it is broadcast or for us */ /* decode the length of the PDU - length is inclusive of BVLC */ - (void) decode_unsigned16(&pdu[2], &pdu_len); + (void)decode_unsigned16(&pdu[2], &pdu_len); /* subtract off the BVLC header */ pdu_len -= 4; if (pdu_len < max_pdu) { @@ -355,7 +348,7 @@ uint16_t bip_receive(BACNET_ADDRESS * src, /* source address */ /* FIXME: check destination address */ /* see if it is broadcast or for us */ /* decode the length of the PDU - length is inclusive of BVLC */ - (void) decode_unsigned16(&pdu[2], &pdu_len); + (void)decode_unsigned16(&pdu[2], &pdu_len); /* subtract off the BVLC header */ pdu_len -= 10; if (pdu_len < max_pdu) { @@ -374,7 +367,7 @@ 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; @@ -382,8 +375,8 @@ void bip_get_my_address(BACNET_ADDRESS * my_address) my_address->mac_len = 6; memcpy(&my_address->mac[0], &BIP_Address, 4); memcpy(&my_address->mac[4], &BIP_Port, 2); - my_address->net = 0; /* local only, no routing */ - my_address->len = 0; /* no SLEN */ + my_address->net = 0; /* local only, no routing */ + my_address->len = 0; /* no SLEN */ for (i = 0; i < MAX_MAC_LEN; i++) { /* no SADR */ my_address->adr[i] = 0; @@ -393,16 +386,16 @@ 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; memcpy(&dest->mac[0], &BIP_Broadcast_Address, 4); memcpy(&dest->mac[4], &BIP_Port, 2); 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/ports/arduino_uno/bv.c b/ports/arduino_uno/bv.c index 3931ff52..3cb0cc65 100644 --- a/ports/arduino_uno/bv.c +++ b/ports/arduino_uno/bv.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2006 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2006 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* Binary Value Objects - customize for your use */ @@ -31,7 +31,7 @@ #include "bacnet/bacdef.h" #include "bacnet/bacdcode.h" #include "bacnet/bacenum.h" -#include "bacnet/config.h" /* the custom stuff */ +#include "bacnet/config.h" /* the custom stuff */ #include "bacnet/wp.h" #include "bacnet/basic/object/bv.h" @@ -87,10 +87,10 @@ static BACNET_BINARY_PV Binary_Value_Present_Value(uint32_t object_instance) /* note: the object name must be unique within this device */ char *Binary_Value_Name(uint32_t object_instance) { - static char text_string[5] = "BV-0"; /* okay for single thread */ + static char text_string[5] = "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; } @@ -98,14 +98,14 @@ 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, uint32_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; @@ -113,15 +113,14 @@ int Binary_Value_Encode_Property_APDU(uint8_t * apdu, switch (property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], OBJECT_BINARY_VALUE, - object_instance); + 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: - characterstring_init_ansi(&char_string, - Binary_Value_Name(object_instance)); + characterstring_init_ansi( + &char_string, Binary_Value_Name(object_instance)); apdu_len = encode_application_character_string(&apdu[0], &char_string); break; @@ -171,11 +170,11 @@ int Binary_Value_Encode_Property_APDU(uint8_t * apdu, } /* 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 */ + bool status = false; /* return value */ unsigned int object_index = 0; int len = 0; BACNET_APPLICATION_DATA_VALUE value; @@ -186,9 +185,8 @@ bool Binary_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, return false; } /* decode the some of the request */ - len = - bacapp_decode_application_data(wp_data->application_data, - wp_data->application_data_len, &value); + len = bacapp_decode_application_data( + wp_data->application_data, wp_data->application_data_len, &value); /* FIXME: len < application_data_len: more data? */ if (len < 0) { /* error while decoding - a value larger than we can handle */ @@ -201,20 +199,19 @@ bool Binary_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, if (value.tag == BACNET_APPLICATION_TAG_ENUMERATED) { if ((value.type.Enumerated == BINARY_ACTIVE) || (value.type.Enumerated == BINARY_INACTIVE)) { - object_index = - Binary_Value_Instance_To_Index(wp_data-> - object_instance); + object_index = Binary_Value_Instance_To_Index( + wp_data->object_instance); /* NOTE: this Binary value has no priority array */ Present_Value[object_index] = - (BACNET_BINARY_PV) value.type.Enumerated; + (BACNET_BINARY_PV)value.type.Enumerated; /* 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. */ if (Present_Value[0] == BINARY_ACTIVE) { -// LED_GREEN_ON(); + // LED_GREEN_ON(); } else { -// LED_GREEN_OFF(); + // LED_GREEN_OFF(); } status = true; } else { @@ -253,7 +250,7 @@ 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; @@ -265,15 +262,12 @@ void testBinary_Value(Test * pTest) BACNET_ERROR_CLASS error_class; BACNET_ERROR_CODE error_code; - - len = - Binary_Value_Encode_Property_APDU(&apdu[0], instance, + len = Binary_Value_Encode_Property_APDU(&apdu[0], instance, 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); - len = - decode_object_id(&apdu[len], (int *) &decoded_type, &decoded_instance); + len = decode_object_id(&apdu[len], (int *)&decoded_type, &decoded_instance); ct_test(pTest, decoded_type == OBJECT_BINARY_VALUE); ct_test(pTest, decoded_instance == instance); @@ -293,7 +287,7 @@ int main(void) ct_setStream(pTest, stdout); ct_run(pTest); - (void) ct_report(pTest); + (void)ct_report(pTest); ct_destroy(pTest); return 0; diff --git a/ports/arduino_uno/bvlc-arduino.c b/ports/arduino_uno/bvlc-arduino.c index 2f8fd8bd..ce582e6e 100644 --- a/ports/arduino_uno/bvlc-arduino.c +++ b/ports/arduino_uno/bvlc-arduino.c @@ -17,7 +17,7 @@ /** result from a client request */ BACNET_BVLC_RESULT BVLC_Result_Code = BVLC_RESULT_SUCCESSFUL_COMPLETION; /** The current BVLC Function Code being handled. */ -BACNET_BVLC_FUNCTION BVLC_Function_Code = BVLC_RESULT; /* A safe default */ +BACNET_BVLC_FUNCTION BVLC_Function_Code = BVLC_RESULT; /* A safe default */ /** Encode the BVLC Result message * @@ -26,8 +26,7 @@ BACNET_BVLC_FUNCTION BVLC_Function_Code = BVLC_RESULT; /* A safe default */ * * @return number of bytes encoded */ -static int bvlc_encode_bvlc_result(uint8_t * pdu, - BACNET_BVLC_RESULT result_code) +static int bvlc_encode_bvlc_result(uint8_t *pdu, BACNET_BVLC_RESULT result_code) { if (pdu) { pdu[0] = BVLL_TYPE_BACNET_IP; @@ -36,7 +35,7 @@ static int bvlc_encode_bvlc_result(uint8_t * pdu, of the entire BVLL message, including the two octets of the length field itself, most significant octet first. */ encode_unsigned16(&pdu[2], 6); - encode_unsigned16(&pdu[4], (uint16_t) result_code); + encode_unsigned16(&pdu[4], (uint16_t)result_code); } return 6; @@ -55,14 +54,14 @@ static int bvlc_encode_bvlc_result(uint8_t * pdu, * @return Upon successful completion, returns the number of bytes sent. * Otherwise, -1 shall be returned and errno set to indicate the error. */ -static int bvlc_send_mpdu(uint8_t * dest_addr, /* the destination address */ +static int bvlc_send_mpdu(uint8_t *dest_addr, /* the destination address */ - uint16_t * dest_port, /* the destination port */ + uint16_t *dest_port, /* the destination port */ - uint8_t * mtu, /* the data */ + uint8_t *mtu, /* the data */ uint16_t mtu_len) -{ /* amount of data to send */ +{ /* amount of data to send */ /* assumes that the driver has already been initialized */ if (bip_valid()) { return 0; @@ -76,15 +75,15 @@ static int bvlc_send_mpdu(uint8_t * dest_addr, /* the destination address */ * @param dest_addr - destination address * @param dest_port - destination port */ -static void bvlc_send_result(uint8_t * dest_addr, - uint16_t * dest_port, /* the destination address */ +static void bvlc_send_result(uint8_t *dest_addr, + uint16_t *dest_port, /* the destination address */ BACNET_BVLC_RESULT result_code) { uint8_t mtu[MAX_MPDU] = { 0 }; uint16_t mtu_len = 0; - mtu_len = (uint16_t) bvlc_encode_bvlc_result(&mtu[0], result_code); + mtu_len = (uint16_t)bvlc_encode_bvlc_result(&mtu[0], result_code); bvlc_send_mpdu(dest_addr, dest_port, mtu, mtu_len); return; @@ -101,30 +100,29 @@ static void bvlc_send_result(uint8_t * dest_addr, * @return Non-zero BVLC_RESULT_ code if we sent a response (NAK) to this * BVLC message. If zero, may need further processing. */ -uint16_t bvlc_for_non_bbmd(uint8_t * addr, - uint16_t * port, - uint8_t * npdu, - uint16_t received_bytes) +uint16_t bvlc_for_non_bbmd( + uint8_t *addr, uint16_t *port, uint8_t *npdu, uint16_t received_bytes) { - uint16_t result_code = 0; /* aka, BVLC_RESULT_SUCCESSFUL_COMPLETION */ + uint16_t result_code = 0; /* aka, BVLC_RESULT_SUCCESSFUL_COMPLETION */ /* To check the BVLC-function code, the buffer of received * bytes has to be at least one byte long. */ if (received_bytes >= 1) { - BVLC_Function_Code = npdu[1]; /* The BVLC function */ + BVLC_Function_Code = npdu[1]; /* The BVLC function */ switch (BVLC_Function_Code) { case BVLC_RESULT: if (received_bytes >= 6) { /* This is the result of our foreign device registration */ - (void) decode_unsigned16(&npdu[4], &result_code); - BVLC_Result_Code = (BACNET_BVLC_RESULT) result_code; + (void)decode_unsigned16(&npdu[4], &result_code); + BVLC_Result_Code = (BACNET_BVLC_RESULT)result_code; fprintf(stderr, "BVLC: Result Code=%d\n", BVLC_Result_Code); /* But don't send any response */ result_code = 0; } break; case BVLC_WRITE_BROADCAST_DISTRIBUTION_TABLE: - result_code = BVLC_RESULT_WRITE_BROADCAST_DISTRIBUTION_TABLE_NAK; + result_code = + BVLC_RESULT_WRITE_BROADCAST_DISTRIBUTION_TABLE_NAK; break; case BVLC_READ_BROADCAST_DIST_TABLE: result_code = BVLC_RESULT_READ_BROADCAST_DISTRIBUTION_TABLE_NAK; diff --git a/ports/arduino_uno/device.c b/ports/arduino_uno/device.c index 9e65bfba..45ee62c1 100644 --- a/ports/arduino_uno/device.c +++ b/ports/arduino_uno/device.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2007 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2007 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include @@ -106,9 +106,8 @@ unsigned Device_Object_List_Count(void) return count; } -bool Device_Object_List_Identifier(uint32_t array_index, - BACNET_OBJECT_TYPE *object_type, - uint32_t * instance) +bool Device_Object_List_Identifier( + uint32_t array_index, BACNET_OBJECT_TYPE *object_type, uint32_t *instance) { bool status = false; uint32_t object_index = 0; @@ -154,15 +153,15 @@ bool Device_Object_List_Identifier(uint32_t 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, uint32_t object_instance, BACNET_PROPERTY_ID property, uint32_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; uint32_t i = 0; @@ -170,13 +169,12 @@ int Device_Encode_Property_APDU(uint8_t * apdu, uint32_t instance = 0; uint32_t count = 0; - //object_instance = object_instance; + // object_instance = object_instance; /* 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); + 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); @@ -195,9 +193,8 @@ int Device_Encode_Property_APDU(uint8_t * apdu, encode_application_character_string(&apdu[0], &char_string); break; case PROP_VENDOR_IDENTIFIER: - apdu_len = - encode_application_unsigned(&apdu[0], - Device_Vendor_Identifier()); + apdu_len = encode_application_unsigned( + &apdu[0], Device_Vendor_Identifier()); break; case PROP_MODEL_NAME: characterstring_init_ansi(&char_string, "GNU Demo"); @@ -220,16 +217,15 @@ int Device_Encode_Property_APDU(uint8_t * apdu, break; case PROP_PROTOCOL_REVISION: apdu_len = - encode_application_unsigned(&apdu[0], - BACNET_PROTOCOL_REVISION); + encode_application_unsigned(&apdu[0], BACNET_PROTOCOL_REVISION); 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)); + 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; @@ -240,7 +236,7 @@ int Device_Encode_Property_APDU(uint8_t * apdu, /* 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); + bitstring_set_bit(&bit_string, (uint8_t)i, false); } /* FIXME: indicate the objects that YOU support */ bitstring_set_bit(&bit_string, OBJECT_DEVICE, true); @@ -260,9 +256,8 @@ int Device_Encode_Property_APDU(uint8_t * apdu, else if (array_index == BACNET_ARRAY_ALL) { for (i = 1; i <= count; i++) { Device_Object_List_Identifier(i, &object_type, &instance); - len = - encode_application_object_id(&apdu[apdu_len], - 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? */ @@ -275,11 +270,10 @@ int Device_Encode_Property_APDU(uint8_t * apdu, } } } else { - if (Device_Object_List_Identifier(array_index, &object_type, - &instance)) - apdu_len = - encode_application_object_id(&apdu[0], object_type, - instance); + 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; @@ -306,25 +300,27 @@ int Device_Encode_Property_APDU(uint8_t * apdu, 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; -// case 512: -// apdu_len = encode_application_unsigned(&apdu[0], stack_size()); -// break; -// case 513: -// apdu_len = encode_application_unsigned(&apdu[0], stack_unused()); -// 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; + // case 512: + // apdu_len = encode_application_unsigned(&apdu[0], + // stack_size()); break; + // case 513: + // apdu_len = encode_application_unsigned(&apdu[0], + // stack_unused()); break; default: *error_class = ERROR_CLASS_PROPERTY; *error_code = ERROR_CODE_UNKNOWN_PROPERTY; @@ -342,11 +338,11 @@ int Device_Encode_Property_APDU(uint8_t * apdu, 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 */ + bool status = false; /* return value */ int len = 0; BACNET_APPLICATION_DATA_VALUE value; @@ -356,9 +352,8 @@ bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, return false; } /* decode the some of the request */ - len = - bacapp_decode_application_data(wp_data->application_data, - wp_data->application_data_len, &value); + len = bacapp_decode_application_data( + wp_data->application_data, wp_data->application_data_len, &value); /* FIXME: len < application_data_len: more data? */ if (len < 0) { /* error while decoding - a value larger than we can handle */ @@ -370,8 +365,8 @@ bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, 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))) { + (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 { @@ -383,35 +378,37 @@ bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, *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_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; @@ -436,20 +433,21 @@ bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data, *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; + // 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_WRITE_ACCESS_DENIED; diff --git a/ports/arduino_uno/h_rp.c b/ports/arduino_uno/h_rp.c index 45d80ae9..ebd2bef5 100644 --- a/ports/arduino_uno/h_rp.c +++ b/ports/arduino_uno/h_rp.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2005 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2005 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include #include @@ -43,10 +43,10 @@ /* Encodes the property APDU and returns the length, or sets the error, and returns -1 */ -int Encode_Property_APDU(uint8_t * apdu, - BACNET_READ_PROPERTY_DATA * rp_data, - BACNET_ERROR_CLASS * error_class, - BACNET_ERROR_CODE * error_code) +int Encode_Property_APDU(uint8_t *apdu, + BACNET_READ_PROPERTY_DATA *rp_data, + BACNET_ERROR_CLASS *error_class, + BACNET_ERROR_CODE *error_code) { int apdu_len = -1; @@ -54,24 +54,21 @@ int Encode_Property_APDU(uint8_t * apdu, switch (rp_data->object_type) { case OBJECT_DEVICE: if (Device_Valid_Object_Instance_Number(rp_data->object_instance)) { - apdu_len = - Device_Encode_Property_APDU(&apdu[0], + apdu_len = Device_Encode_Property_APDU(&apdu[0], rp_data->object_instance, rp_data->object_property, rp_data->array_index, error_class, error_code); } break; case OBJECT_ANALOG_VALUE: if (Analog_Value_Valid_Instance(rp_data->object_instance)) { - apdu_len = - Analog_Value_Encode_Property_APDU(&apdu[0], + apdu_len = Analog_Value_Encode_Property_APDU(&apdu[0], rp_data->object_instance, rp_data->object_property, rp_data->array_index, error_class, error_code); } break; case OBJECT_BINARY_VALUE: if (Binary_Value_Valid_Instance(rp_data->object_instance)) { - apdu_len = - Binary_Value_Encode_Property_APDU(&apdu[0], + apdu_len = Binary_Value_Encode_Property_APDU(&apdu[0], rp_data->object_instance, rp_data->object_property, rp_data->array_index, error_class, error_code); } @@ -85,10 +82,10 @@ int Encode_Property_APDU(uint8_t * 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; @@ -103,13 +100,11 @@ void handler_read_property(uint8_t * service_request, /* encode the NPDU portion of the packet */ datalink_get_my_address(&my_address); npdu_encode_npdu_data(&npdu_data, false, MESSAGE_PRIORITY_NORMAL); - pdu_len = - npdu_encode_pdu(&Handler_Transmit_Buffer[0], src, &my_address, - &npdu_data); + pdu_len = npdu_encode_pdu( + &Handler_Transmit_Buffer[0], src, &my_address, &npdu_data); if (service_data->segmented_message) { /* we don't support segmentation - send an abort */ - len = - abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + len = abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true); goto RP_ABORT; @@ -117,42 +112,38 @@ void handler_read_property(uint8_t * service_request, len = rp_decode_service_request(service_request, service_len, &data); if (len < 0) { /* bad decoding - send an abort */ - len = - abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + len = abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, ABORT_REASON_OTHER, true); goto RP_ABORT; } /* most cases will be error */ - ack_len = - rp_ack_encode_apdu_init(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, &data); + ack_len = rp_ack_encode_apdu_init( + &Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, &data); /* FIXME: add buffer len as passed into function or use smart buffer */ property_len = - Encode_Property_APDU(&Handler_Transmit_Buffer[pdu_len + ack_len], - &data, &error_class, &error_code); + Encode_Property_APDU(&Handler_Transmit_Buffer[pdu_len + ack_len], &data, + &error_class, &error_code); if (property_len >= 0) { - len = - rp_ack_encode_apdu_object_property_end(&Handler_Transmit_Buffer - [pdu_len + property_len + ack_len]); + len = rp_ack_encode_apdu_object_property_end( + &Handler_Transmit_Buffer[pdu_len + property_len + ack_len]); len += ack_len + property_len; } else { switch (property_len) { - /* BACnet APDU too small to fit data, so proper response is Abort */ + /* BACnet APDU too small to fit data, so proper response is + * Abort */ case BACNET_STATUS_ABORT: - len = - abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + len = abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true); break; default: - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + len = bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, SERVICE_CONFIRMED_READ_PROPERTY, error_class, error_code); break; } } - RP_ABORT: +RP_ABORT: pdu_len += len; datalink_send_pdu(src, &npdu_data, &Handler_Transmit_Buffer[0], pdu_len); diff --git a/ports/arduino_uno/h_whois.c b/ports/arduino_uno/h_whois.c index b4f36caf..71c8358c 100644 --- a/ports/arduino_uno/h_whois.c +++ b/ports/arduino_uno/h_whois.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2005 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2005 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include #include @@ -39,8 +39,7 @@ bool Send_I_Am_Flag = true; -void sendIamUnicast(uint8_t * buffer, - BACNET_ADDRESS * src) +void sendIamUnicast(uint8_t *buffer, BACNET_ADDRESS *src) { BACNET_ADDRESS dest; int pdu_len = 0; @@ -61,24 +60,22 @@ void sendIamUnicast(uint8_t * buffer, /* encode the APDU portion of the packet */ apdu_len = iam_encode_apdu(&buffer[npdu_len], Device_Object_Instance_Number(), - MAX_APDU, SEGMENTATION_NONE, Device_Vendor_Identifier()); + MAX_APDU, SEGMENTATION_NONE, Device_Vendor_Identifier()); /* send data */ pdu_len = npdu_len + apdu_len; int bytes = datalink_send_pdu(&dest, &npdu_data, &buffer[0], pdu_len); } -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; int32_t high_limit = 0; int32_t target_device; - len = - whois_decode_service_request(service_request, service_len, &low_limit, - &high_limit); + len = whois_decode_service_request( + service_request, service_len, &low_limit, &high_limit); if (len == 0) { sendIamUnicast(&Handler_Transmit_Buffer[0], src); } else if (len != -1) { diff --git a/ports/arduino_uno/h_wp.c b/ports/arduino_uno/h_wp.c index 5cfc3b56..8f16a22a 100644 --- a/ports/arduino_uno/h_wp.c +++ b/ports/arduino_uno/h_wp.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2005 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2005 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include #include @@ -44,10 +44,10 @@ /* 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,68 +61,62 @@ void handler_write_property(uint8_t * service_request, /* encode the NPDU portion of the packet */ datalink_get_my_address(&my_address); npdu_encode_npdu_data(&npdu_data, false, MESSAGE_PRIORITY_NORMAL); - pdu_len = - npdu_encode_pdu(&Handler_Transmit_Buffer[0], src, &my_address, - &npdu_data); + pdu_len = npdu_encode_pdu( + &Handler_Transmit_Buffer[0], src, &my_address, &npdu_data); /* bad decoding or something we didn't understand - send an abort */ if (len <= 0) { - len = - abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + len = abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, ABORT_REASON_OTHER, true); } else if (service_data->segmented_message) { - len = - abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + len = abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true); } else { 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], + 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); + 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], + 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); + 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], + 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); + service_data->invoke_id, + SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, + error_code); } break; default: - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + len = bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, error_code); break; diff --git a/ports/arduino_uno/main.c b/ports/arduino_uno/main.c index 4d40b3c6..7634e1e1 100644 --- a/ports/arduino_uno/main.c +++ b/ports/arduino_uno/main.c @@ -32,8 +32,8 @@ extern bool Send_I_Am_Flag; /* local version override */ const char *BACnet_Version = "1.0"; -static uint8_t Ethernet_MAC_Address[MAX_MAC_LEN] = - { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; +static uint8_t Ethernet_MAC_Address[MAX_MAC_LEN] = { 0xDE, 0xAD, 0xBE, 0xEF, + 0xFE, 0xED }; uint8_t ipAddress[] = { 192, 168, 0, 185 }; uint8_t gateway[] = { 192, 168, 0, 1 }; uint8_t netmask[] = { 255, 255, 255, 0 }; @@ -53,7 +53,7 @@ bool dcc_communication_enabled(void) void setup() { - //INIT W5100 + // INIT W5100 init_func(CW5100Class_new()); setMACAddress_func(CW5100Class_new(), Ethernet_MAC_Address); setIPAddress_func(CW5100Class_new(), ipAddress); @@ -68,7 +68,6 @@ void setup() #ifdef DEBUG fprintf(stderr, "Starting BACNET application..\n"); #endif - } static uint8_t PDUBuffer[MAX_MPDU]; @@ -83,7 +82,6 @@ int main(void) datalink_init(NULL); for (;;) { - /* other tasks */ /* BACnet handling */ pdu_len = datalink_receive(&src, &PDUBuffer[0], sizeof(PDUBuffer), 0); diff --git a/ports/arduino_uno/uart.c b/ports/arduino_uno/uart.c index c2c6b47c..20db8b8f 100644 --- a/ports/arduino_uno/uart.c +++ b/ports/arduino_uno/uart.c @@ -24,11 +24,10 @@ void uart_init(void) #endif UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */ - UCSR0B = _BV(RXEN0) | _BV(TXEN0); /* Enable RX and TX */ + UCSR0B = _BV(RXEN0) | _BV(TXEN0); /* Enable RX and TX */ } -void uart_putchar(char c, - FILE * stream) +void uart_putchar(char c, FILE *stream) { if (c == '\n') { uart_putchar('\r', stream); @@ -37,8 +36,8 @@ void uart_putchar(char c, UDR0 = c; } -char uart_getchar(FILE * stream) +char uart_getchar(FILE *stream) { - loop_until_bit_is_set(UCSR0A, RXC0); /* Wait until data exists. */ + loop_until_bit_is_set(UCSR0A, RXC0); /* Wait until data exists. */ return UDR0; } diff --git a/ports/at91sam7s/ai.c b/ports/at91sam7s/ai.c index c426a95d..024bbdc8 100644 --- a/ports/at91sam7s/ai.c +++ b/ports/at91sam7s/ai.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2005 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2005 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* Analog Input Objects customize for your use */ @@ -45,31 +45,16 @@ static float Present_Value[MAX_ANALOG_INPUTS]; /* These three arrays are used by the ReadPropertyMultiple handler */ -static const int Analog_Input_Properties_Required[] = { - PROP_OBJECT_IDENTIFIER, - PROP_OBJECT_NAME, - PROP_OBJECT_TYPE, - PROP_PRESENT_VALUE, - PROP_STATUS_FLAGS, - PROP_EVENT_STATE, - PROP_OUT_OF_SERVICE, - PROP_UNITS, - -1 -}; +static const int Analog_Input_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, + PROP_OBJECT_NAME, PROP_OBJECT_TYPE, PROP_PRESENT_VALUE, PROP_STATUS_FLAGS, + PROP_EVENT_STATE, PROP_OUT_OF_SERVICE, PROP_UNITS, -1 }; -static const int Analog_Input_Properties_Optional[] = { - PROP_DESCRIPTION, - -1 -}; +static const int Analog_Input_Properties_Optional[] = { PROP_DESCRIPTION, -1 }; -static const int Analog_Input_Properties_Proprietary[] = { - -1 -}; +static const int Analog_Input_Properties_Proprietary[] = { -1 }; void Analog_Input_Property_Lists( - const int **pRequired, - const int **pOptional, - const int **pProprietary) + const int **pRequired, const int **pOptional, const int **pProprietary) { if (pRequired) *pRequired = Analog_Input_Properties_Required; @@ -84,8 +69,7 @@ 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; @@ -94,36 +78,32 @@ bool Analog_Input_Valid_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; } bool Analog_Input_Object_Name( - uint32_t object_instance, - BACNET_CHARACTER_STRING * object_name) + uint32_t object_instance, BACNET_CHARACTER_STRING *object_name) { - static char text_string[16] = "AI-0"; /* okay for single thread */ + static char text_string[16] = "AI-0"; /* okay for single thread */ bool status = false; if (object_instance < MAX_ANALOG_INPUTS) { - text_string[3] = '0' + (uint8_t) object_instance; + text_string[3] = '0' + (uint8_t)object_instance; status = characterstring_init_ansi(object_name, text_string); } return status; } -float Analog_Input_Present_Value( - uint32_t object_instance) +float Analog_Input_Present_Value(uint32_t object_instance) { float value = 0.0; @@ -133,9 +113,7 @@ float Analog_Input_Present_Value( return value; } -void Analog_Input_Present_Value_Set( - uint32_t object_instance, - float value) +void Analog_Input_Present_Value_Set(uint32_t object_instance, float value) { if (object_instance < MAX_ANALOG_INPUTS) { Present_Value[object_instance] = value; @@ -144,10 +122,9 @@ void Analog_Input_Present_Value_Set( /* return apdu length, or -1 on error */ /* assumption - object has already exists */ -int Analog_Input_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Analog_Input_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ BACNET_BIT_STRING bit_string; BACNET_CHARACTER_STRING char_string; uint8_t *apdu = NULL; @@ -159,9 +136,8 @@ int Analog_Input_Read_Property( apdu = rpdata->application_data; switch (rpdata->object_property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], OBJECT_ANALOG_INPUT, - rpdata->object_instance); + apdu_len = encode_application_object_id( + &apdu[0], OBJECT_ANALOG_INPUT, rpdata->object_instance); break; /* note: Name and Description don't have to be the same. You could make Description writable and different */ @@ -176,9 +152,8 @@ int Analog_Input_Read_Property( encode_application_enumerated(&apdu[0], OBJECT_ANALOG_INPUT); break; case PROP_PRESENT_VALUE: - apdu_len = - encode_application_real(&apdu[0], - Analog_Input_Present_Value(rpdata->object_instance)); + apdu_len = encode_application_real( + &apdu[0], Analog_Input_Present_Value(rpdata->object_instance)); break; case PROP_STATUS_FLAGS: bitstring_init(&bit_string); @@ -214,8 +189,7 @@ int Analog_Input_Read_Property( return apdu_len; } -void Analog_Input_Init( - void) +void Analog_Input_Init(void) { return; } diff --git a/ports/at91sam7s/av.c b/ports/at91sam7s/av.c index e3b99799..31f41ad6 100644 --- a/ports/at91sam7s/av.c +++ b/ports/at91sam7s/av.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2006 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2006 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* Analog Value Objects - customize for your use */ @@ -31,7 +31,7 @@ #include "bacnet/bacdcode.h" #include "bacnet/bacenum.h" #include "bacnet/bacapp.h" -#include "bacnet/config.h" /* the custom stuff */ +#include "bacnet/config.h" /* the custom stuff */ #include "bacnet/basic/object/av.h" #include "bacnet/basic/services.h" @@ -55,35 +55,21 @@ static uint8_t Present_Value[MAX_ANALOG_VALUES]; /* These three arrays are used by the ReadPropertyMultiple handler */ -static const int Analog_Value_Properties_Required[] = { - PROP_OBJECT_IDENTIFIER, - PROP_OBJECT_NAME, - PROP_OBJECT_TYPE, - PROP_PRESENT_VALUE, - PROP_STATUS_FLAGS, - PROP_EVENT_STATE, - PROP_OUT_OF_SERVICE, - PROP_UNITS, - -1 -}; +static const int Analog_Value_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, + PROP_OBJECT_NAME, PROP_OBJECT_TYPE, PROP_PRESENT_VALUE, PROP_STATUS_FLAGS, + PROP_EVENT_STATE, PROP_OUT_OF_SERVICE, PROP_UNITS, -1 }; -static const int Analog_Value_Properties_Optional[] = { - PROP_DESCRIPTION, +static const int Analog_Value_Properties_Optional[] = { PROP_DESCRIPTION, #if 0 PROP_PRIORITY_ARRAY, PROP_RELINQUISH_DEFAULT, #endif - -1 -}; + -1 }; -static const int Analog_Value_Properties_Proprietary[] = { - -1 -}; +static const int Analog_Value_Properties_Proprietary[] = { -1 }; void Analog_Value_Property_Lists( - const int **pRequired, - const int **pOptional, - const int **pProprietary) + const int **pRequired, const int **pOptional, const int **pProprietary) { if (pRequired) *pRequired = Analog_Value_Properties_Required; @@ -95,8 +81,7 @@ void Analog_Value_Property_Lists( return; } -void Analog_Value_Init( - void) +void Analog_Value_Init(void) { unsigned i; @@ -111,8 +96,7 @@ void Analog_Value_Init( /* 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; @@ -122,8 +106,7 @@ bool Analog_Value_Valid_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; } @@ -131,8 +114,7 @@ unsigned Analog_Value_Count( /* 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; } @@ -140,8 +122,7 @@ uint32_t Analog_Value_Index_To_Instance( /* 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; @@ -151,8 +132,7 @@ unsigned Analog_Value_Instance_To_Index( return index; } -float Analog_Value_Present_Value( - uint32_t object_instance) +float Analog_Value_Present_Value(uint32_t object_instance) { float value = ANALOG_RELINQUISH_DEFAULT; unsigned index = 0; @@ -166,14 +146,13 @@ float Analog_Value_Present_Value( } bool Analog_Value_Object_Name( - uint32_t object_instance, - BACNET_CHARACTER_STRING * object_name) + uint32_t object_instance, BACNET_CHARACTER_STRING *object_name) { - static char text_string[16] = "AV-0"; /* okay for single thread */ + static char text_string[16] = "AV-0"; /* okay for single thread */ bool status = false; if (object_instance < MAX_ANALOG_VALUES) { - text_string[3] = '0' + (uint8_t) object_instance; + text_string[3] = '0' + (uint8_t)object_instance; status = characterstring_init_ansi(object_name, text_string); } @@ -181,13 +160,12 @@ bool Analog_Value_Object_Name( } /* return apdu len, or -1 on error */ -int Analog_Value_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Analog_Value_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { - 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; + float real_value = (float)1.414; #if 0 int len = 0; unsigned object_index = 0; @@ -203,9 +181,8 @@ int Analog_Value_Read_Property( apdu = rpdata->application_data; switch (rpdata->object_property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], OBJECT_ANALOG_VALUE, - rpdata->object_instance); + apdu_len = encode_application_object_id( + &apdu[0], OBJECT_ANALOG_VALUE, rpdata->object_instance); break; case PROP_OBJECT_NAME: case PROP_DESCRIPTION: @@ -322,10 +299,9 @@ int Analog_Value_Read_Property( } /* returns true if successful */ -bool Analog_Value_Write_Property( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Analog_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data) { - bool status = false; /* return value */ + bool status = false; /* return value */ unsigned int object_index = 0; unsigned int priority = 0; uint8_t level = ANALOG_LEVEL_NULL; @@ -338,9 +314,8 @@ bool Analog_Value_Write_Property( return false; } /* decode the some of the request */ - len = - bacapp_decode_application_data(wp_data->application_data, - wp_data->application_data_len, &value); + len = bacapp_decode_application_data( + wp_data->application_data, wp_data->application_data_len, &value); /* FIXME: len < application_data_len: more data? */ if (len < 0) { /* error while decoding - a value larger than we can handle */ @@ -363,19 +338,19 @@ bool Analog_Value_Write_Property( algorithm and may not be used for other purposes in any object. */ if (priority && (priority <= BACNET_MAX_PRIORITY) && - (priority != 6 /* reserved */ ) && + (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); + 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) */ + 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 diff --git a/ports/at91sam7s/bi.c b/ports/at91sam7s/bi.c index 9f90eeda..fbd4660f 100644 --- a/ports/at91sam7s/bi.c +++ b/ports/at91sam7s/bi.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2006 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2006 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* Binary Input Objects customize for your use */ @@ -43,31 +43,16 @@ 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[] = { - PROP_OBJECT_IDENTIFIER, - PROP_OBJECT_NAME, - PROP_OBJECT_TYPE, - PROP_PRESENT_VALUE, - PROP_STATUS_FLAGS, - PROP_EVENT_STATE, - PROP_OUT_OF_SERVICE, - PROP_POLARITY, - -1 -}; +static const int Binary_Input_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, + PROP_OBJECT_NAME, PROP_OBJECT_TYPE, PROP_PRESENT_VALUE, PROP_STATUS_FLAGS, + PROP_EVENT_STATE, PROP_OUT_OF_SERVICE, PROP_POLARITY, -1 }; -static const int Binary_Input_Properties_Optional[] = { - PROP_DESCRIPTION, - -1 -}; +static const int Binary_Input_Properties_Optional[] = { PROP_DESCRIPTION, -1 }; -static const int Binary_Input_Properties_Proprietary[] = { - -1 -}; +static const int Binary_Input_Properties_Proprietary[] = { -1 }; void Binary_Input_Property_Lists( - const int **pRequired, - const int **pOptional, - const int **pProprietary) + const int **pRequired, const int **pOptional, const int **pProprietary) { if (pRequired) { *pRequired = Binary_Input_Properties_Required; @@ -82,8 +67,7 @@ void Binary_Input_Property_Lists( return; } -void Binary_Input_Init( - void) +void Binary_Input_Init(void) { unsigned i; @@ -93,8 +77,7 @@ void Binary_Input_Init( } /* 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; @@ -103,15 +86,13 @@ bool Binary_Input_Valid_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; } @@ -119,8 +100,7 @@ uint32_t Binary_Input_Index_To_Instance( /* 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; @@ -130,8 +110,7 @@ unsigned Binary_Input_Instance_To_Index( return index; } -BACNET_BINARY_PV Binary_Input_Present_Value( - uint32_t object_instance) +BACNET_BINARY_PV Binary_Input_Present_Value(uint32_t object_instance) { BACNET_BINARY_PV value = BINARY_INACTIVE; unsigned index = 0; @@ -145,14 +124,13 @@ BACNET_BINARY_PV Binary_Input_Present_Value( } bool Binary_Input_Object_Name( - uint32_t object_instance, - BACNET_CHARACTER_STRING * object_name) + uint32_t object_instance, BACNET_CHARACTER_STRING *object_name) { - static char text_string[16] = "BI-0"; /* okay for single thread */ + static char text_string[16] = "BI-0"; /* okay for single thread */ bool status = false; if (object_instance < MAX_BINARY_INPUTS) { - text_string[3] = '0' + (uint8_t) object_instance; + text_string[3] = '0' + (uint8_t)object_instance; status = characterstring_init_ansi(object_name, text_string); } @@ -161,10 +139,9 @@ bool Binary_Input_Object_Name( /* return apdu length, or -1 on error */ /* assumption - object already exists, and has been bounds checked */ -int Binary_Input_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Binary_Input_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { - 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,9 +155,8 @@ int Binary_Input_Read_Property( apdu = rpdata->application_data; switch (rpdata->object_property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], OBJECT_BINARY_INPUT, - rpdata->object_instance); + apdu_len = encode_application_object_id( + &apdu[0], OBJECT_BINARY_INPUT, rpdata->object_instance); break; case PROP_OBJECT_NAME: case PROP_DESCRIPTION: diff --git a/ports/at91sam7s/blinker.c b/ports/at91sam7s/blinker.c index b29fa0d7..5e676761 100644 --- a/ports/at91sam7s/blinker.c +++ b/ports/at91sam7s/blinker.c @@ -15,11 +15,10 @@ /* global variables */ 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 AT91PS_PIO pPIO = + AT91C_BASE_PIOA; /* pointer to PIO register structure */ volatile unsigned int j, k; /* loop counters */ /* endless loop */ @@ -29,14 +28,17 @@ void blinker( /* 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/ports/at91sam7s/bv.c b/ports/at91sam7s/bv.c index 8478603c..bfdc84fb 100644 --- a/ports/at91sam7s/bv.c +++ b/ports/at91sam7s/bv.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2006 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2006 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* Binary Value Objects - customize for your use */ @@ -31,7 +31,7 @@ #include "bacnet/bacdef.h" #include "bacnet/bacdcode.h" #include "bacnet/bacenum.h" -#include "bacnet/config.h" /* the custom stuff */ +#include "bacnet/config.h" /* the custom stuff */ #include "bacnet/basic/object/bv.h" #include "bacnet/basic/services.h" @@ -45,30 +45,16 @@ static BACNET_BINARY_PV Present_Value[MAX_BINARY_VALUES]; /* These three arrays are used by the ReadPropertyMultiple handler */ -static const int Binary_Value_Properties_Required[] = { - PROP_OBJECT_IDENTIFIER, - PROP_OBJECT_NAME, - PROP_OBJECT_TYPE, - PROP_PRESENT_VALUE, - PROP_STATUS_FLAGS, - PROP_EVENT_STATE, - PROP_OUT_OF_SERVICE, - -1 -}; +static const int Binary_Value_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, + PROP_OBJECT_NAME, PROP_OBJECT_TYPE, PROP_PRESENT_VALUE, PROP_STATUS_FLAGS, + PROP_EVENT_STATE, PROP_OUT_OF_SERVICE, -1 }; -static const int Binary_Value_Properties_Optional[] = { - PROP_DESCRIPTION, - -1 -}; +static const int Binary_Value_Properties_Optional[] = { PROP_DESCRIPTION, -1 }; -static const int Binary_Value_Properties_Proprietary[] = { - -1 -}; +static const int Binary_Value_Properties_Proprietary[] = { -1 }; void Binary_Value_Property_Lists( - const int **pRequired, - const int **pOptional, - const int **pProprietary) + const int **pRequired, const int **pOptional, const int **pProprietary) { if (pRequired) *pRequired = Binary_Value_Properties_Required; @@ -80,8 +66,7 @@ void Binary_Value_Property_Lists( return; } -void Binary_Value_Init( - void) +void Binary_Value_Init(void) { unsigned i; @@ -91,8 +76,7 @@ void Binary_Value_Init( } /* 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; @@ -101,22 +85,19 @@ bool Binary_Value_Valid_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; @@ -126,8 +107,7 @@ unsigned Binary_Value_Instance_To_Index( return index; } -BACNET_BINARY_PV Binary_Value_Present_Value( - uint32_t object_instance) +BACNET_BINARY_PV Binary_Value_Present_Value(uint32_t object_instance) { BACNET_BINARY_PV value = BINARY_INACTIVE; @@ -140,14 +120,13 @@ BACNET_BINARY_PV Binary_Value_Present_Value( /* note: the object name must be unique within this device */ bool Binary_Value_Object_Name( - uint32_t object_instance, - BACNET_CHARACTER_STRING * object_name) + uint32_t object_instance, BACNET_CHARACTER_STRING *object_name) { - static char text_string[16] = "BV-0"; /* okay for single thread */ + static char text_string[16] = "BV-0"; /* okay for single thread */ bool status = false; if (object_instance < MAX_BINARY_VALUES) { - text_string[3] = '0' + (uint8_t) object_instance; + text_string[3] = '0' + (uint8_t)object_instance; status = characterstring_init_ansi(object_name, text_string); } @@ -155,10 +134,9 @@ bool Binary_Value_Object_Name( } /* return apdu len, or -1 on error */ -int Binary_Value_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Binary_Value_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { - 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; @@ -173,9 +151,8 @@ int Binary_Value_Read_Property( apdu = rpdata->application_data; switch (rpdata->object_property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], OBJECT_BINARY_VALUE, - rpdata->object_instance); + apdu_len = encode_application_object_id( + &apdu[0], OBJECT_BINARY_VALUE, rpdata->object_instance); break; /* note: Name and Description don't have to be the same. You could make Description writable and different */ @@ -190,8 +167,7 @@ int Binary_Value_Read_Property( encode_application_enumerated(&apdu[0], OBJECT_BINARY_VALUE); break; case PROP_PRESENT_VALUE: - present_value = - Binary_Value_Present_Value(rpdata->object_instance); + present_value = Binary_Value_Present_Value(rpdata->object_instance); apdu_len = encode_application_enumerated(&apdu[0], present_value); break; case PROP_STATUS_FLAGS: @@ -232,10 +208,9 @@ int Binary_Value_Read_Property( } /* returns true if successful */ -bool Binary_Value_Write_Property( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Binary_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data) { - bool status = false; /* return value */ + bool status = false; /* return value */ unsigned int object_index = 0; unsigned int priority = 0; BACNET_BINARY_PV level = BINARY_NULL; @@ -248,9 +223,8 @@ bool Binary_Value_Write_Property( return false; } /* decode the some of the request */ - len = - bacapp_decode_application_data(wp_data->application_data, - wp_data->application_data_len, &value); + len = bacapp_decode_application_data( + wp_data->application_data, wp_data->application_data_len, &value); /* FIXME: len < application_data_len: more data? */ if (len < 0) { /* error while decoding - a value larger than we can handle */ @@ -273,13 +247,12 @@ bool Binary_Value_Write_Property( algorithm and may not be used for other purposes in any object. */ if (priority && (priority <= BACNET_MAX_PRIORITY) && - (priority != 6 /* reserved */ ) && + (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); + 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; diff --git a/ports/at91sam7s/device.c b/ports/at91sam7s/device.c index 7db7fa08..3ed3b225 100644 --- a/ports/at91sam7s/device.c +++ b/ports/at91sam7s/device.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2007 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2007 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include @@ -50,10 +50,8 @@ static const char *BACnet_Version = BACNET_VERSION_TEXT; /* forward prototype */ -int Device_Read_Property_Local( - BACNET_READ_PROPERTY_DATA * rpdata); -bool Device_Write_Property_Local( - BACNET_WRITE_PROPERTY_DATA * wp_data); +int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA *rpdata); +bool Device_Write_Property_Local(BACNET_WRITE_PROPERTY_DATA *wp_data); static struct my_object_functions { BACNET_OBJECT_TYPE Object_Type; @@ -65,31 +63,28 @@ static struct my_object_functions { read_property_function Object_Read_Property; write_property_function Object_Write_Property; rpm_property_lists_function Object_RPM_List; -} Object_Table[] = { - { - OBJECT_DEVICE, NULL, /* don't init - recursive! */ - Device_Count, Device_Index_To_Instance, - Device_Valid_Object_Instance_Number, Device_Object_Name, - Device_Read_Property_Local, Device_Write_Property_Local, - Device_Property_Lists}, { - OBJECT_ANALOG_INPUT, Analog_Input_Init, Analog_Input_Count, - Analog_Input_Index_To_Instance, Analog_Input_Valid_Instance, - Analog_Input_Object_Name, Analog_Input_Read_Property, NULL, - Analog_Input_Property_Lists}, { - OBJECT_ANALOG_VALUE, Analog_Value_Init, Analog_Value_Count, - Analog_Value_Index_To_Instance, Analog_Value_Valid_Instance, - Analog_Value_Object_Name, Analog_Value_Read_Property, - Analog_Value_Write_Property, Analog_Value_Property_Lists}, { - OBJECT_BINARY_INPUT, Binary_Input_Init, Binary_Input_Count, - Binary_Input_Index_To_Instance, Binary_Input_Valid_Instance, - Binary_Input_Object_Name, Binary_Input_Read_Property, NULL, - Binary_Input_Property_Lists}, { - OBJECT_BINARY_VALUE, Binary_Value_Init, Binary_Value_Count, - Binary_Value_Index_To_Instance, Binary_Value_Valid_Instance, - Binary_Value_Object_Name, Binary_Value_Read_Property, - Binary_Value_Write_Property, Binary_Value_Property_Lists}, { - MAX_BACNET_OBJECT_TYPE, NULL, NULL, NULL, NULL, NULL, NULL, NULL} -}; +} Object_Table[] = { { OBJECT_DEVICE, NULL, /* don't init - recursive! */ + Device_Count, Device_Index_To_Instance, + Device_Valid_Object_Instance_Number, + Device_Object_Name, Device_Read_Property_Local, + Device_Write_Property_Local, Device_Property_Lists }, + { OBJECT_ANALOG_INPUT, Analog_Input_Init, Analog_Input_Count, + Analog_Input_Index_To_Instance, Analog_Input_Valid_Instance, + Analog_Input_Object_Name, Analog_Input_Read_Property, NULL, + Analog_Input_Property_Lists }, + { OBJECT_ANALOG_VALUE, Analog_Value_Init, Analog_Value_Count, + Analog_Value_Index_To_Instance, Analog_Value_Valid_Instance, + Analog_Value_Object_Name, Analog_Value_Read_Property, + Analog_Value_Write_Property, Analog_Value_Property_Lists }, + { OBJECT_BINARY_INPUT, Binary_Input_Init, Binary_Input_Count, + Binary_Input_Index_To_Instance, Binary_Input_Valid_Instance, + Binary_Input_Object_Name, Binary_Input_Read_Property, NULL, + Binary_Input_Property_Lists }, + { OBJECT_BINARY_VALUE, Binary_Value_Init, Binary_Value_Count, + Binary_Value_Index_To_Instance, Binary_Value_Valid_Instance, + Binary_Value_Object_Name, Binary_Value_Read_Property, + Binary_Value_Write_Property, Binary_Value_Property_Lists }, + { MAX_BACNET_OBJECT_TYPE, NULL, NULL, NULL, NULL, NULL, NULL, NULL } }; /* note: you really only need to define variables for properties that are writable or that may change. @@ -103,42 +98,21 @@ static BACNET_REINITIALIZED_STATE Reinitialize_State = BACNET_REINIT_IDLE; static const char *Reinit_Password = "rehmite"; /* These three arrays are used by the ReadPropertyMultiple handler */ -static const int Device_Properties_Required[] = { - PROP_OBJECT_IDENTIFIER, - PROP_OBJECT_NAME, - PROP_OBJECT_TYPE, - PROP_SYSTEM_STATUS, - PROP_VENDOR_NAME, - PROP_VENDOR_IDENTIFIER, - PROP_MODEL_NAME, - PROP_FIRMWARE_REVISION, - PROP_APPLICATION_SOFTWARE_VERSION, - PROP_PROTOCOL_VERSION, - PROP_PROTOCOL_REVISION, - PROP_PROTOCOL_SERVICES_SUPPORTED, - PROP_PROTOCOL_OBJECT_TYPES_SUPPORTED, - PROP_OBJECT_LIST, - PROP_MAX_APDU_LENGTH_ACCEPTED, - PROP_SEGMENTATION_SUPPORTED, - PROP_APDU_TIMEOUT, - PROP_NUMBER_OF_APDU_RETRIES, - PROP_MAX_MASTER, - PROP_MAX_INFO_FRAMES, - PROP_DEVICE_ADDRESS_BINDING, - PROP_DATABASE_REVISION, - -1 -}; +static const int Device_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, + PROP_OBJECT_NAME, PROP_OBJECT_TYPE, PROP_SYSTEM_STATUS, PROP_VENDOR_NAME, + PROP_VENDOR_IDENTIFIER, PROP_MODEL_NAME, PROP_FIRMWARE_REVISION, + PROP_APPLICATION_SOFTWARE_VERSION, PROP_PROTOCOL_VERSION, + PROP_PROTOCOL_REVISION, PROP_PROTOCOL_SERVICES_SUPPORTED, + PROP_PROTOCOL_OBJECT_TYPES_SUPPORTED, PROP_OBJECT_LIST, + PROP_MAX_APDU_LENGTH_ACCEPTED, PROP_SEGMENTATION_SUPPORTED, + PROP_APDU_TIMEOUT, PROP_NUMBER_OF_APDU_RETRIES, PROP_MAX_MASTER, + PROP_MAX_INFO_FRAMES, PROP_DEVICE_ADDRESS_BINDING, PROP_DATABASE_REVISION, + -1 }; -static const int Device_Properties_Optional[] = { - PROP_DESCRIPTION, - PROP_LOCATION, - -1 -}; +static const int Device_Properties_Optional[] = { PROP_DESCRIPTION, + PROP_LOCATION, -1 }; -static const int Device_Properties_Proprietary[] = { - 9600, - -1 -}; +static const int Device_Properties_Proprietary[] = { 9600, -1 }; static struct my_object_functions *Device_Objects_Find_Functions( BACNET_OBJECT_TYPE Object_Type) @@ -159,8 +133,7 @@ static struct my_object_functions *Device_Objects_Find_Functions( } static int Read_Property_Common( - struct my_object_functions *pObject, - BACNET_READ_PROPERTY_DATA * rpdata) + struct my_object_functions *pObject, BACNET_READ_PROPERTY_DATA *rpdata) { int apdu_len = BACNET_STATUS_ERROR; BACNET_CHARACTER_STRING char_string; @@ -184,9 +157,8 @@ static int Read_Property_Common( if (rpdata->object_type == OBJECT_DEVICE) { rpdata->object_instance = Object_Instance_Number; } - apdu_len = - encode_application_object_id(&apdu[0], rpdata->object_type, - rpdata->object_instance); + apdu_len = encode_application_object_id( + &apdu[0], rpdata->object_type, rpdata->object_instance); } break; case PROP_OBJECT_NAME: @@ -198,12 +170,11 @@ static int Read_Property_Common( } else { characterstring_init_ansi(&char_string, ""); if (pObject->Object_Name) { - (void) pObject->Object_Name(rpdata->object_instance, - &char_string); + (void)pObject->Object_Name( + rpdata->object_instance, &char_string); } apdu_len = - encode_application_character_string(&apdu[0], - &char_string); + encode_application_character_string(&apdu[0], &char_string); } break; case PROP_OBJECT_TYPE: @@ -213,9 +184,8 @@ static int Read_Property_Common( rpdata->error_code = ERROR_CODE_PROPERTY_IS_NOT_AN_ARRAY; apdu_len = BACNET_STATUS_ERROR; } else { - apdu_len = - encode_application_enumerated(&apdu[0], - rpdata->object_type); + apdu_len = encode_application_enumerated( + &apdu[0], rpdata->object_type); } break; default: @@ -230,8 +200,7 @@ static int Read_Property_Common( /* Encodes the property APDU and returns the length, or sets the error, and returns BACNET_STATUS_ERROR */ -int Device_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Device_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { int apdu_len = BACNET_STATUS_ERROR; struct my_object_functions *pObject = NULL; @@ -254,8 +223,7 @@ int Device_Read_Property( return apdu_len; } -bool Device_Write_Property( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data) { bool status = false; struct my_object_functions *pObject = NULL; @@ -284,8 +252,7 @@ bool Device_Write_Property( } /* for a given object type, returns the special property list */ -void Device_Objects_Property_List( - BACNET_OBJECT_TYPE object_type, +void Device_Objects_Property_List(BACNET_OBJECT_TYPE object_type, uint32_t object_instance, struct special_property_list_t *pPropertyList) { @@ -308,25 +275,23 @@ void Device_Objects_Property_List( } /* Fetch the counts if available otherwise zero them */ - pPropertyList->Required.count = - pPropertyList->Required.pList == - NULL ? 0 : property_list_count(pPropertyList->Required.pList); + pPropertyList->Required.count = pPropertyList->Required.pList == NULL + ? 0 + : property_list_count(pPropertyList->Required.pList); - pPropertyList->Optional.count = - pPropertyList->Optional.pList == - NULL ? 0 : property_list_count(pPropertyList->Optional.pList); + pPropertyList->Optional.count = pPropertyList->Optional.pList == NULL + ? 0 + : property_list_count(pPropertyList->Optional.pList); - pPropertyList->Proprietary.count = - pPropertyList->Proprietary.pList == - NULL ? 0 : property_list_count(pPropertyList->Proprietary.pList); + pPropertyList->Proprietary.count = pPropertyList->Proprietary.pList == NULL + ? 0 + : property_list_count(pPropertyList->Proprietary.pList); return; } void Device_Property_Lists( - const int **pRequired, - const int **pOptional, - const int **pProprietary) + const int **pRequired, const int **pOptional, const int **pProprietary) { if (pRequired) *pRequired = Device_Properties_Required; @@ -338,22 +303,19 @@ void Device_Property_Lists( return; } -unsigned Device_Count( - void) +unsigned Device_Count(void) { return 1; } -uint32_t Device_Index_To_Instance( - unsigned index) +uint32_t Device_Index_To_Instance(unsigned index) { index = index; return Object_Instance_Number; } bool Device_Object_Name( - uint32_t object_instance, - BACNET_CHARACTER_STRING * object_name) + uint32_t object_instance, BACNET_CHARACTER_STRING *object_name) { bool status = false; @@ -364,10 +326,9 @@ bool Device_Object_Name( return status; } -bool Device_Set_Object_Name( - BACNET_CHARACTER_STRING * object_name) +bool Device_Set_Object_Name(BACNET_CHARACTER_STRING *object_name) { - bool status = false; /*return value */ + bool status = false; /*return value */ if (!characterstring_same(&My_Object_Name, object_name)) { /* Make the change and update the database revision */ @@ -378,8 +339,7 @@ bool Device_Set_Object_Name( return status; } -bool Device_Reinitialize( - BACNET_REINITIALIZE_DEVICE_DATA * rd_data) +bool Device_Reinitialize(BACNET_REINITIALIZE_DEVICE_DATA *rd_data) { bool status = false; @@ -422,20 +382,18 @@ bool Device_Reinitialize( return status; } -BACNET_REINITIALIZED_STATE Device_Reinitialized_State( - void) +BACNET_REINITIALIZED_STATE Device_Reinitialized_State(void) { return Reinitialize_State; } -void Device_Init( - object_functions_t * object_table) +void Device_Init(object_functions_t *object_table) { struct my_object_functions *pObject = NULL; /* we don't use the object table passed in since there is extra stuff we don't need in there. */ - (void) object_table; + (void)object_table; /* our local object table */ pObject = &Object_Table[0]; while (pObject->Object_Type < MAX_BACNET_OBJECT_TYPE) { @@ -450,14 +408,12 @@ void Device_Init( } /* 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 */ @@ -469,21 +425,17 @@ bool Device_Set_Object_Instance_Number( return status; } -bool Device_Valid_Object_Instance_Number( - uint32_t object_id) +bool Device_Valid_Object_Instance_Number(uint32_t object_id) { return (Object_Instance_Number == object_id); } -BACNET_DEVICE_STATUS Device_System_Status( - void) +BACNET_DEVICE_STATUS Device_System_Status(void) { return System_Status; } -int Device_Set_System_Status( - BACNET_DEVICE_STATUS status, - bool local) +int Device_Set_System_Status(BACNET_DEVICE_STATUS status, bool local) { /*return value - 0 = ok, -1 = bad value, -2 = not allowed */ int result = -1; @@ -496,34 +448,29 @@ int Device_Set_System_Status( return result; } -uint16_t Device_Vendor_Identifier( - void) +uint16_t Device_Vendor_Identifier(void) { return BACNET_VENDOR_ID; } -BACNET_SEGMENTATION Device_Segmentation_Supported( - void) +BACNET_SEGMENTATION Device_Segmentation_Supported(void) { return SEGMENTATION_NONE; } -uint32_t Device_Database_Revision( - void) +uint32_t Device_Database_Revision(void) { return Database_Revision; } -void Device_Inc_Database_Revision( - void) +void Device_Inc_Database_Revision(void) { Database_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 = 0; /* number of objects */ struct my_object_functions *pObject = NULL; @@ -541,9 +488,7 @@ unsigned Device_Object_List_Count( } bool Device_Object_List_Identifier( - uint32_t array_index, - BACNET_OBJECT_TYPE *object_type, - uint32_t * instance) + uint32_t array_index, BACNET_OBJECT_TYPE *object_type, uint32_t *instance) { bool status = false; uint32_t count = 0; @@ -574,10 +519,9 @@ bool Device_Object_List_Identifier( return status; } -bool Device_Valid_Object_Name( - BACNET_CHARACTER_STRING * object_name1, +bool Device_Valid_Object_Name(BACNET_CHARACTER_STRING *object_name1, BACNET_OBJECT_TYPE *object_type, - uint32_t * object_instance) + uint32_t *object_instance) { bool found = false; BACNET_OBJECT_TYPE type = OBJECT_NONE; @@ -591,7 +535,7 @@ bool Device_Valid_Object_Name( for (i = 1; i <= max_objects; i++) { check_id = Device_Object_List_Identifier(i, &type, &instance); if (check_id) { - pObject = Device_Objects_Find_Functions((BACNET_OBJECT_TYPE) type); + pObject = Device_Objects_Find_Functions((BACNET_OBJECT_TYPE)type); if ((pObject != NULL) && (pObject->Object_Name != NULL) && (pObject->Object_Name(instance, &object_name2) && characterstring_same(object_name1, &object_name2))) { @@ -611,13 +555,12 @@ bool Device_Valid_Object_Name( } bool Device_Valid_Object_Id( - BACNET_OBJECT_TYPE object_type, - uint32_t object_instance) + BACNET_OBJECT_TYPE object_type, uint32_t object_instance) { - bool status = false; /* return value */ + bool status = false; /* return value */ struct my_object_functions *pObject = NULL; - pObject = Device_Objects_Find_Functions((BACNET_OBJECT_TYPE) object_type); + pObject = Device_Objects_Find_Functions((BACNET_OBJECT_TYPE)object_type); if ((pObject != NULL) && (pObject->Object_Valid_Instance != NULL)) { status = pObject->Object_Valid_Instance(object_instance); } @@ -625,10 +568,9 @@ bool Device_Valid_Object_Id( return status; } -bool Device_Object_Name_Copy( - BACNET_OBJECT_TYPE object_type, +bool Device_Object_Name_Copy(BACNET_OBJECT_TYPE object_type, uint32_t object_instance, - BACNET_CHARACTER_STRING * object_name) + BACNET_CHARACTER_STRING *object_name) { struct my_object_functions *pObject = NULL; bool found = false; @@ -642,11 +584,10 @@ bool Device_Object_Name_Copy( } /* return the length of the apdu encoded or BACNET_STATUS_ERROR for error */ -int Device_Read_Property_Local( - BACNET_READ_PROPERTY_DATA * rpdata) +int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA *rpdata) { - 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; uint32_t i = 0; @@ -661,7 +602,7 @@ int Device_Read_Property_Local( return 0; } apdu = rpdata->application_data; - switch ((int) rpdata->object_property) { + switch ((int)rpdata->object_property) { case PROP_DESCRIPTION: characterstring_init_ansi(&char_string, "BACnet Demo"); apdu_len = @@ -674,8 +615,7 @@ int Device_Read_Property_Local( break; case PROP_SYSTEM_STATUS: apdu_len = - encode_application_enumerated(&apdu[0], - Device_System_Status()); + encode_application_enumerated(&apdu[0], Device_System_Status()); break; case PROP_VENDOR_NAME: characterstring_init_ansi(&char_string, BACNET_VENDOR_NAME); @@ -706,16 +646,15 @@ int Device_Read_Property_Local( break; case PROP_PROTOCOL_REVISION: apdu_len = - encode_application_unsigned(&apdu[0], - BACNET_PROTOCOL_REVISION); + encode_application_unsigned(&apdu[0], BACNET_PROTOCOL_REVISION); 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)); + 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; @@ -725,7 +664,7 @@ int Device_Read_Property_Local( 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); + bitstring_set_bit(&bit_string, (uint8_t)i, false); } /* set the object types with objects to supported */ i = 0; @@ -749,11 +688,10 @@ int Device_Read_Property_Local( /* your maximum APDU size. */ else if (rpdata->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); + 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? */ @@ -773,11 +711,10 @@ int Device_Read_Property_Local( } } } else { - if (Device_Object_List_Identifier(rpdata->array_index, - &object_type, &instance)) - apdu_len = - encode_application_object_id(&apdu[0], object_type, - instance); + if (Device_Object_List_Identifier( + rpdata->array_index, &object_type, &instance)) + apdu_len = encode_application_object_id( + &apdu[0], object_type, instance); else { rpdata->error_class = ERROR_CLASS_PROPERTY; rpdata->error_code = ERROR_CODE_INVALID_ARRAY_INDEX; @@ -789,9 +726,8 @@ int Device_Read_Property_Local( apdu_len = encode_application_unsigned(&apdu[0], MAX_APDU); break; case PROP_SEGMENTATION_SUPPORTED: - apdu_len = - encode_application_enumerated(&apdu[0], - Device_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()); @@ -803,14 +739,12 @@ int Device_Read_Property_Local( /* FIXME: encode the list here, if it exists */ break; case PROP_DATABASE_REVISION: - apdu_len = - encode_application_unsigned(&apdu[0], - Device_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()); + encode_application_unsigned(&apdu[0], dlmstp_max_info_frames()); break; case PROP_MAX_MASTER: apdu_len = @@ -837,19 +771,17 @@ int Device_Read_Property_Local( return apdu_len; } -bool Device_Write_Property_Local( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Device_Write_Property_Local(BACNET_WRITE_PROPERTY_DATA *wp_data) { - bool status = false; /* return value - false=error */ + bool status = false; /* return value - false=error */ int len = 0; uint8_t encoding = 0; size_t length = 0; BACNET_APPLICATION_DATA_VALUE value; /* decode the some of the request */ - len = - bacapp_decode_application_data(wp_data->application_data, - wp_data->application_data_len, &value); + len = bacapp_decode_application_data( + wp_data->application_data, wp_data->application_data_len, &value); /* FIXME: len < application_data_len: more data? */ if (len < 0) { /* error while decoding - a value larger than we can handle */ @@ -864,12 +796,12 @@ bool Device_Write_Property_Local( wp_data->error_code = ERROR_CODE_PROPERTY_IS_NOT_AN_ARRAY; return false; } - switch ((int) wp_data->object_property) { + switch ((int)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))) { + (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 { @@ -921,13 +853,13 @@ bool Device_Write_Property_Local( characterstring_encoding(&value.type.Character_String); if (encoding < MAX_CHARACTER_STRING_ENCODING) { /* All the object names in a device must be unique. */ - if (Device_Valid_Object_Name(&value.type. - Character_String, NULL, NULL)) { + if (Device_Valid_Object_Name( + &value.type.Character_String, NULL, NULL)) { wp_data->error_class = ERROR_CLASS_PROPERTY; wp_data->error_code = ERROR_CODE_DUPLICATE_NAME; } else { - Device_Set_Object_Name(&value.type. - Character_String); + Device_Set_Object_Name( + &value.type.Character_String); status = true; } } else { @@ -937,8 +869,7 @@ bool Device_Write_Property_Local( } } else { wp_data->error_class = ERROR_CLASS_PROPERTY; - wp_data->error_code = - ERROR_CODE_NO_SPACE_TO_WRITE_PROPERTY; + wp_data->error_code = ERROR_CODE_NO_SPACE_TO_WRITE_PROPERTY; } } else { wp_data->error_class = ERROR_CLASS_PROPERTY; diff --git a/ports/at91sam7s/dlmstp.c b/ports/at91sam7s/dlmstp.c index 9f08785c..8af419b6 100644 --- a/ports/at91sam7s/dlmstp.c +++ b/ports/at91sam7s/dlmstp.c @@ -64,19 +64,19 @@ static struct mstp_flag_t { /* A Boolean flag set to TRUE by the Receive State Machine */ /* if an invalid frame is received. */ /* Set to FALSE by the main state machine. */ - unsigned ReceivedInvalidFrame:1; + unsigned ReceivedInvalidFrame : 1; /* A Boolean flag set to TRUE by the Receive State Machine */ /* if a valid frame is received. */ /* Set to FALSE by the main state machine. */ - unsigned ReceivedValidFrame:1; + unsigned ReceivedValidFrame : 1; /* set to TRUE when we get a frame not for us */ - unsigned ReceivedValidFrameNotForUs:1; + unsigned ReceivedValidFrameNotForUs : 1; /* A Boolean flag set to TRUE by the master machine if this node is the */ /* only known master node. */ - unsigned SoleMaster:1; + unsigned SoleMaster : 1; /* A Boolean flag set TRUE by the datalink if a packet has been received, but not processed. */ - unsigned ReceivePacketPending:1; + unsigned ReceivePacketPending : 1; } MSTP_Flag; /* Used to store the data length of a received frame. */ @@ -178,7 +178,11 @@ static uint8_t Nmax_master = 127; #define Tusage_delay 15 /* we need to be able to increment without rolling over */ -#define INCREMENT_AND_LIMIT_UINT8(x) {if (x < 0xFF) x++;} +#define INCREMENT_AND_LIMIT_UINT8(x) \ + { \ + if (x < 0xFF) \ + x++; \ + } /* data structure for MS/TP PDU Queue */ struct mstp_pdu_packet { @@ -194,27 +198,23 @@ struct mstp_pdu_packet { static struct mstp_pdu_packet PDU_Buffer[MSTP_PDU_PACKET_COUNT]; static RING_BUFFER PDU_Queue; -bool dlmstp_init( - char *ifname) +bool dlmstp_init(char *ifname) { - (void) ifname; + (void)ifname; /* initialize hardware */ RS485_Initialize(); - Ringbuf_Init(&PDU_Queue, (uint8_t *) & PDU_Buffer, + Ringbuf_Init(&PDU_Queue, (uint8_t *)&PDU_Buffer, sizeof(struct mstp_pdu_packet), MSTP_PDU_PACKET_COUNT); 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; @@ -237,11 +237,10 @@ void dlmstp_fill_bacnet_address( } } -static bool dlmstp_compare_data_expecting_reply( - uint8_t * request_pdu, +static bool dlmstp_compare_data_expecting_reply(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) { @@ -262,9 +261,8 @@ 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], NULL, &request.address, - &request.npdu_data); + offset = npdu_decode( + &request_pdu[0], NULL, &request.address, &request.npdu_data); if (request.npdu_data.network_layer_message) { return false; } @@ -281,8 +279,7 @@ static bool dlmstp_compare_data_expecting_reply( /* decode the reply data */ reply.address.mac[0] = dest_address; reply.address.mac_len = 1; - offset = - npdu_decode(&reply_pdu[0], &reply.address, NULL, &reply.npdu_data); + offset = npdu_decode(&reply_pdu[0], &reply.address, NULL, &reply.npdu_data); if (reply.npdu_data.network_layer_message) { return false; } @@ -323,7 +320,8 @@ static bool dlmstp_compare_data_expecting_reply( return false; } } - if (request.npdu_data.protocol_version != reply.npdu_data.protocol_version) { + if (request.npdu_data.protocol_version != + reply.npdu_data.protocol_version) { return false; } #if 0 @@ -355,15 +353,15 @@ static bool dlmstp_compare_data_expecting_reply( /* (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 destination, /* destination address */ - uint8_t source, /* source address */ - uint8_t * data, /* any data to be sent - may be null */ + 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) -{ /* 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 */ +{ /* 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 */ /* create the MS/TP header */ buffer[0] = 0x55; @@ -397,8 +395,7 @@ 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; @@ -633,8 +630,7 @@ static void MSTP_Receive_Frame_FSM( } /* 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 */ @@ -719,8 +715,7 @@ static bool MSTP_Master_Node_FSM( /* ReceivedPFM */ /* DestinationAddress is equal to TS */ if (DestinationAddress == This_Station) { - MSTP_Send_Frame( - FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER, + MSTP_Send_Frame(FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER, SourceAddress, This_Station, NULL, 0); } break; @@ -738,9 +733,8 @@ static bool MSTP_Master_Node_FSM( } break; case FRAME_TYPE_TEST_REQUEST: - MSTP_Send_Frame(FRAME_TYPE_TEST_RESPONSE, - SourceAddress, This_Station, &InputBuffer[0], - DataLength); + MSTP_Send_Frame(FRAME_TYPE_TEST_RESPONSE, SourceAddress, + This_Station, &InputBuffer[0], DataLength); break; case FRAME_TYPE_TEST_RESPONSE: default: @@ -765,14 +759,14 @@ static bool MSTP_Master_Node_FSM( transition_now = true; } else { uint8_t frame_type; - pkt = (struct mstp_pdu_packet *) Ringbuf_Peek(&PDU_Queue); + pkt = (struct mstp_pdu_packet *)Ringbuf_Peek(&PDU_Queue); if (pkt->data_expecting_reply) { frame_type = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY; } else { frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; } MSTP_Send_Frame(frame_type, pkt->destination_mac, This_Station, - (uint8_t *) & pkt->buffer[0], pkt->length); + (uint8_t *)&pkt->buffer[0], pkt->length); FrameCount++; switch (frame_type) { case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: @@ -792,7 +786,7 @@ static bool MSTP_Master_Node_FSM( Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; break; } - (void) Ringbuf_Pop(&PDU_Queue, NULL); + (void)Ringbuf_Pop(&PDU_Queue, NULL); } break; case MSTP_MASTER_STATE_WAIT_FOR_REPLY: @@ -903,8 +897,8 @@ static bool MSTP_Master_Node_FSM( 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; @@ -918,7 +912,7 @@ static bool MSTP_Master_Node_FSM( /* no known successor node */ Next_Station = This_Station; RetryCount = 0; - TokenCount = 1; /* changed in Errata SSPC-135-2004 */ + 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; @@ -926,10 +920,10 @@ static bool MSTP_Master_Node_FSM( /* ResetMaintenancePFM */ Poll_Station = This_Station; /* 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; - TokenCount = 1; /* changed in Errata SSPC-135-2004 */ + TokenCount = 1; /* changed in Errata SSPC-135-2004 */ EventCount = 0; Master_State = MSTP_MASTER_STATE_PASS_TOKEN; } @@ -959,8 +953,8 @@ static bool MSTP_Master_Node_FSM( /* RetrySendToken */ RetryCount++; /* 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); EventCount = 0; /* re-enter the current state to listen for NS */ /* to begin using the token. */ @@ -1024,15 +1018,15 @@ static bool MSTP_Master_Node_FSM( /* 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)) { + 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); + MSTP_Send_Frame( + FRAME_TYPE_TOKEN, Next_Station, This_Station, NULL, 0); Poll_Station = This_Station; TokenCount = 0; RetryCount = 0; @@ -1095,10 +1089,9 @@ static bool MSTP_Master_Node_FSM( /* BACnet Data Expecting Reply, a Test_Request, or */ /* a proprietary frame that expects a reply is received. */ case MSTP_MASTER_STATE_ANSWER_DATA_REQUEST: - pkt = (struct mstp_pdu_packet *) Ringbuf_Peek(&PDU_Queue); + pkt = (struct mstp_pdu_packet *)Ringbuf_Peek(&PDU_Queue); if (pkt != NULL) { - matched = - dlmstp_compare_data_expecting_reply(&InputBuffer[0], + matched = dlmstp_compare_data_expecting_reply(&InputBuffer[0], DataLength, SourceAddress, &pkt->buffer[0], pkt->length, pkt->destination_mac); } else { @@ -1119,12 +1112,12 @@ static bool MSTP_Master_Node_FSM( frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; } MSTP_Send_Frame(frame_type, pkt->destination_mac, This_Station, - (uint8_t *) & pkt->buffer[0], pkt->length); + (uint8_t *)&pkt->buffer[0], pkt->length); Master_State = MSTP_MASTER_STATE_IDLE; /* clear our flag we were holding for comparison */ MSTP_Flag.ReceivedValidFrame = false; /* clear the queue */ - (void) Ringbuf_Pop(&PDU_Queue, NULL); + (void)Ringbuf_Pop(&PDU_Queue, NULL); } else if ((Timer_Silence() > Treply_delay) || (pkt != NULL)) { /* DeferredReply */ /* If no reply will be available from the higher layers */ @@ -1150,8 +1143,7 @@ static bool MSTP_Master_Node_FSM( return transition_now; } -static void MSTP_Slave_Node_FSM( - void) +static void MSTP_Slave_Node_FSM(void) { /* packet from the PDU Queue */ struct mstp_pdu_packet *pkt; @@ -1185,9 +1177,8 @@ static void MSTP_Slave_Node_FSM( } } else if (MSTP_Flag.ReceivePacketPending) { if (!Ringbuf_Empty(&PDU_Queue)) { - pkt = (struct mstp_pdu_packet *) Ringbuf_Peek(&PDU_Queue); - matched = - dlmstp_compare_data_expecting_reply(&InputBuffer[0], + pkt = (struct mstp_pdu_packet *)Ringbuf_Peek(&PDU_Queue); + matched = dlmstp_compare_data_expecting_reply(&InputBuffer[0], DataLength, SourceAddress, &pkt->buffer[0], pkt->length, pkt->destination_mac); if (matched) { @@ -1205,8 +1196,8 @@ static void MSTP_Slave_Node_FSM( frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; } MSTP_Send_Frame(frame_type, pkt->destination_mac, This_Station, - (uint8_t *) & pkt->buffer[0], pkt->length); - (void) Ringbuf_Pop(&PDU_Queue, NULL); + (uint8_t *)&pkt->buffer[0], pkt->length); + (void)Ringbuf_Pop(&PDU_Queue, NULL); } /* clear our flag we were holding for comparison */ MSTP_Flag.ReceivePacketPending = false; @@ -1222,17 +1213,16 @@ static void MSTP_Slave_Node_FSM( } /* 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 */ +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 */ +{ /* number of bytes of data */ int bytes_sent = 0; struct mstp_pdu_packet *pkt; uint16_t i = 0; - pkt = (struct mstp_pdu_packet *) Ringbuf_Data_Peek(&PDU_Queue); + pkt = (struct mstp_pdu_packet *)Ringbuf_Data_Peek(&PDU_Queue); if (pkt) { pkt->data_expecting_reply = npdu_data->data_expecting_reply; for (i = 0; i < pdu_len; i++) { @@ -1254,13 +1244,12 @@ int dlmstp_send_pdu( } /* 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 */ +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 */ +{ /* 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) { @@ -1302,8 +1291,7 @@ 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) { @@ -1320,8 +1308,7 @@ void dlmstp_set_mac_address( return; } -uint8_t dlmstp_mac_address( - void) +uint8_t dlmstp_mac_address(void) { return This_Station; } @@ -1333,8 +1320,7 @@ uint8_t dlmstp_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( - 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; @@ -1348,8 +1334,7 @@ void dlmstp_set_max_info_frames( return; } -uint8_t dlmstp_max_info_frames( - void) +uint8_t dlmstp_max_info_frames(void) { return Nmax_info_frames; } @@ -1359,8 +1344,7 @@ uint8_t dlmstp_max_info_frames( /* 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) { @@ -1376,20 +1360,18 @@ void dlmstp_set_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; - my_address->net = 0; /* local only, no routing */ + my_address->net = 0; /* local only, no routing */ my_address->len = 0; for (i = 0; i < MAX_MAC_LEN; i++) { my_address->adr[i] = 0; @@ -1398,16 +1380,15 @@ void dlmstp_get_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/ports/at91sam7s/init.c b/ports/at91sam7s/init.c index 78b6189a..b50aac11 100644 --- a/ports/at91sam7s/init.c +++ b/ports/at91sam7s/init.c @@ -1,39 +1,42 @@ -/* ---------------------------------------------------------------------------- */ +/* ---------------------------------------------------------------------------- + */ /* ATMEL Microcontroller Software Support - ROUSSET - */ -/* ---------------------------------------------------------------------------- */ +/* ---------------------------------------------------------------------------- + */ /* The software is delivered "AS IS" without warranty or condition of any */ /* kind, either express, implied or statutory. This includes without */ /* limitation any warranty or condition with respect to merchantability or */ /* fitness for any particular purpose, or against the infringements of */ /* intellectual property rights of others. */ -/* ---------------------------------------------------------------------------- */ +/* ---------------------------------------------------------------------------- + */ /* File Name : Cstartup_SAM7.c */ -/* Object : Low level initializations written in C for IAR tools */ +/* Object : Low level initializations written in C for IAR tools + */ /* 1.0 08/Sep/04 JPP : Creation */ /* 1.10 10/Sep/04 JPP : Update AT91C_CKGR_PLLCOUNT filed */ -/* ---------------------------------------------------------------------------- */ - +/* ---------------------------------------------------------------------------- + */ /* Include the board file description */ #include "board.h" -/* The following functions must be write in ARM mode this function called directly */ +/* 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 */ /** \brief This function performs very low level HW initialization */ /** 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; @@ -57,7 +60,8 @@ void LowLevelInit( 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 */ /* */ @@ -65,9 +69,11 @@ void LowLevelInit( /* MUL = 72 */ /* PLLCOUNT = 10 */ /* */ - /* Main Clock (MAINCK from crystal oscillator) = 18432000 hz (see AT91SAM7-EK schematic) */ + /* Main Clock (MAINCK from crystal oscillator) = 18432000 hz (see + * AT91SAM7-EK schematic) */ /* MAINCK / DIV = 18432000/14 = 1316571 hz */ - /* PLLCK = 1316571 * (MUL + 1) = 1316571 * (72 + 1) = 1316571 * 73 = 96109683 hz */ + /* PLLCK = 1316571 * (MUL + 1) = 1316571 * (72 + 1) = 1316571 * 73 = + * 96109683 hz */ /* */ /* PLLCOUNT = number of slow clock cycles before the LOCK bit is set */ /* in PMC_SR after CKGR_PLLR is written. */ @@ -76,12 +82,12 @@ void LowLevelInit( /* */ /* 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))); + pPMC->PMC_PLLR = ((AT91C_CKGR_DIV & 14) | + (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 */ /* */ @@ -94,9 +100,9 @@ void LowLevelInit( 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; + AT91C_BASE_AIC->AIC_SVR[0] = (int)AT91F_Default_FIQ_handler; for (i = 1; i < 31; i++) { - AT91C_BASE_AIC->AIC_SVR[i] = (int) AT91F_Default_IRQ_handler; + 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/ports/at91sam7s/irq.c b/ports/at91sam7s/irq.c index 907e0d7b..a0a68200 100644 --- a/ports/at91sam7s/irq.c +++ b/ports/at91sam7s/irq.c @@ -1,32 +1,40 @@ /* The following functions must be written in ARM mode */ /* these functions are called directly by an exception vector */ -/*------------------------------------------------------------------------------ */ +/*------------------------------------------------------------------------------ + */ /* Internal functions */ -/*------------------------------------------------------------------------------ */ -/*------------------------------------------------------------------------------ */ +/*------------------------------------------------------------------------------ + */ +/*------------------------------------------------------------------------------ + */ /* Default spurious interrupt handler. Infinite loop. */ -/*------------------------------------------------------------------------------ */ -void AT91F_Spurious_handler( - void) +/*------------------------------------------------------------------------------ + */ +void AT91F_Spurious_handler(void) { - while (1); + while (1) + ; } -/*------------------------------------------------------------------------------ */ +/*------------------------------------------------------------------------------ + */ /* Default handler for fast interrupt requests. Infinite loop. */ -/*------------------------------------------------------------------------------ */ -void AT91F_Default_FIQ_handler( - void) +/*------------------------------------------------------------------------------ + */ +void AT91F_Default_FIQ_handler(void) { - while (1); + while (1) + ; } -/*------------------------------------------------------------------------------ */ +/*------------------------------------------------------------------------------ + */ /* Default handler for standard interrupt requests. Infinite loop. */ -/*------------------------------------------------------------------------------ */ -void AT91F_Default_IRQ_handler( - void) +/*------------------------------------------------------------------------------ + */ +void AT91F_Default_IRQ_handler(void) { - while (1); + while (1) + ; } diff --git a/ports/at91sam7s/isr.c b/ports/at91sam7s/isr.c index 3ac7bcee..9781354a 100644 --- a/ports/at91sam7s/isr.c +++ b/ports/at91sam7s/isr.c @@ -1,4 +1,5 @@ -/* ********************************************************************************************** */ +/* ********************************************************************************************** + */ /* */ /* File Name : isr.c */ /* Title : interrupt enable/disable functions */ @@ -12,10 +13,13 @@ /* notice remains intact. */ /* */ /* Note from Jim Lynch: */ -/* This module was developed by Bill Knight, RO Software and used with his permission. */ +/* This module was developed by Bill Knight, RO Software and used with his + * permission. */ /* Taken from the Yahoo LPC2000 User's Group - Files Section 'UT050418A.ZIP' */ -/* Specifically, the module armVIC.c with the include file references removed */ -/* ********************************************************************************************** */ +/* Specifically, the module armVIC.c with the include file references removed + */ +/* ********************************************************************************************** + */ #include "at91sam7s256.h" #include "isr.h" @@ -23,23 +27,19 @@ #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(); @@ -47,8 +47,7 @@ unsigned disableIRQ( return _cpsr; } -unsigned restoreIRQ( - unsigned oldCPSR) +unsigned restoreIRQ(unsigned oldCPSR) { unsigned _cpsr; @@ -57,8 +56,7 @@ unsigned restoreIRQ( return _cpsr; } -unsigned enableIRQ( - void) +unsigned enableIRQ(void) { unsigned _cpsr; @@ -67,8 +65,7 @@ unsigned enableIRQ( return _cpsr; } -unsigned disableFIQ( - void) +unsigned disableFIQ(void) { unsigned _cpsr; @@ -77,8 +74,7 @@ unsigned disableFIQ( return _cpsr; } -unsigned restoreFIQ( - unsigned oldCPSR) +unsigned restoreFIQ(unsigned oldCPSR) { unsigned _cpsr; @@ -87,8 +83,7 @@ unsigned restoreFIQ( return _cpsr; } -unsigned enableFIQ( - void) +unsigned enableFIQ(void) { unsigned _cpsr; diff --git a/ports/at91sam7s/main.c b/ports/at91sam7s/main.c index 58e906dd..09613cbc 100644 --- a/ports/at91sam7s/main.c +++ b/ports/at91sam7s/main.c @@ -1,28 +1,28 @@ /************************************************************************** -* -* Copyright (C) 2007 Steve Karg -* Portions of the AT91SAM7S startup code were developed by James P Lynch. -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2007 Steve Karg + * Portions of the AT91SAM7S startup code were developed by James P Lynch. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* hardware specific */ #include "board.h" #include "timer.h" @@ -48,12 +48,9 @@ /* ******************************************************* */ /* 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); /* used by crt.s file */ unsigned FiqCount = 0; @@ -64,8 +61,7 @@ static unsigned long LED_Timer_3 = 0; static unsigned long LED_Timer_4 = 1000; static unsigned long DCC_Timer = 1000; -static inline void millisecond_timer( - void) +static inline void millisecond_timer(void) { while (Timer_Milliseconds) { Timer_Milliseconds--; @@ -88,8 +84,7 @@ static inline void millisecond_timer( /* note: MS/TP silence timer is updated in ISR */ } -static inline void init( - void) +static inline void init(void) { unsigned int pcsr; @@ -133,8 +128,7 @@ static inline void init( pAIC->AIC_IECR = (1 << AT91C_ID_FIQ); } -static inline void bacnet_init( - void) +static inline void bacnet_init(void) { #if defined(BACDL_MSTP) uint8_t MAC_Address = 0x55; @@ -149,31 +143,29 @@ static inline void bacnet_init( /* initialize objects */ Device_Init(NULL); /* set up our confirmed service unrecognized service handler - required! */ - apdu_set_unrecognized_service_handler_handler - (handler_unrecognized_service); + apdu_set_unrecognized_service_handler_handler(handler_unrecognized_service); apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_HAS, handler_who_has); /* we need to handle who-is to support dynamic device binding */ 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, - handler_read_property); - apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROP_MULTIPLE, - handler_read_property_multiple); - apdu_set_confirmed_handler(SERVICE_CONFIRMED_REINITIALIZE_DEVICE, - handler_reinitialize_device); - apdu_set_confirmed_handler(SERVICE_CONFIRMED_WRITE_PROPERTY, - handler_write_property); + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_READ_PROPERTY, handler_read_property); + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_READ_PROP_MULTIPLE, handler_read_property_multiple); + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_REINITIALIZE_DEVICE, handler_reinitialize_device); + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_WRITE_PROPERTY, handler_write_property); /* handle communication so we can shutup when asked */ apdu_set_confirmed_handler(SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL, handler_device_communication_control); } -static uint8_t Receive_PDU[MAX_MPDU]; /* PDU data */ -int main( - void) +static uint8_t Receive_PDU[MAX_MPDU]; /* PDU data */ +int main(void) { - unsigned long IdleCount = 0; /* idle loop blink counter */ + unsigned long IdleCount = 0; /* idle loop blink counter */ bool LED1_Off_Enabled = true; bool LED2_Off_Enabled = true; bool LED3_Off_Enabled = true; diff --git a/ports/at91sam7s/rs485.c b/ports/at91sam7s/rs485.c index a67e2278..b7d35063 100644 --- a/ports/at91sam7s/rs485.c +++ b/ports/at91sam7s/rs485.c @@ -1,28 +1,28 @@ /************************************************************************** -* -* Copyright (C) 2007 Steve Karg -* RS-485 initialization on AT91SAM7S inspired by Keil Eletronik serial.c -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2007 Steve Karg + * RS-485 initialization on AT91SAM7S inspired by Keil Eletronik serial.c + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* The module handles sending data out the RS-485 port */ /* and handles receiving data from the RS-485 port. */ @@ -52,18 +52,17 @@ static int RS485_Baud = 38400; /* At 76800 baud, 40 bit times would be about 0.520 milliseconds */ /* At 115200 baud, 40 bit times would be about 0.347 milliseconds */ /* 40 bits is 4 octets including a start and stop bit with each octet */ -#define Tturnaround (40UL) +#define Tturnaround (40UL) /* turnaround_time_milliseconds = (Tturnaround*1000UL)/RS485_Baud; */ /**************************************************************************** -* DESCRIPTION: Initializes the RS485 hardware and variables, and starts in -* receive mode. -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -void RS485_Initialize( - void) + * DESCRIPTION: Initializes the RS485 hardware and variables, and starts in + * receive mode. + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +void RS485_Initialize(void) { unsigned int pcsr; /* Enable the USART0 clock in the Power Management Controller */ @@ -81,17 +80,18 @@ void RS485_Initialize( /* 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 */ + 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; @@ -102,38 +102,34 @@ void RS485_Initialize( /* baud rate */ 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) { - } /**************************************************************************** -* DESCRIPTION: Returns the baud rate that we are currently running at -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -uint32_t RS485_Get_Baud_Rate( - void) + * DESCRIPTION: Returns the baud rate that we are currently running at + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +uint32_t RS485_Get_Baud_Rate(void) { return RS485_Baud; } /**************************************************************************** -* DESCRIPTION: Sets the baud rate for the chip USART -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -bool RS485_Set_Baud_Rate( - uint32_t baud) + * DESCRIPTION: Sets the baud rate for the chip USART + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +bool RS485_Set_Baud_Rate(uint32_t baud) { bool valid = true; @@ -157,13 +153,12 @@ bool RS485_Set_Baud_Rate( } /**************************************************************************** -* DESCRIPTION: Waits on the SilenceTimer for 40 bits. -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -void RS485_Turnaround_Delay( - void) + * DESCRIPTION: Waits on the SilenceTimer for 40 bits. + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +void RS485_Turnaround_Delay(void) { uint16_t turnaround_time; @@ -177,27 +172,25 @@ void RS485_Turnaround_Delay( } /**************************************************************************** -* DESCRIPTION: Enable or disable the transmitter -* RETURN: none -* ALGORITHM: none -* NOTES: The Atmel ARM7 has an automatic enable/disable in RS485 mode. -*****************************************************************************/ -void RS485_Transmitter_Enable( - bool enable) + * DESCRIPTION: Enable or disable the transmitter + * RETURN: none + * ALGORITHM: none + * NOTES: The Atmel ARM7 has an automatic enable/disable in RS485 mode. + *****************************************************************************/ +void RS485_Transmitter_Enable(bool enable) { - (void) enable; + (void)enable; } /**************************************************************************** -* DESCRIPTION: Send some data and wait until it is sent -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -void RS485_Send_Data( - uint8_t * buffer, /* data to send */ + * DESCRIPTION: Send some data and wait until it is sent + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +void RS485_Send_Data(uint8_t *buffer, /* data to send */ uint16_t nbytes) -{ /* number of bytes of data */ +{ /* number of bytes of data */ /* LED on send */ volatile AT91PS_PIO pPIO = AT91C_BASE_PIOA; /* LED ON */ @@ -219,13 +212,12 @@ void RS485_Send_Data( } /**************************************************************************** -* DESCRIPTION: Return true if a framing or overrun error is present -* RETURN: true if error -* ALGORITHM: none -* NOTES: Clears any error flags. -*****************************************************************************/ -bool RS485_ReceiveError( - void) + * DESCRIPTION: Return true if a framing or overrun error is present + * RETURN: true if error + * ALGORITHM: none + * NOTES: Clears any error flags. + *****************************************************************************/ +bool RS485_ReceiveError(void) { bool ReceiveError = false; /* LED on send */ @@ -244,13 +236,12 @@ bool RS485_ReceiveError( } /**************************************************************************** -* DESCRIPTION: Return true if data is available -* RETURN: true if data is available, with the data in the parameter set -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -bool RS485_DataAvailable( - uint8_t * DataRegister) + * DESCRIPTION: Return true if data is available + * RETURN: true if data is available, with the data in the parameter set + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +bool RS485_DataAvailable(uint8_t *DataRegister) { bool DataAvailable = false; /* LED on send */ @@ -270,8 +261,7 @@ bool RS485_DataAvailable( } #ifdef TEST_RS485 -int main( - void) +int main(void) { unsigned i = 0; uint8_t DataRegister; diff --git a/ports/at91sam7s/timer.c b/ports/at91sam7s/timer.c index 5e319eb8..14c6145a 100644 --- a/ports/at91sam7s/timer.c +++ b/ports/at91sam7s/timer.c @@ -1,4 +1,5 @@ -/* ***************************************************************************** */ +/* ***************************************************************************** + */ /* */ /* Purpose: Set up the 16-bit Timer/Counter */ /* */ @@ -34,8 +35,8 @@ /* Modified by Steve Karg */ /* Changed timer to 1ms. */ /* Encapsulated the intialization */ -/* ***************************************************************************** */ - +/* ***************************************************************************** + */ /********************************************************** Header files @@ -50,18 +51,21 @@ 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) */ /* */ - /* |------------------------------------------------------------------|------| */ - /* | SYNC | */ - /* |------------------------------------------------------------------|------| */ - /* 31 1 0 */ + /* |------------------------------------------------------------------|------| + */ + /* | SYNC | */ + /* |------------------------------------------------------------------|------| + */ + /* 31 1 0 + */ /* */ /* SYNC = 0 (no effect) <===== take default */ - /* SYNC = 1 (generate software trigger for all 3 timer channels simultaneously) */ + /* SYNC = 1 (generate software trigger for all 3 timer channels + * simultaneously) */ /* */ /* create a pointer to TC Global Register structure */ AT91PS_TCB pTCB = AT91C_BASE_TCB; @@ -70,10 +74,13 @@ static void Timer0_Setup( /* TC Block Mode Register TC_BMR (read/write) */ /* */ - /* |-------------------------------------|-----------|-----------|-----------| */ - /* | TC2XC2S TCXC1S TC0XC0S | */ - /* |-------------------------------------|-----------|-----------|-----------| */ - /* 31 5 4 3 2 1 0 */ + /* |-------------------------------------|-----------|-----------|-----------| + */ + /* | TC2XC2S TCXC1S TC0XC0S + * | */ + /* |-------------------------------------|-----------|-----------|-----------| + */ + /* 31 5 4 3 2 1 0 */ /* */ /* TC0XC0S Select = 00 TCLK0 (PA4) */ /* = 01 none <===== we select this one */ @@ -93,13 +100,16 @@ static void Timer0_Setup( /* external clocks not used */ pTCB->TCB_BMR = 0x15; - /* TC Channel Control Register TC_CCR (read/write) */ /* */ - /* |----------------------------------|--------------|------------|-----------| */ - /* | SWTRG CLKDIS CLKENS | */ - /* |----------------------------------|--------------|------------|-----------| */ - /* 31 2 1 0 */ + /* |----------------------------------|--------------|------------|-----------| + */ + /* | SWTRG CLKDIS CLKENS + * | */ + /* |----------------------------------|--------------|------------|-----------| + */ + /* 31 2 1 0 + */ /* */ /* CLKEN = 0 no effect */ /* CLKEN = 1 enables the clock <===== we select this one */ @@ -108,7 +118,8 @@ static void Timer0_Setup( /* CLKDIS = 1 disables the clock */ /* */ /* SWTRG = 0 no effect */ - /* SWTRG = 1 software trigger aserted counter reset and clock starts <===== we select this one */ + /* SWTRG = 1 software trigger aserted counter reset and clock starts + * <===== we select this one */ /* */ /* create a pointer to channel 0 Register structure */ AT91PS_TC pTC = AT91C_BASE_TC0; @@ -137,27 +148,32 @@ static void Timer0_Setup( /* 001 TIMER_CLOCK2 (MCK/8 = 6006855 hz) */ /* 010 TIMER_CLOCK3 (MCK/32 = 1501713 hz) */ /* 011 TIMER_CLOCK4 (MCK/128 = 375428 hz) */ - /* 100 TIMER_CLOCK5 (MCK/1024 = 46928 hz) <===== we select this one */ + /* 100 TIMER_CLOCK5 (MCK/1024 = 46928 hz) <===== we + * select this one */ /* 101 XC0 */ /* 101 XC1 */ /* 101 XC2 */ /* */ /* CLOCK INVERT */ - /* CLKI = 0 counter incremented on rising clock edge <===== we select this one */ + /* CLKI = 0 counter incremented on rising clock edge <===== we + * select this one */ /* CLKI = 1 counter incremented on falling clock edge */ /* */ /* BURST SIGNAL SELECTION */ - /* BURST = 00 clock is not gated by any external system <===== take default */ + /* BURST = 00 clock is not gated by any external system <===== take + * default */ /* 01 XC0 is anded with the clock */ /* 10 XC1 is anded with the clock */ /* 11 XC2 is anded with the clock */ /* */ /* COUNTER CLOCK STOPPED WITH RB LOADING */ - /* LDBSTOP = 0 counter clock is not stopped when RB loading occurs <===== take default */ + /* LDBSTOP = 0 counter clock is not stopped when RB loading occurs + * <===== take default */ /* = 1 counter clock is stopped when RB loading occur */ /* */ /* COUNTER CLOCK DISABLE WITH RB LOADING */ - /* LDBDIS = 0 counter clock is not disabled when RB loading occurs <===== take default */ + /* LDBDIS = 0 counter clock is not disabled when RB loading occurs + * <===== take default */ /* = 1 counter clock is disabled when RB loading occurs */ /* */ /* EXTERNAL TRIGGER EDGE SELECTION */ @@ -172,7 +188,8 @@ static void Timer0_Setup( /* */ /* RC COMPARE TRIGGER ENABLE */ /* CPCTRG = 0 (RC Compare has no effect on the counter and its clock) */ - /* 1 (RC Compare resets the counter and starts the clock) <===== we select this one */ + /* 1 (RC Compare resets the counter and starts the clock) + * <===== we select this one */ /* */ /* WAVE */ /* WAVE = 0 Capture Mode is enabled <===== we select this one */ @@ -195,12 +212,15 @@ static void Timer0_Setup( /* WAVE = 0 (Capture mode enabled) */ pTC->TC_CMR = 0x4004; - /* TC Register C TC_RC (read/write) Compare Register 16-bits */ + /* TC Register C TC_RC (read/write) Compare Register 16-bits + */ /* */ - /* |----------------------------------|----------------------------------------| */ - /* | not used RC | */ - /* |----------------------------------|----------------------------------------| */ - /* 31 16 15 0 */ + /* |----------------------------------|----------------------------------------| + */ + /* | not used RC | */ + /* |----------------------------------|----------------------------------------| + */ + /* 31 16 15 0 */ /* */ /* Timer Calculation: What count gives 1 msec time-out? */ /* */ @@ -221,10 +241,14 @@ static void Timer0_Setup( /* TC Interrupt Enable Register TC_IER (write-only) */ /* */ /* */ - /* |------------|-------|-------|-------|-------|--------|--------|--------|--------| */ - /* | ETRGS LDRBS LDRAS CPCS CPBS CPAS LOVRS COVFS | */ - /* |------------|-------|-------|-------|-------|--------|--------|--------|--------| */ - /* 31 8 7 6 5 4 3 2 1 0 */ + /* |------------|-------|-------|-------|-------|--------|--------|--------|--------| + */ + /* | ETRGS LDRBS LDRAS CPCS CPBS CPAS LOVRS + * COVFS | */ + /* |------------|-------|-------|-------|-------|--------|--------|--------|--------| + */ + /* 31 8 7 6 5 4 3 2 1 + * 0 */ /* */ /* COVFS = 0 no effect <===== take default */ /* 1 enable counter overflow interrupt */ @@ -239,7 +263,8 @@ static void Timer0_Setup( /* 1 enable RB compare interrupt */ /* */ /* CPCS = 0 no effect */ - /* 1 enable RC compare interrupt <===== we select this one */ + /* 1 enable RC compare interrupt <===== we select this one + */ /* */ /* LDRAS = 0 no effect <===== take default */ /* 1 enable RA load interrupt */ @@ -256,22 +281,30 @@ static void Timer0_Setup( /* TC Interrupt Disable Register TC_IDR (write-only) */ /* */ /* */ - /* |------------|-------|-------|-------|-------|--------|--------|--------|--------| */ - /* | ETRGS LDRBS LDRAS CPCS CPBS CPAS LOVRS COVFS | */ - /* |------------|-------|-------|-------|-------|--------|--------|--------|--------| */ - /* 31 8 7 6 5 4 3 2 1 0 */ + /* |------------|-------|-------|-------|-------|--------|--------|--------|--------| + */ + /* | ETRGS LDRBS LDRAS CPCS CPBS CPAS LOVRS + * COVFS | */ + /* |------------|-------|-------|-------|-------|--------|--------|--------|--------| + */ + /* 31 8 7 6 5 4 3 2 1 + * 0 */ /* */ /* COVFS = 0 no effect */ - /* 1 disable counter overflow interrupt <===== we select this one */ + /* 1 disable counter overflow interrupt <===== we select + * this one */ /* */ /* LOVRS = 0 no effect */ - /* 1 disable load overrun interrupt <===== we select this one */ + /* 1 disable load overrun interrupt <===== we select this + * one */ /* */ /* CPAS = 0 no effect */ - /* 1 disable RA compare interrupt <===== we select this one */ + /* 1 disable RA compare interrupt <===== we select this one + */ /* */ /* CPBS = 0 no effect */ - /* 1 disable RB compare interrupt <===== we select this one */ + /* 1 disable RB compare interrupt <===== we select this one + */ /* */ /* CPCS = 0 no effect <===== take default */ /* 1 disable RC compare interrupt */ @@ -283,13 +316,15 @@ static void Timer0_Setup( /* 1 disable RB load interrupt <===== we select this one */ /* */ /* ETRGS = 0 no effect */ - /* 1 disable External Trigger interrupt <===== we select this one */ + /* 1 disable External Trigger interrupt <===== we select + * this one */ /* */ /* disable all except RC compare interrupt */ pTC->TC_IDR = 0xEF; } -/* ***************************************************************************** */ +/* ***************************************************************************** + */ /* */ /* Timer 0 Interrupt Service Routine */ /* */ @@ -298,13 +333,13 @@ static void Timer0_Setup( /* Author: James P Lynch May 12, 2007 */ /* 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 unsigned int dummy; /* temporary */ + volatile AT91PS_TC pTC = + AT91C_BASE_TC0; /* pointer to timer channel 0 register structure */ + volatile unsigned int dummy; /* temporary */ /* read TC0 Status Register to clear interrupt */ dummy = pTC->TC_SR; @@ -316,19 +351,18 @@ static void Timer0IrqHandler( (void)dummy; } -int Timer_Silence( - void) +int Timer_Silence(void) { return SilenceTime; } -void Timer_Silence_Reset( - void) +void Timer_Silence_Reset(void) { SilenceTime = 0; } -/* ***************************************************************************** */ +/* ***************************************************************************** + */ /* */ /* Timer 0 Initialization */ /* */ @@ -336,9 +370,9 @@ void Timer_Silence_Reset( /* Modified by Steve Karg */ /* Moved timer startup code from main */ /* modified the peripheral clock init */ -/* ***************************************************************************** */ -void TimerInit( - void) +/* ***************************************************************************** + */ +void TimerInit(void) { unsigned int pcsr; /* enable the Timer0 peripheral clock */ @@ -353,11 +387,10 @@ void TimerInit( pAIC->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); + pAIC->AIC_SMR[AT91C_ID_TC0] = (AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE | 0x4); /* Clear the TC0 interrupt */ /* in AIC Interrupt Clear Command Register */ pAIC->AIC_ICCR = (1 << AT91C_ID_TC0); diff --git a/ports/atmega168/ai.c b/ports/atmega168/ai.c index 7a3d3581..33493f2a 100644 --- a/ports/atmega168/ai.c +++ b/ports/atmega168/ai.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2005 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2005 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* Analog Input Objects customize for your use */ @@ -44,8 +44,7 @@ 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; @@ -54,34 +53,29 @@ bool Analog_Input_Valid_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; } @@ -90,34 +84,31 @@ char *Analog_Input_Name( /* 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, uint32_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; - - (void) array_index; + (void)array_index; switch (property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], OBJECT_ANALOG_INPUT, - object_instance); + 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)); + characterstring_init_ansi( + &char_string, Analog_Input_Name(object_instance)); apdu_len = encode_application_character_string(&apdu[0], &char_string); break; diff --git a/ports/atmega168/apdu.c b/ports/atmega168/apdu.c index 20df3a77..41f1e93d 100644 --- a/ports/atmega168/apdu.c +++ b/ports/atmega168/apdu.c @@ -41,8 +41,7 @@ #include "bacnet/bacenum.h" #include "bacnet/basic/services.h" -bool apdu_service_supported( - BACNET_SERVICES_SUPPORTED service_supported) +bool apdu_service_supported(BACNET_SERVICES_SUPPORTED service_supported) { bool status = false; @@ -61,15 +60,14 @@ bool apdu_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) + BACNET_CONFIRMED_SERVICE_DATA *service_data, + uint8_t *service_choice, + 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] & BIT(3)) ? true : false; service_data->more_follows = (apdu[0] & BIT(2)) ? true : false; @@ -90,22 +88,22 @@ uint16_t apdu_decode_confirmed_service_request( 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 */ + 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) { @@ -114,8 +112,8 @@ void apdu_handler( } #ifdef WRITE_PROPERTY else if (service_choice == SERVICE_CONFIRMED_WRITE_PROPERTY) { - handler_write_property(service_request, - service_request_len, src, &service_data); + handler_write_property(service_request, service_request_len, + src, &service_data); } #endif else { diff --git a/ports/atmega168/av.c b/ports/atmega168/av.c index 8ebc1ead..88a60ffb 100644 --- a/ports/atmega168/av.c +++ b/ports/atmega168/av.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2006 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2006 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* Analog Value Objects - customize for your use */ @@ -32,7 +32,7 @@ #include "bacnet/bacdcode.h" #include "bacnet/bacenum.h" #include "bacnet/bacapp.h" -#include "bacnet/config.h" /* the custom stuff */ +#include "bacnet/config.h" /* the custom stuff */ #include "bacnet/wp.h" #include "bacnet/basic/object/av.h" @@ -49,8 +49,7 @@ float AV_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; @@ -60,8 +59,7 @@ bool Analog_Value_Valid_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; } @@ -69,8 +67,7 @@ unsigned Analog_Value_Count( /* 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; } @@ -78,28 +75,25 @@ uint32_t Analog_Value_Index_To_Instance( /* 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-"; /* okay for single thread */ - text_string[3] = '0' + (uint8_t) object_instance; + text_string[3] = '0' + (uint8_t)object_instance; return text_string; } /* return apdu len, or -1 on error */ -int Analog_Value_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Analog_Value_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { - 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; @@ -108,13 +102,12 @@ int Analog_Value_Read_Property( apdu = rpdata->application_data; switch (rpdata->object_property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], OBJECT_ANALOG_VALUE, - rpdata->object_instance); + apdu_len = encode_application_object_id( + &apdu[0], OBJECT_ANALOG_VALUE, rpdata->object_instance); break; case PROP_OBJECT_NAME: - characterstring_init_ansi(&char_string, - Analog_Value_Name(rpdata->object_instance)); + characterstring_init_ansi( + &char_string, Analog_Value_Name(rpdata->object_instance)); apdu_len = encode_application_character_string(&apdu[0], &char_string); break; @@ -123,11 +116,10 @@ int Analog_Value_Read_Property( encode_application_enumerated(&apdu[0], OBJECT_ANALOG_VALUE); break; case PROP_PRESENT_VALUE: - object_index = Analog_Value_Instance_To_Index( - rpdata->object_instance); - apdu_len = - encode_application_real(&apdu[0], - AV_Present_Value[object_index]); + object_index = + Analog_Value_Instance_To_Index(rpdata->object_instance); + apdu_len = encode_application_real( + &apdu[0], AV_Present_Value[object_index]); break; case PROP_STATUS_FLAGS: bitstring_init(&bit_string); @@ -154,8 +146,7 @@ int Analog_Value_Read_Property( break; } /* only array properties can have array options */ - if ((apdu_len >= 0) && - (rpdata->array_index != BACNET_ARRAY_ALL)) { + if ((apdu_len >= 0) && (rpdata->array_index != BACNET_ARRAY_ALL)) { rpdata->error_class = ERROR_CLASS_PROPERTY; rpdata->error_code = ERROR_CODE_PROPERTY_IS_NOT_AN_ARRAY; apdu_len = BACNET_STATUS_ERROR; @@ -165,10 +156,9 @@ int Analog_Value_Read_Property( } /* returns true if successful */ -bool Analog_Value_Write_Property( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Analog_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data) { - bool status = false; /* return value */ + bool status = false; /* return value */ unsigned int object_index = 0; int len = 0; BACNET_APPLICATION_DATA_VALUE value; @@ -179,9 +169,8 @@ bool Analog_Value_Write_Property( return false; } /* decode the some of the request */ - len = - bacapp_decode_application_data(wp_data->application_data, - wp_data->application_data_len, &value); + len = bacapp_decode_application_data( + wp_data->application_data, wp_data->application_data_len, &value); /* FIXME: len < application_data_len: more data? */ if (len < 0) { /* error while decoding - a value larger than we can handle */ @@ -233,8 +222,7 @@ bool Analog_Value_Write_Property( #include #include "ctest.h" -void testAnalog_Value( - Test * pTest) +void testAnalog_Value(Test *pTest) { uint8_t apdu[MAX_APDU] = { 0 }; int len = 0; @@ -246,14 +234,12 @@ void testAnalog_Value( BACNET_ERROR_CLASS error_class; BACNET_ERROR_CODE error_code; - len = - Analog_Value_Encode_Property_APDU(&apdu[0], instance, + len = Analog_Value_Encode_Property_APDU(&apdu[0], instance, 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); - len = - decode_object_id(&apdu[len], (int *) &decoded_type, &decoded_instance); + len = decode_object_id(&apdu[len], (int *)&decoded_type, &decoded_instance); ct_test(pTest, decoded_type == OBJECT_ANALOG_VALUE); ct_test(pTest, decoded_instance == instance); @@ -261,8 +247,7 @@ void testAnalog_Value( } #ifdef TEST_ANALOG_VALUE -int main( - void) +int main(void) { Test *pTest; bool rc; @@ -274,7 +259,7 @@ int main( ct_setStream(pTest, stdout); ct_run(pTest); - (void) ct_report(pTest); + (void)ct_report(pTest); ct_destroy(pTest); return 0; diff --git a/ports/atmega168/bv.c b/ports/atmega168/bv.c index db8b8ab0..1e49f35d 100644 --- a/ports/atmega168/bv.c +++ b/ports/atmega168/bv.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2006 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2006 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* Binary Value Objects - customize for your use */ @@ -32,7 +32,7 @@ #include "bacnet/bacdef.h" #include "bacnet/bacdcode.h" #include "bacnet/bacenum.h" -#include "bacnet/config.h" /* the custom stuff */ +#include "bacnet/config.h" /* the custom stuff */ #include "bacnet/wp.h" #include "bacnet/basic/object/bv.h" @@ -47,8 +47,7 @@ static BACNET_BINARY_PV Present_Value[MAX_BINARY_VALUES]; /* 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; @@ -57,22 +56,19 @@ bool Binary_Value_Valid_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; @@ -82,8 +78,7 @@ unsigned Binary_Value_Instance_To_Index( return index; } -BACNET_BINARY_PV Binary_Value_Present_Value( - uint32_t object_instance) +BACNET_BINARY_PV Binary_Value_Present_Value(uint32_t object_instance) { BACNET_BINARY_PV value = BINARY_INACTIVE; @@ -95,13 +90,12 @@ BACNET_BINARY_PV Binary_Value_Present_Value( } /* 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[5] = "BV-0"; /* okay for single thread */ + static char text_string[5] = "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; } @@ -109,10 +103,9 @@ char *Binary_Value_Name( } /* return apdu len, or -1 on error */ -int Binary_Value_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Binary_Value_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { - 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; @@ -122,15 +115,14 @@ int Binary_Value_Read_Property( apdu = rpdata->application_data; switch (rpdata->object_property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], OBJECT_BINARY_VALUE, - rpdata->object_instance); + apdu_len = encode_application_object_id( + &apdu[0], OBJECT_BINARY_VALUE, rpdata->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: - characterstring_init_ansi(&char_string, - Binary_Value_Name(rpdata->object_instance)); + characterstring_init_ansi( + &char_string, Binary_Value_Name(rpdata->object_instance)); apdu_len = encode_application_character_string(&apdu[0], &char_string); break; @@ -180,10 +172,9 @@ int Binary_Value_Read_Property( } /* returns true if successful */ -bool Binary_Value_Write_Property( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Binary_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data) { - bool status = false; /* return value */ + bool status = false; /* return value */ unsigned int object_index = 0; int len = 0; BACNET_APPLICATION_DATA_VALUE value; @@ -194,9 +185,8 @@ bool Binary_Value_Write_Property( return false; } /* decode the some of the request */ - len = - bacapp_decode_application_data(wp_data->application_data, - wp_data->application_data_len, &value); + len = bacapp_decode_application_data( + wp_data->application_data, wp_data->application_data_len, &value); /* FIXME: len < application_data_len: more data? */ if (len < 0) { /* error while decoding - a value larger than we can handle */ @@ -216,12 +206,11 @@ bool Binary_Value_Write_Property( if (value.tag == BACNET_APPLICATION_TAG_ENUMERATED) { if ((value.type.Enumerated == BINARY_ACTIVE) || (value.type.Enumerated == BINARY_INACTIVE)) { - object_index = - Binary_Value_Instance_To_Index - (wp_data->object_instance); + object_index = Binary_Value_Instance_To_Index( + wp_data->object_instance); /* NOTE: this Binary value has no priority array */ Present_Value[object_index] = - (BACNET_BINARY_PV) value.type.Enumerated; + (BACNET_BINARY_PV)value.type.Enumerated; /* 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 @@ -277,8 +266,7 @@ bool Binary_Value_Write_Property( #include #include "ctest.h" -void testBinary_Value( - Test * pTest) +void testBinary_Value(Test *pTest) { uint8_t apdu[MAX_APDU] = { 0 }; int len = 0; @@ -290,15 +278,12 @@ void testBinary_Value( BACNET_ERROR_CLASS error_class; BACNET_ERROR_CODE error_code; - - len = - Binary_Value_Encode_Property_APDU(&apdu[0], instance, + len = Binary_Value_Encode_Property_APDU(&apdu[0], instance, 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); - len = - decode_object_id(&apdu[len], (int *) &decoded_type, &decoded_instance); + len = decode_object_id(&apdu[len], (int *)&decoded_type, &decoded_instance); ct_test(pTest, decoded_type == OBJECT_BINARY_VALUE); ct_test(pTest, decoded_instance == instance); @@ -306,8 +291,7 @@ void testBinary_Value( } #ifdef TEST_BINARY_VALUE -int main( - void) +int main(void) { Test *pTest; bool rc; @@ -319,7 +303,7 @@ int main( ct_setStream(pTest, stdout); ct_run(pTest); - (void) ct_report(pTest); + (void)ct_report(pTest); ct_destroy(pTest); return 0; diff --git a/ports/atmega168/device.c b/ports/atmega168/device.c index e3dc5534..38f080b4 100644 --- a/ports/atmega168/device.c +++ b/ports/atmega168/device.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2007 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2007 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include @@ -50,8 +50,7 @@ static uint32_t Object_Instance_Number = 260001; static char Object_Name[20] = "My Device"; static BACNET_DEVICE_STATUS System_Status = STATUS_OPERATIONAL; -void Device_Init( - object_functions_t * object_table) +void Device_Init(object_functions_t *object_table) { (void)object_table; /* Reinitialize_State = BACNET_REINIT_IDLE; */ @@ -64,14 +63,12 @@ void Device_Init( } /* 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 */ @@ -89,21 +86,18 @@ bool Device_Set_Object_Instance_Number( 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); } -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 */ @@ -115,9 +109,7 @@ unsigned Device_Object_List_Count( } bool Device_Object_List_Identifier( - uint32_t array_index, - BACNET_OBJECT_TYPE *object_type, - uint32_t * instance) + uint32_t array_index, BACNET_OBJECT_TYPE *object_type, uint32_t *instance) { bool status = false; uint32_t object_index = 0; @@ -163,12 +155,11 @@ bool Device_Object_List_Identifier( } /* return the length of the apdu encoded or -1 for error */ -int Device_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Device_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { - uint8_t * apdu; - int apdu_len = 0; /* return value */ - int len = 0; /* apdu len intermediate value */ + uint8_t *apdu; + int apdu_len = 0; /* return value */ + int len = 0; /* apdu len intermediate value */ BACNET_BIT_STRING bit_string; BACNET_CHARACTER_STRING char_string; uint32_t i = 0; @@ -179,9 +170,8 @@ int Device_Read_Property( apdu = rpdata->application_data; switch (rpdata->object_property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], OBJECT_DEVICE, - Object_Instance_Number); + 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); @@ -200,9 +190,8 @@ int Device_Read_Property( encode_application_character_string(&apdu[0], &char_string); break; case PROP_VENDOR_IDENTIFIER: - apdu_len = - encode_application_unsigned(&apdu[0], - Device_Vendor_Identifier()); + apdu_len = encode_application_unsigned( + &apdu[0], Device_Vendor_Identifier()); break; case PROP_MODEL_NAME: characterstring_init_ansi(&char_string, "GNU Demo"); @@ -225,16 +214,15 @@ int Device_Read_Property( break; case PROP_PROTOCOL_REVISION: apdu_len = - encode_application_unsigned(&apdu[0], - BACNET_PROTOCOL_REVISION); + encode_application_unsigned(&apdu[0], BACNET_PROTOCOL_REVISION); 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)); + 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; @@ -245,7 +233,7 @@ int Device_Read_Property( /* 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); + bitstring_set_bit(&bit_string, (uint8_t)i, false); } /* FIXME: indicate the objects that YOU support */ bitstring_set_bit(&bit_string, OBJECT_DEVICE, true); @@ -265,9 +253,8 @@ int Device_Read_Property( else if (rpdata->array_index == BACNET_ARRAY_ALL) { for (i = 1; i <= count; i++) { Device_Object_List_Identifier(i, &object_type, &instance); - len = - encode_application_object_id(&apdu[apdu_len], - 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? */ @@ -280,11 +267,10 @@ int Device_Read_Property( } } } else { - if (Device_Object_List_Identifier(rpdata->array_index, - &object_type, &instance)) - apdu_len = - encode_application_object_id(&apdu[0], object_type, - instance); + if (Device_Object_List_Identifier( + rpdata->array_index, &object_type, &instance)) + apdu_len = encode_application_object_id( + &apdu[0], object_type, instance); else { rpdata->error_class = ERROR_CLASS_PROPERTY; rpdata->error_code = ERROR_CODE_INVALID_ARRAY_INDEX; @@ -313,8 +299,7 @@ int Device_Read_Property( break; case PROP_MAX_INFO_FRAMES: apdu_len = - encode_application_unsigned(&apdu[0], - dlmstp_max_info_frames()); + encode_application_unsigned(&apdu[0], dlmstp_max_info_frames()); break; case PROP_MAX_MASTER: apdu_len = @@ -347,10 +332,9 @@ int Device_Read_Property( return apdu_len; } -bool Device_Write_Property( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data) { - bool status = false; /* return value */ + bool status = false; /* return value */ int len = 0; BACNET_APPLICATION_DATA_VALUE value; @@ -360,9 +344,8 @@ bool Device_Write_Property( return false; } /* decode the some of the request */ - len = - bacapp_decode_application_data(wp_data->application_data, - wp_data->application_data_len, &value); + len = bacapp_decode_application_data( + wp_data->application_data, wp_data->application_data_len, &value); /* FIXME: len < application_data_len: more data? */ if (len < 0) { /* error while decoding - a value larger than we can handle */ @@ -381,8 +364,8 @@ bool Device_Write_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))) { + (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 { @@ -436,11 +419,13 @@ bool Device_Write_Property( status = true; } else { wp_data->error_class = ERROR_CLASS_PROPERTY; - wp_data->error_code = ERROR_CODE_NO_SPACE_TO_WRITE_PROPERTY; + wp_data->error_code = + ERROR_CODE_NO_SPACE_TO_WRITE_PROPERTY; } } else { wp_data->error_class = ERROR_CLASS_PROPERTY; - wp_data->error_code = ERROR_CODE_CHARACTER_SET_NOT_SUPPORTED; + wp_data->error_code = + ERROR_CODE_CHARACTER_SET_NOT_SUPPORTED; } } else { wp_data->error_class = ERROR_CLASS_PROPERTY; diff --git a/ports/atmega168/dlmstp.c b/ports/atmega168/dlmstp.c index c57358e3..6d9381d1 100644 --- a/ports/atmega168/dlmstp.c +++ b/ports/atmega168/dlmstp.c @@ -66,23 +66,23 @@ static struct mstp_flag_t { /* A Boolean flag set to TRUE by the Receive State Machine */ /* if an invalid frame is received. */ /* Set to FALSE by the main state machine. */ - unsigned ReceivedInvalidFrame:1; + unsigned ReceivedInvalidFrame : 1; /* A Boolean flag set to TRUE by the Receive State Machine */ /* if a valid frame is received. */ /* Set to FALSE by the main state machine. */ - unsigned ReceivedValidFrame:1; + unsigned ReceivedValidFrame : 1; /* A Boolean flag set to TRUE by the master machine if this node is the */ /* only known master node. */ - unsigned SoleMaster:1; + unsigned SoleMaster : 1; /* A Boolean flag set TRUE by the datalink transmit if a frame is pending */ - unsigned TransmitPacketPending:1; + unsigned TransmitPacketPending : 1; /* A Boolean flag set TRUE by the datalink transmit if a pending packet is DataExpectingReply */ - unsigned TransmitPacketDER:1; + unsigned TransmitPacketDER : 1; /* A Boolean flag set TRUE by the datalink if a packet has been received, but not processed. */ - unsigned ReceivePacketPending:1; + unsigned ReceivePacketPending : 1; } MSTP_Flag; /* Used to store the data length of a received frame. */ @@ -165,10 +165,13 @@ static uint8_t TransmitPacketDest; #define Treply_delay 250 /* we need to be able to increment without rolling over */ -#define INCREMENT_AND_LIMIT_UINT8(x) {if (x < 0xFF) x++;} +#define INCREMENT_AND_LIMIT_UINT8(x) \ + { \ + if (x < 0xFF) \ + x++; \ + } -bool dlmstp_init( - char *ifname) +bool dlmstp_init(char *ifname) { ifname = ifname; /* initialize hardware */ @@ -177,15 +180,12 @@ bool dlmstp_init( 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; @@ -212,8 +212,7 @@ void dlmstp_fill_bacnet_address( extern bool Send_I_Am_Flag; /* 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; @@ -243,16 +242,16 @@ static uint16_t dlmstp_encode_unconfirmed_frame( /* (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 destination, /* destination address */ - uint8_t source, /* source address */ - uint8_t * pdu, /* any data to be sent - may be null */ + uint8_t destination, /* destination address */ + 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) */ - 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 */ +{ /* 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 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; @@ -290,8 +289,7 @@ 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; @@ -305,7 +303,8 @@ static void MSTP_Receive_Frame_FSM( switch (Receive_State) { case MSTP_RECEIVE_STATE_IDLE: - /* In the IDLE state, the node waits for the beginning of a frame. */ + /* In the IDLE state, the node waits for the beginning of a frame. + */ if (RS485_ReceiveError()) { /* EatAnError */ Timer_Silence_Reset(); @@ -355,10 +354,12 @@ static void MSTP_Receive_Frame_FSM( } break; case MSTP_RECEIVE_STATE_HEADER: - /* In the HEADER state, the node waits for the fixed message header. */ + /* In the HEADER state, the node waits for the fixed message header. + */ if (Timer_Silence() > Tframe_abort) { /* Timeout */ - /* indicate that an error has occurred during the reception of a frame */ + /* indicate that an error has occurred during the reception of a + * frame */ MSTP_Flag.ReceivedInvalidFrame = true; /* wait for the start of a frame. */ Receive_State = MSTP_RECEIVE_STATE_IDLE; @@ -366,7 +367,8 @@ static void MSTP_Receive_Frame_FSM( /* Error */ Timer_Silence_Reset(); INCREMENT_AND_LIMIT_UINT8(EventCount); - /* indicate that an error has occurred during the reception of a frame */ + /* indicate that an error has occurred during the reception of a + * frame */ MSTP_Flag.ReceivedInvalidFrame = true; /* wait for the start of a frame. */ Receive_State = MSTP_RECEIVE_STATE_IDLE; @@ -454,10 +456,12 @@ static void MSTP_Receive_Frame_FSM( } break; case MSTP_RECEIVE_STATE_DATA: - /* In the DATA state, the node waits for the data portion of a frame. */ + /* In the DATA state, the node waits for the data portion of a + * frame. */ if (Timer_Silence() > Tframe_abort) { /* Timeout */ - /* indicate that an error has occurred during the reception of a frame */ + /* indicate that an error has occurred during the reception of a + * frame */ MSTP_Flag.ReceivedInvalidFrame = true; /* wait for the start of the next frame. */ Receive_State = MSTP_RECEIVE_STATE_IDLE; @@ -512,15 +516,16 @@ static void MSTP_Receive_Frame_FSM( } /* 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 */ /* pass the token. */ static uint8_t FrameCount; - /* "Next Station," the MAC address of the node to which This Station passes */ - /* the token. If the Next_Station is unknown, Next_Station shall be equal to */ + /* "Next Station," the MAC address of the node to which This Station passes + */ + /* the token. If the Next_Station is unknown, Next_Station shall be equal to + */ /* This_Station. */ static uint8_t Next_Station; /* "Poll Station," the MAC address of the node to which This Station last */ @@ -529,7 +534,8 @@ static bool MSTP_Master_Node_FSM( /* A counter of transmission retries used for Token and Poll For Master */ /* transmission. */ static unsigned RetryCount; - /* The number of tokens received by this node. When this counter reaches the */ + /* The number of tokens received by this node. When this counter reaches the + */ /* value Npoll, the node polls the address range between TS and NS for */ /* additional master nodes. TokenCount is set to zero at the end of the */ /* polling process. */ @@ -603,7 +609,7 @@ static bool MSTP_Master_Node_FSM( MSTP_Flag.ReceivePacketPending = true; break; case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: - /* indicate successful reception to the higher layers */ + /* indicate successful reception to the higher layers */ MSTP_Flag.ReceivePacketPending = true; /* broadcast DER just remains IDLE */ if (DestinationAddress != MSTP_BROADCAST_ADDRESS) { @@ -612,9 +618,8 @@ static bool MSTP_Master_Node_FSM( } break; case FRAME_TYPE_TEST_REQUEST: - MSTP_Send_Frame(FRAME_TYPE_TEST_RESPONSE, - SourceAddress, This_Station, &InputBuffer[0], - DataLength); + MSTP_Send_Frame(FRAME_TYPE_TEST_RESPONSE, SourceAddress, + This_Station, &InputBuffer[0], DataLength); break; case FRAME_TYPE_TEST_RESPONSE: default: @@ -638,7 +643,7 @@ static bool MSTP_Master_Node_FSM( if (TransmitPacketLen) { MSTP_Send_Frame(FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY, MSTP_BROADCAST_ADDRESS, This_Station, - (uint8_t *) & TransmitPacket[0], TransmitPacketLen); + (uint8_t *)&TransmitPacket[0], TransmitPacketLen); FrameCount++; } else { /* NothingToSend */ @@ -656,8 +661,10 @@ static bool MSTP_Master_Node_FSM( 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 */ + /* 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 { @@ -682,8 +689,10 @@ static bool MSTP_Master_Node_FSM( 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 */ + /* 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; @@ -735,7 +744,8 @@ static bool MSTP_Master_Node_FSM( (Next_Station != next_this_station)) { /* SoleMaster */ /* there are no other known master nodes to */ - /* which the token may be sent (true master-slave operation). */ + /* which the token may be sent (true master-slave + * operation). */ FrameCount = 0; TokenCount++; Master_State = MSTP_MASTER_STATE_USE_TOKEN; @@ -743,13 +753,16 @@ static bool MSTP_Master_Node_FSM( } 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. */ + /* 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; @@ -763,7 +776,7 @@ static bool MSTP_Master_Node_FSM( /* no known successor node */ Next_Station = This_Station; RetryCount = 0; - TokenCount = 1; /* changed in Errata SSPC-135-2004 */ + 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; @@ -771,10 +784,10 @@ static bool MSTP_Master_Node_FSM( /* ResetMaintenancePFM */ Poll_Station = This_Station; /* 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; - TokenCount = 1; /* changed in Errata SSPC-135-2004 */ + TokenCount = 1; /* changed in Errata SSPC-135-2004 */ EventCount = 0; Master_State = MSTP_MASTER_STATE_PASS_TOKEN; } @@ -793,7 +806,8 @@ static bool MSTP_Master_Node_FSM( if (Timer_Silence() <= Tusage_timeout) { if (EventCount > Nmin_octets) { /* SawTokenUser */ - /* Assume that a frame has been sent by the new token user. */ + /* 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; @@ -803,8 +817,8 @@ static bool MSTP_Master_Node_FSM( /* RetrySendToken */ RetryCount++; /* 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); EventCount = 0; /* re-enter the current state to listen for NS */ /* to begin using the token. */ @@ -826,9 +840,11 @@ static bool MSTP_Master_Node_FSM( } } 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 */ + /* 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); @@ -836,7 +852,8 @@ static bool MSTP_Master_Node_FSM( if (EventCount > Nmin_octets) { /* SawFrame */ /* Some other node exists at a lower address. */ - /* Enter the IDLE state to receive and process the incoming frame. */ + /* Enter the IDLE state to receive and process the incoming + * frame. */ Master_State = MSTP_MASTER_STATE_IDLE; transition_now = true; } @@ -855,7 +872,8 @@ static bool MSTP_Master_Node_FSM( RetryCount = 0; TokenCount = 0; /* EventCount = 0; removed Addendum 135-2004d-8 */ - /* enter the POLL_FOR_MASTER state to find a new successor to TS. */ + /* enter the POLL_FOR_MASTER state to find a new successor + * to TS. */ Master_State = MSTP_MASTER_STATE_POLL_FOR_MASTER; } } @@ -865,15 +883,15 @@ static bool MSTP_Master_Node_FSM( /* 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)) { + 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); + MSTP_Send_Frame( + FRAME_TYPE_TOKEN, Next_Station, This_Station, NULL, 0); Poll_Station = This_Station; TokenCount = 0; RetryCount = 0; @@ -920,7 +938,8 @@ static bool MSTP_Master_Node_FSM( /* Re-enter the current state. */ } else { /* DeclareSoleMaster */ - /* to indicate that this station is the only master */ + /* to indicate that this station is the only master + */ MSTP_Flag.SoleMaster = true; FrameCount = 0; Master_State = MSTP_MASTER_STATE_USE_TOKEN; @@ -948,7 +967,7 @@ static bool MSTP_Master_Node_FSM( /* 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); + (uint8_t *)&TransmitPacket[0], TransmitPacketLen); MSTP_Flag.TransmitPacketPending = false; Master_State = MSTP_MASTER_STATE_IDLE; } else { @@ -977,12 +996,11 @@ static bool MSTP_Master_Node_FSM( } /* 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 */ +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 */ +{ /* number of bytes of data */ int bytes_sent = 0; if (MSTP_Flag.TransmitPacketPending == false) { @@ -1003,13 +1021,12 @@ int dlmstp_send_pdu( } /* 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 */ +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 */ +{ /* milliseconds to wait for a packet */ + uint16_t pdu_len = 0; /* return value */ /* dummy - unused parameter */ timeout = timeout; @@ -1051,8 +1068,7 @@ 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) { @@ -1069,8 +1085,7 @@ void dlmstp_set_mac_address( return; } -uint8_t dlmstp_mac_address( - void) +uint8_t dlmstp_mac_address(void) { return This_Station; } @@ -1082,8 +1097,7 @@ uint8_t dlmstp_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( - 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; @@ -1097,8 +1111,7 @@ void dlmstp_set_max_info_frames( return; } -uint8_t dlmstp_max_info_frames( - void) +uint8_t dlmstp_max_info_frames(void) { return Nmax_info_frames; } @@ -1108,8 +1121,7 @@ uint8_t dlmstp_max_info_frames( /* 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) { @@ -1125,20 +1137,18 @@ void dlmstp_set_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; - my_address->net = 0; /* local only, no routing */ + my_address->net = 0; /* local only, no routing */ my_address->len = 0; for (i = 0; i < MAX_MAC_LEN; i++) { my_address->adr[i] = 0; @@ -1147,16 +1157,15 @@ void dlmstp_get_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/ports/atmega168/h_rp.c b/ports/atmega168/h_rp.c index ee61b543..f87b73f6 100644 --- a/ports/atmega168/h_rp.c +++ b/ports/atmega168/h_rp.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2005 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2005 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include #include @@ -44,9 +44,7 @@ /* Encodes the property APDU and returns the length, or sets the error, and returns -1 */ -int Encode_Property_APDU( - uint8_t * apdu, - BACNET_READ_PROPERTY_DATA * rpdata) +int Encode_Property_APDU(uint8_t *apdu, BACNET_READ_PROPERTY_DATA *rpdata) { int apdu_len = -1; @@ -80,11 +78,10 @@ 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; @@ -98,13 +95,11 @@ void handler_read_property( /* encode the NPDU portion of the packet */ datalink_get_my_address(&my_address); npdu_encode_npdu_data(&npdu_data, false, MESSAGE_PRIORITY_NORMAL); - pdu_len = - npdu_encode_pdu(&Handler_Transmit_Buffer[0], src, &my_address, - &npdu_data); + pdu_len = npdu_encode_pdu( + &Handler_Transmit_Buffer[0], src, &my_address, &npdu_data); if (service_data->segmented_message) { /* we don't support segmentation - send an abort */ - len = - abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + len = abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true); goto RP_ABORT; @@ -112,48 +107,42 @@ void handler_read_property( len = rp_decode_service_request(service_request, service_len, &data); if (len < 0) { /* bad decoding - send an abort */ - len = - abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + len = abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, ABORT_REASON_OTHER, true); goto RP_ABORT; } /* most cases will be error */ - ack_len = - rp_ack_encode_apdu_init(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, &data); + ack_len = rp_ack_encode_apdu_init( + &Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, &data); /* FIXME: add buffer len as passed into function or use smart buffer */ data.error_class = ERROR_CLASS_OBJECT; data.error_code = ERROR_CODE_UNKNOWN_OBJECT; - property_len = - Encode_Property_APDU(&Handler_Transmit_Buffer[pdu_len + ack_len], - &data); + property_len = Encode_Property_APDU( + &Handler_Transmit_Buffer[pdu_len + ack_len], &data); if (property_len >= 0) { - len = - rp_ack_encode_apdu_object_property_end(&Handler_Transmit_Buffer - [pdu_len + property_len + ack_len]); + len = rp_ack_encode_apdu_object_property_end( + &Handler_Transmit_Buffer[pdu_len + property_len + ack_len]); len += ack_len + property_len; } else { switch (property_len) { - /* BACnet APDU too small to fit data, so proper response is Abort */ + /* BACnet APDU too small to fit data, so proper response is + * Abort */ case BACNET_STATUS_ABORT: - len = - abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + len = abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true); break; default: - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + len = bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, SERVICE_CONFIRMED_READ_PROPERTY, data.error_class, data.error_code); break; } } - RP_ABORT: +RP_ABORT: pdu_len += 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); (void)bytes_sent; return; diff --git a/ports/atmega168/h_whois.c b/ports/atmega168/h_whois.c index f84a2e3b..61887a23 100644 --- a/ports/atmega168/h_whois.c +++ b/ports/atmega168/h_whois.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2005 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2005 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include #include @@ -40,19 +40,16 @@ bool Send_I_Am_Flag = true; void handler_who_is( - uint8_t * service_request, - uint16_t service_len, - BACNET_ADDRESS * src) + uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS *src) { int len = 0; int32_t low_limit = 0; int32_t high_limit = 0; int32_t target_device; - (void) src; - len = - whois_decode_service_request(service_request, service_len, &low_limit, - &high_limit); + (void)src; + len = whois_decode_service_request( + service_request, service_len, &low_limit, &high_limit); if (len == 0) { Send_I_Am_Flag = true; } else if (len != BACNET_STATUS_ERROR) { diff --git a/ports/atmega168/h_wp.c b/ports/atmega168/h_wp.c index 4fd05c99..409a74c6 100644 --- a/ports/atmega168/h_wp.c +++ b/ports/atmega168/h_wp.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2005 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2005 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include #include @@ -45,11 +45,10 @@ /* 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; @@ -62,17 +61,14 @@ void handler_write_property( /* encode the NPDU portion of the packet */ datalink_get_my_address(&my_address); npdu_encode_npdu_data(&npdu_data, false, MESSAGE_PRIORITY_NORMAL); - pdu_len = - npdu_encode_pdu(&Handler_Transmit_Buffer[0], src, &my_address, - &npdu_data); + pdu_len = npdu_encode_pdu( + &Handler_Transmit_Buffer[0], src, &my_address, &npdu_data); /* bad decoding or something we didn't understand - send an abort */ if (len <= 0) { - len = - abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + len = abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, ABORT_REASON_OTHER, true); } else if (service_data->segmented_message) { - len = - abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + len = abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true); } else { @@ -81,58 +77,53 @@ void handler_write_property( switch (wp_data.object_type) { case OBJECT_DEVICE: if (Device_Write_Property(&wp_data)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], + 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, wp_data.error_class, - wp_data.error_code); + service_data->invoke_id, + SERVICE_CONFIRMED_WRITE_PROPERTY, + wp_data.error_class, wp_data.error_code); } break; case OBJECT_ANALOG_VALUE: if (Analog_Value_Write_Property(&wp_data)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], + 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, wp_data.error_class, - wp_data.error_code); + service_data->invoke_id, + SERVICE_CONFIRMED_WRITE_PROPERTY, + wp_data.error_class, wp_data.error_code); } break; case OBJECT_BINARY_VALUE: if (Binary_Value_Write_Property(&wp_data)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], + 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, wp_data.error_class, - wp_data.error_code); + service_data->invoke_id, + SERVICE_CONFIRMED_WRITE_PROPERTY, + wp_data.error_class, wp_data.error_code); } break; default: - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + len = bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, wp_data.error_class, wp_data.error_code); break; } } pdu_len += 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); (void)bytes_sent; return; diff --git a/ports/atmega168/main.c b/ports/atmega168/main.c index 69048cd0..935cfa26 100644 --- a/ports/atmega168/main.c +++ b/ports/atmega168/main.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2007 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2007 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include @@ -45,14 +45,12 @@ const char *BACnet_Version = "1.0"; http://www.avrfreaks.net/wiki/index.php/Documentation:AVR_GCC/IarToAvrgcc*/ /* dummy function - so we can use default demo handlers */ -bool dcc_communication_enabled( - void) +bool dcc_communication_enabled(void) { return true; } -static void init( - void) +static void init(void) { /* Initialize the Clock Prescaler for ATmega48/88/168 */ /* The default CLKPSx bits are factory set to 0011 */ @@ -105,8 +103,7 @@ static void init( __enable_interrupt(); } -static void task_milliseconds( - void) +static void task_milliseconds(void) { while (Timer_Milliseconds) { Timer_Milliseconds--; @@ -117,8 +114,7 @@ static void task_milliseconds( static uint8_t Address_Switch; -static void input_switch_read( - void) +static void input_switch_read(void) { uint8_t value; static uint8_t old_value = 0; @@ -140,8 +136,7 @@ static void input_switch_read( } static uint8_t PDUBuffer[MAX_MPDU]; -int main( - void) +int main(void) { uint16_t pdu_len = 0; BACNET_ADDRESS src; /* source address */ diff --git a/ports/atmega168/rs485.c b/ports/atmega168/rs485.c index b203e568..4716497a 100644 --- a/ports/atmega168/rs485.c +++ b/ports/atmega168/rs485.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2007 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2007 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* The module handles sending data out the RS-485 port */ /* and handles receiving data from the RS-485 port. */ @@ -45,14 +45,13 @@ static uint8_t LED3_Off_Timer; static uint32_t RS485_Baud = 9600; /**************************************************************************** -* DESCRIPTION: Initializes the RS485 hardware and variables, and starts in -* receive mode. -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -void RS485_Initialize( - void) + * DESCRIPTION: Initializes the RS485 hardware and variables, and starts in + * receive mode. + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +void RS485_Initialize(void) { /* enable Transmit and Receive */ UCSR0B = _BV(TXEN0) | _BV(RXEN0); @@ -81,25 +80,23 @@ void RS485_Initialize( } /**************************************************************************** -* DESCRIPTION: Returns the baud rate that we are currently running at -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -uint32_t RS485_Get_Baud_Rate( - void) + * DESCRIPTION: Returns the baud rate that we are currently running at + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +uint32_t RS485_Get_Baud_Rate(void) { return RS485_Baud; } /**************************************************************************** -* DESCRIPTION: Sets the baud rate for the chip USART -* RETURN: true if valid baud rate -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -bool RS485_Set_Baud_Rate( - uint32_t baud) + * DESCRIPTION: Sets the baud rate for the chip USART + * RETURN: true if valid baud rate + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +bool RS485_Set_Baud_Rate(uint32_t baud) { bool valid = true; @@ -126,13 +123,12 @@ bool RS485_Set_Baud_Rate( } /**************************************************************************** -* DESCRIPTION: Enable or disable the transmitter -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -void RS485_Transmitter_Enable( - bool enable) + * DESCRIPTION: Enable or disable the transmitter + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +void RS485_Transmitter_Enable(bool enable) { if (enable) { BIT_SET(PORTD, PD2); @@ -142,13 +138,12 @@ void RS485_Transmitter_Enable( } /**************************************************************************** -* DESCRIPTION: Waits on the SilenceTimer for 40 bits. -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -void RS485_Turnaround_Delay( - void) + * DESCRIPTION: Waits on the SilenceTimer for 40 bits. + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +void RS485_Turnaround_Delay(void) { uint8_t nbytes = 4; @@ -171,13 +166,12 @@ void RS485_Turnaround_Delay( } /**************************************************************************** -* DESCRIPTION: Timers for delaying the LED indicators going off -* RETURN: none -* ALGORITHM: none -* NOTES: expected to be called once a millisecond -*****************************************************************************/ -void RS485_LED_Timers( - void) + * DESCRIPTION: Timers for delaying the LED indicators going off + * RETURN: none + * ALGORITHM: none + * NOTES: expected to be called once a millisecond + *****************************************************************************/ +void RS485_LED_Timers(void) { if (LED1_Off_Timer) { LED1_Off_Timer--; @@ -194,41 +188,38 @@ void RS485_LED_Timers( } /**************************************************************************** -* DESCRIPTION: Turn on the LED, and set the off timer to turn it off -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -static void RS485_LED1_On( - void) + * DESCRIPTION: Turn on the LED, and set the off timer to turn it off + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +static void RS485_LED1_On(void) { BIT_CLEAR(PORTD, PD6); LED1_Off_Timer = 20; } /**************************************************************************** -* DESCRIPTION: Turn on the LED, and set the off timer to turn it off -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -static void RS485_LED3_On( - void) + * DESCRIPTION: Turn on the LED, and set the off timer to turn it off + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +static void RS485_LED3_On(void) { BIT_CLEAR(PORTD, PD7); LED3_Off_Timer = 20; } /**************************************************************************** -* DESCRIPTION: Send some data and wait until it is sent -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -void RS485_Send_Data( - uint8_t * buffer, /* data to send */ + * DESCRIPTION: Send some data and wait until it is sent + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +void RS485_Send_Data(uint8_t *buffer, /* data to send */ uint16_t nbytes) -{ /* number of bytes of data */ +{ /* number of bytes of data */ RS485_LED3_On(); while (nbytes) { while (!BIT_CHECK(UCSR0A, UDRE0)) { @@ -251,13 +242,12 @@ void RS485_Send_Data( } /**************************************************************************** -* DESCRIPTION: Return true if a framing or overrun error is present -* RETURN: true if error -* ALGORITHM: autobaud - if there are a lot of errors, switch baud rate -* NOTES: Clears any error flags. -*****************************************************************************/ -bool RS485_ReceiveError( - void) + * DESCRIPTION: Return true if a framing or overrun error is present + * RETURN: true if error + * ALGORITHM: autobaud - if there are a lot of errors, switch baud rate + * NOTES: Clears any error flags. + *****************************************************************************/ +bool RS485_ReceiveError(void) { bool ReceiveError = false; volatile uint8_t dummy_data; @@ -287,13 +277,12 @@ bool RS485_ReceiveError( } /**************************************************************************** -* DESCRIPTION: Return true if data is available -* RETURN: true if data is available, with the data in the parameter set -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -bool RS485_DataAvailable( - uint8_t * data) + * DESCRIPTION: Return true if data is available + * RETURN: true if data is available, with the data in the parameter set + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +bool RS485_DataAvailable(uint8_t *data) { bool DataAvailable = false; @@ -308,8 +297,7 @@ bool RS485_DataAvailable( } #ifdef TEST_RS485 -int main( - void) +int main(void) { unsigned i = 0; uint8_t DataRegister; diff --git a/ports/atmega168/stack.c b/ports/atmega168/stack.c index ba01019d..6787d683 100644 --- a/ports/atmega168/stack.c +++ b/ports/atmega168/stack.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2009 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2009 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include "hardware.h" @@ -33,12 +33,10 @@ extern uint8_t __stack; #if defined(__GNUC__) #define STACK_CANARY (0xC5) -void stack_init( - void) __attribute__ ((naked)) __attribute__ ((section(".init1"))); +void stack_init(void) __attribute__((naked)) __attribute__((section(".init1"))); #endif -void stack_init( - void) +void stack_init(void) { #if defined(__GNUC__) #if 0 @@ -49,17 +47,23 @@ void stack_init( p++; } #else - __asm volatile ( - " ldi r30,lo8(_end)\n" " ldi r31,hi8(_end)\n" " ldi r24,lo8(0xc5)\n" /* STACK_CANARY = 0xc5 */ - " ldi r25,hi8(__stack)\n" " rjmp .cmp\n" ".loop:\n" - " st Z+,r24\n" ".cmp:\n" " cpi r30,lo8(__stack)\n" - " cpc r31,r25\n" " brlo .loop\n" " breq .loop"::); + __asm volatile(" ldi r30,lo8(_end)\n" + " ldi r31,hi8(_end)\n" + " ldi r24,lo8(0xc5)\n" /* STACK_CANARY = 0xc5 */ + " ldi r25,hi8(__stack)\n" + " rjmp .cmp\n" + ".loop:\n" + " st Z+,r24\n" + ".cmp:\n" + " cpi r30,lo8(__stack)\n" + " cpc r31,r25\n" + " brlo .loop\n" + " breq .loop" ::); #endif #endif } -unsigned stack_size( - void) +unsigned stack_size(void) { #if defined(__GNUC__) return (&__stack) - (&_end); @@ -68,8 +72,7 @@ unsigned stack_size( #endif } -uint8_t stack_byte( - unsigned offset) +uint8_t stack_byte(unsigned offset) { #if defined(__GNUC__) return *(&_end + offset); @@ -79,8 +82,7 @@ uint8_t stack_byte( #endif } -unsigned stack_unused( - void) +unsigned stack_unused(void) { unsigned count = 0; #if defined(__GNUC__) diff --git a/ports/atmega168/timer.c b/ports/atmega168/timer.c index 5a7b0d7e..d301c1e2 100644 --- a/ports/atmega168/timer.c +++ b/ports/atmega168/timer.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2007 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2007 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include "hardware.h" @@ -30,19 +30,18 @@ /* Prescaling: 1, 8, 64, 256, 1024 */ #define TIMER_PRESCALER 64 /* Count: Timer0 counts up to 0xFF and then signals overflow */ -#define TIMER_TICKS (F_CPU/TIMER_PRESCALER/1000) +#define TIMER_TICKS (F_CPU / TIMER_PRESCALER / 1000) #if (TIMER_TICKS > 0xFF) #error Timer Prescaler value is too small #endif -#define TIMER_COUNT (0xFF-TIMER_TICKS) +#define TIMER_COUNT (0xFF - TIMER_TICKS) /* Global variable millisecond timer - used by main.c for timers task */ volatile uint8_t Timer_Milliseconds = 0; /* MS/TP Silence Timer */ static volatile uint16_t SilenceTime; /* Configure the Timer */ -void Timer_Initialize( - void) +void Timer_Initialize(void) { /* Normal Operation */ TCCR1A = 0; @@ -84,8 +83,7 @@ ISR(TIMER0_OVF_vect) } /* Public access to the Silence Timer */ -uint16_t Timer_Silence( - void) +uint16_t Timer_Silence(void) { uint16_t timer; @@ -97,8 +95,7 @@ uint16_t Timer_Silence( } /* Public reset of the Silence Timer */ -void Timer_Silence_Reset( - void) +void Timer_Silence_Reset(void) { BIT_CLEAR(TIMSK0, TOIE0); SilenceTime = 0; diff --git a/ports/bdk-atxx4-mstp/adc-block.c b/ports/bdk-atxx4-mstp/adc-block.c index a41899ad..dca0e142 100644 --- a/ports/bdk-atxx4-mstp/adc-block.c +++ b/ports/bdk-atxx4-mstp/adc-block.c @@ -1,26 +1,26 @@ /************************************************************************** -* -* Copyright (C) 2009 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*********************************************************************/ + * + * Copyright (C) 2009 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + *********************************************************************/ #include #include #include "hardware.h" @@ -28,23 +28,23 @@ #include "adc.h" /* prescale select bits */ -#if (F_CPU >> 1) < 1000000 -#define ADPS_8BIT (1) -#define ADPS_10BIT (3) +#if (F_CPU >> 1) < 1000000 +#define ADPS_8BIT (1) +#define ADPS_10BIT (3) #elif (F_CPU >> 2) < 1000000 -#define ADPS_8BIT (2) -#define ADPS_10BIT (4) +#define ADPS_8BIT (2) +#define ADPS_10BIT (4) #elif (F_CPU >> 3) < 1000000 -#define ADPS_8BIT (3) -#define ADPS_10BIT (5) +#define ADPS_8BIT (3) +#define ADPS_10BIT (5) #elif (F_CPU >> 4) < 1000000 -#define ADPS_8BIT (4) -#define ADPS_10BIT (6) +#define ADPS_8BIT (4) +#define ADPS_10BIT (6) #elif (F_CPU >> 5) < 1000000 -#define ADPS_8BIT (5) -#define ADPS_10BIT (7) +#define ADPS_8BIT (5) +#define ADPS_10BIT (7) #else -#error "ADC: F_CPU too large for accuracy." +#error "ADC: F_CPU too large for accuracy." #endif /* ADMUX: channel bits 0..4 @@ -59,54 +59,55 @@ ADATE = Auto Trigger Enable */ -void adc_enable( - uint8_t index) +void adc_enable(uint8_t index) { index = index; /* do nothing */ } /************************************************** -* Description: Run a Analog to Digital conversion -* Returns: none -* Notes: none -**************************************************/ -uint8_t adc_result_8bit( - uint8_t channel) -{ /* 0..7 = ADC0..ADC7, respectively */ - uint8_t value = 0; /* return value */ + * Description: Run a Analog to Digital conversion + * Returns: none + * Notes: none + **************************************************/ +uint8_t adc_result_8bit(uint8_t channel) +{ /* 0..7 = ADC0..ADC7, respectively */ + uint8_t value = 0; /* return value */ - while (ADCSRA & (1 << ADSC)); + while (ADCSRA & (1 << ADSC)) + ; ADMUX = channel | (1 << ADLAR) | (0 << REFS1) | (1 << REFS0); /* Delay needed for the stabilization of the ADC input voltage */ _delay_us(10); /* Start the analog to digital conversion */ ADCSRA = (1 << ADEN) | (1 << ADSC) | (1 << ADIF) | ADPS_8BIT; /* Wait for the analog to digital conversion to complete */ - while ((ADCSRA & (1 << ADIF)) == 0); + while ((ADCSRA & (1 << ADIF)) == 0) + ; value = ADCH; return value; } /************************************************** -* Description: Run a Analog to Digital conversion -* Returns: none -* Notes: none -**************************************************/ -uint16_t adc_result_10bit( - uint8_t channel) -{ /* 0..7 = ADC0..ADC7, respectively */ + * Description: Run a Analog to Digital conversion + * Returns: none + * Notes: none + **************************************************/ +uint16_t adc_result_10bit(uint8_t channel) +{ /* 0..7 = ADC0..ADC7, respectively */ uint16_t value = 0; /* return value */ - while (ADCSRA & (1 << ADSC)); + while (ADCSRA & (1 << ADSC)) + ; ADMUX = channel | (0 << ADLAR) | (0 << REFS1) | (1 << REFS0); /* Delay needed for the stabilization of the ADC input voltage */ _delay_us(10); /* Start the analog to digital conversion */ ADCSRA = (1 << ADEN) | (1 << ADSC) | (1 << ADIF) | ADPS_10BIT; /* Wait for the analog to digital conversion to complete */ - while ((ADCSRA & (1 << ADIF)) == 0); + while ((ADCSRA & (1 << ADIF)) == 0) + ; value = ADCL; value |= (ADCH << 8); @@ -114,12 +115,11 @@ uint16_t adc_result_10bit( } /************************************************** -* Description: Initializes the Analog to Digital Converter -* Returns: none -* Notes: none -**************************************************/ -void adc_init( - void) + * Description: Initializes the Analog to Digital Converter + * Returns: none + * Notes: none + **************************************************/ +void adc_init(void) { /* configure ADC for Free Running Mode - ADTS = 000 */ /* AIN1 is applied to the negative input of the Analog Comparator - ACME */ diff --git a/ports/bdk-atxx4-mstp/adc.c b/ports/bdk-atxx4-mstp/adc.c index bdec4ad3..fc588579 100644 --- a/ports/bdk-atxx4-mstp/adc.c +++ b/ports/bdk-atxx4-mstp/adc.c @@ -1,26 +1,26 @@ /************************************************************************** -* -* Copyright (C) 2009 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*********************************************************************/ + * + * Copyright (C) 2009 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + *********************************************************************/ #include #include #include "hardware.h" @@ -28,23 +28,23 @@ #include "adc.h" /* prescale select bits */ -#if (F_CPU >> 1) < 1000000 -#define ADPS_8BIT (1) -#define ADPS_10BIT (3) +#if (F_CPU >> 1) < 1000000 +#define ADPS_8BIT (1) +#define ADPS_10BIT (3) #elif (F_CPU >> 2) < 1000000 -#define ADPS_8BIT (2) -#define ADPS_10BIT (4) +#define ADPS_8BIT (2) +#define ADPS_10BIT (4) #elif (F_CPU >> 3) < 1000000 -#define ADPS_8BIT (3) -#define ADPS_10BIT (5) +#define ADPS_8BIT (3) +#define ADPS_10BIT (5) #elif (F_CPU >> 4) < 1000000 -#define ADPS_8BIT (4) -#define ADPS_10BIT (6) +#define ADPS_8BIT (4) +#define ADPS_10BIT (6) #elif (F_CPU >> 5) < 1000000 -#define ADPS_8BIT (5) -#define ADPS_10BIT (7) +#define ADPS_8BIT (5) +#define ADPS_10BIT (7) #else -#error "ADC: F_CPU too large for accuracy." +#error "ADC: F_CPU too large for accuracy." #endif /* Array of ADC results */ @@ -86,9 +86,8 @@ ISR(ADC_vect) BIT_SET(ADCSRA, ADSC); } -void adc_enable( - uint8_t index) -{ /* 0..7 = ADC0..ADC7, respectively */ +void adc_enable(uint8_t index) +{ /* 0..7 = ADC0..ADC7, respectively */ if (Enabled_Channels) { /* ADC interupt is already started */ BIT_SET(Enabled_Channels, index); @@ -106,9 +105,8 @@ void adc_enable( } } -uint8_t adc_result_8bit( - uint8_t index) -{ /* 0..7 = ADC0..ADC7, respectively */ +uint8_t adc_result_8bit(uint8_t index) +{ /* 0..7 = ADC0..ADC7, respectively */ uint8_t result = 0; uint8_t sreg; @@ -116,16 +114,15 @@ uint8_t adc_result_8bit( adc_enable(index); sreg = SREG; __disable_interrupt(); - result = (uint8_t) (Sample_Result[index] >> 2); + result = (uint8_t)(Sample_Result[index] >> 2); SREG = sreg; } return result; } -uint16_t adc_result_10bit( - uint8_t index) -{ /* 0..7 = ADC0..ADC7, respectively */ +uint16_t adc_result_10bit(uint8_t index) +{ /* 0..7 = ADC0..ADC7, respectively */ uint16_t result = 0; uint8_t sreg; @@ -140,8 +137,7 @@ uint16_t adc_result_10bit( return result; } -void adc_init( - void) +void adc_init(void) { /* Initial channel selection */ /* ADLAR = Left Adjust Result diff --git a/ports/bdk-atxx4-mstp/ai.c b/ports/bdk-atxx4-mstp/ai.c index 77345118..bf18224c 100644 --- a/ports/bdk-atxx4-mstp/ai.c +++ b/ports/bdk-atxx4-mstp/ai.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2005 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2005 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* Analog Input Objects customize for your use */ @@ -42,30 +42,16 @@ static float Present_Value[MAX_ANALOG_INPUTS]; /* These three arrays are used by the ReadPropertyMultiple handler */ -static const int Analog_Input_Properties_Required[] = { - PROP_OBJECT_IDENTIFIER, - PROP_OBJECT_NAME, - PROP_OBJECT_TYPE, - PROP_PRESENT_VALUE, - PROP_STATUS_FLAGS, - PROP_EVENT_STATE, - PROP_OUT_OF_SERVICE, - PROP_UNITS, - -1 -}; +static const int Analog_Input_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, + PROP_OBJECT_NAME, PROP_OBJECT_TYPE, PROP_PRESENT_VALUE, PROP_STATUS_FLAGS, + PROP_EVENT_STATE, PROP_OUT_OF_SERVICE, PROP_UNITS, -1 }; -static const int Analog_Input_Properties_Optional[] = { - -1 -}; +static const int Analog_Input_Properties_Optional[] = { -1 }; -static const int Analog_Input_Properties_Proprietary[] = { - -1 -}; +static const int Analog_Input_Properties_Proprietary[] = { -1 }; void Analog_Input_Property_Lists( - const int **pRequired, - const int **pOptional, - const int **pProprietary) + const int **pRequired, const int **pOptional, const int **pProprietary) { if (pRequired) *pRequired = Analog_Input_Properties_Required; @@ -77,8 +63,7 @@ void Analog_Input_Property_Lists( return; } -void Analog_Input_Init( - void) +void Analog_Input_Init(void) { return; } @@ -86,8 +71,7 @@ void Analog_Input_Init( /* 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; @@ -96,24 +80,21 @@ bool Analog_Input_Valid_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; } bool Analog_Input_Object_Name( - uint32_t object_instance, - BACNET_CHARACTER_STRING * object_name) + uint32_t object_instance, BACNET_CHARACTER_STRING *object_name) { - static char text_string[32]; /* okay for single thread */ + static char text_string[32]; /* okay for single thread */ bool status = false; if (object_instance < MAX_ANALOG_INPUTS) { @@ -124,8 +105,7 @@ bool Analog_Input_Object_Name( return status; } -float Analog_Input_Present_Value( - uint32_t object_instance) +float Analog_Input_Present_Value(uint32_t object_instance) { float value = 0.0; @@ -136,9 +116,7 @@ float Analog_Input_Present_Value( return value; } -void Analog_Input_Present_Value_Set( - uint32_t object_instance, - float value) +void Analog_Input_Present_Value_Set(uint32_t object_instance, float value) { if (object_instance < MAX_ANALOG_INPUTS) { Present_Value[object_instance] = value; @@ -147,10 +125,9 @@ void Analog_Input_Present_Value_Set( /* return apdu length, or -1 on error */ /* assumption - object already exists */ -int Analog_Input_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Analog_Input_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ BACNET_CHARACTER_STRING char_string = { 0 }; BACNET_BIT_STRING bit_string = { 0 }; uint8_t *apdu = NULL; @@ -162,9 +139,8 @@ int Analog_Input_Read_Property( apdu = rpdata->application_data; switch (rpdata->object_property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], rpdata->object_type, - rpdata->object_instance); + apdu_len = encode_application_object_id( + &apdu[0], rpdata->object_type, rpdata->object_instance); break; case PROP_OBJECT_NAME: Analog_Input_Object_Name(rpdata->object_instance, &char_string); @@ -176,9 +152,8 @@ int Analog_Input_Read_Property( encode_application_enumerated(&apdu[0], rpdata->object_type); break; case PROP_PRESENT_VALUE: - apdu_len = - encode_application_real(&apdu[0], - Analog_Input_Present_Value(rpdata->object_instance)); + apdu_len = encode_application_real( + &apdu[0], Analog_Input_Present_Value(rpdata->object_instance)); break; case PROP_STATUS_FLAGS: bitstring_init(&bit_string); diff --git a/ports/bdk-atxx4-mstp/av.c b/ports/bdk-atxx4-mstp/av.c index b3005f12..c2893b86 100644 --- a/ports/bdk-atxx4-mstp/av.c +++ b/ports/bdk-atxx4-mstp/av.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2006 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2006 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* Analog Value Objects - customize for your use */ @@ -33,7 +33,7 @@ #include "bacnet/bacdcode.h" #include "bacnet/bacenum.h" #include "bacnet/bacapp.h" -#include "bacnet/config.h" /* the custom stuff */ +#include "bacnet/config.h" /* the custom stuff */ #include "bacnet/wp.h" #include "bacnet/basic/object/av.h" #include "bacnet/basic/services.h" @@ -46,17 +46,9 @@ static float Present_Value[MAX_ANALOG_VALUES]; /* These three arrays are used by the ReadPropertyMultiple handler */ -static const int Analog_Value_Properties_Required[] = { - PROP_OBJECT_IDENTIFIER, - PROP_OBJECT_NAME, - PROP_OBJECT_TYPE, - PROP_PRESENT_VALUE, - PROP_STATUS_FLAGS, - PROP_EVENT_STATE, - PROP_OUT_OF_SERVICE, - PROP_UNITS, - -1 -}; +static const int Analog_Value_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, + PROP_OBJECT_NAME, PROP_OBJECT_TYPE, PROP_PRESENT_VALUE, PROP_STATUS_FLAGS, + PROP_EVENT_STATE, PROP_OUT_OF_SERVICE, PROP_UNITS, -1 }; static const int Analog_Value_Properties_Optional[] = { #if 0 @@ -66,14 +58,10 @@ static const int Analog_Value_Properties_Optional[] = { -1 }; -static const int Analog_Value_Properties_Proprietary[] = { - -1 -}; +static const int Analog_Value_Properties_Proprietary[] = { -1 }; void Analog_Value_Property_Lists( - const int **pRequired, - const int **pOptional, - const int **pProprietary) + const int **pRequired, const int **pOptional, const int **pProprietary) { if (pRequired) *pRequired = Analog_Value_Properties_Required; @@ -85,8 +73,7 @@ void Analog_Value_Property_Lists( return; } -void Analog_Value_Init( - void) +void Analog_Value_Init(void) { return; } @@ -94,8 +81,7 @@ void Analog_Value_Init( /* 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; @@ -105,8 +91,7 @@ bool Analog_Value_Valid_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; } @@ -114,8 +99,7 @@ unsigned Analog_Value_Count( /* 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; } @@ -123,8 +107,7 @@ uint32_t Analog_Value_Index_To_Instance( /* 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; @@ -134,8 +117,7 @@ unsigned Analog_Value_Instance_To_Index( return index; } -float Analog_Value_Present_Value( - uint32_t object_instance) +float Analog_Value_Present_Value(uint32_t object_instance) { float value = 0; unsigned index = 0; @@ -149,9 +131,7 @@ float Analog_Value_Present_Value( } bool Analog_Value_Present_Value_Set( - uint32_t object_instance, - float value, - uint8_t priority) + uint32_t object_instance, float value, uint8_t priority) { unsigned index = 0; bool status = false; @@ -160,8 +140,8 @@ bool Analog_Value_Present_Value_Set( index = Analog_Value_Instance_To_Index(object_instance); if (index < MAX_ANALOG_VALUES) { if (priority && (priority <= BACNET_MAX_PRIORITY) && - (priority != 6 /* reserved */ ) && - (value >= 0.0) && (value <= 100.0)) { + (priority != 6 /* reserved */) && (value >= 0.0) && + (value <= 100.0)) { Present_Value[index] = value; /* Note: you could set the physical output here if we are the highest priority. @@ -176,10 +156,9 @@ bool Analog_Value_Present_Value_Set( /* note: the object name must be unique within this device */ bool Analog_Value_Object_Name( - uint32_t object_instance, - BACNET_CHARACTER_STRING * object_name) + uint32_t object_instance, BACNET_CHARACTER_STRING *object_name) { - static char text_string[32] = ""; /* okay for single thread */ + static char text_string[32] = ""; /* okay for single thread */ unsigned index = 0; bool status = false; @@ -193,10 +172,9 @@ bool Analog_Value_Object_Name( } /* return apdu len, or -1 on error */ -int Analog_Value_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Analog_Value_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ BACNET_BIT_STRING bit_string; float real_value = 1.414F; BACNET_CHARACTER_STRING char_string = { 0 }; @@ -214,9 +192,8 @@ int Analog_Value_Read_Property( apdu = rpdata->application_data; switch (rpdata->object_property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], rpdata->object_type, - rpdata->object_instance); + apdu_len = encode_application_object_id( + &apdu[0], rpdata->object_type, rpdata->object_instance); break; case PROP_OBJECT_NAME: Analog_Value_Object_Name(rpdata->object_instance, &char_string); @@ -332,10 +309,9 @@ int Analog_Value_Read_Property( } /* returns true if successful */ -bool Analog_Value_Write_Property( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Analog_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data) { - bool status = false; /* return value */ + bool status = false; /* return value */ #if 0 unsigned int object_index = 0; unsigned int priority = 0; @@ -344,9 +320,8 @@ bool Analog_Value_Write_Property( int len = 0; /* decode the some of the request */ - len = - bacapp_decode_application_data(wp_data->application_data, - wp_data->application_data_len, &value); + len = bacapp_decode_application_data( + wp_data->application_data, wp_data->application_data_len, &value); /* FIXME: len < application_data_len: more data? */ if (len < 0) { /* error while decoding - a value larger than we can handle */ @@ -363,18 +338,17 @@ bool Analog_Value_Write_Property( } switch (wp_data->object_property) { case PROP_PRESENT_VALUE: - status = - WPValidateArgType(&value, BACNET_APPLICATION_TAG_REAL, + status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_REAL, &wp_data->error_class, &wp_data->error_code); if (status) { status = Analog_Value_Present_Value_Set(wp_data->object_instance, - value.type.Real, wp_data->priority); + value.type.Real, wp_data->priority); if (!status) { 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. */ + /* Command priority 6 is reserved for use by Minimum + On/Off algorithm and may not be used for other + purposes in any object. */ wp_data->error_class = ERROR_CLASS_PROPERTY; wp_data->error_code = ERROR_CODE_WRITE_ACCESS_DENIED; } else { diff --git a/ports/bdk-atxx4-mstp/bacnet.c b/ports/bdk-atxx4-mstp/bacnet.c index 01ea1eaf..4ad249d4 100644 --- a/ports/bdk-atxx4-mstp/bacnet.c +++ b/ports/bdk-atxx4-mstp/bacnet.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2010 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2010 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include /* hardware layer includes */ @@ -55,18 +55,16 @@ static uint8_t MSTP_MAC_Address; static struct mstimer DCC_Timer; #define DCC_CYCLE_SECONDS 1 -static bool seeprom_version_test( - void) +static bool seeprom_version_test(void) { uint16_t version = 0; uint16_t id = 0; bool status = false; int rv; - rv = seeprom_bytes_read(NV_SEEPROM_TYPE_0, (uint8_t *) & id, 2); + rv = seeprom_bytes_read(NV_SEEPROM_TYPE_0, (uint8_t *)&id, 2); if (rv > 0) { - rv = seeprom_bytes_read(NV_SEEPROM_VERSION_0, (uint8_t *) & version, - 2); + rv = seeprom_bytes_read(NV_SEEPROM_VERSION_0, (uint8_t *)&version, 2); } if ((rv > 0) && (id == SEEPROM_ID) && (version == SEEPROM_VERSION)) { @@ -74,8 +72,8 @@ static bool seeprom_version_test( } else if (rv > 0) { version = SEEPROM_VERSION; id = SEEPROM_ID; - seeprom_bytes_write(NV_SEEPROM_TYPE_0, (uint8_t *) & id, 2); - seeprom_bytes_write(NV_SEEPROM_VERSION_0, (uint8_t *) & version, 2); + seeprom_bytes_write(NV_SEEPROM_TYPE_0, (uint8_t *)&id, 2); + seeprom_bytes_write(NV_SEEPROM_VERSION_0, (uint8_t *)&version, 2); } else { while (1) { /* SEEPROM is faulty! */ @@ -90,8 +88,8 @@ static void device_id_init(uint8_t mac) uint32_t device_id = 0; /* Get the device ID from the eeprom */ - eeprom_bytes_read(NV_EEPROM_DEVICE_0, (uint8_t *) & device_id, - sizeof(device_id)); + eeprom_bytes_read( + NV_EEPROM_DEVICE_0, (uint8_t *)&device_id, sizeof(device_id)); if (device_id < BACNET_MAX_INSTANCE) { Device_Set_Object_Instance_Number(device_id); } else { @@ -100,8 +98,7 @@ static void device_id_init(uint8_t mac) } } -void bacnet_init( - void) +void bacnet_init(void) { uint8_t max_master = 0; @@ -121,33 +118,31 @@ void bacnet_init( Device_Init(NULL); device_id_init(MSTP_MAC_Address); /* set up our confirmed service unrecognized service handler - required! */ - apdu_set_unrecognized_service_handler_handler - (handler_unrecognized_service); + apdu_set_unrecognized_service_handler_handler(handler_unrecognized_service); /* 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); /* 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, - handler_read_property); - apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROP_MULTIPLE, - handler_read_property_multiple); - apdu_set_confirmed_handler(SERVICE_CONFIRMED_REINITIALIZE_DEVICE, - handler_reinitialize_device); - apdu_set_confirmed_handler(SERVICE_CONFIRMED_WRITE_PROPERTY, - handler_write_property); + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_READ_PROPERTY, handler_read_property); + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_READ_PROP_MULTIPLE, handler_read_property_multiple); + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_REINITIALIZE_DEVICE, handler_reinitialize_device); + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_WRITE_PROPERTY, handler_write_property); /* handle communication so we can shutup when asked */ apdu_set_confirmed_handler(SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL, handler_device_communication_control); /* start the cyclic 1 second timer for DCC */ - mstimer_set(&DCC_Timer, DCC_CYCLE_SECONDS*1000); + mstimer_set(&DCC_Timer, DCC_CYCLE_SECONDS * 1000); /* Hello World! */ Send_I_Am(&Handler_Transmit_Buffer[0]); } static uint8_t PDUBuffer[MAX_MPDU]; -void bacnet_task( - void) +void bacnet_task(void) { uint8_t mstp_mac_address; uint16_t pdu_len; diff --git a/ports/bdk-atxx4-mstp/bi.c b/ports/bdk-atxx4-mstp/bi.c index 551d7985..996ae0da 100644 --- a/ports/bdk-atxx4-mstp/bi.c +++ b/ports/bdk-atxx4-mstp/bi.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2006 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2006 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* Binary Input Objects customize for your use */ @@ -42,30 +42,16 @@ 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[] = { - PROP_OBJECT_IDENTIFIER, - PROP_OBJECT_NAME, - PROP_OBJECT_TYPE, - PROP_PRESENT_VALUE, - PROP_STATUS_FLAGS, - PROP_EVENT_STATE, - PROP_OUT_OF_SERVICE, - PROP_POLARITY, - -1 -}; +static const int Binary_Input_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, + PROP_OBJECT_NAME, PROP_OBJECT_TYPE, PROP_PRESENT_VALUE, PROP_STATUS_FLAGS, + PROP_EVENT_STATE, PROP_OUT_OF_SERVICE, PROP_POLARITY, -1 }; -static const int Binary_Input_Properties_Optional[] = { - -1 -}; +static const int Binary_Input_Properties_Optional[] = { -1 }; -static const int Binary_Input_Properties_Proprietary[] = { - -1 -}; +static const int Binary_Input_Properties_Proprietary[] = { -1 }; void Binary_Input_Property_Lists( - const int **pRequired, - const int **pOptional, - const int **pProprietary) + const int **pRequired, const int **pOptional, const int **pProprietary) { if (pRequired) { *pRequired = Binary_Input_Properties_Required; @@ -80,8 +66,7 @@ void Binary_Input_Property_Lists( return; } -void Binary_Input_Init( - void) +void Binary_Input_Init(void) { unsigned i; @@ -91,8 +76,7 @@ void Binary_Input_Init( } /* 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; @@ -101,15 +85,13 @@ bool Binary_Input_Valid_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; } @@ -117,8 +99,7 @@ uint32_t Binary_Input_Index_To_Instance( /* 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; @@ -128,8 +109,7 @@ unsigned Binary_Input_Instance_To_Index( return index; } -BACNET_BINARY_PV Binary_Input_Present_Value( - uint32_t object_instance) +BACNET_BINARY_PV Binary_Input_Present_Value(uint32_t object_instance) { BACNET_BINARY_PV value = BINARY_INACTIVE; unsigned index = 0; @@ -143,8 +123,7 @@ BACNET_BINARY_PV Binary_Input_Present_Value( } bool Binary_Input_Present_Value_Set( - uint32_t object_instance, - BACNET_BINARY_PV value) + uint32_t object_instance, BACNET_BINARY_PV value) { unsigned index = 0; @@ -158,10 +137,9 @@ bool Binary_Input_Present_Value_Set( } bool Binary_Input_Object_Name( - uint32_t object_instance, - BACNET_CHARACTER_STRING * object_name) + uint32_t object_instance, BACNET_CHARACTER_STRING *object_name) { - static char text_string[32]; /* okay for single thread */ + static char text_string[32]; /* okay for single thread */ bool status = false; if (object_instance < MAX_BINARY_INPUTS) { @@ -174,10 +152,9 @@ bool Binary_Input_Object_Name( /* return apdu length, or -1 on error */ /* assumption - object already exists, and has been bounds checked */ -int Binary_Input_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Binary_Input_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ BACNET_BIT_STRING bit_string = { 0 }; BACNET_POLARITY polarity = POLARITY_NORMAL; BACNET_BINARY_PV value = BINARY_INACTIVE; @@ -191,9 +168,8 @@ int Binary_Input_Read_Property( apdu = rpdata->application_data; switch (rpdata->object_property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], rpdata->object_type, - rpdata->object_instance); + apdu_len = encode_application_object_id( + &apdu[0], rpdata->object_type, rpdata->object_instance); break; case PROP_OBJECT_NAME: Binary_Input_Object_Name(rpdata->object_instance, &char_string); diff --git a/ports/bdk-atxx4-mstp/bname.c b/ports/bdk-atxx4-mstp/bname.c index 18a346a3..43e6eeae 100644 --- a/ports/bdk-atxx4-mstp/bname.c +++ b/ports/bdk-atxx4-mstp/bname.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2011 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2011 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include #include @@ -35,10 +35,7 @@ #include "bacnet/basic/object/device.h" #include "bname.h" -static bool bacnet_name_isvalid( - uint8_t encoding, - uint8_t length, - char *str) +static bool bacnet_name_isvalid(uint8_t encoding, uint8_t length, char *str) { bool valid = false; @@ -55,32 +52,27 @@ static bool bacnet_name_isvalid( } bool bacnet_name_save( - uint16_t offset, - uint8_t encoding, - char *str, - uint8_t length) + uint16_t offset, uint8_t encoding, char *str, uint8_t length) { uint8_t buffer[NV_EEPROM_NAME_SIZE] = { 0 }; uint8_t i = 0; if (bacnet_name_isvalid(encoding, length, str)) { eeprom_bytes_write(NV_EEPROM_NAME_LENGTH(offset), &length, 1); - eeprom_bytes_write(NV_EEPROM_NAME_ENCODING(offset), - (uint8_t *) & encoding, 1); + eeprom_bytes_write( + NV_EEPROM_NAME_ENCODING(offset), (uint8_t *)&encoding, 1); for (i = 0; i < length; i++) { buffer[i] = str[i]; } - eeprom_bytes_write(NV_EEPROM_NAME_STRING(offset), &buffer[0], - NV_EEPROM_NAME_SIZE); + eeprom_bytes_write( + NV_EEPROM_NAME_STRING(offset), &buffer[0], NV_EEPROM_NAME_SIZE); return true; } return false; } -bool bacnet_name_set( - uint16_t offset, - BACNET_CHARACTER_STRING * char_string) +bool bacnet_name_set(uint16_t offset, BACNET_CHARACTER_STRING *char_string) { uint8_t encoding = 0; uint8_t length = 0; @@ -92,13 +84,12 @@ bool bacnet_name_set( return bacnet_name_save(offset, encoding, str, length); } -bool bacnet_name_write_unique( - uint16_t offset, +bool bacnet_name_write_unique(uint16_t offset, BACNET_OBJECT_TYPE object_type, uint32_t object_instance, - BACNET_CHARACTER_STRING * char_string, - BACNET_ERROR_CLASS * error_class, - BACNET_ERROR_CODE * error_code) + BACNET_CHARACTER_STRING *char_string, + BACNET_ERROR_CLASS *error_class, + BACNET_ERROR_CODE *error_code) { bool status = false; size_t length = 0; @@ -113,8 +104,8 @@ bool bacnet_name_write_unique( } else if (length <= NV_EEPROM_NAME_SIZE) { encoding = characterstring_encoding(char_string); if (encoding < MAX_CHARACTER_STRING_ENCODING) { - if (Device_Valid_Object_Name(char_string, &duplicate_type, - &duplicate_instance)) { + if (Device_Valid_Object_Name( + char_string, &duplicate_type, &duplicate_instance)) { if ((duplicate_type == object_type) && (duplicate_instance == object_instance)) { /* writing same name to same object */ @@ -145,11 +136,10 @@ bool bacnet_name_write_unique( } /* no required minumum length or duplicate checking */ -bool bacnet_name_write( - uint16_t offset, - BACNET_CHARACTER_STRING * char_string, - BACNET_ERROR_CLASS * error_class, - BACNET_ERROR_CODE * error_code) +bool bacnet_name_write(uint16_t offset, + BACNET_CHARACTER_STRING *char_string, + BACNET_ERROR_CLASS *error_class, + BACNET_ERROR_CODE *error_code) { bool status = false; size_t length = 0; @@ -176,18 +166,14 @@ bool bacnet_name_write( return status; } -void bacnet_name_init( - uint16_t offset, - char *default_string) +void bacnet_name_init(uint16_t offset, char *default_string) { - (void) bacnet_name_save(offset, CHARACTER_UTF8, default_string, - strlen(default_string)); + (void)bacnet_name_save( + offset, CHARACTER_UTF8, default_string, strlen(default_string)); } void bacnet_name( - uint16_t offset, - BACNET_CHARACTER_STRING * char_string, - char *default_string) + uint16_t offset, BACNET_CHARACTER_STRING *char_string, char *default_string) { uint8_t encoding = 0; uint8_t length = 0; @@ -195,8 +181,8 @@ void bacnet_name( eeprom_bytes_read(NV_EEPROM_NAME_ENCODING(offset), &encoding, 1); eeprom_bytes_read(NV_EEPROM_NAME_LENGTH(offset), &length, 1); - eeprom_bytes_read(NV_EEPROM_NAME_STRING(offset), (uint8_t *) & name, - NV_EEPROM_NAME_SIZE); + eeprom_bytes_read( + NV_EEPROM_NAME_STRING(offset), (uint8_t *)&name, NV_EEPROM_NAME_SIZE); if (bacnet_name_isvalid(encoding, length, name)) { characterstring_init(char_string, encoding, &name[0], length); } else if (default_string) { diff --git a/ports/bdk-atxx4-mstp/bo.c b/ports/bdk-atxx4-mstp/bo.c index dd8bf612..e3ec1035 100644 --- a/ports/bdk-atxx4-mstp/bo.c +++ b/ports/bdk-atxx4-mstp/bo.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2009 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2009 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* Binary Output Objects - customize for your use */ @@ -31,7 +31,7 @@ #include "bacnet/bacdef.h" #include "bacnet/bacdcode.h" #include "bacnet/bacenum.h" -#include "bacnet/config.h" /* the custom stuff */ +#include "bacnet/config.h" /* the custom stuff */ #include "bacnet/wp.h" #include "hardware.h" #include "led.h" @@ -55,34 +55,18 @@ static uint8_t Out_Of_Service[MAX_BINARY_OUTPUTS]; static uint8_t Polarity[MAX_BINARY_OUTPUTS]; /* These three arrays are used by the ReadPropertyMultiple handler */ -static const int Binary_Output_Properties_Required[] = { - PROP_OBJECT_IDENTIFIER, - PROP_OBJECT_NAME, - PROP_OBJECT_TYPE, - PROP_PRESENT_VALUE, - PROP_STATUS_FLAGS, - PROP_EVENT_STATE, - PROP_OUT_OF_SERVICE, - PROP_POLARITY, - PROP_PRIORITY_ARRAY, - PROP_RELINQUISH_DEFAULT, - -1 -}; +static const int Binary_Output_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, + PROP_OBJECT_NAME, PROP_OBJECT_TYPE, PROP_PRESENT_VALUE, PROP_STATUS_FLAGS, + PROP_EVENT_STATE, PROP_OUT_OF_SERVICE, PROP_POLARITY, PROP_PRIORITY_ARRAY, + PROP_RELINQUISH_DEFAULT, -1 }; -static const int Binary_Output_Properties_Optional[] = { - PROP_ACTIVE_TEXT, - PROP_INACTIVE_TEXT, - -1 -}; +static const int Binary_Output_Properties_Optional[] = { PROP_ACTIVE_TEXT, + PROP_INACTIVE_TEXT, -1 }; -static const int Binary_Output_Properties_Proprietary[] = { - -1 -}; +static const int Binary_Output_Properties_Proprietary[] = { -1 }; void Binary_Output_Property_Lists( - const int **pRequired, - const int **pOptional, - const int **pProprietary) + const int **pRequired, const int **pOptional, const int **pProprietary) { if (pRequired) *pRequired = Binary_Output_Properties_Required; @@ -95,8 +79,7 @@ void Binary_Output_Property_Lists( } /* we simply have 0-n object instances. */ -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; @@ -105,22 +88,19 @@ bool Binary_Output_Valid_Instance( } /* we simply have 0-n object instances. */ -unsigned Binary_Output_Count( - void) +unsigned Binary_Output_Count(void) { return MAX_BINARY_OUTPUTS; } /* we simply have 0-n object instances. */ -uint32_t Binary_Output_Index_To_Instance( - unsigned index) +uint32_t Binary_Output_Index_To_Instance(unsigned index) { return index; } /* we simply have 0-n object instances. */ -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; @@ -130,8 +110,7 @@ unsigned Binary_Output_Instance_To_Index( return index; } -static BACNET_BINARY_PV Present_Value( - unsigned int index) +static BACNET_BINARY_PV Present_Value(unsigned int index) { BACNET_BINARY_PV value = RELINQUISH_DEFAULT; BACNET_BINARY_PV current_value = RELINQUISH_DEFAULT; @@ -139,9 +118,9 @@ static BACNET_BINARY_PV Present_Value( if (index < MAX_BINARY_OUTPUTS) { for (i = 0; i < BACNET_MAX_PRIORITY; i++) { - current_value = (BACNET_BINARY_PV) Binary_Output_Level[index][i]; + current_value = (BACNET_BINARY_PV)Binary_Output_Level[index][i]; if (current_value != BINARY_NULL) { - value = (BACNET_BINARY_PV) Binary_Output_Level[index][i]; + value = (BACNET_BINARY_PV)Binary_Output_Level[index][i]; break; } } @@ -150,8 +129,7 @@ static BACNET_BINARY_PV Present_Value( return value; } -BACNET_BINARY_PV Binary_Output_Present_Value( - uint32_t object_instance) +BACNET_BINARY_PV Binary_Output_Present_Value(uint32_t object_instance) { unsigned index = 0; @@ -161,17 +139,15 @@ BACNET_BINARY_PV Binary_Output_Present_Value( } bool Binary_Output_Present_Value_Set( - uint32_t instance, - BACNET_BINARY_PV binary_value, - unsigned priority) -{ /* 0..15 */ + uint32_t instance, BACNET_BINARY_PV binary_value, unsigned priority) +{ /* 0..15 */ bool status = false; if (instance < MAX_BINARY_OUTPUTS) { if (priority < BACNET_MAX_PRIORITY) { - Binary_Output_Level[instance][priority] = (uint8_t) binary_value; + Binary_Output_Level[instance][priority] = (uint8_t)binary_value; seeprom_bytes_write(NV_SEEPROM_BINARY_OUTPUT(instance, - NV_SEEPROM_BO_PRIORITY_ARRAY_1 + priority), + NV_SEEPROM_BO_PRIORITY_ARRAY_1 + priority), &Binary_Output_Level[instance][priority], 1); status = true; } @@ -180,17 +156,16 @@ bool Binary_Output_Present_Value_Set( return status; } -bool Binary_Output_Polarity_Set( - uint32_t instance, - BACNET_POLARITY polarity) +bool Binary_Output_Polarity_Set(uint32_t instance, BACNET_POLARITY polarity) { bool status = false; if (instance < MAX_BINARY_OUTPUTS) { if (polarity < MAX_POLARITY) { Polarity[instance] = polarity; - seeprom_bytes_write(NV_SEEPROM_BINARY_OUTPUT(instance, - NV_SEEPROM_BO_POLARITY), &Polarity[instance], 1); + seeprom_bytes_write( + NV_SEEPROM_BINARY_OUTPUT(instance, NV_SEEPROM_BO_POLARITY), + &Polarity[instance], 1); status = true; } } @@ -198,31 +173,28 @@ bool Binary_Output_Polarity_Set( return status; } -BACNET_POLARITY Binary_Output_Polarity( - uint32_t instance) +BACNET_POLARITY Binary_Output_Polarity(uint32_t instance) { BACNET_POLARITY polarity = POLARITY_NORMAL; if (instance < MAX_BINARY_OUTPUTS) { - polarity = (BACNET_POLARITY) Polarity[instance]; + polarity = (BACNET_POLARITY)Polarity[instance]; } return polarity; } -void Binary_Output_Out_Of_Service_Set( - uint32_t instance, - bool flag) +void Binary_Output_Out_Of_Service_Set(uint32_t instance, bool flag) { if (instance < MAX_BINARY_OUTPUTS) { Out_Of_Service[instance] = flag; - seeprom_bytes_write(NV_SEEPROM_BINARY_OUTPUT(instance, - NV_SEEPROM_BO_OUT_OF_SERVICE), &Out_Of_Service[instance], 1); + seeprom_bytes_write( + NV_SEEPROM_BINARY_OUTPUT(instance, NV_SEEPROM_BO_OUT_OF_SERVICE), + &Out_Of_Service[instance], 1); } } -bool Binary_Output_Out_Of_Service( - uint32_t instance) +bool Binary_Output_Out_Of_Service(uint32_t instance) { bool flag = false; @@ -235,10 +207,9 @@ bool Binary_Output_Out_Of_Service( /* note: the object name must be unique within this device */ bool Binary_Output_Object_Name( - uint32_t object_instance, - BACNET_CHARACTER_STRING * object_name) + uint32_t object_instance, BACNET_CHARACTER_STRING *object_name) { - static char text_string[32]; /* okay for single thread */ + static char text_string[32]; /* okay for single thread */ bool status = false; if (object_instance < MAX_BINARY_OUTPUTS) { @@ -250,11 +221,10 @@ bool Binary_Output_Object_Name( } /* return apdu len, or -1 on error */ -int Binary_Output_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Binary_Output_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { int len = 0; - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ BACNET_BIT_STRING bit_string = { 0 }; BACNET_CHARACTER_STRING char_string = { 0 }; BACNET_BINARY_PV present_value = BINARY_INACTIVE; @@ -270,9 +240,8 @@ int Binary_Output_Read_Property( apdu = rpdata->application_data; switch (rpdata->object_property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], rpdata->object_type, - rpdata->object_instance); + apdu_len = encode_application_object_id( + &apdu[0], rpdata->object_type, rpdata->object_instance); break; case PROP_OBJECT_NAME: Binary_Output_Object_Name(rpdata->object_instance, &char_string); @@ -312,8 +281,7 @@ int Binary_Output_Read_Property( object_index = Binary_Output_Instance_To_Index(rpdata->object_instance); apdu_len = - encode_application_enumerated(&apdu[0], - Polarity[object_index]); + encode_application_enumerated(&apdu[0], Polarity[object_index]); break; case PROP_PRIORITY_ARRAY: /* Array element zero is the number of elements in the array */ @@ -327,14 +295,13 @@ int Binary_Output_Read_Property( Binary_Output_Instance_To_Index(rpdata->object_instance); for (i = 0; i < BACNET_MAX_PRIORITY; i++) { /* FIXME: check if we have room before adding it to APDU */ - present_value = (BACNET_BINARY_PV) - Binary_Output_Level[object_index][i]; + present_value = + (BACNET_BINARY_PV)Binary_Output_Level[object_index][i]; if (present_value == BINARY_NULL) { len = encode_application_null(&apdu[apdu_len]); } else { - len = - encode_application_enumerated(&apdu[apdu_len], - present_value); + len = encode_application_enumerated( + &apdu[apdu_len], present_value); } /* add it if we have room */ if ((apdu_len + len) < MAX_APDU) @@ -351,14 +318,13 @@ int Binary_Output_Read_Property( Binary_Output_Instance_To_Index(rpdata->object_instance); if (rpdata->array_index <= BACNET_MAX_PRIORITY) { present_value = (BACNET_BINARY_PV) - Binary_Output_Level[object_index][rpdata->array_index - - 1]; + Binary_Output_Level[object_index] + [rpdata->array_index - 1]; if (present_value == BINARY_NULL) { apdu_len = encode_application_null(&apdu[apdu_len]); } else { - apdu_len = - encode_application_enumerated(&apdu[apdu_len], - present_value); + apdu_len = encode_application_enumerated( + &apdu[apdu_len], present_value); } } else { rpdata->error_class = ERROR_CLASS_PROPERTY; @@ -399,19 +365,17 @@ int Binary_Output_Read_Property( } /* returns true if successful */ -bool Binary_Output_Write_Property( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Binary_Output_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data) { - bool status = false; /* return value */ + bool status = false; /* return value */ unsigned int priority = 0; BACNET_BINARY_PV level = BINARY_NULL; int len = 0; BACNET_APPLICATION_DATA_VALUE value; /* decode the some of the request */ - len = - bacapp_decode_application_data(wp_data->application_data, - wp_data->application_data_len, &value); + len = bacapp_decode_application_data( + wp_data->application_data, wp_data->application_data_len, &value); /* FIXME: len < application_data_len: more data? */ if (len < 0) { /* error while decoding - a value larger than we can handle */ @@ -430,19 +394,19 @@ bool Binary_Output_Write_Property( case PROP_PRESENT_VALUE: status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_ENUMERATED, - &wp_data->error_class, &wp_data->error_code); + &wp_data->error_class, &wp_data->error_code); if (status) { 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 */ ) && + (priority != 6 /* reserved */) && (value.type.Enumerated <= MAX_BINARY_PV)) { - level = (BACNET_BINARY_PV) value.type.Enumerated; + level = (BACNET_BINARY_PV)value.type.Enumerated; priority--; - Binary_Output_Present_Value_Set(wp_data->object_instance, - level, priority); + Binary_Output_Present_Value_Set( + wp_data->object_instance, level, priority); } 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 @@ -456,21 +420,20 @@ bool Binary_Output_Write_Property( wp_data->error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; } } else { - status = - WPValidateArgType(&value, BACNET_APPLICATION_TAG_NULL, + status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_NULL, &wp_data->error_class, &wp_data->error_code); if (status) { level = BINARY_NULL; priority = wp_data->priority; if (priority && (priority <= BACNET_MAX_PRIORITY)) { priority--; - Binary_Output_Present_Value_Set - (wp_data->object_instance, level, priority); + Binary_Output_Present_Value_Set( + wp_data->object_instance, level, priority); } else if (priority == 6) { status = false; - /* Command priority 6 is reserved for use by Minimum On/Off - algorithm and may not be used for other purposes in any - object. */ + /* Command priority 6 is reserved for use by Minimum + On/Off algorithm and may not be used for other + purposes in any object. */ wp_data->error_class = ERROR_CLASS_PROPERTY; wp_data->error_code = ERROR_CODE_WRITE_ACCESS_DENIED; } else { @@ -482,22 +445,21 @@ bool Binary_Output_Write_Property( } break; case PROP_OUT_OF_SERVICE: - status = - WPValidateArgType(&value, BACNET_APPLICATION_TAG_BOOLEAN, + status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_BOOLEAN, &wp_data->error_class, &wp_data->error_code); if (status) { - Binary_Output_Out_Of_Service_Set(wp_data->object_instance, - value.type.Boolean); + Binary_Output_Out_Of_Service_Set( + wp_data->object_instance, value.type.Boolean); } break; case PROP_POLARITY: status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_ENUMERATED, - &wp_data->error_class, &wp_data->error_code); + &wp_data->error_class, &wp_data->error_code); if (status) { if (value.type.Enumerated < MAX_POLARITY) { Binary_Output_Polarity_Set(wp_data->object_instance, - (BACNET_POLARITY) value.type.Enumerated); + (BACNET_POLARITY)value.type.Enumerated); } else { status = false; wp_data->error_class = ERROR_CLASS_PROPERTY; @@ -528,8 +490,7 @@ bool Binary_Output_Write_Property( return status; } -void Binary_Output_Init( - void) +void Binary_Output_Init(void) { unsigned i, j; @@ -541,14 +502,15 @@ void Binary_Output_Init( if (Polarity[i] >= MAX_POLARITY) { Binary_Output_Polarity_Set(i, POLARITY_NORMAL); } - seeprom_bytes_read(NV_SEEPROM_BINARY_OUTPUT(i, - NV_SEEPROM_BO_OUT_OF_SERVICE), &Out_Of_Service[i], 1); + seeprom_bytes_read( + NV_SEEPROM_BINARY_OUTPUT(i, NV_SEEPROM_BO_OUT_OF_SERVICE), + &Out_Of_Service[i], 1); if (Out_Of_Service[i] > 1) { Binary_Output_Out_Of_Service_Set(i, false); } for (j = 0; j < BACNET_MAX_PRIORITY; j++) { - seeprom_bytes_read(NV_SEEPROM_BINARY_OUTPUT(i, - NV_SEEPROM_BO_PRIORITY_ARRAY_1 + j), + seeprom_bytes_read( + NV_SEEPROM_BINARY_OUTPUT(i, NV_SEEPROM_BO_PRIORITY_ARRAY_1 + j), &Binary_Output_Level[i][j], 1); } } diff --git a/ports/bdk-atxx4-mstp/bootloader/main.c b/ports/bdk-atxx4-mstp/bootloader/main.c index ec24ff15..ea1d311c 100644 --- a/ports/bdk-atxx4-mstp/bootloader/main.c +++ b/ports/bdk-atxx4-mstp/bootloader/main.c @@ -1,35 +1,33 @@ /***************************************************************************** -* -* Atmel Corporation -* -* File : main.c -* Compiler : IAR C 3.10C Kickstart, AVR-GCC/avr-libc(>= 1.2.5) -* Revision : $Revision: 1.7 $ -* Date : $Date: Tuesday, June 07, 200 $ -* Updated by : $Author: raapeland $ -* -* Support mail : avr@atmel.com -* -* Target platform : All AVRs with bootloader support -* -* AppNote : AVR109 - Self-programming -* -* Description : This Program allows an AVR with bootloader capabilities to -* Read/write its own Flash/EEprom. To enter Programming mode -* an input pin is checked. If this pin is pulled low, programming mode -* is entered. If not, normal execution is done from $0000 -* "reset" vector in Application area. -* -* Preparations : Use the preprocessor.xls file for obtaining a customized -* defines.h file and linker-file code-segment definition for -* the device you are compiling for. -****************************************************************************/ + * + * Atmel Corporation + * + * File : main.c + * Compiler : IAR C 3.10C Kickstart, AVR-GCC/avr-libc(>= 1.2.5) + * Revision : $Revision: 1.7 $ + * Date : $Date: Tuesday, June 07, 200 $ + * Updated by : $Author: raapeland $ + * + * Support mail : avr@atmel.com + * + * Target platform : All AVRs with bootloader support + * + * AppNote : AVR109 - Self-programming + * + * Description : This Program allows an AVR with bootloader capabilities to + * Read/write its own Flash/EEprom. To enter Programming mode + * an input pin is checked. If this pin is pulled low, + *programming mode is entered. If not, normal execution is done from $0000 + * "reset" vector in Application area. + * + * Preparations : Use the preprocessor.xls file for obtaining a customized + * defines.h file and linker-file code-segment definition for + * the device you are compiling for. + ****************************************************************************/ #include "defines.h" #include "serial.h" #include "flash.h" - - /* Uncomment the following to save code space */ /*#define REMOVE_AVRPROG_SUPPORT */ /*#define REMOVE_FUSE_AND_LOCK_BIT_SUPPORT */ @@ -52,70 +50,66 @@ #endif /* LARGE_MEMORY */ #ifndef REMOVE_BLOCK_SUPPORT -unsigned char BlockLoad( - unsigned int size, - unsigned char mem, - ADDR_T * address); -void BlockRead( - unsigned int size, - unsigned char mem, - ADDR_T * address); +unsigned char BlockLoad(unsigned int size, unsigned char mem, ADDR_T *address); +void BlockRead(unsigned int size, unsigned char mem, ADDR_T *address); -/* BLOCKSIZE should be chosen so that the following holds: BLOCKSIZE*n = PAGESIZE, where n=1,2,3... */ +/* BLOCKSIZE should be chosen so that the following holds: BLOCKSIZE*n = + * PAGESIZE, where n=1,2,3... */ #define BLOCKSIZE PAGESIZE #endif /* REMOVE_BLOCK_SUPPORT */ #ifdef __ICCAVR__ -__C_task void main( - void) +__C_task void main(void) #else /* ! __ICCAVR__ */ -int main( - void) +int main(void) #endif /* __ICCAVR__ */ { ADDR_T address; unsigned int temp_int; unsigned char val; - /* Initialization */ - void ( - *funcptr) ( - void) = 0x0000; /* Set up function pointer to RESET vector. */ - PROGPORT |= (1 << PROG_NO); /* Enable pull-up on PROG_NO line on PROGPORT. */ - initbootuart(); /* Initialize UART. */ - + void (*funcptr)(void) = + 0x0000; /* Set up function pointer to RESET vector. */ + PROGPORT |= + (1 << PROG_NO); /* Enable pull-up on PROG_NO line on PROGPORT. */ + initbootuart(); /* Initialize UART. */ /* Branch to bootloader or application code? */ - if (!(PROGPIN & (1 << PROG_NO))) { /* If PROGPIN is pulled low, enter programmingmode. */ + if (!(PROGPIN & (1 << PROG_NO))) { /* If PROGPIN is pulled low, enter + programmingmode. */ /* Main loop */ for (;;) { - val = recchar(); /* Wait for command character. */ + val = recchar(); /* Wait for command character. */ /* Check autoincrement status. */ if (val == 'a') { - sendchar('Y'); /* Yes, we do autoincrement. */ + sendchar('Y'); /* Yes, we do autoincrement. */ } - /* Set address. */ - else if (val == 'A') { /* Set address... *//* NOTE: Flash addresses are given in words, not bytes. */ - address = (recchar() << 8) | recchar(); /* Read address high and low byte. */ + else if (val == 'A') { + /* Set address... */ /* NOTE: Flash addresses are given in + words, not bytes. */ + address = (recchar() << 8) | + recchar(); /* Read address high and low byte. */ sendchar('\r'); /* Send OK back. */ } - /* Chip erase. */ else if (val == 'e') { - for (address = 0; address < APP_END; address += PAGESIZE) { /* NOTE: Here we use address as a byte-address, not word-address, for convenience. */ + for (address = 0; address < APP_END; address += + PAGESIZE) { /* NOTE: Here we use address as a byte-address, + not word-address, for convenience. */ _WAIT_FOR_SPM(); #ifdef __ICCAVR__ -#pragma diag_suppress=Pe1053 /* Suppress warning for conversion from long-type address to flash ptr. */ +#pragma diag_suppress = Pe1053 /* Suppress warning for conversion from \ + long-type address to flash ptr. */ #endif _PAGE_ERASE(address); #ifdef __ICCAVR__ -#pragma diag_default=Pe1053 /* Back to default. */ +#pragma diag_default = Pe1053 /* Back to default. */ #endif } @@ -124,25 +118,23 @@ int main( #ifndef REMOVE_BLOCK_SUPPORT /* Check block load support. */ else if (val == 'b') { - sendchar('Y'); /* Report block load supported. */ - sendchar((BLOCKSIZE >> 8) & 0xFF); /* MSB first. */ - sendchar(BLOCKSIZE & 0xFF); /* Report BLOCKSIZE (bytes). */ + sendchar('Y'); /* Report block load supported. */ + sendchar((BLOCKSIZE >> 8) & 0xFF); /* MSB first. */ + sendchar(BLOCKSIZE & 0xFF); /* Report BLOCKSIZE (bytes). */ } - /* Start block load. */ else if (val == 'B') { - temp_int = (recchar() << 8) | recchar(); /* Get block size. */ - val = recchar(); /* Get memtype. */ - sendchar(BlockLoad(temp_int, val, &address)); /* Block load. */ + temp_int = (recchar() << 8) | recchar(); /* Get block size. */ + val = recchar(); /* Get memtype. */ + sendchar(BlockLoad(temp_int, val, &address)); /* Block load. */ } - /* Start block read. */ else if (val == 'g') { - temp_int = (recchar() << 8) | recchar(); /* Get block size. */ - val = recchar(); /* Get memtype */ - BlockRead(temp_int, val, &address); /* Block read */ + temp_int = (recchar() << 8) | recchar(); /* Get block size. */ + val = recchar(); /* Get memtype */ + BlockRead(temp_int, val, &address); /* Block read */ } #endif /* REMOVE_BLOCK_SUPPORT */ @@ -153,53 +145,58 @@ int main( _WAIT_FOR_SPM(); _ENABLE_RWW_SECTION(); #ifdef __ICCAVR__ -#pragma diag_suppress=Pe1053 /* Suppress warning for conversion from long-type address to flash ptr. */ +#pragma diag_suppress = Pe1053 /* Suppress warning for conversion from \ + long-type address to flash ptr. */ #endif sendchar(_LOAD_PROGRAM_MEMORY((address << 1) + 1)); sendchar(_LOAD_PROGRAM_MEMORY((address << 1) + 0)); #ifdef __ICCAVR__ -#pragma diag_default=Pe1053 /* Back to default. */ +#pragma diag_default = Pe1053 /* Back to default. */ #endif - address++; /* Auto-advance to next Flash word. */ + address++; /* Auto-advance to next Flash word. */ } - /* Write program memory, low byte. */ - else if (val == 'c') { /* NOTE: Always use this command before sending high byte. */ - temp_int = recchar(); /* Get low byte for later _FILL_TEMP_WORD. */ + else if (val == 'c') { /* NOTE: Always use this command before + sending high byte. */ + temp_int = + recchar(); /* Get low byte for later _FILL_TEMP_WORD. */ sendchar('\r'); /* Send OK back. */ } - /* Write program memory, high byte. */ else if (val == 'C') { - temp_int |= (recchar() << 8); /* Get and insert high byte. */ + temp_int |= (recchar() << 8); /* Get and insert high byte. */ _WAIT_FOR_SPM(); #ifdef __ICCAVR__ -#pragma diag_suppress=Pe1053 /* Suppress warning for conversion from long-type address to flash ptr. */ +#pragma diag_suppress = Pe1053 /* Suppress warning for conversion from \ + long-type address to flash ptr. */ #endif - _FILL_TEMP_WORD((address << 1), temp_int); /* Convert word-address to byte-address and fill. */ + _FILL_TEMP_WORD( + (address << 1), temp_int); /* Convert word-address to + byte-address and fill. */ #ifdef __ICCAVR__ -#pragma diag_default=Pe1053 /* Back to default. */ +#pragma diag_default = Pe1053 /* Back to default. */ #endif - address++; /* Auto-advance to next Flash word. */ + address++; /* Auto-advance to next Flash word. */ sendchar('\r'); /* Send OK back. */ } - /* Write page. */ else if (val == 'm') { - if (address >= (APP_END >> 1)) { /* Protect bootloader area. */ + if (address >= (APP_END >> 1)) { /* Protect bootloader area. */ sendchar('?'); } else { _WAIT_FOR_SPM(); #ifdef __ICCAVR__ -#pragma diag_suppress=Pe1053 /* Suppress warning for conversion from long-type address to flash ptr. */ +#pragma diag_suppress = Pe1053 /* Suppress warning for conversion from \ + long-type address to flash ptr. */ #endif - _PAGE_WRITE(address << 1); /* Convert word-address to byte-address and write. */ + _PAGE_WRITE(address << 1); /* Convert word-address to + byte-address and write. */ #ifdef __ICCAVR__ -#pragma diag_default=Pe1053 /* Back to default. */ +#pragma diag_default = Pe1053 /* Back to default. */ #endif } @@ -211,26 +208,26 @@ int main( /* Write EEPROM memory. */ else if (val == 'D') { _WAIT_FOR_SPM(); - EEARL = address; /* Setup EEPROM address. */ + EEARL = address; /* Setup EEPROM address. */ EEARH = (address >> 8); - EEDR = recchar(); /* Get byte. */ - EECR |= (1 << EEMWE); /* Write byte. */ + EEDR = recchar(); /* Get byte. */ + EECR |= (1 << EEMWE); /* Write byte. */ EECR |= (1 << EEWE); - while (EECR & (1 << EEWE)) /* Wait for write operation to finish. */ + while (EECR & + (1 << EEWE)) /* Wait for write operation to finish. */ ; - address++; /* Auto-advance to next EEPROM byte. */ + address++; /* Auto-advance to next EEPROM byte. */ sendchar('\r'); /* Send OK back. */ } - /* Read EEPROM memory. */ else if (val == 'd') { - EEARL = address; /* Setup EEPROM address. */ + EEARL = address; /* Setup EEPROM address. */ EEARH = (address >> 8); - EECR |= (1 << EERE); /* Read byte... */ + EECR |= (1 << EERE); /* Read byte... */ sendchar(EEDR); /* ...and send it back. */ - address++; /* Auto-advance to next EEPROM byte. */ + address++; /* Auto-advance to next EEPROM byte. */ } #endif /* REMOVE_EEPROM_BYTE_SUPPORT */ @@ -238,7 +235,7 @@ int main( /* Write lockbits. */ else if (val == 'l') { _WAIT_FOR_SPM(); - _SET_LOCK_BITS(recchar()); /* Read and set lock bits. */ + _SET_LOCK_BITS(recchar()); /* Read and set lock bits. */ sendchar('\r'); /* Send OK back. */ } #if defined(_GET_LOCK_BITS) @@ -248,21 +245,18 @@ int main( sendchar(_GET_LOCK_BITS()); } - /* Read fuse bits. */ else if (val == 'F') { _WAIT_FOR_SPM(); sendchar(_GET_LOW_FUSES()); } - /* Read high fuse bits. */ else if (val == 'N') { _WAIT_FOR_SPM(); sendchar(_GET_HIGH_FUSES()); } - /* Read extended fuse bits. */ else if (val == 'Q') { _WAIT_FOR_SPM(); @@ -277,42 +271,40 @@ int main( sendchar('\r'); /* Nothing special to do, just answer OK. */ } - /* Exit bootloader. */ else if (val == 'E') { _WAIT_FOR_SPM(); _ENABLE_RWW_SECTION(); sendchar('\r'); - funcptr(); /* Jump to Reset vector 0x0000 in Application Section. */ + funcptr(); /* Jump to Reset vector 0x0000 in Application + Section. */ } - /* Get programmer type. */ else if (val == 'p') { - sendchar('S'); /* Answer 'SERIAL'. */ + sendchar('S'); /* Answer 'SERIAL'. */ } - /* Return supported device codes. */ else if (val == 't') { -#if PARTCODE+0 > 0 - sendchar(PARTCODE); /* Supports only this device, of course. */ +#if PARTCODE + 0 > 0 + sendchar(PARTCODE); /* Supports only this device, of course. */ #endif /* PARTCODE */ - sendchar(0); /* Send list terminator. */ + sendchar(0); /* Send list terminator. */ } - /* Set LED, clear LED and set device type. */ else if ((val == 'x') || (val == 'y') || (val == 'T')) { - recchar(); /* Ignore the command and it's parameter. */ + recchar(); /* Ignore the command and it's parameter. */ sendchar('\r'); /* Send OK back. */ } #endif /* REMOVE_AVRPROG_SUPPORT */ /* Return programmer identifier. */ else if (val == 'S') { - sendchar('A'); /* Return 'AVRBOOT'. */ - sendchar('V'); /* Software identifier (aka programmer signature) is always 7 characters. */ + sendchar('A'); /* Return 'AVRBOOT'. */ + sendchar('V'); /* Software identifier (aka programmer signature) + is always 7 characters. */ sendchar('R'); sendchar('B'); sendchar('O'); @@ -320,14 +312,12 @@ int main( sendchar('T'); } - /* Return software version. */ else if (val == 'V') { sendchar('1'); sendchar('5'); } - /* Return signature bytes. */ else if (val == 's') { sendchar(SIGNATURE_BYTE_3); @@ -335,25 +325,20 @@ int main( sendchar(SIGNATURE_BYTE_1); } - /* The last command to accept is ESC (synchronization). */ - else if (val != 0x1b) { /* If not ESC, then it is unrecognized... */ + else if (val != 0x1b) { /* If not ESC, then it is unrecognized... */ sendchar('?'); } - } /* end: for(;;) */ + } /* end: for(;;) */ } else { _WAIT_FOR_SPM(); _ENABLE_RWW_SECTION(); - funcptr(); /* Jump to Reset vector 0x0000 in Application Section. */ + funcptr(); /* Jump to Reset vector 0x0000 in Application Section. */ } -} /* end: main */ - +} /* end: main */ #ifndef REMOVE_BLOCK_SUPPORT -unsigned char BlockLoad( - unsigned int size, - unsigned char mem, - ADDR_T * address) +unsigned char BlockLoad(unsigned int size, unsigned char mem, ADDR_T *address) { unsigned char buffer[BLOCKSIZE]; unsigned int data; @@ -368,51 +353,54 @@ unsigned char BlockLoad( /* Then program the EEPROM */ _WAIT_FOR_SPM(); for (tempaddress = 0; tempaddress < size; tempaddress++) { - EEARL = *address; /* Setup EEPROM address */ + EEARL = *address; /* Setup EEPROM address */ EEARH = ((*address) >> 8); EEDR = buffer[tempaddress]; /* Get byte. */ - EECR |= (1 << EEMWE); /* Write byte. */ + EECR |= (1 << EEMWE); /* Write byte. */ EECR |= (1 << EEWE); - while (EECR & (1 << EEWE)) /* Wait for write operation to finish. */ + while (EECR & (1 << EEWE)) /* Wait for write operation to finish. */ ; - (*address)++; /* Select next EEPROM byte */ + (*address)++; /* Select next EEPROM byte */ } - return '\r'; /* Report programming OK */ + return '\r'; /* Report programming OK */ } /* Flash memory type. */ - else if (mem == 'F') { /* NOTE: For flash programming, 'address' is given in words. */ - (*address) <<= 1; /* Convert address to bytes temporarily. */ - tempaddress = (*address); /* Store address in page. */ + else if (mem == + 'F') { /* NOTE: For flash programming, 'address' is given in words. */ + (*address) <<= 1; /* Convert address to bytes temporarily. */ + tempaddress = (*address); /* Store address in page. */ do { data = recchar(); data |= (recchar() << 8); #ifdef __ICCAVR__ -#pragma diag_suppress=Pe1053 /* Suppress warning for conversion from long-type address to flash ptr. */ +#pragma diag_suppress = Pe1053 /* Suppress warning for conversion from \ + long-type address to flash ptr. */ #endif _FILL_TEMP_WORD(*address, data); #ifdef __ICCAVR__ -#pragma diag_default=Pe1053 /* Back to default. */ +#pragma diag_default = Pe1053 /* Back to default. */ #endif - (*address) += 2; /* Select next word in memory. */ - size -= 2; /* Reduce number of bytes to write by two. */ + (*address) += 2; /* Select next word in memory. */ + size -= 2; /* Reduce number of bytes to write by two. */ } while (size); /* Loop until all bytes written. */ #ifdef __ICCAVR__ -#pragma diag_suppress=Pe1053 /* Suppress warning for conversion from long-type address to flash ptr. */ +#pragma diag_suppress = Pe1053 /* Suppress warning for conversion from \ + long-type address to flash ptr. */ #endif _PAGE_WRITE(tempaddress); #ifdef __ICCAVR__ -#pragma diag_default=Pe1053 /* Back to default. */ +#pragma diag_default = Pe1053 /* Back to default. */ #endif _WAIT_FOR_SPM(); _ENABLE_RWW_SECTION(); - (*address) >>= 1; /* Convert address back to Flash words again. */ - return '\r'; /* Report programming OK */ + (*address) >>= 1; /* Convert address back to Flash words again. */ + return '\r'; /* Report programming OK */ } /* Invalid memory type? */ @@ -421,46 +409,42 @@ unsigned char BlockLoad( } } - -void BlockRead( - unsigned int size, - unsigned char mem, - ADDR_T * address) +void BlockRead(unsigned int size, unsigned char mem, ADDR_T *address) { /* EEPROM memory type. */ - if (mem == 'E') { /* Read EEPROM */ + if (mem == 'E') { /* Read EEPROM */ do { - EEARL = *address; /* Setup EEPROM address */ + EEARL = *address; /* Setup EEPROM address */ EEARH = ((*address) >> 8); - (*address)++; /* Select next EEPROM byte */ - EECR |= (1 << EERE); /* Read EEPROM */ - sendchar(EEDR); /* Transmit EEPROM dat ato PC */ + (*address)++; /* Select next EEPROM byte */ + EECR |= (1 << EERE); /* Read EEPROM */ + sendchar(EEDR); /* Transmit EEPROM dat ato PC */ - size--; /* Decrease number of bytes to read */ + size--; /* Decrease number of bytes to read */ } while (size); /* Repeat until all block has been read */ } /* Flash memory type. */ else if (mem == 'F') { - (*address) <<= 1; /* Convert address to bytes temporarily. */ + (*address) <<= 1; /* Convert address to bytes temporarily. */ do { #ifdef __ICCAVR__ -#pragma diag_suppress=Pe1053 /* Suppress warning for conversion from long-type address to flash ptr. */ +#pragma diag_suppress = Pe1053 /* Suppress warning for conversion from \ + long-type address to flash ptr. */ #endif sendchar(_LOAD_PROGRAM_MEMORY(*address)); sendchar(_LOAD_PROGRAM_MEMORY((*address) + 1)); #ifdef __ICCAVR__ -#pragma diag_default=Pe1053 /* Back to default. */ +#pragma diag_default = Pe1053 /* Back to default. */ #endif - (*address) += 2; /* Select next word in memory. */ - size -= 2; /* Subtract two bytes from number of bytes to read */ + (*address) += 2; /* Select next word in memory. */ + size -= 2; /* Subtract two bytes from number of bytes to read */ } while (size); /* Repeat until all block has been read */ - (*address) >>= 1; /* Convert address back to Flash words again. */ + (*address) >>= 1; /* Convert address back to Flash words again. */ } } #endif /* REMOVE_BLOCK_SUPPORT */ - /* end of file */ diff --git a/ports/bdk-atxx4-mstp/bootloader/serial.c b/ports/bdk-atxx4-mstp/bootloader/serial.c index 3d8ec5db..57dc9826 100644 --- a/ports/bdk-atxx4-mstp/bootloader/serial.c +++ b/ports/bdk-atxx4-mstp/bootloader/serial.c @@ -1,44 +1,41 @@ /***************************************************************************** -* -* Atmel Corporation -* -* File : serial.c -* Compiler : IAR C 3.10C Kickstart, AVR-GCC/avr-libc(>= 1.2.5) -* Revision : $Revision: 1.7 $ -* Date : $Date: Tuesday, June 07, 200 $ -* Updated by : $Author: raapeland $ -* -* Support mail : avr@atmel.com -* -* Target platform : All AVRs with bootloader support -* -* AppNote : AVR109 - Self-programming -* -* Description : UART communication routines -****************************************************************************/ + * + * Atmel Corporation + * + * File : serial.c + * Compiler : IAR C 3.10C Kickstart, AVR-GCC/avr-libc(>= 1.2.5) + * Revision : $Revision: 1.7 $ + * Date : $Date: Tuesday, June 07, 200 $ + * Updated by : $Author: raapeland $ + * + * Support mail : avr@atmel.com + * + * Target platform : All AVRs with bootloader support + * + * AppNote : AVR109 - Self-programming + * + * Description : UART communication routines + ****************************************************************************/ #include "defines.h" - -void initbootuart( - void) +void initbootuart(void) { BAUD_RATE_LOW_REG = BRREG_VALUE; - UART_CONTROL_REG = (1 << ENABLE_RECEIVER_BIT) | (1 << ENABLE_TRANSMITTER_BIT); /* enable receive and transmit */ + UART_CONTROL_REG = (1 << ENABLE_RECEIVER_BIT) | + (1 << ENABLE_TRANSMITTER_BIT); /* enable receive and transmit */ } - -void sendchar( - unsigned char c) +void sendchar(unsigned char c) { - UART_DATA_REG = c; /* prepare transmission */ - while (!(UART_STATUS_REG & (1 << TRANSMIT_COMPLETE_BIT))); /* wait until byte sendt */ - UART_STATUS_REG |= (1 << TRANSMIT_COMPLETE_BIT); /* delete TXCflag */ + UART_DATA_REG = c; /* prepare transmission */ + while (!(UART_STATUS_REG & (1 << TRANSMIT_COMPLETE_BIT))) + ; /* wait until byte sendt */ + UART_STATUS_REG |= (1 << TRANSMIT_COMPLETE_BIT); /* delete TXCflag */ } - -unsigned char recchar( - void) +unsigned char recchar(void) { - while (!(UART_STATUS_REG & (1 << RECEIVE_COMPLETE_BIT))); /* wait for data */ + while (!(UART_STATUS_REG & (1 << RECEIVE_COMPLETE_BIT))) + ; /* wait for data */ return UART_DATA_REG; } diff --git a/ports/bdk-atxx4-mstp/device.c b/ports/bdk-atxx4-mstp/device.c index b84e74cd..84a269d9 100644 --- a/ports/bdk-atxx4-mstp/device.c +++ b/ports/bdk-atxx4-mstp/device.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2007 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2007 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include #include @@ -50,10 +50,8 @@ static const char *BACnet_Version = BACNET_VERSION_TEXT; /* forward prototype */ -int Device_Read_Property_Local( - BACNET_READ_PROPERTY_DATA * rpdata); -bool Device_Write_Property_Local( - BACNET_WRITE_PROPERTY_DATA * wp_data); +int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA *rpdata); +bool Device_Write_Property_Local(BACNET_WRITE_PROPERTY_DATA *wp_data); static struct my_object_functions { BACNET_OBJECT_TYPE Object_Type; @@ -65,31 +63,28 @@ static struct my_object_functions { read_property_function Object_Read_Property; write_property_function Object_Write_Property; rpm_property_lists_function Object_RPM_List; -} Object_Table[] = { - { - OBJECT_DEVICE, NULL, /* don't init - recursive! */ - Device_Count, Device_Index_To_Instance, - Device_Valid_Object_Instance_Number, Device_Object_Name, - Device_Read_Property_Local, Device_Write_Property_Local, - Device_Property_Lists}, { - OBJECT_ANALOG_INPUT, Analog_Input_Init, Analog_Input_Count, - Analog_Input_Index_To_Instance, Analog_Input_Valid_Instance, - Analog_Input_Object_Name, Analog_Input_Read_Property, NULL, - Analog_Input_Property_Lists}, { - OBJECT_ANALOG_VALUE, Analog_Value_Init, Analog_Value_Count, - Analog_Value_Index_To_Instance, Analog_Value_Valid_Instance, - Analog_Value_Object_Name, Analog_Value_Read_Property, - Analog_Value_Write_Property, Analog_Value_Property_Lists}, { - OBJECT_BINARY_INPUT, Binary_Input_Init, Binary_Input_Count, - Binary_Input_Index_To_Instance, Binary_Input_Valid_Instance, - Binary_Input_Object_Name, Binary_Input_Read_Property, NULL, - Binary_Input_Property_Lists}, { - OBJECT_BINARY_OUTPUT, Binary_Output_Init, Binary_Output_Count, - Binary_Output_Index_To_Instance, Binary_Output_Valid_Instance, - Binary_Output_Object_Name, Binary_Output_Read_Property, - Binary_Output_Write_Property, Binary_Output_Property_Lists}, { - MAX_BACNET_OBJECT_TYPE, NULL, NULL, NULL, NULL, NULL, NULL, NULL} -}; +} Object_Table[] = { { OBJECT_DEVICE, NULL, /* don't init - recursive! */ + Device_Count, Device_Index_To_Instance, + Device_Valid_Object_Instance_Number, + Device_Object_Name, Device_Read_Property_Local, + Device_Write_Property_Local, Device_Property_Lists }, + { OBJECT_ANALOG_INPUT, Analog_Input_Init, Analog_Input_Count, + Analog_Input_Index_To_Instance, Analog_Input_Valid_Instance, + Analog_Input_Object_Name, Analog_Input_Read_Property, NULL, + Analog_Input_Property_Lists }, + { OBJECT_ANALOG_VALUE, Analog_Value_Init, Analog_Value_Count, + Analog_Value_Index_To_Instance, Analog_Value_Valid_Instance, + Analog_Value_Object_Name, Analog_Value_Read_Property, + Analog_Value_Write_Property, Analog_Value_Property_Lists }, + { OBJECT_BINARY_INPUT, Binary_Input_Init, Binary_Input_Count, + Binary_Input_Index_To_Instance, Binary_Input_Valid_Instance, + Binary_Input_Object_Name, Binary_Input_Read_Property, NULL, + Binary_Input_Property_Lists }, + { OBJECT_BINARY_OUTPUT, Binary_Output_Init, Binary_Output_Count, + Binary_Output_Index_To_Instance, Binary_Output_Valid_Instance, + Binary_Output_Object_Name, Binary_Output_Read_Property, + Binary_Output_Write_Property, Binary_Output_Property_Lists }, + { MAX_BACNET_OBJECT_TYPE, NULL, NULL, NULL, NULL, NULL, NULL, NULL } }; /* note: you really only need to define variables for properties that are writable or that may change. @@ -102,44 +97,20 @@ static BACNET_REINITIALIZED_STATE Reinitialize_State = BACNET_REINIT_IDLE; static const char *Reinit_Password = "rehmite"; /* These three arrays are used by the ReadPropertyMultiple handler */ -static const int Device_Properties_Required[] = { - PROP_OBJECT_IDENTIFIER, - PROP_OBJECT_NAME, - PROP_OBJECT_TYPE, - PROP_SYSTEM_STATUS, - PROP_VENDOR_NAME, - PROP_VENDOR_IDENTIFIER, - PROP_MODEL_NAME, - PROP_FIRMWARE_REVISION, - PROP_APPLICATION_SOFTWARE_VERSION, - PROP_PROTOCOL_VERSION, - PROP_PROTOCOL_REVISION, - PROP_PROTOCOL_SERVICES_SUPPORTED, - PROP_PROTOCOL_OBJECT_TYPES_SUPPORTED, - PROP_OBJECT_LIST, - PROP_MAX_APDU_LENGTH_ACCEPTED, - PROP_SEGMENTATION_SUPPORTED, - PROP_APDU_TIMEOUT, - PROP_NUMBER_OF_APDU_RETRIES, - PROP_DEVICE_ADDRESS_BINDING, - PROP_DATABASE_REVISION, - -1 -}; +static const int Device_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, + PROP_OBJECT_NAME, PROP_OBJECT_TYPE, PROP_SYSTEM_STATUS, PROP_VENDOR_NAME, + PROP_VENDOR_IDENTIFIER, PROP_MODEL_NAME, PROP_FIRMWARE_REVISION, + PROP_APPLICATION_SOFTWARE_VERSION, PROP_PROTOCOL_VERSION, + PROP_PROTOCOL_REVISION, PROP_PROTOCOL_SERVICES_SUPPORTED, + PROP_PROTOCOL_OBJECT_TYPES_SUPPORTED, PROP_OBJECT_LIST, + PROP_MAX_APDU_LENGTH_ACCEPTED, PROP_SEGMENTATION_SUPPORTED, + PROP_APDU_TIMEOUT, PROP_NUMBER_OF_APDU_RETRIES, PROP_DEVICE_ADDRESS_BINDING, + PROP_DATABASE_REVISION, -1 }; -static const int Device_Properties_Optional[] = { - PROP_MAX_MASTER, - PROP_MAX_INFO_FRAMES, - PROP_DESCRIPTION, - PROP_LOCATION, - -1 -}; +static const int Device_Properties_Optional[] = { PROP_MAX_MASTER, + PROP_MAX_INFO_FRAMES, PROP_DESCRIPTION, PROP_LOCATION, -1 }; -static const int Device_Properties_Proprietary[] = { - 512, - 513, - 9600, - -1 -}; +static const int Device_Properties_Proprietary[] = { 512, 513, 9600, -1 }; static struct my_object_functions *Device_Objects_Find_Functions( BACNET_OBJECT_TYPE Object_Type) @@ -161,8 +132,7 @@ static struct my_object_functions *Device_Objects_Find_Functions( /* Encodes the property APDU and returns the length, or sets the error, and returns BACNET_STATUS_ERROR */ -int Device_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Device_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { int apdu_len = BACNET_STATUS_ERROR; struct my_object_functions *pObject = NULL; @@ -183,8 +153,7 @@ int Device_Read_Property( return apdu_len; } -bool Device_Write_Property( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data) { bool status = false; struct my_object_functions *pObject = NULL; @@ -212,8 +181,7 @@ bool Device_Write_Property( return status; } -static unsigned my_property_list_count( - const int *pList) +static unsigned my_property_list_count(const int *pList) { unsigned property_count = 0; @@ -228,8 +196,7 @@ static unsigned my_property_list_count( } /* for a given object type, returns the special property list */ -void Device_Objects_Property_List( - BACNET_OBJECT_TYPE object_type, +void Device_Objects_Property_List(BACNET_OBJECT_TYPE object_type, uint32_t object_instance, struct special_property_list_t *pPropertyList) { @@ -252,25 +219,23 @@ void Device_Objects_Property_List( } /* Fetch the counts if available otherwise zero them */ - pPropertyList->Required.count = - pPropertyList->Required.pList == - NULL ? 0 : my_property_list_count(pPropertyList->Required.pList); + pPropertyList->Required.count = pPropertyList->Required.pList == NULL + ? 0 + : my_property_list_count(pPropertyList->Required.pList); - pPropertyList->Optional.count = - pPropertyList->Optional.pList == - NULL ? 0 : my_property_list_count(pPropertyList->Optional.pList); + pPropertyList->Optional.count = pPropertyList->Optional.pList == NULL + ? 0 + : my_property_list_count(pPropertyList->Optional.pList); - pPropertyList->Proprietary.count = - pPropertyList->Proprietary.pList == - NULL ? 0 : my_property_list_count(pPropertyList->Proprietary.pList); + pPropertyList->Proprietary.count = pPropertyList->Proprietary.pList == NULL + ? 0 + : my_property_list_count(pPropertyList->Proprietary.pList); return; } void Device_Property_Lists( - const int **pRequired, - const int **pOptional, - const int **pProprietary) + const int **pRequired, const int **pOptional, const int **pProprietary) { if (pRequired) *pRequired = Device_Properties_Required; @@ -282,23 +247,20 @@ void Device_Property_Lists( return; } -unsigned Device_Count( - void) +unsigned Device_Count(void) { return 1; } -uint32_t Device_Index_To_Instance( - unsigned index) +uint32_t Device_Index_To_Instance(unsigned index) { index = index; return Object_Instance_Number; } -static char *Device_Name_Default( - void) +static char *Device_Name_Default(void) { - static char text_string[32]; /* okay for single thread */ + static char text_string[32]; /* okay for single thread */ sprintf(text_string, "DEVICE-%lu", Object_Instance_Number); @@ -306,8 +268,7 @@ static char *Device_Name_Default( } bool Device_Object_Name( - uint32_t object_instance, - BACNET_CHARACTER_STRING * object_name) + uint32_t object_instance, BACNET_CHARACTER_STRING *object_name) { bool status = false; @@ -319,8 +280,7 @@ bool Device_Object_Name( return status; } -bool Device_Reinitialize( - BACNET_REINITIALIZE_DEVICE_DATA * rd_data) +bool Device_Reinitialize(BACNET_REINITIALIZE_DEVICE_DATA *rd_data) { bool status = false; @@ -363,20 +323,18 @@ bool Device_Reinitialize( return status; } -BACNET_REINITIALIZED_STATE Device_Reinitialized_State( - void) +BACNET_REINITIALIZED_STATE Device_Reinitialized_State(void) { return Reinitialize_State; } -void Device_Init( - object_functions_t * object_table) +void Device_Init(object_functions_t *object_table) { struct my_object_functions *pObject = NULL; /* we don't use the object table passed in since there is extra stuff we don't need in there. */ - (void) object_table; + (void)object_table; /* our local object table */ pObject = &Object_Table[0]; while (pObject->Object_Type < MAX_BACNET_OBJECT_TYPE) { @@ -389,14 +347,12 @@ void Device_Init( } /* 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 */ @@ -411,21 +367,17 @@ bool Device_Set_Object_Instance_Number( return status; } -bool Device_Valid_Object_Instance_Number( - uint32_t object_id) +bool Device_Valid_Object_Instance_Number(uint32_t object_id) { return (Object_Instance_Number == object_id); } -BACNET_DEVICE_STATUS Device_System_Status( - void) +BACNET_DEVICE_STATUS Device_System_Status(void) { return System_Status; } -int Device_Set_System_Status( - BACNET_DEVICE_STATUS status, - bool local) +int Device_Set_System_Status(BACNET_DEVICE_STATUS status, bool local) { /*return value - 0 = ok, -1 = bad value, -2 = not allowed */ int result = -1; @@ -438,34 +390,29 @@ int Device_Set_System_Status( return result; } -uint16_t Device_Vendor_Identifier( - void) +uint16_t Device_Vendor_Identifier(void) { return BACNET_VENDOR_ID; } -BACNET_SEGMENTATION Device_Segmentation_Supported( - void) +BACNET_SEGMENTATION Device_Segmentation_Supported(void) { return SEGMENTATION_NONE; } -uint32_t Device_Database_Revision( - void) +uint32_t Device_Database_Revision(void) { return Database_Revision; } -void Device_Inc_Database_Revision( - void) +void Device_Inc_Database_Revision(void) { Database_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 = 0; /* number of objects */ struct my_object_functions *pObject = NULL; @@ -483,9 +430,7 @@ unsigned Device_Object_List_Count( } bool Device_Object_List_Identifier( - uint32_t array_index, - BACNET_OBJECT_TYPE *object_type, - uint32_t * instance) + uint32_t array_index, BACNET_OBJECT_TYPE *object_type, uint32_t *instance) { bool status = false; uint32_t count = 0; @@ -516,10 +461,9 @@ bool Device_Object_List_Identifier( return status; } -bool Device_Valid_Object_Name( - BACNET_CHARACTER_STRING * object_name1, +bool Device_Valid_Object_Name(BACNET_CHARACTER_STRING *object_name1, BACNET_OBJECT_TYPE *object_type, - uint32_t * object_instance) + uint32_t *object_instance) { bool found = false; BACNET_OBJECT_TYPE type = OBJECT_NONE; @@ -533,7 +477,7 @@ bool Device_Valid_Object_Name( for (i = 1; i <= max_objects; i++) { check_id = Device_Object_List_Identifier(i, &type, &instance); if (check_id) { - pObject = Device_Objects_Find_Functions((BACNET_OBJECT_TYPE) type); + pObject = Device_Objects_Find_Functions((BACNET_OBJECT_TYPE)type); if ((pObject != NULL) && (pObject->Object_Name != NULL) && (pObject->Object_Name(instance, &object_name2) && characterstring_same(object_name1, &object_name2))) { @@ -553,13 +497,12 @@ bool Device_Valid_Object_Name( } bool Device_Valid_Object_Id( - BACNET_OBJECT_TYPE object_type, - uint32_t object_instance) + BACNET_OBJECT_TYPE object_type, uint32_t object_instance) { - bool status = false; /* return value */ + bool status = false; /* return value */ struct my_object_functions *pObject = NULL; - pObject = Device_Objects_Find_Functions((BACNET_OBJECT_TYPE) object_type); + pObject = Device_Objects_Find_Functions((BACNET_OBJECT_TYPE)object_type); if ((pObject != NULL) && (pObject->Object_Valid_Instance != NULL)) { status = pObject->Object_Valid_Instance(object_instance); } @@ -567,10 +510,9 @@ bool Device_Valid_Object_Id( return status; } -bool Device_Object_Name_Copy( - BACNET_OBJECT_TYPE object_type, +bool Device_Object_Name_Copy(BACNET_OBJECT_TYPE object_type, uint32_t object_instance, - BACNET_CHARACTER_STRING * object_name) + BACNET_CHARACTER_STRING *object_name) { struct my_object_functions *pObject = NULL; bool found = false; @@ -584,11 +526,10 @@ bool Device_Object_Name_Copy( } /* return the length of the apdu encoded or BACNET_STATUS_ERROR for error */ -int Device_Read_Property_Local( - BACNET_READ_PROPERTY_DATA * rpdata) +int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA *rpdata) { - 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 = { 0 }; BACNET_CHARACTER_STRING char_string = { 0 }; uint32_t i = 0; @@ -603,11 +544,10 @@ int Device_Read_Property_Local( return 0; } apdu = rpdata->application_data; - switch ((int) rpdata->object_property) { + switch ((int)rpdata->object_property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], rpdata->object_type, - rpdata->object_instance); + apdu_len = encode_application_object_id( + &apdu[0], rpdata->object_type, rpdata->object_instance); break; case PROP_OBJECT_NAME: Device_Object_Name(rpdata->object_instance, &char_string); @@ -625,15 +565,14 @@ int Device_Read_Property_Local( encode_application_character_string(&apdu[0], &char_string); break; case PROP_LOCATION: - bacnet_name(NV_EEPROM_DEVICE_LOCATION, &char_string, - "default location"); + bacnet_name( + NV_EEPROM_DEVICE_LOCATION, &char_string, "default location"); 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()); + encode_application_enumerated(&apdu[0], Device_System_Status()); break; case PROP_VENDOR_NAME: characterstring_init_ansi(&char_string, BACNET_VENDOR_NAME); @@ -664,16 +603,15 @@ int Device_Read_Property_Local( break; case PROP_PROTOCOL_REVISION: apdu_len = - encode_application_unsigned(&apdu[0], - BACNET_PROTOCOL_REVISION); + encode_application_unsigned(&apdu[0], BACNET_PROTOCOL_REVISION); 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)); + 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; @@ -683,7 +621,7 @@ int Device_Read_Property_Local( 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); + bitstring_set_bit(&bit_string, (uint8_t)i, false); } /* set the object types with objects to supported */ i = 0; @@ -707,11 +645,10 @@ int Device_Read_Property_Local( /* your maximum APDU size. */ else if (rpdata->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); + 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? */ @@ -731,11 +668,10 @@ int Device_Read_Property_Local( } } } else { - if (Device_Object_List_Identifier(rpdata->array_index, - &object_type, &instance)) - apdu_len = - encode_application_object_id(&apdu[0], object_type, - instance); + if (Device_Object_List_Identifier( + rpdata->array_index, &object_type, &instance)) + apdu_len = encode_application_object_id( + &apdu[0], object_type, instance); else { rpdata->error_class = ERROR_CLASS_PROPERTY; rpdata->error_code = ERROR_CODE_INVALID_ARRAY_INDEX; @@ -747,9 +683,8 @@ int Device_Read_Property_Local( apdu_len = encode_application_unsigned(&apdu[0], MAX_APDU); break; case PROP_SEGMENTATION_SUPPORTED: - apdu_len = - encode_application_enumerated(&apdu[0], - Device_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()); @@ -761,14 +696,12 @@ int Device_Read_Property_Local( /* FIXME: encode the list here, if it exists */ break; case PROP_DATABASE_REVISION: - apdu_len = - encode_application_unsigned(&apdu[0], - Device_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()); + encode_application_unsigned(&apdu[0], dlmstp_max_info_frames()); break; case PROP_MAX_MASTER: apdu_len = @@ -781,8 +714,7 @@ int Device_Read_Property_Local( apdu_len = encode_application_unsigned(&apdu[0], stack_unused()); break; case 9600: - apdu_len = - encode_application_unsigned(&apdu[0], rs485_baud_rate()); + apdu_len = encode_application_unsigned(&apdu[0], rs485_baud_rate()); break; default: rpdata->error_class = ERROR_CLASS_PROPERTY; @@ -801,18 +733,16 @@ int Device_Read_Property_Local( return apdu_len; } -bool Device_Write_Property_Local( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Device_Write_Property_Local(BACNET_WRITE_PROPERTY_DATA *wp_data) { - bool status = false; /* return value - false=error */ + bool status = false; /* return value - false=error */ int len = 0; BACNET_APPLICATION_DATA_VALUE value; uint8_t max_master = 0; /* decode the some of the request */ - len = - bacapp_decode_application_data(wp_data->application_data, - wp_data->application_data_len, &value); + len = bacapp_decode_application_data( + wp_data->application_data, wp_data->application_data_len, &value); /* FIXME: len < application_data_len: more data? */ if (len < 0) { /* error while decoding - a value larger than we can handle */ @@ -827,14 +757,14 @@ bool Device_Write_Property_Local( wp_data->error_code = ERROR_CODE_PROPERTY_IS_NOT_AN_ARRAY; return false; } - switch ((int) wp_data->object_property) { + switch ((int)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))) { + (Device_Set_Object_Instance_Number( + value.type.Object_Id.instance))) { eeprom_bytes_write(NV_EEPROM_DEVICE_0, - (uint8_t *) & value.type.Object_Id.instance, 4); + (uint8_t *)&value.type.Object_Id.instance, 4); /* we could send an I-Am broadcast to let the world know */ status = true; } else { @@ -879,8 +809,7 @@ bool Device_Write_Property_Local( break; case PROP_OBJECT_NAME: if (value.tag == BACNET_APPLICATION_TAG_CHARACTER_STRING) { - status = - bacnet_name_write_unique(NV_EEPROM_DEVICE_NAME, + status = bacnet_name_write_unique(NV_EEPROM_DEVICE_NAME, wp_data->object_type, wp_data->object_instance, &value.type.Character_String, &wp_data->error_class, &wp_data->error_code); @@ -891,8 +820,7 @@ bool Device_Write_Property_Local( break; case PROP_DESCRIPTION: if (value.tag == BACNET_APPLICATION_TAG_CHARACTER_STRING) { - status = - bacnet_name_write(NV_EEPROM_DEVICE_DESCRIPTION, + status = bacnet_name_write(NV_EEPROM_DEVICE_DESCRIPTION, &value.type.Character_String, &wp_data->error_class, &wp_data->error_code); } else { @@ -902,8 +830,7 @@ bool Device_Write_Property_Local( break; case PROP_LOCATION: if (value.tag == BACNET_APPLICATION_TAG_CHARACTER_STRING) { - status = - bacnet_name_write(NV_EEPROM_DEVICE_LOCATION, + status = bacnet_name_write(NV_EEPROM_DEVICE_LOCATION, &value.type.Character_String, &wp_data->error_class, &wp_data->error_code); } else { diff --git a/ports/bdk-atxx4-mstp/dlmstp.c b/ports/bdk-atxx4-mstp/dlmstp.c index e58da09f..4ee6479e 100644 --- a/ports/bdk-atxx4-mstp/dlmstp.c +++ b/ports/bdk-atxx4-mstp/dlmstp.c @@ -68,19 +68,19 @@ static struct mstp_flag_t { /* A Boolean flag set to TRUE by the Receive State Machine */ /* if an invalid frame is received. */ /* Set to FALSE by the main state machine. */ - unsigned ReceivedInvalidFrame:1; + unsigned ReceivedInvalidFrame : 1; /* A Boolean flag set to TRUE by the Receive State Machine */ /* if a valid frame is received. */ /* Set to FALSE by the main state machine. */ - unsigned ReceivedValidFrame:1; + unsigned ReceivedValidFrame : 1; /* set to TRUE when we get a frame not for us */ - unsigned ReceivedValidFrameNotForUs:1; + unsigned ReceivedValidFrameNotForUs : 1; /* A Boolean flag set to TRUE by the master machine if this node is the */ /* only known master node. */ - unsigned SoleMaster:1; + unsigned SoleMaster : 1; /* A Boolean flag set TRUE by the datalink if a packet has been received, but not processed. */ - unsigned ReceivePacketPending:1; + unsigned ReceivePacketPending : 1; } MSTP_Flag; /* Used to store the data length of a received frame. */ @@ -182,7 +182,11 @@ static uint8_t Nmax_master = 127; #define Tusage_delay 15 /* we need to be able to increment without rolling over */ -#define INCREMENT_AND_LIMIT_UINT8(x) {if (x < 0xFF) x++;} +#define INCREMENT_AND_LIMIT_UINT8(x) \ + { \ + if (x < 0xFF) \ + x++; \ + } /* data structure for MS/TP PDU Queue */ struct mstp_pdu_packet { @@ -194,26 +198,22 @@ struct mstp_pdu_packet { static struct mstp_pdu_packet PDU_Buffer[MSTP_PDU_PACKET_COUNT]; static RING_BUFFER PDU_Queue; -bool dlmstp_init( - char *ifname) +bool dlmstp_init(char *ifname) { ifname = ifname; - Ringbuf_Init(&PDU_Queue, (uint8_t *) & PDU_Buffer, + Ringbuf_Init(&PDU_Queue, (uint8_t *)&PDU_Buffer, sizeof(struct mstp_pdu_packet), MSTP_PDU_PACKET_COUNT); 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; @@ -236,11 +236,10 @@ void dlmstp_fill_bacnet_address( } } -static bool dlmstp_compare_data_expecting_reply( - uint8_t * request_pdu, +static bool dlmstp_compare_data_expecting_reply(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) { @@ -261,9 +260,8 @@ 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], NULL, &request.address, - &request.npdu_data); + offset = npdu_decode( + &request_pdu[0], NULL, &request.address, &request.npdu_data); if (request.npdu_data.network_layer_message) { return false; } @@ -280,8 +278,7 @@ static bool dlmstp_compare_data_expecting_reply( /* decode the reply data */ reply.address.mac[0] = dest_address; reply.address.mac_len = 1; - offset = - npdu_decode(&reply_pdu[0], &reply.address, NULL, &reply.npdu_data); + offset = npdu_decode(&reply_pdu[0], &reply.address, NULL, &reply.npdu_data); if (reply.npdu_data.network_layer_message) { return false; } @@ -322,7 +319,8 @@ static bool dlmstp_compare_data_expecting_reply( return false; } } - if (request.npdu_data.protocol_version != reply.npdu_data.protocol_version) { + if (request.npdu_data.protocol_version != + reply.npdu_data.protocol_version) { return false; } #if 0 @@ -354,16 +352,16 @@ static bool dlmstp_compare_data_expecting_reply( /* (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 destination, /* destination address */ - uint8_t source, /* source address */ - uint8_t * data, /* any data to be sent - may be null */ + 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) -{ /* 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 header crc */ - uint8_t buffer_crc[2]; /* stores the data crc */ - uint16_t i = 0; /* used to calculate CRC for data */ +{ /* 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 header crc */ + uint8_t buffer_crc[2]; /* stores the data crc */ + uint16_t i = 0; /* used to calculate CRC for data */ /* create the MS/TP header */ buffer[0] = 0x55; @@ -401,8 +399,7 @@ static void MSTP_Send_Frame( rs485_rts_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; @@ -638,8 +635,7 @@ static void MSTP_Receive_Frame_FSM( #ifdef MSTP_DEBUG_STATES static MSTP_MASTER_STATE Master_State_Log[128]; static unsigned master_state_log_index = 0; -void log_master_state( - MSTP_MASTER_STATE state) +void log_master_state(MSTP_MASTER_STATE state) { Master_State_Log[master_state_log_index] = state; master_state_log_index++; @@ -652,8 +648,7 @@ void log_master_state( #endif /* 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 */ @@ -758,9 +753,8 @@ static bool MSTP_Master_Node_FSM( } break; case FRAME_TYPE_TEST_REQUEST: - MSTP_Send_Frame(FRAME_TYPE_TEST_RESPONSE, - SourceAddress, This_Station, &InputBuffer[0], - DataLength); + MSTP_Send_Frame(FRAME_TYPE_TEST_RESPONSE, SourceAddress, + This_Station, &InputBuffer[0], DataLength); break; case FRAME_TYPE_TEST_RESPONSE: default: @@ -785,14 +779,14 @@ static bool MSTP_Master_Node_FSM( transition_now = true; } else { uint8_t frame_type; - pkt = (struct mstp_pdu_packet *) Ringbuf_Peek(&PDU_Queue); + pkt = (struct mstp_pdu_packet *)Ringbuf_Peek(&PDU_Queue); if (pkt->data_expecting_reply) { frame_type = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY; } else { frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; } MSTP_Send_Frame(frame_type, pkt->destination_mac, This_Station, - (uint8_t *) & pkt->buffer[0], pkt->length); + (uint8_t *)&pkt->buffer[0], pkt->length); FrameCount++; switch (frame_type) { case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: @@ -812,7 +806,7 @@ static bool MSTP_Master_Node_FSM( Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; break; } - (void) Ringbuf_Pop(&PDU_Queue, NULL); + (void)Ringbuf_Pop(&PDU_Queue, NULL); } break; case MSTP_MASTER_STATE_WAIT_FOR_REPLY: @@ -924,8 +918,8 @@ static bool MSTP_Master_Node_FSM( 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; @@ -939,7 +933,7 @@ static bool MSTP_Master_Node_FSM( /* no known successor node */ Next_Station = This_Station; RetryCount = 0; - TokenCount = 1; /* changed in Errata SSPC-135-2004 */ + 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; @@ -947,10 +941,10 @@ static bool MSTP_Master_Node_FSM( /* ResetMaintenancePFM */ Poll_Station = This_Station; /* 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; - TokenCount = 1; /* changed in Errata SSPC-135-2004 */ + TokenCount = 1; /* changed in Errata SSPC-135-2004 */ EventCount = 0; Master_State = MSTP_MASTER_STATE_PASS_TOKEN; } @@ -971,8 +965,8 @@ static bool MSTP_Master_Node_FSM( /* RetrySendToken */ RetryCount++; /* 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); EventCount = 0; /* re-enter the current state to listen for NS */ /* to begin using the token. */ @@ -1049,15 +1043,15 @@ static bool MSTP_Master_Node_FSM( /* 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)) { + 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); + MSTP_Send_Frame( + FRAME_TYPE_TOKEN, Next_Station, This_Station, NULL, 0); Poll_Station = This_Station; TokenCount = 0; RetryCount = 0; @@ -1122,12 +1116,11 @@ static bool MSTP_Master_Node_FSM( /* a proprietary frame that expects a reply is received. */ timeout = rs485_silence_time_elapsed(Treply_delay); if (!timeout) { - pkt = (struct mstp_pdu_packet *) Ringbuf_Peek(&PDU_Queue); + pkt = (struct mstp_pdu_packet *)Ringbuf_Peek(&PDU_Queue); if (pkt != NULL) { - matched = - dlmstp_compare_data_expecting_reply(&InputBuffer[0], - DataLength, SourceAddress, &pkt->buffer[0], - pkt->length, pkt->destination_mac); + matched = dlmstp_compare_data_expecting_reply( + &InputBuffer[0], DataLength, SourceAddress, + &pkt->buffer[0], pkt->length, pkt->destination_mac); } else { matched = false; } @@ -1144,17 +1137,15 @@ static bool MSTP_Master_Node_FSM( if (pkt->data_expecting_reply) { frame_type = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY; } else { - frame_type = - FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; + frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; } - MSTP_Send_Frame(frame_type, pkt->destination_mac, - This_Station, (uint8_t *) & pkt->buffer[0], - pkt->length); + MSTP_Send_Frame(frame_type, pkt->destination_mac, This_Station, + (uint8_t *)&pkt->buffer[0], pkt->length); Master_State = MSTP_MASTER_STATE_IDLE; /* clear our flag we were holding for comparison */ MSTP_Flag.ReceivedValidFrame = false; /* clear the queue */ - (void) Ringbuf_Pop(&PDU_Queue, NULL); + (void)Ringbuf_Pop(&PDU_Queue, NULL); } else if ((pkt != NULL) || timeout) { /* DeferredReply */ /* If no reply will be available from the higher layers */ @@ -1180,8 +1171,7 @@ static bool MSTP_Master_Node_FSM( return transition_now; } -static void MSTP_Slave_Node_FSM( - void) +static void MSTP_Slave_Node_FSM(void) { /* packet from the PDU Queue */ struct mstp_pdu_packet *pkt; @@ -1215,9 +1205,8 @@ static void MSTP_Slave_Node_FSM( } } else if (MSTP_Flag.ReceivePacketPending) { if (!Ringbuf_Empty(&PDU_Queue)) { - pkt = (struct mstp_pdu_packet *) Ringbuf_Peek(&PDU_Queue); - matched = - dlmstp_compare_data_expecting_reply(&InputBuffer[0], + pkt = (struct mstp_pdu_packet *)Ringbuf_Peek(&PDU_Queue); + matched = dlmstp_compare_data_expecting_reply(&InputBuffer[0], DataLength, SourceAddress, &pkt->buffer[0], pkt->length, pkt->destination_mac); if (matched) { @@ -1235,8 +1224,8 @@ static void MSTP_Slave_Node_FSM( frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; } MSTP_Send_Frame(frame_type, pkt->destination_mac, This_Station, - (uint8_t *) & pkt->buffer[0], pkt->length); - (void) Ringbuf_Pop(&PDU_Queue, NULL); + (uint8_t *)&pkt->buffer[0], pkt->length); + (void)Ringbuf_Pop(&PDU_Queue, NULL); } /* clear our flag we were holding for comparison */ MSTP_Flag.ReceivePacketPending = false; @@ -1252,17 +1241,16 @@ static void MSTP_Slave_Node_FSM( } /* 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 */ +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 */ +{ /* number of bytes of data */ int bytes_sent = 0; struct mstp_pdu_packet *pkt; uint16_t i = 0; - pkt = (struct mstp_pdu_packet *) Ringbuf_Data_Peek(&PDU_Queue); + pkt = (struct mstp_pdu_packet *)Ringbuf_Data_Peek(&PDU_Queue); if (pkt) { pkt->data_expecting_reply = npdu_data->data_expecting_reply; for (i = 0; i < pdu_len; i++) { @@ -1284,13 +1272,12 @@ int dlmstp_send_pdu( } /* 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 */ +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 */ +{ /* 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) { @@ -1335,8 +1322,7 @@ 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) { @@ -1348,8 +1334,7 @@ void dlmstp_set_mac_address( return; } -uint8_t dlmstp_mac_address( - void) +uint8_t dlmstp_mac_address(void) { return This_Station; } @@ -1361,8 +1346,7 @@ uint8_t dlmstp_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( - uint8_t max_info_frames) +void dlmstp_set_max_info_frames(uint8_t max_info_frames) { if (max_info_frames >= MSTP_PDU_PACKET_COUNT) { Nmax_info_frames = max_info_frames; @@ -1371,8 +1355,7 @@ void dlmstp_set_max_info_frames( return; } -uint8_t dlmstp_max_info_frames( - void) +uint8_t dlmstp_max_info_frames(void) { return Nmax_info_frames; } @@ -1382,8 +1365,7 @@ uint8_t dlmstp_max_info_frames( /* 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) { @@ -1394,20 +1376,18 @@ void dlmstp_set_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; - my_address->net = 0; /* local only, no routing */ + my_address->net = 0; /* local only, no routing */ my_address->len = 0; for (i = 0; i < MAX_MAC_LEN; i++) { my_address->adr[i] = 0; @@ -1416,16 +1396,15 @@ void dlmstp_get_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/ports/bdk-atxx4-mstp/eeprom.c b/ports/bdk-atxx4-mstp/eeprom.c index 41919d97..9eeef3b9 100644 --- a/ports/bdk-atxx4-mstp/eeprom.c +++ b/ports/bdk-atxx4-mstp/eeprom.c @@ -1,26 +1,26 @@ /************************************************************************** -* -* Copyright (C) 2009 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*********************************************************************/ + * + * Copyright (C) 2009 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + *********************************************************************/ #include #include #include @@ -29,13 +29,12 @@ /* Internal EEPROM of the AVR - http://supp.iar.com/Support/?note=45745 */ - int eeprom_bytes_read( - uint16_t eeaddr, /* EEPROM starting memory address (offset of zero) */ - uint8_t * buf, /* data to store */ + uint16_t eeaddr, /* EEPROM starting memory address (offset of zero) */ + uint8_t *buf, /* data to store */ int len) -{ /* number of bytes of data to read */ - int count = 0; /* return value */ +{ /* number of bytes of data to read */ + int count = 0; /* return value */ while (len) { __EEGET(buf[count], eeaddr); @@ -47,11 +46,10 @@ int eeprom_bytes_read( return count; } -int eeprom_bytes_write( - uint16_t eeaddr, /* EEPROM starting memory address */ - uint8_t * buf, /* data to send */ +int eeprom_bytes_write(uint16_t eeaddr, /* EEPROM starting memory address */ + uint8_t *buf, /* data to send */ int len) -{ /* number of bytes of data */ +{ /* number of bytes of data */ int count = 0; while (len) { diff --git a/ports/bdk-atxx4-mstp/fuses.c b/ports/bdk-atxx4-mstp/fuses.c index fe0ff390..551d988f 100644 --- a/ports/bdk-atxx4-mstp/fuses.c +++ b/ports/bdk-atxx4-mstp/fuses.c @@ -1,27 +1,27 @@ /************************************************************************ -* -* Copyright (C) 2009 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*************************************************************************/ + * + * Copyright (C) 2009 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *************************************************************************/ #include "hardware.h" #if defined(__GNUC__) && (__GNUC__ >= 4) @@ -53,39 +53,39 @@ FUSES = { any fuses listed below are cleared fuses, or are CKSEL or SUT bits that are zero. */ .low = (FUSE_CKSEL3 & FUSE_SUT1), - /* == HIGH FUSE or HFUSE settings == */ - /* BOOTRST: Enable Bootloader Reset Vector */ - /* EESAVE: Enable preserve EEPROM on Chip Erase */ - /* WDTON: Enable watchdog timer always on */ - /* SPIEN: Enable Serial Program and Data Downloading */ - /* JTAGEN: Enable JTAG */ - /* OCDEN: Enable OCD */ - /* BOOTSZ configuration: - BOOTSZ1 BOOTSZ0 Boot Size - ------- ------- --------- - 1 1 512 - 1 0 1024 - 0 1 2048 - 0 0 4096 - */ - /* note: fuses are enabled by clearing the bit, so - any fuses listed below are cleared fuses, - or are BOOTSZ bits that are zero. */ - .high = (FUSE_BOOTSZ1 & FUSE_EESAVE & FUSE_SPIEN & FUSE_JTAGEN), - /* == EXTENDED FUSE or EFUSE settings == */ - /* BODLEVEL configuration - BODLEVEL2 BODLEVEL1 BODLEVEL0 Voltage - --------- --------- --------- -------- - 1 1 1 disabled - 1 1 0 1.8V - 1 0 1 2.7V - 1 0 0 4.3V - */ - /* note: fuses are enabled by clearing the bit, so - any fuses listed below are cleared fuses, - or are BODLEVEL bits that are zero. */ - /* Brown-out detection VCC=4.3V */ - .extended = (FUSE_BODLEVEL1 & FUSE_BODLEVEL0) + /* == HIGH FUSE or HFUSE settings == */ + /* BOOTRST: Enable Bootloader Reset Vector */ + /* EESAVE: Enable preserve EEPROM on Chip Erase */ + /* WDTON: Enable watchdog timer always on */ + /* SPIEN: Enable Serial Program and Data Downloading */ + /* JTAGEN: Enable JTAG */ + /* OCDEN: Enable OCD */ + /* BOOTSZ configuration: + BOOTSZ1 BOOTSZ0 Boot Size + ------- ------- --------- + 1 1 512 + 1 0 1024 + 0 1 2048 + 0 0 4096 + */ + /* note: fuses are enabled by clearing the bit, so + any fuses listed below are cleared fuses, + or are BOOTSZ bits that are zero. */ + .high = (FUSE_BOOTSZ1 & FUSE_EESAVE & FUSE_SPIEN & FUSE_JTAGEN), + /* == EXTENDED FUSE or EFUSE settings == */ + /* BODLEVEL configuration + BODLEVEL2 BODLEVEL1 BODLEVEL0 Voltage + --------- --------- --------- -------- + 1 1 1 disabled + 1 1 0 1.8V + 1 0 1 2.7V + 1 0 0 4.3V + */ + /* note: fuses are enabled by clearing the bit, so + any fuses listed below are cleared fuses, + or are BODLEVEL bits that are zero. */ + /* Brown-out detection VCC=4.3V */ + .extended = (FUSE_BODLEVEL1 & FUSE_BODLEVEL0) }; /* AVR lock bits - unlocked */ diff --git a/ports/bdk-atxx4-mstp/init.c b/ports/bdk-atxx4-mstp/init.c index 18e44918..f5e170a6 100644 --- a/ports/bdk-atxx4-mstp/init.c +++ b/ports/bdk-atxx4-mstp/init.c @@ -1,33 +1,32 @@ /************************************************************************** -* -* Copyright (C) 2009 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2009 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include "hardware.h" /* me */ #include "init.h" -void init( - void) +void init(void) { /* clear the MCU Status Register */ MCUSR = 0; diff --git a/ports/bdk-atxx4-mstp/input.c b/ports/bdk-atxx4-mstp/input.c index bec8a6eb..8c2da722 100644 --- a/ports/bdk-atxx4-mstp/input.c +++ b/ports/bdk-atxx4-mstp/input.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2009 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2009 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include #include "hardware.h" @@ -43,8 +43,7 @@ static struct mstimer Debounce_Timer; #if BDK_V1_HACK /* version 1 BDK workaournd for floating inputs */ -static void input_switch_workaround( - void) +static void input_switch_workaround(void) { /* configure the port pins for the switch - as outputs */ BIT_SET(DDRA, DDA0); @@ -76,8 +75,7 @@ static void input_switch_workaround( #endif /* debounce the inputs */ -void input_task( - void) +void input_task(void) { uint8_t value; static uint8_t old_address = 0; @@ -100,7 +98,7 @@ void input_task( Address_Switch = old_address; } old_address = value; -#if (BDK_VERSION==4) +#if (BDK_VERSION == 4) /* pins used are PB3, PB2, PB1 */ value = BITMASK_CHECK(PINB, 0x0E); value >>= 1; @@ -119,20 +117,17 @@ void input_task( #endif } -uint8_t input_address( - void) +uint8_t input_address(void) { return Address_Switch; } -uint8_t input_rotary_value( - uint8_t index) +uint8_t input_rotary_value(uint8_t index) { return Buttons; } -bool input_button_value( - uint8_t index) +bool input_button_value(uint8_t index) { bool value = false; @@ -159,9 +154,7 @@ bool input_button_value( return value; } - -void input_init( - void) +void input_init(void) { /* configure the port pins for the switch */ BIT_CLEAR(DDRA, DDA0); @@ -180,7 +173,7 @@ void input_init( BIT_SET(PORTA, PORTA5); BIT_SET(PORTA, PORTA6); /* configure the port pins for rotary switch inputs */ -#if (BDK_VERSION==4) +#if (BDK_VERSION == 4) BIT_CLEAR(DDRB, DDB1); BIT_CLEAR(DDRB, DDB2); BIT_CLEAR(DDRB, DDB3); diff --git a/ports/bdk-atxx4-mstp/led.c b/ports/bdk-atxx4-mstp/led.c index 3e7c71ca..7e95b332 100644 --- a/ports/bdk-atxx4-mstp/led.c +++ b/ports/bdk-atxx4-mstp/led.c @@ -1,26 +1,26 @@ /************************************************************************** -* -* Copyright (C) 2009 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*********************************************************************/ + * + * Copyright (C) 2009 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + *********************************************************************/ #include #include "hardware.h" #include "bacnet/basic/sys/mstimer.h" @@ -33,12 +33,11 @@ static struct mstimer Off_Delay_Timer[MAX_LEDS]; /************************************************************************* -* Description: Turn on an LED -* Returns: none -* Notes: none -*************************************************************************/ -void led_on( - uint8_t index) + * Description: Turn on an LED + * Returns: none + * Notes: none + *************************************************************************/ +void led_on(uint8_t index) { switch (index) { case 0: @@ -48,14 +47,14 @@ void led_on( BIT_SET(PORTD, PD6); break; case 2: -#if (BDK_VERSION==4) +#if (BDK_VERSION == 4) BIT_SET(PORTB, PB0); #else BIT_SET(PORTC, PC7); #endif break; case 3: -#if (BDK_VERSION==4) +#if (BDK_VERSION == 4) BIT_SET(PORTB, PB4); #else BIT_SET(PORTC, PC6); @@ -70,12 +69,11 @@ void led_on( } /************************************************************************* -* Description: Turn off an LED -* Returns: none -* Notes: none -*************************************************************************/ -void led_off( - uint8_t index) + * Description: Turn off an LED + * Returns: none + * Notes: none + *************************************************************************/ +void led_off(uint8_t index) { switch (index) { case 0: @@ -85,14 +83,14 @@ void led_off( BIT_CLEAR(PORTD, PD6); break; case 2: -#if (BDK_VERSION==4) +#if (BDK_VERSION == 4) BIT_CLEAR(PORTB, PB0); #else BIT_CLEAR(PORTC, PC7); #endif break; case 3: -#if (BDK_VERSION==4) +#if (BDK_VERSION == 4) BIT_CLEAR(PORTB, PB4); #else BIT_CLEAR(PORTC, PC6); @@ -107,12 +105,11 @@ void led_off( } /************************************************************************* -* Description: Get the state of the LED -* Returns: true if on, false if off. -* Notes: none -*************************************************************************/ -bool led_state( - uint8_t index) + * Description: Get the state of the LED + * Returns: true if on, false if off. + * Notes: none + *************************************************************************/ +bool led_state(uint8_t index) { switch (index) { case 0: @@ -120,13 +117,13 @@ bool led_state( case 1: return (BIT_CHECK(PIND, PIND6)); case 2: -#if (BDK_VERSION==4) +#if (BDK_VERSION == 4) return (BIT_CHECK(PINB, PINC0)); #else return (BIT_CHECK(PINC, PINC7)); #endif case 3: -#if (BDK_VERSION==4) +#if (BDK_VERSION == 4) return (BIT_CHECK(PINB, PINC4)); #else return (BIT_CHECK(PINC, PINC6)); @@ -139,12 +136,11 @@ bool led_state( } /************************************************************************* -* Description: Toggle the state of the setup LED -* Returns: none -* Notes: none -*************************************************************************/ -void led_toggle( - uint8_t index) + * Description: Toggle the state of the setup LED + * Returns: none + * Notes: none + *************************************************************************/ +void led_toggle(uint8_t index) { if (led_state(index)) { led_off(index); @@ -154,13 +150,11 @@ void led_toggle( } /************************************************************************* -* Description: Delay before going off to give minimum brightness. -* Returns: none -* Notes: none -*************************************************************************/ -void led_off_delay( - uint8_t index, - uint32_t delay_ms) + * Description: Delay before going off to give minimum brightness. + * Returns: none + * Notes: none + *************************************************************************/ +void led_off_delay(uint8_t index, uint32_t delay_ms) { if (index < MAX_LEDS) { mstimer_set(&Off_Delay_Timer[index], delay_ms); @@ -168,13 +162,11 @@ void led_off_delay( } /************************************************************************* -* Description: Turn on, and delay before going off. -* Returns: none -* Notes: none -*************************************************************************/ -void led_on_interval( - uint8_t index, - uint16_t interval_ms) + * Description: Turn on, and delay before going off. + * Returns: none + * Notes: none + *************************************************************************/ +void led_on_interval(uint8_t index, uint16_t interval_ms) { if (index < MAX_LEDS) { led_on(index); @@ -183,14 +175,13 @@ void led_on_interval( } /************************************************************************* -* Description: Task for blinking LED -* Returns: none -* Notes: none -*************************************************************************/ -void led_task( - void) + * Description: Task for blinking LED + * Returns: none + * Notes: none + *************************************************************************/ +void led_task(void) { - uint8_t i; /* loop counter */ + uint8_t i; /* loop counter */ for (i = 0; i < MAX_LEDS; i++) { if (mstimer_expired(&Off_Delay_Timer[i])) { @@ -201,19 +192,18 @@ void led_task( } /************************************************************************* -* Description: Initialize the LED hardware -* Returns: none -* Notes: none -*************************************************************************/ -void led_init( - void) + * Description: Initialize the LED hardware + * Returns: none + * Notes: none + *************************************************************************/ +void led_init(void) { - uint8_t i; /* loop counter */ + uint8_t i; /* loop counter */ /* configure the port pins as outputs */ BIT_SET(DDRD, DDD7); BIT_SET(DDRD, DDD6); -#if (BDK_VERSION==4) +#if (BDK_VERSION == 4) BIT_SET(DDRB, DDB0); BIT_SET(DDRB, DDB4); #else diff --git a/ports/bdk-atxx4-mstp/main.c b/ports/bdk-atxx4-mstp/main.c index 52c305b3..a958f499 100644 --- a/ports/bdk-atxx4-mstp/main.c +++ b/ports/bdk-atxx4-mstp/main.c @@ -1,27 +1,27 @@ /************************************************************************ -* -* Copyright (C) 2009 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*************************************************************************/ + * + * Copyright (C) 2009 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *************************************************************************/ #include #include @@ -47,8 +47,7 @@ char *BACnet_Version = BACNET_VERSION_TEXT; /* For porting to IAR, see: http://www.avrfreaks.net/wiki/index.php/Documentation:AVR_GCC/IarToAvrgcc*/ -int main( - void) +int main(void) { init(); /* Configure the watchdog timer - Disabled for debugging */ diff --git a/ports/bdk-atxx4-mstp/mstimer-init.c b/ports/bdk-atxx4-mstp/mstimer-init.c index 87ff92f0..98647888 100644 --- a/ports/bdk-atxx4-mstp/mstimer-init.c +++ b/ports/bdk-atxx4-mstp/mstimer-init.c @@ -1,26 +1,26 @@ /************************************************************************** -* -* Copyright (C) 2009 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*********************************************************************/ + * + * Copyright (C) 2009 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + *********************************************************************/ #include #include #include "hardware.h" @@ -32,8 +32,7 @@ /* Timer2 Prescaling: 1, 8, 32, 64, 128, 256, or 1024 */ #define TIMER_MICROSECONDS 1000UL #define TIMER_TICKS(p) \ - (((((F_CPU)/(p))/1000UL) \ - *(TIMER_MICROSECONDS))/1000UL) + (((((F_CPU) / (p)) / 1000UL) * (TIMER_MICROSECONDS)) / 1000UL) #define TIMER_TICKS_MAX 255UL /* adjust the prescaler for the processor clock */ #if (TIMER_TICKS(1) <= TIMER_TICKS_MAX) @@ -55,18 +54,17 @@ #endif #define TIMER2_TICKS TIMER_TICKS(TIMER2_PRESCALER) /* Timer counts up from count to TIMER_TICKS_MAX and then signals overflow */ -#define TIMER2_COUNT (TIMER_TICKS_MAX-TIMER2_TICKS) +#define TIMER2_COUNT (TIMER_TICKS_MAX - TIMER2_TICKS) /* counter for the the timer which wraps every 49.7 days */ static volatile uint32_t Millisecond_Counter; /************************************************************************* -* Description: Timer Interrupt Handler -* Returns: nothing -* Notes: none -*************************************************************************/ -static inline void timer_interrupt_handler( - void) + * Description: Timer Interrupt Handler + * Returns: nothing + * Notes: none + *************************************************************************/ +static inline void timer_interrupt_handler(void) { /* Set the counter for the next interrupt */ TCNT2 = TIMER2_COUNT; @@ -74,24 +72,23 @@ static inline void timer_interrupt_handler( } /************************************************************************* -* Description: Timer Interrupt Service Routine - Timer Overflowed! -* Returns: none -* Notes: Global interupts must be enabled -*************************************************************************/ + * Description: Timer Interrupt Service Routine - Timer Overflowed! + * Returns: none + * Notes: Global interupts must be enabled + *************************************************************************/ ISR(TIMER2_OVF_vect) { timer_interrupt_handler(); } /************************************************************************* -* Description: returns the current millisecond count -* Returns: none -* Notes: This method only disables the timer overflow interrupt. -*************************************************************************/ -unsigned long mstimer_now( - void) + * Description: returns the current millisecond count + * Returns: none + * Notes: This method only disables the timer overflow interrupt. + *************************************************************************/ +unsigned long mstimer_now(void) { - unsigned long timer_value; /* return value */ + unsigned long timer_value; /* return value */ /* Disable the overflow interrupt. Prevents value corruption that would happen if interrupted */ @@ -104,12 +101,11 @@ unsigned long mstimer_now( } /************************************************************************* -* Description: Initialization for Timer -* Returns: none -* Notes: none -*************************************************************************/ -void mstimer_init( - void) + * Description: Initialization for Timer + * Returns: none + * Notes: none + *************************************************************************/ +void mstimer_init(void) { /* Normal Operation */ TCCR2A = 0; @@ -125,19 +121,19 @@ void mstimer_init( 1 1 0 CLKt2s/256 1 1 1 CLKt2s/1024 */ -#if (TIMER2_PRESCALER==1) +#if (TIMER2_PRESCALER == 1) TCCR2B = _BV(CS20); -#elif (TIMER2_PRESCALER==8) +#elif (TIMER2_PRESCALER == 8) TCCR2B = _BV(CS21); -#elif (TIMER2_PRESCALER==32) +#elif (TIMER2_PRESCALER == 32) TCCR2B = _BV(CS21) | _BV(CS20); -#elif (TIMER2_PRESCALER==64) +#elif (TIMER2_PRESCALER == 64) TCCR2B = _BV(CS22); -#elif (TIMER2_PRESCALER==128) +#elif (TIMER2_PRESCALER == 128) TCCR2B = _BV(CS22) | _BV(CS20); -#elif (TIMER2_PRESCALER==256) +#elif (TIMER2_PRESCALER == 256) TCCR2B = _BV(CS22) | _BV(CS21); -#elif (TIMER2_PRESCALER==1024) +#elif (TIMER2_PRESCALER == 1024) TCCR2B = _BV(CS22) | _BV(CS21) | _BV(CS20); #else #error Timer2 Prescale: Invalid Value diff --git a/ports/bdk-atxx4-mstp/rs485.c b/ports/bdk-atxx4-mstp/rs485.c index 9db99060..e5299ea7 100644 --- a/ports/bdk-atxx4-mstp/rs485.c +++ b/ports/bdk-atxx4-mstp/rs485.c @@ -1,26 +1,26 @@ /************************************************************************** -* -* Copyright (C) 2009 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*********************************************************************/ + * + * Copyright (C) 2009 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + *********************************************************************/ #include #include #include @@ -44,7 +44,7 @@ static uint32_t Baud_Rate = 9600; /* At 76800 baud, 40 bit times would be about 0.520 milliseconds */ /* At 115200 baud, 40 bit times would be about 0.347 milliseconds */ /* 40 bits is 4 octets including a start and stop bit with each octet */ -#define Tturnaround (40UL) +#define Tturnaround (40UL) /* turnaround_time_milliseconds = (Tturnaround*1000UL)/Baud_Rate; */ /* buffer for storing received bytes - size must be power of two */ @@ -54,46 +54,42 @@ static FIFO_BUFFER Receive_Buffer; static struct mstimer Silence_Timer; /**************************************************************************** -* DESCRIPTION: Determines the amount of silence time elapsed -* RETURN: true if the amount of silence time has elapsed -* NOTES: none -*****************************************************************************/ -bool rs485_silence_time_elapsed( - uint16_t milliseconds) + * DESCRIPTION: Determines the amount of silence time elapsed + * RETURN: true if the amount of silence time has elapsed + * NOTES: none + *****************************************************************************/ +bool rs485_silence_time_elapsed(uint16_t milliseconds) { return (mstimer_elapsed(&Silence_Timer) > milliseconds); } /**************************************************************************** -* DESCRIPTION: Resets the silence timer -* RETURN: nothing -* NOTES: none -*****************************************************************************/ -void rs485_silence_time_reset( - void) + * DESCRIPTION: Resets the silence timer + * RETURN: nothing + * NOTES: none + *****************************************************************************/ +void rs485_silence_time_reset(void) { mstimer_set(&Silence_Timer, 0); } /**************************************************************************** -* DESCRIPTION: Configures the RTS output -* RETURN: nothing -* NOTES: none -*****************************************************************************/ -static void rs485_rts_init( - void) + * DESCRIPTION: Configures the RTS output + * RETURN: nothing + * NOTES: none + *****************************************************************************/ +static void rs485_rts_init(void) { /* configure the port pin as an output */ BIT_SET(DDRD, DDD4); } /**************************************************************************** -* DESCRIPTION: enable the transmit-enable line on the RS-485 transceiver -* RETURN: nothing -* NOTES: none -*****************************************************************************/ -void rs485_rts_enable( - bool enable) + * DESCRIPTION: enable the transmit-enable line on the RS-485 transceiver + * RETURN: nothing + * NOTES: none + *****************************************************************************/ +void rs485_rts_enable(bool enable) { if (enable) { BIT_SET(PORTD, PD4); @@ -103,23 +99,21 @@ void rs485_rts_enable( } /**************************************************************************** -* DESCRIPTION: enable the UART receiver and interrupt -* RETURN: nothing -* NOTES: none -*****************************************************************************/ -static void rs485_receiver_enable( - void) + * DESCRIPTION: enable the UART receiver and interrupt + * RETURN: nothing + * NOTES: none + *****************************************************************************/ +static void rs485_receiver_enable(void) { UCSR0B = _BV(TXEN0) | _BV(RXEN0) | _BV(RXCIE0); } /**************************************************************************** -* DESCRIPTION: delay for 40 bit times -* RETURN: nothing -* NOTES: none -*****************************************************************************/ -void rs485_turnaround_delay( - void) + * DESCRIPTION: delay for 40 bit times + * RETURN: nothing + * NOTES: none + *****************************************************************************/ +void rs485_turnaround_delay(void) { uint8_t nbytes = 4; @@ -144,10 +138,10 @@ void rs485_turnaround_delay( } /**************************************************************************** -* DESCRIPTION: Interrupt service routine for UART Receiver -* RETURN: nothing -* NOTES: none -*****************************************************************************/ + * DESCRIPTION: Interrupt service routine for UART Receiver + * RETURN: nothing + * NOTES: none + *****************************************************************************/ ISR(USART0_RX_vect) { uint8_t data_byte; @@ -158,19 +152,18 @@ ISR(USART0_RX_vect) #ifdef MSTP_MONITOR UDR1 = data_byte; #endif - (void) FIFO_Put(&Receive_Buffer, data_byte); + (void)FIFO_Put(&Receive_Buffer, data_byte); } } /**************************************************************************** -* DESCRIPTION: Checks for data on the receive UART, and handles errors -* RETURN: none -* NOTES: none -*****************************************************************************/ -bool rs485_byte_available( - uint8_t * data_register) + * DESCRIPTION: Checks for data on the receive UART, and handles errors + * RETURN: none + * NOTES: none + *****************************************************************************/ +bool rs485_byte_available(uint8_t *data_register) { - bool data_available = false; /* return value */ + bool data_available = false; /* return value */ if (!FIFO_Empty(&Receive_Buffer)) { led_on_interval(LED_4, 1); @@ -184,25 +177,23 @@ bool rs485_byte_available( } /**************************************************************************** -* DESCRIPTION: returns an error indication if errors are enabled -* RETURN: nothing -* NOTES: none -*****************************************************************************/ -bool rs485_receive_error( - void) + * DESCRIPTION: returns an error indication if errors are enabled + * RETURN: nothing + * NOTES: none + *****************************************************************************/ +bool rs485_receive_error(void) { return false; } /**************************************************************************** -* DESCRIPTION: Transmits a frame using the UART -* RETURN: none -* NOTES: none -*****************************************************************************/ -void rs485_bytes_send( - uint8_t * buffer, /* data to send */ + * DESCRIPTION: Transmits a frame using the UART + * RETURN: none + * NOTES: none + *****************************************************************************/ +void rs485_bytes_send(uint8_t *buffer, /* data to send */ uint16_t nbytes) -{ /* number of bytes of data */ +{ /* number of bytes of data */ led_on(LED_5); while (!BIT_CHECK(UCSR0A, UDRE0)) { /* do nothing - wait until Tx buffer is empty */ @@ -233,23 +224,21 @@ void rs485_bytes_send( } /**************************************************************************** -* DESCRIPTION: Returns the baud rate that we are currently running at -* RETURN: baud rate in bps -* NOTES: none -*****************************************************************************/ -uint32_t rs485_baud_rate( - void) + * DESCRIPTION: Returns the baud rate that we are currently running at + * RETURN: baud rate in bps + * NOTES: none + *****************************************************************************/ +uint32_t rs485_baud_rate(void) { return Baud_Rate; } /**************************************************************************** -* DESCRIPTION: configure the UART baud rate -* RETURN: nothing -* NOTES: none -*****************************************************************************/ -static void rs485_baud_rate_configure( - void) + * DESCRIPTION: configure the UART baud rate + * RETURN: nothing + * NOTES: none + *****************************************************************************/ +static void rs485_baud_rate_configure(void) { /* 2x speed mode */ BIT_SET(UCSR0A, U2X0); @@ -258,12 +247,11 @@ static void rs485_baud_rate_configure( } /**************************************************************************** -* DESCRIPTION: set the UART baud rate to a standard value -* RETURN: true if the baud rate is valid -* NOTES: none -*****************************************************************************/ -bool rs485_baud_rate_set( - uint32_t baud) + * DESCRIPTION: set the UART baud rate to a standard value + * RETURN: true if the baud rate is valid + * NOTES: none + *****************************************************************************/ +bool rs485_baud_rate_set(uint32_t baud) { bool valid = true; uint8_t baud_k = 0; @@ -290,12 +278,11 @@ bool rs485_baud_rate_set( } /**************************************************************************** -* DESCRIPTION: initialize the hardware UART -* RETURN: nothing -* NOTES: none -*****************************************************************************/ -static void rs485_usart_init( - void) + * DESCRIPTION: initialize the hardware UART + * RETURN: nothing + * NOTES: none + *****************************************************************************/ +static void rs485_usart_init(void) { /* enable the internal pullup on RXD0 */ BIT_CLEAR(DDRD, DDD0); @@ -314,12 +301,11 @@ static void rs485_usart_init( } /**************************************************************************** -* DESCRIPTION: read any non-volatile data -* RETURN: nothing -* NOTES: none -*****************************************************************************/ -static void rs485_init_nvdata( - void) + * DESCRIPTION: read any non-volatile data + * RETURN: nothing + * NOTES: none + *****************************************************************************/ +static void rs485_init_nvdata(void) { uint8_t baud_k = 9; /* from EEPROM value */ @@ -354,15 +340,14 @@ static void rs485_init_nvdata( } /**************************************************************************** -* DESCRIPTION: initialize the module -* RETURN: nothing -* NOTES: none -*****************************************************************************/ -void rs485_init( - void) + * DESCRIPTION: initialize the module + * RETURN: nothing + * NOTES: none + *****************************************************************************/ +void rs485_init(void) { FIFO_Init(&Receive_Buffer, &Receive_Buffer_Data[0], - (unsigned) sizeof(Receive_Buffer_Data)); + (unsigned)sizeof(Receive_Buffer_Data)); rs485_silence_time_reset(); rs485_rts_init(); rs485_usart_init(); diff --git a/ports/bdk-atxx4-mstp/seeprom.c b/ports/bdk-atxx4-mstp/seeprom.c index d5cab89a..a1de8b7e 100644 --- a/ports/bdk-atxx4-mstp/seeprom.c +++ b/ports/bdk-atxx4-mstp/seeprom.c @@ -1,28 +1,28 @@ /************************************************************************** -* -* Copyright (C) 2009 Steve Karg -* Used algorithm and code from Joerg Wunsch and Ruwan Jayanetti. -* http://www.nongnu.org/avr-libc/user-manual/group__twi__demo.html -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*********************************************************************/ + * + * Copyright (C) 2009 Steve Karg + * Used algorithm and code from Joerg Wunsch and Ruwan Jayanetti. + * http://www.nongnu.org/avr-libc/user-manual/group__twi__demo.html + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + *********************************************************************/ #include #include #include @@ -57,7 +57,8 @@ #endif /* The lower 3 bits of TWSR are reserved on the ATmega163 */ -#define TW_STATUS_MASK (_BV(TWS7)|_BV(TWS6)|_BV(TWS5)|_BV(TWS4)|_BV(TWS3)) +#define TW_STATUS_MASK \ + (_BV(TWS7) | _BV(TWS6) | _BV(TWS5) | _BV(TWS4) | _BV(TWS3)) /* start condition transmitted */ #define TW_START 0x08 /* repeated start condition transmitted */ @@ -93,21 +94,20 @@ /* Number of iterations is the max amount to wait for write cycle to complete a full page write */ /* .005s/.000025=200 */ -#define MAX_ITER (((SEEPROM_I2C_CLOCK/1000)/10)*SEEPROM_WRITE_CYCLE) +#define MAX_ITER (((SEEPROM_I2C_CLOCK / 1000) / 10) * SEEPROM_WRITE_CYCLE) /************************************************************************* -* DESCRIPTION: Return bytes from SEEPROM memory at address -* RETURN: number of bytes read, or -1 on error -* NOTES: none -**************************************************************************/ -int seeprom_bytes_read( - uint16_t eeaddr, /* SEEPROM starting memory address */ - uint8_t * buf, /* data to store */ + * DESCRIPTION: Return bytes from SEEPROM memory at address + * RETURN: number of bytes read, or -1 on error + * NOTES: none + **************************************************************************/ +int seeprom_bytes_read(uint16_t eeaddr, /* SEEPROM starting memory address */ + uint8_t *buf, /* data to store */ int len) -{ /* number of bytes of data to read */ +{ /* number of bytes of data to read */ uint8_t sla, twcr, n = 0; int rv = 0; - uint8_t twst; /* status - only valid while TWINT is set. */ + uint8_t twst; /* status - only valid while TWINT is set. */ uint16_t timeout = 0xFFFF; #if SEEPROM_WORD_ADDRESS_16BIT @@ -118,11 +118,11 @@ int seeprom_bytes_read( sla = SEEPROM_I2C_ADDRESS | (((eeaddr >> 8) & 0x07) << 1); #endif /* First cycle: master transmitter mode */ - restart: +restart: if (n++ >= MAX_ITER) { return -1; } - begin: +begin: /* send start condition */ TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN); /* wait for transmission */ @@ -159,7 +159,8 @@ int seeprom_bytes_read( /* clear interrupt to start transmission */ TWCR = _BV(TWINT) | _BV(TWEN); /* wait for transmission */ - while ((TWCR & _BV(TWINT)) == 0); + while ((TWCR & _BV(TWINT)) == 0) + ; twst = TWSR & TW_STATUS_MASK; switch (twst) { case TW_MT_SLA_ACK: @@ -184,7 +185,8 @@ int seeprom_bytes_read( /* clear interrupt to start transmission */ TWCR = _BV(TWINT) | _BV(TWEN); /* wait for transmission */ - while ((TWCR & _BV(TWINT)) == 0); + while ((TWCR & _BV(TWINT)) == 0) + ; twst = TWSR & TW_STATUS_MASK; switch (twst) { case TW_MT_DATA_ACK: @@ -203,7 +205,8 @@ int seeprom_bytes_read( /* clear interrupt to start transmission */ TWCR = _BV(TWINT) | _BV(TWEN); /* wait for transmission */ - while ((TWCR & _BV(TWINT)) == 0); + while ((TWCR & _BV(TWINT)) == 0) + ; twst = TWSR & TW_STATUS_MASK; switch (twst) { case TW_MT_DATA_ACK: @@ -229,7 +232,8 @@ int seeprom_bytes_read( /* send repeated start condition */ TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN); /* wait for transmission */ - while ((TWCR & _BV(TWINT)) == 0); + while ((TWCR & _BV(TWINT)) == 0) + ; twst = TWSR & TW_STATUS_MASK; switch (twst) { case TW_START: @@ -247,7 +251,8 @@ int seeprom_bytes_read( /* clear interrupt to start transmission */ TWCR = _BV(TWINT) | _BV(TWEN); /* wait for transmission */ - while ((TWCR & _BV(TWINT)) == 0); + while ((TWCR & _BV(TWINT)) == 0) + ; twst = TWSR & TW_STATUS_MASK; switch (twst) { case TW_MR_SLA_ACK: @@ -272,7 +277,8 @@ int seeprom_bytes_read( /* clear int to start transmission */ TWCR = twcr; /* wait for transmission */ - while ((TWCR & _BV(TWINT)) == 0); + while ((TWCR & _BV(TWINT)) == 0) + ; twst = TWSR & TW_STATUS_MASK; switch (twst) { case TW_MR_DATA_NACK: @@ -288,32 +294,32 @@ int seeprom_bytes_read( goto error; } } - quit: +quit: /* Except in the case of lost arbitration, all bus transactions must properly be terminated by the master initiating a stop condition. */ /* send stop condition */ TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN); return rv; - error: +error: rv = -1; goto quit; } /************************************************************************* -* DESCRIPTION: Write some data and wait until it is sent -* RETURN: number of bytes written, or -1 on error -* NOTES: only writes from offset to end of page. -**************************************************************************/ + * DESCRIPTION: Write some data and wait until it is sent + * RETURN: number of bytes written, or -1 on error + * NOTES: only writes from offset to end of page. + **************************************************************************/ static int seeprom_bytes_write_page( - uint16_t eeaddr, /* SEEPROM starting memory address */ - uint8_t * buf, /* data to send */ + uint16_t eeaddr, /* SEEPROM starting memory address */ + uint8_t *buf, /* data to send */ int len) -{ /* number of bytes of data */ +{ /* number of bytes of data */ uint8_t sla, n = 0; int rv = 0; uint16_t endaddr; - uint8_t twst; /* status - only valid while TWINT is set. */ + uint8_t twst; /* status - only valid while TWINT is set. */ uint16_t page_end_addr; uint16_t timeout = 0xFFFF; @@ -330,11 +336,11 @@ static int seeprom_bytes_write_page( /* patch high bits of EEPROM address into SLA */ sla = SEEPROM_I2C_ADDRESS | (((eeaddr >> 8) & 0x07) << 1); #endif - restart: +restart: if (n++ >= MAX_ITER) { return -1; } - begin: +begin: /* Writing to the EEPROM device is simpler than reading, since only a master transmitter mode transfer is needed. Note that the first packet after the SLA+W selection is @@ -373,7 +379,8 @@ static int seeprom_bytes_write_page( /* clear interrupt to start transmission */ TWCR = _BV(TWINT) | _BV(TWEN); /* wait for transmission */ - while ((TWCR & _BV(TWINT)) == 0); + while ((TWCR & _BV(TWINT)) == 0) + ; twst = TWSR & TW_STATUS_MASK; switch (twst) { case TW_MT_SLA_ACK: @@ -394,7 +401,8 @@ static int seeprom_bytes_write_page( /* clear interrupt to start transmission */ TWCR = _BV(TWINT) | _BV(TWEN); /* wait for transmission */ - while ((TWCR & _BV(TWINT)) == 0); + while ((TWCR & _BV(TWINT)) == 0) + ; twst = TWSR & TW_STATUS_MASK; switch (twst) { case TW_MT_DATA_ACK: @@ -413,8 +421,7 @@ static int seeprom_bytes_write_page( /* clear interrupt to start transmission */ TWCR = _BV(TWINT) | _BV(TWEN); /* wait for transmission */ - while ((TWCR & _BV(TWINT)) == 0) { - }; + while ((TWCR & _BV(TWINT)) == 0) { }; twst = TWSR & TW_STATUS_MASK; switch (twst) { case TW_MT_DATA_ACK: @@ -432,7 +439,8 @@ static int seeprom_bytes_write_page( /* start transmission */ TWCR = _BV(TWINT) | _BV(TWEN); /* wait for transmission */ - while ((TWCR & _BV(TWINT)) == 0); + while ((TWCR & _BV(TWINT)) == 0) + ; twst = TWSR & TW_STATUS_MASK; switch (twst) { case TW_MT_DATA_NACK: @@ -446,36 +454,35 @@ static int seeprom_bytes_write_page( goto error; } } - quit: +quit: /* send stop condition */ TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN); return rv; - error: +error: rv = -1; goto quit; } /************************************************************************* -* DESCRIPTION: Write some data and wait until it is sent -* RETURN: number of bytes written, or -1 on error -* NOTES: -* When the word address, internally generated, -* reaches the page boundary, the following -* byte is placed at the beginning of the same -* page. If more than 64 data words are -* transmitted to the EEPROM, the data word -* address will "roll over" and previous data will be -* overwritten. The address "roll over" during write -* is from the last byte of the current page to the -* first byte of the same page. -**************************************************************************/ -int seeprom_bytes_write( - uint16_t off, /* SEEPROM starting memory address */ - uint8_t * buf, /* data to send */ + * DESCRIPTION: Write some data and wait until it is sent + * RETURN: number of bytes written, or -1 on error + * NOTES: + * When the word address, internally generated, + * reaches the page boundary, the following + * byte is placed at the beginning of the same + * page. If more than 64 data words are + * transmitted to the EEPROM, the data word + * address will "roll over" and previous data will be + * overwritten. The address "roll over" during write + * is from the last byte of the current page to the + * first byte of the same page. + **************************************************************************/ +int seeprom_bytes_write(uint16_t off, /* SEEPROM starting memory address */ + uint8_t *buf, /* data to send */ int len) -{ /* number of bytes of data */ +{ /* number of bytes of data */ int status = 0; int rv = 0; @@ -497,12 +504,11 @@ int seeprom_bytes_write( } /************************************************************************* -* Description: Initialize the SEEPROM TWI connection -* Returns: none -* Notes: none -**************************************************************************/ -void seeprom_init( - void) + * Description: Initialize the SEEPROM TWI connection + * Returns: none + * Notes: none + **************************************************************************/ +void seeprom_init(void) { /* bit rate prescaler */ TWSR = 0; diff --git a/ports/bdk-atxx4-mstp/serial.c b/ports/bdk-atxx4-mstp/serial.c index 4836c986..ec0d8da9 100644 --- a/ports/bdk-atxx4-mstp/serial.c +++ b/ports/bdk-atxx4-mstp/serial.c @@ -1,26 +1,26 @@ /************************************************************************** -* -* Copyright (C) 2009 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*********************************************************************/ + * + * Copyright (C) 2009 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + *********************************************************************/ #include #include #include @@ -35,8 +35,7 @@ static uint32_t Baud_Rate = 9600; static uint8_t Receive_Buffer_Data[128]; static FIFO_BUFFER Receive_Buffer; -static void serial_receiver_enable( - void) +static void serial_receiver_enable(void) { UCSR1B = _BV(TXEN1) | _BV(RXEN1) | _BV(RXCIE1); } @@ -48,14 +47,13 @@ ISR(USART1_RX_vect) if (BIT_CHECK(UCSR1A, RXC1)) { /* data is available */ data_byte = UDR1; - (void) FIFO_Put(&Receive_Buffer, data_byte); + (void)FIFO_Put(&Receive_Buffer, data_byte); } } -bool serial_byte_get( - uint8_t * data_register) +bool serial_byte_get(uint8_t *data_register) { - bool data_available = false; /* return value */ + bool data_available = false; /* return value */ if (!FIFO_Empty(&Receive_Buffer)) { *data_register = FIFO_Get(&Receive_Buffer); @@ -65,10 +63,9 @@ bool serial_byte_get( return data_available; } -bool serial_byte_peek( - uint8_t * data_register) +bool serial_byte_peek(uint8_t *data_register) { - bool data_available = false; /* return value */ + bool data_available = false; /* return value */ if (!FIFO_Empty(&Receive_Buffer)) { *data_register = FIFO_Peek(&Receive_Buffer); @@ -78,10 +75,9 @@ bool serial_byte_peek( return data_available; } -void serial_bytes_send( - uint8_t * buffer, /* data to send */ +void serial_bytes_send(uint8_t *buffer, /* data to send */ uint16_t nbytes) -{ /* number of bytes of data */ +{ /* number of bytes of data */ while (!BIT_CHECK(UCSR1A, UDRE1)) { /* do nothing - wait until Tx buffer is empty */ } @@ -105,8 +101,7 @@ void serial_bytes_send( return; } -void serial_byte_send( - uint8_t ch) +void serial_byte_send(uint8_t ch) { while (!BIT_CHECK(UCSR1A, UDRE1)) { /* do nothing - wait until Tx buffer is empty */ @@ -117,8 +112,7 @@ void serial_byte_send( return; } -void serial_byte_transmit_complete( - void) +void serial_byte_transmit_complete(void) { /* was the frame sent? */ while (!BIT_CHECK(UCSR1A, TXC1)) { @@ -129,14 +123,12 @@ void serial_byte_transmit_complete( BIT_SET(UCSR1A, TXC1); } -uint32_t serial_baud_rate( - void) +uint32_t serial_baud_rate(void) { return Baud_Rate; } -bool serial_baud_rate_set( - uint32_t baud) +bool serial_baud_rate_set(uint32_t baud) { bool valid = true; @@ -162,8 +154,7 @@ bool serial_baud_rate_set( return valid; } -static void serial_usart_init( - void) +static void serial_usart_init(void) { /* enable the internal pullup on RXD1 */ BIT_CLEAR(DDRD, DDD2); @@ -181,11 +172,10 @@ static void serial_usart_init( power_usart1_enable(); } -void serial_init( - void) +void serial_init(void) { FIFO_Init(&Receive_Buffer, &Receive_Buffer_Data[0], - (unsigned) sizeof(Receive_Buffer_Data)); + (unsigned)sizeof(Receive_Buffer_Data)); serial_usart_init(); serial_baud_rate_set(Baud_Rate); serial_receiver_enable(); diff --git a/ports/bdk-atxx4-mstp/stack.c b/ports/bdk-atxx4-mstp/stack.c index baf0b391..bf347024 100644 --- a/ports/bdk-atxx4-mstp/stack.c +++ b/ports/bdk-atxx4-mstp/stack.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2009 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2009 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include "hardware.h" /* me */ #include "stack.h" @@ -32,11 +32,9 @@ extern uint8_t _end; extern uint8_t __stack; #define STACK_CANARY (0xC5) -void stack_init( - void) __attribute__ ((naked)) __attribute__ ((section(".init1"))); +void stack_init(void) __attribute__((naked)) __attribute__((section(".init1"))); -void stack_init( - void) +void stack_init(void) { #if 0 uint8_t *p = &_end; @@ -46,28 +44,32 @@ void stack_init( p++; } #else - __asm volatile ( - " ldi r30,lo8(_end)\n" " ldi r31,hi8(_end)\n" " ldi r24,lo8(0xc5)\n" /* STACK_CANARY = 0xc5 */ - " ldi r25,hi8(__stack)\n" " rjmp .cmp\n" ".loop:\n" - " st Z+,r24\n" ".cmp:\n" " cpi r30,lo8(__stack)\n" - " cpc r31,r25\n" " brlo .loop\n" " breq .loop"::); + __asm volatile(" ldi r30,lo8(_end)\n" + " ldi r31,hi8(_end)\n" + " ldi r24,lo8(0xc5)\n" /* STACK_CANARY = 0xc5 */ + " ldi r25,hi8(__stack)\n" + " rjmp .cmp\n" + ".loop:\n" + " st Z+,r24\n" + ".cmp:\n" + " cpi r30,lo8(__stack)\n" + " cpc r31,r25\n" + " brlo .loop\n" + " breq .loop" ::); #endif } -unsigned stack_size( - void) +unsigned stack_size(void) { return (&__stack) - (&_end); } -uint8_t stack_byte( - unsigned offset) +uint8_t stack_byte(unsigned offset) { return *(&_end + offset); } -unsigned stack_unused( - void) +unsigned stack_unused(void) { uint8_t *p = &_end; unsigned count = 0; @@ -82,26 +84,21 @@ unsigned stack_unused( return count; } #else -void stack_init( - void) +void stack_init(void) { - } -unsigned stack_size( - void) +unsigned stack_size(void) { return 0; } -uint8_t stack_byte( - unsigned offset) +uint8_t stack_byte(unsigned offset) { return 0; } -unsigned stack_unused( - void) +unsigned stack_unused(void) { return 0; } diff --git a/ports/bdk-atxx4-mstp/test.c b/ports/bdk-atxx4-mstp/test.c index fa7e4dd6..0001feef 100644 --- a/ports/bdk-atxx4-mstp/test.c +++ b/ports/bdk-atxx4-mstp/test.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2010 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2010 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include #include @@ -42,26 +42,24 @@ #endif #define BINARY_STRING_MAX 3 -const char * const binary_string[BINARY_STRING_MAX] = { - "INACTIVE", "ACTIVE", "RELINQUISH" -}; +const char *const binary_string[BINARY_STRING_MAX] = { "INACTIVE", "ACTIVE", + "RELINQUISH" }; /* timer for test task */ static struct mstimer Test_Timer; /* MAC Address of MS/TP */ static uint8_t MSTP_MAC_Address; -void test_init( - void) +void test_init(void) { #ifdef MSTP_MONITOR serial_baud_rate_set(115200); #else serial_baud_rate_set(9600); #endif - mstimer_set(&Test_Timer, 1*1000); + mstimer_set(&Test_Timer, 1 * 1000); /* configure a port pin as output */ -#if (BDK_VERSION==4) +#if (BDK_VERSION == 4) BIT_SET(DDRD, DDB5); #else BIT_SET(DDRB, DDB0); @@ -69,10 +67,10 @@ void test_init( } /** -* @brief send a string of text to the RS-232 port -* @param text - C string of text (null terminated) -*/ -static void test_write_string(const char * text) + * @brief send a string of text to the RS-232 port + * @param text - C string of text (null terminated) + */ +static void test_write_string(const char *text) { size_t len = 0; @@ -82,16 +80,14 @@ static void test_write_string(const char * text) } } - /************************************************************************* -* Description: Turn on a pin -* Returns: none -* Notes: none -*************************************************************************/ -static inline void test_pin_on( - void) + * Description: Turn on a pin + * Returns: none + * Notes: none + *************************************************************************/ +static inline void test_pin_on(void) { -#if (BDK_VERSION==4) +#if (BDK_VERSION == 4) BIT_SET(PORTD, PD5); #else BIT_SET(PORTB, PB0); @@ -99,14 +95,13 @@ static inline void test_pin_on( } /************************************************************************* -* Description: Turn off a pin -* Returns: none -* Notes: none -*************************************************************************/ -static inline void test_pin_off( - void) + * Description: Turn off a pin + * Returns: none + * Notes: none + *************************************************************************/ +static inline void test_pin_off(void) { -#if (BDK_VERSION==4) +#if (BDK_VERSION == 4) BIT_CLEAR(PORTD, PD5); #else BIT_CLEAR(PORTB, PB0); @@ -114,14 +109,13 @@ static inline void test_pin_off( } /************************************************************************* -* Description: Get the state of the test pin -* Returns: true if on, false if off. -* Notes: none -*************************************************************************/ -static inline bool test_pin_state( - void) + * Description: Get the state of the test pin + * Returns: true if on, false if off. + * Notes: none + *************************************************************************/ +static inline bool test_pin_state(void) { -#if (BDK_VERSION==4) +#if (BDK_VERSION == 4) return (BIT_CHECK(PIND, PD5)); #else return (BIT_CHECK(PINB, PB0)); @@ -129,12 +123,11 @@ static inline bool test_pin_state( } /************************************************************************* -* Description: Toggle the test pin -* Returns: none -* Notes: none -*************************************************************************/ -static inline void test_pin_toggle( - void) + * Description: Toggle the test pin + * Returns: none + * Notes: none + *************************************************************************/ +static inline void test_pin_toggle(void) { if (test_pin_state()) { test_pin_off(); @@ -144,8 +137,7 @@ static inline void test_pin_toggle( } #ifdef MSTP_MONITOR -void test_task( - void) +void test_task(void) { if (mstimer_expired(&Test_Timer)) { mstimer_reset(&Test_Timer); @@ -155,8 +147,7 @@ void test_task( #else char Send_Buffer[32]; -void test_task( - void) +void test_task(void) { uint8_t data_register = 0; uint16_t id = 0; @@ -172,7 +163,7 @@ void test_task( Send_Buffer[12] = (MSTP_MAC_Address & BIT(4)) ? '1' : '0'; Send_Buffer[13] = (MSTP_MAC_Address & BIT(5)) ? '1' : '0'; Send_Buffer[14] = (MSTP_MAC_Address & BIT(6)) ? '1' : '0'; - serial_bytes_send((uint8_t *) Send_Buffer, 17); + serial_bytes_send((uint8_t *)Send_Buffer, 17); } if (serial_byte_get(&data_register)) { /* echo the character */ @@ -206,22 +197,19 @@ void test_task( rs485_baud_rate_set(9600); break; case 'e': - seeprom_bytes_read(NV_SEEPROM_TYPE_0, (uint8_t *) & id, 2); + seeprom_bytes_read(NV_SEEPROM_TYPE_0, (uint8_t *)&id, 2); sprintf(Send_Buffer, "\r\n%04X", id); - serial_bytes_send((uint8_t *) Send_Buffer, - strlen(Send_Buffer)); + serial_bytes_send((uint8_t *)Send_Buffer, strlen(Send_Buffer)); break; case 'b': sprintf(Send_Buffer, "\r\n%lubps", - (unsigned long) rs485_baud_rate()); - serial_bytes_send((uint8_t *) Send_Buffer, - strlen(Send_Buffer)); + (unsigned long)rs485_baud_rate()); + serial_bytes_send((uint8_t *)Send_Buffer, strlen(Send_Buffer)); break; case 'm': - sprintf(Send_Buffer, "\r\nMax:%u", - (unsigned) dlmstp_max_master()); - serial_bytes_send((uint8_t *) Send_Buffer, - strlen(Send_Buffer)); + sprintf( + Send_Buffer, "\r\nMax:%u", (unsigned)dlmstp_max_master()); + serial_bytes_send((uint8_t *)Send_Buffer, strlen(Send_Buffer)); break; default: break; diff --git a/ports/bdk-atxx4-mstp/watchdog.c b/ports/bdk-atxx4-mstp/watchdog.c index 62eeb9f8..7e425306 100644 --- a/ports/bdk-atxx4-mstp/watchdog.c +++ b/ports/bdk-atxx4-mstp/watchdog.c @@ -1,32 +1,31 @@ /************************************************************************** -* -* Copyright (C) 2009 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*********************************************************************/ + * + * Copyright (C) 2009 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + *********************************************************************/ #include "hardware.h" #include "watchdog.h" #if !defined(__GNUC__) -static inline void wdt_enable( - int value) +static inline void wdt_enable(int value) { __disable_interrupt(); __watchdog_reset(); @@ -38,8 +37,7 @@ static inline void wdt_enable( __enable_interrupt(); */ } -static inline void wdt_disable( - void) +static inline void wdt_disable(void) { __disable_interrupt(); __watchdog_reset(); @@ -53,31 +51,28 @@ static inline void wdt_disable( __enable_interrupt(); } -static inline void wdt_reset( - void) +static inline void wdt_reset(void) { __watchdog_reset(); } #endif /************************************************************************* -* Description: Reset the watchdog timer -* Returns: none -* Notes: none -**************************************************************************/ -void watchdog_reset( - void) + * Description: Reset the watchdog timer + * Returns: none + * Notes: none + **************************************************************************/ +void watchdog_reset(void) { wdt_reset(); } /************************************************************************* -* Description: Initialize the watchdog timer -* Returns: none -* Notes: none -**************************************************************************/ -void watchdog_init( - unsigned milliseconds) + * Description: Initialize the watchdog timer + * Returns: none + * Notes: none + **************************************************************************/ +void watchdog_init(unsigned milliseconds) { unsigned value = WDTO_15MS; if (milliseconds) { diff --git a/ports/bsd/bip-init.c b/ports/bsd/bip-init.c index 863d95be..53cdf969 100644 --- a/ports/bsd/bip-init.c +++ b/ports/bsd/bip-init.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 "bacnet/bacdcode.h" #include "bacnet/bacint.h" @@ -68,8 +68,10 @@ static bool BIP_Debug = false; * @param str - debug info string * @param addr - IPv4 address */ -static void debug_print_ipv4(const char *str, const struct in_addr *addr, - const unsigned int port, const unsigned int count) +static void debug_print_ipv4(const char *str, + const struct in_addr *addr, + const unsigned int port, + const unsigned int count) { if (BIP_Debug) { fprintf(stderr, "BIP: %s %s:%hu (%u bytes)\n", str, inet_ntoa(*addr), @@ -233,7 +235,7 @@ uint8_t bip_get_subnet_prefix(void) if (test_broadcast == broadcast) { break; } - mask = mask<<1; + mask = mask << 1; } return prefix; @@ -267,8 +269,8 @@ int bip_send_mpdu(BACNET_IP_ADDRESS *dest, uint8_t *mtu, uint16_t mtu_len) memcpy(&bip_dest.sin_addr.s_addr, &dest->address[0], 4); bip_dest.sin_port = htons(dest->port); /* Send the packet */ - debug_print_ipv4("Sending MPDU->", &bip_dest.sin_addr, bip_dest.sin_port, - mtu_len); + debug_print_ipv4( + "Sending MPDU->", &bip_dest.sin_addr, bip_dest.sin_port, mtu_len); return sendto(BIP_Socket, (char *)mtu, mtu_len, 0, (struct sockaddr *)&bip_dest, sizeof(struct sockaddr)); } @@ -341,14 +343,14 @@ uint16_t bip_receive( */ memcpy(&addr.address[0], &sin.sin_addr.s_addr, 4); addr.port = ntohs(sin.sin_port); - debug_print_ipv4("Received MPDU->", &sin.sin_addr, sin.sin_port, - received_bytes); + debug_print_ipv4( + "Received MPDU->", &sin.sin_addr, sin.sin_port, received_bytes); /* pass the packet into the BBMD handler */ offset = bvlc_handler(&addr, src, npdu, received_bytes); if (offset > 0) { npdu_len = received_bytes - offset; - debug_print_ipv4("Received NPDU->", &sin.sin_addr, sin.sin_port, - npdu_len); + debug_print_ipv4( + "Received NPDU->", &sin.sin_addr, sin.sin_port, npdu_len); if (npdu_len <= max_npdu) { /* shift the buffer to return a valid NPDU */ for (i = 0; i < npdu_len; i++) { @@ -414,14 +416,13 @@ bool bip_get_addr_by_name(const char *host_name, BACNET_IP_ADDRESS *addr) return true; } -static void *get_addr_ptr( - struct sockaddr *sockaddr_ptr) +static void *get_addr_ptr(struct sockaddr *sockaddr_ptr) { void *addr_ptr; if (sockaddr_ptr->sa_family == AF_INET) { - addr_ptr = &((struct sockaddr_in *) sockaddr_ptr)->sin_addr; + addr_ptr = &((struct sockaddr_in *)sockaddr_ptr)->sin_addr; } else if (sockaddr_ptr->sa_family == AF_INET6) { - addr_ptr = &((struct sockaddr_in6 *) sockaddr_ptr)->sin6_addr; + addr_ptr = &((struct sockaddr_in6 *)sockaddr_ptr)->sin6_addr; } return addr_ptr; } @@ -434,20 +435,16 @@ static void *get_addr_ptr( * @param addr [out] The netmask addr, broadcast addr, ip addr. * @param request [in] addr broadaddr netmask */ -static int get_local_address( - char *ifname, - struct in_addr *addr, - char *request) +static int get_local_address(char *ifname, struct in_addr *addr, char *request) { - - char rv; /* return value */ + char rv; /* return value */ struct ifaddrs *ifaddrs_ptr; int status; status = getifaddrs(&ifaddrs_ptr); if (status == -1) { - fprintf(stderr, "Error in 'getifaddrs': %d (%s)\n", errno, - strerror(errno)); + fprintf( + stderr, "Error in 'getifaddrs': %d (%s)\n", errno, strerror(errno)); } while (ifaddrs_ptr) { if ((ifaddrs_ptr->ifa_addr->sa_family == AF_INET) && @@ -477,11 +474,10 @@ static int get_local_address( * @param netmask [out] The netmask, in host order. * @return 0 on success, else the error from the getifaddrs() call. */ -int bip_get_local_netmask( - struct in_addr *netmask) +int bip_get_local_netmask(struct in_addr *netmask) { int rv; - char *ifname = getenv("BACNET_IFACE"); /* will probably be null */ + char *ifname = getenv("BACNET_IFACE"); /* will probably be null */ if (ifname == NULL) ifname = "en0"; printf("ifname %s", ifname); @@ -497,8 +493,7 @@ int bip_get_local_netmask( * @param ifname [in] The named interface to use for the network layer. * Eg, for MAC OS X, ifname is en0, en1, and others. */ -void bip_set_interface( - char *ifname) +void bip_set_interface(char *ifname) { struct in_addr local_address; struct in_addr broadcast_address; @@ -550,10 +545,9 @@ void bip_set_interface( * @return True if the socket is successfully opened for BACnet/IP, * else False if the socket functions fail. */ -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; @@ -572,18 +566,16 @@ bool bip_init( /* Allow us to use the same socket for sending and receiving */ /* This makes sure that the src port is correct when sending */ sockopt = 1; - status = - setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &sockopt, - sizeof(sockopt)); + status = setsockopt( + sock_fd, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(sockopt)); if (status < 0) { close(sock_fd); BIP_Socket = -1; return status; } /* allow us to send a broadcast */ - status = - setsockopt(sock_fd, SOL_SOCKET, SO_BROADCAST, &sockopt, - sizeof(sockopt)); + status = setsockopt( + sock_fd, SOL_SOCKET, SO_BROADCAST, &sockopt, sizeof(sockopt)); if (status < 0) { close(sock_fd); BIP_Socket = -1; @@ -595,7 +587,7 @@ bool bip_init( sin.sin_port = BIP_Port; memset(&(sin.sin_zero), '\0', sizeof(sin.sin_zero)); status = - bind(sock_fd, (const struct sockaddr *) &sin, sizeof(struct sockaddr)); + bind(sock_fd, (const struct sockaddr *)&sin, sizeof(struct sockaddr)); if (status < 0) { close(sock_fd); BIP_Socket = -1; @@ -617,8 +609,7 @@ bool bip_valid(void) /** Cleanup and close out the BACnet/IP services by closing the socket. * @ingroup DLBIP */ -void bip_cleanup( - void) +void bip_cleanup(void) { int sock_fd = 0; diff --git a/ports/bsd/datetime-init.c b/ports/bsd/datetime-init.c index 65d7c4bd..705302a8 100644 --- a/ports/bsd/datetime-init.c +++ b/ports/bsd/datetime-init.c @@ -27,11 +27,10 @@ * @param true if DST is enabled and active * @return true if local time was retrieved */ -bool datetime_local( - BACNET_DATE * bdate, - BACNET_TIME * btime, - int16_t * utc_offset_minutes, - bool * dst_active) +bool datetime_local(BACNET_DATE *bdate, + BACNET_TIME *btime, + int16_t *utc_offset_minutes, + bool *dst_active) { bool status = false; struct tm *tblock = NULL; @@ -54,11 +53,10 @@ bool datetime_local( * int tm_isdst Daylight Savings flag. */ datetime_set_date(bdate, (uint16_t)tblock->tm_year + 1900, - (uint8_t)tblock->tm_mon + 1, - (uint8_t)tblock->tm_mday); + (uint8_t)tblock->tm_mon + 1, (uint8_t)tblock->tm_mday); datetime_set_time(btime, (uint8_t)tblock->tm_hour, - (uint8_t)tblock->tm_min, (uint8_t)tblock->tm_sec, - (uint8_t)(tv.tv_usec / 10000)); + (uint8_t)tblock->tm_min, (uint8_t)tblock->tm_sec, + (uint8_t)(tv.tv_usec / 10000)); if (dst_active) { /* The value of tm_isdst is: - positive if Daylight Saving Time is in effect, diff --git a/ports/bsd/main.c b/ports/bsd/main.c index 9ed03ca5..6d6702ae 100644 --- a/ports/bsd/main.c +++ b/ports/bsd/main.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2005 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2005 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include #include @@ -46,7 +46,8 @@ #include "bacnet/basic/tsm/tsm.h" #include "bacnet/datalink/dlenv.h" -/** @file bsd/main.c Example application using the BACnet Stack on BSD/MAC OS X. */ +/** @file bsd/main.c Example application using the BACnet Stack on BSD/MAC OS + * X. */ bool Who_Is_Request = true; @@ -54,9 +55,7 @@ bool Who_Is_Request = true; static uint8_t Rx_Buf[MAX_MPDU] = { 0 }; static void LocalIAmHandler( - uint8_t * service_request, - uint16_t service_len, - BACNET_ADDRESS * src) + uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS *src) { int len = 0; uint32_t device_id = 0; @@ -64,11 +63,10 @@ static void LocalIAmHandler( int segmentation = 0; uint16_t vendor_id = 0; - (void) src; - (void) service_len; - len = - iam_decode_service_request(service_request, &device_id, &max_apdu, - &segmentation, &vendor_id); + (void)src; + (void)service_len; + len = iam_decode_service_request( + service_request, &device_id, &max_apdu, &segmentation, &vendor_id); fprintf(stderr, "Received I-Am Request"); if (len != -1) { fprintf(stderr, " from %u!\n", device_id); @@ -79,8 +77,7 @@ static void LocalIAmHandler( return; } -static void Read_Properties( - void) +static void Read_Properties(void) { uint32_t device_id = 0; bool status = false; @@ -93,32 +90,17 @@ static void Read_Properties( properties in the Device Object. Note that this demo tests for error messages so that the device doesn't have to have all the properties listed here. */ - const int object_props[] = { - PROP_OBJECT_IDENTIFIER, - PROP_OBJECT_NAME, - PROP_OBJECT_TYPE, - PROP_SYSTEM_STATUS, - PROP_VENDOR_NAME, - PROP_VENDOR_IDENTIFIER, - PROP_MODEL_NAME, - PROP_FIRMWARE_REVISION, - PROP_APPLICATION_SOFTWARE_VERSION, - PROP_PROTOCOL_VERSION, - PROP_PROTOCOL_SERVICES_SUPPORTED, - PROP_PROTOCOL_OBJECT_TYPES_SUPPORTED, - PROP_MAX_APDU_LENGTH_ACCEPTED, - PROP_SEGMENTATION_SUPPORTED, - PROP_LOCAL_TIME, - PROP_LOCAL_DATE, - PROP_UTC_OFFSET, - PROP_DAYLIGHT_SAVINGS_STATUS, - PROP_APDU_SEGMENT_TIMEOUT, - PROP_APDU_TIMEOUT, - PROP_NUMBER_OF_APDU_RETRIES, - PROP_TIME_SYNCHRONIZATION_RECIPIENTS, - PROP_MAX_MASTER, - PROP_MAX_INFO_FRAMES, - PROP_DEVICE_ADDRESS_BINDING, + const int object_props[] = { PROP_OBJECT_IDENTIFIER, PROP_OBJECT_NAME, + PROP_OBJECT_TYPE, PROP_SYSTEM_STATUS, PROP_VENDOR_NAME, + PROP_VENDOR_IDENTIFIER, PROP_MODEL_NAME, PROP_FIRMWARE_REVISION, + PROP_APPLICATION_SOFTWARE_VERSION, PROP_PROTOCOL_VERSION, + PROP_PROTOCOL_SERVICES_SUPPORTED, PROP_PROTOCOL_OBJECT_TYPES_SUPPORTED, + PROP_MAX_APDU_LENGTH_ACCEPTED, PROP_SEGMENTATION_SUPPORTED, + PROP_LOCAL_TIME, PROP_LOCAL_DATE, PROP_UTC_OFFSET, + PROP_DAYLIGHT_SAVINGS_STATUS, PROP_APDU_SEGMENT_TIMEOUT, + PROP_APDU_TIMEOUT, PROP_NUMBER_OF_APDU_RETRIES, + PROP_TIME_SYNCHRONIZATION_RECIPIENTS, PROP_MAX_MASTER, + PROP_MAX_INFO_FRAMES, PROP_DEVICE_ADDRESS_BINDING, /* note: PROP_OBJECT_LIST is missing because the result can be very large. Read index 0 which gives us the number of objects in the list, @@ -128,8 +110,7 @@ static void Read_Properties( /* some proprietary properties */ 514, 515, /* end of list */ - -1 - }; + -1 }; if (address_count()) { if (address_get_by_index(index, &device_id, &max_apdu, &src)) { @@ -139,8 +120,10 @@ static void Read_Properties( /* note: if we wanted to do this synchronously, we would get the invoke ID from the sending of the request, and wait until we got the reply with matching invoke ID or the TSM of the - invoke ID expired. This demo is doing things asynchronously. */ - status = Send_Read_Property_Request(device_id, /* destination device */ + invoke ID expired. This demo is doing things asynchronously. + */ + status = Send_Read_Property_Request( + device_id, /* destination device */ OBJECT_DEVICE, device_id, object_props[property], BACNET_ARRAY_ALL); if (status) @@ -160,31 +143,28 @@ static void Read_Properties( return; } -static void Init_Service_Handlers( - void) +static void Init_Service_Handlers(void) { Device_Init(NULL); - handler_read_property_object_set(OBJECT_DEVICE, - Device_Encode_Property_APDU, Device_Valid_Object_Instance_Number); + handler_read_property_object_set(OBJECT_DEVICE, Device_Encode_Property_APDU, + Device_Valid_Object_Instance_Number); /* 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); /* 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 - (handler_unrecognized_service); + apdu_set_unrecognized_service_handler_handler(handler_unrecognized_service); /* 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, - handler_read_property); + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_READ_PROPERTY, handler_read_property); /* handle the data coming back from confirmed requests */ - apdu_set_confirmed_ack_handler(SERVICE_CONFIRMED_READ_PROPERTY, - handler_read_property_ack); + apdu_set_confirmed_ack_handler( + SERVICE_CONFIRMED_READ_PROPERTY, handler_read_property_ack); } -static void print_address_cache( - void) +static void print_address_cache(void) { unsigned i, j; BACNET_ADDRESS address; @@ -205,8 +185,7 @@ static void print_address_cache( } } -static void print_tsm_stats( - void) +static void print_tsm_stats(void) { int idle = 0; int total = 0; @@ -216,8 +195,7 @@ static void print_tsm_stats( 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(); @@ -226,18 +204,16 @@ static void sig_handler( 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 timeout = 100; /* milliseconds */ unsigned count = 0; /* milliseconds */ time_t start_time; time_t new_time = 0; - start_time = time(NULL); /* get current time */ + start_time = time(NULL); /* get current time */ /* Linux specials */ signal(SIGINT, sig_handler); signal(SIGHUP, sig_handler); diff --git a/ports/bsd/mstimer-init.c b/ports/bsd/mstimer-init.c index 32559fc9..a3392783 100644 --- a/ports/bsd/mstimer-init.c +++ b/ports/bsd/mstimer-init.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2009 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2009 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include #include @@ -40,8 +40,7 @@ static volatile unsigned long Millisecond_Counter; static struct timespec start; /* The timeGetTime function retrieves the system time, in milliseconds. The system time is the time elapsed since the OS was started. */ -unsigned long timeGetTime( - void) +unsigned long timeGetTime(void) { struct timespec now; unsigned long ticks; @@ -54,17 +53,16 @@ unsigned long timeGetTime( now.tv_sec = mts.tv_sec; now.tv_nsec = mts.tv_nsec; - ticks = - (now.tv_sec - start.tv_sec) * 1000 + (now.tv_nsec - - start.tv_nsec) / 1000000; + ticks = (now.tv_sec - start.tv_sec) * 1000 + + (now.tv_nsec - start.tv_nsec) / 1000000; return ticks; } /** -* @brief returns the current millisecond count -* @return millisecond counter -*/ + * @brief returns the current millisecond count + * @return millisecond counter + */ unsigned long mstimer_now(void) { unsigned long now = timeGetTime(); @@ -80,10 +78,9 @@ unsigned long mstimer_now(void) } /** -* @brief Initialization for timer -*/ -void timer_init( - void) + * @brief Initialization for timer + */ +void timer_init(void) { clock_serv_t cclock; mach_timespec_t mts; diff --git a/ports/esp32/src/ai.c b/ports/esp32/src/ai.c index f18fbdd2..36c5e99c 100644 --- a/ports/esp32/src/ai.c +++ b/ports/esp32/src/ai.c @@ -1,28 +1,28 @@ /************************************************************************** -* -* Copyright (C) 2005 Steve Karg -* Copyright (C) 2011 Krzysztof Malorny -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2005 Steve Karg + * Copyright (C) 2011 Krzysztof Malorny + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* Analog Input Objects customize for your use */ @@ -34,64 +34,37 @@ #include "bacnet/bacdcode.h" #include "bacnet/bacenum.h" #include "bacnet/bactext.h" -#include "bacnet/config.h" /* the custom stuff */ +#include "bacnet/config.h" /* the custom stuff */ #include "bacnet/basic/object/device.h" #include "bacnet/basic/services.h" #include "bacnet/proplist.h" #include "bacnet/timestamp.h" #include "bacnet/basic/object/ai.h" - #ifndef MAX_ANALOG_INPUTS #define MAX_ANALOG_INPUTS 2 #endif - ANALOG_INPUT_DESCR AI_Descr[MAX_ANALOG_INPUTS]; /* These three arrays are used by the ReadPropertyMultiple handler */ -static const int Properties_Required[] = { - PROP_OBJECT_IDENTIFIER, - PROP_OBJECT_NAME, - PROP_OBJECT_TYPE, - PROP_PRESENT_VALUE, - PROP_STATUS_FLAGS, - PROP_EVENT_STATE, - PROP_OUT_OF_SERVICE, - PROP_UNITS, - -1 -}; +static const int Properties_Required[] = { PROP_OBJECT_IDENTIFIER, + PROP_OBJECT_NAME, PROP_OBJECT_TYPE, PROP_PRESENT_VALUE, PROP_STATUS_FLAGS, + PROP_EVENT_STATE, PROP_OUT_OF_SERVICE, PROP_UNITS, -1 }; -static const int Properties_Optional[] = { - PROP_DESCRIPTION, - PROP_RELIABILITY, +static const int Properties_Optional[] = { PROP_DESCRIPTION, PROP_RELIABILITY, PROP_COV_INCREMENT, #if defined(INTRINSIC_REPORTING) - PROP_TIME_DELAY, - PROP_NOTIFICATION_CLASS, - PROP_HIGH_LIMIT, - PROP_LOW_LIMIT, - PROP_DEADBAND, - PROP_LIMIT_ENABLE, - PROP_EVENT_ENABLE, - PROP_ACKED_TRANSITIONS, - PROP_NOTIFY_TYPE, - PROP_EVENT_TIME_STAMPS, + PROP_TIME_DELAY, PROP_NOTIFICATION_CLASS, PROP_HIGH_LIMIT, PROP_LOW_LIMIT, + PROP_DEADBAND, PROP_LIMIT_ENABLE, PROP_EVENT_ENABLE, PROP_ACKED_TRANSITIONS, + PROP_NOTIFY_TYPE, PROP_EVENT_TIME_STAMPS, #endif - -1 -}; + -1 }; -static const int Properties_Proprietary[] = { - 9997, - 9998, - 9999, - -1 -}; +static const int Properties_Proprietary[] = { 9997, 9998, 9999, -1 }; void Analog_Input_Property_Lists( - const int **pRequired, - const int **pOptional, - const int **pProprietary) + const int **pRequired, const int **pOptional, const int **pProprietary) { if (pRequired) *pRequired = Properties_Required; @@ -103,9 +76,7 @@ void Analog_Input_Property_Lists( return; } - -void Analog_Input_Init( - void) +void Analog_Input_Init(void) { unsigned i; #if defined(INTRINSIC_REPORTING) @@ -132,13 +103,13 @@ void Analog_Input_Init( } /* Set handler for GetEventInformation function */ - handler_get_event_information_set(OBJECT_ANALOG_INPUT, - Analog_Input_Event_Information); + handler_get_event_information_set( + OBJECT_ANALOG_INPUT, Analog_Input_Event_Information); /* Set handler for AcknowledgeAlarm function */ handler_alarm_ack_set(OBJECT_ANALOG_INPUT, Analog_Input_Alarm_Ack); /* Set handler for GetAlarmSummary Service */ - handler_get_alarm_summary_set(OBJECT_ANALOG_INPUT, - Analog_Input_Alarm_Summary); + handler_get_alarm_summary_set( + OBJECT_ANALOG_INPUT, Analog_Input_Alarm_Summary); #endif } } @@ -146,8 +117,7 @@ void Analog_Input_Init( /* 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) { unsigned int index; @@ -160,8 +130,7 @@ bool Analog_Input_Valid_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; } @@ -169,8 +138,7 @@ unsigned Analog_Input_Count( /* 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; } @@ -178,8 +146,7 @@ uint32_t Analog_Input_Index_To_Instance( /* 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_Input_Instance_To_Index( - uint32_t object_instance) +unsigned Analog_Input_Instance_To_Index(uint32_t object_instance) { unsigned index = MAX_ANALOG_INPUTS; @@ -189,8 +156,7 @@ unsigned Analog_Input_Instance_To_Index( return index; } -float Analog_Input_Present_Value( - uint32_t object_instance) +float Analog_Input_Present_Value(uint32_t object_instance) { float value = 0.0; unsigned int index; @@ -203,8 +169,7 @@ float Analog_Input_Present_Value( return value; } -static void Analog_Input_COV_Detect(unsigned int index, - float value) +static void Analog_Input_COV_Detect(unsigned int index, float value) { float prior_value = 0.0; float cov_increment = 0.0; @@ -225,9 +190,7 @@ static void Analog_Input_COV_Detect(unsigned int index, } } -void Analog_Input_Present_Value_Set( - uint32_t object_instance, - float value) +void Analog_Input_Present_Value_Set(uint32_t object_instance, float value) { unsigned int index = 0; @@ -239,30 +202,27 @@ void Analog_Input_Present_Value_Set( } bool Analog_Input_Object_Name( - uint32_t object_instance, - BACNET_CHARACTER_STRING * object_name) + uint32_t object_instance, BACNET_CHARACTER_STRING *object_name) { - static char text_string[32] = ""; + static char text_string[32] = ""; unsigned int index; bool status = false; - if (object_instance==0) - status=characterstring_init_ansi(object_name, "Internal Temperature"); - else if (object_instance==1) - status=characterstring_init_ansi(object_name, "Magnetic field"); - else - { + if (object_instance == 0) + status = characterstring_init_ansi(object_name, "Internal Temperature"); + else if (object_instance == 1) + status = characterstring_init_ansi(object_name, "Magnetic field"); + else { index = Analog_Input_Instance_To_Index(object_instance); if (index < MAX_ANALOG_INPUTS) { - sprintf(text_string, "ANALOG INPUT %lu", (unsigned long) index); + sprintf(text_string, "ANALOG INPUT %lu", (unsigned long)index); status = characterstring_init_ansi(object_name, text_string); } } return status; } -bool Analog_Input_Change_Of_Value( - uint32_t object_instance) +bool Analog_Input_Change_Of_Value(uint32_t object_instance) { unsigned index = 0; bool changed = false; @@ -275,8 +235,7 @@ bool Analog_Input_Change_Of_Value( return changed; } -void Analog_Input_Change_Of_Value_Clear( - uint32_t object_instance) +void Analog_Input_Change_Of_Value_Clear(uint32_t object_instance) { unsigned index = 0; @@ -295,8 +254,7 @@ void Analog_Input_Change_Of_Value_Clear( * @return true if the value list is encoded */ bool Analog_Input_Encode_Value_List( - uint32_t object_instance, - BACNET_PROPERTY_VALUE * value_list) + uint32_t object_instance, BACNET_PROPERTY_VALUE *value_list) { bool status = false; @@ -317,12 +275,12 @@ bool Analog_Input_Encode_Value_List( value_list->value.context_specific = false; value_list->value.tag = BACNET_APPLICATION_TAG_BIT_STRING; bitstring_init(&value_list->value.type.Bit_String); - bitstring_set_bit(&value_list->value.type.Bit_String, - STATUS_FLAG_IN_ALARM, false); - bitstring_set_bit(&value_list->value.type.Bit_String, - STATUS_FLAG_FAULT, false); - bitstring_set_bit(&value_list->value.type.Bit_String, - STATUS_FLAG_OVERRIDDEN, false); + bitstring_set_bit( + &value_list->value.type.Bit_String, STATUS_FLAG_IN_ALARM, false); + bitstring_set_bit( + &value_list->value.type.Bit_String, STATUS_FLAG_FAULT, false); + bitstring_set_bit( + &value_list->value.type.Bit_String, STATUS_FLAG_OVERRIDDEN, false); if (Analog_Input_Out_Of_Service(object_instance)) { bitstring_set_bit(&value_list->value.type.Bit_String, STATUS_FLAG_OUT_OF_SERVICE, true); @@ -339,8 +297,7 @@ bool Analog_Input_Encode_Value_List( return status; } -float Analog_Input_COV_Increment( - uint32_t object_instance) +float Analog_Input_COV_Increment(uint32_t object_instance) { unsigned index = 0; float value = 0; @@ -353,9 +310,7 @@ float Analog_Input_COV_Increment( return value; } -void Analog_Input_COV_Increment_Set( - uint32_t object_instance, - float value) +void Analog_Input_COV_Increment_Set(uint32_t object_instance, float value) { unsigned index = 0; @@ -366,8 +321,7 @@ void Analog_Input_COV_Increment_Set( } } -bool Analog_Input_Out_Of_Service( - uint32_t object_instance) +bool Analog_Input_Out_Of_Service(uint32_t object_instance) { unsigned index = 0; bool value = false; @@ -380,22 +334,20 @@ bool Analog_Input_Out_Of_Service( return value; } -void Analog_Input_Out_Of_Service_Set( - uint32_t object_instance, - bool value) +void Analog_Input_Out_Of_Service_Set(uint32_t object_instance, bool value) { unsigned index = 0; index = Analog_Input_Instance_To_Index(object_instance); if (index < MAX_ANALOG_INPUTS) { - /* BACnet Testing Observed Incident oi00104 - The Changed flag was not being set when a client wrote to the Out-of-Service bit. - Revealed by BACnet Test Client v1.8.16 ( www.bac-test.com/bacnet-test-client-download ) - BC 135.1: 8.2.1-A - BC 135.1: 8.2.2-A - Any discussions can be directed to edward@bac-test.com - Please feel free to remove this comment when my changes accepted after suitable time for - review by all interested parties. Say 6 months -> September 2016 */ + /* BACnet Testing Observed Incident oi00104 + The Changed flag was not being set when a client wrote to the + Out-of-Service bit. Revealed by BACnet Test Client v1.8.16 ( + www.bac-test.com/bacnet-test-client-download ) BC 135.1: 8.2.1-A BC + 135.1: 8.2.2-A Any discussions can be directed to edward@bac-test.com + Please feel free to remove this comment when my changes accepted after + suitable time for review by all interested parties. Say 6 months -> + September 2016 */ if (AI_Descr[index].Out_Of_Service != value) { AI_Descr[index].Changed = true; } @@ -405,10 +357,9 @@ void Analog_Input_Out_Of_Service_Set( /* return apdu length, or BACNET_STATUS_ERROR on error */ /* assumption - object already exists */ -int Analog_Input_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Analog_Input_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ BACNET_BIT_STRING bit_string; BACNET_CHARACTER_STRING char_string; ANALOG_INPUT_DESCR *CurrentAI; @@ -431,11 +382,10 @@ int Analog_Input_Read_Property( return BACNET_STATUS_ERROR; apdu = rpdata->application_data; - switch ((int) rpdata->object_property) { + switch ((int)rpdata->object_property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], OBJECT_ANALOG_INPUT, - rpdata->object_instance); + apdu_len = encode_application_object_id( + &apdu[0], OBJECT_ANALOG_INPUT, rpdata->object_instance); break; case PROP_OBJECT_NAME: @@ -451,9 +401,8 @@ int Analog_Input_Read_Property( break; case PROP_PRESENT_VALUE: - apdu_len = - encode_application_real(&apdu[0], - Analog_Input_Present_Value(rpdata->object_instance)); + apdu_len = encode_application_real( + &apdu[0], Analog_Input_Present_Value(rpdata->object_instance)); break; case PROP_STATUS_FLAGS: @@ -475,8 +424,7 @@ int Analog_Input_Read_Property( case PROP_EVENT_STATE: #if defined(INTRINSIC_REPORTING) apdu_len = - encode_application_enumerated(&apdu[0], - CurrentAI->Event_State); + encode_application_enumerated(&apdu[0], CurrentAI->Event_State); #else apdu_len = encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); @@ -485,14 +433,12 @@ int Analog_Input_Read_Property( case PROP_RELIABILITY: apdu_len = - encode_application_enumerated(&apdu[0], - CurrentAI->Reliability); + encode_application_enumerated(&apdu[0], CurrentAI->Reliability); break; case PROP_OUT_OF_SERVICE: apdu_len = - encode_application_boolean(&apdu[0], - CurrentAI->Out_Of_Service); + encode_application_boolean(&apdu[0], CurrentAI->Out_Of_Service); break; case PROP_UNITS: @@ -501,8 +447,8 @@ int Analog_Input_Read_Property( break; case PROP_COV_INCREMENT: - apdu_len = encode_application_real(&apdu[0], - CurrentAI->COV_Increment); + apdu_len = + encode_application_real(&apdu[0], CurrentAI->COV_Increment); break; #if defined(INTRINSIC_REPORTING) @@ -512,14 +458,12 @@ int Analog_Input_Read_Property( break; case PROP_NOTIFICATION_CLASS: - apdu_len = - encode_application_unsigned(&apdu[0], - CurrentAI->Notification_Class); + apdu_len = encode_application_unsigned( + &apdu[0], CurrentAI->Notification_Class); break; case PROP_HIGH_LIMIT: - apdu_len = - encode_application_real(&apdu[0], CurrentAI->High_Limit); + apdu_len = encode_application_real(&apdu[0], CurrentAI->High_Limit); break; case PROP_LOW_LIMIT: @@ -533,11 +477,11 @@ int Analog_Input_Read_Property( case PROP_LIMIT_ENABLE: bitstring_init(&bit_string); bitstring_set_bit(&bit_string, 0, - (CurrentAI-> - Limit_Enable & EVENT_LOW_LIMIT_ENABLE) ? true : false); + (CurrentAI->Limit_Enable & EVENT_LOW_LIMIT_ENABLE) ? true + : false); bitstring_set_bit(&bit_string, 1, - (CurrentAI-> - Limit_Enable & EVENT_HIGH_LIMIT_ENABLE) ? true : false); + (CurrentAI->Limit_Enable & EVENT_HIGH_LIMIT_ENABLE) ? true + : false); apdu_len = encode_application_bitstring(&apdu[0], &bit_string); break; @@ -545,14 +489,14 @@ int Analog_Input_Read_Property( case PROP_EVENT_ENABLE: bitstring_init(&bit_string); bitstring_set_bit(&bit_string, TRANSITION_TO_OFFNORMAL, - (CurrentAI-> - Event_Enable & EVENT_ENABLE_TO_OFFNORMAL) ? true : false); + (CurrentAI->Event_Enable & EVENT_ENABLE_TO_OFFNORMAL) ? true + : false); bitstring_set_bit(&bit_string, TRANSITION_TO_FAULT, - (CurrentAI-> - Event_Enable & EVENT_ENABLE_TO_FAULT) ? true : false); + (CurrentAI->Event_Enable & EVENT_ENABLE_TO_FAULT) ? true + : false); bitstring_set_bit(&bit_string, TRANSITION_TO_NORMAL, - (CurrentAI-> - Event_Enable & EVENT_ENABLE_TO_NORMAL) ? true : false); + (CurrentAI->Event_Enable & EVENT_ENABLE_TO_NORMAL) ? true + : false); apdu_len = encode_application_bitstring(&apdu[0], &bit_string); break; @@ -560,8 +504,7 @@ int Analog_Input_Read_Property( case PROP_ACKED_TRANSITIONS: bitstring_init(&bit_string); bitstring_set_bit(&bit_string, TRANSITION_TO_OFFNORMAL, - CurrentAI->Acked_Transitions[TRANSITION_TO_OFFNORMAL]. - bIsAcked); + CurrentAI->Acked_Transitions[TRANSITION_TO_OFFNORMAL].bIsAcked); bitstring_set_bit(&bit_string, TRANSITION_TO_FAULT, CurrentAI->Acked_Transitions[TRANSITION_TO_FAULT].bIsAcked); bitstring_set_bit(&bit_string, TRANSITION_TO_NORMAL, @@ -571,33 +514,27 @@ int Analog_Input_Read_Property( break; case PROP_NOTIFY_TYPE: - apdu_len = - encode_application_enumerated(&apdu[0], - CurrentAI->Notify_Type ? NOTIFY_EVENT : NOTIFY_ALARM); + apdu_len = encode_application_enumerated( + &apdu[0], CurrentAI->Notify_Type ? NOTIFY_EVENT : NOTIFY_ALARM); break; case PROP_EVENT_TIME_STAMPS: /* Array element zero is the number of elements in the array */ if (rpdata->array_index == 0) - apdu_len = - encode_application_unsigned(&apdu[0], - MAX_BACNET_EVENT_TRANSITION); + apdu_len = encode_application_unsigned( + &apdu[0], MAX_BACNET_EVENT_TRANSITION); /* if no index was specified, then try to encode the entire list */ /* into one packet. */ else if (rpdata->array_index == BACNET_ARRAY_ALL) { for (i = 0; i < MAX_BACNET_EVENT_TRANSITION; i++) { - len = - encode_opening_tag(&apdu[apdu_len], - TIME_STAMP_DATETIME); - len += - encode_application_date(&apdu[apdu_len + len], + len = encode_opening_tag( + &apdu[apdu_len], TIME_STAMP_DATETIME); + len += encode_application_date(&apdu[apdu_len + len], &CurrentAI->Event_Time_Stamps[i].date); - len += - encode_application_time(&apdu[apdu_len + len], + len += encode_application_time(&apdu[apdu_len + len], &CurrentAI->Event_Time_Stamps[i].time); - len += - encode_closing_tag(&apdu[apdu_len + len], - TIME_STAMP_DATETIME); + len += encode_closing_tag( + &apdu[apdu_len + len], TIME_STAMP_DATETIME); /* add it if we have room */ if ((apdu_len + len) < MAX_APDU) @@ -612,11 +549,9 @@ int Analog_Input_Read_Property( } else if (rpdata->array_index <= MAX_BACNET_EVENT_TRANSITION) { apdu_len = encode_opening_tag(&apdu[apdu_len], TIME_STAMP_DATETIME); - apdu_len += - encode_application_date(&apdu[apdu_len], + apdu_len += encode_application_date(&apdu[apdu_len], &CurrentAI->Event_Time_Stamps[rpdata->array_index].date); - apdu_len += - encode_application_time(&apdu[apdu_len], + apdu_len += encode_application_time(&apdu[apdu_len], &CurrentAI->Event_Time_Stamps[rpdata->array_index].time); apdu_len += encode_closing_tag(&apdu[apdu_len], TIME_STAMP_DATETIME); @@ -632,11 +567,13 @@ int Analog_Input_Read_Property( apdu_len = encode_application_real(&apdu[0], 90.510F); break; case 9998: - /* test case for unsigned encoding-decoding unsigned value correctly */ + /* test case for unsigned encoding-decoding unsigned value correctly + */ apdu_len = encode_application_unsigned(&apdu[0], 90); break; case 9999: - /* test case for signed encoding-decoding negative value correctly */ + /* test case for signed encoding-decoding negative value correctly + */ apdu_len = encode_application_signed(&apdu[0], -200); break; default: @@ -646,8 +583,9 @@ int Analog_Input_Read_Property( break; } /* only array properties can have array options */ - if ((apdu_len >= 0) && (rpdata->object_property != PROP_EVENT_TIME_STAMPS) - && (rpdata->array_index != BACNET_ARRAY_ALL)) { + if ((apdu_len >= 0) && + (rpdata->object_property != PROP_EVENT_TIME_STAMPS) && + (rpdata->array_index != BACNET_ARRAY_ALL)) { rpdata->error_class = ERROR_CLASS_PROPERTY; rpdata->error_code = ERROR_CODE_PROPERTY_IS_NOT_AN_ARRAY; apdu_len = BACNET_STATUS_ERROR; @@ -657,19 +595,17 @@ int Analog_Input_Read_Property( } /* returns true if successful */ -bool Analog_Input_Write_Property( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Analog_Input_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data) { - bool status = false; /* return value */ + bool status = false; /* return value */ unsigned int object_index = 0; int len = 0; BACNET_APPLICATION_DATA_VALUE value; ANALOG_INPUT_DESCR *CurrentAI; /* decode the some of the request */ - len = - bacapp_decode_application_data(wp_data->application_data, - wp_data->application_data_len, &value); + len = bacapp_decode_application_data( + wp_data->application_data, wp_data->application_data_len, &value); /* FIXME: len < application_data_len: more data? */ if (len < 0) { /* error while decoding - a value larger than we can handle */ @@ -691,16 +627,15 @@ bool Analog_Input_Write_Property( return false; } - switch ((int) wp_data->object_property) { + switch ((int)wp_data->object_property) { case PROP_PRESENT_VALUE: - status = - WPValidateArgType(&value, BACNET_APPLICATION_TAG_REAL, + status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_REAL, &wp_data->error_class, &wp_data->error_code); if (status) { if (CurrentAI->Out_Of_Service == true) { - Analog_Input_Present_Value_Set(wp_data->object_instance, - value.type.Real); + Analog_Input_Present_Value_Set( + wp_data->object_instance, value.type.Real); } else { wp_data->error_class = ERROR_CLASS_PROPERTY; wp_data->error_code = ERROR_CODE_WRITE_ACCESS_DENIED; @@ -710,34 +645,30 @@ bool Analog_Input_Write_Property( break; case PROP_OUT_OF_SERVICE: - status = - WPValidateArgType(&value, BACNET_APPLICATION_TAG_BOOLEAN, + status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_BOOLEAN, &wp_data->error_class, &wp_data->error_code); if (status) { Analog_Input_Out_Of_Service_Set( - wp_data->object_instance, - value.type.Boolean); + wp_data->object_instance, value.type.Boolean); } break; case PROP_UNITS: status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_ENUMERATED, - &wp_data->error_class, &wp_data->error_code); + &wp_data->error_class, &wp_data->error_code); if (status) { CurrentAI->Units = value.type.Enumerated; } break; case PROP_COV_INCREMENT: - status = - WPValidateArgType(&value, BACNET_APPLICATION_TAG_REAL, + status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_REAL, &wp_data->error_class, &wp_data->error_code); if (status) { if (value.type.Real >= 0.0) { Analog_Input_COV_Increment_Set( - wp_data->object_instance, - value.type.Real); + wp_data->object_instance, value.type.Real); } else { status = false; wp_data->error_class = ERROR_CLASS_PROPERTY; @@ -750,7 +681,7 @@ bool Analog_Input_Write_Property( case PROP_TIME_DELAY: status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_UNSIGNED_INT, - &wp_data->error_class, &wp_data->error_code); + &wp_data->error_class, &wp_data->error_code); if (status) { CurrentAI->Time_Delay = value.type.Unsigned_Int; @@ -761,7 +692,7 @@ bool Analog_Input_Write_Property( case PROP_NOTIFICATION_CLASS: status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_UNSIGNED_INT, - &wp_data->error_class, &wp_data->error_code); + &wp_data->error_class, &wp_data->error_code); if (status) { CurrentAI->Notification_Class = value.type.Unsigned_Int; @@ -769,8 +700,7 @@ bool Analog_Input_Write_Property( break; case PROP_HIGH_LIMIT: - status = - WPValidateArgType(&value, BACNET_APPLICATION_TAG_REAL, + status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_REAL, &wp_data->error_class, &wp_data->error_code); if (status) { @@ -779,8 +709,7 @@ bool Analog_Input_Write_Property( break; case PROP_LOW_LIMIT: - status = - WPValidateArgType(&value, BACNET_APPLICATION_TAG_REAL, + status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_REAL, &wp_data->error_class, &wp_data->error_code); if (status) { @@ -789,8 +718,7 @@ bool Analog_Input_Write_Property( break; case PROP_DEADBAND: - status = - WPValidateArgType(&value, BACNET_APPLICATION_TAG_REAL, + status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_REAL, &wp_data->error_class, &wp_data->error_code); if (status) { @@ -801,7 +729,7 @@ bool Analog_Input_Write_Property( case PROP_LIMIT_ENABLE: status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_BIT_STRING, - &wp_data->error_class, &wp_data->error_code); + &wp_data->error_class, &wp_data->error_code); if (status) { if (value.type.Bit_String.bits_used == 2) { @@ -817,7 +745,7 @@ bool Analog_Input_Write_Property( case PROP_EVENT_ENABLE: status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_BIT_STRING, - &wp_data->error_class, &wp_data->error_code); + &wp_data->error_class, &wp_data->error_code); if (status) { if (value.type.Bit_String.bits_used == 3) { @@ -833,10 +761,10 @@ bool Analog_Input_Write_Property( case PROP_NOTIFY_TYPE: status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_ENUMERATED, - &wp_data->error_class, &wp_data->error_code); + &wp_data->error_class, &wp_data->error_code); if (status) { - switch ((BACNET_NOTIFY_TYPE) value.type.Enumerated) { + switch ((BACNET_NOTIFY_TYPE)value.type.Enumerated) { case NOTIFY_EVENT: CurrentAI->Notify_Type = 1; break; @@ -878,9 +806,7 @@ bool Analog_Input_Write_Property( return status; } - -void Analog_Input_Intrinsic_Reporting( - uint32_t object_instance) +void Analog_Input_Intrinsic_Reporting(uint32_t object_instance) { #if defined(INTRINSIC_REPORTING) BACNET_EVENT_NOTIFICATION_DATA event_data; @@ -893,7 +819,6 @@ void Analog_Input_Intrinsic_Reporting( float PresentVal = 0.0f; bool SendNotify = false; - object_index = Analog_Input_Instance_To_Index(object_instance); if (object_index < MAX_ANALOG_INPUTS) CurrentAI = &AI_Descr[object_index]; @@ -904,7 +829,6 @@ void Analog_Input_Intrinsic_Reporting( if (!CurrentAI->Limit_Enable) return; /* limits are not configured */ - if (CurrentAI->Ack_notify_data.bSendAckNotify) { /* clean bSendAckNotify flag */ CurrentAI->Ack_notify_data.bSendAckNotify = false; @@ -930,10 +854,12 @@ void Analog_Input_Intrinsic_Reporting( switch (CurrentAI->Event_State) { case EVENT_STATE_NORMAL: /* A TO-OFFNORMAL event is generated under these conditions: - (a) the Present_Value must exceed the High_Limit for a minimum - period of time, specified in the Time_Delay property, and - (b) the HighLimitEnable flag must be set in the Limit_Enable property, and - (c) the TO-OFFNORMAL flag must be set in the Event_Enable property. */ + (a) the Present_Value must exceed the High_Limit for a + minimum period of time, specified in the Time_Delay property, + and (b) the HighLimitEnable flag must be set in the + Limit_Enable property, and + (c) the TO-OFFNORMAL flag must be set in the Event_Enable + property. */ if ((PresentVal > CurrentAI->High_Limit) && ((CurrentAI->Limit_Enable & EVENT_HIGH_LIMIT_ENABLE) == EVENT_HIGH_LIMIT_ENABLE) && @@ -947,10 +873,12 @@ void Analog_Input_Intrinsic_Reporting( } /* A TO-OFFNORMAL event is generated under these conditions: - (a) the Present_Value must exceed the Low_Limit plus the Deadband - for a minimum period of time, specified in the Time_Delay property, and - (b) the LowLimitEnable flag must be set in the Limit_Enable property, and - (c) the TO-NORMAL flag must be set in the Event_Enable property. */ + (a) the Present_Value must exceed the Low_Limit plus the + Deadband for a minimum period of time, specified in the + Time_Delay property, and (b) the LowLimitEnable flag must be + set in the Limit_Enable property, and + (c) the TO-NORMAL flag must be set in the Event_Enable + property. */ if ((PresentVal < CurrentAI->Low_Limit) && ((CurrentAI->Limit_Enable & EVENT_LOW_LIMIT_ENABLE) == EVENT_LOW_LIMIT_ENABLE) && @@ -967,14 +895,17 @@ void Analog_Input_Intrinsic_Reporting( break; case EVENT_STATE_HIGH_LIMIT: - /* Once exceeded, the Present_Value must fall below the High_Limit minus - the Deadband before a TO-NORMAL event is generated under these conditions: - (a) the Present_Value must fall below the High_Limit minus the Deadband - for a minimum period of time, specified in the Time_Delay property, and - (b) the HighLimitEnable flag must be set in the Limit_Enable property, and - (c) the TO-NORMAL flag must be set in the Event_Enable property. */ - if ((PresentVal < CurrentAI->High_Limit - CurrentAI->Deadband) - && ((CurrentAI->Limit_Enable & EVENT_HIGH_LIMIT_ENABLE) == + /* Once exceeded, the Present_Value must fall below the + High_Limit minus the Deadband before a TO-NORMAL event is + generated under these conditions: (a) the Present_Value must + fall below the High_Limit minus the Deadband for a minimum + period of time, specified in the Time_Delay property, and (b) + the HighLimitEnable flag must be set in the Limit_Enable + property, and (c) the TO-NORMAL flag must be set in the + Event_Enable property. */ + if ((PresentVal < + CurrentAI->High_Limit - CurrentAI->Deadband) && + ((CurrentAI->Limit_Enable & EVENT_HIGH_LIMIT_ENABLE) == EVENT_HIGH_LIMIT_ENABLE) && ((CurrentAI->Event_Enable & EVENT_ENABLE_TO_NORMAL) == EVENT_ENABLE_TO_NORMAL)) { @@ -992,12 +923,14 @@ void Analog_Input_Intrinsic_Reporting( /* Once the Present_Value has fallen below the Low_Limit, the Present_Value must exceed the Low_Limit plus the Deadband before a TO-NORMAL event is generated under these conditions: - (a) the Present_Value must exceed the Low_Limit plus the Deadband - for a minimum period of time, specified in the Time_Delay property, and - (b) the LowLimitEnable flag must be set in the Limit_Enable property, and - (c) the TO-NORMAL flag must be set in the Event_Enable property. */ - if ((PresentVal > CurrentAI->Low_Limit + CurrentAI->Deadband) - && ((CurrentAI->Limit_Enable & EVENT_LOW_LIMIT_ENABLE) == + (a) the Present_Value must exceed the Low_Limit plus the + Deadband for a minimum period of time, specified in the + Time_Delay property, and (b) the LowLimitEnable flag must be + set in the Limit_Enable property, and + (c) the TO-NORMAL flag must be set in the Event_Enable + property. */ + if ((PresentVal > CurrentAI->Low_Limit + CurrentAI->Deadband) && + ((CurrentAI->Limit_Enable & EVENT_LOW_LIMIT_ENABLE) == EVENT_LOW_LIMIT_ENABLE) && ((CurrentAI->Event_Enable & EVENT_ENABLE_TO_NORMAL) == EVENT_ENABLE_TO_NORMAL)) { @@ -1013,7 +946,7 @@ void Analog_Input_Intrinsic_Reporting( default: return; /* shouldn't happen */ - } /* switch (FromState) */ + } /* switch (FromState) */ ToState = CurrentAI->Event_State; @@ -1036,19 +969,19 @@ void Analog_Input_Intrinsic_Reporting( case EVENT_STATE_NORMAL: if (FromState == EVENT_STATE_HIGH_LIMIT) { ExceededLimit = CurrentAI->High_Limit; - characterstring_init_ansi(&msgText, - "Back to normal state from high limit"); + characterstring_init_ansi( + &msgText, "Back to normal state from high limit"); } else { ExceededLimit = CurrentAI->Low_Limit; - characterstring_init_ansi(&msgText, - "Back to normal state from low limit"); + characterstring_init_ansi( + &msgText, "Back to normal state from low limit"); } break; default: ExceededLimit = 0; break; - } /* switch (ToState) */ + } /* switch (ToState) */ #if PRINT_ENABLED fprintf(stderr, "Event_State for (%s,%d) goes from %s to %s.\n", @@ -1065,7 +998,6 @@ void Analog_Input_Intrinsic_Reporting( } } - if (SendNotify) { /* Event Object Identifier */ event_data.eventObjectIdentifier.type = OBJECT_ANALOG_INPUT; @@ -1121,18 +1053,20 @@ void Analog_Input_Intrinsic_Reporting( event_data.notificationParams.outOfRange.exceedingValue = PresentVal; /* Status_Flags of the referenced object. */ - bitstring_init(&event_data.notificationParams.outOfRange. - statusFlags); - bitstring_set_bit(&event_data.notificationParams.outOfRange. - statusFlags, STATUS_FLAG_IN_ALARM, - CurrentAI->Event_State ? true : false); - bitstring_set_bit(&event_data.notificationParams.outOfRange. - statusFlags, STATUS_FLAG_FAULT, false); - bitstring_set_bit(&event_data.notificationParams.outOfRange. - statusFlags, STATUS_FLAG_OVERRIDDEN, false); - bitstring_set_bit(&event_data.notificationParams.outOfRange. - statusFlags, STATUS_FLAG_OUT_OF_SERVICE, - CurrentAI->Out_Of_Service); + bitstring_init( + &event_data.notificationParams.outOfRange.statusFlags); + bitstring_set_bit( + &event_data.notificationParams.outOfRange.statusFlags, + STATUS_FLAG_IN_ALARM, CurrentAI->Event_State ? true : false); + bitstring_set_bit( + &event_data.notificationParams.outOfRange.statusFlags, + STATUS_FLAG_FAULT, false); + bitstring_set_bit( + &event_data.notificationParams.outOfRange.statusFlags, + STATUS_FLAG_OVERRIDDEN, false); + bitstring_set_bit( + &event_data.notificationParams.outOfRange.statusFlags, + STATUS_FLAG_OUT_OF_SERVICE, CurrentAI->Out_Of_Service); /* Deadband used for limit checking. */ event_data.notificationParams.outOfRange.deadband = CurrentAI->Deadband; @@ -1151,24 +1085,24 @@ void Analog_Input_Intrinsic_Reporting( case EVENT_STATE_OFFNORMAL: case EVENT_STATE_HIGH_LIMIT: case EVENT_STATE_LOW_LIMIT: - CurrentAI->Acked_Transitions[TRANSITION_TO_OFFNORMAL]. - bIsAcked = false; - CurrentAI->Acked_Transitions[TRANSITION_TO_OFFNORMAL]. - Time_Stamp = event_data.timeStamp.value.dateTime; + CurrentAI->Acked_Transitions[TRANSITION_TO_OFFNORMAL] + .bIsAcked = false; + CurrentAI->Acked_Transitions[TRANSITION_TO_OFFNORMAL] + .Time_Stamp = event_data.timeStamp.value.dateTime; break; case EVENT_STATE_FAULT: - CurrentAI->Acked_Transitions[TRANSITION_TO_FAULT]. - bIsAcked = false; - CurrentAI->Acked_Transitions[TRANSITION_TO_FAULT]. - Time_Stamp = event_data.timeStamp.value.dateTime; + CurrentAI->Acked_Transitions[TRANSITION_TO_FAULT].bIsAcked = + false; + CurrentAI->Acked_Transitions[TRANSITION_TO_FAULT] + .Time_Stamp = event_data.timeStamp.value.dateTime; break; case EVENT_STATE_NORMAL: - CurrentAI->Acked_Transitions[TRANSITION_TO_NORMAL]. - bIsAcked = false; - CurrentAI->Acked_Transitions[TRANSITION_TO_NORMAL]. - Time_Stamp = event_data.timeStamp.value.dateTime; + CurrentAI->Acked_Transitions[TRANSITION_TO_NORMAL] + .bIsAcked = false; + CurrentAI->Acked_Transitions[TRANSITION_TO_NORMAL] + .Time_Stamp = event_data.timeStamp.value.dateTime; break; } } @@ -1176,17 +1110,14 @@ void Analog_Input_Intrinsic_Reporting( #endif /* defined(INTRINSIC_REPORTING) */ } - #if defined(INTRINSIC_REPORTING) int Analog_Input_Event_Information( - unsigned index, - BACNET_GET_EVENT_INFORMATION_DATA * getevent_data) + unsigned index, BACNET_GET_EVENT_INFORMATION_DATA *getevent_data) { bool IsNotAckedTransitions; bool IsActiveEvent; int i; - /* check index */ if (index < MAX_ANALOG_INPUTS) { /* Event_State not equal to NORMAL */ @@ -1195,14 +1126,15 @@ int Analog_Input_Event_Information( /* Acked_Transitions property, which has at least one of the bits (TO-OFFNORMAL, TO-FAULT, TONORMAL) set to FALSE. */ IsNotAckedTransitions = - (AI_Descr[index].Acked_Transitions[TRANSITION_TO_OFFNORMAL]. - bIsAcked == - false) | (AI_Descr[index].Acked_Transitions[TRANSITION_TO_FAULT]. - bIsAcked == - false) | (AI_Descr[index].Acked_Transitions[TRANSITION_TO_NORMAL]. - bIsAcked == false); + (AI_Descr[index] + .Acked_Transitions[TRANSITION_TO_OFFNORMAL] + .bIsAcked == false) | + (AI_Descr[index].Acked_Transitions[TRANSITION_TO_FAULT].bIsAcked == + false) | + (AI_Descr[index].Acked_Transitions[TRANSITION_TO_NORMAL].bIsAcked == + false); } else - return -1; /* end of list */ + return -1; /* end of list */ if ((IsActiveEvent) || (IsNotAckedTransitions)) { /* Object Identifier */ @@ -1215,8 +1147,9 @@ int Analog_Input_Event_Information( bitstring_init(&getevent_data->acknowledgedTransitions); bitstring_set_bit(&getevent_data->acknowledgedTransitions, TRANSITION_TO_OFFNORMAL, - AI_Descr[index].Acked_Transitions[TRANSITION_TO_OFFNORMAL]. - bIsAcked); + AI_Descr[index] + .Acked_Transitions[TRANSITION_TO_OFFNORMAL] + .bIsAcked); bitstring_set_bit(&getevent_data->acknowledgedTransitions, TRANSITION_TO_FAULT, AI_Descr[index].Acked_Transitions[TRANSITION_TO_FAULT].bIsAcked); @@ -1234,35 +1167,31 @@ int Analog_Input_Event_Information( /* Event Enable */ bitstring_init(&getevent_data->eventEnable); bitstring_set_bit(&getevent_data->eventEnable, TRANSITION_TO_OFFNORMAL, - (AI_Descr[index]. - Event_Enable & EVENT_ENABLE_TO_OFFNORMAL) ? true : false); + (AI_Descr[index].Event_Enable & EVENT_ENABLE_TO_OFFNORMAL) ? true + : false); bitstring_set_bit(&getevent_data->eventEnable, TRANSITION_TO_FAULT, - (AI_Descr[index]. - Event_Enable & EVENT_ENABLE_TO_FAULT) ? true : false); + (AI_Descr[index].Event_Enable & EVENT_ENABLE_TO_FAULT) ? true + : false); bitstring_set_bit(&getevent_data->eventEnable, TRANSITION_TO_NORMAL, - (AI_Descr[index]. - Event_Enable & EVENT_ENABLE_TO_NORMAL) ? true : false); + (AI_Descr[index].Event_Enable & EVENT_ENABLE_TO_NORMAL) ? true + : false); /* Event Priorities */ - Notification_Class_Get_Priorities(AI_Descr[index].Notification_Class, - getevent_data->eventPriorities); + Notification_Class_Get_Priorities( + AI_Descr[index].Notification_Class, getevent_data->eventPriorities); - return 1; /* active event */ + return 1; /* active event */ } else - return 0; /* no active event at this index */ + return 0; /* no active event at this index */ } - int Analog_Input_Alarm_Ack( - BACNET_ALARM_ACK_DATA * alarmack_data, - BACNET_ERROR_CODE * error_code) + BACNET_ALARM_ACK_DATA *alarmack_data, BACNET_ERROR_CODE *error_code) { ANALOG_INPUT_DESCR *CurrentAI; unsigned int object_index; - - object_index = - Analog_Input_Instance_To_Index(alarmack_data->eventObjectIdentifier. - instance); + object_index = Analog_Input_Instance_To_Index( + alarmack_data->eventObjectIdentifier.instance); if (object_index < MAX_ANALOG_INPUTS) CurrentAI = &AI_Descr[object_index]; @@ -1275,22 +1204,23 @@ int Analog_Input_Alarm_Ack( case EVENT_STATE_OFFNORMAL: case EVENT_STATE_HIGH_LIMIT: case EVENT_STATE_LOW_LIMIT: - if (CurrentAI->Acked_Transitions[TRANSITION_TO_OFFNORMAL]. - bIsAcked == false) { + if (CurrentAI->Acked_Transitions[TRANSITION_TO_OFFNORMAL] + .bIsAcked == false) { if (alarmack_data->eventTimeStamp.tag != TIME_STAMP_DATETIME) { *error_code = ERROR_CODE_INVALID_TIME_STAMP; return -1; } - if (datetime_compare(&CurrentAI-> - Acked_Transitions[TRANSITION_TO_OFFNORMAL].Time_Stamp, + if (datetime_compare( + &CurrentAI->Acked_Transitions[TRANSITION_TO_OFFNORMAL] + .Time_Stamp, &alarmack_data->eventTimeStamp.value.dateTime) > 0) { *error_code = ERROR_CODE_INVALID_TIME_STAMP; return -1; } /* FIXME: Send ack notification */ - CurrentAI->Acked_Transitions[TRANSITION_TO_OFFNORMAL]. - bIsAcked = true; + CurrentAI->Acked_Transitions[TRANSITION_TO_OFFNORMAL].bIsAcked = + true; } else { *error_code = ERROR_CODE_INVALID_EVENT_STATE; return -1; @@ -1304,8 +1234,9 @@ int Analog_Input_Alarm_Ack( *error_code = ERROR_CODE_INVALID_TIME_STAMP; return -1; } - if (datetime_compare(&CurrentAI-> - Acked_Transitions[TRANSITION_TO_FAULT].Time_Stamp, + if (datetime_compare( + &CurrentAI->Acked_Transitions[TRANSITION_TO_FAULT] + .Time_Stamp, &alarmack_data->eventTimeStamp.value.dateTime) > 0) { *error_code = ERROR_CODE_INVALID_TIME_STAMP; return -1; @@ -1327,8 +1258,9 @@ int Analog_Input_Alarm_Ack( *error_code = ERROR_CODE_INVALID_TIME_STAMP; return -1; } - if (datetime_compare(&CurrentAI-> - Acked_Transitions[TRANSITION_TO_NORMAL].Time_Stamp, + if (datetime_compare( + &CurrentAI->Acked_Transitions[TRANSITION_TO_NORMAL] + .Time_Stamp, &alarmack_data->eventTimeStamp.value.dateTime) > 0) { *error_code = ERROR_CODE_INVALID_TIME_STAMP; return -1; @@ -1353,10 +1285,8 @@ int Analog_Input_Alarm_Ack( } int Analog_Input_Alarm_Summary( - unsigned index, - BACNET_GET_ALARM_SUMMARY_DATA * getalarm_data) + unsigned index, BACNET_GET_ALARM_SUMMARY_DATA *getalarm_data) { - /* check index */ if (index < MAX_ANALOG_INPUTS) { /* Event_State is not equal to NORMAL and @@ -1373,36 +1303,37 @@ int Analog_Input_Alarm_Summary( bitstring_init(&getalarm_data->acknowledgedTransitions); bitstring_set_bit(&getalarm_data->acknowledgedTransitions, TRANSITION_TO_OFFNORMAL, - AI_Descr[index].Acked_Transitions[TRANSITION_TO_OFFNORMAL]. - bIsAcked); + AI_Descr[index] + .Acked_Transitions[TRANSITION_TO_OFFNORMAL] + .bIsAcked); bitstring_set_bit(&getalarm_data->acknowledgedTransitions, TRANSITION_TO_FAULT, - AI_Descr[index]. - Acked_Transitions[TRANSITION_TO_FAULT].bIsAcked); + AI_Descr[index] + .Acked_Transitions[TRANSITION_TO_FAULT] + .bIsAcked); bitstring_set_bit(&getalarm_data->acknowledgedTransitions, TRANSITION_TO_NORMAL, - AI_Descr[index]. - Acked_Transitions[TRANSITION_TO_NORMAL].bIsAcked); + AI_Descr[index] + .Acked_Transitions[TRANSITION_TO_NORMAL] + .bIsAcked); - return 1; /* active alarm */ + return 1; /* active alarm */ } else - return 0; /* no active alarm at this index */ + return 0; /* no active alarm at this index */ } else - return -1; /* end of list */ + return -1; /* end of list */ } #endif /* defined(INTRINSIC_REPORTING) */ - #ifdef TEST #include #include #include "ctest.h" -bool WPValidateArgType( - BACNET_APPLICATION_DATA_VALUE * pValue, +bool WPValidateArgType(BACNET_APPLICATION_DATA_VALUE *pValue, uint8_t ucExpectedTag, - BACNET_ERROR_CLASS * pErrorClass, - BACNET_ERROR_CODE * pErrorCode) + BACNET_ERROR_CLASS *pErrorClass, + BACNET_ERROR_CODE *pErrorCode) { bool bResult; @@ -1420,8 +1351,7 @@ bool WPValidateArgType( return (bResult); } -void testAnalogInput( - Test * pTest) +void testAnalogInput(Test *pTest) { uint8_t apdu[MAX_APDU] = { 0 }; int len = 0; @@ -1450,8 +1380,7 @@ void testAnalogInput( } #ifdef TEST_ANALOG_INPUT -int main( - void) +int main(void) { Test *pTest; bool rc; @@ -1463,7 +1392,7 @@ int main( ct_setStream(pTest, stdout); ct_run(pTest); - (void) ct_report(pTest); + (void)ct_report(pTest); ct_destroy(pTest); return 0; diff --git a/ports/esp32/src/bip_init.c b/ports/esp32/src/bip_init.c index 7f22c03f..002acb4e 100644 --- a/ports/esp32/src/bip_init.c +++ b/ports/esp32/src/bip_init.c @@ -10,8 +10,7 @@ #include "bacnet/datalink/bip.h" -long bip_get_addr_by_name( - const char *host_name) +long bip_get_addr_by_name(const char *host_name) { return 0; } @@ -20,7 +19,7 @@ void bip_set_interface(char *ifname) { } -void bip_cleanup (void) +void bip_cleanup(void) { close(bip_socket()); bip_set_socket(-1); @@ -28,7 +27,6 @@ void bip_cleanup (void) bool bip_init(char *ifname) { - tcpip_adapter_ip_info_t ip_info = { 0 }; int value = 1; @@ -38,9 +36,10 @@ bool bip_init(char *ifname) bip_set_interface(ifname); bip_set_port(0xBAC0U); bip_set_addr(ip_info.ip.addr); - bip_set_broadcast_addr((ip_info.ip.addr&ip_info.netmask.addr)|(~ip_info.netmask.addr)); + bip_set_broadcast_addr( + (ip_info.ip.addr & ip_info.netmask.addr) | (~ip_info.netmask.addr)); - int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); + int sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); struct sockaddr_in saddr = { 0 }; saddr.sin_family = PF_INET; @@ -48,8 +47,8 @@ bool bip_init(char *ifname) saddr.sin_addr.s_addr = htonl(INADDR_ANY); bind(sock, (struct sockaddr *)&saddr, sizeof(struct sockaddr_in)); - setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *) &value, sizeof(value)); - setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *) &value, sizeof(value)); + setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (char *)&value, sizeof(value)); + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&value, sizeof(value)); bip_set_socket(sock); diff --git a/ports/esp32/src/bo.c b/ports/esp32/src/bo.c index 94de931f..ece479e5 100644 --- a/ports/esp32/src/bo.c +++ b/ports/esp32/src/bo.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2005 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2005 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* Binary Output Objects - customize for your use */ @@ -32,7 +32,7 @@ #include "bacnet/bacdcode.h" #include "bacnet/bacenum.h" #include "bacnet/bacapp.h" -#include "bacnet/config.h" /* the custom stuff */ +#include "bacnet/config.h" /* the custom stuff */ #include "bacnet/rp.h" #include "bacnet/wp.h" #include "bacnet/basic/object/bo.h" @@ -46,42 +46,25 @@ /* the Relinquish Default value */ #define RELINQUISH_DEFAULT BINARY_INACTIVE /* Here is our Priority Array.*/ -static BACNET_BINARY_PV - Binary_Output_Level[MAX_BINARY_OUTPUTS][BACNET_MAX_PRIORITY]; +static BACNET_BINARY_PV Binary_Output_Level[MAX_BINARY_OUTPUTS] + [BACNET_MAX_PRIORITY]; /* Writable out-of-service allows others to play with our Present Value */ /* without changing the physical output */ static bool Out_Of_Service[MAX_BINARY_OUTPUTS]; /* These three arrays are used by the ReadPropertyMultiple handler */ -static const int Binary_Output_Properties_Required[] = { - PROP_OBJECT_IDENTIFIER, - PROP_OBJECT_NAME, - PROP_OBJECT_TYPE, - PROP_PRESENT_VALUE, - PROP_STATUS_FLAGS, - PROP_EVENT_STATE, - PROP_OUT_OF_SERVICE, - PROP_POLARITY, - PROP_PRIORITY_ARRAY, - PROP_RELINQUISH_DEFAULT, - -1 -}; +static const int Binary_Output_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, + PROP_OBJECT_NAME, PROP_OBJECT_TYPE, PROP_PRESENT_VALUE, PROP_STATUS_FLAGS, + PROP_EVENT_STATE, PROP_OUT_OF_SERVICE, PROP_POLARITY, PROP_PRIORITY_ARRAY, + PROP_RELINQUISH_DEFAULT, -1 }; -static const int Binary_Output_Properties_Optional[] = { - PROP_DESCRIPTION, - PROP_ACTIVE_TEXT, - PROP_INACTIVE_TEXT, - -1 -}; +static const int Binary_Output_Properties_Optional[] = { PROP_DESCRIPTION, + PROP_ACTIVE_TEXT, PROP_INACTIVE_TEXT, -1 }; -static const int Binary_Output_Properties_Proprietary[] = { - -1 -}; +static const int Binary_Output_Properties_Proprietary[] = { -1 }; void Binary_Output_Property_Lists( - const int **pRequired, - const int **pOptional, - const int **pProprietary) + const int **pRequired, const int **pOptional, const int **pProprietary) { if (pRequired) *pRequired = Binary_Output_Properties_Required; @@ -93,8 +76,7 @@ void Binary_Output_Property_Lists( return; } -void Binary_Output_Init( - void) +void Binary_Output_Init(void) { unsigned i, j; static bool initialized = false; @@ -116,8 +98,7 @@ void Binary_Output_Init( /* 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; @@ -127,8 +108,7 @@ bool Binary_Output_Valid_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; } @@ -136,8 +116,7 @@ unsigned Binary_Output_Count( /* 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; } @@ -145,8 +124,7 @@ uint32_t Binary_Output_Index_To_Instance( /* 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; @@ -156,8 +134,7 @@ unsigned Binary_Output_Instance_To_Index( return index; } -BACNET_BINARY_PV Binary_Output_Present_Value( - uint32_t object_instance) +BACNET_BINARY_PV Binary_Output_Present_Value(uint32_t object_instance) { BACNET_BINARY_PV value = RELINQUISH_DEFAULT; unsigned index = 0; @@ -176,8 +153,7 @@ BACNET_BINARY_PV Binary_Output_Present_Value( return value; } -bool Binary_Output_Out_Of_Service( - uint32_t object_instance) +bool Binary_Output_Out_Of_Service(uint32_t object_instance) { bool value = false; unsigned index = 0; @@ -193,32 +169,28 @@ bool Binary_Output_Out_Of_Service( /* note: the object name must be unique within this device */ bool Binary_Output_Object_Name( - uint32_t object_instance, - BACNET_CHARACTER_STRING * object_name) + uint32_t object_instance, BACNET_CHARACTER_STRING *object_name) { - static char text_string[32] = ""; + static char text_string[32] = ""; bool status = false; - if (object_instance==0) - status=characterstring_init_ansi(object_name, "Led"); - else - { + if (object_instance == 0) + status = characterstring_init_ansi(object_name, "Led"); + else { if (object_instance < MAX_BINARY_OUTPUTS) { sprintf(text_string, "BINARY OUTPUT %lu", - (unsigned long) object_instance); + (unsigned long)object_instance); status = characterstring_init_ansi(object_name, text_string); } } return status; } - /* return apdu len, or BACNET_STATUS_ERROR on error */ -int Binary_Output_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Binary_Output_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { 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; @@ -235,9 +207,8 @@ int Binary_Output_Read_Property( apdu = rpdata->application_data; switch (rpdata->object_property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], OBJECT_BINARY_OUTPUT, - rpdata->object_instance); + apdu_len = encode_application_object_id( + &apdu[0], OBJECT_BINARY_OUTPUT, rpdata->object_instance); break; /* note: Name and Description don't have to be the same. You could make Description writable and different */ @@ -295,9 +266,8 @@ int Binary_Output_Read_Property( 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); + len = encode_application_enumerated( + &apdu[apdu_len], present_value); } /* add it if we have room */ if ((apdu_len + len) < MAX_APDU) @@ -317,11 +287,11 @@ int Binary_Output_Read_Property( 1] == BINARY_NULL) apdu_len = encode_application_null(&apdu[apdu_len]); else { - present_value = Binary_Output_Level[object_index] - [rpdata->array_index - 1]; - apdu_len = - encode_application_enumerated(&apdu[apdu_len], - present_value); + present_value = + Binary_Output_Level[object_index] + [rpdata->array_index - 1]; + apdu_len = encode_application_enumerated( + &apdu[apdu_len], present_value); } } else { rpdata->error_class = ERROR_CLASS_PROPERTY; @@ -363,10 +333,9 @@ int Binary_Output_Read_Property( } /* returns true if successful */ -bool Binary_Output_Write_Property( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Binary_Output_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data) { - bool status = false; /* return value */ + bool status = false; /* return value */ unsigned int object_index = 0; unsigned int priority = 0; BACNET_BINARY_PV level = BINARY_NULL; @@ -374,9 +343,8 @@ bool Binary_Output_Write_Property( BACNET_APPLICATION_DATA_VALUE value; /* decode the some of the request */ - len = - bacapp_decode_application_data(wp_data->application_data, - wp_data->application_data_len, &value); + len = bacapp_decode_application_data( + wp_data->application_data, wp_data->application_data_len, &value); /* FIXME: len < application_data_len: more data? */ if (len < 0) { /* error while decoding - a value larger than we can handle */ @@ -399,19 +367,19 @@ bool Binary_Output_Write_Property( algorithm and may not be used for other purposes in any object. */ if (priority && (priority <= BACNET_MAX_PRIORITY) && - (priority != 6 /* reserved */ ) && + (priority != 6 /* reserved */) && (value.type.Enumerated <= MAX_BINARY_PV)) { - level = (BACNET_BINARY_PV) value.type.Enumerated; - object_index = - Binary_Output_Instance_To_Index - (wp_data->object_instance); + 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) */ + 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 @@ -424,24 +392,23 @@ bool Binary_Output_Write_Property( wp_data->error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; } } else { - status = - WPValidateArgType(&value, BACNET_APPLICATION_TAG_NULL, + status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_NULL, &wp_data->error_class, &wp_data->error_code); if (status) { level = BINARY_NULL; - object_index = - Binary_Output_Instance_To_Index - (wp_data->object_instance); + 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) */ + /* 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) */ } else { status = false; wp_data->error_class = ERROR_CLASS_PROPERTY; @@ -451,14 +418,12 @@ bool Binary_Output_Write_Property( } break; case PROP_OUT_OF_SERVICE: - status = - WPValidateArgType(&value, BACNET_APPLICATION_TAG_BOOLEAN, + status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_BOOLEAN, &wp_data->error_class, &wp_data->error_code); if (status) { object_index = Binary_Output_Instance_To_Index(wp_data->object_instance); - Out_Of_Service[object_index] = - value.type.Boolean; + Out_Of_Service[object_index] = value.type.Boolean; } break; case PROP_OBJECT_IDENTIFIER: @@ -484,17 +449,15 @@ bool Binary_Output_Write_Property( return status; } - #ifdef TEST #include #include #include "ctest.h" -bool WPValidateArgType( - BACNET_APPLICATION_DATA_VALUE * pValue, +bool WPValidateArgType(BACNET_APPLICATION_DATA_VALUE *pValue, uint8_t ucExpectedTag, - BACNET_ERROR_CLASS * pErrorClass, - BACNET_ERROR_CODE * pErrorCode) + BACNET_ERROR_CLASS *pErrorClass, + BACNET_ERROR_CODE *pErrorCode) { pValue = pValue; ucExpectedTag = ucExpectedTag; @@ -504,8 +467,7 @@ bool WPValidateArgType( return false; } -void testBinaryOutput( - Test * pTest) +void testBinaryOutput(Test *pTest) { uint8_t apdu[MAX_APDU] = { 0 }; int len = 0; @@ -534,8 +496,7 @@ void testBinaryOutput( } #ifdef TEST_BINARY_OUTPUT -int main( - void) +int main(void) { Test *pTest; bool rc; @@ -547,7 +508,7 @@ int main( ct_setStream(pTest, stdout); ct_run(pTest); - (void) ct_report(pTest); + (void)ct_report(pTest); ct_destroy(pTest); return 0; diff --git a/ports/esp32/src/device.c b/ports/esp32/src/device.c index 797499c2..1d97263b 100644 --- a/ports/esp32/src/device.c +++ b/ports/esp32/src/device.c @@ -1,47 +1,47 @@ /************************************************************************** -* -* Copyright (C) 2005,2006,2009 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2005,2006,2009 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /** @file device.c Base "class" for handling all BACnet objects belonging * to a BACnet device, as well as Device-specific properties. */ #include #include -#include /* for memmove */ -#include /* for timezone, localtime */ +#include /* for memmove */ +#include /* for timezone, localtime */ #include "bacnet/datalink/datalink.h" #include "bacnet/bacdef.h" #include "bacnet/bacdcode.h" #include "bacnet/bacenum.h" #include "bacnet/bacapp.h" -#include "bacnet/config.h" /* the custom stuff */ +#include "bacnet/config.h" /* the custom stuff */ #include "bacnet/apdu.h" #include "bacnet/wp.h" /* WriteProperty handling */ #include "bacnet/rp.h" /* ReadProperty handling */ -#include "bacnet/dcc.h" /* DeviceCommunicationControl handling */ +#include "bacnet/dcc.h" /* DeviceCommunicationControl handling */ #include "bacnet/version.h" -#include "bacnet/basic/object/device.h" /* me */ +#include "bacnet/basic/object/device.h" /* me */ #include "bacnet/basic/services.h" #include "../lib/stack/address.h" /* os specfic includes */ @@ -61,91 +61,53 @@ #include "bacnet/basic/ucix/ucix.h" #endif /* defined(BAC_UCI) */ - /* Not included in time.h as specified by The Open Group */ /* Difference from UTC and local standard time */ long int timezone; - /* local forward (semi-private) and external prototypes */ -int Device_Read_Property_Local( - BACNET_READ_PROPERTY_DATA * rpdata); -bool Device_Write_Property_Local( - BACNET_WRITE_PROPERTY_DATA * wp_data); -extern int Routed_Device_Read_Property_Local( - BACNET_READ_PROPERTY_DATA * rpdata); +int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA *rpdata); +bool Device_Write_Property_Local(BACNET_WRITE_PROPERTY_DATA *wp_data); +extern int Routed_Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA *rpdata); extern bool Routed_Device_Write_Property_Local( - BACNET_WRITE_PROPERTY_DATA * wp_data); + BACNET_WRITE_PROPERTY_DATA *wp_data); /* may be overridden by outside table */ static object_functions_t *Object_Table; static object_functions_t My_Object_Table[] = { - {OBJECT_DEVICE, - NULL /* Init - don't init Device or it will recourse! */ , - Device_Count, - Device_Index_To_Instance, - Device_Valid_Object_Instance_Number, - Device_Object_Name, - Device_Read_Property_Local, - Device_Write_Property_Local, - Device_Property_Lists, - DeviceGetRRInfo, - NULL /* Iterator */ , - NULL /* Value_Lists */ , - NULL , - NULL, - NULL /* Intrinsic Reporting */ }, - {OBJECT_ANALOG_INPUT, - Analog_Input_Init, - Analog_Input_Count, - Analog_Input_Index_To_Instance, - Analog_Input_Valid_Instance, - Analog_Input_Object_Name, - Analog_Input_Read_Property, - Analog_Input_Write_Property, - Analog_Input_Property_Lists, - NULL /* ReadRangeInfo */ , - NULL /* Iterator */ , - Analog_Input_Encode_Value_List, - Analog_Input_Change_Of_Value, - Analog_Input_Change_Of_Value_Clear, - Analog_Input_Intrinsic_Reporting}, - {OBJECT_BINARY_OUTPUT, - Binary_Output_Init, - Binary_Output_Count, - Binary_Output_Index_To_Instance, - Binary_Output_Valid_Instance, - Binary_Output_Object_Name, - Binary_Output_Read_Property, - Binary_Output_Write_Property, - Binary_Output_Property_Lists, - NULL /* ReadRangeInfo */ , - NULL /* Iterator */ , - NULL /* Value_Lists */ , - NULL /* COV */ , - NULL /* COV Clear */ , - NULL /* Intrinsic Reporting */ }, - {MAX_BACNET_OBJECT_TYPE, - NULL /* Init */ , - NULL /* Count */ , - NULL /* Index_To_Instance */ , - NULL /* Valid_Instance */ , - NULL /* Object_Name */ , - NULL /* Read_Property */ , - NULL /* Write_Property */ , - NULL /* Property_Lists */ , - NULL /* ReadRangeInfo */ , - NULL /* Iterator */ , - NULL /* Value_Lists */ , - NULL /* COV */ , - NULL /* COV Clear */ , + { OBJECT_DEVICE, NULL /* Init - don't init Device or it will recourse! */, + Device_Count, Device_Index_To_Instance, + Device_Valid_Object_Instance_Number, Device_Object_Name, + Device_Read_Property_Local, Device_Write_Property_Local, + Device_Property_Lists, DeviceGetRRInfo, NULL /* Iterator */, + NULL /* Value_Lists */, NULL, NULL, NULL /* Intrinsic Reporting */ }, + { OBJECT_ANALOG_INPUT, Analog_Input_Init, Analog_Input_Count, + Analog_Input_Index_To_Instance, Analog_Input_Valid_Instance, + Analog_Input_Object_Name, Analog_Input_Read_Property, + Analog_Input_Write_Property, Analog_Input_Property_Lists, + NULL /* ReadRangeInfo */, NULL /* Iterator */, + Analog_Input_Encode_Value_List, Analog_Input_Change_Of_Value, + Analog_Input_Change_Of_Value_Clear, Analog_Input_Intrinsic_Reporting }, + { OBJECT_BINARY_OUTPUT, Binary_Output_Init, Binary_Output_Count, + Binary_Output_Index_To_Instance, Binary_Output_Valid_Instance, + Binary_Output_Object_Name, Binary_Output_Read_Property, + Binary_Output_Write_Property, Binary_Output_Property_Lists, + NULL /* ReadRangeInfo */, NULL /* Iterator */, NULL /* Value_Lists */, + NULL /* COV */, NULL /* COV Clear */, NULL /* Intrinsic Reporting */ }, + { MAX_BACNET_OBJECT_TYPE, NULL /* Init */, NULL /* Count */, + NULL /* Index_To_Instance */, NULL /* Valid_Instance */, + NULL /* Object_Name */, NULL /* Read_Property */, + NULL /* Write_Property */, NULL /* Property_Lists */, + NULL /* ReadRangeInfo */, NULL /* Iterator */, NULL /* Value_Lists */, + NULL /* COV */, NULL /* COV Clear */, NULL /* Intrinsic Reporting */ } }; /** Glue function to let the Device object, when called by a handler, * lookup which Object type needs to be invoked. * @ingroup ObjHelpers - * @param Object_Type [in] The type of BACnet Object the handler wants to access. + * @param Object_Type [in] The type of BACnet Object the handler wants to + * access. * @return Pointer to the group of object helper functions that implement this * type of Object. */ @@ -166,17 +128,18 @@ static struct object_functions *Device_Objects_Find_Functions( return (NULL); } -/** Try to find a rr_info_function helper function for the requested object type. +/** Try to find a rr_info_function helper function for the requested object + * type. * @ingroup ObjIntf * - * @param object_type [in] The type of BACnet Object the handler wants to access. + * @param object_type [in] The type of BACnet Object the handler wants to + * access. * @return Pointer to the object helper function that implements the * ReadRangeInfo function, Object_RR_Info, for this type of Object on * success, else a NULL pointer if the type of Object isn't supported * or doesn't have a ReadRangeInfo function. */ -rr_info_function Device_Objects_RR_Info( - BACNET_OBJECT_TYPE object_type) +rr_info_function Device_Objects_RR_Info(BACNET_OBJECT_TYPE object_type) { struct object_functions *pObject = NULL; @@ -195,8 +158,7 @@ rr_info_function Device_Objects_RR_Info( * list, separately, the Required, Optional, and Proprietary object * properties with their counts. */ -void Device_Objects_Property_List( - BACNET_OBJECT_TYPE object_type, +void Device_Objects_Property_List(BACNET_OBJECT_TYPE object_type, uint32_t object_instance, struct special_property_list_t *pPropertyList) { @@ -219,17 +181,17 @@ void Device_Objects_Property_List( } /* Fetch the counts if available otherwise zero them */ - pPropertyList->Required.count = - pPropertyList->Required.pList == - NULL ? 0 : property_list_count(pPropertyList->Required.pList); + pPropertyList->Required.count = pPropertyList->Required.pList == NULL + ? 0 + : property_list_count(pPropertyList->Required.pList); - pPropertyList->Optional.count = - pPropertyList->Optional.pList == - NULL ? 0 : property_list_count(pPropertyList->Optional.pList); + pPropertyList->Optional.count = pPropertyList->Optional.pList == NULL + ? 0 + : property_list_count(pPropertyList->Optional.pList); - pPropertyList->Proprietary.count = - pPropertyList->Proprietary.pList == - NULL ? 0 : property_list_count(pPropertyList->Proprietary.pList); + pPropertyList->Proprietary.count = pPropertyList->Proprietary.pList == NULL + ? 0 + : property_list_count(pPropertyList->Proprietary.pList); return; } @@ -247,8 +209,7 @@ void Device_Objects_Property_List( * On failure, the error class and code will be set. * @return True if succeeds (password is correct), else False. */ -bool Device_Reinitialize( - BACNET_REINITIALIZE_DEVICE_DATA * rd_data) +bool Device_Reinitialize(BACNET_REINITIALIZE_DEVICE_DATA *rd_data) { bool status = false; @@ -286,59 +247,33 @@ bool Device_Reinitialize( } /* These three arrays are used by the ReadPropertyMultiple handler */ -static const int Device_Properties_Required[] = { - PROP_OBJECT_IDENTIFIER, - PROP_OBJECT_NAME, - PROP_OBJECT_TYPE, - PROP_SYSTEM_STATUS, - PROP_VENDOR_NAME, - PROP_VENDOR_IDENTIFIER, - PROP_MODEL_NAME, - PROP_FIRMWARE_REVISION, - PROP_APPLICATION_SOFTWARE_VERSION, - PROP_PROTOCOL_VERSION, - PROP_PROTOCOL_REVISION, - PROP_PROTOCOL_SERVICES_SUPPORTED, - PROP_PROTOCOL_OBJECT_TYPES_SUPPORTED, - PROP_OBJECT_LIST, - PROP_MAX_APDU_LENGTH_ACCEPTED, - PROP_SEGMENTATION_SUPPORTED, - PROP_APDU_TIMEOUT, - PROP_NUMBER_OF_APDU_RETRIES, - PROP_DEVICE_ADDRESS_BINDING, - PROP_DATABASE_REVISION, - -1 -}; +static const int Device_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, + PROP_OBJECT_NAME, PROP_OBJECT_TYPE, PROP_SYSTEM_STATUS, PROP_VENDOR_NAME, + PROP_VENDOR_IDENTIFIER, PROP_MODEL_NAME, PROP_FIRMWARE_REVISION, + PROP_APPLICATION_SOFTWARE_VERSION, PROP_PROTOCOL_VERSION, + PROP_PROTOCOL_REVISION, PROP_PROTOCOL_SERVICES_SUPPORTED, + PROP_PROTOCOL_OBJECT_TYPES_SUPPORTED, PROP_OBJECT_LIST, + PROP_MAX_APDU_LENGTH_ACCEPTED, PROP_SEGMENTATION_SUPPORTED, + PROP_APDU_TIMEOUT, PROP_NUMBER_OF_APDU_RETRIES, PROP_DEVICE_ADDRESS_BINDING, + PROP_DATABASE_REVISION, -1 }; static const int Device_Properties_Optional[] = { #if defined(BACDL_MSTP) - PROP_MAX_MASTER, - PROP_MAX_INFO_FRAMES, + PROP_MAX_MASTER, PROP_MAX_INFO_FRAMES, #endif - PROP_DESCRIPTION, - PROP_LOCAL_TIME, - PROP_UTC_OFFSET, - PROP_LOCAL_DATE, - PROP_DAYLIGHT_SAVINGS_STATUS, - PROP_LOCATION, - PROP_ACTIVE_COV_SUBSCRIPTIONS, + PROP_DESCRIPTION, PROP_LOCAL_TIME, PROP_UTC_OFFSET, PROP_LOCAL_DATE, + PROP_DAYLIGHT_SAVINGS_STATUS, PROP_LOCATION, PROP_ACTIVE_COV_SUBSCRIPTIONS, #if defined(BACNET_TIME_MASTER) - PROP_TIME_SYNCHRONIZATION_RECIPIENTS, - PROP_TIME_SYNCHRONIZATION_INTERVAL, - PROP_ALIGN_INTERVALS, - PROP_INTERVAL_OFFSET, + PROP_TIME_SYNCHRONIZATION_RECIPIENTS, PROP_TIME_SYNCHRONIZATION_INTERVAL, + PROP_ALIGN_INTERVALS, PROP_INTERVAL_OFFSET, #endif -1 }; -static const int Device_Properties_Proprietary[] = { - -1 -}; +static const int Device_Properties_Proprietary[] = { -1 }; void Device_Property_Lists( - const int **pRequired, - const int **pOptional, - const int **pProprietary) + const int **pRequired, const int **pOptional, const int **pProprietary) { if (pRequired) *pRequired = Device_Properties_Required; @@ -363,7 +298,8 @@ static uint16_t Vendor_Identifier = BACNET_VENDOR_ID; static char Model_Name[MAX_DEV_MOD_LEN + 1] = "ESP32"; static char Application_Software_Version[MAX_DEV_VER_LEN + 1] = "1.0"; static char Location[MAX_DEV_LOC_LEN + 1] = "FR"; -static char Description[MAX_DEV_DESC_LEN + 1] = "EPS32 by F. Chaxel, Stack S. Karg"; +static char Description[MAX_DEV_DESC_LEN + 1] = + "EPS32 by F. Chaxel, Stack S. Karg"; static char *BACnet_Version = "0.8.2"; /* static uint8_t Protocol_Version = 1; - constant, not settable */ /* static uint8_t Protocol_Revision = 4; - constant, not settable */ @@ -374,14 +310,14 @@ static char *BACnet_Version = "0.8.2"; /* static uint8_t Max_Segments_Accepted = 0; */ /* VT_Classes_Supported */ /* Active_VT_Sessions */ -static BACNET_TIME Local_Time; /* rely on OS, if there is one */ -static BACNET_DATE Local_Date; /* rely on OS, if there is one */ +static BACNET_TIME Local_Time; /* rely on OS, if there is one */ +static BACNET_DATE Local_Date; /* rely on OS, if there is one */ /* NOTE: BACnet UTC Offset is inverse of common practice. If your UTC offset is -5hours of GMT, then BACnet UTC offset is +5hours. BACnet UTC offset is expressed in minutes. */ static int32_t UTC_Offset = 5 * 60; -static bool Daylight_Savings_Status = false; /* rely on OS */ +static bool Daylight_Savings_Status = false; /* rely on OS */ #if defined(BACNET_TIME_MASTER) static bool Align_Intervals; static uint32_t Interval_Minutes; @@ -403,14 +339,12 @@ static uint32_t Database_Revision = 0; /* Slave_Address_Binding */ /* Profile_Name */ -unsigned Device_Count( - void) +unsigned Device_Count(void) { return 1; } -uint32_t Device_Index_To_Instance( - unsigned index) +uint32_t Device_Index_To_Instance(unsigned index) { index = index; return Object_Instance_Number; @@ -424,8 +358,7 @@ uint32_t Device_Index_To_Instance( * @ingroup ObjIntf * @return The Instance number used in the BACNET_OBJECT_ID for the Device. */ -uint32_t Device_Object_Instance_Number( - void) +uint32_t Device_Object_Instance_Number(void) { #ifdef BAC_ROUTING return Routed_Device_Object_Instance_Number(); @@ -434,8 +367,7 @@ uint32_t Device_Object_Instance_Number( #endif } -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 */ @@ -449,15 +381,13 @@ bool Device_Set_Object_Instance_Number( return status; } -bool Device_Valid_Object_Instance_Number( - uint32_t object_id) +bool Device_Valid_Object_Instance_Number(uint32_t object_id) { return (Object_Instance_Number == object_id); } bool Device_Object_Name( - uint32_t object_instance, - BACNET_CHARACTER_STRING * object_name) + uint32_t object_instance, BACNET_CHARACTER_STRING *object_name) { bool status = false; @@ -468,10 +398,9 @@ bool Device_Object_Name( return status; } -bool Device_Set_Object_Name( - BACNET_CHARACTER_STRING * object_name) +bool Device_Set_Object_Name(BACNET_CHARACTER_STRING *object_name) { - bool status = false; /*return value */ + bool status = false; /*return value */ if (!characterstring_same(&My_Object_Name, object_name)) { /* Make the change and update the database revision */ @@ -482,22 +411,19 @@ bool Device_Set_Object_Name( return status; } -bool Device_Object_Name_ANSI_Init(const char * value) +bool Device_Object_Name_ANSI_Init(const char *value) { return characterstring_init_ansi(&My_Object_Name, value); } -BACNET_DEVICE_STATUS Device_System_Status( - void) +BACNET_DEVICE_STATUS Device_System_Status(void) { return System_Status; } -int Device_Set_System_Status( - BACNET_DEVICE_STATUS status, - bool local) +int Device_Set_System_Status(BACNET_DEVICE_STATUS status, bool local) { - int result = 0; /*return value - 0 = ok, -1 = bad value, -2 = not allowed */ + int result = 0; /*return value - 0 = ok, -1 = bad value, -2 = not allowed */ /* We limit the options available depending on whether the source is * internal or external. */ @@ -558,39 +484,34 @@ int Device_Set_System_Status( return (result); } -const char *Device_Vendor_Name( - void) +const char *Device_Vendor_Name(void) { return Vendor_Name; } /** Returns the Vendor ID for this Device. - * See the assignments at http://www.bacnet.org/VendorID/BACnet%20Vendor%20IDs.htm + * See the assignments at + * http://www.bacnet.org/VendorID/BACnet%20Vendor%20IDs.htm * @return The Vendor ID of this Device. */ -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 */ + bool status = false; /*return value */ if (length < sizeof(Model_Name)) { memmove(Model_Name, name, length); @@ -601,23 +522,19 @@ bool Device_Set_Model_Name( 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, - size_t length) +bool Device_Set_Application_Software_Version(const char *name, size_t length) { - bool status = false; /*return value */ + bool status = false; /*return value */ if (length < sizeof(Application_Software_Version)) { memmove(Application_Software_Version, name, length); @@ -628,17 +545,14 @@ bool Device_Set_Application_Software_Version( 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 */ + bool status = false; /*return value */ if (length < sizeof(Description)) { memmove(Description, name, length); @@ -649,17 +563,14 @@ bool Device_Set_Description( 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 */ + bool status = false; /*return value */ if (length < sizeof(Location)) { memmove(Location, name, length); @@ -670,32 +581,27 @@ bool Device_Set_Location( 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; } -BACNET_SEGMENTATION Device_Segmentation_Supported( - void) +BACNET_SEGMENTATION Device_Segmentation_Supported(void) { return SEGMENTATION_NONE; } -uint32_t Device_Database_Revision( - void) +uint32_t Device_Database_Revision(void) { return Database_Revision; } -void Device_Set_Database_Revision( - uint32_t revision) +void Device_Set_Database_Revision(uint32_t revision) { Database_Revision = revision; } @@ -705,8 +611,7 @@ void Device_Set_Database_Revision( * the most common operation if changing object names and ids is * implemented. */ -void Device_Inc_Database_Revision( - void) +void Device_Inc_Database_Revision(void) { Database_Revision++; } @@ -716,8 +621,7 @@ void Device_Inc_Database_Revision( * for discovery, it must be consistent! * @return The count of objects, for all supported Object types. */ -unsigned Device_Object_List_Count( - void) +unsigned Device_Object_List_Count(void) { unsigned count = 0; /* number of objects */ struct object_functions *pObject = NULL; @@ -745,9 +649,7 @@ unsigned Device_Object_List_Count( * @return True if found, else false. */ bool Device_Object_List_Identifier( - uint32_t array_index, - BACNET_OBJECT_TYPE *object_type, - uint32_t * instance) + uint32_t array_index, BACNET_OBJECT_TYPE *object_type, uint32_t *instance) { bool status = false; uint32_t count = 0; @@ -771,19 +673,19 @@ bool Device_Object_List_Identifier( * look for the index to instance to get the ID */ if (pObject->Object_Iterator) { /* First find the first object */ - temp_index = pObject->Object_Iterator(~(unsigned) 0); + temp_index = pObject->Object_Iterator(~(unsigned)0); /* Then step through the objects to find the nth */ while (object_index != 0) { temp_index = pObject->Object_Iterator(temp_index); object_index--; } - /* set the object_index up before falling through to next bit */ + /* set the object_index up before falling through to next + * bit */ object_index = temp_index; } if (pObject->Object_Index_To_Instance) { *object_type = pObject->Object_Type; - *instance = - pObject->Object_Index_To_Instance(object_index); + *instance = pObject->Object_Index_To_Instance(object_index); status = true; break; } @@ -800,13 +702,13 @@ bool Device_Object_List_Identifier( * and the lookup succeeds, they will be given the resulting values. * @param object_name [in] The desired Object Name to look for. * @param object_type [out] The BACNET_OBJECT_TYPE of the matching Object. - * @param object_instance [out] The object instance number of the matching Object. + * @param object_instance [out] The object instance number of the matching + * Object. * @return True on success or else False if not found. */ -bool Device_Valid_Object_Name( - BACNET_CHARACTER_STRING * object_name1, +bool Device_Valid_Object_Name(BACNET_CHARACTER_STRING *object_name1, BACNET_OBJECT_TYPE *object_type, - uint32_t * object_instance) + uint32_t *object_instance) { bool found = false; BACNET_OBJECT_TYPE type = OBJECT_NONE; @@ -845,10 +747,9 @@ bool Device_Valid_Object_Name( * @return True if found, else False if no such Object in this device. */ bool Device_Valid_Object_Id( - BACNET_OBJECT_TYPE object_type, - uint32_t object_instance) + BACNET_OBJECT_TYPE object_type, uint32_t object_instance) { - bool status = false; /* return value */ + bool status = false; /* return value */ struct object_functions *pObject = NULL; pObject = Device_Objects_Find_Functions(object_type); @@ -865,10 +766,9 @@ bool Device_Valid_Object_Id( * @param object_name [out] The Object Name found for this child Object. * @return True on success or else False if not found. */ -bool Device_Object_Name_Copy( - BACNET_OBJECT_TYPE object_type, +bool Device_Object_Name_Copy(BACNET_OBJECT_TYPE object_type, uint32_t object_instance, - BACNET_CHARACTER_STRING * object_name) + BACNET_CHARACTER_STRING *object_name) { struct object_functions *pObject = NULL; bool found = false; @@ -881,8 +781,7 @@ bool Device_Object_Name_Copy( return found; } -static void Update_Current_Time( - void) +static void Update_Current_Time(void) { struct tm *tblock = NULL; #if defined(_MSC_VER) @@ -913,15 +812,15 @@ int tm_isdst Daylight Savings flag. #endif if (tblock) { - datetime_set_date(&Local_Date, (uint16_t) tblock->tm_year + 1900, - (uint8_t) tblock->tm_mon + 1, (uint8_t) tblock->tm_mday); + datetime_set_date(&Local_Date, (uint16_t)tblock->tm_year + 1900, + (uint8_t)tblock->tm_mon + 1, (uint8_t)tblock->tm_mday); #if !defined(_MSC_VER) - datetime_set_time(&Local_Time, (uint8_t) tblock->tm_hour, - (uint8_t) tblock->tm_min, (uint8_t) tblock->tm_sec, - (uint8_t) (tv.tv_usec / 10000)); + datetime_set_time(&Local_Time, (uint8_t)tblock->tm_hour, + (uint8_t)tblock->tm_min, (uint8_t)tblock->tm_sec, + (uint8_t)(tv.tv_usec / 10000)); #else - datetime_set_time(&Local_Time, (uint8_t) tblock->tm_hour, - (uint8_t) tblock->tm_min, (uint8_t) tblock->tm_sec, 0); + datetime_set_time(&Local_Time, (uint8_t)tblock->tm_hour, + (uint8_t)tblock->tm_min, (uint8_t)tblock->tm_sec, 0); #endif if (tblock->tm_isdst) { Daylight_Savings_Status = true; @@ -937,8 +836,7 @@ int tm_isdst Daylight Savings flag. } } -void Device_getCurrentDateTime( - BACNET_DATE_TIME * DateTime) +void Device_getCurrentDateTime(BACNET_DATE_TIME *DateTime) { Update_Current_Time(); @@ -1039,11 +937,10 @@ uint32_t Device_Interval_Offset(void) /* return the length of the apdu encoded or BACNET_STATUS_ERROR for error or BACNET_STATUS_ABORT for abort message */ -int Device_Read_Property_Local( - BACNET_READ_PROPERTY_DATA * rpdata) +int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA *rpdata) { - 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 = { 0 }; BACNET_CHARACTER_STRING char_string = { 0 }; uint32_t i = 0; @@ -1063,9 +960,8 @@ int Device_Read_Property_Local( apdu_max = rpdata->application_data_len; switch (rpdata->object_property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], OBJECT_DEVICE, - Object_Instance_Number); + apdu_len = encode_application_object_id( + &apdu[0], OBJECT_DEVICE, Object_Instance_Number); break; case PROP_OBJECT_NAME: apdu_len = @@ -1088,8 +984,7 @@ int Device_Read_Property_Local( encode_application_character_string(&apdu[0], &char_string); break; case PROP_VENDOR_IDENTIFIER: - apdu_len = - encode_application_unsigned(&apdu[0], Vendor_Identifier); + apdu_len = encode_application_unsigned(&apdu[0], Vendor_Identifier); break; case PROP_MODEL_NAME: characterstring_init_ansi(&char_string, Model_Name); @@ -1102,8 +997,8 @@ int Device_Read_Property_Local( encode_application_character_string(&apdu[0], &char_string); break; case PROP_APPLICATION_SOFTWARE_VERSION: - characterstring_init_ansi(&char_string, - Application_Software_Version); + characterstring_init_ansi( + &char_string, Application_Software_Version); apdu_len = encode_application_character_string(&apdu[0], &char_string); break; @@ -1130,22 +1025,20 @@ int Device_Read_Property_Local( encode_application_boolean(&apdu[0], Daylight_Savings_Status); break; case PROP_PROTOCOL_VERSION: - apdu_len = - encode_application_unsigned(&apdu[0], - Device_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()); + apdu_len = encode_application_unsigned( + &apdu[0], Device_Protocol_Revision()); 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)); + 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; @@ -1155,7 +1048,7 @@ int Device_Read_Property_Local( 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); + bitstring_set_bit(&bit_string, (uint8_t)i, false); } /* set the object types with objects to supported */ @@ -1179,16 +1072,15 @@ int Device_Read_Property_Local( /* your maximum APDU size. */ else if (rpdata->array_index == BACNET_ARRAY_ALL) { for (i = 1; i <= count; i++) { - found = - Device_Object_List_Identifier(i, &object_type, - &instance); + found = Device_Object_List_Identifier( + i, &object_type, &instance); if (found) { - len = - encode_application_object_id(&apdu[apdu_len], - 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? Don't check for last entry */ + /* can we all fit into the APDU? Don't check for last + * entry */ if ((i != count) && (apdu_len + len) >= apdu_max) { /* Abort response */ rpdata->error_code = @@ -1205,13 +1097,11 @@ int Device_Read_Property_Local( } } } else { - found = - Device_Object_List_Identifier(rpdata->array_index, - &object_type, &instance); + found = Device_Object_List_Identifier( + rpdata->array_index, &object_type, &instance); if (found) { - apdu_len = - encode_application_object_id(&apdu[0], object_type, - instance); + apdu_len = encode_application_object_id( + &apdu[0], object_type, instance); } else { rpdata->error_class = ERROR_CLASS_PROPERTY; rpdata->error_code = ERROR_CODE_INVALID_ARRAY_INDEX; @@ -1223,9 +1113,8 @@ int Device_Read_Property_Local( apdu_len = encode_application_unsigned(&apdu[0], MAX_APDU); break; case PROP_SEGMENTATION_SUPPORTED: - apdu_len = - encode_application_enumerated(&apdu[0], - Device_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()); @@ -1237,14 +1126,12 @@ int Device_Read_Property_Local( apdu_len = address_list_encode(&apdu[0], apdu_max); break; case PROP_DATABASE_REVISION: - apdu_len = - encode_application_unsigned(&apdu[0], 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()); + encode_application_unsigned(&apdu[0], dlmstp_max_info_frames()); break; case PROP_MAX_MASTER: apdu_len = @@ -1261,17 +1148,16 @@ int Device_Read_Property_Local( } break; case PROP_TIME_SYNCHRONIZATION_INTERVAL: - apdu_len = encode_application_unsigned(&apdu[0], - Device_Time_Sync_Interval()); + apdu_len = encode_application_unsigned( + &apdu[0], Device_Time_Sync_Interval()); break; case PROP_ALIGN_INTERVALS: apdu_len = - encode_application_boolean(&apdu[0], - Device_Align_Intervals()); + encode_application_boolean(&apdu[0], Device_Align_Intervals()); break; case PROP_INTERVAL_OFFSET: - apdu_len = encode_application_unsigned(&apdu[0], - Device_Interval_Offset()); + apdu_len = + encode_application_unsigned(&apdu[0], Device_Interval_Offset()); break; #endif case PROP_ACTIVE_COV_SUBSCRIPTIONS: @@ -1294,7 +1180,8 @@ int Device_Read_Property_Local( return apdu_len; } -/** Looks up the requested Object and Property, and encodes its Value in an APDU. +/** Looks up the requested Object and Property, and encodes its Value in an + * APDU. * @ingroup ObjIntf * If the Object or Property can't be found, sets the error class and code. * @@ -1302,8 +1189,7 @@ int Device_Read_Property_Local( * on entry, and APDU message on return. * @return The length of the APDU on success, else BACNET_STATUS_ERROR */ -int Device_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Device_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { int apdu_len = BACNET_STATUS_ERROR; struct object_functions *pObject = NULL; @@ -1321,12 +1207,9 @@ int Device_Read_Property( if (pObject->Object_Read_Property) { #if (BACNET_PROTOCOL_REVISION >= 14) if ((int)rpdata->object_property == PROP_PROPERTY_LIST) { - Device_Objects_Property_List( - rpdata->object_type, - rpdata->object_instance, - &property_list); - apdu_len = property_list_encode( - rpdata, + Device_Objects_Property_List(rpdata->object_type, + rpdata->object_instance, &property_list); + apdu_len = property_list_encode(rpdata, property_list.Required.pList, property_list.Optional.pList, property_list.Proprietary.pList); @@ -1343,10 +1226,9 @@ int Device_Read_Property( } /* returns true if successful */ -bool Device_Write_Property_Local( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Device_Write_Property_Local(BACNET_WRITE_PROPERTY_DATA *wp_data) { - bool status = false; /* return value */ + bool status = false; /* return value */ int len = 0; BACNET_APPLICATION_DATA_VALUE value; BACNET_OBJECT_TYPE object_type = OBJECT_NONE; @@ -1354,9 +1236,8 @@ bool Device_Write_Property_Local( int temp; /* decode the some of the request */ - len = - bacapp_decode_application_data(wp_data->application_data, - wp_data->application_data_len, &value); + len = bacapp_decode_application_data( + wp_data->application_data, wp_data->application_data_len, &value); if (len < 0) { /* error while decoding - a value larger than we can handle */ wp_data->error_class = ERROR_CLASS_PROPERTY; @@ -1373,14 +1254,14 @@ bool Device_Write_Property_Local( /* FIXME: len < application_data_len: more data? */ switch (wp_data->object_property) { case PROP_OBJECT_IDENTIFIER: - status = - WPValidateArgType(&value, BACNET_APPLICATION_TAG_OBJECT_ID, + status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_OBJECT_ID, &wp_data->error_class, &wp_data->error_code); if (status) { 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 */ + (Device_Set_Object_Instance_Number( + value.type.Object_Id.instance))) { + /* FIXME: we could send an I-Am broadcast to let the world + * know */ } else { status = false; wp_data->error_class = ERROR_CLASS_PROPERTY; @@ -1391,38 +1272,37 @@ bool Device_Write_Property_Local( case PROP_NUMBER_OF_APDU_RETRIES: status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_UNSIGNED_INT, - &wp_data->error_class, &wp_data->error_code); + &wp_data->error_class, &wp_data->error_code); if (status) { /* FIXME: bounds check? */ - apdu_retries_set((uint8_t) value.type.Unsigned_Int); + apdu_retries_set((uint8_t)value.type.Unsigned_Int); } break; case PROP_APDU_TIMEOUT: status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_UNSIGNED_INT, - &wp_data->error_class, &wp_data->error_code); + &wp_data->error_class, &wp_data->error_code); if (status) { /* FIXME: bounds check? */ - apdu_timeout_set((uint16_t) value.type.Unsigned_Int); + apdu_timeout_set((uint16_t)value.type.Unsigned_Int); } break; case PROP_VENDOR_IDENTIFIER: status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_UNSIGNED_INT, - &wp_data->error_class, &wp_data->error_code); + &wp_data->error_class, &wp_data->error_code); if (status) { /* FIXME: bounds check? */ - Device_Set_Vendor_Identifier((uint16_t) value. - type.Unsigned_Int); + Device_Set_Vendor_Identifier((uint16_t)value.type.Unsigned_Int); } break; case PROP_SYSTEM_STATUS: status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_ENUMERATED, - &wp_data->error_class, &wp_data->error_code); + &wp_data->error_class, &wp_data->error_code); if (status) { - temp = Device_Set_System_Status((BACNET_DEVICE_STATUS) - value.type.Enumerated, false); + temp = Device_Set_System_Status( + (BACNET_DEVICE_STATUS)value.type.Enumerated, false); if (temp != 0) { status = false; wp_data->error_class = ERROR_CLASS_PROPERTY; @@ -1436,8 +1316,7 @@ bool Device_Write_Property_Local( } break; case PROP_OBJECT_NAME: - status = - WPValidateString(&value, + status = WPValidateString(&value, characterstring_capacity(&My_Object_Name), false, &wp_data->error_class, &wp_data->error_code); if (status) { @@ -1459,33 +1338,30 @@ bool Device_Write_Property_Local( } break; case PROP_LOCATION: - status = - WPValidateString(&value, MAX_DEV_LOC_LEN, true, + status = WPValidateString(&value, MAX_DEV_LOC_LEN, true, &wp_data->error_class, &wp_data->error_code); if (status) { - Device_Set_Location(characterstring_value(&value. - type.Character_String), + Device_Set_Location( + characterstring_value(&value.type.Character_String), characterstring_length(&value.type.Character_String)); } break; case PROP_DESCRIPTION: - status = - WPValidateString(&value, MAX_DEV_DESC_LEN, true, + status = WPValidateString(&value, MAX_DEV_DESC_LEN, true, &wp_data->error_class, &wp_data->error_code); if (status) { - Device_Set_Description(characterstring_value(&value. - type.Character_String), + Device_Set_Description( + characterstring_value(&value.type.Character_String), characterstring_length(&value.type.Character_String)); } break; case PROP_MODEL_NAME: - status = - WPValidateString(&value, MAX_DEV_MOD_LEN, true, + status = WPValidateString(&value, MAX_DEV_MOD_LEN, true, &wp_data->error_class, &wp_data->error_code); if (status) { - Device_Set_Model_Name(characterstring_value(&value. - type.Character_String), + Device_Set_Model_Name( + characterstring_value(&value.type.Character_String), characterstring_length(&value.type.Character_String)); } break; @@ -1539,8 +1415,8 @@ bool Device_Write_Property_Local( #endif case PROP_UTC_OFFSET: if (value.tag == BACNET_APPLICATION_TAG_SIGNED_INT) { - if ((value.type.Signed_Int < (12*60)) && - (value.type.Signed_Int > (-12*60))) { + if ((value.type.Signed_Int < (12 * 60)) && + (value.type.Signed_Int > (-12 * 60))) { Device_UTC_Offset_Set(value.type.Signed_Int); status = true; } else { @@ -1551,16 +1427,16 @@ bool Device_Write_Property_Local( wp_data->error_class = ERROR_CLASS_PROPERTY; wp_data->error_code = ERROR_CODE_INVALID_DATA_TYPE; } - break; + break; #if defined(BACDL_MSTP) case PROP_MAX_INFO_FRAMES: status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_UNSIGNED_INT, - &wp_data->error_class, &wp_data->error_code); + &wp_data->error_class, &wp_data->error_code); if (status) { if (value.type.Unsigned_Int <= 255) { - dlmstp_set_max_info_frames((uint8_t) value. - type.Unsigned_Int); + dlmstp_set_max_info_frames( + (uint8_t)value.type.Unsigned_Int); } else { status = false; wp_data->error_class = ERROR_CLASS_PROPERTY; @@ -1571,11 +1447,11 @@ bool Device_Write_Property_Local( case PROP_MAX_MASTER: status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_UNSIGNED_INT, - &wp_data->error_class, &wp_data->error_code); + &wp_data->error_class, &wp_data->error_code); if (status) { if ((value.type.Unsigned_Int > 0) && (value.type.Unsigned_Int <= 127)) { - dlmstp_set_max_master((uint8_t) value.type.Unsigned_Int); + dlmstp_set_max_master((uint8_t)value.type.Unsigned_Int); } else { status = false; wp_data->error_class = ERROR_CLASS_PROPERTY; @@ -1631,10 +1507,9 @@ bool Device_Write_Property_Local( * and new Value on entry, and APDU message on return. * @return True on success, else False if there is an error. */ -bool Device_Write_Property( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data) { - bool status = false; /* Ever the pessamist! */ + bool status = false; /* Ever the pessamist! */ struct object_functions *pObject = NULL; /* initialize the default return values */ @@ -1678,12 +1553,11 @@ bool Device_Write_Property( * @param [out] The value list * @return True if the object instance supports this feature and value changed. */ -bool Device_Encode_Value_List( - BACNET_OBJECT_TYPE object_type, +bool Device_Encode_Value_List(BACNET_OBJECT_TYPE object_type, uint32_t object_instance, - BACNET_PROPERTY_VALUE * value_list) + BACNET_PROPERTY_VALUE *value_list) { - bool status = false; /* Ever the pessamist! */ + bool status = false; /* Ever the pessamist! */ struct object_functions *pObject = NULL; pObject = Device_Objects_Find_Functions(object_type); @@ -1706,11 +1580,9 @@ bool Device_Encode_Value_List( * @param [in] The object instance to be looked up. * @return True if the COV flag is set */ -bool Device_COV( - BACNET_OBJECT_TYPE object_type, - uint32_t object_instance) +bool Device_COV(BACNET_OBJECT_TYPE object_type, uint32_t object_instance) { - bool status = false; /* Ever the pessamist! */ + bool status = false; /* Ever the pessamist! */ struct object_functions *pObject = NULL; pObject = Device_Objects_Find_Functions(object_type); @@ -1731,9 +1603,7 @@ bool Device_COV( * @param [in] The object type to be looked up. * @param [in] The object instance to be looked up. */ -void Device_COV_Clear( - BACNET_OBJECT_TYPE object_type, - uint32_t object_instance) +void Device_COV_Clear(BACNET_OBJECT_TYPE object_type, uint32_t object_instance) { struct object_functions *pObject = NULL; @@ -1749,8 +1619,7 @@ void Device_COV_Clear( } #if defined(INTRINSIC_REPORTING) -void Device_local_reporting( - void) +void Device_local_reporting(void) { struct object_functions *pObject; uint32_t objects_count; @@ -1782,10 +1651,9 @@ void Device_local_reporting( * @param [in] The object type to be looked up. * @return True if the object instance supports this feature. */ -bool Device_Value_List_Supported( - BACNET_OBJECT_TYPE object_type) +bool Device_Value_List_Supported(BACNET_OBJECT_TYPE object_type) { - bool status = false; /* Ever the pessamist! */ + bool status = false; /* Ever the pessamist! */ struct object_functions *pObject = NULL; pObject = Device_Objects_Find_Functions(object_type); @@ -1806,8 +1674,7 @@ bool Device_Value_List_Supported( * Each Child Object must provide some implementation of each of these * functions in order to properly support the default handlers. */ -void Device_Init( - object_functions_t * object_table) +void Device_Init(object_functions_t *object_table) { struct object_functions *pObject = NULL; #if defined(BAC_UCI) @@ -1842,11 +1709,10 @@ void Device_Init( } } -bool DeviceGetRRInfo( - BACNET_READ_RANGE_DATA * pRequest, /* Info on the request */ - RR_PROP_INFO * pInfo) -{ /* Where to put the response */ - bool status = false; /* return value */ +bool DeviceGetRRInfo(BACNET_READ_RANGE_DATA *pRequest, /* Info on the request */ + RR_PROP_INFO *pInfo) +{ /* Where to put the response */ + bool status = false; /* return value */ switch (pRequest->object_property) { case PROP_VT_CLASSES_SUPPORTED: @@ -1882,7 +1748,6 @@ bool DeviceGetRRInfo( return status; } - #ifdef BAC_ROUTING /**************************************************************************** ************* BACnet Routing Functionality (Optional) ********************** @@ -1898,8 +1763,7 @@ bool DeviceGetRRInfo( * @param first_object_instance Set the first (gateway) Device to this instance number. */ -void Routing_Device_Init( - uint32_t first_object_instance) +void Routing_Device_Init(uint32_t first_object_instance) { struct object_functions *pDevObject = NULL; @@ -1918,17 +1782,15 @@ void Routing_Device_Init( #endif /* BAC_ROUTING */ - #ifdef TEST #include #include #include "ctest.h" -bool WPValidateArgType( - BACNET_APPLICATION_DATA_VALUE * pValue, +bool WPValidateArgType(BACNET_APPLICATION_DATA_VALUE *pValue, uint8_t ucExpectedTag, - BACNET_ERROR_CLASS * pErrorClass, - BACNET_ERROR_CODE * pErrorCode) + BACNET_ERROR_CLASS *pErrorClass, + BACNET_ERROR_CODE *pErrorCode) { pValue = pValue; ucExpectedTag = ucExpectedTag; @@ -1938,12 +1800,11 @@ bool WPValidateArgType( return false; } -bool WPValidateString( - BACNET_APPLICATION_DATA_VALUE * pValue, +bool WPValidateString(BACNET_APPLICATION_DATA_VALUE *pValue, int iMaxLen, bool bEmptyAllowed, - BACNET_ERROR_CLASS * pErrorClass, - BACNET_ERROR_CODE * pErrorCode) + BACNET_ERROR_CLASS *pErrorClass, + BACNET_ERROR_CODE *pErrorCode) { pValue = pValue; iMaxLen = iMaxLen; @@ -1954,9 +1815,7 @@ bool WPValidateString( return false; } -int handler_cov_encode_subscriptions( - uint8_t * apdu, - int max_apdu) +int handler_cov_encode_subscriptions(uint8_t *apdu, int max_apdu) { apdu = apdu; max_apdu = max_apdu; @@ -1964,8 +1823,7 @@ int handler_cov_encode_subscriptions( return 0; } -void testDevice( - Test * pTest) +void testDevice(Test *pTest) { bool status = false; const char *name = "Patricia"; @@ -1977,15 +1835,14 @@ void testDevice( ct_test(pTest, Device_Object_Instance_Number() == BACNET_MAX_INSTANCE); ct_test(pTest, status == true); status = Device_Set_Object_Instance_Number(BACNET_MAX_INSTANCE / 2); - ct_test(pTest, - Device_Object_Instance_Number() == (BACNET_MAX_INSTANCE / 2)); + ct_test( + pTest, Device_Object_Instance_Number() == (BACNET_MAX_INSTANCE / 2)); ct_test(pTest, status == true); status = Device_Set_Object_Instance_Number(BACNET_MAX_INSTANCE + 1); - ct_test(pTest, - Device_Object_Instance_Number() != (BACNET_MAX_INSTANCE + 1)); + ct_test( + pTest, Device_Object_Instance_Number() != (BACNET_MAX_INSTANCE + 1)); ct_test(pTest, status == false); - Device_Set_System_Status(STATUS_NON_OPERATIONAL, true); ct_test(pTest, Device_System_Status() == STATUS_NON_OPERATIONAL); @@ -1998,8 +1855,7 @@ void testDevice( } #ifdef TEST_DEVICE -int main( - void) +int main(void) { Test *pTest; bool rc; @@ -2011,7 +1867,7 @@ int main( ct_setStream(pTest, stdout); ct_run(pTest); - (void) ct_report(pTest); + (void)ct_report(pTest); ct_destroy(pTest); return 0; diff --git a/ports/esp32/src/main.c b/ports/esp32/src/main.c index e8c6fbc4..c4dc63d2 100644 --- a/ports/esp32/src/main.c +++ b/ports/esp32/src/main.c @@ -60,51 +60,49 @@ void StartBACnet() 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 - (handler_unrecognized_service); + /* It is required to send the proper reject message... */ + apdu_set_unrecognized_service_handler_handler(handler_unrecognized_service); /* 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, - handler_read_property); - apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROP_MULTIPLE, - handler_read_property_multiple); - - apdu_set_confirmed_handler(SERVICE_CONFIRMED_WRITE_PROPERTY, - handler_write_property); - apdu_set_confirmed_handler(SERVICE_CONFIRMED_SUBSCRIBE_COV, - handler_cov_subscribe); - - address_init(); - bip_init(NULL); + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_READ_PROPERTY, handler_read_property); + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_READ_PROP_MULTIPLE, handler_read_property_multiple); + + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_WRITE_PROPERTY, handler_write_property); + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_SUBSCRIBE_COV, handler_cov_subscribe); + + address_init(); + bip_init(NULL); Send_I_Am(&Handler_Transmit_Buffer[0]); } /* wifi events handler : start & stop bacnet with an event */ esp_err_t wifi_event_handler(void *ctx, system_event_t *event) { - switch(event->event_id) { - case SYSTEM_EVENT_STA_START: - esp_wifi_connect(); - break; - case SYSTEM_EVENT_STA_CONNECTED: - break ; - case SYSTEM_EVENT_STA_GOT_IP: - if (xEventGroupGetBits(wifi_event_group)!=CONNECTED_BIT) - { - xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); - StartBACnet(); - } - break; - case SYSTEM_EVENT_STA_DISCONNECTED: - /* This is a workaround as ESP32 WiFi libs don't currently - auto-reassociate. */ - esp_wifi_connect(); - xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); - bip_cleanup(); - break; - default: - break; + switch (event->event_id) { + case SYSTEM_EVENT_STA_START: + esp_wifi_connect(); + break; + case SYSTEM_EVENT_STA_CONNECTED: + break; + case SYSTEM_EVENT_STA_GOT_IP: + if (xEventGroupGetBits(wifi_event_group) != CONNECTED_BIT) { + xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); + StartBACnet(); + } + break; + case SYSTEM_EVENT_STA_DISCONNECTED: + /* This is a workaround as ESP32 WiFi libs don't currently + auto-reassociate. */ + esp_wifi_connect(); + xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); + bip_cleanup(); + break; + default: + break; } return ESP_OK; } @@ -125,7 +123,7 @@ void wifi_init_station(void) esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config); - esp_wifi_start() ; + esp_wifi_start(); } /* setup gpio & nv flash, call wifi init code */ @@ -134,25 +132,22 @@ void setup() gpio_pad_select_gpio(BACNET_LED); gpio_set_direction(BACNET_LED, GPIO_MODE_OUTPUT); - gpio_set_level(BACNET_LED,0); + gpio_set_level(BACNET_LED, 0); esp_err_t ret = nvs_flash_init(); - if (ret == ESP_ERR_NVS_NO_FREE_PAGES) - { + if (ret == ESP_ERR_NVS_NO_FREE_PAGES) { nvs_flash_erase(); ret = nvs_flash_init(); } - wifi_init_station(); + wifi_init_station(); } /* Bacnet Task */ void BACnetTask(void *pvParameters) -{ +{ uint16_t pdu_len = 0; - BACNET_ADDRESS src = { - 0 - }; - unsigned timeout = 1; + BACNET_ADDRESS src = { 0 }; + unsigned timeout = 1; // Init Bacnet objets dictionnary Device_Init(NULL); @@ -160,41 +155,41 @@ void BACnetTask(void *pvParameters) setup(); - uint32_t tickcount=xTaskGetTickCount(); + uint32_t tickcount = xTaskGetTickCount(); - for (;;) - { - vTaskDelay(10 / portTICK_PERIOD_MS); // could be remove to speed the code + for (;;) { + vTaskDelay( + 10 / portTICK_PERIOD_MS); // could be remove to speed the code // do nothing if not connected to wifi - xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); - { - uint32_t newtick=xTaskGetTickCount(); + xEventGroupWaitBits( + wifi_event_group, CONNECTED_BIT, false, true, portMAX_DELAY); + { + uint32_t newtick = xTaskGetTickCount(); - // one second elapse at least (maybe much more if Wifi was deconnected for a long) - if ((newtick=configTICK_RATE_HZ)) - { - tickcount=newtick; + // one second elapse at least (maybe much more if Wifi was + // deconnected for a long) + if ((newtick < tickcount) || + ((newtick - tickcount) >= configTICK_RATE_HZ)) { + tickcount = newtick; dcc_timer_seconds(1); - bvlc_maintenance_timer(1); + bvlc_maintenance_timer(1); handler_cov_timer_seconds(1); tsm_timer_milliseconds(1000); // Read analog values from internal sensors - Analog_Input_Present_Value_Set(0,temprature_sens_read()); - Analog_Input_Present_Value_Set(1,hall_sens_read()); - + Analog_Input_Present_Value_Set(0, temprature_sens_read()); + Analog_Input_Present_Value_Set(1, hall_sens_read()); } pdu_len = datalink_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout); - if (pdu_len) - { + if (pdu_len) { npdu_handler(&src, &Rx_Buf[0], pdu_len); - if(Binary_Output_Present_Value(0)==BINARY_ACTIVE) - gpio_set_level(BACNET_LED,1); + if (Binary_Output_Present_Value(0) == BINARY_ACTIVE) + gpio_set_level(BACNET_LED, 1); else - gpio_set_level(BACNET_LED,0); + gpio_set_level(BACNET_LED, 0); } handler_cov_task(); @@ -203,13 +198,13 @@ void BACnetTask(void *pvParameters) } /* Entry point */ void app_main() -{ - // Cannot run BACnet code here, the default stack size is to small : 4096 byte - xTaskCreate( - BACnetTask, /* Function to implement the task */ - "BACnetTask", /* Name of the task */ - 10000, /* Stack size in words */ - NULL, /* Task input parameter */ - 20, /* Priority of the task */ - NULL); /* Task handle. */ +{ + // Cannot run BACnet code here, the default stack size is to small : 4096 + // byte + xTaskCreate(BACnetTask, /* Function to implement the task */ + "BACnetTask", /* Name of the task */ + 10000, /* Stack size in words */ + NULL, /* Task input parameter */ + 20, /* Priority of the task */ + NULL); /* Task handle. */ } \ No newline at end of file diff --git a/ports/linux/arcnet.c b/ports/linux/arcnet.c index f56ae9dd..e0c4468c 100644 --- a/ports/linux/arcnet.c +++ b/ports/linux/arcnet.c @@ -50,7 +50,7 @@ static struct sockaddr ARCNET_Socket_Address; #define ARCNET_BROADCAST 0 /* -Hints: +Hints: When using a PCI20-485D ARCNET card from Contemporary Controls, you might need to know about the following settings: @@ -62,7 +62,7 @@ clockp Clock Prescaler DataRate 0 8 2.5 Mbps 1 16 1.25 Mbps 2 32 625 Kbps -3 64 312.5 Kbps +3 64 312.5 Kbps 4 128 156.25Kbps 1. Install the arcnet driver and arcnet raw mode driver: @@ -76,20 +76,18 @@ clockp Clock Prescaler DataRate on the back of the card. 0 is broadcast, so don't use 0. 4. The backplane mode on the PCI20-485D card is done in hardware, - so the driver does not need to do backplane mode. If you - use another type of PCI20 card, you could pass in backplane=1 or + so the driver does not need to do backplane mode. If you + use another type of PCI20 card, you could pass in backplane=1 or backplane=0 as an option to the modprobe of com20020_pci. */ -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); @@ -98,12 +96,11 @@ void arcnet_cleanup( 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 */ @@ -126,13 +123,15 @@ static int arcnet_bind( "You might need to add the following to modules.conf\n" "(or in /etc/modutils/alias on Debian with update-modules):\n" "alias net-pf-17 af_packet\n" - "Also, add af_packet to /etc/modules.\n" "Then follow it by:\n" + "Also, add af_packet to /etc/modules.\n" + "Then follow it by:\n" "# modprobe af_packet\n"); exit(-1); } if (ARCNET_Sock_FD >= 0) { - /* Bind the socket to an interface name so we only get packets from it */ + /* Bind the socket to an interface name so we only get packets from it + */ ARCNET_Socket_Address.sa_family = ARPHRD_ARCNET; /*ARCNET_Socket_Address.sa_family = PF_INET; */ /* Clear the memory before copying */ @@ -141,8 +140,8 @@ static int arcnet_bind( /* Strcpy the interface name into the address */ strncpy(ARCNET_Socket_Address.sa_data, interface_name, sizeof(ARCNET_Socket_Address.sa_data) - 1); - fprintf(stderr, "arcnet: binding \"%s\"\n", - ARCNET_Socket_Address.sa_data); + fprintf( + stderr, "arcnet: binding \"%s\"\n", ARCNET_Socket_Address.sa_data); if (bind(sock_fd, &ARCNET_Socket_Address, sizeof(ARCNET_Socket_Address)) != 0) { /* Bind problem, close socket and return */ @@ -152,7 +151,8 @@ static int arcnet_bind( "You might need to add the following to modules.conf\n" "(or in /etc/modutils/alias on Debian with update-modules):\n" "alias net-pf-17 af_packet\n" - "Also, add af_packet to /etc/modules.\n" "Then follow it by:\n" + "Also, add af_packet to /etc/modules.\n" + "Then follow it by:\n" "# modprobe af_packet\n"); /* Close the socket */ close(sock_fd); @@ -161,7 +161,7 @@ static int arcnet_bind( } 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; @@ -180,8 +180,7 @@ static int arcnet_bind( 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); @@ -193,19 +192,18 @@ bool arcnet_init( /* 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 */ - BACNET_NPDU_DATA * npdu_data, /* network information */ - uint8_t * pdu, /* any data to be sent - may be null */ +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 */ 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 }; int mtu_len = 0; - struct archdr *pkt = (struct archdr *) mtu; + struct archdr *pkt = (struct archdr *)mtu; - (void) npdu_data; + (void)npdu_data; src.mac[0] = ARCNET_MAC_Address; src.mac_len = 1; @@ -228,21 +226,20 @@ int arcnet_send_pdu( return -3; } /* Logical PDU portion */ - pkt->soft.raw[0] = 0xCD; /* SC for BACnet */ - pkt->soft.raw[1] = 0x82; /* DSAP for BACnet */ - pkt->soft.raw[2] = 0x82; /* SSAP for BACnet */ - pkt->soft.raw[3] = 0x03; /* LLC Control byte in header */ + pkt->soft.raw[0] = 0xCD; /* SC for BACnet */ + pkt->soft.raw[1] = 0x82; /* DSAP for BACnet */ + pkt->soft.raw[2] = 0x82; /* SSAP for BACnet */ + pkt->soft.raw[3] = 0x03; /* LLC Control byte in header */ /* packet length */ - mtu_len = ARC_HDR_SIZE + 4 /*SC,DSAP,SSAP,LLC */ + pdu_len; + mtu_len = ARC_HDR_SIZE + 4 /*SC,DSAP,SSAP,LLC */ + pdu_len; if (mtu_len > 512) { fprintf(stderr, "arcnet: PDU is too big to send!\n"); return -4; } memcpy(&pkt->soft.raw[4], pdu, pdu_len); /* Send the packet */ - bytes = - sendto(ARCNET_Sock_FD, &mtu, mtu_len, 0, - (struct sockaddr *) &ARCNET_Socket_Address, + bytes = sendto(ARCNET_Sock_FD, &mtu, mtu_len, 0, + (struct sockaddr *)&ARCNET_Socket_Address, sizeof(ARCNET_Socket_Address)); /* did it get sent? */ if (bytes < 0) @@ -253,19 +250,18 @@ int arcnet_send_pdu( /* 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 */ + uint8_t buf[512] = { 0 }; /* data */ + uint16_t pdu_len = 0; /* return value */ fd_set read_fds; int max; struct timeval select_timeout; - struct archdr *pkt = (struct archdr *) buf; + struct archdr *pkt = (struct archdr *)buf; /* Make sure the socket is open */ if (ARCNET_Sock_FD <= 0) @@ -307,7 +303,7 @@ uint16_t arcnet_receive( /* printf("arcnet: received %u bytes (offset=%02Xh %02Xh) " "from %02Xh (proto==%02Xh)\n", - received_bytes, pkt->offset[0], pkt->offset[1], + received_bytes, pkt->offset[0], pkt->offset[1], pkt->hard.source, pkt->soft.raw[0]); */ @@ -324,9 +320,9 @@ uint16_t arcnet_receive( fprintf(stderr, "arcnet: This packet is not for us.\n"); return 0; } - if ((pkt->soft.raw[1] != 0x82) || /* DSAP */ - (pkt->soft.raw[2] != 0x82) || /* LSAP */ - (pkt->soft.raw[3] != 0x03)) { /* LLC Control */ + if ((pkt->soft.raw[1] != 0x82) || /* DSAP */ + (pkt->soft.raw[2] != 0x82) || /* LSAP */ + (pkt->soft.raw[3] != 0x03)) { /* LLC Control */ fprintf(stderr, "arcnet: BACnet packet has invalid LLC.\n"); return 0; } @@ -341,7 +337,7 @@ uint16_t arcnet_receive( src->mac[0] = pkt->hard.source; /* compute the PDU length */ pdu_len = received_bytes - ARC_HDR_SIZE; - pdu_len -= 4 /* SC, DSAP, SSAP, LLC Control */ ; + pdu_len -= 4 /* SC, DSAP, SSAP, LLC Control */; /* copy the buffer into the PDU */ if (pdu_len < max_pdu) memmove(&pdu[0], &pkt->soft.raw[4], pdu_len); @@ -352,14 +348,13 @@ uint16_t arcnet_receive( return pdu_len; } -void arcnet_get_my_address( - BACNET_ADDRESS * my_address) +void arcnet_get_my_address(BACNET_ADDRESS *my_address) { int i = 0; my_address->mac_len = 1; my_address->mac[0] = ARCNET_MAC_Address; - my_address->net = 0; /* DNET=0 is local only, no routing */ + my_address->net = 0; /* DNET=0 is local only, no routing */ my_address->len = 0; for (i = 0; i < MAX_MAC_LEN; i++) { my_address->adr[i] = 0; @@ -368,16 +363,15 @@ void arcnet_get_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/ports/linux/bip6.c b/ports/linux/bip6.c index d35d1f9a..ec0ba002 100644 --- a/ports/linux/bip6.c +++ b/ports/linux/bip6.c @@ -35,8 +35,8 @@ #include #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 "bacnet/bacdcode.h" #include "bacnet/config.h" #include "bacnet/datalink/bip6.h" @@ -45,16 +45,16 @@ #include "bacnet/basic/bbmd6/h_bbmd6.h" #include "bacport.h" -static void debug_print_ipv6(const char *str, const struct in6_addr * addr) { - debug_printf( "BIP6: %s %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n", - str, - (int)addr->s6_addr[0], (int)addr->s6_addr[1], - (int)addr->s6_addr[2], (int)addr->s6_addr[3], - (int)addr->s6_addr[4], (int)addr->s6_addr[5], - (int)addr->s6_addr[6], (int)addr->s6_addr[7], - (int)addr->s6_addr[8], (int)addr->s6_addr[9], - (int)addr->s6_addr[10], (int)addr->s6_addr[11], - (int)addr->s6_addr[12], (int)addr->s6_addr[13], +static void debug_print_ipv6(const char *str, const struct in6_addr *addr) +{ + debug_printf("BIP6: %s " + "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%" + "02x:%02x%02x\n", + str, (int)addr->s6_addr[0], (int)addr->s6_addr[1], + (int)addr->s6_addr[2], (int)addr->s6_addr[3], (int)addr->s6_addr[4], + (int)addr->s6_addr[5], (int)addr->s6_addr[6], (int)addr->s6_addr[7], + (int)addr->s6_addr[8], (int)addr->s6_addr[9], (int)addr->s6_addr[10], + (int)addr->s6_addr[11], (int)addr->s6_addr[12], (int)addr->s6_addr[13], (int)addr->s6_addr[14], (int)addr->s6_addr[15]); } @@ -71,8 +71,7 @@ static BACNET_IP6_ADDRESS BIP6_Broadcast_Addr; * * @param ifname - C string for name or text address */ -void bip6_set_interface( - char *ifname) +void bip6_set_interface(char *ifname) { struct ifaddrs *ifa, *ifa_tmp; struct sockaddr_in6 *sin; @@ -85,16 +84,13 @@ void bip6_set_interface( ifa_tmp = ifa; debug_printf("BIP6: seeking interface: %s\n", ifname); while (ifa_tmp) { - if ((ifa_tmp->ifa_addr) && - (ifa_tmp->ifa_addr->sa_family == AF_INET6)) { + if ((ifa_tmp->ifa_addr) && (ifa_tmp->ifa_addr->sa_family == AF_INET6)) { debug_printf("BIP6: found interface: %s\n", ifa_tmp->ifa_name); } - if ((ifa_tmp->ifa_addr) && - (ifa_tmp->ifa_addr->sa_family == AF_INET6) && - (strcasecmp(ifa_tmp->ifa_name,ifname) == 0)) { - sin = (struct sockaddr_in6*) ifa_tmp->ifa_addr; - bvlc6_address_set(&BIP6_Addr, - ntohs(sin->sin6_addr.s6_addr16[0]), + if ((ifa_tmp->ifa_addr) && (ifa_tmp->ifa_addr->sa_family == AF_INET6) && + (strcasecmp(ifa_tmp->ifa_name, ifname) == 0)) { + sin = (struct sockaddr_in6 *)ifa_tmp->ifa_addr; + bvlc6_address_set(&BIP6_Addr, ntohs(sin->sin6_addr.s6_addr16[0]), ntohs(sin->sin6_addr.s6_addr16[1]), ntohs(sin->sin6_addr.s6_addr16[2]), ntohs(sin->sin6_addr.s6_addr16[3]), @@ -119,8 +115,7 @@ void bip6_set_interface( * * @param port - IPv6 UDP port number */ -void bip6_set_port( - uint16_t port) +void bip6_set_port(uint16_t port) { BIP6_Addr.port = port; BIP6_Broadcast_Addr.port = port; @@ -131,8 +126,7 @@ void bip6_set_port( * * @return IPv6 UDP port number */ -uint16_t bip6_get_port( - void) +uint16_t bip6_get_port(void) { return BIP6_Addr.port; } @@ -143,8 +137,7 @@ uint16_t bip6_get_port( * * @param addr - IPv6 source address */ -void bip6_get_broadcast_address( - BACNET_ADDRESS * addr) +void bip6_get_broadcast_address(BACNET_ADDRESS *addr) { if (addr) { addr->net = BACNET_BROADCAST_NETWORK; @@ -158,8 +151,7 @@ void bip6_get_broadcast_address( * * @param addr - IPv6 source address */ -void bip6_get_my_address( - BACNET_ADDRESS * addr) +void bip6_get_my_address(BACNET_ADDRESS *addr) { uint32_t device_id = 0; @@ -174,8 +166,7 @@ void bip6_get_my_address( * * @param addr - network IPv6 address */ -bool bip6_set_addr( - BACNET_IP6_ADDRESS *addr) +bool bip6_set_addr(BACNET_IP6_ADDRESS *addr) { return bvlc6_address_copy(&BIP6_Addr, addr); } @@ -185,8 +176,7 @@ bool bip6_set_addr( * * @return BACnet/IP address */ -bool bip6_get_addr( - BACNET_IP6_ADDRESS *addr) +bool bip6_get_addr(BACNET_IP6_ADDRESS *addr) { return bvlc6_address_copy(addr, &BIP6_Addr); } @@ -196,8 +186,7 @@ bool bip6_get_addr( * * @param addr - network IPv6 address */ -bool bip6_set_broadcast_addr( - BACNET_IP6_ADDRESS *addr) +bool bip6_set_broadcast_addr(BACNET_IP6_ADDRESS *addr) { return bvlc6_address_copy(&BIP6_Broadcast_Addr, addr); } @@ -207,8 +196,7 @@ bool bip6_set_broadcast_addr( * * @return BACnet/IP address */ -bool bip6_get_broadcast_addr( - BACNET_IP6_ADDRESS *addr) +bool bip6_get_broadcast_addr(BACNET_IP6_ADDRESS *addr) { return bvlc6_address_copy(addr, &BIP6_Broadcast_Addr); } @@ -224,10 +212,7 @@ bool bip6_get_broadcast_addr( * @return Upon successful completion, returns the number of bytes sent. * Otherwise, -1 shall be returned and errno set to indicate the error. */ -int bip6_send_mpdu( - BACNET_IP6_ADDRESS *dest, - uint8_t * mtu, - uint16_t mtu_len) +int bip6_send_mpdu(BACNET_IP6_ADDRESS *dest, uint8_t *mtu, uint16_t mtu_len) { struct sockaddr_in6 bvlc_dest = { 0 }; uint16_t addr16[8]; @@ -251,8 +236,8 @@ int bip6_send_mpdu( bvlc_dest.sin6_port = htons(dest->port); debug_print_ipv6("Sending MPDU->", &bvlc_dest.sin6_addr); /* Send the packet */ - return sendto(BIP6_Socket, (char *) mtu, mtu_len, 0, - (struct sockaddr *) &bvlc_dest, sizeof(bvlc_dest)); + return sendto(BIP6_Socket, (char *)mtu, mtu_len, 0, + (struct sockaddr *)&bvlc_dest, sizeof(bvlc_dest)); } /** @@ -286,17 +271,14 @@ int bip6_send_pdu(BACNET_ADDRESS *dest, * @return Number of bytes received, or 0 if none or timeout. */ uint16_t bip6_receive( - BACNET_ADDRESS * src, - uint8_t * npdu, - uint16_t max_npdu, - unsigned timeout) + BACNET_ADDRESS *src, uint8_t *npdu, uint16_t max_npdu, unsigned timeout) { uint16_t npdu_len = 0; /* return value */ fd_set read_fds; int max = 0; struct timeval select_timeout; struct sockaddr_in6 sin = { 0 }; - BACNET_IP6_ADDRESS addr = {{ 0 }}; + BACNET_IP6_ADDRESS addr = { { 0 } }; socklen_t sin_len = sizeof(sin); int received_bytes = 0; int offset = 0; @@ -322,9 +304,8 @@ uint16_t bip6_receive( max = BIP6_Socket; /* see if there is a packet for us */ if (select(max + 1, &read_fds, NULL, NULL, &select_timeout) > 0) { - received_bytes = - recvfrom(BIP6_Socket, (char *) &npdu[0], max_npdu, 0, - (struct sockaddr *) &sin, &sin_len); + received_bytes = recvfrom(BIP6_Socket, (char *)&npdu[0], max_npdu, 0, + (struct sockaddr *)&sin, &sin_len); } else { return 0; } @@ -342,14 +323,10 @@ uint16_t bip6_receive( } /* pass the packet into the BBMD handler */ debug_print_ipv6("Received MPDU->", &sin.sin6_addr); - bvlc6_address_set(&addr, - ntohs(sin.sin6_addr.s6_addr16[0]), - ntohs(sin.sin6_addr.s6_addr16[1]), - ntohs(sin.sin6_addr.s6_addr16[2]), - ntohs(sin.sin6_addr.s6_addr16[3]), - ntohs(sin.sin6_addr.s6_addr16[4]), - ntohs(sin.sin6_addr.s6_addr16[5]), - ntohs(sin.sin6_addr.s6_addr16[6]), + bvlc6_address_set(&addr, ntohs(sin.sin6_addr.s6_addr16[0]), + ntohs(sin.sin6_addr.s6_addr16[1]), ntohs(sin.sin6_addr.s6_addr16[2]), + ntohs(sin.sin6_addr.s6_addr16[3]), ntohs(sin.sin6_addr.s6_addr16[4]), + ntohs(sin.sin6_addr.s6_addr16[5]), ntohs(sin.sin6_addr.s6_addr16[6]), ntohs(sin.sin6_addr.s6_addr16[7])); addr.port = ntohs(sin.sin6_port); offset = bvlc6_handler(&addr, src, npdu, received_bytes); @@ -370,9 +347,8 @@ uint16_t bip6_receive( /** Cleanup and close out the BACnet/IP services by closing the socket. * @ingroup DLBIP6 - */ -void bip6_cleanup( - void) + */ +void bip6_cleanup(void) { if (BIP6_Socket != -1) { close(BIP6_Socket); @@ -399,11 +375,10 @@ void bip6_cleanup( * @return True if the socket is successfully opened for BACnet/IP, * else False if the socket functions fail. */ -bool bip6_init( - char *ifname) +bool bip6_init(char *ifname) { - int status = 0; /* return from socket lib calls */ - struct sockaddr_in6 server = {0}; + int status = 0; /* return from socket lib calls */ + struct sockaddr_in6 server = { 0 }; struct in6_addr broadcast_address; struct ipv6_mreq join_request; int sockopt = 0; @@ -418,9 +393,8 @@ bool bip6_init( } debug_printf("BIP6: IPv6 UDP port: 0x%04X\n", htons(BIP6_Addr.port)); if (BIP6_Broadcast_Addr.address[0] == 0) { - bvlc6_address_set(&BIP6_Broadcast_Addr, - BIP6_MULTICAST_SITE_LOCAL, 0, 0, 0, 0, 0, 0, - BIP6_MULTICAST_GROUP_ID); + bvlc6_address_set(&BIP6_Broadcast_Addr, BIP6_MULTICAST_SITE_LOCAL, 0, 0, + 0, 0, 0, 0, BIP6_MULTICAST_GROUP_ID); } /* assumes that the driver has already been initialized */ BIP6_Socket = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); @@ -429,26 +403,26 @@ bool bip6_init( /* Allow us to use the same socket for sending and receiving */ /* This makes sure that the src port is correct when sending */ sockopt = 1; - status = - setsockopt(BIP6_Socket, SOL_SOCKET, SO_REUSEADDR, &sockopt, - sizeof(sockopt)); + status = setsockopt( + BIP6_Socket, SOL_SOCKET, SO_REUSEADDR, &sockopt, sizeof(sockopt)); if (status < 0) { close(BIP6_Socket); BIP6_Socket = -1; return status; } /* allow us to send a broadcast */ - status = - setsockopt(BIP6_Socket, SOL_SOCKET, SO_BROADCAST, &sockopt, - sizeof(sockopt)); + status = setsockopt( + BIP6_Socket, SOL_SOCKET, SO_BROADCAST, &sockopt, sizeof(sockopt)); if (status < 0) { close(BIP6_Socket); BIP6_Socket = -1; return false; } /* subscribe to a multicast address */ - memcpy(&broadcast_address.s6_addr[0], &BIP6_Broadcast_Addr.address[0], IP6_ADDRESS_MAX); - memcpy(&join_request.ipv6mr_multiaddr, &broadcast_address, sizeof(struct in6_addr)); + memcpy(&broadcast_address.s6_addr[0], &BIP6_Broadcast_Addr.address[0], + IP6_ADDRESS_MAX); + memcpy(&join_request.ipv6mr_multiaddr, &broadcast_address, + sizeof(struct in6_addr)); /* Let system choose the interface */ join_request.ipv6mr_interface = 0; status = setsockopt(BIP6_Socket, IPPROTO_IPV6, IPV6_JOIN_GROUP, @@ -461,7 +435,7 @@ bool bip6_init( server.sin6_family = AF_INET6; server.sin6_addr = in6addr_any; server.sin6_port = htons(BIP6_Addr.port); - status = bind(BIP6_Socket, (const void*)&server, sizeof(server)); + status = bind(BIP6_Socket, (const void *)&server, sizeof(server)); if (status < 0) { perror("BIP: bind"); close(BIP6_Socket); diff --git a/ports/linux/datetime-init.c b/ports/linux/datetime-init.c index 65d7c4bd..705302a8 100644 --- a/ports/linux/datetime-init.c +++ b/ports/linux/datetime-init.c @@ -27,11 +27,10 @@ * @param true if DST is enabled and active * @return true if local time was retrieved */ -bool datetime_local( - BACNET_DATE * bdate, - BACNET_TIME * btime, - int16_t * utc_offset_minutes, - bool * dst_active) +bool datetime_local(BACNET_DATE *bdate, + BACNET_TIME *btime, + int16_t *utc_offset_minutes, + bool *dst_active) { bool status = false; struct tm *tblock = NULL; @@ -54,11 +53,10 @@ bool datetime_local( * int tm_isdst Daylight Savings flag. */ datetime_set_date(bdate, (uint16_t)tblock->tm_year + 1900, - (uint8_t)tblock->tm_mon + 1, - (uint8_t)tblock->tm_mday); + (uint8_t)tblock->tm_mon + 1, (uint8_t)tblock->tm_mday); datetime_set_time(btime, (uint8_t)tblock->tm_hour, - (uint8_t)tblock->tm_min, (uint8_t)tblock->tm_sec, - (uint8_t)(tv.tv_usec / 10000)); + (uint8_t)tblock->tm_min, (uint8_t)tblock->tm_sec, + (uint8_t)(tv.tv_usec / 10000)); if (dst_active) { /* The value of tm_isdst is: - positive if Daylight Saving Time is in effect, diff --git a/ports/linux/dlmstp.c b/ports/linux/dlmstp.c index cea55170..f6cbbc3c 100644 --- a/ports/linux/dlmstp.c +++ b/ports/linux/dlmstp.c @@ -1,28 +1,28 @@ /************************************************************************** -* -* Copyright (C) 2008 Steve Karg -* Updated by Nikola Jelic 2011 -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2008 Steve Karg + * Updated by Nikola Jelic 2011 + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include #include @@ -42,7 +42,8 @@ /* OS Specific include */ #include "bacport.h" -/** @file linux/dlmstp.c Provides Linux-specific DataLink functions for MS/TP. */ +/** @file linux/dlmstp.c Provides Linux-specific DataLink functions for MS/TP. + */ /* Number of MS/TP Packets Rx/Tx */ uint16_t MSTP_Packets = 0; @@ -108,24 +109,23 @@ static struct timespec start; * * @returns True if the difference is negative, otherwise 0. */ -static int timespec_subtract(struct timespec *result, - const struct timespec *l, - const struct timespec *r) +static int timespec_subtract( + struct timespec *result, const struct timespec *l, const struct timespec *r) { -# define NS_PER_S 1000000000 // nano-seconds per second +#define NS_PER_S 1000000000 // nano-seconds per second struct timespec right = *r; int secs; // Perform the carry for the later subtraction by updating y. if (l->tv_nsec < right.tv_nsec) { - secs = (right.tv_nsec - l->tv_nsec) / NS_PER_S + 1; - right.tv_nsec -= NS_PER_S * secs; - right.tv_sec += secs; + secs = (right.tv_nsec - l->tv_nsec) / NS_PER_S + 1; + right.tv_nsec -= NS_PER_S * secs; + right.tv_sec += secs; } if (l->tv_nsec - right.tv_nsec > NS_PER_S) { - secs = (l->tv_nsec - right.tv_nsec) / NS_PER_S; - right.tv_nsec += NS_PER_S * secs; - right.tv_sec -= secs; + secs = (l->tv_nsec - right.tv_nsec) / NS_PER_S; + right.tv_nsec += NS_PER_S * secs; + right.tv_sec -= secs; } // Compute the time remaining. tv_nsec is certainly positive. @@ -144,18 +144,17 @@ static int timespec_subtract(struct timespec *result, */ static void timespec_add_ns(struct timespec *ts, long ns) { - ts->tv_nsec += ns; - if (ts->tv_nsec > NS_PER_S) { - ts->tv_nsec -= NS_PER_S; - ts->tv_sec += 1; - } else if (ts->tv_nsec < 0) { - ts->tv_nsec += NS_PER_S; - ts->tv_sec -= 1; - } + ts->tv_nsec += ns; + if (ts->tv_nsec > NS_PER_S) { + ts->tv_nsec -= NS_PER_S; + ts->tv_sec += 1; + } else if (ts->tv_nsec < 0) { + ts->tv_nsec += NS_PER_S; + ts->tv_sec -= 1; + } } -static uint32_t Timer_Silence( - void *pArg) +static uint32_t Timer_Silence(void *pArg) { struct timespec now, diff; int32_t res; @@ -167,27 +166,23 @@ static uint32_t Timer_Silence( return (res >= 0 ? res : 0); } -static void Timer_Silence_Reset( - void *pArg) +static void Timer_Silence_Reset(void *pArg) { clock_gettime(CLOCK_MONOTONIC, &start); } -static void get_abstime( - struct timespec *abstime, - unsigned long milliseconds) +static void get_abstime(struct timespec *abstime, unsigned long milliseconds) { clock_gettime(CLOCK_MONOTONIC, abstime); if (milliseconds > 1000) { - fprintf(stderr, "DLMSTP: limited timeout of %lums to 1000ms\n", - milliseconds); - milliseconds = 1000; + fprintf(stderr, "DLMSTP: limited timeout of %lums to 1000ms\n", + milliseconds); + milliseconds = 1000; } timespec_add_ns(abstime, 1000000 * milliseconds); } -void dlmstp_cleanup( - void) +void dlmstp_cleanup(void) { pthread_cond_destroy(&Received_Frame_Flag); pthread_cond_destroy(&Receive_Packet_Flag); @@ -198,17 +193,16 @@ void dlmstp_cleanup( } /* 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 */ +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 */ +{ /* number of bytes of data */ int bytes_sent = 0; struct mstp_pdu_packet *pkt; unsigned i = 0; - pkt = (struct mstp_pdu_packet *) Ringbuf_Data_Peek(&PDU_Queue); + pkt = (struct mstp_pdu_packet *)Ringbuf_Data_Peek(&PDU_Queue); if (pkt) { pkt->data_expecting_reply = npdu_data->data_expecting_reply; for (i = 0; i < pdu_len; i++) { @@ -229,22 +223,21 @@ int dlmstp_send_pdu( return bytes_sent; } -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) -{ /* milliseconds to wait for a packet */ +{ /* milliseconds to wait for a packet */ uint16_t pdu_len = 0; struct timespec abstime; - (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 */ pthread_mutex_lock(&Receive_Packet_Mutex); get_abstime(&abstime, timeout); - pthread_cond_timedwait(&Receive_Packet_Flag, &Receive_Packet_Mutex, - &abstime); + pthread_cond_timedwait( + &Receive_Packet_Flag, &Receive_Packet_Mutex, &abstime); if (Receive_Packet.ready) { if (Receive_Packet.pdu_len) { MSTP_Packets++; @@ -253,8 +246,7 @@ uint16_t dlmstp_receive( sizeof(Receive_Packet.address)); } if (pdu) { - memmove(pdu, &Receive_Packet.pdu, - sizeof(Receive_Packet.pdu)); + memmove(pdu, &Receive_Packet.pdu, sizeof(Receive_Packet.pdu)); } pdu_len = Receive_Packet.pdu_len; } @@ -265,13 +257,12 @@ uint16_t dlmstp_receive( return pdu_len; } -static void *dlmstp_master_fsm_task( - void *pArg) +static void *dlmstp_master_fsm_task(void *pArg) { uint32_t silence = 0; bool run_master = false; - (void) pArg; + (void)pArg; for (;;) { if (MSTP_Port.ReceivedValidFrame == false && MSTP_Port.ReceivedInvalidFrame == false) { @@ -314,9 +305,7 @@ static void *dlmstp_master_fsm_task( 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; @@ -340,8 +329,7 @@ void dlmstp_fill_bacnet_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) +uint16_t MSTP_Put_Receive(volatile struct mstp_port_struct_t *mstp_port) { uint16_t pdu_len = 0; @@ -357,10 +345,10 @@ uint16_t MSTP_Put_Receive( if (pdu_len == 0) { debug_printf("MS/TP: PDU Length is 0!\n"); } - memmove((void *) &Receive_Packet.pdu[0], - (void *) &mstp_port->InputBuffer[0], pdu_len); - dlmstp_fill_bacnet_address(&Receive_Packet.address, - mstp_port->SourceAddress); + 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; Receive_Packet.ready = true; pthread_cond_signal(&Receive_Packet_Flag); @@ -373,37 +361,36 @@ 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 frame_type = 0; struct mstp_pdu_packet *pkt; - (void) timeout; + (void)timeout; if (Ringbuf_Empty(&PDU_Queue)) { return 0; } - pkt = (struct mstp_pdu_packet *) Ringbuf_Peek(&PDU_Queue); + pkt = (struct mstp_pdu_packet *)Ringbuf_Peek(&PDU_Queue); if (pkt->data_expecting_reply) { frame_type = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY; } else { frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; } /* convert the PDU into the MSTP Frame */ - pdu_len = MSTP_Create_Frame(&mstp_port->OutputBuffer[0], /* <-- loading this */ - mstp_port->OutputBufferSize, frame_type, pkt->destination_mac, - mstp_port->This_Station, (uint8_t *) & pkt->buffer[0], pkt->length); - (void) Ringbuf_Pop(&PDU_Queue, NULL); + pdu_len = + MSTP_Create_Frame(&mstp_port->OutputBuffer[0], /* <-- loading this */ + mstp_port->OutputBufferSize, frame_type, pkt->destination_mac, + mstp_port->This_Station, (uint8_t *)&pkt->buffer[0], pkt->length); + (void)Ringbuf_Pop(&PDU_Queue, NULL); return pdu_len; } -static bool dlmstp_compare_data_expecting_reply( - uint8_t * request_pdu, +static bool dlmstp_compare_data_expecting_reply(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) { @@ -427,13 +414,13 @@ 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], NULL, &request.address, - &request.npdu_data); + offset = npdu_decode( + &request_pdu[0], NULL, &request.address, &request.npdu_data); if (request.npdu_data.network_layer_message) { #if PRINT_ENABLED fprintf(stderr, - "DLMSTP: DER Compare failed: " "Request is Network message.\n"); + "DLMSTP: DER Compare failed: " + "Request is Network message.\n"); #endif return false; } @@ -441,7 +428,8 @@ static bool dlmstp_compare_data_expecting_reply( if (request.pdu_type != PDU_TYPE_CONFIRMED_SERVICE_REQUEST) { #if PRINT_ENABLED fprintf(stderr, - "DLMSTP: DER Compare failed: " "Not Confirmed Request.\n"); + "DLMSTP: DER Compare failed: " + "Not Confirmed Request.\n"); #endif return false; } @@ -455,12 +443,12 @@ static bool dlmstp_compare_data_expecting_reply( /* decode the reply data */ reply.address.mac[0] = dest_address; reply.address.mac_len = 1; - offset = - npdu_decode(&reply_pdu[0], &reply.address, NULL, &reply.npdu_data); + offset = npdu_decode(&reply_pdu[0], &reply.address, NULL, &reply.npdu_data); if (reply.npdu_data.network_layer_message) { #if PRINT_ENABLED fprintf(stderr, - "DLMSTP: DER Compare failed: " "Reply is Network message.\n"); + "DLMSTP: DER Compare failed: " + "Reply is Network message.\n"); #endif return false; } @@ -498,7 +486,8 @@ static bool dlmstp_compare_data_expecting_reply( if (request.invoke_id != reply.invoke_id) { #if PRINT_ENABLED fprintf(stderr, - "DLMSTP: DER Compare failed: " "Invoke ID mismatch.\n"); + "DLMSTP: DER Compare failed: " + "Invoke ID mismatch.\n"); #endif return false; } @@ -506,19 +495,22 @@ static bool dlmstp_compare_data_expecting_reply( if (request.invoke_id != reply.invoke_id) { #if PRINT_ENABLED fprintf(stderr, - "DLMSTP: DER Compare failed: " "Invoke ID mismatch.\n"); + "DLMSTP: DER Compare failed: " + "Invoke ID mismatch.\n"); #endif return false; } if (request.service_choice != reply.service_choice) { #if PRINT_ENABLED fprintf(stderr, - "DLMSTP: DER Compare failed: " "Service choice mismatch.\n"); + "DLMSTP: DER Compare failed: " + "Service choice mismatch.\n"); #endif return false; } } - if (request.npdu_data.protocol_version != reply.npdu_data.protocol_version) { + if (request.npdu_data.protocol_version != + reply.npdu_data.protocol_version) { #if PRINT_ENABLED fprintf(stderr, "DLMSTP: DER Compare failed: " @@ -540,7 +532,8 @@ static bool dlmstp_compare_data_expecting_reply( if (!bacnet_address_same(&request.address, &reply.address)) { #if PRINT_ENABLED fprintf(stderr, - "DLMSTP: DER Compare failed: " "BACnet Address mismatch.\n"); + "DLMSTP: DER Compare failed: " + "BACnet Address mismatch.\n"); #endif return false; } @@ -550,24 +543,22 @@ static 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 */ bool matched = false; uint8_t frame_type = 0; struct mstp_pdu_packet *pkt; - (void) timeout; + (void)timeout; if (Ringbuf_Empty(&PDU_Queue)) { return 0; } - pkt = (struct mstp_pdu_packet *) Ringbuf_Peek(&PDU_Queue); + pkt = (struct mstp_pdu_packet *)Ringbuf_Peek(&PDU_Queue); /* 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, - (uint8_t *) & pkt->buffer[0], pkt->length, pkt->destination_mac); + (uint8_t *)&pkt->buffer[0], pkt->length, pkt->destination_mac); if (!matched) { return 0; } @@ -577,16 +568,16 @@ uint16_t MSTP_Get_Reply( frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; } /* convert the PDU into the MSTP Frame */ - pdu_len = MSTP_Create_Frame(&mstp_port->OutputBuffer[0], /* <-- loading this */ - mstp_port->OutputBufferSize, frame_type, pkt->destination_mac, - mstp_port->This_Station, (uint8_t *) & pkt->buffer[0], pkt->length); - (void) Ringbuf_Pop(&PDU_Queue, NULL); + pdu_len = + MSTP_Create_Frame(&mstp_port->OutputBuffer[0], /* <-- loading this */ + mstp_port->OutputBufferSize, frame_type, pkt->destination_mac, + mstp_port->This_Station, (uint8_t *)&pkt->buffer[0], pkt->length); + (void)Ringbuf_Pop(&PDU_Queue, NULL); 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) { @@ -603,8 +594,7 @@ void dlmstp_set_mac_address( return; } -uint8_t dlmstp_mac_address( - void) +uint8_t dlmstp_mac_address(void) { return MSTP_Port.This_Station; } @@ -616,8 +606,7 @@ uint8_t dlmstp_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( - 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; @@ -631,8 +620,7 @@ void dlmstp_set_max_info_frames( return; } -uint8_t dlmstp_max_info_frames( - void) +uint8_t dlmstp_max_info_frames(void) { return MSTP_Port.Nmax_info_frames; } @@ -642,8 +630,7 @@ uint8_t dlmstp_max_info_frames( /* 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) { @@ -659,33 +646,29 @@ void dlmstp_set_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(); } -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; - my_address->net = 0; /* local only, no routing */ + my_address->net = 0; /* local only, no routing */ my_address->len = 0; for (i = 0; i < MAX_MAC_LEN; i++) { my_address->adr[i] = 0; @@ -694,16 +677,15 @@ void dlmstp_get_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; } @@ -712,8 +694,7 @@ void dlmstp_get_broadcast_address( return; } -bool dlmstp_init( - char *ifname) +bool dlmstp_init(char *ifname) { unsigned long hThread = 0; pthread_condattr_t attr; @@ -721,13 +702,13 @@ bool dlmstp_init( pthread_condattr_init(&attr); if ((rv = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC)) != 0) { - fprintf(stderr, "MS/TP Interface: %s\n failed to set MONOTONIC clock\n", - ifname); - exit(1); + fprintf(stderr, "MS/TP Interface: %s\n failed to set MONOTONIC clock\n", + ifname); + exit(1); } /* initialize PDU queue */ - Ringbuf_Init(&PDU_Queue, (uint8_t *) & PDU_Buffer, + Ringbuf_Init(&PDU_Queue, (uint8_t *)&PDU_Buffer, sizeof(struct mstp_pdu_packet), MSTP_PDU_PACKET_COUNT); /* initialize packet queue */ Receive_Packet.ready = false; @@ -782,21 +763,18 @@ bool dlmstp_init( #ifdef TEST_DLMSTP #include -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 */ - (void) src; - (void) apdu; - (void) pdu_len; +{ /* for confirmed messages */ + (void)src; + (void)apdu; + (void)pdu_len; } static char *Network_Interface = NULL; -int main( - int argc, - char *argv[]) +int main(int argc, char *argv[]) { uint16_t pdu_len = 0; diff --git a/ports/linux/dlmstp_linux.c b/ports/linux/dlmstp_linux.c index f97b5c67..ce924f02 100644 --- a/ports/linux/dlmstp_linux.c +++ b/ports/linux/dlmstp_linux.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2008 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2008 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include #include @@ -41,24 +41,28 @@ #include "bacport.h" #include "bacnet/basic/sys/ringbuf.h" -/** @file linux/dlmstp.c Provides Linux-specific DataLink functions for MS/TP. */ +/** @file linux/dlmstp.c Provides Linux-specific DataLink functions for MS/TP. + */ #define BACNET_PDU_CONTROL_BYTE_OFFSET 1 #define BACNET_DATA_EXPECTING_REPLY_BIT 2 -#define BACNET_DATA_EXPECTING_REPLY(control) ( (control & (1 << BACNET_DATA_EXPECTING_REPLY_BIT) ) > 0 ) +#define BACNET_DATA_EXPECTING_REPLY(control) \ + ((control & (1 << BACNET_DATA_EXPECTING_REPLY_BIT)) > 0) -#define INCREMENT_AND_LIMIT_UINT16(x) {if (x < 0xFFFF) x++;} -uint32_t Timer_Silence( - void *poPort) +#define INCREMENT_AND_LIMIT_UINT16(x) \ + { \ + if (x < 0xFFFF) \ + x++; \ + } +uint32_t Timer_Silence(void *poPort) { struct timeval now, tmp_diff; SHARED_MSTP_DATA *poSharedData; - struct mstp_port_struct_t *mstp_port = - (struct mstp_port_struct_t *) poPort; + struct mstp_port_struct_t *mstp_port = (struct mstp_port_struct_t *)poPort; if (!mstp_port) { return -1; } - poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData; + poSharedData = (SHARED_MSTP_DATA *)mstp_port->UserData; if (!poSharedData) { return -1; } @@ -72,16 +76,14 @@ uint32_t Timer_Silence( return (res >= 0 ? res : -res); } -void Timer_Silence_Reset( - void *poPort) +void Timer_Silence_Reset(void *poPort) { SHARED_MSTP_DATA *poSharedData; - struct mstp_port_struct_t *mstp_port = - (struct mstp_port_struct_t *) poPort; + struct mstp_port_struct_t *mstp_port = (struct mstp_port_struct_t *)poPort; if (!mstp_port) { return; } - poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData; + poSharedData = (SHARED_MSTP_DATA *)mstp_port->UserData; if (!poSharedData) { return; } @@ -89,9 +91,7 @@ void Timer_Silence_Reset( gettimeofday(&poSharedData->start, NULL); } -void get_abstime( - struct timespec *abstime, - unsigned long milliseconds) +void get_abstime(struct timespec *abstime, unsigned long milliseconds) { struct timeval now, offset, result; @@ -103,23 +103,20 @@ void get_abstime( abstime->tv_nsec = result.tv_usec * 1000; } -void dlmstp_cleanup( - void *poPort) +void dlmstp_cleanup(void *poPort) { SHARED_MSTP_DATA *poSharedData; - struct mstp_port_struct_t *mstp_port = - (struct mstp_port_struct_t *) poPort; + struct mstp_port_struct_t *mstp_port = (struct mstp_port_struct_t *)poPort; if (!mstp_port) { return; } - poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData; + poSharedData = (SHARED_MSTP_DATA *)mstp_port->UserData; if (!poSharedData) { return; } /* restore the old port settings */ - tcsetattr(poSharedData->RS485_Handle, TCSANOW, - &poSharedData->RS485_oldtio); + tcsetattr(poSharedData->RS485_Handle, TCSANOW, &poSharedData->RS485_oldtio); close(poSharedData->RS485_Handle); pthread_cond_destroy(&poSharedData->Received_Frame_Flag); @@ -130,27 +127,25 @@ void dlmstp_cleanup( } /* returns number of bytes sent on success, zero on failure */ -int dlmstp_send_pdu( - void *poPort, - BACNET_ADDRESS * dest, /* destination address */ - uint8_t * pdu, /* any data to be sent - may be null */ +int dlmstp_send_pdu(void *poPort, + BACNET_ADDRESS *dest, /* destination address */ + 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; struct mstp_pdu_packet *pkt; unsigned i = 0; SHARED_MSTP_DATA *poSharedData; - struct mstp_port_struct_t *mstp_port = - (struct mstp_port_struct_t *) poPort; + struct mstp_port_struct_t *mstp_port = (struct mstp_port_struct_t *)poPort; if (!mstp_port) { return 0; } - poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData; + poSharedData = (SHARED_MSTP_DATA *)mstp_port->UserData; if (!poSharedData) { return 0; } - pkt = (struct mstp_pdu_packet *) Ringbuf_Data_Peek(&poSharedData->PDU_Queue); + pkt = (struct mstp_pdu_packet *)Ringbuf_Data_Peek(&poSharedData->PDU_Queue); if (pkt) { pkt->data_expecting_reply = BACNET_DATA_EXPECTING_REPLY(pdu[BACNET_PDU_CONTROL_BYTE_OFFSET]); @@ -167,27 +162,25 @@ int dlmstp_send_pdu( return bytes_sent; } -uint16_t dlmstp_receive( - void *poPort, - 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(void *poPort, + 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 */ uint16_t pdu_len = 0; struct timespec abstime; int rv = 0; SHARED_MSTP_DATA *poSharedData; - struct mstp_port_struct_t *mstp_port = - (struct mstp_port_struct_t *) poPort; + struct mstp_port_struct_t *mstp_port = (struct mstp_port_struct_t *)poPort; if (!mstp_port) { return 0; } - poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData; + poSharedData = (SHARED_MSTP_DATA *)mstp_port->UserData; if (!poSharedData) { return 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 */ get_abstime(&abstime, timeout); @@ -213,18 +206,17 @@ uint16_t dlmstp_receive( return pdu_len; } -void *dlmstp_receive_fsm_task( - void *pArg) +void *dlmstp_receive_fsm_task(void *pArg) { bool received_frame; SHARED_MSTP_DATA *poSharedData; - struct mstp_port_struct_t *mstp_port = (struct mstp_port_struct_t *) pArg; + struct mstp_port_struct_t *mstp_port = (struct mstp_port_struct_t *)pArg; if (!mstp_port) { return NULL; } poSharedData = - (SHARED_MSTP_DATA *) ((struct mstp_port_struct_t *) pArg)->UserData; + (SHARED_MSTP_DATA *)((struct mstp_port_struct_t *)pArg)->UserData; if (!poSharedData) { return NULL; } @@ -235,8 +227,8 @@ void *dlmstp_receive_fsm_task( (mstp_port->ReceivedInvalidFrame == false)) { do { RS485_Check_UART_Data(mstp_port); - MSTP_Receive_Frame_FSM((volatile struct mstp_port_struct_t *) - pArg); + MSTP_Receive_Frame_FSM( + (volatile struct mstp_port_struct_t *)pArg); received_frame = mstp_port->ReceivedValidFrame || mstp_port->ReceivedInvalidFrame; if (received_frame) { @@ -250,19 +242,18 @@ void *dlmstp_receive_fsm_task( return NULL; } -void *dlmstp_master_fsm_task( - void *pArg) +void *dlmstp_master_fsm_task(void *pArg) { uint32_t silence = 0; bool run_master = false; SHARED_MSTP_DATA *poSharedData; - struct mstp_port_struct_t *mstp_port = (struct mstp_port_struct_t *) pArg; + struct mstp_port_struct_t *mstp_port = (struct mstp_port_struct_t *)pArg; if (!mstp_port) { return NULL; } poSharedData = - (SHARED_MSTP_DATA *) ((struct mstp_port_struct_t *) pArg)->UserData; + (SHARED_MSTP_DATA *)((struct mstp_port_struct_t *)pArg)->UserData; if (!poSharedData) { return NULL; } @@ -309,9 +300,7 @@ void *dlmstp_master_fsm_task( 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; @@ -335,11 +324,10 @@ void dlmstp_fill_bacnet_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) +uint16_t MSTP_Put_Receive(volatile struct mstp_port_struct_t *mstp_port) { uint16_t pdu_len = 0; - SHARED_MSTP_DATA *poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData; + SHARED_MSTP_DATA *poSharedData = (SHARED_MSTP_DATA *)mstp_port->UserData; if (!poSharedData) { return 0; @@ -350,10 +338,10 @@ uint16_t MSTP_Put_Receive( pdu_len = mstp_port->DataLength; if (pdu_len > sizeof(poSharedData->Receive_Packet.pdu)) pdu_len = sizeof(poSharedData->Receive_Packet.pdu); - memmove((void *) &poSharedData->Receive_Packet.pdu[0], - (void *) &mstp_port->InputBuffer[0], pdu_len); - dlmstp_fill_bacnet_address(&poSharedData->Receive_Packet.address, - mstp_port->SourceAddress); + memmove((void *)&poSharedData->Receive_Packet.pdu[0], + (void *)&mstp_port->InputBuffer[0], pdu_len); + dlmstp_fill_bacnet_address( + &poSharedData->Receive_Packet.address, mstp_port->SourceAddress); poSharedData->Receive_Packet.pdu_len = mstp_port->DataLength; poSharedData->Receive_Packet.ready = true; sem_post(&poSharedData->Receive_Packet_Flag); @@ -365,42 +353,41 @@ 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 frame_type = 0; struct mstp_pdu_packet *pkt; - SHARED_MSTP_DATA *poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData; + SHARED_MSTP_DATA *poSharedData = (SHARED_MSTP_DATA *)mstp_port->UserData; if (!poSharedData) { return 0; } - (void) timeout; + (void)timeout; if (Ringbuf_Empty(&poSharedData->PDU_Queue)) { return 0; } - pkt = (struct mstp_pdu_packet *) Ringbuf_Peek(&poSharedData->PDU_Queue); + pkt = (struct mstp_pdu_packet *)Ringbuf_Peek(&poSharedData->PDU_Queue); if (pkt->data_expecting_reply) { frame_type = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY; } else { frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; } /* convert the PDU into the MSTP Frame */ - pdu_len = MSTP_Create_Frame(&mstp_port->OutputBuffer[0], /* <-- loading this */ - mstp_port->OutputBufferSize, frame_type, pkt->destination_mac, - mstp_port->This_Station, (uint8_t *) & pkt->buffer[0], pkt->length); - (void) Ringbuf_Pop(&poSharedData->PDU_Queue, NULL); + pdu_len = + MSTP_Create_Frame(&mstp_port->OutputBuffer[0], /* <-- loading this */ + mstp_port->OutputBufferSize, frame_type, pkt->destination_mac, + mstp_port->This_Station, (uint8_t *)&pkt->buffer[0], pkt->length); + (void)Ringbuf_Pop(&poSharedData->PDU_Queue, NULL); return pdu_len; } -bool dlmstp_compare_data_expecting_reply( - uint8_t * request_pdu, +bool dlmstp_compare_data_expecting_reply(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) { @@ -424,13 +411,13 @@ 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], NULL, &request.address, - &request.npdu_data); + offset = npdu_decode( + &request_pdu[0], NULL, &request.address, &request.npdu_data); if (request.npdu_data.network_layer_message) { #if PRINT_ENABLED fprintf(stderr, - "DLMSTP: DER Compare failed: " "Request is Network message.\n"); + "DLMSTP: DER Compare failed: " + "Request is Network message.\n"); #endif return false; } @@ -438,7 +425,8 @@ bool dlmstp_compare_data_expecting_reply( if (request.pdu_type != PDU_TYPE_CONFIRMED_SERVICE_REQUEST) { #if PRINT_ENABLED fprintf(stderr, - "DLMSTP: DER Compare failed: " "Not Confirmed Request.\n"); + "DLMSTP: DER Compare failed: " + "Not Confirmed Request.\n"); #endif return false; } @@ -452,12 +440,12 @@ bool dlmstp_compare_data_expecting_reply( /* decode the reply data */ reply.address.mac[0] = dest_address; reply.address.mac_len = 1; - offset = - npdu_decode(&reply_pdu[0], &reply.address, NULL, &reply.npdu_data); + offset = npdu_decode(&reply_pdu[0], &reply.address, NULL, &reply.npdu_data); if (reply.npdu_data.network_layer_message) { #if PRINT_ENABLED fprintf(stderr, - "DLMSTP: DER Compare failed: " "Reply is Network message.\n"); + "DLMSTP: DER Compare failed: " + "Reply is Network message.\n"); #endif return false; } @@ -495,7 +483,8 @@ bool dlmstp_compare_data_expecting_reply( if (request.invoke_id != reply.invoke_id) { #if PRINT_ENABLED fprintf(stderr, - "DLMSTP: DER Compare failed: " "Invoke ID mismatch.\n"); + "DLMSTP: DER Compare failed: " + "Invoke ID mismatch.\n"); #endif return false; } @@ -503,19 +492,22 @@ bool dlmstp_compare_data_expecting_reply( if (request.invoke_id != reply.invoke_id) { #if PRINT_ENABLED fprintf(stderr, - "DLMSTP: DER Compare failed: " "Invoke ID mismatch.\n"); + "DLMSTP: DER Compare failed: " + "Invoke ID mismatch.\n"); #endif return false; } if (request.service_choice != reply.service_choice) { #if PRINT_ENABLED fprintf(stderr, - "DLMSTP: DER Compare failed: " "Service choice mismatch.\n"); + "DLMSTP: DER Compare failed: " + "Service choice mismatch.\n"); #endif return false; } } - if (request.npdu_data.protocol_version != reply.npdu_data.protocol_version) { + if (request.npdu_data.protocol_version != + reply.npdu_data.protocol_version) { #if PRINT_ENABLED fprintf(stderr, "DLMSTP: DER Compare failed: " @@ -537,7 +529,8 @@ bool dlmstp_compare_data_expecting_reply( if (!bacnet_address_same(&request.address, &reply.address)) { #if PRINT_ENABLED fprintf(stderr, - "DLMSTP: DER Compare failed: " "BACnet Address mismatch.\n"); + "DLMSTP: DER Compare failed: " + "BACnet Address mismatch.\n"); #endif return false; } @@ -547,14 +540,13 @@ 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 */ bool matched = false; uint8_t frame_type = 0; struct mstp_pdu_packet *pkt; - SHARED_MSTP_DATA *poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData; + SHARED_MSTP_DATA *poSharedData = (SHARED_MSTP_DATA *)mstp_port->UserData; if (!poSharedData) { return 0; @@ -563,23 +555,20 @@ uint16_t MSTP_Get_Reply( if (Ringbuf_Empty(&poSharedData->PDU_Queue)) { return 0; } - pkt = (struct mstp_pdu_packet *) Ringbuf_Peek(&poSharedData->PDU_Queue); + pkt = (struct mstp_pdu_packet *)Ringbuf_Peek(&poSharedData->PDU_Queue); /* 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, - (uint8_t *) & pkt->buffer[0], pkt->length, pkt->destination_mac); + (uint8_t *)&pkt->buffer[0], pkt->length, pkt->destination_mac); if (!matched) { /* Walk the rest of the ring buffer to see if we can find a match */ while (!matched && - (pkt = (struct mstp_pdu_packet *)Ringbuf_Peek_Next(&poSharedData->PDU_Queue, (uint8_t *)pkt)) != NULL) { - matched = - dlmstp_compare_data_expecting_reply(&mstp_port->InputBuffer[0], - mstp_port->DataLength, - mstp_port->SourceAddress, - (uint8_t *) & pkt->buffer[0], - pkt->length, - pkt->destination_mac); + (pkt = (struct mstp_pdu_packet *)Ringbuf_Peek_Next( + &poSharedData->PDU_Queue, (uint8_t *)pkt)) != NULL) { + matched = dlmstp_compare_data_expecting_reply( + &mstp_port->InputBuffer[0], mstp_port->DataLength, + mstp_port->SourceAddress, (uint8_t *)&pkt->buffer[0], + pkt->length, pkt->destination_mac); } if (!matched) { /* Still didn't find a match so just bail out */ @@ -592,32 +581,30 @@ uint16_t MSTP_Get_Reply( frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; } /* convert the PDU into the MSTP Frame */ - pdu_len = MSTP_Create_Frame(&mstp_port->OutputBuffer[0], /* <-- loading this */ - mstp_port->OutputBufferSize, frame_type, pkt->destination_mac, - mstp_port->This_Station, (uint8_t *) & pkt->buffer[0], pkt->length); + pdu_len = + MSTP_Create_Frame(&mstp_port->OutputBuffer[0], /* <-- loading this */ + mstp_port->OutputBufferSize, frame_type, pkt->destination_mac, + mstp_port->This_Station, (uint8_t *)&pkt->buffer[0], pkt->length); /* This will pop the element no matter where we found it */ - (void) Ringbuf_Pop_Element(&poSharedData->PDU_Queue, (uint8_t *)pkt, NULL); + (void)Ringbuf_Pop_Element(&poSharedData->PDU_Queue, (uint8_t *)pkt, NULL); return pdu_len; } -void dlmstp_set_mac_address( - void *poPort, - uint8_t mac_address) +void dlmstp_set_mac_address(void *poPort, uint8_t mac_address) { -/* SHARED_MSTP_DATA * poSharedData; */ - struct mstp_port_struct_t *mstp_port = - (struct mstp_port_struct_t *) poPort; + /* SHARED_MSTP_DATA * poSharedData; */ + struct mstp_port_struct_t *mstp_port = (struct mstp_port_struct_t *)poPort; if (!mstp_port) { return; } -/* - poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData; - if(!poSharedData) - { - return; - } -*/ + /* + poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData; + if(!poSharedData) + { + return; + } + */ /* Master Nodes can only have address 0-127 */ if (mac_address <= 127) { mstp_port->This_Station = mac_address; @@ -633,21 +620,19 @@ void dlmstp_set_mac_address( return; } -uint8_t dlmstp_mac_address( - void *poPort) +uint8_t dlmstp_mac_address(void *poPort) { -/* SHARED_MSTP_DATA * poSharedData; */ - struct mstp_port_struct_t *mstp_port = - (struct mstp_port_struct_t *) poPort; + /* SHARED_MSTP_DATA * poSharedData; */ + struct mstp_port_struct_t *mstp_port = (struct mstp_port_struct_t *)poPort; if (!mstp_port) { return 0; } -/* poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData; - if(!poSharedData) - { - return 0; - } -*/ + /* poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData; + if(!poSharedData) + { + return 0; + } + */ return mstp_port->This_Station; } @@ -659,23 +644,20 @@ uint8_t dlmstp_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( - void *poPort, - uint8_t max_info_frames) +void dlmstp_set_max_info_frames(void *poPort, uint8_t max_info_frames) { -/* SHARED_MSTP_DATA * poSharedData; */ - struct mstp_port_struct_t *mstp_port = - (struct mstp_port_struct_t *) poPort; + /* SHARED_MSTP_DATA * poSharedData; */ + struct mstp_port_struct_t *mstp_port = (struct mstp_port_struct_t *)poPort; if (!mstp_port) { return; } -/* - poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData; - if(!poSharedData) - { - return; - } -*/ + /* + poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData; + if(!poSharedData) + { + return; + } + */ if (max_info_frames >= 1) { mstp_port->Nmax_info_frames = max_info_frames; /* FIXME: implement your data storage */ @@ -688,22 +670,20 @@ void dlmstp_set_max_info_frames( return; } -uint8_t dlmstp_max_info_frames( - void *poPort) +uint8_t dlmstp_max_info_frames(void *poPort) { -/* SHARED_MSTP_DATA * poSharedData; */ - struct mstp_port_struct_t *mstp_port = - (struct mstp_port_struct_t *) poPort; + /* SHARED_MSTP_DATA * poSharedData; */ + struct mstp_port_struct_t *mstp_port = (struct mstp_port_struct_t *)poPort; if (!mstp_port) { return 0; } -/* - poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData; - if(!poSharedData) - { - return 0; - } -*/ + /* + poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData; + if(!poSharedData) + { + return 0; + } + */ return mstp_port->Nmax_info_frames; } @@ -712,23 +692,20 @@ uint8_t dlmstp_max_info_frames( /* 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( - void *poPort, - uint8_t max_master) +void dlmstp_set_max_master(void *poPort, uint8_t max_master) { -/* SHARED_MSTP_DATA * poSharedData; */ - struct mstp_port_struct_t *mstp_port = - (struct mstp_port_struct_t *) poPort; + /* SHARED_MSTP_DATA * poSharedData; */ + struct mstp_port_struct_t *mstp_port = (struct mstp_port_struct_t *)poPort; if (!mstp_port) { return; } -/* - poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData; - if(!poSharedData) - { - return; - } -*/ + /* + poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData; + if(!poSharedData) + { + return; + } + */ if (max_master <= 127) { if (mstp_port->This_Station <= max_master) { mstp_port->Nmax_master = max_master; @@ -743,37 +720,32 @@ void dlmstp_set_max_master( return; } -uint8_t dlmstp_max_master( - void *poPort) +uint8_t dlmstp_max_master(void *poPort) { -/* SHARED_MSTP_DATA * poSharedData; */ - struct mstp_port_struct_t *mstp_port = - (struct mstp_port_struct_t *) poPort; + /* SHARED_MSTP_DATA * poSharedData; */ + struct mstp_port_struct_t *mstp_port = (struct mstp_port_struct_t *)poPort; if (!mstp_port) { return 0; } -/* - poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData; - if(!poSharedData) - { - return 0; - } -*/ + /* + poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData; + if(!poSharedData) + { + return 0; + } + */ return mstp_port->Nmax_master; } /* RS485 Baud Rate 9600, 19200, 38400, 57600, 115200 */ -void dlmstp_set_baud_rate( - void *poPort, - uint32_t baud) +void dlmstp_set_baud_rate(void *poPort, uint32_t baud) { SHARED_MSTP_DATA *poSharedData; - struct mstp_port_struct_t *mstp_port = - (struct mstp_port_struct_t *) poPort; + struct mstp_port_struct_t *mstp_port = (struct mstp_port_struct_t *)poPort; if (!mstp_port) { return; } - poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData; + poSharedData = (SHARED_MSTP_DATA *)mstp_port->UserData; if (!poSharedData) { return; } @@ -799,16 +771,14 @@ void dlmstp_set_baud_rate( } } -uint32_t dlmstp_baud_rate( - void *poPort) +uint32_t dlmstp_baud_rate(void *poPort) { SHARED_MSTP_DATA *poSharedData; - struct mstp_port_struct_t *mstp_port = - (struct mstp_port_struct_t *) poPort; + struct mstp_port_struct_t *mstp_port = (struct mstp_port_struct_t *)poPort; if (!mstp_port) { return false; } - poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData; + poSharedData = (SHARED_MSTP_DATA *)mstp_port->UserData; if (!poSharedData) { return false; } @@ -828,24 +798,21 @@ uint32_t dlmstp_baud_rate( } } -void dlmstp_get_my_address( - void *poPort, - BACNET_ADDRESS * my_address) +void dlmstp_get_my_address(void *poPort, BACNET_ADDRESS *my_address) { - int i = 0; /* counter */ + int i = 0; /* counter */ SHARED_MSTP_DATA *poSharedData; - struct mstp_port_struct_t *mstp_port = - (struct mstp_port_struct_t *) poPort; + struct mstp_port_struct_t *mstp_port = (struct mstp_port_struct_t *)poPort; if (!mstp_port) { return; } - poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData; + poSharedData = (SHARED_MSTP_DATA *)mstp_port->UserData; if (!poSharedData) { return; } my_address->mac_len = 1; my_address->mac[0] = mstp_port->This_Station; - my_address->net = 0; /* local only, no routing */ + my_address->net = 0; /* local only, no routing */ my_address->len = 0; for (i = 0; i < MAX_MAC_LEN; i++) { my_address->adr[i] = 0; @@ -854,16 +821,15 @@ void dlmstp_get_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; } @@ -872,30 +838,26 @@ void dlmstp_get_broadcast_address( return; } -bool dlmstp_init( - void *poPort, - char *ifname) +bool dlmstp_init(void *poPort, char *ifname) { unsigned long hThread = 0; int rv = 0; SHARED_MSTP_DATA *poSharedData; - struct mstp_port_struct_t *mstp_port = - (struct mstp_port_struct_t *) poPort; + struct mstp_port_struct_t *mstp_port = (struct mstp_port_struct_t *)poPort; if (!mstp_port) { return false; } - poSharedData = (SHARED_MSTP_DATA *) ((struct mstp_port_struct_t *) - mstp_port)->UserData; + poSharedData = + (SHARED_MSTP_DATA *)((struct mstp_port_struct_t *)mstp_port)->UserData; if (!poSharedData) { return false; } poSharedData->RS485_Port_Name = ifname; /* initialize PDU queue */ - Ringbuf_Init(&poSharedData->PDU_Queue, - (uint8_t *) & poSharedData->PDU_Buffer, sizeof(struct mstp_pdu_packet), - MSTP_PDU_PACKET_COUNT); + Ringbuf_Init(&poSharedData->PDU_Queue, (uint8_t *)&poSharedData->PDU_Buffer, + sizeof(struct mstp_pdu_packet), MSTP_PDU_PACKET_COUNT); /* initialize packet queue */ poSharedData->Receive_Packet.ready = false; poSharedData->Receive_Packet.pdu_len = 0; @@ -913,9 +875,8 @@ bool dlmstp_init( Open device for reading and writing. Blocking mode - more CPU effecient */ - poSharedData->RS485_Handle = - open(poSharedData->RS485_Port_Name, - O_RDWR | O_NOCTTY | O_NONBLOCK /*| O_NDELAY */ ); + poSharedData->RS485_Handle = open(poSharedData->RS485_Port_Name, + O_RDWR | O_NOCTTY | O_NONBLOCK /*| O_NDELAY */); if (poSharedData->RS485_Handle < 0) { perror(poSharedData->RS485_Port_Name); exit(-1); @@ -966,8 +927,7 @@ bool dlmstp_init( #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 Max_Info_Frames: %u\n", mstp_port->Nmax_info_frames); #endif rv = pthread_create(&hThread, NULL, dlmstp_master_fsm_task, mstp_port); diff --git a/ports/linux/ethernet.c b/ports/linux/ethernet.c index 006352e9..e7c7e492 100644 --- a/ports/linux/ethernet.c +++ b/ports/linux/ethernet.c @@ -32,36 +32,35 @@ ------------------------------------------- ####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 "bacport.h" #include "bacnet/bacdef.h" #include "bacnet/datalink/ethernet.h" #include "bacnet/bacint.h" -/** @file linux/ethernet.c Provides Linux-specific functions for BACnet/Ethernet. */ +/** @file linux/ethernet.c Provides Linux-specific functions for + * BACnet/Ethernet. */ /* commonly used comparison address for ethernet */ -uint8_t Ethernet_Broadcast[MAX_MAC_LEN] = - { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; +uint8_t Ethernet_Broadcast[MAX_MAC_LEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF }; /* commonly used empty address for ethernet quick compare */ uint8_t Ethernet_Empty_MAC[MAX_MAC_LEN] = { 0, 0, 0, 0, 0, 0 }; /* my local device data - MAC address */ 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 */ +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); @@ -92,11 +91,9 @@ int setNonblocking( #endif /* 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 */ #if 0 int sockopt = 0; #endif @@ -121,13 +118,14 @@ static int ethernet_bind( /* Attempt to open the socket for 802.2 ethernet frames */ if ((sock_fd = socket(PF_INET, SOCK_PACKET, htons(ETH_P_802_2))) < 0) { /* Error occured */ - fprintf(stderr, "ethernet: Error opening socket: %s\n", - strerror(errno)); + fprintf( + stderr, "ethernet: 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" "alias net-pf-17 af_packet\n" - "Also, add af_packet to /etc/modules.\n" "Then follow it by:\n" + "Also, add af_packet to /etc/modules.\n" + "Then follow it by:\n" "# modprobe af_packet\n"); exit(-1); } @@ -156,7 +154,8 @@ static int ethernet_bind( "You might need to add the following to modules.conf\n" "(or in /etc/modutils/alias on Debian with update-modules):\n" "alias net-pf-17 af_packet\n" - "Also, add af_packet to /etc/modules.\n" "Then follow it by:\n" + "Also, add af_packet to /etc/modules.\n" + "Then follow it by:\n" "# modprobe af_packet\n"); /* Close the socket */ close(sock_fd); @@ -169,13 +168,11 @@ static int ethernet_bind( } /* 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); @@ -184,15 +181,14 @@ static int get_local_hwaddr( 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); @@ -205,40 +201,35 @@ bool ethernet_init( return ethernet_valid(); } -int ethernet_send( - uint8_t * mtu, - int mtu_len) +int ethernet_send(uint8_t *mtu, int mtu_len) { int bytes = 0; /* Send the packet */ - bytes = - sendto(eth802_sockfd, &mtu, mtu_len, 0, (struct sockaddr *) ð_addr, - sizeof(struct sockaddr)); + bytes = sendto(eth802_sockfd, &mtu, mtu_len, 0, + (struct sockaddr *)ð_addr, sizeof(struct sockaddr)); /* did it get sent? */ if (bytes < 0) - fprintf(stderr, "ethernet: Error sending packet: %s\n", - strerror(errno)); + fprintf( + stderr, "ethernet: Error sending packet: %s\n", strerror(errno)); return bytes; - } /* 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 */ - BACNET_NPDU_DATA * npdu_data, /* network information */ - uint8_t * pdu, /* any data to be sent - may be null */ +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 */ 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 */ + uint8_t mtu[MAX_MPDU] = { 0 }; /* our buffer */ int mtu_len = 0; - (void) npdu_data; + (void)npdu_data; /* load the BACnet address for NPDU data */ for (i = 0; i < 6; i++) { src.mac[i] = Ethernet_MAC_Address[i]; @@ -270,9 +261,9 @@ int ethernet_send_pdu( 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"); @@ -284,28 +275,26 @@ int ethernet_send_pdu( encode_unsigned16(&mtu[12], 3 + pdu_len); /* Send the packet */ - bytes = - sendto(eth802_sockfd, &mtu, mtu_len, 0, (struct sockaddr *) ð_addr, - sizeof(struct sockaddr)); + bytes = sendto(eth802_sockfd, &mtu, mtu_len, 0, + (struct sockaddr *)ð_addr, sizeof(struct sockaddr)); /* did it get sent? */ if (bytes < 0) - fprintf(stderr, "ethernet: Error sending packet: %s\n", - strerror(errno)); + fprintf( + stderr, "ethernet: Error sending packet: %s\n", strerror(errno)); return bytes; } /* 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 */ + uint8_t buf[MAX_MPDU] = { 0 }; /* data */ + uint16_t pdu_len = 0; /* return value */ fd_set read_fds; int max; struct timeval select_timeout; @@ -359,14 +348,14 @@ uint16_t ethernet_receive( /* check destination address for when */ /* the Ethernet card is in promiscious mode */ - if ((memcmp(&buf[0], Ethernet_MAC_Address, 6) != 0) - && (memcmp(&buf[0], Ethernet_Broadcast, 6) != 0)) { + if ((memcmp(&buf[0], Ethernet_MAC_Address, 6) != 0) && + (memcmp(&buf[0], Ethernet_Broadcast, 6) != 0)) { /*fprintf(stderr, "ethernet: This packet isn't for us\n"); */ return 0; } - (void) decode_unsigned16(&buf[12], &pdu_len); - pdu_len -= 3 /* DSAP, SSAP, LLC Control */ ; + (void)decode_unsigned16(&buf[12], &pdu_len); + pdu_len -= 3 /* DSAP, SSAP, LLC Control */; /* copy the buffer into the PDU */ if (pdu_len < max_pdu) memmove(&pdu[0], &buf[17], pdu_len); @@ -374,12 +363,10 @@ uint16_t ethernet_receive( else pdu_len = 0; - return pdu_len; } -void ethernet_set_my_address( - BACNET_ADDRESS * my_address) +void ethernet_set_my_address(BACNET_ADDRESS *my_address) { int i = 0; @@ -390,8 +377,7 @@ void ethernet_set_my_address( return; } -void ethernet_get_my_address( - BACNET_ADDRESS * my_address) +void ethernet_get_my_address(BACNET_ADDRESS *my_address) { int i = 0; @@ -400,7 +386,7 @@ void ethernet_get_my_address( my_address->mac[i] = Ethernet_MAC_Address[i]; my_address->mac_len++; } - my_address->net = 0; /* DNET=0 is local only, no routing */ + my_address->net = 0; /* DNET=0 is local only, no routing */ my_address->len = 0; for (i = 0; i < MAX_MAC_LEN; i++) { my_address->adr[i] = 0; @@ -409,10 +395,9 @@ void ethernet_get_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++) { @@ -420,7 +405,7 @@ void ethernet_get_broadcast_address( } 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; } @@ -429,11 +414,9 @@ void ethernet_get_broadcast_address( 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); @@ -442,14 +425,14 @@ void ethernet_debug_address( fprintf(stderr, " MAC Length=%d\n", dest->mac_len); fprintf(stderr, " MAC Address="); for (i = 0; i < MAX_MAC_LEN; i++) { - fprintf(stderr, "%02X ", (unsigned) dest->mac[i]); + fprintf(stderr, "%02X ", (unsigned)dest->mac[i]); } fprintf(stderr, "\n"); fprintf(stderr, " Net=%hu\n", dest->net); fprintf(stderr, " Len=%d\n", dest->len); fprintf(stderr, " Adr="); for (i = 0; i < MAX_MAC_LEN; i++) { - fprintf(stderr, "%02X ", (unsigned) dest->adr[i]); + fprintf(stderr, "%02X ", (unsigned)dest->adr[i]); } fprintf(stderr, "\n"); } diff --git a/ports/linux/mstimer-init.c b/ports/linux/mstimer-init.c index 1e6920e8..2d9312ed 100644 --- a/ports/linux/mstimer-init.c +++ b/ports/linux/mstimer-init.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2009 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2009 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include #include @@ -41,25 +41,23 @@ static struct timespec start; /** @brief The timeGetTime function retrieves the system time, in milliseconds. * The system time is the time elapsed since Windows was started. */ -static unsigned long timeGetTime( - void) +static unsigned long timeGetTime(void) { struct timespec now; unsigned long ticks; clock_gettime(CLOCK_MONOTONIC, &now); - ticks = - (now.tv_sec - start.tv_sec) * 1000 + (now.tv_nsec - - start.tv_nsec) / 1000000; + ticks = (now.tv_sec - start.tv_sec) * 1000 + + (now.tv_nsec - start.tv_nsec) / 1000000; return ticks; } /** -* @brief returns the current millisecond count -* @return millisecond counter -*/ + * @brief returns the current millisecond count + * @return millisecond counter + */ unsigned long mstimer_now(void) { unsigned long now = timeGetTime(); @@ -75,10 +73,9 @@ unsigned long mstimer_now(void) } /** -* @brief Initialization for timer -*/ -void mstimer_init( - void) + * @brief Initialization for timer + */ +void mstimer_init(void) { clock_gettime(CLOCK_MONOTONIC, &start); } diff --git a/ports/linux/mstpsnap.c b/ports/linux/mstpsnap.c index 942172ea..87285607 100644 --- a/ports/linux/mstpsnap.c +++ b/ports/linux/mstpsnap.c @@ -50,11 +50,12 @@ #include "bacnet/datalink/mstptext.h" #include "bacnet/bacint.h" -/** @file linux/mstpsnap.c Example application testing BACnet MS/TP on Linux. */ +/** @file linux/mstpsnap.c Example application testing BACnet MS/TP on Linux. + */ #ifndef max -#define max(a,b) (((a) (b)) ? (a) : (b)) -#define min(a,b) (((a) < (b)) ? (a) : (b)) +#define max(a, b) (((a)(b)) ? (a) : (b)) +#define min(a, b) (((a) < (b)) ? (a) : (b)) #endif /* local port data - shared with RS-485 */ @@ -64,8 +65,7 @@ static uint8_t RxBuffer[MAX_MPDU]; static uint8_t TxBuffer[MAX_MPDU]; static struct mstimer Silence_Timer; -static uint32_t Timer_Silence( - void *pArg) +static uint32_t Timer_Silence(void *pArg) { uint32_t delta_time = 0; @@ -77,17 +77,15 @@ static uint32_t Timer_Silence( return delta_time; } -static void Timer_Silence_Reset( - void *pArg) +static void Timer_Silence_Reset(void *pArg) { mstimer_set(&Silence_Timer, 0); } /* 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) +uint16_t MSTP_Put_Receive(volatile struct mstp_port_struct_t *mstp_port) { - (void) mstp_port; + (void)mstp_port; return 0; } @@ -95,26 +93,22 @@ 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 */ - (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; } -static int network_init( - const char *name, - int protocol) +static int network_init(const char *name, int protocol) { /* check to see if we are being run as root */ if (getuid() != 0) { @@ -146,7 +140,7 @@ static int network_init( sll.sll_ifindex = ifr.ifr_ifindex; sll.sll_protocol = htons(protocol); - if (bind(sockfd, (struct sockaddr *) &sll, sizeof(sll)) == -1) { + if (bind(sockfd, (struct sockaddr *)&sll, sizeof(sll)) == -1) { perror("Unable to bind socket"); return -1; } @@ -155,11 +149,10 @@ static int network_init( } static void snap_received_packet( - volatile struct mstp_port_struct_t *mstp_port, - int sockfd) + volatile struct mstp_port_struct_t *mstp_port, int sockfd) { - uint16_t mtu_len = 0; /* number of octets of packet saved in file */ - unsigned i = 0; /* counter */ + uint16_t mtu_len = 0; /* number of octets of packet saved in file */ + unsigned i = 0; /* counter */ static uint8_t mtu[1500] = { 0 }; uint16_t max_data = 0; @@ -176,17 +169,17 @@ static void snap_received_packet( mtu[10] = 0; mtu[11] = mstp_port->SourceAddress; /* length - 12, 13 */ - mtu[14] = 0xaa; /* DSAP for SNAP */ - mtu[15] = 0xaa; /* SSAP for SNAP */ - mtu[16] = 0x03; /* Control Field for SNAP */ - mtu[17] = 0x00; /* Organization Code: Cimetrics */ - mtu[18] = 0x10; /* Organization Code: Cimetrics */ - mtu[19] = 0x90; /* Organization Code: Cimetrics */ - mtu[20] = 0x00; /* Protocol ID */ - mtu[21] = 0x01; /* Protocol ID */ - mtu[22] = 0x00; /* delta time */ - mtu[23] = 0x00; /* delta time */ - mtu[24] = 0x80; /* unknown byte */ + mtu[14] = 0xaa; /* DSAP for SNAP */ + mtu[15] = 0xaa; /* SSAP for SNAP */ + mtu[16] = 0x03; /* Control Field for SNAP */ + mtu[17] = 0x00; /* Organization Code: Cimetrics */ + mtu[18] = 0x10; /* Organization Code: Cimetrics */ + mtu[19] = 0x90; /* Organization Code: Cimetrics */ + mtu[20] = 0x00; /* Protocol ID */ + mtu[21] = 0x01; /* Protocol ID */ + mtu[22] = 0x00; /* delta time */ + mtu[23] = 0x00; /* delta time */ + mtu[24] = 0x80; /* unknown byte */ mtu[25] = mstp_port->FrameType; mtu[26] = mstp_port->DestinationAddress; mtu[27] = mstp_port->SourceAddress; @@ -205,27 +198,23 @@ static void snap_received_packet( } /* Ethernet length is data only - not address or length bytes */ encode_unsigned16(&mtu[12], mtu_len - 14); - (void) write(sockfd, &mtu[0], mtu_len); + (void)write(sockfd, &mtu[0], mtu_len); } - -static void cleanup( - void) +static void cleanup(void) { } #if (!defined(_WIN32)) -static void sig_int( - int signo) +static void sig_int(int signo) { - (void) signo; + (void)signo; cleanup(); exit(0); } -void signal_init( - void) +void signal_init(void) { signal(SIGINT, sig_int); signal(SIGHUP, sig_int); @@ -234,9 +223,7 @@ void signal_init( #endif /* 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; long my_baud = 38400; @@ -248,14 +235,18 @@ int main( mstp_port = &MSTP_Port; if ((argc > 1) && (strcmp(argv[1], "--help") == 0)) { printf("mstsnap [serial] [baud] [network]\r\n" - "Captures MS/TP packets from a serial interface\r\n" - "and sends them to a network interface using SNAP \r\n" - "protocol packets (mimics Cimetrics U+4 packet).\r\n" "\r\n" - "Command line options:\r\n" "[serial] - serial interface.\r\n" - " defaults to /dev/ttyUSB0.\r\n" - "[baud] - baud rate. 9600, 19200, 38400, 57600, 115200\r\n" - " defaults to 38400.\r\n" "[network] - network interface.\r\n" - " defaults to eth0.\r\n" ""); + "Captures MS/TP packets from a serial interface\r\n" + "and sends them to a network interface using SNAP \r\n" + "protocol packets (mimics Cimetrics U+4 packet).\r\n" + "\r\n" + "Command line options:\r\n" + "[serial] - serial interface.\r\n" + " defaults to /dev/ttyUSB0.\r\n" + "[baud] - baud rate. 9600, 19200, 38400, 57600, 115200\r\n" + " defaults to 38400.\r\n" + "[network] - network interface.\r\n" + " defaults to eth0.\r\n" + ""); return 0; } /* initialize our interface */ @@ -285,11 +276,11 @@ int main( MSTP_Port.SilenceTimerReset = Timer_Silence_Reset; MSTP_Init(mstp_port); fprintf(stdout, "mstpcap: Using %s for capture at %ld bps.\n", - RS485_Interface(), (long) RS485_Get_Baud_Rate()); + RS485_Interface(), (long)RS485_Get_Baud_Rate()); atexit(cleanup); #if defined(_WIN32) SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_PROCESSED_INPUT); - SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlCHandler, TRUE); + SetConsoleCtrlHandler((PHANDLER_ROUTINE)CtrlCHandler, TRUE); #else signal_init(); #endif diff --git a/ports/linux/rs485.c b/ports/linux/rs485.c index 717103bb..b4ffd880 100644 --- a/ports/linux/rs485.c +++ b/ports/linux/rs485.c @@ -54,8 +54,8 @@ #include #include #include -#include /* for struct serial_struct */ -#include /* for calculation of custom divisor */ +#include /* for struct serial_struct */ +#include /* for calculation of custom divisor */ #include /* for scandir */ #include @@ -106,13 +106,12 @@ static uint8_t Rx_Buffer[4096]; #define _POSIX_SOURCE 1 /* POSIX compliant source */ /********************************************************************* -* DESCRIPTION: Configures the interface name -* RETURN: none -* ALGORITHM: none -* NOTES: none -*********************************************************************/ -void RS485_Set_Interface( - char *ifname) + * DESCRIPTION: Configures the interface name + * RETURN: none + * ALGORITHM: none + * NOTES: none + *********************************************************************/ +void RS485_Set_Interface(char *ifname) { /* note: expects a constant char, or char from the heap */ if (ifname) { @@ -121,25 +120,23 @@ void RS485_Set_Interface( } /********************************************************************* -* DESCRIPTION: Returns the interface name -* RETURN: none -* ALGORITHM: none -* NOTES: none -*********************************************************************/ -const char *RS485_Interface( - void) + * DESCRIPTION: Returns the interface name + * RETURN: none + * ALGORITHM: none + * NOTES: none + *********************************************************************/ +const char *RS485_Interface(void) { return RS485_Port_Name; } /**************************************************************************** -* DESCRIPTION: Returns the baud rate that we are currently running at -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -uint32_t RS485_Get_Baud_Rate( - void) + * DESCRIPTION: Returns the baud rate that we are currently running at + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +uint32_t RS485_Get_Baud_Rate(void) { uint32_t baud = 0; @@ -215,16 +212,15 @@ uint32_t RS485_Get_Baud_Rate( } /**************************************************************************** -* DESCRIPTION: Returns the baud rate that we are currently running at -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -uint32_t RS485_Get_Port_Baud_Rate( - volatile struct mstp_port_struct_t * mstp_port) + * DESCRIPTION: Returns the baud rate that we are currently running at + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +uint32_t RS485_Get_Port_Baud_Rate(volatile struct mstp_port_struct_t *mstp_port) { uint32_t baud = 0; - SHARED_MSTP_DATA *poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData; + SHARED_MSTP_DATA *poSharedData = (SHARED_MSTP_DATA *)mstp_port->UserData; if (!poSharedData) { return 0; } @@ -295,13 +291,12 @@ uint32_t RS485_Get_Port_Baud_Rate( } /**************************************************************************** -* DESCRIPTION: Sets the baud rate for the chip USART -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -bool RS485_Set_Baud_Rate( - uint32_t baud) + * DESCRIPTION: Sets the baud rate for the chip USART + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +bool RS485_Set_Baud_Rate(uint32_t baud) { bool valid = true; @@ -381,16 +376,16 @@ bool RS485_Set_Baud_Rate( } /**************************************************************************** -* DESCRIPTION: Transmit a frame on the wire -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ + * DESCRIPTION: Transmit a frame on the wire + * RETURN: none + * 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) */ + 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) */ +{ /* number of bytes of data (up to 501) */ uint32_t turnaround_time = Tturnaround * 1000; uint32_t baud; ssize_t written = 0; @@ -398,7 +393,7 @@ void RS485_Send_Frame( SHARED_MSTP_DATA *poSharedData = NULL; if (mstp_port) { - poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData; + poSharedData = (SHARED_MSTP_DATA *)mstp_port->UserData; } if (!poSharedData) { baud = RS485_Get_Baud_Rate(); @@ -406,11 +401,12 @@ void RS485_Send_Frame( time to change from sending to receiving state. */ usleep(turnaround_time / baud); /* - 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); greska = errno; @@ -423,7 +419,7 @@ void RS485_Send_Frame( /* tcdrain(RS485_Handle); */ /* per MSTP spec, sort of */ if (mstp_port) { - mstp_port->SilenceTimerReset((void *) mstp_port); + mstp_port->SilenceTimerReset((void *)mstp_port); } } else { baud = RS485_Get_Port_Baud_Rate(mstp_port); @@ -431,11 +427,12 @@ void RS485_Send_Frame( time to change from sending to receiving state. */ usleep(turnaround_time / baud); /* - 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(poSharedData->RS485_Handle, buffer, nbytes); greska = errno; @@ -448,7 +445,7 @@ void RS485_Send_Frame( /* tcdrain(RS485_Handle); */ /* per MSTP spec, sort of */ if (mstp_port) { - mstp_port->SilenceTimerReset((void *) mstp_port); + mstp_port->SilenceTimerReset((void *)mstp_port); } } @@ -456,20 +453,19 @@ void RS485_Send_Frame( } /**************************************************************************** -* DESCRIPTION: Get a byte of receive data -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -void RS485_Check_UART_Data( - volatile struct mstp_port_struct_t *mstp_port) + * DESCRIPTION: Get a byte of receive data + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +void RS485_Check_UART_Data(volatile struct mstp_port_struct_t *mstp_port) { fd_set input; struct timeval waiter; uint8_t buf[2048]; int n; - SHARED_MSTP_DATA *poSharedData = (SHARED_MSTP_DATA *) mstp_port->UserData; + SHARED_MSTP_DATA *poSharedData = (SHARED_MSTP_DATA *)mstp_port->UserData; if (!poSharedData) { if (mstp_port->ReceiveError == true) { /* do nothing but wait for state machine to clear the error */ @@ -526,8 +522,7 @@ void RS485_Check_UART_Data( /* grab bytes and stuff them into the FIFO every time */ FD_ZERO(&input); FD_SET(poSharedData->RS485_Handle, &input); - n = select(poSharedData->RS485_Handle + 1, &input, NULL, NULL, - &waiter); + n = select(poSharedData->RS485_Handle + 1, &input, NULL, NULL, &waiter); if (n < 0) { return; } @@ -538,8 +533,7 @@ void RS485_Check_UART_Data( } } -void RS485_Cleanup( - void) +void RS485_Cleanup(void) { /* restore the old port settings */ tcsetattr(RS485_Handle, TCSANOW, &RS485_oldtio); @@ -547,9 +541,7 @@ void RS485_Cleanup( close(RS485_Handle); } - -void RS485_Initialize( - void) +void RS485_Initialize(void) { struct termios newtio; struct serial_struct newserial; @@ -560,7 +552,7 @@ void RS485_Initialize( Open device for reading and writing. Blocking mode - more CPU effecient */ - RS485_Handle = open(RS485_Port_Name, O_RDWR | O_NOCTTY /*| O_NDELAY */ ); + RS485_Handle = open(RS485_Port_Name, O_RDWR | O_NOCTTY /*| O_NDELAY */); if (RS485_Handle < 0) { perror(RS485_Port_Name); exit(-1); @@ -600,14 +592,12 @@ void RS485_Initialize( if (RS485_SpecBaud) { /* 76800, custom divisor must be set */ newserial.flags |= ASYNC_SPD_CUST; - newserial.custom_divisor = - round(((float) newserial.baud_base) / 76800); + newserial.custom_divisor = round(((float)newserial.baud_base) / 76800); /* we must check that we calculated some sane value; small baud bases yield bad custom divisor values */ - baud_error = - fabs(1 - - ((float) newserial.baud_base) / - ((float) newserial.custom_divisor) / 76800); + baud_error = fabs(1 - + ((float)newserial.baud_base) / ((float)newserial.custom_divisor) / + 76800); if ((newserial.custom_divisor == 0) || (baud_error > 0.02)) { /* bad divisor */ fprintf(stderr, "bad custom divisor %d, base baud %d\n", @@ -633,7 +623,7 @@ void RS485_Print_Ports(void) { int n; struct dirent **namelist; - const char* sysdir = "/sys/class/tty/"; + const char *sysdir = "/sys/class/tty/"; struct stat st; char buffer[1024]; char device_dir[1024]; @@ -648,29 +638,27 @@ void RS485_Print_Ports(void) perror("RS485: scandir"); } else { while (n--) { - if (strcmp(namelist[n]->d_name,"..") && - strcmp(namelist[n]->d_name,".")) { - snprintf(device_dir, sizeof(device_dir), - "%s%s/device", - sysdir, namelist[n]->d_name); + if (strcmp(namelist[n]->d_name, "..") && + strcmp(namelist[n]->d_name, ".")) { + snprintf(device_dir, sizeof(device_dir), "%s%s/device", sysdir, + namelist[n]->d_name); // Stat the devicedir and handle it if it is a symlink - if (lstat(device_dir, &st)==0 && S_ISLNK(st.st_mode)) { + if (lstat(device_dir, &st) == 0 && S_ISLNK(st.st_mode)) { memset(buffer, 0, sizeof(buffer)); snprintf(device_dir, sizeof(device_dir), - "%s%s/device/driver", - sysdir, namelist[n]->d_name); + "%s%s/device/driver", sysdir, namelist[n]->d_name); if (readlink(device_dir, buffer, sizeof(buffer)) > 0) { valid_port = false; - driver_name=basename(buffer); - if (strcmp(driver_name,"serial8250") == 0) { + driver_name = basename(buffer); + if (strcmp(driver_name, "serial8250") == 0) { // serial8250-devices must be probed - snprintf(device_dir, sizeof(device_dir), - "/dev/%s", namelist[n]->d_name); - fd = open(device_dir, - O_RDWR | O_NONBLOCK | O_NOCTTY); + snprintf(device_dir, sizeof(device_dir), "/dev/%s", + namelist[n]->d_name); + fd = open( + device_dir, O_RDWR | O_NONBLOCK | O_NOCTTY); if (fd >= 0) { // Get serial_info - if (ioctl(fd, TIOCGSERIAL, &serinfo)==0) { + if (ioctl(fd, TIOCGSERIAL, &serinfo) == 0) { // If device type is not PORT_UNKNOWN // we accept the port if (serinfo.type != PORT_UNKNOWN) { @@ -685,7 +673,7 @@ void RS485_Print_Ports(void) if (valid_port) { // print full absolute file path printf("interface {value=/dev/%s}" - "{display=MS/TP Capture on /dev/%s}\n", + "{display=MS/TP Capture on /dev/%s}\n", namelist[n]->d_name, namelist[n]->d_name); } } @@ -699,13 +687,11 @@ void RS485_Print_Ports(void) #ifdef TEST_RS485 #include -int main( - int argc, - char *argv[]) +int main(int argc, char *argv[]) { - volatile struct mstp_port_struct_t mstp_port = {0}; - uint8_t token_buf[8] = {0x55, 0xFF, 0x00, 0x7E, 0x07, 0x00, 0x00, 0xFD}; - uint8_t pfm_buf[8] = {0x55, 0xFF, 0x01, 0x67, 0x07, 0x00, 0x00, 0x3E}; + volatile struct mstp_port_struct_t mstp_port = { 0 }; + uint8_t token_buf[8] = { 0x55, 0xFF, 0x00, 0x7E, 0x07, 0x00, 0x00, 0xFD }; + uint8_t pfm_buf[8] = { 0x55, 0xFF, 0x01, 0x67, 0x07, 0x00, 0x00, 0x3E }; long baud = 38400; bool write_token = false; bool write_pfm = false; @@ -729,10 +715,10 @@ int main( RS485_Initialize(); for (;;) { if (write_token) { - RS485_Send_Frame(NULL,token_buf, sizeof(token_buf)); + RS485_Send_Frame(NULL, token_buf, sizeof(token_buf)); usleep(25000); } else if (write_pfm) { - RS485_Send_Frame(NULL,pfm_buf, sizeof(pfm_buf)); + RS485_Send_Frame(NULL, pfm_buf, sizeof(pfm_buf)); usleep(100000); } else { RS485_Check_UART_Data(&mstp_port); diff --git a/ports/linux/rx_fsm.c b/ports/linux/rx_fsm.c index 07c58e9e..e560c422 100644 --- a/ports/linux/rx_fsm.c +++ b/ports/linux/rx_fsm.c @@ -45,7 +45,7 @@ #include #include #include -#include /* signal handling functions */ +#include /* signal handling functions */ /* local includes */ #include "bacnet/bytes.h" @@ -54,11 +54,12 @@ #include "bacnet/datalink/mstp.h" #include "bacnet/datalink/mstptext.h" -/** @file linux/rx_fsm.c Example app testing MS/TP Rx State Machine on Linux. */ +/** @file linux/rx_fsm.c Example app testing MS/TP Rx State Machine on Linux. + */ #ifndef max -#define max(a,b) (((a) (b)) ? (a) : (b)) -#define min(a,b) (((a) < (b)) ? (a) : (b)) +#define max(a, b) (((a)(b)) ? (a) : (b)) +#define min(a, b) (((a) < (b)) ? (a) : (b)) #endif #ifndef LOCAL_PRINT @@ -71,27 +72,27 @@ static volatile struct mstp_port_struct_t MSTP_Port; 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) +#define INCREMENT_AND_LIMIT_UINT16(x) \ + { \ + if (x < 0xFFFF) \ + x++; \ + } +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; @@ -107,10 +108,9 @@ void *milliseconds_task( } /* 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) +uint16_t MSTP_Put_Receive(volatile struct mstp_port_struct_t *mstp_port) { - (void) mstp_port; + (void)mstp_port; return 0; } @@ -118,26 +118,23 @@ 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 */ - (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; @@ -158,19 +155,18 @@ int timestamp_ms( } static const char *Capture_Filename = "mstp.cap"; -static FILE *pFile = NULL; /* stream pointer */ +static FILE *pFile = NULL; /* stream pointer */ /* write packet to file in libpcap format */ -static void write_global_header( - void) +static void write_global_header(void) { uint32_t magic_number = 0xa1b2c3d4; /* magic number */ uint16_t version_major = 2; /* major version number */ uint16_t version_minor = 4; /* minor version number */ - int32_t thiszone = 0; /* GMT to local correction */ - uint32_t sigfigs = 0; /* accuracy of timestamps */ - uint32_t snaplen = 65535; /* max length of captured packets, in octets */ - uint32_t network = 165; /* data link type - BACNET_MS_TP */ + int32_t thiszone = 0; /* GMT to local correction */ + uint32_t sigfigs = 0; /* accuracy of timestamps */ + uint32_t snaplen = 65535; /* max length of captured packets, in octets */ + uint32_t network = 165; /* data link type - BACNET_MS_TP */ /* create a new file. */ pFile = fopen(Capture_Filename, "wb"); @@ -188,14 +184,13 @@ static void write_global_header( } } -static void write_received_packet( - volatile struct mstp_port_struct_t *mstp_port) +static void write_received_packet(volatile struct mstp_port_struct_t *mstp_port) { - uint32_t ts_sec; /* timestamp seconds */ - uint32_t ts_usec; /* timestamp microseconds */ - uint32_t incl_len; /* number of octets of packet saved in file */ - uint32_t orig_len; /* actual length of packet */ - uint8_t header[8]; /* MS/TP header */ + uint32_t ts_sec; /* timestamp seconds */ + uint32_t ts_usec; /* timestamp microseconds */ + uint32_t incl_len; /* number of octets of packet saved in file */ + uint32_t orig_len; /* actual length of packet */ + uint8_t header[8]; /* MS/TP header */ struct timeval tv; size_t max_data = 0; @@ -224,8 +219,8 @@ static void write_received_packet( fwrite(header, sizeof(header), 1, pFile); if (mstp_port->DataLength) { fwrite(mstp_port->InputBuffer, max_data, 1, pFile); - fwrite((char *) &(mstp_port->DataCRCActualMSB), 1, 1, pFile); - fwrite((char *) &(mstp_port->DataCRCActualLSB), 1, 1, pFile); + fwrite((char *)&(mstp_port->DataCRCActualMSB), 1, 1, pFile); + fwrite((char *)&(mstp_port->DataCRCActualLSB), 1, 1, pFile); } } else { fprintf(stderr, "rx_fsm: failed to open %s: %s\n", Capture_Filename, @@ -233,19 +228,17 @@ static void write_received_packet( } } -static void cleanup( - void) +static void cleanup(void) { if (pFile) { - fflush(pFile); /* stream pointer */ - fclose(pFile); /* stream pointer */ + fflush(pFile); /* stream pointer */ + fclose(pFile); /* stream pointer */ } pFile = NULL; } #if LOCAL_PRINT -static void print_received_packet( - volatile struct mstp_port_struct_t *mstp_port) +static void print_received_packet(volatile struct mstp_port_struct_t *mstp_port) { unsigned i = 0; int timestamp = 0; @@ -280,17 +273,15 @@ static void print_received_packet( } #endif -static void sig_int( - int signo) +static void sig_int(int signo) { - (void) signo; + (void)signo; cleanup(); exit(0); } -void signal_init( - void) +void signal_init(void) { signal(SIGINT, sig_int); signal(SIGHUP, sig_int); @@ -298,9 +289,7 @@ void signal_init( } /* 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; @@ -339,7 +328,7 @@ int main( rc = pthread_create(&hThread, NULL, milliseconds_task, NULL); atexit(cleanup); write_global_header(); - fflush(pFile); /* stream pointer */ + fflush(pFile); /* stream pointer */ /* run forever */ for (;;) { RS485_Check_UART_Data(mstp_port); @@ -351,7 +340,7 @@ int main( print_received_packet(mstp_port); #endif write_received_packet(mstp_port); - fflush(pFile); /* stream pointer */ + fflush(pFile); /* stream pointer */ } else if (mstp_port->ReceivedInvalidFrame) { mstp_port->ReceivedInvalidFrame = false; fprintf(stderr, "ReceivedInvalidFrame\n"); @@ -359,7 +348,7 @@ int main( print_received_packet(mstp_port); #endif write_received_packet(mstp_port); - fflush(pFile); /* stream pointer */ + fflush(pFile); /* stream pointer */ } } diff --git a/ports/lwip/bip.c b/ports/lwip/bip.c index 50743814..0be836e7 100644 --- a/ports/lwip/bip.c +++ b/ports/lwip/bip.c @@ -31,15 +31,15 @@ License. ------------------------------------------- ####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 "bacnet/bacdef.h" #include "bacnet/bacdcode.h" #include "bacnet/bacint.h" #include "bacnet/datalink/bip.h" #include "bacnet/datalink/bvlc.h" #include "bacnet/basic/services.h" -#include "bacport.h" /* custom per port */ +#include "bacport.h" /* custom per port */ /** @file bip.c Configuration and Operations for BACnet/IP */ @@ -54,9 +54,9 @@ static struct in_addr BIP_Broadcast_Address; static struct udp_pcb *Server_upcb; /* track packets for diagnostics */ struct bacnet_stats { - uint32_t xmit; /* Transmitted packets. */ - uint32_t recv; /* Received packets. */ - uint32_t drop; /* Dropped packets. */ + uint32_t xmit; /* Transmitted packets. */ + uint32_t recv; /* Received packets. */ + uint32_t drop; /* Dropped packets. */ }; struct bacnet_stats BIP_Stats; #define BIP_STATS_INC(x) ++BIP_Stats.x @@ -76,28 +76,24 @@ uint32_t bip_stats_drop(void) return BIP_Stats.drop; } -void bip_set_addr( - uint32_t net_address) -{ /* in network byte order */ +void bip_set_addr(uint32_t net_address) +{ /* in network byte order */ BIP_Address.s_addr = net_address; } /* returns network byte order */ -uint32_t bip_get_addr( - void) +uint32_t bip_get_addr(void) { return BIP_Address.s_addr; } -void bip_set_broadcast_addr( - uint32_t net_address) -{ /* in network byte order */ +void bip_set_broadcast_addr(uint32_t net_address) +{ /* in network byte order */ BIP_Broadcast_Address.s_addr = net_address; } /* returns network byte order */ -uint32_t bip_get_broadcast_addr( - void) +uint32_t bip_get_broadcast_addr(void) { return BIP_Broadcast_Address.s_addr; } @@ -106,8 +102,7 @@ uint32_t bip_get_broadcast_addr( * @brief Set the BACnet IPv4 UDP port number * @param port - IPv4 UDP port number - in host byte order */ -void bip_set_port( - uint16_t port) +void bip_set_port(uint16_t port) { if (BIP_Port != htons(port)) { BIP_Port_Changed = true; @@ -121,41 +116,35 @@ bool bip_port_changed(void) } /* returns host byte order */ -uint16_t bip_get_port( - void) +uint16_t bip_get_port(void) { return ntohs(BIP_Port); } -static void bip_mac_to_addr( - struct ip_addr *address, - uint8_t * mac) +static void bip_mac_to_addr(struct ip_addr *address, uint8_t *mac) { if (mac && address) { - address->addr = ((u32_t) ((((uint32_t) mac[0]) << 24) & 0xff000000)); - address->addr |= ((u32_t) ((((uint32_t) mac[1]) << 16) & 0x00ff0000)); - address->addr |= ((u32_t) ((((uint32_t) mac[2]) << 8) & 0x0000ff00)); - address->addr |= ((u32_t) (((uint32_t) mac[3]) & 0x000000ff)); + address->addr = ((u32_t)((((uint32_t)mac[0]) << 24) & 0xff000000)); + address->addr |= ((u32_t)((((uint32_t)mac[1]) << 16) & 0x00ff0000)); + address->addr |= ((u32_t)((((uint32_t)mac[2]) << 8) & 0x0000ff00)); + address->addr |= ((u32_t)(((uint32_t)mac[3]) & 0x000000ff)); } } -static void bip_addr_to_mac( - uint8_t * mac, - struct ip_addr *address) +static void bip_addr_to_mac(uint8_t *mac, struct ip_addr *address) { if (mac && address) { - mac[0] = (uint8_t) (address->addr >> 24); - mac[1] = (uint8_t) (address->addr >> 16); - mac[2] = (uint8_t) (address->addr >> 8); - mac[3] = (uint8_t) (address->addr); + mac[0] = (uint8_t)(address->addr >> 24); + mac[1] = (uint8_t)(address->addr >> 16); + mac[2] = (uint8_t)(address->addr >> 8); + mac[3] = (uint8_t)(address->addr); } } -static int bip_decode_bip_address( - BACNET_ADDRESS * bac_addr, - struct ip_addr *address, /* in network format */ - uint16_t * port) -{ /* in network format */ +static int bip_decode_bip_address(BACNET_ADDRESS *bac_addr, + struct ip_addr *address, /* in network format */ + uint16_t *port) +{ /* in network format */ int len = 0; if (bac_addr) { @@ -177,9 +166,7 @@ static int bip_decode_bip_address( * @return true if the packet was sent */ static bool bip_send_mpdu( - struct ip_addr *dst_ip, - uint16_t port, - struct pbuf *pkt) + struct ip_addr *dst_ip, uint16_t port, struct pbuf *pkt) { err_t status = ERR_OK; @@ -203,14 +190,13 @@ static bool bip_send_mpdu( * * @return number of bytes sent */ -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 */ +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 */ unsigned pdu_len) { struct pbuf *pkt = NULL, *pkt0 = NULL; - uint8_t mtu[4] = {0}; + uint8_t mtu[4] = { 0 }; int mtu_len = 0; /* addr and port in host format */ struct ip_addr dst_ip; @@ -240,7 +226,7 @@ int bip_send_pdu( /* invalid address */ return -1; } - mtu_len = pdu_len + 4 /*inclusive */ ; + mtu_len = pdu_len + 4 /*inclusive */; encode_unsigned16(&mtu[2], mtu_len); pbuf_take(pkt0, mtu, 4); pbuf_take(pkt, pdu, pdu_len); @@ -260,9 +246,7 @@ int bip_send_pdu( * * @return number of bytes encoded */ -static bool bvlc_send_result( - struct ip_addr *addr, - uint16_t result_code) +static bool bvlc_send_result(struct ip_addr *addr, uint16_t result_code) { struct pbuf *pkt = NULL; uint8_t mtu[6] = { 0 }; @@ -284,8 +268,7 @@ static bool bvlc_send_result( return bip_send_mpdu(addr, BIP_Port, pkt); } -void bip_server_callback( - void *arg, +void bip_server_callback(void *arg, struct udp_pcb *upcb, struct pbuf *pkt, struct ip_addr *addr, @@ -294,12 +277,10 @@ void bip_server_callback( uint8_t function = 0; uint16_t pdu_len = 0; uint16_t pdu_offset = 0; - BACNET_ADDRESS src = { - 0 - }; /* address where message came from */ + BACNET_ADDRESS src = { 0 }; /* address where message came from */ struct ip_addr sin_addr; uint16_t sin_port = 0; - uint8_t *pdu = (uint8_t *) pkt->payload; + uint8_t *pdu = (uint8_t *)pkt->payload; /* the signature of a BACnet/IP packet */ if (pdu[0] != BVLL_TYPE_BACNET_IP) { @@ -317,13 +298,13 @@ void bip_server_callback( bip_addr_to_mac(&src.mac[0], addr); memcpy(&src.mac[4], &port, 2); /* decode the length of the PDU - length is inclusive of BVLC */ - (void) decode_unsigned16(&pdu[2], &pdu_len); + (void)decode_unsigned16(&pdu[2], &pdu_len); /* subtract off the BVLC header */ pdu_len -= 4; pdu_offset = 4; } } else if (function == BVLC_FORWARDED_NPDU) { - IP4_ADDR(&sin_addr,pdu[4],pdu[5],pdu[6],pdu[7]); + IP4_ADDR(&sin_addr, pdu[4], pdu[5], pdu[6], pdu[7]); decode_unsigned16(&pdu[8], &sin_port); if ((sin_addr.addr == BIP_Address.s_addr) && (sin_port == BIP_Port)) { /* ignore forwarded messages from me */ @@ -334,29 +315,26 @@ void bip_server_callback( bip_addr_to_mac(&src.mac[0], &sin_addr); memcpy(&src.mac[4], &sin_port, 2); /* decode the length of the PDU - length is inclusive of BVLC */ - (void) decode_unsigned16(&pdu[2], &pdu_len); + (void)decode_unsigned16(&pdu[2], &pdu_len); /* subtract off the BVLC header */ pdu_len -= 10; pdu_offset = 10; } } else if (function == BVLC_WRITE_BROADCAST_DISTRIBUTION_TABLE) { - bvlc_send_result(addr, - BVLC_RESULT_WRITE_BROADCAST_DISTRIBUTION_TABLE_NAK); + bvlc_send_result( + addr, BVLC_RESULT_WRITE_BROADCAST_DISTRIBUTION_TABLE_NAK); } else if (function == BVLC_READ_BROADCAST_DIST_TABLE) { - bvlc_send_result(addr, - BVLC_RESULT_READ_BROADCAST_DISTRIBUTION_TABLE_NAK); + bvlc_send_result( + addr, BVLC_RESULT_READ_BROADCAST_DISTRIBUTION_TABLE_NAK); } else if (function == BVLC_REGISTER_FOREIGN_DEVICE) { - bvlc_send_result(addr, - BVLC_RESULT_REGISTER_FOREIGN_DEVICE_NAK); + bvlc_send_result(addr, BVLC_RESULT_REGISTER_FOREIGN_DEVICE_NAK); } else if (function == BVLC_READ_FOREIGN_DEVICE_TABLE) { - bvlc_send_result(addr, - BVLC_RESULT_READ_FOREIGN_DEVICE_TABLE_NAK); + bvlc_send_result(addr, BVLC_RESULT_READ_FOREIGN_DEVICE_TABLE_NAK); } else if (function == BVLC_DELETE_FOREIGN_DEVICE_TABLE_ENTRY) { - bvlc_send_result(addr, - BVLC_RESULT_DELETE_FOREIGN_DEVICE_TABLE_ENTRY_NAK); + bvlc_send_result( + addr, BVLC_RESULT_DELETE_FOREIGN_DEVICE_TABLE_ENTRY_NAK); } else if (function == BVLC_DISTRIBUTE_BROADCAST_TO_NETWORK) { - bvlc_send_result(addr, - BVLC_RESULT_DISTRIBUTE_BROADCAST_TO_NETWORK_NAK); + bvlc_send_result(addr, BVLC_RESULT_DISTRIBUTE_BROADCAST_TO_NETWORK_NAK); } if (pdu_len) { BIP_STATS_INC(recv); @@ -376,8 +354,7 @@ void bip_server_callback( pbuf_free(pkt); } -void bip_get_my_address( - BACNET_ADDRESS * my_address) +void bip_get_my_address(BACNET_ADDRESS *my_address) { int i = 0; @@ -385,8 +362,8 @@ void bip_get_my_address( my_address->mac_len = 6; memcpy(&my_address->mac[0], &BIP_Address.s_addr, 4); memcpy(&my_address->mac[4], &BIP_Port, 2); - my_address->net = 0; /* local only, no routing */ - my_address->len = 0; /* no SLEN */ + my_address->net = 0; /* local only, no routing */ + my_address->len = 0; /* no SLEN */ for (i = 0; i < MAX_MAC_LEN; i++) { /* no SADR */ my_address->adr[i] = 0; @@ -396,17 +373,16 @@ void bip_get_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; memcpy(&dest->mac[0], &BIP_Broadcast_Address.s_addr, 4); memcpy(&dest->mac[4], &BIP_Port, 2); 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; @@ -433,16 +409,14 @@ void bip_get_broadcast_address( * @return True if the socket is successfully opened for BACnet/IP, * else False if the socket functions fail. */ -bool bip_init( - char *ifname) +bool bip_init(char *ifname) { - (void) ifname; + (void)ifname; /* Create a new UDP control block */ Server_upcb = udp_new(); if (Server_upcb == NULL) { /* increase MEMP_NUM_UDP_PCB in lwipopts.h */ - while (1) { - }; + while (1) { }; } /* Bind the upcb to the UDP_PORT port */ /* Using IP_ADDR_ANY allow the upcb to be used by any local interface */ diff --git a/ports/pic18f6720/ai.c b/ports/pic18f6720/ai.c index 2d73e9e1..b5112c68 100644 --- a/ports/pic18f6720/ai.c +++ b/ports/pic18f6720/ai.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2005 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2005 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* Analog Input Objects customize for your use */ @@ -44,8 +44,7 @@ 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; @@ -54,34 +53,30 @@ bool Analog_Input_Valid_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 */ + static char text_string[16] = ""; /* okay for single thread */ if (object_instance < MAX_ANALOG_INPUTS) { - sprintf(text_string, "AI-%lu", (unsigned long) object_instance); + sprintf(text_string, "AI-%lu", (unsigned long)object_instance); return text_string; } return NULL; } -float Analog_Input_Present_Value( - uint32_t object_instance) +float Analog_Input_Present_Value(uint32_t object_instance) { float value = 0.0; @@ -91,9 +86,7 @@ float Analog_Input_Present_Value( return value; } -void Analog_Input_Present_Value_Set( - uint32_t object_instance, - float value) +void Analog_Input_Present_Value_Set(uint32_t object_instance, float value) { if (object_instance < MAX_ANALOG_INPUTS) { Present_Value[object_instance] = value; @@ -102,10 +95,9 @@ void Analog_Input_Present_Value_Set( /* return apdu length, or -1 on error */ /* assumption - object has already exists */ -int Analog_Input_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Analog_Input_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ BACNET_BIT_STRING bit_string; BACNET_CHARACTER_STRING char_string; uint8_t *apdu = NULL; @@ -117,16 +109,15 @@ int Analog_Input_Read_Property( apdu = rpdata->application_data; switch (rpdata->object_property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], OBJECT_ANALOG_INPUT, - rpdata->object_instance); + apdu_len = encode_application_object_id( + &apdu[0], OBJECT_ANALOG_INPUT, rpdata->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(rpdata->object_instance)); + characterstring_init_ansi( + &char_string, Analog_Input_Name(rpdata->object_instance)); apdu_len = encode_application_character_string(&apdu[0], &char_string); break; @@ -135,9 +126,8 @@ int Analog_Input_Read_Property( encode_application_enumerated(&apdu[0], OBJECT_ANALOG_INPUT); break; case PROP_PRESENT_VALUE: - apdu_len = - encode_application_real(&apdu[0], - Analog_Input_Present_Value(rpdata->object_instance)); + apdu_len = encode_application_real( + &apdu[0], Analog_Input_Present_Value(rpdata->object_instance)); break; case PROP_STATUS_FLAGS: bitstring_init(&bit_string); diff --git a/ports/pic18f6720/apdu.c b/ports/pic18f6720/apdu.c index 85005fe8..3a776d0d 100644 --- a/ports/pic18f6720/apdu.c +++ b/ports/pic18f6720/apdu.c @@ -43,20 +43,17 @@ /* me */ #include "bacnet/apdu.h" -uint16_t apdu_timeout( - void) +uint16_t apdu_timeout(void) { return 3000; } -uint8_t apdu_retries( - void) +uint8_t apdu_retries(void) { return 3; } -bool apdu_service_supported( - BACNET_SERVICES_SUPPORTED service_supported) +bool apdu_service_supported(BACNET_SERVICES_SUPPORTED service_supported) { bool status = false; @@ -75,15 +72,14 @@ bool apdu_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) + BACNET_CONFIRMED_SERVICE_DATA *service_data, + uint8_t *service_choice, + 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] & BIT(3)) ? true : false; service_data->more_follows = (apdu[0] & BIT(2)) ? true : false; @@ -110,8 +106,7 @@ uint16_t apdu_decode_confirmed_service_request( When the initiation of communications is disabled, all APDUs shall be processed and responses returned as required... */ -static bool apdu_confirmed_dcc_disabled( - uint8_t service_choice) +static bool apdu_confirmed_dcc_disabled(uint8_t service_choice) { bool status = false; @@ -136,8 +131,7 @@ static bool apdu_confirmed_dcc_disabled( DISABLE_INITIATION, the responding BACnet-user shall discontinue the initiation of messages except for I-Am requests issued in accordance with the Who-Is service procedure.*/ -static bool apdu_unconfirmed_dcc_disabled( - uint8_t service_choice) +static bool apdu_unconfirmed_dcc_disabled(uint8_t service_choice) { bool status = false; @@ -159,36 +153,37 @@ static bool apdu_unconfirmed_dcc_disabled( return status; } -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 */ + len = apdu_decode_confirmed_service_request( + &apdu[0], /* APDU data */ apdu_len, &service_data, &service_choice, &service_request, &service_request_len); if (apdu_confirmed_dcc_disabled(service_choice)) { /* When network communications are completely disabled, - only DeviceCommunicationControl and ReinitializeDevice APDUs - shall be processed and no messages shall be initiated. */ + only DeviceCommunicationControl and ReinitializeDevice + APDUs shall be processed and no messages shall be + initiated. */ break; } if (service_choice == SERVICE_CONFIRMED_READ_PROPERTY) { handler_read_property(service_request, service_request_len, src, &service_data); } else if (service_choice == SERVICE_CONFIRMED_WRITE_PROPERTY) { - handler_write_property(service_request, - service_request_len, src, &service_data); + handler_write_property(service_request, service_request_len, + src, &service_data); } else if (service_choice == SERVICE_CONFIRMED_REINITIALIZE_DEVICE) { handler_reinitialize_device(service_request, @@ -208,10 +203,10 @@ void apdu_handler( service_request_len = apdu_len - 2; if (apdu_unconfirmed_dcc_disabled(service_choice)) { /* When network communications are disabled, - only DeviceCommunicationControl and ReinitializeDevice APDUs - shall be processed and no messages shall be initiated. - If communications have been initiation disabled, then - WhoIs may be processed. */ + only DeviceCommunicationControl and ReinitializeDevice + APDUs shall be processed and no messages shall be + initiated. If communications have been initiation + disabled, then WhoIs may be processed. */ break; } if (service_choice == SERVICE_UNCONFIRMED_WHO_IS) { diff --git a/ports/pic18f6720/av.c b/ports/pic18f6720/av.c index c77677df..ef1ddd31 100644 --- a/ports/pic18f6720/av.c +++ b/ports/pic18f6720/av.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2006 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2006 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* Analog Value Objects - customize for your use */ @@ -32,7 +32,7 @@ #include "bacnet/bacdcode.h" #include "bacnet/bacenum.h" #include "bacnet/bacapp.h" -#include "bacnet/config.h" /* the custom stuff */ +#include "bacnet/config.h" /* the custom stuff */ #include "bacnet/wp.h" #include "bacnet/rp.h" #include "bacnet/basic/object/av.h" @@ -54,8 +54,7 @@ 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; @@ -74,8 +73,7 @@ void Analog_Value_Init( /* 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) @@ -86,8 +84,7 @@ bool Analog_Value_Valid_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; @@ -96,8 +93,7 @@ unsigned Analog_Value_Count( /* 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; @@ -106,8 +102,7 @@ uint32_t Analog_Value_Index_To_Instance( /* 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; @@ -118,8 +113,7 @@ unsigned Analog_Value_Instance_To_Index( return index; } -float Analog_Value_Present_Value( - uint32_t object_instance) +float Analog_Value_Present_Value(uint32_t object_instance) { float value = ANALOG_RELINQUISH_DEFAULT; unsigned index = 0; @@ -135,10 +129,9 @@ float Analog_Value_Present_Value( } /* 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 */ + static char text_string[32] = ""; /* okay for single thread */ if (object_instance < MAX_ANALOG_VALUES) { sprintf(text_string, "AV-%lu", object_instance); @@ -149,14 +142,13 @@ char *Analog_Value_Name( } /* return apdu len, or -1 on error */ -int Analog_Value_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Analog_Value_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { 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; + float real_value = (float)1.414; unsigned object_index = 0; unsigned i = 0; bool state = false; @@ -170,14 +162,13 @@ int Analog_Value_Read_Property( apdu = rpdata->application_data; switch (rpdata->object_property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], OBJECT_ANALOG_VALUE, - rpdata->object_instance); + apdu_len = encode_application_object_id( + &apdu[0], OBJECT_ANALOG_VALUE, rpdata->object_instance); break; case PROP_OBJECT_NAME: case PROP_DESCRIPTION: - characterstring_init_ansi(&char_string, - Analog_Value_Name(rpdata->object_instance)); + characterstring_init_ansi( + &char_string, Analog_Value_Name(rpdata->object_instance)); apdu_len = encode_application_character_string(&apdu[0], &char_string); break; @@ -287,10 +278,9 @@ int Analog_Value_Read_Property( } /* returns true if successful */ -bool Analog_Value_Write_Property( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Analog_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data) { - bool status = false; /* return value */ + bool status = false; /* return value */ unsigned int object_index = 0; unsigned int priority = 0; uint8_t level = ANALOG_LEVEL_NULL; @@ -304,9 +294,8 @@ bool Analog_Value_Write_Property( return false; } /* decode the some of the request */ - len = - bacapp_decode_application_data(wp_data->application_data, - wp_data->application_data_len, &value); + len = bacapp_decode_application_data( + wp_data->application_data, wp_data->application_data_len, &value); /* FIXME: len < application_data_len: more data? */ if (len < 0) { /* error while decoding - a value larger than we can handle */ @@ -329,19 +318,19 @@ bool Analog_Value_Write_Property( algorithm and may not be used for other purposes in any object. */ if (priority && (priority <= BACNET_MAX_PRIORITY) && - (priority != 6 /* reserved */ ) && + (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); + 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) */ + 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 diff --git a/ports/pic18f6720/bi.c b/ports/pic18f6720/bi.c index 86fe747e..e3c39650 100644 --- a/ports/pic18f6720/bi.c +++ b/ports/pic18f6720/bi.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2006 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2006 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* Binary Input Objects customize for your use */ @@ -40,8 +40,7 @@ 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; @@ -55,8 +54,7 @@ static void Binary_Input_Initialize( } /* 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; @@ -65,15 +63,13 @@ bool Binary_Input_Valid_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; } @@ -81,8 +77,7 @@ uint32_t Binary_Input_Index_To_Instance( /* 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; @@ -92,8 +87,7 @@ unsigned Binary_Input_Instance_To_Index( return index; } -BACNET_BINARY_PV Binary_Input_Present_Value( - uint32_t object_instance) +BACNET_BINARY_PV Binary_Input_Present_Value(uint32_t object_instance) { BACNET_BINARY_PV value = BINARY_INACTIVE; unsigned index = 0; @@ -107,10 +101,9 @@ BACNET_BINARY_PV Binary_Input_Present_Value( 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 */ + static char text_string[16] = ""; /* okay for single thread */ if (object_instance < MAX_BINARY_INPUTS) { sprintf(text_string, "BI-%lu", object_instance); @@ -122,10 +115,9 @@ char *Binary_Input_Name( /* return apdu length, or -1 on error */ /* assumption - object already exists, and has been bounds checked */ -int Binary_Input_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Binary_Input_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { - 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; @@ -140,15 +132,14 @@ int Binary_Input_Read_Property( apdu = rpdata->application_data; switch (rpdata->object_property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], OBJECT_BINARY_INPUT, - rpdata->object_instance); + apdu_len = encode_application_object_id( + &apdu[0], OBJECT_BINARY_INPUT, rpdata->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(rpdata->object_instance)); + characterstring_init_ansi( + &char_string, Binary_Input_Name(rpdata->object_instance)); apdu_len = encode_application_character_string(&apdu[0], &char_string); break; diff --git a/ports/pic18f6720/bv.c b/ports/pic18f6720/bv.c index a57f57e4..7c0f5901 100644 --- a/ports/pic18f6720/bv.c +++ b/ports/pic18f6720/bv.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2006 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2006 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* Binary Value Objects - customize for your use */ @@ -31,7 +31,7 @@ #include "bacnet/bacdef.h" #include "bacnet/bacdcode.h" #include "bacnet/bacenum.h" -#include "bacnet/config.h" /* the custom stuff */ +#include "bacnet/config.h" /* the custom stuff */ #include "bacnet/wp.h" #include "bacnet/rp.h" #include "bacnet/basic/object/bv.h" @@ -40,8 +40,7 @@ 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; @@ -55,8 +54,7 @@ static void Binary_Value_Initialize( } /* 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; @@ -65,22 +63,19 @@ bool Binary_Value_Valid_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; @@ -90,8 +85,7 @@ unsigned Binary_Value_Instance_To_Index( return index; } -BACNET_BINARY_PV Binary_Value_Present_Value( - uint32_t object_instance) +BACNET_BINARY_PV Binary_Value_Present_Value(uint32_t object_instance) { BACNET_BINARY_PV value = BINARY_INACTIVE; @@ -104,10 +98,9 @@ BACNET_BINARY_PV Binary_Value_Present_Value( } /* 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 */ + static char text_string[16] = ""; /* okay for single thread */ if (object_instance < MAX_BINARY_VALUES) { sprintf(text_string, "BV-%lu", object_instance); @@ -118,11 +111,10 @@ char *Binary_Value_Name( } /* return apdu len, or -1 on error */ -int Binary_Value_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Binary_Value_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { 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; @@ -140,16 +132,15 @@ int Binary_Value_Read_Property( apdu = rpdata->application_data; switch (rpdata->object_property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], OBJECT_BINARY_VALUE, - rpdata->object_instance); + apdu_len = encode_application_object_id( + &apdu[0], OBJECT_BINARY_VALUE, rpdata->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(rpdata->object_instance)); + characterstring_init_ansi( + &char_string, Binary_Value_Name(rpdata->object_instance)); apdu_len = encode_application_character_string(&apdu[0], &char_string); break; @@ -158,8 +149,7 @@ int Binary_Value_Read_Property( encode_application_enumerated(&apdu[0], OBJECT_BINARY_VALUE); break; case PROP_PRESENT_VALUE: - present_value = - Binary_Value_Present_Value(rpdata->object_instance); + present_value = Binary_Value_Present_Value(rpdata->object_instance); apdu_len = encode_application_enumerated(&apdu[0], present_value); break; case PROP_STATUS_FLAGS: @@ -200,10 +190,9 @@ int Binary_Value_Read_Property( } /* returns true if successful */ -bool Binary_Value_Write_Property( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Binary_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data) { - bool status = false; /* return value */ + bool status = false; /* return value */ unsigned int object_index = 0; unsigned int priority = 0; BACNET_BINARY_PV level = BINARY_NULL; @@ -216,9 +205,8 @@ bool Binary_Value_Write_Property( return false; } /* decode the some of the request */ - len = - bacapp_decode_application_data(wp_data->application_data, - wp_data->application_data_len, &value); + len = bacapp_decode_application_data( + wp_data->application_data, wp_data->application_data_len, &value); /* FIXME: len < application_data_len: more data? */ if (len < 0) { /* error while decoding - a value larger than we can handle */ @@ -241,13 +229,12 @@ bool Binary_Value_Write_Property( algorithm and may not be used for other purposes in any object. */ if (priority && (priority <= BACNET_MAX_PRIORITY) && - (priority != 6 /* reserved */ ) && + (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); + 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; diff --git a/ports/pic18f6720/device.c b/ports/pic18f6720/device.c index b800bce8..ecbaac20 100644 --- a/ports/pic18f6720/device.c +++ b/ports/pic18f6720/device.c @@ -1,36 +1,36 @@ /************************************************************************** -* -* Copyright (C) 2007 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2007 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include -#include /* for memmove */ +#include /* for memmove */ #include "bacnet/bacdef.h" #include "bacnet/bacdcode.h" #include "bacnet/bacstr.h" #include "bacnet/bacenum.h" -#include "bacnet/config.h" /* the custom stuff */ +#include "bacnet/config.h" /* the custom stuff */ #include "bacnet/apdu.h" #include "bacnet/datalink/dlmstp.h" #include "rs485.h" @@ -42,7 +42,7 @@ #include "bacnet/wp.h" #include "bacnet/dcc.h" #include "bacnet/version.h" -#include "bacnet/basic/object/device.h" /* me */ +#include "bacnet/basic/object/device.h" /* me */ /* note: you really only need to define variables for properties that are writable or that may change. @@ -54,8 +54,7 @@ static uint8_t Database_Revision; BACNET_REINITIALIZED_STATE Reinitialize_State = BACNET_REINIT_IDLE; static char Reinit_Password[16] = "filister"; -bool Device_Reinitialize( - BACNET_REINITIALIZE_DEVICE_DATA * rd_data) +bool Device_Reinitialize(BACNET_REINITIALIZE_DEVICE_DATA *rd_data) { bool status = false; @@ -98,16 +97,14 @@ bool Device_Reinitialize( return status; } -BACNET_REINITIALIZED_STATE Device_Reinitialized_State( - void) +BACNET_REINITIALIZED_STATE Device_Reinitialized_State(void) { return Reinitialize_State; } -void Device_Init( - object_functions_t * object_table) +void Device_Init(object_functions_t *object_table) { - (void) object_table; + (void)object_table; Reinitialize_State = BACNET_REINIT_IDLE; dcc_set_status_duration(COMMUNICATION_ENABLE, 0); /* FIXME: Get the data from the eeprom */ @@ -118,14 +115,12 @@ void Device_Init( } /* 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 */ @@ -144,66 +139,56 @@ bool Device_Set_Object_Instance_Number( 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); } -BACNET_DEVICE_STATUS Device_System_Status( - void) +BACNET_DEVICE_STATUS Device_System_Status(void) { return System_Status; } -int Device_Set_System_Status( - BACNET_DEVICE_STATUS status, - bool local) +int Device_Set_System_Status(BACNET_DEVICE_STATUS status, bool local) { 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 BACNET_PROTOCOL_VERSION; } -uint8_t Device_Protocol_Revision( - void) +uint8_t Device_Protocol_Revision(void) { return BACNET_PROTOCOL_REVISION; } -BACNET_SEGMENTATION Device_Segmentation_Supported( - void) +BACNET_SEGMENTATION Device_Segmentation_Supported(void) { return SEGMENTATION_NONE; } -uint32_t Device_Database_Revision( - void) +uint32_t Device_Database_Revision(void) { return Database_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; /* at least 1 for device object */ -/* FIXME: add objects as needed */ + /* FIXME: add objects as needed */ count += Binary_Value_Count(); count += Analog_Input_Count(); count += Binary_Input_Count(); @@ -215,9 +200,7 @@ unsigned Device_Object_List_Count( /* Since many network clients depend on the object list */ /* for discovery, it must be consistent! */ bool Device_Object_List_Identifier( - uint32_t array_index, - BACNET_OBJECT_TYPE *object_type, - uint32_t * instance) + uint32_t array_index, BACNET_OBJECT_TYPE *object_type, uint32_t *instance) { bool status = false; uint32_t object_index = 0; @@ -287,13 +270,12 @@ bool Device_Object_List_Identifier( } /* returns true if successful */ -int Device_Read_Property_Local( - BACNET_READ_PROPERTY_DATA * rpdata) +int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA *rpdata) { static char string_buffer[28]; static BACNET_CHARACTER_STRING char_string; - 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; uint32_t i = 0; BACNET_OBJECT_TYPE object_type = OBJECT_NONE; @@ -313,12 +295,11 @@ int Device_Read_Property_Local( /* FIXME: change the hardcoded names to suit your application */ switch (rpdata->object_property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], OBJECT_DEVICE, - Object_Instance_Number); + 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"); + (void)strcpypgm2ram(&string_buffer[0], "PIC18F6720 Device"); characterstring_init_ansi(&char_string, string_buffer); apdu_len = encode_application_character_string(&apdu[0], &char_string); @@ -327,68 +308,64 @@ int Device_Read_Property_Local( apdu_len = encode_application_enumerated(&apdu[0], OBJECT_DEVICE); break; case PROP_DESCRIPTION: - (void) strcpypgm2ram(&string_buffer[0], "BACnet Demo"); + (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()); + encode_application_enumerated(&apdu[0], Device_System_Status()); break; case PROP_VENDOR_NAME: - (void) strcpypgm2ram(&string_buffer[0], BACNET_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()); + apdu_len = encode_application_unsigned( + &apdu[0], Device_Vendor_Identifier()); break; case PROP_MODEL_NAME: - (void) strcpypgm2ram(&string_buffer[0], "GNU Demo"); + (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: - (void) strcpypgm2ram(&string_buffer[0], BACNET_VERSION_TEXT); + (void)strcpypgm2ram(&string_buffer[0], BACNET_VERSION_TEXT); characterstring_init_ansi(&char_string, string_buffer); apdu_len = encode_application_character_string(&apdu[0], &char_string); break; case PROP_APPLICATION_SOFTWARE_VERSION: - (void) strcpypgm2ram(&string_buffer[0], "1.0"); + (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"); + (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()); + 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()); + apdu_len = encode_application_unsigned( + &apdu[0], Device_Protocol_Revision()); 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)); + bitstring_set_bit( + &bit_string, (uint8_t)i, apdu_service_supported(i)); } apdu_len = encode_application_bitstring(&apdu[0], &bit_string); break; @@ -398,7 +375,7 @@ int Device_Read_Property_Local( 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); + bitstring_set_bit(&bit_string, (uint8_t)i, false); } /* FIXME: indicate the objects that YOU support */ bitstring_set_bit(&bit_string, OBJECT_DEVICE, true); @@ -419,11 +396,10 @@ int Device_Read_Property_Local( /* your maximum APDU size. */ else if (rpdata->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); + 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? */ @@ -442,11 +418,10 @@ int Device_Read_Property_Local( } } } else { - if (Device_Object_List_Identifier(rpdata->array_index, - &object_type, &instance)) - apdu_len = - encode_application_object_id(&apdu[0], object_type, - instance); + if (Device_Object_List_Identifier( + rpdata->array_index, &object_type, &instance)) + apdu_len = encode_application_object_id( + &apdu[0], object_type, instance); else { rpdata->error_class = ERROR_CLASS_PROPERTY; rpdata->error_code = ERROR_CODE_INVALID_ARRAY_INDEX; @@ -458,9 +433,8 @@ int Device_Read_Property_Local( apdu_len = encode_application_unsigned(&apdu[0], MAX_APDU); break; case PROP_SEGMENTATION_SUPPORTED: - apdu_len = - encode_application_enumerated(&apdu[0], - Device_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()); @@ -472,14 +446,12 @@ int Device_Read_Property_Local( /* FIXME: encode the list here, if it exists */ break; case PROP_DATABASE_REVISION: - apdu_len = - encode_application_unsigned(&apdu[0], - Device_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()); + encode_application_unsigned(&apdu[0], dlmstp_max_info_frames()); break; case PROP_MAX_MASTER: apdu_len = @@ -496,14 +468,14 @@ int Device_Read_Property_Local( case PROP_UTC_OFFSET: /* Note: BACnet Time Zone is offset of local time and UTC, rather than offset of GMT. It is expressed in minutes */ - apdu_len = encode_application_signed(&apdu[0], 5 * 60 /* EST */ ); + apdu_len = encode_application_signed(&apdu[0], 5 * 60 /* 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 */ + 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: @@ -531,8 +503,7 @@ int Device_Read_Property_Local( return apdu_len; } -int Device_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Device_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { int apdu_len = BACNET_STATUS_ERROR; @@ -572,10 +543,9 @@ int Device_Read_Property( return apdu_len; } -bool Device_Write_Property_Local( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Device_Write_Property_Local(BACNET_WRITE_PROPERTY_DATA *wp_data) { - bool status = false; /* return value */ + bool status = false; /* return value */ int len = 0; BACNET_APPLICATION_DATA_VALUE value; @@ -585,9 +555,8 @@ bool Device_Write_Property_Local( return false; } /* decode the some of the request */ - len = - bacapp_decode_application_data(wp_data->application_data, - wp_data->application_data_len, &value); + len = bacapp_decode_application_data( + wp_data->application_data, wp_data->application_data_len, &value); /* FIXME: len < application_data_len: more data? */ if (len < 0) { /* error while decoding - a value larger than we can handle */ @@ -606,8 +575,8 @@ bool Device_Write_Property_Local( 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))) { + (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 { @@ -660,10 +629,11 @@ bool Device_Write_Property_Local( 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. */ + 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 { wp_data->error_class = ERROR_CLASS_PROPERTY; wp_data->error_code = @@ -729,10 +699,9 @@ bool Device_Write_Property_Local( return status; } -bool Device_Write_Property( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data) { - bool status = false; /* Ever the pessamist! */ + bool status = false; /* Ever the pessamist! */ struct object_functions *pObject = NULL; /* initialize the default return values */ diff --git a/ports/pic18f6720/dlmstp.c b/ports/pic18f6720/dlmstp.c index 1bf15ffd..3ef3ac1d 100644 --- a/ports/pic18f6720/dlmstp.c +++ b/ports/pic18f6720/dlmstp.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2006 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2006 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include #include @@ -48,16 +48,18 @@ static DLMSTP_PACKET Receive_Buffer; volatile struct mstp_port_struct_t MSTP_Port; #pragma udata -#define INCREMENT_AND_LIMIT_UINT16(x) {if (x < 0xFFFF) x++;} +#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); @@ -65,8 +67,7 @@ void dlmstp_reinit( dlmstp_set_max_master(DEFAULT_MAX_MASTER); } -void dlmstp_init( - void) +void dlmstp_init(void) { uint8_t data; @@ -79,24 +80,22 @@ void dlmstp_init( MSTP_Init(&MSTP_Port); } -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 */ - BACNET_NPDU_DATA * npdu_data, /* network information */ - uint8_t * pdu, /* any data to be sent - may be null */ +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 */ +{ /* number of bytes of data */ int bytes_sent = 0; unsigned npdu_len = 0; uint8_t frame_type = 0; BACNET_ADDRESS src; - unsigned i = 0; /* loop counter */ + unsigned i = 0; /* loop counter */ if (MSTP_Port.TxReady == false) { if (npdu_data->data_expecting_reply) @@ -115,8 +114,7 @@ int dlmstp_send_pdu( if ((MAX_HEADER + pdu_len) > MAX_MPDU) { return -4; } - bytes_sent = - MSTP_Create_Frame((uint8_t *) & MSTP_Port.TxBuffer[0], + bytes_sent = MSTP_Create_Frame((uint8_t *)&MSTP_Port.TxBuffer[0], sizeof(MSTP_Port.TxBuffer), MSTP_Port.TxFrameType, MSTP_Port.TxDestination, MSTP_Port.This_Station, pdu, pdu_len); MSTP_Port.TxLength = bytes_sent; @@ -127,8 +125,7 @@ int dlmstp_send_pdu( return bytes_sent; } -void dlmstp_task( - void) +void dlmstp_task(void) { bool bytes_remaining; bool received_frame; @@ -139,8 +136,8 @@ void dlmstp_task( do { bytes_remaining = RS485_Check_UART_Data(&MSTP_Port); MSTP_Receive_Frame_FSM(&MSTP_Port); - received_frame = MSTP_Port.ReceivedValidFrame || - MSTP_Port.ReceivedInvalidFrame; + received_frame = + MSTP_Port.ReceivedValidFrame || MSTP_Port.ReceivedInvalidFrame; if (received_frame) break; } while (bytes_remaining); @@ -167,9 +164,7 @@ void dlmstp_task( 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; @@ -193,19 +188,17 @@ void dlmstp_fill_bacnet_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) { @@ -222,8 +215,7 @@ void dlmstp_set_my_address( return; } -uint8_t dlmstp_my_address( - void) +uint8_t dlmstp_my_address(void) { return MSTP_Port.This_Station; } @@ -235,8 +227,7 @@ uint8_t dlmstp_my_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( - 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; @@ -250,8 +241,7 @@ void dlmstp_set_max_info_frames( return; } -unsigned dlmstp_max_info_frames( - void) +unsigned dlmstp_max_info_frames(void) { return MSTP_Port.Nmax_info_frames; } @@ -261,8 +251,7 @@ unsigned dlmstp_max_info_frames( /* 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) { @@ -278,20 +267,18 @@ void dlmstp_set_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; - my_address->net = 0; /* local only, no routing */ + my_address->net = 0; /* local only, no routing */ my_address->len = 0; for (i = 0; i < MAX_MAC_LEN; i++) { my_address->adr[i] = 0; @@ -300,16 +287,15 @@ void dlmstp_get_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/ports/pic18f6720/isr.c b/ports/pic18f6720/isr.c index 23554963..7a97202c 100644 --- a/ports/pic18f6720/isr.c +++ b/ports/pic18f6720/isr.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2007 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2007 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include "stdint.h" #include "hardware.h" #include "rs485.h" @@ -30,42 +30,32 @@ /* 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} + _asm goto InterruptHandlerHigh _endasm +} #pragma code #pragma code InterruptVectorLow = 0x18 -void InterruptVectorLow( - void) +void InterruptVectorLow(void) { /* jump to interrupt routine */ -_asm goto InterruptHandlerLow _endasm} + _asm goto InterruptHandlerLow _endasm +} #pragma code #pragma interrupt InterruptHandlerHigh -void InterruptHandlerHigh( - void) +void InterruptHandlerHigh(void) { #if 0 /* check for USART Rx int */ @@ -92,11 +82,10 @@ void InterruptHandlerHigh( } } -#pragma interruptlow InterruptHandlerLow save = PROD, section(".tmpdata"), TABLAT, TBLPTR, section \ - ("MATH_DATA") +#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)) { @@ -133,74 +122,69 @@ void InterruptHandlerLow( RS485_Interrupt_Rx(); } -/* Unused Interrupts - //check for timer1 int - if ((PIR1bits.TMR1IF) && (PIE1bits.TMR1IE)) - { - PIR1bits.TMR1IF = 0; - Interrupt_Timer1(); - } + /* Unused Interrupts + //check for timer1 int + if ((PIR1bits.TMR1IF) && (PIE1bits.TMR1IE)) + { + PIR1bits.TMR1IF = 0; + Interrupt_Timer1(); + } - //check for compare int - if ((PIR1bits.CCP1IF) && (PIE1bits.CCP1IE)) - { - PIR1bits.CCP1IF = 0; - Interrupt_CCP1(); - } + //check for compare int + if ((PIR1bits.CCP1IF) && (PIE1bits.CCP1IE)) + { + PIR1bits.CCP1IF = 0; + Interrupt_CCP1(); + } - //check for compare int - if ((PIR3bits.CCP3IF) && (PIE3bits.CCP3IE)) - { - PIR3bits.CCP3IF = 0; - Interrupt_CCP3(); - } + //check for compare int + if ((PIR3bits.CCP3IF) && (PIE3bits.CCP3IE)) + { + PIR3bits.CCP3IF = 0; + Interrupt_CCP3(); + } - //check for compare int - if ((PIR3bits.CCP4IF) && (PIE3bits.CCP4IE)) - { - PIR3bits.CCP4IF = 0; + //check for compare int + if ((PIR3bits.CCP4IF) && (PIE3bits.CCP4IE)) + { + PIR3bits.CCP4IF = 0; - Interrupt_CCP4(); - } + Interrupt_CCP4(); + } - //check for AD int - if ((PIR1bits.ADIF) && (PIE1bits.ADIE)) - { - PIR1bits.ADIF = 0; - Interrupt_ADC(); - } + //check for AD int + if ((PIR1bits.ADIF) && (PIE1bits.ADIE)) + { + PIR1bits.ADIF = 0; + Interrupt_ADC(); + } - //check for MSSP int - if ((PIR1bits.SSPIF) && (PIE1bits.SSPIE)) - { - PIR1bits.SSPIF = 0; - Interrupt_SSP(); - } + //check for MSSP int + if ((PIR1bits.SSPIF) && (PIE1bits.SSPIE)) + { + PIR1bits.SSPIF = 0; + Interrupt_SSP(); + } -*/ + */ } -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/ports/pic18f6720/main.c b/ports/pic18f6720/main.c index 009b5815..5272be0f 100644 --- a/ports/pic18f6720/main.c +++ b/ports/pic18f6720/main.c @@ -1,31 +1,31 @@ /************************************************************************** -* -* Copyright (C) 2007 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2007 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include -#include /* for memmove */ +#include /* for memmove */ #include #include #include "stdint.h" @@ -92,8 +92,7 @@ volatile uint8_t Milliseconds = 0; volatile uint8_t Zero_Cross_Timeout = 0; -void Reinitialize( - void) +void Reinitialize(void) { uint8_t i; char name = 0; @@ -101,8 +100,7 @@ void Reinitialize( _asm reset _endasm return; } -void Global_Int( - enum INT_STATE state) +void Global_Int(enum INT_STATE state) { static uint8_t intstate = 0; @@ -125,8 +123,7 @@ void Global_Int( } } -void Hardware_Initialize( - void) +void Hardware_Initialize(void) { TRISA = 0x00; TRISB = 0x00; @@ -153,8 +150,7 @@ void Hardware_Initialize( 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(); @@ -164,8 +160,7 @@ void Initialize_Variables( Milliseconds = 0; } -void MainTasks( - void) +void MainTasks(void) { static uint16_t millisecond_counter = 0; /* Handle our millisecond counters */ @@ -180,8 +175,7 @@ void MainTasks( } } -void main( - void) +void main(void) { RCONbits.NOT_POR = 1; RCONbits.NOT_RI = 1; diff --git a/ports/pic18f6720/mstp.c b/ports/pic18f6720/mstp.c index 02e53493..21417bb6 100644 --- a/ports/pic18f6720/mstp.c +++ b/ports/pic18f6720/mstp.c @@ -150,25 +150,27 @@ #define Tusage_timeout 20 /* we need to be able to increment without rolling over */ -#define INCREMENT_AND_LIMIT_UINT8(x) {if (x < 0xFF) x++;} +#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 buffer_len, /* amount of space available */ +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 destination, /* destination address */ + 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) */ - uint8_t crc8 = 0xFF; /* used to calculate the crc value */ - uint16_t crc16 = 0xFFFF; /* used to calculate the crc value */ +{ /* 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 */ /* not enough to do a header */ @@ -209,47 +211,48 @@ unsigned MSTP_Create_Frame( 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 */ + 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 destination, /* destination address */ + 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) */ - uint8_t buffer[MAX_MPDU] = { 0 }; /* buffer for sending */ - uint16_t len = 0; /* number of bytes to send */ +{ /* 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 */ - len = (uint16_t) MSTP_Create_Frame(&buffer[0], /* where frame is loaded */ + 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 */ + 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) */ + 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; #endif #if PRINT_ENABLED_RECEIVE fprintf(stderr, - "MSTP Rx: State=%s Data=%02X hCRC=%02X Index=%u EC=%u DateLen=%u Silence=%u\n", + "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); #endif switch (mstp_port->receive_state) { - /* In the IDLE state, the node waits for the beginning of a frame. */ + /* In the IDLE state, the node waits for the beginning of a frame. + */ case MSTP_RECEIVE_STATE_IDLE: /* EatAnError */ if (mstp_port->ReceiveError == true) { @@ -283,7 +286,8 @@ void MSTP_Receive_Frame_FSM( } } break; - /* In the PREAMBLE state, the node waits for the second octet of the preamble. */ + /* 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) { @@ -330,11 +334,13 @@ void MSTP_Receive_Frame_FSM( } } break; - /* In the HEADER state, the node waits for the fixed message header. */ + /* 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 */ + /* 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; @@ -344,7 +350,8 @@ void MSTP_Receive_Frame_FSM( 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 */ + /* 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; @@ -356,9 +363,8 @@ void MSTP_Receive_Frame_FSM( 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->HeaderCRC = CRC_Calc_Header( + mstp_port->DataRegister, mstp_port->HeaderCRC); mstp_port->FrameType = mstp_port->DataRegister; mstp_port->DataAvailable = false; mstp_port->Index = 1; @@ -368,9 +374,8 @@ void MSTP_Receive_Frame_FSM( 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->HeaderCRC = CRC_Calc_Header( + mstp_port->DataRegister, mstp_port->HeaderCRC); mstp_port->DestinationAddress = mstp_port->DataRegister; mstp_port->DataAvailable = false; mstp_port->Index = 2; @@ -380,9 +385,8 @@ void MSTP_Receive_Frame_FSM( 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->HeaderCRC = CRC_Calc_Header( + mstp_port->DataRegister, mstp_port->HeaderCRC); mstp_port->SourceAddress = mstp_port->DataRegister; mstp_port->DataAvailable = false; mstp_port->Index = 3; @@ -392,9 +396,8 @@ void MSTP_Receive_Frame_FSM( 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->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; @@ -404,9 +407,8 @@ void MSTP_Receive_Frame_FSM( 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->HeaderCRC = CRC_Calc_Header( + mstp_port->DataRegister, mstp_port->HeaderCRC); mstp_port->DataLength += mstp_port->DataRegister; mstp_port->DataAvailable = false; mstp_port->Index = 5; @@ -416,21 +418,21 @@ void MSTP_Receive_Frame_FSM( 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->HeaderCRC = CRC_Calc_Header( + mstp_port->DataRegister, mstp_port->HeaderCRC); mstp_port->DataAvailable = false; /* don't wait for next state - do it here */ if (mstp_port->HeaderCRC != 0x55) { /* BadCRC */ - /* indicate that an error has occurred during the reception of a frame */ + /* 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_port->This_Station) || + (mstp_port->DestinationAddress == MSTP_BROADCAST_ADDRESS)) { /* FrameTooLong */ if (mstp_port->DataLength > MAX_MPDU) { @@ -443,7 +445,8 @@ void MSTP_Receive_Frame_FSM( } /* NoData */ else if (mstp_port->DataLength == 0) { - /* indicate that a frame with no data has been received */ + /* 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 = @@ -465,7 +468,6 @@ void MSTP_Receive_Frame_FSM( } } - } /* not per MS/TP standard, but it is a case not covered */ else { @@ -480,11 +482,13 @@ void MSTP_Receive_Frame_FSM( } } break; - /* In the DATA state, the node waits for the data portion of a frame. */ + /* 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 */ + /* 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; @@ -493,7 +497,8 @@ void MSTP_Receive_Frame_FSM( 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 */ + /* 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; @@ -503,9 +508,8 @@ void MSTP_Receive_Frame_FSM( #endif /* DataOctet */ if (mstp_port->Index < mstp_port->DataLength) { - mstp_port->DataCRC = - CRC_Calc_Data(mstp_port->DataRegister, - mstp_port->DataCRC); + mstp_port->DataCRC = CRC_Calc_Data( + mstp_port->DataRegister, mstp_port->DataCRC); mstp_port->InputBuffer[mstp_port->Index] = mstp_port->DataRegister; mstp_port->Index++; @@ -513,17 +517,15 @@ void MSTP_Receive_Frame_FSM( } /* CRC1 */ else if (mstp_port->Index == mstp_port->DataLength) { - mstp_port->DataCRC = - CRC_Calc_Data(mstp_port->DataRegister, - mstp_port->DataCRC); + 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); + 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) @@ -553,11 +555,10 @@ void MSTP_Receive_Frame_FSM( return; } -static bool mstp_compare_data_expecting_reply( - uint8_t * request_pdu, +static bool mstp_compare_data_expecting_reply(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) { @@ -578,9 +579,8 @@ static bool mstp_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], NULL, &request.address, - &request.npdu_data); + offset = npdu_decode( + &request_pdu[0], NULL, &request.address, &request.npdu_data); if (request.npdu_data.network_layer_message) { return false; } @@ -597,8 +597,7 @@ static bool mstp_compare_data_expecting_reply( /* decode the reply data */ reply.address.mac[0] = dest_address; reply.address.mac_len = 1; - offset = - npdu_decode(&reply_pdu[0], &reply.address, NULL, &reply.npdu_data); + offset = npdu_decode(&reply_pdu[0], &reply.address, NULL, &reply.npdu_data); if (reply.npdu_data.network_layer_message) { return false; } @@ -639,7 +638,8 @@ static bool mstp_compare_data_expecting_reply( return false; } } - if (request.npdu_data.protocol_version != reply.npdu_data.protocol_version) { + if (request.npdu_data.protocol_version != + reply.npdu_data.protocol_version) { return false; } #if 0 @@ -657,8 +657,7 @@ static bool mstp_compare_data_expecting_reply( } /* 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; @@ -684,11 +683,12 @@ bool MSTP_Master_Node_FSM( 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, + "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 @@ -711,7 +711,7 @@ bool MSTP_Master_Node_FSM( /* LostToken */ if (mstp_port->SilenceTimer >= Tno_token) { /* assume that the token has been lost */ - mstp_port->EventCount = 0; /* Addendum 135-2004d-8 */ + mstp_port->EventCount = 0; /* Addendum 135-2004d-8 */ mstp_port->master_state = MSTP_MASTER_STATE_NO_TOKEN; /* set the receive frame flags to false in case we received some bytes and had a timeout for some reason */ @@ -727,16 +727,17 @@ bool MSTP_Master_Node_FSM( } 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: 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)) { + if ((mstp_port->DestinationAddress == + mstp_port->This_Station) || + (mstp_port->DestinationAddress == MSTP_BROADCAST_ADDRESS)) { switch (mstp_port->FrameType) { /* ReceivedToken */ case FRAME_TYPE_TOKEN: @@ -763,16 +764,18 @@ bool MSTP_Master_Node_FSM( } break; case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: - /* indicate successful reception to the higher layers */ + /* indicate successful reception to the higher + * layers */ dlmstp_put_receive(mstp_port->SourceAddress, - (uint8_t *) & mstp_port->InputBuffer[0], + (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 */ + /* indicate successful reception to the higher + * layers */ dlmstp_put_receive(mstp_port->SourceAddress, - (uint8_t *) & mstp_port->InputBuffer[0], + (uint8_t *)&mstp_port->InputBuffer[0], mstp_port->DataLength); /* broadcast DER just remains IDLE */ if (mstp_port->DestinationAddress != @@ -786,7 +789,7 @@ bool MSTP_Master_Node_FSM( FRAME_TYPE_TEST_RESPONSE, mstp_port->SourceAddress, mstp_port->This_Station, - (uint8_t *) & mstp_port->InputBuffer[0], + (uint8_t *)&mstp_port->InputBuffer[0], mstp_port->DataLength); break; case FRAME_TYPE_TEST_RESPONSE: @@ -808,8 +811,8 @@ bool MSTP_Master_Node_FSM( transition_now = true; } else { uint8_t destination = mstp_port->TxBuffer[3]; - RS485_Send_Frame(mstp_port, - (uint8_t *) & mstp_port->TxBuffer[0], mstp_port->TxLength); + 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: @@ -845,8 +848,10 @@ bool MSTP_Master_Node_FSM( 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 */ + /* 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 { @@ -854,8 +859,7 @@ bool MSTP_Master_Node_FSM( /* InvalidFrame */ /* error in frame reception */ mstp_port->ReceivedInvalidFrame = false; - mstp_port->master_state = - MSTP_MASTER_STATE_DONE_WITH_TOKEN; + mstp_port->master_state = MSTP_MASTER_STATE_DONE_WITH_TOKEN; transition_now = true; } else if (mstp_port->ReceivedValidFrame == true) { if (mstp_port->DestinationAddress == @@ -872,10 +876,14 @@ bool MSTP_Master_Node_FSM( 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], + /* 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; @@ -890,7 +898,7 @@ bool MSTP_Master_Node_FSM( } else { /* ReceivedUnexpectedFrame */ /* an unexpected frame was received */ - /* This may indicate the presence of multiple tokens. */ + /* 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; @@ -927,7 +935,8 @@ bool MSTP_Master_Node_FSM( (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). */ + /* 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; @@ -935,9 +944,12 @@ bool MSTP_Master_Node_FSM( } 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. */ + /* 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, @@ -957,11 +969,12 @@ bool MSTP_Master_Node_FSM( /* 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 */ + 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; + mstp_port->master_state = MSTP_MASTER_STATE_POLL_FOR_MASTER; } else { /* ResetMaintenancePFM */ mstp_port->Poll_Station = mstp_port->This_Station; @@ -970,7 +983,8 @@ bool MSTP_Master_Node_FSM( 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->TokenCount = + 1; /* changed in Errata SSPC-135-2004 */ mstp_port->EventCount = 0; mstp_port->master_state = MSTP_MASTER_STATE_PASS_TOKEN; } @@ -990,7 +1004,8 @@ bool MSTP_Master_Node_FSM( 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. */ + /* 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; @@ -1019,16 +1034,18 @@ bool MSTP_Master_Node_FSM( 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 */ + /* 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; + 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 */ + /* 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); @@ -1036,7 +1053,8 @@ bool MSTP_Master_Node_FSM( 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. */ + /* Enter the IDLE state to receive and process the incoming + * frame. */ mstp_port->master_state = MSTP_MASTER_STATE_IDLE; transition_now = true; } @@ -1056,10 +1074,11 @@ bool MSTP_Master_Node_FSM( 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; + /* 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; @@ -1068,8 +1087,9 @@ bool MSTP_Master_Node_FSM( /* 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 == + if ((mstp_port->DestinationAddress == + mstp_port->This_Station) && + (mstp_port->FrameType == FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER)) { /* ReceivedReplyToPFM */ mstp_port->SoleMaster = false; @@ -1128,7 +1148,8 @@ bool MSTP_Master_Node_FSM( /* Re-enter the current state. */ } else { /* DeclareSoleMaster */ - /* to indicate that this station is the only master */ + /* to indicate that this station is the only master + */ mstp_port->SoleMaster = true; mstp_port->FrameCount = 0; mstp_port->master_state = @@ -1148,11 +1169,10 @@ bool MSTP_Master_Node_FSM( /* Compare the APDU type received and see if the message is that same APDU type along with the matching src/dest and invoke ID */ - matched = - mstp_compare_data_expecting_reply(&mstp_port->InputBuffer - [0], mstp_port->DataLength, mstp_port->SourceAddress, - &mstp_port->TxBuffer[8], mstp_port->TxLength, - mstp_port->TxDestination); + matched = mstp_compare_data_expecting_reply( + &mstp_port->InputBuffer[0], mstp_port->DataLength, + mstp_port->SourceAddress, &mstp_port->TxBuffer[8], + mstp_port->TxLength, mstp_port->TxDestination); } if (matched && mstp_port->TxReady) { /* Reply */ @@ -1160,10 +1180,11 @@ bool MSTP_Master_Node_FSM( /* 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 */ + /* 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); + 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 if (mstp_port->SilenceTimer > Treply_delay) { @@ -1193,10 +1214,9 @@ bool MSTP_Master_Node_FSM( /* 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; @@ -1223,7 +1243,7 @@ void MSTP_Init( mstp_port->ReceivedValidFrame = false; mstp_port->RetryCount = 0; mstp_port->SilenceTimer = 0; -/* mstp_port->ReplyPostponedTimer = 0; */ + /* mstp_port->ReplyPostponedTimer = 0; */ mstp_port->SoleMaster = false; mstp_port->SourceAddress = 0; mstp_port->TokenCount = 0; @@ -1233,7 +1253,8 @@ void MSTP_Init( mstp_port->Nmax_master = DEFAULT_MAX_MASTER; #endif - /* An array of octets, used to store PDU octets prior to being transmitted. */ + /* An array of octets, used to store PDU octets prior to being + * transmitted. */ /* This array is only used for APDU messages */ for (i = 0; i < sizeof(mstp_port->TxBuffer); i++) { mstp_port->TxBuffer[i] = 0; @@ -1241,6 +1262,5 @@ void MSTP_Init( mstp_port->TxLength = 0; mstp_port->TxReady = false; mstp_port->TxFrameType = 0; - } } diff --git a/ports/pic18f6720/rs485.c b/ports/pic18f6720/rs485.c index 86fa51b7..ca9678a0 100644 --- a/ports/pic18f6720/rs485.c +++ b/ports/pic18f6720/rs485.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2005 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2005 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* The module handles sending data out the RS-485 port */ /* and handles receiving data from the RS-485 port. */ @@ -52,17 +52,17 @@ volatile uint8_t RS485_Tx_Buffer[128]; #pragma udata /**************************************************************************** -* DESCRIPTION: Transmits a frame using the UART -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ + * DESCRIPTION: Transmits a frame using the UART + * RETURN: none + * 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) */ + 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) @@ -102,13 +102,12 @@ void RS485_Send_Frame( } /**************************************************************************** -* DESCRIPTION: Checks for data on the receive UART, and handles errors -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -bool RS485_Check_UART_Data( - volatile struct mstp_port_struct_t * mstp_port) + * DESCRIPTION: Checks for data on the receive UART, and handles errors + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +bool RS485_Check_UART_Data(volatile struct mstp_port_struct_t *mstp_port) { /* check for data */ if (!FIFO_Empty(&FIFO_Rx)) { @@ -128,8 +127,7 @@ bool RS485_Check_UART_Data( NOTES: none *************************************************************************** */ -void RS485_Interrupt_Rx( - void) +void RS485_Interrupt_Rx(void) { uint8_t data_byte; @@ -154,14 +152,14 @@ void RS485_Interrupt_Rx( NOTES: none *************************************************************************** */ -void RS485_Interrupt_Tx( - void) +void RS485_Interrupt_Tx(void) { if (!FIFO_Empty(&FIFO_Tx)) { TXREG2 = FIFO_Get(&FIFO_Tx); } else { /* wait for the USART to be empty */ - while (!TXSTA2bits.TRMT); + while (!TXSTA2bits.TRMT) + ; /* disable this interrupt */ PIE3bits.TX2IE = 0; /* enable the receiver */ @@ -174,25 +172,23 @@ void RS485_Interrupt_Tx( } /**************************************************************************** -* DESCRIPTION: Returns the baud rate that we are currently running at -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -uint32_t RS485_Get_Baud_Rate( - void) + * DESCRIPTION: Returns the baud rate that we are currently running at + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +uint32_t RS485_Get_Baud_Rate(void) { return RS485_Baud_Rate; } /**************************************************************************** -* DESCRIPTION: Sets the baud rate for the chip USART -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -bool RS485_Set_Baud_Rate( - uint32_t baud) + * DESCRIPTION: Sets the baud rate for the chip USART + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +bool RS485_Set_Baud_Rate(uint32_t baud) { bool valid = true; @@ -223,16 +219,14 @@ bool RS485_Set_Baud_Rate( } /**************************************************************************** -* DESCRIPTION: Initializes the RS485 hardware and variables, and starts in -* receive mode. -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -void RS485_Initialize_Port( - void) + * DESCRIPTION: Initializes the RS485 hardware and variables, and starts in + * receive mode. + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +void RS485_Initialize_Port(void) { - /* Reset USART registers to POR state */ TXSTA2 = 0; RCSTA2 = 0; @@ -304,39 +298,36 @@ void RS485_Initialize_Port( } /**************************************************************************** -* DESCRIPTION: Disables the RS485 hardware -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -void RS485_Disable_Port( - void) + * DESCRIPTION: Disables the RS485 hardware + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +void RS485_Disable_Port(void) { - RCSTA2 &= 0x4F; /* Disable the receiver */ - TXSTA2bits.TXEN = 0; /* and transmitter */ - PIE3 &= 0xCF; /* Disable both interrupts */ + RCSTA2 &= 0x4F; /* Disable the receiver */ + TXSTA2bits.TXEN = 0; /* and transmitter */ + PIE3 &= 0xCF; /* Disable both interrupts */ } /**************************************************************************** -* DESCRIPTION: Reinitializes the port -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -void RS485_Reinit( - void) + * DESCRIPTION: Reinitializes the port + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +void RS485_Reinit(void) { RS485_Set_Baud_Rate(38400); } /**************************************************************************** -* DESCRIPTION: Initializes the data and the port -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -void RS485_Initialize( - void) + * DESCRIPTION: Initializes the data and the port + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +void RS485_Initialize(void) { /* Init the Rs485 buffers */ FIFO_Init(&FIFO_Rx, RS485_Rx_Buffer, sizeof(RS485_Rx_Buffer)); diff --git a/ports/pic18f97j60/ai.c b/ports/pic18f97j60/ai.c index 2d73e9e1..b5112c68 100644 --- a/ports/pic18f97j60/ai.c +++ b/ports/pic18f97j60/ai.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2005 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2005 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* Analog Input Objects customize for your use */ @@ -44,8 +44,7 @@ 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; @@ -54,34 +53,30 @@ bool Analog_Input_Valid_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 */ + static char text_string[16] = ""; /* okay for single thread */ if (object_instance < MAX_ANALOG_INPUTS) { - sprintf(text_string, "AI-%lu", (unsigned long) object_instance); + sprintf(text_string, "AI-%lu", (unsigned long)object_instance); return text_string; } return NULL; } -float Analog_Input_Present_Value( - uint32_t object_instance) +float Analog_Input_Present_Value(uint32_t object_instance) { float value = 0.0; @@ -91,9 +86,7 @@ float Analog_Input_Present_Value( return value; } -void Analog_Input_Present_Value_Set( - uint32_t object_instance, - float value) +void Analog_Input_Present_Value_Set(uint32_t object_instance, float value) { if (object_instance < MAX_ANALOG_INPUTS) { Present_Value[object_instance] = value; @@ -102,10 +95,9 @@ void Analog_Input_Present_Value_Set( /* return apdu length, or -1 on error */ /* assumption - object has already exists */ -int Analog_Input_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Analog_Input_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ BACNET_BIT_STRING bit_string; BACNET_CHARACTER_STRING char_string; uint8_t *apdu = NULL; @@ -117,16 +109,15 @@ int Analog_Input_Read_Property( apdu = rpdata->application_data; switch (rpdata->object_property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], OBJECT_ANALOG_INPUT, - rpdata->object_instance); + apdu_len = encode_application_object_id( + &apdu[0], OBJECT_ANALOG_INPUT, rpdata->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(rpdata->object_instance)); + characterstring_init_ansi( + &char_string, Analog_Input_Name(rpdata->object_instance)); apdu_len = encode_application_character_string(&apdu[0], &char_string); break; @@ -135,9 +126,8 @@ int Analog_Input_Read_Property( encode_application_enumerated(&apdu[0], OBJECT_ANALOG_INPUT); break; case PROP_PRESENT_VALUE: - apdu_len = - encode_application_real(&apdu[0], - Analog_Input_Present_Value(rpdata->object_instance)); + apdu_len = encode_application_real( + &apdu[0], Analog_Input_Present_Value(rpdata->object_instance)); break; case PROP_STATUS_FLAGS: bitstring_init(&bit_string); diff --git a/ports/pic18f97j60/apdu.c b/ports/pic18f97j60/apdu.c index 85005fe8..3a776d0d 100644 --- a/ports/pic18f97j60/apdu.c +++ b/ports/pic18f97j60/apdu.c @@ -43,20 +43,17 @@ /* me */ #include "bacnet/apdu.h" -uint16_t apdu_timeout( - void) +uint16_t apdu_timeout(void) { return 3000; } -uint8_t apdu_retries( - void) +uint8_t apdu_retries(void) { return 3; } -bool apdu_service_supported( - BACNET_SERVICES_SUPPORTED service_supported) +bool apdu_service_supported(BACNET_SERVICES_SUPPORTED service_supported) { bool status = false; @@ -75,15 +72,14 @@ bool apdu_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) + BACNET_CONFIRMED_SERVICE_DATA *service_data, + uint8_t *service_choice, + 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] & BIT(3)) ? true : false; service_data->more_follows = (apdu[0] & BIT(2)) ? true : false; @@ -110,8 +106,7 @@ uint16_t apdu_decode_confirmed_service_request( When the initiation of communications is disabled, all APDUs shall be processed and responses returned as required... */ -static bool apdu_confirmed_dcc_disabled( - uint8_t service_choice) +static bool apdu_confirmed_dcc_disabled(uint8_t service_choice) { bool status = false; @@ -136,8 +131,7 @@ static bool apdu_confirmed_dcc_disabled( DISABLE_INITIATION, the responding BACnet-user shall discontinue the initiation of messages except for I-Am requests issued in accordance with the Who-Is service procedure.*/ -static bool apdu_unconfirmed_dcc_disabled( - uint8_t service_choice) +static bool apdu_unconfirmed_dcc_disabled(uint8_t service_choice) { bool status = false; @@ -159,36 +153,37 @@ static bool apdu_unconfirmed_dcc_disabled( return status; } -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 */ + len = apdu_decode_confirmed_service_request( + &apdu[0], /* APDU data */ apdu_len, &service_data, &service_choice, &service_request, &service_request_len); if (apdu_confirmed_dcc_disabled(service_choice)) { /* When network communications are completely disabled, - only DeviceCommunicationControl and ReinitializeDevice APDUs - shall be processed and no messages shall be initiated. */ + only DeviceCommunicationControl and ReinitializeDevice + APDUs shall be processed and no messages shall be + initiated. */ break; } if (service_choice == SERVICE_CONFIRMED_READ_PROPERTY) { handler_read_property(service_request, service_request_len, src, &service_data); } else if (service_choice == SERVICE_CONFIRMED_WRITE_PROPERTY) { - handler_write_property(service_request, - service_request_len, src, &service_data); + handler_write_property(service_request, service_request_len, + src, &service_data); } else if (service_choice == SERVICE_CONFIRMED_REINITIALIZE_DEVICE) { handler_reinitialize_device(service_request, @@ -208,10 +203,10 @@ void apdu_handler( service_request_len = apdu_len - 2; if (apdu_unconfirmed_dcc_disabled(service_choice)) { /* When network communications are disabled, - only DeviceCommunicationControl and ReinitializeDevice APDUs - shall be processed and no messages shall be initiated. - If communications have been initiation disabled, then - WhoIs may be processed. */ + only DeviceCommunicationControl and ReinitializeDevice + APDUs shall be processed and no messages shall be + initiated. If communications have been initiation + disabled, then WhoIs may be processed. */ break; } if (service_choice == SERVICE_UNCONFIRMED_WHO_IS) { diff --git a/ports/pic18f97j60/av.c b/ports/pic18f97j60/av.c index c77677df..ef1ddd31 100644 --- a/ports/pic18f97j60/av.c +++ b/ports/pic18f97j60/av.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2006 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2006 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* Analog Value Objects - customize for your use */ @@ -32,7 +32,7 @@ #include "bacnet/bacdcode.h" #include "bacnet/bacenum.h" #include "bacnet/bacapp.h" -#include "bacnet/config.h" /* the custom stuff */ +#include "bacnet/config.h" /* the custom stuff */ #include "bacnet/wp.h" #include "bacnet/rp.h" #include "bacnet/basic/object/av.h" @@ -54,8 +54,7 @@ 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; @@ -74,8 +73,7 @@ void Analog_Value_Init( /* 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) @@ -86,8 +84,7 @@ bool Analog_Value_Valid_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; @@ -96,8 +93,7 @@ unsigned Analog_Value_Count( /* 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; @@ -106,8 +102,7 @@ uint32_t Analog_Value_Index_To_Instance( /* 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; @@ -118,8 +113,7 @@ unsigned Analog_Value_Instance_To_Index( return index; } -float Analog_Value_Present_Value( - uint32_t object_instance) +float Analog_Value_Present_Value(uint32_t object_instance) { float value = ANALOG_RELINQUISH_DEFAULT; unsigned index = 0; @@ -135,10 +129,9 @@ float Analog_Value_Present_Value( } /* 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 */ + static char text_string[32] = ""; /* okay for single thread */ if (object_instance < MAX_ANALOG_VALUES) { sprintf(text_string, "AV-%lu", object_instance); @@ -149,14 +142,13 @@ char *Analog_Value_Name( } /* return apdu len, or -1 on error */ -int Analog_Value_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Analog_Value_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { 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; + float real_value = (float)1.414; unsigned object_index = 0; unsigned i = 0; bool state = false; @@ -170,14 +162,13 @@ int Analog_Value_Read_Property( apdu = rpdata->application_data; switch (rpdata->object_property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], OBJECT_ANALOG_VALUE, - rpdata->object_instance); + apdu_len = encode_application_object_id( + &apdu[0], OBJECT_ANALOG_VALUE, rpdata->object_instance); break; case PROP_OBJECT_NAME: case PROP_DESCRIPTION: - characterstring_init_ansi(&char_string, - Analog_Value_Name(rpdata->object_instance)); + characterstring_init_ansi( + &char_string, Analog_Value_Name(rpdata->object_instance)); apdu_len = encode_application_character_string(&apdu[0], &char_string); break; @@ -287,10 +278,9 @@ int Analog_Value_Read_Property( } /* returns true if successful */ -bool Analog_Value_Write_Property( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Analog_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data) { - bool status = false; /* return value */ + bool status = false; /* return value */ unsigned int object_index = 0; unsigned int priority = 0; uint8_t level = ANALOG_LEVEL_NULL; @@ -304,9 +294,8 @@ bool Analog_Value_Write_Property( return false; } /* decode the some of the request */ - len = - bacapp_decode_application_data(wp_data->application_data, - wp_data->application_data_len, &value); + len = bacapp_decode_application_data( + wp_data->application_data, wp_data->application_data_len, &value); /* FIXME: len < application_data_len: more data? */ if (len < 0) { /* error while decoding - a value larger than we can handle */ @@ -329,19 +318,19 @@ bool Analog_Value_Write_Property( algorithm and may not be used for other purposes in any object. */ if (priority && (priority <= BACNET_MAX_PRIORITY) && - (priority != 6 /* reserved */ ) && + (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); + 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) */ + 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 diff --git a/ports/pic18f97j60/bi.c b/ports/pic18f97j60/bi.c index 86fe747e..e3c39650 100644 --- a/ports/pic18f97j60/bi.c +++ b/ports/pic18f97j60/bi.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2006 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2006 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* Binary Input Objects customize for your use */ @@ -40,8 +40,7 @@ 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; @@ -55,8 +54,7 @@ static void Binary_Input_Initialize( } /* 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; @@ -65,15 +63,13 @@ bool Binary_Input_Valid_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; } @@ -81,8 +77,7 @@ uint32_t Binary_Input_Index_To_Instance( /* 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; @@ -92,8 +87,7 @@ unsigned Binary_Input_Instance_To_Index( return index; } -BACNET_BINARY_PV Binary_Input_Present_Value( - uint32_t object_instance) +BACNET_BINARY_PV Binary_Input_Present_Value(uint32_t object_instance) { BACNET_BINARY_PV value = BINARY_INACTIVE; unsigned index = 0; @@ -107,10 +101,9 @@ BACNET_BINARY_PV Binary_Input_Present_Value( 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 */ + static char text_string[16] = ""; /* okay for single thread */ if (object_instance < MAX_BINARY_INPUTS) { sprintf(text_string, "BI-%lu", object_instance); @@ -122,10 +115,9 @@ char *Binary_Input_Name( /* return apdu length, or -1 on error */ /* assumption - object already exists, and has been bounds checked */ -int Binary_Input_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Binary_Input_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { - 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; @@ -140,15 +132,14 @@ int Binary_Input_Read_Property( apdu = rpdata->application_data; switch (rpdata->object_property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], OBJECT_BINARY_INPUT, - rpdata->object_instance); + apdu_len = encode_application_object_id( + &apdu[0], OBJECT_BINARY_INPUT, rpdata->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(rpdata->object_instance)); + characterstring_init_ansi( + &char_string, Binary_Input_Name(rpdata->object_instance)); apdu_len = encode_application_character_string(&apdu[0], &char_string); break; diff --git a/ports/pic18f97j60/bv.c b/ports/pic18f97j60/bv.c index a57f57e4..7c0f5901 100644 --- a/ports/pic18f97j60/bv.c +++ b/ports/pic18f97j60/bv.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2006 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2006 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* Binary Value Objects - customize for your use */ @@ -31,7 +31,7 @@ #include "bacnet/bacdef.h" #include "bacnet/bacdcode.h" #include "bacnet/bacenum.h" -#include "bacnet/config.h" /* the custom stuff */ +#include "bacnet/config.h" /* the custom stuff */ #include "bacnet/wp.h" #include "bacnet/rp.h" #include "bacnet/basic/object/bv.h" @@ -40,8 +40,7 @@ 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; @@ -55,8 +54,7 @@ static void Binary_Value_Initialize( } /* 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; @@ -65,22 +63,19 @@ bool Binary_Value_Valid_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; @@ -90,8 +85,7 @@ unsigned Binary_Value_Instance_To_Index( return index; } -BACNET_BINARY_PV Binary_Value_Present_Value( - uint32_t object_instance) +BACNET_BINARY_PV Binary_Value_Present_Value(uint32_t object_instance) { BACNET_BINARY_PV value = BINARY_INACTIVE; @@ -104,10 +98,9 @@ BACNET_BINARY_PV Binary_Value_Present_Value( } /* 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 */ + static char text_string[16] = ""; /* okay for single thread */ if (object_instance < MAX_BINARY_VALUES) { sprintf(text_string, "BV-%lu", object_instance); @@ -118,11 +111,10 @@ char *Binary_Value_Name( } /* return apdu len, or -1 on error */ -int Binary_Value_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Binary_Value_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { 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; @@ -140,16 +132,15 @@ int Binary_Value_Read_Property( apdu = rpdata->application_data; switch (rpdata->object_property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], OBJECT_BINARY_VALUE, - rpdata->object_instance); + apdu_len = encode_application_object_id( + &apdu[0], OBJECT_BINARY_VALUE, rpdata->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(rpdata->object_instance)); + characterstring_init_ansi( + &char_string, Binary_Value_Name(rpdata->object_instance)); apdu_len = encode_application_character_string(&apdu[0], &char_string); break; @@ -158,8 +149,7 @@ int Binary_Value_Read_Property( encode_application_enumerated(&apdu[0], OBJECT_BINARY_VALUE); break; case PROP_PRESENT_VALUE: - present_value = - Binary_Value_Present_Value(rpdata->object_instance); + present_value = Binary_Value_Present_Value(rpdata->object_instance); apdu_len = encode_application_enumerated(&apdu[0], present_value); break; case PROP_STATUS_FLAGS: @@ -200,10 +190,9 @@ int Binary_Value_Read_Property( } /* returns true if successful */ -bool Binary_Value_Write_Property( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Binary_Value_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data) { - bool status = false; /* return value */ + bool status = false; /* return value */ unsigned int object_index = 0; unsigned int priority = 0; BACNET_BINARY_PV level = BINARY_NULL; @@ -216,9 +205,8 @@ bool Binary_Value_Write_Property( return false; } /* decode the some of the request */ - len = - bacapp_decode_application_data(wp_data->application_data, - wp_data->application_data_len, &value); + len = bacapp_decode_application_data( + wp_data->application_data, wp_data->application_data_len, &value); /* FIXME: len < application_data_len: more data? */ if (len < 0) { /* error while decoding - a value larger than we can handle */ @@ -241,13 +229,12 @@ bool Binary_Value_Write_Property( algorithm and may not be used for other purposes in any object. */ if (priority && (priority <= BACNET_MAX_PRIORITY) && - (priority != 6 /* reserved */ ) && + (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); + 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; diff --git a/ports/pic18f97j60/device.c b/ports/pic18f97j60/device.c index 57a60b51..58d239f3 100644 --- a/ports/pic18f97j60/device.c +++ b/ports/pic18f97j60/device.c @@ -1,36 +1,36 @@ /************************************************************************** -* -* Copyright (C) 2007 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2007 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include -#include /* for memmove */ +#include /* for memmove */ #include "bacnet/bacdef.h" #include "bacnet/bacdcode.h" #include "bacnet/bacstr.h" #include "bacnet/bacenum.h" -#include "bacnet/config.h" /* the custom stuff */ +#include "bacnet/config.h" /* the custom stuff */ #include "bacnet/apdu.h" #include "bacnet/datalink/dlmstp.h" #include "rs485.h" @@ -42,7 +42,7 @@ #include "bacnet/wp.h" #include "bacnet/dcc.h" #include "bacnet/version.h" -#include "bacnet/basic/object/device.h" /* me */ +#include "bacnet/basic/object/device.h" /* me */ /* note: you really only need to define variables for properties that are writable or that may change. @@ -53,8 +53,7 @@ static BACNET_DEVICE_STATUS System_Status = STATUS_OPERATIONAL; static uint8_t Database_Revision; BACNET_REINITIALIZED_STATE Reinitialize_State = BACNET_REINIT_IDLE; -bool Device_Reinitialize( - BACNET_REINITIALIZE_DEVICE_DATA * rd_data) +bool Device_Reinitialize(BACNET_REINITIALIZE_DEVICE_DATA *rd_data) { bool status = false; char password[16] = "filister"; @@ -76,16 +75,14 @@ bool Device_Reinitialize( return status; } -BACNET_REINITIALIZED_STATE Device_Reinitialized_State( - void) +BACNET_REINITIALIZED_STATE Device_Reinitialized_State(void) { return Reinitialize_State; } -void Device_Init( - object_functions_t * object_table) +void Device_Init(object_functions_t *object_table) { - (void) object_table; + (void)object_table; Reinitialize_State = BACNET_REINIT_IDLE; dcc_set_status_duration(COMMUNICATION_ENABLE, 0); /* FIXME: Get the data from the eeprom */ @@ -96,14 +93,12 @@ void Device_Init( } /* 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 */ @@ -122,66 +117,56 @@ bool Device_Set_Object_Instance_Number( 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); } -BACNET_DEVICE_STATUS Device_System_Status( - void) +BACNET_DEVICE_STATUS Device_System_Status(void) { return System_Status; } -int Device_Set_System_Status( - BACNET_DEVICE_STATUS status, - bool local) +int Device_Set_System_Status(BACNET_DEVICE_STATUS status, bool local) { 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 BACNET_PROTOCOL_VERSION; } -uint8_t Device_Protocol_Revision( - void) +uint8_t Device_Protocol_Revision(void) { return BACNET_PROTOCOL_REVISION; } -BACNET_SEGMENTATION Device_Segmentation_Supported( - void) +BACNET_SEGMENTATION Device_Segmentation_Supported(void) { return SEGMENTATION_NONE; } -uint32_t Device_Database_Revision( - void) +uint32_t Device_Database_Revision(void) { return Database_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; /* at least 1 for device object */ -/* FIXME: add objects as needed */ + /* FIXME: add objects as needed */ count += Binary_Value_Count(); count += Analog_Input_Count(); count += Binary_Input_Count(); @@ -193,9 +178,7 @@ unsigned Device_Object_List_Count( /* Since many network clients depend on the object list */ /* for discovery, it must be consistent! */ bool Device_Object_List_Identifier( - uint32_t array_index, - BACNET_OBJECT_TYPE *object_type, - uint32_t * instance) + uint32_t array_index, BACNET_OBJECT_TYPE *object_type, uint32_t *instance) { bool status = false; uint32_t object_index = 0; @@ -265,11 +248,10 @@ bool Device_Object_List_Identifier( } /* returns true if successful */ -int Device_Read_Property_Local( - BACNET_READ_PROPERTY_DATA * rpdata) +int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA *rpdata) { - 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; uint32_t i = 0; @@ -291,12 +273,11 @@ int Device_Read_Property_Local( /* FIXME: change the hardcoded names to suit your application */ switch (rpdata->object_property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], OBJECT_DEVICE, - Object_Instance_Number); + 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"); + (void)strcpypgm2ram(&string_buffer[0], "PIC18F6720 Device"); characterstring_init_ansi(&char_string, string_buffer); apdu_len = encode_application_character_string(&apdu[0], &char_string); @@ -305,68 +286,64 @@ int Device_Read_Property_Local( apdu_len = encode_application_enumerated(&apdu[0], OBJECT_DEVICE); break; case PROP_DESCRIPTION: - (void) strcpypgm2ram(&string_buffer[0], "BACnet Demo"); + (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()); + encode_application_enumerated(&apdu[0], Device_System_Status()); break; case PROP_VENDOR_NAME: - (void) strcpypgm2ram(&string_buffer[0], BACNET_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()); + apdu_len = encode_application_unsigned( + &apdu[0], Device_Vendor_Identifier()); break; case PROP_MODEL_NAME: - (void) strcpypgm2ram(&string_buffer[0], "GNU Demo"); + (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: - (void) strcpypgm2ram(&string_buffer[0], BACNET_VERSION_TEXT); + (void)strcpypgm2ram(&string_buffer[0], BACNET_VERSION_TEXT); characterstring_init_ansi(&char_string, string_buffer); apdu_len = encode_application_character_string(&apdu[0], &char_string); break; case PROP_APPLICATION_SOFTWARE_VERSION: - (void) strcpypgm2ram(&string_buffer[0], "1.0"); + (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"); + (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()); + 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()); + apdu_len = encode_application_unsigned( + &apdu[0], Device_Protocol_Revision()); 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)); + bitstring_set_bit( + &bit_string, (uint8_t)i, apdu_service_supported(i)); } apdu_len = encode_application_bitstring(&apdu[0], &bit_string); break; @@ -376,7 +353,7 @@ int Device_Read_Property_Local( 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); + bitstring_set_bit(&bit_string, (uint8_t)i, false); } /* FIXME: indicate the objects that YOU support */ bitstring_set_bit(&bit_string, OBJECT_DEVICE, true); @@ -397,11 +374,10 @@ int Device_Read_Property_Local( /* your maximum APDU size. */ else if (rpdata->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); + 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? */ @@ -420,11 +396,10 @@ int Device_Read_Property_Local( } } } else { - if (Device_Object_List_Identifier(rpdata->array_index, - &object_type, &instance)) - apdu_len = - encode_application_object_id(&apdu[0], object_type, - instance); + if (Device_Object_List_Identifier( + rpdata->array_index, &object_type, &instance)) + apdu_len = encode_application_object_id( + &apdu[0], object_type, instance); else { rpdata->error_class = ERROR_CLASS_PROPERTY; rpdata->error_code = ERROR_CODE_INVALID_ARRAY_INDEX; @@ -436,9 +411,8 @@ int Device_Read_Property_Local( apdu_len = encode_application_unsigned(&apdu[0], MAX_APDU); break; case PROP_SEGMENTATION_SUPPORTED: - apdu_len = - encode_application_enumerated(&apdu[0], - Device_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()); @@ -450,14 +424,12 @@ int Device_Read_Property_Local( /* FIXME: encode the list here, if it exists */ break; case PROP_DATABASE_REVISION: - apdu_len = - encode_application_unsigned(&apdu[0], - Device_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()); + encode_application_unsigned(&apdu[0], dlmstp_max_info_frames()); break; case PROP_MAX_MASTER: apdu_len = @@ -474,14 +446,14 @@ int Device_Read_Property_Local( case PROP_UTC_OFFSET: /* Note: BACnet Time Zone is offset of local time and UTC, rather than offset of GMT. It is expressed in minutes */ - apdu_len = encode_application_signed(&apdu[0], 5 * 60 /* EST */ ); + apdu_len = encode_application_signed(&apdu[0], 5 * 60 /* 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 */ + 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: @@ -509,8 +481,7 @@ int Device_Read_Property_Local( return apdu_len; } -int Device_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Device_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { int apdu_len = BACNET_STATUS_ERROR; @@ -550,10 +521,9 @@ int Device_Read_Property( return apdu_len; } -bool Device_Write_Property_Local( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Device_Write_Property_Local(BACNET_WRITE_PROPERTY_DATA *wp_data) { - bool status = false; /* return value */ + bool status = false; /* return value */ int len = 0; BACNET_APPLICATION_DATA_VALUE value; @@ -563,9 +533,8 @@ bool Device_Write_Property_Local( return false; } /* decode the some of the request */ - len = - bacapp_decode_application_data(wp_data->application_data, - wp_data->application_data_len, &value); + len = bacapp_decode_application_data( + wp_data->application_data, wp_data->application_data_len, &value); /* FIXME: len < application_data_len: more data? */ if (len < 0) { /* error while decoding - a value larger than we can handle */ @@ -584,8 +553,8 @@ bool Device_Write_Property_Local( 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))) { + (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 { @@ -638,10 +607,11 @@ bool Device_Write_Property_Local( 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. */ + 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 { wp_data->error_class = ERROR_CLASS_PROPERTY; wp_data->error_code = @@ -707,10 +677,9 @@ bool Device_Write_Property_Local( return status; } -bool Device_Write_Property( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data) { - bool status = false; /* Ever the pessamist! */ + bool status = false; /* Ever the pessamist! */ struct object_functions *pObject = NULL; /* initialize the default return values */ diff --git a/ports/pic18f97j60/dlmstp.c b/ports/pic18f97j60/dlmstp.c index 1bf15ffd..3ef3ac1d 100644 --- a/ports/pic18f97j60/dlmstp.c +++ b/ports/pic18f97j60/dlmstp.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2006 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2006 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include #include @@ -48,16 +48,18 @@ static DLMSTP_PACKET Receive_Buffer; volatile struct mstp_port_struct_t MSTP_Port; #pragma udata -#define INCREMENT_AND_LIMIT_UINT16(x) {if (x < 0xFFFF) x++;} +#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); @@ -65,8 +67,7 @@ void dlmstp_reinit( dlmstp_set_max_master(DEFAULT_MAX_MASTER); } -void dlmstp_init( - void) +void dlmstp_init(void) { uint8_t data; @@ -79,24 +80,22 @@ void dlmstp_init( MSTP_Init(&MSTP_Port); } -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 */ - BACNET_NPDU_DATA * npdu_data, /* network information */ - uint8_t * pdu, /* any data to be sent - may be null */ +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 */ +{ /* number of bytes of data */ int bytes_sent = 0; unsigned npdu_len = 0; uint8_t frame_type = 0; BACNET_ADDRESS src; - unsigned i = 0; /* loop counter */ + unsigned i = 0; /* loop counter */ if (MSTP_Port.TxReady == false) { if (npdu_data->data_expecting_reply) @@ -115,8 +114,7 @@ int dlmstp_send_pdu( if ((MAX_HEADER + pdu_len) > MAX_MPDU) { return -4; } - bytes_sent = - MSTP_Create_Frame((uint8_t *) & MSTP_Port.TxBuffer[0], + bytes_sent = MSTP_Create_Frame((uint8_t *)&MSTP_Port.TxBuffer[0], sizeof(MSTP_Port.TxBuffer), MSTP_Port.TxFrameType, MSTP_Port.TxDestination, MSTP_Port.This_Station, pdu, pdu_len); MSTP_Port.TxLength = bytes_sent; @@ -127,8 +125,7 @@ int dlmstp_send_pdu( return bytes_sent; } -void dlmstp_task( - void) +void dlmstp_task(void) { bool bytes_remaining; bool received_frame; @@ -139,8 +136,8 @@ void dlmstp_task( do { bytes_remaining = RS485_Check_UART_Data(&MSTP_Port); MSTP_Receive_Frame_FSM(&MSTP_Port); - received_frame = MSTP_Port.ReceivedValidFrame || - MSTP_Port.ReceivedInvalidFrame; + received_frame = + MSTP_Port.ReceivedValidFrame || MSTP_Port.ReceivedInvalidFrame; if (received_frame) break; } while (bytes_remaining); @@ -167,9 +164,7 @@ void dlmstp_task( 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; @@ -193,19 +188,17 @@ void dlmstp_fill_bacnet_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) { @@ -222,8 +215,7 @@ void dlmstp_set_my_address( return; } -uint8_t dlmstp_my_address( - void) +uint8_t dlmstp_my_address(void) { return MSTP_Port.This_Station; } @@ -235,8 +227,7 @@ uint8_t dlmstp_my_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( - 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; @@ -250,8 +241,7 @@ void dlmstp_set_max_info_frames( return; } -unsigned dlmstp_max_info_frames( - void) +unsigned dlmstp_max_info_frames(void) { return MSTP_Port.Nmax_info_frames; } @@ -261,8 +251,7 @@ unsigned dlmstp_max_info_frames( /* 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) { @@ -278,20 +267,18 @@ void dlmstp_set_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; - my_address->net = 0; /* local only, no routing */ + my_address->net = 0; /* local only, no routing */ my_address->len = 0; for (i = 0; i < MAX_MAC_LEN; i++) { my_address->adr[i] = 0; @@ -300,16 +287,15 @@ void dlmstp_get_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/ports/pic18f97j60/isr.c b/ports/pic18f97j60/isr.c index 23554963..7a97202c 100644 --- a/ports/pic18f97j60/isr.c +++ b/ports/pic18f97j60/isr.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2007 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2007 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include "stdint.h" #include "hardware.h" #include "rs485.h" @@ -30,42 +30,32 @@ /* 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} + _asm goto InterruptHandlerHigh _endasm +} #pragma code #pragma code InterruptVectorLow = 0x18 -void InterruptVectorLow( - void) +void InterruptVectorLow(void) { /* jump to interrupt routine */ -_asm goto InterruptHandlerLow _endasm} + _asm goto InterruptHandlerLow _endasm +} #pragma code #pragma interrupt InterruptHandlerHigh -void InterruptHandlerHigh( - void) +void InterruptHandlerHigh(void) { #if 0 /* check for USART Rx int */ @@ -92,11 +82,10 @@ void InterruptHandlerHigh( } } -#pragma interruptlow InterruptHandlerLow save = PROD, section(".tmpdata"), TABLAT, TBLPTR, section \ - ("MATH_DATA") +#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)) { @@ -133,74 +122,69 @@ void InterruptHandlerLow( RS485_Interrupt_Rx(); } -/* Unused Interrupts - //check for timer1 int - if ((PIR1bits.TMR1IF) && (PIE1bits.TMR1IE)) - { - PIR1bits.TMR1IF = 0; - Interrupt_Timer1(); - } + /* Unused Interrupts + //check for timer1 int + if ((PIR1bits.TMR1IF) && (PIE1bits.TMR1IE)) + { + PIR1bits.TMR1IF = 0; + Interrupt_Timer1(); + } - //check for compare int - if ((PIR1bits.CCP1IF) && (PIE1bits.CCP1IE)) - { - PIR1bits.CCP1IF = 0; - Interrupt_CCP1(); - } + //check for compare int + if ((PIR1bits.CCP1IF) && (PIE1bits.CCP1IE)) + { + PIR1bits.CCP1IF = 0; + Interrupt_CCP1(); + } - //check for compare int - if ((PIR3bits.CCP3IF) && (PIE3bits.CCP3IE)) - { - PIR3bits.CCP3IF = 0; - Interrupt_CCP3(); - } + //check for compare int + if ((PIR3bits.CCP3IF) && (PIE3bits.CCP3IE)) + { + PIR3bits.CCP3IF = 0; + Interrupt_CCP3(); + } - //check for compare int - if ((PIR3bits.CCP4IF) && (PIE3bits.CCP4IE)) - { - PIR3bits.CCP4IF = 0; + //check for compare int + if ((PIR3bits.CCP4IF) && (PIE3bits.CCP4IE)) + { + PIR3bits.CCP4IF = 0; - Interrupt_CCP4(); - } + Interrupt_CCP4(); + } - //check for AD int - if ((PIR1bits.ADIF) && (PIE1bits.ADIE)) - { - PIR1bits.ADIF = 0; - Interrupt_ADC(); - } + //check for AD int + if ((PIR1bits.ADIF) && (PIE1bits.ADIE)) + { + PIR1bits.ADIF = 0; + Interrupt_ADC(); + } - //check for MSSP int - if ((PIR1bits.SSPIF) && (PIE1bits.SSPIE)) - { - PIR1bits.SSPIF = 0; - Interrupt_SSP(); - } + //check for MSSP int + if ((PIR1bits.SSPIF) && (PIE1bits.SSPIE)) + { + PIR1bits.SSPIF = 0; + Interrupt_SSP(); + } -*/ + */ } -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/ports/pic18f97j60/main.c b/ports/pic18f97j60/main.c index 41dce3a8..5881a1b8 100644 --- a/ports/pic18f97j60/main.c +++ b/ports/pic18f97j60/main.c @@ -1,31 +1,31 @@ /************************************************************************** -* -* Copyright (C) 2007 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2007 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include -#include /* for memmove */ +#include /* for memmove */ #include #include #include "stdint.h" @@ -58,8 +58,7 @@ volatile uint8_t Milliseconds = 0; volatile uint8_t Zero_Cross_Timeout = 0; -void Reinitialize( - void) +void Reinitialize(void) { uint8_t i; char name = 0; @@ -67,8 +66,7 @@ void Reinitialize( _asm reset _endasm return; } -void Global_Int( - enum INT_STATE state) +void Global_Int(enum INT_STATE state) { static uint8_t intstate = 0; @@ -91,8 +89,7 @@ void Global_Int( } } -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 @@ -169,8 +166,7 @@ void Hardware_Initialize( 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(); @@ -180,8 +176,7 @@ void Initialize_Variables( Milliseconds = 0; } -void MainTasks( - void) +void MainTasks(void) { static uint16_t millisecond_counter = 0; /* Handle our millisecond counters */ @@ -196,8 +191,7 @@ void MainTasks( } } -void main( - void) +void main(void) { RCONbits.NOT_POR = 1; RCONbits.NOT_RI = 1; diff --git a/ports/pic18f97j60/mstp.c b/ports/pic18f97j60/mstp.c index 83fa3e4e..87e590e0 100644 --- a/ports/pic18f97j60/mstp.c +++ b/ports/pic18f97j60/mstp.c @@ -150,25 +150,27 @@ #define Tusage_timeout 20 /* we need to be able to increment without rolling over */ -#define INCREMENT_AND_LIMIT_UINT8(x) {if (x < 0xFF) x++;} +#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 buffer_len, /* amount of space available */ +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 destination, /* destination address */ + 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) */ - uint8_t crc8 = 0xFF; /* used to calculate the crc value */ - uint16_t crc16 = 0xFFFF; /* used to calculate the crc value */ +{ /* 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 */ /* not enough to do a header */ @@ -209,47 +211,48 @@ unsigned MSTP_Create_Frame( 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 */ + 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 destination, /* destination address */ + 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) */ - uint8_t buffer[MAX_MPDU] = { 0 }; /* buffer for sending */ - uint16_t len = 0; /* number of bytes to send */ +{ /* 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 */ - len = (uint16_t) MSTP_Create_Frame(&buffer[0], /* where frame is loaded */ + 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 */ + 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) */ + 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; #endif #if PRINT_ENABLED_RECEIVE fprintf(stderr, - "MSTP Rx: State=%s Data=%02X hCRC=%02X Index=%u EC=%u DateLen=%u Silence=%u\n", + "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); #endif switch (mstp_port->receive_state) { - /* In the IDLE state, the node waits for the beginning of a frame. */ + /* In the IDLE state, the node waits for the beginning of a frame. + */ case MSTP_RECEIVE_STATE_IDLE: /* EatAnError */ if (mstp_port->ReceiveError == true) { @@ -283,7 +286,8 @@ void MSTP_Receive_Frame_FSM( } } break; - /* In the PREAMBLE state, the node waits for the second octet of the preamble. */ + /* 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) { @@ -330,11 +334,13 @@ void MSTP_Receive_Frame_FSM( } } break; - /* In the HEADER state, the node waits for the fixed message header. */ + /* 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 */ + /* 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; @@ -344,7 +350,8 @@ void MSTP_Receive_Frame_FSM( 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 */ + /* 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; @@ -356,9 +363,8 @@ void MSTP_Receive_Frame_FSM( 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->HeaderCRC = CRC_Calc_Header( + mstp_port->DataRegister, mstp_port->HeaderCRC); mstp_port->FrameType = mstp_port->DataRegister; mstp_port->DataAvailable = false; mstp_port->Index = 1; @@ -368,9 +374,8 @@ void MSTP_Receive_Frame_FSM( 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->HeaderCRC = CRC_Calc_Header( + mstp_port->DataRegister, mstp_port->HeaderCRC); mstp_port->DestinationAddress = mstp_port->DataRegister; mstp_port->DataAvailable = false; mstp_port->Index = 2; @@ -380,9 +385,8 @@ void MSTP_Receive_Frame_FSM( 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->HeaderCRC = CRC_Calc_Header( + mstp_port->DataRegister, mstp_port->HeaderCRC); mstp_port->SourceAddress = mstp_port->DataRegister; mstp_port->DataAvailable = false; mstp_port->Index = 3; @@ -392,9 +396,8 @@ void MSTP_Receive_Frame_FSM( 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->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; @@ -404,9 +407,8 @@ void MSTP_Receive_Frame_FSM( 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->HeaderCRC = CRC_Calc_Header( + mstp_port->DataRegister, mstp_port->HeaderCRC); mstp_port->DataLength += mstp_port->DataRegister; mstp_port->DataAvailable = false; mstp_port->Index = 5; @@ -416,21 +418,21 @@ void MSTP_Receive_Frame_FSM( 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->HeaderCRC = CRC_Calc_Header( + mstp_port->DataRegister, mstp_port->HeaderCRC); mstp_port->DataAvailable = false; /* don't wait for next state - do it here */ if (mstp_port->HeaderCRC != 0x55) { /* BadCRC */ - /* indicate that an error has occurred during the reception of a frame */ + /* 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_port->This_Station) || + (mstp_port->DestinationAddress == MSTP_BROADCAST_ADDRESS)) { /* FrameTooLong */ if (mstp_port->DataLength > MAX_MPDU) { @@ -443,7 +445,8 @@ void MSTP_Receive_Frame_FSM( } /* NoData */ else if (mstp_port->DataLength == 0) { - /* indicate that a frame with no data has been received */ + /* 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 = @@ -465,7 +468,6 @@ void MSTP_Receive_Frame_FSM( } } - } /* not per MS/TP standard, but it is a case not covered */ else { @@ -480,11 +482,13 @@ void MSTP_Receive_Frame_FSM( } } break; - /* In the DATA state, the node waits for the data portion of a frame. */ + /* 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 */ + /* 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; @@ -493,7 +497,8 @@ void MSTP_Receive_Frame_FSM( 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 */ + /* 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; @@ -503,9 +508,8 @@ void MSTP_Receive_Frame_FSM( #endif /* DataOctet */ if (mstp_port->Index < mstp_port->DataLength) { - mstp_port->DataCRC = - CRC_Calc_Data(mstp_port->DataRegister, - mstp_port->DataCRC); + mstp_port->DataCRC = CRC_Calc_Data( + mstp_port->DataRegister, mstp_port->DataCRC); mstp_port->InputBuffer[mstp_port->Index] = mstp_port->DataRegister; mstp_port->Index++; @@ -513,17 +517,15 @@ void MSTP_Receive_Frame_FSM( } /* CRC1 */ else if (mstp_port->Index == mstp_port->DataLength) { - mstp_port->DataCRC = - CRC_Calc_Data(mstp_port->DataRegister, - mstp_port->DataCRC); + 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); + 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) @@ -553,11 +555,10 @@ void MSTP_Receive_Frame_FSM( return; } -static bool mstp_compare_data_expecting_reply( - uint8_t * request_pdu, +static bool mstp_compare_data_expecting_reply(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) { @@ -578,9 +579,8 @@ static bool mstp_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], NULL, &request.address, - &request.npdu_data); + offset = npdu_decode( + &request_pdu[0], NULL, &request.address, &request.npdu_data); if (request.npdu_data.network_layer_message) { return false; } @@ -597,8 +597,7 @@ static bool mstp_compare_data_expecting_reply( /* decode the reply data */ reply.address.mac[0] = dest_address; reply.address.mac_len = 1; - offset = - npdu_decode(&reply_pdu[0], &reply.address, NULL, &reply.npdu_data); + offset = npdu_decode(&reply_pdu[0], &reply.address, NULL, &reply.npdu_data); if (reply.npdu_data.network_layer_message) { return false; } @@ -647,7 +646,8 @@ static bool mstp_compare_data_expecting_reply( return false; } } - if (request.npdu_data.protocol_version != reply.npdu_data.protocol_version) { + if (request.npdu_data.protocol_version != + reply.npdu_data.protocol_version) { return false; } #if 0 @@ -665,8 +665,7 @@ static bool mstp_compare_data_expecting_reply( } /* 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; @@ -692,11 +691,12 @@ bool MSTP_Master_Node_FSM( 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, + "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 @@ -719,7 +719,7 @@ bool MSTP_Master_Node_FSM( /* LostToken */ if (mstp_port->SilenceTimer >= Tno_token) { /* assume that the token has been lost */ - mstp_port->EventCount = 0; /* Addendum 135-2004d-8 */ + mstp_port->EventCount = 0; /* Addendum 135-2004d-8 */ mstp_port->master_state = MSTP_MASTER_STATE_NO_TOKEN; /* set the receive frame flags to false in case we received some bytes and had a timeout for some reason */ @@ -735,16 +735,17 @@ bool MSTP_Master_Node_FSM( } 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: 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)) { + if ((mstp_port->DestinationAddress == + mstp_port->This_Station) || + (mstp_port->DestinationAddress == MSTP_BROADCAST_ADDRESS)) { switch (mstp_port->FrameType) { /* ReceivedToken */ case FRAME_TYPE_TOKEN: @@ -771,16 +772,18 @@ bool MSTP_Master_Node_FSM( } break; case FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY: - /* indicate successful reception to the higher layers */ + /* indicate successful reception to the higher + * layers */ dlmstp_put_receive(mstp_port->SourceAddress, - (uint8_t *) & mstp_port->InputBuffer[0], + (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 */ + /* indicate successful reception to the higher + * layers */ dlmstp_put_receive(mstp_port->SourceAddress, - (uint8_t *) & mstp_port->InputBuffer[0], + (uint8_t *)&mstp_port->InputBuffer[0], mstp_port->DataLength); /* broadcast DER just remains IDLE */ if (mstp_port->DestinationAddress != @@ -794,7 +797,7 @@ bool MSTP_Master_Node_FSM( FRAME_TYPE_TEST_RESPONSE, mstp_port->SourceAddress, mstp_port->This_Station, - (uint8_t *) & mstp_port->InputBuffer[0], + (uint8_t *)&mstp_port->InputBuffer[0], mstp_port->DataLength); break; case FRAME_TYPE_TEST_RESPONSE: @@ -816,8 +819,8 @@ bool MSTP_Master_Node_FSM( transition_now = true; } else { uint8_t destination = mstp_port->TxBuffer[3]; - RS485_Send_Frame(mstp_port, - (uint8_t *) & mstp_port->TxBuffer[0], mstp_port->TxLength); + 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: @@ -853,8 +856,10 @@ bool MSTP_Master_Node_FSM( 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 */ + /* 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 { @@ -862,8 +867,7 @@ bool MSTP_Master_Node_FSM( /* InvalidFrame */ /* error in frame reception */ mstp_port->ReceivedInvalidFrame = false; - mstp_port->master_state = - MSTP_MASTER_STATE_DONE_WITH_TOKEN; + mstp_port->master_state = MSTP_MASTER_STATE_DONE_WITH_TOKEN; transition_now = true; } else if (mstp_port->ReceivedValidFrame == true) { if (mstp_port->DestinationAddress == @@ -880,10 +884,14 @@ bool MSTP_Master_Node_FSM( 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], + /* 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; @@ -898,7 +906,7 @@ bool MSTP_Master_Node_FSM( } else { /* ReceivedUnexpectedFrame */ /* an unexpected frame was received */ - /* This may indicate the presence of multiple tokens. */ + /* 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; @@ -935,7 +943,8 @@ bool MSTP_Master_Node_FSM( (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). */ + /* 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; @@ -943,9 +952,12 @@ bool MSTP_Master_Node_FSM( } 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. */ + /* 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, @@ -965,11 +977,12 @@ bool MSTP_Master_Node_FSM( /* 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 */ + 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; + mstp_port->master_state = MSTP_MASTER_STATE_POLL_FOR_MASTER; } else { /* ResetMaintenancePFM */ mstp_port->Poll_Station = mstp_port->This_Station; @@ -978,7 +991,8 @@ bool MSTP_Master_Node_FSM( 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->TokenCount = + 1; /* changed in Errata SSPC-135-2004 */ mstp_port->EventCount = 0; mstp_port->master_state = MSTP_MASTER_STATE_PASS_TOKEN; } @@ -998,7 +1012,8 @@ bool MSTP_Master_Node_FSM( 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. */ + /* 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; @@ -1027,16 +1042,18 @@ bool MSTP_Master_Node_FSM( 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 */ + /* 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; + 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 */ + /* 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); @@ -1044,7 +1061,8 @@ bool MSTP_Master_Node_FSM( 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. */ + /* Enter the IDLE state to receive and process the incoming + * frame. */ mstp_port->master_state = MSTP_MASTER_STATE_IDLE; transition_now = true; } @@ -1064,10 +1082,11 @@ bool MSTP_Master_Node_FSM( 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; + /* 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; @@ -1076,8 +1095,9 @@ bool MSTP_Master_Node_FSM( /* 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 == + if ((mstp_port->DestinationAddress == + mstp_port->This_Station) && + (mstp_port->FrameType == FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER)) { /* ReceivedReplyToPFM */ mstp_port->SoleMaster = false; @@ -1136,7 +1156,8 @@ bool MSTP_Master_Node_FSM( /* Re-enter the current state. */ } else { /* DeclareSoleMaster */ - /* to indicate that this station is the only master */ + /* to indicate that this station is the only master + */ mstp_port->SoleMaster = true; mstp_port->FrameCount = 0; mstp_port->master_state = @@ -1156,11 +1177,10 @@ bool MSTP_Master_Node_FSM( /* Compare the APDU type received and see if the message is that same APDU type along with the matching src/dest and invoke ID */ - matched = - mstp_compare_data_expecting_reply(&mstp_port->InputBuffer - [0], mstp_port->DataLength, mstp_port->SourceAddress, - &mstp_port->TxBuffer[8], mstp_port->TxLength, - mstp_port->TxDestination); + matched = mstp_compare_data_expecting_reply( + &mstp_port->InputBuffer[0], mstp_port->DataLength, + mstp_port->SourceAddress, &mstp_port->TxBuffer[8], + mstp_port->TxLength, mstp_port->TxDestination); } if (matched && mstp_port->TxReady) { /* Reply */ @@ -1168,10 +1188,11 @@ bool MSTP_Master_Node_FSM( /* 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 */ + /* 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); + 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 if (mstp_port->SilenceTimer > Treply_delay) { @@ -1201,10 +1222,9 @@ bool MSTP_Master_Node_FSM( /* 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; @@ -1231,7 +1251,7 @@ void MSTP_Init( mstp_port->ReceivedValidFrame = false; mstp_port->RetryCount = 0; mstp_port->SilenceTimer = 0; -/* mstp_port->ReplyPostponedTimer = 0; */ + /* mstp_port->ReplyPostponedTimer = 0; */ mstp_port->SoleMaster = false; mstp_port->SourceAddress = 0; mstp_port->TokenCount = 0; @@ -1241,7 +1261,8 @@ void MSTP_Init( mstp_port->Nmax_master = DEFAULT_MAX_MASTER; #endif - /* An array of octets, used to store PDU octets prior to being transmitted. */ + /* An array of octets, used to store PDU octets prior to being + * transmitted. */ /* This array is only used for APDU messages */ for (i = 0; i < sizeof(mstp_port->TxBuffer); i++) { mstp_port->TxBuffer[i] = 0; @@ -1249,6 +1270,5 @@ void MSTP_Init( mstp_port->TxLength = 0; mstp_port->TxReady = false; mstp_port->TxFrameType = 0; - } } diff --git a/ports/pic18f97j60/rs485.c b/ports/pic18f97j60/rs485.c index 86fa51b7..ca9678a0 100644 --- a/ports/pic18f97j60/rs485.c +++ b/ports/pic18f97j60/rs485.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2005 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2005 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* The module handles sending data out the RS-485 port */ /* and handles receiving data from the RS-485 port. */ @@ -52,17 +52,17 @@ volatile uint8_t RS485_Tx_Buffer[128]; #pragma udata /**************************************************************************** -* DESCRIPTION: Transmits a frame using the UART -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ + * DESCRIPTION: Transmits a frame using the UART + * RETURN: none + * 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) */ + 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) @@ -102,13 +102,12 @@ void RS485_Send_Frame( } /**************************************************************************** -* DESCRIPTION: Checks for data on the receive UART, and handles errors -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -bool RS485_Check_UART_Data( - volatile struct mstp_port_struct_t * mstp_port) + * DESCRIPTION: Checks for data on the receive UART, and handles errors + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +bool RS485_Check_UART_Data(volatile struct mstp_port_struct_t *mstp_port) { /* check for data */ if (!FIFO_Empty(&FIFO_Rx)) { @@ -128,8 +127,7 @@ bool RS485_Check_UART_Data( NOTES: none *************************************************************************** */ -void RS485_Interrupt_Rx( - void) +void RS485_Interrupt_Rx(void) { uint8_t data_byte; @@ -154,14 +152,14 @@ void RS485_Interrupt_Rx( NOTES: none *************************************************************************** */ -void RS485_Interrupt_Tx( - void) +void RS485_Interrupt_Tx(void) { if (!FIFO_Empty(&FIFO_Tx)) { TXREG2 = FIFO_Get(&FIFO_Tx); } else { /* wait for the USART to be empty */ - while (!TXSTA2bits.TRMT); + while (!TXSTA2bits.TRMT) + ; /* disable this interrupt */ PIE3bits.TX2IE = 0; /* enable the receiver */ @@ -174,25 +172,23 @@ void RS485_Interrupt_Tx( } /**************************************************************************** -* DESCRIPTION: Returns the baud rate that we are currently running at -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -uint32_t RS485_Get_Baud_Rate( - void) + * DESCRIPTION: Returns the baud rate that we are currently running at + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +uint32_t RS485_Get_Baud_Rate(void) { return RS485_Baud_Rate; } /**************************************************************************** -* DESCRIPTION: Sets the baud rate for the chip USART -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -bool RS485_Set_Baud_Rate( - uint32_t baud) + * DESCRIPTION: Sets the baud rate for the chip USART + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +bool RS485_Set_Baud_Rate(uint32_t baud) { bool valid = true; @@ -223,16 +219,14 @@ bool RS485_Set_Baud_Rate( } /**************************************************************************** -* DESCRIPTION: Initializes the RS485 hardware and variables, and starts in -* receive mode. -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -void RS485_Initialize_Port( - void) + * DESCRIPTION: Initializes the RS485 hardware and variables, and starts in + * receive mode. + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +void RS485_Initialize_Port(void) { - /* Reset USART registers to POR state */ TXSTA2 = 0; RCSTA2 = 0; @@ -304,39 +298,36 @@ void RS485_Initialize_Port( } /**************************************************************************** -* DESCRIPTION: Disables the RS485 hardware -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -void RS485_Disable_Port( - void) + * DESCRIPTION: Disables the RS485 hardware + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +void RS485_Disable_Port(void) { - RCSTA2 &= 0x4F; /* Disable the receiver */ - TXSTA2bits.TXEN = 0; /* and transmitter */ - PIE3 &= 0xCF; /* Disable both interrupts */ + RCSTA2 &= 0x4F; /* Disable the receiver */ + TXSTA2bits.TXEN = 0; /* and transmitter */ + PIE3 &= 0xCF; /* Disable both interrupts */ } /**************************************************************************** -* DESCRIPTION: Reinitializes the port -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -void RS485_Reinit( - void) + * DESCRIPTION: Reinitializes the port + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +void RS485_Reinit(void) { RS485_Set_Baud_Rate(38400); } /**************************************************************************** -* DESCRIPTION: Initializes the data and the port -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -void RS485_Initialize( - void) + * DESCRIPTION: Initializes the data and the port + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +void RS485_Initialize(void) { /* Init the Rs485 buffers */ FIFO_Init(&FIFO_Rx, RS485_Rx_Buffer, sizeof(RS485_Rx_Buffer)); diff --git a/ports/rx62n/bacnet.c b/ports/rx62n/bacnet.c index cbfad060..b5bd59c0 100644 --- a/ports/rx62n/bacnet.c +++ b/ports/rx62n/bacnet.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2011 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2011 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include /* hardware layer includes */ @@ -45,40 +45,37 @@ static struct mstimer DCC_Timer; #define DCC_CYCLE_SECONDS 1 -void bacnet_init( - void) +void bacnet_init(void) { /* initialize objects */ Device_Init(NULL); /* set up our confirmed service unrecognized service handler - required! */ - apdu_set_unrecognized_service_handler_handler - (handler_unrecognized_service); + apdu_set_unrecognized_service_handler_handler(handler_unrecognized_service); /* 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); /* 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, - handler_read_property); - apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROP_MULTIPLE, - handler_read_property_multiple); - apdu_set_confirmed_handler(SERVICE_CONFIRMED_REINITIALIZE_DEVICE, - handler_reinitialize_device); - apdu_set_confirmed_handler(SERVICE_CONFIRMED_WRITE_PROPERTY, - handler_write_property); + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_READ_PROPERTY, handler_read_property); + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_READ_PROP_MULTIPLE, handler_read_property_multiple); + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_REINITIALIZE_DEVICE, handler_reinitialize_device); + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_WRITE_PROPERTY, handler_write_property); /* handle communication so we can shutup when asked */ apdu_set_confirmed_handler(SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL, handler_device_communication_control); /* start the cyclic 1 second timer for DCC */ - mstimer_set(&DCC_Timer, DCC_CYCLE_SECONDS*1000); + mstimer_set(&DCC_Timer, DCC_CYCLE_SECONDS * 1000); /* Hello World! */ Send_I_Am(&Handler_Transmit_Buffer[0]); } static uint8_t PDUBuffer[MAX_MPDU]; -void bacnet_task( - void) +void bacnet_task(void) { uint16_t pdu_len; BACNET_ADDRESS src; /* source address */ diff --git a/ports/rx62n/bo.c b/ports/rx62n/bo.c index 02b656e5..d9327f73 100644 --- a/ports/rx62n/bo.c +++ b/ports/rx62n/bo.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2011 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2011 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* Binary Output Objects - customize for your use */ @@ -31,7 +31,7 @@ #include "bacnet/bacdef.h" #include "bacnet/bacdcode.h" #include "bacnet/bacenum.h" -#include "bacnet/config.h" /* the custom stuff */ +#include "bacnet/config.h" /* the custom stuff */ #include "bacnet/wp.h" #include "hardware.h" #include "led.h" @@ -54,34 +54,18 @@ static uint8_t Out_Of_Service[MAX_BINARY_OUTPUTS]; static uint8_t Polarity[MAX_BINARY_OUTPUTS]; /* These three arrays are used by the ReadPropertyMultiple handler */ -static const int Binary_Output_Properties_Required[] = { - PROP_OBJECT_IDENTIFIER, - PROP_OBJECT_NAME, - PROP_OBJECT_TYPE, - PROP_PRESENT_VALUE, - PROP_STATUS_FLAGS, - PROP_EVENT_STATE, - PROP_OUT_OF_SERVICE, - PROP_POLARITY, - PROP_PRIORITY_ARRAY, - PROP_RELINQUISH_DEFAULT, - -1 -}; +static const int Binary_Output_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, + PROP_OBJECT_NAME, PROP_OBJECT_TYPE, PROP_PRESENT_VALUE, PROP_STATUS_FLAGS, + PROP_EVENT_STATE, PROP_OUT_OF_SERVICE, PROP_POLARITY, PROP_PRIORITY_ARRAY, + PROP_RELINQUISH_DEFAULT, -1 }; -static const int Binary_Output_Properties_Optional[] = { - PROP_ACTIVE_TEXT, - PROP_INACTIVE_TEXT, - -1 -}; +static const int Binary_Output_Properties_Optional[] = { PROP_ACTIVE_TEXT, + PROP_INACTIVE_TEXT, -1 }; -static const int Binary_Output_Properties_Proprietary[] = { - -1 -}; +static const int Binary_Output_Properties_Proprietary[] = { -1 }; void Binary_Output_Property_Lists( - const int **pRequired, - const int **pOptional, - const int **pProprietary) + const int **pRequired, const int **pOptional, const int **pProprietary) { if (pRequired) *pRequired = Binary_Output_Properties_Required; @@ -94,8 +78,7 @@ void Binary_Output_Property_Lists( } /* we simply have 0-n object instances. */ -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; @@ -104,22 +87,19 @@ bool Binary_Output_Valid_Instance( } /* we simply have 0-n object instances. */ -unsigned Binary_Output_Count( - void) +unsigned Binary_Output_Count(void) { return MAX_BINARY_OUTPUTS; } /* we simply have 0-n object instances. */ -uint32_t Binary_Output_Index_To_Instance( - unsigned index) +uint32_t Binary_Output_Index_To_Instance(unsigned index) { return index; } /* we simply have 0-n object instances. */ -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; @@ -129,8 +109,7 @@ unsigned Binary_Output_Instance_To_Index( return index; } -static BACNET_BINARY_PV Present_Value( - unsigned int index) +static BACNET_BINARY_PV Present_Value(unsigned int index) { BACNET_BINARY_PV value = RELINQUISH_DEFAULT; BACNET_BINARY_PV current_value = RELINQUISH_DEFAULT; @@ -138,9 +117,9 @@ static BACNET_BINARY_PV Present_Value( if (index < MAX_BINARY_OUTPUTS) { for (i = 0; i < BACNET_MAX_PRIORITY; i++) { - current_value = (BACNET_BINARY_PV) Binary_Output_Level[index][i]; + current_value = (BACNET_BINARY_PV)Binary_Output_Level[index][i]; if (current_value != BINARY_NULL) { - value = (BACNET_BINARY_PV) Binary_Output_Level[index][i]; + value = (BACNET_BINARY_PV)Binary_Output_Level[index][i]; break; } } @@ -149,8 +128,7 @@ static BACNET_BINARY_PV Present_Value( return value; } -BACNET_BINARY_PV Binary_Output_Present_Value( - uint32_t object_instance) +BACNET_BINARY_PV Binary_Output_Present_Value(uint32_t object_instance) { unsigned index = 0; @@ -160,15 +138,13 @@ BACNET_BINARY_PV Binary_Output_Present_Value( } bool Binary_Output_Present_Value_Set( - uint32_t instance, - BACNET_BINARY_PV binary_value, - unsigned priority) -{ /* 0..15 */ + uint32_t instance, BACNET_BINARY_PV binary_value, unsigned priority) +{ /* 0..15 */ bool status = false; if (instance < MAX_BINARY_OUTPUTS) { if (priority < BACNET_MAX_PRIORITY) { - Binary_Output_Level[instance][priority] = (uint8_t) binary_value; + Binary_Output_Level[instance][priority] = (uint8_t)binary_value; status = true; } } @@ -177,8 +153,7 @@ bool Binary_Output_Present_Value_Set( } static void Binary_Output_Polarity_Set( - uint32_t instance, - BACNET_POLARITY polarity) + uint32_t instance, BACNET_POLARITY polarity) { if (instance < MAX_BINARY_OUTPUTS) { if (polarity < MAX_POLARITY) { @@ -187,29 +162,25 @@ static void Binary_Output_Polarity_Set( } } -BACNET_POLARITY Binary_Output_Polarity( - uint32_t instance) +BACNET_POLARITY Binary_Output_Polarity(uint32_t instance) { BACNET_POLARITY polarity = POLARITY_NORMAL; if (instance < MAX_BINARY_OUTPUTS) { - polarity = (BACNET_POLARITY) Polarity[instance]; + polarity = (BACNET_POLARITY)Polarity[instance]; } return polarity; } -static void Binary_Output_Out_Of_Service_Set( - uint32_t instance, - bool flag) +static void Binary_Output_Out_Of_Service_Set(uint32_t instance, bool flag) { if (instance < MAX_BINARY_OUTPUTS) { Out_Of_Service[instance] = flag; } } -bool Binary_Output_Out_Of_Service( - uint32_t instance) +bool Binary_Output_Out_Of_Service(uint32_t instance) { bool flag = false; @@ -221,10 +192,9 @@ bool Binary_Output_Out_Of_Service( } /* 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 */ + static char text_string[32]; /* okay for single thread */ if (object_instance < MAX_BINARY_OUTPUTS) { sprintf(text_string, "BO-%lu", object_instance); @@ -235,11 +205,10 @@ char *Binary_Output_Name( } /* return apdu len, or -1 on error */ -int Binary_Output_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Binary_Output_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { 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; @@ -254,7 +223,8 @@ int Binary_Output_Read_Property( } apdu = rpdata->application_data; switch (rpdata->object_property) { - /* object id, object name, object type are handled in Device object */ + /* object id, object name, object type are handled in Device object + */ case PROP_PRESENT_VALUE: present_value = Binary_Output_Present_Value(rpdata->object_instance); @@ -284,8 +254,7 @@ int Binary_Output_Read_Property( object_index = Binary_Output_Instance_To_Index(rpdata->object_instance); apdu_len = - encode_application_enumerated(&apdu[0], - Polarity[object_index]); + encode_application_enumerated(&apdu[0], Polarity[object_index]); break; case PROP_PRIORITY_ARRAY: /* Array element zero is the number of elements in the array */ @@ -299,14 +268,13 @@ int Binary_Output_Read_Property( Binary_Output_Instance_To_Index(rpdata->object_instance); for (i = 0; i < BACNET_MAX_PRIORITY; i++) { /* FIXME: check if we have room before adding it to APDU */ - present_value = (BACNET_BINARY_PV) - Binary_Output_Level[object_index][i]; + present_value = + (BACNET_BINARY_PV)Binary_Output_Level[object_index][i]; if (present_value == BINARY_NULL) { len = encode_application_null(&apdu[apdu_len]); } else { - len = - encode_application_enumerated(&apdu[apdu_len], - present_value); + len = encode_application_enumerated( + &apdu[apdu_len], present_value); } /* add it if we have room */ if ((apdu_len + len) < MAX_APDU) @@ -323,14 +291,13 @@ int Binary_Output_Read_Property( Binary_Output_Instance_To_Index(rpdata->object_instance); if (rpdata->array_index <= BACNET_MAX_PRIORITY) { present_value = (BACNET_BINARY_PV) - Binary_Output_Level[object_index][rpdata->array_index - - 1]; + Binary_Output_Level[object_index] + [rpdata->array_index - 1]; if (present_value == BINARY_NULL) { apdu_len = encode_application_null(&apdu[apdu_len]); } else { - apdu_len = - encode_application_enumerated(&apdu[apdu_len], - present_value); + apdu_len = encode_application_enumerated( + &apdu[apdu_len], present_value); } } else { rpdata->error_class = ERROR_CLASS_PROPERTY; @@ -371,19 +338,17 @@ int Binary_Output_Read_Property( } /* returns true if successful */ -bool Binary_Output_Write_Property( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Binary_Output_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data) { - bool status = false; /* return value */ + bool status = false; /* return value */ unsigned int priority = 0; BACNET_BINARY_PV level = BINARY_NULL; int len = 0; BACNET_APPLICATION_DATA_VALUE value; /* decode the some of the request */ - len = - bacapp_decode_application_data(wp_data->application_data, - wp_data->application_data_len, &value); + len = bacapp_decode_application_data( + wp_data->application_data, wp_data->application_data_len, &value); /* FIXME: len < application_data_len: more data? */ if (len < 0) { /* error while decoding - a value larger than we can handle */ @@ -402,19 +367,19 @@ bool Binary_Output_Write_Property( case PROP_PRESENT_VALUE: status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_ENUMERATED, - &wp_data->error_class, &wp_data->error_code); + &wp_data->error_class, &wp_data->error_code); if (status) { 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 */ ) && + (priority != 6 /* reserved */) && (value.type.Enumerated <= MAX_BINARY_PV)) { - level = (BACNET_BINARY_PV) value.type.Enumerated; + level = (BACNET_BINARY_PV)value.type.Enumerated; priority--; - Binary_Output_Present_Value_Set(wp_data->object_instance, - level, priority); + Binary_Output_Present_Value_Set( + wp_data->object_instance, level, priority); } 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 @@ -428,21 +393,20 @@ bool Binary_Output_Write_Property( wp_data->error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; } } else { - status = - WPValidateArgType(&value, BACNET_APPLICATION_TAG_NULL, + status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_NULL, &wp_data->error_class, &wp_data->error_code); if (status) { level = BINARY_NULL; priority = wp_data->priority; if (priority && (priority <= BACNET_MAX_PRIORITY)) { priority--; - Binary_Output_Present_Value_Set - (wp_data->object_instance, level, priority); + Binary_Output_Present_Value_Set( + wp_data->object_instance, level, priority); } else if (priority == 6) { status = false; - /* Command priority 6 is reserved for use by Minimum On/Off - algorithm and may not be used for other purposes in any - object. */ + /* Command priority 6 is reserved for use by Minimum + On/Off algorithm and may not be used for other + purposes in any object. */ wp_data->error_class = ERROR_CLASS_PROPERTY; wp_data->error_code = ERROR_CODE_WRITE_ACCESS_DENIED; } else { @@ -454,22 +418,21 @@ bool Binary_Output_Write_Property( } break; case PROP_OUT_OF_SERVICE: - status = - WPValidateArgType(&value, BACNET_APPLICATION_TAG_BOOLEAN, + status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_BOOLEAN, &wp_data->error_class, &wp_data->error_code); if (status) { - Binary_Output_Out_Of_Service_Set(wp_data->object_instance, - value.type.Boolean); + Binary_Output_Out_Of_Service_Set( + wp_data->object_instance, value.type.Boolean); } break; case PROP_POLARITY: status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_ENUMERATED, - &wp_data->error_class, &wp_data->error_code); + &wp_data->error_class, &wp_data->error_code); if (status) { if (value.type.Enumerated < MAX_POLARITY) { Binary_Output_Polarity_Set(wp_data->object_instance, - (BACNET_POLARITY) value.type.Enumerated); + (BACNET_POLARITY)value.type.Enumerated); } else { status = false; wp_data->error_class = ERROR_CLASS_PROPERTY; @@ -502,8 +465,7 @@ bool Binary_Output_Write_Property( return status; } -void Binary_Output_Init( - void) +void Binary_Output_Init(void) { unsigned i, j; diff --git a/ports/rx62n/device.c b/ports/rx62n/device.c index ff8f0e9f..9f19b4b5 100644 --- a/ports/rx62n/device.c +++ b/ports/rx62n/device.c @@ -1,43 +1,43 @@ /************************************************************************** -* -* Copyright (C) 2011 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2011 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include -#include /* for memmove */ +#include /* for memmove */ #include "bacnet/bacdef.h" #include "bacnet/bacdcode.h" #include "bacnet/bacstr.h" #include "bacnet/bacenum.h" #include "bacnet/bacapp.h" -#include "bacnet/config.h" /* the custom stuff */ +#include "bacnet/config.h" /* the custom stuff */ #include "bacnet/apdu.h" #include "bacnet/wp.h" /* WriteProperty handling */ #include "bacnet/rp.h" /* ReadProperty handling */ -#include "bacnet/dcc.h" /* DeviceCommunicationControl handling */ +#include "bacnet/dcc.h" /* DeviceCommunicationControl handling */ #include "bacnet/version.h" -#include "bacnet/basic/object/device.h" /* me */ +#include "bacnet/basic/object/device.h" /* me */ #include "bacnet/basic/services.h" #include "bacnet/datalink/datalink.h" #include "bacnet/basic/binding/address.h" @@ -48,10 +48,8 @@ #include "bacnet/basic/object/bo.h" /* forward prototype */ -int Device_Read_Property_Local( - BACNET_READ_PROPERTY_DATA * rpdata); -bool Device_Write_Property_Local( - BACNET_WRITE_PROPERTY_DATA * wp_data); +int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA *rpdata); +bool Device_Write_Property_Local(BACNET_WRITE_PROPERTY_DATA *wp_data); static struct my_object_functions { BACNET_OBJECT_TYPE Object_Type; @@ -63,19 +61,16 @@ static struct my_object_functions { read_property_function Object_Read_Property; write_property_function Object_Write_Property; rpm_property_lists_function Object_RPM_List; -} Object_Table[] = { - { - OBJECT_DEVICE, NULL, /* don't init - recursive! */ - Device_Count, Device_Index_To_Instance, - Device_Valid_Object_Instance_Number, Device_Name, - Device_Read_Property_Local, Device_Write_Property_Local, - Device_Property_Lists}, { - OBJECT_BINARY_OUTPUT, Binary_Output_Init, Binary_Output_Count, - Binary_Output_Index_To_Instance, Binary_Output_Valid_Instance, - Binary_Output_Name, Binary_Output_Read_Property, - Binary_Output_Write_Property, Binary_Output_Property_Lists}, { - MAX_BACNET_OBJECT_TYPE, NULL, NULL, NULL, NULL, NULL, NULL, NULL} -}; +} Object_Table[] = { { OBJECT_DEVICE, NULL, /* don't init - recursive! */ + Device_Count, Device_Index_To_Instance, + Device_Valid_Object_Instance_Number, Device_Name, + Device_Read_Property_Local, + Device_Write_Property_Local, Device_Property_Lists }, + { OBJECT_BINARY_OUTPUT, Binary_Output_Init, Binary_Output_Count, + Binary_Output_Index_To_Instance, Binary_Output_Valid_Instance, + Binary_Output_Name, Binary_Output_Read_Property, + Binary_Output_Write_Property, Binary_Output_Property_Lists }, + { MAX_BACNET_OBJECT_TYPE, NULL, NULL, NULL, NULL, NULL, NULL, NULL } }; /* note: you really only need to define variables for properties that are writable or that may change. @@ -94,40 +89,20 @@ static char Description[MAX_DEV_DESC_LEN + 1] = "Renesas Rulz!"; static uint32_t Database_Revision = 0; /* These three arrays are used by the ReadPropertyMultiple handler */ -static const int Device_Properties_Required[] = { - PROP_OBJECT_IDENTIFIER, - PROP_OBJECT_NAME, - PROP_OBJECT_TYPE, - PROP_SYSTEM_STATUS, - PROP_VENDOR_NAME, - PROP_VENDOR_IDENTIFIER, - PROP_MODEL_NAME, - PROP_FIRMWARE_REVISION, - PROP_APPLICATION_SOFTWARE_VERSION, - PROP_PROTOCOL_VERSION, - PROP_PROTOCOL_REVISION, - PROP_PROTOCOL_SERVICES_SUPPORTED, - PROP_PROTOCOL_OBJECT_TYPES_SUPPORTED, - PROP_OBJECT_LIST, - PROP_MAX_APDU_LENGTH_ACCEPTED, - PROP_SEGMENTATION_SUPPORTED, - PROP_APDU_TIMEOUT, - PROP_NUMBER_OF_APDU_RETRIES, - PROP_MAX_MASTER, - PROP_MAX_INFO_FRAMES, - PROP_DEVICE_ADDRESS_BINDING, - PROP_DATABASE_REVISION, - -1 -}; +static const int Device_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, + PROP_OBJECT_NAME, PROP_OBJECT_TYPE, PROP_SYSTEM_STATUS, PROP_VENDOR_NAME, + PROP_VENDOR_IDENTIFIER, PROP_MODEL_NAME, PROP_FIRMWARE_REVISION, + PROP_APPLICATION_SOFTWARE_VERSION, PROP_PROTOCOL_VERSION, + PROP_PROTOCOL_REVISION, PROP_PROTOCOL_SERVICES_SUPPORTED, + PROP_PROTOCOL_OBJECT_TYPES_SUPPORTED, PROP_OBJECT_LIST, + PROP_MAX_APDU_LENGTH_ACCEPTED, PROP_SEGMENTATION_SUPPORTED, + PROP_APDU_TIMEOUT, PROP_NUMBER_OF_APDU_RETRIES, PROP_MAX_MASTER, + PROP_MAX_INFO_FRAMES, PROP_DEVICE_ADDRESS_BINDING, PROP_DATABASE_REVISION, + -1 }; -static const int Device_Properties_Optional[] = { - PROP_DESCRIPTION, - -1 -}; +static const int Device_Properties_Optional[] = { PROP_DESCRIPTION, -1 }; -static const int Device_Properties_Proprietary[] = { - -1 -}; +static const int Device_Properties_Proprietary[] = { -1 }; static struct my_object_functions *Device_Objects_Find_Functions( BACNET_OBJECT_TYPE Object_Type) @@ -148,8 +123,7 @@ static struct my_object_functions *Device_Objects_Find_Functions( } static int Read_Property_Common( - struct my_object_functions *pObject, - BACNET_READ_PROPERTY_DATA * rpdata) + struct my_object_functions *pObject, BACNET_READ_PROPERTY_DATA *rpdata) { int apdu_len = BACNET_STATUS_ERROR; BACNET_CHARACTER_STRING char_string; @@ -168,9 +142,8 @@ static int Read_Property_Common( if (rpdata->object_type == OBJECT_DEVICE) { rpdata->object_instance = Object_Instance_Number; } - apdu_len = - encode_application_object_id(&apdu[0], rpdata->object_type, - rpdata->object_instance); + apdu_len = encode_application_object_id( + &apdu[0], rpdata->object_type, rpdata->object_instance); break; case PROP_OBJECT_NAME: if (pObject->Object_Name) { @@ -194,8 +167,7 @@ static int Read_Property_Common( return apdu_len; } -static unsigned property_list_count( - const int *pList) +static unsigned property_list_count(const int *pList) { unsigned property_count = 0; @@ -220,8 +192,7 @@ static unsigned property_list_count( * list, separately, the Required, Optional, and Proprietary object * properties with their counts. */ -void Device_Objects_Property_List( - BACNET_OBJECT_TYPE object_type, +void Device_Objects_Property_List(BACNET_OBJECT_TYPE object_type, uint32_t object_instance, struct special_property_list_t *pPropertyList) { @@ -244,25 +215,23 @@ void Device_Objects_Property_List( } /* Fetch the counts if available otherwise zero them */ - pPropertyList->Required.count = - pPropertyList->Required.pList == - NULL ? 0 : property_list_count(pPropertyList->Required.pList); + pPropertyList->Required.count = pPropertyList->Required.pList == NULL + ? 0 + : property_list_count(pPropertyList->Required.pList); - pPropertyList->Optional.count = - pPropertyList->Optional.pList == - NULL ? 0 : property_list_count(pPropertyList->Optional.pList); + pPropertyList->Optional.count = pPropertyList->Optional.pList == NULL + ? 0 + : property_list_count(pPropertyList->Optional.pList); - pPropertyList->Proprietary.count = - pPropertyList->Proprietary.pList == - NULL ? 0 : property_list_count(pPropertyList->Proprietary.pList); + pPropertyList->Proprietary.count = pPropertyList->Proprietary.pList == NULL + ? 0 + : property_list_count(pPropertyList->Proprietary.pList); return; } void Device_Property_Lists( - const int **pRequired, - const int **pOptional, - const int **pProprietary) + const int **pRequired, const int **pOptional, const int **pProprietary) { if (pRequired) *pRequired = Device_Properties_Required; @@ -274,21 +243,18 @@ void Device_Property_Lists( return; } -unsigned Device_Count( - void) +unsigned Device_Count(void) { return 1; } -uint32_t Device_Index_To_Instance( - unsigned index) +uint32_t Device_Index_To_Instance(unsigned index) { index = index; return Object_Instance_Number; } -bool Device_Reinitialize( - BACNET_REINITIALIZE_DEVICE_DATA * rd_data) +bool Device_Reinitialize(BACNET_REINITIALIZE_DEVICE_DATA *rd_data) { bool status = false; @@ -331,21 +297,18 @@ bool Device_Reinitialize( return status; } -BACNET_REINITIALIZED_STATE Device_Reinitialized_State( - void) +BACNET_REINITIALIZED_STATE Device_Reinitialized_State(void) { return Reinitialize_State; } /* 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 */ @@ -357,14 +320,12 @@ bool Device_Set_Object_Instance_Number( return status; } -bool Device_Valid_Object_Instance_Number( - uint32_t object_id) +bool Device_Valid_Object_Instance_Number(uint32_t object_id) { return (Object_Instance_Number == object_id); } -char *Device_Name( - uint32_t object_instance) +char *Device_Name(uint32_t object_instance) { if (object_instance == Object_Instance_Number) { return My_Object_Name; @@ -373,17 +334,14 @@ char *Device_Name( return NULL; } -const char *Device_Object_Name( - void) +const char *Device_Object_Name(void) { return My_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 */ + bool status = false; /*return value */ /* FIXME: All the object names in a device must be unique. Disallow setting the Device Object Name to any objects in @@ -399,15 +357,12 @@ bool Device_Set_Object_Name( return status; } -BACNET_DEVICE_STATUS Device_System_Status( - void) +BACNET_DEVICE_STATUS Device_System_Status(void) { return System_Status; } -int Device_Set_System_Status( - BACNET_DEVICE_STATUS status, - bool local) +int Device_Set_System_Status(BACNET_DEVICE_STATUS status, bool local) { /*return value - 0 = ok, -1 = bad value, -2 = not allowed */ int result = -1; @@ -420,17 +375,14 @@ int Device_Set_System_Status( return result; } -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 */ + bool status = false; /*return value */ if (length < sizeof(Description)) { memmove(Description, name, length); @@ -441,17 +393,14 @@ bool Device_Set_Description( 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 */ + bool status = false; /*return value */ if (length < sizeof(Location)) { memmove(Location, name, length); @@ -462,46 +411,39 @@ bool Device_Set_Location( 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_Vendor_Identifier( - void) +uint16_t Device_Vendor_Identifier(void) { return BACNET_VENDOR_ID; } -BACNET_SEGMENTATION Device_Segmentation_Supported( - void) +BACNET_SEGMENTATION Device_Segmentation_Supported(void) { return SEGMENTATION_NONE; } -uint32_t Device_Database_Revision( - void) +uint32_t Device_Database_Revision(void) { return Database_Revision; } -void Device_Inc_Database_Revision( - void) +void Device_Inc_Database_Revision(void) { Database_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 = 0; /* number of objects */ struct my_object_functions *pObject = NULL; @@ -519,9 +461,7 @@ unsigned Device_Object_List_Count( } bool Device_Object_List_Identifier( - uint32_t array_index, - BACNET_OBJECT_TYPE *object_type, - uint32_t * instance) + uint32_t array_index, BACNET_OBJECT_TYPE *object_type, uint32_t *instance) { bool status = false; uint32_t count = 0; @@ -557,13 +497,13 @@ bool Device_Object_List_Identifier( * and the lookup succeeds, they will be given the resulting values. * @param object_name [in] The desired Object Name to look for. * @param object_type [out] The BACNET_OBJECT_TYPE of the matching Object. - * @param object_instance [out] The object instance number of the matching Object. + * @param object_instance [out] The object instance number of the matching + * Object. * @return True on success or else False if not found. */ -bool Device_Valid_Object_Name( - const char *object_name, +bool Device_Valid_Object_Name(const char *object_name, BACNET_OBJECT_TYPE *object_type, - uint32_t * object_instance) + uint32_t *object_instance) { bool found = false; BACNET_OBJECT_TYPE type = OBJECT_NONE; @@ -595,13 +535,12 @@ bool Device_Valid_Object_Name( /* returns the name or NULL if not found */ char *Device_Valid_Object_Id( - BACNET_OBJECT_TYPE object_type, - uint32_t object_instance) + BACNET_OBJECT_TYPE object_type, uint32_t object_instance) { - char *name = NULL; /* return value */ + char *name = NULL; /* return value */ struct my_object_functions *pObject = NULL; - pObject = Device_Objects_Find_Functions((BACNET_OBJECT_TYPE) object_type); + pObject = Device_Objects_Find_Functions((BACNET_OBJECT_TYPE)object_type); if ((pObject) && (pObject->Object_Name)) { name = pObject->Object_Name(object_instance); } @@ -610,11 +549,10 @@ char *Device_Valid_Object_Id( } /* return the length of the apdu encoded or BACNET_STATUS_ERROR for error */ -int Device_Read_Property_Local( - BACNET_READ_PROPERTY_DATA * rpdata) +int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA *rpdata) { - 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; uint32_t i = 0; @@ -638,8 +576,7 @@ int Device_Read_Property_Local( break; case PROP_SYSTEM_STATUS: apdu_len = - encode_application_enumerated(&apdu[0], - Device_System_Status()); + encode_application_enumerated(&apdu[0], Device_System_Status()); break; case PROP_VENDOR_NAME: characterstring_init_ansi(&char_string, BACNET_VENDOR_NAME); @@ -660,8 +597,8 @@ int Device_Read_Property_Local( encode_application_character_string(&apdu[0], &char_string); break; case PROP_APPLICATION_SOFTWARE_VERSION: - characterstring_init_ansi(&char_string, - Application_Software_Version); + characterstring_init_ansi( + &char_string, Application_Software_Version); apdu_len = encode_application_character_string(&apdu[0], &char_string); break; @@ -676,16 +613,15 @@ int Device_Read_Property_Local( break; case PROP_PROTOCOL_REVISION: apdu_len = - encode_application_unsigned(&apdu[0], - BACNET_PROTOCOL_REVISION); + encode_application_unsigned(&apdu[0], BACNET_PROTOCOL_REVISION); 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)); + 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; @@ -696,7 +632,7 @@ int Device_Read_Property_Local( 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); + bitstring_set_bit(&bit_string, (uint8_t)i, false); } /* set the object types with objects to supported */ i = 0; @@ -720,11 +656,10 @@ int Device_Read_Property_Local( /* your maximum APDU size. */ else if (rpdata->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); + 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? */ @@ -744,13 +679,11 @@ int Device_Read_Property_Local( } } } else { - found = - Device_Object_List_Identifier(rpdata->array_index, - &object_type, &instance); + found = Device_Object_List_Identifier( + rpdata->array_index, &object_type, &instance); if (found) { - apdu_len = - encode_application_object_id(&apdu[0], object_type, - instance); + apdu_len = encode_application_object_id( + &apdu[0], object_type, instance); } else { rpdata->error_class = ERROR_CLASS_PROPERTY; rpdata->error_code = ERROR_CODE_INVALID_ARRAY_INDEX; @@ -762,9 +695,8 @@ int Device_Read_Property_Local( apdu_len = encode_application_unsigned(&apdu[0], MAX_APDU); break; case PROP_SEGMENTATION_SUPPORTED: - apdu_len = - encode_application_enumerated(&apdu[0], - Device_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()); @@ -776,8 +708,7 @@ int Device_Read_Property_Local( /* FIXME: encode the list here, if it exists */ break; case PROP_DATABASE_REVISION: - apdu_len = - encode_application_unsigned(&apdu[0], Database_Revision); + apdu_len = encode_application_unsigned(&apdu[0], Database_Revision); break; default: rpdata->error_class = ERROR_CLASS_PROPERTY; @@ -796,7 +727,8 @@ int Device_Read_Property_Local( return apdu_len; } -/** Looks up the requested Object and Property, and encodes its Value in an APDU. +/** Looks up the requested Object and Property, and encodes its Value in an + * APDU. * @ingroup ObjIntf * If the Object or Property can't be found, sets the error class and code. * @@ -804,8 +736,7 @@ int Device_Read_Property_Local( * on entry, and APDU message on return. * @return The length of the APDU on success, else BACNET_STATUS_ERROR */ -int Device_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Device_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { int apdu_len = BACNET_STATUS_ERROR; struct my_object_functions *pObject = NULL; @@ -831,18 +762,16 @@ int Device_Read_Property( } /* returns true if successful */ -bool Device_Write_Property_Local( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Device_Write_Property_Local(BACNET_WRITE_PROPERTY_DATA *wp_data) { - bool status = false; /* return value */ + bool status = false; /* return value */ int len = 0; BACNET_APPLICATION_DATA_VALUE value; int temp; /* decode the some of the request */ - len = - bacapp_decode_application_data(wp_data->application_data, - wp_data->application_data_len, &value); + len = bacapp_decode_application_data( + wp_data->application_data, wp_data->application_data_len, &value); /* FIXME: len < application_data_len: more data? */ if (len < 0) { /* error while decoding - a value larger than we can handle */ @@ -859,13 +788,12 @@ bool Device_Write_Property_Local( } switch (wp_data->object_property) { case PROP_OBJECT_IDENTIFIER: - status = - WPValidateArgType(&value, BACNET_APPLICATION_TAG_OBJECT_ID, + status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_OBJECT_ID, &wp_data->error_class, &wp_data->error_code); if (status) { if ((value.type.Object_Id.type == OBJECT_DEVICE) && - (Device_Set_Object_Instance_Number(value.type. - Object_Id.instance))) { + (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 { @@ -878,10 +806,10 @@ bool Device_Write_Property_Local( case PROP_SYSTEM_STATUS: status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_ENUMERATED, - &wp_data->error_class, &wp_data->error_code); + &wp_data->error_class, &wp_data->error_code); if (status) { - temp = Device_Set_System_Status((BACNET_DEVICE_STATUS) - value.type.Enumerated, false); + temp = Device_Set_System_Status( + (BACNET_DEVICE_STATUS)value.type.Enumerated, false); if (temp != 0) { status = false; wp_data->error_class = ERROR_CLASS_PROPERTY; @@ -895,33 +823,30 @@ bool Device_Write_Property_Local( } break; case PROP_OBJECT_NAME: - status = - WPValidateString(&value, MAX_DEV_NAME_LEN, false, + status = WPValidateString(&value, MAX_DEV_NAME_LEN, false, &wp_data->error_class, &wp_data->error_code); if (status) { - Device_Set_Object_Name(characterstring_value(&value. - type.Character_String), + Device_Set_Object_Name( + characterstring_value(&value.type.Character_String), characterstring_length(&value.type.Character_String)); } break; case PROP_LOCATION: - status = - WPValidateString(&value, MAX_DEV_LOC_LEN, true, + status = WPValidateString(&value, MAX_DEV_LOC_LEN, true, &wp_data->error_class, &wp_data->error_code); if (status) { - Device_Set_Location(characterstring_value(&value. - type.Character_String), + Device_Set_Location( + characterstring_value(&value.type.Character_String), characterstring_length(&value.type.Character_String)); } break; case PROP_DESCRIPTION: - status = - WPValidateString(&value, MAX_DEV_DESC_LEN, true, + status = WPValidateString(&value, MAX_DEV_DESC_LEN, true, &wp_data->error_class, &wp_data->error_code); if (status) { - Device_Set_Description(characterstring_value(&value. - type.Character_String), + Device_Set_Description( + characterstring_value(&value.type.Character_String), characterstring_length(&value.type.Character_String)); } break; @@ -964,8 +889,7 @@ bool Device_Write_Property_Local( * and new Value on entry, and APDU message on return. * @return True on success, else False if there is an error. */ -bool Device_Write_Property( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data) { bool status = false; struct my_object_functions *pObject = NULL; @@ -998,13 +922,12 @@ bool Device_Write_Property( /** Initialize the Device Object and each of its child Object instances. * @ingroup ObjIntf */ -void Device_Init( - object_functions_t * object_table) +void Device_Init(object_functions_t *object_table) { struct my_object_functions *pObject = NULL; /* not using the standard table - using our own */ - (void) object_table; + (void)object_table; pObject = &Object_Table[0]; while (pObject->Object_Type < MAX_BACNET_OBJECT_TYPE) { if (pObject->Object_Init) { diff --git a/ports/rx62n/ethernet.c b/ports/rx62n/ethernet.c index 8ec6f5ac..11e15d1f 100644 --- a/ports/rx62n/ethernet.c +++ b/ports/rx62n/ethernet.c @@ -1,29 +1,29 @@ /************************************************************************** -* -* Copyright (C) 2011 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ -#include /* for standard integer types uint8_t etc. */ -#include /* for the standard bool type. */ + * + * Copyright (C) 2011 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ +#include /* for standard integer types uint8_t etc. */ +#include /* for the standard bool type. */ #include "bacnet/bacdef.h" #include "bacnet/datalink/ethernet.h" #include "bacnet/bacint.h" @@ -33,8 +33,8 @@ for BACnet/Ethernet. */ /* commonly used comparison address for ethernet */ -static uint8_t Ethernet_Broadcast[MAX_MAC_LEN] = - { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; +static uint8_t Ethernet_Broadcast[MAX_MAC_LEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF }; /* IEEE maintains list of 48-bit MAC "addresses" AKA EUI-48 identifiers. An EUI-48 is structured into an initial 3-octet OUI @@ -43,14 +43,13 @@ static uint8_t Ethernet_Broadcast[MAX_MAC_LEN] = /* see [RFC5342] for current information and registration procedures. */ /* The OUI 00-00-5E has been allocated to IANA. */ /* my local device data - MAC address */ -static uint8_t Ethernet_MAC_Address[MAX_MAC_LEN] = - { 0x00, 0x00, 0x5E, 0x00, 0x00, 0x01 }; +static uint8_t Ethernet_MAC_Address[MAX_MAC_LEN] = { 0x00, 0x00, 0x5E, 0x00, + 0x00, 0x01 }; /* status of the link */ static int32_t Ethernet_Status = R_ETHER_ERROR; -bool ethernet_valid( - void) +bool ethernet_valid(void) { if (Ethernet_Status != R_ETHER_OK) { Ethernet_Status = R_Ether_Open(0, Ethernet_MAC_Address); @@ -59,8 +58,7 @@ bool ethernet_valid( return (Ethernet_Status != R_ETHER_ERROR); } -void ethernet_cleanup( - void) +void ethernet_cleanup(void) { R_Ether_Close(0); Ethernet_Status = R_ETHER_ERROR; @@ -68,8 +66,7 @@ void ethernet_cleanup( return; } -bool ethernet_init( - char *interface_name) +bool ethernet_init(char *interface_name) { interface_name = interface_name; Ethernet_Status = R_Ether_Open(0, Ethernet_MAC_Address); @@ -77,9 +74,7 @@ bool ethernet_init( return (Ethernet_Status == R_ETHER_OK); } -int ethernet_send( - uint8_t * mtu, - int mtu_len) +int ethernet_send(uint8_t *mtu, int mtu_len) { int bytes = 0; @@ -87,24 +82,22 @@ int ethernet_send( bytes = R_Ether_Write(0, mtu, mtu_len); return bytes; - } /* 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 */ - BACNET_NPDU_DATA * npdu_data, /* network information */ - uint8_t * pdu, /* any data to be sent - may be null */ +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 */ 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 */ + uint8_t mtu[MAX_MPDU] = { 0 }; /* our buffer */ int mtu_len = 0; - (void) npdu_data; + (void)npdu_data; /* load the BACnet address for NPDU data */ for (i = 0; i < 6; i++) { src.mac[i] = Ethernet_MAC_Address[i]; @@ -133,9 +126,9 @@ int ethernet_send_pdu( 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) { return -4; @@ -153,21 +146,20 @@ int ethernet_send_pdu( /* 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 */ + uint8_t buf[MAX_MPDU] = { 0 }; /* data */ + uint16_t pdu_len = 0; /* return value */ /* Make sure the socket is open */ if (!ethernet_valid()) return 0; - received_bytes = R_Ether_Read(0, (void *) buf); + received_bytes = R_Ether_Read(0, (void *)buf); if (received_bytes == 0) return 0; @@ -182,13 +174,13 @@ uint16_t ethernet_receive( /* check destination address for when */ /* the Ethernet card is in promiscious mode */ - if ((memcmp(&buf[0], Ethernet_MAC_Address, 6) != 0) - && (memcmp(&buf[0], Ethernet_Broadcast, 6) != 0)) { + if ((memcmp(&buf[0], Ethernet_MAC_Address, 6) != 0) && + (memcmp(&buf[0], Ethernet_Broadcast, 6) != 0)) { return 0; } - (void) decode_unsigned16(&buf[12], &pdu_len); - pdu_len -= 3 /* DSAP, SSAP, LLC Control */ ; + (void)decode_unsigned16(&buf[12], &pdu_len); + pdu_len -= 3 /* DSAP, SSAP, LLC Control */; /* copy the buffer into the PDU */ if (pdu_len < max_pdu) memmove(&pdu[0], &buf[17], pdu_len); @@ -196,12 +188,10 @@ uint16_t ethernet_receive( else pdu_len = 0; - return pdu_len; } -void ethernet_set_my_address( - BACNET_ADDRESS * my_address) +void ethernet_set_my_address(BACNET_ADDRESS *my_address) { int i = 0; @@ -212,8 +202,7 @@ void ethernet_set_my_address( return; } -void ethernet_get_my_address( - BACNET_ADDRESS * my_address) +void ethernet_get_my_address(BACNET_ADDRESS *my_address) { int i = 0; @@ -222,7 +211,7 @@ void ethernet_get_my_address( my_address->mac[i] = Ethernet_MAC_Address[i]; my_address->mac_len++; } - my_address->net = 0; /* DNET=0 is local only, no routing */ + my_address->net = 0; /* DNET=0 is local only, no routing */ my_address->len = 0; for (i = 0; i < MAX_MAC_LEN; i++) { my_address->adr[i] = 0; @@ -231,10 +220,9 @@ void ethernet_get_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++) { @@ -242,7 +230,7 @@ void ethernet_get_broadcast_address( } 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; } diff --git a/ports/rx62n/led.c b/ports/rx62n/led.c index 36804d02..ab032b12 100644 --- a/ports/rx62n/led.c +++ b/ports/rx62n/led.c @@ -1,26 +1,26 @@ /************************************************************************** -* -* Copyright (C) 2011 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*********************************************************************/ + * + * Copyright (C) 2011 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + *********************************************************************/ #include #include "hardware.h" #include "bacnet/basic/sys/mstimer.h" @@ -29,16 +29,14 @@ static struct mstimer Off_Delay_Timer[MAX_LEDS]; static bool LED_Status[MAX_LEDS]; -#define LED_OFF (0) - +#define LED_OFF (0) /************************************************************************* -* Description: Turn on an LED -* Returns: none -* Notes: none -*************************************************************************/ -void led_on( - uint8_t index) + * Description: Turn on an LED + * Returns: none + * Notes: none + *************************************************************************/ +void led_on(uint8_t index) { switch (index) { case 4: @@ -87,12 +85,11 @@ void led_on( } /************************************************************************* -* Description: Turn off an LED -* Returns: none -* Notes: none -*************************************************************************/ -void led_off( - uint8_t index) + * Description: Turn off an LED + * Returns: none + * Notes: none + *************************************************************************/ +void led_off(uint8_t index) { switch (index) { case 4: @@ -141,12 +138,11 @@ void led_off( } /************************************************************************* -* Description: Get the state of the LED -* Returns: true if on, false if off. -* Notes: none -*************************************************************************/ -bool led_state( - uint8_t index) + * Description: Get the state of the LED + * Returns: true if on, false if off. + * Notes: none + *************************************************************************/ +bool led_state(uint8_t index) { bool state = false; @@ -158,12 +154,11 @@ bool led_state( } /************************************************************************* -* Description: Toggle the state of the setup LED -* Returns: none -* Notes: none -*************************************************************************/ -void led_toggle( - uint8_t index) + * Description: Toggle the state of the setup LED + * Returns: none + * Notes: none + *************************************************************************/ +void led_toggle(uint8_t index) { if (led_state(index)) { led_off(index); @@ -173,13 +168,11 @@ void led_toggle( } /************************************************************************* -* Description: Delay before going off to give minimum brightness. -* Returns: none -* Notes: none -*************************************************************************/ -void led_off_delay( - uint8_t index, - uint32_t delay_ms) + * Description: Delay before going off to give minimum brightness. + * Returns: none + * Notes: none + *************************************************************************/ +void led_off_delay(uint8_t index, uint32_t delay_ms) { if (index < MAX_LEDS) { mstimer_set(&Off_Delay_Timer[index], delay_ms); @@ -187,13 +180,11 @@ void led_off_delay( } /************************************************************************* -* Description: Turn on, and delay before going off. -* Returns: none -* Notes: none -*************************************************************************/ -void led_on_interval( - uint8_t index, - uint16_t interval_ms) + * Description: Turn on, and delay before going off. + * Returns: none + * Notes: none + *************************************************************************/ +void led_on_interval(uint8_t index, uint16_t interval_ms) { if (index < MAX_LEDS) { led_on(index); @@ -202,14 +193,13 @@ void led_on_interval( } /************************************************************************* -* Description: Task for blinking LED -* Returns: none -* Notes: none -*************************************************************************/ -void led_task( - void) + * Description: Task for blinking LED + * Returns: none + * Notes: none + *************************************************************************/ +void led_task(void) { - uint8_t i; /* loop counter */ + uint8_t i; /* loop counter */ for (i = 0; i < MAX_LEDS; i++) { if (mstimer_interval(&Off_Delay_Timer[i]) > 0) { @@ -222,12 +212,11 @@ void led_task( } /************************************************************************* -* Description: Initialize the LED hardware -* Returns: none -* Notes: none -*************************************************************************/ -void led_init( - void) + * Description: Initialize the LED hardware + * Returns: none + * Notes: none + *************************************************************************/ +void led_init(void) { unsigned i = 0; diff --git a/ports/rx62n/main.c b/ports/rx62n/main.c index 8d3ba751..18949729 100644 --- a/ports/rx62n/main.c +++ b/ports/rx62n/main.c @@ -1,27 +1,27 @@ /************************************************************************ -* -* Copyright (C) 2011 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*************************************************************************/ + * + * Copyright (C) 2011 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *************************************************************************/ #include #include #include "hardware.h" @@ -30,8 +30,7 @@ #include "led.h" /** Main function of BACnet demo for RX62N evaluation board */ -int main( - void) +int main(void) { InitialiseLCD(); ClearLCD(); diff --git a/ports/rx62n/mstimer-init.c b/ports/rx62n/mstimer-init.c index 44a7d780..298c2661 100644 --- a/ports/rx62n/mstimer-init.c +++ b/ports/rx62n/mstimer-init.c @@ -1,26 +1,26 @@ /************************************************************************** -* -* Copyright (C) 2011 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*********************************************************************/ + * + * Copyright (C) 2011 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + *********************************************************************/ #include #include #include "hardware.h" @@ -29,40 +29,36 @@ /* counter for the the timer which wraps every 49.7 days */ static volatile uint32_t Millisecond_Counter; /* forward prototype for interrupt service routine */ -void int_cmt0_isr( - void); +void int_cmt0_isr(void); /************************************************************************* -* Description: Timer Interrupt Handler -* Returns: nothing -* Notes: none -*************************************************************************/ -static void timer_interrupt_handler( - void) + * Description: Timer Interrupt Handler + * Returns: nothing + * Notes: none + *************************************************************************/ +static void timer_interrupt_handler(void) { Millisecond_Counter++; } /************************************************************************* -* Description: Timer Interrupt Service Routine -* Returns: nothing -* Notes: none -*************************************************************************/ -void int_cmt0_isr( - void) + * Description: Timer Interrupt Service Routine + * Returns: nothing + * Notes: none + *************************************************************************/ +void int_cmt0_isr(void) { timer_interrupt_handler(); } /************************************************************************* -* Description: returns the current millisecond count -* Returns: none -* Notes: This method only disables the timer overflow interrupt. -*************************************************************************/ -unsigned long mstimer_now( - void) + * Description: returns the current millisecond count + * Returns: none + * Notes: This method only disables the timer overflow interrupt. + *************************************************************************/ +unsigned long mstimer_now(void) { - unsigned long timer_value; /* return value */ + unsigned long timer_value; /* return value */ timer_value = Millisecond_Counter; @@ -70,12 +66,11 @@ unsigned long mstimer_now( } /************************************************************************* -* Description: Initialization for Timer -* Returns: none -* Notes: none -*************************************************************************/ -void timer_init( - void) + * Description: Initialization for Timer + * Returns: none + * Notes: none + *************************************************************************/ +void timer_init(void) { /* Declare error flag */ bool err = true; @@ -85,5 +80,6 @@ void timer_init( err &= R_CMT_Create(3, PDL_CMT_PERIOD, 1E-3, int_cmt0_isr, 3); /* Halt in while loop when RPDL errors detected */ - while (!err); + while (!err) + ; } diff --git a/ports/stm32f10x/CMSIS/core_cm3.c b/ports/stm32f10x/CMSIS/core_cm3.c index fcff0d13..b0e3ba31 100644 --- a/ports/stm32f10x/CMSIS/core_cm3.c +++ b/ports/stm32f10x/CMSIS/core_cm3.c @@ -1,51 +1,54 @@ -/**************************************************************************//** - * @file core_cm3.c - * @brief CMSIS Cortex-M3 Core Peripheral Access Layer Source File - * @version V1.30 - * @date 30. October 2009 - * - * @note - * Copyright (C) 2009 ARM Limited. All rights reserved. - * - * @par - * ARM Limited (ARM) is supplying this software for use with Cortex-M - * processor based microcontrollers. This file can be freely distributed - * within development tools that are supporting such ARM based processors. - * - * @par - * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED - * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. - * ARM SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR - * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. - * - ******************************************************************************/ +/**************************************************************************/ /** + * @file core_cm3.c + * @brief CMSIS Cortex-M3 Core Peripheral Access Layer Source File + * @version V1.30 + * @date 30. October 2009 + * + * @note + * Copyright (C) 2009 ARM Limited. All rights reserved. + * + * @par + * ARM Limited (ARM) is supplying this software for use with Cortex-M + * processor based microcontrollers. This file can be freely distributed + * within development tools that are supporting such ARM based processors. + * + * @par + * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED + * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. + * ARM SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR + * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. + * + ******************************************************************************/ #include /* define compiler specific symbols */ -#if defined ( __CC_ARM ) - #define __ASM __asm /*!< asm keyword for ARM Compiler */ - #define __INLINE __inline /*!< inline keyword for ARM Compiler */ +#if defined(__CC_ARM) +#define __ASM __asm /*!< asm keyword for ARM Compiler */ +#define __INLINE __inline /*!< inline keyword for ARM Compiler */ -#elif defined ( __ICCARM__ ) - #define __ASM __asm /*!< asm keyword for IAR Compiler */ - #define __INLINE inline /*!< inline keyword for IAR Compiler. Only avaiable in High optimization mode! */ +#elif defined(__ICCARM__) +#define __ASM __asm /*!< asm keyword for IAR Compiler */ +#define __INLINE \ + inline /*!< inline keyword for IAR Compiler. Only avaiable in High \ + optimization mode! */ -#elif defined ( __GNUC__ ) - #define __ASM __asm /*!< asm keyword for GNU Compiler */ - #define __INLINE inline /*!< inline keyword for GNU Compiler */ +#elif defined(__GNUC__) +#define __ASM __asm /*!< asm keyword for GNU Compiler */ +#define __INLINE inline /*!< inline keyword for GNU Compiler */ -#elif defined ( __TASKING__ ) - #define __ASM __asm /*!< asm keyword for TASKING Compiler */ - #define __INLINE inline /*!< inline keyword for TASKING Compiler */ +#elif defined(__TASKING__) +#define __ASM __asm /*!< asm keyword for TASKING Compiler */ +#define __INLINE inline /*!< inline keyword for TASKING Compiler */ #endif +/* ################### Compiler specific Intrinsics + * ########################### */ -/* ################### Compiler specific Intrinsics ########################### */ - -#if defined ( __CC_ARM ) /*------------------RealView Compiler -----------------*/ +#if defined(__CC_ARM) /*------------------RealView Compiler \ + -----------------*/ /* ARM armcc specific functions */ /** @@ -55,25 +58,17 @@ * * Return the actual process stack pointer */ -__ASM uint32_t __get_PSP(void) -{ - mrs r0, psp - bx lr -} +__ASM uint32_t __get_PSP(void) { mrs r0, psp bx lr } /** * @brief Set the Process Stack Pointer * * @param topOfProcStack Process Stack Pointer * - * Assign the value ProcessStackPointer to the MSP + * Assign the value ProcessStackPointer to the MSP * (process stack pointer) Cortex processor register */ -__ASM void __set_PSP(uint32_t topOfProcStack) -{ - msr psp, r0 - bx lr -} +__ASM void __set_PSP(uint32_t topOfProcStack) { msr psp, r0 bx lr } /** * @brief Return the Main Stack Pointer @@ -83,25 +78,17 @@ __ASM void __set_PSP(uint32_t topOfProcStack) * Return the current value of the MSP (main stack pointer) * Cortex processor register */ -__ASM uint32_t __get_MSP(void) -{ - mrs r0, msp - bx lr -} +__ASM uint32_t __get_MSP(void) { mrs r0, msp bx lr } /** * @brief Set the Main Stack Pointer * * @param topOfMainStack Main Stack Pointer * - * Assign the value mainStackPointer to the MSP + * Assign the value mainStackPointer to the MSP * (main stack pointer) Cortex processor register */ -__ASM void __set_MSP(uint32_t mainStackPointer) -{ - msr msp, r0 - bx lr -} +__ASM void __set_MSP(uint32_t mainStackPointer) { msr msp, r0 bx lr } /** * @brief Reverse byte order in unsigned short value @@ -111,14 +98,11 @@ __ASM void __set_MSP(uint32_t mainStackPointer) * * Reverse byte order in unsigned short value */ -__ASM uint32_t __REV16(uint16_t value) -{ - rev16 r0, r0 - bx lr -} +__ASM uint32_t __REV16(uint16_t value) { rev16 r0, r0 bx lr } /** - * @brief Reverse byte order in signed short value with sign extension to integer + * @brief Reverse byte order in signed short value with sign extension to + * integer * * @param value value to reverse * @return reversed value @@ -127,11 +111,9 @@ __ASM uint32_t __REV16(uint16_t value) */ __ASM int32_t __REVSH(int16_t value) { - revsh r0, r0 - bx lr + revsh r0, r0 bx lr } - #if (__ARMCC_VERSION < 400000) /** @@ -139,10 +121,7 @@ __ASM int32_t __REVSH(int16_t value) * * Removes the exclusive lock which is created by ldrex. */ -__ASM void __CLREX(void) -{ - clrex -} +__ASM void __CLREX(void) { clrex } /** * @brief Return the Base Priority value @@ -151,11 +130,7 @@ __ASM void __CLREX(void) * * Return the content of the base priority register */ -__ASM uint32_t __get_BASEPRI(void) -{ - mrs r0, basepri - bx lr -} +__ASM uint32_t __get_BASEPRI(void) { mrs r0, basepri bx lr } /** * @brief Set the Base Priority value @@ -164,11 +139,7 @@ __ASM uint32_t __get_BASEPRI(void) * * Set the base priority register */ -__ASM void __set_BASEPRI(uint32_t basePri) -{ - msr basepri, r0 - bx lr -} +__ASM void __set_BASEPRI(uint32_t basePri) { msr basepri, r0 bx lr } /** * @brief Return the Priority Mask value @@ -177,11 +148,7 @@ __ASM void __set_BASEPRI(uint32_t basePri) * * Return state of the priority mask bit from the priority mask register */ -__ASM uint32_t __get_PRIMASK(void) -{ - mrs r0, primask - bx lr -} +__ASM uint32_t __get_PRIMASK(void) { mrs r0, primask bx lr } /** * @brief Set the Priority Mask value @@ -190,11 +157,7 @@ __ASM uint32_t __get_PRIMASK(void) * * Set the priority mask bit in the priority mask register */ -__ASM void __set_PRIMASK(uint32_t priMask) -{ - msr primask, r0 - bx lr -} +__ASM void __set_PRIMASK(uint32_t priMask) { msr primask, r0 bx lr } /** * @brief Return the Fault Mask value @@ -203,11 +166,7 @@ __ASM void __set_PRIMASK(uint32_t priMask) * * Return the content of the fault mask register */ -__ASM uint32_t __get_FAULTMASK(void) -{ - mrs r0, faultmask - bx lr -} +__ASM uint32_t __get_FAULTMASK(void) { mrs r0, faultmask bx lr } /** * @brief Set the Fault Mask value @@ -216,24 +175,16 @@ __ASM uint32_t __get_FAULTMASK(void) * * Set the fault mask register */ -__ASM void __set_FAULTMASK(uint32_t faultMask) -{ - msr faultmask, r0 - bx lr -} +__ASM void __set_FAULTMASK(uint32_t faultMask) { msr faultmask, r0 bx lr } /** * @brief Return the Control Register value - * + * * @return Control value * * Return the content of the control register */ -__ASM uint32_t __get_CONTROL(void) -{ - mrs r0, control - bx lr -} +__ASM uint32_t __get_CONTROL(void) { mrs r0, control bx lr } /** * @brief Set the Control Register value @@ -244,17 +195,15 @@ __ASM uint32_t __get_CONTROL(void) */ __ASM void __set_CONTROL(uint32_t control) { - msr control, r0 - bx lr + msr control, r0 bx lr } -#endif /* __ARMCC_VERSION */ +#endif /* __ARMCC_VERSION */ - - -#elif (defined (__ICCARM__)) /*------------------ ICC Compiler -------------------*/ +#elif (defined( \ + __ICCARM__)) /*------------------ ICC Compiler -------------------*/ /* IAR iccarm specific functions */ -#pragma diag_suppress=Pe940 +#pragma diag_suppress = Pe940 /** * @brief Return the Process Stack Pointer @@ -265,8 +214,8 @@ __ASM void __set_CONTROL(uint32_t control) */ uint32_t __get_PSP(void) { - __ASM("mrs r0, psp"); - __ASM("bx lr"); + __ASM("mrs r0, psp"); + __ASM("bx lr"); } /** @@ -274,13 +223,13 @@ uint32_t __get_PSP(void) * * @param topOfProcStack Process Stack Pointer * - * Assign the value ProcessStackPointer to the MSP + * Assign the value ProcessStackPointer to the MSP * (process stack pointer) Cortex processor register */ void __set_PSP(uint32_t topOfProcStack) { - __ASM("msr psp, r0"); - __ASM("bx lr"); + __ASM("msr psp, r0"); + __ASM("bx lr"); } /** @@ -293,8 +242,8 @@ void __set_PSP(uint32_t topOfProcStack) */ uint32_t __get_MSP(void) { - __ASM("mrs r0, msp"); - __ASM("bx lr"); + __ASM("mrs r0, msp"); + __ASM("bx lr"); } /** @@ -302,13 +251,13 @@ uint32_t __get_MSP(void) * * @param topOfMainStack Main Stack Pointer * - * Assign the value mainStackPointer to the MSP + * Assign the value mainStackPointer to the MSP * (main stack pointer) Cortex processor register */ void __set_MSP(uint32_t topOfMainStack) { - __ASM("msr msp, r0"); - __ASM("bx lr"); + __ASM("msr msp, r0"); + __ASM("bx lr"); } /** @@ -321,8 +270,8 @@ void __set_MSP(uint32_t topOfMainStack) */ uint32_t __REV16(uint16_t value) { - __ASM("rev16 r0, r0"); - __ASM("bx lr"); + __ASM("rev16 r0, r0"); + __ASM("bx lr"); } /** @@ -335,8 +284,8 @@ uint32_t __REV16(uint16_t value) */ uint32_t __RBIT(uint32_t value) { - __ASM("rbit r0, r0"); - __ASM("bx lr"); + __ASM("rbit r0, r0"); + __ASM("bx lr"); } /** @@ -349,8 +298,8 @@ uint32_t __RBIT(uint32_t value) */ uint8_t __LDREXB(uint8_t *addr) { - __ASM("ldrexb r0, [r0]"); - __ASM("bx lr"); + __ASM("ldrexb r0, [r0]"); + __ASM("bx lr"); } /** @@ -363,8 +312,8 @@ uint8_t __LDREXB(uint8_t *addr) */ uint16_t __LDREXH(uint16_t *addr) { - __ASM("ldrexh r0, [r0]"); - __ASM("bx lr"); + __ASM("ldrexh r0, [r0]"); + __ASM("bx lr"); } /** @@ -377,8 +326,8 @@ uint16_t __LDREXH(uint16_t *addr) */ uint32_t __LDREXW(uint32_t *addr) { - __ASM("ldrex r0, [r0]"); - __ASM("bx lr"); + __ASM("ldrex r0, [r0]"); + __ASM("bx lr"); } /** @@ -392,8 +341,8 @@ uint32_t __LDREXW(uint32_t *addr) */ uint32_t __STREXB(uint8_t value, uint8_t *addr) { - __ASM("strexb r0, r0, [r1]"); - __ASM("bx lr"); + __ASM("strexb r0, r0, [r1]"); + __ASM("bx lr"); } /** @@ -407,8 +356,8 @@ uint32_t __STREXB(uint8_t value, uint8_t *addr) */ uint32_t __STREXH(uint16_t value, uint16_t *addr) { - __ASM("strexh r0, r0, [r1]"); - __ASM("bx lr"); + __ASM("strexh r0, r0, [r1]"); + __ASM("bx lr"); } /** @@ -422,14 +371,14 @@ uint32_t __STREXH(uint16_t value, uint16_t *addr) */ uint32_t __STREXW(uint32_t value, uint32_t *addr) { - __ASM("strex r0, r0, [r1]"); - __ASM("bx lr"); + __ASM("strex r0, r0, [r1]"); + __ASM("bx lr"); } -#pragma diag_default=Pe940 +#pragma diag_default = Pe940 - -#elif (defined (__GNUC__)) /*------------------ GNU Compiler ---------------------*/ +#elif (defined( \ + __GNUC__)) /*------------------ GNU Compiler ---------------------*/ /* GNU gcc specific functions */ /** @@ -439,15 +388,16 @@ uint32_t __STREXW(uint32_t value, uint32_t *addr) * * Return the actual process stack pointer */ -uint32_t __get_PSP(void) __attribute__( ( naked ) ); +uint32_t __get_PSP(void) __attribute__((naked)); uint32_t __get_PSP(void) { - uint32_t result=0; + uint32_t result = 0; - __ASM volatile ("MRS %0, psp\n\t" - "MOV r0, %0 \n\t" - "BX lr \n\t" : "=r" (result) ); - return(result); + __ASM volatile("MRS %0, psp\n\t" + "MOV r0, %0 \n\t" + "BX lr \n\t" + : "=r"(result)); + return (result); } /** @@ -455,14 +405,16 @@ uint32_t __get_PSP(void) * * @param topOfProcStack Process Stack Pointer * - * Assign the value ProcessStackPointer to the MSP + * Assign the value ProcessStackPointer to the MSP * (process stack pointer) Cortex processor register */ -void __set_PSP(uint32_t topOfProcStack) __attribute__( ( naked ) ); +void __set_PSP(uint32_t topOfProcStack) __attribute__((naked)); void __set_PSP(uint32_t topOfProcStack) { - __ASM volatile ("MSR psp, %0\n\t" - "BX lr \n\t" : : "r" (topOfProcStack) ); + __ASM volatile("MSR psp, %0\n\t" + "BX lr \n\t" + : + : "r"(topOfProcStack)); } /** @@ -473,15 +425,16 @@ void __set_PSP(uint32_t topOfProcStack) * Return the current value of the MSP (main stack pointer) * Cortex processor register */ -uint32_t __get_MSP(void) __attribute__( ( naked ) ); +uint32_t __get_MSP(void) __attribute__((naked)); uint32_t __get_MSP(void) { - uint32_t result=0; + uint32_t result = 0; - __ASM volatile ("MRS %0, msp\n\t" - "MOV r0, %0 \n\t" - "BX lr \n\t" : "=r" (result) ); - return(result); + __ASM volatile("MRS %0, msp\n\t" + "MOV r0, %0 \n\t" + "BX lr \n\t" + : "=r"(result)); + return (result); } /** @@ -489,14 +442,16 @@ uint32_t __get_MSP(void) * * @param topOfMainStack Main Stack Pointer * - * Assign the value mainStackPointer to the MSP + * Assign the value mainStackPointer to the MSP * (main stack pointer) Cortex processor register */ -void __set_MSP(uint32_t topOfMainStack) __attribute__( ( naked ) ); +void __set_MSP(uint32_t topOfMainStack) __attribute__((naked)); void __set_MSP(uint32_t topOfMainStack) { - __ASM volatile ("MSR msp, %0\n\t" - "BX lr \n\t" : : "r" (topOfMainStack) ); + __ASM volatile("MSR msp, %0\n\t" + "BX lr \n\t" + : + : "r"(topOfMainStack)); } /** @@ -508,10 +463,10 @@ void __set_MSP(uint32_t topOfMainStack) */ uint32_t __get_BASEPRI(void) { - uint32_t result=0; - - __ASM volatile ("MRS %0, basepri_max" : "=r" (result) ); - return(result); + uint32_t result = 0; + + __ASM volatile("MRS %0, basepri_max" : "=r"(result)); + return (result); } /** @@ -523,7 +478,7 @@ uint32_t __get_BASEPRI(void) */ void __set_BASEPRI(uint32_t value) { - __ASM volatile ("MSR basepri, %0" : : "r" (value) ); + __ASM volatile("MSR basepri, %0" : : "r"(value)); } /** @@ -535,10 +490,10 @@ void __set_BASEPRI(uint32_t value) */ uint32_t __get_PRIMASK(void) { - uint32_t result=0; + uint32_t result = 0; - __ASM volatile ("MRS %0, primask" : "=r" (result) ); - return(result); + __ASM volatile("MRS %0, primask" : "=r"(result)); + return (result); } /** @@ -550,7 +505,7 @@ uint32_t __get_PRIMASK(void) */ void __set_PRIMASK(uint32_t priMask) { - __ASM volatile ("MSR primask, %0" : : "r" (priMask) ); + __ASM volatile("MSR primask, %0" : : "r"(priMask)); } /** @@ -562,10 +517,10 @@ void __set_PRIMASK(uint32_t priMask) */ uint32_t __get_FAULTMASK(void) { - uint32_t result=0; - - __ASM volatile ("MRS %0, faultmask" : "=r" (result) ); - return(result); + uint32_t result = 0; + + __ASM volatile("MRS %0, faultmask" : "=r"(result)); + return (result); } /** @@ -577,22 +532,22 @@ uint32_t __get_FAULTMASK(void) */ void __set_FAULTMASK(uint32_t faultMask) { - __ASM volatile ("MSR faultmask, %0" : : "r" (faultMask) ); + __ASM volatile("MSR faultmask, %0" : : "r"(faultMask)); } /** * @brief Return the Control Register value -* -* @return Control value + * + * @return Control value * * Return the content of the control register */ uint32_t __get_CONTROL(void) { - uint32_t result=0; + uint32_t result = 0; - __ASM volatile ("MRS %0, control" : "=r" (result) ); - return(result); + __ASM volatile("MRS %0, control" : "=r"(result)); + return (result); } /** @@ -604,10 +559,9 @@ uint32_t __get_CONTROL(void) */ void __set_CONTROL(uint32_t control) { - __ASM volatile ("MSR control, %0" : : "r" (control) ); + __ASM volatile("MSR control, %0" : : "r"(control)); } - /** * @brief Reverse byte order in integer value * @@ -618,10 +572,10 @@ void __set_CONTROL(uint32_t control) */ uint32_t __REV(uint32_t value) { - uint32_t result=0; - - __ASM volatile ("rev %0, %1" : "=r" (result) : "r" (value) ); - return(result); + uint32_t result = 0; + + __ASM volatile("rev %0, %1" : "=r"(result) : "r"(value)); + return (result); } /** @@ -634,14 +588,15 @@ uint32_t __REV(uint32_t value) */ uint32_t __REV16(uint16_t value) { - uint32_t result=0; - - __ASM volatile ("rev16 %0, %1" : "=r" (result) : "r" (value) ); - return(result); + uint32_t result = 0; + + __ASM volatile("rev16 %0, %1" : "=r"(result) : "r"(value)); + return (result); } /** - * @brief Reverse byte order in signed short value with sign extension to integer + * @brief Reverse byte order in signed short value with sign extension to + * integer * * @param value value to reverse * @return reversed value @@ -650,10 +605,10 @@ uint32_t __REV16(uint16_t value) */ int32_t __REVSH(int16_t value) { - uint32_t result=0; - - __ASM volatile ("revsh %0, %1" : "=r" (result) : "r" (value) ); - return(result); + uint32_t result = 0; + + __ASM volatile("revsh %0, %1" : "=r"(result) : "r"(value)); + return (result); } /** @@ -666,10 +621,10 @@ int32_t __REVSH(int16_t value) */ uint32_t __RBIT(uint32_t value) { - uint32_t result=0; - - __ASM volatile ("rbit %0, %1" : "=r" (result) : "r" (value) ); - return(result); + uint32_t result = 0; + + __ASM volatile("rbit %0, %1" : "=r"(result) : "r"(value)); + return (result); } /** @@ -682,10 +637,10 @@ uint32_t __RBIT(uint32_t value) */ uint8_t __LDREXB(uint8_t *addr) { - uint8_t result=0; - - __ASM volatile ("ldrexb %0, [%1]" : "=r" (result) : "r" (addr) ); - return(result); + uint8_t result = 0; + + __ASM volatile("ldrexb %0, [%1]" : "=r"(result) : "r"(addr)); + return (result); } /** @@ -698,10 +653,10 @@ uint8_t __LDREXB(uint8_t *addr) */ uint16_t __LDREXH(uint16_t *addr) { - uint16_t result=0; - - __ASM volatile ("ldrexh %0, [%1]" : "=r" (result) : "r" (addr) ); - return(result); + uint16_t result = 0; + + __ASM volatile("ldrexh %0, [%1]" : "=r"(result) : "r"(addr)); + return (result); } /** @@ -714,10 +669,10 @@ uint16_t __LDREXH(uint16_t *addr) */ uint32_t __LDREXW(uint32_t *addr) { - uint32_t result=0; - - __ASM volatile ("ldrex %0, [%1]" : "=r" (result) : "r" (addr) ); - return(result); + uint32_t result = 0; + + __ASM volatile("ldrex %0, [%1]" : "=r"(result) : "r"(addr)); + return (result); } /** @@ -731,10 +686,12 @@ uint32_t __LDREXW(uint32_t *addr) */ uint32_t __STREXB(uint8_t value, uint8_t *addr) { - uint32_t result=0; - - __ASM volatile ("strexb %0, %2, [%1]" : "=r" (result) : "r" (addr), "r" (value) ); - return(result); + uint32_t result = 0; + + __ASM volatile("strexb %0, %2, [%1]" + : "=r"(result) + : "r"(addr), "r"(value)); + return (result); } /** @@ -748,10 +705,12 @@ uint32_t __STREXB(uint8_t value, uint8_t *addr) */ uint32_t __STREXH(uint16_t value, uint16_t *addr) { - uint32_t result=0; - - __ASM volatile ("strexh %0, %2, [%1]" : "=r" (result) : "r" (addr), "r" (value) ); - return(result); + uint32_t result = 0; + + __ASM volatile("strexh %0, %2, [%1]" + : "=r"(result) + : "r"(addr), "r"(value)); + return (result); } /** @@ -765,14 +724,14 @@ uint32_t __STREXH(uint16_t value, uint16_t *addr) */ uint32_t __STREXW(uint32_t value, uint32_t *addr) { - uint32_t result=0; - - __ASM volatile ("strex %0, %2, [%1]" : "=r" (result) : "r" (addr), "r" (value) ); - return(result); + uint32_t result = 0; + + __ASM volatile("strex %0, %2, [%1]" : "=r"(result) : "r"(addr), "r"(value)); + return (result); } - -#elif (defined (__TASKING__)) /*------------------ TASKING Compiler ---------------------*/ +#elif (defined(__TASKING__)) /*------------------ TASKING Compiler \ + ---------------------*/ /* TASKING carm specific functions */ /* diff --git a/ports/stm32f10x/automac.c b/ports/stm32f10x/automac.c index a6cb4fe8..02ed0212 100644 --- a/ports/stm32f10x/automac.c +++ b/ports/stm32f10x/automac.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2010 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2010 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include #include @@ -37,14 +37,14 @@ /* table to track tokens and poll-for-master frames */ typedef struct { /* Poll For Master indicates empty slot */ - bool pfm:1; + bool pfm : 1; /* a device that emits a frame indicates a used slot */ - bool emitter:1; + bool emitter : 1; /* token - indicates a token was passed from this slot */ /* important to know who the Next Station is */ - bool token:1; + bool token : 1; /* reserve some slots for fixed addresses */ - bool reserved:1; + bool reserved : 1; } AUTO_MAC_DATA; /* starting number available for AutoMAC */ #define MAC_SLOTS_OFFSET 32 @@ -61,45 +61,41 @@ static bool PFM_Cycle_Complete; static bool Auto_Mode_Enabled; /**************************************************************************** -* DESCRIPTION: Indication that we are an automode node -* RETURN: true if automode enabled -* NOTES: none -*****************************************************************************/ -bool automac_enabled( - void) + * DESCRIPTION: Indication that we are an automode node + * RETURN: true if automode enabled + * NOTES: none + *****************************************************************************/ +bool automac_enabled(void) { return Auto_Mode_Enabled; } /**************************************************************************** -* DESCRIPTION: Sets the automode status -* RETURN: nothing -* NOTES: none -*****************************************************************************/ -void automac_enabled_set( - bool status) + * DESCRIPTION: Sets the automode status + * RETURN: nothing + * NOTES: none + *****************************************************************************/ +void automac_enabled_set(bool status) { Auto_Mode_Enabled = status; } /**************************************************************************** -* DESCRIPTION: Indication that PFM has happened for a full cycle -* RETURN: true if full -* NOTES: none -*****************************************************************************/ -bool automac_pfm_cycle_complete( - void) + * DESCRIPTION: Indication that PFM has happened for a full cycle + * RETURN: true if full + * NOTES: none + *****************************************************************************/ +bool automac_pfm_cycle_complete(void) { return PFM_Cycle_Complete; } /**************************************************************************** -* DESCRIPTION: Indicates that an address is used or taken -* RETURN: true if used -* NOTES: none -*****************************************************************************/ -static bool automac_address_used( - uint8_t mac) + * DESCRIPTION: Indicates that an address is used or taken + * RETURN: true if used + * NOTES: none + *****************************************************************************/ +static bool automac_address_used(uint8_t mac) { bool status = false; @@ -114,12 +110,11 @@ static bool automac_address_used( } /**************************************************************************** -* DESCRIPTION: Validates an address as available, not taken, and within bounds -* RETURN: true if valid -* NOTES: none -*****************************************************************************/ -bool automac_free_address_valid( - uint8_t mac) + * DESCRIPTION: Validates an address as available, not taken, and within bounds + * RETURN: true if valid + * NOTES: none + *****************************************************************************/ +bool automac_free_address_valid(uint8_t mac) { bool status = false; @@ -133,16 +128,15 @@ bool automac_free_address_valid( } /**************************************************************************** -* DESCRIPTION: Determines the next station address to send the token -* RETURN: Next_Station, or 255 if there are no next stations -* NOTES: none -*****************************************************************************/ -uint8_t automac_next_station( - uint8_t mac) + * DESCRIPTION: Determines the next station address to send the token + * RETURN: Next_Station, or 255 if there are no next stations + * NOTES: none + *****************************************************************************/ +uint8_t automac_next_station(uint8_t mac) { - uint8_t i = 0; /* loop counter */ + uint8_t i = 0; /* loop counter */ uint8_t next_station = 255; /* return value */ - uint8_t test_station = 0; /* station number to test for token */ + uint8_t test_station = 0; /* station number to test for token */ test_station = (mac + 1) % 128; for (i = 0; i < MAC_SLOTS_MAX; i++) { @@ -157,12 +151,11 @@ uint8_t automac_next_station( } /**************************************************************************** -* DESCRIPTION: Determines the number of free MAC addresses -* RETURN: Number of free MAC addresses -* NOTES: none -*****************************************************************************/ -uint8_t automac_free_address_count( - void) + * DESCRIPTION: Determines the number of free MAC addresses + * RETURN: Number of free MAC addresses + * NOTES: none + *****************************************************************************/ +uint8_t automac_free_address_count(void) { uint8_t i = 0; uint8_t slots = 0; @@ -177,12 +170,11 @@ uint8_t automac_free_address_count( } /**************************************************************************** -* DESCRIPTION: Determines the number of free MAC addresses -* RETURN: Number of free MAC addresses -* NOTES: none -*****************************************************************************/ -uint8_t automac_free_address_mac( - uint8_t count) + * DESCRIPTION: Determines the number of free MAC addresses + * RETURN: Number of free MAC addresses + * NOTES: none + *****************************************************************************/ +uint8_t automac_free_address_mac(uint8_t count) { uint8_t i = 0; uint8_t slots = 0; @@ -202,12 +194,11 @@ uint8_t automac_free_address_mac( } /**************************************************************************** -* DESCRIPTION: Gets a free random address to use -* RETURN: free MAC addresses -* NOTES: none -*****************************************************************************/ -uint8_t automac_free_address_random( - void) + * DESCRIPTION: Gets a free random address to use + * RETURN: free MAC addresses + * NOTES: none + *****************************************************************************/ +uint8_t automac_free_address_random(void) { uint8_t count = 0; uint8_t random_count = 0; @@ -215,7 +206,7 @@ uint8_t automac_free_address_random( count = automac_free_address_count(); if (count) { - random_count =(uint8_t)(rand() % count); + random_count = (uint8_t)(rand() % count); mac = automac_free_address_mac(random_count); } @@ -223,60 +214,55 @@ uint8_t automac_free_address_random( } /**************************************************************************** -* DESCRIPTION: Gets the address stored. -* RETURN: MAC addresses -* NOTES: none -*****************************************************************************/ -uint8_t automac_address( - void) + * DESCRIPTION: Gets the address stored. + * RETURN: MAC addresses + * NOTES: none + *****************************************************************************/ +uint8_t automac_address(void) { return My_MAC_Address; } /**************************************************************************** -* DESCRIPTION: Sets the MAC address -* RETURN: MAC addresses -* NOTES: none -*****************************************************************************/ -void automac_address_set( - uint8_t mac) + * DESCRIPTION: Sets the MAC address + * RETURN: MAC addresses + * NOTES: none + *****************************************************************************/ +void automac_address_set(uint8_t mac) { My_MAC_Address = mac; } /**************************************************************************** -* DESCRIPTION: Gets the address stored. -* RETURN: MAC addresses -* NOTES: none -*****************************************************************************/ -uint16_t automac_time_slot( - void) + * DESCRIPTION: Gets the address stored. + * RETURN: MAC addresses + * NOTES: none + *****************************************************************************/ +uint16_t automac_time_slot(void) { return My_Time_Slot; } /**************************************************************************** -* DESCRIPTION: Sets the MAC address -* RETURN: MAC addresses -* NOTES: none -*****************************************************************************/ -void automac_address_init( - void) + * DESCRIPTION: Sets the MAC address + * RETURN: MAC addresses + * NOTES: none + *****************************************************************************/ +void automac_address_init(void) { My_MAC_Address = MAC_SLOTS_OFFSET + rand() % (MAC_SLOTS_MAX - MAC_SLOTS_OFFSET); /* at least as long as a dropped token - worst case */ My_Time_Slot = Tno_token + (MAC_SLOTS_MAX * Tslot); - My_Time_Slot += (uint16_t) My_MAC_Address *Tslot; + My_Time_Slot += (uint16_t)My_MAC_Address * Tslot; } /**************************************************************************** -* DESCRIPTION: Sets an open address slot -* RETURN: nothing -* NOTES: none -*****************************************************************************/ -void automac_pfm_set( - uint8_t mac) + * DESCRIPTION: Sets an open address slot + * RETURN: nothing + * NOTES: none + *****************************************************************************/ +void automac_pfm_set(uint8_t mac) { if (mac < MAC_SLOTS_MAX) { if (Auto_MAC_Data[mac].pfm) { @@ -290,12 +276,11 @@ void automac_pfm_set( } /**************************************************************************** -* DESCRIPTION: Sets a used address slot -* RETURN: nothing -* NOTES: none -*****************************************************************************/ -void automac_token_set( - uint8_t mac) + * DESCRIPTION: Sets a used address slot + * RETURN: nothing + * NOTES: none + *****************************************************************************/ +void automac_token_set(uint8_t mac) { if (mac < MAC_SLOTS_MAX) { Auto_MAC_Data[mac].token = true; @@ -303,12 +288,11 @@ void automac_token_set( } /**************************************************************************** -* DESCRIPTION: Sets a used address slot -* RETURN: nothing -* NOTES: none -*****************************************************************************/ -void automac_emitter_set( - uint8_t mac) + * DESCRIPTION: Sets a used address slot + * RETURN: nothing + * NOTES: none + *****************************************************************************/ +void automac_emitter_set(uint8_t mac) { if (mac < MAC_SLOTS_MAX) { Auto_MAC_Data[mac].emitter = true; @@ -316,12 +300,11 @@ void automac_emitter_set( } /**************************************************************************** -* DESCRIPTION: Initializes the auto MAC module -* RETURN: nothing -* NOTES: none -*****************************************************************************/ -void automac_init( - void) + * DESCRIPTION: Initializes the auto MAC module + * RETURN: nothing + * NOTES: none + *****************************************************************************/ +void automac_init(void) { uint8_t i = 0; @@ -346,8 +329,7 @@ void automac_init( #include "ctest.h" /* test the ring buffer */ -void test_Auto_MAC( - Test * pTest) +void test_Auto_MAC(Test *pTest) { uint8_t mac = 255; @@ -387,8 +369,8 @@ void test_Auto_MAC( mac = automac_free_address_mac(1); ct_test(pTest, mac == (MAC_SLOTS_OFFSET + 2)); mac = automac_free_address_random(); - ct_test(pTest, (mac == (MAC_SLOTS_OFFSET + 1)) || - (mac == (MAC_SLOTS_OFFSET + 2))); + ct_test(pTest, + (mac == (MAC_SLOTS_OFFSET + 1)) || (mac == (MAC_SLOTS_OFFSET + 2))); /* test 3 free addresses */ automac_pfm_set(126); mac = automac_free_address_mac(0); @@ -398,8 +380,9 @@ void test_Auto_MAC( mac = automac_free_address_mac(2); ct_test(pTest, mac == 126); mac = automac_free_address_random(); - ct_test(pTest, (mac == (MAC_SLOTS_OFFSET + 1)) || - (mac == (MAC_SLOTS_OFFSET + 2)) || (mac == 126)); + ct_test(pTest, + (mac == (MAC_SLOTS_OFFSET + 1)) || (mac == (MAC_SLOTS_OFFSET + 2)) || + (mac == 126)); /* test the stored address */ mac = automac_address(); ct_test(pTest, mac < MAC_SLOTS_MAX); @@ -414,8 +397,7 @@ void test_Auto_MAC( } #ifdef TEST_AUTOMAC -int main( - void) +int main(void) { Test *pTest; bool rc; @@ -428,7 +410,7 @@ int main( ct_setStream(pTest, stdout); ct_run(pTest); - (void) ct_report(pTest); + (void)ct_report(pTest); ct_destroy(pTest); diff --git a/ports/stm32f10x/bacnet.c b/ports/stm32f10x/bacnet.c index 11a92941..8c941c4c 100644 --- a/ports/stm32f10x/bacnet.c +++ b/ports/stm32f10x/bacnet.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2011 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2011 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include /* hardware layer includes */ @@ -46,8 +46,7 @@ static struct mstimer DCC_Timer; #define DCC_CYCLE_SECONDS 1 -void bacnet_init( - void) +void bacnet_init(void) { dlmstp_set_mac_address(255); dlmstp_set_max_master(127); @@ -57,33 +56,31 @@ void bacnet_init( Device_Init(NULL); /* set up our confirmed service unrecognized service handler - required! */ - apdu_set_unrecognized_service_handler_handler - (handler_unrecognized_service); + apdu_set_unrecognized_service_handler_handler(handler_unrecognized_service); /* 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); /* 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, - handler_read_property); - apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROP_MULTIPLE, - handler_read_property_multiple); - apdu_set_confirmed_handler(SERVICE_CONFIRMED_REINITIALIZE_DEVICE, - handler_reinitialize_device); - apdu_set_confirmed_handler(SERVICE_CONFIRMED_WRITE_PROPERTY, - handler_write_property); + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_READ_PROPERTY, handler_read_property); + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_READ_PROP_MULTIPLE, handler_read_property_multiple); + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_REINITIALIZE_DEVICE, handler_reinitialize_device); + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_WRITE_PROPERTY, handler_write_property); /* handle communication so we can shutup when asked */ apdu_set_confirmed_handler(SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL, handler_device_communication_control); /* start the cyclic 1 second timer for DCC */ - mstimer_set(&DCC_Timer, DCC_CYCLE_SECONDS*1000); + mstimer_set(&DCC_Timer, DCC_CYCLE_SECONDS * 1000); /* Hello World! */ Send_I_Am(&Handler_Transmit_Buffer[0]); } static uint8_t PDUBuffer[MAX_MPDU]; -void bacnet_task( - void) +void bacnet_task(void) { uint16_t pdu_len; BACNET_ADDRESS src; /* source address */ diff --git a/ports/stm32f10x/bo.c b/ports/stm32f10x/bo.c index 49c0d3d4..837bb2e1 100644 --- a/ports/stm32f10x/bo.c +++ b/ports/stm32f10x/bo.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2011 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2011 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* Binary Output Objects - customize for your use */ @@ -53,34 +53,18 @@ static uint8_t Out_Of_Service[MAX_BINARY_OUTPUTS]; static uint8_t Polarity[MAX_BINARY_OUTPUTS]; /* These three arrays are used by the ReadPropertyMultiple handler */ -static const int Binary_Output_Properties_Required[] = { - PROP_OBJECT_IDENTIFIER, - PROP_OBJECT_NAME, - PROP_OBJECT_TYPE, - PROP_PRESENT_VALUE, - PROP_STATUS_FLAGS, - PROP_EVENT_STATE, - PROP_OUT_OF_SERVICE, - PROP_POLARITY, - PROP_PRIORITY_ARRAY, - PROP_RELINQUISH_DEFAULT, - -1 -}; +static const int Binary_Output_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, + PROP_OBJECT_NAME, PROP_OBJECT_TYPE, PROP_PRESENT_VALUE, PROP_STATUS_FLAGS, + PROP_EVENT_STATE, PROP_OUT_OF_SERVICE, PROP_POLARITY, PROP_PRIORITY_ARRAY, + PROP_RELINQUISH_DEFAULT, -1 }; -static const int Binary_Output_Properties_Optional[] = { - PROP_ACTIVE_TEXT, - PROP_INACTIVE_TEXT, - -1 -}; +static const int Binary_Output_Properties_Optional[] = { PROP_ACTIVE_TEXT, + PROP_INACTIVE_TEXT, -1 }; -static const int Binary_Output_Properties_Proprietary[] = { - -1 -}; +static const int Binary_Output_Properties_Proprietary[] = { -1 }; void Binary_Output_Property_Lists( - const int **pRequired, - const int **pOptional, - const int **pProprietary) + const int **pRequired, const int **pOptional, const int **pProprietary) { if (pRequired) *pRequired = Binary_Output_Properties_Required; @@ -93,8 +77,7 @@ void Binary_Output_Property_Lists( } /* we simply have 0-n object instances. */ -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; @@ -103,22 +86,19 @@ bool Binary_Output_Valid_Instance( } /* we simply have 0-n object instances. */ -unsigned Binary_Output_Count( - void) +unsigned Binary_Output_Count(void) { return MAX_BINARY_OUTPUTS; } /* we simply have 0-n object instances. */ -uint32_t Binary_Output_Index_To_Instance( - unsigned index) +uint32_t Binary_Output_Index_To_Instance(unsigned index) { return index; } /* we simply have 0-n object instances. */ -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; @@ -128,8 +108,7 @@ unsigned Binary_Output_Instance_To_Index( return index; } -static BACNET_BINARY_PV Present_Value( - unsigned int index) +static BACNET_BINARY_PV Present_Value(unsigned int index) { BACNET_BINARY_PV value = RELINQUISH_DEFAULT; BACNET_BINARY_PV current_value = RELINQUISH_DEFAULT; @@ -137,9 +116,9 @@ static BACNET_BINARY_PV Present_Value( if (index < MAX_BINARY_OUTPUTS) { for (i = 0; i < BACNET_MAX_PRIORITY; i++) { - current_value = (BACNET_BINARY_PV) Binary_Output_Level[index][i]; + current_value = (BACNET_BINARY_PV)Binary_Output_Level[index][i]; if (current_value != BINARY_NULL) { - value = (BACNET_BINARY_PV) Binary_Output_Level[index][i]; + value = (BACNET_BINARY_PV)Binary_Output_Level[index][i]; break; } } @@ -148,8 +127,7 @@ static BACNET_BINARY_PV Present_Value( return value; } -BACNET_BINARY_PV Binary_Output_Present_Value( - uint32_t object_instance) +BACNET_BINARY_PV Binary_Output_Present_Value(uint32_t object_instance) { unsigned index = 0; @@ -159,15 +137,13 @@ BACNET_BINARY_PV Binary_Output_Present_Value( } bool Binary_Output_Present_Value_Set( - uint32_t instance, - BACNET_BINARY_PV binary_value, - unsigned priority) -{ /* 0..15 */ + uint32_t instance, BACNET_BINARY_PV binary_value, unsigned priority) +{ /* 0..15 */ bool status = false; if (instance < MAX_BINARY_OUTPUTS) { if (priority < BACNET_MAX_PRIORITY) { - Binary_Output_Level[instance][priority] = (uint8_t) binary_value; + Binary_Output_Level[instance][priority] = (uint8_t)binary_value; status = true; } } @@ -175,9 +151,7 @@ bool Binary_Output_Present_Value_Set( return status; } -bool Binary_Output_Polarity_Set( - uint32_t instance, - BACNET_POLARITY polarity) +bool Binary_Output_Polarity_Set(uint32_t instance, BACNET_POLARITY polarity) { bool status = false; @@ -191,29 +165,25 @@ bool Binary_Output_Polarity_Set( return status; } -BACNET_POLARITY Binary_Output_Polarity( - uint32_t instance) +BACNET_POLARITY Binary_Output_Polarity(uint32_t instance) { BACNET_POLARITY polarity = POLARITY_NORMAL; if (instance < MAX_BINARY_OUTPUTS) { - polarity = (BACNET_POLARITY) Polarity[instance]; + polarity = (BACNET_POLARITY)Polarity[instance]; } return polarity; } -void Binary_Output_Out_Of_Service_Set( - uint32_t instance, - bool flag) +void Binary_Output_Out_Of_Service_Set(uint32_t instance, bool flag) { if (instance < MAX_BINARY_OUTPUTS) { Out_Of_Service[instance] = flag; } } -bool Binary_Output_Out_Of_Service( - uint32_t instance) +bool Binary_Output_Out_Of_Service(uint32_t instance) { bool flag = false; @@ -226,14 +196,13 @@ bool Binary_Output_Out_Of_Service( /* note: the object name must be unique within this device */ bool Binary_Output_Object_Name( - uint32_t object_instance, - BACNET_CHARACTER_STRING * object_name) + uint32_t object_instance, BACNET_CHARACTER_STRING *object_name) { - static char text_string[16] = "BO-0"; /* okay for single thread */ + static char text_string[16] = "BO-0"; /* okay for single thread */ bool status = false; if (object_instance < MAX_BINARY_OUTPUTS) { - text_string[3] = '0' + (uint8_t) object_instance; + text_string[3] = '0' + (uint8_t)object_instance; status = characterstring_init_ansi(object_name, text_string); } @@ -241,11 +210,10 @@ bool Binary_Output_Object_Name( } /* return apdu len, or -1 on error */ -int Binary_Output_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Binary_Output_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { 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; @@ -260,7 +228,8 @@ int Binary_Output_Read_Property( } apdu = rpdata->application_data; switch (rpdata->object_property) { - /* object id, object name, object type are handled in Device object */ + /* object id, object name, object type are handled in Device object + */ case PROP_PRESENT_VALUE: present_value = Binary_Output_Present_Value(rpdata->object_instance); @@ -290,8 +259,7 @@ int Binary_Output_Read_Property( object_index = Binary_Output_Instance_To_Index(rpdata->object_instance); apdu_len = - encode_application_enumerated(&apdu[0], - Polarity[object_index]); + encode_application_enumerated(&apdu[0], Polarity[object_index]); break; case PROP_PRIORITY_ARRAY: /* Array element zero is the number of elements in the array */ @@ -305,14 +273,13 @@ int Binary_Output_Read_Property( Binary_Output_Instance_To_Index(rpdata->object_instance); for (i = 0; i < BACNET_MAX_PRIORITY; i++) { /* FIXME: check if we have room before adding it to APDU */ - present_value = (BACNET_BINARY_PV) - Binary_Output_Level[object_index][i]; + present_value = + (BACNET_BINARY_PV)Binary_Output_Level[object_index][i]; if (present_value == BINARY_NULL) { len = encode_application_null(&apdu[apdu_len]); } else { - len = - encode_application_enumerated(&apdu[apdu_len], - present_value); + len = encode_application_enumerated( + &apdu[apdu_len], present_value); } /* add it if we have room */ if ((apdu_len + len) < MAX_APDU) @@ -329,14 +296,13 @@ int Binary_Output_Read_Property( Binary_Output_Instance_To_Index(rpdata->object_instance); if (rpdata->array_index <= BACNET_MAX_PRIORITY) { present_value = (BACNET_BINARY_PV) - Binary_Output_Level[object_index][rpdata->array_index - - 1]; + Binary_Output_Level[object_index] + [rpdata->array_index - 1]; if (present_value == BINARY_NULL) { apdu_len = encode_application_null(&apdu[apdu_len]); } else { - apdu_len = - encode_application_enumerated(&apdu[apdu_len], - present_value); + apdu_len = encode_application_enumerated( + &apdu[apdu_len], present_value); } } else { rpdata->error_class = ERROR_CLASS_PROPERTY; @@ -377,19 +343,17 @@ int Binary_Output_Read_Property( } /* returns true if successful */ -bool Binary_Output_Write_Property( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Binary_Output_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data) { - bool status = false; /* return value */ + bool status = false; /* return value */ unsigned int priority = 0; BACNET_BINARY_PV level = BINARY_NULL; int len = 0; BACNET_APPLICATION_DATA_VALUE value; /* decode the some of the request */ - len = - bacapp_decode_application_data(wp_data->application_data, - wp_data->application_data_len, &value); + len = bacapp_decode_application_data( + wp_data->application_data, wp_data->application_data_len, &value); /* FIXME: len < application_data_len: more data? */ if (len < 0) { /* error while decoding - a value larger than we can handle */ @@ -408,19 +372,19 @@ bool Binary_Output_Write_Property( case PROP_PRESENT_VALUE: status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_ENUMERATED, - &wp_data->error_class, &wp_data->error_code); + &wp_data->error_class, &wp_data->error_code); if (status) { 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 */ ) && + (priority != 6 /* reserved */) && (value.type.Enumerated <= MAX_BINARY_PV)) { - level = (BACNET_BINARY_PV) value.type.Enumerated; + level = (BACNET_BINARY_PV)value.type.Enumerated; priority--; - Binary_Output_Present_Value_Set(wp_data->object_instance, - level, priority); + Binary_Output_Present_Value_Set( + wp_data->object_instance, level, priority); } 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 @@ -434,21 +398,20 @@ bool Binary_Output_Write_Property( wp_data->error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; } } else { - status = - WPValidateArgType(&value, BACNET_APPLICATION_TAG_NULL, + status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_NULL, &wp_data->error_class, &wp_data->error_code); if (status) { level = BINARY_NULL; priority = wp_data->priority; if (priority && (priority <= BACNET_MAX_PRIORITY)) { priority--; - Binary_Output_Present_Value_Set - (wp_data->object_instance, level, priority); + Binary_Output_Present_Value_Set( + wp_data->object_instance, level, priority); } else if (priority == 6) { status = false; - /* Command priority 6 is reserved for use by Minimum On/Off - algorithm and may not be used for other purposes in any - object. */ + /* Command priority 6 is reserved for use by Minimum + On/Off algorithm and may not be used for other + purposes in any object. */ wp_data->error_class = ERROR_CLASS_PROPERTY; wp_data->error_code = ERROR_CODE_WRITE_ACCESS_DENIED; } else { @@ -460,22 +423,21 @@ bool Binary_Output_Write_Property( } break; case PROP_OUT_OF_SERVICE: - status = - WPValidateArgType(&value, BACNET_APPLICATION_TAG_BOOLEAN, + status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_BOOLEAN, &wp_data->error_class, &wp_data->error_code); if (status) { - Binary_Output_Out_Of_Service_Set(wp_data->object_instance, - value.type.Boolean); + Binary_Output_Out_Of_Service_Set( + wp_data->object_instance, value.type.Boolean); } break; case PROP_POLARITY: status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_ENUMERATED, - &wp_data->error_class, &wp_data->error_code); + &wp_data->error_class, &wp_data->error_code); if (status) { if (value.type.Enumerated < MAX_POLARITY) { Binary_Output_Polarity_Set(wp_data->object_instance, - (BACNET_POLARITY) value.type.Enumerated); + (BACNET_POLARITY)value.type.Enumerated); } else { status = false; wp_data->error_class = ERROR_CLASS_PROPERTY; @@ -507,8 +469,7 @@ bool Binary_Output_Write_Property( return status; } -void Binary_Output_Init( - void) +void Binary_Output_Init(void) { unsigned i, j; diff --git a/ports/stm32f10x/device.c b/ports/stm32f10x/device.c index 753c8ebf..a02de167 100644 --- a/ports/stm32f10x/device.c +++ b/ports/stm32f10x/device.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2011 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2011 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include @@ -42,10 +42,8 @@ #include "bacnet/basic/object/bo.h" /* forward prototype */ -int Device_Read_Property_Local( - BACNET_READ_PROPERTY_DATA * rpdata); -bool Device_Write_Property_Local( - BACNET_WRITE_PROPERTY_DATA * wp_data); +int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA *rpdata); +bool Device_Write_Property_Local(BACNET_WRITE_PROPERTY_DATA *wp_data); static struct my_object_functions { BACNET_OBJECT_TYPE Object_Type; @@ -57,19 +55,16 @@ static struct my_object_functions { read_property_function Object_Read_Property; write_property_function Object_Write_Property; rpm_property_lists_function Object_RPM_List; -} Object_Table[] = { - { - OBJECT_DEVICE, NULL, /* don't init - recursive! */ - Device_Count, Device_Index_To_Instance, - Device_Valid_Object_Instance_Number, Device_Object_Name, - Device_Read_Property_Local, Device_Write_Property_Local, - Device_Property_Lists}, { - OBJECT_BINARY_OUTPUT, Binary_Output_Init, Binary_Output_Count, - Binary_Output_Index_To_Instance, Binary_Output_Valid_Instance, - Binary_Output_Object_Name, Binary_Output_Read_Property, - Binary_Output_Write_Property, Binary_Output_Property_Lists}, { - MAX_BACNET_OBJECT_TYPE, NULL, NULL, NULL, NULL, NULL, NULL, NULL} -}; +} Object_Table[] = { { OBJECT_DEVICE, NULL, /* don't init - recursive! */ + Device_Count, Device_Index_To_Instance, + Device_Valid_Object_Instance_Number, + Device_Object_Name, Device_Read_Property_Local, + Device_Write_Property_Local, Device_Property_Lists }, + { OBJECT_BINARY_OUTPUT, Binary_Output_Init, Binary_Output_Count, + Binary_Output_Index_To_Instance, Binary_Output_Valid_Instance, + Binary_Output_Object_Name, Binary_Output_Read_Property, + Binary_Output_Write_Property, Binary_Output_Property_Lists }, + { MAX_BACNET_OBJECT_TYPE, NULL, NULL, NULL, NULL, NULL, NULL, NULL } }; /* note: you really only need to define variables for properties that are writable or that may change. @@ -84,42 +79,20 @@ static const char *Reinit_Password = "stm32-challenge"; static const char *BACnet_Version = BACNET_VERSION_TEXT; /* These three arrays are used by the ReadPropertyMultiple handler */ -static const int Device_Properties_Required[] = { - PROP_OBJECT_IDENTIFIER, - PROP_OBJECT_NAME, - PROP_OBJECT_TYPE, - PROP_SYSTEM_STATUS, - PROP_VENDOR_NAME, - PROP_VENDOR_IDENTIFIER, - PROP_MODEL_NAME, - PROP_FIRMWARE_REVISION, - PROP_APPLICATION_SOFTWARE_VERSION, - PROP_PROTOCOL_VERSION, - PROP_PROTOCOL_REVISION, - PROP_PROTOCOL_SERVICES_SUPPORTED, - PROP_PROTOCOL_OBJECT_TYPES_SUPPORTED, - PROP_OBJECT_LIST, - PROP_MAX_APDU_LENGTH_ACCEPTED, - PROP_SEGMENTATION_SUPPORTED, - PROP_APDU_TIMEOUT, - PROP_NUMBER_OF_APDU_RETRIES, - PROP_DEVICE_ADDRESS_BINDING, - PROP_DATABASE_REVISION, - -1 -}; +static const int Device_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, + PROP_OBJECT_NAME, PROP_OBJECT_TYPE, PROP_SYSTEM_STATUS, PROP_VENDOR_NAME, + PROP_VENDOR_IDENTIFIER, PROP_MODEL_NAME, PROP_FIRMWARE_REVISION, + PROP_APPLICATION_SOFTWARE_VERSION, PROP_PROTOCOL_VERSION, + PROP_PROTOCOL_REVISION, PROP_PROTOCOL_SERVICES_SUPPORTED, + PROP_PROTOCOL_OBJECT_TYPES_SUPPORTED, PROP_OBJECT_LIST, + PROP_MAX_APDU_LENGTH_ACCEPTED, PROP_SEGMENTATION_SUPPORTED, + PROP_APDU_TIMEOUT, PROP_NUMBER_OF_APDU_RETRIES, PROP_DEVICE_ADDRESS_BINDING, + PROP_DATABASE_REVISION, -1 }; -static const int Device_Properties_Optional[] = { - PROP_DESCRIPTION, - PROP_LOCATION, - PROP_MAX_MASTER, - PROP_MAX_INFO_FRAMES, - -1 -}; +static const int Device_Properties_Optional[] = { PROP_DESCRIPTION, + PROP_LOCATION, PROP_MAX_MASTER, PROP_MAX_INFO_FRAMES, -1 }; -static const int Device_Properties_Proprietary[] = { - 9600, - -1 -}; +static const int Device_Properties_Proprietary[] = { 9600, -1 }; static struct my_object_functions *Device_Objects_Find_Functions( BACNET_OBJECT_TYPE Object_Type) @@ -140,8 +113,7 @@ static struct my_object_functions *Device_Objects_Find_Functions( } static int Read_Property_Common( - struct my_object_functions *pObject, - BACNET_READ_PROPERTY_DATA * rpdata) + struct my_object_functions *pObject, BACNET_READ_PROPERTY_DATA *rpdata) { int apdu_len = BACNET_STATUS_ERROR; BACNET_CHARACTER_STRING char_string; @@ -165,9 +137,8 @@ static int Read_Property_Common( if (rpdata->object_type == OBJECT_DEVICE) { rpdata->object_instance = Object_Instance_Number; } - apdu_len = - encode_application_object_id(&apdu[0], rpdata->object_type, - rpdata->object_instance); + apdu_len = encode_application_object_id( + &apdu[0], rpdata->object_type, rpdata->object_instance); } break; case PROP_OBJECT_NAME: @@ -179,12 +150,11 @@ static int Read_Property_Common( } else { characterstring_init_ansi(&char_string, ""); if (pObject->Object_Name) { - (void) pObject->Object_Name(rpdata->object_instance, - &char_string); + (void)pObject->Object_Name( + rpdata->object_instance, &char_string); } apdu_len = - encode_application_character_string(&apdu[0], - &char_string); + encode_application_character_string(&apdu[0], &char_string); } break; case PROP_OBJECT_TYPE: @@ -194,9 +164,8 @@ static int Read_Property_Common( rpdata->error_code = ERROR_CODE_PROPERTY_IS_NOT_AN_ARRAY; apdu_len = BACNET_STATUS_ERROR; } else { - apdu_len = - encode_application_enumerated(&apdu[0], - rpdata->object_type); + apdu_len = encode_application_enumerated( + &apdu[0], rpdata->object_type); } break; default: @@ -211,8 +180,7 @@ static int Read_Property_Common( /* Encodes the property APDU and returns the length, or sets the error, and returns BACNET_STATUS_ERROR */ -int Device_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Device_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { int apdu_len = BACNET_STATUS_ERROR; struct my_object_functions *pObject = NULL; @@ -235,8 +203,7 @@ int Device_Read_Property( return apdu_len; } -bool Device_Write_Property( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data) { bool status = false; struct my_object_functions *pObject = NULL; @@ -265,8 +232,7 @@ bool Device_Write_Property( } /* for a given object type, returns the special property list */ -void Device_Objects_Property_List( - BACNET_OBJECT_TYPE object_type, +void Device_Objects_Property_List(BACNET_OBJECT_TYPE object_type, uint32_t object_instance, struct special_property_list_t *pPropertyList) { @@ -289,25 +255,23 @@ void Device_Objects_Property_List( } /* Fetch the counts if available otherwise zero them */ - pPropertyList->Required.count = - pPropertyList->Required.pList == - NULL ? 0 : property_list_count(pPropertyList->Required.pList); + pPropertyList->Required.count = pPropertyList->Required.pList == NULL + ? 0 + : property_list_count(pPropertyList->Required.pList); - pPropertyList->Optional.count = - pPropertyList->Optional.pList == - NULL ? 0 : property_list_count(pPropertyList->Optional.pList); + pPropertyList->Optional.count = pPropertyList->Optional.pList == NULL + ? 0 + : property_list_count(pPropertyList->Optional.pList); - pPropertyList->Proprietary.count = - pPropertyList->Proprietary.pList == - NULL ? 0 : property_list_count(pPropertyList->Proprietary.pList); + pPropertyList->Proprietary.count = pPropertyList->Proprietary.pList == NULL + ? 0 + : property_list_count(pPropertyList->Proprietary.pList); return; } void Device_Property_Lists( - const int **pRequired, - const int **pOptional, - const int **pProprietary) + const int **pRequired, const int **pOptional, const int **pProprietary) { if (pRequired) *pRequired = Device_Properties_Required; @@ -319,22 +283,19 @@ void Device_Property_Lists( return; } -unsigned Device_Count( - void) +unsigned Device_Count(void) { return 1; } -uint32_t Device_Index_To_Instance( - unsigned index) +uint32_t Device_Index_To_Instance(unsigned index) { (void)index; return Object_Instance_Number; } bool Device_Object_Name( - uint32_t object_instance, - BACNET_CHARACTER_STRING * object_name) + uint32_t object_instance, BACNET_CHARACTER_STRING *object_name) { bool status = false; @@ -345,10 +306,9 @@ bool Device_Object_Name( return status; } -bool Device_Set_Object_Name( - BACNET_CHARACTER_STRING * object_name) +bool Device_Set_Object_Name(BACNET_CHARACTER_STRING *object_name) { - bool status = false; /*return value */ + bool status = false; /*return value */ if (!characterstring_same(&My_Object_Name, object_name)) { /* Make the change and update the database revision */ @@ -359,8 +319,7 @@ bool Device_Set_Object_Name( return status; } -bool Device_Reinitialize( - BACNET_REINITIALIZE_DEVICE_DATA * rd_data) +bool Device_Reinitialize(BACNET_REINITIALIZE_DEVICE_DATA *rd_data) { bool status = false; @@ -405,20 +364,18 @@ bool Device_Reinitialize( return status; } -BACNET_REINITIALIZED_STATE Device_Reinitialized_State( - void) +BACNET_REINITIALIZED_STATE Device_Reinitialized_State(void) { return Reinitialize_State; } -void Device_Init( - object_functions_t * object_table) +void Device_Init(object_functions_t *object_table) { struct my_object_functions *pObject = NULL; /* we don't use the object table passed in since there is extra stuff we don't need in there. */ - (void) object_table; + (void)object_table; /* our local object table */ pObject = &Object_Table[0]; while (pObject->Object_Type < MAX_BACNET_OBJECT_TYPE) { @@ -436,14 +393,12 @@ void Device_Init( } /* 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 */ @@ -455,21 +410,17 @@ bool Device_Set_Object_Instance_Number( return status; } -bool Device_Valid_Object_Instance_Number( - uint32_t object_id) +bool Device_Valid_Object_Instance_Number(uint32_t object_id) { return (Object_Instance_Number == object_id); } -BACNET_DEVICE_STATUS Device_System_Status( - void) +BACNET_DEVICE_STATUS Device_System_Status(void) { return System_Status; } -int Device_Set_System_Status( - BACNET_DEVICE_STATUS status, - bool local) +int Device_Set_System_Status(BACNET_DEVICE_STATUS status, bool local) { /*return value - 0 = ok, -1 = bad value, -2 = not allowed */ int result = -1; @@ -482,34 +433,29 @@ int Device_Set_System_Status( return result; } -uint16_t Device_Vendor_Identifier( - void) +uint16_t Device_Vendor_Identifier(void) { return BACNET_VENDOR_ID; } -BACNET_SEGMENTATION Device_Segmentation_Supported( - void) +BACNET_SEGMENTATION Device_Segmentation_Supported(void) { return SEGMENTATION_NONE; } -uint32_t Device_Database_Revision( - void) +uint32_t Device_Database_Revision(void) { return Database_Revision; } -void Device_Inc_Database_Revision( - void) +void Device_Inc_Database_Revision(void) { Database_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 = 0; /* number of objects */ struct my_object_functions *pObject = NULL; @@ -527,9 +473,7 @@ unsigned Device_Object_List_Count( } bool Device_Object_List_Identifier( - uint32_t array_index, - BACNET_OBJECT_TYPE *object_type, - uint32_t * instance) + uint32_t array_index, BACNET_OBJECT_TYPE *object_type, uint32_t *instance) { bool status = false; uint32_t count = 0; @@ -560,10 +504,9 @@ bool Device_Object_List_Identifier( return status; } -bool Device_Valid_Object_Name( - BACNET_CHARACTER_STRING * object_name1, +bool Device_Valid_Object_Name(BACNET_CHARACTER_STRING *object_name1, BACNET_OBJECT_TYPE *object_type, - uint32_t * object_instance) + uint32_t *object_instance) { bool found = false; BACNET_OBJECT_TYPE type = OBJECT_NONE; @@ -577,7 +520,7 @@ bool Device_Valid_Object_Name( for (i = 1; i <= max_objects; i++) { check_id = Device_Object_List_Identifier(i, &type, &instance); if (check_id) { - pObject = Device_Objects_Find_Functions((BACNET_OBJECT_TYPE) type); + pObject = Device_Objects_Find_Functions((BACNET_OBJECT_TYPE)type); if ((pObject != NULL) && (pObject->Object_Name != NULL) && (pObject->Object_Name(instance, &object_name2) && characterstring_same(object_name1, &object_name2))) { @@ -597,13 +540,12 @@ bool Device_Valid_Object_Name( } bool Device_Valid_Object_Id( - BACNET_OBJECT_TYPE object_type, - uint32_t object_instance) + BACNET_OBJECT_TYPE object_type, uint32_t object_instance) { - bool status = false; /* return value */ + bool status = false; /* return value */ struct my_object_functions *pObject = NULL; - pObject = Device_Objects_Find_Functions((BACNET_OBJECT_TYPE) object_type); + pObject = Device_Objects_Find_Functions((BACNET_OBJECT_TYPE)object_type); if ((pObject != NULL) && (pObject->Object_Valid_Instance != NULL)) { status = pObject->Object_Valid_Instance(object_instance); } @@ -611,10 +553,9 @@ bool Device_Valid_Object_Id( return status; } -bool Device_Object_Name_Copy( - BACNET_OBJECT_TYPE object_type, +bool Device_Object_Name_Copy(BACNET_OBJECT_TYPE object_type, uint32_t object_instance, - BACNET_CHARACTER_STRING * object_name) + BACNET_CHARACTER_STRING *object_name) { struct my_object_functions *pObject = NULL; bool found = false; @@ -628,11 +569,10 @@ bool Device_Object_Name_Copy( } /* return the length of the apdu encoded or BACNET_STATUS_ERROR for error */ -int Device_Read_Property_Local( - BACNET_READ_PROPERTY_DATA * rpdata) +int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA *rpdata) { - 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; uint32_t i = 0; @@ -660,8 +600,7 @@ int Device_Read_Property_Local( break; case PROP_SYSTEM_STATUS: apdu_len = - encode_application_enumerated(&apdu[0], - Device_System_Status()); + encode_application_enumerated(&apdu[0], Device_System_Status()); break; case PROP_VENDOR_NAME: characterstring_init_ansi(&char_string, BACNET_VENDOR_NAME); @@ -692,16 +631,15 @@ int Device_Read_Property_Local( break; case PROP_PROTOCOL_REVISION: apdu_len = - encode_application_unsigned(&apdu[0], - BACNET_PROTOCOL_REVISION); + encode_application_unsigned(&apdu[0], BACNET_PROTOCOL_REVISION); 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)); + 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; @@ -711,7 +649,7 @@ int Device_Read_Property_Local( 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); + bitstring_set_bit(&bit_string, (uint8_t)i, false); } /* set the object types with objects to supported */ i = 0; @@ -735,11 +673,10 @@ int Device_Read_Property_Local( /* your maximum APDU size. */ else if (rpdata->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); + 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? */ @@ -759,11 +696,10 @@ int Device_Read_Property_Local( } } } else { - if (Device_Object_List_Identifier(rpdata->array_index, - &object_type, &instance)) - apdu_len = - encode_application_object_id(&apdu[0], object_type, - instance); + if (Device_Object_List_Identifier( + rpdata->array_index, &object_type, &instance)) + apdu_len = encode_application_object_id( + &apdu[0], object_type, instance); else { rpdata->error_class = ERROR_CLASS_PROPERTY; rpdata->error_code = ERROR_CODE_INVALID_ARRAY_INDEX; @@ -775,9 +711,8 @@ int Device_Read_Property_Local( apdu_len = encode_application_unsigned(&apdu[0], MAX_APDU); break; case PROP_SEGMENTATION_SUPPORTED: - apdu_len = - encode_application_enumerated(&apdu[0], - Device_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()); @@ -789,22 +724,19 @@ int Device_Read_Property_Local( /* FIXME: encode the list here, if it exists */ break; case PROP_DATABASE_REVISION: - apdu_len = - encode_application_unsigned(&apdu[0], - Device_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()); + 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_baud_rate()); + apdu_len = encode_application_unsigned(&apdu[0], rs485_baud_rate()); break; default: rpdata->error_class = ERROR_CLASS_PROPERTY; @@ -823,19 +755,17 @@ int Device_Read_Property_Local( return apdu_len; } -bool Device_Write_Property_Local( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Device_Write_Property_Local(BACNET_WRITE_PROPERTY_DATA *wp_data) { - bool status = false; /* return value - false=error */ + bool status = false; /* return value - false=error */ int len = 0; uint8_t encoding = 0; size_t length = 0; BACNET_APPLICATION_DATA_VALUE value; /* decode the some of the request */ - len = - bacapp_decode_application_data(wp_data->application_data, - wp_data->application_data_len, &value); + len = bacapp_decode_application_data( + wp_data->application_data, wp_data->application_data_len, &value); /* FIXME: len < application_data_len: more data? */ if (len < 0) { /* error while decoding - a value larger than we can handle */ @@ -854,8 +784,8 @@ bool Device_Write_Property_Local( 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))) { + (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 { @@ -907,13 +837,13 @@ bool Device_Write_Property_Local( characterstring_encoding(&value.type.Character_String); if (encoding < MAX_CHARACTER_STRING_ENCODING) { /* All the object names in a device must be unique. */ - if (Device_Valid_Object_Name(&value.type. - Character_String, NULL, NULL)) { + if (Device_Valid_Object_Name( + &value.type.Character_String, NULL, NULL)) { wp_data->error_class = ERROR_CLASS_PROPERTY; wp_data->error_code = ERROR_CODE_DUPLICATE_NAME; } else { - Device_Set_Object_Name(&value.type. - Character_String); + Device_Set_Object_Name( + &value.type.Character_String); status = true; } } else { @@ -923,8 +853,7 @@ bool Device_Write_Property_Local( } } else { wp_data->error_class = ERROR_CLASS_PROPERTY; - wp_data->error_code = - ERROR_CODE_NO_SPACE_TO_WRITE_PROPERTY; + wp_data->error_code = ERROR_CODE_NO_SPACE_TO_WRITE_PROPERTY; } } else { wp_data->error_class = ERROR_CLASS_PROPERTY; diff --git a/ports/stm32f10x/dlmstp.c b/ports/stm32f10x/dlmstp.c index 58830d60..f898e946 100644 --- a/ports/stm32f10x/dlmstp.c +++ b/ports/stm32f10x/dlmstp.c @@ -64,19 +64,19 @@ static struct mstp_flag_t { /* A Boolean flag set to TRUE by the Receive State Machine */ /* if an invalid frame is received. */ /* Set to FALSE by the main state machine. */ - unsigned ReceivedInvalidFrame:1; + unsigned ReceivedInvalidFrame : 1; /* A Boolean flag set to TRUE by the Receive State Machine */ /* if a valid frame is received. */ /* Set to FALSE by the main state machine. */ - unsigned ReceivedValidFrame:1; + unsigned ReceivedValidFrame : 1; /* set to TRUE when we get a frame not for us */ - unsigned ReceivedValidFrameNotForUs:1; + unsigned ReceivedValidFrameNotForUs : 1; /* A Boolean flag set to TRUE by the master machine if this node is the */ /* only known master node. */ - unsigned SoleMaster:1; + unsigned SoleMaster : 1; /* A Boolean flag set TRUE by the datalink if a packet has been received, but not processed. */ - unsigned ReceivePacketPending:1; + unsigned ReceivePacketPending : 1; } MSTP_Flag; /* Used to store the data length of a received frame. */ @@ -143,7 +143,7 @@ static uint8_t Nmax_master = 127; /* The maximum time a node may wait after reception of a frame that expects */ /* a reply before sending the first octet of a reply or Reply Postponed */ /* frame: 250 milliseconds. */ -#define Treply_delay (250-50) +#define Treply_delay (250 - 50) /* The width of the time slot within which a node may generate a token: */ /* 10 milliseconds. */ @@ -155,7 +155,11 @@ static uint8_t Nmax_master = 127; #define Tusage_delay 15 /* we need to be able to increment without rolling over */ -#define INCREMENT_AND_LIMIT_UINT8(x) {if (x < 0xFF) x++;} +#define INCREMENT_AND_LIMIT_UINT8(x) \ + { \ + if (x < 0xFF) \ + x++; \ + } /* data structure for MS/TP transmit packet */ struct mstp_tx_packet { @@ -192,16 +196,14 @@ static RING_BUFFER PDU_Queue; /* node, its value shall be 1. */ static uint8_t Nmax_info_frames = MSTP_PDU_PACKET_COUNT; -void dlmstp_automac_hander( - void); +void dlmstp_automac_hander(void); -bool dlmstp_init( - char *ifname) +bool dlmstp_init(char *ifname) { (void)ifname; - Ringbuf_Init(&Transmit_Queue, (uint8_t *) Transmit_Buffer, + Ringbuf_Init(&Transmit_Queue, (uint8_t *)Transmit_Buffer, sizeof(struct mstp_tx_packet), MSTP_TRANSMIT_PACKET_COUNT); - Ringbuf_Init(&PDU_Queue, (uint8_t *) & PDU_Buffer, + Ringbuf_Init(&PDU_Queue, (uint8_t *)&PDU_Buffer, sizeof(struct mstp_pdu_packet), MSTP_PDU_PACKET_COUNT); rs485_init(); automac_init(); @@ -209,15 +211,12 @@ bool dlmstp_init( 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; @@ -240,11 +239,10 @@ void dlmstp_fill_bacnet_address( } } -static bool dlmstp_compare_data_expecting_reply( - uint8_t * request_pdu, +static bool dlmstp_compare_data_expecting_reply(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) { @@ -265,9 +263,8 @@ 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], NULL, &request.address, - &request.npdu_data); + offset = npdu_decode( + &request_pdu[0], NULL, &request.address, &request.npdu_data); if (request.npdu_data.network_layer_message) { return false; } @@ -284,8 +281,7 @@ static bool dlmstp_compare_data_expecting_reply( /* decode the reply data */ reply.address.mac[0] = dest_address; reply.address.mac_len = 1; - offset = - npdu_decode(&reply_pdu[0], &reply.address, NULL, &reply.npdu_data); + offset = npdu_decode(&reply_pdu[0], &reply.address, NULL, &reply.npdu_data); if (reply.npdu_data.network_layer_message) { return false; } @@ -326,7 +322,8 @@ static bool dlmstp_compare_data_expecting_reply( return false; } } - if (request.npdu_data.protocol_version != reply.npdu_data.protocol_version) { + if (request.npdu_data.protocol_version != + reply.npdu_data.protocol_version) { return false; } #if 0 @@ -349,19 +346,17 @@ typedef enum { MSTP_TX_STATE_SEND_WAIT, MSTP_TX_STATE_STOP } MSTP_TX_STATE; -static bool MSTP_Transmit_FSM( - void) +static bool MSTP_Transmit_FSM(void) { static MSTP_TX_STATE state = MSTP_TX_STATE_IDLE; static struct mstp_tx_packet *pkt; - MSTP_TX_START: +MSTP_TX_START: switch (state) { case MSTP_TX_STATE_IDLE: if (!Ringbuf_Empty(&Transmit_Queue)) { /* get the packet - but don't remove it from queue */ - pkt = (struct mstp_tx_packet *) - Ringbuf_Peek(&Transmit_Queue); + pkt = (struct mstp_tx_packet *)Ringbuf_Peek(&Transmit_Queue); state = MSTP_TX_STATE_SILENCE_WAIT; } break; @@ -392,7 +387,7 @@ static bool MSTP_Transmit_FSM( if (rs485_byte_sent() && rs485_frame_sent()) { rs485_rts_enable(false); /* remove the packet from the queue */ - (void) Ringbuf_Pop(&Transmit_Queue, NULL); + (void)Ringbuf_Pop(&Transmit_Queue, NULL); state = MSTP_TX_STATE_IDLE; } break; @@ -419,17 +414,17 @@ static bool MSTP_Transmit_FSM( /* (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 destination, /* destination address */ - uint8_t source, /* source address */ - uint8_t * data, /* any data to be sent - may be null */ + 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) -{ /* 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 */ +{ /* 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 */ struct mstp_tx_packet *pkt; - uint16_t i = 0; /* used to calculate CRC for data */ + uint16_t i = 0; /* used to calculate CRC for data */ - pkt = (struct mstp_tx_packet *) Ringbuf_Data_Peek(&Transmit_Queue); + pkt = (struct mstp_tx_packet *)Ringbuf_Data_Peek(&Transmit_Queue); if (pkt) { /* create the MS/TP header */ pkt->buffer[0] = 0x55; @@ -464,8 +459,7 @@ static void MSTP_Send_Frame( } } -static void MSTP_Receive_Frame_FSM( - void) +static void MSTP_Receive_Frame_FSM(void) { /* stores the latest received data octet */ uint8_t DataRegister = 0; @@ -701,8 +695,7 @@ static void MSTP_Receive_Frame_FSM( } /* 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 */ @@ -832,9 +825,8 @@ static bool MSTP_Master_Node_FSM( } break; case FRAME_TYPE_TEST_REQUEST: - MSTP_Send_Frame(FRAME_TYPE_TEST_RESPONSE, - SourceAddress, This_Station, &InputBuffer[0], - DataLength); + MSTP_Send_Frame(FRAME_TYPE_TEST_RESPONSE, SourceAddress, + This_Station, &InputBuffer[0], DataLength); break; case FRAME_TYPE_TEST_RESPONSE: default: @@ -859,14 +851,14 @@ static bool MSTP_Master_Node_FSM( transition_now = true; } else { uint8_t frame_type; - pkt = (struct mstp_pdu_packet *) Ringbuf_Peek(&PDU_Queue); + pkt = (struct mstp_pdu_packet *)Ringbuf_Peek(&PDU_Queue); if (pkt->data_expecting_reply) { frame_type = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY; } else { frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; } MSTP_Send_Frame(frame_type, pkt->destination_mac, This_Station, - (uint8_t *) & pkt->buffer[0], pkt->length); + (uint8_t *)&pkt->buffer[0], pkt->length); FrameCount++; switch (frame_type) { case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: @@ -886,7 +878,7 @@ static bool MSTP_Master_Node_FSM( Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; break; } - (void) Ringbuf_Pop(&PDU_Queue, NULL); + (void)Ringbuf_Pop(&PDU_Queue, NULL); } break; case MSTP_MASTER_STATE_WAIT_FOR_REPLY: @@ -998,8 +990,8 @@ static bool MSTP_Master_Node_FSM( 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; @@ -1013,7 +1005,7 @@ static bool MSTP_Master_Node_FSM( /* no known successor node */ Next_Station = This_Station; RetryCount = 0; - TokenCount = 1; /* changed in Errata SSPC-135-2004 */ + 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; @@ -1021,10 +1013,10 @@ static bool MSTP_Master_Node_FSM( /* ResetMaintenancePFM */ Poll_Station = This_Station; /* 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; - TokenCount = 1; /* changed in Errata SSPC-135-2004 */ + TokenCount = 1; /* changed in Errata SSPC-135-2004 */ EventCount = 0; Master_State = MSTP_MASTER_STATE_PASS_TOKEN; } @@ -1045,8 +1037,8 @@ static bool MSTP_Master_Node_FSM( /* RetrySendToken */ RetryCount++; /* 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); EventCount = 0; /* re-enter the current state to listen for NS */ /* to begin using the token. */ @@ -1123,15 +1115,15 @@ static bool MSTP_Master_Node_FSM( /* 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)) { + 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); + MSTP_Send_Frame( + FRAME_TYPE_TOKEN, Next_Station, This_Station, NULL, 0); Poll_Station = This_Station; TokenCount = 0; RetryCount = 0; @@ -1194,10 +1186,9 @@ static bool MSTP_Master_Node_FSM( /* BACnet Data Expecting Reply, a Test_Request, or */ /* a proprietary frame that expects a reply is received. */ case MSTP_MASTER_STATE_ANSWER_DATA_REQUEST: - pkt = (struct mstp_pdu_packet *) Ringbuf_Peek(&PDU_Queue); + pkt = (struct mstp_pdu_packet *)Ringbuf_Peek(&PDU_Queue); if (pkt != NULL) { - matched = - dlmstp_compare_data_expecting_reply(&InputBuffer[0], + matched = dlmstp_compare_data_expecting_reply(&InputBuffer[0], DataLength, SourceAddress, &pkt->buffer[0], pkt->length, pkt->destination_mac); } else { @@ -1218,12 +1209,12 @@ static bool MSTP_Master_Node_FSM( frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; } MSTP_Send_Frame(frame_type, pkt->destination_mac, This_Station, - (uint8_t *) & pkt->buffer[0], pkt->length); + (uint8_t *)&pkt->buffer[0], pkt->length); Master_State = MSTP_MASTER_STATE_IDLE; /* clear our flag we were holding for comparison */ MSTP_Flag.ReceivedValidFrame = false; /* clear the queue */ - (void) Ringbuf_Pop(&PDU_Queue, NULL); + (void)Ringbuf_Pop(&PDU_Queue, NULL); } else if (rs485_silence_elapsed(Treply_delay) || (pkt != NULL)) { /* DeferredReply */ /* If no reply will be available from the higher layers */ @@ -1250,17 +1241,16 @@ static bool MSTP_Master_Node_FSM( } /* 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 */ +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 */ +{ /* number of bytes of data */ int bytes_sent = 0; struct mstp_pdu_packet *pkt; uint16_t i = 0; - pkt = (struct mstp_pdu_packet *) Ringbuf_Data_Peek(&PDU_Queue); + pkt = (struct mstp_pdu_packet *)Ringbuf_Data_Peek(&PDU_Queue); if (pkt) { pkt->data_expecting_reply = npdu_data->data_expecting_reply; for (i = 0; i < pdu_len; i++) { @@ -1302,8 +1292,7 @@ typedef enum { } AUTOMAC_STATE; /* buffer used to send and validate a response - size is min APDU size */ static uint8_t AutoMAC_Test_Buffer[50]; -void dlmstp_automac_hander( - void) +void dlmstp_automac_hander(void) { static AUTOMAC_STATE state = AUTOMAC_STATE_IDLE; uint8_t mac = 0; @@ -1363,8 +1352,7 @@ void dlmstp_automac_hander( automac_init(); state = AUTOMAC_STATE_IDLE; } else if (mac == DestinationAddress) { - MSTP_Send_Frame - (FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER, + MSTP_Send_Frame(FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER, SourceAddress, mac, NULL, 0); state = AUTOMAC_STATE_TOKEN; } @@ -1490,13 +1478,12 @@ void dlmstp_automac_hander( } /* 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 */ +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 */ +{ /* milliseconds to wait for a packet */ + uint16_t pdu_len = 0; /* return value */ bool transmitting = false; /* set the input buffer to the same data storage for zero copy */ @@ -1551,8 +1538,7 @@ 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) { @@ -1568,8 +1554,7 @@ void dlmstp_set_mac_address( return; } -uint8_t dlmstp_mac_address( - void) +uint8_t dlmstp_mac_address(void) { return This_Station; } @@ -1581,8 +1566,7 @@ uint8_t dlmstp_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( - uint8_t max_info_frames) +void dlmstp_set_max_info_frames(uint8_t max_info_frames) { if (max_info_frames >= MSTP_PDU_PACKET_COUNT) { Nmax_info_frames = max_info_frames; @@ -1591,8 +1575,7 @@ void dlmstp_set_max_info_frames( return; } -uint8_t dlmstp_max_info_frames( - void) +uint8_t dlmstp_max_info_frames(void) { return Nmax_info_frames; } @@ -1602,8 +1585,7 @@ uint8_t dlmstp_max_info_frames( /* 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 == 255) || (This_Station <= max_master)) { @@ -1614,20 +1596,18 @@ void dlmstp_set_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; - my_address->net = 0; /* local only, no routing */ + my_address->net = 0; /* local only, no routing */ my_address->len = 0; for (i = 0; i < MAX_MAC_LEN; i++) { my_address->adr[i] = 0; @@ -1636,16 +1616,15 @@ void dlmstp_get_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; } @@ -1654,8 +1633,7 @@ void dlmstp_get_broadcast_address( return; } -bool dlmstp_sole_master( - void) +bool dlmstp_sole_master(void) { if (MSTP_Flag.SoleMaster) { return true; diff --git a/ports/stm32f10x/led.c b/ports/stm32f10x/led.c index 8e3a7e19..95d9cfd9 100644 --- a/ports/stm32f10x/led.c +++ b/ports/stm32f10x/led.c @@ -1,26 +1,26 @@ /************************************************************************** -* -* Copyright (C) 2011 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*********************************************************************/ + * + * Copyright (C) 2011 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + *********************************************************************/ #include #include "hardware.h" #include "bacnet/basic/sys/mstimer.h" @@ -33,12 +33,11 @@ static bool Tx_State; static bool LD3_State; /************************************************************************* -* Description: Activate the LED -* Returns: nothing -* Notes: none -**************************************************************************/ -void led_tx_on( - void) + * Description: Activate the LED + * Returns: nothing + * Notes: none + **************************************************************************/ +void led_tx_on(void) { GPIO_WriteBit(GPIOB, GPIO_Pin_15, Bit_SET); mstimer_set(&Off_Delay_Timer_Tx, 0); @@ -46,12 +45,11 @@ void led_tx_on( } /************************************************************************* -* Description: Activate the LED -* Returns: nothing -* Notes: none -**************************************************************************/ -void led_rx_on( - void) + * Description: Activate the LED + * Returns: nothing + * Notes: none + **************************************************************************/ +void led_rx_on(void) { GPIO_WriteBit(GPIOB, GPIO_Pin_14, Bit_SET); mstimer_set(&Off_Delay_Timer_Rx, 0); @@ -59,12 +57,11 @@ void led_rx_on( } /************************************************************************* -* Description: Deactivate the LED -* Returns: nothing -* Notes: none -**************************************************************************/ -void led_tx_off( - void) + * Description: Deactivate the LED + * Returns: nothing + * Notes: none + **************************************************************************/ +void led_tx_off(void) { GPIO_WriteBit(GPIOB, GPIO_Pin_15, Bit_RESET); mstimer_set(&Off_Delay_Timer_Tx, 0); @@ -72,12 +69,11 @@ void led_tx_off( } /************************************************************************* -* Description: Deactivate the LED -* Returns: nothing -* Notes: none -**************************************************************************/ -void led_rx_off( - void) + * Description: Deactivate the LED + * Returns: nothing + * Notes: none + **************************************************************************/ +void led_rx_off(void) { GPIO_WriteBit(GPIOB, GPIO_Pin_14, Bit_RESET); mstimer_set(&Off_Delay_Timer_Rx, 0); @@ -85,34 +81,31 @@ void led_rx_off( } /************************************************************************* -* Description: Get the state of the LED -* Returns: true if on, false if off. -* Notes: none -*************************************************************************/ -bool led_rx_state( - void) + * Description: Get the state of the LED + * Returns: true if on, false if off. + * Notes: none + *************************************************************************/ +bool led_rx_state(void) { return Rx_State; } /************************************************************************* -* Description: Get the state of the LED -* Returns: true if on, false if off. -* Notes: none -*************************************************************************/ -bool led_tx_state( - void) + * Description: Get the state of the LED + * Returns: true if on, false if off. + * Notes: none + *************************************************************************/ +bool led_tx_state(void) { return Tx_State; } /************************************************************************* -* Description: Toggle the state of the LED -* Returns: none -* Notes: none -*************************************************************************/ -void led_tx_toggle( - void) + * Description: Toggle the state of the LED + * Returns: none + * Notes: none + *************************************************************************/ +void led_tx_toggle(void) { if (led_tx_state()) { led_tx_off(); @@ -122,12 +115,11 @@ void led_tx_toggle( } /************************************************************************* -* Description: Toggle the state of the LED -* Returns: none -* Notes: none -*************************************************************************/ -void led_rx_toggle( - void) + * Description: Toggle the state of the LED + * Returns: none + * Notes: none + *************************************************************************/ +void led_rx_toggle(void) { if (led_rx_state()) { led_rx_off(); @@ -137,58 +129,53 @@ void led_rx_toggle( } /************************************************************************* -* Description: Delay before going off to give minimum brightness. -* Returns: none -* Notes: none -*************************************************************************/ -void led_rx_off_delay( - uint32_t delay_ms) + * Description: Delay before going off to give minimum brightness. + * Returns: none + * Notes: none + *************************************************************************/ +void led_rx_off_delay(uint32_t delay_ms) { mstimer_set(&Off_Delay_Timer_Rx, delay_ms); } /************************************************************************* -* Description: Delay before going off to give minimum brightness. -* Returns: none -* Notes: none -*************************************************************************/ -void led_tx_off_delay( - uint32_t delay_ms) + * Description: Delay before going off to give minimum brightness. + * Returns: none + * Notes: none + *************************************************************************/ +void led_tx_off_delay(uint32_t delay_ms) { mstimer_set(&Off_Delay_Timer_Tx, delay_ms); } /************************************************************************* -* Description: Turn on, and delay before going off. -* Returns: none -* Notes: none -*************************************************************************/ -void led_rx_on_interval( - uint16_t interval_ms) + * Description: Turn on, and delay before going off. + * Returns: none + * Notes: none + *************************************************************************/ +void led_rx_on_interval(uint16_t interval_ms) { led_rx_on(); mstimer_set(&Off_Delay_Timer_Rx, interval_ms); } /************************************************************************* -* Description: Turn on, and delay before going off. -* Returns: none -* Notes: none -*************************************************************************/ -void led_tx_on_interval( - uint16_t interval_ms) + * Description: Turn on, and delay before going off. + * Returns: none + * Notes: none + *************************************************************************/ +void led_tx_on_interval(uint16_t interval_ms) { led_tx_on(); mstimer_set(&Off_Delay_Timer_Tx, interval_ms); } /************************************************************************* -* Description: Task for blinking LED -* Returns: none -* Notes: none -*************************************************************************/ -void led_task( - void) + * Description: Task for blinking LED + * Returns: none + * Notes: none + *************************************************************************/ +void led_task(void) { if (mstimer_expired(&Off_Delay_Timer_Rx)) { mstimer_set(&Off_Delay_Timer_Rx, 0); @@ -201,69 +188,63 @@ void led_task( } /************************************************************************* -* Description: Activate the LED -* Returns: nothing -* Notes: none -**************************************************************************/ -void led_ld4_on( - void) + * Description: Activate the LED + * Returns: nothing + * Notes: none + **************************************************************************/ +void led_ld4_on(void) { GPIO_WriteBit(GPIOC, GPIO_Pin_8, Bit_SET); } /************************************************************************* -* Description: Deactivate the LED -* Returns: nothing -* Notes: none -**************************************************************************/ -void led_ld4_off( - void) + * Description: Deactivate the LED + * Returns: nothing + * Notes: none + **************************************************************************/ +void led_ld4_off(void) { GPIO_WriteBit(GPIOC, GPIO_Pin_8, Bit_RESET); } /************************************************************************* -* Description: Activate the LED -* Returns: nothing -* Notes: none -**************************************************************************/ -void led_ld3_on( - void) + * Description: Activate the LED + * Returns: nothing + * Notes: none + **************************************************************************/ +void led_ld3_on(void) { GPIO_WriteBit(GPIOC, GPIO_Pin_9, Bit_SET); LD3_State = true; } /************************************************************************* -* Description: Deactivate the LED -* Returns: nothing -* Notes: none -**************************************************************************/ -void led_ld3_off( - void) + * Description: Deactivate the LED + * Returns: nothing + * Notes: none + **************************************************************************/ +void led_ld3_off(void) { GPIO_WriteBit(GPIOC, GPIO_Pin_9, Bit_RESET); LD3_State = false; } /************************************************************************* -* Description: Get the state of the LED -* Returns: true if on, false if off. -* Notes: none -*************************************************************************/ -bool led_ld3_state( - void) + * Description: Get the state of the LED + * Returns: true if on, false if off. + * Notes: none + *************************************************************************/ +bool led_ld3_state(void) { return LD3_State; } /************************************************************************* -* Description: Toggle the state of the LED -* Returns: none -* Notes: none -*************************************************************************/ -void led_ld3_toggle( - void) + * Description: Toggle the state of the LED + * Returns: none + * Notes: none + *************************************************************************/ +void led_ld3_toggle(void) { if (led_ld3_state()) { led_ld3_off(); @@ -273,12 +254,11 @@ void led_ld3_toggle( } /************************************************************************* -* Description: Initialize the LED hardware -* Returns: none -* Notes: none -*************************************************************************/ -void led_init( - void) + * Description: Initialize the LED hardware + * Returns: none + * Notes: none + *************************************************************************/ +void led_init(void) { GPIO_InitTypeDef GPIO_InitStructure; diff --git a/ports/stm32f10x/main.c b/ports/stm32f10x/main.c index fae8d3e0..9beb8f04 100644 --- a/ports/stm32f10x/main.c +++ b/ports/stm32f10x/main.c @@ -1,27 +1,27 @@ /************************************************************************ -* -* Copyright (C) 2011 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*************************************************************************/ + * + * Copyright (C) 2011 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *************************************************************************/ #include #include @@ -35,34 +35,31 @@ /* local version override */ char *BACnet_Version = "1.0"; -#ifdef USE_FULL_ASSERT +#ifdef USE_FULL_ASSERT /** - * @brief Reports the name of the source file and the source line number - * where the assert_param error has occurred. - * @param file: pointer to the source file name - * @param line: assert_param error line source number - * @retval None - */ -void assert_failed( - uint8_t * file, - uint32_t line) + * @brief Reports the name of the source file and the source line number + * where the assert_param error has occurred. + * @param file: pointer to the source file name + * @param line: assert_param error line source number + * @retval None + */ +void assert_failed(uint8_t *file, uint32_t line) { - /* User can add his own implementation to report the file name and line number, - ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ + /* User can add his own implementation to report the file name and line + number, ex: printf("Wrong parameters value: file %s on line %d\r\n", + file, line) */ /* Infinite loop */ - while (1) { - } + while (1) { } } #endif /* Private define ------------------------------------------------------------*/ -#define LSE_FAIL_FLAG 0x80 -#define LSE_PASS_FLAG 0x100 +#define LSE_FAIL_FLAG 0x80 +#define LSE_PASS_FLAG 0x100 -void lse_init( - void) +void lse_init(void) { uint32_t LSE_Delay = 0; struct mstimer Delay_Timer; @@ -76,7 +73,7 @@ void lse_init( while (1) { if (LSE_Delay < LSE_FAIL_FLAG) { mstimer_set(&Delay_Timer, 0); - while (mstimer_elapsed(&Delay_Timer) < 500) { + while (mstimer_elapsed(&Delay_Timer) < 500) { /* do nothing */ } /* check whether LSE is ready, with 4 seconds timeout */ @@ -105,8 +102,7 @@ void lse_init( } } -int main( - void) +int main(void) { struct mstimer Blink_Timer; @@ -118,7 +114,7 @@ int main( RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | - RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE, + RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE, ENABLE); mstimer_init(); lse_init(); diff --git a/ports/stm32f10x/mstimer-init.c b/ports/stm32f10x/mstimer-init.c index 61b67409..41184a72 100644 --- a/ports/stm32f10x/mstimer-init.c +++ b/ports/stm32f10x/mstimer-init.c @@ -1,30 +1,30 @@ /************************************************************************** -* -* Copyright (C) 2011 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -* Module Description: -* Generate a periodic timer tick for use by generic timers in the code. -* -*************************************************************************/ + * + * Copyright (C) 2011 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * Module Description: + * Generate a periodic timer tick for use by generic timers in the code. + * + *************************************************************************/ #include #include #include "hardware.h" @@ -38,8 +38,7 @@ static volatile struct mstimer_callback_data_t *Callback_Head; /** * Activate the LED */ -static void timer_debug_on( - void) +static void timer_debug_on(void) { GPIO_WriteBit(GPIOB, GPIO_Pin_13, Bit_SET); } @@ -47,8 +46,7 @@ static void timer_debug_on( /** * Deactivate the LED */ -static void timer_debug_off( - void) +static void timer_debug_off(void) { GPIO_WriteBit(GPIOB, GPIO_Pin_13, Bit_RESET); } @@ -56,8 +54,7 @@ static void timer_debug_off( /** * Toggle the state of the debug LED */ -static void timer_debug_toggle( - void) +static void timer_debug_toggle(void) { static bool state = false; @@ -73,8 +70,7 @@ static void timer_debug_toggle( /** * Handles the interrupt from the timer */ -void SysTick_Handler( - void) +void SysTick_Handler(void) { struct mstimer_callback_data_t *cb; @@ -98,8 +94,7 @@ void SysTick_Handler( * * @return the current milliseconds count */ -unsigned long mstimer_now( - void) +unsigned long mstimer_now(void) { return Millisecond_Counter; } @@ -113,8 +108,7 @@ unsigned long mstimer_now( * * @return true if successfully added and enabled */ -void mstimer_callback( - struct mstimer_callback_data_t *new_cb, +void mstimer_callback(struct mstimer_callback_data_t *new_cb, mstimer_callback_function callback, unsigned long milliseconds) { @@ -142,8 +136,7 @@ void mstimer_callback( /** * Timer setup for 1 millisecond timer */ -void mstimer_init( - void) +void mstimer_init(void) { GPIO_InitTypeDef GPIO_InitStructure; @@ -157,7 +150,7 @@ void mstimer_init( /* Setup SysTick Timer for 1ms interrupts */ if (SysTick_Config(SystemCoreClock / 1000)) { /* Capture error */ - while (1); + while (1) + ; } - } diff --git a/ports/stm32f10x/rs485.c b/ports/stm32f10x/rs485.c index ce5bcdbc..91d8148f 100644 --- a/ports/stm32f10x/rs485.c +++ b/ports/stm32f10x/rs485.c @@ -1,29 +1,29 @@ /************************************************************************** -* -* Copyright (C) 2011 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -* Module Description: -* Handle the configuration and operation of the RS485 bus. -**************************************************************************/ + * + * Copyright (C) 2011 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * Module Description: + * Handle the configuration and operation of the RS485 bus. + **************************************************************************/ #include #include #include @@ -51,37 +51,34 @@ static uint32_t Baud_Rate = 38400; /* At 76800 baud, 40 bit times would be about 0.520 milliseconds */ /* At 115200 baud, 40 bit times would be about 0.347 milliseconds */ /* 40 bits is 4 octets including a start and stop bit with each octet */ -#define Tturnaround (40UL) +#define Tturnaround (40UL) /************************************************************************* -* Description: Reset the silence on the wire timer. -* Returns: nothing -* Notes: none -**************************************************************************/ -void rs485_silence_reset( - void) + * Description: Reset the silence on the wire timer. + * Returns: nothing + * Notes: none + **************************************************************************/ +void rs485_silence_reset(void) { mstimer_set(&Silence_Timer, 0); } /************************************************************************* -* Description: Determine the amount of silence on the wire from the timer. -* Returns: true if the amount of time has elapsed -* Notes: none -**************************************************************************/ -bool rs485_silence_elapsed( - uint32_t interval) + * Description: Determine the amount of silence on the wire from the timer. + * Returns: true if the amount of time has elapsed + * Notes: none + **************************************************************************/ +bool rs485_silence_elapsed(uint32_t interval) { return (mstimer_elapsed(&Silence_Timer) > interval); } /************************************************************************* -* Description: Baud rate determines turnaround time. -* Returns: amount of milliseconds -* Notes: none -**************************************************************************/ -static uint16_t rs485_turnaround_time( - void) + * Description: Baud rate determines turnaround time. + * Returns: amount of milliseconds + * Notes: none + **************************************************************************/ +static uint16_t rs485_turnaround_time(void) { /* delay after reception before transmitting - per MS/TP spec */ /* wait a minimum 40 bit times since reception */ @@ -94,55 +91,55 @@ static uint16_t rs485_turnaround_time( } /************************************************************************* -* Description: Use the silence timer to determine turnaround time. -* Returns: true if turnaround time has expired. -* Notes: none -**************************************************************************/ -bool rs485_turnaround_elapsed( - void) + * Description: Use the silence timer to determine turnaround time. + * Returns: true if turnaround time has expired. + * Notes: none + **************************************************************************/ +bool rs485_turnaround_elapsed(void) { return (mstimer_elapsed(&Silence_Timer) > rs485_turnaround_time()); } - /************************************************************************* -* Description: Determines if an error occured while receiving -* Returns: true an error occurred. -* Notes: none -**************************************************************************/ -bool rs485_receive_error( - void) + * Description: Determines if an error occured while receiving + * Returns: true an error occurred. + * Notes: none + **************************************************************************/ +bool rs485_receive_error(void) { return false; } -/*********************************************************************//** - * @brief USARTx interrupt handler sub-routine - * @param[in] None - * @return None - **********************************************************************/ -void USART2_IRQHandler( - void) +/*********************************************************************/ /** + * @brief + *USARTx + *interrupt + *handler + *sub-routine + * @param[in] + *None + * @return + *None + **********************************************************************/ +void USART2_IRQHandler(void) { uint8_t data_byte; - if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) { /* Read one byte from the receive data register */ data_byte = USART_ReceiveData(USART2); - (void) FIFO_Put(&Receive_Buffer, data_byte); + (void)FIFO_Put(&Receive_Buffer, data_byte); } } /************************************************************************* -* DESCRIPTION: Return true if a byte is available -* RETURN: true if a byte is available, with the byte in the parameter -* NOTES: none -**************************************************************************/ -bool rs485_byte_available( - uint8_t * data_register) + * DESCRIPTION: Return true if a byte is available + * RETURN: true if a byte is available, with the byte in the parameter + * NOTES: none + **************************************************************************/ +bool rs485_byte_available(uint8_t *data_register) { - bool data_available = false; /* return value */ + bool data_available = false; /* return value */ if (!FIFO_Empty(&Receive_Buffer)) { if (data_register) { @@ -157,12 +154,11 @@ bool rs485_byte_available( } /************************************************************************* -* DESCRIPTION: Sends a byte of data -* RETURN: nothing -* NOTES: none -**************************************************************************/ -void rs485_byte_send( - uint8_t tx_byte) + * DESCRIPTION: Sends a byte of data + * RETURN: nothing + * NOTES: none + **************************************************************************/ +void rs485_byte_send(uint8_t tx_byte) { led_tx_on_interval(10); USART_SendData(USART2, tx_byte); @@ -170,37 +166,34 @@ void rs485_byte_send( } /************************************************************************* -* Description: Determines if a byte in the USART has been shifted from -* register -* Returns: true if the USART register is empty -* Notes: none -**************************************************************************/ -bool rs485_byte_sent( - void) + * Description: Determines if a byte in the USART has been shifted from + * register + * Returns: true if the USART register is empty + * Notes: none + **************************************************************************/ +bool rs485_byte_sent(void) { return USART_GetFlagStatus(USART2, USART_FLAG_TXE); } /************************************************************************* -* Description: Determines if the entire frame is sent from USART FIFO -* Returns: true if the USART FIFO is empty -* Notes: none -**************************************************************************/ -bool rs485_frame_sent( - void) + * Description: Determines if the entire frame is sent from USART FIFO + * Returns: true if the USART FIFO is empty + * Notes: none + **************************************************************************/ +bool rs485_frame_sent(void) { return USART_GetFlagStatus(USART2, USART_FLAG_TC); } /************************************************************************* -* DESCRIPTION: Send some data and wait until it is sent -* RETURN: true if a collision or timeout occurred -* NOTES: none -**************************************************************************/ -void rs485_bytes_send( - uint8_t * buffer, /* data to send */ + * DESCRIPTION: Send some data and wait until it is sent + * RETURN: true if a collision or timeout occurred + * NOTES: none + **************************************************************************/ +void rs485_bytes_send(uint8_t *buffer, /* data to send */ uint16_t nbytes) -{ /* number of bytes of data */ +{ /* number of bytes of data */ uint8_t tx_byte; while (nbytes) { @@ -225,12 +218,11 @@ void rs485_bytes_send( } /************************************************************************* -* Description: Configures the baud rate of the USART -* Returns: nothing -* Notes: none -**************************************************************************/ -static void rs485_baud_rate_configure( - void) + * Description: Configures the baud rate of the USART + * Returns: nothing + * Notes: none + **************************************************************************/ +static void rs485_baud_rate_configure(void) { USART_InitTypeDef USART_InitStructure; @@ -247,12 +239,11 @@ static void rs485_baud_rate_configure( } /************************************************************************* -* Description: Sets the baud rate to non-volatile storeage and configures USART -* Returns: true if a value baud rate was saved -* Notes: none -**************************************************************************/ -bool rs485_baud_rate_set( - uint32_t baud) + * Description: Sets the baud rate to non-volatile storeage and configures USART + * Returns: true if a value baud rate was saved + * Notes: none + **************************************************************************/ +bool rs485_baud_rate_set(uint32_t baud) { bool valid = true; @@ -275,23 +266,21 @@ bool rs485_baud_rate_set( } /************************************************************************* -* Description: Determines the baud rate in bps -* Returns: baud rate in bps -* Notes: none -**************************************************************************/ -uint32_t rs485_baud_rate( - void) + * Description: Determines the baud rate in bps + * Returns: baud rate in bps + * Notes: none + **************************************************************************/ +uint32_t rs485_baud_rate(void) { return Baud_Rate; } /************************************************************************* -* Description: Enable the Request To Send (RTS) aka Transmit Enable pin -* Returns: nothing -* Notes: none -**************************************************************************/ -void rs485_rts_enable( - bool enable) + * Description: Enable the Request To Send (RTS) aka Transmit Enable pin + * Returns: nothing + * Notes: none + **************************************************************************/ +void rs485_rts_enable(bool enable) { if (enable) { GPIO_WriteBit(GPIOA, GPIO_Pin_1, Bit_SET); @@ -301,12 +290,11 @@ void rs485_rts_enable( } /************************************************************************* -* Description: Initialize the room network USART -* Returns: nothing -* Notes: none -**************************************************************************/ -void rs485_init( - void) + * Description: Initialize the room network USART + * Returns: nothing + * Notes: none + **************************************************************************/ +void rs485_init(void) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; @@ -351,6 +339,6 @@ void rs485_init( USART_Cmd(USART2, ENABLE); FIFO_Init(&Receive_Buffer, &Receive_Buffer_Data[0], - (unsigned) sizeof(Receive_Buffer_Data)); + (unsigned)sizeof(Receive_Buffer_Data)); rs485_silence_reset(); } diff --git a/ports/stm32f10x/stm32f10x_it.c b/ports/stm32f10x/stm32f10x_it.c index 48a829d4..5777fcf3 100644 --- a/ports/stm32f10x/stm32f10x_it.c +++ b/ports/stm32f10x/stm32f10x_it.c @@ -1,35 +1,35 @@ /** - ****************************************************************************** - * @file I2C/EEPROM/stm32f10x_it.c - * @author MCD Application Team - * @version V3.4.0 - * @date 10/15/2010 - * @brief Main Interrupt Service Routines. - * This file provides template for all exceptions handler and - * peripherals interrupt service routine. - ****************************************************************************** - * @copy - * - * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS - * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE - * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY - * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING - * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE - * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. - * - *

© COPYRIGHT 2010 STMicroelectronics

- */ + ****************************************************************************** + * @file I2C/EEPROM/stm32f10x_it.c + * @author MCD Application Team + * @version V3.4.0 + * @date 10/15/2010 + * @brief Main Interrupt Service Routines. + * This file provides template for all exceptions handler and + * peripherals interrupt service routine. + ****************************************************************************** + * @copy + * + * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS + * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE + * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY + * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING + * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE + * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. + * + *

© COPYRIGHT 2010 STMicroelectronics

+ */ /* Includes ------------------------------------------------------------------*/ #include "stm32f10x_it.h" /** @addtogroup STM32F10x_StdPeriph_Examples - * @{ - */ + * @{ + */ /** @addtogroup I2C_EEPROM - * @{ - */ + * @{ + */ /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ @@ -43,12 +43,11 @@ /******************************************************************************/ /** - * @brief This function handles NMI exception. - * @param None - * @retval None - */ -void NMI_Handler( - void) + * @brief This function handles NMI exception. + * @param None + * @retval None + */ +void NMI_Handler(void) { } @@ -57,26 +56,26 @@ void NMI_Handler( // hard fault handler in C, // with stack frame location as input parameter // called from HardFault_Handler in file xxx.s -void hard_fault_handler_c (unsigned int * hardfault_args) +void hard_fault_handler_c(unsigned int *hardfault_args) { - unsigned int stacked_r0; - unsigned int stacked_r1; - unsigned int stacked_r2; - unsigned int stacked_r3; - unsigned int stacked_r12; - unsigned int stacked_lr; - unsigned int stacked_pc; - unsigned int stacked_psr; + unsigned int stacked_r0; + unsigned int stacked_r1; + unsigned int stacked_r2; + unsigned int stacked_r3; + unsigned int stacked_r12; + unsigned int stacked_lr; + unsigned int stacked_pc; + unsigned int stacked_psr; - stacked_r0 = ((unsigned long) hardfault_args[0]); - stacked_r1 = ((unsigned long) hardfault_args[1]); - stacked_r2 = ((unsigned long) hardfault_args[2]); - stacked_r3 = ((unsigned long) hardfault_args[3]); + stacked_r0 = ((unsigned long)hardfault_args[0]); + stacked_r1 = ((unsigned long)hardfault_args[1]); + stacked_r2 = ((unsigned long)hardfault_args[2]); + stacked_r3 = ((unsigned long)hardfault_args[3]); - stacked_r12 = ((unsigned long) hardfault_args[4]); - stacked_lr = ((unsigned long) hardfault_args[5]); - stacked_pc = ((unsigned long) hardfault_args[6]); - stacked_psr = ((unsigned long) hardfault_args[7]); + stacked_r12 = ((unsigned long)hardfault_args[4]); + stacked_lr = ((unsigned long)hardfault_args[5]); + stacked_pc = ((unsigned long)hardfault_args[6]); + stacked_psr = ((unsigned long)hardfault_args[7]); #if 0 printf ("\n\n[Hard fault handler - all numbers in hex]\n"); @@ -95,103 +94,95 @@ void hard_fault_handler_c (unsigned int * hardfault_args) printf ("AFSR = %x\n", (*((volatile unsigned long *)(0xE000ED3C)))); printf ("SCB_SHCSR = %x\n", SCB->SHCSR); #else - (void)stacked_r0; - (void)stacked_r1; - (void)stacked_r2; - (void)stacked_r3; + (void)stacked_r0; + (void)stacked_r1; + (void)stacked_r2; + (void)stacked_r3; - (void)stacked_r12; - (void)stacked_lr; - (void)stacked_pc; - (void)stacked_psr; + (void)stacked_r12; + (void)stacked_lr; + (void)stacked_pc; + (void)stacked_psr; #endif - while (1); + while (1) + ; } #endif /** - * @brief This function handles Hard Fault exception. - * @param None - * @retval None - */ + * @brief This function handles Hard Fault exception. + * @param None + * @retval None + */ void HardFault_Handler(void) { #ifndef NDEBUG -__ASM("TST LR, #4"); -__ASM("ITE EQ \n" - "MRSEQ R0, MSP \n" - "MRSNE R0, PSP"); -__ASM("B hard_fault_handler_c"); + __ASM("TST LR, #4"); + __ASM("ITE EQ \n" + "MRSEQ R0, MSP \n" + "MRSNE R0, PSP"); + __ASM("B hard_fault_handler_c"); #endif } /** - * @brief This function handles Memory Manage exception. - * @param None - * @retval None - */ -void MemManage_Handler( - void) + * @brief This function handles Memory Manage exception. + * @param None + * @retval None + */ +void MemManage_Handler(void) { /* Go to infinite loop when Memory Manage exception occurs */ - while (1) { - } + while (1) { } } /** - * @brief This function handles Bus Fault exception. - * @param None - * @retval None - */ -void BusFault_Handler( - void) + * @brief This function handles Bus Fault exception. + * @param None + * @retval None + */ +void BusFault_Handler(void) { /* Go to infinite loop when Bus Fault exception occurs */ - while (1) { - } + while (1) { } } /** - * @brief This function handles Usage Fault exception. - * @param None - * @retval None - */ -void UsageFault_Handler( - void) + * @brief This function handles Usage Fault exception. + * @param None + * @retval None + */ +void UsageFault_Handler(void) { /* Go to infinite loop when Usage Fault exception occurs */ - while (1) { - } + while (1) { } } /** - * @brief This function handles SVCall exception. - * @param None - * @retval None - */ -void SVC_Handler( - void) + * @brief This function handles SVCall exception. + * @param None + * @retval None + */ +void SVC_Handler(void) { } /** - * @brief This function handles Debug Monitor exception. - * @param None - * @retval None - */ -void DebugMon_Handler( - void) + * @brief This function handles Debug Monitor exception. + * @param None + * @retval None + */ +void DebugMon_Handler(void) { } /** - * @brief This function handles PendSV_Handler exception. - * @param None - * @retval None - */ -void PendSV_Handler( - void) + * @brief This function handles PendSV_Handler exception. + * @param None + * @retval None + */ +void PendSV_Handler(void) { } diff --git a/ports/stm32f10x/system_stm32f10x.c b/ports/stm32f10x/system_stm32f10x.c index 7b2caa43..e10573c9 100644 --- a/ports/stm32f10x/system_stm32f10x.c +++ b/ports/stm32f10x/system_stm32f10x.c @@ -1,224 +1,227 @@ /** - ****************************************************************************** - * @file SysTick/system_stm32f10x.c - * @author MCD Application Team - * @version V3.4.0 - * @date 10/15/2010 - * @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Source File. - ****************************************************************************** - * - * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS - * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE - * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY - * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING - * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE - * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. - * - *

© COPYRIGHT 2010 STMicroelectronics

- ****************************************************************************** - */ + ****************************************************************************** + * @file SysTick/system_stm32f10x.c + * @author MCD Application Team + * @version V3.4.0 + * @date 10/15/2010 + * @brief CMSIS Cortex-M3 Device Peripheral Access Layer System Source File. + ****************************************************************************** + * + * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS + * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE + * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY + * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING + * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE + * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. + * + *

© COPYRIGHT 2010 STMicroelectronics

+ ****************************************************************************** + */ /** @addtogroup CMSIS - * @{ - */ + * @{ + */ /** @addtogroup stm32f10x_system - * @{ - */ + * @{ + */ /** @addtogroup STM32F10x_System_Private_Includes - * @{ - */ + * @{ + */ #include "stm32f10x.h" /** - * @} - */ + * @} + */ /** @addtogroup STM32F10x_System_Private_TypesDefinitions - * @{ - */ + * @{ + */ /** - * @} - */ + * @} + */ /** @addtogroup STM32F10x_System_Private_Defines - * @{ - */ + * @{ + */ /*!< Uncomment the line corresponding to the desired System clock (SYSCLK) frequency (after reset the HSI is used as SYSCLK source) - + IMPORTANT NOTE: - ============== + ============== 1. After each device reset the HSI is used as System clock source. - 2. Please make sure that the selected System clock doesn't exceed your device's - maximum frequency. - + 2. Please make sure that the selected System clock doesn't exceed your + device's maximum frequency. + 3. If none of the define below is enabled, the HSI is used as System clock source. - 4. The System clock configuration functions provided within this file assume that: - - For Low, Medium and High density Value line devices an external 8MHz + 4. The System clock configuration functions provided within this file assume + that: + - For Low, Medium and High density Value line devices an external 8MHz crystal is used to drive the System clock. - For Low, Medium and High density devices an external 8MHz crystal is used to drive the System clock. - - For Connectivity line devices an external 25MHz crystal is used to drive - the System clock. - If you are using different crystal you have to adapt those functions accordingly. + - For Connectivity line devices an external 25MHz crystal is used to + drive the System clock. If you are using different crystal you have to adapt + those functions accordingly. */ -#if defined (STM32F10X_LD_VL) || (defined STM32F10X_MD_VL) || (defined STM32F10X_HD_VL) +#if defined(STM32F10X_LD_VL) || (defined STM32F10X_MD_VL) || \ + (defined STM32F10X_HD_VL) /* #define SYSCLK_FREQ_HSE HSE_VALUE */ -#define SYSCLK_FREQ_24MHz 24000000 +#define SYSCLK_FREQ_24MHz 24000000 #else /* #define SYSCLK_FREQ_HSE HSE_VALUE */ /* #define SYSCLK_FREQ_24MHz 24000000 */ /* #define SYSCLK_FREQ_36MHz 36000000 */ /* #define SYSCLK_FREQ_48MHz 48000000 */ /* #define SYSCLK_FREQ_56MHz 56000000 */ -#define SYSCLK_FREQ_72MHz 72000000 +#define SYSCLK_FREQ_72MHz 72000000 #endif /*!< Uncomment the following line if you need to use external SRAM mounted - on STM3210E-EVAL board (STM32 High density and XL-density devices) or on - STM32100E-EVAL board (STM32 High-density value line devices) as data memory */ -#if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL) + on STM3210E-EVAL board (STM32 High density and XL-density devices) or on + STM32100E-EVAL board (STM32 High-density value line devices) as data memory + */ +#if defined(STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL) /* #define DATA_IN_ExtSRAM */ #endif /*!< Uncomment the following line if you need to relocate your vector Table in Internal SRAM. */ /* #define VECT_TAB_SRAM */ -#define VECT_TAB_OFFSET 0x0 /*!< Vector Table base offset field. - This value must be a multiple of 0x100. */ - +#define VECT_TAB_OFFSET \ + 0x0 /*!< Vector Table base offset field. \ + This value must be a multiple of 0x100. */ /** - * @} - */ + * @} + */ /** @addtogroup STM32F10x_System_Private_Macros - * @{ - */ + * @{ + */ /** - * @} - */ + * @} + */ /** @addtogroup STM32F10x_System_Private_Variables - * @{ - */ + * @{ + */ /******************************************************************************* -* Clock Definitions -*******************************************************************************/ + * Clock Definitions + *******************************************************************************/ #ifdef SYSCLK_FREQ_HSE -uint32_t SystemCoreClock = SYSCLK_FREQ_HSE; /*!< System Clock Frequency (Core Clock) */ +uint32_t SystemCoreClock = + SYSCLK_FREQ_HSE; /*!< System Clock Frequency (Core Clock) */ #elif defined SYSCLK_FREQ_24MHz -uint32_t SystemCoreClock = SYSCLK_FREQ_24MHz; /*!< System Clock Frequency (Core Clock) */ +uint32_t SystemCoreClock = + SYSCLK_FREQ_24MHz; /*!< System Clock Frequency (Core Clock) */ #elif defined SYSCLK_FREQ_36MHz -uint32_t SystemCoreClock = SYSCLK_FREQ_36MHz; /*!< System Clock Frequency (Core Clock) */ +uint32_t SystemCoreClock = + SYSCLK_FREQ_36MHz; /*!< System Clock Frequency (Core Clock) */ #elif defined SYSCLK_FREQ_48MHz -uint32_t SystemCoreClock = SYSCLK_FREQ_48MHz; /*!< System Clock Frequency (Core Clock) */ +uint32_t SystemCoreClock = + SYSCLK_FREQ_48MHz; /*!< System Clock Frequency (Core Clock) */ #elif defined SYSCLK_FREQ_56MHz -uint32_t SystemCoreClock = SYSCLK_FREQ_56MHz; /*!< System Clock Frequency (Core Clock) */ +uint32_t SystemCoreClock = + SYSCLK_FREQ_56MHz; /*!< System Clock Frequency (Core Clock) */ #elif defined SYSCLK_FREQ_72MHz -uint32_t SystemCoreClock = SYSCLK_FREQ_72MHz; /*!< System Clock Frequency (Core Clock) */ +uint32_t SystemCoreClock = + SYSCLK_FREQ_72MHz; /*!< System Clock Frequency (Core Clock) */ #else /*!< HSI Selected as System Clock source */ -uint32_t SystemCoreClock = HSI_VALUE; /*!< System Clock Frequency (Core Clock) */ +uint32_t SystemCoreClock = + HSI_VALUE; /*!< System Clock Frequency (Core Clock) */ #endif -__I uint8_t AHBPrescTable[16] = - { 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9 }; +__I uint8_t AHBPrescTable[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, + 9 }; /** - * @} - */ + * @} + */ /** @addtogroup STM32F10x_System_Private_FunctionPrototypes - * @{ - */ + * @{ + */ -static void SetSysClock( - void); +static void SetSysClock(void); #ifdef SYSCLK_FREQ_HSE -static void SetSysClockToHSE( - void); +static void SetSysClockToHSE(void); #elif defined SYSCLK_FREQ_24MHz -static void SetSysClockTo24( - void); +static void SetSysClockTo24(void); #elif defined SYSCLK_FREQ_36MHz -static void SetSysClockTo36( - void); +static void SetSysClockTo36(void); #elif defined SYSCLK_FREQ_48MHz -static void SetSysClockTo48( - void); +static void SetSysClockTo48(void); #elif defined SYSCLK_FREQ_56MHz -static void SetSysClockTo56( - void); +static void SetSysClockTo56(void); #elif defined SYSCLK_FREQ_72MHz -static void SetSysClockTo72( - void); +static void SetSysClockTo72(void); #endif #ifdef DATA_IN_ExtSRAM -static void SystemInit_ExtMemCtl( - void); +static void SystemInit_ExtMemCtl(void); #endif /* DATA_IN_ExtSRAM */ /** - * @} - */ + * @} + */ /** @addtogroup STM32F10x_System_Private_Functions - * @{ - */ + * @{ + */ /** - * @brief Setup the microcontroller system - * Initialize the Embedded Flash Interface, the PLL and update the - * SystemCoreClock variable. - * @note This function should be used only after reset. - * @param None - * @retval None - */ -void SystemInit( - void) + * @brief Setup the microcontroller system + * Initialize the Embedded Flash Interface, the PLL and update the + * SystemCoreClock variable. + * @note This function should be used only after reset. + * @param None + * @retval None + */ +void SystemInit(void) { - /* Reset the RCC clock configuration to the default reset state(for debug purpose) */ + /* Reset the RCC clock configuration to the default reset state(for debug + * purpose) */ /* Set HSION bit */ - RCC->CR |= (uint32_t) 0x00000001; + RCC->CR |= (uint32_t)0x00000001; /* Reset SW, HPRE, PPRE1, PPRE2, ADCPRE and MCO bits */ #ifndef STM32F10X_CL - RCC->CFGR &= (uint32_t) 0xF8FF0000; + RCC->CFGR &= (uint32_t)0xF8FF0000; #else - RCC->CFGR &= (uint32_t) 0xF0FF0000; + RCC->CFGR &= (uint32_t)0xF0FF0000; #endif /* STM32F10X_CL */ /* Reset HSEON, CSSON and PLLON bits */ - RCC->CR &= (uint32_t) 0xFEF6FFFF; + RCC->CR &= (uint32_t)0xFEF6FFFF; /* Reset HSEBYP bit */ - RCC->CR &= (uint32_t) 0xFFFBFFFF; + RCC->CR &= (uint32_t)0xFFFBFFFF; /* Reset PLLSRC, PLLXTPRE, PLLMUL and USBPRE/OTGFSPRE bits */ - RCC->CFGR &= (uint32_t) 0xFF80FFFF; + RCC->CFGR &= (uint32_t)0xFF80FFFF; #ifdef STM32F10X_CL /* Reset PLL2ON and PLL3ON bits */ - RCC->CR &= (uint32_t) 0xEBFFFFFF; + RCC->CR &= (uint32_t)0xEBFFFFFF; /* Disable all interrupts and clear pending bits */ RCC->CIR = 0x00FF0000; /* Reset CFGR2 register */ RCC->CFGR2 = 0x00000000; -#elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL) +#elif defined(STM32F10X_LD_VL) || defined(STM32F10X_MD_VL) || \ + (defined STM32F10X_HD_VL) /* Disable all interrupts and clear pending bits */ RCC->CIR = 0x009F0000; @@ -229,7 +232,7 @@ void SystemInit( RCC->CIR = 0x009F0000; #endif /* STM32F10X_CL */ -#if defined (STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL) +#if defined(STM32F10X_HD) || (defined STM32F10X_XL) || (defined STM32F10X_HD_VL) #ifdef DATA_IN_ExtSRAM SystemInit_ExtMemCtl(); #endif /* DATA_IN_ExtSRAM */ @@ -240,45 +243,49 @@ void SystemInit( SetSysClock(); #ifdef VECT_TAB_SRAM - SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */ + SCB->VTOR = SRAM_BASE | + VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM. */ #else - SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */ + SCB->VTOR = FLASH_BASE | + VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH. */ #endif } /** - * @brief Update SystemCoreClock according to Clock Register Values - * @note None - * @param None - * @retval None - */ -void SystemCoreClockUpdate( - void) + * @brief Update SystemCoreClock according to Clock Register Values + * @note None + * @param None + * @retval None + */ +void SystemCoreClockUpdate(void) { uint32_t tmp = 0, pllmull = 0, pllsource = 0; -#ifdef STM32F10X_CL - uint32_t prediv1source = 0, prediv1factor = 0, prediv2factor = - 0, pll2mull = 0; +#ifdef STM32F10X_CL + uint32_t prediv1source = 0, prediv1factor = 0, prediv2factor = 0, + pll2mull = 0; #endif /* STM32F10X_CL */ -#if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL) +#if defined(STM32F10X_LD_VL) || defined(STM32F10X_MD_VL) || \ + (defined STM32F10X_HD_VL) uint32_t prediv1factor = 0; #endif /* STM32F10X_LD_VL or STM32F10X_MD_VL or STM32F10X_HD_VL */ - /* Get SYSCLK source ------------------------------------------------------- */ + /* Get SYSCLK source ------------------------------------------------------- + */ tmp = RCC->CFGR & RCC_CFGR_SWS; switch (tmp) { - case 0x00: /* HSI used as system clock */ + case 0x00: /* HSI used as system clock */ SystemCoreClock = HSI_VALUE; break; - case 0x04: /* HSE used as system clock */ + case 0x04: /* HSE used as system clock */ SystemCoreClock = HSE_VALUE; break; - case 0x08: /* PLL used as system clock */ + case 0x08: /* PLL used as system clock */ - /* Get PLL clock source and multiplication factor ---------------------- */ + /* Get PLL clock source and multiplication factor + * ---------------------- */ pllmull = RCC->CFGR & RCC_CFGR_PLLMULL; pllsource = RCC->CFGR & RCC_CFGR_PLLSRC; @@ -286,16 +293,19 @@ void SystemCoreClockUpdate( pllmull = (pllmull >> 18) + 2; if (pllsource == 0x00) { - /* HSI oscillator clock divided by 2 selected as PLL clock entry */ + /* HSI oscillator clock divided by 2 selected as PLL clock entry + */ SystemCoreClock = (HSI_VALUE >> 1) * pllmull; } else { -#if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || (defined STM32F10X_HD_VL) +#if defined(STM32F10X_LD_VL) || defined(STM32F10X_MD_VL) || \ + (defined STM32F10X_HD_VL) prediv1factor = (RCC->CFGR2 & RCC_CFGR2_PREDIV1) + 1; /* HSE oscillator clock selected as PREDIV1 clock entry */ SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull; #else /* HSE selected as PLL clock entry */ - if ((RCC->CFGR & RCC_CFGR_PLLXTPRE) != (uint32_t) RESET) { /* HSE oscillator clock divided by 2 */ + if ((RCC->CFGR & RCC_CFGR_PLLXTPRE) != + (uint32_t)RESET) { /* HSE oscillator clock divided by 2 */ SystemCoreClock = (HSE_VALUE >> 1) * pllmull; } else { SystemCoreClock = HSE_VALUE * pllmull; @@ -307,14 +317,15 @@ void SystemCoreClockUpdate( if (pllmull != 0x0D) { pllmull += 2; - } else { /* PLL multiplication factor = PLL input clock * 6.5 */ + } else { /* PLL multiplication factor = PLL input clock * 6.5 */ pllmull = 13 / 2; } if (pllsource == 0x00) { - /* HSI oscillator clock divided by 2 selected as PLL clock entry */ + /* HSI oscillator clock divided by 2 selected as PLL clock entry + */ SystemCoreClock = (HSI_VALUE >> 1) * pllmull; - } else { /* PREDIV1 selected as PLL clock entry */ + } else { /* PREDIV1 selected as PLL clock entry */ /* Get PREDIV1 clock source and division factor */ prediv1source = RCC->CFGR2 & RCC_CFGR2_PREDIV1SRC; @@ -323,15 +334,16 @@ void SystemCoreClockUpdate( if (prediv1source == 0) { /* HSE oscillator clock selected as PREDIV1 clock entry */ SystemCoreClock = (HSE_VALUE / prediv1factor) * pllmull; - } else { /* PLL2 clock selected as PREDIV1 clock entry */ + } else { /* PLL2 clock selected as PREDIV1 clock entry */ - /* Get PREDIV2 division factor and PLL2 multiplication factor */ - prediv2factor = - ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> 4) + 1; + /* Get PREDIV2 division factor and PLL2 multiplication + * factor */ + prediv2factor = ((RCC->CFGR2 & RCC_CFGR2_PREDIV2) >> 4) + 1; pll2mull = ((RCC->CFGR2 & RCC_CFGR2_PLL2MUL) >> 8) + 2; SystemCoreClock = (((HSE_VALUE / prediv2factor) * pll2mull) / - prediv1factor) * pllmull; + prediv1factor) * + pllmull; } } #endif /* STM32F10X_CL */ @@ -350,12 +362,12 @@ void SystemCoreClockUpdate( } /** - * @brief Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers. - * @param None - * @retval None - */ -static void SetSysClock( - void) + * @brief Configures the System clock frequency, HCLK, PCLK2 and PCLK1 + * prescalers. + * @param None + * @retval None + */ +static void SetSysClock(void) { #ifdef SYSCLK_FREQ_HSE SetSysClockToHSE(); @@ -376,26 +388,25 @@ static void SetSysClock( } /** - * @brief Setup the external memory controller. Called in startup_stm32f10x.s - * before jump to __main - * @param None - * @retval None - */ + * @brief Setup the external memory controller. Called in startup_stm32f10x.s + * before jump to __main + * @param None + * @retval None + */ #ifdef DATA_IN_ExtSRAM /** - * @brief Setup the external memory controller. - * Called in startup_stm32f10x_xx.s/.c before jump to main. - * This function configures the external SRAM mounted on STM3210E-EVAL - * board (STM32 High density devices). This SRAM will be used as program - * data memory (including heap and stack). - * @param None - * @retval None - */ -void SystemInit_ExtMemCtl( - void) + * @brief Setup the external memory controller. + * Called in startup_stm32f10x_xx.s/.c before jump to main. + * This function configures the external SRAM mounted on + * STM3210E-EVAL board (STM32 High density devices). This SRAM will be used as + * program data memory (including heap and stack). + * @param None + * @retval None + */ +void SystemInit_ExtMemCtl(void) { -/*!< FSMC Bank1 NOR/SRAM3 is used for the STM3210E-EVAL, if another Bank is - required, then adjust the Register Addresses */ + /*!< FSMC Bank1 NOR/SRAM3 is used for the STM3210E-EVAL, if another Bank is + required, then adjust the Register Addresses */ /* Enable FSMC clock */ RCC->AHBENR = 0x00000114; @@ -403,11 +414,16 @@ void SystemInit_ExtMemCtl( /* Enable GPIOD, GPIOE, GPIOF and GPIOG clocks */ RCC->APB2ENR = 0x000001E0; -/* --------------- SRAM Data lines, NOE and NWE configuration ---------------*/ -/*---------------- SRAM Address lines configuration -------------------------*/ -/*---------------- NOE and NWE configuration --------------------------------*/ -/*---------------- NE3 configuration ----------------------------------------*/ -/*---------------- NBL0, NBL1 configuration ---------------------------------*/ + /* --------------- SRAM Data lines, NOE and NWE configuration + * ---------------*/ + /*---------------- SRAM Address lines configuration + * -------------------------*/ + /*---------------- NOE and NWE configuration + * --------------------------------*/ + /*---------------- NE3 configuration + * ----------------------------------------*/ + /*---------------- NBL0, NBL1 configuration + * ---------------------------------*/ GPIOD->CRL = 0x44BB44BB; GPIOD->CRH = 0xBBBBBBBB; @@ -421,8 +437,10 @@ void SystemInit_ExtMemCtl( GPIOG->CRL = 0x44BBBBBB; GPIOG->CRH = 0x44444B44; -/*---------------- FSMC Configuration ---------------------------------------*/ -/*---------------- Enable FSMC Bank1_SRAM Bank ------------------------------*/ + /*---------------- FSMC Configuration + * ---------------------------------------*/ + /*---------------- Enable FSMC Bank1_SRAM Bank + * ------------------------------*/ FSMC_Bank1->BTCR[4] = 0x00001011; FSMC_Bank1->BTCR[5] = 0x00000200; @@ -431,20 +449,20 @@ void SystemInit_ExtMemCtl( #ifdef SYSCLK_FREQ_HSE /** - * @brief Selects HSE as System clock source and configure HCLK, PCLK2 - * and PCLK1 prescalers. - * @note This function should be used only after reset. - * @param None - * @retval None - */ -static void SetSysClockToHSE( - void) + * @brief Selects HSE as System clock source and configure HCLK, PCLK2 + * and PCLK1 prescalers. + * @note This function should be used only after reset. + * @param None + * @retval None + */ +static void SetSysClockToHSE(void) { __IO uint32_t StartUpCounter = 0, HSEStatus = 0; - /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration --------------------------- */ + /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration --------------------------- + */ /* Enable HSE */ - RCC->CR |= ((uint32_t) RCC_CR_HSEON); + RCC->CR |= ((uint32_t)RCC_CR_HSEON); /* Wait till HSE is ready and if Time out is reached exit */ do { @@ -453,67 +471,67 @@ static void SetSysClockToHSE( } while ((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT)); if ((RCC->CR & RCC_CR_HSERDY) != RESET) { - HSEStatus = (uint32_t) 0x01; + HSEStatus = (uint32_t)0x01; } else { - HSEStatus = (uint32_t) 0x00; + HSEStatus = (uint32_t)0x00; } - if (HSEStatus == (uint32_t) 0x01) { - -#if !defined STM32F10X_LD_VL && !defined STM32F10X_MD_VL && !defined STM32F10X_HD_VL + if (HSEStatus == (uint32_t)0x01) { +#if !defined STM32F10X_LD_VL && !defined STM32F10X_MD_VL && \ + !defined STM32F10X_HD_VL /* Enable Prefetch Buffer */ FLASH->ACR |= FLASH_ACR_PRFTBE; /* Flash 0 wait state */ - FLASH->ACR &= (uint32_t) ((uint32_t) ~ FLASH_ACR_LATENCY); + FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY); #ifndef STM32F10X_CL - FLASH->ACR |= (uint32_t) FLASH_ACR_LATENCY_0; + FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0; #else if (HSE_VALUE <= 24000000) { - FLASH->ACR |= (uint32_t) FLASH_ACR_LATENCY_0; + FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0; } else { - FLASH->ACR |= (uint32_t) FLASH_ACR_LATENCY_1; + FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1; } #endif /* STM32F10X_CL */ #endif /* HCLK = SYSCLK */ - RCC->CFGR |= (uint32_t) RCC_CFGR_HPRE_DIV1; + RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1; /* PCLK2 = HCLK */ - RCC->CFGR |= (uint32_t) RCC_CFGR_PPRE2_DIV1; + RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1; /* PCLK1 = HCLK */ - RCC->CFGR |= (uint32_t) RCC_CFGR_PPRE1_DIV1; + RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1; /* Select HSE as system clock source */ - RCC->CFGR &= (uint32_t) ((uint32_t) ~ (RCC_CFGR_SW)); - RCC->CFGR |= (uint32_t) RCC_CFGR_SW_HSE; + RCC->CFGR &= (uint32_t)((uint32_t) ~(RCC_CFGR_SW)); + RCC->CFGR |= (uint32_t)RCC_CFGR_SW_HSE; /* Wait till HSE is used as system clock source */ - while ((RCC->CFGR & (uint32_t) RCC_CFGR_SWS) != (uint32_t) 0x04) { - } - } else { /* If HSE fails to start-up, the application will have wrong clock - configuration. User can add here some code to deal with this error */ + while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x04) { } + } else { /* If HSE fails to start-up, the application will have wrong clock + configuration. User can add here some code to deal with this + error */ } } #elif defined SYSCLK_FREQ_24MHz /** - * @brief Sets System clock frequency to 24MHz and configure HCLK, PCLK2 - * and PCLK1 prescalers. - * @note This function should be used only after reset. - * @param None - * @retval None - */ -static void SetSysClockTo24( - void) + * @brief Sets System clock frequency to 24MHz and configure HCLK, PCLK2 + * and PCLK1 prescalers. + * @note This function should be used only after reset. + * @param None + * @retval None + */ +static void SetSysClockTo24(void) { __IO uint32_t StartUpCounter = 0, HSEStatus = 0; - /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration --------------------------- */ + /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration --------------------------- + */ /* Enable HSE */ - RCC->CR |= ((uint32_t) RCC_CR_HSEON); + RCC->CR |= ((uint32_t)RCC_CR_HSEON); /* Wait till HSE is ready and if Time out is reached exit */ do { @@ -522,106 +540,99 @@ static void SetSysClockTo24( } while ((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT)); if ((RCC->CR & RCC_CR_HSERDY) != RESET) { - HSEStatus = (uint32_t) 0x01; + HSEStatus = (uint32_t)0x01; } else { - HSEStatus = (uint32_t) 0x00; + HSEStatus = (uint32_t)0x00; } - if (HSEStatus == (uint32_t) 0x01) { -#if !defined STM32F10X_LD_VL && !defined STM32F10X_MD_VL && !defined STM32F10X_HD_VL + if (HSEStatus == (uint32_t)0x01) { +#if !defined STM32F10X_LD_VL && !defined STM32F10X_MD_VL && \ + !defined STM32F10X_HD_VL /* Enable Prefetch Buffer */ FLASH->ACR |= FLASH_ACR_PRFTBE; /* Flash 0 wait state */ - FLASH->ACR &= (uint32_t) ((uint32_t) ~ FLASH_ACR_LATENCY); - FLASH->ACR |= (uint32_t) FLASH_ACR_LATENCY_0; + FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY); + FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_0; #endif /* HCLK = SYSCLK */ - RCC->CFGR |= (uint32_t) RCC_CFGR_HPRE_DIV1; + RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1; /* PCLK2 = HCLK */ - RCC->CFGR |= (uint32_t) RCC_CFGR_PPRE2_DIV1; + RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1; /* PCLK1 = HCLK */ - RCC->CFGR |= (uint32_t) RCC_CFGR_PPRE1_DIV1; + RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1; #ifdef STM32F10X_CL - /* Configure PLLs ------------------------------------------------------ */ + /* Configure PLLs ------------------------------------------------------ + */ /* PLL configuration: PLLCLK = PREDIV1 * 6 = 24 MHz */ - RCC->CFGR &= - (uint32_t) ~ (RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | - RCC_CFGR_PLLMULL); - RCC->CFGR |= - (uint32_t) (RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 | - RCC_CFGR_PLLMULL6); + RCC->CFGR &= (uint32_t) ~( + RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL); + RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | + RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLMULL6); /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */ /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 10 = 4 MHz */ - RCC->CFGR2 &= - (uint32_t) ~ (RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL | + RCC->CFGR2 &= (uint32_t) ~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL | RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC); - RCC->CFGR2 |= - (uint32_t) (RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 | + RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 | RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV10); /* Enable PLL2 */ RCC->CR |= RCC_CR_PLL2ON; /* Wait till PLL2 is ready */ - while ((RCC->CR & RCC_CR_PLL2RDY) == 0) { - } -#elif defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL) + while ((RCC->CR & RCC_CR_PLL2RDY) == 0) { } +#elif defined(STM32F10X_LD_VL) || defined(STM32F10X_MD_VL) || \ + defined(STM32F10X_HD_VL) /* PLL configuration: = (HSE / 2) * 6 = 24 MHz */ - RCC->CFGR &= - (uint32_t) ((uint32_t) ~ (RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | - RCC_CFGR_PLLMULL)); - RCC->CFGR |= - (uint32_t) (RCC_CFGR_PLLSRC_PREDIV1 | + RCC->CFGR &= (uint32_t)((uint32_t) ~( + RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL)); + RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLXTPRE_PREDIV1_Div2 | RCC_CFGR_PLLMULL6); #else /* PLL configuration: = (HSE / 2) * 6 = 24 MHz */ - RCC->CFGR &= - (uint32_t) ((uint32_t) ~ (RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | - RCC_CFGR_PLLMULL)); - RCC->CFGR |= - (uint32_t) (RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLXTPRE_HSE_Div2 | - RCC_CFGR_PLLMULL6); + RCC->CFGR &= (uint32_t)((uint32_t) ~( + RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL)); + RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | + RCC_CFGR_PLLXTPRE_HSE_Div2 | RCC_CFGR_PLLMULL6); #endif /* STM32F10X_CL */ /* Enable PLL */ RCC->CR |= RCC_CR_PLLON; /* Wait till PLL is ready */ - while ((RCC->CR & RCC_CR_PLLRDY) == 0) { - } + while ((RCC->CR & RCC_CR_PLLRDY) == 0) { } /* Select PLL as system clock source */ - RCC->CFGR &= (uint32_t) ((uint32_t) ~ (RCC_CFGR_SW)); - RCC->CFGR |= (uint32_t) RCC_CFGR_SW_PLL; + RCC->CFGR &= (uint32_t)((uint32_t) ~(RCC_CFGR_SW)); + RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL; /* Wait till PLL is used as system clock source */ - while ((RCC->CFGR & (uint32_t) RCC_CFGR_SWS) != (uint32_t) 0x08) { - } - } else { /* If HSE fails to start-up, the application will have wrong clock - configuration. User can add here some code to deal with this error */ + while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08) { } + } else { /* If HSE fails to start-up, the application will have wrong clock + configuration. User can add here some code to deal with this + error */ } } #elif defined SYSCLK_FREQ_36MHz /** - * @brief Sets System clock frequency to 36MHz and configure HCLK, PCLK2 - * and PCLK1 prescalers. - * @note This function should be used only after reset. - * @param None - * @retval None - */ -static void SetSysClockTo36( - void) + * @brief Sets System clock frequency to 36MHz and configure HCLK, PCLK2 + * and PCLK1 prescalers. + * @note This function should be used only after reset. + * @param None + * @retval None + */ +static void SetSysClockTo36(void) { __IO uint32_t StartUpCounter = 0, HSEStatus = 0; - /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration --------------------------- */ + /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration --------------------------- + */ /* Enable HSE */ - RCC->CR |= ((uint32_t) RCC_CR_HSEON); + RCC->CR |= ((uint32_t)RCC_CR_HSEON); /* Wait till HSE is ready and if Time out is reached exit */ do { @@ -630,99 +641,92 @@ static void SetSysClockTo36( } while ((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT)); if ((RCC->CR & RCC_CR_HSERDY) != RESET) { - HSEStatus = (uint32_t) 0x01; + HSEStatus = (uint32_t)0x01; } else { - HSEStatus = (uint32_t) 0x00; + HSEStatus = (uint32_t)0x00; } - if (HSEStatus == (uint32_t) 0x01) { + if (HSEStatus == (uint32_t)0x01) { /* Enable Prefetch Buffer */ FLASH->ACR |= FLASH_ACR_PRFTBE; /* Flash 1 wait state */ - FLASH->ACR &= (uint32_t) ((uint32_t) ~ FLASH_ACR_LATENCY); - FLASH->ACR |= (uint32_t) FLASH_ACR_LATENCY_1; + FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY); + FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1; /* HCLK = SYSCLK */ - RCC->CFGR |= (uint32_t) RCC_CFGR_HPRE_DIV1; + RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1; /* PCLK2 = HCLK */ - RCC->CFGR |= (uint32_t) RCC_CFGR_PPRE2_DIV1; + RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1; /* PCLK1 = HCLK */ - RCC->CFGR |= (uint32_t) RCC_CFGR_PPRE1_DIV1; + RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV1; #ifdef STM32F10X_CL - /* Configure PLLs ------------------------------------------------------ */ + /* Configure PLLs ------------------------------------------------------ + */ /* PLL configuration: PLLCLK = PREDIV1 * 9 = 36 MHz */ - RCC->CFGR &= - (uint32_t) ~ (RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | - RCC_CFGR_PLLMULL); - RCC->CFGR |= - (uint32_t) (RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 | - RCC_CFGR_PLLMULL9); + RCC->CFGR &= (uint32_t) ~( + RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL); + RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | + RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLMULL9); /*!< PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */ /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 10 = 4 MHz */ - RCC->CFGR2 &= - (uint32_t) ~ (RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL | + RCC->CFGR2 &= (uint32_t) ~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL | RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC); - RCC->CFGR2 |= - (uint32_t) (RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 | + RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 | RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV10); /* Enable PLL2 */ RCC->CR |= RCC_CR_PLL2ON; /* Wait till PLL2 is ready */ - while ((RCC->CR & RCC_CR_PLL2RDY) == 0) { - } + while ((RCC->CR & RCC_CR_PLL2RDY) == 0) { } #else /* PLL configuration: PLLCLK = (HSE / 2) * 9 = 36 MHz */ - RCC->CFGR &= - (uint32_t) ((uint32_t) ~ (RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | - RCC_CFGR_PLLMULL)); - RCC->CFGR |= - (uint32_t) (RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLXTPRE_HSE_Div2 | - RCC_CFGR_PLLMULL9); + RCC->CFGR &= (uint32_t)((uint32_t) ~( + RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL)); + RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | + RCC_CFGR_PLLXTPRE_HSE_Div2 | RCC_CFGR_PLLMULL9); #endif /* STM32F10X_CL */ /* Enable PLL */ RCC->CR |= RCC_CR_PLLON; /* Wait till PLL is ready */ - while ((RCC->CR & RCC_CR_PLLRDY) == 0) { - } + while ((RCC->CR & RCC_CR_PLLRDY) == 0) { } /* Select PLL as system clock source */ - RCC->CFGR &= (uint32_t) ((uint32_t) ~ (RCC_CFGR_SW)); - RCC->CFGR |= (uint32_t) RCC_CFGR_SW_PLL; + RCC->CFGR &= (uint32_t)((uint32_t) ~(RCC_CFGR_SW)); + RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL; /* Wait till PLL is used as system clock source */ - while ((RCC->CFGR & (uint32_t) RCC_CFGR_SWS) != (uint32_t) 0x08) { - } - } else { /* If HSE fails to start-up, the application will have wrong clock - configuration. User can add here some code to deal with this error */ + while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08) { } + } else { /* If HSE fails to start-up, the application will have wrong clock + configuration. User can add here some code to deal with this + error */ } } #elif defined SYSCLK_FREQ_48MHz /** - * @brief Sets System clock frequency to 48MHz and configure HCLK, PCLK2 - * and PCLK1 prescalers. - * @note This function should be used only after reset. - * @param None - * @retval None - */ -static void SetSysClockTo48( - void) + * @brief Sets System clock frequency to 48MHz and configure HCLK, PCLK2 + * and PCLK1 prescalers. + * @note This function should be used only after reset. + * @param None + * @retval None + */ +static void SetSysClockTo48(void) { __IO uint32_t StartUpCounter = 0, HSEStatus = 0; - /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration --------------------------- */ + /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration --------------------------- + */ /* Enable HSE */ - RCC->CR |= ((uint32_t) RCC_CR_HSEON); + RCC->CR |= ((uint32_t)RCC_CR_HSEON); /* Wait till HSE is ready and if Time out is reached exit */ do { @@ -731,97 +735,90 @@ static void SetSysClockTo48( } while ((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT)); if ((RCC->CR & RCC_CR_HSERDY) != RESET) { - HSEStatus = (uint32_t) 0x01; + HSEStatus = (uint32_t)0x01; } else { - HSEStatus = (uint32_t) 0x00; + HSEStatus = (uint32_t)0x00; } - if (HSEStatus == (uint32_t) 0x01) { + if (HSEStatus == (uint32_t)0x01) { /* Enable Prefetch Buffer */ FLASH->ACR |= FLASH_ACR_PRFTBE; /* Flash 1 wait state */ - FLASH->ACR &= (uint32_t) ((uint32_t) ~ FLASH_ACR_LATENCY); - FLASH->ACR |= (uint32_t) FLASH_ACR_LATENCY_1; + FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY); + FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_1; /* HCLK = SYSCLK */ - RCC->CFGR |= (uint32_t) RCC_CFGR_HPRE_DIV1; + RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1; /* PCLK2 = HCLK */ - RCC->CFGR |= (uint32_t) RCC_CFGR_PPRE2_DIV1; + RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1; /* PCLK1 = HCLK */ - RCC->CFGR |= (uint32_t) RCC_CFGR_PPRE1_DIV2; + RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2; #ifdef STM32F10X_CL - /* Configure PLLs ------------------------------------------------------ */ + /* Configure PLLs ------------------------------------------------------ + */ /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */ /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */ - RCC->CFGR2 &= - (uint32_t) ~ (RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL | + RCC->CFGR2 &= (uint32_t) ~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL | RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC); - RCC->CFGR2 |= - (uint32_t) (RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 | + RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 | RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5); /* Enable PLL2 */ RCC->CR |= RCC_CR_PLL2ON; /* Wait till PLL2 is ready */ - while ((RCC->CR & RCC_CR_PLL2RDY) == 0) { - } - + while ((RCC->CR & RCC_CR_PLL2RDY) == 0) { } /* PLL configuration: PLLCLK = PREDIV1 * 6 = 48 MHz */ - RCC->CFGR &= - (uint32_t) ~ (RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | - RCC_CFGR_PLLMULL); - RCC->CFGR |= - (uint32_t) (RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 | - RCC_CFGR_PLLMULL6); + RCC->CFGR &= (uint32_t) ~( + RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL); + RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | + RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLMULL6); #else /* PLL configuration: PLLCLK = HSE * 6 = 48 MHz */ - RCC->CFGR &= - (uint32_t) ((uint32_t) ~ (RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | - RCC_CFGR_PLLMULL)); - RCC->CFGR |= (uint32_t) (RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL6); + RCC->CFGR &= (uint32_t)((uint32_t) ~( + RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL)); + RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL6); #endif /* STM32F10X_CL */ /* Enable PLL */ RCC->CR |= RCC_CR_PLLON; /* Wait till PLL is ready */ - while ((RCC->CR & RCC_CR_PLLRDY) == 0) { - } + while ((RCC->CR & RCC_CR_PLLRDY) == 0) { } /* Select PLL as system clock source */ - RCC->CFGR &= (uint32_t) ((uint32_t) ~ (RCC_CFGR_SW)); - RCC->CFGR |= (uint32_t) RCC_CFGR_SW_PLL; + RCC->CFGR &= (uint32_t)((uint32_t) ~(RCC_CFGR_SW)); + RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL; /* Wait till PLL is used as system clock source */ - while ((RCC->CFGR & (uint32_t) RCC_CFGR_SWS) != (uint32_t) 0x08) { - } - } else { /* If HSE fails to start-up, the application will have wrong clock - configuration. User can add here some code to deal with this error */ + while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08) { } + } else { /* If HSE fails to start-up, the application will have wrong clock + configuration. User can add here some code to deal with this + error */ } } #elif defined SYSCLK_FREQ_56MHz /** - * @brief Sets System clock frequency to 56MHz and configure HCLK, PCLK2 - * and PCLK1 prescalers. - * @note This function should be used only after reset. - * @param None - * @retval None - */ -static void SetSysClockTo56( - void) + * @brief Sets System clock frequency to 56MHz and configure HCLK, PCLK2 + * and PCLK1 prescalers. + * @note This function should be used only after reset. + * @param None + * @retval None + */ +static void SetSysClockTo56(void) { __IO uint32_t StartUpCounter = 0, HSEStatus = 0; - /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration --------------------------- */ + /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration --------------------------- + */ /* Enable HSE */ - RCC->CR |= ((uint32_t) RCC_CR_HSEON); + RCC->CR |= ((uint32_t)RCC_CR_HSEON); /* Wait till HSE is ready and if Time out is reached exit */ do { @@ -830,60 +827,54 @@ static void SetSysClockTo56( } while ((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT)); if ((RCC->CR & RCC_CR_HSERDY) != RESET) { - HSEStatus = (uint32_t) 0x01; + HSEStatus = (uint32_t)0x01; } else { - HSEStatus = (uint32_t) 0x00; + HSEStatus = (uint32_t)0x00; } - if (HSEStatus == (uint32_t) 0x01) { + if (HSEStatus == (uint32_t)0x01) { /* Enable Prefetch Buffer */ FLASH->ACR |= FLASH_ACR_PRFTBE; /* Flash 2 wait state */ - FLASH->ACR &= (uint32_t) ((uint32_t) ~ FLASH_ACR_LATENCY); - FLASH->ACR |= (uint32_t) FLASH_ACR_LATENCY_2; + FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY); + FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2; /* HCLK = SYSCLK */ - RCC->CFGR |= (uint32_t) RCC_CFGR_HPRE_DIV1; + RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1; /* PCLK2 = HCLK */ - RCC->CFGR |= (uint32_t) RCC_CFGR_PPRE2_DIV1; + RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1; /* PCLK1 = HCLK */ - RCC->CFGR |= (uint32_t) RCC_CFGR_PPRE1_DIV2; + RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2; #ifdef STM32F10X_CL - /* Configure PLLs ------------------------------------------------------ */ + /* Configure PLLs ------------------------------------------------------ + */ /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */ /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */ - RCC->CFGR2 &= - (uint32_t) ~ (RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL | + RCC->CFGR2 &= (uint32_t) ~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL | RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC); - RCC->CFGR2 |= - (uint32_t) (RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 | + RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 | RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5); /* Enable PLL2 */ RCC->CR |= RCC_CR_PLL2ON; /* Wait till PLL2 is ready */ - while ((RCC->CR & RCC_CR_PLL2RDY) == 0) { - } - + while ((RCC->CR & RCC_CR_PLL2RDY) == 0) { } /* PLL configuration: PLLCLK = PREDIV1 * 7 = 56 MHz */ - RCC->CFGR &= - (uint32_t) ~ (RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | - RCC_CFGR_PLLMULL); - RCC->CFGR |= - (uint32_t) (RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 | - RCC_CFGR_PLLMULL7); + RCC->CFGR &= (uint32_t) ~( + RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL); + RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | + RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLMULL7); #else /* PLL configuration: PLLCLK = HSE * 7 = 56 MHz */ - RCC->CFGR &= - (uint32_t) ((uint32_t) ~ (RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | - RCC_CFGR_PLLMULL)); - RCC->CFGR |= (uint32_t) (RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL7); + RCC->CFGR &= (uint32_t)((uint32_t) ~( + RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL)); + RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL7); #endif /* STM32F10X_CL */ @@ -891,37 +882,36 @@ static void SetSysClockTo56( RCC->CR |= RCC_CR_PLLON; /* Wait till PLL is ready */ - while ((RCC->CR & RCC_CR_PLLRDY) == 0) { - } + while ((RCC->CR & RCC_CR_PLLRDY) == 0) { } /* Select PLL as system clock source */ - RCC->CFGR &= (uint32_t) ((uint32_t) ~ (RCC_CFGR_SW)); - RCC->CFGR |= (uint32_t) RCC_CFGR_SW_PLL; + RCC->CFGR &= (uint32_t)((uint32_t) ~(RCC_CFGR_SW)); + RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL; /* Wait till PLL is used as system clock source */ - while ((RCC->CFGR & (uint32_t) RCC_CFGR_SWS) != (uint32_t) 0x08) { - } - } else { /* If HSE fails to start-up, the application will have wrong clock - configuration. User can add here some code to deal with this error */ + while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08) { } + } else { /* If HSE fails to start-up, the application will have wrong clock + configuration. User can add here some code to deal with this + error */ } } #elif defined SYSCLK_FREQ_72MHz /** - * @brief Sets System clock frequency to 72MHz and configure HCLK, PCLK2 - * and PCLK1 prescalers. - * @note This function should be used only after reset. - * @param None - * @retval None - */ -static void SetSysClockTo72( - void) + * @brief Sets System clock frequency to 72MHz and configure HCLK, PCLK2 + * and PCLK1 prescalers. + * @note This function should be used only after reset. + * @param None + * @retval None + */ +static void SetSysClockTo72(void) { __IO uint32_t StartUpCounter = 0, HSEStatus = 0; - /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration --------------------------- */ + /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration --------------------------- + */ /* Enable HSE */ - RCC->CR |= ((uint32_t) RCC_CR_HSEON); + RCC->CR |= ((uint32_t)RCC_CR_HSEON); /* Wait till HSE is ready and if Time out is reached exit */ do { @@ -930,92 +920,84 @@ static void SetSysClockTo72( } while ((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT)); if ((RCC->CR & RCC_CR_HSERDY) != RESET) { - HSEStatus = (uint32_t) 0x01; + HSEStatus = (uint32_t)0x01; } else { - HSEStatus = (uint32_t) 0x00; + HSEStatus = (uint32_t)0x00; } - if (HSEStatus == (uint32_t) 0x01) { + if (HSEStatus == (uint32_t)0x01) { /* Enable Prefetch Buffer */ FLASH->ACR |= FLASH_ACR_PRFTBE; /* Flash 2 wait state */ - FLASH->ACR &= (uint32_t) ((uint32_t) ~ FLASH_ACR_LATENCY); - FLASH->ACR |= (uint32_t) FLASH_ACR_LATENCY_2; - + FLASH->ACR &= (uint32_t)((uint32_t)~FLASH_ACR_LATENCY); + FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2; /* HCLK = SYSCLK */ - RCC->CFGR |= (uint32_t) RCC_CFGR_HPRE_DIV1; + RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1; /* PCLK2 = HCLK */ - RCC->CFGR |= (uint32_t) RCC_CFGR_PPRE2_DIV1; + RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1; /* PCLK1 = HCLK */ - RCC->CFGR |= (uint32_t) RCC_CFGR_PPRE1_DIV2; + RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2; #ifdef STM32F10X_CL - /* Configure PLLs ------------------------------------------------------ */ + /* Configure PLLs ------------------------------------------------------ + */ /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */ /* PREDIV1 configuration: PREDIV1CLK = PLL2 / 5 = 8 MHz */ - RCC->CFGR2 &= - (uint32_t) ~ (RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL | + RCC->CFGR2 &= (uint32_t) ~(RCC_CFGR2_PREDIV2 | RCC_CFGR2_PLL2MUL | RCC_CFGR2_PREDIV1 | RCC_CFGR2_PREDIV1SRC); - RCC->CFGR2 |= - (uint32_t) (RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 | + RCC->CFGR2 |= (uint32_t)(RCC_CFGR2_PREDIV2_DIV5 | RCC_CFGR2_PLL2MUL8 | RCC_CFGR2_PREDIV1SRC_PLL2 | RCC_CFGR2_PREDIV1_DIV5); /* Enable PLL2 */ RCC->CR |= RCC_CR_PLL2ON; /* Wait till PLL2 is ready */ - while ((RCC->CR & RCC_CR_PLL2RDY) == 0) { - } - + while ((RCC->CR & RCC_CR_PLL2RDY) == 0) { } /* PLL configuration: PLLCLK = PREDIV1 * 9 = 72 MHz */ - RCC->CFGR &= - (uint32_t) ~ (RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | - RCC_CFGR_PLLMULL); - RCC->CFGR |= - (uint32_t) (RCC_CFGR_PLLXTPRE_PREDIV1 | RCC_CFGR_PLLSRC_PREDIV1 | - RCC_CFGR_PLLMULL9); + RCC->CFGR &= (uint32_t) ~( + RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLSRC | RCC_CFGR_PLLMULL); + RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLXTPRE_PREDIV1 | + RCC_CFGR_PLLSRC_PREDIV1 | RCC_CFGR_PLLMULL9); #else /* PLL configuration: PLLCLK = HSE * 9 = 72 MHz */ - RCC->CFGR &= - (uint32_t) ((uint32_t) ~ (RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | - RCC_CFGR_PLLMULL)); - RCC->CFGR |= (uint32_t) (RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL9); + RCC->CFGR &= (uint32_t)((uint32_t) ~( + RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMULL)); + RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL9); #endif /* STM32F10X_CL */ /* Enable PLL */ RCC->CR |= RCC_CR_PLLON; /* Wait till PLL is ready */ - while ((RCC->CR & RCC_CR_PLLRDY) == 0) { - } + while ((RCC->CR & RCC_CR_PLLRDY) == 0) { } /* Select PLL as system clock source */ - RCC->CFGR &= (uint32_t) ((uint32_t) ~ (RCC_CFGR_SW)); - RCC->CFGR |= (uint32_t) RCC_CFGR_SW_PLL; + RCC->CFGR &= (uint32_t)((uint32_t) ~(RCC_CFGR_SW)); + RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL; /* Wait till PLL is used as system clock source */ - while ((RCC->CFGR & (uint32_t) RCC_CFGR_SWS) != (uint32_t) 0x08) { - } - } else { /* If HSE fails to start-up, the application will have wrong clock - configuration. User can add here some code to deal with this error */ + while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08) { } + } else { /* If HSE fails to start-up, the application will have wrong clock + configuration. User can add here some code to deal with this + error */ } } #endif /** - * @} - */ + * @} + */ /** - * @} - */ + * @} + */ /** - * @} - */ + * @} + */ /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/ diff --git a/ports/uip/bip.c b/ports/uip/bip.c index ae912740..89c5ceb2 100644 --- a/ports/uip/bip.c +++ b/ports/uip/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 "bacnet/bacdcode.h" #include "bacnet/datalink/bip.h" #include "eth.h" -#include "bacport.h" /* custom per port */ +#include "bacport.h" /* custom per port */ static int BIP_Socket = -1; /* port to use - stored in host byte order */ @@ -47,86 +47,75 @@ 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); */ + /* if (bip_valid()) */ + /* close(BIP_Socket); */ BIP_Socket = -1; return; } /* 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 = 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 = 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 */ - BACNET_NPDU_DATA * npdu_data, /* network information */ - uint8_t * pdu, /* any data to be sent - may be null */ +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 */ unsigned pdu_len) -{ /* number of bytes of data */ +{ /* number of bytes of data */ struct sockaddr_in bip_dest; uint8_t mtu[4]; @@ -136,11 +125,11 @@ int bip_send_pdu( IP_HDR iphdr; uint8_t mac[6]; - (void) npdu_data; + (void)npdu_data; /* assumes that the driver has already been initialized */ -/* if (BIP_Socket < 0) */ -/* return BIP_Socket; */ + /* if (BIP_Socket < 0) */ + /* return BIP_Socket; */ mtu[0] = BVLL_TYPE_BACNET_IP; bip_dest.sin_family = AF_INET; @@ -161,9 +150,8 @@ int bip_send_pdu( return -1; mtu_len = 2; - mtu_len += - encode_unsigned16(&mtu[mtu_len], - (uint16_t) (pdu_len + 4 /*inclusive */ )); + 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 */ @@ -182,9 +170,10 @@ int bip_send_pdu( iphdr.destipaddr = bip_dest.sin_addr.s_addr; /* Calculate IP checksum. */ - iphdr.ipchksum = ~(ip_getchksum((uint8_t *) & iphdr)); + iphdr.ipchksum = ~(ip_getchksum((uint8_t *)&iphdr)); - /* Ports are sent in Network byte order. BIP_Port is stored in Host byte order */ + /* 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)); @@ -192,36 +181,33 @@ int bip_send_pdu( udphdr.udpchksum = 0; EthernetSendHeader(mac, UIP_ETHTYPE_IP); - EthernetSend((uint8_t *) & iphdr, sizeof(IP_HDR)); - EthernetSend((uint8_t *) & udphdr, sizeof(UDP_HDR)); + 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 */ +{ /* 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_unsigned16(&my_address->mac[4], htons(BIP_Port)); - my_address->net = 0; /* local only, no routing */ - my_address->len = 0; /* no SLEN */ + (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 */ for (i = 0; i < MAX_MAC_LEN; i++) { /* no SADR */ my_address->adr[i] = 0; @@ -230,18 +216,17 @@ void bip_get_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; - (void) encode_unsigned32(&dest->mac[0], - htonl(BIP_Broadcast_Address.s_addr)); - (void) encode_unsigned16(&dest->mac[4], htons(BIP_Port)); + (void)encode_unsigned32( + &dest->mac[0], 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/ports/win32/bip-init.c b/ports/win32/bip-init.c index 158a9305..e9495e56 100644 --- a/ports/win32/bip-init.c +++ b/ports/win32/bip-init.c @@ -35,8 +35,8 @@ #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 "bacnet/bacdcode.h" #include "bacnet/bacint.h" #include "bacnet/config.h" @@ -75,8 +75,10 @@ static bool BIP_Debug = false; * @param str - debug info string * @param addr - IPv4 address */ -static void debug_print_ipv4(const char *str, const struct in_addr *addr, - const unsigned int port, const unsigned int count) +static void debug_print_ipv4(const char *str, + const struct in_addr *addr, + const unsigned int port, + const unsigned int count) { if (BIP_Debug) { fprintf(stderr, "BIP: %s %s:%hu (%u bytes)\n", str, inet_ntoa(*addr), @@ -96,8 +98,7 @@ void bip_debug_enable(void) /** * @brief Get the text string for Windows Error Codes */ -static char *winsock_error_code_text( - int code) +static char *winsock_error_code_text(int code) { switch (code) { case WSAEACCES: @@ -116,9 +117,9 @@ static char *winsock_error_code_text( 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."; + "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: @@ -145,9 +146,9 @@ static char *winsock_error_code_text( 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."; + "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: @@ -180,34 +181,35 @@ static char *winsock_error_code_text( 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."; + return "Host 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."; + "This error indicates that the key " + "(name, address, and so on) was not found."; default: return "unknown"; } @@ -219,8 +221,8 @@ static char *winsock_error_code_text( static void print_last_error(const char *info) { int Code = WSAGetLastError(); - fprintf(stderr, "BIP: %s [error code %i] %s\n", - info, Code, winsock_error_code_text(Code)); + fprintf(stderr, "BIP: %s [error code %i] %s\n", info, Code, + winsock_error_code_text(Code)); fflush(stderr); } @@ -392,7 +394,7 @@ uint8_t bip_get_subnet_prefix(void) if (test_broadcast == broadcast) { break; } - mask = mask<<1; + mask = mask << 1; } return prefix; @@ -427,8 +429,8 @@ int bip_send_mpdu(BACNET_IP_ADDRESS *dest, uint8_t *mtu, uint16_t mtu_len) memcpy(&bip_dest.sin_addr.s_addr, &dest->address[0], 4); bip_dest.sin_port = htons(dest->port); /* Send the packet */ - debug_print_ipv4("Sending MPDU->", &bip_dest.sin_addr, bip_dest.sin_port, - mtu_len); + debug_print_ipv4( + "Sending MPDU->", &bip_dest.sin_addr, bip_dest.sin_port, mtu_len); rv = sendto(BIP_Socket, (char *)mtu, mtu_len, 0, (struct sockaddr *)&bip_dest, sizeof(struct sockaddr)); if (rv == SOCKET_ERROR) { @@ -506,8 +508,8 @@ uint16_t bip_receive( */ memcpy(&addr.address[0], &sin.sin_addr.s_addr, 4); addr.port = ntohs(sin.sin_port); - debug_print_ipv4("Received MPDU->", &sin.sin_addr, sin.sin_port, - received_bytes); + debug_print_ipv4( + "Received MPDU->", &sin.sin_addr, sin.sin_port, received_bytes); /* pass the packet into the BBMD handler */ offset = bvlc_handler(&addr, src, npdu, received_bytes); if (offset > 0) { @@ -575,8 +577,7 @@ bool bip_get_addr_by_name(const char *host_name, BACNET_IP_ADDRESS *addr) } /* 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]; @@ -591,20 +592,19 @@ static long gethostaddr( } if (BIP_Debug) { fprintf(stderr, "BIP: host %s at %u.%u.%u.%u\n", host_name, - (unsigned) ((uint8_t *) host_ent->h_addr)[0], - (unsigned) ((uint8_t *) host_ent->h_addr)[1], - (unsigned) ((uint8_t *) host_ent->h_addr)[2], - (unsigned) ((uint8_t *) host_ent->h_addr)[3]); + (unsigned)((uint8_t *)host_ent->h_addr)[0], + (unsigned)((uint8_t *)host_ent->h_addr)[1], + (unsigned)((uint8_t *)host_ent->h_addr)[2], + (unsigned)((uint8_t *)host_ent->h_addr)[3]); fflush(stderr); } /* note: network byte order */ - return *(long *) host_ent->h_addr; + return *(long *)host_ent->h_addr; } #if ((USE_INADDR == 0) || (USE_CLASSADDR == 0)) /* returns the subnet mask in network byte order */ -static uint32_t getIpMaskForIpAddress( - uint32_t ipAddress) +static uint32_t getIpMaskForIpAddress(uint32_t ipAddress) { /* Allocate information for up to 16 NICs */ IP_ADAPTER_INFO AdapterInfo[16]; @@ -618,8 +618,7 @@ static uint32_t getIpMaskForIpAddress( /* GetAdapterInfo: [out] buffer to receive data [in] size of receive data buffer */ - DWORD dwStatus = GetAdaptersInfo(AdapterInfo, - &dwBufLen); + DWORD dwStatus = GetAdaptersInfo(AdapterInfo, &dwBufLen); if (dwStatus == ERROR_SUCCESS) { /* Verify return value is valid, no buffer overflow Contains pointer to current adapter info */ @@ -648,8 +647,7 @@ static uint32_t getIpMaskForIpAddress( } #endif -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 @@ -698,8 +696,7 @@ static void set_broadcast_address( * @param ifname [in] The named interface to use for the network layer. * Eg, for Windows, ifname is the dotted ip address of the interface */ -void bip_set_interface( - char *ifname) +void bip_set_interface(char *ifname) { bip_init_windows(); /* setup local address */ @@ -734,8 +731,7 @@ void bip_set_interface( * @return True if the socket is successfully opened for BACnet/IP, * else False if the socket functions fail. */ -bool bip_init( - char *ifname) +bool bip_init(char *ifname) { int rv = 0; /* return from socket lib calls */ struct sockaddr_in sin = { -1 }; @@ -771,8 +767,8 @@ bool bip_init( } /* Allow us to use the same socket for sending and receiving */ /* This makes sure that the src port is correct when sending */ - rv = setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, (char *) &value, - sizeof(value)); + rv = setsockopt( + sock_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&value, sizeof(value)); if (rv < 0) { print_last_error("failed to set REUSEADDR socket option"); closesocket(sock_fd); @@ -780,8 +776,8 @@ bool bip_init( return false; } /* Enables transmission and receipt of broadcast messages on the socket. */ - rv = setsockopt(sock_fd, SOL_SOCKET, SO_BROADCAST, (char *) &value, - sizeof(value)); + rv = setsockopt( + sock_fd, SOL_SOCKET, SO_BROADCAST, (char *)&value, sizeof(value)); if (rv < 0) { print_last_error("failed to set BROADCAST socket option"); closesocket(sock_fd); @@ -816,8 +812,7 @@ bool bip_init( ntohs(sin.sin_port)); fflush(stderr); } - rv = bind(sock_fd, (const struct sockaddr *) &sin, - sizeof(struct sockaddr)); + rv = bind(sock_fd, (const struct sockaddr *)&sin, sizeof(struct sockaddr)); if (rv < 0) { print_last_error("failed to bind"); closesocket(sock_fd); @@ -839,9 +834,8 @@ bool bip_valid(void) /** Cleanup and close out the BACnet/IP services by closing the socket. * @ingroup DLBIP - */ -void bip_cleanup( - void) + */ +void bip_cleanup(void) { SOCKET sock_fd = 0; diff --git a/ports/win32/bip6.c b/ports/win32/bip6.c index 55742385..8496bcb0 100644 --- a/ports/win32/bip6.c +++ b/ports/win32/bip6.c @@ -34,8 +34,8 @@ #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 "bacnet/bacdcode.h" #include "bacnet/config.h" #include "bacnet/basic/sys/debug.h" @@ -50,16 +50,16 @@ static SOCKET BIP6_Socket = INVALID_SOCKET; static BACNET_IP6_ADDRESS BIP6_Addr; static BACNET_IP6_ADDRESS BIP6_Broadcast_Addr; -static void debug_print_ipv6(const char *str, const struct in6_addr * addr) { - debug_printf( "BIP6: %s %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x\n", - str, - (int)addr->s6_addr[0], (int)addr->s6_addr[1], - (int)addr->s6_addr[2], (int)addr->s6_addr[3], - (int)addr->s6_addr[4], (int)addr->s6_addr[5], - (int)addr->s6_addr[6], (int)addr->s6_addr[7], - (int)addr->s6_addr[8], (int)addr->s6_addr[9], - (int)addr->s6_addr[10], (int)addr->s6_addr[11], - (int)addr->s6_addr[12], (int)addr->s6_addr[13], +static void debug_print_ipv6(const char *str, const struct in6_addr *addr) +{ + debug_printf("BIP6: %s " + "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%" + "02x:%02x%02x\n", + str, (int)addr->s6_addr[0], (int)addr->s6_addr[1], + (int)addr->s6_addr[2], (int)addr->s6_addr[3], (int)addr->s6_addr[4], + (int)addr->s6_addr[5], (int)addr->s6_addr[6], (int)addr->s6_addr[7], + (int)addr->s6_addr[8], (int)addr->s6_addr[9], (int)addr->s6_addr[10], + (int)addr->s6_addr[11], (int)addr->s6_addr[12], (int)addr->s6_addr[13], (int)addr->s6_addr[14], (int)addr->s6_addr[15]); } @@ -72,19 +72,18 @@ static LPSTR PrintError(int ErrorCode) // (And of course, free the buffer when we were done with it) FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | - FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, ErrorCode, - MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPSTR) Message, 1024, NULL); + FORMAT_MESSAGE_MAX_WIDTH_MASK, + NULL, ErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + (LPSTR)Message, 1024, NULL); return Message; } /* on Windows, ifname is the IPv6 address of the interface */ -void bip6_set_interface( - char *ifname) +void bip6_set_interface(char *ifname) { int i, RetVal; struct addrinfo Hints, *AddrInfo, *AI; - struct sockaddr_in6* sin; + struct sockaddr_in6 *sin; struct sockaddr_in6 server; struct in6_addr broadcast_address; struct ipv6_mreq join_request; @@ -99,7 +98,7 @@ void bip6_set_interface( // entry per allowed protocol family containing the unspecified address // for that family. // - memset(&Hints, 0, sizeof (Hints)); + memset(&Hints, 0, sizeof(Hints)); Hints.ai_family = PF_INET6; Hints.ai_socktype = SOCK_DGRAM; Hints.ai_protocol = IPPROTO_UDP; @@ -108,8 +107,8 @@ void bip6_set_interface( debug_printf("BIP6: getaddrinfo - IPv6 address %s port %s\n", ifname, port); RetVal = getaddrinfo(ifname, &port[0], &Hints, &AddrInfo); if (RetVal != 0) { - fprintf(stderr, "BIP6: getaddrinfo failed with error %d: %s\n", - RetVal, gai_strerror(RetVal)); + fprintf(stderr, "BIP6: getaddrinfo failed with error %d: %s\n", RetVal, + gai_strerror(RetVal)); WSACleanup(); return; } @@ -122,7 +121,8 @@ void bip6_set_interface( // Highly unlikely, but check anyway. if (i == FD_SETSIZE) { fprintf(stderr, - "BIP6: getaddrinfo returned more addresses than we could use.\n"); + "BIP6: getaddrinfo returned more addresses than we could " + "use.\n"); break; } // only support PF_INET6. @@ -140,20 +140,18 @@ void bip6_set_interface( BIP6_Socket = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP); if (BIP6_Socket == INVALID_SOCKET) { fprintf(stderr, "BIP6: socket() failed with error %d: %s\n", - WSAGetLastError(), PrintError(WSAGetLastError())); + WSAGetLastError(), PrintError(WSAGetLastError())); continue; } - if ((AI->ai_family == PF_INET6) && - IN6_IS_ADDR_LINKLOCAL(AI->ai_addr) && - (((SOCKADDR_IN6 *) (AI->ai_addr))->sin6_scope_id == 0)) { - fprintf(stderr, - "BIP6: IPv6 link local addresses needs a scope ID!\n"); + if ((AI->ai_family == PF_INET6) && IN6_IS_ADDR_LINKLOCAL(AI->ai_addr) && + (((SOCKADDR_IN6 *)(AI->ai_addr))->sin6_scope_id == 0)) { + fprintf( + stderr, "BIP6: IPv6 link local addresses needs a scope ID!\n"); } /* Allow us to use the same socket for sending and receiving */ /* This makes sure that the src port is correct when sending */ sockopt = 1; - RetVal = - setsockopt(BIP6_Socket, SOL_SOCKET, SO_REUSEADDR, + RetVal = setsockopt(BIP6_Socket, SOL_SOCKET, SO_REUSEADDR, (char *)&sockopt, sizeof(sockopt)); if (RetVal < 0) { closesocket(BIP6_Socket); @@ -161,13 +159,13 @@ void bip6_set_interface( continue; } /* allow us to send a broadcast */ - RetVal = - setsockopt(BIP6_Socket, SOL_SOCKET, SO_BROADCAST, + RetVal = setsockopt(BIP6_Socket, SOL_SOCKET, SO_BROADCAST, (char *)&sockopt, sizeof(sockopt)); if (RetVal < 0) { closesocket(BIP6_Socket); BIP6_Socket = INVALID_SOCKET; - fprintf(stderr, "BIP6: setsockopt(SO_BROADCAST) failed " + fprintf(stderr, + "BIP6: setsockopt(SO_BROADCAST) failed " "with error %d: %s\n", WSAGetLastError(), PrintError(WSAGetLastError())); continue; @@ -182,7 +180,8 @@ void bip6_set_interface( RetVal = setsockopt(BIP6_Socket, IPPROTO_IPV6, IPV6_JOIN_GROUP, (char *)&join_request, sizeof(join_request)); if (RetVal < 0) { - fprintf(stderr, "BIP6: setsockopt(IPV6_JOIN_GROUP) failed " + fprintf(stderr, + "BIP6: setsockopt(IPV6_JOIN_GROUP) failed " "with error %d: %s\n", WSAGetLastError(), PrintError(WSAGetLastError())); } @@ -196,16 +195,15 @@ void bip6_set_interface( server.sin6_family = PF_INET6; server.sin6_port = htons(BIP6_Addr.port); server.sin6_addr = in6addr_any; - if (bind(BIP6_Socket, (struct sockaddr *) &server, sizeof(server)) == + if (bind(BIP6_Socket, (struct sockaddr *)&server, sizeof(server)) == SOCKET_ERROR) { fprintf(stderr, "BIP6: bind() failed with error %d: %s\n", - WSAGetLastError(), PrintError(WSAGetLastError())); + WSAGetLastError(), PrintError(WSAGetLastError())); closesocket(ServSock[i]); continue; } else { - sin = (struct sockaddr_in6*)AI->ai_addr; - bvlc6_address_set(&BIP6_Addr, - ntohs(sin->sin6_addr.s6_addr16[0]), + sin = (struct sockaddr_in6 *)AI->ai_addr; + bvlc6_address_set(&BIP6_Addr, ntohs(sin->sin6_addr.s6_addr16[0]), ntohs(sin->sin6_addr.s6_addr16[1]), ntohs(sin->sin6_addr.s6_addr16[2]), ntohs(sin->sin6_addr.s6_addr16[3]), @@ -213,7 +211,8 @@ void bip6_set_interface( ntohs(sin->sin6_addr.s6_addr16[5]), ntohs(sin->sin6_addr.s6_addr16[6]), ntohs(sin->sin6_addr.s6_addr16[7])); - /* https://msdn.microsoft.com/en-us/library/windows/desktop/ms740496(v=vs.85).aspx */ + /* https://msdn.microsoft.com/en-us/library/windows/desktop/ms740496(v=vs.85).aspx + */ } i++; if (BIP6_Socket != INVALID_SOCKET) { @@ -228,8 +227,7 @@ void bip6_set_interface( * * @param port - IPv6 UDP port number */ -void bip6_set_port( - uint16_t port) +void bip6_set_port(uint16_t port) { BIP6_Addr.port = port; BIP6_Broadcast_Addr.port = port; @@ -240,8 +238,7 @@ void bip6_set_port( * * @return IPv6 UDP port number */ -uint16_t bip6_get_port( - void) +uint16_t bip6_get_port(void) { return BIP6_Addr.port; } @@ -252,8 +249,7 @@ uint16_t bip6_get_port( * * @param addr - IPv6 source address */ -void bip6_get_broadcast_address( - BACNET_ADDRESS * addr) +void bip6_get_broadcast_address(BACNET_ADDRESS *addr) { if (addr) { addr->net = BACNET_BROADCAST_NETWORK; @@ -267,8 +263,7 @@ void bip6_get_broadcast_address( * * @param addr - IPv6 source address */ -void bip6_get_my_address( - BACNET_ADDRESS * addr) +void bip6_get_my_address(BACNET_ADDRESS *addr) { uint32_t device_id = 0; @@ -283,8 +278,7 @@ void bip6_get_my_address( * * @param addr - network IPv6 address */ -bool bip6_set_addr( - BACNET_IP6_ADDRESS *addr) +bool bip6_set_addr(BACNET_IP6_ADDRESS *addr) { return bvlc6_address_copy(&BIP6_Addr, addr); } @@ -294,13 +288,11 @@ bool bip6_set_addr( * * @return BACnet/IP address */ -bool bip6_get_addr( - BACNET_IP6_ADDRESS *addr) +bool bip6_get_addr(BACNET_IP6_ADDRESS *addr) { return bvlc6_address_copy(addr, &BIP6_Addr); } - /** * Determine if the BACnet/IPv6 address matches our own address * @@ -308,8 +300,7 @@ bool bip6_get_addr( * * @return true if the BACnet/IPv6 address matches our own address */ -bool bip6_address_match_self( - BACNET_IP6_ADDRESS *addr) +bool bip6_address_match_self(BACNET_IP6_ADDRESS *addr) { return !bvlc6_address_different(addr, &BIP6_Addr); } @@ -319,8 +310,7 @@ bool bip6_address_match_self( * * @param addr - network IPv6 address */ -bool bip6_set_broadcast_addr( - BACNET_IP6_ADDRESS *addr) +bool bip6_set_broadcast_addr(BACNET_IP6_ADDRESS *addr) { return bvlc6_address_copy(&BIP6_Broadcast_Addr, addr); } @@ -330,8 +320,7 @@ bool bip6_set_broadcast_addr( * * @return BACnet/IP address */ -bool bip6_get_broadcast_addr( - BACNET_IP6_ADDRESS *addr) +bool bip6_get_broadcast_addr(BACNET_IP6_ADDRESS *addr) { return bvlc6_address_copy(addr, &BIP6_Broadcast_Addr); } @@ -347,10 +336,7 @@ bool bip6_get_broadcast_addr( * @return Upon successful completion, returns the number of bytes sent. * Otherwise, -1 shall be returned and errno set to indicate the error. */ -int bip6_send_mpdu( - BACNET_IP6_ADDRESS *dest, - uint8_t * mtu, - uint16_t mtu_len) +int bip6_send_mpdu(BACNET_IP6_ADDRESS *dest, uint8_t *mtu, uint16_t mtu_len) { struct sockaddr_in6 bvlc_dest = { 0 }; uint16_t addr16[8]; @@ -374,8 +360,8 @@ int bip6_send_mpdu( bvlc_dest.sin6_port = htons(dest->port); debug_print_ipv6("BIP6: Sending MPDU->", &bvlc_dest.sin6_addr); /* Send the packet */ - return sendto(BIP6_Socket, (char *) mtu, mtu_len, 0, - (struct sockaddr *) &bvlc_dest, sizeof(bvlc_dest)); + return sendto(BIP6_Socket, (char *)mtu, mtu_len, 0, + (struct sockaddr *)&bvlc_dest, sizeof(bvlc_dest)); } /** @@ -409,17 +395,14 @@ int bip6_send_pdu(BACNET_ADDRESS *dest, * @return Number of bytes received, or 0 if none or timeout. */ uint16_t bip6_receive( - BACNET_ADDRESS * src, - uint8_t * npdu, - uint16_t max_npdu, - unsigned timeout) + BACNET_ADDRESS *src, uint8_t *npdu, uint16_t max_npdu, unsigned timeout) { uint16_t npdu_len = 0; /* return value */ fd_set read_fds; int max = 0; struct timeval select_timeout; struct sockaddr_in6 sin = { 0 }; - BACNET_IP6_ADDRESS addr = {{ 0 }}; + BACNET_IP6_ADDRESS addr = { { 0 } }; socklen_t sin_len = sizeof(sin); int received_bytes = 0; int offset = 0; @@ -445,9 +428,8 @@ uint16_t bip6_receive( max = BIP6_Socket; /* see if there is a packet for us */ if (select(max + 1, &read_fds, NULL, NULL, &select_timeout) > 0) { - received_bytes = - recvfrom(BIP6_Socket, (char *) &npdu[0], max_npdu, 0, - (struct sockaddr *) &sin, &sin_len); + received_bytes = recvfrom(BIP6_Socket, (char *)&npdu[0], max_npdu, 0, + (struct sockaddr *)&sin, &sin_len); } else { return 0; } @@ -484,9 +466,8 @@ uint16_t bip6_receive( /** Cleanup and close out the BACnet/IP services by closing the socket. * @ingroup DLBIP6 - */ -void bip6_cleanup( - void) + */ +void bip6_cleanup(void) { if (BIP6_Socket != -1) { close(BIP6_Socket); @@ -516,16 +497,15 @@ void bip6_cleanup( * @return True if the socket is successfully opened for BACnet/IPv6, * else False if the socket functions fail. */ -bool bip6_init( - char *ifname) +bool bip6_init(char *ifname) { WSADATA wd; int RetVal; // Ask for Winsock version 2.2. if ((RetVal = WSAStartup(MAKEWORD(2, 2), &wd)) != 0) { - fprintf(stderr, "BIP6: WSAStartup failed with error %d: %s\n", - RetVal, PrintError(RetVal)); + fprintf(stderr, "BIP6: WSAStartup failed with error %d: %s\n", RetVal, + PrintError(RetVal)); WSACleanup(); exit(1); } @@ -535,9 +515,8 @@ bool bip6_init( debug_printf("BIP6: IPv6 UDP port: 0x%04X\n", BIP6_Addr.port); bip6_set_interface(ifname); if (BIP6_Broadcast_Addr.address[0] == 0) { - bvlc6_address_set(&BIP6_Broadcast_Addr, - BIP6_MULTICAST_LINK_LOCAL, 0, 0, 0, 0, 0, 0, - BIP6_MULTICAST_GROUP_ID); + bvlc6_address_set(&BIP6_Broadcast_Addr, BIP6_MULTICAST_LINK_LOCAL, 0, 0, + 0, 0, 0, 0, BIP6_MULTICAST_GROUP_ID); } if (BIP6_Socket == INVALID_SOCKET) { fprintf(stderr, "BIP6: Fatal error: unable to serve on any address.\n"); diff --git a/ports/win32/datetime-init.c b/ports/win32/datetime-init.c index 76825a1e..554f7f80 100644 --- a/ports/win32/datetime-init.c +++ b/ports/win32/datetime-init.c @@ -21,9 +21,9 @@ /* Offset between Windows epoch 1/1/1601 and Unix epoch 1/1/1970 in 100 nanosec units */ #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) || defined(__BORLANDC__) -#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 +#define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 #else -#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL +#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL #endif #if defined(__BORLANDC__) || defined(_WIN32) @@ -35,21 +35,19 @@ long int timezone; #if defined(_MSC_VER) || defined(__BORLANDC__) struct timezone { int tz_minuteswest; /* minutes W of Greenwich */ - int tz_dsttime; /* type of dst correction */ + int tz_dsttime; /* type of dst correction */ }; /************************************************************************* -* Description: simulate the gettimeofday Linux function -* Returns: zero -* Note: The resolution of GetSystemTimeAsFileTime() is 15625 microseconds. -* The resolution of _ftime() is about 16 milliseconds. -* To get microseconds accuracy we need to use QueryPerformanceCounter or -* timeGetTime for the elapsed time. -*************************************************************************/ + * Description: simulate the gettimeofday Linux function + * Returns: zero + * Note: The resolution of GetSystemTimeAsFileTime() is 15625 microseconds. + * The resolution of _ftime() is about 16 milliseconds. + * To get microseconds accuracy we need to use QueryPerformanceCounter or + * timeGetTime for the elapsed time. + *************************************************************************/ -int gettimeofday( - struct timeval *tp, - void *tzp) +int gettimeofday(struct timeval *tp, void *tzp) { static int tzflag = 0; struct timezone *tz; @@ -71,16 +69,16 @@ int gettimeofday( usec_timer <<= 32; usec_timer |= ft.dwLowDateTime; /*converting file time to unix epoch 1970 */ - usec_timer /= 10; /*convert into microseconds */ + usec_timer /= 10; /*convert into microseconds */ usec_timer -= DELTA_EPOCH_IN_MICROSECS; - tp->tv_sec = (long) (usec_timer / 1000000UL); - tp->tv_usec = (long) (usec_timer % 1000000UL); + tp->tv_sec = (long)(usec_timer / 1000000UL); + tp->tv_usec = (long)(usec_timer % 1000000UL); time_start = timeGetTime(); } else { elapsed_milliseconds = timeGetTime() - time_start; - usec_elapsed = usec_timer + ((LONGLONG) elapsed_milliseconds * 1000UL); - tp->tv_sec = (long) (usec_elapsed / 1000000UL); - tp->tv_usec = (long) (usec_elapsed % 1000000UL); + usec_elapsed = usec_timer + ((LONGLONG)elapsed_milliseconds * 1000UL); + tp->tv_sec = (long)(usec_elapsed / 1000000UL); + tp->tv_usec = (long)(usec_elapsed % 1000000UL); } if (tzp) { if (!tzflag) { @@ -105,11 +103,10 @@ int gettimeofday( * @param true if DST is enabled and active * @return true if local time was retrieved */ -bool datetime_local( - BACNET_DATE * bdate, - BACNET_TIME * btime, - int16_t * utc_offset_minutes, - bool * dst_active) +bool datetime_local(BACNET_DATE *bdate, + BACNET_TIME *btime, + int16_t *utc_offset_minutes, + bool *dst_active) { bool status = false; struct tm *tblock = NULL; @@ -141,15 +138,14 @@ bool datetime_local( * int tm_isdst Daylight Savings flag. */ datetime_set_date(bdate, (uint16_t)tblock->tm_year + 1900, - (uint8_t)tblock->tm_mon + 1, - (uint8_t)tblock->tm_mday); + (uint8_t)tblock->tm_mon + 1, (uint8_t)tblock->tm_mday); #if !defined(_MSC_VER) datetime_set_time(btime, (uint8_t)tblock->tm_hour, - (uint8_t)tblock->tm_min, (uint8_t)tblock->tm_sec, - (uint8_t)(tv.tv_usec / 10000)); + (uint8_t)tblock->tm_min, (uint8_t)tblock->tm_sec, + (uint8_t)(tv.tv_usec / 10000)); #else datetime_set_time(btime, (uint8_t)tblock->tm_hour, - (uint8_t)tblock->tm_min, (uint8_t)tblock->tm_sec, 0); + (uint8_t)tblock->tm_min, (uint8_t)tblock->tm_sec, 0); #endif if (dst_active) { /* The value of tm_isdst is: diff --git a/ports/win32/dlmstp-mm.c b/ports/win32/dlmstp-mm.c index 95009b09..93ffb3a2 100644 --- a/ports/win32/dlmstp-mm.c +++ b/ports/win32/dlmstp-mm.c @@ -1,28 +1,28 @@ /************************************************************************** -* -* Copyright (C) 2006 Steve Karg -* Multimedia Timer contribution by Cameron Crothers, 2008 -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2006 Steve Karg + * Multimedia Timer contribution by Cameron Crothers, 2008 + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include #include @@ -40,7 +40,7 @@ #define WIN32_LEAN_AND_MEAN #define STRICT 1 #include -#include /* for multimedia timers */ +#include /* for multimedia timers */ /* Number of MS/TP Packets Rx/Tx */ uint16_t MSTP_Packets = 0; @@ -62,8 +62,7 @@ static uint32_t TimeBeginPeriod; /* 1-millisecond target resolution */ #define TARGET_RESOLUTION 1 -static uint16_t Timer_Silence( - void) +static uint16_t Timer_Silence(void) { uint32_t now = timeGetTime(); uint32_t delta_time = 0; @@ -77,17 +76,15 @@ static uint16_t Timer_Silence( delta_time = 0xFFFF; } - return (uint16_t) delta_time; + return (uint16_t)delta_time; } -static void Timer_Silence_Reset( - void) +static void Timer_Silence_Reset(void) { SilenceStartTime = timeGetTime(); } -void dlmstp_reinit( - void) +void dlmstp_reinit(void) { /*RS485_Reinit(); */ dlmstp_set_mac_address(DEFAULT_MAC_ADDRESS); @@ -97,8 +94,7 @@ void dlmstp_reinit( timeEndPeriod(TimeBeginPeriod); } -void dlmstp_cleanup( - void) +void dlmstp_cleanup(void) { /* nothing to do for static buffers */ if (Received_Frame_Flag) { @@ -110,19 +106,17 @@ void dlmstp_cleanup( } /* 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 */ +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 */ +{ /* number of bytes of data */ int bytes_sent = 0; unsigned i = 0; if (!Transmit_Packet.ready) { if (npdu_data->data_expecting_reply) { - Transmit_Packet.frame_type = - FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY; + Transmit_Packet.frame_type = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY; } else { Transmit_Packet.frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; @@ -139,16 +133,15 @@ int dlmstp_send_pdu( return bytes_sent; } -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) -{ /* milliseconds to wait for a packet */ +{ /* 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); @@ -161,8 +154,8 @@ uint16_t dlmstp_receive( sizeof(Receive_Packet.address)); } if (pdu) { - memmove(pdu, &Receive_Packet.pdu, - sizeof(Receive_Packet.pdu)); + memmove( + pdu, &Receive_Packet.pdu, sizeof(Receive_Packet.pdu)); } pdu_len = Receive_Packet.pdu_len; } @@ -173,14 +166,12 @@ 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) && @@ -199,14 +190,12 @@ static void dlmstp_receive_fsm_task( } } -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: @@ -228,9 +217,7 @@ static void dlmstp_master_fsm_task( } } -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; @@ -254,8 +241,7 @@ void dlmstp_fill_bacnet_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) +uint16_t MSTP_Put_Receive(volatile struct mstp_port_struct_t *mstp_port) { uint16_t pdu_len = 0; BOOL rc; @@ -265,10 +251,10 @@ 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); - dlmstp_fill_bacnet_address(&Receive_Packet.address, - mstp_port->SourceAddress); + 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; Receive_Packet.ready = true; rc = ReleaseSemaphore(Receive_Packet_Flag, 1, NULL); @@ -280,13 +266,12 @@ 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 */ + uint8_t destination = 0; /* destination address */ - (void) timeout; + (void)timeout; if (!Transmit_Packet.ready) { return 0; } @@ -301,22 +286,22 @@ 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 */ - mstp_port->OutputBufferSize, Transmit_Packet.frame_type, destination, - mstp_port->This_Station, &Transmit_Packet.pdu[0], - Transmit_Packet.pdu_len); + 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.ready = false; return pdu_len; } -bool dlmstp_compare_data_expecting_reply( - uint8_t * request_pdu, +bool dlmstp_compare_data_expecting_reply(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 @@ -338,9 +323,8 @@ 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], NULL, &request.address, - &request.npdu_data); + offset = npdu_decode( + &request_pdu[0], NULL, &request.address, &request.npdu_data); if (request.npdu_data.network_layer_message) { return false; } @@ -356,8 +340,7 @@ bool dlmstp_compare_data_expecting_reply( request.service_choice = request_pdu[offset + 3]; /* decode the reply data */ bacnet_address_copy(&reply.address, dest_address); - offset = - npdu_decode(&reply_pdu[0], &reply.address, NULL, &reply.npdu_data); + offset = npdu_decode(&reply_pdu[0], &reply.address, NULL, &reply.npdu_data); if (reply.npdu_data.network_layer_message) { return false; } @@ -402,7 +385,8 @@ bool dlmstp_compare_data_expecting_reply( return false; } } - if (request.npdu_data.protocol_version != reply.npdu_data.protocol_version) { + if (request.npdu_data.protocol_version != + reply.npdu_data.protocol_version) { return false; } #if 0 @@ -421,14 +405,13 @@ 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 */ - uint8_t destination = 0; /* destination address */ + 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; } @@ -442,25 +425,24 @@ 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); if (!matched) return 0; /* convert the PDU into the MSTP Frame */ - 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); + 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.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) { @@ -477,8 +459,7 @@ void dlmstp_set_mac_address( return; } -uint8_t dlmstp_mac_address( - void) +uint8_t dlmstp_mac_address(void) { return MSTP_Port.This_Station; } @@ -490,8 +471,7 @@ uint8_t dlmstp_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( - 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; @@ -505,8 +485,7 @@ void dlmstp_set_max_info_frames( return; } -uint8_t dlmstp_max_info_frames( - void) +uint8_t dlmstp_max_info_frames(void) { return MSTP_Port.Nmax_info_frames; } @@ -516,8 +495,7 @@ uint8_t dlmstp_max_info_frames( /* 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) { @@ -533,33 +511,29 @@ void dlmstp_set_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(); } -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; - my_address->net = 0; /* local only, no routing */ + my_address->net = 0; /* local only, no routing */ my_address->len = 0; for (i = 0; i < MAX_MAC_LEN; i++) { my_address->adr[i] = 0; @@ -568,16 +542,15 @@ void dlmstp_get_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; } @@ -586,8 +559,7 @@ void dlmstp_get_broadcast_address( return; } -bool dlmstp_init( - char *ifname) +bool dlmstp_init(char *ifname) { unsigned long hThread = 0; uint32_t arg_value = 0; @@ -658,8 +630,7 @@ bool dlmstp_init( if (timeGetDevCaps(&tc, sizeof(TIMECAPS)) != TIMERR_NOERROR) { fprintf(stderr, "Failed to set timer resolution\n"); } - TimeBeginPeriod = - min(max(tc.wPeriodMin, TARGET_RESOLUTION), tc.wPeriodMax); + TimeBeginPeriod = min(max(tc.wPeriodMin, TARGET_RESOLUTION), tc.wPeriodMax); timeBeginPeriod(TimeBeginPeriod); /* start the threads */ @@ -678,19 +649,17 @@ bool dlmstp_init( #ifdef TEST_DLMSTP #include -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 */ - (void) src; - (void) apdu; - (void) 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; @@ -705,9 +674,7 @@ uint32_t timestamp_ms( static char *Network_Interface = "COM3"; -int main( - int argc, - char *argv[]) +int main(int argc, char *argv[]) { uint16_t pdu_len = 0; diff --git a/ports/win32/dlmstp.c b/ports/win32/dlmstp.c index 6a8339dc..c05d6e2b 100644 --- a/ports/win32/dlmstp.c +++ b/ports/win32/dlmstp.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2006 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2006 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include @@ -69,20 +69,17 @@ static uint8_t Tusage_timeout = 30; static struct mstimer Silence_Timer; /* Timer that indicates line silence - and functions */ -static uint32_t Timer_Silence( - void *pArg) +static uint32_t Timer_Silence(void *pArg) { return mstimer_elapsed(&Silence_Timer); } -static void Timer_Silence_Reset( - void *pArg) +static void Timer_Silence_Reset(void *pArg) { mstimer_set(&Silence_Timer, 0); } -void dlmstp_cleanup( - void) +void dlmstp_cleanup(void) { /* nothing to do for static buffers */ if (Received_Frame_Flag) { @@ -94,24 +91,22 @@ void dlmstp_cleanup( } /* 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 */ +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 */ +{ /* number of bytes of data */ int bytes_sent = 0; unsigned i = 0; if (!Transmit_Packet.ready) { if (npdu_data->data_expecting_reply) { - Transmit_Packet.frame_type = - FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY; + Transmit_Packet.frame_type = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY; } else { Transmit_Packet.frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; } - Transmit_Packet.pdu_len = (uint16_t) pdu_len; + Transmit_Packet.pdu_len = (uint16_t)pdu_len; for (i = 0; i < pdu_len; i++) { Transmit_Packet.pdu[i] = pdu[i]; } @@ -123,16 +118,15 @@ int dlmstp_send_pdu( return bytes_sent; } -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) -{ /* milliseconds to wait for a packet */ +{ /* 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); @@ -145,8 +139,8 @@ uint16_t dlmstp_receive( sizeof(Receive_Packet.address)); } if (pdu) { - memmove(pdu, &Receive_Packet.pdu, - sizeof(Receive_Packet.pdu)); + memmove( + pdu, &Receive_Packet.pdu, sizeof(Receive_Packet.pdu)); } pdu_len = Receive_Packet.pdu_len; } @@ -157,14 +151,12 @@ 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); for (;;) { /* only do receive state machine while we don't have a frame */ if ((MSTP_Port.ReceivedValidFrame == false) && @@ -183,14 +175,12 @@ static void dlmstp_receive_fsm_task( } } -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); for (;;) { switch (MSTP_Port.master_state) { case MSTP_MASTER_STATE_IDLE: @@ -212,9 +202,7 @@ static void dlmstp_master_fsm_task( } } -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; @@ -238,8 +226,7 @@ void dlmstp_fill_bacnet_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) +uint16_t MSTP_Put_Receive(volatile struct mstp_port_struct_t *mstp_port) { uint16_t pdu_len = 0; BOOL rc; @@ -249,14 +236,14 @@ 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); - dlmstp_fill_bacnet_address(&Receive_Packet.address, - mstp_port->SourceAddress); + 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; Receive_Packet.ready = true; rc = ReleaseSemaphore(Receive_Packet_Flag, 1, NULL); - (void) rc; + (void)rc; } return pdu_len; @@ -265,13 +252,12 @@ 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 */ + uint8_t destination = 0; /* destination address */ - (void) timeout; + (void)timeout; if (!Transmit_Packet.ready) { return 0; } @@ -285,22 +271,22 @@ 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 */ - mstp_port->OutputBufferSize, Transmit_Packet.frame_type, destination, - mstp_port->This_Station, &Transmit_Packet.pdu[0], - Transmit_Packet.pdu_len); + 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.ready = false; return pdu_len; } -static bool dlmstp_compare_data_expecting_reply( - uint8_t * request_pdu, +static bool dlmstp_compare_data_expecting_reply(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 @@ -322,9 +308,8 @@ static bool dlmstp_compare_data_expecting_reply( /* decode the request data */ request.address.mac[0] = src_address; request.address.mac_len = 1; - offset = - (uint16_t) npdu_decode(&request_pdu[0], NULL, &request.address, - &request.npdu_data); + offset = (uint16_t)npdu_decode( + &request_pdu[0], NULL, &request.address, &request.npdu_data); if (request.npdu_data.network_layer_message) { return false; } @@ -340,9 +325,8 @@ static bool dlmstp_compare_data_expecting_reply( request.service_choice = request_pdu[offset + 3]; /* decode the reply data */ bacnet_address_copy(&reply.address, dest_address); - offset = - (uint16_t) npdu_decode(&reply_pdu[0], &reply.address, NULL, - &reply.npdu_data); + offset = (uint16_t)npdu_decode( + &reply_pdu[0], &reply.address, NULL, &reply.npdu_data); if (reply.npdu_data.network_layer_message) { return false; } @@ -387,7 +371,8 @@ static bool dlmstp_compare_data_expecting_reply( return false; } } - if (request.npdu_data.protocol_version != reply.npdu_data.protocol_version) { + if (request.npdu_data.protocol_version != + reply.npdu_data.protocol_version) { return false; } #if 0 @@ -406,14 +391,13 @@ static 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 */ - uint8_t destination = 0; /* destination address */ + 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; } @@ -427,25 +411,24 @@ 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); if (!matched) return 0; /* convert the PDU into the MSTP Frame */ - 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); + 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.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) { @@ -462,8 +445,7 @@ void dlmstp_set_mac_address( return; } -uint8_t dlmstp_mac_address( - void) +uint8_t dlmstp_mac_address(void) { return MSTP_Port.This_Station; } @@ -475,8 +457,7 @@ uint8_t dlmstp_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( - 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; @@ -490,8 +471,7 @@ void dlmstp_set_max_info_frames( return; } -uint8_t dlmstp_max_info_frames( - void) +uint8_t dlmstp_max_info_frames(void) { return MSTP_Port.Nmax_info_frames; } @@ -501,8 +481,7 @@ uint8_t dlmstp_max_info_frames( /* 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) { @@ -518,33 +497,29 @@ void dlmstp_set_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(); } -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; - my_address->net = 0; /* local only, no routing */ + my_address->net = 0; /* local only, no routing */ my_address->len = 0; for (i = 0; i < MAX_MAC_LEN; i++) { my_address->adr[i] = 0; @@ -553,16 +528,15 @@ void dlmstp_get_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; } @@ -571,8 +545,7 @@ void dlmstp_get_broadcast_address( return; } -bool dlmstp_init( - char *ifname) +bool dlmstp_init(char *ifname) { unsigned long hThread = 0; uint32_t arg_value = 0; @@ -634,7 +607,7 @@ bool dlmstp_init( 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", - (unsigned) MSTP_Port.Nmax_info_frames); + (unsigned)MSTP_Port.Nmax_info_frames); #endif hThread = _beginthread(dlmstp_receive_fsm_task, 4096, &arg_value); if (hThread == 0) { @@ -651,19 +624,17 @@ bool dlmstp_init( #ifdef TEST_DLMSTP #include -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 */ - (void) src; - (void) apdu; - (void) 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; @@ -678,9 +649,7 @@ uint32_t timestamp_ms( static char *Network_Interface = NULL; -int main( - int argc, - char *argv[]) +int main(int argc, char *argv[]) { uint16_t pdu_len = 0; diff --git a/ports/win32/ethernet.c b/ports/win32/ethernet.c index ab4253a6..3f01b9fc 100644 --- a/ports/win32/ethernet.c +++ b/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 @@ -42,7 +42,6 @@ #include "bacnet/datalink/ethernet.h" #include "bacnet/bacdcode.h" - /* Uses WinPCap to access raw ethernet */ /* Notes: */ /* To make ethernet.c work under win32, you have to: */ @@ -59,10 +58,9 @@ #include "ntddndis.h" #include "remote-ext.h" - /* commonly used comparison address for ethernet */ -uint8_t Ethernet_Broadcast[MAX_MAC_LEN] = - { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; +uint8_t Ethernet_Broadcast[MAX_MAC_LEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF }; /* commonly used empty address for ethernet quick compare */ uint8_t Ethernet_Empty_MAC[MAX_MAC_LEN] = { 0, 0, 0, 0, 0, 0 }; @@ -71,31 +69,24 @@ uint8_t Ethernet_MAC_Address[MAX_MAC_LEN] = { 0 }; /* couple of var for using winpcap */ static char pcap_errbuf[PCAP_ERRBUF_SIZE + 1]; -static pcap_t *pcap_eth802_fp = NULL; /* 802.2 file handle, from winpcap */ +static pcap_t *pcap_eth802_fp = NULL; /* 802.2 file handle, from winpcap */ 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); @@ -104,8 +95,7 @@ void ethernet_cleanup( LogInfo("ethernet.c: ethernet_cleanup() ok.\n"); } -void ethernet_set_timeout( - unsigned timeout) +void ethernet_set_timeout(unsigned timeout) { eth_timeout = timeout; } @@ -113,7 +103,7 @@ void ethernet_set_timeout( /*---------------------------------------------------------------------- Portable function to set a socket into nonblocking mode. Calling this on a socket causes all future read() and write() calls on - that socket to do only as much as they can immediately, and return + that socket to do only as much as they can immediately, and return without waiting. If no data can be read or written, they return -1 and set errno to EAGAIN (or EWOULDBLOCK). @@ -135,8 +125,7 @@ int setNonblocking(int fd) } */ -bool ethernet_init( - char *if_name) +bool ethernet_init(char *if_name) { PPACKET_OID_DATA pOidData; LPADAPTER lpAdapter; @@ -155,8 +144,8 @@ bool ethernet_init( */ /* Retrieve the device list */ if (pcap_findalldevs(&pcap_all_if, pcap_errbuf) == -1) { - sprintf(msgBuf, "ethernet.c: error in pcap_findalldevs: %s\n", - pcap_errbuf); + sprintf( + msgBuf, "ethernet.c: error in pcap_findalldevs: %s\n", pcap_errbuf); LogError(msgBuf); return false; } @@ -165,10 +154,10 @@ bool ethernet_init( if (strcmp(if_name, dev->name) == 0) break; } - pcap_freealldevs(pcap_all_if); /* we don't need it anymore */ + pcap_freealldevs(pcap_all_if); /* we don't need it anymore */ if (dev == NULL) { - sprintf(msgBuf, "ethernet.c: specified interface not found: %s\n", - if_name); + sprintf( + msgBuf, "ethernet.c: specified interface not found: %s\n", if_name); LogError(msgBuf); return false; } @@ -185,7 +174,7 @@ bool ethernet_init( LogError(msgBuf); return false; } - pOidData = (PPACKET_OID_DATA) str; + pOidData = (PPACKET_OID_DATA)str; pOidData->Oid = OID_802_3_CURRENT_ADDRESS; pOidData->Length = 6; result = PacketRequest(lpAdapter, FALSE, pOidData); @@ -204,17 +193,18 @@ bool ethernet_init( */ /* Open the output device */ pcap_eth802_fp = pcap_open(if_name, /* name of the device */ - 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 */ - ); + 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 */ + ); if (pcap_eth802_fp == NULL) { PacketCloseAdapter(lpAdapter); ethernet_cleanup(); sprintf(msgBuf, - "ethernet.c: unable to open the adapter. %s is not supported by WinPcap\n", + "ethernet.c: unable to open the adapter. %s is not supported by " + "WinPcap\n", if_name); LogError(msgBuf); return false; @@ -229,12 +219,11 @@ bool ethernet_init( /* 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 */ - BACNET_ADDRESS * src, /* source address */ - uint8_t * pdu, /* any data to be sent - may be null */ - unsigned pdu_len /* number of bytes of data */ - ) +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 */ +) { int bytes = 0; uint8_t mtu[MAX_MPDU] = { 0 }; @@ -272,11 +261,11 @@ int ethernet_send( 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 */ - mtu[mtu_len++] = 0x03; /* Control byte in header */ + mtu[mtu_len++] = 0x82; /* DSAP for BACnet */ + mtu[mtu_len++] = 0x82; /* SSAP for BACnet */ + mtu[mtu_len++] = 0x03; /* Control byte in header */ memcpy(&mtu[mtu_len], pdu, pdu_len); mtu_len += pdu_len; @@ -295,13 +284,12 @@ int ethernet_send( /* 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++) { @@ -310,26 +298,26 @@ int ethernet_send_pdu( } /* 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 */ + 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 */ - ); + ); } /* 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; int res; u_char *pkt_data; - uint16_t pdu_len = 0; /* return value */ + uint16_t pdu_len = 0; /* return value */ /* Make sure the socket is open */ if (!ethernet_valid()) { @@ -361,14 +349,14 @@ uint16_t ethernet_receive( /* check destination address for when */ /* the Ethernet card is in promiscious mode */ - if ((memcmp(&pkt_data[0], Ethernet_MAC_Address, 6) != 0) - && (memcmp(&pkt_data[0], Ethernet_Broadcast, 6) != 0)) { + if ((memcmp(&pkt_data[0], Ethernet_MAC_Address, 6) != 0) && + (memcmp(&pkt_data[0], Ethernet_Broadcast, 6) != 0)) { /*eth_log_error( "ethernet.c: This packet isn't for us\n"); */ return 0; } - (void) decode_unsigned16(&pkt_data[12], &pdu_len); - pdu_len -= 3 /* DSAP, SSAP, LLC Control */ ; + (void)decode_unsigned16(&pkt_data[12], &pdu_len); + pdu_len -= 3 /* DSAP, SSAP, LLC Control */; /* copy the buffer into the PDU */ if (pdu_len < max_pdu) memmove(&pdu[0], &pkt_data[17], pdu_len); @@ -379,8 +367,7 @@ uint16_t ethernet_receive( return pdu_len; } -void ethernet_set_my_address( - BACNET_ADDRESS * my_address) +void ethernet_set_my_address(BACNET_ADDRESS *my_address) { int i = 0; @@ -391,8 +378,7 @@ void ethernet_set_my_address( return; } -void ethernet_get_my_address( - BACNET_ADDRESS * my_address) +void ethernet_get_my_address(BACNET_ADDRESS *my_address) { int i = 0; @@ -401,7 +387,7 @@ void ethernet_get_my_address( my_address->mac[i] = Ethernet_MAC_Address[i]; my_address->mac_len++; } - my_address->net = 0; /* local only, no routing */ + my_address->net = 0; /* local only, no routing */ my_address->len = 0; for (i = 0; i < MAX_MAC_LEN; i++) { my_address->adr[i] = 0; @@ -410,10 +396,9 @@ void ethernet_get_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++) { @@ -421,7 +406,7 @@ void ethernet_get_broadcast_address( } 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; } @@ -430,11 +415,9 @@ void ethernet_get_broadcast_address( 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) { @@ -443,20 +426,20 @@ void ethernet_debug_address( } /* if */ if (dest) { - sprintf(msgBuf, "Address:\n MAC Length=%d\n MAC Address=", - dest->mac_len); + sprintf( + msgBuf, "Address:\n MAC Length=%d\n MAC Address=", dest->mac_len); LogInfo(msgBuf); for (i = 0; i < MAX_MAC_LEN; i++) { - sprintf(msgBuf, "%02X ", (unsigned) dest->mac[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); LogInfo(msgBuf); for (i = 0; i < MAX_MAC_LEN; i++) { - sprintf(msgBuf, "%02X ", (unsigned) dest->adr[i]); + sprintf(msgBuf, "%02X ", (unsigned)dest->adr[i]); LogInfo(msgBuf); - } /* for */ + } /* for */ LogInfo("\n"); } /* if ( dest ) */ diff --git a/ports/win32/main.c b/ports/win32/main.c index 8dde5455..e582467b 100644 --- a/ports/win32/main.c +++ b/ports/win32/main.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2005 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2005 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* This is one way to use the embedded BACnet stack under Win32 */ /* compiled with Borland C++ 5.02 or Visual C++ 6.0 */ @@ -29,7 +29,7 @@ #include #include #include -#include /* for kbhit and getch */ +#include /* for kbhit and getch */ #include "bacnet/iam.h" #include "bacnet/basic/binding/address.h" #include "bacnet/config.h" @@ -66,8 +66,7 @@ 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; @@ -80,47 +79,32 @@ static void Read_Properties( Device Object note: you could just loop through all the properties in all the objects. */ - const int object_props[] = { - PROP_OBJECT_IDENTIFIER, - PROP_OBJECT_NAME, - PROP_OBJECT_TYPE, - PROP_SYSTEM_STATUS, - PROP_VENDOR_NAME, - PROP_VENDOR_IDENTIFIER, - PROP_MODEL_NAME, - PROP_FIRMWARE_REVISION, - PROP_APPLICATION_SOFTWARE_VERSION, - PROP_PROTOCOL_VERSION, - PROP_PROTOCOL_SERVICES_SUPPORTED, - PROP_PROTOCOL_OBJECT_TYPES_SUPPORTED, - PROP_MAX_APDU_LENGTH_ACCEPTED, - PROP_SEGMENTATION_SUPPORTED, - PROP_LOCAL_TIME, - PROP_LOCAL_DATE, - PROP_UTC_OFFSET, - PROP_DAYLIGHT_SAVINGS_STATUS, - PROP_APDU_SEGMENT_TIMEOUT, - PROP_APDU_TIMEOUT, - PROP_NUMBER_OF_APDU_RETRIES, - PROP_TIME_SYNCHRONIZATION_RECIPIENTS, - PROP_MAX_MASTER, - PROP_MAX_INFO_FRAMES, - PROP_DEVICE_ADDRESS_BINDING, + const int object_props[] = { PROP_OBJECT_IDENTIFIER, PROP_OBJECT_NAME, + PROP_OBJECT_TYPE, PROP_SYSTEM_STATUS, PROP_VENDOR_NAME, + PROP_VENDOR_IDENTIFIER, PROP_MODEL_NAME, PROP_FIRMWARE_REVISION, + PROP_APPLICATION_SOFTWARE_VERSION, PROP_PROTOCOL_VERSION, + PROP_PROTOCOL_SERVICES_SUPPORTED, PROP_PROTOCOL_OBJECT_TYPES_SUPPORTED, + PROP_MAX_APDU_LENGTH_ACCEPTED, PROP_SEGMENTATION_SUPPORTED, + PROP_LOCAL_TIME, PROP_LOCAL_DATE, PROP_UTC_OFFSET, + PROP_DAYLIGHT_SAVINGS_STATUS, PROP_APDU_SEGMENT_TIMEOUT, + PROP_APDU_TIMEOUT, PROP_NUMBER_OF_APDU_RETRIES, + PROP_TIME_SYNCHRONIZATION_RECIPIENTS, PROP_MAX_MASTER, + PROP_MAX_INFO_FRAMES, PROP_DEVICE_ADDRESS_BINDING, /* note: PROP_OBJECT_LIST is missing cause we need to get it with an index method since the list could be very large */ /* some proprietary properties */ 514, 515, /* end of list */ - -1 - }; + -1 }; if (address_count()) { if (address_get_by_index(index, &device_id, &max_apdu, &src)) { if (object_props[property] < 0) next_device = true; else { - status = Send_Read_Property_Request(device_id, /* destination device */ + status = Send_Read_Property_Request( + device_id, /* destination device */ OBJECT_DEVICE, device_id, object_props[property], BACNET_ARRAY_ALL); if (status) @@ -141,9 +125,7 @@ static void Read_Properties( } static void LocalIAmHandler( - uint8_t * service_request, - uint16_t service_len, - BACNET_ADDRESS * src) + uint8_t *service_request, uint16_t service_len, BACNET_ADDRESS *src) { int len = 0; uint32_t device_id = 0; @@ -151,11 +133,10 @@ static void LocalIAmHandler( int segmentation = 0; uint16_t vendor_id = 0; - (void) src; - (void) service_len; - len = - iam_decode_service_request(service_request, &device_id, &max_apdu, - &segmentation, &vendor_id); + (void)src; + (void)service_len; + len = iam_decode_service_request( + service_request, &device_id, &max_apdu, &segmentation, &vendor_id); fprintf(stderr, "Received I-Am Request"); if (len != -1) { fprintf(stderr, " from %u!\n", device_id); @@ -166,8 +147,7 @@ static void LocalIAmHandler( return; } -static void Init_Service_Handlers( - void) +static void Init_Service_Handlers(void) { Device_Init(NULL); @@ -177,22 +157,21 @@ static void Init_Service_Handlers( /* 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 - (handler_unrecognized_service); + apdu_set_unrecognized_service_handler_handler(handler_unrecognized_service); /* we must implement read property - it's required! */ - apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY, - handler_read_property); - apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROP_MULTIPLE, - handler_read_property_multiple); + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_READ_PROPERTY, handler_read_property); + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_READ_PROP_MULTIPLE, handler_read_property_multiple); /* handle the data coming back from confirmed requests */ - apdu_set_confirmed_ack_handler(SERVICE_CONFIRMED_READ_PROPERTY, - handler_read_property_ack); + apdu_set_confirmed_ack_handler( + SERVICE_CONFIRMED_READ_PROPERTY, handler_read_property_ack); #if defined(BACFILE) - apdu_set_confirmed_handler(SERVICE_CONFIRMED_ATOMIC_READ_FILE, - handler_atomic_read_file); + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_ATOMIC_READ_FILE, handler_atomic_read_file); #endif - apdu_set_confirmed_handler(SERVICE_CONFIRMED_SUBSCRIBE_COV, - handler_cov_subscribe); + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_SUBSCRIBE_COV, handler_cov_subscribe); #if 0 /* Adding these handlers require the project(s) to change. */ @@ -216,11 +195,9 @@ static void Init_Service_Handlers( #endif } -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); @@ -231,8 +208,7 @@ static void print_address( } } -static void print_address_cache( - void) +static void print_address_cache(void) { int i, j; BACNET_ADDRESS address; @@ -253,19 +229,15 @@ static void print_address_cache( } } -int main( - int argc, - char *argv[]) +int main(int argc, char *argv[]) { - BACNET_ADDRESS src = { - 0 - }; /* address where message came from */ + BACNET_ADDRESS src = { 0 }; /* address where message came from */ uint16_t pdu_len = 0; - unsigned timeout = 100; /* milliseconds */ + unsigned timeout = 100; /* milliseconds */ BACNET_ADDRESS my_address, broadcast_address; - (void) argc; - (void) argv; + (void)argc; + (void)argv; Device_Set_Object_Instance_Number(4194300); address_init(); Init_Service_Handlers(); diff --git a/ports/win32/mstimer-init.c b/ports/win32/mstimer-init.c index 87c85b90..2f90de95 100644 --- a/ports/win32/mstimer-init.c +++ b/ports/win32/mstimer-init.c @@ -1,28 +1,28 @@ /************************************************************************** -* -* Copyright (C) 2009 Steve Karg -* Multimedia Timer contribution by Cameron Crothers, 2008 -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2009 Steve Karg + * Multimedia Timer contribution by Cameron Crothers, 2008 + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include #include @@ -39,15 +39,14 @@ static volatile unsigned long Millisecond_Counter; static unsigned long Timer_Period = 1; /** -* @brief returns the current millisecond count -* @return millisecond counter -*/ + * @brief returns the current millisecond count + * @return millisecond counter + */ unsigned long mstimer_now(void) { unsigned long now = timeGetTime(); unsigned long delta_time = 0; - if (Millisecond_Counter <= now) { delta_time = now - Millisecond_Counter; } else { @@ -58,17 +57,16 @@ unsigned long mstimer_now(void) } /** -* @brief Shut down for timer -*/ -static void timer_cleanup( - void) + * @brief Shut down for timer + */ +static void timer_cleanup(void) { timeEndPeriod(Timer_Period); } /** -* @brief Initialization for timer -*/ + * @brief Initialization for timer + */ void mstimer_init(void) { TIMECAPS tc; @@ -81,8 +79,9 @@ void mstimer_init(void) Timer_Period = min(max(tc.wPeriodMin, 1L), tc.wPeriodMax); if (Timer_Period != 1L) { fprintf(stderr, - "Failed to set timer to 1ms. " "Time period set to %ums\n", - (unsigned) Timer_Period); + "Failed to set timer to 1ms. " + "Time period set to %ums\n", + (unsigned)Timer_Period); } timeBeginPeriod(Timer_Period); atexit(timer_cleanup); diff --git a/ports/win32/rs485.c b/ports/win32/rs485.c index c1e1ff08..5e8ee77a 100644 --- a/ports/win32/rs485.c +++ b/ports/win32/rs485.c @@ -86,29 +86,27 @@ static DWORD RS485_DTRControl = DTR_CONTROL_DISABLE; static DWORD RS485_RTSControl = RTS_CONTROL_DISABLE; /**************************************************************************** -* DESCRIPTION: Change the characters in a string to uppercase -* RETURN: nothing -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -static void strupper( - char *str) + * DESCRIPTION: Change the characters in a string to uppercase + * RETURN: nothing + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +static void strupper(char *str) { char *p; for (p = str; *p != '\0'; ++p) { - *p = (char) toupper(*p); + *p = (char)toupper(*p); } } /**************************************************************************** -* DESCRIPTION: Initializes the RS485 hardware and variables, and starts in -* receive mode. -* RETURN: none -* ALGORITHM: none -* NOTES: expects a constant char ifname, or char from the heap -*****************************************************************************/ -void RS485_Set_Interface( - char *ifname) + * DESCRIPTION: Initializes the RS485 hardware and variables, and starts in + * receive mode. + * RETURN: none + * ALGORITHM: none + * NOTES: expects a constant char ifname, or char from the heap + *****************************************************************************/ +void RS485_Set_Interface(char *ifname) { /* For COM ports greater than 9 you have to use a special syntax for CreateFile. The syntax also works for COM ports 1-9. */ @@ -126,13 +124,12 @@ void RS485_Set_Interface( } /**************************************************************************** -* DESCRIPTION: Check the serial port to see if port exists -* RETURN: true if port exists -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -bool RS485_Interface_Valid( - unsigned port_number) + * DESCRIPTION: Check the serial port to see if port exists + * RETURN: true if port exists + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +bool RS485_Interface_Valid(unsigned port_number) { HANDLE h = 0; DWORD err = 0; @@ -140,8 +137,8 @@ bool RS485_Interface_Valid( char ifname[255] = ""; sprintf(ifname, "\\\\.\\COM%u", port_number); - h = CreateFile(ifname, GENERIC_READ | GENERIC_WRITE, 0, NULL, - OPEN_EXISTING, 0, NULL); + h = CreateFile( + ifname, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if (h == INVALID_HANDLE_VALUE) { err = GetLastError(); if ((err == ERROR_ACCESS_DENIED) || (err == ERROR_GEN_FAILURE) || @@ -156,33 +153,29 @@ bool RS485_Interface_Valid( return status; } -const char *RS485_Interface( - void) +const char *RS485_Interface(void) { return RS485_Port_Name; } -void RS485_Print_Error( - void) +void RS485_Print_Error(void) { LPVOID lpMsgBuf; FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPTSTR) & lpMsgBuf, 0, NULL); + (LPTSTR)&lpMsgBuf, 0, NULL); MessageBox(NULL, lpMsgBuf, "GetLastError", MB_OK | MB_ICONINFORMATION); LocalFree(lpMsgBuf); return; } -static void RS485_Configure_Status( - void) +static void RS485_Configure_Status(void) { DCB dcb = { 0 }; COMMTIMEOUTS ctNew; - dcb.DCBlength = sizeof(dcb); /* get current DCB settings */ if (!GetCommState(RS485_Handle, &dcb)) { @@ -193,9 +186,9 @@ static void RS485_Configure_Status( /* update DCB rate, byte size, parity, and stop bits size */ dcb.BaudRate = RS485_Baud; - dcb.ByteSize = (unsigned char) RS485_ByteSize; - dcb.Parity = (unsigned char) RS485_Parity; - dcb.StopBits = (unsigned char) RS485_StopBits; + dcb.ByteSize = (unsigned char)RS485_ByteSize; + dcb.Parity = (unsigned char)RS485_Parity; + dcb.StopBits = (unsigned char)RS485_StopBits; /* update flow control settings */ dcb.fDtrControl = RS485_DTRControl; @@ -242,13 +235,12 @@ static void RS485_Configure_Status( } /**************************************************************************** -* DESCRIPTION: Cleans up any handles that were created at startup. -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -static void RS485_Cleanup( - void) + * DESCRIPTION: Cleans up any handles that were created at startup. + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +static void RS485_Cleanup(void) { if (!EscapeCommFunction(RS485_Handle, CLRDTR)) { RS485_Print_Error(); @@ -262,20 +254,17 @@ static void RS485_Cleanup( } /**************************************************************************** -* DESCRIPTION: Initializes the RS485 hardware and variables, and starts in -* receive mode. -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -void RS485_Initialize( - void) + * DESCRIPTION: Initializes the RS485 hardware and variables, and starts in + * receive mode. + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +void RS485_Initialize(void) { - RS485_Handle = - CreateFile(RS485_Port_Name, GENERIC_READ | GENERIC_WRITE, 0, 0, - OPEN_EXISTING, - /*FILE_FLAG_OVERLAPPED */ 0, - 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); RS485_Print_Error(); @@ -295,13 +284,12 @@ void RS485_Initialize( } /**************************************************************************** -* DESCRIPTION: Returns the baud rate that we are currently running at -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -uint32_t RS485_Get_Baud_Rate( - void) + * DESCRIPTION: Returns the baud rate that we are currently running at + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +uint32_t RS485_Get_Baud_Rate(void) { switch (RS485_Baud) { case CBR_19200: @@ -347,13 +335,12 @@ uint32_t RS485_Get_Baud_Rate( } /**************************************************************************** -* DESCRIPTION: Sets the baud rate for the chip USART -* RETURN: none -* ALGORITHM: none -* NOTES: none -*****************************************************************************/ -bool RS485_Set_Baud_Rate( - uint32_t baud) + * DESCRIPTION: Sets the baud rate for the chip USART + * RETURN: none + * ALGORITHM: none + * NOTES: none + *****************************************************************************/ +bool RS485_Set_Baud_Rate(uint32_t baud) { bool valid = true; @@ -432,10 +419,10 @@ bool RS485_Set_Baud_Rate( /* 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) */ + 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) */ +{ /* number of bytes of data (up to 501) */ DWORD dwWritten = 0; if (mstp_port) { @@ -464,8 +451,7 @@ 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; @@ -490,12 +476,11 @@ void RS485_Check_UART_Data( } /************************************************************************* -* Description: print available COM ports -* Returns: none -* Notes: none -**************************************************************************/ -void RS485_Print_Ports( - void) + * Description: print available COM ports + * Returns: none + * Notes: none + **************************************************************************/ +void RS485_Print_Ports(void) { unsigned i = 0; @@ -504,7 +489,8 @@ void RS485_Print_Ports( if (RS485_Interface_Valid(i)) { /* note: format for Wireshark ExtCap */ printf("interface {value=COM%u}" - "{display=BACnet MS/TP on COM%u}\n", i, i); + "{display=BACnet MS/TP on COM%u}\n", + i, i); } } } @@ -513,9 +499,7 @@ void RS485_Print_Ports( #include "bacnet/datalink/mstpdef.h" - -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; @@ -527,8 +511,7 @@ static void test_transmit_task( } #if defined(_WIN32) -static BOOL WINAPI CtrlCHandler( - DWORD dwCtrlType) +static BOOL WINAPI CtrlCHandler(DWORD dwCtrlType) { dwCtrlType = dwCtrlType; exit(0); @@ -536,8 +519,7 @@ static BOOL WINAPI CtrlCHandler( } #endif -static int ascii_hex_to_int( - char ch) +static int ascii_hex_to_int(char ch) { int rv = -1; @@ -552,9 +534,7 @@ static int ascii_hex_to_int( return rv; } -int main( - int argc, - char *argv[]) +int main(int argc, char *argv[]) { unsigned long hThread = 0; uint32_t arg_value = 0; @@ -577,7 +557,7 @@ int main( RS485_Initialize(); #if defined(_WIN32) SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), ENABLE_PROCESSED_INPUT); - SetConsoleCtrlHandler((PHANDLER_ROUTINE) CtrlCHandler, TRUE); + SetConsoleCtrlHandler((PHANDLER_ROUTINE)CtrlCHandler, TRUE); #endif #ifdef TEST_RS485_TRANSMIT /* read a stream of characters from stdin or argument */ diff --git a/ports/win32/rx_fsm.c b/ports/win32/rx_fsm.c index be3e625a..be5a140f 100644 --- a/ports/win32/rx_fsm.c +++ b/ports/win32/rx_fsm.c @@ -53,27 +53,27 @@ #include "crc.h" #ifndef max -#define max(a,b) (((a) (b)) ? (a) : (b)) -#define min(a,b) (((a) < (b)) ? (a) : (b)) +#define max(a, b) (((a)(b)) ? (a) : (b)) +#define min(a, b) (((a) < (b)) ? (a) : (b)) #endif /* file format for libpcap/winpcap */ /* from http://wiki.wireshark.org/Development/LibpcapFileFormat */ typedef struct pcap_hdr_s { - uint32_t magic_number; /* magic number */ - uint16_t version_major; /* major version number */ - uint16_t version_minor; /* minor version number */ - int32_t thiszone; /* GMT to local correction */ - uint32_t sigfigs; /* accuracy of timestamps */ - uint32_t snaplen; /* max length of captured packets, in octets */ - uint32_t network; /* data link type */ + uint32_t magic_number; /* magic number */ + uint16_t version_major; /* major version number */ + uint16_t version_minor; /* minor version number */ + int32_t thiszone; /* GMT to local correction */ + uint32_t sigfigs; /* accuracy of timestamps */ + uint32_t snaplen; /* max length of captured packets, in octets */ + uint32_t network; /* data link type */ } pcap_hdr_t; typedef struct pcaprec_hdr_s { - uint32_t ts_sec; /* timestamp seconds */ - uint32_t ts_usec; /* timestamp microseconds */ - uint32_t incl_len; /* number of octets of packet saved in file */ - uint32_t orig_len; /* actual length of packet */ + uint32_t ts_sec; /* timestamp seconds */ + uint32_t ts_usec; /* timestamp microseconds */ + uint32_t incl_len; /* number of octets of packet saved in file */ + uint32_t orig_len; /* actual length of packet */ } pcaprec_hdr_t; /* local port data - shared with RS-485 */ @@ -81,29 +81,29 @@ volatile struct mstp_port_struct_t MSTP_Port; 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) +#define INCREMENT_AND_LIMIT_UINT16(x) \ + { \ + if (x < 0xFFFF) \ + x++; \ + } +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(); @@ -113,10 +113,9 @@ void *milliseconds_task( } /* 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) +uint16_t MSTP_Put_Receive(volatile struct mstp_port_struct_t *mstp_port) { - (void) mstp_port; + (void)mstp_port; return 0; } @@ -124,20 +123,18 @@ 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 */ - (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; } @@ -175,10 +172,9 @@ static void print_received_packet( #endif /* returns a delta timestamp */ -void timestamp( - uint32_t * ts_sec, /* timestamp seconds since epoch (Unix) */ - uint32_t * ts_usec) -{ /* timestamp microseconds (unix) */ +void timestamp(uint32_t *ts_sec, /* timestamp seconds since epoch (Unix) */ + uint32_t *ts_usec) +{ /* timestamp microseconds (unix) */ DWORD ticks = 0; static DWORD initial_ticks = 0; static time_t initial_seconds = 0; @@ -202,19 +198,18 @@ void timestamp( } static const char *Capture_Filename = "mstp.cap"; -static FILE *pFile = NULL; /* stream pointer */ +static FILE *pFile = NULL; /* stream pointer */ /* write packet to file in libpcap format */ -static void write_global_header( - void) +static void write_global_header(void) { uint32_t magic_number = 0xa1b2c3d4; /* magic number */ uint16_t version_major = 2; /* major version number */ uint16_t version_minor = 4; /* minor version number */ - int32_t thiszone = 0; /* GMT to local correction */ - uint32_t sigfigs = 0; /* accuracy of timestamps */ - uint32_t snaplen = 65535; /* max length of captured packets, in octets */ - uint32_t network = 165; /* data link type */ + int32_t thiszone = 0; /* GMT to local correction */ + uint32_t sigfigs = 0; /* accuracy of timestamps */ + uint32_t snaplen = 65535; /* max length of captured packets, in octets */ + uint32_t network = 165; /* data link type */ /* create a new file. */ pFile = fopen(Capture_Filename, "wb"); @@ -232,14 +227,13 @@ static void write_global_header( } } -static void write_received_packet( - volatile struct mstp_port_struct_t *mstp_port) +static void write_received_packet(volatile struct mstp_port_struct_t *mstp_port) { - uint32_t ts_sec; /* timestamp seconds */ - uint32_t ts_usec; /* timestamp microseconds */ - uint32_t incl_len; /* number of octets of packet saved in file */ - uint32_t orig_len; /* actual length of packet */ - uint8_t header[8]; /* MS/TP header */ + uint32_t ts_sec; /* timestamp seconds */ + uint32_t ts_usec; /* timestamp microseconds */ + uint32_t incl_len; /* number of octets of packet saved in file */ + uint32_t orig_len; /* actual length of packet */ + uint8_t header[8]; /* MS/TP header */ if (pFile) { timestamp(&ts_sec, &ts_usec); @@ -274,20 +268,17 @@ static void write_received_packet( static char *Network_Interface = NULL; -static void cleanup( - void) +static void cleanup(void) { if (pFile) { - fflush(pFile); /* stream pointer */ - fclose(pFile); /* stream pointer */ + fflush(pFile); /* stream pointer */ + fclose(pFile); /* stream pointer */ } pFile = 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; @@ -332,8 +323,7 @@ int main( } atexit(cleanup); write_global_header(); - (void) SetThreadPriority(GetCurrentThread(), - THREAD_PRIORITY_TIME_CRITICAL); + (void)SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL); /* run forever */ for (;;) { RS485_Check_UART_Data(mstp_port); diff --git a/ports/xplained/adc-hdw.c b/ports/xplained/adc-hdw.c index 1f7ebc49..efaa52a8 100644 --- a/ports/xplained/adc-hdw.c +++ b/ports/xplained/adc-hdw.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2014 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2014 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include #include @@ -34,10 +34,10 @@ static uint16_t ADC_Channel_Value[ADC_CHANNELS_MAX]; static uint8_t ADC_Current_Channel; /************************************************************************* -* DESCRIPTION: set the active channel in the ADC -* RETURN: nothing -* NOTES: called from ISR, so handle as non-blocking -**************************************************************************/ + * DESCRIPTION: set the active channel in the ADC + * RETURN: nothing + * NOTES: called from ISR, so handle as non-blocking + **************************************************************************/ static void adc_set_channel(unsigned channel) { struct adc_channel_config adc_ch_conf; @@ -84,10 +84,10 @@ static void adc_set_channel(unsigned channel) } /************************************************************************* -* DESCRIPTION: run the active channels through the ADC -* RETURN: nothing -* NOTES: called from ISR, so handle as non-blocking -**************************************************************************/ + * DESCRIPTION: run the active channels through the ADC + * RETURN: nothing + * NOTES: called from ISR, so handle as non-blocking + **************************************************************************/ static void adc_handler(ADC_t *adc, uint8_t ch_mask, adc_result_t raw_value) { unsigned channel; @@ -104,27 +104,28 @@ static void adc_handler(ADC_t *adc, uint8_t ch_mask, adc_result_t raw_value) } /************************************************************************* -* DESCRIPTION: initialize Analog to Digital Converter (ADC) -* RETURN: nothing -* NOTES: none -**************************************************************************/ + * DESCRIPTION: initialize Analog to Digital Converter (ADC) + * RETURN: nothing + * NOTES: none + **************************************************************************/ void adc_init(void) { struct adc_config adc_conf; - ioport_configure_pin(IOPORT_CREATE_PIN(PORTA, 7),IOPORT_DIR_INPUT); - ioport_configure_pin(IOPORT_CREATE_PIN(PORTB, 0),IOPORT_DIR_INPUT); - ioport_configure_pin(IOPORT_CREATE_PIN(PORTB, 1),IOPORT_DIR_INPUT); - ioport_configure_pin(IOPORT_CREATE_PIN(PORTB, 2),IOPORT_DIR_INPUT); - ioport_configure_pin(IOPORT_CREATE_PIN(PORTB, 3),IOPORT_DIR_INPUT); - ioport_configure_pin(IOPORT_CREATE_PIN(PORTA, 2),IOPORT_DIR_INPUT); - ioport_configure_pin(IOPORT_CREATE_PIN(PORTA, 3),IOPORT_DIR_INPUT); - ioport_configure_pin(IOPORT_CREATE_PIN(PORTA, 4),IOPORT_DIR_INPUT); - ioport_configure_pin(IOPORT_CREATE_PIN(PORTA, 5),IOPORT_DIR_INPUT); - ioport_configure_pin(IOPORT_CREATE_PIN(PORTA, 6),IOPORT_DIR_INPUT); + ioport_configure_pin(IOPORT_CREATE_PIN(PORTA, 7), IOPORT_DIR_INPUT); + ioport_configure_pin(IOPORT_CREATE_PIN(PORTB, 0), IOPORT_DIR_INPUT); + ioport_configure_pin(IOPORT_CREATE_PIN(PORTB, 1), IOPORT_DIR_INPUT); + ioport_configure_pin(IOPORT_CREATE_PIN(PORTB, 2), IOPORT_DIR_INPUT); + ioport_configure_pin(IOPORT_CREATE_PIN(PORTB, 3), IOPORT_DIR_INPUT); + ioport_configure_pin(IOPORT_CREATE_PIN(PORTA, 2), IOPORT_DIR_INPUT); + ioport_configure_pin(IOPORT_CREATE_PIN(PORTA, 3), IOPORT_DIR_INPUT); + ioport_configure_pin(IOPORT_CREATE_PIN(PORTA, 4), IOPORT_DIR_INPUT); + ioport_configure_pin(IOPORT_CREATE_PIN(PORTA, 5), IOPORT_DIR_INPUT); + ioport_configure_pin(IOPORT_CREATE_PIN(PORTA, 6), IOPORT_DIR_INPUT); /* Clear the ADC configuration structs */ adc_read_configuration(&ADCA, &adc_conf); - adc_set_conversion_parameters(&adc_conf, ADC_SIGN_ON, ADC_RES_12, ADC_REF_AREFA); + adc_set_conversion_parameters( + &adc_conf, ADC_SIGN_ON, ADC_RES_12, ADC_REF_AREFA); adc_set_clock_rate(&adc_conf, 200000UL); adc_set_conversion_trigger(&adc_conf, ADC_TRIG_MANUAL, 1, 0); adc_write_configuration(&ADCA, &adc_conf); @@ -135,16 +136,17 @@ void adc_init(void) } /************************************************************************* -* DESCRIPTION: Get a result from the ADC 10-bit value -* RETURN: 12-bit ADC value -* NOTES: channel 0..9 are supported -**************************************************************************/ + * DESCRIPTION: Get a result from the ADC 10-bit value + * RETURN: 12-bit ADC value + * NOTES: channel 0..9 are supported + **************************************************************************/ uint16_t adc_result_12bit(uint8_t channel) { uint16_t value = 0; if (channel < ADC_CHANNELS_MAX) { - ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { + ATOMIC_BLOCK(ATOMIC_RESTORESTATE) + { value = ADC_Channel_Value[channel]; } } @@ -153,10 +155,10 @@ uint16_t adc_result_12bit(uint8_t channel) } /************************************************************************* -* DESCRIPTION: Get a result from the ADC 10-bit value -* RETURN: 10-bit ADC value -* NOTES: channel 0..9 are supported -**************************************************************************/ + * DESCRIPTION: Get a result from the ADC 10-bit value + * RETURN: 10-bit ADC value + * NOTES: channel 0..9 are supported + **************************************************************************/ uint16_t adc_result_10bit(uint8_t channel) { uint16_t result; @@ -168,10 +170,10 @@ uint16_t adc_result_10bit(uint8_t channel) } /************************************************************************* -* DESCRIPTION: Get a result from the ADC 8-bit value -* RETURN: 8-bit ADC value -* NOTES: channel 0..9 are supported -**************************************************************************/ + * DESCRIPTION: Get a result from the ADC 8-bit value + * RETURN: 8-bit ADC value + * NOTES: channel 0..9 are supported + **************************************************************************/ uint8_t adc_result_8bit(uint8_t channel) { uint16_t result; diff --git a/ports/xplained/ai.c b/ports/xplained/ai.c index 0671e3af..84f6816c 100644 --- a/ports/xplained/ai.c +++ b/ports/xplained/ai.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2005 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2005 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ /* Analog Input Objects customize for your use */ @@ -44,30 +44,16 @@ static bool Out_Of_Service[MAX_ANALOG_INPUTS]; static BACNET_ENGINEERING_UNITS Units[MAX_ANALOG_INPUTS]; /* These three arrays are used by the ReadPropertyMultiple handler */ -static const int Analog_Input_Properties_Required[] = { - PROP_OBJECT_IDENTIFIER, - PROP_OBJECT_NAME, - PROP_OBJECT_TYPE, - PROP_PRESENT_VALUE, - PROP_STATUS_FLAGS, - PROP_EVENT_STATE, - PROP_OUT_OF_SERVICE, - PROP_UNITS, - -1 -}; +static const int Analog_Input_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, + PROP_OBJECT_NAME, PROP_OBJECT_TYPE, PROP_PRESENT_VALUE, PROP_STATUS_FLAGS, + PROP_EVENT_STATE, PROP_OUT_OF_SERVICE, PROP_UNITS, -1 }; -static const int Analog_Input_Properties_Optional[] = { - -1 -}; +static const int Analog_Input_Properties_Optional[] = { -1 }; -static const int Analog_Input_Properties_Proprietary[] = { - -1 -}; +static const int Analog_Input_Properties_Proprietary[] = { -1 }; void Analog_Input_Property_Lists( - const int **pRequired, - const int **pOptional, - const int **pProprietary) + const int **pRequired, const int **pOptional, const int **pProprietary) { if (pRequired) *pRequired = Analog_Input_Properties_Required; @@ -79,22 +65,19 @@ void Analog_Input_Property_Lists( return; } -void Analog_Input_Init( - void) +void Analog_Input_Init(void) { return; } /* 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 instance) +unsigned Analog_Input_Instance_To_Index(uint32_t instance) { return instance; } @@ -102,13 +85,12 @@ unsigned Analog_Input_Instance_To_Index( /* 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) { - unsigned index = 0; + unsigned index = 0; - index = Analog_Input_Instance_To_Index(object_instance); - if (index < MAX_ANALOG_INPUTS) { + index = Analog_Input_Instance_To_Index(object_instance); + if (index < MAX_ANALOG_INPUTS) { return true; } @@ -116,22 +98,20 @@ bool Analog_Input_Valid_Instance( } /* we simply have 0-n object instances. */ -unsigned Analog_Input_Count( - void) +unsigned Analog_Input_Count(void) { return MAX_ANALOG_INPUTS; } bool Analog_Input_Object_Name( - uint32_t object_instance, - BACNET_CHARACTER_STRING * object_name) + uint32_t object_instance, BACNET_CHARACTER_STRING *object_name) { - static char text_string[32]; /* okay for single thread */ + static char text_string[32]; /* okay for single thread */ bool status = false; - unsigned index = 0; + unsigned index = 0; - index = Analog_Input_Instance_To_Index(object_instance); - if (index < MAX_ANALOG_INPUTS) { + index = Analog_Input_Instance_To_Index(object_instance); + if (index < MAX_ANALOG_INPUTS) { sprintf(text_string, "AI-%lu", object_instance); status = characterstring_init_ansi(object_name, text_string); } @@ -139,67 +119,59 @@ bool Analog_Input_Object_Name( return status; } -float Analog_Input_Present_Value( - uint32_t object_instance) +float Analog_Input_Present_Value(uint32_t object_instance) { float value = 0.0; - unsigned index = 0; + unsigned index = 0; - index = Analog_Input_Instance_To_Index(object_instance); - if (index < MAX_ANALOG_INPUTS) { + index = Analog_Input_Instance_To_Index(object_instance); + if (index < MAX_ANALOG_INPUTS) { value = Present_Value[index]; } return value; } -void Analog_Input_Present_Value_Set( - uint32_t object_instance, - float value) +void Analog_Input_Present_Value_Set(uint32_t object_instance, float value) { - unsigned index = 0; + unsigned index = 0; - index = Analog_Input_Instance_To_Index(object_instance); - if (index < MAX_ANALOG_INPUTS) { + index = Analog_Input_Instance_To_Index(object_instance); + if (index < MAX_ANALOG_INPUTS) { Present_Value[index] = value; } } -bool Analog_Input_Out_Of_Service( - uint32_t object_instance) +bool Analog_Input_Out_Of_Service(uint32_t object_instance) { - unsigned index = 0; - bool value = false; + unsigned index = 0; + bool value = false; - index = Analog_Input_Instance_To_Index(object_instance); - if (index < MAX_ANALOG_INPUTS) { - value = Out_Of_Service[index]; - } + index = Analog_Input_Instance_To_Index(object_instance); + if (index < MAX_ANALOG_INPUTS) { + value = Out_Of_Service[index]; + } - return value; + return value; } -void Analog_Input_Out_Of_Service_Set( - uint32_t object_instance, - bool value) +void Analog_Input_Out_Of_Service_Set(uint32_t object_instance, bool value) { - unsigned index = 0; + unsigned index = 0; - index = Analog_Input_Instance_To_Index(object_instance); - if (index < MAX_ANALOG_INPUTS) { - Out_Of_Service[index] = value; - } + index = Analog_Input_Instance_To_Index(object_instance); + if (index < MAX_ANALOG_INPUTS) { + Out_Of_Service[index] = value; + } } -bool Analog_Input_Units_Set( - uint32_t object_instance, - uint16_t value) +bool Analog_Input_Units_Set(uint32_t object_instance, uint16_t value) { - unsigned index = 0; + unsigned index = 0; bool status = false; - index = Analog_Input_Instance_To_Index(object_instance); - if (index < MAX_ANALOG_INPUTS) { + index = Analog_Input_Instance_To_Index(object_instance); + if (index < MAX_ANALOG_INPUTS) { Units[index] = value; status = true; } @@ -207,14 +179,13 @@ bool Analog_Input_Units_Set( return status; } -uint16_t Analog_Input_Units( - uint32_t object_instance) +uint16_t Analog_Input_Units(uint32_t object_instance) { - unsigned index = 0; + unsigned index = 0; uint16_t value = UNITS_NO_UNITS; - index = Analog_Input_Instance_To_Index(object_instance); - if (index < MAX_ANALOG_INPUTS) { + index = Analog_Input_Instance_To_Index(object_instance); + if (index < MAX_ANALOG_INPUTS) { value = Units[index]; } @@ -223,10 +194,9 @@ uint16_t Analog_Input_Units( /* return apdu length, or -1 on error */ /* assumption - object already exists */ -int Analog_Input_Read_Property( - BACNET_READ_PROPERTY_DATA * rpdata) +int Analog_Input_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { - int apdu_len = 0; /* return value */ + int apdu_len = 0; /* return value */ BACNET_CHARACTER_STRING char_string = { 0 }; BACNET_BIT_STRING bit_string = { 0 }; uint8_t *apdu = NULL; @@ -238,9 +208,8 @@ int Analog_Input_Read_Property( apdu = rpdata->application_data; switch (rpdata->object_property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], rpdata->object_type, - rpdata->object_instance); + apdu_len = encode_application_object_id( + &apdu[0], rpdata->object_type, rpdata->object_instance); break; case PROP_OBJECT_NAME: Analog_Input_Object_Name(rpdata->object_instance, &char_string); @@ -252,9 +221,8 @@ int Analog_Input_Read_Property( encode_application_enumerated(&apdu[0], rpdata->object_type); break; case PROP_PRESENT_VALUE: - apdu_len = - encode_application_real(&apdu[0], - Analog_Input_Present_Value(rpdata->object_instance)); + apdu_len = encode_application_real( + &apdu[0], Analog_Input_Present_Value(rpdata->object_instance)); break; case PROP_STATUS_FLAGS: bitstring_init(&bit_string); @@ -270,12 +238,12 @@ int Analog_Input_Read_Property( encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); break; case PROP_OUT_OF_SERVICE: - apdu_len = encode_application_boolean(&apdu[0], - Analog_Input_Out_Of_Service(rpdata->object_instance)); + apdu_len = encode_application_boolean( + &apdu[0], Analog_Input_Out_Of_Service(rpdata->object_instance)); break; case PROP_UNITS: - apdu_len = encode_application_enumerated(&apdu[0], - Analog_Input_Units(rpdata->object_instance)); + apdu_len = encode_application_enumerated( + &apdu[0], Analog_Input_Units(rpdata->object_instance)); break; default: rpdata->error_class = ERROR_CLASS_PROPERTY; @@ -294,17 +262,15 @@ int Analog_Input_Read_Property( } /* returns true if successful */ -bool Analog_Input_Write_Property( - BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Analog_Input_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data) { - bool status = false; /* return value */ + bool status = false; /* return value */ int len = 0; BACNET_APPLICATION_DATA_VALUE value; /* decode the some of the request */ - len = - bacapp_decode_application_data(wp_data->application_data, - wp_data->application_data_len, &value); + len = bacapp_decode_application_data( + wp_data->application_data, wp_data->application_data_len, &value); /* FIXME: len < application_data_len: more data? */ if (len < 0) { /* error while decoding - a value larger than we can handle */ @@ -319,14 +285,14 @@ bool Analog_Input_Write_Property( wp_data->error_code = ERROR_CODE_PROPERTY_IS_NOT_AN_ARRAY; return false; } - switch ((int) wp_data->object_property) { + switch ((int)wp_data->object_property) { case PROP_PRESENT_VALUE: status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_REAL, &wp_data->error_class, &wp_data->error_code); if (status) { if (Analog_Input_Out_Of_Service(wp_data->object_instance)) { - Analog_Input_Present_Value_Set(wp_data->object_instance, - value.type.Real); + Analog_Input_Present_Value_Set( + wp_data->object_instance, value.type.Real); } else { wp_data->error_class = ERROR_CLASS_PROPERTY; wp_data->error_code = ERROR_CODE_WRITE_ACCESS_DENIED; @@ -335,23 +301,20 @@ bool Analog_Input_Write_Property( } break; case PROP_OUT_OF_SERVICE: - status = - WPValidateArgType(&value, BACNET_APPLICATION_TAG_BOOLEAN, + status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_BOOLEAN, &wp_data->error_class, &wp_data->error_code); if (status) { Analog_Input_Out_Of_Service_Set( - wp_data->object_instance, - value.type.Boolean); + wp_data->object_instance, value.type.Boolean); } break; case PROP_UNITS: status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_ENUMERATED, - &wp_data->error_class, &wp_data->error_code); + &wp_data->error_class, &wp_data->error_code); if (status) { Analog_Input_Out_Of_Service_Set( - wp_data->object_instance, - value.type.Enumerated); + wp_data->object_instance, value.type.Enumerated); } break; case PROP_OBJECT_IDENTIFIER: diff --git a/ports/xplained/bacnet.c b/ports/xplained/bacnet.c index ed543d29..39a49755 100644 --- a/ports/xplained/bacnet.c +++ b/ports/xplained/bacnet.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2013 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2013 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include #include @@ -77,10 +77,10 @@ static struct mstimer Reinit_Timer; static uint8_t PDUBuffer[MAX_MPDU]; /************************************************************************** -* Description: handles reinitializing the device after a few seconds -* Returns: none -* Notes: gives the device enough time to acknowledge the RD request -**************************************************************************/ + * Description: handles reinitializing the device after a few seconds + * Returns: none + * Notes: gives the device enough time to acknowledge the RD request + **************************************************************************/ static void reinit_task(void) { BACNET_REINITIALIZED_STATE state = BACNET_REINIT_IDLE; @@ -100,12 +100,11 @@ static void reinit_task(void) } /************************************************************************** -* Description: handles recurring strictly timed task -* Returns: none -* Notes: called by ISR every 5 milliseconds -**************************************************************************/ -void bacnet_task_timed( - void) + * Description: handles recurring strictly timed task + * Returns: none + * Notes: called by ISR every 5 milliseconds + **************************************************************************/ +void bacnet_task_timed(void) { struct mstp_rx_packet *pkt = NULL; uint16_t pdu_len = 0; @@ -113,7 +112,7 @@ void bacnet_task_timed( pdu_len = dlmstp_receive(&src, &PDUBuffer[0], sizeof(PDUBuffer), 5); if (pdu_len) { - pkt = (struct mstp_rx_packet *) Ringbuf_Data_Peek(&Receive_Queue); + pkt = (struct mstp_rx_packet *)Ringbuf_Data_Peek(&Receive_Queue); if (pkt) { memcpy(pkt->buffer, PDUBuffer, MAX_MPDU); bacnet_address_copy(&pkt->src, &src); @@ -124,38 +123,38 @@ void bacnet_task_timed( } /************************************************************************** -* Description: handles recurring task -* Returns: none -* Notes: none -**************************************************************************/ + * Description: handles recurring task + * Returns: none + * Notes: none + **************************************************************************/ static void bacnet_test_task(void) { - static unsigned index = 0; - uint32_t instance; - float float_value; - uint16_t adc_value; + static unsigned index = 0; + uint32_t instance; + float float_value; + uint16_t adc_value; instance = Analog_Input_Index_To_Instance(index); if (!Analog_Input_Out_Of_Service(instance)) { - adc_value = adc_result_12bit(index); + adc_value = adc_result_12bit(index); float_value = adc_value; float_value /= 4095; Analog_Input_Present_Value_Set(instance, float_value); } index++; - if (index >= MAX_ANALOG_INPUTS) { + if (index >= MAX_ANALOG_INPUTS) { index = 0; } } /************************************************************************** -* Description: handles recurring task -* Returns: none -* Notes: none -**************************************************************************/ + * Description: handles recurring task + * Returns: none + * Notes: none + **************************************************************************/ void bacnet_task(void) { - struct mstp_rx_packet pkt = {{0}}; + struct mstp_rx_packet pkt = { { 0 } }; bool pdu_available = false; /* hello, World! */ @@ -167,68 +166,65 @@ void bacnet_task(void) if (mstimer_expired(&DCC_Timer)) { mstimer_reset(&DCC_Timer); dcc_timer_seconds(DCC_CYCLE_SECONDS); - led_on_interval(LED_DEBUG,500); + led_on_interval(LED_DEBUG, 500); } if (mstimer_expired(&TSM_Timer)) { mstimer_reset(&TSM_Timer); tsm_timer_milliseconds(mstimer_interval(&TSM_Timer)); } reinit_task(); - bacnet_test_task(); + bacnet_test_task(); /* handle the messaging */ - if ((!dlmstp_send_pdu_queue_full()) && - (!Ringbuf_Empty(&Receive_Queue))) { + if ((!dlmstp_send_pdu_queue_full()) && (!Ringbuf_Empty(&Receive_Queue))) { Ringbuf_Pop(&Receive_Queue, (uint8_t *)&pkt); pdu_available = true; } if (pdu_available) { - led_on_interval(LED_APDU,125); + led_on_interval(LED_APDU, 125); npdu_handler(&pkt.src, &pkt.buffer[0], pkt.length); } } /************************************************************************** -* Description: initializes the BACnet library -* Returns: none -* Notes: none -**************************************************************************/ + * Description: initializes the BACnet library + * Returns: none + * Notes: none + **************************************************************************/ void bacnet_init(void) { unsigned i; - Ringbuf_Init(&Receive_Queue, (uint8_t *) & Receive_Buffer, + Ringbuf_Init(&Receive_Queue, (uint8_t *)&Receive_Buffer, sizeof(struct mstp_rx_packet), MSTP_RECEIVE_PACKET_COUNT); dlmstp_init(NULL); /* initialize objects */ Device_Init(NULL); /* set up our confirmed service unrecognized service handler - required! */ - apdu_set_unrecognized_service_handler_handler - (handler_unrecognized_service); + apdu_set_unrecognized_service_handler_handler(handler_unrecognized_service); /* 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); /* 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, - handler_read_property); - apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROP_MULTIPLE, - handler_read_property_multiple); - apdu_set_confirmed_handler(SERVICE_CONFIRMED_REINITIALIZE_DEVICE, - handler_reinitialize_device); - apdu_set_confirmed_handler(SERVICE_CONFIRMED_WRITE_PROPERTY, - handler_write_property); + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_READ_PROPERTY, handler_read_property); + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_READ_PROP_MULTIPLE, handler_read_property_multiple); + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_REINITIALIZE_DEVICE, handler_reinitialize_device); + apdu_set_confirmed_handler( + SERVICE_CONFIRMED_WRITE_PROPERTY, handler_write_property); /* handle communication so we can shut up when asked */ apdu_set_confirmed_handler(SERVICE_CONFIRMED_DEVICE_COMMUNICATION_CONTROL, handler_device_communication_control); /* start the cyclic 1 second timer for DCC */ - mstimer_set(&DCC_Timer, DCC_CYCLE_SECONDS*1000); + mstimer_set(&DCC_Timer, DCC_CYCLE_SECONDS * 1000); /* start the cyclic 1 second timer for COV */ - mstimer_set(&COV_Timer, COV_CYCLE_SECONDS*1000); + mstimer_set(&COV_Timer, COV_CYCLE_SECONDS * 1000); /* start the cyclic 1 second timer for TSM */ - mstimer_set(&TSM_Timer, TSM_CYCLE_SECONDS*1000); - for (i = 0; i < MAX_ANALOG_INPUTS; i++) { + mstimer_set(&TSM_Timer, TSM_CYCLE_SECONDS * 1000); + for (i = 0; i < MAX_ANALOG_INPUTS; i++) { Analog_Input_Units_Set( - Analog_Input_Index_To_Instance(i), - UNITS_PERCENT); + Analog_Input_Index_To_Instance(i), UNITS_PERCENT); } } diff --git a/ports/xplained/bname.c b/ports/xplained/bname.c index f27d3110..45c2c893 100644 --- a/ports/xplained/bname.c +++ b/ports/xplained/bname.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2011 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2011 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include #include @@ -34,13 +34,11 @@ #include "bname.h" /************************************************************************* -* DESCRIPTION: Test the BACnet CharacterString for validity -* RETURN: true if valid -* NOTES: none -**************************************************************************/ -static bool bacnet_name_isvalid(uint8_t encoding, - uint8_t length, - char *str) + * DESCRIPTION: Test the BACnet CharacterString for validity + * RETURN: true if valid + * NOTES: none + **************************************************************************/ +static bool bacnet_name_isvalid(uint8_t encoding, uint8_t length, char *str) { bool valid = false; @@ -57,14 +55,11 @@ static bool bacnet_name_isvalid(uint8_t encoding, } /************************************************************************* -* DESCRIPTION: Copy the name from non-volatile memory at offset -* RETURN: number of bytes read, or -1 on error -* NOTES: none -**************************************************************************/ -int bacnet_name_copy( - uint16_t offset, - uint8_t *dest, - uint8_t dest_len) + * DESCRIPTION: Copy the name from non-volatile memory at offset + * RETURN: number of bytes read, or -1 on error + * NOTES: none + **************************************************************************/ +int bacnet_name_copy(uint16_t offset, uint8_t *dest, uint8_t dest_len) { uint8_t encoding = 0; uint8_t length = 0; @@ -74,9 +69,8 @@ int bacnet_name_copy( nvm_read(NVM_NAME_ENCODING(offset), &encoding, 1); nvm_read(NVM_NAME_LENGTH(offset), &length, 1); - nvm_read(NVM_NAME_STRING(offset), - (uint8_t *) & name, NVM_NAME_SIZE); - if (bacnet_name_isvalid(encoding, length, name)) { + nvm_read(NVM_NAME_STRING(offset), (uint8_t *)&name, NVM_NAME_SIZE); + if (bacnet_name_isvalid(encoding, length, name)) { if (dest_len > NVM_NAME_SIZE) { dest_len = NVM_NAME_SIZE; } @@ -98,12 +92,11 @@ int bacnet_name_copy( } /************************************************************************* -* DESCRIPTION: Encode the name in a buffer in the sequence stored in EEPROM. -* RETURN: number of bytes in buffer, or 0 if too big to fit. -* NOTES: none -**************************************************************************/ -uint8_t bacnet_name_encode( - uint8_t *buffer, + * DESCRIPTION: Encode the name in a buffer in the sequence stored in EEPROM. + * RETURN: number of bytes in buffer, or 0 if too big to fit. + * NOTES: none + **************************************************************************/ +uint8_t bacnet_name_encode(uint8_t *buffer, uint8_t buffer_len, uint8_t encoding, char *str, @@ -112,13 +105,13 @@ uint8_t bacnet_name_encode( unsigned len = 0; unsigned i = 0; - if (str_len < (255-2)) { + if (str_len < (255 - 2)) { len = 1 + 1 + str_len; if (len <= buffer_len) { buffer[NVM_NAME_LENGTH(0)] = str_len; buffer[NVM_NAME_ENCODING(0)] = encoding; for (i = 0; i < str_len; i++) { - buffer[NVM_NAME_STRING(0)+i] = str[i]; + buffer[NVM_NAME_STRING(0) + i] = str[i]; } } else { len = 0; @@ -129,30 +122,21 @@ uint8_t bacnet_name_encode( } /************************************************************************* -* DESCRIPTION: Store the name to non-volatile memory at offset -* RETURN: true if name is a valid set of characters -* NOTES: none -**************************************************************************/ + * DESCRIPTION: Store the name to non-volatile memory at offset + * RETURN: true if name is a valid set of characters + * NOTES: none + **************************************************************************/ bool bacnet_name_save( - uint16_t offset, - uint8_t encoding, - char *str, - uint8_t str_len) + uint16_t offset, uint8_t encoding, char *str, uint8_t str_len) { uint8_t buffer[NVM_NAME_SIZE] = { 0 }; uint8_t length = 0; if (bacnet_name_isvalid(encoding, str_len, str)) { - length = bacnet_name_encode( - buffer, - sizeof(buffer), - encoding, - str, - str_len); + length = + bacnet_name_encode(buffer, sizeof(buffer), encoding, str, str_len); if (length) { - nvm_write( - offset, - &buffer[0],length); + nvm_write(offset, &buffer[0], length); return true; } } @@ -160,8 +144,7 @@ bool bacnet_name_save( return false; } -bool bacnet_name_set(uint16_t offset, - BACNET_CHARACTER_STRING * char_string) +bool bacnet_name_set(uint16_t offset, BACNET_CHARACTER_STRING *char_string) { uint8_t encoding = 0; uint8_t length = 0; @@ -176,9 +159,9 @@ bool bacnet_name_set(uint16_t offset, bool bacnet_name_write_unique(uint16_t offset, int object_type, uint32_t object_instance, - BACNET_CHARACTER_STRING * char_string, - BACNET_ERROR_CLASS * error_class, - BACNET_ERROR_CODE * error_code) + BACNET_CHARACTER_STRING *char_string, + BACNET_ERROR_CLASS *error_class, + BACNET_ERROR_CODE *error_code) { bool status = false; size_t length = 0; @@ -193,8 +176,8 @@ bool bacnet_name_write_unique(uint16_t offset, } else if (length <= NVM_NAME_SIZE) { encoding = characterstring_encoding(char_string); if (encoding < MAX_CHARACTER_STRING_ENCODING) { - if (Device_Valid_Object_Name(char_string, &duplicate_type, - &duplicate_instance)) { + if (Device_Valid_Object_Name( + char_string, &duplicate_type, &duplicate_instance)) { if ((duplicate_type == object_type) && (duplicate_instance == object_instance)) { /* writing same name to same object */ @@ -226,9 +209,9 @@ bool bacnet_name_write_unique(uint16_t offset, /* no required minumum length or duplicate checking */ bool bacnet_name_write(uint16_t offset, - BACNET_CHARACTER_STRING * char_string, - BACNET_ERROR_CLASS * error_class, - BACNET_ERROR_CODE * error_code) + BACNET_CHARACTER_STRING *char_string, + BACNET_ERROR_CLASS *error_class, + BACNET_ERROR_CODE *error_code) { bool status = false; size_t length = 0; @@ -255,16 +238,14 @@ bool bacnet_name_write(uint16_t offset, return status; } -void bacnet_name_init(uint16_t offset, - char *default_string) +void bacnet_name_init(uint16_t offset, char *default_string) { - (void) bacnet_name_save(offset, CHARACTER_UTF8, default_string, - strlen(default_string)); + (void)bacnet_name_save( + offset, CHARACTER_UTF8, default_string, strlen(default_string)); } -void bacnet_name(uint16_t offset, - BACNET_CHARACTER_STRING * char_string, - char *default_string) +void bacnet_name( + uint16_t offset, BACNET_CHARACTER_STRING *char_string, char *default_string) { uint8_t encoding = 0; uint8_t length = 0; @@ -272,7 +253,7 @@ void bacnet_name(uint16_t offset, nvm_read(NVM_NAME_ENCODING(offset), &encoding, 1); nvm_read(NVM_NAME_LENGTH(offset), &length, 1); - nvm_read(NVM_NAME_STRING(offset), (uint8_t *) & name[0], NVM_NAME_SIZE); + nvm_read(NVM_NAME_STRING(offset), (uint8_t *)&name[0], NVM_NAME_SIZE); if (bacnet_name_isvalid(encoding, length, name)) { characterstring_init(char_string, encoding, &name[0], length); } else if (default_string) { diff --git a/ports/xplained/device.c b/ports/xplained/device.c index d4af5057..4ab69bc6 100644 --- a/ports/xplained/device.c +++ b/ports/xplained/device.c @@ -1,27 +1,27 @@ /************************************************************************** -* -* Copyright (C) 2015 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2015 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include #include #include @@ -45,8 +45,8 @@ #include "bacnet/basic/object/ai.h" /* forward prototype */ -int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA * rpdata); -bool Device_Write_Property_Local(BACNET_WRITE_PROPERTY_DATA * wp_data); +int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA *rpdata); +bool Device_Write_Property_Local(BACNET_WRITE_PROPERTY_DATA *wp_data); static struct my_object_functions { BACNET_OBJECT_TYPE Object_Type; @@ -62,19 +62,16 @@ static struct my_object_functions { object_cov_function Object_COV; object_cov_clear_function Object_COV_Clear; } Object_Table[] = { - { - OBJECT_DEVICE, NULL, - Device_Count, Device_Index_To_Instance, - Device_Valid_Object_Instance_Number, Device_Object_Name, - Device_Read_Property_Local, Device_Write_Property_Local, - Device_Property_Lists, - NULL, NULL, NULL}, { - OBJECT_ANALOG_INPUT, Analog_Input_Init, Analog_Input_Count, - Analog_Input_Index_To_Instance, Analog_Input_Valid_Instance, - Analog_Input_Object_Name, Analog_Input_Read_Property, - Analog_Input_Write_Property, Analog_Input_Property_Lists, - NULL, NULL, NULL}, { - MAX_BACNET_OBJECT_TYPE, NULL, NULL, NULL, NULL, NULL, NULL, NULL} + { OBJECT_DEVICE, NULL, Device_Count, Device_Index_To_Instance, + Device_Valid_Object_Instance_Number, Device_Object_Name, + Device_Read_Property_Local, Device_Write_Property_Local, + Device_Property_Lists, NULL, NULL, NULL }, + { OBJECT_ANALOG_INPUT, Analog_Input_Init, Analog_Input_Count, + Analog_Input_Index_To_Instance, Analog_Input_Valid_Instance, + Analog_Input_Object_Name, Analog_Input_Read_Property, + Analog_Input_Write_Property, Analog_Input_Property_Lists, NULL, NULL, + NULL }, + { MAX_BACNET_OBJECT_TYPE, NULL, NULL, NULL, NULL, NULL, NULL, NULL } }; /* note: you really only need to define variables for @@ -87,44 +84,23 @@ static uint32_t Database_Revision; static BACNET_REINITIALIZED_STATE Reinitialize_State = BACNET_REINIT_IDLE; /* These three arrays are used by the ReadPropertyMultiple handler */ -static const int Device_Properties_Required[] = { - PROP_OBJECT_IDENTIFIER, - PROP_OBJECT_NAME, - PROP_OBJECT_TYPE, - PROP_SYSTEM_STATUS, - PROP_VENDOR_NAME, - PROP_VENDOR_IDENTIFIER, - PROP_MODEL_NAME, - PROP_FIRMWARE_REVISION, - PROP_APPLICATION_SOFTWARE_VERSION, - PROP_PROTOCOL_VERSION, - PROP_PROTOCOL_REVISION, - PROP_PROTOCOL_SERVICES_SUPPORTED, - PROP_PROTOCOL_OBJECT_TYPES_SUPPORTED, - PROP_OBJECT_LIST, - PROP_MAX_APDU_LENGTH_ACCEPTED, - PROP_SEGMENTATION_SUPPORTED, - PROP_APDU_TIMEOUT, - PROP_NUMBER_OF_APDU_RETRIES, - PROP_DEVICE_ADDRESS_BINDING, - PROP_DATABASE_REVISION, - -1 -}; +static const int Device_Properties_Required[] = { PROP_OBJECT_IDENTIFIER, + PROP_OBJECT_NAME, PROP_OBJECT_TYPE, PROP_SYSTEM_STATUS, PROP_VENDOR_NAME, + PROP_VENDOR_IDENTIFIER, PROP_MODEL_NAME, PROP_FIRMWARE_REVISION, + PROP_APPLICATION_SOFTWARE_VERSION, PROP_PROTOCOL_VERSION, + PROP_PROTOCOL_REVISION, PROP_PROTOCOL_SERVICES_SUPPORTED, + PROP_PROTOCOL_OBJECT_TYPES_SUPPORTED, PROP_OBJECT_LIST, + PROP_MAX_APDU_LENGTH_ACCEPTED, PROP_SEGMENTATION_SUPPORTED, + PROP_APDU_TIMEOUT, PROP_NUMBER_OF_APDU_RETRIES, PROP_DEVICE_ADDRESS_BINDING, + PROP_DATABASE_REVISION, -1 }; -static const int Device_Properties_Optional[] = { - PROP_MAX_MASTER, - PROP_MAX_INFO_FRAMES, - PROP_DESCRIPTION, - PROP_LOCATION, - -1 -}; +static const int Device_Properties_Optional[] = { PROP_MAX_MASTER, + PROP_MAX_INFO_FRAMES, PROP_DESCRIPTION, PROP_LOCATION, -1 }; -static const int Device_Properties_Proprietary[] = { - -1 -}; +static const int Device_Properties_Proprietary[] = { -1 }; -static struct my_object_functions - *Device_Objects_Find_Functions(BACNET_OBJECT_TYPE Object_Type) +static struct my_object_functions *Device_Objects_Find_Functions( + BACNET_OBJECT_TYPE Object_Type) { struct my_object_functions *pObject = NULL; @@ -142,7 +118,7 @@ static struct my_object_functions /* Encodes the property APDU and returns the length, or sets the error, and returns BACNET_STATUS_ERROR */ -int Device_Read_Property(BACNET_READ_PROPERTY_DATA * rpdata) +int Device_Read_Property(BACNET_READ_PROPERTY_DATA *rpdata) { int apdu_len = BACNET_STATUS_ERROR; struct my_object_functions *pObject = NULL; @@ -163,7 +139,7 @@ int Device_Read_Property(BACNET_READ_PROPERTY_DATA * rpdata) return apdu_len; } -bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA *wp_data) { bool status = false; struct my_object_functions *pObject = NULL; @@ -192,8 +168,7 @@ bool Device_Write_Property(BACNET_WRITE_PROPERTY_DATA * wp_data) } /* for a given object type, returns the special property list */ -void Device_Objects_Property_List( - BACNET_OBJECT_TYPE object_type, +void Device_Objects_Property_List(BACNET_OBJECT_TYPE object_type, uint32_t object_instance, struct special_property_list_t *pPropertyList) { @@ -216,24 +191,23 @@ void Device_Objects_Property_List( } /* Fetch the counts if available otherwise zero them */ - pPropertyList->Required.count = - pPropertyList->Required.pList == - NULL ? 0 : property_list_count(pPropertyList->Required.pList); + pPropertyList->Required.count = pPropertyList->Required.pList == NULL + ? 0 + : property_list_count(pPropertyList->Required.pList); - pPropertyList->Optional.count = - pPropertyList->Optional.pList == - NULL ? 0 : property_list_count(pPropertyList->Optional.pList); + pPropertyList->Optional.count = pPropertyList->Optional.pList == NULL + ? 0 + : property_list_count(pPropertyList->Optional.pList); - pPropertyList->Proprietary.count = - pPropertyList->Proprietary.pList == - NULL ? 0 : property_list_count(pPropertyList->Proprietary.pList); + pPropertyList->Proprietary.count = pPropertyList->Proprietary.pList == NULL + ? 0 + : property_list_count(pPropertyList->Proprietary.pList); return; } -void Device_Property_Lists(const int **pRequired, - const int **pOptional, - const int **pProprietary) +void Device_Property_Lists( + const int **pRequired, const int **pOptional, const int **pProprietary) { if (pRequired) *pRequired = Device_Properties_Required; @@ -256,10 +230,9 @@ uint32_t Device_Index_To_Instance(unsigned index) return Object_Instance_Number; } -static char *Device_Name_Default( - void) +static char *Device_Name_Default(void) { - static char text_string[32]; /* okay for single thread */ + static char text_string[32]; /* okay for single thread */ sprintf(text_string, "DEVICE-%lu", Object_Instance_Number); @@ -267,8 +240,7 @@ static char *Device_Name_Default( } bool Device_Object_Name( - uint32_t object_instance, - BACNET_CHARACTER_STRING * object_name) + uint32_t object_instance, BACNET_CHARACTER_STRING *object_name) { bool status = false; @@ -300,7 +272,7 @@ const char *Device_Application_Software_Version(void) return BACNET_VERSION_TEXT; } -bool Device_Reinitialize(BACNET_REINITIALIZE_DEVICE_DATA * rd_data) +bool Device_Reinitialize(BACNET_REINITIALIZE_DEVICE_DATA *rd_data) { bool status = false; @@ -326,13 +298,13 @@ BACNET_REINITIALIZED_STATE Device_Reinitialized_State(void) return Reinitialize_State; } -void Device_Init(object_functions_t * object_table) +void Device_Init(object_functions_t *object_table) { struct my_object_functions *pObject = NULL; /* we don't use the object table passed in since there is extra stuff we don't need in there. */ - (void) object_table; + (void)object_table; /* our local object table */ pObject = &Object_Table[0]; while (pObject->Object_Type < MAX_BACNET_OBJECT_TYPE) { @@ -375,8 +347,7 @@ BACNET_DEVICE_STATUS Device_System_Status(void) return System_Status; } -int Device_Set_System_Status(BACNET_DEVICE_STATUS status, - bool local) +int Device_Set_System_Status(BACNET_DEVICE_STATUS status, bool local) { /*return value - 0 = ok, -1 = bad value, -2 = not allowed */ int result = -1; @@ -409,22 +380,20 @@ void Device_Inc_Database_Revision(void) Database_Revision++; } -bool Device_Encode_Value_List( - BACNET_OBJECT_TYPE object_type, +bool Device_Encode_Value_List(BACNET_OBJECT_TYPE object_type, uint32_t object_instance, - BACNET_PROPERTY_VALUE * value_list) + BACNET_PROPERTY_VALUE *value_list) { - bool status = false; /* Ever the pessamist! */ + bool status = false; /* Ever the pessamist! */ struct my_object_functions *pObject = NULL; pObject = Device_Objects_Find_Functions(object_type); if (pObject != NULL) { if (pObject->Object_Valid_Instance && - pObject->Object_Valid_Instance(object_instance)) { + pObject->Object_Valid_Instance(object_instance)) { if (pObject->Object_Value_List) { - status = pObject->Object_Value_List( - object_instance, - value_list); + status = + pObject->Object_Value_List(object_instance, value_list); } } } @@ -432,20 +401,17 @@ bool Device_Encode_Value_List( return (status); } -bool Device_COV( - BACNET_OBJECT_TYPE object_type, - uint32_t object_instance) +bool Device_COV(BACNET_OBJECT_TYPE object_type, uint32_t object_instance) { - bool status = false; /* Ever the pessamist! */ + bool status = false; /* Ever the pessamist! */ struct my_object_functions *pObject = NULL; pObject = Device_Objects_Find_Functions(object_type); if (pObject != NULL) { if (pObject->Object_Valid_Instance && - pObject->Object_Valid_Instance(object_instance)) { + pObject->Object_Valid_Instance(object_instance)) { if (pObject->Object_COV) { - status = pObject->Object_COV( - object_instance); + status = pObject->Object_COV(object_instance); } } } @@ -453,16 +419,14 @@ bool Device_COV( return (status); } -void Device_COV_Clear( - BACNET_OBJECT_TYPE object_type, - uint32_t object_instance) +void Device_COV_Clear(BACNET_OBJECT_TYPE object_type, uint32_t object_instance) { struct my_object_functions *pObject = NULL; pObject = Device_Objects_Find_Functions(object_type); if (pObject != NULL) { if (pObject->Object_Valid_Instance && - pObject->Object_Valid_Instance(object_instance)) { + pObject->Object_Valid_Instance(object_instance)) { if (pObject->Object_COV_Clear) { pObject->Object_COV_Clear(object_instance); } @@ -470,10 +434,9 @@ void Device_COV_Clear( } } -bool Device_Value_List_Supported( - BACNET_OBJECT_TYPE object_type) +bool Device_Value_List_Supported(BACNET_OBJECT_TYPE object_type) { - bool status = false; /* Ever the pessimist! */ + bool status = false; /* Ever the pessimist! */ struct my_object_functions *pObject = NULL; pObject = Device_Objects_Find_Functions(object_type); @@ -505,9 +468,8 @@ unsigned Device_Object_List_Count(void) return count; } -bool Device_Object_List_Identifier(uint32_t array_index, - BACNET_OBJECT_TYPE *object_type, - uint32_t * instance) +bool Device_Object_List_Identifier( + uint32_t array_index, BACNET_OBJECT_TYPE *object_type, uint32_t *instance) { bool status = false; uint32_t count = 0; @@ -538,9 +500,9 @@ bool Device_Object_List_Identifier(uint32_t array_index, return status; } -bool Device_Valid_Object_Name(BACNET_CHARACTER_STRING * object_name1, +bool Device_Valid_Object_Name(BACNET_CHARACTER_STRING *object_name1, BACNET_OBJECT_TYPE *object_type, - uint32_t * object_instance) + uint32_t *object_instance) { bool found = false; BACNET_OBJECT_TYPE type = OBJECT_NONE; @@ -554,7 +516,7 @@ bool Device_Valid_Object_Name(BACNET_CHARACTER_STRING * object_name1, for (i = 1; i <= max_objects; i++) { check_id = Device_Object_List_Identifier(i, &type, &instance); if (check_id) { - pObject = Device_Objects_Find_Functions((BACNET_OBJECT_TYPE) type); + pObject = Device_Objects_Find_Functions((BACNET_OBJECT_TYPE)type); if ((pObject != NULL) && (pObject->Object_Name != NULL) && (pObject->Object_Name(instance, &object_name2) && characterstring_same(object_name1, &object_name2))) { @@ -573,13 +535,13 @@ bool Device_Valid_Object_Name(BACNET_CHARACTER_STRING * object_name1, return found; } -bool Device_Valid_Object_Id(BACNET_OBJECT_TYPE object_type, - uint32_t object_instance) +bool Device_Valid_Object_Id( + BACNET_OBJECT_TYPE object_type, uint32_t object_instance) { - bool status = false; /* return value */ + bool status = false; /* return value */ struct my_object_functions *pObject = NULL; - pObject = Device_Objects_Find_Functions((BACNET_OBJECT_TYPE) object_type); + pObject = Device_Objects_Find_Functions((BACNET_OBJECT_TYPE)object_type); if ((pObject != NULL) && (pObject->Object_Valid_Instance != NULL)) { status = pObject->Object_Valid_Instance(object_instance); } @@ -589,7 +551,7 @@ bool Device_Valid_Object_Id(BACNET_OBJECT_TYPE object_type, bool Device_Object_Name_Copy(BACNET_OBJECT_TYPE object_type, uint32_t object_instance, - BACNET_CHARACTER_STRING * object_name) + BACNET_CHARACTER_STRING *object_name) { struct my_object_functions *pObject = NULL; bool found = false; @@ -603,10 +565,10 @@ bool Device_Object_Name_Copy(BACNET_OBJECT_TYPE object_type, } /* return the length of the apdu encoded or BACNET_STATUS_ERROR for error */ -int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA * rpdata) +int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA *rpdata) { - 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 = { 0 }; BACNET_CHARACTER_STRING char_string = { 0 }; uint32_t i = 0; @@ -621,11 +583,10 @@ int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA * rpdata) return 0; } apdu = rpdata->application_data; - switch ((int) rpdata->object_property) { + switch ((int)rpdata->object_property) { case PROP_OBJECT_IDENTIFIER: - apdu_len = - encode_application_object_id(&apdu[0], rpdata->object_type, - rpdata->object_instance); + apdu_len = encode_application_object_id( + &apdu[0], rpdata->object_type, rpdata->object_instance); break; case PROP_OBJECT_NAME: Device_Object_Name(rpdata->object_instance, &char_string); @@ -637,21 +598,19 @@ int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA * rpdata) encode_application_enumerated(&apdu[0], rpdata->object_type); break; case PROP_DESCRIPTION: - bacnet_name(NVM_DEVICE_DESCRIPTION, &char_string, - "default description"); + bacnet_name( + NVM_DEVICE_DESCRIPTION, &char_string, "default description"); apdu_len = encode_application_character_string(&apdu[0], &char_string); break; case PROP_LOCATION: - bacnet_name(NVM_DEVICE_LOCATION, &char_string, - "default location"); + bacnet_name(NVM_DEVICE_LOCATION, &char_string, "default location"); 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()); + encode_application_enumerated(&apdu[0], Device_System_Status()); break; case PROP_VENDOR_NAME: characterstring_init_ansi(&char_string, Device_Vendor_Name()); @@ -667,14 +626,13 @@ int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA * rpdata) encode_application_character_string(&apdu[0], &char_string); break; case PROP_FIRMWARE_REVISION: - characterstring_init_ansi(&char_string, - Device_Firmware_Revision()); + characterstring_init_ansi(&char_string, Device_Firmware_Revision()); apdu_len = encode_application_character_string(&apdu[0], &char_string); break; case PROP_APPLICATION_SOFTWARE_VERSION: - characterstring_init_ansi(&char_string, - Device_Application_Software_Version()); + characterstring_init_ansi( + &char_string, Device_Application_Software_Version()); apdu_len = encode_application_character_string(&apdu[0], &char_string); break; @@ -684,16 +642,15 @@ int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA * rpdata) break; case PROP_PROTOCOL_REVISION: apdu_len = - encode_application_unsigned(&apdu[0], - BACNET_PROTOCOL_REVISION); + encode_application_unsigned(&apdu[0], BACNET_PROTOCOL_REVISION); 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)); + 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; @@ -703,7 +660,7 @@ int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA * rpdata) 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); + bitstring_set_bit(&bit_string, (uint8_t)i, false); } /* set the object types with objects to supported */ i = 0; @@ -727,11 +684,10 @@ int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA * rpdata) /* your maximum APDU size. */ else if (rpdata->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); + 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? */ @@ -751,11 +707,10 @@ int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA * rpdata) } } } else { - if (Device_Object_List_Identifier(rpdata->array_index, - &object_type, &instance)) - apdu_len = - encode_application_object_id(&apdu[0], object_type, - instance); + if (Device_Object_List_Identifier( + rpdata->array_index, &object_type, &instance)) + apdu_len = encode_application_object_id( + &apdu[0], object_type, instance); else { rpdata->error_class = ERROR_CLASS_PROPERTY; rpdata->error_code = ERROR_CODE_INVALID_ARRAY_INDEX; @@ -767,9 +722,8 @@ int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA * rpdata) apdu_len = encode_application_unsigned(&apdu[0], MAX_APDU); break; case PROP_SEGMENTATION_SUPPORTED: - apdu_len = - encode_application_enumerated(&apdu[0], - Device_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()); @@ -781,14 +735,12 @@ int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA * rpdata) /* FIXME: encode the list here, if it exists */ break; case PROP_DATABASE_REVISION: - apdu_len = - encode_application_unsigned(&apdu[0], - Device_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()); + encode_application_unsigned(&apdu[0], dlmstp_max_info_frames()); break; case PROP_MAX_MASTER: apdu_len = @@ -817,17 +769,16 @@ int Device_Read_Property_Local(BACNET_READ_PROPERTY_DATA * rpdata) return apdu_len; } -bool Device_Write_Property_Local(BACNET_WRITE_PROPERTY_DATA * wp_data) +bool Device_Write_Property_Local(BACNET_WRITE_PROPERTY_DATA *wp_data) { - bool status = false; /* return value - false=error */ + bool status = false; /* return value - false=error */ int len = 0; BACNET_APPLICATION_DATA_VALUE value; uint8_t max_master = 0; /* decode the some of the request */ - len = - bacapp_decode_application_data(wp_data->application_data, - wp_data->application_data_len, &value); + len = bacapp_decode_application_data( + wp_data->application_data, wp_data->application_data_len, &value); /* FIXME: len < application_data_len: more data? */ if (len < 0) { /* error while decoding - a value larger than we can handle */ @@ -842,14 +793,14 @@ bool Device_Write_Property_Local(BACNET_WRITE_PROPERTY_DATA * wp_data) wp_data->error_code = ERROR_CODE_PROPERTY_IS_NOT_AN_ARRAY; return false; } - switch ((int) wp_data->object_property) { + switch ((int)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))) { + (Device_Set_Object_Instance_Number( + value.type.Object_Id.instance))) { nvm_write(NVM_DEVICE_0, - (uint8_t *) & value.type.Object_Id.instance, 4); + (uint8_t *)&value.type.Object_Id.instance, 4); /* we could send an I-Am broadcast to let the world know */ status = true; } else { @@ -894,8 +845,7 @@ bool Device_Write_Property_Local(BACNET_WRITE_PROPERTY_DATA * wp_data) break; case PROP_OBJECT_NAME: if (value.tag == BACNET_APPLICATION_TAG_CHARACTER_STRING) { - status = - bacnet_name_write_unique(NVM_DEVICE_NAME, + status = bacnet_name_write_unique(NVM_DEVICE_NAME, wp_data->object_type, wp_data->object_instance, &value.type.Character_String, &wp_data->error_class, &wp_data->error_code); @@ -906,8 +856,7 @@ bool Device_Write_Property_Local(BACNET_WRITE_PROPERTY_DATA * wp_data) break; case PROP_DESCRIPTION: if (value.tag == BACNET_APPLICATION_TAG_CHARACTER_STRING) { - status = - bacnet_name_write(NVM_DEVICE_DESCRIPTION, + status = bacnet_name_write(NVM_DEVICE_DESCRIPTION, &value.type.Character_String, &wp_data->error_class, &wp_data->error_code); } else { @@ -917,8 +866,7 @@ bool Device_Write_Property_Local(BACNET_WRITE_PROPERTY_DATA * wp_data) break; case PROP_LOCATION: if (value.tag == BACNET_APPLICATION_TAG_CHARACTER_STRING) { - status = - bacnet_name_write(NVM_DEVICE_LOCATION, + status = bacnet_name_write(NVM_DEVICE_LOCATION, &value.type.Character_String, &wp_data->error_class, &wp_data->error_code); } else { diff --git a/ports/xplained/dlmstp.c b/ports/xplained/dlmstp.c index 6a1cd0ee..122c956f 100644 --- a/ports/xplained/dlmstp.c +++ b/ports/xplained/dlmstp.c @@ -68,19 +68,19 @@ static volatile struct mstp_flag_t { /* A Boolean flag set to TRUE by the Receive State Machine */ /* if an invalid frame is received. */ /* Set to FALSE by the main state machine. */ - unsigned ReceivedInvalidFrame:1; + unsigned ReceivedInvalidFrame : 1; /* A Boolean flag set to TRUE by the Receive State Machine */ /* if a valid frame is received. */ /* Set to FALSE by the main state machine. */ - unsigned ReceivedValidFrame:1; + unsigned ReceivedValidFrame : 1; /* set to TRUE when we get a frame not for us */ - unsigned ReceivedValidFrameNotForUs:1; + unsigned ReceivedValidFrameNotForUs : 1; /* A Boolean flag set to TRUE by the master machine if this node is the */ /* only known master node. */ - unsigned SoleMaster:1; + unsigned SoleMaster : 1; /* A Boolean flag set TRUE by the datalink if a packet has been received, but not processed. */ - unsigned ReceivePacketPending:1; + unsigned ReceivePacketPending : 1; } MSTP_Flag; /* Used to store the data length of a received frame. */ @@ -154,7 +154,7 @@ static volatile uint8_t Nmax_master = 127; /* The maximum time a node may wait after reception of a frame that expects */ /* a reply before sending the first octet of a reply or Reply Postponed */ /* frame: 250 milliseconds. */ -#define Treply_delay (250-50) +#define Treply_delay (250 - 50) /* The width of the time slot within which a node may generate a token: */ /* 10 milliseconds. */ @@ -166,7 +166,11 @@ static volatile uint8_t Nmax_master = 127; #define Tusage_delay 15 /* we need to be able to increment without rolling over */ -#define INCREMENT_AND_LIMIT_UINT8(x) {if (x < 0xFF) x++;} +#define INCREMENT_AND_LIMIT_UINT8(x) \ + { \ + if (x < 0xFF) \ + x++; \ + } /* data structure for MS/TP PDU Queue */ struct mstp_pdu_packet { @@ -182,7 +186,7 @@ bool dlmstp_init(char *ifname) { ifname = ifname; - Ringbuf_Init(&PDU_Queue, (uint8_t *) & PDU_Buffer, + Ringbuf_Init(&PDU_Queue, (uint8_t *)&PDU_Buffer, sizeof(struct mstp_pdu_packet), MSTP_PDU_PACKET_COUNT); return true; @@ -193,8 +197,7 @@ 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; @@ -217,10 +220,10 @@ void dlmstp_fill_bacnet_address(BACNET_ADDRESS * src, } } -static bool dlmstp_compare_data_expecting_reply(uint8_t * request_pdu, +static bool dlmstp_compare_data_expecting_reply(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) { @@ -241,9 +244,8 @@ static bool dlmstp_compare_data_expecting_reply(uint8_t * request_pdu, /* decode the request data */ request.address.mac[0] = src_address; request.address.mac_len = 1; - offset = - npdu_decode(&request_pdu[0], NULL, &request.address, - &request.npdu_data); + offset = npdu_decode( + &request_pdu[0], NULL, &request.address, &request.npdu_data); if (request.npdu_data.network_layer_message) { return false; } @@ -260,8 +262,7 @@ static bool dlmstp_compare_data_expecting_reply(uint8_t * request_pdu, /* decode the reply data */ reply.address.mac[0] = dest_address; reply.address.mac_len = 1; - offset = - npdu_decode(&reply_pdu[0], &reply.address, NULL, &reply.npdu_data); + offset = npdu_decode(&reply_pdu[0], &reply.address, NULL, &reply.npdu_data); if (reply.npdu_data.network_layer_message) { return false; } @@ -302,7 +303,8 @@ static bool dlmstp_compare_data_expecting_reply(uint8_t * request_pdu, return false; } } - if (request.npdu_data.protocol_version != reply.npdu_data.protocol_version) { + if (request.npdu_data.protocol_version != + reply.npdu_data.protocol_version) { return false; } #if 0 @@ -332,21 +334,22 @@ static bool dlmstp_compare_data_expecting_reply(uint8_t * request_pdu, /* Data CRC: (present only if Length is non-zero) two octets, */ /* 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 */ +static void MSTP_Send_Frame( + uint8_t frame_type, /* type of frame to send - see defines */ - uint8_t destination, /* destination address */ + uint8_t destination, /* destination address */ - uint8_t source, /* source address */ + uint8_t source, /* source address */ - uint8_t * data, /* any data to be sent - may be null */ + uint8_t *data, /* any data to be sent - may be null */ uint16_t data_len) -{ /* 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 header crc */ - uint8_t buffer_crc[2]; /* stores the data crc */ - uint16_t i = 0; /* used to calculate CRC for data */ +{ /* 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 header crc */ + uint8_t buffer_crc[2]; /* stores the data crc */ + uint16_t i = 0; /* used to calculate CRC for data */ /* create the MS/TP header */ buffer[0] = 0x55; @@ -640,7 +643,7 @@ static void MSTP_Slave_Node_FSM(void) /* source address */ uint8_t source; /* any data to be sent - may be null */ - uint8_t * data; + uint8_t *data; /* amount of data to be sent - may be 0 */ uint16_t data_len; /* packet from the PDU Queue */ @@ -657,7 +660,7 @@ static void MSTP_Slave_Node_FSM(void) /* 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. */ - pkt = (struct mstp_pdu_packet *) Ringbuf_Peek(&PDU_Queue); + pkt = (struct mstp_pdu_packet *)Ringbuf_Peek(&PDU_Queue); if (pkt != NULL) { uint8_t frame_type; if (pkt->data_expecting_reply) { @@ -667,13 +670,13 @@ static void MSTP_Slave_Node_FSM(void) FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; } MSTP_Send_Frame(frame_type, pkt->destination_mac, - This_Station, (uint8_t *) & pkt->buffer[0], + This_Station, (uint8_t *)&pkt->buffer[0], pkt->length); Master_State = MSTP_MASTER_STATE_IDLE; /* clear our flag we were holding for comparison */ MSTP_Flag.ReceivedValidFrame = false; /* clear the queue */ - (void) Ringbuf_Pop(&PDU_Queue, NULL); + (void)Ringbuf_Pop(&PDU_Queue, NULL); } else if (rs485_silence_elapsed(Treply_delay)) { /* If no reply will be available from the higher layers within Treply_delay after the reception of the final @@ -693,8 +696,8 @@ static void MSTP_Slave_Node_FSM(void) source = This_Station; data = &InputBuffer[0]; data_len = DataLength; - MSTP_Send_Frame(FRAME_TYPE_TEST_RESPONSE, - destination, source, data, data_len); + MSTP_Send_Frame(FRAME_TYPE_TEST_RESPONSE, destination, source, + data, data_len); break; case FRAME_TYPE_TOKEN: case FRAME_TYPE_POLL_FOR_MASTER: @@ -738,7 +741,7 @@ static bool MSTP_Master_Node_FSM(void) /* source address */ uint8_t source; /* any data to be sent - may be null */ - uint8_t * data; + uint8_t *data; /* amount of data to be sent - may be 0 */ uint16_t data_len; /* timeout values */ @@ -828,8 +831,8 @@ static bool MSTP_Master_Node_FSM(void) source = This_Station; data = &InputBuffer[0]; data_len = DataLength; - MSTP_Send_Frame(FRAME_TYPE_TEST_RESPONSE, - destination, source, data, data_len); + MSTP_Send_Frame(FRAME_TYPE_TEST_RESPONSE, destination, + source, data, data_len); break; case FRAME_TYPE_TEST_RESPONSE: default: @@ -854,14 +857,14 @@ static bool MSTP_Master_Node_FSM(void) transition_now = true; } else { uint8_t frame_type; - pkt = (struct mstp_pdu_packet *) Ringbuf_Peek(&PDU_Queue); + pkt = (struct mstp_pdu_packet *)Ringbuf_Peek(&PDU_Queue); if (pkt->data_expecting_reply) { frame_type = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY; } else { frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; } MSTP_Send_Frame(frame_type, pkt->destination_mac, This_Station, - (uint8_t *) & pkt->buffer[0], pkt->length); + (uint8_t *)&pkt->buffer[0], pkt->length); FrameCount++; switch (frame_type) { case FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY: @@ -881,7 +884,7 @@ static bool MSTP_Master_Node_FSM(void) Master_State = MSTP_MASTER_STATE_DONE_WITH_TOKEN; break; } - (void) Ringbuf_Pop(&PDU_Queue, NULL); + (void)Ringbuf_Pop(&PDU_Queue, NULL); } break; case MSTP_MASTER_STATE_WAIT_FOR_REPLY: @@ -994,8 +997,8 @@ static bool MSTP_Master_Node_FSM(void) 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; @@ -1009,7 +1012,7 @@ static bool MSTP_Master_Node_FSM(void) /* no known successor node */ Next_Station = This_Station; RetryCount = 0; - TokenCount = 1; /* changed in Errata SSPC-135-2004 */ + 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; @@ -1017,10 +1020,10 @@ static bool MSTP_Master_Node_FSM(void) /* ResetMaintenancePFM */ Poll_Station = This_Station; /* 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; - TokenCount = 1; /* changed in Errata SSPC-135-2004 */ + TokenCount = 1; /* changed in Errata SSPC-135-2004 */ EventCount = 0; Master_State = MSTP_MASTER_STATE_PASS_TOKEN; } @@ -1041,8 +1044,8 @@ static bool MSTP_Master_Node_FSM(void) /* RetrySendToken */ RetryCount++; /* 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); EventCount = 0; /* re-enter the current state to listen for NS */ /* to begin using the token. */ @@ -1120,15 +1123,15 @@ static bool MSTP_Master_Node_FSM(void) case MSTP_MASTER_STATE_POLL_FOR_MASTER: if (MSTP_Flag.ReceivedValidFrame == true) { destination = DestinationAddress; - if ((destination == This_Station) - && (FrameType == FRAME_TYPE_REPLY_TO_POLL_FOR_MASTER)) { + if ((destination == 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); + MSTP_Send_Frame( + FRAME_TYPE_TOKEN, Next_Station, This_Station, NULL, 0); Poll_Station = This_Station; TokenCount = 0; RetryCount = 0; @@ -1197,12 +1200,11 @@ static bool MSTP_Master_Node_FSM(void) data_len = DataLength; timeout = rs485_silence_elapsed(Treply_delay); if (!timeout) { - pkt = (struct mstp_pdu_packet *) Ringbuf_Peek(&PDU_Queue); + pkt = (struct mstp_pdu_packet *)Ringbuf_Peek(&PDU_Queue); if (pkt != NULL) { - matched = - dlmstp_compare_data_expecting_reply(data, - data_len, destination, &pkt->buffer[0], - pkt->length, pkt->destination_mac); + matched = dlmstp_compare_data_expecting_reply(data, + data_len, destination, &pkt->buffer[0], pkt->length, + pkt->destination_mac); } else { matched = false; } @@ -1219,17 +1221,15 @@ static bool MSTP_Master_Node_FSM(void) if (pkt->data_expecting_reply) { frame_type = FRAME_TYPE_BACNET_DATA_EXPECTING_REPLY; } else { - frame_type = - FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; + frame_type = FRAME_TYPE_BACNET_DATA_NOT_EXPECTING_REPLY; } - MSTP_Send_Frame(frame_type, pkt->destination_mac, - This_Station, (uint8_t *) & pkt->buffer[0], - pkt->length); + MSTP_Send_Frame(frame_type, pkt->destination_mac, This_Station, + (uint8_t *)&pkt->buffer[0], pkt->length); Master_State = MSTP_MASTER_STATE_IDLE; /* clear our flag we were holding for comparison */ MSTP_Flag.ReceivedValidFrame = false; /* clear the queue */ - (void) Ringbuf_Pop(&PDU_Queue, NULL); + (void)Ringbuf_Pop(&PDU_Queue, NULL); } else if ((pkt != NULL) || timeout) { /* DeferredReply */ /* If no reply will be available from the higher layers */ @@ -1240,8 +1240,8 @@ static bool MSTP_Master_Node_FSM(void) /* 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, destination, - source, NULL, 0); + MSTP_Send_Frame( + FRAME_TYPE_REPLY_POSTPONED, destination, source, NULL, 0); Master_State = MSTP_MASTER_STATE_IDLE; /* clear our flag we were holding for comparison */ MSTP_Flag.ReceivedValidFrame = false; @@ -1268,19 +1268,19 @@ bool dlmstp_send_pdu_queue_full(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 */ + 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; struct mstp_pdu_packet *pkt; uint16_t i = 0; - pkt = (struct mstp_pdu_packet *) Ringbuf_Data_Peek(&PDU_Queue); + pkt = (struct mstp_pdu_packet *)Ringbuf_Data_Peek(&PDU_Queue); if (pkt) { pkt->data_expecting_reply = npdu_data->data_expecting_reply; for (i = 0; i < pdu_len; i++) { @@ -1311,12 +1311,10 @@ int dlmstp_send_pdu(BACNET_ADDRESS * dest, /* destination address */ * * @return Return the length of the packet */ -uint16_t dlmstp_receive(BACNET_ADDRESS * src, - uint8_t * pdu, - uint16_t max_pdu, - unsigned timeout) +uint16_t dlmstp_receive( + BACNET_ADDRESS *src, uint8_t *pdu, uint16_t max_pdu, unsigned timeout) { - uint16_t pdu_len = 0; /* return value */ + uint16_t pdu_len = 0; /* return value */ bool transmitting = false; (void)timeout; @@ -1401,8 +1399,7 @@ uint8_t dlmstp_mac_address(void) /* node, its value shall be 1. */ void dlmstp_set_max_info_frames(uint8_t max_info_frames) { - if ((max_info_frames >= 1) && - (max_info_frames <= MSTP_PDU_PACKET_COUNT)) { + if ((max_info_frames >= 1) && (max_info_frames <= MSTP_PDU_PACKET_COUNT)) { Nmax_info_frames = max_info_frames; } @@ -1433,13 +1430,13 @@ 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; - my_address->net = 0; /* local only, no routing */ + my_address->net = 0; /* local only, no routing */ my_address->len = 0; for (i = 0; i < MAX_MAC_LEN; i++) { my_address->adr[i] = 0; @@ -1448,15 +1445,15 @@ 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/ports/xplained/led.c b/ports/xplained/led.c index 39d1bb5a..99833ca1 100644 --- a/ports/xplained/led.c +++ b/ports/xplained/led.c @@ -1,26 +1,26 @@ /************************************************************************** -* -* Copyright (C) 2009 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -*********************************************************************/ + * + * Copyright (C) 2009 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + *********************************************************************/ #include #include "board.h" #include "ioport.h" @@ -28,18 +28,18 @@ #include "led.h" #ifdef CONF_BOARD_ENABLE_RS485_XPLAINED -#define RS485_XPLAINED_LD1 IOPORT_CREATE_PIN(PORTC, 6) -#define RS485_XPLAINED_LD2 IOPORT_CREATE_PIN(PORTC, 7) -#define RS485_XPLAINED_LD3 IOPORT_CREATE_PIN(PORTC, 4) -#define RS485_XPLAINED_LD4 IOPORT_CREATE_PIN(PORTC, 5) +#define RS485_XPLAINED_LD1 IOPORT_CREATE_PIN(PORTC, 6) +#define RS485_XPLAINED_LD2 IOPORT_CREATE_PIN(PORTC, 7) +#define RS485_XPLAINED_LD3 IOPORT_CREATE_PIN(PORTC, 4) +#define RS485_XPLAINED_LD4 IOPORT_CREATE_PIN(PORTC, 5) static struct itimer Off_Delay_Timer[LEDS_MAX]; /************************************************************************* -* Description: Turn on an LED -* Returns: none -* Notes: none -*************************************************************************/ + * Description: Turn on an LED + * Returns: none + * Notes: none + *************************************************************************/ void led_on(uint8_t index) { switch (index) { @@ -64,10 +64,10 @@ void led_on(uint8_t index) } /************************************************************************* -* Description: Turn off an LED -* Returns: none -* Notes: none -*************************************************************************/ + * Description: Turn off an LED + * Returns: none + * Notes: none + *************************************************************************/ void led_off(uint8_t index) { switch (index) { @@ -92,10 +92,10 @@ void led_off(uint8_t index) } /************************************************************************* -* Description: Get the state of the LED -* Returns: true if on, false if off. -* Notes: none -*************************************************************************/ + * Description: Get the state of the LED + * Returns: true if on, false if off. + * Notes: none + *************************************************************************/ bool led_state(uint8_t index) { switch (index) { @@ -115,10 +115,10 @@ bool led_state(uint8_t index) } /************************************************************************* -* Description: Toggle the state of the setup LED -* Returns: none -* Notes: none -*************************************************************************/ + * Description: Toggle the state of the setup LED + * Returns: none + * Notes: none + *************************************************************************/ void led_toggle(uint8_t index) { if (led_state(index)) { @@ -129,12 +129,11 @@ void led_toggle(uint8_t index) } /************************************************************************* -* Description: Delay before going off to give minimum brightness. -* Returns: none -* Notes: none -*************************************************************************/ -void led_off_delay(uint8_t index, - uint32_t delay_ms) + * Description: Delay before going off to give minimum brightness. + * Returns: none + * Notes: none + *************************************************************************/ +void led_off_delay(uint8_t index, uint32_t delay_ms) { if (index < LEDS_MAX) { timer_interval_start(&Off_Delay_Timer[index], delay_ms); @@ -142,12 +141,11 @@ void led_off_delay(uint8_t index, } /************************************************************************* -* Description: Turn on, and delay before going off. -* Returns: none -* Notes: none -*************************************************************************/ -void led_on_interval(uint8_t index, - uint16_t interval_ms) + * Description: Turn on, and delay before going off. + * Returns: none + * Notes: none + *************************************************************************/ +void led_on_interval(uint8_t index, uint16_t interval_ms) { if (index < LEDS_MAX) { led_on(index); @@ -156,13 +154,13 @@ void led_on_interval(uint8_t index, } /************************************************************************* -* Description: Task for blinking LED -* Returns: none -* Notes: none -*************************************************************************/ + * Description: Task for blinking LED + * Returns: none + * Notes: none + *************************************************************************/ void led_task(void) { - uint8_t i; /* loop counter */ + uint8_t i; /* loop counter */ for (i = 0; i < LEDS_MAX; i++) { if (timer_interval_expired(&Off_Delay_Timer[i])) { @@ -173,27 +171,27 @@ void led_task(void) } /************************************************************************* -* Description: Initialize the LED hardware -* Returns: none -* Notes: none -*************************************************************************/ + * Description: Initialize the LED hardware + * Returns: none + * Notes: none + *************************************************************************/ void led_init(void) { - uint8_t i; /* loop counter */ + uint8_t i; /* loop counter */ /* configure the LEDs for Rx and Tx indication */ - ioport_configure_pin(RS485_XPLAINED_LD1, - IOPORT_DIR_OUTPUT | IOPORT_INIT_LOW); - ioport_configure_pin(RS485_XPLAINED_LD2, - IOPORT_DIR_OUTPUT | IOPORT_INIT_LOW); - ioport_configure_pin(RS485_XPLAINED_LD3, - IOPORT_DIR_OUTPUT | IOPORT_INIT_LOW); - ioport_configure_pin(RS485_XPLAINED_LD4, - IOPORT_DIR_OUTPUT | IOPORT_INIT_LOW); + ioport_configure_pin( + RS485_XPLAINED_LD1, IOPORT_DIR_OUTPUT | IOPORT_INIT_LOW); + ioport_configure_pin( + RS485_XPLAINED_LD2, IOPORT_DIR_OUTPUT | IOPORT_INIT_LOW); + ioport_configure_pin( + RS485_XPLAINED_LD3, IOPORT_DIR_OUTPUT | IOPORT_INIT_LOW); + ioport_configure_pin( + RS485_XPLAINED_LD4, IOPORT_DIR_OUTPUT | IOPORT_INIT_LOW); /* initialize the timers, while giving LEDs a brief test */ for (i = 0; i < LEDS_MAX; i++) { led_on(i); - led_off_delay(i,500); - } + led_off_delay(i, 500); + } } #endif diff --git a/ports/xplained/main.c b/ports/xplained/main.c index 2a18f4b8..f5f76d93 100644 --- a/ports/xplained/main.c +++ b/ports/xplained/main.c @@ -28,7 +28,7 @@ int main(void) mstimer_init(); rs485_init(); led_init(); - adc_init(); + adc_init(); #ifdef CONF_BOARD_ENABLE_RS485_XPLAINED // Enable display backlight gpio_set_pin_high(NHD_C12832A1Z_BACKLIGHT); diff --git a/ports/xplained/mstimer-init.c b/ports/xplained/mstimer-init.c index e164c9c4..06bec0b6 100644 --- a/ports/xplained/mstimer-init.c +++ b/ports/xplained/mstimer-init.c @@ -88,8 +88,7 @@ unsigned long mstimer_now(void) * * @return true if successfully added and enabled */ -void mstimer_callback( - struct mstimer_callback_data_t *new_cb, +void mstimer_callback(struct mstimer_callback_data_t *new_cb, mstimer_callback_function callback, unsigned long milliseconds) { diff --git a/ports/xplained/nvmdata.c b/ports/xplained/nvmdata.c index 339c577d..04e321f4 100644 --- a/ports/xplained/nvmdata.c +++ b/ports/xplained/nvmdata.c @@ -1,10 +1,10 @@ /** -* @file -* @author Steve Karg -* @date 2013 -* @brief Store and retrieve non-volatile data -* -*/ + * @file + * @author Steve Karg + * @date 2013 + * @brief Store and retrieve non-volatile data + * + */ #include #include #include "nvmdata.h" @@ -12,8 +12,8 @@ #include "bacnet/basic/object/device.h" /** -* Initializes the non-volatile memory module -*/ + * Initializes the non-volatile memory module + */ void nvm_data_init(void) { uint32_t device_id = 127; @@ -32,7 +32,7 @@ void nvm_data_init(void) } dlmstp_set_max_master(max_master); /* Get the device ID from the EEPROM */ - nvm_read(NVM_DEVICE_0, (uint8_t *) & device_id, sizeof(device_id)); + nvm_read(NVM_DEVICE_0, (uint8_t *)&device_id, sizeof(device_id)); if (device_id < BACNET_MAX_INSTANCE) { Device_Set_Object_Instance_Number(device_id); } else { diff --git a/ports/xplained/rs485.c b/ports/xplained/rs485.c index d9120643..05e0caf2 100644 --- a/ports/xplained/rs485.c +++ b/ports/xplained/rs485.c @@ -41,18 +41,18 @@ #include "rs485.h" #ifdef CONF_BOARD_ENABLE_RS485_XPLAINED -#define RS485_RE IOPORT_CREATE_PIN(PORTC, 1) -#define RS485_DE IOPORT_CREATE_PIN(PORTC, 0) -#define RS485_TXD IOPORT_CREATE_PIN(PORTC, 3) -#define RS485_RXD IOPORT_CREATE_PIN(PORTC, 2) -#define RS485_USART USARTC0 +#define RS485_RE IOPORT_CREATE_PIN(PORTC, 1) +#define RS485_DE IOPORT_CREATE_PIN(PORTC, 0) +#define RS485_TXD IOPORT_CREATE_PIN(PORTC, 3) +#define RS485_RXD IOPORT_CREATE_PIN(PORTC, 2) +#define RS485_USART USARTC0 #define RS485_TXC_vect USARTC0_TXC_vect #define RS485_RXC_vect USARTC0_RXC_vect #else -#define RS485_RE IOPORT_CREATE_PIN(PORTE, 0) -#define RS485_DE IOPORT_CREATE_PIN(PORTE, 0) -#define RS485_TXD IOPORT_CREATE_PIN(PORTE, 3) -#define RS485_RXD IOPORT_CREATE_PIN(PORTE, 2) +#define RS485_RE IOPORT_CREATE_PIN(PORTE, 0) +#define RS485_DE IOPORT_CREATE_PIN(PORTE, 0) +#define RS485_TXD IOPORT_CREATE_PIN(PORTE, 3) +#define RS485_RXD IOPORT_CREATE_PIN(PORTE, 2) #define RS485_USART USARTE0 #define RS485_TXC_vect USARTE0_TXC_vect #define RS485_RXC_vect USARTE0_RXC_vect @@ -164,9 +164,9 @@ bool rs485_turnaround_elapsed(void) * * @return true if a byte is available */ -bool rs485_byte_available(uint8_t * data_register) +bool rs485_byte_available(uint8_t *data_register) { - bool data_available = false; /* return value */ + bool data_available = false; /* return value */ if (FIFO_Empty(&Receive_Queue)) { led_off_delay(LED_RS485_RX, 2); @@ -202,14 +202,13 @@ bool rs485_frame_sent(void) } /** -* Transmit one or more bytes on RS-485. Can be called while transmitting to add -* additional bytes to transmit queue. -* -* @param buffer - array of one or more bytes to transmit -* @param nbytes - number of bytes to transmit -*/ -bool rs485_bytes_send(uint8_t * buffer, - uint16_t nbytes) + * Transmit one or more bytes on RS-485. Can be called while transmitting to + * add additional bytes to transmit queue. + * + * @param buffer - array of one or more bytes to transmit + * @param nbytes - number of bytes to transmit + */ +bool rs485_bytes_send(uint8_t *buffer, uint16_t nbytes) { bool status = false; bool start_required = false; @@ -234,8 +233,8 @@ bool rs485_bytes_send(uint8_t * buffer, } /** -* RS485 RX interrupt -*/ + * RS485 RX interrupt + */ ISR(RS485_RXC_vect) { unsigned char ch; @@ -246,8 +245,8 @@ ISR(RS485_RXC_vect) } /** -* RS485 TX interrupt -*/ + * RS485 TX interrupt + */ ISR(RS485_TXC_vect) { uint8_t ch; @@ -268,8 +267,7 @@ ISR(RS485_TXC_vect) * * @return baud - RS-485 baud rate in bits per second (bps) */ -uint32_t rs485_baud_rate( - void) +uint32_t rs485_baud_rate(void) { return Baud_Rate; } @@ -281,8 +279,7 @@ uint32_t rs485_baud_rate( * * @return true if set and valid */ -bool rs485_baud_rate_set( - uint32_t baud) +bool rs485_baud_rate_set(uint32_t baud) { bool valid = true; unsigned long frequency; @@ -295,7 +292,7 @@ bool rs485_baud_rate_set( case 76800: case 115200: frequency = sysclk_get_peripheral_bus_hz(&RS485_USART); - valid = usart_set_baudrate (&RS485_USART, baud, frequency); + valid = usart_set_baudrate(&RS485_USART, baud, frequency); if (valid) { Baud_Rate = baud; } @@ -308,7 +305,6 @@ bool rs485_baud_rate_set( return valid; } - /** * Initialize the RS-485 UART interface, receive interrupts enabled */ @@ -318,22 +314,18 @@ void rs485_init(void) /* initialize the Rx and Tx byte queues */ FIFO_Init(&Receive_Queue, &Receive_Queue_Data[0], - (unsigned) sizeof(Receive_Queue_Data)); + (unsigned)sizeof(Receive_Queue_Data)); FIFO_Init(&Transmit_Queue, &Transmit_Queue_Data[0], - (unsigned) sizeof(Transmit_Queue_Data)); + (unsigned)sizeof(Transmit_Queue_Data)); /* initialize the silence timer */ rs485_silence_reset(); /* configure the TX pin */ - ioport_configure_pin(RS485_TXD, - IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH); + ioport_configure_pin(RS485_TXD, IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH); /* configure the RX pin */ - ioport_configure_pin(RS485_RXD, - IOPORT_DIR_INPUT); + ioport_configure_pin(RS485_RXD, IOPORT_DIR_INPUT); /* configure the RTS pins */ - ioport_configure_pin(RS485_RE, - IOPORT_DIR_OUTPUT | IOPORT_INIT_LOW); - ioport_configure_pin(RS485_DE, - IOPORT_DIR_OUTPUT | IOPORT_INIT_LOW); + ioport_configure_pin(RS485_RE, IOPORT_DIR_OUTPUT | IOPORT_INIT_LOW); + ioport_configure_pin(RS485_DE, IOPORT_DIR_OUTPUT | IOPORT_INIT_LOW); option.baudrate = Baud_Rate; option.charlength = USART_CHSIZE_8BIT_gc; option.paritytype = USART_PMODE_DISABLED_gc; diff --git a/ports/xplained/stack.c b/ports/xplained/stack.c index 4513ca07..eaf6592c 100644 --- a/ports/xplained/stack.c +++ b/ports/xplained/stack.c @@ -1,47 +1,47 @@ /************************************************************************** -* -* Copyright (C) 2009 Steve Karg -* -* Permission is hereby granted, free of charge, to any person obtaining -* a copy of this software and associated documentation files (the -* "Software"), to deal in the Software without restriction, including -* without limitation the rights to use, copy, modify, merge, publish, -* distribute, sublicense, and/or sell copies of the Software, and to -* permit persons to whom the Software is furnished to do so, subject to -* the following conditions: -* -* The above copyright notice and this permission notice shall be included -* in all copies or substantial portions of the Software. -* -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, -* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE -* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* -*********************************************************************/ + * + * Copyright (C) 2009 Steve Karg + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + *********************************************************************/ #include "board.h" /* me */ #include "stack.h" #if defined(__GNUC__) /* stack checking technique by Michael McTernan */ -/* With AVR gcc, two symbols are defined by the linker - that can make this easy. These are _end and __stack - which define the first free byte of SRAM after - program variables, and the starting address of +/* With AVR gcc, two symbols are defined by the linker + that can make this easy. These are _end and __stack + which define the first free byte of SRAM after + program variables, and the starting address of the stack, respectively. - The stack starts at __stack, which is conventionally - the highest byte of SRAM, and grows towards zero; - _end will be somewhere between zero and __stack. - If the stack ever falls below _end, it has almost + The stack starts at __stack, which is conventionally + the highest byte of SRAM, and grows towards zero; + _end will be somewhere between zero and __stack. + If the stack ever falls below _end, it has almost certainly corrupted program data. - The following C declarations gain access to these - linker symbols: + The following C declarations gain access to these + linker symbols: */ extern uint8_t _end; extern uint8_t __stack; @@ -49,22 +49,20 @@ extern uint8_t __stack; /* canary value */ #define STACK_CANARY (0xC5) -/* This is declared in such a way that AVR-libc will - execute the assembly before the program has started - running or configured the stack. It also runs at a - point before some of the normal runtime setup, - hence assembly should be used as C may not be - fully reliable (this is discussed in the AVR libc manual). +/* This is declared in such a way that AVR-libc will + execute the assembly before the program has started + running or configured the stack. It also runs at a + point before some of the normal runtime setup, + hence assembly should be used as C may not be + fully reliable (this is discussed in the AVR libc manual). */ -void stack_init( - void) __attribute__ ((naked)) __attribute__ ((section(".init1"))); +void stack_init(void) __attribute__((naked)) __attribute__((section(".init1"))); -/* The function itself simply fills the stack with the canary value, - the idea being that stack usage will overwrite this with some - other value, hence making stack usage detectable. +/* The function itself simply fills the stack with the canary value, + the idea being that stack usage will overwrite this with some + other value, hence making stack usage detectable. */ -void stack_init( - void) +void stack_init(void) { #if 0 uint8_t *p = &_end; @@ -74,36 +72,40 @@ void stack_init( p++; } #else - __asm volatile ( - " ldi r30,lo8(_end)\n" " ldi r31,hi8(_end)\n" " ldi r24,lo8(0xc5)\n" /* STACK_CANARY = 0xc5 */ - " ldi r25,hi8(__stack)\n" " rjmp .cmp\n" ".loop:\n" - " st Z+,r24\n" ".cmp:\n" " cpi r30,lo8(__stack)\n" - " cpc r31,r25\n" " brlo .loop\n" " breq .loop"::); + __asm volatile(" ldi r30,lo8(_end)\n" + " ldi r31,hi8(_end)\n" + " ldi r24,lo8(0xc5)\n" /* STACK_CANARY = 0xc5 */ + " ldi r25,hi8(__stack)\n" + " rjmp .cmp\n" + ".loop:\n" + " st Z+,r24\n" + ".cmp:\n" + " cpi r30,lo8(__stack)\n" + " cpc r31,r25\n" + " brlo .loop\n" + " breq .loop" ::); #endif } -unsigned stack_size( - void) +unsigned stack_size(void) { return (&__stack) - (&_end); } -uint8_t stack_byte( - unsigned offset) +uint8_t stack_byte(unsigned offset) { return *(&_end + offset); } -/* The following function can be used to count - how many bytes of stack have not been overwritten. - This function can be called at any time - to check how much stack space has never been over written. - If it returns 0, you are probably in trouble as - all the stack has been used, most likely destroying - some program variables. +/* The following function can be used to count + how many bytes of stack have not been overwritten. + This function can be called at any time + to check how much stack space has never been over written. + If it returns 0, you are probably in trouble as + all the stack has been used, most likely destroying + some program variables. */ -unsigned stack_unused( - void) +unsigned stack_unused(void) { uint8_t *p = &_end; unsigned count = 0; @@ -118,26 +120,21 @@ unsigned stack_unused( return count; } #else -void stack_init( - void) +void stack_init(void) { - } -unsigned stack_size( - void) +unsigned stack_size(void) { return 0; } -uint8_t stack_byte( - unsigned offset) +uint8_t stack_byte(unsigned offset) { return 0; } -unsigned stack_unused( - void) +unsigned stack_unused(void) { return 0; }