Blog about development IOT applications on Windows

Menu

Intel Galileo Windows Application To Azure Storage

Intel Galileo on Windows platform currently supports only C++ project in Visual Studio, so if you want to connect your Galileo project to Azure Storage, you will need to code it in C++ and use only C++ libraries. And..there is some good and bad news about that

The good news:

there is a C++ SDK availible that enables calling REST services. It is C++ REST SDK (code name Casablanca)

there is a C++ Azure Storage library, which is using C++ REST SDK and provides C++ classes for the communication with Storage services on Azure

The bad news:

both libraries won’t work on Galileo without some changes/workarounds.

standard Windows libraries don’t work exactly the same way on standard workstation Windows and on Galileo. Because of this differences, calling Azure services from Galileo wan’t work at all.

But, the good news..there is a workaround, that I’m going to explain, which enable calling Azure services directly from Galileo. In this post I will discribe both C++ REST and Azure Storage library and explain what changes are needed to make it works on Galileo Board.

Casablanca Project

Casablanca is a code name of the C++ REST SKD library project. It’s a library from Microsoft published as an open source project on CodePlex. https://casablanca.codeplex.com

Casablanca allows you to write native code to access REST services and uses an asynchronous approach to consuming HTTP and JSON based services.The API surface of C++ Rest SDK is platform-agnostic, and it already supports many platforms including iOS and Android. And of course, you can use it on Windows. But what about Windows on Galileo? At this moment you can use this SDK in Galileo project only with some modifications. Currently only version 2.2. can be used on Galileo. Instructions how to use it are here: http://ms-iot.github.io/content/Casablanca.htm

As you can read on this location, you should build a project by yourself using an important change in project properties:

Set Enable Enhanced Instruction Set = No Enhanced Instructions (/arch:IA32) for both Debug and Release

Why is that necessary? Normally, when compiling a Win32 application for a desktop PC, we will compile against Win32 libraries contained in C:\Windows\System32. When targeting the Intel Galileo board you will notice that the default Intel Galileo Wiring App template links against a single library, mincore.lib. So, we need to compile against mincore.lib in order to obtain code capable of running on the Galileo as the mappings for System and Win32 functions are completely different in Windows for IoT and contained in this particular lib.

We should also consider the hardware present on the Galileo board itself. Intel outfits the board with an Intel® Quark™ SoC X1000 application processor, a 32-bit, single-core, single-thread, Intel® Pentium® processor instruction set architecture (ISA)-compatible, operating at speeds up to 400 MHz. This processor does not support enhanced instruction set including SSE, SSE2, AVX, or AVX 2. And that’s the reason we should ensure all code is compiled with the /arch:IA32 compiler flag

Using these changes you should create your own Casablanca 2.2 package that will be used in Azure Storage C++ library.

Azure Storage Client Library for C++

Currently the Azure Storage Client Library for C++ library is a CTP (Community Technology Preview) release. Current version is 0.4.0. And if you just take it to your Galileo project, it won’t work. But, the good news, this is an open source project and with some modifications, it works just fine.

After that, you should change the depending Casablanca project version to your local 2.2 version of the C++ REST SDK and compile a solution. If you already prepare your own Casablanca 2.2 version as described in previous section, you can open the solution “Microsoft.WindowsAzure.Storage.v120.sln”, go to Tools “NuGet Package Manager-> Package Manager Console” and type:

Install-Package cpprestsdk -Version 2.2.0

After you build the solution, you should take the compiled dll and lib and take it to your Galileo project.

Let’s see how it works:

Create Galileo Wiring App Project

Install NuGet Package CPP Rest SDK Version 2.2

Install-Package cpprestsdk -Version 2.2.0

Manually add Azure Storage Client Library for C++ in your project

Copy was and wascore folders to your project folder

Copy wastorage.lib to your project folder

Copy wastorage.dll to your output folder on your desktop. After deploy on Galileo, you should manually copy this dll to Galileo in output folder

The show begins when you try to run it on Galileo board. Let’s start a romore debug session. Don’t forget to copy wastorage.dll to your Galileo deploy folder (“\\mygalileo\c$\test”).

When you run the remote session, you will probably see something like this:

Entry point not found doesn’t really explains much. The problem is that in some depending dll, some method is missing. To enable more diagnostic output, you can change registry key GlobalFlag=2 under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager.

After that you’ll see that following methods is missing UnregisterClassW. This method is defined in User32.dll and if you compare User32.dll in you workstation Windows with the one in Galileo, you’ll see that these files are pretty much different. The Galileo version doesn’t have a lot of method which are defined in standard Windows version. Our Application need only one Method form this dll and it is UnregisterClassW, which is in our scenario not very important, but without this method the application can’t run at all. So let see hot to fix/workaround this problem.

Galileo Workaround with Azure Storage Client Library

First we need to remove dependency on User32 library in Microsoft.WindowsAzure.Storage project and we will also remove dependency to some other default libraries:

Compile the project and copy new version on wastorage.lib and dll to you Galileo project. Compile your Galileo project, copy wastorage.dll manually to your test folder on Galileo and start the application on Galileo. The “entry point not found” error is gone. Application is running!

But, we have another problem now. The Azure storage returns an error:

The remote server returned an error: (403) Server failed to authenticate the request.

Date Format in REST Azure Storage call from Galileo

The Azure storage returns 403 error only if it’s called from Galileo. It works perfect when it’s called from a standard workstation. If you compare POST command (using visual studio, fiddler or some other diagnostic tool) you will see the difference in date (x-ms-date) format in header.

All authenticated requests must include the Coordinated Universal Time (UTC) timestamp for the request. You can specify the timestamp either in the x-ms-date header, or in the standard HTTP/HTTPS Date header. If both headers are specified on the request, the value of x-ms-date is used as the request’s time of creation.

The storage services ensure that a request is no older than 15 minutes by the time it reaches the service. This guards against certain security attacks, including replay attacks. When this check fails, the server returns response code 403 (Forbidden).

I our case the problem is in format. From unknown reason (probably a bug in Galileo Windows), date Format in Galileo differs from the expected date format in Azure REST header. So there is also a workaround for this problem. In method shared_key_authentication_handler::sign_request (azure-storage-cpp-master\Microsoft.WindowsAzure.Storage\src\authentication.cpp) change the code to avoid system formatting of x-ms-date in REST signing:

Hi!
If you already have weather station information, you just need to send it to Azure Storage. I did it using Azure Storage Client Library for C++ as described in this blog post. Soon, I will publish my new blog post about similar scenario. How to collect temperature and humidity using DHT11 sensor and send it to Azure.