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