1 /* 2 * Copyright (c) 2024 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 "ddk_sysfs_device.h" 27 #include "ddk_device_manager.h" 28 #include "hdf_dlist.h" 29 #include "hdf_io_service_if.h" 30 #include "hdf_sbuf.h" 31 #include "osal_mem.h" 32 #include "osal_mutex.h" 33 #include "hdf_usb_pnp_manage.h" 34 35 namespace OHOS { 36 namespace HDI { 37 namespace Usb { 38 namespace Serial { 39 namespace V1_0 { 40 41 #ifdef __cplusplus 42 extern "C" { 43 #endif /* __cplusplus */ 44 45 int32_t DdkSysfsGetDevice(const char *deviceDir, struct UsbPnpNotifyMatchInfoTable *device); 46 int32_t DdkSysfsGetDevNodePath(DevInterfaceInfo *devInfo, const char *prefix, char *buff, uint32_t buffSize); 47 48 #ifdef __cplusplus 49 } 50 #endif /* __cplusplus */ 51 52 struct Serialfd { 53 int32_t fd; 54 int32_t portId; 55 }; 56 57 struct UsbDdkDeviceInfo { 58 struct OsalMutex deviceMutex; 59 struct DListHead list; 60 struct UsbPnpNotifyMatchInfoTable info; 61 }; 62 63 class LinuxSerial { 64 public: 65 static LinuxSerial &GetInstance(); 66 int32_t SerialOpen(int32_t portId); 67 int32_t SerialClose(int32_t portId); 68 int32_t SerialGetPortList(std::vector<SerialPort>& portIds); 69 int32_t SerialRead(int32_t portId, std::vector<uint8_t>& data, uint32_t size, uint32_t timeout); 70 int32_t SerialWrite(int32_t portId, const std::vector<uint8_t>& data, uint32_t size, uint32_t timeout); 71 int32_t SerialGetAttribute(int32_t portId, struct SerialAttribute& attribute); 72 int32_t SerialSetAttribute(int32_t portId, const struct SerialAttribute& attribute); 73 74 private: 75 LinuxSerial(); 76 ~LinuxSerial(); 77 78 int32_t GetFdByPortId(int32_t portId); 79 int32_t GetBaudrate(unsigned int baudrate); 80 tcflag_t GetDatabits(unsigned char dataBits); 81 tcflag_t GetParity(tcflag_t c_cflag, unsigned char parity); 82 tcflag_t GetStopbits(tcflag_t c_cflag, unsigned char stopBits); 83 void HandleDevListEntry(struct UsbPnpNotifyMatchInfoTable *device, std::vector<SerialPort>& portIds); 84 private: 85 std::mutex portMutex_; 86 std::vector<Serialfd> serialPortList_; 87 struct termios options_; 88 }; 89 } // V1_0 90 } // Serial 91 } // Usb 92 } // HDI 93 } // OHOS 94 95 #endif // LINUX_SERIAL_H