05afbcccb3003528cbe2dbb2e7c97ddf7d068a13
[platform/core/system/sensord.git] / include / sensor_internal.h
1 /*
2  * sensord
3  *
4  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #ifndef __SENSORD_INTERNAL_H__
21 #define __SENSORD_INTERNAL_H__
22
23 #ifndef API
24 #define API __attribute__((visibility("default")))
25 #endif
26
27 #include <stdbool.h>
28 #include <sys/types.h>
29 #include <limits.h>
30
31 /*header for common sensor type*/
32 #include <sensor_types.h>
33 #include <sensor_deprecated.h>
34 #include <sensor_internal_deprecated.h>
35
36 const unsigned int SENSOR_BATCH_LATENCY_DEFAULT = UINT_MAX;
37
38 #ifdef __cplusplus
39 extern "C"
40 {
41 #endif
42
43 typedef void (*sensor_cb_t)(sensor_t sensor, unsigned int event_type, sensor_data_t *data, void *user_data);
44 typedef void (*sensor_events_cb_t)(sensor_t sensor, unsigned int event_type, sensor_data_t events[], int events_count, void *user_data);
45 typedef void (*sensorhub_cb_t)(sensor_t sensor, unsigned int event_type, sensorhub_data_t *data, void *user_data);
46 typedef void (*sensor_accuracy_changed_cb_t) (sensor_t sensor, unsigned long long timestamp, int accuracy, void *user_data);
47 typedef void (*sensor_attribute_int_changed_cb_t)(sensor_t sensor, int attribute, int value, void *user_data);
48 typedef void (*sensor_attribute_str_changed_cb_t)(sensor_t sensor, int attribute, const char *value, int len, void *user_data);
49
50 /**
51  * @brief Get the list of available sensors of a certain type,  use ALL_SENSOR to get all the sensors.
52  *
53  * @param[in] type the type of sensors requested.
54  * @param[out] list the list of sensors matching the asked type,  the caller should explicitly free this list.
55  * @param[out] sensor count the count of sensors contained in the list.
56  * @return true on success, otherwise false.
57  */
58 bool sensord_get_sensor_list(sensor_type_t type, sensor_t **list, int *sensor_count);
59
60 /**
61  * @brief Get the default sensor for a given type.
62  *
63  * @param[in] type the type of a sensor requested.
64  * @return the default sensor matching the asked type on success, otherwise NULL.
65  */
66 sensor_t sensord_get_sensor(sensor_type_t type);
67
68 /**
69  * @brief Get the list of available sensors of a certain type,  use ALL_SENSOR to get all the sensors.
70  *
71  * @param[in] type the type of sensors requested.
72  * @param[out] list the list of sensors matching the asked type,  the caller should explicitly free this list.
73  * @param[out] sensor count the count of sensors contained in the list.
74  * @return 0 on success, otherwise a negative error value
75  * @retval 0 Successful
76  * @retval -EPERM Operation not permitted
77  * @retval -EACCES Permission denied
78  * @retval -ENODATA NO sensor available
79  */
80 int sensord_get_sensors(sensor_type_t type, sensor_t **list, int *sensor_count);
81
82 /**
83  * @brief Get the default sensor for a given type.
84  *
85  * @param[in] type the type of a sensor requested.
86  * @param[out] a sensor matching the asked type.
87  * @return 0 on success, otherwise a negative error value
88  * @retval 0 Successful
89  * @retval -EPERM Operation not permitted
90  * @retval -EACCES Permission denied
91  */
92 int sensord_get_default_sensor(sensor_type_t type, sensor_t *sensor);
93
94 /**
95  * @brief Get the type of this sensor.
96  *
97  * @param[in] sensor a sensor to get type.
98  * @param[out] type the type of this sensor.
99  * @return return true on success, otherwise false.
100  */
101 bool sensord_get_type(sensor_t sensor, sensor_type_t *type);
102
103 /**
104  * @brief Get the URI string of this sensor.
105  *
106  * @param[in] sensor a sensor to get uri.
107  * @return the name string of this sensor on success, otherwise NULL.
108  */
109 const char* sensord_get_uri(sensor_t sensor);
110
111 /**
112  * @brief Get the name string of this sensor.
113  *
114  * @param[in] sensor a sensor to get name.
115  * @return the name string of this sensor on success, otherwise NULL.
116  */
117 const char* sensord_get_name(sensor_t sensor);
118
119 /**
120  * @brief Get the vendor string of this sensor.
121  *
122  * @param[in] sensor a sensor to get vendor.
123  * @return the vendor string of this sensor on success, otherwise NULL.
124  */
125 const char* sensord_get_vendor(sensor_t sensor);
126
127 /**
128  * @brief Get the privilege of this sensor.
129  *
130  * @param[in] sensor a sensor to get privilege.
131  * @param[out] privilege the privilege of this sensor.
132  * @return true on success, otherwise false.
133  */
134 bool sensord_get_privilege(sensor_t sensor, sensor_privilege_t *privilege);
135
136 /**
137  * @brief Get the minimum range of this sensor in the sensor's unit.
138  *
139  * @param[in] sensor a sensor to get minimum range.
140  * @param[out] min_range the minimum range of this sensor in the sensor's unit.
141  * @return true on success, otherwise false.
142  */
143 bool sensord_get_min_range(sensor_t sensor, float *min_range);
144
145 /**
146  * @brief Get the maximum range of this sensor in the sensor's unit.
147  *
148  * @param[in] sensor a sensor to get maximum range.
149  * @param[out] max_range the maximum range of this sensor in the sensor's unit.
150  * @return true on success, otherwise false.
151  */
152 bool sensord_get_max_range(sensor_t sensor, float *max_range);
153
154 /**
155  * @brief Get the resolution of this sensor in the sensor's unit.
156  *
157  * @param[in] sensor a sensor to get resolution.
158  * @param[out] resolution the resolution of this sensor in the sensor's unit.
159  * @return true on success, otherwise false.
160  */
161 bool sensord_get_resolution(sensor_t sensor, float *resolution);
162
163 /**
164  * @brief Get the minimum interval allowed between two events in microsecond or zero if this sensor only returns a value when the data it's measuring changes.
165  *
166  * @param[in] sensor a sensor to get minimum interval.
167  * @param[out] min_interval the minimum interval of this sensor.
168  * @return true on success, otherwise false.
169  */
170 bool sensord_get_min_interval(sensor_t sensor, int *min_interval);
171
172 /**
173  * @brief Get the number of events reserved for this sensor in the batch mode FIFO.
174  *
175  * @param[in] sensor a sensor to get the number of fifo count
176  * @param[out] fifo_count the number of events reserved for this sensor in the batch mode FIFO
177  * @return true on success, otherwise false
178  */
179 bool sensord_get_fifo_count(sensor_t sensor, int *fifo_count);
180
181 /**
182  * @brief Get the maximum number of events of this sensor that could be batched. If this value is zero it indicates that batch mode is not supported for this sensor.
183  *
184  * @param[in] sensor a sensor to the maximum number of events that could be batched.
185  * @param[out] max_batch_count the maximum number of events of this sensor that could be batched.
186  * @return true on success, otherwise false.
187  */
188 bool sensord_get_max_batch_count(sensor_t sensor, int *max_batch_count);
189
190 /**
191  * @brief Get the supported event types of this sensor.
192  *
193  * @param[in] sensor a sensor to get the supported event types.
194  * @param[out] event_types the array containing supported event types of this sensor, the caller should explicitly free this array.
195  * @param[out] count the count of the supported event types of this sensor.
196  * @return true on success, otherwise false.
197  */
198 bool sensord_get_supported_event_types(sensor_t sensor, unsigned int **event_types, int *count);
199
200 /**
201  * @brief Check a given event type is supporeted by this sensor.
202  *
203  * @param[in] sensor a sensor to check a given event type is supporeted.
204  * @param[out] event_type an event type to be checked whether supported or not.
205  * @param[out] supported whether a given event is supported or not in this sensor.
206  * @return true on success, otherwise false.
207  */
208 bool sensord_is_supported_event_type(sensor_t sensor, unsigned int event_type, bool *supported);
209
210 /**
211  * @brief Check a wakeup supported or not by this sensor.
212  *
213  * @param[in] sensor a sensor to check a given event type is supporeted.
214  * @return true on success, otherwise false.
215  */
216 bool sensord_is_wakeup_supported(sensor_t sensor);
217
218 /**
219  * @brief Connect a given sensor and get a handle of a given sensor.
220  *
221  * @param[in] sensor a sensor to connect
222  * @return a handle of a given sensor on success, otherwise negative value
223  */
224 int sensord_connect(sensor_t sensor);
225
226 /**
227  * @brief Disconnect a given sensor.
228  *
229  * @param[in] handle a handle to disconnect.
230  * @return true on success, otherwise false.
231  */
232 bool sensord_disconnect(int handle);
233
234 /**
235  * @brief Register a callback with a connected sensor for a given event_type. This callback will be called when a given event occurs in a connected sensor.
236  *
237  * @param[in] handle a handle represensting a connected sensor.
238  * @param[in] event_type an event type  to register
239  * @param[in] interval The desired interval between two consecutive events in microseconds. This is only a hint to the system so events may be received faster or slower than the specified interval.
240  *                                   It can be one of SENSOR_INTERVAL_NORMAL,  SENSOR_INTERVAL_FASTEST or the interval in microseconds.
241  * @param[in] max_batch_latency An event in the batch can be delayed by at most max_batch_latency microseconds. If this is set to zero, batch mode is disabled.
242  * @param[in] cb a callback which is called when a given event occurs
243  * @param[in] user_data the callback is called with user_data
244  * @return true on success, otherwise false.
245  */
246 bool sensord_register_event(int handle, unsigned int event_type, unsigned int interval, unsigned int max_batch_latency, sensor_cb_t cb, void *user_data);
247
248 /**
249  * @brief Register a callback with a connected sensor for a given event_type. This callback will be called when a given event occurs in a connected sensor.
250  *
251  * @param[in] handle a handle represensting a connected sensor.
252  * @param[in] event_type an event type  to register
253  * @param[in] max_batch_latency An event in the batch can be delayed by at most max_batch_latency microseconds. If this is set to zero, batch mode is disabled.
254  * @param[in] cb a callback which is called when a given event occurs
255  * @param[in] user_data the callback is called with user_data
256  * @return true on success, otherwise false.
257  */
258 bool sensord_register_events(int handle, unsigned int event_type, unsigned int max_batch_latency, sensor_events_cb_t cb, void *user_data);
259
260 /**
261  * @brief Register a callback with a connected context sensor for a given event_type. This callback will be called when a given event occurs in a connected context sensor.
262  *
263  * @param[in] handle a handle represensting a connected context sensor.
264  * @param[in] event_type an event type to register
265  * @param[in] interval The desired interval between two consecutive events in microseconds. This is only a hint to the system so events may be received faster or slower than the specified interval.
266  *                                    It can be one of SENSOR_INTERVAL_NORMAL,  SENSOR_INTERVAL_FASTEST or the interval in microseconds.
267  * @param[in] max_batch_latency An event in the batch can be delayed by at most max_batch_latency microseconds. If this is set to zero, batch mode is disabled.
268  * @param[in] cb a callback which is called when a given event occurs
269  * @param[in] user_data the callback is called with user_data
270  * @return true on success, otherwise false.
271  */
272 bool sensord_register_hub_event(int handle, unsigned int event_type, unsigned int interval, unsigned int max_batch_latency, sensorhub_cb_t cb, void *user_data);
273
274 /**
275  * @brief Unregister a event with a connected sensor.  After unregistering, that event will not be sent.
276  *
277  * @param[in] handle a handle represensting a connected sensor.
278  * @param[in] event_type an event type to unregister.
279  * @return true on success, otherwise false.
280  */
281 bool sensord_unregister_event(int handle, unsigned int event_type);
282
283 /**
284  * @brief Unregister a event with a connected sensor.  After unregistering, that event will not be sent.
285  *
286  * @param[in] handle a handle represensting a connected sensor.
287  * @param[in] event_type an event type to unregister.
288  * @return true on success, otherwise false.
289  */
290 bool sensord_unregister_events(int handle, unsigned int event_type);
291
292 /**
293  * @brief Register a callback with a connected sensor. This callback will be called when the accuracy of a sensor has changed.
294  *
295  * @param[in] handle a handle represensting a connected sensor.
296  * @param[in] cb a callback which is called when he accuracy of a sensor has changed.
297  * @param[in] user_data the callback is called with user_data
298  * @return true on success, otherwise false.
299  */
300 bool sensord_register_accuracy_cb(int handle, sensor_accuracy_changed_cb_t cb, void *user_data);
301
302 /**
303  * @brief Unregister a callback with a connected sensor.  After unregistering,  sensor_accuray_change_cb will not be called.
304  *
305  * @param[in] handle a handle represensting a connected sensor.
306  * @return true on success, otherwise false.
307  */
308 bool sensord_unregister_accuracy_cb(int handle);
309
310 /**
311  * @brief Register a callback with a connected sensor. This callback will be called when attributes of a sensor has changed.
312  *
313  * @param[in] handle a handle represensting a connected sensor.
314  * @param[in] cb a callback which is called when he attributes of a sensor has changed.
315  * @param[in] user_data the callback is called with user_data
316  * @return true on success, otherwise false.
317  */
318 bool sensord_register_attribute_int_changed_cb(int handle, sensor_attribute_int_changed_cb_t cb, void *user_data);
319
320 /**
321  * @brief Unregister a callback with a connected sensor. After unregistering, sensor_attribute_int_changed_cb will not be called.
322  *
323  * @param[in] handle a handle represensting a connected sensor.
324  * @return true on success, otherwise false.
325  */
326 bool sensord_unregister_attribute_int_changed_cb(int handle);
327
328 /**
329  * @brief Register a callback with a connected sensor. This callback will be called when attributes of a sensor has changed.
330  *
331  * @param[in] handle a handle represensting a connected sensor.
332  * @param[in] cb a callback which is called when he attributes of a sensor has changed.
333  * @param[in] user_data the callback is called with user_data
334  * @return true on success, otherwise false.
335  */
336 bool sensord_register_attribute_str_changed_cb(int handle, sensor_attribute_str_changed_cb_t cb, void *user_data);
337
338 /**
339  * @brief Unregister a callback with a connected sensor. After unregistering, sensor_attribute_str_changed_cb will not be called.
340  *
341  * @param[in] handle a handle represensting a connected sensor.
342  * @return true on success, otherwise false.
343  */
344 bool sensord_unregister_attribute_str_changed_cb(int handle);
345
346 /**
347  * @brief Start listening events with a connected sensor.
348  *
349  * @param[in] handle a handle represensting a connected sensor.
350  * @param[in] option either one of SENSOR_OPTION_DEFAULT and  SENSOR_OPTION_ALWAYS_ON.
351  *                                 with SENSOR_OPTION_DEFAULT, it stops to listening events when LCD is off or in power save mode.
352  *                                 with SENSOR_OPTION_ALWAYS_ON, it continues to listening events even when LCD is off or in power save mode.
353  * @return true on success, otherwise false.
354  */
355 bool sensord_start(int handle, int option);
356
357 /**
358  * @brief Stop listening events with a connected sensor.
359  *
360  * @param[in] handle a handle represensting a connected sensor.
361  * @return true on success, otherwise false.
362  */
363 bool sensord_stop(int handle);
364
365 /**
366  * @brief Change the interval of a specifed event type in a connected sensor.
367  *
368  * @param[in] handle a handle represensting a connected sensor.
369  * @param[in] event_type an event type to change interval.
370  * @param[in] interval The desired interval between two consecutive events in microseconds. This is only a hint to the system so events may be received faster or slower than the specified interval.
371  *                                    It can be one of SENSOR_INTERVAL_NORMAL,  SENSOR_INTERVAL_FASTEST or the interval in microseconds.
372  * @return true on success, otherwise false.
373  */
374 bool sensord_change_event_interval(int handle, unsigned int event_type, unsigned int interval);
375
376 /**
377  * @brief Change the max batch latency of a specifed event type in a connected sensor.
378  *
379  * @param[in] handle a handle represensting a connected sensor.
380  * @param[in] event_type an event type to change max batch latency
381  * @param[in] max_batch_latency an event in the batch can be delayed by at most max_batch_latency microseconds. If this is set to zero, batch mode is disabled.
382  * @return true on success, otherwise false.
383  */
384 bool sensord_change_event_max_batch_latency(int handle, unsigned int event_type, unsigned int max_batch_latency);
385
386 /**
387  * @brief Change the option of a connected sensor.
388  *
389  * @param[in] handle a handle represensting a connected sensor.
390  * @param[in] option either one of SENSOR_OPTION_DEFAULT and  SENSOR_OPTION_ALWAYS_ON.
391  *                                 with SENSOR_OPTION_DEFAULT, it stops to listening events when LCD is off or in power save mode.
392  *                                 with SENSOR_OPTION_ALWAYS_ON, it continues to listening events even when LCD is off or in power save mode.
393  * @return true on success, otherwise false.
394  */
395 bool sensord_set_option(int handle, int option);
396
397 /*
398  * @brief Set the attribute to a connected sensor
399  *
400  * @param[in] handle a handle represensting a connected sensor.
401  * @param[in] attribute an attribute to change
402  * @param[in] value an attribute value
403  * @return 0 on success, otherwise a negative error value
404  * @retval 0 Successful
405  * @retval -EINVAL Invalid parameter
406  * @retval -EPERM Operation not permitted
407  */
408 int sensord_set_attribute_int(int handle, int attribute, int value);
409
410 /*
411  * @brief Get the attribute to a connected sensor
412  *
413  * @param[in] handle a handle represensting a connected sensor.
414  * @param[in] attribute an attribute to get value
415  * @param[out] value an attribute value
416  * @return 0 on success, otherwise a negative error value
417  * @retval 0 Successful
418  * @retval -EINVAL Invalid parameter
419  * @retval -EPERM Operation not permitted
420  */
421 int sensord_get_attribute_int(int handle, int attribute, int* value);
422
423 /**
424  * @brief Set the attribute to a connected sensor
425  *
426  * @param[in] handle a handle represensting a connected sensor.
427  * @param[in] attribute an attribute to change
428  * @param[in] value an attribute value
429  * @param[in] value_len the length of value
430  * @return 0 on success, otherwise a negative error value
431  * @retval 0 Successful
432  * @retval -EINVAL Invalid parameter
433  * @retval -EPERM Operation not permitted
434  */
435 int sensord_set_attribute_str(int handle, int attribute, const char *value, int len);
436
437 /**
438  * @brief Get the attribute to a connected sensor
439  *
440  * @param[in] handle a handle represensting a connected sensor.
441  * @param[in] attribute an attribute to get value
442  * @param[out] value an attribute value, the caller should explicitly free this value
443  * @param[out] len the length of value
444  * @return 0 on success, otherwise a negative error value
445  * @retval 0 Successful
446  * @retval -EINVAL Invalid parameter
447  * @retval -EPERM Operation not permitted
448  */
449 int sensord_get_attribute_str(int handle, int attribute, char **value, int *len);
450
451 /**
452  * @brief Send data to sensorhub
453  *
454  * @param[in] handle a handle represensting a connected context sensor.
455  * @param[in] data it holds data to send to sensorhub
456  * @param[in] data_len the length of data
457  * @return true on success, otherwise false.
458  */
459 bool sensord_send_sensorhub_data(int handle, const char *data, int data_len);
460 bool sensord_send_command(int handle, const char *command, int command_len);
461
462 /**
463  * @brief get sensor data from a connected sensor
464  *
465  * @param[in] handle a handle represensting a connected context sensor.
466  * @param[in] data_id it specifies data to get
467  * @param[out] sensor_data data from connected sensor
468  * @return true on success, otherwise false.
469  */
470 bool sensord_get_data(int handle, unsigned int data_id, sensor_data_t* sensor_data);
471
472 /**
473  * @brief get sensor data from a connected sensor
474  *
475  * @param[in] handle a handle represensting a connected context sensor.
476  * @param[in] data_id it specifies data to get
477  * @param[out] sensor_data the data list from connected sensor, the caller should explicitly free this list.
478  * @param[out] count the count of data contained in the list.
479  * @return true on success, otherwise false.
480  */
481 bool sensord_get_data_list(int handle, unsigned int data_id, sensor_data_t** sensor_data, int* count);
482
483 /**
484  * @brief flush sensor data from a connected sensor
485  *
486  * @param[in] handle a handle represensting a connected context sensor.
487  * @return true on success, otherwise false.
488  */
489 bool sensord_flush(int handle);
490
491 bool sensord_set_passive_mode(int handle, bool passive);
492
493
494 /* Sensor Internal API using URI */
495 int sensord_get_default_sensor_by_uri(const char *uri, sensor_t *sensor);
496 int sensord_get_sensors_by_uri(const char *uri, sensor_t **list, int *sensor_count);
497
498 typedef void (*sensord_added_cb)(const char *uri, void *user_data);
499 int sensord_add_sensor_added_cb(sensord_added_cb callback, void *user_data);
500 int sensord_remove_sensor_added_cb(sensord_added_cb callback);
501
502 typedef void (*sensord_removed_cb)(const char *uri, void *user_data);
503 int sensord_add_sensor_removed_cb(sensord_removed_cb callback, void *user_data);
504 int sensord_remove_sensor_removed_cb(sensord_removed_cb callback);
505
506 /* Sensor provider */
507 typedef void *sensord_provider_h;
508 int sensord_create_provider(const char *uri, sensord_provider_h *provider);
509 int sensord_destroy_provider(sensord_provider_h provider);
510 int sensord_add_provider(sensord_provider_h provider);
511 int sensord_remove_provider(sensord_provider_h provider);
512
513 int sensord_provider_set_name(sensord_provider_h provider, const char *name);
514 int sensord_provider_set_vendor(sensord_provider_h provider, const char *vendor);
515 int sensord_provider_set_range(sensord_provider_h provider, float min_range, float max_range);
516 int sensord_provider_set_resolution(sensord_provider_h provider, float resolution);
517
518 typedef void (*sensord_provider_start_cb)(sensord_provider_h provider, void *user_data);
519 int sensord_provider_set_start_cb(sensord_provider_h provider, sensord_provider_start_cb callback, void *user_data);
520
521 typedef void (*sensord_provider_stop_cb)(sensord_provider_h provider, void *user_data);
522 int sensord_provider_set_stop_cb(sensord_provider_h provider, sensord_provider_stop_cb callback, void *user_data);
523
524 typedef void (*sensord_provider_interval_changed_cb)(sensord_provider_h provider, unsigned int interval_ms, void *user_data);
525 int sensord_provider_set_interval_changed_cb(sensord_provider_h provider, sensord_provider_interval_changed_cb callback, void *user_data);
526
527 typedef void (*sensord_provider_attribute_str_cb)(sensord_provider_h provider, int attribute, const char *value, int count, void *user_data);
528 int sensord_provider_set_attribute_str_cb(sensord_provider_h provider, sensord_provider_attribute_str_cb callback, void *user_data);
529
530 int sensord_provider_publish(sensord_provider_h provider, sensor_data_t data);
531 int sensord_provider_publish_events(sensord_provider_h provider, sensor_data_t events[], int count);
532
533 /* Deprecated */
534 typedef void (*sensor_external_command_cb_t)(int handle, const char* data, int data_cnt, void *user_data);
535 int sensord_external_connect(const char *key, sensor_external_command_cb_t cb, void *user_data);
536 bool sensord_external_disconnect(int handle);
537 bool sensord_external_post(int handle, unsigned long long timestamp, const float* data, int data_cnt);
538
539 /**
540   * @}
541  */
542
543 #ifdef __cplusplus
544 }
545 #endif
546
547 #endif /* __SENSORD_INTERNAL_H__ */