1 /*
2  * Copyright 2020 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 "test/headless/get_options.h"
18 
19 #include <getopt.h>
20 #include <unistd.h>
21 
22 #include <list>
23 #include <sstream>
24 #include <string>
25 
26 #include "os/log.h"
27 #include "types/bluetooth/uuid.h"
28 #include "types/raw_address.h"
29 
30 namespace {
31 enum OptionType {
32   kOptionDevice = 0,
33   kOptionLoop = 1,
34   kOptionUuid = 2,
35   kOptionMsleep = 3,
36   kOptionStdErr = 4,
37   kOptionFlags = 5,
38   kOptionClear = 6,
39 };
40 
41 constexpr struct option long_options[] = {
42     {"device", required_argument, 0, 0},  // kOptionDevice
43     {"loop", required_argument, 0, 0},    // kOptionLoop/
44     {"uuid", required_argument, 0, 0},    // kOptionUuid
45     {"msleep", required_argument, 0, 0},  // kOptionMsleep
46     {"stderr", no_argument, 0, 0},        // kOptionStdErr
47     {"flags", required_argument, 0, 0},   // kOptionFlags
48     {"clear", no_argument, 0, 0},         // kOptionDevice
49     {0, 0, 0, 0}};
50 
51 const char* kShortArgs = "cd:l:u:";
52 
53 }  // namespace
54 
Usage() const55 void bluetooth::test::headless::GetOpt::Usage() const {
56   fprintf(stdout, "%s: Usage:\n", name_);
57   fprintf(stdout, "%s  -c  Clear logcat logs\n", name_);
58   fprintf(stdout,
59           "%s  --device=<device,>  Comma separated list of remote devices\n",
60           name_);
61   fprintf(stdout,
62           "%s  --flags=<flags,>  Comma separated list of gd init flags\n",
63           name_);
64   fprintf(stdout, "%s  --uuid=<uuid,>      Comma separated list of uuids\n",
65           name_);
66   fprintf(stdout, "%s  --loop=<loop>       Number of loops\n", name_);
67   fprintf(stdout, "%s  --msleep=<msecs>    Sleep msec between loops\n", name_);
68   fprintf(stdout, "%s  --stderr            Dump stderr to stdout\n", name_);
69   fflush(nullptr);
70 }
71 
ParseValue(char * optarg,std::list<std::string> & string_list)72 void bluetooth::test::headless::GetOpt::ParseValue(
73     char* optarg, std::list<std::string>& string_list) {
74   log::assert_that(optarg != nullptr, "assert failed: optarg != nullptr");
75   char* p = optarg;
76   char* pp = optarg;
77   while (*p != '\0') {
78     if (*p == ',') {
79       *p = 0;
80       string_list.push_back(std::string(pp));
81       pp = p + 1;
82     }
83     p++;
84   }
85   if (pp != p) string_list.push_back(std::string(pp));
86 }
87 
Split(std::string s)88 std::vector<std::string> bluetooth::test::headless::GetOpt::Split(
89     std::string s) {
90   std::stringstream ss(s);
91   std::vector<std::string> values;
92   std::string item;
93   while (std::getline(ss, item, '=')) {
94     values.push_back(item);
95   }
96   return values;
97 }
98 
ProcessOption(int option_index,char * optarg)99 void bluetooth::test::headless::GetOpt::ProcessOption(int option_index,
100                                                       char* optarg) {
101   std::list<std::string> string_list;
102   OptionType option_type = static_cast<OptionType>(option_index);
103 
104   switch (option_type) {
105     case kOptionDevice:
106       if (!optarg) return;
107       ParseValue(optarg, string_list);
108       for (auto& entry : string_list) {
109         if (RawAddress::IsValidAddress(entry)) {
110           RawAddress address;
111           RawAddress::FromString(entry, address);
112           device_.push_back(address);
113         }
114       }
115       break;
116     case kOptionLoop:
117       loop_ = std::stoul(optarg, nullptr, 0);
118       break;
119     case kOptionUuid:
120       if (!optarg) return;
121       ParseValue(optarg, string_list);
122       for (auto& entry : string_list) {
123         uuid_.push_back(
124             bluetooth::Uuid::From16Bit(std::stoul(entry.c_str(), nullptr, 0)));
125       }
126       break;
127     case kOptionMsleep:
128       if (!optarg) return;
129       msec_ = std::stoul(optarg, nullptr, 0);
130       break;
131     case kOptionStdErr:
132       close_stderr_ = false;
133       break;
134     case kOptionFlags:
135       if (!optarg) return;
136       ParseValue(optarg, string_list);
137       for (auto& flag : string_list) {
138         init_flags_.push_back(flag);
139       }
140       break;
141     case kOptionClear:
142       clear_logcat_ = true;
143       break;
144     default:
145       fflush(nullptr);
146       valid_ = false;
147       return;
148       break;
149   }
150 }
151 
ParseStackInitFlags()152 void bluetooth::test::headless::GetOpt::ParseStackInitFlags() {
153   if (init_flags_.size() == 0) return;
154 
155   log::assert_that(stack_init_flags_ == nullptr,
156                    "assert failed: stack_init_flags_ == nullptr");
157 
158   unsigned idx = 0;
159   stack_init_flags_ = (const char**)calloc(sizeof(char*), init_flags_.size());
160   for (const std::string& flag : init_flags_)
161     stack_init_flags_[idx++] = flag.c_str();
162   stack_init_flags_[idx] = nullptr;
163 }
164 
StackInitFlags() const165 const char** bluetooth::test::headless::GetOpt::StackInitFlags() const {
166   return stack_init_flags_;
167 }
168 
GetOpt(int argc,char ** argv)169 bluetooth::test::headless::GetOpt::GetOpt(int argc, char** argv)
170     : name_(argv[0]) {
171   while (1) {
172     int option_index = 0;
173     int c =
174         getopt_long_only(argc, argv, kShortArgs, long_options, &option_index);
175     if (c == -1) break;
176 
177     switch (c) {
178       case 0:
179         ProcessOption(static_cast<OptionType>(option_index), optarg);
180         break;
181       case '?':
182         Usage();
183         valid_ = false;
184         return;
185       default:
186         printf("?? getopt returned character code 0%o ??\n", c);
187     }
188   }
189 
190   while (optind < argc) {
191     non_options_.push_back(argv[optind++]);
192   }
193 
194   ParseStackInitFlags();
195 
196   fflush(nullptr);
197 }
198 
~GetOpt()199 bluetooth::test::headless::GetOpt::~GetOpt() { free(stack_init_flags_); }
200