0% found this document useful (0 votes)
118 views5 pages

C Programming Lab 6: Sensor Data Management

This document provides instructions for a lab exercise on creating and using static and dynamic libraries in C programming. Students will create libraries containing list implementations and use them in example code. They will also write a data manager program to collect sensor data from files into a linked list, compute running averages, and log events if temperatures exceed thresholds. The program must read mappings of room and sensor IDs from one file and sensor readings from a binary file to populate and update the list over time.

Uploaded by

AHMED AL HELALI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views5 pages

C Programming Lab 6: Sensor Data Management

This document provides instructions for a lab exercise on creating and using static and dynamic libraries in C programming. Students will create libraries containing list implementations and use them in example code. They will also write a data manager program to collect sensor data from files into a linked list, compute running averages, and log events if temperatures exceed thresholds. The program must read mappings of room and sensor IDs from one file and sensor readings from a binary file to populate and update the list over time.

Uploaded by

AHMED AL HELALI
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

System Programming

C programming manual: lab 6


2015 - 2016

Bachelor Electronics/ICT

Course coördinator: Luc Vandeurzen


Lab coaches: Jeroen Van Aken
Stef Desmet
Tim stas
Luc Vandeurzen

Last update: March 7, 2016


C programming

Lab targets: learn to create and use static and dynamic libraries, ldd, nm, and objdump;
be able to implement code related to file reading and writing.

For your information


The picture below visually sketches the final assignment of this course. The relationship
of this lab to the final assignment is indicated by the dashed blue line.

App process
Room/
TCP sensor SQL-connection
file
TCP Connection Data Storage
Manager manager manager

Store sensor Fetch sensor Fetch sensor


data data data

Shared data
Sensor data

FIFO: log events

Log
Log process file

Exercise 1: create and use a static library


Create a static library containing the list implementation. Copy the library to a local
directory in your home folder, e.g. /home/lucvd/mylibs. Implement a main.c function that
uses a list. Build main.c and the list library to an executable (assuming that the main.c file
is not in the same directory of the static library). Run and test the program. Use ‘nm’ and
'objdump' to find out the addresses of the symbols in your code and check if the library
code is really included in the executable.
Exercise 2: create and use a dynamic library
This is a similar exercise as the previous one, but this time we build a shared library
containing the list implementation. Again, copy the library to the local directory in your
home folder, e.g. /home/lucvd/mylibs. Use the main.c function from the previous exercise

System programming: lab 6 -2-


to build an executable using the list library (again assuming that the main.c file is not in
the same directory of the dynamic library). Use 'ldd' to find out if the loader has a
reference to the shared library. If that's ok, run and test the program. Again, Use ‘nm’ and
'objdump' to find out the addresses of the symbols in your code and check that the library
code is NOT really included in the executable.
Exercise 3: working with text and binary files, using a pointer list to organize data
THE SOLUTION OF THIS EXERCISE NEEDS TO BE UPLOADED AS A ZIP FILE ON
[Link] BEFORE THE NEXT LAB.

YOUR SOLUTION IS ONLY ACCEPTED IF THE CRITERIA FOR THIS EXERCISE AS


DESCRIBED ON [Link] ARE SATISFIED!

Assume that a sensor network is used to monitor the temperature of all rooms in an office
building. Write a program, called the ‘data manager’, that collects sensor data and
implements the sensor system intelligence. For example, the data manager could apply
decision logic to control a HVAC installation. Obviously, controlling the HVAC
installation of a real building is not an option (and we are happy for that!).

Sensor data Room/


sensor Data sensor
data manager file
file

log events

stderr

Before handling sensor data, the data manager should first read a file called
'room_sensor.map' containing all room - sensor node mappings. The file is a text file (i.e.
you can open and modify the file in a standard editor) with every line having the format:
<room ID><space><sensor ID><\n>
A room ID and sensor ID are both positive 16-bit integers (uint16_t).
The data manager organizes all sensor nodes in a pointer list data structure. Use the
library implementation of a pointer list implemented in the previous exercises to do this
(try the static and dynamic version). An element in this list maintains at least information
on (i) sensor node ID, (ii) room ID, (iii) data to compute a running average, and (iv) a
last-modified timestamp that contains the timestamp of the last received sensor data used
to update the running average of this sensor. The picture below visualizes this data

System programming: lab 6 -3-


structure.

dplist_t dplist_node_t
dplist_node_t *
dplist_t * head

callback fct

pointers
Void *

sensor ID sensor ID
sensor ID
room ID room ID
room ID
running avg running avg
running avg
Last-modified Last-modified
Last-modified

