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