Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / xwalk / tizen / mobile / sensor / tizen_data_fetcher_shared_memory.cc
1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <math.h>
6
7 #include "xwalk/tizen/mobile/sensor/tizen_data_fetcher_shared_memory.h"
8
9 namespace xwalk {
10
11 TizenDataFetcherSharedMemory::TizenDataFetcherSharedMemory()
12     : motion_buffer_(NULL),
13       orientation_buffer_(NULL) {
14 }
15
16 TizenDataFetcherSharedMemory::~TizenDataFetcherSharedMemory() {
17   Stop(content::CONSUMER_TYPE_MOTION);
18   Stop(content::CONSUMER_TYPE_ORIENTATION);
19 }
20
21 void TizenDataFetcherSharedMemory::OnAccelerationChanged(
22     float raw_x, float raw_y, float raw_z,
23     float x, float y, float z) {
24   if (!motion_buffer_)
25     return;
26
27   motion_buffer_->seqlock.WriteBegin();
28   motion_buffer_->data.accelerationIncludingGravityX = raw_x;
29   motion_buffer_->data.hasAccelerationIncludingGravityX = true;
30   motion_buffer_->data.accelerationIncludingGravityY = raw_y;
31   motion_buffer_->data.hasAccelerationIncludingGravityY = true;
32   motion_buffer_->data.accelerationIncludingGravityZ = raw_z;
33   motion_buffer_->data.hasAccelerationIncludingGravityZ = true;
34   if (x != FP_NAN) {
35     motion_buffer_->data.accelerationX = x;
36     motion_buffer_->data.hasAccelerationX = true;
37   }
38   if (y != FP_NAN) {
39     motion_buffer_->data.accelerationY = y;
40     motion_buffer_->data.hasAccelerationY = true;
41   }
42   if (z != FP_NAN) {
43     motion_buffer_->data.accelerationZ = z;
44     motion_buffer_->data.hasAccelerationZ = true;
45   }
46   motion_buffer_->data.allAvailableSensorsAreActive = true;
47   motion_buffer_->seqlock.WriteEnd();
48 }
49
50 void TizenDataFetcherSharedMemory::OnOrientationChanged(float alpha,
51                                                         float beta,
52                                                         float gamma) {
53   if (!orientation_buffer_)
54     return;
55
56   orientation_buffer_->seqlock.WriteBegin();
57   orientation_buffer_->data.alpha = alpha;
58   orientation_buffer_->data.hasAlpha = true;
59   orientation_buffer_->data.beta = beta;
60   orientation_buffer_->data.hasBeta = true;
61   orientation_buffer_->data.gamma = gamma;
62   orientation_buffer_->data.hasGamma = true;
63   orientation_buffer_->data.allAvailableSensorsAreActive = true;
64   orientation_buffer_->seqlock.WriteEnd();
65 }
66
67 void TizenDataFetcherSharedMemory::OnRotationRateChanged(float alpha,
68                                                          float beta,
69                                                          float gamma) {
70   if (!motion_buffer_)
71     return;
72
73   motion_buffer_->seqlock.WriteBegin();
74   motion_buffer_->data.rotationRateAlpha = alpha;
75   motion_buffer_->data.hasRotationRateAlpha = true;
76   motion_buffer_->data.rotationRateBeta = beta;
77   motion_buffer_->data.hasRotationRateBeta = true;
78   motion_buffer_->data.rotationRateGamma = gamma;
79   motion_buffer_->data.hasRotationRateGamma = true;
80   motion_buffer_->data.allAvailableSensorsAreActive = true;
81   motion_buffer_->seqlock.WriteEnd();
82 }
83
84 bool TizenDataFetcherSharedMemory::Start(content::ConsumerType type,
85                                          void* buffer) {
86   DCHECK(buffer);
87
88   bool started = (motion_buffer_ || orientation_buffer_);
89   switch (type) {
90     case content::CONSUMER_TYPE_MOTION:
91       motion_buffer_ =
92           static_cast<content::DeviceMotionHardwareBuffer*>(buffer);
93       break;
94     case content::CONSUMER_TYPE_ORIENTATION:
95       orientation_buffer_ =
96           static_cast<content::DeviceOrientationHardwareBuffer*>(buffer);
97       break;
98     default:
99       NOTREACHED();
100       return false;
101   }
102
103   if (!started && SensorProvider::GetInstance()->connected())
104     SensorProvider::GetInstance()->AddObserver(this);
105
106   return true;
107 }
108
109 bool TizenDataFetcherSharedMemory::Stop(content::ConsumerType type) {
110   switch (type) {
111     case content::CONSUMER_TYPE_MOTION:
112       if (motion_buffer_) {
113         motion_buffer_->seqlock.WriteBegin();
114         motion_buffer_->data.allAvailableSensorsAreActive = false;
115         motion_buffer_->seqlock.WriteEnd();
116         motion_buffer_ = NULL;
117       }
118       break;
119     case content::CONSUMER_TYPE_ORIENTATION:
120       if (orientation_buffer_) {
121         orientation_buffer_->seqlock.WriteBegin();
122         orientation_buffer_->data.allAvailableSensorsAreActive = false;
123         orientation_buffer_->seqlock.WriteEnd();
124         orientation_buffer_ = NULL;
125       }
126       break;
127     default:
128       NOTREACHED();
129       return false;
130   }
131
132   if (!motion_buffer_ && !orientation_buffer_ &&
133       SensorProvider::GetInstance()->connected())
134     SensorProvider::GetInstance()->RemoveObserver(this);
135
136   return true;
137 }
138
139 }  // namespace xwalk