16 #include <odp/helper/odph_api.h>
28 static void usage(
const char *prog)
31 "Usage: %s [options]\n"
34 " -t, --time <sec> Keep CLI open for <sec> seconds. (default -1 (infinite))\n"
35 " -a, --address <addr> Bind listening socket to IP address <addr>.\n"
36 " -p, --port <port> Bind listening socket to port <port>.\n"
38 "ODP helper defaults are used for address and port, if the options are\n"
44 static void parse_args(
int argc,
char *argv[], options_t *opt)
46 static const struct option longopts[] = {
47 {
"time", required_argument, NULL,
't' },
48 {
"address", required_argument, NULL,
'a' },
49 {
"port", required_argument, NULL,
'p' },
50 {
"help", no_argument, NULL,
'h' },
54 static const char *shortopts =
"+t:a:p:h";
57 int c = getopt_long(argc, argv, shortopts, longopts, NULL);
64 opt->time = atoi(optarg);
70 opt->port = atoi(optarg);
82 static volatile int shutdown_sig;
84 static void sig_handler(
int signo)
91 static void my_cmd(
int argc,
char *argv[])
93 odph_cli_log(
"%s(%d): %s\n", __FILE__, __LINE__, __func__);
95 for (
int i = 0; i < argc; i++)
96 odph_cli_log(
"argv[%d]: %s\n", i, argv[i]);
102 if (odph_cli_run()) {
103 ODPH_ERR(
"odph_cli_run() failed.\n");
110 int main(
int argc,
char *argv[])
112 signal(SIGINT, sig_handler);
114 odph_helper_options_t helper_options;
117 argc = odph_parse_options(argc, argv);
118 if (odph_options(&helper_options)) {
119 ODPH_ERR(
"Error: reading ODP helper options failed.\n");
126 init.
mem_model = helper_options.mem_model;
134 parse_args(argc, argv, &opt);
141 ODPH_ERR(
"Global init failed.\n");
146 ODPH_ERR(
"Local init failed.\n");
152 odph_cli_param_t cli_param;
154 odph_cli_param_init(&cli_param);
157 cli_param.address = opt.addr;
160 cli_param.port = opt.port;
163 if (odph_cli_init(&cli_param)) {
164 ODPH_ERR(
"CLI helper initialization failed.\n");
169 if (odph_cli_register_command(
"my_command", my_cmd,
170 "Example user command.")) {
171 ODPH_ERR(
"Registering user command failed.\n");
178 odph_thread_common_param_t thr_common;
179 odph_thread_param_t thr_param;
180 odph_thread_t thr_server;
181 odph_thread_join_result_t res;
184 ODPH_ERR(
"Failed to get default CPU mask.\n");
188 odph_thread_common_param_init(&thr_common);
189 thr_common.instance = inst;
190 thr_common.cpumask = &cpumask;
192 odph_thread_param_init(&thr_param);
194 thr_param.start = cli_server;
196 memset(&thr_server, 0,
sizeof(thr_server));
198 if (odph_thread_create(&thr_server, &thr_common, &thr_param, 1) != 1) {
199 ODPH_ERR(
"Failed to create server thread.\n");
203 printf(
"CLI server started on %s:%d.\n", cli_param.address,
207 for (
int i = 0; (opt.time < 0 || i < opt.time) && !shutdown_sig; i++)
210 printf(
"Stopping CLI server.\n");
213 if (odph_cli_stop()) {
214 ODPH_ERR(
"CLI stop failed.\n");
219 if (odph_thread_join_result(&thr_server, &res, 1) != 1) {
220 ODPH_ERR(
"Failed to join server thread.\n");
224 if (res.is_sig || res.ret != 0) {
225 ODPH_ERR(
"Worker thread failure%s: %d.\n", res.is_sig ?
" (signaled)" :
"",
231 if (odph_cli_term()) {
232 ODPH_ERR(
"CLI helper termination failed.\n");
239 ODPH_ERR(
"Local term failed.\n");
244 ODPH_ERR(
"Global term failed.\n");
#define ODP_UNUSED
Intentionally unused variables of functions.
int odp_cpumask_default_control(odp_cpumask_t *mask, int num)
Default CPU mask for control threads.
void odp_init_param_init(odp_init_t *param)
Initialize the odp_init_t to default values for all fields.
int odp_init_local(odp_instance_t instance, odp_thread_type_t thr_type)
Thread local ODP initialization.
int odp_init_global(odp_instance_t *instance, const odp_init_t *params, const odp_platform_init_t *platform_params)
Global ODP initialization.
int odp_term_local(void)
Thread local ODP termination.
int odp_term_global(odp_instance_t instance)
Global ODP termination.
uint64_t odp_instance_t
ODP instance ID.
@ ODP_THREAD_CONTROL
Control thread.
#define ODP_TIME_SEC_IN_NS
A second in nanoseconds.
void odp_time_wait_ns(uint64_t ns)
Wait the specified number of nanoseconds.
Global initialization parameters.
odp_mem_model_t mem_model
Application memory model.