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.car.internal.evs;
18 
19 import static android.car.evs.CarEvsManager.SERVICE_TYPE_REARVIEW;
20 import static android.car.evs.CarEvsManager.SERVICE_TYPE_SURROUNDVIEW;
21 import static android.car.evs.CarEvsManager.SERVICE_TYPE_FRONTVIEW;
22 import static android.car.evs.CarEvsManager.SERVICE_TYPE_LEFTVIEW;
23 import static android.car.evs.CarEvsManager.SERVICE_TYPE_RIGHTVIEW;
24 import static android.car.evs.CarEvsManager.SERVICE_TYPE_DRIVERVIEW;
25 import static android.car.evs.CarEvsManager.SERVICE_TYPE_FRONT_PASSENGERSVIEW;
26 import static android.car.evs.CarEvsManager.SERVICE_TYPE_REAR_PASSENGERSVIEW;
27 import static android.car.evs.CarEvsManager.SERVICE_TYPE_USER_DEFINED;
28 
29 import android.car.builtin.util.Slogf;
30 import android.car.evs.CarEvsManager.CarEvsServiceType;
31 
32 /**
33  * This class provide utility methods for CarEvsService clients.
34  *
35  * @hide
36  */
37 public final class CarEvsUtils {
38     private static final String TAG = CarEvsUtils.class.getSimpleName();
39 
40     // To identify the origin of frame buffers and stream events, CarEvsService tags them with their
41     // origin service type in 8-MSB of the frame buffer id and the stream event id. These constants
42     // are used to implement this tagging operation.
43     private static final int TAG_BIT_LEFT_SHIFT = 24;
44     private static final int DATA_BIT_MASK = ~(0xFF << TAG_BIT_LEFT_SHIFT);
45 
CarEvsUtils()46     private CarEvsUtils() {}
47 
convertToServiceType(String type)48     public static @CarEvsServiceType int convertToServiceType(String type) {
49         switch (type) {
50             case "REARVIEW":
51                 return SERVICE_TYPE_REARVIEW;
52             case "SURROUNDVIEW":
53                 return SERVICE_TYPE_SURROUNDVIEW;
54             case "FRONTVIEW":
55                 return SERVICE_TYPE_FRONTVIEW;
56             case "LEFTVIEW":
57                 return SERVICE_TYPE_LEFTVIEW;
58             case "RIGHTVIEW":
59                 return SERVICE_TYPE_RIGHTVIEW;
60             case "DRIVERVIEW":
61                 return SERVICE_TYPE_DRIVERVIEW;
62             case "FRONT_PASSENGERSVIEW":
63                 return SERVICE_TYPE_FRONT_PASSENGERSVIEW;
64             case "REAR_PASSENGERSVIEW":
65                 return SERVICE_TYPE_REAR_PASSENGERSVIEW;
66             default:
67                 Slogf.w(TAG, "USER_DEFINED will be returned for a unknown service type " + type);
68                 // fall through
69             case "USER_DEFINED":
70                 return SERVICE_TYPE_USER_DEFINED;
71         }
72     }
73 
convertToString(@arEvsServiceType int type)74     public static String convertToString(@CarEvsServiceType int type) {
75         switch (type) {
76               case SERVICE_TYPE_REARVIEW:
77                   return "REARVIEW";
78               case SERVICE_TYPE_SURROUNDVIEW:
79                   return "SURROUNDVIEW";
80               case SERVICE_TYPE_FRONTVIEW:
81                   return "FRONTVIEW";
82               case SERVICE_TYPE_LEFTVIEW:
83                   return "LEFTVIEW";
84               case SERVICE_TYPE_RIGHTVIEW:
85                   return "RIGHTVIEW";
86               case SERVICE_TYPE_DRIVERVIEW:
87                   return "DRIVERVIEW";
88               case SERVICE_TYPE_FRONT_PASSENGERSVIEW:
89                   return "FRONT_PASSENGERVIEW";
90               case SERVICE_TYPE_REAR_PASSENGERSVIEW:
91                   return "REAR_PASSENGERVIEW";
92               case SERVICE_TYPE_USER_DEFINED:
93                   return "USER_DEFINED";
94               default:
95                   return "Unknown type= + type";
96         }
97     }
98 
99     /**
100      * Extracts a service type from a given value and returns it.
101      *
102      * @param value This should be either an event or CarEvsBufferDescriptor id that are sent by
103      *              ICarEvsStreamCallback.onStreamEvent() and ICarEvsStreamCallback.onNewFrame()
104      *              callbacks respectively.
105      * @return A service type embedded in 8-MSB of a given value.
106      */
getTag(int value)107     public static @CarEvsServiceType int getTag(int value) {
108         return value >> TAG_BIT_LEFT_SHIFT;
109     }
110 
111     /**
112      * Extracts an actual buffer id or an event id from a given value and returns it.
113      *
114      * @param value This should be either an event or CarEvsBufferDescriptor id that are sent by
115      *              ICarEvsStreamCallback.onStreamEvent() and ICarEvsStreamCallback.onNewFrame()
116      *              callbacks respectively.
117      * @return A buffer id or an event.
118      */
getValue(int value)119     public static int getValue(int value) {
120         return value &= DATA_BIT_MASK;
121     }
122 
123     /**
124      * Embeds a given tag in 8-MSB of a given value and returns it.
125      *
126      * @param tag Additional information to identify the origin of a given value that is either a
127      *            buffer id or an event.
128      * @param value This should be either an event or CarEvsBufferDescriptor id that are sent by
129      *              ICarEvsStreamCallback.onStreamEvent() and ICarEvsStreamCallback.onNewFrame()
130      *              callbacks respectively.
131      * @return 32-bit integer that contains a tag in 8-MSB and a value in the rest.
132      */
putTag(int tag, int value)133     public static int putTag(int tag, int value) {
134         if (tag > 0xFF) {
135             Slogf.w(TAG, "A given tag %d is greater than 0xFF. Only 8-LSB will be effective.", tag);
136         }
137         return ((tag & 0xFF) << TAG_BIT_LEFT_SHIFT) | (value & DATA_BIT_MASK);
138     }
139 }
140