1 /*
2  * Copyright (c) 2023-2024 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 DMS_CONTINUE_RECV_MANAGER_H
17 #define DMS_CONTINUE_RECV_MANAGER_H
18 
19 #include <deque>
20 #include <map>
21 #include <mutex>
22 #include <queue>
23 #include <string>
24 #include <thread>
25 #include <utility>
26 #include <vector>
27 
28 #include "bundle/bundle_manager_internal.h"
29 #include "distributed_mission_broadcast_listener.h"
30 #include "distributed_mission_died_listener.h"
31 #include "distributed_mission_focused_listener.h"
32 #include "event_handler.h"
33 #include "mission_info.h"
34 
35 namespace OHOS {
36 namespace DistributedSchedule {
37 const std::string CONTINUE_RECV_MANAGER = "continue_recv_manager";
38 struct currentIconInfo {
39     std::string senderNetworkId;
40     std::string bundleName;
41     std::string continueType;
42 
43     std::string sourceBundleName;
44 
isEmptycurrentIconInfo45     bool isEmpty()
46     {
47         return (this->senderNetworkId == "" && this->bundleName == "" && this->continueType == "");
48     }
49 
50     currentIconInfo(const std::string &source_device_id, const std::string &source_bundle_name,
51         const std::string &sink_bundle_name, const std::string &continueType = "")
52         : senderNetworkId(source_device_id),
53           bundleName(sink_bundle_name),
54           continueType(continueType),
55           sourceBundleName(source_bundle_name) {
56     }
57 
58     currentIconInfo() = default;
59 
60     ~currentIconInfo() = default;
61 };
62 
63 class DMSContinueRecvMgr {
64     DECLARE_SINGLE_INSTANCE(DMSContinueRecvMgr);
65 
66 public:
67     constexpr static uint8_t DMS_DATA_LEN = 3; // Dms data Length
68     constexpr static int32_t DMS_SEND_LEN = 4; // Maximum Broadcast Length
69     constexpr static uint8_t DMS_0XF0 = 0xf0;
70     constexpr static uint8_t DMS_0X0F = 0x0f;
71     constexpr static uint8_t DMS_0XFF = 0xff;
72     constexpr static uint8_t DMS_FOCUSED_TYPE = 0x00;
73     constexpr static uint8_t DMS_UNFOCUSED_TYPE = 0x01;
74     constexpr static uint8_t CONTINUE_SHIFT_24 = 0x18;
75     constexpr static uint8_t CONTINUE_SHIFT_16 = 0x10;
76     constexpr static uint8_t CONTINUE_SHIFT_08 = 0x08;
77     constexpr static uint8_t CONTINUE_SHIFT_04 = 0x04;
78     constexpr static int32_t INVALID_MISSION_ID = -1;
79 
80     void Init();
81     void UnInit();
82     void NotifyDataRecv(std::string& senderNetworkId, uint8_t* payload, uint32_t dataLen);
83     int32_t RegisterOnListener(const std::string& type, const sptr<IRemoteObject>& obj);
84     int32_t RegisterOffListener(const std::string& type, const sptr<IRemoteObject>& obj);
85     void NotifyDied(const sptr<IRemoteObject>& obj);
86     void NotifyDeviceOffline(const std::string& networkId);
87     void NotifyPackageRemoved(const std::string& sinkBundleName);
88     void OnDeviceScreenOff();
89     void OnContinueSwitchOff();
90     std::string GetContinueType(const std::string& bundleName);
91     bool CheckRegSoftbusListener();
92 
93 private:
94     void StartEvent();
95     int32_t RetryPostBroadcast(const std::string& senderNetworkId, uint16_t bundleNameId, uint8_t continueTypeId,
96         const int32_t state, const int32_t retry);
97     bool GetFinalBundleName(DmsBundleInfo& distributedBundleInfo,  std::string &finalBundleName,
98         AppExecFwk::BundleInfo& localBundleInfo, std::string& continueType);
99     int32_t VerifyBroadcastSource(const std::string& senderNetworkId, const std::string& srcBundleName,
100         const std::string& sinkBundleName, const std::string& continueType, const int32_t state);
101     void PostOnBroadcastBusiness(const std::string& senderNetworkId, uint16_t bundleNameId, uint8_t continueTypeId,
102         const int32_t state, const int32_t delay = 0, const int32_t retry = 0);
103     void FindContinueType(const DmsBundleInfo &distributedBundleInfo, uint8_t &continueTypeId,
104         std::string &continueType);
105     int32_t DealOnBroadcastBusiness(const std::string& senderNetworkId, uint16_t bundleNameId, uint8_t continueTypeId,
106         const int32_t state, const int32_t retry = 0);
107     void NotifyRecvBroadcast(const sptr<IRemoteObject>& obj, const currentIconInfo& continueInfo, const int32_t state);
108     bool IsBundleContinuable(const AppExecFwk::BundleInfo& bundleInfo);
109 private:
110     currentIconInfo iconInfo_;
111     sptr<DistributedMissionDiedListener> missionDiedListener_;
112     std::string onType_;
113     std::map<std::string, std::vector<sptr<IRemoteObject>>> registerOnListener_;
114     std::thread eventThread_;
115     std::condition_variable eventCon_;
116     std::mutex eventMutex_;
117     std::mutex iconMutex_;
118     std::shared_ptr<OHOS::AppExecFwk::EventHandler> eventHandler_;
119     bool hasRegSoftbusEventListener_ = false;
120 };
121 } // namespace DistributedSchedule
122 } // namespace OHOS
123 #endif // DMS_CONTINUE_RECV_MANAGER_H
124