Android-cuttlefish cvd tool
usb_linux_netlink.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <string>
20#include <string_view>
21#include <unordered_map>
22
23#include <sys/types.h>
24
25// Helper class to parse netlink messages.
26
27// A message is a sequence of null-terminated strings as follows.
28// add@/devices/pci0000:00/0000:00:03.1/0000:02:00.0/0000:03:08.0/0000:04:00.1/usb4/4-1
29// ACTION=add
30// DEVPATH=/devices/pci0000:00/0000:00:03.1/0000:02:00.0/0000:03:08.0/0000:04:00.1/usb4/4-1
31// SUBSYSTEM=usb
32// MAJOR=189
33// MINOR=509
34// DEVNAME=bus/usb/004/126
35// DEVTYPE=usb_device
36// PRODUCT=18d1/4ee7/601
37// TYPE=0/0/0
38// BUSNUM=004
39// DEVNUM=126
40// SEQNUM=10453
41//
42// This class only capture key/value pairs, the "header@" is ignored.
44 public:
45 NetlinkMessage(const char* buffer, ssize_t len);
46
47 // Return the value for the given key. If the key is not found, return an empty string.
48 std::string attr(const std::string& key) const;
49
50 // Return true if the message has the given attribute with the given value.
51 bool has_attr(const std::string& key, std::string_view value) const;
52
53 private:
54 std::unordered_map<std::string, std::string> attrs_;
55};
Definition: usb_linux_netlink.h:43
bool has_attr(const std::string &key, std::string_view value) const
Definition: usb_linux_netlink.cpp:58
std::string attr(const std::string &key) const
Definition: usb_linux_netlink.cpp:53
NetlinkMessage(const char *buffer, ssize_t len)
Definition: usb_linux_netlink.cpp:21
std::unordered_map< std::string, std::string > attrs_
Definition: usb_linux_netlink.h:54