1 /* Copyright 2021 Carlo Caione, <ccaione@baylibre.com>
2  * SPDX-License-Identifier: Apache-2.0
3  */
4 
5 #include <zephyr/device.h>
6 #include <zephyr/devicetree.h>
7 #include <zephyr/ztest.h>
8 #include <zephyr/ipc/ipc_service.h>
9 
received_cb(const void * data,size_t len,void * priv)10 static void received_cb(const void *data, size_t len, void *priv)
11 {
12 	uintptr_t expected = (uintptr_t) priv;
13 	uint8_t *msg = (uint8_t *) data;
14 
15 	zassert_equal(*msg, expected, "msg doesn't match the expected value");
16 
17 	printk("Received: %d, expected: %ld\n", *msg, expected);
18 }
19 
20 static struct ipc_ept_cfg ept_cfg = {
21 	.name = "test_ept",
22 	.cb = {
23 		.received = received_cb,
24 	},
25 };
26 
ZTEST(ipc_service,test_ipc_service)27 ZTEST(ipc_service, test_ipc_service)
28 {
29 	const struct device *dev_10;
30 	const struct device *dev_20;
31 	struct ipc_ept ept_10;
32 	struct ipc_ept ept_20;
33 	uint8_t msg;
34 	int ret;
35 
36 	dev_10 = DEVICE_DT_GET(DT_NODELABEL(ipc10));
37 	dev_20 = DEVICE_DT_GET(DT_NODELABEL(ipc20));
38 
39 	/*
40 	 * We send 10 through the ipc10 instance so we expect 20 in the
41 	 * receiving callback (10 + 10 == 20)
42 	 */
43 	msg = 10;
44 	printk("Sending %d\n", msg);
45 
46 	/* Save the expected result in priv */
47 	ept_cfg.priv = (void *) 20;
48 
49 	ret = ipc_service_register_endpoint(dev_10, &ept_10, &ept_cfg);
50 	zassert_ok(ret, "ipc_service_register_endpoint() failed");
51 
52 	ret = ipc_service_send(&ept_10, &msg, sizeof(msg));
53 	zassert_ok(ret, "ipc_service_send() failed");
54 
55 	/*
56 	 * We send 10 again this time through the ipc20 instance so we expect
57 	 * 20 in the receiving callback (10 + 20 == 30)
58 	 */
59 	msg = 10;
60 	printk("Sending %d\n", msg);
61 
62 	/* Save the expected result in priv */
63 	ept_cfg.priv = (void *) 30;
64 
65 	ret = ipc_service_register_endpoint(dev_20, &ept_20, &ept_cfg);
66 	zassert_ok(ret, "ipc_service_register_endpoint() failed");
67 
68 	ret = ipc_service_send(&ept_20, &msg, sizeof(msg));
69 	zassert_ok(ret, "ipc_service_send() failed");
70 
71 	/*
72 	 * Deregister the endpoint and ensure that we fail
73 	 * correctly
74 	 */
75 	ret = ipc_service_deregister_endpoint(&ept_10);
76 	zassert_ok(ret, "ipc_service_deregister_endpoint() failed");
77 
78 	ret = ipc_service_send(&ept_10, &msg, sizeof(msg));
79 	zassert_equal(ret, -ENOENT, "ipc_service_send() should return -ENOENT");
80 }
81 
82 ZTEST_SUITE(ipc_service, NULL, NULL, NULL, NULL, NULL);
83