1 /*
2  * Copyright (C) 2020 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 java.util.ArrayList;
20 import java.util.List;
21 
22 /**
23  * Buffer for processing the incoming CEC messages while allocating logical addresses.
24  */
25 final class CecMessageBuffer {
26     private List<HdmiCecMessage> mBuffer = new ArrayList<>();
27     private HdmiControlService mHdmiControlService;
28 
CecMessageBuffer(HdmiControlService hdmiControlService)29     CecMessageBuffer(HdmiControlService hdmiControlService) {
30         mHdmiControlService = hdmiControlService;
31     }
32 
33     /**
34      * Adds a message to the buffer.
35      * Only certain types of messages need to be buffered.
36      * @param message The message to add to the buffer
37      * @return Whether the message was added to the buffer
38      */
bufferMessage(HdmiCecMessage message)39     public boolean bufferMessage(HdmiCecMessage message) {
40         switch (message.getOpcode()) {
41             case Constants.MESSAGE_ACTIVE_SOURCE:
42                 bufferActiveSource(message);
43                 return true;
44             case Constants.MESSAGE_IMAGE_VIEW_ON:
45             case Constants.MESSAGE_TEXT_VIEW_ON:
46                 bufferImageOrTextViewOn(message);
47                 return true;
48             case Constants.MESSAGE_SYSTEM_AUDIO_MODE_REQUEST:
49                 bufferSystemAudioModeRequest(message);
50                 return true;
51             case Constants.MESSAGE_ROUTING_CHANGE:
52                 bufferRoutingChange(message);
53                 return true;
54             case Constants.MESSAGE_SET_STREAM_PATH:
55                 bufferSetStreamPath(message);
56                 return true;
57             // Add here if new message that needs to buffer
58             default:
59                 // Do not need to buffer messages other than above
60                 return false;
61         }
62     }
63 
64     /**
65      * Process all messages in the buffer.
66      */
processMessages()67     public void processMessages() {
68         for (final HdmiCecMessage message : mBuffer) {
69             mHdmiControlService.runOnServiceThread(new Runnable() {
70                 @Override
71                 public void run() {
72                     mHdmiControlService.handleCecCommand(message);
73                 }
74             });
75         }
76         mBuffer.clear();
77     }
78 
bufferActiveSource(HdmiCecMessage message)79     private void bufferActiveSource(HdmiCecMessage message) {
80         if (!replaceMessageIfBuffered(message, Constants.MESSAGE_ACTIVE_SOURCE)) {
81             mBuffer.add(message);
82         }
83     }
84 
bufferImageOrTextViewOn(HdmiCecMessage message)85     private void bufferImageOrTextViewOn(HdmiCecMessage message) {
86         if (!replaceMessageIfBuffered(message, Constants.MESSAGE_IMAGE_VIEW_ON)
87                 && !replaceMessageIfBuffered(message, Constants.MESSAGE_TEXT_VIEW_ON)) {
88             mBuffer.add(message);
89         }
90     }
91 
bufferSystemAudioModeRequest(HdmiCecMessage message)92     private void bufferSystemAudioModeRequest(HdmiCecMessage message) {
93         if (!replaceMessageIfBuffered(message, Constants.MESSAGE_SYSTEM_AUDIO_MODE_REQUEST)) {
94             mBuffer.add(message);
95         }
96     }
97 
bufferRoutingChange(HdmiCecMessage message)98     private void bufferRoutingChange(HdmiCecMessage message) {
99         if (!replaceMessageIfBuffered(message, Constants.MESSAGE_ROUTING_CHANGE)) {
100             mBuffer.add(message);
101         }
102     }
103 
bufferSetStreamPath(HdmiCecMessage message)104     private void bufferSetStreamPath(HdmiCecMessage message) {
105         if (!replaceMessageIfBuffered(message, Constants.MESSAGE_SET_STREAM_PATH)) {
106             mBuffer.add(message);
107         }
108     }
109 
getBuffer()110     public List<HdmiCecMessage> getBuffer() {
111         return new ArrayList<>(mBuffer);
112     }
113 
114     // Returns true if the message is replaced
replaceMessageIfBuffered(HdmiCecMessage message, int opcode)115     private boolean replaceMessageIfBuffered(HdmiCecMessage message, int opcode) {
116         for (int i = 0; i < mBuffer.size(); i++) {
117             HdmiCecMessage bufferedMessage = mBuffer.get(i);
118             if (bufferedMessage.getOpcode() == opcode) {
119                 mBuffer.set(i, message);
120                 return true;
121             }
122         }
123         return false;
124     }
125 }
126