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
|
#include <pch.h>
NTSTATUS ProcessRetrieveDeviceAuthentication(_In_ WDFDEVICE Device, _In_ WDFFILEOBJECT FileObject, _In_ WDFREQUEST Request, _Inout_ ULONG_PTR* Information);
NTSTATUS ProcessAuthenticateDevice(_In_ WDFDEVICE Device, _In_ WDFFILEOBJECT FileObject, _In_ WDFREQUEST Request);
NTSTATUS ProcessDeauthenticateDevice(_In_ WDFDEVICE Device, _In_ WDFFILEOBJECT FileObject, _In_ WDFREQUEST Request);
NTSTATUS ProcessUpdateKey(_In_ WDFDEVICE Device, _In_ WDFFILEOBJECT FileObject, _In_ WDFREQUEST Request);
NTSTATUS ProcessGetPropertyRequest(_In_ WDFREQUEST Request, _In_ size_t InputBufferLength, _Inout_ ULONG_PTR* Information);
NTSTATUS ProcessSetPropertyRequest(_In_ WDFREQUEST Request, _In_ size_t InputBufferLength, _Inout_ ULONG_PTR* Information);
NTSTATUS ProcessRetrieveStatisticsRequest(_In_ WDFREQUEST Request, _In_ size_t OutputBufferLength, _Inout_ ULONG_PTR* Information);
NTSTATUS ProcessResetStatisticsRequest(_In_ WDFREQUEST Request);
NTSTATUS ProcessUpdateStatisticsRequest(_In_ WDFREQUEST Request);
NTSTATUS ProcessCheckHealthRequest(_In_ WDFREQUEST Request, _Inout_ ULONG_PTR* Information);
NTSTATUS ProcessGetDeviceBasicsRequest(_In_ WDFREQUEST Request, _Inout_ ULONG_PTR* Information);
/*
** Driver TODO: Complete the implementation of EvtIoDeviceControl for your specific device (if necessary)
**
** WDF calls this callback when a device instance is added to the driver. Good drivers will do a lot of
** work here to set up everything necessary, such as adding callbacks for PNP power state changes.
** This function defines an IO queue for handling DeviceIoControl and file read requests, both of which are
** important to the POS magnetic stripe reader model.
**
** Note that this is not a complete device add implementation, as the PNP power callbacks are not handled.
** Additionally, driver writers may wish to set up additional queues to serialize device property requests
** (see Ioctl.cpp for more info).
*/
VOID EvtIoDeviceControl(_In_ WDFQUEUE Queue, _In_ WDFREQUEST Request, _In_ size_t OutputBufferLength, _In_ size_t InputBufferLength, _In_ ULONG IoControlCode)
{
UNREFERENCED_PARAMETER(Queue);
NTSTATUS status = STATUS_SUCCESS;
ULONG_PTR information = 0;
WDFDEVICE device = WdfIoQueueGetDevice(Queue);
WDFFILEOBJECT fileObject = WdfRequestGetFileObject(Request);
// These are the set of IOCTLs that your device should handle to work with the Windows.Devices.PointOfService APIs.
switch (IoControlCode)
{
// The first three IOCTLs shouldn't require additional processing other than handing them off to PosCx
case IOCTL_POINT_OF_SERVICE_CLAIM_DEVICE:
status = PosCxClaimDevice(device, Request);
break;
case IOCTL_POINT_OF_SERVICE_RELEASE_DEVICE:
status = PosCxReleaseDevice(device, fileObject);
break;
case IOCTL_POINT_OF_SERVICE_RETAIN_DEVICE:
status = PosCxRetainDevice(device, Request);
break;
case IOCTL_POINT_OF_SERVICE_MSR_RETRIEVE_DEVICE_AUTHENTICATION:
status = ProcessRetrieveDeviceAuthentication(device, fileObject, Request, &information);
break;
case IOCTL_POINT_OF_SERVICE_MSR_AUTHENTICATE_DEVICE:
status = ProcessAuthenticateDevice(device, fileObject, Request);
break;
case IOCTL_POINT_OF_SERVICE_MSR_DEAUTHENTICATE_DEVICE:
status = ProcessDeauthenticateDevice(device, fileObject, Request);
break;
case IOCTL_POINT_OF_SERVICE_MSR_UPDATE_KEY:
status = ProcessUpdateKey(device, fileObject, Request);
break;
case IOCTL_POINT_OF_SERVICE_GET_PROPERTY:
status = ProcessGetPropertyRequest(Request, InputBufferLength, &information);
break;
case IOCTL_POINT_OF_SERVICE_SET_PROPERTY:
status = ProcessSetPropertyRequest(Request, InputBufferLength, &information);
break;
case IOCTL_POINT_OF_SERVICE_RETRIEVE_STATISTICS:
status = ProcessRetrieveStatisticsRequest(Request, OutputBufferLength, &information);
break;
case IOCTL_POINT_OF_SERVICE_RESET_STATISTICS:
status = ProcessResetStatisticsRequest(Request);
break;
case IOCTL_POINT_OF_SERVICE_UPDATE_STATISTICS:
status = ProcessUpdateStatisticsRequest(Request);
break;
case IOCTL_POINT_OF_SERVICE_CHECK_HEALTH:
status = ProcessCheckHealthRequest(Request, &information);
break;
// The Get Device Basics IOCTL is always the first IOCTL called by an application using the Windows.Devices.PointOfService APIs.
// Use it to determine when to call PosCxMarkPosApp (see notes about apps marked this way in IoRead.cpp)
case IOCTL_POINT_OF_SERVICE_GET_DEVICE_BASICS:
status = ProcessGetDeviceBasicsRequest(Request, &information);
(void)PosCxMarkPosApp(device, fileObject, TRUE);
break;
default:
// Your device may support additional IOCTLs. In this sample, we return failure for anything else.
status = STATUS_NOT_SUPPORTED;
break;
}
if (status != STATUS_PENDING)
{
WdfRequestCompleteWithInformation(Request, status, information);
}
}
/*
** Driver TODO: Add code to handle various get-property cases.
**
** Implement this function to handle requests for the device authentication information.
** This should be step 1 in authenticating or deauthenticating the device.
*/
NTSTATUS ProcessRetrieveDeviceAuthentication(_In_ WDFDEVICE Device, _In_ WDFFILEOBJECT FileObject, _In_ WDFREQUEST Request, _Inout_ ULONG_PTR* Information)
{
// If the caller is not the device owner, fail the request
if (!PosCxIsDeviceOwner(Device, FileObject))
{
return STATUS_ACCESS_DENIED;
}
*Information = (ULONG_PTR)sizeof(MSR_RETRIEVE_DEVICE_AUTHENTICATION_DATA);
PMSR_RETRIEVE_DEVICE_AUTHENTICATION_DATA reqBufferPtr;
size_t reqBufferSize;
NTSTATUS status = WdfRequestRetrieveOutputBuffer(
Request,
sizeof(MSR_RETRIEVE_DEVICE_AUTHENTICATION_DATA),
reinterpret_cast<PVOID*>(&reqBufferPtr),
&reqBufferSize
);
if (!NT_SUCCESS(status))
{
return status;
}
// TODO: Fill in the reqBufferPtr with the authentication information for your device.
return status;
}
/*
** Driver TODO: Add code to handle various get-property cases.
**
** Implement this function to handle requests to authenticate your device.
** This is step 2 in authenticating the device.
*/
NTSTATUS ProcessAuthenticateDevice(_In_ WDFDEVICE Device, _In_ WDFFILEOBJECT FileObject, _In_ WDFREQUEST Request)
{
// If the caller is not the device owner, fail the request
if (!PosCxIsDeviceOwner(Device, FileObject))
{
return STATUS_ACCESS_DENIED;
}
PMSR_AUTHENTICATE_DEVICE reqBufferPtr;
size_t reqBufferSize;
NTSTATUS status = WdfRequestRetrieveInputBuffer(
Request,
sizeof(MSR_AUTHENTICATE_DEVICE),
reinterpret_cast<PVOID*>(&reqBufferPtr),
&reqBufferSize
);
if (!NT_SUCCESS(status))
{
return status;
}
// TODO: Send the authentication information to the device
return status;
}
/*
** Driver TODO: Add code to handle various get-property cases.
**
** Implement this function to handle requests to deauthenticate your device.
** This is step 2 in deauthenticating the device.
*/
NTSTATUS ProcessDeauthenticateDevice(_In_ WDFDEVICE Device, _In_ WDFFILEOBJECT FileObject, _In_ WDFREQUEST Request)
{
// If the caller is not the device owner, fail the request
if (!PosCxIsDeviceOwner(Device, FileObject))
{
return STATUS_ACCESS_DENIED;
}
PMSR_DEAUTHENTICATE_DEVICE reqBufferPtr;
size_t reqBufferSize;
NTSTATUS status = WdfRequestRetrieveInputBuffer(
Request,
sizeof(MSR_DEAUTHENTICATE_DEVICE),
reinterpret_cast<PVOID*>(&reqBufferPtr),
&reqBufferSize
);
if (!NT_SUCCESS(status))
{
return status;
}
// TODO: Send the deauthentication information to the device
return status;
}
/*
** Driver TODO: Add code to handle various get-property cases.
**
** Implement this function to handle requests to update the key in your device.
*/
NTSTATUS ProcessUpdateKey(_In_ WDFDEVICE Device, _In_ WDFFILEOBJECT FileObject, _In_ WDFREQUEST Request)
{
// If the caller is not the device owner, fail the request
if (!PosCxIsDeviceOwner(Device, FileObject))
{
return STATUS_ACCESS_DENIED;
}
PMSR_UPDATE_KEY reqBufferPtr;
size_t reqBufferSize;
NTSTATUS status = WdfRequestRetrieveInputBuffer(
Request,
sizeof(MSR_UPDATE_KEY),
reinterpret_cast<PVOID*>(&reqBufferPtr),
&reqBufferSize
);
if (!NT_SUCCESS(status))
{
return status;
}
// TODO: Send the updated key information to the device
return status;
}
/*
** Driver TODO: Add code to handle various get-property cases.
**
** Implement this function to handle property get requests.
*/
NTSTATUS ProcessGetPropertyRequest(_In_ WDFREQUEST Request, _In_ size_t InputBufferLength, _Inout_ ULONG_PTR* Information)
{
if (Information == nullptr || Request == nullptr || InputBufferLength < sizeof(PosPropertyId))
{
return STATUS_INVALID_PARAMETER;
}
// POS properties are identified by the property ID that's transmitted in the input buffer.
PosPropertyId* propertyId;
NTSTATUS status = WdfRequestRetrieveInputBuffer(
Request,
sizeof(PosPropertyId),
reinterpret_cast<PVOID*>(&propertyId),
nullptr
);
if (!NT_SUCCESS(status))
{
return status;
}
// All get properties will need access to the output buffer in order to return results.
// The minimum size returned is a UINT32
void* outputBuffer;
size_t outputBufferLength;
status = WdfRequestRetrieveOutputBuffer(
Request,
sizeof(UINT32),
&outputBuffer,
&outputBufferLength
);
if (!NT_SUCCESS(status))
{
return status;
}
// Handle this set of readable properties
switch (*propertyId)
{
case PosPropertyId::IsEnabled:
// BOOL result, true when the app has called SetProperty(IsEnabled) = TRUE
{
BOOL isEnabled = TRUE; // Get this value from device context or by querying the device
*((BOOL*)outputBuffer) = isEnabled;
*Information = sizeof(BOOL);
}
break;
case PosPropertyId::IsDisabledOnDataReceived:
// BOOL result, true when the app has called SetProperty(IsDisabledOnDataReceived) = TRUE
{
BOOL isDisabledOnDataReceived = TRUE; // Get this value from device context or by querying the device
*((BOOL*)outputBuffer) = isDisabledOnDataReceived;
*Information = sizeof(BOOL);
}
break;
case PosPropertyId::MagneticStripeReaderIsDecodeDataEnabled:
// BOOL result, true when the app has called SetProperty(MagneticStripeReaderIsDecodeDataEnabled) = TRUE
{
BOOL isDecodeDataEnabled = TRUE; // Get this value from device context or by querying the device
*((BOOL*)outputBuffer) = isDecodeDataEnabled;
*Information = sizeof(BOOL);
}
break;
case PosPropertyId::MagneticStripeReaderCapabilities:
{
// PosMagneticStripeReaderCapabilitiesType result
// These capabilities are likely hard-coded for the specific device. In this case, example values are provided and should
// be replaced with values that match your hardware
PosMagneticStripeReaderCapabilitiesType capabilities;
capabilities.PowerReportingType = DriverUnifiedPosPowerReportingType::Standard;
capabilities.IsStatisticsReportingSupported = TRUE;
capabilities.IsStatisticsUpdatingSupported = TRUE;
capabilities.CardAuthenticationLength = 0;
capabilities.SupportedEncryptionAlgorithms = MsrDataEncryption::MsrDataEncryption_AES;
capabilities.AuthenticationLevel = DriverMagneticStripeReaderAuthenticationLevel::Optional;
capabilities.IsIsoSupported = TRUE;
capabilities.IsJisOneSupported = TRUE;
capabilities.IsJisTwoSupported = TRUE;
capabilities.IsTrackDataMaskingSupported = TRUE;
capabilities.IsTransmitSentinelsSupported = TRUE;
size_t bytesToCopy = sizeof(PosMagneticStripeReaderCapabilitiesType);
if (outputBufferLength < bytesToCopy)
{
bytesToCopy = outputBufferLength;
status = STATUS_BUFFER_OVERFLOW;
}
memcpy(outputBuffer, &capabilities, bytesToCopy);
*Information = bytesToCopy;
}
break;
case PosPropertyId::MagneticStripeReaderSupportedCardTypes:
{
// Supported card types. The API understands Bank and AAMVA, but additional, device-specific values can be added as well
// This property is typically hard coded based on the device type
MSR_SUPPORTED_CARD_TYPES supportedCardTypes;
RtlZeroMemory(&supportedCardTypes, sizeof(MSR_SUPPORTED_CARD_TYPES));
supportedCardTypes.Count = 2;
supportedCardTypes.CardTypes[0] = (unsigned int)MsrCardType::MsrCardType_Bank;
supportedCardTypes.CardTypes[1] = (unsigned int)MsrCardType::MsrCardType_Aamva;
size_t bytesToCopy = sizeof(MSR_SUPPORTED_CARD_TYPES);
if (outputBufferLength < bytesToCopy)
{
bytesToCopy = outputBufferLength;
status = STATUS_BUFFER_OVERFLOW;
}
memcpy(outputBuffer, &supportedCardTypes, bytesToCopy);
*Information = bytesToCopy;
}
break;
case PosPropertyId::MagneticStripeReaderDeviceAuthenticationProtocol:
{
// Returns whether the device supports challenge/response authentication or not.
*((MsrAuthenticationProtocolType*)outputBuffer) = MsrAuthenticationProtocolType::MsrAuthenticationProtocolType_ChallengeResponse;
*Information = sizeof(MsrAuthenticationProtocolType);
}
break;
case PosPropertyId::MagneticStripeReaderErrorReportingType:
{
// Returns whether the device should report errors at the card or track level.
// This value is typically retrieved from the device context based on a prior SetProperty(MagneticStripeReaderErrorReportingType)
*((MsrErrorReportingType*)outputBuffer) = MsrErrorReportingType::MsrErrorReportingType_CardLevel;
*Information = sizeof(MsrErrorReportingType);
}
break;
case PosPropertyId::MagneticStripeReaderTracksToRead:
{
// typically this value is saved in the device context to track which tracks the application wants to read from the card
MsrTrackIds tracksToRead = (MsrTrackIds)(MsrTrackIds::MsrTrackIds_Track1 | MsrTrackIds::MsrTrackIds_Track2);
*((MsrTrackIds*)outputBuffer) = tracksToRead;
*Information = sizeof(MsrTrackIds);
}
break;
case PosPropertyId::MagneticStripeReaderIsTransmitSentinelsEnabled:
{
// BOOL property. True if the data that is sent to the application will include start/end sentinals or not
// This property can return STATUS_NOT_SUPPORTED if the IsTransmitSentinelsSupported capability is FALSE
// Otherwise, the boolean that's returned should be captured from the device context or from the device itself
BOOL isTransmitSentinelsEnabled = TRUE;
*((BOOL*)outputBuffer) = isTransmitSentinelsEnabled;
*Information = sizeof(BOOL);
}
break;
case PosPropertyId::MagneticStripeReaderIsDeviceAuthenticated:
{
// BOOL property. True if the the authentication process has been successful
// This value should come from the device context or from the device itself
BOOL isAuthenticated = TRUE;
*((BOOL*)outputBuffer) = isAuthenticated;
*Information = sizeof(BOOL);
}
break;
case PosPropertyId::MagneticStripeReaderDataEncryptionAlgorithm:
{
// Returns the encryption algorithm used to decrypt the card data
// If the device supports multiple algorithms, it should store the current one in the device context and use that value here
// (or retrieve the value from the device itself)
MsrDataEncryption currentEncryptionAlgorithm = MsrDataEncryption::MsrDataEncryption_AES;
*((MsrDataEncryption*)outputBuffer) = currentEncryptionAlgorithm;
}
break;
default:
// no other readable properties for magnetic stripe reader
return STATUS_INVALID_PARAMETER;
}
return STATUS_SUCCESS;
}
/*
** Driver TODO: Add code to handle various set-property cases.
**
** Implement this function to handle property set requests.
*/
NTSTATUS ProcessSetPropertyRequest(_In_ WDFREQUEST Request, _In_ size_t InputBufferLength, _Inout_ ULONG_PTR* Information)
{
if (Information == nullptr || Request == nullptr || InputBufferLength < sizeof(PosPropertyId))
{
return STATUS_INVALID_PARAMETER;
}
// POS properties are identified by the property ID that's transmitted in the input buffer.
// The data that is used to set the property immediately follows the property ID, so the input buffer must be big enough to contain both.
PosPropertyId* propertyId;
NTSTATUS status = WdfRequestRetrieveInputBuffer(
Request,
sizeof(PosPropertyId),
reinterpret_cast<PVOID*>(&propertyId),
nullptr
);
if (!NT_SUCCESS(status))
{
return status;
}
size_t argumentLength = InputBufferLength - sizeof(PosPropertyId);
void* argumentData = (void*)(propertyId + 1);
// Handle this set of writable properties
switch (*propertyId)
{
case PosPropertyId::IsEnabled:
// BOOL value
if (argumentLength >= sizeof(BOOL))
{
// The driver should use this value to ensure the device is ready to take data.
// The value may also need to be cached in a device context object so that it can be returned in GetProperty(IsEnabled)
BOOL isEnabled = *((BOOL*)argumentData);
UNREFERENCED_PARAMETER(isEnabled);
}
else
{
status = STATUS_BUFFER_TOO_SMALL;
}
break;
case PosPropertyId::IsDisabledOnDataReceived:
// BOOL value
if (argumentLength >= sizeof(BOOL))
{
// Typically this value will get cached in the device context so that, when
// an MSR read occurs, the driver can disable the device.
BOOL isDisabledOnDataReceived = *((BOOL*)argumentData);
UNREFERENCED_PARAMETER(isDisabledOnDataReceived);
}
else
{
status = STATUS_BUFFER_TOO_SMALL;
}
break;
case PosPropertyId::MagneticStripeReaderIsDecodeDataEnabled:
// BOOL value
if (argumentLength >= sizeof(BOOL))
{
// Typically this value will get cached in the device context so that, when
// an MSR read occurs, the driver can decode the raw data to get the scan data label.
BOOL isDecodeDataEnabled = *((BOOL*)argumentData);
UNREFERENCED_PARAMETER(isDecodeDataEnabled);
}
else
{
status = STATUS_BUFFER_TOO_SMALL;
}
break;
case PosPropertyId::MagneticStripeReaderErrorReportingType:
// MsrErrorReportingType value
if (argumentLength >= sizeof(MsrErrorReportingType))
{
// Typically this value will get cached in the device context so that, when
// a failed MSR read occurs, the driver can report the error correctly
MsrErrorReportingType errorReporting = *((MsrErrorReportingType*)argumentData);
UNREFERENCED_PARAMETER(errorReporting);
}
else
{
status = STATUS_BUFFER_TOO_SMALL;
}
break;
case PosPropertyId::MagneticStripeReaderTracksToRead:
// MsrTrackIds value
if (argumentLength >= sizeof(MsrErrorReportingType))
{
// This value will either be sent to the device to limit the tracks that are read,
// or cached in the device context so that the driver can only report the given tracks
// during an MSR read.
MsrTrackIds tracksToRead = *((MsrTrackIds*)argumentData);
UNREFERENCED_PARAMETER(tracksToRead);
}
else
{
status = STATUS_BUFFER_TOO_SMALL;
}
break;
case PosPropertyId::MagneticStripeReaderIsTransmitSentinelsEnabled:
// BOOL value
if (argumentLength >= sizeof(BOOL))
{
// This value will either be sent to the device to enable or disable sending sentinel data,
// or cached in the device context so that the driver can insert or remove the sentinel data during an MSR read.
BOOL isTransmitSentinelsEnabled = *((BOOL*)argumentData);
UNREFERENCED_PARAMETER(isTransmitSentinelsEnabled);
}
else
{
status = STATUS_BUFFER_TOO_SMALL;
}
break;
case PosPropertyId::MagneticStripeReaderDataEncryptionAlgorithm:
// MsrDataEncryption value
if (argumentLength >= sizeof(MsrDataEncryption))
{
// This property may be rejected if the SupportedEncryptionAlgorithms capability indicates that no encryption is supported.
// Otherwise it should set the current decryption algorithm (either by sending it to the device or by caching it for use
// by the driver during an MSR read).
MsrDataEncryption encryptionAlgorithm = *((MsrDataEncryption*)argumentData);
UNREFERENCED_PARAMETER(encryptionAlgorithm);
}
else
{
status = STATUS_BUFFER_TOO_SMALL;
}
break;
default:
// no other writable properties for magnetic stripe reader
return STATUS_INVALID_PARAMETER;
}
return STATUS_SUCCESS;
}
/*
** Driver TODO: Replace the data in the ProcessRetrieveStatisticsRequest with your own statistics data
**
** Implement this function to handle retrieve statistics requests.
*/
NTSTATUS ProcessRetrieveStatisticsRequest(_In_ WDFREQUEST Request, _In_ size_t OutputBufferLength, _Inout_ ULONG_PTR* Information)
{
if (Information == nullptr || Request == nullptr)
{
return STATUS_INVALID_PARAMETER;
}
struct
{
PosStatisticsHeader Header;
PosValueStatisticsEntry Entries[1];
} StatisticsData;
// Initialize statics data so header data are identical for the same requested
RtlZeroMemory(&StatisticsData, sizeof(StatisticsData));
StatisticsData.Header.DataLength = sizeof(StatisticsData);
wcscpy_s(StatisticsData.Header.DeviceInformation.DeviceCategory, L"MSR");
wcscpy_s(StatisticsData.Header.DeviceInformation.FirmwareRevision, L"<eg, 1.1>");
wcscpy_s(StatisticsData.Header.DeviceInformation.InstallationDate, L"<installation date>");
wcscpy_s(StatisticsData.Header.DeviceInformation.Interface, L"<eg, USB>");
wcscpy_s(StatisticsData.Header.DeviceInformation.ManufactureDate, L"<eg, 2015/03/17>");
wcscpy_s(StatisticsData.Header.DeviceInformation.ManufacturerName, L"<eg, Conteso>");
wcscpy_s(StatisticsData.Header.DeviceInformation.MechanicalRevision, L"<eg, 2.0a>");
wcscpy_s(StatisticsData.Header.DeviceInformation.ModelName, L"<eg, MSR Model M>");
wcscpy_s(StatisticsData.Header.DeviceInformation.SerialNumber, L"<eg, 12345>");
wcscpy_s(StatisticsData.Header.DeviceInformation.UnifiedPOSVersion, L"1.14");
StatisticsData.Header.EntryCount = 1;
wcscpy_s(StatisticsData.Entries[0].EntryName, L"<device specific statistics value>");
StatisticsData.Entries[0].Value = (LONG)1;
// This IOCTL is called twice by the Windows.Devices.PointOfService APIs
// The first time will just retrieve the header to determine how big the buffer needs to be.
PVOID outputBuffer;
NTSTATUS status = WdfRequestRetrieveOutputBuffer(
Request,
sizeof(PosStatisticsHeader),
&outputBuffer,
nullptr
);
if (!NT_SUCCESS(status))
{
*Information = sizeof(StatisticsData);
return status;
}
if (OutputBufferLength == sizeof(PosStatisticsHeader))
{
memcpy(outputBuffer, &(StatisticsData.Header), sizeof(PosStatisticsHeader));
*Information = sizeof(PosStatisticsHeader);
}
else if (OutputBufferLength < sizeof(StatisticsData))
{
*Information = sizeof(StatisticsData);
status = STATUS_BUFFER_TOO_SMALL;
}
else
{
memcpy(outputBuffer, &StatisticsData, sizeof(StatisticsData));
*Information = sizeof(StatisticsData);
}
return status;
}
/*
** Driver TODO: loop over statisticsEntry[0]...statisticsEntry[inputBuffer->EntryCount - 1] and reset each statistics value named
**
** Implement this function to handle statistics reset requests.
*/
NTSTATUS ProcessResetStatisticsRequest(_In_ WDFREQUEST Request)
{
if (Request == nullptr)
{
return STATUS_INVALID_PARAMETER;
}
// The input buffer must be PosStatisticsHeader followed by one or more PosValueStatisticsEntry (where the value is ignored, just the name is used to
// reset the statistics value).
PosStatisticsHeader* inputBuffer;
size_t totalLength;
NTSTATUS status = WdfRequestRetrieveInputBuffer(
Request,
sizeof(PosStatisticsHeader),
(PVOID*)&inputBuffer,
&totalLength);
if (!NT_SUCCESS(status))
{
return status;
}
if (inputBuffer->DataLength > totalLength)
{
return STATUS_BUFFER_TOO_SMALL;
}
size_t entryLength = inputBuffer->DataLength - sizeof(PosStatisticsHeader);
if (
entryLength % sizeof(PosValueStatisticsEntry) ||
(entryLength / sizeof(PosValueStatisticsEntry)) != inputBuffer->EntryCount ||
inputBuffer->EntryCount == 0
)
{
return STATUS_INVALID_PARAMETER;
}
PosValueStatisticsEntry* statisticsEntry = (PosValueStatisticsEntry*) (inputBuffer + 1);
for (UINT32 index = 0; index < inputBuffer->EntryCount; ++index)
{
// reset this value:
statisticsEntry[index].EntryName;
}
return STATUS_SUCCESS;
}
/*
** Driver TODO: loop over statisticsEntry[0]...statisticsEntry[inputBuffer->EntryCount - 1] and update each statistics value named
**
** Implement this function to handle statistics update requests.
*/
NTSTATUS ProcessUpdateStatisticsRequest(_In_ WDFREQUEST Request)
{
if (Request == nullptr)
{
return STATUS_INVALID_PARAMETER;
}
// The input buffer must be PosStatisticsHeader followed by one or more PosValueStatisticsEntry
PosStatisticsHeader* inputBuffer;
size_t totalLength;
NTSTATUS status = WdfRequestRetrieveInputBuffer(
Request,
sizeof(PosStatisticsHeader),
(PVOID*)&inputBuffer,
&totalLength);
if (!NT_SUCCESS(status))
{
return status;
}
if (inputBuffer->DataLength > totalLength)
{
return STATUS_BUFFER_TOO_SMALL;
}
size_t entryLength = inputBuffer->DataLength - sizeof(PosStatisticsHeader);
if (
entryLength % sizeof(PosValueStatisticsEntry) ||
(entryLength / sizeof(PosValueStatisticsEntry)) != inputBuffer->EntryCount ||
inputBuffer->EntryCount == 0
)
{
return STATUS_INVALID_PARAMETER;
}
PosValueStatisticsEntry* statisticsEntry = (PosValueStatisticsEntry*)(inputBuffer + 1);
for (UINT32 index = 0; index < inputBuffer->EntryCount; ++index)
{
// update the statistics entry:
statisticsEntry[index].EntryName;
// with the value:
statisticsEntry[index].Value;
}
return STATUS_SUCCESS;
}
/*
** Driver TODO: Add code to ProcessCheckHealthRequest to handle different health check cases. The result should be a localized string that is returned to the user in the output buffer of the IOCTL.
**
** Implement this function to handle health check requests.
*/
NTSTATUS ProcessCheckHealthRequest(_In_ WDFREQUEST Request, _Inout_ ULONG_PTR* Information)
{
if (Information == nullptr || Request == nullptr)
{
return STATUS_INVALID_PARAMETER;
}
DriverUnifiedPosHealthCheckLevel* level;
NTSTATUS status = WdfRequestRetrieveInputBuffer(
Request,
sizeof(DriverUnifiedPosHealthCheckLevel),
(PVOID*)&level,
nullptr);
if (!NT_SUCCESS(status))
{
return status;
}
PosStringType* outputBuffer;
size_t outputBufferLength;
status = WdfRequestRetrieveOutputBuffer(
Request,
sizeof(PosStringType),
(void**)(&outputBuffer),
&outputBufferLength
);
if (!NT_SUCCESS(status))
{
return status;
}
switch (*level)
{
case DriverUnifiedPosHealthCheckLevel::POSInternal:
case DriverUnifiedPosHealthCheckLevel::External:
case DriverUnifiedPosHealthCheckLevel::Interactive:
{
// Handle the specific health check level, depending on the applicability to your device.
// Return the result as a string that the user can use to determine whether the device is
// operational or needs attention.
LPCWSTR result = L"OK";
size_t lengthInBytes = wcslen(result) * sizeof(WCHAR);
status = RtlSizeTToUInt32(lengthInBytes, &(outputBuffer->DataLengthInBytes));
if (NT_SUCCESS(status))
{
*Information = sizeof(PosStringType);
if (outputBufferLength >= sizeof(PosStringType)+outputBuffer->DataLengthInBytes)
{
void* outputStringStart = (void*)(outputBuffer + 1);
memcpy(outputStringStart, result, outputBuffer->DataLengthInBytes);
*Information += outputBuffer->DataLengthInBytes;
}
else
{
status = STATUS_BUFFER_OVERFLOW;
}
}
}
break;
default:
return STATUS_INVALID_PARAMETER;
}
return STATUS_SUCCESS;
}
/*
** Driver TODO:
**
** Implement this function to handle the initial handshake IOCTL for Windows.Devices.PointOfService API <-> Driver communication.
** This sample will likely work for most cases.
*/
NTSTATUS ProcessGetDeviceBasicsRequest(_In_ WDFREQUEST Request, _Inout_ ULONG_PTR* Information)
{
if (Information == nullptr || Request == nullptr)
{
return STATUS_INVALID_PARAMETER;
}
UINT32* runtimeVersion;
PosDeviceBasicsType* outputData;
NTSTATUS status = WdfRequestRetrieveInputBuffer(
Request,
sizeof(UINT32),
(PVOID*)&runtimeVersion,
nullptr);
if (!NT_SUCCESS(status))
{
return status;
}
status = WdfRequestRetrieveOutputBuffer(
Request,
sizeof(PosDeviceBasicsType),
(PVOID*)&outputData,
nullptr);
if (!NT_SUCCESS(status))
{
return status;
}
// Tell the runtime what version of the POS IOCTL interface this driver supports so that
// it won't send IOCTLs that the driver doesn't support.
outputData->Version = POS_DRIVER_VERSION;
// This is a magnetic stripe reader driver
outputData->DeviceType = PosDeviceType::PosDeviceType_MagneticStripeReader;
// This value will be used to set the initial ReadFile buffer size. A small size that is
// likely to cover most of the data events is suggested. The runtime will grow the ReadFile
// buffer size as needed.
outputData->RecommendedBufferSize = 128;
*Information = sizeof(PosDeviceBasicsType);
return STATUS_SUCCESS;
}
|