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: ipc.c */ 25 /* PURPOSE: Inter Process Communication utils */ 26 /* Note: This module is for LINUX compilation only! */ 27 /* */ 28 /****************************************************************************************************/ 29 30 #include <sys/mman.h> 31 #include <sys/ipc.h> 32 #include <errno.h> 33 #include <fcntl.h> 34 #include <signal.h> 35 #include <string.h> 36 #include <unistd.h> 37 38 #include "console.h" 39 #include "ticon.h" 40 #include "ipc.h" 41 42 /*********************/ 43 /* Global variables */ 44 /*******************/ 45 46 int ethernet_wipp_process_pid = 0; 47 int ethernet_g_tester_process_pid = 0; 48 int ethernet_logger_process_pid = 0; 49 50 51 int ethernet_wipp_control_pipe[2]; 52 int ethernet_g_tester_pipe[2]; 53 int ethernet_logger_pipe[2]; 54 55 int ipc_pipe[2]; 56 57 void *p_shared_memory; 58 59 60 /************************************************************************ 61 * ipc_initialize * 62 ************************************************************************ 63 DESCRIPTION: Initialize the IPC 64 65 CONTEXT: main process only! 66 ************************************************************************/ 67 int ipc_initialize() 68 { 69 /*****************************/ 70 /* Create IPC shared memory */ 71 /***************************/ 72 73 if ((p_shared_memory = mmap(0, SHARED_MEMORY_SIZE, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0)) == (void *)-1) 74 { 75 /* I should use the regular 'printf' function, because the 76 'console_printf_terminal' is not working w/o shared memory */ 77 printf("IPC Error, can't create shared memory mapping (%s)!\n", strerror(errno)); 78 79 return -1; 80 } 81 82 SHARED_MEMORY_TERMINAL_OUTPUT_PATH() = OUTPUT_PATH_SIMPLE_UART; 83 84 /************************/ 85 /* Create the IPC pipe */ 86 /**********************/ 87 88 if (pipe(ipc_pipe) < 0) 89 { 90 console_printf_terminal("IPC Error, can't create pipe\n"); 91 92 return -1; 93 } 94 95 /* Close the write direction of the pipe - because i only read information from this pipe. */ 96 /*close(ipc_pipe[1]);*/ 97 98 return 0; 99 } 100 101 /************************************************************************ 102 * ipc_deinitialize * 103 ************************************************************************ 104 DESCRIPTION: Deinitialize the IPC 105 106 CONTEXT: main process only! 107 ************************************************************************/ 108 void ipc_deinitialize() 109 { 110 /* Close the read direction of the pipe */ 111 close(ipc_pipe[0]); 112 } 113 114 /************************************************************************ 115 * ipc_send_command_to_main_process * 116 ************************************************************************ 117 DESCRIPTION: Handles the 'SIGUSR1' signal 118 119 CONTEXT: All child process - NOT FROM parent process!!!! 120 ************************************************************************/ 121 void ipc_send_command_to_main_process(int module_index, unsigned char *command, int size) 122 { 123 int pid = getpid(); 124 125 /*********************************************************************/ 126 /* Flow control */ 127 /* The pid of the caller process is inserted, so the main process */ 128 /* will signal it back and release the waiting condition */ 129 /*****************************************************************/ 130 131 command[0] = command[1] = 0xFF; 132 133 switch (module_index) 134 { 135 case ETHERNET_UTILS_G_TESTER_MODULE_ID: 136 command[0] = (pid & 0x00FF); 137 command[1] = ((pid & 0xFF00) >> 8); 138 command[2] = '-'; 139 break; 140 141 case ETHERNET_UTILS_WIPP_MODULE_ID: 142 case ETHERNET_UTILS_LOGGER_MODULE_ID: 143 command[0] = (pid & 0x00FF); 144 command[1] = ((pid & 0xFF00) >> 8); 145 command[2] = '+'; 146 break; 147 148 case GENERAL_PROCESS_MODULE_ID: 149 command[2] = '!'; 150 break; 151 } 152 153 /* Send the buffer to the main process */ 154 write(ipc_pipe[1], command, size); 155 156 /* Wait for 300usec (probably the signal will release us earlier) */ 157 usleep(300); 158 } 159 160