1 /*
2  * Copyright 2019 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 "stack_manager.h"
18 
19 #include <netinet/in.h>
20 #include <sys/socket.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 #include <csignal>
24 #include <cstring>
25 #include <string>
26 #include <thread>
27 
28 #include "facade/grpc_root_server.h"
29 #include "grpc/grpc_module.h"
30 #include "hal/hci_hal.h"
31 #include "hal/hci_hal_host_rootcanal.h"
32 #include "hal/snoop_logger.h"
33 
34 using ::bluetooth::hal::HciHalHostRootcanalConfig;
35 using ::bluetooth::StackManager;
36 using ::bluetooth::grpc::GrpcModule;
37 using ::bluetooth::ModuleList;
38 using ::bluetooth::os::Thread;
39 
40 namespace {
41 ::bluetooth::facade::GrpcRootServer grpc_root_server;
42 
43 void interrupt_handler(int) {
44   grpc_root_server.StopServer();
45 }
46 }  // namespace
47 
48 // The entry point for the binary with libbluetooth + facades
49 int main(int argc, const char** argv) {
50   int root_server_port = 8897;
51   int grpc_port = 8899;
52   int signal_port = 8895;
53 
54   const std::string arg_grpc_root_server_port = "--root-server-port=";
55   const std::string arg_grpc_server_port = "--grpc-port=";
56   const std::string arg_rootcanal_port = "--rootcanal-port=";
57   const std::string arg_signal_port = "--signal-port=";
58   const std::string arg_btsnoop_path = "--btsnoop=";
59   std::string btsnoop_path;
60   for (int i = 1; i < argc; i++) {
61     std::string arg = argv[i];
62     if (arg.find(arg_grpc_root_server_port) == 0) {
63       auto port_number = arg.substr(arg_grpc_root_server_port.size());
64       root_server_port = std::stoi(port_number);
65     }
66     if (arg.find(arg_grpc_server_port) == 0) {
67       auto port_number = arg.substr(arg_grpc_server_port.size());
68       grpc_port = std::stoi(port_number);
69     }
70     if (arg.find(arg_rootcanal_port) == 0) {
71       auto port_number = arg.substr(arg_rootcanal_port.size());
72       HciHalHostRootcanalConfig::Get()->SetPort(std::stoi(port_number));
73     }
74     if (arg.find(arg_btsnoop_path) == 0) {
75       btsnoop_path = arg.substr(arg_btsnoop_path.size());
76       ::bluetooth::hal::SnoopLogger::SetFilePath(btsnoop_path);
77     }
78     if (arg.find(arg_signal_port) == 0) {
79       auto port_number = arg.substr(arg_signal_port.size());
80       signal_port = std::stoi(port_number);
81     }
82   }
83 
84   signal(SIGINT, interrupt_handler);
85   grpc_root_server.StartServer("0.0.0.0", root_server_port, grpc_port);
86   int tester_signal_socket = socket(AF_INET, SOCK_STREAM, 0);
87   struct sockaddr_in addr;
88   memset(&addr, 0, sizeof(addr));
89   addr.sin_family = AF_INET;
90   addr.sin_port = htons(signal_port);
91   addr.sin_addr.s_addr = htonl(INADDR_ANY);
92   connect(tester_signal_socket, (sockaddr*)&addr, sizeof(addr));
93   close(tester_signal_socket);
94   auto wait_thread = std::thread([] { grpc_root_server.RunGrpcLoop(); });
95   wait_thread.join();
96 
97   return 0;
98 }
99