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