1 /*
2  * Copyright (C) 2017 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 "include/serviceutils/PriorityDumper.h"
18 
19 namespace android {
20 
21 const char16_t PriorityDumper::PROTO_ARG[] = u"--proto";
22 const char16_t PriorityDumper::PRIORITY_ARG[] = u"--dump-priority";
23 const char16_t PriorityDumper::PRIORITY_ARG_CRITICAL[] = u"CRITICAL";
24 const char16_t PriorityDumper::PRIORITY_ARG_HIGH[] = u"HIGH";
25 const char16_t PriorityDumper::PRIORITY_ARG_NORMAL[] = u"NORMAL";
26 
27 enum class PriorityType { INVALID, CRITICAL, HIGH, NORMAL };
28 
getPriorityType(const String16 & arg)29 static PriorityType getPriorityType(const String16& arg) {
30     if (arg == PriorityDumper::PRIORITY_ARG_CRITICAL) {
31         return PriorityType::CRITICAL;
32     } else if (arg == PriorityDumper::PRIORITY_ARG_HIGH) {
33         return PriorityType::HIGH;
34     } else if (arg == PriorityDumper::PRIORITY_ARG_NORMAL) {
35         return PriorityType::NORMAL;
36     }
37     return PriorityType::INVALID;
38 }
39 
dumpAll(int fd,const Vector<String16> & args,bool asProto)40 status_t PriorityDumper::dumpAll(int fd, const Vector<String16>& args, bool asProto) {
41     status_t status;
42     status = dumpCritical(fd, args, asProto);
43     if (status != OK) return status;
44     status = dumpHigh(fd, args, asProto);
45     if (status != OK) return status;
46     status = dumpNormal(fd, args, asProto);
47     if (status != OK) return status;
48     return status;
49 }
50 
priorityDump(int fd,const Vector<String16> & args)51 status_t PriorityDumper::priorityDump(int fd, const Vector<String16>& args) {
52     status_t status;
53     bool asProto = false;
54     PriorityType priority = PriorityType::INVALID;
55 
56     Vector<String16> strippedArgs;
57     for (uint32_t argIndex = 0; argIndex < args.size(); argIndex++) {
58         if (args[argIndex] == PROTO_ARG) {
59             asProto = true;
60         } else if (args[argIndex] == PRIORITY_ARG) {
61             if (argIndex + 1 < args.size()) {
62                 argIndex++;
63                 priority = getPriorityType(args[argIndex]);
64             }
65         } else {
66             strippedArgs.add(args[argIndex]);
67         }
68     }
69 
70     switch (priority) {
71         case PriorityType::CRITICAL:
72             status = dumpCritical(fd, strippedArgs, asProto);
73             break;
74         case PriorityType::HIGH:
75             status = dumpHigh(fd, strippedArgs, asProto);
76             break;
77         case PriorityType::NORMAL:
78             status = dumpNormal(fd, strippedArgs, asProto);
79             break;
80         default:
81             status = dumpAll(fd, strippedArgs, asProto);
82             break;
83     }
84     return status;
85 }
86 } // namespace android
87