// SPDX-License-Identifier: GPL-3.0-or-later #pragma once #include "tools_native.h" #include "engine_profile.h" static QJsonObject canIdentity(const FW_RX_PARAMS &p, int id) { return {{"can_id",id},{"uuid",QString::fromLatin1(p.uuid.toHex())},{"hardware",p.hw}, {"major",p.major},{"minor",p.minor},{"hardware_type",int(p.hwType)},{"test_firmware",p.isTestFw},{"custom_configs",p.customConfigNum}}; } static bool bmsCommandsSupported(const FW_RX_PARAMS &p) { if(p.hwType!=HW_TYPE_VESC_BMS || p.isTestFw!=0)return false; for(const auto &v:engineProfile().value("bms_profiles").toArray()) { const auto profile=v.toObject(); if(p.major==profile.value("major").toInt() && p.minor==profile.value("minor").toInt())return true; } return false; } // Use the original temporary route. Neither the local schema nor the local UUID // is replaced by the CAN reply; restore the route even when a check throws. class NativeCanTarget { VescInterface &vesc; bool &remote; public: NativeCanTarget(VescInterface &v, bool &flag, int id):vesc(v),remote(flag) { require(id>=0 && id<=253,"Invalid unicast CAN target"); require(!remote && !v.commands()->getSendCan() && !v.isIgnoringCanChanges(),"CAN route already owned"); remote=true;vesc.canTmpOverride(true,id); } ~NativeCanTarget(){vesc.canTmpOverrideEnd();remote=false;} FW_RX_PARAMS identify() { FW_RX_PARAMS p; require(Utility::getFwVersionBlocking(&vesc,&p,1500),"CAN target did not identify"); require(p.uuid.size()==12 && p.uuid!=QByteArray(12,'\0') && p.hw.size()>0 && p.hw.size()<=64,"CAN identity incomplete"); return p; } }; static void sendBmsCommand(Commands *cmd, const QString &action) { const QMap codes{{"charge_enable",COMM_BMS_SET_CHARGE_ALLOWED}, {"charge_disable",COMM_BMS_SET_CHARGE_ALLOWED},{"balance_enable",COMM_BMS_FORCE_BALANCE}, {"balance_disable",COMM_BMS_FORCE_BALANCE},{"reset_ah",COMM_BMS_RESET_COUNTERS}, {"reset_wh",COMM_BMS_RESET_COUNTERS},{"zero_current",COMM_BMS_ZERO_CURRENT_OFFSET}}; require(codes.contains(action),"Unknown BMS operation"); require(cmd->getSendCan() && cmd->getCanSendId()>=0 && cmd->getCanSendId()<=253,"BMS needs an exact CAN route"); require(cmd->getLimitedCompatibilityCommands().contains(codes.value(action)),"BMS command unsupported by transport firmware"); if(action=="charge_enable" || action=="charge_disable")cmd->bmsSetChargeAllowed(action=="charge_enable"); else if(action=="balance_enable" || action=="balance_disable")cmd->bmsForceBalance(action=="balance_enable"); else if(action=="reset_ah" || action=="reset_wh")cmd->bmsResetCounters(action=="reset_ah",action=="reset_wh"); else cmd->bmsZeroCurrentOffset(); } static void requireSameCanIdentity(const FW_RX_PARAMS &a, const FW_RX_PARAMS &b) { require(a.uuid==b.uuid && a.hw==b.hw && a.major==b.major && a.minor==b.minor && a.hwType==b.hwType && a.isTestFw==b.isTestFw, "CAN target identity changed"); }