1 /*
2  * Copyright (c) 2022 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "test.h"
8 
9 #include <zephyr/logging/log.h>
10 #include <zephyr/pm/policy.h>
11 #include <zephyr/sys_clock.h>
12 
13 LOG_MODULE_REGISTER(dev_test, CONFIG_APP_LOG_LEVEL);
14 
15 struct dev_test_data {
16 	struct pm_policy_latency_subscription subs;
17 	struct pm_policy_latency_request req;
18 };
19 
on_latency_changed(int32_t latency)20 static void on_latency_changed(int32_t latency)
21 {
22 	if (latency == SYS_FOREVER_US) {
23 		LOG_INF("Latency constraint changed: none");
24 	} else {
25 		LOG_INF("Latency constraint changed: %" PRId32 "ms",
26 			latency / USEC_PER_MSEC);
27 	}
28 }
29 
dev_test_open(const struct device * dev)30 static void dev_test_open(const struct device *dev)
31 {
32 	struct dev_test_data *data = dev->data;
33 
34 	LOG_INF("Adding latency constraint: 20ms");
35 	pm_policy_latency_request_add(&data->req, 20000);
36 
37 	pm_policy_latency_changed_subscribe(&data->subs, on_latency_changed);
38 }
39 
dev_test_close(const struct device * dev)40 static void dev_test_close(const struct device *dev)
41 {
42 	struct dev_test_data *data = dev->data;
43 
44 	LOG_INF("Removing latency constraint");
45 	pm_policy_latency_request_remove(&data->req);
46 
47 	pm_policy_latency_changed_unsubscribe(&data->subs);
48 }
49 
50 static const struct test_api dev_test_api = {
51 	.open = dev_test_open,
52 	.close = dev_test_close,
53 };
54 
dev_test_init(const struct device * dev)55 int dev_test_init(const struct device *dev)
56 {
57 	return 0;
58 }
59 
60 static struct dev_test_data data;
61 
62 DEVICE_DEFINE(dev_test, "dev_test", &dev_test_init, NULL, &data, NULL,
63 	      POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &dev_test_api);
64