Skip to content

Commit

Permalink
GNSS: Use poll instead of signal
Browse files Browse the repository at this point in the history
Implementations using signal may not work concurrently with other libraries
because it affects semaphore-like wait operations. So use poll instead of signal.
  • Loading branch information
SPRESENSE committed Feb 13, 2023
1 parent f4a7c59 commit dc9e135
Showing 1 changed file with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <GNSSPositionData.h>
#include <Arduino.h>
#include <signal.h>

#include <poll.h>

#define SP_GNSS_DEBUG

Expand All @@ -39,6 +39,9 @@
# define PRINT_I(c)
#endif /* SP_GNSS_DEBUG */

//#define SP_GNSS_USE_SIGNAL
#define GNSS_POLL_FD_NUM 1

const char SP_GNSS_DEV_NAME[] = "/dev/gps";
const int SP_GNSS_SIG = 18;
const unsigned int MAGIC_NUMBER = 0xDEADBEEF;
Expand All @@ -47,9 +50,11 @@ const unsigned int BIN_BUF_SIZE = sizeof(GnssPositionData);
SpPrintLevel SpGnss::DebugPrintLevel = PrintNone; /* Print level */
Stream& SpGnss::DebugOut = Serial;

#ifdef SP_GNSS_USE_SIGNAL
static struct cxd56_gnss_signal_setting_s setting;
static sigset_t mask;
static int no_handler = 0;
#endif
static struct cxd56_gnss_positiondata_s *pPosdat = NULL;
static uint32_t crc_table[256];

Expand Down Expand Up @@ -224,10 +229,12 @@ SpGnss::~SpGnss()
(void) end();
}

#ifdef SP_GNSS_USE_SIGNAL
static void signal_handler( int no )
{
no_handler = no;
}
#endif

/**
* @brief Power on GNSS hardware block
Expand All @@ -237,8 +244,6 @@ int SpGnss::begin(void)
{
PRINT_I("SpGnss : begin in\n");

int ret;

if (fd_ < 0)
{
fd_ = open(SP_GNSS_DEV_NAME, O_RDONLY);
Expand All @@ -248,6 +253,8 @@ int SpGnss::begin(void)
return -1;
}
}
#ifdef SP_GNSS_USE_SIGNAL
int ret;

/* Configure mask to notify GNSS signal. */

Expand Down Expand Up @@ -283,7 +290,7 @@ int SpGnss::begin(void)
sa.sa_mask = mask;
sigaction( SP_GNSS_SIG, &sa, 0 );
}

#endif
/* Init CRC. */

make_crc_table();
Expand Down Expand Up @@ -468,6 +475,7 @@ int SpGnss::isUpdate(void)
int SpGnss::waitUpdate(long timeout)
{
int ret = 0;
#ifdef SP_GNSS_USE_SIGNAL
int sig_ret = 0;

/* Check update */
Expand All @@ -491,8 +499,20 @@ int SpGnss::waitUpdate(long timeout)
ret = 1;
no_handler = 0;
}

return ret;
#else
struct pollfd fds[GNSS_POLL_FD_NUM];
int msec;

msec = (timeout > 0) ? (int)(timeout * 1000) : timeout;

fds[0].fd = fd_;
fds[0].events = POLLIN;

ret = poll(fds, GNSS_POLL_FD_NUM, msec);

return (ret > 0) ? 1 : 0;
#endif
}

/**
Expand Down

0 comments on commit dc9e135

Please sign in to comment.