Arun Raj / Mbed OS MAXREFDES101_SOURCE

Dependencies:   max32630fthr Adafruit_FeatherOLED USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SSGenericCmd.cpp Source File

SSGenericCmd.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 "DSInterface.h"
00035 #include "SSGenericCmd.h"
00036 
00037 #include "../../../../Utilities/mxm_assert.h"
00038 #include "Peripherals.h"
00039 #include "utils.h"
00040 #include "CRC8.h"
00041 
00042 static const char* const cmd_tbl[] = {
00043     "ss_write",
00044     "ss_read",
00045 };
00046 
00047 SSGenericCmd::SSGenericCmd(USBSerial *USB, SSInterface* ssInterface, DSInterface* dsInterface)
00048     :SensorComm("ss_test", false), m_USB(USB), ss_int(ssInterface), ds_int(dsInterface)
00049 {
00050 }
00051 
00052 bool SSGenericCmd::parse_command(const char* cmd)
00053 {
00054     SS_STATUS status;
00055     bool recognizedCmd = false;
00056     char response[1024];
00057     int ridx = 0;
00058 
00059     if (!ss_int) {
00060         pr_err("No SmartSensor Interface defined!");
00061         return false;
00062     }
00063     if (!ds_int) {
00064         pr_err("No DeviceStudio Interface defined!");
00065         return false;
00066     }
00067 
00068     for (int i = 0; i < NUM_CMDS; i++) {
00069         if (starts_with(cmd, cmd_tbl[i])) {
00070             cmd_state_t user_cmd = (cmd_state_t)i;
00071             recognizedCmd = true;
00072 
00073             switch (user_cmd) {
00074                 case (ss_write):
00075                 {
00076                     uint8_t cmd_bytes[256];
00077                     int num_cmd_bytes = parse_cmd_data(cmd, cmd_tbl[i], &cmd_bytes[0], sizeof(cmd_bytes), true);
00078                     if (num_cmd_bytes <= 0) {
00079                         ridx += snprintf(response + ridx, sizeof(response) - ridx - 1,
00080                                 "\r\n%s err=%d", cmd, num_cmd_bytes);
00081                         break;
00082                     }
00083 
00084                     status = ss_int->write_cmd(&cmd_bytes[0], num_cmd_bytes,
00085                                                         0, 0, 500);
00086                     ridx += snprintf(response + ridx, sizeof(response) - ridx - 1,
00087                             "\r\n%s err=0\r\n", cmd);
00088 
00089                     ridx += snprintf(response + ridx, sizeof(response) - ridx - 1,
00090                             "\r\nWrote: { ");
00091                     for (int i = 0; i < num_cmd_bytes; i++) {
00092                         ridx += snprintf(response + ridx, sizeof(response) - ridx - 1,
00093                                 "%02X ", cmd_bytes[i]);
00094                     }
00095                     ridx += snprintf(response + ridx, sizeof(response) - ridx - 1,
00096                             "}\r\n");
00097                     ridx += snprintf(response + ridx, sizeof(response) - ridx - 1,
00098                             "Status: { %02X }\r\n", (uint8_t)status);
00099 
00100                 } break;
00101 
00102                 case (ss_read):
00103                 {
00104                     uint8_t cmd_bytes[256];
00105                     uint8_t read_bytes[256];
00106 
00107 
00108                     int num_cmd_bytes = parse_cmd_data(cmd, cmd_tbl[i], &cmd_bytes[0], sizeof(cmd_bytes), true);
00109                     if (num_cmd_bytes <= 0) {
00110                         ridx += snprintf(response + ridx, sizeof(response) - ridx - 1,
00111                                 "\r\n%s err=%d", cmd, num_cmd_bytes);
00112                         break;
00113                     }
00114 
00115                     //Last space separated value is the number of bytes to read
00116                     uint8_t num_rx_bytes = cmd_bytes[num_cmd_bytes - 1];
00117                     if (num_rx_bytes > sizeof(read_bytes)) {
00118                         pr_err("Can read up to %d bytes", num_rx_bytes);
00119                         ridx += snprintf(response + ridx, sizeof(response) - ridx - 1,
00120                                 "\r\n%s err=-1", cmd);
00121                         break;
00122                     }
00123 
00124                     status = ss_int->read_cmd(&cmd_bytes[0], num_cmd_bytes - 1,
00125                                                         0, 0,
00126                                                         &read_bytes[0], num_rx_bytes,
00127                                                         500);
00128 
00129                     ridx += snprintf(response + ridx, sizeof(response) - ridx - 1,
00130                                         "\r\n%s err=0\r\n", cmd);
00131 
00132                     ridx += snprintf(response + ridx, sizeof(response) - ridx - 1,
00133                             "\r\nWrote: { ");
00134                     for (int i = 0; i < num_cmd_bytes; i++) {
00135                         ridx += snprintf(response + ridx, sizeof(response) - ridx - 1,
00136                                 "%02X ", cmd_bytes[i]);
00137                     }
00138                     ridx += snprintf(response + ridx, sizeof(response) - ridx - 1,
00139                             "}\r\n");
00140                     ridx += snprintf(response + ridx, sizeof(response) - ridx - 1,
00141                             "Read: { ");
00142                     for (int i = 0; i < num_rx_bytes; i++) {
00143                         ridx += snprintf(response + ridx, sizeof(response) - ridx - 1,
00144                                 "%02X ", read_bytes[i]);
00145                     }
00146                     ridx += snprintf(response + ridx, sizeof(response) - ridx - 1,
00147                             "}\r\n");
00148                     ridx += snprintf(response + ridx, sizeof(response) - ridx - 1,
00149                             "Status: { %02X }\r\n", (uint8_t)status);
00150 
00151                 } break;
00152 
00153                 default:
00154                 {
00155                     recognizedCmd = false;
00156                     mxm_assert_msg(false, "Invalid switch case!");
00157                 } break;
00158 
00159             }
00160             break;
00161         }
00162     }
00163 
00164     if (recognizedCmd) {
00165         m_USB->printf(response);
00166     }
00167 
00168     return recognizedCmd;
00169 }