Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protobuf communication #75

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "protobuf-definitions"]
path = src/main/proto
url = https://github.com/uorocketry/protobuf-definitions
14 changes: 14 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'com.google.protobuf'
archivesBaseName = 'GroundStation'

repositories {
Expand All @@ -17,6 +18,17 @@ jar {
from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
duplicatesStrategy = DuplicatesStrategy.INCLUDE
}

buildscript {
repositories {
mavenCentral()
}

dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.18'
}
}

dependencies {
Expand All @@ -25,6 +37,8 @@ dependencies {
implementation 'org.knowm.xchart:xchart:3.6.1'
implementation 'org.java-websocket:Java-WebSocket:1.5.1'
implementation 'org.jetbrains:annotations:19.0.0'
implementation group: 'com.google.protobuf', name: 'protobuf-java', version: '3.19.1'
implementation group: 'com.google.protobuf', name: 'protobuf-java-util', version: '3.19.1'

testImplementation 'org.junit.jupiter:junit-jupiter:5.4.2'
}
Expand Down
216 changes: 216 additions & 0 deletions src/main/java/themarpe/cobs/Cobs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,216 @@
package themarpe.cobs;

/**
* Created by TheMarpe (https://github.com/themarpe/cobs-java)
*
* Licensed under MIT License
* ----------------------------------------------------------------------------
* Copyright (c) 2010 Craig McQueen
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
* ----------------------------------------------------------------------------
*/
public class Cobs {

public static int encodeDstBufMaxLen(int srcLen) {
return ((srcLen) + (((srcLen) + 253) / 254));
}

public static int decodeDstBufMaxLen(int srcLen) {
return (((srcLen) == 0) ? 0 : ((srcLen) - 1));
}


public enum EncodeStatus {
OK,
NULL_POINTER,
OUT_BUFFER_OVERFLOW;
}

public static class EncodeResult {
public int outLen;
public EncodeStatus status;
}

public enum DecodeStatus {
OK,
NULL_POINTER,
OUT_BUFFER_OVERFLOW,
ZERO_BYTE_IN_INPUT,
INPUT_TOO_SHORT;
}

public static class DecodeResult {
public int outLen;
public DecodeStatus status;
}

public static EncodeResult encode(byte[] dst_buf_ptr, byte[] src_ptr) {
EncodeResult result = new EncodeResult();
result.outLen = 0;
result.status = EncodeStatus.OK;

int dst_write_counter = 1;
int dst_code_write_counter = 0;
int dst_buf_end_counter = dst_buf_ptr.length;

int src_ptr_counter = 0;
int src_end_counter = src_ptr.length;

int search_len = 1;

if (dst_buf_ptr == null || src_ptr == null) {
result.status = EncodeStatus.NULL_POINTER;
return result;
}


if (src_ptr.length != 0) {
/* Iterate over the source bytes */
for (; ; ) {
/* Check for running out of output buffer space */
if (dst_write_counter >= dst_buf_end_counter) {
result.status = EncodeStatus.OUT_BUFFER_OVERFLOW;
break;
}

int src_byte = (int) src_ptr[src_ptr_counter++];

if (src_byte == 0) {
/* We found a zero byte */
dst_buf_ptr[dst_code_write_counter] = (byte) (search_len & 0xFF);
dst_code_write_counter = dst_write_counter++;
search_len = 1;
if (src_ptr_counter >= src_end_counter) {
break;
}
} else {
/* Copy the non-zero byte to the destination buffer */
dst_buf_ptr[dst_write_counter++] = (byte) (src_byte & 0xFF);

search_len++;
if (src_ptr_counter >= src_end_counter) {
break;
}
if (search_len == 0xFF) {
/* We have a long string of non-zero bytes, so we need
* to write out a length code of 0xFF. */
dst_buf_ptr[dst_code_write_counter] = (byte) (search_len & 0xFF);

dst_code_write_counter = dst_write_counter++;
search_len = 1;
}
}
}
}

/* We've reached the end of the source data (or possibly run out of output buffer)
* Finalise the remaining output. In particular, write the code (length) byte.
* Update the pointer to calculate the final output length.
*/
if (dst_code_write_counter >= dst_buf_end_counter) {
/* We've run out of output buffer to write the code byte. */
result.status = EncodeStatus.OUT_BUFFER_OVERFLOW;
dst_write_counter = dst_buf_end_counter;
} else {
/* Write the last code (length) byte. */
dst_buf_ptr[dst_code_write_counter] = (byte) (search_len & 0xFF);
}

/* Calculate the output length, from the value of dst_code_write_ptr */
result.outLen = dst_write_counter;

return result;
}


public static DecodeResult decode(byte[] dst_buf_ptr, byte[] src_ptr) {
DecodeResult result = new DecodeResult();
result.outLen = 0;
result.status = DecodeStatus.OK;

int src_ptr_counter = 0;
int src_end_counter = src_ptr.length;
int dst_buf_end_counter = dst_buf_ptr.length;
int dst_write_counter = 0;
int remaining_bytes;
byte src_byte;
int i;
int len_code;



/* First, do a NULL pointer check and return immediately if it fails. */
if ((dst_buf_ptr == null) || (src_ptr == null)) {
result.status = DecodeStatus.NULL_POINTER;
return result;
}

if (src_ptr.length != 0) {
for (; ; ) {
len_code = (int) src_ptr[src_ptr_counter++];
if (len_code == 0) {
result.status = DecodeStatus.ZERO_BYTE_IN_INPUT;
break;
}
len_code--;

/* Check length code against remaining input bytes */
remaining_bytes = src_end_counter - src_ptr_counter;
if (len_code > remaining_bytes) {
result.status = DecodeStatus.INPUT_TOO_SHORT;
len_code = remaining_bytes;
}

/* Check length code against remaining output buffer space */
remaining_bytes = dst_buf_end_counter - dst_write_counter;
if (len_code > remaining_bytes) {
result.status = DecodeStatus.OUT_BUFFER_OVERFLOW;
len_code = remaining_bytes;
}

for (i = len_code; i != 0; i--) {
src_byte = (byte) (src_ptr[src_ptr_counter++] & 0xFF);
if (src_byte == 0) {
result.status = DecodeStatus.ZERO_BYTE_IN_INPUT;
}

dst_buf_ptr[dst_write_counter++] = src_byte;
}

if (src_ptr_counter >= src_end_counter) {
break;
}

/* Add a zero to the end */
if (len_code != 0xFE) {
if (dst_write_counter >= dst_buf_end_counter) {
result.status = DecodeStatus.OUT_BUFFER_OVERFLOW;
break;
}
dst_buf_ptr[dst_write_counter++] = 0;
}
}
}

result.outLen = dst_write_counter;

return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public abstract class AbstractConnectionMethod implements ConnectionMethod {

protected final byte[] DELIMITER = "\n".getBytes(StandardCharsets.UTF_8);
protected final byte[] DELIMITER = { 0 };

protected ConnectionMethodListener listener;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package uorocketry.basestation.connections.method;

import themarpe.cobs.Cobs;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Arrays;

public class TcpConnectionMethod extends AbstractConnectionMethod implements Runnable {

Expand Down Expand Up @@ -78,7 +81,14 @@ public void run() {
result[i] = receivedData.get(i);
}

listener.receivedData(result);
byte[] decoded = new byte[receivedData.size()];
Cobs.DecodeResult decodeResult = Cobs.decode(decoded, result);

if (decodeResult.status == Cobs.DecodeStatus.OK) {
listener.receivedData(result);
} else {
System.err.println("Failed to decode using COBS due to " + decodeResult.status.toString() + ": " + Arrays.toString(decoded));
}
}

} catch (IOException e) {
Expand Down
1 change: 1 addition & 0 deletions src/main/proto
Submodule proto added at 38da2e