summaryrefslogtreecommitdiff
path: root/src/modules/eeze
diff options
context:
space:
mode:
authorStefan Schmidt <s.schmidt@samsung.com>2013-04-16 17:35:26 +0100
committerStefan Schmidt <s.schmidt@samsung.com>2013-04-17 09:09:42 +0100
commitf58ffeb7be86bf9aea275d943c0b840a32c9df26 (patch)
tree8be19862b937470c9620ba87bcda0cddf7d6633f /src/modules/eeze
parent4497815cafa4ecfbca6d564913da8fe2bec95c2e (diff)
eeze/sensor: Add small udev module to read temperature from sysfs
This module does only provide a temperature sensor. Some pieces borrowed from the temget code.
Diffstat (limited to 'src/modules/eeze')
-rw-r--r--src/modules/eeze/sensor/udev/udev.c214
1 files changed, 214 insertions, 0 deletions
diff --git a/src/modules/eeze/sensor/udev/udev.c b/src/modules/eeze/sensor/udev/udev.c
new file mode 100644
index 0000000000..3a24fde4b4
--- /dev/null
+++ b/src/modules/eeze/sensor/udev/udev.c
@@ -0,0 +1,214 @@
1#ifdef HAVE_CONFIG_H
2# include "config.h"
3#endif
4
5#include <stdio.h>
6#include <sys/time.h>
7
8#include <Eina.h>
9#include <Ecore.h>
10#include <Eeze.h>
11#include <Eeze_Sensor.h>
12#include "eeze_sensor_private.h"
13
14static int _eeze_sensor_udev_log_dom = -1;
15
16#ifdef ERR
17#undef ERR
18#endif
19#define ERR(...) EINA_LOG_DOM_ERR(_eeze_sensor_udev_log_dom, __VA_ARGS__)
20
21Eeze_Sensor_Module *esensor_module;
22
23Eina_List *devices;
24
25Eina_Bool
26udev_init(void)
27{
28 /* We only offer a temperature sensor right now */
29 Eeze_Sensor_Obj *obj = calloc(1, sizeof(Eeze_Sensor_Obj));
30 obj->type = EEZE_SENSOR_TYPE_TEMPERATURE;
31 esensor_module->sensor_list = eina_list_append(esensor_module->sensor_list, obj);
32
33 devices = eeze_udev_find_by_type(EEZE_UDEV_TYPE_IS_IT_HOT_OR_IS_IT_COLD_SENSOR, NULL);
34
35 return EINA_TRUE;
36}
37
38Eina_Bool
39udev_shutdown(void)
40{
41 char *data;
42
43 EINA_LIST_FREE(devices, data);
44 free(data);
45
46 return EINA_TRUE;
47}
48
49double
50_udev_read(void)
51{
52 Eina_List *l;
53 double cur, temp;
54 char *syspath;
55 const char *test;
56 char buf[256];
57 int x, y, sensors = 0;
58 temp = -274.0; /* Absolute zero is -273.15° C */
59
60 if (eina_list_count(devices))
61 {
62 temp = 0.0;
63 EINA_LIST_FOREACH(devices, l, syspath)
64 {
65 for (x = 1, y = 0; x < 15; x++)
66 {
67 if (y >= 2) break;
68 sprintf(buf, "temp%d_input", x);
69 if ((test = eeze_udev_syspath_get_sysattr(syspath, buf)))
70 {
71 y = 0;
72 cur = strtod(test, NULL);
73 if (cur > -274.0)
74 {
75 /* temp is in celcius * 1000 */
76 temp += (cur / 1000);
77 sensors++;
78 }
79 }
80 /* keep checking for temp sensors until we get 2 in a row that don't exist */
81 else y++;
82 }
83 }
84 if (sensors)
85 temp /= (double)sensors;
86 }
87
88 return temp;
89}
90
91Eina_Bool
92udev_read(Eeze_Sensor_Type sensor_type, Eeze_Sensor_Obj *lobj)
93{
94 Eeze_Sensor_Obj *obj = NULL;
95
96 obj = eeze_sensor_obj_get(sensor_type);
97 if (obj == NULL)
98 {
99 ERR("No matching sensor object found in list");
100 return EINA_FALSE;
101 }
102
103 switch (sensor_type)
104 {
105 case EEZE_SENSOR_TYPE_TEMPERATURE:
106 obj->accuracy = -1;
107 obj->data[0] = _udev_read();
108 obj->data[1] = 0;
109 obj->data[2] = 0;
110 obj->timestamp = ecore_time_get();
111 break;
112
113 default:
114 ERR("Not possible to read from this sensor type.");
115 free(obj);
116 return EINA_FALSE;
117 }
118
119 memcpy(lobj, obj, sizeof(Eeze_Sensor_Obj));
120 free(obj);
121
122 return EINA_TRUE;
123}
124
125Eina_Bool
126udev_async_read(Eeze_Sensor_Type sensor_type, void *user_data EINA_UNUSED)
127{
128 Eeze_Sensor_Obj *obj = NULL;
129
130 obj = eeze_sensor_obj_get(sensor_type);
131 if (obj == NULL)
132 {
133 ERR("No matching sensor object found in list.");
134 return EINA_FALSE;
135 }
136
137 switch (sensor_type)
138 {
139 case EEZE_SENSOR_TYPE_TEMPERATURE:
140 obj->accuracy = -1;
141 obj->data[0] = _udev_read();
142 obj->data[1] = 0;
143 obj->data[2] = 0;
144 obj->timestamp = ecore_time_get();
145 ecore_event_add(EEZE_SENSOR_EVENT_TEMPERATURE, obj, NULL, NULL);
146 break;
147
148 default:
149 ERR("Not possible to read from this sensor type.");
150 free(obj);
151 return EINA_FALSE;
152 }
153 return EINA_TRUE;
154}
155
156/* This function gets called when the module is loaded from the disk. Its the
157 * entry point to anything in this module. After setting ourself up we register
158 * into the core of eeze sensor to make our functionality available.
159 */
160Eina_Bool
161sensor_udev_init(void)
162{
163
164 _eeze_sensor_udev_log_dom = eina_log_domain_register("eeze_sensor_udev", EINA_COLOR_BLUE);
165 if (_eeze_sensor_udev_log_dom < 0)
166 {
167 EINA_LOG_ERR("Could not register 'eeze_sensor_udev' log domain.");
168 return EINA_FALSE;
169 }
170
171 /* Check to avoid multi-init */
172 if (esensor_module) return EINA_FALSE;
173
174 /* Set module function pointer to allow calls into the module */
175 esensor_module = calloc(1, sizeof(Eeze_Sensor_Module));
176 if (!esensor_module) return EINA_FALSE;
177
178 /* Setup our function pointers to allow the core accessing this modules
179 * functions
180 */
181 esensor_module->init = udev_init;
182 esensor_module->shutdown = udev_shutdown;
183 esensor_module->read = udev_read;
184 esensor_module->async_read = udev_async_read;
185
186 if (!eeze_sensor_module_register("fake", esensor_module))
187 {
188 ERR("Failed to register udev module.");
189 return EINA_FALSE;
190 }
191 return EINA_TRUE;
192}
193
194/* Cleanup when the module gets unloaded. Unregister ourself from the core to
195 * avoid calls into a not loaded module.
196 */
197void
198sensor_udev_shutdown(void)
199{
200 Eeze_Sensor_Obj *sens;
201
202 eeze_sensor_module_unregister("udev");
203 EINA_LIST_FREE(esensor_module->sensor_list, sens)
204 free(sens);
205
206 free(esensor_module);
207 esensor_module = NULL;
208
209 eina_log_domain_unregister(_eeze_sensor_udev_log_dom);
210 _eeze_sensor_udev_log_dom = -1;
211}
212
213EINA_MODULE_INIT(sensor_udev_init);
214EINA_MODULE_SHUTDOWN(sensor_udev_shutdown);