]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ipc/ipcdev.git/blob - linux/src/tests/ping_rpmsg.c
bc1962b0875f1885b064d3d9115843108b6be881
[ipc/ipcdev.git] / linux / src / tests / ping_rpmsg.c
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 CORE0 (0)  /* This should be MultiProc_getId("CORE0") - 1 */
54 #define NUMLOOPS 100
56 long diff(struct timespec start, struct timespec end)
57 {
58     struct timespec temp;
60     if ((end.tv_nsec - start.tv_nsec) < 0) {
61         temp.tv_sec = end.tv_sec - start.tv_sec-1;
62         temp.tv_nsec = 1000000000UL + end.tv_nsec - start.tv_nsec;
63     } else {
64         temp.tv_sec = end.tv_sec - start.tv_sec;
65         temp.tv_nsec = end.tv_nsec - start.tv_nsec;
66     }
68     return (temp.tv_sec * 1000000UL + temp.tv_nsec / 1000);
69 }
71 int main(void)
72 {
73     int sock, err;
74     struct sockaddr_rpmsg src_addr, dst_addr;
75     socklen_t len;
76     const char *msg = "Ping!";
77     char buf[512];
78     struct timespec   start,end;
79     long              elapsed=0,delta;
80     int i;
82     /* create an RPMSG socket */
83     /* QNX PORTING NOTE:  call fd = open("/dev/????", ...); */
84     sock = socket(AF_RPMSG, SOCK_SEQPACKET, 0);
85     if (sock < 0) {
86         printf("socket failed: %s (%d)\n", strerror(errno), errno);
87         return -1;
88     }
90     /* connect to remote service */
91     memset(&dst_addr, 0, sizeof(dst_addr));
92     dst_addr.family = AF_RPMSG;
93     dst_addr.vproc_id = CORE0;
94     dst_addr.addr = 51; // use 51 for ping_tasks;
95     //dst_addr.addr = 61; // use 61 for messageQ transport;
97     printf("Connecting to address 0x%x on vprocId %d\n",
98             dst_addr.addr, dst_addr.vproc_id);
100     len = sizeof(struct sockaddr_rpmsg);
101     /* QNX PORTING NOTE:  call ioctl(fd, CONNECT_IOTL, *args)*/
102     err = connect(sock, (struct sockaddr *)&dst_addr, len);
103     if (err < 0) {
104         printf("connect failed: %s (%d)\n", strerror(errno), errno);
105         return -1;
106     }
108     /* let's see what local address we got */
109     /* QNX PORTING NOTE:  call ioctl(fd, GETLOCALENDPOINT_IOTL, *args)*/
110     err = getsockname(sock, (struct sockaddr *)&src_addr, &len);
111     if (err < 0) {
112         printf("getpeername failed: %s (%d)\n", strerror(errno), errno);
113         return -1;
114     }
116     printf("Our address: socket family: %d, proc id = %d, addr = %d\n",
117                  src_addr.family, src_addr.vproc_id, src_addr.addr);
119     printf("Sending \"%s\" in a loop.\n", msg);
121     for (i = 0; i < NUMLOOPS; i++) {
122         clock_gettime(CLOCK_REALTIME, &start);
124         /* QNX PORTING NOTE:  call write(fd, msg,len); */
125         err = send(sock, msg, strlen(msg) + 1, 0);
126         if (err < 0) {
127             printf("sendto failed: %s (%d)\n", strerror(errno), errno);
128             return -1;
129         }
131         memset(&src_addr, 0, sizeof(src_addr));
133         len = sizeof(src_addr);
135         /* QNX PORTING NOTE:  len = read(fd, buf, len); */
136         err = recvfrom(sock, buf, sizeof(buf), 0,
137                        (struct sockaddr *)&src_addr, &len);
139         if (err < 0) {
140             printf("recvfrom failed: %s (%d)\n", strerror(errno), errno);
141             return -1;
142         }
143         if (len != sizeof(src_addr)) {
144             printf("recvfrom: got bad addr len (%d)\n", len);
145             return -1;
146         }
148         clock_gettime(CLOCK_REALTIME, &end);
149         delta = diff(start,end);
150         elapsed += delta;
152         printf("%d: Received msg: %s, from: %d\n", i, buf, src_addr.addr);
154         /*
155         printf ("Message time: %ld usecs\n", delta);
156         printf("Received a msg from address 0x%x on processor %d\n",
157                            src_addr.addr, src_addr.vproc_id);
158         printf("Message content: \"%s\".\n", buf);
159         */
160     }
161     printf ("Avg time: %ld usecs over %d iterations\n", elapsed / i, i);
163     /* QNX PORTING NOTE:  close(fd); */
164     close(sock);
166     return 0;