Erick / Mbed 2 deprecated ICE-F412

Dependencies:   mbed-rtos mbed

ICE-Application/src/Utilities/utilities.cpp

Committer:
jmarkel44
Date:
2017-01-24
Revision:
0:61364762ee0e

File content as of revision 0:61364762ee0e:

/******************************************************************************
 * File:            utilities.cpp
 * Description:
 ******************************************************************************/
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include "mbed.h"
#include "global.h"
#include "cJSON.h"
#include "ICELog.h"
#include "utilities.h"

//
// function:            capture
// description:         captures the output from the __heapstats function
//
static int capture(void* pBuffer, char const* pFormatString, ...)
{
    char*   pStringEnd = (char*)pBuffer + strlen((char*)pBuffer);
    va_list valist;

    va_start(valist, pFormatString);
    return vsprintf(pStringEnd, pFormatString, valist);
}

//
// function:            Util_isSequenceSubControl
// description:         
//
bool Util_isSequenceSubControl(std::string controlFile)
{
    return (controlFile.substr(0, 4) == "seq_") ? true : false;
}

// 
// function:            Util_isVirtualOutput
// description:         
//
bool Util_isVirtualOutput(std::string output) 
{
    return (output.substr(0, 2) == "v_" ) ? true : false;
}

//
// function:            getHeapData
// description:         cleaned version of __heapstats()
//
// @param[in]           none
// @param[out]          none
// @return              none
//
std::string Util_getHeapData(void)
{
#ifndef __ICCARM__
    char data[256];
    memset(data, 0, sizeof(data));
    __heapstats(capture, data);
    std::string msg = data;
    std::string::size_type pos = 0;
    while ((pos = msg.find("\n", pos)) != std::string::npos) {
        msg.erase(pos, std::string::npos);
    }
    return msg;
#endif 
}

//
// function:            getLastBootTime
// description:         read the boot.time file and return the timestamp
//
// @param[in]           none
// @param[out]          none
// @return              none
//
std::string Util_getLastBootTime(void)
{
#ifdef MDOT_ICE
    mDot::mdot_file file = GLOBAL_mdot->openUserFile("boot.time", mDot::FM_RDONLY);
    if ( file.fd < 0 ) {
        logError("%s: failed to open boot.time", __func__);
        return "error";
    }
    char *data_buf = (char*) malloc(file.size);
    bool rc = GLOBAL_mdot->readUserFile(file, data_buf, file.size);
    if ( rc != true ) {
        logError("%s: failed to read boot.time", __func__);
        free(data_buf);
        GLOBAL_mdot->closeUserFile(file);
        return "error";
    }
    std::string bootTime = data_buf;

    free(data_buf);
    GLOBAL_mdot->closeUserFile(file);
    return bootTime;
#endif
    return 0;
}

std::string Util_GetFileById(const std::string id)
{
    // loop through all the files looking for a matching ID
    
}

std::string Util_GetIdbyFile(const std::string filename)
{
    char buf[MAX_FILE_SIZE];
    std::string id = "";
    int n = GLOBAL_mdot->readUserFile(filename.c_str(), (void*) buf, sizeof(buf));
    if ( n != sizeof(buf) ) {
        logError("%s: error reading %s", __func__, filename.c_str());
        return id;
    }
    cJSON *root = cJSON_Parse(buf);
    if ( !cJSON_HasObjectItem(root, "id") ) {
        logError("%s: failed to find id in file %s\n", __func__, filename.c_str());
    }
    id = cJSON_GetObjectItem(root, "id")->valuestring;
    cJSON_Delete(root);
    return id;
}