| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.os; |
| 17 |
|
| 18 |
import static android.system.OsConstants.MAP_SHARED; |
| 19 |
import static android.system.OsConstants.PROT_READ; |
| 20 |
|
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.Nullable; |
| 23 |
import android.annotation.SuppressLint; |
| 24 |
import android.system.ErrnoException; |
| 25 |
import android.system.Os; |
| 26 |
import android.util.Log; |
| 27 |
|
| 28 |
import com.android.internal.util.Preconditions; |
| 29 |
|
| 30 |
import java.io.FileDescriptor; |
| 31 |
import java.nio.ByteBuffer; |
| 32 |
import java.nio.DirectByteBuffer; |
| 33 |
import java.util.ArrayList; |
| 34 |
import java.util.List; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public final class HidlMemoryUtil { |
| 42 |
static private final String TAG = "HidlMemoryUtil"; |
| 43 |
|
| 44 |
private HidlMemoryUtil() { |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public static @NonNull |
| 57 |
HidlMemory byteArrayToHidlMemory(@NonNull byte[] input) { |
| 58 |
return byteArrayToHidlMemory(input, null); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public static @NonNull |
| 72 |
HidlMemory byteArrayToHidlMemory(@NonNull byte[] input, @Nullable String name) { |
| 73 |
Preconditions.checkNotNull(input); |
| 74 |
|
| 75 |
if (input.length == 0) { |
| 76 |
return new HidlMemory("ashmem", 0, null); |
| 77 |
} |
| 78 |
|
| 79 |
try (SharedMemory shmem = SharedMemory.create(name != null ? name : "", input.length)) { |
| 80 |
ByteBuffer buffer = shmem.mapReadWrite(); |
| 81 |
buffer.put(input); |
| 82 |
shmem.unmap(buffer); |
| 83 |
return sharedMemoryToHidlMemory(shmem); |
| 84 |
} catch (ErrnoException e) { |
| 85 |
throw new RuntimeException(e); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
public static @NonNull |
| 99 |
HidlMemory byteListToHidlMemory(@NonNull List<Byte> input) { |
| 100 |
return byteListToHidlMemory(input, null); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public static @NonNull |
| 114 |
HidlMemory byteListToHidlMemory(@NonNull List<Byte> input, @Nullable String name) { |
| 115 |
Preconditions.checkNotNull(input); |
| 116 |
|
| 117 |
if (input.isEmpty()) { |
| 118 |
return new HidlMemory("ashmem", 0, null); |
| 119 |
} |
| 120 |
|
| 121 |
try (SharedMemory shmem = SharedMemory.create(name != null ? name : "", input.size())) { |
| 122 |
ByteBuffer buffer = shmem.mapReadWrite(); |
| 123 |
for (Byte b : input) { |
| 124 |
buffer.put(b); |
| 125 |
} |
| 126 |
shmem.unmap(buffer); |
| 127 |
return sharedMemoryToHidlMemory(shmem); |
| 128 |
} catch (ErrnoException e) { |
| 129 |
throw new RuntimeException(e); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public static @NonNull |
| 141 |
byte[] hidlMemoryToByteArray(@NonNull HidlMemory mem) { |
| 142 |
Preconditions.checkNotNull(mem); |
| 143 |
Preconditions.checkArgumentInRange(mem.getSize(), 0L, (long) Integer.MAX_VALUE, |
| 144 |
"Memory size"); |
| 145 |
Preconditions.checkArgument(mem.getSize() == 0 || mem.getName().equals("ashmem"), |
| 146 |
"Unsupported memory type: %s", mem.getName()); |
| 147 |
|
| 148 |
if (mem.getSize() == 0) { |
| 149 |
return new byte[0]; |
| 150 |
} |
| 151 |
|
| 152 |
ByteBuffer buffer = getBuffer(mem); |
| 153 |
byte[] result = new byte[buffer.remaining()]; |
| 154 |
buffer.get(result); |
| 155 |
return result; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
@SuppressLint("ConcreteCollection") |
| 166 |
public static @NonNull |
| 167 |
ArrayList<Byte> hidlMemoryToByteList(@NonNull HidlMemory mem) { |
| 168 |
Preconditions.checkNotNull(mem); |
| 169 |
Preconditions.checkArgumentInRange(mem.getSize(), 0L, (long) Integer.MAX_VALUE, |
| 170 |
"Memory size"); |
| 171 |
Preconditions.checkArgument(mem.getSize() == 0 || mem.getName().equals("ashmem"), |
| 172 |
"Unsupported memory type: %s", mem.getName()); |
| 173 |
|
| 174 |
if (mem.getSize() == 0) { |
| 175 |
return new ArrayList<>(); |
| 176 |
} |
| 177 |
|
| 178 |
ByteBuffer buffer = getBuffer(mem); |
| 179 |
|
| 180 |
ArrayList<Byte> result = new ArrayList<>(buffer.remaining()); |
| 181 |
while (buffer.hasRemaining()) { |
| 182 |
result.add(buffer.get()); |
| 183 |
} |
| 184 |
return result; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
@NonNull public static HidlMemory sharedMemoryToHidlMemory(@Nullable SharedMemory shmem) { |
| 195 |
if (shmem == null) { |
| 196 |
return new HidlMemory("ashmem", 0, null); |
| 197 |
} |
| 198 |
return fileDescriptorToHidlMemory(shmem.getFileDescriptor(), shmem.getSize()); |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
@NonNull public static HidlMemory fileDescriptorToHidlMemory(@Nullable FileDescriptor fd, |
| 210 |
int size) { |
| 211 |
Preconditions.checkArgument(fd != null || size == 0); |
| 212 |
if (fd == null) { |
| 213 |
return new HidlMemory("ashmem", 0, null); |
| 214 |
} |
| 215 |
try { |
| 216 |
NativeHandle handle = new NativeHandle(Os.dup(fd), true); |
| 217 |
return new HidlMemory("ashmem", size, handle); |
| 218 |
} catch (ErrnoException e) { |
| 219 |
throw new RuntimeException(e); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
private static ByteBuffer getBuffer(@NonNull HidlMemory mem) { |
| 224 |
try { |
| 225 |
final int size = (int) mem.getSize(); |
| 226 |
|
| 227 |
if (size == 0) { |
| 228 |
return ByteBuffer.wrap(new byte[0]); |
| 229 |
} |
| 230 |
|
| 231 |
NativeHandle handle = mem.getHandle(); |
| 232 |
|
| 233 |
final long address = Os.mmap(0, size, PROT_READ, MAP_SHARED, handle.getFileDescriptor(), |
| 234 |
0); |
| 235 |
return new DirectByteBuffer(size, address, handle.getFileDescriptor(), () -> { |
| 236 |
try { |
| 237 |
Os.munmap(address, size); |
| 238 |
} catch (ErrnoException e) { |
| 239 |
Log.wtf(TAG, e); |
| 240 |
} |
| 241 |
}, true); |
| 242 |
} catch (ErrnoException e) { |
| 243 |
throw new RuntimeException(e); |
| 244 |
} |
| 245 |
} |
| 246 |
} |