Skip to content

Commit 882d59e

Browse files
committed
Support for autonomous actions
Essential part of the utility is to handle input data and optionally interact with hardware to fetch this data. So the utility have an advaced common interface for data fetching (connectors) and connector configuration is always mandatory. This is due to the fact that connector descriptor accessed via pointer in many places without checking it for validity. But sometime we need an autonomous (self-contained) action that do not interacts with outer word, e.d. do not access hardware, do not read input dumps, etc. Require the user to specify a connector (see above), that will be in fact ignored is senseless. Reworking whole code and check connector pointer each time before the access is not an option. So, add a special action flag that indicates that a particular action is autonomous. If the user has not specified a connector invoking such an autonomous action, then silently setup a special 'stub' connector. This 'stub' connector actually do nothing and its purpose to make init and exit handlers invocation safe without rewriting a lot of code.
1 parent 2c516d8 commit 882d59e

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ TARGET=atheepmgr
44
OBJ=\
55
atheepmgr.o \
66
con_file.o \
7+
con_stub.o \
78
eep_5211.o \
89
eep_5416.o \
910
eep_9285.o \

atheepmgr.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ static int act_reg_write(struct atheepmgr *aem, int argc, char *argv[])
398398

399399
#define ACT_F_DATA (1 << 0) /* Action will interact with EEPROM/OTP data */
400400
#define ACT_F_HW (1 << 1) /* Action require direct HW access */
401+
#define ACT_F_AUTONOMOUS (1 << 2) /* Action do not require input data or HW */
401402

402403
static const struct action {
403404
const char *name;
@@ -657,11 +658,6 @@ int main(int argc, char *argv[])
657658
}
658659
}
659660

660-
if (!aem->con) {
661-
fprintf(stderr, "Connector is not specified\n");
662-
goto exit;
663-
}
664-
665661
if (optind >= argc) {
666662
act = &actions[0];
667663
} else {
@@ -678,6 +674,15 @@ int main(int argc, char *argv[])
678674
optind++;
679675
}
680676

677+
if (!aem->con) {
678+
if (act->flags & ACT_F_AUTONOMOUS) {
679+
aem->con = &con_stub; /* to avoid conn. init crash */
680+
} else {
681+
fprintf(stderr, "Connector is not specified\n");
682+
goto exit;
683+
}
684+
}
685+
681686
if ((act->flags & ACT_F_HW) && !(aem->con->caps & CON_CAP_HW)) {
682687
fprintf(stderr, "%s action require direct HW access, which is not proved by %s connector\n",
683688
act->name, aem->con->name);

atheepmgr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ struct atheepmgr {
256256
extern const struct connector con_file;
257257
extern const struct connector con_mem;
258258
extern const struct connector con_pci;
259+
extern const struct connector con_stub;
259260

260261
extern const struct eepmap eepmap_5211;
261262
extern const struct eepmap eepmap_5416;

con_stub.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2020 Sergey Ryazanov <[email protected]>
3+
*
4+
* Permission to use, copy, modify, and/or distribute this software for any
5+
* purpose with or without fee is hereby granted, provided that the above
6+
* copyright notice and this permission notice appear in all copies.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
*/
16+
17+
#include "atheepmgr.h"
18+
19+
static int stub_init(struct atheepmgr *aem, const char *arg)
20+
{
21+
return 0;
22+
}
23+
24+
static void stub_clean(struct atheepmgr *aem)
25+
{
26+
}
27+
28+
const struct connector con_stub = {
29+
.name = "Stub",
30+
.init = stub_init,
31+
.clean = stub_clean,
32+
};

0 commit comments

Comments
 (0)