1 /* 2 * Copyright (c) 2022-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 #include "enumerator.h" 17 18 #include <dirent.h> 19 #include <sys/stat.h> 20 21 #include "devicestatus_define.h" 22 #include "fi_log.h" 23 #include "napi_constants.h" 24 #include "utility.h" 25 26 namespace OHOS { 27 namespace Msdp { 28 namespace DeviceStatus { 29 namespace { 30 constexpr OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "Enumerator" }; 31 } // namespace 32 SetDeviceMgr(IDeviceMgr * devMgr)33void Enumerator::SetDeviceMgr(IDeviceMgr *devMgr) 34 { 35 CALL_DEBUG_ENTER; 36 CHKPV(devMgr); 37 devMgr_ = devMgr; 38 } 39 ScanDevices()40void Enumerator::ScanDevices() 41 { 42 CALL_DEBUG_ENTER; 43 ScanAndAddDevices(); 44 } 45 ScanAndAddDevices()46void Enumerator::ScanAndAddDevices() 47 { 48 CALL_DEBUG_ENTER; 49 DIR *dir = opendir(DEV_INPUT_PATH.c_str()); 50 CHKPV(dir); 51 struct dirent *dent; 52 53 while ((dent = readdir(dir)) != nullptr) { 54 const std::string devNode { dent->d_name }; 55 const std::string devPath { DEV_INPUT_PATH + devNode }; 56 struct stat statbuf; 57 58 if (stat(devPath.c_str(), &statbuf) != 0) { 59 continue; 60 } 61 if (!S_ISCHR(statbuf.st_mode)) { 62 continue; 63 } 64 AddDevice(devNode); 65 } 66 67 closedir(dir); 68 } 69 AddDevice(const std::string & devNode) const70void Enumerator::AddDevice(const std::string &devNode) const 71 { 72 CALL_DEBUG_ENTER; 73 CHKPV(devMgr_); 74 devMgr_->AddDevice(devNode); 75 } 76 } // namespace DeviceStatus 77 } // namespace Msdp 78 } // namespace OHOS 79