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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
/** \file
* \brief Wrappers for the Accelerometer Sensor, LinearAccelerationSensor,
* and GravitySensor
*
* \see https://www.w3.org/TR/accelerometer/
* \see https://www.w3.org/TR/accelerometer/#linearaccelerationsensor
* \see https://www.w3.org/TR/accelerometer/#gravitysensor
*
* \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
* Readings from the Accelerometer, LinearAccelerationSensor, and GravitySensor
* of the Generic Sensor API should be secured as they provide a potentially
* valuable data for creating fingerprints. There are multiple options.
* A unique fingerprint can be obtained by describing the device's vibrations
* (See https://link.springer.com/chapter/10.1007/978-3-319-30806-7_7).
* Using trajectory inference and matching of the model to map data, one may
* use the readings from the Accelerometer to determing the device's position
* (See https://www.researchgate.net/publication/220990763_ACComplice_Location_
* inference_using_accelerometers_on_smartphones).
* Accelerometer readings can also be used for determining human walking patterns
* (See https://www.researchgate.net/publication/322835708_Classifying_Human_
* Walking_Patterns_using_Accelerometer_Data_from_Smartphone).
*
*
* WRAPPING
* The wrapper replaces the "XYZ" getters of the Accelerometer sensor,
* LinearAccelerationSensor, and GravitySensor. The wrapping's goal is to
* simulate a stationary device that can be possibly rotated. The rotation
* of the device is represented by the fake rotation matrix "orient.rotMat".
*
* The GravitySensor should provide readings of gravity acceleration applied
* to the device. This is represented by a vector made of x, y, z portions.
* To get this faked gravity vector for the device, the reference vector
* [0, 0, 9.8] is multipled with the rotation matrix. Wrappers for the
* GravitySensor's getters return x, y, z portions of the fake gravity vector.
*
* Next, the LinearAccelerationSensor should return acceleration values without
* the contribution of gravity. For a stationary device, it should be all zeroes.
* Yet, there could be vibrations that may change values a little bit, e.g.,
* spin around -0.1 to +0.1, as seen on the examined devices. This usually does
* not happed with every reading but only in intervals of seconds. And thus,
* after a few seconds we pseudo-randomly change these values.
*
* Finally, the Accelerometer sensor combines the previous two. Our wrappers thus
* return tha values from the LinearAccelerationSensor with the fake gravity
* vector portions added.
*
*
* POSSIBLE IMPROVEMENTS
* Support for simulation of a non-stationary device where the rotation
* can change. Currently, the calculation of the gravity vector is done
* only once by the initDataGenerator() where the reference vector is
* multiplied with the rotation matrix. If orient.rotMat could change,
* the dataGen would have to be updated periodically.
* Moreover, such a change should also be taken into account in wrappers
* for other movement-related sensors (Gyroscope, 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, gVector: null};
var previousReading = previousReading || {orig_x: null, orig_y: null, orig_z: null, timestamp: null,
fake_x: null, fake_y: null, fake_z: null, gVector: 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(Accelerometer.prototype, "x").get;
var origGetY = Object.getOwnPropertyDescriptor(Accelerometer.prototype, "y").get;
var origGetZ = Object.getOwnPropertyDescriptor(Accelerometer.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 accelerometer values
*/
class DataGenerator {
constructor() {
this.NEXT_CHANGE_MS_MIN = 1000;
this.NEXT_CHANGE_MS_MAX = 10000;
/* Reference gravity vector
* For a non-rotated device lying bottom-down on a flat surface,
* only axis "z" is afected by g.
*/
let referenceGravityVector = [0, 0, 9.8];
/*
* For a rotated device, the reference gravity vector needs to be
* multiplied by the rotation matrix.
* Here we calculate and store the device gravity vector
*/
this.gVector = multVectRot(referenceGravityVector, orient.rotMat);
/*
* Values for the linear acceleration are fluctuating
*/
this.x = {
name: "x",
min: 0.0,
max: 0.11,
decimalPlaces: 1,
canBeNegative: true,
value: null
};
this.y = {
name: "y",
min: 0.0,
max: 0.11,
decimalPlaces: 1,
canBeNegative: true,
value: null
};
this.z = {
name: "z",
min: 0.0,
max: 0.11,
decimalPlaces: 1,
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;
currentReading.fake_gVector = dataGenerator.gVector;
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 + device_orientation_functions
+ DataGenerator + shake + updateReadings;
var hc = init_data + orig_getters + helping_functions + generators;
var wrappers = [
{
parent_object: "Accelerometer.prototype",
parent_object_property: "x",
wrapped_objects: [],
helping_code: hc,
post_wrapping_code: [
{
code_type: "object_properties",
parent_object: "Accelerometer.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);
if (this.__proto__.constructor.name === 'GravitySensor') {
return fixedNumber(currentReading.fake_gVector[0], 1);
} else if (this.__proto__.constructor.name === 'LinearAccelerationSensor') {
return fixedNumber(currentReading.fake_x, 1);
}
return fixedNumber(currentReading.fake_x + currentReading.fake_gVector[0], 1);
}`,
},
],
}
],
},
{
parent_object: "Accelerometer.prototype",
parent_object_property: "y",
wrapped_objects: [],
helping_code: hc,
post_wrapping_code: [
{
code_type: "object_properties",
parent_object: "Accelerometer.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);
if (this.__proto__.constructor.name === 'GravitySensor') {
return fixedNumber(currentReading.fake_gVector[1], 1);
} else if (this.__proto__.constructor.name === 'LinearAccelerationSensor') {
return fixedNumber(currentReading.fake_y, 1);
}
return fixedNumber(currentReading.fake_y + currentReading.fake_gVector[1], 1);
}`,
},
],
}
],
},
{
parent_object: "Accelerometer.prototype",
parent_object_property: "z",
wrapped_objects: [],
helping_code: hc,
post_wrapping_code: [
{
code_type: "object_properties",
parent_object: "Accelerometer.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);
if (this.__proto__.constructor.name === 'GravitySensor') {
return fixedNumber(currentReading.fake_gVector[2], 1);
} else if (this.__proto__.constructor.name === 'LinearAccelerationSensor') {
return fixedNumber(currentReading.fake_z, 1);
}
return fixedNumber(currentReading.fake_z + currentReading.fake_gVector[2], 1);
}`,
},
],
}
],
},
]
add_wrappers(wrappers);
})()
|