blob: a818f747595a63470704e85ee766cfa77ce1f6f2 [file] [log] [blame]
Ian Coolidge611fcf92015-06-03 17:20:30 -07001//
2// Copyright (C) 2015 Google, Inc.
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#include <errno.h>
17#include <stdio.h>
18#include <stdlib.h>
19
20#define LOG_TAG "BtHost"
21#include "osi/include/log.h"
22// For system properties
23// TODO(icoolidge): abstraction or non-cutils stub.
24#include <cutils/properties.h>
25// For init socket environment variable decode
26// TODO(icoolidge): abstraction or remove.
27#include <cutils/sockets.h>
28
29#include "core_stack.h"
30#include "host.h"
31
32namespace {
33
34const char kDisableProperty[] = "persist.bluetooth.disable";
35const char kSocketFromInit[] = "bluetooth";
36
37} // namespace
38
39int main() {
40 char disable_value[PROPERTY_VALUE_MAX];
41 int status = property_get(kDisableProperty, disable_value, nullptr);
42 if (status && !strcmp(disable_value, "1")) {
43 LOG_INFO("service disabled");
44 return EXIT_SUCCESS;
45 }
46
47 int server_socket = android_get_control_socket(kSocketFromInit);
48 if (server_socket == -1) {
49 LOG_ERROR("failed to get socket from init");
50 return EXIT_FAILURE;
51 }
52
53 status = listen(server_socket, SOMAXCONN);
54 if (status == -1) {
55 LOG_ERROR("listen failed: %s", strerror(errno));
56 return EXIT_FAILURE;
57 }
58
59 bluetooth::CoreStack bt;
60 bt.Initialize();
61
62 // TODO(icoolidge): accept simultaneous clients
63 while (true) {
64 int client_socket = accept4(server_socket, nullptr, nullptr, SOCK_NONBLOCK);
65 if (status == -1) {
66 LOG_ERROR("accept failed: %s", strerror(errno));
67 return EXIT_FAILURE;
68 }
69
70 LOG_INFO("client connected: %d", client_socket);
71 bluetooth::Host bluetooth_host(client_socket, &bt);
72 bluetooth_host.EventLoop();
73 }
74
75 close(server_socket);
76 return EXIT_SUCCESS;
77}