Skip to content
This repository was archived by the owner on Dec 16, 2025. It is now read-only.

Commit a11eca0

Browse files
committed
Implement WiFi ping functionality and bootscreen
1 parent 57bc1fe commit a11eca0

File tree

6 files changed

+102
-40
lines changed

6 files changed

+102
-40
lines changed

common/src/main/java/dev/ultreon/devices/api/driver/WifiDriver.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,6 @@ default boolean isWireless() {
2525
CompletableFuture<Void> disconnect();
2626

2727
CompletableFuture<List<WiFiNetwork>> scan();
28+
29+
CompletableFuture<WiFiNetwork> ping();
2830
}

common/src/main/java/dev/ultreon/devices/core/ComputerScreen.java

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public class ComputerScreen extends Screen implements OperatingSystem {
8989
private static boolean loaded;
9090
private final Bios bios;
9191
private final Map<Class<?>, List<? extends Driver>> drivers = new HashMap<>();
92-
private final NetworkManagerImpl networkManager = new NetworkManagerImpl(this);
92+
private NetworkManagerImpl networkManager;
9393
private float tickTime;
9494

9595
public static List<Application> getApplicationsForFabric() {
@@ -153,6 +153,8 @@ public ComputerScreen(ComputerBlockEntity laptop, boolean worldLess) {
153153
videoInfo.setResolution(new CustomResolution(320, 240));
154154

155155
loadDrivers().thenRun(() -> {
156+
networkManager = new NetworkManagerImpl(this);
157+
156158
// Laptop data.
157159
appData = laptop.getApplicationData();
158160
systemData = laptop.getSystemData();
@@ -244,6 +246,11 @@ private CompletableFuture<Void> loadDrivers() {
244246
}
245247
}
246248
}
249+
try {
250+
Thread.sleep(5000);
251+
} catch (InterruptedException e) {
252+
return;
253+
}
247254
}, UltreonDevices.OS_EXECUTOR);
248255
}
249256

