128. UART Baud Rate Validation

A UARTDriver class manages the baud rate configuration for a UART peripheral in an embedded system.
Due to hardware clock divider limitations, the UART supports only three discrete baud rates:

  • 9600
  • 19200
  • 115200

The driver currently exposes its baud_rate member publicly, allowing application code to assign arbitrary integer values. This can result in invalid clock configurations and UART communication failure.

Your task is to refactor the driver to protect its internal state and enforce valid hardware configurations.

You must:

  • Encapsulate the baud_rate member.
  • Provide a public setter function that validates requested baud rates.
  • Update the internal baud rate only if the requested value is supported.
  • Ignore invalid requests and retain the previous valid baud rate (fail-safe behavior).
  • Initialize the driver to a default safe baud rate of 9600.

This problem simulates a common firmware requirement: protecting hardware configuration registers from invalid application-level input.

Program Flow:

  1. Initialize a UARTDriver instance (default baud rate: 9600).
  2. Read integer N — the number of baud rate update requests.
  3. Loop N times:
    • Read an integer request_baud.
    • Attempt to update the UART baud rate using the driver’s API.
    • Print the currently active baud rate.

Input Format:

  • First line: Integer N (number of commands)
  • Next N lines: One integer request_baud per line

Input is provided via standard input (stdin).

Output Format:

For each command, print the active baud rate in the following format:

Active: <value> 

Each output must be printed on a new line.

Example:

Input

4
115200
500
19200
-1 

Output

Active: 115200
Active: 115200
Active: 19200
Active: 19200 

Constraints:

  • 1 ≤ N ≤ 20
  • -1,000,000 ≤ request_baud ≤ 1,000,000
  • Allowed baud rates: {9600, 19200, 115200}
  • Initial baud rate: 9600
  • Invalid inputs must not modify the driver state
  • No dynamic memory allocation
  • Deterministic behavior required

 

 

 

Loading...

Input

4 115200 500 19200 -1

Expected Output

Active: 115200 Active: 115200 Active: 19200 Active: 19200