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 * ======== ping_rpmsg.c ========
34 *
35 * Works with the ping_rpmsg BIOS sample over the rpmsg-proto socket.
36 */
38 #include <sys/select.h>
39 #include <sys/time.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <errno.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <time.h>
49 /* Ipc Socket Protocol Family */
50 #include <net/rpmsg.h>
52 #define NUM_LOOPS_DFLT 100
53 #define CORE_ID_DFLT 0 /* CORE ID should be (MultiProc ID - 1) */
55 long diff(struct timespec start, struct timespec end)
56 {
57 struct timespec temp;
59 if ((end.tv_nsec - start.tv_nsec) < 0) {
60 temp.tv_sec = end.tv_sec - start.tv_sec-1;
61 temp.tv_nsec = 1000000000UL + end.tv_nsec - start.tv_nsec;
62 } else {
63 temp.tv_sec = end.tv_sec - start.tv_sec;
64 temp.tv_nsec = end.tv_nsec - start.tv_nsec;
65 }
67 return (temp.tv_sec * 1000000UL + temp.tv_nsec / 1000);
68 }
70 int main (int argc, char ** argv)
71 {
72 int status = 0;
73 unsigned int numLoops = NUM_LOOPS_DFLT;
74 short coreId = CORE_ID_DFLT;
75 int sock, err;
76 struct sockaddr_rpmsg src_addr, dst_addr;
77 socklen_t len;
78 const char *msg = "Ping!";
79 char buf[512];
80 struct timespec start,end;
81 long elapsed=0,delta;
82 int i;
84 /* Parse Args: */
85 switch (argc) {
86 case 1:
87 /* use defaults */
88 break;
89 case 2:
90 numLoops = atoi(argv[1]);
91 break;
92 case 3:
93 numLoops = atoi(argv[1]);
94 coreId = atoi(argv[2]);
95 break;
96 default:
97 printf("Usage: %s [<numLoops>] [<CoreId>]\n", argv[0]);
98 printf("\tDefaults: numLoops: %d; CoreId: %d\n",
99 NUM_LOOPS_DFLT, CORE_ID_DFLT);
100 exit(0);
101 }
103 /* create an RPMSG socket */
104 sock = socket(AF_RPMSG, SOCK_SEQPACKET, 0);
105 if (sock < 0) {
106 printf("socket failed: %s (%d)\n", strerror(errno), errno);
107 return -1;
108 }
110 /* connect to remote service */
111 memset(&dst_addr, 0, sizeof(dst_addr));
112 dst_addr.family = AF_RPMSG;
113 dst_addr.vproc_id = coreId;
114 dst_addr.addr = 51; // use 51 for ping_tasks;
115 //dst_addr.addr = 61; // use 61 for messageQ transport;
117 printf("Connecting to address 0x%x on vprocId %d\n",
118 dst_addr.addr, dst_addr.vproc_id);
120 len = sizeof(struct sockaddr_rpmsg);
121 err = connect(sock, (struct sockaddr *)&dst_addr, len);
122 if (err < 0) {
123 printf("connect failed: %s (%d)\n", strerror(errno), errno);
124 return -1;
125 }
127 /* let's see what local address we got */
128 err = getsockname(sock, (struct sockaddr *)&src_addr, &len);
129 if (err < 0) {
130 printf("getpeername failed: %s (%d)\n", strerror(errno), errno);
131 return -1;
132 }
134 printf("Our address: socket family: %d, proc id = %d, addr = %d\n",
135 src_addr.family, src_addr.vproc_id, src_addr.addr);
137 printf("Sending \"%s\" in a loop.\n", msg);
139 for (i = 0; i < numLoops; i++) {
140 clock_gettime(CLOCK_REALTIME, &start);
142 err = send(sock, msg, strlen(msg) + 1, 0);
143 if (err < 0) {
144 printf("sendto failed: %s (%d)\n", strerror(errno), errno);
145 return -1;
146 }
148 memset(&src_addr, 0, sizeof(src_addr));
150 len = sizeof(src_addr);
152 err = recvfrom(sock, buf, sizeof(buf), 0,
153 (struct sockaddr *)&src_addr, &len);
155 if (err < 0) {
156 printf("recvfrom failed: %s (%d)\n", strerror(errno), errno);
157 return -1;
158 }
159 if (len != sizeof(src_addr)) {
160 printf("recvfrom: got bad addr len (%d)\n", len);
161 return -1;
162 }
164 clock_gettime(CLOCK_REALTIME, &end);
165 delta = diff(start,end);
166 elapsed += delta;
168 printf("%d: Received msg: %s, from: %d\n", i, buf, src_addr.addr);
170 /*
171 printf ("Message time: %ld usecs\n", delta);
172 printf("Received a msg from address 0x%x on processor %d\n",
173 src_addr.addr, src_addr.vproc_id);
174 printf("Message content: \"%s\".\n", buf);
175 */
176 }
177 printf ("Avg time: %ld usecs over %d iterations\n", elapsed / i, i);
179 close(sock);
181 return 0;
182 }