Skip to content

Android changes #23

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

Open
wants to merge 5 commits into
base: master
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
Expand Up @@ -2,3 +2,5 @@
.classpath
.project
.settings/
.idea
.vscode
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ before_install:
- .travis/prepare

script:
- mvn cobertura:cobertura
- mvn clean test

after_success:
- bash <(curl -s https://codecov.io/bash)
Expand Down
34 changes: 22 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<configuration>
<source>8</source>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
Expand All @@ -139,19 +142,26 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<configuration>
<formats>
<format>html</format>
<format>xml</format>
</formats>
<check />
</configuration>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</plugins>
</build>

<version>1.0.1</version>
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/github/mob41/blapi/A1Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.io.IOException;
import java.net.DatagramPacket;

import javax.xml.bind.DatatypeConverter;
import static com.github.mob41.blapi.HexUtil.bytes2hex;

import com.github.mob41.blapi.mac.Mac;
import com.github.mob41.blapi.pkt.CmdPayload;
Expand Down Expand Up @@ -68,13 +68,13 @@ public byte[] getData() {
});
byte[] data = packet.getData();

log.debug("A1 check sensors received encrypted bytes: " + DatatypeConverter.printHexBinary(data));
log.debug("A1 check sensors received encrypted bytes: " + bytes2hex(data));

int err = data[0x22] | (data[0x23] << 8);

