blob: ddbaf86beff233a28233cf82ab48cfd333fc0d8f [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
19import android.hardware.contexthub.V1_0.ContextHub;
Arthur Ishiguro170611d2017-11-17 10:02:17 -080020import android.hardware.contexthub.V1_0.ContextHubMsg;
21import android.hardware.contexthub.V1_0.HostEndPoint;
Arthur Ishiguro7a23a962017-11-01 10:52:28 -070022import android.hardware.contexthub.V1_0.HubAppInfo;
23import android.hardware.location.ContextHubInfo;
24import android.hardware.location.NanoAppBinary;
Arthur Ishiguro170611d2017-11-17 10:02:17 -080025import android.hardware.location.NanoAppMessage;
Arthur Ishiguro7a23a962017-11-01 10:52:28 -070026import android.hardware.location.NanoAppState;
27import android.util.Log;
28
29import java.util.List;
30import java.util.ArrayList;
31
32/**
33 * A class encapsulating helper functions used by the ContextHubService class
34 */
35/* package */ class ContextHubServiceUtil {
36 private static final String TAG = "ContextHubServiceUtil";
37
38 /**
39 * Creates a ContextHubInfo array from an ArrayList of HIDL ContextHub objects.
40 *
41 * @param hubList the ContextHub ArrayList
42 * @return the ContextHubInfo array
43 */
44 /* package */
45 static ContextHubInfo[] createContextHubInfoArray(List<ContextHub> hubList) {
46 ContextHubInfo[] contextHubInfoList = new ContextHubInfo[hubList.size()];
47 for (int i = 0; i < hubList.size(); i++) {
48 contextHubInfoList[i] = new ContextHubInfo(hubList.get(i));
49 }
50
51 return contextHubInfoList;
52 }
53
54 /**
55 * Copies a primitive byte array to a ArrayList<Byte>.
56 *
57 * @param inputArray the primitive byte array
58 * @param outputArray the ArrayList<Byte> array to append
59 */
60 /* package */
61 static void copyToByteArrayList(byte[] inputArray, ArrayList<Byte> outputArray) {
62 outputArray.clear();
63 outputArray.ensureCapacity(inputArray.length);
64 for (byte element : inputArray) {
65 outputArray.add(element);
66 }
67 }
68
69 /**
70 * Creates a byte array given a ArrayList<Byte> and copies its contents.
71 *
72 * @param array the ArrayList<Byte> object
73 * @return the byte array
74 */
75 /* package */
76 static byte[] createPrimitiveByteArray(ArrayList<Byte> array) {
77 byte[] primitiveArray = new byte[array.size()];
78 for (int i = 0; i < array.size(); i++) {
79 primitiveArray[i] = array.get(i);
80 }
81
82 return primitiveArray;
83 }
84
85 /**
86 * Generates the Context Hub HAL's NanoAppBinary object from the client-facing
87 * android.hardware.location.NanoAppBinary object.
88 *
89 * @param nanoAppBinary the client-facing NanoAppBinary object
90 * @return the Context Hub HAL's NanoAppBinary object
91 */
92 /* package */
93 static android.hardware.contexthub.V1_0.NanoAppBinary createHidlNanoAppBinary(
94 NanoAppBinary nanoAppBinary) {
95 android.hardware.contexthub.V1_0.NanoAppBinary hidlNanoAppBinary =
96 new android.hardware.contexthub.V1_0.NanoAppBinary();
97
98 hidlNanoAppBinary.appId = nanoAppBinary.getNanoAppId();
99 hidlNanoAppBinary.appVersion = nanoAppBinary.getNanoAppVersion();
100 hidlNanoAppBinary.flags = nanoAppBinary.getFlags();
101 hidlNanoAppBinary.targetChreApiMajorVersion = nanoAppBinary.getTargetChreApiMajorVersion();
102 hidlNanoAppBinary.targetChreApiMinorVersion = nanoAppBinary.getTargetChreApiMinorVersion();
103
104 // Log exceptions while processing the binary, but continue to pass down the binary
105 // since the error checking is deferred to the Context Hub.
106 try {
107 copyToByteArrayList(nanoAppBinary.getBinaryNoHeader(), hidlNanoAppBinary.customBinary);
108 } catch (IndexOutOfBoundsException e) {
109 Log.w(TAG, e.getMessage());
110 } catch (NullPointerException e) {
111 Log.w(TAG, "NanoApp binary was null");
112 }
113
114 return hidlNanoAppBinary;
115 }
116
117 /**
118 * Generates a client-facing NanoAppState array from a HAL HubAppInfo array.
119 *
120 * @param nanoAppInfoList the array of HubAppInfo objects
121 * @return the corresponding array of NanoAppState objects
122 */
123 /* package */
124 static List<NanoAppState> createNanoAppStateList(
125 List<HubAppInfo> nanoAppInfoList) {
126 ArrayList<NanoAppState> nanoAppStateList = new ArrayList<>();
127 for (HubAppInfo appInfo : nanoAppInfoList) {
128 nanoAppStateList.add(
129 new NanoAppState(appInfo.appId, appInfo.version, appInfo.enabled));
130 }
131
132 return nanoAppStateList;
133 }
Arthur Ishiguro170611d2017-11-17 10:02:17 -0800134
135 /**
136 * Creates a HIDL ContextHubMsg object to send to a nanoapp.
137 *
138 * @param hostEndPoint the ID of the client sending the message
139 * @param message the client-facing NanoAppMessage object describing the message
140 * @return the HIDL ContextHubMsg object
141 */
142 /* package */
143 static ContextHubMsg createHidlContextHubMessage(short hostEndPoint, NanoAppMessage message) {
144 ContextHubMsg hidlMessage = new ContextHubMsg();
145
146 hidlMessage.appName = message.getNanoAppId();
147 hidlMessage.hostEndPoint = hostEndPoint;
148 hidlMessage.msgType = message.getMessageType();
149 copyToByteArrayList(message.getMessageBody(), hidlMessage.msg);
150
151 return hidlMessage;
152 }
153
154 /**
155 * Creates a client-facing NanoAppMessage object to send to a client.
156 *
157 * @param message the HIDL ContextHubMsg object from a nanoapp
158 * @return the NanoAppMessage object
159 */
160 /* package */
161 static NanoAppMessage createNanoAppMessage(ContextHubMsg message) {
162 byte[] messageArray = createPrimitiveByteArray(message.msg);
163
164 return NanoAppMessage.createMessageFromNanoApp(
165 message.appName, message.msgType, messageArray,
166 message.hostEndPoint == HostEndPoint.BROADCAST);
167 }
Arthur Ishiguro7a23a962017-11-01 10:52:28 -0700168}