File: general.c

package info (click to toggle)
libssc 0.3.0-1
  • links: PTS
  • area: main
  • in suites: experimental
  • size: 1,840 kB
  • sloc: ansic: 3,551; python: 647; sh: 15; makefile: 11
file content (659 lines) | stat: -rw-r--r-- 22,592 bytes parent folder | download
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
/*
 * libssc: Library to expose Qualcomm Sensor Core sensors
 * Copyright (C) 2022-2025 Dylan Van Assche
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <glib.h>
#include <locale.h>
#include "libssc-client-private.h"
#include "libssc-sensor.h"
#include "libssc-sensor-proximity.h"
#include "libssc-sensor-light.h"
#include "libssc-sensor-accelerometer.h"
#include "libssc-sensor-magnetometer.h"
#include "libssc-sensor-compass.h"

#define TIMEOUT 2

typedef struct {
	SSCSensor *sensor;
	GArray *measurements;
	GMainLoop *loop;
} SensorData;

typedef struct {
	gfloat x;
	gfloat y;
	gfloat z;
} XYZMeasurement;

static gboolean
proximity_close_cb (SensorData *data)
{
	SSCSensorProximity *self = SSC_SENSOR_PROXIMITY (data->sensor);
	GArray *measurements = data->measurements;
	GMainLoop *loop = data->loop;
	g_autoptr (GError) error = NULL;

	/* Close sensor */
	g_assert_true (ssc_sensor_proximity_close_sync (self, NULL, &error));

	/* 
	 * Check measurements: proximity sensor measurements which are
	 * the same as the previous value are skipped by libssc
	 */
	g_assert_cmpint (measurements->len, >=, 8);
	g_assert_true (g_array_index (measurements, gboolean, 0) == FALSE);
	g_assert_true (g_array_index (measurements, gboolean, 1) == TRUE);
	g_assert_true (g_array_index (measurements, gboolean, 2) == FALSE);
	g_assert_true (g_array_index (measurements, gboolean, 3) == TRUE);
	g_assert_true (g_array_index (measurements, gboolean, 4) == FALSE);
	g_assert_true (g_array_index (measurements, gboolean, 5) == TRUE);
	g_assert_true (g_array_index (measurements, gboolean, 6) == FALSE);
	g_assert_true (g_array_index (measurements, gboolean, 7) == TRUE);

	g_main_loop_quit (loop);

	return G_SOURCE_REMOVE;
}

static void
proximity_measurement (SSCSensorProximity *sensor, gboolean near, gpointer user_data)
{
	GArray *measurements = user_data;

	g_test_message("NEAR: %d", near);

	/* Collect measurement */
	g_array_append_val (measurements, near);
}

static void
test_libssc_sensor_proximity(void)
{
	g_autoptr (GError) error = NULL;
	GArray *measurements = g_array_new (FALSE, FALSE, sizeof (gboolean));
	GMainLoop *loop = g_main_loop_new (NULL, FALSE);
	SensorData data;

	/* Test information */
	g_test_summary ("Test `proximity sensor operations`");

	/* Create sensor */
	SSCSensorProximity *sensor = ssc_sensor_proximity_new_sync (NULL, &error);

	/* Connect measurement signal */
	g_signal_connect (sensor, "measurement", G_CALLBACK (proximity_measurement), measurements);

	/* Wait until all mocking measurements are received */
	data.sensor = SSC_SENSOR (sensor);
	data.measurements = measurements;
	data.loop = loop;
	g_timeout_add_seconds (TIMEOUT, (GSourceFunc)proximity_close_cb, &data);

	/* Open sensor */
	g_assert_true (ssc_sensor_proximity_open_sync (sensor, NULL, &error));

	/* Run main loop to process signals and timers */
	g_main_loop_run (loop);
}

