12322a670394db095f145519554ba6539ed228975Robert Ginda/*
22322a670394db095f145519554ba6539ed228975Robert Ginda * Copyright (C) 2015 The Android Open Source Project
32322a670394db095f145519554ba6539ed228975Robert Ginda *
42322a670394db095f145519554ba6539ed228975Robert Ginda * Licensed under the Apache License, Version 2.0 (the "License");
52322a670394db095f145519554ba6539ed228975Robert Ginda * you may not use this file except in compliance with the License.
62322a670394db095f145519554ba6539ed228975Robert Ginda * You may obtain a copy of the License at
72322a670394db095f145519554ba6539ed228975Robert Ginda *
82322a670394db095f145519554ba6539ed228975Robert Ginda *      http://www.apache.org/licenses/LICENSE-2.0
92322a670394db095f145519554ba6539ed228975Robert Ginda *
102322a670394db095f145519554ba6539ed228975Robert Ginda * Unless required by applicable law or agreed to in writing, software
112322a670394db095f145519554ba6539ed228975Robert Ginda * distributed under the License is distributed on an "AS IS" BASIS,
122322a670394db095f145519554ba6539ed228975Robert Ginda * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132322a670394db095f145519554ba6539ed228975Robert Ginda * See the License for the specific language governing permissions and
142322a670394db095f145519554ba6539ed228975Robert Ginda * limitations under the License.
152322a670394db095f145519554ba6539ed228975Robert Ginda */
162322a670394db095f145519554ba6539ed228975Robert Ginda
172322a670394db095f145519554ba6539ed228975Robert Ginda#include "buffet/flouride_socket_bluetooth_client.h"
182322a670394db095f145519554ba6539ed228975Robert Ginda
192322a670394db095f145519554ba6539ed228975Robert Ginda#include <sys/socket.h>
202322a670394db095f145519554ba6539ed228975Robert Ginda#include <sys/un.h>
212322a670394db095f145519554ba6539ed228975Robert Ginda
224170585fe75d99036883229081420f2972dd4ec1Alex Vakulenko#include <brillo/streams/file_stream.h>
232322a670394db095f145519554ba6539ed228975Robert Ginda
242322a670394db095f145519554ba6539ed228975Robert Gindanamespace buffet {
252322a670394db095f145519554ba6539ed228975Robert Ginda
262322a670394db095f145519554ba6539ed228975Robert Gindaconst char kFlourideSocketPath[] = "/dev/socket/bluetooth";
272322a670394db095f145519554ba6539ed228975Robert Ginda
282322a670394db095f145519554ba6539ed228975Robert Gindastd::unique_ptr<BluetoothClient> BluetoothClient::CreateInstance() {
292322a670394db095f145519554ba6539ed228975Robert Ginda  return std::unique_ptr<BluetoothClient>{new FlourideSocketBluetoothClient};
302322a670394db095f145519554ba6539ed228975Robert Ginda}
312322a670394db095f145519554ba6539ed228975Robert Ginda
322322a670394db095f145519554ba6539ed228975Robert GindaFlourideSocketBluetoothClient::FlourideSocketBluetoothClient() {}
332322a670394db095f145519554ba6539ed228975Robert Ginda
342322a670394db095f145519554ba6539ed228975Robert GindaFlourideSocketBluetoothClient::~FlourideSocketBluetoothClient() {}
352322a670394db095f145519554ba6539ed228975Robert Ginda
362322a670394db095f145519554ba6539ed228975Robert Ginda// TODO(rginda): Call this from somewhere.
372322a670394db095f145519554ba6539ed228975Robert Gindabool FlourideSocketBluetoothClient::OpenSocket() {
382322a670394db095f145519554ba6539ed228975Robert Ginda  LOG(INFO) << "Opening: " << kFlourideSocketPath;
392322a670394db095f145519554ba6539ed228975Robert Ginda
402322a670394db095f145519554ba6539ed228975Robert Ginda  int socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);
412322a670394db095f145519554ba6539ed228975Robert Ginda  if (socket_fd < 0) {
422322a670394db095f145519554ba6539ed228975Robert Ginda    PLOG(ERROR) << "Failed to create domain socket: " << kFlourideSocketPath;
432322a670394db095f145519554ba6539ed228975Robert Ginda    return false;
442322a670394db095f145519554ba6539ed228975Robert Ginda  }
452322a670394db095f145519554ba6539ed228975Robert Ginda
462322a670394db095f145519554ba6539ed228975Robert Ginda  sockaddr_un addr{AF_UNIX};
472322a670394db095f145519554ba6539ed228975Robert Ginda  static_assert(sizeof(kFlourideSocketPath) <= sizeof(addr.sun_path),
482322a670394db095f145519554ba6539ed228975Robert Ginda                "kFlourideSocketPath too long");
492322a670394db095f145519554ba6539ed228975Robert Ginda  strncpy(addr.sun_path, kFlourideSocketPath, sizeof(addr.sun_path));
502322a670394db095f145519554ba6539ed228975Robert Ginda  if (connect(socket_fd, reinterpret_cast<sockaddr *>(&addr),
512322a670394db095f145519554ba6539ed228975Robert Ginda              sizeof(sockaddr_un))) {
522322a670394db095f145519554ba6539ed228975Robert Ginda    PLOG(ERROR) << "Failed to connect to domain socket: "
532322a670394db095f145519554ba6539ed228975Robert Ginda                << kFlourideSocketPath;
542322a670394db095f145519554ba6539ed228975Robert Ginda    close(socket_fd);
552322a670394db095f145519554ba6539ed228975Robert Ginda    return false;
562322a670394db095f145519554ba6539ed228975Robert Ginda  }
572322a670394db095f145519554ba6539ed228975Robert Ginda
584170585fe75d99036883229081420f2972dd4ec1Alex Vakulenko  stream_ = brillo::FileStream::FromFileDescriptor(socket_fd, true, nullptr);
592322a670394db095f145519554ba6539ed228975Robert Ginda  return true;
602322a670394db095f145519554ba6539ed228975Robert Ginda}
612322a670394db095f145519554ba6539ed228975Robert Ginda
622322a670394db095f145519554ba6539ed228975Robert Ginda}  // namespace buffet
63