aboutsummaryrefslogtreecommitdiff
path: root/helper/test/cli.c
blob: 08e7501530245476182a7da99caeff5b26e14c86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright (c) 2021 Nokia
 */

#include <odp_api.h>
#include <odp/helper/odph_api.h>

static int cli_server(void *arg ODP_UNUSED)
{
	if (odph_cli_run()) {
		ODPH_ERR("odph_cli_run() failed.\n");
		exit(EXIT_FAILURE);
	}

	return 0;
}

int main(int argc, char *argv[])
{
	odp_instance_t instance;
	odph_helper_options_t helper_options;
	odp_init_t init_param;

	argc = odph_parse_options(argc, argv);
	if (odph_options(&helper_options)) {
		ODPH_ERR("Error: reading ODP helper options failed.\n");
		exit(EXIT_FAILURE);
	}

	odp_init_param_init(&init_param);
	init_param.mem_model = helper_options.mem_model;

	memset(&instance, 0, sizeof(instance));

	if (odp_init_global(&instance, NULL, NULL)) {
		ODPH_ERR("Error: ODP global init failed.\n");
		exit(EXIT_FAILURE);
	}

	if (odp_init_local(instance, ODP_THREAD_CONTROL)) {
		ODPH_ERR("Error: ODP local init failed.\n");
		exit(EXIT_FAILURE);
	}

	odph_cli_param_t cli_param;

	odph_cli_param_init(&cli_param);

	if (odph_cli_init(&cli_param)) {
		ODPH_ERR("Error: odph_cli_init() failed.\n");
		exit(EXIT_FAILURE);
	}

	odp_cpumask_t cpumask;
	odph_thread_common_param_t thr_common;
	odph_thread_param_t thr_param;
	odph_thread_t thr_server;

	if (odp_cpumask_default_control(&cpumask, 1) != 1) {
		ODPH_ERR("Failed to get default CPU mask.\n");
		exit(EXIT_FAILURE);
	}

	odph_thread_common_param_init(&thr_common);
	thr_common.instance = instance;
	thr_common.cpumask = &cpumask;

	odph_thread_param_init(&thr_param);
	thr_param.thr_type = ODP_THREAD_CONTROL;
	thr_param.start = cli_server;

	memset(&thr_server, 0, sizeof(thr_server));

	if (odph_thread_create(&thr_server, &thr_common, &thr_param, 1) != 1) {
		ODPH_ERR("Failed to create server thread.\n");
		exit(EXIT_FAILURE);
	}

	/*
	 * Wait for a bit to ensure that the server thread has time to start.
	 */
	odp_time_wait_ns(ODP_TIME_SEC_IN_NS / 10);

	if (odph_cli_stop()) {
		ODPH_ERR("Error: odph_cli_stop() failed.\n");
		exit(EXIT_FAILURE);
	}

	if (odph_thread_join(&thr_server, 1) != 1) {
		ODPH_ERR("Failed to join server thread.\n");
		exit(EXIT_FAILURE);
	}

	if (odph_cli_term()) {
		ODPH_ERR("Error: odph_cli_term() failed.\n");
		exit(EXIT_FAILURE);
	}

	if (odp_term_local()) {
		ODPH_ERR("Error: ODP local term failed.\n");
		exit(EXIT_FAILURE);
	}

	if (odp_term_global(instance)) {
		ODPH_ERR("Error: ODP global term failed.\n");
		exit(EXIT_FAILURE);
	}

	return EXIT_SUCCESS;
}