+/*
+ * Bump this when adding new models[] above. Make sure the text buffer
+ * for the ID response can hold the longest sequence that we expect in
+ * the field which consists of: vendor + model [ + version ][ + serno ].
+ * Don't be too generous here, the maximum receive buffer size affects
+ * the timeout within which the first response character is expected.
+ */
+static const size_t id_text_buffer_size = 48;
+
+/*
+ * Check whether the device's "*IDN?" response matches a supported model.
+ * The caller already stripped off the optional serial number.
+ */
+static gboolean model_matches(const struct korad_kaxxxxp_model *model,
+ const char *id_text)
+{
+ gboolean matches;
+ gboolean opt_version, skip_vendor, accept_trail;
+ const char *want;
+
+ if (!model)
+ return FALSE;
+
+ /*
+ * When the models[] entry contains a specific response text,
+ * then expect to see this very text in literal form. This
+ * lets the driver map weird and untypical responses to a
+ * specific set of display texts for vendor and model names.
+ * Accept an optionally trailing version if models[] says so.
+ */
+ if (model->id && model->id[0]) {
+ opt_version = model->quirks & KORAD_QUIRK_ID_OPT_VERSION;
+ if (!opt_version) {
+ matches = g_strcmp0(id_text, model->id) == 0;
+ if (!matches)
+ return FALSE;
+ sr_dbg("Matches expected ID text: '%s'.", model->id);
+ return TRUE;
+ }
+ matches = g_str_has_prefix(id_text, model->id);
+ if (!matches)
+ return FALSE;
+ id_text += strlen(model->id);
+ while (isspace((int)*id_text))
+ id_text++;
+ if (*id_text == 'V') {
+ id_text++;
+ while (*id_text == '.' || isdigit((int)*id_text))
+ id_text++;
+ while (isspace((int)*id_text))
+ id_text++;
+ }
+ if (*id_text)
+ return FALSE;
+ sr_dbg("Matches expected ID text [vers]: '%s'.", model->id);
+ return TRUE;
+ }
+
+ /*
+ * A more generic approach which covers most devices: Check
+ * for the very vendor and model names which also are shown
+ * to users (the display texts). Weakened to match responses
+ * more widely: Case insensitive checks, optional whitespace
+ * in responses, optional version details. Optional trailing
+ * garbage. Optional omission of the vendor name. Shall match
+ * all the devices which were individually listed in earlier
+ * implementations of the driver, and shall also match firmware
+ * versions that were not listed before.
+ */
+ skip_vendor = model->quirks & KORAD_QUIRK_ID_NO_VENDOR;
+ accept_trail = model->quirks & KORAD_QUIRK_ID_TRAILING;
+ if (!skip_vendor) {
+ want = model->vendor;
+ matches = g_ascii_strncasecmp(id_text, want, strlen(want)) == 0;
+ if (!matches)
+ return FALSE;
+ id_text += strlen(want);
+ while (isspace((int)*id_text))
+ id_text++;
+ }
+ want = model->name;
+ matches = g_ascii_strncasecmp(id_text, want, strlen(want)) == 0;
+ if (!matches)
+ return FALSE;
+ id_text += strlen(want);
+ while (isspace((int)*id_text))
+ id_text++;
+ if (*id_text == 'V') {
+ /* TODO Isolate and (also) return version details? */
+ id_text++;
+ while (*id_text == '.' || isdigit((int)*id_text))
+ id_text++;
+ while (isspace((int)*id_text))
+ id_text++;
+ }
+ if (accept_trail) {
+ /*
+ * TODO Determine how many non-printables to accept here
+ * and how strict to check for "known" garbage variants.
+ */
+ switch (*id_text) {
+ case '\x01':
+ case '\xbc':
+ id_text++;
+ break;
+ case '\x00':
+ /* EMPTY */
+ break;
+ default:
+ return FALSE;
+ }
+ }
+ if (*id_text)
+ return FALSE;
+ sr_dbg("Matches generic '[vendor] model [vers] [trail]' pattern.");
+ return TRUE;
+}
+
+/* Lookup a model from an ID response text. */
+static const struct korad_kaxxxxp_model *model_lookup(const char *id_text)
+{
+ size_t idx;
+ const struct korad_kaxxxxp_model *check;
+
+ if (!id_text || !*id_text)
+ return NULL;
+ sr_dbg("Looking up: [%s].", id_text);
+
+ for (idx = 0; idx < ARRAY_SIZE(models); idx++) {
+ check = &models[idx];
+ if (!check->name || !check->name[0])
+ continue;
+ if (!model_matches(check, id_text))
+ continue;
+ sr_dbg("Found: [%s] [%s]", check->vendor, check->name);
+ return check;
+ }
+ sr_dbg("Not found");
+
+ return NULL;
+}
+