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

//! A high level interface for interacting with radios

#![deny(missing_docs)]

#[macro_use]
extern crate failure;
extern crate nom;

use nom::IResult;
use std::cell::RefCell;

use failure::Error;

/// Common Error for Radio Actions
#[derive(Debug, Fail)]
pub enum RadioError {
    #[fail(display = "Parse error: {}", message)]
    /// There was a problem parsing the result data
    ParseError {
        /// The message from original error
        message: String,
    },
}

fn nom_to_radio_error<T>(err: nom::Err<&[u8]>) -> Result<(&[u8], T), RadioError> {
    Err(match err {
        nom::Err::Error(nom::simple_errors::Context::Code(_, e)) => RadioError::ParseError {
            message: e.description().to_string(),
        },
        nom::Err::Failure(nom::simple_errors::Context::Code(_, e)) => RadioError::ParseError {
            message: e.description().to_string(),
        },
        nom::Err::Incomplete(_) => RadioError::ParseError {
            message: "Incomplete data".to_string(),
        },
    })
}

/// Custom error type for radio operations.
pub type RadioResult<T> = Result<T, Error>;

/// The signature of parse functions used in Connection read calls.
pub type ParseFn<T> = fn(input: &[u8]) -> IResult<&[u8], T>;

/// Connections expect a struct instance with this trait to represent streams.
pub trait Stream {
    /// Write raw bytes to the stream.
    fn write(&self, data: &[u8]) -> RadioResult<()>;
    /// Read raw bytes from the stream.
    fn read(&self) -> RadioResult<Vec<u8>>;
}

/// A connection is like a stream, but allowed parsed reads with properly buffered
/// input data.
pub struct Connection {
    stream: Box<Stream>,
    buffer: RefCell<Vec<u8>>,
}

impl Connection {
    /// Convenience constructor to create connection from stream.
    pub fn new(stream: Box<Stream>) -> Connection {
        Connection {
            stream,
            buffer: RefCell::new(Vec::new()),
        }
    }

    /// Write out raw bytes to the underlying stream.
    pub fn write(&self, data: &[u8]) -> RadioResult<()> {
        self.stream.write(data)
    }

    /// Read the next object using provided parser.
    pub fn read<T>(&self, parse: ParseFn<T>) -> RadioResult<T> {
        let mut buffer = self.buffer.borrow_mut();
        loop {
            let copy = buffer.clone();
            let res = parse(&copy);

            if let Err(nom::Err::Incomplete(_)) = res {
                let more = self.stream.read()?;
                buffer.extend_from_slice(&more);
                continue;
            }

            let (extra, value) = res.or_else(nom_to_radio_error)?;
            buffer.clear();
            buffer.extend_from_slice(extra);
            return Ok(value);
        }
    }
}