decrazyo / unifying

FOSS re-implementation of the Logitech Unifying protocol
GNU General Public License v3.0
66 stars 1 forks source link

I have a question. #2

Open dbdbdee opened 10 months ago

dbdbdee commented 10 months ago

First of all, I am not good at English. Please understand.

I start pairing on Unifying software and when I connect the device, it works well until the device presses the key.

But when I disconnect and reconnect the device, it doesn't work.

Every time I reconnect the device, I have to make a new pairing to make the device work.

I want the device to work without pairing when I reconnect the device if it's already paired.

What should I do?

decrazyo commented 10 months ago

A device gets a unique RF address and AES encryption key when it's paired to a receiver. My example code will forget those values when it's powered off.

To fix this, you would have to save the RF address and AES encryption key to non-volatile memory, such as flash or EEPROM, after pairing and load those values back into memory when the device is rebooted. The way you do that depends on what hardware you're using, which is why I didn't include that functionality in the example code.

I have added comments to the example code to indicate what needs to be done.

I would recommend looking at the Arduino EEPROM library to learn about saving and loading data. https://docs.arduino.cc/learn/built-in-libraries/eeprom

dbdbdee commented 10 months ago

Thank you for your answer.

if(err) { uint8_t id = random(); uint16_t product_id = 0x1025; uint16_t device_type = 0x0147; uint32_t crypto = random(); uint32_t serial = 0xA58094B6; uint16_t capabilities = 0x1E40; char name[] = "Hacked"; uint8_t name_length = strlen(name);

err = unifying_pair(&state, id, product_id, device_type, crypto, serial, capabilities, name, name_length); }

printf("Address : %d %d %d %d %d\n",address[0],address[1],address[2],address[3],address[4]); printf("AesKey : %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d \n",aes_key[0],aes_key[1],aes_key[2],aes_key[3],aes_key[4],aes_key[5],aes_key[6],aes_key[7],aes_key[8],aes_key[9],aes_key[10],aes_key[11],aes_key[12],aes_key[13],aes_key[14],aes_key[15]);

for(int i = 0; i < 5; i++) {
EEPROM.write(i, address[i]); } for(int i =0; i <16; i++) {
EEPROM.write(i+5, aes_key[i]); }

I modified the code like this at the bottom.

uint32_t aes_counter = random(); // TODO: Check if we have previously paired to a receiver. // If so, load "address" and "aes_key" from non-volatile memory. for(int i = 0; i < 5; i++) {
address[i]=EEPROM.read(i); } for(int i = 0; i <= 16; i++) {
aes_key[i]=EEPROM.read(i+5); } printf("Address : %d %d %d %d %d\n",address[0],address[1],address[2],address[3],address[4]); printf("AesKey : %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d \n",aes_key[0],aes_key[1],aes_key[2],aes_key[3],aes_key[4],aes_key[5],aes_key[6],aes_key[7],aes_key[8],aes_key[9],aes_key[10],aes_key[11],aes_key[12],aes_key[13],aes_key[14],aes_key[15]);

I modified the code at the top like this.

But after the initial pairing was successful, it doesn't work when I remove and reconnect the device.