Vehicle status data is obtained from the IVIO_Device interface to two types of vehicle devices: polled devices and streaming devices. Polled devices provide relatively static data, such as coolant temperature. Streaming devices provide dynamic data, such as RPM.
The following code example shows how to obtain a polled device interface and how to retrieve data from that device.
IVIO_Device *pDevice;
VARIANT vCoolantTemp;
LONG lCoolantTemp;
hr = CoCreateInstance(CLSID_VIO_COOLANT_TEMP, NULL,
CLSCTX_INPROC_SERVER, IID_VIODEVICE,
(PVOID *) &pDevice);
VariantInit(&vCoolantTemp);
// Read the coolant temperature from the vehicle.
hr = pDevice->GetData(&vCoolantTemp, 2000);
// If you read the coolant temperature successfully, save it.
lCoolantTemp = V_I4(&vCoolantTemp);
// Free the resources of the VARIANT.
VariantClear(&vCoolantTemp);
// Release the reference to the device.
pDevice->Release();
If you need data from a device that provides streaming data, you must not only obtain an interface to the device, but also create a callback object. The following code example shows how to obtain the necessary interface and how to invoke the callback object.
IVIO_Device *pDevice;
IVIO_EventSink *pEventSink;
hr = CoCreateInstance(CLSID_VIO_VEHICLE_SPEED, NULL,
CLSCTX_INPROC_SERVER, IID_VIODEVICE,
(PVOID *) &pDevice);
CVioSpeedCallBack *pCallback = new CVioSpeedCallback;
pCallback->QueryInterface(IID_VIOEVENTSINK, (PVOID *) &pEventSink);
// Release the callback object.
pCallback->Release();
// Start the streaming vehicle speedometer device.
hr = pDevice->SetNotify(NULL, pEventSink, 0);
// Other code
// Before exiting the application, turn off the streaming device.
hr = pDevice->SetNotify(NULL, NULL, 0);
// Release the reference to the event sink object.
pEventSink->Release();
// Release the reference to the device.
pDevice->Release();
The following code example shows how to create a callback object.
class CVioSpeedCallBack : public IVIO_EventSink
{
public:
CVioSpeedCallback();
virtual ~CVioSpeedCallback();
// Standard IUnknown methods.
// IVIO_EventSink method.
STDMETHOD (ReceiveMsg) (LPVARIANT pData, LPARAM lParam);
};
// Note: The callback function returns on its own thread.
STDMETHODIMP CVioSpeedCallback::ReceiveMsg(LPVARIANT pData, LPARAM
lParam)
{
USHORT usVehicleSpeed;
// Get the speed from the VARIANT.
usVehicleSpeed = V_UI2(pData);
return S_OK;
}