https://blog.csdn.net/hellozex/article/details/81742348
1, Basics
The so-called correction, that is, through a function module A module B b () complete certain functions, but the function b () are unable to achieve full functionality requires far as to call a function in module A of a () to complete this a () is the callback function. As shown below
① agreed interface specification. Interface specification must be agreed in module B, is defined callback functions a () function prototype
Define the callback function prototype where best to follow typedef void (* SCT_XXX) (LPVOID lp, const CBParamStruct & cbNode); SCT_XXX a pointer type to a function (the original, said callback function name), LP callback context, CBParamStruct callback parameter, Usually the callbacks due to more than one parameter, so the definition of a structure convenient.
② registered callback function. In order for the callback function module B know they were going to be used, there must be a function or statement to register a callback function
The definition of registered callback function follows the void RCF_XXX (SCT_XXX pfn, LPVOID lp); RCF_XXX is a registered name of the function, pfn is SCT_XXX type of pointer variable that points to the callback function (text said pfn is a callback function name (pointer)), lp is a callback context. A module generally complete after initialization module B calls the callback function address assignment defined in the module A to pfn, lp assignment is this.
③ do things in module A:
First, the callback function declared as static, static void CF_XXX (LPVOID lp, const CBParamStruct & cbNode); function of the parameters must be consistent with the callback parameter module B function prototype.
/*--------------------------------------------------------------------------------------------------------------------------------------------------------*/
2、回调函数的简单实现
例:代码就简单写了:
void basketball()//函数1
{
printf("选择篮球");
}
void football()//函数2
{
printf("选择足球");
}
void selectball(void (* ball)())//函数3
{
printf("选择什么球?");
ball();
}
int main(void)
{
selectball(basketball);
selectball(football);
return 0;
函数1和函数2就是回调函数,都是属于上文的模块A,而函数3属于上文的模块B,主函数在调用模块B里面的函数时,模块B得不到答案,故将1函数和2函数的函数名传给3函数的形参(函数指针),得到想要的答案。
打个比方,一个芯片厂家为了方便用户开发,为芯片写了一个函数库,这个函数库就是一个注册回调函数(如函数3),为什么厂家不直接把函数一套流程全给写出来呢?用户那么多,厂家不可能为每个用户编写出适合其的一整套函数,故就写一个注册回调函数,用户只需要把自己的处理函数(回调函数)名,传给函数指针就行。
而我们就是编写一些回调函数(如函数1,2),来供厂家的注册回调函数调用。这样我们就只需在我们自己编的回调函数中编写一些处理代码,从而得到我们想要的处理结果。
3、回调函数什么时候执行?
当该回调函数关心的那个事件或者中断触发的时候,回调函数将被执行。下面看具体例子
战舰STM32F103中的LWIP RAW_TCP客户端例程截取:
tcppcb=tcp_new(); //创建一个新的pcb
if(tcppcb) //创建成功
{
IP4_ADDR(&rmtipaddr,lwipdev.remoteip[0],lwipdev.remoteip[1],lwipdev.remoteip[2],lwipdev.remoteip[3]);
tcp_connect(tcppcb,&rmtipaddr,TCP_CLIENT_PORT,tcp_client_connected); //连接到目的地址的指定端口上,当连接成功后回调tcp_client_connected()函数
}else res=1;
这段代码是创建一个TCP控制块,如果创建成功(回调函数关心的事件),则回调tcp_client_connected()函数。来做一些连接成功后的处理。
先看红色的函数:
/**
* Connects to another host. The function given as the "connected"
* argument will be called when the connection has been established.
*
* @param pcb the tcp_pcb used to establish the connection
* @param ipaddr the remote ip address to connect to
* @param port the remote tcp port to connect to
* @param connected callback function to call when connected (or on error)
* @return ERR_VAL if invalid arguments are given
* ERR_OK if connect request has been sent
* other err_t values if connect request couldn't be sent
*/
err_t
tcp_connect(struct tcp_pcb *pcb, ip_addr_t *ipaddr, u16_t port,
tcp_connected_fn connected)
//tcp_connected_fn是指向函数的指针类型,而connected是该类型的指针变量,指向一个输入参数为void,err_t类型,返回值为err_t类型的函数。原型是
//typedef err_t (*tcp_connected_fn)(void *arg, struct tcp_pcb *tpcb, err_t err);
{
err_t ret;
u32_t iss;
u16_t old_local_port;
.........
这是LWIP内核的函数,是一个注册回调函数。
再看蓝色函数:
//lwIP TCP连接建立后调用回调函数
err_t tcp_client_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
{
struct tcp_client_struct *es=NULL;
if(err==ERR_OK)
{
es=(struct tcp_client_struct*)mem_malloc(sizeof(struct tcp_client_struct)); //申请内存
if(es) //内存申请成功
{
es->state=ES_TCPCLIENT_CONNECTED;//状态为连接成功
es->pcb=tpcb;
es->p=NULL;
tcp_arg(tpcb,es); //使用es更新tpcb的callback_arg
tcp_recv(tpcb,tcp_client_recv); //初始化LwIP的tcp_recv回调功能
tcp_err(tpcb,tcp_client_error); //初始化tcp_err()回调函数
tcp_sent(tpcb,tcp_client_sent); //初始化LwIP的tcp_sent回调功能
tcp_poll(tpcb,tcp_client_poll,1); //初始化LwIP的tcp_poll回调功能
tcp_client_flag|=1<<5; //标记连接到服务器了
err=ERR_OK
这是正点原子官方写的函数,是回调函数,在这个函数中做了一些初始化和标记处理。
4、回调函数起什么作用?
把要处理执行的这个任务写成一个函数,将这个函数和某一事件或者中断建立关联。
以上是我对回调函数浅易的理解,不知有没有讲清楚,有不对的地方还请各位看官拍砖!