summaryrefslogtreecommitdiff
path: root/general/toaster/toastDrv/kmdf/func/featured/wmi.c
blob: f62eacd3babf8b8dc313d756b16496003daeaf64 (plain)
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
/*++

Copyright (c) Microsoft Corporation.  All rights reserved.

    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
    KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
    PURPOSE.

Module Name:

    WMI.C

Abstract:

    This module handles WMI support for the featured WDF toaster driver.

Environment:

    Kernel mode

--*/

#include "toaster.h"
#include "trace.h"
#include <wmilib.h>
#include <wmistr.h>
#include <ToasterMof.h>

//
// This tmh is generated by the WPP Preprocessor
//
#include "wmi.tmh"

static
NTSTATUS
GetDeviceFriendlyName(
    _In_  WDFDEVICE Device,
    _Out_ WDFMEMORY* DeviceName
    );

static
ULONG
ToasterHelperFunction1(
    _In_ ULONG InData
    );

static
ULONG
ToasterHelperFunction2(
    _In_ ULONG InData1,
    _In_ ULONG InData2
    );

static
VOID
ToasterHelperFunction3(
    _In_  ULONG InData1,
    _In_  ULONG InData2,
    _Out_ PULONG OutData1,
    _Out_ PULONG OutData2
    );


#define ToasterDeviceInformation_SIZE UFIELD_OFFSET(ToasterDeviceInformation, VariableData)

WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(ToasterDeviceInformation, ToasterWmiGetData)
WDF_DECLARE_CONTEXT_TYPE_WITH_NAME(ToasterControl, ToasterWmiGetControlData)


#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, ToasterWmiRegistration)
#pragma alloc_text(PAGE, EvtWmiInstanceStdDeviceDataQueryInstance)
#pragma alloc_text(PAGE, EvtWmiInstanceStdDeviceDataSetInstance)
#pragma alloc_text(PAGE, EvtWmiInstanceStdDeviceDataSetItem)
#pragma alloc_text(PAGE, EvtWmiInstanceToasterControlQueryInstance)
#pragma alloc_text(PAGE, EvtWmiInstanceToasterControlExecuteMethod)
#pragma alloc_text(PAGE, EvtWmiInstanceToasterControlSetInstance)
#pragma alloc_text(PAGE, ToasterHelperFunction1)
#pragma alloc_text(PAGE, ToasterHelperFunction2)
#pragma alloc_text(PAGE, ToasterHelperFunction3)
#endif

//
// Global debug error level
//

extern ULONG DebugLevel;

NTSTATUS
ToasterWmiRegistration(
    _In_ WDFDEVICE Device
    )

/*++

Routine Description

    Registers with WMI as a data provider for this instance of the device.

Arguments:

    Device - The Framework device object for which the WMI provider instances
        are to be created and registered. This device object will be the parent
        object of the new WMI instance objects.

Return Value:

    NT Status code.

--*/

