1//
2// Copyright 2015 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#pragma once
18
19#include <cstdint>
20#include <vector>
21
22#include "base/macros.h"
23#include "packet.h"
24
25namespace test_vendor_lib {
26
27// The following is specified in the Bluetooth Core Specification Version 4.2,
28// Volume 2, Part E, Section 5.4.1 (page 470). Command Packets begin with a 3
29// octet header formatted as follows:
30// - Opcode: 2 octets
31//   - Opcode Group Field (OGF): Upper bits 10-15
32//   - Opcode Command Field (OCF): Lower bits 0-9
33// - Payload size (in octets): 1 octet
34// The header is followed by the payload, which contains command specific
35// parameters and has a maximum size of 255 octets. Valid command opcodes are
36// defined in stack/include/hcidefs.h. The OGF ranges from 0x00 to 0x3F, with
37// 0x3F reserved for vendor-specific debug functions. The OCF ranges from
38// 0x0000 to 0x03FF. Note that the payload size is the size in octets of the
39// command parameters and not the number of parameters. Finally, although the
40// parameters contained in the payload are command specific (including the size
41// and number of parameters), each parameter will be an integer number of octets
42// in size.
43class CommandPacket : public Packet {
44 public:
45  explicit CommandPacket(std::vector<uint8_t> header);
46  explicit CommandPacket(uint16_t opcode);
47  CommandPacket(std::vector<uint8_t> header, std::vector<uint8_t> payload);
48
49  CommandPacket(const CommandPacket&) = default;
50  CommandPacket& operator=(const CommandPacket&) = default;
51  CommandPacket(CommandPacket&&) = default;
52  CommandPacket& operator=(CommandPacket&&) = default;
53  virtual ~CommandPacket() override = default;
54
55  // Returns the command opcode as defined in stack/include/hcidefs.h.
56  // See the Bluetooth Core Specification Version 4.2, Volume 2, Part E,
57  // Section 7 for more information about each HCI commands and for a listing
58  // of their specific opcodes/OGF and OCF values.
59  uint16_t GetOpcode() const;
60
61  // Returns the 6 bit opcode group field that specifies the general category of
62  // the command. The OGF can be one of seven values:
63  // - 0x01: Link control commands
64  // - 0x02: Link policy commands
65  // - 0x03: Controller and baseband commands
66  // - 0x04: Informational parameters commands
67  // - 0x05: Status parameters commands
68  // - 0x06: Testing commands
69  // - 0x08: Low energy controller commands
70  // The upper 2 bits will be zero filled.
71  uint8_t GetOGF() const;
72
73  // Returns the 10 bit opcode command field that specifies an exact command
74  // within an opcode group field. The upper 6 bits will be zero filled.
75  uint16_t GetOCF() const;
76
77  // Size of a command packet header, which consists of a 2 octet opcode
78  static const size_t kCommandHeaderSize = 2;
79};
80
81}  // namespace test_vendor_lib
82