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