{
    NTSTATUS status;
    PFDO_DATA fdoData;
    PToasterDeviceInformation pData;
    PToasterControl controlData;

    WDFWMIINSTANCE instance;
    WDF_OBJECT_ATTRIBUTES woa;
    WDF_WMI_PROVIDER_CONFIG providerConfig;
    WDF_WMI_INSTANCE_CONFIG instanceConfig;
    DECLARE_CONST_UNICODE_STRING(mofResourceName, MOFRESOURCENAME);

    PAGED_CODE();

    fdoData = ToasterFdoGetData(Device);

    //
    // Register the MOF resource names of any customized WMI data providers
    // that are not defined in wmicore.mof.
    //
    status = WdfDeviceAssignMofResourceName(Device, &mofResourceName);
    if (!NT_SUCCESS(status)) {

        WppPrintDeviceError(fdoData->WppRecorderLog,
                            "[Toaster] Status = 0x%08x, WdfDeviceAssignMofResourceName failed\n",
                            status);
        return status;
    }

    //
    // Initialize the config structures for the Provider and the Instance and
    // define event callback functions that support a WMI client's request to
    // access the driver's WMI data blocks.
    //

    WDF_WMI_PROVIDER_CONFIG_INIT(&providerConfig, &ToasterDeviceInformation_GUID);

    //
    // Specify minimum expected buffer size for query and set instance requests.
    // Since the query block size is different than the set block size, set it
    // to zero and manually check for the buffer size for each operation.
    //
    providerConfig.MinInstanceBufferSize = 0;

    //
    // The WDFWMIPROVIDER handle is needed if multiple instances for the provider
    // has to be created or if the instances have to be created sometime after
    // the provider is created. In case below, the provider handle is not needed
    // because only one instance is needed and can be created when the provider
    // is created.
    //
    WDF_WMI_INSTANCE_CONFIG_INIT_PROVIDER_CONFIG(&instanceConfig, &providerConfig);

    //
    // Create the Provider object as part of the Instance creation call by setting
    // the Register value in the Instance Config to TRUE. This eliminates the
    // need to call WdfWmiProviderRegister.
    //
    instanceConfig.Register = TRUE;

    instanceConfig.EvtWmiInstanceQueryInstance = EvtWmiInstanceStdDeviceDataQueryInstance;
    instanceConfig.EvtWmiInstanceSetInstance   = EvtWmiInstanceStdDeviceDataSetInstance;
    instanceConfig.EvtWmiInstanceSetItem       = EvtWmiInstanceStdDeviceDataSetItem;

    WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&woa, ToasterDeviceInformation);

    //
    // Create the WMI instance object for this data block.
    //
    status = WdfWmiInstanceCreate(Device,
                                  &instanceConfig,
                                  &woa,
                                  &instance);
    if (!NT_SUCCESS(status)) {

        WppPrintDeviceError(fdoData->WppRecorderLog,
                            "[Toaster] Status = 0x%08x, WdfWmiInstanceCreate failed\n",
                            status);
        return status;
    }

    pData = ToasterWmiGetData(instance);

    pData->ConnectorType = TOASTER_WMI_STD_USB;
    pData->Capacity = 2000;
    pData->ErrorCount = 0;
    pData->Controls = 5;
    pData->DebugPrintLevel = DebugLevel;

    WDF_WMI_PROVIDER_CONFIG_INIT(&providerConfig, &TOASTER_NOTIFY_DEVICE_ARRIVAL_EVENT);
    providerConfig.Flags = WdfWmiProviderEventOnly;

    //
    // Specify minimum expected buffer size for query and set instance requests.
    // Since the query block size is different than the set block size, set it
    // to zero and manually check for the buffer size for each operation.
    //
    providerConfig.MinInstanceBufferSize = 0;

    WDF_WMI_INSTANCE_CONFIG_INIT_PROVIDER_CONFIG(&instanceConfig, &providerConfig);
    instanceConfig.Register = TRUE;

    //
    // Create the WMI instance object for this data block.
    //
    status = WdfWmiInstanceCreate(Device,
                                  &instanceConfig,
                                  WDF_NO_OBJECT_ATTRIBUTES,
                                  &fdoData->WmiDeviceArrivalEvent);

    if (!NT_SUCCESS(status)) {

        WppPrintDeviceError(fdoData->WppRecorderLog,
                            "[Toaster] Status = 0x%08x, WdfWmiInstanceCreate failed\n",
                            status);
        return status;
    }


    //
    // Register the Toaster Control class.
    //

    //
    // Initialize the config structures for the Provider and the Instance and
    // define event callback functions that support a WMI client's request to
    // access the driver's WMI data blocks.
    //

    WDF_WMI_PROVIDER_CONFIG_INIT(&providerConfig, &ToasterControl_GUID);

    //
    // Specify minimum expected buffer size for query and set instance requests.
    //
    providerConfig.MinInstanceBufferSize = ToasterControl_SIZE;

    //
    // The WDFWMIPROVIDER handle is needed if multiple instances for the provider
    // has to be created or if the instances have to be created sometime after
    // the provider is created. In case below, the provider handle is not needed
    // because only one instance is needed and can be created when the provider
    // is created.
    //
    WDF_WMI_INSTANCE_CONFIG_INIT_PROVIDER_CONFIG(&instanceConfig, &providerConfig);

    //
    // Create the Provider object as part of the Instance creation call by setting
    // the Register value in the Instance Config to TRUE. This eliminates the
    // need to call WdfWmiProviderRegister.
    //
    instanceConfig.Register = TRUE;

    instanceConfig.EvtWmiInstanceQueryInstance = EvtWmiInstanceToasterControlQueryInstance;
    instanceConfig.EvtWmiInstanceSetInstance   = EvtWmiInstanceToasterControlSetInstance;
    instanceConfig.EvtWmiInstanceSetItem       = EvtWmiInstanceToasterControlSetItem;
    instanceConfig.EvtWmiInstanceExecuteMethod = EvtWmiInstanceToasterControlExecuteMethod;

    WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&woa, ToasterControl);

    //
    // Create the WMI instance object for this data block.
    //
    status = WdfWmiInstanceCreate(Device,
                                  &instanceConfig,
                                  &woa,
                                  &instance);
    if (!NT_SUCCESS(status)) {

        WppPrintDeviceError(fdoData->WppRecorderLog,
                            "[Toaster] Status = 0x%08x, WdfWmiInstanceCreate failed\n",
                            status);
        return status;
    }

    controlData = ToasterWmiGetControlData(instance);
    controlData->ControlValue = 25;

    return status;
}

