Subversion Repositories svn.mios32

Rev

Rev 1914 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

// $Id: terminal.cpp 2425 2016-11-03 00:44:22Z tk $
/*
 * The command/configuration Terminal
 *
 * ==========================================================================
 *
 *  Copyright (C) 2010 Thorsten Klose (tk@midibox.org)
 *  Licensed for personal non-commercial use only.
 *  All other rights reserved.
 *
 * ==========================================================================
 */


/////////////////////////////////////////////////////////////////////////////
// Include files
/////////////////////////////////////////////////////////////////////////////

#include <mios32.h>
#include <string.h>
#include <file.h>

#include <midi_port.h>
#include <midi_router.h>
#include <midimon.h>
#include <aout.h>
#include <app_lcd.h>

#include <app.h>
#include <MbCvEnvironment.h>

#include "tasks.h"
#include "terminal.h"
#include "mbcv_patch.h"
#include "uip_terminal.h"
#include "mbcv_hwcfg.h"
#include "mbcv_file.h"
#include "mbcv_file_p.h"
#include "mbcv_file_b.h"
#include "mbcv_file_hw.h"

extern "C" {
#include <ff.h>
}


/////////////////////////////////////////////////////////////////////////////
// Local defines
/////////////////////////////////////////////////////////////////////////////

#define STRING_MAX 100 // recommended size for file transfers via FILE_BrowserHandler()


/////////////////////////////////////////////////////////////////////////////
// Local variables
/////////////////////////////////////////////////////////////////////////////

static char line_buffer[STRING_MAX];
static u16 line_ix;
static char autoload_cv2_file[9];


/////////////////////////////////////////////////////////////////////////////
// Local prototypes
/////////////////////////////////////////////////////////////////////////////

static s32 TERMINAL_ParseFilebrowser(mios32_midi_port_t port, char byte);

static s32 TERMINAL_BrowserUploadCallback(char *filename);


/////////////////////////////////////////////////////////////////////////////
// Initialisation
/////////////////////////////////////////////////////////////////////////////
s32 TERMINAL_Init(u32 mode)
{
  // install the callback function which is called on incoming characters from MIOS Terminal
  MIOS32_MIDI_DebugCommandCallback_Init(TERMINAL_Parse);

  // install the callback function which is called on incoming characters from MIOS Filebrowser
  MIOS32_MIDI_FilebrowserCommandCallback_Init(TERMINAL_ParseFilebrowser);

  // clear line buffer
  line_buffer[0] = 0;
  line_ix = 0;

  // invalidate autoload file
  autoload_cv2_file[0] = 0;

  return 0; // no error
}


/////////////////////////////////////////////////////////////////////////////
// help function which parses a decimal or hex value
// returns >= 0 if value is valid
// returns -1 if value is invalid
/////////////////////////////////////////////////////////////////////////////
static s32 get_dec(char *word)
{
  if( word == NULL )
    return -1;

  char *next;
  long l = strtol(word, &next, 0);

  if( word == next )
    return -1;

  return l; // value is valid
}


/////////////////////////////////////////////////////////////////////////////
// help function which parses for on or off
// returns 0 if 'off', 1 if 'on', -1 if invalid
/////////////////////////////////////////////////////////////////////////////
//static s32 get_on_off(char *word)
//{
//  if( strcmp(word, "on") == 0 )
//    return 1;
//
//  if( strcmp(word, "off") == 0 )
//    return 0;
//
//  return -1;
//}


