- /**
- * @brief Command line processing callback function
- * @param[in] session Handle referencing an SCP session
- * @param[in] user NULL-terminated string that contains the user name
- * @param[in] path Canonical path of the file
- * @return Permissions for the specified file
- **/
- #define MAX_BUFFER_SIZE 256 // Adjust buffer size as needed
- error_t shellServerCommandLineCallback(ShellServerSession *session,
- char_t *commandLine) {
- error_t error;
- // Debug message
- TRACE_INFO("Shell server: Command line received\r\n");
- TRACE_INFO(" %s\r\n", commandLine);
- // Flag to control the continuous UART communication loop
- bool exitLoop = false;
- // Buffer to store received characters until a newline is detected
- char receivedBuffer[MAX_BUFFER_SIZE];
- int bufferIndex = 0;
- // Check command name
- if (!strcasecmp(commandLine, "hello")) {
- // Send response to the client
- error = shellServerWriteStream(session, "Hello World!\r\n", 14,
- NULL, 0);
- // UART output: Sending a hello message
- UARTCharPut(UART0_BASE, 'S');
- UARTCharPut(UART0_BASE, 'e');
- UARTCharPut(UART0_BASE, 'n');
- UARTCharPut(UART0_BASE, 'd');
- UARTCharPut(UART0_BASE, 'i');
- UARTCharPut(UART0_BASE, 'n');
- UARTCharPut(UART0_BASE, 'g');
- UARTCharPut(UART0_BASE, ' ');
- UARTCharPut(UART0_BASE, 'H');
- UARTCharPut(UART0_BASE, 'e');
- UARTCharPut(UART0_BASE, 'l');
- UARTCharPut(UART0_BASE, 'l');
- UARTCharPut(UART0_BASE, 'o');
- UARTCharPut(UART0_BASE, ' ');
- UARTCharPut(UART0_BASE, 'W');
- UARTCharPut(UART0_BASE, 'o');
- UARTCharPut(UART0_BASE, 'r');
- UARTCharPut(UART0_BASE, 'l');
- UARTCharPut(UART0_BASE, 'd');
- UARTCharPut(UART0_BASE, '!');
- UARTCharPut(UART0_BASE, '\r');
- UARTCharPut(UART0_BASE, '\n');
- // Continuous UART communication loop
- while (!exitLoop) {
- if (UARTCharsAvail(UART0_BASE)) {
- char receivedChar = UARTCharGet(UART0_BASE);
- UARTCharPut(UART0_BASE, receivedChar);
- // Buffer the received character
- receivedBuffer[bufferIndex++] = receivedChar;
- // Check for newline character or buffer overflow
- if (receivedChar == '\n' || bufferIndex >= MAX_BUFFER_SIZE) {
- // Null-terminate the buffer to treat it as a string
- receivedBuffer[bufferIndex] = '\0';
- bufferIndex = 0; // Reset buffer index
- // Relay the buffered line to SSH server
- shellServerWriteStream(session, receivedBuffer, strlen(receivedBuffer), NULL, 0);
- // Check for exit command
- if (strcmp(receivedBuffer, "exit1\n") == 0 || strcmp(receivedBuffer, "quit1\n") == 0) {
- exitLoop = true;
- }
- }
- }
- }
- } else if (!strcasecmp(commandLine, "exit") || !strcasecmp(commandLine, "quit")) {
- // Close shell session
- error = ERROR_END_OF_STREAM;
- } else {
- // Unknown command received
- error = shellServerWriteStream(session, "Unknown command!\r\n", 18,
- NULL, 0);
- }
- // Return status code
- return error;
- }