Get device monitor notifications in Linux using C++

595 views Asked by At

I wrote a C++ program in Linux that notifies me whenever an external device is connected or disconnected from my system. I used netlink for the purpose and poll function call is used to wait for events. But now I am facing a major problem:

When I insert a pendrive to my system, the poll() is called many times(I happened to see it 4 times). I am storing the values to a std::map for later use. Now when I connect another device, I want the map to be cleared for storing the new information. How can I understand whether the output is related to a second device or not?

if(*prev_tok == "ACTION")
{
  cout << "Action = "<< *curr_tok << "\n";
  DeviceDetails["Action"] = *curr_tok;
}
else if(*prev_tok == "ID_FS_LABEL")
{
  cout << "LABEL = "<< *curr_tok << "\n";
  DeviceDetails["LABEL"] = *curr_tok;
}
else if(*prev_tok == "ID_FS_VERSION")
{
  cout << "FileSystem Version = "<< *curr_tok << "\n";
  DeviceDetails["FileSystem Version"] = *curr_tok;
}
else if(*prev_tok == "ID_FS_UUID")
{
  cout << "UUID = "<< *curr_tok << "\n";
  DeviceDetails["UUID"] = *curr_tok;
}
else if(*prev_tok == "ID_MODEL_ID")
{
  cout << "Model ID = "<< *curr_tok << "\n";
  DeviceDetails["Model ID"] = *curr_tok;
}
else if(*prev_tok == "ID_MODEL")
{
  cout << "Model Name = "<< *curr_tok << "\n";
  DeviceDetails["Model Name"] = *curr_tok;
}
else if(*prev_tok == "ID_SERIAL_SHORT")
{
  cout << "Serial Number = "<< *curr_tok << "\n";
  DeviceDetails["Serial Number"] = *curr_tok;
}
else if(*prev_tok == "ID_REVISION")
{
  cout << "Version = "<< *curr_tok << "\n";
  DeviceDetails["Version"] = *curr_tok;
}
else if(*prev_tok == "ID_VENDOR")
{
  cout << "Vendor Name = "<< *curr_tok << "\n";
  DeviceDetails["Vendor Name"] = *curr_tok;
}
else if(*prev_tok == "ID_VENDOR_ID")
{
  cout << "Vendor ID = "<< *curr_tok << "\n";
  DeviceDetails["Vendor ID"] = *curr_tok;
}
else if(*prev_tok == "MODALIAS")
{
  cout << "Description = "<< *curr_tok << "\n";
  DeviceDetails["Description"] = *curr_tok;
}
else if(*prev_tok == "DEVPATH")
{
  cout << "Device Path = "<< *curr_tok << "\n";
  DeviceDetails["Device Path"] = *curr_tok;
}
else if(*prev_tok == "DEVLINKS")
{
  cout << "Device Links = "<< *curr_tok << "\n";
  DeviceDetails["Device LINk"] = *curr_tok;
}
else if(*prev_tok == "ID_CDROM_MEDIA")
{
  cout << "CD INSERTED\n";
  DeviceDetails["CDROM_MEDIA"] = *curr_tok;
}
else if(*prev_tok == "ID_CDROM_DVD")
{
  cout << "DVD\n\n";
  DeviceDetails["CDROM_DVD"] = *curr_tok;
}
else if(*prev_tok == "ID_TYPE")
{
  cout << "Device Type = "<< *curr_tok << "\n";
  DeviceDetails["Device Type"] = *curr_tok;
}
0

There are 0 answers