├── examples ├── ESP32IOBoard │ └── .gitignore ├── ESP32CanLoadTest │ └── .gitignore ├── ESP32SerialBridge │ ├── .gitignore │ └── config.h ├── ESP32WifiCanBridge │ ├── .gitignore │ └── config.h ├── Stm32CanUSB │ └── build_opt.h ├── Stm32CanSerial │ └── build_opt.h └── Stm32CanSerialNode │ └── build_opt.h ├── src ├── utils │ ├── HubDeviceSelect.cpp │ ├── stdio_logging.h │ ├── MockTrain.hxx │ ├── Queue.cpp │ ├── SimpleQueue.cpp │ ├── JSHubPort.cpp │ ├── ServiceLocator.cpp │ ├── Destructable.hxx │ ├── Revision.hxx │ ├── ConfigUpdateListener.cpp │ ├── StringPrintf.hxx │ ├── errno_exit.c │ ├── ReflashBootloader.hxx │ ├── GcTcpHub.cpp │ ├── Base64.hxx │ ├── StringPrintf.cpp │ ├── FileUtils.hxx │ ├── QMember.hxx │ ├── median.hxx │ ├── logging.cpp │ ├── ConfigUpdateService.hxx │ ├── GcTcpHub.hxx │ ├── Crc.hxx │ ├── GcStreamParser.cpp │ ├── Clock.hxx │ ├── MultiMap.hxx │ └── GcStreamParser.hxx ├── openlcb │ ├── TractionThrottle.cpp │ ├── ServoConsumerConfig.hxx │ ├── PIPClient.cpp │ ├── WriteHelper.cpp │ ├── Payload.hxx │ ├── BroadcastTime.cpp │ ├── DccAccyProducer.cpp │ ├── TcpDefs.cpp │ ├── DefaultNode.cpp │ ├── NodeInitializeFlow.cpp │ ├── FirmwareUpgradeDefs.hxx │ ├── TractionProxy.hxx │ ├── ConfigEntry.cpp │ ├── DatagramTcp.cpp │ ├── RoutingLogic.cpp │ ├── DatagramTcp.hxx │ ├── TcpDefs.hxx │ ├── TractionDefs.cpp │ ├── TractionTestTrain.hxx │ ├── DatagramCan.hxx │ ├── DefaultNode.hxx │ ├── EventHandler.cpp │ ├── EventHandlerMock.hxx │ ├── nmranet_constants.cpp │ ├── Node.hxx │ ├── TractionCvCdi.hxx │ ├── CanDefs.cpp │ ├── SimpleNodeInfoMockUserFile.hxx │ ├── SimpleNodeInfoMockUserFile.cpp │ ├── EndianHelper.hxx │ ├── NodeBrowser.cpp │ ├── DefaultCdi.cpp │ ├── StreamDefs.hxx │ └── BootloaderPort.hxx ├── freertos_drivers │ ├── stm32 │ │ └── stm32f_hal_conf.hxx │ └── arduino │ │ ├── Can.cpp │ │ ├── WifiDefs.hxx │ │ ├── WifiDefs.cpp │ │ ├── DeviceBuffer.cpp │ │ └── libatomic.c ├── freertos_includes.h ├── os │ ├── OSImpl.cpp │ ├── os_private.h │ ├── watchdog.h │ ├── stack_malloc.c │ └── watchdog.c ├── executor │ ├── Service.cpp │ ├── Service.hxx │ └── Notifiable.cpp ├── dcc │ ├── PacketFlowInterface.hxx │ ├── DccDebug.hxx │ ├── RailcomDebug.cpp │ ├── Defs.hxx │ ├── RailcomHub.hxx │ ├── UpdateLoop.cpp │ ├── railcom.h │ ├── FakeTrackIf.hxx │ └── Address.hxx ├── OpenMRNLite.cpp └── ifaddrs.h ├── keywords.txt ├── CONTRIBUTING.md ├── library.properties ├── LICENSE └── library.json /examples/ESP32IOBoard/.gitignore: -------------------------------------------------------------------------------- 1 | wifi_params.cpp 2 | -------------------------------------------------------------------------------- /examples/ESP32CanLoadTest/.gitignore: -------------------------------------------------------------------------------- 1 | wifi_params.cpp 2 | -------------------------------------------------------------------------------- /examples/ESP32SerialBridge/.gitignore: -------------------------------------------------------------------------------- 1 | wifi_params.cpp 2 | -------------------------------------------------------------------------------- /examples/ESP32WifiCanBridge/.gitignore: -------------------------------------------------------------------------------- 1 | wifi_params.cpp 2 | -------------------------------------------------------------------------------- /examples/Stm32CanUSB/build_opt.h: -------------------------------------------------------------------------------- 1 | -DHAL_CAN_MODULE_ENABLED 2 | -------------------------------------------------------------------------------- /examples/Stm32CanSerial/build_opt.h: -------------------------------------------------------------------------------- 1 | -DHAL_CAN_MODULE_ENABLED 2 | -------------------------------------------------------------------------------- /examples/Stm32CanSerialNode/build_opt.h: -------------------------------------------------------------------------------- 1 | -DHAL_CAN_MODULE_ENABLED 2 | -------------------------------------------------------------------------------- /src/utils/HubDeviceSelect.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include "utils/HubDeviceSelect.hxx" 3 | -------------------------------------------------------------------------------- /src/openlcb/TractionThrottle.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include "openlcb/TractionThrottle.hxx" 4 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | # Datatypes (KEYWORD1) 2 | 3 | SerialBridge KEYWORD1 4 | CanBridge KEYWORD1 5 | OpenMRN KEYWORD1 6 | CanHubFlow KEYWORD1 7 | Can KEYWORD1 8 | 9 | # Methods and Functions (KEYWORD2) 10 | 11 | # Constants (LITERAL1) 12 | 13 | -------------------------------------------------------------------------------- /src/freertos_drivers/stm32/stm32f_hal_conf.hxx: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | static inline void SetInterruptPriority(uint32_t irq, uint8_t priority) 5 | { 6 | NVIC_SetPriority((IRQn_Type)irq, priority >> (8U - __NVIC_PRIO_BITS)); 7 | } 8 | 9 | #ifndef configKERNEL_INTERRUPT_PRIORITY 10 | #define configKERNEL_INTERRUPT_PRIORITY 0xA0 11 | #endif 12 | -------------------------------------------------------------------------------- /src/freertos_includes.h: -------------------------------------------------------------------------------- 1 | #ifdef ESP32 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #define NSEC_TO_TICK(ns) \ 8 | (((((long long)(ns)) / 1000 * configTICK_RATE_HZ) + 999999) / 1000000) 9 | 10 | #else 11 | 12 | #include 13 | #include 14 | #include 15 | 16 | #define NSEC_TO_TICK(ns) ((ns) >> NSEC_TO_TICK_SHIFT) 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /src/utils/stdio_logging.h: -------------------------------------------------------------------------------- 1 | /// Include this file into main.cxx to log stuff to stdio on FreeRTOS. 2 | 3 | #include 4 | 5 | #ifdef __cplusplus 6 | extern "C" { 7 | #endif 8 | 9 | #if defined(__linux__) || defined(__MACH__) || defined(__EMSCRIPTEN__) 10 | #define LOGWEAK __attribute__((weak)) 11 | #else 12 | #define LOGWEAK 13 | #endif 14 | 15 | LOGWEAK void log_output(char* buf, int size) { 16 | if (size <= 0) return; 17 | fwrite(buf, size, 1, stderr); 18 | fwrite("\n", 1, 1, stderr); 19 | } 20 | 21 | #ifdef __cplusplus 22 | } 23 | #endif 24 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to OpenMRNLite 2 | 3 | Thank you for considering to volunteer your time in making this software better. 4 | 5 | **All changes must go to the [bakerstu/openmrn](https://github.com/bakerstu/openmrn) repository** 6 | 7 | This is important, because the 8 | [openmrn/OpenMRNLite](https://github.com/openmrn/OpenMRNLite) gets periodically 9 | overwritten by the changes from upstream, and all changes to that repo will be 10 | lost at the next release. 11 | 12 | ## Release process 13 | 14 | is documented at https://github.com/bakerstu/openmrn/blob/master/arduino/RELEASE.md 15 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=OpenMRNLite 2 | version=1.0.3 3 | author=Stuart Baker, Mike Dunston, Balazs Racz 4 | maintainer=Mike Dunston , Balazs Racz 5 | includes=OpenMRNLite.h 6 | sentence=Network protocol stack for model railroading: OpenLCB and LCC implementation. 7 | paragraph=This library implements network protocols for model railroading. In the center is the OpenLCB protocol suite (Open Layout Control Bus), which has been adopted by the NMRA and referenced as LCC (Layout Command Control): a high-performance and highly extensible communications protocol suite for model railroad control. OpenMRN is one of the most extensible implementation of this protocol suite. The Lite version has been adapted to work with the programming model and drivers of the Arduino ecosystem. Currently supports esp32 and stm32 cores. 8 | category=Communication 9 | url=http://github.com/openmrn/OpenMRNLite 10 | architectures=esp32,stm32,sam,samd 11 | dot_a_linkage=true 12 | -------------------------------------------------------------------------------- /src/utils/MockTrain.hxx: -------------------------------------------------------------------------------- 1 | #ifndef _UTILS_MOCKTRAIN_HXX_ 2 | #define _UTILS_MOCKTRAIN_HXX_ 3 | 4 | #include "gmock/gmock.h" 5 | #include "openlcb/TractionDefs.hxx" 6 | #include "openlcb/TractionTrain.hxx" 7 | #include "dcc/Defs.hxx" 8 | 9 | namespace openlcb { 10 | 11 | /// Test helper class for TrainImpl using GoogleMock. Allows creating a train 12 | /// node without an implementation. All calls have to have an explicit 13 | /// expectation using the GoogleMock framework. 14 | class MockTrain : public TrainImpl 15 | { 16 | public: 17 | MOCK_METHOD1(set_speed, void(SpeedType speed)); 18 | MOCK_METHOD0(get_speed, SpeedType()); 19 | MOCK_METHOD0(get_commanded_speed, SpeedType()); 20 | MOCK_METHOD0(get_actual_speed, SpeedType()); 21 | MOCK_METHOD0(set_emergencystop, void()); 22 | MOCK_METHOD0(get_emergencystop, bool()); 23 | MOCK_METHOD2(set_fn, void(uint32_t address, uint16_t value)); 24 | MOCK_METHOD1(get_fn, uint16_t(uint32_t address)); 25 | MOCK_METHOD0(legacy_address, uint32_t()); 26 | MOCK_METHOD0(legacy_address_type, dcc::TrainAddressType()); 27 | }; 28 | 29 | } // namespace openlcb 30 | 31 | #endif // _UTILS_MOCKTRAIN_HXX_ 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 2-Clause License 2 | 3 | Copyright (c) 2019, OpenMRN 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 20 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 23 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | -------------------------------------------------------------------------------- /library.json: -------------------------------------------------------------------------------- 1 | { 2 | "name":"OpenMRNLite", 3 | "description":"OpenMRN (Open Model Railroad Network) is a set of software libraries that are designed to make it easier to implement support for the NMRA's LCC (Layout Command Control) bus.", 4 | "keywords":"openlcb,lcc", 5 | "authors": [ 6 | { 7 | "name":"Stuart Baker" 8 | }, 9 | { 10 | "name":"Mike Dunston", 11 | "email": "m_dunston@comcast.net", 12 | "maintainer": true 13 | }, 14 | { 15 | "name":"Balazs Racz", 16 | "email": "balazs.racz@gmail.com", 17 | "maintainer": true 18 | } 19 | ], 20 | "repository": { 21 | "type": "git", 22 | "url": "https://github.com/openmrn/OpenMRNLite" 23 | }, 24 | "version": "1.0.3", 25 | "license": "BSD-2-Clause", 26 | "frameworks": "arduino", 27 | "platforms": "espressif32", 28 | "build": { 29 | "srcFilter" : [ 30 | "-<.git/>", 31 | "-", 32 | "+<*>", 33 | "+", 34 | "+", 35 | "+", 36 | "+", 37 | "+", 38 | "+", 39 | "+", 40 | "+" 41 | ], 42 | "libArchive" : true, 43 | "libLDFMode" : "chain+" 44 | } 45 | } 46 | 47 | -------------------------------------------------------------------------------- /src/openlcb/ServoConsumerConfig.hxx: -------------------------------------------------------------------------------- 1 | #ifndef _OPENLCB_SERVOCONSUMERCONFIG_HXX_ 2 | #define _OPENLCB_SERVOCONSUMERCONFIG_HXX_ 3 | 4 | #include "openlcb/ConfigRepresentation.hxx" 5 | 6 | namespace openlcb 7 | { 8 | 9 | CDI_GROUP(ServoConsumerConfig); 10 | CDI_GROUP_ENTRY(description, StringConfigEntry<16>, Name("Description"), 11 | Description("User name of this output.")); 12 | CDI_GROUP_ENTRY(event_rotate_min, EventConfigEntry, // 13 | Name("Minimum Rotation Event ID"), 14 | Description("Receiving this event ID will rotate the servo to its mimimum " 15 | "configured point.")); 16 | CDI_GROUP_ENTRY(event_rotate_max, EventConfigEntry, // 17 | Name("Maximum Rotation Event ID"), 18 | Description("Receiving this event ID will rotate the servo to its maximum " 19 | "configured point.")); 20 | #define SERVO_DESCRIPTION_SUFFIX \ 21 | "stop point of the servo, as a percentage: generally 0-100. " \ 22 | "May be under/over-driven by setting a percentage value " \ 23 | "of -99 to 200, respectively." 24 | CDI_GROUP_ENTRY(servo_min_percent, Int16ConfigEntry, Default(0), Min(-99), 25 | Max(200), Name("Servo Minimum Stop Point Percentage"), 26 | Description("Low-end " SERVO_DESCRIPTION_SUFFIX)); 27 | CDI_GROUP_ENTRY(servo_max_percent, Int16ConfigEntry, Default(100), Min(-99), 28 | Max(200), Name("Servo Maximum Stop Point Percentage"), 29 | Description("High-end " SERVO_DESCRIPTION_SUFFIX)); 30 | CDI_GROUP_END(); // ServoConsumerConfig 31 | 32 | } // namespace openlcb 33 | 34 | #endif // _OPENLCB_SERVOCONSUMERCONFIG_HXX_ -------------------------------------------------------------------------------- /src/utils/Queue.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2013, Stuart W Baker 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file Queue.cxx 28 | * This file provides implementation of non-templated queue methods. 29 | * 30 | * @author Stuart W. Baker 31 | * @date 3 August 2013 32 | */ 33 | 34 | #include "utils/Queue.hxx" 35 | -------------------------------------------------------------------------------- /src/utils/SimpleQueue.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2015, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file SimpleQueue.cxx 28 | * A simple fast single-linked queue class with non-virtual methods. 29 | * 30 | * @author Balazs Racz 31 | * @date 6 Apr 2015 32 | */ 33 | 34 | #include "utils/SimpleQueue.hxx" 35 | 36 | QMember* const SimpleQueue::PTR_END = nullptr; 37 | -------------------------------------------------------------------------------- /src/openlcb/PIPClient.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2015, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file PIPClient.cxx 28 | * 29 | * PIP client definitions. 30 | * 31 | * @author Balazs Racz 32 | * @date 2 Nov 2015 33 | */ 34 | 35 | #include "openlcb/PIPClient.hxx" 36 | 37 | namespace openlcb { 38 | 39 | long long PIP_CLIENT_TIMEOUT_NSEC = SEC_TO_NSEC(4); 40 | 41 | } // namespace openlcb 42 | -------------------------------------------------------------------------------- /src/utils/JSHubPort.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2015, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file JSHubPort.hxx 28 | * 29 | * Shared base class for all javascript "device drivers". 30 | * 31 | * @author Balazs Racz 32 | * @date 13 Sep 2015 33 | */ 34 | 35 | #ifdef __EMSCRIPTEN__ 36 | 37 | extern int JSHubPort_debug_port_num; 38 | int JSHubPort_debug_port_num = 0; 39 | 40 | #endif // __EMSCRIPTEN__ 41 | -------------------------------------------------------------------------------- /src/freertos_drivers/arduino/Can.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2018, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file Can.cxx 28 | * 29 | * Base class for CAN device drivers in the Arduino environment. 30 | * 31 | * @author Balazs Racz 32 | * @date 8 July 2018 33 | */ 34 | 35 | #include "Can.hxx" 36 | #include "can_frame.h" 37 | #include 38 | 39 | unsigned Can::numReceivedPackets_{0}; 40 | unsigned Can::numTransmittedPackets_{0}; 41 | -------------------------------------------------------------------------------- /src/openlcb/WriteHelper.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2013, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file WriteHelper.cxx 28 | * 29 | * Class that allows enqueing an outgoing message. 30 | * 31 | * @author Balazs Racz 32 | * @date 3 Nov 2013 33 | */ 34 | 35 | #include 36 | 37 | #include "openlcb/WriteHelper.hxx" 38 | #include "nmranet_config.h" 39 | #include "endian.h" 40 | 41 | namespace openlcb 42 | { 43 | 44 | } /* namespace openlcb */ 45 | 46 | -------------------------------------------------------------------------------- /src/freertos_drivers/arduino/WifiDefs.hxx: -------------------------------------------------------------------------------- 1 | #ifndef _FREERTOS_DRIVERS_COMMON_WIFIDEFS_HXX_ 2 | #define _FREERTOS_DRIVERS_COMMON_WIFIDEFS_HXX_ 3 | 4 | #include 5 | 6 | /// Wifi not associated to access point: continuous short blinks. 7 | #define WIFI_BLINK_NOTASSOCIATED 0b1010 8 | /// Waiting for IP address: double short blink, pause, double short blink, ... 9 | #define WIFI_BLINK_ASSOC_NOIP 0b101000 10 | /// Connecting to hub: long blinks 11 | #define WIFI_BLINK_CONNECTING 0b1100 12 | /// Connecting to hub: long blinks 13 | #define WIFI_BLINK_FAILED 0b10101100 14 | 15 | enum class WlanState : uint8_t 16 | { 17 | OK = 0, 18 | NOT_ASSOCIATED = 1, 19 | NO_IP, 20 | NO_CONNECTION, 21 | CONNECTING, 22 | MDNS_LOOKUP = 5, 23 | CONNECT_MDNS, 24 | CONNECT_STATIC, 25 | CONNECT_FAILED, 26 | CONNECTION_LOST, 27 | WRONG_PASSWORD, 28 | UPDATE_DISPLAY = 20, 29 | }; 30 | 31 | /** Operating Role. 32 | */ 33 | enum class WlanRole : uint8_t 34 | { 35 | UNKNOWN = 0, /**< Wi-Fi station mode */ 36 | STA, /**< Wi-Fi station mode */ 37 | AP /**< Wi-Fi access point mode */ 38 | }; 39 | 40 | enum class CountryCode : uint8_t 41 | { 42 | US, ///< United States 43 | EU, ///< European Union 44 | JP, ///< Japan 45 | UNKNOWN, ///< unknown country code 46 | }; 47 | 48 | enum class WlanConnectResult 49 | { 50 | CONNECT_OK = 0, ///< success 51 | PASSWORD_INVALID, /// password privided is invalid 52 | }; 53 | 54 | extern "C" { 55 | /// Name of wifi accesspoint to connect to. 56 | extern char WIFI_SSID[]; 57 | /// Password of wifi connection. If empty, use no encryption. 58 | extern char WIFI_PASS[]; 59 | /// Hostname at which the OpenLCB hub is at. 60 | extern char WIFI_HUB_HOSTNAME[]; 61 | /// Port number of the OpenLCB hub. 62 | extern int WIFI_HUB_PORT; 63 | } 64 | 65 | #endif // _FREERTOS_DRIVERS_COMMON_WIFIDEFS_HXX_ 66 | -------------------------------------------------------------------------------- /src/utils/ServiceLocator.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2019, John Socha-Leialoha 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file ServiceLocator.cxx 28 | * This file implements a very simple service locator where you can register 29 | * and retrieve pointers by name. 30 | * 31 | * @author John Socha-Leialoha 32 | * @date 15 September 2019 33 | */ 34 | 35 | #include "utils/ServiceLocator.hxx" 36 | 37 | std::map> ServiceLocatorImpl::services; 38 | Atomic ServiceLocatorImpl::lock_; -------------------------------------------------------------------------------- /src/os/OSImpl.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2019, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file OSImpl.cxx 28 | * C++ implementation of certain OS functions. 29 | * 30 | * @author Balazs Racz 31 | * @date 10 April 2019 32 | */ 33 | 34 | #include "utils/Atomic.hxx" 35 | 36 | static Atomic osGlobalAtomic; 37 | 38 | extern "C" { 39 | 40 | void os_atomic_lock() { 41 | osGlobalAtomic.lock(); 42 | } 43 | 44 | void os_atomic_unlock() { 45 | osGlobalAtomic.unlock(); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/executor/Service.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2013, Stuart W Baker 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file Service.cxx 28 | * 29 | * Class to control execution of state machines pulled out of a queue. 30 | * 31 | * @author Stuart W Baker 32 | * @date 20 December 2013 33 | */ 34 | 35 | #include "executor/Service.hxx" 36 | #include "executor/StateFlow.hxx" 37 | #include "utils/Buffer.hxx" 38 | 39 | Service::Service(ExecutorBase *e) 40 | : executor_(e) 41 | { 42 | init_main_buffer_pool(); 43 | } 44 | -------------------------------------------------------------------------------- /src/openlcb/Payload.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2014, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file Payload.hxx 28 | * 29 | * Forward declaration of the class storing the payload value in an NMRAnet 30 | * message object. 31 | * 32 | * @author Balazs Racz 33 | * @date 18 May 2014 34 | */ 35 | 36 | #ifndef _OPENLCB_PAYLOAD_HXX_ 37 | #define _OPENLCB_PAYLOAD_HXX_ 38 | 39 | #include 40 | 41 | namespace openlcb { 42 | 43 | typedef string Payload; 44 | 45 | } // namespace openlcb 46 | 47 | #endif // _OPENLCB_PAYLOAD_HXX_ 48 | -------------------------------------------------------------------------------- /src/utils/Destructable.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2015, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file Destructable.hxx 28 | * 29 | * A base class with a virtual destructor. 30 | * 31 | * @author Balazs Racz 32 | * @date 11 Mar 2015 33 | */ 34 | 35 | #ifndef _UTILS_DESTRUCTABLE_HXX_ 36 | #define _UTILS_DESTRUCTABLE_HXX_ 37 | 38 | /// Base class of everything with a virtual destructor. Useful to keep a 39 | /// mixture of objects in a vector> to avoid leaking 40 | /// them. 41 | class Destructable { 42 | public: 43 | virtual ~Destructable() {} 44 | }; 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /src/openlcb/BroadcastTime.cpp: -------------------------------------------------------------------------------- 1 | /** @copyright 2 | * Copyright (c) 2018, Stuart W. Baker 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * @file BroadcastTime.cxx 28 | * 29 | * Implementation of a Broadcast Time Protocol Interface. 30 | * 31 | * @author Stuart W. Baker 32 | * @date 4 November 2018 33 | */ 34 | 35 | #ifndef _POSIX_C_SOURCE 36 | #define _POSIX_C_SOURCE 200112L 37 | #endif 38 | 39 | #include "openlcb/BroadcastTime.hxx" 40 | 41 | namespace openlcb 42 | { 43 | 44 | // 45 | // BroadcastTimeClient::clear_timezone 46 | // 47 | void BroadcastTime::clear_timezone() 48 | { 49 | setenv("TZ", "GMT0", 1); 50 | tzset(); 51 | } 52 | 53 | } // namespace openlcb 54 | -------------------------------------------------------------------------------- /src/dcc/PacketFlowInterface.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2015, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file PacketFlowInterface.hxx 28 | * 29 | * Shared declarations for sending DCC packets. 30 | * 31 | * @author Balazs Racz 32 | * @date 16 May 2015 33 | */ 34 | 35 | #ifndef _DCC_PACKETFLOWINTERFACE_HXX_ 36 | #define _DCC_PACKETFLOWINTERFACE_HXX_ 37 | 38 | #include "executor/StateFlow.hxx" 39 | #include "dcc/Packet.hxx" 40 | 41 | namespace dcc { 42 | 43 | /// Interface for flows and ports receiving a sequence of DCC (track) packets. 44 | typedef FlowInterface> PacketFlowInterface; 45 | 46 | } // namespace dcc 47 | 48 | 49 | #endif 50 | -------------------------------------------------------------------------------- /src/utils/Revision.hxx: -------------------------------------------------------------------------------- 1 | /** @copyright 2 | * Copyright (c) 2018, Stuart W. Baker 3 | * All rights reserved 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are strictly prohibited without written consent. 7 | * 8 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 9 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 10 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 11 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 12 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 13 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 14 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 15 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 16 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 17 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 18 | * POSSIBILITY OF SUCH DAMAGE. 19 | * 20 | * @file Revision.hxx 21 | * 22 | * Utility for obtaining build revision info. 23 | * 24 | * @author Stuart Baker 25 | * @date 28 July 2018 26 | */ 27 | 28 | #ifndef _UTILS_REVISION_H_ 29 | #define _UTILS_REVISION_H_ 30 | 31 | #include 32 | 33 | /// Helper object for obtaining buid revision info. 34 | class Revision 35 | { 36 | public: 37 | /// Get a revision out of the revision string array. 38 | /// @param index index within the array to get the string for 39 | /// @return revision string, else empty string if at or beyond array bounds 40 | static std::string get(unsigned index) 41 | { 42 | if (index < count()) 43 | { 44 | return REVISION[index]; 45 | } 46 | return ""; 47 | } 48 | 49 | /// Get the total number of valid revision strings 50 | /// @return total number of valid revision strings 51 | static size_t count(); 52 | 53 | private: 54 | static const char *REVISION[]; 55 | }; 56 | 57 | #endif // _UTILS_REVISION_H_ 58 | -------------------------------------------------------------------------------- /src/utils/ConfigUpdateListener.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2015, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file ConfigUpdateListener.cxx 28 | * 29 | * Definitions for the default config update listener. 30 | * 31 | * @author Balazs Racz 32 | * @date 21 Dec 2015 33 | */ 34 | 35 | #include "utils/ConfigUpdateListener.hxx" 36 | #include "utils/ConfigUpdateService.hxx" 37 | 38 | DefaultConfigUpdateListener::DefaultConfigUpdateListener() 39 | { 40 | Singleton::instance()->register_update_listener(this); 41 | } 42 | DefaultConfigUpdateListener::~DefaultConfigUpdateListener() 43 | { 44 | Singleton::instance()->unregister_update_listener( 45 | this); 46 | } 47 | -------------------------------------------------------------------------------- /src/dcc/DccDebug.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2017, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file DccDebug.hxx 28 | * 29 | * Defines helper functions for debugging DCC packets 30 | * 31 | * @author Balazs Racz 32 | * @date 28 Dec 2017 33 | */ 34 | 35 | #ifndef _DCC_DCCDEBUG_HXX_ 36 | #define _DCC_DCCDEBUG_HXX_ 37 | 38 | #include "dcc/Packet.hxx" 39 | 40 | namespace dcc { 41 | 42 | /// Renders a DCC packet as a debug string. 43 | /// 44 | /// @param pkt DCC packet 45 | /// @param bin_payload if true, displays all bytes in hex 46 | /// 47 | /// @return debug string 48 | /// 49 | string packet_to_string(const DCCPacket& pkt, bool bin_payload = false); 50 | 51 | } // namespace dcc 52 | 53 | #endif // _DCC_DCCDEBUG_HXX_ 54 | -------------------------------------------------------------------------------- /src/openlcb/DccAccyProducer.cpp: -------------------------------------------------------------------------------- 1 | /** @copyright 2 | * Copyright (c) 2017, Stuart W Baker 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * @file DccAccyProducer.cxx 28 | * 29 | * Producer class that represents 2044 consecutive bits out of DCC accessory 30 | * control Well-Known Event ID space. 31 | * 32 | * @author Stuart Baker 33 | * @date 17 June 2017 34 | */ 35 | 36 | #include "openlcb/DccAccyProducer.hxx" 37 | 38 | namespace openlcb 39 | { 40 | 41 | uninitialized DccAccyProducer::eventProducer_; 42 | uninitialized> DccAccyProducer::instances_; 43 | OSThreadOnce DccAccyProducer::once_(DccAccyProducer::once_routine); 44 | Atomic DccAccyProducer::instancesLock_; 45 | 46 | } // namespace openlcb 47 | -------------------------------------------------------------------------------- /src/openlcb/TcpDefs.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2017, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file TcpDefs.cxx 28 | * 29 | * Static declarations, enums and helper functions for the OpenLCB TCP 30 | * interfaces. 31 | * 32 | * @author Balazs Racz 33 | * @date 12 September 2017 34 | */ 35 | 36 | #include "openlcb/TcpDefs.hxx" 37 | 38 | namespace openlcb { 39 | 40 | const char TcpDefs::MDNS_PROTOCOL_TCP[] = "_tcp"; 41 | const char TcpDefs::MDNS_SERVICE_NAME_HUB[] = "_openlcb-hub"; 42 | const char TcpDefs::MDNS_SERVICE_NAME_HUB_TCP[] = "_openlcb-hub._tcp"; 43 | const char TcpDefs::MDNS_SERVICE_NAME_GRIDCONNECT_CAN[] = "_openlcb-can"; 44 | const char TcpDefs::MDNS_SERVICE_NAME_GRIDCONNECT_CAN_TCP[] = "_openlcb-can._tcp"; 45 | 46 | } // namespace openlcb 47 | -------------------------------------------------------------------------------- /src/openlcb/DefaultNode.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2013, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file DefaultNode.cxx 28 | * 29 | * Default AsyncNode implementation for a fat virtual node. 30 | * 31 | * @author Balazs Racz 32 | * @date 7 December 2013 33 | */ 34 | 35 | #include "utils/logging.h" 36 | #include "openlcb/DefaultNode.hxx" 37 | #include "openlcb/If.hxx" 38 | 39 | namespace openlcb 40 | { 41 | 42 | extern void StartInitializationFlow(Node* node); 43 | 44 | DefaultNode::DefaultNode(If* iface, NodeID node_id) 45 | : nodeId_(node_id), isInitialized_(0), iface_(iface) 46 | { 47 | iface_->add_local_node(this); 48 | StartInitializationFlow(this); 49 | } 50 | 51 | DefaultNode::~DefaultNode() 52 | { 53 | } 54 | 55 | } // namespace openlcb 56 | -------------------------------------------------------------------------------- /src/openlcb/NodeInitializeFlow.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2013, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file NodeInitializeFlow.cxx 28 | * 29 | * Default AsyncNode implementation for a fat virtual node. 30 | * 31 | * @author Balazs Racz 32 | * @date 7 December 2013 33 | */ 34 | 35 | #include "openlcb/NodeInitializeFlow.hxx" 36 | 37 | namespace openlcb 38 | { 39 | 40 | InitializeFlow::~InitializeFlow() 41 | { 42 | } 43 | 44 | void StartInitializationFlow(Node *node) 45 | { 46 | auto *g_initialize_flow = Singleton::instance(); 47 | auto *b = g_initialize_flow->alloc(); 48 | b->data()->node = node; 49 | g_initialize_flow->send(b); 50 | } 51 | 52 | } // namespace openlcb 53 | 54 | //DEFINE_SINGLETON_INSTANCE(openlcb::InitializeFlow); 55 | -------------------------------------------------------------------------------- /src/openlcb/FirmwareUpgradeDefs.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2019, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file FirmwareUpgradeDefs.hxx 28 | * 29 | * Definitions from the Firmware Upgrade Standard 30 | * 31 | * @author Balazs Racz 32 | * @date 14 Dec 2019 33 | */ 34 | 35 | #ifndef _OPENLCB_FIRMWAREUPGRADEDEFS_HXX_ 36 | #define _OPENLCB_FIRMWAREUPGRADEDEFS_HXX_ 37 | 38 | namespace openlcb 39 | { 40 | 41 | /// Contains definitions from the Firmware Upgrade Standard. 42 | struct FirmwareUpgradeDefs 43 | { 44 | enum 45 | { 46 | ERROR_WRITE_CHECKSUM_FAILED = 0x2088, 47 | ERROR_INCOMPATIBLE_FIRMWARE = 0x1088, 48 | ERROR_CORRUPTED_DATA = 0x1089, 49 | }; 50 | 51 | enum 52 | { 53 | SPACE_FIRMWARE = 0xEF, 54 | }; 55 | }; 56 | 57 | } 58 | 59 | #endif // _OPENLCB_FIRMWAREUPGRADEDEFS_HXX_ 60 | -------------------------------------------------------------------------------- /src/openlcb/TractionProxy.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2015, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file TractionProxy.hxx 28 | * 29 | * Service for automatically creating train nodes from addresses punched into 30 | * the throttle. 31 | * 32 | * @author Balazs Racz 33 | * @date 1 Feb 2015 34 | */ 35 | 36 | #include "executor/Service.hxx" 37 | #include "openlcb/TractionDefs.hxx" 38 | #include "openlcb/TractionTrain.hxx" 39 | 40 | namespace openlcb 41 | { 42 | 43 | /// Implements the unapproved Traction Proxy Protocol for dynamic allocation of 44 | /// train nodes. 45 | class TractionProxyService : public Service 46 | { 47 | public: 48 | TractionProxyService(TrainService *train_service, Node* proxy_node); 49 | ~TractionProxyService(); 50 | 51 | private: 52 | struct Impl; 53 | Impl *impl_; 54 | }; 55 | 56 | } // namespace openlcb 57 | -------------------------------------------------------------------------------- /src/utils/StringPrintf.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2015, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file StringPrintf.hxx 28 | * 29 | * Utility for creating c++ strings on demand with a printf-like structure. 30 | * 31 | * @author Balazs Racz 32 | * @date 10 May 2015 33 | */ 34 | 35 | #ifndef _UTILS_STIRNGPRINTF_HXX_ 36 | #define _UTILS_STIRNGPRINTF_HXX_ 37 | 38 | #include 39 | #include 40 | #include 41 | 42 | #include "utils/macros.h" 43 | 44 | /** Conveninence utility to do a printf directly into a C++ string. 45 | * @param format is a c printf format string. 46 | * @param ... are additional arguments to the format string 47 | * @return a string with the formatted data. */ 48 | std::string StringPrintf(const char *format, ...) 49 | __attribute__((format(printf, 1, 2))); 50 | 51 | #endif // _UTILS_STIRNGPRINTF_HXX_ 52 | -------------------------------------------------------------------------------- /src/utils/errno_exit.c: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2013, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file errno_exit.c 28 | * 29 | * Expensive (in terms of code-size) function to print errno to stderr and exit. 30 | * Due to the code size implications this is in a separate .c file. 31 | * 32 | * @author Balazs Racz 33 | * @date 3 Aug 2013 34 | */ 35 | 36 | #include 37 | #include 38 | 39 | #include "utils/logging.h" 40 | 41 | /// Prints an error message about errno to std error and terminates the current 42 | /// program. 43 | /// 44 | /// @param where C-style string describing where the error has happened. Will 45 | /// be printed too. 46 | /// 47 | void print_errno_and_exit(const char *where) 48 | { 49 | volatile int last_errno; 50 | last_errno = errno; 51 | (void)last_errno; 52 | LOG(FATAL, "%s: error (%d) %s\n", where, errno, strerror(errno)); 53 | } 54 | -------------------------------------------------------------------------------- /src/os/os_private.h: -------------------------------------------------------------------------------- 1 | /** @copyright 2 | * Copyright (c) 2018, Stuart W Baker 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * @file os_private.h 28 | * This file is a bit of a hack and should only used under extreme caution. 29 | * its purpose is to allow alternate niche platform support for the OS API's 30 | * 31 | * @author Stuart W. Baker 32 | * @date 29 December 2018 33 | */ 34 | 35 | #ifndef _OS_OS_PRIVATE_H_ 36 | #define _OS_OS_PRIVATE_H_ 37 | 38 | #ifdef __cplusplus 39 | extern "C" { 40 | #endif 41 | 42 | #if defined (__FreeRTOS__) 43 | extern void os_thread_start(void *arg); 44 | #endif // __FreeRTOS__ 45 | 46 | /// Locks a single global Atomic used to guard some OS structures. 47 | void os_atomic_lock(void); 48 | /// Unlocks a single global Atomic used to guard some OS structures. 49 | void os_atomic_unlock(void); 50 | 51 | #ifdef __cplusplus 52 | } // extern "C" 53 | #endif 54 | 55 | #endif // _OS_OS_PRIVATE_H_ 56 | 57 | -------------------------------------------------------------------------------- /src/OpenMRNLite.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2018, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file OpenMRN.cpp 28 | * 29 | * Implementation that needs to be compiled for the Arduino. 30 | * 31 | * @author Balazs Racz 32 | * @date 24 July 2018 33 | */ 34 | 35 | #include 36 | 37 | namespace openmrn_arduino { 38 | 39 | OpenMRN::OpenMRN(openlcb::NodeID node_id) 40 | { 41 | init(node_id); 42 | } 43 | 44 | #ifdef ESP32 45 | extern "C" { 46 | 47 | #ifndef OPENMRN_EXCLUDE_REBOOT_IMPL 48 | /// Reboots the ESP32 via the arduino-esp32 provided restart function. 49 | void reboot() 50 | { 51 | ESP.restart(); 52 | } 53 | #endif // OPENMRN_EXCLUDE_REBOOT_IMPL 54 | 55 | #ifndef OPENMRN_EXCLUDE_FREE_HEAP_IMPL 56 | ssize_t os_get_free_heap() 57 | { 58 | return ESP.getFreeHeap(); 59 | } 60 | #endif // OPENMRN_EXCLUDE_FREE_HEAP_IMPL 61 | 62 | } 63 | #endif // ESP32 64 | 65 | } // namespace openmrn_arduino 66 | -------------------------------------------------------------------------------- /src/utils/ReflashBootloader.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2015, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file ReflashBootloader.hxx 28 | * 29 | * Declarations for the bootloader table. 30 | * 31 | * @author Balazs Racz 32 | * @date 27 Oct 2015 33 | */ 34 | 35 | #ifndef _UTILS_REFLASHBOOTLOADER_HXX_ 36 | #define _UTILS_REFLASHBOOTLOADER_HXX_ 37 | 38 | #include 39 | 40 | /// Declares that a given piece of data has to be copied from one place in 41 | /// memory to a place in flash. A list of such segments defines how to 42 | /// overwrite the bootloader by the reflash_bootloader application. 43 | typedef struct 44 | { 45 | /// address in the flash space where to write 46 | uint8_t *dst_address; 47 | /// address where to read the golden data from 48 | const uint8_t *src_address; 49 | /// number of bytes to write 50 | uint32_t length; 51 | } SegmentTable; 52 | 53 | 54 | #endif // _UTILS_REFLASHBOOTLOADER_HXX_ 55 | -------------------------------------------------------------------------------- /src/dcc/RailcomDebug.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2015, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file RailcomDebug.cxx 28 | * 29 | * Implementation of railcom printf debugging. 30 | * 31 | * @author Balazs Racz 32 | * @date 18 May 2015 33 | */ 34 | 35 | #include "dcc/RailCom.hxx" 36 | #include "utils/StringPrintf.hxx" 37 | 38 | namespace dcc 39 | { 40 | 41 | string railcom_debug(const Feedback &fb) 42 | { 43 | string ret; 44 | ret += StringPrintf("ch1(%d) ", fb.ch1Size); 45 | for (int i = 0; i < fb.ch1Size; ++i) 46 | { 47 | ret += StringPrintf( 48 | " %02x=%02x", fb.ch1Data[i], railcom_decode[fb.ch1Data[i]]); 49 | } 50 | ret += StringPrintf(" ch2(%d) ", fb.ch2Size); 51 | for (int i = 0; i < fb.ch2Size; ++i) 52 | { 53 | ret += StringPrintf( 54 | " %02x=%02x", fb.ch2Data[i], railcom_decode[fb.ch2Data[i]]); 55 | } 56 | return ret; 57 | } 58 | 59 | } // namespace dcc 60 | -------------------------------------------------------------------------------- /src/openlcb/ConfigEntry.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2015, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file ConfigEntry.cxx 28 | * 29 | * Configuration option reader classes. 30 | * 31 | * @author Balazs Racz 32 | * @date 5 June 2014 33 | */ 34 | 35 | #include "openlcb/ConfigEntry.hxx" 36 | 37 | #include 38 | #include 39 | #include "utils/logging.h" 40 | #include "utils/FdUtils.hxx" 41 | 42 | namespace openlcb 43 | { 44 | 45 | void ConfigEntryBase::repeated_read(int fd, void *buf, size_t size) const 46 | { 47 | int ret = lseek(fd, offset_, SEEK_SET); 48 | ERRNOCHECK("seek_config", ret); 49 | FdUtils::repeated_read(fd, buf, size); 50 | } 51 | 52 | void ConfigEntryBase::repeated_write(int fd, const void *buf, size_t size) const 53 | { 54 | int ret = lseek(fd, offset_, SEEK_SET); 55 | ERRNOCHECK("seek_config", ret); 56 | FdUtils::repeated_write(fd, buf, size); 57 | } 58 | 59 | } // namespace openlcb 60 | -------------------------------------------------------------------------------- /src/utils/GcTcpHub.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2014, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file GcTcpHub.cxx 28 | * A component that starts a gridconnect-protocol HUB listening on a TCP port. 29 | * 30 | * @author Balazs Racz 31 | * @date 26 Apr 2014 32 | */ 33 | 34 | #include 35 | 36 | #include "utils/GcTcpHub.hxx" 37 | 38 | #include "nmranet_config.h" 39 | #include "utils/GridConnectHub.hxx" 40 | 41 | void GcTcpHub::OnNewConnection(int fd) 42 | { 43 | const bool use_select = 44 | (config_gridconnect_tcp_use_select() == CONSTANT_TRUE); 45 | create_gc_port_for_can_hub(canHub_, fd, nullptr, use_select); 46 | } 47 | 48 | GcTcpHub::GcTcpHub(CanHubFlow *can_hub, int port) 49 | : canHub_(can_hub) 50 | , tcpListener_(port, std::bind(&GcTcpHub::OnNewConnection, this, 51 | std::placeholders::_1)) 52 | { 53 | } 54 | 55 | GcTcpHub::~GcTcpHub() 56 | { 57 | tcpListener_.shutdown(); 58 | } 59 | -------------------------------------------------------------------------------- /src/openlcb/DatagramTcp.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2019, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file DatagramTcp.cxx 28 | * 29 | * TCP-If datagram parser and renderer flows. 30 | * 31 | * @author Balazs Racz 32 | * @date 24 March 2019 33 | */ 34 | 35 | #include "openlcb/DatagramTcp.hxx" 36 | 37 | #include "openlcb/DatagramImpl.hxx" 38 | 39 | namespace openlcb 40 | { 41 | 42 | TcpDatagramService::TcpDatagramService( 43 | IfTcp *iface, int num_registry_entries, int num_clients) 44 | : DatagramService(iface, num_registry_entries) 45 | { 46 | auto *dg_send = if_tcp()->addressed_message_write_flow(); 47 | for (int i = 0; i < num_clients; ++i) 48 | { 49 | auto *client_flow = new DatagramClientImpl(if_tcp(), dg_send); 50 | if_tcp()->add_owned_flow(client_flow); 51 | client_allocator()->insert(static_cast(client_flow)); 52 | } 53 | } 54 | 55 | TcpDatagramService::~TcpDatagramService() 56 | { 57 | } 58 | 59 | } // namespace openlcb 60 | -------------------------------------------------------------------------------- /src/utils/Base64.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2019, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file Base64.hxx 28 | * 29 | * Helper function to perform encoding & decoding of Base64 data. 30 | * 31 | * @author Balazs Racz 32 | * @date 31 July 2019 33 | */ 34 | 35 | #ifndef _UTILS_BASE64_HXX_ 36 | #define _UTILS_BASE64_HXX_ 37 | 38 | #include 39 | 40 | /// Performs encoding of data in base64 format. 41 | /// @param binary data to encode 42 | /// @return base64 representation. The size will be (binary.size() + 2) / 3 * 4. 43 | std::string base64_encode(const std::string &binary); 44 | 45 | /// Performs decoding of data from base64 format to binary. 46 | /// @param base64 base64 encoded data 47 | /// @param data decoded data will be written here. 48 | /// @return true if decoding was successful. false if the encoding was 49 | /// erroneous, in which case the content of data is undefined. 50 | bool base64_decode(const std::string &base64, std::string *data); 51 | 52 | #endif // _UTILS_BASE64_HXX_ 53 | -------------------------------------------------------------------------------- /src/utils/StringPrintf.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2015, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file StringPrintf.hxx 28 | * 29 | * Utility for creating c++ strings on demand with a printf-like structure. 30 | * 31 | * @author Balazs Racz 32 | * @date 10 May 2015 33 | */ 34 | 35 | #include "utils/StringPrintf.hxx" 36 | 37 | std::string StringPrintf(const char *format, ...) 38 | { 39 | #ifdef __FreeRTOS__ 40 | static const int kBufSize = 64; 41 | #else 42 | static const int kBufSize = 1000; 43 | #endif 44 | char buffer[kBufSize]; 45 | va_list ap; 46 | 47 | va_start(ap, format); 48 | int n = vsnprintf(buffer, kBufSize, format, ap); 49 | va_end(ap); 50 | HASSERT(n >= 0); 51 | if (n < kBufSize) 52 | { 53 | return string(buffer, n); 54 | } 55 | string ret(n + 1, 0); 56 | va_start(ap, format); 57 | n = vsnprintf(&ret[0], ret.size(), format, ap); 58 | va_end(ap); 59 | HASSERT(n >= 0); 60 | ret.resize(n); 61 | return ret; 62 | } 63 | -------------------------------------------------------------------------------- /src/executor/Service.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2013, Stuart W Baker 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file Service.hxx 28 | * 29 | * Class to control execution of state machines pulled out of a queue. 30 | * 31 | * @author Stuart W Baker 32 | * @date 20 December 2013 33 | */ 34 | 35 | #ifndef _EXECUTOR_SERVICE_HXX_ 36 | #define _EXECUTOR_SERVICE_HXX_ 37 | 38 | #include "executor/Executor.hxx" 39 | 40 | /** Collection of related state machines that pend on incoming messages. 41 | */ 42 | class Service 43 | { 44 | public: 45 | /** Constructor. 46 | * @param e Executor to run this service from. 47 | */ 48 | Service(ExecutorBase *e); 49 | 50 | /** Destructor. */ 51 | ~Service() 52 | { 53 | } 54 | 55 | /** @returns the executor used by this service. */ 56 | ExecutorBase *executor() 57 | { 58 | return executor_; 59 | } 60 | 61 | private: 62 | /** Executor to use */ 63 | ExecutorBase *executor_; 64 | }; 65 | 66 | #endif /* _EXECUTOR_SERVICE_HXX_ */ 67 | -------------------------------------------------------------------------------- /src/os/watchdog.h: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2013, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file watchdog.h 28 | * Interface for a software watchdog that aborts if not reset periodically. 29 | * 30 | * @author Balazs Racz 31 | * @date 25 May 2013 32 | */ 33 | 34 | #ifndef _OS_WATCHDOG_H_ 35 | #define _OS_WATCHDOG_H_ 36 | 37 | #ifdef __cplusplus 38 | extern "C" { 39 | #endif 40 | 41 | /// Starts a watchdog thread that must be reset more often than period_msec. 42 | void start_watchdog(int period_msec); 43 | /// Resets the watchdog. 44 | void reset_watchdog(void); 45 | /** 46 | Inserts a timer that will periodically reset the watchdog, thereby 47 | practically making the watchdog watch the timer thread. This makes it 48 | possible to add timers that watch other resources for not overflowing. 49 | 50 | @param period_msec tells how often to reset the watchdog. 51 | */ 52 | void add_watchdog_reset_timer(int period_msec); 53 | 54 | #ifdef __cplusplus 55 | } 56 | #endif 57 | 58 | #endif // _OS_WATCHDOG_H_ 59 | -------------------------------------------------------------------------------- /src/dcc/Defs.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2014, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file dcc/Defs.hxx 28 | * 29 | * Definitions for DCC concepts. 30 | * 31 | * @author Balazs Racz 32 | * @date 27 Feb 2016 33 | */ 34 | 35 | #ifndef _DCC_DEFS_HXX_ 36 | #define _DCC_DEFS_HXX_ 37 | 38 | namespace dcc { 39 | 40 | /// Which address type this legacy train node uses. These address types 41 | /// translate to mutually independent packets on the track. 42 | enum class TrainAddressType : uint8_t 43 | { 44 | /// DCC packets with short address (1..127) 45 | DCC_SHORT_ADDRESS = 1, 46 | /// DCC packets with long address (128..~10000) 47 | DCC_LONG_ADDRESS, 48 | /// Marklin-motorola packets. Addresses 1..80 are supported. 49 | MM, 50 | /// Unsupported address type (e.g. a protocol we don't have an 51 | /// implementation for). 52 | UNSUPPORTED = 255, 53 | /// Unspecified address type (default / match any). 54 | UNSPECIFIED = 254, 55 | }; 56 | 57 | } // namespace dcc 58 | 59 | #endif 60 | -------------------------------------------------------------------------------- /src/dcc/RailcomHub.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2015, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file RailcomHub.hxx 28 | * 29 | * Declarations for listening to incoming railcom packets. 30 | * 31 | * @author Balazs Racz 32 | * @date 16 May 2015 33 | */ 34 | 35 | #ifndef _DCC_RAILCOMHUB_HXX_ 36 | #define _DCC_RAILCOMHUB_HXX_ 37 | 38 | #include "utils/Hub.hxx" 39 | #include "dcc/RailCom.hxx" 40 | 41 | namespace dcc { 42 | 43 | /// Data payload sent in the buffers for Railcom dispatchers and hubs. 44 | typedef HubContainer > RailcomHubData; 45 | /// Interface class for consumers of railcom data. 46 | typedef FlowInterface > RailcomHubPortInterface; 47 | /// Base class for consumers of railcom data that are implemented as state 48 | /// flows. 49 | typedef StateFlow, QList<1>> RailcomHubPort; 50 | /// The hub flow that sends a copy of each packet to each listener port 51 | /// registered. 52 | typedef GenericHubFlow RailcomHubFlow; 53 | 54 | } // namespace dcc 55 | 56 | #endif // _DCC_RAILCOMHUB_HXX_ 57 | -------------------------------------------------------------------------------- /src/utils/FileUtils.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2015, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file FileUtils.hxx 28 | * 29 | * Utilities for dealing with files on host OSes. 30 | * 31 | * @author Balazs Racz 32 | * @date 4 Dec 2015 33 | */ 34 | 35 | #ifndef _UTILS_FILEUTILS_HXX_ 36 | #define _UTILS_FILEUTILS_HXX_ 37 | 38 | #include 39 | 40 | /// Opens a file, reads the entire contents, stores it in a c++ std::string and 41 | /// returns this string. Helper function in some client applications. Exits the 42 | /// current application if there is an error. 43 | /// 44 | /// @param filename name of file to open. 45 | /// 46 | /// @return the file contents. 47 | /// 48 | string read_file_to_string(const string &filename); 49 | 50 | /// Opens (or creates) a file, truncates it and overwrites the contents with 51 | /// what is given in a string. Terminates the application if an error is 52 | /// encountered. 53 | /// 54 | /// @param filename name of file to open. 55 | /// @param data what to write into the file. 56 | void write_string_to_file(const string &filename, const string &data); 57 | 58 | #endif //_UTILS_FILEUTILS_HXX_ 59 | -------------------------------------------------------------------------------- /src/freertos_drivers/arduino/WifiDefs.cpp: -------------------------------------------------------------------------------- 1 | /** @copyright 2 | * Copyright (c) 2017, Stuart W Baker 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * @file WifiDefs.cxx 28 | * This file provides weak (reference) definitions for the Wi-Fi credentials. 29 | * 30 | * @author Stuart W Baker 31 | * @date 3 February 2017 32 | */ 33 | 34 | // The following defaults are a template for what needs to be defined in a 35 | // given application for Wi-Fi to work. Do not modify this file. Copy these 36 | // globals into an application specific file and remove the weak attribute. 37 | // They are defined here weak so that a build can complete without failure 38 | // but are not expected to work as is. 39 | 40 | extern "C" { 41 | /// Name of wifi accesspoint to connect to. 42 | char __attribute__((weak)) WIFI_SSID[] = "YourSSIDHere"; 43 | /// Password of wifi connection. If empty, use no encryption. 44 | char __attribute__((weak)) WIFI_PASS[] = "pass-or-empty-for-open"; 45 | /// Hostname at which the OpenLCB hub is at. 46 | char __attribute__((weak)) WIFI_HUB_HOSTNAME[] = "10.0.0.7"; 47 | /// Port number of the OpenLCB hub. 48 | int __attribute__((weak)) WIFI_HUB_PORT = 12021; 49 | } 50 | 51 | -------------------------------------------------------------------------------- /src/dcc/UpdateLoop.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2014, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file UpdateLoop.hxx 28 | * 29 | * Proxy implementation to the global update loop. 30 | * 31 | * @author Balazs Racz 32 | * @date 10 May 2014 33 | */ 34 | 35 | #include "dcc/UpdateLoop.hxx" 36 | #include "utils/Singleton.hxx" 37 | 38 | namespace dcc { 39 | 40 | void packet_processor_notify_update(PacketSource* source, unsigned code) { 41 | Singleton::instance()->notify_update(source, code); 42 | } 43 | 44 | /** Adds a new refresh source to the background refresh loop. */ 45 | bool packet_processor_add_refresh_source( 46 | PacketSource *source, unsigned priority) 47 | { 48 | return Singleton::instance()->add_refresh_source( 49 | source, priority); 50 | } 51 | 52 | /** Removes a refresh source from the background refresh loop. */ 53 | void packet_processor_remove_refresh_source(PacketSource* source) { 54 | Singleton::instance()->remove_refresh_source(source); 55 | } 56 | 57 | UpdateLoopBase::~UpdateLoopBase() {} 58 | 59 | } 60 | 61 | //DEFINE_SINGLETON_INSTANCE(dcc::UpdateLoopBase); 62 | -------------------------------------------------------------------------------- /src/dcc/railcom.h: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2016, Stuart W Baker 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file RailCom.h 28 | * 29 | * Defines a RailCom feedback structure. 30 | * 31 | * @author Stuart W Baker 32 | * @date 25 November 2016 33 | */ 34 | 35 | #ifndef _DCC_RAILCOM_H_ 36 | #define _DCC_RAILCOM_H_ 37 | 38 | #ifdef __cplusplus 39 | extern "C" { 40 | #endif 41 | 42 | typedef struct dcc_feedback 43 | { 44 | /// Number of bytes in channel one. 45 | uint8_t ch1Size; 46 | /// Payload of channel 1. 47 | uint8_t ch1Data[2]; 48 | /// Number of bytes in channel two. 49 | uint8_t ch2Size; 50 | /// Payload of channel 2. 51 | uint8_t ch2Data[6]; 52 | /// Used by multi-channel railcom receiver drivers. Specifies which 53 | /// hardware channel captured this data. 54 | uint8_t channel; 55 | /// Opaque identifier that allows linking outgoing dcc::Packet sent to the 56 | /// DCC waveform generator to the incoming dcc::Feedback structure read 57 | /// back from the railcom driver. 58 | uintptr_t feedbackKey; 59 | } DCCFeedback; 60 | 61 | #ifdef __cplusplus 62 | } 63 | #endif 64 | 65 | #endif // _DCC_RAILCOM_H_ 66 | -------------------------------------------------------------------------------- /src/openlcb/RoutingLogic.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2016, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file RoutingLogic.cxx 28 | * 29 | * Dat structure for gateways and routers of OpenLCB, keeping track of the 30 | * necessary information to make routing decisions. 31 | * 32 | * @author Balazs Racz 33 | * @date 23 May 2016 34 | */ 35 | 36 | #include "openlcb/RoutingLogic.hxx" 37 | 38 | namespace openlcb { 39 | 40 | /** Decodes an event range, encoded according to the Event Transport protocol 41 | * specification. 42 | * 43 | * @param event is a pointer to the variable holding the event range. This 44 | * value will be modified to hold only the base value of the event, without the 45 | * mask bits. 46 | * 47 | * @returns the number of mask bits that were there, in the range of 1..64. 48 | */ 49 | uint8_t event_range_to_bit_count(EventId* event) { 50 | EventId e = *event; 51 | EventId mask = 1; 52 | uint8_t ret = 0; 53 | EventId value = e & mask; 54 | while (ret < 64 && ((e & mask) == value)) { 55 | mask <<= 1; 56 | value <<= 1; 57 | ++ret; 58 | } 59 | *event &= ~(mask - 1); 60 | return ret; 61 | } 62 | 63 | } // namespace openlcb 64 | -------------------------------------------------------------------------------- /src/os/stack_malloc.c: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2014, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * @file os/stack_malloc.c 28 | * This file provides a link time way to allocate RAM bubers. 29 | * 30 | * @author Balazs Racz 31 | * @date 24 June 2014 32 | */ 33 | #include 34 | 35 | #if defined(__FreeRTOS__) 36 | const void *__attribute__((weak)) stack_malloc(unsigned long length); 37 | 38 | const void *stack_malloc(unsigned long length) 39 | { 40 | /* We do a trick here to ensure that the compiler will output a stack frame 41 | * for this function. We want to avoid tail-chain optimization in this 42 | * function or else it disappears from the stack traces done for memory 43 | * tracing. */ 44 | void *volatile v = malloc(length); 45 | return v; 46 | } 47 | #endif 48 | 49 | void *buffer_malloc(size_t length) __attribute__((weak)); 50 | 51 | void *buffer_malloc(size_t length) 52 | { 53 | /* We do a trick here to ensure that the compiler will output a stack frame 54 | * for this function. We want to avoid tail-chain optimization in this 55 | * function or else it disappears from the stack traces done for memory 56 | * tracing. */ 57 | void *volatile v = malloc(length); 58 | return v; 59 | } 60 | -------------------------------------------------------------------------------- /src/openlcb/DatagramTcp.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2019, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file DatagramTcp.hxx 28 | * 29 | * TCP-If datagram parser and renderer flows. 30 | * 31 | * @author Balazs Racz 32 | * @date 24 March 2019 33 | */ 34 | 35 | #ifndef _OPENLCB_DATAGRAMTCP_HXX_ 36 | #define _OPENLCB_DATAGRAMTCP_HXX_ 37 | 38 | #include "openlcb/Datagram.hxx" 39 | #include "openlcb/IfTcp.hxx" 40 | 41 | namespace openlcb 42 | { 43 | 44 | /// Implementation of the DatagramService on TCP transfer. This class is also 45 | /// responsible for instantiating the correct DatagramClient objects. 46 | class TcpDatagramService : public DatagramService 47 | { 48 | public: 49 | /// @param iface is the TCP interface. 50 | /// @param num_registry_entries is the size of the registry map (how many 51 | /// datagram handlers can be registered) 52 | /// @param num_clients how many datagram clients to create. These are 53 | /// allocated and freed on demand by flows sending datagrams. 54 | TcpDatagramService(IfTcp *iface, int num_registry_entries, int num_clients); 55 | 56 | ~TcpDatagramService(); 57 | 58 | IfTcp *if_tcp() 59 | { 60 | return static_cast(iface()); 61 | } 62 | }; 63 | 64 | } // namespace openlcb 65 | 66 | #endif // _OPENLCB_DATAGRAMTCP_HXX_ 67 | -------------------------------------------------------------------------------- /src/openlcb/TcpDefs.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2017, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file TcpDefs.hxx 28 | * 29 | * Static declarations, enums and helper functions for the OpenLCB TCP 30 | * interfaces. 31 | * 32 | * @author Balazs Racz 33 | * @date 12 September 2017 34 | */ 35 | 36 | #ifndef _OPENLCB_TCPDEFS_HXX_ 37 | #define _OPENLCB_TCPDEFS_HXX_ 38 | 39 | #include "utils/macros.h" 40 | 41 | namespace openlcb { 42 | 43 | class TcpDefs { 44 | public: 45 | /// Protocol to be used for mDNS broadcast 46 | static const char MDNS_PROTOCOL_TCP[]; 47 | /// base name of the mDNS Service Name for mDNS broadcast as a hub 48 | static const char MDNS_SERVICE_NAME_HUB[]; 49 | /// complete mDNS broadcast name for a TCP hub 50 | static const char MDNS_SERVICE_NAME_HUB_TCP[]; 51 | /// base name of the mDNS Service Name for mDNS broadcast as a client 52 | static const char MDNS_SERVICE_NAME_GRIDCONNECT_CAN[]; 53 | /// complete mDNS broadcast name for a TCP GridConnect protocol client 54 | static const char MDNS_SERVICE_NAME_GRIDCONNECT_CAN_TCP[]; 55 | 56 | private: 57 | /// Nobody can construct this class. 58 | TcpDefs(); 59 | DISALLOW_COPY_AND_ASSIGN(TcpDefs); 60 | }; // class TcpDefs 61 | 62 | } // namespace openlcb 63 | 64 | #endif // _OPENLCB_TCPDEFS_HXX_ 65 | -------------------------------------------------------------------------------- /src/utils/QMember.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2014, Stuart W Baker 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file QMember.hxx 28 | * Base class for objects that can be enqueued. 29 | * 30 | * @author Stuart W. Baker 31 | * @date 10 March 2014 32 | */ 33 | 34 | #ifndef _UTILS_QMEMBER_HXX_ 35 | #define _UTILS_QMEMBER_HXX_ 36 | 37 | #include "utils/macros.h" 38 | 39 | /** Essentially a "next" pointer container. 40 | */ 41 | class QMember 42 | { 43 | public: 44 | /** Initiailize a QMember, in place of a public placement construction. 45 | */ 46 | void init() 47 | { 48 | next = NULL; 49 | } 50 | 51 | protected: 52 | /** Constructor. 53 | */ 54 | QMember() : next(NULL) 55 | { 56 | } 57 | 58 | /** Destructor. 59 | */ 60 | ~QMember() 61 | { 62 | } 63 | 64 | /** pointer to the next member in the queue */ 65 | QMember *next; 66 | 67 | /** This class is a helper of Q */ 68 | friend class Q; 69 | /** This class is a helper of SimpleQueue */ 70 | friend class SimpleQueue; 71 | /** ActiveTimers needs to iterate through the queue. */ 72 | friend class ActiveTimers; 73 | /** ActiveTimers needs to iterate through the queue. */ 74 | friend class ExecutorBase; 75 | friend class TimerTest; 76 | }; 77 | 78 | #endif /* _UTILS_QMEMBER_HXX_ */ 79 | -------------------------------------------------------------------------------- /src/utils/median.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2018, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file median.hxx 28 | * 29 | * Utilities for computing medians 30 | * 31 | * @author Balazs Racz 32 | * @date 24 June 2018 33 | */ 34 | 35 | #ifndef _UTILS_MEDIAN_HXX_ 36 | #define _UTILS_MEDIAN_HXX_ 37 | 38 | /// Sorts two values. 39 | /// @param a first value (will be the smaller after the call) 40 | /// @param b second value (will be the greater after the call) 41 | inline void __attribute__((__always_inline__, optimize("O3"))) 42 | comp_swap(unsigned &a, unsigned &b) 43 | { 44 | if (a > b) 45 | { 46 | unsigned tmp = a; 47 | a = b; 48 | b = tmp; 49 | } 50 | } 51 | 52 | /// Computes the median of five values. 53 | /// @return median value. 54 | inline unsigned __attribute__((__always_inline__, optimize("O3"))) 55 | median_5(unsigned a, unsigned b, unsigned c, unsigned d, unsigned e) 56 | { 57 | comp_swap(a, c); 58 | comp_swap(b, d); 59 | comp_swap(a, b); 60 | comp_swap(c, d); 61 | // Now: a is the smallest of [a,b,c,d] 62 | // d is the greatest of [a,b,c,d]; neither of these can be the median. 63 | comp_swap(b, c); 64 | // Now: [a,b,c,d] are sorted 65 | if (e > c) { 66 | return c; 67 | } 68 | if (e < b) { 69 | return b; 70 | } 71 | return e; 72 | } 73 | 74 | #endif // _UTILS_MEDIAN_HXX_ 75 | -------------------------------------------------------------------------------- /src/openlcb/TractionDefs.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2014, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file TractionDefs.cxx 28 | * 29 | * Implementations for static functions related to traction protocol. 30 | * 31 | * @author Balazs Racz 32 | * @date 5 May 2014 33 | */ 34 | 35 | #include "openlcb/TractionDefs.hxx" 36 | 37 | #include 38 | #include "utils/macros.h" 39 | 40 | namespace openlcb { 41 | 42 | SpeedType fp16_to_speed(const void *fp16) { 43 | Velocity speed; 44 | DASSERT(sizeof(speed) == 4); 45 | float16_t input; 46 | const uint8_t* in_p = static_cast(fp16); 47 | // We assemble the input assuming it is big-endian. 48 | input = *in_p++; 49 | input <<= 8; 50 | input |= *in_p; 51 | speed.set_wire(input); 52 | return speed; 53 | } 54 | 55 | void speed_to_fp16(SpeedType speed, void *fp16) { 56 | DASSERT(sizeof(speed) == 4); 57 | float16_t output = speed.get_wire(); 58 | uint8_t* o = static_cast(fp16); 59 | *o++ = output >> 8; 60 | *o = output & 0xff; 61 | } 62 | 63 | const uint64_t TractionDefs::IS_TRAIN_EVENT; 64 | const uint64_t TractionDefs::IS_PROXY_EVENT; 65 | 66 | const uint64_t TractionDefs::NODE_ID_DC_BLOCK; 67 | const uint64_t TractionDefs::NODE_ID_DCC; 68 | const uint64_t TractionDefs::NODE_ID_TMCC; 69 | const uint64_t TractionDefs::NODE_ID_MARKLIN_MOTOROLA; 70 | const uint64_t TractionDefs::NODE_ID_MTH_DCS; 71 | 72 | } // namespace openlcb 73 | -------------------------------------------------------------------------------- /examples/ESP32SerialBridge/config.h: -------------------------------------------------------------------------------- 1 | #ifndef _ARDUINO_EXAMPLE_ESP32SERIALBRIDGE_CONFIG_H_ 2 | #define _ARDUINO_EXAMPLE_ESP32SERIALBRIDGE_CONFIG_H_ 3 | 4 | #include "openlcb/ConfiguredConsumer.hxx" 5 | #include "openlcb/ConfiguredProducer.hxx" 6 | #include "openlcb/ConfigRepresentation.hxx" 7 | #include "openlcb/MemoryConfig.hxx" 8 | 9 | namespace openlcb 10 | { 11 | 12 | /// Defines the identification information for the node. The arguments are: 13 | /// 14 | /// - 4 (version info, always 4 by the standard 15 | /// - Manufacturer name 16 | /// - Model name 17 | /// - Hardware version 18 | /// - Software version 19 | /// 20 | /// This data will be used for all purposes of the identification: 21 | /// 22 | /// - the generated cdi.xml will include this data 23 | /// - the Simple Node Ident Info Protocol will return this data 24 | /// - the ACDI memory space will contain this data. 25 | extern const SimpleNodeStaticValues SNIP_STATIC_DATA = { 26 | 4, 27 | "OpenMRN", 28 | "Arduino Serial Bridge", 29 | ARDUINO_VARIANT, 30 | "1.00"}; 31 | 32 | /// Modify this value every time the EEPROM needs to be cleared on the node 33 | /// after an update. 34 | static constexpr uint16_t CANONICAL_VERSION = 0x1000; 35 | 36 | /// Defines the main segment in the configuration CDI. This is laid out at 37 | /// origin 128 to give space for the ACDI user data at the beginning. 38 | CDI_GROUP(IoBoardSegment, Segment(MemoryConfigDefs::SPACE_CONFIG), Offset(128)); 39 | /// Each entry declares the name of the current entry, then the type and then 40 | /// optional arguments list. 41 | CDI_GROUP_ENTRY(internal_config, InternalConfigData); 42 | CDI_GROUP_END(); 43 | 44 | /// This segment is only needed temporarily until there is program code to set 45 | /// the ACDI user data version byte. 46 | CDI_GROUP(VersionSeg, Segment(MemoryConfigDefs::SPACE_CONFIG), 47 | Name("Version information")); 48 | CDI_GROUP_ENTRY(acdi_user_version, Uint8ConfigEntry, 49 | Name("ACDI User Data version"), Description("Set to 2 and do not change.")); 50 | CDI_GROUP_END(); 51 | 52 | /// The main structure of the CDI. ConfigDef is the symbol we use in main.cxx 53 | /// to refer to the configuration defined here. 54 | CDI_GROUP(ConfigDef, MainCdi()); 55 | /// Adds the tag with the values from SNIP_STATIC_DATA above. 56 | CDI_GROUP_ENTRY(ident, Identification); 57 | /// Adds an tag. 58 | CDI_GROUP_ENTRY(acdi, Acdi); 59 | /// Adds a segment for changing the values in the ACDI user-defined 60 | /// space. UserInfoSegment is defined in the system header. 61 | CDI_GROUP_ENTRY(userinfo, UserInfoSegment); 62 | /// Adds the main configuration segment. 63 | CDI_GROUP_ENTRY(seg, IoBoardSegment); 64 | /// Adds the versioning segment. 65 | CDI_GROUP_ENTRY(version, VersionSeg); 66 | CDI_GROUP_END(); 67 | 68 | } // namespace openlcb 69 | 70 | #endif // _ARDUINO_EXAMPLE_ESP32SERIALBRIDGE_CONFIG_H_ 71 | -------------------------------------------------------------------------------- /src/freertos_drivers/arduino/DeviceBuffer.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2015, Stuart W Baker 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file DeviceBuffer.hxx 28 | * This file provides a buffer class that is useful in the construction of 29 | * FreeRTOS device drivers. 30 | * 31 | * @author Stuart W. Baker 32 | * @date 2 March 2015 33 | */ 34 | 35 | #include "DeviceBuffer.hxx" 36 | 37 | #ifndef ARDUINO 38 | 39 | #include 40 | 41 | /** Block until the wait condition is true. The condition is defined by 42 | * the user of the buffer and could be that there is data in the buffer or 43 | * it could be that there is room in the buffer. In any case, this method 44 | * should be called only when the buffer is locked within a critical 45 | * section. 46 | * 47 | * Internally the lock is released before blocking to prevent 48 | * deadlock. The lock is grabbed once again before the method returns. 49 | * If multiple threads are waiting on the same condition, there is a race 50 | * between them as to who will consume the condition first. Any thread(s) 51 | * loosing that race would typically make another call to 52 | * @ref block_until_condition() until another wakeup condition occurs. 53 | */ 54 | void DeviceBufferBase::block_until_condition(File *file, bool read) 55 | { 56 | fd_set fds; 57 | FD_ZERO(&fds); 58 | int fd = Device::fd_lookup(file); 59 | FD_SET(fd, &fds); 60 | 61 | ::select(fd + 1, read ? &fds : NULL, read ? NULL : &fds, NULL, NULL); 62 | } 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /src/utils/logging.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2013, Stuart W Baker 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file logging.cxx 28 | * Facility to do debug printf's on a configurable loglevel. 29 | * 30 | * @author Balazs Racz 31 | * @date 3 August 2013 32 | */ 33 | 34 | #include "logging.h" 35 | 36 | #if defined(__linux__) || defined(__MACH__) 37 | char logbuffer[4096]; 38 | #elif defined(ESP32) 39 | char logbuffer[1024]; 40 | #else 41 | /// Temporary buffer to sprintf() the log lines into. 42 | char logbuffer[256]; 43 | #endif 44 | 45 | #ifdef LOCKED_LOGGING 46 | os_mutex_t g_log_mutex = OS_MUTEX_INITIALIZER; 47 | #endif 48 | 49 | #ifdef MBED_USE_STDIO_LOGGING // TARGET_LPC1768 50 | 51 | extern "C" { void send_stdio_serial_message(const char* data); } 52 | 53 | void log_output(char* buf, int size) { 54 | if (size <= 0) return; 55 | buf[size] = '\0'; 56 | send_stdio_serial_message(buf); 57 | } 58 | 59 | #elif defined(__linux__) || defined(__MACH__) || defined(__EMSCRIPTEN__) || defined(ESP32) 60 | 61 | #include "utils/stdio_logging.h" 62 | 63 | #else 64 | 65 | /// Prints a line of log to the log output destination. 66 | /// 67 | /// @param buf is the logging buffer. Guaranteed to be available until this 68 | /// function returns. 69 | /// @param size is now many bytes are there in the logging buffer. There is 70 | /// never a terminating \n in the log buffer. There is a terminating zero at 71 | /// buf[size]. 72 | __attribute__((weak)) void log_output(char* buf, int size) {} 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /src/openlcb/TractionTestTrain.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2014, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file TractionTestTrain.hxx 28 | * 29 | * Train implementation plugins helpful for testing without a layout. 30 | * 31 | * @author Balazs Racz 32 | * @date 4 Aug 2014 33 | */ 34 | 35 | #ifndef _OPENLCB_TRACTIONTESTTRAIN_HXX_ 36 | #define _OPENLCB_TRACTIONTESTTRAIN_HXX_ 37 | 38 | #include 39 | 40 | #include "openlcb/TrainInterface.hxx" 41 | 42 | namespace openlcb 43 | { 44 | 45 | /** Test train implementation that just logs every action to the info log. */ 46 | class LoggingTrain : public TrainImpl 47 | { 48 | public: 49 | LoggingTrain(uint32_t legacy_address, 50 | dcc::TrainAddressType address_type = 51 | dcc::TrainAddressType::DCC_LONG_ADDRESS); 52 | ~LoggingTrain(); 53 | void set_speed(SpeedType speed) OVERRIDE; 54 | SpeedType get_speed() OVERRIDE; 55 | void set_emergencystop() OVERRIDE; 56 | bool get_emergencystop() OVERRIDE; 57 | void set_fn(uint32_t address, uint16_t value) OVERRIDE; 58 | uint16_t get_fn(uint32_t address) OVERRIDE; 59 | uint32_t legacy_address() OVERRIDE; 60 | dcc::TrainAddressType legacy_address_type() OVERRIDE; 61 | 62 | private: 63 | uint32_t legacyAddress_; 64 | dcc::TrainAddressType legacyAddressType_; 65 | SpeedType currentSpeed_; 66 | bool estopActive_; 67 | std::map fnValues_; 68 | }; 69 | 70 | } // namespace openlcb 71 | 72 | #endif // _OPENLCB_TRACTIONTESTTRAIN_HXX_ 73 | -------------------------------------------------------------------------------- /src/openlcb/DatagramCan.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2014, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file DatagramCan.hxx 28 | * 29 | * CANbus datagram parser and renderer flows. 30 | * 31 | * @author Balazs Racz 32 | * @date 25 Jan 2014 33 | */ 34 | 35 | #ifndef _OPENLCB_DATAGRAMCAN_HXX_ 36 | #define _OPENLCB_DATAGRAMCAN_HXX_ 37 | 38 | #include "openlcb/IfCan.hxx" 39 | #include "openlcb/Datagram.hxx" 40 | 41 | namespace openlcb 42 | { 43 | 44 | /// Implementation of the DatagramService with the CANbus-specific OpenLCB 45 | /// datagram protocol. This service is responsible for fragmenting outgoing 46 | /// datagram messages to the CANbus, assembling incoming datagram frames into 47 | /// messages and managing the necessary temporary buffers. This class is also 48 | /// responsible for instantiating the correct DatagramClient objects. 49 | class CanDatagramService : public DatagramService 50 | { 51 | public: 52 | /* 53 | * @param num_registry_entries is the size of the registry map (how 54 | * many datagram handlers can be registered)*/ 55 | CanDatagramService(IfCan *iface, int num_registry_entries, 56 | int num_clients); 57 | 58 | ~CanDatagramService(); 59 | 60 | IfCan *if_can() 61 | { 62 | return static_cast(iface()); 63 | } 64 | }; 65 | 66 | /// Creates a CAN datagram parser flow. Exposed for testing only. 67 | Executable *TEST_CreateCanDatagramParser(IfCan *if_can); 68 | 69 | } // namespace openlcb 70 | 71 | #endif // _OPENLCB_DATAGRAMCAN_HXX_ 72 | -------------------------------------------------------------------------------- /src/openlcb/DefaultNode.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2013, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file DefaultNode.hxx 28 | * 29 | * Default AsyncNode implementation for a fat virtual node. 30 | * 31 | * @author Balazs Racz 32 | * @date 7 December 2013 33 | */ 34 | 35 | #ifndef _OPENLCB_DEFAULTNODE_HXX_ 36 | #define _OPENLCB_DEFAULTNODE_HXX_ 37 | 38 | #include "openlcb/Node.hxx" 39 | 40 | namespace openlcb 41 | { 42 | 43 | /// Trivial implementation of a virtual Node. Stores all dynamic information in 44 | /// class member variables. 45 | class DefaultNode: public Node 46 | { 47 | public: 48 | DefaultNode(If* iface, NodeID node_id); 49 | virtual ~DefaultNode(); 50 | 51 | NodeID node_id() OVERRIDE 52 | { 53 | return nodeId_; 54 | } 55 | If* iface() OVERRIDE 56 | { 57 | return iface_; 58 | } 59 | bool is_initialized() OVERRIDE 60 | { 61 | return isInitialized_; 62 | } 63 | 64 | // Sets the initialized status to true. 65 | void set_initialized() OVERRIDE 66 | { 67 | isInitialized_ = 1; 68 | } 69 | 70 | // Used for restarting the stack. 71 | void clear_initialized() OVERRIDE 72 | { 73 | isInitialized_ = 0; 74 | } 75 | 76 | private: 77 | /** 48-bit node identifier of this node. */ 78 | NodeID nodeId_ : 48; 79 | /** 1 if the node has reached initialized state. */ 80 | unsigned isInitialized_ : 1; 81 | /** Interface this node is bound to. */ 82 | If* iface_; 83 | }; 84 | 85 | } // namespace openlcb 86 | 87 | #endif // _OPENLCB_DEFAULTNODE_HXX_ 88 | -------------------------------------------------------------------------------- /src/openlcb/EventHandler.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2013, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file EventHandler.cxx 28 | * Static declarations for handling NMRAnet events. 29 | * 30 | * @author Balazs Racz 31 | * @date 19 October 2013 32 | */ 33 | 34 | #include "openlcb/EventHandler.hxx" 35 | #include "openlcb/WriteHelper.hxx" 36 | 37 | namespace openlcb 38 | { 39 | 40 | EventRegistry::EventRegistry() 41 | { 42 | } 43 | 44 | EventRegistry::~EventRegistry() 45 | { 46 | } 47 | 48 | // static 49 | unsigned EventRegistry::align_mask(EventId *event, unsigned size) 50 | { 51 | // example: size = 140. highest bit set is bit 7 52 | // example2: size = 256. highest bit set is bit 8, but it is power-of-two. 53 | HASSERT(event); 54 | if (size <= 1) 55 | { 56 | return 0; 57 | } 58 | // should be 7 in both examples 59 | unsigned log2 = sizeof(size) * 8 - __builtin_clz(size - 1) - 1; 60 | uint64_t new_event, rounded_range; 61 | do 62 | { 63 | ++log2; // 8 in both examples 64 | rounded_range = 1ULL << log2; // 256 65 | new_event = *event & (~(rounded_range - 1)); 66 | // we have to be careful for overflowing uint64 in new_event = 67 | // rounded_range 68 | } while (log2 < 64 && 69 | ((new_event + (rounded_range - 1)) < (*event + (size - 1)))); 70 | if (log2 >= 64) 71 | { 72 | new_event = 0; 73 | log2 = 64; 74 | } 75 | // The rounding is successful. 76 | *event = new_event; 77 | return log2; 78 | } 79 | 80 | } /* namespace openlcb */ 81 | -------------------------------------------------------------------------------- /src/openlcb/EventHandlerMock.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2013, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file EventHandlerMock.hxx 28 | * 29 | * Helper utilities for testing event handlers. 30 | * 31 | * This file must only ever be included in unittests. 32 | * 33 | * @author Balazs Racz 34 | * @date 7 December 2013 35 | */ 36 | 37 | #ifndef _OPENLCB_EVENTHANDLERMOCK_HXX_ 38 | #define _OPENLCB_EVENTHANDLERMOCK_HXX_ 39 | 40 | #include "gmock/gmock.h" 41 | #include "openlcb/EventHandler.hxx" 42 | 43 | namespace openlcb { 44 | 45 | /// Test handler for receiving incoming event related messages via the 46 | /// EventService. Incoming messages need GoogleMock expectations. 47 | class MockEventHandler : public EventHandler 48 | { 49 | public: 50 | /// Proxies an event handler function to a gmock function. 51 | /// 52 | /// @param FN name of the function to proxy. 53 | #define DEFPROXYFN(FN) \ 54 | MOCK_METHOD3(FN, void(const EventRegistryEntry &, EventReport *event, \ 55 | BarrierNotifiable *done)) 56 | 57 | DEFPROXYFN(handle_event_report); 58 | DEFPROXYFN(handle_consumer_identified); 59 | DEFPROXYFN(handle_consumer_range_identified); 60 | DEFPROXYFN(handle_producer_identified); 61 | DEFPROXYFN(handle_producer_range_identified); 62 | DEFPROXYFN(handle_identify_global); 63 | DEFPROXYFN(handle_identify_consumer); 64 | DEFPROXYFN(handle_identify_producer); 65 | 66 | #undef DEFPROXYFN 67 | }; 68 | 69 | } // namespace openlcb 70 | 71 | #endif // _OPENLCB_EVENTHANDLERMOCK_HXX_ 72 | 73 | 74 | -------------------------------------------------------------------------------- /examples/ESP32WifiCanBridge/config.h: -------------------------------------------------------------------------------- 1 | #ifndef _ARDUINO_EXAMPLE_ESP32WIFICANBRIDGE_CONFIG_H_ 2 | #define _ARDUINO_EXAMPLE_ESP32WIFICANBRIDGE_CONFIG_H_ 3 | 4 | #include "openlcb/ConfiguredConsumer.hxx" 5 | #include "openlcb/ConfiguredProducer.hxx" 6 | #include "openlcb/ConfigRepresentation.hxx" 7 | #include "openlcb/MemoryConfig.hxx" 8 | 9 | #include "freertos_drivers/esp32/Esp32WiFiConfiguration.hxx" 10 | 11 | namespace openlcb 12 | { 13 | 14 | /// Defines the identification information for the node. The arguments are: 15 | /// 16 | /// - 4 (version info, always 4 by the standard 17 | /// - Manufacturer name 18 | /// - Model name 19 | /// - Hardware version 20 | /// - Software version 21 | /// 22 | /// This data will be used for all purposes of the identification: 23 | /// 24 | /// - the generated cdi.xml will include this data 25 | /// - the Simple Node Ident Info Protocol will return this data 26 | /// - the ACDI memory space will contain this data. 27 | extern const SimpleNodeStaticValues SNIP_STATIC_DATA = { 28 | 4, 29 | "OpenMRN", 30 | "Arduino WifiCan Bridge", 31 | ARDUINO_VARIANT, 32 | "1.00"}; 33 | 34 | /// Modify this value every time the EEPROM needs to be cleared on the node 35 | /// after an update. 36 | static constexpr uint16_t CANONICAL_VERSION = 0x1000; 37 | 38 | /// Defines the main segment in the configuration CDI. This is laid out at 39 | /// origin 128 to give space for the ACDI user data at the beginning. 40 | CDI_GROUP(IoBoardSegment, Segment(MemoryConfigDefs::SPACE_CONFIG), Offset(128)); 41 | /// Each entry declares the name of the current entry, then the type and then 42 | /// optional arguments list. 43 | CDI_GROUP_ENTRY(internal_config, InternalConfigData); 44 | CDI_GROUP_ENTRY(wifi, WiFiConfiguration, Name("WiFi Configuration")); 45 | CDI_GROUP_END(); 46 | 47 | /// This segment is only needed temporarily until there is program code to set 48 | /// the ACDI user data version byte. 49 | CDI_GROUP(VersionSeg, Segment(MemoryConfigDefs::SPACE_CONFIG), 50 | Name("Version information")); 51 | CDI_GROUP_ENTRY(acdi_user_version, Uint8ConfigEntry, 52 | Name("ACDI User Data version"), Description("Set to 2 and do not change.")); 53 | CDI_GROUP_END(); 54 | 55 | /// The main structure of the CDI. ConfigDef is the symbol we use in main.cxx 56 | /// to refer to the configuration defined here. 57 | CDI_GROUP(ConfigDef, MainCdi()); 58 | /// Adds the tag with the values from SNIP_STATIC_DATA above. 59 | CDI_GROUP_ENTRY(ident, Identification); 60 | /// Adds an tag. 61 | CDI_GROUP_ENTRY(acdi, Acdi); 62 | /// Adds a segment for changing the values in the ACDI user-defined 63 | /// space. UserInfoSegment is defined in the system header. 64 | CDI_GROUP_ENTRY(userinfo, UserInfoSegment); 65 | /// Adds the main configuration segment. 66 | CDI_GROUP_ENTRY(seg, IoBoardSegment); 67 | /// Adds the versioning segment. 68 | CDI_GROUP_ENTRY(version, VersionSeg); 69 | CDI_GROUP_END(); 70 | 71 | } // namespace openlcb 72 | 73 | #endif // _ARDUINO_EXAMPLE_ESP32WIFICANBRIDGE_CONFIG_H_ 74 | -------------------------------------------------------------------------------- /src/utils/ConfigUpdateService.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2015, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file ConfigUpdateService.hxx 28 | * 29 | * Base class for the configuration listeners to register themself. 30 | * 31 | * @author Balazs Racz 32 | * @date 13 June 2015 33 | */ 34 | 35 | #ifndef _UTILS_CONFIGUPDATESERVICE_HXX_ 36 | #define _UTILS_CONFIGUPDATESERVICE_HXX_ 37 | 38 | #include "utils/Singleton.hxx" 39 | 40 | class ConfigUpdateListener; 41 | 42 | /// Virtual interface for the config update listeners to register themselves 43 | /// for receiving configuration updates. 44 | class ConfigUpdateService : public Singleton 45 | { 46 | public: 47 | /// Adds a config update listener to be called upon configuration 48 | /// updates. Should be called before the startup of the stack in order to 49 | /// ensure that the initial load will be successful. 50 | /// 51 | /// @param listener pointer to the implementation that needs to listen to 52 | /// config updates. 53 | /// 54 | virtual void register_update_listener(ConfigUpdateListener *listener) = 0; 55 | /// Removes a config update listener. Requires: the listener has been 56 | /// inserted before using \ref register_update_listener. 57 | /// 58 | /// @param listener pointer to the implementation that needs to be removed. 59 | /// 60 | virtual void unregister_update_listener(ConfigUpdateListener *listener) = 0; 61 | 62 | /// Executes an update in response to the configuration having changed. 63 | virtual void trigger_update() = 0; 64 | }; 65 | 66 | #endif // _UTILS_CONFIGUPDATESERVICE_HXX_ 67 | -------------------------------------------------------------------------------- /src/executor/Notifiable.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2013, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file Notifiable.cxx 28 | * Implementation of basic Notifiables. 29 | * 30 | * @author Balazs Racz 31 | * @date 2 September 2013 32 | */ 33 | 34 | #include "executor/Notifiable.hxx" 35 | 36 | #include "utils/macros.h" 37 | 38 | /// Default instance of an empty notifiable. 39 | static EmptyNotifiable default_empty_notifiable; 40 | 41 | /// @return Default instance of an empty notifiable. 42 | Notifiable* EmptyNotifiable::DefaultInstance() { 43 | return &default_empty_notifiable; 44 | } 45 | 46 | /// Default instance of a crashing notifiable. 47 | static CrashNotifiable default_crash_notifiable; 48 | 49 | /// @return Default instance of a crashing notifiable. 50 | Notifiable* CrashNotifiable::DefaultInstance() { 51 | return &default_crash_notifiable; 52 | } 53 | 54 | void CrashNotifiable::notify() { DIE("Called CrashNotifiable."); } 55 | 56 | BarrierNotifiable* BarrierNotifiable::new_child() { 57 | AtomicHolder h(this); 58 | count_++; 59 | return this; 60 | } 61 | 62 | void BarrierNotifiable::notify() { 63 | unsigned new_value; 64 | { 65 | AtomicHolder h(this); 66 | HASSERT(count_ && "barrier notifyable received too many notifys"); 67 | new_value = --count_; 68 | } 69 | if (!new_value) { 70 | HASSERT(done_); 71 | done_->notify(); 72 | } 73 | } 74 | 75 | BarrierNotifiable::~BarrierNotifiable() { HASSERT(!count_); } 76 | 77 | BarrierNotifiable* BarrierNotifiable::reset(Notifiable* done) { 78 | AtomicHolder h(this); 79 | HASSERT(!count_); 80 | count_ = 1; 81 | done_ = done; 82 | return this; 83 | } 84 | -------------------------------------------------------------------------------- /src/openlcb/nmranet_constants.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2014, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file nmranet_constants.cxx 28 | * Specifies the default values for configuration constants related to NMRAnet. 29 | * 30 | * @author Balazs Racz 31 | * @date 10 Mar 2015 32 | */ 33 | 34 | #include "utils/constants.hxx" 35 | 36 | /** Number of entries in the remote alias cache */ 37 | DEFAULT_CONST(remote_alias_cache_size, 10); 38 | 39 | /** Number of entries in the local alias cache */ 40 | DEFAULT_CONST(local_alias_cache_size, 3); 41 | 42 | /** Maximum number of local nodes */ 43 | DEFAULT_CONST(local_nodes_count, 2); 44 | 45 | /** Number of datagram registry entries. This is how many datagram handlers can 46 | * be registered (e.g. memory config protocol is one). */ 47 | DEFAULT_CONST(num_datagram_registry_entries, 2); 48 | 49 | /** Number of datagram clients. This is how many datagram send operations can 50 | * happen concurrently. */ 51 | DEFAULT_CONST(num_datagram_clients, 2); 52 | 53 | /** Maximum number of memory spaces that can be registered for the MemoryConfig 54 | * datagram handler. */ 55 | DEFAULT_CONST(num_memory_spaces, 5); 56 | 57 | /** Set to CONSTANT_TRUE if you want to export an "all memory" memory space 58 | * from the SimpleStack. Note that this should not be enabled in production, 59 | * because there is no protection against segfaults in it. */ 60 | DEFAULT_CONST_FALSE(enable_all_memory_space); 61 | 62 | /** Set to CONSTANT_TRUE if you want the nodes to send out producer / consumer 63 | * identified messages at boot time. This is required by the OpenLCB 64 | * standard. */ 65 | DEFAULT_CONST_TRUE(node_init_identify); 66 | -------------------------------------------------------------------------------- /src/openlcb/Node.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2013, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file Node.hxx 28 | * 29 | * Node definition for asynchronous NMRAnet nodes. 30 | * 31 | * @author Balazs Racz 32 | * @date 7 December 2013 33 | */ 34 | 35 | #ifndef _OPENLCB_NODE_HXX_ 36 | #define _OPENLCB_NODE_HXX_ 37 | 38 | #include "openlcb/Defs.hxx" // for NodeID 39 | 40 | namespace openlcb 41 | { 42 | 43 | class If; 44 | 45 | /** Base class for NMRAnet nodes conforming to the asynchronous interface. 46 | * 47 | * It is important for this interface to contain no data members, since certain 48 | * implementations might need to be very lightweight (e.g. a command station 49 | * might have hundreds of train nodes.) 50 | */ 51 | class Node 52 | { 53 | public: 54 | virtual ~Node() {} 55 | // @returns the 48-bit NMRAnet node id for this node. 56 | virtual NodeID node_id() = 0; 57 | // @returns the interface this virtual node is bound to. 58 | virtual If* iface() = 0; 59 | /** @returns true if the node is in the initialized state. 60 | * 61 | * Nodes not in initialized state may not send traffic to the bus. */ 62 | virtual bool is_initialized() = 0; 63 | 64 | /** Callback from the node initialization flow when the node finished 65 | * initialization. Nodes are not required to implement if they are not 66 | * using NodeInitializationFlow. */ 67 | virtual void set_initialized() {} 68 | 69 | /** Callback from the simple stack when the node has to return to 70 | * uninitialized state. */ 71 | virtual void clear_initialized() = 0; 72 | }; 73 | 74 | } // namespace openlcb 75 | 76 | #endif // _OPENLCB_NODE_HXX_ 77 | -------------------------------------------------------------------------------- /src/ifaddrs.h: -------------------------------------------------------------------------------- 1 | /** @copyright 2 | * Copyright (c) 2018, Stuart W Baker 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * @file ifaddr.h 28 | * This file implements Linux/BSD compatible ifaddr.h prototypes. 29 | * 30 | * @author Stuart W. Baker 31 | * @date 2 September 2018 32 | */ 33 | 34 | #ifndef _IFADDRS_H_ 35 | #define _IFADDRS_H_ 36 | 37 | #include 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | 43 | /// network interface address list member 44 | struct ifaddrs 45 | { 46 | struct ifaddrs *ifa_next; ///< next item in list 47 | char *ifa_name; ///< name of interface. 48 | unsigned int *ifa_flags; ///< flags from SIOCGIFFLAGS 49 | struct sockaddr *ifa_addr; ///< address of interface 50 | struct sockaddr *ifa_netmask; ///< netmask of interface 51 | union 52 | { 53 | struct sockaddr *ifu_broadaddr; ///< broadcast address of interface 54 | struct sockaddr *ifu_dstadr; ///< point-to-point destination address 55 | } ifa_ifu; 56 | void *ifa_data; ///< address-specific data 57 | }; 58 | 59 | /// Create a linked list of structures describing the network interfaces of the 60 | /// local system. 61 | /// 62 | /// @param ifap the first item in the list is in *ifap 63 | /// @return 0 upon success, else -1 with errno set appropriately 64 | int getifaddrs(struct ifaddrs **ifap); 65 | 66 | /// Free a previously generated linked list of structures describing the network 67 | /// interfaces of the local system. 68 | /// 69 | /// @param ifa pointer to the list that will be freed 70 | void freeifaddrs(struct ifaddrs *ifa); 71 | 72 | #ifdef __cplusplus 73 | } 74 | #endif 75 | 76 | #endif /* _IFADDRS_H_ */ 77 | -------------------------------------------------------------------------------- /src/utils/GcTcpHub.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2014, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file GcTcpHub.hxx 28 | * A component that starts a gridconnect-protocol HUB listening on a TCP port. 29 | * 30 | * @author Balazs Racz 31 | * @date 26 Apr 2014 32 | */ 33 | 34 | #ifndef _UTILS_GCTCPHUB_HXX_ 35 | #define _UTILS_GCTCPHUB_HXX_ 36 | 37 | #include "utils/socket_listener.hxx" 38 | #include "utils/Hub.hxx" 39 | 40 | class ExecutorBase; 41 | 42 | /** This class runs a CAN-bus HUB listening on TCP socket using the gridconnect 43 | * format. Any new incoming connection will be wired into the same virtual CAN 44 | * hub. All packets will be forwarded to every participant, without 45 | * loopback. */ 46 | class GcTcpHub 47 | { 48 | public: 49 | /// Constructor. 50 | /// 51 | /// @param can_hub Which CAN-hub should we attach the TCP gridconnect hub 52 | /// onto. 53 | /// @param port TCp port number to listen on. 54 | GcTcpHub(CanHubFlow *can_hub, int port); 55 | ~GcTcpHub(); 56 | 57 | /// @return true of the listener is ready to accept incoming connections. 58 | bool is_started() 59 | { 60 | return tcpListener_.is_started(); 61 | } 62 | 63 | private: 64 | /// Callback when a new connection arrives. 65 | /// 66 | /// @param fd filedes of the freshly established incoming connection. 67 | /// 68 | void OnNewConnection(int fd); 69 | 70 | /// @param can_hub Which CAN-hub should we attach the TCP gridconnect hub 71 | /// onto. 72 | CanHubFlow *canHub_; 73 | /// Helper object representing the listening on the socket. 74 | SocketListener tcpListener_; 75 | }; 76 | 77 | #endif // _UTILS_GCTCPHUB_HXX_ 78 | -------------------------------------------------------------------------------- /src/dcc/FakeTrackIf.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2016, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file FakeTrackIf.hxx 28 | * 29 | * Allows running a full commandstation update loop in a virtual (e.g. linux) 30 | * setup for testing. 31 | * 32 | * @author Balazs Racz 33 | * @date 25 Mar 2016 34 | */ 35 | 36 | #ifndef _DCC_FAKETRACKIF_HXX_ 37 | #define _DCC_FAKETRACKIF_HXX_ 38 | 39 | #include "executor/Executor.hxx" 40 | #include "executor/StateFlow.hxx" 41 | #include "dcc/Packet.hxx" 42 | 43 | namespace dcc 44 | { 45 | 46 | /// StateFlow that accepts dcc::Packet structures and drops them to the floor. 47 | class FakeTrackIf : public StateFlow, QList<1>> 48 | { 49 | public: 50 | /// Constructor. 51 | /// 52 | /// @param service defines which executor *this should be running on. 53 | /// @param pool_size how many packets we should generate ahead of time. 54 | FakeTrackIf(Service *service, int pool_size) 55 | : StateFlow, QList<1>>(service) 56 | , pool_(sizeof(Buffer), pool_size) 57 | { 58 | } 59 | 60 | FixedPool *pool() OVERRIDE 61 | { 62 | return &pool_; 63 | } 64 | 65 | protected: 66 | Action entry() OVERRIDE 67 | { 68 | return sleep_and_call(&timer_, MSEC_TO_NSEC(10), STATE(finish)); 69 | } 70 | 71 | /// Do nothing. @return next action. 72 | Action finish() 73 | { 74 | return release_and_exit(); 75 | } 76 | 77 | /// Pool of unallocated packets. 78 | FixedPool pool_; 79 | /// Helper object for timing. 80 | StateFlowTimer timer_{this}; 81 | }; 82 | 83 | } // namespace dcc 84 | 85 | #endif // _DCC_FAKETRACKIF_HXX_ 86 | -------------------------------------------------------------------------------- /src/openlcb/TractionCvCdi.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2014-2016, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file TractionCvCdi.hxx 28 | * 29 | * CDI entry defining the commandstation traindb entry. 30 | * 31 | * @author Balazs Racz 32 | * @date 20 Mar 2016 33 | */ 34 | 35 | #ifndef _OPENLCB_TRACTIONCVCDI_HXX_ 36 | #define _OPENLCB_TRACTIONCVCDI_HXX_ 37 | 38 | #include "openlcb/ConfigRepresentation.hxx" 39 | 40 | namespace openlcb { 41 | 42 | /// CDI group that can be used with the TractionCvSpace class to enable CV 43 | /// access via MemoryConfig protocol. 44 | CDI_GROUP(TractionShortCvSpace, Segment(openlcb::MemoryConfigDefs::SPACE_DCC_CV), Offset(0x7F000000), Name("CV access"), Description("Individual CVs can be read and modified for Railcom-enabled locomotives using POM commands. Write the CV number variable first, then write or read the CV value variable.")); 45 | /// Which CV number to read/write. 46 | CDI_GROUP_ENTRY(number, Uint32ConfigEntry, Name("CV number")); 47 | /// Value of the given CV. 48 | CDI_GROUP_ENTRY(value, Uint8ConfigEntry, Name("CV value")); 49 | /// Verify value 50 | CDI_GROUP_ENTRY(verify_value, Uint8ConfigEntry, Name("Verify CV value"), 51 | Description("Write this value first to send out programming track verify " 52 | "commands")); 53 | CDI_GROUP_ENTRY( 54 | verify_result, Uint8ConfigEntry, Name("Verify CV result"), 55 | Description( 56 | "Refresh this line to verify CV value. Return 0=no ack, 1=ack.")); 57 | 58 | CDI_GROUP_END(); 59 | 60 | static_assert(TractionShortCvSpace::size() == 7, 61 | "Traction CV space's size not as expected."); 62 | 63 | } // namespace openlcb 64 | 65 | #endif // _OPENLCB_TRACTIONCVCDI_HXX_ 66 | -------------------------------------------------------------------------------- /src/utils/Crc.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2014, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file Crc.hxx 28 | * 29 | * Helper functions for computing checksums. 30 | * 31 | * @author Balazs Racz 32 | * @date 16 Dec 2014 33 | */ 34 | 35 | #include 36 | #include 37 | 38 | /** Computes the 16-bit CRC value over data using the CRC16-ANSI (aka 39 | * CRC16-IBM) settings. This involves zero init value, zero terminating value, 40 | * reversed polynomial 0xA001, reversing input bits and reversing output 41 | * bits. The example CRC value of "123456789" is 0xbb3d. 42 | * @param data what to compute the checksum over 43 | * @param length_bytes how long data is 44 | * @return the CRC-16-IBM value of the checksummed data. 45 | */ 46 | uint16_t crc_16_ibm(const void* data, size_t length_bytes); 47 | 48 | /** Computes the triple-CRC value over a chunk of data. checksum is an array of 49 | * 3 halfwords. The first halfword will get the CRC of the data array, the 50 | * second halfword the CRC of all odd bytes (starting with the first byte), the 51 | * third halfword will get the CRC of all even bytes. 52 | * 53 | * This routine is helpful, because a similar routine is part of the TIVA 54 | * microcontroller ROM, thus needs no implementation on an actual part. It is 55 | * important to note that the TIVA version takes the length in 4-byte words and 56 | * allows only using data length divisible by 4. 57 | * @param data what to compute the checksum over 58 | * @param length_bytes how long data is 59 | * @param checksum is the output buffer where to store the 48-bit checksum. 60 | */ 61 | void crc3_crc16_ibm(const void* data, size_t length_bytes, uint16_t* checksum); 62 | -------------------------------------------------------------------------------- /src/utils/GcStreamParser.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2016, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file GcStreamParser.hxx 28 | * 29 | * Finds gridconnect packets in a stream of characters. 30 | * 31 | * @author Balazs Racz 32 | * @date 26 May 2016 33 | */ 34 | 35 | #include 36 | 37 | #include "utils/GcStreamParser.hxx" 38 | #include "utils/gc_format.h" 39 | 40 | bool GcStreamParser::consume_byte(char c) 41 | { 42 | if (c == ':') 43 | { 44 | // Frame is starting here. 45 | offset_ = 0; 46 | return false; 47 | } 48 | if (c == ';') 49 | { 50 | if (offset_ < 0) 51 | { 52 | return false; 53 | } 54 | // Frame ends here. 55 | cbuf_[offset_] = 0; 56 | offset_ = -1; 57 | return true; 58 | } 59 | if (offset_ >= static_cast(sizeof(cbuf_) - 1)) 60 | { 61 | // We overran the buffer, so this can't be a valid frame. 62 | // Reset and look for sync byte again. 63 | offset_ = -1; 64 | return false; 65 | } 66 | if (offset_ >= 0) 67 | { 68 | cbuf_[offset_++] = c; 69 | } 70 | else 71 | { 72 | // Drop byte to the floor -- we're not in the middle of a 73 | // packet. 74 | } 75 | return false; 76 | } 77 | 78 | void GcStreamParser::frame_buffer(std::string* payload) { 79 | if (offset_ >= 0) { 80 | payload->assign(cbuf_, offset_); 81 | } else { 82 | payload->assign(cbuf_); 83 | } 84 | } 85 | 86 | bool GcStreamParser::parse_frame_to_output(struct can_frame* output_frame) { 87 | int ret = gc_format_parse(cbuf_, output_frame); 88 | return (ret == 0); 89 | } 90 | -------------------------------------------------------------------------------- /src/openlcb/CanDefs.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2013, Stuart W Baker 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file CanDefs.cxx 28 | * Implementations of some static functions on NMRAnet CAN identifiers. 29 | * 30 | * @author Stuart W. Baker 31 | * @date 18 September 2013 32 | */ 33 | 34 | #include "openlcb/CanDefs.hxx" 35 | 36 | namespace openlcb 37 | { 38 | 39 | /** Get the NMRAnet MTI from a can identifier. 40 | * @param can_id CAN identifider 41 | * @return NMRAnet MTI 42 | */ 43 | Defs::MTI CanDefs::nmranet_mti(uint32_t can_id) 44 | { 45 | switch (get_can_frame_type(can_id)) 46 | { 47 | default: 48 | return Defs::MTI_NONE; 49 | case GLOBAL_ADDRESSED: 50 | return (Defs::MTI)get_mti(can_id); 51 | case DATAGRAM_ONE_FRAME: 52 | /* fall through */ 53 | case DATAGRAM_FIRST_FRAME: 54 | /* fall through */ 55 | case DATAGRAM_MIDDLE_FRAME: 56 | /* fall through */ 57 | case DATAGRAM_FINAL_FRAME: 58 | return Defs::MTI_DATAGRAM; 59 | case STREAM_DATA: 60 | return Defs::MTI_STREAM_DATA; 61 | } 62 | } 63 | 64 | /** Get the CAN identifier from an NMRAnet mti and source alias. 65 | * @param mti NMRAnet MTI 66 | * @param src Source node alias 67 | * @return CAN identifier 68 | */ 69 | uint32_t CanDefs::can_identifier(Defs::MTI mti, NodeAlias src) 70 | { 71 | return ((src << SRC_SHIFT ) & SRC_MASK) + 72 | ((mti << MTI_SHIFT ) & MTI_MASK) + 73 | ((1 << CAN_FRAME_TYPE_SHIFT) ) + 74 | ((1 << FRAME_TYPE_SHIFT ) ) + 75 | ((1 << PRIORITY_SHIFT ) ); 76 | } 77 | 78 | }; /* namespace openlcb */ 79 | -------------------------------------------------------------------------------- /src/openlcb/SimpleNodeInfoMockUserFile.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2015, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file SimpleNodeInfoMockUserFile.hxx 28 | * 29 | * Mock file implementation for the SNIP user-modifiable data. Use this in 30 | * tests and when there is no storage available. 31 | * 32 | * @author Balazs Racz 33 | * @date 22 Mar 2015 34 | */ 35 | 36 | 37 | #ifndef _OPENLCB_SIMPLENODEINFOMOCKUSERFILE_HXX_ 38 | #define _OPENLCB_SIMPLENODEINFOMOCKUSERFILE_HXX_ 39 | 40 | #include "SimpleNodeInfo.hxx" 41 | #ifdef __FreeRTOS__ 42 | #include "freertos_drivers/common/RamDisk.hxx" 43 | #endif 44 | 45 | #ifndef __WINNT__ 46 | #include "os/TempFile.hxx" 47 | 48 | namespace openlcb { 49 | 50 | /** Helper class for mock implementations. Creates a mock file with the SNIP 51 | * user-modifiable data inside that can be used as SNIP_DYNAMIC_FILENAME. 52 | * 53 | * Usage: 54 | * 55 | * static MockSNIPUserFile g_snip_file(g_dir, "Default user name", "Default user description"); 56 | * const char *const SNIP_DYNAMIC_FILENAME = MockSNIPUserFile::snip_user_file_path; 57 | */ 58 | class MockSNIPUserFile 59 | { 60 | public: 61 | #ifdef __FreeRTOS__ 62 | static constexpr const char* snip_user_file_path = "/etc/snip_user_data"; 63 | #else 64 | static char snip_user_file_path[128]; 65 | #endif 66 | MockSNIPUserFile(const char *user_name, 67 | const char *user_description); 68 | 69 | ~MockSNIPUserFile(); 70 | 71 | private: 72 | #ifdef __FreeRTOS__ 73 | SimpleNodeDynamicValues snipData_; 74 | RamDisk userFile_; 75 | #else 76 | TempFile userFile_; 77 | #endif 78 | }; 79 | 80 | } // namespace openlcb 81 | 82 | #endif // !winnt 83 | #endif // _OPENLCB_SIMPLENODEINFOMOCKUSERFILE_HXX_ 84 | -------------------------------------------------------------------------------- /src/dcc/Address.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2014, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file Address.hxx 28 | * 29 | * Defines structures for holding and identifying DCC addresses. 30 | * 31 | * @author Balazs Racz 32 | * @date 10 May 2014 33 | */ 34 | 35 | #ifndef _DCC_ADDRESS_HXX_ 36 | #define _DCC_ADDRESS_HXX_ 37 | 38 | #include 39 | 40 | #include "utils/macros.h" 41 | 42 | namespace dcc { 43 | 44 | /// Strongly typed wrapper representing a short DCC address. This allows C++ 45 | /// type inference to decide whether a particular value is a long or short 46 | /// address. 47 | struct DccShortAddress { 48 | /// Address value. 49 | uint8_t value; 50 | /// Constructor. @param v is the address value (0<=v<128); 51 | explicit DccShortAddress(uint8_t v) 52 | : value(v) { 53 | HASSERT(value < 128); 54 | } 55 | }; 56 | 57 | /// Strongly typed wrapper representing a long DCC address. This allows C++ 58 | /// type inference to decide whether a particular value is a long or short 59 | /// address. 60 | struct DccLongAddress { 61 | /// Address value. 62 | uint16_t value; 63 | /// Constructor. @param v is the address value (0<=v<10239); 64 | explicit DccLongAddress(uint16_t v) 65 | : value(v) { 66 | HASSERT(value <= 10239); 67 | } 68 | }; 69 | 70 | /// Strongly typed wrapper representing a marklin-motorola protocol 71 | /// address. This address is between 0 and 80. 72 | struct MMAddress { 73 | /// Address value. 74 | uint8_t value; 75 | /// Constructor. @param v is the address value (0<=v<81); 76 | explicit MMAddress(uint8_t v) 77 | : value(v) { 78 | HASSERT(v <= 80); 79 | } 80 | }; 81 | 82 | } // namespace dcc 83 | 84 | #endif // _DCC_ADDRESS_HXX_ 85 | -------------------------------------------------------------------------------- /src/openlcb/SimpleNodeInfoMockUserFile.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2015, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file SimpleNodeInfoMockUserFile.hxx 28 | * 29 | * Mock file implementation for the SNIP user-modifiable data. Use this in 30 | * tests and when there is no storage available. 31 | * 32 | * @author Balazs Racz 33 | * @date 22 Mar 2015 34 | */ 35 | 36 | #ifndef _POSIX_C_SOURCE 37 | #define _POSIX_C_SOURCE 200112L 38 | #endif 39 | 40 | #include "SimpleNodeInfoMockUserFile.hxx" 41 | 42 | #ifdef __FreeRTOS__ 43 | openlcb::MockSNIPUserFile::MockSNIPUserFile(const char *user_name, 44 | const char *user_description) 45 | : snipData_{2} 46 | , userFile_(MockSNIPUserFile::snip_user_file_path, &snipData_, false) 47 | { 48 | strncpy(snipData_.user_name, user_name, sizeof(snipData_.user_name)); 49 | strncpy(snipData_.user_description, user_description, 50 | sizeof(snipData_.user_description)); 51 | } 52 | 53 | openlcb::MockSNIPUserFile::~MockSNIPUserFile() 54 | { 55 | } 56 | 57 | #elif !defined(__WINNT__) 58 | #include "os/TempFile.hxx" 59 | 60 | openlcb::MockSNIPUserFile::MockSNIPUserFile(const char *user_name, 61 | const char *user_description) 62 | : userFile_(*TempDir::instance(), "snip_user_file") 63 | { 64 | init_snip_user_file(userFile_.fd(), user_name, user_description); 65 | HASSERT(userFile_.name().size() < sizeof(snip_user_file_path)); 66 | strncpy(snip_user_file_path, userFile_.name().c_str(), 67 | sizeof(snip_user_file_path)); 68 | } 69 | 70 | char openlcb::MockSNIPUserFile::snip_user_file_path[128] = "/dev/zero"; 71 | 72 | openlcb::MockSNIPUserFile::~MockSNIPUserFile() 73 | { 74 | } 75 | 76 | #endif 77 | 78 | -------------------------------------------------------------------------------- /src/openlcb/EndianHelper.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2014, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file EndianHelper.hxx 28 | * 29 | * Helper functions for dealing with network byte order and NMRAnet identifiers. 30 | * 31 | * @author Balazs Racz 32 | * @date 6 Mar 2014 33 | */ 34 | 35 | #ifndef _OPENLCB_ENDIANHELPER_HXX_ 36 | #define _OPENLCB_ENDIANHELPER_HXX_ 37 | 38 | #include "endian.h" 39 | #include "openlcb/Defs.hxx" 40 | 41 | namespace openlcb { 42 | 43 | /** Takes six bytes (big-endian) from *data, and returns the node ID they 44 | * represent. */ 45 | inline NodeID NetworkToNodeID(const uint8_t* data) { 46 | uint64_t ret = 0; 47 | memcpy(&ret, data, 6); 48 | return be64toh(ret); 49 | } 50 | 51 | /** Takes a node id from @param id, and copies 6 bytes network-endian into 52 | * *dst. */ 53 | inline void NodeIDToNetwork(const NodeID id, uint8_t* dst) { 54 | uint64_t be_id = htobe64(id); 55 | memcpy(dst, &be_id, 6); 56 | } 57 | 58 | /** Takes 8 bytes (big-endian) from *data, and returns the event id they 59 | * represent. */ 60 | inline uint64_t NetworkToEventID(const void* data) { 61 | uint64_t ret = 0; 62 | memcpy(&ret, data, 8); 63 | return be64toh(ret); 64 | } 65 | 66 | /** Takes an event id from id, and copies it network-endian into *data. */ 67 | inline void EventIDToNetwork(const uint64_t event_id, void* dst) { 68 | uint64_t be_id = htobe64(event_id); 69 | memcpy(dst, &be_id, 8); 70 | } 71 | 72 | /** Takes an event ID and returns a network encoding of it in a payload 73 | * buffer.*/ 74 | inline string EventIDToPayload(const uint64_t event_id) { 75 | string v(8, 0); 76 | EventIDToNetwork(event_id, &v[0]); 77 | return v; 78 | } 79 | 80 | } // namespace NRMAnet 81 | 82 | #endif // _OPENLCB_ENDIANHELPER_HXX_ 83 | -------------------------------------------------------------------------------- /src/utils/Clock.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2015, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file Clock.hxx 28 | * 29 | * Abstracts getting time in a testable fashion. 30 | * 31 | * @author Balazs Racz 32 | * @date 5 May 2015 33 | */ 34 | 35 | #include "os/os.h" 36 | 37 | /// Virtual base class for a real-time clock. 38 | /// 39 | /// The purpose for this base class is to have a real and a mock 40 | /// implementation. The Clock* pointer can be injected into components that 41 | /// need to use real time for business logic, and then tests can inject a mock 42 | /// or fake clock to drive the test scenario manually without needing to call 43 | /// sleep. 44 | /// 45 | /// see @ref RealClock and @ref MockClock. 46 | class Clock 47 | { 48 | public: 49 | /// @returns the current time of the clock in nanoseconds. 50 | virtual long long get_time_nsec() = 0; 51 | }; 52 | 53 | /// Implementation of @ref Clock that returns the current real time form the OS. 54 | class RealClock : public Clock 55 | { 56 | public: 57 | long long get_time_nsec() 58 | { 59 | return os_get_time_monotonic(); 60 | } 61 | }; 62 | 63 | /// Fake implementation of @ref Clock that returns an injected time. 64 | /// 65 | /// @todo(balazs.racz) this should be called FakeClock instead. 66 | class MockClock : public Clock 67 | { 68 | public: 69 | /// Consturctor. @param time the clock vale to set. 70 | MockClock(long long time) : time_(time) 71 | { 72 | } 73 | /// @return the mock time. 74 | long long get_time_nsec() 75 | { 76 | return time_; 77 | } 78 | /// @param time is the mock time to set. 79 | void set_time(long long time) 80 | { 81 | time_ = time; 82 | } 83 | private: 84 | /// Current mock time. 85 | long long time_; 86 | }; 87 | -------------------------------------------------------------------------------- /src/utils/MultiMap.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2014, Stuart W Baker 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file MultiMap.hxx 28 | * This file provides a C++ abstraction of the several Key/Value pair 29 | * multi-mapping implementations. 30 | * 31 | * @author Stuart W. Baker 32 | * @date 15 March 2014 33 | */ 34 | 35 | #ifndef _UTILS_MULTIMAP_HXX_ 36 | #define _UTILS_MULTIMAP_HXX_ 37 | 38 | #if defined (__LINEAR_MULTIMAP__) 39 | #include "utils/LinearMultiMap.hxx" 40 | /** Define LinearMap as the base class for @ref MultiMap */ 41 | #define BASE_CLASS LinearMultiMap 42 | 43 | #elif defined (__USE_LIBSTDCPP__) 44 | #include "utils/StlMultiMap.hxx" 45 | /** Define stlMultiMap as the base class for @ref MultiMap */ 46 | #define BASE_CLASS StlMultiMap 47 | 48 | #else 49 | #include "utils/StlMultiMap.hxx" 50 | /** Define StlMultiMap as the base class for @ref MultiMap */ 51 | #define BASE_CLASS StlMultiMap 52 | 53 | #endif 54 | 55 | /** MultiMap abstraction that will allow access to one of StlMultiMap or 56 | * LinearMultiMap. 57 | */ 58 | template class MultiMap : public BASE_CLASS 59 | { 60 | public: 61 | /** Default Constructor which with no mapping entry limit. 62 | */ 63 | MultiMap() 64 | : BASE_CLASS() 65 | { 66 | } 67 | 68 | /** Constructor that limits the number of mappings to a static pool. 69 | * @param entries number of nodes to statically create and track 70 | */ 71 | MultiMap(size_t entries) 72 | : BASE_CLASS(entries) 73 | { 74 | } 75 | 76 | /** Destructor. 77 | */ 78 | ~MultiMap() 79 | { 80 | } 81 | 82 | private: 83 | DISALLOW_COPY_AND_ASSIGN(MultiMap); 84 | }; 85 | 86 | #undef BASE_CLASS 87 | 88 | #endif /* _UTILS_MULTIMAP_HXX_ */ 89 | -------------------------------------------------------------------------------- /src/openlcb/NodeBrowser.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2019, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file NodeBrowser.cxx 28 | * Module that allows monitoring the list of nodes on the network. 29 | * 30 | * @author Balazs Racz 31 | * @date 8 March 2019 32 | */ 33 | 34 | #include "openlcb/NodeBrowser.hxx" 35 | 36 | namespace openlcb 37 | { 38 | 39 | NodeBrowser::NodeBrowser(Node *node, CallbackFunction cb) 40 | : node_(node) 41 | , callback_(std::move(cb)) 42 | { 43 | register_callbacks(); 44 | } 45 | 46 | NodeBrowser::~NodeBrowser() 47 | { 48 | unregister_callbacks(); 49 | } 50 | 51 | void NodeBrowser::refresh() 52 | { 53 | auto b = node_->iface()->global_message_write_flow()->alloc(); 54 | b->data()->reset( 55 | Defs::MTI_VERIFY_NODE_ID_GLOBAL, node_->node_id(), EMPTY_PAYLOAD); 56 | node_->iface()->global_message_write_flow()->send(b); 57 | } 58 | 59 | void NodeBrowser::register_callbacks() 60 | { 61 | node_->iface()->dispatcher()->register_handler( 62 | &handler_, Defs::MTI_VERIFIED_NODE_ID_NUMBER, Defs::MTI_EXACT); 63 | node_->iface()->dispatcher()->register_handler( 64 | &handler_, Defs::MTI_INITIALIZATION_COMPLETE, Defs::MTI_EXACT); 65 | } 66 | 67 | void NodeBrowser::unregister_callbacks() 68 | { 69 | node_->iface()->dispatcher()->unregister_handler_all(&handler_); 70 | } 71 | 72 | NodeBrowser::VerifiedHandler::VerifiedHandler(NodeBrowser *parent) 73 | : parent_(parent) 74 | { 75 | } 76 | 77 | void NodeBrowser::VerifiedHandler::send(Buffer *b, unsigned) 78 | { 79 | auto d = get_buffer_deleter(b); 80 | if (b->data()->payload.size() != 6) 81 | { 82 | return; 83 | } 84 | NodeID tgt = buffer_to_node_id(b->data()->payload); 85 | parent_->callback_(tgt); 86 | } 87 | 88 | } // namespace openlcb 89 | -------------------------------------------------------------------------------- /src/openlcb/DefaultCdi.cpp: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2015, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file DefaultCdi.cxx 28 | * 29 | * Weak definitions of the CDI symbols for SimpleStack. These must be in a 30 | * different compilation unit to avoid gcc optimizing away the weak linker 31 | * symbol dereference. 32 | * 33 | * @author Balazs Racz 34 | * @date 18 Jun 2015 35 | */ 36 | 37 | #include 38 | #include 39 | 40 | namespace openlcb { 41 | 42 | extern const uint16_t __attribute__((weak)) CDI_EVENT_OFFSETS[] = {0}; 43 | 44 | extern const char __attribute__((weak)) CDI_DATA[] = 45 | R"cdi( 46 | 47 | 48 | 49 | 50 | 51 | Manufacturer Information 52 | Manufacturer-provided fixed node description 53 | 54 | Version 55 | 56 | 57 | Manufacturer Name 58 | 59 | 60 | Node Type 61 | 62 | 63 | Hardware Version 64 | 65 | 66 | Software Version 67 | 68 | 69 | 70 | 71 | 72 | User Identification 73 | Lets the user add his own description 74 | 75 | Version 76 | 77 | 78 | Node Name 79 | 80 | 81 | Node Description 82 | 83 | 84 | 85 | 86 | )cdi"; 87 | 88 | 89 | } // namespace openlcb 90 | -------------------------------------------------------------------------------- /src/openlcb/StreamDefs.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2014, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file StreamDefs.hxx 28 | * Constants and helper function useful for a stream implementation. 29 | * 30 | * @author Balazs Racz 31 | * @date 14 December 2014 32 | */ 33 | 34 | #include "openlcb/If.hxx" 35 | 36 | namespace openlcb 37 | { 38 | 39 | /// Static constants and helper functions for the OpenLCB streaming protocol. 40 | struct StreamDefs 41 | { 42 | static const uint16_t MAX_PAYLOAD = 0xffff; 43 | 44 | enum Flags 45 | { 46 | FLAG_CARRIES_ID = 0x01, 47 | FLAG_REJECT_OUT_OF_ORDER = 0x02, 48 | FLAG_PERMANENT_ERROR = 0x40, 49 | FLAG_ACCEPT = 0x80, 50 | }; 51 | 52 | enum AdditionalFlags 53 | { 54 | REJECT_INFO_LOGGED = 0x01, 55 | REJECT_PERMANENT_INVALID_REQUEST = 0x20, 56 | REJECT_PERMANENT_SRC_NOT_PERMITTED = 0x40, 57 | REJECT_PERMANENT_STREAMS_NOT_ACCEPTED = 0x80, 58 | REJECT_TEMPORARY_BUFFER_FULL = 0x20, 59 | REJECT_TEMPORARY_OUT_OF_ORDER = 0x40, 60 | }; 61 | 62 | static Payload create_initiate_request(uint16_t max_buffer_size, 63 | bool has_ident, 64 | uint8_t src_stream_id) 65 | { 66 | Payload p(5, 0); 67 | p[0] = max_buffer_size >> 8; 68 | p[1] = max_buffer_size & 0xff; 69 | p[2] = has_ident ? FLAG_CARRIES_ID : 0; 70 | p[3] = 0; 71 | p[4] = src_stream_id; 72 | return p; 73 | } 74 | 75 | static Payload create_close_request(uint8_t src_stream_id, uint8_t dst_stream_id) 76 | { 77 | Payload p(2, 0); 78 | p[0] = src_stream_id; 79 | p[1] = dst_stream_id; 80 | return p; 81 | } 82 | }; 83 | 84 | } // namespace openlcb 85 | -------------------------------------------------------------------------------- /src/openlcb/BootloaderPort.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2016, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file BootloaderPort.hxx 28 | * 29 | * Helper class for connecting a bootloader HAL to a standard CAN hub. Used for 30 | * unittests and bootloaders that do not need to be small. 31 | * 32 | * @author Balazs Racz 33 | * @date 19 Jun 2016 34 | */ 35 | 36 | #ifndef _OPENLCB_BOOTLOADERPORT_HXX_ 37 | #define _OPENLCB_BOOTLOADERPORT_HXX_ 38 | 39 | #include "utils/Hub.hxx" 40 | 41 | namespace openlcb { 42 | 43 | /** Proxy class for sending canbus traffic to the Bootloader HAL. */ 44 | class BootloaderPort : public CanHubPort 45 | { 46 | public: 47 | BootloaderPort(Service* service) 48 | : CanHubPort(service) 49 | { 50 | is_waiting_ = false; 51 | } 52 | 53 | bool is_waiting() 54 | { 55 | return is_waiting_; 56 | } 57 | 58 | Action entry() override 59 | { 60 | AtomicHolder h(this); 61 | is_waiting_ = true; 62 | return wait_and_call(STATE(sent)); 63 | } 64 | 65 | Action sent() 66 | { 67 | return release_and_exit(); 68 | } 69 | 70 | bool read_can_frame(struct can_frame *frame) 71 | { 72 | { 73 | AtomicHolder h(this); 74 | if (is_waiting_) 75 | { 76 | *frame = *message()->data(); 77 | is_waiting_ = false; 78 | } 79 | else 80 | { 81 | return false; 82 | } 83 | } 84 | notify(); 85 | return true; 86 | } 87 | 88 | private: 89 | /** True if an incoming message is ready for dispatching and the current 90 | * flow is waiting for a notify. */ 91 | bool is_waiting_ = false; 92 | }; 93 | 94 | } // namespace openlcb 95 | 96 | #endif //_OPENLCB_BOOTLOADERPORT_HXX_ 97 | 98 | -------------------------------------------------------------------------------- /src/freertos_drivers/arduino/libatomic.c: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2019, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file libatomic.c 28 | * 29 | * A partial implementation of libatomic for Cortex-M0 for the necessary 30 | * operations in OpenMRN. 31 | * 32 | * @author Balazs Racz 33 | * @date 30 Dec 2019 34 | */ 35 | 36 | #include 37 | 38 | #if defined(STM32F0xx) || (!defined(ARDUINO) && !defined(ESP32)) 39 | // On Cortex-M0 the only way to do atomic operation is to disable interrupts. 40 | 41 | /// Disables interrupts and saves the interrupt enable flag in a register. 42 | #define ACQ_LOCK() \ 43 | int _pastlock; \ 44 | __asm volatile(" mrs %0, PRIMASK \n cpsid i\n" : "=r"(_pastlock)); 45 | 46 | /// Restores the interrupte enable flag from a register. 47 | #define REL_LOCK() __asm volatile(" msr PRIMASK, %0\n " : : "r"(_pastlock)); 48 | 49 | uint16_t __atomic_fetch_sub_2(uint16_t *ptr, uint16_t val, int memorder) 50 | { 51 | ACQ_LOCK(); 52 | uint16_t ret = *ptr; 53 | *ptr -= val; 54 | REL_LOCK(); 55 | return ret; 56 | } 57 | 58 | uint8_t __atomic_exchange_1(uint8_t *ptr, uint8_t val, int memorder) 59 | { 60 | ACQ_LOCK(); 61 | uint8_t ret = *ptr; 62 | *ptr = val; 63 | REL_LOCK(); 64 | return ret; 65 | } 66 | 67 | uint8_t __atomic_fetch_or_1(uint8_t *ptr, uint8_t val, int memorder) 68 | { 69 | ACQ_LOCK(); 70 | uint8_t ret = *ptr; 71 | *ptr = ret | val; 72 | REL_LOCK(); 73 | return ret; 74 | } 75 | 76 | uint8_t __atomic_fetch_and_1(uint8_t *ptr, uint8_t val, int memorder) 77 | { 78 | ACQ_LOCK(); 79 | uint8_t ret = *ptr; 80 | *ptr = ret & val; 81 | REL_LOCK(); 82 | return ret; 83 | } 84 | 85 | #endif // guard for arduino compilation 86 | -------------------------------------------------------------------------------- /src/utils/GcStreamParser.hxx: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2016, Balazs Racz 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file GcStreamParser.hxx 28 | * 29 | * Finds gridconnect packets in a stream of characters. 30 | * 31 | * @author Balazs Racz 32 | * @date 26 May 2016 33 | */ 34 | 35 | #ifndef _UTILS_GCSTREAMPARSER_HXX_ 36 | #define _UTILS_GCSTREAMPARSER_HXX_ 37 | 38 | #include 39 | 40 | /** 41 | Parses a sequence of characters; finds GridConnect protocol packet 42 | boundaries in the sequence of packets. Contains an internal buffer holding 43 | the partial (or last found) gridconnect packet. 44 | 45 | This class is not thread-safe, but thread-compatible. 46 | */ 47 | class GcStreamParser 48 | { 49 | public: 50 | GcStreamParser() 51 | : offset_(-1) 52 | { 53 | } 54 | 55 | /** Adds the next character from the source stream. @return true if the 56 | * internal buffer contains a complete frame. @param c next character. */ 57 | bool consume_byte(char c); 58 | 59 | /** Parses the current contents of the frame buffer to a can_frame 60 | * struct. Should be called if and inly if the previous consume_char call 61 | * returned true. 62 | * 63 | * @param output_frame is an output argument, non-NULL, into this we will 64 | * be writing the binary frame. 65 | * @return true on success, false if there was a parse error. In this case 66 | * the frame is set to an error frame. */ 67 | bool parse_frame_to_output(struct can_frame *output_frame); 68 | 69 | /** @param payload fills with the current contents of the frame buffer. */ 70 | void frame_buffer(std::string *payload); 71 | 72 | private: 73 | /// Collects data from a partial GC packet. 74 | char cbuf_[32]; 75 | /// offset of next byte in cbuf to write. 76 | int offset_; 77 | }; 78 | 79 | #endif // _UTILS_GCSTREAMPARSER_HXX_ 80 | -------------------------------------------------------------------------------- /src/os/watchdog.c: -------------------------------------------------------------------------------- 1 | /** \copyright 2 | * Copyright (c) 2013, Stuart W Baker 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without 6 | * modification, are permitted provided that the following conditions are met: 7 | * 8 | * - Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * - Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 | * POSSIBILITY OF SUCH DAMAGE. 26 | * 27 | * \file watchdog.c 28 | * 29 | * Implements a simple software watchdog that runs a thread which terminates if 30 | * the watchdog was not reset in the given period. 31 | * 32 | * @author Balazs Racz 33 | * @date 26 May 2013 34 | */ 35 | 36 | #define _DEFAULT_SOURCE 37 | 38 | #include 39 | 40 | #include "os/watchdog.h" 41 | #include "os/os.h" 42 | 43 | 44 | /// What is the timeout of the watchdog in milliseconds. 45 | static int watchdog_period_msec; 46 | /// How many times have we seen the watchdog tick without being fed. 47 | static int watchdog_ticks = 0; 48 | 49 | /// Thread running a watchdog. 50 | static void* watchdog_thread(void* arg) 51 | { 52 | while (1) 53 | { 54 | usleep(((useconds_t)1000) * watchdog_period_msec); 55 | if (++watchdog_ticks > 1) 56 | { 57 | #ifdef __FreeRTOS__ 58 | diewith(BLINK_DIE_WATCHDOG); 59 | #else 60 | abort(); 61 | #endif 62 | } 63 | } 64 | return NULL; 65 | } 66 | 67 | void start_watchdog(int period_msec) 68 | { 69 | watchdog_period_msec = period_msec; 70 | reset_watchdog(); 71 | #ifdef __FreeRTOS__ 72 | const int kStackSize = 256; 73 | #else 74 | const int kStackSize = 2048; 75 | #endif 76 | os_thread_create(NULL, "watchdog", 0, kStackSize, 77 | &watchdog_thread, NULL); 78 | } 79 | 80 | void reset_watchdog(void) 81 | { 82 | watchdog_ticks = 0; 83 | } 84 | 85 | #if 0 86 | static long long watchdog_reset_timer(void* unused1, void* unused2) 87 | { 88 | reset_watchdog(); 89 | return OS_TIMER_RESTART; 90 | } 91 | #endif 92 | 93 | void add_watchdog_reset_timer(int period_msec) 94 | { 95 | //os_timer_start(os_timer_create(&watchdog_reset_timer, NULL, NULL), 96 | // MSEC_TO_NSEC(period_msec)); 97 | } 98 | --------------------------------------------------------------------------------