Here's the guts of BLS...
Code:
g_mq = CreateMsgQueue(L"AvianWavesBattLineSvcPwrNotifQueue", &mqopt);
if (g_mq)
{
g_pn = RequestPowerNotifications(g_mq, PBT_TRANSITION | PBT_POWERSTATUSCHANGE | PBT_POWERINFOCHANGE);
if (g_pn)
{
while (WaitForSingleObject(g_mq, INFINITE) == WAIT_OBJECT_0)
{
// Eat the power notification
ReadMsgQueue(g_mq, &buf, sizeof(DWORD) * 16, &numberRead, -1, &flags);
// Refresh the battery on any power change (we repeat several times since sometimes the battery level does not refresh in sync with the status change
RefreshBatteryStatus();
g_nowRepeat = 1;
if (g_battTimerNowProc == 0)
{
g_battTimerNowProc = SetTimer(0, 0, 2000, battTimerNowProc);
}
}
}
g_pn = NULL;
}
The following seems problematic to me:
Code:
while (WaitForSingleObject(g_mq, INFINITE) == WAIT_OBJECT_0)
Sleep indefinitely until the OS gives us a message on the message queue.
Considering the fact that MSN sais:
<http://msdn.microsoft.com/en-us/library/ms687032(VS.85).aspx>
The WaitForSingleObject function can wait for the following objects:
Change notification
Console input
Event
Memory resource notification
Mutex
Process
Semaphore
Thread
Waitable timer
I would have dropped that completly and would have changed:
Code:
ReadMsgQueue(g_mq, &buf, sizeof(DWORD) * 16, &numberRead, -1, &flags);
We don't care what the message is because we perform the same action on any message, so we just do a ReadMsgQueue to eat the message, but don't use the data in the buffer.
to:
while (1){
if (ReadMsgQueue(g_mq, &buf, sizeof(DWORD) * 16, &numberRead,
<INFINITE>, &flags)){....
and you know what . . .
your assumption that the battery level hardware is jittering and producing thousands of messages unnecessarily might also be possible.
you know, those little volemeters and ampermeters installed in these devices might have accuracy problems, and stability problems,
and might be effected by environmental RF transmissions (and considering a phone is an RF transmitter . . .)
Maybe you should dump a "debug mode" log of all the messages with the batt level and time you received the notification,
just to check that theory.