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
|
// Copyright (C) Microsoft Corporation, All Rights Reserved.
//
// Abstract:
// This module contains the implementation of driver callback function from clx to activity detection driver.
//
// Environment:
// Windows User-Mode Driver Framework (UMDF)
#include "Device.h"
#include "HardwareSimulator.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 ActivityDevice::GetData()
{
BOOLEAN dataReady = FALSE;
NTSTATUS status = STATUS_SUCCESS;
SENSOR_FunctionEnter();
if (NULL != m_SimulatorInstance)
{
// Use simulator to get m_pFiltered Sample
PHardwareSimulator pSimulator = GetHardwareSimulatorContextFromInstance(m_SimulatorInstance);
if (nullptr != pSimulator)
{
status = pSimulator->GetSample(m_pFilteredSample);
}
else
{
status = STATUS_INVALID_PARAMETER;
}
}
if (NT_SUCCESS(status))
{
status = CollectionsListSortSubscribedActivitiesByConfidence(m_pThresholds, m_pFilteredSample);
if (!NT_SUCCESS(status))
{
TraceError("ACT %!FUNC! CollectionsListSortSubscribedActivitiesByConfidence failed! %!STATUS!", status);
}
else
{
// new sample?
if (FALSE != m_FirstSample)
{
dataReady = TRUE;
}
else
{
dataReady = EvaluateActivityThresholds(m_pFilteredSample, m_pLastSample, m_pThresholds);
}
}
}
if (FALSE != dataReady)
{
// update last sample
FILETIME TimeStamp = {};
memcpy_s(m_pLastSample, m_pFilteredSample->AllocatedSizeInBytes, m_pFilteredSample, m_pFilteredSample->AllocatedSizeInBytes);
GetSystemTimePreciseAsFileTime(&TimeStamp);
InitPropVariantFromFileTime(&TimeStamp, &(m_pLastSample->List[ACTIVITY_DATA_TIMESTAMP].Value));
// push to clx
SensorsCxSensorDataReady(m_SensorInstance, m_pLastSample);
m_FirstSample = FALSE;
}
else
{
status = STATUS_DATA_NOT_ACCEPTED;
TraceInformation("ACT %!FUNC! Data did NOT meet the threshold");
}
SENSOR_FunctionExit(status);
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 ActivityDevice::OnTimerExpire(_In_ WDFTIMER timer)
{
NTSTATUS status = STATUS_SUCCESS;
SENSOR_FunctionEnter();
PActivityDevice pDevice = GetActivityContextFromSensorInstance(WdfTimerGetParentObject(timer));
if (nullptr == pDevice)
{
status = STATUS_INSUFFICIENT_RESOURCES;
TraceError("ACT %!FUNC! GetActivityContextFromSensorInstance failed %!STATUS!", status);
}
else
{
// Get data and push to clx
WdfWaitLockAcquire(pDevice->m_Lock, NULL);
status = pDevice->GetData();
if (!NT_SUCCESS(status) && STATUS_DATA_NOT_ACCEPTED != status)
{
TraceError("ACT %!FUNC! GetAccData Failed %!STATUS!", status);
}
WdfWaitLockRelease(pDevice->m_Lock);
// Schedule next wake up time
if (Act_Default_MinDataInterval_Ms <= pDevice->m_Interval &&
FALSE != pDevice->m_PoweredOn &&
FALSE != pDevice->m_Started)
{
WdfTimerStart(pDevice->m_Timer, WDF_REL_TIMEOUT_IN_MS(pDevice->m_Interval));
}
}
SENSOR_FunctionExit(status);
}
// Called by Sensor CLX to begin continously sampling the sensor.
NTSTATUS ActivityDevice::OnStart(_In_ SENSOROBJECT sensorInstance)
{
NTSTATUS status = STATUS_SUCCESS;
SENSOR_FunctionEnter();
PActivityDevice pDevice = GetActivityContextFromSensorInstance(sensorInstance);
if (nullptr == pDevice)
{
status = STATUS_INVALID_PARAMETER;
TraceError("ACT %!FUNC! Sensor parameter is invalid. Failed %!STATUS!", status);
}
else
{
if (FALSE == pDevice->m_PoweredOn)
{
status = STATUS_DEVICE_NOT_READY;
TraceError("ACT %!FUNC! Sensor is not powered on! %!STATUS!", status);
}
else
{
// Start simulation
if (NULL != pDevice->m_SimulatorInstance)
{
PHardwareSimulator pSimulator = GetHardwareSimulatorContextFromInstance(pDevice->m_SimulatorInstance);
if (nullptr != pSimulator)
{
pSimulator->Start();
}
}
// Start sensing
pDevice->m_FirstSample = TRUE;
pDevice->m_Started = TRUE;
InitPropVariantFromUInt32(SensorState_Active, &(pDevice->m_pProperties->List[SENSOR_PROPERTY_STATE].Value));
WdfTimerStart(pDevice->m_Timer, WDF_REL_TIMEOUT_IN_MS(Act_Default_MinDataInterval_Ms));
}
}
SENSOR_FunctionExit(status);
return status;
}
// Called by Sensor CLX to stop continously sampling the sensor.
NTSTATUS ActivityDevice::OnStop(_In_ SENSOROBJECT sensorInstance)
{
NTSTATUS status = STATUS_SUCCESS;
SENSOR_FunctionEnter();
PActivityDevice pDevice = GetActivityContextFromSensorInstance(sensorInstance);
if (nullptr == pDevice)
{
status = STATUS_INVALID_PARAMETER;
TraceError("ACT %!FUNC! Sensor parameter is invalid. Failed %!STATUS!", status);
}
else
{
// Stop sensing
pDevice->m_Started = FALSE;
WdfTimerStop(pDevice->m_Timer, TRUE);
InitPropVariantFromUInt32(SensorState_Idle, &(pDevice->m_pProperties->List[SENSOR_PROPERTY_STATE].Value));
// Stop simulation
if (NULL != pDevice->m_SimulatorInstance)
{
PHardwareSimulator pSimulator = GetHardwareSimulatorContextFromInstance(pDevice->m_SimulatorInstance);
if (nullptr != pSimulator)
{
pSimulator->Stop();
}
}
}
SENSOR_FunctionExit(status);
return status;
}
// This routine is called by history retrieval thread to remove an entry from the history buffer.
// Note this function must be called under lock
_Requires_lock_held_(m_HistoryLock)
NTSTATUS ActivityDevice::RemoveDataElementFromHistoryBuffer(_Out_ PActivitySample pData)
{
NTSTATUS status = STATUS_SUCCESS;
if (nullptr == pData)
{
status = STATUS_INVALID_PARAMETER;
TraceError("ACT %!FUNC! NULL ptr passed in %!STATUS!", status);
}
else
{
if (0 == m_History.NumOfElems)
{
// buffer empty
status = STATUS_NO_MORE_ENTRIES;
}
else
{
// Remove the oldest element from the circular buffer
*pData = m_History.pData[m_History.FirstElemIndex];
m_History.FirstElemIndex++;
m_History.FirstElemIndex %= m_History.BufferLength;
m_History.NumOfElems--;
if (0 == m_History.NumOfElems)
{
// Buffer Empty. 'LastElemIndex' should be same as 'FirstElemIndex'
m_History.LastElemIndex = m_History.FirstElemIndex;
}
}
}
return status;
}
// This routine is called by worker thread to add an entry to the history buffer.
// Note this function must be called under lock
_Requires_lock_held_(m_HistoryLock)
NTSTATUS ActivityDevice::AddDataElementToHistoryBuffer(_In_ PActivitySample pData)
{
NTSTATUS status = STATUS_SUCCESS;
if (nullptr == pData)
{
status = STATUS_INVALID_PARAMETER;
TraceError("ACT %!FUNC! GetActivityContextFromSensorInstance failed %!STATUS!", status);
}
else
{
if (0 == m_History.NumOfElems)
{
// buffer empty
m_History.NumOfElems++;
}
else if (m_History.BufferLength > m_History.NumOfElems)
{
// buffer not full yet. Increment the index of the last element
m_History.LastElemIndex++;
m_History.LastElemIndex %= m_History.BufferLength;
m_History.NumOfElems++;
}
else if (m_History.BufferLength == m_History.NumOfElems)
{
// buffer full. Over-write the oldest element
m_History.FirstElemIndex++;
m_History.FirstElemIndex %= m_History.BufferLength;
m_History.LastElemIndex++;
m_History.LastElemIndex %= m_History.BufferLength;
}
memcpy_s(&(m_History.pData[m_History.LastElemIndex]),
sizeof(m_History.pData[m_History.LastElemIndex]),
pData,
sizeof(*pData));
}
return status;
}
// This routine is for clearing the history buffer. Note this function must be called under lock.
_Requires_lock_held_(m_HistoryLock)
NTSTATUS ActivityDevice::ClearHistoryBuffer()
{
m_History.FirstElemIndex = 0;
m_History.LastElemIndex = 0;
m_History.NumOfElems = 0;
RtlZeroMemory(m_History.pData, (m_History.BufferLength*sizeof(ActivitySample)));
return STATUS_SUCCESS;
}
// This callback is called when interval wait time has expired and driver is ready
// to collect new sample. The callback stores activity data in history buffer,
// and schedules next wake up time.
VOID ActivityDevice::OnHistoryTimerExpire(_In_ WDFTIMER historyTimer)
{
NTSTATUS status = STATUS_SUCCESS;
SENSOR_FunctionEnter();
PActivityDevice pDevice = GetActivityContextFromSensorInstance(WdfTimerGetParentObject(historyTimer));
if (nullptr == pDevice)
{
status = STATUS_INSUFFICIENT_RESOURCES;
TraceError("ACT %!FUNC! GetActivityContextFromSensorInstance failed %!STATUS!", status);
}
else
{
ActivitySample data = {};
if (NULL != pDevice->m_SimulatorInstance)
{
PHardwareSimulator pSimulator = GetHardwareSimulatorContextFromInstance(pDevice->m_SimulatorInstance);
if (nullptr != pSimulator)
{
status = pSimulator->GetSample(&data);
}
else
{
status = STATUS_INVALID_PARAMETER;
}
}
GetSystemTimePreciseAsFileTime(&(data.Timestamp));
if (NT_SUCCESS(status))
{
// Add data to the buffer
WdfWaitLockAcquire(pDevice->m_HistoryLock, NULL);
status = pDevice->AddDataElementToHistoryBuffer(&data);
if (!NT_SUCCESS(status))
{
TraceError("ACT %!FUNC! AddDataElementToHistoryBuffer Failed %!STATUS!", status);
}
WdfWaitLockRelease(pDevice->m_HistoryLock);
}
// Schedule next wake up time
if (FALSE != pDevice->m_HistoryStarted)
{
WdfTimerStart(pDevice->m_HistoryTimer, WDF_REL_TIMEOUT_IN_MS(pDevice->m_HistoryIntervalInMs));
}
}
SENSOR_FunctionExit(status);
}
// Called by Sensor CLX to begin keeping history
NTSTATUS ActivityDevice::OnStartHistory(_In_ SENSOROBJECT sensorInstance)
{
NTSTATUS status = STATUS_SUCCESS;
SENSOR_FunctionEnter();
PActivityDevice pDevice = GetActivityContextFromSensorInstance(sensorInstance);
if (nullptr == pDevice)
{
status = STATUS_INVALID_PARAMETER;
TraceError("ACT %!FUNC! Sensor parameter is invalid. Failed %!STATUS!", status);
}
else if (0 == pDevice->m_HistorySizeInRecords)
{
status = STATUS_NOT_SUPPORTED;
TraceError("ACT %!FUNC! Sensor does not support History");
}
else if (FALSE == pDevice->m_PoweredOn)
{
status = STATUS_DEVICE_NOT_READY;
TraceError("ACT %!FUNC! Sensor is not powered on! %!STATUS!", status);
}
else
{
// Start keeping history
pDevice->m_HistoryStarted = TRUE;
WdfTimerStart(pDevice->m_HistoryTimer, WDF_REL_TIMEOUT_IN_MS(pDevice->m_HistoryIntervalInMs));
}
SENSOR_FunctionExit(status);
return status;
}
// Called by Sensor CLX to stop keeping history.
NTSTATUS ActivityDevice::OnStopHistory(_In_ SENSOROBJECT sensorInstance)
{
NTSTATUS status = STATUS_SUCCESS;
SENSOR_FunctionEnter();
PActivityDevice pDevice = GetActivityContextFromSensorInstance(sensorInstance);
if (nullptr == pDevice)
{
status = STATUS_INVALID_PARAMETER;
TraceError("ACT %!FUNC! Sensor parameter is invalid. Failed %!STATUS!", status);
}
else
{
// Stop keeping history
pDevice->m_HistoryStarted = FALSE;
WdfTimerStop(pDevice->m_HistoryTimer, TRUE);
}
SENSOR_FunctionExit(status);
return status;
}
// Called by Sensor CLX to clear all history stored in the sensor.
NTSTATUS ActivityDevice::OnClearHistory(_In_ SENSOROBJECT sensorInstance)
{
NTSTATUS status = STATUS_SUCCESS;
SENSOR_FunctionEnter();
PActivityDevice pDevice = GetActivityContextFromSensorInstance(sensorInstance);
if (nullptr == pDevice)
{
status = STATUS_INVALID_PARAMETER;
TraceError("ACT %!FUNC! Sensor parameter is invalid. Failed %!STATUS!", status);
}
else
{
WdfWaitLockAcquire(pDevice->m_HistoryLock, NULL);
status = pDevice->ClearHistoryBuffer();
if (!NT_SUCCESS(status))
{
TraceError("ACT %!FUNC! ClearHistoryBuffer Failed %!STATUS!", status);
}
WdfWaitLockRelease(pDevice->m_HistoryLock);
}
SENSOR_FunctionExit(status);
return status;
}
// Called by Sensor CLX to start retrieving history.
NTSTATUS ActivityDevice::OnStartHistoryRetrieval(
_In_ SENSOROBJECT sensorInstance,
_Inout_updates_bytes_(historySizeInBytes) PSENSOR_COLLECTION_LIST pHistoryBuffer,
_In_ ULONG historySizeInBytes
)
{
NTSTATUS status = STATUS_SUCCESS;
SENSOR_FunctionEnter();
PActivityDevice pDevice = GetActivityContextFromSensorInstance(sensorInstance);
if (nullptr == pDevice)
{
status = STATUS_INVALID_PARAMETER;
TraceError("ACT %!FUNC! Sensor parameter is invalid. Failed %!STATUS!", status);
}
else
{
// 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_HistoryLock, NULL);
BOOLEAN IsStarted = pDevice->m_HistoryRetrievalStarted;
WdfWaitLockRelease(pDevice->m_HistoryLock);
if (FALSE != IsStarted)
{
status = STATUS_DEVICE_BUSY;
TraceError("ACT %!FUNC! StartHistoryRetrieval called again before the previous call"
"finishes retrieving history %!STATUS!", status);
}
else
{
// 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;
}
// check that the buffer is not null
if (NULL == pHistoryBuffer)
{
status = STATUS_INVALID_PARAMETER;
TraceError("ACT %!FUNC! History buffer cannot be NULL. %!STATUS!", status);
}
else
{
// check that the buffer can hold at least one entry
if (historySizeInBytes < pDevice->m_HistoryMarshalledRecordSize)
{
status = STATUS_BUFFER_TOO_SMALL;
TraceError("ACT %!FUNC! History buffer is too small to even fill one complete entry. %!STATUS!", status);
}
else
{
pDevice->m_ClientHistoryBuffer = pHistoryBuffer;
pDevice->m_ClientHistoryBufferSize = historySizeInBytes;
// Reset exit event
ResetEvent(pDevice->m_ExitEvent);
WdfWaitLockAcquire(pDevice->m_HistoryLock, NULL);
// 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<PVOID>(sensorInstance), // pointer to variable to be passed
0, // run immediately
NULL); // Thread ID
if (NULL == pDevice->m_hThread)
{
status = STATUS_UNSUCCESSFUL;
TraceError("ACT %!FUNC! CreateThread Failed %!STATUS!", status);
}
else
{
pDevice->m_HistoryRetrievalStarted = TRUE;
}
WdfWaitLockRelease(pDevice->m_HistoryLock);
}
}
}
}
SENSOR_FunctionExit(status);
if (nullptr != pDevice && !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 ActivityDevice::OnCancelHistoryRetrieval(_In_ SENSOROBJECT sensorInstance, _Out_ PULONG pBytesWritten)
{
NTSTATUS status = STATUS_SUCCESS;
*pBytesWritten = 0;
SENSOR_FunctionEnter();
PActivityDevice pDevice = GetActivityContextFromSensorInstance(sensorInstance);
if (nullptr == pDevice)
{
status = STATUS_INVALID_PARAMETER;
TraceError("ACT %!FUNC! Sensor parameter is invalid. Failed %!STATUS!", status);
}
else
{
// Check if history retrieval operation is in progress
WdfWaitLockAcquire(pDevice->m_HistoryLock, NULL);
BOOLEAN IsStarted = pDevice->m_HistoryRetrievalStarted;
WdfWaitLockRelease(pDevice->m_HistoryLock);
if (FALSE == IsStarted)
{
status = STATUS_INVALID_DEVICE_REQUEST;
TraceError("ACT %!FUNC! History retrieval is not in progress %!STATUS!", status);
}
else
{
// Signal the history retrieval thread to exit
SetEvent(pDevice->m_ExitEvent);
// Wait for the history retrieval thread to finish writing the entry since no partial entries are allowed.
DWORD result = WaitForSingleObjectEx(pDevice->m_hThread, Act_TimeoutForHistoryThread_Ms, FALSE);
if (WAIT_OBJECT_0 != result)
{
// continue to mark history retrieval stop
status = NTSTATUS_FROM_WIN32(result);
TraceError("ACT %!FUNC! WaitForSingleObjectEx failed! %!STATUS!", status);
}
WdfWaitLockAcquire(pDevice->m_HistoryLock, NULL);
*pBytesWritten = CollectionsListGetMarshalledSizeWithoutSerialization(pDevice->m_ClientHistoryBuffer);
pDevice->m_HistoryRetrievalStarted = FALSE;
WdfWaitLockRelease(pDevice->m_HistoryLock);
}
}
SENSOR_FunctionExit(status);
return status;
}
// Created on calling StartHistoryRetrieval function.
ULONG WINAPI ActivityDevice::HistoryRetrievalThread(_In_ LPVOID lpParam)
{
NTSTATUS status = STATUS_SUCCESS;
BOOLEAN exitSignal = FALSE;
UINT existingCount = 0;
UINT fillableCount = 0;
SENSOROBJECT sensorInstance = reinterpret_cast<SENSOROBJECT>(lpParam);
PActivityDevice pDevice = GetActivityContextFromSensorInstance(sensorInstance);
if (nullptr == pDevice ||
NULL == pDevice->m_ClientHistoryBuffer ||
pDevice->m_HistoryMarshalledRecordSize > pDevice->m_ClientHistoryBufferSize)
{
status = STATUS_INVALID_PARAMETER;
TraceError("ACT %!FUNC! Sensor parameter is invalid. Failed %!STATUS!", status);
}
else
{
// Capture the number of entries in the driver's history buffer at this moment
WdfWaitLockAcquire(pDevice->m_HistoryLock, NULL);
existingCount = pDevice->m_History.NumOfElems;
WdfWaitLockRelease(pDevice->m_HistoryLock);
// How many entries can the client supplied buffer hold
fillableCount = (pDevice->m_ClientHistoryBufferSize - SENSOR_COLLECTION_LIST_HEADER_SIZE) / (pDevice->m_HistoryMarshalledRecordSize - SENSOR_COLLECTION_LIST_HEADER_SIZE);
// Find the minimum of the number of entries in the driver's history buffer and
// the number of entries that can be held by the client supplied buffer
UINT count = min(existingCount, fillableCount);
// All entries into the client's buffer must be complete i.e. no partial entries are allowed.
// Check whether the exit event is signaled before looping through each time to retrieve an entry from the driver's history buffer.
// Each history record will only contain first entry of the most probable state.
for (UINT index = 0; index < count; index++)
{
ActivitySample data = {};
UINT elementIndex = ACTIVITY_DATA_COUNT * index;
if (WAIT_OBJECT_0 == WaitForSingleObject(pDevice->m_ExitEvent, 0))
{
exitSignal = TRUE;
break;
}
WdfWaitLockAcquire(pDevice->m_HistoryLock, NULL);
if (!NT_SUCCESS(pDevice->RemoveDataElementFromHistoryBuffer(&data)))
{
// Only return error if there is no more entry which is not an error. Therefore no need to set status.
WdfWaitLockRelease(pDevice->m_HistoryLock);
break;
}
elementIndex = (index * ACTIVITY_DATA_COUNT) + ACTIVITY_DATA_TIMESTAMP;
pDevice->m_ClientHistoryBuffer->List[elementIndex].Key = PKEY_SensorData_Timestamp;
InitPropVariantFromFileTime(&(data.Timestamp), &(pDevice->m_ClientHistoryBuffer->List[elementIndex].Value));
elementIndex = (index * ACTIVITY_DATA_COUNT) + ACTIVITY_DATA_CURRENT_STATE;
pDevice->m_ClientHistoryBuffer->List[elementIndex].Key = PKEY_SensorData_CurrentActivityState;
InitPropVariantFromUInt32(data.Activity, &(pDevice->m_ClientHistoryBuffer->List[elementIndex].Value));
elementIndex = (index * ACTIVITY_DATA_COUNT) + ACTIVITY_DATA_CURRENT_CONFIDENCE;
pDevice->m_ClientHistoryBuffer->List[elementIndex].Key = PKEY_SensorData_CurrentActivityStateConfidence_Percentage;
InitPropVariantFromUInt16(data.Confidence, &(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 = (index + 1) * ACTIVITY_DATA_COUNT;
WdfWaitLockRelease(pDevice->m_HistoryLock);
}
}
if (FALSE == exitSignal)
{
if (fillableCount < existingCount)
{
status = STATUS_BUFFER_OVERFLOW;
}
else if (0 == existingCount)
{
status = STATUS_NO_MORE_ENTRIES;
}
// call completed callback only if retrieval was not canceled
SensorsCxSensorHistoryRetrievalCompleted(pDevice->m_SensorInstance, CollectionsListGetMarshalledSizeWithoutSerialization(pDevice->m_ClientHistoryBuffer), status);
}
WdfWaitLockAcquire(pDevice->m_HistoryLock, NULL);
pDevice->m_HistoryRetrievalStarted = FALSE;
WdfWaitLockRelease(pDevice->m_HistoryLock);
return ERROR_SUCCESS;
}
// 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 ActivityDevice::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
{
NTSTATUS status = STATUS_SUCCESS;
SENSOR_FunctionEnter();
PActivityDevice pDevice = GetActivityContextFromSensorInstance(sensorInstance);
if (nullptr == pDevice || nullptr == pSize)
{
status = STATUS_INVALID_PARAMETER;
TraceError("ACT %!FUNC! Invalid parameters! %!STATUS!", status);
}
else
{
if (nullptr == pFields)
{
// Just return size
*pSize = pDevice->m_pSupportedDataFields->AllocatedSizeInBytes;
}
else
{
if (pFields->AllocatedSizeInBytes < pDevice->m_pSupportedDataFields->AllocatedSizeInBytes)
{
status = STATUS_INSUFFICIENT_RESOURCES;
TraceError("ACT %!FUNC! Buffer is too small. Failed %!STATUS!", status);
}
else
{
// Fill out data
status = PropertiesListCopy(pFields, pDevice->m_pSupportedDataFields);
if (!NT_SUCCESS(status))
{
TraceError("ACT %!FUNC! PropertiesListCopy failed %!STATUS!", status);
}
else
{
*pSize = pDevice->m_pSupportedDataFields->AllocatedSizeInBytes;
}
}
}
}
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 ActivityDevice::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
{
NTSTATUS status = STATUS_SUCCESS;
SENSOR_FunctionEnter();
PActivityDevice pDevice = GetActivityContextFromSensorInstance(sensorInstance);
if (nullptr == pDevice || nullptr == pSize)
{
status = STATUS_INVALID_PARAMETER;
TraceError("ACT %!FUNC! Invalid parameters! %!STATUS!", status);
}
else
{
if (nullptr == pProperties)
{
// Just return size
*pSize = CollectionsListGetMarshalledSize(pDevice->m_pProperties);
}
else
{
if (pProperties->AllocatedSizeInBytes <
CollectionsListGetMarshalledSize(pDevice->m_pProperties))
{
status = STATUS_INSUFFICIENT_RESOURCES;
TraceError("ACT %!FUNC! Buffer is too small. Failed %!STATUS!", status);
}
else
{
// Fill out all data
status = CollectionsListCopyAndMarshall(pProperties, pDevice->m_pProperties);
if (!NT_SUCCESS(status))
{
TraceError("ACT %!FUNC! CollectionsListCopyAndMarshall failed %!STATUS!", status);
}
else
{
*pSize = CollectionsListGetMarshalledSize(pDevice->m_pProperties);
}
}
}
}
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 ActivityDevice::OnGetDataFieldProperties(
_In_ SENSOROBJECT sensorInstance, // sensor device object
_In_ const PROPERTYKEY *pDataField, // 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
{
NTSTATUS status = STATUS_SUCCESS;
SENSOR_FunctionEnter();
PActivityDevice pDevice = GetActivityContextFromSensorInstance(sensorInstance);
if (nullptr == pDevice || nullptr == pSize || nullptr == pDataField)
{
status = STATUS_INVALID_PARAMETER;
TraceError("ACT %!FUNC! Invalid parameters! %!STATUS!", status);
}
else
{
if (*pDataField == PKEY_SensorData_CurrentActivityStateConfidence_Percentage)
{
if (nullptr == pProperties)
{
// Just return size
*pSize = CollectionsListGetMarshalledSize(pDevice->m_pDataFieldProperties);
}
else
{
if (pProperties->AllocatedSizeInBytes < CollectionsListGetMarshalledSize(pDevice->m_pDataFieldProperties))
{
status = STATUS_INSUFFICIENT_RESOURCES;
TraceError("ACT %!FUNC! Buffer is too small. Failed %!STATUS!", status);
}
else
{
// Fill out all data
status = CollectionsListCopyAndMarshall(pProperties, pDevice->m_pDataFieldProperties);
if (!NT_SUCCESS(status))
{
TraceError("ACT %!FUNC! CollectionsListCopyAndMarshall failed %!STATUS!", status);
}
else
{
*pSize = CollectionsListGetMarshalledSize(pDevice->m_pDataFieldProperties);
}
}
}
}
else
{
status = STATUS_NOT_SUPPORTED;
TraceError("ACT %!FUNC! AccFake does NOT have properties for this data field. Failed %!STATUS!", status);
}
}
if (!NT_SUCCESS(status))
{
*pSize = 0;
}
SENSOR_FunctionExit(status);
return status;
}
// Called by Sensor CLX to get sampling rate of the sensor.
NTSTATUS ActivityDevice::OnGetDataInterval(_In_ SENSOROBJECT sensorInstance, _Out_ PULONG pDataRateMs)
{
NTSTATUS status = STATUS_SUCCESS;
*pDataRateMs = 0;
SENSOR_FunctionEnter();
PActivityDevice pDevice = GetActivityContextFromSensorInstance(sensorInstance);
if (nullptr == pDevice || nullptr == pDataRateMs)
{
status = STATUS_INVALID_PARAMETER;
TraceError("ACT %!FUNC! Sensor parameter is invalid. Failed %!STATUS!", status);
}
else
{
*pDataRateMs = pDevice->m_Interval;
}
SENSOR_FunctionExit(status);
return status;
}
// Called by Sensor CLX to set sampling rate of the sensor.
NTSTATUS ActivityDevice::OnSetDataInterval(_In_ SENSOROBJECT sensorInstance, _In_ ULONG dataRateMs)
{
NTSTATUS status = STATUS_SUCCESS;
SENSOR_FunctionEnter();
PActivityDevice pDevice = GetActivityContextFromSensorInstance(sensorInstance);
if (nullptr == pDevice || Act_Default_MinDataInterval_Ms > dataRateMs)
{
status = STATUS_INVALID_PARAMETER;
TraceError("ACT %!FUNC! Invalid parameters! %!STATUS!", status);
}
else
{
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(Act_Default_MinDataInterval_Ms));
}
}
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 ActivityDevice::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
{
NTSTATUS status = STATUS_SUCCESS;
SENSOR_FunctionEnter();
PActivityDevice pDevice = GetActivityContextFromSensorInstance(sensorInstance);
if (nullptr == pDevice || nullptr == pSize)
{
status = STATUS_INVALID_PARAMETER;
TraceError("ACT %!FUNC! Invalid parameters! %!STATUS!", status);
}
else
{
if (nullptr == pThresholds)
{
// Just return size
*pSize = CollectionsListGetMarshalledSize(pDevice->m_pThresholds);
}
else
{
if (pThresholds->AllocatedSizeInBytes < CollectionsListGetMarshalledSize(pDevice->m_pThresholds))
{
status = STATUS_INSUFFICIENT_RESOURCES;
TraceError("ACT %!FUNC! Buffer is too small. Failed %!STATUS!", status);
}
else
{
// Fill out all data
status = CollectionsListCopyAndMarshall(pThresholds, pDevice->m_pThresholds);
if (!NT_SUCCESS(status))
{
TraceError("ACT %!FUNC! CollectionsListCopyAndMarshall failed %!STATUS!", status);
}
else
{
*pSize = CollectionsListGetMarshalledSize(pDevice->m_pThresholds);
}
}
}
}
if (!NT_SUCCESS(status))
{
*pSize = 0;
}
SENSOR_FunctionExit(status);
return status;
}
// Called by Sensor CLX to set data thresholds.
NTSTATUS ActivityDevice::OnSetDataThresholds(
_In_ SENSOROBJECT sensorInstance, // sensor device object
_In_ PSENSOR_COLLECTION_LIST pThresholds) // pointer to a list of sensor thresholds
{
NTSTATUS status = STATUS_SUCCESS;
SENSOR_FunctionEnter();
PActivityDevice pDevice = GetActivityContextFromSensorInstance(sensorInstance);
if (nullptr == pDevice)
{
status = STATUS_INVALID_PARAMETER;
TraceError("ACT %!FUNC! Sensor(0x%p) parameter is invalid. Failed %!STATUS!", sensorInstance, status);
}
else
{
WdfWaitLockAcquire(pDevice->m_Lock, NULL);
for (ULONG 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("ACT %!FUNC! ActivityFake does NOT have threshold for this data field. Failed %!STATUS!", status);
break;
}
}
WdfWaitLockRelease(pDevice->m_Lock);
}
SENSOR_FunctionExit(status);
return status;
}
// Called by Sensor CLX to handle IOCTLs that clx does not support.
NTSTATUS ActivityDevice::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
{
SENSOR_FunctionEnter();
WdfRequestComplete(request, STATUS_NOT_SUPPORTED);
SENSOR_FunctionExit(STATUS_NOT_SUPPORTED);
return STATUS_NOT_SUPPORTED;
}
|