Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit edb75e34 authored by Marvin Ramin's avatar Marvin Ramin
Browse files

CEC: Extract buildMessage from hex byte string

Extract buildMessage function used in multiple tests, move to HdmiUtils
and verify with tests.

Bug: 173182293
Test: atest HdmiUtilsTest
Change-Id: I5bf108014b73d08d95387215af618c32027f31f4
parent bf126a7a
Loading
Loading
Loading
Loading
+27 −0
Original line number Diff line number Diff line
@@ -536,6 +536,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;
+1 −17
Original line number Diff line number Diff line
@@ -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);
    }
}
+1 −18
Original line number Diff line number Diff line
@@ -372,23 +372,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)));
    }
}
+48 −0
Original line number Diff line number Diff line
@@ -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.HdmiDeviceInfo;
import android.platform.test.annotations.Presubmit;
@@ -598,4 +599,51 @@ public class HdmiUtilsTest {
        assertThat(HdmiUtils.isEligibleAddressForCecVersion(Constants.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});
    }
}