targets/Xenomai/plc_Xenomai_main.c
changeset 3315 5f9db9c6c69c
parent 3298 e6131894be1d
child 3398 7ca3924be865
equal deleted inserted replaced
3314:f4d89cecc6e6 3315:5f9db9c6c69c
     6 #include <unistd.h>
     6 #include <unistd.h>
     7 #include <string.h>
     7 #include <string.h>
     8 #include <time.h>
     8 #include <time.h>
     9 #include <signal.h>
     9 #include <signal.h>
    10 #include <stdlib.h>
    10 #include <stdlib.h>
       
    11 #include <errno.h>
    11 #include <sys/mman.h>
    12 #include <sys/mman.h>
    12 #include <sys/fcntl.h>
    13 #include <sys/fcntl.h>
    13 
    14 
    14 #include <alchemy/task.h>
    15 #include <alchemy/task.h>
    15 #include <alchemy/timer.h>
    16 #include <alchemy/timer.h>
   115     }
   116     }
   116 }
   117 }
   117 
   118 
   118 static unsigned long __debug_tick;
   119 static unsigned long __debug_tick;
   119 
   120 
   120 #define _LogAndReturnNull(text) \
   121 #define _Log(text, err) \
   121     {\
   122     {\
   122     	char mstr[256] = text " for ";\
   123         char mstr[256];\
   123         strncat(mstr, name, 255);\
   124         snprintf(mstr, 255, text " for %s (%d)", name, err);\
   124         LogMessage(LOG_CRITICAL, mstr, strlen(mstr));\
   125         LogMessage(LOG_CRITICAL, mstr, strlen(mstr));\
   125         return NULL;\
       
   126     }
   126     }
   127 
   127 
   128 void *create_RT_to_nRT_signal(char* name){
   128 void *create_RT_to_nRT_signal(char* name){
   129     int new_index = -1;
   129     int new_index = -1;
       
   130     int ret;
   130     RT_to_nRT_signal_t *sig;
   131     RT_to_nRT_signal_t *sig;
   131     char pipe_dev[64];
   132     char pipe_dev[64];
   132 
   133 
   133     /* find a free slot */
   134     /* find a free slot */
   134     for(int i=0; i < max_RT_to_nRT_signals; i++){
   135     for(int i=0; i < max_RT_to_nRT_signals; i++){
   139         }
   140         }
   140     }
   141     }
   141 
   142 
   142     /* fail if none found */
   143     /* fail if none found */
   143     if(new_index == -1) {
   144     if(new_index == -1) {
   144     	_LogAndReturnNull("Maximum count of RT-PIPE reached while creating pipe");
   145     	_Log("Maximum count of RT-PIPE reached while creating pipe", max_RT_to_nRT_signals);
       
   146         return NULL;
   145     }
   147     }
   146 
   148 
   147     /* create rt pipe */
   149     /* create rt pipe */
   148     if(rt_pipe_create(&sig->pipe, name, new_index, PIPE_SIZE) < 0){
   150     if(ret = rt_pipe_create(&sig->pipe, name, new_index, PIPE_SIZE) < 0){
   149     	_LogAndReturnNull("Failed opening real-time end of RT-PIPE");
   151     	_Log("Failed opening real-time end of RT-PIPE", ret);
       
   152         return NULL;
   150     }
   153     }
   151 
   154 
   152     /* open pipe's userland */
   155     /* open pipe's userland */
   153     snprintf(pipe_dev, 64, "/dev/rtp%d", new_index);
   156     snprintf(pipe_dev, 63, "/dev/rtp%d", new_index);
   154     if((sig->pipe_fd = open(pipe_dev, O_RDWR)) == -1){
   157     if((sig->pipe_fd = open(pipe_dev, O_RDWR)) == -1){
   155         rt_pipe_delete(&sig->pipe);
   158         rt_pipe_delete(&sig->pipe);
   156     	_LogAndReturnNull("Failed opening non-real-time end of RT-PIPE");
   159     	_Log("Failed opening non-real-time end of RT-PIPE", errno);
       
   160         return NULL;
   157     }
   161     }
   158 
   162 
   159     sig->used = 1;
   163     sig->used = 1;
   160     sig->name = name;
   164     sig->name = name;
   161 
   165 
   162     return sig;
   166     return sig;
   163 }
   167 }
   164 
   168 
   165 void delete_RT_to_nRT_signal(void* handle){
   169 void delete_RT_to_nRT_signal(void* handle){
       
   170     int ret;
   166     RT_to_nRT_signal_t *sig = (RT_to_nRT_signal_t*)handle;
   171     RT_to_nRT_signal_t *sig = (RT_to_nRT_signal_t*)handle;
       
   172     char *name = sig->name;
   167 
   173 
   168     if(!sig->used) return;
   174     if(!sig->used) return;
   169 
   175 
   170     rt_pipe_delete(&sig->pipe);
   176     if(ret = rt_pipe_delete(&sig->pipe) != 0){
   171 
   177     	_Log("Failed closing real-time end of RT-PIPE", ret);
   172     close(sig->pipe_fd);
   178     }
       
   179 
       
   180     if(close(sig->pipe_fd) != 0){
       
   181     	_Log("Failed closing non-real-time end of RT-PIPE", errno);
       
   182     }
   173 
   183 
   174     sig->used = 0;
   184     sig->used = 0;
   175 }
   185 }
   176 
   186 
   177 int wait_RT_to_nRT_signal(void* handle){
   187 int wait_RT_to_nRT_signal(void* handle){