1 /* 2 * Copyright (C) 2016 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.hardware.location; 17 18 import android.annotation.NonNull; 19 import android.annotation.Nullable; 20 import android.annotation.SystemApi; 21 import android.hardware.contexthub.V1_0.ContextHub; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 import android.util.proto.ProtoOutputStream; 25 26 import java.util.Arrays; 27 28 /** 29 * @hide 30 */ 31 @SystemApi 32 public class ContextHubInfo implements Parcelable { 33 private int mId; 34 private String mName; 35 private String mVendor; 36 private String mToolchain; 37 private int mPlatformVersion; 38 private int mToolchainVersion; 39 private float mPeakMips; 40 private float mStoppedPowerDrawMw; 41 private float mSleepPowerDrawMw; 42 private float mPeakPowerDrawMw; 43 private int mMaxPacketLengthBytes; 44 private byte mChreApiMajorVersion; 45 private byte mChreApiMinorVersion; 46 private short mChrePatchVersion; 47 private long mChrePlatformId; 48 49 private int[] mSupportedSensors; 50 51 private MemoryRegion[] mMemoryRegions; 52 53 /* 54 * TODO(b/67734082): Deprecate this constructor and mark private fields as final. 55 */ ContextHubInfo()56 public ContextHubInfo() { 57 } 58 59 /** 60 * @hide 61 */ ContextHubInfo(ContextHub contextHub)62 public ContextHubInfo(ContextHub contextHub) { 63 mId = contextHub.hubId; 64 mName = contextHub.name; 65 mVendor = contextHub.vendor; 66 mToolchain = contextHub.toolchain; 67 mPlatformVersion = contextHub.platformVersion; 68 mToolchainVersion = contextHub.toolchainVersion; 69 mPeakMips = contextHub.peakMips; 70 mStoppedPowerDrawMw = contextHub.stoppedPowerDrawMw; 71 mSleepPowerDrawMw = contextHub.sleepPowerDrawMw; 72 mPeakPowerDrawMw = contextHub.peakPowerDrawMw; 73 mMaxPacketLengthBytes = contextHub.maxSupportedMsgLen; 74 mChrePlatformId = contextHub.chrePlatformId; 75 mChreApiMajorVersion = contextHub.chreApiMajorVersion; 76 mChreApiMinorVersion = contextHub.chreApiMinorVersion; 77 mChrePatchVersion = contextHub.chrePatchVersion; 78 79 mSupportedSensors = new int[0]; 80 mMemoryRegions = new MemoryRegion[0]; 81 } 82 /** 83 * @hide 84 */ ContextHubInfo(android.hardware.contexthub.ContextHubInfo contextHub)85 public ContextHubInfo(android.hardware.contexthub.ContextHubInfo contextHub) { 86 mId = contextHub.id; 87 mName = contextHub.name; 88 mVendor = contextHub.vendor; 89 mToolchain = contextHub.toolchain; 90 mPlatformVersion = 0; 91 mToolchainVersion = 0; 92 mPeakMips = contextHub.peakMips; 93 mStoppedPowerDrawMw = 0; 94 mSleepPowerDrawMw = 0; 95 mPeakPowerDrawMw = 0; 96 mMaxPacketLengthBytes = contextHub.maxSupportedMessageLengthBytes; 97 mChrePlatformId = contextHub.chrePlatformId; 98 mChreApiMajorVersion = contextHub.chreApiMajorVersion; 99 mChreApiMinorVersion = contextHub.chreApiMinorVersion; 100 mChrePatchVersion = (short) contextHub.chrePatchVersion; 101 102 mSupportedSensors = new int[0]; 103 mMemoryRegions = new MemoryRegion[0]; 104 } 105 106 /** 107 * returns the maximum number of bytes that can be sent per message to the hub 108 * 109 * @return int - maximum bytes that can be transmitted in a 110 * single packet 111 */ getMaxPacketLengthBytes()112 public int getMaxPacketLengthBytes() { 113 return mMaxPacketLengthBytes; 114 } 115 116 /** 117 * get the context hub unique identifer 118 * 119 * @return int - unique system wide identifier 120 */ getId()121 public int getId() { 122 return mId; 123 } 124 125 /** 126 * get a string as a hub name 127 * 128 * @return String - a name for the hub 129 */ getName()130 public String getName() { 131 return mName; 132 } 133 134 /** 135 * get a string as the vendor name 136 * 137 * @return String - a name for the vendor 138 */ getVendor()139 public String getVendor() { 140 return mVendor; 141 } 142 143 /** 144 * get tool chain string 145 * 146 * @return String - description of the tool chain 147 */ getToolchain()148 public String getToolchain() { 149 return mToolchain; 150 } 151 152 /** 153 * get platform version 154 * 155 * @return int - platform version number 156 */ getPlatformVersion()157 public int getPlatformVersion() { 158 return mPlatformVersion; 159 } 160 161 /** 162 * get static platform version number 163 * 164 * @return int - platform version number 165 */ getStaticSwVersion()166 public int getStaticSwVersion() { 167 return (mChreApiMajorVersion << 24) | (mChreApiMinorVersion << 16) | (mChrePatchVersion); 168 } 169 170 /** 171 * get the tool chain version 172 * 173 * @return int - the tool chain version 174 */ getToolchainVersion()175 public int getToolchainVersion() { 176 return mToolchainVersion; 177 } 178 179 /** 180 * get the peak processing mips the hub can support 181 * 182 * @return float - peak MIPS that this hub can deliver 183 */ getPeakMips()184 public float getPeakMips() { 185 return mPeakMips; 186 } 187 188 /** 189 * get the stopped power draw in milliwatts 190 * This assumes that the hub enter a stopped state - which is 191 * different from the sleep state. Latencies on exiting the 192 * sleep state are typically higher and expect to be in multiple 193 * milliseconds. 194 * 195 * @return float - power draw by the hub in stopped state 196 */ getStoppedPowerDrawMw()197 public float getStoppedPowerDrawMw() { 198 return mStoppedPowerDrawMw; 199 } 200 201 /** 202 * get the power draw of the hub in sleep mode. This assumes 203 * that the hub supports a sleep mode in which the power draw is 204 * lower than the power consumed when the hub is actively 205 * processing. As a guideline, assume that the hub should be 206 * able to enter sleep mode if it knows reliably on completion 207 * of some task that the next interrupt/scheduled work item is 208 * at least 250 milliseconds later. 209 * 210 * @return float - sleep power draw in milli watts 211 */ getSleepPowerDrawMw()212 public float getSleepPowerDrawMw() { 213 return mSleepPowerDrawMw; 214 } 215 216 /** 217 * get the peak powe draw of the hub. This is the power consumed 218 * by the hub at maximum load. 219 * 220 * @return float - peak power draw 221 */ getPeakPowerDrawMw()222 public float getPeakPowerDrawMw() { 223 return mPeakPowerDrawMw; 224 } 225 226 /** 227 * get the sensors supported by this hub 228 * 229 * @return int[] - all the supported sensors on this hub 230 * 231 * @see ContextHubManager 232 */ getSupportedSensors()233 public int[] getSupportedSensors() { 234 return Arrays.copyOf(mSupportedSensors, mSupportedSensors.length); 235 } 236 237 /** 238 * get the various memory regions on this hub 239 * 240 * @return MemoryRegion[] - all the memory regions on this hub 241 * 242 * @see MemoryRegion 243 */ getMemoryRegions()244 public MemoryRegion[] getMemoryRegions() { 245 return Arrays.copyOf(mMemoryRegions, mMemoryRegions.length); 246 } 247 248 /** 249 * @return the CHRE platform ID as defined in chre/version.h 250 */ getChrePlatformId()251 public long getChrePlatformId() { 252 return mChrePlatformId; 253 } 254 255 /** 256 * @return the CHRE API's major version as defined in chre/version.h 257 */ getChreApiMajorVersion()258 public byte getChreApiMajorVersion() { 259 return mChreApiMajorVersion; 260 } 261 262 /** 263 * @return the CHRE API's minor version as defined in chre/version.h 264 */ getChreApiMinorVersion()265 public byte getChreApiMinorVersion() { 266 return mChreApiMinorVersion; 267 } 268 269 /** 270 * @return the CHRE patch version as defined in chre/version.h 271 */ getChrePatchVersion()272 public short getChrePatchVersion() { 273 return mChrePatchVersion; 274 } 275 276 @NonNull 277 @Override toString()278 public String toString() { 279 String retVal = ""; 280 retVal += "ID/handle : " + mId; 281 retVal += ", Name : " + mName; 282 retVal += "\n\tVendor : " + mVendor; 283 retVal += ", Toolchain : " + mToolchain; 284 retVal += ", Toolchain version: 0x" + Integer.toHexString(mToolchainVersion); 285 retVal += "\n\tPlatformVersion : 0x" + Integer.toHexString(mPlatformVersion); 286 retVal += ", SwVersion : " 287 + mChreApiMajorVersion + "." + mChreApiMinorVersion + "." + mChrePatchVersion; 288 retVal += ", CHRE platform ID: 0x" + Long.toHexString(mChrePlatformId); 289 retVal += "\n\tPeakMips : " + mPeakMips; 290 retVal += ", StoppedPowerDraw : " + mStoppedPowerDrawMw + " mW"; 291 retVal += ", PeakPowerDraw : " + mPeakPowerDrawMw + " mW"; 292 retVal += ", MaxPacketLength : " + mMaxPacketLengthBytes + " Bytes"; 293 294 return retVal; 295 } 296 297 /** 298 * Dump the internal state as a ContextHubInfoProto to the given ProtoOutputStream. 299 * 300 * If the output belongs to a sub message, the caller is responsible for wrapping this function 301 * between {@link ProtoOutputStream#start(long)} and {@link ProtoOutputStream#end(long)}. 302 * 303 * @hide 304 */ dump(ProtoOutputStream proto)305 public void dump(ProtoOutputStream proto) { 306 proto.write(ContextHubInfoProto.ID, mId); 307 proto.write(ContextHubInfoProto.NAME, mName); 308 proto.write(ContextHubInfoProto.VENDOR, mVendor); 309 proto.write(ContextHubInfoProto.TOOLCHAIN, mToolchain); 310 proto.write(ContextHubInfoProto.PLATFORM_VERSION, mPlatformVersion); 311 proto.write(ContextHubInfoProto.STATIC_SW_VERSION, getStaticSwVersion()); 312 proto.write(ContextHubInfoProto.TOOLCHAIN_VERSION, mToolchainVersion); 313 proto.write(ContextHubInfoProto.CHRE_PLATFORM_ID, mChrePlatformId); 314 proto.write(ContextHubInfoProto.PEAK_MIPS, mPeakMips); 315 proto.write(ContextHubInfoProto.STOPPED_POWER_DRAW_MW, mStoppedPowerDrawMw); 316 proto.write(ContextHubInfoProto.SLEEP_POWER_DRAW_MW, mSleepPowerDrawMw); 317 proto.write(ContextHubInfoProto.PEAK_POWER_DRAW_MW, mPeakPowerDrawMw); 318 proto.write(ContextHubInfoProto.MAX_PACKET_LENGTH_BYTES, mMaxPacketLengthBytes); 319 } 320 321 @Override equals(@ullable Object object)322 public boolean equals(@Nullable Object object) { 323 if (object == this) { 324 return true; 325 } 326 327 boolean isEqual = false; 328 if (object instanceof ContextHubInfo) { 329 ContextHubInfo other = (ContextHubInfo) object; 330 isEqual = (other.getId() == mId) 331 && other.getName().equals(mName) 332 && other.getVendor().equals(mVendor) 333 && other.getToolchain().equals(mToolchain) 334 && (other.getToolchainVersion() == mToolchainVersion) 335 && (other.getStaticSwVersion() == getStaticSwVersion()) 336 && (other.getChrePlatformId() == mChrePlatformId) 337 && (other.getPeakMips() == mPeakMips) 338 && (other.getStoppedPowerDrawMw() == mStoppedPowerDrawMw) 339 && (other.getSleepPowerDrawMw() == mSleepPowerDrawMw) 340 && (other.getPeakPowerDrawMw() == mPeakPowerDrawMw) 341 && (other.getMaxPacketLengthBytes() == mMaxPacketLengthBytes) 342 && Arrays.equals(other.getSupportedSensors(), mSupportedSensors) 343 && Arrays.equals(other.getMemoryRegions(), mMemoryRegions); 344 } 345 346 return isEqual; 347 } 348 ContextHubInfo(Parcel in)349 private ContextHubInfo(Parcel in) { 350 mId = in.readInt(); 351 mName = in.readString(); 352 mVendor = in.readString(); 353 mToolchain = in.readString(); 354 mPlatformVersion = in.readInt(); 355 mToolchainVersion = in.readInt(); 356 mPeakMips = in.readFloat(); 357 mStoppedPowerDrawMw = in.readFloat(); 358 mSleepPowerDrawMw = in.readFloat(); 359 mPeakPowerDrawMw = in.readFloat(); 360 mMaxPacketLengthBytes = in.readInt(); 361 mChrePlatformId = in.readLong(); 362 mChreApiMajorVersion = in.readByte(); 363 mChreApiMinorVersion = in.readByte(); 364 mChrePatchVersion = (short) in.readInt(); 365 366 int numSupportedSensors = in.readInt(); 367 mSupportedSensors = new int[numSupportedSensors]; 368 in.readIntArray(mSupportedSensors); 369 mMemoryRegions = in.createTypedArray(MemoryRegion.CREATOR); 370 } 371 describeContents()372 public int describeContents() { 373 return 0; 374 } 375 writeToParcel(Parcel out, int flags)376 public void writeToParcel(Parcel out, int flags) { 377 out.writeInt(mId); 378 out.writeString(mName); 379 out.writeString(mVendor); 380 out.writeString(mToolchain); 381 out.writeInt(mPlatformVersion); 382 out.writeInt(mToolchainVersion); 383 out.writeFloat(mPeakMips); 384 out.writeFloat(mStoppedPowerDrawMw); 385 out.writeFloat(mSleepPowerDrawMw); 386 out.writeFloat(mPeakPowerDrawMw); 387 out.writeInt(mMaxPacketLengthBytes); 388 out.writeLong(mChrePlatformId); 389 out.writeByte(mChreApiMajorVersion); 390 out.writeByte(mChreApiMinorVersion); 391 out.writeInt(mChrePatchVersion); 392 393 out.writeInt(mSupportedSensors.length); 394 out.writeIntArray(mSupportedSensors); 395 out.writeTypedArray(mMemoryRegions, flags); 396 } 397 398 public static final @android.annotation.NonNull Parcelable.Creator<ContextHubInfo> CREATOR 399 = new Parcelable.Creator<ContextHubInfo>() { 400 public ContextHubInfo createFromParcel(Parcel in) { 401 return new ContextHubInfo(in); 402 } 403 404 public ContextHubInfo[] newArray(int size) { 405 return new ContextHubInfo[size]; 406 } 407 }; 408 } 409