Merge "CEC: Extract buildMessage from hex byte string"
This commit is contained in:
committed by
Android (Google) Code Review
commit
f20b2e4b51
@@ -537,6 +537,33 @@ final class HdmiUtils {
|
||||
return cmd.getParams()[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a CEC message from a hex byte string with bytes separated by {@code :}.
|
||||
*
|
||||
* <p>This format is used by both cec-client and www.cec-o-matic.com
|
||||
*/
|
||||
public static HdmiCecMessage buildMessage(String message) {
|
||||
String[] parts = message.split(":");
|
||||
|
||||
if (parts.length < 2) {
|
||||
throw new IllegalArgumentException("Message is too short");
|
||||
}
|
||||
for (String part : parts) {
|
||||
if (part.length() != 2) {
|
||||
throw new IllegalArgumentException("Malformatted CEC message: " + message);
|
||||
}
|
||||
}
|
||||
|
||||
int src = Integer.parseInt(parts[0].substring(0, 1), 16);
|
||||
int dest = Integer.parseInt(parts[0].substring(1, 2), 16);
|
||||
int opcode = Integer.parseInt(parts[1], 16);
|
||||
byte[] params = new byte[parts.length - 2];
|
||||
for (int i = 0; i < params.length; i++) {
|
||||
params[i] = (byte) Integer.parseInt(parts[i + 2], 16);
|
||||
}
|
||||
return new HdmiCecMessage(src, dest, opcode, params);
|
||||
}
|
||||
|
||||
public static class ShortAudioDescriptorXmlParser {
|
||||
// We don't use namespaces
|
||||
private static final String NS = null;
|
||||
|
||||
@@ -18,6 +18,7 @@ package com.android.server.hdmi;
|
||||
import static com.android.server.hdmi.Constants.ADDR_AUDIO_SYSTEM;
|
||||
import static com.android.server.hdmi.Constants.ADDR_PLAYBACK_1;
|
||||
import static com.android.server.hdmi.Constants.ADDR_TV;
|
||||
import static com.android.server.hdmi.HdmiUtils.buildMessage;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
@@ -183,21 +184,4 @@ public class HdmiCecMessageBuilderTest {
|
||||
|
||||
assertThat(message).isEqualTo(buildMessage("0F:A6:06:10:4A:10"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a CEC message from a hex byte string with bytes separated by {@code :}.
|
||||
*
|
||||
* <p>This format is used by both cec-client and www.cec-o-matic.com
|
||||
*/
|
||||
private static HdmiCecMessage buildMessage(String message) {
|
||||
String[] parts = message.split(":");
|
||||
int src = Integer.parseInt(parts[0].substring(0, 1), 16);
|
||||
int dest = Integer.parseInt(parts[0].substring(1, 2), 16);
|
||||
int opcode = Integer.parseInt(parts[1], 16);
|
||||
byte[] params = new byte[parts.length - 2];
|
||||
for (int i = 0; i < params.length; i++) {
|
||||
params[i] = (byte) Integer.parseInt(parts[i + 2], 16);
|
||||
}
|
||||
return new HdmiCecMessage(src, dest, opcode, params);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -513,23 +513,6 @@ public class HdmiCecMessageValidatorTest {
|
||||
}
|
||||
|
||||
private IntegerSubject assertMessageValidity(String message) {
|
||||
return assertThat(mHdmiCecMessageValidator.isValid(buildMessage(message)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a CEC message from a hex byte string with bytes separated by {@code :}.
|
||||
*
|
||||
* <p>This format is used by both cec-client and www.cec-o-matic.com
|
||||
*/
|
||||
private static HdmiCecMessage buildMessage(String message) {
|
||||
String[] parts = message.split(":");
|
||||
int src = Integer.parseInt(parts[0].substring(0, 1), 16);
|
||||
int dest = Integer.parseInt(parts[0].substring(1, 2), 16);
|
||||
int opcode = Integer.parseInt(parts[1], 16);
|
||||
byte[] params = new byte[parts.length - 2];
|
||||
for (int i = 0; i < params.length; i++) {
|
||||
params[i] = (byte) Integer.parseInt(parts[i + 2], 16);
|
||||
}
|
||||
return new HdmiCecMessage(src, dest, opcode, params);
|
||||
return assertThat(mHdmiCecMessageValidator.isValid(HdmiUtils.buildMessage(message)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.testng.Assert.assertThrows;
|
||||
|
||||
import android.hardware.hdmi.HdmiControlManager;
|
||||
import android.hardware.hdmi.HdmiDeviceInfo;
|
||||
@@ -614,4 +615,51 @@ public class HdmiUtilsTest {
|
||||
assertThat(HdmiUtils.isEligibleAddressForCecVersion(HdmiControlManager.HDMI_CEC_VERSION_2_0,
|
||||
Constants.ADDR_SPECIFIC_USE)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildMessage_validation() {
|
||||
assertThrows(IllegalArgumentException.class, () -> HdmiUtils.buildMessage("04"));
|
||||
assertThrows(IllegalArgumentException.class, () -> HdmiUtils.buildMessage("041"));
|
||||
assertThrows(IllegalArgumentException.class, () -> HdmiUtils.buildMessage("041:00"));
|
||||
assertThrows(IllegalArgumentException.class, () -> HdmiUtils.buildMessage("04:000"));
|
||||
assertThrows(IllegalArgumentException.class, () -> HdmiUtils.buildMessage("04:00:000"));
|
||||
assertThrows(IllegalArgumentException.class, () -> HdmiUtils.buildMessage("04:00:00:000"));
|
||||
|
||||
assertThrows(NumberFormatException.class, () -> HdmiUtils.buildMessage("G0:00"));
|
||||
assertThrows(NumberFormatException.class, () -> HdmiUtils.buildMessage("0G:00"));
|
||||
assertThrows(NumberFormatException.class, () -> HdmiUtils.buildMessage("04:G0"));
|
||||
assertThrows(NumberFormatException.class, () -> HdmiUtils.buildMessage("04:00:G0"));
|
||||
assertThrows(NumberFormatException.class, () -> HdmiUtils.buildMessage("04:00:0G"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildMessage_source() {
|
||||
assertThat(HdmiUtils.buildMessage("04:00").getSource()).isEqualTo(Constants.ADDR_TV);
|
||||
assertThat(HdmiUtils.buildMessage("40:00").getSource()).isEqualTo(
|
||||
Constants.ADDR_PLAYBACK_1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildMessage_destination() {
|
||||
assertThat(HdmiUtils.buildMessage("04:00").getDestination()).isEqualTo(
|
||||
Constants.ADDR_PLAYBACK_1);
|
||||
assertThat(HdmiUtils.buildMessage("40:00").getDestination()).isEqualTo(Constants.ADDR_TV);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildMessage_opcode() {
|
||||
assertThat(HdmiUtils.buildMessage("04:00").getOpcode()).isEqualTo(
|
||||
Constants.MESSAGE_FEATURE_ABORT);
|
||||
assertThat(HdmiUtils.buildMessage("04:36").getOpcode()).isEqualTo(
|
||||
Constants.MESSAGE_STANDBY);
|
||||
assertThat(HdmiUtils.buildMessage("04:FF").getOpcode()).isEqualTo(
|
||||
Integer.parseInt("FF", 16));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildMessage_params() {
|
||||
assertThat(HdmiUtils.buildMessage("04:00:00").getParams()).isEqualTo(new byte[]{0x00});
|
||||
assertThat(HdmiUtils.buildMessage("40:32:65:6E:67").getParams()).isEqualTo(
|
||||
new byte[]{0x65, 0x6E, 0x67});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user