• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 package com.android.server.wifi.nl80211;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 
22 import java.nio.ByteBuffer;
23 import java.util.Objects;
24 
25 /**
26  * Representation of a generic Netlink message header.
27  *
28  * See kernel/uapi/linux/genetlink.h
29  */
30 public class StructGenNlMsgHdr {
31     public static final int STRUCT_SIZE = 4;
32 
33     public final short command; // byte
34     public final byte version;
35     public final short reserved;
36 
StructGenNlMsgHdr(short command)37     public StructGenNlMsgHdr(short command) {
38         this.command = command;
39         this.version = 1;   // only V1 is used
40         this.reserved = 0;  // unused
41     }
42 
StructGenNlMsgHdr(short command, byte version, short reserved)43     private StructGenNlMsgHdr(short command, byte version, short reserved) {
44         this.command = command;
45         this.version = version;
46         this.reserved = reserved;
47     }
48 
49     /**
50      * Read a StructGenNlMsgHdr object from a ByteBuffer.
51      */
52     @Nullable
parse(@onNull final ByteBuffer byteBuffer)53     public static StructGenNlMsgHdr parse(@NonNull final ByteBuffer byteBuffer) {
54         if (byteBuffer == null || byteBuffer.remaining() < STRUCT_SIZE) {
55             return null;
56         }
57         // Assume that the byte buffer has already been set to native order
58         short command = (short) (byteBuffer.get() & 0xFF);
59         byte version = byteBuffer.get();
60         short reserved = byteBuffer.getShort();
61         return new StructGenNlMsgHdr(command, version, reserved);
62     }
63 
64     /**
65      * Write a StructGenNlMsgHdr object to a ByteBuffer.
66      */
pack(@onNull final ByteBuffer byteBuffer)67     public void pack(@NonNull final ByteBuffer byteBuffer) {
68         if (byteBuffer == null || byteBuffer.remaining() < STRUCT_SIZE) {
69             return;
70         }
71         // Assume that the byte buffer has already been set to native order
72         byteBuffer.put((byte) (command & 0xFF));
73         byteBuffer.put(version);
74         byteBuffer.putShort(reserved);
75     }
76 
77     @Override
equals(Object o)78     public boolean equals(Object o) {
79         if (o == this) return true;
80         if (o == null || !(o instanceof StructGenNlMsgHdr)) return false;
81         StructGenNlMsgHdr other = (StructGenNlMsgHdr) o;
82         return this.command == other.command
83                 && this.version == other.version
84                 && this.reserved == other.reserved;
85     }
86 
87     @Override
hashCode()88     public int hashCode() {
89         return Objects.hash(command, version, reserved);
90     }
91 
92     @Override
toString()93     public String toString() {
94         return "StructGenNlMsgHdr{ "
95                 + "command{" + command + "}, "
96                 + "version{" + version + "}, "
97                 + "reserved{" + reserved + "} "
98                 + "}";
99     }
100 }
101