Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CodenameOne/src/com/codename1/impl/CodenameOneImplementation.java
Original file line number Diff line number Diff line change
Expand Up @@ -7333,6 +7333,17 @@ public com.codename1.webrtc.spi.WebRtcBridge getWebRtcBridge() {
return null;
}

/// Returns the platform bridge used by `com.codename1.payment.terminal.tap.TapToPay`: the
/// Stripe Terminal SDK on a device, or the simulator's simulated phone. The default answers
/// `null`, which `TapToPay.isSupported()` reads as unsupported.
///
/// #### Returns
///
/// the Tap to Pay bridge, or null when unsupported
public com.codename1.payment.terminal.spi.TapToPayBridge getTapToPayBridge() {
return null;
}

/// Returns the platform bridge used by the `com.codename1.vpn` API to install and control VPN
/// configurations. Ports supporting VPN management override this; the default answers `null`.
///
Expand Down
192 changes: 192 additions & 0 deletions CodenameOne/src/com/codename1/io/link/ByteLink.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/*
* Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codename1.io.link;

import com.codename1.ui.CN;
import java.io.IOException;

/// A two-way byte connection to one device: a serial port, a TCP socket, a
/// USB endpoint pair, or anything else with bytes in and out.
///
/// Calls block, so a protocol driver runs them on a thread of its own and
/// hands results to the EDT with `CN.callSerially`. Every blocking call
/// refuses to run on the EDT, where it would freeze the app.
///
/// ```java
/// ByteLink link = new TcpLink("192.168.1.50", 9100);
/// new Thread(() -> {
/// try {
/// link.open();
/// link.write("Hello\n".getBytes("UTF-8"));
/// } catch (IOException err) {
/// CN.callSerially(() -> System.out.println("Printer unreachable: " + err));
/// } finally {
/// link.close();
/// }
/// }).start();
/// ```
public abstract class ByteLink {
/// Connects. Does nothing when already open.
///
/// #### Throws
///
/// - `IOException`: when the device cannot be reached
public abstract void open() throws IOException;

/// Whether [#open()] succeeded and [#close()] has not been called.
///
/// #### Returns
///
/// true while open
public abstract boolean isOpen();

/// Writes bytes, returning once they are handed to the device.
///
/// #### Parameters
///
/// - `data`: the buffer
///
/// - `offset`: where to start in it
///
/// - `length`: how many bytes
///
/// #### Throws
///
/// - `IOException`: when the link is closed or broken
public abstract void write(byte[] data, int offset, int length) throws IOException;

/// Reads what has arrived, waiting up to `timeout` milliseconds for the
/// first byte.
///
/// #### Parameters
///
/// - `buffer`: where to put the bytes
///
/// - `offset`: where to start in it
///
/// - `length`: at most how many bytes
///
/// - `timeout`: how long to wait, in milliseconds
///
/// #### Returns
///
/// the number of bytes read; 0 when nothing arrived in time
///
/// #### Throws
///
/// - `IOException`: when the link is closed or broken, including when
/// the other side closed it
public abstract int read(byte[] buffer, int offset, int length, int timeout) throws IOException;

/// Disconnects. Safe to call more than once, and from any thread; a read
/// blocked in another thread fails soon after.
public abstract void close();

/// Writes all of `data`.
///
/// #### Parameters
///
/// - `data`: the bytes
///
/// #### Throws
///
/// - `IOException`: when the link is closed or broken
public void write(byte[] data) throws IOException {
write(data, 0, data.length);
}

/// Reads one byte.
///
/// #### Parameters
///
/// - `timeout`: how long to wait, in milliseconds
///
/// #### Returns
///
/// the byte, 0 to 255; -1 when nothing arrived in time
///
/// #### Throws
///
/// - `IOException`: when the link is closed or broken
public int readByte(int timeout) throws IOException {
byte[] one = new byte[1];
return read(one, 0, 1, timeout) == 1 ? one[0] & 0xff : -1;
}

/// Reads exactly `length` bytes, each within `timeout` milliseconds of
/// the one before.
///
/// #### Parameters
///
/// - `buffer`: where to put them
///
/// - `offset`: where to start in it
///
/// - `length`: how many
///
/// - `timeout`: the longest gap allowed, in milliseconds
///
/// #### Returns
///
/// true when all arrived; false on a gap longer than `timeout`
///
/// #### Throws
///
/// - `IOException`: when the link is closed or broken
public boolean readFully(byte[] buffer, int offset, int length, int timeout) throws IOException {
int done = 0;
while (done < length) {
int n = read(buffer, offset + done, length - done, timeout);
if (n <= 0) {
return false;
}
done += n;
}
return true;
}

/// Throws when called on the EDT; for implementations, at the top of
/// every blocking method.
///
/// #### Throws
///
/// - `IllegalStateException`: on the EDT
protected static void checkNotEdt() {
if (CN.isEdt()) {
throw new IllegalStateException("Byte links block; use them from a thread of your own, not the EDT");
}
}

/// Sleeps briefly between polls; for implementations.
///
/// #### Parameters
///
/// - `millis`: how long
protected static void pause(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
148 changes: 148 additions & 0 deletions CodenameOne/src/com/codename1/io/link/MemoryLink.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/*
* Copyright (c) 2026, Codename One and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Codename One designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Codename One through http://www.codenameone.com/ if you
* need additional information or have any questions.
*/
package com.codename1.io.link;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicReference;