static gboolean
light_close_cb (SensorData *data)
{
	SSCSensorLight *self = SSC_SENSOR_LIGHT (data->sensor);
	GArray *measurements = data->measurements;
	GMainLoop *loop = data->loop;
	g_autoptr (GError) error = NULL;

	/* Close sensor */
	g_assert_true (ssc_sensor_light_close_sync (self, NULL, &error));

	/* Check measurements */
	g_assert_cmpint (measurements->len, >=, 8);
	g_assert_cmpfloat (g_array_index (measurements, gfloat, 0), ==, 5.0);
	g_assert_cmpfloat (g_array_index (measurements, gfloat, 1), ==, 7.0);
	g_assert_cmpfloat (g_array_index (measurements, gfloat, 2), ==, 1.0);
	g_assert_cmpfloat (g_array_index (measurements, gfloat, 3), ==, 0.0);
	g_assert_cmpfloat (g_array_index (measurements, gfloat, 4), ==, 5.0);
	g_assert_cmpfloat (g_array_index (measurements, gfloat, 5), ==, 5.0);
	g_assert_cmpfloat (g_array_index (measurements, gfloat, 6), ==, 5.0);
	g_assert_cmpfloat (g_array_index (measurements, gfloat, 7), ==, 5.0);

	g_main_loop_quit (loop);

	return G_SOURCE_REMOVE;
}

static void
light_measurement (SSCSensorLight *sensor, gfloat intensity, gpointer user_data)
{
	GArray *measurements = user_data;

	g_test_message("Intensity: %f Lux", intensity);

	/* Collect measurement */
	g_array_append_val (measurements, intensity);
}

static void
test_libssc_sensor_light(void)
{
	g_autoptr (GError) error = NULL;
	GArray *measurements = g_array_new (FALSE, FALSE, sizeof (gfloat));
	GMainLoop *loop = g_main_loop_new (NULL, FALSE);
	SensorData data;

	/* Test information */
	g_test_summary ("Test `light sensor operations`");

	/* Create sensor */
	SSCSensorLight *sensor = ssc_sensor_light_new_sync (NULL, &error);

	/* Connect measurement signal */
	g_signal_connect (sensor, "measurement", G_CALLBACK (light_measurement), measurements);

	/* Wait until all mocking measurements are received */
	data.sensor = SSC_SENSOR (sensor);
	data.measurements = measurements;
	data.loop = loop;
	g_timeout_add_seconds (TIMEOUT, (GSourceFunc)light_close_cb, &data);

	/* Open sensor */
	g_assert_true (ssc_sensor_light_open_sync (sensor, NULL, &error));

	/* Run main loop to process signals and timers */
	g_main_loop_run (loop);
}

static gboolean
accelerometer_close_cb (SensorData *data)
{
	SSCSensorAccelerometer *self = SSC_SENSOR_ACCELEROMETER (data->sensor);
	GArray *measurements = data->measurements;
	GMainLoop *loop = data->loop;
	g_autoptr (GError) error = NULL;

	/* Close sensor */
	g_assert_true (ssc_sensor_accelerometer_close_sync (self, NULL, &error));

	/* Check measurements */
	g_assert_cmpint (measurements->len, >=, 8);

	g_assert_cmpfloat (g_array_index (measurements, XYZMeasurement, 0).x, ==, 0.0);
	g_assert_cmpfloat (g_array_index (measurements, XYZMeasurement, 0).y, ==, 0.0);
	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, XYZMeasurement, 0).z, -9.81, 0.001);

	g_assert_cmpfloat (g_array_index (measurements, XYZMeasurement, 1).x, ==, 2.5);
	g_assert_cmpfloat (g_array_index (measurements, XYZMeasurement, 1).y, ==, 1.5);
	g_assert_cmpfloat (g_array_index (measurements, XYZMeasurement, 1).z, ==, 0.0);

	g_assert_cmpfloat (g_array_index (measurements, XYZMeasurement, 2).x, ==, -2.5);
	g_assert_cmpfloat (g_array_index (measurements, XYZMeasurement, 2).y, ==, -1.5);
	g_assert_cmpfloat (g_array_index (measurements, XYZMeasurement, 2).z, ==, 0.0);

	g_assert_cmpfloat (g_array_index (measurements, XYZMeasurement, 3).x, ==, 0.0);
	g_assert_cmpfloat (g_array_index (measurements, XYZMeasurement, 3).y, ==, 0.0);
	g_assert_cmpfloat (g_array_index (measurements, XYZMeasurement, 3).z, ==, 0.0);

	g_assert_cmpfloat (g_array_index (measurements, XYZMeasurement, 4).x, ==, 0.0);
	g_assert_cmpfloat (g_array_index (measurements, XYZMeasurement, 4).y, ==, 0.0);
	g_assert_cmpfloat (g_array_index (measurements, XYZMeasurement, 4).z, ==, 0.0);

	g_assert_cmpfloat (g_array_index (measurements, XYZMeasurement, 5).x, ==, 1.0);
	g_assert_cmpfloat (g_array_index (measurements, XYZMeasurement, 5).y, ==, 1.0);
	g_assert_cmpfloat (g_array_index (measurements, XYZMeasurement, 5).z, ==, 1.0);

	g_assert_cmpfloat (g_array_index (measurements, XYZMeasurement, 6).x, ==, 1.0);
	g_assert_cmpfloat (g_array_index (measurements, XYZMeasurement, 6).z, ==, 1.0);
	g_assert_cmpfloat (g_array_index (measurements, XYZMeasurement, 6).y, ==, 1.0);

	g_assert_cmpfloat (g_array_index (measurements, XYZMeasurement, 7).x, ==, 1.0);
	g_assert_cmpfloat (g_array_index (measurements, XYZMeasurement, 7).y, ==, 1.0);
	g_assert_cmpfloat (g_array_index (measurements, XYZMeasurement, 7).z, ==, 1.0);

	g_main_loop_quit (loop);

	return G_SOURCE_REMOVE;
}

