Files
adcs_api
cbor_protocol
channel_protocol
clyde_3g_eps_api
clyde_3g_eps_service
comms_service
db_test
eps_api
example_rust_c_service
example_rust_service
extern_lib
file_protocol
file_service
iobc_supervisor_service
isis_ants
isis_ants_api
isis_ants_service
isis_imtq_api
isis_iobc_supervisor
kubos_app
kubos_app_service
kubos_build_helper
kubos_file_client
kubos_service
kubos_shell_client
kubos_system
kubos_telemetry_db
large_download
large_upload
local_comms_service
mai400
mai400_api
mai400_service
monitor_service
novatel_oem6_api
novatel_oem6_service
nsl_duplex_d2
nsl_duplex_d2_comms_service
obc_hs
radio_api
rust_i2c
rust_mission_app
rust_uart
scheduler_service
serial_comms_service
shell_protocol
shell_service
telemetry_service
uart_comms_client
udp_client
utils
  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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
//
// Copyright (C) 2018 Kubos Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License")
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

use isis_ants_api::*;
use log::*;

fn arm(ants: &AntS) -> u8 {
    match ants.arm() {
        Ok(()) => {
            info!("[Arm Test] Test completed successfully");
            0
        }
        Err(err) => {
            error!("[Arm Test] Failed to arm AntS: {}", err);
            1
        }
    }
}

fn disarm(ants: &AntS) -> u8 {
    match ants.disarm() {
        Ok(()) => {
            info!("[Disarm Test] Test completed successfully");
            0
        }
        Err(err) => {
            error!("[Disarm Test] Failed to disarm AntS: {}", err);
            1
        }
    }
}

fn configure(ants: &AntS) -> u8 {
    match ants.configure(KANTSController::Secondary) {
        Ok(()) => {
            info!("[Configure Test] Test completed successfully");
            0
        }
        Err(err) => {
            error!("[Configure Test] Failed to configure AntS: {}", err);
            1
        }
    }
}

fn deploy(ants: &AntS) -> u8 {
    match ants.deploy(KANTSAnt::Ant3, false, 10) {
        Ok(()) => {
            info!("[Deploy Test] Test completed successfully");
            0
        }
        Err(err) => {
            error!("[Deploy Test] Failed to deploy antenna 3: {}", err);
            1
        }
    }
}

fn deploy_override(ants: &AntS) -> u8 {
    match ants.deploy(KANTSAnt::Ant1, true, 10) {
        Ok(()) => {
            info!("[Deploy Override Test] Test completed successfully");
            0
        }
        Err(err) => {
            error!("[Deploy Override Test] Failed to deploy antenna 1: {}", err);
            1
        }
    }
}

fn auto_deploy(ants: &AntS) -> u8 {
    match ants.auto_deploy(2) {
        Ok(()) => {
            info!("[Auto-Deploy Test] Test completed successfully");
            0
        }
        Err(err) => {
            error!("[Auto-Deploy Test] Failed to auto-deploy antennas: {}", err);
            1
        }
    }
}

fn cancel_deploy(ants: &AntS) -> u8 {
    match ants.cancel_deploy() {
        Ok(()) => {
            info!("[Disarm Test] Test completed successfully");
            0
        }
        Err(err) => {
            error!("[Disarm Test] Failed to cancel AntS deployment: {}", err);
            1
        }
    }
}

fn passthrough(ants: &AntS) -> u8 {
    let tx: [u8; 1] = [0xC3];
    let mut rx: [u8; 2] = [0; 2];

    match ants.passthrough(&tx, &mut rx) {
        Ok(()) => {
            info!("[Passthrough Test] Result: {:?}", rx);
            0
        }
        Err(err) => {
            error!(
                "[Passthrough Test] Failed to read AntS deployment status: {}",
                err
            );
            1
        }
    }
}

fn reset(ants: &AntS) -> u8 {
    match ants.reset() {
        Ok(()) => {
            info!("[Reset Test] Test completed successfully");
            0
        }
        Err(err) => {
            error!("[Reset Test] Failed to reset AntS: {}", err);
            1
        }
    }
}

