summaryrefslogtreecommitdiff
path: root/src/libthermal/thermal.c
blob: 66dc0092fd21b80c122e9d5cd713bb418d2e7786 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <sys/epoll.h>

#include <netlink/netlink.h>
#include <netlink/genl/genl.h>
#include <netlink/genl/ctrl.h>

#include <libnl3/netlink/genl/genl.h>
#include <libnl3/netlink/genl/mngt.h>
#include <libnl3/netlink/genl/ctrl.h>

#include "thermal.h"

struct thermal_handler *thermal_init(struct thermal_ops *ops)
{
	struct thermal_handler *th;

	th = malloc(sizeof(*th));
	if (!th)
		return NULL;
	th->ops = ops;

	if (thermal_events_init(th))
		goto out_free;

	if (ops->tz_temp && thermal_sampling_init(th))
		goto out_free;
	
	return th;

out_free:
	free(th);

	return NULL;
}

/*************************TEST POINT*****************************/

static int tz_create(const char *name, int tz_id)
{
	return 0;
}

static int tz_delete(int tz_id)
{
	return 0;
}

static int tz_disable(int tz_id)
{
	printf("Thermal zone %d disabled\n", tz_id);

	return 0;
}

static int tz_enable(int tz_id)
{
	printf("Thermal zone %d enabled\n", tz_id);

	return 0;
}

static int tz_temp(int tz_id, int temp)
{
	printf("Thermal zone %d temperature: %d\n", tz_id, temp);
	return 0;
}

static int trip_high(int tz_id, int trip_id, int temp)
{
	printf("Trip point %d crossed way up with %d °C\n", trip_id, temp);
	return 0;
}

static int trip_low(int tz_id, int trip_id, int temp)
{
	printf("Trip point %d crossed way down with %d °C\n", trip_id, temp);
	return 0;
}

static int trip_add(int tz_id, int trip_id, int type, int temp, int hyst)
{
	printf("Trip point added %d: id=%d, type=%d, temp=%d, hyst=%d\n",
	       tz_id, trip_id, type, temp, hyst);

	return 0;
}

static int trip_delete(int tz_id, int trip_id)
{
	printf("Trip point deleted %d: id=%d\n", tz_id, trip_id);

	return 0;
}

static int trip_change(int tz_id, int trip_id, int type, int temp, int hyst)
{
	printf("Trip point changed %d: id=%d, type=%d, temp=%d, hyst=%d\n",
	       tz_id, trip_id, type, temp, hyst);

	return 0;
}

static int cdev_add(const char *name, int cdev_id, int max_state)
{
	return 0;
}

static int cdev_delete(int cdev_id)
{
	return 0;
}

static int cdev_update(int cdev_id, int cur_state)
{
	printf("cdev:%d state:%d\n", cdev_id, cur_state);

	return 0;
}

static int gov_change(int tz_id, const char *name)
{
	printf("tz %d, governor=%s\n", tz_id, name);

	return 0;
}

static struct thermal_ops ops = {
	.tz_create	= tz_create,
	.tz_delete	= tz_delete,
	.tz_disable	= tz_disable,
	.tz_enable	= tz_enable,
	.tz_temp	= tz_temp,
	.trip_high	= trip_high,
	.trip_low	= trip_low,
	.trip_add	= trip_add,
	.trip_delete	= trip_delete,
	.trip_change	= trip_change,
	.cdev_add	= cdev_add,
	.cdev_delete	= cdev_delete,
	.cdev_update	= cdev_update,
	.gov_change	= gov_change
};

#define MAX_EVENTS 10

int main(void)
{
	struct thermal_handler *th;
	struct epoll_event ev;
	struct epoll_event events[MAX_EVENTS];	
	int epollfd;
	int nfds;
	int i;

	th = thermal_init(&ops);
	if (!th)
		return -1;

	epollfd = epoll_create1(0);
	if (epollfd < 0)
		return -1;

	ev.events = EPOLLIN;
	ev.data.ptr = thermal_events_handle;

	if (epoll_ctl(epollfd, EPOLL_CTL_ADD, thermal_events_fd(th), &ev) == -1)
		return -1;

	ev.events = EPOLLIN;
	ev.data.ptr = thermal_sampling_handle;
	
	if (epoll_ctl(epollfd, EPOLL_CTL_ADD, thermal_sampling_fd(th), &ev) == -1)
		return -1;
	
	while (1) {

		nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1);
		for (i = 0; i < nfds; i++) {
			if (events[i].data.ptr == thermal_events_handle) {
				thermal_events_handle(th);
			} else if (events[i].data.ptr == thermal_sampling_handle) {
				thermal_sampling_handle(th);
			}
		}
	}

	return 0;
}