• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <base/logging.h>
20 #include <getopt.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 
24 #include <list>
25 #include <string>
26 
27 #include "gd/os/log.h"
28 #include "types/bluetooth/uuid.h"
29 #include "types/raw_address.h"
30 
31 namespace {
32 enum OptionType {
33   kOptionDevice = 0,
34   kOptionLoop = 1,
35   kOptionUuid = 2,
36   kOptionMsleep = 3,
37   kOptionStdErr = 4,
38   kOptionFlags = 5,
39   kOptionClear = 6,
40 };
41 
42 constexpr struct option long_options[] = {
43     {"device", required_argument, 0, 0},  // kOptionDevice
44     {"loop", required_argument, 0, 0},    // kOptionLoop/
45     {"uuid", required_argument, 0, 0},    // kOptionUuid
46     {"msleep", required_argument, 0, 0},  // kOptionMsleep
47     {"stderr", no_argument, 0, 0},        // kOptionStdErr
48     {"flags", required_argument, 0, 0},   // kOptionFlags
49     {"clear", no_argument, 0, 0},         // kOptionDevice
50     {0, 0, 0, 0}};
51 
52 const char* kShortArgs = "cd:l:u:";
53 
54 }  // namespace
55 
Usage() const56 void bluetooth::test::headless::GetOpt::Usage() const {
57   fprintf(stdout, "%s: Usage:\n", name_);
58   fprintf(stdout, "%s  -c  Clear logcat logs\n", name_);
59   fprintf(stdout,
60           "%s  --device=<device,>  Comma separated list of remote devices\n",
61           name_);
62   fprintf(stdout,
63           "%s  --flags=<flags,>  Comma separated list of gd init flags\n",
64           name_);
65   fprintf(stdout, "%s  --uuid=<uuid,>      Comma separated list of uuids\n",
66           name_);
67   fprintf(stdout, "%s  --loop=<loop>       Number of loops\n", name_);
68   fprintf(stdout, "%s  --msleep=<msecs>    Sleep msec between loops\n", name_);
69   fprintf(stdout, "%s  --stderr            Dump stderr to stdout\n", name_);
70   fflush(nullptr);
71 }
72 
ParseValue(char * optarg,std::list<std::string> & string_list)73 void bluetooth::test::headless::GetOpt::ParseValue(
74     char* optarg, std::list<std::string>& string_list) {
75   CHECK(optarg != nullptr);
76   char* p = optarg;
77   char* pp = optarg;
78   while (*p != '\0') {
79     if (*p == ',') {
80       *p = 0;
81       string_list.push_back(std::string(pp));
82       pp = p + 1;
83     }
84     p++;
85   }
86   if (pp != p) string_list.push_back(std::string(pp));
87 }
88 
Split(std::string s)89 std::vector<std::string> bluetooth::test::headless::GetOpt::Split(
90     std::string s) {
91   std::stringstream ss(s);
92   std::vector<std::string> values;
93   std::string item;
94   while (std::getline(ss, item, '=')) {
95     values.push_back(item);
96   }
97   return values;
98 }
99 
ProcessOption(int option_index,char * optarg)100 void bluetooth::test::headless::GetOpt::ProcessOption(int option_index,
101                                                       char* optarg) {
102   std::list<std::string> string_list;
103   OptionType option_type = static_cast<OptionType>(option_index);
104 
105   switch (option_type) {
106     case kOptionDevice:
107       if (!optarg) return;
108       ParseValue(optarg, string_list);
109       for (auto& entry : string_list) {
110         if (RawAddress::IsValidAddress(entry)) {
111           RawAddress address;
112           RawAddress::FromString(entry, address);
113           device_.push_back(address);
114         }
115       }
116       break;
117     case kOptionLoop:
118       loop_ = std::stoul(optarg, nullptr, 0);
119       break;
120     case kOptionUuid:
121       if (!optarg) return;
122       ParseValue(optarg, string_list);
123       for (auto& entry : string_list) {
124         uuid_.push_back(
125             bluetooth::Uuid::From16Bit(std::stoul(entry.c_str(), nullptr, 0)));
126       }
127       break;
128     case kOptionMsleep:
129       if (!optarg) return;
130       msec_ = std::stoul(optarg, nullptr, 0);
131       break;
132     case kOptionStdErr:
133       close_stderr_ = false;
134       break;
135     case kOptionFlags:
136       if (!optarg) return;
137       ParseValue(optarg, string_list);
138       for (auto& flag : string_list) {
139         init_flags_.push_back(flag);
140       }
141       break;
142     case kOptionClear:
143       clear_logcat_ = true;
144       break;
145     default:
146       fflush(nullptr);
147       valid_ = false;
148       return;
149       break;
150   }
151 }
152 
ParseStackInitFlags()153 void bluetooth::test::headless::GetOpt::ParseStackInitFlags() {
154   if (init_flags_.size() == 0) return;
155 
156   ASSERT(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