In the last blog post we investigated the fundamental characteristics of a sensor driver (properties and data fields) and how the Sensor Class Extension interacts with them. Here we will expand the basic driver template to include raising events and managing clients. A client is an application that has requested a connection to the driver through the class extension.
This blog post will focus on the Time Sensor Sample from the Windows Driver Kit. In general this driver creates a thread that periodically requests data from the ‘device’ (just the system time), and posts it to the class extension. This differs from the skeleton sample examined previously, which merely responds to synchronous data requests. Let’s look at the following code snippet from _EventThreadProc in CSampleEvents to illustrate this additional functionality. Note some comments have been modified.
In the first two blocks of code, we get a list of supported data fields and populate an IPortableDeviceValues collection with their current values. OnGetDataFields is the same method the class extension would call into if a client requested data on demand. Next we set the event type using the event property key. Setting the correct type is important because clients can register to be notified of specific types of events. For example an application may care about a new data event but not a state change event. The sensor and location platform defines several event property keys and types, which can be seen at MSDN. Finally we call into the PostDataEvent helper method to signal the class extension. Here is this method in its entirety.
PostDataEvent simply takes the data field values, adds them to an IPortableDeviceValeus collection, and posts the collection to the Sensor Class Extension. The extension will notify appropriate clients of new data.
The Sensor Class Extension also informs a sensor driver of client connection and subscription changes through several methods: OnClientConnect, OnClientDisconnect, On ClientSubscribeToEvents, and OnClientUnsubscribeFromEvents. These enable a driver to adjust its activity accordingly (such as disabling device sampling when no client is connected or adjusting event frequency). For instance, the Time Sensor Sample keeps track of client applications in a Clients list, which it uses to update the driver’s report interval.
To wrap up, these simple additions allow a sensor driver can take advantage of events in the sensor and location platform. This event-driven model can improve performance in both the driver and client applications. For more information about sensor driver events, take a look here. Best of luck!
-- Sensor & Location Platform Team
This posting is provided "AS IS" with no warranties and confers no rights.