diff --git a/arduino/victron-ble/.clangd b/arduino/victron-ble/.clangd new file mode 100644 index 0000000..1a5241b --- /dev/null +++ b/arduino/victron-ble/.clangd @@ -0,0 +1,21 @@ +If: + PathMatch: .*\.ino$ +CompileFlags: + Add: + - -x + - c++ + - -include + - Arduino.h + - -isystem/home/david/.arduino15/packages/esp32/tools/esp-x32/2411/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/include/c++/14.2.0 + - -isystem/home/david/.arduino15/packages/esp32/tools/esp-x32/2411/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/include/c++/14.2.0/xtensa-esp-elf/esp32 + - -isystem/home/david/.arduino15/packages/esp32/tools/esp-x32/2411/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/include/c++/14.2.0/backward + - -isystem/home/david/.arduino15/packages/esp32/tools/esp-x32/2411/bin/../lib/gcc/xtensa-esp-elf/14.2.0/include + - -isystem/home/david/.arduino15/packages/esp32/tools/esp-x32/2411/bin/../lib/gcc/xtensa-esp-elf/14.2.0/include-fixed + - -isystem/home/david/.arduino15/packages/esp32/tools/esp-x32/2411/bin/../lib/gcc/xtensa-esp-elf/14.2.0/../../../../xtensa-esp-elf/include + CompilationDatabase: build + Remove: + - -mlongcalls + - -fno-tree-switch-conversion + - -fstrict-volatile-bitfields + - -mdisable-hardware-atomics + - -mtext-section-literals diff --git a/arduino/victron-ble/.gitignore b/arduino/victron-ble/.gitignore new file mode 100644 index 0000000..e8f8cea --- /dev/null +++ b/arduino/victron-ble/.gitignore @@ -0,0 +1,6 @@ +/build/ +.zed/ +.clangd/ + +# Don't commit encryption key. +KEY.h diff --git a/arduino/victron-ble/KEY.h.example b/arduino/victron-ble/KEY.h.example new file mode 100644 index 0000000..9b3f5ff --- /dev/null +++ b/arduino/victron-ble/KEY.h.example @@ -0,0 +1,21 @@ +/* + * This file is an example format for the required key for the AES-CTR + * decryption of the victron advertisement packets. Make a copy of + * this file and name it 'Key.h'. + * + * The key can be retrieved by connecting to the solar charge controller + * using the VictronConnect App. + * 1. Select the solar charge controller in question + * 2. Go to settings + * 3. Go to 3 bars in top right > Product Info > Encryption Data > Show + * 4. Copy the MAC and encryption key. + */ +#pragma once + +// Encryption key for the Victron Solar Charge Controller. +const uint8_t VICTRON_KEY[16] = { + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, +}; diff --git a/arduino/victron-ble/Makefile b/arduino/victron-ble/Makefile new file mode 100644 index 0000000..28cf566 --- /dev/null +++ b/arduino/victron-ble/Makefile @@ -0,0 +1,28 @@ +FQBN := esp32:esp32:esp32 +PORT := /dev/ttyUSB1 +BAUD := 115200 +export TMPDIR := $(HOME)/tmp + +.PHONY: compile upload monitor flash compdb clean + +compile: + arduino-cli compile -b $(FQBN) ./ --build-path ./build + @$(MAKE) --no-print-directory map-ino + +compdb: + arduino-cli compile --only-compilation-database -b $(FQBN) ./ --build-path ./build + @$(MAKE) --no-print-directory map-ino + +map-ino: + @python3 -c 'import json, os; p="build/compile_commands.json"; ino=os.path.abspath("victron-netsender.ino"); db=[e for e in json.load(open(p)) if e["file"]!=ino]; src=next((e for e in db if e["file"].endswith(".ino.cpp")), None); src and db.append(dict(src, file=ino)); json.dump(db, open(p,"w"), indent=2)' + +upload: compile + arduino-cli upload -p $(PORT) -b $(FQBN) --input-dir ./build + +monitor: + arduino-cli monitor -p $(PORT) -c baudrate=$(BAUD) + +flash: upload monitor + +clean: + rm -rf build diff --git a/arduino/victron-ble/victron-ble.ino b/arduino/victron-ble/victron-ble.ino new file mode 100644 index 0000000..657ffb1 --- /dev/null +++ b/arduino/victron-ble/victron-ble.ino @@ -0,0 +1,174 @@ +/* + Author: + David Sutton + + License: + Copyright (C) 2026 The Australian Ocean Lab (AusOcean). + + This file is part of NetSender. NetSender is free software: you can + redistribute it and/or modify it under the terms of the GNU + General Public License as published by the Free Software + Foundation, either version 3 of the License, or (at your option) + any later version. + + NetSender is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with NetSender in gpl.txt. If not, see + . +*/ + +#include +#include +#include +#include +#include +#include "KEY.h" + +// Uncomment to see verbose comments. +// #define VERBOSE + +CTR ctr; + +// MAC for the Victron Solar Charge Controller to read. +constexpr auto MAC = "f6:f3:6a:d7:9f:d9"; + +// Device instance to read. +BLEDevice victron; + +// decrypts a uint8_t array of data given a nonce value, using the key defined in "KEY.h" +// +// See: https://communityarchive.victronenergy.com/storage/attachments/extra-manufacturer-data-2022-12-14.pdf +// See: https://www.victronenergy.com/upload/documents/BlueSolar-HEX-protocol.pdf +void decrypt_payload(const uint8_t* data, const int data_len, const uint8_t nonce_hi, const uint8_t nonce_lo) { + // Clear the encryption blocks. + ctr.clear(); + + // Set the decryption key. + ctr.setKey(VICTRON_KEY, 16); + + // Set initial value (with nonce). + uint8_t iv[16] = {0}; + iv[0] = nonce_hi; + iv[1] = nonce_lo; + ctr.setIV(iv, 16); + + // Output plaintext. + uint8_t plaintext[24] = {0}; + + // Decrypt the cyphertext. + ctr.decrypt(plaintext, data, data_len); + + #ifdef VERBOSE + Serial.print("Data: "); + for (auto i = 0; i < data_len; i++){ + Serial.printf("%02X ", plaintext[i]); + } + Serial.println(); + #endif + + // Print received data. + Serial.printf("Charger state: \t\t%02X\n", plaintext[0]); + Serial.printf("Charger error: \t\t%02X\n", plaintext[1]); + Serial.printf("Battery voltage: \t%d0mV\n", (plaintext[3] << 8) + plaintext[2]); + Serial.printf("Battery current: \t%d00mA\n", (plaintext[5] << 8) + plaintext[4]); + Serial.printf("PV Yield Today: \t%d0Wh\n", (plaintext[7] << 8) + plaintext[6]); + Serial.printf("PV Power: \t\t%dW\n", (plaintext[9] << 8) + plaintext[8]); + Serial.printf("Load Current: \t\t%d00mA\n", ((plaintext[11]&0x01) << 8) + plaintext[10]); +} + +void setup() { + // Setup serial for logging. + Serial.begin(115200); + delay(100); + Serial.println("Initialised Serial"); + + // Setup Bluetooth Low Energy (BLE). + if (!BLE.begin()) { + Serial.println("starting Bluetooth® Low Energy module failed!"); + while (1); + } + + // Scan for the SCC, allowing duplicates. + if (!BLE.scanForAddress(MAC, true)) { + Serial.println("failed to perform BLE scan"); + while(1); + }; +} + +void loop() { + // Check if there is data available on BLE connection. + auto start_time = millis(); + while (millis()-start_time <= 10000) { + victron = BLE.available(); + if (victron) { + break; + } + } + + // Advertisement data. + uint8_t data[512]; + if (victron.hasAdvertisementData()) { + #ifdef VERBOSE + Serial.println("SCC has advertisement data"); + #endif + auto len = victron.advertisementDataLength(); + if (len > sizeof(data)) len = sizeof(data); // Prevent buffer overflow + + // Put ad-data into buffer. + victron.advertisementData(data, len); + + #ifdef VERBOSE + Serial.print("Raw: "); + for (size_t i = 0; i < len; i++) { + Serial.printf("%02X", data[i]); + } + Serial.println(); + #endif + + auto ad_len = data[0]; + auto ad_start = 1; + #ifdef VERBOSE + if (data[ad_start] == 0x01) { + Serial.println("Flags:"); + Serial.printf("\t%d - LE Limited Discoverable Mode\n", data[ad_start+1] & 0x1); + Serial.printf("\t%d - LE General Discoverable Mode\n", (data[ad_start+1] >> 1) & 0x1); + Serial.printf("\t%d - BR/EDR Not Supported.\n", (data[ad_start+1] >> 2) & 0x1); + Serial.printf("\t%d - Simultaneous LE and BR/EDR to Same Device Capable (Controller).\n", (data[ad_start+1] >> 3) & 0x1); + Serial.printf("\t%d - Previously Used\n", (data[ad_start+1] >> 4) & 0x1); + } else { + Serial.printf("Expected flag data type (0x01), got: %02X\n", data[ad_start]); + } + #endif + + ad_start = ad_start+ad_len+1; + ad_len = data[ad_start - 1]; + + #ifdef VERBOSE + Serial.printf("AD Len: %d\n", data[ad_start - 1]); + #endif + if (data[ad_start] == 0xFF) { + #ifdef VERBOSE + Serial.printf("Manufacturer Identifier: %02X%02X\n", data[ad_start+2], data[ad_start+1]); + Serial.printf("Manufacturer Data Record type: %02X\n", data[ad_start+3]); + Serial.printf("Extra Bytes: %02X %02X %02X\n", data[ad_start+4], data[ad_start+5], data[ad_start+6]); + Serial.printf("Record type: %02X\n", data[ad_start+7]); + Serial.printf("Nonce/Count: %02X%02X\n", data[ad_start+9], data[ad_start+8]); + Serial.printf("Encryption key byte 0: %02X\n", data[ad_start+10]); + #endif + + // Decrypt data. + decrypt_payload(&data[ad_start+11], ad_len-10, data[ad_start+8], data[ad_start+9]); + + Serial.println(); + } else { + Serial.printf("Expected manufacturer specific data type (0xFF), got: %02X\n", data[ad_start]); + } + Serial.println(); + } + + delay(5000); +}