Maxim Integrated / Mbed OS MAXREFDES220#

Dependencies:   USBDevice max32630fthr

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DSInterface.cpp Source File

DSInterface.cpp

00001 /***************************************************************************
00002 * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
00003 *
00004 * Permission is hereby granted, free of charge, to any person obtaining a
00005 * copy of this software and associated documentation files (the "Software"),
00006 * to deal in the Software without restriction, including without limitation
00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 * and/or sell copies of the Software, and to permit persons to whom the
00009 * Software is furnished to do so, subject to the following conditions:
00010 *
00011 * The above copyright notice and this permission notice shall be included
00012 * in all copies or substantial portions of the Software.
00013 *
00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020 * OTHER DEALINGS IN THE SOFTWARE.
00021 *
00022 * Except as contained in this notice, the name of Maxim Integrated
00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024 * Products, Inc. Branding Policy.
00025 *
00026 * The mere transfer of this software does not imply any licenses
00027 * of trade secrets, proprietary technology, copyrights, patents,
00028 * trademarks, maskwork rights, or any other form of intellectual
00029 * property whatsoever. Maxim Integrated Products, Inc. retains all
00030 * ownership rights.
00031 ****************************************************************************
00032 */
00033 
00034 #include <ctype.h>
00035 #include <string.h>
00036 #include "DSInterface.h"
00037 #include "../version.h"
00038 #include "Peripherals.h"
00039 #include "assert.h"
00040 #include "utils.h"
00041 
00042 #ifdef ENABLE_BLE
00043 #include "BLE_ICARUS.h"
00044 #endif
00045 
00046 static const char *glbl_cmds[] = {
00047     "stop",
00048     "get_device_info",
00049     "silent_mode 0",
00050     "silent_mode 1",
00051     "pause 0",
00052     "pause 1",
00053     "enable console",
00054     "disable console",
00055 };
00056 
00057 typedef enum {
00058     stop=0,
00059     get_device_info,
00060     silent0_mode,
00061     silent1_mode,
00062     pause0_mode,
00063     pause1_mode,
00064     enable_console,
00065     disable_console,
00066     NUM_CMDS,
00067 } glbl_cmd_state;
00068 
00069 
00070 DSInterface::DSInterface(USBSerial* USB)
00071 {
00072     cmd_idx = 0;
00073     silent_mode = false;
00074     pause_mode = false;
00075 
00076     memset(sensor_list, 0, DS_MAX_NUM_SENSORCOMMS * sizeof(SensorComm*));
00077     num_sensors = 0;
00078 
00079     m_USB = USB;
00080 
00081     data_report_mode = 0;
00082     console_interface_exists = true;
00083 }
00084 
00085 DSInterface::~DSInterface()
00086 {
00087 }
00088 
00089 void DSInterface::add_sensor_comm(SensorComm *s)
00090 {
00091     assert_msg(num_sensors < DS_MAX_NUM_SENSORCOMMS, "Too many sensors added to DSInterface. Increase DS_MAX_NUM_SENSORCOMMS.");
00092     sensor_list[num_sensors++] = s;
00093 }
00094 
00095 void DSInterface::enable_console_interface()
00096 {
00097     console_interface_exists = true;
00098 }
00099 
00100 void DSInterface::build_command(char ch)
00101 {
00102     if (!this->silent_mode) /* BUG: POTENTIAL BUG, what uart port to echo, not only console */
00103         m_USB->printf("%c", ch);
00104 
00105     if (ch == 0x00) {
00106         pr_err("Ignored char 0x00");
00107         return;
00108     }
00109 
00110     if ((ch == '\n') || (ch == '\r')) {
00111         if (cmd_idx < (int)CONSOLE_STR_BUF_SZ)
00112         cmd_str[cmd_idx++] = '\0';
00113         m_USB->printf("\r\n");
00114         parse_command();
00115 
00116         //Clear cmd_str
00117         while (cmd_idx > 0) /* BUG: POTENTIAL BUG for multiple port access */
00118             cmd_str[--cmd_idx] = '\0';
00119 
00120     } else if ((ch == 0x08 || ch == 0x7F) && cmd_idx > 0) {
00121         //Backspace character
00122         if (cmd_idx > 0)
00123             cmd_str[--cmd_idx] = '\0';
00124     } else {
00125         /* BUG: POTENTIAL BUG for multiple port access */
00126         if (cmd_idx < (int)CONSOLE_STR_BUF_SZ)
00127             cmd_str[cmd_idx++] = ch;
00128     }
00129 
00130 }
00131 
00132 void DSInterface::parse_command()
00133 {
00134     int i;
00135     glbl_cmd_state cmd;
00136     char charbuf[512];
00137     int data_len = 0;
00138     int ret;
00139     bool parsed_cmd = true;
00140 
00141     //If top level command, then handle it
00142     for (i = 0; i < NUM_CMDS; i++) {
00143         if (starts_with(&cmd_str[0], glbl_cmds[i])) {
00144             cmd = (glbl_cmd_state)i;
00145 
00146             switch (cmd) {
00147                 case (stop): {
00148                     for (int i = 0; i < num_sensors; i++) {
00149                         sensor_list[i]->stop();
00150                     }
00151                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00152                             "\r\n%s err=0\r\n", cmd_str);
00153                 } break;
00154                 case (get_device_info): {
00155                     data_len = snprintf(charbuf, sizeof(charbuf),
00156                         "\r\n%s platform=%s firmware_ver=%s sensors=", cmd_str, platform_name, firmware_version);
00157 
00158                     //Add list of sensors
00159                     for (int i = 0; i < num_sensors; i++) {
00160                         if (sensor_list[i]->is_visible()) {
00161                             data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len,
00162                                             "%s", sensor_list[i]->get_type());
00163                             if (i < (num_sensors - 1))
00164                                 data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len, ",");
00165                         }
00166                     }
00167 
00168 
00169                     for (int i = 0; i < num_sensors; i++) {
00170                         SensorComm *s = sensor_list[i];
00171                         if (!s->is_visible())
00172                             continue;
00173 
00174                         //Add part name
00175                         data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len,
00176                                             " part_name_%s=%s", s->get_type(), s->get_part_name());
00177 
00178                         uint8_t part_id, part_rev;
00179                         ret = s->get_part_info(&part_id, &part_rev);
00180 
00181                         if (ret == 0) {
00182                             //Add part id
00183                             data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len,
00184                                             " part_id_%s=%d", s->get_type(), part_id);
00185 
00186                             //Add rev id
00187                             data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len,
00188                                             " part_rev_%s=%d", s->get_type(), part_rev);
00189                         }
00190                     }
00191 
00192 
00193                     data_len += snprintf(charbuf + data_len, sizeof(charbuf) - data_len, " err=0\r\n");
00194 
00195                 } break;
00196                 case (silent0_mode): {
00197                     silent_mode = false;
00198                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00199                             "\r\n%s err=0\r\n", cmd_str);
00200                 } break;
00201                 case (silent1_mode): {
00202                     silent_mode = true;
00203                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00204                             "\r\n%s err=0\r\n", cmd_str);
00205                 } break;
00206                 case (pause0_mode): {
00207                     pause_mode = false;
00208                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00209                             "\r\n%s err=0\r\n", cmd_str);
00210                 } break;
00211                 case (pause1_mode): {
00212                     pause_mode = true;
00213                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00214                             "\r\n%s err=0\r\n", cmd_str);
00215                 } break;
00216                 case (enable_console): {
00217                     console_interface_exists = true;
00218                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00219                             "\r\n%s err=0\r\n", cmd_str);
00220                 } break;
00221                 case (disable_console): {
00222                     console_interface_exists = false;
00223                     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00224                             "\r\n%s err=0\r\n", cmd_str);
00225                 } break;
00226                 default:
00227                     parsed_cmd = false;
00228                     break;
00229             }
00230 
00231             if (parsed_cmd) {
00232                 m_USB->printf(charbuf);
00233 #if ENABLE_BLE
00234                 if (BLE_Icarus_Interface_Exists()) {
00235                     BLE_Icarus_AddtoQueue((uint8_t *)charbuf, (int32_t)sizeof(charbuf), data_len);
00236                     }
00237 #endif
00238             }
00239 
00240             return;
00241         }
00242     }
00243 
00244     //Loop through each sensor in sensorList
00245     //If sensor.get_type() is contained in cmd_str, pass cmd_str to that sensor's parser
00246     for (int i = 0; i < num_sensors; i++) {
00247         if (strstr(&cmd_str[0], sensor_list[i]->get_type())) {
00248             if (sensor_list[i]->parse_command(cmd_str))
00249                 return;
00250             break;
00251         }
00252     }
00253 
00254     //If we still haven't found a way to parse the command,
00255     //send it to every sensor until one handles it
00256     for (int i = 0; i < num_sensors; i++) {
00257         if (sensor_list[i]->parse_command(cmd_str))
00258             return;
00259     }
00260     
00261 
00262     //No one could handle the command, print cmd not recognized string
00263     data_len += snprintf(charbuf, sizeof(charbuf) - 1,
00264             "\r\n%s err=-255\r\n", cmd_str);
00265     m_USB->printf(charbuf);
00266 #if ENABLE_BLE
00267     if (BLE_Icarus_Interface_Exists()) {
00268         BLE_Icarus_AddtoQueue((uint8_t *)charbuf, (int32_t)sizeof(charbuf), data_len);
00269     }
00270 #endif
00271 }
00272 
00273 void DSInterface::data_report_execute()
00274 {
00275     char buffer[256];
00276     int data_len = 0;
00277 
00278     buffer[0] = '\0';
00279 
00280     for (int i = 0; i < num_sensors; i++) {
00281         //if ((*it)->is_enabled()) {
00282         data_len = sensor_list[i]->data_report_execute(buffer, sizeof(buffer));
00283         if (!this->pause_mode && (data_len > 0)) {
00284 #ifdef ENABLE_BLE
00285             if (BLE_Icarus_Interface_Exists()) {
00286                 BLE_Icarus_AddtoQueue((uint8_t *)buffer, (int32_t)sizeof(buffer), data_len);
00287             }
00288 #endif
00289             if (console_interface_exists){
00290 #ifdef ASCII_COMM
00291                 m_USB->printf(buffer);
00292 #else
00293                 m_USB->writeBlock((uint8_t*)buffer, data_len);
00294 #endif
00295             }
00296             data_len = 0;
00297         }
00298         //We only support streaming from one device at a time, so break here
00299         //  break;
00300         //}
00301     }
00302 }
00303 
00304 void DSInterface::set_fw_version(const char* version)
00305 {
00306     if (version && *version)
00307         firmware_version = version;
00308 }
00309 
00310 void DSInterface::set_fw_platform(const char* platform)
00311 {
00312     if (platform && *platform)
00313         platform_name = platform;
00314 }