static void
accelerometer_measurement (SSCSensorAccelerometer *sensor, gfloat accel_x, gfloat accel_y, gfloat accel_z, gpointer user_data)
{
	GArray *measurements = user_data;
	XYZMeasurement measurement;

	g_test_message("Acceleration: X%f Y%f Z%f", accel_x, accel_y, accel_z);
	measurement.x = accel_x;
	measurement.y = accel_y;
	measurement.z = accel_z;

	/* Collect measurement */
	g_array_append_val (measurements, measurement);
}

static void
test_libssc_sensor_accelerometer(void)
{
	g_autoptr (GError) error = NULL;
	GArray *measurements = g_array_new (FALSE, FALSE, sizeof (XYZMeasurement));
	GMainLoop *loop = g_main_loop_new (NULL, FALSE);
	SensorData data;

	/* Test information */
	g_test_summary ("Test `accelerometer sensor operations`");

	/* Create sensor */
	SSCSensorAccelerometer *sensor = ssc_sensor_accelerometer_new_sync (NULL, &error);

	/* Connect measurement signal */
	g_signal_connect (sensor, "measurement", G_CALLBACK (accelerometer_measurement), measurements);

	/* Wait until all mocking measurements are received */
	data.sensor = SSC_SENSOR (sensor);
	data.measurements = measurements;
	data.loop = loop;
	g_timeout_add_seconds (TIMEOUT, (GSourceFunc)accelerometer_close_cb, &data);

	/* Open sensor */
	g_assert_true (ssc_sensor_accelerometer_open_sync (sensor, NULL, &error));

	/* Run main loop to process signals and timers */
	g_main_loop_run (loop);
}

static gboolean
compass_close_cb (SensorData *data)
{
	SSCSensorCompass *self = SSC_SENSOR_COMPASS (data->sensor);
	GArray *measurements = data->measurements;
	GMainLoop *loop = data->loop;
	g_autoptr (GError) error = NULL;

	/* Close sensor */
	g_assert_true (ssc_sensor_compass_close_sync (self, NULL, &error));

	/* Check measurements */
	g_assert_cmpint (measurements->len, >=, 8);
	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, gfloat, 0), 226.468811, 0.1);
	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, gfloat, 1), 113.62372, 0.1);
	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, gfloat, 2), 0.0, 0.1);
	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, gfloat, 3), 226.468811, 0.1);
	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, gfloat, 4), 226.468811, 0.1);
	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, gfloat, 5), 226.468811, 0.1);
	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, gfloat, 6), 226.468811, 0.1);
	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, gfloat, 7), 226.468811, 0.1);

	g_main_loop_quit (loop);

	return G_SOURCE_REMOVE;
}

static void
compass_measurement (SSCSensorCompass *sensor, gfloat heading, gpointer user_data)
{
	GArray *measurements = user_data;

	g_test_message("Compass: %f degrees", heading);

	/* Collect measurement */
	g_array_append_val (measurements, heading);
}

