1 /*
2  * Copyright (C) 2014 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 
17 package com.android.server.hdmi;
18 
19 import android.util.FastImmutableArraySet;
20 import android.util.SparseArray;
21 
22 /**
23  * Cache for incoming message. It caches {@link HdmiCecMessage} with source address and opcode
24  * as a key.
25  *
26  * <p>Note that whenever a device is removed it should call {@link #flushMessagesFrom(int)}
27  * to clean up messages come from the device.
28  */
29 final class HdmiCecMessageCache {
30     private static final FastImmutableArraySet<Integer> CACHEABLE_OPCODES =
31             new FastImmutableArraySet<>(new Integer[] {
32                     Constants.MESSAGE_SET_OSD_NAME,
33                     Constants.MESSAGE_REPORT_PHYSICAL_ADDRESS,
34                     Constants.MESSAGE_DEVICE_VENDOR_ID,
35                     Constants.MESSAGE_CEC_VERSION,
36             });
37 
38     // It's like [Source Logical Address, [Opcode, HdmiCecMessage]].
39     private final SparseArray<SparseArray<HdmiCecMessage>> mCache = new SparseArray<>();
40 
HdmiCecMessageCache()41     HdmiCecMessageCache() {
42     }
43 
44     /**
45      * Return a {@link HdmiCecMessage} corresponding to the given {@code address} and
46      * {@code opcode}.
47      *
48      * @param address a logical address of source device
49      * @param opcode opcode of message
50      * @return null if has no {@link HdmiCecMessage} matched to the given {@code address} and {code
51      *         opcode}
52      */
getMessage(int address, int opcode)53     public HdmiCecMessage getMessage(int address, int opcode) {
54         SparseArray<HdmiCecMessage> messages = mCache.get(address);
55         if (messages == null) {
56             return null;
57         }
58 
59         return messages.get(opcode);
60     }
61 
62     /**
63      * Flush all {@link HdmiCecMessage}s sent from the given {@code address}.
64      *
65      * @param address a logical address of source device
66      */
flushMessagesFrom(int address)67     public void flushMessagesFrom(int address) {
68         mCache.remove(address);
69     }
70 
71     /**
72      * Flush all cached {@link HdmiCecMessage}s.
73      */
flushAll()74     public void flushAll() {
75         mCache.clear();
76     }
77 
78     /**
79      * Cache incoming {@link HdmiCecMessage}. If opcode of message is not listed on
80      * cacheable opcodes list, just ignore it.
81      *
82      * @param message a {@link HdmiCecMessage} to be cached
83      */
cacheMessage(HdmiCecMessage message)84     public void cacheMessage(HdmiCecMessage message) {
85         int opcode = message.getOpcode();
86         if (!isCacheable(opcode)) {
87             return;
88         }
89 
90         int source = message.getSource();
91         SparseArray<HdmiCecMessage> messages = mCache.get(source);
92         if (messages == null) {
93             messages = new SparseArray<>();
94             mCache.put(source, messages);
95         }
96         messages.put(opcode, message);
97     }
98 
isCacheable(int opcode)99     private boolean isCacheable(int opcode) {
100         return CACHEABLE_OPCODES.contains(opcode);
101     }
102 }
103