2020-02-01 16:59:16 +00:00
|
|
|
#include <SPI.h>
|
|
|
|
#include <RH_RF95.h>
|
2020-02-01 19:25:07 +00:00
|
|
|
#include <RHMesh.h>
|
|
|
|
#include <assert.h>
|
2020-02-01 16:59:16 +00:00
|
|
|
|
2020-02-02 20:45:32 +00:00
|
|
|
#include <pb_encode.h>
|
|
|
|
#include <pb_decode.h>
|
2020-02-01 16:59:16 +00:00
|
|
|
#include "MeshRadio.h"
|
|
|
|
#include "configuration.h"
|
2020-02-03 17:13:19 +00:00
|
|
|
#include "NodeDB.h"
|
2020-02-01 16:59:16 +00:00
|
|
|
|
2020-02-03 19:15:17 +00:00
|
|
|
// Change to 434.0 or other frequency, must match RX's freq! FIXME, choose a better default value
|
|
|
|
#define RF95_FREQ_US 915.0f
|
2020-02-01 16:59:16 +00:00
|
|
|
|
2020-02-03 19:53:38 +00:00
|
|
|
RadioConfig radioConfig = RadioConfig_init_zero;
|
2020-02-01 16:59:16 +00:00
|
|
|
|
2020-02-02 20:45:32 +00:00
|
|
|
MeshRadio::MeshRadio(MemoryPool<MeshPacket> &_pool, PointerQueue<MeshPacket> &_rxDest)
|
|
|
|
: rf95(NSS_GPIO, DIO0_GPIO),
|
2020-02-03 17:13:19 +00:00
|
|
|
manager(rf95, nodeDB.getNodeNum()),
|
2020-02-02 20:45:32 +00:00
|
|
|
pool(_pool),
|
|
|
|
rxDest(_rxDest),
|
|
|
|
txQueue(MAX_TX_QUEUE)
|
|
|
|
{
|
2020-02-03 19:15:17 +00:00
|
|
|
radioConfig.tx_power = 23;
|
|
|
|
radioConfig.center_freq = RF95_FREQ_US; // FIXME, pull this config from flash
|
2020-02-02 20:45:32 +00:00
|
|
|
}
|
2020-02-01 16:59:16 +00:00
|
|
|
|
2020-02-02 20:45:32 +00:00
|
|
|
bool MeshRadio::init()
|
|
|
|
{
|
2020-02-05 23:37:58 +00:00
|
|
|
#ifdef RESET_GPIO
|
2020-02-02 20:45:32 +00:00
|
|
|
pinMode(RESET_GPIO, OUTPUT); // Deassert reset
|
|
|
|
digitalWrite(RESET_GPIO, HIGH);
|
|
|
|
|
|
|
|
// pulse reset
|
|
|
|
digitalWrite(RESET_GPIO, LOW);
|
|
|
|
delay(10);
|
|
|
|
digitalWrite(RESET_GPIO, HIGH);
|
|
|
|
delay(10);
|
2020-02-05 23:37:58 +00:00
|
|
|
#endif
|
2020-02-02 20:45:32 +00:00
|
|
|
|
|
|
|
if (!manager.init())
|
|
|
|
{
|
2020-02-04 16:17:44 +00:00
|
|
|
DEBUG_MSG("LoRa radio init failed\n");
|
|
|
|
DEBUG_MSG("Uncomment '#define SERIAL_DEBUG' in RH_RF95.cpp for detailed debug info\n");
|
2020-02-02 20:45:32 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-02-01 16:59:16 +00:00
|
|
|
|
2020-02-04 16:17:44 +00:00
|
|
|
DEBUG_MSG("LoRa radio init OK!\n");
|
2020-02-01 19:25:07 +00:00
|
|
|
|
2020-02-02 20:45:32 +00:00
|
|
|
// Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM
|
2020-02-03 19:15:17 +00:00
|
|
|
if (!rf95.setFrequency(radioConfig.center_freq))
|
2020-02-02 20:45:32 +00:00
|
|
|
{
|
2020-02-04 16:17:44 +00:00
|
|
|
DEBUG_MSG("setFrequency failed\n");
|
|
|
|
assert(0); // fixme panic
|
2020-02-02 20:45:32 +00:00
|
|
|
}
|
2020-02-04 16:17:44 +00:00
|
|
|
DEBUG_MSG("Set Freq to: %f\n", radioConfig.center_freq);
|
2020-02-01 19:25:07 +00:00
|
|
|
|
2020-02-02 20:45:32 +00:00
|
|
|
// Defaults after init are 434.0MHz, 13dBm, Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on
|
2020-02-01 19:25:07 +00:00
|
|
|
|
2020-02-02 20:45:32 +00:00
|
|
|
// The default transmitter power is 13dBm, using PA_BOOST.
|
|
|
|
// If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then
|
|
|
|
// you can set transmitter powers from 5 to 23 dBm:
|
|
|
|
// FIXME - can we do this? It seems to be in the Heltec board.
|
2020-02-03 19:15:17 +00:00
|
|
|
rf95.setTxPower(radioConfig.tx_power, false);
|
2020-02-02 02:45:27 +00:00
|
|
|
|
2020-02-02 20:45:32 +00:00
|
|
|
return true;
|
2020-02-01 19:25:07 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 20:45:32 +00:00
|
|
|
ErrorCode MeshRadio::send(MeshPacket *p)
|
|
|
|
{
|
2020-02-06 18:58:19 +00:00
|
|
|
DEBUG_MSG("enquing packet for send from=%d, to=%d\n", p->from, p->to);
|
2020-02-02 21:29:53 +00:00
|
|
|
return txQueue.enqueue(p, 0); // nowait
|
2020-02-01 16:59:16 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 20:45:32 +00:00
|
|
|
ErrorCode MeshRadio::sendTo(NodeNum dest, const uint8_t *buf, size_t len)
|
|
|
|
{
|
2020-02-04 16:17:44 +00:00
|
|
|
DEBUG_MSG("mesh sendTo %d bytes to %d\n", len, dest);
|
2020-02-02 20:45:32 +00:00
|
|
|
// FIXME - for now we do all packets as broadcast
|
|
|
|
dest = NODENUM_BROADCAST;
|
|
|
|
|
2020-02-03 03:08:04 +00:00
|
|
|
assert(len <= 255); // Make sure we don't overflow the tiny max packet size
|
2020-02-02 21:29:53 +00:00
|
|
|
|
2020-02-02 20:45:32 +00:00
|
|
|
// Note: we don't use sendToWait here because we don't want to wait and for the time being don't require
|
|
|
|
// reliable delivery
|
|
|
|
// return manager.sendtoWait((uint8_t *) buf, len, dest);
|
|
|
|
return manager.sendto((uint8_t *)buf, len, dest) ? ERRNO_OK : ERRNO_UNKNOWN;
|
|
|
|
}
|
2020-02-01 16:59:16 +00:00
|
|
|
|
2020-02-03 03:08:04 +00:00
|
|
|
/// enqueue a received packet in rxDest
|
|
|
|
void MeshRadio::handleReceive(MeshPacket *mp)
|
|
|
|
{
|
|
|
|
int res = rxDest.enqueue(mp, 0); // NOWAIT - fixme, if queue is full, delete older messages
|
|
|
|
assert(res == pdTRUE);
|
|
|
|
}
|
|
|
|
|
2020-02-03 19:15:17 +00:00
|
|
|
|
2020-02-02 20:45:32 +00:00
|
|
|
void MeshRadio::loop()
|
2020-02-01 16:59:16 +00:00
|
|
|
{
|
2020-02-02 20:45:32 +00:00
|
|
|
// FIXME read from radio with recvfromAckTimeout
|
2020-02-01 19:25:07 +00:00
|
|
|
|
2020-02-02 20:45:32 +00:00
|
|
|
#if 0
|
|
|
|
static int16_t packetnum = 0; // packet counter, we increment per xmission
|
2020-02-02 00:05:12 +00:00
|
|
|
|
2020-02-01 16:59:16 +00:00
|
|
|
char radiopacket[20] = "Hello World # ";
|
2020-02-01 19:25:07 +00:00
|
|
|
sprintf(radiopacket, "hello %d", packetnum++);
|
2020-02-01 16:59:16 +00:00
|
|
|
|
2020-02-02 20:45:32 +00:00
|
|
|
assert(sendTo(NODENUM_BROADCAST, (uint8_t *)radiopacket, sizeof(radiopacket)) == ERRNO_OK);
|
|
|
|
#endif
|
|
|
|
|
2020-02-02 21:29:53 +00:00
|
|
|
/// A temporary buffer used for sending/receving packets, sized to hold the biggest buffer we might need
|
|
|
|
static uint8_t radiobuf[SubPacket_size];
|
|
|
|
uint8_t rxlen;
|
|
|
|
uint8_t srcaddr, destaddr, id, flags;
|
|
|
|
|
|
|
|
// Poll to see if we've received a packet
|
|
|
|
if (manager.recvfromAckTimeout(radiobuf, &rxlen, 0, &srcaddr, &destaddr, &id, &flags))
|
|
|
|
{
|
|
|
|
// We received a packet
|
2020-02-04 16:17:44 +00:00
|
|
|
DEBUG_MSG("Received packet from mesh src=%d,dest=%d,id=%d,len=%d\n", srcaddr, destaddr, id, rxlen);
|
2020-02-02 21:29:53 +00:00
|
|
|
|
|
|
|
MeshPacket *mp = pool.allocZeroed();
|
|
|
|
assert(mp); // FIXME
|
|
|
|
|
|
|
|
SubPacket *p = &mp->payload;
|
|
|
|
|
|
|
|
mp->from = srcaddr;
|
|
|
|
mp->to = destaddr;
|
2020-02-03 19:15:17 +00:00
|
|
|
if (!pb_decode_from_bytes(radiobuf, rxlen, SubPacket_fields, p))
|
2020-02-02 21:29:53 +00:00
|
|
|
{
|
|
|
|
pool.release(mp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// parsing was successful, queue for our recipient
|
|
|
|
mp->has_payload = true;
|
2020-02-03 03:08:04 +00:00
|
|
|
handleReceive(mp);
|
2020-02-02 21:29:53 +00:00
|
|
|
}
|
|
|
|
}
|
2020-02-02 20:45:32 +00:00
|
|
|
|
2020-02-02 21:29:53 +00:00
|
|
|
// Poll to see if we need to send any packets
|
|
|
|
MeshPacket *txp = txQueue.dequeuePtr(0); // nowait
|
|
|
|
if (txp)
|
|
|
|
{
|
2020-02-04 16:17:44 +00:00
|
|
|
DEBUG_MSG("sending queued packet on mesh\n");
|
2020-02-02 21:29:53 +00:00
|
|
|
assert(txp->has_payload);
|
|
|
|
|
2020-02-03 19:15:17 +00:00
|
|
|
size_t numbytes = pb_encode_to_bytes(radiobuf, sizeof(radiobuf), SubPacket_fields, &txp->payload);
|
|
|
|
|
|
|
|
int res = sendTo(txp->to, radiobuf, numbytes);
|
|
|
|
assert(res == ERRNO_OK);
|
2020-02-02 21:29:53 +00:00
|
|
|
|
2020-02-03 03:08:04 +00:00
|
|
|
bool loopbackTest = false; // if true we will pretend to receive any packets we just sent
|
|
|
|
if (loopbackTest)
|
|
|
|
handleReceive(txp);
|
|
|
|
else
|
|
|
|
pool.release(txp);
|
2020-02-02 21:29:53 +00:00
|
|
|
}
|
|
|
|
}
|