xref: /aosp_15_r20/hardware/interfaces/automotive/can/1.0/default/libnetdevice/ethtool.cpp (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
1 /*
2  * Copyright (C) 2020 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 #include <libnetdevice/ethtool.h>
18 
19 #include "ifreqs.h"
20 
21 #include <linux/ethtool.h>
22 #include <linux/sockios.h>
23 
24 namespace android::netdevice::ethtool {
25 
getValue(std::string_view ifname,uint32_t command)26 std::optional<uint32_t> getValue(std::string_view ifname, uint32_t command) {
27     struct ethtool_value valueop = {};
28     valueop.cmd = command;
29 
30     auto ifr = ifreqs::fromName(ifname);
31     ifr.ifr_data = reinterpret_cast<caddr_t>(&valueop);
32 
33     if (!ifreqs::send(SIOCETHTOOL, ifr)) return std::nullopt;
34     return valueop.data;
35 }
36 
setValue(std::string_view ifname,uint32_t command,uint32_t value)37 bool setValue(std::string_view ifname, uint32_t command, uint32_t value) {
38     struct ethtool_value valueop = {};
39     valueop.cmd = command;
40     valueop.data = value;
41 
42     auto ifr = ifreqs::fromName(ifname);
43     ifr.ifr_data = reinterpret_cast<caddr_t>(&valueop);
44 
45     return ifreqs::send(SIOCETHTOOL, ifr);
46 }
47 
48 }  // namespace android::netdevice::ethtool
49