//
// WMI System Call back functions
//

NTSTATUS
EvtWmiInstanceStdDeviceDataQueryInstance(
    _In_  WDFWMIINSTANCE WmiInstance,
    _In_  ULONG OutBufferSize,
    _Out_writes_bytes_to_(OutBufferSize, *BufferUsed) PVOID OutBuffer,
    _Out_ PULONG BufferUsed
    )
{
    PUCHAR          pBuf;
    ULONG           size;
    UNICODE_STRING  string;
    NTSTATUS        status;

    PAGED_CODE();

    string.Buffer = L"Aishwarya\0\0";
    string.Length = (USHORT) (wcslen(string.Buffer) + 1) * sizeof(WCHAR);
    string.MaximumLength = string.Length + sizeof(UNICODE_NULL);

    //
    // A USHORT value is needed to contain the length of the buffer in the data
    //
    size = ToasterDeviceInformation_SIZE + string.Length + sizeof(USHORT);

    *BufferUsed = size;

    if (OutBufferSize < size) {
        return STATUS_BUFFER_TOO_SMALL;
    }

    pBuf = (PUCHAR) OutBuffer;

    //
    // Copy the structure information
    //
    RtlCopyMemory(pBuf,
                  ToasterWmiGetData(WmiInstance),
                  ToasterDeviceInformation_SIZE);

    pBuf += ToasterDeviceInformation_SIZE;

    //
    // Copy the string. Put length of string ahead of string.
    //
    status = WDF_WMI_BUFFER_APPEND_STRING(pBuf,
                                          size - ToasterDeviceInformation_SIZE,
                                          &string,
                                          &size);

    return status;
}


NTSTATUS
EvtWmiInstanceStdDeviceDataSetInstance(
    _In_ WDFWMIINSTANCE WmiInstance,
    _In_ ULONG InBufferSize,
    _In_reads_bytes_(InBufferSize) PVOID InBuffer
    )
{
    PAGED_CODE();

    if (InBufferSize < ToasterDeviceInformation_SIZE) {
        return STATUS_BUFFER_TOO_SMALL;
    }

    //
    // We will update only writable elements.
    //
    DebugLevel = ToasterWmiGetData(WmiInstance)->DebugPrintLevel =
        ((PToasterDeviceInformation)InBuffer)->DebugPrintLevel;

    return STATUS_SUCCESS;
}


