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
//
// 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.
//

//! Kubos Service for interacting with [ISIS Antenna Systems](https://www.isispace.nl/product-category/products/antenna-systems/)
//!
//! # Configuration
//!
//! The service must be configured in `/home/system/etc/config.toml` with the following fields:
//!
//! - `[isis-ants-service.addr]`
//!
//!     - `ip` - Specifies the service's IP address
//!     - `port` - Specifies the port on which the service will be listening for UDP packets
//!
//! - `[isis-ants-service]`
//!
//!     - `bus` - Specifies the I2C bus the antenna system is connected to
//! 	- `primary` - Specifies the I2C address of the antenna system's primary microcontroller
//! 	- `secondary` - Specifies the I2C address of the secondary microcontroller. If no secondary contoller is present, this value should be `"0x00"`.
//! 	- `antennas` - Specifies the number of antennas present in the system. Expected value: 2 or 4.
//! 	- `wd_timeout` - Specifies the interval at which the AntS watchdog should be automatically kicked. To disable automatic kicking, this value should be `0`.
//!
//! For example:
//!
//! ```toml
//! [isis-ants-service.addr]
//! ip = "0.0.0.0"
//! port = 8006
//!
//! [isis-ants-service]
//! bus = "/dev/i2c-0"
//! primary = "0x31"
//! secondary = "0x32"
//! antennas = 4
//! wd_timeout = 10
//! ```
//!
//! # Starting the Service
//!
//! The service should be started automatically by its init script, but may also be started manually:
//!
//! ```shell
//! $ isis-ants-service
//! Kubos antenna systems service started
//! Listening on: 0.0.0.0:8006
//! ```
//!
//! # Queries
//!
//! ## Ping
//!
//! Test query to verify service is running without attempting
//! to communicate with the underlying subsystem
//!
//! ```json
//! {
//!     ping: "pong"
//! }
//! ```
//!
//! ## ACK
//!
//! Get the last run mutation
//!
//! ```json
//! {
//!     ack: AckCommand
//! }
//! ```
//!
//! ## Errors
//!
//! Get all errors encountered since the last time this field was queried
//!
//! ```json
//! {
//!     errors: [String]
//! }
//! ```
//!
//! ## Power Status
//!
//! Get the current power state and uptime of the system
//!
//! ```json
//! {
//!     power {
//!         state: PowerState,
//!         uptime: Int
//!     }
//! }
//! ```
//!
//! ## Configuration
//!
//! Get the current microcontroller which commands will be sent to
//!
//! ```json
//! {
//!     config: ConfigureController
//! }
//! ```
//!
//! ## Telemetry
//!
//! Get current telemetry information for the system
//!
//! ```json
//! {
//!     telemetry {
//!         nominal {
//!             rawTemp: Int,
//!             uptime: Int,
//!             sysBurnActive: Boolean,
//!             sysIgnoreDeploy: Boolean,
//!             sysArmed: Boolean,
//!             ant1NotDeployed: Boolean,
//!             ant1StoppedTime: Boolean,
//!             ant1Active: Boolean,
//!             ant2NotDeployed: Boolean,
//!             ant2StoppedTime: Boolean,
//!             ant2Active: Boolean,
//!             ant3NotDeployed: Boolean,
//!             ant3StoppedTime: Boolean,
//!             ant3Active: Boolean,
//!             ant4NotDeployed: Boolean,
//!             ant4StoppedTime: Boolean,
//!             ant4Active: Boolean
//!         },
//!     	   debug {
//!             ant1ActivationCount: Int,
//!             ant1ActivationTime: Int,
//!             ant2ActivationCount: Int,
//!             ant2ActivationTime: Int,
//!             ant3ActivationCount: Int,
//!             ant3ActivationTime: Int,
//!             ant4ActivationCount: Int,
//!             ant4ActivationTime: Int,
//!         }
//!     }
//! }
//! ```
//!
//! ## Test Results
//!
//! Get the test results of the last run test
//!
//! Note: For this service, this actually just fetches the nominal
//! and debug telemetry of the system, since there is no actual
//! built-in test
//!
//! ```json
//! {
//!     testResults{
//!         success,
//!         telemetryNominal{...},
//!         telemetryDebug{...}
//!     }
//! }
//! ```
//!
//! ## System Armed Status
//!
//! Get the current armed/disarmed status of the system
//!
//! ```json
//! {
//!     armStatus: ArmStatus
//! }
//! ```
//!
//! ## System deployment status
//!
//! Get the current deployment status of the system
//!
//! ```json
//! {
//!     deploymentStatus {
//!         status: DeploymentStatus,
//!         sysBurnActive: Boolean,
//!         sysIgnoreDeploy: Boolean,
//!         sysArmed: Boolean,
//!         ant1NotDeployed: Boolean,
//!         ant1StoppedTime: Boolean,
//!         ant1Active: Boolean,
//!         ant2NotDeployed: Boolean,
//!         ant2StoppedTime: Boolean,
//!         ant2Active: Boolean,
//!         ant3NotDeployed: Boolean,
//!         ant3StoppedTime: Boolean,
//!         ant3Active: Boolean,
//!         ant4NotDeployed: Boolean,
//!         ant4StoppedTime: Boolean,
//!         ant4Active: Boolean
//! }
//! ```
//!
//! # Mutations
//!
//!
//! ## Errors
//!
//! Get all errors encountered while processing this GraphQL request
//!
//! Note: This will only return errors thrown by fields which have
//! already been processed, so it is recommended that this field be specified last.
//!
//! ```json
//! mutation {
//!     errors: [String]
//! }
//! ```
//!
//! ## No-Op
//!
//! Execute a trivial command against the system
//!
//! ```json
//! mutation {
//!     noop {
//!         errors: String,
//!         success: Boolean
//!    }
//! }
//! ```
//!
//! ## Set Power State
//!
//! Control the power state of the system
//!
//! - state: Power state the system should be changed to
//!   Note: The only valid input for this service is `RESET`
//!
//! ```json
//! mutation {
//!     controlPower(state: PowerState) {
//!         errors: String,
//!         success: Boolean,
//!         power: PowerState
//!     }
//! }
//! ```
//!
//! ## Configuration
//!
//! Configure the system
//!
//! - config: Set which microcontroller future commands should be issued from
//!
//! ```json
//! mutation {
//!     configureHardware(config: ConfigureController) {
//!         errors: String,
//!         success: Boolean,
//!         config: ConfigureController
//!    }
//! }
//! ```
//!
//! ## System Self-Test
//!
//! Run a system self-test
//!
//! - test: Type of self-test to perform
//!
//! ```json
//! mutation {
//!     testHardware(test: TestType) {
//!         ... on IntegrationTestResults {
//!             errors: String,
//!             success: Boolean,
//!             telemetryNominal{...},
//!             telemetryDebug{...}
//!         }
//!         ... on HardwareTestResults {
//!             errors: "Not Implemented",
//!             success: true,
//!             data: Empty
//!         }
//!    }
//! }
//! ```
//!
//! ## Passthrough
//!
//! Pass a custom command through to the system
//!
//! - command: String containing the hex values to be sent (ex. "C3")
//!   It will be converted to a byte array before transfer.
//! - rxLen: Number of response bytes to read
//!
//! ```json
//! mutation {
//!     issueRawCommand(command: String, rx_len: Int) {
//!         errors: String,
//!         success: Boolean,
//!         response: String
//!     }
//! }
//! ```
//!
//! ## Arm/Disarm
//!
//! Arm/Disarm the system
//!
//! - state: Armed/Disarmed state the system should be changed to
//!
//! ```json
//! mutation {
//!     arm(state: ArmState) {
//!         errors: String,
//!         success: Boolean
//!    }
//! ```
//!
//! ## Deploy Antennas
//!
//! Deploy antenna/s
//!
//! - ant: (Default - All) Antenna to deploy
//! - force: (Default - false) Whether current deployment state should be ignored/overridden
//! - time: Maximum amount of time to spend attempting to deploy the antenna
//!   (for 'All', this is the amount of time spent for each antenna)
//!
//! ```json
//! mutation {
//!     deploy(ant: DeployType = DeployType::All, force: Boolean = false, time: Int) {
//!         errors: String,
//!         success: Boolean
//!    }
//! }
//! ```
//!

