summaryrefslogtreecommitdiff
path: root/wpd/WpdServiceSampleDriver/WpdServiceCapabilities.cpp
blob: 7f67e199d14ddfb244f37e9c7bef7d542e935809 (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
#include "stdafx.h"

#include "WpdServiceCapabilities.tmh"

WpdServiceCapabilities::WpdServiceCapabilities() : m_pContactsService(NULL)
{

}

WpdServiceCapabilities::~WpdServiceCapabilities()
{

}

HRESULT WpdServiceCapabilities::Initialize(_In_ FakeContactsService* pContactsService)
{
    if (pContactsService == NULL)
    {
        return E_POINTER;
    }
    m_pContactsService = pContactsService;
    return S_OK;
}

HRESULT WpdServiceCapabilities::DispatchWpdMessage(
    _In_    REFPROPERTYKEY         Command,
    _In_    IPortableDeviceValues* pParams,
    _In_    IPortableDeviceValues* pResults)
{
    HRESULT hr = S_OK;
    if (Command.fmtid != WPD_CATEGORY_SERVICE_CAPABILITIES)
    {
        hr = E_INVALIDARG;
        CHECK_HR(hr, "This object does not support this command category %ws",CComBSTR(Command.fmtid));
        return hr;
    }

    if (IsEqualPropertyKey(Command, WPD_COMMAND_SERVICE_CAPABILITIES_GET_SUPPORTED_COMMANDS))
    {
        hr = OnGetSupportedCommands(pParams, pResults);
        CHECK_HR(hr, "Failed to get supported commands");
    }
    else if (IsEqualPropertyKey(Command, WPD_COMMAND_SERVICE_CAPABILITIES_GET_COMMAND_OPTIONS))
    {
        hr = OnGetCommandOptions(pParams, pResults);
        CHECK_HR(hr, "Failed to get command options");
    }
    else if (IsEqualPropertyKey(Command, WPD_COMMAND_SERVICE_CAPABILITIES_GET_SUPPORTED_METHODS))
    {
        hr = OnGetSupportedMethods(pParams, pResults);
        CHECK_HR(hr, "Failed to get supported methods");
    }
    else if (IsEqualPropertyKey(Command, WPD_COMMAND_SERVICE_CAPABILITIES_GET_SUPPORTED_METHODS_BY_FORMAT))
    {
        hr = OnGetSupportedMethodsByFormat(pParams, pResults);
        CHECK_HR(hr, "Failed to get supported methods by format");
    }
    else if (IsEqualPropertyKey(Command, WPD_COMMAND_SERVICE_CAPABILITIES_GET_METHOD_ATTRIBUTES))
    {
        hr = OnGetMethodAttributes(pParams, pResults);
        CHECK_HR(hr, "Failed to get method attributes");
    }
    else if (IsEqualPropertyKey(Command, WPD_COMMAND_SERVICE_CAPABILITIES_GET_METHOD_PARAMETER_ATTRIBUTES))
    {
        hr = OnGetMethodParameterAttributes(pParams, pResults);
        CHECK_HR(hr, "Failed to get method parameter attributes");
    }
    else if (IsEqualPropertyKey(Command, WPD_COMMAND_SERVICE_CAPABILITIES_GET_SUPPORTED_FORMATS))
    {
        hr = OnGetSupportedFormats(pParams, pResults);
        CHECK_HR(hr, "Failed to get supported formats");
    }
    else if (IsEqualPropertyKey(Command, WPD_COMMAND_SERVICE_CAPABILITIES_GET_FORMAT_ATTRIBUTES))
    {
        hr = OnGetFormatAttributes(pParams, pResults);
        CHECK_HR(hr, "Failed to get format attributes");
    }
    else if (IsEqualPropertyKey(Command, WPD_COMMAND_SERVICE_CAPABILITIES_GET_SUPPORTED_FORMAT_PROPERTIES))
    {
        hr = OnGetSupportedFormatProperties(pParams, pResults);
        CHECK_HR(hr, "Failed to get supported format properties");
    }
    else if (IsEqualPropertyKey(Command, WPD_COMMAND_SERVICE_CAPABILITIES_GET_FORMAT_PROPERTY_ATTRIBUTES))
    {
        hr = OnGetFormatPropertyAttributes(pParams, pResults);
        CHECK_HR(hr, "Failed to get format property attributes");
    }
    else if (IsEqualPropertyKey(Command, WPD_COMMAND_SERVICE_CAPABILITIES_GET_SUPPORTED_EVENTS))
    {
        hr = OnGetSupportedEvents(pParams, pResults);
        CHECK_HR(hr, "Failed to get supported events");
    }
    else if (IsEqualPropertyKey(Command, WPD_COMMAND_SERVICE_CAPABILITIES_GET_EVENT_ATTRIBUTES))
    {
        hr = OnGetEventAttributes(pParams, pResults);
        CHECK_HR(hr, "Failed to get event attributes");
    }
    else if (IsEqualPropertyKey(Command, WPD_COMMAND_SERVICE_CAPABILITIES_GET_EVENT_PARAMETER_ATTRIBUTES))
    {
        hr = OnGetEventParameterAttributes(pParams, pResults);
        CHECK_HR(hr, "Failed to get event parameter attributes");
    }
    else if (IsEqualPropertyKey(Command, WPD_COMMAND_SERVICE_CAPABILITIES_GET_INHERITED_SERVICES))
    {
        hr = OnGetInheritedServices(pParams, pResults);
        CHECK_HR(hr, "Failed to get inherited services");
    }
    else
    {
        hr = E_NOTIMPL;
        CHECK_HR(hr, "This object does not support this command id %d", Command.pid);
    }

    return hr;
}


/**
 *  This method is called when we receive a WPD_COMMAND_SERVICE_CAPABILITIES_GET_SUPPORTED_COMMANDS
 *  command.
 *
 *  The parameters sent to us are:
 *  - none.
 *
 *  The driver should:
 *  - Return all commands supported by this service as an
 *    IPortableDeviceKeyCollection in WPD_PROPERTY_SERVICE_CAPABILITIES_SUPPORTED_COMMANDS.
 *    This includes custom commands, if any.
 */
HRESULT WpdServiceCapabilities::OnGetSupportedCommands(
    _In_    IPortableDeviceValues*  pParams,
    _In_    IPortableDeviceValues*  pResults)
{
    UNREFERENCED_PARAMETER(pParams);
    HRESULT hr = S_OK;
    CComPtr<IPortableDeviceKeyCollection> pCommands;

    // CoCreate a collection to store the supported commands.
    if (hr == S_OK)
    {
        hr = CoCreateInstance(CLSID_PortableDeviceKeyCollection,
                              NULL,
                              CLSCTX_INPROC_SERVER,
                              IID_IPortableDeviceKeyCollection,
                              (VOID**) &pCommands);
        CHECK_HR(hr, "Failed to CoCreate CLSID_PortableDeviceKeyCollection");
    }

    // Add the supported commands to the collection.
    if (hr == S_OK)
    {
        hr = m_pContactsService->GetSupportedCommands(pCommands);
        CHECK_HR(hr, "Failed to get the supported commands");
    }

    // Set the WPD_PROPERTY_SERVICE_CAPABILITIES_SUPPORTED_COMMANDS value in the results.
    if (hr == S_OK)
    {
        hr = pResults->SetIUnknownValue(WPD_PROPERTY_SERVICE_CAPABILITIES_SUPPORTED_COMMANDS, pCommands);
        CHECK_HR(hr, "Failed to set WPD_PROPERTY_SERVICE_CAPABILITIES_SUPPORTED_COMMANDS");
    }

    return hr;
}

/**
 *  This method is called when we receive a WPD_COMMAND_SERVICE_CAPABILITIES_GET_COMMAND_OPTIONS
 *  command.
 *
 *  The parameters sent to us are:
 *  - WPD_PROPERTY_SERVICE_CAPABILITIES_COMMAND: a collection of property keys containing a single value,
 *      which identifies the specific command options are requested to return.
 *
 *  The driver should:
 *  - Return an IPortableDeviceValues in WPD_PROPERTY_SERVICE_CAPABILITIES_COMMAND_OPTIONS, containing
 *      the relevant options.  If no options are available for this command, the driver should
 *      return an IPortableDeviceValues with no elements in it.
 */
HRESULT WpdServiceCapabilities::OnGetCommandOptions(
    _In_    IPortableDeviceValues*  pParams,
    _In_    IPortableDeviceValues*  pResults)
{
    HRESULT     hr      = S_OK;
    PROPERTYKEY Command = WPD_PROPERTY_NULL;
    CComPtr<IPortableDeviceValues> pOptions;

    // Get the command whose options have been requested
    if (hr == S_OK)
    {
        hr = pParams->GetKeyValue(WPD_PROPERTY_SERVICE_CAPABILITIES_COMMAND, &Command);
        CHECK_HR(hr, "Missing value for WPD_PROPERTY_SERVICE_CAPABILITIES_COMMAND");
    }

    // CoCreate a collection to store the command options.
    if (hr == S_OK)
    {
        hr = CoCreateInstance(CLSID_PortableDeviceValues,
                              NULL,
                              CLSCTX_INPROC_SERVER,
                              IID_IPortableDeviceValues,
                              (VOID**) &pOptions);
        CHECK_HR(hr, "Failed to CoCreateInstance CLSID_PortableDeviceValues");
    }

    // Add command options to the collection
    if (hr == S_OK)
    {
        hr = m_pContactsService->GetCommandOptions(Command, pOptions);
        CHECK_HR(hr, "Failed to get the command options");
    }

    // Set the WPD_PROPERTY_SERVICE_CAPABILITIES_COMMAND_OPTIONS value in the results.
    if (hr == S_OK)
    {
        hr = pResults->SetIUnknownValue(WPD_PROPERTY_SERVICE_CAPABILITIES_COMMAND_OPTIONS, pOptions);
        CHECK_HR(hr, "Failed to set WPD_PROPERTY_SERVICE_CAPABILITIES_COMMAND_OPTIONS");
    }

    return hr;
}

/**
 *  This method is called when we receive a WPD_COMMAND_SERVICE_CAPABILITIES_GET_SUPPORTED_METHODS command.
 *
 *  The parameters sent to us are:
 *  - none.
 *
 *  The driver should:
 *  - Return all methods supported by this service as an
 *    IPortableDevicePropVariantCollection in WPD_PROPERTY_SERVICE_CAPABILITIES_SUPPORTED_METHODS.
 *    If no methods are available for this service, the driver should return an IPortableDevicePropVariantCollection 
 *    with no elements in it.
 */
HRESULT WpdServiceCapabilities::OnGetSupportedMethods(
    _In_    IPortableDeviceValues*  pParams,
    _In_    IPortableDeviceValues*  pResults)
{
    UNREFERENCED_PARAMETER(pParams);

    CComPtr<IPortableDevicePropVariantCollection> pMethods;

    // CoCreate a collection to store the supported methods.
    HRESULT hr = CoCreateInstance(CLSID_PortableDevicePropVariantCollection,
                                      NULL,
                                      CLSCTX_INPROC_SERVER,
                                      IID_IPortableDevicePropVariantCollection,
                                      (VOID**) &pMethods);
    CHECK_HR(hr, "Failed to CoCreate CLSID_PortableDevicePropVariantCollection");
    
    if (hr == S_OK)
    {
        hr = m_pContactsService->GetSupportedMethods(pMethods);
        CHECK_HR(hr, "Failed to get the supported methods");
    }

    // Set the WPD_PROPERTY_SERVICE_CAPABILITIES_SUPPORTED_METHODS value in the results.
    if (hr == S_OK)
    {
        hr = pResults->SetIPortableDevicePropVariantCollectionValue(WPD_PROPERTY_SERVICE_CAPABILITIES_SUPPORTED_METHODS, pMethods);
        CHECK_HR(hr, "Failed to set WPD_PROPERTY_SERVICE_CAPABILITIES_SUPPORTED_METHODS");
    }

    return hr;
}

/**
 *  This method is called when we receive a WPD_COMMAND_SERVICE_CAPABILITIES_GET_SUPPORTED_METHODS_BY_FORMAT command.
 *
 *  The parameters sent to us are:
 *  - WPD_PROPERTY_SERVICE_CAPABILITIES_FORMAT: Identifies the format whose methods are being requested
 *
 *  The driver should:
 *  - Return an IPortableDevicePropVariantCollection in WPD_PROPERTY_SERVICE_CAPABILITIES_SUPPORTED_METHODS, 
 *    containing the supported methods that apply to this format.  If no methods are available for this format, 
 *    the driver should return an IPortableDevicePropVariantCollection with no elements in it.
 */
HRESULT WpdServiceCapabilities::OnGetSupportedMethodsByFormat(
    _In_    IPortableDeviceValues*  pParams,
    _In_    IPortableDeviceValues*  pResults)
{
    HRESULT hr     = S_OK;
    GUID    Format = GUID_NULL;

    CComPtr<IPortableDevicePropVariantCollection> pMethods;

    // Get the format parameter
    hr = pParams->GetGuidValue(WPD_PROPERTY_SERVICE_CAPABILITIES_FORMAT, &Format);
    CHECK_HR(hr, "Failed to get WPD_PROPERTY_SERVICE_CAPABILITIES_FORMAT");

    if (hr == S_OK)
    {
        // CoCreate a collection to store the supported methods.
        hr = CoCreateInstance(CLSID_PortableDevicePropVariantCollection,
                              NULL,
                              CLSCTX_INPROC_SERVER,
                              IID_IPortableDevicePropVariantCollection,
                              (VOID**) &pMethods);
        CHECK_HR(hr, "Failed to CoCreate CLSID_PortableDevicePropVariantCollection");
    }

    if (hr == S_OK)
    {
        hr = m_pContactsService->GetSupportedMethodsByFormat(Format, pMethods);
        CHECK_HR(hr, "Failed to get the supported methods by format");
    }

    // Set the WPD_PROPERTY_SERVICE_CAPABILITIES_SUPPORTED_METHODS value in the results.
    if (hr == S_OK)
    {
        hr = pResults->SetIPortableDevicePropVariantCollectionValue(WPD_PROPERTY_SERVICE_CAPABILITIES_SUPPORTED_METHODS, pMethods);
        CHECK_HR(hr, "Failed to set WPD_PROPERTY_SERVICE_CAPABILITIES_SUPPORTED_METHODS");
    }

    return hr;
}

/**
 *  This method is called when we receive a WPD_COMMAND_SERVICE_CAPABILITIES_GET_METHOD_ATTRIBUTES command.
 *
 *  The parameters sent to us are:
 *  - WPD_PROPERTY_SERVICE_CAPABILITIES_METHOD: Identifies the method whose attributes are being requested
 *
 *  The driver should:
 *  - Return an IPortableDeviceValues in WPD_PROPERTY_SERVICE_CAPABILITIES_METHOD_ATTRIBUTES, containing
 *      the method attributes.  If no attributes are available for this method, the driver should
 *      return an IPortableDeviceValues with no elements in it.
 */
HRESULT WpdServiceCapabilities::OnGetMethodAttributes(
    _In_    IPortableDeviceValues*  pParams,
    _In_    IPortableDeviceValues*  pResults)
{
    HRESULT hr     = S_OK;
    GUID    Method = GUID_NULL;
    
    CComPtr<IPortableDeviceValues> pAttributes;

    // Get the method
    hr = pParams->GetGuidValue(WPD_PROPERTY_SERVICE_CAPABILITIES_METHOD, &Method);
    CHECK_HR(hr, "Failed to get WPD_PROPERTY_SERVICE_CAPABILITIES_METHOD");
    
    if (hr == S_OK)
    {
        // CoCreate a collection to store the method attributes.
        hr = CoCreateInstance(CLSID_PortableDeviceValues,
                              NULL,
                              CLSCTX_INPROC_SERVER,
                              IID_IPortableDeviceValues,
                              (VOID**) &pAttributes);
        CHECK_HR(hr, "Failed to CoCreate CLSID_PortableDeviceValues");
    }

    if (hr == S_OK)
    {
        hr = m_pContactsService->GetMethodAttributes(Method, pAttributes);
        CHECK_HR(hr, "Failed to add method attributes");
    }

    // Set the WPD_PROPERTY_SERVICE_CAPABILITIES_METHOD_ATTRIBUTES value in the results.
    if (hr == S_OK)
    {
        hr = pResults->SetIPortableDeviceValuesValue(WPD_PROPERTY_SERVICE_CAPABILITIES_METHOD_ATTRIBUTES, pAttributes);
        CHECK_HR(hr, "Failed to set WPD_PROPERTY_SERVICE_CAPABILITIES_SUPPORTED_METHODS");
    }

    return hr;
}

/**
 *  This method is called when we receive a WPD_COMMAND_SERVICE_CAPABILITIES_GET_METHOD_PARAMETER_ATTRIBUTES command.
 *
 *  The parameters sent to us are:
 *  - WPD_PROPERTY_SERVICE_CAPABILITIES_PARAMETER: Identifies the parameter whose attributes are being requested
 *
 *  The driver should:
 *  - Return an IPortableDeviceValues in WPD_PROPERTY_SERVICE_CAPABILITIES_METHOD_PARAMETER_ATTRIBUTES, containing
 *      the parameter attributes.  If no attributes are available for this parameter, the driver should
 *      return an IPortableDeviceValues with no elements in it.
 */
HRESULT WpdServiceCapabilities::OnGetMethodParameterAttributes(
    _In_    IPortableDeviceValues*  pParams,
    _In_    IPortableDeviceValues*  pResults)
{
    HRESULT     hr        = S_OK;
    PROPERTYKEY Parameter = WPD_PROPERTY_NULL;
    
    CComPtr<IPortableDeviceValues> pAttributes;

    // Get the method
    hr = pParams->GetKeyValue(WPD_PROPERTY_SERVICE_CAPABILITIES_PARAMETER, &Parameter);
    CHECK_HR(hr, "Failed to get WPD_PROPERTY_SERVICE_CAPABILITIES_PARAMETER");
    
    if (hr == S_OK)
    {
        // CoCreate a collection to store the parameter attributes.
        hr = CoCreateInstance(CLSID_PortableDeviceValues,
                              NULL,
                              CLSCTX_INPROC_SERVER,
                              IID_IPortableDeviceValues,
                              (VOID**) &pAttributes);
        CHECK_HR(hr, "Failed to CoCreate CLSID_PortableDeviceValues");
    }

    if (hr == S_OK)
    {
        hr = m_pContactsService->GetMethodParameterAttributes(Parameter, pAttributes);
        CHECK_HR(hr, "Failed to get the method parameter attributes");
    }

    // Set the WPD_PROPERTY_SERVICE_CAPABILITIES_PARAMETER_ATTRIBUTES value in the results.
    if (hr == S_OK)
    {
        hr = pResults->SetIPortableDeviceValuesValue(WPD_PROPERTY_SERVICE_CAPABILITIES_PARAMETER_ATTRIBUTES, pAttributes);
        CHECK_HR(hr, "Failed to set WPD_PROPERTY_SERVICE_CAPABILITIES_PARAMETER_ATTRIBUTES");
    }

    return hr;
}

/**
 *  This method is called when we receive a WPD_COMMAND_SERVICE_CAPABILITIES_GET_SUPPORTED_FORMATS command.
 *
 *  The parameters sent to us are:
 *  - None
 *
 *  The driver should:
 *  - Return an IPortableDevicePropVariantCollection in WPD_PROPERTY_SERVICE_CAPABILITIES_FORMATS, containing
 *      the supported formats for the service.  If no formats are supported by this service, the driver should
 *      return an IPortableDevicePropVariantCollection with no elements in it.
 */
HRESULT WpdServiceCapabilities::OnGetSupportedFormats(
    _In_    IPortableDeviceValues*  pParams,
    _In_    IPortableDeviceValues*  pResults)
{
    UNREFERENCED_PARAMETER(pParams);

    CComPtr<IPortableDevicePropVariantCollection> pFormats;
   
    // CoCreate a collection to store the formats.
    HRESULT hr = CoCreateInstance(CLSID_PortableDevicePropVariantCollection,
                          NULL,
                          CLSCTX_INPROC_SERVER,
                          IID_IPortableDevicePropVariantCollection,
                          (VOID**) &pFormats);
    CHECK_HR(hr, "Failed to CoCreate CLSID_PortableDevicePropVariantCollection");

    if (hr == S_OK)
    {
        hr = m_pContactsService->GetSupportedFormats(pFormats);
        CHECK_HR(hr, "Failed to get the supported formats");
    }

    // Set the WPD_PROPERTY_SERVICE_CAPABILITIES_FORMATS value in the results.
    if (hr == S_OK)
    {
        hr = pResults->SetIPortableDevicePropVariantCollectionValue(WPD_PROPERTY_SERVICE_CAPABILITIES_FORMATS, pFormats);
        CHECK_HR(hr, "Failed to set WPD_PROPERTY_SERVICE_CAPABILITIES_FORMATS");
    }
   
    return hr;
}

/**
 *  This method is called when we receive a WPD_COMMAND_SERVICE_CAPABILITIES_GET_FORMAT_ATTRIBUTES command.
 *
 *  The parameters sent to us are:
 *  - WPD_PROPERTY_SERVICE_CAPABILITIES_FORMAT: Identifies the format whose attributes are being requested
 *
 *  The driver should:
 *  - Return an IPortableDeviceValues in WPD_PROPERTY_SERVICE_CAPABILITIES_FORMAT_ATTRIBUTES, containing
 *      the attributes for the format.  If no attributes are supported by the format, the driver should
 *      return an IPortableDeviceValues with no elements in it.
 */
HRESULT WpdServiceCapabilities::OnGetFormatAttributes(
    _In_    IPortableDeviceValues*  pParams,
    _In_    IPortableDeviceValues*  pResults)
{
    HRESULT hr     = S_OK;
    GUID    Format = GUID_NULL;
    
    CComPtr<IPortableDeviceValues> pAttributes;

    // Get the format
    hr = pParams->GetGuidValue(WPD_PROPERTY_SERVICE_CAPABILITIES_FORMAT, &Format);
    CHECK_HR(hr, "Failed to get WPD_PROPERTY_SERVICE_CAPABILITIES_FORMAT");
    
    if (hr == S_OK)
    {
        // CoCreate a collection to store the attributes.
        hr = CoCreateInstance(CLSID_PortableDeviceValues,
                              NULL,
                              CLSCTX_INPROC_SERVER,
                              IID_IPortableDeviceValues,
                              (VOID**) &pAttributes);
        CHECK_HR(hr, "Failed to CoCreate CLSID_PortableDeviceValues");
    }

    if (hr == S_OK)
    {
        hr = m_pContactsService->GetFormatAttributes(Format, pAttributes);
        CHECK_HR(hr, "Failed to add format attributes");
    }

    // Set the WPD_PROPERTY_SERVICE_CAPABILITIES_FORMAT_ATTRIBUTES value in the results.
    if (hr == S_OK)
    {
        hr = pResults->SetIPortableDeviceValuesValue(WPD_PROPERTY_SERVICE_CAPABILITIES_FORMAT_ATTRIBUTES, pAttributes);
        CHECK_HR(hr, "Failed to set WPD_PROPERTY_SERVICE_CAPABILITIES_FORMAT_ATTRIBUTES");
    }
   
    return hr;
}

/**
 *  This method is called when we receive a WPD_COMMAND_SERVICE_CAPABILITIES_GET_SUPPORTED_FORMAT_PROPERTIES command.
 *  This list is the super-set of all properties that will be supported by an object of the given format. 
 *  Individual objects can be queried for their properties using WPD_COMMAND_OBJECT_PROPERTIES_GET_SUPPORTED. 
 *  Note that this method is generally much quicker than calling WPD_COMMAND_OBJECT_PROPERTIES_GET_SUPPORTED, 
 *  since the driver does not have to perform a dynamic lookup based on a specific object.
 *
 *  The parameters sent to us are:
 *  - WPD_PROPERTY_SERVICE_CAPABILITIES_FORMAT: Identifies the format whose attributes are being requested
 *
 *  The driver should:
 *  - Return an IPortableDeviceKeyCollection in WPD_PROPERTY_SERVICE_CAPABILITIES_PROPERTY_KEYS, containing
 *      the supported properties for the format.  If no properties are supported by the format, the driver should
 *      return an IPortableDeviceKeyCollection with no elements in it.
 */
HRESULT WpdServiceCapabilities::OnGetSupportedFormatProperties(
    _In_    IPortableDeviceValues*  pParams,
    _In_    IPortableDeviceValues*  pResults)
{
    HRESULT hr     = S_OK;
    GUID    Format = GUID_NULL;
  
    CComPtr<IPortableDeviceKeyCollection> pKeys;

    // Get the format
    hr = pParams->GetGuidValue(WPD_PROPERTY_SERVICE_CAPABILITIES_FORMAT, &Format);
    CHECK_HR(hr, "Failed to get WPD_PROPERTY_SERVICE_CAPABILITIES_FORMAT");

    if (hr == S_OK)
    {
        // CoCreate a collection to store the attributes.
        hr = CoCreateInstance(CLSID_PortableDeviceKeyCollection,
                              NULL,
                              CLSCTX_INPROC_SERVER,
                              IID_IPortableDeviceKeyCollection,
                              (VOID**) &pKeys);
        CHECK_HR(hr, "Failed to CoCreate CLSID_PortableDeviceKeyCollection");
    }

    if (hr == S_OK)
    {
        hr = m_pContactsService->GetSupportedFormatProperties(Format, pKeys);
        CHECK_HR(hr, "Failed to add the supported format properties");
    }

    // Set the WPD_PROPERTY_SERVICE_CAPABILITIES_PROPERTY_KEYS value in the results.
    if (hr == S_OK)
    {
        hr = pResults->SetIPortableDeviceKeyCollectionValue(WPD_PROPERTY_SERVICE_CAPABILITIES_PROPERTY_KEYS, pKeys);
        CHECK_HR(hr, "Failed to set WPD_PROPERTY_SERVICE_CAPABILITIES_PROPERTY_KEYS");
    }
   
    return hr;
}

/**
 *  This method is called when we receive a WPD_COMMAND_SERVICE_CAPABILITIES_GET_FORMAT_PROPERTY_ATTRIBUTES command.
 *  Often, a driver treats objects of a given format the same. Many properties therefore will have attributes 
 *  that are identical across all objects of that format. These can be returned here. There are some attributes 
 *  which may be differ per object instance, which are not returned here. See WPD_COMMAND_OBJECT_PROPERTIES_GET_ATTRIBUTES.
 *
 *  The parameters sent to us are:
 *  - WPD_PROPERTY_SERVICE_CAPABILITIES_FORMAT: Identifies the format whose property attributes are being requested
 *  - WPD_PROPERTY_SERVICE_CAPABILITIES_PROPERTY_KEYS: An IPortableDeviceKeyCollection containing a single value, 
 *    which is the key identifying the specific property attributes the driver is requested to return.
 *
 *  The driver should:
 *  - Return an IPortableDeviceValues in WPD_PROPERTY_SERVICE_CAPABILITIES_PROPERTY_ATTRIBUTES, containing
 *      the attributes for the property.  If no attributes are supported by the property, the driver should
 *      return an IPortableDeviceValues with no elements in it.
 */
HRESULT WpdServiceCapabilities::OnGetFormatPropertyAttributes(
    _In_    IPortableDeviceValues*  pParams,
    _In_    IPortableDeviceValues*  pResults)
{
    HRESULT     hr                   = S_OK;
    GUID        Format               = GUID_NULL;
    PROPERTYKEY Property             = WPD_PROPERTY_NULL;

    CComPtr<IPortableDeviceValues> pAttributes;

    // Get the format
    hr = pParams->GetGuidValue(WPD_PROPERTY_SERVICE_CAPABILITIES_FORMAT, &Format);
    CHECK_HR(hr, "Failed to get WPD_PROPERTY_SERVICE_CAPABILITIES_FORMAT");

    if (hr == S_OK)
    {
        // Get the property
        hr = pParams->GetKeyValue(WPD_PROPERTY_SERVICE_CAPABILITIES_PROPERTY_KEYS, &Property);
        CHECK_HR(hr, "Failed to get WPD_PROPERTY_SERVICE_CAPABILITIES_FORMAT");
    }

    if (hr == S_OK)
    {
        // CoCreate a collection to store the attributes.
        hr = CoCreateInstance(CLSID_PortableDeviceValues,
                              NULL,
                              CLSCTX_INPROC_SERVER,
                              IID_IPortableDeviceValues,
                              (VOID**) &pAttributes);
        CHECK_HR(hr, "Failed to CoCreate CLSID_PortableDeviceValues");
    }

    if (hr == S_OK)
    {
        hr = m_pContactsService->GetPropertyAttributes(Format, Property, pAttributes);
        CHECK_HR(hr, "Failed to get the supported property attributes");
    }

    // Set the WPD_PROPERTY_SERVICE_CAPABILITIES_PROPERTY_ATTRIBUTES value in the results.
    if (hr == S_OK)
    {
        hr = pResults->SetIPortableDeviceValuesValue(WPD_PROPERTY_SERVICE_CAPABILITIES_PROPERTY_ATTRIBUTES, pAttributes);
        CHECK_HR(hr, "Failed to set WPD_PROPERTY_SERVICE_CAPABILITIES_PROPERTY_ATTRIBUTES");
    }

    return hr;

}

/**
 *  This method is called when we receive a WPD_COMMAND_SERVICE_CAPABILITIES_GET_SUPPORTED_EVENTS command.
 *
 *  The parameters sent to us are:
 *  - None
 *
 *  The driver should:
 *  - Return an IPortableDevicePropVariantCollection in WPD_PROPERTY_SERVICE_CAPABILITIES_SUPPORTED_EVENTS, containing
 *    the events for the service.  If no events are supported by the service, the driver should
 *    return an IPortableDevicePropVariantCollection with no elements in it.
 */
HRESULT WpdServiceCapabilities::OnGetSupportedEvents(
    _In_    IPortableDeviceValues*  pParams,
    _In_    IPortableDeviceValues*  pResults)
{
    UNREFERENCED_PARAMETER(pParams);

    CComPtr<IPortableDevicePropVariantCollection> pEvents;

    // CoCreate a collection to store the supported events.
    HRESULT hr = CoCreateInstance(CLSID_PortableDevicePropVariantCollection,
                          NULL,
                          CLSCTX_INPROC_SERVER,
                          IID_IPortableDevicePropVariantCollection,
                          (VOID**) &pEvents);
    CHECK_HR(hr, "Failed to CoCreate CLSID_PortableDevicePropVariantCollection");

    // Add the supported events to the collection.
    if (hr == S_OK)
    {
        hr = m_pContactsService->GetSupportedEvents(pEvents);
        CHECK_HR(hr, "Failed to get the supported events");
    }

    // Set the WPD_PROPERTY_SERVICE_CAPABILITIES_SUPPORTED_EVENTS value in the results.
    if (hr == S_OK)
    {
        hr = pResults->SetIPortableDevicePropVariantCollectionValue(WPD_PROPERTY_SERVICE_CAPABILITIES_SUPPORTED_EVENTS, pEvents);
        CHECK_HR(hr, "Failed to set WPD_PROPERTY_SERVICE_CAPABILITIES_SUPPORTED_EVENTS");
    }

    return hr;

}

/**
 *  This method is called when we receive a WPD_COMMAND_SERVICE_CAPABILITIES_GET_EVENT_ATTRIBUTES command.
 *
 *  The parameters sent to us are:
 *  - WPD_PROPERTY_SERVICE_CAPABILITIES_EVENT: Indicates the event the caller is interested in
 *
 *  The driver should:
 *  - Return an IPortableDeviceValues in WPD_PROPERTY_SERVICE_CAPABILITIES_EVENT_ATTRIBUTES, containing
 *    the event attributes.  If there are no attributes for that event, the driver should
 *    return an IPortableDeviceValues with no elements in it.  
 */
HRESULT WpdServiceCapabilities::OnGetEventAttributes(
    _In_    IPortableDeviceValues*  pParams,
    _In_    IPortableDeviceValues*  pResults)
{
    HRESULT hr    = S_OK;
    GUID    Event = GUID_NULL;
  
    CComPtr<IPortableDeviceValues> pAttributes;

    // Get the format
    hr = pParams->GetGuidValue(WPD_PROPERTY_SERVICE_CAPABILITIES_EVENT, &Event);
    CHECK_HR(hr, "Failed to get WPD_PROPERTY_SERVICE_CAPABILITIES_EVENT");

    if (hr == S_OK)
    {
        // CoCreate a collection to store the attributes.
        hr = CoCreateInstance(CLSID_PortableDeviceValues,
                              NULL,
                              CLSCTX_INPROC_SERVER,
                              IID_IPortableDeviceValues,
                              (VOID**) &pAttributes);
        CHECK_HR(hr, "Failed to CoCreate CLSID_PortableDeviceValues");
    }
    
    if (hr == S_OK)
    {
        hr = m_pContactsService->GetEventAttributes(Event, pAttributes);
        CHECK_HR(hr, "Failed to add event attributes");
    }

    // Set the WPD_PROPERTY_SERVICE_CAPABILITIES_SUPPORTED_EVENTS value in the results.
    if (hr == S_OK)
    {
        hr = pResults->SetIPortableDeviceValuesValue(WPD_PROPERTY_SERVICE_CAPABILITIES_EVENT_ATTRIBUTES, pAttributes);
        CHECK_HR(hr, "Failed to set WPD_PROPERTY_SERVICE_CAPABILITIES_EVENT_ATTRIBUTES");
    }
    
    return hr;
}


/**
 *  This method is called when we receive a WPD_COMMAND_SERVICE_CAPABILITIES_GET_EVENT_PARAMETER_ATTRIBUTES command.
 *
 *  The parameters sent to us are:
 *  - WPD_PROPERTY_SERVICE_CAPABILITIES_PARAMETER: Identifies the parameter whose attributes are being requested
 *
 *  The driver should:
 *  - Return an IPortableDeviceValues in WPD_PROPERTY_SERVICE_CAPABILITIES_EVENT_PARAMETER_ATTRIBUTES, containing
 *      the parameter attributes.  If no attributes are available for this parameter, the driver should
 *      return an IPortableDeviceValues with no elements in it.
 */
HRESULT WpdServiceCapabilities::OnGetEventParameterAttributes(
    _In_    IPortableDeviceValues*  pParams,
    _In_    IPortableDeviceValues*  pResults)
{
    HRESULT     hr        = S_OK;
    PROPERTYKEY Parameter = WPD_PROPERTY_NULL;
    
    CComPtr<IPortableDeviceValues> pAttributes;

    // Get the method
    hr = pParams->GetKeyValue(WPD_PROPERTY_SERVICE_CAPABILITIES_PARAMETER, &Parameter);
    CHECK_HR(hr, "Failed to get WPD_PROPERTY_SERVICE_CAPABILITIES_PARAMETER");
    
    if (hr == S_OK)
    {
        // CoCreate a collection to store the parameter attributes.
        hr = CoCreateInstance(CLSID_PortableDeviceValues,
                              NULL,
                              CLSCTX_INPROC_SERVER,
                              IID_IPortableDeviceValues,
                              (VOID**) &pAttributes);
        CHECK_HR(hr, "Failed to CoCreate CLSID_PortableDeviceValues");
    }

    if (hr == S_OK)
    {
        hr = m_pContactsService->GetEventParameterAttributes(Parameter, pAttributes);
        CHECK_HR(hr, "Failed to get the event parameter attributes");
    }

    // Set the WPD_PROPERTY_SERVICE_CAPABILITIES_PARAMETER_ATTRIBUTES value in the results.
    if (hr == S_OK)
    {
        hr = pResults->SetIPortableDeviceValuesValue(WPD_PROPERTY_SERVICE_CAPABILITIES_PARAMETER_ATTRIBUTES, pAttributes);
        CHECK_HR(hr, "Failed to set WPD_PROPERTY_SERVICE_CAPABILITIES_PARAMETER_ATTRIBUTES");
    }

    return hr;
}

/**
 *  This method is called when we receive a WPD_COMMAND_SERVICE_CAPABILITIES_GET_INHERITED_SERVICES command.
 *
 *  The parameters sent to us are:
 *  - WPD_PROPERTY_SERVICE_CAPABILITIES_INHERITANCE_TYPE: Indicates the inheritance type the caller is interested in
 *    Possible values are from the WPD_SERVICE_INHERITANCE_TYPES enumeration 
 *
 *  The driver should:
 *  - Return an IPortableDevicePropVariantCollection in WPD_PROPERTY_SERVICE_CAPABILITIES_INHERITED_SERVICES, containing
 *    the inherited services.  For WPD_SERVICE_INHERITANCE_IMPLEMENTATION, this will be an 
 *    IPortableDevicePropVariantCollection (of type VT_CLSID) containing the inherited service type GUIDs.
 *    If there are no inherited services, the driver should return an IPortableDevicePropVariantCollection with no elements in it.  
 */
HRESULT WpdServiceCapabilities::OnGetInheritedServices(
    _In_    IPortableDeviceValues*  pParams,
    _In_    IPortableDeviceValues*  pResults)
{
    HRESULT hr                = S_OK;
    DWORD   dwInheritanceType = 0;

    CComPtr<IPortableDevicePropVariantCollection> pServices;

    hr = pParams->GetUnsignedIntegerValue(WPD_PROPERTY_SERVICE_CAPABILITIES_INHERITANCE_TYPE, &dwInheritanceType);
    CHECK_HR(hr, "Failed to get WPD_PROPERTY_SERVICE_CAPABILITIES_INHERITANCE_TYPE");

    if (hr == S_OK)
    {
        // CoCreate a collection to store the attributes.
        hr = CoCreateInstance(CLSID_PortableDevicePropVariantCollection,
                              NULL,
                              CLSCTX_INPROC_SERVER,
                              IID_IPortableDevicePropVariantCollection,
                              (VOID**) &pServices);
        CHECK_HR(hr, "Failed to CoCreate CLSID_PortableDevicePropVariantCollection");
    }
    
    if (hr == S_OK)
    {
        hr = m_pContactsService->GetInheritedServices(dwInheritanceType, pServices);
        CHECK_HR(hr, "Failed to add inherited services");
    }

    // Set the WPD_PROPERTY_SERVICE_CAPABILITIES_INHERITED_SERVICES value in the results.
    if (hr == S_OK)
    {
        hr = pResults->SetIPortableDevicePropVariantCollectionValue(WPD_PROPERTY_SERVICE_CAPABILITIES_INHERITED_SERVICES, pServices);
        CHECK_HR(hr, "Failed to set WPD_PROPERTY_SERVICE_CAPABILITIES_EVENT_ATTRIBUTES");
    }
    
    return hr;
}