1 /*
2 * Copyright 2020 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 <bluetooth/log.h>
20
21 #include "hci/hci_packets.h"
22 #include "os/log.h"
23
24 namespace bluetooth {
25 namespace hci {
26
27 template <class T>
check_complete(CommandCompleteView view)28 void check_complete(CommandCompleteView view) {
29 log::assert_that(view.IsValid(), "assert failed: view.IsValid()");
30 auto status_view = T::Create(view);
31 if (!status_view.IsValid()) {
32 log::error("Invalid packet, opcode {}", OpCodeText(view.GetCommandOpCode()));
33 return;
34 }
35 ErrorCode status = status_view.GetStatus();
36 OpCode op_code = status_view.GetCommandOpCode();
37 if (status != ErrorCode::SUCCESS) {
38 log::error("Error code {}, opcode {}", ErrorCodeText(status), OpCodeText(op_code));
39 return;
40 }
41 }
42
43 template <class T>
check_status(CommandStatusView view)44 void check_status(CommandStatusView view) {
45 log::assert_that(view.IsValid(), "assert failed: view.IsValid()");
46 auto status_view = T::Create(view);
47 if (!status_view.IsValid()) {
48 log::error("Invalid packet, opcode {}", OpCodeText(view.GetCommandOpCode()));
49 return;
50 }
51 ErrorCode status = status_view.GetStatus();
52 OpCode op_code = status_view.GetCommandOpCode();
53 if (status != ErrorCode::SUCCESS) {
54 log::error("Error code {}, opcode {}", ErrorCodeText(status), OpCodeText(op_code));
55 return;
56 }
57 }
58
59 } // namespace hci
60 } // namespace bluetooth
61