/// Two links joined back to back in memory: what one writes the other reads.
/// For tests and for a simulated device a driver can talk to without
/// hardware.
///
/// ```java
/// MemoryLink[] pair = MemoryLink.pair();
/// ByteLink tillSide = pair[0];
/// ByteLink deviceSide = pair[1]; // answer from a test thread
/// ```
public final class MemoryLink extends ByteLink {
private final Pipe incoming;
private final Pipe outgoing;
private boolean closed;

private MemoryLink(Pipe incoming, Pipe outgoing) {
this.incoming = incoming;
this.outgoing = outgoing;
}

/// Two connected links.
///
/// #### Returns
///
/// the two ends; either may be the device's
public static MemoryLink[] pair() {
Pipe a = new Pipe();
Pipe b = new Pipe();
return new MemoryLink[] {new MemoryLink(a, b), new MemoryLink(b, a)};
}

@Override
public void open() {
// Always connected.
}

@Override
public boolean isOpen() {
return !closed;
}

@Override
public void write(byte[] data, int offset, int length) throws IOException {
if (closed || outgoing.isClosed()) {
throw new IOException("The link is closed");
}
byte[] copy = new byte[length];
System.arraycopy(data, offset, copy, 0, length);
outgoing.append(copy);
}

@Override
public int read(byte[] buffer, int offset, int length, int timeout) throws IOException {
long end = System.currentTimeMillis() + timeout;
while (true) {
if (closed) {
throw new IOException("The link is closed");
}
int n = incoming.take(buffer, offset, length);
if (n > 0) {
return n;
}
if (incoming.isClosed()) {
throw new IOException("The other end closed the link");
}
if (System.currentTimeMillis() >= end) {
return 0;
}
pause(2);
}
}

@Override
public void close() {
closed = true;
outgoing.close();
incoming.close();
}

/// One direction: an immutable byte array swapped atomically, so the
/// writer's thread and the reader's need no lock.
private static final class Pipe {
private final AtomicReference<byte[]> data = new AtomicReference<byte[]>(new byte[0]);
private final AtomicReference<Boolean> closed = new AtomicReference<Boolean>(Boolean.FALSE);

void append(byte[] more) {
while (true) {
byte[] cur = data.get();
byte[] next = new byte[cur.length + more.length];
System.arraycopy(cur, 0, next, 0, cur.length);
System.arraycopy(more, 0, next, cur.length, more.length);
if (data.compareAndSet(cur, next)) {
return;
}
}
}

int take(byte[] buffer, int offset, int length) {
while (true) {
byte[] cur = data.get();
if (cur.length == 0) {
return 0;
}
int n = Math.min(length, cur.length);
byte[] rest = new byte[cur.length - n];
System.arraycopy(cur, n, rest, 0, rest.length);
if (data.compareAndSet(cur, rest)) {
System.arraycopy(cur, 0, buffer, offset, n);
return n;
}
}
}

void close() {
closed.set(Boolean.TRUE);
}

boolean isClosed() {
return closed.get().booleanValue();
}
}
}
Loading
Loading