1 /* 2 * Copyright (C) 2021-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 #ifndef OHOS_IPC_SERVICES_DBINDER_DBINDER_STUB_H 17 #define OHOS_IPC_SERVICES_DBINDER_DBINDER_STUB_H 18 19 #include <string> 20 #include <parcel.h> 21 #include "ipc_object_stub.h" 22 23 namespace OHOS { 24 #ifdef BINDER_IPC_32BIT 25 typedef unsigned int binder_uintptr_t; 26 #else 27 typedef unsigned long long binder_uintptr_t; 28 #endif 29 30 enum NegotiationStatus { 31 NEGO_INIT, 32 NEGO_DOING, 33 NEGO_FINISHED 34 }; 35 36 class DBinderServiceStub : public IPCObjectStub { 37 public: 38 explicit DBinderServiceStub(const std::u16string &serviceName, const std::string &deviceID, 39 binder_uintptr_t binderObject, uint32_t pid = 0, uint32_t uid = 0); 40 ~DBinderServiceStub(); 41 42 /** 43 * @brief Serialize a specified DBinderServiceStub object. 44 * @param parcel Indicates the object storing the data. 45 * @param object Indicates the serialized object. 46 * @return Returns <b>true</b> if serialized successfully; returns <b>false</b> otherwise. 47 * @since 12 48 */ 49 static bool Marshalling(Parcel &parcel, const sptr<IRemoteObject> &object); 50 51 /** 52 * @brief Serialize self. 53 * @param parcel Indicates the object storing the data. 54 * @return Returns <b>true</b> if serialized successfully; returns <b>false</b> otherwise. 55 * @since 12 56 */ 57 bool Marshalling(Parcel &parcel) const override; 58 59 /** 60 * @brief Gets the process protocol. 61 * @param code Indicates the message code of the request. 62 * @param data Indicates the object storing the data to be sent. 63 * @param reply Indicates the object receiving the response data. 64 * @param option Indicates a synchronous (default) or asynchronous request. 65 * @return Returns {@code 0} if valid notifications; returns an error code if the operation fails. 66 * @since 9 67 */ 68 int32_t ProcessProto(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; 69 70 /** 71 * @brief Response processing of the request. 72 * @param code Indicates the service request code sent from the peer end. 73 * @param data Indicates the object sent from the peer end. 74 * @param reply Indicates the response message object sent from the remote service. 75 * @param options Indicates whether the operation is synchronous or asynchronous. 76 * @return Returns {@code 0} if the operation succeeds; returns an error code if the operation fails. 77 * @since 9 78 */ 79 int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; 80 81 /** 82 * @brief Get and save the dbinder object data. 83 * @param pid Indicates the sender pid. 84 * @param uid Indicates the sender uid. 85 * @return Returns {@link ERR_NONE} if the operation is successful; returns an error code 86 * defined in {@link ipc_types.h} otherwise. 87 * @since 12 88 */ 89 int GetAndSaveDBinderData(pid_t pid, uid_t uid) override; 90 91 /** 92 * @brief Obtains the service name. 93 * @return Returns the service name. 94 * @since 9 95 */ 96 const std::u16string &GetServiceName(); 97 98 /** 99 * @brief Obtain the device ID. 100 * @return Returns the device ID. 101 * @since 9 102 */ 103 const std::string &GetDeviceID(); 104 105 /** 106 * @brief Obtain the binder object. 107 * @return Returns the binder object. 108 * @since 9 109 */ 110 binder_uintptr_t GetBinderObject() const; 111 112 uint32_t GetPeerPid(); 113 114 uint32_t GetPeerUid(); 115 116 void SetSeqNumber(uint32_t seqNum); 117 118 uint32_t GetSeqNumber(); 119 120 void SetNegoStatusAndTime(NegotiationStatus status, uint64_t time); 121 122 void GetNegoStatusAndTime(NegotiationStatus &status, uint64_t &time); 123 124 private: 125 int32_t ProcessDeathRecipient(MessageParcel &data); 126 int32_t AddDbinderDeathRecipient(MessageParcel &data); 127 int32_t RemoveDbinderDeathRecipient(MessageParcel &data); 128 bool CheckSessionObjectValidity(); 129 int SaveDBinderData(const std::string &localBusName); 130 int DBinderClearServiceState(uint32_t code, MessageParcel &data, MessageParcel &reply, 131 MessageOption &option) override; 132 133 const std::u16string serviceName_; 134 const std::string deviceID_; 135 binder_uintptr_t binderObject_; 136 std::unique_ptr<uint8_t[]> dbinderData_ {nullptr}; 137 uint32_t seqNum_ = 0; 138 uint32_t peerPid_ = 0; 139 uint32_t peerUid_ = 0; 140 uint64_t negoTime_ = 0; 141 std::atomic<bool> isInited_ = false; 142 std::atomic<NegotiationStatus> negoStatus_ = NegotiationStatus::NEGO_INIT; 143 }; 144 } // namespace OHOS 145 #endif // OHOS_IPC_SERVICES_DBINDER_DBINDER_STUB_H 146