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 OHOS_SOFTBUS_ADAPTER_H
17 #define OHOS_SOFTBUS_ADAPTER_H
18 
19 #include <string>
20 #include <thread>
21 
22 #include "dsched_data_buffer.h"
23 #include "event_handler.h"
24 #include "single_instance.h"
25 #include "softbus_adapter_listener.h"
26 
27 namespace OHOS {
28 namespace DistributedSchedule {
29 class SoftbusAdapter {
30 public:
31     DECLARE_SINGLE_INSTANCE_BASE(SoftbusAdapter);
32 public:
33     void Init();
34     void UnInit();
35     int32_t SendSoftbusEvent(std::shared_ptr<DSchedDataBuffer> buffer);
36     int32_t StopSoftbusEvent();
37     int32_t RegisterSoftbusEventListener(const std::shared_ptr<SoftbusAdapterListener>& listener);
38     int32_t UnregisterSoftbusEventListener(const std::shared_ptr<SoftbusAdapterListener>& listener);
39     void OnBroadCastRecv(std::string& networkId, uint8_t* data, uint32_t dataLen);
40 
41 private:
SoftbusAdapter()42     SoftbusAdapter() {}
43     void StartEvent();
44     int32_t DealSendSoftbusEvent(std::shared_ptr<DSchedDataBuffer> buffer, const int32_t retry = 0);
45     int32_t RetrySendSoftbusEvent(std::shared_ptr<DSchedDataBuffer> buffer, const int32_t retry);
46     std::shared_ptr<SoftbusAdapterListener> softbusAdapterListener_;
47     std::string pkgName_ = "dms";
48     std::thread eventThread_;
49     std::condition_variable eventCon_;
50     std::mutex eventMutex_;
51     std::mutex softbusAdapterListenerMutex_;
52     std::shared_ptr<OHOS::AppExecFwk::EventHandler> eventHandler_ = nullptr;
53 };
54 } // namespace DistributedSchedule
55 } // namespace OHOS
56 #endif /* OHOS_SOFTBUS_ADAPTER_H */
57