rcbench
Servo and ESC test bench: panel, IOmcu and shared C
Loading...
Searching...
No Matches
esc_registry.c File Reference

The profiles the bench offers: built in, with the card's in front. More...

#include "esc_profile.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

Data Structures

struct  slot_t
 One card profile held by the registry. More...

Functions

static const esc_profile_t * builtin_find (const char *id)
 Find a built-in profile by id.
static slot_t * over_find (const char *id)
 Find a card profile by id.
bool esc_brand_same (const char *a, const char *b)
 Whether a and b name one maker: the letters A to Z folded, every other byte itself, as the list groups them.
static size_t maker_models_with (const esc_profile_t *p)
 The models p's maker would list with p in place of the profile of its id.
static bool refuse (void *block, const char **why, const char *text)
 Refuse a card profile: free its block and set the reason.
bool esc_profiles_override (const esc_profile_t *p, void *block)
 Add a parsed profile from the card.
bool esc_profiles_override_why (const esc_profile_t *p, void *block, const char **why)
 esc_profiles_override(), with why it refused in why (a static string; may be NULL): "no profile", "more than 32 on the card", or "its maker would list more than 512 models" – the maker's models across every profile, built in and from the card, with this one in place of any it replaces, makers compared with the letters A to Z folded.
void esc_profiles_clear_overrides (void)
 Drop every card profile; the built-in ones remain.
size_t esc_profiles_override_count (void)
 Card profiles held, for a status line.
size_t esc_profiles_count (void)
 Every profile the bench offers: the built-in ones with card files in their place, then card profiles with new ids.
const esc_profile_t * esc_profiles_at (size_t i)
 The profile at index i of the list the bench offers.
const esc_profile_t * esc_profiles_find (const char *id)
 NULL when no profile has this id.
bool esc_profiles_is_override (const esc_profile_t *p)
 Whether the profile at p came from the card.
bool esc_profile_file_is (const char *file_name, const char *id)
 Whether file_name is the name the profile id belongs in: the id with ".json" after it, letters compared without regard to case, since a card written on another computer may change them.
static unsigned char fold (unsigned char c)
 Fold an ASCII capital letter to lower case.
static unsigned char joined_at (const char *a, size_t na, const char *b, size_t i)
 Byte i of a, a space and b read as one text of n bytes; b NULL is a alone.
static bool match_joined (const char *a, const char *b, const char *pattern)
 The wildcard match, with a "*" implied at either end.
bool esc_text_matches (const char *text, const char *pattern)
 Whether pattern is found in text.
bool esc_profile_matches (const esc_profile_t *p, const char *pattern)
 Whether pattern (as esc_text_matches()) is found in the profile's manufacturer and name read as one text, "brand family", or in its manufacturer and one of its models' names, "brand model": a family such as "JAZZ / MINIJAZZ" names its sizes only in its models ("JAZZ 55 LV").
bool esc_item_applies (const esc_profile_t *p, unsigned item, int model)
 Whether item item of p is on model model: an item without applies_to is on every model; one with it only on the models it names.
bool esc_model_matches (const esc_profile_t *p, unsigned model, const char *pattern)
 Whether pattern finds model model of p: in the maker and the family read as one text, which every model of the family shares, or in the maker and the model's own name, as esc_profile_matches() reads them.
unsigned esc_profile_manual_count (const esc_profile_t *p, esc_manual_when_t when)
 How many of the profile's manual steps are due at when.

Variables

static slot_t s_over [ESC_PROFILE_MAX_OVERRIDES]
 Card profiles, in the order the card was read.
static size_t s_over_n
 Number of card profiles held in s_over.

Detailed Description

The profiles the bench offers: built in, with the card's in front.

See esc_profile.h.

A card profile with a built-in id takes that profile's place in the list, so an operator who corrects a default on the card sees one entry, not two. A card profile with a new id follows the built-in ones in the order the card was read.

Not thread-safe: the panel fills it once at start-up, before anything reads it.

SPDX-License-Identifier: MIT

Function Documentation

◆ builtin_find()

const esc_profile_t * builtin_find ( const char * id)
static

Find a built-in profile by id.

Parameters
idThe profile id.
Returns
The built-in profile, or NULL when no built-in profile has the id.

◆ esc_brand_same()

bool esc_brand_same ( const char * a,
const char * b )

Whether a and b name one maker: the letters A to Z folded, every other byte itself, as the list groups them.

Parameters
aA maker name, or NULL.
bA maker name, or NULL.
Returns
true when the names are equal with A to Z folded. Two NULL names are equal; a NULL name and a name are not.

◆ esc_item_applies()

bool esc_item_applies ( const esc_profile_t * p,
unsigned item,
int model )

Whether item item of p is on model model: an item without applies_to is on every model; one with it only on the models it names.

For -1 – the family, no model picked – only an item on every model, as the one item the ESC is sure to have.

Parameters
pThe profile.
itemIndex into the profile's items.
modelIndex into the profile's models, or -1 for none picked.
Returns
true when the item applies

◆ esc_model_matches()

bool esc_model_matches ( const esc_profile_t * p,
unsigned model,
const char * pattern )

Whether pattern finds model model of p: in the maker and the family read as one text, which every model of the family shares, or in the maker and the model's own name, as esc_profile_matches() reads them.

Parameters
pThe profile.
modelIndex into the profile's models.
patternThe pattern, with "*" as a wildcard.
Returns
true when the pattern finds the model

◆ esc_profile_file_is()

bool esc_profile_file_is ( const char * file_name,
const char * id )

