mailbox的client

前面的文章说了mailbox的controller后,这一节我们看看mailbox的client 要如何写?
mailbox的client可以自己定义,下面这个是模板
struct demo_client {
	struct mbox_client cl;
	struct mbox_chan *mbox;
	struct completion c;
	bool async;
	/* ... */
};
client 可以采用按是否发送数据后是否block分成两种,按发送给controller的数据是否同步还是异步也可以分为两种.
static void client_demo(struct platform_device *pdev)
{
	struct demo_client *dc_sync, *dc_async;
	/* The controller already knows async_pkt and sync_pkt */
	struct async_pkt ap;
	struct sync_pkt sp;

	dc_sync = kzalloc(sizeof(*dc_sync), GFP_KERNEL);
	dc_async = kzalloc(sizeof(*dc_async), GFP_KERNEL);
//初始化non-blocking mode client
	/* Populate non-blocking mode client */
	dc_async->cl.dev = &pdev->dev;
//controller收到数据后会调用rx_callback
	dc_async->cl.rx_callback = message_from_remote;
//调用tx_done 发送数据
	dc_async->cl.tx_done = sample_sent;
	dc_async->cl.tx_block = false;
	dc_async->cl.tx_tout = 0; /* doesn't matter here */
	dc_async->cl.knows_txdone = false; /* depending upon protocol */
	dc_async->async = true;
	init_completion(&dc_async->c);

/初始化blocking mode client
	dc_sync->cl.dev = &pdev->dev;
	dc_sync->cl.rx_callback = message_from_remote;
	dc_sync->cl.tx_done = NULL; /* operate in blocking mode */
	dc_sync->cl.tx_block = true;
	dc_sync->cl.tx_tout = 500; /* by half a second */
	dc_sync->cl.knows_txdone = false; /* depending upon protocol */
	dc_sync->async = false;

//异步发送给controller数据
//向controller发送数据之前必须知名当前用的是controller的哪个channel
	dc_async->mbox = mbox_request_channel(&dc_async->cl, 1);
	/* Populate data packet */
	/* ap.xxx = 123; etc */
	/* Send async message to remote */
	mbox_send_message(dc_async->mbox, &ap);
	/* Now wait for async chan to be done */
	wait_for_completion(&dc_async->c);
//同步发送给controller数据
	dc_sync->mbox = mbox_request_channel(&dc_sync->cl, 0);
	/* Populate data packet */
	/* sp.abc = 123; etc */
	/* Send message to remote in blocking mode */
	mbox_send_message(dc_sync->mbox, &sp);
	/* At this point 'sp' has been sent */
}


*
 * This is the handler for data received from remote. The behaviour is purely
 * dependent upon the protocol. This is just an example.
 */
static void message_from_remote(struct mbox_client *cl, void *mssg)
{
	struct demo_client *dc = container_of(cl, struct demo_client, cl);
	if (dc->async) {
		if (is_an_ack(mssg)) {
			/* An ACK to our last sample sent */
			return; /* Or do something else here */
		} else { /* A new message from remote */
			queue_req(mssg);
		}
	} else {
		/* Remote f/w sends only ACK packets on this channel */
		return;
	}
}

static void sample_sent(struct mbox_client *cl, void *mssg, int r)
{
	struct demo_client *dc = container_of(cl, struct demo_client, cl);
	complete(&dc->c);
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值