mirror of
https://github.com/meshtastic/firmware.git
synced 2025-12-29 14:10:53 +00:00
Put a bit of order in the src directory, group and name things appropriately
This commit is contained in:
79
src/platform/portduino/CrossPlatformCryptoEngine.cpp
Normal file
79
src/platform/portduino/CrossPlatformCryptoEngine.cpp
Normal file
@@ -0,0 +1,79 @@
|
||||
#include "AES.h"
|
||||
#include "CTR.h"
|
||||
#include "CryptoEngine.h"
|
||||
#include "configuration.h"
|
||||
|
||||
/** A platform independent AES engine implemented using Tiny-AES
|
||||
*/
|
||||
class CrossPlatformCryptoEngine : public CryptoEngine
|
||||
{
|
||||
|
||||
CTRCommon *ctr = NULL;
|
||||
|
||||
public:
|
||||
CrossPlatformCryptoEngine() {}
|
||||
|
||||
~CrossPlatformCryptoEngine() {}
|
||||
|
||||
/**
|
||||
* Set the key used for encrypt, decrypt.
|
||||
*
|
||||
* As a special case: If all bytes are zero, we assume _no encryption_ and send all data in cleartext.
|
||||
*
|
||||
* @param numBytes must be 16 (AES128), 32 (AES256) or 0 (no crypt)
|
||||
* @param bytes a _static_ buffer that will remain valid for the life of this crypto instance (i.e. this class will cache the
|
||||
* provided pointer)
|
||||
*/
|
||||
virtual void setKey(const CryptoKey &k) override
|
||||
{
|
||||
CryptoEngine::setKey(k);
|
||||
DEBUG_MSG("Installing AES%d key!\n", key.length * 8);
|
||||
if (ctr) {
|
||||
delete ctr;
|
||||
ctr = NULL;
|
||||
}
|
||||
if (key.length != 0) {
|
||||
if (key.length == 16)
|
||||
ctr = new CTR<AES128>();
|
||||
else
|
||||
ctr = new CTR<AES256>();
|
||||
|
||||
ctr->setKey(key.bytes, key.length);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Encrypt a packet
|
||||
*
|
||||
* @param bytes is updated in place
|
||||
*/
|
||||
virtual void encrypt(uint32_t fromNode, uint64_t packetId, size_t numBytes, uint8_t *bytes) override
|
||||
{
|
||||
if (key.length > 0) {
|
||||
//uint8_t stream_block[16];
|
||||
static uint8_t scratch[MAX_BLOCKSIZE];
|
||||
//size_t nc_off = 0;
|
||||
|
||||
// DEBUG_MSG("ESP32 encrypt!\n");
|
||||
initNonce(fromNode, packetId);
|
||||
assert(numBytes <= MAX_BLOCKSIZE);
|
||||
memcpy(scratch, bytes, numBytes);
|
||||
memset(scratch + numBytes, 0,
|
||||
sizeof(scratch) - numBytes); // Fill rest of buffer with zero (in case cypher looks at it)
|
||||
|
||||
ctr->setIV(nonce, sizeof(nonce));
|
||||
ctr->setCounterSize(4);
|
||||
ctr->encrypt(bytes, scratch, numBytes);
|
||||
}
|
||||
}
|
||||
|
||||
virtual void decrypt(uint32_t fromNode, uint64_t packetId, size_t numBytes, uint8_t *bytes) override
|
||||
{
|
||||
// For CTR, the implementation is the same
|
||||
encrypt(fromNode, packetId, numBytes, bytes);
|
||||
}
|
||||
|
||||
private:
|
||||
};
|
||||
|
||||
CryptoEngine *crypto = new CrossPlatformCryptoEngine();
|
||||
104
src/platform/portduino/PortduinoGlue.cpp
Normal file
104
src/platform/portduino/PortduinoGlue.cpp
Normal file
@@ -0,0 +1,104 @@
|
||||
#include "CryptoEngine.h"
|
||||
#include "PortduinoGPIO.h"
|
||||
#include "SPIChip.h"
|
||||
#include "mesh/RF95Interface.h"
|
||||
#include "sleep.h"
|
||||
#include "target_specific.h"
|
||||
|
||||
#include <Utility.h>
|
||||
#include <assert.h>
|
||||
#include <linux/gpio/LinuxGPIOPin.h>
|
||||
|
||||
// FIXME - move setBluetoothEnable into a HALPlatform class
|
||||
|
||||
void setBluetoothEnable(bool on)
|
||||
{
|
||||
// not needed
|
||||
}
|
||||
|
||||
void cpuDeepSleep(uint64_t msecs)
|
||||
{
|
||||
notImplemented("cpuDeepSleep");
|
||||
}
|
||||
|
||||
void updateBatteryLevel(uint8_t level) NOT_IMPLEMENTED("updateBatteryLevel");
|
||||
|
||||
|
||||
/** a simulated pin for busted IRQ hardware
|
||||
* Porduino helper class to do this i2c based polling:
|
||||
*/
|
||||
class PolledIrqPin : public GPIOPin
|
||||
{
|
||||
public:
|
||||
PolledIrqPin() : GPIOPin(LORA_DIO1, "loraIRQ") {}
|
||||
|
||||
/// Read the low level hardware for this pin
|
||||
virtual PinStatus readPinHardware()
|
||||
{
|
||||
if (isrPinStatus < 0)
|
||||
return LOW; // No interrupt handler attached, don't bother polling i2c right now
|
||||
else {
|
||||
extern RadioInterface *rIf; // FIXME, temporary hack until we know if we need to keep this
|
||||
|
||||
assert(rIf);
|
||||
RadioLibInterface *rIf95 = static_cast<RadioLibInterface *>(rIf);
|
||||
bool p = rIf95->isIRQPending();
|
||||
log(SysGPIO, LogDebug, "PolledIrqPin::readPinHardware(%s, %d, %d)", getName(), getPinNum(), p);
|
||||
return p ? HIGH : LOW;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
static GPIOPin *loraIrq;
|
||||
|
||||
/** apps run under portduino can optionally define a portduinoSetup() to
|
||||
* use portduino specific init code (such as gpioBind) to setup portduino on their host machine,
|
||||
* before running 'arduino' code.
|
||||
*/
|
||||
void portduinoSetup()
|
||||
{
|
||||
printf("Setting up Meshtastic on Porduino...\n");
|
||||
|
||||
#ifdef PORTDUINO_LINUX_HARDWARE
|
||||
SPI.begin(); // We need to create SPI
|
||||
bool usePineLora = !spiChip->isSimulated();
|
||||
if(usePineLora) {
|
||||
printf("Connecting to PineLora board...\n");
|
||||
|
||||
// FIXME: remove this hack once interrupts are confirmed to work on new pine64 board
|
||||
// loraIrq = new PolledIrqPin();
|
||||
loraIrq = new LinuxGPIOPin(LORA_DIO1, "ch341", "int", "loraIrq"); // or "err"?
|
||||
loraIrq->setSilent();
|
||||
gpioBind(loraIrq);
|
||||
|
||||
// BUSY hw was busted on current board - just use the simulated pin (which will read low)
|
||||
auto busy = new LinuxGPIOPin(SX126X_BUSY, "ch341", "slct", "loraBusy");
|
||||
busy->setSilent();
|
||||
gpioBind(busy);
|
||||
|
||||
gpioBind(new LinuxGPIOPin(SX126X_RESET, "ch341", "ini", "loraReset"));
|
||||
|
||||
auto loraCs = new LinuxGPIOPin(SX126X_CS, "ch341", "cs0", "loraCs");
|
||||
loraCs->setSilent();
|
||||
gpioBind(loraCs);
|
||||
}
|
||||
else
|
||||
#endif
|
||||
|
||||
{
|
||||
auto fakeBusy = new SimGPIOPin(SX126X_BUSY, "fakeBusy");
|
||||
fakeBusy->writePin(LOW);
|
||||
fakeBusy->setSilent(true);
|
||||
gpioBind(fakeBusy);
|
||||
|
||||
auto cs = new SimGPIOPin(SX126X_CS, "fakeLoraCS");
|
||||
cs->setSilent(true);
|
||||
gpioBind(cs);
|
||||
|
||||
gpioBind(new SimGPIOPin(SX126X_RESET, "fakeLoraReset"));
|
||||
gpioBind(new SimGPIOPin(LORA_DIO1, "fakeLoraIrq"));
|
||||
}
|
||||
|
||||
// gpioBind((new SimGPIOPin(LORA_RESET, "LORA_RESET")));
|
||||
// gpioBind((new SimGPIOPin(RF95_NSS, "RF95_NSS"))->setSilent());
|
||||
}
|
||||
16
src/platform/portduino/architecture.h
Normal file
16
src/platform/portduino/architecture.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#define ARCH_PORTDUINO
|
||||
|
||||
//
|
||||
// defaults for NRF52 architecture
|
||||
//
|
||||
|
||||
//
|
||||
// set HW_VENDOR
|
||||
//
|
||||
|
||||
#define HW_VENDOR HardwareModel_PORTDUINO
|
||||
|
||||
#define HAS_RTC 1
|
||||
#define HAS_WIFI 1
|
||||
Reference in New Issue
Block a user