PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/net/wireless/tiwlan1251/CUDK/CLI/eth_utils.c

https://bitbucket.org/cyanogenmod/cm-kernel
C | 552 lines | 256 code | 122 blank | 174 comment | 36 complexity | 0fdd909f0fbc1b262733ebc74ee9e884 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  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. /* MODULE: eth_utils.c */
  23. /* PURPOSE: Ethernet communication utilities */
  24. /* */
  25. /****************************************************************************************************/
  26. #include <sys/types.h>
  27. #include <sys/socket.h>
  28. #include <sys/select.h>
  29. #include <unistd.h>
  30. #include <arpa/inet.h>
  31. #include "ipc.h"
  32. #include "ticon.h"
  33. #include "errno.h"
  34. #include "signal.h"
  35. #include "eth_utils.h"
  36. extern char* inet_ntoa(struct in_addr);
  37. /************/
  38. /* Defines */
  39. /**********/
  40. #define LOOGER_ETHERNET_PORT (700)
  41. #define WIPP_ETHERNET_PORT (701)
  42. #define G_TESTER_ETHERNET_PORT (702)
  43. #define READ_STATE_GET_HEADER (0x00)
  44. #define READ_STATE_GET_LENGTH1 (0x01)
  45. #define READ_STATE_GET_LENGTH2 (0x02)
  46. #define READ_STATE_GET_PAYLOAD (0x03)
  47. #define PACKET_PREFIX (0xFF)
  48. #define IN_BUFFER_SIZE (1024)
  49. #define OUT_BUFFER_SIZE (512)
  50. /*********************/
  51. /* Global variables */
  52. /*******************/
  53. unsigned char ethernet_utils_welcome_message[] = {'W', 2, 0, 2, ETH_UTILS_PROTOCOL_VERSION};
  54. char ethernet_utils_module_names[ETHERNET_UTILS_NUMBER_OF_MODULES][20] =
  55. {
  56. "terminal",
  57. "logger",
  58. "wipp control",
  59. "g tester"
  60. };
  61. /********************************/
  62. /* static functions prototypes */
  63. /******************************/
  64. int ethernet_utils_init_module(int module_index, u_short port_number, int *module_pipe, unsigned char mux_uart_id);
  65. void ethernet_utils_signal_handler(int signal);
  66. int ethernet_utils_process_in_command(int module_index, unsigned char *read_state, unsigned short *packet_size, unsigned char *in_buffer, unsigned int *in_buffer_offset);
  67. int ethernet_utils_write_socket(int module_index, int length, unsigned char *buffer);
  68. void ethernet_utils_disconnect_socket(int module_index);
  69. /**************/
  70. /* Functions */
  71. /************/
  72. /************************************************************************
  73. * ethernet_utils_init *
  74. ************************************************************************
  75. DESCRIPTION: Initialize the ethernet communication
  76. CONTEXT : main process only!
  77. ************************************************************************/
  78. void ethernet_utils_init()
  79. {
  80. ethernet_logger_process_pid = ethernet_utils_init_module(ETHERNET_UTILS_LOGGER_MODULE_ID, LOOGER_ETHERNET_PORT, ethernet_logger_pipe, LOGGER_MUX_UART_ID);
  81. ethernet_wipp_process_pid = ethernet_utils_init_module(ETHERNET_UTILS_WIPP_MODULE_ID, WIPP_ETHERNET_PORT, ethernet_wipp_control_pipe, WIPP_CONTROL_MUX_UART_ID);
  82. ethernet_g_tester_process_pid = ethernet_utils_init_module(ETHERNET_UTILS_G_TESTER_MODULE_ID, G_TESTER_ETHERNET_PORT, ethernet_g_tester_pipe, G_TESTER_MUX_UART_ID);
  83. }
  84. /************************************************************************
  85. * ethernet_utils_deinit *
  86. ************************************************************************
  87. DESCRIPTION: Deinitialize the ethernet communication
  88. CONTEXT : main process only!
  89. ************************************************************************/
  90. void ethernet_utils_deinit()
  91. {
  92. /* Kill the logger process */
  93. if (ethernet_logger_process_pid > 0)
  94. {
  95. kill(ethernet_logger_process_pid, SIGKILL);
  96. }
  97. /* Kill the wipp control process */
  98. if (ethernet_wipp_process_pid > 0)
  99. {
  100. kill(ethernet_wipp_process_pid, SIGKILL);
  101. }
  102. /* Kill the wipp control process */
  103. if (ethernet_g_tester_process_pid > 0)
  104. {
  105. kill(ethernet_g_tester_process_pid, SIGKILL);
  106. }
  107. }
  108. /************************************************************************
  109. * ethernet_utils_signal_handler *
  110. ************************************************************************
  111. DESCRIPTION: Signal handler - receive the USER
  112. CONTEXT : Signal owner
  113. ************************************************************************/
  114. void ethernet_utils_signal_handler(int signal)
  115. {
  116. }
  117. /************************************************************************
  118. * ethernet_utils_init_module *
  119. ************************************************************************
  120. DESCRIPTION: Initialize ethernet communication
  121. RETURNS: : Process ID of the new process or -1 if error.
  122. CONTEXT : main process only!
  123. ************************************************************************/
  124. int ethernet_utils_init_module(int module_index, u_short port_number, int *module_pipe, unsigned char mux_uart_id)
  125. {
  126. int child_process_id;
  127. /***************************************/
  128. /* Create a pipe to control the child */
  129. /*************************************/
  130. if (pipe(module_pipe) < 0)
  131. {
  132. console_printf_terminal("eth_utils, error creating pipe\n");
  133. return -1;
  134. }
  135. /* Set the shared memory variables */
  136. SHARED_MEMORY_IPC_PIPE(module_index) = module_pipe[1];
  137. SHARED_MEMORY_MUX_UART_ID(module_index) = mux_uart_id;
  138. /* Create a child process */
  139. child_process_id = fork();
  140. if (0 == child_process_id)
  141. {
  142. /******************/
  143. /* Child process */
  144. /****************/
  145. int result;
  146. int socket_id;
  147. int optval = 1;
  148. int socket_alive;
  149. int max_fd_index;
  150. socklen_t client_addr_len;
  151. fd_set read_set;
  152. unsigned char out_buffer[OUT_BUFFER_SIZE];
  153. unsigned char in_buffer[IN_BUFFER_SIZE];
  154. unsigned char read_state;
  155. unsigned short packet_size;
  156. unsigned int in_buffer_offset;
  157. struct sockaddr_in server_addr;
  158. struct sockaddr_in client_addr;
  159. console_printf_terminal("eth_utils, Hello from %s child module (pid = %d).\n", ethernet_utils_module_names[module_index], getpid());
  160. /* Close the write direction of the pipe - because i only read information from this pipe. */
  161. close(module_pipe[1]);
  162. SHARED_MEMORY_OUTPUT_PATH(module_index) = OUTPUT_PATH_SIMPLE_UART;
  163. /* Set the signal handler for the 'SIGUSR1' signal */
  164. signal(SIGUSR1, ethernet_utils_signal_handler);
  165. while (TRUE)
  166. {
  167. /******************/
  168. /* Open a socket */
  169. /****************/
  170. socket_id = socket(PF_INET, SOCK_STREAM, 0);
  171. if (!socket_id)
  172. {
  173. /* Error opening socket */
  174. console_printf_terminal("eth_utils, error opening %s socket. (errno = %d)\n", ethernet_utils_module_names[module_index], errno);
  175. _exit(1);
  176. }
  177. /*************************/
  178. /* Configure the socket */
  179. /***********************/
  180. if (setsockopt(socket_id, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)) == -1)
  181. {
  182. /* Error setting socket option */
  183. console_printf_terminal("eth_utils, error setting %s socket option. (errno = %d)\n", ethernet_utils_module_names[module_index], errno);
  184. _exit(1);
  185. }
  186. /********************/
  187. /* Bind the socket */
  188. /******************/
  189. server_addr.sin_family = AF_INET;
  190. server_addr.sin_addr.s_addr = htonl (INADDR_ANY);
  191. server_addr.sin_port = htons(port_number);
  192. result = bind(socket_id, (struct sockaddr *)&server_addr, sizeof(server_addr));
  193. if (result != 0)
  194. {
  195. /* Error binding socket */
  196. console_printf_terminal("eth_utils, error binding %s socket. (errno = %d)\n", ethernet_utils_module_names[module_index], errno);
  197. _exit(1);
  198. }
  199. /****************************/
  200. /* Listening to the socket */
  201. /**************************/
  202. result = listen(socket_id, 5);
  203. if (-1 == result)
  204. {
  205. /* Error listening to socket */
  206. console_printf_terminal("eth_utils, error listening to %s socket. (errno = %d)\n", ethernet_utils_module_names[module_index], errno);
  207. _exit(1);
  208. }
  209. /**********************/
  210. /* Accept connection */
  211. /********************/
  212. client_addr_len = sizeof(client_addr);
  213. /* We suppose to get new socket id after accept (blocking action) */
  214. result = accept(socket_id, (struct sockaddr *)&client_addr, &client_addr_len);
  215. if (-1 == result)
  216. {
  217. /* Error accepting connection */
  218. console_printf_terminal("eth_utils, error accepting %s connection. (errno = %d)\n", ethernet_utils_module_names[module_index], errno);
  219. _exit(1);
  220. }
  221. close(socket_id);
  222. socket_id = result;
  223. read_state = READ_STATE_GET_HEADER;
  224. SHARED_MEMORY_ETHERNET_SOCKET_ID(module_index) = socket_id;
  225. console_printf_terminal("\n***********************************************\n");
  226. console_printf_terminal("* eth_utils, %s connection accepted. \n", ethernet_utils_module_names[module_index]);
  227. console_printf_terminal("*\n* Remote IP: %s\n***********************************************\n", inet_ntoa(client_addr.sin_addr));
  228. /* Switch to ethernet output */
  229. SHARED_MEMORY_OUTPUT_PATH(module_index) = OUTPUT_PATH_ETHERNET;
  230. /*********************************/
  231. /* Send Hello message to client */
  232. /*******************************/
  233. ethernet_utils_write_socket(module_index, sizeof(ethernet_utils_welcome_message), ethernet_utils_welcome_message);
  234. /**********************/
  235. /* Manage connection */
  236. /********************/
  237. socket_alive = TRUE;
  238. while (socket_alive)
  239. {
  240. /***********************************************************************************/
  241. /* Wait for one of two external events: */
  242. /* ----------------------------------- */
  243. /* */
  244. /* 1. Data received from TCP client and should be transfered to parent process */
  245. /* 2. Data received from parent process and should be transfered to client */
  246. /*****************************************************************************/
  247. /* Prepare the read set fields */
  248. FD_ZERO(&read_set);
  249. FD_SET(socket_id, &read_set);
  250. FD_SET(module_pipe[0], &read_set);
  251. /* Determine the maximum index of the file descriptor */
  252. max_fd_index = (max(socket_id, module_pipe[0]) + 1);
  253. /* Wait for event - blocking */
  254. result = select(max_fd_index, &read_set, NULL, NULL, NULL);
  255. /************************/
  256. /* Check event results */
  257. /**********************/
  258. if (result > 0)
  259. {
  260. if (FD_ISSET(socket_id, &read_set))
  261. {
  262. /******************************/
  263. /* Data received from client */
  264. /****************************/
  265. /* Process the input command */
  266. if (ethernet_utils_process_in_command(module_index, &read_state, &packet_size, in_buffer, &in_buffer_offset) <= 0)
  267. {
  268. socket_alive = FALSE;
  269. }
  270. }
  271. if (FD_ISSET(module_pipe[0], &read_set))
  272. {
  273. /**************************************/
  274. /* Data received from parent process */
  275. /************************************/
  276. result = read(module_pipe[0], out_buffer, sizeof(out_buffer));
  277. if( result < 0 )
  278. {
  279. console_printf_terminal("eth_utils, read error (err=%d)\n", result);
  280. socket_alive = FALSE;
  281. }
  282. if (ethernet_utils_write_socket(module_index, result, out_buffer) == -1)
  283. {
  284. socket_alive = FALSE;
  285. }
  286. }
  287. }
  288. else
  289. {
  290. console_printf_terminal("eth_utils, 'select' command error\n");
  291. socket_alive = FALSE;
  292. }
  293. }
  294. /* Disconnect the socket */
  295. ethernet_utils_disconnect_socket(module_index);
  296. }
  297. }
  298. /* Close the read direction of the pipe - because i only write information from this pipe. (to child process) */
  299. close(module_pipe[0]);
  300. /* return the process id, I will use it later when i want to kill this process */
  301. return child_process_id;
  302. }
  303. /************************************************************************
  304. * ethernet_utils_process_in_command *
  305. ************************************************************************
  306. DESCRIPTION: Handle In commands
  307. CONTEXT : Only the same process that is reading from the socket
  308. ************************************************************************/
  309. int ethernet_utils_process_in_command(int module_index, unsigned char *read_state, unsigned short *packet_size, unsigned char *in_buffer, unsigned int *in_buffer_offset)
  310. {
  311. unsigned char prefix;
  312. int socket_id = SHARED_MEMORY_ETHERNET_SOCKET_ID(module_index);
  313. int result = 1;
  314. /*console_printf_terminal("ethernet_utils_process_in_command (socket = %d)\n", socket_id); */
  315. /* Continue while there is data in the RX buffer */
  316. switch (*read_state)
  317. {
  318. case READ_STATE_GET_HEADER:
  319. /* Read the packet prefix - one byte */
  320. result = read(socket_id, &prefix, sizeof(prefix));
  321. /*console_printf_terminal("ethernet_utils_process_in_command (State = READ_STATE_GET_HEADER, length = %d)\n", result); */
  322. if (result > 0)
  323. {
  324. if (prefix == PACKET_PREFIX)
  325. {
  326. *read_state = READ_STATE_GET_LENGTH1;
  327. }
  328. else
  329. {
  330. console_printf_terminal("ethernet_utils_process_in_command, Error: protocol sync error! \n");
  331. result = -1;
  332. }
  333. }
  334. break;
  335. case READ_STATE_GET_LENGTH1:
  336. /* Read the packet size first byte */
  337. result = read(socket_id, (void *)((unsigned char *)(packet_size) + 0), sizeof(unsigned char));
  338. /*console_printf_terminal("ethernet_utils_process_in_command (State = READ_STATE_GET_LENGTH1, length = %d)\n", result); */
  339. if (result > 0)
  340. {
  341. *read_state = READ_STATE_GET_LENGTH2;
  342. }
  343. break;
  344. case READ_STATE_GET_LENGTH2:
  345. /* Read the packet size second byte */
  346. result = read(socket_id, (void *)((unsigned char *)(packet_size) + 1), sizeof(unsigned char));
  347. /*console_printf_terminal("ethernet_utils_process_in_command (State = READ_STATE_GET_LENGTH2, length = %d, packet_size = %d)\n", result, *packet_size); */
  348. if (result > 0)
  349. {
  350. /* Sanity check on the length */
  351. *in_buffer_offset = 0;
  352. *read_state = READ_STATE_GET_PAYLOAD;
  353. }
  354. break;
  355. case READ_STATE_GET_PAYLOAD:
  356. /* Read the packet size second byte */
  357. result = read(socket_id, (in_buffer + 3 + *in_buffer_offset), (*packet_size - *in_buffer_offset));
  358. /*console_printf_terminal("ethernet_utils_process_in_command (State = READ_STATE_GET_PAYLOAD, offset = %d, length = %d)\n", *in_buffer_offset, result); */
  359. if (result > 0)
  360. {
  361. *in_buffer_offset += result;
  362. if (*packet_size == *in_buffer_offset)
  363. {
  364. /* All the packet has arrived */
  365. *read_state = READ_STATE_GET_HEADER;
  366. /* Send it to the main process */
  367. ipc_send_command_to_main_process(module_index, in_buffer, (*packet_size + 3));
  368. }
  369. }
  370. break;
  371. }
  372. return result;
  373. }
  374. /************************************************************************
  375. * ethernet_utils_write_socket *
  376. ************************************************************************
  377. DESCRIPTION: write data to socket
  378. CONTEXT : Only the same process that is writing to the socket
  379. ************************************************************************/
  380. int ethernet_utils_write_socket(int module_index, int length, unsigned char *buffer)
  381. {
  382. int result;
  383. /* console_printf_terminal("eth_utils, ethernet_utils_wipp_write() (length = %d).\n", length); */
  384. /* Write to the socket */
  385. result = write(SHARED_MEMORY_ETHERNET_SOCKET_ID(module_index), buffer, length);
  386. /* console_printf_terminal("eth_utils, ethernet_utils_wipp_write() (result = %d).\n", result); */
  387. if (result != length)
  388. {
  389. /**************************/
  390. /* Error writing to port */
  391. /************************/
  392. console_printf_terminal("eth_utils, Error writing to %s socket (result = %d, errno = %d, error = %s)\n", ethernet_utils_module_names[module_index], result, errno, strerror(errno));
  393. result = -1;
  394. }
  395. return result;
  396. }
  397. /************************************************************************
  398. * ethernet_utils_disconnect_socket *
  399. ************************************************************************
  400. DESCRIPTION: Disconnect a socket
  401. CONTEXT : Only the process that is disconnecting
  402. ************************************************************************/
  403. void ethernet_utils_disconnect_socket(int module_index)
  404. {
  405. char temp_buf[6] = {'x','x','x', '5', '0', 0};
  406. /* Switch to UART output */
  407. SHARED_MEMORY_SWITCH_TO_UART_OUTPUT(module_index);
  408. console_printf_terminal("eth_utils, disconnecting from %s socket.\n", ethernet_utils_module_names[module_index]);
  409. switch (module_index)
  410. {
  411. case ETHERNET_UTILS_LOGGER_MODULE_ID:
  412. /* Set debug path back to UART */
  413. ipc_send_command_to_main_process(ETHERNET_UTILS_LOGGER_MODULE_ID, (unsigned char*)temp_buf, 5);
  414. break;
  415. case ETHERNET_UTILS_WIPP_MODULE_ID:
  416. temp_buf[3] = WIPP_CONTROL_FROM_GENERAL_PROCESS_DEACTIVATE_IPERF;
  417. ipc_send_command_to_main_process(GENERAL_PROCESS_MODULE_ID,(unsigned char*) temp_buf, 5);
  418. break;
  419. }
  420. /* Close the socket */
  421. close(SHARED_MEMORY_ETHERNET_SOCKET_ID(module_index));
  422. }