blob: 6faeb72e2a4d97000bb2d60ff2a83b8eb3bd0bc0 [file] [log] [blame]
Arthur Ishiguro7a23a962017-11-01 10:52:28 -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 */
16
17package com.android.server.location;
18
Arthur Ishiguro4e39aa12017-11-14 14:59:08 -080019import android.Manifest;
20import android.content.Context;
Arthur Ishiguro7a23a962017-11-01 10:52:28 -070021import android.hardware.contexthub.V1_0.ContextHub;
Arthur Ishiguro170611d2017-11-17 10:02:17 -080022import android.hardware.contexthub.V1_0.ContextHubMsg;
23import android.hardware.contexthub.V1_0.HostEndPoint;
Arthur Ishiguro7a23a962017-11-01 10:52:28 -070024import android.hardware.contexthub.V1_0.HubAppInfo;
Arthur Ishiguro4e39aa12017-11-14 14:59:08 -080025import android.hardware.contexthub.V1_0.Result;
Arthur Ishiguro7a23a962017-11-01 10:52:28 -070026import android.hardware.location.ContextHubInfo;
Arthur Ishiguro4e39aa12017-11-14 14:59:08 -080027import android.hardware.location.ContextHubTransaction;
Arthur Ishiguro7a23a962017-11-01 10:52:28 -070028import android.hardware.location.NanoAppBinary;
Arthur Ishiguro170611d2017-11-17 10:02:17 -080029import android.hardware.location.NanoAppMessage;
Arthur Ishiguro7a23a962017-11-01 10:52:28 -070030import android.hardware.location.NanoAppState;
31import android.util.Log;
32
33import java.util.List;
34import java.util.ArrayList;
35
36/**
37 * A class encapsulating helper functions used by the ContextHubService class
38 */
39/* package */ class ContextHubServiceUtil {
40 private static final String TAG = "ContextHubServiceUtil";
Arthur Ishiguro4e39aa12017-11-14 14:59:08 -080041 private static final String HARDWARE_PERMISSION = Manifest.permission.LOCATION_HARDWARE;
42 private static final String ENFORCE_HW_PERMISSION_MESSAGE = "Permission '"
43 + HARDWARE_PERMISSION + "' not granted to access ContextHub Hardware";
Arthur Ishiguro7a23a962017-11-01 10:52:28 -070044
45 /**
46 * Creates a ContextHubInfo array from an ArrayList of HIDL ContextHub objects.
47 *
48 * @param hubList the ContextHub ArrayList
49 * @return the ContextHubInfo array
50 */
51 /* package */
52 static ContextHubInfo[] createContextHubInfoArray(List<ContextHub> hubList) {
53 ContextHubInfo[] contextHubInfoList = new ContextHubInfo[hubList.size()];
54 for (int i = 0; i < hubList.size(); i++) {
55 contextHubInfoList[i] = new ContextHubInfo(hubList.get(i));
56 }
57
58 return contextHubInfoList;
59 }
60
61 /**
62 * Copies a primitive byte array to a ArrayList<Byte>.
63 *
64 * @param inputArray the primitive byte array
65 * @param outputArray the ArrayList<Byte> array to append
66 */
67 /* package */
68 static void copyToByteArrayList(byte[] inputArray, ArrayList<Byte> outputArray) {
69 outputArray.clear();
70 outputArray.ensureCapacity(inputArray.length);
71 for (byte element : inputArray) {
72 outputArray.add(element);
73 }
74 }
75
76 /**
77 * Creates a byte array given a ArrayList<Byte> and copies its contents.
78 *
79 * @param array the ArrayList<Byte> object
80 * @return the byte array
81 */
82 /* package */
83 static byte[] createPrimitiveByteArray(ArrayList<Byte> array) {
84 byte[] primitiveArray = new byte[array.size()];
85 for (int i = 0; i < array.size(); i++) {
86 primitiveArray[i] = array.get(i);
87 }
88
89 return primitiveArray;
90 }
91
92 /**
93 * Generates the Context Hub HAL's NanoAppBinary object from the client-facing
94 * android.hardware.location.NanoAppBinary object.
95 *
96 * @param nanoAppBinary the client-facing NanoAppBinary object
97 * @return the Context Hub HAL's NanoAppBinary object
98 */
99 /* package */
100 static android.hardware.contexthub.V1_0.NanoAppBinary createHidlNanoAppBinary(
101 NanoAppBinary nanoAppBinary) {
102 android.hardware.contexthub.V1_0.NanoAppBinary hidlNanoAppBinary =
103 new android.hardware.contexthub.V1_0.NanoAppBinary();
104
105 hidlNanoAppBinary.appId = nanoAppBinary.getNanoAppId();
106 hidlNanoAppBinary.appVersion = nanoAppBinary.getNanoAppVersion();
107 hidlNanoAppBinary.flags = nanoAppBinary.getFlags();
108 hidlNanoAppBinary.targetChreApiMajorVersion = nanoAppBinary.getTargetChreApiMajorVersion();
109 hidlNanoAppBinary.targetChreApiMinorVersion = nanoAppBinary.getTargetChreApiMinorVersion();
110
111 // Log exceptions while processing the binary, but continue to pass down the binary
112 // since the error checking is deferred to the Context Hub.
113 try {
114 copyToByteArrayList(nanoAppBinary.getBinaryNoHeader(), hidlNanoAppBinary.customBinary);
115 } catch (IndexOutOfBoundsException e) {
116 Log.w(TAG, e.getMessage());
117 } catch (NullPointerException e) {
118 Log.w(TAG, "NanoApp binary was null");
119 }
120
121 return hidlNanoAppBinary;
122 }
123
124 /**
125 * Generates a client-facing NanoAppState array from a HAL HubAppInfo array.
126 *
127 * @param nanoAppInfoList the array of HubAppInfo objects
128 * @return the corresponding array of NanoAppState objects
129 */
130 /* package */
131 static List<NanoAppState> createNanoAppStateList(
132 List<HubAppInfo> nanoAppInfoList) {
133 ArrayList<NanoAppState> nanoAppStateList = new ArrayList<>();
134 for (HubAppInfo appInfo : nanoAppInfoList) {
135 nanoAppStateList.add(
136 new NanoAppState(appInfo.appId, appInfo.version, appInfo.enabled));
137 }
138
139 return nanoAppStateList;
140 }
Arthur Ishiguro170611d2017-11-17 10:02:17 -0800141
142 /**
143 * Creates a HIDL ContextHubMsg object to send to a nanoapp.
144 *
145 * @param hostEndPoint the ID of the client sending the message
146 * @param message the client-facing NanoAppMessage object describing the message
147 * @return the HIDL ContextHubMsg object
148 */
149 /* package */
150 static ContextHubMsg createHidlContextHubMessage(short hostEndPoint, NanoAppMessage message) {
151 ContextHubMsg hidlMessage = new ContextHubMsg();
152
153 hidlMessage.appName = message.getNanoAppId();
154 hidlMessage.hostEndPoint = hostEndPoint;
155 hidlMessage.msgType = message.getMessageType();
156 copyToByteArrayList(message.getMessageBody(), hidlMessage.msg);
157
158 return hidlMessage;
159 }
160
161 /**
162 * Creates a client-facing NanoAppMessage object to send to a client.
163 *
164 * @param message the HIDL ContextHubMsg object from a nanoapp
165 * @return the NanoAppMessage object
166 */
167 /* package */
168 static NanoAppMessage createNanoAppMessage(ContextHubMsg message) {
169 byte[] messageArray = createPrimitiveByteArray(message.msg);
170
171 return NanoAppMessage.createMessageFromNanoApp(
172 message.appName, message.msgType, messageArray,
173 message.hostEndPoint == HostEndPoint.BROADCAST);
174 }
Arthur Ishiguro4e39aa12017-11-14 14:59:08 -0800175
176 /**
177 * Checks for location hardware permissions.
178 *
179 * @param context the context of the service
180 */
181 /* package */
182 static void checkPermissions(Context context) {
183 context.enforceCallingPermission(HARDWARE_PERMISSION, ENFORCE_HW_PERMISSION_MESSAGE);
184 }
185
186 /**
187 * Helper function to convert from the HAL Result enum error code to the
188 * ContextHubTransaction.Result type.
189 *
190 * @param halResult the Result enum error code
191 * @return the ContextHubTransaction.Result equivalent
192 */
193 @ContextHubTransaction.Result
194 /* package */
195 static int toTransactionResult(int halResult) {
196 switch (halResult) {
197 case Result.OK:
198 return ContextHubTransaction.TRANSACTION_SUCCESS;
199 case Result.BAD_PARAMS:
200 return ContextHubTransaction.TRANSACTION_FAILED_BAD_PARAMS;
201 case Result.NOT_INIT:
202 return ContextHubTransaction.TRANSACTION_FAILED_UNINITIALIZED;
203 case Result.TRANSACTION_PENDING:
204 return ContextHubTransaction.TRANSACTION_FAILED_PENDING;
205 case Result.TRANSACTION_FAILED:
206 case Result.UNKNOWN_FAILURE:
207 default: /* fall through */
208 return ContextHubTransaction.TRANSACTION_FAILED_UNKNOWN;
209 }
210 }
Arthur Ishiguro7a23a962017-11-01 10:52:28 -0700211}