Skip to content

Commit

Permalink
Merge pull request #68 from sudomesh/1.0.0-rc.1
Browse files Browse the repository at this point in the history
1.0.0 rc.1
  • Loading branch information
paidforby authored Apr 14, 2020
2 parents d0f8319 + 440bde1 commit bdcc33c
Show file tree
Hide file tree
Showing 37 changed files with 903 additions and 298 deletions.
2 changes: 1 addition & 1 deletion fetch_deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ cd ../

git clone https://github.com/sudomesh/LoRaLayer2
cd LoRaLayer2/
git checkout 8509821d2a04b5edccaaaab7a6ef3260aa7b2cba
git checkout 0.2.1
cd ../

git clone https://github.com/paidforby/AsyncSDServer
Expand Down
4 changes: 2 additions & 2 deletions firmware/esp32/DisasterClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class DisasterClient

virtual void setup(){};
virtual void loop(){};
virtual void receive(String message){};
virtual void receive(struct Datagram datagram, size_t len){};
};

#endif
#endif
4 changes: 2 additions & 2 deletions firmware/esp32/DisasterHistory.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
class DisasterHistory
{
public:
virtual void record(String message) = 0;
virtual void record(struct Datagram datagram, size_t len) = 0;
virtual void replay(DisasterClient *client) = 0;
};

#endif
#endif
8 changes: 4 additions & 4 deletions firmware/esp32/DisasterMiddleware.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ void DisasterMiddleware::loop()
}
};

void DisasterMiddleware::receive(String message)
void DisasterMiddleware::receive(struct Datagram datagram, size_t len)
{
if (client)
{
client->receive(message);
client->receive(datagram, len);
}
};

void DisasterMiddleware::transmit(DisasterClient *c, String message)
void DisasterMiddleware::transmit(DisasterClient *c, struct Datagram datagram, size_t len)
{
if (server)
{
server->transmit(this, message);
server->transmit(this, datagram, len);
}
};
6 changes: 3 additions & 3 deletions firmware/esp32/DisasterMiddleware.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class DisasterMiddleware : public DisasterClient, public DisasterServer
virtual void setup();
virtual void loop();

virtual void receive(String message);
virtual void transmit(DisasterClient *client, String message);
virtual void receive(struct Datagram datagram, size_t len);
virtual void transmit(DisasterClient *client, struct Datagram datagram, size_t len);
};

#endif
#endif
13 changes: 11 additions & 2 deletions firmware/esp32/DisasterServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@

#include "DisasterClient.h"

#define DATAGRAM_HEADER 5
#define DATAGRAM_MESSAGE 239

struct Datagram {
uint8_t destination[4];
uint8_t type; // 1 byte (typically a char) corresponding to intended client/service
uint8_t message[DATAGRAM_MESSAGE]; // content of message, possibly binary data, not necessarily null-terminated
};

class DisasterClient;

class DisasterServer
Expand All @@ -17,7 +26,7 @@ class DisasterServer
virtual void setup() = 0;
virtual void loop() = 0;

virtual void transmit(DisasterClient *client, String message) = 0;
virtual void transmit(DisasterClient *client, struct Datagram datagram, size_t len) = 0;
};

#endif
#endif
188 changes: 135 additions & 53 deletions firmware/esp32/client/BleUartClient.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#ifdef USE_BLE
#include "BleUartClient.h"
#include "settings/settings.h"

/** Comment out to stop debug output */
// #define DEBUG_OUT

/** Service UUID for Uart */
#define UART_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
Expand Down Expand Up @@ -27,7 +30,7 @@ BLEServer *pServer;
bool deviceConnected = false;

/** Flag if we already registered on the server */
bool connectedToServer = false;
bool notifEnabled = false;

/** Unique device name */
char apName[] = "DR-xxxxxxxxxxxx";
Expand All @@ -40,31 +43,35 @@ bool dataRcvd = false;
extern BleUartClient ble_client;
void (*connectCallback)(BleUartClient *);

