[keystone-rtos/netapi.git] / ti / runtime / netapi / applications / ipsec_offload / config-app / src / cmd_shell_main.c
1 /*
2 * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
3 *
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the
15 * distribution.
16 *
17 * Neither the name of Texas Instruments Incorporated nor the names of
18 * its contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <sys/types.h>
38 #include <time.h>
39 #include <pthread.h>
40 #include <string.h>
42 #include <ipsecmgr_ipc.h>
43 #include <ipsecmgr_syslog.h>
45 #include "cmd_shell_loc.h"
47 typedef pthread_t task_handle;
49 #define DEFAULT_STACK_SIZE 0x8000
50 static int task_create ( void *(start_routine)(void*), void* args, void* handle)
51 {
52 int max_priority, err;
53 pthread_t thread;
54 pthread_attr_t attr;
55 struct sched_param param;
57 max_priority = sched_get_priority_max(SCHED_FIFO);
58 err = pthread_attr_init(&attr);
59 if (err) {
60 ipsecmgr_syslog_msg(SYSLOG_LEVEL_ERROR,
61 "pthread_attr_init failed: (%s)\n", strerror(err));
62 return err;
63 }
64 err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
65 if (err) {
66 ipsecmgr_syslog_msg(SYSLOG_LEVEL_ERROR,
67 "pthread_attr_setdetachstate failed: (%s)\n", strerror(err));
68 return err;
69 }
70 err = pthread_attr_setstacksize(&attr, DEFAULT_STACK_SIZE);
71 if (err) {
72 ipsecmgr_syslog_msg(SYSLOG_LEVEL_ERROR,
73 "pthread_attr_setstacksize failed: (%s)\n", strerror(err));
74 return err;
75 }
76 err = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
77 if (err) {
78 ipsecmgr_syslog_msg(SYSLOG_LEVEL_ERROR,
79 "pthread_attr_setinheritsched failed: (%s)\n", strerror(err));
80 return err;
81 }
82 err = pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
83 if (err) {
84 ipsecmgr_syslog_msg(SYSLOG_LEVEL_ERROR,
85 "pthread_attr_setschedpolicy failed: (%s)\n", strerror(err));
86 return err;
87 }
88 memset(¶m, 0, sizeof(param));
89 param.sched_priority = max_priority;
90 err = pthread_attr_setschedparam(&attr, ¶m);
91 if (err) {
92 ipsecmgr_syslog_msg(SYSLOG_LEVEL_ERROR,
93 "pthread_attr_setschedparam failed: (%s)\n", strerror(err));
94 return err;
95 }
96 if (err) return err;
97 err = pthread_create(&thread, &attr, start_routine, args);
98 if (err) {
99 ipsecmgr_syslog_msg(SYSLOG_LEVEL_ERROR,
100 "pthread_create failed: (%s)\n", strerror(err));
101 return err;
102 }
103 if (err) return err;
104 *(pthread_t*)handle = thread;
105 return 0;
106 }
108 static void task_wait (void *handle)
109 {
110 pthread_join(*((pthread_t*)handle), NULL);
111 return;
112 }
114 static void task_sleep(int time_in_msec)
115 {
116 pthread_mutex_t fake_mutex = PTHREAD_MUTEX_INITIALIZER;
117 pthread_cond_t fake_cond = PTHREAD_COND_INITIALIZER;
118 struct timespec ts;
119 int rt;
120 unsigned int sec, nsec;
122 sec = time_in_msec/1000;
123 nsec = (time_in_msec - (sec*1000)) * 1000000;
125 /* Use the wall-clock time */
126 clock_gettime(CLOCK_REALTIME, &ts);
128 ts.tv_sec += sec;
129 ts.tv_nsec += nsec;
131 pthread_mutex_lock(&fake_mutex);
132 rt = pthread_cond_timedwait(&fake_cond, &fake_mutex, &ts);
133 pthread_mutex_unlock(&fake_mutex);
134 }
136 #define IPC_POLL_INTVL 1000
138 static void *ipc_poll(void *args)
139 {
140 while(1) {
141 ipsecmgr_ipc_poll();
142 task_sleep(IPC_POLL_INTVL);
143 } /* end while */
144 return (void*)(0);
145 }
147 /* usage ipsecmgr_cmd_shell
148 */
149 int main(int argc, char **argv)
150 {
151 ipsecmgr_ipc_cfg_t ipc_cfg;
152 ipsecmgr_ipc_user_recv_if_t recv_if;
153 task_handle cmd_th, ipc_th;
154 int status;
156 /* Start logging module */
157 if (ipsecmgr_syslog_init()) {
158 printf ("Failed to initialize syslog\n");
159 return -1;
160 }
162 memset(&ipc_cfg, 0, sizeof(ipc_cfg));
163 ipc_cfg.mode = IPC_MODE_USER_APP;
165 if (ipsecmgr_ipc_init(&ipc_cfg)) {
166 printf ("Failed to initialize IPC\n");
167 return -1;
168 }
170 recv_if.offload_sp_rsp = cmd_shell_offload_sp_rsp;
171 recv_if.stop_offload_rsp = cmd_shell_stop_offload_rsp;
173 if (ipsecmgr_ipc_register_user_recv_iface(&recv_if)) {
174 printf ("Failed to IPC recv interface\n");
175 return -1;
176 }
178 /* Create the command line interpreter task */
179 if (status = task_create(cmd_shell, NULL, &cmd_th)) {
180 ipsecmgr_syslog_msg(SYSLOG_LEVEL_ERROR,
181 "ERROR: Command shell task-create failed (%d)\n", status);
182 return (-1);
183 }
185 /* Create the FP IPC poll task */
186 if (status = task_create(ipc_poll, NULL, &ipc_th)) {
187 ipsecmgr_syslog_msg(SYSLOG_LEVEL_ERROR,
188 "ERROR: IPC recv task-create failed (%d)\n", status);
189 return (-1);
190 }
192 task_wait(&cmd_th);
193 return 0;
194 }