Etherios Cloud Connector very first porting for mbed. Tested in an LPC1768

Etherios Cloud Connector for Embedded v2.1.0.3 library for mbed. Early porting.

This port is centered mainly in the platform code. So it should work properly with the provided examples of send_data, device_request, data_points, RCI and firmware_update (stub implementation, not a real one... yet ;-)). Filesystem is not implemented yet, and some examples might need changes.

To run, it needs the following libraries: - mbed - mbed-rtos - EthernetInterface

Find more information (and the source code!) about Etherios Cloud Connector for Embedded here: http://www.etherios.com/products/devicecloud/support/connector and in: http://www.etherios.com

Committer:
spastor
Date:
Tue Dec 03 14:10:48 2013 +0000
Revision:
1:908afea5a49d
Parent:
0:1c358ea10753
Use internal Thread.h instead of Threads.h

Who changed what in which revision?

UserRevisionLine numberNew contents of line
spastor 0:1c358ea10753 1 /*
spastor 0:1c358ea10753 2 * Copyright (c) 2013 Digi International Inc.,
spastor 0:1c358ea10753 3 * All rights not expressly granted are reserved.
spastor 0:1c358ea10753 4 *
spastor 0:1c358ea10753 5 * This Source Code Form is subject to the terms of the Mozilla Public
spastor 0:1c358ea10753 6 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
spastor 0:1c358ea10753 7 * You can obtain one at http://mozilla.org/MPL/2.0/.
spastor 0:1c358ea10753 8 *
spastor 0:1c358ea10753 9 * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
spastor 0:1c358ea10753 10 * =======================================================================
spastor 0:1c358ea10753 11 */
spastor 0:1c358ea10753 12
spastor 0:1c358ea10753 13 #include "mbed.h"
spastor 0:1c358ea10753 14 #include "Thread.h"
spastor 0:1c358ea10753 15 #include <rtc_time.h>
spastor 0:1c358ea10753 16
spastor 0:1c358ea10753 17 extern "C" {
spastor 0:1c358ea10753 18 #include "connector_api.h"
spastor 0:1c358ea10753 19 #include "ecc_platform.h"
spastor 0:1c358ea10753 20 }
spastor 0:1c358ea10753 21 #include <stdio.h>
spastor 0:1c358ea10753 22 #include <stdlib.h>
spastor 0:1c358ea10753 23 #include <stdarg.h>
spastor 0:1c358ea10753 24
spastor 0:1c358ea10753 25 int connector_snprintf(char * const str, size_t const size, char const * const format, ...)
spastor 0:1c358ea10753 26 {
spastor 0:1c358ea10753 27 va_list args;
spastor 0:1c358ea10753 28 int result;
spastor 0:1c358ea10753 29
spastor 0:1c358ea10753 30 va_start(args, format);
spastor 0:1c358ea10753 31
spastor 0:1c358ea10753 32 #if __STDC_VERSION__ >= 199901L
spastor 0:1c358ea10753 33 result = vsnprintf(str, size, format, args);
spastor 0:1c358ea10753 34 #else
spastor 0:1c358ea10753 35 /*************************************************************************
spastor 0:1c358ea10753 36 * NOTE: Decided to have 64 bytes here considering following assumption *
spastor 0:1c358ea10753 37 * 1. In the worst case, only one format specifier will be used. *
spastor 0:1c358ea10753 38 * 2. Maximum of 48 bytes are used to represent a single precision value *
spastor 0:1c358ea10753 39 *************************************************************************/
spastor 0:1c358ea10753 40 #define SAFE_BUFFER_BYTES 64
spastor 0:1c358ea10753 41
spastor 0:1c358ea10753 42 if (size >= SAFE_BUFFER_BYTES)
spastor 0:1c358ea10753 43 {
spastor 0:1c358ea10753 44 result = vsprintf(str, format, args);
spastor 0:1c358ea10753 45 }
spastor 0:1c358ea10753 46 else
spastor 0:1c358ea10753 47 {
spastor 0:1c358ea10753 48 char local_buffer[SAFE_BUFFER_BYTES];
spastor 0:1c358ea10753 49 size_t const bytes_needed = vsprintf(local_buffer, format, args);
spastor 0:1c358ea10753 50 result = (bytes_needed < size) ? bytes_needed : size - 1;
spastor 0:1c358ea10753 51 if (result > 0)
spastor 0:1c358ea10753 52 {
spastor 0:1c358ea10753 53 memcpy(str, local_buffer, result);
spastor 0:1c358ea10753 54 str[result] = '\0';
spastor 0:1c358ea10753 55 result = bytes_needed;
spastor 0:1c358ea10753 56 }
spastor 0:1c358ea10753 57 }
spastor 0:1c358ea10753 58 #undef SAFE_BUFFER_BYTES
spastor 0:1c358ea10753 59 #endif
spastor 0:1c358ea10753 60 va_end(args);
spastor 0:1c358ea10753 61
spastor 0:1c358ea10753 62 return result;
spastor 0:1c358ea10753 63 }
spastor 0:1c358ea10753 64
spastor 0:1c358ea10753 65 connector_callback_status_t app_os_malloc(size_t const size, void ** ptr)
spastor 0:1c358ea10753 66 {
spastor 0:1c358ea10753 67 connector_callback_status_t status = connector_callback_abort;
spastor 0:1c358ea10753 68
spastor 0:1c358ea10753 69 *ptr = malloc(size);
spastor 0:1c358ea10753 70 if (*ptr != NULL)
spastor 0:1c358ea10753 71 {
spastor 0:1c358ea10753 72 status = connector_callback_continue;
spastor 0:1c358ea10753 73 }
spastor 0:1c358ea10753 74 else
spastor 0:1c358ea10753 75 {
spastor 0:1c358ea10753 76 APP_DEBUG("app_os_malloc: Failed to malloc\n");
spastor 0:1c358ea10753 77 }
spastor 0:1c358ea10753 78
spastor 0:1c358ea10753 79 return status;
spastor 0:1c358ea10753 80 }
spastor 0:1c358ea10753 81
spastor 0:1c358ea10753 82 connector_callback_status_t app_os_free(void const * const ptr)
spastor 0:1c358ea10753 83 {
spastor 0:1c358ea10753 84 void * const free_ptr = (void *)ptr;
spastor 0:1c358ea10753 85
spastor 0:1c358ea10753 86 if (free_ptr != NULL)
spastor 0:1c358ea10753 87 {
spastor 0:1c358ea10753 88 free(free_ptr);
spastor 0:1c358ea10753 89 }
spastor 0:1c358ea10753 90 else
spastor 0:1c358ea10753 91 {
spastor 0:1c358ea10753 92 APP_DEBUG("app_os_free: called with NULL\n");
spastor 0:1c358ea10753 93 }
spastor 0:1c358ea10753 94
spastor 0:1c358ea10753 95 return connector_callback_continue;
spastor 0:1c358ea10753 96 }
spastor 0:1c358ea10753 97
spastor 0:1c358ea10753 98 connector_callback_status_t app_os_get_system_time(unsigned long * const uptime)
spastor 0:1c358ea10753 99 {
spastor 0:1c358ea10753 100 static time_t start_system_up_time;
spastor 0:1c358ea10753 101 time_t present_time = time(NULL);
spastor 0:1c358ea10753 102
spastor 0:1c358ea10753 103 if (start_system_up_time == 0)
spastor 0:1c358ea10753 104 start_system_up_time = present_time;
spastor 0:1c358ea10753 105
spastor 0:1c358ea10753 106 present_time -= start_system_up_time;
spastor 0:1c358ea10753 107 *uptime = (unsigned long) present_time;
spastor 0:1c358ea10753 108
spastor 0:1c358ea10753 109 return connector_callback_continue;
spastor 0:1c358ea10753 110 }
spastor 0:1c358ea10753 111
spastor 0:1c358ea10753 112 connector_callback_status_t app_os_yield(connector_status_t const * const status)
spastor 0:1c358ea10753 113 {
spastor 0:1c358ea10753 114 wait(0.01);
spastor 0:1c358ea10753 115 return connector_callback_continue;
spastor 0:1c358ea10753 116 }
spastor 0:1c358ea10753 117
spastor 0:1c358ea10753 118 extern "C" void mbed_reset();
spastor 0:1c358ea10753 119
spastor 0:1c358ea10753 120 static connector_callback_status_t app_os_reboot(void)
spastor 0:1c358ea10753 121 {
spastor 0:1c358ea10753 122
spastor 0:1c358ea10753 123 APP_DEBUG("app_os_reboot!\n");
spastor 0:1c358ea10753 124
spastor 0:1c358ea10753 125 mbed_reset();
spastor 0:1c358ea10753 126 return connector_callback_continue;
spastor 0:1c358ea10753 127 }
spastor 0:1c358ea10753 128
spastor 0:1c358ea10753 129 connector_callback_status_t app_os_handler(connector_request_id_os_t const request,
spastor 0:1c358ea10753 130 void * const data)
spastor 0:1c358ea10753 131 {
spastor 0:1c358ea10753 132 connector_callback_status_t status;
spastor 0:1c358ea10753 133
spastor 0:1c358ea10753 134 switch (request)
spastor 0:1c358ea10753 135 {
spastor 0:1c358ea10753 136 case connector_request_id_os_malloc:
spastor 0:1c358ea10753 137 {
spastor 0:1c358ea10753 138 connector_os_malloc_t * p = (connector_os_malloc_t *)data;
spastor 0:1c358ea10753 139 status = app_os_malloc(p->size, &p->ptr);
spastor 0:1c358ea10753 140 }
spastor 0:1c358ea10753 141 break;
spastor 0:1c358ea10753 142
spastor 0:1c358ea10753 143 case connector_request_id_os_free:
spastor 0:1c358ea10753 144 {
spastor 0:1c358ea10753 145 connector_os_free_t * p = (connector_os_free_t *)data;
spastor 0:1c358ea10753 146 status = app_os_free(p->ptr);
spastor 0:1c358ea10753 147 }
spastor 0:1c358ea10753 148 break;
spastor 0:1c358ea10753 149
spastor 0:1c358ea10753 150 case connector_request_id_os_system_up_time:
spastor 0:1c358ea10753 151 {
spastor 0:1c358ea10753 152 connector_os_system_up_time_t * p = (connector_os_system_up_time_t *)data;
spastor 0:1c358ea10753 153 status = app_os_get_system_time(&p->sys_uptime);
spastor 0:1c358ea10753 154 }
spastor 0:1c358ea10753 155 break;
spastor 0:1c358ea10753 156
spastor 0:1c358ea10753 157 case connector_request_id_os_yield:
spastor 0:1c358ea10753 158 {
spastor 0:1c358ea10753 159 connector_os_yield_t * p = (connector_os_yield_t *)data;
spastor 0:1c358ea10753 160 status = app_os_yield(&p->status);
spastor 0:1c358ea10753 161 }
spastor 0:1c358ea10753 162 break;
spastor 0:1c358ea10753 163
spastor 0:1c358ea10753 164 case connector_request_id_os_reboot:
spastor 0:1c358ea10753 165 status = app_os_reboot();
spastor 0:1c358ea10753 166 break;
spastor 0:1c358ea10753 167
spastor 0:1c358ea10753 168 default:
spastor 0:1c358ea10753 169 APP_DEBUG("app_os_handler: unrecognized request [%d]\n", request);
spastor 0:1c358ea10753 170 status = connector_callback_unrecognized;
spastor 0:1c358ea10753 171 break;
spastor 0:1c358ea10753 172 }
spastor 0:1c358ea10753 173
spastor 0:1c358ea10753 174 return status;
spastor 0:1c358ea10753 175 }
spastor 0:1c358ea10753 176
spastor 0:1c358ea10753 177
spastor 0:1c358ea10753 178