George Roussos Senior Program Manager Arvind Aiyar Senior Software Engineer Building apps for USB Accessories.

Download Report

Transcript George Roussos Senior Program Manager Arvind Aiyar Senior Software Engineer Building apps for USB Accessories.

George Roussos
Senior Program Manager
Arvind Aiyar
Senior Software Engineer
Building apps for USB Accessories
Develop apps that can connect with
hardware from the past and present
Windows 10 introduces
1) USB Accessories on Phones
2) Windows.Devices.SerialCommunication
enabling you to develop apps that connect with
hardware from the past and present.
USB Accessories
•
Windows 10 Mobile SKU adds
support for USB connected
accessories on new Phones and
Tablets via USB Dual Role.
•
Developers can build accessories
based upon open industry
standard hardware without perunit royalties.
USB Accessories in Windows 10 Mobile SKU
Windows 10 supports two kinds of
USB Accessories in Mobile SKU:
1.
2.
Typical accessories
Vendor specific accessories
Typical accessories on Mobile SKU
Examples
Accessories that just
work with OS
•
•
Windows natively
supports these
accessory types
regardless of who
makes the
hardware.
Windows provides
built-in OS
experiences for
many of them
Audio
Files
Point of
Service
Accessories
Namespace
Microphone
MIDI
Speakers
Windows.Media.Capture
Windows.Devices.Midi
Removable Storage
Windows.Storage
Magnetic Stripe Reader
Windows.Devices.PointofService
.BarcodeScanner
Barcode Scanner
Windows.Devices.PointofService
.MagneticStripeReader
Vendor specific accessories
•
Examples: Fitness devices,
Robots, Smart Toys, etc
Windows.Devices.HumanInterfaceDevice
Custom HID Device Access
Windows.Devices.SerialCommunication
Custom Serial Device Access
Windows.Devices.Usb
Custom USB Device Access
Driver installation for vendor specific accessories
Use OS inbox drivers for HID,
USBSerial, or WinUSB.
• Accessories should report hardware
or compatible IDs that install these
drivers.
• Mobile SKU does NOT support
installing vendor drivers from WU
• Audience Challenge:
Produce a USB to RS232 cable that
reports these compatible IDs
•
API
Compatible IDs
HID
Follow Standard. Report
Vendor_Specific Top Level Collection
Serial
USB\Class_02&SubClass_02&Prot_01
USB\Class_02&SubClass_02
WinUSB
WinUSB (Winusb.sys) Installation
Start developing apps for USB Accessories
1. Start developing a universal app on desktop w/
USB port
2. Make sure the UX of your app lays out properly on
phones
3. Once available get a Dual Role capable Mobile
device to test your app with accessories
Identifying mobile systems that support
USB Accessories
New Phones and Tablets that
support USB dual role
• Before you buy a new device
•
•
Should be clear in device marketing if USB dual
role is supported or not
•
On your current device
USB Settings UX informs users if their mobile
device supports USB connections or not
Serial Communication using WinRT
Overview
Device Protocol APIs
New Windows
Runtime APIs:
• Bluetooth
• Bluetooth Smart
• HID
• USB
• Serial
• Serial
Devices
Unified and built-in model for
device discovery, access and
instantiation
Serial in
Windows 10
New Windows Runtime API that
simplifies communicating to/from
your device
Works with Universal Apps – write
once, deploy everywhere …
Prerequisites
1. Working knowledge of Serial Communication
Introduction to Serial Communications – MSDN
•
2 . Good to know …
Before
you begin…
•
•
Win32 Serial Programming interface - MSDN
.NET serial programming interface - MSDN
3. Knowledge of Windows Runtime concepts
and paradigms
•
•
•
Async operations
Data buffers and streams
Fundamentals of working with devices
Architecture
AppContainer
Architecture
diagram
Windows Store app
Windows.Devices.SerialCommunication API
Device Access
Broker
Native Serial layer
Serial device
Demo: Arduino
Getting Started
Accessing devices from the Windows Runtime
Capability
1) Author an app manifest
Declare the correct capability for your device
2) Device discovery
Device discovery
Communication
Find out what devices are connected to your system
3) Find the device you’re looking for
Information about a specific device
4) Communicate
Access information on your device
Cleanup
5) Clean up resources
App capabilities (i.e. app manifest)
Capability
Device discovery
Communication
1. Declare the device capability
•
2. Declare the device
•
•
Device Id=“any” Device Id=“vidpid:<vid> <pid>” - if you want a specific device
3. Declare usages
•
Cleanup
DeviceCapability Name=“serialcommunication”
Function Type=“name:serialPort”
Editing the Package.appxmanifest
AppXmanifest: example
Package.appxmanifest:
<Capabilities>
<DeviceCapability Name="serialcommunication">
<Device Id="any">
<Function Type="name:serialPort" />
</Device>
</DeviceCapability >
<DeviceCapability Name="serialcommunication">
<Device Id="vidpid:2341 0043">
<Function Type="name:serialPort" />
</Device>
</DeviceCapability>
</Capabilities>
Device discovery
•
Finding serial devices on your system
Capability
Device discovery
Communication
Cleanup
Capability
1. Use the static DeviceSelector methods in SerialDevice to build
a serial-specific AQS:
• SerialDevice.GetDeviceSelector();
• SerialDevice.GetDeviceSelector(portName);
Device discovery
• SerialDevice.GetDeviceSelectorFromUsbVidPid(vid, pid);
• You can augment the selector to be more specific.
2. Use the Device Enumeration APIs to find the serial device
Communication
•
DeviceInformation.FindAllAsync(aqsFilter)
OR
•
DeviceInformation.CreateWatcher(aqsFilter)
•
SerialDevice FromIdAsync(DeviceInformation.Id)
Cleanup
Device discovery
using Windows.Devices.SerialCommunication;
using Windows.Devices.Enumeration
// Create an AQS using on of the static DeviceSelector methods
// USB VID/PID selector
string selector = SerialDevice.GetDeviceSelectorFromUsbVidPid(vid, pid);
// Friendly Name selector
string selector2 = SerialDevice.GetDeviceSelector(“COM1”);
// Pass the selector to FindAllAsync to get a collection of DeviceInformation
// objects.
DeviceInformationCollection deviceInformation;
deviceInformation
= await DeviceInformation.FindAllAsync(selector);
Device instantiation
await MainPage.Current.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
{
var openedDevice = await SerialDevice.FromIdAsync(deviceInformation.Id);
if (openedDevice == null)
{
}
else
{
// Begin watching for disconnect. Begin accessing the device.
}
}
Device instantiation
Runtime Checks:
1. Capability declarations in the manifest.
2. Allowed by OS Policy.
3. Exclusivity.
4. Consent and Dynamic Access.
If your app is denied access as a result of any of the above, the
function returns a NULL SerialDevice object.
SerialDevice is NULL ?
if (openedDevice == null)
{
var deviceAccessStatus = DeviceAccessInformation.CreateFromId(deviceInformation.Id).CurrentStatus;
if (deviceAccessStatus == DeviceAccessStatus.DeniedByUser)
{
notificationMessage = "Access to the device was blocked by the user : " + deviceInformation.Id;
}
else if (deviceAccessStatus == DeviceAccessStatus.DeniedBySystem)
{
// (Possible failure with Package.appxmanifest declaration, seen at development time only)
}
else
{
// Most likely the device is opened by another app, but cannot be sure
notificationMessage = "Unknown error, possibly opened by another app : " + deviceInformation.Id;
}
Basic communication
•
How it all comes together …
Basic communication
Declare
capability
Device discovery
Communication
Cleanup
Basic Communication
//
// Configure the device
//
serialDevice.BaudRate = 9600;
serialDevice.Parity = SerialParity.None;
serialDevice.StopBits = SerialStopBitCount.One;
serialDevice.Handshake = SerialHandshake.None;
serialDevice.DataBits = 8;
Basic Communication
///
/// Write to the device
///
var dataWriter = new DataWriter(serialDevice.OutputStream);
dataWriter.WriteString(“hello");
var bytesWritten = await dataWriter.StoreAsync();
this.NotifyUser("Write completed - " + bytesWritten.ToString() + " bytes written,
NotifyType.StatusMessage);
dataWriter.DetachStream();
Basic Communication
///
/// Read from the device
///
var dataReader = new DataReader(serialDevice.InputStream);
uint readBufferLength = 8;
UInt32 bytesRead = await dataReader.LoadAsync(readBufferLength);
if (bytesRead > 0)
{
string temp = dataReader.ReadString(bytesRead);
this.NotifyUser("Read completed - " + bytesRead.ToString() + " bytes were read",
NotifyType.StatusMessage);
}
dataReader.DetachStream();
Closing the device
Declare
capability
1. When done with device communication,
the app must close the SerialDevice.
Device discovery
Communication
Cleanup
2. Be aware of language-specific nuances
for object close and deletion.
Closing the device
C++:
delete device;
device = nullptr;
C#:
device.Dispose();
device = null;
JS:
this._device.close();
this._device = null;
Suspension/Resume
App must register
for suspension
1. It must close the SerialDevice as it will be
invalidated.
2. It should not send any new I/O.
3. On app resume, app must re-open the
device.
Arduino: Redux
•
How it all comes together …
Additional features
Background tasks
• App can do long running operations using
background tasks.
• Supports all existing triggers e.g. System Event
trigger, Control Channel trigger, DeviceUse
trigger
• Tasks aren’t throttled like other system
background tasks (no CPU time quota) but will
run with reduced priority to keep foreground
apps responsive.
Tracing:
• Windows.Devices.SerialCommunication
• Device Broker
Details on MSDN page
Introducing the Windows App Model
Moving to Universal: Porting to Windows 10 from Windows 8.1 XAML or Windows
Phone Silverlight
Developing Windows 10 Universal Apps in Visual Studio 2015
Universal App Packaging and Deployment for Windows 10 Devices
Windows 10 App Lifecycle:
From Activation & Suspension to Background Execution and Multitasking
Optimizing Universal Apps for Continuum
New Retail Peripherals and NFC/HCE Support in Windows 10
Developing universal audio and video apps for Windows
Building Rich, Contextually Aware Applications Using Sensors
Windows for Makers: Raspberry Pi 2, Arduino and More!
Internet of Things: Overview
Building Windows Apps that Discover, Connect, and Interact with Other Devices
and Cloud Services Using AllJoyn
Resources: Typical accessories on Mobile SKU
Examples
Accessories that just
work with OS
•
•
Windows natively
supports these
accessory types
regardless of who
makes the
hardware.
Windows provides
built-in OS
experiences for
many of them
Audio
Files
Point of
Service
Accessories
Namespace
Microphone
MIDI
Speakers
Windows.Media.Capture
Windows.Devices.Midi
Removable Storage
Windows.Storage
Magnetic Stripe Reader
Windows.Devices.PointofService
.BarcodeScanner
Barcode Scanner
Windows.Devices.PointofService
.MagneticStripeReader
Windows.Devices.HumanInterfaceDevice
Custom HID Device Access
Windows.Devices.SerialCommunication
Custom Serial Device Access
Windows.Devices.Usb
Custom USB device access
USB Accessories
1. Start developing a Universal Windows App on desktop w/ USB port
2. Make sure the UX of you’re app lays out properly on phones
3. Once available, get a Dual Role capable Mobile device to test your
app with accessories
4. Make sure your vendor specific accessories report the correct
hardware IDs to work with Mobile SKU :
Feedback: Anything missing in our APIs?
mailto:[email protected]