• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #ifndef SERIAL_MANAGER_H
17 #define SERIAL_MANAGER_H
18 
19 #include <algorithm>
20 #include <map>
21 #include <mutex>
22 #include <string>
23 #include <vector>
24 #include "v1_0/iserial_interface.h"
25 #include <ipc_skeleton.h>
26 #include "usb_right_manager.h"
27 
28 namespace OHOS {
29 namespace SERIAL {
30 class SerialManager {
31 public:
32     SerialManager();
33     ~SerialManager();
34 
35     int32_t SerialOpen(int32_t portId);
36     int32_t SerialClose(int32_t portId);
37     int32_t SerialRead(int32_t portId, std::vector<uint8_t>& data, uint32_t size,
38         uint32_t &actualSize, uint32_t timeout);
39     int32_t SerialWrite(int32_t portId, const std::vector<uint8_t>& data, uint32_t size,
40         uint32_t &actualSize, uint32_t timeout);
41     int32_t SerialGetAttribute(int32_t portId, OHOS::HDI::Usb::Serial::V1_0::SerialAttribute& attribute);
42     int32_t SerialSetAttribute(int32_t portId, const OHOS::HDI::Usb::Serial::V1_0::SerialAttribute& attribute);
43     int32_t SerialGetPortList(std::vector<OHOS::HDI::Usb::Serial::V1_0::SerialPort>& serialPortList);
44     void SerialPortListDump(int32_t fd, const std::vector<std::string>& args);
45     void ListGetDumpHelp(int32_t fd);
46     void SerialGetAttributeDump(int32_t fd, const std::vector<std::string>& args);
47     bool IsPortIdExist(int32_t portId);
48     void FreeTokenId(int32_t portId, uint32_t tokenId);
49     bool GetSerialPort(int32_t portId, OHOS::HDI::Usb::Serial::V1_0::SerialPort& serialPort);
50     bool CheckDataAndProcessPortId(int32_t fd, const std::vector<std::string>& args,
51         OHOS::HDI::Usb::Serial::V1_0::SerialAttribute attribute);
52 private:
53     bool IsPortStatus(int32_t portId);
54     bool CheckTokenIdValidity(int32_t portId);
55     void UpdateSerialPortMap(std::vector<OHOS::HDI::Usb::Serial::V1_0::SerialPort>& serialPortList);
56     int32_t CheckPortAndTokenId(int32_t portId);
57     void ReportSerialOperateSysEvent(std::string interfaceName, int32_t portId, uint32_t tokenId);
58     void ReportSerialOperateSetAttributeSysEvent(int32_t portId, uint32_t tokenId,
59         const OHOS::HDI::Usb::Serial::V1_0::SerialAttribute& attribute);
60     std::map<int32_t, uint32_t> portTokenMap_;
61     std::map<int32_t, OHOS::HDI::Usb::Serial::V1_0::SerialPort> serialPortMap_;
62     sptr<OHOS::HDI::Usb::Serial::V1_0::ISerialInterface> serial_ = nullptr;
63     std::shared_ptr<USB::UsbRightManager> usbRightManager_;
64     std::mutex serialPortMapMutex_;
65 };
66 } // namespace SERIAL
67 } // namespace OHOS
68 
69 #endif //SERIAL_MANAGER_H
70