tizen beta release
[framework/web/wrt-plugins-common.git] / src / modules / tizen / Accelerometer / Accelerometer.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /*
17  * @file          Accelerometer.h
18  * @author      Qi Xiangguo (xiangguo.qi@samsung.com)
19  * @version     0.1
20  * @brief
21  */
22
23 #ifndef WRTDEVICEAPIS_ACCELEROMETER_ACCELEROMETER_H_
24 #define WRTDEVICEAPIS_ACCELEROMETER_ACCELEROMETER_H_
25
26 #include <map>
27 #include <dpl/shared_ptr.h>
28 #include <dpl/noncopyable.h>
29 #include <sensor.h>
30 #include <sensor_accel.h>
31 #include <Accelerometer/IAccelerometer.h>
32 #include <Accelerometer/AccelerometerFactory.h>
33 #include <Accelerometer/EventAccelerationChanged.h>
34 #include <Commons/Emitters.h>
35
36 namespace WrtDeviceApis {
37 namespace Accelerometer {
38
39 class Accelerometer : public Api::IAccelerometer
40 {
41     int m_handle;
42     gulong m_signalId;
43
44     /**
45      * @See: IgetCurrentAcceleration
46      */
47     friend class Api::AccelerometerFactory;
48
49   public:
50     class Watcher
51     {
52       private:
53         int m_handle;
54         Api::EventAccelerationChangedEmitterPtr m_emitter;
55
56       public:
57         Watcher(
58                 int handle,
59                 const Api::EventAccelerationChangedEmitterPtr&
60                 emitter) :
61             m_handle(handle),
62             m_emitter(emitter)
63         {
64         }
65
66         int& getHandle()
67         {
68             return m_handle;
69         }
70
71         Api::EventAccelerationChangedEmitterPtr getEmitter()
72         {
73             return m_emitter;
74         }
75
76         void emit(const Api::EventAccelerationChangedPtr& event)
77         {
78             m_emitter->emit(event);
79         }
80         void getCurrentAccelerationForWatch();
81         void accelerationHasChanged(sensor_event_data_t *sensorEvent);
82     };
83     typedef DPL::SharedPtr<Watcher> WatcherPtr;
84
85   public:
86     virtual ~Accelerometer();
87
88     /**
89      * @See: IgetCurrentAcceleration
90      */
91     virtual void getCurrentAcceleration(
92             const Api::EventGetCurrentAccelerationPtr& event);
93
94     /**
95      * @See: IwatchAcceleration
96      */
97     virtual long watchAcceleration(
98             const Api::EventAccelerationChangedEmitterPtr&
99             emitter,
100             long minNotificationInterval);
101
102     /**
103      * @See: IclearWatch
104      */
105     virtual void clearWatch(
106             Api::EventAccelerationChangedEmitter::IdType id);
107
108     /**
109      * Method to run from platform's callback function
110      */
111     void accelerationHasChanged(sensor_event_data_t *sensorEvent);
112
113     /**
114      * Method to run currentAcceleration for watch function
115      */
116
117     void getCurrentAccelerationForWatch();
118
119   protected:
120     Accelerometer();
121     virtual void OnRequestReceived(
122             const Api::EventGetCurrentAccelerationPtr& event);
123
124   private:
125     typedef Commons::Emitters<Api::EventAccelerationChangedEmitter>
126     AccelerationChangedEmitters;
127
128     /**
129      * Initialize accelerometer sensor.  This method returns when platform error occurs.
130      */
131     int initialize();
132
133     /**
134      * Start accelerometer sensor. This method sends a start command to sensor server.
135      */
136     void start(int handle);
137
138     /**
139      *  Stop accelerometer sensor. This method sends a stop command to sensor server.
140      */
141     void stop(int& handle, bool isWatcherHandle = false);
142
143     /**
144      * Method to restart accelerometer sensor server.
145      */
146     void restart(int& handle);
147
148     AccelerationChangedEmitters m_AccelerationEmitters;
149     DPL::Mutex m_initializationMutex;
150     bool m_initialized;
151     bool m_isWatch;
152
153     std::vector<WatcherPtr> m_watchers;
154 };
155
156 }
157 }
158
159 #endif // WRTDEVICEAPIS_ACCELEROMETER_ACCELEROMETER_H_