Facebook
From test, 1 Month ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 140
  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. #define MAX_BUFFER_SIZE 256 // Adjust buffer size as needed
  10.  
  11. error_t shellServerCommandLineCallback(ShellServerSession *session,
  12.                                        char_t *commandLine) {
  13.     error_t error;
  14.  
  15.     // Debug message
  16.     TRACE_INFO("Shell server: Command line received\r\n");
  17.     TRACE_INFO("  %s\r\n", commandLine);
  18.  
  19.     // Flag to control the continuous UART communication loop
  20.     bool exitLoop = false;
  21.  
  22.     // Buffer to store received characters until a newline is detected
  23.     char receivedBuffer[MAX_BUFFER_SIZE];
  24.     int bufferIndex = 0;
  25.  
  26.     // Check command name
  27.     if (!strcasecmp(commandLine, "hello")) {
  28.         // Send response to the client
  29.         error = shellServerWriteStream(session, "Hello World!\r\n", 14,
  30.                                        NULL, 0);
  31.  
  32.         // UART output: Sending a hello message
  33.         UARTCharPut(UART0_BASE, 'S');
  34.         UARTCharPut(UART0_BASE, 'e');
  35.         UARTCharPut(UART0_BASE, 'n');
  36.         UARTCharPut(UART0_BASE, 'd');
  37.         UARTCharPut(UART0_BASE, 'i');
  38.         UARTCharPut(UART0_BASE, 'n');
  39.         UARTCharPut(UART0_BASE, 'g');
  40.         UARTCharPut(UART0_BASE, ' ');
  41.         UARTCharPut(UART0_BASE, 'H');
  42.         UARTCharPut(UART0_BASE, 'e');
  43.         UARTCharPut(UART0_BASE, 'l');
  44.         UARTCharPut(UART0_BASE, 'l');
  45.         UARTCharPut(UART0_BASE, 'o');
  46.         UARTCharPut(UART0_BASE, ' ');
  47.         UARTCharPut(UART0_BASE, 'W');
  48.         UARTCharPut(UART0_BASE, 'o');
  49.         UARTCharPut(UART0_BASE, 'r');
  50.         UARTCharPut(UART0_BASE, 'l');
  51.         UARTCharPut(UART0_BASE, 'd');
  52.         UARTCharPut(UART0_BASE, '!');
  53.         UARTCharPut(UART0_BASE, '\r');
  54.         UARTCharPut(UART0_BASE, '\n');
  55.  
  56.         // Continuous UART communication loop
  57.         while (!exitLoop) {
  58.             if (UARTCharsAvail(UART0_BASE)) {
  59.                 char receivedChar = UARTCharGet(UART0_BASE);
  60.                 UARTCharPut(UART0_BASE, receivedChar);
  61.  
  62.                 // Buffer the received character
  63.                 receivedBuffer[bufferIndex++] = receivedChar;
  64.                 // Check for newline character or buffer overflow
  65.                 if (receivedChar == '\n' || bufferIndex >= MAX_BUFFER_SIZE) {
  66.                     // Null-terminate the buffer to treat it as a string
  67.                     receivedBuffer[bufferIndex] = '\0';
  68.                     bufferIndex = 0; // Reset buffer index
  69.  
  70.                     // Relay the buffered line to SSH server
  71.                     shellServerWriteStream(session, receivedBuffer, strlen(receivedBuffer), NULL, 0);
  72.  
  73.                     // Check for exit command
  74.                     if (strcmp(receivedBuffer, "exit1\n") == 0 || strcmp(receivedBuffer, "quit1\n") == 0) {
  75.                         exitLoop = true;
  76.                     }
  77.                 }
  78.             }
  79.         }
  80.     } else if (!strcasecmp(commandLine, "exit") || !strcasecmp(commandLine, "quit")) {
  81.         // Close shell session
  82.         error = ERROR_END_OF_STREAM;
  83.     } else {
  84.         // Unknown command received
  85.         error = shellServerWriteStream(session, "Unknown command!\r\n", 18,
  86.                                        NULL, 0);
  87.     }
  88.  
  89.     // Return status code
  90.     return error;
  91. }
  92.