1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker * Copyright (C) 2017 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker *
4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker *
8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker *
10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker */
16*38e8c45fSAndroid Build Coastguard Worker
17*38e8c45fSAndroid Build Coastguard Worker #include "include/serviceutils/PriorityDumper.h"
18*38e8c45fSAndroid Build Coastguard Worker
19*38e8c45fSAndroid Build Coastguard Worker namespace android {
20*38e8c45fSAndroid Build Coastguard Worker
21*38e8c45fSAndroid Build Coastguard Worker const char16_t PriorityDumper::PROTO_ARG[] = u"--proto";
22*38e8c45fSAndroid Build Coastguard Worker const char16_t PriorityDumper::PRIORITY_ARG[] = u"--dump-priority";
23*38e8c45fSAndroid Build Coastguard Worker const char16_t PriorityDumper::PRIORITY_ARG_CRITICAL[] = u"CRITICAL";
24*38e8c45fSAndroid Build Coastguard Worker const char16_t PriorityDumper::PRIORITY_ARG_HIGH[] = u"HIGH";
25*38e8c45fSAndroid Build Coastguard Worker const char16_t PriorityDumper::PRIORITY_ARG_NORMAL[] = u"NORMAL";
26*38e8c45fSAndroid Build Coastguard Worker
27*38e8c45fSAndroid Build Coastguard Worker enum class PriorityType { INVALID, CRITICAL, HIGH, NORMAL };
28*38e8c45fSAndroid Build Coastguard Worker
getPriorityType(const String16 & arg)29*38e8c45fSAndroid Build Coastguard Worker static PriorityType getPriorityType(const String16& arg) {
30*38e8c45fSAndroid Build Coastguard Worker if (arg == PriorityDumper::PRIORITY_ARG_CRITICAL) {
31*38e8c45fSAndroid Build Coastguard Worker return PriorityType::CRITICAL;
32*38e8c45fSAndroid Build Coastguard Worker } else if (arg == PriorityDumper::PRIORITY_ARG_HIGH) {
33*38e8c45fSAndroid Build Coastguard Worker return PriorityType::HIGH;
34*38e8c45fSAndroid Build Coastguard Worker } else if (arg == PriorityDumper::PRIORITY_ARG_NORMAL) {
35*38e8c45fSAndroid Build Coastguard Worker return PriorityType::NORMAL;
36*38e8c45fSAndroid Build Coastguard Worker }
37*38e8c45fSAndroid Build Coastguard Worker return PriorityType::INVALID;
38*38e8c45fSAndroid Build Coastguard Worker }
39*38e8c45fSAndroid Build Coastguard Worker
dumpAll(int fd,const Vector<String16> & args,bool asProto)40*38e8c45fSAndroid Build Coastguard Worker status_t PriorityDumper::dumpAll(int fd, const Vector<String16>& args, bool asProto) {
41*38e8c45fSAndroid Build Coastguard Worker status_t status;
42*38e8c45fSAndroid Build Coastguard Worker status = dumpCritical(fd, args, asProto);
43*38e8c45fSAndroid Build Coastguard Worker if (status != OK) return status;
44*38e8c45fSAndroid Build Coastguard Worker status = dumpHigh(fd, args, asProto);
45*38e8c45fSAndroid Build Coastguard Worker if (status != OK) return status;
46*38e8c45fSAndroid Build Coastguard Worker status = dumpNormal(fd, args, asProto);
47*38e8c45fSAndroid Build Coastguard Worker if (status != OK) return status;
48*38e8c45fSAndroid Build Coastguard Worker return status;
49*38e8c45fSAndroid Build Coastguard Worker }
50*38e8c45fSAndroid Build Coastguard Worker
priorityDump(int fd,const Vector<String16> & args)51*38e8c45fSAndroid Build Coastguard Worker status_t PriorityDumper::priorityDump(int fd, const Vector<String16>& args) {
52*38e8c45fSAndroid Build Coastguard Worker status_t status;
53*38e8c45fSAndroid Build Coastguard Worker bool asProto = false;
54*38e8c45fSAndroid Build Coastguard Worker PriorityType priority = PriorityType::INVALID;
55*38e8c45fSAndroid Build Coastguard Worker
56*38e8c45fSAndroid Build Coastguard Worker Vector<String16> strippedArgs;
57*38e8c45fSAndroid Build Coastguard Worker for (uint32_t argIndex = 0; argIndex < args.size(); argIndex++) {
58*38e8c45fSAndroid Build Coastguard Worker if (args[argIndex] == PROTO_ARG) {
59*38e8c45fSAndroid Build Coastguard Worker asProto = true;
60*38e8c45fSAndroid Build Coastguard Worker } else if (args[argIndex] == PRIORITY_ARG) {
61*38e8c45fSAndroid Build Coastguard Worker if (argIndex + 1 < args.size()) {
62*38e8c45fSAndroid Build Coastguard Worker argIndex++;
63*38e8c45fSAndroid Build Coastguard Worker priority = getPriorityType(args[argIndex]);
64*38e8c45fSAndroid Build Coastguard Worker }
65*38e8c45fSAndroid Build Coastguard Worker } else {
66*38e8c45fSAndroid Build Coastguard Worker strippedArgs.add(args[argIndex]);
67*38e8c45fSAndroid Build Coastguard Worker }
68*38e8c45fSAndroid Build Coastguard Worker }
69*38e8c45fSAndroid Build Coastguard Worker
70*38e8c45fSAndroid Build Coastguard Worker switch (priority) {
71*38e8c45fSAndroid Build Coastguard Worker case PriorityType::CRITICAL:
72*38e8c45fSAndroid Build Coastguard Worker status = dumpCritical(fd, strippedArgs, asProto);
73*38e8c45fSAndroid Build Coastguard Worker break;
74*38e8c45fSAndroid Build Coastguard Worker case PriorityType::HIGH:
75*38e8c45fSAndroid Build Coastguard Worker status = dumpHigh(fd, strippedArgs, asProto);
76*38e8c45fSAndroid Build Coastguard Worker break;
77*38e8c45fSAndroid Build Coastguard Worker case PriorityType::NORMAL:
78*38e8c45fSAndroid Build Coastguard Worker status = dumpNormal(fd, strippedArgs, asProto);
79*38e8c45fSAndroid Build Coastguard Worker break;
80*38e8c45fSAndroid Build Coastguard Worker default:
81*38e8c45fSAndroid Build Coastguard Worker status = dumpAll(fd, strippedArgs, asProto);
82*38e8c45fSAndroid Build Coastguard Worker break;
83*38e8c45fSAndroid Build Coastguard Worker }
84*38e8c45fSAndroid Build Coastguard Worker return status;
85*38e8c45fSAndroid Build Coastguard Worker }
86*38e8c45fSAndroid Build Coastguard Worker } // namespace android
87