diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 705c42ef8..2f3fd275f 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -104,5 +104,6 @@ define(MODAL_REACTORS) define(NUMBER_OF_FEDERATES) define(NUMBER_OF_WORKERS) define(SCHEDULER) -define(TARGET_FILES_DIRECTORY) +define(LF_SOURCE_DIRECTORY) +define(LF_FILE_SEPARATOR) define(WORKERS_NEEDED_FOR_FEDERATE) diff --git a/core/trace.c b/core/trace.c index 9b2878485..648c26812 100644 --- a/core/trace.c +++ b/core/trace.c @@ -237,7 +237,7 @@ void flush_trace(int worker) { lf_critical_section_exit(); } -void start_trace(char* filename) { +void start_trace(const char* filename) { // FIXME: location of trace file should be customizable. _lf_trace_file = fopen(filename, "w"); if (_lf_trace_file == NULL) { diff --git a/include/api/set.h b/include/api/set.h index 44c29aa1b..3e61f3d37 100644 --- a/include/api/set.h +++ b/include/api/set.h @@ -80,7 +80,7 @@ do { \ } while (0) /** - * Version of lf_set for output types given as 'type[]' where you + * Version of lf_set for output types given as `type[]` or `type*` where you * want to send a previously dynamically allocated array. * * The deallocation is delegated to downstream reactors, which @@ -95,7 +95,7 @@ do { \ #define SET_ARRAY(out, val, elem_size, length) \ do { \ _Pragma ("Warning \"'SET_ARRAY' is deprecated.\""); \ - _LF_SET_ARRAY(out, val, length); \ + lf_set_array(out, val, length); \ } while (0) /** diff --git a/include/core/reactor.h b/include/core/reactor.h index 6ff487082..936b54a8b 100644 --- a/include/core/reactor.h +++ b/include/core/reactor.h @@ -131,14 +131,14 @@ do { \ * @see lf_token_t */ #ifndef __cplusplus -#define _LF_SET_ARRAY(out, val, length) \ +#define lf_set_array(out, val, length) \ do { \ _lf_set_present((lf_port_base_t*)out); \ lf_token_t* token = _lf_initialize_token_with_value((token_template_t*)out, val, length); \ out->value = token->value; \ } while(0) #else -#define _LF_SET_ARRAY(out, val, length) \ +#define lf_set_array(out, val, length) \ do { \ _lf_set_present((lf_port_base_t*)out); \ lf_token_t* token = _lf_initialize_token_with_value((token_template_t*)out, val, length); \ diff --git a/include/core/trace.h b/include/core/trace.h index 5b2cf2589..2aa66f38b 100644 --- a/include/core/trace.h +++ b/include/core/trace.h @@ -248,7 +248,7 @@ int register_user_trace_event(char* description); * Open a trace file and start tracing. * @param filename The filename for the trace file. */ -void start_trace(char* filename); +void start_trace(const char* filename); /** * Trace an event identified by a type and a pointer to the self struct of the reactor instance. diff --git a/util/audio_loop.h b/util/audio_loop.h index 9df35bcc8..011b31eb5 100644 --- a/util/audio_loop.h +++ b/util/audio_loop.h @@ -2,66 +2,34 @@ * @file * @author Edward A. Lee * @author Soroush Bateni + * @copyright (c) 2020-2023, The University of California at Berkeley and UT Dallas. + * License in [BSD 2-clause](https://github.com/lf-lang/reactor-c/blob/main/LICENSE.md) + * + * @brief Utility function for playing audio on Linux or MacOS. * - * @section LICENSE -Copyright (c) 2020, The University of California at Berkeley and UT Dallas - -Redistribution and use in source and binary forms, with or without modification, -are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY -EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL -THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, -STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF -THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - * @section DESCRIPTION - * - * Audio functions for Linux. To start an audio loop, call - * `lf_start_audio_loop`, passing it the logical time at which + * Audio functions for Linux or MacOS. To start an audio loop, call + * `lf_start_audio_loop()`, passing it the logical time at which * you would like the loop to start. To play a waveform, * call `lf_play_audio_waveform()`. A waveform may be * synthesized or read from a .wav file using * `read_wave_file()` (see wave_file_reader.h). * - * To use this, include the following flags in your target properties: - * If you are running on Linux: + * To use this, include the following in your target properties: *
- * target C { - * flags: "-lasound -lm", - * files: ["/lib/C/util/audio_loop_linux.c", "/lib/C/util/audio_loop.h"] - * }; - *- * If you are running on Mac: - *
- * target C { - * flags: "-framework AudioToolbox -framework CoreFoundation -lm", - * files: ["/lib/C/util/audio_loop_mac.c", "/lib/C/util/audio_loop.h"] - * }; + files: [ + "/lib/c/reactor-c/util/audio_loop_mac.c", + "/lib/c/reactor-c/util/audio_loop.h", + "/lib/c/reactor-c/util/audio_loop_linux.c", + ], + cmake-include: [ + "/lib/c/reactor-c/util/audio_loop.cmake" + ] ** * In addition, you need this in your Lingua Franca file: - * If you are running on Linux: - *
- * preamble {= - * #include "audio_loop_linux.c" - * =} - *- * If you are running on Mac: *
* preamble {= - * #include "audio_loop_mac.c" + * #include "audio_loop.h" * =} **/ diff --git a/util/audio_loop_linux.c b/util/audio_loop_linux.c index 52ea6a9a2..ecc9e4395 100644 --- a/util/audio_loop_linux.c +++ b/util/audio_loop_linux.c @@ -2,33 +2,10 @@ * @file * @author Edward A. Lee * @author Soroush Bateni - * - * @section LICENSE -Copyright (c) 2020, The University of California at Berkeley and TU Dresden - -Redistribution and use in source and binary forms, with or without modification, -are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY -EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL -THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, -STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF -THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - * @section DESCRIPTION + * @copyright (c) 2020-2023, The University of California at Berkeley and UT Dallas. + * License in [BSD 2-clause](https://github.com/lf-lang/reactor-c/blob/main/LICENSE.md) * - * Audio functions for Linux. + * @brief Utility function for playing audio on Linux. * * See audio_loop.h for instructions. * @@ -313,13 +290,6 @@ void* run_audio_loop(void* ignored) { pthread_t loop_thread_id; bool loop_thread_started = false; -/** - * Start an audio loop thread that becomes ready to receive - * audio amplitude samples via add_to_sound(). If there is - * already an audio loop running, then do nothing. - * @param start_time The logical time that aligns with the - * first audio buffer. - */ void lf_start_audio_loop(instant_t start_time) { if (loop_thread_started) return; @@ -341,30 +311,10 @@ void lf_start_audio_loop(instant_t start_time) { pthread_create(&loop_thread_id, NULL, &run_audio_loop, NULL); } -/** - * Stop the audio loop thread. - */ void lf_stop_audio_loop() { stop_audio = true; } -/** - * Play the specified waveform with the specified emphasis at - * the specified time. If the waveform is null, play a simple tick - * (an impulse). If the waveform has length zero or volume 0, - * play nothing. - * - * If the time is too far in the future - * (beyond the window of the current audio write buffer), then - * block until the audio output catches up. If the audio playback - * has already passed the specified point, then play the waveform - * as soon as possible and return 1. - * Otherwise, return 0. - * - * @param waveform The waveform to play or NULL to just play a tick. - * @param emphasis The emphasis (0.0 for silence, 1.0 for waveform volume). - * @param start_time The time to start playing the waveform. - */ int lf_play_audio_waveform(lf_waveform_t* waveform, float emphasis, instant_t start_time) { int result = 0; pthread_mutex_lock(&lf_audio_mutex); diff --git a/util/audio_loop_mac.c b/util/audio_loop_mac.c index 0a2ada606..c592d2d4d 100644 --- a/util/audio_loop_mac.c +++ b/util/audio_loop_mac.c @@ -1,33 +1,10 @@ /** * @file * @author Edward A. Lee - * - * @section LICENSE -Copyright (c) 2020, The University of California at Berkeley and TU Dresden - -Redistribution and use in source and binary forms, with or without modification, -are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY -EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL -THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, -STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF -THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - * @section DESCRIPTION + * @copyright (c) 2020-2023, The University of California at Berkeley and UT Dallas. + * License in [BSD 2-clause](https://github.com/lf-lang/reactor-c/blob/main/LICENSE.md) * - * Audio functions for MacOS. + * @brief Utility function for playing audio on MacOS. * * See audio_loop.h for instructions. * diff --git a/util/sensor_simulator.c b/util/sensor_simulator.c index 38cfea964..6f404b95e 100644 --- a/util/sensor_simulator.c +++ b/util/sensor_simulator.c @@ -1,34 +1,12 @@ /** -@file -@author Edward A. Lee (eal@berkeley.edu) - -@section LICENSE -Copyright (c) 2020, The University of California at Berkeley. - -Redistribution and use in source and binary forms, with or without modification, -are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY -EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL -THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, -STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF -THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -@section DESCRIPTION - -See sensor_simulator.h. -*/ + * @file + * @author Edward A. Lee + * @copyright (c) 2020-2023, The University of California at Berkeley and UT Dallas. + * License in [BSD 2-clause](https://github.com/lf-lang/reactor-c/blob/main/LICENSE.md) + * + * @brief Simple terminal-based user interface based on ncurses. + * See sensor_simulator.h. + */ #include
-target C { - cmake-include: "include/ncurses-cmake-extension.txt", // Adds support for ncurses - files: ["/lib/C/util/sensor_simulator.c", "/lib/C/util/sensor_simulator.h"] -}; --This requires `ncurses`, a library providing somewhat portable keyboard access. - -In addition, you need this in your Lingua Franca file: -
-preamble {= - #include "sensor_simulator.c" -=} --To start the sensor simulator, call `start_sensor_simulator` passing it -an array of strings to print and the width of the window to use to display -characters using the `show_tick` function. - -To print messages to the screen, rather than using printf(), you should use -the messaging functions in util.h, such as lf_print(). Otherwise, your messages -will be printed over other information. -*/ - #ifndef SENSOR_SIMULATOR_H #define SENSOR_SIMULATOR_H +/** + * @file + * @author Edward A. Lee + * @copyright (c) 2020-2023, The University of California at Berkeley and UT Dallas. + * License in [BSD 2-clause](https://github.com/lf-lang/reactor-c/blob/main/LICENSE.md) + * + * @brief Simple terminal-based user interface based on ncurses. + * + * When prototyping Lingua Franca programs on a laptop, it is convenient to use + * the laptop keyboard to simulate asynchronous sensor input. This small library + * provides a convenient way to do that. + * + * To use this, include the following flags in your target properties: + *
+ * target C { + files: [ + "/lib/c/reactor-c/util/sensor_simulator.c", + "/lib/c/reactor-c/util/sensor_simulator.h", + ], + cmake-include: [ + "/lib/c/reactor-c/util/sensor_simulator.cmake", + ] + * }; + *+ * This requires `ncurses`, a library providing somewhat portable keyboard access. + * + * In addition, you need this in your Lingua Franca file: + *
+ * preamble {= + * #include "sensor_simulator.c" + * =} + *+ * To start the sensor simulator, call `start_sensor_simulator` passing it + * an array of strings to print and the width of the window to use to display + * characters using the `show_tick` function. + * + * To print messages to the screen, rather than using printf(), you should use + * the messaging functions in util.h, such as lf_print(). Otherwise, your messages + * will be printed over other information. + */ /** * Start the sensor simulator if it has not been already * started. This must be called at least once before any - * call to register_sensor_key. If given, this will - * put a message in the center of the terminal window. - * The message will be left justified, with each string - * in the specified array on a new line. - * If a tick_window_width is given, then a second window - * will be created that is intended to show activity, - * typically using a single character per event. - * @param message_lines The message lines or NULL for none. - * @param number_of_lines The number of message lines or 0 for none. + * call to register_sensor_key. The specified message + * is an initial message to display at the upper left, + * typically a set of instructions, that remains displayed + * throughout the lifetime of the window. Please ensure that + * the message_lines array and its contained strings are not + * on the stack because they will be used later in a separate + * thread. + * @param message_lines The message lines. + * @param number_of_lines The number of lines. * @param tick_window_width The width of the tick window or 0 for none. * @param log_file If non-NULL, the name of a file to which to write logging messages. * @param log_level The level of log messages to redirect to the file. * The level should be one of LOG_LEVEL_ERROR, LOG_LEVEL_WARNING, - * LOG_LEVEL_INFO, LOG_LEVEL_LOG, or LOG_LEVEL_DEBUG. + * LOG_LEVEL_INFO, LOG_LEVEL_LOG, LOG_LEVEL_DEBUG, or LOG_LEVEL_ALL. + * @return 0 for success, error code for failure. */ int start_sensor_simulator( - char* message_lines[], + const char* message_lines[], int number_of_lines, int tick_window_width, char* log_file, @@ -93,7 +77,7 @@ void end_sensor_simulator(); * Place a tick (usually a single character) in the tick window. * @param character The tick character. */ -void show_tick(char* character); +void show_tick(const char* character); /** * Register a keyboard key to trigger the specified action. diff --git a/util/wave_file_reader.c b/util/wave_file_reader.c index 1bf4e9fe7..bb84bd120 100644 --- a/util/wave_file_reader.c +++ b/util/wave_file_reader.c @@ -1,35 +1,9 @@ /** * @file * @author Edward A. Lee - * - * @section LICENSE -Copyright (c) 2020, The University of California at Berkeley and TU Dresden - -Redistribution and use in source and binary forms, with or without modification, -are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY -EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL -THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, -PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS -INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, -STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF -THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - - * @section DESCRIPTION - * - * Utility functions for importing audio files with the - * wave audio format. This code has few dependencies, so - * it should run on just about any platform. + * @copyright (c) 2020-2023, The University of California at Berkeley + * License in [BSD 2-clause](https://github.com/lf-lang/reactor-c/blob/main/LICENSE.md) + * @brief Utility function for reading WAV audio files. * * See wave_file_reader.h for instructions. */ @@ -37,6 +11,7 @@ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include
- * target C { - * files: ["/lib/C/util/wave_file_reader.c", "/lib/C/util/wave_file_reader.h"] - * }; +target C { + files: [ + "/lib/c/reactor-c/util/wave_file_reader.c", + "/lib/c/reactor-c/util/wave_file_reader.h" + ], + cmake-include: [ + "/lib/c/reactor-c/util/wave_file_reader.cmake" + ] +} ** - * In addition, you need this in your Lingua Franca file: + * In addition, you need this in your Lingua Franca file or reactor: *
* preamble {= - * #include "wave_file_reader.c" + * #include "wave_file_reader.h" * =} **/