sensord: use shared_ptr for sensor data which can be used in serveral callbacks
[platform/core/system/sensord.git] / src / client / sensor_event_listener.h
1 /*
2  * sensord
3  *
4  * Copyright (c) 2013 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 <memory>
38 #include <cmutex.h>
39 #include <poller.h>
40
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         void *cb;
56         std::shared_ptr<void> sensor_data;
57         void *user_data;
58         sensor_accuracy_changed_cb_t accuracy_cb;
59         unsigned long long timestamp;
60         int accuracy;
61         void *accuracy_user_data;
62 } client_callback_info;
63
64 typedef void (*hup_observer_t)(void);
65
66 class sensor_event_listener {
67 public:
68         static sensor_event_listener& get_instance(void);
69         bool start_handle(int handle);
70         bool stop_handle(int handle);
71
72         void operate_sensor(sensor_id_t sensor, int power_save_state);
73         void get_listening_sensors(sensor_id_vector &sensors);
74
75         bool start_event_listener(void);
76         void stop_event_listener(void);
77         void clear(void);
78
79         void set_hup_observer(hup_observer_t observer);
80 private:
81         enum thread_state {
82                 THREAD_STATE_START,
83                 THREAD_STATE_STOP,
84                 THREAD_STATE_TERMINATE,
85         };
86         typedef std::lock_guard<std::mutex> lock;
87         typedef std::unique_lock<std::mutex> ulock;
88
89         csocket m_event_socket;
90         poller *m_poller;
91
92         thread_state m_thread_state;
93         std::mutex m_thread_mutex;
94         std::condition_variable m_thread_cond;
95
96         hup_observer_t m_hup_observer;
97
98         sensor_client_info &m_client_info;
99
100         sensor_event_listener();
101         ~sensor_event_listener();
102
103         sensor_event_listener& operator=(const sensor_event_listener&);
104
105         bool create_event_channel(void);
106         void close_event_channel(void);
107
108         ssize_t sensor_event_poll(void *buffer, int buffer_len, struct epoll_event &event);
109
110         void listen_events(void);
111         client_callback_info* handle_calibration_cb(sensor_handle_info &handle_info, unsigned event_type, unsigned long long time, int accuracy);
112         void handle_events(void* event);
113
114         client_callback_info* get_callback_info(sensor_id_t sensor_id, const reg_event_info *event_info, std::shared_ptr<void> sensor_data);
115
116         unsigned long long renew_event_id(void);
117
118         void post_callback_to_main_loop(client_callback_info *cb_info);
119
120         bool is_valid_callback(client_callback_info *cb_info);
121         static gboolean callback_dispatcher(gpointer data);
122
123         void set_thread_state(thread_state state);
124 };
125 #endif /* _SENSOR_EVENT_LISTENER_H_ */