/*
 * XDDP-based RT/NRT threads communication demo.
 *
 * Real-time Xenomai threads and regular Linux threads may want to
 * exchange data in a way that does not require the former to leave
 * the real-time domain (i.e. secondary mode). Message pipes - as
 * implemented by the RTDM-based XDDP protocol - are provided for this
 * purpose.
 *
 * On the Linux domain side, pseudo-device files named /dev/rtp<minor>
 * give regular POSIX threads access to non real-time communication
 * endpoints, via the standard character-based I/O interface. On the
 * Xenomai domain side, sockets may be bound to XDDP ports, which act
 * as proxies to send and receive data to/from the associated
 * pseudo-device files. Ports and pseudo-device minor numbers are
 * paired, meaning that e.g. port 7 will proxy the traffic for
 * /dev/rtp7. Therefore, port numbers may range from 0 to
 * CONFIG_XENO_OPT_PIPE_NRDEV - 1.
 *
 * All data sent through a bound/connected XDDP socket via sendto(2) or
 * write(2) will be passed to the peer endpoint in the Linux domain,
 * and made available for reading via the standard read(2) system
 * call. Conversely, all data sent using write(2) through the non
 * real-time endpoint will be conveyed to the real-time socket
 * endpoint, and made available to the recvfrom(2) or read(2) system
 * calls.
 *
 * Both threads can use the bi-directional data path to send and
 * receive datagrams in a FIFO manner, as illustrated by the simple
 * echoing process implemented by this program.
 *
 * realtime_thread------------------------------>-------+
 *   =>  get socket                                     |
 *   =>  bind socket to port 0                          v
 *   =>  write traffic to NRT domain via sendto()       |
 *   =>  read traffic from NRT domain via recvfrom() <--|--+
 *                                                      |  |
 * regular_thread---------------------------------------+  |
 *   =>  open /dev/rtp0                                 |  ^
 *   =>  read traffic from RT domain via read()         |  |
 *   =>  echo traffic back to RT domain via write()     +--+
 *
 * See Makefile in this directory for build directives.
 *
 * NOTE: XDDP is a replacement for the legacy RT_PIPE interface
 * available from the native skin until Xenomai 3.
 */
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <malloc.h>
#include <pthread.h>
#include <fcntl.h>
#include <errno.h>
#include <rtdk.h>
#include <rtdm/rtipc.h>

pthread_t rt, nrt;

#define XDDP_PORT 0    /* [0..CONFIG-XENO_OPT_PIPE_NRDEV - 1] */

static const char *msg[] = {
    "Surfing With The Alien",
    "Lords of Karma",
    "Banana Mango",
    "Psycho Monkey",
    "Luminous Flesh Giants",
    "Moroccan Sunset",
    "Satch Boogie",
    "Flying In A Blue Dream",
    "Ride",
    "Summer Song",
    "Speed Of Light",
    "Crystal Planet",
    "Raspberry Jam Delta-V",
    "Champagne?",
    "Clouds Race Across The Sky",
    "Engines Of Creation"
};

static void fail(const char *reason)
{
    perror(reason);
    exit(EXIT_FAILURE);
}

static void *realtime_thread(void *arg)
{
    struct sockaddr_ipc saddr;
    int ret, s, n = 0, len;
    struct timespec ts;
    size_t poolsz;
    char buf[128];

/*
     * Get a datagram socket to bind to the RT endpoint. Each
     * endpoint is represented by a port number within the XDDP
     * protocol namespace.
     */
    s = socket(AF_RTIPC, SOCK_DGRAM, IPCPROTO_XDDP);
    if (s < 0) {
        perror("socket");
        exit(EXIT_FAILURE);
    }

/*
     * Set a local 16k pool for the RT endpoint. Memory needed to
     * convey datagrams will be pulled from this pool, instead of
     * Xenomai's system pool.
     */
    poolsz = 16384; /* bytes */
    ret = setsockopt(s, SOL_XDDP, XDDP_POOLSZ,
             &poolsz, sizeof(poolsz));
    if (ret)
        fail("setsockopt");

/*
     * Bind the socket to the port, to setup a proxy to channel
     * traffic to/from the Linux domain.
     *
     * saddr.sipc_port specifies the port number to use.
     */
    memset(&saddr, 0, sizeof(saddr));
    saddr.sipc_family = AF_RTIPC;
    saddr.sipc_port = XDDP_PORT;
    ret = bind(s, (struct sockaddr *)&saddr, sizeof(saddr));
    if (ret)
        fail("bind");

for (;;) {
        len = strlen(msg[n]);
        /*
         * Send a datagram to the NRT endpoint via the proxy.
         * We may pass a NULL destination address, since a
         * bound socket is assigned a default destination
         * address matching the binding address (unless
         * connect(2) was issued before bind(2), in which case
         * the former would prevail).
         */
        ret = sendto(s, msg[n], len, 0, NULL, 0);
        if (ret != len)
            fail("sendto");

rt_printf("%s: sent %d bytes, \"%.*s\"\n",
              __FUNCTION__, ret, ret, msg[n]);

/* Read back packets echoed by the regular thread */
        ret = recvfrom(s, buf, sizeof(buf), 0, NULL, 0);
        if (ret <= 0)
            fail("recvfrom");

rt_printf("   => \"%.*s\" echoed by peer\n", ret, buf);

n = (n + 1) % (sizeof(msg) / sizeof(msg[0]));
        /*
         * We run in full real-time mode (i.e. primary mode),
         * so we have to let the system breathe between two
         * iterations.
         */
        ts.tv_sec = 0;
        ts.tv_nsec = 500000000; /* 500 ms */
        clock_nanosleep(CLOCK_REALTIME, 0, &ts, NULL);
    }

return NULL;
}

