165. 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

 

 

 

Need Help? Refer to the Quick Guide below

Encapsulation is the mechanism of bundling data (variables) and methods (functions) together into a single unit (a Class) and restricting direct access to some of that object's components.

It is not just about "hiding secrets"; it is about System Integrity. By hiding internal data (private), you ensure that the object cannot be put into an invalid or dangerous state by external code. The object manages its own state through a controlled interface (public).

Access Specifiers (The Security Levels)

C++ provides three keywords to control who can see and modify class members.

SpecifierAccessible ByUsage Scenarios
publicEveryone (External code, main()).The Public API (functions users are meant to call).
privateOnly the Class itself.Internal state, helper functions, hardware register pointers.
protectedThe Class + Derived Classes (Children).Internal logic shared with children but hidden from the world.

Detailed Syntax & Usage

The "Safe Hardware" Example

Imagine a Motor Driver where the duty cycle must never exceed 90% (safety limit) and never be negative.

class MotorDriver {
private: 
    // DATA HIDING:
    // If this were public, a user could write 'duty_cycle = 200;' and burn the motor.
    float duty_cycle;
    uint32_t* pwm_reg; 

    // INTERNAL HELPER:
    // A complex hardware sequence user doesn't need to see.
    void trigger_update() { 
        *pwm_reg = (uint32_t)(duty_cycle * 1000); 
    }

public:
    // CONSTRUCTOR: Initializes state safely.
    MotorDriver() : duty_cycle(0.0f) { /* ... */ }

    // PUBLIC INTERFACE (Setter):
    // Contains Validation Logic (The "Invariant").
    void setSpeed(float speed) {
        if (speed > 90.0f) speed = 90.0f; // Clamp to safe max
        if (speed < 0.0f)  speed = 0.0f;  // Clamp to safe min
        
        duty_cycle = speed; // Update internal state
        trigger_update();   // Update hardware
    }

    // PUBLIC INTERFACE (Getter):
    // Read-only access.
    float getSpeed() const {
        return duty_cycle;
    }
};

Advanced Encapsulation Features

  • The friend Keyword (Controlled Breach):

    Sometimes, you want one specific external class or function to access your private members without making them public to the whole world.

    • Usage: Unit Testing (Test fixture needs to check private state) or tightly coupled classes (e.g., LinkedList accessing Node internals).
    class Box {
    private:
        int secret_data;
        friend class TestSuite; // 'TestSuite' can now touch 'secret_data'
    };
  • Struct vs. Class:

    Technically, they are almost identical. The only difference is the default encapsulation level.

    • class: Members are private by default. (Prefers Hiding).
    • struct: Members are public by default. (Prefers Exposure).
    • Best Practice: Use struct for "Plain Old Data" (POD) where there are no invariants to protect (e.g., Coordinate {x, y}). Use class for everything else.

Why Encapsulation Matters in Embedded Systems

  • Maintaining Invariants:

    An invariant is a condition that must always be true.

    • Example: "The Buffer Head index must always be < BUFFER_SIZE".
    • If head is public, user code buf.head++ might overflow. If head is private, the push() function guarantees the wrap-around logic (head = (head + 1) % SIZE).
  • Read-Only Hardware:

    Many sensors provide data that should be read but never written to by software.

    • Solution: Make the variable private and provide only a getVal() function. No setVal() means the data is effectively read-only.
  • Decoupling Implementation:

    You can change the internal logic without breaking user code.

    • Scenario: You switch from a software timer to a hardware timer.
    • If users accessed the timer_counter variable directly, their code breaks.
    • If users called timer.getTime(), you just update the function body. Their code remains untouched.

Performance Myths (Inlining)

The Fear: "Calling getSpeed() adds function call overhead (stack push/pop). Direct access motor.speed is faster."

The Reality: C++ compilers are incredibly smart. If you define the getter in the header file:

// In header
inline float getSpeed() const { return duty_cycle; }

The compiler performs Inlining. It replaces the function call with the direct assembly instruction to read the memory address.

Result: You get the safety of Encapsulation with Zero Performance Penalty.

Common Pitfalls & Best Practices

PitfallDetails
❌ Getters/Setters for EverythingDon't automatically create getVariable() and setVariable() for every private member. This breaks encapsulation by exposing internals. Only expose what is necessary for the API.
❌ Returning Non-Const PointersIf you have private: int* buffer; and a public function int* getBuf() { return buffer; }, you have broken encapsulation. The user can now trash your private memory through that pointer. Fix: Return const int* or a safe copy.
❌ Friend AbuseOverusing friend turns your code into "Spaghetti Code" where everything touches everything else. Use it only when strictly necessary.
✅ The "Const Correctness"Always mark getters as const (e.g., int get() const). This assures the compiler and the user that this function will not modify the object's state.