CEC: Add External Timer validator

Used to validate <Set External Timer> and
<Clear External Timer> message parameters

Test: atest HdmiCecMessageValidatorTest
Bug: 170811408
Change-Id: I49a33e91937907fe5a5828c1cdf31d0004a01d46
This commit is contained in:
Shraddha Basantwani
2020-10-12 15:35:59 +05:30
parent e8543c290e
commit bcae9da108
2 changed files with 91 additions and 0 deletions

View File

@@ -125,8 +125,12 @@ public class HdmiCecMessageValidator {
// TODO: Handle messages for the Timer Programming.
addValidationInfo(
Constants.MESSAGE_CLEAR_ANALOG_TIMER, new AnalogueTimerValidator(), DEST_DIRECT);
addValidationInfo(
Constants.MESSAGE_CLEAR_EXTERNAL_TIMER, new ExternalTimerValidator(), DEST_DIRECT);
addValidationInfo(
Constants.MESSAGE_SET_ANALOG_TIMER, new AnalogueTimerValidator(), DEST_DIRECT);
addValidationInfo(
Constants.MESSAGE_SET_EXTERNAL_TIMER, new ExternalTimerValidator(), DEST_DIRECT);
// Messages for the System Information.
FixedLengthValidator oneByteValidator = new FixedLengthValidator(1);
@@ -455,6 +459,40 @@ public class HdmiCecMessageValidator {
return isWithinRange(value, 0, 31);
}
/**
* Check if the given value is a valid External Plug. A valid value is one which falls within
* the range description defined in CEC 1.4 Specification : Operand Descriptions (Section 17)
*
* @param value External Plug
* @return true if the External Plug is valid
*/
private boolean isValidExternalPlug(int value) {
return isWithinRange(value, 1, 255);
}
/**
* Check if the given value is a valid External Source. A valid value is one which falls within
* the range description defined in CEC 1.4 Specification : Operand Descriptions (Section 17)
*
* @param value External Source Specifier
* @return true if the External Source is valid
*/
private boolean isValidExternalSource(byte[] params, int offset) {
int externalSourceSpecifier = params[offset];
offset = offset + 1;
if (externalSourceSpecifier == 0x04) {
// External Plug
return isValidExternalPlug(params[offset]);
} else if (externalSourceSpecifier == 0x05) {
// External Physical Address
// Validate it contains 2 bytes Physical Address
if (params.length - offset >= 2) {
return isValidPhysicalAddress(params, offset);
}
}
return false;
}
private class PhysicalAddressValidator implements ParameterValidator {
@Override
public int isValid(byte[] params) {
@@ -610,4 +648,27 @@ public class HdmiCecMessageValidator {
&& isValidBroadcastSystem(params[10])); // Broadcast System
}
}
/**
* Check if the given External Timer message parameters are valid. Valid parameters should
* adhere to message description of External Timer defined in CEC 1.4 Specification : Message
* Descriptions for Timer Programming Feature (CEC Table 12)
*/
private class ExternalTimerValidator implements ParameterValidator {
@Override
public int isValid(byte[] params) {
if (params.length < 9) {
return ERROR_PARAMETER_SHORT;
}
return toErrorCode(
isValidDayOfMonth(params[0]) // Day of Month
&& isValidMonthOfYear(params[1]) // Month of Year
&& isValidHour(params[2]) // Start Time - Hour
&& isValidMinute(params[3]) // Start Time - Minute
&& isValidDurationHours(params[4]) // Duration - Duration Hours
&& isValidMinute(params[5]) // Duration - Minute
&& isValidRecordingSequence(params[6]) // Recording Sequence
&& isValidExternalSource(params, 7)); // External Source
}
}
}

View File

@@ -216,6 +216,36 @@ public class HdmiCecMessageValidatorTest {
assertMessageValidity("04:34:04:0C:16:0F:08:37:00:02:EA:60:20").isEqualTo(ERROR_PARAMETER);
}
@Test
public void isValid_setExternalTimer_clearExternalTimer() {
assertMessageValidity("40:A1:0C:08:15:05:04:1E:02:04:20").isEqualTo(OK);
assertMessageValidity("40:A2:14:09:12:28:4B:19:10:05:10:00").isEqualTo(OK);
assertMessageValidity("4F:A1:0C:08:15:05:04:1E:02:04:20").isEqualTo(ERROR_DESTINATION);
assertMessageValidity("F4:A2:14:09:12:28:4B:19:10:05:10:00").isEqualTo(ERROR_SOURCE);
assertMessageValidity("40:A1:0C:08:15:05:04:1E:02:04").isEqualTo(ERROR_PARAMETER_SHORT);
// Out of range Day of Month
assertMessageValidity("40:A2:28:09:12:28:4B:19:10:05:10:00").isEqualTo(ERROR_PARAMETER);
// Out of range Month of Year
assertMessageValidity("40:A1:0C:0F:15:05:04:1E:02:04:20").isEqualTo(ERROR_PARAMETER);
// Out of range Start Time - Hour
assertMessageValidity("40:A2:14:09:1A:28:4B:19:10:05:10:00").isEqualTo(ERROR_PARAMETER);
// Out of range Start Time - Minute
assertMessageValidity("40:A1:0C:08:15:48:04:1E:02:04:20").isEqualTo(ERROR_PARAMETER);
// Out of range Duration - Duration Hours
assertMessageValidity("40:A2:14:09:12:28:66:19:10:05:10:00").isEqualTo(ERROR_PARAMETER);
// Out of range Duration - Minute
assertMessageValidity("40:A1:0C:08:15:05:04:3F:02:04:20").isEqualTo(ERROR_PARAMETER);
// Invalid Recording Sequence
assertMessageValidity("40:A2:14:09:12:28:4B:19:84:05:10:00").isEqualTo(ERROR_PARAMETER);
// Invalid Recording Sequence
assertMessageValidity("40:A1:0C:08:15:05:04:1E:14:04:20").isEqualTo(ERROR_PARAMETER);
// Invalid external source specifier
assertMessageValidity("40:A2:14:09:12:28:4B:19:10:08:10:00").isEqualTo(ERROR_PARAMETER);
// Invalid External PLug
assertMessageValidity("04:A1:0C:08:15:05:04:1E:02:04:00").isEqualTo(ERROR_PARAMETER);
}
private IntegerSubject assertMessageValidity(String message) {
return assertThat(mHdmiCecMessageValidator.isValid(buildMessage(message)));
}