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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
/** \file
* \brief Wrappers for the Gyroscope Sensor
*
* \see https://www.w3.org/TR/Gyroscope/
*
* \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
* Gyroscope readings can be used for speech recognition: https://crypto.stanford.edu/gyrophone/
* and various fingerprinting operations. For stationary devices, the resonance of the unique internal or
* external sounds affects angular velocities affect the Gyroscope and allow to create a fingerprint:
* https://www.researchgate.net/publication/356678825_Mobile_Device_Fingerprint_Identification_Using_Gyroscope_Resonance
* For moving devices, one of the options is using the Gyroscope analyze human walking patterns:
* https://www.ncbi.nlm.nih.gov/pmc/articles/PMC7071017/
*
*
* WRAPPING
* The Gyroscope sensor provides readings of the angular velocity of the device alongthe x/y/z axes.
* For a stationary device, all velocities should be zero in an ideal state. As we observed on the
* examined devices, device sensor imperfections andlittle vibrations cause the `x`, `y` and `z` to
* oscillate between -0.002 and 0.002 on the examined devices. The wrapper simulates the same behavior.
*
*
* POSSIBLE IMPROVEMENTS
* Support for simulation of a non-stationary device. This would require
* modifications to other movement-related sensors (Accelerometer, etc.)
*
*/
/*
* Create private namespace
*/
(function() {
/*
* \brief Initialization of data for storing sensor readings
*/
var init_data = `
var currentReading = currentReading || {orig_x: null, orig_y: null, orig_z: null, timestamp: null,
fake_x: null, fake_y: null, fake_z: null};
var previousReading = previousReading || {orig_x: null, orig_y: null, orig_z: null, timestamp: null,
fake_x: null, fake_y: null, fake_z: null};
var emulateStationaryDevice = (typeof args === 'undefined') ? true : args[0];
var debugMode = false;
const TWOPI = 2 * Math.PI;
`;
/*
* \brief Property getters of the original sensor object
*/
var orig_getters = `
var origGetX = Object.getOwnPropertyDescriptor(Gyroscope.prototype, "x").get;
var origGetY = Object.getOwnPropertyDescriptor(Gyroscope.prototype, "y").get;
var origGetZ = Object.getOwnPropertyDescriptor(Gyroscope.prototype, "z").get;
var origGetTimestamp = Object.getOwnPropertyDescriptor(Sensor.prototype, "timestamp").get;
`;
/*
* \brief Changes the value on the given axis to a new one from the given interval
*
* \param the axis object (min, max, value, and decimalPlaces properties required)
*/
function shake(axis) {
val = sen_prng() * (axis.max - axis.min) + axis.min;
var precision = Math.pow(10, -1 * axis.decimalPlaces);
if (val < precision) {
val = 0;
}
if (axis.canBeNegative) {
val *= Math.round(sen_prng()) ? 1 : -1;
}
if (val == 0) {
axis.value = 0;
} else {
axis.value = fixedNumber(val, axis.decimalPlaces);
}
}
/*
* \brief The data generator for creating fake Gyroscope values
*/
class DataGenerator {
constructor() {
this.NEXT_CHANGE_MS_MIN = 500;
this.NEXT_CHANGE_MS_MAX = 2000;
this.x = {
name: "x",
min: 0.0,
max: 0.0021,
decimalPlaces: 3,
canBeNegative: true,
value: null
};
this.y = {
name: "y",
min: 0.0,
max: 0.0021,
decimalPlaces: 3,
canBeNegative: true,
value: null
};
this.z = {
name: "z",
min: 0.0,
max: 0.0021,
decimalPlaces: 3,
canBeNegative: true,
value: null
};
this.nextChangeTimeX = null; // miliseconds
this.nextChangeTimeY = null;
this.nextChangeTimeZ = null;
}
/*
* \brief Updates the x/y/z axes values based on the current timestamp
*
* \param Current timestamp from the sensor object
*/
update(currentTimestamp) {
// Simulate the Gyroscope changes
if (this.shouldWeUpdateX(currentTimestamp)) {
shake(this.x);
this.setNextChangeX(currentTimestamp);
};
if (this.shouldWeUpdateY(currentTimestamp)) {
shake(this.y);
this.setNextChangeY(currentTimestamp);
};
if (this.shouldWeUpdateZ(currentTimestamp)) {
shake(this.z);
this.setNextChangeZ(currentTimestamp);
};
}
/*
* \brief Boolean function that decides if the value on the axis X
* should be updated. Returns true if update is needed.
*
* \param Current timestamp from the sensor object
*/
shouldWeUpdateX(currentTimestamp) {
if (currentTimestamp === null || this.nextChangeTimeX === null) {
return true;
}
if (currentTimestamp >= this.nextChangeTimeX) {
return true;
} else {
return false;
}
}
/*
* \brief Boolean function that decides if the value on the axis Y
* should be updated. Returns true if update is needed.
*
* \param Current timestamp from the sensor object
*/
shouldWeUpdateY(currentTimestamp) {
if (currentTimestamp === null || this.nextChangeTimeY === null) {
return true;
}
if (currentTimestamp >= this.nextChangeTimeY) {
return true;
} else {
return false;
}
}
/*
* \brief Boolean function that decides if the value on the axis Z
* should be updated. Returns true if update is needed.
*
* \param Current timestamp from the sensor object
*/
shouldWeUpdateZ(currentTimestamp) {
if (currentTimestamp === null || this.nextChangeTimeZ === null) {
return true;
}
if (currentTimestamp >= this.nextChangeTimeZ) {
return true;
} else {
return false;
}
}
/*
* \brief Sets the timestamp of the next update of value on the axis X.
*
* \param Current timestamp from the sensor object
*/
setNextChangeX(currentTimestamp) {
let interval_ms = Math.floor(
sen_prng() * (this.NEXT_CHANGE_MS_MAX - this.NEXT_CHANGE_MS_MIN + 1)
+ this.NEXT_CHANGE_MS_MIN
);
this.nextChangeTimeX = currentTimestamp + interval_ms;
}
/*
* \brief Sets the timestamp of the next update of value on the axis Y.
*
* \param Current timestamp from the sensor object
*/
setNextChangeY(currentTimestamp) {
let interval_ms = Math.floor(
sen_prng() * (this.NEXT_CHANGE_MS_MAX - this.NEXT_CHANGE_MS_MIN + 1)
+ this.NEXT_CHANGE_MS_MIN
);
this.nextChangeTimeY = currentTimestamp + interval_ms;
}
/*
* \brief Sets the timestamp of the next update of value on the axis Z.
*
* \param Current timestamp from the sensor object
*/
setNextChangeZ(currentTimestamp) {
let interval_ms = Math.floor(
sen_prng() * (this.NEXT_CHANGE_MS_MAX - this.NEXT_CHANGE_MS_MIN + 1)
+ this.NEXT_CHANGE_MS_MIN
);
this.nextChangeTimeZ = currentTimestamp + interval_ms;
}
};
/*
* \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 x,y,z
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 sensor object directly.
currentTimestamp = sensorObject.timestamp;
}
if (currentTimestamp === previousReading.timestamp) {
// 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
currentReading.orig_x = origGetX.call(sensorObject);
currentReading.orig_y = origGetY.call(sensorObject);
currentReading.orig_z = origGetZ.call(sensorObject);
currentReading.timestamp = currentTimestamp;
dataGenerator.update(currentTimestamp);
currentReading.fake_x = dataGenerator.x.value;
currentReading.fake_y = dataGenerator.y.value;
currentReading.fake_z = dataGenerator.z.value;
if (debugMode) {
}
}
/*
* \brief Initializes the related generators
*/
var generators = `
// Initialize the data generator, if not initialized before
var dataGenerator = dataGenerator || new DataGenerator();
`;
var helping_functions = sensorapi_prng_functions
+ DataGenerator + shake + updateReadings;
var hc = init_data + orig_getters + helping_functions + generators;
var wrappers = [
{
parent_object: "Gyroscope.prototype",
parent_object_property: "x",
wrapped_objects: [],
helping_code: hc,
post_wrapping_code: [
{
code_type: "object_properties",
parent_object: "Gyroscope.prototype",
parent_object_property: "x",
wrapped_objects: [],
/** \brief replaces Sensor.prototype.x getter to return a faked value
*/
wrapped_properties: [
{
property_name: "get",
property_value: `
function() {
updateReadings(this);
return currentReading.fake_x;
}`,
},
],
}
],
},
{
parent_object: "Gyroscope.prototype",
parent_object_property: "y",
wrapped_objects: [],
helping_code: hc,
post_wrapping_code: [
{
code_type: "object_properties",
parent_object: "Gyroscope.prototype",
parent_object_property: "y",
wrapped_objects: [],
/** \brief replaces Sensor.prototype.y getter to return a faked value
*/
wrapped_properties: [
{
property_name: "get",
property_value: `
function() {
updateReadings(this);
return currentReading.fake_y;
}`,
},
],
}
],
},
{
parent_object: "Gyroscope.prototype",
parent_object_property: "z",
wrapped_objects: [],
helping_code: hc,
post_wrapping_code: [
{
code_type: "object_properties",
parent_object: "Gyroscope.prototype",
parent_object_property: "z",
wrapped_objects: [],
/** \brief replaces Sensor.prototype.z getter to return a faked value
*/
wrapped_properties: [
{
property_name: "get",
property_value: `
function() {
updateReadings(this);
return currentReading.fake_z;
}`,
},
],
}
],
},
]
add_wrappers(wrappers);
})()
|