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