fn get_deploy(ants: &AntS) -> u8 {
    let deploy = match ants.get_deploy() {
        Ok(result) => result,
        Err(err) => {
            error!(
                "[Passthrough Test] Failed to read AntS deployment status: {}",
                err
            );
            return 1;
        }
    };

    info!("Antenna deployment status:");
    info!("    sys_burn_active: {}", deploy.sys_burn_active);
    info!("    sys_ignore_deploy: {}", deploy.sys_ignore_deploy);
    info!("    sys_armed: {}", deploy.sys_armed);
    info!("    ant_1_not_deployed: {}", deploy.ant_1_not_deployed);
    info!("    ant_1_stopped_time: {}", deploy.ant_1_stopped_time);
    info!("    ant_1_active: {}", deploy.ant_1_active);
    info!("    ant_2_not_deployed: {}", deploy.ant_2_not_deployed);
    info!("    ant_2_stopped_time: {}", deploy.ant_2_stopped_time);
    info!("    ant_2_active: {}", deploy.ant_2_active);
    info!("    ant_3_not_deployed: {}", deploy.ant_3_not_deployed);
    info!("    ant_3_stopped_time: {}", deploy.ant_3_stopped_time);
    info!("    ant_3_active: {}", deploy.ant_3_active);
    info!("    ant_4_not_deployed: {}", deploy.ant_4_not_deployed);
    info!("    ant_4_stopped_time: {}", deploy.ant_4_stopped_time);
    info!("    ant_4_active: {}", deploy.ant_4_active);

    if !deploy.sys_armed {
        error!("[Deploy Status Test] AntS not reporting as armed");
        return 1;
    }

    info!("[Deploy Status Test] Test completed successfully");
    0
}

fn get_sys_telem(ants: &AntS) -> u8 {
    let sys_telem = match ants.get_system_telemetry() {
        Ok(result) => result,
        Err(err) => {
            error!(
                "[Passthrough Test] Failed to read AntS deployment status: {}",
                err
            );
            return 1;
        }
    };

    info!("Antenna system telemetry:");
    info!("    raw_temp: {}", sys_telem.raw_temp);
    info!("    deploy_status:");
    info!(
        "        sys_burn_active: {}",
        sys_telem.deploy_status.sys_burn_active
    );
    info!(
        "        sys_ignore_deploy: {}",
        sys_telem.deploy_status.sys_ignore_deploy
    );
    info!("        sys_armed: {}", sys_telem.deploy_status.sys_armed);
    info!(
        "        ant_1_not_deployed: {}",
        sys_telem.deploy_status.ant_1_not_deployed
    );
    info!(
        "        ant_1_stopped_time: {}",
        sys_telem.deploy_status.ant_1_stopped_time
    );
    info!(
        "        ant_1_active: {}",
        sys_telem.deploy_status.ant_1_active
    );
    info!(
        "        ant_2_not_deployed: {}",
        sys_telem.deploy_status.ant_2_not_deployed
    );
    info!(
        "        ant_2_stopped_time: {}",
        sys_telem.deploy_status.ant_2_stopped_time
    );
    info!(
        "        ant_2_active: {}",
        sys_telem.deploy_status.ant_2_active
    );
    info!(
        "        ant_3_not_deployed: {}",
        sys_telem.deploy_status.ant_3_not_deployed
    );
    info!(
        "        ant_3_stopped_time: {}",
        sys_telem.deploy_status.ant_3_stopped_time
    );
    info!(
        "        ant_3_active: {}",
        sys_telem.deploy_status.ant_3_active
    );
    info!(
        "        ant_4_not_deployed: {}",
        sys_telem.deploy_status.ant_4_not_deployed
    );
    info!(
        "        ant_4_stopped_time: {}",
        sys_telem.deploy_status.ant_4_stopped_time
    );
    info!(
        "        ant_4_active: {}",
        sys_telem.deploy_status.ant_4_active
    );
    info!("    uptime: {}", sys_telem.uptime);

    info!("[System Telemetry Test] Test completed successfully");

    0
}

fn get_act_counts(ants: &AntS) -> u8 {
    let act_count = match ants.get_activation_count(KANTSAnt::Ant1) {
        Ok(result) => result,
        Err(err) => {
            error!(
                "[Activation Count Test] Failed to get antenna 1's activation count: {}",
                err
            );
            return 1;
        }
    };

    info!("Antenna 1 activation count: {}", act_count);

    let act_count = match ants.get_activation_count(KANTSAnt::Ant2) {
        Ok(result) => result,
        Err(err) => {
            error!(
                "[Activation Count Test] Failed to get antenna 2's activation count: {}",
                err
            );
            return 1;
        }
    };

    info!("Antenna 2 activation count: {}", act_count);

    let act_count = match ants.get_activation_count(KANTSAnt::Ant3) {
        Ok(result) => result,
        Err(err) => {
            error!(
                "[Activation Count Test] Failed to get antenna 3's activation count: {}",
                err
            );
            return 1;
        }
    };

    info!("Antenna 3 activation count: {}", act_count);

    let act_count = match ants.get_activation_count(KANTSAnt::Ant4) {
        Ok(result) => result,
        Err(err) => {
            error!(
                "[Activation Count Test] Failed to get antenna 4's activation count: {}",
                err
            );
            return 1;
        }
    };

    info!("Antenna 4 activation count: {}", act_count);

    info!("[Activation Counts Test] Test completed successfully");

    0
}

