128. Abstract Communication Driver

In embedded firmware systems, communication peripherals such as UART and SPI are often accessed through a common abstract interface. This allows higher-level modules to interact with different hardware drivers without changing application logic.

In this problem, you will implement an abstract communication driver using Embedded C++ concepts.

You are given a base class CommDriver. Your task is to declare two pure virtual member functions in this class:

  • send(const string& data)
  • receive() → returns string

Two derived classes, UartDriver and SpiDriver, already implement these functions.

The program will:

  1. Read a driver type ("UART" or "SPI")
  2. Read a message (single word, no spaces)
  3. Create the appropriate driver object
  4. Send the message
  5. Print the value returned by receive()

This exercise focuses on abstract base classes, pure virtual functions, and runtime polymorphism, which are commonly used in embedded HAL (Hardware Abstraction Layer) design.

Input / Output Specification:

Input

  • A single line containing:
    • type"UART" or "SPI"
    • message → non-empty, single-word string (no spaces)

Output

  • Two lines:
    1. <TYPE> SEND: <message>
    2. <TYPE> RECV: <message>

Example:

Input:

UART Hello

Output:

UART SEND: Hello 
UART RECV: Hello

Constraints & Assumptions:

  • Driver type is always valid
  • Message length ≥ 1
  • Message contains no whitespace
  • This is a host-based simulation of firmware behavior

 

 

 

Loading...

Input

UART Hello

Expected Output

UART SEND: Hello UART RECV: Hello