1 /******************************************************************************
2  *
3  *  Copyright (C) 2022 Google, Inc.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 #include "hal/syscall_wrapper_impl.h"
20 
21 #include <unistd.h>
22 
23 #include <cerrno>
24 
25 namespace bluetooth {
26 namespace hal {
27 
Socket(int domain,int type,int protocol)28 int SyscallWrapperImpl::Socket(int domain, int type, int protocol) {
29   int ret = socket(domain, type, protocol);
30   errno_ = errno;
31   return ret;
32 }
33 
Bind(int fd,const struct sockaddr * addr,socklen_t len)34 int SyscallWrapperImpl::Bind(int fd, const struct sockaddr* addr, socklen_t len) {
35   int ret = bind(fd, addr, len);
36   errno_ = errno;
37   return ret;
38 }
39 
Connect(int fd,const struct sockaddr * addr,socklen_t len)40 int SyscallWrapperImpl::Connect(int fd, const struct sockaddr* addr, socklen_t len) {
41   int ret = connect(fd, addr, len);
42   errno_ = errno;
43   return ret;
44 }
45 
Send(int fd,const void * buf,size_t n,int flags)46 ssize_t SyscallWrapperImpl::Send(int fd, const void* buf, size_t n, int flags) {
47   int ret = send(fd, buf, n, flags);
48   errno_ = errno;
49   return ret;
50 }
51 
Recv(int fd,void * buf,size_t n,int flags)52 ssize_t SyscallWrapperImpl::Recv(int fd, void* buf, size_t n, int flags) {
53   int ret = recv(fd, buf, n, flags);
54   errno_ = errno;
55   return ret;
56 }
57 
Setsockopt(int fd,int level,int optname,const void * optval,socklen_t optlen)58 int SyscallWrapperImpl::Setsockopt(int fd, int level, int optname, const void* optval, socklen_t optlen) {
59   int ret = setsockopt(fd, level, optname, optval, optlen);
60   errno_ = errno;
61   return ret;
62 }
63 
Listen(int fd,int n)64 int SyscallWrapperImpl::Listen(int fd, int n) {
65   int ret = listen(fd, n);
66   errno_ = errno;
67   return ret;
68 }
69 
Accept(int fd,struct sockaddr * addr,socklen_t * addr_len,int flags)70 int SyscallWrapperImpl::Accept(int fd, struct sockaddr* addr, socklen_t* addr_len, int flags) {
71   int ret = accept4(fd, addr, addr_len, flags);
72   errno_ = errno;
73   return ret;
74 }
75 
Write(int fd,const void * buf,size_t count)76 ssize_t SyscallWrapperImpl::Write(int fd, const void* buf, size_t count) {
77   ssize_t ret = write(fd, buf, count);
78   errno_ = errno;
79   return ret;
80 }
81 
Close(int fd)82 int SyscallWrapperImpl::Close(int fd) {
83   int ret = close(fd);
84   errno_ = errno;
85   return ret;
86 }
87 
Pipe2(int * pipefd,int flags)88 int SyscallWrapperImpl::Pipe2(int* pipefd, int flags) {
89   int ret = pipe2(pipefd, flags);
90   errno_ = errno;
91   return ret;
92 }
93 
GetErrno() const94 int SyscallWrapperImpl::GetErrno() const {
95   return errno_;
96 }
97 
FDSet(int fd,fd_set * set)98 void SyscallWrapperImpl::FDSet(int fd, fd_set* set) {
99   FD_SET(fd, set);
100 }
101 
FDClr(int fd,fd_set * set)102 void SyscallWrapperImpl::FDClr(int fd, fd_set* set) {
103   FD_CLR(fd, set);
104 }
105 
FDIsSet(int fd,fd_set * set)106 bool SyscallWrapperImpl::FDIsSet(int fd, fd_set* set) {
107   return FD_ISSET(fd, set);
108 }
109 
FDZero(fd_set * set)110 void SyscallWrapperImpl::FDZero(fd_set* set) {
111   FD_ZERO(set);
112 }
113 
Select(int __nfds,fd_set * __readfds,fd_set * __writefds,fd_set * __exceptfds,struct timeval * __timeout)114 int SyscallWrapperImpl::Select(
115     int __nfds, fd_set* __readfds, fd_set* __writefds, fd_set* __exceptfds, struct timeval* __timeout) {
116   int ret = select(__nfds, __readfds, __writefds, __exceptfds, __timeout);
117   errno_ = errno;
118   return ret;
119 }
120 
121 }  // namespace hal
122 }  // namespace bluetooth
123