1 /*
2  * Copyright (c) 2023 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 #include "edm_ipc_interface_code.h"
16 #include "get_bluetooth_info_plugin.h"
17 #include "parameters.h"
18 #include "string_serializer.h"
19 #include "plugin_manager.h"
20 
21 
22 namespace OHOS {
23 namespace EDM {
24 enum BluetoothState {
25     TURN_OFF,
26     TURNING_ON,
27     TURN_ON,
28     TURNING_OFF,
29 };
30 
31 enum BluetoothConnectionState {
32     DISCONNECTED,
33     CONNECTING,
34     CONNECTED,
35     DISCONNECTING,
36 };
37 
38 const bool REGISTER_RESULT = PluginManager::GetInstance()->AddPlugin(GetBluetoothInfoPlugin::GetPlugin());
39 
InitPlugin(std::shared_ptr<IPluginTemplate<GetBluetoothInfoPlugin,std::string>> ptr)40 void GetBluetoothInfoPlugin::InitPlugin(std::shared_ptr<IPluginTemplate<GetBluetoothInfoPlugin,
41     std::string>> ptr)
42 {
43     EDMLOGI("GetBluetoothInfoPlugin InitPlugin...");
44     ptr->InitAttribute(EdmInterfaceCode::GET_BLUETOOTH_INFO, "get_bluetooth_info",
45         "ohos.permission.ENTERPRISE_MANAGE_BLUETOOTH", IPlugin::PermissionType::SUPER_DEVICE_ADMIN, false);
46     ptr->SetSerializer(StringSerializer::GetInstance());
47 }
48 
TransformBluetoothState(int32_t state)49 int32_t GetBluetoothInfoPlugin::TransformBluetoothState(int32_t state)
50 {
51     int32_t realState;
52     switch (state) {
53         case static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURNING_ON):
54             realState = BluetoothState::TURNING_ON;
55             break;
56         case static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURN_ON):
57             realState = BluetoothState::TURN_ON;
58             break;
59         case static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURNING_OFF):
60             realState = BluetoothState::TURNING_OFF;
61             break;
62         case static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURN_OFF):
63             realState = BluetoothState::TURN_OFF;
64             break;
65         default:
66             realState = -1;
67     }
68     return realState;
69 }
70 
TransformBluetoothConnectionState(int32_t connectionState)71 int32_t GetBluetoothInfoPlugin::TransformBluetoothConnectionState(int32_t connectionState)
72 {
73     int32_t realConnectionState;
74     switch (connectionState) {
75         case static_cast<int32_t>(Bluetooth::BTConnectState::CONNECTING):
76             realConnectionState = BluetoothConnectionState::CONNECTING;
77             break;
78         case static_cast<int32_t>(Bluetooth::BTConnectState::CONNECTED):
79             realConnectionState = BluetoothConnectionState::CONNECTED;
80             break;
81         case static_cast<int32_t>(Bluetooth::BTConnectState::DISCONNECTING):
82             realConnectionState = BluetoothConnectionState::DISCONNECTING;
83             break;
84         case static_cast<int32_t>(Bluetooth::BTConnectState::DISCONNECTED):
85             realConnectionState = BluetoothConnectionState::DISCONNECTED;
86             break;
87         default:
88             realConnectionState = -1;
89     }
90     return realConnectionState;
91 }
92 
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)93 ErrCode GetBluetoothInfoPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data,
94     MessageParcel &reply, int32_t userId)
95 {
96     EDMLOGI("GetBluetoothInfoPlugin OnGetPolicy.");
97     std::string name = Bluetooth::BluetoothHost::GetDefaultHost().GetLocalName();
98     int32_t state = Bluetooth::BluetoothHost::GetDefaultHost().GetBtState();
99     int32_t connectionState = Bluetooth::BluetoothHost::GetDefaultHost().GetBtConnectionState();
100     int32_t realState = TransformBluetoothState(state);
101     int32_t realConnectionState = TransformBluetoothConnectionState(connectionState);
102     if (realState == -1 || realConnectionState == -1) {
103         EDMLOGD("GetBluetoothInfoPlugin::get bluetooth info failed.State or connectionState is illegally.");
104         reply.WriteInt32(EdmReturnErrCode::SYSTEM_ABNORMALLY);
105         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
106     }
107     reply.WriteInt32(ERR_OK);
108     reply.WriteString(name);
109     reply.WriteInt32(realState);
110     reply.WriteInt32(realConnectionState);
111     return ERR_OK;
112 }
113 } // namespace EDM
114 } // namespace OHOS