Skip to content

Commit

Permalink
driver: refactor namespace and init logic
Browse files Browse the repository at this point in the history
  • Loading branch information
estkme committed Mar 21, 2024
1 parent b8fedaa commit 94be1bc
Show file tree
Hide file tree
Showing 16 changed files with 34 additions and 41 deletions.
2 changes: 1 addition & 1 deletion driver/apdu/at.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ static void libapduinterface_fini(void)
{
}

const struct lpac_driver driver_apdu_at = {
const struct euicc_driver driver_apdu_at = {
.type = DRIVER_APDU,
.name = "at",
.init = (int (*)(void *))libapduinterface_init,
Expand Down
2 changes: 1 addition & 1 deletion driver/apdu/at.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma once
#include <driver.private.h>

extern const struct lpac_driver driver_apdu_at;
extern const struct euicc_driver driver_apdu_at;
2 changes: 1 addition & 1 deletion driver/apdu/gbinder_hidl.c
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ static void libapduinterface_fini(void)
{
}

const struct lpac_driver driver_apdu_gbinder_hidl = {
const struct euicc_driver driver_apdu_gbinder_hidl = {
.type = DRIVER_APDU,
.name = "gbinder_hidl",
.init = libapduinterface_init,
Expand Down
2 changes: 1 addition & 1 deletion driver/apdu/gbinder_hidl.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma once
#include <driver.private.h>

extern const struct lpac_driver driver_apdu_gbinder_hidl;
extern const struct euicc_driver driver_apdu_gbinder_hidl;
2 changes: 1 addition & 1 deletion driver/apdu/pcsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ static void libapduinterface_fini(void)
{
}

const struct lpac_driver driver_apdu_pcsc = {
const struct euicc_driver driver_apdu_pcsc = {
.type = DRIVER_APDU,
.name = "pcsc",
.init = (int (*)(void *))libapduinterface_init,
Expand Down
2 changes: 1 addition & 1 deletion driver/apdu/pcsc.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma once
#include <driver.private.h>

extern const struct lpac_driver driver_apdu_pcsc;
extern const struct euicc_driver driver_apdu_pcsc;
2 changes: 1 addition & 1 deletion driver/apdu/stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ static void libapduinterface_fini(void)
{
}

const struct lpac_driver driver_apdu_stdio = {
const struct euicc_driver driver_apdu_stdio = {
.type = DRIVER_APDU,
.name = "stdio",
.init = (int (*)(void *))libapduinterface_init,
Expand Down
2 changes: 1 addition & 1 deletion driver/apdu/stdio.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma once
#include <driver.private.h>

extern const struct lpac_driver driver_apdu_stdio;
extern const struct euicc_driver driver_apdu_stdio;
17 changes: 8 additions & 9 deletions driver/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "driver.private.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef LPAC_WITH_APDU_GBINDER
Expand All @@ -21,7 +20,7 @@
#include "driver/apdu/stdio.h"
#include "driver/http/stdio.h"

static const struct lpac_driver *drivers[] = {
static const struct euicc_driver *drivers[] = {
#ifdef LPAC_WITH_APDU_GBINDER
&driver_apdu_gbinder_hidl,
#endif
Expand All @@ -39,19 +38,19 @@ static const struct lpac_driver *drivers[] = {
NULL,
};

static const struct lpac_driver *_driver_apdu = NULL;
static const struct lpac_driver *_driver_http = NULL;
static const struct euicc_driver *_driver_apdu = NULL;
static const struct euicc_driver *_driver_http = NULL;

struct euicc_apdu_interface euicc_driver_interface_apdu;
struct euicc_http_interface euicc_driver_interface_http;
int (*euicc_driver_main_apdu)(int argc, char **argv) = NULL;
int (*euicc_driver_main_http)(int argc, char **argv) = NULL;

static const struct lpac_driver *_find_driver(enum lpac_driver_type type, const char *name)
static const struct euicc_driver *_find_driver(enum euicc_driver_type type, const char *name)
{
for (int i = 0; drivers[i] != NULL; i++)
{
const struct lpac_driver *d = drivers[i];
const struct euicc_driver *d = drivers[i];
if (d->type != type)
{
continue;
Expand All @@ -68,16 +67,16 @@ static const struct lpac_driver *_find_driver(enum lpac_driver_type type, const
return NULL;
}

int euicc_driver_init()
int euicc_driver_init(const char *apdu_driver_name, const char *http_driver_name)
{
_driver_apdu = _find_driver(DRIVER_APDU, getenv("LPAC_APDU"));
_driver_apdu = _find_driver(DRIVER_APDU, apdu_driver_name);
if (_driver_apdu == NULL)
{
fprintf(stderr, "No APDU driver found\n");
return -1;
}

_driver_http = _find_driver(DRIVER_HTTP, getenv("LPAC_HTTP"));
_driver_http = _find_driver(DRIVER_HTTP, http_driver_name);
if (_driver_http == NULL)
{
fprintf(stderr, "No HTTP driver found\n");
Expand Down
2 changes: 1 addition & 1 deletion driver/driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ extern struct euicc_http_interface euicc_driver_interface_http;
extern int (*euicc_driver_main_apdu)(int argc, char **argv);
extern int (*euicc_driver_main_http)(int argc, char **argv);

int euicc_driver_init(void);
int euicc_driver_init(const char *apdu_driver_name, const char *http_driver_name);
void euicc_driver_fini(void);
7 changes: 3 additions & 4 deletions driver/driver.private.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
#pragma once

enum lpac_driver_type
enum euicc_driver_type
{
DRIVER_APDU,
DRIVER_HTTP,
};

struct lpac_driver
struct euicc_driver
{
enum lpac_driver_type type;
enum euicc_driver_type type;
const char *name;
int (*init)(void *interface);
int (*main)(int argc, char **argv);
void (*fini)(void);
};

2 changes: 1 addition & 1 deletion driver/http/curl.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ static void libhttpinterface_fini(void)
{
}

const struct lpac_driver driver_http_curl = {
const struct euicc_driver driver_http_curl = {
.type = DRIVER_HTTP,
.name = "curl",
.init = (int (*)(void *))libhttpinterface_init,
Expand Down
2 changes: 1 addition & 1 deletion driver/http/curl.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma once
#include <driver.private.h>

extern const struct lpac_driver driver_http_curl;
extern const struct euicc_driver driver_http_curl;
2 changes: 1 addition & 1 deletion driver/http/stdio.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ static void libhttpinterface_fini(void)
{
}

const struct lpac_driver driver_http_stdio = {
const struct euicc_driver driver_http_stdio = {
.type = DRIVER_HTTP,
.name = "stdio",
.init = (int (*)(void *))libhttpinterface_init,
Expand Down
2 changes: 1 addition & 1 deletion driver/http/stdio.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#pragma once
#include <driver.private.h>

extern const struct lpac_driver driver_http_stdio;
extern const struct euicc_driver driver_http_stdio;
25 changes: 10 additions & 15 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,17 @@
#include "applet/notification.h"
#include "applet/version.h"

static struct applet_entry applet_apdu = {
.name = "apdu",
.main = NULL,
};
static struct applet_entry applet_http = {
.name = "http",
.main = NULL,
};

static int driver_applet_main(int argc, char **argv)
{
applet_apdu.main = euicc_driver_main_apdu;
applet_http.main = euicc_driver_main_http;
static const struct applet_entry *applets[] = {
&applet_apdu,
&applet_http,
const struct applet_entry *applets[] = {
&(struct applet_entry){
.name = "apdu",
.main = euicc_driver_main_apdu,
},
&(struct applet_entry){
.name = "http",
.main = euicc_driver_main_http,
},
NULL,
};
return applet_entry(argc, argv, applets);
Expand Down Expand Up @@ -79,7 +74,7 @@ int main(int argc, char **argv)

memset(&euicc_ctx, 0, sizeof(euicc_ctx));

if (euicc_driver_init())
if (euicc_driver_init(getenv("LPAC_APDU"), getenv("LPAC_HTTP")))
{
return -1;
}
Expand Down

0 comments on commit 94be1bc

Please sign in to comment.