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
26namespace OHOS {
27namespace Msdp {
28namespace DeviceStatus {
29namespace Cooperate {
30class Cooperate final : public ICooperate {
31public:
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    int32_t SetDamplingCoefficient(uint32_t direction, double coefficient) override;
55    void Dump(int32_t fd) override;
56
57private:
58    void Loop();
59    void StartWorker();
60    void StopWorker();
61    void LoadMotionDrag();
62    void SetDamplingCoefficient(const CooperateEvent &event);
63
64    IContext *env_ { nullptr };
65    Context context_;
66    StateMachine sm_;
67    std::mutex lock_;
68    bool workerStarted_ { false };
69    std::thread worker_;
70    Channel<CooperateEvent>::Receiver receiver_;
71};
72} // namespace Cooperate
73} // namespace DeviceStatus
74} // namespace Msdp
75} // namespace OHOS
76#endif // COOPERATE_H
77