Home | History | Annotate | Download | only in default
      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 <fcntl.h>
     22 #include <log/log.h>
     23 
     24 namespace android {
     25 namespace hardware {
     26 namespace bluetooth {
     27 namespace hci {
     28 
     29 size_t HciProtocol::WriteSafely(int fd, const uint8_t* data, size_t length) {
     30   size_t transmitted_length = 0;
     31   while (length > 0) {
     32     ssize_t ret =
     33         TEMP_FAILURE_RETRY(write(fd, data + transmitted_length, length));
     34 
     35     if (ret == -1) {
     36       if (errno == EAGAIN) continue;
     37       ALOGE("%s error writing to UART (%s)", __func__, strerror(errno));
     38       break;
     39 
     40     } else if (ret == 0) {
     41       // Nothing written :(
     42       ALOGE("%s zero bytes written - something went wrong...", __func__);
     43       break;
     44     }
     45 
     46     transmitted_length += ret;
     47     length -= ret;
     48   }
     49 
     50   return transmitted_length;
     51 }
     52 
     53 }  // namespace hci
     54 }  // namespace bluetooth
     55 }  // namespace hardware
     56 }  // namespace android
     57