lib/common.c
changeset 1826 ec6223c3b7ec
parent 1497 c9308eb34c0e
child 1959 656f114153c2
equal deleted inserted replaced
1825:65781b048a47 1826:ec6223c3b7ec
    69 #define MAX_PATH_LEN 64
    69 #define MAX_PATH_LEN 64
    70 
    70 
    71 ec_master_t *ecrt_open_master(unsigned int master_index)
    71 ec_master_t *ecrt_open_master(unsigned int master_index)
    72 {
    72 {
    73     char path[MAX_PATH_LEN];
    73     char path[MAX_PATH_LEN];
    74     ec_master_t *master;
    74     ec_master_t *master = NULL;
       
    75     ec_ioctl_module_t module_data;
    75 
    76 
    76     master = malloc(sizeof(ec_master_t));
    77     master = malloc(sizeof(ec_master_t));
    77     if (!master) {
    78     if (!master) {
    78         fprintf(stderr, "Failed to allocate memory.\n");
    79         fprintf(stderr, "Failed to allocate memory.\n");
    79         return 0;
    80         return 0;
    85     snprintf(path, MAX_PATH_LEN - 1, "/dev/EtherCAT%u", master_index);
    86     snprintf(path, MAX_PATH_LEN - 1, "/dev/EtherCAT%u", master_index);
    86 
    87 
    87     master->fd = open(path, O_RDWR);
    88     master->fd = open(path, O_RDWR);
    88     if (master->fd == -1) {
    89     if (master->fd == -1) {
    89         fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
    90         fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
    90         free(master);
    91         goto out_free;
    91         return 0;
    92     }
       
    93 
       
    94     if (ioctl(master->fd, EC_IOCTL_MODULE, &module_data) < 0) {
       
    95         fprintf(stderr, "Failed to get module information from %s: %s\n",
       
    96                 path, strerror(errno));
       
    97         goto out_close;
       
    98     }
       
    99 
       
   100     if (module_data.ioctl_version_magic != EC_IOCTL_VERSION_MAGIC) {
       
   101         fprintf(stderr, "ioctl() version magic is differing:"
       
   102                 " %s: %u, libethercat: %u.\n",
       
   103                 path, module_data.ioctl_version_magic,
       
   104                 EC_IOCTL_VERSION_MAGIC);
       
   105         goto out_close;
    92     }
   106     }
    93 
   107 
    94     return master;
   108     return master;
       
   109 
       
   110 out_close:
       
   111     close(master->fd);
       
   112 out_free:
       
   113     free(master);
       
   114     return 0;
    95 }
   115 }
    96 
   116 
    97 /*****************************************************************************/
   117 /*****************************************************************************/
    98 
   118 
    99 void ecrt_release_master(ec_master_t *master)
   119 void ecrt_release_master(ec_master_t *master)