Skip to content

Commit

Permalink
#72: fix issues with spectfd, extend local API
Browse files Browse the repository at this point in the history
  • Loading branch information
alvieboy committed Apr 16, 2021
1 parent e676dd0 commit eed9504
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
40 changes: 37 additions & 3 deletions esp32/main/spectfd.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "spectfd.h"
#include <string.h>
#include <unistd.h>
#include <stdbool.h>

#define MAX_SPECT_FD 16
#define SPECTFD_RESERVED (0)

static int spectfd_map[MAX_SPECT_FD];

Expand All @@ -11,11 +13,21 @@ void spectfd__init(void)
memset(spectfd_map, 0xFF, sizeof(spectfd_map));
}

static bool spectfd__is_valid_system(int index)
{
return (spectfd_map[index]>SPECTFD_RESERVED);
}

static bool spectfd__is_valid_system_or_reserved(int index)
{
return (spectfd_map[index]>SPECTFD_RESERVED);
}

void spectfd__close_all(void)
{
unsigned i;
for (i=0;i<MAX_SPECT_FD;i++) {
if (spectfd_map[i]<0)
if (!spectfd__is_valid_system(i))
continue;
close(spectfd_map[i]);
spectfd_map[i] = SPECTFD_ERROR;
Expand All @@ -34,11 +46,33 @@ spectfd_t spectfd__alloc(int systemfd)
{
unsigned i;
for (i=0;i<MAX_SPECT_FD;i++) {
if (spectfd_map[i]<0) {
if (spectfd__is_valid_system_or_reserved(i)) {
continue;
}
spectfd_map[i] = systemfd;
spectfd_map[i] = systemfd;
return i;
}
return SPECTFD_ERROR;
}

spectfd_t spectfd__prealloc()
{
return spectfd__alloc( SPECTFD_RESERVED );
}

void spectfd__update_systemfd(spectfd_t fd, int systemfd)
{
spectfd_map[fd] = systemfd;
}

int spectfd__close(spectfd_t fd)
{
int sysfd = spectfd_map[fd];
if (spectfd__is_valid_system(fd))
close(sysfd);

spectfd_map[fd] = SPECTFD_ERROR;
return 0;
}


4 changes: 3 additions & 1 deletion esp32/main/spectfd.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define __SPECTFD_H__

#include <inttypes.h>

/**
* \defgrou Spectrum file descriptors
* \ingroup misc
Expand All @@ -16,5 +15,8 @@ void spectfd__init(void);
void spectfd__close_all(void);
int spectfd__spect_to_system(spectfd_t fd);
spectfd_t spectfd__alloc(int systemfd);
spectfd_t spectfd__prealloc();
void spectfd__update_systemfd(spectfd_t fd, int systemfd);
int spectfd__close(spectfd_t fd);

#endif

0 comments on commit eed9504

Please sign in to comment.