Sebastián Pastor / EtheriosCloudConnector
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers os.cpp Source File

os.cpp

00001 /*
00002  * Copyright (c) 2013 Digi International Inc.,
00003  * All rights not expressly granted are reserved.
00004  *
00005  * This Source Code Form is subject to the terms of the Mozilla Public
00006  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
00007  * You can obtain one at http://mozilla.org/MPL/2.0/.
00008  *
00009  * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
00010  * =======================================================================
00011  */
00012 
00013 #include "mbed.h"
00014 #include "Thread.h"
00015 #include <rtc_time.h>
00016 
00017 extern "C" {
00018 #include "connector_api.h"
00019 #include "ecc_platform.h"
00020 }
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <stdarg.h>
00024 
00025 int connector_snprintf(char * const str, size_t const size, char const * const format, ...)
00026 {
00027     va_list args;
00028     int result;
00029 
00030     va_start(args, format);
00031 
00032 #if __STDC_VERSION__ >= 199901L
00033     result = vsnprintf(str, size, format, args);
00034 #else
00035     /*************************************************************************
00036      * NOTE: Decided to have 64 bytes here considering following assumption  *
00037      * 1. In the worst case, only one format specifier will be used.         *
00038      * 2. Maximum of 48 bytes are used to represent a single precision value *
00039      *************************************************************************/
00040     #define SAFE_BUFFER_BYTES 64
00041 
00042     if (size >= SAFE_BUFFER_BYTES)
00043     {
00044         result = vsprintf(str, format, args);
00045     }
00046     else
00047     {
00048         char local_buffer[SAFE_BUFFER_BYTES];
00049         size_t const bytes_needed = vsprintf(local_buffer, format, args);
00050         result = (bytes_needed < size) ? bytes_needed : size - 1;
00051         if (result > 0)
00052         {
00053             memcpy(str, local_buffer, result);
00054             str[result] = '\0';
00055             result = bytes_needed;
00056         }
00057     }
00058     #undef SAFE_BUFFER_BYTES
00059 #endif
00060     va_end(args);
00061 
00062     return result;
00063 }
00064 
00065 connector_callback_status_t app_os_malloc(size_t const size, void ** ptr)
00066 {
00067     connector_callback_status_t status = connector_callback_abort;
00068 
00069     *ptr = malloc(size);
00070     if (*ptr != NULL)
00071     {
00072         status = connector_callback_continue;
00073     }
00074     else
00075     {
00076         APP_DEBUG("app_os_malloc: Failed to malloc\n");
00077     }
00078 
00079     return status;
00080 }
00081 
00082 connector_callback_status_t app_os_free(void const * const ptr)
00083 {
00084     void * const free_ptr = (void *)ptr;
00085 
00086     if (free_ptr != NULL)
00087     {
00088         free(free_ptr);
00089     }
00090     else
00091     {
00092         APP_DEBUG("app_os_free: called with NULL\n");
00093     }
00094 
00095     return connector_callback_continue;
00096 }
00097 
00098 connector_callback_status_t app_os_get_system_time(unsigned long * const uptime)
00099 {
00100     static time_t start_system_up_time;
00101     time_t present_time = time(NULL);
00102 
00103     if (start_system_up_time == 0)
00104        start_system_up_time = present_time;
00105 
00106     present_time -= start_system_up_time;
00107     *uptime = (unsigned long) present_time;
00108 
00109     return connector_callback_continue;
00110 }
00111 
00112 connector_callback_status_t app_os_yield(connector_status_t const * const status)
00113 {
00114     wait(0.01);
00115     return connector_callback_continue;
00116 }
00117 
00118 extern "C" void mbed_reset();
00119 
00120 static connector_callback_status_t app_os_reboot(void)
00121 {
00122     
00123     APP_DEBUG("app_os_reboot!\n");
00124 
00125     mbed_reset();
00126     return connector_callback_continue;
00127 }
00128 
00129 connector_callback_status_t app_os_handler(connector_request_id_os_t const request,
00130                                            void * const data)
00131 {
00132     connector_callback_status_t status;
00133 
00134     switch (request)
00135     {
00136     case connector_request_id_os_malloc:
00137         {
00138             connector_os_malloc_t * p = (connector_os_malloc_t *)data;
00139             status = app_os_malloc(p->size, &p->ptr);
00140         }
00141         break;
00142 
00143     case connector_request_id_os_free:
00144         {
00145             connector_os_free_t * p = (connector_os_free_t *)data;
00146             status = app_os_free(p->ptr);
00147         }
00148         break;
00149 
00150     case connector_request_id_os_system_up_time:
00151         {
00152             connector_os_system_up_time_t * p = (connector_os_system_up_time_t *)data;
00153             status = app_os_get_system_time(&p->sys_uptime);
00154         }
00155         break;
00156 
00157     case connector_request_id_os_yield:
00158         {
00159             connector_os_yield_t * p = (connector_os_yield_t *)data;
00160             status = app_os_yield(&p->status);
00161         }
00162         break;
00163 
00164     case connector_request_id_os_reboot:
00165         status = app_os_reboot();
00166         break;
00167 
00168     default:
00169         APP_DEBUG("app_os_handler: unrecognized request [%d]\n", request);
00170         status = connector_callback_unrecognized;
00171         break;
00172     }
00173 
00174     return status;
00175 }
00176 
00177 
00178