• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_DEVICE_MANAGER_IPC_SERVER_STUB_H
17 #define OHOS_DEVICE_MANAGER_IPC_SERVER_STUB_H
18 
19 #include <memory>
20 #include <mutex>
21 #include <map>
22 #include <tuple>
23 #include <vector>
24 
25 #include "nlohmann/json.hpp"
26 
27 #include "system_ability.h"
28 #include "thread_pool.h"
29 #include "iremote_stub.h"
30 #include "ipc_remote_broker.h"
31 
32 #include "single_instance.h"
33 #include "hichain_connector.h"
34 
35 namespace OHOS {
36 namespace DistributedHardware {
37 enum class ServiceRunningState {
38     STATE_NOT_START,
39     STATE_RUNNING
40 };
41 
42 class AppDeathRecipient : public IRemoteObject::DeathRecipient {
43 public:
44     void OnRemoteDied(const wptr<IRemoteObject> &remote) override;
45     AppDeathRecipient() = default;
46     ~AppDeathRecipient() = default;
47 };
48 
49 class IpcServerStub : public SystemAbility, public IRemoteStub<IpcRemoteBroker> {
50 DECLARE_SYSTEM_ABILITY(IpcServerStub);
51 DECLARE_SINGLE_INSTANCE_BASE(IpcServerStub);
52 public:
53     void OnStart() override;
54     void OnStop() override;
55     int32_t OnRemoteRequest(uint32_t code,
56         MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
57     int32_t SendCmd(int32_t cmdCode, std::shared_ptr<IpcReq> req, std::shared_ptr<IpcRsp> rsp) override;
58     int32_t RegisterDeviceManagerListener(std::string &pkgName, sptr<IRemoteObject> listener);
59     int32_t UnRegisterDeviceManagerListener(std::string &pkgName);
60     ServiceRunningState QueryServiceState() const;
61     const std::map<std::string, sptr<IRemoteObject>> &GetDmListener();
62     const sptr<IpcRemoteBroker> GetDmListener(std::string pkgName) const;
63 private:
64     IpcServerStub();
65     ~IpcServerStub() = default;
66     bool Init();
67 private:
68     bool registerToService_;
69     ServiceRunningState state_;
70     std::mutex listenerLock_;
71     std::map<std::string, sptr<AppDeathRecipient>> appRecipient_;
72     std::map<std::string, sptr<IRemoteObject>> dmListener_;
73 };
74 } // namespace DistributedHardware
75 } // namespace OHOS
76 #endif // OHOS_DEVICE_MANAGER_IPC_SERVER_STUB_H
77