Follow+Star Public Number, don’t miss the wonderful content
Author | strongerHuang
WeChat Public Account | strongerHuang
Most operating systems on the market have a type of API function interface: HOOK functions (also known as hook functions).
HOOK functions can be found in desktop operating systems like Windows, Linux, as well as real-time operating systems like RT-Thread, µC/OS, and FreeRTOS.
Many netizens may have such questions: What is a HOOK function? What is its purpose?
Let’s talk about what a HOOK function is, using µC/OS as an example.
A HOOK function, also known as a hook function, is a function that is called internally by the operating system.
If HOOK is enabled, the operating system will call the corresponding Hook function (your written hook function) when a specific event occurs.
For example: In RTOS, when a Task is deleted, the corresponding App_TaskIdleHook function will be called. (This “App_TaskIdleHook” function needs to be written by you)
Most online discussions about HOOK content take Windows as an example, describing the role and examples of hook functions, such as Baidu Encyclopedia:
Hook functions are part of the Windows message processing mechanism. By setting a “hook”, applications can filter all messages and events at the system level, accessing messages that are normally inaccessible. The essence of a hook is a piece of code used to process system messages, which is hooked into the system via system calls.
Why is it called a “hook”?
There are many explanations online, for example: it is “hooked” by the operating system. Interested readers can look it up online.
Uses and Methods of HOOK Functions
Familiar real-time operating systems like µC/OS and FreeRTOS all have Hook functions, which can be enabled in the config configuration file.
Let’s discuss the uses and methods of HOOK functions in conjunction with µC/OS:
Many RTOS have one (or more) configuration files to configure (trim) the system.
For example, in µC/OS, there is a system configuration file called os_cfg.h. It is essentially a series of enable switches, enabling necessary functions and disabling unnecessary ones to achieve system trimming.
/* ---------------------- MISCELLANEOUS ----------------------- */#define OS_APP_HOOKS_EN 1 /* Application-defined hooks are called from the uC/OS-II hooks */#define OS_ARG_CHK_EN 0 /* Enable (1) or Disable (0) argument checking */#define OS_CPU_HOOKS_EN 1 /* uC/OS-II hooks are found in the processor port files */#define OS_DEBUG_EN 1 /* Enable(1) debug variables */
Similarly, HOOK functions are also enabled through the cfg configuration file, for example:
#define OS_APP_HOOKS_EN 1#define OS_CPU_HOOKS_EN 1
Likewise, FreeRTOS has similar configurations:
We enable the HOOK we need to use.
TaskIdleHook, or Task Idle Hook Function, is called when the task is idle.
This hook function is present in many operating systems and is called when all other tasks are suspended.
For example, the CPU utilization we talk about is statistically calculated during the idle task, taking µC/OS as an example:
void OS_TaskIdle (void *p_arg){#if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */ OS_CPU_SR cpu_sr = 0u;#endif p_arg = p_arg; /* Prevent compiler warning for not using 'p_arg' */ for (;;) { OS_ENTER_CRITICAL(); OSIdleCtr++; OS_EXIT_CRITICAL(); OSTaskIdleHook(); /* Call user definable HOOK */ }}
System Idle Hook Function OSTaskIdleHook:
#if OS_CPU_HOOKS_EN > 0uvoid OSTaskIdleHook (void){#if OS_APP_HOOKS_EN > 0u App_TaskIdleHook();#endif}#endif
Application Idle Hook Function App_TaskIdleHook:
If enabled, this function will be executed, and we need to implement it ourselves. For example, if the idle count exceeds 50, we print a message:
void App_TaskIdleHook (void){ UserIdleCtr++; // Count idle occurrences if(50 < UserIdleCtr) { UserIdleCtr = 0; printf("SYS_IDLE"); }}
At this point, do you understand the principle of HOOK functions? Analyzing it step by step from the system is actually quite simple.
Because the operating system is mostly idle, IDLE idle functions are executed quite frequently, which many people may not understand.
The HOOK function, as mentioned above, is called and executed when a specific event occurs, while IDLE idle tasks are executed more frequently, events likedeleting a Task occur less often, so such HOOK functions are generated less frequently.
Alright, that’s all for now. I hope this helps you understand HOOK functions!
● Column “Embedded Tools”
● Column “Embedded Development”
● Column “Keil Tutorials”
● Selected Tutorials from the Embedded Column
Follow the public account reply “Join Group” to join the technical exchange group according to the rules, reply “1024” to see more content.
Click “Read Original” to see more shares.