mailbox的client

萧业
2023-12-01
前面的文章说了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);
}


 类似资料: