Assignment 6-A

Custom Data Structure Design

Build a comprehensive smart home system using structs, unions, bit fields, and enums. Design and implement device control structures with sensor variants and hardware register simulation.

8-10 hours
Advanced
250 Points
Submit Assignment
What You'll Practice
  • Design complex struct hierarchies
  • Implement unions for variant data
  • Use bit fields for memory efficiency
  • Apply enums for state management
  • Build real-world IoT system
Contents
01

Assignment Overview

In this assignment, you will build a Smart Home Device Controller System that applies ALL concepts from Module 6: structures, nested structures, unions, bit fields, and enumerations. This real-world project demonstrates how professional C code uses custom data types to manage complex systems efficiently.

Standards Compliance: Your code must follow C99 or C11 standard. Use proper memory management, avoid undefined behavior, and write defensive code.
Skills Applied: Structures (Topic 6.1), Nested structures (Topic 6.2), Unions & Bit Fields (Topic 6.3), Enumerations (Topic 6.4).
Structures (6.1)

Nested structs, typedef, arrays, pointers

Unions & Bit Fields (6.3)

Memory-efficient storage, variant data

Enums (6.4)

State machines, custom constants

Ready to submit? Already completed? Submit your work now!
Submit Now
02

The Scenario

SmartHome Systems Inc.

You have been hired as a Firmware Engineer at SmartHome Systems Inc., building IoT devices for home automation. You need to build the core control system that manages various devices and sensors.

"Our IoT hub needs to control temperature sensors, motion detectors, smart lights, and smart locks. Each device type has different data. Build a system that efficiently stores sensor data, manages device states, and simulates hardware register access patterns."

Your Task

Create a C program called smart_home.c (and optionally smart_home.h) that implements a complete smart home device controller. Use structs for complex data, unions for sensor variants, bit fields for device flags, and enums for state management.

03

The Datasets

You will work with TWO data files. Create these files exactly as shown below for testing your system:

File 1: devices.txt (Device Configuration)

TEMPERATURE_SENSOR, 101, 22.5, 65
MOTION_DETECTOR, 102, 1, 5
LIGHT, 103, 200, 4000
LOCK, 104, 1, 85
TEMPERATURE_SENSOR, 105, 19.8, 55
MOTION_DETECTOR, 106, 0, 7
LIGHT, 107, 150, 5000
LOCK, 108, 0, 45

File 2: system_config.txt (System Configuration)

SYSTEM_NAME=SmartHome IoT Controller
DEVICES_PER_ZONE=4
ALERT_THRESHOLD=10
AUTO_MODE_ENABLED=1
DEBUG_MODE=0
POWER_STATE=1
File Format Details
devices.txt Format

Each line contains one device:

  • DEVICE_TYPE - One of: TEMPERATURE_SENSOR, MOTION_DETECTOR, LIGHT, LOCK
  • DEVICE_ID - Unique integer identifier (100+)
  • VALUE_1 - First sensor reading (temperature, motion, brightness, lock_state)
  • VALUE_2 - Second sensor reading (humidity, sensitivity, color_temp, battery)
system_config.txt Format

Configuration key-value pairs:

  • SYSTEM_NAME - System identifier (string)
  • DEVICES_PER_ZONE - Max devices per zone (integer)
  • ALERT_THRESHOLD - Low battery alert level (integer 0-100)
  • AUTO_MODE_ENABLED - Enable automatic control (0 or 1)
  • DEBUG_MODE - Enable detailed logging (0 or 1)
  • POWER_STATE - System power state (0 or 1)

Sample Device Data Breakdown

Device ID Type Value 1 Value 2 Purpose
101 TEMPERATURE_SENSOR 22.5°C 65% humidity Living room climate monitoring
102 MOTION_DETECTOR Motion: Yes (1) Sensitivity: 5 Hallway motion detection
103 LIGHT Brightness: 200 Color Temp: 4000K Living room lighting
104 LOCK Locked: Yes (1) Battery: 85% Front door smart lock
Tip: Create these data files in your project directory and hardcode file paths in your main.c for reading. Your program should parse these files and populate the Device structures.
04

Requirements

Your smart_home.c (with optional smart_home.h) must implement ALL of the following functions and structures. Each function and data type is mandatory and will be tested individually.

1. Required Data Types & Enumerations

Define these enum types and structures in your code:

A
DeviceType Enum
typedef enum {
    TEMPERATURE_SENSOR,
    MOTION_DETECTOR,
    LIGHT,
    LOCK
} DeviceType;
B
DeviceState Enum
typedef enum {
    OFFLINE,
    IDLE,
    ACTIVE,
    ERROR
} DeviceState;
C
SensorData Union
typedef union {
    struct { float temperature; int humidity; } temp_data;
    struct { int motion_detected; int sensitivity; } motion_data;
    struct { int brightness; int color_temp; } light_data;
    struct { int locked; int battery_level; } lock_data;
} SensorData;
D
HardwareRegister with Bit Fields
typedef struct {
    unsigned int power_on : 1;
    unsigned int alert_enabled : 1;
    unsigned int auto_mode : 1;
    unsigned int debug_mode : 1;
    unsigned int reserved : 4;
} HardwareRegister;

