1 /*******************************************************************************
2 **+--------------------------------------------------------------------------+**
3 **| |**
4 **| Copyright 1998-2008 Texas Instruments, Inc. - http://www.ti.com/ |**
5 **| |**
6 **| Licensed under the Apache License, Version 2.0 (the "License"); |**
7 **| you may not use this file except in compliance with the License. |**
8 **| You may obtain a copy of the License at |**
9 **| |**
10 **| http://www.apache.org/licenses/LICENSE-2.0 |**
11 **| |**
12 **| Unless required by applicable law or agreed to in writing, software |**
13 **| distributed under the License is distributed on an "AS IS" BASIS, |**
14 **| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |**
15 **| See the License for the specific language governing permissions and |**
16 **| limitations under the License. |**
17 **| |**
18 **+--------------------------------------------------------------------------+**
19 *******************************************************************************/
20
21
22 /****************************************************************************************************/
23 /* */
24 /* MODULE: dbg_module.c */
25 /* PURPOSE: Handle the debug messages */
26 /* Note: This module is for LINUX compilation only! */
27 /* */
28 /****************************************************************************************************/
29
30
31 #include <stdarg.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <termios.h>
35 #include <fcntl.h>
36 #include <errno.h>
37 #include <signal.h>
38 #include <fcntl.h>
39 #include <stdarg.h>
40
41 #include <sys/time.h>
42 #include <sys/stat.h>
43 #include <sys/types.h>
44 #include <sys/ioctl.h>
45 #include <sys/socket.h>
46 #include <sys/resource.h>
47
48 #include "ipc.h"
49 #include "ticon.h"
50 #include "console.h"
51 #include "dbg_module.h"
52 #include "eth_utils.h"
53 #include "debug_module_ioctl.h"
54
55 /************/
56 /* Defines */
57 /**********/
58
59 #define DEBUG_MODULE_BUFFER_SIZE (255)
60
61 /*********************/
62 /* Global variables */
63 /*******************/
64
65 int debug_module_dev_file = -1;
66 int debug_module_process_pid = 0;
67
68 /********************************/
69 /* static functions prototypes */
70 /******************************/
71
72 unsigned char file_exists(const char *file_name);
73
74 /**************/
75 /* Functions */
76 /************/
77
78 /************************************************************************
79 * debug_module_init *
80 ************************************************************************
81 DESCRIPTION: Initialize the debug module user mode process
82
83 CONTEXT : main process only!
84 ************************************************************************/
debug_module_init(void)85 void debug_module_init(void)
86 {
87 int return_value;
88
89 /*********************************/
90 /* Create the debug device file */
91 /*******************************/
92
93 if (!file_exists("/dev/debug_msg_dev"))
94 {
95 /* Create the debug device file */
96 return_value = system("mknod /dev/debug_msg_dev c 254 0");
97 }
98
99 debug_module_dev_file = open("/dev/debug_msg_dev", O_RDWR);
100
101 if (debug_module_dev_file == -1)
102 {
103 console_printf_terminal("Debug module, error opening \"/dev/debug_msg_dev\"\n");
104
105 return;
106 }
107
108 /* Create another instance of this process */
109 debug_module_process_pid = fork();
110
111 if (debug_module_process_pid == 0)
112 {
113 /******************/
114 /* Child process */
115 /****************/
116
117 unsigned char bytes_read;
118 unsigned char ioctl_return_size;
119 t_ioctol1_format ioctl_data;
120 unsigned char in_debug_buffer[DEBUG_MODULE_BUFFER_SIZE + 1];
121
122 /* Set the priority of this process to the lowest */
123 /* setpriority(PRIO_PROCESS, getpid(), -20);*/
124
125 console_printf_terminal("Debug module, Hello from child process (pid = %d).\n", getpid());
126
127 /*******************************/
128 /* Prepare the ioctl's fields */
129 /*****************************/
130
131 ioctl_data.return_size = &ioctl_return_size;
132 ioctl_data.buffer = (in_debug_buffer + 1);
133 ioctl_data.buffer_size = DEBUG_MODULE_BUFFER_SIZE;
134
135 while (TRUE)
136 {
137 /*console_printf_terminal("Debug module, before ioctl\n");*/
138
139 /* Read data from device file - blocking command */
140 return_value = ioctl(debug_module_dev_file, 10, &ioctl_data);
141
142 /*console_printf_terminal("Debug module, after ioctl (%d)\n", return_value);*/
143
144 if (return_value != -1)
145 {
146 bytes_read = ioctl_return_size;
147
148 if (bytes_read > 0)
149 {
150 /* Mark that this is log message */
151 in_debug_buffer[0] = 0;
152
153 /* Put '0' in the end of the string */
154 in_debug_buffer[bytes_read + 1] = 0;
155
156 console_send_buffer_to_host(ETHERNET_UTILS_LOGGER_MODULE_ID, in_debug_buffer, (bytes_read + 1));
157 }
158 }
159 else
160 {
161 console_printf_terminal("Debug module, error reading from device file.\n");
162 }
163 }
164 }
165 }
166
167 /************************************************************************
168 * debug_module_deinit *
169 ************************************************************************
170 DESCRIPTION: Deinitialize the debug module user mode process
171
172 CONTEXT : main process only!
173 ************************************************************************/
debug_module_deinit(void)174 void debug_module_deinit(void)
175 {
176 if (debug_module_process_pid)
177 {
178 kill(debug_module_process_pid, SIGKILL);
179 }
180 }
181
182 /************************************************************************
183 * debug_module_get_queue_status *
184 ************************************************************************
185 DESCRIPTION: Gets the status of the debug module queue
186
187 CONTEXT : main process only!
188 ************************************************************************/
189
190 struct q_report {
191 unsigned int q_size;
192 unsigned int q_used;
193 unsigned int q_overrun;
194 };
195
196 #define QUEUE_STATUS_LEN (12)
debug_module_get_queue_status(void)197 void debug_module_get_queue_status(void)
198 {
199 int return_value;
200 char status_result[QUEUE_STATUS_LEN + 1];
201 struct q_report report;
202
203 /* Read data from device file - blocking command */
204 return_value = ioctl(debug_module_dev_file, 11, &report);
205
206 if (return_value != -1)
207 {
208 /* console_printf_terminal("Debug module, debug_module_get_queue_status. (size = %d, used = %d, overrun = %d)\n", report.q_size , report.q_used, report.q_overrun); */
209
210 memcpy(status_result + 1, &report, sizeof(report));
211
212 /* Mark that this is report message */
213 status_result[0] = 1;
214
215 console_send_buffer_to_host(ETHERNET_UTILS_LOGGER_MODULE_ID, (tiUINT8*) status_result, QUEUE_STATUS_LEN + 1);
216 }
217 else
218 {
219 console_printf_terminal("Debug module, error reading from device file.\n");
220 }
221 }
222
223 /************************************************************************
224 * file_exists *
225 ************************************************************************
226 DESCRIPTION: Check if a specific file exists
227
228 CONTEXT : All process.
229
230 Returns: TRUE if the file exists (FALSE otherwise).
231 ************************************************************************/
file_exists(const char * file_name)232 unsigned char file_exists(const char *file_name)
233 {
234 int test_file;
235 tiBOOL result = FALSE;;
236
237 /* Try to open the file */
238 test_file = open(file_name, O_RDONLY);
239
240 if (test_file != -1)
241 {
242 /*************************************************/
243 /* The file was successfullu opened - it exists */
244 /***********************************************/
245
246 close(test_file);
247
248 result = TRUE;
249 }
250
251 return result;
252 }
253
254