#![deny(missing_docs)]
#![recursion_limit = "256"]

#[macro_use]
extern crate juniper;

use crate::model::Subsystem;
pub use crate::objects::*;
use crate::schema::{MutationRoot, QueryRoot};
use isis_ants_api::AntSResult;
use kubos_service::{Config, Service};
use syslog::Facility;

mod model;
mod objects;
mod schema;
#[cfg(test)]
mod tests;

fn main() -> AntSResult<()> {
    syslog::init(
        Facility::LOG_DAEMON,
        log::LevelFilter::Debug,
        Some("isis-ants-service"),
    )
    .unwrap();

    let config = Config::new("isis-ants-service");

    let bus = config
        .get("bus")
        .expect("No 'bus' value found in 'isis-ants-service' section of config");
    let bus = bus.as_str().unwrap();

    let primary = config
        .get("primary")
        .expect("No 'primary' value found in 'isis-ants-service' section of config");
    let primary = primary.as_str().unwrap();
    let primary: u8 = if primary.starts_with("0x") {
        u8::from_str_radix(&primary[2..], 16).unwrap()
    } else {
        u8::from_str_radix(primary, 16).unwrap()
    };

    let secondary = config
        .get("secondary")
        .expect("No 'secondary' value found in 'isis-ants-service' section of config");
    let secondary = secondary.as_str().unwrap();
    let secondary: u8 = if secondary.starts_with("0x") {
        u8::from_str_radix(&secondary[2..], 16).unwrap()
    } else {
        u8::from_str_radix(secondary, 16).unwrap()
    };

    let antennas = config
        .get("antennas")
        .expect("No 'antennas' value found in 'isis-ants-service' section of config");
    let antennas = antennas.as_integer().unwrap() as u8;

    let wd_timeout = config
        .get("wd_timeout")
        .expect("No 'wd_timeout' value found in 'isis-ants-service' section of config");
    let wd_timeout = wd_timeout.as_integer().unwrap() as u32;

    Service::new(
        config,
        Subsystem::new(bus, primary, secondary, antennas, wd_timeout)?,
        QueryRoot,
        MutationRoot,
    )
    .start();

    Ok(())
}