@@ -435,17 +442,20 @@ public void revalidateDisplay() {
435442
@Override
436443
public void tick() {
437444
try {
438-
networkManager.tick();
439-
bar.onTick();
440-
441-
for (Window<?> window : List.copyOf(windows)) {
442-
window.onTick();
443-
if (window.removed) {
444-
windows.remove(window);
445+
if (networkManager != null) networkManager.tick();
446+
if (bar != null) bar.onTick();
447+
448+
if (windows != null) {
449+
for (Window<?> window : List.copyOf(windows)) {
450+
window.onTick();
451+
if (window.removed) {
452+
windows.remove(window);
453+
}
445454
}
446455
}
447456

448-
FileBrowser.refreshList = false;
457+
if (booted)
458+
FileBrowser.refreshList = false;
449459
} catch (Exception e) {
450460
bsod(e);
451461
}
@@ -581,16 +591,16 @@ private void renderOS(@NotNull GuiGraphics graphics, int mouseX, int mouseY, flo
581591
tickTime += frameTime;
582592
int h = resolution.height();
583593
int w = resolution.width();
584-
graphics.fill(posX + w / 2 - 10, posY + h / 3 - 10, posX + w / 2 - 10 + 20, posY + h / 3 - 10 + 20, 0xFFFFFFFF);
585-
graphics.fill(posX + w / 2 - 10, posY + h / 3 + 12, posX + w / 2 - 10 + 20, posY + h / 3 + 12 + 4, 0xFFFFFFFF);
594+
graphics.fill(posX + w / 2 - 17, posY + h / 3 - 17, posX + w / 2 - 10 + 34, posY + h / 3 - 10 + 26, 0xFFFFFFFF);
595+
graphics.fill(posX + w / 2 - 17, posY + h / 3 - 10 + 30, posX + w / 2 - 10 + 34, posY + h / 3 - 10 + 34, 0xFFFFFFFF);
586596

587-
int v = (int) ((tickTime * 10) % 4);
597+
int v = (int) ((tickTime / 10) % 4);
588598
graphics.fill(posX + w / 2 - 5, posY + h / 3 + h / 3 - 5, posX + w / 2 - 5 + 4, posY + h / 3 + h / 3 - 5 + 4, v == 0 ? 0xFF303030 : 0xFF101010);
589599
graphics.fill(posX + w / 2 + 1, posY + h / 3 + h / 3 - 5, posX + w / 2 + 1 + 4, posY + h / 3 + h / 3 - 5 + 4, v == 1 ? 0xFF303030 : 0xFF101010);
590-
graphics.fill(posX + w / 2 - 5, posY + h / 3 + h / 3 + 1, posX + w / 2 - 5 + 4, posY + h / 3 + h / 3 + 1 + 4, v == 2 ? 0xFF303030 : 0xFF101010);
591-
graphics.fill(posX + w / 2 + 1, posY + h / 3 + h / 3 + 1, posX + w / 2 + 1 + 4, posY + h / 3 + h / 3 + 1 + 4, v == 3 ? 0xFF303030 : 0xFF101010);
600+
graphics.fill(posX + w / 2 - 5, posY + h / 3 + h / 3 + 1, posX + w / 2 - 5 + 4, posY + h / 3 + h / 3 + 1 + 4, v == 3 ? 0xFF303030 : 0xFF101010);
601+
graphics.fill(posX + w / 2 + 1, posY + h / 3 + h / 3 + 1, posX + w / 2 + 1 + 4, posY + h / 3 + h / 3 + 1 + 4, v == 2 ? 0xFF303030 : 0xFF101010);
592602

593-
graphics.drawString(getFont(), "Loading...", posX + w / 2 - 10, posY + h / 3 + 10, 0xFFFFFFFF);
603+
graphics.drawCenteredString(getFont(), "Loading...", posX + w / 2, posY + h / 3 + h / 3 + 20, 0xFFFFFFFF);
594604
return;
595605
}
596606

common/src/main/java/dev/ultreon/devices/core/jobs/Jobs.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import dev.ultreon.devices.api.device.Device;
55
import dev.ultreon.devices.api.device.DeviceOrigin;
66
import dev.ultreon.devices.api.device.DeviceSerializer;
7+
import dev.ultreon.devices.core.network.WiFiNetwork;
78
import dev.ultreon.devices.impl.hardware.gwifi.GWiFiPacket;
89
import dev.ultreon.devices.impl.hardware.gwifi.GWiFiPacket.GWiFiResponse;
910
import io.netty.handler.codec.DecoderException;
@@ -71,4 +72,15 @@ public class Jobs {
7172
.sendReply((data, buf) -> data.write(buf, (writeBuf, response) -> response.encode(writeBuf)))
7273
.receiveReply(buf -> HardwareResponse.read(buf, (RegistryFriendlyByteBuf buffer) -> (GWiFiPacket.Scan) GWiFiResponse.decode(buffer)))
7374
.build();
75+
public static final SimpleJob<HardwareRequest<Void>, HardwareResponse<GWiFiPacket.Ping>> HW_GWIFI_PING = new SimpleJobBuilder<HardwareRequest<Void>, HardwareResponse<GWiFiPacket.Ping>>()
76+
.sendData((request, buf) -> request.writeRemote(buf, (writeBuf, network) -> {}))
77+
.receiveData(buf -> HardwareRequest.read(buf, readBuf -> null))
78+
.reply((data, player) -> {
79+
DeviceOrigin deviceOrigin = data.origin().right().orElseThrow(() -> new DecoderException("Device origin is not a remote device"));
80+
Device device = deviceOrigin.locate(player.server, player.serverLevel(), player);
81+
return device.onHardwareRequest(data, GWiFiPacket.Ping.class);
82+
})
83+
.sendReply((data, buf) -> data.write(buf, (writeBuf, response) -> response.encode(writeBuf)))
84+
.receiveReply(buf -> HardwareResponse.read(buf, (RegistryFriendlyByteBuf buffer) -> (GWiFiPacket.Ping) GWiFiResponse.decode(buffer)))
85+
.build();
7486
}

common/src/main/java/dev/ultreon/devices/core/network/NetworkManagerImpl.java

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class NetworkManagerImpl implements NetworkManager {
1818
private final OperatingSystem system;
1919
private int pingTimer;
2020
private WifiStrength strength;
21+
private boolean pinging;
2122

2223
public NetworkManagerImpl(OperatingSystem system) {
2324
this.system = system;
@@ -42,35 +43,35 @@ public CompletableFuture<NetworkConnection> connect(String ssid) {
4243
}
4344

4445
public void tick() {
45-
if (++pingTimer >= DeviceConfig.PING_RATE.get()) {
46-
runPingTask();
46+
if (++pingTimer >= DeviceConfig.PING_RATE.get() && !pinging) {
47+
pinging = true;
48+
runPingTask().thenAccept(network -> {
49+
if (network != null) {
50+
strength = network.strength();
51+
} else {
52+
strength = WifiStrength.NONE;
53+
}
54+
pinging = false;
55+
}).exceptionally(throwable -> {
56+
system.logError("Failed to ping network!", throwable);
57+
pinging = false;
58+
return null;
59+
});
4760
pingTimer = 0;
4861
}
4962
}
5063

51-
private void runPingTask() {
52-
TaskPing task = new TaskPing(ComputerScreen.getPos());
53-
task.setCallback((tag, success) -> {
54-
if (success) {
55-
assert tag != null;
56-
int strength = tag.getInt("strength");
57-
switch (strength) {
58-
case 2 -> {
59-
this.strength = WifiStrength.LOW;
60-
}
61-
case 1 -> {
62-
this.strength = WifiStrength.MED;
63-
}
64-
case 0 -> {
65-
this.strength = WifiStrength.HIGH;
66-
}
67-
default -> {
68-
this.strength = WifiStrength.NONE;
69-
}
70-
}
64+
private CompletableFuture<WiFiNetwork> runPingTask() {
65+
try {
66+
WifiDriver[] drivers = system.getDrivers(WifiDriver.class);
67+
for (WifiDriver driver : drivers) {
68+
if (driver.isConnected())
69+
return driver.ping();
7170
}
72-
});
73-
TaskManager.sendTask(task);
71+
return CompletableFuture.completedFuture(null);
72+
} catch (Exception e) {
73+
return CompletableFuture.failedFuture(e);
74+
}
7475
}
7576

7677
public WifiStrength getStrength() {

common/src/main/java/dev/ultreon/devices/impl/driver/GenericWifiDriver.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ public CompletableFuture<List<WiFiNetwork>> scan() {
6868
});
6969
}
7070

71+
@Override
72+
public CompletableFuture<WiFiNetwork> ping() {
73+
return hardware.send(new GWiFiPacket(GWiFiPacket.Type.PING), UltreonDevicesClient.getInstance()).thenApply(packet -> {
74+
if (packet instanceof GWiFiPacket.Ping) return ((GWiFiPacket.Ping) packet).network();
75+
return null;
76+
});
77+
}
78+
7179
@Override
7280
public NetworkState getNetworkState() {
7381
return state;

common/src/main/java/dev/ultreon/devices/impl/hardware/gwifi/GWiFiPacket.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public enum Type {
4040
CONNECT(Connect::process, GWiFiPacket::sendConnect, GWiFiPacket::receiveConnect),
4141
DISCONNECT(Disconnect::process, GWiFiPacket::sendDisconnect, GWiFiPacket::receiveClose),
4242
INITIALIZE(Initialize::process, GWiFiPacket::sendInitialize, GWiFiPacket::receiveInitialize),
43-
SCAN(Scan::process, GWiFiPacket::sendScan, GWiFiPacket::receiveScan);
43+
SCAN(Scan::process, GWiFiPacket::sendScan, GWiFiPacket::receiveScan),
44+
PING(Ping::process, GWiFiPacket::sendPing, GWiFiPacket::receivePing);
4445
private final Function<RegistryFriendlyByteBuf, GWiFiResponse> processor;
4546
private final Function<GWiFiPacket, CompletableFuture<GWiFiResponse>> sender;
4647

@@ -78,6 +79,10 @@ private GWiFiResponse receiveScan(RegistryFriendlyByteBuf registryFriendlyByteBu
7879
return new Scan(registryFriendlyByteBuf.readList(WiFiNetwork::read));
7980
}
8081

82+
private GWiFiResponse receivePing(RegistryFriendlyByteBuf registryFriendlyByteBuf) {
83+
return new Ping(WiFiNetwork.read(registryFriendlyByteBuf));
84+
}
85+
8186
private GWiFiResponse receiveConnect(RegistryFriendlyByteBuf registryFriendlyByteBuf) {
8287
return new Connect(registryFriendlyByteBuf.readEnum(NetworkState.class));
8388
}
@@ -141,6 +146,18 @@ private CompletableFuture<GWiFiResponse> sendScan() {
141146
});
142147
}
143148

149+
private CompletableFuture<GWiFiResponse> sendPing() {
150+
UltreonDevicesClient client = UltreonDevicesClient.getInstance();
151+
RemoteDevice openDevice = client.getOpenDevice();
152+
int requestId = client.getJobs().requestId();
153+
return client.sendJob(Jobs.HW_GWIFI_PING, new HardwareRequest<>(requestId, KnownVendorIDs.UltreonStudios, KnownProductIDs.GWiFi, Either.left(openDevice.getOrigin()), null))
154+
.thenApply(response -> {
155+
client.getJobs().freeId(requestId);
156+
if (response == null) return null;
157+
return response.data();
158+
});
159+
}
160+
144161
private CompletableFuture<GWiFiResponse> sendConnect() {
145162
UltreonDevicesClient client = UltreonDevicesClient.getInstance();
146163
RemoteDevice openDevice = client.getOpenDevice();
@@ -242,4 +259,16 @@ public static Scan process(RegistryFriendlyByteBuf buffer) {
242259
return new Scan(networks);
243260
}
244261
}
262+
263+
public record Ping(WiFiNetwork network) implements GWiFiResponse {
264+
@Override
265+
public void encode(RegistryFriendlyByteBuf buffer) {
266+
buffer.writeEnum(Type.PING);
267+
network.write(buffer);
268+
}
269+
270+
public static Ping process(RegistryFriendlyByteBuf buffer) {
271+
return new Ping(WiFiNetwork.read(buffer));
272+
}
273+
}
245274
}

0 commit comments

Comments
 (0)