Crestron, a leading provider of home automation and building controls, finally has a rich Apple TV IP driver, including support for the Gen 4 box, this week. The module (£50) was developed by Ultamation in the U.K., using Crestron's complicated but robust programming language SIMPL#, based on C#. Ultamation managing director Oliver Hall explains the power of a true objected-oriented language, and how it was used to develop the Apple TV driver.
Crestron has always provided a number of ways to deliver a working home- or building-control system, tailored to the client’s requirements. For some solutions, there are wizards and auto-generated programming which, while they get the job done, are inherently limited in one way or another. Crestron is investing considerable effort into this area with such things as Studio, AVFramework and Home Elements, in an attempt to lower the barriers to deploying Crestron solutions. Good programmers are – and quite rightly, in my opinion – a valuable resource.
As a Crestron dealer with a strong bias towards the “custom” solution, we either already have an in-house developed framework or prefer to remain unconstrained by automated programming tools. For some time we’ve pushed the available programming environments to their limits and occasionally found those limits to fall short of the design vision. That can be embarrassing when clients point toward manufacturers with less expensive solutions and say, “But they can do it, why can’t Crestron?”
Crestron’s SIMPL# changes that. Although it’s been available for over three years now, there seems to be slow uptake within the Crestron programming community. There are some inescapable reasons for that but, in this article, I’ll try and provide some incentive, taken from personal experience, to persuade Crestron programmers to take the plunge.
The Challenges of SIMPL#
As I see it, there are two genuine challenges to an existing Crestron programmer moving into SIMPL#.
First of all, the 3-Series processor platform is based on Windows CE which uses the .NET Compact Framework. Unfortunately, Microsoft abandoned support for .NET CF after Visual Studio 2008, so that means to develop Crestron SIMPL#, you need to obtain a copy of the fairly ancient Visual Studio 2008 Professional SP1. This can present a prohibitively high cost to entry for many. However, if you can manage it, do get a copy. It’s worth it.
Secondly, SIMPL# is C# – a full-blown, mature language, with all the bells and whistles that come with a contemporary language. That’s a very different beast compared to SIMPL or even SIMPL+ (which, despite the use of the ‘+’ hasn’t been within a country mile of anything remotely object orientated.) That will likely present a challenge for programmers who haven’t benefited from any formal training in today’s programming paradigms.
It’s difficult to convey the benefits that C#, as a true object-orientated language with a rich wealth of library support, can bring to bear on programming effort to anyone who hasn’t experienced the power (and programming pleasure) of a good inheritance structure, or polymorphism, but I’ll do my best.
The Virtues of SIMPL#
I’m going to use one of our own modules to introduce some of the features of SIMPL# that I think are game changers for Crestron programmers by looking at one of those “but they can do it!” scenarios: controlling Apple TV, including the new 4th-generation device.
The AppleTV is a device born in the connected consumer age of protocols such as Apple’s ‘Bonjour’ and UPnP. The guys that designed these protocol suites were free to express them using the latest standards and technologies and the fact that Crestron didn’t have any native programming support for multicast IP, or even HTTP(S), and required “rolling your own” wasn’t of any concern. Of course, from our point of view, the lack of these foundations makes writing a device driver to support a device such as the AppleTV uneconomical, if not totally impracticable.
Through SIMPL#, Crestron provides a group of classes to address one of these issues in the Crestron.SimpleSharp.Net namespace.
Crestron.SimplSharp.Net.HttpClient makes communicating with RESTful HTTP servers fairly trivial and a simple request/response can be implemented in just a few lines of code. Remote control of the Apple TV uses a proprietary Apple protocol known as DAAP which, fortunately, uses HTTP as its transport.
The HttpClientRequest and HttpClientResponse classes provide access to the guts of the HTTP communication and even though the Apple TV sometimes responds with compressed data, the Crestron.SimplSharp.CrestronIO.Compression classes DeflateStream and GZipStream mean that handling those responses can also be accomplished with another few lines. These are things that would have taken tens or hundreds of programmer-hours to achieve in SIMPL+.
VIDEO: IP Control of Apple TV (up to Gen 4) through Crestron Home Automation
Once basic communication has been established we can turn our attention to managing the conversation between the Crestron system and an Apple TV. Apple uses its Home Sharing mechanism to authenticate devices and we made use of a number Crestron.SimplSharp.CrestronIO classes in SIMPL#, such as FileStream, StreamReader, Directory and Path, to simplify the caching and retrieval of authentication tokens and, in our case, thread safety was maintained through Crestron.SimplSharp.CCriticalSection’s Enter() and Leave() methods.
Marshalling the control system requests, such as cursor up, down, select, etc. through to the Apple TVs is a typical programming problem that fits the Producer-Consumer design pattern. For those not familiar with the idea of design patterns, the notion is simply that many programming problems, when teased out to their constituent parts, and fallacious dependencies are removed, fall into a small number of templates or “patterns.” Using these patterns avoids reinventing the wheel and improves robustness at the same time as reducing development time.
The Producer-Consumer pattern often has a specific type of “collection’” class at its heart, called a Queue and the Crestron.SimplSharp.CrestronQueue provides the perfect class with which to tackle the problem. What we wanted was a FIFO mechanism into which we could push commands, and from which requests could be removed, dispatched to the Apple TV and the responses processed.
A queue is the perfect data structure for this and the CrestronQueue exposes another supremely useful feature of the C# language: Generics.
Generics provide a way for the programmer to declare a class that collaborates with another class and provides compile time type checking. We therefore created a CrestronQueue whose queued elements are declared as the type of our custom AppleTVRequest class. These requests are added to the queue with the Enqueue( AppleTVRequest request ) method.
The CrestronQueue not only provides the required FIFO behavior, but also implements a Dequeue() method which efficiently blocks if there’s nothing waiting in the queue to be processed.
The only thing left was then to handle the Dequeue() loop in a thread that would not impact the main processing thread of the Crestron processor.
Again, Crestron provides a way to achieve this through the Crestron.SimplSharp.CrestronInvoke class. The static CrestronInvoke.BeginInvoke( callback ) method allows a method to be run in another thread, allowing the main thread to continue.
In this way, we were able to create a queue that manages the HTTP request-response conversation continuously, as quickly as the AppleTV can process the requests. This neatly separates the code that prepares requests from the module’s SIMPL+ wrapper and adds them to the queue from the more complex code that authenticates with, and transfers requests and responses between, the Apple TVs.
The Gift of #
Prior to the release of SIMPL#, developing an Apple TV IP control module may not have been impossible, but would certainly have been uneconomical. There’s no question that programming in SIMPL# presents a steep learning curve for anyone who’s yet to dip their toe into the O-O programming pool, which is laden with new concepts – some of which aren’t all that intuitive to begin with – but once you become familiar with C# syntax, then explore the .NET and Crestron frameworks and realize how much of the heavy lifting they can do for you, and eventually master the O-O paradigm, you’ll wonder how you ever got anything worthwhile done before.
And even if you don’t buy Visual Studio 2008 to get into SIMPL# right away, there is nothing to get in your way of learning generic C# with one of the free Community editions of Visual Studio.
For more information on the AppleTV IP control module, please visit the Ultamation shop @ www.ultamation.com.
VIDEO: Using slider on Crestron home automation touchscreen to navigate through Apple TV shows.
FAQ for Ultamation's AppleTV IP Remote
Ultamation’s module for Crestron control provides IP control and feedback for the recent generations of the AppleTV. This document attempts to answer the common questions.
COMPATIBILITY
Which generations of AppleTV does the module support?
The module has been tested with generations 2, 3 and the latest 4th generation.
What if Apple change the way they do things?
There is no guarantee that Apple won’t change the way the AppleTV is controlled, as they did between the 1st generation unit and the next. Whilst this is a concern, we believe the likelihood of Apple making a breaking change is limited since the protocol is common between a number of end-points (e.g. iTunes, AppleTV, the “Remote” iOS app). Given that the latest 4th generation device continues to use Home Sharing authentication and a consistent protocol we feel this further reduces the likelihood of future compatibility issues.
Which Crestron processors will the module work on?
The module requires Crestron’s 3-Series processors as it is written in SIMPL#. 2-Series processors and earlier are not supported.
Apple TV IP Control driver for Crestron home automation is available now from Ultamation for £50.Will this work with Crestron PYNG?
Not at present. We’d be happy to discuss this with Crestron.
Will this work with the Smart Graphics Media Player application?
The initial release is not compatible with the Media Player application, but we are considering a “professional” version that will integrate with the application.
What if I want to add another Apple TV to the system?
The module supports multiple AppleTVs but each instance of the module requires a unique licence key which is tied to the Apple Id used for Home Sharing and the device’s IP address in the installation.
What if the client has multiple Home Sharing IDs?
Each discrete Home Sharing Id will require its own licence. Multiple modules can use different Home Sharing Ids against different AppleTVs. We have not tested dynamic changing of the Home Sharing Id on a single AppleTV and this is not supported.
Is the control compatible with all Bluetooth remote functions
At present, we can only implement what Apple exposes via the IP protocol and the various control methods (IR, Bluetooth, IP) are not yet in parity. We understand that Apple will release a new iOS app and updates to TVOS to provide the same functionality via IP as the Bluetooth remote offers. At that time, we will look to implement these functions. Until then, some features – such as some apps and games – may not work with the module.
FEATURES
What are the advantages of controlling over IP compared to IR?
There are many. From an installation perspective, the AppleTV will not require an IR bud, which can represent a sizable chunk of the module cost. These are susceptible to falling off and also require additional installation effort (if done neatly!). There is no possibility of “cross-talk” between IR buds on adjacent AppleTVs. A free IR port will be required which, once the on-board outputs are exhausted, can be an expensive addition. The module uses the Ethernet cable that is already necessary for communication. Not only do we offer more discrete control over IR, but we also provide feedback for certain functions.
What does the module provide?
The standard module provides the same control as the silver AppleTV remote – i.e. up, down, left, right, play/pause and menu. We’ve also included transport control skip back, skip forward, play, pause and stop. In addition, we provide feedback for various functions, such as player state, device name, and so on. Please refer to the module datasheet for detailed information.
Is the control robust?
We believe so. Please watch our YouTube video where we demonstrate control of three AppleTVs. We disconnect the Ethernet cable from one and the module reconnects when the connection is remade. We have also tested by removing the power from the AppleTV and once it has rebooted, control is restored.
TECHNICAL
I thought Apple had locked things down… how does it all work?
We’re not going to give the game away, but the idea that Apple have “locked down” the AppleTV is not quite accurate. An encrypted DRM mechanism is involved in most streaming activities, but for control and general feedback, the Crestron system need only authenticate with the AppleTV. Apple’s “Home Sharing” is the basis for this.
Do I need to configure anything on my AppleTV?
Yes – you will need to give your AppleTV a static IP address, and turn on Home Sharing. Both pieces of information must match the details used when purchasing the module. Please refer to Apple’s support site https://www.apple.com/support/ for information on how to do this for each of generation of device. Also, set ‘Sleep’ to ‘Never’ (see below).
Does the module require access to the Internet?
For initial Home Sharing authentication, the module does require a valid Internet connection and working DNS. Once successfully authenticated, the Home Sharing “key” is cached in a file on the Crestron processor (NVRAMUltamationhs.cache). This cache is used from then on, and the Internet is no longer required (though the AppleTV functionality will be severely limited). The cache file (or entries from it) can be copied from a device which has previously authenticated for a given Home Sharing id.
Can I let my AppleTV go to sleep?
No, as – without IR or Bluetooth – there is no way to wake up the AppleTV (we have not tested WoL though this may provide an alternative to turning off sleep).
How many Apple TVs can I control within a system?
There are no limits within the module on the maximum number of devices in a single system, but there may be a limit based on processor performance. We are unaware of what that limit might be.
I do not have a static IP address for my Apple TV?
A static address is a requirement of the module. Please ensure this consideration has been made before purchasing the module.
Can I use any IP address for the AppleTV?
Apple’s Home Sharing requires all participating devices to be on the same subnet/broadcast domain. If in doubt, ensure that your AppleTV addresses are in the same logical network as your Crestron processor.
My Apple TV is not responding to commands, what should I do?
Contact support@ultamation.com and we will do everything we can to get you up and running.
What if I (or my client) resets the AppleTV?
If the AppleTV is reset to factory defaults, it will stop working with the module. You will need to apply the correct Home Sharing, IP address and sleep settings to get it working again.
LICENCING
Why do you need the client’s Home Sharing Id & Password?
Apple’s mechanism for control and sharing information requires that devices be part of the same Home Sharing domain. That means that the Crestron system also needs to authenticate with the Home Sharing services. Therefore, Home Sharing must be used on the AppleTV for the module to work.
Why do you require the client’s Home Sharing Id at time of purchase?
The licence key is linked to a specific Home Sharing Id and this cannot be changed once purchased, so it is extremely important that the Apple Id is entered carefully and accurately.
Why do you require the Apple TV’s static IP Address at time of purchase?
The licence key is linked to a AppleTV to be controlled’s IP address and this cannot be changed once purchased, so it is essential that the IP address is defined before purchase, and entered very carefully and accurately using standard dotted quad notation (e.g. 192.168.1.123).
What if I want to change the Apple TV’s IP Address after purchase?
A licence cannot be transferred. Any change to Home Sharing Id or IP address, or errors made at the time of purchase will require an additional licence to be purchased.
Can I use this module on multiple systems?
The module is licenced for a specific AppleTV device and Home Sharing account, so a new licence is required for each system.
What if the client changes his Home Sharing ID?
This will require a new module licence key to be purchased, and the program will require recompiling with the new Home Sharing Id (and possibly password) and key entered into the module parameters.
I have misspelt the Home Sharing Id on purchase, what do I do?
Unfortunately, this will require the purchase of an additional licence. If you are unsure about the information required at the point of purchase, we will be happy to validate it for you. Please contact support@ultamation.com
The client no longer wants Apple TV control, can I get a refund?
No. Once purchased, the Home Sharing Id & IP address combination is unique and cannot be returned.
Oliver Hall founded Ultamation, a Crestron-authorized dealer based in Liverpool, Great Britain, in 2007. Prior to launching the company, he spent 15 years with Sony Computer Entertainment in a lead role covering both the infrastructure and software architecture of PlayStation Network. He is passionate about developing the best-possible solutions in home automation for his clients and is also an advocate for greater adoption of contemporary programming paradigms and methodologies in the CI Industry.
Ultmation has won numerous industry awards for innovative programming and user experience products. The company is the developer of Theme Creator, a Crestron-endorsed, standalone Windows application for Crestron Smart Graphics theme development.