1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "bluetooth_host_dumper.h"
17 
18 #include "bluetooth_host_server.h"
19 #include "bluetooth_log.h"
20 
21 namespace OHOS {
22 namespace Bluetooth {
23 namespace {
24 constexpr size_t MIN_ARGS_SIZE = 1;
25 const std::string ARGS_HELP = "-h";
26 const std::string ARGS_BR = "-br";
27 }
28 
BluetoothDump(const std::vector<std::string> & args,std::string & result)29 void BluetoothHostDumper::BluetoothDump(const std::vector<std::string>& args, std::string& result)
30 {
31     result.clear();
32     if (args.size() < MIN_ARGS_SIZE) {
33         ShowDumpHelp(result);
34         return;
35     }
36 
37     if (args.size() == MIN_ARGS_SIZE) {
38         // -h
39         if (args[0] == ARGS_HELP) {
40             ShowDumpHelp(result);
41             return;
42         }
43         // -br
44         if (args[0] == ARGS_BR) {
45             BtCommStateDump(result);
46             return;
47         }
48     }
49     IllegalDumpInput(result);
50 }
51 
ShowDumpHelp(std::string & result)52 void BluetoothHostDumper::ShowDumpHelp(std::string& result)
53 {
54     result.append("Bluetooth Dump options:\n")
55         .append("[-h]: show cmd help.\n")
56         .append("[-br]: show common state.\n");
57 }
58 
BtCommStateDump(std::string & result)59 void BluetoothHostDumper::BtCommStateDump(std::string& result)
60 {
61     sptr<BluetoothHostServer> hostServer = BluetoothHostServer::GetInstance();
62     // bt conn state
63     std::string btState = hostServer->IsBrEnabled() ? "enabled\n" : "disabled\n";
64     result.append("Bt enable state: ").append(btState);
65 
66     // ble conn state
67     std::string bleState = hostServer->IsBleEnabled() ? "enabled\n" : "disabled\n";
68     result.append("Ble enable state: ").append(bleState);
69 }
70 
IllegalDumpInput(std::string & result)71 void BluetoothHostDumper::IllegalDumpInput(std::string& result)
72 {
73     result.append("The dump args are illegal and you can enter '-h' for help.\n");
74 }
75 }  // namespace Bluetooth
76 }  // namespace OHOS
77