Facebook
From test, 1 Month ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 146
  1. /**
  2.  * @brief Command line processing callback function
  3.  * @param[in] session Handle referencing an SCP session
  4.  * @param[in] user NULL-terminated string that contains the user name
  5.  * @param[in] path Canonical path of the file
  6.  * @return Permissions for the specified file
  7.  **/
  8.  
  9. error_t shellServerCommandLineCallback(ShellServerSession *session,
  10.                                        char_t *commandLine) {
  11.     error_t error;
  12.  
  13.     // Debug message
  14.     TRACE_INFO("Shell server: Command line received\r\n");
  15.     TRACE_INFO("  %s\r\n", commandLine);
  16.  
  17.     // Flag to control the continuous UART communication loop
  18.     bool exitLoop = false;
  19.  
  20.     // Check command name
  21.     if (!strcasecmp(commandLine, "hello")) {
  22.         // Send response to the client
  23.         error = shellServerWriteStream(session, "Hello World!\r\n", 14,
  24.                                        NULL, 0);
  25.  
  26.         // UART output: Sending a hello message
  27.         UARTCharPut(UART0_BASE, 'S');
  28.         UARTCharPut(UART0_BASE, 'e');
  29.         UARTCharPut(UART0_BASE, 'n');
  30.         UARTCharPut(UART0_BASE, 'd');
  31.         UARTCharPut(UART0_BASE, 'i');
  32.         UARTCharPut(UART0_BASE, 'n');
  33.         UARTCharPut(UART0_BASE, 'g');
  34.         UARTCharPut(UART0_BASE, ' ');
  35.         UARTCharPut(UART0_BASE, 'H');
  36.         UARTCharPut(UART0_BASE, 'e');
  37.         UARTCharPut(UART0_BASE, 'l');
  38.         UARTCharPut(UART0_BASE, 'l');
  39.         UARTCharPut(UART0_BASE, 'o');
  40.         UARTCharPut(UART0_BASE, ' ');
  41.         UARTCharPut(UART0_BASE, 'W');
  42.         UARTCharPut(UART0_BASE, 'o');
  43.         UARTCharPut(UART0_BASE, 'r');
  44.         UARTCharPut(UART0_BASE, 'l');
  45.         UARTCharPut(UART0_BASE, 'd');
  46.         UARTCharPut(UART0_BASE, '!');
  47.         UARTCharPut(UART0_BASE, '\r');
  48.         UARTCharPut(UART0_BASE, '\n');
  49.  
  50.         // Continuous UART communication loop
  51.         while (!exitLoop) {
  52.             if (UARTCharsAvail(UART0_BASE)) {
  53.                 char receivedChar = UARTCharGet(UART0_BASE);
  54.  
  55.                 // Check for exit command
  56.                 if (receivedChar == 'e' || receivedChar == 'E') {
  57.                     // Check if the next characters match "exit" or "quit"
  58.                     char nextChar1 = UARTCharGet(UART0_BASE);
  59.                     char nextChar2 = UARTCharGet(UART0_BASE);
  60.                     if ((nextChar1 == 'x' || nextChar1 == 'X') &&
  61.                         (nextChar2 == 'i' || nextChar2 == 'I') &&
  62.                         (UARTCharGet(UART0_BASE) == 't' || UARTCharGet(UART0_BASE) == 'T')) {
  63.                         // Exit the loop
  64.                         exitLoop = true;
  65.                         break; // Exit the loop immediately
  66.                     }
  67.                 }
  68.  
  69.                 // Echo the received character back to UART
  70.                 UARTCharPut(UART0_BASE, receivedChar);
  71.  
  72.                 // Relay UART input to SSH server
  73.                 shellServerWriteStream(session, &receivedChar;, 1, NULL, 0);
  74.                 }
  75.               }
  76.     } else if (!strcasecmp(commandLine, "exit") || !strcasecmp(commandLine, "quit")) {
  77.         // Close shell session
  78.         error = ERROR_END_OF_STREAM;
  79.     } else {
  80.         // Unknown command received
  81.         error = shellServerWriteStream(session, "Unknown command!\r\n", 18,
  82.                                        NULL, 0);
  83.     }
  84.  
  85.     // Return status code
  86.     return error;
  87. }