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