1/* 2 * Copyright (c) 2021 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 "device/device_info.h" 17#include "dfsu_exception.h" 18#include "utils_log.h" 19 20namespace OHOS { 21namespace Storage { 22namespace DistributedFile { 23using namespace std; 24 25DeviceInfo::DeviceInfo(const DistributedHardware::DmDeviceInfo &nodeInfo) 26{ 27 cid_ = string(nodeInfo.networkId); 28 initCidFlag_ = true; 29 extraData_ = nodeInfo.extraData; 30 deviceId_ = string(nodeInfo.deviceId); 31 // convert networkId to udid 32 auto &deviceManager = DistributedHardware::DeviceManager::GetInstance(); 33 deviceManager.GetUdidByNetworkId(IDaemon::SERVICE_NAME, cid_, udid_); 34} 35 36DeviceInfo &DeviceInfo::operator=(const DistributedHardware::DmDeviceInfo &nodeInfo) 37{ 38 cid_ = string(nodeInfo.networkId); 39 initCidFlag_ = true; 40 extraData_ = nodeInfo.extraData; 41 deviceId_ = string(nodeInfo.deviceId); 42 return *this; 43} 44 45DeviceInfo::DeviceInfo(const DeviceInfo &nodeInfo) : cid_(nodeInfo.cid_), udid_(nodeInfo.udid_), 46 extraData_(nodeInfo.extraData_), deviceId_(nodeInfo.deviceId_) 47{ 48 initCidFlag_.store(nodeInfo.initCidFlag_.load()); 49} 50 51void DeviceInfo::SetCid(const string &cid) 52{ 53 if (initCidFlag_ == false) { 54 cid_ = cid; 55 initCidFlag_ = true; 56 } else { 57 LOGI("Cid is already initialized"); 58 } 59} 60 61const string &DeviceInfo::GetCid() const 62{ 63 if (initCidFlag_ == false) { 64 ThrowException(ERR_DEVICE_CID_UN_INIT, "cid uninitialized"); 65 } 66 return cid_; 67} 68 69const string &DeviceInfo::GetExtraData() const 70{ 71 if (extraData_.empty()) { 72 LOGE("extraData is empty."); 73 } 74 return extraData_; 75} 76 77const string &DeviceInfo::GetDeviceId() const 78{ 79 if (deviceId_.empty()) { 80 LOGE("deviceId is empty."); 81 } 82 return deviceId_; 83} 84} // namespace DistributedFile 85} // namespace Storage 86} // namespace OHOS 87