NTSTATUS
EvtWmiInstanceStdDeviceDataSetItem(
    _In_ WDFWMIINSTANCE WmiInstance,
    _In_ ULONG DataItemId,
    _In_ ULONG InBufferSize,
    _In_reads_bytes_(InBufferSize) PVOID InBuffer
    )
{
    PAGED_CODE();

    if (DataItemId == ToasterDeviceInformation_DebugPrintLevel_ID) {
        if (InBufferSize < sizeof(ULONG)) {
            return STATUS_BUFFER_TOO_SMALL;
        }

        DebugLevel = ToasterWmiGetData(WmiInstance)->DebugPrintLevel =
            *((PULONG)InBuffer);

        return STATUS_SUCCESS;

    } else {
        return STATUS_WMI_READ_ONLY;
    }
}


NTSTATUS
ToasterFireArrivalEvent(
    _In_ WDFDEVICE Device
    )
{
    WDFMEMORY memory;
    PWNODE_SINGLE_INSTANCE  wnode;
    ULONG                   wnodeSize;
    ULONG                   wnodeDataBlockSize;
    ULONG                   wnodeInstanceNameSize;
    ULONG                   size;
    ULONG                   length;
    UNICODE_STRING          deviceName;
    UNICODE_STRING          modelName;
    NTSTATUS                status;
    PFDO_DATA               fdoData;

    fdoData = ToasterFdoGetData(Device);

    //
    // *NOTE*
    // WdfWmiFireEvent only fires single instance events at the moment so
    // continue to use this method of firing events
    // *NOTE*
    //

    RtlInitUnicodeString(&modelName, L"Sonali\0\0");
    //
    // Get the device name.
    //
    status = GetDeviceFriendlyName(Device, &memory);

    if (!NT_SUCCESS(status)) {
        return status;
    }

    RtlInitUnicodeString(&deviceName, (PWSTR) WdfMemoryGetBuffer(memory, NULL));

    //
    // Determine the amount of wnode information we need.
    //
    wnodeSize = sizeof(WNODE_SINGLE_INSTANCE);
    wnodeInstanceNameSize = deviceName.Length + sizeof(USHORT);
    wnodeDataBlockSize = modelName.Length + sizeof(USHORT);

    size = wnodeSize + wnodeInstanceNameSize + wnodeDataBlockSize;

    //
    // Allocate memory for the WNODE from NonPagedPoolNx
    //
    wnode = ExAllocatePool2(POOL_FLAG_NON_PAGED, size, TOASTER_POOL_TAG);

    if (NULL != wnode) {
        wnode->WnodeHeader.BufferSize = size;
        wnode->WnodeHeader.ProviderId =
                            IoWMIDeviceObjectToProviderId(
                                    WdfDeviceWdmGetDeviceObject(Device));
        wnode->WnodeHeader.Version = 1;
        KeQuerySystemTime(&wnode->WnodeHeader.TimeStamp);

        RtlCopyMemory(&wnode->WnodeHeader.Guid,
                      &TOASTER_NOTIFY_DEVICE_ARRIVAL_EVENT,
                      sizeof(GUID));

        //
        // *NOTE*
        // WdfWmiFireEvent only fires single instance events at the moment so
        // continue to use this method of firing events
        // *NOTE*
        //

        //
        // Set flags to indicate that you are creating dynamic instance names.
        // The reason we chose to do dynamic instance is becuase we can fire
        // the events anytime. If we do static instance names, we can only
        // fire events after WMI queries for IRP_MN_REGINFO, which happens
        // after the device has been started. Another point to note is that if you
        // are firing an event after the device is started, you should
        // check whether the event is enabled, because that
        // indicates that someone is interested in receiving the event.
        // Why fire an event when nobody is interested and waste
        // system resources?
        //

        wnode->WnodeHeader.Flags = WNODE_FLAG_EVENT_ITEM |
                                    WNODE_FLAG_SINGLE_INSTANCE;

        wnode->OffsetInstanceName = wnodeSize;
        wnode->DataBlockOffset = wnodeSize + wnodeInstanceNameSize;
        wnode->SizeDataBlock = wnodeDataBlockSize;

        //
        // Write the instance name
        //
        size -= wnodeSize;
        status = WDF_WMI_BUFFER_APPEND_STRING(
            WDF_PTR_ADD_OFFSET(wnode, wnode->OffsetInstanceName),
            size,
            &deviceName,
            &length
            );

        //
        // Size was precomputed, this should never fail
        //
        ASSERT(NT_SUCCESS(status));


        //
        // Write the data, which is the model name as a string
        //
        size -= wnodeInstanceNameSize;
        WDF_WMI_BUFFER_APPEND_STRING(
            WDF_PTR_ADD_OFFSET(wnode,  wnode->DataBlockOffset),
            size,
            &modelName,
            &length
            );

        //
        // Size was precomputed, this should never fail
        //
        ASSERT(NT_SUCCESS(status));

        //
        // Indicate the event to WMI. WMI will take care of freeing
        // the WMI struct back to pool.
        //
        status = IoWMIWriteEvent(wnode);

        if (!NT_SUCCESS(status)) {
            WppPrintDeviceError(fdoData->WppRecorderLog,
                                "IoWMIWriteEvent failed %x\n",
                                status);
            ExFreePool(wnode);
        }
    }
    else {
        status = STATUS_INSUFFICIENT_RESOURCES;
    }

    //
    // Free the memory allocated by GetDeviceFriendlyName function.
    //
    WdfObjectDelete(memory);

    return status;
}


