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/ConfigurationHandler/Algorithms/CompositeAlgorithm.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 | * |
| jmarkel44 | 0:61364762ee0e | 3 | * File: CompositeAlgorithm.cpp |
| jmarkel44 | 0:61364762ee0e | 4 | * Desciption: source for the ICE Composite Algorithm |
| jmarkel44 | 0:61364762ee0e | 5 | * |
| jmarkel44 | 0:61364762ee0e | 6 | *****************************************************************************/ |
| jmarkel44 | 0:61364762ee0e | 7 | #include "global.h" |
| jmarkel44 | 0:61364762ee0e | 8 | #include "cJSON.h" |
| jmarkel44 | 0:61364762ee0e | 9 | #include "ICELog.h" |
| jmarkel44 | 0:61364762ee0e | 10 | #include "CompositeAlgorithm.h" |
| jmarkel44 | 0:61364762ee0e | 11 | //#include "../add-ons/MTSLog/MTSLog.h" |
| jmarkel44 | 0:61364762ee0e | 12 | |
| jmarkel44 | 0:61364762ee0e | 13 | // |
| jmarkel44 | 0:61364762ee0e | 14 | // method: load |
| jmarkel44 | 0:61364762ee0e | 15 | // description: load the pertinents from the control file |
| jmarkel44 | 0:61364762ee0e | 16 | // |
| jmarkel44 | 0:61364762ee0e | 17 | // @param[in] _controlFile : name of the control file |
| jmarkel44 | 0:61364762ee0e | 18 | // @param[out] none |
| jmarkel44 | 0:61364762ee0e | 19 | // @return none |
| jmarkel44 | 0:61364762ee0e | 20 | // |
| jmarkel44 | 0:61364762ee0e | 21 | bool CompositeAlgorithm::load(const std::string _controlFile) |
| jmarkel44 | 0:61364762ee0e | 22 | { |
| jmarkel44 | 0:61364762ee0e | 23 | controlFile = _controlFile; |
| jmarkel44 | 0:61364762ee0e | 24 | |
| jmarkel44 | 0:61364762ee0e | 25 | // read the data into a buffer |
| jmarkel44 | 0:61364762ee0e | 26 | char dataBuf[MAX_FILE_SIZE]; |
| jmarkel44 | 0:61364762ee0e | 27 | bool rc = GLOBAL_mdot->readUserFile(controlFile.c_str(), (void *)dataBuf, sizeof(dataBuf)); |
| jmarkel44 | 0:61364762ee0e | 28 | if ( rc != true ) { |
| jmarkel44 | 0:61364762ee0e | 29 | logError("%s: failed to read %s", __func__, controlFile.c_str()); |
| jmarkel44 | 0:61364762ee0e | 30 | // caller should destroy the object |
| jmarkel44 | 0:61364762ee0e | 31 | return false; |
| jmarkel44 | 0:61364762ee0e | 32 | } |
| jmarkel44 | 0:61364762ee0e | 33 | |
| jmarkel44 | 0:61364762ee0e | 34 | // parse the JSON data |
| jmarkel44 | 0:61364762ee0e | 35 | cJSON * root = cJSON_Parse(dataBuf); |
| jmarkel44 | 0:61364762ee0e | 36 | |
| jmarkel44 | 0:61364762ee0e | 37 | if ( !cJSON_HasObjectItem(root, "id") || |
| jmarkel44 | 0:61364762ee0e | 38 | !cJSON_HasObjectItem(root, "opr") || |
| jmarkel44 | 0:61364762ee0e | 39 | !cJSON_HasObjectItem(root, "op") || |
| jmarkel44 | 0:61364762ee0e | 40 | !cJSON_HasObjectItem(root, "true") || |
| jmarkel44 | 0:61364762ee0e | 41 | !cJSON_HasObjectItem(root, "false") ) { |
| jmarkel44 | 0:61364762ee0e | 42 | logError("%s: control file is missing expected tags", __func__); |
| jmarkel44 | 0:61364762ee0e | 43 | cJSON_Delete(root); |
| jmarkel44 | 0:61364762ee0e | 44 | return false; |
| jmarkel44 | 0:61364762ee0e | 45 | } |
| jmarkel44 | 0:61364762ee0e | 46 | |
| jmarkel44 | 0:61364762ee0e | 47 | id = cJSON_GetObjectItem(root, "id")->valuestring; |
| jmarkel44 | 0:61364762ee0e | 48 | opr = cJSON_GetObjectItem(root, "opr")->valuestring; |
| jmarkel44 | 0:61364762ee0e | 49 | op = cJSON_GetObjectItem(root, "op")->valuestring; |
| jmarkel44 | 0:61364762ee0e | 50 | resultTrue = cJSON_GetObjectItem(root, "true")->valuestring; |
| jmarkel44 | 0:61364762ee0e | 51 | resultFalse = cJSON_GetObjectItem(root, "false")->valuestring; |
| jmarkel44 | 0:61364762ee0e | 52 | |
| jmarkel44 | 0:61364762ee0e | 53 | cJSON_Delete(root); |
| jmarkel44 | 0:61364762ee0e | 54 | return true; |
| jmarkel44 | 0:61364762ee0e | 55 | } |
| jmarkel44 | 0:61364762ee0e | 56 | |
| jmarkel44 | 0:61364762ee0e | 57 | // |
| jmarkel44 | 0:61364762ee0e | 58 | // method: validateControlData |
| jmarkel44 | 0:61364762ee0e | 59 | // description: validate the JSON formatted string |
| jmarkel44 | 0:61364762ee0e | 60 | // |
| jmarkel44 | 0:61364762ee0e | 61 | // @param[in] buf -> JSON formatted string |
| jmarkel44 | 0:61364762ee0e | 62 | // @param[out] none |
| jmarkel44 | 0:61364762ee0e | 63 | // @return true if valid; false otherwise |
| jmarkel44 | 0:61364762ee0e | 64 | // |
| jmarkel44 | 0:61364762ee0e | 65 | bool CompositeAlgorithm::validateControlData(const char *buf) |
| jmarkel44 | 0:61364762ee0e | 66 | { |
| jmarkel44 | 0:61364762ee0e | 67 | bool rc = true; |
| jmarkel44 | 0:61364762ee0e | 68 | cJSON * root = cJSON_Parse(buf); |
| jmarkel44 | 0:61364762ee0e | 69 | |
| jmarkel44 | 0:61364762ee0e | 70 | if ( !cJSON_HasObjectItem(root, "id") || |
| jmarkel44 | 0:61364762ee0e | 71 | !cJSON_HasObjectItem(root, "opr") || |
| jmarkel44 | 0:61364762ee0e | 72 | !cJSON_HasObjectItem(root, "op") || |
| jmarkel44 | 0:61364762ee0e | 73 | !cJSON_HasObjectItem(root, "true") || |
| jmarkel44 | 0:61364762ee0e | 74 | !cJSON_HasObjectItem(root, "false") ) { |
| jmarkel44 | 0:61364762ee0e | 75 | logError("%s: control file is missing expected tags", __func__); |
| jmarkel44 | 0:61364762ee0e | 76 | rc = false; |
| jmarkel44 | 0:61364762ee0e | 77 | } |
| jmarkel44 | 0:61364762ee0e | 78 | cJSON_Delete(root); |
| jmarkel44 | 0:61364762ee0e | 79 | return rc; |
| jmarkel44 | 0:61364762ee0e | 80 | } |
| jmarkel44 | 0:61364762ee0e | 81 | |
| jmarkel44 | 0:61364762ee0e | 82 | // |
| jmarkel44 | 0:61364762ee0e | 83 | // method: copyControlData |
| jmarkel44 | 0:61364762ee0e | 84 | // description: copy JSON data to control data |
| jmarkel44 | 0:61364762ee0e | 85 | // |
| jmarkel44 | 0:61364762ee0e | 86 | // @param[in] buf -> JSON formatted string |
| jmarkel44 | 0:61364762ee0e | 87 | // @param[out] none |
| jmarkel44 | 0:61364762ee0e | 88 | // @return |
| jmarkel44 | 0:61364762ee0e | 89 | void CompositeAlgorithm::copyControlData(const char *buf) |
| jmarkel44 | 0:61364762ee0e | 90 | { |
| jmarkel44 | 0:61364762ee0e | 91 | cJSON * root = cJSON_Parse(buf); |
| jmarkel44 | 0:61364762ee0e | 92 | |
| jmarkel44 | 0:61364762ee0e | 93 | id = cJSON_GetObjectItem(root, "id")->valuestring; |
| jmarkel44 | 0:61364762ee0e | 94 | opr = cJSON_GetObjectItem(root, "opr")->valuestring; |
| jmarkel44 | 0:61364762ee0e | 95 | op = cJSON_GetObjectItem(root, "op")->valuestring; |
| jmarkel44 | 0:61364762ee0e | 96 | resultTrue = cJSON_GetObjectItem(root, "true")->valuestring; |
| jmarkel44 | 0:61364762ee0e | 97 | resultFalse = cJSON_GetObjectItem(root, "false")->valuestring; |
| jmarkel44 | 0:61364762ee0e | 98 | |
| jmarkel44 | 0:61364762ee0e | 99 | cJSON_Delete(root); |
| jmarkel44 | 0:61364762ee0e | 100 | } |
| jmarkel44 | 0:61364762ee0e | 101 | |
| jmarkel44 | 0:61364762ee0e | 102 | // |
| jmarkel44 | 0:61364762ee0e | 103 | // method: display |
| jmarkel44 | 0:61364762ee0e | 104 | // description: display the pertinents |
| jmarkel44 | 0:61364762ee0e | 105 | // |
| jmarkel44 | 0:61364762ee0e | 106 | // @param[in] none |
| jmarkel44 | 0:61364762ee0e | 107 | // @param[out] none |
| jmarkel44 | 0:61364762ee0e | 108 | // @return none |
| jmarkel44 | 0:61364762ee0e | 109 | // |
| jmarkel44 | 0:61364762ee0e | 110 | void CompositeAlgorithm::display(void) |
| jmarkel44 | 0:61364762ee0e | 111 | { |
| jmarkel44 | 0:61364762ee0e | 112 | printf("\r id : %s\n", id.c_str()); |
| jmarkel44 | 0:61364762ee0e | 113 | printf("\r opr : %s\n", opr.c_str()); |
| jmarkel44 | 0:61364762ee0e | 114 | printf("\r op : %s\n", op.c_str()); |
| jmarkel44 | 0:61364762ee0e | 115 | printf("\r true : %s\n", resultTrue.c_str()); |
| jmarkel44 | 0:61364762ee0e | 116 | printf("\r false : %s\n", resultFalse.c_str()); |
| jmarkel44 | 0:61364762ee0e | 117 | printf("\r\n"); |
| jmarkel44 | 0:61364762ee0e | 118 | } |