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