1 /* 2 * Copyright (C) 2022 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 com.google.android.chre.utils.pigweed; 17 18 import static android.hardware.location.ContextHubManager.AUTHORIZATION_DENIED; 19 import static android.hardware.location.ContextHubManager.AUTHORIZATION_GRANTED; 20 21 import android.content.Intent; 22 import android.hardware.location.ContextHubIntentEvent; 23 import android.hardware.location.ContextHubManager; 24 import android.util.Log; 25 26 import java.util.Objects; 27 28 import dev.pigweed.pw_rpc.Client; 29 30 /** 31 * Handles RPC events in CHRE intent. 32 */ 33 public class ChreIntentHandler { 34 private static final String TAG = "ChreIntentHandler"; 35 36 /** 37 * Handles CHRE intents. 38 * 39 * @param intent the intent, non null 40 * @param nanoappId ID of the RPC Server nanoapp 41 * @param rpcClient The Pigweed RPC client, non null 42 * @param channelOutput The ChannelOutput used by Pigweed, non null 43 */ handle(Intent intent, long nanoappId, Client rpcClient, ChreChannelOutput channelOutput)44 public static void handle(Intent intent, long nanoappId, Client rpcClient, 45 ChreChannelOutput channelOutput) { 46 Objects.requireNonNull(intent); 47 Objects.requireNonNull(rpcClient); 48 Objects.requireNonNull(channelOutput); 49 50 ContextHubIntentEvent event = ContextHubIntentEvent.fromIntent(intent); 51 52 switch (event.getEventType()) { 53 case ContextHubManager.EVENT_NANOAPP_LOADED: 54 // Nothing to do. 55 break; 56 case ContextHubManager.EVENT_NANOAPP_UNLOADED: 57 rpcClient.closeChannel(channelOutput.getChannelId()); 58 break; 59 case ContextHubManager.EVENT_NANOAPP_ENABLED: 60 // Nothing to do. 61 break; 62 case ContextHubManager.EVENT_NANOAPP_DISABLED: 63 rpcClient.closeChannel(channelOutput.getChannelId()); 64 break; 65 case ContextHubManager.EVENT_NANOAPP_ABORTED: 66 rpcClient.closeChannel(channelOutput.getChannelId()); 67 break; 68 case ContextHubManager.EVENT_NANOAPP_MESSAGE: 69 if (event.getNanoAppId() == nanoappId) { 70 rpcClient.processPacket(event.getNanoAppMessage().getMessageBody()); 71 } 72 break; 73 case ContextHubManager.EVENT_HUB_RESET: 74 rpcClient.closeChannel(channelOutput.getChannelId()); 75 break; 76 case ContextHubManager.EVENT_CLIENT_AUTHORIZATION: 77 if (event.getNanoAppId() == nanoappId) { 78 if (event.getClientAuthorizationState() == AUTHORIZATION_DENIED) { 79 channelOutput.setAuthDenied(true /* denied */); 80 rpcClient.closeChannel(channelOutput.getChannelId()); 81 } else if (event.getClientAuthorizationState() == AUTHORIZATION_GRANTED) { 82 channelOutput.setAuthDenied(false /* denied */); 83 } 84 } 85 break; 86 default: 87 Log.e(TAG, "Unexpected event: " + event); 88 } 89 } 90 } 91