ef2908ac478bdde7d9c83169ab3170e6703d9fb8
1 /**************************************************************
2 * FILE PURPOSE : NETAPI Resource Manager Configuration
3 *
4 **************************************************************
5 * @file netapi_rm.c
6 *
7 * @brief DESCRIPTION: netapi resource manager file used for user space
8 * transport library
9 *
10 * REVISION HISTORY:
11 *
12 * Copyright (c) Texas Instruments Incorporated 2014
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 *
18 * Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 *
21 * Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the
24 * distribution.
25 *
26 * Neither the name of Texas Instruments Incorporated nor the names of
27 * its contributors may be used to endorse or promote products derived
28 * from this software without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
34 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
36 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
40 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 *****************************************************************************/
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <stdarg.h>
47 #include "ti/runtime/netapi/netapi.h"
50 uint32_t Osal_rm_MallocCounter = 0;
51 uint32_t Osal_rm_FreeCounter = 0;
54 /* Socket Includes */
55 #include "sockutils.h"
56 #include "sockrmmsg.h"
58 /* RM Includes */
59 #include <ti/drv/rm/rm.h>
60 #include <ti/drv/rm/rm_transport.h>
61 #include <ti/drv/rm/rm_services.h>
63 /* Socket timeout */
64 #define CLIENT_SOCK_TIMEOUT_USEC (500)
66 /* Application's registered RM transport indices */
67 #define SERVER_TO_CLIENT_MAP_ENTRY 0
68 /* Maximum number of registered RM transports */
69 #define MAX_MAPPING_ENTRIES 1
71 /* Error checking macro */
72 #define RM_ERROR_CHECK(checkVal, resultVal, rmInstName, printMsg) \
73 if (resultVal != checkVal) { \
74 char errorMsgToPrint[] = printMsg; \
75 printf("RM Inst : %s : ", rmInstName); \
76 printf("%s with error code : %d, exiting\n", errorMsgToPrint, resultVal); \
77 return(-1); \
78 }
80 /* RM registered transport mapping structure */
81 typedef struct trans_map_entry_s {
82 /* Registered RM transport handle */
83 Rm_TransportHandle transportHandle;
84 /* Remote socket tied to the transport handle */
85 sock_name_t *remote_sock;
86 } Transport_MapEntry;
92 /* RM Client Vars */
93 Rm_Handle rmClientHandle = NULL;
94 Rm_ServiceHandle *rmClientServiceHandle = NULL;
95 sock_h rmClientSocket;
97 /* Client instance name (must match with RM Global Resource List (GRL) and policies */
98 char rmClientName[RM_NAME_MAX_CHARS] = "RM_Client_NETAPI";
100 /* Client socket name */
101 char rmClientSockName[] = "/tmp/var/run/rm/rm_client_netapi";
103 /* Transport map stores the RM transport handle to IPC MessageQ queue mapping */
104 Transport_MapEntry rmTransportMap[MAX_MAPPING_ENTRIES];
107 hplib_spinLock_T net_test_rm_lock;
111 void *Osal_rmMalloc (uint32_t num_bytes)
112 {
113 /* Increment the allocation counter. */
114 Osal_rm_MallocCounter++;
116 /* Allocate memory. */
117 return calloc(1, num_bytes);
118 }
120 void Osal_rmFree (void *ptr, uint32_t size)
121 {
122 /* Increment the free counter. */
123 Osal_rm_FreeCounter++;
124 free(ptr);
125 }
127 void *Osal_rmCsEnter(void)
128 {
129 return NULL;
130 }
132 void Osal_rmCsExit(void *CsHandle)
133 {
135 }
137 void Osal_rmBeginMemAccess(void *ptr, uint32_t size)
138 {
139 return;
140 }
142 void Osal_rmEndMemAccess(void *ptr, uint32_t size)
143 {
144 return;
145 }
147 void *Osal_rmTaskBlockCreate(void)
148 {
149 return(NULL);
150 }
152 void Osal_rmTaskBlock(void *handle)
153 {
155 }
157 void Osal_rmTaskUnblock(void *handle)
158 {
160 }
162 void Osal_rmTaskBlockDelete(void *handle)
163 {
165 }
167 void Osal_rmLog (char *fmt, ... )
168 {
169 va_list ap;
171 va_start(ap, fmt);
172 vprintf(fmt, ap);
173 va_end(ap);
174 }
176 Rm_Packet *transportAlloc(Rm_AppTransportHandle appTransport, uint32_t pktSize, Rm_PacketHandle *pktHandle)
177 {
178 Rm_Packet *rm_pkt = NULL;
180 rm_pkt = calloc(1, sizeof(*rm_pkt));
181 if (!rm_pkt) {
182 printf("can't malloc for RM send message (err: %s)\n", strerror(errno));
183 return (NULL);
184 }
185 rm_pkt->pktLenBytes = pktSize;
186 *pktHandle = rm_pkt;
188 return(rm_pkt);
189 }
191 void transportFree (Rm_Packet *rm_pkt)
192 {
193 if (rm_pkt) {
194 free (rm_pkt);
195 }
196 }
198 void transportReceive (void)
199 {
200 int32_t rm_result;
201 int retval;
202 int length = 0;
203 sock_name_t server_sock_addr;
204 Rm_Packet *rm_pkt = NULL;
205 struct sockaddr_un server_addr;
207 retval = sock_wait(rmClientSocket, &length, NULL, -1);
208 if (retval == -2) {
209 /* Timeout */
210 printf("Error socket timeout\n");
211 return;
212 }
213 else if (retval < 0) {
214 printf("Error in reading from socket, error %d\n", retval);
215 return;
216 }
218 if (length < sizeof(*rm_pkt)) {
219 printf("invalid RM message length %d\n", length);
220 return;
221 }
222 rm_pkt = calloc(1, length);
223 if (!rm_pkt) {
224 printf("can't malloc for recv'd RM message (err: %s)\n",
225 strerror(errno));
226 return;
227 }
229 server_sock_addr.type = sock_addr_e;
230 server_sock_addr.s.addr = &server_addr;
231 retval = sock_recv(rmClientSocket, (char *)rm_pkt, length, &server_sock_addr);
232 if (retval != length) {
233 printf("recv RM pkt failed from socket, received = %d, expected = %d\n",
234 retval, length);
235 return;
236 }
239 /* Provide packet to RM Client for processing */
240 if ((rm_result = Rm_receivePacket(rmTransportMap[SERVER_TO_CLIENT_MAP_ENTRY].transportHandle, rm_pkt))) {
241 printf("RM failed to process received packet !!: %d\n", rm_result);
242 }
244 transportFree(rm_pkt);
245 }
247 int32_t transportSendRcv (Rm_AppTransportHandle appTransport, Rm_PacketHandle pktHandle)
248 {
249 sock_name_t *server_sock_name = (sock_name_t *)appTransport;
250 Rm_Packet *rm_pkt = (Rm_Packet *)pktHandle;
252 hplib_mSpinLockLock(&net_test_rm_lock);
253 if (sock_send(rmClientSocket, (char *)rm_pkt, (int) rm_pkt->pktLenBytes, server_sock_name))
254 {
255 hplib_mSpinLockUnlock(&net_test_rm_lock);
256 return (-1);
257 }
258 /* Wait for response from Server */
259 transportReceive();
260 hplib_mSpinLockUnlock(&net_test_rm_lock);
262 return (0);
263 }
265 int connection_setup(void)
266 {
267 Rm_TransportCfg rmTransCfg;
268 int32_t rm_result;
269 int i;
270 sock_name_t sock_name;
271 char server_sock_name[] = RM_SERVER_SOCKET_NAME;
273 /* Initialize the transport map */
274 for (i = 0; i < MAX_MAPPING_ENTRIES; i++) {
275 rmTransportMap[i].transportHandle = NULL;
276 }
278 sock_name.type = sock_name_e;
279 sock_name.s.name = rmClientSockName;
281 rmClientSocket = sock_open(&sock_name);
282 if (!rmClientSocket) {
283 printf("connection_setup: Client socket open failed\n");
284 return (-1);
285 }
287 rmTransportMap[SERVER_TO_CLIENT_MAP_ENTRY].remote_sock = calloc(1, sizeof(sock_name_t));
288 rmTransportMap[SERVER_TO_CLIENT_MAP_ENTRY].remote_sock->type = sock_name_e;
289 rmTransportMap[SERVER_TO_CLIENT_MAP_ENTRY].remote_sock->s.name = calloc(1, strlen(server_sock_name)+1);
290 strncpy(rmTransportMap[SERVER_TO_CLIENT_MAP_ENTRY].remote_sock->s.name, server_sock_name, strlen(server_sock_name)+1);
292 /* Register the Server with the Client instance */
293 rmTransCfg.rmHandle = rmClientHandle;
294 rmTransCfg.appTransportHandle = (Rm_AppTransportHandle) rmTransportMap[SERVER_TO_CLIENT_MAP_ENTRY].remote_sock;
295 rmTransCfg.remoteInstType = Rm_instType_SERVER;
296 rmTransCfg.transportCallouts.rmAllocPkt = transportAlloc;
297 rmTransCfg.transportCallouts.rmSendPkt = transportSendRcv;
298 rmTransportMap[SERVER_TO_CLIENT_MAP_ENTRY].transportHandle = Rm_transportRegister(&rmTransCfg, &rm_result);
299 printf("connection_setup: complete !!!!!!!!!!!!!!!!!!!!!!!!!!\n");
300 return(0);
301 }
303 /** ============================================================================
304 * @n@b initRm
305 *
306 * @b Description
307 * @n This API initializes the RM Client for the QMSS test establishing
308 * a socket connection with the RM Server
309 *
310 * @return int32_t
311 * -1 - Error
312 * 0 - Success
313 * =============================================================================
314 */
315 int initRm (void)
316 {
317 Rm_InitCfg rmInitCfg;
318 int32_t result;
321 //net_test_rm_lock = hplib_spinLock_UNLOCKED_INITIALIZER;
322 hplib_mSpinLockInit(&net_test_rm_lock );
324 /* Initialize the RM Client - RM must be initialized before anything else in the system */
325 memset(&rmInitCfg, 0, sizeof(rmInitCfg));
326 rmInitCfg.instName = rmClientName;
327 rmInitCfg.instType = Rm_instType_CLIENT;
328 rmClientHandle = Rm_init(&rmInitCfg, &result);
329 RM_ERROR_CHECK(RM_OK, result, rmClientName, "Initialization failed");
331 printf("\n\nInitialized %s\n\n", rmClientName);
333 /* Open Client service handle */
334 rmClientServiceHandle = Rm_serviceOpenHandle(rmClientHandle, &result);
335 RM_ERROR_CHECK(RM_OK, result, rmClientName, "Service handle open failed");
337 return(connection_setup());
338 }
340 int32_t closeRm(void)
341 {
342 int32_t result = 0;
344 if(rmClientHandle)
345 {
346 result = Rm_serviceCloseHandle(rmClientServiceHandle);
347 RM_ERROR_CHECK(RM_OK, result, rmClientName, "Service handle close failed");
349 result = Rm_transportUnregister(rmTransportMap[SERVER_TO_CLIENT_MAP_ENTRY].transportHandle);
350 RM_ERROR_CHECK(RM_OK, result, rmClientName, "Unregister of CD transport failed");
352 result = Rm_delete(rmClientHandle, 1);
353 RM_ERROR_CHECK(RM_OK, result, rmClientName, "Instance delete failed");
354 }
355 return (result);
356 }