static void
test_libssc_sensor_compass(void)
{
	g_autoptr (GError) error = NULL;
	GArray *measurements = g_array_new (FALSE, FALSE, sizeof (gfloat));
	GMainLoop *loop = g_main_loop_new (NULL, FALSE);
	SensorData data;

	/* Test information */
	g_test_summary ("Test `compass sensor operations`");

	/* Create sensor */
	SSCSensorCompass *sensor = ssc_sensor_compass_new_sync (NULL, &error);

	/* Connect measurement signal */
	g_signal_connect (sensor, "measurement", G_CALLBACK (compass_measurement), measurements);

	/* Wait until all mocking measurements are received */
	data.sensor = SSC_SENSOR (sensor);
	data.measurements = measurements;
	data.loop = loop;
	g_timeout_add_seconds (TIMEOUT, (GSourceFunc)compass_close_cb, &data);

	/* Open sensor */
	g_assert_true (ssc_sensor_compass_open_sync (sensor, NULL, &error));

	/* Run main loop to process signals and timers */
	g_main_loop_run (loop);
}

static gboolean
magnetometer_close_cb (SensorData *data)
{
	SSCSensorMagnetometer *self = SSC_SENSOR_MAGNETOMETER (data->sensor);
	GArray *measurements = data->measurements;
	GMainLoop *loop = data->loop;
	g_autoptr (GError) error = NULL;

	/* Close sensor */
	g_assert_true (ssc_sensor_magnetometer_close_sync (self, NULL, &error));

	/* Check measurements */
	g_assert_cmpint (measurements->len, >=, 8);

	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, XYZMeasurement, 0).x, 0.1, 0.01);
	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, XYZMeasurement, 0).y, 0.2, 0.01);
	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, XYZMeasurement, 0).z, 0.3, 0.01);

	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, XYZMeasurement, 1).x, 0.3, 0.01);
	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, XYZMeasurement, 1).y, 0.2, 0.01);
	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, XYZMeasurement, 1).z, 0.1, 0.01);

	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, XYZMeasurement, 2).x, 0.3, 0.01);
	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, XYZMeasurement, 2).y, 0.2, 0.01);
	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, XYZMeasurement, 2).z, 0.1, 0.01);

	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, XYZMeasurement, 3).x, 0.0, 0.01);
	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, XYZMeasurement, 3).y, 0.0, 0.01);
	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, XYZMeasurement, 3).z, 0.0, 0.01);

	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, XYZMeasurement, 4).x, 0.0, 0.01);
	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, XYZMeasurement, 4).y, 0.0, 0.01);
	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, XYZMeasurement, 4).z, 0.0, 0.01);

	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, XYZMeasurement, 5).x, 1.0, 0.01);
	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, XYZMeasurement, 5).y, 1.0, 0.01);
	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, XYZMeasurement, 5).z, 1.0, 0.01);

	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, XYZMeasurement, 6).x, 1.0, 0.01);
	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, XYZMeasurement, 6).y, 1.0, 0.01);
	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, XYZMeasurement, 6).z, 1.0, 0.01);

	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, XYZMeasurement, 7).x, 1.0, 0.01);
	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, XYZMeasurement, 7).y, 1.0, 0.01);
	g_assert_cmpfloat_with_epsilon (g_array_index (measurements, XYZMeasurement, 7).z, 1.0, 0.01);

	g_main_loop_quit (loop);

	return G_SOURCE_REMOVE;
}

static void
magnetometer_measurement (SSCSensorMagnetometer *sensor, gfloat magn_x, gfloat magn_y, gfloat magn_z, gpointer user_data)
{
	GArray *measurements = user_data;
	XYZMeasurement measurement;

	g_test_message("Magnetic field: X%f Y%f Z%f", magn_x, magn_y, magn_z);
	measurement.x = magn_x;
	measurement.y = magn_y;
	measurement.z = magn_z;

	/* Collect measurement */
	g_array_append_val (measurements, measurement);
}

static void
test_libssc_sensor_magnetometer(void)
{
	g_autoptr (GError) error = NULL;
	GArray *measurements = g_array_new (FALSE, FALSE, sizeof (XYZMeasurement));
	GMainLoop *loop = g_main_loop_new (NULL, FALSE);
	SensorData data;

	/* Test information */
	g_test_summary ("Test `magnetometer sensor operations`");

	/* Create sensor */
	SSCSensorMagnetometer *sensor = ssc_sensor_magnetometer_new_sync (NULL, &error);

	/* Connect measurement signal */
	g_signal_connect (sensor, "measurement", G_CALLBACK (magnetometer_measurement), measurements);

	data.sensor = SSC_SENSOR (sensor);
	data.measurements = measurements;
	data.loop = loop;
	g_timeout_add_seconds (TIMEOUT, (GSourceFunc)magnetometer_close_cb, &data);

	/* Open sensor */
	g_assert_true (ssc_sensor_magnetometer_open_sync (sensor, NULL, &error));

	/* Run main loop to process signals and timers */
	g_main_loop_run (loop);
}

