1cc290419Sopenharmony_ci/*
2cc290419Sopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd.
3cc290419Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4cc290419Sopenharmony_ci * you may not use this file except in compliance with the License.
5cc290419Sopenharmony_ci * You may obtain a copy of the License at
6cc290419Sopenharmony_ci *
7cc290419Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8cc290419Sopenharmony_ci *
9cc290419Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10cc290419Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11cc290419Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12cc290419Sopenharmony_ci * See the License for the specific language governing permissions and
13cc290419Sopenharmony_ci * limitations under the License.
14cc290419Sopenharmony_ci */
15cc290419Sopenharmony_ci#include <cstdarg>
16cc290419Sopenharmony_ci#include "securec.h"
17cc290419Sopenharmony_ci#include "usb_util.h"
18cc290419Sopenharmony_ci#include "log.h"
19cc290419Sopenharmony_ci
20cc290419Sopenharmony_ci#ifdef HOST_MINGW
21cc290419Sopenharmony_ci#include <windows.h>
22cc290419Sopenharmony_ci#endif
23cc290419Sopenharmony_ciusing namespace Hdc;
24cc290419Sopenharmony_ciconstexpr auto USB_FFS_BASE = "/dev/usb-ffs/";
25cc290419Sopenharmony_ci
26cc290419Sopenharmony_cistd::string GetDevPath(const std::string &path)
27cc290419Sopenharmony_ci{
28cc290419Sopenharmony_ci    DIR *dir = ::opendir(path.c_str());
29cc290419Sopenharmony_ci    if (dir == nullptr) {
30cc290419Sopenharmony_ci        WRITE_LOG(LOG_WARN, "%s: cannot open devpath: errno: %d\n", path.c_str(), errno);
31cc290419Sopenharmony_ci        return "";
32cc290419Sopenharmony_ci    }
33cc290419Sopenharmony_ci
34cc290419Sopenharmony_ci    std::string res = USB_FFS_BASE;
35cc290419Sopenharmony_ci    std::string node;
36cc290419Sopenharmony_ci    int count = 0;
37cc290419Sopenharmony_ci    struct dirent *entry = nullptr;
38cc290419Sopenharmony_ci    while ((entry = ::readdir(dir))) {
39cc290419Sopenharmony_ci        if (*entry->d_name == '.') {
40cc290419Sopenharmony_ci            continue;
41cc290419Sopenharmony_ci        }
42cc290419Sopenharmony_ci        node = entry->d_name;
43cc290419Sopenharmony_ci        ++count;
44cc290419Sopenharmony_ci    }
45cc290419Sopenharmony_ci    if (count > 1) {
46cc290419Sopenharmony_ci        res += "hdc";
47cc290419Sopenharmony_ci    } else {
48cc290419Sopenharmony_ci        res += node;
49cc290419Sopenharmony_ci    }
50cc290419Sopenharmony_ci    ::closedir(dir);
51cc290419Sopenharmony_ci    return res;
52cc290419Sopenharmony_ci}
53cc290419Sopenharmony_ci
54