/////////////////////////////////////////////////////////////////////////////
// Parser
/////////////////////////////////////////////////////////////////////////////
s32 TERMINAL_Parse(mios32_midi_port_t port, char byte)
{
  // temporary change debug port (will be restored at the end of this function)
  mios32_midi_port_t prev_debug_port = MIOS32_MIDI_DebugPortGet();
  MIOS32_MIDI_DebugPortSet(port);

  if( byte == '\r' ) {
    // ignore
  } else if( byte == '\n' ) {
    MUTEX_MIDIOUT_TAKE;
    TERMINAL_ParseLine(line_buffer, MIOS32_MIDI_SendDebugMessage);
    MUTEX_MIDIOUT_GIVE;
    line_ix = 0;
    line_buffer[line_ix] = 0;
  } else if( line_ix < (STRING_MAX-1) ) {
    line_buffer[line_ix++] = byte;
    line_buffer[line_ix] = 0;
  }

  // restore debug port
  MIOS32_MIDI_DebugPortSet(prev_debug_port);

  return 0; // no error
}


/////////////////////////////////////////////////////////////////////////////
// Parser for Filebrowser
/////////////////////////////////////////////////////////////////////////////
s32 TERMINAL_ParseFilebrowser(mios32_midi_port_t port, char byte)
{
  if( byte == '\r' ) {
    // ignore
  } else if( byte == '\n' ) {
    // for the auto-load function
    FILE_BrowserUploadCallback_Init(TERMINAL_BrowserUploadCallback);

    MUTEX_MIDIOUT_TAKE;
    MUTEX_SDCARD_TAKE;
    FILE_BrowserHandler(port, line_buffer);
    MUTEX_SDCARD_GIVE;
    MUTEX_MIDIOUT_GIVE;
    line_ix = 0;
    line_buffer[line_ix] = 0;
  } else if( line_ix < (STRING_MAX-1) ) {
    line_buffer[line_ix++] = byte;
    line_buffer[line_ix] = 0;
  }

  return 0; // no error
}


/////////////////////////////////////////////////////////////////////////////
//! For the auto-load function
/////////////////////////////////////////////////////////////////////////////
static s32 TERMINAL_BrowserUploadCallback(char *filename)
{
  if( filename ) {
    // invalidate autoload file
    autoload_cv2_file[0] = 0;

    // check for .CV2
    if( filename[0] == '/' )
      ++filename;
    int len = strlen(filename);

    if( len < 5 || len > 12 ||
    (strcasecmp((char *)&filename[len-4], ".cv2") != 0) )
      return 0; // no .CV2 file

    int i;
    for(i=0; i<len; ++i) {
      if( filename[i] == '/' )
    return 0; // not in root dir
    }

    strncpy(autoload_cv2_file, filename, len-4);
    autoload_cv2_file[len-4] = 0;
  } else {
    if( autoload_cv2_file[0] ) {
      DEBUG_MSG("AUTOLOAD '%s'\n", autoload_cv2_file);

      if( strcasecmp(autoload_cv2_file, "MBCV_HW") == 0 ) {
    MBCV_HWCFG_Init(0);
    MBCV_FILE_HW_Init(0);
    MBCV_FILE_HW_Load();
      } else {
    s32 status = MBCV_PATCH_LoadGlobal(autoload_cv2_file);
    if( status >= 0 ) {
      DEBUG_MSG("Patch '%s' loaded from SD Card!", autoload_cv2_file);
    } else {
      DEBUG_MSG("ERROR: failed to load patch '%s' on SD Card (status %d)!", autoload_cv2_file, status);
    }
      }
    }
  }

  return 0; // no error
}