void BleUartClient::receive(String message)
void BleUartClient::receive(struct Datagram datagram, size_t len)
{
if (deviceConnected)
{
// /// \todo for debug only
// Serial.printf("BLE: sending %s with len %d\n", message.c_str(), message.length());
// /// \todo end of for debug only

// TODO: msg id? defaulting to 0 for now
uint16_t msg_id = 0x2020;

unsigned char buf[2 + message.length() + 2] = {'\0'};
memcpy(buf, &msg_id, 2);
message.getBytes(buf + 2, message.length() + 1);

// /// \todo for debug only
// Serial.println("BLE: Sending raw data");
// for (int idx = 0; idx < sizeof(buf); idx++)
// {
// Serial.printf("%02X ", buf[idx]);
// }
// Serial.println("");
// /// \todo end of for debug only

pCharacteristicUartTX->setValue(buf, 2 + message.length() + 2);
#ifdef DEBUG_OUT
Serial.printf("BLE:receive %s length %d\n", (char *)datagram.message, len - DATAGRAM_HEADER);
Serial.println("BLE::receive raw data");
for (int idx = 0; idx < len - DATAGRAM_HEADER; idx++)
{
Serial.printf("%02X ", datagram.message[idx]);
}
Serial.println("");
#endif

unsigned char buf[len - DATAGRAM_HEADER] = {'\0'};
memcpy(buf, &datagram.message, len-DATAGRAM_HEADER);

#ifdef DEBUG_OUT
Serial.printf("BLE: Sending raw data with len %d\n", sizeof(buf));
for (int idx = 0; idx < sizeof(buf); idx++)
{
Serial.printf("%02X ", buf[idx]);
}
Serial.println("");

Serial.printf("BLE::receive %s len %d type %c\n", buf, sizeof(buf), datagram.type);
#endif

pCharacteristicUartTX->setValue(buf, sizeof(buf));
pCharacteristicUartTX->notify();

// Give BLE time to get the data out
Expand All @@ -74,29 +81,62 @@ void BleUartClient::receive(String message)

void BleUartClient::handleData(void *data, size_t len)
{
uint16_t msg_id;
char msg[len - 2 + 1] = {'\0'};

// /// \todo for debug only
// char debug[len] = {'\0'};
// memcpy(debug, data, len);
// Serial.println("BLE: Received raw data");
// for (int idx = 0; idx < len; idx++)
// {
// Serial.printf("%02X ", debug[idx]);
// }
// Serial.println("");
// /// \todo end of for debug only

// parse out message and message id
memcpy(&msg_id, data, 2);
memcpy(msg, data + 2, rxLen - 2);

// /// \todo for debug only
// Serial.printf("BLE: received %s len %d\n", msg, len - 2 + 1);
// /// \todo end of for debug only

server->transmit(this, String(msg));
#ifdef DEBUG_OUT
char debug[len] = {'\0'};
memcpy(debug, data, len);
Serial.println("BLE: Received raw data");
for (int idx = 0; idx < len; idx++)
{
Serial.printf("%02X ", debug[idx]);
}
Serial.println("");
#endif

char *msg = (char *)data;

if (msg[2] == 'u')
{
if (msg[4] == '~')
{
// delete username
username = "";
saveUsername(username);
#ifdef DEBUG_OUT
Serial.printf("Deleted username over BLE\n");
#endif
return;
}
else
{
// username set over BLE
username = String(&msg[4]);
Serial.printf("Username set over BLE: %s\n", username.c_str());
saveUsername(username);
return;
}
}

if (msg[2] == 'i')
{
// Switch to WiFi interface
#ifdef DEBUG_OUT
Serial.printf("User wants to switch to WiFi");
#endif
saveUI(false);
delay(500);
ESP.restart();
}

struct Datagram datagram = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
memset(datagram.message, 0, 233);
datagram.type = msg[2];
memcpy(&datagram.message, data, len);

#ifdef DEBUG_OUT
Serial.printf("BLE::handleData %s len %d type %c\n", msg, len, datagram.type);
#endif

server->transmit(this, datagram, len + DATAGRAM_HEADER);
}

/**
Expand All @@ -106,15 +146,20 @@ class MyServerCallbacks : public BLEServerCallbacks
{
void onConnect(BLEServer *pServer)
{
#ifdef DEBUG_OUT
Serial.println("BLE client connected");
#endif
pServer->updatePeerMTU(pServer->getConnId(), 260);
deviceConnected = true;
};

void onDisconnect(BLEServer *pServer)
{
#ifdef DEBUG_OUT
Serial.println("BLE client disconnected");
#endif
deviceConnected = false;
notifEnabled = false;
pAdvertising->start();
}
};
Expand All @@ -134,7 +179,15 @@ class UartTxCbHandler : public BLECharacteristicCallbacks
{
strncpy((char *)rxData, rxValue.c_str(), 512);

Serial.printf("UART write callback received %s\n", (char *)rxData);
#ifdef DEBUG_OUT
Serial.printf("BLE:onWrite write callback received %s length %d\n", (char *)rxData, rxLen);
Serial.println("BLE:onWrite raw data");
for (int idx = 0; idx < rxLen; idx++)
{
Serial.printf("%02X ", rxData[idx]);
}
Serial.println("");
#endif
dataRcvd = true;
}
};
Expand All @@ -148,13 +201,15 @@ class DescriptorCallbacks : public BLEDescriptorCallbacks
descrValue = pDescriptor->getValue();
if (descrValue[0] & (1 << 0))
{
#ifdef DEBUG_OUT
Serial.println("Notifications enabled");
#endif
if (connectCallback)
{
if (!connectedToServer)
if (!notifEnabled)
{
connectCallback(&ble_client);
connectedToServer = true;
notifEnabled = true;
}
else
{
Expand All @@ -165,6 +220,13 @@ class DescriptorCallbacks : public BLEDescriptorCallbacks
}
}
}
else
{
#ifdef DEBUG_OUT
Serial.println("Notifications disabled");
#endif
notifEnabled = false;
}
};
};

Expand All @@ -177,6 +239,25 @@ void BleUartClient::loop(void)
handleData(rxData, rxLen);
dataRcvd = false;
}
if (notifEnabled && !oldStatus)
{
oldStatus = true;
if (!username.isEmpty())
{
Datagram nameDatagram;
nameDatagram.type = 'u';
int msgLen = sprintf((char *)nameDatagram.message, "00u|%s", username.c_str());
receive(nameDatagram, msgLen + DATAGRAM_HEADER);
}
if (history)
{
history->replay(&ble_client);
}
}
if (!notifEnabled && oldStatus)
{
oldStatus = false;
}
}

void BleUartClient::startServer(void (*callback)(BleUartClient *))
Expand All @@ -194,16 +275,19 @@ void BleUartClient::init()
(uint8_t)(uniqueId), (uint8_t)(uniqueId >> 8),
(uint8_t)(uniqueId >> 16), (uint8_t)(uniqueId >> 24),
(uint8_t)(uniqueId >> 32), (uint8_t)(uniqueId >> 40));
#ifdef DEBUG_OUT
Serial.printf("Device name: %s\n", apName);
#endif

// Initialize BLE and set output power
BLEDevice::init(apName);
BLEDevice::setMTU(260);
BLEDevice::setPower(ESP_PWR_LVL_P7);

#ifdef DEBUG_OUT
BLEAddress thisAddress = BLEDevice::getAddress();

Serial.printf("BLE address: %s\n", thisAddress.toString().c_str());
#endif

// Create BLE Server
pServer = BLEDevice::createServer();
Expand Down Expand Up @@ -234,7 +318,6 @@ void BleUartClient::init()
txDescriptor = pCharacteristicUartTX->getDescriptorByUUID("2902");
if (txDescriptor != NULL)
{
Serial.println("Got descriptor for TX as 2902");
txDescriptor->setCallbacks(new DescriptorCallbacks());
}

Expand All @@ -246,4 +329,3 @@ void BleUartClient::init()
pAdvertising->addServiceUUID(UART_UUID);
pAdvertising->start();
}
#endif // #ifdef USE_BLE
Loading

0 comments on commit bdcc33c

Please sign in to comment.