-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathminiclass.cpp
625 lines (489 loc) · 18.9 KB
/
miniclass.cpp
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
/*++
This module implements battery miniclass functionality specific to the
simulated battery driver.
--*/
#include "simbatt.h"
#include "simbattdriverif.h"
//------------------------------------------------------------------- Prototypes
_IRQL_requires_same_
void UpdateTag (_Inout_ BATT_FDO_DATA* DevExt);
BCLASS_QUERY_TAG_CALLBACK QueryTag;
BCLASS_QUERY_INFORMATION_CALLBACK QueryInformation;
BCLASS_SET_INFORMATION_CALLBACK SetInformation;
BCLASS_QUERY_STATUS_CALLBACK QueryStatus;
BCLASS_SET_STATUS_NOTIFY_CALLBACK SetStatusNotify;
BCLASS_DISABLE_STATUS_NOTIFY_CALLBACK DisableStatusNotify;
_Must_inspect_result_
_Success_(return==STATUS_SUCCESS)
NTSTATUS SetBatteryStatus (_In_ WDFDEVICE Device, _In_ BATTERY_STATUS* BatteryStatus);
_Must_inspect_result_
_Success_(return==STATUS_SUCCESS)
NTSTATUS SetBatteryInformation (_In_ WDFDEVICE Device, _In_ BATTERY_INFORMATION* BatteryInformation);
//------------------------------------------------------------ Battery Interface
_Use_decl_annotations_
void InitializeBatteryState (WDFDEVICE Device)
/*++
Routine Description:
This routine is called to initialize battery data to sane values.
A real battery would query hardware to determine if a battery is present,
query its static capabilities, etc.
Arguments:
Device - Supplies the device to initialize.
--*/
{
DebugEnter();
BATT_FDO_DATA* DevExt = GetDeviceExtension(Device);
// Get this battery's state - use defaults.
{
WdfWaitLockAcquire(DevExt->StateLock, NULL);
UpdateTag(DevExt);
// manufactured on 8th September 2024
DevExt->State.ManufactureDate.Day = 8;
DevExt->State.ManufactureDate.Month = 9;
DevExt->State.ManufactureDate.Year = 2024;
DevExt->State.BatteryInfo.Capabilities = BATTERY_SYSTEM_BATTERY;
DevExt->State.BatteryInfo.Technology = 1;
DevExt->State.BatteryInfo.Chemistry[0] = 'F';
DevExt->State.BatteryInfo.Chemistry[1] = 'a';
DevExt->State.BatteryInfo.Chemistry[2] = 'k';
DevExt->State.BatteryInfo.Chemistry[3] = 'e';
DevExt->State.BatteryInfo.DesignedCapacity = 110;
DevExt->State.BatteryInfo.FullChargedCapacity = 100;
DevExt->State.BatteryInfo.DefaultAlert1 = 0;
DevExt->State.BatteryInfo.DefaultAlert2 = 0;
DevExt->State.BatteryInfo.CriticalBias = 0;
DevExt->State.BatteryInfo.CycleCount = 100;
DevExt->State.BatteryStatus.PowerState = BATTERY_POWER_ON_LINE;
DevExt->State.BatteryStatus.Capacity = 90;
DevExt->State.BatteryStatus.Voltage = BATTERY_UNKNOWN_VOLTAGE;
DevExt->State.BatteryStatus.Rate = 0;
//DevExt->State.GranularityCount = 0;
//for (unsigned int i = 0; i < DevExt->State.GranularityCount; ++i) {
// DevExt->State.GranularityScale[i].Granularity = 0; // granularity [mWh]
// DevExt->State.GranularityScale[i].Capacity = 0; // upper capacity limit for Granularity [mWh]
//}
DevExt->State.EstimatedTime = BATTERY_UNKNOWN_TIME; // battery run time, in seconds
DevExt->State.Temperature = 2981; // 25 degree Celsius [10ths of a degree Kelvin]
RtlStringCchCopyW(DevExt->State.DeviceName, MAX_BATTERY_STRING_SIZE, L"SimulatedBattery");
RtlStringCchCopyW(DevExt->State.ManufacturerName, MAX_BATTERY_STRING_SIZE, L"OpenSource");
RtlStringCchCopyW(DevExt->State.SerialNumber, MAX_BATTERY_STRING_SIZE, L"1234");
RtlStringCchCopyW(DevExt->State.UniqueId, MAX_BATTERY_STRING_SIZE, L"SimulatedBattery007");
WdfWaitLockRelease(DevExt->StateLock);
}
}
_Use_decl_annotations_
void UpdateTag (BATT_FDO_DATA* DevExt)
/*++
Routine Description:
This routine is called when static battery properties have changed to
update the battery tag.
--*/
{
DevExt->BatteryTag += 1;
if (DevExt->BatteryTag == BATTERY_TAG_INVALID) {
DevExt->BatteryTag += 1;
}
}
_Use_decl_annotations_
NTSTATUS QueryTag (void* Context, ULONG* BatteryTag)
/*++
Routine Description:
This routine is called to get the value of the current battery tag.
Arguments:
Context - Supplies the miniport context value for battery
BatteryTag - Supplies a pointer to a ULONG to receive the battery tag.
--*/
{
NTSTATUS Status;
DebugEnter();
BATT_FDO_DATA* DevExt = (BATT_FDO_DATA*)Context;
WdfWaitLockAcquire(DevExt->StateLock, NULL);
*BatteryTag = DevExt->BatteryTag;
WdfWaitLockRelease(DevExt->StateLock);
if (*BatteryTag == BATTERY_TAG_INVALID) {
Status = STATUS_NO_SUCH_DEVICE;
} else {
Status = STATUS_SUCCESS;
}
DebugExitStatus(Status);
return Status;
}
_Use_decl_annotations_
NTSTATUS QueryInformation (
void* Context,
ULONG BatteryTag,
BATTERY_QUERY_INFORMATION_LEVEL Level,
LONG AtRate,
void* Buffer,
ULONG BufferLength,
ULONG* ReturnedLength)
/*++
Routine Description:
Called by the class driver to retrieve battery information
The battery class driver will serialize all requests it issues to
the miniport for a given battery.
Return invalid parameter when a request for a specific level of information
can't be handled. This is defined in the battery class spec.
Arguments:
Context - Supplies the miniport context value for battery
BatteryTag - Supplies the tag of current battery
Level - Supplies the type of information required
AtRate - Supplies the rate of drain for the BatteryEstimatedTime level
Buffer - Supplies a pointer to a buffer to place the information
BufferLength - Supplies the length in bytes of the buffer
ReturnedLength - Supplies the length in bytes of the returned data
Return Value:
Success if there is a battery currently installed, else no such device.
--*/
{
ULONG ResultValue;
NTSTATUS Status;
UNREFERENCED_PARAMETER(AtRate);
DebugEnter();
BATT_FDO_DATA* DevExt = (BATT_FDO_DATA*)Context;
WdfWaitLockAcquire(DevExt->StateLock, NULL);
if (BatteryTag != DevExt->BatteryTag) {
Status = STATUS_NO_SUCH_DEVICE;
goto QueryInformationEnd;
}
// Determine the value of the information being queried for and return it.
// In a real battery, this would require hardware/firmware accesses. The
// simulated battery fakes this by storing the data to be returned in
// memory.
void* ReturnBuffer = NULL;
size_t ReturnBufferLength = 0;
DebugPrint(DPFLTR_INFO_LEVEL, "Query for information level 0x%x\n", Level);
Status = STATUS_INVALID_DEVICE_REQUEST;
switch (Level) {
case BatteryInformation:
ReturnBuffer = &DevExt->State.BatteryInfo;
ReturnBufferLength = sizeof(BATTERY_INFORMATION);
Status = STATUS_SUCCESS;
break;
case BatteryEstimatedTime:
if (DevExt->State.EstimatedTime == SIMBATT_RATE_CALCULATE) {
if (AtRate == 0) {
AtRate = DevExt->State.BatteryStatus.Rate;
}
if (AtRate < 0) {
ResultValue = (3600 * DevExt->State.BatteryStatus.Capacity) /
(-AtRate);
} else {
ResultValue = BATTERY_UNKNOWN_TIME;
}
} else {
ResultValue = DevExt->State.EstimatedTime;
}
ReturnBuffer = &ResultValue;
ReturnBufferLength = sizeof(ResultValue);
Status = STATUS_SUCCESS;
break;
case BatteryUniqueID:
ReturnBuffer = DevExt->State.UniqueId;
Status = RtlStringCbLengthW(DevExt->State.UniqueId,
sizeof(DevExt->State.UniqueId),
&ReturnBufferLength);
ReturnBufferLength += sizeof(WCHAR);
break;
case BatteryManufactureName:
ReturnBuffer = DevExt->State.ManufacturerName;
Status = RtlStringCbLengthW(DevExt->State.ManufacturerName,
sizeof(DevExt->State.ManufacturerName),
&ReturnBufferLength);
ReturnBufferLength += sizeof(WCHAR);
break;
case BatteryDeviceName:
ReturnBuffer = DevExt->State.DeviceName;
Status = RtlStringCbLengthW(DevExt->State.DeviceName,
sizeof(DevExt->State.DeviceName),
&ReturnBufferLength);
ReturnBufferLength += sizeof(WCHAR);
break;
case BatterySerialNumber:
ReturnBuffer = DevExt->State.SerialNumber;
Status = RtlStringCbLengthW(DevExt->State.SerialNumber,
sizeof(DevExt->State.SerialNumber),
&ReturnBufferLength);
ReturnBufferLength += sizeof(WCHAR);
break;
case BatteryManufactureDate:
if (DevExt->State.ManufactureDate.Day != 0) {
ReturnBuffer = &DevExt->State.ManufactureDate;
ReturnBufferLength = sizeof(BATTERY_MANUFACTURE_DATE);
Status = STATUS_SUCCESS;
}
break;
case BatteryGranularityInformation:
if (DevExt->State.GranularityCount > 0) {
ReturnBuffer = DevExt->State.GranularityScale;
ReturnBufferLength = DevExt->State.GranularityCount*sizeof(BATTERY_REPORTING_SCALE);
Status = STATUS_SUCCESS;
}
break;
case BatteryTemperature:
ReturnBuffer = &DevExt->State.Temperature;
ReturnBufferLength = sizeof(ULONG);
Status = STATUS_SUCCESS;
break;
default:
Status = STATUS_INVALID_PARAMETER;
break;
}
NT_ASSERT(((ReturnBufferLength == 0) && (ReturnBuffer == NULL)) ||
((ReturnBufferLength > 0) && (ReturnBuffer != NULL)));
if (NT_SUCCESS(Status)) {
*ReturnedLength = (ULONG)ReturnBufferLength;
if (ReturnBuffer != NULL) {
if ((Buffer == NULL) || (BufferLength < ReturnBufferLength)) {
Status = STATUS_BUFFER_TOO_SMALL;
} else {
memcpy(Buffer, ReturnBuffer, ReturnBufferLength);
}
}
} else {
*ReturnedLength = 0;
}
QueryInformationEnd:
WdfWaitLockRelease(DevExt->StateLock);
DebugExitStatus(Status);
return Status;
}
_Use_decl_annotations_
NTSTATUS QueryStatus (void* Context, ULONG BatteryTag, BATTERY_STATUS* BatteryStatus)
/*++
Routine Description:
Called by the class driver to retrieve the batteries current status
The battery class driver will serialize all requests it issues to
the miniport for a given battery.
Arguments:
Context - Supplies the miniport context value for battery
BatteryTag - Supplies the tag of current battery
BatteryStatus - Supplies a pointer to the structure to return the current
battery status in
Return Value:
Success if there is a battery currently installed, else no such device.
--*/
{
NTSTATUS Status;
DebugEnter();
BATT_FDO_DATA* DevExt = (BATT_FDO_DATA*)Context;
WdfWaitLockAcquire(DevExt->StateLock, NULL);
if (BatteryTag != DevExt->BatteryTag) {
Status = STATUS_NO_SUCH_DEVICE;
goto QueryStatusEnd;
}
RtlCopyMemory(BatteryStatus,
&DevExt->State.BatteryStatus,
sizeof(BATTERY_STATUS));
Status = STATUS_SUCCESS;
QueryStatusEnd:
WdfWaitLockRelease(DevExt->StateLock);
DebugExitStatus(Status);
return Status;
}
_Use_decl_annotations_
NTSTATUS SetStatusNotify (void* Context, ULONG BatteryTag, BATTERY_NOTIFY* BatteryNotify)
/*++
Routine Description:
Called by the class driver to set the capacity and power state levels
at which the class driver requires notification.
The battery class driver will serialize all requests it issues to
the miniport for a given battery.
Arguments:
Context - Supplies the miniport context value for battery
BatteryTag - Supplies the tag of current battery
BatteryNotify - Supplies a pointer to a structure containing the
notification critera.
Return Value:
Success if there is a battery currently installed, else no such device.
--*/
{
NTSTATUS Status;
UNREFERENCED_PARAMETER(BatteryNotify);
DebugEnter();
BATT_FDO_DATA* DevExt = (BATT_FDO_DATA*)Context;
WdfWaitLockAcquire(DevExt->StateLock, NULL);
if (BatteryTag != DevExt->BatteryTag) {
Status = STATUS_NO_SUCH_DEVICE;
goto SetStatusNotifyEnd;
}
Status = STATUS_NOT_SUPPORTED;
SetStatusNotifyEnd:
WdfWaitLockRelease(DevExt->StateLock);
DebugExitStatus(Status);
return Status;
}
_Use_decl_annotations_
NTSTATUS DisableStatusNotify (void* Context)
/*++
Routine Description:
Called by the class driver to disable notification.
The battery class driver will serialize all requests it issues to
the miniport for a given battery.
Arguments:
Context - Supplies the miniport context value for battery
Return Value:
Success if there is a battery currently installed, else no such device.
--*/
{
UNREFERENCED_PARAMETER(Context);
DebugEnter();
NTSTATUS Status = STATUS_NOT_SUPPORTED;
DebugExitStatus(Status);
return Status;
}
_Use_decl_annotations_
NTSTATUS SetInformation (
void* Context,
ULONG BatteryTag,
BATTERY_SET_INFORMATION_LEVEL Level,
void* Buffer)
/*
Routine Description:
Called by the class driver to set the battery's charge/discharge state,
critical bias, or charge current.
Arguments:
Context - Supplies the miniport context value for battery
BatteryTag - Supplies the tag of current battery
Level - Supplies action requested
Buffer - Supplies a critical bias value if level is BatteryCriticalBias.
--*/
{
NTSTATUS Status;
UNREFERENCED_PARAMETER(Level);
DebugEnter();
BATT_FDO_DATA* DevExt = (BATT_FDO_DATA*)Context;
WdfWaitLockAcquire(DevExt->StateLock, NULL);
if (BatteryTag != DevExt->BatteryTag) {
Status = STATUS_NO_SUCH_DEVICE;
goto SetInformationEnd;
}
if (Buffer == NULL) {
Status = STATUS_INVALID_PARAMETER_4;
} else {
Status = STATUS_NOT_SUPPORTED;
}
SetInformationEnd:
WdfWaitLockRelease(DevExt->StateLock);
DebugExitStatus(Status);
return Status;
}
//------------------------------------------------- Battery Simulation Interface
//
// The following IO control handler and associated SimBattSetXxx routines
// implement the control side of the simulated battery. A real battery would
// not implement this interface, and instead read battery data from hardware/
// firmware interfaces.
void BattIoDeviceControl (
WDFQUEUE Queue,
WDFREQUEST Request,
size_t OutputBufferLength,
size_t InputBufferLength,
ULONG IoControlCode)
/*++
Routine Description:
Handle changes to the simulated battery state.
Arguments:
Queue - Supplies a handle to the framework queue object that is associated
with the I/O request.
Request - Supplies a handle to a framework request object. This one
represents the IRP_MJ_DEVICE_CONTROL IRP received by the framework.
OutputBufferLength - Supplies the length, in bytes, of the request's output
buffer, if an output buffer is available.
InputBufferLength - Supplies the length, in bytes, of the request's input
buffer, if an input buffer is available.
IoControlCode - Supplies the Driver-defined or system-defined I/O control
code (IOCTL) that is associated with the request.
--*/
{
BATTERY_INFORMATION* BatteryInformation;
BATTERY_STATUS* BatteryStatus;
size_t Length;
NTSTATUS TempStatus;
UNREFERENCED_PARAMETER(OutputBufferLength);
UNREFERENCED_PARAMETER(InputBufferLength);
ULONG BytesReturned = 0;
WDFDEVICE Device = WdfIoQueueGetDevice(Queue);
DebugPrint(DPFLTR_INFO_LEVEL, "BattIoDeviceControl: 0x%p\n", Device);
NTSTATUS Status = STATUS_INVALID_PARAMETER;
switch (IoControlCode) {
case IOCTL_SIMBATT_SET_STATUS:
TempStatus = WdfRequestRetrieveInputBuffer(Request, sizeof(BATTERY_STATUS), (void**)&BatteryStatus, &Length);
if (NT_SUCCESS(TempStatus) && (Length == sizeof(BATTERY_STATUS))) {
Status = SetBatteryStatus(Device, BatteryStatus);
}
break;
case IOCTL_SIMBATT_SET_INFORMATION:
TempStatus = WdfRequestRetrieveInputBuffer(Request, sizeof(BATTERY_INFORMATION), (void**)&BatteryInformation, &Length);
if (NT_SUCCESS(TempStatus) && (Length == sizeof(BATTERY_INFORMATION))) {
Status = SetBatteryInformation(Device, BatteryInformation);
}
break;
default:
break;
}
WdfRequestCompleteWithInformation(Request, Status, BytesReturned);
}
_Use_decl_annotations_
NTSTATUS SetBatteryStatus (WDFDEVICE Device, BATTERY_STATUS* BatteryStatus)
/*++
Routine Description:
Set the simulated battery status structure values.
Arguments:
Device - Supplies the device to set data for.
BatteryStatus - Supplies the new status data to set.
--*/
{
NTSTATUS Status = STATUS_INVALID_PARAMETER;
BATT_FDO_DATA* DevExt = GetDeviceExtension(Device);
ULONG ValidPowerState = BATTERY_CHARGING |
BATTERY_DISCHARGING |
BATTERY_CRITICAL |
BATTERY_POWER_ON_LINE;
if ((BatteryStatus->PowerState & ~ValidPowerState) != 0) {
goto SetBatteryStatusEnd;
}
WdfWaitLockAcquire(DevExt->StateLock, NULL);
static_assert(sizeof(DevExt->State.BatteryStatus) == sizeof(*BatteryStatus));
RtlCopyMemory(&DevExt->State.BatteryStatus, BatteryStatus, sizeof(BATTERY_STATUS));
WdfWaitLockRelease(DevExt->StateLock);
BatteryClassStatusNotify(DevExt->ClassHandle);
Status = STATUS_SUCCESS;
SetBatteryStatusEnd:
return Status;
}
_Use_decl_annotations_
NTSTATUS SetBatteryInformation (WDFDEVICE Device, BATTERY_INFORMATION* BatteryInformation)
/*++
Routine Description:
Set the simulated battery information structure values.
Arguments:
Device - Supplies the device to set data for.
BatteryInformation - Supplies the new information data to set.
--*/
{
NTSTATUS Status = STATUS_INVALID_PARAMETER;
BATT_FDO_DATA* DevExt = GetDeviceExtension(Device);
ULONG ValidCapabilities = BATTERY_CAPACITY_RELATIVE |
BATTERY_IS_SHORT_TERM |
BATTERY_SYSTEM_BATTERY;
if ((BatteryInformation->Capabilities & ~ValidCapabilities) != 0) {
goto SetBatteryInformationEnd;
}
if (BatteryInformation->Technology > 1) {
goto SetBatteryInformationEnd;
}
WdfWaitLockAcquire(DevExt->StateLock, NULL);
static_assert(sizeof(DevExt->State.BatteryInfo) == sizeof(*BatteryInformation));
RtlCopyMemory(&DevExt->State.BatteryInfo, BatteryInformation, sizeof(BATTERY_INFORMATION));
// To indicate that battery information has changed, update the battery tag
// and notify the class driver that the battery status has updated. The
// status query will fail due to a different battery tag, causing the class
// driver to query for the new tag and new information.
UpdateTag(DevExt);
WdfWaitLockRelease(DevExt->StateLock);
BatteryClassStatusNotify(DevExt->ClassHandle);
Status = STATUS_SUCCESS;
SetBatteryInformationEnd:
return Status;
}