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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
|
//Copyright (C) Microsoft Corporation, All Rights Reserved.
//
//Abstract:
//
// This module contains the implementation of driver callback function
// from clx to pedometer.
//
//Environment:
//
// Windows User-Mode Driver Framework (UMDF)
#include "Device.h"
#include "HardwareSimulator.h"
#include <timeapi.h>
#include <Intsafe.h>
#include "Client.tmh"
// This routine is called by worker thread to read a single sample, compare threshold
// and push it back to CLX. It simulates hardware thresholding by only generating data
// when the change of data is greater than threshold.
NTSTATUS
PedometerDevice::GetData(
)
{
PHardwareSimulator pSimulator = GetHardwareSimulatorContextFromInstance(m_SimulatorInstance);
BOOLEAN DataReady = FALSE;
NTSTATUS Status = STATUS_SUCCESS;
ULONG CachedStepCountLimit = 0;
ULONG LastStepCountLimit = 0;
PedometerSample Sample = {};
SENSOR_FunctionEnter();
if (nullptr == pSimulator)
{
Status = STATUS_INSUFFICIENT_RESOURCES;
TraceError("PED %!FUNC! GetHardwareSimulatorContextFromInstance failed %!STATUS!", Status);
goto Exit;
}
Status = pSimulator->GetSample(&Sample);
if (!NT_SUCCESS(Status))
{
TraceError("PED %!FUNC! GetSample failed %!STATUS!", Status);
goto Exit;
}
if (FALSE != m_FirstSample)
{
Status = GetPerformanceTime(&m_StartTime);
if (!NT_SUCCESS(Status))
{
m_StartTime = 0;
TraceError("PED %!FUNC! GetPerformanceTime failed %!STATUS!", Status);
}
m_SampleCount = 0;
DataReady = TRUE;
}
else
{
if (0 == m_CachedThreshold || FALSE != Sample.IsFirstAfterReset)
{
// Streaming mode
DataReady = TRUE;
}
else
{
if (FAILED(ULongAdd(Sample.UnknownStepCount, Sample.WalkingStepCount, &CachedStepCountLimit)) ||
FAILED(ULongAdd(Sample.RunningStepCount, CachedStepCountLimit, &CachedStepCountLimit)))
{
// If an overflow happened, we assume we reached the threshold
// in other words, there is no threshold value that can be larger
// than an overflowed value.
DataReady = TRUE;
}
else if (FAILED(ULongAdd(m_LastSample.UnknownStepCount, m_LastSample.WalkingStepCount, &LastStepCountLimit)) ||
FAILED(ULongAdd(m_LastSample.RunningStepCount, LastStepCountLimit, &LastStepCountLimit)))
{
// If an overflow happened, we assume we reached the threshold
// in other words, there is no threshold value that can be larger
// than an overflowed value.
DataReady = TRUE;
}
else if ((LastStepCountLimit < m_CachedThreshold && CachedStepCountLimit >= m_CachedThreshold) ||
(FALSE != Sample.IsFirstAfterReset))
{
// Compare the change of data to threshold, and only push the data back to
// clx if the change exceeds threshold or if this is the first sample after reset. This is usually done in HW.
DataReady = TRUE;
}
}
}
if (FALSE != DataReady)
{
// update last sample
m_LastSample = Sample;
// push to clx
InitPropVariantFromBoolean(m_LastSample.IsFirstAfterReset, &(m_pData->List[PEDOMETER_DATA_FIRST_AFTER_RESET].Value));
InitPropVariantFromUInt32(PedometerStepType_Unknown, &(m_pData->List[PEDOMETER_DATA_UNKNOWN_STEP_TYPE].Value));
InitPropVariantFromInt64(m_LastSample.UnknownStepDurationMs, &(m_pData->List[PEDOMETER_DATA_UNKNOWN_STEP_DURATION].Value));
InitPropVariantFromUInt32(m_LastSample.UnknownStepCount, &(m_pData->List[PEDOMETER_DATA_UNKNOWN_STEP_COUNT].Value));
InitPropVariantFromUInt32(PedometerStepType_Walking, &(m_pData->List[PEDOMETER_DATA_WALKING_STEP_TYPE].Value));
InitPropVariantFromInt64(m_LastSample.WalkingStepDurationMs, &(m_pData->List[PEDOMETER_DATA_WALKING_STEP_DURATION].Value));
InitPropVariantFromUInt32(m_LastSample.WalkingStepCount, &(m_pData->List[PEDOMETER_DATA_WALKING_STEP_COUNT].Value));
InitPropVariantFromUInt32(PedometerStepType_Running, &(m_pData->List[PEDOMETER_DATA_RUNNING_STEP_TYPE].Value));
InitPropVariantFromInt64(m_LastSample.RunningStepDurationMs, &(m_pData->List[PEDOMETER_DATA_RUNNING_STEP_DURATION].Value));
InitPropVariantFromUInt32(m_LastSample.RunningStepCount, &(m_pData->List[PEDOMETER_DATA_RUNNING_STEP_COUNT].Value));
// reset IsFirstAfterReset
m_LastSample.IsFirstAfterReset = FALSE;
InitPropVariantFromFileTime(&m_LastSample.Timestamp, &(m_pData->List[PEDOMETER_DATA_TIMESTAMP].Value));
SensorsCxSensorDataReady(m_SensorInstance, m_pData);
m_FirstSample = FALSE;
}
else
{
Status = STATUS_DATA_NOT_ACCEPTED;
TraceInformation("PED %!FUNC! Data did NOT meet the threshold");
}
SENSOR_FunctionExit(Status);
Exit:
return Status;
}
// This callback is called when interval wait time has expired and driver is ready
// to collect new sample. The callback reads current value, compare value to threshold,
// pushes it up to CLX framework, and schedule next wake up time.
VOID
PedometerDevice::OnTimerExpire(
_In_ WDFTIMER Timer // WDF timer object
)
{
PPedometerDevice pDevice = nullptr;
NTSTATUS Status = STATUS_SUCCESS;
SENSOR_FunctionEnter();
pDevice = GetPedometerContextFromSensorInstance(WdfTimerGetParentObject(Timer));
if (nullptr == pDevice)
{
Status = STATUS_INSUFFICIENT_RESOURCES;
TraceError("PED %!FUNC! GetPedometerContextFromSensorInstance failed %!STATUS!", Status);
goto Exit;
}
// Get data and push to clx
WdfWaitLockAcquire(pDevice->m_Lock, NULL);
Status = pDevice->GetData();
if (!NT_SUCCESS(Status) && Status != STATUS_DATA_NOT_ACCEPTED)
{
TraceError("PED %!FUNC! GetData Failed %!STATUS!", Status);
}
WdfWaitLockRelease(pDevice->m_Lock);
// Schedule next wake up time
if (FALSE != pDevice->m_PoweredOn &&
FALSE != pDevice->m_Started)
{
LONGLONG WaitTimeHundredNanoseconds = 0; // in unit of 100ns
if (0 == pDevice->m_StartTime)
{
// in case we fail to get sensor start time, use static wait time
WaitTimeHundredNanoseconds = WDF_REL_TIMEOUT_IN_MS(pDevice->m_Interval);
}
else
{
ULONG CurrentTimeMs = 0;
// dynamically calculate wait time to avoid jitter
Status = GetPerformanceTime (&CurrentTimeMs);
if (!NT_SUCCESS(Status))
{
TraceError("PED %!FUNC! GetPerformanceTime %!STATUS!", Status);
WaitTimeHundredNanoseconds = WDF_REL_TIMEOUT_IN_MS(pDevice->m_Interval);
}
else
{
pDevice->m_SampleCount++;
if (CurrentTimeMs > (pDevice->m_StartTime + (pDevice->m_Interval * (pDevice->m_SampleCount + 1))))
{
// If we skipped two or more beats, reschedule the timer with a zero due time to catch up on missing samples
WaitTimeHundredNanoseconds = 0;
}
else
{
WaitTimeHundredNanoseconds = (pDevice->m_StartTime +
(pDevice->m_Interval * (pDevice->m_SampleCount + 1))) - CurrentTimeMs;
}
WaitTimeHundredNanoseconds = WDF_REL_TIMEOUT_IN_MS(WaitTimeHundredNanoseconds);
}
}
WdfTimerStart(pDevice->m_Timer, WaitTimeHundredNanoseconds);
}
Exit:
SENSOR_FunctionExit(Status);
}
// Called by Sensor CLX to begin continuously sampling the sensor.
NTSTATUS
PedometerDevice::OnStart(
_In_ SENSOROBJECT SensorInstance // Sensor device object
)
{
PHardwareSimulator pSimulator = nullptr;
PPedometerDevice pDevice = GetPedometerContextFromSensorInstance(SensorInstance);
NTSTATUS Status = STATUS_SUCCESS;
SENSOR_FunctionEnter();
if (nullptr == pDevice)
{
Status = STATUS_INVALID_PARAMETER;
TraceError("PED %!FUNC! Sensor(0x%p) parameter is invalid. Failed %!STATUS!", SensorInstance, Status);
goto Exit;
}
// Get the simulator context
pSimulator = GetHardwareSimulatorContextFromInstance(pDevice->m_SimulatorInstance);
if (nullptr == pSimulator)
{
Status = STATUS_INSUFFICIENT_RESOURCES;
TraceError("PED %!FUNC! GetHardwareSimulatorContextFromInstance failed %!STATUS!", Status);
}
if (NT_SUCCESS(Status))
{
// Start the simulator
pSimulator->Start();
pDevice->m_FirstSample = TRUE;
// Start polling
pDevice->m_Started = TRUE;
InitPropVariantFromUInt32(SensorState_Active,
&(pDevice->m_pProperties->List[SENSOR_PROPERTY_STATE].Value));
// Start the sample polling timer.
//
// Note: The polling timer is configured to allow for the first sample to be reported immediately.
// Some hardware may want to delay the first sample report a little to account for hardware start time.
WdfTimerStart(pDevice->m_Timer, WDF_REL_TIMEOUT_IN_MS(Pedometer_Default_MinDataInterval_Ms));
}
Exit:
SENSOR_FunctionExit(Status);
return Status;
}
// Called by Sensor CLX to stop continuously sampling the sensor.
NTSTATUS
PedometerDevice::OnStop(
_In_ SENSOROBJECT SensorInstance // Sensor device object
)
{
PHardwareSimulator pSimulator = nullptr;
PPedometerDevice pDevice = GetPedometerContextFromSensorInstance(SensorInstance);
NTSTATUS Status = STATUS_SUCCESS;
SENSOR_FunctionEnter();
if (nullptr == pDevice)
{
Status = STATUS_INVALID_PARAMETER;
TraceError("PED %!FUNC! Sensor(0x%p) parameter is invalid. Failed %!STATUS!", SensorInstance, Status);
goto Exit;
}
// Stop polling
pDevice->m_Started = FALSE;
// Waiting for the callback to complete, then stopping the timer
WdfTimerStop(pDevice->m_Timer, TRUE);
InitPropVariantFromUInt32(SensorState_Idle,
&(pDevice->m_pProperties->List[SENSOR_PROPERTY_STATE].Value));
// Stop the simulator
pSimulator = GetHardwareSimulatorContextFromInstance(pDevice->m_SimulatorInstance);
if (nullptr == pSimulator)
{
Status = STATUS_INSUFFICIENT_RESOURCES;
TraceError("PED %!FUNC! GetHardwareSimulatorContextFromInstance failed %!STATUS!", Status);
goto Exit;
}
pSimulator->Stop();
Exit:
SENSOR_FunctionExit(Status);
return Status;
}
// Called by Sensor CLX to begin keeping history
NTSTATUS
PedometerDevice::OnStartHistory(
_In_ SENSOROBJECT SensorInstance // Sensor device object
)
{
PPedometerDevice pDevice = GetPedometerContextFromSensorInstance(SensorInstance);
NTSTATUS Status = STATUS_SUCCESS;
PHardwareSimulator pSimulator = nullptr;
SENSOR_FunctionEnter();
if (nullptr == pDevice)
{
Status = STATUS_INVALID_PARAMETER;
TraceError("PED %!FUNC! Sensor(0x%p) parameter is invalid. Failed %!STATUS!", SensorInstance, Status);
goto Exit;
}
if (FALSE == pDevice->m_HistorySupported)
{
Status = STATUS_NOT_SUPPORTED;
TraceError("PED %!FUNC! History is not supported by the HW");
goto Exit;
}
if (FALSE == pDevice->m_PoweredOn)
{
Status = STATUS_DEVICE_NOT_READY;
TraceError("PED %!FUNC! Sensor is not powered on! %!STATUS!", Status);
goto Exit;
}
pSimulator = GetHardwareSimulatorContextFromInstance(pDevice->m_SimulatorInstance);
if (nullptr == pSimulator)
{
Status = STATUS_INSUFFICIENT_RESOURCES;
TraceError("PED %!FUNC! GetHardwareSimulatorContextFromInstance failed %!STATUS!", Status);
goto Exit;
}
// Start the pedometer history
Status = pSimulator->StartHistory();
if (!NT_SUCCESS(Status))
{
TraceError("PED %!FUNC! Start History failed %!STATUS!", Status);
}
Exit:
SENSOR_FunctionExit(Status);
return Status;
}
// Called by Sensor CLX to stop keeping history.
NTSTATUS
PedometerDevice::OnStopHistory(
_In_ SENSOROBJECT SensorInstance // Sensor device object
)
{
PPedometerDevice pDevice = GetPedometerContextFromSensorInstance(SensorInstance);
NTSTATUS Status = STATUS_SUCCESS;
PHardwareSimulator pSimulator = nullptr;
SENSOR_FunctionEnter();
if (nullptr == pDevice)
{
Status = STATUS_INVALID_PARAMETER;
TraceError("PED %!FUNC! Sensor(0x%p) parameter is invalid. Failed %!STATUS!", SensorInstance, Status);
goto Exit;
}
if (FALSE == pDevice->m_HistorySupported)
{
Status = STATUS_NOT_SUPPORTED;
TraceError("PED %!FUNC! History is not supported by the HW");
goto Exit;
}
pSimulator = GetHardwareSimulatorContextFromInstance(pDevice->m_SimulatorInstance);
if (nullptr == pSimulator)
{
Status = STATUS_INSUFFICIENT_RESOURCES;
TraceError("PED %!FUNC! GetHardwareSimulatorContextFromInstance failed %!STATUS!", Status);
goto Exit;
}
// Stop the pedometer history
Status = pSimulator->StopHistory();
if (!NT_SUCCESS(Status))
{
TraceError("PED %!FUNC! Stop History failed %!STATUS!", Status);
}
Exit:
SENSOR_FunctionExit(Status);
return Status;
}
// Resets the pedometer to its initial values.
VOID
PedometerDevice::ResetPedometer()
{
NTSTATUS Status = STATUS_SUCCESS;
PHardwareSimulator pSimulator = GetHardwareSimulatorContextFromInstance(m_SimulatorInstance);
SENSOR_FunctionEnter();
if (nullptr == pSimulator)
{
Status = STATUS_INSUFFICIENT_RESOURCES;
TraceError("PED %!FUNC! GetHardwareSimulatorContextFromInstance failed %!STATUS!", Status);
}
else
{
// Reset the pedometer
pSimulator->Reset();
}
SENSOR_FunctionExit(Status);
}
// Called by Sensor CLX to clear all history stored in the sensor.
NTSTATUS
PedometerDevice::OnClearHistory(
_In_ SENSOROBJECT SensorInstance // Sensor device object
)
{
PPedometerDevice pDevice = GetPedometerContextFromSensorInstance(SensorInstance);
NTSTATUS Status = STATUS_SUCCESS;
PHardwareSimulator pSimulator;
SENSOR_FunctionEnter();
if (nullptr == pDevice)
{
Status = STATUS_INVALID_PARAMETER;
TraceError("PED %!FUNC! Sensor(0x%p) parameter is invalid. Failed %!STATUS!", SensorInstance, Status);
goto Exit;
}
if (FALSE == pDevice->m_HistorySupported)
{
Status = STATUS_NOT_SUPPORTED;
TraceError("PED %!FUNC! History is not supported by the HW");
goto Exit;
}
pSimulator = GetHardwareSimulatorContextFromInstance(pDevice->m_SimulatorInstance);
if (nullptr == pSimulator)
{
Status = STATUS_INSUFFICIENT_RESOURCES;
TraceError("PED %!FUNC! GetHardwareSimulatorContextFromInstance failed %!STATUS!", Status);
goto Exit;
}
// Clear the pedometer history
Status = pSimulator->ClearHistory();
if (!NT_SUCCESS(Status))
{
TraceError("PED %!FUNC! Clear History failed %!STATUS!", Status);
}
Exit:
SENSOR_FunctionExit(Status);
return Status;
}
// Called by Sensor CLX to start retrieving history.
//
// Arguments:
// SensorInstance: IN:
//
// Return Value:
// NTSTATUS code
//------------------------------------------------------------------------------
NTSTATUS
PedometerDevice::OnStartHistoryRetrieval(
_In_ SENSOROBJECT SensorInstance, // sensor device object
_Inout_updates_bytes_(HistorySizeInBytes) PSENSOR_COLLECTION_LIST pHistoryBuffer, // Pointer to a buffer containing the history elements
_In_ ULONG HistorySizeInBytes // Size of the pHistoryBuffer buffer in bytes
)
{
PPedometerDevice pDevice = GetPedometerContextFromSensorInstance(SensorInstance);
NTSTATUS Status = STATUS_SUCCESS;
BOOLEAN IsLocked = FALSE;
SENSOR_FunctionEnter();
if (nullptr == pDevice)
{
Status = STATUS_INVALID_PARAMETER;
TraceError("PED %!FUNC! Sensor(0x%p) parameter is invalid. Failed %!STATUS!", SensorInstance, Status);
goto Exit;
}
if (FALSE == pDevice->m_HistorySupported)
{
Status = STATUS_NOT_SUPPORTED;
TraceError("PED %!FUNC! History is not supported by the HW");
goto Exit;
}
// Check if the previously created history retrieval thread finished.
// When StartHistoryRetrieval is called again before we signal to CLX that history retrieval is
// completed, fail the second call.
WdfWaitLockAcquire(pDevice->m_Lock, NULL);
BOOLEAN IsStarted = pDevice->m_HistoryRetrievalStarted;
WdfWaitLockRelease(pDevice->m_Lock);
if (FALSE != IsStarted)
{
Status = STATUS_DEVICE_BUSY;
TraceError("PED %!FUNC! StartHistoryRetrieval called again before the previous call"
"finishes retrieving history %!STATUS!", Status);
goto Exit;
}
// check that the buffer is not null
if (NULL == pHistoryBuffer)
{
Status = STATUS_INVALID_PARAMETER;
TraceError("PED %!FUNC! History buffer cannot be NULL. %!STATUS!", Status);
goto Exit;
}
// check that the buffer can hold at least one record
if (HistorySizeInBytes < pDevice->m_HistoryMarshalledRecordSize)
{
Status = STATUS_BUFFER_TOO_SMALL;
TraceError("PED %!FUNC! History buffer is too small to even fill one complete entry. %!STATUS!", Status);
goto Exit;
}
// Before creating a new thread, close handle to the previously created thread.
if (NULL != pDevice->m_hThread)
{
CloseHandle(pDevice->m_hThread);
pDevice->m_hThread = NULL;
}
pDevice->m_ClientHistoryBuffer = pHistoryBuffer;
pDevice->m_ClientHistoryBufferSize = HistorySizeInBytes;
WdfWaitLockAcquire(pDevice->m_Lock, NULL);
IsLocked = TRUE;
// Create a new thread to retrieve data from the driver's history buffer
pDevice->m_hThread = CreateThread(NULL, // thread attributes
0, // default stack size
HistoryRetrievalThread, //start address
reinterpret_cast<void*>(SensorInstance), // pointer to variable to be passed
0, // run immediately
NULL); // Thread ID
if (NULL == pDevice->m_hThread)
{
Status = STATUS_UNSUCCESSFUL;
TraceError("PED %!FUNC! CreateThread Failed %!STATUS!", Status);
goto Exit;
}
pDevice->m_HistoryRetrievalStarted = TRUE;
WdfWaitLockRelease(pDevice->m_Lock);
IsLocked = FALSE;
Exit:
SENSOR_FunctionExit(Status);
if (nullptr != pDevice)
{
if (FALSE != IsLocked)
{
WdfWaitLockRelease(pDevice->m_Lock);
}
if (!NT_SUCCESS(Status) && NULL != pDevice->m_hThread)
{
CloseHandle(pDevice->m_hThread);
pDevice->m_hThread = NULL;
}
}
return Status;
}
// Called by Sensor CLX to cancel history retrieval.
NTSTATUS
PedometerDevice::OnCancelHistoryRetrieval(
_In_ SENSOROBJECT SensorInstance, // sensor device object
_Out_ PULONG pBytesWritten // Upon exit, contains the number of bytes written to the history buffer
)
{
PPedometerDevice pDevice = GetPedometerContextFromSensorInstance(SensorInstance);
NTSTATUS Status = STATUS_SUCCESS;
PHardwareSimulator pSimulator = nullptr;
SENSOR_FunctionEnter();
if (nullptr == pDevice || nullptr == pBytesWritten)
{
Status = STATUS_INVALID_PARAMETER;
TraceError("PED %!FUNC! Invalid Parameters: Sensor(0x%p), pDevice (0x%p), pBytesWritten (0x%p). Failed %!STATUS!", SensorInstance, pDevice, pBytesWritten, Status);
goto Exit;
}
if (FALSE == pDevice->m_HistorySupported)
{
Status = STATUS_NOT_SUPPORTED;
TraceError("PED %!FUNC! History is not supported by the HW");
goto Exit;
}
*pBytesWritten = 0;
// Check if history retrieval operation is in progress
WdfWaitLockAcquire(pDevice->m_Lock, NULL);
BOOLEAN IsStarted = pDevice->m_HistoryRetrievalStarted;
WdfWaitLockRelease(pDevice->m_Lock);
if (FALSE == IsStarted)
{
Status = STATUS_INVALID_DEVICE_REQUEST;
TraceError("PED %!FUNC! History retrieval is not in progress %!STATUS!", Status);
goto Exit;
}
// Get the simulator context
pSimulator = GetHardwareSimulatorContextFromInstance(pDevice->m_SimulatorInstance);
if (nullptr == pSimulator)
{
Status = STATUS_INSUFFICIENT_RESOURCES;
TraceError("PED %!FUNC! GetHardwareSimulatorContextFromInstance failed %!STATUS!", Status);
}
Status = pSimulator->SignalReadCancellation();
if (!NT_SUCCESS(Status))
{
TraceError("PED %!FUNC! Failed to cancel history retrieval from HW %!STATUS!", Status);
}
// Wait for the history retrieval thread to finish writing the entry since no partial entries are allowed.
DWORD result = WaitForSingleObjectEx(pDevice->m_hThread, Pedometer_TimeoutForHistoryThread_Ms, FALSE);
if (WAIT_OBJECT_0 != result)
{
TraceError("PED %!FUNC! WaitForSingleObjectEx failed with error %d", result);
Status = STATUS_DEVICE_BUSY;
goto Exit;
}
*pBytesWritten = CollectionsListGetMarshalledSizeWithoutSerialization(pDevice->m_ClientHistoryBuffer);
Exit:
SENSOR_FunctionExit(Status);
return Status;
}
// Created on calling StartHistoryRetrieval function
ULONG
WINAPI
PedometerDevice::HistoryRetrievalThread(
_In_ LPVOID lpParam // sensor device object
)
{
WDF_OBJECT_ATTRIBUTES MemoryAttributes;
WDFMEMORY MemoryHandle = NULL;
PHardwareSimulator pSimulator = nullptr;
SENSOROBJECT SensorInstance = reinterpret_cast<SENSOROBJECT>(lpParam);
PPedometerDevice pDevice = GetPedometerContextFromSensorInstance(SensorInstance);
ULONG ReturnCode = ERROR_SUCCESS;
PPedometerSample HwHistoryBuffer = nullptr;
NTSTATUS Status = STATUS_SUCCESS;
ULONG FillableCount = 0;
if (nullptr == pDevice)
{
Status = STATUS_INVALID_PARAMETER;
TraceError("PED %!FUNC! Sensor(0x%p) parameter is invalid. pDevice is null", SensorInstance);
goto Exit;
}
// Get the simulator context
pSimulator = GetHardwareSimulatorContextFromInstance(pDevice->m_SimulatorInstance);
if (nullptr == pSimulator)
{
Status = STATUS_INSUFFICIENT_RESOURCES;
TraceError("PED %!FUNC! GetHardwareSimulatorContextFromInstance failed %!STATUS!", Status);
goto Exit;
}
// Get the number of elements that can actually fit into the provided client buffer.
FillableCount = (pDevice->m_ClientHistoryBufferSize - SENSOR_COLLECTION_LIST_HEADER_SIZE) / (pDevice->m_HistoryMarshalledRecordSize - SENSOR_COLLECTION_LIST_HEADER_SIZE);
// Allocate enough memory to read the samples from HW
MemoryHandle = NULL;
WDF_OBJECT_ATTRIBUTES_INIT(&MemoryAttributes);
MemoryAttributes.ParentObject = SensorInstance;
Status = WdfMemoryCreate(&MemoryAttributes,
PagedPool,
SENSOR_POOL_TAG_PEDOMETER,
FillableCount * sizeof(PedometerSample),
&MemoryHandle,
reinterpret_cast<PVOID*>(&HwHistoryBuffer));
if (!NT_SUCCESS(Status) || nullptr == HwHistoryBuffer)
{
TraceError("PED %!FUNC! WdfMemoryCreate failed %!STATUS!", Status);
goto Exit;
}
Status = pSimulator->ReadHistory(&FillableCount, HwHistoryBuffer);
if (STATUS_BUFFER_OVERFLOW == Status)
{
// It is okay to return as much as that fits in the buffer.
TraceWarning("PED %!FUNC! Buffer Overflow. More entries available than requested %!STATUS!", Status);
}
else if (STATUS_CANCELLED == Status)
{
TraceWarning("PED %!FUNC! Retrieval canceled. Filling the buffer with retrieved entries %!STATUS!", Status);
}
else if (STATUS_NO_MORE_ENTRIES == Status)
{
TraceInformation("PED %!FUNC! No history entries available %!STATUS!", Status);
goto Exit;
}
else if (!NT_SUCCESS(Status))
{
TraceError("PED %!FUNC! Failed to read history from the HW %!STATUS!", Status);
goto Exit;
}
// All entries into the client's buffer must be complete i.e. no partial entries are allowed.
for (ULONG index = 0; index < FillableCount; index++)
{
ULONG ElementIndex = 0;
PedometerSample Data = HwHistoryBuffer[index];
ElementIndex = (index * PEDOMETER_DATA_COUNT) + PEDOMETER_DATA_TIMESTAMP;
pDevice->m_ClientHistoryBuffer->List[ElementIndex].Key = PKEY_SensorData_Timestamp;
InitPropVariantFromFileTime(&(Data.Timestamp), &(pDevice->m_ClientHistoryBuffer->List[ElementIndex].Value));
ElementIndex = (index * PEDOMETER_DATA_COUNT) + PEDOMETER_DATA_FIRST_AFTER_RESET;
pDevice->m_ClientHistoryBuffer->List[ElementIndex].Key = PKEY_SensorData_PedometerReset;
InitPropVariantFromBoolean(Data.IsFirstAfterReset, &(pDevice->m_ClientHistoryBuffer->List[ElementIndex].Value));
ElementIndex = (index * PEDOMETER_DATA_COUNT) + PEDOMETER_DATA_UNKNOWN_STEP_TYPE;
pDevice->m_ClientHistoryBuffer->List[ElementIndex].Key = PKEY_SensorData_PedometerStepType;
InitPropVariantFromUInt32(PedometerStepType_Unknown, &(pDevice->m_ClientHistoryBuffer->List[ElementIndex].Value));
ElementIndex = (index * PEDOMETER_DATA_COUNT) + PEDOMETER_DATA_UNKNOWN_STEP_COUNT;
pDevice->m_ClientHistoryBuffer->List[ElementIndex].Key = PKEY_SensorData_PedometerStepCount;
InitPropVariantFromUInt32(Data.UnknownStepCount, &(pDevice->m_ClientHistoryBuffer->List[ElementIndex].Value));
ElementIndex = (index * PEDOMETER_DATA_COUNT) + PEDOMETER_DATA_UNKNOWN_STEP_DURATION;
pDevice->m_ClientHistoryBuffer->List[ElementIndex].Key = PKEY_SensorData_PedometerStepDuration_Ms;
InitPropVariantFromInt64(Data.UnknownStepDurationMs, &(pDevice->m_ClientHistoryBuffer->List[ElementIndex].Value));
ElementIndex = (index * PEDOMETER_DATA_COUNT) + PEDOMETER_DATA_WALKING_STEP_TYPE;
pDevice->m_ClientHistoryBuffer->List[ElementIndex].Key = PKEY_SensorData_PedometerStepType;
InitPropVariantFromUInt32(PedometerStepType_Walking, &(pDevice->m_ClientHistoryBuffer->List[ElementIndex].Value));
ElementIndex = (index * PEDOMETER_DATA_COUNT) + PEDOMETER_DATA_WALKING_STEP_COUNT;
pDevice->m_ClientHistoryBuffer->List[ElementIndex].Key = PKEY_SensorData_PedometerStepCount;
InitPropVariantFromUInt32(Data.WalkingStepCount, &(pDevice->m_ClientHistoryBuffer->List[ElementIndex].Value));
ElementIndex = (index * PEDOMETER_DATA_COUNT) + PEDOMETER_DATA_WALKING_STEP_DURATION;
pDevice->m_ClientHistoryBuffer->List[ElementIndex].Key = PKEY_SensorData_PedometerStepDuration_Ms;
InitPropVariantFromInt64(Data.WalkingStepDurationMs, &(pDevice->m_ClientHistoryBuffer->List[ElementIndex].Value));
ElementIndex = (index * PEDOMETER_DATA_COUNT) + PEDOMETER_DATA_RUNNING_STEP_TYPE;
pDevice->m_ClientHistoryBuffer->List[ElementIndex].Key = PKEY_SensorData_PedometerStepType;
InitPropVariantFromUInt32(PedometerStepType_Running, &(pDevice->m_ClientHistoryBuffer->List[ElementIndex].Value));
ElementIndex = (index * PEDOMETER_DATA_COUNT) + PEDOMETER_DATA_RUNNING_STEP_COUNT;
pDevice->m_ClientHistoryBuffer->List[ElementIndex].Key = PKEY_SensorData_PedometerStepCount;
InitPropVariantFromUInt32(Data.RunningStepCount, &(pDevice->m_ClientHistoryBuffer->List[ElementIndex].Value));
ElementIndex = (index * PEDOMETER_DATA_COUNT) + PEDOMETER_DATA_RUNNING_STEP_DURATION;
pDevice->m_ClientHistoryBuffer->List[ElementIndex].Key = PKEY_SensorData_PedometerStepDuration_Ms;
InitPropVariantFromInt64(Data.RunningStepDurationMs, &(pDevice->m_ClientHistoryBuffer->List[ElementIndex].Value));
// Keep the count in the sensor collection list updated
// so that the count is valid even when exit is signaled.
pDevice->m_ClientHistoryBuffer->Count += PEDOMETER_DATA_COUNT;
}
Exit:
if (NULL != MemoryHandle)
{
// delete the memory handle and associated memory
WdfObjectDelete(MemoryHandle);
}
// Update the state before we notify the Sensor CX as we are done with this request at this point
// It would also help should the CX invoke 'EvtSensorStartHistoryRetrieval' on the same thread
WdfWaitLockAcquire(pDevice->m_Lock, NULL);
pDevice->m_HistoryRetrievalStarted = FALSE;
WdfWaitLockRelease(pDevice->m_Lock);
// call completed method only if retrieval was not canceled
if (STATUS_CANCELLED != Status)
{
SensorsCxSensorHistoryRetrievalCompleted(pDevice->m_SensorInstance, CollectionsListGetMarshalledSizeWithoutSerialization(pDevice->m_ClientHistoryBuffer), Status);
}
return ReturnCode;
}
// Called by Sensor CLX to get supported data fields. The typical usage is to call
// this function once with buffer pointer as NULL to acquire the required size
// for the buffer, allocate buffer, then call the function again to retrieve
// sensor information.
NTSTATUS
PedometerDevice::OnGetSupportedDataFields(
_In_ SENSOROBJECT SensorInstance, // Sensor device object
_Inout_opt_ PSENSOR_PROPERTY_LIST pFields, // Pointer to a list of supported properties
_Out_ PULONG pSize // Number of bytes for the list of supported properties
)
{
PPedometerDevice pDevice = GetPedometerContextFromSensorInstance(SensorInstance);
NTSTATUS Status = STATUS_SUCCESS;
SENSOR_FunctionEnter();
if (nullptr == pDevice || nullptr == pSize)
{
Status = STATUS_INVALID_PARAMETER;
TraceError("PED %!FUNC! Invalid parameters! %!STATUS!", Status);
goto Exit;
}
if (nullptr == pFields)
{
// Just return size
*pSize = pDevice->m_pSupportedDataFields->AllocatedSizeInBytes;
}
else
{
if (pFields->AllocatedSizeInBytes < pDevice->m_pSupportedDataFields->AllocatedSizeInBytes)
{
Status = STATUS_INSUFFICIENT_RESOURCES;
TraceError("PED %!FUNC! Buffer is too small. Failed %!STATUS!", Status);
goto Exit;
}
// Fill out data
Status = PropertiesListCopy(pFields, pDevice->m_pSupportedDataFields);
if (!NT_SUCCESS(Status))
{
TraceError("PED %!FUNC! PropertiesListCopy failed %!STATUS!", Status);
goto Exit;
}
*pSize = pDevice->m_pSupportedDataFields->AllocatedSizeInBytes;
}
Exit:
if (!NT_SUCCESS(Status))
{
*pSize = 0;
}
SENSOR_FunctionExit(Status);
return Status;
}
// Called by Sensor CLX to get sensor properties. The typical usage is to call
// this function once with buffer pointer as NULL to acquire the required size
// for the buffer, allocate buffer, then call the function again to retrieve
// sensor information.
NTSTATUS
PedometerDevice::OnGetProperties(
_In_ SENSOROBJECT SensorInstance, // Sensor device object
_Inout_opt_ PSENSOR_COLLECTION_LIST pProperties, // Pointer to a list of sensor properties
_Out_ PULONG pSize // Number of bytes for the list of sensor properties
)
{
PPedometerDevice pDevice = GetPedometerContextFromSensorInstance(SensorInstance);
NTSTATUS Status = STATUS_SUCCESS;
SENSOR_FunctionEnter();
if (nullptr == pDevice || nullptr == pSize)
{
Status = STATUS_INVALID_PARAMETER;
TraceError("PED %!FUNC! Invalid parameters! %!STATUS!", Status);
goto Exit;
}
if (nullptr == pProperties)
{
// Just return size
*pSize = CollectionsListGetMarshalledSize(pDevice->m_pProperties);
}
else
{
if (pProperties->AllocatedSizeInBytes <
CollectionsListGetMarshalledSize(pDevice->m_pProperties))
{
Status = STATUS_INSUFFICIENT_RESOURCES;
TraceError("PED %!FUNC! Buffer is too small. Failed %!STATUS!", Status);
goto Exit;
}
// Fill out all data
Status = CollectionsListCopyAndMarshall(pProperties, pDevice->m_pProperties);
if (!NT_SUCCESS(Status))
{
TraceError("PED %!FUNC! CollectionsListCopyAndMarshall failed %!STATUS!", Status);
goto Exit;
}
*pSize = CollectionsListGetMarshalledSize(pDevice->m_pProperties);
}
Exit:
if (!NT_SUCCESS(Status))
{
*pSize = 0;
}
SENSOR_FunctionExit(Status);
return Status;
}
// Called by Sensor CLX to get data field properties. The typical usage is to call
// this function once with buffer pointer as NULL to acquire the required size
// for the buffer, allocate buffer, then call the function again to retrieve
// sensor information.
NTSTATUS
PedometerDevice::OnGetDataFieldProperties(
_In_ SENSOROBJECT SensorInstance, // Sensor device object
_In_ const PROPERTYKEY *DataField, // Pointer to the propertykey of requested property
_Inout_opt_ PSENSOR_COLLECTION_LIST pProperties, // Pointer to a list of sensor properties
_Out_ PULONG pSize // Number of bytes for the list of sensor properties
)
{
PPedometerDevice pDevice = GetPedometerContextFromSensorInstance(SensorInstance);
NTSTATUS Status = STATUS_SUCCESS;
SENSOR_FunctionEnter();
if (nullptr == pDevice || nullptr == pSize || nullptr == DataField)
{
Status = STATUS_INVALID_PARAMETER;
TraceError("PED %!FUNC! Invalid parameters! %!STATUS!", Status);
goto Exit;
}
if ((*DataField == PKEY_SensorData_PedometerStepCount))
{
if (nullptr == pProperties)
{
// Just return size
*pSize = CollectionsListGetMarshalledSize(pDevice->m_pDataFieldProperties);
}
else
{
if (pProperties->AllocatedSizeInBytes <
CollectionsListGetMarshalledSize(pDevice->m_pDataFieldProperties))
{
Status = STATUS_INSUFFICIENT_RESOURCES;
TraceError("PED %!FUNC! Buffer is too small. Failed %!STATUS!", Status);
goto Exit;
}
// Fill out all data
Status = CollectionsListCopyAndMarshall (pProperties, pDevice->m_pDataFieldProperties);
if (!NT_SUCCESS(Status))
{
TraceError("PED %!FUNC! CollectionsListCopyAndMarshall failed %!STATUS!", Status);
goto Exit;
}
*pSize = CollectionsListGetMarshalledSize(pDevice->m_pDataFieldProperties);
}
}
else
{
Status = STATUS_NOT_SUPPORTED;
TraceError("PED %!FUNC! Ped does NOT have properties for this data field. Failed %!STATUS!", Status);
goto Exit;
}
Exit:
if (!NT_SUCCESS(Status))
{
*pSize = 0;
}
SENSOR_FunctionExit(Status);
return Status;
}
// Called by Sensor CLX to get sampling rate of the sensor.
NTSTATUS
PedometerDevice::OnGetDataInterval(
_In_ SENSOROBJECT SensorInstance, // Sensor device object
_Out_ PULONG DataRateMs // Sampling rate in ms
)
{
PPedometerDevice pDevice = GetPedometerContextFromSensorInstance(SensorInstance);
NTSTATUS Status = STATUS_SUCCESS;
SENSOR_FunctionEnter();
if (nullptr == pDevice)
{
Status = STATUS_INVALID_PARAMETER;
TraceError("PED %!FUNC! Sensor(0x%p) parameter is invalid. Failed %!STATUS!", SensorInstance, Status);
goto Exit;
}
if (nullptr == DataRateMs)
{
Status = STATUS_INVALID_PARAMETER;
TraceError("PED %!FUNC! DataRateMs(0x%p) parameter is invalid. Failed %!STATUS!", DataRateMs, Status);
goto Exit;
}
*DataRateMs = pDevice->m_Interval;
Exit:
SENSOR_FunctionExit(Status);
return Status;
}
// Called by Sensor CLX to set sampling rate of the sensor.
NTSTATUS
PedometerDevice::OnSetDataInterval(
_In_ SENSOROBJECT SensorInstance, // Sensor device object
_In_ ULONG DataRateMs // Sampling rate in ms
)
{
PPedometerDevice pDevice = GetPedometerContextFromSensorInstance(SensorInstance);
NTSTATUS Status = STATUS_SUCCESS;
SENSOR_FunctionEnter();
if (nullptr == pDevice)
{
Status = STATUS_INVALID_PARAMETER;
TraceError("PED %!FUNC! Sensor(0x%p) parameter is invalid. Failed %!STATUS!", SensorInstance, Status);
goto Exit;
}
if (Pedometer_Default_MinDataInterval_Ms > DataRateMs)
{
Status = STATUS_INVALID_PARAMETER;
TraceError("PED %!FUNC! DataRateMs(%d) parameter is smaller than the minimum data interval. Failed %!STATUS!", DataRateMs, Status);
goto Exit;
}
pDevice->m_Interval = DataRateMs;
// reschedule sample to return as soon as possible if it's started
if (FALSE != pDevice->m_Started)
{
pDevice->m_Started = FALSE;
WdfTimerStop(pDevice->m_Timer, TRUE);
pDevice->m_Started = TRUE;
pDevice->m_FirstSample = TRUE;
WdfTimerStart(pDevice->m_Timer, WDF_REL_TIMEOUT_IN_MS(Pedometer_Default_MinDataInterval_Ms));
}
Exit:
SENSOR_FunctionExit(Status);
return Status;
}
// Called by Sensor CLX to get data thresholds. The typical usage is to call
// this function once with buffer pointer as NULL to acquire the required size
// for the buffer, allocate buffer, then call the function again to retrieve
// sensor information.
NTSTATUS
PedometerDevice::OnGetDataThresholds(
_In_ SENSOROBJECT SensorInstance, // Sensor device object
_Inout_opt_ PSENSOR_COLLECTION_LIST pThresholds, // Pointer to a list of sensor thresholds
_Out_ PULONG pSize // Number of bytes for the list of sensor thresholds
)
{
PPedometerDevice pDevice = GetPedometerContextFromSensorInstance(SensorInstance);
NTSTATUS Status = STATUS_SUCCESS;
SENSOR_FunctionEnter();
if (nullptr == pDevice || nullptr == pSize)
{
Status = STATUS_INVALID_PARAMETER;
TraceError("PED %!FUNC! Invalid parameters! %!STATUS!", Status);
goto Exit;
}
if (nullptr == pThresholds)
{
// Just return size
*pSize = CollectionsListGetMarshalledSize(pDevice->m_pThresholds);
}
else
{
if (pThresholds->AllocatedSizeInBytes <
CollectionsListGetMarshalledSize(pDevice->m_pThresholds))
{
Status = STATUS_INSUFFICIENT_RESOURCES;
TraceError("PED %!FUNC! Buffer is too small. Failed %!STATUS!", Status);
goto Exit;
}
// Fill out all data
Status = CollectionsListCopyAndMarshall(pThresholds, pDevice->m_pThresholds);
if (!NT_SUCCESS(Status))
{
TraceError("PED %!FUNC! CollectionsListCopyAndMarshall failed %!STATUS!", Status);
goto Exit;
}
*pSize = CollectionsListGetMarshalledSize(pDevice->m_pThresholds);
}
Exit:
if (!NT_SUCCESS(Status))
{
*pSize = 0;
}
SENSOR_FunctionExit(Status);
return Status;
}
// Called by Sensor CLX to set data thresholds.
NTSTATUS
PedometerDevice::OnSetDataThresholds(
_In_ SENSOROBJECT SensorInstance, // Sensor device object
_In_ PSENSOR_COLLECTION_LIST pThresholds // Pointer to a list of sensor thresholds
)
{
ULONG Element;
BOOLEAN IsLocked = FALSE;
PPedometerDevice pDevice = GetPedometerContextFromSensorInstance(SensorInstance);
NTSTATUS Status = STATUS_SUCCESS;
SENSOR_FunctionEnter();
if (pDevice == nullptr)
{
Status = STATUS_INVALID_PARAMETER;
TraceError("PED %!FUNC! Sensor(0x%p) parameter is invalid. Failed %!STATUS!", SensorInstance, Status);
goto Exit;
}
WdfWaitLockAcquire(pDevice->m_Lock, NULL);
IsLocked = TRUE;
for (Element = 0; Element < pThresholds->Count; Element++)
{
Status = PropKeyFindKeySetPropVariant(pDevice->m_pThresholds,
&(pThresholds->List[Element].Key),
TRUE,
&(pThresholds->List[Element].Value));
if (!NT_SUCCESS(Status))
{
Status = STATUS_INVALID_PARAMETER;
TraceError("PED %!FUNC! Pedometer driver does NOT have threshold for this data field. Failed %!STATUS!", Status);
goto Exit;
}
}
// Get data thresholds
Status = PropKeyFindKeyGetUlong(pDevice->m_pThresholds,
&PKEY_SensorData_PedometerStepCount,
&(pDevice->m_CachedThreshold));
if (!NT_SUCCESS(Status))
{
TraceError("PED %!FUNC! PropKeyFindKeyGetUlong for PedometerStepCount failed! %!STATUS!", Status);
goto Exit;
}
Exit:
if (FALSE != IsLocked)
{
WdfWaitLockRelease(pDevice->m_Lock);
IsLocked = FALSE;
}
SENSOR_FunctionExit(Status);
return Status;
}
// Called by Sensor CLX to handle IOCTLs that clx does not support
NTSTATUS
PedometerDevice::OnIoControl(
_In_ SENSOROBJECT /*SensorInstance*/, // WDF queue object
_In_ WDFREQUEST /*Request*/, // WDF request object
_In_ size_t /*OutputBufferLength*/, // number of bytes to retrieve from output buffer
_In_ size_t /*InputBufferLength*/, // number of bytes to retrieve from input buffer
_In_ ULONG /*IoControlCode*/ // IOCTL control code
)
{
NTSTATUS Status = STATUS_NOT_SUPPORTED;
SENSOR_FunctionEnter();
SENSOR_FunctionExit(Status);
return Status;
}
|