Skip to content

Commit

Permalink
[posix] allow disabling CLI for daemon (openthread#9357)
Browse files Browse the repository at this point in the history
The CLI interface for the posix daemon ise useful for testing and
debugging but may not be useful for release/user builds. on size
sensitive plaforms such as Android, disabling the daemon CLI will
reduce the ot-daemon binary size by around 150 KB.

So this commit defines config
"OPENTHREAD_POSIX_CONFIG_DAEMON_CLI_ENABLE" to make the daemon CLI
interface optional.
  • Loading branch information
wgtdkp authored Aug 17, 2023
1 parent f14d726 commit 102a631
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/posix/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,10 @@ int main(int argc, char *argv[])
#if !OPENTHREAD_POSIX_CONFIG_DAEMON_ENABLE
otAppCliInit(instance);
#endif

#if !OPENTHREAD_POSIX_CONFIG_DAEMON_ENABLE || OPENTHREAD_POSIX_CONFIG_DAEMON_CLI_ENABLE
IgnoreError(otCliSetUserCommands(kCommands, OT_ARRAY_LENGTH(kCommands), instance));
#endif

while (true)
{
Expand Down
18 changes: 18 additions & 0 deletions src/posix/platform/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ void GetFilename(Filename &aFilename, const char *aPattern)

} // namespace

int Daemon::OutputFormat(const char *aFormat, ...)
{
int ret;
va_list ap;

va_start(ap, aFormat);
ret = OutputFormatV(aFormat, ap);
va_end(ap);

return ret;
}

int Daemon::OutputFormatV(const char *aFormat, va_list aArguments)
{
static constexpr char truncatedMsg[] = "(truncated ...)";
Expand Down Expand Up @@ -244,12 +256,14 @@ void Daemon::SetUp(void)
DieNowWithMessage("listen", OT_EXIT_ERROR_ERRNO);
}

#if OPENTHREAD_POSIX_CONFIG_DAEMON_CLI_ENABLE
otCliInit(
gInstance,
[](void *aContext, const char *aFormat, va_list aArguments) -> int {
return static_cast<Daemon *>(aContext)->OutputFormatV(aFormat, aArguments);
},
this);
#endif

Mainloop::Manager::Get().Add(*this);

Expand Down Expand Up @@ -349,7 +363,11 @@ void Daemon::Process(const otSysMainloopContext &aContext)
if (rval > 0)
{
buffer[rval] = '\0';
#if OPENTHREAD_POSIX_CONFIG_DAEMON_CLI_ENABLE
otCliInputLine(reinterpret_cast<char *>(buffer));
#else
OutputFormat("Error: CLI is disabled!\n");
#endif
}
else
{
Expand Down
1 change: 1 addition & 0 deletions src/posix/platform/daemon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Daemon : public Mainloop::Source, private NonCopyable
void Process(const otSysMainloopContext &aContext) override;

private:
int OutputFormat(const char *aFormat, ...);
int OutputFormatV(const char *aFormat, va_list aArguments);
void InitializeSessionSocket(void);

Expand Down
10 changes: 10 additions & 0 deletions src/posix/platform/openthread-posix-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@
#define OPENTHREAD_POSIX_CONFIG_DAEMON_ENABLE 0
#endif

/**
* @def OPENTHREAD_POSIX_CONFIG_DAEMON_CLI_ENABLE
*
* Define to 1 to enable CLI for the posix daemon.
*
*/
#ifndef OPENTHREAD_POSIX_CONFIG_DAEMON_CLI_ENABLE
#define OPENTHREAD_POSIX_CONFIG_DAEMON_CLI_ENABLE 1
#endif

/**
* RCP bus UART.
*
Expand Down

0 comments on commit 102a631

Please sign in to comment.