summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJason Reiss <jreiss@multitech.com>2015-10-21 13:51:31 -0500
committerJason Reiss <jreiss@multitech.com>2015-10-21 13:51:31 -0500
commit631dfedf1b535b6c3861d3c3868e02f48a84fb06 (patch)
tree9222c646624d4bf56241f5dc35c3d6b757536c4b
parentd6e01b5a566c08c89f17ae4a26d4ef003e39167c (diff)
downloadlora_gateway-1.7.0-mts.tar.gz
lora_gateway-1.7.0-mts.tar.bz2
lora_gateway-1.7.0-mts.zip
create lock files to choose mpsse index in order to support two lora cards1.7.0-mts
-rw-r--r--libloragw/src/loragw_spi.ftdi.c39
1 files 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 <sys/types.h>
#include <linux/limits.h>
+#include <sys/file.h>
#include <mpsse.h>
#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 */