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 #ifndef COOPERATE_H
17 #define COOPERATE_H
18 
19 #include <mutex>
20 
21 #include "nocopyable.h"
22 
23 #include "i_context.h"
24 #include "state_machine.h"
25 
26 namespace OHOS {
27 namespace Msdp {
28 namespace DeviceStatus {
29 namespace Cooperate {
30 class Cooperate final : public ICooperate {
31 public:
32     Cooperate(IContext *env);
33     ~Cooperate();
34     DISALLOW_COPY_AND_MOVE(Cooperate);
35 
36     void AddObserver(std::shared_ptr<ICooperateObserver> observer) override;
37     void RemoveObserver(std::shared_ptr<ICooperateObserver> observer) override;
38 
39     int32_t RegisterListener(int32_t pid) override;
40     int32_t UnregisterListener(int32_t pid) override;
41     int32_t RegisterHotAreaListener(int32_t pid) override;
42     int32_t UnregisterHotAreaListener(int32_t pid) override;
43     int32_t RegisterEventListener(int32_t pid, const std::string &networkId) override;
44     int32_t UnregisterEventListener(int32_t pid, const std::string &networkId) override;
45 
46     int32_t Enable(int32_t tokenId, int32_t pid, int32_t userData) override;
47     int32_t Disable(int32_t pid, int32_t userData) override;
48     int32_t Start(int32_t pid, int32_t userData, const std::string &remoteNetworkId, int32_t startDeviceId) override;
49     int32_t Stop(int32_t pid, int32_t userData, bool isUnchained) override;
50 
51     int32_t GetCooperateState(int32_t pid, int32_t userData, const std::string &networkId) override;
52     int32_t GetCooperateState(const std::string &udId, bool &state) override;
53     int32_t Update(uint32_t mask, uint32_t flag) override;
54     void Dump(int32_t fd) override;
55 
56 private:
57     void Loop();
58     void StartWorker();
59     void StopWorker();
60     void LoadMotionDrag();
61     void UnloadMotionDrag();
62 
63     IContext *env_ { nullptr };
64     Context context_;
65     StateMachine sm_;
66     std::mutex lock_;
67     bool workerStarted_ { false };
68     std::thread worker_;
69     Channel<CooperateEvent>::Receiver receiver_;
70 };
71 } // namespace Cooperate
72 } // namespace DeviceStatus
73 } // namespace Msdp
74 } // namespace OHOS
75 #endif // COOPERATE_H
76