static void
sensor_unavailable_open_ready (SSCSensor *self, GAsyncResult *result, gpointer user_data)
{
	GError *error = NULL;
	GMainLoop *loop = user_data;
	gboolean success = FALSE;

	success = ssc_sensor_open_finish (self, result, &error);
	g_assert_false (success);

	g_main_loop_quit (loop);
}

static void
sensor_unavailable_ready (SSCClient *self, GAsyncResult *result, gpointer user_data)
{
	GError *error = NULL;
	SSCSensor *sensor = NULL;

	sensor = ssc_sensor_new_finish (result, &error);
	g_assert_no_error (error);
	ssc_sensor_open (sensor, NULL, (GAsyncReadyCallback) sensor_unavailable_open_ready, user_data);
}

static void
test_libssc_sensor_unavailable (void)
{
	GMainLoop *loop = g_main_loop_new (NULL, FALSE);

	/* Create a sensor which is unavailable according to attribute */
	ssc_sensor_new ("unavailable", NULL, (GAsyncReadyCallback) sensor_unavailable_ready, loop);

	/* Run main loop to process signals and timers */
	g_main_loop_run (loop);
}

static void
sensor_unsupported_ready (SSCClient *self, GAsyncResult *result, gpointer user_data)
{
	GMainLoop *loop = user_data;
	GError *error = NULL;
	SSCSensor *sensor = NULL;

	sensor = ssc_sensor_new_finish (result, &error);
	g_assert_true (sensor == NULL);

	g_main_loop_quit (loop);
}

static void
test_libssc_sensor_unsupported (void)
{
	GMainLoop *loop = g_main_loop_new (NULL, FALSE);

	/* Discover a sensor which is unsupported by DSP */
	ssc_sensor_new ("unsupported", NULL, (GAsyncReadyCallback) sensor_unsupported_ready, loop);

	/* Run main loop to process signals and timers */
	g_main_loop_run (loop);
}

static void
sensor_no_sample_rate_open_ready (SSCSensor *self, GAsyncResult *result, gpointer user_data)
{
	GError *error = NULL;
	GMainLoop *loop = user_data;
	gboolean success = FALSE;

	success = ssc_sensor_open_finish (self, result, &error);
	g_assert_false (success);

	g_main_loop_quit (loop);
}

static void
sensor_no_sample_rate_ready (SSCClient *self, GAsyncResult *result, gpointer user_data)
{
	GError *error = NULL;
	SSCSensor *sensor = NULL;

	sensor = ssc_sensor_new_finish (result, &error);
	g_assert_no_error (error);
	ssc_sensor_open (sensor, NULL, (GAsyncReadyCallback) sensor_no_sample_rate_open_ready, user_data);
}

static void
test_libssc_sensor_no_sample_rate (void)
{
	GMainLoop *loop = g_main_loop_new (NULL, FALSE);

	/* Discover a sensor in continuous mode with missing required sample rate */
	ssc_sensor_new ("no-sample-rate", NULL, (GAsyncReadyCallback) sensor_no_sample_rate_ready, loop);

	/* Run main loop to process signals and timers */
	g_main_loop_run (loop);
}

static void
test_libssc_sensor_proximity_probe_sync (void)
{
	g_autoptr (GError) error = NULL;

	/* Test information */
	g_test_summary ("Test `probing proximity sensor with open_sync and close_sync`");

	/* Create sensor */
	SSCSensorProximity *sensor = ssc_sensor_proximity_new_sync (NULL, &error);

	/* Open sensor */
	g_assert_true (ssc_sensor_proximity_open_sync (sensor, NULL, &error));

	/* Close sensor */
	g_assert_true (ssc_sensor_proximity_close_sync (sensor, NULL, &error));
}

