• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 package android.edi.cts;
17 
18 import com.android.compatibility.common.util.DeviceInfo;
19 import com.android.compatibility.common.util.HostInfoStore;
20 import com.android.tradefed.device.ITestDevice;
21 import com.android.tradefed.log.LogUtil.CLog;
22 import com.android.tradefed.util.CommandResult;
23 
24 import java.util.Set;
25 
26 public class ServiceDeviceInfo extends DeviceInfo {
27     // Add new binder services to be monitored in the following Set
28     private static final Set<String> MONITORED_SERVICES = Set.of("tradeinmode");
29 
30     private static final String PACKAGE = "binder_services";
31     private static final String NAME = "name";
32     private static final String INTERFACE_DESCRIPTOR = "interface_descriptor";
33     private static final String AVAILABILITY = "is_available";
34 
35     private static class ServiceEntry {
36         public String name;
37         public String interfaceDescriptor;
38     }
39 
parseServicesLine(HostInfoStore store, String line)40     protected ServiceEntry parseServicesLine(HostInfoStore store, String line) throws Exception {
41         // The first line begins returns the number of services found and can
42         // be skipped. It's the only line string with "Found".
43         if (line.startsWith("Found")) {
44             return null;
45         }
46 
47         // Each line has the format:
48         // "<int:line number> <str:service name>: [<interface descriptor>]
49         String[] entries = line.split("\\s+");
50         if (entries.length != 3) {
51             return null;
52         }
53 
54         // Remove the last character of the string as it corresponds to a colon.
55         if (entries[1].length() == 0) {
56             return null;
57         }
58 
59         ServiceEntry se = new ServiceEntry();
60 
61         se.name = entries[1].substring(0, entries[1].length() - 1);
62 
63         // Remove the first and last characters of the string as they correspond
64         // to square brackets.
65         if (entries.length > 2 && entries[2].length() > 2) {
66             se.interfaceDescriptor = entries[2].substring(1, entries[2].length() - 1);
67         }
68 
69         return se;
70     }
71 
72     @Override
collectDeviceInfo(HostInfoStore store)73     protected void collectDeviceInfo(HostInfoStore store) throws Exception {
74         ITestDevice device = getDevice();
75 
76         CommandResult commandResult = device.executeShellV2Command("service list");
77         if (commandResult.getExitCode() != 0) {
78             CLog.e("The 'service' command exited with error");
79             return;
80         }
81 
82         String output = commandResult.getStdout();
83         if (output == null) {
84             CLog.e("Empty output");
85             return;
86         }
87 
88         output = output.trim();
89         if (output.isEmpty()) {
90             CLog.e("Empty output");
91             return;
92         }
93 
94         store.startArray(PACKAGE);
95 
96         for (String name : MONITORED_SERVICES) {
97             ServiceEntry se = null;
98             boolean found = false;
99 
100             // Iterate over every line of the output, each line corresponding to a
101             // service found in the device.
102             for (String line : output.split("\\r?\\n")) {
103                 se = parseServicesLine(store, line);
104                 if (se != null && name.equals(se.name)) {
105                     found = true;
106                     break;
107                 }
108             }
109             store.startGroup();
110             store.addResult(NAME, name);
111             store.addResult(INTERFACE_DESCRIPTOR, found ? se.interfaceDescriptor : "");
112             store.addResult(AVAILABILITY, found);
113             store.endGroup();
114         }
115 
116         store.endArray();
117     }
118 }
119