static void *regular_thread(void *arg)
{
    char buf[128], *devname;
    int fd, ret;

if (asprintf(&devname, "/dev/rtp%d", XDDP_PORT) < 0)
        fail("asprintf");

fd = open(devname, O_RDWR);
    free(devname);
    if (fd < 0)
        fail("open");

for (;;) {
        /* Get the next message from realtime_thread. */
        ret = read(fd, buf, sizeof(buf));
        if (ret <= 0)
            fail("read");

/* Echo the message back to realtime_thread. */
        ret = write(fd, buf, ret);
        if (ret <= 0)
            fail("write");
    }

return NULL;
}

static void cleanup_upon_sig(int sig)
{
    pthread_cancel(rt);
    pthread_cancel(nrt);
    signal(sig, SIG_DFL);
    pthread_join(rt, NULL);
    pthread_join(nrt, NULL);
}

int main(int argc, char **argv)
{
    struct sched_param rtparam = { .sched_priority = 42 };
    pthread_attr_t rtattr, regattr;
    sigset_t mask, oldmask;

mlockall(MCL_CURRENT | MCL_FUTURE);

sigemptyset(&mask);
    sigaddset(&mask, SIGINT);
    signal(SIGINT, cleanup_upon_sig);
    sigaddset(&mask, SIGTERM);
    signal(SIGTERM, cleanup_upon_sig);
    sigaddset(&mask, SIGHUP);
    signal(SIGHUP, cleanup_upon_sig);
    pthread_sigmask(SIG_BLOCK, &mask, &oldmask);

/*
     * This is a real-time compatible printf() package from
     * Xenomai's RT Development Kit (RTDK), that does NOT cause
     * any transition to secondary (i.e. non real-time) mode when
     * writing output.
     */
    rt_print_auto_init(1);

pthread_attr_init(&rtattr);
    pthread_attr_setdetachstate(&rtattr, PTHREAD_CREATE_JOINABLE);
    pthread_attr_setinheritsched(&rtattr, PTHREAD_EXPLICIT_SCHED);
    pthread_attr_setschedpolicy(&rtattr, SCHED_FIFO);
    pthread_attr_setschedparam(&rtattr, &rtparam);

errno = pthread_create(&rt, &rtattr, &realtime_thread, NULL);
    if (errno)
        fail("pthread_create");

pthread_attr_init(&regattr);
    pthread_attr_setdetachstate(&regattr, PTHREAD_CREATE_JOINABLE);
    pthread_attr_setinheritsched(&regattr, PTHREAD_EXPLICIT_SCHED);
    pthread_attr_setschedpolicy(&regattr, SCHED_OTHER);

errno = pthread_create(&nrt, &regattr, &regular_thread, NULL);
    if (errno)
        fail("pthread_create");

sigsuspend(&oldmask);

return 0;
}

################################################################

测试结果:

realtime_thread: sent 21 bytes, "Raspberry Jam Delta-V"                         
   => "Raspberry Jam Delta-V" echoed by peer                                    
realtime_thread: sent 10 bytes, "Champagne?"                                    
   => "Champagne?" echoed by peer                                               
realtime_thread: sent 26 bytes, "Clouds Race Across The Sky"                    
   => "Clouds Race Across The Sky" echoed by peer                               
realtime_thread: sent 19 bytes, "Engines Of Creation"                           
   => "Engines Of Creation" echoed by peer                                      
