• 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 "ffi_utils.h"
16 #include "uart.h"
17 
18 #include <string>
19 
20 namespace Hdc {
21 
GetUartSpeedExt(int32_t speed)22 extern "C" int32_t GetUartSpeedExt(int32_t speed)
23 {
24     return static_cast<int32_t>(GetUartSpeed(static_cast<int>(speed)));
25 }
26 
GetUartBitsExt(int32_t bits)27 extern "C" int32_t GetUartBitsExt(int32_t bits)
28 {
29     return static_cast<int32_t>(GetUartBits(static_cast<int>(bits)));
30 }
31 
OpenSerialPortExt(const char * portName)32 extern "C" int32_t OpenSerialPortExt(const char* portName)
33 {
34     return static_cast<int32_t>(OpenSerialPort(std::string(portName)));
35 }
36 
SetSerialExt(int32_t fd,int32_t nSpeed,int32_t nBits,uint8_t nEvent,int32_t nStop)37 extern "C" int32_t SetSerialExt(int32_t fd, int32_t nSpeed, int32_t nBits, uint8_t nEvent, int32_t nStop)
38 {
39     return static_cast<int32_t>(SetSerial(static_cast<int>(fd), static_cast<int>(nSpeed),
40                                           static_cast<int>(nBits), static_cast<char>(nEvent),
41                                           static_cast<int>(nStop)));
42 }
43 
ReadUartDevExt(int32_t fd,uint32_t expectedSize)44 extern "C" SerializedBuffer ReadUartDevExt(int32_t fd, uint32_t expectedSize)
45 {
46     std::vector<uint8_t> readBuf;
47     ssize_t length = 0;
48     while (length == 0) {
49         length = ReadUartDev(static_cast<int>(fd), readBuf, static_cast<size_t>(expectedSize));
50     }
51 
52     char *bufRet = new char;
53     (void)memset_s(bufRet, length, 0, length);
54     if (memcpy_s(bufRet, length, readBuf.data(), length) != EOK) {
55         return SerializedBuffer{bufRet, static_cast<uint64_t>(length)};
56     }
57     return SerializedBuffer{bufRet, static_cast<uint64_t>(length)};
58 }
59 
WriteUartDevExt(int32_t fd,SerializedBuffer buf)60 extern "C" int32_t WriteUartDevExt(int32_t fd, SerializedBuffer buf)
61 {
62     return static_cast<int32_t>(WriteUartDev(static_cast<int>(fd), reinterpret_cast<uint8_t *>(buf.ptr),
63                                              static_cast<size_t>(buf.size)));
64 }
65 
CloseSerialPortExt(int32_t fd)66 extern "C" uint8_t CloseSerialPortExt(int32_t fd)
67 {
68     return static_cast<uint8_t>(CloseSerialPort(fd));
69 }
70 
71 }
72