2. Device Structure

typedef struct {
    int device_id;
    DeviceType type;
    DeviceState state;
    SensorData data;
    HardwareRegister registers;
    unsigned long last_update;
} Device;

3. Required Functions

1
create_device()

Device* create_device(int device_id, DeviceType type)

  • Allocate device with malloc()
  • Initialize ID, type, and state to OFFLINE
  • Initialize registers to all zeros
  • Return pointer to allocated device
2
update_temperature()

void update_temperature(Device* device, float temp, int humidity)

  • Verify device type is TEMPERATURE_SENSOR
  • Update union with temperature and humidity
  • Set device state to ACTIVE
  • Update last_update timestamp
3
update_motion()

void update_motion(Device* device, int detected)

  • Verify device type is MOTION_DETECTOR
  • Update union with motion detection status
  • Set state to ACTIVE if motion detected, IDLE otherwise
  • Update last_update timestamp
4
control_light()

void control_light(Device* device, int brightness, int color_temp)

  • Verify device type is LIGHT
  • Validate brightness (0-255) and color_temp (2700-6500)
  • Update union with values if valid
  • Set state to ACTIVE if brightness > 0
  • Update last_update timestamp
5
control_lock()

void control_lock(Device* device, int lock_state)

  • Verify device type is LOCK
  • Update union with lock state (0=unlocked, 1=locked)
  • Check battery level and set ERROR state if < 10%
  • Update last_update timestamp
6
set_register_bit()

void set_register_bit(Device* device, int bit_position, int value)

  • Manipulate hardware register bit fields
  • bit_position: 0=power_on, 1=alert_enabled, 2=auto_mode, 3=debug_mode
  • Validate bit_position (0-3) and value (0-1)
  • Use bitwise operations for manipulation
7
display_device_info()

void display_device_info(Device* device)

  • Print device ID, type, and state
  • Print sensor data based on device type
  • Print register status (binary representation)
  • Print last_update timestamp
  • Use formatted output (printf with proper formatting)
8
free_device()

void free_device(Device* device)

  • Properly deallocate memory using free()
  • Set pointer to NULL after freeing (if passing by reference)
  • Include safety check for NULL pointer
05

Submission

Create a public GitHub repository with the exact name shown below:

Required Repository Name
smart-home-iot-system
github.com/<your-username>/smart-home-iot-system
Required Files
smart-home-iot-system/
├── smart_home.c          # Implementation with all 8 functions
├── smart_home.h          # Header file (optional but recommended)
├── main.c                # Main program demonstrating functionality
├── Makefile              # Build instructions
├── program.exe           # Compiled executable (or a.out on Linux)
└── README.md             # REQUIRED - see contents below
README.md Must Include:
  • Your full name and submission date
  • Brief description of your solution approach to the SmartHome system
  • Any challenges faced with structs/unions/bit fields and how you solved them
  • Instructions to compile and run your program (gcc/Makefile)
  • Example output showing device creation and control
Do Include
  • All 8 functions fully implemented
  • All required enums and data structures
  • Proper memory management (malloc/free)
  • Comments explaining complex logic
  • Demonstration of union, enum, and bit field usage
  • Test output showing all device types working
  • README.md with all required sections
  • .gitignore for compiled files
Do Not Include
  • Compiled binaries (.o, .exe files)
  • IDE-specific files (.vs, .vscode)
  • Any .dll or library files
  • Code that doesn't compile cleanly
  • Hardcoded test data (device IDs should be flexible)
  • Missing function implementations
  • Memory leaks or undefined behavior
Compilation Instructions

Your program must compile without warnings using:

gcc -Wall -Wextra -std=c99 -o smart_home smart_home.c main.c

Or using Makefile:

make
./smart_home
Important: Before submitting, compile your program with -Wall -Wextra flags to catch all warnings. Your code must run without errors and properly demonstrate all device types and functions!
Submit Your Assignment

Enter your GitHub username - we'll verify your repository automatically

06

Grading Rubric

Your assignment will be graded on the following criteria:

Component Points Details
Enum & Typedef Definitions 25 Correct DeviceType, DeviceState enums with all values
Struct Design 30 Device struct with proper nesting, union for sensor data
Union Implementation 25 SensorData union correctly handles all device types
Bit Fields 20 HardwareRegister with correct bit field layout and manipulation
create_device() 15 Proper malloc, initialization of all fields
update_temperature() 10 Updates union, validates device type, sets state
update_motion() 10 Handles motion detection, state management
control_light() 15 Validates brightness/color_temp ranges, updates state
control_lock() 15 Manages lock state, checks battery level
set_register_bit() 15 Correct bit manipulation, validation of bit position
display_device_info() 15 Formatted output, handles all device types correctly
free_device() 10 Proper memory deallocation, NULL checks
Memory Management 15 No memory leaks, proper malloc/free usage
Code Quality 20 Comments, clean code, follows C99 standards, compiles without warnings
GitHub Repository & README 15 Proper repo name, all files included, comprehensive README
Main Program & Testing 20 Demonstrates all device types, functions, and features working
TOTAL 260 Total points (scaled to 250 for submission system)
Assignment complete? Submit your GitHub repository below!
Submit Now