Whether file_name is the name the profile id belongs in: the id with ".json" after it, letters compared without regard to case, since a card written on another computer may change them.

A card file whose name and id differ is refused, as tools/gen_esc_profiles.py refuses one in the repository: a renamed copy would otherwise replace a profile its name does not mention.

Parameters
file_nameThe file name as found on the card.
idThe profile id to compare against.
Returns
true when the names match

◆ esc_profile_manual_count()

unsigned esc_profile_manual_count ( const esc_profile_t * p,
esc_manual_when_t when )

How many of the profile's manual steps are due at when.

Parameters
pThe profile.
whenThe point in a run to count for.
Returns
The number of manual steps due at when.

◆ esc_profile_matches()

bool esc_profile_matches ( const esc_profile_t * p,
const char * pattern )

Whether pattern (as esc_text_matches()) is found in the profile's manufacturer and name read as one text, "brand family", or in its manufacturer and one of its models' names, "brand model": a family such as "JAZZ / MINIJAZZ" names its sizes only in its models ("JAZZ 55 LV").

Parameters
pThe profile to search.
patternThe pattern, with "*" as a wildcard.
Returns
true when the pattern is found

◆ esc_profiles_at()

const esc_profile_t * esc_profiles_at ( size_t i)

The profile at index i of the list the bench offers.

Parameters
iIndex into the list, from 0.
Returns
The profile at i, or NULL when i is past the last profile.

◆ esc_profiles_count()

size_t esc_profiles_count ( void )

Every profile the bench offers: the built-in ones with card files in their place, then card profiles with new ids.

Returns
The number of profiles the bench offers.

◆ esc_profiles_find()

const esc_profile_t * esc_profiles_find ( const char * id)

NULL when no profile has this id.

Parameters
idThe profile id.
Returns
The card profile with this id if there is one, else the built-in profile, or NULL when id is NULL or no profile has it.

◆ esc_profiles_is_override()

bool esc_profiles_is_override ( const esc_profile_t * p)

Whether the profile at p came from the card.

Parameters
pThe profile.
Returns
true when p is one of the card profiles.

◆ esc_profiles_override()

bool esc_profiles_override ( const esc_profile_t * p,
void * block )

Add a parsed profile from the card.

Ownership of block passes to the registry. A second file with the same id replaces the first.

Parameters
pThe parsed profile.
blockThe malloc() block that holds p.
Returns
false when the registry is full; block is freed then too

◆ esc_profiles_override_count()

size_t esc_profiles_override_count ( void )

Card profiles held, for a status line.

Returns
The number of card profiles held.

◆ esc_profiles_override_why()

bool esc_profiles_override_why ( const esc_profile_t * p,
void * block,
const char ** why )

esc_profiles_override(), with why it refused in why (a static string; may be NULL): "no profile", "more than 32 on the card", or "its maker would list more than 512 models" – the maker's models across every profile, built in and from the card, with this one in place of any it replaces, makers compared with the letters A to Z folded.

Parameters
pThe parsed profile.
blockThe malloc() block that holds p; freed when the profile is refused.
whyReceives a static reason string when refused, or NULL.
Returns
true when p is registered, false when it is refused.

◆ esc_text_matches()

bool esc_text_matches ( const char * text,
const char * pattern )

Whether pattern is found in text.

Letters A to Z match without regard to case; every other byte matches itself, so "Ü" does not match "ü". "*" stands for any run of characters, none included, and the pattern is found anywhere in the text: "sky*v2" is in "Skywalker V2". An empty pattern, or one of "*" only, is found in every text. A NULL pattern counts as empty and a NULL text as "".

Parameters
textThe text to search.
patternThe pattern, with "*" as a wildcard.
Returns
true when the pattern is found

◆ fold()

unsigned char fold ( unsigned char c)
static

Fold an ASCII capital letter to lower case.

Parameters
cThe byte.
Returns
The lower-case letter for A to Z; every other byte unchanged.

◆ joined_at()

unsigned char joined_at ( const char * a,
size_t na,
const char * b,
size_t i )
static

Byte i of a, a space and b read as one text of n bytes; b NULL is a alone.

No copy is made, since a card profile's strings have no length limit short of the file's.

Parameters
aThe first text.
naLength of a, bytes.
bThe second text, or NULL.
iPosition in the joined text.
Returns
The byte at i.

◆ maker_models_with()

size_t maker_models_with ( const esc_profile_t * p)
static

The models p's maker would list with p in place of the profile of its id.

Parameters
pThe profile, as it would be registered.
Returns
The number of models the maker would list.

◆ match_joined()

bool match_joined ( const char * a,
const char * b,
const char * pattern )
static

The wildcard match, with a "*" implied at either end.

On a mismatch the last "*" takes one byte more and the rest of the pattern is tried again from there: at most text times pattern comparisons, no recursion. A multi-byte character cannot match from its middle, because a UTF-8 lead byte never equals a continuation byte.

Parameters
aThe first text.
bThe second text, joined to a with a space; NULL for none.
patternThe pattern, with "*" as a wildcard; NULL counts as "".
Returns
true when the pattern is found in the joined text.

◆ over_find()

slot_t * over_find ( const char * id)
static

Find a card profile by id.

Parameters
idThe profile id.
Returns
The card slot, or NULL when no card profile has the id.

◆ refuse()

bool refuse ( void * block,
const char ** why,
const char * text )
static

Refuse a card profile: free its block and set the reason.

Parameters
blockThe malloc() block that holds the profile, freed here.
whyReceives text when not NULL.
textThe static reason string.
Returns
false, always.