1 /*
2 * Copyright (c) 2012-2013, Texas Instruments Incorporated
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of Texas Instruments Incorporated nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 /*
33 * @file SocketFxns.c
34 *
35 * @brief Shared socket functions.
36 */
38 /* Standard headers */
39 #include <ti/ipc/Std.h>
41 /* Socket Headers */
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <errno.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <stdlib.h>
49 /* Socket Protocol Family */
50 #include <net/rpmsg.h>
52 /* For PRINTVERBOSE* */
53 #include <_lad.h>
55 /* For MultiProc id to remoteproc index map */
56 #include <_MultiProc.h>
58 static Bool verbose = FALSE;
60 int ConnectSocket(int sock, UInt16 procId, int dst)
61 {
62 int err;
63 struct sockaddr_rpmsg srcAddr, dstAddr;
64 socklen_t len;
66 /* connect to remote service */
67 memset(&dstAddr, 0, sizeof(dstAddr));
68 dstAddr.family = AF_RPMSG;
69 /* convert MultiProc 'procId' to remoteproc index */
70 dstAddr.vproc_id = _MultiProc_cfg.rprocList[procId];
71 dstAddr.addr = dst;
73 len = sizeof(struct sockaddr_rpmsg);
74 err = connect(sock, (struct sockaddr *)&dstAddr, len);
75 if (err < 0) {
76 /* don't hard-printf since this is no longer fatal */
77 PRINTVERBOSE2("connect failed: %s (%d)\n", strerror(errno), errno);
78 return (-1);
79 }
81 /* let's see what local address we got */
82 err = getsockname(sock, (struct sockaddr *)&srcAddr, &len);
83 if (err < 0) {
84 printf("getpeername failed: %s (%d)\n", strerror(errno), errno);
85 return (-1);
86 }
88 PRINTVERBOSE3("Connected over sock: %d\n\tdst vproc_id: %d, dst addr: %d\n",
89 sock, dstAddr.vproc_id, dstAddr.addr)
90 PRINTVERBOSE2("\tsrc vproc_id: %d, src addr: %d\n",
91 srcAddr.vproc_id, srcAddr.addr)
93 return(0);
94 }
96 int SocketBindAddr(int fd, UInt16 rprocId, UInt32 localAddr)
97 {
98 int err;
99 socklen_t len;
100 struct sockaddr_rpmsg srcAddr;
102 /* Now bind to the source address. */
103 memset(&srcAddr, 0, sizeof(srcAddr));
104 srcAddr.family = AF_RPMSG;
105 /* We bind the remote proc ID, but local address! */
106 srcAddr.vproc_id = _MultiProc_cfg.rprocList[rprocId];
107 srcAddr.addr = localAddr;
109 len = sizeof(struct sockaddr_rpmsg);
110 err = bind(fd, (struct sockaddr *)&srcAddr, len);
111 if (err >= 0) {
112 PRINTVERBOSE3("socket_bind_addr: bound sock: %d\n\tto dst "
113 "vproc_id: %d, src addr: %d\n",
114 fd, srcAddr.vproc_id, srcAddr.addr)
116 /* let's see what local address we got */
117 err = getsockname(fd, (struct sockaddr *)&srcAddr, &len);
118 if (err < 0) {
119 printf("getsockname failed: %s (%d)\n", strerror(errno), errno);
120 }
121 else {
122 PRINTVERBOSE2("\tsrc vproc_id: %d, src addr: %d\n",
123 srcAddr.vproc_id, srcAddr.addr)
124 }
125 }
127 return (err);
128 }