[ipc/ipcdev.git] / qnx / src / ipc3x_dev / ti / syslink / ipc / hlos / usr / Qnx / MessageQCopyDrv.c
1 /*
2 * @file MessageQCopyDrv.c
3 *
4 * @brief User-side OS-specific implementation of MessageQCopy driver for Qnx
5 *
6 *
7 * @ver 02.00.00.53_alpha2
8 *
9 * ============================================================================
10 *
11 * Copyright (c) 2011-2012, Texas Instruments Incorporated
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * * Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * * Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * * Neither the name of Texas Instruments Incorporated nor the names of
25 * its contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
30 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
32 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
35 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
36 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
37 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
38 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * Contact information for paper mail:
40 * Texas Instruments
41 * Post Office Box 655303
42 * Dallas, Texas 75265
43 * Contact information:
44 * http://www-k.ext.ti.com/sc/technical-support/product-information-centers.htm?
45 * DCMP=TIHomeTracking&HQS=Other+OT+home_d_contact
46 * ============================================================================
47 *
48 */
51 /* Linux specific header files */
52 #include <signal.h>
53 #include <sys/types.h>
54 #include <sys/ioctl.h>
55 #include <fcntl.h>
56 #include <stdio.h>
57 #include <unistd.h>
58 #include <errno.h>
59 #include <pthread.h>
60 #include <string.h>
62 /* Standard headers */
63 #include <ti/syslink/Std.h>
65 /* OSAL & Utils headers */
66 #include <ti/syslink/utils/Trace.h>
67 #include <MessageQCopyDrv.h>
69 /* Module headers */
70 #include <MessageQCopyDrvDefs.h>
73 #if defined (__cplusplus)
74 extern "C" {
75 #endif /* defined (__cplusplus) */
78 /** ============================================================================
79 * Macros and types
80 * ============================================================================
81 */
82 /*!
83 * @brief Driver name for MessageQCopy.
84 */
85 #define MESSAGEQCOPY_DRIVER_NAME "/dev/ipc"
88 /** ============================================================================
89 * Globals
90 * ============================================================================
91 */
92 /*!
93 * @brief Driver handle for MessageQCopy in this process.
94 */
95 static Int32 MessageQCopyDrv_handle = -1;
97 /*!
98 * @brief Reference count for the driver handle.
99 */
100 static UInt32 MessageQCopyDrv_refCount = 0;
102 /*!
103 * @brief Indicates whether MessageQCopyDrv has been setup in this process.
104 */
105 static UInt32 MessageQCopyDrv_setup = 0;
108 /** ============================================================================
109 * Functions
110 * ============================================================================
111 */
112 /*!
113 * @brief Function to open the MessageQCopy driver.
114 *
115 * @param None.
116 *
117 * @sa MessageQCopyDrv_close
118 */
119 Int
120 MessageQCopyDrv_open (Void)
121 {
122 Int status = MessageQCopy_S_SUCCESS;
123 int osStatus = 0;
124 Bool isForked = FALSE;
126 GT_0trace (curTrace, GT_ENTER, "MessageQCopyDrv_open");
128 if (MessageQCopyDrv_refCount == 0) {
129 /* TBD: Protection for refCount. */
130 MessageQCopyDrv_refCount++;
132 MessageQCopyDrv_handle = open (MESSAGEQCOPY_DRIVER_NAME, O_SYNC | O_RDWR);
133 if (MessageQCopyDrv_handle < 0) {
134 perror ("MessageQCopy driver open: " MESSAGEQCOPY_DRIVER_NAME);
135 /*! @retval MessageQCopy_E_OSFAILURE Failed to open MessageQCopy
136 driver with OS */
137 status = MessageQCopy_E_OSFAILURE;
138 GT_setFailureReason (curTrace,
139 GT_4CLASS,
140 "MessageQCopyDrv_open",
141 status,
142 "Failed to open MessageQCopy driver with OS!");
143 }
144 else {
145 osStatus = fcntl (MessageQCopyDrv_handle, F_SETFD, FD_CLOEXEC);
146 if (osStatus != 0) {
147 /*! @retval MessaegQCopy_E_OSFAILURE Failed to set file descriptor
148 flags */
149 status = MessageQCopy_E_OSFAILURE;
150 GT_setFailureReason (curTrace,
151 GT_4CLASS,
152 "MessageQCopyDrv_open",
153 status,
154 "Failed to set file descriptor flags!");
155 }
156 }
157 }
158 else {
159 if (MessageQCopyDrv_setup != getpid ()) {
160 /* Indicates that this is a forked process - Ang - need to check this? */
161 MessageQCopyDrv_setup = getpid ();
162 isForked = TRUE;
163 }
164 else {
165 /* TBD: Protection for refCount. */
166 MessageQCopyDrv_refCount++;
167 }
168 }
170 GT_1trace (curTrace, GT_LEAVE, "MessageQCopyDrv_open", status);
172 /*! @retval MessageQCopy_S_SUCCESS Operation successfully completed. */
173 return status;
174 }
177 /*!
178 * @brief Function to close the MessageQCopy driver.
179 *
180 * @param None.
181 *
182 * @sa MessageQCopyDrv_open
183 */
184 Int
185 MessageQCopyDrv_close (Void)
186 {
187 Int status = MessageQCopy_S_SUCCESS;
188 int osStatus = 0;
190 GT_0trace (curTrace, GT_ENTER, "MessageQCopyDrv_close");
191 /* TBD: Protection for refCount. */
192 if (MessageQCopyDrv_refCount == 1) {
193 MessageQCopyDrv_refCount--;
195 osStatus = close (MessageQCopyDrv_handle);
196 if (osStatus != 0) {
197 perror ("MessageQCopy driver close: " MESSAGEQCOPY_DRIVER_NAME);
198 /*! @retval MessageQCopy_E_OSFAILURE Failed to open MessageQCopy
199 driver with OS */
200 status = MessageQCopy_E_OSFAILURE;
201 GT_setFailureReason (curTrace,
202 GT_4CLASS,
203 "MessageQCopyDrv_close",
204 status,
205 "Failed to close MessageQCopy driver with OS!");
206 }
207 else {
208 MessageQCopyDrv_handle = -1;
209 }
210 }
211 else {
212 MessageQCopyDrv_refCount--;
213 }
215 GT_1trace (curTrace, GT_LEAVE, "MessageQCopyDrv_close", status);
217 /*! @retval MessageQCopy_S_SUCCESS Operation successfully completed. */
218 return status;
219 }
222 /*!
223 * @brief Function to invoke the APIs through ioctl.
224 *
225 * @param cmd Command for driver ioctl
226 * @param args Arguments for the ioctl command
227 *
228 * @sa
229 */
230 Int
231 MessageQCopyDrv_ioctl (UInt32 cmd, Ptr args)
232 {
233 Int status = MessageQCopy_S_SUCCESS;
234 int osStatus = 0;
236 GT_2trace (curTrace, GT_ENTER, "MessageQCopyDrv_ioctl", cmd, args);
238 GT_assert (curTrace, (MessageQCopyDrv_refCount > 0));
240 osStatus = ioctl (MessageQCopyDrv_handle, cmd, args);
241 if (osStatus < 0) {
242 /*! @retval MessageQCopy_E_OSFAILURE Driver ioctl failed */
243 status = MessageQCopy_E_OSFAILURE;
244 GT_setFailureReason (curTrace,
245 GT_4CLASS,
246 "MessageQCopyDrv_ioctl",
247 status,
248 "Driver ioctl failed!");
249 }
250 else {
251 /* First field in the structure is the API status. */
252 status = ((MessageQCopyDrv_CmdArgs *) args)->apiStatus;
253 }
255 GT_1trace (curTrace, GT_LEAVE, "MessageQCopyDrv_ioctl", status);
257 /*! @retval MessageQCopy_S_SUCCESS Operation successfully completed. */
258 return status;
259 }
262 #if defined (__cplusplus)
263 }
264 #endif /* defined (__cplusplus) */