1 /*
2  * Copyright (C) 2021 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 LOG_TAG
16 #define LOG_TAG "bt_fwk_socket_inutputstream"
17 #endif
18 
19 #include <unistd.h>
20 #include <cstddef>
21 #include "bluetooth_log.h"
22 #include "bluetooth_socket_inputstream.h"
23 #include <cerrno>
24 #include "sys/socket.h"
25 
26 namespace OHOS {
27 namespace Bluetooth {
InputStream(int socketFd)28 InputStream::InputStream(int socketFd) : socketFd_(socketFd)
29 {}
30 
~InputStream()31 InputStream::~InputStream()
32 {}
33 
Read(uint8_t * buf,size_t length)34 ssize_t InputStream::Read(uint8_t *buf, size_t length)
35 {
36     if (socketFd_ == -1) {
37         HILOGE("socket closed");
38         return -1;
39     }
40 
41     auto ret = recv(socketFd_, buf, length, MSG_NOSIGNAL);
42 
43     HILOGD("ret:%{public}zd", ret);
44 
45     if (ret <= 0) {
46         HILOGE("socket read exception! ret:%{public}zd errno:%{public}d", ret, errno);
47     }
48     return ret;
49 }
50 }  // namespace Bluetooth
51 }  // namespace OHOS