44
Using the Sensor & Location API on Windows Embedded Standard 7 to Create Exciting Connected Applications Alexander Wechsler Wechsler Consulting GmbH & Co SESSION CODE: WEM306

Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Embed Size (px)

Citation preview

Page 1: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Using the Sensor & Location API on Windows Embedded Standard 7 to Create Exciting Connected Applications

Alexander WechslerWechsler Consulting GmbH & Co. KG

SESSION CODE: WEM306

Page 2: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Where am I and what is going onhere?Smart beings need smart devices!

The Sensor & Location Platform

Page 3: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

…your computer knew where youwere and adjusted itself accordingly?

Update local weather, news, eventsAutomatically adjust clock

…your computer could sense its environment and optimize your experience?

Adjust display backlight based on ambient lightOptimize UI elements for improved readability

What If…?

Page 4: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Location devices are exposed as virtual COM ports

Exclusive application accessNot secureProprietary data formats (NMEA, others)

GPS doesn’t work indoorsHard to support multiple technologies at once

Sensors are integrated as vertical solutionsApplications need to know sensorhardware specificsLimited adoption and scope

Limitations Of Sensors in the Past

Page 5: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Sensor Platform Overview

Windows 7 features a new API for working with sensorsCOM basedWorks with drivers using the Sensor Class Extension

BenefitsNo need to target vendor-specific APIs or to know hardware specificsConsistent interface for all sensor typesPrivacy & security

Page 6: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

ArchitectureHang on….

Page 7: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Sensor Platform Architecture

UMDF Sensor Driver

Sensor Class Extension

Sensor API

Application

Location and Other Sensors Control Panel

SensorDevice

Application

User

System

This is a partial diagram of the Sensor and Location Platform, showing only Sensor-related parts

Page 8: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Windows Sensor And Location Platform

Provides unified driver model for all types of sensor devices

Physical sensors (e.g., GPS devices, Light Sensors)Logical sensor (e.g., Wi-Fi triangulation resolver)

Provides standard APIs for accessing sensorsSensor API: C++/COM

Raw access to any sensorLocation API: C++/COM, IDispatch (scriptable)

Abstracted API for location data

Puts user in control of information disclosure

Page 9: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Sensor API Architecture

COM-based API (includes sensorsapi.h & sensors.h)Consists of these main interfaces:

ISensorManagerSensor enumeration, attachment event, request permissions

ISensorGet/set properties, get report, events (new report, detachment, state change, custom) and more

ISensorDataReportGet sensors report data fields

Page 10: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Privacy & Access Control

Location data is considered PIIUser consent is required to share data

All sensors are disabled by defaultAdmin rights required to enable a sensorSensors can be configured on a per-user basis“Enable Dialog” invoked by applications

Page 11: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Sensor API Architecture

Page 12: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

What is a sensor?Enumerated via category and type GUIDs

Category represents what is being sensed (e.g. environment, location, motion, electrical)

Type represents how it is being sensed (e.g. thermometer, GPS, accelerometer, voltage)

PropertiesRead-only (e.g. model) or read-write (e.g. report interval). Sensors

may have custom properties.Data

Get (sensor-specific) data report object synchronously (not recommended) or asynchronously (events)

EventsState change, leave (detach), data updated, custom

StateIs sensor working properly? Do you have access?

Page 13: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Enumerating Sensors#include <sensorsapi.h>#include <sensors.h>

HRESULT hr;CComPtr<ISensorManager> pSensorManager;

pSensorManager.CoCreateInstance(CLSID_SensorManager);

CComPtr<ISensorCollection> pALSCollection;CComPtr<ISensor> pALSSensor;

// Get all the ALS sensors on the systempSensorManager->GetSensorsByType(SENSOR_TYPE_AMBIENT_LIGHT, &pALSCollection);