if (err == 0) {
byte[] pl = decryptFromDeviceMessage(data);
log.debug("A1 check sensors received bytes (decrypted):" + DatatypeConverter.printHexBinary(pl));
log.debug("A1 check sensors received bytes (decrypted):" + bytes2hex(pl));

float temp = (float) ((pl[0x4] * 10 + pl[0x5]) / 10.0);
float hum = (float) ((pl[0x6] * 10 + pl[0x7]) / 10.0);
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/github/mob41/blapi/BLDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import java.util.List;
import java.util.Random;

import javax.xml.bind.DatatypeConverter;
import static com.github.mob41.blapi.HexUtil.bytes2hex;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -389,7 +389,7 @@ public boolean auth(boolean reauth) throws IOException {
log.debug("auth Sending CmdPacket with AuthCmdPayload: cmd=" + Integer.toHexString(sendPayload.getCommand())
+ " len=" + sendPayload.getPayload().getData().length);

log.debug("auth AuthPayload initial bytes to send: {}", DatatypeConverter.printHexBinary(sendPayload.getPayload().getData()));
log.debug("auth AuthPayload initial bytes to send: {}", bytes2hex(sendPayload.getPayload().getData()));

DatagramPacket recvPack = sendCmdPkt(10000, 2048, sendPayload);

Expand All @@ -401,7 +401,7 @@ public boolean auth(boolean reauth) throws IOException {
return false;
}

log.debug("auth recv encrypted data bytes (" + data.length +") after initial req: {}", DatatypeConverter.printHexBinary(data));
log.debug("auth recv encrypted data bytes (" + data.length +") after initial req: {}", bytes2hex(data));

byte[] payload = null;
try {
Expand All @@ -417,11 +417,11 @@ public boolean auth(boolean reauth) throws IOException {
return false;
}

log.debug("auth Packet received payload bytes: " + DatatypeConverter.printHexBinary(payload));
log.debug("auth Packet received payload bytes: " + bytes2hex(payload));

key = subbytes(payload, 0x04, 0x14);

log.debug("auth Packet received key bytes: " + DatatypeConverter.printHexBinary(key));
log.debug("auth Packet received key bytes: " + bytes2hex(key));

if (key.length % 16 != 0) {
log.error("auth Received key len is not a multiple of 16! Aborting");
Expand All @@ -434,7 +434,7 @@ public boolean auth(boolean reauth) throws IOException {

id = subbytes(payload, 0x00, 0x04);

log.debug("auth Packet received id bytes: " + DatatypeConverter.printHexBinary(id) + " with ID len=" + id.length);
log.debug("auth Packet received id bytes: " + bytes2hex(id) + " with ID len=" + id.length);

log.debug("auth End of authentication method");
alreadyAuthorized = true;
Expand Down Expand Up @@ -530,7 +530,7 @@ public DatagramPacket sendCmdPkt(int timeout, int bufSize, CmdPayload cmdPayload
public DatagramPacket sendCmdPkt(InetAddress sourceIpAddr, int sourcePort, int timeout, int bufSize,
CmdPayload cmdPayload) throws IOException {
CmdPacket cmdPkt = new CmdPacket(mac, pktCount++, id, aes, cmdPayload);
log.debug("sendCmdPkt - Send Command Packet bytes: {}", DatatypeConverter.printHexBinary(cmdPkt.getData()));
log.debug("sendCmdPkt - Send Command Packet bytes: {}", bytes2hex(cmdPkt.getData()));
return sendPkt(sock, cmdPkt, InetAddress.getByName(host), 80, timeout, bufSize);
}

Expand Down Expand Up @@ -1041,7 +1041,7 @@ public static DatagramPacket sendPkt(DatagramSocket sock, Packet pkt, InetAddres
}
}

log.debug("sendPkt - recv data bytes (" + recepack.getData().length +") after initial req: {}", DatatypeConverter.printHexBinary(recepack.getData()));
log.debug("sendPkt - recv data bytes (" + recepack.getData().length +") after initial req: {}", bytes2hex(recepack.getData()));
recepack.setData(removeNullsFromEnd(recepack.getData(), 0));
return recepack;
}
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/com/github/mob41/blapi/HexUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.github.mob41.blapi;

public class HexUtil {

private static final char[] hexCode = "0123456789abcdef".toCharArray();

public static String bytes2hex(byte[] data) {
StringBuilder r = new StringBuilder(data.length * 2);
byte[] var3 = data;
int var4 = data.length;

for(int var5 = 0; var5 < var4; ++var5) {
byte b = var3[var5];
r.append(hexCode[b >> 4 & 15]);
r.append(hexCode[b & 15]);
}

return r.toString();
}
public static final byte[] hex2bytes(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}

}
8 changes: 4 additions & 4 deletions src/main/java/com/github/mob41/blapi/MP1Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import java.io.IOException;
import java.net.DatagramPacket;

import javax.xml.bind.DatatypeConverter;
import static com.github.mob41.blapi.HexUtil.bytes2hex;

import com.github.mob41.blapi.mac.Mac;
import com.github.mob41.blapi.pkt.CmdPayload;
Expand Down Expand Up @@ -116,7 +116,7 @@ public byte[] getData() {
int err = data[0x22] | (data[0x23] << 8);

if (err == 0) {
log.debug("MP1 set state mask received encrypted bytes: " + DatatypeConverter.printHexBinary(data));
log.debug("MP1 set state mask received encrypted bytes: " + bytes2hex(data));
} else {
log.warn("MP1 set state mask received returned err: " + Integer.toHexString(err) + " / " + err);
}
Expand Down Expand Up @@ -171,13 +171,13 @@ public byte[] getData() {

byte[] data = packet.getData();

log.debug("MP1 get states raw received encrypted bytes: " + DatatypeConverter.printHexBinary(data));
log.debug("MP1 get states raw received encrypted bytes: " + bytes2hex(data));

int err = data[0x22] | (data[0x23] << 8);

if (err == 0) {
byte[] pl = decryptFromDeviceMessage(data);
log.debug("MP1 get states raw received bytes (decrypted): " + DatatypeConverter.printHexBinary(pl));
log.debug("MP1 get states raw received bytes (decrypted): " + bytes2hex(pl));
byte state = 0;
if (pl[0x3c] >= 48 && pl[0x3c] <= 57) {
String decodeValue1;
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/github/mob41/blapi/RM2Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import java.io.IOException;
import java.net.DatagramPacket;

import javax.xml.bind.DatatypeConverter;
import com.github.mob41.blapi.HexUtil;

import com.github.mob41.blapi.mac.Mac;
import com.github.mob41.blapi.pkt.cmd.rm2.CheckDataCmdPayload;
Expand Down Expand Up @@ -88,7 +88,7 @@ public byte[] checkData() throws Exception {

int err = data[0x22] | (data[0x23] << 8);

log.debug("RM2 check data received encrypted bytes: " + DatatypeConverter.printHexBinary(data));
log.debug("RM2 check data received encrypted bytes: " + HexUtil.bytes2hex(data));


if (err == 0) {
Expand All @@ -115,7 +115,7 @@ public boolean enterLearning() throws IOException {
DatagramPacket packet = sendCmdPkt(10000, cmdPayload);

byte[] data = packet.getData();
log.debug("RM2 enter learning received encrypted bytes: " + DatatypeConverter.printHexBinary(data));
log.debug("RM2 enter learning received encrypted bytes: " + HexUtil.bytes2hex(data));
int err = data[0x22] | (data[0x23] << 8);

if (err == 0) {
Expand All @@ -139,12 +139,12 @@ public double getTemp() throws Exception {
DatagramPacket packet = sendCmdPkt(new RMTempCmdPayload());
byte[] data = packet.getData();

log.debug("RM2 get temp received encrypted bytes: " + DatatypeConverter.printHexBinary(data));
log.debug("RM2 get temp received encrypted bytes: " + HexUtil.bytes2hex(data));
int err = data[0x22] | (data[0x23] << 8);

if (err == 0) {
byte[] pl = decryptFromDeviceMessage(data);
log.debug("RM2 get temp received bytes (decrypted): " + DatatypeConverter.printHexBinary(pl));
log.debug("RM2 get temp received bytes (decrypted): " + HexUtil.bytes2hex(pl));

return (double) (pl[0x4] * 10 + pl[0x5]) / 10.0;
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/github/mob41/blapi/SP1Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import java.io.IOException;
import java.net.DatagramPacket;

import javax.xml.bind.DatatypeConverter;
import static com.github.mob41.blapi.HexUtil.bytes2hex;

import com.github.mob41.blapi.mac.Mac;
import com.github.mob41.blapi.pkt.CmdPayload;
Expand Down Expand Up @@ -70,7 +70,7 @@ public byte[] getData() {

byte[] data = packet.getData();

log.debug("SP1 set power received encrypted bytes: " + DatatypeConverter.printHexBinary(data));
log.debug("SP1 set power received encrypted bytes: " + bytes2hex(data));

int err = data[0x22] | (data[0x23] << 8);

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/github/mob41/blapi/SP2Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import java.io.IOException;
import java.net.DatagramPacket;

import javax.xml.bind.DatatypeConverter;
import static com.github.mob41.blapi.HexUtil.bytes2hex;

import com.github.mob41.blapi.mac.Mac;
import com.github.mob41.blapi.pkt.CmdPayload;
Expand Down Expand Up @@ -75,7 +75,7 @@ public byte[] getData() {

byte[] data = packet.getData();

log.debug("SP2 set state received encrypted bytes: " + DatatypeConverter.printHexBinary(data));
log.debug("SP2 set state received encrypted bytes: " + bytes2hex(data));

int err = data[0x22] | (data[0x23] << 8);

Expand Down Expand Up @@ -109,13 +109,13 @@ public byte[] getData() {
});
byte[] data = packet.getData();

log.debug("SP2 get state received encrypted bytes: " + DatatypeConverter.printHexBinary(data));
log.debug("SP2 get state received encrypted bytes: " + bytes2hex(data));

int err = data[0x22] | (data[0x23] << 8);

if (err == 0) {
byte[] pl = decryptFromDeviceMessage(data);
log.debug("SP2 get state received bytes (decrypted): " + DatatypeConverter.printHexBinary(pl));
log.debug("SP2 get state received bytes (decrypted): " + bytes2hex(pl));
return pl[0x4] == 1 ? true : false;
} else {
log.warn("SP2 get state received an error: " + Integer.toHexString(err) + " / " + err);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

import java.io.IOException;

import javax.xml.bind.DatatypeConverter;
import static com.github.mob41.blapi.HexUtil.bytes2hex;

import com.github.mob41.blapi.BLDevice;
import com.github.mob41.blapi.mac.Mac;
Expand Down Expand Up @@ -92,7 +92,7 @@ public double getRoomTemp() throws Exception {
public BaseStatusInfo getBasicStatus() throws Exception {
byte[] pl = new GetBasicInfoCommand().execute(this);
if (pl != null) {
log.debug("getBasicStatus - received bytes: {}", DatatypeConverter.printHexBinary(pl));
log.debug("getBasicStatus - received bytes: {}", bytes2hex(pl));
return new BaseStatusInfo(pl);
}
return null;
Expand All @@ -101,7 +101,7 @@ public BaseStatusInfo getBasicStatus() throws Exception {
public AdvancedStatusInfo getAdvancedStatus() throws Exception {
byte[] pl = new GetStatusCommand().execute(this);
if (pl != null) {
log.debug("getAdvancedStatus - received bytes: {}", DatatypeConverter.printHexBinary(pl));
log.debug("getAdvancedStatus - received bytes: {}", bytes2hex(pl));
return new AdvancedStatusInfo(pl);
}
return null;
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/com/github/mob41/blapi/pkt/CmdPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@
*******************************************************************************/
package com.github.mob41.blapi.pkt;

import javax.xml.bind.DatatypeConverter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.github.mob41.blapi.BLDevice;
import com.github.mob41.blapi.ex.BLApiRuntimeException;
import com.github.mob41.blapi.mac.Mac;
import com.github.mob41.blapi.pkt.auth.AES;

import static com.github.mob41.blapi.HexUtil.bytes2hex;
/**
* This constructs a byte array with the format of a command to the Broadlink
* device
Expand Down Expand Up @@ -133,7 +131,7 @@ public CmdPacket(Mac targetMac, int count, byte[] id, AES aesInstance, CmdPayloa

int checksumpayload = 0xbeaf;
for (int i = 0; i < payloadPad.length; i++) {
checksumpayload = checksumpayload + Byte.toUnsignedInt(payloadPad[i]);
checksumpayload = checksumpayload + (int)(Byte.valueOf(payloadPad[i]).intValue() & 0xff);
checksumpayload = checksumpayload & 0xffff;
}

Expand All @@ -146,7 +144,7 @@ public CmdPacket(Mac targetMac, int count, byte[] id, AES aesInstance, CmdPayloa
log.debug("Encrypting payload");

payload = aesInstance.encrypt(payloadPad);
log.debug("Encrypted payload bytes: {}", DatatypeConverter.printHexBinary(payload));
log.debug("Encrypted payload bytes: {}", bytes2hex(payload));

log.debug("Encrypted. len=" + payload.length);
} catch (Exception e) {
Expand All @@ -168,7 +166,7 @@ public CmdPacket(Mac targetMac, int count, byte[] id, AES aesInstance, CmdPayloa

int checksumpkt = 0xbeaf;
for (int i = 0; i < data.length; i++) {
checksumpkt = checksumpkt + Byte.toUnsignedInt(data[i]);
checksumpkt = checksumpkt + (int)(Byte.valueOf(data[i]).intValue() & 0xff);
checksumpkt = checksumpkt & 0xffff;
// log.debug("index: " + i + ", data byte: " + Byte.toUnsignedInt(data[i]) + ", checksum: " + checksumpkt);
}
Expand Down
Loading