00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00026 #include "udev_control.h"
00027
00028 class_udev_control::class_udev_control(string id_)
00029 {
00030
00031 udev = udev_new();
00032 if (!udev)
00033 {
00034 cout<<"Can't create udev!"<<endl;
00035 return;
00036 }
00037
00038 id=id_;
00039 fd=-1;
00040 }
00041
00042 class_udev_control::~class_udev_control()
00043 {
00044 udev_unref(udev);
00045 }
00046
00047 bool class_udev_control::RegistryAction(string action,void (*callback)(string action,string node,void*data),void*data)
00048 {
00049 actions[action]=callback;
00050 user_data[action]=data;
00051
00052 return true;
00053 }
00054 bool class_udev_control::AddProperty(string name,string value)
00055 {
00056 to_upper(name);
00057 to_upper(value);
00058
00059
00060
00061 properties[name]=value;
00062 return true;
00063 }
00064
00065 bool class_udev_control::AddSubsystem(string subsystem)
00066 {
00067
00068 sub_systems.push_back(subsystem);
00069 return true;
00070 }
00071
00072 bool class_udev_control::SetUpMonitoring()
00073 {
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 mon = udev_monitor_new_from_netlink(udev, "udev");
00091
00092 for(uint i=0;i<sub_systems.size();i++)
00093 udev_monitor_filter_add_match_subsystem_devtype(mon,sub_systems[i].c_str(),NULL);
00094
00095 udev_monitor_enable_receiving(mon);
00096
00097
00098 fd = udev_monitor_get_fd(mon);
00099 return true;
00100 }
00101
00102 string class_udev_control::GetProperty(struct udev_device *dev_,string name)
00103 {
00104 string value;
00105
00106
00107 value=udev_device_get_property_value(dev,name.c_str());
00108
00109 return value;
00110 }
00111
00112 bool class_udev_control::FilterProperties(struct udev_device *dev_)
00113 {
00114 int num_properties_to_match=properties.size();
00115 int num_properties_match=0;
00116
00117
00118 struct udev_list_entry * sysattr_list;
00119
00120 sysattr_list=udev_device_get_properties_list_entry(dev_);
00121
00122 while(sysattr_list!=NULL)
00123 {
00124 string name=udev_list_entry_get_name(sysattr_list);
00125 string value=udev_list_entry_get_value(sysattr_list);
00126
00127 to_upper(name);
00128 to_upper(value);
00129
00130
00131
00132 properties_it=properties.find(name);
00133 if(properties_it!=properties.end())
00134 if(properties_it->second==value)
00135 {
00136
00137 num_properties_match++;
00138 }
00139 sysattr_list=udev_list_entry_get_next(sysattr_list);
00140 }
00141
00142
00143
00144
00145
00146
00147
00148
00149 return num_properties_match==num_properties_to_match;
00150 }
00151
00152 bool class_udev_control::EnumerateDevices()
00153 {
00154
00155 enumerate = udev_enumerate_new(udev);
00156
00157 for(uint i=0;i<sub_systems.size();i++)
00158 udev_enumerate_add_match_subsystem(enumerate,sub_systems[i].c_str());
00159
00160 udev_enumerate_scan_devices(enumerate);
00161 devices = udev_enumerate_get_list_entry(enumerate);
00162
00163
00164
00165
00166
00167
00168
00169
00170 string last_minor="0";
00171 string minor;
00172 udev_list_entry_foreach(dev_list_entry, devices)
00173 {
00174 const char *path;
00175 string lpath;
00176
00177
00178
00179 path = udev_list_entry_get_name(dev_list_entry);
00180 dev = udev_device_new_from_syspath(udev, path);
00181
00182 if(FilterProperties(dev))
00183 {
00184
00185
00186 minor=GetProperty(dev,"MINOR");
00187
00188 lpath=udev_device_get_devnode(dev);
00189
00190 if(minor>last_minor)
00191 device_list.push_back(lpath);
00192 else
00193 {
00194 vector<string>::iterator it;
00195 it=device_list.begin();
00196 device_list.insert(it,lpath);
00197 }
00198
00199 cout<<"Added device to local list, ID: "<<id<<" Path: "<<lpath<<endl;
00200
00201 last_minor=minor;
00202 }
00203
00204
00205
00206 udev_device_unref(dev);
00207 }
00208
00209
00210 udev_enumerate_unref(enumerate);
00211
00212 return true;
00213 }
00214
00215
00216 bool class_udev_control::Monitoring()
00217 {
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235 fd_set fds;
00236 struct timeval tv;
00237 int ret;
00238
00239 FD_ZERO(&fds);
00240 FD_SET(fd, &fds);
00241 tv.tv_sec = 0;
00242 tv.tv_usec = 0;
00243
00244 if(fd<0)
00245 {
00246 cout<<"Monitoring was not configured, please run SetUpMonitoring() before calling Monitoring()"<<endl;
00247 return false;
00248 }
00249
00250 ret = select(fd+1, &fds, NULL, NULL, &tv);
00251
00252
00253 if (ret > 0 && FD_ISSET(fd, &fds))
00254 {
00255
00256
00257 dev = udev_monitor_receive_device(mon);
00258 if (dev)
00259 {
00260
00261
00262 if(FilterProperties(dev))
00263 {
00264 actions_it=actions.find(udev_device_get_action(dev));
00265 if(actions_it!=actions.end())
00266 actions[udev_device_get_action(dev)](udev_device_get_action(dev),udev_device_get_devnode(dev),user_data[udev_device_get_action(dev)]);
00267 }
00268
00269 udev_device_unref(dev);
00270 }
00271 else
00272 cout<<"No Device from receive_device(). An error occured."<<endl;
00273 }
00274
00275 return true;
00276 }