realtime_thread: sent 22 bytes, "Surfing With The Alien"                        
   => "Surfing With The Alien" echoed by peer                                   
realtime_thread: sent 14 bytes, "Lords of Karma"                                
   => "Lords of Karma" echoed by peer                                           
realtime_thread: sent 12 bytes, "Banana Mango"                                  
   => "Banana Mango" echoed by peer                                             
realtime_thread: sent 13 bytes, "Psycho Monkey"                                 
   => "Psycho Monkey" echoed by peer                                            
realtime_thread: sent 21 bytes, "Luminous Flesh Giants"                         
   => "Luminous Flesh Giants" echoed by peer                                    
realtime_thread: sent 15 bytes, "Moroccan Sunset"                               
   => "Moroccan Sunset" echoed by peer                                          
realtime_thread: sent 12 bytes, "Satch Boogie"                                  
   => "Satch Boogie" echoed by peer                                             
realtime_thread: sent 22 bytes, "Flying In A Blue Dream"                        
   => "Flying In A Blue Dream" echoed by peer                                   
realtime_thread: sent 4 bytes, "Ride"                                           
   => "Ride" echoed by peer                                                     
realtime_thread: sent 11 bytes, "Summer Song"                                   
   => "Summer Song" echoed by peer                                              
realtime_thread: sent 14 bytes, "Speed Of Light"                                
   => "Speed Of Light" echoed by peer                                           
realtime_thread: sent 14 bytes, "Crystal Planet"                                
   => "Crystal Planet" echoed by peer                                           
realtime_thread: sent 21 bytes, "Raspberry Jam Delta-V"                         
   => "Raspberry Jam Delta-V" echoed by peer                                    
realtime_thread: sent 10 bytes, "Champagne?"                                    
   => "Champagne?" echoed by peer                                               
realtime_thread: sent 26 bytes, "Clouds Race Across The Sky"                    
   => "Clouds Race Across The Sky" echoed by peer                               
realtime_thread: sent 19 bytes, "Engines Of Creation"                           
   => "Engines Of Creation" echoed by peer                                      
realtime_thread: sent 22 bytes, "Surfing With The Alien"                        
   => "Surfing With The Alien" echoed by peer                                   
realtime_thread: sent 14 bytes, "Lords of Karma"                                
   => "Lords of Karma" echoed by peer                                           
realtime_thread: sent 12 bytes, "Banana Mango"                                  
   => "Banana Mango" echoed by peer                                             
realtime_thread: sent 13 bytes, "Psycho Monkey"                                 
   => "Psycho Monkey" echoed by peer                                            
realtime_thread: sent 21 bytes, "Luminous Flesh Giants"                         
   => "Luminous Flesh Giants" echoed by peer                                    
realtime_thread: sent 15 bytes, "Moroccan Sunset"                               
   => "Moroccan Sunset" echoed by peer                                          
realtime_thread: sent 12 bytes, "Satch Boogie"                                  
   => "Satch Boogie" echoed by peer                                             
realtime_thread: sent 22 bytes, "Flying In A Blue Dream"                        
   => "Flying In A Blue Dream" echoed by peer                                   
realtime_thread: sent 4 bytes, "Ride"                                           
   => "Ride" echoed by peer                                                     
realtime_thread: sent 11 bytes, "Summer Song"                                   
   => "Summer Song" echoed by peer                                              
realtime_thread: sent 14 bytes, "Speed Of Light"                                
   => "Speed Of Light" echoed by peer                                           
realtime_thread: sent 14 bytes, "Crystal Planet"                                
   => "Crystal Planet" echoed by peer                                           
realtime_thread: sent 21 bytes, "Raspberry Jam Delta-V"                         
   => "Raspberry Jam Delta-V" echoed by peer                                    
realtime_thread: sent 10 bytes, "Champagne?"                                    
   => "Champagne?" echoed by peer                                               
realtime_thread: sent 26 bytes, "Clouds Race Across The Sky"                    
   => "Clouds Race Across The Sky" echoed by peer                               
realtime_thread: sent 19 bytes, "Engines Of Creation"                           
   => "Engines Of Creation" echoed by peer                                      
realtime_thread: sent 22 bytes, "Surfing With The Alien"                        
   => "Surfing With The Alien" echoed by peer

