lib/common.c
changeset 1963 6aaf77798445
parent 1959 656f114153c2
child 1975 8e173dddd183
equal deleted inserted replaced
1962:df9b00bda8dc 1963:6aaf77798445
    53 ec_master_t *ecrt_request_master(unsigned int master_index)
    53 ec_master_t *ecrt_request_master(unsigned int master_index)
    54 {
    54 {
    55     ec_master_t *master = ecrt_open_master(master_index);
    55     ec_master_t *master = ecrt_open_master(master_index);
    56     if (master) {
    56     if (master) {
    57         if (ecrt_master_reserve(master) < 0) {
    57         if (ecrt_master_reserve(master) < 0) {
    58             close(master->fd);
    58             ec_master_clear(master);
    59             free(master);
    59             free(master);
    60             master = 0;
    60             master = NULL;
    61         }
    61         }
    62     }
    62     }
    63 
    63 
    64     return master;
    64     return master;
    65 }
    65 }
    80         return 0;
    80         return 0;
    81     }
    81     }
    82 
    82 
    83     master->process_data = NULL;
    83     master->process_data = NULL;
    84     master->process_data_size = 0;
    84     master->process_data_size = 0;
       
    85     master->first_domain = NULL;
       
    86     master->first_config = NULL;
    85 
    87 
    86     snprintf(path, MAX_PATH_LEN - 1, "/dev/EtherCAT%u", master_index);
    88     snprintf(path, MAX_PATH_LEN - 1, "/dev/EtherCAT%u", master_index);
    87 
    89 
    88     master->fd = open(path, O_RDWR);
    90     master->fd = open(path, O_RDWR);
    89     if (master->fd == -1) {
    91     if (master->fd == -1) {
    90         fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
    92         fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno));
    91         goto out_free;
    93         goto out_clear;
    92     }
    94     }
    93 
    95 
    94     if (ioctl(master->fd, EC_IOCTL_MODULE, &module_data) < 0) {
    96     if (ioctl(master->fd, EC_IOCTL_MODULE, &module_data) < 0) {
    95         fprintf(stderr, "Failed to get module information from %s: %s\n",
    97         fprintf(stderr, "Failed to get module information from %s: %s\n",
    96                 path, strerror(errno));
    98                 path, strerror(errno));
    97         goto out_close;
    99         goto out_clear;
    98     }
   100     }
    99 
   101 
   100     if (module_data.ioctl_version_magic != EC_IOCTL_VERSION_MAGIC) {
   102     if (module_data.ioctl_version_magic != EC_IOCTL_VERSION_MAGIC) {
   101         fprintf(stderr, "ioctl() version magic is differing:"
   103         fprintf(stderr, "ioctl() version magic is differing:"
   102                 " %s: %u, libethercat: %u.\n",
   104                 " %s: %u, libethercat: %u.\n",
   103                 path, module_data.ioctl_version_magic,
   105                 path, module_data.ioctl_version_magic,
   104                 EC_IOCTL_VERSION_MAGIC);
   106                 EC_IOCTL_VERSION_MAGIC);
   105         goto out_close;
   107         goto out_clear;
   106     }
   108     }
   107 
   109 
   108     return master;
   110     return master;
   109 
   111 
   110 out_close:
   112 out_clear:
   111     close(master->fd);
   113     ec_master_clear(master);
   112 out_free:
       
   113     free(master);
   114     free(master);
   114     return 0;
   115     return 0;
   115 }
   116 }
   116 
   117 
   117 /*****************************************************************************/
   118 /*****************************************************************************/
   118 
   119 
   119 void ecrt_release_master(ec_master_t *master)
   120 void ecrt_release_master(ec_master_t *master)
   120 {
   121 {
   121     if (master->process_data)  {
   122     ec_master_clear(master);
   122         munmap(master->process_data, master->process_data_size);
       
   123     }
       
   124 
       
   125     close(master->fd);
       
   126     free(master);
   123     free(master);
   127 }
   124 }
   128 
   125 
   129 /*****************************************************************************/
   126 /*****************************************************************************/