179a732c7Sopenharmony_ci/* 279a732c7Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 379a732c7Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 479a732c7Sopenharmony_ci * you may not use this file except in compliance with the License. 579a732c7Sopenharmony_ci * You may obtain a copy of the License at 679a732c7Sopenharmony_ci * 779a732c7Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 879a732c7Sopenharmony_ci * 979a732c7Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1079a732c7Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1179a732c7Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1279a732c7Sopenharmony_ci * See the License for the specific language governing permissions and 1379a732c7Sopenharmony_ci * limitations under the License. 1479a732c7Sopenharmony_ci */ 1579a732c7Sopenharmony_ci 1679a732c7Sopenharmony_ci#include "softbus_publish.h" 1779a732c7Sopenharmony_ci 1879a732c7Sopenharmony_ci#include <mutex> 1979a732c7Sopenharmony_ci 2079a732c7Sopenharmony_ci#ifdef SUPPORT_BLUETOOTH 2179a732c7Sopenharmony_ci#include "bluetooth_def.h" 2279a732c7Sopenharmony_ci#endif // SUPPORT_BLUETOOTH 2379a732c7Sopenharmony_ci#include "dm_constants.h" 2479a732c7Sopenharmony_ci#include "dm_log.h" 2579a732c7Sopenharmony_ci#include "system_ability_definition.h" 2679a732c7Sopenharmony_ci#ifdef SUPPORT_WIFI 2779a732c7Sopenharmony_ci#include "wifi_msg.h" 2879a732c7Sopenharmony_ci#endif // SUPPORT_WIFI 2979a732c7Sopenharmony_ci 3079a732c7Sopenharmony_cinamespace OHOS { 3179a732c7Sopenharmony_cinamespace DistributedHardware { 3279a732c7Sopenharmony_ci 3379a732c7Sopenharmony_cistatic IPublishCb softbusPublishCallback_ = { 3479a732c7Sopenharmony_ci .OnPublishResult = SoftbusPublish::OnSoftbusPublishResult, 3579a732c7Sopenharmony_ci}; 3679a732c7Sopenharmony_ci 3779a732c7Sopenharmony_cistd::mutex g_publishMutex; 3879a732c7Sopenharmony_ci 3979a732c7Sopenharmony_civoid PublishCommonEventCallback(int32_t bluetoothState, int32_t wifiState, int32_t screenState) 4079a732c7Sopenharmony_ci{ 4179a732c7Sopenharmony_ci LOGI("PublishCommonEventCallback start, bleState: %{public}d, wifiState: %{public}d, screenState: %{public}d", 4279a732c7Sopenharmony_ci bluetoothState, wifiState, screenState); 4379a732c7Sopenharmony_ci std::lock_guard<std::mutex> saLock(g_publishMutex); 4479a732c7Sopenharmony_ci SoftbusPublish softbusPublish; 4579a732c7Sopenharmony_ci if (screenState == DM_SCREEN_OFF) { 4679a732c7Sopenharmony_ci int32_t ret = softbusPublish.StopPublishSoftbusLNN(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID); 4779a732c7Sopenharmony_ci if (ret != DM_OK) { 4879a732c7Sopenharmony_ci LOGE("stop publish failed, ret : %{public}d.", ret); 4979a732c7Sopenharmony_ci return; 5079a732c7Sopenharmony_ci } 5179a732c7Sopenharmony_ci LOGI("stop publish successed, ret : %{public}d.", ret); 5279a732c7Sopenharmony_ci return; 5379a732c7Sopenharmony_ci } 5479a732c7Sopenharmony_ci#ifdef SUPPORT_BLUETOOTH 5579a732c7Sopenharmony_ci if (bluetoothState == static_cast<int32_t>(Bluetooth::BTStateID::STATE_TURN_ON) && 5679a732c7Sopenharmony_ci screenState == DM_SCREEN_ON) { 5779a732c7Sopenharmony_ci softbusPublish.StopPublishSoftbusLNN(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID); 5879a732c7Sopenharmony_ci int32_t ret = softbusPublish.PublishSoftbusLNN(); 5979a732c7Sopenharmony_ci if (ret != DM_OK) { 6079a732c7Sopenharmony_ci LOGE("bluetooth publish failed, ret : %{public}d.", ret); 6179a732c7Sopenharmony_ci return; 6279a732c7Sopenharmony_ci } 6379a732c7Sopenharmony_ci LOGI("bluetooth publish successed, ret : %{public}d.", ret); 6479a732c7Sopenharmony_ci return; 6579a732c7Sopenharmony_ci } 6679a732c7Sopenharmony_ci softbusPublish.StopPublishSoftbusLNN(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID); 6779a732c7Sopenharmony_ci#endif // SUPPORT_BLUETOOTH 6879a732c7Sopenharmony_ci 6979a732c7Sopenharmony_ci#ifdef SUPPORT_WIFI 7079a732c7Sopenharmony_ci if (wifiState == static_cast<int32_t>(OHOS::Wifi::WifiState::ENABLED) && 7179a732c7Sopenharmony_ci screenState == DM_SCREEN_ON) { 7279a732c7Sopenharmony_ci softbusPublish.StopPublishSoftbusLNN(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID); 7379a732c7Sopenharmony_ci int32_t ret = softbusPublish.PublishSoftbusLNN(); 7479a732c7Sopenharmony_ci if (ret != DM_OK) { 7579a732c7Sopenharmony_ci LOGE("wifi publish failed, ret : %{public}d.", ret); 7679a732c7Sopenharmony_ci return; 7779a732c7Sopenharmony_ci } 7879a732c7Sopenharmony_ci LOGI("wifi publish successed, ret : %{public}d.", ret); 7979a732c7Sopenharmony_ci return; 8079a732c7Sopenharmony_ci } 8179a732c7Sopenharmony_ci softbusPublish.StopPublishSoftbusLNN(DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID); 8279a732c7Sopenharmony_ci#endif // SUPPORT_WIFI 8379a732c7Sopenharmony_ci} 8479a732c7Sopenharmony_ci 8579a732c7Sopenharmony_ciSoftbusPublish::SoftbusPublish() 8679a732c7Sopenharmony_ci{ 8779a732c7Sopenharmony_ci LOGI("SoftbusPublish constructor."); 8879a732c7Sopenharmony_ci} 8979a732c7Sopenharmony_ci 9079a732c7Sopenharmony_ciSoftbusPublish::~SoftbusPublish() 9179a732c7Sopenharmony_ci{ 9279a732c7Sopenharmony_ci LOGI("SoftbusPublish destructor."); 9379a732c7Sopenharmony_ci} 9479a732c7Sopenharmony_ci 9579a732c7Sopenharmony_civoid SoftbusPublish::OnSoftbusPublishResult(int publishId, PublishResult result) 9679a732c7Sopenharmony_ci{ 9779a732c7Sopenharmony_ci LOGD("OnSoftbusPublishResult, publishId: %{public}d, result: %{public}d.", publishId, result); 9879a732c7Sopenharmony_ci} 9979a732c7Sopenharmony_ci 10079a732c7Sopenharmony_ciint32_t SoftbusPublish::PublishSoftbusLNN() 10179a732c7Sopenharmony_ci{ 10279a732c7Sopenharmony_ci LOGI("Begin."); 10379a732c7Sopenharmony_ci PublishInfo publishInfo; 10479a732c7Sopenharmony_ci publishInfo.publishId = DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID; 10579a732c7Sopenharmony_ci publishInfo.mode = DiscoverMode::DISCOVER_MODE_PASSIVE; 10679a732c7Sopenharmony_ci publishInfo.medium = ExchangeMedium::AUTO; 10779a732c7Sopenharmony_ci publishInfo.freq = ExchangeFreq::LOW; 10879a732c7Sopenharmony_ci publishInfo.capability = DM_CAPABILITY_OSD; 10979a732c7Sopenharmony_ci publishInfo.ranging = false; 11079a732c7Sopenharmony_ci 11179a732c7Sopenharmony_ci LOGI("Begin, publishId: %{public}d, mode: 0x%{public}x, medium: %{public}d, capability:" 11279a732c7Sopenharmony_ci "%{public}s, ranging: %{public}d, freq: %{public}d.", publishInfo.publishId, publishInfo.mode, 11379a732c7Sopenharmony_ci publishInfo.medium, publishInfo.capability, publishInfo.ranging, publishInfo.freq); 11479a732c7Sopenharmony_ci 11579a732c7Sopenharmony_ci int32_t ret = PublishLNN(DM_PKG_NAME, &publishInfo, &softbusPublishCallback_); 11679a732c7Sopenharmony_ci if (ret != DM_OK) { 11779a732c7Sopenharmony_ci LOGE("[SOFTBUS]PublishLNN failed, ret: %{public}d.", ret); 11879a732c7Sopenharmony_ci return ERR_DM_PUBLISH_FAILED; 11979a732c7Sopenharmony_ci } 12079a732c7Sopenharmony_ci return DM_OK; 12179a732c7Sopenharmony_ci} 12279a732c7Sopenharmony_ci 12379a732c7Sopenharmony_ciint32_t SoftbusPublish::StopPublishSoftbusLNN(int32_t publishId) 12479a732c7Sopenharmony_ci{ 12579a732c7Sopenharmony_ci LOGI("Begin, publishId: %{public}d.", publishId); 12679a732c7Sopenharmony_ci int32_t ret = StopPublishLNN(DM_PKG_NAME, publishId); 12779a732c7Sopenharmony_ci if (ret != DM_OK) { 12879a732c7Sopenharmony_ci LOGE("[SOFTBUS]StopPublishLNN failed, ret: %{public}d.", ret); 12979a732c7Sopenharmony_ci return ERR_DM_STOP_PUBLISH_LNN_FAILED; 13079a732c7Sopenharmony_ci } 13179a732c7Sopenharmony_ci return DM_OK; 13279a732c7Sopenharmony_ci} 13379a732c7Sopenharmony_ci} // namespace DistributedHardware 13479a732c7Sopenharmony_ci} // namespace OHOS