1 //
2 // Copyright 2017 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #include "hci_protocol.h"
18 
19 #define LOG_TAG "android.hardware.bluetooth-hci-hci_protocol"
20 #include <assert.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <log/log.h>
24 #include <unistd.h>
25 
26 namespace android {
27 namespace hardware {
28 namespace bluetooth {
29 namespace hci {
30 
WriteSafely(int fd,const uint8_t * data,size_t length)31 size_t HciProtocol::WriteSafely(int fd, const uint8_t* data, size_t length) {
32   size_t transmitted_length = 0;
33   while (length > 0) {
34     ssize_t ret =
35         TEMP_FAILURE_RETRY(write(fd, data + transmitted_length, length));
36 
37     if (ret == -1) {
38       if (errno == EAGAIN) continue;
39       ALOGE("%s error writing to UART (%s)", __func__, strerror(errno));
40       break;
41 
42     } else if (ret == 0) {
43       // Nothing written :(
44       ALOGE("%s zero bytes written - something went wrong...", __func__);
45       break;
46     }
47 
48     transmitted_length += ret;
49     length -= ret;
50   }
51 
52   return transmitted_length;
53 }
54 
55 }  // namespace hci
56 }  // namespace bluetooth
57 }  // namespace hardware
58 }  // namespace android
59