static void
test_libssc_sensor_accelerometer_probe_sync (void)
{
	g_autoptr (GError) error = NULL;

	/* Test information */
	g_test_summary ("Test `probing accelerometer sensor with open_sync and close_sync`");

	/* Create sensor */
	SSCSensorAccelerometer *sensor = ssc_sensor_accelerometer_new_sync (NULL, &error);

	/* Open sensor */
	g_assert_true (ssc_sensor_accelerometer_open_sync (sensor, NULL, &error));

	/* Close sensor */
	g_assert_true (ssc_sensor_accelerometer_close_sync (sensor, NULL, &error));
}

static void
test_libssc_sensor_light_probe_sync (void)
{
	g_autoptr (GError) error = NULL;

	/* Test information */
	g_test_summary ("Test `probing light sensor with open_sync and close_sync`");

	/* Create sensor */
	SSCSensorLight *sensor = ssc_sensor_light_new_sync (NULL, &error);

	/* Open sensor */
	g_assert_true (ssc_sensor_light_open_sync (sensor, NULL, &error));

	/* Close sensor */
	g_assert_true (ssc_sensor_light_close_sync (sensor, NULL, &error));
}

static void
test_libssc_sensor_magnetometer_probe_sync (void)
{
	g_autoptr (GError) error = NULL;

	/* Test information */
	g_test_summary ("Test `probing magnetometer sensor with open_sync and close_sync`");

	/* Create sensor */
	SSCSensorMagnetometer *sensor = ssc_sensor_magnetometer_new_sync (NULL, &error);

	/* Open sensor */
	g_assert_true (ssc_sensor_magnetometer_open_sync (sensor, NULL, &error));

	/* Close sensor */
	g_assert_true (ssc_sensor_magnetometer_close_sync (sensor, NULL, &error));
}

static void
test_libssc_sensor_compass_probe_sync (void)
{
	g_autoptr (GError) error = NULL;

	/* Test information */
	g_test_summary ("Test `probing compass sensor with open_sync and close_sync`");

	/* Create sensor */
	SSCSensorCompass *sensor = ssc_sensor_compass_new_sync (NULL, &error);

	/* Open sensor */
	g_assert_true (ssc_sensor_compass_open_sync (sensor, NULL, &error));

	/* Close sensor */
	g_assert_true (ssc_sensor_compass_close_sync (sensor, NULL, &error));
}

int main (int argc, char *argv[])
{
	GLogLevelFlags mask;

	setlocale (LC_ALL, "");

	/* Initialize test framework */
	g_test_init (&argc, &argv, NULL);

	/* Allow warnings */
	mask = (GLogLevelFlags) g_log_set_always_fatal ((GLogLevelFlags) G_LOG_FATAL_MASK);
	mask = (GLogLevelFlags) (mask & (~G_LOG_LEVEL_WARNING));
	g_log_set_always_fatal ((GLogLevelFlags) mask);

	/* Tests */
	g_test_add_func("/libssc/sensor/proximity/measurements", test_libssc_sensor_proximity);
	g_test_add_func("/libssc/sensor/proximity/probe-sync", test_libssc_sensor_proximity_probe_sync);
	g_test_add_func("/libssc/sensor/light/measurements", test_libssc_sensor_light);
	g_test_add_func("/libssc/sensor/light/probe-sync", test_libssc_sensor_light_probe_sync);
	g_test_add_func("/libssc/sensor/accelerometer/measurements", test_libssc_sensor_accelerometer);
	g_test_add_func("/libssc/sensor/accelerometer/probe-sync", test_libssc_sensor_accelerometer_probe_sync);
	g_test_add_func("/libssc/sensor/compass/measurements", test_libssc_sensor_compass);
	g_test_add_func("/libssc/sensor/compass/probe-sync", test_libssc_sensor_compass_probe_sync);
	g_test_add_func("/libssc/sensor/magnetometer/measurements", test_libssc_sensor_magnetometer);
	g_test_add_func("/libssc/sensor/magnetometer/probe-sync", test_libssc_sensor_magnetometer_probe_sync);
	g_test_add_func("/libssc/sensor/unsupported", test_libssc_sensor_unsupported);
	g_test_add_func("/libssc/sensor/unavailable", test_libssc_sensor_unavailable);
	g_test_add_func("/libssc/sensor/no-sample-rate", test_libssc_sensor_no_sample_rate);

	/* Execute tests */
	return g_test_run();
}