command_packet.h revision 99195bc17fe1f7c4a7e43590d9bce550b177a931
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>
21using std::vector;
22
23#include "base/macros.h"
24#include "vendor_libs/test_vendor_lib/include/packet.h"
25
26namespace test_vendor_lib {
27
28// The following is specified in the Bluetooth Core Specification Version 4.2,
29// Volume 2, Part E, Section 5.4.1 (page 470). Command Packets begin with a 3
30// octet header formatted as follows:
31// - Opcode: 2 octets
32//   - Opcode Group Field (OGF): Upper bits 10-15
33//   - Opcode Command Field (OCF): Lower bits 0-9
34// - Payload size (in octets): 1 octet
35// The header is followed by the payload, which contains command specific
36// parameters and has a maximum size of 255 octets. Valid command opcodes are
37// defined in stack/include/hcidefs.h. The OGF ranges from 0x00 to 0x3F, with
38// 0x3F reserved for vendor-specific debug functions. The OCF ranges from
39// 0x0000 to 0x03FF. Note that the payload size is the size in octets of the
40// command parameters and not the number of parameters. Finally, although the
41// parameters contained in the payload are command specific (including the size
42// and number of parameters), each parameter will be an integer number of octets
43// in size.
44class CommandPacket : public Packet {
45 public:
46  CommandPacket(vector<uint8_t> header);
47  CommandPacket(uint16_t opcode);
48  CommandPacket(vector<uint8_t> header, vector<uint8_t> payload);
49
50  virtual ~CommandPacket() override = default;
51
52  // Returns the command opcode as defined in stack/include/hcidefs.h.
53  // See the Bluetooth Core Specification Version 4.2, Volume 2, Part E,
54  // Section 7 for more information about each HCI commands and for a listing
55  // of their specific opcodes/OGF and OCF values.
56  uint16_t GetOpcode() const;
57
58  // Returns the 6 bit opcode group field that specifies the general category of
59  // the command. The OGF can be one of seven values:
60  // - 0x01: Link control commands
61  // - 0x02: Link policy commands
62  // - 0x03: Controller and baseband commands
63  // - 0x04: Informational parameters commands
64  // - 0x05: Status parameters commands
65  // - 0x06: Testing commands
66  // - 0x08: Low energy controller commands
67  // The upper 2 bits will be zero filled.
68  uint8_t GetOGF() const;
69
70  // Returns the 10 bit opcode command field that specifies an exact command
71  // within an opcode group field. The upper 6 bits will be zero filled.
72  uint16_t GetOCF() const;
73
74  // Size of a command packet header, which consists of a 2 octet opcode
75  static const size_t kCommandHeaderSize = 2;
76
77 private:
78  // Disallow any copies of the singleton to be made.
79  DISALLOW_COPY_AND_ASSIGN(CommandPacket);
80};
81
82}  // namespace test_vendor_lib
83