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_BLUETOOTH_STANDARD_SOCKET_SERVER_H 17 #define OHOS_BLUETOOTH_STANDARD_SOCKET_SERVER_H 18 19 #include <sys/socket.h> 20 #include <unistd.h> 21 #include "bluetooth_socket_stub.h" 22 #include "bt_uuid.h" 23 #include "bluetooth_raw_address.h" 24 #include "base_def.h" 25 26 namespace OHOS { 27 namespace Bluetooth { 28 class BluetoothSocketServer : public BluetoothSocketStub { 29 public: 30 class SocketObserverList { 31 public: 32 struct SocketObserverApplication { SocketObserverApplicationSocketObserverApplication33 SocketObserverApplication(int fd, const sptr<IRemoteObject> &remote) : fd(fd), remote(remote) {} 34 35 int fd; 36 wptr<IRemoteObject> remote; 37 }; 38 class DeathRecipient : public IRemoteObject::DeathRecipient { 39 public: DeathRecipient(SocketObserverList & applications)40 explicit DeathRecipient(SocketObserverList &applications) : applications_(applications) {} 41 ~DeathRecipient() = default; OnRemoteDied(const wptr<IRemoteObject> & remote)42 void OnRemoteDied(const wptr<IRemoteObject> &remote) override 43 { 44 applications_.OnRemoteDied(remote); 45 } 46 private: 47 SocketObserverList &applications_; 48 }; 49 SocketObserverList()50 SocketObserverList() 51 { 52 deathRecipient_ = new DeathRecipient(*this); 53 } 54 ~SocketObserverList() = default; OnRemoteDied(const wptr<IRemoteObject> & remote)55 void OnRemoteDied(const wptr<IRemoteObject> &remote) 56 { 57 std::lock_guard<std::mutex> lock(observerListLock_); 58 auto iter = GetObserverApplication(remote); 59 if (iter != observerList_.end()) { 60 iter->remote->RemoveDeathRecipient(deathRecipient_); 61 shutdown(iter->fd, SHUT_RD); 62 shutdown(iter->fd, SHUT_WR); 63 close(iter->fd); 64 observerList_.erase(iter); 65 } 66 } 67 AddObserver(int fd,const sptr<IRemoteObject> & remote)68 void AddObserver(int fd, const sptr<IRemoteObject> &remote) 69 { 70 if (GetObserverApplication(remote) != observerList_.end()) { 71 return; 72 } 73 SocketObserverApplication app(fd, remote); 74 remote->AddDeathRecipient(deathRecipient_); 75 observerList_.push_back(app); 76 } 77 RemoveObserver(const sptr<IRemoteObject> & remote)78 void RemoveObserver(const sptr<IRemoteObject> &remote) 79 { 80 auto iter = GetObserverApplication(remote); 81 if (iter != observerList_.end()) { 82 iter->remote->RemoveDeathRecipient(deathRecipient_); 83 observerList_.erase(iter); 84 } 85 } 86 private: GetObserverApplication(const wptr<IRemoteObject> & remote)87 std::vector<SocketObserverApplication>::iterator GetObserverApplication(const wptr<IRemoteObject> &remote) 88 { 89 return std::find_if(observerList_.begin(), observerList_.end(), 90 [remote](const auto &app) {return app.remote == remote; }); 91 } 92 private: 93 std::mutex observerListLock_; 94 std::vector<SocketObserverApplication> observerList_; 95 sptr<DeathRecipient> deathRecipient_; 96 }; BluetoothSocketServer()97 BluetoothSocketServer() : socketObserverList_(std::make_unique<SocketObserverList>()) {} ~BluetoothSocketServer()98 ~BluetoothSocketServer() {} 99 100 int Connect(ConnectSocketParam ¶m, int &fd) override; 101 int Listen(ListenSocketParam ¶m, int &fd) override; 102 int DeregisterServerObserver(const sptr<IBluetoothServerSocketObserver> &observer) override; 103 int RegisterClientObserver(const BluetoothRawAddress &addr, const bluetooth::Uuid uuid, 104 const sptr<IBluetoothClientSocketObserver> &observer) override; 105 int DeregisterClientObserver(const BluetoothRawAddress &addr, const bluetooth::Uuid uuid, 106 const sptr<IBluetoothClientSocketObserver> &observer) override; 107 int UpdateCocConnectionParams(const BluetoothSocketCocInfo &info) override; 108 private: 109 std::unique_ptr<SocketObserverList> socketObserverList_; 110 }; 111 } // namespace Bluetooth 112 } // namespace OHOS 113 #endif // OHOS_BLUETOOTH_STANDARD_SOCKET_SERVER_H