fn get_act_times(ants: &AntS) -> u8 {
    let act_time = match ants.get_activation_time(KANTSAnt::Ant1) {
        Ok(result) => result,
        Err(err) => {
            error!(
                "[Activation Time Test] Failed to get antenna 1's activation time: {}",
                err
            );
            return 1;
        }
    };

    info!("Antenna 1 activation time: {}", act_time);

    let act_time = match ants.get_activation_time(KANTSAnt::Ant2) {
        Ok(result) => result,
        Err(err) => {
            error!(
                "[Activation Time Test] Failed to get antenna 2's activation time: {}",
                err
            );
            return 1;
        }
    };

    info!("Antenna 2 activation time: {}", act_time);

    let act_time = match ants.get_activation_time(KANTSAnt::Ant3) {
        Ok(result) => result,
        Err(err) => {
            error!(
                "[Activation Time Test] Failed to get antenna 3's activation time: {}",
                err
            );
            return 1;
        }
    };

    info!("Antenna 3 activation time: {}", act_time);

    let act_time = match ants.get_activation_time(KANTSAnt::Ant4) {
        Ok(result) => result,
        Err(err) => {
            error!(
                "[Activation Time Test] Failed to get antenna 4's activation time: {}",
                err
            );
            return 1;
        }
    };

    info!("Antenna 4 activation time: {}", act_time);

    info!("[Activation Times Test] Test completed successfully");

    0
}

fn get_uptime(ants: &AntS) -> u8 {
    let uptime = match ants.get_uptime() {
        Ok(result) => result,
        Err(err) => {
            error!(
                "[Passthrough Test] Failed to read AntS deployment status: {}",
                err
            );
            return 1;
        }
    };

    info!("System uptime: {}", uptime);
    info!("[Uptime Test] Test completed successfully");

    0
}

fn watchdog_kick(ants: &AntS) -> u8 {
    match ants.watchdog_kick() {
        Ok(()) => {
            info!("[WD Test] Test completed successfully");
            0
        }
        Err(err) => {
            error!("[WD Test] Failed to kick WD: {}", err);
            1
        }
    }
}

pub fn main() {
    let mut error_count: u8 = 0;

    use log4rs::append::console::ConsoleAppender;
    use log4rs::encode::pattern::PatternEncoder;
    use log4rs_syslog::SyslogAppender;
    // Use custom PatternEncoder to avoid duplicate timestamps in logs.
    let syslog_encoder = Box::new(PatternEncoder::new("{m}"));
    // Set up logging which will be routed to syslog for processing
    let syslog = Box::new(
        SyslogAppender::builder()
            .encoder(syslog_encoder)
            .openlog(
                "isis-ants",
                log4rs_syslog::LogOption::LOG_PID | log4rs_syslog::LogOption::LOG_CONS,
                log4rs_syslog::Facility::User,
            )
            .build(),
    );

    // Set up logging which will be routed to stdout
    let stdout = Box::new(ConsoleAppender::builder().build());

    // Combine the loggers into one master config
    let config = log4rs::config::Config::builder()
        .appender(log4rs::config::Appender::builder().build("syslog", syslog))
        .appender(log4rs::config::Appender::builder().build("stdout", stdout))
        .build(
            log4rs::config::Root::builder()
                .appender("syslog")
                .appender("stdout")
                // Set the minimum logging level to record
                .build(log::LevelFilter::Debug),
        )
        .unwrap();

    // Start the logger
    log4rs::init_config(config).unwrap();

    // Initialize connection with the antenna system
    let ants = match AntS::new("/dev/i2c-0", 0x31, 0x32, 4, 10) {
        Ok(result) => result,
        Err(err) => {
            error!("Failed to init connection: {}", err);
            panic!("Unable to connect to AntS. Cancelling test");
        }
    };

    info!("ISIS AntS Integration Tests");

    error_count += arm(&ants);
    error_count += disarm(&ants);
    error_count += configure(&ants);

    // Prep for deployment tests
    if ants.arm().is_err() {
        error_count += 1;
        error!("Failed to arm AntS");
    }

    error_count += deploy(&ants);
    error_count += deploy_override(&ants);
    error_count += auto_deploy(&ants);
    error_count += cancel_deploy(&ants);
    error_count += passthrough(&ants);
    error_count += get_deploy(&ants);
    error_count += get_sys_telem(&ants);
    error_count += get_act_counts(&ants);
    error_count += get_act_times(&ants);
    error_count += get_uptime(&ants);

    error_count += reset(&ants);
    error_count += watchdog_kick(&ants);

    info!("ISIS AntS Integration Tests Complete");

    if error_count == 0 {
        // Since we don't have the logger set up to print to stdout,
        // we need a manual println call here
        println!("AntS tests completed successfully");
        info!("AntS tests completed successfully");
    } else {
        eprintln!("One or more AntS tests have failed. See ant-results.txt for info");
        warn!("One or more AntS tests have failed");
    }
}