An mbed wrapper around the helium-client to communicate with the Helium Atom

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HeliumUtil.cpp Source File

HeliumUtil.cpp

00001 /*
00002  * Copyright 2017, Helium Systems, Inc.
00003  * All Rights Reserved. See LICENCE.txt for license information
00004  */
00005 
00006 #include "mbed.h"
00007 #include "HeliumUtil.h"
00008 
00009 int
00010 report_status(int status, int result)
00011 {
00012     if (helium_status_OK == status)
00013     {
00014         if (result == 0)
00015         {
00016             DBG_PRINTF("Succeeded\n");
00017         }
00018         else
00019         {
00020             DBG_PRINTF("Failed - %d\n", result);
00021         }
00022     }
00023     else
00024     {
00025         DBG_PRINTF("Failed\n");
00026     }
00027     return status;
00028 }
00029 
00030 
00031 void
00032 helium_connect(Helium * helium)
00033 {
00034     while (!helium->connected())
00035     {
00036         DBG_PRINTF("Connecting - ");
00037         int status = helium->connect();
00038         if (report_status(status) != helium_status_OK)
00039         {
00040             wait_ms(1000);
00041         }
00042     }
00043 }
00044 
00045 void
00046 channel_create(Channel * channel, const char * channel_name)
00047 {
00048     int8_t result;
00049     int    status;
00050     do
00051     {
00052         // Ensure we're connected
00053         helium_connect(channel->helium);
00054         DBG_PRINTF("Creating Channel - ");
00055         status = channel->begin(channel_name, &result);
00056         // Print status and result
00057         if (report_status(status, result) != helium_status_OK)
00058         {
00059             wait_ms(1000);
00060         }
00061     } while (helium_status_OK != status || result != 0);
00062 }
00063 
00064 void
00065 channel_send(Channel *    channel,
00066              const char * channel_name,
00067              void const * data,
00068              size_t       len)
00069 {
00070     int    status;
00071     int8_t result;
00072 
00073     do
00074     {
00075         // Try to send
00076         DBG_PRINTF("Sending - ");
00077         status = channel->send(data, len, &result);
00078         report_status(status, result);
00079         // Create the channel if any service errors are returned
00080         if (status == helium_status_OK && result != 0)
00081         {
00082             channel_create(channel, channel_name);
00083         }
00084         else if (status != helium_status_OK)
00085         {
00086             wait_ms(1000);
00087         }
00088     } while (helium_status_OK != status || result != 0);
00089 }