001c6606016f600803179437fc2c92866ba1af51
[processor-sdk/open-amp.git] / apps / rpc_demo / rpc_demo.c
1 /* This is a sample demonstration application that showcases usage of proxy from the remote core. 
2  This application is meant to run on the remote CPU running baremetal.
3  This applicationr can print to to master console and perform file I/O using proxy mechanism. */
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdlib.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include "openamp/open_amp.h"
11 #include "rsc_table.h"
12 #include "platform_info.h"
13 #include "openamp/rpmsg_retarget.h"
15 /* Internal functions */
16 static void rpmsg_channel_created(struct rpmsg_channel *rp_chnl);
17 static void rpmsg_channel_deleted(struct rpmsg_channel *rp_chnl);
18 static void rpmsg_read_cb(struct rpmsg_channel *, void *, int, void *,
19                           unsigned long);
20 static void shutdown_cb(struct rpmsg_channel *rp_chnl);
22 /* Globals */
23 static struct rpmsg_channel *app_rp_chnl;
24 static volatile int chnl_is_alive = 0;
25 static struct remote_proc *proc = NULL;
26 static struct rsc_table_info rsc_info;
27 extern const struct remote_resource_table resources;
28 extern struct rproc_info_plat_local proc_table;
30 /* External functions */
31 extern void init_system();
32 extern void cleanup_system();
34 #define REDEF_O_CREAT 100
35 #define REDEF_O_EXCL 200
36 #define REDEF_O_RDONLY 0
37 #define REDEF_O_WRONLY 1
38 #define REDEF_O_RDWR 2
39 #define REDEF_O_APPEND 2000
40 #define REDEF_O_ACCMODE 3
42 #define RPC_CHANNEL_READY_TO_CLOSE "rpc_channel_ready_to_close"
44 /* Application entry point */
45 int main()
46 {
47         int fd, bytes_written, bytes_read;
48         char fname[] = "remote.file";
49         char wbuff[50];
50         char rbuff[1024];
51         char ubuff[50];
52         float fdata;
53         int idata;
54         int ret;
55         int status;
57         /* Initialize HW system components */
58         init_system();
60         /* Resource table needs to be provided to remoteproc_resource_init() */
61         rsc_info.rsc_tab = (struct resource_table *)&resources;
62         rsc_info.size = sizeof(resources);
64         /* Initialize RPMSG framework */
65         status = remoteproc_resource_init(&rsc_info, &proc_table,
66                                           rpmsg_channel_created,
67                                           rpmsg_channel_deleted, rpmsg_read_cb,
68                                           &proc, 0);
69         if (RPROC_SUCCESS != status) {
70                 return -1;
71         }
73         while (!chnl_is_alive) {
74                 hil_poll(proc->proc, 0);
75         }
77         /* redirect I/Os */
78         rpmsg_retarget_init(app_rp_chnl, shutdown_cb);
80         printf("\r\nRemote>Baremetal Remote Procedure Call (RPC) Demonstration\r\n");
81         printf("\r\nRemote>***************************************************\r\n");
83         printf("\r\nRemote>Rpmsg based retargetting to proxy initialized..\r\n");
85         /* Remote performing file IO on Master */
86         printf("\r\nRemote>FileIO demo ..\r\n");
88         printf("\r\nRemote>Creating a file on master and writing to it..\r\n");
89         fd = open(fname, REDEF_O_CREAT | REDEF_O_WRONLY | REDEF_O_APPEND,
90                   S_IRUSR | S_IWUSR);
91         printf("\r\nRemote>Opened file '%s' with fd = %d\r\n", fname, fd);
93         sprintf(wbuff, "This is a test string being written to file..");
94         bytes_written = write(fd, wbuff, strlen(wbuff));
95         printf("\r\nRemote>Wrote to fd = %d, size = %d, content = %s\r\n", fd,
96                bytes_written, wbuff);
97         close(fd);
98         printf("\r\nRemote>Closed fd = %d\r\n", fd);
100         /* Remote performing file IO on Master */
101         printf("\r\nRemote>Reading a file on master and displaying its contents..\r\n");
102         fd = open(fname, REDEF_O_RDONLY, S_IRUSR | S_IWUSR);
103         printf("\r\nRemote>Opened file '%s' with fd = %d\r\n", fname, fd);
104         bytes_read = read(fd, rbuff, 1024);
105         *(char *)(&rbuff[0] + bytes_read + 1) = 0;
106         printf("\r\nRemote>Read from fd = %d, size = %d, printing contents below .. %s\r\n",
107              fd, bytes_read, rbuff);
108         close(fd);
109         printf("\r\nRemote>Closed fd = %d\r\n", fd);
111         while (1) {
112                 /* Remote performing STDIO on Master */
113                 printf("\r\nRemote>Remote firmware using scanf and printf ..\r\n");
114                 printf("\r\nRemote>Scanning user input from master..\r\n");
115                 printf("\r\nRemote>Enter name\r\n");
116                 ret = scanf("%s", ubuff);
117                 if (ret) {
118                         printf("\r\nRemote>Enter age\r\n");
119                         ret = scanf("%d", &idata);
120                         if (ret) {
121                                 printf("\r\nRemote>Enter value for pi\r\n");
122                                 ret = scanf("%f", &fdata);
123                                 if (ret) {
124                                         printf("\r\nRemote>User name = '%s'\r\n", ubuff);
125                                         printf("\r\nRemote>User age = '%d'\r\n", idata);
126                                         printf("\r\nRemote>User entered value of pi = '%f'\r\n", fdata);
127                                 }
128                         }
129                 }
130                 if (!ret) {
131                         scanf("%s", ubuff);
132                         printf("Remote> Invalid value. Starting again....");
133                 } else {
134                         printf("\r\nRemote>Repeat demo ? (enter yes or no) \r\n");
135                         scanf("%s", ubuff);
136                         if ((strcmp(ubuff, "no")) && (strcmp(ubuff, "yes"))) {
137                                 printf("\r\nRemote>Invalid option. Starting again....\r\n");
138                         } else if ((!strcmp(ubuff, "no"))) {
139                                 printf("\r\nRemote>RPC retargetting quitting ...\r\n");
140                                 sprintf(wbuff, RPC_CHANNEL_READY_TO_CLOSE);
141                                 break;
142                         }
143                 }
144         }
146         printf("\r\nRemote> Firmware's rpmsg-openamp-demo-channel going down! \r\n");
148         rpmsg_retarget_send(wbuff, sizeof (RPC_CHANNEL_READY_TO_CLOSE) + 1);
150         while (chnl_is_alive)
151                 hil_poll(proc->proc, 0);
153         rpmsg_retarget_deinit(app_rp_chnl);
154         remoteproc_resource_deinit(proc);
155         cleanup_system();
156         return 0;
159 static void rpmsg_channel_created(struct rpmsg_channel *rp_chnl)
161         app_rp_chnl = rp_chnl;
162         chnl_is_alive = 1;
165 static void rpmsg_channel_deleted(struct rpmsg_channel *rp_chnl)
167         (void)rp_chnl;
168         app_rp_chnl = NULL;
171 static void rpmsg_read_cb(struct rpmsg_channel *rp_chnl, void *data, int len,
172                           void *priv, unsigned long src)
174         (void)rp_chnl;
175         (void)data;
176         (void)len;
177         (void)priv;
178         (void)src;
181 static void shutdown_cb(struct rpmsg_channel *rp_chnl)
183         (void)rp_chnl;
184         chnl_is_alive = 0;