at_commands/lowlevel/
command.rs

1// Copyright 2020 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5//! This module contains an AST for AT commands.
6//!
7//! The format of of these is not specifed in any one place in the spec, but they are
8//! described thoughout HFP 1.8.
9
10use crate::lowlevel::arguments;
11use crate::lowlevel::write_to::WriteTo;
12use std::io;
13
14/// A single AT command.
15#[derive(Debug, Clone, PartialEq)]
16pub enum Command {
17    /// A command for executing some procedure or setting state.  They may have arguments.
18    /// For example `AT+EXAMPLE=1,2,3`.
19    Execute { name: String, is_extension: bool, arguments: arguments::DelimitedArguments },
20    /// A command for reading some state.  For example `AT+EXAMPLE?`.
21    Read { name: String, is_extension: bool },
22    /// A command for querying the capabilities of the remote side.  For example `AT+EXAMPLE=?`.
23    Test { name: String, is_extension: bool },
24}
25
26impl Command {
27    fn name(&self) -> &str {
28        match self {
29            Command::Execute { name, .. }
30            | Command::Read { name, .. }
31            | Command::Test { name, .. } => name,
32        }
33    }
34
35    fn is_extension(&self) -> bool {
36        match self {
37            Command::Execute { is_extension, .. }
38            | Command::Read { is_extension, .. }
39            | Command::Test { is_extension, .. } => *is_extension,
40        }
41    }
42}
43
44impl WriteTo for Command {
45    fn write_to<W: io::Write>(&self, sink: &mut W) -> io::Result<()> {
46        sink.write_all(b"AT")?;
47        if self.is_extension() {
48            sink.write_all(b"+")?
49        }
50        sink.write_all(self.name().as_bytes())?;
51        match self {
52            Command::Execute { arguments: args, .. } => args.write_to(sink)?,
53            Command::Read { .. } => sink.write_all(b"?")?,
54            Command::Test { .. } => sink.write_all(b"=?")?,
55        };
56        // Commands are terminated by CR.
57        sink.write_all(b"\r")
58    }
59}