linux 域和xenomai 实时域之间的交互的更多相关文章

  1. Linux ln命令:在文件之间建立链接(硬链接和软链接)详解版1

    Linux ln命令:在文件之间建立链接(硬链接和软链接)详解版 < Linux创建文件及修改文件时间戳(touch命令)Linux复制文件和目录(cp命令) > <Linux就该这 ...

  2. python基础之类和对象、对象之间的交互、类名称空间与对象/实例名称空间

    一 面向对象初识 Python要么是面向过程要么是面向对象. 概念及优缺点: 面向过程的程序设计的核心是过程,过程即解决问题的步骤,面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东 ...

  3. 第四节:Vuejs组件及组件之间的交互

    一. 组件及其交互 1.组件的注册 (1).全局注册 Vue.component('组件名称', { }) 第1个参数是标签名称,第2个参数是一个选项对象. 选项参数包括 data:必须是一个func ...

  4. 设计模式系列之中介者模式(Mediator Pattern)——协调多个对象之间的交互

    说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...

  5. python基础--面向对象基础(类与对象、对象之间的交互和组合、面向对象的命名空间、面向对象的三大特性等)

    python基础--面向对象 (1)面向过程VS面向对象 面向过程的程序设计的核心是过程(流水线式思维),过程即解决问题的步骤,面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西. ...

  6. 我的Android第五章:通过Intent实现活动与活动之间的交互

    Intent在活动的操作 作用: Itent是Android程序中各个组件直接交换的一个重要方式可以指定当前组件要执行任务同时也可以给各个组件直接进行数据交互              同时Inten ...

  7. Fragments之间的交互(实现参数传递)

    Fragments之间的交互(实现参数传递) 日常开发中,通常Fragments之间可能需要交互,比如基于用户事件改变Fragment的内容.所有Fragment之间的交互需要通过他们关联的Activ ...

  8. AngularJs-指令和指令之间的交互(动感超人)

    前言: 上节我们学习到了指令和控制器之间的交互,通过给指令添加动作,调用了控制器中的方法.本节我们学习指令和指令之间是如何交互的,我们通过一个小游戏来和大家一起学习,听大漠老师说这是国外的人写的dem ...

  9. OC和JS之间的交互

    OC和JS之间的交互 目录 对OC和JS之间交互的理解 JS调用OC OC调用JS 对OC和JS之间交互的理解 JS调用OC JS文件 function sendCommand(cmd,param){ ...

随机推荐

  1. Hibernate 延迟载入

    一.延迟载入定义                 延迟载入,也叫懒载入,它是Hibernate为提高程序运行效率而提供的一种机制,即当仅仅有真正使用该对象的数据时才会创建. 说白了,所谓的延迟载入不是 ...

  2. CentOS6.4下Samba服务器的安装与配置

    一.先恶狠狠地吐槽一下: 这篇随笔真是让我折腾了2天2夜才敢下笔写!!!为什么呢?之前是通过去Samba的官网下载的源码包,也就是.tar.gz来进行安装配置,不过这个让我折腾来折腾去就是没折腾出结果 ...

  3. linux 的空命令:(冒号)

    php里面又“空操作”这个东西,于是想一想linux的命令中是否有“空命令”这种东西,搜索一下,结果发现真的有这个东西存在 -------:) 冒号 : 就是空命令.即什么也不做,是一个命令占位符 # ...

  4. Cognos如何开启CJAP认证程序日志

    步骤: 1:修改ipfaaaclientconfig.xml.sample为ipfclientconfig.xml,修改<param name="File" value=&q ...

  5. Python - 带参数的方法

    import math class Point: def move(self, x, y): self.x = x self.y = y def reset(self): self.move(0, 0 ...

  6. es5 - array - reverse

    /** * 描述:也就是数组元素反转 * 使用:arr.reverse() */ var a = [1,2,3]; console.log(a.reverse()); /** * 一个经典的问题:如何 ...

  7. Android Studio集成SVN报错:can&#39;t use subversion command line client : svn

    Android Studio集成SVN插件,check out出代码后.每次开启都会在右上角出现例如以下错误: Can't use Subversion command line client: sv ...

  8. 中文latex参考文献格式

    中文latex参考文献格式 原来英文: \begin{thebibliography}{1} \bibitem{Ben-Shimon2015RecSys} D.~Ben-Shimon, A.~Tsik ...

  9. cocopods卸载、安装、重装等问题解决(转)

    今日在升级af库的时候,头脑发热把cocopods给卸载了,然后重装就出现了一些问题,主要是Mac ox s升级至10.11之后,好多命令都和以前不一样了,现在重新总结其安装步骤,如下: 一.全新安装 ...

  10. Linux-软件包管理-yum在线安装rz、sz工具

    yum search lrzsz yum方式查询 rpm -q lrzsz rpm方式查询 yum -y install lrzsz 安装 rpm -q lrzsz rpm方式查询 sz下载 rz上传