NTSTATUS
GetDeviceFriendlyName(
    _In_    WDFDEVICE Device,
    _Out_   WDFMEMORY* DeviceName
    )

/*++

Routine Description:

    Return the friendly name associated with the given device object.

Arguments:

Return Value:

    NT status

--*/

{
    NTSTATUS status;
    PFDO_DATA fdoData;

    fdoData = ToasterFdoGetData(Device);

    //
    // First get the length of the string. If the FriendlyName
    // is not there then get the lenght of device description.
    //
    status = WdfDeviceAllocAndQueryProperty(Device,
                                            DevicePropertyFriendlyName,
                                            NonPagedPoolNx,
                                            WDF_NO_OBJECT_ATTRIBUTES,
                                            DeviceName);

    if (!NT_SUCCESS(status) && status != STATUS_INSUFFICIENT_RESOURCES) {
        status = WdfDeviceAllocAndQueryProperty(Device,
                                                DevicePropertyDeviceDescription,
                                                NonPagedPoolNx,
                                                WDF_NO_OBJECT_ATTRIBUTES,
                                                DeviceName);

    }

    if (!NT_SUCCESS(status)) {
        WppPrintDeviceError(fdoData->WppRecorderLog,
                            "WdfDeviceQueryProperty returned %x\n",
                            status);
    }

    return status;
}


NTSTATUS
EvtWmiInstanceToasterControlQueryInstance(
    _In_  WDFWMIINSTANCE WmiInstance,
    _In_  ULONG OutBufferSize,
    _Out_writes_bytes_to_(OutBufferSize, *BufferUsed) PVOID OutBuffer,
    _Out_ PULONG BufferUsed
    )

/*++

Routine Description:

    This is the callback routine for the WMI Query irp on the Instance representing
    the ToasterControl Class. This method copies the ToasterControl Class data into
    the given buffer.

Arguments:

    WmiInstance - The handle to the WMI instance object.

    OutBufferSize - The size (in bytes) of the output buffer into which the
        instance data is to be copied.

    OutBuffer - Pointer to the output buffer.

    BufferUsed - Pointer to the location that receives the number of bytes that
        were copied into the output buffer.

Return Value:

    NT Status code.

--*/

