From 631dfedf1b535b6c3861d3c3868e02f48a84fb06 Mon Sep 17 00:00:00 2001 From: Jason Reiss Date: Wed, 21 Oct 2015 13:51:31 -0500 Subject: create lock files to choose mpsse index in order to support two lora cards --- libloragw/src/loragw_spi.ftdi.c | 39 ++++++++++++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/libloragw/src/loragw_spi.ftdi.c b/libloragw/src/loragw_spi.ftdi.c index d57e764..4f0928a 100644 --- a/libloragw/src/loragw_spi.ftdi.c +++ b/libloragw/src/loragw_spi.ftdi.c @@ -28,6 +28,7 @@ Maintainer: Sylvain Miermont #include #include +#include #include #include "loragw_spi.h" @@ -60,6 +61,8 @@ Maintainer: Sylvain Miermont #define PID 0x6010 #endif +#define LCK_SPI0 "/var/lock/LCK..spi0" +#define LCK_SPI1 "/var/lock/LCK..spi1" /* -------------------------------------------------------------------------- */ /* --- PUBLIC FUNCTIONS DEFINITION ------------------------------------------ */ @@ -78,15 +81,33 @@ int lgw_spi_open(void **spi_target_ptr) { CHECK_NULL(spi_target_ptr); /* cannot be null, must point on a void pointer (*spi_target_ptr can be null) */ /* try to open the first available FTDI device matching VID/PID parameters */ - mpsse = OpenIndex(VID,PID,SPI0, SIX_MHZ, MSB, IFACE_A, NULL, NULL, 0); - if (mpsse == NULL) { - DEBUG_MSG("ERROR: MPSSE OPEN FUNCTION RETURNED NULL\n"); - return LGW_SPI_ERROR; - } - if (mpsse->open != 1) { - DEBUG_MSG("ERROR: MPSSE OPEN FUNCTION FAILED\n"); - return LGW_SPI_ERROR; - } + int i = 0; + + int fd = open(LCK_SPI0, O_RDWR | O_CREAT, 0666); // open or create lockfile + //check open success... + int rc = flock(fd, LOCK_EX | LOCK_NB); // grab exclusive lock, fail if can't obtain. + if (rc) + { + i = 1; + fd = open(LCK_SPI1, O_RDWR | O_CREAT, 0666); // open or create lockfile + //check open success... + rc = flock(fd, LOCK_EX | LOCK_NB); // grab exclusive lock, fail if can't obtain. + + if (rc) { + DEBUG_MSG("ERROR: FAILED TO OBTAIN LOCK ON SPI DEVICE\n"); + return LGW_SPI_ERROR; + } + } + + mpsse = OpenIndex(VID,PID,SPI0, SIX_MHZ, MSB, IFACE_A, NULL, NULL, i); + if (mpsse == NULL) { + DEBUG_MSG("ERROR: MPSSE OPEN FUNCTION RETURNED NULL\n"); + return LGW_SPI_ERROR; + } + if (mpsse->open != 1) { + DEBUG_MSG("ERROR: MPSSE OPEN FUNCTION FAILED\n"); + return LGW_SPI_ERROR; + } /* toggle pin ADBUS5 of the FT2232H */ /* On the Semtech reference board, it resets the SX1301 */ -- cgit v1.2.3