Skip to content

Commit e2ef0ed

Browse files
authored
feat: Add initial implementation of the library with CMake build system (#7)
Signed-off-by: Amir Alavi <[email protected]>
1 parent 77c52a6 commit e2ef0ed

30 files changed

+5176
-9
lines changed

.gitignore

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,43 @@
11
.venv
22
.cache
3+
4+
# CMake build files
5+
build*/
6+
CMakeCache.txt
7+
CMakeFiles/
8+
Makefile
9+
cmake_install.cmake
10+
install_manifest.txt
11+
CTestTestfile.cmake
12+
Testing/
13+
14+
# Coverage files
15+
*.gcda
16+
*.gcno
17+
*.gcov
18+
*.info
19+
coverage_html/
20+
coverage_report.md
21+
coverage_summary.txt
22+
23+
# Generated files
24+
*.a
25+
*.so
26+
*.dll
27+
*.exe
28+
29+
# IDE files
30+
.vscode/
31+
.idea/
32+
*.swp
33+
*.swo
34+
*~
35+
36+
# OS generated files
37+
.DS_Store
38+
.DS_Store?
39+
._*
40+
.Spotlight-V100
41+
.Trashes
42+
ehthumbs.db
43+
Thumbs.db

CMakeLists.txt

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
3+
project(EmbedIDS
4+
VERSION 0.1.0
5+
DESCRIPTION "Lightweight Runtime Intrusion Detection SDK for embedded IoT devices"
6+
LANGUAGES C CXX
7+
)
8+
9+
# Standards
10+
set(CMAKE_C_STANDARD 11)
11+
set(CMAKE_C_STANDARD_REQUIRED ON)
12+
set(CMAKE_CXX_STANDARD 17)
13+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
14+
set(CMAKE_CXX_EXTENSIONS OFF)
15+
16+
# Options
17+
option(BUILD_TESTS "Build unit tests" OFF)
18+
option(BUILD_EXAMPLES "Build examples" OFF)
19+
option(ENABLE_COVERAGE "Enable code coverage" OFF)
20+
21+
# Compiler flags
22+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Os -ffunction-sections -fdata-sections")
23+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Os -ffunction-sections -fdata-sections")
24+
25+
# Coverage configuration
26+
if(ENABLE_COVERAGE AND CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
27+
include(CTest)
28+
message(STATUS "Enabling code coverage with ${CMAKE_C_COMPILER_ID}")
29+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -g --coverage")
30+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -g --coverage")
31+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
32+
elseif(ENABLE_COVERAGE)
33+
message(WARNING "Coverage only supported with GCC or Clang")
34+
endif()
35+
36+
# Configure main header with version info
37+
configure_file(include/embedids.h.in ${CMAKE_CURRENT_BINARY_DIR}/include/embedids.h @ONLY)
38+
39+
# Include directories
40+
include_directories(${CMAKE_CURRENT_BINARY_DIR}/include)
41+
42+
# Build targets
43+
add_subdirectory(src)
44+
add_library(EmbedIDS::embedids ALIAS embedids)
45+
46+
if(BUILD_TESTS)
47+
enable_testing()
48+
add_subdirectory(tests)
49+
endif()
50+
51+
if(BUILD_EXAMPLES)
52+
add_subdirectory(examples)
53+
endif()
54+
55+
# Installation
56+
install(TARGETS embedids
57+
ARCHIVE DESTINATION lib
58+
PUBLIC_HEADER DESTINATION include
59+
)
60+
61+
install(FILES
62+
${CMAKE_CURRENT_BINARY_DIR}/include/embedids.h
63+
DESTINATION include
64+
)
65+
66+
# Package configuration
67+
include(CMakePackageConfigHelpers)
68+
write_basic_package_version_file("EmbedIDSConfigVersion.cmake"
69+
VERSION ${PROJECT_VERSION} COMPATIBILITY SameMajorVersion)
70+
configure_package_config_file("cmake/EmbedIDSConfig.cmake.in" "EmbedIDSConfig.cmake"
71+
INSTALL_DESTINATION lib/cmake/EmbedIDS)
72+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/EmbedIDSConfig.cmake"
73+
"${CMAKE_CURRENT_BINARY_DIR}/EmbedIDSConfigVersion.cmake"
74+
DESTINATION lib/cmake/EmbedIDS)
75+
76+
# Coverage targets
77+
if(ENABLE_COVERAGE AND BUILD_TESTS)
78+
add_custom_target(coverage
79+
COMMAND ${CMAKE_CTEST_COMMAND} -T Test -T Coverage --output-on-failure
80+
DEPENDS embedids_tests
81+
COMMENT "Running tests and generating coverage report"
82+
)
83+
message(STATUS "Coverage target added: make coverage")
84+
endif()

README.md

Lines changed: 165 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,166 @@
11
# EmbedIDS
2-
Modern Intrusion Detection Systems (IDS) for Embedded Systems
2+
3+
> **Modern Intrusion Detection System for Embedded Devices & IoT**
4+
5+
EmbedIDS is a lightweight, extensible intrusion detection library designed for embedded systems and IoT devices. It features user-managed memory, custom metrics, and pluggable detection algorithms with zero runtime overhead when disabled.
6+
7+
## Quick Start
8+
9+
```c
10+
#include <embedids.h>
11+
12+
// Allocate history buffer (user-managed memory)
13+
static embedids_metric_datapoint_t cpu_history[50];
14+
15+
// Configure CPU monitoring with 80% threshold
16+
embedids_metric_config_t cpu_config = {
17+
.metric = {
18+
.name = "cpu_usage",
19+
.type = EMBEDIDS_METRIC_TYPE_PERCENTAGE,
20+
.history = cpu_history,
21+
.max_history_size = 50,
22+
.enabled = true
23+
},
24+
.algorithms = {{
25+
.type = EMBEDIDS_ALGORITHM_THRESHOLD,
26+
.enabled = true,
27+
.config.threshold = {
28+
.max_threshold.f32 = 80.0f,
29+
.check_max = true
30+
}
31+
}},
32+
.num_algorithms = 1
33+
};
34+
35+
// Initialize context and system
36+
embedids_context_t context;
37+
memset(&context, 0, sizeof(context));
38+
39+
embedids_system_config_t system = {
40+
.metrics = &cpu_config,
41+
.max_metrics = 1,
42+
.num_active_metrics = 1
43+
};
44+
45+
embedids_init(&context, &system);
46+
47+
// Monitor in real-time
48+
embedids_metric_value_t value = {.f32 = get_cpu_usage()};
49+
embedids_add_datapoint(&context, "cpu_usage", value, timestamp_ms);
50+
51+
if (embedids_analyze_metric(&context, "cpu_usage") != EMBEDIDS_OK) {
52+
handle_intrusion_detected();
53+
}
54+
```
55+
56+
## Architecture & Features
57+
58+
### **Extensible Design**
59+
- **User-Managed Memory**: No malloc/free - perfect for embedded systems
60+
- **Custom Metrics**: Support for float, int, percentage, boolean, enum types
61+
- **Pluggable Algorithms**: Threshold, trend analysis, statistical, and custom detection
62+
- **Multiple Algorithms per Metric**: Run several detection methods simultaneously
63+
- **Real-time Analysis**: Low-latency threat detection with configurable history
64+
65+
### **Detection Algorithms**
66+
| Algorithm | Description | Use Case |
67+
|-----------|-------------|----------|
68+
| **Threshold** | Min/max boundary checking | CPU usage, memory limits |
69+
| **Trend** | Slope-based anomaly detection | Memory leaks, performance degradation |
70+
| **Statistical** | Advanced statistical analysis | Complex pattern detection |
71+
| **Custom** | User-defined detection functions | Domain-specific threats |
72+
73+
### **Metric Types**
74+
- `EMBEDIDS_METRIC_TYPE_PERCENTAGE` - CPU usage, memory utilization (0-100%)
75+
- `EMBEDIDS_METRIC_TYPE_FLOAT` - Sensor readings, network traffic
76+
- `EMBEDIDS_METRIC_TYPE_UINT32/64` - Packet counts, process counts
77+
- `EMBEDIDS_METRIC_TYPE_BOOL` - System states, security flags
78+
- `EMBEDIDS_METRIC_TYPE_ENUM` - Custom enumerated values
79+
80+
## Installation
81+
82+
### **CMake (Recommended)**
83+
```bash
84+
mkdir build && cd build
85+
cmake .. -DBUILD_EXAMPLES=ON -DBUILD_TESTS=ON
86+
make -j$(nproc)
87+
sudo make install
88+
```
89+
90+
### **Integration Options**
91+
```cmake
92+
# Option 1: Installed package
93+
find_package(EmbedIDS REQUIRED)
94+
target_link_libraries(your_app EmbedIDS::embedids)
95+
96+
# Option 2: FetchContent (Git repository)
97+
include(FetchContent)
98+
FetchContent_Declare(
99+
EmbedIDS
100+
GIT_REPOSITORY https://github.com/samiralavi/EmbedIDS.git
101+
GIT_BRANCH main # Fetch the main branch
102+
)
103+
FetchContent_MakeAvailable(EmbedIDS)
104+
target_link_libraries(your_app embedids)
105+
```
106+
107+
### Build Options
108+
109+
- `BUILD_TESTS=ON/OFF` - Unit tests with GoogleTest (default: ON)
110+
- `BUILD_EXAMPLES=ON/OFF` - Example applications (default: ON)
111+
- `ENABLE_COVERAGE=ON/OFF` - Code coverage reporting (default: OFF)
112+
113+
## Testing & Coverage
114+
115+
### Running Unit Tests
116+
117+
There are multiple ways to run the test suites
118+
119+
#### Method 1: Using CTest (Recommended)
120+
```bash
121+
# Build the project first
122+
mkdir build && cd build
123+
cmake .. -DBUILD_TESTS=ON
124+
make -j$(nproc)
125+
126+
# Run all tests
127+
ctest
128+
129+
# Run tests with detailed output
130+
ctest --verbose
131+
132+
# List available tests
133+
ctest --list-tests
134+
```
135+
136+
#### Method 2: Direct Test Execution
137+
```bash
138+
# After building, run tests directly
139+
./tests/embedids_tests
140+
141+
# Run specific test patterns (GoogleTest)
142+
./tests/embedids_tests --gtest_filter="*Threshold*"
143+
```
144+
145+
#### Method 3: Using make (if available)
146+
```bash
147+
make test # May not be available in all configurations
148+
```
149+
150+
### Code Coverage Analysis
151+
152+
Generate detailed coverage reports to see test effectiveness:
153+
154+
```bash
155+
# Configure with coverage enabled
156+
mkdir build && cd build
157+
cmake .. -DBUILD_TESTS=ON -DENABLE_COVERAGE=ON
158+
make -j$(nproc)
159+
160+
# Generate coverage report
161+
make coverage
162+
```
163+
164+
## License
165+
166+
Licensed under the Apache License, Version 2.0. See [LICENSE](LICENSE) file for details.

cmake/EmbedIDSConfig.cmake.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@PACKAGE_INIT@
2+
3+
include("${CMAKE_CURRENT_LIST_DIR}/EmbedIDSTargets.cmake")
4+
5+
check_required_components(EmbedIDS)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)