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
use std::io;
#[cfg(any(feature = "fona", feature = "raspicam"))]
use std::time::Duration;
#[cfg(feature = "fona")]
use std::thread;
#[cfg(feature = "no_power_off")]
use std::process;
#[cfg(any(feature = "gps", feature = "fona", feature = "raspicam"))]
use failure::ResultExt;
use log::{error, info};
#[cfg(any(feature = "gps", feature = "fona", feature = "raspicam"))]
use super::error as crate_error;
#[cfg(feature = "gps")]
use super::AcquiringFix;
#[cfg(not(feature = "gps"))]
use super::EternalLoop;
use super::{Error, Init, OpenStratos, StateMachine, CONFIG};
#[cfg(feature = "fona")]
use crate::fona::FONA;
#[cfg(feature = "gps")]
use crate::gps::GPS;
#[cfg(feature = "raspicam")]
use crate::raspicam::VIDEO_DIR;
#[cfg(feature = "raspicam")]
pub const TEST_VIDEO_FILE: &str = "test.h264";
impl StateMachine for OpenStratos<Init> {
#[cfg(feature = "gps")]
type Next = OpenStratos<AcquiringFix>;
#[cfg(not(feature = "gps"))]
type Next = OpenStratos<EternalLoop>;
#[allow(clippy::block_in_if_condition_expr)]
fn execute(self) -> Result<Self::Next, Error> {
check_disk_space()?;
#[cfg(feature = "gps")]
{
if let Err(e) = initialize_gps() {
return Err(e);
}
}
#[cfg(feature = "fona")]
{
if let Err(e) = initialize_fona() {
return Err(e);
}
}
#[cfg(feature = "raspicam")]
{
if let Err(e) = test_raspicam() {
return Err(e);
}
}
#[cfg(feature = "gps")]
{
Ok(OpenStratos {
state: AcquiringFix,
})
}
#[cfg(not(feature = "gps"))]
{
Ok(OpenStratos { state: EternalLoop })
}
}
}
fn check_disk_space() -> Result<(), Error> {
let disk_space = get_available_disk_space()?;
#[allow(clippy::cast_precision_loss)]
{
info!(
"Available disk space: {:.2} GiB",
disk_space as f32 / 1024_f32 / 1024_f32 / 1024_f32
);
}
#[cfg(feature = "raspicam")]
info!(
"Disk space enough for about {} minutes of fullHD video.",
CONFIG.video().bitrate() / (8 * 60)
);
#[cfg(feature = "raspicam")]
let enough_space = disk_space
> u64::from(CONFIG.flight().length()) * 6 * 60 * u64::from(CONFIG.video().bitrate())
/ (8 * 5);
#[cfg(not(feature = "raspicam"))]
let enough_space = disk_space > 2 * 1024 * 1024 * 1024;
if !enough_space {
error!("Not enough disk space.");
#[cfg(not(feature = "no_power_off"))]
power_off()?;
#[cfg(feature = "no_power_off")]
process::exit(1);
}
Ok(())
}
#[cfg(feature = "gps")]
fn initialize_gps() -> Result<(), Error> {
info!("Initializing GPS\u{2026}");
match GPS.lock() {
Ok(mut gps) => gps.initialize().context(crate_error::Init::Gps)?,
Err(poisoned) => {
error!("The GPS mutex was poisoned.");
poisoned
.into_inner()
.initialize()
.context(crate_error::Init::Gps)?
}
}
info!("GPS initialized.");
Ok(())
}
#[cfg(feature = "fona")]
fn initialize_fona() -> Result<(), Error> {
info!("Initializing Adafruit FONA GSM module\u{2026}");
match FONA.lock() {
Ok(mut fona) => fona.initialize().context(crate_error::Init::Fona)?,
Err(poisoned) => {
error!("The FONA mutex was poisoned.");
poisoned
.into_inner()
.initialize()
.context(crate_error::Init::Fona)?
}
}
info!("Adafruit FONA GSM module initialized.");
check_batteries()?;
info!("Waiting for GSM connectivity\u{2026}");
while {
match FONA.lock() {
Ok(mut fona) => !fona
.has_connectivity()
.context(crate_error::Init::CheckGsmConnectivity)?,
Err(poisoned) => {
error!("The FONA mutex was poisoned.");
!poisoned
.into_inner()
.has_connectivity()
.context(crate_error::Init::CheckGsmConnectivity)?
}
}
} {
thread::sleep(Duration::from_secs(1));
}
info!("GSM connected.");
Ok(())
}
#[cfg(feature = "fona")]
fn check_batteries() -> Result<(), Error> {
info!("Checking batteries\u{2026}");
let fona_bat_percent = match FONA.lock() {
Ok(mut fona) => fona
.battery_percent()
.context(crate_error::Init::CheckBatteries)?,
Err(poisoned) => {
error!("The FONA mutex was poisoned.");
poisoned
.into_inner()
.battery_percent()
.context(crate_error::Init::CheckBatteries)?
}
};
let adc_voltage = match FONA.lock() {
Ok(mut fona) => fona
.adc_voltage()
.context(crate_error::Init::CheckBatteries)?,
Err(poisoned) => {
error!("The FONA mutex was poisoned.");
poisoned
.into_inner()
.adc_voltage()
.context(crate_error::Init::CheckBatteries)?
}
};
let main_bat_percent = (adc_voltage - CONFIG.battery().main_min())
/ (CONFIG.battery().main_max() - CONFIG.battery().main_min());
info!(
"Batteries checked => Main battery: {} - GSM battery: {}",
if main_bat_percent > -1_f32 {
format!("{}%", main_bat_percent * 100_f32)
} else {
"disconnected".to_owned()
},
if fona_bat_percent > -1_f32 {
format!("{}%", fona_bat_percent * 100_f32)
} else {
"disconnected".to_owned()
}
);
if (main_bat_percent < CONFIG.battery().main_min_percent() && main_bat_percent > -1_f32)
|| fona_bat_percent < CONFIG.battery().fona_min_percent()
{
error!("Not enough battery.");
Err(crate_error::Init::NotEnoughBattery.into())
} else {
Ok(())
}
}
#[cfg(feature = "raspicam")]
fn test_raspicam() -> Result<(), Error> {
use std::fs::remove_file;
use crate::raspicam::CAMERA;
info!("Testing camera recording\u{2026}");
info!("Recording 10 seconds as test\u{2026}");
match CAMERA.lock() {
Ok(mut cam) => cam
.record(Duration::from_secs(10), TEST_VIDEO_FILE)
.context(crate_error::Raspicam::Test)?,
Err(poisoned) => {
error!("The CAMERA mutex was poisoned.");
poisoned
.into_inner()
.record(Duration::from_secs(10), TEST_VIDEO_FILE)
.context(crate_error::Raspicam::Test)?
}
}
let video_path = CONFIG.data_dir().join(VIDEO_DIR).join(TEST_VIDEO_FILE);
if video_path.exists() {
info!("Camera test OK.");
info!("Removing test file\u{2026}");
remove_file(&video_path).context(crate_error::Raspicam::TestRemove {
test_file: video_path.clone(),
})?;
info!("Test file removed.");
} else {
error!("Camera test file was not created.");
#[cfg(not(feature = "no_power_off"))]
power_off()?;
#[cfg(feature = "no_power_off")]
process::exit(1);
}
Ok(())
}
fn get_available_disk_space() -> Result<u64, Error> {
use std::{ffi::CString, mem, os::unix::ffi::OsStrExt};
let dir = CString::new(CONFIG.data_dir().as_os_str().as_bytes())?;
let mut stats: libc::statvfs;
let res = unsafe {
stats = mem::uninitialized();
libc::statvfs(dir.as_ptr(), &mut stats)
};
if res == 0 {
Ok(stats.f_bsize * stats.f_bavail)
} else {
Err(io::Error::last_os_error().into())
}
}
#[cfg(not(feature = "no_power_off"))]
fn power_off() -> Result<(), io::Error> {
use libc::{reboot, sync, RB_POWER_OFF};
unsafe {
sync();
}
if unsafe { reboot(RB_POWER_OFF) } == -1 {
Err(io::Error::last_os_error())
} else {
Ok(())
}
}