hr = pSensorManager->RequestPermissions(0, // Owner window pALSCollection, // Collection of sensors requiring permissionsTRUE); // Modal flag

if(SUCCEEDED(hr)){ pALSCollection->GetAt(0, &pALSSensor);}

Page 14: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Getting Current Light LevelSTDMETHODIMP CALSEventSink::OnDataUpdated(

ISensor* pSensor, ISensorDataReport* pNewData){

PROPVARIANT lightLevel; PropVariantInit(&lightLevel);

// Get the sensor reading from the ISensorDataReport objectpNewData->GetSensorValue(SENSOR_DATA_TYPE_LIGHT_LEVEL_LUX, &lightLevel);

// Extract the float value from the PROPVARIANT objectfloat luxValue = V_FLOAT(lightLevel);

// Normalize the light sensor datadouble lightNormalized = ::pow(luxValue, 0.4) / 100.0;

// Handle UI changes based on the normalized LUX data // which ranges from 0.0 - 1.0 for a lux range of// 0 lux to 100,000 lux, this method represents such a // handler that would be implemented in your application UpdateUI(lightNormalized);

PropVariantClear(&lightLevel);return S_OK;

}

Page 15: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Managed Sensor Wrapper Win 7 API Pack

Enables access to Windows 7 Sensors from managed code.Strongly typed sensor properties, events and data report properties

No need to lookup GUIDs and data typesYou can still work in the traditional way

Provides wrappers for several well-known sensor typesExtensible – write wrappers for custom sensor typesIt is a sample library not a full product

Open source with no supportUse at your own risk

Page 16: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Sensor Wrapper Architecture

The three classes retain the same roles as they had in the COM API, only wrapping properties and events

Page 17: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Sensor Wrapper Architecture

Sensor is an abstract base class with a derived type for each sensor typeDerived types can add properties and events

Page 18: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Sensor Wrapper Architecture

There’s also a SensorDataReport-derived type for each sensor type. Provides a strongly typed way to access data

Page 19: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Location Platform Overview

The Windows 7 Location API is built on top of the sensor APICOM based (includes locationapi.h)It is a high level abstraction

Leverages sensors which provide location informationSome sensors (location providers) may be logical

WiFi hotspot triangulation

Page 20: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Sensor and Location Platform Architecture – The Big Picture

UMDF Sensor Driver

Sensor Class Extension

Sensor API

Gadget or Script Application

Location and Other Sensors Control Panel

SensorDevice

Location APILocation IDispatch Interface

Application

UMDF Sensor Driver

Sensor Class Extension

Logical Location Sensor(Triangulation)

User

System

Page 21: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Location Platform Benefits

Single API call to answer “Where am I?”Provider (i.e. GPS, IP Resolver, WiFi, etc…) IndependentSynchronous and Asynchronous modelsScript / automation compatible

Automatic transition between providersMost accurate providers have priority

Concurrent access for multiple applicationsDefault Location

Provided by user as fallback when no other sources are available

Page 22: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Location API Architecture

Consists of four main interfacesILocation – Get location report, query status, register for notification, request permissionsILocationReport – Base interface. Two derived types exist: ILatLongReport and ICivicAddressReport

Page 23: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Types of Location Data

Geographic Data (ILatLongReport)Latitude, longitude, altitude, associated error requiredMost common formatBest format for precise locationCan reverse geo-code later

Civic Address (ICivicAddressReport)Zip, Country requiredMost human readableBest for ‘rough’ location estimates,street directions

Page 24: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Location Wrapper Overview.NET 4.0

Wraps COM API in a friendly and familiar .NET styleUser-extensible

It is a sample library not a full productOpen source with no supportUse at your own risk

Page 25: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Location Wrapper ArchitectureConsists of two main classes: LocationProvider and LocationReport

Page 26: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Getting L& S into Windows Embedded Standard 7Can you say feature pack….

Page 27: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

ToDo

Page 28: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Accessing SensorsGo, get data….

Page 29: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Enumerating Sensors

Accelerometer3D[] sensors = SensorManager.GetSensorsByType<Accelerometer3D>();Accelerometer3D a3dSensor = null;if (sensors.Length > 0){ a3dSensor = sensors[0]; SensorManager.RequestPermission(IntPtr.Zero, true, a3dSensor);}

Page 30: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Receiving Data Reports from Sensor

a3dSensor.DataUpdated += OnDataUpdated;

void OnDataUpdated(Sensor sensor, SensorDataReport dataReport){ Accelerometer3DReport a3dReport = (Accelerometer3DReport)dataReport; Console.WriteLine("X: {0} Y: {1} Z: {2}", a3dReport.AxisX_G, a3dReport.AxisY_G, a3dReport.AxisZ_G); }

Page 31: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Getting a Location ReportHRESULT hr;IID civicReportIID = IID_ICivicAddressReport;

CComPtr<ILocation> pLocation;CComPtr<ILocationReport> pReport;

hr = pLocation.CoCreateInstance(CLSID_Location);hr = pLocation->RequestPermissions(0, &civicReportIID, 1, TRUE);hr = pLocation->GetReport(civicReportIID, &pReport);

CComPtr<ICivicAddressReport> pCivicAddressReport;pReport.QueryInterface<ICivicAddressReport>(&pCivicAddressReport);

BSTR str;hr = pCivicAddressReport->GetCountryRegion(&str);_tprintf(_T("Country/region: %s\n"), str);SysFreeString(str);

Page 32: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Accessing SensorsGo, get data…. in all tools and many coding languages ….everywhere

Page 33: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Using Location Wrappervar provider = new CivicAddressLocationProvider(10000);LocationProvider.RequestPermissions(IntPtr.Zero,

true, provider);var report = (CivicAddressLocationReport)

provider.GetReport();Console.WriteLine("Country/region: “, report.CountryOrRegion);

Page 34: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Working with the magic badgeDemos / Demos / Demos

Page 35: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Advanced Embedded ScenariosDo you have crazier ideas???

Page 36: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Summary

Native support for SensorTypes of location data, default locationLocation APIManaged Sensor API Wrapper

Custom sensors

ToDo

Page 37: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Track Resourceshttp://www.WindowsEmbedded.comhttp://msdn.microsoft.com/en-us/windowsembeddedhttp://social.msdn.microsoft.com/Forums/en-US/category/embeddedwindows/http://social.msdn.microsoft.com/Forums/en-US/category/windowsembeddedcompacthttps://connect.microsoft.com/windowsembeddedce

Page 38: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Related ContentBreakout Sessions

WEM201 | Discover Windows Embedded Standard 7 as Your Next Application PlatformWEM301 | Deploying Windows Embedded Standard 7 with StyleWEM302 | Explore the Multimedia Potential of Windows Embedded Standard 7WEM303 | Gamechanger: Using Microsoft Silverlight for Windows Embedded to Create an Amazing Embedded UIWEM305 | How to Choose a Windows Embedded Operating SystemWEM306 | Using the Sensor & Location API on Windows Embedded Standard 7 to Create Exciting Connected ApplicationsWEM307 | Windows Embedded Compact: New Tools and Developer StoryWEM308 | Windows Embedded Overview: Demos of the Latest and Upcoming ReleasesWEM309 | Programming Microsoft Silverlight for Windows Embedded Using Microsoft .NET

Interactive SessionsWEM01-INT | Build a Secure Device with Windows Embedded Standard 7WEM02-INT | Delivering Flexible Peripheral Support for Point of SaleWEM03-INT | How Windows Embedded Solutions Help to Protect the EnvironmentWEM05-INT | What a Desktop Developer Needs to Know to Develop for Windows EmbeddedWEM06-INT | Windows Embedded Compact CompeteWEM07-INT | Server Appliances with Windows Embedded ServersWEM08-INT | Roundtable: Windows Embedded @ Tech·Ed 2011 - Tell Us What You Want to Learn

Page 39: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Related ContentHands-on Labs

WEM01-HOL | Build Your Own Embedded SystemWEM04-HOL | Porting Third-Party Drivers into Image Configuration Editor

Product Demo Stations (all on Windows Embedded booth)TLC-46 | Get Your Hands on Windows EmbeddedTLC-47 | Powered by Windows Embedded POSReady – Touch ScreenTLC-48 | The Intel® Intelligent Digital Signage Proof of ConceptTLC-49 | Windows Embedded AutomotiveTLC-50 | Windows Embedded Device Showcase

Page 40: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Resources

www.microsoft.com/teched

Sessions On-Demand & Community Microsoft Certification & Training Resources

Resources for IT Professionals Resources for Developers

www.microsoft.com/learning

http://microsoft.com/technet http://microsoft.com/msdn

Learning

Page 41: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Complete an evaluation on CommNet and enter to win!

Page 42: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

Sign up for Tech·Ed 2011 and save $500 starting June 8 – June 31st

http://northamerica.msteched.com/registration

You can also register at the

North America 2011 kiosk located at registrationJoin us in Atlanta next year

Page 43: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

© 2010 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to

be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Page 44: Alexander Wechsler Wechsler Consulting GmbH & Co. KG SESSION CODE: WEM306

JUNE 7-10, 2010 | NEW ORLEANS, LA