/////////////////////////////////////////////////////////////////////////////
// Parser for a complete line - also used by shell.c for telnet
/////////////////////////////////////////////////////////////////////////////
s32 TERMINAL_ParseLine(char *input, void *_output_function)
{
  void (*out)(char *format, ...) = _output_function;
  char *separators = " \t";
  char *brkt;
  char *parameter;

  if( UIP_TERMINAL_ParseLine(input, _output_function) > 0 )
    return 0; // command parsed by UIP Terminal

  if( MIDIMON_TerminalParseLine(input, _output_function) > 0 )
    return 0; // command parsed

  if( MIDI_ROUTER_TerminalParseLine(input, _output_function) > 0 )
    return 0; // command parsed

  if( AOUT_TerminalParseLine(input, _output_function) > 0 )
    return 0; // command parsed

#ifdef MIOS32_LCD_universal
  if( APP_LCD_TerminalParseLine(input, _output_function) >= 1 )
    return 0; // command parsed
#endif

  if( (parameter = strtok_r(input, separators, &brkt)) ) {
    if( strcmp(parameter, "help") == 0 ) {
      out("Welcome to " MIOS32_LCD_BOOT_MSG_LINE1 "!");
      out("Following commands are available:");
      out("  system:                           print system info");
      out("  memory:                           print memory allocation info\n");
      out("  sdcard:                           print SD Card info\n");
      out("  sdcard_format:                    formats the SD Card (you will be asked for confirmation)\n");
      UIP_TERMINAL_Help(_output_function);
      MIDIMON_TerminalHelp(_output_function);
      MIDI_ROUTER_TerminalHelp(_output_function);
      out("  set dout <pin> <0|1>:             directly sets DOUT (all or 0..%d) to given level (1 or 0)", MIOS32_SRIO_NUM_SR*8 - 1);
      out("  set update_rate <1..%d>:          sets update rate of sound engine (factor*500 Hz), current: %d\n", APP_CV_UPDATE_RATE_FACTOR_MAX, APP_CvUpdateRateFactorGet());
      AOUT_TerminalHelp(_output_function);
#ifdef MIOS32_LCD_universal
      APP_LCD_TerminalHelp(_output_function);
#endif
      out("  save <name>:                      stores current config on SD Card");
      out("  load <name>:                      restores config from SD Card");
      out("  show:                             shows the current configuration file");
      out("  nrpn:                             shows the current NRPN parameters");
      out("  msd <on|off>:                     enables Mass Storage Device driver");
      out("  reset:                            resets the MIDIbox (!)\n");
      out("  help:                             this page");
      out("  exit:                             (telnet only) exits the terminal");
    } else if( strcmp(parameter, "system") == 0 ) {
      TERMINAL_PrintSystem(_output_function);
    } else if( strcmp(parameter, "memory") == 0 ) {
      TERMINAL_PrintMemoryInfo(out);
    } else if( strcmp(parameter, "sdcard") == 0 ) {
      TERMINAL_PrintSdCardInfo(out);
    } else if( strcmp(parameter, "sdcard_format") == 0 ) {
      if( !brkt || strcasecmp(brkt, "yes, I'm sure") != 0 ) {
    out("ATTENTION: this command will format your SD Card!!!");
    out("           ALL DATA WILL BE DELETED FOREVER!!!");
    out("           Check the current content with the 'sdcard' command");
    out("           Create a backup on your computer if necessary!");
    out("To start formatting, please enter: sdcard_format yes, I'm sure");
    if( brkt ) {
      out("('%s' wasn't the right \"password\")", brkt);
    }
      } else {
    MUTEX_SDCARD_TAKE;
    out("Formatting SD Card...");
    FRESULT res;
    if( (res=f_mkfs(0,0,0)) != FR_OK ) {
      out("Formatting failed with error code: %d!", res);
    } else {
      out("...with success!");
      MBCV_FILE_UnloadAllFiles();
      MBCV_FILE_CreateDefaultFiles();
    }
    MUTEX_SDCARD_GIVE;
      }
    } else if( strcmp(parameter, "msd") == 0 ) {
      char *arg = NULL;
      if( (arg = strtok_r(NULL, separators, &brkt)) ) {
    if( strcmp(arg, "on") == 0 ) {
      if( TASK_MSD_EnableGet() ) {
        out("Mass Storage Device Mode already activated!\n");
      } else {
        out("Mass Storage Device Mode activated - USB MIDI will be disabled!!!\n");
        // wait a second to ensure that this message is print in MIOS Terminal
        int d;
        for(d=0; d<1000; ++d)
          MIOS32_DELAY_Wait_uS(1000);
        // activate MSD mode
        TASK_MSD_EnableSet(1);
      }
    } else if( strcmp(arg, "off") == 0 ) {
      if( !TASK_MSD_EnableGet() ) {
        out("Mass Storage Device Mode already deactivated!\n");
      } else {
        out("Mass Storage Device Mode deactivated - USB MIDI will be available again.n");
        TASK_MSD_EnableSet(0);
      }
    } else
      arg = NULL;
      }
      if( arg == NULL ) {
    out("Please enter 'msd on' or 'msd off'\n");
      }      
    } else if( strcmp(parameter, "save") == 0 ) {
      if( !(parameter = strtok_r(NULL, separators, &brkt)) ) {
    out("ERROR: please specify filename for patch (up to 8 characters)!");
      } else {
    if( strlen(parameter) > 8 ) {
      out("ERROR: 8 characters maximum!");
    } else {
      s32 status = MBCV_PATCH_StoreGlobal(parameter);
      if( status >= 0 ) {
        out("Patch '%s' stored on SD Card!", parameter);
      } else {
        out("ERROR: failed to store patch '%s' on SD Card (status %d)!", parameter, status);
      }
    }
      }
    } else if( strcmp(parameter, "load") == 0 ) {
      if( !(parameter = strtok_r(NULL, separators, &brkt)) ) {
    out("ERROR: please specify filename for patch (up to 8 characters)!");
      } else {
    if( strlen(parameter) > 8 ) {
      out("ERROR: 8 characters maximum!");
    } else {
      s32 status = MBCV_PATCH_LoadGlobal(parameter);
      if( status >= 0 ) {
        out("Patch '%s' loaded from SD Card!", parameter);
      } else {
        out("ERROR: failed to load patch '%s' on SD Card (status %d)!", parameter, status);
      }
    }
      }
    } else if( strcmp(parameter, "show") == 0 ) {
      MBCV_FILE_P_Debug();
    } else if( strcmp(parameter, "nrpn") == 0 || strcmp(parameter, "nrpns") == 0 ) {
      TERMINAL_ShowNrpns(out);
    } else if( strcmp(parameter, "reset") == 0 ) {
      MIOS32_SYS_Reset();
    } else if( strcmp(parameter, "set") == 0 ) {
      if( (parameter = strtok_r(NULL, separators, &brkt)) ) {
    if( strcmp(parameter, "dout") == 0 ) {
      s32 pin = -1;
      if( (parameter = strtok_r(NULL, separators, &brkt)) ) {
        if( strcmp(parameter, "all") == 0 ) {
          pin = -42;
        } else {
          pin = get_dec(parameter);
        }
      }

      if( (pin < 0 && pin != -42) || pin >= (MIOS32_SRIO_NUM_SR*8) ) {
        out("Pin number should be between 0..%d", MIOS32_SRIO_NUM_SR*8 - 1);
      } else {
        s32 value = -1;
        if( (parameter = strtok_r(NULL, separators, &brkt)) )
          value = get_dec(parameter);

        if( value < 0 || value > 1 ) {
          out("Expecting value 1 or 0 for DOUT pin %d", pin);
        } else {
          if( pin == -42 ) {
        for(pin=0; pin<(MIOS32_SRIO_NUM_SR*8); ++pin)
          MIOS32_DOUT_PinSet(pin, value);
        out("All DOUT pins set to %d", value);
          } else {
        MIOS32_DOUT_PinSet(pin, value);
        out("DOUT Pin %d (SR#%d.D%d) set to %d", pin, (pin/8)+1, 7-(pin%8), value);
          }
        }
      }
    } else if( strcmp(parameter, "update_rate") == 0 ) {
      s32 factor = -1;
      if( (parameter = strtok_r(NULL, separators, &brkt)) ) {
        factor = get_dec(parameter);
      }

      if( (factor < 1 || factor > APP_CV_UPDATE_RATE_FACTOR_MAX) ) {
        out("Update Rate should be between 1..%d!", APP_CV_UPDATE_RATE_FACTOR_MAX);
      } else {
        APP_CvUpdateRateFactorSet(factor);
        out("Update Rate set to %d Hz (factor %d)", 500*APP_CvUpdateRateFactorGet(), APP_CvUpdateRateFactorGet());
      }
    } else {
      out("Unknown set parameter: '%s'!", parameter);
    }
      } else {
    out("Missing parameter after 'set'!");
      }
    } else {
      out("Unknown command - type 'help' to list available commands!");
    }
  }

  return 0; // no error
}


