From 2315dc80d645feed13ba38e731eea1eb538ce947 Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Wed, 12 Jul 2023 12:12:11 -0500 Subject: [PATCH 01/10] examples: linux: Factor out app_rpmsg_create_ept into common file Signed-off-by: Andrew Davis --- examples/linux/common/common.c | 18 ++++++++++++++++++ examples/linux/common/common.h | 10 ++++++++++ examples/linux/rpmsg-echo-test/Makefile | 4 ++-- examples/linux/rpmsg-echo-test/echo_test.c | 13 ++----------- examples/linux/rpmsg-mat-mul/Makefile | 4 ++-- examples/linux/rpmsg-mat-mul/mat_mul_demo.c | 13 ++----------- examples/linux/rpmsg-proxy-app/Makefile | 4 ++-- examples/linux/rpmsg-proxy-app/proxy_app.c | 13 ++----------- 8 files changed, 40 insertions(+), 39 deletions(-) create mode 100644 examples/linux/common/common.c create mode 100644 examples/linux/common/common.h diff --git a/examples/linux/common/common.c b/examples/linux/common/common.c new file mode 100644 index 0000000..d5e2852 --- /dev/null +++ b/examples/linux/common/common.c @@ -0,0 +1,18 @@ +// SPDX-License-Identifier: BSD-3-Clause + +#include +#include +#include +#include + +#include "common.h" + +int app_rpmsg_create_ept(int rpfd, struct rpmsg_endpoint_info *eptinfo) +{ + int ret; + + ret = ioctl(rpfd, RPMSG_CREATE_EPT_IOCTL, eptinfo); + if (ret) + perror("Failed to create endpoint.\n"); + return ret; +} diff --git a/examples/linux/common/common.h b/examples/linux/common/common.h new file mode 100644 index 0000000..0ef25ac --- /dev/null +++ b/examples/linux/common/common.h @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: BSD-3-Clause + +#ifndef __COMMON__H__ +#define __COMMON__H__ + +#include + +int app_rpmsg_create_ept(int rpfd, struct rpmsg_endpoint_info *eptinfo); + +#endif /* __COMMON__H__ */ diff --git a/examples/linux/rpmsg-echo-test/Makefile b/examples/linux/rpmsg-echo-test/Makefile index 31943f3..6707911 100644 --- a/examples/linux/rpmsg-echo-test/Makefile +++ b/examples/linux/rpmsg-echo-test/Makefile @@ -1,6 +1,6 @@ APP = echo_test -APP_OBJS = echo_test.o +APP_OBJS = echo_test.o ../common/common.o # Add any other object files to this list below @@ -11,7 +11,7 @@ $(APP): $(APP_OBJS) $(CC) $(LDFLAGS) -o $@ $(APP_OBJS) $(LDLIBS) clean: - rm -rf $(APP) *.o + rm -rf $(APP) $(APP_OBJS) %.o: %.c $(CC) -c $(CFLAGS) -o $@ $< diff --git a/examples/linux/rpmsg-echo-test/echo_test.c b/examples/linux/rpmsg-echo-test/echo_test.c index 827584e..c75f65e 100644 --- a/examples/linux/rpmsg-echo-test/echo_test.c +++ b/examples/linux/rpmsg-echo-test/echo_test.c @@ -18,12 +18,13 @@ #include #include #include -#include #include #include #include #include +#include "../common/common.h" + struct _payload { unsigned long num; unsigned long size; @@ -64,16 +65,6 @@ void send_shutdown(int fd) perror("write SHUTDOWN_MSG\n"); } -int app_rpmsg_create_ept(int rpfd, struct rpmsg_endpoint_info *eptinfo) -{ - int ret; - - ret = ioctl(rpfd, RPMSG_CREATE_EPT_IOCTL, eptinfo); - if (ret) - perror("Failed to create endpoint.\n"); - return ret; -} - static char *get_rpmsg_ept_dev_name(const char *rpmsg_char_name, const char *ept_name, char *ept_dev_name) diff --git a/examples/linux/rpmsg-mat-mul/Makefile b/examples/linux/rpmsg-mat-mul/Makefile index aa89036..e711edb 100644 --- a/examples/linux/rpmsg-mat-mul/Makefile +++ b/examples/linux/rpmsg-mat-mul/Makefile @@ -1,6 +1,6 @@ APP = mat_mul_demo -APP_OBJS = mat_mul_demo.o +APP_OBJS = mat_mul_demo.o ../common/common.o # Add any other object files to this list below @@ -11,7 +11,7 @@ $(APP): $(APP_OBJS) $(CC) $(LDFLAGS) -o $@ $(APP_OBJS) $(LDLIBS) -lpthread clean: - rm -rf $(APP) *.o + rm -rf $(APP) $(APP_OBJS) %.o: %.c $(CC) -c $(CFLAGS) -o $@ $< diff --git a/examples/linux/rpmsg-mat-mul/mat_mul_demo.c b/examples/linux/rpmsg-mat-mul/mat_mul_demo.c index 0e33348..3f2648a 100644 --- a/examples/linux/rpmsg-mat-mul/mat_mul_demo.c +++ b/examples/linux/rpmsg-mat-mul/mat_mul_demo.c @@ -15,12 +15,13 @@ #include #include #include -#include #include #include #include #include +#include "../common/common.h" + #define RPMSG_BUS_SYS "/sys/bus/rpmsg" #define PR_DBG(fmt, args ...) printf("%s():%u "fmt, __func__, __LINE__, ##args) @@ -113,16 +114,6 @@ void send_shutdown(int fd) perror("write SHUTDOWN_MSG\n"); } -int app_rpmsg_create_ept(int rpfd, struct rpmsg_endpoint_info *eptinfo) -{ - int ret; - - ret = ioctl(rpfd, RPMSG_CREATE_EPT_IOCTL, eptinfo); - if (ret) - perror("Failed to create endpoint.\n"); - return ret; -} - static char *get_rpmsg_ept_dev_name(const char *rpmsg_char_name, const char *ept_name, char *ept_dev_name) diff --git a/examples/linux/rpmsg-proxy-app/Makefile b/examples/linux/rpmsg-proxy-app/Makefile index ad503a9..0d1e550 100644 --- a/examples/linux/rpmsg-proxy-app/Makefile +++ b/examples/linux/rpmsg-proxy-app/Makefile @@ -1,6 +1,6 @@ APP = proxy_app -APP_OBJS = proxy_app.o +APP_OBJS = proxy_app.o ../common/common.o # Add any other object files to this list below @@ -11,7 +11,7 @@ $(APP): $(APP_OBJS) $(CC) $(LDFLAGS) -o $@ $(APP_OBJS) $(LDLIBS) clean: - rm -rf $(APP) *.o + rm -rf $(APP) $(APP_OBJS) %.o: %.c $(CC) -c $(CFLAGS) -o $@ $< diff --git a/examples/linux/rpmsg-proxy-app/proxy_app.c b/examples/linux/rpmsg-proxy-app/proxy_app.c index 90939e0..3bcd901 100644 --- a/examples/linux/rpmsg-proxy-app/proxy_app.c +++ b/examples/linux/rpmsg-proxy-app/proxy_app.c @@ -8,12 +8,13 @@ #include #include #include -#include #include #include #include "proxy_app.h" #include +#include "../common/common.h" + #define RPMSG_BUS_SYS "/sys/bus/rpmsg" #define RPC_BUFF_SIZE 512 @@ -209,16 +210,6 @@ void exit_action_handler(int signum) proxy->active = 0; } -static int app_rpmsg_create_ept(int rpfd, struct rpmsg_endpoint_info *eptinfo) -{ - int ret; - - ret = ioctl(rpfd, RPMSG_CREATE_EPT_IOCTL, eptinfo); - if (ret) - perror("Failed to create endpoint.\n"); - return ret; -} - static char *get_rpmsg_ept_dev_name(const char *rpmsg_char_name, const char *ept_name, char *ept_dev_name) From 7c36634fca67bc58ee309d9ceb9ee41e9b4367de Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Wed, 12 Jul 2023 12:19:12 -0500 Subject: [PATCH 02/10] examples: linux: Factor out get_rpmsg_ept_dev_name into common file Signed-off-by: Andrew Davis --- examples/linux/common/common.c | 41 +++++++++++++++++++++ examples/linux/common/common.h | 3 ++ examples/linux/rpmsg-echo-test/echo_test.c | 39 -------------------- examples/linux/rpmsg-mat-mul/mat_mul_demo.c | 39 -------------------- examples/linux/rpmsg-proxy-app/proxy_app.c | 39 -------------------- 5 files changed, 44 insertions(+), 117 deletions(-) diff --git a/examples/linux/common/common.c b/examples/linux/common/common.c index d5e2852..698ab3b 100644 --- a/examples/linux/common/common.c +++ b/examples/linux/common/common.c @@ -3,7 +3,9 @@ #include #include #include +#include #include +#include #include "common.h" @@ -16,3 +18,42 @@ int app_rpmsg_create_ept(int rpfd, struct rpmsg_endpoint_info *eptinfo) perror("Failed to create endpoint.\n"); return ret; } + +char *get_rpmsg_ept_dev_name(const char *rpmsg_char_name, + const char *ept_name, + char *ept_dev_name) +{ + char sys_rpmsg_ept_name_path[64]; + char svc_name[64]; + char *sys_rpmsg_path = "/sys/class/rpmsg"; + FILE *fp; + int i; + int ept_name_len; + + for (i = 0; i < 128; i++) { + sprintf(sys_rpmsg_ept_name_path, "%s/%s/rpmsg%d/name", + sys_rpmsg_path, rpmsg_char_name, i); + printf("checking %s\n", sys_rpmsg_ept_name_path); + if (access(sys_rpmsg_ept_name_path, F_OK) < 0) + continue; + fp = fopen(sys_rpmsg_ept_name_path, "r"); + if (!fp) { + printf("failed to open %s\n", sys_rpmsg_ept_name_path); + break; + } + fgets(svc_name, sizeof(svc_name), fp); + fclose(fp); + printf("svc_name: %s.\n",svc_name); + ept_name_len = strlen(ept_name); + if (ept_name_len > sizeof(svc_name)) + ept_name_len = sizeof(svc_name); + if (!strncmp(svc_name, ept_name, ept_name_len)) { + sprintf(ept_dev_name, "rpmsg%d", i); + return ept_dev_name; + } + } + + printf("Not able to RPMsg endpoint file for %s:%s.\n", + rpmsg_char_name, ept_name); + return NULL; +} diff --git a/examples/linux/common/common.h b/examples/linux/common/common.h index 0ef25ac..b8ad75f 100644 --- a/examples/linux/common/common.h +++ b/examples/linux/common/common.h @@ -6,5 +6,8 @@ #include int app_rpmsg_create_ept(int rpfd, struct rpmsg_endpoint_info *eptinfo); +char *get_rpmsg_ept_dev_name(const char *rpmsg_char_name, + const char *ept_name, + char *ept_dev_name); #endif /* __COMMON__H__ */ diff --git a/examples/linux/rpmsg-echo-test/echo_test.c b/examples/linux/rpmsg-echo-test/echo_test.c index c75f65e..3d5a3f6 100644 --- a/examples/linux/rpmsg-echo-test/echo_test.c +++ b/examples/linux/rpmsg-echo-test/echo_test.c @@ -65,45 +65,6 @@ void send_shutdown(int fd) perror("write SHUTDOWN_MSG\n"); } -static char *get_rpmsg_ept_dev_name(const char *rpmsg_char_name, - const char *ept_name, - char *ept_dev_name) -{ - char sys_rpmsg_ept_name_path[64]; - char svc_name[64]; - char *sys_rpmsg_path = "/sys/class/rpmsg"; - FILE *fp; - int i; - int ept_name_len; - - for (i = 0; i < 128; i++) { - sprintf(sys_rpmsg_ept_name_path, "%s/%s/rpmsg%d/name", - sys_rpmsg_path, rpmsg_char_name, i); - printf("checking %s\n", sys_rpmsg_ept_name_path); - if (access(sys_rpmsg_ept_name_path, F_OK) < 0) - continue; - fp = fopen(sys_rpmsg_ept_name_path, "r"); - if (!fp) { - printf("failed to open %s\n", sys_rpmsg_ept_name_path); - break; - } - fgets(svc_name, sizeof(svc_name), fp); - fclose(fp); - printf("svc_name: %s.\n",svc_name); - ept_name_len = strlen(ept_name); - if (ept_name_len > sizeof(svc_name)) - ept_name_len = sizeof(svc_name); - if (!strncmp(svc_name, ept_name, ept_name_len)) { - sprintf(ept_dev_name, "rpmsg%d", i); - return ept_dev_name; - } - } - - printf("Not able to RPMsg endpoint file for %s:%s.\n", - rpmsg_char_name, ept_name); - return NULL; -} - static int bind_rpmsg_chrdev(const char *rpmsg_dev_name) { char fpath[256]; diff --git a/examples/linux/rpmsg-mat-mul/mat_mul_demo.c b/examples/linux/rpmsg-mat-mul/mat_mul_demo.c index 3f2648a..6683305 100644 --- a/examples/linux/rpmsg-mat-mul/mat_mul_demo.c +++ b/examples/linux/rpmsg-mat-mul/mat_mul_demo.c @@ -114,45 +114,6 @@ void send_shutdown(int fd) perror("write SHUTDOWN_MSG\n"); } -static char *get_rpmsg_ept_dev_name(const char *rpmsg_char_name, - const char *ept_name, - char *ept_dev_name) -{ - char sys_rpmsg_ept_name_path[64]; - char svc_name[64]; - char *sys_rpmsg_path = "/sys/class/rpmsg"; - FILE *fp; - int i; - int ept_name_len; - - for (i = 0; i < 128; i++) { - sprintf(sys_rpmsg_ept_name_path, "%s/%s/rpmsg%d/name", - sys_rpmsg_path, rpmsg_char_name, i); - printf("checking %s\n", sys_rpmsg_ept_name_path); - if (access(sys_rpmsg_ept_name_path, F_OK) < 0) - continue; - fp = fopen(sys_rpmsg_ept_name_path, "r"); - if (!fp) { - printf("failed to open %s\n", sys_rpmsg_ept_name_path); - break; - } - fgets(svc_name, sizeof(svc_name), fp); - fclose(fp); - printf("svc_name: %s.\n",svc_name); - ept_name_len = strlen(ept_name); - if (ept_name_len > sizeof(svc_name)) - ept_name_len = sizeof(svc_name); - if (!strncmp(svc_name, ept_name, ept_name_len)) { - sprintf(ept_dev_name, "rpmsg%d", i); - return ept_dev_name; - } - } - - printf("Not able to RPMsg endpoint file for %s:%s.\n", - rpmsg_char_name, ept_name); - return NULL; -} - static int bind_rpmsg_chrdev(const char *rpmsg_dev_name) { char fpath[256]; diff --git a/examples/linux/rpmsg-proxy-app/proxy_app.c b/examples/linux/rpmsg-proxy-app/proxy_app.c index 3bcd901..bc27da7 100644 --- a/examples/linux/rpmsg-proxy-app/proxy_app.c +++ b/examples/linux/rpmsg-proxy-app/proxy_app.c @@ -210,45 +210,6 @@ void exit_action_handler(int signum) proxy->active = 0; } -static char *get_rpmsg_ept_dev_name(const char *rpmsg_char_name, - const char *ept_name, - char *ept_dev_name) -{ - char sys_rpmsg_ept_name_path[64]; - char svc_name[64]; - char *sys_rpmsg_path = "/sys/class/rpmsg"; - FILE *fp; - int i; - int ept_name_len; - - for (i = 0; i < 128; i++) { - sprintf(sys_rpmsg_ept_name_path, "%s/%s/rpmsg%d/name", - sys_rpmsg_path, rpmsg_char_name, i); - printf("checking %s\n", sys_rpmsg_ept_name_path); - if (access(sys_rpmsg_ept_name_path, F_OK) < 0) - continue; - fp = fopen(sys_rpmsg_ept_name_path, "r"); - if (!fp) { - printf("failed to open %s\n", sys_rpmsg_ept_name_path); - break; - } - fgets(svc_name, sizeof(svc_name), fp); - fclose(fp); - printf("svc_name: %s.\n",svc_name); - ept_name_len = strlen(ept_name); - if (ept_name_len > sizeof(svc_name)) - ept_name_len = sizeof(svc_name); - if (!strncmp(svc_name, ept_name, ept_name_len)) { - sprintf(ept_dev_name, "rpmsg%d", i); - return ept_dev_name; - } - } - - printf("Not able to RPMsg endpoint file for %s:%s.\n", - rpmsg_char_name, ept_name); - return NULL; -} - static int bind_rpmsg_chrdev(const char *rpmsg_dev_name) { char fpath[256]; From 071fcd943a0756135cdde57f92bf0584cb7417dd Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Wed, 12 Jul 2023 12:26:29 -0500 Subject: [PATCH 03/10] examples: linux: Factor out bind_rpmsg_chrdev into common file Signed-off-by: Andrew Davis --- examples/linux/common/common.c | 46 +++++++++++++++++++++ examples/linux/common/common.h | 3 ++ examples/linux/rpmsg-echo-test/echo_test.c | 44 -------------------- examples/linux/rpmsg-mat-mul/mat_mul_demo.c | 44 -------------------- examples/linux/rpmsg-proxy-app/proxy_app.c | 46 --------------------- 5 files changed, 49 insertions(+), 134 deletions(-) diff --git a/examples/linux/common/common.c b/examples/linux/common/common.c index 698ab3b..03908b2 100644 --- a/examples/linux/common/common.c +++ b/examples/linux/common/common.c @@ -1,5 +1,7 @@ // SPDX-License-Identifier: BSD-3-Clause +#include +#include #include #include #include @@ -57,3 +59,47 @@ char *get_rpmsg_ept_dev_name(const char *rpmsg_char_name, rpmsg_char_name, ept_name); return NULL; } + +int bind_rpmsg_chrdev(const char *rpmsg_dev_name) +{ + char fpath[256]; + char *rpmsg_chdrv = "rpmsg_chrdev"; + int fd; + int ret; + + /* rpmsg dev overrides path */ + sprintf(fpath, "%s/devices/%s/driver_override", + RPMSG_BUS_SYS, rpmsg_dev_name); + printf("open %s\n", fpath); + fd = open(fpath, O_WRONLY); + if (fd < 0) { + fprintf(stderr, "Failed to open %s, %s\n", + fpath, strerror(errno)); + return -EINVAL; + } + ret = write(fd, rpmsg_chdrv, strlen(rpmsg_chdrv) + 1); + if (ret < 0) { + fprintf(stderr, "Failed to write %s to %s, %s\n", + rpmsg_chdrv, fpath, strerror(errno)); + return -EINVAL; + } + close(fd); + + /* bind the rpmsg device to rpmsg char driver */ + sprintf(fpath, "%s/drivers/%s/bind", RPMSG_BUS_SYS, rpmsg_chdrv); + fd = open(fpath, O_WRONLY); + if (fd < 0) { + fprintf(stderr, "Failed to open %s, %s\n", + fpath, strerror(errno)); + return -EINVAL; + } + printf("write %s to %s\n", rpmsg_dev_name, fpath); + ret = write(fd, rpmsg_dev_name, strlen(rpmsg_dev_name) + 1); + if (ret < 0) { + fprintf(stderr, "Failed to write %s to %s, %s\n", + rpmsg_dev_name, fpath, strerror(errno)); + return -EINVAL; + } + close(fd); + return 0; +} diff --git a/examples/linux/common/common.h b/examples/linux/common/common.h index b8ad75f..1fdd0e1 100644 --- a/examples/linux/common/common.h +++ b/examples/linux/common/common.h @@ -5,9 +5,12 @@ #include +#define RPMSG_BUS_SYS "/sys/bus/rpmsg" + int app_rpmsg_create_ept(int rpfd, struct rpmsg_endpoint_info *eptinfo); char *get_rpmsg_ept_dev_name(const char *rpmsg_char_name, const char *ept_name, char *ept_dev_name); +int bind_rpmsg_chrdev(const char *rpmsg_dev_name); #endif /* __COMMON__H__ */ diff --git a/examples/linux/rpmsg-echo-test/echo_test.c b/examples/linux/rpmsg-echo-test/echo_test.c index 3d5a3f6..9d4eff7 100644 --- a/examples/linux/rpmsg-echo-test/echo_test.c +++ b/examples/linux/rpmsg-echo-test/echo_test.c @@ -65,50 +65,6 @@ void send_shutdown(int fd) perror("write SHUTDOWN_MSG\n"); } -static int bind_rpmsg_chrdev(const char *rpmsg_dev_name) -{ - char fpath[256]; - char *rpmsg_chdrv = "rpmsg_chrdev"; - int fd; - int ret; - - /* rpmsg dev overrides path */ - sprintf(fpath, "%s/devices/%s/driver_override", - RPMSG_BUS_SYS, rpmsg_dev_name); - PR_DBG("open %s\n", fpath); - fd = open(fpath, O_WRONLY); - if (fd < 0) { - fprintf(stderr, "Failed to open %s, %s\n", - fpath, strerror(errno)); - return -EINVAL; - } - ret = write(fd, rpmsg_chdrv, strlen(rpmsg_chdrv) + 1); - if (ret < 0) { - fprintf(stderr, "Failed to write %s to %s, %s\n", - rpmsg_chdrv, fpath, strerror(errno)); - return -EINVAL; - } - close(fd); - - /* bind the rpmsg device to rpmsg char driver */ - sprintf(fpath, "%s/drivers/%s/bind", RPMSG_BUS_SYS, rpmsg_chdrv); - fd = open(fpath, O_WRONLY); - if (fd < 0) { - fprintf(stderr, "Failed to open %s, %s\n", - fpath, strerror(errno)); - return -EINVAL; - } - PR_DBG("write %s to %s\n", rpmsg_dev_name, fpath); - ret = write(fd, rpmsg_dev_name, strlen(rpmsg_dev_name) + 1); - if (ret < 0) { - fprintf(stderr, "Failed to write %s to %s, %s\n", - rpmsg_dev_name, fpath, strerror(errno)); - return -EINVAL; - } - close(fd); - return 0; -} - static int get_rpmsg_chrdev_fd(const char *rpmsg_dev_name, char *rpmsg_ctrl_name) { diff --git a/examples/linux/rpmsg-mat-mul/mat_mul_demo.c b/examples/linux/rpmsg-mat-mul/mat_mul_demo.c index 6683305..1d6221d 100644 --- a/examples/linux/rpmsg-mat-mul/mat_mul_demo.c +++ b/examples/linux/rpmsg-mat-mul/mat_mul_demo.c @@ -114,50 +114,6 @@ void send_shutdown(int fd) perror("write SHUTDOWN_MSG\n"); } -static int bind_rpmsg_chrdev(const char *rpmsg_dev_name) -{ - char fpath[256]; - char *rpmsg_chdrv = "rpmsg_chrdev"; - int fd; - int ret; - - /* rpmsg dev overrides path */ - sprintf(fpath, "%s/devices/%s/driver_override", - RPMSG_BUS_SYS, rpmsg_dev_name); - PR_DBG("open %s\n", fpath); - fd = open(fpath, O_WRONLY); - if (fd < 0) { - fprintf(stderr, "Failed to open %s, %s\n", - fpath, strerror(errno)); - return -EINVAL; - } - ret = write(fd, rpmsg_chdrv, strlen(rpmsg_chdrv) + 1); - if (ret < 0) { - fprintf(stderr, "Failed to write %s to %s, %s\n", - rpmsg_chdrv, fpath, strerror(errno)); - return -EINVAL; - } - close(fd); - - /* bind the rpmsg device to rpmsg char driver */ - sprintf(fpath, "%s/drivers/%s/bind", RPMSG_BUS_SYS, rpmsg_chdrv); - fd = open(fpath, O_WRONLY); - if (fd < 0) { - fprintf(stderr, "Failed to open %s, %s\n", - fpath, strerror(errno)); - return -EINVAL; - } - PR_DBG("write %s to %s\n", rpmsg_dev_name, fpath); - ret = write(fd, rpmsg_dev_name, strlen(rpmsg_dev_name) + 1); - if (ret < 0) { - fprintf(stderr, "Failed to write %s to %s, %s\n", - rpmsg_dev_name, fpath, strerror(errno)); - return -EINVAL; - } - close(fd); - return 0; -} - static int get_rpmsg_chrdev_fd(const char *rpmsg_dev_name, char *rpmsg_ctrl_name) { diff --git a/examples/linux/rpmsg-proxy-app/proxy_app.c b/examples/linux/rpmsg-proxy-app/proxy_app.c index bc27da7..23120ff 100644 --- a/examples/linux/rpmsg-proxy-app/proxy_app.c +++ b/examples/linux/rpmsg-proxy-app/proxy_app.c @@ -15,8 +15,6 @@ #include "../common/common.h" -#define RPMSG_BUS_SYS "/sys/bus/rpmsg" - #define RPC_BUFF_SIZE 512 #define PROXY_ENDPOINT 127 @@ -210,50 +208,6 @@ void exit_action_handler(int signum) proxy->active = 0; } -static int bind_rpmsg_chrdev(const char *rpmsg_dev_name) -{ - char fpath[256]; - char *rpmsg_chdrv = "rpmsg_chrdev"; - int fd; - int ret; - - /* rpmsg dev overrides path */ - sprintf(fpath, "%s/devices/%s/driver_override", - RPMSG_BUS_SYS, rpmsg_dev_name); - printf("open %s\n", fpath); - fd = open(fpath, O_WRONLY); - if (fd < 0) { - fprintf(stderr, "Failed to open %s, %s\n", - fpath, strerror(errno)); - return -EINVAL; - } - ret = write(fd, rpmsg_chdrv, strlen(rpmsg_chdrv) + 1); - if (ret < 0) { - fprintf(stderr, "Failed to write %s to %s, %s\n", - rpmsg_chdrv, fpath, strerror(errno)); - return -EINVAL; - } - close(fd); - - /* bind the rpmsg device to rpmsg char driver */ - sprintf(fpath, "%s/drivers/%s/bind", RPMSG_BUS_SYS, rpmsg_chdrv); - fd = open(fpath, O_WRONLY); - if (fd < 0) { - fprintf(stderr, "Failed to open %s, %s\n", - fpath, strerror(errno)); - return -EINVAL; - } - printf("write %s to %s\n", rpmsg_dev_name, fpath); - ret = write(fd, rpmsg_dev_name, strlen(rpmsg_dev_name) + 1); - if (ret < 0) { - fprintf(stderr, "Failed to write %s to %s, %s\n", - rpmsg_dev_name, fpath, strerror(errno)); - return -EINVAL; - } - close(fd); - return 0; -} - static int get_rpmsg_chrdev_fd(const char *rpmsg_dev_name, char *rpmsg_ctrl_name) { From 9f49ab4701fd18a263383076cbd2cb0f4a7fc5d3 Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Wed, 12 Jul 2023 12:33:27 -0500 Subject: [PATCH 04/10] examples: linux: Factor out get_rpmsg_chrdev_fd into common file Signed-off-by: Andrew Davis --- examples/linux/common/common.c | 37 +++++++++++++++++++++ examples/linux/common/common.h | 1 + examples/linux/rpmsg-echo-test/echo_test.c | 36 -------------------- examples/linux/rpmsg-mat-mul/mat_mul_demo.c | 36 -------------------- examples/linux/rpmsg-proxy-app/proxy_app.c | 34 ------------------- 5 files changed, 38 insertions(+), 106 deletions(-) diff --git a/examples/linux/common/common.c b/examples/linux/common/common.c index 03908b2..f3b4122 100644 --- a/examples/linux/common/common.c +++ b/examples/linux/common/common.c @@ -1,7 +1,9 @@ // SPDX-License-Identifier: BSD-3-Clause +#include #include #include +#include #include #include #include @@ -103,3 +105,38 @@ int bind_rpmsg_chrdev(const char *rpmsg_dev_name) close(fd); return 0; } + +int get_rpmsg_chrdev_fd(const char *rpmsg_dev_name, char *rpmsg_ctrl_name) +{ + char dpath[2*NAME_MAX]; + DIR *dir; + struct dirent *ent; + int fd; + + sprintf(dpath, "%s/devices/%s/rpmsg", RPMSG_BUS_SYS, rpmsg_dev_name); + printf("opendir %s\n", dpath); + dir = opendir(dpath); + if (dir == NULL) { + fprintf(stderr, "opendir %s, %s\n", dpath, strerror(errno)); + return -EINVAL; + } + while ((ent = readdir(dir)) != NULL) { + if (!strncmp(ent->d_name, "rpmsg_ctrl", 10)) { + sprintf(dpath, "/dev/%s", ent->d_name); + closedir(dir); + printf("open %s\n", dpath); + fd = open(dpath, O_RDWR | O_NONBLOCK); + if (fd < 0) { + fprintf(stderr, "open %s, %s\n", + dpath, strerror(errno)); + return fd; + } + sprintf(rpmsg_ctrl_name, "%s", ent->d_name); + return fd; + } + } + + fprintf(stderr, "No rpmsg_ctrl file found in %s\n", dpath); + closedir(dir); + return -EINVAL; +} diff --git a/examples/linux/common/common.h b/examples/linux/common/common.h index 1fdd0e1..99d3596 100644 --- a/examples/linux/common/common.h +++ b/examples/linux/common/common.h @@ -12,5 +12,6 @@ char *get_rpmsg_ept_dev_name(const char *rpmsg_char_name, const char *ept_name, char *ept_dev_name); int bind_rpmsg_chrdev(const char *rpmsg_dev_name); +int get_rpmsg_chrdev_fd(const char *rpmsg_dev_name, char *rpmsg_ctrl_name); #endif /* __COMMON__H__ */ diff --git a/examples/linux/rpmsg-echo-test/echo_test.c b/examples/linux/rpmsg-echo-test/echo_test.c index 9d4eff7..dc740fe 100644 --- a/examples/linux/rpmsg-echo-test/echo_test.c +++ b/examples/linux/rpmsg-echo-test/echo_test.c @@ -65,42 +65,6 @@ void send_shutdown(int fd) perror("write SHUTDOWN_MSG\n"); } -static int get_rpmsg_chrdev_fd(const char *rpmsg_dev_name, - char *rpmsg_ctrl_name) -{ - char dpath[2*NAME_MAX]; - DIR *dir; - struct dirent *ent; - int fd; - - sprintf(dpath, "%s/devices/%s/rpmsg", RPMSG_BUS_SYS, rpmsg_dev_name); - PR_DBG("opendir %s\n", dpath); - dir = opendir(dpath); - if (dir == NULL) { - fprintf(stderr, "opendir %s, %s\n", dpath, strerror(errno)); - return -EINVAL; - } - while ((ent = readdir(dir)) != NULL) { - if (!strncmp(ent->d_name, "rpmsg_ctrl", 10)) { - sprintf(dpath, "/dev/%s", ent->d_name); - closedir(dir); - PR_DBG("open %s\n", dpath); - fd = open(dpath, O_RDWR | O_NONBLOCK); - if (fd < 0) { - fprintf(stderr, "open %s, %s\n", - dpath, strerror(errno)); - return fd; - } - sprintf(rpmsg_ctrl_name, "%s", ent->d_name); - return fd; - } - } - - fprintf(stderr, "No rpmsg_ctrl file found in %s\n", dpath); - closedir(dir); - return -EINVAL; -} - static void set_src_dst(char *out, struct rpmsg_endpoint_info *pep) { long dst = 0; diff --git a/examples/linux/rpmsg-mat-mul/mat_mul_demo.c b/examples/linux/rpmsg-mat-mul/mat_mul_demo.c index 1d6221d..dbc68d9 100644 --- a/examples/linux/rpmsg-mat-mul/mat_mul_demo.c +++ b/examples/linux/rpmsg-mat-mul/mat_mul_demo.c @@ -114,42 +114,6 @@ void send_shutdown(int fd) perror("write SHUTDOWN_MSG\n"); } -static int get_rpmsg_chrdev_fd(const char *rpmsg_dev_name, - char *rpmsg_ctrl_name) -{ - char dpath[2*NAME_MAX]; - DIR *dir; - struct dirent *ent; - int fd; - - sprintf(dpath, "%s/devices/%s/rpmsg", RPMSG_BUS_SYS, rpmsg_dev_name); - PR_DBG("opendir %s\n", dpath); - dir = opendir(dpath); - if (dir == NULL) { - fprintf(stderr, "opendir %s, %s\n", dpath, strerror(errno)); - return -EINVAL; - } - while ((ent = readdir(dir)) != NULL) { - if (!strncmp(ent->d_name, "rpmsg_ctrl", 10)) { - sprintf(dpath, "/dev/%s", ent->d_name); - closedir(dir); - PR_DBG("open %s\n", dpath); - fd = open(dpath, O_RDWR | O_NONBLOCK); - if (fd < 0) { - fprintf(stderr, "open %s, %s\n", - dpath, strerror(errno)); - return fd; - } - sprintf(rpmsg_ctrl_name, "%s", ent->d_name); - return fd; - } - } - - fprintf(stderr, "No rpmsg_ctrl file found in %s\n", dpath); - closedir(dir); - return -EINVAL; -} - static void set_src_dst(char *out, struct rpmsg_endpoint_info *pep) { long dst = 0; diff --git a/examples/linux/rpmsg-proxy-app/proxy_app.c b/examples/linux/rpmsg-proxy-app/proxy_app.c index 23120ff..895e975 100644 --- a/examples/linux/rpmsg-proxy-app/proxy_app.c +++ b/examples/linux/rpmsg-proxy-app/proxy_app.c @@ -208,40 +208,6 @@ void exit_action_handler(int signum) proxy->active = 0; } -static int get_rpmsg_chrdev_fd(const char *rpmsg_dev_name, - char *rpmsg_ctrl_name) -{ - char dpath[2*NAME_MAX]; - DIR *dir; - struct dirent *ent; - int fd; - - sprintf(dpath, "%s/devices/%s/rpmsg", RPMSG_BUS_SYS, rpmsg_dev_name); - printf("opendir %s\n", dpath); - dir = opendir(dpath); - if (dir == NULL) { - fprintf(stderr, "opendir %s, %s\n", dpath, strerror(errno)); - return -EINVAL; - } - while ((ent = readdir(dir)) != NULL) { - if (!strncmp(ent->d_name, "rpmsg_ctrl", 10)) { - sprintf(dpath, "/dev/%s", ent->d_name); - printf("open %s\n", dpath); - fd = open(dpath, O_RDWR | O_NONBLOCK); - if (fd < 0) { - fprintf(stderr, "open %s, %s\n", - dpath, strerror(errno)); - return fd; - } - sprintf(rpmsg_ctrl_name, "%s", ent->d_name); - return fd; - } - } - - fprintf(stderr, "No rpmsg char dev file is found\n"); - return -EINVAL; -} - static void set_src_dst(char *out, struct rpmsg_endpoint_info *pep) { long dst = 0; From 1c832e2c814e90478c1fbf92500d7df1baf8748b Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Wed, 12 Jul 2023 13:28:46 -0500 Subject: [PATCH 05/10] examples: linux: Factor out lookup_channel into common file Signed-off-by: Andrew Davis --- examples/linux/common/common.c | 45 +++++++++++++++++++++ examples/linux/common/common.h | 1 + examples/linux/rpmsg-echo-test/echo_test.c | 44 -------------------- examples/linux/rpmsg-mat-mul/mat_mul_demo.c | 44 -------------------- examples/linux/rpmsg-proxy-app/proxy_app.c | 45 --------------------- 5 files changed, 46 insertions(+), 133 deletions(-) diff --git a/examples/linux/common/common.c b/examples/linux/common/common.c index f3b4122..75605aa 100644 --- a/examples/linux/common/common.c +++ b/examples/linux/common/common.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -140,3 +141,47 @@ int get_rpmsg_chrdev_fd(const char *rpmsg_dev_name, char *rpmsg_ctrl_name) closedir(dir); return -EINVAL; } + +static void set_src_dst(char *out, struct rpmsg_endpoint_info *pep) +{ + long dst = 0; + char *lastdot = strrchr(out, '.'); + + if (lastdot == NULL) + return; + dst = strtol(lastdot + 1, NULL, 10); + if ((errno == ERANGE && (dst == LONG_MAX || dst == LONG_MIN)) + || (errno != 0 && dst == 0)) { + return; + } + pep->dst = (unsigned int)dst; +} + +/* + * return the first dirent matching rpmsg-openamp-demo-channel + * in /sys/bus/rpmsg/devices/ E.g.: + * virtio0.rpmsg-openamp-demo-channel.-1.1024 + */ +int lookup_channel(char *out, struct rpmsg_endpoint_info *pep) +{ + char dpath[] = RPMSG_BUS_SYS "/devices"; + struct dirent *ent; + DIR *dir = opendir(dpath); + + if (dir == NULL) { + fprintf(stderr, "opendir %s, %s\n", dpath, strerror(errno)); + return -EINVAL; + } + while ((ent = readdir(dir)) != NULL) { + if (strstr(ent->d_name, pep->name)) { + strncpy(out, ent->d_name, NAME_MAX); + set_src_dst(out, pep); + printf("using dev file: %s\n", out); + closedir(dir); + return 0; + } + } + closedir(dir); + fprintf(stderr, "No dev file for %s in %s\n", pep->name, dpath); + return -EINVAL; +} diff --git a/examples/linux/common/common.h b/examples/linux/common/common.h index 99d3596..c5cba3e 100644 --- a/examples/linux/common/common.h +++ b/examples/linux/common/common.h @@ -13,5 +13,6 @@ char *get_rpmsg_ept_dev_name(const char *rpmsg_char_name, char *ept_dev_name); int bind_rpmsg_chrdev(const char *rpmsg_dev_name); int get_rpmsg_chrdev_fd(const char *rpmsg_dev_name, char *rpmsg_ctrl_name); +int lookup_channel(char *out, struct rpmsg_endpoint_info *pep); #endif /* __COMMON__H__ */ diff --git a/examples/linux/rpmsg-echo-test/echo_test.c b/examples/linux/rpmsg-echo-test/echo_test.c index dc740fe..a5ee863 100644 --- a/examples/linux/rpmsg-echo-test/echo_test.c +++ b/examples/linux/rpmsg-echo-test/echo_test.c @@ -12,7 +12,6 @@ * to application which then app validates the data returned. */ -#include #include #include #include @@ -65,49 +64,6 @@ void send_shutdown(int fd) perror("write SHUTDOWN_MSG\n"); } -static void set_src_dst(char *out, struct rpmsg_endpoint_info *pep) -{ - long dst = 0; - char *lastdot = strrchr(out, '.'); - - if (lastdot == NULL) - return; - dst = strtol(lastdot + 1, NULL, 10); - if ((errno == ERANGE && (dst == LONG_MAX || dst == LONG_MIN)) - || (errno != 0 && dst == 0)) { - return; - } - pep->dst = (unsigned int)dst; -} - -/* - * return the first dirent matching rpmsg-openamp-demo-channel - * in /sys/bus/rpmsg/devices/ E.g.: - * virtio0.rpmsg-openamp-demo-channel.-1.1024 - */ -static void lookup_channel(char *out, struct rpmsg_endpoint_info *pep) -{ - char dpath[] = RPMSG_BUS_SYS "/devices"; - struct dirent *ent; - DIR *dir = opendir(dpath); - - if (dir == NULL) { - fprintf(stderr, "opendir %s, %s\n", dpath, strerror(errno)); - return; - } - while ((ent = readdir(dir)) != NULL) { - if (strstr(ent->d_name, pep->name)) { - strncpy(out, ent->d_name, NAME_MAX); - set_src_dst(out, pep); - PR_DBG("using dev file: %s\n", out); - closedir(dir); - return; - } - } - closedir(dir); - fprintf(stderr, "No dev file for %s in %s\n", pep->name, dpath); -} - void print_help(void) { extern char *__progname; diff --git a/examples/linux/rpmsg-mat-mul/mat_mul_demo.c b/examples/linux/rpmsg-mat-mul/mat_mul_demo.c index dbc68d9..2f74bc7 100644 --- a/examples/linux/rpmsg-mat-mul/mat_mul_demo.c +++ b/examples/linux/rpmsg-mat-mul/mat_mul_demo.c @@ -9,7 +9,6 @@ * and transmits the results back to this application. */ -#include #include #include #include @@ -114,49 +113,6 @@ void send_shutdown(int fd) perror("write SHUTDOWN_MSG\n"); } -static void set_src_dst(char *out, struct rpmsg_endpoint_info *pep) -{ - long dst = 0; - char *lastdot = strrchr(out, '.'); - - if (lastdot == NULL) - return; - dst = strtol(lastdot + 1, NULL, 10); - if ((errno == ERANGE && (dst == LONG_MAX || dst == LONG_MIN)) - || (errno != 0 && dst == 0)) { - return; - } - pep->dst = (unsigned int)dst; -} - -/* - * return the first dirent matching rpmsg-openamp-demo-channel - * in /sys/bus/rpmsg/devices/ E.g.: - * virtio0.rpmsg-openamp-demo-channel.-1.1024 - */ -static void lookup_channel(char *out, struct rpmsg_endpoint_info *pep) -{ - char dpath[] = RPMSG_BUS_SYS "/devices"; - struct dirent *ent; - DIR *dir = opendir(dpath); - - if (dir == NULL) { - fprintf(stderr, "opendir %s, %s\n", dpath, strerror(errno)); - return; - } - while ((ent = readdir(dir)) != NULL) { - if (strstr(ent->d_name, pep->name)) { - strncpy(out, ent->d_name, NAME_MAX); - set_src_dst(out, pep); - PR_DBG("using dev file: %s\n", out); - closedir(dir); - return; - } - } - closedir(dir); - fprintf(stderr, "No dev file for %s in %s\n", pep->name, dpath); -} - void print_help(void) { extern char *__progname; diff --git a/examples/linux/rpmsg-proxy-app/proxy_app.c b/examples/linux/rpmsg-proxy-app/proxy_app.c index 895e975..0ab8268 100644 --- a/examples/linux/rpmsg-proxy-app/proxy_app.c +++ b/examples/linux/rpmsg-proxy-app/proxy_app.c @@ -1,6 +1,5 @@ //SPDX-License-Identifier: BSD-3-Clause -#include #include #include #include @@ -208,50 +207,6 @@ void exit_action_handler(int signum) proxy->active = 0; } -static void set_src_dst(char *out, struct rpmsg_endpoint_info *pep) -{ - long dst = 0; - char *lastdot = strrchr(out, '.'); - - if (lastdot == NULL) - return; - dst = strtol(lastdot + 1, NULL, 10); - if ((errno == ERANGE && (dst == LONG_MAX || dst == LONG_MIN)) - || (errno != 0 && dst == 0)) { - return; - } - pep->dst = (unsigned int)dst; -} - -/* - * return the first dirent matching rpmsg-openamp-demo-channel - * in /sys/bus/rpmsg/devices/ E.g.: - * virtio0.rpmsg-openamp-demo-channel.-1.1024 - */ -static int lookup_channel(char *out, struct rpmsg_endpoint_info *pep) -{ - char dpath[] = RPMSG_BUS_SYS "/devices"; - struct dirent *ent; - DIR *dir = opendir(dpath); - - if (dir == NULL) { - fprintf(stderr, "opendir %s, %s\n", dpath, strerror(errno)); - return -EINVAL; - } - while ((ent = readdir(dir)) != NULL) { - if (strstr(ent->d_name, pep->name)) { - strncpy(out, ent->d_name, NAME_MAX); - set_src_dst(out, pep); - printf("using dev file: %s\n", out); - closedir(dir); - return 0; - } - } - closedir(dir); - fprintf(stderr, "No dev file for %s in %s\n", pep->name, dpath); - return -EINVAL; -} - void kill_action_handler(int signum) { printf("\r\nHost>RPC service killed !!\r\n"); From 14a6805fe42b8d818ed300e4fa8b047539ec1ac7 Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Wed, 12 Jul 2023 12:56:37 -0500 Subject: [PATCH 06/10] examples: rpmsg-mat-mul: Cleanup matrix printout Make matrix_print() into a common function and use it for both printing the input and output matrices. Also add 3 wide padding so the output looks more like a table when the numbers are not all the same length. Signed-off-by: Andrew Davis --- examples/linux/rpmsg-mat-mul/mat_mul_demo.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/examples/linux/rpmsg-mat-mul/mat_mul_demo.c b/examples/linux/rpmsg-mat-mul/mat_mul_demo.c index 2f74bc7..ad22bb9 100644 --- a/examples/linux/rpmsg-mat-mul/mat_mul_demo.c +++ b/examples/linux/rpmsg-mat-mul/mat_mul_demo.c @@ -36,14 +36,12 @@ static void matrix_print(struct _matrix *m) { int i, j; - /* Generate two random matrices */ - printf(" \r\n Host : Linux : Printing results \r\n"); - for (i = 0; i < m->size; ++i) { for (j = 0; j < m->size; ++j) - printf(" %d ", (unsigned int)m->elements[i][j]); + printf(" %3d ", (unsigned int)m->elements[i][j]); printf("\r\n"); } + printf("\r\n"); } static void generate_matrices(int num_matrices, @@ -60,19 +58,17 @@ static void generate_matrices(int num_matrices, /* Initialize workload */ p_matrix[i].size = matrix_size; - printf(" \r\n Host : Linux : Input matrix %d \r\n", i); for (j = 0; j < matrix_size; j++) { - printf("\r\n"); for (k = 0; k < matrix_size; k++) { value = (rand() & 0x7F); value = value % 10; p_matrix[i].elements[j][k] = value; - printf(" %d ", - (unsigned int)p_matrix[i].elements[j][k]); } } - printf("\r\n"); + + printf(" \r\n Host : Linux : Input matrix %d \r\n", i); + matrix_print(&p_matrix[i]); } } @@ -86,6 +82,7 @@ void matrix_mult(int ntimes) int i; for (i=0; i < ntimes; i++){ + /* Generate two random matrices */ generate_matrices(2, MATRIX_SIZE, i_matrix); printf("%d: write rpmsg: %lu bytes\n", i, sizeof(i_matrix)); @@ -97,6 +94,7 @@ void matrix_mult(int ntimes) do { rc = read(fd, &r_matrix, sizeof(r_matrix)); } while (rc < (int)sizeof(r_matrix)); + printf(" \r\n Host : Linux : Printing results \r\n"); matrix_print(&r_matrix); printf("End of Matrix multiplication demo Round %d\n", i); } From aae12b1edc23236852472fda7a4404d9ea4cf963 Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Wed, 12 Jul 2023 13:54:50 -0500 Subject: [PATCH 07/10] examples: linux: Remove uses of global variables Usually considered bad practice and the code is more readable without. Signed-off-by: Andrew Davis --- examples/linux/rpmsg-echo-test/echo_test.c | 5 +-- examples/linux/rpmsg-mat-mul/mat_mul_demo.c | 50 ++++++++++----------- examples/linux/rpmsg-proxy-app/proxy_app.c | 2 - 3 files changed, 26 insertions(+), 31 deletions(-) diff --git a/examples/linux/rpmsg-echo-test/echo_test.c b/examples/linux/rpmsg-echo-test/echo_test.c index a5ee863..e688d28 100644 --- a/examples/linux/rpmsg-echo-test/echo_test.c +++ b/examples/linux/rpmsg-echo-test/echo_test.c @@ -30,9 +30,6 @@ struct _payload { char data[]; }; -struct _payload *i_payload; -struct _payload *r_payload; - #define RPMSG_GET_KFIFO_SIZE 1 #define RPMSG_GET_AVAIL_DATA_SIZE 2 #define RPMSG_GET_FREE_SPACE 3 @@ -91,6 +88,8 @@ int main(int argc, char *argv[]) }; char ept_dev_name[16]; char ept_dev_path[32]; + struct _payload *i_payload; + struct _payload *r_payload; printf("\r\n Echo test start \r\n"); lookup_channel(rpmsg_dev, &eptinfo); diff --git a/examples/linux/rpmsg-mat-mul/mat_mul_demo.c b/examples/linux/rpmsg-mat-mul/mat_mul_demo.c index ad22bb9..61db36a 100644 --- a/examples/linux/rpmsg-mat-mul/mat_mul_demo.c +++ b/examples/linux/rpmsg-mat-mul/mat_mul_demo.c @@ -73,31 +73,25 @@ static void generate_matrices(int num_matrices, } -static int charfd = -1, fd; -static struct _matrix i_matrix[2]; -static struct _matrix r_matrix; - -void matrix_mult(int ntimes) +void matrix_mult(int fd) { - int i; - - for (i=0; i < ntimes; i++){ - /* Generate two random matrices */ - generate_matrices(2, MATRIX_SIZE, i_matrix); - - printf("%d: write rpmsg: %lu bytes\n", i, sizeof(i_matrix)); - ssize_t rc = write(fd, i_matrix, sizeof(i_matrix)); - if (rc < 0) - fprintf(stderr, "write,errno = %ld, %d\n", rc, errno); - - puts("read results"); - do { - rc = read(fd, &r_matrix, sizeof(r_matrix)); - } while (rc < (int)sizeof(r_matrix)); - printf(" \r\n Host : Linux : Printing results \r\n"); - matrix_print(&r_matrix); - printf("End of Matrix multiplication demo Round %d\n", i); - } + struct _matrix i_matrix[2]; + struct _matrix r_matrix; + + /* Generate two random matrices */ + generate_matrices(2, MATRIX_SIZE, i_matrix); + + printf("write rpmsg: %lu bytes\n", sizeof(i_matrix)); + ssize_t rc = write(fd, i_matrix, sizeof(i_matrix)); + if (rc < 0) + fprintf(stderr, "write,errno = %ld, %d\n", rc, errno); + + puts("read results"); + do { + rc = read(fd, &r_matrix, sizeof(r_matrix)); + } while (rc < (int)sizeof(r_matrix)); + printf(" \r\n Host : Linux : Printing results \r\n"); + matrix_print(&r_matrix); } /* @@ -106,6 +100,7 @@ void matrix_mult(int ntimes) */ void send_shutdown(int fd) { + struct _matrix i_matrix[2]; memset(i_matrix, SHUTDOWN_MSG, sizeof(struct _matrix)); if (write(fd, i_matrix, sizeof(i_matrix)) < 0) perror("write SHUTDOWN_MSG\n"); @@ -125,7 +120,7 @@ void print_help(void) int main(int argc, char *argv[]) { int ntimes = 1; - int opt, ret; + int opt, ret, fd, charfd = -1; char rpmsg_dev[NAME_MAX] = "virtio0.rpmsg-openamp-demo-channel.-1.0"; char rpmsg_ctrl_dev_name[NAME_MAX] = "virtio0.rpmsg_ctrl.0.0"; char rpmsg_char_name[16]; @@ -199,7 +194,10 @@ int main(int argc, char *argv[]) } PR_DBG("matrix_mult(%d)\n", ntimes); - matrix_mult(ntimes); + for (int i = 0; i < ntimes; i++) { + matrix_mult(fd); + printf("End of Matrix multiplication demo Round %d\n", i); + } send_shutdown(fd); close(fd); diff --git a/examples/linux/rpmsg-proxy-app/proxy_app.c b/examples/linux/rpmsg-proxy-app/proxy_app.c index 0ab8268..b574560 100644 --- a/examples/linux/rpmsg-proxy-app/proxy_app.c +++ b/examples/linux/rpmsg-proxy-app/proxy_app.c @@ -29,8 +29,6 @@ struct _proxy_data { }; static struct _proxy_data *proxy; -char fw_dst_path[] = "/lib/firmware/image_rpc_demo"; -char sbuf[512]; int handle_open(struct _sys_rpc *rpc) { From ec48eb9340af20fc156b32c3c27eba01044f4375 Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Wed, 12 Jul 2023 14:10:40 -0500 Subject: [PATCH 08/10] examples: rpmsg-mat-mul: Only send 32bit shutdown message Signed-off-by: Andrew Davis --- examples/linux/rpmsg-mat-mul/mat_mul_demo.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/examples/linux/rpmsg-mat-mul/mat_mul_demo.c b/examples/linux/rpmsg-mat-mul/mat_mul_demo.c index 61db36a..bf137cf 100644 --- a/examples/linux/rpmsg-mat-mul/mat_mul_demo.c +++ b/examples/linux/rpmsg-mat-mul/mat_mul_demo.c @@ -10,6 +10,7 @@ */ #include +#include #include #include #include @@ -94,15 +95,11 @@ void matrix_mult(int fd) matrix_print(&r_matrix); } -/* - * Probably an overkill to memset(.., sizeof(struct _matrix)) as - * the firmware looks for SHUTDOWN_MSG in the first 32 bits. - */ +/* The firmware looks for SHUTDOWN_MSG in the first 32 bits */ void send_shutdown(int fd) { - struct _matrix i_matrix[2]; - memset(i_matrix, SHUTDOWN_MSG, sizeof(struct _matrix)); - if (write(fd, i_matrix, sizeof(i_matrix)) < 0) + uint32_t msg = SHUTDOWN_MSG; + if (write(fd, &msg, sizeof(msg)) < 0) perror("write SHUTDOWN_MSG\n"); } From 96b4529fe800171c634ce9902c20eac4d621925a Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Wed, 12 Jul 2023 14:29:32 -0500 Subject: [PATCH 09/10] examples: rpmsg-mat-mul: Cleanup printouts and remove debug statements Signed-off-by: Andrew Davis --- examples/linux/rpmsg-mat-mul/mat_mul_demo.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/linux/rpmsg-mat-mul/mat_mul_demo.c b/examples/linux/rpmsg-mat-mul/mat_mul_demo.c index bf137cf..a4b3014 100644 --- a/examples/linux/rpmsg-mat-mul/mat_mul_demo.c +++ b/examples/linux/rpmsg-mat-mul/mat_mul_demo.c @@ -24,7 +24,6 @@ #define RPMSG_BUS_SYS "/sys/bus/rpmsg" -#define PR_DBG(fmt, args ...) printf("%s():%u "fmt, __func__, __LINE__, ##args) #define SHUTDOWN_MSG 0xEF56A55A #define MATRIX_SIZE 6 @@ -82,15 +81,16 @@ void matrix_mult(int fd) /* Generate two random matrices */ generate_matrices(2, MATRIX_SIZE, i_matrix); - printf("write rpmsg: %lu bytes\n", sizeof(i_matrix)); + printf("Sending RPMSG: %lu bytes\n", sizeof(i_matrix)); ssize_t rc = write(fd, i_matrix, sizeof(i_matrix)); if (rc < 0) fprintf(stderr, "write,errno = %ld, %d\n", rc, errno); - puts("read results"); do { rc = read(fd, &r_matrix, sizeof(r_matrix)); } while (rc < (int)sizeof(r_matrix)); + printf("Received RPMSG: %lu bytes\n", rc); + printf(" \r\n Host : Linux : Printing results \r\n"); matrix_print(&r_matrix); } @@ -128,7 +128,7 @@ int main(int argc, char *argv[]) char ept_dev_name[16]; char ept_dev_path[32]; - printf("\r\n Matrix multiplication demo start \r\n"); + printf("Matrix multiplication demo start\n"); lookup_channel(rpmsg_dev, &eptinfo); while ((opt = getopt(argc, argv, "d:n:s:e:")) != -1) { @@ -170,7 +170,7 @@ int main(int argc, char *argv[]) } /* Create endpoint from rpmsg char driver */ - PR_DBG("app_rpmsg_create_ept: %s[src=%#x,dst=%#x]\n", + printf("Creating RPMSG endpoint with name: %s, src=%#x, dst=%#x\n", eptinfo.name, eptinfo.src, eptinfo.dst); ret = app_rpmsg_create_ept(charfd, &eptinfo); if (ret) { @@ -190,10 +190,10 @@ int main(int argc, char *argv[]) return -1; } - PR_DBG("matrix_mult(%d)\n", ntimes); + printf("Start of Matrix multiplication demo with %d rounds\n", ntimes); for (int i = 0; i < ntimes; i++) { matrix_mult(fd); - printf("End of Matrix multiplication demo Round %d\n", i); + printf("End of Matrix multiplication demo round %d\n", i); } send_shutdown(fd); From d6c88aa5d94ba4e59aa79e0c4a38b420bbb20c71 Mon Sep 17 00:00:00 2001 From: Andrew Davis Date: Wed, 19 Jul 2023 13:56:11 -0500 Subject: [PATCH 10/10] examples: linux: Close file on error in bind_rpmsg_chrdev The file should be closed when returning from this function in the error path. Fix this here. Signed-off-by: Andrew Davis --- examples/linux/common/common.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/linux/common/common.c b/examples/linux/common/common.c index 75605aa..ae086bc 100644 --- a/examples/linux/common/common.c +++ b/examples/linux/common/common.c @@ -84,6 +84,7 @@ int bind_rpmsg_chrdev(const char *rpmsg_dev_name) if (ret < 0) { fprintf(stderr, "Failed to write %s to %s, %s\n", rpmsg_chdrv, fpath, strerror(errno)); + close(fd); return -EINVAL; } close(fd); @@ -101,6 +102,7 @@ int bind_rpmsg_chrdev(const char *rpmsg_dev_name) if (ret < 0) { fprintf(stderr, "Failed to write %s to %s, %s\n", rpmsg_dev_name, fpath, strerror(errno)); + close(fd); return -EINVAL; } close(fd);