1 /**
2  * Copyright (C) 2023 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 package com.android.server.soundtrigger;
18 
19 import android.util.Slog;
20 
21 import com.android.server.utils.EventLogger.Event;
22 
23 import java.util.UUID;
24 
25 public abstract class SoundTriggerEvent extends Event {
26 
27     @Override
printLog(int type, String tag)28     public Event printLog(int type, String tag) {
29         switch (type) {
30             case ALOGI:
31                 Slog.i(tag, eventToString());
32                 break;
33             case ALOGE:
34                 Slog.e(tag, eventToString());
35                 break;
36             case ALOGW:
37                 Slog.w(tag, eventToString());
38                 break;
39             case ALOGV:
40             default:
41                 Slog.v(tag, eventToString());
42         }
43         return this;
44     }
45 
46     public static class ServiceEvent extends SoundTriggerEvent {
47         public enum Type {
48             ATTACH,
49             LIST_MODULE,
50             DETACH,
51         }
52 
53         private final Type mType;
54         private final String mPackageName;
55         private final String mErrorString;
56 
ServiceEvent(Type type)57         public ServiceEvent(Type type) {
58             this(type, null, null);
59         }
60 
ServiceEvent(Type type, String packageName)61         public ServiceEvent(Type type, String packageName) {
62             this(type, packageName, null);
63         }
64 
ServiceEvent(Type type, String packageName, String errorString)65         public ServiceEvent(Type type, String packageName, String errorString) {
66             mType = type;
67             mPackageName = packageName;
68             mErrorString = errorString;
69         }
70 
71         @Override
eventToString()72         public String eventToString() {
73             var res = new StringBuilder(String.format("%-12s", mType.name()));
74             if (mErrorString != null) {
75                 res.append(" ERROR: ").append(mErrorString);
76             }
77             if (mPackageName != null) {
78                 res.append(" for: ").append(mPackageName);
79             }
80             return res.toString();
81         }
82     }
83 
84     public static class SessionEvent extends SoundTriggerEvent {
85         public enum Type {
86             // Downward calls
87             START_RECOGNITION,
88             STOP_RECOGNITION,
89             LOAD_MODEL,
90             UNLOAD_MODEL,
91             UPDATE_MODEL,
92             DELETE_MODEL,
93             START_RECOGNITION_SERVICE,
94             STOP_RECOGNITION_SERVICE,
95             GET_MODEL_STATE,
96             SET_PARAMETER,
97             GET_MODULE_PROPERTIES,
98             DETACH,
99             // Callback events
100             RECOGNITION,
101             RESUME,
102             RESUME_FAILED,
103             PAUSE,
104             PAUSE_FAILED,
105             RESOURCES_AVAILABLE,
106             MODULE_DIED
107         }
108 
109         private final UUID mModelUuid;
110         private final Type mType;
111         private final String mErrorString;
112 
SessionEvent(Type type, UUID modelUuid, String errorString)113         public SessionEvent(Type type, UUID modelUuid, String errorString) {
114             mType = type;
115             mModelUuid = modelUuid;
116             mErrorString = errorString;
117         }
118 
SessionEvent(Type type, UUID modelUuid)119         public SessionEvent(Type type, UUID modelUuid) {
120             this(type, modelUuid, null);
121         }
122 
123         @Override
eventToString()124         public String eventToString() {
125             var res = new StringBuilder(String.format("%-25s", mType.name()));
126             if (mErrorString != null) {
127                 res.append(" ERROR: ").append(mErrorString);
128             }
129             if (mModelUuid != null) {
130                 res.append(" for: ").append(mModelUuid);
131             }
132             return res.toString();
133         }
134     }
135 }
136