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 
16 #include "bluetooth_socket_observer_stub.h"
17 #include "bluetooth_log.h"
18 #include "bluetooth_raw_address.h"
19 #include "bluetooth_bt_uuid.h"
20 #include "bluetooth_errorcode.h"
21 
22 namespace OHOS {
23 namespace Bluetooth {
OnRemoteRequest( uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)24 int32_t BluetoothClientSocketObserverStub::OnRemoteRequest(
25     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
26 {
27     HILOGD("BluetoothClientSocketObserverStub: transaction of code: %{public}d is received", code);
28     if (BluetoothClientSocketObserverStub::GetDescriptor() != data.ReadInterfaceToken()) {
29         HILOGE("BluetoothClientSocketObserverStub::OnRemoteRequest, local descriptor is not equal to remote");
30         return BT_ERR_INVALID_STATE;
31     }
32 
33     int32_t errcode = BT_NO_ERROR;
34     switch (code) {
35         case static_cast<uint32_t>(BluetoothSocketObserverInterfaceCode::BT_SOCKET_OBSERVER_CONNECTION_STATE_CHANGED):
36             errcode = OnConnectionStateChangedInner(data, reply);
37             break;
38         default:
39             HILOGW("default case, need check.");
40             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
41     }
42     return errcode;
43 }
44 
OnConnectionStateChangedInner(MessageParcel &data, MessageParcel &reply)45 int32_t BluetoothClientSocketObserverStub::OnConnectionStateChangedInner(MessageParcel &data, MessageParcel &reply)
46 {
47     HILOGD("enter");
48 
49     std::shared_ptr<BluetoothRawAddress> addr(data.ReadParcelable<BluetoothRawAddress>());
50     if (!addr) {
51         HILOGE("read addr failed");
52         return BT_ERR_IPC_TRANS_FAILED;
53     }
54     std::shared_ptr<BluetoothUuid> uuid(data.ReadParcelable<BluetoothUuid>());
55     if (!uuid) {
56         HILOGE("read uuid failed");
57         return BT_ERR_IPC_TRANS_FAILED;
58     }
59     bluetooth::Uuid btUuid = bluetooth::Uuid(*uuid);
60     int status = data.ReadInt32();
61     int result = data.ReadInt32();
62     int type = data.ReadInt32();
63     int psm = data.ReadInt32();
64     CallbackParam callbackParam = {
65         .dev = *addr,
66         .uuid = btUuid,
67         .status = status,
68         .result = result,
69         .type = type,
70         .psm = psm,
71     };
72     OnConnectionStateChanged(callbackParam);
73     return BT_NO_ERROR;
74 }
75 
OnRemoteRequest( uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)76 int32_t BluetoothServerSocketObserverStub::OnRemoteRequest(
77     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
78 {
79     HILOGD("BluetoothServerSocketObserverStub: transaction of code: %{public}d is received", code);
80     if (BluetoothServerSocketObserverStub::GetDescriptor() != data.ReadInterfaceToken()) {
81         HILOGE("BluetoothServerSocketObserverStub::OnRemoteRequest, local descriptor is not equal to remote");
82         return BT_ERR_INVALID_STATE;
83     }
84 
85     HILOGW("BluetoothServerSocketObserverStub::OnRemoteRequest, default case, need check.");
86     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
87 }
88 }  // namespace Bluetooth
89 }  // namespace OHOS