1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#![doc(
    html_logo_url = "https://openstratos.org/wp-content/uploads/2017/05/OpenStratos-768x226.png",
    html_favicon_url = "https://openstratos.org/wp-content/uploads/2015/10/OpenStratos-mark.png",
    html_root_url = "https://openstratos.github.io/server-rs/"
)]

//! OpenStratos balloon software.
//!
//! This crate provides the functionality required to control a stratospheric balloon. It provides
//! several modules that can be enabled using cargo features, and it can be extended by adding more
//! modules.
//!
//! ## Example:
//!
//! If you for example want to use the GPS and the GSM, but no real-time telemetry or Raspberry Pi
//! camera, it's as simple as compiling the crate as follows:
//!
//! ```text
//! cargo build --no-default-features --features="gps fona"
//! ```
//!
//! Here, the `--no-default-features` is required since by default, GPS, GSM (Adafruit FONA),
//! Raspberry Pi camera and real-time transparent serial telemetry will be activated.
//!
//! ## Configuration
//!
//! OpenStratos is highly configurable. Please refer to the [`config`](config/index.html) module for
//! further information.
//!
//! ## Launcher
//!
//! The project has a launcher in `src/main.rs` and can be launched by running `cargo run`. More
//! information can be found in the [`launcher`](../launcher/index.html) crate.
//!
//! ## Simulation mode
//!
//! *In development…*

#![deny(clippy::all)]
#![forbid(anonymous_parameters)]
#![warn(clippy::pedantic)]
#![deny(
    variant_size_differences,
    unused_results,
    unused_qualifications,
    unused_import_braces,
    unsafe_code,
    trivial_numeric_casts,
    trivial_casts,
    missing_docs,
    missing_debug_implementations,
    missing_copy_implementations,
    box_pointers,
    unused_extern_crates
)]
// Removing some warnings
#![allow(unsafe_code, box_pointers, clippy::use_self)]

/// Configuration file.
pub const CONFIG_FILE: &str = "config.toml";
/// Last state file, in the `data` directory.
pub const STATE_FILE: &str = "last_state";

pub mod config;
pub mod error;
#[cfg(feature = "fona")]
pub mod fona;
#[cfg(feature = "gps")]
pub mod gps;
pub mod logic;
#[cfg(feature = "raspicam")]
pub mod raspicam;
#[cfg(feature = "telemetry")]
pub mod telemetry;

use std::fs;

use failure::{Error, ResultExt};

pub use crate::config::CONFIG;
use crate::logic::{MainLogic, State};

/// The main logic of the program.
pub fn run() -> Result<(), Error> {
    initialize_data_filesystem().context(error::Fs::DataInit)?;

    if let Some(_state) = State::get_last().context(error::LastState::Read)? {
        // TODO recover from last state and continue
        unimplemented!()
    } else {
        logic::init().context(error::Logic::Init)?.main_logic()
    }
}

/// Initializes the data file system for videos and images.
pub fn initialize_data_filesystem() -> Result<(), Error> {
    let video_path = CONFIG.data_dir().join("video");
    fs::create_dir_all(&video_path).context(error::Fs::DirectoryCreation { path: video_path })?;

    let img_path = CONFIG.data_dir().join("img");
    fs::create_dir_all(&img_path).context(error::Fs::DirectoryCreation { path: img_path })?;

    Ok(())
}

/// Generates a stack trace string of an error.
#[allow(clippy::use_debug)]
pub fn generate_error_string<S>(error: &Error, main_error: S) -> String
where
    S: AsRef<str>,
{
    let mut result = format!("{}:\n{}\n", main_error.as_ref(), error);

    for e in error.iter_causes() {
        result.push_str(&format!("\tcaused by: {}\n", e));
    }

    // TODO: print only on debug mode
    result.push_str(&format!("\tbacktrace: {:?}\n", error.backtrace()));

    result
}

