aboutsummaryrefslogtreecommitdiff
path: root/vendor_libs/test_vendor_lib/model/devices/hci_socket_device.cc
blob: d86dc41a1886627eb913dd655a4bba5577392a24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "hci_socket_device"

#include "hci_socket_device.h"

#include <fcntl.h>
#include <netinet/in.h>
#include <sys/socket.h>

#include "osi/include/log.h"

#include "model/setup/device_boutique.h"

using std::vector;

namespace test_vendor_lib {

HciSocketDevice::HciSocketDevice(int file_descriptor) : socket_file_descriptor_(file_descriptor) {
  advertising_interval_ms_ = std::chrono::milliseconds(1000);

  page_scan_delay_ms_ = std::chrono::milliseconds(600);

  properties_.SetPageScanRepetitionMode(0);
  properties_.SetClassOfDevice(0x600420);
  properties_.SetExtendedInquiryData({
      16,  // length
      9,   // Type: Device Name
      'g',
      'D',
      'e',
      'v',
      'i',
      'c',
      'e',
      '-',
      'h',
      'c',
      'i',
      '_',
      'n',
      'e',
      't',
  });
  properties_.SetName({
      'g',
      'D',
      'e',
      'v',
      'i',
      'c',
      'e',
      '-',
      'H',
      'C',
      'I',
      '_',
      'N',
      'e',
      't',
  });

  h4_ = hci::H4Packetizer(
      socket_file_descriptor_,
      [this](const std::vector<uint8_t>& raw_command) {
        std::shared_ptr<std::vector<uint8_t>> packet_copy = std::make_shared<std::vector<uint8_t>>(raw_command);
        HandleCommand(packet_copy);
      },
      [](const std::vector<uint8_t>&) {
        CHECK(false) << "Unexpected Event in HciSocketDevice!";
      },
      [this](const std::vector<uint8_t>& raw_acl) {
        std::shared_ptr<std::vector<uint8_t>> packet_copy = std::make_shared<std::vector<uint8_t>>(raw_acl);
        HandleAcl(packet_copy);
      },
      [this](const std::vector<uint8_t>& raw_sco) {
        std::shared_ptr<std::vector<uint8_t>> packet_copy = std::make_shared<std::vector<uint8_t>>(raw_sco);
        HandleSco(packet_copy);
      },
      [this]() {
        LOG_INFO(LOG_TAG, "HCI socket device disconnected");
        close_callback_();
      });

  RegisterEventChannel([this](std::shared_ptr<std::vector<uint8_t>> packet) {
    SendHci(hci::PacketType::EVENT, packet);
  });
  RegisterAclChannel([this](std::shared_ptr<std::vector<uint8_t>> packet) {
    SendHci(hci::PacketType::ACL, packet);
  });
  RegisterScoChannel([this](std::shared_ptr<std::vector<uint8_t>> packet) {
    SendHci(hci::PacketType::SCO, packet);
  });
}

void HciSocketDevice::TimerTick() {
  h4_.OnDataReady(socket_file_descriptor_);
  DualModeController::TimerTick();
}

void HciSocketDevice::SendHci(hci::PacketType packet_type, const std::shared_ptr<std::vector<uint8_t>> packet) {
  if (socket_file_descriptor_ == -1) {
    LOG_INFO(LOG_TAG, "socket_file_descriptor == -1");
    return;
  }
  uint8_t type = static_cast<uint8_t>(packet_type);
  int bytes_written;
  bytes_written = write(socket_file_descriptor_, &type, sizeof(type));
  if (bytes_written != sizeof(type)) {
    LOG_WARN(LOG_TAG, "bytes_written %d != sizeof(type)", bytes_written);
  }
  bytes_written = write(socket_file_descriptor_, packet->data(), packet->size());
  if (static_cast<size_t>(bytes_written) != packet->size()) {
    LOG_WARN(LOG_TAG, "bytes_written %d != packet->size", bytes_written);
  }
}

void HciSocketDevice::RegisterCloseCallback(
    std::function<void()> close_callback) {
  close_callback_ = close_callback;
}

}  // namespace test_vendor_lib