4 * Copyright (c) 2013 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #ifndef _SENSOR_EVENT_LISTENER_H_
21 #define _SENSOR_EVENT_LISTENER_H_
24 #include <sys/types.h>
25 #include <sys/epoll.h>
31 #include <unordered_map>
36 #include <condition_variable>
41 #include <sensor_handle_info.h>
42 #include <sensor_client_info.h>
43 #include <command_channel.h>
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;
51 unsigned long long event_id;
54 unsigned int event_type;
56 std::shared_ptr<void> sensor_data;
58 sensor_accuracy_changed_cb_t accuracy_cb;
59 unsigned long long timestamp;
61 void *accuracy_user_data;
62 } client_callback_info;
64 typedef void (*hup_observer_t)(void);
66 class sensor_event_listener {
68 static sensor_event_listener& get_instance(void);
69 bool start_handle(int handle);
70 bool stop_handle(int handle);
72 void operate_sensor(sensor_id_t sensor, int power_save_state);
73 void get_listening_sensors(sensor_id_vector &sensors);
75 bool start_event_listener(void);
76 void stop_event_listener(void);
79 void set_hup_observer(hup_observer_t observer);
84 THREAD_STATE_TERMINATE,
86 typedef std::lock_guard<std::mutex> lock;
87 typedef std::unique_lock<std::mutex> ulock;
89 csocket m_event_socket;
92 thread_state m_thread_state;
93 std::mutex m_thread_mutex;
94 std::condition_variable m_thread_cond;
96 hup_observer_t m_hup_observer;
98 sensor_client_info &m_client_info;
100 sensor_event_listener();
101 ~sensor_event_listener();
103 sensor_event_listener& operator=(const sensor_event_listener&);
105 bool create_event_channel(void);
106 void close_event_channel(void);
108 ssize_t sensor_event_poll(void *buffer, int buffer_len, struct epoll_event &event);
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);
114 client_callback_info* get_callback_info(sensor_id_t sensor_id, const reg_event_info *event_info, std::shared_ptr<void> sensor_data);
116 unsigned long long renew_event_id(void);
118 void post_callback_to_main_loop(client_callback_info *cb_info);
120 bool is_valid_callback(client_callback_info *cb_info);
121 static gboolean callback_dispatcher(gpointer data);
123 void set_thread_state(thread_state state);
125 #endif /* _SENSOR_EVENT_LISTENER_H_ */