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 #ifndef LINUX_SERIAL_H 16 #define LINUX_SERIAL_H 17 18 #include <mutex> 19 #include <atomic> 20 #include <string> 21 #include <vector> 22 #include <termios.h> 23 #include <thread> 24 #include <map> 25 #include "v1_0/serial_types.h" 26 #include "serial_sysfs_device.h" 27 #include "hdf_dlist.h" 28 #include "hdf_io_service_if.h" 29 #include "hdf_sbuf.h" 30 #include "osal_mem.h" 31 #include "osal_mutex.h" 32 33 namespace OHOS { 34 namespace HDI { 35 namespace Usb { 36 namespace Serial { 37 namespace V1_0 { 38 39 struct Serialfd { 40 int32_t fd; 41 int32_t portId; 42 }; 43 44 struct UsbDdkDeviceInfo { 45 struct OsalMutex deviceMutex; 46 struct DListHead list; 47 struct UsbPnpNotifyMatchInfoTable info; 48 }; 49 50 class LinuxSerial { 51 public: 52 static LinuxSerial &GetInstance(); 53 int32_t SerialOpen(int32_t portId); 54 int32_t SerialClose(int32_t portId); 55 int32_t SerialGetPortList(std::vector<SerialPort>& portIds); 56 int32_t SerialRead(int32_t portId, std::vector<uint8_t>& data, uint32_t size, uint32_t timeout); 57 int32_t SerialWrite(int32_t portId, const std::vector<uint8_t>& data, uint32_t size, uint32_t timeout); 58 int32_t SerialGetAttribute(int32_t portId, struct SerialAttribute& attribute); 59 int32_t SerialSetAttribute(int32_t portId, const struct SerialAttribute& attribute); 60 61 private: 62 LinuxSerial(); 63 ~LinuxSerial(); 64 65 int32_t GetFdByPortId(int32_t portId); 66 int32_t GetBaudrate(unsigned int baudrate); 67 int32_t GetDatabits(unsigned char dataBits, tcflag_t& cflag); 68 int32_t GetParity(tcflag_t& cflag, unsigned char parity); 69 int32_t GetStopbits(tcflag_t& cflag, unsigned char stopBits); 70 void HandleDevListEntry(struct UsbPnpNotifyMatchInfoTable *device, std::vector<SerialPort>& portIds, 71 std::string targetPath); 72 private: 73 std::mutex portMutex_; 74 std::vector<Serialfd> serialPortList_; 75 struct termios options_ = {}; 76 }; 77 } // V1_0 78 } // Serial 79 } // Usb 80 } // HDI 81 } // OHOS 82 83 #endif // LINUX_SERIAL_H