Skip to content

Commit 3205239

Browse files
committed
initial files
0 parents  commit 3205239

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed

README.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Steps to run:
2+
1. gcc -shared -o libdummy_ectool.so -fPIC dummy_ectool.c
3+
2. python3 fw-fanctrl.py
4+
5+
# Explanation of Files
6+
## fw-fanctrl.py
7+
This is the Python main script that works with the dummy_ectool library. It does the following:
8+
1. Declares functions to read and write the fan speed.
9+
2. Declares a function to read the fan status.
10+
3. Contains an example usage block that shows how to call the functions.
11+
12+
## dummy_ectool.py
13+
This Python script is a wrapper of the libdummy_ectool.so shared library. It:
14+
1. Loads the shared library using the ctypes module.
15+
2. Specifies the C function signatures (i.e., argument types and return types).
16+
3. Offers Python functions that invoke the respective C functions.
17+
18+
## dummy_ectool.c
19+
This is a C source file that performs the fan control logic. It:
20+
1. Declares functions to obtain and set the fan speed.
21+
2. Declares a function to retrieve the fan status.
22+
3. Utilizes a global variable to emulate the fan speed.
23+
24+
# How the Program Works
25+
The dummy_ectool.c source file is compiled into a shared library (libdummy_ectool.so) with the gcc compiler. The flags:
26+
27+
-shared: Instructs the compiler to produce a shared library.
28+
-fPIC: Produces position-independent code, which is necessary for shared libraries.
29+
30+
The dummy_ectool.py script imports the shared library with ctypes.CDLL() and declares the function signatures for the C functions. It also declares argument types and return types.
31+
32+
The fw-fanctrl.py command loads the dummy_ectool module, through which the access to the C functions is allowed. The program then:
33+
34+
Displays the fan status and current speed.
35+
Sets the fan speed to 3000 RPM and displays the new fan status and speed.
36+
Tries to set the fan speed to 6000 RPM, causing an error due to the correct range being 0-5000 RPM. This checks for the error handling.
37+
38+
THIS IS ONLY A PROOF OF CONCEPT FOR GSOC AND LARGE SCALE IMPLEMENTATION WILL FOLLOW THROUGH.

dummy_ectool.c

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#ifdef __cplusplus
2+
extern "C" {
3+
#endif
4+
5+
#include <stdio.h>
6+
#include <stdlib.h>
7+
8+
// Global variable to simulate fan speed
9+
static int fan_speed = 2500;
10+
11+
// Function to get the current fan speed
12+
int get_fan_speed() {
13+
return fan_speed;
14+
}
15+
16+
// Function to set the fan speed (for simulation purposes)
17+
int set_fan_speed(int speed) {
18+
if (speed < 0 || speed > 5000) {
19+
fprintf(stderr, "Error: Invalid fan speed. Must be between 0 and 5000.\n");
20+
return -1; // Return error code
21+
}
22+
fan_speed = speed;
23+
printf("Fan speed set to: %d\n", fan_speed);
24+
return 0; // Success
25+
}
26+
27+
// Function to check the fan status (running or idle)
28+
const char* get_fan_status() {
29+
if (fan_speed == 0) {
30+
return "Idle";
31+
}
32+
return "Running";
33+
}
34+
35+
#ifdef __cplusplus
36+
}
37+
#endif

dummy_ectool.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import ctypes
2+
import os
3+
4+
# Explicitly set the absolute path if needed
5+
lib_path = os.path.abspath("./libdummy_ectool.so")
6+
lib = ctypes.CDLL(lib_path)
7+
8+
# Define the return types and argument types for the C functions
9+
lib.get_fan_speed.restype = ctypes.c_int
10+
lib.set_fan_speed.argtypes = [ctypes.c_int]
11+
lib.set_fan_speed.restype = ctypes.c_int
12+
lib.get_fan_status.restype = ctypes.c_char_p
13+
14+
def get_fan_speed():
15+
return lib.get_fan_speed()
16+
17+
def set_fan_speed(speed):
18+
result = lib.set_fan_speed(speed)
19+
if result == -1:
20+
raise ValueError("Invalid fan speed. Must be between 0 and 5000.")
21+
return result
22+
23+
def get_fan_status():
24+
return lib.get_fan_status().decode('utf-8')

fw-fanctrl.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import dummy_ectool
2+
3+
# Function to get the current fan speed
4+
def get_fan_speed():
5+
return dummy_ectool.get_fan_speed()
6+
7+
# Function to set the fan speed
8+
def set_fan_speed(speed):
9+
try:
10+
dummy_ectool.set_fan_speed(speed)
11+
print(f"Fan speed successfully set to {speed} RPM.")
12+
except ValueError as e:
13+
print("Error:", e)
14+
15+
# Function to get the fan status
16+
def get_fan_status():
17+
return dummy_ectool.get_fan_status()
18+
19+
# Example usage
20+
if __name__ == "__main__":
21+
# Display current status
22+
print("Current Fan Speed:", get_fan_speed())
23+
print("Fan Status:", get_fan_status())
24+
25+
# Set a new fan speed and check status
26+
set_fan_speed(3000)
27+
print("Updated Fan Speed:", get_fan_speed())
28+
print("Fan Status:", get_fan_status())
29+
30+
# Try setting an invalid speed
31+
set_fan_speed(6000) # This should trigger an error

0 commit comments

Comments
 (0)