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

Initial proxy implementation #1

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/proxy/out/
/proxy/.idea
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this .gitignore working? I see .idea in the PR.

16 changes: 9 additions & 7 deletions protocol/protocol.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,19 @@ commands that are querying information.
* Connect
* Request
* `0c` - Command.
* `00 0a` - ?? fixed.
* `eb 2b 76 14 0c 1b 07 15 35 04` - ?? varying with each connection.
Is `0a` an array length for this?
* `00 0a` - ?? fixed. [Confirmed same across two devices -- App version?]
* `eb 2b 76` ?? varying.
* `14 0c 1b 07 15 35 04` - Current time: Year Month Day Day-of-week? Hour Minute Second

* Response
* `c9` - ?? varying.
* `01` - ?? varying 00 or 01.
* `09 54 72 69 6d 6c 69 67 68 74` - "Trimlight" (assuming 09 is a string length).
* `09` - String length
* `54 72 69 6d 6c 69 67 68 74` - "Trimlight"
Controller name.
* `00 00` - ?? fixed.
* `02 58` - Pixel count? **TODO: Confirm this with a different system.*
* `03 00` - ?? fixed.
* `00 00` - ?? fixed. [RGB order?]
* `02 58` - Pixel count
* `03 00` - ?? fixed. [IC?]
* Controller mode change
* Request
* `0d` - Command.
Expand Down
3 changes: 3 additions & 0 deletions proxy/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions proxy/.idea/description.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions proxy/.idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions proxy/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions proxy/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions proxy/.idea/project-template.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions proxy/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions proxy/proxy.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
29 changes: 29 additions & 0 deletions proxy/src/com/trimlighthacking/BuiltInPatternSetRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.trimlighthacking;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider com.knottysoftware.trimlight.proxy?

You should generally own the domain you use for your package name.


import java.util.Arrays;

public class BuiltInPatternSetRequest extends Request {
private final int fixedUnknown1;
private final int fixedUnknown2;

public BuiltInPatternSetRequest(byte[] buffer) {

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whitespace?


this.fixedUnknown1 = buffer[2] & 0xFF;
this.fixedUnknown2 = buffer[3] & 0xFF;

this.PatternIndex = buffer[4] & 0xFF;
this.Speed = buffer[5] & 0xFF;
this.Brightness = buffer[6] & 0xFF;
// Reduce to interesting slice
this.rawBytes = Arrays.copyOfRange(buffer, 2, buffer.length - 1);
}

int PatternIndex; // TODO: Enumerate names
int Speed;
int Brightness;

public String toString() {
return String.format("%s <Built-In Pattern Set Request> Index: %s Speed: %d Brightness: %d Unknowns: (Fixed: %02X %02X)", this.LogPrefix, PatternIndex, Speed, Brightness, fixedUnknown1, fixedUnknown2);
}
}
28 changes: 28 additions & 0 deletions proxy/src/com/trimlighthacking/ByteArrayFormatter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.trimlighthacking;

public class ByteArrayFormatter {
public static String format(byte[] bytes, int count) {
StringBuilder out = new StringBuilder(bytes.length * 3);
out.append("\n ");
if(count > bytes.length) {
throw new IllegalArgumentException(String.format("Count %d is greater than buffer length %d", count, bytes.length));
}
for(int i = 0; i < count; i++) {
byte b = bytes[i];
if((b & 0xFF) < 32) {
out.append(String.format("^%s ", (char)((b & 0xFF) + 64)));
} else if((b & 0xFF) < 128) {
out.append(String.format(" %s ", (char)(b)));
}
else {
out.append(" ");
}

}
out.append("\n ");
for(int i = 0; i < count; i++) {
out.append(String.format("%02X ", bytes[i]));
}
return out.toString();
}
}
49 changes: 49 additions & 0 deletions proxy/src/com/trimlighthacking/ConnectionRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.trimlighthacking;

