Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
ICE-Application/src/Utilities/utilities.cpp@0:61364762ee0e, 2017-01-24 (annotated)
- Committer:
- jmarkel44
- Date:
- Tue Jan 24 19:05:33 2017 +0000
- Revision:
- 0:61364762ee0e
Port from IAR to Nucleo-F412 board
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| jmarkel44 | 0:61364762ee0e | 1 | /****************************************************************************** |
| jmarkel44 | 0:61364762ee0e | 2 | * File: utilities.cpp |
| jmarkel44 | 0:61364762ee0e | 3 | * Description: |
| jmarkel44 | 0:61364762ee0e | 4 | ******************************************************************************/ |
| jmarkel44 | 0:61364762ee0e | 5 | #include <string.h> |
| jmarkel44 | 0:61364762ee0e | 6 | #include <stdarg.h> |
| jmarkel44 | 0:61364762ee0e | 7 | #include <stdlib.h> |
| jmarkel44 | 0:61364762ee0e | 8 | #include "mbed.h" |
| jmarkel44 | 0:61364762ee0e | 9 | #include "global.h" |
| jmarkel44 | 0:61364762ee0e | 10 | #include "cJSON.h" |
| jmarkel44 | 0:61364762ee0e | 11 | #include "ICELog.h" |
| jmarkel44 | 0:61364762ee0e | 12 | #include "utilities.h" |
| jmarkel44 | 0:61364762ee0e | 13 | |
| jmarkel44 | 0:61364762ee0e | 14 | // |
| jmarkel44 | 0:61364762ee0e | 15 | // function: capture |
| jmarkel44 | 0:61364762ee0e | 16 | // description: captures the output from the __heapstats function |
| jmarkel44 | 0:61364762ee0e | 17 | // |
| jmarkel44 | 0:61364762ee0e | 18 | static int capture(void* pBuffer, char const* pFormatString, ...) |
| jmarkel44 | 0:61364762ee0e | 19 | { |
| jmarkel44 | 0:61364762ee0e | 20 | char* pStringEnd = (char*)pBuffer + strlen((char*)pBuffer); |
| jmarkel44 | 0:61364762ee0e | 21 | va_list valist; |
| jmarkel44 | 0:61364762ee0e | 22 | |
| jmarkel44 | 0:61364762ee0e | 23 | va_start(valist, pFormatString); |
| jmarkel44 | 0:61364762ee0e | 24 | return vsprintf(pStringEnd, pFormatString, valist); |
| jmarkel44 | 0:61364762ee0e | 25 | } |
| jmarkel44 | 0:61364762ee0e | 26 | |
| jmarkel44 | 0:61364762ee0e | 27 | // |
| jmarkel44 | 0:61364762ee0e | 28 | // function: Util_isSequenceSubControl |
| jmarkel44 | 0:61364762ee0e | 29 | // description: |
| jmarkel44 | 0:61364762ee0e | 30 | // |
| jmarkel44 | 0:61364762ee0e | 31 | bool Util_isSequenceSubControl(std::string controlFile) |
| jmarkel44 | 0:61364762ee0e | 32 | { |
| jmarkel44 | 0:61364762ee0e | 33 | return (controlFile.substr(0, 4) == "seq_") ? true : false; |
| jmarkel44 | 0:61364762ee0e | 34 | } |
| jmarkel44 | 0:61364762ee0e | 35 | |
| jmarkel44 | 0:61364762ee0e | 36 | // |
| jmarkel44 | 0:61364762ee0e | 37 | // function: Util_isVirtualOutput |
| jmarkel44 | 0:61364762ee0e | 38 | // description: |
| jmarkel44 | 0:61364762ee0e | 39 | // |
| jmarkel44 | 0:61364762ee0e | 40 | bool Util_isVirtualOutput(std::string output) |
| jmarkel44 | 0:61364762ee0e | 41 | { |
| jmarkel44 | 0:61364762ee0e | 42 | return (output.substr(0, 2) == "v_" ) ? true : false; |
| jmarkel44 | 0:61364762ee0e | 43 | } |
| jmarkel44 | 0:61364762ee0e | 44 | |
| jmarkel44 | 0:61364762ee0e | 45 | // |
| jmarkel44 | 0:61364762ee0e | 46 | // function: getHeapData |
| jmarkel44 | 0:61364762ee0e | 47 | // description: cleaned version of __heapstats() |
| jmarkel44 | 0:61364762ee0e | 48 | // |
| jmarkel44 | 0:61364762ee0e | 49 | // @param[in] none |
| jmarkel44 | 0:61364762ee0e | 50 | // @param[out] none |
| jmarkel44 | 0:61364762ee0e | 51 | // @return none |
| jmarkel44 | 0:61364762ee0e | 52 | // |
| jmarkel44 | 0:61364762ee0e | 53 | std::string Util_getHeapData(void) |
| jmarkel44 | 0:61364762ee0e | 54 | { |
| jmarkel44 | 0:61364762ee0e | 55 | #ifndef __ICCARM__ |
| jmarkel44 | 0:61364762ee0e | 56 | char data[256]; |
| jmarkel44 | 0:61364762ee0e | 57 | memset(data, 0, sizeof(data)); |
| jmarkel44 | 0:61364762ee0e | 58 | __heapstats(capture, data); |
| jmarkel44 | 0:61364762ee0e | 59 | std::string msg = data; |
| jmarkel44 | 0:61364762ee0e | 60 | std::string::size_type pos = 0; |
| jmarkel44 | 0:61364762ee0e | 61 | while ((pos = msg.find("\n", pos)) != std::string::npos) { |
| jmarkel44 | 0:61364762ee0e | 62 | msg.erase(pos, std::string::npos); |
| jmarkel44 | 0:61364762ee0e | 63 | } |
| jmarkel44 | 0:61364762ee0e | 64 | return msg; |
| jmarkel44 | 0:61364762ee0e | 65 | #endif |
| jmarkel44 | 0:61364762ee0e | 66 | } |
| jmarkel44 | 0:61364762ee0e | 67 | |
| jmarkel44 | 0:61364762ee0e | 68 | // |
| jmarkel44 | 0:61364762ee0e | 69 | // function: getLastBootTime |
| jmarkel44 | 0:61364762ee0e | 70 | // description: read the boot.time file and return the timestamp |
| jmarkel44 | 0:61364762ee0e | 71 | // |
| jmarkel44 | 0:61364762ee0e | 72 | // @param[in] none |
| jmarkel44 | 0:61364762ee0e | 73 | // @param[out] none |
| jmarkel44 | 0:61364762ee0e | 74 | // @return none |
| jmarkel44 | 0:61364762ee0e | 75 | // |
| jmarkel44 | 0:61364762ee0e | 76 | std::string Util_getLastBootTime(void) |
| jmarkel44 | 0:61364762ee0e | 77 | { |
| jmarkel44 | 0:61364762ee0e | 78 | #ifdef MDOT_ICE |
| jmarkel44 | 0:61364762ee0e | 79 | mDot::mdot_file file = GLOBAL_mdot->openUserFile("boot.time", mDot::FM_RDONLY); |
| jmarkel44 | 0:61364762ee0e | 80 | if ( file.fd < 0 ) { |
| jmarkel44 | 0:61364762ee0e | 81 | logError("%s: failed to open boot.time", __func__); |
| jmarkel44 | 0:61364762ee0e | 82 | return "error"; |
| jmarkel44 | 0:61364762ee0e | 83 | } |
| jmarkel44 | 0:61364762ee0e | 84 | char *data_buf = (char*) malloc(file.size); |
| jmarkel44 | 0:61364762ee0e | 85 | bool rc = GLOBAL_mdot->readUserFile(file, data_buf, file.size); |
| jmarkel44 | 0:61364762ee0e | 86 | if ( rc != true ) { |
| jmarkel44 | 0:61364762ee0e | 87 | logError("%s: failed to read boot.time", __func__); |
| jmarkel44 | 0:61364762ee0e | 88 | free(data_buf); |
| jmarkel44 | 0:61364762ee0e | 89 | GLOBAL_mdot->closeUserFile(file); |
| jmarkel44 | 0:61364762ee0e | 90 | return "error"; |
| jmarkel44 | 0:61364762ee0e | 91 | } |
| jmarkel44 | 0:61364762ee0e | 92 | std::string bootTime = data_buf; |
| jmarkel44 | 0:61364762ee0e | 93 | |
| jmarkel44 | 0:61364762ee0e | 94 | free(data_buf); |
| jmarkel44 | 0:61364762ee0e | 95 | GLOBAL_mdot->closeUserFile(file); |
| jmarkel44 | 0:61364762ee0e | 96 | return bootTime; |
| jmarkel44 | 0:61364762ee0e | 97 | #endif |
| jmarkel44 | 0:61364762ee0e | 98 | return 0; |
| jmarkel44 | 0:61364762ee0e | 99 | } |
| jmarkel44 | 0:61364762ee0e | 100 | |
| jmarkel44 | 0:61364762ee0e | 101 | std::string Util_GetFileById(const std::string id) |
| jmarkel44 | 0:61364762ee0e | 102 | { |
| jmarkel44 | 0:61364762ee0e | 103 | // loop through all the files looking for a matching ID |
| jmarkel44 | 0:61364762ee0e | 104 | |
| jmarkel44 | 0:61364762ee0e | 105 | } |
| jmarkel44 | 0:61364762ee0e | 106 | |
| jmarkel44 | 0:61364762ee0e | 107 | std::string Util_GetIdbyFile(const std::string filename) |
| jmarkel44 | 0:61364762ee0e | 108 | { |
| jmarkel44 | 0:61364762ee0e | 109 | char buf[MAX_FILE_SIZE]; |
| jmarkel44 | 0:61364762ee0e | 110 | std::string id = ""; |
| jmarkel44 | 0:61364762ee0e | 111 | int n = GLOBAL_mdot->readUserFile(filename.c_str(), (void*) buf, sizeof(buf)); |
| jmarkel44 | 0:61364762ee0e | 112 | if ( n != sizeof(buf) ) { |
| jmarkel44 | 0:61364762ee0e | 113 | logError("%s: error reading %s", __func__, filename.c_str()); |
| jmarkel44 | 0:61364762ee0e | 114 | return id; |
| jmarkel44 | 0:61364762ee0e | 115 | } |
| jmarkel44 | 0:61364762ee0e | 116 | cJSON *root = cJSON_Parse(buf); |
| jmarkel44 | 0:61364762ee0e | 117 | if ( !cJSON_HasObjectItem(root, "id") ) { |
| jmarkel44 | 0:61364762ee0e | 118 | logError("%s: failed to find id in file %s\n", __func__, filename.c_str()); |
| jmarkel44 | 0:61364762ee0e | 119 | } |
| jmarkel44 | 0:61364762ee0e | 120 | id = cJSON_GetObjectItem(root, "id")->valuestring; |
| jmarkel44 | 0:61364762ee0e | 121 | cJSON_Delete(root); |
| jmarkel44 | 0:61364762ee0e | 122 | return id; |
| jmarkel44 | 0:61364762ee0e | 123 | } |