sensord: clean up header dependency
[platform/core/system/sensord.git] / src / client / sensor_event_listener.h
1 /*
2  * libsensord
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 _SENSOR_EVENT_LISTENER_H_
21 #define _SENSOR_EVENT_LISTENER_H_
22
23 #include <glib.h>
24 #include <sys/types.h>
25 #include <sys/epoll.h>
26 #include <unistd.h>
27 #include <csocket.h>
28 #include <string.h>
29 #include <algorithm>
30 #include <sstream>
31 #include <unordered_map>
32 #include <vector>
33 #include <string>
34 #include <queue>
35 #include <mutex>
36 #include <condition_variable>
37 #include <cmutex.h>
38 #include <poller.h>
39
40 #include <sf_common.h>
41 #include <sensor_handle_info.h>
42 #include <sensor_client_info.h>
43 #include <command_channel.h>
44
45 typedef std::vector<unsigned int> handle_vector;
46 typedef std::vector<sensor_id_t> sensor_id_vector;
47 typedef std::unordered_map<int,sensor_handle_info> sensor_handle_info_map;
48 typedef std::unordered_map<sensor_id_t, command_channel*> sensor_command_channel_map;
49
50 typedef struct {
51         unsigned long long event_id;
52         int handle;
53         sensor_t sensor;
54         unsigned int event_type;
55         int cb_type;
56         void *cb;
57         void *sensor_data;
58         void *user_data;
59         sensor_accuracy_changed_cb_t accuracy_cb;
60         unsigned long long timestamp;
61         int accuracy;
62         void *accuracy_user_data;
63         GMainContext *maincontext;
64         void *buffer;
65 } client_callback_info;
66
67 typedef void (*hup_observer_t)(void);
68
69 class sensor_event_listener {
70 public:
71         static sensor_event_listener& get_instance(void);
72         bool start_handle(int handle);
73         bool stop_handle(int handle);
74
75         void operate_sensor(sensor_id_t sensor, int power_save_state);
76         void get_listening_sensors(sensor_id_vector &sensors);
77
78         bool start_event_listener(void);
79         void stop_event_listener(void);
80         void clear(void);
81
82         void set_hup_observer(hup_observer_t observer);
83 private:
84         enum thread_state {
85                 THREAD_STATE_START,
86                 THREAD_STATE_STOP,
87                 THREAD_STATE_TERMINATE,
88         };
89         typedef std::lock_guard<std::mutex> lock;
90         typedef std::unique_lock<std::mutex> ulock;
91
92         csocket m_event_socket;
93         poller *m_poller;
94
95         thread_state m_thread_state;
96         std::mutex m_thread_mutex;
97         std::condition_variable m_thread_cond;
98
99         hup_observer_t m_hup_observer;
100
101         sensor_event_listener();
102         ~sensor_event_listener();
103
104         sensor_event_listener(const sensor_event_listener&);
105         sensor_event_listener& operator=(const sensor_event_listener&);
106
107         bool create_event_channel(void);
108         void close_event_channel(void);
109
110         ssize_t sensor_event_poll(void* buffer, int buffer_len, struct epoll_event &event);
111
112         void listen_events(void);
113         client_callback_info* handle_calibration_cb(sensor_handle_info &handle_info, unsigned event_type, unsigned long long time, int accuracy);
114         void handle_events(void* event);
115
116         client_callback_info* get_callback_info(sensor_id_t sensor_id, const reg_event_info *event_info, void *sensor_data, void *buffer);
117
118         unsigned long long renew_event_id(void);
119
120         void post_callback_to_main_loop(client_callback_info *cb_info);
121
122         bool is_valid_callback(client_callback_info *cb_info);
123         static gboolean callback_dispatcher(gpointer data);
124
125         void set_thread_state(thread_state state);
126
127         sensor_client_info &m_client_info;
128 };
129 #endif /* _SENSOR_EVENT_LISTENER_H_ */