Skip to content

Commit

Permalink
[posix] add otSysCliInitUsingDaemon API (#9897)
Browse files Browse the repository at this point in the history
This is to initialize CLI for daemon instance.

With this API, user can redirect the Vendor server CLI output when
needed, and then reset back using this API when the CLI output should
be restored.
  • Loading branch information
morningboata authored Mar 6, 2024
1 parent 125e726 commit 036f6eb
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
7 changes: 1 addition & 6 deletions src/posix/platform/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,7 @@ void Daemon::SetUp(void)
}

#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);
otSysCliInitUsingDaemon(gInstance);
#endif

Mainloop::Manager::Get().Add(*this);
Expand Down
2 changes: 1 addition & 1 deletion src/posix/platform/daemon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ class Daemon : public Mainloop::Source, private NonCopyable
void TearDown(void);
void Update(otSysMainloopContext &aContext) override;
void Process(const otSysMainloopContext &aContext) override;
int OutputFormatV(const char *aFormat, va_list aArguments);

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

Expand Down
13 changes: 13 additions & 0 deletions src/posix/platform/include/openthread/openthread-system.h
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,19 @@ void otSysSetInfraNetif(const char *aInfraNetifName, int aIcmp6Socket);
*/
bool otSysInfraIfIsRunning(void);

/**
* Initializes the CLI module using the daemon.
*
* This function initializes the CLI module, and assigns the daemon to handle
* the CLI output. This function can be invoked multiple times. The typical use case
* is that, after OTBR/vendor_server's CLI output redirection, it uses this API to
* restore the original daemon's CLI output.
*
* @param[in] aInstance The OpenThread instance structure.
*
*/
void otSysCliInitUsingDaemon(otInstance *aInstance);

#ifdef __cplusplus
} // end of extern "C"
#endif
Expand Down
13 changes: 13 additions & 0 deletions src/posix/platform/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

#include <openthread-core-config.h>
#include <openthread/border_router.h>
#include <openthread/cli.h>
#include <openthread/heap.h>
#include <openthread/tasklet.h>
#include <openthread/platform/alarm-milli.h>
Expand Down Expand Up @@ -401,3 +402,15 @@ void otSysMainloopProcess(otInstance *aInstance, const otSysMainloopContext *aMa
}

bool IsSystemDryRun(void) { return gDryRun; }

#if OPENTHREAD_POSIX_CONFIG_DAEMON_ENABLE && OPENTHREAD_POSIX_CONFIG_DAEMON_CLI_ENABLE
void otSysCliInitUsingDaemon(otInstance *aInstance)
{
otCliInit(
aInstance,
[](void *aContext, const char *aFormat, va_list aArguments) -> int {
return static_cast<ot::Posix::Daemon *>(aContext)->OutputFormatV(aFormat, aArguments);
},
&ot::Posix::Daemon::Get());
}
#endif

0 comments on commit 036f6eb

Please sign in to comment.