1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
/** \file
* \brief Wrappers for the AbsoluteOrientationSensor and RelativeOrientationSensor
*
* \see https://www.w3.org/TR/orientation-sensor
* \see https://www.w3.org/TR/orientation-sensor/#absoluteorientationsensor-model
* \see https://www.w3.org/TR/orientation-sensor/#relativeorientationsensor-model
*
* \author Copyright (C) 2021 Radek Hranicky
*
* \license SPDX-License-Identifier: GPL-3.0-or-later
*/
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
/** \file
* \ingroup wrappers
*
* MOTIVATION
* Device orientation sensors can be easily used for fingerprinting. As it highly
* unlikely that two devices visiting the same site will be oriented exactly
* the same, the orientation itself can serve as a fingerprint.
*
*
* WRAPPING
* AbsoluteOrientationSensor returns a quaterion decribing the physical
* orientation of the device in relation to the Earth's reference coordinate
* system. The faked orientation of the device is saved inside the "orient"
* global variable that is accessible to all wrappers. The value is chosen
* pseudorandomly from the domain hash. The wrappper supports possible change
* of orientation. With each reading, it loads the "orient"'s contents,
* converts the rotation matrix to a quaternion that is returned by the wrapped
* getter.
*
* RelativeOrientationSensor also describes the orientation, but without
* regard to the Earth's reference coordinate system. We suppose the coordinate
* system is chosen at the beginning of the sensor instance creation.
* As we observed, no matter how the device is oriented, there is always a slight
* difference from the AbsoluteOrientationSensor's in at least one axis.
* When the device moves, both sensors' readings change. But their difference
* should be always constant. And thus, we pseudorandomly generate a deviation
* from the Earth's reference coordinate system. And for each reading, we
* take the values from the fake AbsoluteOrientationSensor and modify them
* by the constant deviation.
*
* POSSIBLE IMPROVEMENTS
* Study the supported coordinate systems of the RelativeOrientationSensor
* and modify the wrapper behavior if needed.
*/
/*
* Create private namespace
*/
(function() {
/*
* \brief Initialization of data for storing sensor readings
*/
var init_data = `
var currentReading = currentReading || {quaternion: null, fake_quaternion: null, fake_quaternion_rel: null, timestamp: null};
var previousReading = previousReading || {quaternion: null, fake_quaternion: null, fake_quaternion_rel: null, timestamp: null};
var debugMode = false;
const TWOPI = 2 * Math.PI;
`;
/*
* \brief Property getters of the original sensor object
*/
var orig_getters = `
var origGetQuaternion = Object.getOwnPropertyDescriptor(OrientationSensor.prototype, "quaternion").get;
var origGetTimestamp = Object.getOwnPropertyDescriptor(Sensor.prototype, "timestamp").get;
`;
/*
* \brief Convert a given 3D rotation matrix to quaternion
*
* \param Rotation matrix
*/
function matrixToQuaternion(rot) {
var q = {x: null, y: null, z: null, w: null};
var m;
if (rot[2][2] < 0) {
if (rot[0][0] > rot[1][1]) {
m = 1 + rot[0][0] -rot[1][1] -rot[2][2];
q.x = m;
q.y = rot[0][1]+rot[1][0];
q.z = rot[2][0]+rot[0][2];
q.w = rot[1][2]-rot[2][1];
} else {
m = 1 -rot[0][0] + rot[1][1] -rot[2][2];
q.x = rot[0][1]+rot[1][0];
q.y = m;
q.z = rot[1][2]+rot[2][1];
q.w = rot[2][0]-rot[0][2];
}
} else {
if (rot[0][0] < -rot[1][1]) {
m = 1 -rot[0][0] -rot[1][1] + rot[2][2];
q.x = rot[2][0]+rot[0][2];
q.y = rot[1][2]+rot[2][1];
q.z = m;
q.w = rot[0][1]-rot[1][0];
} else {
m = 1 + rot[0][0] + rot[1][1] + rot[2][2];
q.x = rot[1][2]-rot[2][1];
q.y = rot[2][0]-rot[0][2];
q.z = rot[0][1]-rot[1][0];
q.w = m;
}
}
var multiplier = 0.5 / Math.sqrt(m);
q.x *= multiplier;
q.y *= multiplier;
q.z *= multiplier;
q.w *= multiplier;
return q;
}
/*
* \brief The fake quaternion generator class
* Note: Requires "orient" global var to be set.
*/
class QuaternionGenerator {
constructor() {
this.DEVIATION_MIN = 0;
this.DEVIATION_MAX = (Math.PI / 2) / 90 * 10; // 10°
this.quaternion = null;
this.quaternion_rel = null;
this.yawDeviation = this.generateDeviation();
this.pitchDeviation = this.generateDeviation();
this.rollDeviation = this.generateDeviation();
}
/*
* \brief Generates the rotation deviation
*/
generateDeviation() {
var devi = sen_prng() * (this.DEVIATION_MAX - this.DEVIATION_MIN) + this.DEVIATION_MIN;
devi *= Math.round(sen_prng()) ? 1 : -1;
return devi;
}
/*
* \brief Updates the fake quaternions
*
* \param Current timestamp from the sensor object
*/
update(t) {
// Calculate quaternion for absolute orientation
var rotMat = orient.rotMat; // Get the device rotation matrix
var q = matrixToQuaternion(rotMat);
this.quaternion = [
fixedNumber(q.x, 3),
fixedNumber(q.y, 3),
fixedNumber(q.z, 3),
fixedNumber(q.w, 3)
];
// Calculate quaternion for relative orientation
var relYaw = (orient.yaw + this.yawDeviation) % TWOPI;
var relPitch = (orient.pitch + this.pitchDeviation) % TWOPI;
var relRoll = (orient.roll + this.rollDeviation) % TWOPI;
var relMat = calculateRotationMatrix(relYaw, relPitch, relRoll);
var qr = matrixToQuaternion(relMat);
this.quaternion_rel = [
fixedNumber(qr.x, 3),
fixedNumber(qr.y, 3),
fixedNumber(qr.z, 3),
fixedNumber(qr.w, 3)
];
}
}
/*
* \brief Updates the stored (both real and fake) sensor readings
* according to the data from the sensor object.
*
* \param The sensor object
*/
function updateReadings(sensorObject) {
// We need the original reading's timestamp to see if it differs
// from the previous sample. If so, we need to update the faked quaternion
let previousTimestamp = previousReading.timestamp;
let currentTimestamp = origGetTimestamp.call(sensorObject);
if (debugMode) {
// [!] Debug mode: overriding timestamp
// This allows test suites to set a custom timestamp externally
// by modifying the property of the Magnetometer object directly.
currentTimestamp = sensorObject.timestamp;
}
if (currentTimestamp === previousTimestamp) {
// No new reading, nothing to update
return;
}
// Rotate the readings: previous <- current
previousReading = JSON.parse(JSON.stringify(currentReading));
// Update current reading
// NOTE: Original values are also stored for possible future use
// in improvements of the magnetic field generator
currentReading.orig_quaterion = origGetQuaternion.call(sensorObject);
currentReading.timestamp = currentTimestamp;
quaternionGenerator.update(currentTimestamp);
currentReading.fake_quaternion = quaternionGenerator.quaternion;
currentReading.fake_quaternion_rel = quaternionGenerator.quaternion_rel;
if (debugMode) {
}
}
/*
* \brief Initializes the related generators
*/
var generators = `
// Initialize the quaternion generator, if not initialized before
var quaternionGenerator = quaternionGenerator || new QuaternionGenerator();
`;
var helping_functions = sensorapi_prng_functions + device_orientation_functions
+ matrixToQuaternion + QuaternionGenerator + updateReadings;
var hc = init_data + orig_getters + helping_functions + generators;
var wrappers = [
{
parent_object: "OrientationSensor.prototype",
parent_object_property: "quaternion",
wrapped_objects: [],
helping_code: hc,
post_wrapping_code: [
{
code_type: "object_properties",
parent_object: "OrientationSensor.prototype",
parent_object_property: "quaternion",
wrapped_objects: [],
/** \brief replaces OrientationSensor.quaternion getter to return a faked value
*/
wrapped_properties: [
{
property_name: "get",
property_value: `
function() {
updateReadings(this);
if (this.__proto__.constructor.name === 'AbsoluteOrientationSensor') {
// AbsoluteOrientationSensor
return currentReading.fake_quaternion;
} else {
// RelativeOrientationSensor
return currentReading.fake_quaternion_rel;
}
}`,
},
],
}
],
}
]
add_wrappers(wrappers);
})()
|