Skip to content

Replace javax.xml.bind.DatatypeConverter with java.util.Base64 for Java 9+ compatibility #33

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 2 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
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 java.util.Base64;

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: " + Base64.getEncoder().encodeToString(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):" + Base64.getEncoder().encodeToString(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 java.util.Base64;

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: {}", Base64.getEncoder().encodeToString(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: {}", Base64.getEncoder().encodeToString(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: " + Base64.getEncoder().encodeToString(payload));

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

log.debug("auth Packet received key bytes: " + DatatypeConverter.printHexBinary(key));
log.debug("auth Packet received key bytes: " + Base64.getEncoder().encodeToString(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: " + Base64.getEncoder().encodeToString(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: {}", Base64.getEncoder().encodeToString(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: {}", Base64.getEncoder().encodeToString(recepack.getData()));
recepack.setData(removeNullsFromEnd(recepack.getData(), 0));
return recepack;
}
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/github/mob41/blapi/LibraryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.github.mob41.blapi;

import com.github.mob41.blapi.mac.Mac; //Necessary if using <code>2.ii</code>

public class LibraryTest {
public static void main(String[] args){

System.out.println("Testing Library...");
try {
BLDevice dev = new RM2Device("192.168.1.3", new Mac("78:0f:77:17:ec:ee"));
dev.auth();
if (dev instanceof RM2Device){
RM2Device rm2 = (RM2Device) dev;

boolean success = rm2.enterLearning();
System.out.println("Enter Learning status: " + (success ? "Success!" : "Failed!"));
} else {
System.out.println("The \"dev\" is not a RM2Device instance.");
}
} catch (Exception e) {
e.printStackTrace();
}
}

}
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 java.util.Base64;

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: " + Base64.getEncoder().encodeToString(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: " + Base64.getEncoder().encodeToString(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): " + Base64.getEncoder().encodeToString(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 java.util.Base64;

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: " + Base64.getEncoder().encodeToString(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: " + Base64.getEncoder().encodeToString(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: " + Base64.getEncoder().encodeToString(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): " + Base64.getEncoder().encodeToString(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 java.util.Base64;

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: " + Base64.getEncoder().encodeToString(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 java.util.Base64;

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: " + Base64.getEncoder().encodeToString(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: " + Base64.getEncoder().encodeToString(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): " + Base64.getEncoder().encodeToString(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 java.util.Base64;

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: {}", Base64.getEncoder().encodeToString(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: {}", Base64.getEncoder().encodeToString(pl));
return new AdvancedStatusInfo(pl);
}
return null;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/github/mob41/blapi/pkt/CmdPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
*******************************************************************************/
package com.github.mob41.blapi.pkt;

import javax.xml.bind.DatatypeConverter;
import java.util.Base64;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -146,7 +146,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: {}", Base64.getEncoder().encodeToString(payload));

log.debug("Encrypted. len=" + payload.length);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.net.DatagramPacket;
import java.util.Arrays;

import javax.xml.bind.DatatypeConverter;
import java.util.Base64;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -33,14 +33,14 @@ public byte[] execute(BaseHysenDevice device) throws Exception {
byte[] data = packet.getData();

log.debug(this.getClass().getSimpleName() + " received encrypted bytes: "
+ DatatypeConverter.printHexBinary(data));
+ Base64.getEncoder().encodeToString(data));

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

if (err == 0) {
byte[] pl = device.decryptFromDeviceMessage(data);
log.debug(this.getClass().getSimpleName() + " received bytes (decrypted): "
+ DatatypeConverter.printHexBinary(pl));
+ Base64.getEncoder().encodeToString(pl));
return Arrays.copyOfRange(pl, 2, pl.length);
} else {
log.warn(this.getClass().getSimpleName() + " received an error: " + Integer.toHexString(err) + " / " + err);
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/github/mob41/blapi/DevicesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class DevicesTest {
@Test
@Ignore
public void testDevices() throws Exception {
BLDevice[] devs = BLDevice.discoverDevices(InetAddress.getByName("192.168.1.7"), 0, 0);
BLDevice[] devs = BLDevice.discoverDevices(InetAddress.getByName("192.168.1.9"), 0, 0);
log.info("BLDevice returned " + devs.length + " number of devices.");
for (int i = 0; i < devs.length; i++) {
BLDevice dev = devs[i];
Expand Down