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.
45//! 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.
910use crate::lowlevel::arguments;
11use crate::lowlevel::write_to::WriteTo;
12use std::io;
1314/// 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`.
19Execute { name: String, is_extension: bool, arguments: arguments::DelimitedArguments },
20/// A command for reading some state. For example `AT+EXAMPLE?`.
21Read { name: String, is_extension: bool },
22/// A command for querying the capabilities of the remote side. For example `AT+EXAMPLE=?`.
23Test { name: String, is_extension: bool },
24}
2526impl Command {
27fn name(&self) -> &str {
28match self {
29 Command::Execute { name, .. }
30 | Command::Read { name, .. }
31 | Command::Test { name, .. } => name,
32 }
33 }
3435fn is_extension(&self) -> bool {
36match self {
37 Command::Execute { is_extension, .. }
38 | Command::Read { is_extension, .. }
39 | Command::Test { is_extension, .. } => *is_extension,
40 }
41 }
42}
4344impl WriteTo for Command {
45fn write_to<W: io::Write>(&self, sink: &mut W) -> io::Result<()> {
46 sink.write_all(b"AT")?;
47if self.is_extension() {
48 sink.write_all(b"+")?
49}
50 sink.write_all(self.name().as_bytes())?;
51match 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.
57sink.write_all(b"\r")
58 }
59}