b6e9e3837abfcbf780d1bfe4622f5d90e731de16
1 /*
2 * Copyright (c) 2012-2014, 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 unsigned int numLoops = NUM_LOOPS_DFLT;
73 short coreId = CORE_ID_DFLT;
74 int sock, err;
75 struct sockaddr_rpmsg src_addr, dst_addr;
76 socklen_t len;
77 const char *msg = "Ping!";
78 char buf[512];
79 struct timespec start,end;
80 long elapsed=0,delta;
81 int i;
83 /* Parse Args: */
84 switch (argc) {
85 case 1:
86 /* use defaults */
87 break;
88 case 2:
89 numLoops = atoi(argv[1]);
90 break;
91 case 3:
92 numLoops = atoi(argv[1]);
93 coreId = atoi(argv[2]);
94 break;
95 default:
96 printf("Usage: %s [<numLoops>] [<CoreId>]\n", argv[0]);
97 printf("\tDefaults: numLoops: %d; CoreId: %d\n",
98 NUM_LOOPS_DFLT, CORE_ID_DFLT);
99 exit(0);
100 }
102 /* create an RPMSG socket */
103 sock = socket(AF_RPMSG, SOCK_SEQPACKET, 0);
104 if (sock < 0) {
105 printf("socket failed: %s (%d)\n", strerror(errno), errno);
106 return -1;
107 }
109 /* connect to remote service */
110 memset(&dst_addr, 0, sizeof(dst_addr));
111 dst_addr.family = AF_RPMSG;
112 dst_addr.vproc_id = coreId;
113 dst_addr.addr = 51; // use 51 for ping_tasks;
114 //dst_addr.addr = 61; // use 61 for messageQ transport;
116 printf("Connecting to address 0x%x on vprocId %d\n",
117 dst_addr.addr, dst_addr.vproc_id);
119 len = sizeof(struct sockaddr_rpmsg);
120 err = connect(sock, (struct sockaddr *)&dst_addr, len);
121 if (err < 0) {
122 printf("connect failed: %s (%d)\n", strerror(errno), errno);
123 return -1;
124 }
126 /* let's see what local address we got */
127 err = getsockname(sock, (struct sockaddr *)&src_addr, &len);
128 if (err < 0) {
129 printf("getpeername failed: %s (%d)\n", strerror(errno), errno);
130 return -1;
131 }
133 printf("Our address: socket family: %d, proc id = %d, addr = %d\n",
134 src_addr.family, src_addr.vproc_id, src_addr.addr);
136 printf("Sending \"%s\" in a loop.\n", msg);
138 for (i = 0; i < numLoops; i++) {
139 clock_gettime(CLOCK_REALTIME, &start);
141 err = send(sock, msg, strlen(msg) + 1, 0);
142 if (err < 0) {
143 printf("sendto failed: %s (%d)\n", strerror(errno), errno);
144 return -1;
145 }
147 memset(&src_addr, 0, sizeof(src_addr));
149 len = sizeof(src_addr);
151 err = recvfrom(sock, buf, sizeof(buf), 0,
152 (struct sockaddr *)&src_addr, &len);
154 if (err < 0) {
155 printf("recvfrom failed: %s (%d)\n", strerror(errno), errno);
156 return -1;
157 }
158 if (len != sizeof(src_addr)) {
159 printf("recvfrom: got bad addr len (%d)\n", len);
160 return -1;
161 }
163 clock_gettime(CLOCK_REALTIME, &end);
164 delta = diff(start,end);
165 elapsed += delta;
167 printf("%d: Received msg: %s, from: %d\n", i, buf, src_addr.addr);
169 /*
170 printf ("Message time: %ld usecs\n", delta);
171 printf("Received a msg from address 0x%x on processor %d\n",
172 src_addr.addr, src_addr.vproc_id);
173 printf("Message content: \"%s\".\n", buf);
174 */
175 }
176 printf ("Avg time: %ld usecs over %d iterations\n", elapsed / i, i);
178 close(sock);
180 return 0;
181 }