1/*
2 * Copyright (c) 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#include "softbus_publish.h"
17
18#include <mutex>
19
20#ifdef SUPPORT_BLUETOOTH
21#include "bluetooth_def.h"
22#endif // SUPPORT_BLUETOOTH
23#include "dm_constants.h"
24#include "dm_log.h"
25#include "system_ability_definition.h"
26#ifdef SUPPORT_WIFI
27#include "wifi_msg.h"
28#endif // SUPPORT_WIFI
29
30namespace OHOS {
31namespace DistributedHardware {
32
33static IPublishCb softbusPublishCallback_ = {
34    .OnPublishResult = SoftbusPublish::OnSoftbusPublishResult,
35};
36
37std::mutex g_publishMutex;
38
39void PublishCommonEventCallback(int32_t bluetoothState, int32_t wifiState, int32_t screenState)
40{
41    LOGI("PublishCommonEventCallback start, bleState: %{public}d, wifiState: %{public}d, screenState: %{public}d",
42        bluetoothState, wifiState, screenState);
43    std::lock_guard<std::mutex> saLock(g_publishMutex);
44    SoftbusPublish softbusPublish;
45    if (screenState == DM_SCREEN_OFF) {
46        int32_t ret = softbusPublish.StopPublishSoftbusLNN(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID);
47        if (ret != DM_OK) {
48            LOGE("stop publish failed, ret : %{public}d.", ret);
49            return;
50        }
51        LOGI("stop publish successed, ret : %{public}d.", ret);
52        return;
53    }
54#ifdef SUPPORT_BLUETOOTH
55    if (bluetoothState == static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURN_ON) &&
56        screenState == DM_SCREEN_ON) {
57        softbusPublish.StopPublishSoftbusLNN(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID);
58        int32_t ret = softbusPublish.PublishSoftbusLNN();
59        if (ret != DM_OK) {
60            LOGE("bluetooth publish failed, ret : %{public}d.", ret);
61            return;
62        }
63        LOGI("bluetooth publish successed, ret : %{public}d.", ret);
64        return;
65    }
66    softbusPublish.StopPublishSoftbusLNN(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID);
67#endif // SUPPORT_BLUETOOTH
68
69#ifdef SUPPORT_WIFI
70    if (wifiState == static_cast<int32_t>(OHOS::Wifi::WifiState::ENABLED) &&
71        screenState == DM_SCREEN_ON) {
72        softbusPublish.StopPublishSoftbusLNN(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID);
73        int32_t ret = softbusPublish.PublishSoftbusLNN();
74        if (ret != DM_OK) {
75            LOGE("wifi publish failed, ret : %{public}d.", ret);
76            return;
77        }
78        LOGI("wifi publish successed, ret : %{public}d.", ret);
79        return;
80    }
81    softbusPublish.StopPublishSoftbusLNN(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID);
82#endif // SUPPORT_WIFI
83}
84
85SoftbusPublish::SoftbusPublish()
86{
87    LOGI("SoftbusPublish constructor.");
88}
89
90SoftbusPublish::~SoftbusPublish()
91{
92    LOGI("SoftbusPublish destructor.");
93}
94
95void SoftbusPublish::OnSoftbusPublishResult(int publishId, PublishResult result)
96{
97    LOGD("OnSoftbusPublishResult, publishId: %{public}d, result: %{public}d.", publishId, result);
98}
99
100int32_t SoftbusPublish::PublishSoftbusLNN()
101{
102    LOGI("Begin.");
103    PublishInfo publishInfo;
104    publishInfo.publishId = DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID;
105    publishInfo.mode = DiscoverMode::DISCOVER_MODE_PASSIVE;
106    publishInfo.medium = ExchangeMedium::AUTO;
107    publishInfo.freq = ExchangeFreq::LOW;
108    publishInfo.capability = DM_CAPABILITY_OSD;
109    publishInfo.ranging = false;
110
111    LOGI("Begin, publishId: %{public}d, mode: 0x%{public}x, medium: %{public}d, capability:"
112        "%{public}s, ranging: %{public}d, freq: %{public}d.", publishInfo.publishId, publishInfo.mode,
113        publishInfo.medium, publishInfo.capability, publishInfo.ranging, publishInfo.freq);
114
115    int32_t ret = PublishLNN(DM_PKG_NAME, &publishInfo, &softbusPublishCallback_);
116    if (ret != DM_OK) {
117        LOGE("[SOFTBUS]PublishLNN failed, ret: %{public}d.", ret);
118        return ERR_DM_PUBLISH_FAILED;
119    }
120    return DM_OK;
121}
122
123int32_t SoftbusPublish::StopPublishSoftbusLNN(int32_t publishId)
124{
125    LOGI("Begin, publishId: %{public}d.", publishId);
126    int32_t ret = StopPublishLNN(DM_PKG_NAME, publishId);
127    if (ret != DM_OK) {
128        LOGE("[SOFTBUS]StopPublishLNN failed, ret: %{public}d.", ret);
129        return ERR_DM_STOP_PUBLISH_LNN_FAILED;
130    }
131    return DM_OK;
132}
133} // namespace DistributedHardware
134} // namespace OHOS