{
    UNREFERENCED_PARAMETER(OutBufferSize);
    PAGED_CODE();

    //
    // Since the minimum buffer size required for querying the instance data was
    // already specified during the WMI instance setup, the Framework will make
    // sure that the incoming buffer size is large enough for this instance query.
    // There is no need to check the size of the given buffer again. The instance
    // information can be copied directly to the given buffer.
    //
    _Analysis_assume_(ToasterControl_SIZE <= OutBufferSize);
    RtlCopyMemory(OutBuffer,
                  ToasterWmiGetControlData(WmiInstance),
                  ToasterControl_SIZE);
    *BufferUsed = ToasterControl_SIZE;

    return STATUS_SUCCESS;
}


NTSTATUS
EvtWmiInstanceToasterControlSetInstance(
    _In_ WDFWMIINSTANCE WmiInstance,
    _In_ ULONG InBufferSize,
    _In_reads_bytes_(InBufferSize) PVOID InBuffer
    )

/*++

Routine Description:

    This is the callback routine for the WMI Set instance information irp on the
    Instance representing the ToasterControl Class. This method reads the data
    from the input buffer and updates the writable elements of the Instance.

Arguments:

    WmiInstance - The handle to the WMI instance object.

    InBufferSize - The size (in bytes) of the input buffer.

    InBuffer - Pointer to the input buffer containing the data for the instance
        updates.

Return Value:

    NT Status code.

--*/

{
    UNREFERENCED_PARAMETER(InBufferSize);
    PAGED_CODE();

    //
    // Since the minimum buffer size required for setting the instance data was
    // already specified during the WMI instance setup, the Framework will make
    // sure that the incoming buffer size is large enough for this set requrest.
    // There is no need to check the size of the given buffer again. The instance
    // information can be copied directly from the given buffer. Also, note that
    // only the wriable elements need to be copied.
    //
    ToasterWmiGetControlData(WmiInstance)->ControlValue = ((PToasterControl)InBuffer)->ControlValue;

    return STATUS_SUCCESS;
}


NTSTATUS
EvtWmiInstanceToasterControlSetItem(
    _In_ WDFWMIINSTANCE WmiInstance,
    _In_ ULONG DataItemId,
    _In_ ULONG InBufferSize,
    _In_reads_bytes_(InBufferSize) PVOID InBuffer
    )
{
    if (DataItemId == ToasterControl_ControlValue_ID) {
        if (InBufferSize < sizeof(ULONG)) {
            return STATUS_BUFFER_TOO_SMALL;
        }

        ToasterWmiGetControlData(WmiInstance)->ControlValue = *((PULONG)InBuffer);
        return STATUS_SUCCESS;

    } else {
        return STATUS_WMI_READ_ONLY;
    }
}


NTSTATUS
EvtWmiInstanceToasterControlExecuteMethod(
    _In_    WDFWMIINSTANCE WmiInstance,
    _In_    ULONG MethodId,
    _In_    ULONG InBufferSize,
    _In_    ULONG OutBufferSize,
    _When_(InBufferSize >= OutBufferSize, _Inout_updates_bytes_(InBufferSize))
    _When_(InBufferSize < OutBufferSize, _Inout_updates_bytes_(OutBufferSize))
    PVOID Buffer,
    _Out_   PULONG BufferUsed
    )

/*++

Routine Description:

    This routine handles the Execute Method WMI request to the Toaster device.

Arguments:

    WmiInstance - The handle to the WMI instance object.

    MethodId - The method id of the method in ToasterControl class to execute.

    InBufferSize - The size (in bytes) of the input buffer from which the input
        parameter values are read.

    OutBufferSize - The size (in bytes) of the output buffer into which the
        instance data is to be copied.

    Buffer - Pointer to the buffer used for both input and output of data.

    BufferUsed - Pointer to the location that receives the number of bytes that
        were copied into the output buffer.

Return Value:

    NT Status code.

--*/