import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class ConnectionRequest extends Request {
private final int fixedUnknown1;
private final int fixedUnknown2;
private final int varUnknown1;
private final int varUnknown2;
private final int varUnknown3;

public ConnectionRequest(byte[] buffer) {
// We don't know what indices 2-6 are used for yet.
int year = buffer[7] & 0xff;
int month = buffer[8] & 0xff;
int day = buffer[9] & 0xff;
int hour = buffer[11] & 0xff;
int minute = buffer[12] & 0xff;
int second = buffer[13] & 0xff;

this.fixedUnknown1 = buffer[2] & 0xff;
this.fixedUnknown2 = buffer[3] & 0xff;
this.varUnknown1 = buffer[4] & 0xff;
this.varUnknown2 = buffer[5] & 0xff;
this.varUnknown3 = buffer[6] & 0xff;

Calendar calendar = new GregorianCalendar();

calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DAY_OF_MONTH, day);
calendar.set(Calendar.HOUR, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, second);
this.ConnectionStartDate = calendar;

// Reduce to interesting slice
this.rawBytes = Arrays.copyOfRange(buffer, 2, buffer.length - 1);
}

public final Calendar ConnectionStartDate;

@Override
public String toString() {
return String.format("%s <Connection Request> Connection Started: %s Unknowns: (Fixed: %02X %02X Var: %02X %02X %02X)", this.LogPrefix, this.ConnectionStartDate.getTime().toString(), fixedUnknown1, fixedUnknown2, varUnknown1, varUnknown2, varUnknown3);
}
}
37 changes: 37 additions & 0 deletions proxy/src/com/trimlighthacking/ConnectionResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.trimlighthacking;

import java.util.Arrays;

public class ConnectionResponse extends Response {

private final int varUnknown1;
private final int fixedUnknown1;
private final int fixedUnknown2;
private final int fixedUnknown3;
private final int fixedUnknown4;
private final int fixedUnknown5;

public ConnectionResponse(byte[] buffer) {

this.varUnknown1 = buffer[1] & 0xff;
this.fixedUnknown1 = buffer[2] & 0xff;
int hostnameLength = buffer[3] & 0xff;
int hostnameEnd = 4 + hostnameLength;
byte[] hostname = Arrays.copyOfRange(buffer, 4, hostnameEnd);
this.HostName = new String(hostname);
this.fixedUnknown2 = buffer[hostnameEnd] & 0xff;
this.fixedUnknown3 = buffer[hostnameEnd + 1] & 0xff;
this.PixelCount = (((buffer[hostnameEnd + 2] & 0xff) << 8) + (buffer[hostnameEnd + 3] & 0xff));
this.fixedUnknown4 = buffer[hostnameEnd + 4] & 0xff;
this.fixedUnknown5 = buffer[hostnameEnd + 5] & 0xff;
// Reduce to interesting slice
this.rawBytes = Arrays.copyOfRange(buffer, 2, buffer.length - 1);
}

public String HostName;
public int PixelCount;
@Override
public String toString() {
return String.format("%s <Connection Response> HostName: %s PixelCount: %d Unknowns: (Fixed: %02X %02X %02X %02X %02X Var: %02X)", this.LogPrefix, this.HostName, this.PixelCount, fixedUnknown1, fixedUnknown2, fixedUnknown3, fixedUnknown4, fixedUnknown5, varUnknown1);
}
}
19 changes: 19 additions & 0 deletions proxy/src/com/trimlighthacking/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.trimlighthacking;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

public class Main {

public static void main(String[] args) {
System.out.println("TrimLight Proxy v0.000001");

Server s = new Server(System.out, 12); // log stream, max connections
try {
s.addService(new TrimlightProxyServer("192.168.254.50", 8189), 8189);
} catch (IOException e) {
e.printStackTrace();
}
}
}
20 changes: 20 additions & 0 deletions proxy/src/com/trimlighthacking/MalformedBufferException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.trimlighthacking;

public class MalformedBufferException extends RuntimeException {
public MalformedBufferException(String message, byte[] buffer) {
super(message);
this.buffer = buffer;
}

public MalformedBufferException(String message, Throwable cause, byte[] buffer) {
super(message, cause);
this.buffer = buffer;
}

public MalformedBufferException(Throwable cause, byte[] buffer) {
super(cause);
this.buffer = buffer;
}

public final byte[] buffer;
}
23 changes: 23 additions & 0 deletions proxy/src/com/trimlighthacking/PatternEntryRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.trimlighthacking;

