• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 #include <cstdarg>
16 #include "securec.h"
17 #include "usb_util.h"
18 
GetDevPath(const std::string & path)19 std::string GetDevPath(const std::string &path)
20 {
21     DIR *dir = ::opendir(path.c_str());
22     if (dir == nullptr) {
23         printf("%s: cannot open devpath: errno: %d\n", path.c_str(), errno);
24         return "";
25     }
26 
27     std::string res = Hdc::USB_FFS_BASE;
28     std::string node;
29     int count = 0;
30     struct dirent *entry = nullptr;
31     while ((entry = ::readdir(dir))) {
32         if (*entry->d_name == '.') {
33             continue;
34         }
35         node = entry->d_name;
36         ++count;
37     }
38     if (count > 1) {
39         res += "hdc";
40     } else {
41         res += node;
42     }
43     ::closedir(dir);
44     return res;
45 }
46 
BuildPacketHeader(uint32_t sessionId,uint8_t option,uint32_t data_size)47 std::vector<uint8_t> BuildPacketHeader(uint32_t sessionId, uint8_t option, uint32_t data_size)
48 {
49     std::vector<uint8_t> vecData;
50     USBHead head;
51     head.sessionId = htonl(sessionId);
52     for (size_t i = 0; i < sizeof(head.flag); i++) {
53         head.flag[i] = USB_PACKET_FLAG.data()[i];
54     }
55     head.option = option;
56     head.data_size = htonl(data_size);
57     vecData.insert(vecData.end(), (uint8_t *)&head, (uint8_t *)&head + sizeof(USBHead));
58     return vecData;
59 }
60 
StringFormat(const char * const formater,va_list & vaArgs)61 const std::string StringFormat(const char * const formater, va_list &vaArgs)
62 {
63     std::vector<char> args(MAX_SIZE_IOBUF);
64     const int retSize = vsnprintf_s(args.data(), MAX_SIZE_IOBUF, MAX_SIZE_IOBUF - 1, formater, vaArgs);
65     if (retSize < 0) {
66         return std::string("");
67     } else {
68         return std::string(args.data(), retSize);
69     }
70 }
71 
StringFormat(const char * const formater,...)72 const std::string StringFormat(const char * const formater, ...)
73 {
74     va_list vaArgs;
75     va_start(vaArgs, formater);
76     std::string ret = StringFormat(formater, vaArgs);
77     va_end(vaArgs);
78     return ret;
79 }
80 
RunPipeComand(const char * cmdString,char * outBuf,uint16_t sizeOutBuf,bool ignoreTailLf)81 bool RunPipeComand(const char *cmdString, char *outBuf, uint16_t sizeOutBuf, bool ignoreTailLf)
82 {
83     FILE *pipeHandle = popen(cmdString, "r");
84     if (pipeHandle == nullptr) {
85         return false;
86     }
87     int bytesRead = 0;
88     int bytesOnce = 0;
89     while (!feof(pipeHandle)) {
90         bytesOnce = fread(outBuf, 1, sizeOutBuf - bytesRead, pipeHandle);
91         if (bytesOnce <= 0) {
92             break;
93         }
94         bytesRead += bytesOnce;
95     }
96     if (bytesRead && ignoreTailLf) {
97         if (outBuf[bytesRead - 1] == '\n') {
98             outBuf[bytesRead - 1] = '\0';
99         }
100     }
101     pclose(pipeHandle);
102     return bytesRead;
103 }
104 
SetDevItem(const char * key,const char * value)105 bool SetDevItem(const char *key, const char *value)
106 {
107     char outBuf[256] = "";
108     std::string stringBuf = StringFormat("param set %s %s", key, value);
109     RunPipeComand(stringBuf.c_str(), outBuf, sizeof(outBuf), true);
110     return true;
111 }
112