]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ipc/ipcdev.git/blobdiff - linux/src/utils/SocketFxns.c
Change error-related printf()s to fprintf(stderr)
[ipc/ipcdev.git] / linux / src / utils / SocketFxns.c
index d41637d0daa237efba32117b18ead48982121dc8..8284b07475c0020dee2500f68e8367be65e45e92 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2012-2014, Texas Instruments Incorporated
+ * Copyright (c) 2012-2015 Texas Instruments Incorporated - http://www.ti.com
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
 /* For MultiProc id to remoteproc index map */
 #include <_MultiProc.h>
 
-/* traces in this file are controlled via _SocketFxns_verbose */
+/*  Traces in this file are controlled via _SocketFxns_verbose
+ *
+ *  Caution! This file is used by both LAD and TransportRpmsg. The
+ *  PRINTVERBOSE macros use printf(), which sends its output to
+ *  STDOUT (fd = 0). However, LAD closes STDOUT and by unfortunate
+ *  chance, the file descriptor zero (fd = 0) is reused for the outbound
+ *  pipe to the first client. This means all trace messages go into
+ *  the pipe and corrupt the data on the client side.
+
+ *  If you want to enable this trace for LAD, in addition to setting
+ *  _SocketFxns_versbose to TRUE below, you must also comment out the
+ *  three calls to close() in linux/src/daemon/lad.c:
+ *
+ *      close(STDIN_FILENO);
+ *      close(STDOUT_FILENO);
+ *      close(STDERR_FILENO);
+ *
+ *  If you only want to enable trace in the client (i.e. calls from
+ *  TransportRpmsg), then simply set the environment variable IPC_DEBUG
+ *  (to either 2 or 3) before you run your program.
+ *
+ *  IPC_DEBUG=3 ./app_host
+ */
 Bool _SocketFxns_verbose = FALSE;
 #define verbose _SocketFxns_verbose
 
@@ -64,12 +86,16 @@ int ConnectSocket(int sock, UInt16 procId, int dst)
     int                   err;
     struct sockaddr_rpmsg srcAddr, dstAddr;
     socklen_t             len;
+    UInt16                clusterId;
+
+    /* map procId to clusterId (rprocList[] indexed by clusterId) */
+    clusterId = procId - _MultiProc_cfg.baseIdOfCluster;
 
     /* connect to remote service */
     memset(&dstAddr, 0, sizeof(dstAddr));
     dstAddr.family     = AF_RPMSG;
-    /* convert MultiProc 'procId' to remoteproc index */
-    dstAddr.vproc_id   = _MultiProc_cfg.rprocList[procId];
+    /* convert MultiProc 'clusterId' to remoteproc index */
+    dstAddr.vproc_id   = _MultiProc_cfg.rprocList[clusterId];
     dstAddr.addr       = dst;
 
     len = sizeof(struct sockaddr_rpmsg);
@@ -83,7 +109,8 @@ int ConnectSocket(int sock, UInt16 procId, int dst)
     /* let's see what local address we got */
     err = getsockname(sock, (struct sockaddr *)&srcAddr, &len);
     if (err < 0) {
-        printf("getpeername failed: %s (%d)\n", strerror(errno), errno);
+        fprintf(stderr, "getpeername failed: %s (%d)\n",
+                strerror(errno), errno);
         return (-1);
     }
 
@@ -95,17 +122,21 @@ int ConnectSocket(int sock, UInt16 procId, int dst)
     return(0);
 }
 
-int SocketBindAddr(int fd, UInt16 rprocId, UInt32 localAddr)
+int SocketBindAddr(int fd, UInt16 procId, UInt32 localAddr)
 {
     int         err;
     socklen_t    len;
     struct sockaddr_rpmsg srcAddr;
+    UInt16 clusterId;
+
+    /* map procId to clusterId (rprocList[] indexed by clusterId) */
+    clusterId = procId - _MultiProc_cfg.baseIdOfCluster;
 
     /* Now bind to the source address.   */
     memset(&srcAddr, 0, sizeof(srcAddr));
     srcAddr.family = AF_RPMSG;
-    /* We bind the remote proc ID, but local address! */
-    srcAddr.vproc_id   = _MultiProc_cfg.rprocList[rprocId];
+    /* We bind the remote clusterId, but local address! */
+    srcAddr.vproc_id   = _MultiProc_cfg.rprocList[clusterId];
     srcAddr.addr  = localAddr;
 
     len = sizeof(struct sockaddr_rpmsg);
@@ -118,7 +149,8 @@ int SocketBindAddr(int fd, UInt16 rprocId, UInt32 localAddr)
         /* let's see what local address we got */
         err = getsockname(fd, (struct sockaddr *)&srcAddr, &len);
         if (err < 0) {
-            printf("getsockname failed: %s (%d)\n", strerror(errno), errno);
+            fprintf(stderr, "getsockname failed: %s (%d)\n",
+                    strerror(errno), errno);
         }
         else {
             PRINTVERBOSE2("\tsrc vproc_id: %d, src addr: %d\n",