/// Initializes all loggers.
pub fn init_loggers() -> Result<log4rs::Handle, Error> {
    use chrono::Utc;
    use log::LevelFilter;
    use log4rs::{
        append::{console::ConsoleAppender, file::FileAppender},
        config::{Appender, Config, Logger, Root},
        encode::pattern::PatternEncoder,
    };
    // Only required for GPS, FONA or telemetry
    #[cfg(any(feature = "gps", feature = "fona", feature = "telemetry"))]
    use log::Record;
    #[cfg(any(feature = "gps", feature = "fona", feature = "telemetry"))]
    use log4rs::filter::{threshold::ThresholdFilter, Filter, Response};

    /// Filter that filters all but debug records.
    #[cfg(any(feature = "gps", feature = "fona", feature = "telemetry"))]
    #[derive(Debug, Clone, Copy)]
    struct DebugFilter;
    #[cfg(any(feature = "gps", feature = "fona", feature = "telemetry"))]
    impl Filter for DebugFilter {
        fn filter(&self, record: &Record) -> Response {
            if record.level() == LevelFilter::Debug {
                Response::Neutral
            } else {
                Response::Reject
            }
        }
    }

    /// Filter that filters all but trace records.
    #[cfg(any(feature = "gps", feature = "fona", feature = "telemetry"))]
    #[derive(Debug, Clone, Copy)]
    struct TraceFilter;
    #[cfg(any(feature = "gps", feature = "fona", feature = "telemetry"))]
    impl Filter for TraceFilter {
        fn filter(&self, record: &Record) -> Response {
            if record.level() == LevelFilter::Trace {
                Response::Neutral
            } else {
                Response::Reject
            }
        }
    }

    let now = Utc::now().format("%Y-%m-%d-%H-%M-%S");
    let pattern_naive = "[{d(%Y-%m-%d %H:%M:%S %Z)(utc)}][{l}] - {m}{n}";

    // Only required for GPS, FONA or telemetry
    #[cfg(any(feature = "gps", feature = "fona", feature = "telemetry"))]
    let pattern_exact = "[{d(%Y-%m-%d %H:%M:%S%.3f %Z)(utc)}][{l}] - {m}{n}";

    let stdout = ConsoleAppender::builder().build();
    let main = FileAppender::builder()
        .encoder(Box::new(PatternEncoder::new(pattern_naive)))
        .build(format!("data/logs/main-{}.log", now))
        .context(error::Log::Appender { name: "main" })?;
    let system = FileAppender::builder()
        .encoder(Box::new(PatternEncoder::new(pattern_naive)))
        .build(format!("data/logs/system-{}.log", now))
        .context(error::Log::Appender { name: "system" })?;

    // Only required for GPS, FONA or telemetry
    #[cfg(any(feature = "gps", feature = "fona", feature = "telemetry"))]
    let log_level = if CONFIG.debug() {
        LevelFilter::Trace
    } else {
        LevelFilter::Debug
    };

    let config = Config::builder()
        // Appenders
        .appender(Appender::builder().build("stdout", Box::new(stdout)))
        .appender(Appender::builder().build("main", Box::new(main)))
        .appender(Appender::builder().build("system", Box::new(system)))
        // Loggers
        .logger(
            Logger::builder()
                .appender("system")
                .additive(false)
                .build("system", LevelFilter::Info),
        );

    #[cfg(feature = "raspicam")]
    let config = {
        let camera = FileAppender::builder()
            .encoder(Box::new(PatternEncoder::new(pattern_naive)))
            .build(format!("data/logs/camera-{}.log", now))
            .context(error::Log::Appender { name: "camera" })?;

        config
            .appender(Appender::builder().build("camera", Box::new(camera)))
            .logger(
                Logger::builder()
                    .appender("camera")
                    .additive(false)
                    .build("os_balloon::camera", LevelFilter::Info),
            )
    };

    #[cfg(feature = "gps")]
    let config = {
        let gps = FileAppender::builder()
            .encoder(Box::new(PatternEncoder::new(pattern_naive)))
            .build(format!("data/logs/gps-{}.log", now))
            .context(error::Log::Appender {
                name: "os_balloon::gps",
            })?;
        let gps_frames = FileAppender::builder()
            .encoder(Box::new(PatternEncoder::new(pattern_exact)))
            .build(format!("data/logs/gps_frames-{}.log", now))
            .context(error::Log::Appender {
                name: "os_balloon::gps_frames",
            })?;
        let gps_logger = {
            let mut builder = Logger::builder()
                .appender("gps")
                .appender("gps_frames")
                .additive(false);
            if CONFIG.debug() {
                builder = builder.appender("gps_serial");
            }
            builder.build("os_balloon::gps", log_level)
        };

        let config = config
            .appender(
                Appender::builder()
                    .filter(Box::new(ThresholdFilter::new(LevelFilter::Info)))
                    .build("gps", Box::new(gps)),
            )
            .appender(
                Appender::builder()
                    .filter(Box::new(DebugFilter))
                    .build("gps_frames", Box::new(gps_frames)),
            )
            .logger(gps_logger);

        if CONFIG.debug() {
            let gps_serial = FileAppender::builder()
                .encoder(Box::new(PatternEncoder::new(pattern_exact)))
                .build(format!("data/logs/gps_serial-{}.log", now))
                .context(error::Log::Appender { name: "gps_serial" })?;
            config.appender(
                Appender::builder()
                    .filter(Box::new(TraceFilter))
                    .build("gps_serial", Box::new(gps_serial)),
            )
        } else {
            config
        }
    };

    #[cfg(feature = "fona")]
    let config = {
        let fona = FileAppender::builder()
            .encoder(Box::new(PatternEncoder::new(pattern_naive)))
            .build(format!("data/logs/fona-{}.log", now))
            .context(error::Log::Appender {
                name: "os_balloon::fona",
            })?;
        let fona_frames = FileAppender::builder()
            .encoder(Box::new(PatternEncoder::new(pattern_exact)))
            .build(format!("data/logs/fona_frames-{}.log", now))
            .context(error::Log::Appender {
                name: "os_balloon::fona_frames",
            })?;
        let fona_logger = {
            let mut builder = Logger::builder()
                .appender("fona")
                .appender("fona_frames")
                .additive(false);
            if CONFIG.debug() {
                builder = builder.appender("fona_serial");
            }
            builder.build("os_balloon::fona", log_level)
        };
        let config = config
            .appender(
                Appender::builder()
                    .filter(Box::new(ThresholdFilter::new(LevelFilter::Info)))
                    .build("fona", Box::new(fona)),
            )
            .appender(
                Appender::builder()
                    .filter(Box::new(DebugFilter))
                    .build("fona_frames", Box::new(fona_frames)),
            )
            .logger(fona_logger);

        if CONFIG.debug() {
            let gsm_serial = FileAppender::builder()
                .encoder(Box::new(PatternEncoder::new(pattern_exact)))
                .build(format!("data/logs/fona_serial-{}.log", now))
                .context(error::Log::Appender {
                    name: "fona_serial",
                })?;
            config.appender(
                Appender::builder()
                    .filter(Box::new(TraceFilter))
                    .build("fona_serial", Box::new(gsm_serial)),
            )
        } else {
            config
        }
    };

    #[cfg(feature = "telemetry")]
    let config = {
        let telemetry = FileAppender::builder()
            .encoder(Box::new(PatternEncoder::new(pattern_exact)))
            .build(format!("data/logs/telemetry-{}.log", now))
            .context(error::Log::Appender { name: "telemetry" })?;
        let telemetry_frames = FileAppender::builder()
            .encoder(Box::new(PatternEncoder::new(pattern_exact)))
            .build(format!("data/logs/telemetry_frames-{}.log", now))
            .context(error::Log::Appender {
                name: "telemetry_frames",
            })?;

        let telemetry_logger = {
            let mut builder = Logger::builder()
                .appender("telemetry")
                .appender("telemetry_frames")
                .additive(false);
            if CONFIG.debug() {
                builder = builder.appender("telemetry_serial");
            }
            builder.build("os_balloon::telemetry", log_level)
        };

        let config = config
            .appender(
                Appender::builder()
                    .filter(Box::new(ThresholdFilter::new(LevelFilter::Info)))
                    .build("telemetry", Box::new(telemetry)),
            )
            .appender(
                Appender::builder()
                    .filter(Box::new(DebugFilter))
                    .build("telemetry_frames", Box::new(telemetry_frames)),
            )
            .logger(telemetry_logger);

        if CONFIG.debug() {
            let telemetry_serial = FileAppender::builder()
                .encoder(Box::new(PatternEncoder::new(pattern_exact)))
                .build(format!("data/logs/telemetry_serial-{}.log", now))
                .context(error::Log::Appender {
                    name: "telemetry_serial",
                })?;

            config.appender(
                Appender::builder()
                    .filter(Box::new(TraceFilter))
                    .build("telemetry_serial", Box::new(telemetry_serial)),
            )
        } else {
            config
        }
    };

    let config = config
        .build(
            Root::builder()
                .appender("stdout")
                .appender("main")
                .build(LevelFilter::Info),
        )
        .context(error::Log::Build)?;

    Ok(log4rs::init_config(config)?)
}