blob: fec1f7148a1e99040e07cff16ef6e19144b604d4 [file] [log] [blame]
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -07001/*
2 * Copyright 2017 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
Arthur Ishigurof2b6f012017-11-28 15:21:38 -080018import android.annotation.SystemApi;
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070019import android.os.Parcel;
20import android.os.Parcelable;
21
Neil Fullerb5d1c152019-04-04 21:02:06 +010022import libcore.util.HexEncoding;
23
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070024/**
25 * A class describing messages send to or from nanoapps through the Context Hub Service.
26 *
27 * The basis of the class is in the IContextHub.hal ContextHubMsg definition.
28 *
29 * @hide
30 */
Arthur Ishigurof2b6f012017-11-28 15:21:38 -080031@SystemApi
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070032public final class NanoAppMessage implements Parcelable {
Arthur Ishiguro1d3b23c2018-01-05 16:05:25 -080033 private static final int DEBUG_LOG_NUM_BYTES = 16;
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -070034 private long mNanoAppId;
35 private int mMessageType;
36 private byte[] mMessageBody;
37 private boolean mIsBroadcasted;
38
39 private NanoAppMessage(
40 long nanoAppId, int messageType, byte[] messageBody, boolean broadcasted) {
41 mNanoAppId = nanoAppId;
42 mMessageType = messageType;
43 mMessageBody = messageBody;
44 mIsBroadcasted = broadcasted;
45 }
46
47 /**
48 * Creates a NanoAppMessage object to send to a nanoapp.
49 *
50 * This factory method can be used to generate a NanoAppMessage object to be used in
51 * the ContextHubClient.sendMessageToNanoApp API.
52 *
53 * @param targetNanoAppId the ID of the nanoapp to send the message to
54 * @param messageType the nanoapp-dependent message type
55 * @param messageBody the byte array message contents
56 *
57 * @return the NanoAppMessage object
58 */
59 public static NanoAppMessage createMessageToNanoApp(
60 long targetNanoAppId, int messageType, byte[] messageBody) {
61 return new NanoAppMessage(
62 targetNanoAppId, messageType, messageBody, false /* broadcasted */);
63 }
64
65 /**
66 * Creates a NanoAppMessage object sent from a nanoapp.
67 *
68 * This factory method is intended only to be used by the Context Hub Service when delivering
69 * messages from a nanoapp to clients.
70 *
71 * @param sourceNanoAppId the ID of the nanoapp that the message was sent from
72 * @param messageType the nanoapp-dependent message type
73 * @param messageBody the byte array message contents
74 * @param broadcasted {@code true} if the message was broadcasted, {@code false} otherwise
75 *
76 * @return the NanoAppMessage object
77 */
78 public static NanoAppMessage createMessageFromNanoApp(
79 long sourceNanoAppId, int messageType, byte[] messageBody, boolean broadcasted) {
80 return new NanoAppMessage(sourceNanoAppId, messageType, messageBody, broadcasted);
81 }
82
83 /**
84 * @return the ID of the source or destination nanoapp
85 */
86 public long getNanoAppId() {
87 return mNanoAppId;
88 }
89
90 /**
91 * @return the type of the message that is nanoapp-dependent
92 */
93 public int getMessageType() {
94 return mMessageType;
95 }
96
97 /**
98 * @return the byte array contents of the message
99 */
100 public byte[] getMessageBody() {
101 return mMessageBody;
102 }
103
104 /**
105 * @return {@code true} if the message is broadcasted, {@code false} otherwise
106 */
107 public boolean isBroadcastMessage() {
108 return mIsBroadcasted;
109 }
110
111 private NanoAppMessage(Parcel in) {
112 mNanoAppId = in.readLong();
113 mIsBroadcasted = (in.readInt() == 1);
114 mMessageType = in.readInt();
115
116 int msgSize = in.readInt();
117 mMessageBody = new byte[msgSize];
118 in.readByteArray(mMessageBody);
119 }
120
121 @Override
122 public int describeContents() {
123 return 0;
124 }
125
126 @Override
127 public void writeToParcel(Parcel out, int flags) {
128 out.writeLong(mNanoAppId);
129 out.writeInt(mIsBroadcasted ? 1 : 0);
130 out.writeInt(mMessageType);
131
132 out.writeInt(mMessageBody.length);
133 out.writeByteArray(mMessageBody);
134 }
135
136 public static final Creator<NanoAppMessage> CREATOR =
137 new Creator<NanoAppMessage>() {
138 @Override
139 public NanoAppMessage createFromParcel(Parcel in) {
140 return new NanoAppMessage(in);
141 }
142
143 @Override
144 public NanoAppMessage[] newArray(int size) {
145 return new NanoAppMessage[size];
146 }
147 };
Arthur Ishiguro1d3b23c2018-01-05 16:05:25 -0800148
149 @Override
150 public String toString() {
151 int length = mMessageBody.length;
152
153 String ret = "NanoAppMessage[type = " + mMessageType + ", length = " + mMessageBody.length
154 + " bytes, " + (mIsBroadcasted ? "broadcast" : "unicast") + ", nanoapp = 0x"
155 + Long.toHexString(mNanoAppId) + "](";
156 if (length > 0) {
157 ret += "data = 0x";
158 }
159 for (int i = 0; i < Math.min(length, DEBUG_LOG_NUM_BYTES); i++) {
Neil Fullerb5d1c152019-04-04 21:02:06 +0100160 ret += HexEncoding.encodeToString(mMessageBody[i], true /* upperCase */);
Arthur Ishiguro1d3b23c2018-01-05 16:05:25 -0800161
162 if ((i + 1) % 4 == 0) {
163 ret += " ";
164 }
165 }
166 if (length > DEBUG_LOG_NUM_BYTES) {
167 ret += "...";
168 }
169 ret += ")";
170
171 return ret;
172 }
Arthur Ishigurob9ae7bd2017-10-09 12:47:52 -0700173}