import java.util.Arrays;

public class PatternEntryRequest extends Request {
private final int fixedUnknown1;
private final int fixedUnknown2;

public PatternEntryRequest(byte[] buffer) {
this.fixedUnknown1 = buffer[2] & 0xFF;
this.fixedUnknown2 = buffer[3] & 0xFF;

this.PatternIndex = buffer[4] & 0xFF;
// Reduce to interesting slice
this.rawBytes = Arrays.copyOfRange(buffer, 2, buffer.length - 1);
}

public int PatternIndex;

public String toString() {
return String.format("%s <Pattern Entry Request> Index: %d Unknowns: (Fixed: %02X %02X)", this.LogPrefix, this.PatternIndex, fixedUnknown1, fixedUnknown2);
}
}
44 changes: 44 additions & 0 deletions proxy/src/com/trimlighthacking/PatternEntryResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.trimlighthacking;

import sun.plugin2.util.ColorUtil;

import java.awt.*;
import java.util.Arrays;

public class PatternEntryResponse extends Response {
public PatternEntryResponse(byte[] buffer) {
this.PatternIndex = buffer[1] & 0xFF;
byte[] patternNameBytes = Arrays.copyOfRange(buffer, 2, 27);
this.PatternName = new String(patternNameBytes);
for(int i = 0; i < 7; i++) {
Lengths[i] = buffer[31 + i] & 0xFF;
}

for(int i = 0; i < 7; i++) {
int tripletStart = i * 3 + 38;
int r = buffer[tripletStart + 0] & 0xFF;
int g = buffer[tripletStart + 1] & 0xFF;
int b = buffer[tripletStart + 2] & 0xFF;
Colors[i] = new Color(r,g,b);
}
// Reduce to interesting slice
this.rawBytes = Arrays.copyOfRange(buffer, 2, buffer.length - 1);
}

public int PatternIndex;
public String PatternName;
public int[] Lengths = new int[7];
public Color[] Colors = new Color[7];

public String toString() {
String formattedByteArray = ByteArrayFormatter.format(this.rawBytes, this.rawBytes.length);
StringBuilder sb = new StringBuilder();
for(int i = 0; i < 7; i++) {
sb.append(Colors[i].toString().replace("java.awt.Color", ""));
sb.append(" x ");
sb.append(Lengths[i]);
sb.append(" ");
}
return String.format("%s <Pattern Entry Response> #%d \"%s\" %s", this.LogPrefix, this.PatternIndex, this.PatternName, sb.toString());
}
}
21 changes: 21 additions & 0 deletions proxy/src/com/trimlighthacking/PatternLibraryQueryRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.trimlighthacking;

import java.util.Arrays;

public class PatternLibraryQueryRequest extends Request {

private final int fixedUnknown1;
private final int fixedUnknown2;

public PatternLibraryQueryRequest(byte[] buffer) {

this.fixedUnknown1 = buffer[1] & 0xFF;
this.fixedUnknown2 = buffer[2] & 0xFF;
// Reduce to interesting slice
this.rawBytes = Arrays.copyOfRange(buffer, 2, buffer.length - 1);
}

public String toString() {
return String.format("%s <Pattern Library Query Request> Unknowns: (Fixed: %02X %02X)", this.LogPrefix, fixedUnknown1, fixedUnknown2);
}
}
23 changes: 23 additions & 0 deletions proxy/src/com/trimlighthacking/PatternLibraryQueryResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.trimlighthacking;

import java.util.Arrays;

public class PatternLibraryQueryResponse extends Response {

public PatternLibraryQueryResponse(byte[] buffer) {

/*this.PatternIndex = buffer[1] & 0xFF;
byte[] patternNameBytes = Arrays.copyOfRange(buffer, 2, 27);
this.PatternName = new String(patternNameBytes);*/
// Reduce to interesting slice
this.rawBytes = Arrays.copyOfRange(buffer, 2, buffer.length - 1);
}

public int PatternIndex;
public String PatternName;

public String toString() {
String formattedByteArray = ByteArrayFormatter.format(this.rawBytes, this.rawBytes.length);
return String.format("%s <Pattern Library Query Response> %s", this.LogPrefix, formattedByteArray);
}
}
Loading