Skip to content

Commit

Permalink
chore(driver): improve memory management in at driver (#140)
Browse files Browse the repository at this point in the history
* chore(driver): improve memory management in at driver

* chore(driver): AT driver only supports linux

* chore: use malloc to allocate memory

* chore(driver): use macros instead of static variable

* fix: If memory allocation fails return -1
  • Loading branch information
damonto authored Aug 2, 2024
1 parent c86edd5 commit 19df553
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion driver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ include(CMakeDependentOption)
cmake_dependent_option(LPAC_DYNAMIC_DRIVERS "Build lpac/libeuicc driver backends as a dynamic library" OFF "LPAC_DYNAMIC_LIBEUICC" OFF)

option(LPAC_WITH_APDU_PCSC "Build APDU PCSC Backend (requires PCSC libraries)" ON)
option(LPAC_WITH_APDU_AT "Build APDU AT Backend" ON)
option(LPAC_WITH_APDU_AT "Build APDU AT Backend" Linux)
option(LPAC_WITH_APDU_GBINDER "Build APDU Gbinder backend for libhybris devices (requires gbinder headers)" OFF)
option(LPAC_WITH_APDU_QMI "Build QMI backend for Qualcomm devices (requires libqmi)" OFF)
option(LPAC_WITH_APDU_QMI_QRTR "Build QMI-over-QRTR backend for Qualcomm devices (requires libqrtr and libqmi headers)" OFF)
Expand Down
14 changes: 12 additions & 2 deletions driver/apdu/at.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@
#include <euicc/interface.h>
#include <euicc/hexutil.h>

#define AT_BUFFER_SIZE 20480
static FILE *fuart;
static int logic_channel = 0;
static char *buffer;

static int at_expect(char **response, const char *expected)
{
char buffer[1024];
memset(buffer, 0, AT_BUFFER_SIZE);

if (response)
*response = NULL;

while (1)
{
fgets(buffer, sizeof(buffer), fuart);
fgets(buffer, AT_BUFFER_SIZE, fuart);
buffer[strcspn(buffer, "\r\n")] = 0;
if (getenv("AT_DEBUG"))
printf("AT_DEBUG: %s\r\n", buffer);
Expand Down Expand Up @@ -210,6 +212,13 @@ static int libapduinterface_init(struct euicc_apdu_interface *ifstruct)
ifstruct->logic_channel_close = apdu_interface_logic_channel_close;
ifstruct->transmit = apdu_interface_transmit;

buffer = malloc(AT_BUFFER_SIZE);
if (!buffer)
{
fprintf(stderr, "Failed to allocate memory\n");
return -1;
}

return 0;
}

Expand All @@ -220,6 +229,7 @@ static int libapduinterface_main(int argc, char **argv)

static void libapduinterface_fini(struct euicc_apdu_interface *ifstruct)
{
free(buffer);
}

const struct euicc_driver driver_apdu_at = {
Expand Down

0 comments on commit 19df553

Please sign in to comment.