{
    NTSTATUS status = STATUS_SUCCESS;

    UNREFERENCED_PARAMETER(WmiInstance);
    PAGED_CODE();

    switch (MethodId) {
    case ToasterControl1:
        {
            PToasterControl1_IN  InBuffer  = (PToasterControl1_IN) Buffer;
            PToasterControl1_OUT OutBuffer = (PToasterControl1_OUT)Buffer;

            //
            // Check the size of the input and output buffers.
            //
            if (InBufferSize < ToasterControl1_IN_SIZE) {
                status = STATUS_INVALID_PARAMETER_MIX;
                break;
            }

            if (OutBufferSize < ToasterControl1_OUT_SIZE) {
                status = STATUS_BUFFER_TOO_SMALL;
                break;
            }

            //
            // Call the helper function for the Toaster Method 1.
            //
            OutBuffer->OutData = ToasterHelperFunction1(InBuffer->InData);

            //
            // Update the size of the output buffer used.
            //
            *BufferUsed = ToasterControl1_OUT_SIZE;

        }
        break;

    case ToasterControl2:
        {
            PToasterControl2_IN  InBuffer  = (PToasterControl2_IN) Buffer;
            PToasterControl2_OUT OutBuffer = (PToasterControl2_OUT)Buffer;

            //
            // Check the size of the input and output buffers.
            //
            if (InBufferSize < ToasterControl2_IN_SIZE) {
                status = STATUS_INVALID_PARAMETER_MIX;
                break;
            }

            if (OutBufferSize < ToasterControl2_OUT_SIZE) {
                status = STATUS_BUFFER_TOO_SMALL;
                break;
            }

            //
            // Call the helper function for the Toaster Method 2.
            //
            OutBuffer->OutData = ToasterHelperFunction2(InBuffer->InData1, InBuffer->InData2);

            //
            // Update the size of the output buffer used.
            //
            *BufferUsed = ToasterControl2_OUT_SIZE;

        }
        break;

    case ToasterControl3:
        {
            PToasterControl3_IN  InBuffer  = (PToasterControl3_IN) Buffer;
            PToasterControl3_OUT OutBuffer = (PToasterControl3_OUT)Buffer;

            //
            // Check the size of the input and output buffers.
            //
            if (InBufferSize < ToasterControl3_IN_SIZE) {
                status = STATUS_INVALID_PARAMETER_MIX;
                break;
            }

            if (OutBufferSize < ToasterControl3_OUT_SIZE) {
                status = STATUS_BUFFER_TOO_SMALL;
                break;
            }

            //
            // Call the helper function for the Toaster Method 3.
            //
            ToasterHelperFunction3(InBuffer->InData1,
                                   InBuffer->InData2,
                                   &(OutBuffer->OutData1),
                                   &(OutBuffer->OutData2));

            //
            // Update the size of the output buffer used.
            //
            *BufferUsed = ToasterControl3_OUT_SIZE;

        }
        break;

    default:
        status = STATUS_WMI_ITEMID_NOT_FOUND;
        break;
    }

    return status;
}


ULONG
ToasterHelperFunction1(
    _In_ ULONG InData
    )
{
    PAGED_CODE();

    return (InData + 1);
}


ULONG
ToasterHelperFunction2(
    _In_ ULONG InData1,
    _In_ ULONG InData2
    )
{
    PAGED_CODE();

    return (InData1 + InData2);
}


VOID
ToasterHelperFunction3(
    _In_  ULONG InData1,
    _In_  ULONG InData2,
    _Out_ PULONG OutData1,
    _Out_ PULONG OutData2
    )
{
    PAGED_CODE();

    *OutData1 = InData1 + 1;
    *OutData2 = InData2 + 1;

    return;
}