/////////////////////////////////////////////////////////////////////////////
// System Informations
/////////////////////////////////////////////////////////////////////////////
s32 TERMINAL_PrintSystem(void *_output_function)
{
  void (*out)(char *format, ...) = _output_function;

  out("Application: " MIOS32_LCD_BOOT_MSG_LINE1);

  MIDIMON_TerminalPrintConfig(out);

  AOUT_TerminalPrintConfig(out);

#if !defined(MIOS32_FAMILY_EMULATION) && configGENERATE_RUN_TIME_STATS
  // send Run Time Stats to MIOS terminal
  out("FreeRTOS Task RunTime Stats:\n");
  FREERTOS_UTILS_RunTimeStats();
#endif

  return 0; // no error
}


/////////////////////////////////////////////////////////////////////////////
// Memory allocation Informations
/////////////////////////////////////////////////////////////////////////////
s32 TERMINAL_PrintMemoryInfo(void *_output_function)
{
  void (*out)(char *format, ...) = _output_function;

  out("Not supported yet");

  return 0; // no error
}

/////////////////////////////////////////////////////////////////////////////
// SDCard Informations
/////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
// These time and date functions and other bits of following code were adapted from
// Rickey's world of Microelectronics under the creative commons 2.5 license.
// http://www.8051projects.net/mmc-sd-interface-fat16/final-code.php
static void ShowFatTime(u32 ThisTime, char* msg)
{
   u8 AM = 1;

   int Hour, Minute, Second;

   Hour = ThisTime >> 11;        // bits 15 through 11 hold Hour...
   Minute = ThisTime & 0x07E0;   // bits 10 through 5 hold Minute... 0000 0111 1110 0000
   Minute = Minute >> 5;
   Second = ThisTime & 0x001F;   //bits 4 through 0 hold Second...   0000 0000 0001 1111
   
   if( Hour > 11 )
   {
      AM = 0;
      if( Hour > 12 )
         Hour -= 12;
   }
     
   sprintf( msg, "%02d:%02d:%02d %s", Hour, Minute, Second*2,
         (AM)?"AM":"PM");
   return;
}

