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 #include <errno.h>
20 #include <fcntl.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 #include "log.h"
25
26 namespace rootcanal {
27
WriteSafely(AsyncDataChannel * socket,const uint8_t * data,size_t length)28 size_t HciProtocol::WriteSafely(AsyncDataChannel* socket, const uint8_t* data,
29 size_t length) {
30 size_t transmitted_length = 0;
31 while (length > 0) {
32 ssize_t ret = socket->Send(data + transmitted_length, length);
33
34 if (ret == -1) {
35 if (errno == EAGAIN) {
36 continue;
37 }
38 LOG_ERROR("%s error writing to UART (%s)", __func__, strerror(errno));
39 break;
40 }
41 if (ret == 0) {
42 // Nothing written :(
43 LOG_ERROR("%s zero bytes written - something went wrong...", __func__);
44 break;
45 }
46
47 transmitted_length += ret;
48 length -= ret;
49 }
50
51 return transmitted_length;
52 }
53
54 } // namespace rootcanal
55