📖
📖
📖
📖
Job Memo
Search…
Cheat sheets of git and repo
Implement your own HIDL
Communicate with Kernel driver
Compile your own kernel and install
Notes of GDBus
introduction of Yocto project
Yocto – how to initialize your module?
Useful Linux Commands
Benefits of using Macro
Samba to create sharing folders between host and VM
How to write a daemon?
C/C++
Use dlopen and dlsym to get handler of dynamic library(.so)
argc? argv ?
How to use C api in C++ ?
What is void* ?
Utilize function pointers to build Handlers
Concepts of Makefile
Makefile parameters
Powered By
GitBook
Utilize function pointers to build Handlers
有時候因為 Event 過多,不想寫太多 Switch case 或是 if … else,我們可以
想個辦法
(學習晶片廠的施工方法) 整理成 table 的形式,方便維護。
#define COMMAND_A 1
#define COMMAND_B 2
int demo_funcA(int, uint8_t); // handle function 要呼叫的
int demo_funcB(int, uint8_t);
typedef int (*cmd_handler)(int,uint8_t); //要呼叫的 function,及要傳入的參數
typedef struct cmd_handler_entry{
int command;
cmd_handler handler;
}cmd_handler_entry;
/*
重點在這邊可以做成 table 的形式,後續維護很方便,不過要注意傳入參數的型態
*/
static cmd_handler_entry cmd_handler_entries[] = {
{ COMMAND_A, demo_funcA},
{ COMMAND_B, demo_funcB},
{ NULL, NULL}
};
int main(){
//在這函式中,需要下 COMMAND_B,並執行它對應的函數
cmd_handler_entry* msg_handler;
msg_handler = cmd_handler_entries;
while(msg_handler->handler != NULL){
if(msg_handler->command == COMMAND_B){
break;
}else{
msg_handler++;
}
}
if(msg_handler->handler != NULL){
//需要下 COMMAND_B
msg_handler->command = COMMAND_B;
// handler 會自動指向它的函式:demo_funcB,並傳入參數(123, ‘0x123’)
msg_handler->handler(123, ‘0x123’);
return 1;
}else{
printf(“Handler ERROR!”);
return 0;
}
}
C/C++ - Previous
What is void* ?
Next - C/C++
Concepts of Makefile
Last modified
5mo ago
Copy link