static void ShowFatDate(u32 ThisDate, char* msg)
{

   int Year, Month, Day;

   Year = ThisDate >> 9;         // bits 15 through 9 hold year...
   Month = ThisDate & 0x01E0;    // bits 8 through 5 hold month... 0000 0001 1110 0000
   Month = Month >> 5;
   Day = ThisDate & 0x001F;      //bits 4 through 0 hold day...    0000 0000 0001 1111
   sprintf( msg, "%02d/%02d/%02d", Month, Day, Year-20);
   return;
}

s32 TERMINAL_PrintSdCardInfo(void *_output_function)
{
  void (*out)(char *format, ...) = _output_function;

  FRESULT res;
  FILINFO fno;
  DIR dir;
  char *fn;
  char str_buffer[128];

  MUTEX_MIDIOUT_TAKE;

  out("SD Card Informations\n");
  out("====================\n");

#if !defined(MIOS32_FAMILY_EMULATION)
  // this yield ensures, that Debug Messages are sent before we continue the execution
  // Since MIOS Studio displays the time at which the messages arrived, this allows
  // us to measure the delay of following operations
  taskYIELD();

  MUTEX_SDCARD_TAKE;
  FILE_PrintSDCardInfos();
  MUTEX_SDCARD_GIVE;
#endif

  out("\n");
  out("Reading Root Directory\n");
  out("======================\n");

  taskYIELD();

  if( !FILE_SDCardAvailable() ) {
    sprintf(str_buffer, "not connected");
  } else if( !FILE_VolumeAvailable() ) {
    sprintf(str_buffer, "Invalid FAT");
  } else {
    out("Retrieving SD Card informations - please wait!\n");
    MUTEX_MIDIOUT_GIVE;
    MUTEX_SDCARD_TAKE;
    FILE_UpdateFreeBytes();
    MUTEX_SDCARD_GIVE;
    MUTEX_MIDIOUT_TAKE;

    sprintf(str_buffer, "'%s': %u of %u MB free",
        FILE_VolumeLabel(),
        (unsigned int)(FILE_VolumeBytesFree()/1000000),
        (unsigned int)(FILE_VolumeBytesTotal()/1000000));
  }
  out("SD Card: %s\n", str_buffer);

  taskYIELD();

#if _USE_LFN
  static char lfn[_MAX_LFN * (_DF1S ? 2 : 1) + 1];
  fno.lfname = lfn;
  fno.lfsize = sizeof(lfn);
#endif

  MUTEX_SDCARD_TAKE;
  if( (res=f_opendir(&dir, "/")) != FR_OK ) {
    out("Failed to open root directory - error status: %d\n", res);
  } else {
    while( (f_readdir(&dir, &fno) == FR_OK) && fno.fname[0] ) {
#if _USE_LFN
      fn = *fno.lfname ? fno.lfname : fno.fname;
#else
      fn = fno.fname;
#endif
      char date[10];
      ShowFatDate(fno.fdate,(char*)&date);
      char time[12];
      ShowFatTime(fno.ftime,(char*)&time);
      out("[%s%s%s%s%s%s%s] %s  %s   %s %u %s\n",
        (fno.fattrib & AM_RDO ) ? "r" : ".",
        (fno.fattrib & AM_HID ) ? "h" : ".",
        (fno.fattrib & AM_SYS ) ? "s" : ".",
        (fno.fattrib & AM_VOL ) ? "v" : ".",
        (fno.fattrib & AM_LFN ) ? "l" : ".",
        (fno.fattrib & AM_DIR ) ? "d" : ".",
        (fno.fattrib & AM_ARC ) ? "a" : ".",
        date,time,
        (fno.fattrib & AM_DIR) ? "<DIR>" : " ",
        fno.fsize,fn);
    }
  }
  MUTEX_SDCARD_GIVE;

  taskYIELD();

  out("done.\n");
  MUTEX_MIDIOUT_GIVE;

  return 0; // no error
}

