4 * Copyright (c) 2014 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>
40 #include <sf_common.h>
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;
59 sensor_accuracy_changed_cb_t accuracy_cb;
60 unsigned long long timestamp;
62 void *accuracy_user_data;
63 GMainContext *maincontext;
65 } client_callback_info;
67 typedef void (*hup_observer_t)(void);
69 class sensor_event_listener {
71 static sensor_event_listener& get_instance(void);
72 bool start_handle(int handle);
73 bool stop_handle(int handle);
75 void operate_sensor(sensor_id_t sensor, int power_save_state);
76 void get_listening_sensors(sensor_id_vector &sensors);
78 bool start_event_listener(void);
79 void stop_event_listener(void);
82 void set_hup_observer(hup_observer_t observer);
87 THREAD_STATE_TERMINATE,
89 typedef std::lock_guard<std::mutex> lock;
90 typedef std::unique_lock<std::mutex> ulock;
92 csocket m_event_socket;
95 thread_state m_thread_state;
96 std::mutex m_thread_mutex;
97 std::condition_variable m_thread_cond;
99 hup_observer_t m_hup_observer;
101 sensor_event_listener();
102 ~sensor_event_listener();
104 sensor_event_listener(const sensor_event_listener&);
105 sensor_event_listener& operator=(const sensor_event_listener&);
107 bool create_event_channel(void);
108 void close_event_channel(void);
110 ssize_t sensor_event_poll(void* buffer, int buffer_len, struct epoll_event &event);
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);
116 client_callback_info* get_callback_info(sensor_id_t sensor_id, const reg_event_info *event_info, void *sensor_data, void *buffer);
118 unsigned long long renew_event_id(void);
120 void post_callback_to_main_loop(client_callback_info *cb_info);
122 bool is_valid_callback(client_callback_info *cb_info);
123 static gboolean callback_dispatcher(gpointer data);
125 void set_thread_state(thread_state state);
127 sensor_client_info &m_client_info;
129 #endif /* _SENSOR_EVENT_LISTENER_H_ */