12-OBD-Speed: read-only UDS DID scan of the car's modules

- Log page now runs a diagnostic scan instead of CAN listening (the Fiesta's OBD
  port carries no broadcast traffic): unfiltered check, module discovery
  0x700-0x7EF (22F188, then 3E00 with 1 s timeout), all 65536 DIDs per module,
  progress in NVS, speed/voltage polled every 2 s in between
- Result (drives 2026-11-12..18): only the PCM 0x7E0 answers; 389 DIDs supported
  (logs in docs/canlog/scan, scan2)
- Reset scan button, BLE connection interval request, progress lines
- CLAUDE.md: screen-has-priority rule, web UI future idea, findings

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
mangavalkandClaude Opus 5.5 committed 2026-11-18 16:06:31 +01:00
1 parent 60cd7cf262
commit c9ad39c8ed
27 files changed
+15622 -380

No files matched your search

+470 -217
View File
@@ -12,13 +12,7 @@
#define VOLTAGE_INTERVAL_MS 2000 // ATRV while driving
#define IDLE_VOLTAGE_INTERVAL_MS 30000 // ATRV while idle (adapter-internal, doesn't wake the car's bus)
#define WAKE_VOLTAGE 13.2f // above this the alternator is charging: engine running
#define LOG_OBD_INTERVAL_MS 2000 // speed while logging (between monitor windows)
#define LOG_SURVEY_MS 1300 // per ID range; > 1 s so slow (1 Hz) messages are seen
#define LOG_SURVEY_MAX_LINES 300 // more in one window: split the range
#define LOG_CAPTURE_MS 1300 // waiting for one frame of an ID
#define LOG_RAW_MS 2000
#define LOG_RESURVEY_MS (10 * 60 * 1000)
#define LOG_TARGET_EVERY 8 // the candidate IDs after every 8 others
#define LOG_OBD_INTERVAL_MS 2000 // speed while scanning (between requests)
// ---------- shared state (protected by `mtx`) ----------
static SemaphoreHandle_t mtx;
@@ -46,17 +40,8 @@ static NimBLERemoteCharacteristic *chr_write = nullptr, *chr_notify = nullptr;
static Preferences prefs;
static uint32_t next_attempt = 0, next_voltage = 0;
static int fails = 0;
static bool scan_ready = false; // diagnostic scan set up on this connection (see below)
// CAN logging (see can_log.h), worker-owned
enum class LogMode
{
Off, // not logging (or not set up since the last connect)
Survey, // finding the IDs on the bus, range by range
Capture, // one frame of every known ID in turn
Raw, // adapter can't filter by wildcard: unfiltered monitor windows
NoCan, // not a CAN car: only speed/voltage are logged
};
static LogMode log_mode = LogMode::Off;
static void lock() { xSemaphoreTake(mtx, portMAX_DELAY); }
static void unlock() { xSemaphoreGive(mtx); }
@@ -261,12 +246,12 @@ static void disconnect()
if (client && client->isConnected())
client->disconnect();
speed_kmh = -1;
if (log_mode != LogMode::Off)
if (scan_ready)
{
can_log_line("E disconnected");
can_log_set_phase("Adapter not connected");
}
log_mode = LogMode::Off; // ATZ on reconnect resets the adapter: set up again
scan_ready = false; // ATZ on reconnect resets the adapter: set up again
}
// Finds the ELM UART characteristics: service FFF0 (verified: FFF1 notify + write), else any notify+write pair
@@ -326,6 +311,7 @@ static bool connect_and_init()
set_status("Adapter not found");
return false;
}
client->updateConnParams(6, 12, 0, 400); // 7.5-15 ms interval (the scan sends thousands of requests)
if (!find_characteristics() || !chr_notify->subscribe(chr_notify->canNotify(), on_notify))
{
set_status("Not an ELM adapter?");
@@ -391,181 +377,485 @@ static void poll_speed()
}
}
// ---------- CAN logging ----------
// Ford candidates from opendbc (ford_lincoln_base_pt.dbc, newer Fords - to be confirmed on the Fiesta Mk8):
// 0x165 EngBrakeData: Veh_V_DsplyCcSet (cruise set speed as shown), 0x202: Veh_V_RqCcSet (ACC set speed),
// 0x3D9 IPMA_Data2: IsaVLim_D_Rq (speed limit from the camera)
static const uint16_t LOG_TARGETS[CANLOG_SLOTS] = {0x165, 0x202, 0x3D9};
static std::vector<String> survey_queue; // ID patterns still to listen to ("3XX", "3DX", "3D9")
static std::vector<uint16_t> log_ids; // all IDs seen, sorted
static size_t capture_index = 0;
static uint32_t capture_count = 0;
static uint32_t next_log_obd = 0, next_survey = 0;
static bool exact_filter = false; // adapter supports ATCRA hhh
static int id_digits = 3; // 11-bit IDs; 29-bit in Raw mode
static bool caf_on = true; // CAN auto formatting (ISO-TP), the adapter's default
// ---------- diagnostic scan (while logging is on, see can_log.h) ----------
// The Fiesta Mk8's OBD port only carries diagnostic traffic (gateway; first test drive, docs/canlog), so the
// cruise set speed and the speed limit have to be asked from the modules. Only UDS ReadDataByIdentifier (0x22)
// in the default session is used: read-only, nothing in the car is changed. Steps (progress kept in NVS "scan"):
// 1. unfiltered monitor check, once: is there really no broadcast traffic?
// 2. module discovery: 22 F188 (software number) to every address 0x700..0x7EF, reply on address + 8
// 3. DID scan of the modules, most promising first; every positive reply is logged with its data
enum class ScanStep : uint8_t
{
RawCheck,
Discover,
Dids,
Done,
};
// Ford: IPMA (camera, speed signs), CCM (radar, ACC), IPC (cluster), PCM; other modules found come after these
static const uint16_t SCAN_PRIORITY[] = {0x706, 0x764, 0x720, 0x7E0};
// order in which the DID space is walked: likely Ford live-data ranges first
static const uint16_t SCAN_SEGMENTS[][2] = {
{0xD000, 0xDFFF}, {0x4000, 0x4FFF}, {0x0000, 0x3FFF}, {0x5000, 0xCFFF}, {0xE000, 0xFFFF}};
static bool scan_loaded = false; // progress read from NVS
static ScanStep scan_step = ScanStep::RawCheck;
static std::vector<uint16_t> scan_modules; // discovered, in scan order
static uint32_t scan_module = 0; // index into scan_modules
static uint32_t scan_pos = 0; // discovery: address; DID scan: position in the walk (0..65535)
static uint32_t scan_found = 0; // positive replies in the current module
static uint32_t scan_unsaved = 0;
static int scan_silent = 0; // consecutive requests without any reply
static uint16_t current_header = 0x7DF;
static uint32_t next_scan_obd = 0;
static bool count_suffix = false; // adapter understands "<request>1" (return after the first reply)
static bool caf_on = true; // CAN auto formatting (ISO-TP), the adapter's default
static volatile bool req_scan_reset = false;
static bool scan_probed = false; // second module search done (NVS)
static uint16_t probe_addr = 0; // 0 = not started on this connection
static int probe_new = 0;
static int multi_did = 0; // 0 = not tested, 1 = several DIDs per request work, 2 = one at a time
static uint16_t multi_module = 0; // module multi_did was tested on
static uint32_t progress_ms = 0;
// Monitoring wants the raw frames (CAF off), OBD requests need CAF on (adds/strips the ISO-TP byte)
static String hex3(uint16_t v)
{
char b[4];
snprintf(b, sizeof(b), "%03X", v);
return b;
}
static String hex4(uint16_t v)
{
char b[5];
snprintf(b, sizeof(b), "%04X", v);
return b;
}
// Monitoring wants the raw frames (CAF off), requests need CAF on (adds/strips the ISO-TP bytes)
static void set_caf(bool on)
{
if (on != caf_on && elm_command(on ? "ATCAF1" : "ATCAF0", 1500).length())
caf_on = on;
}
// "3D9FE00000000000" (ATH1, ATS0) -> id "3D9", data "FE00000000000000"
static bool parse_frame(String line, String &id, String &data)
static uint16_t did_at(uint32_t pos)
{
line.replace(" ", "");
if (line.length() < (unsigned)id_digits || (line.length() - id_digits) % 2)
return false;
for (unsigned i = 0; i < line.length(); i++)
if (!isxdigit((unsigned char)line[i]))
return false;
line.toUpperCase();
id = line.substring(0, id_digits);
data = line.substring(id_digits);
return true;
}
static void note_id(uint16_t id)
{
auto it = std::lower_bound(log_ids.begin(), log_ids.end(), id);
if (it == log_ids.end() || *it != id)
log_ids.insert(it, id);
}
// Logs the received lines: frames (at most `per_id` per ID, 0 = all) and adapter messages
static void log_lines(const std::vector<String> &lines, int per_id, bool *overflow)
{
std::vector<std::pair<String, int>> counts;
for (const String &line : lines)
for (auto &seg : SCAN_SEGMENTS)
{
String id, data;
if (!parse_frame(line, id, data))
{
if (line.indexOf("BUFFER FULL") >= 0 && overflow)
*overflow = true;
can_log_line("E " + line);
continue;
}
if (id_digits == 3)
note_id((uint16_t)strtol(id.c_str(), nullptr, 16));
int n = 0;
for (auto &c : counts)
if (c.first == id)
n = ++c.second;
if (n == 0)
counts.push_back({id, n = 1});
if (per_id == 0 || n <= per_id)
can_log_line("F " + id + " " + data);
for (int s = 0; s < CANLOG_SLOTS; s++)
if (id_digits == 3 && strtol(id.c_str(), nullptr, 16) == LOG_TARGETS[s])
can_log_set_frame(s, data);
uint32_t len = seg[1] - seg[0] + 1;
if (pos < len)
return seg[0] + pos;
pos -= len;
}
return 0xFFFF;
}
static String id_hex(uint16_t id)
static void scan_save()
{
char b[4];
snprintf(b, sizeof(b), "%03X", id);
return b;
Preferences p;
p.begin("scan", false);
p.putUChar("step", (uint8_t)scan_step);
p.putBytes("mods", scan_modules.data(), scan_modules.size() * sizeof(uint16_t));
p.putUInt("mod", scan_module);
p.putUInt("pos", scan_pos);
p.putUInt("found", scan_found);
p.putBool("probed", scan_probed);
p.end();
scan_unsaved = 0;
}
// Waits for one frame of `id`
static void capture_id(uint16_t id)
static void scan_load()
{
if (elm_command("ATCRA " + id_hex(id), 1500).length() == 0)
return note_no_reply();
std::vector<String> lines;
if (!elm_monitor(LOG_CAPTURE_MS, 1, lines))
return note_no_reply();
Preferences p;
p.begin("scan", true);
scan_step = (ScanStep)p.getUChar("step", (uint8_t)ScanStep::RawCheck);
scan_modules.resize(p.getBytesLength("mods") / sizeof(uint16_t));
if (scan_modules.size())
p.getBytes("mods", scan_modules.data(), scan_modules.size() * sizeof(uint16_t));
scan_module = p.getUInt("mod", 0);
scan_pos = p.getUInt("pos", 0);
scan_found = p.getUInt("found", 0);
scan_probed = p.getBool("probed", false);
p.end();
if (scan_step > ScanStep::Done)
scan_step = ScanStep::RawCheck;
scan_loaded = true;
}
static void scan_reset()
{
scan_step = ScanStep::RawCheck;
scan_modules.clear();
scan_module = scan_pos = scan_found = 0;
scan_probed = false;
scan_save();
can_log_line("E scan reset");
}
// Header (request address) + receive filter (reply address = request + 8); 0x7DF = default OBD broadcast
static void use_header(uint16_t addr)
{
if (addr == current_header)
return;
elm_command("ATSH " + hex3(addr), 1500);
elm_command(addr == 0x7DF ? String("ATAR") : "ATCRA " + hex3(addr + 8), 1500);
current_header = addr;
}
// Reply without spaces / multi-frame line numbers: "62D00012", "7F2231", "" = nothing, else adapter text
static String clean_reply(const String &reply)
{
String out;
int start = 0;
int line_no = 0;
while (start <= (int)reply.length())
{
int nl = reply.indexOf('\n', start);
if (nl < 0)
nl = reply.length();
String line = reply.substring(start, nl);
start = nl + 1;
line.replace(" ", "");
line.trim();
if (!line.length())
continue;
if (line.length() > 2 && line[1] == ':')
line = line.substring(2); // "0:62D0..." multi-frame part
else if (line_no == 0 && line.length() == 3 && reply.indexOf(':') >= 0)
continue; // multi-frame byte count "014"
out += line;
line_no++;
}
out.toUpperCase();
return out;
}
static String scan_request(uint16_t addr, const String &req)
{
use_header(addr);
String r = elm_command(count_suffix ? req + "1" : req, 1500);
if (r.length() == 0)
{
note_no_reply();
return "?TIMEOUT";
}
fails = 0;
if (lines.empty())
can_log_line("N " + id_hex(id));
log_lines(lines, 0, nullptr);
r = clean_reply(r);
if (r.indexOf("7F" + req.substring(0, 2) + "78") >= 0)
{
vTaskDelay(pdMS_TO_TICKS(300)); // "response pending": let the late reply pass (next command clears it)
}
return r;
}
// Listens to one ID range ("3XX"); a range too busy for the adapter is split into 16 smaller ones
static void survey_range(const String &pattern)
static bool is_reply(const String &r, const String &req)
{
if (elm_command("ATCRA " + pattern, 1500).length() == 0)
return note_no_reply();
std::vector<String> lines;
if (!elm_monitor(LOG_SURVEY_MS, LOG_SURVEY_MAX_LINES, lines))
return note_no_reply();
fails = 0;
bool overflow = (int)lines.size() >= LOG_SURVEY_MAX_LINES;
log_lines(lines, 2, &overflow);
int x = pattern.indexOf('X');
if (overflow && x >= 0)
for (int d = 0; d < 16; d++)
{
String child = pattern;
child.setCharAt(x, "0123456789ABCDEF"[d]);
survey_queue.push_back(child);
}
return r.startsWith(String("6") + req.substring(1, 2)) || r.startsWith("7F" + req.substring(0, 2));
}
static void start_survey()
{
survey_queue.clear();
for (int i = 0; i < 8; i++)
survey_queue.push_back(String(i) + "XX");
log_mode = LogMode::Survey;
}
// Called once per connection while logging is on: checks the protocol and what the adapter can filter
static void log_setup()
// Called once per connection while logging is on
static void scan_setup()
{
if (!scan_loaded)
scan_load();
can_log_line("C " + (saved_name.length() ? saved_name : saved_addr) + " " + elm_command("ATI", 1500));
String dp = elm_command("ATDPN", 1500); // "A6" = auto, ISO 15765-4 CAN 11 bit 500 kbit
can_log_line("P " + dp);
char p = dp.length() ? dp[dp.length() - 1] : '?';
next_log_obd = 0;
caf_on = true; // adapter was just reset (ATZ)
if (p < '6' || p > '9')
{
log_mode = LogMode::NoCan;
can_log_set_phase("Not a CAN protocol (" + dp + ")");
return;
}
// silent monitoring: the adapter never acknowledges or disturbs frames on the bus
can_log_line("E ATCSM1 " + elm_command("ATCSM1", 1500));
elm_command("ATH1", 1500); // IDs in front of the data
id_digits = (p == '7' || p == '9') ? 8 : 3;
exact_filter = id_digits == 3 && elm_command("ATCRA 3D9", 1500).indexOf('?') < 0;
bool wildcard = exact_filter && elm_command("ATCRA 7XX", 1500).indexOf('?') < 0;
elm_command("ATAR", 1500);
if (wildcard)
{
start_survey();
}
else
{
log_mode = LogMode::Raw;
can_log_line(String("E no wildcard filter") + (exact_filter ? "" : ", no ID filter") + ": unfiltered");
}
caf_on = true; // adapter was just reset (ATZ)
current_header = 0x7DF;
elm_command("ATST19", 1500); // reply timeout 100 ms (default 200): modules answer within a few ms
String r = elm_command("01001", 1500);
count_suffix = r.indexOf("4100") >= 0 || r.indexOf("41 00") >= 0;
can_log_line(String("E count suffix ") + (count_suffix ? "yes" : "no") + ", scan step " + (int)scan_step +
", module " + scan_module + ", pos " + scan_pos);
next_scan_obd = 0;
probe_addr = 0;
multi_module = 0;
progress_ms = millis();
scan_ready = true;
}
static void log_stop()
static void scan_stop()
{
set_caf(true);
elm_command("ATAR", 1500);
elm_command("ATH0", 1500);
log_mode = LogMode::Off;
use_header(0x7DF);
elm_command("ATST32", 1500);
scan_ready = false;
can_log_set_phase("");
}
// One unit of logging work (one ID range / one ID / one raw window), with speed + voltage every 2 s
static void scan_raw_check()
{
can_log_set_phase("Listening (no filter)");
int frames = 0;
for (int caf = 0; caf < 2; caf++)
{
set_caf(caf == 1);
elm_command("ATH1", 1500);
std::vector<String> lines;
bool ok = elm_monitor(5000, 200, lines);
elm_command("ATH0", 1500);
for (const String &l : lines)
{
can_log_line(String("F ") + l);
if (l.indexOf("NO DATA") < 0)
frames++;
}
if (!ok)
return note_no_reply();
}
set_caf(true);
can_log_line("E raw check: " + String(frames) + " lines");
scan_step = ScanStep::Discover;
scan_pos = 0x700;
scan_save();
}
static void scan_discover_step()
{
if (scan_pos == 0x700)
scan_modules.clear();
if (scan_pos == 0x7DF) // functional (broadcast) address
scan_pos++;
if (scan_pos > 0x7EF)
{
bool pcm = std::find(scan_modules.begin(), scan_modules.end(), 0x7E0) != scan_modules.end();
String list;
for (uint16_t m : scan_modules)
list += " " + hex3(m);
can_log_line("I" + list);
if (!pcm)
{
// not even the engine computer answered: ignition off? try again later
can_log_line("E discovery found no PCM, again");
scan_pos = 0x700;
vTaskDelay(pdMS_TO_TICKS(2000));
return;
}
// scan order: the promising modules first, then the rest
std::vector<uint16_t> order;
for (uint16_t p : SCAN_PRIORITY)
if (std::find(scan_modules.begin(), scan_modules.end(), p) != scan_modules.end())
order.push_back(p);
for (uint16_t m : scan_modules)
if (std::find(order.begin(), order.end(), m) == order.end())
order.push_back(m);
scan_modules = order;
scan_step = ScanStep::Dids;
scan_module = scan_pos = scan_found = 0;
scan_save();
return;
}
uint16_t addr = scan_pos;
can_log_set_phase("Finding modules " + hex3(addr) + ", " + String(scan_modules.size()) + " found");
String r = scan_request(addr, "22F188");
if (r == "?TIMEOUT")
return;
if (is_reply(r, "22F188"))
{
scan_modules.push_back(addr);
can_log_line("D " + hex3(addr) + " " + r);
}
else if (r.length() && r != "NODATA")
can_log_line("E " + hex3(addr) + " " + r);
scan_pos++;
}
// Second module search (test drive 2 found only the PCM with 22F188 and a 100 ms timeout): TesterPresent (3E00,
// single-frame reply) with a 1 s timeout to every address not known yet. Runs once (NVS "probed").
static void scan_probe_step()
{
if (probe_addr == 0)
{
probe_addr = 0x700;
probe_new = 0;
elm_command("ATSTFF", 1500); // ~1 s
}
while (probe_addr == 0x7DF ||
std::find(scan_modules.begin(), scan_modules.end(), probe_addr) != scan_modules.end())
probe_addr++;
if (probe_addr > 0x7EF)
{
elm_command("ATST19", 1500);
String list;
for (uint16_t m : scan_modules)
list += " " + hex3(m);
can_log_line("I probe, " + String(probe_new) + " new:" + list);
if (probe_new)
{
// promising modules first again; the PCM scan starts over (it had only just begun)
std::vector<uint16_t> order;
for (uint16_t p : SCAN_PRIORITY)
if (std::find(scan_modules.begin(), scan_modules.end(), p) != scan_modules.end())
order.push_back(p);
for (uint16_t m : scan_modules)
if (std::find(order.begin(), order.end(), m) == order.end())
order.push_back(m);
if (order[0] != scan_modules[scan_module < scan_modules.size() ? scan_module : 0])
scan_module = scan_pos = scan_found = 0;
scan_modules = order;
}
scan_probed = true;
scan_save();
return;
}
uint16_t addr = probe_addr;
can_log_set_phase("Probing modules " + hex3(addr) + " (1 s timeout)");
String r = scan_request(addr, "3E00");
if (r == "?TIMEOUT")
return;
if (r.startsWith("7E") || r.startsWith("7F3E"))
{
String id = scan_request(addr, "22F188");
can_log_line("D " + hex3(addr) + " 3E:" + r + " F188:" + id);
scan_modules.push_back(addr);
probe_new++;
}
else if (r.length() && r != "NODATA")
can_log_line("E " + hex3(addr) + " 3E00 " + r);
probe_addr++;
}
// Does the module answer a request with several DIDs (ISO 14229: only the supported ones are returned)?
// F1FF / F1FE are practically never used, F188 always is (the module was found with it).
static void scan_test_multi(uint16_t mod)
{
String r = scan_request(mod, "22F1FFF188F1FE");
multi_did = r.startsWith("62F188") ? 1 : 2;
can_log_line("E multi-DID " + hex3(mod) + ": " + (multi_did == 1 ? "yes" : "no") + " (" + r.substring(0, 12) + ")");
multi_module = mod;
}
// Logs one DID's reply; returns false if the module looks gone (no reply)
static bool scan_single(uint16_t mod, uint16_t did)
{
String r = scan_request(mod, "22" + hex4(did));
if (r.startsWith("62" + hex4(did)))
{
scan_found++;
String data = r.substring(6);
can_log_line("S " + hex3(mod) + " " + hex4(did) + " " + data);
can_log_set_frame(2, "Last: " + hex3(mod) + " " + hex4(did) + " = " + data.substring(0, 16));
}
else if (r.startsWith("7F22"))
{
String nrc = r.substring(4, 6);
if (nrc != "31") // 31 = not supported; others (33 security, 22 conditions...) are worth knowing
can_log_line("R " + hex3(mod) + " " + hex4(did) + " " + nrc);
}
else if (r == "NODATA" || r.length() == 0)
return false;
else if (r != "?TIMEOUT")
can_log_line("E " + hex3(mod) + " " + hex4(did) + " " + r);
return true;
}
static void scan_did_step()
{
if (scan_module >= scan_modules.size())
{
scan_step = ScanStep::Done;
scan_save();
can_log_line("E scan done");
return;
}
uint16_t mod = scan_modules[scan_module];
if (scan_pos >= 0x10000)
{
can_log_line("E module " + hex3(mod) + " done, " + String(scan_found) + " DIDs");
scan_module++;
scan_pos = scan_found = 0;
scan_save();
return;
}
if (multi_module != mod)
scan_test_multi(mod);
// up to 3 DIDs per request (fits one CAN frame); a hit is then asked one by one
int n = multi_did == 1 ? (int)std::min<uint32_t>(3, 0x10000 - scan_pos) : 1;
uint16_t dids[3];
String req = "22";
for (int i = 0; i < n; i++)
{
dids[i] = did_at(scan_pos + i);
req += hex4(dids[i]);
}
bool answered = true;
if (n == 1)
{
answered = scan_single(mod, dids[0]);
}
else
{
String r = scan_request(mod, req);
if (r == "?TIMEOUT")
return;
if (r == "NODATA" || r.length() == 0)
answered = false;
else if (r.startsWith("7F2231"))
; // none of them supported
else if (r.startsWith("7F2213") || r.startsWith("7F2212"))
{
can_log_line("E multi-DID refused at " + hex4(dids[0]) + ", single from now on");
multi_did = 2;
return;
}
else
for (int i = 0; i < n; i++)
scan_single(mod, dids[i]);
}
if (!answered)
{
if (++scan_silent >= 20)
{
// module stopped answering (ignition off / asleep): go back and wait
can_log_line("E " + hex3(mod) + " not answering at " + hex4(dids[0]));
scan_pos = scan_pos >= 19u * n ? scan_pos - 19 * n : 0;
scan_silent = 0;
vTaskDelay(pdMS_TO_TICKS(5000));
return;
}
}
else
scan_silent = 0;
uint32_t before = scan_pos;
scan_pos += n;
scan_unsaved += n;
if (scan_unsaved >= 64)
scan_save();
if ((before >> 6) != (scan_pos >> 6))
{
char ph[64];
snprintf(ph, sizeof(ph), "Scan %03X (%u/%u) %04X %.1f %%", mod, (unsigned)scan_module + 1,
(unsigned)scan_modules.size(), dids[0], scan_pos * 100.0f / 65536);
can_log_set_phase(ph);
can_log_set_frame(1, "Found in " + hex3(mod) + ": " + String(scan_found));
}
if ((before >> 9) != (scan_pos >> 9))
{
// progress line every 512 DIDs, with the rate
uint32_t now = millis();
can_log_line("E progress " + hex3(mod) + " " + hex4(dids[0]) + " pos " + String(scan_pos) + ", " +
String(scan_found) + " found, " + String((now - progress_ms) / 512) + " ms/DID");
progress_ms = now;
}
}
// One unit of scan work (one request), with speed + voltage every 2 s
static void log_step()
{
if (log_mode == LogMode::Off)
log_setup();
if ((int32_t)(millis() - next_log_obd) >= 0)
if (req_scan_reset)
{
next_log_obd = millis() + LOG_OBD_INTERVAL_MS;
elm_command("ATAR", 1500); // the ID filter would hide the ECU's reply
set_caf(true);
req_scan_reset = false;
if (!scan_loaded)
scan_load();
scan_reset();
}
if (!scan_ready)
scan_setup();
if ((int32_t)(millis() - next_scan_obd) >= 0)
{
next_scan_obd = millis() + LOG_OBD_INTERVAL_MS;
use_header(0x7DF);
poll_speed();
if ((int32_t)(millis() - next_voltage) >= 0)
{
@@ -573,69 +863,30 @@ static void log_step()
read_voltage();
can_log_line("B " + String(voltage, 1));
}
String mods;
for (uint16_t m : scan_modules)
mods += " " + hex3(m);
can_log_set_frame(0, "Modules:" + (mods.length() ? mods : String(" -")));
return;
}
if (!client->isConnected() || !chr_write)
return;
if (log_mode != LogMode::NoCan)
set_caf(false);
switch (log_mode)
switch (scan_step)
{
case LogMode::Survey:
if (survey_queue.empty())
{
String ids;
for (uint16_t id : log_ids)
ids += " " + id_hex(id);
can_log_line("I" + ids);
log_mode = LogMode::Capture;
next_survey = millis() + LOG_RESURVEY_MS;
break;
}
{
String pattern = survey_queue.front();
survey_queue.erase(survey_queue.begin());
can_log_set_phase("Survey " + pattern + ": " + log_ids.size() + " IDs, " + survey_queue.size() + " ranges left");
survey_range(pattern);
}
case ScanStep::RawCheck:
scan_raw_check();
break;
case LogMode::Capture:
if ((int32_t)(millis() - next_survey) >= 0)
{
start_survey(); // now and then look for IDs that were quiet before
break;
}
if (capture_count++ % (LOG_TARGET_EVERY + 1) == 0 || log_ids.empty())
{
for (uint16_t t : LOG_TARGETS)
capture_id(t);
}
case ScanStep::Discover:
scan_discover_step();
break;
case ScanStep::Dids:
if (!scan_probed)
scan_probe_step();
else
{
uint16_t id = log_ids[capture_index++ % log_ids.size()];
if (std::find(std::begin(LOG_TARGETS), std::end(LOG_TARGETS), id) == std::end(LOG_TARGETS))
capture_id(id);
}
can_log_set_phase("Capturing " + String(log_ids.size()) + " IDs");
scan_did_step();
break;
case LogMode::Raw:
{
can_log_set_phase("Unfiltered (adapter can't filter)");
std::vector<String> lines;
if (!elm_monitor(LOG_RAW_MS, LOG_SURVEY_MAX_LINES, lines))
return note_no_reply();
fails = 0;
log_lines(lines, 0, nullptr);
if (exact_filter)
{
for (uint16_t t : LOG_TARGETS)
capture_id(t);
elm_command("ATAR", 1500);
}
break;
}
default: // NoCan: only speed and voltage
default:
can_log_set_phase("Scan finished");
vTaskDelay(pdMS_TO_TICKS(100));
break;
}
@@ -756,14 +1007,14 @@ static void worker_task(void *)
continue;
}
// CAN logging on: monitor windows with speed/voltage in between
// logging on: diagnostic scan with speed/voltage in between
if (can_log_enabled())
{
log_step();
continue;
}
if (log_mode != LogMode::Off)
log_stop();
if (scan_ready)
scan_stop();
if ((int32_t)(millis() - next_voltage) >= 0)
{
@@ -839,3 +1090,5 @@ void obd_select_device(const ObdDevInfo &dev)
}
void obd_forget_device() { req_forget = true; }
void obd_scan_reset() { req_scan_reset = true; }