/////////////////////////////////////////////////////////////////////////////
// NRPN list
/////////////////////////////////////////////////////////////////////////////
s32 TERMINAL_ShowNrpns(void *_output_function)
{
  void (*out)(char *format, ...) = _output_function;
  MbCvEnvironment* env = APP_GetEnv();

  MUTEX_MIDIOUT_TAKE;

  int prev_cv = -1;
  for(int nrpnNumber=0; nrpnNumber<16384; ++nrpnNumber) {
    MbCvNrpnInfoT info;
    if( env->getNRPNInfo(nrpnNumber, &info) ) {
      if( prev_cv != info.cv ) {
    prev_cv = info.cv;
    out("+-------------------------------------------+-------+--------+------+------+------------+");
    out("| Parameter                                 | Number Dec/Hex | Val. |  Dec | Range      |");
    out("+-------------------------------------------+-------+--------+------+------+------------+");
      }

      int value = info.value;
      int min = info.min;
      int max = info.max;

      if( info.is_bidir ) {
    u16 range = max - min + 1;
    min -= range / 2;
    max -= range / 2;
    value -= range / 2;
      }
      out("| %s %s | %5d | 0x%04x | 0x%02x | %4d | %4d..%4d |", info.nameString, info.valueString, nrpnNumber, nrpnNumber, info.value, value, min, max);
    }
  }
  out("+-------------------------------------------+-------+--------+------+------+------------+");

  MUTEX_MIDIOUT_GIVE;

  return 0; // no error
}