LCOV - code coverage report
Current view: top level - libs/bsw/asyncConsole/src/console - AsyncConsole.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 13.6 % 44 6
Test Date: 2026-06-18 08:29:03 Functions: 33.3 % 6 2

            Line data    Source code
       1              : /********************************************************************************
       2              :  * Copyright (c) 2024 Accenture
       3              :  *
       4              :  * This program and the accompanying materials are made available under the
       5              :  * terms of the Apache License Version 2.0 which is available at
       6              :  * https://www.apache.org/licenses/LICENSE-2.0
       7              :  *
       8              :  * SPDX-License-Identifier: Apache-2.0
       9              :  ********************************************************************************/
      10              : 
      11              : // NOLINTBEGIN(cppcoreguidelines-pro-type-vararg): Logger/StringWriter API is variadic by design.
      12              : 
      13              : #include "console/AsyncConsole.h"
      14              : 
      15              : #include "console/SyncCommandWrapper.h"
      16              : #include "logger/ConsoleLogger.h"
      17              : 
      18              : #include <etl/singleton_base.h>
      19              : #include <etl/string.h>
      20              : #include <util/command/HelpCommand.h>
      21              : #include <util/command/ParentCommand.h>
      22              : #include <util/format/SharedStringWriter.h>
      23              : 
      24              : namespace
      25              : {
      26            1 : ::util::command::ParentCommand& getParentCommand()
      27              : {
      28            1 :     static ::util::command::ParentCommand parentCommand("root", "root");
      29            1 :     return parentCommand;
      30              : }
      31              : } // namespace
      32              : 
      33              : namespace console
      34              : {
      35              : using ::util::logger::CONSOLE;
      36              : using ::util::logger::Logger;
      37              : 
      38            0 : AsyncConsole::AsyncConsole()
      39            0 : : ::etl::singleton_base<AsyncConsole>(*this), fpOutputStream(nullptr), fOnLineProcessed()
      40              : {
      41            0 :     static ::util::command::HelpCommand helpCommand(getParentCommand());
      42            0 :     static ::console::SyncCommandWrapper syncCommandWrapper(helpCommand);
      43            0 : }
      44              : 
      45            1 : void AsyncConsole::addCommand(::util::command::ICommand& command)
      46              : {
      47            1 :     getParentCommand().addCommand(command);
      48            1 : }
      49              : 
      50            0 : void AsyncConsole::commandExecuted(::util::command::ICommand::ExecuteResult result)
      51              : {
      52            0 :     AsyncConsole::instance().terminate(result);
      53            0 : }
      54              : 
      55            0 : void AsyncConsole::onLineReceived(
      56              :     ::util::stream::ISharedOutputStream& outputStream,
      57              :     ::etl::istring const& line,
      58              :     OnLineProcessed const& onLineProcessed)
      59              : {
      60            0 :     auto const cmd = ::util::string::ConstString(line.c_str(), line.length());
      61            0 :     Logger::info(CONSOLE, "Received console command \"%.*s\"", cmd.length(), cmd.data());
      62              : 
      63            0 :     fOnLineProcessed = onLineProcessed;
      64            0 :     fpOutputStream   = &outputStream;
      65              : 
      66              :     ::util::command::ICommand::ExecuteResult const result
      67            0 :         = getParentCommand().execute(cmd, &outputStream);
      68              : 
      69            0 :     if (result.getResult() == ::util::command::ICommand::Result::OK)
      70              :     { // in this case, we terminate since commandExecuted is called
      71            0 :         return;
      72              :     }
      73              : 
      74            0 :     Logger::info(CONSOLE, "Console command failed");
      75              : 
      76            0 :     ::util::format::SharedStringWriter sharedStringWriter(outputStream);
      77            0 :     sharedStringWriter.printf("error\n");
      78              : 
      79            0 :     onLineProcessed();
      80            0 : }
      81              : 
      82            0 : void AsyncConsole::terminate(::util::command::ICommand::ExecuteResult result)
      83              : {
      84            0 :     if (fpOutputStream != nullptr)
      85              :     {
      86            0 :         ::util::format::StringWriter stringWriter(fpOutputStream->startOutput(nullptr));
      87              : 
      88            0 :         switch (result.getResult())
      89              :         {
      90            0 :             case ::util::command::ICommand::Result::OK:
      91              :             {
      92            0 :                 Logger::info(CONSOLE, "Console command succeeded");
      93            0 :                 stringWriter.printf("ok\n");
      94            0 :                 break;
      95              :             }
      96            0 :             default:
      97              :             {
      98            0 :                 Logger::info(CONSOLE, "Console command failed");
      99            0 :                 stringWriter.printf("error\n");
     100            0 :                 break;
     101              :             }
     102              :         }
     103            0 :         fpOutputStream = nullptr;
     104              :     }
     105              : 
     106            0 :     OnLineProcessed onLineProcessed = fOnLineProcessed;
     107            0 :     fOnLineProcessed                = OnLineProcessed();
     108            0 :     onLineProcessed();
     109            0 : }
     110              : 
     111              : } /* namespace console */
     112              : 
     113              : // NOLINTEND(cppcoreguidelines-pro-type-vararg)
        

Generated by: LCOV version 2.0-1