Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
22 changes: 4 additions & 18 deletions examples/java/example/apps/LoopbackSerialIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,10 @@ public int get(ByteBuffer buffer, int maxLen, int timeoutMs) {
*
* @param buffer ByteBuffer containing data to write
* @param len number of bytes to write from the buffer
* @param timeoutMs timeout in milliseconds, 0 for non-blocking
* @return number of bytes actually written
*/
@Override
public int put(ByteBuffer buffer, int len, int timeoutMs) {
public int put(ByteBuffer buffer, int len) {
if (buffer == null || len <= 0) {
return 0;
}
Expand All @@ -104,22 +103,9 @@ public int put(ByteBuffer buffer, int len, int timeoutMs) {
for (int i = 0; i < bytesToWrite; i++) {
byte b = buffer.get();
boolean success = false;

if (timeoutMs == 0) {
// Non-blocking mode
success = dataQueue.offer(b);
} else {
// Blocking mode with timeout
try {
success = dataQueue.offer(b, timeoutMs, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
// Put the byte back to the buffer since it wasn't written
buffer.position(buffer.position() - 1);
break;
}
}


success = dataQueue.offer(b);

if (success) {
bytesWritten++;
} else {
Expand Down
2 changes: 1 addition & 1 deletion examples/java/example/apps/SerialIOTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private static void testLoopbackSerialIO() {
writeBuffer.put(testData);
writeBuffer.flip();

int bytesWritten = serialIO.put(writeBuffer, testData.length, 0);
int bytesWritten = serialIO.put(writeBuffer, testData.length);
System.out.println("Wrote " + bytesWritten + " bytes: \"" + testMessage + "\"");

// Test reading
Expand Down
2 changes: 1 addition & 1 deletion test/zcm/ApiRetcodesTest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ int zcm_msg_validate(zcm_msg_t msg)
size_t generic_get_mtu(zcm_trans_t *zt) { return GENERIC_MTU; }
int generic_sendmsg(zcm_trans_t *zt, zcm_msg_t msg) { return zcm_msg_validate(msg); }
int generic_recvmsg_enable(zcm_trans_t *zt, const char *channel, bool enable) { return ZCM_EOK; }
int generic_update(zcm_trans_t *zt) { TS_FAIL("update should never be called on a blocking transport"); return ZCM_EINVALID; }
int generic_update(zcm_trans_t *zt) { return ZCM_EOK; }
int generic_recvmsg(zcm_trans_t *zt, zcm_msg_t *msg, unsigned timeout) { usleep(timeout*1000); return ZCM_EAGAIN; }
void generic_destroy(zcm_trans_t *zt) {}

Expand Down
5 changes: 5 additions & 0 deletions zcm/blocking.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,11 @@ bool zcm_blocking_t::dispatchOneMessage(bool returnIfPaused)

bool zcm_blocking_t::sendOneMessage(bool returnIfPaused)
{
if (!sendQueue.hasMessage() && zt->vtbl->update) {
int ret = zcm_trans_update(zt);
(void)ret;
}

Msg* m = sendQueue.top();
// If the Queue was forcibly woken-up, recheck the
// running condition, and then retry.
Expand Down
6 changes: 3 additions & 3 deletions zcm/java/jni/zcm_zcm_ZCMGenericSerialTransport.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ static size_t javaGetCallback(uint8_t* data, size_t nData, uint32_t timeoutMs, v
}

// Put function callback - calls Java nativePut method
static size_t javaPutCallback(const uint8_t* data, size_t nData, uint32_t timeoutMs, void* usr)
static size_t javaPutCallback(const uint8_t* data, size_t nData, void* usr)
{
JavaSerialTransport *jst = (JavaSerialTransport*)usr;

Expand Down Expand Up @@ -130,7 +130,7 @@ static size_t javaPutCallback(const uint8_t* data, size_t nData, uint32_t timeou
// Call Java nativePut method
jint bytesWritten =
(*env)->CallIntMethod(env, jst->javaObj, jst->getNativePutMethodID,
directBuffer, (jint)nData, (jint)timeoutMs);
directBuffer, (jint)nData);

// Check for exceptions
if ((*env)->ExceptionCheck(env)) {
Expand Down Expand Up @@ -184,7 +184,7 @@ JNIEXPORT jboolean JNICALL Java_zcm_zcm_ZCMGenericSerialTransport_initializeNati
// Get method IDs for the callback methods
jclass cls = (*env)->GetObjectClass(env, self);
jst->getNativeGetMethodID = (*env)->GetMethodID(env, cls, "nativeGet", "(Ljava/nio/ByteBuffer;II)I");
jst->getNativePutMethodID = (*env)->GetMethodID(env, cls, "nativePut", "(Ljava/nio/ByteBuffer;II)I");
jst->getNativePutMethodID = (*env)->GetMethodID(env, cls, "nativePut", "(Ljava/nio/ByteBuffer;I)I");

if (jst->getNativeGetMethodID == NULL || jst->getNativePutMethodID == NULL) {
(*env)->DeleteGlobalRef(env, jst->javaObj);
Expand Down
3 changes: 1 addition & 2 deletions zcm/java/zcm/zcm/SerialIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public interface SerialIO {
*
* @param buffer ByteBuffer containing data to write (direct buffer, zero-copy)
* @param len number of bytes to write from the buffer
* @param timeoutMs number of ms this call may block for. 0 indicates nonblocking
* @return number of bytes actually written
*/
int put(ByteBuffer buffer, int len, int timeoutMs);
int put(ByteBuffer buffer, int len);
}
5 changes: 2 additions & 3 deletions zcm/java/zcm/zcm/ZCMGenericSerialTransport.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,11 @@ private int nativeGet(ByteBuffer buffer, int maxLen, int timeoutMs) {
*
* @param buffer direct ByteBuffer wrapping the native data array
* @param len number of bytes to write
* @param timeoutMs number of ms this call may block for. 0 indicates nonblocking
* @return number of bytes actually written
*/
private int nativePut(ByteBuffer buffer, int len, int timeoutMs) {
private int nativePut(ByteBuffer buffer, int len) {
try {
return serialIO.put(buffer, len, timeoutMs);
return serialIO.put(buffer, len);
} catch (Exception e) {
// Don't let exceptions propagate through JNI
System.err.println("Exception in SerialIO.put(): " + e.getMessage());
Expand Down
2 changes: 1 addition & 1 deletion zcm/nonblocking.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ int zcm_nonblocking_handle_nonblock(zcm_nonblocking_t* zcm)
int ret;
zcm_msg_t msg;

/* Perform any required traansport-level updates */
/* Perform any required transport-level updates */
zcm_trans_update(zcm->zt);

/* Try to receive a messages from the transport and dispatch them */
Expand Down
7 changes: 5 additions & 2 deletions zcm/transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,11 @@
*
* int update(zcm_trans_t* zt);
* --------------------------------------------------------------------
* This method is unused (in this mode) and should not be called by the user.
* An implementation is allowed to set this field to NULL.
* This method is optional for blocking transports; however, it can be implemented
* in order to allow the publish thread to tell the transport when it has called
* "sendmsg" on all currently available messages. The transport is free to update
* its hardware with the data from messages either during sendmsg calls or when
* update is called. If set to NULL in the vtable, it will not be called.
*
* void destroy(zcm_trans_t* zt)
* --------------------------------------------------------------------
Expand Down
61 changes: 27 additions & 34 deletions zcm/transport/generic_serial_transport.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ struct zcm_trans_generic_serial_t
size_t (*get)(uint8_t* data, size_t nData, void* usr);
size_t (*put)(const uint8_t* data, size_t nData, void* usr);
void* put_get_usr;
uint32_t timeoutPut;
uint32_t timeoutGet;

uint64_t getUtimeStop;

uint64_t (*time)(void* usr);
void* time_usr;
Expand Down Expand Up @@ -127,7 +127,7 @@ int serial_recvmsg_enable(zcm_trans_generic_serial_t *zt, const char *channel, b
return ZCM_EOK;
}

int serial_recvmsg(zcm_trans_generic_serial_t *zt, zcm_msg_t *msg, unsigned timeout)
int serial_recvmsg(zcm_trans_generic_serial_t *zt, zcm_msg_t *msg, unsigned timeoutMs)
{
uint64_t utime;
size_t incomingSize;
Expand Down Expand Up @@ -246,10 +246,13 @@ int serial_update_rx(zcm_trans_t *_zt)
static size_t get_blocking(uint8_t *data, size_t nData, void *usr)
{
zcm_trans_generic_serial_t* zt = cast(usr);
int ret = ((size_t (*)(uint8_t* data, size_t nData, uint32_t timeoutMs, void* usr))zt->get)
(data, nData, zt->timeoutGet, zt->put_get_usr);
zt->timeoutGet = 0;
return ret;

uint64_t startUs = zt->time(zt->time_usr);
// just approx / 1000 for speed
uint32_t timeoutMs = startUs > zt->getUtimeStop ? 0 : ((zt->getUtimeStop - startUs) >> 10);

return ((size_t (*)(uint8_t* data, size_t nData, uint32_t timeoutMs, void* usr))zt->get)
(data, nData, timeoutMs, zt->put_get_usr);
}

int serial_update_rx_blocking(zcm_trans_t *_zt)
Expand All @@ -266,22 +269,6 @@ int serial_update_tx(zcm_trans_t *_zt)
return ZCM_EOK;
}

static size_t put_blocking(const uint8_t *data, size_t nData, void *usr)
{
zcm_trans_generic_serial_t* zt = cast(usr);
int ret = ((size_t (*)(const uint8_t* data, size_t nData, uint32_t timeoutMs, void* usr))zt->put)
(data, nData, zt->timeoutPut, zt->put_get_usr);
zt->timeoutPut = 0;
return ret;
}

int serial_update_tx_blocking(zcm_trans_t *_zt)
{
zcm_trans_generic_serial_t* zt = cast(_zt);
cb_flush_out(&zt->sendBuffer, put_blocking, zt);
return ZCM_EOK;
}

/********************** STATICS **********************/
static size_t _serial_get_mtu(zcm_trans_t *zt)
{ return serial_get_mtu(cast(zt)); }
Expand All @@ -292,8 +279,8 @@ static int _serial_sendmsg(zcm_trans_t *zt, zcm_msg_t msg)
static int _serial_recvmsg_enable(zcm_trans_t *zt, const char *channel, bool enable)
{ return serial_recvmsg_enable(cast(zt), channel, enable); }

static int _serial_recvmsg(zcm_trans_t *zt, zcm_msg_t *msg, unsigned timeout)
{ return serial_recvmsg(cast(zt), msg, timeout); }
static int _serial_recvmsg(zcm_trans_t *zt, zcm_msg_t *msg, unsigned timeoutMs)
{ return serial_recvmsg(cast(zt), msg, timeoutMs); }

static int _serial_update(zcm_trans_t *zt)
{
Expand All @@ -306,19 +293,25 @@ static int _serial_sendmsg_blocking(zcm_trans_t *_zt, zcm_msg_t msg)
{
zcm_trans_generic_serial_t* zt = cast(_zt);
int ret = serial_sendmsg(zt, msg);
zt->timeoutPut = PUT_TIMEOUT_MS;
serial_update_tx_blocking(_zt);
if (ret == ZCM_EAGAIN) {
serial_update_tx(_zt);
ret = serial_sendmsg(zt, msg);
}
return ret;
}

static int _serial_recvmsg_blocking(zcm_trans_t *_zt, zcm_msg_t *msg, unsigned timeout)
static int _serial_recvmsg_blocking(zcm_trans_t *_zt, zcm_msg_t *msg, unsigned timeoutMs)
{
zcm_trans_generic_serial_t* zt = cast(_zt);
int ret = serial_recvmsg(zt, msg, timeout);
int ret = serial_recvmsg(zt, msg, timeoutMs);
if (ret == ZCM_EOK) return ZCM_EOK;
zt->timeoutGet = timeout;
serial_update_rx_blocking(_zt);
return ret;

zt->getUtimeStop = zt->time(zt->time_usr) + timeoutMs * 1000;
do {
serial_update_rx_blocking(_zt);
} while (zt->time(zt->time_usr) < zt->getUtimeStop);

return ZCM_EAGAIN;
}

static zcm_trans_methods_t methods = {
Expand All @@ -337,7 +330,7 @@ static zcm_trans_methods_t methods_blocking = {
&_serial_recvmsg_enable,
&_serial_recvmsg_blocking,
NULL, // drops
NULL, // update is part of recvmsg
&serial_update_tx, // force hardware tx update
&zcm_trans_generic_serial_destroy,
};

Expand Down Expand Up @@ -392,7 +385,7 @@ zcm_trans_t *zcm_trans_generic_serial_create(

zcm_trans_t *zcm_trans_generic_serial_blocking_create(
size_t (*get)(uint8_t* data, size_t nData, uint32_t timeoutMs, void* usr),
size_t (*put)(const uint8_t* data, size_t nData, uint32_t timeoutMs, void* usr),
size_t (*put)(const uint8_t* data, size_t nData, void* usr),
void* put_get_usr,
uint64_t (*timestamp_now)(void* usr),
void* time_usr,
Expand Down
8 changes: 4 additions & 4 deletions zcm/transport/generic_serial_transport.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _ZCM_TRANS_NONBLOCKING_SERIAL_H
#define _ZCM_TRANS_NONBLOCKING_SERIAL_H
#ifndef _ZCM_TRANS_GENERIC_SERIAL_H
#define _ZCM_TRANS_GENERIC_SERIAL_H

#ifdef __cplusplus
extern "C" {
Expand All @@ -22,7 +22,7 @@ zcm_trans_t *zcm_trans_generic_serial_create(
// The caller is responsible for thread safety inside those functions, if required
zcm_trans_t *zcm_trans_generic_serial_blocking_create(
size_t (*get)(uint8_t* data, size_t nData, uint32_t timeoutMs, void* usr),
size_t (*put)(const uint8_t* data, size_t nData, uint32_t timeoutMs, void* usr),
size_t (*put)(const uint8_t* data, size_t nData, void* usr),
void* put_get_usr,
uint64_t (*timestamp_now)(void* usr),
void* time_usr,
Expand All @@ -38,4 +38,4 @@ int serial_update_tx(zcm_trans_t *zt);
}
#endif

#endif /* _ZCM_TRANS_NONBLOCKING_SERIAL_H */
#endif /* _ZCM_TRANS_GENERIC_SERIAL_H */
Loading