The data manager starts collecting sensor data and computes for every sensor node a
running average. We define a running average in this context as the average of the last
RUN_AVG_LENGTH sensor values. If this running average exceeds a minimum or
maximum temperature value, a log-event (message send to stderr) should be generated
indicating in which room it’s too cold or too hot. RUN_AVG_LENGTH should be set at
compile-time with the preprocessor directives RUN_AVG_LENGTH=<some_value>. If
this isn't done, the default value of 5 should be used. It should also be possible to define
the minimum and maximum temperature values at compile-time with the preprocessor
directives SET_MIN_TEMP=<some_value> and SET_MAX_TEMP=<some_value>
where <some_value> is expressed in degrees Celsius. Compilation should fail and an
appropriate error message should be printed when these preprocessor directives are not
set at compile-time.
Sensor data is defined as a struct with the following fields (see also the given ‘config.h’
file):
• sensor_id: a positive 16-bit integer;
• temperature: a double;
• timestamp: a time_t value;
The data manager reads sensor data from a binary file called 'sensor_data' with the
format:

<sensor ID><temperature><timestamp><sensor ID><temperature><timestamp> ...

Notice this is a binary file, not readable in a text editor, and spaces and newlines (\n) have
no meaning.
A program ‘file_creator’ is given to generate the ‘room_sensor.map’ and ‘sensor_data’
files to test your program. If you compile this program with the ‘-DDEBUG’ flag on, the
sensor data is also printed to a text file. You may assume that the sensor data on file is

System programming: lab 6 -4-


sorted on the timestamps (earliest sensor data first). Sensor data of a sensor ID that did
not occur in the room_sensor.map file is ignored, but an appropriate message is logged.
Finally, organize your code wisely in a main.c, a datamgr.c and a datamgr.h file. This will
help you to easily re-use the data manager code for the final assignment!

System programming: lab 6 -5-

Common questions

Powered by AI

When a sensor ID does not appear in the 'room_sensor.map' file, the mechanism involves ignoring the data from that sensor, coupled with appropriate logging of a message. This ensures the system does not process irrelevant data but keeps track of such occurrences for debugging or analysis. This error-handling mechanism highlights the importance of robust systems that can operate smoothly despite data discrepancies .

Using both static and dynamic versions of the pointer list library in the lab assignment serves several educational purposes. It exposes students to different methods of linking and emphasizes the importance of flexibility and efficiency in software design. Static libraries are directly included in executables, facilitating standalone use, whereas dynamic libraries allow code sharing across programs, reducing memory use and enabling easier updates. This dual approach imparts an understanding of trade-offs and application scenarios where each method might be preferable .

The primary goal of the task related to static and dynamic libraries in the lab assignment is to learn how to create and use both types of libraries. Students are expected to create a static library containing the list implementation, use it in a main.c file, and check symbol addresses using ‘nm’ and 'objdump'. Similarly, they are required to create a shared library, use 'ldd' to verify references, and confirm library code is not included in the executable .

Using both ‘nm’ and 'objdump' commands is necessary for detailed symbol management during compilation. ‘nm’ helps verify the inclusion of library code by listing symbols in object files, while 'objdump' provides a low-level examination of an executable, showing symbol details such as addresses. This combination ensures thorough validation of whether static library code is included and dynamic links resolve properly. It is crucial for debugging, optimizing linkages, and understanding the executable's structure .

Logging events when temperature thresholds are exceeded serves as an alert mechanism to identify rooms where conditions are deemed 'too cold' or 'too hot.' These events are configured using preprocessor directives to set minimum and maximum temperature parameters, ensuring that log messages are only generated for significant deviations. This component simulates the data manager's ability to trigger responses or further investigation in a real-world HVAC system .

The 'room_sensor.map' file is a text file used by the data manager to map room IDs to sensor IDs. Each line contains a room ID and a sensor ID, separated by a space, with both IDs being positive 16-bit integers. This mapping allows the data manager to organize sensor nodes into a pointer list data structure, coordinating which sensor data corresponds to which room, essential for calculating temperature averages and monitoring conditions per room .

Organizing sensor data into a pointer list data structure benefits the data manager by allowing efficient data handling and facilitating complex operations like sorting and averaging. This flexibility is crucial when managing dynamic data, such as computing running averages of temperatures and time-stamping updates. The pointer list structure also enables extensibility and reusability of data management code for different applications, as it can easily incorporate various data types like sensor IDs, room IDs, and temperature data .

Preprocessor directives in this lab assignment are crucial for setting compile-time constants that govern the program's behavior. They define parameters such as RUN_AVG_LENGTH, SET_MIN_TEMP, and SET_MAX_TEMP, which control the length of the running average and the temperature thresholds for logging events. These directives allow for flexibility and customization without altering the source code. Compilation is set to fail, providing an error message if these directives are not defined, ensuring that critical parameters are configured before execution .

The data manager contributes to HVAC system operation by collecting sensor data, performing calculations like running averages, and determining if temperature extremes occur in any room. This logic can potentially trigger HVAC actions, although actual HVAC control is not implemented in the lab. The manager uses sensor IDs and computes running averages to decide whether conditions are 'too cold' or 'too hot,' logging such events, which simulates decision-making for HVAC controls .

The program handles varying RUN_AVG_LENGTH values using compile-time preprocessor directives, allowing dynamic assignment of this constant without altering the source code. This approach ensures flexibility and robustness, enabling different running average computations to be easily configured for different scenarios. It allows testing and optimization of the data manager under varying conditions and constraints, enhancing the educational value of this exercise .

You might also like